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