xref: /freebsd/sys/netinet/ip_output.c (revision 33b77e2decd50e53798014b70bf7ca3bdc4c0c7e)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1990, 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_output.c	8.3 (Berkeley) 1/21/94
34  *	$Id: ip_output.c,v 1.61 1997/11/05 20:17:23 joerg Exp $
35  */
36 
37 #define _IP_VHL
38 
39 #include "opt_ipdivert.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 
49 #include <net/if.h>
50 #include <net/route.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/in_var.h>
57 #include <netinet/ip_var.h>
58 
59 #ifdef vax
60 #include <machine/mtpr.h>
61 #endif
62 #include <machine/in_cksum.h>
63 
64 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
65 
66 #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
67 #undef COMPAT_IPFW
68 #define COMPAT_IPFW 1
69 #else
70 #undef COMPAT_IPFW
71 #endif
72 
73 u_short ip_id;
74 
75 static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
76 static void	ip_mloopback
77 	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *, int));
78 static int	ip_getmoptions
79 	__P((int, struct ip_moptions *, struct mbuf **));
80 static int	ip_pcbopts __P((struct mbuf **, struct mbuf *));
81 static int	ip_setmoptions
82 	__P((int, struct ip_moptions **, struct mbuf *));
83 
84 #if defined(IPFILTER_LKM) || defined(IPFILTER)
85 int	ip_optcopy __P((struct ip *, struct ip *));
86 extern int fr_check __P((struct ip *, int, struct ifnet *, int, struct mbuf **));
87 extern int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **));
88 #else
89 static int	ip_optcopy __P((struct ip *, struct ip *));
90 #endif
91 
92 
93 extern	struct protosw inetsw[];
94 
95 /*
96  * IP output.  The packet in mbuf chain m contains a skeletal IP
97  * header (with len, off, ttl, proto, tos, src, dst).
98  * The mbuf chain containing the packet will be freed.
99  * The mbuf opt, if present, will not be freed.
100  */
101 int
102 ip_output(m0, opt, ro, flags, imo)
103 	struct mbuf *m0;
104 	struct mbuf *opt;
105 	struct route *ro;
106 	int flags;
107 	struct ip_moptions *imo;
108 {
109 	struct ip *ip, *mhip;
110 	struct ifnet *ifp;
111 	struct mbuf *m = m0;
112 	int hlen = sizeof (struct ip);
113 	int len, off, error = 0;
114 	struct sockaddr_in *dst;
115 	struct in_ifaddr *ia;
116 	int isbroadcast;
117 
118 #ifdef	DIAGNOSTIC
119 	if ((m->m_flags & M_PKTHDR) == 0)
120 		panic("ip_output no HDR");
121 	if (!ro)
122 		panic("ip_output no route, proto = %d",
123 		      mtod(m, struct ip *)->ip_p);
124 #endif
125 	if (opt) {
126 		m = ip_insertoptions(m, opt, &len);
127 		hlen = len;
128 	}
129 	ip = mtod(m, struct ip *);
130 	/*
131 	 * Fill in IP header.
132 	 */
133 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
134 		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
135 		ip->ip_off &= IP_DF;
136 		ip->ip_id = htons(ip_id++);
137 		ipstat.ips_localout++;
138 	} else {
139 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
140 	}
141 
142 	dst = (struct sockaddr_in *)&ro->ro_dst;
143 	/*
144 	 * If there is a cached route,
145 	 * check that it is to the same destination
146 	 * and is still up.  If not, free it and try again.
147 	 */
148 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
149 	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
150 		RTFREE(ro->ro_rt);
151 		ro->ro_rt = (struct rtentry *)0;
152 	}
153 	if (ro->ro_rt == 0) {
154 		dst->sin_family = AF_INET;
155 		dst->sin_len = sizeof(*dst);
156 		dst->sin_addr = ip->ip_dst;
157 	}
158 	/*
159 	 * If routing to interface only,
160 	 * short circuit routing lookup.
161 	 */
162 #define ifatoia(ifa)	((struct in_ifaddr *)(ifa))
163 #define sintosa(sin)	((struct sockaddr *)(sin))
164 	if (flags & IP_ROUTETOIF) {
165 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
166 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
167 			ipstat.ips_noroute++;
168 			error = ENETUNREACH;
169 			goto bad;
170 		}
171 		ifp = ia->ia_ifp;
172 		ip->ip_ttl = 1;
173 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
174 	} else {
175 		/*
176 		 * If this is the case, we probably don't want to allocate
177 		 * a protocol-cloned route since we didn't get one from the
178 		 * ULP.  This lets TCP do its thing, while not burdening
179 		 * forwarding or ICMP with the overhead of cloning a route.
180 		 * Of course, we still want to do any cloning requested by
181 		 * the link layer, as this is probably required in all cases
182 		 * for correct operation (as it is for ARP).
183 		 */
184 		if (ro->ro_rt == 0)
185 			rtalloc_ign(ro, RTF_PRCLONING);
186 		if (ro->ro_rt == 0) {
187 			ipstat.ips_noroute++;
188 			error = EHOSTUNREACH;
189 			goto bad;
190 		}
191 		ia = ifatoia(ro->ro_rt->rt_ifa);
192 		ifp = ro->ro_rt->rt_ifp;
193 		ro->ro_rt->rt_use++;
194 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
195 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
196 		if (ro->ro_rt->rt_flags & RTF_HOST)
197 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
198 		else
199 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
200 	}
201 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
202 		struct in_multi *inm;
203 
204 		m->m_flags |= M_MCAST;
205 		/*
206 		 * IP destination address is multicast.  Make sure "dst"
207 		 * still points to the address in "ro".  (It may have been
208 		 * changed to point to a gateway address, above.)
209 		 */
210 		dst = (struct sockaddr_in *)&ro->ro_dst;
211 		/*
212 		 * See if the caller provided any multicast options
213 		 */
214 		if (imo != NULL) {
215 			ip->ip_ttl = imo->imo_multicast_ttl;
216 			if (imo->imo_multicast_ifp != NULL)
217 				ifp = imo->imo_multicast_ifp;
218 			if (imo->imo_multicast_vif != -1)
219 				ip->ip_src.s_addr =
220 				    ip_mcast_src(imo->imo_multicast_vif);
221 		} else
222 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
223 		/*
224 		 * Confirm that the outgoing interface supports multicast.
225 		 */
226 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
227 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
228 				ipstat.ips_noroute++;
229 				error = ENETUNREACH;
230 				goto bad;
231 			}
232 		}
233 		/*
234 		 * If source address not specified yet, use address
235 		 * of outgoing interface.
236 		 */
237 		if (ip->ip_src.s_addr == INADDR_ANY) {
238 			register struct in_ifaddr *ia1;
239 
240 			for (ia1 = in_ifaddrhead.tqh_first; ia1;
241 			     ia1 = ia1->ia_link.tqe_next)
242 				if (ia1->ia_ifp == ifp) {
243 					ip->ip_src = IA_SIN(ia1)->sin_addr;
244 					break;
245 				}
246 		}
247 
248 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
249 		if (inm != NULL &&
250 		   (imo == NULL || imo->imo_multicast_loop)) {
251 			/*
252 			 * If we belong to the destination multicast group
253 			 * on the outgoing interface, and the caller did not
254 			 * forbid loopback, loop back a copy.
255 			 */
256 			ip_mloopback(ifp, m, dst, hlen);
257 		}
258 		else {
259 			/*
260 			 * If we are acting as a multicast router, perform
261 			 * multicast forwarding as if the packet had just
262 			 * arrived on the interface to which we are about
263 			 * to send.  The multicast forwarding function
264 			 * recursively calls this function, using the
265 			 * IP_FORWARDING flag to prevent infinite recursion.
266 			 *
267 			 * Multicasts that are looped back by ip_mloopback(),
268 			 * above, will be forwarded by the ip_input() routine,
269 			 * if necessary.
270 			 */
271 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
272 				/*
273 				 * Check if rsvp daemon is running. If not, don't
274 				 * set ip_moptions. This ensures that the packet
275 				 * is multicast and not just sent down one link
276 				 * as prescribed by rsvpd.
277 				 */
278 				if (!rsvp_on)
279 				  imo = NULL;
280 				if (ip_mforward(ip, ifp, m, imo) != 0) {
281 					m_freem(m);
282 					goto done;
283 				}
284 			}
285 		}
286 
287 		/*
288 		 * Multicasts with a time-to-live of zero may be looped-
289 		 * back, above, but must not be transmitted on a network.
290 		 * Also, multicasts addressed to the loopback interface
291 		 * are not sent -- the above call to ip_mloopback() will
292 		 * loop back a copy if this host actually belongs to the
293 		 * destination group on the loopback interface.
294 		 */
295 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
296 			m_freem(m);
297 			goto done;
298 		}
299 
300 		goto sendit;
301 	}
302 #ifndef notdef
303 	/*
304 	 * If source address not specified yet, use address
305 	 * of outgoing interface.
306 	 */
307 	if (ip->ip_src.s_addr == INADDR_ANY)
308 		ip->ip_src = IA_SIN(ia)->sin_addr;
309 #endif
310 	/*
311 	 * Verify that we have any chance at all of being able to queue
312 	 *      the packet or packet fragments
313 	 */
314 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
315 		ifp->if_snd.ifq_maxlen) {
316 			error = ENOBUFS;
317 			goto bad;
318 	}
319 
320 	/*
321 	 * Look for broadcast address and
322 	 * and verify user is allowed to send
323 	 * such a packet.
324 	 */
325 	if (isbroadcast) {
326 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
327 			error = EADDRNOTAVAIL;
328 			goto bad;
329 		}
330 		if ((flags & IP_ALLOWBROADCAST) == 0) {
331 			error = EACCES;
332 			goto bad;
333 		}
334 		/* don't allow broadcast messages to be fragmented */
335 		if ((u_short)ip->ip_len > ifp->if_mtu) {
336 			error = EMSGSIZE;
337 			goto bad;
338 		}
339 		m->m_flags |= M_BCAST;
340 	} else {
341 		m->m_flags &= ~M_BCAST;
342 	}
343 
344 sendit:
345 #if defined(IPFILTER) || defined(IPFILTER_LKM)
346 	/*
347 	 * looks like most checking has been done now...do a filter check
348 	 */
349 	if (fr_checkp) {
350 		struct  mbuf    *m1 = m;
351 
352 		if ((*fr_checkp)(ip, hlen, ifp, 1, &m1))
353 			error = EHOSTUNREACH;
354 		if (error || !m1)
355 			goto done;
356 		ip = mtod(m = m1, struct ip *);
357 	}
358 #endif
359         /*
360 	 * IpHack's section.
361 	 * - Xlate: translate packet's addr/port (NAT).
362 	 * - Firewall: deny/allow/etc.
363 	 * - Wrap: fake packet's addr/port <unimpl.>
364 	 * - Encapsulate: put it in another IP and send out. <unimp.>
365 	 */
366 
367 #ifdef COMPAT_IPFW
368         if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, ifp, IP_NAT_OUT)) {
369 		error = EACCES;
370 		goto done;
371 	}
372 
373 	/*
374 	 * Check with the firewall...
375 	 */
376 	if (ip_fw_chk_ptr) {
377 #ifdef IPDIVERT
378 		ip_divert_port = (*ip_fw_chk_ptr)(&ip,
379 		    hlen, ifp, ip_divert_ignore, &m);
380 		ip_divert_ignore = 0;
381 		if (ip_divert_port) {		/* Divert packet */
382 			(*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, 0);
383 			goto done;
384 		}
385 #else
386 		/* If ipfw says divert, we have to just drop packet */
387 		if ((*ip_fw_chk_ptr)(&ip, hlen, ifp, 0, &m)) {
388 			m_freem(m);
389 			goto done;
390 		}
391 #endif
392 		if (!m) {
393 			error = EACCES;
394 			goto done;
395 		}
396 	}
397 #endif /* COMPAT_IPFW */
398 
399 	/*
400 	 * If small enough for interface, can just send directly.
401 	 */
402 	if ((u_short)ip->ip_len <= ifp->if_mtu) {
403 		ip->ip_len = htons((u_short)ip->ip_len);
404 		ip->ip_off = htons((u_short)ip->ip_off);
405 		ip->ip_sum = 0;
406 		if (ip->ip_vhl == IP_VHL_BORING) {
407 			ip->ip_sum = in_cksum_hdr(ip);
408 		} else {
409 			ip->ip_sum = in_cksum(m, hlen);
410 		}
411 		error = (*ifp->if_output)(ifp, m,
412 				(struct sockaddr *)dst, ro->ro_rt);
413 		goto done;
414 	}
415 	/*
416 	 * Too large for interface; fragment if possible.
417 	 * Must be able to put at least 8 bytes per fragment.
418 	 */
419 	if (ip->ip_off & IP_DF) {
420 		error = EMSGSIZE;
421 		/*
422 		 * This case can happen if the user changed the MTU
423 		 * of an interface after enabling IP on it.  Because
424 		 * most netifs don't keep track of routes pointing to
425 		 * them, there is no way for one to update all its
426 		 * routes when the MTU is changed.
427 		 */
428 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
429 		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
430 		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
431 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
432 		}
433 		ipstat.ips_cantfrag++;
434 		goto bad;
435 	}
436 	len = (ifp->if_mtu - hlen) &~ 7;
437 	if (len < 8) {
438 		error = EMSGSIZE;
439 		goto bad;
440 	}
441 
442     {
443 	int mhlen, firstlen = len;
444 	struct mbuf **mnext = &m->m_nextpkt;
445 
446 	/*
447 	 * Loop through length of segment after first fragment,
448 	 * make new header and copy data of each part and link onto chain.
449 	 */
450 	m0 = m;
451 	mhlen = sizeof (struct ip);
452 	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
453 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
454 		if (m == 0) {
455 			error = ENOBUFS;
456 			ipstat.ips_odropped++;
457 			goto sendorfree;
458 		}
459 		m->m_data += max_linkhdr;
460 		mhip = mtod(m, struct ip *);
461 		*mhip = *ip;
462 		if (hlen > sizeof (struct ip)) {
463 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
464 			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
465 		}
466 		m->m_len = mhlen;
467 		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
468 		if (ip->ip_off & IP_MF)
469 			mhip->ip_off |= IP_MF;
470 		if (off + len >= (u_short)ip->ip_len)
471 			len = (u_short)ip->ip_len - off;
472 		else
473 			mhip->ip_off |= IP_MF;
474 		mhip->ip_len = htons((u_short)(len + mhlen));
475 		m->m_next = m_copy(m0, off, len);
476 		if (m->m_next == 0) {
477 			(void) m_free(m);
478 			error = ENOBUFS;	/* ??? */
479 			ipstat.ips_odropped++;
480 			goto sendorfree;
481 		}
482 		m->m_pkthdr.len = mhlen + len;
483 		m->m_pkthdr.rcvif = (struct ifnet *)0;
484 		mhip->ip_off = htons((u_short)mhip->ip_off);
485 		mhip->ip_sum = 0;
486 		if (mhip->ip_vhl == IP_VHL_BORING) {
487 			mhip->ip_sum = in_cksum_hdr(mhip);
488 		} else {
489 			mhip->ip_sum = in_cksum(m, mhlen);
490 		}
491 		*mnext = m;
492 		mnext = &m->m_nextpkt;
493 		ipstat.ips_ofragments++;
494 	}
495 	/*
496 	 * Update first fragment by trimming what's been copied out
497 	 * and updating header, then send each fragment (in order).
498 	 */
499 	m = m0;
500 	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
501 	m->m_pkthdr.len = hlen + firstlen;
502 	ip->ip_len = htons((u_short)m->m_pkthdr.len);
503 	ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
504 	ip->ip_sum = 0;
505 	if (ip->ip_vhl == IP_VHL_BORING) {
506 		ip->ip_sum = in_cksum_hdr(ip);
507 	} else {
508 		ip->ip_sum = in_cksum(m, hlen);
509 	}
510 sendorfree:
511 	for (m = m0; m; m = m0) {
512 		m0 = m->m_nextpkt;
513 		m->m_nextpkt = 0;
514 		if (error == 0)
515 			error = (*ifp->if_output)(ifp, m,
516 			    (struct sockaddr *)dst, ro->ro_rt);
517 		else
518 			m_freem(m);
519 	}
520 
521 	if (error == 0)
522 		ipstat.ips_fragmented++;
523     }
524 done:
525 	return (error);
526 bad:
527 	m_freem(m0);
528 	goto done;
529 }
530 
531 /*
532  * Insert IP options into preformed packet.
533  * Adjust IP destination as required for IP source routing,
534  * as indicated by a non-zero in_addr at the start of the options.
535  *
536  * XXX This routine assumes that the packet has no options in place.
537  */
538 static struct mbuf *
539 ip_insertoptions(m, opt, phlen)
540 	register struct mbuf *m;
541 	struct mbuf *opt;
542 	int *phlen;
543 {
544 	register struct ipoption *p = mtod(opt, struct ipoption *);
545 	struct mbuf *n;
546 	register struct ip *ip = mtod(m, struct ip *);
547 	unsigned optlen;
548 
549 	optlen = opt->m_len - sizeof(p->ipopt_dst);
550 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
551 		return (m);		/* XXX should fail */
552 	if (p->ipopt_dst.s_addr)
553 		ip->ip_dst = p->ipopt_dst;
554 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
555 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
556 		if (n == 0)
557 			return (m);
558 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
559 		m->m_len -= sizeof(struct ip);
560 		m->m_data += sizeof(struct ip);
561 		n->m_next = m;
562 		m = n;
563 		m->m_len = optlen + sizeof(struct ip);
564 		m->m_data += max_linkhdr;
565 		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
566 	} else {
567 		m->m_data -= optlen;
568 		m->m_len += optlen;
569 		m->m_pkthdr.len += optlen;
570 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
571 	}
572 	ip = mtod(m, struct ip *);
573 	bcopy(p->ipopt_list, ip + 1, optlen);
574 	*phlen = sizeof(struct ip) + optlen;
575 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
576 	ip->ip_len += optlen;
577 	return (m);
578 }
579 
580 /*
581  * Copy options from ip to jp,
582  * omitting those not copied during fragmentation.
583  */
584 #if !defined(IPFILTER) && !defined(IPFILTER_LKM)
585 static
586 #endif
587 int
588 ip_optcopy(ip, jp)
589 	struct ip *ip, *jp;
590 {
591 	register u_char *cp, *dp;
592 	int opt, optlen, cnt;
593 
594 	cp = (u_char *)(ip + 1);
595 	dp = (u_char *)(jp + 1);
596 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
597 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
598 		opt = cp[0];
599 		if (opt == IPOPT_EOL)
600 			break;
601 		if (opt == IPOPT_NOP) {
602 			/* Preserve for IP mcast tunnel's LSRR alignment. */
603 			*dp++ = IPOPT_NOP;
604 			optlen = 1;
605 			continue;
606 		} else
607 			optlen = cp[IPOPT_OLEN];
608 		/* bogus lengths should have been caught by ip_dooptions */
609 		if (optlen > cnt)
610 			optlen = cnt;
611 		if (IPOPT_COPIED(opt)) {
612 			bcopy(cp, dp, optlen);
613 			dp += optlen;
614 		}
615 	}
616 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
617 		*dp++ = IPOPT_EOL;
618 	return (optlen);
619 }
620 
621 /*
622  * IP socket option processing.
623  */
624 int
625 ip_ctloutput(op, so, level, optname, mp, p)
626 	int op;
627 	struct socket *so;
628 	int level, optname;
629 	struct mbuf **mp;
630 	struct proc *p;
631 {
632 	register struct inpcb *inp = sotoinpcb(so);
633 	register struct mbuf *m = *mp;
634 	register int optval = 0;
635 	int error = 0;
636 
637 	if (level != IPPROTO_IP) {
638 		error = EINVAL;
639 		if (op == PRCO_SETOPT && *mp)
640 			(void) m_free(*mp);
641 	} else switch (op) {
642 
643 	case PRCO_SETOPT:
644 		switch (optname) {
645 		case IP_OPTIONS:
646 #ifdef notyet
647 		case IP_RETOPTS:
648 			return (ip_pcbopts(optname, &inp->inp_options, m));
649 #else
650 			return (ip_pcbopts(&inp->inp_options, m));
651 #endif
652 
653 		case IP_TOS:
654 		case IP_TTL:
655 		case IP_RECVOPTS:
656 		case IP_RECVRETOPTS:
657 		case IP_RECVDSTADDR:
658 		case IP_RECVIF:
659 			if (m == 0 || m->m_len != sizeof(int))
660 				error = EINVAL;
661 			else {
662 				optval = *mtod(m, int *);
663 				switch (optname) {
664 
665 				case IP_TOS:
666 					inp->inp_ip_tos = optval;
667 					break;
668 
669 				case IP_TTL:
670 					inp->inp_ip_ttl = optval;
671 					break;
672 #define	OPTSET(bit) \
673 	if (optval) \
674 		inp->inp_flags |= bit; \
675 	else \
676 		inp->inp_flags &= ~bit;
677 
678 				case IP_RECVOPTS:
679 					OPTSET(INP_RECVOPTS);
680 					break;
681 
682 				case IP_RECVRETOPTS:
683 					OPTSET(INP_RECVRETOPTS);
684 					break;
685 
686 				case IP_RECVDSTADDR:
687 					OPTSET(INP_RECVDSTADDR);
688 					break;
689 
690 				case IP_RECVIF:
691 					OPTSET(INP_RECVIF);
692 					break;
693 				}
694 			}
695 			break;
696 #undef OPTSET
697 
698 		case IP_MULTICAST_IF:
699 		case IP_MULTICAST_VIF:
700 		case IP_MULTICAST_TTL:
701 		case IP_MULTICAST_LOOP:
702 		case IP_ADD_MEMBERSHIP:
703 		case IP_DROP_MEMBERSHIP:
704 			error = ip_setmoptions(optname, &inp->inp_moptions, m);
705 			break;
706 
707 		case IP_PORTRANGE:
708 			if (m == 0 || m->m_len != sizeof(int))
709 				error = EINVAL;
710 			else {
711 				optval = *mtod(m, int *);
712 
713 				switch (optval) {
714 
715 				case IP_PORTRANGE_DEFAULT:
716 					inp->inp_flags &= ~(INP_LOWPORT);
717 					inp->inp_flags &= ~(INP_HIGHPORT);
718 					break;
719 
720 				case IP_PORTRANGE_HIGH:
721 					inp->inp_flags &= ~(INP_LOWPORT);
722 					inp->inp_flags |= INP_HIGHPORT;
723 					break;
724 
725 				case IP_PORTRANGE_LOW:
726 					inp->inp_flags &= ~(INP_HIGHPORT);
727 					inp->inp_flags |= INP_LOWPORT;
728 					break;
729 
730 				default:
731 					error = EINVAL;
732 					break;
733 				}
734 			}
735 			break;
736 
737 		default:
738 			error = ENOPROTOOPT;
739 			break;
740 		}
741 		if (m)
742 			(void)m_free(m);
743 		break;
744 
745 	case PRCO_GETOPT:
746 		switch (optname) {
747 		case IP_OPTIONS:
748 		case IP_RETOPTS:
749 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
750 			if (inp->inp_options) {
751 				m->m_len = inp->inp_options->m_len;
752 				bcopy(mtod(inp->inp_options, void *),
753 				    mtod(m, void *), m->m_len);
754 			} else
755 				m->m_len = 0;
756 			break;
757 
758 		case IP_TOS:
759 		case IP_TTL:
760 		case IP_RECVOPTS:
761 		case IP_RECVRETOPTS:
762 		case IP_RECVDSTADDR:
763 		case IP_RECVIF:
764 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
765 			m->m_len = sizeof(int);
766 			switch (optname) {
767 
768 			case IP_TOS:
769 				optval = inp->inp_ip_tos;
770 				break;
771 
772 			case IP_TTL:
773 				optval = inp->inp_ip_ttl;
774 				break;
775 
776 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
777 
778 			case IP_RECVOPTS:
779 				optval = OPTBIT(INP_RECVOPTS);
780 				break;
781 
782 			case IP_RECVRETOPTS:
783 				optval = OPTBIT(INP_RECVRETOPTS);
784 				break;
785 
786 			case IP_RECVDSTADDR:
787 				optval = OPTBIT(INP_RECVDSTADDR);
788 				break;
789 
790 			case IP_RECVIF:
791 				optval = OPTBIT(INP_RECVIF);
792 				break;
793 			}
794 			*mtod(m, int *) = optval;
795 			break;
796 
797 		case IP_MULTICAST_IF:
798 		case IP_MULTICAST_VIF:
799 		case IP_MULTICAST_TTL:
800 		case IP_MULTICAST_LOOP:
801 		case IP_ADD_MEMBERSHIP:
802 		case IP_DROP_MEMBERSHIP:
803 			error = ip_getmoptions(optname, inp->inp_moptions, mp);
804 			break;
805 
806 		case IP_PORTRANGE:
807 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
808 			m->m_len = sizeof(int);
809 
810 			if (inp->inp_flags & INP_HIGHPORT)
811 				optval = IP_PORTRANGE_HIGH;
812 			else if (inp->inp_flags & INP_LOWPORT)
813 				optval = IP_PORTRANGE_LOW;
814 			else
815 				optval = 0;
816 
817 			*mtod(m, int *) = optval;
818 			break;
819 
820 		default:
821 			error = ENOPROTOOPT;
822 			break;
823 		}
824 		break;
825 	}
826 	return (error);
827 }
828 
829 /*
830  * Set up IP options in pcb for insertion in output packets.
831  * Store in mbuf with pointer in pcbopt, adding pseudo-option
832  * with destination address if source routed.
833  */
834 static int
835 #ifdef notyet
836 ip_pcbopts(optname, pcbopt, m)
837 	int optname;
838 #else
839 ip_pcbopts(pcbopt, m)
840 #endif
841 	struct mbuf **pcbopt;
842 	register struct mbuf *m;
843 {
844 	register cnt, optlen;
845 	register u_char *cp;
846 	u_char opt;
847 
848 	/* turn off any old options */
849 	if (*pcbopt)
850 		(void)m_free(*pcbopt);
851 	*pcbopt = 0;
852 	if (m == (struct mbuf *)0 || m->m_len == 0) {
853 		/*
854 		 * Only turning off any previous options.
855 		 */
856 		if (m)
857 			(void)m_free(m);
858 		return (0);
859 	}
860 
861 #ifndef	vax
862 	if (m->m_len % sizeof(long))
863 		goto bad;
864 #endif
865 	/*
866 	 * IP first-hop destination address will be stored before
867 	 * actual options; move other options back
868 	 * and clear it when none present.
869 	 */
870 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
871 		goto bad;
872 	cnt = m->m_len;
873 	m->m_len += sizeof(struct in_addr);
874 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
875 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
876 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
877 
878 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
879 		opt = cp[IPOPT_OPTVAL];
880 		if (opt == IPOPT_EOL)
881 			break;
882 		if (opt == IPOPT_NOP)
883 			optlen = 1;
884 		else {
885 			optlen = cp[IPOPT_OLEN];
886 			if (optlen <= IPOPT_OLEN || optlen > cnt)
887 				goto bad;
888 		}
889 		switch (opt) {
890 
891 		default:
892 			break;
893 
894 		case IPOPT_LSRR:
895 		case IPOPT_SSRR:
896 			/*
897 			 * user process specifies route as:
898 			 *	->A->B->C->D
899 			 * D must be our final destination (but we can't
900 			 * check that since we may not have connected yet).
901 			 * A is first hop destination, which doesn't appear in
902 			 * actual IP option, but is stored before the options.
903 			 */
904 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
905 				goto bad;
906 			m->m_len -= sizeof(struct in_addr);
907 			cnt -= sizeof(struct in_addr);
908 			optlen -= sizeof(struct in_addr);
909 			cp[IPOPT_OLEN] = optlen;
910 			/*
911 			 * Move first hop before start of options.
912 			 */
913 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
914 			    sizeof(struct in_addr));
915 			/*
916 			 * Then copy rest of options back
917 			 * to close up the deleted entry.
918 			 */
919 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
920 			    sizeof(struct in_addr)),
921 			    (caddr_t)&cp[IPOPT_OFFSET+1],
922 			    (unsigned)cnt + sizeof(struct in_addr));
923 			break;
924 		}
925 	}
926 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
927 		goto bad;
928 	*pcbopt = m;
929 	return (0);
930 
931 bad:
932 	(void)m_free(m);
933 	return (EINVAL);
934 }
935 
936 /*
937  * Set the IP multicast options in response to user setsockopt().
938  */
939 static int
940 ip_setmoptions(optname, imop, m)
941 	int optname;
942 	struct ip_moptions **imop;
943 	struct mbuf *m;
944 {
945 	register int error = 0;
946 	u_char loop;
947 	register int i;
948 	struct in_addr addr;
949 	register struct ip_mreq *mreq;
950 	register struct ifnet *ifp;
951 	register struct ip_moptions *imo = *imop;
952 	struct route ro;
953 	register struct sockaddr_in *dst;
954 	int s;
955 
956 	if (imo == NULL) {
957 		/*
958 		 * No multicast option buffer attached to the pcb;
959 		 * allocate one and initialize to default values.
960 		 */
961 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
962 		    M_WAITOK);
963 
964 		if (imo == NULL)
965 			return (ENOBUFS);
966 		*imop = imo;
967 		imo->imo_multicast_ifp = NULL;
968 		imo->imo_multicast_vif = -1;
969 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
970 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
971 		imo->imo_num_memberships = 0;
972 	}
973 
974 	switch (optname) {
975 	/* store an index number for the vif you wanna use in the send */
976 	case IP_MULTICAST_VIF:
977 		if (!legal_vif_num) {
978 			error = EOPNOTSUPP;
979 			break;
980 		}
981 		if (m == NULL || m->m_len != sizeof(int)) {
982 			error = EINVAL;
983 			break;
984 		}
985 		i = *(mtod(m, int *));
986 		if (!legal_vif_num(i) && (i != -1)) {
987 			error = EINVAL;
988 			break;
989 		}
990 		imo->imo_multicast_vif = i;
991 		break;
992 
993 	case IP_MULTICAST_IF:
994 		/*
995 		 * Select the interface for outgoing multicast packets.
996 		 */
997 		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
998 			error = EINVAL;
999 			break;
1000 		}
1001 		addr = *(mtod(m, struct in_addr *));
1002 		/*
1003 		 * INADDR_ANY is used to remove a previous selection.
1004 		 * When no interface is selected, a default one is
1005 		 * chosen every time a multicast packet is sent.
1006 		 */
1007 		if (addr.s_addr == INADDR_ANY) {
1008 			imo->imo_multicast_ifp = NULL;
1009 			break;
1010 		}
1011 		/*
1012 		 * The selected interface is identified by its local
1013 		 * IP address.  Find the interface and confirm that
1014 		 * it supports multicasting.
1015 		 */
1016 		s = splimp();
1017 		INADDR_TO_IFP(addr, ifp);
1018 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1019 			splx(s);
1020 			error = EADDRNOTAVAIL;
1021 			break;
1022 		}
1023 		imo->imo_multicast_ifp = ifp;
1024 		splx(s);
1025 		break;
1026 
1027 	case IP_MULTICAST_TTL:
1028 		/*
1029 		 * Set the IP time-to-live for outgoing multicast packets.
1030 		 */
1031 		if (m == NULL || m->m_len != 1) {
1032 			error = EINVAL;
1033 			break;
1034 		}
1035 		imo->imo_multicast_ttl = *(mtod(m, u_char *));
1036 		break;
1037 
1038 	case IP_MULTICAST_LOOP:
1039 		/*
1040 		 * Set the loopback flag for outgoing multicast packets.
1041 		 * Must be zero or one.
1042 		 */
1043 		if (m == NULL || m->m_len != 1 ||
1044 		   (loop = *(mtod(m, u_char *))) > 1) {
1045 			error = EINVAL;
1046 			break;
1047 		}
1048 		imo->imo_multicast_loop = loop;
1049 		break;
1050 
1051 	case IP_ADD_MEMBERSHIP:
1052 		/*
1053 		 * Add a multicast group membership.
1054 		 * Group must be a valid IP multicast address.
1055 		 */
1056 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1057 			error = EINVAL;
1058 			break;
1059 		}
1060 		mreq = mtod(m, struct ip_mreq *);
1061 		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1062 			error = EINVAL;
1063 			break;
1064 		}
1065 		s = splimp();
1066 		/*
1067 		 * If no interface address was provided, use the interface of
1068 		 * the route to the given multicast address.
1069 		 */
1070 		if (mreq->imr_interface.s_addr == INADDR_ANY) {
1071 			bzero((caddr_t)&ro, sizeof(ro));
1072 			dst = (struct sockaddr_in *)&ro.ro_dst;
1073 			dst->sin_len = sizeof(*dst);
1074 			dst->sin_family = AF_INET;
1075 			dst->sin_addr = mreq->imr_multiaddr;
1076 			rtalloc(&ro);
1077 			if (ro.ro_rt == NULL) {
1078 				error = EADDRNOTAVAIL;
1079 				splx(s);
1080 				break;
1081 			}
1082 			ifp = ro.ro_rt->rt_ifp;
1083 			rtfree(ro.ro_rt);
1084 		}
1085 		else {
1086 			INADDR_TO_IFP(mreq->imr_interface, ifp);
1087 		}
1088 
1089 		/*
1090 		 * See if we found an interface, and confirm that it
1091 		 * supports multicast.
1092 		 */
1093 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1094 			error = EADDRNOTAVAIL;
1095 			splx(s);
1096 			break;
1097 		}
1098 		/*
1099 		 * See if the membership already exists or if all the
1100 		 * membership slots are full.
1101 		 */
1102 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1103 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1104 			    imo->imo_membership[i]->inm_addr.s_addr
1105 						== mreq->imr_multiaddr.s_addr)
1106 				break;
1107 		}
1108 		if (i < imo->imo_num_memberships) {
1109 			error = EADDRINUSE;
1110 			splx(s);
1111 			break;
1112 		}
1113 		if (i == IP_MAX_MEMBERSHIPS) {
1114 			error = ETOOMANYREFS;
1115 			splx(s);
1116 			break;
1117 		}
1118 		/*
1119 		 * Everything looks good; add a new record to the multicast
1120 		 * address list for the given interface.
1121 		 */
1122 		if ((imo->imo_membership[i] =
1123 		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1124 			error = ENOBUFS;
1125 			splx(s);
1126 			break;
1127 		}
1128 		++imo->imo_num_memberships;
1129 		splx(s);
1130 		break;
1131 
1132 	case IP_DROP_MEMBERSHIP:
1133 		/*
1134 		 * Drop a multicast group membership.
1135 		 * Group must be a valid IP multicast address.
1136 		 */
1137 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1138 			error = EINVAL;
1139 			break;
1140 		}
1141 		mreq = mtod(m, struct ip_mreq *);
1142 		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1143 			error = EINVAL;
1144 			break;
1145 		}
1146 
1147 		s = splimp();
1148 		/*
1149 		 * If an interface address was specified, get a pointer
1150 		 * to its ifnet structure.
1151 		 */
1152 		if (mreq->imr_interface.s_addr == INADDR_ANY)
1153 			ifp = NULL;
1154 		else {
1155 			INADDR_TO_IFP(mreq->imr_interface, ifp);
1156 			if (ifp == NULL) {
1157 				error = EADDRNOTAVAIL;
1158 				splx(s);
1159 				break;
1160 			}
1161 		}
1162 		/*
1163 		 * Find the membership in the membership array.
1164 		 */
1165 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1166 			if ((ifp == NULL ||
1167 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1168 			     imo->imo_membership[i]->inm_addr.s_addr ==
1169 			     mreq->imr_multiaddr.s_addr)
1170 				break;
1171 		}
1172 		if (i == imo->imo_num_memberships) {
1173 			error = EADDRNOTAVAIL;
1174 			splx(s);
1175 			break;
1176 		}
1177 		/*
1178 		 * Give up the multicast address record to which the
1179 		 * membership points.
1180 		 */
1181 		in_delmulti(imo->imo_membership[i]);
1182 		/*
1183 		 * Remove the gap in the membership array.
1184 		 */
1185 		for (++i; i < imo->imo_num_memberships; ++i)
1186 			imo->imo_membership[i-1] = imo->imo_membership[i];
1187 		--imo->imo_num_memberships;
1188 		splx(s);
1189 		break;
1190 
1191 	default:
1192 		error = EOPNOTSUPP;
1193 		break;
1194 	}
1195 
1196 	/*
1197 	 * If all options have default values, no need to keep the mbuf.
1198 	 */
1199 	if (imo->imo_multicast_ifp == NULL &&
1200 	    imo->imo_multicast_vif == -1 &&
1201 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1202 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1203 	    imo->imo_num_memberships == 0) {
1204 		free(*imop, M_IPMOPTS);
1205 		*imop = NULL;
1206 	}
1207 
1208 	return (error);
1209 }
1210 
1211 /*
1212  * Return the IP multicast options in response to user getsockopt().
1213  */
1214 static int
1215 ip_getmoptions(optname, imo, mp)
1216 	int optname;
1217 	register struct ip_moptions *imo;
1218 	register struct mbuf **mp;
1219 {
1220 	u_char *ttl;
1221 	u_char *loop;
1222 	struct in_addr *addr;
1223 	struct in_ifaddr *ia;
1224 
1225 	*mp = m_get(M_WAIT, MT_SOOPTS);
1226 
1227 	switch (optname) {
1228 
1229 	case IP_MULTICAST_VIF:
1230 		if (imo != NULL)
1231 			*(mtod(*mp, int *)) = imo->imo_multicast_vif;
1232 		else
1233 			*(mtod(*mp, int *)) = -1;
1234 		(*mp)->m_len = sizeof(int);
1235 		return(0);
1236 
1237 	case IP_MULTICAST_IF:
1238 		addr = mtod(*mp, struct in_addr *);
1239 		(*mp)->m_len = sizeof(struct in_addr);
1240 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1241 			addr->s_addr = INADDR_ANY;
1242 		else {
1243 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1244 			addr->s_addr = (ia == NULL) ? INADDR_ANY
1245 					: IA_SIN(ia)->sin_addr.s_addr;
1246 		}
1247 		return (0);
1248 
1249 	case IP_MULTICAST_TTL:
1250 		ttl = mtod(*mp, u_char *);
1251 		(*mp)->m_len = 1;
1252 		*ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1253 				     : imo->imo_multicast_ttl;
1254 		return (0);
1255 
1256 	case IP_MULTICAST_LOOP:
1257 		loop = mtod(*mp, u_char *);
1258 		(*mp)->m_len = 1;
1259 		*loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1260 				      : imo->imo_multicast_loop;
1261 		return (0);
1262 
1263 	default:
1264 		return (EOPNOTSUPP);
1265 	}
1266 }
1267 
1268 /*
1269  * Discard the IP multicast options.
1270  */
1271 void
1272 ip_freemoptions(imo)
1273 	register struct ip_moptions *imo;
1274 {
1275 	register int i;
1276 
1277 	if (imo != NULL) {
1278 		for (i = 0; i < imo->imo_num_memberships; ++i)
1279 			in_delmulti(imo->imo_membership[i]);
1280 		free(imo, M_IPMOPTS);
1281 	}
1282 }
1283 
1284 /*
1285  * Routine called from ip_output() to loop back a copy of an IP multicast
1286  * packet to the input queue of a specified interface.  Note that this
1287  * calls the output routine of the loopback "driver", but with an interface
1288  * pointer that might NOT be a loopback interface -- evil, but easier than
1289  * replicating that code here.
1290  */
1291 static void
1292 ip_mloopback(ifp, m, dst, hlen)
1293 	struct ifnet *ifp;
1294 	register struct mbuf *m;
1295 	register struct sockaddr_in *dst;
1296 	int hlen;
1297 {
1298 	register struct ip *ip;
1299 	struct mbuf *copym;
1300 
1301 	copym = m_copy(m, 0, M_COPYALL);
1302 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1303 		copym = m_pullup(copym, hlen);
1304 	if (copym != NULL) {
1305 		/*
1306 		 * We don't bother to fragment if the IP length is greater
1307 		 * than the interface's MTU.  Can this possibly matter?
1308 		 */
1309 		ip = mtod(copym, struct ip *);
1310 		ip->ip_len = htons((u_short)ip->ip_len);
1311 		ip->ip_off = htons((u_short)ip->ip_off);
1312 		ip->ip_sum = 0;
1313 		if (ip->ip_vhl == IP_VHL_BORING) {
1314 			ip->ip_sum = in_cksum_hdr(ip);
1315 		} else {
1316 			ip->ip_sum = in_cksum(copym, hlen);
1317 		}
1318 		/*
1319 		 * NB:
1320 		 * It's not clear whether there are any lingering
1321 		 * reentrancy problems in other areas which might
1322 		 * be exposed by using ip_input directly (in
1323 		 * particular, everything which modifies the packet
1324 		 * in-place).  Yet another option is using the
1325 		 * protosw directly to deliver the looped back
1326 		 * packet.  For the moment, we'll err on the side
1327 		 * of safety by continuing to abuse looutput().
1328 		 */
1329 #ifdef notdef
1330 		copym->m_pkthdr.rcvif = ifp;
1331 		ip_input(copym)
1332 #else
1333 		(void) looutput(ifp, copym, (struct sockaddr *)dst, NULL);
1334 #endif
1335 	}
1336 }
1337