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