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