xref: /freebsd/sys/netinet/ip_output.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
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.1.1.2 1997/04/03 10:39:32 darrenr Exp $
35  */
36 
37 #define _IP_VHL
38 
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/errno.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 #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
65 #undef COMPAT_IPFW
66 #define COMPAT_IPFW 1
67 #else
68 #undef COMPAT_IPFW
69 #endif
70 
71 u_short ip_id;
72 
73 static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
74 static void	ip_mloopback
75 	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *));
76 static int	ip_getmoptions
77 	__P((int, struct ip_moptions *, struct mbuf **));
78 static int	ip_pcbopts __P((struct mbuf **, struct mbuf *));
79 static int	ip_setmoptions
80 	__P((int, struct ip_moptions **, struct mbuf *));
81 
82 #if defined(IPFILTER_LKM) || defined(IPFILTER)
83 int	ip_optcopy __P((struct ip *, struct ip *));
84 extern int fr_check __P((struct ip *, int, struct ifnet *, int, struct mbuf **));
85 extern int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **));
86 #else
87 static int	ip_optcopy __P((struct ip *, struct ip *));
88 #endif
89 
90 
91 extern	struct protosw inetsw[];
92 
93 /*
94  * IP output.  The packet in mbuf chain m contains a skeletal IP
95  * header (with len, off, ttl, proto, tos, src, dst).
96  * The mbuf chain containing the packet will be freed.
97  * The mbuf opt, if present, will not be freed.
98  */
99 int
100 ip_output(m0, opt, ro, flags, imo)
101 	struct mbuf *m0;
102 	struct mbuf *opt;
103 	struct route *ro;
104 	int flags;
105 	struct ip_moptions *imo;
106 {
107 	struct ip *ip, *mhip;
108 	struct ifnet *ifp;
109 	struct mbuf *m = m0;
110 	int hlen = sizeof (struct ip);
111 	int len, off, error = 0;
112 	struct sockaddr_in *dst;
113 	struct in_ifaddr *ia;
114 	int isbroadcast;
115 
116 #ifdef	DIAGNOSTIC
117 	if ((m->m_flags & M_PKTHDR) == 0)
118 		panic("ip_output no HDR");
119 	if (!ro)
120 		panic("ip_output no route, proto = %d",
121 		      mtod(m, struct ip *)->ip_p);
122 #endif
123 	if (opt) {
124 		m = ip_insertoptions(m, opt, &len);
125 		hlen = len;
126 	}
127 	ip = mtod(m, struct ip *);
128 	/*
129 	 * Fill in IP header.
130 	 */
131 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
132 		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
133 		ip->ip_off &= IP_DF;
134 		ip->ip_id = htons(ip_id++);
135 		ipstat.ips_localout++;
136 	} else {
137 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
138 	}
139 
140 	dst = (struct sockaddr_in *)&ro->ro_dst;
141 	/*
142 	 * If there is a cached route,
143 	 * check that it is to the same destination
144 	 * and is still up.  If not, free it and try again.
145 	 */
146 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
147 	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
148 		RTFREE(ro->ro_rt);
149 		ro->ro_rt = (struct rtentry *)0;
150 	}
151 	if (ro->ro_rt == 0) {
152 		dst->sin_family = AF_INET;
153 		dst->sin_len = sizeof(*dst);
154 		dst->sin_addr = ip->ip_dst;
155 	}
156 	/*
157 	 * If routing to interface only,
158 	 * short circuit routing lookup.
159 	 */
160 #define ifatoia(ifa)	((struct in_ifaddr *)(ifa))
161 #define sintosa(sin)	((struct sockaddr *)(sin))
162 	if (flags & IP_ROUTETOIF) {
163 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
164 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
165 			ipstat.ips_noroute++;
166 			error = ENETUNREACH;
167 			goto bad;
168 		}
169 		ifp = ia->ia_ifp;
170 		ip->ip_ttl = 1;
171 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
172 	} else {
173 		/*
174 		 * If this is the case, we probably don't want to allocate
175 		 * a protocol-cloned route since we didn't get one from the
176 		 * ULP.  This lets TCP do its thing, while not burdening
177 		 * forwarding or ICMP with the overhead of cloning a route.
178 		 * Of course, we still want to do any cloning requested by
179 		 * the link layer, as this is probably required in all cases
180 		 * for correct operation (as it is for ARP).
181 		 */
182 		if (ro->ro_rt == 0)
183 			rtalloc_ign(ro, RTF_PRCLONING);
184 		if (ro->ro_rt == 0) {
185 			ipstat.ips_noroute++;
186 			error = EHOSTUNREACH;
187 			goto bad;
188 		}
189 		ia = ifatoia(ro->ro_rt->rt_ifa);
190 		ifp = ro->ro_rt->rt_ifp;
191 		ro->ro_rt->rt_use++;
192 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
193 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
194 		if (ro->ro_rt->rt_flags & RTF_HOST)
195 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
196 		else
197 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
198 	}
199 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
200 		struct in_multi *inm;
201 
202 		m->m_flags |= M_MCAST;
203 		/*
204 		 * IP destination address is multicast.  Make sure "dst"
205 		 * still points to the address in "ro".  (It may have been
206 		 * changed to point to a gateway address, above.)
207 		 */
208 		dst = (struct sockaddr_in *)&ro->ro_dst;
209 		/*
210 		 * See if the caller provided any multicast options
211 		 */
212 		if (imo != NULL) {
213 			ip->ip_ttl = imo->imo_multicast_ttl;
214 			if (imo->imo_multicast_ifp != NULL)
215 				ifp = imo->imo_multicast_ifp;
216 			if (imo->imo_multicast_vif != -1)
217 				ip->ip_src.s_addr =
218 				    ip_mcast_src(imo->imo_multicast_vif);
219 		} else
220 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
221 		/*
222 		 * Confirm that the outgoing interface supports multicast.
223 		 */
224 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
225 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
226 				ipstat.ips_noroute++;
227 				error = ENETUNREACH;
228 				goto bad;
229 			}
230 		}
231 		/*
232 		 * If source address not specified yet, use address
233 		 * of outgoing interface.
234 		 */
235 		if (ip->ip_src.s_addr == INADDR_ANY) {
236 			register struct in_ifaddr *ia;
237 
238 			for (ia = in_ifaddrhead.tqh_first; ia;
239 			     ia = ia->ia_link.tqe_next)
240 				if (ia->ia_ifp == ifp) {
241 					ip->ip_src = IA_SIN(ia)->sin_addr;
242 					break;
243 				}
244 		}
245 
246 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
247 		if (inm != NULL &&
248 		   (imo == NULL || imo->imo_multicast_loop)) {
249 			/*
250 			 * If we belong to the destination multicast group
251 			 * on the outgoing interface, and the caller did not
252 			 * forbid loopback, loop back a copy.
253 			 */
254 			ip_mloopback(ifp, m, dst);
255 		}
256 		else {
257 			/*
258 			 * If we are acting as a multicast router, perform
259 			 * multicast forwarding as if the packet had just
260 			 * arrived on the interface to which we are about
261 			 * to send.  The multicast forwarding function
262 			 * recursively calls this function, using the
263 			 * IP_FORWARDING flag to prevent infinite recursion.
264 			 *
265 			 * Multicasts that are looped back by ip_mloopback(),
266 			 * above, will be forwarded by the ip_input() routine,
267 			 * if necessary.
268 			 */
269 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
270 				/*
271 				 * Check if rsvp daemon is running. If not, don't
272 				 * set ip_moptions. This ensures that the packet
273 				 * is multicast and not just sent down one link
274 				 * as prescribed by rsvpd.
275 				 */
276 				if (!rsvp_on)
277 				  imo = NULL;
278 				if (ip_mforward(ip, ifp, m, imo) != 0) {
279 					m_freem(m);
280 					goto done;
281 				}
282 			}
283 		}
284 
285 		/*
286 		 * Multicasts with a time-to-live of zero may be looped-
287 		 * back, above, but must not be transmitted on a network.
288 		 * Also, multicasts addressed to the loopback interface
289 		 * are not sent -- the above call to ip_mloopback() will
290 		 * loop back a copy if this host actually belongs to the
291 		 * destination group on the loopback interface.
292 		 */
293 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
294 			m_freem(m);
295 			goto done;
296 		}
297 
298 		goto sendit;
299 	}
300 #ifndef notdef
301 	/*
302 	 * If source address not specified yet, use address
303 	 * of outgoing interface.
304 	 */
305 	if (ip->ip_src.s_addr == INADDR_ANY)
306 		ip->ip_src = IA_SIN(ia)->sin_addr;
307 #endif
308 	/*
309 	 * Verify that we have any chance at all of being able to queue
310 	 *      the packet or packet fragments
311 	 */
312 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
313 		ifp->if_snd.ifq_maxlen) {
314 			error = ENOBUFS;
315 			goto bad;
316 	}
317 
318 	/*
319 	 * Look for broadcast address and
320 	 * and verify user is allowed to send
321 	 * such a packet.
322 	 */
323 	if (isbroadcast) {
324 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
325 			error = EADDRNOTAVAIL;
326 			goto bad;
327 		}
328 		if ((flags & IP_ALLOWBROADCAST) == 0) {
329 			error = EACCES;
330 			goto bad;
331 		}
332 		/* don't allow broadcast messages to be fragmented */
333 		if ((u_short)ip->ip_len > ifp->if_mtu) {
334 			error = EMSGSIZE;
335 			goto bad;
336 		}
337 		m->m_flags |= M_BCAST;
338 	} else {
339 		m->m_flags &= ~M_BCAST;
340 	}
341 
342 sendit:
343 #if defined(IPFILTER) || defined(IPFILTER_LKM)
344 	/*
345 	 * looks like most checking has been done now...do a filter check
346 	 */
347 	if (fr_checkp) {
348 		struct  mbuf    *m1 = m;
349 
350 		if ((*fr_checkp)(ip, hlen, ifp, 1, &m1))
351 			error = EHOSTUNREACH;
352 		if (error || !m1)
353 			goto done;
354 		ip = mtod(m = m1, struct ip *);
355 	}
356 #endif
357         /*
358 	 * IpHack's section.
359 	 * - Xlate: translate packet's addr/port (NAT).
360 	 * - Firewall: deny/allow
361 	 * - Wrap: fake packet's addr/port <unimpl.>
362 	 * - Encapsulate: put it in another IP and send out. <unimp.>
363 	 */
364 
365 #ifdef COMPAT_IPFW
366         if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, ifp, IP_NAT_OUT)) {
367 		error = EACCES;
368 		goto done;
369 	}
370 
371 	/*
372 	 * Check with the firewall...
373 	 */
374 	if (ip_fw_chk_ptr) {
375 		int action;
376 
377 #ifdef IPDIVERT
378 		action = (*ip_fw_chk_ptr)(&ip,
379 				hlen, ifp, (~0 << 16) | ip_divert_ignore, &m);
380 		ip_divert_ignore = 0;
381 #else
382 		action = (*ip_fw_chk_ptr)(&ip, hlen, ifp, (~0 << 16), &m);
383 #endif
384 		if (action == -1) {
385 			error = EACCES;		/* XXX is this appropriate? */
386 			goto done;
387 		} else if (action != 0) {
388 #ifdef IPDIVERT
389 			ip_divert_port = action;	/* divert to port */
390 			(*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, 0);
391 			goto done;
392 #else
393 			m_freem(m);	/* ipfw says divert, but we can't */
394 			goto done;
395 #endif
396 		}
397 	}
398 #endif /* COMPAT_IPFW */
399 
400 	/*
401 	 * If small enough for interface, can just send directly.
402 	 */
403 	if ((u_short)ip->ip_len <= ifp->if_mtu) {
404 		ip->ip_len = htons((u_short)ip->ip_len);
405 		ip->ip_off = htons((u_short)ip->ip_off);
406 		ip->ip_sum = 0;
407 		if (ip->ip_vhl == IP_VHL_BORING) {
408 			ip->ip_sum = in_cksum_hdr(ip);
409 		} else {
410 			ip->ip_sum = in_cksum(m, hlen);
411 		}
412 		error = (*ifp->if_output)(ifp, m,
413 				(struct sockaddr *)dst, ro->ro_rt);
414 		goto done;
415 	}
416 	/*
417 	 * Too large for interface; fragment if possible.
418 	 * Must be able to put at least 8 bytes per fragment.
419 	 */
420 	if (ip->ip_off & IP_DF) {
421 		error = EMSGSIZE;
422 		/*
423 		 * This case can happen if the user changed the MTU
424 		 * of an interface after enabling IP on it.  Because
425 		 * most netifs don't keep track of routes pointing to
426 		 * them, there is no way for one to update all its
427 		 * routes when the MTU is changed.
428 		 */
429 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
430 		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
431 		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
432 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
433 		}
434 		ipstat.ips_cantfrag++;
435 		goto bad;
436 	}
437 	len = (ifp->if_mtu - hlen) &~ 7;
438 	if (len < 8) {
439 		error = EMSGSIZE;
440 		goto bad;
441 	}
442 
443     {
444 	int mhlen, firstlen = len;
445 	struct mbuf **mnext = &m->m_nextpkt;
446 
447 	/*
448 	 * Loop through length of segment after first fragment,
449 	 * make new header and copy data of each part and link onto chain.
450 	 */
451 	m0 = m;
452 	mhlen = sizeof (struct ip);
453 	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
454 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
455 		if (m == 0) {
456 			error = ENOBUFS;
457 			ipstat.ips_odropped++;
458 			goto sendorfree;
459 		}
460 		m->m_data += max_linkhdr;
461 		mhip = mtod(m, struct ip *);
462 		*mhip = *ip;
463 		if (hlen > sizeof (struct ip)) {
464 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
465 			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
466 		}
467 		m->m_len = mhlen;
468 		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
469 		if (ip->ip_off & IP_MF)
470 			mhip->ip_off |= IP_MF;
471 		if (off + len >= (u_short)ip->ip_len)
472 			len = (u_short)ip->ip_len - off;
473 		else
474 			mhip->ip_off |= IP_MF;
475 		mhip->ip_len = htons((u_short)(len + mhlen));
476 		m->m_next = m_copy(m0, off, len);
477 		if (m->m_next == 0) {
478 			(void) m_free(m);
479 			error = ENOBUFS;	/* ??? */
480 			ipstat.ips_odropped++;
481 			goto sendorfree;
482 		}
483 		m->m_pkthdr.len = mhlen + len;
484 		m->m_pkthdr.rcvif = (struct ifnet *)0;
485 		mhip->ip_off = htons((u_short)mhip->ip_off);
486 		mhip->ip_sum = 0;
487 		if (mhip->ip_vhl == IP_VHL_BORING) {
488 			mhip->ip_sum = in_cksum_hdr(mhip);
489 		} else {
490 			mhip->ip_sum = in_cksum(m, mhlen);
491 		}
492 		*mnext = m;
493 		mnext = &m->m_nextpkt;
494 		ipstat.ips_ofragments++;
495 	}
496 	/*
497 	 * Update first fragment by trimming what's been copied out
498 	 * and updating header, then send each fragment (in order).
499 	 */
500 	m = m0;
501 	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
502 	m->m_pkthdr.len = hlen + firstlen;
503 	ip->ip_len = htons((u_short)m->m_pkthdr.len);
504 	ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
505 	ip->ip_sum = 0;
506 	if (ip->ip_vhl == IP_VHL_BORING) {
507 		ip->ip_sum = in_cksum_hdr(ip);
508 	} else {
509 		ip->ip_sum = in_cksum(m, hlen);
510 	}
511 sendorfree:
512 	for (m = m0; m; m = m0) {
513 		m0 = m->m_nextpkt;
514 		m->m_nextpkt = 0;
515 		if (error == 0)
516 			error = (*ifp->if_output)(ifp, m,
517 			    (struct sockaddr *)dst, ro->ro_rt);
518 		else
519 			m_freem(m);
520 	}
521 
522 	if (error == 0)
523 		ipstat.ips_fragmented++;
524     }
525 done:
526 	return (error);
527 bad:
528 	m_freem(m0);
529 	goto done;
530 }
531 
532 /*
533  * Insert IP options into preformed packet.
534  * Adjust IP destination as required for IP source routing,
535  * as indicated by a non-zero in_addr at the start of the options.
536  *
537  * XXX This routine assumes that the packet has no options in place.
538  */
539 static struct mbuf *
540 ip_insertoptions(m, opt, phlen)
541 	register struct mbuf *m;
542 	struct mbuf *opt;
543 	int *phlen;
544 {
545 	register struct ipoption *p = mtod(opt, struct ipoption *);
546 	struct mbuf *n;
547 	register struct ip *ip = mtod(m, struct ip *);
548 	unsigned optlen;
549 
550 	optlen = opt->m_len - sizeof(p->ipopt_dst);
551 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
552 		return (m);		/* XXX should fail */
553 	if (p->ipopt_dst.s_addr)
554 		ip->ip_dst = p->ipopt_dst;
555 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
556 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
557 		if (n == 0)
558 			return (m);
559 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
560 		m->m_len -= sizeof(struct ip);
561 		m->m_data += sizeof(struct ip);
562 		n->m_next = m;
563 		m = n;
564 		m->m_len = optlen + sizeof(struct ip);
565 		m->m_data += max_linkhdr;
566 		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
567 	} else {
568 		m->m_data -= optlen;
569 		m->m_len += optlen;
570 		m->m_pkthdr.len += optlen;
571 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
572 	}
573 	ip = mtod(m, struct ip *);
574 	bcopy(p->ipopt_list, ip + 1, optlen);
575 	*phlen = sizeof(struct ip) + optlen;
576 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
577 	ip->ip_len += optlen;
578 	return (m);
579 }
580 
581 /*
582  * Copy options from ip to jp,
583  * omitting those not copied during fragmentation.
584  */
585 #if !defined(IPFILTER) && !defined(IPFILTER_LKM)
586 static
587 #endif
588 int
589 ip_optcopy(ip, jp)
590 	struct ip *ip, *jp;
591 {
592 	register u_char *cp, *dp;
593 	int opt, optlen, cnt;
594 
595 	cp = (u_char *)(ip + 1);
596 	dp = (u_char *)(jp + 1);
597 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
598 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
599 		opt = cp[0];
600 		if (opt == IPOPT_EOL)
601 			break;
602 		if (opt == IPOPT_NOP) {
603 			/* Preserve for IP mcast tunnel's LSRR alignment. */
604 			*dp++ = IPOPT_NOP;
605 			optlen = 1;
606 			continue;
607 		} else
608 			optlen = cp[IPOPT_OLEN];
609 		/* bogus lengths should have been caught by ip_dooptions */
610 		if (optlen > cnt)
611 			optlen = cnt;
612 		if (IPOPT_COPIED(opt)) {
613 			bcopy(cp, dp, optlen);
614 			dp += optlen;
615 		}
616 	}
617 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
618 		*dp++ = IPOPT_EOL;
619 	return (optlen);
620 }
621 
622 /*
623  * IP socket option processing.
624  */
625 int
626 ip_ctloutput(op, so, level, optname, mp)
627 	int op;
628 	struct socket *so;
629 	int level, optname;
630 	struct mbuf **mp;
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)
1293 	struct ifnet *ifp;
1294 	register struct mbuf *m;
1295 	register struct sockaddr_in *dst;
1296 {
1297 	register struct ip *ip;
1298 	struct mbuf *copym;
1299 
1300 	copym = m_copy(m, 0, M_COPYALL);
1301 	if (copym != NULL) {
1302 		/*
1303 		 * We don't bother to fragment if the IP length is greater
1304 		 * than the interface's MTU.  Can this possibly matter?
1305 		 */
1306 		ip = mtod(copym, struct ip *);
1307 		ip->ip_len = htons((u_short)ip->ip_len);
1308 		ip->ip_off = htons((u_short)ip->ip_off);
1309 		ip->ip_sum = 0;
1310 		if (ip->ip_vhl == IP_VHL_BORING) {
1311 			ip->ip_sum = in_cksum_hdr(ip);
1312 		} else {
1313 			ip->ip_sum = in_cksum(copym,
1314 					      IP_VHL_HL(ip->ip_vhl) << 2);
1315 		}
1316 		/*
1317 		 * NB:
1318 		 * It's not clear whether there are any lingering
1319 		 * reentrancy problems in other areas which might
1320 		 * be exposed by using ip_input directly (in
1321 		 * particular, everything which modifies the packet
1322 		 * in-place).  Yet another option is using the
1323 		 * protosw directly to deliver the looped back
1324 		 * packet.  For the moment, we'll err on the side
1325 		 * of safety by continuing to abuse looutput().
1326 		 */
1327 #ifdef notdef
1328 		copym->m_pkthdr.rcvif = ifp;
1329 		ip_input(copym)
1330 #else
1331 		(void) looutput(ifp, copym, (struct sockaddr *)dst, NULL);
1332 #endif
1333 	}
1334 }
1335