xref: /freebsd/sys/netinet/ip_input.c (revision 817420dc8eac7df799c78f5309b75092b7f7cd40)
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  * $FreeBSD$
35  */
36 
37 #define	_IP_VHL
38 
39 #include "opt_bootp.h"
40 #include "opt_ipfw.h"
41 #include "opt_ipdn.h"
42 #include "opt_ipdivert.h"
43 #include "opt_ipfilter.h"
44 #include "opt_ipstealth.h"
45 #include "opt_ipsec.h"
46 #include "opt_pfil_hooks.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/mbuf.h>
51 #include <sys/malloc.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/time.h>
56 #include <sys/kernel.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
59 
60 #include <net/pfil.h>
61 #include <net/if.h>
62 #include <net/if_var.h>
63 #include <net/if_dl.h>
64 #include <net/route.h>
65 #include <net/netisr.h>
66 #include <net/intrq.h>
67 
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip.h>
72 #include <netinet/in_pcb.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/ip_icmp.h>
75 #include <machine/in_cksum.h>
76 
77 #include <netinet/ipprotosw.h>
78 
79 #include <sys/socketvar.h>
80 
81 #include <netinet/ip_fw.h>
82 
83 #ifdef IPSEC
84 #include <netinet6/ipsec.h>
85 #include <netkey/key.h>
86 #endif
87 
88 #include "faith.h"
89 #if defined(NFAITH) && NFAITH > 0
90 #include <net/if_types.h>
91 #endif
92 
93 #ifdef DUMMYNET
94 #include <netinet/ip_dummynet.h>
95 #endif
96 
97 int rsvp_on = 0;
98 static int ip_rsvp_on;
99 struct socket *ip_rsvpd;
100 
101 int	ipforwarding = 0;
102 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
103     &ipforwarding, 0, "Enable IP forwarding between interfaces");
104 
105 static int	ipsendredirects = 1; /* XXX */
106 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
107     &ipsendredirects, 0, "Enable sending IP redirects");
108 
109 int	ip_defttl = IPDEFTTL;
110 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
111     &ip_defttl, 0, "Maximum TTL on IP packets");
112 
113 static int	ip_dosourceroute = 0;
114 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
115     &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
116 
117 static int	ip_acceptsourceroute = 0;
118 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
119     CTLFLAG_RW, &ip_acceptsourceroute, 0,
120     "Enable accepting source routed IP packets");
121 
122 static int	ip_keepfaith = 0;
123 SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
124 	&ip_keepfaith,	0,
125 	"Enable packet capture for FAITH IPv4->IPv6 translater daemon");
126 
127 #ifdef DIAGNOSTIC
128 static int	ipprintfs = 0;
129 #endif
130 
131 extern	struct domain inetdomain;
132 extern	struct ipprotosw inetsw[];
133 u_char	ip_protox[IPPROTO_MAX];
134 static int	ipqmaxlen = IFQ_MAXLEN;
135 struct	in_ifaddrhead in_ifaddrhead; /* first inet address */
136 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
137     &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
138 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
139     &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
140 
141 struct ipstat ipstat;
142 SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD,
143     &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
144 
145 /* Packet reassembly stuff */
146 #define IPREASS_NHASH_LOG2      6
147 #define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
148 #define IPREASS_HMASK           (IPREASS_NHASH - 1)
149 #define IPREASS_HASH(x,y) \
150 	(((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
151 
152 static struct ipq ipq[IPREASS_NHASH];
153 static int    nipq = 0;         /* total # of reass queues */
154 static int    maxnipq;
155 const  int    ipintrq_present = 1;
156 
157 #ifdef IPCTL_DEFMTU
158 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
159     &ip_mtu, 0, "Default MTU");
160 #endif
161 
162 #ifdef IPSTEALTH
163 static int	ipstealth = 0;
164 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
165     &ipstealth, 0, "");
166 #endif
167 
168 
169 /* Firewall hooks */
170 ip_fw_chk_t *ip_fw_chk_ptr;
171 ip_fw_ctl_t *ip_fw_ctl_ptr;
172 int fw_enable = 1 ;
173 
174 #ifdef DUMMYNET
175 ip_dn_ctl_t *ip_dn_ctl_ptr;
176 #endif
177 
178 
179 /*
180  * We need to save the IP options in case a protocol wants to respond
181  * to an incoming packet over the same route if the packet got here
182  * using IP source routing.  This allows connection establishment and
183  * maintenance when the remote end is on a network that is not known
184  * to us.
185  */
186 static int	ip_nhops = 0;
187 static	struct ip_srcrt {
188 	struct	in_addr dst;			/* final destination */
189 	char	nop;				/* one NOP to align */
190 	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
191 	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
192 } ip_srcrt;
193 
194 struct sockaddr_in *ip_fw_fwd_addr;
195 
196 static void	save_rte __P((u_char *, struct in_addr));
197 static int	ip_dooptions __P((struct mbuf *));
198 static void	ip_forward __P((struct mbuf *, int));
199 static void	ip_freef __P((struct ipq *));
200 #ifdef IPDIVERT
201 static struct	mbuf *ip_reass __P((struct mbuf *,
202 			struct ipq *, struct ipq *, u_int32_t *, u_int16_t *));
203 #else
204 static struct	mbuf *ip_reass __P((struct mbuf *, struct ipq *, struct ipq *));
205 #endif
206 static struct	in_ifaddr *ip_rtaddr __P((struct in_addr));
207 static void	ipintr __P((void));
208 
209 /*
210  * IP initialization: fill in IP protocol switch table.
211  * All protocols not implemented in kernel go to raw IP protocol handler.
212  */
213 void
214 ip_init()
215 {
216 	register struct ipprotosw *pr;
217 	register int i;
218 
219 	TAILQ_INIT(&in_ifaddrhead);
220 	pr = (struct ipprotosw *)pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
221 	if (pr == 0)
222 		panic("ip_init");
223 	for (i = 0; i < IPPROTO_MAX; i++)
224 		ip_protox[i] = pr - inetsw;
225 	for (pr = (struct ipprotosw *)inetdomain.dom_protosw;
226 	    pr < (struct ipprotosw *)inetdomain.dom_protoswNPROTOSW; pr++)
227 		if (pr->pr_domain->dom_family == PF_INET &&
228 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
229 			ip_protox[pr->pr_protocol] = pr - inetsw;
230 
231 	for (i = 0; i < IPREASS_NHASH; i++)
232 	    ipq[i].next = ipq[i].prev = &ipq[i];
233 
234 	maxnipq = nmbclusters/4;
235 
236 	ip_id = time_second & 0xffff;
237 	ipintrq.ifq_maxlen = ipqmaxlen;
238 
239 	register_netisr(NETISR_IP, ipintr);
240 }
241 
242 static struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
243 static struct	route ipforward_rt;
244 
245 /*
246  * Ip input routine.  Checksum and byte swap header.  If fragmented
247  * try to reassemble.  Process options.  Pass to next level.
248  */
249 void
250 ip_input(struct mbuf *m)
251 {
252 	struct ip *ip;
253 	struct ipq *fp;
254 	struct in_ifaddr *ia = NULL;
255 	int    i, hlen;
256 	u_short sum;
257 	u_int16_t divert_cookie;		/* firewall cookie */
258 #ifdef IPDIVERT
259 	u_int32_t divert_info = 0;		/* packet divert/tee info */
260 #endif
261 	struct ip_fw_chain *rule = NULL;
262 #ifdef PFIL_HOOKS
263 	struct packet_filter_hook *pfh;
264 	struct mbuf *m0;
265 	int rv;
266 #endif /* PFIL_HOOKS */
267 
268 #ifdef IPDIVERT
269 	/* Get and reset firewall cookie */
270 	divert_cookie = ip_divert_cookie;
271 	ip_divert_cookie = 0;
272 #else
273 	divert_cookie = 0;
274 #endif
275 
276 #if defined(IPFIREWALL) && defined(DUMMYNET)
277         /*
278          * dummynet packet are prepended a vestigial mbuf with
279          * m_type = MT_DUMMYNET and m_data pointing to the matching
280          * rule.
281          */
282         if (m->m_type == MT_DUMMYNET) {
283             rule = (struct ip_fw_chain *)(m->m_data) ;
284             m = m->m_next ;
285             ip = mtod(m, struct ip *);
286             hlen = IP_VHL_HL(ip->ip_vhl) << 2;
287             goto iphack ;
288         } else
289             rule = NULL ;
290 #endif
291 
292 #ifdef	DIAGNOSTIC
293 	if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
294 		panic("ip_input no HDR");
295 #endif
296 	ipstat.ips_total++;
297 
298 	if (m->m_pkthdr.len < sizeof(struct ip))
299 		goto tooshort;
300 
301 	if (m->m_len < sizeof (struct ip) &&
302 	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
303 		ipstat.ips_toosmall++;
304 		return;
305 	}
306 	ip = mtod(m, struct ip *);
307 
308 	if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
309 		ipstat.ips_badvers++;
310 		goto bad;
311 	}
312 
313 	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
314 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
315 		ipstat.ips_badhlen++;
316 		goto bad;
317 	}
318 	if (hlen > m->m_len) {
319 		if ((m = m_pullup(m, hlen)) == 0) {
320 			ipstat.ips_badhlen++;
321 			return;
322 		}
323 		ip = mtod(m, struct ip *);
324 	}
325 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
326 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
327 	} else {
328 		if (hlen == sizeof(struct ip)) {
329 			sum = in_cksum_hdr(ip);
330 		} else {
331 			sum = in_cksum(m, hlen);
332 		}
333 	}
334 	if (sum) {
335 		ipstat.ips_badsum++;
336 		goto bad;
337 	}
338 
339 	/*
340 	 * Convert fields to host representation.
341 	 */
342 	NTOHS(ip->ip_len);
343 	if (ip->ip_len < hlen) {
344 		ipstat.ips_badlen++;
345 		goto bad;
346 	}
347 	NTOHS(ip->ip_off);
348 
349 	/*
350 	 * Check that the amount of data in the buffers
351 	 * is as at least much as the IP header would have us expect.
352 	 * Trim mbufs if longer than we expect.
353 	 * Drop packet if shorter than we expect.
354 	 */
355 	if (m->m_pkthdr.len < ip->ip_len) {
356 tooshort:
357 		ipstat.ips_tooshort++;
358 		goto bad;
359 	}
360 	if (m->m_pkthdr.len > ip->ip_len) {
361 		if (m->m_len == m->m_pkthdr.len) {
362 			m->m_len = ip->ip_len;
363 			m->m_pkthdr.len = ip->ip_len;
364 		} else
365 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
366 	}
367 	/*
368 	 * IpHack's section.
369 	 * Right now when no processing on packet has done
370 	 * and it is still fresh out of network we do our black
371 	 * deals with it.
372 	 * - Firewall: deny/allow/divert
373 	 * - Xlate: translate packet's addr/port (NAT).
374 	 * - Pipe: pass pkt through dummynet.
375 	 * - Wrap: fake packet's addr/port <unimpl.>
376 	 * - Encapsulate: put it in another IP and send out. <unimp.>
377  	 */
378 
379 #if defined(IPFIREWALL) && defined(DUMMYNET)
380 iphack:
381 #endif
382 
383 #ifdef PFIL_HOOKS
384 	/*
385 	 * Run through list of hooks for input packets.  If there are any
386 	 * filters which require that additional packets in the flow are
387 	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
388 	 * Note that filters must _never_ set this flag, as another filter
389 	 * in the list may have previously cleared it.
390 	 */
391 	m0 = m;
392 	pfh = pfil_hook_get(PFIL_IN, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
393 	for (; pfh; pfh = pfh->pfil_link.tqe_next)
394 		if (pfh->pfil_func) {
395 			rv = pfh->pfil_func(ip, hlen,
396 					    m->m_pkthdr.rcvif, 0, &m0);
397 			if (rv)
398 				return;
399 			m = m0;
400 			if (m == NULL)
401 				return;
402 			ip = mtod(m, struct ip *);
403 		}
404 #endif /* PFIL_HOOKS */
405 
406 	if (fw_enable && ip_fw_chk_ptr) {
407 #ifdef IPFIREWALL_FORWARD
408 		/*
409 		 * If we've been forwarded from the output side, then
410 		 * skip the firewall a second time
411 		 */
412 		if (ip_fw_fwd_addr)
413 			goto ours;
414 #endif	/* IPFIREWALL_FORWARD */
415 		/*
416 		 * See the comment in ip_output for the return values
417 		 * produced by the firewall.
418 		 */
419 		i = (*ip_fw_chk_ptr)(&ip,
420 		    hlen, NULL, &divert_cookie, &m, &rule, &ip_fw_fwd_addr);
421 		if (m == NULL)		/* Packet discarded by firewall */
422 			return;
423 		if (i == 0 && ip_fw_fwd_addr == NULL)	/* common case */
424 			goto pass;
425 #ifdef DUMMYNET
426                 if ((i & IP_FW_PORT_DYNT_FLAG) != 0) {
427                         /* Send packet to the appropriate pipe */
428                         dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule,
429 				    0);
430 			return;
431 		}
432 #endif
433 #ifdef IPDIVERT
434 		if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) {
435 			/* Divert or tee packet */
436 			divert_info = i;
437 			goto ours;
438 		}
439 #endif
440 #ifdef IPFIREWALL_FORWARD
441 		if (i == 0 && ip_fw_fwd_addr != NULL)
442 			goto pass;
443 #endif
444 		/*
445 		 * if we get here, the packet must be dropped
446 		 */
447 		m_freem(m);
448 		return;
449 	}
450 pass:
451 
452 	/*
453 	 * Process options and, if not destined for us,
454 	 * ship it on.  ip_dooptions returns 1 when an
455 	 * error was detected (causing an icmp message
456 	 * to be sent and the original packet to be freed).
457 	 */
458 	ip_nhops = 0;		/* for source routed packets */
459 	if (hlen > sizeof (struct ip) && ip_dooptions(m)) {
460 #ifdef IPFIREWALL_FORWARD
461 		ip_fw_fwd_addr = NULL;
462 #endif
463 		return;
464 	}
465 
466         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
467          * matter if it is destined to another node, or whether it is
468          * a multicast one, RSVP wants it! and prevents it from being forwarded
469          * anywhere else. Also checks if the rsvp daemon is running before
470 	 * grabbing the packet.
471          */
472 	if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
473 		goto ours;
474 
475 	/*
476 	 * Check our list of addresses, to see if the packet is for us.
477 	 * If we don't have any addresses, assume any unicast packet
478 	 * we receive might be for us (and let the upper layers deal
479 	 * with it).
480 	 */
481 	if (TAILQ_EMPTY(&in_ifaddrhead) &&
482 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
483 		goto ours;
484 
485 	for (ia = TAILQ_FIRST(&in_ifaddrhead); ia;
486 					ia = TAILQ_NEXT(ia, ia_link)) {
487 #define	satosin(sa)	((struct sockaddr_in *)(sa))
488 
489 #ifdef BOOTP_COMPAT
490 		if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
491 			goto ours;
492 #endif
493 #ifdef IPFIREWALL_FORWARD
494 		/*
495 		 * If the addr to forward to is one of ours, we pretend to
496 		 * be the destination for this packet.
497 		 */
498 		if (ip_fw_fwd_addr == NULL) {
499 			if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
500 				goto ours;
501 		} else if (IA_SIN(ia)->sin_addr.s_addr ==
502 					 ip_fw_fwd_addr->sin_addr.s_addr)
503 			goto ours;
504 #else
505 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
506 			goto ours;
507 #endif
508 		if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
509 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
510 			    ip->ip_dst.s_addr)
511 				goto ours;
512 			if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
513 				goto ours;
514 		}
515 	}
516 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
517 		struct in_multi *inm;
518 		if (ip_mrouter) {
519 			/*
520 			 * If we are acting as a multicast router, all
521 			 * incoming multicast packets are passed to the
522 			 * kernel-level multicast forwarding function.
523 			 * The packet is returned (relatively) intact; if
524 			 * ip_mforward() returns a non-zero value, the packet
525 			 * must be discarded, else it may be accepted below.
526 			 */
527 			if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
528 				ipstat.ips_cantforward++;
529 				m_freem(m);
530 				return;
531 			}
532 
533 			/*
534 			 * The process-level routing demon needs to receive
535 			 * all multicast IGMP packets, whether or not this
536 			 * host belongs to their destination groups.
537 			 */
538 			if (ip->ip_p == IPPROTO_IGMP)
539 				goto ours;
540 			ipstat.ips_forward++;
541 		}
542 		/*
543 		 * See if we belong to the destination multicast group on the
544 		 * arrival interface.
545 		 */
546 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
547 		if (inm == NULL) {
548 			ipstat.ips_notmember++;
549 			m_freem(m);
550 			return;
551 		}
552 		goto ours;
553 	}
554 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
555 		goto ours;
556 	if (ip->ip_dst.s_addr == INADDR_ANY)
557 		goto ours;
558 
559 #if defined(NFAITH) && 0 < NFAITH
560 	/*
561 	 * FAITH(Firewall Aided Internet Translator)
562 	 */
563 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
564 		if (ip_keepfaith) {
565 			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
566 				goto ours;
567 		}
568 		m_freem(m);
569 		return;
570 	}
571 #endif
572 	/*
573 	 * Not for us; forward if possible and desirable.
574 	 */
575 	if (ipforwarding == 0) {
576 		ipstat.ips_cantforward++;
577 		m_freem(m);
578 	} else
579 		ip_forward(m, 0);
580 #ifdef IPFIREWALL_FORWARD
581 	ip_fw_fwd_addr = NULL;
582 #endif
583 	return;
584 
585 ours:
586 	/* Count the packet in the ip address stats */
587 	if (ia != NULL) {
588 		ia->ia_ifa.if_ipackets++;
589 		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
590 	}
591 
592 	/*
593 	 * If offset or IP_MF are set, must reassemble.
594 	 * Otherwise, nothing need be done.
595 	 * (We could look in the reassembly queue to see
596 	 * if the packet was previously fragmented,
597 	 * but it's not worth the time; just let them time out.)
598 	 */
599 	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
600 
601 		sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
602 		/*
603 		 * Look for queue of fragments
604 		 * of this datagram.
605 		 */
606 		for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
607 			if (ip->ip_id == fp->ipq_id &&
608 			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
609 			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
610 			    ip->ip_p == fp->ipq_p)
611 				goto found;
612 
613 		fp = 0;
614 
615 		/* check if there's a place for the new queue */
616 		if (nipq > maxnipq) {
617 		    /*
618 		     * drop something from the tail of the current queue
619 		     * before proceeding further
620 		     */
621 		    if (ipq[sum].prev == &ipq[sum]) {   /* gak */
622 			for (i = 0; i < IPREASS_NHASH; i++) {
623 			    if (ipq[i].prev != &ipq[i]) {
624 				ip_freef(ipq[i].prev);
625 				break;
626 			    }
627 			}
628 		    } else
629 			ip_freef(ipq[sum].prev);
630 		}
631 found:
632 		/*
633 		 * Adjust ip_len to not reflect header,
634 		 * convert offset of this to bytes.
635 		 */
636 		ip->ip_len -= hlen;
637 		if (ip->ip_off & IP_MF) {
638 		        /*
639 		         * Make sure that fragments have a data length
640 			 * that's a non-zero multiple of 8 bytes.
641 		         */
642 			if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
643 				ipstat.ips_toosmall++; /* XXX */
644 				goto bad;
645 			}
646 			m->m_flags |= M_FRAG;
647 		}
648 		ip->ip_off <<= 3;
649 
650 		/*
651 		 * Attempt reassembly; if it succeeds, proceed.
652 		 */
653 		ipstat.ips_fragments++;
654 		m->m_pkthdr.header = ip;
655 #ifdef IPDIVERT
656 		m = ip_reass(m,
657 		    fp, &ipq[sum], &divert_info, &divert_cookie);
658 #else
659 		m = ip_reass(m, fp, &ipq[sum]);
660 #endif
661 		if (m == 0) {
662 #ifdef IPFIREWALL_FORWARD
663 			ip_fw_fwd_addr = NULL;
664 #endif
665 			return;
666 		}
667 		ipstat.ips_reassembled++;
668 		ip = mtod(m, struct ip *);
669 		/* Get the header length of the reassembled packet */
670 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
671 #ifdef IPDIVERT
672 		/* Restore original checksum before diverting packet */
673 		if (divert_info != 0) {
674 			ip->ip_len += hlen;
675 			HTONS(ip->ip_len);
676 			HTONS(ip->ip_off);
677 			ip->ip_sum = 0;
678 			if (hlen == sizeof(struct ip))
679 				ip->ip_sum = in_cksum_hdr(ip);
680 			else
681 				ip->ip_sum = in_cksum(m, hlen);
682 			NTOHS(ip->ip_off);
683 			NTOHS(ip->ip_len);
684 			ip->ip_len -= hlen;
685 		}
686 #endif
687 	} else
688 		ip->ip_len -= hlen;
689 
690 #ifdef IPDIVERT
691 	/*
692 	 * Divert or tee packet to the divert protocol if required.
693 	 *
694 	 * If divert_info is zero then cookie should be too, so we shouldn't
695 	 * need to clear them here.  Assume divert_packet() does so also.
696 	 */
697 	if (divert_info != 0) {
698 		struct mbuf *clone = NULL;
699 
700 		/* Clone packet if we're doing a 'tee' */
701 		if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0)
702 			clone = m_dup(m, M_DONTWAIT);
703 
704 		/* Restore packet header fields to original values */
705 		ip->ip_len += hlen;
706 		HTONS(ip->ip_len);
707 		HTONS(ip->ip_off);
708 
709 		/* Deliver packet to divert input routine */
710 		ip_divert_cookie = divert_cookie;
711 		divert_packet(m, 1, divert_info & 0xffff);
712 		ipstat.ips_delivered++;
713 
714 		/* If 'tee', continue with original packet */
715 		if (clone == NULL)
716 			return;
717 		m = clone;
718 		ip = mtod(m, struct ip *);
719 	}
720 #endif
721 
722 	/*
723 	 * Switch out to protocol's input routine.
724 	 */
725 	ipstat.ips_delivered++;
726     {
727 	int off = hlen, nh = ip->ip_p;
728 
729 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, off, nh);
730 #ifdef	IPFIREWALL_FORWARD
731 	ip_fw_fwd_addr = NULL;	/* tcp needed it */
732 #endif
733 	return;
734     }
735 bad:
736 #ifdef	IPFIREWALL_FORWARD
737 	ip_fw_fwd_addr = NULL;
738 #endif
739 	m_freem(m);
740 }
741 
742 /*
743  * IP software interrupt routine - to go away sometime soon
744  */
745 static void
746 ipintr(void)
747 {
748 	int s;
749 	struct mbuf *m;
750 
751 	while(1) {
752 		s = splimp();
753 		IF_DEQUEUE(&ipintrq, m);
754 		splx(s);
755 		if (m == 0)
756 			return;
757 		ip_input(m);
758 	}
759 }
760 
761 /*
762  * Take incoming datagram fragment and try to reassemble it into
763  * whole datagram.  If a chain for reassembly of this datagram already
764  * exists, then it is given as fp; otherwise have to make a chain.
765  *
766  * When IPDIVERT enabled, keep additional state with each packet that
767  * tells us if we need to divert or tee the packet we're building.
768  */
769 
770 static struct mbuf *
771 #ifdef IPDIVERT
772 ip_reass(m, fp, where, divinfo, divcookie)
773 #else
774 ip_reass(m, fp, where)
775 #endif
776 	register struct mbuf *m;
777 	register struct ipq *fp;
778 	struct   ipq    *where;
779 #ifdef IPDIVERT
780 	u_int32_t *divinfo;
781 	u_int16_t *divcookie;
782 #endif
783 {
784 	struct ip *ip = mtod(m, struct ip *);
785 	register struct mbuf *p, *q, *nq;
786 	struct mbuf *t;
787 	int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
788 	int i, next;
789 
790 	/*
791 	 * Presence of header sizes in mbufs
792 	 * would confuse code below.
793 	 */
794 	m->m_data += hlen;
795 	m->m_len -= hlen;
796 
797 	/*
798 	 * If first fragment to arrive, create a reassembly queue.
799 	 */
800 	if (fp == 0) {
801 		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
802 			goto dropfrag;
803 		fp = mtod(t, struct ipq *);
804 		insque(fp, where);
805 		nipq++;
806 		fp->ipq_ttl = IPFRAGTTL;
807 		fp->ipq_p = ip->ip_p;
808 		fp->ipq_id = ip->ip_id;
809 		fp->ipq_src = ip->ip_src;
810 		fp->ipq_dst = ip->ip_dst;
811 		fp->ipq_frags = m;
812 		m->m_nextpkt = NULL;
813 #ifdef IPDIVERT
814 		fp->ipq_div_info = 0;
815 		fp->ipq_div_cookie = 0;
816 #endif
817 		goto inserted;
818 	}
819 
820 #define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
821 
822 	/*
823 	 * Find a segment which begins after this one does.
824 	 */
825 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
826 		if (GETIP(q)->ip_off > ip->ip_off)
827 			break;
828 
829 	/*
830 	 * If there is a preceding segment, it may provide some of
831 	 * our data already.  If so, drop the data from the incoming
832 	 * segment.  If it provides all of our data, drop us, otherwise
833 	 * stick new segment in the proper place.
834 	 *
835 	 * If some of the data is dropped from the the preceding
836 	 * segment, then it's checksum is invalidated.
837 	 */
838 	if (p) {
839 		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
840 		if (i > 0) {
841 			if (i >= ip->ip_len)
842 				goto dropfrag;
843 			m_adj(m, i);
844 			m->m_pkthdr.csum_flags = 0;
845 			ip->ip_off += i;
846 			ip->ip_len -= i;
847 		}
848 		m->m_nextpkt = p->m_nextpkt;
849 		p->m_nextpkt = m;
850 	} else {
851 		m->m_nextpkt = fp->ipq_frags;
852 		fp->ipq_frags = m;
853 	}
854 
855 	/*
856 	 * While we overlap succeeding segments trim them or,
857 	 * if they are completely covered, dequeue them.
858 	 */
859 	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
860 	     q = nq) {
861 		i = (ip->ip_off + ip->ip_len) -
862 		    GETIP(q)->ip_off;
863 		if (i < GETIP(q)->ip_len) {
864 			GETIP(q)->ip_len -= i;
865 			GETIP(q)->ip_off += i;
866 			m_adj(q, i);
867 			q->m_pkthdr.csum_flags = 0;
868 			break;
869 		}
870 		nq = q->m_nextpkt;
871 		m->m_nextpkt = nq;
872 		m_freem(q);
873 	}
874 
875 inserted:
876 
877 #ifdef IPDIVERT
878 	/*
879 	 * Transfer firewall instructions to the fragment structure.
880 	 * Any fragment diverting causes the whole packet to divert.
881 	 */
882 	fp->ipq_div_info = *divinfo;
883 	fp->ipq_div_cookie = *divcookie;
884 	*divinfo = 0;
885 	*divcookie = 0;
886 #endif
887 
888 	/*
889 	 * Check for complete reassembly.
890 	 */
891 	next = 0;
892 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
893 		if (GETIP(q)->ip_off != next)
894 			return (0);
895 		next += GETIP(q)->ip_len;
896 	}
897 	/* Make sure the last packet didn't have the IP_MF flag */
898 	if (p->m_flags & M_FRAG)
899 		return (0);
900 
901 	/*
902 	 * Reassembly is complete.  Make sure the packet is a sane size.
903 	 */
904 	q = fp->ipq_frags;
905 	ip = GETIP(q);
906 	if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
907 		ipstat.ips_toolong++;
908 		ip_freef(fp);
909 		return (0);
910 	}
911 
912 	/*
913 	 * Concatenate fragments.
914 	 */
915 	m = q;
916 	t = m->m_next;
917 	m->m_next = 0;
918 	m_cat(m, t);
919 	nq = q->m_nextpkt;
920 	q->m_nextpkt = 0;
921 	for (q = nq; q != NULL; q = nq) {
922 		nq = q->m_nextpkt;
923 		q->m_nextpkt = NULL;
924 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
925 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
926 		m_cat(m, q);
927 	}
928 
929 #ifdef IPDIVERT
930 	/*
931 	 * Extract firewall instructions from the fragment structure.
932 	 */
933 	*divinfo = fp->ipq_div_info;
934 	*divcookie = fp->ipq_div_cookie;
935 #endif
936 
937 	/*
938 	 * Create header for new ip packet by
939 	 * modifying header of first packet;
940 	 * dequeue and discard fragment reassembly header.
941 	 * Make header visible.
942 	 */
943 	ip->ip_len = next;
944 	ip->ip_src = fp->ipq_src;
945 	ip->ip_dst = fp->ipq_dst;
946 	remque(fp);
947 	nipq--;
948 	(void) m_free(dtom(fp));
949 	m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
950 	m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
951 	/* some debugging cruft by sklower, below, will go away soon */
952 	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
953 		register int plen = 0;
954 		for (t = m; t; t = t->m_next)
955 			plen += t->m_len;
956 		m->m_pkthdr.len = plen;
957 	}
958 	return (m);
959 
960 dropfrag:
961 #ifdef IPDIVERT
962 	*divinfo = 0;
963 	*divcookie = 0;
964 #endif
965 	ipstat.ips_fragdropped++;
966 	m_freem(m);
967 	return (0);
968 
969 #undef GETIP
970 }
971 
972 /*
973  * Free a fragment reassembly header and all
974  * associated datagrams.
975  */
976 static void
977 ip_freef(fp)
978 	struct ipq *fp;
979 {
980 	register struct mbuf *q;
981 
982 	while (fp->ipq_frags) {
983 		q = fp->ipq_frags;
984 		fp->ipq_frags = q->m_nextpkt;
985 		m_freem(q);
986 	}
987 	remque(fp);
988 	(void) m_free(dtom(fp));
989 	nipq--;
990 }
991 
992 /*
993  * IP timer processing;
994  * if a timer expires on a reassembly
995  * queue, discard it.
996  */
997 void
998 ip_slowtimo()
999 {
1000 	register struct ipq *fp;
1001 	int s = splnet();
1002 	int i;
1003 
1004 	for (i = 0; i < IPREASS_NHASH; i++) {
1005 		fp = ipq[i].next;
1006 		if (fp == 0)
1007 			continue;
1008 		while (fp != &ipq[i]) {
1009 			--fp->ipq_ttl;
1010 			fp = fp->next;
1011 			if (fp->prev->ipq_ttl == 0) {
1012 				ipstat.ips_fragtimeout++;
1013 				ip_freef(fp->prev);
1014 			}
1015 		}
1016 	}
1017 	ipflow_slowtimo();
1018 	splx(s);
1019 }
1020 
1021 /*
1022  * Drain off all datagram fragments.
1023  */
1024 void
1025 ip_drain()
1026 {
1027 	int     i;
1028 
1029 	for (i = 0; i < IPREASS_NHASH; i++) {
1030 		while (ipq[i].next != &ipq[i]) {
1031 			ipstat.ips_fragdropped++;
1032 			ip_freef(ipq[i].next);
1033 		}
1034 	}
1035 	in_rtqdrain();
1036 }
1037 
1038 /*
1039  * Do option processing on a datagram,
1040  * possibly discarding it if bad options are encountered,
1041  * or forwarding it if source-routed.
1042  * Returns 1 if packet has been forwarded/freed,
1043  * 0 if the packet should be processed further.
1044  */
1045 static int
1046 ip_dooptions(m)
1047 	struct mbuf *m;
1048 {
1049 	register struct ip *ip = mtod(m, struct ip *);
1050 	register u_char *cp;
1051 	register struct ip_timestamp *ipt;
1052 	register struct in_ifaddr *ia;
1053 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1054 	struct in_addr *sin, dst;
1055 	n_time ntime;
1056 
1057 	dst = ip->ip_dst;
1058 	cp = (u_char *)(ip + 1);
1059 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1060 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1061 		opt = cp[IPOPT_OPTVAL];
1062 		if (opt == IPOPT_EOL)
1063 			break;
1064 		if (opt == IPOPT_NOP)
1065 			optlen = 1;
1066 		else {
1067 			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1068 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1069 				goto bad;
1070 			}
1071 			optlen = cp[IPOPT_OLEN];
1072 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1073 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1074 				goto bad;
1075 			}
1076 		}
1077 		switch (opt) {
1078 
1079 		default:
1080 			break;
1081 
1082 		/*
1083 		 * Source routing with record.
1084 		 * Find interface with current destination address.
1085 		 * If none on this machine then drop if strictly routed,
1086 		 * or do nothing if loosely routed.
1087 		 * Record interface address and bring up next address
1088 		 * component.  If strictly routed make sure next
1089 		 * address is on directly accessible net.
1090 		 */
1091 		case IPOPT_LSRR:
1092 		case IPOPT_SSRR:
1093 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1094 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1095 				goto bad;
1096 			}
1097 			ipaddr.sin_addr = ip->ip_dst;
1098 			ia = (struct in_ifaddr *)
1099 				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1100 			if (ia == 0) {
1101 				if (opt == IPOPT_SSRR) {
1102 					type = ICMP_UNREACH;
1103 					code = ICMP_UNREACH_SRCFAIL;
1104 					goto bad;
1105 				}
1106 				if (!ip_dosourceroute)
1107 					goto nosourcerouting;
1108 				/*
1109 				 * Loose routing, and not at next destination
1110 				 * yet; nothing to do except forward.
1111 				 */
1112 				break;
1113 			}
1114 			off--;			/* 0 origin */
1115 			if (off > optlen - (int)sizeof(struct in_addr)) {
1116 				/*
1117 				 * End of source route.  Should be for us.
1118 				 */
1119 				if (!ip_acceptsourceroute)
1120 					goto nosourcerouting;
1121 				save_rte(cp, ip->ip_src);
1122 				break;
1123 			}
1124 
1125 			if (!ip_dosourceroute) {
1126 				if (ipforwarding) {
1127 					char buf[16]; /* aaa.bbb.ccc.ddd\0 */
1128 					/*
1129 					 * Acting as a router, so generate ICMP
1130 					 */
1131 nosourcerouting:
1132 					strcpy(buf, inet_ntoa(ip->ip_dst));
1133 					log(LOG_WARNING,
1134 					    "attempted source route from %s to %s\n",
1135 					    inet_ntoa(ip->ip_src), buf);
1136 					type = ICMP_UNREACH;
1137 					code = ICMP_UNREACH_SRCFAIL;
1138 					goto bad;
1139 				} else {
1140 					/*
1141 					 * Not acting as a router, so silently drop.
1142 					 */
1143 					ipstat.ips_cantforward++;
1144 					m_freem(m);
1145 					return (1);
1146 				}
1147 			}
1148 
1149 			/*
1150 			 * locate outgoing interface
1151 			 */
1152 			(void)memcpy(&ipaddr.sin_addr, cp + off,
1153 			    sizeof(ipaddr.sin_addr));
1154 
1155 			if (opt == IPOPT_SSRR) {
1156 #define	INA	struct in_ifaddr *
1157 #define	SA	struct sockaddr *
1158 			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1159 				ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1160 			} else
1161 				ia = ip_rtaddr(ipaddr.sin_addr);
1162 			if (ia == 0) {
1163 				type = ICMP_UNREACH;
1164 				code = ICMP_UNREACH_SRCFAIL;
1165 				goto bad;
1166 			}
1167 			ip->ip_dst = ipaddr.sin_addr;
1168 			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1169 			    sizeof(struct in_addr));
1170 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1171 			/*
1172 			 * Let ip_intr's mcast routing check handle mcast pkts
1173 			 */
1174 			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1175 			break;
1176 
1177 		case IPOPT_RR:
1178 			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1179 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1180 				goto bad;
1181 			}
1182 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1183 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1184 				goto bad;
1185 			}
1186 			/*
1187 			 * If no space remains, ignore.
1188 			 */
1189 			off--;			/* 0 origin */
1190 			if (off > optlen - (int)sizeof(struct in_addr))
1191 				break;
1192 			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1193 			    sizeof(ipaddr.sin_addr));
1194 			/*
1195 			 * locate outgoing interface; if we're the destination,
1196 			 * use the incoming interface (should be same).
1197 			 */
1198 			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1199 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
1200 				type = ICMP_UNREACH;
1201 				code = ICMP_UNREACH_HOST;
1202 				goto bad;
1203 			}
1204 			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1205 			    sizeof(struct in_addr));
1206 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1207 			break;
1208 
1209 		case IPOPT_TS:
1210 			code = cp - (u_char *)ip;
1211 			ipt = (struct ip_timestamp *)cp;
1212 			if (ipt->ipt_len < 5)
1213 				goto bad;
1214 			if (ipt->ipt_ptr >
1215 			    ipt->ipt_len - (int)sizeof(int32_t)) {
1216 				if (++ipt->ipt_oflw == 0)
1217 					goto bad;
1218 				break;
1219 			}
1220 			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
1221 			switch (ipt->ipt_flg) {
1222 
1223 			case IPOPT_TS_TSONLY:
1224 				break;
1225 
1226 			case IPOPT_TS_TSANDADDR:
1227 				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1228 				    sizeof(struct in_addr) > ipt->ipt_len)
1229 					goto bad;
1230 				ipaddr.sin_addr = dst;
1231 				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1232 							    m->m_pkthdr.rcvif);
1233 				if (ia == 0)
1234 					continue;
1235 				(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1236 				    sizeof(struct in_addr));
1237 				ipt->ipt_ptr += sizeof(struct in_addr);
1238 				break;
1239 
1240 			case IPOPT_TS_PRESPEC:
1241 				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1242 				    sizeof(struct in_addr) > ipt->ipt_len)
1243 					goto bad;
1244 				(void)memcpy(&ipaddr.sin_addr, sin,
1245 				    sizeof(struct in_addr));
1246 				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1247 					continue;
1248 				ipt->ipt_ptr += sizeof(struct in_addr);
1249 				break;
1250 
1251 			default:
1252 				goto bad;
1253 			}
1254 			ntime = iptime();
1255 			(void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
1256 			    sizeof(n_time));
1257 			ipt->ipt_ptr += sizeof(n_time);
1258 		}
1259 	}
1260 	if (forward && ipforwarding) {
1261 		ip_forward(m, 1);
1262 		return (1);
1263 	}
1264 	return (0);
1265 bad:
1266 	icmp_error(m, type, code, 0, 0);
1267 	ipstat.ips_badoptions++;
1268 	return (1);
1269 }
1270 
1271 /*
1272  * Given address of next destination (final or next hop),
1273  * return internet address info of interface to be used to get there.
1274  */
1275 static struct in_ifaddr *
1276 ip_rtaddr(dst)
1277 	 struct in_addr dst;
1278 {
1279 	register struct sockaddr_in *sin;
1280 
1281 	sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
1282 
1283 	if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
1284 		if (ipforward_rt.ro_rt) {
1285 			RTFREE(ipforward_rt.ro_rt);
1286 			ipforward_rt.ro_rt = 0;
1287 		}
1288 		sin->sin_family = AF_INET;
1289 		sin->sin_len = sizeof(*sin);
1290 		sin->sin_addr = dst;
1291 
1292 		rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1293 	}
1294 	if (ipforward_rt.ro_rt == 0)
1295 		return ((struct in_ifaddr *)0);
1296 	return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
1297 }
1298 
1299 /*
1300  * Save incoming source route for use in replies,
1301  * to be picked up later by ip_srcroute if the receiver is interested.
1302  */
1303 void
1304 save_rte(option, dst)
1305 	u_char *option;
1306 	struct in_addr dst;
1307 {
1308 	unsigned olen;
1309 
1310 	olen = option[IPOPT_OLEN];
1311 #ifdef DIAGNOSTIC
1312 	if (ipprintfs)
1313 		printf("save_rte: olen %d\n", olen);
1314 #endif
1315 	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1316 		return;
1317 	bcopy(option, ip_srcrt.srcopt, olen);
1318 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1319 	ip_srcrt.dst = dst;
1320 }
1321 
1322 /*
1323  * Retrieve incoming source route for use in replies,
1324  * in the same form used by setsockopt.
1325  * The first hop is placed before the options, will be removed later.
1326  */
1327 struct mbuf *
1328 ip_srcroute()
1329 {
1330 	register struct in_addr *p, *q;
1331 	register struct mbuf *m;
1332 
1333 	if (ip_nhops == 0)
1334 		return ((struct mbuf *)0);
1335 	m = m_get(M_DONTWAIT, MT_HEADER);
1336 	if (m == 0)
1337 		return ((struct mbuf *)0);
1338 
1339 #define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1340 
1341 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1342 	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1343 	    OPTSIZ;
1344 #ifdef DIAGNOSTIC
1345 	if (ipprintfs)
1346 		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1347 #endif
1348 
1349 	/*
1350 	 * First save first hop for return route
1351 	 */
1352 	p = &ip_srcrt.route[ip_nhops - 1];
1353 	*(mtod(m, struct in_addr *)) = *p--;
1354 #ifdef DIAGNOSTIC
1355 	if (ipprintfs)
1356 		printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1357 #endif
1358 
1359 	/*
1360 	 * Copy option fields and padding (nop) to mbuf.
1361 	 */
1362 	ip_srcrt.nop = IPOPT_NOP;
1363 	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1364 	(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1365 	    &ip_srcrt.nop, OPTSIZ);
1366 	q = (struct in_addr *)(mtod(m, caddr_t) +
1367 	    sizeof(struct in_addr) + OPTSIZ);
1368 #undef OPTSIZ
1369 	/*
1370 	 * Record return path as an IP source route,
1371 	 * reversing the path (pointers are now aligned).
1372 	 */
1373 	while (p >= ip_srcrt.route) {
1374 #ifdef DIAGNOSTIC
1375 		if (ipprintfs)
1376 			printf(" %lx", (u_long)ntohl(q->s_addr));
1377 #endif
1378 		*q++ = *p--;
1379 	}
1380 	/*
1381 	 * Last hop goes to final destination.
1382 	 */
1383 	*q = ip_srcrt.dst;
1384 #ifdef DIAGNOSTIC
1385 	if (ipprintfs)
1386 		printf(" %lx\n", (u_long)ntohl(q->s_addr));
1387 #endif
1388 	return (m);
1389 }
1390 
1391 /*
1392  * Strip out IP options, at higher
1393  * level protocol in the kernel.
1394  * Second argument is buffer to which options
1395  * will be moved, and return value is their length.
1396  * XXX should be deleted; last arg currently ignored.
1397  */
1398 void
1399 ip_stripoptions(m, mopt)
1400 	register struct mbuf *m;
1401 	struct mbuf *mopt;
1402 {
1403 	register int i;
1404 	struct ip *ip = mtod(m, struct ip *);
1405 	register caddr_t opts;
1406 	int olen;
1407 
1408 	olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1409 	opts = (caddr_t)(ip + 1);
1410 	i = m->m_len - (sizeof (struct ip) + olen);
1411 	bcopy(opts + olen, opts, (unsigned)i);
1412 	m->m_len -= olen;
1413 	if (m->m_flags & M_PKTHDR)
1414 		m->m_pkthdr.len -= olen;
1415 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1416 }
1417 
1418 u_char inetctlerrmap[PRC_NCMDS] = {
1419 	0,		0,		0,		0,
1420 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1421 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1422 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1423 	0,		0,		0,		0,
1424 	ENOPROTOOPT
1425 };
1426 
1427 /*
1428  * Forward a packet.  If some error occurs return the sender
1429  * an icmp packet.  Note we can't always generate a meaningful
1430  * icmp message because icmp doesn't have a large enough repertoire
1431  * of codes and types.
1432  *
1433  * If not forwarding, just drop the packet.  This could be confusing
1434  * if ipforwarding was zero but some routing protocol was advancing
1435  * us as a gateway to somewhere.  However, we must let the routing
1436  * protocol deal with that.
1437  *
1438  * The srcrt parameter indicates whether the packet is being forwarded
1439  * via a source route.
1440  */
1441 static void
1442 ip_forward(m, srcrt)
1443 	struct mbuf *m;
1444 	int srcrt;
1445 {
1446 	register struct ip *ip = mtod(m, struct ip *);
1447 	register struct sockaddr_in *sin;
1448 	register struct rtentry *rt;
1449 	int error, type = 0, code = 0;
1450 	struct mbuf *mcopy;
1451 	n_long dest;
1452 	struct ifnet *destifp;
1453 #ifdef IPSEC
1454 	struct ifnet dummyifp;
1455 #endif
1456 
1457 	dest = 0;
1458 #ifdef DIAGNOSTIC
1459 	if (ipprintfs)
1460 		printf("forward: src %lx dst %lx ttl %x\n",
1461 		    (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
1462 		    ip->ip_ttl);
1463 #endif
1464 
1465 
1466 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1467 		ipstat.ips_cantforward++;
1468 		m_freem(m);
1469 		return;
1470 	}
1471 #ifdef IPSTEALTH
1472 	if (!ipstealth) {
1473 #endif
1474 		if (ip->ip_ttl <= IPTTLDEC) {
1475 			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1476 			    dest, 0);
1477 			return;
1478 		}
1479 #ifdef IPSTEALTH
1480 	}
1481 #endif
1482 
1483 	sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
1484 	if ((rt = ipforward_rt.ro_rt) == 0 ||
1485 	    ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
1486 		if (ipforward_rt.ro_rt) {
1487 			RTFREE(ipforward_rt.ro_rt);
1488 			ipforward_rt.ro_rt = 0;
1489 		}
1490 		sin->sin_family = AF_INET;
1491 		sin->sin_len = sizeof(*sin);
1492 		sin->sin_addr = ip->ip_dst;
1493 
1494 		rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1495 		if (ipforward_rt.ro_rt == 0) {
1496 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1497 			return;
1498 		}
1499 		rt = ipforward_rt.ro_rt;
1500 	}
1501 
1502 	/*
1503 	 * Save at most 64 bytes of the packet in case
1504 	 * we need to generate an ICMP message to the src.
1505 	 */
1506 	mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
1507 	if (mcopy && (mcopy->m_flags & M_EXT))
1508 		m_copydata(mcopy, 0, sizeof(struct ip), mtod(mcopy, caddr_t));
1509 
1510 #ifdef IPSTEALTH
1511 	if (!ipstealth) {
1512 #endif
1513 		ip->ip_ttl -= IPTTLDEC;
1514 #ifdef IPSTEALTH
1515 	}
1516 #endif
1517 
1518 	/*
1519 	 * If forwarding packet using same interface that it came in on,
1520 	 * perhaps should send a redirect to sender to shortcut a hop.
1521 	 * Only send redirect if source is sending directly to us,
1522 	 * and if packet was not source routed (or has any options).
1523 	 * Also, don't send redirect if forwarding using a default route
1524 	 * or a route modified by a redirect.
1525 	 */
1526 #define	satosin(sa)	((struct sockaddr_in *)(sa))
1527 	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1528 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1529 	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1530 	    ipsendredirects && !srcrt) {
1531 #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1532 		u_long src = ntohl(ip->ip_src.s_addr);
1533 
1534 		if (RTA(rt) &&
1535 		    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1536 		    if (rt->rt_flags & RTF_GATEWAY)
1537 			dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1538 		    else
1539 			dest = ip->ip_dst.s_addr;
1540 		    /* Router requirements says to only send host redirects */
1541 		    type = ICMP_REDIRECT;
1542 		    code = ICMP_REDIRECT_HOST;
1543 #ifdef DIAGNOSTIC
1544 		    if (ipprintfs)
1545 		        printf("redirect (%d) to %lx\n", code, (u_long)dest);
1546 #endif
1547 		}
1548 	}
1549 
1550 	error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1551 			  IP_FORWARDING, 0);
1552 	if (error)
1553 		ipstat.ips_cantforward++;
1554 	else {
1555 		ipstat.ips_forward++;
1556 		if (type)
1557 			ipstat.ips_redirectsent++;
1558 		else {
1559 			if (mcopy) {
1560 				ipflow_create(&ipforward_rt, mcopy);
1561 				m_freem(mcopy);
1562 			}
1563 			return;
1564 		}
1565 	}
1566 	if (mcopy == NULL)
1567 		return;
1568 	destifp = NULL;
1569 
1570 	switch (error) {
1571 
1572 	case 0:				/* forwarded, but need redirect */
1573 		/* type, code set above */
1574 		break;
1575 
1576 	case ENETUNREACH:		/* shouldn't happen, checked above */
1577 	case EHOSTUNREACH:
1578 	case ENETDOWN:
1579 	case EHOSTDOWN:
1580 	default:
1581 		type = ICMP_UNREACH;
1582 		code = ICMP_UNREACH_HOST;
1583 		break;
1584 
1585 	case EMSGSIZE:
1586 		type = ICMP_UNREACH;
1587 		code = ICMP_UNREACH_NEEDFRAG;
1588 #ifndef IPSEC
1589 		if (ipforward_rt.ro_rt)
1590 			destifp = ipforward_rt.ro_rt->rt_ifp;
1591 #else
1592 		/*
1593 		 * If the packet is routed over IPsec tunnel, tell the
1594 		 * originator the tunnel MTU.
1595 		 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1596 		 * XXX quickhack!!!
1597 		 */
1598 		if (ipforward_rt.ro_rt) {
1599 			struct secpolicy *sp = NULL;
1600 			int ipsecerror;
1601 			int ipsechdr;
1602 			struct route *ro;
1603 
1604 			sp = ipsec4_getpolicybyaddr(mcopy,
1605 						    IPSEC_DIR_OUTBOUND,
1606 			                            IP_FORWARDING,
1607 			                            &ipsecerror);
1608 
1609 			if (sp == NULL)
1610 				destifp = ipforward_rt.ro_rt->rt_ifp;
1611 			else {
1612 				/* count IPsec header size */
1613 				ipsechdr = ipsec4_hdrsiz(mcopy,
1614 							 IPSEC_DIR_OUTBOUND,
1615 							 NULL);
1616 
1617 				/*
1618 				 * find the correct route for outer IPv4
1619 				 * header, compute tunnel MTU.
1620 				 *
1621 				 * XXX BUG ALERT
1622 				 * The "dummyifp" code relies upon the fact
1623 				 * that icmp_error() touches only ifp->if_mtu.
1624 				 */
1625 				/*XXX*/
1626 				destifp = NULL;
1627 				if (sp->req != NULL
1628 				 && sp->req->sav != NULL
1629 				 && sp->req->sav->sah != NULL) {
1630 					ro = &sp->req->sav->sah->sa_route;
1631 					if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1632 						dummyifp.if_mtu =
1633 						    ro->ro_rt->rt_ifp->if_mtu;
1634 						dummyifp.if_mtu -= ipsechdr;
1635 						destifp = &dummyifp;
1636 					}
1637 				}
1638 
1639 				key_freesp(sp);
1640 			}
1641 		}
1642 #endif /*IPSEC*/
1643 		ipstat.ips_cantfrag++;
1644 		break;
1645 
1646 	case ENOBUFS:
1647 		type = ICMP_SOURCEQUENCH;
1648 		code = 0;
1649 		break;
1650 
1651 	case EACCES:			/* ipfw denied packet */
1652 		m_freem(mcopy);
1653 		return;
1654 	}
1655 	if (mcopy->m_flags & M_EXT)
1656 		m_copyback(mcopy, 0, sizeof(struct ip), mtod(mcopy, caddr_t));
1657 	icmp_error(mcopy, type, code, dest, destifp);
1658 }
1659 
1660 void
1661 ip_savecontrol(inp, mp, ip, m)
1662 	register struct inpcb *inp;
1663 	register struct mbuf **mp;
1664 	register struct ip *ip;
1665 	register struct mbuf *m;
1666 {
1667 	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1668 		struct timeval tv;
1669 
1670 		microtime(&tv);
1671 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1672 			SCM_TIMESTAMP, SOL_SOCKET);
1673 		if (*mp)
1674 			mp = &(*mp)->m_next;
1675 	}
1676 	if (inp->inp_flags & INP_RECVDSTADDR) {
1677 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1678 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1679 		if (*mp)
1680 			mp = &(*mp)->m_next;
1681 	}
1682 #ifdef notyet
1683 	/* XXX
1684 	 * Moving these out of udp_input() made them even more broken
1685 	 * than they already were.
1686 	 */
1687 	/* options were tossed already */
1688 	if (inp->inp_flags & INP_RECVOPTS) {
1689 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1690 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1691 		if (*mp)
1692 			mp = &(*mp)->m_next;
1693 	}
1694 	/* ip_srcroute doesn't do what we want here, need to fix */
1695 	if (inp->inp_flags & INP_RECVRETOPTS) {
1696 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1697 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1698 		if (*mp)
1699 			mp = &(*mp)->m_next;
1700 	}
1701 #endif
1702 	if (inp->inp_flags & INP_RECVIF) {
1703 		struct ifnet *ifp;
1704 		struct sdlbuf {
1705 			struct sockaddr_dl sdl;
1706 			u_char	pad[32];
1707 		} sdlbuf;
1708 		struct sockaddr_dl *sdp;
1709 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1710 
1711 		if (((ifp = m->m_pkthdr.rcvif))
1712 		&& ( ifp->if_index && (ifp->if_index <= if_index))) {
1713 			sdp = (struct sockaddr_dl *)(ifnet_addrs
1714 					[ifp->if_index - 1]->ifa_addr);
1715 			/*
1716 			 * Change our mind and don't try copy.
1717 			 */
1718 			if ((sdp->sdl_family != AF_LINK)
1719 			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1720 				goto makedummy;
1721 			}
1722 			bcopy(sdp, sdl2, sdp->sdl_len);
1723 		} else {
1724 makedummy:
1725 			sdl2->sdl_len
1726 				= offsetof(struct sockaddr_dl, sdl_data[0]);
1727 			sdl2->sdl_family = AF_LINK;
1728 			sdl2->sdl_index = 0;
1729 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1730 		}
1731 		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
1732 			IP_RECVIF, IPPROTO_IP);
1733 		if (*mp)
1734 			mp = &(*mp)->m_next;
1735 	}
1736 }
1737 
1738 int
1739 ip_rsvp_init(struct socket *so)
1740 {
1741 	if (so->so_type != SOCK_RAW ||
1742 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1743 	  return EOPNOTSUPP;
1744 
1745 	if (ip_rsvpd != NULL)
1746 	  return EADDRINUSE;
1747 
1748 	ip_rsvpd = so;
1749 	/*
1750 	 * This may seem silly, but we need to be sure we don't over-increment
1751 	 * the RSVP counter, in case something slips up.
1752 	 */
1753 	if (!ip_rsvp_on) {
1754 		ip_rsvp_on = 1;
1755 		rsvp_on++;
1756 	}
1757 
1758 	return 0;
1759 }
1760 
1761 int
1762 ip_rsvp_done(void)
1763 {
1764 	ip_rsvpd = NULL;
1765 	/*
1766 	 * This may seem silly, but we need to be sure we don't over-decrement
1767 	 * the RSVP counter, in case something slips up.
1768 	 */
1769 	if (ip_rsvp_on) {
1770 		ip_rsvp_on = 0;
1771 		rsvp_on--;
1772 	}
1773 	return 0;
1774 }
1775