#define _SYS_PERCPU_H_ #define COUNTERS_BOOT_MEMORY(_name, _n) #define COUNTERS_BOOT_INITIALIZER(_) ((void *)0) /* APIs are not copyrightable. This is a stub */ #ifndef CPA_pf_h #define CPA_pf_h 1 #include "pf.h" #endif #ifndef CPA_sys_param_h #define CPA_sys_param_h 1 #include #endif #ifndef CPA_sys_systm_h #define CPA_sys_systm_h 1 #include #endif #ifndef CPA_sys_malloc_h #define CPA_sys_malloc_h 1 #include #endif #ifndef CPA_sys_mbuf_h #define CPA_sys_mbuf_h 1 #include #endif #ifndef CPA_sys_kernel_h #define CPA_sys_kernel_h 1 #include #endif #ifndef CPA_sys_syslog_h #define CPA_sys_syslog_h 1 #include #endif #ifndef CPA_sys_domain_h #define CPA_sys_domain_h 1 #include #endif #ifndef CPA_sys_protosw_h #define CPA_sys_protosw_h 1 #include #endif #ifndef CPA_sys_pool_h #define CPA_sys_pool_h 1 #include #endif #ifndef CPA_sys_percpu_h #define CPA_sys_percpu_h 1 #include #endif #ifndef CPA_sys_sysctl_h #define CPA_sys_sysctl_h 1 #include #endif #ifndef CPA_sys_socket_h #define CPA_sys_socket_h 1 #include #endif #ifndef CPA_sys_socketvar_h #define CPA_sys_socketvar_h 1 #include #endif #ifndef CPA_net_if_h #define CPA_net_if_h 1 #include #endif #ifndef CPA_uvm_uvm_extern_h #define CPA_uvm_uvm_extern_h 1 #include #endif #ifdef DDB #ifndef CPA_machine_db_machdep_h #define CPA_machine_db_machdep_h 1 #include #endif #endif #if NPF > 0 #ifndef CPA_net_pfvar_h #define CPA_net_pfvar_h 1 #include #endif #endif /* NPF > 0 */ /* mbuf stats */ COUNTERS_BOOT_MEMORY(mbstat_boot, MBSTAT_COUNT); struct cpumem *mbstat = COUNTERS_BOOT_INITIALIZER(mbstat_boot); static char mclnames[MCLPOOLS][8]; // XXX int max_linkhdr; /* largest link-level header */ int max_protohdr; /* largest protocol header */ int max_hdr; /* largest link+protocol header */ struct mutex m_extref_mtx = MUTEX_INITIALIZER(IPL_NET); void m_extfree(struct mbuf *); void nmbclust_update(void); void m_zero(struct mbuf *); unsigned int mbuf_mem_limit; /* how much memory can be allocated */ unsigned int mbuf_mem_alloc; /* how much memory has been allocated */ static void (*mextfree_fns[4])(caddr_t, u_int, void *); static u_int num_extfree_fns; int __VERIFIER_nondet_bool(void); /* * Initialize the mbuf allocator. */ void mbinit(void) { // no pools no sorrows } void mbcpuinit() { // no counters nope } void nmbclust_update(void) { // no } /* * Space allocation routines. */ struct mbuf * m_get(int nowait, int type) { struct mbuf *m; if (nowait == M_NOWAIT && __VERIFIER_nondet_bool()) { return (NULL); } m = malloc(sizeof(struct mbuf), 0, 0); m->m_type = type; m->m_next = NULL; m->m_nextpkt = NULL; m->m_data = m->m_dat; m->m_flags = 0; return (m); } struct mbuf * m_gethdr(int nowait, int type) { KDASSERT(type < MT_NTYPES); struct mbuf *m; if (nowait == M_NOWAIT && __VERIFIER_nondet_bool()) { return (NULL); } m = malloc(sizeof(struct mbuf), 0, 0); m->m_type = type; return (m_inithdr(m)); } struct mbuf * m_inithdr(struct mbuf *m) { m->m_next = NULL; m->m_nextpkt = NULL; m->m_data = m->m_pktdat; m->m_flags = M_PKTHDR; memset(&m->m_pkthdr, 0, sizeof(m->m_pkthdr)); // XXX m->m_pkthdr.pf.prio = IFQ_DEFPRIO; return (m); } void m_resethdr(struct mbuf *m) { int len = m->m_pkthdr.len; u_int8_t loopcnt = m->m_pkthdr.ph_loopcnt; KASSERT(m->m_flags & M_PKTHDR); m->m_flags &= (M_EXT|M_PKTHDR|M_EOR|M_EXTWR|M_ZEROIZE); /* delete all mbuf tags to reset the state */ m_tag_delete_chain(m); // XXX #if NPF > 0 pf_mbuf_unlink_state_key(m); // XXX #endif /* NPF > 0 */ /* like m_inithdr(), but keep any associated data and mbufs */ memset(&m->m_pkthdr, 0, sizeof(m->m_pkthdr)); // XXX m->m_pkthdr.pf.prio = IFQ_DEFPRIO; m->m_pkthdr.len = len; m->m_pkthdr.ph_loopcnt = loopcnt; } struct mbuf * m_getclr(int nowait, int type) { struct mbuf *m; MGET(m, nowait, type); if (m == NULL) return (NULL); memset(mtod(m, caddr_t), 0, MLEN); // XXX return (m); } struct mbuf * m_clget(struct mbuf *m, int how, u_int pktlen) { struct mbuf *m0 = NULL; caddr_t buf; /* XXX Assuming that m_clpool stuff usually works */ if (pktlen > MAXMCLBYTES) panic("m_clget: request for %u byte cluster", pktlen); if (m == NULL) { m0 = m_gethdr(how, MT_DATA); if (m0 == NULL) return (NULL); m = m0; } if (how == M_NOWAIT && __VERIFIER_nondet_bool()) { m_freem(m0); return (NULL); } buf = malloc(sizeof(struct mbuf), 0, m); (m)->m_hdr.mh_data = (m)->M_dat.MH.MH_dat.MH_ext.ext_buf = (caddr_t)(buf); (m)->m_hdr.mh_flags |= 0x0001 | (0x0008 & 0x0008); (m)->M_dat.MH.MH_dat.MH_ext.ext_size = MAXMCLBYTES; (m)->M_dat.MH.MH_dat.MH_ext.ext_free_fn = (0); (m)->M_dat.MH.MH_dat.MH_ext.ext_arg = (((void *)0)); (m)->M_dat.MH.MH_dat.MH_ext.ext_prevref = (m); (m)->M_dat.MH.MH_dat.MH_ext.ext_nextref = (m); return (m); } void m_extfree_pool(caddr_t buf, u_int size, void *pp) { free(buf, M_TEMP, size); } struct mbuf * m_free(struct mbuf *m) { struct mbuf *n; if (m == NULL) return (NULL); n = m->m_next; if (m->m_flags & M_ZEROIZE) { m_zero(m); // XXX /* propagate M_ZEROIZE to the next mbuf in the chain */ if (n) n->m_flags |= M_ZEROIZE; } if (m->m_flags & M_PKTHDR) { m_tag_delete_chain(m); // XXX #if NPF > 0 pf_mbuf_unlink_state_key(m); // XXX #endif /* NPF > 0 */ } if (m->m_flags & M_EXT) m_extfree(m); // XXX free(m, M_TEMP, sizeof(struct mbuf)); return (n); } void m_extref(struct mbuf *o, struct mbuf *n) { int refs = MCLISREFERENCED(o); n->m_flags |= o->m_flags & (M_EXT|M_EXTWR); if (refs) mtx_enter(&m_extref_mtx); n->m_ext.ext_nextref = o->m_ext.ext_nextref; n->m_ext.ext_prevref = o; o->m_ext.ext_nextref = n; n->m_ext.ext_nextref->m_ext.ext_prevref = n; if (refs) mtx_leave(&m_extref_mtx); MCLREFDEBUGN((n), __FILE__, __LINE__); } static inline u_int m_extunref(struct mbuf *m) { int refs = 1; if (!MCLISREFERENCED(m)) return (0); mtx_enter(&m_extref_mtx); if (MCLISREFERENCED(m)) { m->m_ext.ext_nextref->m_ext.ext_prevref = m->m_ext.ext_prevref; m->m_ext.ext_prevref->m_ext.ext_nextref = m->m_ext.ext_nextref; } else refs = 0; mtx_leave(&m_extref_mtx); return (refs); } /* * Returns a number for use with MEXTADD. * Should only be called once per function. * Drivers can be assured that the index will be non zero. */ u_int mextfree_register(void (*fn)(caddr_t, u_int, void *)) { KASSERT(num_extfree_fns < nitems(mextfree_fns)); mextfree_fns[num_extfree_fns] = fn; return num_extfree_fns++; } void m_extfree(struct mbuf *m) { if (m_extunref(m) == 0) { KASSERT(m->m_ext.ext_free_fn < num_extfree_fns); mextfree_fns[m->m_ext.ext_free_fn](m->m_ext.ext_buf, m->m_ext.ext_size, m->m_ext.ext_arg); } m->m_flags &= ~(M_EXT|M_EXTWR); } struct mbuf * m_freem(struct mbuf *m) { struct mbuf *n; if (m == NULL) return (NULL); n = m->m_nextpkt; do m = m_free(m); while (m != NULL); return (n); } void m_purge(struct mbuf *m) { while (m != NULL) m = m_freem(m); } /* * mbuf chain defragmenter. This function uses some evil tricks to defragment * an mbuf chain into a single buffer without changing the mbuf pointer. * This needs to know a lot of the mbuf internals to make this work. */ int m_defrag(struct mbuf *m, int how) { struct mbuf *m0; if (m->m_next == NULL) return (0); KASSERT(m->m_flags & M_PKTHDR); if ((m0 = m_gethdr(how, m->m_type)) == NULL) return (ENOBUFS); if (m->m_pkthdr.len > MHLEN) { MCLGETI(m0, how, NULL, m->m_pkthdr.len); if (!(m0->m_flags & M_EXT)) { m_free(m0); return (ENOBUFS); } } m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t)); m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len; /* free chain behind and possible ext buf on the first mbuf */ m_freem(m->m_next); m->m_next = NULL; if (m->m_flags & M_EXT) m_extfree(m); /* * Bounce copy mbuf over to the original mbuf and set everything up. * This needs to reset or clear all pointers that may go into the * original mbuf chain. */ if (m0->m_flags & M_EXT) { memcpy(&m->m_ext, &m0->m_ext, sizeof(struct mbuf_ext)); MCLINITREFERENCE(m); m->m_flags |= m0->m_flags & (M_EXT|M_EXTWR); m->m_data = m->m_ext.ext_buf; } else { m->m_data = m->m_pktdat; memcpy(m->m_data, m0->m_data, m0->m_len); } m->m_pkthdr.len = m->m_len = m0->m_len; m0->m_flags &= ~(M_EXT|M_EXTWR); /* cluster is gone */ m_free(m0); return (0); } /* * Mbuffer utility routines. */ /* * Ensure len bytes of contiguous space at the beginning of the mbuf chain */ struct mbuf * m_prepend(struct mbuf *m, int len, int how) { struct mbuf *mn; if (len > MHLEN) panic("mbuf prepend length too big"); if (M_LEADINGSPACE(m) >= len) { m->m_data -= len; m->m_len += len; } else { MGET(mn, how, m->m_type); if (mn == NULL) { m_freem(m); return (NULL); } if (m->m_flags & M_PKTHDR) M_MOVE_PKTHDR(mn, m); mn->m_next = m; m = mn; MH_ALIGN(m, len); m->m_len = len; } if (m->m_flags & M_PKTHDR) m->m_pkthdr.len += len; return (m); } /* * Make a copy of an mbuf chain starting "off" bytes from the beginning, * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller. */ struct mbuf * m_copym(struct mbuf *m0, int off, int len, int wait) { struct mbuf *m, *n, **np; struct mbuf *top; int copyhdr = 0; if (off < 0 || len < 0) panic("m_copym0: off %d, len %d", off, len); if (off == 0 && m0->m_flags & M_PKTHDR) copyhdr = 1; if ((m = m_getptr(m0, off, &off)) == NULL) panic("m_copym0: short mbuf chain"); np = ⊤ top = NULL; while (len > 0) { if (m == NULL) { if (len != M_COPYALL) panic("m_copym0: m == NULL and not COPYALL"); break; } MGET(n, wait, m->m_type); *np = n; if (n == NULL) goto nospace; if (copyhdr) { if (m_dup_pkthdr(n, m0, wait)) goto nospace; if (len != M_COPYALL) n->m_pkthdr.len = len; copyhdr = 0; } n->m_len = min(len, m->m_len - off); if (m->m_flags & M_EXT) { n->m_data = m->m_data + off; n->m_ext = m->m_ext; MCLADDREFERENCE(m, n); } else { n->m_data += m->m_data - (m->m_flags & M_PKTHDR ? m->m_pktdat : m->m_dat); n->m_data += off; memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, n->m_len); } if (len != M_COPYALL) len -= n->m_len; off += n->m_len; #ifdef DIAGNOSTIC if (off > m->m_len) panic("m_copym0 overrun"); #endif if (off == m->m_len) { m = m->m_next; off = 0; } np = &n->m_next; } return (top); nospace: m_freem(top); return (NULL); } /* * Copy data from an mbuf chain starting "off" bytes from the beginning, * continuing for "len" bytes, into the indicated buffer. */ void m_copydata(struct mbuf *m, int off, int len, caddr_t cp) { unsigned count; if (off < 0) panic("m_copydata: off %d < 0", off); if (len < 0) panic("m_copydata: len %d < 0", len); if ((m = m_getptr(m, off, &off)) == NULL) panic("m_copydata: short mbuf chain"); while (len > 0) { if (m == NULL) panic("m_copydata: null mbuf"); count = min(m->m_len - off, len); memmove(cp, mtod(m, caddr_t) + off, count); len -= count; cp += count; off = 0; m = m->m_next; } } /* * Copy data from a buffer back into the indicated mbuf chain, * starting "off" bytes from the beginning, extending the mbuf * chain if necessary. The mbuf needs to be properly initialized * including the setting of m_len. */ int m_copyback(struct mbuf *m0, int off, int len, const void *_cp, int wait) { int mlen, totlen = 0; struct mbuf *m = m0, *n; caddr_t cp = (caddr_t)_cp; int error = 0; if (m0 == NULL) return (0); while (off > (mlen = m->m_len)) { off -= mlen; totlen += mlen; if (m->m_next == NULL) { if ((n = m_get(wait, m->m_type)) == NULL) { error = ENOBUFS; goto out; } if (off + len > MLEN) { MCLGETI(n, wait, NULL, off + len); if (!(n->m_flags & M_EXT)) { m_free(n); error = ENOBUFS; goto out; } } memset(mtod(n, caddr_t), 0, off); n->m_len = len + off; m->m_next = n; } m = m->m_next; } while (len > 0) { /* extend last packet to be filled fully */ if (m->m_next == NULL && (len > m->m_len - off)) m->m_len += min(len - (m->m_len - off), M_TRAILINGSPACE(m)); mlen = min(m->m_len - off, len); memmove(mtod(m, caddr_t) + off, cp, mlen); cp += mlen; len -= mlen; totlen += mlen + off; if (len == 0) break; off = 0; if (m->m_next == NULL) { if ((n = m_get(wait, m->m_type)) == NULL) { error = ENOBUFS; goto out; } if (len > MLEN) { MCLGETI(n, wait, NULL, len); if (!(n->m_flags & M_EXT)) { m_free(n); error = ENOBUFS; goto out; } } n->m_len = len; m->m_next = n; } m = m->m_next; } out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) m->m_pkthdr.len = totlen; return (error); } /* * Concatenate mbuf chain n to m. * n might be copied into m (when n->m_len is small), therefore data portion of * n could be copied into an mbuf of different mbuf type. * Therefore both chains should be of the same type (e.g. MT_DATA). * Any m_pkthdr is not updated. */ void m_cat(struct mbuf *m, struct mbuf *n) { while (m->m_next) m = m->m_next; while (n) { if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) { /* just join the two chains */ m->m_next = n; return; } /* splat the data from one into the other */ memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t), n->m_len); m->m_len += n->m_len; n = m_free(n); } } void m_adj(struct mbuf *mp, int req_len) { int len = req_len; struct mbuf *m; int count; if (mp == NULL) return; if (len >= 0) { /* * Trim from head. */ m = mp; while (m != NULL && len > 0) { if (m->m_len <= len) { len -= m->m_len; m->m_data += m->m_len; m->m_len = 0; m = m->m_next; } else { m->m_data += len; m->m_len -= len; len = 0; } } if (mp->m_flags & M_PKTHDR) mp->m_pkthdr.len -= (req_len - len); } else { /* * Trim from tail. Scan the mbuf chain, * calculating its length and finding the last mbuf. * If the adjustment only affects this mbuf, then just * adjust and return. Otherwise, rescan and truncate * after the remaining size. */ len = -len; count = 0; m = mp; for (;;) { count += m->m_len; if (m->m_next == NULL) break; m = m->m_next; } if (m->m_len >= len) { m->m_len -= len; if (mp->m_flags & M_PKTHDR) mp->m_pkthdr.len -= len; return; } count -= len; if (count < 0) count = 0; /* * Correct length for chain is "count". * Find the mbuf with last data, adjust its length, * and toss data from remaining mbufs on chain. */ if (mp->m_flags & M_PKTHDR) mp->m_pkthdr.len = count; m = mp; for (;;) { if (m->m_len >= count) { m->m_len = count; break; } count -= m->m_len; m = m->m_next; } while ((m = m->m_next) != NULL) m->m_len = 0; } } /* * Rearrange an mbuf chain so that len bytes are contiguous * and in the data area of an mbuf (so that mtod will work * for a structure of size len). Returns the resulting * mbuf chain on success, frees it and returns null on failure. */ struct mbuf * m_pullup(struct mbuf *n, int len) { struct mbuf *m; unsigned int adj; caddr_t head, tail; unsigned int space; /* if n is already contig then don't do any work */ if (len <= n->m_len) return (n); adj = (unsigned long)n->m_data & ALIGNBYTES; head = (caddr_t)ALIGN(mtod(n, caddr_t) - M_LEADINGSPACE(n)) + adj; tail = mtod(n, caddr_t) + n->m_len + M_TRAILINGSPACE(n); if (head < tail && len <= tail - head) { /* there's enough space in the first mbuf */ if (len > tail - mtod(n, caddr_t)) { /* need to memmove to make space at the end */ memmove(head, mtod(n, caddr_t), n->m_len); n->m_data = head; } len -= n->m_len; m = n; n = m->m_next; } else { /* the first mbuf is too small so prepend one with space */ space = adj + len; if (space > MAXMCLBYTES) goto bad; MGET(m, M_DONTWAIT, n->m_type); if (m == NULL) goto bad; if (space > MHLEN) { MCLGETI(m, M_DONTWAIT, NULL, space); if ((m->m_flags & M_EXT) == 0) { m_free(m); goto bad; } } if (n->m_flags & M_PKTHDR) M_MOVE_PKTHDR(m, n); m->m_len = 0; m->m_data += adj; } KASSERT(M_TRAILINGSPACE(m) >= len); do { if (n == NULL) { (void)m_free(m); goto bad; } space = min(len, n->m_len); memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t), space); len -= space; m->m_len += space; n->m_len -= space; if (n->m_len > 0) n->m_data += space; else n = m_free(n); } while (len > 0); m->m_next = n; return (m); bad: m_freem(n); return (NULL); } /* * Return a pointer to mbuf/offset of location in mbuf chain. */ struct mbuf * m_getptr(struct mbuf *m, int loc, int *off) { while (loc >= 0) { /* Normal end of search */ if (m->m_len > loc) { *off = loc; return (m); } else { loc -= m->m_len; if (m->m_next == NULL) { if (loc == 0) { /* Point at the end of valid data */ *off = m->m_len; return (m); } else { return (NULL); } } else { m = m->m_next; } } } return (NULL); } /* * Partition an mbuf chain in two pieces, returning the tail -- * all but the first len0 bytes. In case of failure, it returns NULL and * attempts to restore the chain to its original state. */ struct mbuf * m_split(struct mbuf *m0, int len0, int wait) { struct mbuf *m, *n; unsigned len = len0, remain, olen; for (m = m0; m && len > m->m_len; m = m->m_next) len -= m->m_len; if (m == NULL) return (NULL); remain = m->m_len - len; if (m0->m_flags & M_PKTHDR) { MGETHDR(n, wait, m0->m_type); if (n == NULL) return (NULL); if (m_dup_pkthdr(n, m0, wait)) { m_freem(n); return (NULL); } n->m_pkthdr.len -= len0; olen = m0->m_pkthdr.len; m0->m_pkthdr.len = len0; if (remain == 0) { n->m_next = m->m_next; m->m_next = NULL; n->m_len = 0; return (n); } if (m->m_flags & M_EXT) goto extpacket; if (remain > MHLEN) { /* m can't be the lead packet */ MH_ALIGN(n, 0); n->m_next = m_split(m, len, wait); if (n->m_next == NULL) { (void) m_free(n); m0->m_pkthdr.len = olen; return (NULL); } else { n->m_len = 0; return (n); } } else MH_ALIGN(n, remain); } else if (remain == 0) { n = m->m_next; m->m_next = NULL; return (n); } else { MGET(n, wait, m->m_type); if (n == NULL) return (NULL); M_ALIGN(n, remain); } extpacket: if (m->m_flags & M_EXT) { n->m_ext = m->m_ext; MCLADDREFERENCE(m, n); n->m_data = m->m_data + len; } else { memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain); } n->m_len = remain; m->m_len = len; n->m_next = m->m_next; m->m_next = NULL; return (n); } /* * Make space for a new header of length hlen at skip bytes * into the packet. When doing this we allocate new mbufs only * when absolutely necessary. The mbuf where the new header * is to go is returned together with an offset into the mbuf. * If NULL is returned then the mbuf chain may have been modified; * the caller is assumed to always free the chain. */ struct mbuf * m_makespace(struct mbuf *m0, int skip, int hlen, int *off) { struct mbuf *m; unsigned remain; KASSERT(m0->m_flags & M_PKTHDR); /* * Limit the size of the new header to MHLEN. In case * skip = 0 and the first buffer is not a cluster this * is the maximum space available in that mbuf. * In other words this code never prepends a mbuf. */ KASSERT(hlen < MHLEN); for (m = m0; m && skip > m->m_len; m = m->m_next) skip -= m->m_len; if (m == NULL) return (NULL); /* * At this point skip is the offset into the mbuf m * where the new header should be placed. Figure out * if there's space to insert the new header. If so, * and copying the remainder makes sense then do so. * Otherwise insert a new mbuf in the chain, splitting * the contents of m as needed. */ remain = m->m_len - skip; /* data to move */ if (skip < remain && hlen <= M_LEADINGSPACE(m)) { if (skip) memmove(m->m_data-hlen, m->m_data, skip); m->m_data -= hlen; m->m_len += hlen; *off = skip; } else if (hlen > M_TRAILINGSPACE(m)) { struct mbuf *n; if (remain > 0) { MGET(n, M_DONTWAIT, m->m_type); if (n && remain > MLEN) { MCLGETI(n, M_DONTWAIT, NULL, remain); if ((n->m_flags & M_EXT) == 0) { m_free(n); n = NULL; } } if (n == NULL) return (NULL); memcpy(n->m_data, mtod(m, char *) + skip, remain); n->m_len = remain; m->m_len -= remain; n->m_next = m->m_next; m->m_next = n; } if (hlen <= M_TRAILINGSPACE(m)) { m->m_len += hlen; *off = skip; } else { n = m_get(M_DONTWAIT, m->m_type); if (n == NULL) return NULL; n->m_len = hlen; n->m_next = m->m_next; m->m_next = n; *off = 0; /* header is at front ... */ m = n; /* ... of new mbuf */ } } else { /* * Copy the remainder to the back of the mbuf * so there's space to write the new header. */ if (remain > 0) memmove(mtod(m, caddr_t) + skip + hlen, mtod(m, caddr_t) + skip, remain); m->m_len += hlen; *off = skip; } m0->m_pkthdr.len += hlen; /* adjust packet length */ return m; } /* * Routine to copy from device local memory into mbufs. */ struct mbuf * m_devget(char *buf, int totlen, int off) { struct mbuf *m; struct mbuf *top, **mp; int len; top = NULL; mp = ⊤ if (off < 0 || off > MHLEN) return (NULL); MGETHDR(m, M_DONTWAIT, MT_DATA); if (m == NULL) return (NULL); m->m_pkthdr.len = totlen; len = MHLEN; while (totlen > 0) { if (top != NULL) { MGET(m, M_DONTWAIT, MT_DATA); if (m == NULL) { /* * As we might get called by pfkey, make sure * we do not leak sensitive data. */ top->m_flags |= M_ZEROIZE; m_freem(top); return (NULL); } len = MLEN; } if (totlen + off >= MINCLSIZE) { MCLGET(m, M_DONTWAIT); if (m->m_flags & M_EXT) len = MCLBYTES; } else { /* Place initial small packet/header at end of mbuf. */ if (top == NULL && totlen + off + max_linkhdr <= len) { m->m_data += max_linkhdr; len -= max_linkhdr; } } if (off) { m->m_data += off; len -= off; off = 0; } m->m_len = len = min(totlen, len); memcpy(mtod(m, void *), buf, (size_t)len); buf += len; *mp = m; mp = &m->m_next; totlen -= len; } return (top); } void m_zero(struct mbuf *m) { #ifdef DIAGNOSTIC if (M_READONLY(m)) panic("m_zero: M_READONLY"); #endif /* DIAGNOSTIC */ if (m->m_flags & M_EXT) explicit_bzero(m->m_ext.ext_buf, m->m_ext.ext_size); else { if (m->m_flags & M_PKTHDR) explicit_bzero(m->m_pktdat, MHLEN); else explicit_bzero(m->m_dat, MLEN); } } /* * Apply function f to the data in an mbuf chain starting "off" bytes from the * beginning, continuing for "len" bytes. */ int m_apply(struct mbuf *m, int off, int len, int (*f)(caddr_t, caddr_t, unsigned int), caddr_t fstate) { int rval; unsigned int count; if (len < 0) panic("m_apply: len %d < 0", len); if (off < 0) panic("m_apply: off %d < 0", off); while (off > 0) { if (m == NULL) panic("m_apply: null mbuf in skip"); if (off < m->m_len) break; off -= m->m_len; m = m->m_next; } while (len > 0) { if (m == NULL) panic("m_apply: null mbuf"); count = min(m->m_len - off, len); rval = f(fstate, mtod(m, caddr_t) + off, count); if (rval) return (rval); len -= count; off = 0; m = m->m_next; } return (0); } int m_leadingspace(struct mbuf *m) { if (M_READONLY(m)) return 0; return (m->m_flags & M_EXT ? m->m_data - m->m_ext.ext_buf : m->m_flags & M_PKTHDR ? m->m_data - m->m_pktdat : m->m_data - m->m_dat); } int m_trailingspace(struct mbuf *m) { if (M_READONLY(m)) return 0; return (m->m_flags & M_EXT ? m->m_ext.ext_buf + m->m_ext.ext_size - (m->m_data + m->m_len) : &m->m_dat[MLEN] - (m->m_data + m->m_len)); } /* * Duplicate mbuf pkthdr from from to to. * from must have M_PKTHDR set, and to must be empty. */ int m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int wait) { int error; KASSERT(from->m_flags & M_PKTHDR); to->m_flags = (to->m_flags & (M_EXT | M_EXTWR)); to->m_flags |= (from->m_flags & M_COPYFLAGS); to->m_pkthdr = from->m_pkthdr; #if NPF > 0 to->m_pkthdr.pf.statekey = NULL; pf_mbuf_link_state_key(to, from->m_pkthdr.pf.statekey); #endif /* NPF > 0 */ SLIST_INIT(&to->m_pkthdr.ph_tags); if ((error = m_tag_copy_chain(to, from, wait)) != 0) return (error); if ((to->m_flags & M_EXT) == 0) to->m_data = to->m_pktdat; return (0); } struct mbuf * m_dup_pkt(struct mbuf *m0, unsigned int adj, int wait) { struct mbuf *m; int len; KASSERT(m0->m_flags & M_PKTHDR); len = m0->m_pkthdr.len + adj; if (len > MAXMCLBYTES) /* XXX */ return (NULL); m = m_get(wait, m0->m_type); if (m == NULL) return (NULL); if (m_dup_pkthdr(m, m0, wait) != 0) goto fail; if (len > MHLEN) { MCLGETI(m, wait, NULL, len); if (!ISSET(m->m_flags, M_EXT)) goto fail; } m->m_len = m->m_pkthdr.len = len; m_adj(m, adj); m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t)); return (m); fail: m_freem(m); return (NULL); } #ifdef DDB void m_print(void *v, int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) { struct mbuf *m = v; (*pr)("mbuf %p\n", m); (*pr)("m_type: %i\tm_flags: %b\n", m->m_type, m->m_flags, M_BITS); (*pr)("m_next: %p\tm_nextpkt: %p\n", m->m_next, m->m_nextpkt); (*pr)("m_data: %p\tm_len: %u\n", m->m_data, m->m_len); (*pr)("m_dat: %p\tm_pktdat: %p\n", m->m_dat, m->m_pktdat); if (m->m_flags & M_PKTHDR) { (*pr)("m_ptkhdr.ph_ifidx: %u\tm_pkthdr.len: %i\n", m->m_pkthdr.ph_ifidx, m->m_pkthdr.len); (*pr)("m_ptkhdr.ph_tags: %p\tm_pkthdr.ph_tagsset: %b\n", SLIST_FIRST(&m->m_pkthdr.ph_tags), m->m_pkthdr.ph_tagsset, MTAG_BITS); (*pr)("m_pkthdr.ph_flowid: %u\tm_pkthdr.ph_loopcnt: %u\n", m->m_pkthdr.ph_flowid, m->m_pkthdr.ph_loopcnt); (*pr)("m_pkthdr.csum_flags: %b\n", m->m_pkthdr.csum_flags, MCS_BITS); (*pr)("m_pkthdr.ether_vtag: %u\tm_ptkhdr.ph_rtableid: %u\n", m->m_pkthdr.ether_vtag, m->m_pkthdr.ph_rtableid); (*pr)("m_pkthdr.pf.statekey: %p\tm_pkthdr.pf.inp %p\n", m->m_pkthdr.pf.statekey, m->m_pkthdr.pf.inp); (*pr)("m_pkthdr.pf.qid: %u\tm_pkthdr.pf.tag: %u\n", m->m_pkthdr.pf.qid, m->m_pkthdr.pf.tag); (*pr)("m_pkthdr.pf.flags: %b\n", m->m_pkthdr.pf.flags, MPF_BITS); (*pr)("m_pkthdr.pf.routed: %u\tm_pkthdr.pf.prio: %u\n", m->m_pkthdr.pf.routed, m->m_pkthdr.pf.prio); } if (m->m_flags & M_EXT) { (*pr)("m_ext.ext_buf: %p\tm_ext.ext_size: %u\n", m->m_ext.ext_buf, m->m_ext.ext_size); (*pr)("m_ext.ext_free_fn: %u\tm_ext.ext_arg: %p\n", m->m_ext.ext_free_fn, m->m_ext.ext_arg); (*pr)("m_ext.ext_nextref: %p\tm_ext.ext_prevref: %p\n", m->m_ext.ext_nextref, m->m_ext.ext_prevref); } } #endif /* * mbuf lists */ void ml_init(struct mbuf_list *ml) { ml->ml_head = ml->ml_tail = NULL; ml->ml_len = 0; } void ml_enqueue(struct mbuf_list *ml, struct mbuf *m) { if (ml->ml_tail == NULL) ml->ml_head = ml->ml_tail = m; else { ml->ml_tail->m_nextpkt = m; ml->ml_tail = m; } m->m_nextpkt = NULL; ml->ml_len++; } void ml_enlist(struct mbuf_list *mla, struct mbuf_list *mlb) { if (!ml_empty(mlb)) { if (ml_empty(mla)) mla->ml_head = mlb->ml_head; else mla->ml_tail->m_nextpkt = mlb->ml_head; mla->ml_tail = mlb->ml_tail; mla->ml_len += mlb->ml_len; ml_init(mlb); } } struct mbuf * ml_dequeue(struct mbuf_list *ml) { struct mbuf *m; m = ml->ml_head; if (m != NULL) { ml->ml_head = m->m_nextpkt; if (ml->ml_head == NULL) ml->ml_tail = NULL; m->m_nextpkt = NULL; ml->ml_len--; } return (m); } struct mbuf * ml_dechain(struct mbuf_list *ml) { struct mbuf *m0; m0 = ml->ml_head; ml_init(ml); return (m0); } unsigned int ml_purge(struct mbuf_list *ml) { struct mbuf *m, *n; unsigned int len; for (m = ml->ml_head; m != NULL; m = n) { n = m->m_nextpkt; m_freem(m); } len = ml->ml_len; ml_init(ml); return (len); } /* * mbuf queues */ void mq_init(struct mbuf_queue *mq, u_int maxlen, int ipl) { mtx_init(&mq->mq_mtx, ipl); ml_init(&mq->mq_list); mq->mq_maxlen = maxlen; } int mq_enqueue(struct mbuf_queue *mq, struct mbuf *m) { int dropped = 0; mtx_enter(&mq->mq_mtx); if (mq_len(mq) < mq->mq_maxlen) ml_enqueue(&mq->mq_list, m); else { mq->mq_drops++; dropped = 1; } mtx_leave(&mq->mq_mtx); if (dropped) m_freem(m); return (dropped); } struct mbuf * mq_dequeue(struct mbuf_queue *mq) { struct mbuf *m; mtx_enter(&mq->mq_mtx); m = ml_dequeue(&mq->mq_list); mtx_leave(&mq->mq_mtx); return (m); } int mq_enlist(struct mbuf_queue *mq, struct mbuf_list *ml) { struct mbuf *m; int dropped = 0; mtx_enter(&mq->mq_mtx); if (mq_len(mq) < mq->mq_maxlen) ml_enlist(&mq->mq_list, ml); else { dropped = ml_len(ml); mq->mq_drops += dropped; } mtx_leave(&mq->mq_mtx); if (dropped) { while ((m = ml_dequeue(ml)) != NULL) m_freem(m); } return (dropped); } void mq_delist(struct mbuf_queue *mq, struct mbuf_list *ml) { mtx_enter(&mq->mq_mtx); *ml = mq->mq_list; ml_init(&mq->mq_list); mtx_leave(&mq->mq_mtx); } struct mbuf * mq_dechain(struct mbuf_queue *mq) { struct mbuf *m0; mtx_enter(&mq->mq_mtx); m0 = ml_dechain(&mq->mq_list); mtx_leave(&mq->mq_mtx); return (m0); } unsigned int mq_purge(struct mbuf_queue *mq) { struct mbuf_list ml; mq_delist(mq, &ml); return (ml_purge(&ml)); } int sysctl_mq(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen, struct mbuf_queue *mq) { unsigned int maxlen; int error; /* All sysctl names at this level are terminal. */ if (namelen != 1) return (ENOTDIR); switch (name[0]) { case IFQCTL_LEN: return (sysctl_rdint(oldp, oldlenp, newp, mq_len(mq))); case IFQCTL_MAXLEN: maxlen = mq->mq_maxlen; error = sysctl_int(oldp, oldlenp, newp, newlen, &maxlen); if (!error && maxlen != mq->mq_maxlen) { mtx_enter(&mq->mq_mtx); mq->mq_maxlen = maxlen; mtx_leave(&mq->mq_mtx); } return (error); case IFQCTL_DROPS: return (sysctl_rdint(oldp, oldlenp, newp, mq_drops(mq))); default: return (EOPNOTSUPP); } /* NOTREACHED */ } /* mbuf2 api stub */ #ifndef CPA_sys_param_h #define CPA_sys_param_h 1 #include #endif #ifndef CPA_sys_systm_h #define CPA_sys_systm_h 1 #include #endif #ifndef CPA_sys_malloc_h #define CPA_sys_malloc_h 1 #include #endif #ifndef CPA_sys_pool_h #define CPA_sys_pool_h 1 #include #endif #ifndef CPA_sys_mbuf_h #define CPA_sys_mbuf_h 1 #include #endif /* can't call it m_dup(), as freebsd[34] uses m_dup() with different arg */ static struct mbuf *m_dup1(struct mbuf *, int, int, int); int __VERIFIER_nondet_bool(void); /* * ensure that [off, off + len] is contiguous on the mbuf chain "m". * packet chain before "off" is kept untouched. * if offp == NULL, the target will start at on resulting chain. * if offp != NULL, the target will start at on resulting chain. * * on error return (NULL return value), original "m" will be freed. * * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster) */ struct mbuf * m_pulldown(struct mbuf *m, int off, int len, int *offp) { struct mbuf *n, *o; int hlen, tlen, olen; int sharedcluster; /* check invalid arguments. */ if (m == NULL) panic("m == NULL in m_pulldown()"); if ((n = m_getptr(m, off, &off)) == NULL) { m_freem(m); return (NULL); /* mbuf chain too short */ } sharedcluster = M_READONLY(n); /* * the target data is on . * if we got enough data on the mbuf "n", we're done. */ if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster) goto ok; /* * when len <= n->m_len - off and off != 0, it is a special case. * len bytes from sits in single mbuf, but the caller does * not like the starting position (off). * chop the current mbuf into two pieces, set off to 0. */ if (len <= n->m_len - off) { struct mbuf *mlast; o = m_dup1(n, off, n->m_len - off, M_DONTWAIT); if (o == NULL) { m_freem(m); return (NULL); /* ENOBUFS */ } for (mlast = o; mlast->m_next != NULL; mlast = mlast->m_next) ; n->m_len = off; mlast->m_next = n->m_next; n->m_next = o; n = o; off = 0; goto ok; } /* * we need to take hlen from and tlen from m_next, 0>, * and construct contiguous mbuf with m_len == len. * note that hlen + tlen == len, and tlen > 0. */ hlen = n->m_len - off; tlen = len - hlen; /* * ensure that we have enough trailing data on mbuf chain. * if not, we can do nothing about the chain. */ olen = 0; for (o = n->m_next; o != NULL; o = o->m_next) olen += o->m_len; if (hlen + olen < len) { m_freem(m); return (NULL); /* mbuf chain too short */ } /* * easy cases first. * we need to use m_copydata() to get data from m_next, 0>. */ if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen && !sharedcluster) { m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len); n->m_len += tlen; m_adj(n->m_next, tlen); goto ok; } if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen && !sharedcluster && n->m_next->m_len >= tlen) { n->m_next->m_data -= hlen; n->m_next->m_len += hlen; memmove(mtod(n->m_next, caddr_t), mtod(n, caddr_t) + off, hlen); n->m_len -= hlen; n = n->m_next; off = 0; goto ok; } /* * now, we need to do the hard way. don't m_copym as there's no room * on both ends. */ if (len > MAXMCLBYTES) { m_freem(m); return (NULL); } MGET(o, M_DONTWAIT, m->m_type); if (o && len > MLEN) { MCLGETI(o, M_DONTWAIT, NULL, len); if ((o->m_flags & M_EXT) == 0) { m_free(o); o = NULL; } } if (!o) { m_freem(m); return (NULL); /* ENOBUFS */ } /* get hlen from into */ o->m_len = hlen; memmove(mtod(o, caddr_t), mtod(n, caddr_t) + off, hlen); n->m_len -= hlen; /* get tlen from m_next, 0> into */ m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len); o->m_len += tlen; m_adj(n->m_next, tlen); o->m_next = n->m_next; n->m_next = o; n = o; off = 0; ok: if (offp) *offp = off; return (n); } static struct mbuf * m_dup1(struct mbuf *m, int off, int len, int wait) { struct mbuf *n; int l; if (len > MAXMCLBYTES) return (NULL); if (off == 0 && (m->m_flags & M_PKTHDR) != 0) { MGETHDR(n, wait, m->m_type); if (n == NULL) return (NULL); if (m_dup_pkthdr(n, m, wait)) { m_free(n); return (NULL); } l = MHLEN; } else { MGET(n, wait, m->m_type); l = MLEN; } if (n && len > l) { MCLGETI(n, wait, NULL, len); if ((n->m_flags & M_EXT) == 0) { m_free(n); n = NULL; } } if (!n) return (NULL); m_copydata(m, off, len, mtod(n, caddr_t)); n->m_len = len; return (n); } /* Get a packet tag structure along with specified data following. */ struct m_tag * m_tag_get(int type, int len, int wait) { struct m_tag *t; if (len < 0) return (NULL); if (len > PACKET_TAG_MAXSIZE) panic("requested tag size for pool %#x is too big", type); if (wait == M_NOWAIT && __VERIFIER_nondet_bool()) { return (NULL); } t = malloc(sizeof(struct m_tag), 0, 0); t->m_tag_id = type; t->m_tag_len = len; return (t); } /* Prepend a packet tag. */ void m_tag_prepend(struct mbuf *m, struct m_tag *t) { SLIST_INSERT_HEAD(&m->m_pkthdr.ph_tags, t, m_tag_link); m->m_pkthdr.ph_tagsset |= t->m_tag_id; } /* Unlink and free a packet tag. */ void m_tag_delete(struct mbuf *m, struct m_tag *t) { u_int32_t ph_tagsset = 0; struct m_tag *p; SLIST_REMOVE(&m->m_pkthdr.ph_tags, t, m_tag, m_tag_link); free(t, M_TEMP, sizeof(struct m_tag)); SLIST_FOREACH(p, &m->m_pkthdr.ph_tags, m_tag_link) ph_tagsset |= p->m_tag_id; m->m_pkthdr.ph_tagsset = ph_tagsset; } /* Unlink and free a packet tag chain. */ void m_tag_delete_chain(struct mbuf *m) { struct m_tag *p; while ((p = SLIST_FIRST(&m->m_pkthdr.ph_tags)) != NULL) { SLIST_REMOVE_HEAD(&m->m_pkthdr.ph_tags, m_tag_link); free(p, M_TEMP, sizeof(struct m_tag)); } m->m_pkthdr.ph_tagsset = 0; } /* Find a tag, starting from a given position. */ struct m_tag * m_tag_find(struct mbuf *m, int type, struct m_tag *t) { struct m_tag *p; if (!(m->m_pkthdr.ph_tagsset & type)) return (NULL); if (t == NULL) p = SLIST_FIRST(&m->m_pkthdr.ph_tags); else p = SLIST_NEXT(t, m_tag_link); while (p != NULL) { if (p->m_tag_id == type) return (p); p = SLIST_NEXT(p, m_tag_link); } return (NULL); } /* Copy a single tag. */ struct m_tag * m_tag_copy(struct m_tag *t, int wait) { struct m_tag *p; p = m_tag_get(t->m_tag_id, t->m_tag_len, wait); if (p == NULL) return (NULL); memcpy(p + 1, t + 1, t->m_tag_len); /* Copy the data */ return (p); } /* * Copy two tag chains. The destination mbuf (to) loses any attached * tags even if the operation fails. This should not be a problem, as * m_tag_copy_chain() is typically called with a newly-allocated * destination mbuf. */ int m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int wait) { struct m_tag *p, *t, *tprev = NULL; m_tag_delete_chain(to); SLIST_FOREACH(p, &from->m_pkthdr.ph_tags, m_tag_link) { t = m_tag_copy(p, wait); if (t == NULL) { m_tag_delete_chain(to); return (ENOBUFS); } if (tprev == NULL) SLIST_INSERT_HEAD(&to->m_pkthdr.ph_tags, t, m_tag_link); else SLIST_INSERT_AFTER(tprev, t, m_tag_link); tprev = t; to->m_pkthdr.ph_tagsset |= t->m_tag_id; } return (0); } /* Initialize tags on an mbuf. */ void m_tag_init(struct mbuf *m) { SLIST_INIT(&m->m_pkthdr.ph_tags); } /* Get first tag in chain. */ struct m_tag * m_tag_first(struct mbuf *m) { return (SLIST_FIRST(&m->m_pkthdr.ph_tags)); } /* Get next tag in chain. */ struct m_tag * m_tag_next(struct mbuf *m, struct m_tag *t) { return (SLIST_NEXT(t, m_tag_link)); } #ifndef CPA_sys_time_h #define CPA_sys_time_h 1 #include #endif #ifndef CPA_sys_types_h #define CPA_sys_types_h 1 #include #endif #ifndef CPA_sys_malloc_h #define CPA_sys_malloc_h 1 #include #endif #ifndef CPA_sys_pool_h #define CPA_sys_pool_h 1 #include #endif #ifndef CPA_machine_param_h #define CPA_machine_param_h 1 #include #endif void pool_init(struct pool *, size_t, u_int, int, int, const char *, struct pool_allocator *); void pool_destroy(struct pool *); void * pool_get(struct pool *, int); void pool_put(struct pool *, void *); int pool_prime(struct pool *, int); void pool_sethiwat(struct pool *, int); void pool_setlowat(struct pool *, int); int pool_sethardlimit(struct pool *, unsigned, const char *, int); void *pool_page_alloc(struct pool *, int, int *); void pool_page_free(struct pool *, void *); struct pool_allocator pool_allocator_single = { pool_page_alloc, pool_page_free, POOL_ALLOC_SIZE(PAGE_SIZE, POOL_ALLOC_ALIGNED) }; struct pool_allocator pool_allocator_multi = { pool_page_alloc, pool_page_free, POOL_ALLOC_SIZE(PAGE_SIZE, POOL_ALLOC_ALIGNED) }; struct pool_allocator pool_allocator_multi_ni = { pool_page_alloc, pool_page_free, POOL_ALLOC_SIZE(PAGE_SIZE, POOL_ALLOC_ALIGNED) }; void pool_init(struct pool *pool, size_t size, u_int align, int ipl, int flags, const char *wmesg, struct pool_allocator *palloc) { pool->pr_size = size; } void pool_destroy(struct pool *pp) { // nothing } void * pool_get(struct pool *pp, int flags) { void *v = NULL; v = malloc(pp->pr_size,0,0); // kernel malloc, ignore last 2 params return (v); } void pool_put(struct pool *pp, void *item) { free(item); } int pool_prime(struct pool *pp, int nitems) { // nothing } void pool_sethiwat(struct pool *pp, int n) { // nothing } void pool_setlowat(struct pool *pp, int n) { // nothing } int pool_sethardlimit(struct pool *pp, unsigned n, const char *warnmess, int ratecap) { // nothing } void *pool_page_alloc(struct pool *pp, int a, int *b) {} void pool_page_free(struct pool *, void *a) {} /* $OpenBSD: if_etherip.c,v 1.30 2018/01/22 09:06:22 mpi Exp $ */ /* * Copyright (c) 2015 Kazuya GODA * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef CPA_bpfilter_h #define CPA_bpfilter_h 1 #include "bpfilter.h" #endif #ifndef CPA_pf_h #define CPA_pf_h 1 #include "pf.h" #endif #ifndef CPA_sys_param_h #define CPA_sys_param_h 1 #include #endif #ifndef CPA_sys_systm_h #define CPA_sys_systm_h 1 #include #endif #ifndef CPA_sys_mbuf_h #define CPA_sys_mbuf_h 1 #include #endif #ifndef CPA_sys_socket_h #define CPA_sys_socket_h 1 #include #endif #ifndef CPA_sys_ioctl_h #define CPA_sys_ioctl_h 1 #include #endif #ifndef CPA_sys_device_h #define CPA_sys_device_h 1 #include #endif #ifndef CPA_sys_sysctl_h #define CPA_sys_sysctl_h 1 #include #endif #ifndef CPA_net_if_h #define CPA_net_if_h 1 #include #endif #ifndef CPA_net_if_var_h #define CPA_net_if_var_h 1 #include #endif #ifndef CPA_net_if_dl_h #define CPA_net_if_dl_h 1 #include #endif #ifndef CPA_net_if_media_h #define CPA_net_if_media_h 1 #include #endif #ifndef CPA_net_rtable_h #define CPA_net_rtable_h 1 #include #endif #ifndef CPA_netinet_in_h #define CPA_netinet_in_h 1 #include #endif #ifndef CPA_netinet_ip_h #define CPA_netinet_ip_h 1 #include #endif #ifndef CPA_netinet_ip_var_h #define CPA_netinet_ip_var_h 1 #include #endif #ifndef CPA_netinet_if_ether_h #define CPA_netinet_if_ether_h 1 #include #endif #ifndef CPA_netinet_ip_ether_h #define CPA_netinet_ip_ether_h 1 #include #endif #ifdef INET6 #ifndef CPA_netinet_ip6_h #define CPA_netinet_ip6_h 1 #include #endif #ifndef CPA_netinet6_ip6_var_h #define CPA_netinet6_ip6_var_h 1 #include #endif #endif #if NBPFILTER > 0 #ifndef CPA_net_bpf_h #define CPA_net_bpf_h 1 #include #endif #endif #if NPF > 0 #ifndef CPA_net_pfvar_h #define CPA_net_pfvar_h 1 #include #endif #endif #ifndef CPA_net_if_etherip_h #define CPA_net_if_etherip_h 1 #include #endif struct etherip_softc { struct arpcom sc_ac; struct ifmedia sc_media; unsigned int sc_rdomain; struct sockaddr_storage sc_src; struct sockaddr_storage sc_dst; LIST_ENTRY(etherip_softc) sc_entry; }; LIST_HEAD(, etherip_softc) etherip_softc_list; /* * We can control the acceptance of EtherIP packets by altering the sysctl * net.inet.etherip.allow value. Zero means drop them, all else is acceptance. */ int etherip_allow = 0; struct cpumem *etheripcounters; void etheripattach(int); int etherip_clone_create(struct if_clone *, int); int etherip_clone_destroy(struct ifnet *); int etherip_ioctl(struct ifnet *, u_long, caddr_t); void etherip_start(struct ifnet *); int etherip_media_change(struct ifnet *); void etherip_media_status(struct ifnet *, struct ifmediareq *); int etherip_set_tunnel_addr(struct ifnet *, struct sockaddr_storage *, struct sockaddr_storage *); struct if_clone etherip_cloner = IF_CLONE_INITIALIZER("etherip", etherip_clone_create, etherip_clone_destroy); void etheripattach(int count) { if_clone_attach(ðerip_cloner); etheripcounters = counters_alloc(etherips_ncounters); } int etherip_clone_create(struct if_clone *ifc, int unit) { struct ifnet *ifp; struct etherip_softc *sc; sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO); ifp = &sc->sc_ac.ac_if; snprintf(ifp->if_xname, sizeof ifp->if_xname, "etherip%d", unit); ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; ether_fakeaddr(ifp); ifp->if_softc = sc; ifp->if_ioctl = etherip_ioctl; ifp->if_start = etherip_start; ifp->if_xflags = IFXF_CLONED; IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); ifp->if_capabilities = IFCAP_VLAN_MTU; ifmedia_init(&sc->sc_media, 0, etherip_media_change, etherip_media_status); ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL); ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO); if_attach(ifp); ether_ifattach(ifp); LIST_INSERT_HEAD(ðerip_softc_list, sc, sc_entry); return 0; } int etherip_clone_destroy(struct ifnet *ifp) { struct etherip_softc *sc = ifp->if_softc; LIST_REMOVE(sc, sc_entry); ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY); ether_ifdetach(ifp); if_detach(ifp); free(sc, M_DEVBUF, sizeof(*sc)); return 0; } int etherip_media_change(struct ifnet *ifp) { return 0; } void etherip_media_status(struct ifnet *ifp, struct ifmediareq *imr) { imr->ifm_active = IFM_ETHER | IFM_AUTO; imr->ifm_status = IFM_AVALID | IFM_ACTIVE; } void etherip_start(struct ifnet *ifp) { struct etherip_softc *sc = ifp->if_softc; struct mbuf *m; int error; for (;;) { IFQ_DEQUEUE(&ifp->if_snd, m); if (m == NULL) break; #if NBPFILTER > 0 if (ifp->if_bpf) bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT); #endif if (sc->sc_src.ss_family == AF_UNSPEC || sc->sc_dst.ss_family == AF_UNSPEC) { m_freem(m); continue; } switch (sc->sc_src.ss_family) { case AF_INET: error = ip_etherip_output(ifp, m); break; #ifdef INET6 case AF_INET6: error = ip6_etherip_output(ifp, m); break; #endif default: unhandled_af(sc->sc_src.ss_family); } if (error) ifp->if_oerrors++; } } int etherip_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) { struct etherip_softc *sc = ifp->if_softc; struct if_laddrreq *lifr = (struct if_laddrreq *)data; struct ifreq *ifr = (struct ifreq *)data; struct sockaddr_storage *src, *dst; struct proc *p = curproc; int error = 0; switch (cmd) { case SIOCSIFADDR: ifp->if_flags |= IFF_UP; /* FALLTHROUGH */ case SIOCSIFFLAGS: if (ifp->if_flags & IFF_UP) ifp->if_flags |= IFF_RUNNING; else ifp->if_flags &= ~IFF_RUNNING; break; case SIOCSLIFPHYRTABLE: if ((error = suser(p, 0)) != 0) break; if (ifr->ifr_rdomainid < 0 || ifr->ifr_rdomainid > RT_TABLEID_MAX || !rtable_exists(ifr->ifr_rdomainid)) { error = EINVAL; break; } sc->sc_rdomain = ifr->ifr_rdomainid; break; case SIOCGLIFPHYRTABLE: ifr->ifr_rdomainid = sc->sc_rdomain; break; case SIOCSLIFPHYADDR: if ((error = suser(p, 0)) != 0) break; src = &lifr->addr; dst = &lifr->dstaddr; if (src->ss_family == AF_UNSPEC || dst->ss_family == AF_UNSPEC) return EADDRNOTAVAIL; switch (src->ss_family) { case AF_INET: if (src->ss_len != sizeof(struct sockaddr_in) || dst->ss_len != sizeof(struct sockaddr_in)) return EINVAL; break; #ifdef INET6 case AF_INET6: if (src->ss_len != sizeof(struct sockaddr_in6) || dst->ss_len != sizeof(struct sockaddr_in6)) return EINVAL; break; #endif default: return EAFNOSUPPORT; } error = etherip_set_tunnel_addr(ifp, src, dst); break; case SIOCDIFPHYADDR: if ((error = suser(p, 0)) != 0) break; ifp->if_flags &= ~IFF_RUNNING; memset(&sc->sc_src, 0, sizeof(sc->sc_src)); memset(&sc->sc_dst, 0, sizeof(sc->sc_dst)); break; case SIOCGLIFPHYADDR: if (sc->sc_dst.ss_family == AF_UNSPEC) return EADDRNOTAVAIL; memset(&lifr->addr, 0, sizeof(lifr->addr)); memset(&lifr->dstaddr, 0, sizeof(lifr->dstaddr)); memcpy(&lifr->addr, &sc->sc_src, sc->sc_src.ss_len); memcpy(&lifr->dstaddr, &sc->sc_dst, sc->sc_dst.ss_len); break; case SIOCSIFMEDIA: case SIOCGIFMEDIA: error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); break; default: error = ether_ioctl(ifp, &sc->sc_ac, cmd, data); break; } return error; } int etherip_set_tunnel_addr(struct ifnet *ifp, struct sockaddr_storage *src, struct sockaddr_storage *dst) { struct etherip_softc *sc, *tsc; int error = 0; sc = ifp->if_softc; LIST_FOREACH(tsc, ðerip_softc_list, sc_entry) { if (tsc == sc) continue; if (tsc->sc_src.ss_family != src->ss_family || tsc->sc_dst.ss_family != dst->ss_family || tsc->sc_src.ss_len != src->ss_len || tsc->sc_dst.ss_len != dst->ss_len) continue; if (tsc->sc_rdomain == sc->sc_rdomain && memcmp(&tsc->sc_dst, dst, dst->ss_len) == 0 && memcmp(&tsc->sc_src, src, src->ss_len) == 0) { error = EADDRNOTAVAIL; goto out; } } memcpy(&sc->sc_src, src, src->ss_len); memcpy(&sc->sc_dst, dst, dst->ss_len); out: return error; } int ip_etherip_output(struct ifnet *ifp, struct mbuf *m) { struct etherip_softc *sc = (struct etherip_softc *)ifp->if_softc; struct sockaddr_in *src, *dst; struct etherip_header *eip; struct ip *ip; src = (struct sockaddr_in *)&sc->sc_src; dst = (struct sockaddr_in *)&sc->sc_dst; if (src == NULL || dst == NULL || src->sin_family != AF_INET || dst->sin_family != AF_INET) { m_freem(m); return EAFNOSUPPORT; } if (dst->sin_addr.s_addr == INADDR_ANY) { m_freem(m); return ENETUNREACH; } /* * Remove multicast and broadcast flags or encapsulated packet * ends up as multicast or broadcast packet. */ m->m_flags &= ~(M_BCAST|M_MCAST); M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT); if (m == NULL) { etheripstat_inc(etherips_adrops); return ENOBUFS; } eip = mtod(m, struct etherip_header *); eip->eip_ver = ETHERIP_VERSION; eip->eip_res = 0; eip->eip_pad = 0; M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); if (m == NULL) { etheripstat_inc(etherips_adrops); return ENOBUFS; } ip = mtod(m, struct ip *); memset(ip, 0, sizeof(struct ip)); ip->ip_v = IPVERSION; ip->ip_hl = sizeof(struct ip) >> 2; ip->ip_id = htons(ip_randomid()); ip->ip_tos = IPTOS_LOWDELAY; ip->ip_p = IPPROTO_ETHERIP; ip->ip_len = htons(m->m_pkthdr.len); ip->ip_ttl = IPDEFTTL; ip->ip_src = src->sin_addr; ip->ip_dst = dst->sin_addr; m->m_pkthdr.ph_rtableid = sc->sc_rdomain; #if NPF > 0 pf_pkt_addr_changed(m); #endif etheripstat_pkt(etherips_opackets, etherips_obytes, m->m_pkthdr.len - (sizeof(struct ip) + sizeof(struct etherip_header))); ip_send(m); return (0); } int ip_etherip_input(struct mbuf **mp, int *offp, int proto, int af) { struct mbuf *m = *mp; struct mbuf_list ml = MBUF_LIST_INITIALIZER(); struct etherip_softc *sc; const struct ip *ip; struct etherip_header *eip; struct sockaddr_in *src, *dst; struct ifnet *ifp = NULL; ip = mtod(m, struct ip *); if (ip->ip_p != IPPROTO_ETHERIP) { m_freem(m); ipstat_inc(ips_noproto); return IPPROTO_DONE; } if (!etherip_allow && (m->m_flags & (M_AUTH|M_CONF)) == 0) { m_freem(m); etheripstat_inc(etherips_pdrops); return IPPROTO_DONE; } NET_ASSERT_LOCKED(); LIST_FOREACH(sc, ðerip_softc_list, sc_entry) { if (sc->sc_src.ss_family != AF_INET || sc->sc_dst.ss_family != AF_INET) continue; src = (struct sockaddr_in *)&sc->sc_src; dst = (struct sockaddr_in *)&sc->sc_dst; if (sc->sc_rdomain != rtable_l2(m->m_pkthdr.ph_rtableid) || src->sin_addr.s_addr != ip->ip_dst.s_addr || dst->sin_addr.s_addr != ip->ip_src.s_addr) continue; ifp = &sc->sc_ac.ac_if; break; } if (ifp == NULL) { etheripstat_inc(etherips_noifdrops); m_freem(m); return IPPROTO_DONE; } m_adj(m, *offp); m = *mp = m_pullup(m, sizeof(struct etherip_header)); if (m == NULL) { etheripstat_inc(etherips_adrops); return IPPROTO_DONE; } eip = mtod(m, struct etherip_header *); if (eip->eip_ver != ETHERIP_VERSION || eip->eip_pad) { etheripstat_inc(etherips_adrops); m_freem(m); return IPPROTO_DONE; } etheripstat_pkt(etherips_ipackets, etherips_ibytes, m->m_pkthdr.len - sizeof(struct etherip_header)); m_adj(m, sizeof(struct etherip_header)); m = *mp = m_pullup(m, sizeof(struct ether_header)); if (m == NULL) { etheripstat_inc(etherips_adrops); return IPPROTO_DONE; } m->m_flags &= ~(M_BCAST|M_MCAST); #if NPF > 0 pf_pkt_addr_changed(m); #endif ml_enqueue(&ml, m); if_input(ifp, &ml); return IPPROTO_DONE; } #ifdef INET6 int ip6_etherip_output(struct ifnet *ifp, struct mbuf *m) { struct etherip_softc *sc = (struct etherip_softc *)ifp->if_softc; struct sockaddr_in6 *src, *dst; struct etherip_header *eip; struct ip6_hdr *ip6; int error; src = (struct sockaddr_in6 *)&sc->sc_src; dst = (struct sockaddr_in6 *)&sc->sc_dst; if (src == NULL || dst == NULL || src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6) { error = EAFNOSUPPORT; goto drop; } if (IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) { error = ENETUNREACH; goto drop; } /* * Remove multicast and broadcast flags or encapsulated packet * ends up as multicast or broadcast packet. */ m->m_flags &= ~(M_BCAST|M_MCAST); M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT); if (m == NULL) { etheripstat_inc(etherips_adrops); return ENOBUFS; } eip = mtod(m, struct etherip_header *); eip->eip_ver = ETHERIP_VERSION; eip->eip_res = 0; eip->eip_pad = 0; M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT); if (m == NULL) { etheripstat_inc(etherips_adrops); return ENOBUFS; } ip6 = mtod(m, struct ip6_hdr *); ip6->ip6_flow = 0; ip6->ip6_vfc &= ~IPV6_VERSION_MASK; ip6->ip6_vfc |= IPV6_VERSION; ip6->ip6_nxt = IPPROTO_ETHERIP; ip6->ip6_hlim = ip6_defhlim; ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); error = in6_embedscope(&ip6->ip6_src, src, NULL); if (error != 0) goto drop; error = in6_embedscope(&ip6->ip6_dst, dst, NULL); if (error != 0) goto drop; m->m_pkthdr.ph_rtableid = sc->sc_rdomain; #if NPF > 0 pf_pkt_addr_changed(m); #endif etheripstat_pkt(etherips_opackets, etherips_obytes, m->m_pkthdr.len - (sizeof(struct ip6_hdr) + sizeof(struct etherip_header))); ip6_send(m); return (0); drop: m_freem(m); return (error); } int ip6_etherip_input(struct mbuf **mp, int *offp, int proto, int af) { struct mbuf *m = *mp; struct mbuf_list ml = MBUF_LIST_INITIALIZER(); struct etherip_softc *sc; const struct ip6_hdr *ip6; struct etherip_header *eip; struct sockaddr_in6 ipsrc, ipdst; struct sockaddr_in6 *src6, *dst6; struct ifnet *ifp = NULL; if (!etherip_allow && (m->m_flags & (M_AUTH|M_CONF)) == 0) { m_freem(m); etheripstat_inc(etherips_pdrops); return IPPROTO_NONE; } ip6 = mtod(m, const struct ip6_hdr *); in6_recoverscope(&ipsrc, &ip6->ip6_src); in6_recoverscope(&ipdst, &ip6->ip6_dst); NET_ASSERT_LOCKED(); LIST_FOREACH(sc, ðerip_softc_list, sc_entry) { if (sc->sc_src.ss_family != AF_INET6 || sc->sc_dst.ss_family != AF_INET6) continue; src6 = (struct sockaddr_in6 *)&sc->sc_src; dst6 = (struct sockaddr_in6 *)&sc->sc_dst; if (IN6_ARE_ADDR_EQUAL(&src6->sin6_addr, &ipdst.sin6_addr) && src6->sin6_scope_id == ipdst.sin6_scope_id && IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ipsrc.sin6_addr) && dst6->sin6_scope_id == ipsrc.sin6_scope_id) { ifp = &sc->sc_ac.ac_if; break; } } if (ifp == NULL) { etheripstat_inc(etherips_noifdrops); m_freem(m); return IPPROTO_DONE; } m_adj(m, *offp); m = *mp = m_pullup(m, sizeof(struct etherip_header)); if (m == NULL) { etheripstat_inc(etherips_adrops); return IPPROTO_DONE; } eip = mtod(m, struct etherip_header *); if ((eip->eip_ver != ETHERIP_VERSION) || eip->eip_pad) { etheripstat_inc(etherips_adrops); m_freem(m); return IPPROTO_DONE; } etheripstat_pkt(etherips_ipackets, etherips_ibytes, m->m_pkthdr.len - sizeof(struct etherip_header)); m_adj(m, sizeof(struct etherip_header)); m = *mp = m_pullup(m, sizeof(struct ether_header)); if (m == NULL) { etheripstat_inc(etherips_adrops); return IPPROTO_DONE; } m->m_flags &= ~(M_BCAST|M_MCAST); #if NPF > 0 pf_pkt_addr_changed(m); #endif ml_enqueue(&ml, m); if_input(ifp, &ml); return IPPROTO_DONE; } #endif /* INET6 */ int etherip_sysctl_etheripstat(void *oldp, size_t *oldlenp, void *newp) { struct etheripstat etheripstat; CTASSERT(sizeof(etheripstat) == (etherips_ncounters * sizeof(uint64_t))); memset(ðeripstat, 0, sizeof etheripstat); counters_read(etheripcounters, (uint64_t *)ðeripstat, etherips_ncounters); return (sysctl_rdstruct(oldp, oldlenp, newp, ðeripstat, sizeof(etheripstat))); } int etherip_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen) { int error; /* All sysctl names at this level are terminal. */ if (namelen != 1) return ENOTDIR; switch (name[0]) { case ETHERIPCTL_ALLOW: NET_LOCK(); error = sysctl_int(oldp, oldlenp, newp, newlen, ðerip_allow); NET_UNLOCK(); return (error); case ETHERIPCTL_STATS: return (etherip_sysctl_etheripstat(oldp, oldlenp, newp)); default: break; } return ENOPROTOOPT; } /* $OpenBSD: in6_proto.c,v 1.100 2017/11/23 13:45:46 mpi Exp $ */ /* $KAME: in6_proto.c,v 1.66 2000/10/10 15:35:47 itojun Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the project nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Copyright (c) 1982, 1986, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)in_proto.c 8.1 (Berkeley) 6/10/93 */ #ifndef CPA_sys_param_h #define CPA_sys_param_h 1 #include #endif #ifndef CPA_sys_socket_h #define CPA_sys_socket_h 1 #include #endif #ifndef CPA_sys_protosw_h #define CPA_sys_protosw_h 1 #include #endif #ifndef CPA_sys_kernel_h #define CPA_sys_kernel_h 1 #include #endif #ifndef CPA_sys_domain_h #define CPA_sys_domain_h 1 #include #endif #ifndef CPA_sys_mbuf_h #define CPA_sys_mbuf_h 1 #include #endif #ifndef CPA_net_if_h #define CPA_net_if_h 1 #include #endif #ifndef CPA_net_if_var_h #define CPA_net_if_var_h 1 #include #endif #ifndef CPA_net_route_h #define CPA_net_route_h 1 #include #endif #ifndef CPA_net_rtable_h #define CPA_net_rtable_h 1 #include #endif #ifndef CPA_netinet_in_h #define CPA_netinet_in_h 1 #include #endif #ifndef CPA_netinet_ip_h #define CPA_netinet_ip_h 1 #include #endif #ifndef CPA_netinet_ip_var_h #define CPA_netinet_ip_var_h 1 #include #endif #ifndef CPA_netinet_in_pcb_h #define CPA_netinet_in_pcb_h 1 #include #endif #ifndef CPA_netinet_ip6_h #define CPA_netinet_ip6_h 1 #include #endif #ifndef CPA_netinet6_ip6_var_h #define CPA_netinet6_ip6_var_h 1 #include #endif #ifndef CPA_netinet_icmp6_h #define CPA_netinet_icmp6_h 1 #include #endif #ifndef CPA_netinet_tcp_h #define CPA_netinet_tcp_h 1 #include #endif #ifndef CPA_netinet_tcp_timer_h #define CPA_netinet_tcp_timer_h 1 #include #endif #ifndef CPA_netinet_tcp_var_h #define CPA_netinet_tcp_var_h 1 #include #endif #ifndef CPA_netinet_udp_h #define CPA_netinet_udp_h 1 #include #endif #ifndef CPA_netinet_udp_var_h #define CPA_netinet_udp_var_h 1 #include #endif #ifndef CPA_netinet_ip_ipsp_h #define CPA_netinet_ip_ipsp_h 1 #include #endif #ifndef CPA_netinet_ip_ah_h #define CPA_netinet_ip_ah_h 1 #include #endif #ifndef CPA_netinet_ip_esp_h #define CPA_netinet_ip_esp_h 1 #include #endif #ifndef CPA_netinet_ip_ipip_h #define CPA_netinet_ip_ipip_h 1 #include #endif #ifndef CPA_netinet6_in6_var_h #define CPA_netinet6_in6_var_h 1 #include #endif #ifndef CPA_netinet6_nd6_h #define CPA_netinet6_nd6_h 1 #include #endif #ifndef CPA_netinet6_ip6protosw_h #define CPA_netinet6_ip6protosw_h 1 #include #endif #ifndef CPA_gif_h #define CPA_gif_h 1 #include "gif.h" #endif #if NGIF > 0 #ifndef CPA_netinet_ip_ether_h #define CPA_netinet_ip_ether_h 1 #include #endif #ifndef CPA_net_if_gif_h #define CPA_net_if_gif_h 1 #include #endif #endif #ifndef CPA_carp_h #define CPA_carp_h 1 #include "carp.h" #endif #if NCARP > 0 #ifndef CPA_netinet_ip_carp_h #define CPA_netinet_ip_carp_h 1 #include #endif #endif #ifndef CPA_pf_h #define CPA_pf_h 1 #include "pf.h" #endif #if NPF > 0 #ifndef CPA_netinet6_ip6_divert_h #define CPA_netinet6_ip6_divert_h 1 #include #endif #endif #ifndef CPA_etherip_h #define CPA_etherip_h 1 #include "etherip.h" #endif #if NETHERIP > 0 #ifndef CPA_net_if_etherip_h #define CPA_net_if_etherip_h 1 #include #endif #endif /* * TCP/IP protocol family: IP6, ICMP6, UDP, TCP. */ u_char ip6_protox[IPPROTO_MAX]; const struct protosw inet6sw[] = { { .pr_domain = &inet6domain, .pr_protocol = IPPROTO_IPV6, .pr_init = ip6_init, .pr_slowtimo = frag6_slowtimo, .pr_sysctl = ip6_sysctl }, { .pr_type = SOCK_DGRAM, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_UDP, .pr_flags = PR_ATOMIC|PR_ADDR|PR_SPLICE, .pr_input = udp_input, .pr_ctlinput = udp6_ctlinput, .pr_ctloutput = ip6_ctloutput, .pr_usrreq = udp_usrreq, .pr_attach = udp_attach, .pr_detach = udp_detach, .pr_sysctl = udp_sysctl }, { .pr_type = SOCK_STREAM, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_TCP, .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_ABRTACPTDIS|PR_SPLICE, .pr_input = tcp_input, .pr_ctlinput = tcp6_ctlinput, .pr_ctloutput = tcp_ctloutput, .pr_usrreq = tcp_usrreq, .pr_attach = tcp_attach, .pr_detach = tcp_detach, .pr_sysctl = tcp_sysctl }, { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_RAW, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = rip6_input, .pr_ctlinput = rip6_ctlinput, .pr_ctloutput = rip6_ctloutput, .pr_usrreq = rip6_usrreq, .pr_attach = rip6_attach, .pr_detach = rip6_detach, .pr_sysctl = rip6_sysctl }, { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_ICMPV6, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = icmp6_input, .pr_ctlinput = rip6_ctlinput, .pr_ctloutput = rip6_ctloutput, .pr_usrreq = rip6_usrreq, .pr_attach = rip6_attach, .pr_detach = rip6_detach, .pr_init = icmp6_init, .pr_fasttimo = icmp6_fasttimo, .pr_sysctl = icmp6_sysctl }, { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_DSTOPTS, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = dest6_input }, { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_ROUTING, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = route6_input }, { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_FRAGMENT, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = frag6_input }, #ifdef IPSEC { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_AH, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = ah6_input, .pr_ctloutput = rip6_ctloutput, .pr_usrreq = rip6_usrreq, .pr_attach = rip6_attach, .pr_detach = rip6_detach, .pr_sysctl = ah_sysctl }, { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_ESP, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = esp6_input, .pr_ctloutput = rip6_ctloutput, .pr_usrreq = rip6_usrreq, .pr_attach = rip6_attach, .pr_detach = rip6_detach, .pr_sysctl = esp_sysctl }, { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_IPCOMP, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = ipcomp6_input, .pr_ctloutput = rip6_ctloutput, .pr_usrreq = rip6_usrreq, .pr_attach = rip6_attach, .pr_detach = rip6_detach, .pr_sysctl = ipcomp_sysctl }, #endif /* IPSEC */ { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_IPV4, .pr_flags = PR_ATOMIC|PR_ADDR, #if NGIF > 0 .pr_input = in6_gif_input, #else .pr_input = ipip_input, #endif .pr_ctloutput = rip6_ctloutput, .pr_usrreq = rip6_usrreq, /* XXX */ .pr_attach = rip6_attach, .pr_detach = rip6_detach, }, { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_IPV6, .pr_flags = PR_ATOMIC|PR_ADDR, #if NGIF > 0 .pr_input = in6_gif_input, #else .pr_input = ipip_input, #endif .pr_ctloutput = rip6_ctloutput, .pr_usrreq = rip6_usrreq, /* XXX */ .pr_attach = rip6_attach, .pr_detach = rip6_detach, }, #if NCARP > 0 { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_CARP, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = carp6_proto_input, .pr_ctloutput = rip6_ctloutput, .pr_usrreq = rip6_usrreq, .pr_attach = rip6_attach, .pr_detach = rip6_detach, .pr_sysctl = carp_sysctl }, #endif /* NCARP */ #if NPF > 0 { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_DIVERT, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_ctloutput = rip6_ctloutput, .pr_usrreq = divert6_usrreq, .pr_attach = divert6_attach, .pr_detach = divert6_detach, .pr_init = divert6_init, .pr_sysctl = divert6_sysctl }, #endif /* NPF > 0 */ #if NETHERIP > 0 { .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_protocol = IPPROTO_ETHERIP, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = ip6_etherip_input, .pr_ctloutput = rip6_ctloutput, .pr_usrreq = rip6_usrreq, .pr_attach = rip6_attach, .pr_detach = rip6_detach, }, #endif /* NETHERIP */ { /* raw wildcard */ .pr_type = SOCK_RAW, .pr_domain = &inet6domain, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = rip6_input, .pr_ctloutput = rip6_ctloutput, .pr_usrreq = rip6_usrreq, .pr_attach = rip6_attach, .pr_detach = rip6_detach, .pr_init = rip6_init } }; struct domain inet6domain = { .dom_family = AF_INET6, .dom_name = "internet6", .dom_protosw = inet6sw, .dom_protoswNPROTOSW = &inet6sw[nitems(inet6sw)], .dom_rtkeylen = sizeof(struct sockaddr_in6), .dom_rtoffset = offsetof(struct sockaddr_in6, sin6_addr), .dom_maxplen = 128, .dom_ifattach = in6_domifattach, .dom_ifdetach = in6_domifdetach }; /* * Internet configuration info */ int ip6_forwarding = 0; /* no forwarding unless sysctl'd to enable */ int ip6_mforwarding = 0; /* no multicast forwarding unless ... */ int ip6_multipath = 0; /* no using multipath routes unless ... */ int ip6_sendredirects = 1; int ip6_defhlim = IPV6_DEFHLIM; int ip6_defmcasthlim = IPV6_DEFAULT_MULTICAST_HOPS; int ip6_maxfragpackets = 200; int ip6_maxfrags = 200; int ip6_log_interval = 5; int ip6_hdrnestlimit = 10; /* appropriate? */ int ip6_dad_count = 1; /* DupAddrDetectionTransmits */ int ip6_dad_pending; /* number of currently running DADs */ int ip6_auto_flowlabel = 1; int ip6_use_deprecated = 1; /* allow deprecated addr (RFC2462 5.5.4) */ int ip6_mcast_pmtu = 0; /* enable pMTU discovery for multicast? */ int ip6_neighborgcthresh = 2048; /* Threshold # of NDP entries for GC */ int ip6_maxdynroutes = 4096; /* Max # of routes created via redirect */ time_t ip6_log_time = (time_t)0L; /* raw IP6 parameters */ /* * Nominal space allocated to a raw ip socket. */ #define RIPV6SNDQ 8192 #define RIPV6RCVQ 8192 u_long rip6_sendspace = RIPV6SNDQ; u_long rip6_recvspace = RIPV6RCVQ; /* ICMPV6 parameters */ int icmp6_redirtimeout = 10 * 60; /* 10 minutes */ int icmp6errppslim = 100; /* 100pps */ int ip6_mtudisc_timeout = IPMTUDISCTIMEOUT; /* $OpenBSD: in_proto.c,v 1.88 2017/11/23 13:45:46 mpi Exp $ */ /* $NetBSD: in_proto.c,v 1.14 1996/02/18 18:58:32 christos Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the project nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Copyright (c) 1982, 1986, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 * * NRL grants permission for redistribution and use in source and binary * forms, with or without modification, of the software and documentation * created at NRL provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgements: * This product includes software developed by the University of * California, Berkeley and its contributors. * This product includes software developed at the Information * Technology Division, US Naval Research Laboratory. * 4. Neither the name of the NRL nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation * are those of the authors and should not be interpreted as representing * official policies, either expressed or implied, of the US Naval * Research Laboratory (NRL). */ #ifndef CPA_sys_param_h #define CPA_sys_param_h 1 #include #endif #ifndef CPA_sys_socket_h #define CPA_sys_socket_h 1 #include #endif #ifndef CPA_sys_protosw_h #define CPA_sys_protosw_h 1 #include #endif #ifndef CPA_sys_domain_h #define CPA_sys_domain_h 1 #include #endif #ifndef CPA_sys_mbuf_h #define CPA_sys_mbuf_h 1 #include #endif #ifndef CPA_net_if_h #define CPA_net_if_h 1 #include #endif #ifndef CPA_net_if_var_h #define CPA_net_if_var_h 1 #include #endif #ifndef CPA_net_route_h #define CPA_net_route_h 1 #include #endif #ifndef CPA_net_rtable_h #define CPA_net_rtable_h 1 #include #endif #ifndef CPA_netinet_in_h #define CPA_netinet_in_h 1 #include #endif #ifndef CPA_netinet_ip_h #define CPA_netinet_ip_h 1 #include #endif #ifndef CPA_netinet_ip_var_h #define CPA_netinet_ip_var_h 1 #include #endif #ifndef CPA_netinet_ip_icmp_h #define CPA_netinet_ip_icmp_h 1 #include #endif #ifndef CPA_netinet_in_pcb_h #define CPA_netinet_in_pcb_h 1 #include #endif #ifdef INET6 #ifndef CPA_netinet_ip6_h #define CPA_netinet_ip6_h 1 #include #endif #endif #ifndef CPA_netinet_igmp_var_h #define CPA_netinet_igmp_var_h 1 #include #endif #ifndef CPA_netinet_tcp_h #define CPA_netinet_tcp_h 1 #include #endif #ifndef CPA_netinet_tcp_timer_h #define CPA_netinet_tcp_timer_h 1 #include #endif #ifndef CPA_netinet_tcp_var_h #define CPA_netinet_tcp_var_h 1 #include #endif #ifndef CPA_netinet_udp_h #define CPA_netinet_udp_h 1 #include #endif #ifndef CPA_netinet_udp_var_h #define CPA_netinet_udp_var_h 1 #include #endif /* * TCP/IP protocol family: IP, ICMP, UDP, TCP. */ #ifndef CPA_gif_h #define CPA_gif_h 1 #include "gif.h" #endif #if NGIF > 0 #ifndef CPA_net_if_gif_h #define CPA_net_if_gif_h 1 #include #endif #endif #ifdef INET6 #ifndef CPA_netinet6_ip6_var_h #define CPA_netinet6_ip6_var_h 1 #include #endif #endif /* INET6 */ #ifdef IPSEC #ifndef CPA_netinet_ip_ipsp_h #define CPA_netinet_ip_ipsp_h 1 #include #endif #endif #ifndef CPA_netinet_ip_ether_h #define CPA_netinet_ip_ether_h 1 #include #endif #ifndef CPA_netinet_ip_ipip_h #define CPA_netinet_ip_ipip_h 1 #include #endif #ifndef CPA_gre_h #define CPA_gre_h 1 #include "gre.h" #endif #if NGRE > 0 #ifndef CPA_netinet_ip_gre_h #define CPA_netinet_ip_gre_h 1 #include #endif #ifndef CPA_net_if_gre_h #define CPA_net_if_gre_h 1 #include #endif #endif #ifndef CPA_carp_h #define CPA_carp_h 1 #include "carp.h" #endif #if NCARP > 0 #ifndef CPA_netinet_ip_carp_h #define CPA_netinet_ip_carp_h 1 #include #endif #endif #ifndef CPA_pfsync_h #define CPA_pfsync_h 1 #include "pfsync.h" #endif #if NPFSYNC > 0 #ifndef CPA_net_pfvar_h #define CPA_net_pfvar_h 1 #include #endif #ifndef CPA_net_if_pfsync_h #define CPA_net_if_pfsync_h 1 #include #endif #endif #ifndef CPA_pf_h #define CPA_pf_h 1 #include "pf.h" #endif #if NPF > 0 #ifndef CPA_netinet_ip_divert_h #define CPA_netinet_ip_divert_h 1 #include #endif #endif #ifndef CPA_etherip_h #define CPA_etherip_h 1 #include "etherip.h" #endif #if NETHERIP > 0 #ifndef CPA_net_if_etherip_h #define CPA_net_if_etherip_h 1 #include #endif #endif u_char ip_protox[IPPROTO_MAX]; const struct protosw inetsw[] = { { .pr_domain = &inetdomain, .pr_init = ip_init, .pr_slowtimo = ip_slowtimo, .pr_sysctl = ip_sysctl }, { .pr_type = SOCK_DGRAM, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_UDP, .pr_flags = PR_ATOMIC|PR_ADDR|PR_SPLICE, .pr_input = udp_input, .pr_ctlinput = udp_ctlinput, .pr_ctloutput = ip_ctloutput, .pr_usrreq = udp_usrreq, .pr_attach = udp_attach, .pr_detach = udp_detach, .pr_init = udp_init, .pr_sysctl = udp_sysctl }, { .pr_type = SOCK_STREAM, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_TCP, .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_ABRTACPTDIS|PR_SPLICE, .pr_input = tcp_input, .pr_ctlinput = tcp_ctlinput, .pr_ctloutput = tcp_ctloutput, .pr_usrreq = tcp_usrreq, .pr_attach = tcp_attach, .pr_detach = tcp_detach, .pr_init = tcp_init, .pr_slowtimo = tcp_slowtimo, .pr_sysctl = tcp_sysctl }, { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_RAW, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = rip_input, .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, }, { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_ICMP, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = icmp_input, .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, .pr_init = icmp_init, .pr_sysctl = icmp_sysctl }, { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_IPV4, .pr_flags = PR_ATOMIC|PR_ADDR, #if NGIF > 0 .pr_input = in_gif_input, #else .pr_input = ipip_input, #endif .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, .pr_sysctl = ipip_sysctl, .pr_init = ipip_init }, #ifdef INET6 { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_IPV6, .pr_flags = PR_ATOMIC|PR_ADDR, #if NGIF > 0 .pr_input = in_gif_input, #else .pr_input = ipip_input, #endif .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, /* XXX */ .pr_attach = rip_attach, .pr_detach = rip_detach, }, #endif #if defined(MPLS) && NGIF > 0 { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_MPLS, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = mplsip_input, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, }, #endif /* MPLS && GIF */ { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_IGMP, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = igmp_input, .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, .pr_init = igmp_init, .pr_fasttimo = igmp_fasttimo, .pr_slowtimo = igmp_slowtimo, .pr_sysctl = igmp_sysctl }, #ifdef IPSEC { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_AH, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = ah4_input, .pr_ctlinput = ah4_ctlinput, .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, .pr_sysctl = ah_sysctl }, { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_ESP, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = esp4_input, .pr_ctlinput = esp4_ctlinput, .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, .pr_sysctl = esp_sysctl }, { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_IPCOMP, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = ipcomp4_input, .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, .pr_sysctl = ipcomp_sysctl }, #endif /* IPSEC */ #if NGRE > 0 { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_GRE, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = gre_input, .pr_ctloutput = rip_ctloutput, .pr_usrreq = gre_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, .pr_sysctl = gre_sysctl }, { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_MOBILE, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = gre_mobile_input, .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, .pr_sysctl = ipmobile_sysctl }, #endif /* NGRE > 0 */ #if NCARP > 0 { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_CARP, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = carp_proto_input, .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, .pr_sysctl = carp_sysctl }, #endif /* NCARP > 0 */ #if NPFSYNC > 0 { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_PFSYNC, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = pfsync_input, .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, .pr_sysctl = pfsync_sysctl }, #endif /* NPFSYNC > 0 */ #if NPF > 0 { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_DIVERT, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_ctloutput = rip_ctloutput, .pr_usrreq = divert_usrreq, .pr_attach = divert_attach, .pr_detach = divert_detach, .pr_init = divert_init, .pr_sysctl = divert_sysctl }, #endif /* NPF > 0 */ #if NETHERIP > 0 { .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_ETHERIP, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = ip_etherip_input, .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, .pr_sysctl = etherip_sysctl }, #endif /* NETHERIP */ { /* raw wildcard */ .pr_type = SOCK_RAW, .pr_domain = &inetdomain, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = rip_input, .pr_ctloutput = rip_ctloutput, .pr_usrreq = rip_usrreq, .pr_attach = rip_attach, .pr_detach = rip_detach, .pr_init = rip_init } }; struct domain inetdomain = { .dom_family = AF_INET, .dom_name = "internet", .dom_protosw = inetsw, .dom_protoswNPROTOSW = &inetsw[nitems(inetsw)], .dom_rtkeylen = sizeof(struct sockaddr_in), .dom_rtoffset = offsetof(struct sockaddr_in, sin_addr), .dom_maxplen = 32 }; /* $OpenBSD: ip_input.c,v 1.336 2017/12/29 17:05:25 bluhm Exp $ */ /* $NetBSD: ip_input.c,v 1.30 1996/03/16 23:53:58 christos Exp $ */ /* * Copyright (c) 1982, 1986, 1988, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 */ #ifndef CPA_pf_h #define CPA_pf_h 1 #include "pf.h" #endif #ifndef CPA_carp_h #define CPA_carp_h 1 #include "carp.h" #endif #ifndef CPA_sys_param_h #define CPA_sys_param_h 1 #include #endif #ifndef CPA_sys_systm_h #define CPA_sys_systm_h 1 #include #endif #ifndef CPA_sys_mbuf_h #define CPA_sys_mbuf_h 1 #include #endif #ifndef CPA_sys_domain_h #define CPA_sys_domain_h 1 #include #endif #ifndef CPA_sys_mutex_h #define CPA_sys_mutex_h 1 #include #endif #ifndef CPA_sys_protosw_h #define CPA_sys_protosw_h 1 #include #endif #ifndef CPA_sys_socket_h #define CPA_sys_socket_h 1 #include #endif #ifndef CPA_sys_socketvar_h #define CPA_sys_socketvar_h 1 #include #endif #ifndef CPA_sys_sysctl_h #define CPA_sys_sysctl_h 1 #include #endif #ifndef CPA_sys_pool_h #define CPA_sys_pool_h 1 #include #endif #ifndef CPA_sys_task_h #define CPA_sys_task_h 1 #include #endif #ifndef CPA_net_if_h #define CPA_net_if_h 1 #include #endif #ifndef CPA_net_if_var_h #define CPA_net_if_var_h 1 #include #endif #ifndef CPA_net_if_dl_h #define CPA_net_if_dl_h 1 #include #endif #ifndef CPA_net_route_h #define CPA_net_route_h 1 #include #endif #ifndef CPA_net_netisr_h #define CPA_net_netisr_h 1 #include #endif #ifndef CPA_netinet_in_h #define CPA_netinet_in_h 1 #include #endif #ifndef CPA_netinet_in_systm_h #define CPA_netinet_in_systm_h 1 #include #endif #ifndef CPA_netinet_if_ether_h #define CPA_netinet_if_ether_h 1 #include #endif #ifndef CPA_netinet_ip_h #define CPA_netinet_ip_h 1 #include #endif #ifndef CPA_netinet_in_pcb_h #define CPA_netinet_in_pcb_h 1 #include #endif #ifndef CPA_netinet_in_var_h #define CPA_netinet_in_var_h 1 #include #endif #ifndef CPA_netinet_ip_var_h #define CPA_netinet_ip_var_h 1 #include #endif #ifndef CPA_netinet_ip_icmp_h #define CPA_netinet_ip_icmp_h 1 #include #endif #ifdef INET6 #ifndef CPA_netinet6_ip6protosw_h #define CPA_netinet6_ip6protosw_h 1 #include #endif #ifndef CPA_netinet6_ip6_var_h #define CPA_netinet6_ip6_var_h 1 #include #endif #endif #if NPF > 0 #ifndef CPA_net_pfvar_h #define CPA_net_pfvar_h 1 #include #endif #endif #ifdef MROUTING #ifndef CPA_netinet_ip_mroute_h #define CPA_netinet_ip_mroute_h 1 #include #endif #endif #ifdef IPSEC #ifndef CPA_netinet_ip_ipsp_h #define CPA_netinet_ip_ipsp_h 1 #include #endif #endif /* IPSEC */ #if NCARP > 0 #ifndef CPA_net_if_types_h #define CPA_net_if_types_h 1 #include #endif #ifndef CPA_netinet_ip_carp_h #define CPA_netinet_ip_carp_h 1 #include #endif #endif /* values controllable via sysctl */ int ipforwarding = 0; int ipmforwarding = 0; int ipmultipath = 0; int ipsendredirects = 1; int ip_dosourceroute = 0; int ip_defttl = IPDEFTTL; int ip_mtudisc = 1; u_int ip_mtudisc_timeout = IPMTUDISCTIMEOUT; int ip_directedbcast = 0; struct rttimer_queue *ip_mtudisc_timeout_q = NULL; /* Protects `ipq' and `ip_frags'. */ struct mutex ipq_mutex = MUTEX_INITIALIZER(IPL_SOFTNET); /* IP reassembly queue */ LIST_HEAD(, ipq) ipq; /* Keep track of memory used for reassembly */ int ip_maxqueue = 300; int ip_frags = 0; int *ipctl_vars[IPCTL_MAXID] = IPCTL_VARS; struct niqueue ipintrq = NIQUEUE_INITIALIZER(IPQ_MAXLEN, NETISR_IP); struct pool ipqent_pool; struct pool ipq_pool; struct cpumem *ipcounters; int ip_sysctl_ipstat(void *, size_t *, void *); static struct mbuf_queue ipsend_mq; int ip_ours(struct mbuf **, int *, int, int); int ip_local(struct mbuf **, int *, int, int); int ip_dooptions(struct mbuf *, struct ifnet *); int in_ouraddr(struct mbuf *, struct ifnet *, struct rtentry **); static void ip_send_dispatch(void *); static struct task ipsend_task = TASK_INITIALIZER(ip_send_dispatch, &ipsend_mq); /* * Used to save the IP options in case a protocol wants to respond * to an incoming packet over the same route if the packet got here * using IP source routing. This allows connection establishment and * maintenance when the remote end is on a network that is not known * to us. */ struct ip_srcrt { int isr_nhops; /* number of hops */ struct in_addr isr_dst; /* final destination */ char isr_nop; /* one NOP to align */ char isr_hdr[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN & OFFSET */ struct in_addr isr_routes[MAX_IPOPTLEN/sizeof(struct in_addr)]; }; void save_rte(struct mbuf *, u_char *, struct in_addr); /* * IP initialization: fill in IP protocol switch table. * All protocols not implemented in kernel go to raw IP protocol handler. */ void ip_init(void) { const struct protosw *pr; int i; const u_int16_t defbaddynamicports_tcp[] = DEFBADDYNAMICPORTS_TCP; const u_int16_t defbaddynamicports_udp[] = DEFBADDYNAMICPORTS_UDP; const u_int16_t defrootonlyports_tcp[] = DEFROOTONLYPORTS_TCP; const u_int16_t defrootonlyports_udp[] = DEFROOTONLYPORTS_UDP; ipcounters = counters_alloc(ips_ncounters); pool_init(&ipqent_pool, sizeof(struct ipqent), 0, IPL_SOFTNET, 0, "ipqe", NULL); pool_init(&ipq_pool, sizeof(struct ipq), 0, IPL_SOFTNET, 0, "ipq", NULL); pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); if (pr == NULL) panic("ip_init"); for (i = 0; i < IPPROTO_MAX; i++) ip_protox[i] = pr - inetsw; for (pr = inetdomain.dom_protosw; pr < inetdomain.dom_protoswNPROTOSW; pr++) if (pr->pr_domain->dom_family == PF_INET && pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW && pr->pr_protocol < IPPROTO_MAX) ip_protox[pr->pr_protocol] = pr - inetsw; LIST_INIT(&ipq); if (ip_mtudisc != 0) ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout); /* Fill in list of ports not to allocate dynamically. */ memset(&baddynamicports, 0, sizeof(baddynamicports)); for (i = 0; defbaddynamicports_tcp[i] != 0; i++) DP_SET(baddynamicports.tcp, defbaddynamicports_tcp[i]); for (i = 0; defbaddynamicports_udp[i] != 0; i++) DP_SET(baddynamicports.udp, defbaddynamicports_udp[i]); /* Fill in list of ports only root can bind to. */ memset(&rootonlyports, 0, sizeof(rootonlyports)); for (i = 0; defrootonlyports_tcp[i] != 0; i++) DP_SET(rootonlyports.tcp, defrootonlyports_tcp[i]); for (i = 0; defrootonlyports_udp[i] != 0; i++) DP_SET(rootonlyports.udp, defrootonlyports_udp[i]); mq_init(&ipsend_mq, 64, IPL_SOFTNET); #ifdef IPSEC ipsec_init(); #endif } /* * Enqueue packet for local delivery. Queuing is used as a boundary * between the network layer (input/forward path) running without * KERNEL_LOCK() and the transport layer still needing it. */ int ip_ours(struct mbuf **mp, int *offp, int nxt, int af) { /* We are already in a IPv4/IPv6 local deliver loop. */ if (af != AF_UNSPEC) return ip_local(mp, offp, nxt, af); niq_enqueue(&ipintrq, *mp); *mp = NULL; return IPPROTO_DONE; } /* * Dequeue and process locally delivered packets. */ void ipintr(void) { struct mbuf *m; int off, nxt; while ((m = niq_dequeue(&ipintrq)) != NULL) { #ifdef DIAGNOSTIC if ((m->m_flags & M_PKTHDR) == 0) panic("ipintr no HDR"); #endif off = 0; nxt = ip_local(&m, &off, IPPROTO_IPV4, AF_UNSPEC); KASSERT(nxt == IPPROTO_DONE); } } /* * IPv4 input routine. * * Checksum and byte swap header. Process options. Forward or deliver. */ void ipv4_input(struct ifnet *ifp, struct mbuf *m) { int off, nxt; off = 0; nxt = ip_input_if(&m, &off, IPPROTO_IPV4, AF_UNSPEC, ifp); KASSERT(nxt == IPPROTO_DONE); } int ip_input_if(struct mbuf **mp, int *offp, int nxt, int af, struct ifnet *ifp) { struct mbuf *m = *mp; struct rtentry *rt = NULL; struct ip *ip; int hlen, len; in_addr_t pfrdr = 0; KASSERT(*offp == 0); ipstat_inc(ips_total); if (m->m_len < sizeof (struct ip) && (m = *mp = m_pullup(m, sizeof (struct ip))) == NULL) { ipstat_inc(ips_toosmall); goto bad; } ip = mtod(m, struct ip *); if (ip->ip_v != IPVERSION) { ipstat_inc(ips_badvers); goto bad; } hlen = ip->ip_hl << 2; if (hlen < sizeof(struct ip)) { /* minimum header length */ ipstat_inc(ips_badhlen); goto bad; } if (hlen > m->m_len) { if ((m = *mp = m_pullup(m, hlen)) == NULL) { ipstat_inc(ips_badhlen); goto bad; } ip = mtod(m, struct ip *); } /* 127/8 must not appear on wire - RFC1122 */ if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { if ((ifp->if_flags & IFF_LOOPBACK) == 0) { ipstat_inc(ips_badaddr); goto bad; } } if ((m->m_pkthdr.csum_flags & M_IPV4_CSUM_IN_OK) == 0) { if (m->m_pkthdr.csum_flags & M_IPV4_CSUM_IN_BAD) { ipstat_inc(ips_badsum); goto bad; } ipstat_inc(ips_inswcsum); if (in_cksum(m, hlen) != 0) { ipstat_inc(ips_badsum); goto bad; } } /* Retrieve the packet length. */ len = ntohs(ip->ip_len); /* * Convert fields to host representation. */ if (len < hlen) { ipstat_inc(ips_badlen); goto bad; } /* * Check that the amount of data in the buffers * is at least as much as the IP header would have us expect. * Trim mbufs if longer than we expect. * Drop packet if shorter than we expect. */ if (m->m_pkthdr.len < len) { ipstat_inc(ips_tooshort); goto bad; } if (m->m_pkthdr.len > len) { if (m->m_len == m->m_pkthdr.len) { m->m_len = len; m->m_pkthdr.len = len; } else m_adj(m, len - m->m_pkthdr.len); } #if NCARP > 0 if (ifp->if_type == IFT_CARP && carp_lsdrop(m, AF_INET, &ip->ip_src.s_addr, &ip->ip_dst.s_addr, (ip->ip_p == IPPROTO_ICMP ? 0 : 1))) goto bad; #endif #if NPF > 0 /* * Packet filter */ pfrdr = ip->ip_dst.s_addr; if (pf_test(AF_INET, PF_IN, ifp, mp) != PF_PASS) goto bad; m = *mp; if (m == NULL) goto bad; ip = mtod(m, struct ip *); hlen = ip->ip_hl << 2; pfrdr = (pfrdr != ip->ip_dst.s_addr); #endif /* * Process options and, if not destined for us, * ship it on. ip_dooptions returns 1 when an * error was detected (causing an icmp message * to be sent and the original packet to be freed). */ if (hlen > sizeof (struct ip) && ip_dooptions(m, ifp)) { m = *mp = NULL; goto bad; } if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_dst.s_addr == INADDR_ANY) { nxt = ip_ours(mp, offp, nxt, af); goto out; } if (in_ouraddr(m, ifp, &rt)) { nxt = ip_ours(mp, offp, nxt, af); goto out; } if (IN_MULTICAST(ip->ip_dst.s_addr)) { /* * Make sure M_MCAST is set. It should theoretically * already be there, but let's play safe because upper * layers check for this flag. */ m->m_flags |= M_MCAST; #ifdef MROUTING if (ipmforwarding && ip_mrouter[ifp->if_rdomain]) { int error; if (m->m_flags & M_EXT) { if ((m = *mp = m_pullup(m, hlen)) == NULL) { ipstat_inc(ips_toosmall); goto bad; } ip = mtod(m, struct ip *); } /* * If we are acting as a multicast router, all * incoming multicast packets are passed to the * kernel-level multicast forwarding function. * The packet is returned (relatively) intact; if * ip_mforward() returns a non-zero value, the packet * must be discarded, else it may be accepted below. * * (The IP ident field is put in the same byte order * as expected when ip_mforward() is called from * ip_output().) */ KERNEL_LOCK(); error = ip_mforward(m, ifp); KERNEL_UNLOCK(); if (error) { ipstat_inc(ips_cantforward); goto bad; } /* * The process-level routing daemon needs to receive * all multicast IGMP packets, whether or not this * host belongs to their destination groups. */ if (ip->ip_p == IPPROTO_IGMP) { nxt = ip_ours(mp, offp, nxt, af); goto out; } ipstat_inc(ips_forward); } #endif /* * See if we belong to the destination multicast group on the * arrival interface. */ if (!in_hasmulti(&ip->ip_dst, ifp)) { ipstat_inc(ips_notmember); if (!IN_LOCAL_GROUP(ip->ip_dst.s_addr)) ipstat_inc(ips_cantforward); goto bad; } nxt = ip_ours(mp, offp, nxt, af); goto out; } #if NCARP > 0 if (ifp->if_type == IFT_CARP && ip->ip_p == IPPROTO_ICMP && carp_lsdrop(m, AF_INET, &ip->ip_src.s_addr, &ip->ip_dst.s_addr, 1)) goto bad; #endif /* * Not for us; forward if possible and desirable. */ if (ipforwarding == 0) { ipstat_inc(ips_cantforward); goto bad; } #ifdef IPSEC if (ipsec_in_use) { int rv; rv = ipsec_forward_check(m, hlen, AF_INET); if (rv != 0) { ipstat_inc(ips_cantforward); goto bad; } /* * Fall through, forward packet. Outbound IPsec policy * checking will occur in ip_output(). */ } #endif /* IPSEC */ ip_forward(m, ifp, rt, pfrdr); *mp = NULL; return IPPROTO_DONE; bad: nxt = IPPROTO_DONE; m_freemp(mp); out: rtfree(rt); return nxt; } /* * IPv4 local-delivery routine. * * If fragmented try to reassemble. Pass to next level. */ int ip_local(struct mbuf **mp, int *offp, int nxt, int af) { struct mbuf *m = *mp; struct ip *ip = mtod(m, struct ip *); struct ipq *fp; struct ipqent *ipqe; int mff, hlen; hlen = ip->ip_hl << 2; /* * If offset or IP_MF are set, must reassemble. * Otherwise, nothing need be done. * (We could look in the reassembly queue to see * if the packet was previously fragmented, * but it's not worth the time; just let them time out.) */ if (ip->ip_off &~ htons(IP_DF | IP_RF)) { if (m->m_flags & M_EXT) { /* XXX */ if ((m = *mp = m_pullup(m, hlen)) == NULL) { ipstat_inc(ips_toosmall); return IPPROTO_DONE; } ip = mtod(m, struct ip *); } mtx_enter(&ipq_mutex); /* * Look for queue of fragments * of this datagram. */ LIST_FOREACH(fp, &ipq, ipq_q) { if (ip->ip_id == fp->ipq_id && ip->ip_src.s_addr == fp->ipq_src.s_addr && ip->ip_dst.s_addr == fp->ipq_dst.s_addr && ip->ip_p == fp->ipq_p) break; } /* * Adjust ip_len to not reflect header, * set ipqe_mff if more fragments are expected, * convert offset of this to bytes. */ ip->ip_len = htons(ntohs(ip->ip_len) - hlen); mff = (ip->ip_off & htons(IP_MF)) != 0; if (mff) { /* * Make sure that fragments have a data length * that's a non-zero multiple of 8 bytes. */ if (ntohs(ip->ip_len) == 0 || (ntohs(ip->ip_len) & 0x7) != 0) { ipstat_inc(ips_badfrags); goto bad; } } ip->ip_off = htons(ntohs(ip->ip_off) << 3); /* * If datagram marked as having more fragments * or if this is not the first fragment, * attempt reassembly; if it succeeds, proceed. */ if (mff || ip->ip_off) { ipstat_inc(ips_fragments); if (ip_frags + 1 > ip_maxqueue) { ip_flush(); ipstat_inc(ips_rcvmemdrop); goto bad; } ipqe = pool_get(&ipqent_pool, PR_NOWAIT); if (ipqe == NULL) { ipstat_inc(ips_rcvmemdrop); goto bad; } ip_frags++; ipqe->ipqe_mff = mff; ipqe->ipqe_m = m; ipqe->ipqe_ip = ip; m = *mp = ip_reass(ipqe, fp); if (m == NULL) goto bad; ipstat_inc(ips_reassembled); ip = mtod(m, struct ip *); hlen = ip->ip_hl << 2; ip->ip_len = htons(ntohs(ip->ip_len) + hlen); } else if (fp) ip_freef(fp); mtx_leave(&ipq_mutex); } *offp = hlen; nxt = ip->ip_p; /* Check wheter we are already in a IPv4/IPv6 local deliver loop. */ if (af == AF_UNSPEC) nxt = ip_deliver(mp, offp, nxt, AF_INET); return nxt; bad: mtx_leave(&ipq_mutex); m_freemp(mp); return IPPROTO_DONE; } #ifndef INET6 #define IPSTAT_INC(name) ipstat_inc(ips_##name) #else #define IPSTAT_INC(name) (af == AF_INET ? \ ipstat_inc(ips_##name) : ip6stat_inc(ip6s_##name)) #endif int ip_deliver(struct mbuf **mp, int *offp, int nxt, int af) { const struct protosw *psw; int naf = af; #ifdef INET6 int nest = 0; #endif /* INET6 */ /* pf might have modified stuff, might have to chksum */ switch (af) { case AF_INET: in_proto_cksum_out(*mp, NULL); break; #ifdef INET6 case AF_INET6: in6_proto_cksum_out(*mp, NULL); break; #endif /* INET6 */ } /* * Tell launch routine the next header */ IPSTAT_INC(delivered); while (nxt != IPPROTO_DONE) { #ifdef INET6 if (af == AF_INET6 && ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { ip6stat_inc(ip6s_toomanyhdr); goto bad; } #endif /* INET6 */ /* * protection against faulty packet - there should be * more sanity checks in header chain processing. */ if ((*mp)->m_pkthdr.len < *offp) { IPSTAT_INC(tooshort); goto bad; } #ifdef INET6 /* draft-itojun-ipv6-tcp-to-anycast */ if (af == AF_INET6 && ISSET((*mp)->m_flags, M_ACAST) && (nxt == IPPROTO_TCP)) { if ((*mp)->m_len >= sizeof(struct ip6_hdr)) { icmp6_error(*mp, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, offsetof(struct ip6_hdr, ip6_dst)); *mp = NULL; } goto bad; } #endif /* INET6 */ #ifdef IPSEC if (ipsec_in_use) { if (ipsec_local_check(*mp, *offp, nxt, af) != 0) { IPSTAT_INC(cantforward); goto bad; } } /* Otherwise, just fall through and deliver the packet */ #endif /* IPSEC */ switch (nxt) { case IPPROTO_IPV4: naf = AF_INET; ipstat_inc(ips_delivered); break; #ifdef INET6 case IPPROTO_IPV6: naf = AF_INET6; ip6stat_inc(ip6s_delivered); break; #endif /* INET6 */ } switch (af) { case AF_INET: psw = &inetsw[ip_protox[nxt]]; break; #ifdef INET6 case AF_INET6: psw = &inet6sw[ip6_protox[nxt]]; break; #endif /* INET6 */ } nxt = ip6_etherip_input(mp, offp, nxt, af); af = naf; } return nxt; bad: m_freemp(mp); return IPPROTO_DONE; } #undef IPSTAT_INC int in_ouraddr(struct mbuf *m, struct ifnet *ifp, struct rtentry **prt) { struct rtentry *rt; struct ip *ip; struct sockaddr_in sin; int match = 0; #if NPF > 0 switch (pf_ouraddr(m)) { case 0: return (0); case 1: return (1); default: /* pf does not know it */ break; } #endif ip = mtod(m, struct ip *); memset(&sin, 0, sizeof(sin)); sin.sin_len = sizeof(sin); sin.sin_family = AF_INET; sin.sin_addr = ip->ip_dst; rt = rtalloc_mpath(sintosa(&sin), &ip->ip_src.s_addr, m->m_pkthdr.ph_rtableid); if (rtisvalid(rt)) { if (ISSET(rt->rt_flags, RTF_LOCAL)) match = 1; /* * If directedbcast is enabled we only consider it local * if it is received on the interface with that address. */ if (ISSET(rt->rt_flags, RTF_BROADCAST) && (!ip_directedbcast || rt->rt_ifidx == ifp->if_index)) { match = 1; /* Make sure M_BCAST is set */ m->m_flags |= M_BCAST; } } *prt = rt; if (!match) { struct ifaddr *ifa; /* * No local address or broadcast address found, so check for * ancient classful broadcast addresses. * It must have been broadcast on the link layer, and for an * address on the interface it was received on. */ if (!ISSET(m->m_flags, M_BCAST) || !IN_CLASSFULBROADCAST(ip->ip_dst.s_addr, ip->ip_dst.s_addr)) return (0); if (ifp->if_rdomain != rtable_l2(m->m_pkthdr.ph_rtableid)) return (0); /* * The check in the loop assumes you only rx a packet on an UP * interface, and that M_BCAST will only be set on a BROADCAST * interface. */ NET_ASSERT_LOCKED(); TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { if (ifa->ifa_addr->sa_family != AF_INET) continue; if (IN_CLASSFULBROADCAST(ip->ip_dst.s_addr, ifatoia(ifa)->ia_addr.sin_addr.s_addr)) { match = 1; break; } } } return (match); } /* * Take incoming datagram fragment and try to * reassemble it into whole datagram. If a chain for * reassembly of this datagram already exists, then it * is given as fp; otherwise have to make a chain. */ struct mbuf * ip_reass(struct ipqent *ipqe, struct ipq *fp) { struct mbuf *m = ipqe->ipqe_m; struct ipqent *nq, *p, *q; struct ip *ip; struct mbuf *t; int hlen = ipqe->ipqe_ip->ip_hl << 2; int i, next; u_int8_t ecn, ecn0; MUTEX_ASSERT_LOCKED(&ipq_mutex); /* * Presence of header sizes in mbufs * would confuse code below. */ m->m_data += hlen; m->m_len -= hlen; /* * If first fragment to arrive, create a reassembly queue. */ if (fp == NULL) { fp = pool_get(&ipq_pool, PR_NOWAIT); if (fp == NULL) goto dropfrag; LIST_INSERT_HEAD(&ipq, fp, ipq_q); fp->ipq_ttl = IPFRAGTTL; fp->ipq_p = ipqe->ipqe_ip->ip_p; fp->ipq_id = ipqe->ipqe_ip->ip_id; LIST_INIT(&fp->ipq_fragq); fp->ipq_src = ipqe->ipqe_ip->ip_src; fp->ipq_dst = ipqe->ipqe_ip->ip_dst; p = NULL; goto insert; } /* * Handle ECN by comparing this segment with the first one; * if CE is set, do not lose CE. * drop if CE and not-ECT are mixed for the same packet. */ ecn = ipqe->ipqe_ip->ip_tos & IPTOS_ECN_MASK; ecn0 = LIST_FIRST(&fp->ipq_fragq)->ipqe_ip->ip_tos & IPTOS_ECN_MASK; if (ecn == IPTOS_ECN_CE) { if (ecn0 == IPTOS_ECN_NOTECT) goto dropfrag; if (ecn0 != IPTOS_ECN_CE) LIST_FIRST(&fp->ipq_fragq)->ipqe_ip->ip_tos |= IPTOS_ECN_CE; } if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) goto dropfrag; /* * Find a segment which begins after this one does. */ for (p = NULL, q = LIST_FIRST(&fp->ipq_fragq); q != NULL; p = q, q = LIST_NEXT(q, ipqe_q)) if (ntohs(q->ipqe_ip->ip_off) > ntohs(ipqe->ipqe_ip->ip_off)) break; /* * If there is a preceding segment, it may provide some of * our data already. If so, drop the data from the incoming * segment. If it provides all of our data, drop us. */ if (p != NULL) { i = ntohs(p->ipqe_ip->ip_off) + ntohs(p->ipqe_ip->ip_len) - ntohs(ipqe->ipqe_ip->ip_off); if (i > 0) { if (i >= ntohs(ipqe->ipqe_ip->ip_len)) goto dropfrag; m_adj(ipqe->ipqe_m, i); ipqe->ipqe_ip->ip_off = htons(ntohs(ipqe->ipqe_ip->ip_off) + i); ipqe->ipqe_ip->ip_len = htons(ntohs(ipqe->ipqe_ip->ip_len) - i); } } /* * While we overlap succeeding segments trim them or, * if they are completely covered, dequeue them. */ for (; q != NULL && ntohs(ipqe->ipqe_ip->ip_off) + ntohs(ipqe->ipqe_ip->ip_len) > ntohs(q->ipqe_ip->ip_off); q = nq) { i = (ntohs(ipqe->ipqe_ip->ip_off) + ntohs(ipqe->ipqe_ip->ip_len)) - ntohs(q->ipqe_ip->ip_off); if (i < ntohs(q->ipqe_ip->ip_len)) { q->ipqe_ip->ip_len = htons(ntohs(q->ipqe_ip->ip_len) - i); q->ipqe_ip->ip_off = htons(ntohs(q->ipqe_ip->ip_off) + i); m_adj(q->ipqe_m, i); break; } nq = LIST_NEXT(q, ipqe_q); m_freem(q->ipqe_m); LIST_REMOVE(q, ipqe_q); pool_put(&ipqent_pool, q); ip_frags--; } insert: /* * Stick new segment in its place; * check for complete reassembly. */ if (p == NULL) { LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q); } else { LIST_INSERT_AFTER(p, ipqe, ipqe_q); } next = 0; for (p = NULL, q = LIST_FIRST(&fp->ipq_fragq); q != NULL; p = q, q = LIST_NEXT(q, ipqe_q)) { if (ntohs(q->ipqe_ip->ip_off) != next) return (0); next += ntohs(q->ipqe_ip->ip_len); } if (p->ipqe_mff) return (0); /* * Reassembly is complete. Check for a bogus message size and * concatenate fragments. */ q = LIST_FIRST(&fp->ipq_fragq); ip = q->ipqe_ip; if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) { ipstat_inc(ips_toolong); ip_freef(fp); return (0); } m = q->ipqe_m; t = m->m_next; m->m_next = 0; m_cat(m, t); nq = LIST_NEXT(q, ipqe_q); pool_put(&ipqent_pool, q); ip_frags--; for (q = nq; q != NULL; q = nq) { t = q->ipqe_m; nq = LIST_NEXT(q, ipqe_q); pool_put(&ipqent_pool, q); ip_frags--; m_cat(m, t); } /* * Create header for new ip packet by * modifying header of first packet; * dequeue and discard fragment reassembly header. * Make header visible. */ ip->ip_len = htons(next); ip->ip_src = fp->ipq_src; ip->ip_dst = fp->ipq_dst; LIST_REMOVE(fp, ipq_q); pool_put(&ipq_pool, fp); m->m_len += (ip->ip_hl << 2); m->m_data -= (ip->ip_hl << 2); /* some debugging cruft by sklower, below, will go away soon */ if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ int plen = 0; for (t = m; t; t = t->m_next) plen += t->m_len; m->m_pkthdr.len = plen; } return (m); dropfrag: ipstat_inc(ips_fragdropped); m_freem(m); pool_put(&ipqent_pool, ipqe); ip_frags--; return (NULL); } /* * Free a fragment reassembly header and all * associated datagrams. */ void ip_freef(struct ipq *fp) { struct ipqent *q; MUTEX_ASSERT_LOCKED(&ipq_mutex); while ((q = LIST_FIRST(&fp->ipq_fragq)) != NULL) { LIST_REMOVE(q, ipqe_q); m_freem(q->ipqe_m); pool_put(&ipqent_pool, q); ip_frags--; } LIST_REMOVE(fp, ipq_q); pool_put(&ipq_pool, fp); } /* * IP timer processing; * if a timer expires on a reassembly queue, discard it. */ void ip_slowtimo(void) { struct ipq *fp, *nfp; mtx_enter(&ipq_mutex); LIST_FOREACH_SAFE(fp, &ipq, ipq_q, nfp) { if (--fp->ipq_ttl == 0) { ipstat_inc(ips_fragtimeout); ip_freef(fp); } } mtx_leave(&ipq_mutex); } /* * Flush a bunch of datagram fragments, till we are down to 75%. */ void ip_flush(void) { int max = 50; MUTEX_ASSERT_LOCKED(&ipq_mutex); while (!LIST_EMPTY(&ipq) && ip_frags > ip_maxqueue * 3 / 4 && --max) { ipstat_inc(ips_fragdropped); ip_freef(LIST_FIRST(&ipq)); } } /* * Do option processing on a datagram, * possibly discarding it if bad options are encountered, * or forwarding it if source-routed. * Returns 1 if packet has been forwarded/freed, * 0 if the packet should be processed further. */ int ip_dooptions(struct mbuf *m, struct ifnet *ifp) { struct ip *ip = mtod(m, struct ip *); unsigned int rtableid = m->m_pkthdr.ph_rtableid; struct rtentry *rt; struct sockaddr_in ipaddr; u_char *cp; struct ip_timestamp ipt; struct in_ifaddr *ia; int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; struct in_addr sin, dst; u_int32_t ntime; dst = ip->ip_dst; cp = (u_char *)(ip + 1); cnt = (ip->ip_hl << 2) - sizeof (struct ip); KERNEL_LOCK(); for (; cnt > 0; cnt -= optlen, cp += optlen) { opt = cp[IPOPT_OPTVAL]; if (opt == IPOPT_EOL) break; if (opt == IPOPT_NOP) optlen = 1; else { if (cnt < IPOPT_OLEN + sizeof(*cp)) { code = &cp[IPOPT_OLEN] - (u_char *)ip; goto bad; } optlen = cp[IPOPT_OLEN]; if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { code = &cp[IPOPT_OLEN] - (u_char *)ip; goto bad; } } switch (opt) { default: break; /* * Source routing with record. * Find interface with current destination address. * If none on this machine then drop if strictly routed, * or do nothing if loosely routed. * Record interface address and bring up next address * component. If strictly routed make sure next * address is on directly accessible net. */ case IPOPT_LSRR: case IPOPT_SSRR: if (!ip_dosourceroute) { type = ICMP_UNREACH; code = ICMP_UNREACH_SRCFAIL; goto bad; } if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { code = &cp[IPOPT_OFFSET] - (u_char *)ip; goto bad; } memset(&ipaddr, 0, sizeof(ipaddr)); ipaddr.sin_family = AF_INET; ipaddr.sin_len = sizeof(ipaddr); ipaddr.sin_addr = ip->ip_dst; ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr), m->m_pkthdr.ph_rtableid)); if (ia == NULL) { if (opt == IPOPT_SSRR) { type = ICMP_UNREACH; code = ICMP_UNREACH_SRCFAIL; goto bad; } /* * Loose routing, and not at next destination * yet; nothing to do except forward. */ break; } off--; /* 0 origin */ if ((off + sizeof(struct in_addr)) > optlen) { /* * End of source route. Should be for us. */ save_rte(m, cp, ip->ip_src); break; } /* * locate outgoing interface */ memset(&ipaddr, 0, sizeof(ipaddr)); ipaddr.sin_family = AF_INET; ipaddr.sin_len = sizeof(ipaddr); memcpy(&ipaddr.sin_addr, cp + off, sizeof(ipaddr.sin_addr)); /* keep packet in the virtual instance */ rt = rtalloc(sintosa(&ipaddr), RT_RESOLVE, rtableid); if (!rtisvalid(rt) || ((opt == IPOPT_SSRR) && ISSET(rt->rt_flags, RTF_GATEWAY))) { type = ICMP_UNREACH; code = ICMP_UNREACH_SRCFAIL; rtfree(rt); goto bad; } ia = ifatoia(rt->rt_ifa); memcpy(cp + off, &ia->ia_addr.sin_addr, sizeof(struct in_addr)); rtfree(rt); cp[IPOPT_OFFSET] += sizeof(struct in_addr); ip->ip_dst = ipaddr.sin_addr; /* * Let ip_intr's mcast routing check handle mcast pkts */ forward = !IN_MULTICAST(ip->ip_dst.s_addr); break; case IPOPT_RR: if (optlen < IPOPT_OFFSET + sizeof(*cp)) { code = &cp[IPOPT_OLEN] - (u_char *)ip; goto bad; } if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { code = &cp[IPOPT_OFFSET] - (u_char *)ip; goto bad; } /* * If no space remains, ignore. */ off--; /* 0 origin */ if ((off + sizeof(struct in_addr)) > optlen) break; memset(&ipaddr, 0, sizeof(ipaddr)); ipaddr.sin_family = AF_INET; ipaddr.sin_len = sizeof(ipaddr); ipaddr.sin_addr = ip->ip_dst; /* * locate outgoing interface; if we're the destination, * use the incoming interface (should be same). * Again keep the packet inside the virtual instance. */ rt = rtalloc(sintosa(&ipaddr), RT_RESOLVE, rtableid); if (!rtisvalid(rt)) { type = ICMP_UNREACH; code = ICMP_UNREACH_HOST; rtfree(rt); goto bad; } ia = ifatoia(rt->rt_ifa); memcpy(cp + off, &ia->ia_addr.sin_addr, sizeof(struct in_addr)); rtfree(rt); cp[IPOPT_OFFSET] += sizeof(struct in_addr); break; case IPOPT_TS: code = cp - (u_char *)ip; if (optlen < sizeof(struct ip_timestamp)) goto bad; memcpy(&ipt, cp, sizeof(struct ip_timestamp)); if (ipt.ipt_ptr < 5 || ipt.ipt_len < 5) goto bad; if (ipt.ipt_ptr - 1 + sizeof(u_int32_t) > ipt.ipt_len) { if (++ipt.ipt_oflw == 0) goto bad; break; } memcpy(&sin, cp + ipt.ipt_ptr - 1, sizeof sin); switch (ipt.ipt_flg) { case IPOPT_TS_TSONLY: break; case IPOPT_TS_TSANDADDR: if (ipt.ipt_ptr - 1 + sizeof(u_int32_t) + sizeof(struct in_addr) > ipt.ipt_len) goto bad; memset(&ipaddr, 0, sizeof(ipaddr)); ipaddr.sin_family = AF_INET; ipaddr.sin_len = sizeof(ipaddr); ipaddr.sin_addr = dst; ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr), ifp)); if (ia == NULL) continue; memcpy(&sin, &ia->ia_addr.sin_addr, sizeof(struct in_addr)); ipt.ipt_ptr += sizeof(struct in_addr); break; case IPOPT_TS_PRESPEC: if (ipt.ipt_ptr - 1 + sizeof(u_int32_t) + sizeof(struct in_addr) > ipt.ipt_len) goto bad; memset(&ipaddr, 0, sizeof(ipaddr)); ipaddr.sin_family = AF_INET; ipaddr.sin_len = sizeof(ipaddr); ipaddr.sin_addr = sin; if (ifa_ifwithaddr(sintosa(&ipaddr), m->m_pkthdr.ph_rtableid) == NULL) continue; ipt.ipt_ptr += sizeof(struct in_addr); break; default: /* XXX can't take &ipt->ipt_flg */ code = (u_char *)&ipt.ipt_ptr - (u_char *)ip + 1; goto bad; } ntime = iptime(); memcpy(cp + ipt.ipt_ptr - 1, &ntime, sizeof(u_int32_t)); ipt.ipt_ptr += sizeof(u_int32_t); } } KERNEL_UNLOCK(); if (forward && ipforwarding) { ip_forward(m, ifp, NULL, 1); return (1); } return (0); bad: KERNEL_UNLOCK(); icmp_error(m, type, code, 0, 0); ipstat_inc(ips_badoptions); return (1); } /* * Save incoming source route for use in replies, * to be picked up later by ip_srcroute if the receiver is interested. */ void save_rte(struct mbuf *m, u_char *option, struct in_addr dst) { struct ip_srcrt *isr; struct m_tag *mtag; unsigned olen; olen = option[IPOPT_OLEN]; if (olen > sizeof(isr->isr_hdr) + sizeof(isr->isr_routes)) return; mtag = m_tag_get(PACKET_TAG_SRCROUTE, sizeof(*isr), M_NOWAIT); if (mtag == NULL) return; isr = (struct ip_srcrt *)(mtag + 1); memcpy(isr->isr_hdr, option, olen); isr->isr_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); isr->isr_dst = dst; m_tag_prepend(m, mtag); } /* * Retrieve incoming source route for use in replies, * in the same form used by setsockopt. * The first hop is placed before the options, will be removed later. */ struct mbuf * ip_srcroute(struct mbuf *m0) { struct in_addr *p, *q; struct mbuf *m; struct ip_srcrt *isr; struct m_tag *mtag; if (!ip_dosourceroute) return (NULL); mtag = m_tag_find(m0, PACKET_TAG_SRCROUTE, NULL); if (mtag == NULL) return (NULL); isr = (struct ip_srcrt *)(mtag + 1); if (isr->isr_nhops == 0) return (NULL); m = m_get(M_DONTWAIT, MT_SOOPTS); if (m == NULL) return (NULL); #define OPTSIZ (sizeof(isr->isr_nop) + sizeof(isr->isr_hdr)) /* length is (nhops+1)*sizeof(addr) + sizeof(nop + header) */ m->m_len = (isr->isr_nhops + 1) * sizeof(struct in_addr) + OPTSIZ; /* * First save first hop for return route */ p = &(isr->isr_routes[isr->isr_nhops - 1]); *(mtod(m, struct in_addr *)) = *p--; /* * Copy option fields and padding (nop) to mbuf. */ isr->isr_nop = IPOPT_NOP; isr->isr_hdr[IPOPT_OFFSET] = IPOPT_MINOFF; memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), &isr->isr_nop, OPTSIZ); q = (struct in_addr *)(mtod(m, caddr_t) + sizeof(struct in_addr) + OPTSIZ); #undef OPTSIZ /* * Record return path as an IP source route, * reversing the path (pointers are now aligned). */ while (p >= isr->isr_routes) { *q++ = *p--; } /* * Last hop goes to final destination. */ *q = isr->isr_dst; m_tag_delete(m0, (struct m_tag *)isr); return (m); } /* * Strip out IP options, at higher level protocol in the kernel. */ void ip_stripoptions(struct mbuf *m) { int i; struct ip *ip = mtod(m, struct ip *); caddr_t opts; int olen; olen = (ip->ip_hl<<2) - sizeof (struct ip); opts = (caddr_t)(ip + 1); i = m->m_len - (sizeof (struct ip) + olen); memmove(opts, opts + olen, i); m->m_len -= olen; if (m->m_flags & M_PKTHDR) m->m_pkthdr.len -= olen; ip->ip_hl = sizeof(struct ip) >> 2; ip->ip_len = htons(ntohs(ip->ip_len) - olen); } const int inetctlerrmap[PRC_NCMDS] = { 0, 0, 0, 0, 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, EMSGSIZE, EHOSTUNREACH, 0, 0, 0, 0, 0, 0, ENOPROTOOPT }; /* * Forward a packet. If some error occurs return the sender * an icmp packet. Note we can't always generate a meaningful * icmp message because icmp doesn't have a large enough repertoire * of codes and types. * * If not forwarding, just drop the packet. This could be confusing * if ipforwarding was zero but some routing protocol was advancing * us as a gateway to somewhere. However, we must let the routing * protocol deal with that. * * The srcrt parameter indicates whether the packet is being forwarded * via a source route. */ void ip_forward(struct mbuf *m, struct ifnet *ifp, struct rtentry *rt, int srcrt) { struct mbuf mfake, *mcopy = NULL; struct ip *ip = mtod(m, struct ip *); struct sockaddr_in *sin; struct route ro; int error, type = 0, code = 0, destmtu = 0, fake = 0, len; u_int32_t dest; dest = 0; if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { ipstat_inc(ips_cantforward); m_freem(m); goto freecopy; } if (ip->ip_ttl <= IPTTLDEC) { icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); goto freecopy; } sin = satosin(&ro.ro_dst); memset(sin, 0, sizeof(*sin)); sin->sin_family = AF_INET; sin->sin_len = sizeof(*sin); sin->sin_addr = ip->ip_dst; if (!rtisvalid(rt)) { rtfree(rt); rt = rtalloc_mpath(sintosa(sin), &ip->ip_src.s_addr, m->m_pkthdr.ph_rtableid); if (rt == NULL) { icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); return; } } /* * Save at most 68 bytes of the packet in case * we need to generate an ICMP message to the src. * The data is saved in the mbuf on the stack that * acts as a temporary storage not intended to be * passed down the IP stack or to the mfree. */ memset(&mfake.m_hdr, 0, sizeof(mfake.m_hdr)); mfake.m_type = m->m_type; if (m_dup_pkthdr(&mfake, m, M_DONTWAIT) == 0) { mfake.m_data = mfake.m_pktdat; len = min(ntohs(ip->ip_len), 68); m_copydata(m, 0, len, mfake.m_pktdat); mfake.m_pkthdr.len = mfake.m_len = len; #if NPF > 0 pf_pkt_addr_changed(&mfake); #endif /* NPF > 0 */ fake = 1; } ip->ip_ttl -= IPTTLDEC; /* * If forwarding packet using same interface that it came in on, * perhaps should send a redirect to sender to shortcut a hop. * Only send redirect if source is sending directly to us, * and if packet was not source routed (or has any options). * Also, don't send redirect if forwarding using a default route * or a route modified by a redirect. * Don't send redirect if we advertise destination's arp address * as ours (proxy arp). */ if ((rt->rt_ifidx == ifp->if_index) && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && satosin(rt_key(rt))->sin_addr.s_addr != 0 && ipsendredirects && !srcrt && !arpproxy(satosin(rt_key(rt))->sin_addr, m->m_pkthdr.ph_rtableid)) { if ((ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_netmask) == ifatoia(rt->rt_ifa)->ia_net) { if (rt->rt_flags & RTF_GATEWAY) dest = satosin(rt->rt_gateway)->sin_addr.s_addr; else dest = ip->ip_dst.s_addr; /* Router requirements says to only send host redirects */ type = ICMP_REDIRECT; code = ICMP_REDIRECT_HOST; } } ro.ro_rt = rt; ro.ro_tableid = m->m_pkthdr.ph_rtableid; error = ip_output(m, NULL, &ro, (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), NULL, NULL, 0); rt = ro.ro_rt; if (error) ipstat_inc(ips_cantforward); else { ipstat_inc(ips_forward); if (type) ipstat_inc(ips_redirectsent); else goto freecopy; } if (!fake) goto freecopy; switch (error) { case 0: /* forwarded, but need redirect */ /* type, code set above */ break; case ENETUNREACH: /* shouldn't happen, checked above */ case EHOSTUNREACH: case ENETDOWN: case EHOSTDOWN: default: type = ICMP_UNREACH; code = ICMP_UNREACH_HOST; break; case EMSGSIZE: type = ICMP_UNREACH; code = ICMP_UNREACH_NEEDFRAG; #ifdef IPSEC if (rt != NULL) { if (rt->rt_mtu) destmtu = rt->rt_mtu; else { struct ifnet *destifp; destifp = if_get(rt->rt_ifidx); if (destifp != NULL) destmtu = destifp->if_mtu; if_put(destifp); } } #endif /*IPSEC*/ ipstat_inc(ips_cantfrag); break; case EACCES: /* * pf(4) blocked the packet. There is no need to send an ICMP * packet back since pf(4) takes care of it. */ goto freecopy; case ENOBUFS: /* * a router should not generate ICMP_SOURCEQUENCH as * required in RFC1812 Requirements for IP Version 4 Routers. * source quench could be a big problem under DoS attacks, * or the underlying interface is rate-limited. */ goto freecopy; } mcopy = m_copym(&mfake, 0, len, M_DONTWAIT); if (mcopy) icmp_error(mcopy, type, code, dest, destmtu); freecopy: if (fake) m_tag_delete_chain(&mfake); rtfree(rt); } int ip_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen) { int error; #ifdef MROUTING extern int ip_mrtproto; extern struct mrtstat mrtstat; #endif /* Almost all sysctl names at this level are terminal. */ if (namelen != 1 && name[0] != IPCTL_IFQUEUE) return (ENOTDIR); switch (name[0]) { case IPCTL_SOURCEROUTE: /* * Don't allow this to change in a secure environment. */ if (newp && securelevel > 0) return (EPERM); NET_LOCK(); error = sysctl_int(oldp, oldlenp, newp, newlen, &ip_dosourceroute); NET_UNLOCK(); return (error); case IPCTL_MTUDISC: NET_LOCK(); error = sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtudisc); if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) { ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout); } else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) { rt_timer_queue_destroy(ip_mtudisc_timeout_q); ip_mtudisc_timeout_q = NULL; } NET_UNLOCK(); return error; case IPCTL_MTUDISCTIMEOUT: NET_LOCK(); error = sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtudisc_timeout); if (ip_mtudisc_timeout_q != NULL) rt_timer_queue_change(ip_mtudisc_timeout_q, ip_mtudisc_timeout); NET_UNLOCK(); return (error); #ifdef IPSEC case IPCTL_ENCDEBUG: case IPCTL_IPSEC_EXPIRE_ACQUIRE: case IPCTL_IPSEC_EMBRYONIC_SA_TIMEOUT: case IPCTL_IPSEC_REQUIRE_PFS: case IPCTL_IPSEC_SOFT_ALLOCATIONS: case IPCTL_IPSEC_ALLOCATIONS: case IPCTL_IPSEC_SOFT_BYTES: case IPCTL_IPSEC_BYTES: case IPCTL_IPSEC_TIMEOUT: case IPCTL_IPSEC_SOFT_TIMEOUT: case IPCTL_IPSEC_SOFT_FIRSTUSE: case IPCTL_IPSEC_FIRSTUSE: case IPCTL_IPSEC_ENC_ALGORITHM: case IPCTL_IPSEC_AUTH_ALGORITHM: case IPCTL_IPSEC_IPCOMP_ALGORITHM: return (ipsec_sysctl(name, namelen, oldp, oldlenp, newp, newlen)); #endif case IPCTL_IFQUEUE: return (sysctl_niq(name + 1, namelen - 1, oldp, oldlenp, newp, newlen, &ipintrq)); case IPCTL_STATS: return (ip_sysctl_ipstat(oldp, oldlenp, newp)); #ifdef MROUTING case IPCTL_MRTSTATS: return (sysctl_rdstruct(oldp, oldlenp, newp, &mrtstat, sizeof(mrtstat))); case IPCTL_MRTPROTO: return (sysctl_rdint(oldp, oldlenp, newp, ip_mrtproto)); case IPCTL_MRTMFC: if (newp) return (EPERM); NET_LOCK(); error = mrt_sysctl_mfc(oldp, oldlenp); NET_UNLOCK(); return (error); case IPCTL_MRTVIF: if (newp) return (EPERM); NET_LOCK(); error = mrt_sysctl_vif(oldp, oldlenp); NET_UNLOCK(); return (error); #else case IPCTL_MRTPROTO: case IPCTL_MRTSTATS: case IPCTL_MRTMFC: case IPCTL_MRTVIF: return (EOPNOTSUPP); #endif default: if (name[0] < IPCTL_MAXID) { NET_LOCK(); error = sysctl_int_arr(ipctl_vars, name, namelen, oldp, oldlenp, newp, newlen); NET_UNLOCK(); return (error); } return (EOPNOTSUPP); } /* NOTREACHED */ } int ip_sysctl_ipstat(void *oldp, size_t *oldlenp, void *newp) { uint64_t counters[ips_ncounters]; struct ipstat ipstat; u_long *words = (u_long *)&ipstat; int i; CTASSERT(sizeof(ipstat) == (nitems(counters) * sizeof(u_long))); memset(&ipstat, 0, sizeof ipstat); counters_read(ipcounters, counters, nitems(counters)); for (i = 0; i < nitems(counters); i++) words[i] = (u_long)counters[i]; return (sysctl_rdstruct(oldp, oldlenp, newp, &ipstat, sizeof(ipstat))); } void ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, struct mbuf *m) { if (inp->inp_socket->so_options & SO_TIMESTAMP) { struct timeval tv; microtime(&tv); *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), SCM_TIMESTAMP, SOL_SOCKET); if (*mp) mp = &(*mp)->m_next; } if (inp->inp_flags & INP_RECVDSTADDR) { *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); if (*mp) mp = &(*mp)->m_next; } #ifdef notyet /* this code is broken and will probably never be fixed. */ /* options were tossed already */ if (inp->inp_flags & INP_RECVOPTS) { *mp = sbcreatecontrol((caddr_t) opts_deleted_above, sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); if (*mp) mp = &(*mp)->m_next; } /* ip_srcroute doesn't do what we want here, need to fix */ if (inp->inp_flags & INP_RECVRETOPTS) { *mp = sbcreatecontrol((caddr_t) ip_srcroute(m), sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); if (*mp) mp = &(*mp)->m_next; } #endif if (inp->inp_flags & INP_RECVIF) { struct sockaddr_dl sdl; struct ifnet *ifp; ifp = if_get(m->m_pkthdr.ph_ifidx); if (ifp == NULL || ifp->if_sadl == NULL) { memset(&sdl, 0, sizeof(sdl)); sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]); sdl.sdl_family = AF_LINK; sdl.sdl_index = ifp != NULL ? ifp->if_index : 0; sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0; *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len, IP_RECVIF, IPPROTO_IP); } else { *mp = sbcreatecontrol((caddr_t) ifp->if_sadl, ifp->if_sadl->sdl_len, IP_RECVIF, IPPROTO_IP); } if (*mp) mp = &(*mp)->m_next; if_put(ifp); } if (inp->inp_flags & INP_RECVTTL) { *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl, sizeof(u_int8_t), IP_RECVTTL, IPPROTO_IP); if (*mp) mp = &(*mp)->m_next; } if (inp->inp_flags & INP_RECVRTABLE) { u_int rtableid = inp->inp_rtableid; #if NPF > 0 if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) { struct pf_divert *divert; divert = pf_find_divert(m); KASSERT(divert != NULL); rtableid = divert->rdomain; } #endif *mp = sbcreatecontrol((caddr_t) &rtableid, sizeof(u_int), IP_RECVRTABLE, IPPROTO_IP); if (*mp) mp = &(*mp)->m_next; } } void ip_send_dispatch(void *xmq) { struct mbuf_queue *mq = xmq; struct mbuf *m; struct mbuf_list ml; mq_delist(mq, &ml); if (ml_empty(&ml)) return; NET_RLOCK(); while ((m = ml_dequeue(&ml)) != NULL) { ip_output(m, NULL, NULL, 0, NULL, NULL, 0); } NET_RUNLOCK(); } void ip_send(struct mbuf *m) { mq_enqueue(&ipsend_mq, m); task_add(net_tq(0), &ipsend_task); } /* $OpenBSD: uipc_domain.c,v 1.55 2017/11/23 13:45:46 mpi Exp $ */ /* $NetBSD: uipc_domain.c,v 1.14 1996/02/09 19:00:44 christos Exp $ */ /* * Copyright (c) 1982, 1986, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)uipc_domain.c 8.2 (Berkeley) 10/18/93 */ #ifndef CPA_sys_param_h #define CPA_sys_param_h 1 #include #endif #ifndef CPA_sys_socket_h #define CPA_sys_socket_h 1 #include #endif #ifndef CPA_sys_protosw_h #define CPA_sys_protosw_h 1 #include #endif #ifndef CPA_sys_domain_h #define CPA_sys_domain_h 1 #include #endif #ifndef CPA_sys_mbuf_h #define CPA_sys_mbuf_h 1 #include #endif #ifndef CPA_sys_time_h #define CPA_sys_time_h 1 #include #endif #ifndef CPA_sys_systm_h #define CPA_sys_systm_h 1 #include #endif #ifndef CPA_sys_sysctl_h #define CPA_sys_sysctl_h 1 #include #endif #ifndef CPA_sys_timeout_h #define CPA_sys_timeout_h 1 #include #endif #ifndef CPA_bpfilter_h #define CPA_bpfilter_h 1 #include "bpfilter.h" #endif #ifndef CPA_pflow_h #define CPA_pflow_h 1 #include "pflow.h" #endif extern struct domain mplsdomain; extern struct domain pfkeydomain; extern struct domain inet6domain; extern struct domain inetdomain; extern struct domain unixdomain; extern struct domain routedomain; struct domain *domains[] = { #ifdef MPLS &mplsdomain, #endif #if defined (IPSEC) || defined (TCP_SIGNATURE) &pfkeydomain, #endif #ifdef INET6 &inet6domain, #endif /* INET6 */ &inetdomain, &unixdomain, &routedomain, NULL }; void pffasttimo(void *); void pfslowtimo(void *); struct domain * pffinddomain(int); void domaininit(void) { struct domain *dp; const struct protosw *pr; static struct timeout pffast_timeout; static struct timeout pfslow_timeout; int i; for (i = 0; (dp = domains[i]) != NULL; i++) { if (dp->dom_init) (*dp->dom_init)(); for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) if (pr->pr_init) (*pr->pr_init)(); } /* * max_linkhdr of 64 was chosen to encompass tunnelling * traffic in IP payloads, eg, by etherip(4) or gif(4), * without needing to prepend an mbuf to fit those * headers. */ if (max_linkhdr < 64) max_linkhdr = 64; max_hdr = max_linkhdr + max_protohdr; timeout_set_proc(&pffast_timeout, pffasttimo, &pffast_timeout); timeout_set_proc(&pfslow_timeout, pfslowtimo, &pfslow_timeout); timeout_add(&pffast_timeout, 1); timeout_add(&pfslow_timeout, 1); } struct domain * pffinddomain(int family) { struct domain *dp; int i; for (i = 0; (dp = domains[i]) != NULL; i++) { if (dp->dom_family == family) return (dp); } return (NULL); } const struct protosw * pffindtype(int family, int type) { struct domain *dp; const struct protosw *pr; dp = pffinddomain(family); if (dp == NULL) return (NULL); for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) if (pr->pr_type && pr->pr_type == type) return (pr); return (NULL); } const struct protosw * pffindproto(int family, int protocol, int type) { struct domain *dp; const struct protosw *pr; const struct protosw *maybe = NULL; if (family == 0) return (NULL); dp = pffinddomain(family); if (dp == NULL) return (NULL); for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { if ((pr->pr_protocol == protocol) && (pr->pr_type == type)) return (pr); if (type == SOCK_RAW && pr->pr_type == SOCK_RAW && pr->pr_protocol == 0 && maybe == NULL) maybe = pr; } return (maybe); } int net_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen, struct proc *p) { struct domain *dp; const struct protosw *pr; int error, family, protocol; /* * All sysctl names at this level are nonterminal. * Usually: next two components are protocol family and protocol * number, then at least one addition component. */ if (namelen < 2) return (EISDIR); /* overloaded */ family = name[0]; if (family == 0) return (0); #if NBPFILTER > 0 if (family == PF_BPF) return (bpf_sysctl(name + 1, namelen - 1, oldp, oldlenp, newp, newlen)); #endif #if NPFLOW > 0 if (family == PF_PFLOW) return (pflow_sysctl(name + 1, namelen - 1, oldp, oldlenp, newp, newlen)); #endif #ifdef PIPEX if (family == PF_PIPEX) return (pipex_sysctl(name + 1, namelen - 1, oldp, oldlenp, newp, newlen)); #endif #ifdef MPLS if (family == PF_MPLS) return (mpls_sysctl(name + 1, namelen - 1, oldp, oldlenp, newp, newlen)); #endif dp = pffinddomain(family); if (dp == NULL) return (ENOPROTOOPT); if (namelen < 3) return (EISDIR); /* overloaded */ protocol = name[1]; for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) if (pr->pr_protocol == protocol && pr->pr_sysctl) { error = (*pr->pr_sysctl)(name + 2, namelen - 2, oldp, oldlenp, newp, newlen); return (error); } return (ENOPROTOOPT); } void pfctlinput(int cmd, struct sockaddr *sa) { struct domain *dp; const struct protosw *pr; int i; NET_ASSERT_LOCKED(); for (i = 0; (dp = domains[i]) != NULL; i++) { for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) if (pr->pr_ctlinput) (*pr->pr_ctlinput)(cmd, sa, 0, NULL); } } void pfslowtimo(void *arg) { struct timeout *to = (struct timeout *)arg; struct domain *dp; const struct protosw *pr; int i; for (i = 0; (dp = domains[i]) != NULL; i++) { for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) if (pr->pr_slowtimo) (*pr->pr_slowtimo)(); } timeout_add_msec(to, 500); } void pffasttimo(void *arg) { struct timeout *to = (struct timeout *)arg; struct domain *dp; const struct protosw *pr; int i; for (i = 0; (dp = domains[i]) != NULL; i++) { for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) if (pr->pr_fasttimo) (*pr->pr_fasttimo)(); } timeout_add_msec(to, 200); } void *__VERIFIER_nondet_pointer(void) ; void __VERIFIER_error(void) ; void __VERIFIER_assume(int expression) ; int __VERIFIER_nondet_int(void) ; void panic(const char *fmt, ...); void int main(void); void panic(const char *fmt, ...) { __VERIFIER_error(); } int main() { struct mbuf *m; int len, off; ip_init(); ip6_init(); m = m_gethdr(0, MT_HEADER); m_tag_init(m); len = __VERIFIER_nondet_int(); __VERIFIER_assume(len > 0); __VERIFIER_assume(len <= MHLEN); off = __VERIFIER_nondet_int(); __VERIFIER_assume(off > 0); __VERIFIER_assume(off <= len); m->m_hdr.mh_len = m->M_dat.MH.MH_pkthdr.len = len; ip_deliver(&m, &off, IPPROTO_IP, AF_INET6); }