ip_output.c revision 1.328 1 /* $NetBSD: ip_output.c,v 1.328 2025/06/11 02:45:06 ozaki-r Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Public Access Networks Corporation ("Panix"). It was developed under
38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
60 */
61
62 /*
63 * Copyright (c) 1982, 1986, 1988, 1990, 1993
64 * The Regents of the University of California. All rights reserved.
65 *
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
68 * are met:
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 * notice, this list of conditions and the following disclaimer in the
73 * documentation and/or other materials provided with the distribution.
74 * 3. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
77 *
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 * SUCH DAMAGE.
89 *
90 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
91 */
92
93 #include <sys/cdefs.h>
94 __KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.328 2025/06/11 02:45:06 ozaki-r Exp $");
95
96 #ifdef _KERNEL_OPT
97 #include "opt_inet.h"
98 #include "opt_ipsec.h"
99 #include "opt_mrouting.h"
100 #include "opt_net_mpsafe.h"
101 #include "opt_mpls.h"
102 #endif
103
104 #include "arp.h"
105
106 #include <sys/param.h>
107 #include <sys/kmem.h>
108 #include <sys/mbuf.h>
109 #include <sys/socket.h>
110 #include <sys/socketvar.h>
111 #include <sys/kauth.h>
112 #include <sys/systm.h>
113 #include <sys/syslog.h>
114
115 #include <net/if.h>
116 #include <net/if_types.h>
117 #include <net/route.h>
118 #include <net/pfil.h>
119
120 #include <netinet/in.h>
121 #include <netinet/in_systm.h>
122 #include <netinet/ip.h>
123 #include <netinet/in_pcb.h>
124 #include <netinet/in_var.h>
125 #include <netinet/ip_var.h>
126 #include <netinet/ip_private.h>
127 #include <netinet/in_offload.h>
128 #include <netinet/portalgo.h>
129 #include <netinet/udp.h>
130 #include <netinet/udp_var.h>
131
132 #ifdef INET6
133 #include <netinet6/ip6_var.h>
134 #endif
135
136 #ifdef MROUTING
137 #include <netinet/ip_mroute.h>
138 #endif
139
140 #ifdef IPSEC
141 #include <netipsec/ipsec.h>
142 #include <netipsec/key.h>
143 #endif
144
145 #ifdef MPLS
146 #include <netmpls/mpls.h>
147 #include <netmpls/mpls_var.h>
148 #endif
149
150 static int ip_pcbopts(struct inpcb *, const struct sockopt *);
151 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
152 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
153 static void ip_mloopback(struct ifnet *, struct mbuf *,
154 const struct sockaddr_in *);
155 static int ip_ifaddrvalid(const struct in_ifaddr *);
156
157 extern pfil_head_t *inet_pfil_hook; /* XXX */
158
159 int ip_do_loopback_cksum = 0;
160
161 static int
162 ip_mark_mpls(struct ifnet * const ifp, struct mbuf * const m,
163 const struct rtentry *rt)
164 {
165 int error = 0;
166 #ifdef MPLS
167 union mpls_shim msh;
168
169 if (rt == NULL || rt_gettag(rt) == NULL ||
170 rt_gettag(rt)->sa_family != AF_MPLS ||
171 (m->m_flags & (M_MCAST | M_BCAST)) != 0 ||
172 ifp->if_type != IFT_ETHER)
173 return 0;
174
175 msh.s_addr = MPLS_GETSADDR(rt);
176 if (msh.shim.label != MPLS_LABEL_IMPLNULL) {
177 struct m_tag *mtag;
178 /*
179 * XXX tentative solution to tell ether_output
180 * it's MPLS. Need some more efficient solution.
181 */
182 mtag = m_tag_get(PACKET_TAG_MPLS,
183 sizeof(int) /* dummy */,
184 M_NOWAIT);
185 if (mtag == NULL)
186 return ENOMEM;
187 m_tag_prepend(m, mtag);
188 }
189 #endif
190 return error;
191 }
192
193 /*
194 * Send an IP packet to a host.
195 */
196 int
197 ip_if_output(struct ifnet * const ifp, struct mbuf * const m,
198 const struct sockaddr * const dst, const struct rtentry *rt)
199 {
200 int error = 0;
201
202 if (rt != NULL) {
203 error = rt_check_reject_route(rt, ifp);
204 if (error != 0) {
205 IP_STATINC(IP_STAT_RTREJECT);
206 m_freem(m);
207 return error;
208 }
209 }
210
211 error = ip_mark_mpls(ifp, m, rt);
212 if (error != 0) {
213 m_freem(m);
214 return error;
215 }
216
217 error = if_output_lock(ifp, ifp, m, dst, rt);
218
219 return error;
220 }
221
222 /*
223 * IP output. The packet in mbuf chain m contains a skeletal IP
224 * header (with len, off, ttl, proto, tos, src, dst).
225 * The mbuf chain containing the packet will be freed.
226 * The mbuf opt, if present, will not be freed.
227 */
228 int
229 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, int flags,
230 struct ip_moptions *imo, struct inpcb *inp)
231 {
232 struct rtentry *rt;
233 struct ip *ip;
234 struct ifnet *ifp, *mifp = NULL;
235 struct mbuf *m = m0;
236 int len, hlen, error = 0;
237 struct route iproute;
238 const struct sockaddr_in *dst;
239 struct in_ifaddr *ia = NULL;
240 struct ifaddr *ifa;
241 int isbroadcast;
242 int sw_csum;
243 u_long mtu;
244 bool natt_frag = false;
245 bool rtmtu_nolock;
246 union {
247 struct sockaddr sa;
248 struct sockaddr_in sin;
249 } udst, usrc;
250 struct sockaddr *rdst = &udst.sa; /* real IP destination, as
251 * opposed to the nexthop
252 */
253 struct psref psref, psref_ia;
254 int bound;
255 bool bind_need_restore = false;
256 const struct sockaddr *sa;
257
258 len = 0;
259
260 MCLAIM(m, &ip_tx_mowner);
261
262 KASSERT((m->m_flags & M_PKTHDR) != 0);
263 KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) == 0);
264 KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) !=
265 (M_CSUM_TCPv4|M_CSUM_UDPv4));
266 KASSERT(m->m_len >= sizeof(struct ip));
267
268 hlen = sizeof(struct ip);
269 if (opt) {
270 m = ip_insertoptions(m, opt, &len);
271 hlen = len;
272 }
273 ip = mtod(m, struct ip *);
274
275 /*
276 * Fill in IP header.
277 */
278 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
279 ip->ip_v = IPVERSION;
280 ip->ip_off = htons(0);
281 /* ip->ip_id filled in after we find out source ia */
282 ip->ip_hl = hlen >> 2;
283 IP_STATINC(IP_STAT_LOCALOUT);
284 } else {
285 hlen = ip->ip_hl << 2;
286 }
287
288 /*
289 * Route packet.
290 */
291 if (ro == NULL) {
292 memset(&iproute, 0, sizeof(iproute));
293 ro = &iproute;
294 }
295 sockaddr_in_init(&udst.sin, &ip->ip_dst, 0);
296 dst = satocsin(rtcache_getdst(ro));
297
298 /*
299 * If there is a cached route, check that it is to the same
300 * destination and is still up. If not, free it and try again.
301 * The address family should also be checked in case of sharing
302 * the cache with IPv6.
303 */
304 if (dst && (dst->sin_family != AF_INET ||
305 !in_hosteq(dst->sin_addr, ip->ip_dst)))
306 rtcache_free(ro);
307
308 /* XXX must be before rtcache operations */
309 bound = curlwp_bind();
310 bind_need_restore = true;
311
312 if ((rt = rtcache_validate(ro)) == NULL &&
313 (rt = rtcache_update(ro, 1)) == NULL) {
314 dst = &udst.sin;
315 error = rtcache_setdst(ro, &udst.sa);
316 if (error != 0) {
317 IP_STATINC(IP_STAT_ODROPPED);
318 goto bad;
319 }
320 }
321
322 /*
323 * If routing to interface only, short circuit routing lookup.
324 */
325 if (flags & IP_ROUTETOIF) {
326 ifa = ifa_ifwithladdr_psref(sintocsa(dst), &psref_ia);
327 if (ifa == NULL) {
328 IP_STATINC(IP_STAT_NOROUTE);
329 error = ENETUNREACH;
330 goto bad;
331 }
332 /* ia is already referenced by psref_ia */
333 ia = ifatoia(ifa);
334
335 /* Need a reference to keep ifp after ia4_release(ia). */
336 ifp = mifp = if_get_byindex(ia->ia_ifp->if_index, &psref);
337 if (__predict_false(ifp == NULL)) {
338 IP_STATINC(IP_STAT_NOROUTE);
339 error = ENETUNREACH;
340 goto bad;
341 }
342 mtu = ifp->if_mtu;
343 ip->ip_ttl = 1;
344 isbroadcast = in_broadcast(dst->sin_addr, ifp);
345 } else if (((IN_MULTICAST(ip->ip_dst.s_addr) ||
346 ip->ip_dst.s_addr == INADDR_BROADCAST) ||
347 (flags & IP_ROUTETOIFINDEX)) &&
348 imo != NULL && imo->imo_multicast_if_index != 0) {
349 ifp = mifp = if_get_byindex(imo->imo_multicast_if_index, &psref);
350 if (ifp == NULL) {
351 IP_STATINC(IP_STAT_NOROUTE);
352 error = ENETUNREACH;
353 goto bad;
354 }
355 mtu = ifp->if_mtu;
356 ia = in_get_ia_from_ifp_psref(ifp, &psref_ia);
357 if (IN_MULTICAST(ip->ip_dst.s_addr) ||
358 ip->ip_dst.s_addr == INADDR_BROADCAST) {
359 isbroadcast = 0;
360 } else {
361 /* IP_ROUTETOIFINDEX */
362 isbroadcast = in_broadcast(dst->sin_addr, ifp);
363 if ((isbroadcast == 0) && ((ifp->if_flags &
364 (IFF_LOOPBACK | IFF_POINTOPOINT)) == 0) &&
365 (in_direct(dst->sin_addr, ifp) == 0)) {
366 /* gateway address required */
367 if (rt == NULL)
368 rt = rtcache_init(ro);
369 if (rt == NULL || rt->rt_ifp != ifp) {
370 IP_STATINC(IP_STAT_NOROUTE);
371 error = EHOSTUNREACH;
372 goto bad;
373 }
374 rt->rt_use++;
375 if (rt->rt_flags & RTF_GATEWAY)
376 dst = satosin(rt->rt_gateway);
377 if (rt->rt_flags & RTF_HOST)
378 isbroadcast =
379 rt->rt_flags & RTF_BROADCAST;
380 }
381 }
382 } else {
383 if (rt == NULL)
384 rt = rtcache_init(ro);
385 if (rt == NULL) {
386 IP_STATINC(IP_STAT_NOROUTE);
387 error = EHOSTUNREACH;
388 goto bad;
389 }
390 if (ifa_is_destroying(rt->rt_ifa)) {
391 rtcache_unref(rt, ro);
392 rt = NULL;
393 IP_STATINC(IP_STAT_NOROUTE);
394 error = EHOSTUNREACH;
395 goto bad;
396 }
397 ifa_acquire(rt->rt_ifa, &psref_ia);
398 ia = ifatoia(rt->rt_ifa);
399 ifp = rt->rt_ifp;
400 if ((mtu = rt->rt_rmx.rmx_mtu) == 0)
401 mtu = ifp->if_mtu;
402 rt->rt_use++;
403 if (rt->rt_flags & RTF_GATEWAY)
404 dst = satosin(rt->rt_gateway);
405 if (rt->rt_flags & RTF_HOST)
406 isbroadcast = rt->rt_flags & RTF_BROADCAST;
407 else
408 isbroadcast = in_broadcast(dst->sin_addr, ifp);
409 }
410 rtmtu_nolock = rt && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0;
411
412 if (IN_MULTICAST(ip->ip_dst.s_addr) ||
413 (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
414 bool inmgroup;
415
416 m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
417 M_BCAST : M_MCAST;
418 /*
419 * See if the caller provided any multicast options
420 */
421 if (imo != NULL)
422 ip->ip_ttl = imo->imo_multicast_ttl;
423 else
424 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
425
426 /*
427 * if we don't know the outgoing ifp yet, we can't generate
428 * output
429 */
430 if (!ifp) {
431 IP_STATINC(IP_STAT_NOROUTE);
432 error = ENETUNREACH;
433 goto bad;
434 }
435
436 /*
437 * If the packet is multicast or broadcast, confirm that
438 * the outgoing interface can transmit it.
439 */
440 if (((m->m_flags & M_MCAST) &&
441 (ifp->if_flags & IFF_MULTICAST) == 0) ||
442 ((m->m_flags & M_BCAST) &&
443 (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)) {
444 IP_STATINC(IP_STAT_NOROUTE);
445 error = ENETUNREACH;
446 goto bad;
447 }
448 /*
449 * If source address not specified yet, use an address
450 * of outgoing interface.
451 */
452 if (in_nullhost(ip->ip_src)) {
453 struct in_ifaddr *xia;
454 struct ifaddr *xifa;
455 struct psref _psref;
456
457 xia = in_get_ia_from_ifp_psref(ifp, &_psref);
458 if (!xia) {
459 IP_STATINC(IP_STAT_IFNOADDR);
460 error = EADDRNOTAVAIL;
461 goto bad;
462 }
463 xifa = &xia->ia_ifa;
464 if (xifa->ifa_getifa != NULL) {
465 ia4_release(xia, &_psref);
466 /* FIXME ifa_getifa is NOMPSAFE */
467 xia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
468 if (xia == NULL) {
469 IP_STATINC(IP_STAT_IFNOADDR);
470 error = EADDRNOTAVAIL;
471 goto bad;
472 }
473 ia4_acquire(xia, &_psref);
474 }
475 ip->ip_src = xia->ia_addr.sin_addr;
476 ia4_release(xia, &_psref);
477 }
478
479 inmgroup = in_multi_group(ip->ip_dst, ifp, flags);
480 if (inmgroup && (imo == NULL || imo->imo_multicast_loop)) {
481 /*
482 * If we belong to the destination multicast group
483 * on the outgoing interface, and the caller did not
484 * forbid loopback, loop back a copy.
485 */
486 ip_mloopback(ifp, m, &udst.sin);
487 }
488 #ifdef MROUTING
489 else {
490 /*
491 * If we are acting as a multicast router, perform
492 * multicast forwarding as if the packet had just
493 * arrived on the interface to which we are about
494 * to send. The multicast forwarding function
495 * recursively calls this function, using the
496 * IP_FORWARDING flag to prevent infinite recursion.
497 *
498 * Multicasts that are looped back by ip_mloopback(),
499 * above, will be forwarded by the ip_input() routine,
500 * if necessary.
501 */
502 extern struct socket *ip_mrouter;
503
504 if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
505 if (ip_mforward(m, ifp) != 0) {
506 m_freem(m);
507 goto done;
508 }
509 }
510 }
511 #endif
512 /*
513 * Multicasts with a time-to-live of zero may be looped-
514 * back, above, but must not be transmitted on a network.
515 * Also, multicasts addressed to the loopback interface
516 * are not sent -- the above call to ip_mloopback() will
517 * loop back a copy if this host actually belongs to the
518 * destination group on the loopback interface.
519 */
520 if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
521 IP_STATINC(IP_STAT_ODROPPED);
522 m_freem(m);
523 goto done;
524 }
525 goto sendit;
526 }
527
528 /*
529 * If source address not specified yet, use address
530 * of outgoing interface.
531 */
532 if (in_nullhost(ip->ip_src)) {
533 struct ifaddr *xifa;
534
535 xifa = &ia->ia_ifa;
536 if (xifa->ifa_getifa != NULL) {
537 ia4_release(ia, &psref_ia);
538 /* FIXME ifa_getifa is NOMPSAFE */
539 ia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
540 if (ia == NULL) {
541 error = EADDRNOTAVAIL;
542 goto bad;
543 }
544 ia4_acquire(ia, &psref_ia);
545 }
546 ip->ip_src = ia->ia_addr.sin_addr;
547 }
548
549 /*
550 * Packets with Class-D address as source are not valid per
551 * RFC1112.
552 */
553 if (IN_MULTICAST(ip->ip_src.s_addr)) {
554 IP_STATINC(IP_STAT_ODROPPED);
555 error = EADDRNOTAVAIL;
556 goto bad;
557 }
558
559 /*
560 * Look for broadcast address and verify user is allowed to
561 * send such a packet.
562 */
563 if (isbroadcast) {
564 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
565 IP_STATINC(IP_STAT_BCASTDENIED);
566 error = EADDRNOTAVAIL;
567 goto bad;
568 }
569 if ((flags & IP_ALLOWBROADCAST) == 0) {
570 IP_STATINC(IP_STAT_BCASTDENIED);
571 error = EACCES;
572 goto bad;
573 }
574 /* don't allow broadcast messages to be fragmented */
575 if (ntohs(ip->ip_len) > ifp->if_mtu) {
576 IP_STATINC(IP_STAT_BCASTDENIED);
577 error = EMSGSIZE;
578 goto bad;
579 }
580 m->m_flags |= M_BCAST;
581 } else
582 m->m_flags &= ~M_BCAST;
583
584 sendit:
585 if ((flags & (IP_FORWARDING|IP_NOIPNEWID)) == 0) {
586 if (m->m_pkthdr.len < IP_MINFRAGSIZE) {
587 ip->ip_id = 0;
588 } else if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
589 ip->ip_id = ip_newid();
590 } else {
591 /*
592 * TSO capable interfaces (typically?) increment
593 * ip_id for each segment.
594 * "allocate" enough ids here to increase the chance
595 * for them to be unique.
596 *
597 * note that the following calculation is not
598 * needed to be precise. wasting some ip_id is fine.
599 */
600
601 unsigned int segsz = m->m_pkthdr.segsz;
602 unsigned int datasz = ntohs(ip->ip_len) - hlen;
603 unsigned int num = howmany(datasz, segsz);
604
605 ip->ip_id = ip_newid_range(num);
606 }
607 }
608 if (ia != NULL) {
609 ia4_release(ia, &psref_ia);
610 ia = NULL;
611 }
612
613 /*
614 * If we're doing Path MTU Discovery, we need to set DF unless
615 * the route's MTU is locked.
616 */
617 if ((flags & IP_MTUDISC) != 0 && rtmtu_nolock) {
618 ip->ip_off |= htons(IP_DF);
619 }
620
621 #ifdef IPSEC
622 if (ipsec_used) {
623 bool ipsec_done = false;
624 bool count_drop = false;
625
626 /* Perform IPsec processing, if any. */
627 error = ipsec4_output(m, inp, flags, &mtu, &natt_frag,
628 &ipsec_done, &count_drop);
629 if (count_drop)
630 IP_STATINC(IP_STAT_IPSECDROP_OUT);
631 if (error || ipsec_done)
632 goto done;
633 }
634
635 if (!ipsec_used || !natt_frag)
636 #endif
637 {
638 /*
639 * Run through list of hooks for output packets.
640 */
641 error = pfil_run_hooks(inet_pfil_hook, &m, ifp, PFIL_OUT);
642 if (error || m == NULL) {
643 IP_STATINC(IP_STAT_PFILDROP_OUT);
644 goto done;
645 }
646 }
647
648 ip = mtod(m, struct ip *);
649 hlen = ip->ip_hl << 2;
650
651 m->m_pkthdr.csum_data |= hlen << 16;
652
653 /*
654 * search for the source address structure to
655 * maintain output statistics, and verify address
656 * validity
657 */
658 KASSERT(ia == NULL);
659 sockaddr_in_init(&usrc.sin, &ip->ip_src, 0);
660 ifa = ifaof_ifpforaddr_psref(&usrc.sa, ifp, &psref_ia);
661 if (ifa != NULL)
662 ia = ifatoia(ifa);
663
664 /*
665 * Ensure we only send from a valid address.
666 * A NULL address is valid because the packet could be
667 * generated from a packet filter.
668 */
669 if (ia != NULL && (flags & IP_FORWARDING) == 0 &&
670 (error = ip_ifaddrvalid(ia)) != 0)
671 {
672 ARPLOG(LOG_ERR,
673 "refusing to send from invalid address %s (pid %d)\n",
674 ARPLOGADDR(&ip->ip_src), curproc->p_pid);
675 IP_STATINC(IP_STAT_ODROPPED);
676 if (error == 1)
677 /*
678 * Address exists, but is tentative or detached.
679 * We can't send from it because it's invalid,
680 * so we drop the packet.
681 */
682 error = 0;
683 else
684 error = EADDRNOTAVAIL;
685 goto bad;
686 }
687
688 /* Maybe skip checksums on loopback interfaces. */
689 if (IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) {
690 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
691 }
692 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
693
694 /* Need to fragment the packet */
695 if (ntohs(ip->ip_len) > mtu &&
696 (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
697 goto fragment;
698 }
699
700 #if IFA_STATS
701 if (ia)
702 ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
703 #endif
704 /*
705 * Always initialize the sum to 0! Some HW assisted
706 * checksumming requires this.
707 */
708 ip->ip_sum = 0;
709
710 if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
711 /*
712 * Perform any checksums that the hardware can't do
713 * for us.
714 *
715 * XXX Does any hardware require the {th,uh}_sum
716 * XXX fields to be 0?
717 */
718 if (sw_csum & M_CSUM_IPv4) {
719 KASSERT(IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4));
720 ip->ip_sum = in_cksum(m, hlen);
721 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
722 }
723 if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
724 if (IN_NEED_CHECKSUM(ifp,
725 sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
726 in_undefer_cksum_tcpudp(m);
727 }
728 m->m_pkthdr.csum_flags &=
729 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
730 }
731 }
732
733 sa = (m->m_flags & M_MCAST) ? sintocsa(rdst) : sintocsa(dst);
734
735 /* Send it */
736 if (__predict_false(sw_csum & M_CSUM_TSOv4)) {
737 /*
738 * TSO4 is required by a packet, but disabled for
739 * the interface.
740 */
741 error = ip_tso_output(ifp, m, sa, rt);
742 } else
743 error = ip_if_output(ifp, m, sa, rt);
744 goto done;
745
746 fragment:
747 /*
748 * We can't use HW checksumming if we're about to fragment the packet.
749 *
750 * XXX Some hardware can do this.
751 */
752 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
753 if (IN_NEED_CHECKSUM(ifp,
754 m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
755 in_undefer_cksum_tcpudp(m);
756 }
757 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
758 }
759
760 /*
761 * Too large for interface; fragment if possible.
762 * Must be able to put at least 8 bytes per fragment.
763 */
764 if (ntohs(ip->ip_off) & IP_DF) {
765 if (flags & IP_RETURNMTU) {
766 KASSERT(inp != NULL);
767 in4p_errormtu(inp) = mtu;
768 }
769 error = EMSGSIZE;
770 IP_STATINC(IP_STAT_CANTFRAG);
771 goto bad;
772 }
773
774 error = ip_fragment(m, ifp, mtu);
775 if (error) {
776 m = NULL;
777 goto bad;
778 }
779
780 for (; m; m = m0) {
781 m0 = m->m_nextpkt;
782 m->m_nextpkt = NULL;
783 if (error) {
784 m_freem(m);
785 continue;
786 }
787 #if IFA_STATS
788 if (ia)
789 ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
790 #endif
791 /*
792 * If we get there, the packet has not been handled by
793 * IPsec whereas it should have. Now that it has been
794 * fragmented, re-inject it in ip_output so that IPsec
795 * processing can occur.
796 */
797 if (natt_frag) {
798 error = ip_output(m, opt, NULL,
799 flags | IP_RAWOUTPUT | IP_NOIPNEWID,
800 imo, inp);
801 } else {
802 KASSERT((m->m_pkthdr.csum_flags &
803 (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
804 error = ip_if_output(ifp, m, (m->m_flags & M_MCAST) ?
805 sintocsa(rdst) : sintocsa(dst), rt);
806 }
807 }
808 if (error == 0) {
809 IP_STATINC(IP_STAT_FRAGMENTED);
810 }
811
812 done:
813 ia4_release(ia, &psref_ia);
814 rtcache_unref(rt, ro);
815 if (ro == &iproute) {
816 rtcache_free(&iproute);
817 }
818 if (mifp != NULL) {
819 if_put(mifp, &psref);
820 }
821 if (bind_need_restore)
822 curlwp_bindx(bound);
823 return error;
824
825 bad:
826 m_freem(m);
827 goto done;
828 }
829
830 int
831 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
832 {
833 struct ip *ip, *mhip;
834 struct mbuf *m0;
835 int len, hlen, off;
836 int mhlen, firstlen;
837 struct mbuf **mnext;
838 int sw_csum = m->m_pkthdr.csum_flags;
839 int fragments = 0;
840 int error = 0;
841 int ipoff, ipflg;
842
843 ip = mtod(m, struct ip *);
844 hlen = ip->ip_hl << 2;
845
846 /* Preserve the offset and flags. */
847 ipoff = ntohs(ip->ip_off) & IP_OFFMASK;
848 ipflg = ntohs(ip->ip_off) & (IP_RF|IP_DF|IP_MF);
849
850 if (ifp != NULL)
851 sw_csum &= ~ifp->if_csum_flags_tx;
852
853 len = (mtu - hlen) &~ 7;
854 if (len < 8) {
855 IP_STATINC(IP_STAT_CANTFRAG);
856 m_freem(m);
857 return EMSGSIZE;
858 }
859
860 firstlen = len;
861 mnext = &m->m_nextpkt;
862
863 /*
864 * Loop through length of segment after first fragment,
865 * make new header and copy data of each part and link onto chain.
866 */
867 m0 = m;
868 mhlen = sizeof(struct ip);
869 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
870 MGETHDR(m, M_DONTWAIT, MT_HEADER);
871 if (m == NULL) {
872 error = ENOBUFS;
873 IP_STATINC(IP_STAT_ODROPPED);
874 goto sendorfree;
875 }
876 MCLAIM(m, m0->m_owner);
877
878 *mnext = m;
879 mnext = &m->m_nextpkt;
880
881 m->m_data += max_linkhdr;
882 mhip = mtod(m, struct ip *);
883 *mhip = *ip;
884
885 /* we must inherit the flags */
886 m->m_flags |= m0->m_flags & M_COPYFLAGS;
887
888 if (hlen > sizeof(struct ip)) {
889 mhlen = ip_optcopy(ip, mhip) + sizeof(struct ip);
890 mhip->ip_hl = mhlen >> 2;
891 }
892 m->m_len = mhlen;
893
894 mhip->ip_off = ((off - hlen) >> 3) + ipoff;
895 mhip->ip_off |= ipflg;
896 if (off + len >= ntohs(ip->ip_len))
897 len = ntohs(ip->ip_len) - off;
898 else
899 mhip->ip_off |= IP_MF;
900 HTONS(mhip->ip_off);
901
902 mhip->ip_len = htons((u_int16_t)(len + mhlen));
903 m->m_next = m_copym(m0, off, len, M_DONTWAIT);
904 if (m->m_next == NULL) {
905 error = ENOBUFS;
906 IP_STATINC(IP_STAT_ODROPPED);
907 goto sendorfree;
908 }
909
910 m->m_pkthdr.len = mhlen + len;
911 m_reset_rcvif(m);
912
913 mhip->ip_sum = 0;
914 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
915 if (sw_csum & M_CSUM_IPv4) {
916 mhip->ip_sum = in_cksum(m, mhlen);
917 } else {
918 /*
919 * checksum is hw-offloaded or not necessary.
920 */
921 m->m_pkthdr.csum_flags |=
922 m0->m_pkthdr.csum_flags & M_CSUM_IPv4;
923 m->m_pkthdr.csum_data |= mhlen << 16;
924 KASSERT(!(ifp != NULL &&
925 IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) ||
926 (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
927 }
928 IP_STATINC(IP_STAT_OFRAGMENTS);
929 fragments++;
930 }
931
932 /*
933 * Update first fragment by trimming what's been copied out
934 * and updating header, then send each fragment (in order).
935 */
936 m = m0;
937 m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
938 m->m_pkthdr.len = hlen + firstlen;
939 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
940 ip->ip_off |= htons(IP_MF);
941 ip->ip_sum = 0;
942 if (sw_csum & M_CSUM_IPv4) {
943 ip->ip_sum = in_cksum(m, hlen);
944 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
945 } else {
946 /*
947 * checksum is hw-offloaded or not necessary.
948 */
949 KASSERT(!(ifp != NULL && IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) ||
950 (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
951 KASSERT(M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) >=
952 sizeof(struct ip));
953 }
954
955 sendorfree:
956 /*
957 * If there is no room for all the fragments, don't queue
958 * any of them.
959 */
960 if (ifp != NULL) {
961 IFQ_LOCK(&ifp->if_snd);
962 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments &&
963 error == 0) {
964 error = ENOBUFS;
965 IP_STATINC(IP_STAT_ODROPPED);
966 IFQ_INC_DROPS(&ifp->if_snd);
967 }
968 IFQ_UNLOCK(&ifp->if_snd);
969 }
970 if (error) {
971 for (m = m0; m; m = m0) {
972 m0 = m->m_nextpkt;
973 m->m_nextpkt = NULL;
974 m_freem(m);
975 }
976 }
977
978 return error;
979 }
980
981 /*
982 * Determine the maximum length of the options to be inserted;
983 * we would far rather allocate too much space rather than too little.
984 */
985 u_int
986 ip_optlen(struct inpcb *inp)
987 {
988 struct mbuf *m = inp->inp_options;
989
990 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst)) {
991 return (m->m_len - offsetof(struct ipoption, ipopt_dst));
992 }
993 return 0;
994 }
995
996 /*
997 * Insert IP options into preformed packet.
998 * Adjust IP destination as required for IP source routing,
999 * as indicated by a non-zero in_addr at the start of the options.
1000 */
1001 static struct mbuf *
1002 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
1003 {
1004 struct ipoption *p = mtod(opt, struct ipoption *);
1005 struct mbuf *n;
1006 struct ip *ip = mtod(m, struct ip *);
1007 unsigned optlen;
1008
1009 optlen = opt->m_len - sizeof(p->ipopt_dst);
1010 KASSERT(optlen % 4 == 0);
1011 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
1012 return m; /* XXX should fail */
1013 if (!in_nullhost(p->ipopt_dst))
1014 ip->ip_dst = p->ipopt_dst;
1015 if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) {
1016 MGETHDR(n, M_DONTWAIT, MT_HEADER);
1017 if (n == NULL)
1018 return m;
1019 MCLAIM(n, m->m_owner);
1020 m_move_pkthdr(n, m);
1021 m->m_len -= sizeof(struct ip);
1022 m->m_data += sizeof(struct ip);
1023 n->m_next = m;
1024 n->m_len = optlen + sizeof(struct ip);
1025 n->m_data += max_linkhdr;
1026 memcpy(mtod(n, void *), ip, sizeof(struct ip));
1027 m = n;
1028 } else {
1029 m->m_data -= optlen;
1030 m->m_len += optlen;
1031 memmove(mtod(m, void *), ip, sizeof(struct ip));
1032 }
1033 m->m_pkthdr.len += optlen;
1034 ip = mtod(m, struct ip *);
1035 memcpy(ip + 1, p->ipopt_list, optlen);
1036 *phlen = sizeof(struct ip) + optlen;
1037 ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
1038 return m;
1039 }
1040
1041 /*
1042 * Copy options from ipsrc to ipdst, omitting those not copied during
1043 * fragmentation.
1044 */
1045 int
1046 ip_optcopy(struct ip *ipsrc, struct ip *ipdst)
1047 {
1048 u_char *cp, *dp;
1049 int opt, optlen, cnt;
1050
1051 cp = (u_char *)(ipsrc + 1);
1052 dp = (u_char *)(ipdst + 1);
1053 cnt = (ipsrc->ip_hl << 2) - sizeof(struct ip);
1054 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1055 opt = cp[0];
1056 if (opt == IPOPT_EOL)
1057 break;
1058 if (opt == IPOPT_NOP) {
1059 /* Preserve for IP mcast tunnel's LSRR alignment. */
1060 *dp++ = IPOPT_NOP;
1061 optlen = 1;
1062 continue;
1063 }
1064
1065 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp));
1066 optlen = cp[IPOPT_OLEN];
1067 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen < cnt);
1068
1069 /* Invalid lengths should have been caught by ip_dooptions. */
1070 if (optlen > cnt)
1071 optlen = cnt;
1072 if (IPOPT_COPIED(opt)) {
1073 bcopy((void *)cp, (void *)dp, (unsigned)optlen);
1074 dp += optlen;
1075 }
1076 }
1077
1078 for (optlen = dp - (u_char *)(ipdst+1); optlen & 0x3; optlen++) {
1079 *dp++ = IPOPT_EOL;
1080 }
1081
1082 return optlen;
1083 }
1084
1085 /*
1086 * IP socket option processing.
1087 */
1088 int
1089 ip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1090 {
1091 struct inpcb *inp = sotoinpcb(so);
1092 struct ip *ip = &in4p_ip(inp);
1093 int inpflags = inp->inp_flags;
1094 int optval = 0, error = 0;
1095 struct in_pktinfo pktinfo;
1096
1097 KASSERT(solocked(so));
1098
1099 if (sopt->sopt_level != IPPROTO_IP) {
1100 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER)
1101 return 0;
1102 return ENOPROTOOPT;
1103 }
1104
1105 switch (op) {
1106 case PRCO_SETOPT:
1107 switch (sopt->sopt_name) {
1108 case IP_OPTIONS:
1109 #ifdef notyet
1110 case IP_RETOPTS:
1111 #endif
1112 error = ip_pcbopts(inp, sopt);
1113 break;
1114
1115 case IP_TOS:
1116 case IP_TTL:
1117 case IP_MINTTL:
1118 case IP_RECVOPTS:
1119 case IP_RECVRETOPTS:
1120 case IP_RECVDSTADDR:
1121 case IP_RECVIF:
1122 case IP_RECVPKTINFO:
1123 case IP_RECVTTL:
1124 case IP_BINDANY:
1125 error = sockopt_getint(sopt, &optval);
1126 if (error)
1127 break;
1128
1129 switch (sopt->sopt_name) {
1130 case IP_TOS:
1131 ip->ip_tos = optval;
1132 break;
1133
1134 case IP_TTL:
1135 ip->ip_ttl = optval;
1136 break;
1137
1138 case IP_MINTTL:
1139 if (optval > 0 && optval <= MAXTTL)
1140 in4p_ip_minttl(inp) = optval;
1141 else
1142 error = EINVAL;
1143 break;
1144 #define OPTSET(bit) \
1145 if (optval) \
1146 inpflags |= bit; \
1147 else \
1148 inpflags &= ~bit;
1149
1150 case IP_RECVOPTS:
1151 OPTSET(INP_RECVOPTS);
1152 break;
1153
1154 case IP_RECVPKTINFO:
1155 OPTSET(INP_RECVPKTINFO);
1156 break;
1157
1158 case IP_RECVRETOPTS:
1159 OPTSET(INP_RECVRETOPTS);
1160 break;
1161
1162 case IP_RECVDSTADDR:
1163 OPTSET(INP_RECVDSTADDR);
1164 break;
1165
1166 case IP_RECVIF:
1167 OPTSET(INP_RECVIF);
1168 break;
1169
1170 case IP_RECVTTL:
1171 OPTSET(INP_RECVTTL);
1172 break;
1173
1174 case IP_BINDANY:
1175 error = kauth_authorize_network(
1176 kauth_cred_get(), KAUTH_NETWORK_BIND,
1177 KAUTH_REQ_NETWORK_BIND_ANYADDR, so,
1178 NULL, NULL);
1179 if (error == 0) {
1180 OPTSET(INP_BINDANY);
1181 }
1182 break;
1183 }
1184 break;
1185 case IP_PKTINFO:
1186 error = sockopt_getint(sopt, &optval);
1187 if (!error) {
1188 /* Linux compatibility */
1189 OPTSET(INP_RECVPKTINFO);
1190 break;
1191 }
1192 error = sockopt_get(sopt, &pktinfo, sizeof(pktinfo));
1193 if (error)
1194 break;
1195
1196 if (pktinfo.ipi_ifindex == 0) {
1197 in4p_prefsrcip(inp) = pktinfo.ipi_addr;
1198 break;
1199 }
1200
1201 /* Solaris compatibility */
1202 struct ifnet *ifp;
1203 struct in_ifaddr *ia;
1204 int s;
1205
1206 /* pick up primary address */
1207 s = pserialize_read_enter();
1208 ifp = if_byindex(pktinfo.ipi_ifindex);
1209 if (ifp == NULL) {
1210 pserialize_read_exit(s);
1211 error = EADDRNOTAVAIL;
1212 break;
1213 }
1214 ia = in_get_ia_from_ifp(ifp);
1215 if (ia == NULL) {
1216 pserialize_read_exit(s);
1217 error = EADDRNOTAVAIL;
1218 break;
1219 }
1220 in4p_prefsrcip(inp) = IA_SIN(ia)->sin_addr;
1221 pserialize_read_exit(s);
1222 break;
1223 break;
1224 #undef OPTSET
1225
1226 case IP_MULTICAST_IF:
1227 case IP_MULTICAST_TTL:
1228 case IP_MULTICAST_LOOP:
1229 case IP_ADD_MEMBERSHIP:
1230 case IP_DROP_MEMBERSHIP:
1231 error = ip_setmoptions(&inp->inp_moptions, sopt);
1232 break;
1233
1234 case IP_PORTRANGE:
1235 error = sockopt_getint(sopt, &optval);
1236 if (error)
1237 break;
1238
1239 switch (optval) {
1240 case IP_PORTRANGE_DEFAULT:
1241 case IP_PORTRANGE_HIGH:
1242 inpflags &= ~(INP_LOWPORT);
1243 break;
1244
1245 case IP_PORTRANGE_LOW:
1246 inpflags |= INP_LOWPORT;
1247 break;
1248
1249 default:
1250 error = EINVAL;
1251 break;
1252 }
1253 break;
1254
1255 case IP_PORTALGO:
1256 error = sockopt_getint(sopt, &optval);
1257 if (error)
1258 break;
1259
1260 error = portalgo_algo_index_select(inp, optval);
1261 break;
1262
1263 #if defined(IPSEC)
1264 case IP_IPSEC_POLICY:
1265 if (ipsec_enabled) {
1266 error = ipsec_set_policy(inp,
1267 sopt->sopt_data, sopt->sopt_size,
1268 curlwp->l_cred);
1269 } else
1270 error = ENOPROTOOPT;
1271 break;
1272 #endif /* IPSEC */
1273
1274 default:
1275 error = ENOPROTOOPT;
1276 break;
1277 }
1278 break;
1279
1280 case PRCO_GETOPT:
1281 switch (sopt->sopt_name) {
1282 case IP_OPTIONS:
1283 case IP_RETOPTS: {
1284 struct mbuf *mopts = inp->inp_options;
1285
1286 if (mopts) {
1287 struct mbuf *m;
1288
1289 m = m_copym(mopts, 0, M_COPYALL, M_DONTWAIT);
1290 if (m == NULL) {
1291 error = ENOBUFS;
1292 break;
1293 }
1294 error = sockopt_setmbuf(sopt, m);
1295 }
1296 break;
1297 }
1298 case IP_TOS:
1299 case IP_TTL:
1300 case IP_MINTTL:
1301 case IP_RECVOPTS:
1302 case IP_RECVRETOPTS:
1303 case IP_RECVDSTADDR:
1304 case IP_RECVIF:
1305 case IP_RECVPKTINFO:
1306 case IP_RECVTTL:
1307 case IP_ERRORMTU:
1308 case IP_BINDANY:
1309 switch (sopt->sopt_name) {
1310 case IP_TOS:
1311 optval = ip->ip_tos;
1312 break;
1313
1314 case IP_TTL:
1315 optval = ip->ip_ttl;
1316 break;
1317
1318 case IP_MINTTL:
1319 optval = in4p_ip_minttl(inp);
1320 break;
1321
1322 case IP_ERRORMTU:
1323 optval = in4p_errormtu(inp);
1324 break;
1325
1326 #define OPTBIT(bit) (inpflags & bit ? 1 : 0)
1327
1328 case IP_RECVOPTS:
1329 optval = OPTBIT(INP_RECVOPTS);
1330 break;
1331
1332 case IP_RECVPKTINFO:
1333 optval = OPTBIT(INP_RECVPKTINFO);
1334 break;
1335
1336 case IP_RECVRETOPTS:
1337 optval = OPTBIT(INP_RECVRETOPTS);
1338 break;
1339
1340 case IP_RECVDSTADDR:
1341 optval = OPTBIT(INP_RECVDSTADDR);
1342 break;
1343
1344 case IP_RECVIF:
1345 optval = OPTBIT(INP_RECVIF);
1346 break;
1347
1348 case IP_RECVTTL:
1349 optval = OPTBIT(INP_RECVTTL);
1350 break;
1351
1352 case IP_BINDANY:
1353 optval = OPTBIT(INP_BINDANY);
1354 break;
1355 }
1356 error = sockopt_setint(sopt, optval);
1357 break;
1358
1359 case IP_PKTINFO:
1360 switch (sopt->sopt_size) {
1361 case sizeof(int):
1362 /* Linux compatibility */
1363 optval = OPTBIT(INP_RECVPKTINFO);
1364 error = sockopt_setint(sopt, optval);
1365 break;
1366 case sizeof(struct in_pktinfo):
1367 /* Solaris compatibility */
1368 pktinfo.ipi_ifindex = 0;
1369 pktinfo.ipi_addr = in4p_prefsrcip(inp);
1370 error = sockopt_set(sopt, &pktinfo,
1371 sizeof(pktinfo));
1372 break;
1373 default:
1374 /*
1375 * While size is stuck at 0, and, later, if
1376 * the caller doesn't use an exactly sized
1377 * recipient for the data, default to Linux
1378 * compatibility
1379 */
1380 optval = OPTBIT(INP_RECVPKTINFO);
1381 error = sockopt_setint(sopt, optval);
1382 break;
1383 }
1384 break;
1385
1386 #if 0 /* defined(IPSEC) */
1387 case IP_IPSEC_POLICY:
1388 {
1389 struct mbuf *m = NULL;
1390
1391 /* XXX this will return EINVAL as sopt is empty */
1392 error = ipsec_get_policy(inp, sopt->sopt_data,
1393 sopt->sopt_size, &m);
1394 if (error == 0)
1395 error = sockopt_setmbuf(sopt, m);
1396 break;
1397 }
1398 #endif /*IPSEC*/
1399
1400 case IP_MULTICAST_IF:
1401 case IP_MULTICAST_TTL:
1402 case IP_MULTICAST_LOOP:
1403 case IP_ADD_MEMBERSHIP:
1404 case IP_DROP_MEMBERSHIP:
1405 error = ip_getmoptions(inp->inp_moptions, sopt);
1406 break;
1407
1408 case IP_PORTRANGE:
1409 if (inpflags & INP_LOWPORT)
1410 optval = IP_PORTRANGE_LOW;
1411 else
1412 optval = IP_PORTRANGE_DEFAULT;
1413 error = sockopt_setint(sopt, optval);
1414 break;
1415
1416 case IP_PORTALGO:
1417 optval = inp->inp_portalgo;
1418 error = sockopt_setint(sopt, optval);
1419 break;
1420
1421 default:
1422 error = ENOPROTOOPT;
1423 break;
1424 }
1425 break;
1426 }
1427
1428 if (!error) {
1429 inp->inp_flags = inpflags;
1430 }
1431 return error;
1432 }
1433
1434 static int
1435 ip_pktinfo_prepare(const struct inpcb *inp, const struct in_pktinfo *pktinfo,
1436 struct ip_pktopts *pktopts, int *flags, kauth_cred_t cred)
1437 {
1438 struct ip_moptions *imo;
1439 int error = 0;
1440 bool addrset = false;
1441
1442 if (!in_nullhost(pktinfo->ipi_addr)) {
1443 pktopts->ippo_laddr.sin_addr = pktinfo->ipi_addr;
1444 /* EADDRNOTAVAIL? */
1445 error = inpcb_bindableaddr(inp, &pktopts->ippo_laddr, cred);
1446 if (error != 0)
1447 return error;
1448 addrset = true;
1449 }
1450
1451 if (pktinfo->ipi_ifindex != 0) {
1452 if (!addrset) {
1453 struct ifnet *ifp;
1454 struct in_ifaddr *ia;
1455 int s;
1456
1457 /* pick up primary address */
1458 s = pserialize_read_enter();
1459 ifp = if_byindex(pktinfo->ipi_ifindex);
1460 if (ifp == NULL) {
1461 pserialize_read_exit(s);
1462 return EADDRNOTAVAIL;
1463 }
1464 ia = in_get_ia_from_ifp(ifp);
1465 if (ia == NULL) {
1466 pserialize_read_exit(s);
1467 return EADDRNOTAVAIL;
1468 }
1469 pktopts->ippo_laddr.sin_addr = IA_SIN(ia)->sin_addr;
1470 pserialize_read_exit(s);
1471 }
1472
1473 /*
1474 * If specified ipi_ifindex,
1475 * use copied or locally initialized ip_moptions.
1476 * Original ip_moptions must not be modified.
1477 */
1478 imo = &pktopts->ippo_imobuf; /* local buf in pktopts */
1479 if (pktopts->ippo_imo != NULL) {
1480 memcpy(imo, pktopts->ippo_imo, sizeof(*imo));
1481 } else {
1482 memset(imo, 0, sizeof(*imo));
1483 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1484 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1485 }
1486 imo->imo_multicast_if_index = pktinfo->ipi_ifindex;
1487 pktopts->ippo_imo = imo;
1488 *flags |= IP_ROUTETOIFINDEX;
1489 }
1490 return error;
1491 }
1492
1493 /*
1494 * Set up IP outgoing packet options. Even if control is NULL,
1495 * pktopts->ippo_laddr and pktopts->ippo_imo are set and used.
1496 */
1497 int
1498 ip_setpktopts(struct mbuf *control, struct ip_pktopts *pktopts, int *flags,
1499 struct inpcb *inp, kauth_cred_t cred)
1500 {
1501 struct cmsghdr *cm;
1502 struct in_pktinfo pktinfo;
1503 int error;
1504
1505 pktopts->ippo_imo = inp->inp_moptions;
1506
1507 struct in_addr *ia = in_nullhost(in4p_prefsrcip(inp)) ? &in4p_laddr(inp) :
1508 &in4p_prefsrcip(inp);
1509 sockaddr_in_init(&pktopts->ippo_laddr, ia, 0);
1510
1511 if (control == NULL)
1512 return 0;
1513
1514 /*
1515 * XXX: Currently, we assume all the optional information is
1516 * stored in a single mbuf.
1517 */
1518 if (control->m_next)
1519 return EINVAL;
1520
1521 for (; control->m_len > 0;
1522 control->m_data += CMSG_ALIGN(cm->cmsg_len),
1523 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1524 cm = mtod(control, struct cmsghdr *);
1525 if ((control->m_len < sizeof(*cm)) ||
1526 (cm->cmsg_len == 0) ||
1527 (cm->cmsg_len > control->m_len)) {
1528 return EINVAL;
1529 }
1530 if (cm->cmsg_level != IPPROTO_IP)
1531 continue;
1532
1533 switch (cm->cmsg_type) {
1534 case IP_PKTINFO:
1535 if (cm->cmsg_len != CMSG_LEN(sizeof(pktinfo)))
1536 return EINVAL;
1537 memcpy(&pktinfo, CMSG_DATA(cm), sizeof(pktinfo));
1538 error = ip_pktinfo_prepare(inp, &pktinfo, pktopts,
1539 flags, cred);
1540 if (error)
1541 return error;
1542 break;
1543 case IP_SENDSRCADDR: /* FreeBSD compatibility */
1544 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in_addr)))
1545 return EINVAL;
1546 pktinfo.ipi_ifindex = 0;
1547 pktinfo.ipi_addr =
1548 ((struct in_pktinfo *)CMSG_DATA(cm))->ipi_addr;
1549 error = ip_pktinfo_prepare(inp, &pktinfo, pktopts,
1550 flags, cred);
1551 if (error)
1552 return error;
1553 break;
1554 default:
1555 return ENOPROTOOPT;
1556 }
1557 }
1558 return 0;
1559 }
1560
1561 /*
1562 * Set up IP options in pcb for insertion in output packets.
1563 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1564 * with destination address if source routed.
1565 */
1566 static int
1567 ip_pcbopts(struct inpcb *inp, const struct sockopt *sopt)
1568 {
1569 struct mbuf *m;
1570 const u_char *cp;
1571 u_char *dp;
1572 int cnt;
1573
1574 KASSERT(inp_locked(inp));
1575
1576 /* Turn off any old options. */
1577 if (inp->inp_options) {
1578 m_free(inp->inp_options);
1579 }
1580 inp->inp_options = NULL;
1581 if ((cnt = sopt->sopt_size) == 0) {
1582 /* Only turning off any previous options. */
1583 return 0;
1584 }
1585 cp = sopt->sopt_data;
1586
1587 if (cnt % 4) {
1588 /* Must be 4-byte aligned, because there's no padding. */
1589 return EINVAL;
1590 }
1591
1592 m = m_get(M_DONTWAIT, MT_SOOPTS);
1593 if (m == NULL)
1594 return ENOBUFS;
1595
1596 dp = mtod(m, u_char *);
1597 memset(dp, 0, sizeof(struct in_addr));
1598 dp += sizeof(struct in_addr);
1599 m->m_len = sizeof(struct in_addr);
1600
1601 /*
1602 * IP option list according to RFC791. Each option is of the form
1603 *
1604 * [optval] [olen] [(olen - 2) data bytes]
1605 *
1606 * We validate the list and copy options to an mbuf for prepending
1607 * to data packets. The IP first-hop destination address will be
1608 * stored before actual options and is zero if unset.
1609 */
1610 while (cnt > 0) {
1611 uint8_t optval, olen, offset;
1612
1613 optval = cp[IPOPT_OPTVAL];
1614
1615 if (optval == IPOPT_EOL || optval == IPOPT_NOP) {
1616 olen = 1;
1617 } else {
1618 if (cnt < IPOPT_OLEN + 1)
1619 goto bad;
1620
1621 olen = cp[IPOPT_OLEN];
1622 if (olen < IPOPT_OLEN + 1 || olen > cnt)
1623 goto bad;
1624 }
1625
1626 if (optval == IPOPT_LSRR || optval == IPOPT_SSRR) {
1627 /*
1628 * user process specifies route as:
1629 * ->A->B->C->D
1630 * D must be our final destination (but we can't
1631 * check that since we may not have connected yet).
1632 * A is first hop destination, which doesn't appear in
1633 * actual IP option, but is stored before the options.
1634 */
1635 if (olen < IPOPT_OFFSET + 1 + sizeof(struct in_addr))
1636 goto bad;
1637
1638 offset = cp[IPOPT_OFFSET];
1639 memcpy(mtod(m, u_char *), cp + IPOPT_OFFSET + 1,
1640 sizeof(struct in_addr));
1641
1642 cp += sizeof(struct in_addr);
1643 cnt -= sizeof(struct in_addr);
1644 olen -= sizeof(struct in_addr);
1645
1646 if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
1647 goto bad;
1648
1649 memcpy(dp, cp, olen);
1650 dp[IPOPT_OPTVAL] = optval;
1651 dp[IPOPT_OLEN] = olen;
1652 dp[IPOPT_OFFSET] = offset;
1653 break;
1654 } else {
1655 if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
1656 goto bad;
1657
1658 memcpy(dp, cp, olen);
1659 break;
1660 }
1661
1662 dp += olen;
1663 m->m_len += olen;
1664
1665 if (optval == IPOPT_EOL)
1666 break;
1667
1668 cp += olen;
1669 cnt -= olen;
1670 }
1671
1672 inp->inp_options = m;
1673 return 0;
1674
1675 bad:
1676 (void)m_free(m);
1677 return EINVAL;
1678 }
1679
1680 /*
1681 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1682 * Must be called in a pserialize critical section.
1683 */
1684 static struct ifnet *
1685 ip_multicast_if(struct in_addr *a, int *ifindexp)
1686 {
1687 int ifindex;
1688 struct ifnet *ifp = NULL;
1689 struct in_ifaddr *ia;
1690
1691 if (ifindexp)
1692 *ifindexp = 0;
1693 if (ntohl(a->s_addr) >> 24 == 0) {
1694 ifindex = ntohl(a->s_addr) & 0xffffff;
1695 ifp = if_byindex(ifindex);
1696 if (!ifp)
1697 return NULL;
1698 if (ifindexp)
1699 *ifindexp = ifindex;
1700 } else {
1701 IN_ADDRHASH_READER_FOREACH(ia, a->s_addr) {
1702 if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
1703 (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
1704 ifp = ia->ia_ifp;
1705 if (if_is_deactivated(ifp))
1706 ifp = NULL;
1707 break;
1708 }
1709 }
1710 }
1711 return ifp;
1712 }
1713
1714 static int
1715 ip_getoptval(const struct sockopt *sopt, u_int8_t *val, u_int maxval)
1716 {
1717 u_int tval;
1718 u_char cval;
1719 int error;
1720
1721 if (sopt == NULL)
1722 return EINVAL;
1723
1724 switch (sopt->sopt_size) {
1725 case sizeof(u_char):
1726 error = sockopt_get(sopt, &cval, sizeof(u_char));
1727 tval = cval;
1728 break;
1729
1730 case sizeof(u_int):
1731 error = sockopt_get(sopt, &tval, sizeof(u_int));
1732 break;
1733
1734 default:
1735 error = EINVAL;
1736 }
1737
1738 if (error)
1739 return error;
1740
1741 if (tval > maxval)
1742 return EINVAL;
1743
1744 *val = tval;
1745 return 0;
1746 }
1747
1748 static int
1749 ip_get_membership(const struct sockopt *sopt, struct ifnet **ifp,
1750 struct psref *psref, struct in_addr *ia, bool add)
1751 {
1752 int error;
1753 struct ip_mreq mreq;
1754
1755 error = sockopt_get(sopt, &mreq, sizeof(mreq));
1756 if (error)
1757 return error;
1758
1759 if (!IN_MULTICAST(mreq.imr_multiaddr.s_addr))
1760 return EINVAL;
1761
1762 memcpy(ia, &mreq.imr_multiaddr, sizeof(*ia));
1763
1764 if (in_nullhost(mreq.imr_interface)) {
1765 union {
1766 struct sockaddr dst;
1767 struct sockaddr_in dst4;
1768 } u;
1769 struct route ro;
1770
1771 if (!add) {
1772 *ifp = NULL;
1773 return 0;
1774 }
1775 /*
1776 * If no interface address was provided, use the interface of
1777 * the route to the given multicast address.
1778 */
1779 struct rtentry *rt;
1780 memset(&ro, 0, sizeof(ro));
1781
1782 sockaddr_in_init(&u.dst4, ia, 0);
1783 error = rtcache_setdst(&ro, &u.dst);
1784 if (error != 0)
1785 return error;
1786 *ifp = (rt = rtcache_init(&ro)) != NULL ? rt->rt_ifp : NULL;
1787 if (*ifp != NULL) {
1788 if (if_is_deactivated(*ifp))
1789 *ifp = NULL;
1790 else
1791 if_acquire(*ifp, psref);
1792 }
1793 rtcache_unref(rt, &ro);
1794 rtcache_free(&ro);
1795 } else {
1796 int s = pserialize_read_enter();
1797 *ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1798 if (!add && *ifp == NULL) {
1799 pserialize_read_exit(s);
1800 return EADDRNOTAVAIL;
1801 }
1802 if (*ifp != NULL) {
1803 if (if_is_deactivated(*ifp))
1804 *ifp = NULL;
1805 else
1806 if_acquire(*ifp, psref);
1807 }
1808 pserialize_read_exit(s);
1809 }
1810 return 0;
1811 }
1812
1813 /*
1814 * Add a multicast group membership.
1815 * Group must be a valid IP multicast address.
1816 */
1817 static int
1818 ip_add_membership(struct ip_moptions *imo, const struct sockopt *sopt)
1819 {
1820 struct ifnet *ifp = NULL; // XXX: gcc [ppc]
1821 struct in_addr ia;
1822 int i, error, bound;
1823 struct psref psref;
1824
1825 /* imo is protected by solock or referenced only by the caller */
1826
1827 bound = curlwp_bind();
1828 if (sopt->sopt_size == sizeof(struct ip_mreq))
1829 error = ip_get_membership(sopt, &ifp, &psref, &ia, true);
1830 else {
1831 #ifdef INET6
1832 error = ip6_get_membership(sopt, &ifp, &psref, &ia, sizeof(ia));
1833 #else
1834 error = EINVAL;
1835 #endif
1836 }
1837
1838 if (error)
1839 goto out;
1840
1841 /*
1842 * See if we found an interface, and confirm that it
1843 * supports multicast.
1844 */
1845 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1846 error = EADDRNOTAVAIL;
1847 goto out;
1848 }
1849
1850 /*
1851 * See if the membership already exists or if all the
1852 * membership slots are full.
1853 */
1854 for (i = 0; i < imo->imo_num_memberships; ++i) {
1855 if (imo->imo_membership[i]->inm_ifp == ifp &&
1856 in_hosteq(imo->imo_membership[i]->inm_addr, ia))
1857 break;
1858 }
1859 if (i < imo->imo_num_memberships) {
1860 error = EADDRINUSE;
1861 goto out;
1862 }
1863
1864 if (i == IP_MAX_MEMBERSHIPS) {
1865 error = ETOOMANYREFS;
1866 goto out;
1867 }
1868
1869 /*
1870 * Everything looks good; add a new record to the multicast
1871 * address list for the given interface.
1872 */
1873 imo->imo_membership[i] = in_addmulti(&ia, ifp);
1874 if (imo->imo_membership[i] == NULL) {
1875 error = ENOBUFS;
1876 goto out;
1877 }
1878
1879 ++imo->imo_num_memberships;
1880 error = 0;
1881 out:
1882 if_put(ifp, &psref);
1883 curlwp_bindx(bound);
1884 return error;
1885 }
1886
1887 /*
1888 * Drop a multicast group membership.
1889 * Group must be a valid IP multicast address.
1890 */
1891 static int
1892 ip_drop_membership(struct ip_moptions *imo, const struct sockopt *sopt)
1893 {
1894 struct in_addr ia = { .s_addr = 0 }; // XXX: gcc [ppc]
1895 struct ifnet *ifp = NULL; // XXX: gcc [ppc]
1896 int i, error, bound;
1897 struct psref psref;
1898
1899 /* imo is protected by solock or referenced only by the caller */
1900
1901 bound = curlwp_bind();
1902 if (sopt->sopt_size == sizeof(struct ip_mreq))
1903 error = ip_get_membership(sopt, &ifp, &psref, &ia, false);
1904 else {
1905 #ifdef INET6
1906 error = ip6_get_membership(sopt, &ifp, &psref, &ia, sizeof(ia));
1907 #else
1908 error = EINVAL;
1909 #endif
1910 }
1911
1912 if (error)
1913 goto out;
1914
1915 /*
1916 * Find the membership in the membership array.
1917 */
1918 for (i = 0; i < imo->imo_num_memberships; ++i) {
1919 if ((ifp == NULL ||
1920 imo->imo_membership[i]->inm_ifp == ifp) &&
1921 in_hosteq(imo->imo_membership[i]->inm_addr, ia))
1922 break;
1923 }
1924 if (i == imo->imo_num_memberships) {
1925 error = EADDRNOTAVAIL;
1926 goto out;
1927 }
1928
1929 /*
1930 * Give up the multicast address record to which the
1931 * membership points.
1932 */
1933 in_delmulti(imo->imo_membership[i]);
1934
1935 /*
1936 * Remove the gap in the membership array.
1937 */
1938 for (++i; i < imo->imo_num_memberships; ++i)
1939 imo->imo_membership[i-1] = imo->imo_membership[i];
1940 --imo->imo_num_memberships;
1941 error = 0;
1942 out:
1943 if_put(ifp, &psref);
1944 curlwp_bindx(bound);
1945 return error;
1946 }
1947
1948 /*
1949 * Set the IP multicast options in response to user setsockopt().
1950 */
1951 int
1952 ip_setmoptions(struct ip_moptions **pimo, const struct sockopt *sopt)
1953 {
1954 struct ip_moptions *imo = *pimo;
1955 struct in_addr addr;
1956 struct ifnet *ifp;
1957 int ifindex, error = 0;
1958
1959 /* The passed imo isn't NULL, it should be protected by solock */
1960
1961 if (!imo) {
1962 /*
1963 * No multicast option buffer attached to the pcb;
1964 * allocate one and initialize to default values.
1965 */
1966 imo = kmem_intr_alloc(sizeof(*imo), KM_NOSLEEP);
1967 if (imo == NULL)
1968 return ENOBUFS;
1969
1970 imo->imo_multicast_if_index = 0;
1971 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1972 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1973 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1974 imo->imo_num_memberships = 0;
1975 *pimo = imo;
1976 }
1977
1978 switch (sopt->sopt_name) {
1979 case IP_MULTICAST_IF: {
1980 int s;
1981 /*
1982 * Select the interface for outgoing multicast packets.
1983 */
1984 error = sockopt_get(sopt, &addr, sizeof(addr));
1985 if (error)
1986 break;
1987
1988 /*
1989 * INADDR_ANY is used to remove a previous selection.
1990 * When no interface is selected, a default one is
1991 * chosen every time a multicast packet is sent.
1992 */
1993 if (in_nullhost(addr)) {
1994 imo->imo_multicast_if_index = 0;
1995 break;
1996 }
1997 /*
1998 * The selected interface is identified by its local
1999 * IP address. Find the interface and confirm that
2000 * it supports multicasting.
2001 */
2002 s = pserialize_read_enter();
2003 ifp = ip_multicast_if(&addr, &ifindex);
2004 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2005 pserialize_read_exit(s);
2006 error = EADDRNOTAVAIL;
2007 break;
2008 }
2009 imo->imo_multicast_if_index = ifp->if_index;
2010 pserialize_read_exit(s);
2011 if (ifindex)
2012 imo->imo_multicast_addr = addr;
2013 else
2014 imo->imo_multicast_addr.s_addr = INADDR_ANY;
2015 break;
2016 }
2017
2018 case IP_MULTICAST_TTL:
2019 /*
2020 * Set the IP time-to-live for outgoing multicast packets.
2021 */
2022 error = ip_getoptval(sopt, &imo->imo_multicast_ttl, MAXTTL);
2023 break;
2024
2025 case IP_MULTICAST_LOOP:
2026 /*
2027 * Set the loopback flag for outgoing multicast packets.
2028 * Must be zero or one.
2029 */
2030 error = ip_getoptval(sopt, &imo->imo_multicast_loop, 1);
2031 break;
2032
2033 case IP_ADD_MEMBERSHIP: /* IPV6_JOIN_GROUP */
2034 error = ip_add_membership(imo, sopt);
2035 break;
2036
2037 case IP_DROP_MEMBERSHIP: /* IPV6_LEAVE_GROUP */
2038 error = ip_drop_membership(imo, sopt);
2039 break;
2040
2041 default:
2042 error = EOPNOTSUPP;
2043 break;
2044 }
2045
2046 /*
2047 * If all options have default values, no need to keep the mbuf.
2048 */
2049 if (imo->imo_multicast_if_index == 0 &&
2050 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
2051 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
2052 imo->imo_num_memberships == 0) {
2053 kmem_intr_free(imo, sizeof(*imo));
2054 *pimo = NULL;
2055 }
2056
2057 return error;
2058 }
2059
2060 /*
2061 * Return the IP multicast options in response to user getsockopt().
2062 */
2063 int
2064 ip_getmoptions(struct ip_moptions *imo, struct sockopt *sopt)
2065 {
2066 struct in_addr addr;
2067 uint8_t optval;
2068 int error = 0;
2069
2070 /* imo is protected by solock or referenced only by the caller */
2071
2072 switch (sopt->sopt_name) {
2073 case IP_MULTICAST_IF:
2074 if (imo == NULL || imo->imo_multicast_if_index == 0)
2075 addr = zeroin_addr;
2076 else if (imo->imo_multicast_addr.s_addr) {
2077 /* return the value user has set */
2078 addr = imo->imo_multicast_addr;
2079 } else {
2080 struct ifnet *ifp;
2081 struct in_ifaddr *ia = NULL;
2082 int s = pserialize_read_enter();
2083
2084 ifp = if_byindex(imo->imo_multicast_if_index);
2085 if (ifp != NULL) {
2086 ia = in_get_ia_from_ifp(ifp);
2087 }
2088 addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
2089 pserialize_read_exit(s);
2090 }
2091 error = sockopt_set(sopt, &addr, sizeof(addr));
2092 break;
2093
2094 case IP_MULTICAST_TTL:
2095 optval = imo ? imo->imo_multicast_ttl
2096 : IP_DEFAULT_MULTICAST_TTL;
2097
2098 error = sockopt_set(sopt, &optval, sizeof(optval));
2099 break;
2100
2101 case IP_MULTICAST_LOOP:
2102 optval = imo ? imo->imo_multicast_loop
2103 : IP_DEFAULT_MULTICAST_LOOP;
2104
2105 error = sockopt_set(sopt, &optval, sizeof(optval));
2106 break;
2107
2108 default:
2109 error = EOPNOTSUPP;
2110 }
2111
2112 return error;
2113 }
2114
2115 /*
2116 * Discard the IP multicast options.
2117 */
2118 void
2119 ip_freemoptions(struct ip_moptions *imo)
2120 {
2121 int i;
2122
2123 /* The owner of imo (inp) should be protected by solock */
2124
2125 if (imo != NULL) {
2126 for (i = 0; i < imo->imo_num_memberships; ++i) {
2127 struct in_multi *inm = imo->imo_membership[i];
2128 in_delmulti(inm);
2129 /* ifp should not leave thanks to solock */
2130 }
2131
2132 kmem_intr_free(imo, sizeof(*imo));
2133 }
2134 }
2135
2136 /*
2137 * Routine called from ip_output() to loop back a copy of an IP multicast
2138 * packet to the input queue of a specified interface. Note that this
2139 * calls the output routine of the loopback "driver", but with an interface
2140 * pointer that might NOT be lo0ifp -- easier than replicating that code here.
2141 */
2142 static void
2143 ip_mloopback(struct ifnet *ifp, struct mbuf *m, const struct sockaddr_in *dst)
2144 {
2145 struct ip *ip;
2146 struct mbuf *copym;
2147
2148 copym = m_copypacket(m, M_DONTWAIT);
2149 if (copym != NULL &&
2150 (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
2151 copym = m_pullup(copym, sizeof(struct ip));
2152 if (copym == NULL)
2153 return;
2154 /*
2155 * We don't bother to fragment if the IP length is greater
2156 * than the interface's MTU. Can this possibly matter?
2157 */
2158 ip = mtod(copym, struct ip *);
2159
2160 if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
2161 in_undefer_cksum_tcpudp(copym);
2162 copym->m_pkthdr.csum_flags &=
2163 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
2164 }
2165
2166 ip->ip_sum = 0;
2167 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
2168 KERNEL_LOCK_UNLESS_NET_MPSAFE();
2169 (void)looutput(ifp, copym, sintocsa(dst), NULL);
2170 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2171 }
2172
2173 /*
2174 * Ensure sending address is valid.
2175 * Returns 0 on success, -1 if an error should be sent back or 1
2176 * if the packet could be dropped without error (protocol dependent).
2177 */
2178 static int
2179 ip_ifaddrvalid(const struct in_ifaddr *ia)
2180 {
2181
2182 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
2183 return 0;
2184
2185 if (ia->ia4_flags & IN_IFF_DUPLICATED)
2186 return -1;
2187 else if (ia->ia4_flags & (IN_IFF_TENTATIVE | IN_IFF_DETACHED))
2188 return 1;
2189
2190 return 0;
2191 }
2192