xref: /openbsd-src/sys/netinet/ip_output.c (revision 897fc685943471cf985a0fe38ba076ea6fe74fa5)
1 /*	$OpenBSD: ip_output.c,v 1.346 2018/03/21 14:42:41 bluhm Exp $	*/
2 /*	$NetBSD: ip_output.c,v 1.28 1996/02/13 23:43:07 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1988, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
33  */
34 
35 #include "pf.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/proc.h>
44 #include <sys/kernel.h>
45 
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_enc.h>
49 #include <net/route.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/ip.h>
53 #include <netinet/in_pcb.h>
54 #include <netinet/in_var.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/ip_icmp.h>
57 #include <netinet/tcp.h>
58 #include <netinet/udp.h>
59 #include <netinet/tcp_timer.h>
60 #include <netinet/tcp_var.h>
61 #include <netinet/udp_var.h>
62 
63 #if NPF > 0
64 #include <net/pfvar.h>
65 #endif
66 
67 #ifdef IPSEC
68 #ifdef ENCDEBUG
69 #define DPRINTF(x)    do { if (encdebug) printf x ; } while (0)
70 #else
71 #define DPRINTF(x)
72 #endif
73 #endif /* IPSEC */
74 
75 int ip_pcbopts(struct mbuf **, struct mbuf *);
76 int ip_setmoptions(int, struct ip_moptions **, struct mbuf *, u_int);
77 void ip_mloopback(struct ifnet *, struct mbuf *, struct sockaddr_in *);
78 static __inline u_int16_t __attribute__((__unused__))
79     in_cksum_phdr(u_int32_t, u_int32_t, u_int32_t);
80 void in_delayed_cksum(struct mbuf *);
81 
82 #ifdef IPSEC
83 struct tdb *
84 ip_output_ipsec_lookup(struct mbuf *m, int hlen, int *error, struct inpcb *inp,
85     int ipsecflowinfo);
86 int
87 ip_output_ipsec_send(struct tdb *, struct mbuf *, struct route *, int);
88 #endif /* IPSEC */
89 
90 /*
91  * IP output.  The packet in mbuf chain m contains a skeletal IP
92  * header (with len, off, ttl, proto, tos, src, dst).
93  * The mbuf chain containing the packet will be freed.
94  * The mbuf opt, if present, will not be freed.
95  */
96 int
97 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, int flags,
98     struct ip_moptions *imo, struct inpcb *inp, u_int32_t ipsecflowinfo)
99 {
100 	struct ip *ip;
101 	struct ifnet *ifp = NULL;
102 	struct mbuf *m = m0;
103 	int hlen = sizeof (struct ip);
104 	int len, error = 0;
105 	struct route iproute;
106 	struct sockaddr_in *dst;
107 	struct tdb *tdb = NULL;
108 	u_long mtu;
109 #if defined(MROUTING)
110 	int rv;
111 #endif
112 
113 	NET_ASSERT_LOCKED();
114 
115 #ifdef IPSEC
116 	if (inp && (inp->inp_flags & INP_IPV6) != 0)
117 		panic("ip_output: IPv6 pcb is passed");
118 #endif /* IPSEC */
119 
120 #ifdef	DIAGNOSTIC
121 	if ((m->m_flags & M_PKTHDR) == 0)
122 		panic("ip_output no HDR");
123 #endif
124 	if (opt) {
125 		m = ip_insertoptions(m, opt, &len);
126 		hlen = len;
127 	}
128 
129 	ip = mtod(m, struct ip *);
130 
131 	/*
132 	 * Fill in IP header.
133 	 */
134 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
135 		ip->ip_v = IPVERSION;
136 		ip->ip_off &= htons(IP_DF);
137 		ip->ip_id = htons(ip_randomid());
138 		ip->ip_hl = hlen >> 2;
139 		ipstat_inc(ips_localout);
140 	} else {
141 		hlen = ip->ip_hl << 2;
142 	}
143 
144 	/*
145 	 * We should not send traffic to 0/8 say both Stevens and RFCs
146 	 * 5735 section 3 and 1122 sections 3.2.1.3 and 3.3.6.
147 	 */
148 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == 0) {
149 		error = ENETUNREACH;
150 		goto bad;
151 	}
152 
153 #if NPF > 0
154 reroute:
155 #endif
156 
157 	/*
158 	 * Do a route lookup now in case we need the source address to
159 	 * do an SPD lookup in IPsec; for most packets, the source address
160 	 * is set at a higher level protocol. ICMPs and other packets
161 	 * though (e.g., traceroute) have a source address of zeroes.
162 	 */
163 	if (ro == NULL) {
164 		ro = &iproute;
165 		memset(ro, 0, sizeof(*ro));
166 	}
167 
168 	dst = satosin(&ro->ro_dst);
169 
170 	/*
171 	 * If there is a cached route, check that it is to the same
172 	 * destination and is still up.  If not, free it and try again.
173 	 */
174 	if (!rtisvalid(ro->ro_rt) ||
175 	    dst->sin_addr.s_addr != ip->ip_dst.s_addr ||
176 	    ro->ro_tableid != m->m_pkthdr.ph_rtableid) {
177 		rtfree(ro->ro_rt);
178 		ro->ro_rt = NULL;
179 	}
180 
181 	if (ro->ro_rt == NULL) {
182 		dst->sin_family = AF_INET;
183 		dst->sin_len = sizeof(*dst);
184 		dst->sin_addr = ip->ip_dst;
185 		ro->ro_tableid = m->m_pkthdr.ph_rtableid;
186 	}
187 
188 	if ((IN_MULTICAST(ip->ip_dst.s_addr) ||
189 	    (ip->ip_dst.s_addr == INADDR_BROADCAST)) &&
190 	    imo != NULL && (ifp = if_get(imo->imo_ifidx)) != NULL) {
191 
192 		mtu = ifp->if_mtu;
193 		if (ip->ip_src.s_addr == INADDR_ANY) {
194 			struct in_ifaddr *ia;
195 
196 			IFP_TO_IA(ifp, ia);
197 			if (ia != NULL)
198 				ip->ip_src = ia->ia_addr.sin_addr;
199 		}
200 	} else {
201 		struct in_ifaddr *ia;
202 
203 		if (ro->ro_rt == NULL)
204 			ro->ro_rt = rtalloc_mpath(&ro->ro_dst,
205 			    &ip->ip_src.s_addr, ro->ro_tableid);
206 
207 		if (ro->ro_rt == NULL) {
208 			ipstat_inc(ips_noroute);
209 			error = EHOSTUNREACH;
210 			goto bad;
211 		}
212 
213 		ia = ifatoia(ro->ro_rt->rt_ifa);
214 		if (ISSET(ro->ro_rt->rt_flags, RTF_LOCAL))
215 			ifp = if_get(rtable_loindex(m->m_pkthdr.ph_rtableid));
216 		else
217 			ifp = if_get(ro->ro_rt->rt_ifidx);
218 		/*
219 		 * We aren't using rtisvalid() here because the UP/DOWN state
220 		 * machine is broken with some Ethernet drivers like em(4).
221 		 * As a result we might try to use an invalid cached route
222 		 * entry while an interface is being detached.
223 		 */
224 		if (ifp == NULL) {
225 			ipstat_inc(ips_noroute);
226 			error = EHOSTUNREACH;
227 			goto bad;
228 		}
229 		if ((mtu = ro->ro_rt->rt_mtu) == 0)
230 			mtu = ifp->if_mtu;
231 
232 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
233 			dst = satosin(ro->ro_rt->rt_gateway);
234 
235 		/* Set the source IP address */
236 		if (ip->ip_src.s_addr == INADDR_ANY && ia)
237 			ip->ip_src = ia->ia_addr.sin_addr;
238 	}
239 
240 #ifdef IPSEC
241 	if (ipsec_in_use || inp != NULL) {
242 		/* Do we have any pending SAs to apply ? */
243 		tdb = ip_output_ipsec_lookup(m, hlen, &error, inp,
244 		    ipsecflowinfo);
245 		if (error != 0) {
246 			/* Should silently drop packet */
247 			if (error == -EINVAL)
248 				error = 0;
249 			m_freem(m);
250 			goto done;
251 		}
252 		if (tdb != NULL) {
253 			/*
254 			 * If it needs TCP/UDP hardware-checksumming, do the
255 			 * computation now.
256 			 */
257 			in_proto_cksum_out(m, NULL);
258 		}
259 	}
260 #endif /* IPSEC */
261 
262 	if (IN_MULTICAST(ip->ip_dst.s_addr) ||
263 	    (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
264 
265 		m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
266 			M_BCAST : M_MCAST;
267 
268 		/*
269 		 * IP destination address is multicast.  Make sure "dst"
270 		 * still points to the address in "ro".  (It may have been
271 		 * changed to point to a gateway address, above.)
272 		 */
273 		dst = satosin(&ro->ro_dst);
274 
275 		/*
276 		 * See if the caller provided any multicast options
277 		 */
278 		if (imo != NULL)
279 			ip->ip_ttl = imo->imo_ttl;
280 		else
281 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
282 
283 		/*
284 		 * if we don't know the outgoing ifp yet, we can't generate
285 		 * output
286 		 */
287 		if (!ifp) {
288 			ipstat_inc(ips_noroute);
289 			error = EHOSTUNREACH;
290 			goto bad;
291 		}
292 
293 		/*
294 		 * Confirm that the outgoing interface supports multicast,
295 		 * but only if the packet actually is going out on that
296 		 * interface (i.e., no IPsec is applied).
297 		 */
298 		if ((((m->m_flags & M_MCAST) &&
299 		      (ifp->if_flags & IFF_MULTICAST) == 0) ||
300 		     ((m->m_flags & M_BCAST) &&
301 		      (ifp->if_flags & IFF_BROADCAST) == 0)) && (tdb == NULL)) {
302 			ipstat_inc(ips_noroute);
303 			error = ENETUNREACH;
304 			goto bad;
305 		}
306 
307 		/*
308 		 * If source address not specified yet, use address
309 		 * of outgoing interface.
310 		 */
311 		if (ip->ip_src.s_addr == INADDR_ANY) {
312 			struct in_ifaddr *ia;
313 
314 			IFP_TO_IA(ifp, ia);
315 			if (ia != NULL)
316 				ip->ip_src = ia->ia_addr.sin_addr;
317 		}
318 
319 		if ((imo == NULL || imo->imo_loop) &&
320 		    in_hasmulti(&ip->ip_dst, ifp)) {
321 			/*
322 			 * If we belong to the destination multicast group
323 			 * on the outgoing interface, and the caller did not
324 			 * forbid loopback, loop back a copy.
325 			 * Can't defer TCP/UDP checksumming, do the
326 			 * computation now.
327 			 */
328 			in_proto_cksum_out(m, NULL);
329 			ip_mloopback(ifp, m, dst);
330 		}
331 #ifdef MROUTING
332 		else {
333 			/*
334 			 * If we are acting as a multicast router, perform
335 			 * multicast forwarding as if the packet had just
336 			 * arrived on the interface to which we are about
337 			 * to send.  The multicast forwarding function
338 			 * recursively calls this function, using the
339 			 * IP_FORWARDING flag to prevent infinite recursion.
340 			 *
341 			 * Multicasts that are looped back by ip_mloopback(),
342 			 * above, will be forwarded by the ip_input() routine,
343 			 * if necessary.
344 			 */
345 			if (ipmforwarding && ip_mrouter[ifp->if_rdomain] &&
346 			    (flags & IP_FORWARDING) == 0) {
347 				KERNEL_LOCK();
348 				rv = ip_mforward(m, ifp);
349 				KERNEL_UNLOCK();
350 				if (rv != 0) {
351 					m_freem(m);
352 					goto done;
353 				}
354 			}
355 		}
356 #endif
357 		/*
358 		 * Multicasts with a time-to-live of zero may be looped-
359 		 * back, above, but must not be transmitted on a network.
360 		 * Also, multicasts addressed to the loopback interface
361 		 * are not sent -- the above call to ip_mloopback() will
362 		 * loop back a copy if this host actually belongs to the
363 		 * destination group on the loopback interface.
364 		 */
365 		if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
366 			m_freem(m);
367 			goto done;
368 		}
369 
370 		goto sendit;
371 	}
372 
373 	/*
374 	 * Look for broadcast address and verify user is allowed to send
375 	 * such a packet; if the packet is going in an IPsec tunnel, skip
376 	 * this check.
377 	 */
378 	if ((tdb == NULL) && ((dst->sin_addr.s_addr == INADDR_BROADCAST) ||
379 	    (ro && ro->ro_rt && ISSET(ro->ro_rt->rt_flags, RTF_BROADCAST)))) {
380 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
381 			error = EADDRNOTAVAIL;
382 			goto bad;
383 		}
384 		if ((flags & IP_ALLOWBROADCAST) == 0) {
385 			error = EACCES;
386 			goto bad;
387 		}
388 
389 		/* Don't allow broadcast messages to be fragmented */
390 		if (ntohs(ip->ip_len) > ifp->if_mtu) {
391 			error = EMSGSIZE;
392 			goto bad;
393 		}
394 		m->m_flags |= M_BCAST;
395 	} else
396 		m->m_flags &= ~M_BCAST;
397 
398 sendit:
399 	/*
400 	 * If we're doing Path MTU discovery, we need to set DF unless
401 	 * the route's MTU is locked.
402 	 */
403 	if ((flags & IP_MTUDISC) && ro && ro->ro_rt &&
404 	    (ro->ro_rt->rt_locks & RTV_MTU) == 0)
405 		ip->ip_off |= htons(IP_DF);
406 
407 #ifdef IPSEC
408 	/*
409 	 * Check if the packet needs encapsulation.
410 	 */
411 	if (tdb != NULL) {
412 		/* Callee frees mbuf */
413 		error = ip_output_ipsec_send(tdb, m, ro,
414 		    (flags & IP_FORWARDING) ? 1 : 0);
415 		goto done;
416 	}
417 #endif /* IPSEC */
418 
419 	/*
420 	 * Packet filter
421 	 */
422 #if NPF > 0
423 	if (pf_test(AF_INET, (flags & IP_FORWARDING) ? PF_FWD : PF_OUT,
424 	    ifp, &m) != PF_PASS) {
425 		error = EACCES;
426 		m_freem(m);
427 		goto done;
428 	}
429 	if (m == NULL)
430 		goto done;
431 	ip = mtod(m, struct ip *);
432 	hlen = ip->ip_hl << 2;
433 	if ((m->m_pkthdr.pf.flags & (PF_TAG_REROUTE | PF_TAG_GENERATED)) ==
434 	    (PF_TAG_REROUTE | PF_TAG_GENERATED))
435 		/* already rerun the route lookup, go on */
436 		m->m_pkthdr.pf.flags &= ~(PF_TAG_GENERATED | PF_TAG_REROUTE);
437 	else if (m->m_pkthdr.pf.flags & PF_TAG_REROUTE) {
438 		/* tag as generated to skip over pf_test on rerun */
439 		m->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
440 		ro = NULL;
441 		if_put(ifp); /* drop reference since target changed */
442 		ifp = NULL;
443 		goto reroute;
444 	}
445 #endif
446 	in_proto_cksum_out(m, ifp);
447 
448 #ifdef IPSEC
449 	if (ipsec_in_use && (flags & IP_FORWARDING) && (ipforwarding == 2) &&
450 	    (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) == NULL)) {
451 		error = EHOSTUNREACH;
452 		m_freem(m);
453 		goto done;
454 	}
455 #endif
456 
457 	/*
458 	 * If small enough for interface, can just send directly.
459 	 */
460 	if (ntohs(ip->ip_len) <= mtu) {
461 		ip->ip_sum = 0;
462 		if ((ifp->if_capabilities & IFCAP_CSUM_IPv4) &&
463 		    (ifp->if_bridgeport == NULL))
464 			m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT;
465 		else {
466 			ipstat_inc(ips_outswcsum);
467 			ip->ip_sum = in_cksum(m, hlen);
468 		}
469 
470 		error = ifp->if_output(ifp, m, sintosa(dst), ro->ro_rt);
471 		goto done;
472 	}
473 
474 	/*
475 	 * Too large for interface; fragment if possible.
476 	 * Must be able to put at least 8 bytes per fragment.
477 	 */
478 	if (ip->ip_off & htons(IP_DF)) {
479 #ifdef IPSEC
480 		if (ip_mtudisc)
481 			ipsec_adjust_mtu(m, ifp->if_mtu);
482 #endif
483 		error = EMSGSIZE;
484 		/*
485 		 * This case can happen if the user changed the MTU
486 		 * of an interface after enabling IP on it.  Because
487 		 * most netifs don't keep track of routes pointing to
488 		 * them, there is no way for one to update all its
489 		 * routes when the MTU is changed.
490 		 */
491 		if (rtisvalid(ro->ro_rt) &&
492 		    ISSET(ro->ro_rt->rt_flags, RTF_HOST) &&
493 		    !(ro->ro_rt->rt_locks & RTV_MTU) &&
494 		    (ro->ro_rt->rt_mtu > ifp->if_mtu)) {
495 			ro->ro_rt->rt_mtu = ifp->if_mtu;
496 		}
497 		ipstat_inc(ips_cantfrag);
498 		goto bad;
499 	}
500 
501 	error = ip_fragment(m, ifp, mtu);
502 	if (error) {
503 		m = m0 = NULL;
504 		goto bad;
505 	}
506 
507 	for (; m; m = m0) {
508 		m0 = m->m_nextpkt;
509 		m->m_nextpkt = 0;
510 		if (error == 0)
511 			error = ifp->if_output(ifp, m, sintosa(dst), ro->ro_rt);
512 		else
513 			m_freem(m);
514 	}
515 
516 	if (error == 0)
517 		ipstat_inc(ips_fragmented);
518 
519 done:
520 	if (ro == &iproute && ro->ro_rt)
521 		rtfree(ro->ro_rt);
522 	if_put(ifp);
523 	return (error);
524 bad:
525 	m_freem(m0);
526 	goto done;
527 }
528 
529 #ifdef IPSEC
530 struct tdb *
531 ip_output_ipsec_lookup(struct mbuf *m, int hlen, int *error, struct inpcb *inp,
532     int ipsecflowinfo)
533 {
534 	struct m_tag *mtag;
535 	struct tdb_ident *tdbi;
536 	struct tdb *tdb;
537 
538 	/* Do we have any pending SAs to apply ? */
539 	tdb = ipsp_spd_lookup(m, AF_INET, hlen, error, IPSP_DIRECTION_OUT,
540 	    NULL, inp, ipsecflowinfo);
541 	if (tdb == NULL)
542 		return NULL;
543 	/* Loop detection */
544 	for (mtag = m_tag_first(m); mtag != NULL; mtag = m_tag_next(m, mtag)) {
545 		if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE)
546 			continue;
547 		tdbi = (struct tdb_ident *)(mtag + 1);
548 		if (tdbi->spi == tdb->tdb_spi &&
549 		    tdbi->proto == tdb->tdb_sproto &&
550 		    tdbi->rdomain == tdb->tdb_rdomain &&
551 		    !memcmp(&tdbi->dst, &tdb->tdb_dst,
552 		    sizeof(union sockaddr_union))) {
553 			/* no IPsec needed */
554 			return NULL;
555 		}
556 	}
557 	return tdb;
558 }
559 
560 int
561 ip_output_ipsec_send(struct tdb *tdb, struct mbuf *m, struct route *ro, int fwd)
562 {
563 #if NPF > 0
564 	struct ifnet *encif;
565 #endif
566 	struct ip *ip;
567 
568 #if NPF > 0
569 	/*
570 	 * Packet filter
571 	 */
572 	if ((encif = enc_getif(tdb->tdb_rdomain, tdb->tdb_tap)) == NULL ||
573 	    pf_test(AF_INET, fwd ? PF_FWD : PF_OUT, encif, &m) != PF_PASS) {
574 		m_freem(m);
575 		return EACCES;
576 	}
577 	if (m == NULL)
578 		return 0;
579 	/*
580 	 * PF_TAG_REROUTE handling or not...
581 	 * Packet is entering IPsec so the routing is
582 	 * already overruled by the IPsec policy.
583 	 * Until now the change was not reconsidered.
584 	 * What's the behaviour?
585 	 */
586 	in_proto_cksum_out(m, encif);
587 #endif
588 
589 	/* Check if we are allowed to fragment */
590 	ip = mtod(m, struct ip *);
591 	if (ip_mtudisc && (ip->ip_off & htons(IP_DF)) && tdb->tdb_mtu &&
592 	    ntohs(ip->ip_len) > tdb->tdb_mtu &&
593 	    tdb->tdb_mtutimeout > time_second) {
594 		struct rtentry *rt = NULL;
595 		int rt_mtucloned = 0;
596 		int transportmode = 0;
597 
598 		transportmode = (tdb->tdb_dst.sa.sa_family == AF_INET) &&
599 		    (tdb->tdb_dst.sin.sin_addr.s_addr == ip->ip_dst.s_addr);
600 
601 		/* Find a host route to store the mtu in */
602 		if (ro != NULL)
603 			rt = ro->ro_rt;
604 		/* but don't add a PMTU route for transport mode SAs */
605 		if (transportmode)
606 			rt = NULL;
607 		else if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0) {
608 			rt = icmp_mtudisc_clone(ip->ip_dst,
609 			    m->m_pkthdr.ph_rtableid);
610 			rt_mtucloned = 1;
611 		}
612 		DPRINTF(("%s: spi %08x mtu %d rt %p cloned %d\n", __func__,
613 		    ntohl(tdb->tdb_spi), tdb->tdb_mtu, rt, rt_mtucloned));
614 		if (rt != NULL) {
615 			rt->rt_mtu = tdb->tdb_mtu;
616 			if (ro && ro->ro_rt != NULL) {
617 				rtfree(ro->ro_rt);
618 				ro->ro_rt = rtalloc(&ro->ro_dst, RT_RESOLVE,
619 				    m->m_pkthdr.ph_rtableid);
620 			}
621 			if (rt_mtucloned)
622 				rtfree(rt);
623 		}
624 		ipsec_adjust_mtu(m, tdb->tdb_mtu);
625 		m_freem(m);
626 		return EMSGSIZE;
627 	}
628 
629 	/*
630 	 * Clear these -- they'll be set in the recursive invocation
631 	 * as needed.
632 	 */
633 	m->m_flags &= ~(M_MCAST | M_BCAST);
634 
635 	/* Callee frees mbuf */
636 	return ipsp_process_packet(m, tdb, AF_INET, 0);
637 }
638 #endif /* IPSEC */
639 
640 int
641 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
642 {
643 	struct ip *ip, *mhip;
644 	struct mbuf *m0;
645 	int len, hlen, off;
646 	int mhlen, firstlen;
647 	struct mbuf **mnext;
648 	int fragments = 0;
649 	int error = 0;
650 
651 	ip = mtod(m, struct ip *);
652 	hlen = ip->ip_hl << 2;
653 
654 	len = (mtu - hlen) &~ 7;
655 	if (len < 8) {
656 		m_freem(m);
657 		return (EMSGSIZE);
658 	}
659 
660 	/*
661 	 * If we are doing fragmentation, we can't defer TCP/UDP
662 	 * checksumming; compute the checksum and clear the flag.
663 	 */
664 	in_proto_cksum_out(m, NULL);
665 	firstlen = len;
666 	mnext = &m->m_nextpkt;
667 
668 	/*
669 	 * Loop through length of segment after first fragment,
670 	 * make new header and copy data of each part and link onto chain.
671 	 */
672 	m0 = m;
673 	mhlen = sizeof (struct ip);
674 	for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
675 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
676 		if (m == NULL) {
677 			ipstat_inc(ips_odropped);
678 			error = ENOBUFS;
679 			goto sendorfree;
680 		}
681 		*mnext = m;
682 		mnext = &m->m_nextpkt;
683 		m->m_data += max_linkhdr;
684 		mhip = mtod(m, struct ip *);
685 		*mhip = *ip;
686 		/* we must inherit MCAST/BCAST flags, routing table and prio */
687 		m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
688 		m->m_pkthdr.ph_rtableid = m0->m_pkthdr.ph_rtableid;
689 		m->m_pkthdr.pf.prio = m0->m_pkthdr.pf.prio;
690 		if (hlen > sizeof (struct ip)) {
691 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
692 			mhip->ip_hl = mhlen >> 2;
693 		}
694 		m->m_len = mhlen;
695 		mhip->ip_off = ((off - hlen) >> 3) +
696 		    (ntohs(ip->ip_off) & ~IP_MF);
697 		if (ip->ip_off & htons(IP_MF))
698 			mhip->ip_off |= IP_MF;
699 		if (off + len >= ntohs(ip->ip_len))
700 			len = ntohs(ip->ip_len) - off;
701 		else
702 			mhip->ip_off |= IP_MF;
703 		mhip->ip_len = htons((u_int16_t)(len + mhlen));
704 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
705 		if (m->m_next == 0) {
706 			ipstat_inc(ips_odropped);
707 			error = ENOBUFS;
708 			goto sendorfree;
709 		}
710 		m->m_pkthdr.len = mhlen + len;
711 		m->m_pkthdr.ph_ifidx = 0;
712 		mhip->ip_off = htons((u_int16_t)mhip->ip_off);
713 		mhip->ip_sum = 0;
714 		if ((ifp != NULL) &&
715 		    (ifp->if_capabilities & IFCAP_CSUM_IPv4) &&
716 		    (ifp->if_bridgeport == NULL))
717 			m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT;
718 		else {
719 			ipstat_inc(ips_outswcsum);
720 			mhip->ip_sum = in_cksum(m, mhlen);
721 		}
722 		ipstat_inc(ips_ofragments);
723 		fragments++;
724 	}
725 	/*
726 	 * Update first fragment by trimming what's been copied out
727 	 * and updating header, then send each fragment (in order).
728 	 */
729 	m = m0;
730 	m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
731 	m->m_pkthdr.len = hlen + firstlen;
732 	ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
733 	ip->ip_off |= htons(IP_MF);
734 	ip->ip_sum = 0;
735 	if ((ifp != NULL) &&
736 	    (ifp->if_capabilities & IFCAP_CSUM_IPv4) &&
737 	    (ifp->if_bridgeport == NULL))
738 		m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT;
739 	else {
740 		ipstat_inc(ips_outswcsum);
741 		ip->ip_sum = in_cksum(m, hlen);
742 	}
743 sendorfree:
744 	if (error) {
745 		for (m = m0; m; m = m0) {
746 			m0 = m->m_nextpkt;
747 			m->m_nextpkt = NULL;
748 			m_freem(m);
749 		}
750 	}
751 
752 	return (error);
753 }
754 
755 /*
756  * Insert IP options into preformed packet.
757  * Adjust IP destination as required for IP source routing,
758  * as indicated by a non-zero in_addr at the start of the options.
759  */
760 struct mbuf *
761 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
762 {
763 	struct ipoption *p = mtod(opt, struct ipoption *);
764 	struct mbuf *n;
765 	struct ip *ip = mtod(m, struct ip *);
766 	unsigned int optlen;
767 
768 	optlen = opt->m_len - sizeof(p->ipopt_dst);
769 	if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
770 		return (m);		/* XXX should fail */
771 	if (p->ipopt_dst.s_addr)
772 		ip->ip_dst = p->ipopt_dst;
773 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
774 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
775 		if (n == NULL)
776 			return (m);
777 		M_MOVE_HDR(n, m);
778 		n->m_pkthdr.len += optlen;
779 		m->m_len -= sizeof(struct ip);
780 		m->m_data += sizeof(struct ip);
781 		n->m_next = m;
782 		m = n;
783 		m->m_len = optlen + sizeof(struct ip);
784 		m->m_data += max_linkhdr;
785 		memcpy(mtod(m, caddr_t), ip, sizeof(struct ip));
786 	} else {
787 		m->m_data -= optlen;
788 		m->m_len += optlen;
789 		m->m_pkthdr.len += optlen;
790 		memmove(mtod(m, caddr_t), (caddr_t)ip, sizeof(struct ip));
791 	}
792 	ip = mtod(m, struct ip *);
793 	memcpy(ip + 1, p->ipopt_list, optlen);
794 	*phlen = sizeof(struct ip) + optlen;
795 	ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
796 	return (m);
797 }
798 
799 /*
800  * Copy options from ip to jp,
801  * omitting those not copied during fragmentation.
802  */
803 int
804 ip_optcopy(struct ip *ip, struct ip *jp)
805 {
806 	u_char *cp, *dp;
807 	int opt, optlen, cnt;
808 
809 	cp = (u_char *)(ip + 1);
810 	dp = (u_char *)(jp + 1);
811 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
812 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
813 		opt = cp[0];
814 		if (opt == IPOPT_EOL)
815 			break;
816 		if (opt == IPOPT_NOP) {
817 			/* Preserve for IP mcast tunnel's LSRR alignment. */
818 			*dp++ = IPOPT_NOP;
819 			optlen = 1;
820 			continue;
821 		}
822 #ifdef DIAGNOSTIC
823 		if (cnt < IPOPT_OLEN + sizeof(*cp))
824 			panic("malformed IPv4 option passed to ip_optcopy");
825 #endif
826 		optlen = cp[IPOPT_OLEN];
827 #ifdef DIAGNOSTIC
828 		if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
829 			panic("malformed IPv4 option passed to ip_optcopy");
830 #endif
831 		/* bogus lengths should have been caught by ip_dooptions */
832 		if (optlen > cnt)
833 			optlen = cnt;
834 		if (IPOPT_COPIED(opt)) {
835 			memcpy(dp, cp, optlen);
836 			dp += optlen;
837 		}
838 	}
839 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
840 		*dp++ = IPOPT_EOL;
841 	return (optlen);
842 }
843 
844 /*
845  * IP socket option processing.
846  */
847 int
848 ip_ctloutput(int op, struct socket *so, int level, int optname,
849     struct mbuf *m)
850 {
851 	struct inpcb *inp = sotoinpcb(so);
852 	int optval = 0;
853 	struct proc *p = curproc; /* XXX */
854 	int error = 0;
855 	u_int rtid = 0;
856 
857 	if (level != IPPROTO_IP) {
858 		error = EINVAL;
859 	} else switch (op) {
860 	case PRCO_SETOPT:
861 		switch (optname) {
862 		case IP_OPTIONS:
863 			return (ip_pcbopts(&inp->inp_options, m));
864 
865 		case IP_TOS:
866 		case IP_TTL:
867 		case IP_MINTTL:
868 		case IP_RECVOPTS:
869 		case IP_RECVRETOPTS:
870 		case IP_RECVDSTADDR:
871 		case IP_RECVIF:
872 		case IP_RECVTTL:
873 		case IP_RECVDSTPORT:
874 		case IP_RECVRTABLE:
875 		case IP_IPSECFLOWINFO:
876 			if (m == NULL || m->m_len != sizeof(int))
877 				error = EINVAL;
878 			else {
879 				optval = *mtod(m, int *);
880 				switch (optname) {
881 
882 				case IP_TOS:
883 					inp->inp_ip.ip_tos = optval;
884 					break;
885 
886 				case IP_TTL:
887 					if (optval > 0 && optval <= MAXTTL)
888 						inp->inp_ip.ip_ttl = optval;
889 					else if (optval == -1)
890 						inp->inp_ip.ip_ttl = ip_defttl;
891 					else
892 						error = EINVAL;
893 					break;
894 
895 				case IP_MINTTL:
896 					if (optval >= 0 && optval <= MAXTTL)
897 						inp->inp_ip_minttl = optval;
898 					else
899 						error = EINVAL;
900 					break;
901 #define	OPTSET(bit) \
902 	if (optval) \
903 		inp->inp_flags |= bit; \
904 	else \
905 		inp->inp_flags &= ~bit;
906 
907 				case IP_RECVOPTS:
908 					OPTSET(INP_RECVOPTS);
909 					break;
910 
911 				case IP_RECVRETOPTS:
912 					OPTSET(INP_RECVRETOPTS);
913 					break;
914 
915 				case IP_RECVDSTADDR:
916 					OPTSET(INP_RECVDSTADDR);
917 					break;
918 				case IP_RECVIF:
919 					OPTSET(INP_RECVIF);
920 					break;
921 				case IP_RECVTTL:
922 					OPTSET(INP_RECVTTL);
923 					break;
924 				case IP_RECVDSTPORT:
925 					OPTSET(INP_RECVDSTPORT);
926 					break;
927 				case IP_RECVRTABLE:
928 					OPTSET(INP_RECVRTABLE);
929 					break;
930 				case IP_IPSECFLOWINFO:
931 					OPTSET(INP_IPSECFLOWINFO);
932 					break;
933 				}
934 			}
935 			break;
936 #undef OPTSET
937 
938 		case IP_MULTICAST_IF:
939 		case IP_MULTICAST_TTL:
940 		case IP_MULTICAST_LOOP:
941 		case IP_ADD_MEMBERSHIP:
942 		case IP_DROP_MEMBERSHIP:
943 			error = ip_setmoptions(optname, &inp->inp_moptions, m,
944 			    inp->inp_rtableid);
945 			break;
946 
947 		case IP_PORTRANGE:
948 			if (m == NULL || m->m_len != sizeof(int))
949 				error = EINVAL;
950 			else {
951 				optval = *mtod(m, int *);
952 
953 				switch (optval) {
954 
955 				case IP_PORTRANGE_DEFAULT:
956 					inp->inp_flags &= ~(INP_LOWPORT);
957 					inp->inp_flags &= ~(INP_HIGHPORT);
958 					break;
959 
960 				case IP_PORTRANGE_HIGH:
961 					inp->inp_flags &= ~(INP_LOWPORT);
962 					inp->inp_flags |= INP_HIGHPORT;
963 					break;
964 
965 				case IP_PORTRANGE_LOW:
966 					inp->inp_flags &= ~(INP_HIGHPORT);
967 					inp->inp_flags |= INP_LOWPORT;
968 					break;
969 
970 				default:
971 
972 					error = EINVAL;
973 					break;
974 				}
975 			}
976 			break;
977 		case IP_AUTH_LEVEL:
978 		case IP_ESP_TRANS_LEVEL:
979 		case IP_ESP_NETWORK_LEVEL:
980 		case IP_IPCOMP_LEVEL:
981 #ifndef IPSEC
982 			error = EOPNOTSUPP;
983 #else
984 			if (m == NULL || m->m_len != sizeof(int)) {
985 				error = EINVAL;
986 				break;
987 			}
988 			optval = *mtod(m, int *);
989 
990 			if (optval < IPSEC_LEVEL_BYPASS ||
991 			    optval > IPSEC_LEVEL_UNIQUE) {
992 				error = EINVAL;
993 				break;
994 			}
995 
996 			switch (optname) {
997 			case IP_AUTH_LEVEL:
998 				if (optval < IPSEC_AUTH_LEVEL_DEFAULT &&
999 				    suser(p)) {
1000 					error = EACCES;
1001 					break;
1002 				}
1003 				inp->inp_seclevel[SL_AUTH] = optval;
1004 				break;
1005 
1006 			case IP_ESP_TRANS_LEVEL:
1007 				if (optval < IPSEC_ESP_TRANS_LEVEL_DEFAULT &&
1008 				    suser(p)) {
1009 					error = EACCES;
1010 					break;
1011 				}
1012 				inp->inp_seclevel[SL_ESP_TRANS] = optval;
1013 				break;
1014 
1015 			case IP_ESP_NETWORK_LEVEL:
1016 				if (optval < IPSEC_ESP_NETWORK_LEVEL_DEFAULT &&
1017 				    suser(p)) {
1018 					error = EACCES;
1019 					break;
1020 				}
1021 				inp->inp_seclevel[SL_ESP_NETWORK] = optval;
1022 				break;
1023 			case IP_IPCOMP_LEVEL:
1024 				if (optval < IPSEC_IPCOMP_LEVEL_DEFAULT &&
1025 				    suser(p)) {
1026 					error = EACCES;
1027 					break;
1028 				}
1029 				inp->inp_seclevel[SL_IPCOMP] = optval;
1030 				break;
1031 			}
1032 #endif
1033 			break;
1034 
1035 		case IP_IPSEC_LOCAL_ID:
1036 		case IP_IPSEC_REMOTE_ID:
1037 			error = EOPNOTSUPP;
1038 			break;
1039 		case SO_RTABLE:
1040 			if (m == NULL || m->m_len < sizeof(u_int)) {
1041 				error = EINVAL;
1042 				break;
1043 			}
1044 			rtid = *mtod(m, u_int *);
1045 			if (inp->inp_rtableid == rtid)
1046 				break;
1047 			/* needs privileges to switch when already set */
1048 			if (p->p_p->ps_rtableid != rtid &&
1049 			    p->p_p->ps_rtableid != 0 &&
1050 			    (error = suser(p)) != 0)
1051 				break;
1052 			/* table must exist */
1053 			if (!rtable_exists(rtid)) {
1054 				error = EINVAL;
1055 				break;
1056 			}
1057 			if (inp->inp_lport) {
1058 				error = EBUSY;
1059 				break;
1060 			}
1061 			inp->inp_rtableid = rtid;
1062 			in_pcbrehash(inp);
1063 			break;
1064 		case IP_PIPEX:
1065 			if (m != NULL && m->m_len == sizeof(int))
1066 				inp->inp_pipex = *mtod(m, int *);
1067 			else
1068 				error = EINVAL;
1069 			break;
1070 
1071 		default:
1072 			error = ENOPROTOOPT;
1073 			break;
1074 		}
1075 		break;
1076 
1077 	case PRCO_GETOPT:
1078 		switch (optname) {
1079 		case IP_OPTIONS:
1080 		case IP_RETOPTS:
1081 			if (inp->inp_options) {
1082 				m->m_len = inp->inp_options->m_len;
1083 				memcpy(mtod(m, caddr_t),
1084 				    mtod(inp->inp_options, caddr_t), m->m_len);
1085 			} else
1086 				m->m_len = 0;
1087 			break;
1088 
1089 		case IP_TOS:
1090 		case IP_TTL:
1091 		case IP_MINTTL:
1092 		case IP_RECVOPTS:
1093 		case IP_RECVRETOPTS:
1094 		case IP_RECVDSTADDR:
1095 		case IP_RECVIF:
1096 		case IP_RECVTTL:
1097 		case IP_RECVDSTPORT:
1098 		case IP_RECVRTABLE:
1099 		case IP_IPSECFLOWINFO:
1100 		case IP_IPDEFTTL:
1101 			m->m_len = sizeof(int);
1102 			switch (optname) {
1103 
1104 			case IP_TOS:
1105 				optval = inp->inp_ip.ip_tos;
1106 				break;
1107 
1108 			case IP_TTL:
1109 				optval = inp->inp_ip.ip_ttl;
1110 				break;
1111 
1112 			case IP_MINTTL:
1113 				optval = inp->inp_ip_minttl;
1114 				break;
1115 
1116 			case IP_IPDEFTTL:
1117 				optval = ip_defttl;
1118 				break;
1119 
1120 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1121 
1122 			case IP_RECVOPTS:
1123 				optval = OPTBIT(INP_RECVOPTS);
1124 				break;
1125 
1126 			case IP_RECVRETOPTS:
1127 				optval = OPTBIT(INP_RECVRETOPTS);
1128 				break;
1129 
1130 			case IP_RECVDSTADDR:
1131 				optval = OPTBIT(INP_RECVDSTADDR);
1132 				break;
1133 			case IP_RECVIF:
1134 				optval = OPTBIT(INP_RECVIF);
1135 				break;
1136 			case IP_RECVTTL:
1137 				optval = OPTBIT(INP_RECVTTL);
1138 				break;
1139 			case IP_RECVDSTPORT:
1140 				optval = OPTBIT(INP_RECVDSTPORT);
1141 				break;
1142 			case IP_RECVRTABLE:
1143 				optval = OPTBIT(INP_RECVRTABLE);
1144 				break;
1145 			case IP_IPSECFLOWINFO:
1146 				optval = OPTBIT(INP_IPSECFLOWINFO);
1147 				break;
1148 			}
1149 			*mtod(m, int *) = optval;
1150 			break;
1151 
1152 		case IP_MULTICAST_IF:
1153 		case IP_MULTICAST_TTL:
1154 		case IP_MULTICAST_LOOP:
1155 		case IP_ADD_MEMBERSHIP:
1156 		case IP_DROP_MEMBERSHIP:
1157 			error = ip_getmoptions(optname, inp->inp_moptions, m);
1158 			break;
1159 
1160 		case IP_PORTRANGE:
1161 			m->m_len = sizeof(int);
1162 
1163 			if (inp->inp_flags & INP_HIGHPORT)
1164 				optval = IP_PORTRANGE_HIGH;
1165 			else if (inp->inp_flags & INP_LOWPORT)
1166 				optval = IP_PORTRANGE_LOW;
1167 			else
1168 				optval = 0;
1169 
1170 			*mtod(m, int *) = optval;
1171 			break;
1172 
1173 		case IP_AUTH_LEVEL:
1174 		case IP_ESP_TRANS_LEVEL:
1175 		case IP_ESP_NETWORK_LEVEL:
1176 		case IP_IPCOMP_LEVEL:
1177 #ifndef IPSEC
1178 			m->m_len = sizeof(int);
1179 			*mtod(m, int *) = IPSEC_LEVEL_NONE;
1180 #else
1181 			m->m_len = sizeof(int);
1182 			switch (optname) {
1183 			case IP_AUTH_LEVEL:
1184 				optval = inp->inp_seclevel[SL_AUTH];
1185 				break;
1186 
1187 			case IP_ESP_TRANS_LEVEL:
1188 				optval = inp->inp_seclevel[SL_ESP_TRANS];
1189 				break;
1190 
1191 			case IP_ESP_NETWORK_LEVEL:
1192 				optval = inp->inp_seclevel[SL_ESP_NETWORK];
1193 				break;
1194 			case IP_IPCOMP_LEVEL:
1195 				optval = inp->inp_seclevel[SL_IPCOMP];
1196 				break;
1197 			}
1198 			*mtod(m, int *) = optval;
1199 #endif
1200 			break;
1201 		case IP_IPSEC_LOCAL_ID:
1202 		case IP_IPSEC_REMOTE_ID:
1203 			error = EOPNOTSUPP;
1204 			break;
1205 		case SO_RTABLE:
1206 			m->m_len = sizeof(u_int);
1207 			*mtod(m, u_int *) = inp->inp_rtableid;
1208 			break;
1209 		case IP_PIPEX:
1210 			m->m_len = sizeof(int);
1211 			*mtod(m, int *) = inp->inp_pipex;
1212 			break;
1213 		default:
1214 			error = ENOPROTOOPT;
1215 			break;
1216 		}
1217 		break;
1218 	}
1219 	return (error);
1220 }
1221 
1222 /*
1223  * Set up IP options in pcb for insertion in output packets.
1224  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1225  * with destination address if source routed.
1226  */
1227 int
1228 ip_pcbopts(struct mbuf **pcbopt, struct mbuf *m)
1229 {
1230 	int cnt, optlen;
1231 	u_char *cp;
1232 	u_char opt;
1233 
1234 	/* turn off any old options */
1235 	m_free(*pcbopt);
1236 	*pcbopt = 0;
1237 	if (m == NULL || m->m_len == 0) {
1238 		/*
1239 		 * Only turning off any previous options.
1240 		 */
1241 		return (0);
1242 	}
1243 
1244 	if (m->m_len % sizeof(int32_t))
1245 		return (EINVAL);
1246 
1247 	/*
1248 	 * IP first-hop destination address will be stored before
1249 	 * actual options; move other options back
1250 	 * and clear it when none present.
1251 	 */
1252 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1253 		return (EINVAL);
1254 	cnt = m->m_len;
1255 	m->m_len += sizeof(struct in_addr);
1256 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1257 	memmove((caddr_t)cp, mtod(m, caddr_t), (unsigned)cnt);
1258 	memset(mtod(m, caddr_t), 0, sizeof(struct in_addr));
1259 
1260 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1261 		opt = cp[IPOPT_OPTVAL];
1262 		if (opt == IPOPT_EOL)
1263 			break;
1264 		if (opt == IPOPT_NOP)
1265 			optlen = 1;
1266 		else {
1267 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1268 				return (EINVAL);
1269 			optlen = cp[IPOPT_OLEN];
1270 			if (optlen < IPOPT_OLEN  + sizeof(*cp) || optlen > cnt)
1271 				return (EINVAL);
1272 		}
1273 		switch (opt) {
1274 
1275 		default:
1276 			break;
1277 
1278 		case IPOPT_LSRR:
1279 		case IPOPT_SSRR:
1280 			/*
1281 			 * user process specifies route as:
1282 			 *	->A->B->C->D
1283 			 * D must be our final destination (but we can't
1284 			 * check that since we may not have connected yet).
1285 			 * A is first hop destination, which doesn't appear in
1286 			 * actual IP option, but is stored before the options.
1287 			 */
1288 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1289 				return (EINVAL);
1290 			m->m_len -= sizeof(struct in_addr);
1291 			cnt -= sizeof(struct in_addr);
1292 			optlen -= sizeof(struct in_addr);
1293 			cp[IPOPT_OLEN] = optlen;
1294 			/*
1295 			 * Move first hop before start of options.
1296 			 */
1297 			memcpy(mtod(m, caddr_t), &cp[IPOPT_OFFSET+1],
1298 			    sizeof(struct in_addr));
1299 			/*
1300 			 * Then copy rest of options back
1301 			 * to close up the deleted entry.
1302 			 */
1303 			memmove((caddr_t)&cp[IPOPT_OFFSET+1],
1304 			    (caddr_t)(&cp[IPOPT_OFFSET+1] +
1305 			    sizeof(struct in_addr)),
1306 			    (unsigned)cnt - (IPOPT_OFFSET+1));
1307 			break;
1308 		}
1309 	}
1310 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1311 		return (EINVAL);
1312 	*pcbopt = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1313 	if (*pcbopt == NULL)
1314 		return (ENOBUFS);
1315 
1316 	return (0);
1317 }
1318 
1319 /*
1320  * Set the IP multicast options in response to user setsockopt().
1321  */
1322 int
1323 ip_setmoptions(int optname, struct ip_moptions **imop, struct mbuf *m,
1324     u_int rtableid)
1325 {
1326 	struct in_addr addr;
1327 	struct in_ifaddr *ia;
1328 	struct ip_mreq *mreq;
1329 	struct ifnet *ifp = NULL;
1330 	struct ip_moptions *imo = *imop;
1331 	struct in_multi **immp;
1332 	struct rtentry *rt;
1333 	struct sockaddr_in sin;
1334 	int i, error = 0;
1335 	u_char loop;
1336 
1337 	if (imo == NULL) {
1338 		/*
1339 		 * No multicast option buffer attached to the pcb;
1340 		 * allocate one and initialize to default values.
1341 		 */
1342 		imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK|M_ZERO);
1343 		immp = (struct in_multi **)malloc(
1344 		    (sizeof(*immp) * IP_MIN_MEMBERSHIPS), M_IPMOPTS,
1345 		    M_WAITOK|M_ZERO);
1346 		*imop = imo;
1347 		imo->imo_ifidx = 0;
1348 		imo->imo_ttl = IP_DEFAULT_MULTICAST_TTL;
1349 		imo->imo_loop = IP_DEFAULT_MULTICAST_LOOP;
1350 		imo->imo_num_memberships = 0;
1351 		imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1352 		imo->imo_membership = immp;
1353 	}
1354 
1355 	switch (optname) {
1356 
1357 	case IP_MULTICAST_IF:
1358 		/*
1359 		 * Select the interface for outgoing multicast packets.
1360 		 */
1361 		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
1362 			error = EINVAL;
1363 			break;
1364 		}
1365 		addr = *(mtod(m, struct in_addr *));
1366 		/*
1367 		 * INADDR_ANY is used to remove a previous selection.
1368 		 * When no interface is selected, a default one is
1369 		 * chosen every time a multicast packet is sent.
1370 		 */
1371 		if (addr.s_addr == INADDR_ANY) {
1372 			imo->imo_ifidx = 0;
1373 			break;
1374 		}
1375 		/*
1376 		 * The selected interface is identified by its local
1377 		 * IP address.  Find the interface and confirm that
1378 		 * it supports multicasting.
1379 		 */
1380 		memset(&sin, 0, sizeof(sin));
1381 		sin.sin_len = sizeof(sin);
1382 		sin.sin_family = AF_INET;
1383 		sin.sin_addr = addr;
1384 		ia = ifatoia(ifa_ifwithaddr(sintosa(&sin), rtableid));
1385 		if (ia == NULL ||
1386 		    (ia->ia_ifp->if_flags & IFF_MULTICAST) == 0) {
1387 			error = EADDRNOTAVAIL;
1388 			break;
1389 		}
1390 		imo->imo_ifidx = ia->ia_ifp->if_index;
1391 		break;
1392 
1393 	case IP_MULTICAST_TTL:
1394 		/*
1395 		 * Set the IP time-to-live for outgoing multicast packets.
1396 		 */
1397 		if (m == NULL || m->m_len != 1) {
1398 			error = EINVAL;
1399 			break;
1400 		}
1401 		imo->imo_ttl = *(mtod(m, u_char *));
1402 		break;
1403 
1404 	case IP_MULTICAST_LOOP:
1405 		/*
1406 		 * Set the loopback flag for outgoing multicast packets.
1407 		 * Must be zero or one.
1408 		 */
1409 		if (m == NULL || m->m_len != 1 ||
1410 		   (loop = *(mtod(m, u_char *))) > 1) {
1411 			error = EINVAL;
1412 			break;
1413 		}
1414 		imo->imo_loop = loop;
1415 		break;
1416 
1417 	case IP_ADD_MEMBERSHIP:
1418 		/*
1419 		 * Add a multicast group membership.
1420 		 * Group must be a valid IP multicast address.
1421 		 */
1422 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1423 			error = EINVAL;
1424 			break;
1425 		}
1426 		mreq = mtod(m, struct ip_mreq *);
1427 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1428 			error = EINVAL;
1429 			break;
1430 		}
1431 		/*
1432 		 * If no interface address was provided, use the interface of
1433 		 * the route to the given multicast address.
1434 		 */
1435 		if (mreq->imr_interface.s_addr == INADDR_ANY) {
1436 			memset(&sin, 0, sizeof(sin));
1437 			sin.sin_len = sizeof(sin);
1438 			sin.sin_family = AF_INET;
1439 			sin.sin_addr = mreq->imr_multiaddr;
1440 			rt = rtalloc(sintosa(&sin), RT_RESOLVE, rtableid);
1441 			if (!rtisvalid(rt)) {
1442 				rtfree(rt);
1443 				error = EADDRNOTAVAIL;
1444 				break;
1445 			}
1446 		} else {
1447 			memset(&sin, 0, sizeof(sin));
1448 			sin.sin_len = sizeof(sin);
1449 			sin.sin_family = AF_INET;
1450 			sin.sin_addr = mreq->imr_interface;
1451 			rt = rtalloc(sintosa(&sin), 0, rtableid);
1452 			if (!rtisvalid(rt) || !ISSET(rt->rt_flags, RTF_LOCAL)) {
1453 				rtfree(rt);
1454 				error = EADDRNOTAVAIL;
1455 				break;
1456 			}
1457 		}
1458 		ifp = if_get(rt->rt_ifidx);
1459 		rtfree(rt);
1460 
1461 		/*
1462 		 * See if we found an interface, and confirm that it
1463 		 * supports multicast.
1464 		 */
1465 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1466 			error = EADDRNOTAVAIL;
1467 			if_put(ifp);
1468 			break;
1469 		}
1470 		/*
1471 		 * See if the membership already exists or if all the
1472 		 * membership slots are full.
1473 		 */
1474 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1475 			if (imo->imo_membership[i]->inm_ifidx
1476 						== ifp->if_index &&
1477 			    imo->imo_membership[i]->inm_addr.s_addr
1478 						== mreq->imr_multiaddr.s_addr)
1479 				break;
1480 		}
1481 		if (i < imo->imo_num_memberships) {
1482 			error = EADDRINUSE;
1483 			if_put(ifp);
1484 			break;
1485 		}
1486 		if (imo->imo_num_memberships == imo->imo_max_memberships) {
1487 			struct in_multi **nmships, **omships;
1488 			size_t newmax;
1489 			/*
1490 			 * Resize the vector to next power-of-two minus 1. If the
1491 			 * size would exceed the maximum then we know we've really
1492 			 * run out of entries. Otherwise, we reallocate the vector.
1493 			 */
1494 			nmships = NULL;
1495 			omships = imo->imo_membership;
1496 			newmax = ((imo->imo_max_memberships + 1) * 2) - 1;
1497 			if (newmax <= IP_MAX_MEMBERSHIPS) {
1498 				nmships = (struct in_multi **)mallocarray(
1499 				    newmax, sizeof(*nmships), M_IPMOPTS,
1500 				    M_NOWAIT|M_ZERO);
1501 				if (nmships != NULL) {
1502 					memcpy(nmships, omships,
1503 					    sizeof(*omships) *
1504 					    imo->imo_max_memberships);
1505 					free(omships, M_IPMOPTS,
1506 					    sizeof(*omships) *
1507 					    imo->imo_max_memberships);
1508 					imo->imo_membership = nmships;
1509 					imo->imo_max_memberships = newmax;
1510 				}
1511 			}
1512 			if (nmships == NULL) {
1513 				error = ENOBUFS;
1514 				if_put(ifp);
1515 				break;
1516 			}
1517 		}
1518 		/*
1519 		 * Everything looks good; add a new record to the multicast
1520 		 * address list for the given interface.
1521 		 */
1522 		if ((imo->imo_membership[i] =
1523 		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1524 			error = ENOBUFS;
1525 			if_put(ifp);
1526 			break;
1527 		}
1528 		++imo->imo_num_memberships;
1529 		if_put(ifp);
1530 		break;
1531 
1532 	case IP_DROP_MEMBERSHIP:
1533 		/*
1534 		 * Drop a multicast group membership.
1535 		 * Group must be a valid IP multicast address.
1536 		 */
1537 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1538 			error = EINVAL;
1539 			break;
1540 		}
1541 		mreq = mtod(m, struct ip_mreq *);
1542 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1543 			error = EINVAL;
1544 			break;
1545 		}
1546 		/*
1547 		 * If an interface address was specified, get a pointer
1548 		 * to its ifnet structure.
1549 		 */
1550 		if (mreq->imr_interface.s_addr == INADDR_ANY)
1551 			ifp = NULL;
1552 		else {
1553 			memset(&sin, 0, sizeof(sin));
1554 			sin.sin_len = sizeof(sin);
1555 			sin.sin_family = AF_INET;
1556 			sin.sin_addr = mreq->imr_interface;
1557 			ia = ifatoia(ifa_ifwithaddr(sintosa(&sin), rtableid));
1558 			if (ia == NULL) {
1559 				error = EADDRNOTAVAIL;
1560 				break;
1561 			}
1562 			ifp = ia->ia_ifp;
1563 		}
1564 		/*
1565 		 * Find the membership in the membership array.
1566 		 */
1567 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1568 			if ((ifp == NULL ||
1569 			    imo->imo_membership[i]->inm_ifidx ==
1570 			        ifp->if_index) &&
1571 			     imo->imo_membership[i]->inm_addr.s_addr ==
1572 			     mreq->imr_multiaddr.s_addr)
1573 				break;
1574 		}
1575 		if (i == imo->imo_num_memberships) {
1576 			error = EADDRNOTAVAIL;
1577 			break;
1578 		}
1579 		/*
1580 		 * Give up the multicast address record to which the
1581 		 * membership points.
1582 		 */
1583 		in_delmulti(imo->imo_membership[i]);
1584 		/*
1585 		 * Remove the gap in the membership array.
1586 		 */
1587 		for (++i; i < imo->imo_num_memberships; ++i)
1588 			imo->imo_membership[i-1] = imo->imo_membership[i];
1589 		--imo->imo_num_memberships;
1590 		break;
1591 
1592 	default:
1593 		error = EOPNOTSUPP;
1594 		break;
1595 	}
1596 
1597 	/*
1598 	 * If all options have default values, no need to keep the data.
1599 	 */
1600 	if (imo->imo_ifidx == 0 &&
1601 	    imo->imo_ttl == IP_DEFAULT_MULTICAST_TTL &&
1602 	    imo->imo_loop == IP_DEFAULT_MULTICAST_LOOP &&
1603 	    imo->imo_num_memberships == 0) {
1604 		free(imo->imo_membership , M_IPMOPTS, 0);
1605 		free(*imop, M_IPMOPTS, sizeof(**imop));
1606 		*imop = NULL;
1607 	}
1608 
1609 	return (error);
1610 }
1611 
1612 /*
1613  * Return the IP multicast options in response to user getsockopt().
1614  */
1615 int
1616 ip_getmoptions(int optname, struct ip_moptions *imo, struct mbuf *m)
1617 {
1618 	u_char *ttl;
1619 	u_char *loop;
1620 	struct in_addr *addr;
1621 	struct in_ifaddr *ia;
1622 	struct ifnet *ifp;
1623 
1624 	switch (optname) {
1625 
1626 	case IP_MULTICAST_IF:
1627 		addr = mtod(m, struct in_addr *);
1628 		m->m_len = sizeof(struct in_addr);
1629 		if (imo == NULL || (ifp = if_get(imo->imo_ifidx)) == NULL)
1630 			addr->s_addr = INADDR_ANY;
1631 		else {
1632 			IFP_TO_IA(ifp, ia);
1633 			if_put(ifp);
1634 			addr->s_addr = (ia == NULL) ? INADDR_ANY
1635 					: ia->ia_addr.sin_addr.s_addr;
1636 		}
1637 		return (0);
1638 
1639 	case IP_MULTICAST_TTL:
1640 		ttl = mtod(m, u_char *);
1641 		m->m_len = 1;
1642 		*ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1643 				     : imo->imo_ttl;
1644 		return (0);
1645 
1646 	case IP_MULTICAST_LOOP:
1647 		loop = mtod(m, u_char *);
1648 		m->m_len = 1;
1649 		*loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1650 				      : imo->imo_loop;
1651 		return (0);
1652 
1653 	default:
1654 		return (EOPNOTSUPP);
1655 	}
1656 }
1657 
1658 /*
1659  * Discard the IP multicast options.
1660  */
1661 void
1662 ip_freemoptions(struct ip_moptions *imo)
1663 {
1664 	int i;
1665 
1666 	if (imo != NULL) {
1667 		for (i = 0; i < imo->imo_num_memberships; ++i)
1668 			in_delmulti(imo->imo_membership[i]);
1669 		free(imo->imo_membership, M_IPMOPTS, 0);
1670 		free(imo, M_IPMOPTS, sizeof(*imo));
1671 	}
1672 }
1673 
1674 /*
1675  * Routine called from ip_output() to loop back a copy of an IP multicast
1676  * packet to the input queue of a specified interface.
1677  */
1678 void
1679 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst)
1680 {
1681 	struct ip *ip;
1682 	struct mbuf *copym;
1683 
1684 	copym = m_dup_pkt(m, max_linkhdr, M_DONTWAIT);
1685 	if (copym != NULL) {
1686 		/*
1687 		 * We don't bother to fragment if the IP length is greater
1688 		 * than the interface's MTU.  Can this possibly matter?
1689 		 */
1690 		ip = mtod(copym, struct ip *);
1691 		ip->ip_sum = 0;
1692 		ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1693 		if_input_local(ifp, copym, dst->sin_family);
1694 	}
1695 }
1696 
1697 /*
1698  *	Compute significant parts of the IPv4 checksum pseudo-header
1699  *	for use in a delayed TCP/UDP checksum calculation.
1700  */
1701 static __inline u_int16_t __attribute__((__unused__))
1702 in_cksum_phdr(u_int32_t src, u_int32_t dst, u_int32_t lenproto)
1703 {
1704 	u_int32_t sum;
1705 
1706 	sum = lenproto +
1707 	      (u_int16_t)(src >> 16) +
1708 	      (u_int16_t)(src /*& 0xffff*/) +
1709 	      (u_int16_t)(dst >> 16) +
1710 	      (u_int16_t)(dst /*& 0xffff*/);
1711 
1712 	sum = (u_int16_t)(sum >> 16) + (u_int16_t)(sum /*& 0xffff*/);
1713 
1714 	if (sum > 0xffff)
1715 		sum -= 0xffff;
1716 
1717 	return (sum);
1718 }
1719 
1720 /*
1721  * Process a delayed payload checksum calculation.
1722  */
1723 void
1724 in_delayed_cksum(struct mbuf *m)
1725 {
1726 	struct ip *ip;
1727 	u_int16_t csum, offset;
1728 
1729 	ip = mtod(m, struct ip *);
1730 	offset = ip->ip_hl << 2;
1731 	csum = in4_cksum(m, 0, offset, m->m_pkthdr.len - offset);
1732 	if (csum == 0 && ip->ip_p == IPPROTO_UDP)
1733 		csum = 0xffff;
1734 
1735 	switch (ip->ip_p) {
1736 	case IPPROTO_TCP:
1737 		offset += offsetof(struct tcphdr, th_sum);
1738 		break;
1739 
1740 	case IPPROTO_UDP:
1741 		offset += offsetof(struct udphdr, uh_sum);
1742 		break;
1743 
1744 	case IPPROTO_ICMP:
1745 		offset += offsetof(struct icmp, icmp_cksum);
1746 		break;
1747 
1748 	default:
1749 		return;
1750 	}
1751 
1752 	if ((offset + sizeof(u_int16_t)) > m->m_len)
1753 		m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT);
1754 	else
1755 		*(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
1756 }
1757 
1758 void
1759 in_proto_cksum_out(struct mbuf *m, struct ifnet *ifp)
1760 {
1761 	struct ip *ip = mtod(m, struct ip *);
1762 
1763 	/* some hw and in_delayed_cksum need the pseudo header cksum */
1764 	if (m->m_pkthdr.csum_flags &
1765 	    (M_TCP_CSUM_OUT|M_UDP_CSUM_OUT|M_ICMP_CSUM_OUT)) {
1766 		u_int16_t csum = 0, offset;
1767 
1768 		offset = ip->ip_hl << 2;
1769 		if (m->m_pkthdr.csum_flags & (M_TCP_CSUM_OUT|M_UDP_CSUM_OUT))
1770 			csum = in_cksum_phdr(ip->ip_src.s_addr,
1771 			    ip->ip_dst.s_addr, htonl(ntohs(ip->ip_len) -
1772 			    offset + ip->ip_p));
1773 		if (ip->ip_p == IPPROTO_TCP)
1774 			offset += offsetof(struct tcphdr, th_sum);
1775 		else if (ip->ip_p == IPPROTO_UDP)
1776 			offset += offsetof(struct udphdr, uh_sum);
1777 		else if (ip->ip_p == IPPROTO_ICMP)
1778 			offset += offsetof(struct icmp, icmp_cksum);
1779 		if ((offset + sizeof(u_int16_t)) > m->m_len)
1780 			m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT);
1781 		else
1782 			*(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
1783 	}
1784 
1785 	if (m->m_pkthdr.csum_flags & M_TCP_CSUM_OUT) {
1786 		if (!ifp || !(ifp->if_capabilities & IFCAP_CSUM_TCPv4) ||
1787 		    ip->ip_hl != 5 || ifp->if_bridgeport != NULL) {
1788 			tcpstat_inc(tcps_outswcsum);
1789 			in_delayed_cksum(m);
1790 			m->m_pkthdr.csum_flags &= ~M_TCP_CSUM_OUT; /* Clear */
1791 		}
1792 	} else if (m->m_pkthdr.csum_flags & M_UDP_CSUM_OUT) {
1793 		if (!ifp || !(ifp->if_capabilities & IFCAP_CSUM_UDPv4) ||
1794 		    ip->ip_hl != 5 || ifp->if_bridgeport != NULL) {
1795 			udpstat_inc(udps_outswcsum);
1796 			in_delayed_cksum(m);
1797 			m->m_pkthdr.csum_flags &= ~M_UDP_CSUM_OUT; /* Clear */
1798 		}
1799 	} else if (m->m_pkthdr.csum_flags & M_ICMP_CSUM_OUT) {
1800 		in_delayed_cksum(m);
1801 		m->m_pkthdr.csum_flags &= ~M_ICMP_CSUM_OUT; /* Clear */
1802 	}
1803 }
1804