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