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