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