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