Annotation of src/sys/netinet/ip_input.c, Revision 1.18
1.18 ! mycroft 1: /* $NetBSD: ip_input.c,v 1.17 1995/05/15 02:09:58 cgd 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.1 cgd 36: */
37:
1.5 mycroft 38: #include <sys/param.h>
39: #include <sys/systm.h>
40: #include <sys/malloc.h>
41: #include <sys/mbuf.h>
42: #include <sys/domain.h>
43: #include <sys/protosw.h>
44: #include <sys/socket.h>
45: #include <sys/errno.h>
46: #include <sys/time.h>
47: #include <sys/kernel.h>
1.1 cgd 48:
1.5 mycroft 49: #include <net/if.h>
50: #include <net/route.h>
1.1 cgd 51:
1.5 mycroft 52: #include <netinet/in.h>
53: #include <netinet/in_systm.h>
54: #include <netinet/ip.h>
55: #include <netinet/in_pcb.h>
56: #include <netinet/in_var.h>
57: #include <netinet/ip_var.h>
58: #include <netinet/ip_icmp.h>
1.1 cgd 59:
60: #ifndef IPFORWARDING
61: #ifdef GATEWAY
62: #define IPFORWARDING 1 /* forward IP packets not for us */
63: #else /* GATEWAY */
64: #define IPFORWARDING 0 /* don't forward IP packets not for us */
65: #endif /* GATEWAY */
66: #endif /* IPFORWARDING */
67: #ifndef IPSENDREDIRECTS
68: #define IPSENDREDIRECTS 1
69: #endif
70: int ipforwarding = IPFORWARDING;
71: int ipsendredirects = IPSENDREDIRECTS;
1.13 mycroft 72: int ip_defttl = IPDEFTTL;
1.1 cgd 73: #ifdef DIAGNOSTIC
74: int ipprintfs = 0;
75: #endif
76:
77: extern struct domain inetdomain;
78: extern struct protosw inetsw[];
79: u_char ip_protox[IPPROTO_MAX];
80: int ipqmaxlen = IFQ_MAXLEN;
81: struct in_ifaddr *in_ifaddr; /* first inet address */
1.13 mycroft 82: struct ifqueue ipintrq;
1.1 cgd 83:
84: /*
85: * We need to save the IP options in case a protocol wants to respond
86: * to an incoming packet over the same route if the packet got here
87: * using IP source routing. This allows connection establishment and
88: * maintenance when the remote end is on a network that is not known
89: * to us.
90: */
91: int ip_nhops = 0;
92: static struct ip_srcrt {
93: struct in_addr dst; /* final destination */
94: char nop; /* one NOP to align */
95: char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
96: struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
97: } ip_srcrt;
98:
99: #ifdef GATEWAY
100: extern int if_index;
1.15 cgd 101: u_int32_t *ip_ifmatrix;
1.1 cgd 102: #endif
103:
1.13 mycroft 104: static void save_rte __P((u_char *, struct in_addr));
1.1 cgd 105: /*
106: * IP initialization: fill in IP protocol switch table.
107: * All protocols not implemented in kernel go to raw IP protocol handler.
108: */
1.8 mycroft 109: void
1.1 cgd 110: ip_init()
111: {
112: register struct protosw *pr;
113: register int i;
114:
115: pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
116: if (pr == 0)
117: panic("ip_init");
118: for (i = 0; i < IPPROTO_MAX; i++)
119: ip_protox[i] = pr - inetsw;
120: for (pr = inetdomain.dom_protosw;
121: pr < inetdomain.dom_protoswNPROTOSW; pr++)
122: if (pr->pr_domain->dom_family == PF_INET &&
123: pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
124: ip_protox[pr->pr_protocol] = pr - inetsw;
125: ipq.next = ipq.prev = &ipq;
126: ip_id = time.tv_sec & 0xffff;
127: ipintrq.ifq_maxlen = ipqmaxlen;
128: #ifdef GATEWAY
1.15 cgd 129: i = (if_index + 1) * (if_index + 1) * sizeof (u_int32_t);
130: ip_ifmatrix = (u_int32_t *) malloc(i, M_RTABLE, M_WAITOK);
1.13 mycroft 131: bzero((char *)ip_ifmatrix, i);
1.1 cgd 132: #endif
133: }
134:
135: struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
136: struct route ipforward_rt;
137:
138: /*
139: * Ip input routine. Checksum and byte swap header. If fragmented
140: * try to reassemble. Process options. Pass to next level.
141: */
1.8 mycroft 142: void
1.1 cgd 143: ipintr()
144: {
145: register struct ip *ip;
146: register struct mbuf *m;
147: register struct ipq *fp;
148: register struct in_ifaddr *ia;
149: int hlen, s;
150:
151: next:
152: /*
153: * Get next datagram off input queue and get IP header
154: * in first mbuf.
155: */
156: s = splimp();
157: IF_DEQUEUE(&ipintrq, m);
158: splx(s);
1.13 mycroft 159: if (m == 0)
1.1 cgd 160: return;
161: #ifdef DIAGNOSTIC
162: if ((m->m_flags & M_PKTHDR) == 0)
163: panic("ipintr no HDR");
164: #endif
165: /*
166: * If no IP addresses have been set yet but the interfaces
167: * are receiving, can't do anything with incoming packets yet.
168: */
169: if (in_ifaddr == NULL)
170: goto bad;
171: ipstat.ips_total++;
172: if (m->m_len < sizeof (struct ip) &&
173: (m = m_pullup(m, sizeof (struct ip))) == 0) {
174: ipstat.ips_toosmall++;
175: goto next;
176: }
177: ip = mtod(m, struct ip *);
1.13 mycroft 178: if (ip->ip_v != IPVERSION) {
179: ipstat.ips_badvers++;
180: goto bad;
181: }
1.1 cgd 182: hlen = ip->ip_hl << 2;
183: if (hlen < sizeof(struct ip)) { /* minimum header length */
184: ipstat.ips_badhlen++;
185: goto bad;
186: }
187: if (hlen > m->m_len) {
188: if ((m = m_pullup(m, hlen)) == 0) {
189: ipstat.ips_badhlen++;
190: goto next;
191: }
192: ip = mtod(m, struct ip *);
193: }
194: if (ip->ip_sum = in_cksum(m, hlen)) {
195: ipstat.ips_badsum++;
196: goto bad;
197: }
198:
199: /*
200: * Convert fields to host representation.
201: */
202: NTOHS(ip->ip_len);
203: if (ip->ip_len < hlen) {
204: ipstat.ips_badlen++;
205: goto bad;
206: }
207: NTOHS(ip->ip_id);
208: NTOHS(ip->ip_off);
209:
210: /*
211: * Check that the amount of data in the buffers
212: * is as at least much as the IP header would have us expect.
213: * Trim mbufs if longer than we expect.
214: * Drop packet if shorter than we expect.
215: */
216: if (m->m_pkthdr.len < ip->ip_len) {
217: ipstat.ips_tooshort++;
218: goto bad;
219: }
220: if (m->m_pkthdr.len > ip->ip_len) {
221: if (m->m_len == m->m_pkthdr.len) {
222: m->m_len = ip->ip_len;
223: m->m_pkthdr.len = ip->ip_len;
224: } else
225: m_adj(m, ip->ip_len - m->m_pkthdr.len);
226: }
227:
228: /*
229: * Process options and, if not destined for us,
230: * ship it on. ip_dooptions returns 1 when an
231: * error was detected (causing an icmp message
232: * to be sent and the original packet to be freed).
233: */
234: ip_nhops = 0; /* for source routed packets */
235: if (hlen > sizeof (struct ip) && ip_dooptions(m))
236: goto next;
237:
238: /*
239: * Check our list of addresses, to see if the packet is for us.
240: */
241: for (ia = in_ifaddr; ia; ia = ia->ia_next) {
242: #define satosin(sa) ((struct sockaddr_in *)(sa))
243:
244: if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
245: goto ours;
246: if (
247: #ifdef DIRECTED_BROADCAST
248: ia->ia_ifp == m->m_pkthdr.rcvif &&
249: #endif
250: (ia->ia_ifp->if_flags & IFF_BROADCAST)) {
1.18 ! mycroft 251: if (ip->ip_dst.s_addr ==
! 252: satosin(&ia->ia_broadaddr)->sin_addr.s_addr)
1.1 cgd 253: goto ours;
254: if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
255: goto ours;
256: /*
257: * Look for all-0's host part (old broadcast addr),
258: * either for subnet or net.
259: */
1.18 ! mycroft 260: if (ip->ip_dst.s_addr == ia->ia_subnet ||
! 261: ip->ip_dst.s_addr == ia->ia_net)
1.1 cgd 262: goto ours;
263: }
264: }
1.18 ! mycroft 265: if (IN_MULTICAST(ip->ip_dst.s_addr)) {
1.4 hpeyerl 266: struct in_multi *inm;
267: #ifdef MROUTING
268: extern struct socket *ip_mrouter;
1.10 brezak 269:
270: if (m->m_flags & M_EXT) {
271: if ((m = m_pullup(m, hlen)) == 0) {
272: ipstat.ips_toosmall++;
273: goto next;
274: }
275: ip = mtod(m, struct ip *);
276: }
1.4 hpeyerl 277:
278: if (ip_mrouter) {
279: /*
280: * If we are acting as a multicast router, all
281: * incoming multicast packets are passed to the
282: * kernel-level multicast forwarding function.
283: * The packet is returned (relatively) intact; if
284: * ip_mforward() returns a non-zero value, the packet
285: * must be discarded, else it may be accepted below.
286: *
287: * (The IP ident field is put in the same byte order
288: * as expected when ip_mforward() is called from
289: * ip_output().)
290: */
291: ip->ip_id = htons(ip->ip_id);
1.13 mycroft 292: if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
293: ipstat.ips_cantforward++;
1.4 hpeyerl 294: m_freem(m);
295: goto next;
296: }
297: ip->ip_id = ntohs(ip->ip_id);
298:
299: /*
300: * The process-level routing demon needs to receive
301: * all multicast IGMP packets, whether or not this
302: * host belongs to their destination groups.
303: */
304: if (ip->ip_p == IPPROTO_IGMP)
305: goto ours;
1.13 mycroft 306: ipstat.ips_forward++;
1.4 hpeyerl 307: }
308: #endif
309: /*
310: * See if we belong to the destination multicast group on the
311: * arrival interface.
312: */
313: IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
314: if (inm == NULL) {
1.13 mycroft 315: ipstat.ips_cantforward++;
1.4 hpeyerl 316: m_freem(m);
317: goto next;
318: }
319: goto ours;
320: }
1.18 ! mycroft 321: if (ip->ip_dst.s_addr == INADDR_BROADCAST)
1.1 cgd 322: goto ours;
323: if (ip->ip_dst.s_addr == INADDR_ANY)
324: goto ours;
325:
326: /*
327: * Not for us; forward if possible and desirable.
328: */
329: if (ipforwarding == 0) {
330: ipstat.ips_cantforward++;
331: m_freem(m);
332: } else
333: ip_forward(m, 0);
334: goto next;
335:
336: ours:
337: /*
338: * If offset or IP_MF are set, must reassemble.
339: * Otherwise, nothing need be done.
340: * (We could look in the reassembly queue to see
341: * if the packet was previously fragmented,
342: * but it's not worth the time; just let them time out.)
343: */
344: if (ip->ip_off &~ IP_DF) {
345: if (m->m_flags & M_EXT) { /* XXX */
346: if ((m = m_pullup(m, sizeof (struct ip))) == 0) {
347: ipstat.ips_toosmall++;
348: goto next;
349: }
350: ip = mtod(m, struct ip *);
351: }
352: /*
353: * Look for queue of fragments
354: * of this datagram.
355: */
356: for (fp = ipq.next; fp != &ipq; fp = fp->next)
357: if (ip->ip_id == fp->ipq_id &&
358: ip->ip_src.s_addr == fp->ipq_src.s_addr &&
359: ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
360: ip->ip_p == fp->ipq_p)
361: goto found;
362: fp = 0;
363: found:
364:
365: /*
366: * Adjust ip_len to not reflect header,
367: * set ip_mff if more fragments are expected,
368: * convert offset of this to bytes.
369: */
370: ip->ip_len -= hlen;
1.13 mycroft 371: ((struct ipasfrag *)ip)->ipf_mff &= ~1;
1.16 cgd 372: if (ip->ip_off & IP_MF) {
373: /*
374: * Make sure that fragments have a data length
375: * that's a non-zero multiple of 8 bytes.
376: */
1.17 cgd 377: if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
1.16 cgd 378: ipstat.ips_badfrags++;
379: goto bad;
380: }
1.13 mycroft 381: ((struct ipasfrag *)ip)->ipf_mff |= 1;
1.16 cgd 382: }
1.1 cgd 383: ip->ip_off <<= 3;
384:
385: /*
386: * If datagram marked as having more fragments
387: * or if this is not the first fragment,
388: * attempt reassembly; if it succeeds, proceed.
389: */
1.13 mycroft 390: if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) {
1.1 cgd 391: ipstat.ips_fragments++;
392: ip = ip_reass((struct ipasfrag *)ip, fp);
393: if (ip == 0)
394: goto next;
1.13 mycroft 395: ipstat.ips_reassembled++;
1.1 cgd 396: m = dtom(ip);
397: } else
398: if (fp)
399: ip_freef(fp);
400: } else
401: ip->ip_len -= hlen;
402:
403: /*
404: * Switch out to protocol's input routine.
405: */
406: ipstat.ips_delivered++;
407: (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
408: goto next;
409: bad:
410: m_freem(m);
411: goto next;
412: }
413:
414: /*
415: * Take incoming datagram fragment and try to
416: * reassemble it into whole datagram. If a chain for
417: * reassembly of this datagram already exists, then it
418: * is given as fp; otherwise have to make a chain.
419: */
420: struct ip *
421: ip_reass(ip, fp)
422: register struct ipasfrag *ip;
423: register struct ipq *fp;
424: {
425: register struct mbuf *m = dtom(ip);
426: register struct ipasfrag *q;
427: struct mbuf *t;
428: int hlen = ip->ip_hl << 2;
429: int i, next;
430:
431: /*
432: * Presence of header sizes in mbufs
433: * would confuse code below.
434: */
435: m->m_data += hlen;
436: m->m_len -= hlen;
437:
438: /*
439: * If first fragment to arrive, create a reassembly queue.
440: */
441: if (fp == 0) {
442: if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
443: goto dropfrag;
444: fp = mtod(t, struct ipq *);
445: insque(fp, &ipq);
446: fp->ipq_ttl = IPFRAGTTL;
447: fp->ipq_p = ip->ip_p;
448: fp->ipq_id = ip->ip_id;
449: fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
450: fp->ipq_src = ((struct ip *)ip)->ip_src;
451: fp->ipq_dst = ((struct ip *)ip)->ip_dst;
452: q = (struct ipasfrag *)fp;
453: goto insert;
454: }
455:
456: /*
457: * Find a segment which begins after this one does.
458: */
459: for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
460: if (q->ip_off > ip->ip_off)
461: break;
462:
463: /*
464: * If there is a preceding segment, it may provide some of
465: * our data already. If so, drop the data from the incoming
466: * segment. If it provides all of our data, drop us.
467: */
468: if (q->ipf_prev != (struct ipasfrag *)fp) {
469: i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
470: if (i > 0) {
471: if (i >= ip->ip_len)
472: goto dropfrag;
473: m_adj(dtom(ip), i);
474: ip->ip_off += i;
475: ip->ip_len -= i;
476: }
477: }
478:
479: /*
480: * While we overlap succeeding segments trim them or,
481: * if they are completely covered, dequeue them.
482: */
483: while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
484: i = (ip->ip_off + ip->ip_len) - q->ip_off;
485: if (i < q->ip_len) {
486: q->ip_len -= i;
487: q->ip_off += i;
488: m_adj(dtom(q), i);
489: break;
490: }
491: q = q->ipf_next;
492: m_freem(dtom(q->ipf_prev));
493: ip_deq(q->ipf_prev);
494: }
495:
496: insert:
497: /*
498: * Stick new segment in its place;
499: * check for complete reassembly.
500: */
501: ip_enq(ip, q->ipf_prev);
502: next = 0;
503: for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
504: if (q->ip_off != next)
505: return (0);
506: next += q->ip_len;
507: }
1.13 mycroft 508: if (q->ipf_prev->ipf_mff & 1)
1.1 cgd 509: return (0);
510:
511: /*
512: * Reassembly is complete; concatenate fragments.
513: */
514: q = fp->ipq_next;
515: m = dtom(q);
516: t = m->m_next;
517: m->m_next = 0;
518: m_cat(m, t);
519: q = q->ipf_next;
520: while (q != (struct ipasfrag *)fp) {
521: t = dtom(q);
522: q = q->ipf_next;
523: m_cat(m, t);
524: }
525:
526: /*
527: * Create header for new ip packet by
528: * modifying header of first packet;
529: * dequeue and discard fragment reassembly header.
530: * Make header visible.
531: */
532: ip = fp->ipq_next;
533: ip->ip_len = next;
1.13 mycroft 534: ip->ipf_mff &= ~1;
1.1 cgd 535: ((struct ip *)ip)->ip_src = fp->ipq_src;
536: ((struct ip *)ip)->ip_dst = fp->ipq_dst;
537: remque(fp);
538: (void) m_free(dtom(fp));
539: m = dtom(ip);
540: m->m_len += (ip->ip_hl << 2);
541: m->m_data -= (ip->ip_hl << 2);
542: /* some debugging cruft by sklower, below, will go away soon */
543: if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
544: register int plen = 0;
545: for (t = m; m; m = m->m_next)
546: plen += m->m_len;
547: t->m_pkthdr.len = plen;
548: }
549: return ((struct ip *)ip);
550:
551: dropfrag:
552: ipstat.ips_fragdropped++;
553: m_freem(m);
554: return (0);
555: }
556:
557: /*
558: * Free a fragment reassembly header and all
559: * associated datagrams.
560: */
1.8 mycroft 561: void
1.1 cgd 562: ip_freef(fp)
563: struct ipq *fp;
564: {
565: register struct ipasfrag *q, *p;
566:
567: for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {
568: p = q->ipf_next;
569: ip_deq(q);
570: m_freem(dtom(q));
571: }
572: remque(fp);
573: (void) m_free(dtom(fp));
574: }
575:
576: /*
577: * Put an ip fragment on a reassembly chain.
578: * Like insque, but pointers in middle of structure.
579: */
1.8 mycroft 580: void
1.1 cgd 581: ip_enq(p, prev)
582: register struct ipasfrag *p, *prev;
583: {
584:
585: p->ipf_prev = prev;
586: p->ipf_next = prev->ipf_next;
587: prev->ipf_next->ipf_prev = p;
588: prev->ipf_next = p;
589: }
590:
591: /*
592: * To ip_enq as remque is to insque.
593: */
1.8 mycroft 594: void
1.1 cgd 595: ip_deq(p)
596: register struct ipasfrag *p;
597: {
598:
599: p->ipf_prev->ipf_next = p->ipf_next;
600: p->ipf_next->ipf_prev = p->ipf_prev;
601: }
602:
603: /*
604: * IP timer processing;
605: * if a timer expires on a reassembly
606: * queue, discard it.
607: */
1.8 mycroft 608: void
1.1 cgd 609: ip_slowtimo()
610: {
611: register struct ipq *fp;
612: int s = splnet();
613:
614: fp = ipq.next;
615: if (fp == 0) {
616: splx(s);
617: return;
618: }
619: while (fp != &ipq) {
620: --fp->ipq_ttl;
621: fp = fp->next;
622: if (fp->prev->ipq_ttl == 0) {
623: ipstat.ips_fragtimeout++;
624: ip_freef(fp->prev);
625: }
626: }
627: splx(s);
628: }
629:
630: /*
631: * Drain off all datagram fragments.
632: */
1.8 mycroft 633: void
1.1 cgd 634: ip_drain()
635: {
636:
637: while (ipq.next != &ipq) {
638: ipstat.ips_fragdropped++;
639: ip_freef(ipq.next);
640: }
641: }
642:
643: /*
644: * Do option processing on a datagram,
645: * possibly discarding it if bad options are encountered,
646: * or forwarding it if source-routed.
647: * Returns 1 if packet has been forwarded/freed,
648: * 0 if the packet should be processed further.
649: */
1.8 mycroft 650: int
1.1 cgd 651: ip_dooptions(m)
652: struct mbuf *m;
653: {
654: register struct ip *ip = mtod(m, struct ip *);
655: register u_char *cp;
656: register struct ip_timestamp *ipt;
657: register struct in_ifaddr *ia;
658: int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1.13 mycroft 659: struct in_addr *sin, dst;
1.1 cgd 660: n_time ntime;
661:
1.13 mycroft 662: dst = ip->ip_dst;
1.1 cgd 663: cp = (u_char *)(ip + 1);
664: cnt = (ip->ip_hl << 2) - sizeof (struct ip);
665: for (; cnt > 0; cnt -= optlen, cp += optlen) {
666: opt = cp[IPOPT_OPTVAL];
667: if (opt == IPOPT_EOL)
668: break;
669: if (opt == IPOPT_NOP)
670: optlen = 1;
671: else {
672: optlen = cp[IPOPT_OLEN];
673: if (optlen <= 0 || optlen > cnt) {
674: code = &cp[IPOPT_OLEN] - (u_char *)ip;
675: goto bad;
676: }
677: }
678: switch (opt) {
679:
680: default:
681: break;
682:
683: /*
684: * Source routing with record.
685: * Find interface with current destination address.
686: * If none on this machine then drop if strictly routed,
687: * or do nothing if loosely routed.
688: * Record interface address and bring up next address
689: * component. If strictly routed make sure next
690: * address is on directly accessible net.
691: */
692: case IPOPT_LSRR:
693: case IPOPT_SSRR:
694: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
695: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
696: goto bad;
697: }
698: ipaddr.sin_addr = ip->ip_dst;
699: ia = (struct in_ifaddr *)
700: ifa_ifwithaddr((struct sockaddr *)&ipaddr);
701: if (ia == 0) {
702: if (opt == IPOPT_SSRR) {
703: type = ICMP_UNREACH;
704: code = ICMP_UNREACH_SRCFAIL;
705: goto bad;
706: }
707: /*
708: * Loose routing, and not at next destination
709: * yet; nothing to do except forward.
710: */
711: break;
712: }
713: off--; /* 0 origin */
714: if (off > optlen - sizeof(struct in_addr)) {
715: /*
716: * End of source route. Should be for us.
717: */
718: save_rte(cp, ip->ip_src);
719: break;
720: }
721: /*
722: * locate outgoing interface
723: */
724: bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
725: sizeof(ipaddr.sin_addr));
726: if (opt == IPOPT_SSRR) {
727: #define INA struct in_ifaddr *
728: #define SA struct sockaddr *
729: if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1.13 mycroft 730: ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1.1 cgd 731: } else
732: ia = ip_rtaddr(ipaddr.sin_addr);
733: if (ia == 0) {
734: type = ICMP_UNREACH;
735: code = ICMP_UNREACH_SRCFAIL;
736: goto bad;
737: }
738: ip->ip_dst = ipaddr.sin_addr;
739: bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
740: (caddr_t)(cp + off), sizeof(struct in_addr));
741: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1.13 mycroft 742: /*
743: * Let ip_intr's mcast routing check handle mcast pkts
744: */
1.18 ! mycroft 745: forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1.1 cgd 746: break;
747:
748: case IPOPT_RR:
749: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
750: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
751: goto bad;
752: }
753: /*
754: * If no space remains, ignore.
755: */
756: off--; /* 0 origin */
757: if (off > optlen - sizeof(struct in_addr))
758: break;
759: bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
760: sizeof(ipaddr.sin_addr));
761: /*
762: * locate outgoing interface; if we're the destination,
763: * use the incoming interface (should be same).
764: */
765: if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
766: (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
767: type = ICMP_UNREACH;
768: code = ICMP_UNREACH_HOST;
769: goto bad;
770: }
771: bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
772: (caddr_t)(cp + off), sizeof(struct in_addr));
773: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
774: break;
775:
776: case IPOPT_TS:
777: code = cp - (u_char *)ip;
778: ipt = (struct ip_timestamp *)cp;
779: if (ipt->ipt_len < 5)
780: goto bad;
1.15 cgd 781: if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1.1 cgd 782: if (++ipt->ipt_oflw == 0)
783: goto bad;
784: break;
785: }
786: sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
787: switch (ipt->ipt_flg) {
788:
789: case IPOPT_TS_TSONLY:
790: break;
791:
792: case IPOPT_TS_TSANDADDR:
793: if (ipt->ipt_ptr + sizeof(n_time) +
794: sizeof(struct in_addr) > ipt->ipt_len)
795: goto bad;
1.13 mycroft 796: ipaddr.sin_addr = dst;
797: ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
798: m->m_pkthdr.rcvif);
799: if (ia == 0)
800: continue;
1.1 cgd 801: bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
802: (caddr_t)sin, sizeof(struct in_addr));
803: ipt->ipt_ptr += sizeof(struct in_addr);
804: break;
805:
806: case IPOPT_TS_PRESPEC:
807: if (ipt->ipt_ptr + sizeof(n_time) +
808: sizeof(struct in_addr) > ipt->ipt_len)
809: goto bad;
810: bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
811: sizeof(struct in_addr));
812: if (ifa_ifwithaddr((SA)&ipaddr) == 0)
813: continue;
814: ipt->ipt_ptr += sizeof(struct in_addr);
815: break;
816:
817: default:
818: goto bad;
819: }
820: ntime = iptime();
821: bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
822: sizeof(n_time));
823: ipt->ipt_ptr += sizeof(n_time);
824: }
825: }
826: if (forward) {
827: ip_forward(m, 1);
828: return (1);
1.13 mycroft 829: }
830: return (0);
1.1 cgd 831: bad:
1.13 mycroft 832: ip->ip_len -= ip->ip_hl << 2; /* XXX icmp_error adds in hdr length */
833: icmp_error(m, type, code, 0, 0);
834: ipstat.ips_badoptions++;
1.1 cgd 835: return (1);
836: }
837:
838: /*
839: * Given address of next destination (final or next hop),
840: * return internet address info of interface to be used to get there.
841: */
842: struct in_ifaddr *
843: ip_rtaddr(dst)
844: struct in_addr dst;
845: {
846: register struct sockaddr_in *sin;
847:
848: sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
849:
850: if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
851: if (ipforward_rt.ro_rt) {
852: RTFREE(ipforward_rt.ro_rt);
853: ipforward_rt.ro_rt = 0;
854: }
855: sin->sin_family = AF_INET;
856: sin->sin_len = sizeof(*sin);
857: sin->sin_addr = dst;
858:
859: rtalloc(&ipforward_rt);
860: }
861: if (ipforward_rt.ro_rt == 0)
862: return ((struct in_ifaddr *)0);
863: return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
864: }
865:
866: /*
867: * Save incoming source route for use in replies,
868: * to be picked up later by ip_srcroute if the receiver is interested.
869: */
1.13 mycroft 870: void
1.1 cgd 871: save_rte(option, dst)
872: u_char *option;
873: struct in_addr dst;
874: {
875: unsigned olen;
876:
877: olen = option[IPOPT_OLEN];
878: #ifdef DIAGNOSTIC
879: if (ipprintfs)
880: printf("save_rte: olen %d\n", olen);
881: #endif
882: if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
883: return;
884: bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
885: ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
886: ip_srcrt.dst = dst;
887: }
888:
889: /*
890: * Retrieve incoming source route for use in replies,
891: * in the same form used by setsockopt.
892: * The first hop is placed before the options, will be removed later.
893: */
894: struct mbuf *
895: ip_srcroute()
896: {
897: register struct in_addr *p, *q;
898: register struct mbuf *m;
899:
900: if (ip_nhops == 0)
901: return ((struct mbuf *)0);
902: m = m_get(M_DONTWAIT, MT_SOOPTS);
903: if (m == 0)
904: return ((struct mbuf *)0);
905:
1.13 mycroft 906: #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1.1 cgd 907:
908: /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
909: m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
910: OPTSIZ;
911: #ifdef DIAGNOSTIC
912: if (ipprintfs)
913: printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
914: #endif
915:
916: /*
917: * First save first hop for return route
918: */
919: p = &ip_srcrt.route[ip_nhops - 1];
920: *(mtod(m, struct in_addr *)) = *p--;
921: #ifdef DIAGNOSTIC
922: if (ipprintfs)
923: printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr));
924: #endif
925:
926: /*
927: * Copy option fields and padding (nop) to mbuf.
928: */
929: ip_srcrt.nop = IPOPT_NOP;
930: ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
931: bcopy((caddr_t)&ip_srcrt.nop,
932: mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
933: q = (struct in_addr *)(mtod(m, caddr_t) +
934: sizeof(struct in_addr) + OPTSIZ);
935: #undef OPTSIZ
936: /*
937: * Record return path as an IP source route,
938: * reversing the path (pointers are now aligned).
939: */
940: while (p >= ip_srcrt.route) {
941: #ifdef DIAGNOSTIC
942: if (ipprintfs)
943: printf(" %lx", ntohl(q->s_addr));
944: #endif
945: *q++ = *p--;
946: }
947: /*
948: * Last hop goes to final destination.
949: */
950: *q = ip_srcrt.dst;
951: #ifdef DIAGNOSTIC
952: if (ipprintfs)
953: printf(" %lx\n", ntohl(q->s_addr));
954: #endif
955: return (m);
956: }
957:
958: /*
959: * Strip out IP options, at higher
960: * level protocol in the kernel.
961: * Second argument is buffer to which options
962: * will be moved, and return value is their length.
963: * XXX should be deleted; last arg currently ignored.
964: */
1.8 mycroft 965: void
1.1 cgd 966: ip_stripoptions(m, mopt)
967: register struct mbuf *m;
968: struct mbuf *mopt;
969: {
970: register int i;
971: struct ip *ip = mtod(m, struct ip *);
972: register caddr_t opts;
973: int olen;
974:
975: olen = (ip->ip_hl<<2) - sizeof (struct ip);
976: opts = (caddr_t)(ip + 1);
977: i = m->m_len - (sizeof (struct ip) + olen);
978: bcopy(opts + olen, opts, (unsigned)i);
979: m->m_len -= olen;
980: if (m->m_flags & M_PKTHDR)
981: m->m_pkthdr.len -= olen;
982: ip->ip_hl = sizeof(struct ip) >> 2;
983: }
984:
985: u_char inetctlerrmap[PRC_NCMDS] = {
986: 0, 0, 0, 0,
987: 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
988: EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
989: EMSGSIZE, EHOSTUNREACH, 0, 0,
990: 0, 0, 0, 0,
991: ENOPROTOOPT
992: };
993:
994: /*
995: * Forward a packet. If some error occurs return the sender
996: * an icmp packet. Note we can't always generate a meaningful
997: * icmp message because icmp doesn't have a large enough repertoire
998: * of codes and types.
999: *
1000: * If not forwarding, just drop the packet. This could be confusing
1001: * if ipforwarding was zero but some routing protocol was advancing
1002: * us as a gateway to somewhere. However, we must let the routing
1003: * protocol deal with that.
1004: *
1005: * The srcrt parameter indicates whether the packet is being forwarded
1006: * via a source route.
1007: */
1.13 mycroft 1008: void
1.1 cgd 1009: ip_forward(m, srcrt)
1010: struct mbuf *m;
1011: int srcrt;
1012: {
1013: register struct ip *ip = mtod(m, struct ip *);
1014: register struct sockaddr_in *sin;
1015: register struct rtentry *rt;
1016: int error, type = 0, code;
1017: struct mbuf *mcopy;
1.13 mycroft 1018: n_long dest;
1019: struct ifnet *destifp;
1.1 cgd 1020:
1.13 mycroft 1021: dest = 0;
1.1 cgd 1022: #ifdef DIAGNOSTIC
1023: if (ipprintfs)
1024: printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
1025: ip->ip_dst, ip->ip_ttl);
1026: #endif
1027: if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
1028: ipstat.ips_cantforward++;
1029: m_freem(m);
1030: return;
1031: }
1032: HTONS(ip->ip_id);
1033: if (ip->ip_ttl <= IPTTLDEC) {
1.13 mycroft 1034: icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1.1 cgd 1035: return;
1036: }
1037: ip->ip_ttl -= IPTTLDEC;
1038:
1039: sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
1040: if ((rt = ipforward_rt.ro_rt) == 0 ||
1041: ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
1042: if (ipforward_rt.ro_rt) {
1043: RTFREE(ipforward_rt.ro_rt);
1044: ipforward_rt.ro_rt = 0;
1045: }
1046: sin->sin_family = AF_INET;
1047: sin->sin_len = sizeof(*sin);
1048: sin->sin_addr = ip->ip_dst;
1049:
1050: rtalloc(&ipforward_rt);
1051: if (ipforward_rt.ro_rt == 0) {
1.13 mycroft 1052: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1.1 cgd 1053: return;
1054: }
1055: rt = ipforward_rt.ro_rt;
1056: }
1057:
1058: /*
1059: * Save at most 64 bytes of the packet in case
1060: * we need to generate an ICMP message to the src.
1061: */
1062: mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
1063:
1064: #ifdef GATEWAY
1065: ip_ifmatrix[rt->rt_ifp->if_index +
1066: if_index * m->m_pkthdr.rcvif->if_index]++;
1067: #endif
1068: /*
1069: * If forwarding packet using same interface that it came in on,
1070: * perhaps should send a redirect to sender to shortcut a hop.
1071: * Only send redirect if source is sending directly to us,
1072: * and if packet was not source routed (or has any options).
1073: * Also, don't send redirect if forwarding using a default route
1074: * or a route modified by a redirect.
1075: */
1076: #define satosin(sa) ((struct sockaddr_in *)(sa))
1077: if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1078: (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1079: satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1080: ipsendredirects && !srcrt) {
1.13 mycroft 1081: #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1082: if (RTA(rt) &&
1.18 ! mycroft 1083: (ip->ip_src.s_addr & RTA(rt)->ia_subnetmask) ==
! 1084: RTA(rt)->ia_subnet) {
1.1 cgd 1085: if (rt->rt_flags & RTF_GATEWAY)
1.13 mycroft 1086: dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1.1 cgd 1087: else
1.13 mycroft 1088: dest = ip->ip_dst.s_addr;
1089: /* Router requirements says to only send host redirects */
1.1 cgd 1090: type = ICMP_REDIRECT;
1.13 mycroft 1091: code = ICMP_REDIRECT_HOST;
1.1 cgd 1092: #ifdef DIAGNOSTIC
1093: if (ipprintfs)
1.15 cgd 1094: printf("redirect (%d) to %lx\n", code, (u_int32_t)dest);
1.1 cgd 1095: #endif
1096: }
1097: }
1098:
1.13 mycroft 1099: error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING
1.9 mycroft 1100: #ifdef DIRECTED_BROADCAST
1.13 mycroft 1101: | IP_ALLOWBROADCAST
1.9 mycroft 1102: #endif
1.13 mycroft 1103: , 0);
1.1 cgd 1104: if (error)
1105: ipstat.ips_cantforward++;
1106: else {
1107: ipstat.ips_forward++;
1108: if (type)
1109: ipstat.ips_redirectsent++;
1110: else {
1111: if (mcopy)
1112: m_freem(mcopy);
1113: return;
1114: }
1115: }
1116: if (mcopy == NULL)
1117: return;
1.13 mycroft 1118: destifp = NULL;
1119:
1.1 cgd 1120: switch (error) {
1121:
1122: case 0: /* forwarded, but need redirect */
1123: /* type, code set above */
1124: break;
1125:
1126: case ENETUNREACH: /* shouldn't happen, checked above */
1127: case EHOSTUNREACH:
1128: case ENETDOWN:
1129: case EHOSTDOWN:
1130: default:
1131: type = ICMP_UNREACH;
1132: code = ICMP_UNREACH_HOST;
1133: break;
1134:
1135: case EMSGSIZE:
1136: type = ICMP_UNREACH;
1137: code = ICMP_UNREACH_NEEDFRAG;
1.13 mycroft 1138: if (ipforward_rt.ro_rt)
1139: destifp = ipforward_rt.ro_rt->rt_ifp;
1.1 cgd 1140: ipstat.ips_cantfrag++;
1141: break;
1142:
1143: case ENOBUFS:
1144: type = ICMP_SOURCEQUENCH;
1145: code = 0;
1146: break;
1147: }
1.13 mycroft 1148: icmp_error(mcopy, type, code, dest, destifp);
1149: }
1150:
1151: int
1152: ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1153: int *name;
1154: u_int namelen;
1155: void *oldp;
1156: size_t *oldlenp;
1157: void *newp;
1158: size_t newlen;
1159: {
1160: /* All sysctl names at this level are terminal. */
1161: if (namelen != 1)
1162: return (ENOTDIR);
1163:
1164: switch (name[0]) {
1165: case IPCTL_FORWARDING:
1166: return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding));
1167: case IPCTL_SENDREDIRECTS:
1168: return (sysctl_int(oldp, oldlenp, newp, newlen,
1169: &ipsendredirects));
1170: case IPCTL_DEFTTL:
1171: return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl));
1172: #ifdef notyet
1173: case IPCTL_DEFMTU:
1174: return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
1175: #endif
1176: default:
1177: return (EOPNOTSUPP);
1178: }
1179: /* NOTREACHED */
1.1 cgd 1180: }
CVSweb <webmaster@jp.NetBSD.org>