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