xref: /freebsd/sys/netinet/ip_input.c (revision 0640d357f29fb1c0daaaffadd0416c5981413afd)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
34  * $Id: ip_input.c,v 1.103 1998/10/27 09:11:41 dfr Exp $
35  *	$ANA: ip_input.c,v 1.5 1996/09/18 14:34:59 wollman Exp $
36  */
37 
38 #define	_IP_VHL
39 
40 #include "opt_bootp.h"
41 #include "opt_ipfw.h"
42 #include "opt_ipdivert.h"
43 #include "opt_ipfilter.h"
44 
45 #include <stddef.h>
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/mbuf.h>
50 #include <sys/domain.h>
51 #include <sys/protosw.h>
52 #include <sys/socket.h>
53 #include <sys/time.h>
54 #include <sys/kernel.h>
55 #include <sys/syslog.h>
56 #include <sys/sysctl.h>
57 
58 #include <net/if.h>
59 #include <net/if_var.h>
60 #include <net/if_dl.h>
61 #include <net/route.h>
62 #include <net/netisr.h>
63 
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/ip_icmp.h>
71 #include <machine/in_cksum.h>
72 
73 #include <sys/socketvar.h>
74 
75 #ifdef IPFIREWALL
76 #include <netinet/ip_fw.h>
77 #endif
78 
79 int rsvp_on = 0;
80 static int ip_rsvp_on;
81 struct socket *ip_rsvpd;
82 
83 int	ipforwarding = 0;
84 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
85 	&ipforwarding, 0, "");
86 
87 static int	ipsendredirects = 1; /* XXX */
88 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
89 	&ipsendredirects, 0, "");
90 
91 int	ip_defttl = IPDEFTTL;
92 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
93 	&ip_defttl, 0, "");
94 
95 static int	ip_dosourceroute = 0;
96 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
97 	&ip_dosourceroute, 0, "");
98 
99 static int	ip_acceptsourceroute = 0;
100 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
101 	CTLFLAG_RW, &ip_acceptsourceroute, 0, "");
102 #ifdef DIAGNOSTIC
103 static int	ipprintfs = 0;
104 #endif
105 
106 extern	struct domain inetdomain;
107 extern	struct protosw inetsw[];
108 u_char	ip_protox[IPPROTO_MAX];
109 static int	ipqmaxlen = IFQ_MAXLEN;
110 struct	in_ifaddrhead in_ifaddrhead; /* first inet address */
111 struct	ifqueue ipintrq;
112 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RD,
113 	&ipintrq.ifq_maxlen, 0, "");
114 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
115 	&ipintrq.ifq_drops, 0, "");
116 
117 struct ipstat ipstat;
118 SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD,
119 	&ipstat, ipstat, "");
120 
121 /* Packet reassembly stuff */
122 #define IPREASS_NHASH_LOG2      6
123 #define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
124 #define IPREASS_HMASK           (IPREASS_NHASH - 1)
125 #define IPREASS_HASH(x,y) \
126 	((((x) & 0xF | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
127 
128 static struct ipq ipq[IPREASS_NHASH];
129 static int    nipq = 0;         /* total # of reass queues */
130 static int    maxnipq;
131 
132 #ifdef IPCTL_DEFMTU
133 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
134 	&ip_mtu, 0, "");
135 #endif
136 
137 #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
138 #undef COMPAT_IPFW
139 #define COMPAT_IPFW 1
140 #else
141 #undef COMPAT_IPFW
142 #endif
143 
144 #ifdef COMPAT_IPFW
145 
146 #include <netinet/ip_fw.h>
147 
148 /* Firewall hooks */
149 ip_fw_chk_t *ip_fw_chk_ptr;
150 ip_fw_ctl_t *ip_fw_ctl_ptr;
151 
152 /* IP Network Address Translation (NAT) hooks */
153 ip_nat_t *ip_nat_ptr;
154 ip_nat_ctl_t *ip_nat_ctl_ptr;
155 #endif
156 
157 #if defined(IPFILTER_LKM) || defined(IPFILTER)
158 int iplattach __P((void));
159 int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)) = NULL;
160 #endif
161 
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 static 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 
178 #ifdef IPDIVERT
179 /*
180  * Shared variable between ip_input() and ip_reass() to communicate
181  * about which packets, once assembled from fragments, get diverted,
182  * and to which port.
183  */
184 static u_short	frag_divert_port;
185 #endif
186 
187 struct sockaddr_in *ip_fw_fwd_addr;
188 
189 static void save_rte __P((u_char *, struct in_addr));
190 static int	 ip_dooptions __P((struct mbuf *));
191 static void	 ip_forward __P((struct mbuf *, int));
192 static void	 ip_freef __P((struct ipq *));
193 static struct ip *
194 	 ip_reass __P((struct mbuf *, struct ipq *, struct ipq *));
195 static struct in_ifaddr *
196 	 ip_rtaddr __P((struct in_addr));
197 static void	ipintr __P((void));
198 /*
199  * IP initialization: fill in IP protocol switch table.
200  * All protocols not implemented in kernel go to raw IP protocol handler.
201  */
202 void
203 ip_init()
204 {
205 	register struct protosw *pr;
206 	register int i;
207 
208 	TAILQ_INIT(&in_ifaddrhead);
209 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
210 	if (pr == 0)
211 		panic("ip_init");
212 	for (i = 0; i < IPPROTO_MAX; i++)
213 		ip_protox[i] = pr - inetsw;
214 	for (pr = inetdomain.dom_protosw;
215 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
216 		if (pr->pr_domain->dom_family == PF_INET &&
217 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
218 			ip_protox[pr->pr_protocol] = pr - inetsw;
219 
220 	for (i = 0; i < IPREASS_NHASH; i++)
221 	    ipq[i].next = ipq[i].prev = &ipq[i];
222 
223 	maxnipq = nmbclusters/4;
224 
225 	ip_id = time_second & 0xffff;
226 	ipintrq.ifq_maxlen = ipqmaxlen;
227 #ifdef IPNAT
228         ip_nat_init();
229 #endif
230 #ifdef IPFILTER
231         iplattach();
232 #endif
233 
234 }
235 
236 static struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
237 static struct	route ipforward_rt;
238 
239 /*
240  * Ip input routine.  Checksum and byte swap header.  If fragmented
241  * try to reassemble.  Process options.  Pass to next level.
242  */
243 void
244 ip_input(struct mbuf *m)
245 {
246 	struct ip *ip;
247 	struct ipq *fp;
248 	struct ipqent *ipqe;
249 	struct in_ifaddr *ia;
250 	int    i, hlen, mff;
251 	u_short sum;
252 
253 #ifdef	DIAGNOSTIC
254 	if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
255 		panic("ip_input no HDR");
256 #endif
257 	/*
258 	 * If no IP addresses have been set yet but the interfaces
259 	 * are receiving, can't do anything with incoming packets yet.
260 	 * XXX This is broken! We should be able to receive broadcasts
261 	 * and multicasts even without any local addresses configured.
262 	 */
263 	if (TAILQ_EMPTY(&in_ifaddrhead))
264 		goto bad;
265 	ipstat.ips_total++;
266 
267 	if (m->m_pkthdr.len < sizeof(struct ip))
268 		goto tooshort;
269 
270 	if (m->m_len < sizeof (struct ip) &&
271 	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
272 		ipstat.ips_toosmall++;
273 		return;
274 	}
275 	ip = mtod(m, struct ip *);
276 
277 	if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
278 		ipstat.ips_badvers++;
279 		goto bad;
280 	}
281 
282 	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
283 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
284 		ipstat.ips_badhlen++;
285 		goto bad;
286 	}
287 	if (hlen > m->m_len) {
288 		if ((m = m_pullup(m, hlen)) == 0) {
289 			ipstat.ips_badhlen++;
290 			return;
291 		}
292 		ip = mtod(m, struct ip *);
293 	}
294 	if (hlen == sizeof(struct ip)) {
295 		sum = in_cksum_hdr(ip);
296 	} else {
297 		sum = in_cksum(m, hlen);
298 	}
299 	if (sum) {
300 		ipstat.ips_badsum++;
301 		goto bad;
302 	}
303 
304 	/*
305 	 * Convert fields to host representation.
306 	 */
307 	NTOHS(ip->ip_len);
308 	if (ip->ip_len < hlen) {
309 		ipstat.ips_badlen++;
310 		goto bad;
311 	}
312 	NTOHS(ip->ip_id);
313 	NTOHS(ip->ip_off);
314 
315 	/*
316 	 * Check that the amount of data in the buffers
317 	 * is as at least much as the IP header would have us expect.
318 	 * Trim mbufs if longer than we expect.
319 	 * Drop packet if shorter than we expect.
320 	 */
321 	if (m->m_pkthdr.len < ip->ip_len) {
322 tooshort:
323 		ipstat.ips_tooshort++;
324 		goto bad;
325 	}
326 	if (m->m_pkthdr.len > ip->ip_len) {
327 		if (m->m_len == m->m_pkthdr.len) {
328 			m->m_len = ip->ip_len;
329 			m->m_pkthdr.len = ip->ip_len;
330 		} else
331 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
332 	}
333 	/*
334 	 * IpHack's section.
335 	 * Right now when no processing on packet has done
336 	 * and it is still fresh out of network we do our black
337 	 * deals with it.
338 	 * - Firewall: deny/allow/divert
339 	 * - Xlate: translate packet's addr/port (NAT).
340 	 * - Wrap: fake packet's addr/port <unimpl.>
341 	 * - Encapsulate: put it in another IP and send out. <unimp.>
342  	 */
343 #if defined(IPFILTER) || defined(IPFILTER_LKM)
344 	/*
345 	 * Check if we want to allow this packet to be processed.
346 	 * Consider it to be bad if not.
347 	 */
348 	if (fr_checkp) {
349 		struct	mbuf	*m1 = m;
350 
351 		if ((*fr_checkp)(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1)
352 			return;
353 		ip = mtod(m = m1, struct ip *);
354 	}
355 #endif
356 #ifdef COMPAT_IPFW
357 	if (ip_fw_chk_ptr) {
358 		u_int16_t	port;
359 
360 #ifdef IPFIREWALL_FORWARD
361 		/*
362 		 * If we've been forwarded from the output side, then
363 		 * skip the firewall a second time
364 		 */
365 		if (ip_fw_fwd_addr)
366 			goto ours;
367 #endif	/* IPFIREWALL_FORWARD */
368 #ifdef IPDIVERT
369 		port = (*ip_fw_chk_ptr)(&ip, hlen, NULL, &ip_divert_cookie,
370 					&m, &ip_fw_fwd_addr);
371 		if (port) {
372 			/* Divert packet */
373 			frag_divert_port = port;
374 			goto ours;
375 		}
376 #else	/* !DIVERT */
377 		/*
378 		 * If ipfw says divert, we have to just drop packet
379 		 *  Use port as a dummy argument.
380 		 */
381 		port = 0;
382 		if ((*ip_fw_chk_ptr)(&ip, hlen, NULL, &port,
383 					&m, &ip_fw_fwd_addr)) {
384 			m_freem(m);
385 			m = NULL;
386 		}
387 #endif	/* !DIVERT */
388 		if (!m)
389 			return;
390 	}
391 
392         if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, m->m_pkthdr.rcvif, IP_NAT_IN))
393 		return;
394 #endif	/* !COMPAT_IPFW */
395 
396 	/*
397 	 * Process options and, if not destined for us,
398 	 * ship it on.  ip_dooptions returns 1 when an
399 	 * error was detected (causing an icmp message
400 	 * to be sent and the original packet to be freed).
401 	 */
402 	ip_nhops = 0;		/* for source routed packets */
403 	if (hlen > sizeof (struct ip) && ip_dooptions(m))
404 		return;
405 
406         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
407          * matter if it is destined to another node, or whether it is
408          * a multicast one, RSVP wants it! and prevents it from being forwarded
409          * anywhere else. Also checks if the rsvp daemon is running before
410 	 * grabbing the packet.
411          */
412 	if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
413 		goto ours;
414 
415 	/*
416 	 * Check our list of addresses, to see if the packet is for us.
417 	 */
418 	for (ia = TAILQ_FIRST(&in_ifaddrhead); ia;
419 					ia = TAILQ_NEXT(ia, ia_link)) {
420 #define	satosin(sa)	((struct sockaddr_in *)(sa))
421 
422 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
423 			goto ours;
424 #ifdef BOOTP_COMPAT
425 		if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
426 			goto ours;
427 #endif
428 #ifdef IPFIREWALL_FORWARD
429 		/*
430 		 * If the addr to forward to is one of ours, we pretend to
431 		 * be the destination for this packet.
432 		 */
433 		if (ip_fw_fwd_addr != NULL &&
434 			IA_SIN(ia)->sin_addr.s_addr ==
435 					 ip_fw_fwd_addr->sin_addr.s_addr)
436 			goto ours;
437 #endif
438 		if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
439 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
440 			    ip->ip_dst.s_addr)
441 				goto ours;
442 			if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
443 				goto ours;
444 		}
445 	}
446 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
447 		struct in_multi *inm;
448 		if (ip_mrouter) {
449 			/*
450 			 * If we are acting as a multicast router, all
451 			 * incoming multicast packets are passed to the
452 			 * kernel-level multicast forwarding function.
453 			 * The packet is returned (relatively) intact; if
454 			 * ip_mforward() returns a non-zero value, the packet
455 			 * must be discarded, else it may be accepted below.
456 			 *
457 			 * (The IP ident field is put in the same byte order
458 			 * as expected when ip_mforward() is called from
459 			 * ip_output().)
460 			 */
461 			ip->ip_id = htons(ip->ip_id);
462 			if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
463 				ipstat.ips_cantforward++;
464 				m_freem(m);
465 				return;
466 			}
467 			ip->ip_id = ntohs(ip->ip_id);
468 
469 			/*
470 			 * The process-level routing demon needs to receive
471 			 * all multicast IGMP packets, whether or not this
472 			 * host belongs to their destination groups.
473 			 */
474 			if (ip->ip_p == IPPROTO_IGMP)
475 				goto ours;
476 			ipstat.ips_forward++;
477 		}
478 		/*
479 		 * See if we belong to the destination multicast group on the
480 		 * arrival interface.
481 		 */
482 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
483 		if (inm == NULL) {
484 			ipstat.ips_notmember++;
485 			m_freem(m);
486 			return;
487 		}
488 		goto ours;
489 	}
490 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
491 		goto ours;
492 	if (ip->ip_dst.s_addr == INADDR_ANY)
493 		goto ours;
494 
495 	/*
496 	 * Not for us; forward if possible and desirable.
497 	 */
498 	if (ipforwarding == 0) {
499 		ipstat.ips_cantforward++;
500 		m_freem(m);
501 	} else
502 		ip_forward(m, 0);
503 	return;
504 
505 ours:
506 
507 	/*
508 	 * If offset or IP_MF are set, must reassemble.
509 	 * Otherwise, nothing need be done.
510 	 * (We could look in the reassembly queue to see
511 	 * if the packet was previously fragmented,
512 	 * but it's not worth the time; just let them time out.)
513 	 */
514 	if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) {
515 		if (m->m_flags & M_EXT) {		/* XXX */
516 			if ((m = m_pullup(m, sizeof (struct ip))) == 0) {
517 				ipstat.ips_toosmall++;
518 #ifdef IPDIVERT
519 				frag_divert_port = 0;
520 				ip_divert_cookie = 0;
521 #endif
522 				return;
523 			}
524 			ip = mtod(m, struct ip *);
525 		}
526 		sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
527 		/*
528 		 * Look for queue of fragments
529 		 * of this datagram.
530 		 */
531 		for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
532 			if (ip->ip_id == fp->ipq_id &&
533 			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
534 			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
535 			    ip->ip_p == fp->ipq_p)
536 				goto found;
537 
538 		fp = 0;
539 
540 		/* check if there's a place for the new queue */
541 		if (nipq > maxnipq) {
542 		    /*
543 		     * drop something from the tail of the current queue
544 		     * before proceeding further
545 		     */
546 		    if (ipq[sum].prev == &ipq[sum]) {   /* gak */
547 			for (i = 0; i < IPREASS_NHASH; i++) {
548 			    if (ipq[i].prev != &ipq[i]) {
549 				ip_freef(ipq[i].prev);
550 				break;
551 			    }
552 			}
553 		    } else
554 			ip_freef(ipq[sum].prev);
555 		}
556 found:
557 		/*
558 		 * Adjust ip_len to not reflect header,
559 		 * set ip_mff if more fragments are expected,
560 		 * convert offset of this to bytes.
561 		 */
562 		ip->ip_len -= hlen;
563 		mff = (ip->ip_off & IP_MF) != 0;
564 		if (mff) {
565 		        /*
566 		         * Make sure that fragments have a data length
567 			 * that's a non-zero multiple of 8 bytes.
568 		         */
569 			if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
570 				ipstat.ips_toosmall++; /* XXX */
571 				goto bad;
572 			}
573 			m->m_flags |= M_FRAG;
574 		}
575 		ip->ip_off <<= 3;
576 
577 		/*
578 		 * If datagram marked as having more fragments
579 		 * or if this is not the first fragment,
580 		 * attempt reassembly; if it succeeds, proceed.
581 		 */
582 		if (mff || ip->ip_off) {
583 			ipstat.ips_fragments++;
584 			m->m_pkthdr.header = ip;
585 			ip = ip_reass(m, fp, &ipq[sum]);
586 			if (ip == 0) {
587 #ifdef	IPFIREWALL_FORWARD
588 				ip_fw_fwd_addr = NULL;
589 #endif
590 				return;
591 			}
592 			/* Get the length of the reassembled packets header */
593 			hlen = IP_VHL_HL(ip->ip_vhl) << 2;
594 			ipstat.ips_reassembled++;
595 			m = dtom(ip);
596 #ifdef IPDIVERT
597 			if (frag_divert_port) {
598 				ip->ip_len += hlen;
599 				HTONS(ip->ip_len);
600 				HTONS(ip->ip_off);
601 				HTONS(ip->ip_id);
602 				ip->ip_sum = 0;
603 				ip->ip_sum = in_cksum_hdr(ip);
604 				NTOHS(ip->ip_id);
605 				NTOHS(ip->ip_off);
606 				NTOHS(ip->ip_len);
607 				ip->ip_len -= hlen;
608 			}
609 #endif
610 		} else
611 			if (fp)
612 				ip_freef(fp);
613 	} else
614 		ip->ip_len -= hlen;
615 
616 #ifdef IPDIVERT
617 	/*
618 	 * Divert reassembled packets to the divert protocol if required
619 	 *  If divert port is null then cookie should be too,
620 	 * so we shouldn't need to clear them here. Assume ip_divert does so.
621 	 */
622 	if (frag_divert_port) {
623 		ipstat.ips_delivered++;
624 		ip_divert_port = frag_divert_port;
625 		frag_divert_port = 0;
626 		(*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, hlen);
627 		return;
628 	}
629 
630 	/* Don't let packets divert themselves */
631 	if (ip->ip_p == IPPROTO_DIVERT) {
632 		ipstat.ips_noproto++;
633 		goto bad;
634 	}
635 
636 #endif
637 
638 	/*
639 	 * Switch out to protocol's input routine.
640 	 */
641 	ipstat.ips_delivered++;
642 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
643 #ifdef	IPFIREWALL_FORWARD
644 	ip_fw_fwd_addr = NULL;	/* tcp needed it */
645 #endif
646 	return;
647 bad:
648 #ifdef	IPFIREWALL_FORWARD
649 	ip_fw_fwd_addr = NULL;
650 #endif
651 	m_freem(m);
652 }
653 
654 /*
655  * IP software interrupt routine - to go away sometime soon
656  */
657 static void
658 ipintr(void)
659 {
660 	int s;
661 	struct mbuf *m;
662 
663 	while(1) {
664 		s = splimp();
665 		IF_DEQUEUE(&ipintrq, m);
666 		splx(s);
667 		if (m == 0)
668 			return;
669 		ip_input(m);
670 	}
671 }
672 
673 NETISR_SET(NETISR_IP, ipintr);
674 
675 /*
676  * Take incoming datagram fragment and try to
677  * reassemble it into whole datagram.  If a chain for
678  * reassembly of this datagram already exists, then it
679  * is given as fp; otherwise have to make a chain.
680  */
681 static struct ip *
682 ip_reass(m, fp, where)
683 	register struct mbuf *m;
684 	register struct ipq *fp;
685 	struct   ipq    *where;
686 {
687 	struct ip *ip = mtod(m, struct ip *);
688 	register struct mbuf *p = 0, *q, *nq;
689 	struct mbuf *t;
690 	int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
691 	int i, next;
692 
693 	/*
694 	 * Presence of header sizes in mbufs
695 	 * would confuse code below.
696 	 */
697 	m->m_data += hlen;
698 	m->m_len -= hlen;
699 
700 	/*
701 	 * If first fragment to arrive, create a reassembly queue.
702 	 */
703 	if (fp == 0) {
704 		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
705 			goto dropfrag;
706 		fp = mtod(t, struct ipq *);
707 		insque(fp, where);
708 		nipq++;
709 		fp->ipq_ttl = IPFRAGTTL;
710 		fp->ipq_p = ip->ip_p;
711 		fp->ipq_id = ip->ip_id;
712 		fp->ipq_src = ip->ip_src;
713 		fp->ipq_dst = ip->ip_dst;
714 		fp->ipq_frags = m;
715 		m->m_nextpkt = NULL;
716 #ifdef IPDIVERT
717 		fp->ipq_divert = 0;
718 		fp->ipq_div_cookie = 0;
719 #endif
720 		goto inserted;
721 	}
722 
723 #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
724 
725 	/*
726 	 * Find a segment which begins after this one does.
727 	 */
728 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
729 		if (GETIP(q)->ip_off > ip->ip_off)
730 			break;
731 
732 	/*
733 	 * If there is a preceding segment, it may provide some of
734 	 * our data already.  If so, drop the data from the incoming
735 	 * segment.  If it provides all of our data, drop us, otherwise
736 	 * stick new segment in the proper place.
737 	 */
738 	if (p) {
739 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
740 		if (i > 0) {
741 			if (i >= ip->ip_len)
742 				goto dropfrag;
743 			m_adj(dtom(ip), i);
744 			ip->ip_off += i;
745 			ip->ip_len -= i;
746 		}
747 		m->m_nextpkt = p->m_nextpkt;
748 		p->m_nextpkt = m;
749 	} else {
750 		m->m_nextpkt = fp->ipq_frags;
751 		fp->ipq_frags = m;
752 	}
753 
754 	/*
755 	 * While we overlap succeeding segments trim them or,
756 	 * if they are completely covered, dequeue them.
757 	 */
758 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
759 	     q = nq) {
760 		i = (ip->ip_off + ip->ip_len) -
761 		    GETIP(q)->ip_off;
762 		if (i < GETIP(q)->ip_len) {
763 			GETIP(q)->ip_len -= i;
764 			GETIP(q)->ip_off += i;
765 			m_adj(q, i);
766 			break;
767 		}
768 		nq = q->m_nextpkt;
769 		m->m_nextpkt = nq;
770 		m_freem(q);
771 	}
772 
773 inserted:
774 
775 #ifdef IPDIVERT
776 	/*
777 	 * Any fragment diverting causes the whole packet to divert
778 	 */
779 	if (frag_divert_port) {
780 		fp->ipq_divert = frag_divert_port;
781 		fp->ipq_div_cookie = ip_divert_cookie;
782 	}
783 	frag_divert_port = 0;
784 	ip_divert_cookie = 0;
785 #endif
786 
787 	/*
788 	 * Check for complete reassembly.
789 	 */
790 	next = 0;
791 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
792 		if (GETIP(q)->ip_off != next)
793 			return (0);
794 		next += GETIP(q)->ip_len;
795 	}
796 	/* Make sure the last packet didn't have the IP_MF flag */
797 	if (p->m_flags & M_FRAG)
798 		return (0);
799 
800 	/*
801 	 * Reassembly is complete.  Make sure the packet is a sane size.
802 	 */
803 	q = fp->ipq_frags;
804 	ip = GETIP(q);
805 	if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
806 		ipstat.ips_toolong++;
807 		ip_freef(fp);
808 		return (0);
809 	}
810 
811 	/*
812 	 * Concatenate fragments.
813 	 */
814 	m = q;
815 	t = m->m_next;
816 	m->m_next = 0;
817 	m_cat(m, t);
818 	nq = q->m_nextpkt;
819 	q->m_nextpkt = 0;
820 	for (q = nq; q != NULL; q = nq) {
821 		nq = q->m_nextpkt;
822 		q->m_nextpkt = NULL;
823 		m_cat(m, q);
824 	}
825 
826 #ifdef IPDIVERT
827 	/*
828 	 * extract divert port for packet, if any
829 	 */
830 	frag_divert_port = fp->ipq_divert;
831 	ip_divert_cookie = fp->ipq_div_cookie;
832 #endif
833 
834 	/*
835 	 * Create header for new ip packet by
836 	 * modifying header of first packet;
837 	 * dequeue and discard fragment reassembly header.
838 	 * Make header visible.
839 	 */
840 	ip->ip_len = next;
841 	ip->ip_src = fp->ipq_src;
842 	ip->ip_dst = fp->ipq_dst;
843 	remque(fp);
844 	nipq--;
845 	(void) m_free(dtom(fp));
846 	m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
847 	m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
848 	/* some debugging cruft by sklower, below, will go away soon */
849 	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
850 		register int plen = 0;
851 		for (t = m; m; m = m->m_next)
852 			plen += m->m_len;
853 		t->m_pkthdr.len = plen;
854 	}
855 	return (ip);
856 
857 dropfrag:
858 #ifdef IPDIVERT
859 	frag_divert_port = 0;
860 	ip_divert_cookie = 0;
861 #endif
862 	ipstat.ips_fragdropped++;
863 	m_freem(m);
864 	return (0);
865 
866 #undef GETIP
867 }
868 
869 /*
870  * Free a fragment reassembly header and all
871  * associated datagrams.
872  */
873 static void
874 ip_freef(fp)
875 	struct ipq *fp;
876 {
877 	register struct mbuf *q;
878 
879 	while (fp->ipq_frags) {
880 		q = fp->ipq_frags;
881 		fp->ipq_frags = q->m_nextpkt;
882 		m_freem(q);
883 	}
884 	remque(fp);
885 	(void) m_free(dtom(fp));
886 	nipq--;
887 }
888 
889 /*
890  * IP timer processing;
891  * if a timer expires on a reassembly
892  * queue, discard it.
893  */
894 void
895 ip_slowtimo()
896 {
897 	register struct ipq *fp;
898 	int s = splnet();
899 	int i;
900 
901 	for (i = 0; i < IPREASS_NHASH; i++) {
902 		fp = ipq[i].next;
903 		if (fp == 0)
904 			continue;
905 		while (fp != &ipq[i]) {
906 			--fp->ipq_ttl;
907 			fp = fp->next;
908 			if (fp->prev->ipq_ttl == 0) {
909 				ipstat.ips_fragtimeout++;
910 				ip_freef(fp->prev);
911 			}
912 		}
913 	}
914 	ipflow_slowtimo();
915 	splx(s);
916 }
917 
918 /*
919  * Drain off all datagram fragments.
920  */
921 void
922 ip_drain()
923 {
924 	int     i;
925 
926 	for (i = 0; i < IPREASS_NHASH; i++) {
927 		while (ipq[i].next != &ipq[i]) {
928 			ipstat.ips_fragdropped++;
929 			ip_freef(ipq[i].next);
930 		}
931 	}
932 	in_rtqdrain();
933 }
934 
935 /*
936  * Do option processing on a datagram,
937  * possibly discarding it if bad options are encountered,
938  * or forwarding it if source-routed.
939  * Returns 1 if packet has been forwarded/freed,
940  * 0 if the packet should be processed further.
941  */
942 static int
943 ip_dooptions(m)
944 	struct mbuf *m;
945 {
946 	register struct ip *ip = mtod(m, struct ip *);
947 	register u_char *cp;
948 	register struct ip_timestamp *ipt;
949 	register struct in_ifaddr *ia;
950 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
951 	struct in_addr *sin, dst;
952 	n_time ntime;
953 
954 	dst = ip->ip_dst;
955 	cp = (u_char *)(ip + 1);
956 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
957 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
958 		opt = cp[IPOPT_OPTVAL];
959 		if (opt == IPOPT_EOL)
960 			break;
961 		if (opt == IPOPT_NOP)
962 			optlen = 1;
963 		else {
964 			optlen = cp[IPOPT_OLEN];
965 			if (optlen <= 0 || optlen > cnt) {
966 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
967 				goto bad;
968 			}
969 		}
970 		switch (opt) {
971 
972 		default:
973 			break;
974 
975 		/*
976 		 * Source routing with record.
977 		 * Find interface with current destination address.
978 		 * If none on this machine then drop if strictly routed,
979 		 * or do nothing if loosely routed.
980 		 * Record interface address and bring up next address
981 		 * component.  If strictly routed make sure next
982 		 * address is on directly accessible net.
983 		 */
984 		case IPOPT_LSRR:
985 		case IPOPT_SSRR:
986 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
987 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
988 				goto bad;
989 			}
990 			ipaddr.sin_addr = ip->ip_dst;
991 			ia = (struct in_ifaddr *)
992 				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
993 			if (ia == 0) {
994 				if (opt == IPOPT_SSRR) {
995 					type = ICMP_UNREACH;
996 					code = ICMP_UNREACH_SRCFAIL;
997 					goto bad;
998 				}
999 				if (!ip_dosourceroute)
1000 					goto nosourcerouting;
1001 				/*
1002 				 * Loose routing, and not at next destination
1003 				 * yet; nothing to do except forward.
1004 				 */
1005 				break;
1006 			}
1007 			off--;			/* 0 origin */
1008 			if (off > optlen - sizeof(struct in_addr)) {
1009 				/*
1010 				 * End of source route.  Should be for us.
1011 				 */
1012 				if (!ip_acceptsourceroute)
1013 					goto nosourcerouting;
1014 				save_rte(cp, ip->ip_src);
1015 				break;
1016 			}
1017 
1018 			if (!ip_dosourceroute) {
1019 				if (ipforwarding) {
1020 					char buf[16]; /* aaa.bbb.ccc.ddd\0 */
1021 					/*
1022 					 * Acting as a router, so generate ICMP
1023 					 */
1024 nosourcerouting:
1025 					strcpy(buf, inet_ntoa(ip->ip_dst));
1026 					log(LOG_WARNING,
1027 					    "attempted source route from %s to %s\n",
1028 					    inet_ntoa(ip->ip_src), buf);
1029 					type = ICMP_UNREACH;
1030 					code = ICMP_UNREACH_SRCFAIL;
1031 					goto bad;
1032 				} else {
1033 					/*
1034 					 * Not acting as a router, so silently drop.
1035 					 */
1036 					ipstat.ips_cantforward++;
1037 					m_freem(m);
1038 					return (1);
1039 				}
1040 			}
1041 
1042 			/*
1043 			 * locate outgoing interface
1044 			 */
1045 			(void)memcpy(&ipaddr.sin_addr, cp + off,
1046 			    sizeof(ipaddr.sin_addr));
1047 
1048 			if (opt == IPOPT_SSRR) {
1049 #define	INA	struct in_ifaddr *
1050 #define	SA	struct sockaddr *
1051 			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1052 				ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1053 			} else
1054 				ia = ip_rtaddr(ipaddr.sin_addr);
1055 			if (ia == 0) {
1056 				type = ICMP_UNREACH;
1057 				code = ICMP_UNREACH_SRCFAIL;
1058 				goto bad;
1059 			}
1060 			ip->ip_dst = ipaddr.sin_addr;
1061 			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1062 			    sizeof(struct in_addr));
1063 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1064 			/*
1065 			 * Let ip_intr's mcast routing check handle mcast pkts
1066 			 */
1067 			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1068 			break;
1069 
1070 		case IPOPT_RR:
1071 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1072 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1073 				goto bad;
1074 			}
1075 			/*
1076 			 * If no space remains, ignore.
1077 			 */
1078 			off--;			/* 0 origin */
1079 			if (off > optlen - sizeof(struct in_addr))
1080 				break;
1081 			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1082 			    sizeof(ipaddr.sin_addr));
1083 			/*
1084 			 * locate outgoing interface; if we're the destination,
1085 			 * use the incoming interface (should be same).
1086 			 */
1087 			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1088 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
1089 				type = ICMP_UNREACH;
1090 				code = ICMP_UNREACH_HOST;
1091 				goto bad;
1092 			}
1093 			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1094 			    sizeof(struct in_addr));
1095 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1096 			break;
1097 
1098 		case IPOPT_TS:
1099 			code = cp - (u_char *)ip;
1100 			ipt = (struct ip_timestamp *)cp;
1101 			if (ipt->ipt_len < 5)
1102 				goto bad;
1103 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof(int32_t)) {
1104 				if (++ipt->ipt_oflw == 0)
1105 					goto bad;
1106 				break;
1107 			}
1108 			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
1109 			switch (ipt->ipt_flg) {
1110 
1111 			case IPOPT_TS_TSONLY:
1112 				break;
1113 
1114 			case IPOPT_TS_TSANDADDR:
1115 				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1116 				    sizeof(struct in_addr) > ipt->ipt_len)
1117 					goto bad;
1118 				ipaddr.sin_addr = dst;
1119 				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1120 							    m->m_pkthdr.rcvif);
1121 				if (ia == 0)
1122 					continue;
1123 				(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1124 				    sizeof(struct in_addr));
1125 				ipt->ipt_ptr += sizeof(struct in_addr);
1126 				break;
1127 
1128 			case IPOPT_TS_PRESPEC:
1129 				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1130 				    sizeof(struct in_addr) > ipt->ipt_len)
1131 					goto bad;
1132 				(void)memcpy(&ipaddr.sin_addr, sin,
1133 				    sizeof(struct in_addr));
1134 				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1135 					continue;
1136 				ipt->ipt_ptr += sizeof(struct in_addr);
1137 				break;
1138 
1139 			default:
1140 				goto bad;
1141 			}
1142 			ntime = iptime();
1143 			(void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
1144 			    sizeof(n_time));
1145 			ipt->ipt_ptr += sizeof(n_time);
1146 		}
1147 	}
1148 	if (forward && ipforwarding) {
1149 		ip_forward(m, 1);
1150 		return (1);
1151 	}
1152 	return (0);
1153 bad:
1154 	ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2;   /* XXX icmp_error adds in hdr length */
1155 	icmp_error(m, type, code, 0, 0);
1156 	ipstat.ips_badoptions++;
1157 	return (1);
1158 }
1159 
1160 /*
1161  * Given address of next destination (final or next hop),
1162  * return internet address info of interface to be used to get there.
1163  */
1164 static struct in_ifaddr *
1165 ip_rtaddr(dst)
1166 	 struct in_addr dst;
1167 {
1168 	register struct sockaddr_in *sin;
1169 
1170 	sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
1171 
1172 	if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
1173 		if (ipforward_rt.ro_rt) {
1174 			RTFREE(ipforward_rt.ro_rt);
1175 			ipforward_rt.ro_rt = 0;
1176 		}
1177 		sin->sin_family = AF_INET;
1178 		sin->sin_len = sizeof(*sin);
1179 		sin->sin_addr = dst;
1180 
1181 		rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1182 	}
1183 	if (ipforward_rt.ro_rt == 0)
1184 		return ((struct in_ifaddr *)0);
1185 	return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
1186 }
1187 
1188 /*
1189  * Save incoming source route for use in replies,
1190  * to be picked up later by ip_srcroute if the receiver is interested.
1191  */
1192 void
1193 save_rte(option, dst)
1194 	u_char *option;
1195 	struct in_addr dst;
1196 {
1197 	unsigned olen;
1198 
1199 	olen = option[IPOPT_OLEN];
1200 #ifdef DIAGNOSTIC
1201 	if (ipprintfs)
1202 		printf("save_rte: olen %d\n", olen);
1203 #endif
1204 	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1205 		return;
1206 	bcopy(option, ip_srcrt.srcopt, olen);
1207 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1208 	ip_srcrt.dst = dst;
1209 }
1210 
1211 /*
1212  * Retrieve incoming source route for use in replies,
1213  * in the same form used by setsockopt.
1214  * The first hop is placed before the options, will be removed later.
1215  */
1216 struct mbuf *
1217 ip_srcroute()
1218 {
1219 	register struct in_addr *p, *q;
1220 	register struct mbuf *m;
1221 
1222 	if (ip_nhops == 0)
1223 		return ((struct mbuf *)0);
1224 	m = m_get(M_DONTWAIT, MT_HEADER);
1225 	if (m == 0)
1226 		return ((struct mbuf *)0);
1227 
1228 #define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1229 
1230 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1231 	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1232 	    OPTSIZ;
1233 #ifdef DIAGNOSTIC
1234 	if (ipprintfs)
1235 		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1236 #endif
1237 
1238 	/*
1239 	 * First save first hop for return route
1240 	 */
1241 	p = &ip_srcrt.route[ip_nhops - 1];
1242 	*(mtod(m, struct in_addr *)) = *p--;
1243 #ifdef DIAGNOSTIC
1244 	if (ipprintfs)
1245 		printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr));
1246 #endif
1247 
1248 	/*
1249 	 * Copy option fields and padding (nop) to mbuf.
1250 	 */
1251 	ip_srcrt.nop = IPOPT_NOP;
1252 	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1253 	(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1254 	    &ip_srcrt.nop, OPTSIZ);
1255 	q = (struct in_addr *)(mtod(m, caddr_t) +
1256 	    sizeof(struct in_addr) + OPTSIZ);
1257 #undef OPTSIZ
1258 	/*
1259 	 * Record return path as an IP source route,
1260 	 * reversing the path (pointers are now aligned).
1261 	 */
1262 	while (p >= ip_srcrt.route) {
1263 #ifdef DIAGNOSTIC
1264 		if (ipprintfs)
1265 			printf(" %lx", ntohl(q->s_addr));
1266 #endif
1267 		*q++ = *p--;
1268 	}
1269 	/*
1270 	 * Last hop goes to final destination.
1271 	 */
1272 	*q = ip_srcrt.dst;
1273 #ifdef DIAGNOSTIC
1274 	if (ipprintfs)
1275 		printf(" %lx\n", ntohl(q->s_addr));
1276 #endif
1277 	return (m);
1278 }
1279 
1280 /*
1281  * Strip out IP options, at higher
1282  * level protocol in the kernel.
1283  * Second argument is buffer to which options
1284  * will be moved, and return value is their length.
1285  * XXX should be deleted; last arg currently ignored.
1286  */
1287 void
1288 ip_stripoptions(m, mopt)
1289 	register struct mbuf *m;
1290 	struct mbuf *mopt;
1291 {
1292 	register int i;
1293 	struct ip *ip = mtod(m, struct ip *);
1294 	register caddr_t opts;
1295 	int olen;
1296 
1297 	olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1298 	opts = (caddr_t)(ip + 1);
1299 	i = m->m_len - (sizeof (struct ip) + olen);
1300 	bcopy(opts + olen, opts, (unsigned)i);
1301 	m->m_len -= olen;
1302 	if (m->m_flags & M_PKTHDR)
1303 		m->m_pkthdr.len -= olen;
1304 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1305 }
1306 
1307 u_char inetctlerrmap[PRC_NCMDS] = {
1308 	0,		0,		0,		0,
1309 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1310 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1311 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1312 	0,		0,		0,		0,
1313 	ENOPROTOOPT
1314 };
1315 
1316 /*
1317  * Forward a packet.  If some error occurs return the sender
1318  * an icmp packet.  Note we can't always generate a meaningful
1319  * icmp message because icmp doesn't have a large enough repertoire
1320  * of codes and types.
1321  *
1322  * If not forwarding, just drop the packet.  This could be confusing
1323  * if ipforwarding was zero but some routing protocol was advancing
1324  * us as a gateway to somewhere.  However, we must let the routing
1325  * protocol deal with that.
1326  *
1327  * The srcrt parameter indicates whether the packet is being forwarded
1328  * via a source route.
1329  */
1330 static void
1331 ip_forward(m, srcrt)
1332 	struct mbuf *m;
1333 	int srcrt;
1334 {
1335 	register struct ip *ip = mtod(m, struct ip *);
1336 	register struct sockaddr_in *sin;
1337 	register struct rtentry *rt;
1338 	int error, type = 0, code = 0;
1339 	struct mbuf *mcopy;
1340 	n_long dest;
1341 	struct ifnet *destifp;
1342 
1343 	dest = 0;
1344 #ifdef DIAGNOSTIC
1345 	if (ipprintfs)
1346 		printf("forward: src %lx dst %lx ttl %x\n",
1347 		    (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
1348 		    ip->ip_ttl);
1349 #endif
1350 
1351 
1352 	if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
1353 		ipstat.ips_cantforward++;
1354 		m_freem(m);
1355 		return;
1356 	}
1357 	HTONS(ip->ip_id);
1358 	if (ip->ip_ttl <= IPTTLDEC) {
1359 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1360 		return;
1361 	}
1362 	ip->ip_ttl -= IPTTLDEC;
1363 
1364 	sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
1365 	if ((rt = ipforward_rt.ro_rt) == 0 ||
1366 	    ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
1367 		if (ipforward_rt.ro_rt) {
1368 			RTFREE(ipforward_rt.ro_rt);
1369 			ipforward_rt.ro_rt = 0;
1370 		}
1371 		sin->sin_family = AF_INET;
1372 		sin->sin_len = sizeof(*sin);
1373 		sin->sin_addr = ip->ip_dst;
1374 
1375 		rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1376 		if (ipforward_rt.ro_rt == 0) {
1377 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1378 			return;
1379 		}
1380 		rt = ipforward_rt.ro_rt;
1381 	}
1382 
1383 	/*
1384 	 * Save at most 64 bytes of the packet in case
1385 	 * we need to generate an ICMP message to the src.
1386 	 */
1387 	mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
1388 
1389 	/*
1390 	 * If forwarding packet using same interface that it came in on,
1391 	 * perhaps should send a redirect to sender to shortcut a hop.
1392 	 * Only send redirect if source is sending directly to us,
1393 	 * and if packet was not source routed (or has any options).
1394 	 * Also, don't send redirect if forwarding using a default route
1395 	 * or a route modified by a redirect.
1396 	 */
1397 #define	satosin(sa)	((struct sockaddr_in *)(sa))
1398 	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1399 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1400 	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1401 	    ipsendredirects && !srcrt) {
1402 #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1403 		u_long src = ntohl(ip->ip_src.s_addr);
1404 
1405 		if (RTA(rt) &&
1406 		    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1407 		    if (rt->rt_flags & RTF_GATEWAY)
1408 			dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1409 		    else
1410 			dest = ip->ip_dst.s_addr;
1411 		    /* Router requirements says to only send host redirects */
1412 		    type = ICMP_REDIRECT;
1413 		    code = ICMP_REDIRECT_HOST;
1414 #ifdef DIAGNOSTIC
1415 		    if (ipprintfs)
1416 		        printf("redirect (%d) to %lx\n", code, (u_long)dest);
1417 #endif
1418 		}
1419 	}
1420 
1421 	error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1422 			  IP_FORWARDING, 0);
1423 	if (error)
1424 		ipstat.ips_cantforward++;
1425 	else {
1426 		ipstat.ips_forward++;
1427 		if (type)
1428 			ipstat.ips_redirectsent++;
1429 		else {
1430 			if (mcopy) {
1431 				ipflow_create(&ipforward_rt, mcopy);
1432 				m_freem(mcopy);
1433 			}
1434 			return;
1435 		}
1436 	}
1437 	if (mcopy == NULL)
1438 		return;
1439 	destifp = NULL;
1440 
1441 	switch (error) {
1442 
1443 	case 0:				/* forwarded, but need redirect */
1444 		/* type, code set above */
1445 		break;
1446 
1447 	case ENETUNREACH:		/* shouldn't happen, checked above */
1448 	case EHOSTUNREACH:
1449 	case ENETDOWN:
1450 	case EHOSTDOWN:
1451 	default:
1452 		type = ICMP_UNREACH;
1453 		code = ICMP_UNREACH_HOST;
1454 		break;
1455 
1456 	case EMSGSIZE:
1457 		type = ICMP_UNREACH;
1458 		code = ICMP_UNREACH_NEEDFRAG;
1459 		if (ipforward_rt.ro_rt)
1460 			destifp = ipforward_rt.ro_rt->rt_ifp;
1461 		ipstat.ips_cantfrag++;
1462 		break;
1463 
1464 	case ENOBUFS:
1465 		type = ICMP_SOURCEQUENCH;
1466 		code = 0;
1467 		break;
1468 	}
1469 	icmp_error(mcopy, type, code, dest, destifp);
1470 }
1471 
1472 void
1473 ip_savecontrol(inp, mp, ip, m)
1474 	register struct inpcb *inp;
1475 	register struct mbuf **mp;
1476 	register struct ip *ip;
1477 	register struct mbuf *m;
1478 {
1479 	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1480 		struct timeval tv;
1481 
1482 		microtime(&tv);
1483 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1484 			SCM_TIMESTAMP, SOL_SOCKET);
1485 		if (*mp)
1486 			mp = &(*mp)->m_next;
1487 	}
1488 	if (inp->inp_flags & INP_RECVDSTADDR) {
1489 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1490 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1491 		if (*mp)
1492 			mp = &(*mp)->m_next;
1493 	}
1494 #ifdef notyet
1495 	/* XXX
1496 	 * Moving these out of udp_input() made them even more broken
1497 	 * than they already were.
1498 	 */
1499 	/* options were tossed already */
1500 	if (inp->inp_flags & INP_RECVOPTS) {
1501 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1502 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1503 		if (*mp)
1504 			mp = &(*mp)->m_next;
1505 	}
1506 	/* ip_srcroute doesn't do what we want here, need to fix */
1507 	if (inp->inp_flags & INP_RECVRETOPTS) {
1508 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1509 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1510 		if (*mp)
1511 			mp = &(*mp)->m_next;
1512 	}
1513 #endif
1514 	if (inp->inp_flags & INP_RECVIF) {
1515 		struct ifnet *ifp;
1516 		struct sdlbuf {
1517 			struct sockaddr_dl sdl;
1518 			u_char	pad[32];
1519 		} sdlbuf;
1520 		struct sockaddr_dl *sdp;
1521 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1522 
1523 		if (((ifp = m->m_pkthdr.rcvif))
1524 		&& ( ifp->if_index && (ifp->if_index <= if_index))) {
1525 			sdp = (struct sockaddr_dl *)(ifnet_addrs
1526 					[ifp->if_index - 1]->ifa_addr);
1527 			/*
1528 			 * Change our mind and don't try copy.
1529 			 */
1530 			if ((sdp->sdl_family != AF_LINK)
1531 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1532 				goto makedummy;
1533 			}
1534 			bcopy(sdp, sdl2, sdp->sdl_len);
1535 		} else {
1536 makedummy:
1537 			sdl2->sdl_len
1538 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1539 			sdl2->sdl_family = AF_LINK;
1540 			sdl2->sdl_index = 0;
1541 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1542 		}
1543 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
1544 			IP_RECVIF, IPPROTO_IP);
1545 		if (*mp)
1546 			mp = &(*mp)->m_next;
1547 	}
1548 }
1549 
1550 int
1551 ip_rsvp_init(struct socket *so)
1552 {
1553 	if (so->so_type != SOCK_RAW ||
1554 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1555 	  return EOPNOTSUPP;
1556 
1557 	if (ip_rsvpd != NULL)
1558 	  return EADDRINUSE;
1559 
1560 	ip_rsvpd = so;
1561 	/*
1562 	 * This may seem silly, but we need to be sure we don't over-increment
1563 	 * the RSVP counter, in case something slips up.
1564 	 */
1565 	if (!ip_rsvp_on) {
1566 		ip_rsvp_on = 1;
1567 		rsvp_on++;
1568 	}
1569 
1570 	return 0;
1571 }
1572 
1573 int
1574 ip_rsvp_done(void)
1575 {
1576 	ip_rsvpd = NULL;
1577 	/*
1578 	 * This may seem silly, but we need to be sure we don't over-decrement
1579 	 * the RSVP counter, in case something slips up.
1580 	 */
1581 	if (ip_rsvp_on) {
1582 		ip_rsvp_on = 0;
1583 		rsvp_on--;
1584 	}
1585 	return 0;
1586 }
1587