Annotation of src/sys/netinet/ip_input.c, Revision 1.71
1.71 ! tls 1: /* $NetBSD: ip_input.c,v 1.70 1998/09/09 04:57:18 thorpej Exp $ */
1.14 cgd 2:
1.1 cgd 3: /*
1.13 mycroft 4: * Copyright (c) 1982, 1986, 1988, 1993
5: * The Regents of the University of California. All rights reserved.
1.1 cgd 6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: *
1.14 cgd 35: * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
1.71 ! tls 36: */
! 37:
! 38: /*-
! 39: * Copyright (c) 1998 The NetBSD Foundation, Inc.
! 40: * All rights reserved.
! 41: *
! 42: * This code is derived from software contributed to The NetBSD Foundation
! 43: * by Public Access Networks Corporation ("Panix"). It was developed under
! 44: * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
! 45: *
! 46: * Redistribution and use in source and binary forms, with or without
! 47: * modification, are permitted provided that the following conditions
! 48: * are met:
! 49: * 1. Redistributions of source code must retain the above copyright
! 50: * notice, this list of conditions and the following disclaimer.
! 51: * 2. Redistributions in binary form must reproduce the above copyright
! 52: * notice, this list of conditions and the following disclaimer in the
! 53: * documentation and/or other materials provided with the distribution.
! 54: * 3. All advertising materials mentioning features or use of this software
! 55: * must display the following acknowledgement:
! 56: * This product includes software developed by the NetBSD
! 57: * Foundation, Inc. and its contributors.
! 58: * 4. Neither the name of The NetBSD Foundation nor the names of its
! 59: * contributors may be used to endorse or promote products derived
! 60: * from this software without specific prior written permission.
! 61: *
! 62: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
! 63: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
! 64: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
! 65: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
! 66: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
! 67: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
! 68: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
! 69: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
! 70: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
! 71: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
! 72: * POSSIBILITY OF SUCH DAMAGE.
1.1 cgd 73: */
1.55 scottr 74:
1.62 matt 75: #include "opt_gateway.h"
1.69 mrg 76: #include "opt_pfil_hooks.h"
1.55 scottr 77: #include "opt_mrouting.h"
1.1 cgd 78:
1.5 mycroft 79: #include <sys/param.h>
80: #include <sys/systm.h>
81: #include <sys/malloc.h>
82: #include <sys/mbuf.h>
83: #include <sys/domain.h>
84: #include <sys/protosw.h>
85: #include <sys/socket.h>
1.44 thorpej 86: #include <sys/socketvar.h>
1.5 mycroft 87: #include <sys/errno.h>
88: #include <sys/time.h>
89: #include <sys/kernel.h>
1.28 christos 90: #include <sys/proc.h>
91:
92: #include <vm/vm.h>
93: #include <sys/sysctl.h>
1.1 cgd 94:
1.5 mycroft 95: #include <net/if.h>
1.44 thorpej 96: #include <net/if_dl.h>
1.5 mycroft 97: #include <net/route.h>
1.45 mrg 98: #include <net/pfil.h>
1.1 cgd 99:
1.5 mycroft 100: #include <netinet/in.h>
101: #include <netinet/in_systm.h>
102: #include <netinet/ip.h>
103: #include <netinet/in_pcb.h>
104: #include <netinet/in_var.h>
105: #include <netinet/ip_var.h>
106: #include <netinet/ip_icmp.h>
1.44 thorpej 107:
1.1 cgd 108: #ifndef IPFORWARDING
109: #ifdef GATEWAY
110: #define IPFORWARDING 1 /* forward IP packets not for us */
111: #else /* GATEWAY */
112: #define IPFORWARDING 0 /* don't forward IP packets not for us */
113: #endif /* GATEWAY */
114: #endif /* IPFORWARDING */
115: #ifndef IPSENDREDIRECTS
116: #define IPSENDREDIRECTS 1
117: #endif
1.26 thorpej 118: #ifndef IPFORWSRCRT
1.47 cjs 119: #define IPFORWSRCRT 1 /* forward source-routed packets */
120: #endif
121: #ifndef IPALLOWSRCRT
1.48 mrg 122: #define IPALLOWSRCRT 1 /* allow source-routed packets */
1.26 thorpej 123: #endif
1.53 kml 124: #ifndef IPMTUDISC
125: #define IPMTUDISC 0
126: #endif
1.60 kml 127: #ifndef IPMTUDISCTIMEOUT
1.61 kml 128: #define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */
1.60 kml 129: #endif
1.53 kml 130:
1.27 thorpej 131: /*
132: * Note: DIRECTED_BROADCAST is handled this way so that previous
133: * configuration using this option will Just Work.
134: */
135: #ifndef IPDIRECTEDBCAST
136: #ifdef DIRECTED_BROADCAST
137: #define IPDIRECTEDBCAST 1
138: #else
139: #define IPDIRECTEDBCAST 0
140: #endif /* DIRECTED_BROADCAST */
141: #endif /* IPDIRECTEDBCAST */
1.1 cgd 142: int ipforwarding = IPFORWARDING;
143: int ipsendredirects = IPSENDREDIRECTS;
1.13 mycroft 144: int ip_defttl = IPDEFTTL;
1.26 thorpej 145: int ip_forwsrcrt = IPFORWSRCRT;
1.27 thorpej 146: int ip_directedbcast = IPDIRECTEDBCAST;
1.47 cjs 147: int ip_allowsrcrt = IPALLOWSRCRT;
1.53 kml 148: int ip_mtudisc = IPMTUDISC;
1.60 kml 149: u_int ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
1.1 cgd 150: #ifdef DIAGNOSTIC
151: int ipprintfs = 0;
152: #endif
153:
1.60 kml 154: struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
155:
1.1 cgd 156: extern struct domain inetdomain;
157: extern struct protosw inetsw[];
158: u_char ip_protox[IPPROTO_MAX];
159: int ipqmaxlen = IFQ_MAXLEN;
1.22 mycroft 160: struct in_ifaddrhead in_ifaddr;
1.57 tls 161: struct in_ifaddrhashhead *in_ifaddrhashtbl;
1.13 mycroft 162: struct ifqueue ipintrq;
1.63 matt 163: struct ipstat ipstat;
164: u_int16_t ip_id;
165: int ip_defttl;
166: struct ipqhead ipq;
1.1 cgd 167:
168: /*
169: * We need to save the IP options in case a protocol wants to respond
170: * to an incoming packet over the same route if the packet got here
171: * using IP source routing. This allows connection establishment and
172: * maintenance when the remote end is on a network that is not known
173: * to us.
174: */
175: int ip_nhops = 0;
176: static struct ip_srcrt {
177: struct in_addr dst; /* final destination */
178: char nop; /* one NOP to align */
179: char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
180: struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
181: } ip_srcrt;
182:
1.13 mycroft 183: static void save_rte __P((u_char *, struct in_addr));
1.35 mycroft 184:
1.1 cgd 185: /*
186: * IP initialization: fill in IP protocol switch table.
187: * All protocols not implemented in kernel go to raw IP protocol handler.
188: */
1.8 mycroft 189: void
1.1 cgd 190: ip_init()
191: {
192: register struct protosw *pr;
193: register int i;
194:
195: pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
196: if (pr == 0)
197: panic("ip_init");
198: for (i = 0; i < IPPROTO_MAX; i++)
199: ip_protox[i] = pr - inetsw;
200: for (pr = inetdomain.dom_protosw;
201: pr < inetdomain.dom_protoswNPROTOSW; pr++)
202: if (pr->pr_domain->dom_family == PF_INET &&
203: pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
204: ip_protox[pr->pr_protocol] = pr - inetsw;
1.25 cgd 205: LIST_INIT(&ipq);
1.1 cgd 206: ip_id = time.tv_sec & 0xffff;
207: ipintrq.ifq_maxlen = ipqmaxlen;
1.22 mycroft 208: TAILQ_INIT(&in_ifaddr);
1.57 tls 209: in_ifaddrhashtbl =
210: hashinit(IN_IFADDR_HASH_SIZE, M_IFADDR, M_WAITOK, &in_ifaddrhash);
1.60 kml 211: if (ip_mtudisc != 0)
212: ip_mtudisc_timeout_q =
213: rt_timer_queue_create(ip_mtudisc_timeout);
1.1 cgd 214: }
215:
216: struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
217: struct route ipforward_rt;
218:
219: /*
220: * Ip input routine. Checksum and byte swap header. If fragmented
221: * try to reassemble. Process options. Pass to next level.
222: */
1.8 mycroft 223: void
1.1 cgd 224: ipintr()
225: {
1.33 mrg 226: register struct ip *ip = NULL;
1.1 cgd 227: register struct mbuf *m;
228: register struct ipq *fp;
229: register struct in_ifaddr *ia;
1.57 tls 230: register struct ifaddr *ifa;
1.25 cgd 231: struct ipqent *ipqe;
1.35 mycroft 232: int hlen = 0, mff, len, s;
1.36 mrg 233: #ifdef PFIL_HOOKS
1.33 mrg 234: struct packet_filter_hook *pfh;
235: struct mbuf *m0;
1.43 mrg 236: int rv;
1.36 mrg 237: #endif /* PFIL_HOOKS */
1.1 cgd 238:
239: next:
240: /*
241: * Get next datagram off input queue and get IP header
242: * in first mbuf.
243: */
244: s = splimp();
245: IF_DEQUEUE(&ipintrq, m);
246: splx(s);
1.13 mycroft 247: if (m == 0)
1.1 cgd 248: return;
249: #ifdef DIAGNOSTIC
250: if ((m->m_flags & M_PKTHDR) == 0)
251: panic("ipintr no HDR");
252: #endif
253: /*
254: * If no IP addresses have been set yet but the interfaces
255: * are receiving, can't do anything with incoming packets yet.
256: */
1.22 mycroft 257: if (in_ifaddr.tqh_first == 0)
1.1 cgd 258: goto bad;
259: ipstat.ips_total++;
260: if (m->m_len < sizeof (struct ip) &&
261: (m = m_pullup(m, sizeof (struct ip))) == 0) {
262: ipstat.ips_toosmall++;
263: goto next;
264: }
265: ip = mtod(m, struct ip *);
1.13 mycroft 266: if (ip->ip_v != IPVERSION) {
267: ipstat.ips_badvers++;
268: goto bad;
269: }
1.1 cgd 270: hlen = ip->ip_hl << 2;
271: if (hlen < sizeof(struct ip)) { /* minimum header length */
272: ipstat.ips_badhlen++;
273: goto bad;
274: }
275: if (hlen > m->m_len) {
276: if ((m = m_pullup(m, hlen)) == 0) {
277: ipstat.ips_badhlen++;
278: goto next;
279: }
280: ip = mtod(m, struct ip *);
281: }
1.28 christos 282: if ((ip->ip_sum = in_cksum(m, hlen)) != 0) {
1.1 cgd 283: ipstat.ips_badsum++;
284: goto bad;
285: }
286:
287: /*
288: * Convert fields to host representation.
289: */
290: NTOHS(ip->ip_len);
291: NTOHS(ip->ip_id);
292: NTOHS(ip->ip_off);
1.35 mycroft 293: len = ip->ip_len;
1.1 cgd 294:
295: /*
296: * Check that the amount of data in the buffers
297: * is as at least much as the IP header would have us expect.
298: * Trim mbufs if longer than we expect.
299: * Drop packet if shorter than we expect.
300: */
1.35 mycroft 301: if (m->m_pkthdr.len < len) {
1.1 cgd 302: ipstat.ips_tooshort++;
303: goto bad;
304: }
1.35 mycroft 305: if (m->m_pkthdr.len > len) {
1.1 cgd 306: if (m->m_len == m->m_pkthdr.len) {
1.35 mycroft 307: m->m_len = len;
308: m->m_pkthdr.len = len;
1.1 cgd 309: } else
1.35 mycroft 310: m_adj(m, len - m->m_pkthdr.len);
1.1 cgd 311: }
312:
1.64 thorpej 313: /*
314: * Assume that we can create a fast-forward IP flow entry
315: * based on this packet.
316: */
317: m->m_flags |= M_CANFASTFWD;
318:
1.36 mrg 319: #ifdef PFIL_HOOKS
1.33 mrg 320: /*
1.64 thorpej 321: * Run through list of hooks for input packets. If there are any
322: * filters which require that additional packets in the flow are
323: * not fast-forwarded, they must clear the M_CANFASTFWD flag.
324: * Note that filters must _never_ set this flag, as another filter
325: * in the list may have previously cleared it.
1.33 mrg 326: */
327: m0 = m;
1.59 mrg 328: for (pfh = pfil_hook_get(PFIL_IN); pfh; pfh = pfh->pfil_link.tqe_next)
1.33 mrg 329: if (pfh->pfil_func) {
1.43 mrg 330: rv = pfh->pfil_func(ip, hlen, m->m_pkthdr.rcvif, 0, &m0);
331: if (rv)
1.40 veego 332: goto next;
1.68 sommerfe 333: m = m0;
334: if (m == NULL)
335: goto next;
336: ip = mtod(m, struct ip *);
1.33 mrg 337: }
1.36 mrg 338: #endif /* PFIL_HOOKS */
1.33 mrg 339:
1.1 cgd 340: /*
341: * Process options and, if not destined for us,
342: * ship it on. ip_dooptions returns 1 when an
343: * error was detected (causing an icmp message
344: * to be sent and the original packet to be freed).
345: */
346: ip_nhops = 0; /* for source routed packets */
347: if (hlen > sizeof (struct ip) && ip_dooptions(m))
348: goto next;
349:
350: /*
351: * Check our list of addresses, to see if the packet is for us.
352: */
1.57 tls 353: INADDR_TO_IA(ip->ip_dst, ia);
354: if (ia != NULL) goto ours;
355: if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
356: for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
357: ifa != NULL; ifa = ifa->ifa_list.tqe_next) {
358: if (ifa->ifa_addr->sa_family != AF_INET) continue;
359: ia = ifatoia(ifa);
1.35 mycroft 360: if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
361: in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
1.20 mycroft 362: /*
363: * Look for all-0's host part (old broadcast addr),
364: * either for subnet or net.
365: */
366: ip->ip_dst.s_addr == ia->ia_subnet ||
1.18 mycroft 367: ip->ip_dst.s_addr == ia->ia_net)
1.1 cgd 368: goto ours;
1.57 tls 369: /*
370: * An interface with IP address zero accepts
371: * all packets that arrive on that interface.
372: */
373: if (in_nullhost(ia->ia_addr.sin_addr))
374: goto ours;
1.1 cgd 375: }
376: }
1.18 mycroft 377: if (IN_MULTICAST(ip->ip_dst.s_addr)) {
1.4 hpeyerl 378: struct in_multi *inm;
379: #ifdef MROUTING
380: extern struct socket *ip_mrouter;
1.10 brezak 381:
382: if (m->m_flags & M_EXT) {
383: if ((m = m_pullup(m, hlen)) == 0) {
384: ipstat.ips_toosmall++;
385: goto next;
386: }
387: ip = mtod(m, struct ip *);
388: }
1.4 hpeyerl 389:
390: if (ip_mrouter) {
391: /*
392: * If we are acting as a multicast router, all
393: * incoming multicast packets are passed to the
394: * kernel-level multicast forwarding function.
395: * The packet is returned (relatively) intact; if
396: * ip_mforward() returns a non-zero value, the packet
397: * must be discarded, else it may be accepted below.
398: *
399: * (The IP ident field is put in the same byte order
400: * as expected when ip_mforward() is called from
401: * ip_output().)
402: */
403: ip->ip_id = htons(ip->ip_id);
1.13 mycroft 404: if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
405: ipstat.ips_cantforward++;
1.4 hpeyerl 406: m_freem(m);
407: goto next;
408: }
409: ip->ip_id = ntohs(ip->ip_id);
410:
411: /*
412: * The process-level routing demon needs to receive
413: * all multicast IGMP packets, whether or not this
414: * host belongs to their destination groups.
415: */
416: if (ip->ip_p == IPPROTO_IGMP)
417: goto ours;
1.13 mycroft 418: ipstat.ips_forward++;
1.4 hpeyerl 419: }
420: #endif
421: /*
422: * See if we belong to the destination multicast group on the
423: * arrival interface.
424: */
425: IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
426: if (inm == NULL) {
1.13 mycroft 427: ipstat.ips_cantforward++;
1.4 hpeyerl 428: m_freem(m);
429: goto next;
430: }
431: goto ours;
432: }
1.19 mycroft 433: if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
1.35 mycroft 434: in_nullhost(ip->ip_dst))
1.1 cgd 435: goto ours;
436:
437: /*
438: * Not for us; forward if possible and desirable.
439: */
440: if (ipforwarding == 0) {
441: ipstat.ips_cantforward++;
442: m_freem(m);
443: } else
444: ip_forward(m, 0);
445: goto next;
446:
447: ours:
448: /*
449: * If offset or IP_MF are set, must reassemble.
450: * Otherwise, nothing need be done.
451: * (We could look in the reassembly queue to see
452: * if the packet was previously fragmented,
453: * but it's not worth the time; just let them time out.)
454: */
1.37 perry 455: if (ip->ip_off & ~(IP_DF|IP_RF)) {
1.1 cgd 456: /*
457: * Look for queue of fragments
458: * of this datagram.
459: */
1.25 cgd 460: for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next)
1.1 cgd 461: if (ip->ip_id == fp->ipq_id &&
1.35 mycroft 462: in_hosteq(ip->ip_src, fp->ipq_src) &&
463: in_hosteq(ip->ip_dst, fp->ipq_dst) &&
1.1 cgd 464: ip->ip_p == fp->ipq_p)
465: goto found;
466: fp = 0;
467: found:
468:
469: /*
470: * Adjust ip_len to not reflect header,
1.25 cgd 471: * set ipqe_mff if more fragments are expected,
1.1 cgd 472: * convert offset of this to bytes.
473: */
474: ip->ip_len -= hlen;
1.25 cgd 475: mff = (ip->ip_off & IP_MF) != 0;
476: if (mff) {
1.16 cgd 477: /*
478: * Make sure that fragments have a data length
479: * that's a non-zero multiple of 8 bytes.
480: */
1.17 cgd 481: if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
1.16 cgd 482: ipstat.ips_badfrags++;
483: goto bad;
484: }
485: }
1.1 cgd 486: ip->ip_off <<= 3;
487:
488: /*
489: * If datagram marked as having more fragments
490: * or if this is not the first fragment,
491: * attempt reassembly; if it succeeds, proceed.
492: */
1.25 cgd 493: if (mff || ip->ip_off) {
1.1 cgd 494: ipstat.ips_fragments++;
1.25 cgd 495: MALLOC(ipqe, struct ipqent *, sizeof (struct ipqent),
496: M_IPQ, M_NOWAIT);
497: if (ipqe == NULL) {
498: ipstat.ips_rcvmemdrop++;
499: goto bad;
500: }
501: ipqe->ipqe_mff = mff;
1.50 thorpej 502: ipqe->ipqe_m = m;
1.25 cgd 503: ipqe->ipqe_ip = ip;
1.50 thorpej 504: m = ip_reass(ipqe, fp);
505: if (m == 0)
1.1 cgd 506: goto next;
1.13 mycroft 507: ipstat.ips_reassembled++;
1.50 thorpej 508: ip = mtod(m, struct ip *);
1.1 cgd 509: } else
510: if (fp)
511: ip_freef(fp);
512: } else
513: ip->ip_len -= hlen;
514:
515: /*
516: * Switch out to protocol's input routine.
517: */
518: ipstat.ips_delivered++;
519: (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
520: goto next;
521: bad:
522: m_freem(m);
523: goto next;
524: }
525:
526: /*
527: * Take incoming datagram fragment and try to
528: * reassemble it into whole datagram. If a chain for
529: * reassembly of this datagram already exists, then it
530: * is given as fp; otherwise have to make a chain.
531: */
1.50 thorpej 532: struct mbuf *
1.25 cgd 533: ip_reass(ipqe, fp)
534: register struct ipqent *ipqe;
1.1 cgd 535: register struct ipq *fp;
536: {
1.50 thorpej 537: register struct mbuf *m = ipqe->ipqe_m;
1.25 cgd 538: register struct ipqent *nq, *p, *q;
539: struct ip *ip;
1.1 cgd 540: struct mbuf *t;
1.25 cgd 541: int hlen = ipqe->ipqe_ip->ip_hl << 2;
1.1 cgd 542: int i, next;
543:
544: /*
545: * Presence of header sizes in mbufs
546: * would confuse code below.
547: */
548: m->m_data += hlen;
549: m->m_len -= hlen;
550:
551: /*
552: * If first fragment to arrive, create a reassembly queue.
553: */
554: if (fp == 0) {
1.50 thorpej 555: MALLOC(fp, struct ipq *, sizeof (struct ipq),
556: M_FTABLE, M_NOWAIT);
557: if (fp == NULL)
1.1 cgd 558: goto dropfrag;
1.25 cgd 559: LIST_INSERT_HEAD(&ipq, fp, ipq_q);
1.1 cgd 560: fp->ipq_ttl = IPFRAGTTL;
1.25 cgd 561: fp->ipq_p = ipqe->ipqe_ip->ip_p;
562: fp->ipq_id = ipqe->ipqe_ip->ip_id;
563: LIST_INIT(&fp->ipq_fragq);
564: fp->ipq_src = ipqe->ipqe_ip->ip_src;
565: fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
566: p = NULL;
1.1 cgd 567: goto insert;
568: }
569:
570: /*
571: * Find a segment which begins after this one does.
572: */
1.25 cgd 573: for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
574: p = q, q = q->ipqe_q.le_next)
575: if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off)
1.1 cgd 576: break;
577:
578: /*
579: * If there is a preceding segment, it may provide some of
580: * our data already. If so, drop the data from the incoming
581: * segment. If it provides all of our data, drop us.
582: */
1.25 cgd 583: if (p != NULL) {
584: i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len -
585: ipqe->ipqe_ip->ip_off;
1.1 cgd 586: if (i > 0) {
1.25 cgd 587: if (i >= ipqe->ipqe_ip->ip_len)
1.1 cgd 588: goto dropfrag;
1.50 thorpej 589: m_adj(ipqe->ipqe_m, i);
1.25 cgd 590: ipqe->ipqe_ip->ip_off += i;
591: ipqe->ipqe_ip->ip_len -= i;
1.1 cgd 592: }
593: }
594:
595: /*
596: * While we overlap succeeding segments trim them or,
597: * if they are completely covered, dequeue them.
598: */
1.25 cgd 599: for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len >
600: q->ipqe_ip->ip_off; q = nq) {
601: i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) -
602: q->ipqe_ip->ip_off;
603: if (i < q->ipqe_ip->ip_len) {
604: q->ipqe_ip->ip_len -= i;
605: q->ipqe_ip->ip_off += i;
1.50 thorpej 606: m_adj(q->ipqe_m, i);
1.1 cgd 607: break;
608: }
1.25 cgd 609: nq = q->ipqe_q.le_next;
1.50 thorpej 610: m_freem(q->ipqe_m);
1.25 cgd 611: LIST_REMOVE(q, ipqe_q);
612: FREE(q, M_IPQ);
1.1 cgd 613: }
614:
615: insert:
616: /*
617: * Stick new segment in its place;
618: * check for complete reassembly.
619: */
1.25 cgd 620: if (p == NULL) {
621: LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
622: } else {
623: LIST_INSERT_AFTER(p, ipqe, ipqe_q);
624: }
1.1 cgd 625: next = 0;
1.25 cgd 626: for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
627: p = q, q = q->ipqe_q.le_next) {
628: if (q->ipqe_ip->ip_off != next)
1.1 cgd 629: return (0);
1.25 cgd 630: next += q->ipqe_ip->ip_len;
1.1 cgd 631: }
1.25 cgd 632: if (p->ipqe_mff)
1.1 cgd 633: return (0);
634:
635: /*
1.41 thorpej 636: * Reassembly is complete. Check for a bogus message size and
637: * concatenate fragments.
1.1 cgd 638: */
1.25 cgd 639: q = fp->ipq_fragq.lh_first;
640: ip = q->ipqe_ip;
1.41 thorpej 641: if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
642: ipstat.ips_toolong++;
643: ip_freef(fp);
644: return (0);
645: }
1.50 thorpej 646: m = q->ipqe_m;
1.1 cgd 647: t = m->m_next;
648: m->m_next = 0;
649: m_cat(m, t);
1.25 cgd 650: nq = q->ipqe_q.le_next;
651: FREE(q, M_IPQ);
652: for (q = nq; q != NULL; q = nq) {
1.50 thorpej 653: t = q->ipqe_m;
1.25 cgd 654: nq = q->ipqe_q.le_next;
655: FREE(q, M_IPQ);
1.1 cgd 656: m_cat(m, t);
657: }
658:
659: /*
660: * Create header for new ip packet by
661: * modifying header of first packet;
662: * dequeue and discard fragment reassembly header.
663: * Make header visible.
664: */
665: ip->ip_len = next;
1.25 cgd 666: ip->ip_src = fp->ipq_src;
667: ip->ip_dst = fp->ipq_dst;
668: LIST_REMOVE(fp, ipq_q);
1.50 thorpej 669: FREE(fp, M_FTABLE);
1.1 cgd 670: m->m_len += (ip->ip_hl << 2);
671: m->m_data -= (ip->ip_hl << 2);
672: /* some debugging cruft by sklower, below, will go away soon */
673: if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
674: register int plen = 0;
1.50 thorpej 675: for (t = m; t; t = t->m_next)
676: plen += t->m_len;
677: m->m_pkthdr.len = plen;
1.1 cgd 678: }
1.50 thorpej 679: return (m);
1.1 cgd 680:
681: dropfrag:
682: ipstat.ips_fragdropped++;
683: m_freem(m);
1.25 cgd 684: FREE(ipqe, M_IPQ);
1.1 cgd 685: return (0);
686: }
687:
688: /*
689: * Free a fragment reassembly header and all
690: * associated datagrams.
691: */
1.8 mycroft 692: void
1.1 cgd 693: ip_freef(fp)
694: struct ipq *fp;
695: {
1.25 cgd 696: register struct ipqent *q, *p;
1.1 cgd 697:
1.25 cgd 698: for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) {
699: p = q->ipqe_q.le_next;
1.50 thorpej 700: m_freem(q->ipqe_m);
1.25 cgd 701: LIST_REMOVE(q, ipqe_q);
702: FREE(q, M_IPQ);
1.1 cgd 703: }
1.25 cgd 704: LIST_REMOVE(fp, ipq_q);
1.50 thorpej 705: FREE(fp, M_FTABLE);
1.1 cgd 706: }
707:
708: /*
709: * IP timer processing;
710: * if a timer expires on a reassembly
711: * queue, discard it.
712: */
1.8 mycroft 713: void
1.1 cgd 714: ip_slowtimo()
715: {
1.25 cgd 716: register struct ipq *fp, *nfp;
1.24 mycroft 717: int s = splsoftnet();
1.1 cgd 718:
1.25 cgd 719: for (fp = ipq.lh_first; fp != NULL; fp = nfp) {
720: nfp = fp->ipq_q.le_next;
721: if (--fp->ipq_ttl == 0) {
1.1 cgd 722: ipstat.ips_fragtimeout++;
1.25 cgd 723: ip_freef(fp);
1.1 cgd 724: }
725: }
1.63 matt 726: #ifdef GATEWAY
727: ipflow_slowtimo();
728: #endif
1.1 cgd 729: splx(s);
730: }
731:
732: /*
733: * Drain off all datagram fragments.
734: */
1.8 mycroft 735: void
1.1 cgd 736: ip_drain()
737: {
738:
1.25 cgd 739: while (ipq.lh_first != NULL) {
1.1 cgd 740: ipstat.ips_fragdropped++;
1.25 cgd 741: ip_freef(ipq.lh_first);
1.1 cgd 742: }
743: }
744:
745: /*
746: * Do option processing on a datagram,
747: * possibly discarding it if bad options are encountered,
748: * or forwarding it if source-routed.
749: * Returns 1 if packet has been forwarded/freed,
750: * 0 if the packet should be processed further.
751: */
1.8 mycroft 752: int
1.1 cgd 753: ip_dooptions(m)
754: struct mbuf *m;
755: {
756: register struct ip *ip = mtod(m, struct ip *);
757: register u_char *cp;
758: register struct ip_timestamp *ipt;
759: register struct in_ifaddr *ia;
760: int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1.13 mycroft 761: struct in_addr *sin, dst;
1.1 cgd 762: n_time ntime;
763:
1.13 mycroft 764: dst = ip->ip_dst;
1.1 cgd 765: cp = (u_char *)(ip + 1);
766: cnt = (ip->ip_hl << 2) - sizeof (struct ip);
767: for (; cnt > 0; cnt -= optlen, cp += optlen) {
768: opt = cp[IPOPT_OPTVAL];
769: if (opt == IPOPT_EOL)
770: break;
771: if (opt == IPOPT_NOP)
772: optlen = 1;
773: else {
774: optlen = cp[IPOPT_OLEN];
775: if (optlen <= 0 || optlen > cnt) {
776: code = &cp[IPOPT_OLEN] - (u_char *)ip;
777: goto bad;
778: }
779: }
780: switch (opt) {
781:
782: default:
783: break;
784:
785: /*
786: * Source routing with record.
787: * Find interface with current destination address.
788: * If none on this machine then drop if strictly routed,
789: * or do nothing if loosely routed.
790: * Record interface address and bring up next address
791: * component. If strictly routed make sure next
792: * address is on directly accessible net.
793: */
794: case IPOPT_LSRR:
795: case IPOPT_SSRR:
1.47 cjs 796: if (ip_allowsrcrt == 0) {
797: type = ICMP_UNREACH;
798: code = ICMP_UNREACH_NET_PROHIB;
799: goto bad;
800: }
1.1 cgd 801: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
802: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
803: goto bad;
804: }
805: ipaddr.sin_addr = ip->ip_dst;
1.19 mycroft 806: ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1.1 cgd 807: if (ia == 0) {
808: if (opt == IPOPT_SSRR) {
809: type = ICMP_UNREACH;
810: code = ICMP_UNREACH_SRCFAIL;
811: goto bad;
812: }
813: /*
814: * Loose routing, and not at next destination
815: * yet; nothing to do except forward.
816: */
817: break;
818: }
819: off--; /* 0 origin */
820: if (off > optlen - sizeof(struct in_addr)) {
821: /*
822: * End of source route. Should be for us.
823: */
824: save_rte(cp, ip->ip_src);
825: break;
826: }
827: /*
828: * locate outgoing interface
829: */
830: bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
831: sizeof(ipaddr.sin_addr));
832: if (opt == IPOPT_SSRR) {
833: #define INA struct in_ifaddr *
834: #define SA struct sockaddr *
1.29 mrg 835: ia = (INA)ifa_ifwithladdr((SA)&ipaddr);
1.1 cgd 836: } else
837: ia = ip_rtaddr(ipaddr.sin_addr);
838: if (ia == 0) {
839: type = ICMP_UNREACH;
840: code = ICMP_UNREACH_SRCFAIL;
841: goto bad;
842: }
843: ip->ip_dst = ipaddr.sin_addr;
1.20 mycroft 844: bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1 cgd 845: (caddr_t)(cp + off), sizeof(struct in_addr));
846: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1.13 mycroft 847: /*
848: * Let ip_intr's mcast routing check handle mcast pkts
849: */
1.18 mycroft 850: forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1.1 cgd 851: break;
852:
853: case IPOPT_RR:
854: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
855: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
856: goto bad;
857: }
858: /*
859: * If no space remains, ignore.
860: */
861: off--; /* 0 origin */
862: if (off > optlen - sizeof(struct in_addr))
863: break;
864: bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
865: sizeof(ipaddr.sin_addr));
866: /*
867: * locate outgoing interface; if we're the destination,
868: * use the incoming interface (should be same).
869: */
870: if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
871: (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
872: type = ICMP_UNREACH;
873: code = ICMP_UNREACH_HOST;
874: goto bad;
875: }
1.20 mycroft 876: bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1 cgd 877: (caddr_t)(cp + off), sizeof(struct in_addr));
878: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
879: break;
880:
881: case IPOPT_TS:
882: code = cp - (u_char *)ip;
883: ipt = (struct ip_timestamp *)cp;
884: if (ipt->ipt_len < 5)
885: goto bad;
1.15 cgd 886: if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1.1 cgd 887: if (++ipt->ipt_oflw == 0)
888: goto bad;
889: break;
890: }
891: sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
892: switch (ipt->ipt_flg) {
893:
894: case IPOPT_TS_TSONLY:
895: break;
896:
897: case IPOPT_TS_TSANDADDR:
1.66 thorpej 898: if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.1 cgd 899: sizeof(struct in_addr) > ipt->ipt_len)
900: goto bad;
1.13 mycroft 901: ipaddr.sin_addr = dst;
902: ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
903: m->m_pkthdr.rcvif);
904: if (ia == 0)
905: continue;
1.20 mycroft 906: bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1 cgd 907: (caddr_t)sin, sizeof(struct in_addr));
908: ipt->ipt_ptr += sizeof(struct in_addr);
909: break;
910:
911: case IPOPT_TS_PRESPEC:
1.66 thorpej 912: if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.1 cgd 913: sizeof(struct in_addr) > ipt->ipt_len)
914: goto bad;
915: bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
916: sizeof(struct in_addr));
917: if (ifa_ifwithaddr((SA)&ipaddr) == 0)
918: continue;
919: ipt->ipt_ptr += sizeof(struct in_addr);
920: break;
921:
922: default:
923: goto bad;
924: }
925: ntime = iptime();
926: bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
927: sizeof(n_time));
928: ipt->ipt_ptr += sizeof(n_time);
929: }
930: }
931: if (forward) {
1.26 thorpej 932: if (ip_forwsrcrt == 0) {
933: type = ICMP_UNREACH;
934: code = ICMP_UNREACH_SRCFAIL;
935: goto bad;
936: }
1.1 cgd 937: ip_forward(m, 1);
938: return (1);
1.13 mycroft 939: }
940: return (0);
1.1 cgd 941: bad:
1.13 mycroft 942: ip->ip_len -= ip->ip_hl << 2; /* XXX icmp_error adds in hdr length */
943: icmp_error(m, type, code, 0, 0);
944: ipstat.ips_badoptions++;
1.1 cgd 945: return (1);
946: }
947:
948: /*
949: * Given address of next destination (final or next hop),
950: * return internet address info of interface to be used to get there.
951: */
952: struct in_ifaddr *
953: ip_rtaddr(dst)
954: struct in_addr dst;
955: {
956: register struct sockaddr_in *sin;
957:
1.19 mycroft 958: sin = satosin(&ipforward_rt.ro_dst);
1.1 cgd 959:
1.35 mycroft 960: if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) {
1.1 cgd 961: if (ipforward_rt.ro_rt) {
962: RTFREE(ipforward_rt.ro_rt);
963: ipforward_rt.ro_rt = 0;
964: }
965: sin->sin_family = AF_INET;
966: sin->sin_len = sizeof(*sin);
967: sin->sin_addr = dst;
968:
969: rtalloc(&ipforward_rt);
970: }
971: if (ipforward_rt.ro_rt == 0)
972: return ((struct in_ifaddr *)0);
1.19 mycroft 973: return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
1.1 cgd 974: }
975:
976: /*
977: * Save incoming source route for use in replies,
978: * to be picked up later by ip_srcroute if the receiver is interested.
979: */
1.13 mycroft 980: void
1.1 cgd 981: save_rte(option, dst)
982: u_char *option;
983: struct in_addr dst;
984: {
985: unsigned olen;
986:
987: olen = option[IPOPT_OLEN];
988: #ifdef DIAGNOSTIC
989: if (ipprintfs)
1.39 christos 990: printf("save_rte: olen %d\n", olen);
1.1 cgd 991: #endif
992: if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
993: return;
994: bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
995: ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
996: ip_srcrt.dst = dst;
997: }
998:
999: /*
1000: * Retrieve incoming source route for use in replies,
1001: * in the same form used by setsockopt.
1002: * The first hop is placed before the options, will be removed later.
1003: */
1004: struct mbuf *
1005: ip_srcroute()
1006: {
1007: register struct in_addr *p, *q;
1008: register struct mbuf *m;
1009:
1010: if (ip_nhops == 0)
1011: return ((struct mbuf *)0);
1012: m = m_get(M_DONTWAIT, MT_SOOPTS);
1013: if (m == 0)
1014: return ((struct mbuf *)0);
1015:
1.13 mycroft 1016: #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1.1 cgd 1017:
1018: /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1019: m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1020: OPTSIZ;
1021: #ifdef DIAGNOSTIC
1022: if (ipprintfs)
1.39 christos 1023: printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1.1 cgd 1024: #endif
1025:
1026: /*
1027: * First save first hop for return route
1028: */
1029: p = &ip_srcrt.route[ip_nhops - 1];
1030: *(mtod(m, struct in_addr *)) = *p--;
1031: #ifdef DIAGNOSTIC
1032: if (ipprintfs)
1.39 christos 1033: printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1.1 cgd 1034: #endif
1035:
1036: /*
1037: * Copy option fields and padding (nop) to mbuf.
1038: */
1039: ip_srcrt.nop = IPOPT_NOP;
1040: ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1041: bcopy((caddr_t)&ip_srcrt.nop,
1042: mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
1043: q = (struct in_addr *)(mtod(m, caddr_t) +
1044: sizeof(struct in_addr) + OPTSIZ);
1045: #undef OPTSIZ
1046: /*
1047: * Record return path as an IP source route,
1048: * reversing the path (pointers are now aligned).
1049: */
1050: while (p >= ip_srcrt.route) {
1051: #ifdef DIAGNOSTIC
1052: if (ipprintfs)
1.39 christos 1053: printf(" %x", ntohl(q->s_addr));
1.1 cgd 1054: #endif
1055: *q++ = *p--;
1056: }
1057: /*
1058: * Last hop goes to final destination.
1059: */
1060: *q = ip_srcrt.dst;
1061: #ifdef DIAGNOSTIC
1062: if (ipprintfs)
1.39 christos 1063: printf(" %x\n", ntohl(q->s_addr));
1.1 cgd 1064: #endif
1065: return (m);
1066: }
1067:
1068: /*
1069: * Strip out IP options, at higher
1070: * level protocol in the kernel.
1071: * Second argument is buffer to which options
1072: * will be moved, and return value is their length.
1073: * XXX should be deleted; last arg currently ignored.
1074: */
1.8 mycroft 1075: void
1.1 cgd 1076: ip_stripoptions(m, mopt)
1077: register struct mbuf *m;
1078: struct mbuf *mopt;
1079: {
1080: register int i;
1081: struct ip *ip = mtod(m, struct ip *);
1082: register caddr_t opts;
1083: int olen;
1084:
1085: olen = (ip->ip_hl<<2) - sizeof (struct ip);
1086: opts = (caddr_t)(ip + 1);
1087: i = m->m_len - (sizeof (struct ip) + olen);
1088: bcopy(opts + olen, opts, (unsigned)i);
1089: m->m_len -= olen;
1090: if (m->m_flags & M_PKTHDR)
1091: m->m_pkthdr.len -= olen;
1092: ip->ip_hl = sizeof(struct ip) >> 2;
1093: }
1094:
1.23 mycroft 1095: int inetctlerrmap[PRC_NCMDS] = {
1.1 cgd 1096: 0, 0, 0, 0,
1097: 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1098: EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1099: EMSGSIZE, EHOSTUNREACH, 0, 0,
1100: 0, 0, 0, 0,
1101: ENOPROTOOPT
1102: };
1103:
1104: /*
1105: * Forward a packet. If some error occurs return the sender
1106: * an icmp packet. Note we can't always generate a meaningful
1107: * icmp message because icmp doesn't have a large enough repertoire
1108: * of codes and types.
1109: *
1110: * If not forwarding, just drop the packet. This could be confusing
1111: * if ipforwarding was zero but some routing protocol was advancing
1112: * us as a gateway to somewhere. However, we must let the routing
1113: * protocol deal with that.
1114: *
1115: * The srcrt parameter indicates whether the packet is being forwarded
1116: * via a source route.
1117: */
1.13 mycroft 1118: void
1.1 cgd 1119: ip_forward(m, srcrt)
1120: struct mbuf *m;
1121: int srcrt;
1122: {
1123: register struct ip *ip = mtod(m, struct ip *);
1124: register struct sockaddr_in *sin;
1125: register struct rtentry *rt;
1.28 christos 1126: int error, type = 0, code = 0;
1.1 cgd 1127: struct mbuf *mcopy;
1.13 mycroft 1128: n_long dest;
1129: struct ifnet *destifp;
1.1 cgd 1130:
1.13 mycroft 1131: dest = 0;
1.1 cgd 1132: #ifdef DIAGNOSTIC
1133: if (ipprintfs)
1.70 thorpej 1134: printf("forward: src %2.2x dst %2.2x ttl %x\n",
1135: ntohl(ip->ip_src.s_addr),
1136: ntohl(ip->ip_dst.s_addr), ip->ip_ttl);
1.1 cgd 1137: #endif
1138: if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
1139: ipstat.ips_cantforward++;
1140: m_freem(m);
1141: return;
1142: }
1143: HTONS(ip->ip_id);
1144: if (ip->ip_ttl <= IPTTLDEC) {
1.13 mycroft 1145: icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1.1 cgd 1146: return;
1147: }
1148: ip->ip_ttl -= IPTTLDEC;
1149:
1.19 mycroft 1150: sin = satosin(&ipforward_rt.ro_dst);
1.1 cgd 1151: if ((rt = ipforward_rt.ro_rt) == 0 ||
1.35 mycroft 1152: !in_hosteq(ip->ip_dst, sin->sin_addr)) {
1.1 cgd 1153: if (ipforward_rt.ro_rt) {
1154: RTFREE(ipforward_rt.ro_rt);
1155: ipforward_rt.ro_rt = 0;
1156: }
1157: sin->sin_family = AF_INET;
1.35 mycroft 1158: sin->sin_len = sizeof(struct sockaddr_in);
1.1 cgd 1159: sin->sin_addr = ip->ip_dst;
1160:
1161: rtalloc(&ipforward_rt);
1162: if (ipforward_rt.ro_rt == 0) {
1.13 mycroft 1163: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1.1 cgd 1164: return;
1165: }
1166: rt = ipforward_rt.ro_rt;
1167: }
1168:
1169: /*
1.34 mycroft 1170: * Save at most 68 bytes of the packet in case
1.1 cgd 1171: * we need to generate an ICMP message to the src.
1172: */
1.34 mycroft 1173: mcopy = m_copy(m, 0, imin((int)ip->ip_len, 68));
1.1 cgd 1174:
1175: /*
1176: * If forwarding packet using same interface that it came in on,
1177: * perhaps should send a redirect to sender to shortcut a hop.
1178: * Only send redirect if source is sending directly to us,
1179: * and if packet was not source routed (or has any options).
1180: * Also, don't send redirect if forwarding using a default route
1181: * or a route modified by a redirect.
1182: */
1183: if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1184: (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1.35 mycroft 1185: !in_nullhost(satosin(rt_key(rt))->sin_addr) &&
1.1 cgd 1186: ipsendredirects && !srcrt) {
1.19 mycroft 1187: if (rt->rt_ifa &&
1188: (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1189: ifatoia(rt->rt_ifa)->ia_subnet) {
1.1 cgd 1190: if (rt->rt_flags & RTF_GATEWAY)
1.13 mycroft 1191: dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1.1 cgd 1192: else
1.13 mycroft 1193: dest = ip->ip_dst.s_addr;
1194: /* Router requirements says to only send host redirects */
1.1 cgd 1195: type = ICMP_REDIRECT;
1.13 mycroft 1196: code = ICMP_REDIRECT_HOST;
1.1 cgd 1197: #ifdef DIAGNOSTIC
1198: if (ipprintfs)
1.39 christos 1199: printf("redirect (%d) to %x\n", code, (u_int32_t)dest);
1.1 cgd 1200: #endif
1201: }
1202: }
1203:
1.27 thorpej 1204: error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1205: (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0);
1.1 cgd 1206: if (error)
1207: ipstat.ips_cantforward++;
1208: else {
1209: ipstat.ips_forward++;
1210: if (type)
1211: ipstat.ips_redirectsent++;
1212: else {
1.63 matt 1213: if (mcopy) {
1214: #ifdef GATEWAY
1.64 thorpej 1215: if (mcopy->m_flags & M_CANFASTFWD)
1216: ipflow_create(&ipforward_rt, mcopy);
1.63 matt 1217: #endif
1.1 cgd 1218: m_freem(mcopy);
1.63 matt 1219: }
1.1 cgd 1220: return;
1221: }
1222: }
1223: if (mcopy == NULL)
1224: return;
1.13 mycroft 1225: destifp = NULL;
1226:
1.1 cgd 1227: switch (error) {
1228:
1229: case 0: /* forwarded, but need redirect */
1230: /* type, code set above */
1231: break;
1232:
1233: case ENETUNREACH: /* shouldn't happen, checked above */
1234: case EHOSTUNREACH:
1235: case ENETDOWN:
1236: case EHOSTDOWN:
1237: default:
1238: type = ICMP_UNREACH;
1239: code = ICMP_UNREACH_HOST;
1240: break;
1241:
1242: case EMSGSIZE:
1243: type = ICMP_UNREACH;
1244: code = ICMP_UNREACH_NEEDFRAG;
1.13 mycroft 1245: if (ipforward_rt.ro_rt)
1246: destifp = ipforward_rt.ro_rt->rt_ifp;
1.1 cgd 1247: ipstat.ips_cantfrag++;
1248: break;
1249:
1250: case ENOBUFS:
1251: type = ICMP_SOURCEQUENCH;
1252: code = 0;
1253: break;
1254: }
1.13 mycroft 1255: icmp_error(mcopy, type, code, dest, destifp);
1.44 thorpej 1256: }
1257:
1258: void
1259: ip_savecontrol(inp, mp, ip, m)
1260: register struct inpcb *inp;
1261: register struct mbuf **mp;
1262: register struct ip *ip;
1263: register struct mbuf *m;
1264: {
1265:
1266: if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1267: struct timeval tv;
1268:
1269: microtime(&tv);
1270: *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1271: SCM_TIMESTAMP, SOL_SOCKET);
1272: if (*mp)
1273: mp = &(*mp)->m_next;
1274: }
1275: if (inp->inp_flags & INP_RECVDSTADDR) {
1276: *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1277: sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1278: if (*mp)
1279: mp = &(*mp)->m_next;
1280: }
1281: #ifdef notyet
1282: /*
1283: * XXX
1284: * Moving these out of udp_input() made them even more broken
1285: * than they already were.
1286: * - fenner@parc.xerox.com
1287: */
1288: /* options were tossed already */
1289: if (inp->inp_flags & INP_RECVOPTS) {
1290: *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1291: sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1292: if (*mp)
1293: mp = &(*mp)->m_next;
1294: }
1295: /* ip_srcroute doesn't do what we want here, need to fix */
1296: if (inp->inp_flags & INP_RECVRETOPTS) {
1297: *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1298: sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1299: if (*mp)
1300: mp = &(*mp)->m_next;
1301: }
1302: #endif
1303: if (inp->inp_flags & INP_RECVIF) {
1304: struct sockaddr_dl sdl;
1305:
1306: sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
1307: sdl.sdl_family = AF_LINK;
1308: sdl.sdl_index = m->m_pkthdr.rcvif ?
1309: m->m_pkthdr.rcvif->if_index : 0;
1310: sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
1311: *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
1312: IP_RECVIF, IPPROTO_IP);
1313: if (*mp)
1314: mp = &(*mp)->m_next;
1315: }
1.13 mycroft 1316: }
1317:
1318: int
1319: ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1320: int *name;
1321: u_int namelen;
1322: void *oldp;
1323: size_t *oldlenp;
1324: void *newp;
1325: size_t newlen;
1326: {
1.52 thorpej 1327: extern int subnetsarelocal;
1328:
1.54 lukem 1329: int error, old;
1330:
1.13 mycroft 1331: /* All sysctl names at this level are terminal. */
1332: if (namelen != 1)
1333: return (ENOTDIR);
1334:
1335: switch (name[0]) {
1336: case IPCTL_FORWARDING:
1337: return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding));
1338: case IPCTL_SENDREDIRECTS:
1339: return (sysctl_int(oldp, oldlenp, newp, newlen,
1340: &ipsendredirects));
1341: case IPCTL_DEFTTL:
1342: return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl));
1343: #ifdef notyet
1344: case IPCTL_DEFMTU:
1345: return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
1346: #endif
1.26 thorpej 1347: case IPCTL_FORWSRCRT:
1.47 cjs 1348: /* Don't allow this to change in a secure environment. */
1.26 thorpej 1349: if (securelevel > 0)
1.46 cjs 1350: return (sysctl_rdint(oldp, oldlenp, newp,
1351: ip_forwsrcrt));
1352: else
1353: return (sysctl_int(oldp, oldlenp, newp, newlen,
1354: &ip_forwsrcrt));
1.27 thorpej 1355: case IPCTL_DIRECTEDBCAST:
1356: return (sysctl_int(oldp, oldlenp, newp, newlen,
1357: &ip_directedbcast));
1.47 cjs 1358: case IPCTL_ALLOWSRCRT:
1359: return (sysctl_int(oldp, oldlenp, newp, newlen,
1360: &ip_allowsrcrt));
1.52 thorpej 1361: case IPCTL_SUBNETSARELOCAL:
1362: return (sysctl_int(oldp, oldlenp, newp, newlen,
1363: &subnetsarelocal));
1.53 kml 1364: case IPCTL_MTUDISC:
1.60 kml 1365: error = sysctl_int(oldp, oldlenp, newp, newlen,
1366: &ip_mtudisc);
1367: if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) {
1368: ip_mtudisc_timeout_q =
1369: rt_timer_queue_create(ip_mtudisc_timeout);
1370: } else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) {
1371: rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE);
1372: ip_mtudisc_timeout_q = NULL;
1373: }
1374: return error;
1.54 lukem 1375: case IPCTL_ANONPORTMIN:
1376: old = anonportmin;
1377: error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin);
1378: if (anonportmin >= anonportmax || anonportmin > 65535
1379: #ifndef IPNOPRIVPORTS
1380: || anonportmin < IPPORT_RESERVED
1381: #endif
1382: ) {
1383: anonportmin = old;
1384: return (EINVAL);
1385: }
1386: return (error);
1387: case IPCTL_ANONPORTMAX:
1388: old = anonportmax;
1389: error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax);
1390: if (anonportmin >= anonportmax || anonportmax > 65535
1391: #ifndef IPNOPRIVPORTS
1392: || anonportmax < IPPORT_RESERVED
1393: #endif
1394: ) {
1395: anonportmax = old;
1396: return (EINVAL);
1397: }
1.60 kml 1398: return (error);
1399: case IPCTL_MTUDISCTIMEOUT:
1400: error = sysctl_int(oldp, oldlenp, newp, newlen,
1401: &ip_mtudisc_timeout);
1402: if (ip_mtudisc_timeout_q != NULL)
1403: rt_timer_queue_change(ip_mtudisc_timeout_q,
1404: ip_mtudisc_timeout);
1.54 lukem 1405: return (error);
1.65 matt 1406: #ifdef GATEWAY
1407: case IPCTL_MAXFLOWS:
1.67 thorpej 1408: {
1409: int s;
1410:
1.65 matt 1411: error = sysctl_int(oldp, oldlenp, newp, newlen,
1412: &ip_maxflows);
1.67 thorpej 1413: s = splsoftnet();
1.65 matt 1414: ipflow_reap(0);
1.67 thorpej 1415: splx(s);
1.65 matt 1416: return (error);
1.67 thorpej 1417: }
1.65 matt 1418: #endif
1.13 mycroft 1419: default:
1420: return (EOPNOTSUPP);
1421: }
1422: /* NOTREACHED */
1.1 cgd 1423: }
CVSweb <webmaster@jp.NetBSD.org>