ip_output.c revision 1.235 1 /* $NetBSD: ip_output.c,v 1.235 2015/03/31 08:44:43 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.235 2015/03/31 08:44:43 ozaki-r Exp $");
95
96 #include "opt_inet.h"
97 #include "opt_ipsec.h"
98 #include "opt_mrouting.h"
99
100 #include <sys/param.h>
101 #include <sys/kmem.h>
102 #include <sys/mbuf.h>
103 #include <sys/protosw.h>
104 #include <sys/socket.h>
105 #include <sys/socketvar.h>
106 #include <sys/kauth.h>
107 #ifdef IPSEC
108 #include <sys/domain.h>
109 #endif
110 #include <sys/systm.h>
111
112 #include <net/if.h>
113 #include <net/route.h>
114 #include <net/pfil.h>
115
116 #include <netinet/in.h>
117 #include <netinet/in_systm.h>
118 #include <netinet/ip.h>
119 #include <netinet/in_pcb.h>
120 #include <netinet/in_var.h>
121 #include <netinet/ip_var.h>
122 #include <netinet/ip_private.h>
123 #include <netinet/in_offload.h>
124 #include <netinet/portalgo.h>
125 #include <netinet/udp.h>
126
127 #ifdef INET6
128 #include <netinet6/ip6_var.h>
129 #endif
130
131 #ifdef MROUTING
132 #include <netinet/ip_mroute.h>
133 #endif
134
135 #ifdef IPSEC
136 #include <netipsec/ipsec.h>
137 #include <netipsec/key.h>
138 #endif
139
140 static int ip_pcbopts(struct inpcb *, const struct sockopt *);
141 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
142 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
143 static void ip_mloopback(struct ifnet *, struct mbuf *,
144 const struct sockaddr_in *);
145
146 extern pfil_head_t *inet_pfil_hook; /* XXX */
147
148 int ip_do_loopback_cksum = 0;
149
150 /*
151 * IP output. The packet in mbuf chain m contains a skeletal IP
152 * header (with len, off, ttl, proto, tos, src, dst).
153 * The mbuf chain containing the packet will be freed.
154 * The mbuf opt, if present, will not be freed.
155 */
156 int
157 ip_output(struct mbuf *m0, ...)
158 {
159 struct rtentry *rt;
160 struct ip *ip;
161 struct ifnet *ifp;
162 struct mbuf *m = m0;
163 int hlen = sizeof (struct ip);
164 int len, error = 0;
165 struct route iproute;
166 const struct sockaddr_in *dst;
167 struct in_ifaddr *ia;
168 int isbroadcast;
169 struct mbuf *opt;
170 struct route *ro;
171 int flags, sw_csum;
172 u_long mtu;
173 struct ip_moptions *imo;
174 struct socket *so;
175 va_list ap;
176 #ifdef IPSEC
177 struct secpolicy *sp = NULL;
178 #endif
179 bool natt_frag = false;
180 bool rtmtu_nolock;
181 union {
182 struct sockaddr dst;
183 struct sockaddr_in dst4;
184 } u;
185 struct sockaddr *rdst = &u.dst; /* real IP destination, as opposed
186 * to the nexthop
187 */
188
189 len = 0;
190 va_start(ap, m0);
191 opt = va_arg(ap, struct mbuf *);
192 ro = va_arg(ap, struct route *);
193 flags = va_arg(ap, int);
194 imo = va_arg(ap, struct ip_moptions *);
195 so = va_arg(ap, struct socket *);
196 va_end(ap);
197
198 MCLAIM(m, &ip_tx_mowner);
199
200 KASSERT((m->m_flags & M_PKTHDR) != 0);
201 KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) == 0);
202 KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) !=
203 (M_CSUM_TCPv4|M_CSUM_UDPv4));
204
205 if (opt) {
206 m = ip_insertoptions(m, opt, &len);
207 if (len >= sizeof(struct ip))
208 hlen = len;
209 }
210 ip = mtod(m, struct ip *);
211
212 /*
213 * Fill in IP header.
214 */
215 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
216 ip->ip_v = IPVERSION;
217 ip->ip_off = htons(0);
218 /* ip->ip_id filled in after we find out source ia */
219 ip->ip_hl = hlen >> 2;
220 IP_STATINC(IP_STAT_LOCALOUT);
221 } else {
222 hlen = ip->ip_hl << 2;
223 }
224
225 /*
226 * Route packet.
227 */
228 if (ro == NULL) {
229 memset(&iproute, 0, sizeof(iproute));
230 ro = &iproute;
231 }
232 sockaddr_in_init(&u.dst4, &ip->ip_dst, 0);
233 dst = satocsin(rtcache_getdst(ro));
234
235 /*
236 * If there is a cached route, check that it is to the same
237 * destination and is still up. If not, free it and try again.
238 * The address family should also be checked in case of sharing
239 * the cache with IPv6.
240 */
241 if (dst && (dst->sin_family != AF_INET ||
242 !in_hosteq(dst->sin_addr, ip->ip_dst)))
243 rtcache_free(ro);
244
245 if ((rt = rtcache_validate(ro)) == NULL &&
246 (rt = rtcache_update(ro, 1)) == NULL) {
247 dst = &u.dst4;
248 rtcache_setdst(ro, &u.dst);
249 }
250
251 /*
252 * If routing to interface only, short circuit routing lookup.
253 */
254 if (flags & IP_ROUTETOIF) {
255 if ((ia = ifatoia(ifa_ifwithladdr(sintocsa(dst)))) == NULL) {
256 IP_STATINC(IP_STAT_NOROUTE);
257 error = ENETUNREACH;
258 goto bad;
259 }
260 ifp = ia->ia_ifp;
261 mtu = ifp->if_mtu;
262 ip->ip_ttl = 1;
263 isbroadcast = in_broadcast(dst->sin_addr, ifp);
264 } else if ((IN_MULTICAST(ip->ip_dst.s_addr) ||
265 ip->ip_dst.s_addr == INADDR_BROADCAST) &&
266 imo != NULL && imo->imo_multicast_ifp != NULL) {
267 ifp = imo->imo_multicast_ifp;
268 mtu = ifp->if_mtu;
269 IFP_TO_IA(ifp, ia);
270 isbroadcast = 0;
271 } else {
272 if (rt == NULL)
273 rt = rtcache_init(ro);
274 if (rt == NULL) {
275 IP_STATINC(IP_STAT_NOROUTE);
276 error = EHOSTUNREACH;
277 goto bad;
278 }
279 ia = ifatoia(rt->rt_ifa);
280 ifp = rt->rt_ifp;
281 if ((mtu = rt->rt_rmx.rmx_mtu) == 0)
282 mtu = ifp->if_mtu;
283 rt->rt_use++;
284 if (rt->rt_flags & RTF_GATEWAY)
285 dst = satosin(rt->rt_gateway);
286 if (rt->rt_flags & RTF_HOST)
287 isbroadcast = rt->rt_flags & RTF_BROADCAST;
288 else
289 isbroadcast = in_broadcast(dst->sin_addr, ifp);
290 }
291 rtmtu_nolock = rt && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0;
292
293 if (IN_MULTICAST(ip->ip_dst.s_addr) ||
294 (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
295 bool inmgroup;
296
297 m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
298 M_BCAST : M_MCAST;
299 /*
300 * See if the caller provided any multicast options
301 */
302 if (imo != NULL)
303 ip->ip_ttl = imo->imo_multicast_ttl;
304 else
305 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
306
307 /*
308 * if we don't know the outgoing ifp yet, we can't generate
309 * output
310 */
311 if (!ifp) {
312 IP_STATINC(IP_STAT_NOROUTE);
313 error = ENETUNREACH;
314 goto bad;
315 }
316
317 /*
318 * If the packet is multicast or broadcast, confirm that
319 * the outgoing interface can transmit it.
320 */
321 if (((m->m_flags & M_MCAST) &&
322 (ifp->if_flags & IFF_MULTICAST) == 0) ||
323 ((m->m_flags & M_BCAST) &&
324 (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)) {
325 IP_STATINC(IP_STAT_NOROUTE);
326 error = ENETUNREACH;
327 goto bad;
328 }
329 /*
330 * If source address not specified yet, use an address
331 * of outgoing interface.
332 */
333 if (in_nullhost(ip->ip_src)) {
334 struct in_ifaddr *xia;
335 struct ifaddr *xifa;
336
337 IFP_TO_IA(ifp, xia);
338 if (!xia) {
339 error = EADDRNOTAVAIL;
340 goto bad;
341 }
342 xifa = &xia->ia_ifa;
343 if (xifa->ifa_getifa != NULL) {
344 xia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
345 }
346 ip->ip_src = xia->ia_addr.sin_addr;
347 }
348
349 inmgroup = in_multi_group(ip->ip_dst, ifp, flags);
350 if (inmgroup && (imo == NULL || imo->imo_multicast_loop)) {
351 /*
352 * If we belong to the destination multicast group
353 * on the outgoing interface, and the caller did not
354 * forbid loopback, loop back a copy.
355 */
356 ip_mloopback(ifp, m, &u.dst4);
357 }
358 #ifdef MROUTING
359 else {
360 /*
361 * If we are acting as a multicast router, perform
362 * multicast forwarding as if the packet had just
363 * arrived on the interface to which we are about
364 * to send. The multicast forwarding function
365 * recursively calls this function, using the
366 * IP_FORWARDING flag to prevent infinite recursion.
367 *
368 * Multicasts that are looped back by ip_mloopback(),
369 * above, will be forwarded by the ip_input() routine,
370 * if necessary.
371 */
372 extern struct socket *ip_mrouter;
373
374 if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
375 if (ip_mforward(m, ifp) != 0) {
376 m_freem(m);
377 goto done;
378 }
379 }
380 }
381 #endif
382 /*
383 * Multicasts with a time-to-live of zero may be looped-
384 * back, above, but must not be transmitted on a network.
385 * Also, multicasts addressed to the loopback interface
386 * are not sent -- the above call to ip_mloopback() will
387 * loop back a copy if this host actually belongs to the
388 * destination group on the loopback interface.
389 */
390 if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
391 m_freem(m);
392 goto done;
393 }
394 goto sendit;
395 }
396
397 /*
398 * If source address not specified yet, use address
399 * of outgoing interface.
400 */
401 if (in_nullhost(ip->ip_src)) {
402 struct ifaddr *xifa;
403
404 xifa = &ia->ia_ifa;
405 if (xifa->ifa_getifa != NULL)
406 ia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
407 ip->ip_src = ia->ia_addr.sin_addr;
408 }
409
410 /*
411 * packets with Class-D address as source are not valid per
412 * RFC 1112
413 */
414 if (IN_MULTICAST(ip->ip_src.s_addr)) {
415 IP_STATINC(IP_STAT_ODROPPED);
416 error = EADDRNOTAVAIL;
417 goto bad;
418 }
419
420 /*
421 * Look for broadcast address and and verify user is allowed to
422 * send such a packet.
423 */
424 if (isbroadcast) {
425 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
426 error = EADDRNOTAVAIL;
427 goto bad;
428 }
429 if ((flags & IP_ALLOWBROADCAST) == 0) {
430 error = EACCES;
431 goto bad;
432 }
433 /* don't allow broadcast messages to be fragmented */
434 if (ntohs(ip->ip_len) > ifp->if_mtu) {
435 error = EMSGSIZE;
436 goto bad;
437 }
438 m->m_flags |= M_BCAST;
439 } else
440 m->m_flags &= ~M_BCAST;
441
442 sendit:
443 if ((flags & (IP_FORWARDING|IP_NOIPNEWID)) == 0) {
444 if (m->m_pkthdr.len < IP_MINFRAGSIZE) {
445 ip->ip_id = 0;
446 } else if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
447 ip->ip_id = ip_newid(ia);
448 } else {
449
450 /*
451 * TSO capable interfaces (typically?) increment
452 * ip_id for each segment.
453 * "allocate" enough ids here to increase the chance
454 * for them to be unique.
455 *
456 * note that the following calculation is not
457 * needed to be precise. wasting some ip_id is fine.
458 */
459
460 unsigned int segsz = m->m_pkthdr.segsz;
461 unsigned int datasz = ntohs(ip->ip_len) - hlen;
462 unsigned int num = howmany(datasz, segsz);
463
464 ip->ip_id = ip_newid_range(ia, num);
465 }
466 }
467
468 /*
469 * If we're doing Path MTU Discovery, we need to set DF unless
470 * the route's MTU is locked.
471 */
472 if ((flags & IP_MTUDISC) != 0 && rtmtu_nolock) {
473 ip->ip_off |= htons(IP_DF);
474 }
475
476 #ifdef IPSEC
477 if (ipsec_used) {
478 bool ipsec_done = false;
479
480 /* Perform IPsec processing, if any. */
481 error = ipsec4_output(m, so, flags, &sp, &mtu, &natt_frag,
482 &ipsec_done);
483 if (error || ipsec_done)
484 goto done;
485 }
486 #endif
487
488 /*
489 * Run through list of hooks for output packets.
490 */
491 error = pfil_run_hooks(inet_pfil_hook, &m, ifp, PFIL_OUT);
492 if (error)
493 goto done;
494 if (m == NULL)
495 goto done;
496
497 ip = mtod(m, struct ip *);
498 hlen = ip->ip_hl << 2;
499
500 m->m_pkthdr.csum_data |= hlen << 16;
501
502 #if IFA_STATS
503 /*
504 * search for the source address structure to
505 * maintain output statistics.
506 */
507 INADDR_TO_IA(ip->ip_src, ia);
508 #endif
509
510 /* Maybe skip checksums on loopback interfaces. */
511 if (IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) {
512 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
513 }
514 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
515 /*
516 * If small enough for mtu of path, or if using TCP segmentation
517 * offload, can just send directly.
518 */
519 if (ntohs(ip->ip_len) <= mtu ||
520 (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0) {
521 const struct sockaddr *sa;
522
523 #if IFA_STATS
524 if (ia)
525 ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
526 #endif
527 /*
528 * Always initialize the sum to 0! Some HW assisted
529 * checksumming requires this.
530 */
531 ip->ip_sum = 0;
532
533 if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
534 /*
535 * Perform any checksums that the hardware can't do
536 * for us.
537 *
538 * XXX Does any hardware require the {th,uh}_sum
539 * XXX fields to be 0?
540 */
541 if (sw_csum & M_CSUM_IPv4) {
542 KASSERT(IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4));
543 ip->ip_sum = in_cksum(m, hlen);
544 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
545 }
546 if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
547 if (IN_NEED_CHECKSUM(ifp,
548 sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
549 in_delayed_cksum(m);
550 }
551 m->m_pkthdr.csum_flags &=
552 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
553 }
554 }
555
556 sa = (m->m_flags & M_MCAST) ? sintocsa(rdst) : sintocsa(dst);
557 if (__predict_true(
558 (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0 ||
559 (ifp->if_capenable & IFCAP_TSOv4) != 0)) {
560 KERNEL_LOCK(1, NULL);
561 error = (*ifp->if_output)(ifp, m, sa, rt);
562 KERNEL_UNLOCK_ONE(NULL);
563 } else {
564 error = ip_tso_output(ifp, m, sa, rt);
565 }
566 goto done;
567 }
568
569 /*
570 * We can't use HW checksumming if we're about to
571 * to fragment the packet.
572 *
573 * XXX Some hardware can do this.
574 */
575 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
576 if (IN_NEED_CHECKSUM(ifp,
577 m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
578 in_delayed_cksum(m);
579 }
580 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
581 }
582
583 /*
584 * Too large for interface; fragment if possible.
585 * Must be able to put at least 8 bytes per fragment.
586 */
587 if (ntohs(ip->ip_off) & IP_DF) {
588 if (flags & IP_RETURNMTU) {
589 struct inpcb *inp;
590
591 KASSERT(so && solocked(so));
592 inp = sotoinpcb(so);
593 inp->inp_errormtu = mtu;
594 }
595 error = EMSGSIZE;
596 IP_STATINC(IP_STAT_CANTFRAG);
597 goto bad;
598 }
599
600 error = ip_fragment(m, ifp, mtu);
601 if (error) {
602 m = NULL;
603 goto bad;
604 }
605
606 for (; m; m = m0) {
607 m0 = m->m_nextpkt;
608 m->m_nextpkt = 0;
609 if (error) {
610 m_freem(m);
611 continue;
612 }
613 #if IFA_STATS
614 if (ia)
615 ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
616 #endif
617 /*
618 * If we get there, the packet has not been handled by
619 * IPsec whereas it should have. Now that it has been
620 * fragmented, re-inject it in ip_output so that IPsec
621 * processing can occur.
622 */
623 if (natt_frag) {
624 error = ip_output(m, opt, ro,
625 flags | IP_RAWOUTPUT | IP_NOIPNEWID,
626 imo, so);
627 } else {
628 KASSERT((m->m_pkthdr.csum_flags &
629 (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
630 KERNEL_LOCK(1, NULL);
631 error = (*ifp->if_output)(ifp, m,
632 (m->m_flags & M_MCAST) ?
633 sintocsa(rdst) : sintocsa(dst), rt);
634 KERNEL_UNLOCK_ONE(NULL);
635 }
636 }
637 if (error == 0) {
638 IP_STATINC(IP_STAT_FRAGMENTED);
639 }
640 done:
641 if (ro == &iproute) {
642 rtcache_free(&iproute);
643 }
644 #ifdef IPSEC
645 if (sp) {
646 KEY_FREESP(&sp);
647 }
648 #endif
649 return error;
650 bad:
651 m_freem(m);
652 goto done;
653 }
654
655 int
656 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
657 {
658 struct ip *ip, *mhip;
659 struct mbuf *m0;
660 int len, hlen, off;
661 int mhlen, firstlen;
662 struct mbuf **mnext;
663 int sw_csum = m->m_pkthdr.csum_flags;
664 int fragments = 0;
665 int s;
666 int error = 0;
667
668 ip = mtod(m, struct ip *);
669 hlen = ip->ip_hl << 2;
670 if (ifp != NULL)
671 sw_csum &= ~ifp->if_csum_flags_tx;
672
673 len = (mtu - hlen) &~ 7;
674 if (len < 8) {
675 m_freem(m);
676 return (EMSGSIZE);
677 }
678
679 firstlen = len;
680 mnext = &m->m_nextpkt;
681
682 /*
683 * Loop through length of segment after first fragment,
684 * make new header and copy data of each part and link onto chain.
685 */
686 m0 = m;
687 mhlen = sizeof (struct ip);
688 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
689 MGETHDR(m, M_DONTWAIT, MT_HEADER);
690 if (m == 0) {
691 error = ENOBUFS;
692 IP_STATINC(IP_STAT_ODROPPED);
693 goto sendorfree;
694 }
695 MCLAIM(m, m0->m_owner);
696 *mnext = m;
697 mnext = &m->m_nextpkt;
698 m->m_data += max_linkhdr;
699 mhip = mtod(m, struct ip *);
700 *mhip = *ip;
701 /* we must inherit MCAST and BCAST flags */
702 m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
703 if (hlen > sizeof (struct ip)) {
704 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
705 mhip->ip_hl = mhlen >> 2;
706 }
707 m->m_len = mhlen;
708 mhip->ip_off = ((off - hlen) >> 3) +
709 (ntohs(ip->ip_off) & ~IP_MF);
710 if (ip->ip_off & htons(IP_MF))
711 mhip->ip_off |= IP_MF;
712 if (off + len >= ntohs(ip->ip_len))
713 len = ntohs(ip->ip_len) - off;
714 else
715 mhip->ip_off |= IP_MF;
716 HTONS(mhip->ip_off);
717 mhip->ip_len = htons((u_int16_t)(len + mhlen));
718 m->m_next = m_copym(m0, off, len, M_DONTWAIT);
719 if (m->m_next == 0) {
720 error = ENOBUFS; /* ??? */
721 IP_STATINC(IP_STAT_ODROPPED);
722 goto sendorfree;
723 }
724 m->m_pkthdr.len = mhlen + len;
725 m->m_pkthdr.rcvif = NULL;
726 mhip->ip_sum = 0;
727 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
728 if (sw_csum & M_CSUM_IPv4) {
729 mhip->ip_sum = in_cksum(m, mhlen);
730 } else {
731 /*
732 * checksum is hw-offloaded or not necessary.
733 */
734 m->m_pkthdr.csum_flags |=
735 m0->m_pkthdr.csum_flags & M_CSUM_IPv4;
736 m->m_pkthdr.csum_data |= mhlen << 16;
737 KASSERT(!(ifp != NULL &&
738 IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4))
739 || (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
740 }
741 IP_STATINC(IP_STAT_OFRAGMENTS);
742 fragments++;
743 }
744 /*
745 * Update first fragment by trimming what's been copied out
746 * and updating header, then send each fragment (in order).
747 */
748 m = m0;
749 m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
750 m->m_pkthdr.len = hlen + firstlen;
751 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
752 ip->ip_off |= htons(IP_MF);
753 ip->ip_sum = 0;
754 if (sw_csum & M_CSUM_IPv4) {
755 ip->ip_sum = in_cksum(m, hlen);
756 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
757 } else {
758 /*
759 * checksum is hw-offloaded or not necessary.
760 */
761 KASSERT(!(ifp != NULL && IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4))
762 || (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
763 KASSERT(M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) >=
764 sizeof(struct ip));
765 }
766 sendorfree:
767 /*
768 * If there is no room for all the fragments, don't queue
769 * any of them.
770 */
771 if (ifp != NULL) {
772 s = splnet();
773 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments &&
774 error == 0) {
775 error = ENOBUFS;
776 IP_STATINC(IP_STAT_ODROPPED);
777 IFQ_INC_DROPS(&ifp->if_snd);
778 }
779 splx(s);
780 }
781 if (error) {
782 for (m = m0; m; m = m0) {
783 m0 = m->m_nextpkt;
784 m->m_nextpkt = NULL;
785 m_freem(m);
786 }
787 }
788 return (error);
789 }
790
791 /*
792 * Process a delayed payload checksum calculation.
793 */
794 void
795 in_delayed_cksum(struct mbuf *m)
796 {
797 struct ip *ip;
798 u_int16_t csum, offset;
799
800 ip = mtod(m, struct ip *);
801 offset = ip->ip_hl << 2;
802 csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset);
803 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0)
804 csum = 0xffff;
805
806 offset += M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data);
807
808 if ((offset + sizeof(u_int16_t)) > m->m_len) {
809 /* This happen when ip options were inserted
810 printf("in_delayed_cksum: pullup len %d off %d proto %d\n",
811 m->m_len, offset, ip->ip_p);
812 */
813 m_copyback(m, offset, sizeof(csum), (void *) &csum);
814 } else
815 *(u_int16_t *)(mtod(m, char *) + offset) = csum;
816 }
817
818 /*
819 * Determine the maximum length of the options to be inserted;
820 * we would far rather allocate too much space rather than too little.
821 */
822
823 u_int
824 ip_optlen(struct inpcb *inp)
825 {
826 struct mbuf *m = inp->inp_options;
827
828 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst)) {
829 return (m->m_len - offsetof(struct ipoption, ipopt_dst));
830 }
831 return 0;
832 }
833
834 /*
835 * Insert IP options into preformed packet.
836 * Adjust IP destination as required for IP source routing,
837 * as indicated by a non-zero in_addr at the start of the options.
838 */
839 static struct mbuf *
840 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
841 {
842 struct ipoption *p = mtod(opt, struct ipoption *);
843 struct mbuf *n;
844 struct ip *ip = mtod(m, struct ip *);
845 unsigned optlen;
846
847 optlen = opt->m_len - sizeof(p->ipopt_dst);
848 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
849 return (m); /* XXX should fail */
850 if (!in_nullhost(p->ipopt_dst))
851 ip->ip_dst = p->ipopt_dst;
852 if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) {
853 MGETHDR(n, M_DONTWAIT, MT_HEADER);
854 if (n == 0)
855 return (m);
856 MCLAIM(n, m->m_owner);
857 M_MOVE_PKTHDR(n, m);
858 m->m_len -= sizeof(struct ip);
859 m->m_data += sizeof(struct ip);
860 n->m_next = m;
861 m = n;
862 m->m_len = optlen + sizeof(struct ip);
863 m->m_data += max_linkhdr;
864 bcopy((void *)ip, mtod(m, void *), sizeof(struct ip));
865 } else {
866 m->m_data -= optlen;
867 m->m_len += optlen;
868 memmove(mtod(m, void *), ip, sizeof(struct ip));
869 }
870 m->m_pkthdr.len += optlen;
871 ip = mtod(m, struct ip *);
872 bcopy((void *)p->ipopt_list, (void *)(ip + 1), (unsigned)optlen);
873 *phlen = sizeof(struct ip) + optlen;
874 ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
875 return (m);
876 }
877
878 /*
879 * Copy options from ip to jp,
880 * omitting those not copied during fragmentation.
881 */
882 int
883 ip_optcopy(struct ip *ip, struct ip *jp)
884 {
885 u_char *cp, *dp;
886 int opt, optlen, cnt;
887
888 cp = (u_char *)(ip + 1);
889 dp = (u_char *)(jp + 1);
890 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
891 for (; cnt > 0; cnt -= optlen, cp += optlen) {
892 opt = cp[0];
893 if (opt == IPOPT_EOL)
894 break;
895 if (opt == IPOPT_NOP) {
896 /* Preserve for IP mcast tunnel's LSRR alignment. */
897 *dp++ = IPOPT_NOP;
898 optlen = 1;
899 continue;
900 }
901
902 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp));
903 optlen = cp[IPOPT_OLEN];
904 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen < cnt);
905
906 /* Invalid lengths should have been caught by ip_dooptions. */
907 if (optlen > cnt)
908 optlen = cnt;
909 if (IPOPT_COPIED(opt)) {
910 bcopy((void *)cp, (void *)dp, (unsigned)optlen);
911 dp += optlen;
912 }
913 }
914 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
915 *dp++ = IPOPT_EOL;
916 return (optlen);
917 }
918
919 /*
920 * IP socket option processing.
921 */
922 int
923 ip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
924 {
925 struct inpcb *inp = sotoinpcb(so);
926 struct ip *ip = &inp->inp_ip;
927 int inpflags = inp->inp_flags;
928 int optval = 0, error = 0;
929
930 if (sopt->sopt_level != IPPROTO_IP) {
931 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER)
932 return 0;
933 return ENOPROTOOPT;
934 }
935
936 switch (op) {
937 case PRCO_SETOPT:
938 switch (sopt->sopt_name) {
939 case IP_OPTIONS:
940 #ifdef notyet
941 case IP_RETOPTS:
942 #endif
943 error = ip_pcbopts(inp, sopt);
944 break;
945
946 case IP_TOS:
947 case IP_TTL:
948 case IP_MINTTL:
949 case IP_PKTINFO:
950 case IP_RECVOPTS:
951 case IP_RECVRETOPTS:
952 case IP_RECVDSTADDR:
953 case IP_RECVIF:
954 case IP_RECVPKTINFO:
955 case IP_RECVTTL:
956 error = sockopt_getint(sopt, &optval);
957 if (error)
958 break;
959
960 switch (sopt->sopt_name) {
961 case IP_TOS:
962 ip->ip_tos = optval;
963 break;
964
965 case IP_TTL:
966 ip->ip_ttl = optval;
967 break;
968
969 case IP_MINTTL:
970 if (optval > 0 && optval <= MAXTTL)
971 inp->inp_ip_minttl = optval;
972 else
973 error = EINVAL;
974 break;
975 #define OPTSET(bit) \
976 if (optval) \
977 inpflags |= bit; \
978 else \
979 inpflags &= ~bit;
980
981 case IP_PKTINFO:
982 OPTSET(INP_PKTINFO);
983 break;
984
985 case IP_RECVOPTS:
986 OPTSET(INP_RECVOPTS);
987 break;
988
989 case IP_RECVPKTINFO:
990 OPTSET(INP_RECVPKTINFO);
991 break;
992
993 case IP_RECVRETOPTS:
994 OPTSET(INP_RECVRETOPTS);
995 break;
996
997 case IP_RECVDSTADDR:
998 OPTSET(INP_RECVDSTADDR);
999 break;
1000
1001 case IP_RECVIF:
1002 OPTSET(INP_RECVIF);
1003 break;
1004
1005 case IP_RECVTTL:
1006 OPTSET(INP_RECVTTL);
1007 break;
1008 }
1009 break;
1010 #undef OPTSET
1011
1012 case IP_MULTICAST_IF:
1013 case IP_MULTICAST_TTL:
1014 case IP_MULTICAST_LOOP:
1015 case IP_ADD_MEMBERSHIP:
1016 case IP_DROP_MEMBERSHIP:
1017 error = ip_setmoptions(&inp->inp_moptions, sopt);
1018 break;
1019
1020 case IP_PORTRANGE:
1021 error = sockopt_getint(sopt, &optval);
1022 if (error)
1023 break;
1024
1025 switch (optval) {
1026 case IP_PORTRANGE_DEFAULT:
1027 case IP_PORTRANGE_HIGH:
1028 inpflags &= ~(INP_LOWPORT);
1029 break;
1030
1031 case IP_PORTRANGE_LOW:
1032 inpflags |= INP_LOWPORT;
1033 break;
1034
1035 default:
1036 error = EINVAL;
1037 break;
1038 }
1039 break;
1040
1041 case IP_PORTALGO:
1042 error = sockopt_getint(sopt, &optval);
1043 if (error)
1044 break;
1045
1046 error = portalgo_algo_index_select(
1047 (struct inpcb_hdr *)inp, optval);
1048 break;
1049
1050 #if defined(IPSEC)
1051 case IP_IPSEC_POLICY:
1052 if (ipsec_enabled) {
1053 error = ipsec4_set_policy(inp, sopt->sopt_name,
1054 sopt->sopt_data, sopt->sopt_size,
1055 curlwp->l_cred);
1056 break;
1057 }
1058 /*FALLTHROUGH*/
1059 #endif /* IPSEC */
1060
1061 default:
1062 error = ENOPROTOOPT;
1063 break;
1064 }
1065 break;
1066
1067 case PRCO_GETOPT:
1068 switch (sopt->sopt_name) {
1069 case IP_OPTIONS:
1070 case IP_RETOPTS: {
1071 struct mbuf *mopts = inp->inp_options;
1072
1073 if (mopts) {
1074 struct mbuf *m;
1075
1076 m = m_copym(mopts, 0, M_COPYALL, M_DONTWAIT);
1077 if (m == NULL) {
1078 error = ENOBUFS;
1079 break;
1080 }
1081 error = sockopt_setmbuf(sopt, m);
1082 }
1083 break;
1084 }
1085 case IP_PKTINFO:
1086 case IP_TOS:
1087 case IP_TTL:
1088 case IP_MINTTL:
1089 case IP_RECVOPTS:
1090 case IP_RECVRETOPTS:
1091 case IP_RECVDSTADDR:
1092 case IP_RECVIF:
1093 case IP_RECVPKTINFO:
1094 case IP_RECVTTL:
1095 case IP_ERRORMTU:
1096 switch (sopt->sopt_name) {
1097 case IP_TOS:
1098 optval = ip->ip_tos;
1099 break;
1100
1101 case IP_TTL:
1102 optval = ip->ip_ttl;
1103 break;
1104
1105 case IP_MINTTL:
1106 optval = inp->inp_ip_minttl;
1107 break;
1108
1109 case IP_ERRORMTU:
1110 optval = inp->inp_errormtu;
1111 break;
1112
1113 #define OPTBIT(bit) (inpflags & bit ? 1 : 0)
1114
1115 case IP_PKTINFO:
1116 optval = OPTBIT(INP_PKTINFO);
1117 break;
1118
1119 case IP_RECVOPTS:
1120 optval = OPTBIT(INP_RECVOPTS);
1121 break;
1122
1123 case IP_RECVPKTINFO:
1124 optval = OPTBIT(INP_RECVPKTINFO);
1125 break;
1126
1127 case IP_RECVRETOPTS:
1128 optval = OPTBIT(INP_RECVRETOPTS);
1129 break;
1130
1131 case IP_RECVDSTADDR:
1132 optval = OPTBIT(INP_RECVDSTADDR);
1133 break;
1134
1135 case IP_RECVIF:
1136 optval = OPTBIT(INP_RECVIF);
1137 break;
1138
1139 case IP_RECVTTL:
1140 optval = OPTBIT(INP_RECVTTL);
1141 break;
1142 }
1143 error = sockopt_setint(sopt, optval);
1144 break;
1145
1146 #if 0 /* defined(IPSEC) */
1147 case IP_IPSEC_POLICY:
1148 {
1149 struct mbuf *m = NULL;
1150
1151 /* XXX this will return EINVAL as sopt is empty */
1152 error = ipsec4_get_policy(inp, sopt->sopt_data,
1153 sopt->sopt_size, &m);
1154 if (error == 0)
1155 error = sockopt_setmbuf(sopt, m);
1156 break;
1157 }
1158 #endif /*IPSEC*/
1159
1160 case IP_MULTICAST_IF:
1161 case IP_MULTICAST_TTL:
1162 case IP_MULTICAST_LOOP:
1163 case IP_ADD_MEMBERSHIP:
1164 case IP_DROP_MEMBERSHIP:
1165 error = ip_getmoptions(inp->inp_moptions, sopt);
1166 break;
1167
1168 case IP_PORTRANGE:
1169 if (inpflags & INP_LOWPORT)
1170 optval = IP_PORTRANGE_LOW;
1171 else
1172 optval = IP_PORTRANGE_DEFAULT;
1173 error = sockopt_setint(sopt, optval);
1174 break;
1175
1176 case IP_PORTALGO:
1177 optval = inp->inp_portalgo;
1178 error = sockopt_setint(sopt, optval);
1179 break;
1180
1181 default:
1182 error = ENOPROTOOPT;
1183 break;
1184 }
1185 break;
1186 }
1187
1188 if (!error) {
1189 inp->inp_flags = inpflags;
1190 }
1191 return error;
1192 }
1193
1194 /*
1195 * Set up IP options in pcb for insertion in output packets.
1196 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1197 * with destination address if source routed.
1198 */
1199 static int
1200 ip_pcbopts(struct inpcb *inp, const struct sockopt *sopt)
1201 {
1202 struct mbuf *m;
1203 const u_char *cp;
1204 u_char *dp;
1205 int cnt;
1206
1207 /* Turn off any old options. */
1208 if (inp->inp_options) {
1209 m_free(inp->inp_options);
1210 }
1211 inp->inp_options = NULL;
1212 if ((cnt = sopt->sopt_size) == 0) {
1213 /* Only turning off any previous options. */
1214 return 0;
1215 }
1216 cp = sopt->sopt_data;
1217
1218 #ifndef __vax__
1219 if (cnt % sizeof(int32_t))
1220 return (EINVAL);
1221 #endif
1222
1223 m = m_get(M_DONTWAIT, MT_SOOPTS);
1224 if (m == NULL)
1225 return (ENOBUFS);
1226
1227 dp = mtod(m, u_char *);
1228 memset(dp, 0, sizeof(struct in_addr));
1229 dp += sizeof(struct in_addr);
1230 m->m_len = sizeof(struct in_addr);
1231
1232 /*
1233 * IP option list according to RFC791. Each option is of the form
1234 *
1235 * [optval] [olen] [(olen - 2) data bytes]
1236 *
1237 * We validate the list and copy options to an mbuf for prepending
1238 * to data packets. The IP first-hop destination address will be
1239 * stored before actual options and is zero if unset.
1240 */
1241 while (cnt > 0) {
1242 uint8_t optval, olen, offset;
1243
1244 optval = cp[IPOPT_OPTVAL];
1245
1246 if (optval == IPOPT_EOL || optval == IPOPT_NOP) {
1247 olen = 1;
1248 } else {
1249 if (cnt < IPOPT_OLEN + 1)
1250 goto bad;
1251
1252 olen = cp[IPOPT_OLEN];
1253 if (olen < IPOPT_OLEN + 1 || olen > cnt)
1254 goto bad;
1255 }
1256
1257 if (optval == IPOPT_LSRR || optval == IPOPT_SSRR) {
1258 /*
1259 * user process specifies route as:
1260 * ->A->B->C->D
1261 * D must be our final destination (but we can't
1262 * check that since we may not have connected yet).
1263 * A is first hop destination, which doesn't appear in
1264 * actual IP option, but is stored before the options.
1265 */
1266 if (olen < IPOPT_OFFSET + 1 + sizeof(struct in_addr))
1267 goto bad;
1268
1269 offset = cp[IPOPT_OFFSET];
1270 memcpy(mtod(m, u_char *), cp + IPOPT_OFFSET + 1,
1271 sizeof(struct in_addr));
1272
1273 cp += sizeof(struct in_addr);
1274 cnt -= sizeof(struct in_addr);
1275 olen -= sizeof(struct in_addr);
1276
1277 if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
1278 goto bad;
1279
1280 memcpy(dp, cp, olen);
1281 dp[IPOPT_OPTVAL] = optval;
1282 dp[IPOPT_OLEN] = olen;
1283 dp[IPOPT_OFFSET] = offset;
1284 break;
1285 } else {
1286 if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
1287 goto bad;
1288
1289 memcpy(dp, cp, olen);
1290 break;
1291 }
1292
1293 dp += olen;
1294 m->m_len += olen;
1295
1296 if (optval == IPOPT_EOL)
1297 break;
1298
1299 cp += olen;
1300 cnt -= olen;
1301 }
1302
1303 inp->inp_options = m;
1304 return 0;
1305 bad:
1306 (void)m_free(m);
1307 return EINVAL;
1308 }
1309
1310 /*
1311 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1312 */
1313 static struct ifnet *
1314 ip_multicast_if(struct in_addr *a, int *ifindexp)
1315 {
1316 int ifindex;
1317 struct ifnet *ifp = NULL;
1318 struct in_ifaddr *ia;
1319
1320 if (ifindexp)
1321 *ifindexp = 0;
1322 if (ntohl(a->s_addr) >> 24 == 0) {
1323 ifindex = ntohl(a->s_addr) & 0xffffff;
1324 ifp = if_byindex(ifindex);
1325 if (!ifp)
1326 return NULL;
1327 if (ifindexp)
1328 *ifindexp = ifindex;
1329 } else {
1330 LIST_FOREACH(ia, &IN_IFADDR_HASH(a->s_addr), ia_hash) {
1331 if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
1332 (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
1333 ifp = ia->ia_ifp;
1334 break;
1335 }
1336 }
1337 }
1338 return ifp;
1339 }
1340
1341 static int
1342 ip_getoptval(const struct sockopt *sopt, u_int8_t *val, u_int maxval)
1343 {
1344 u_int tval;
1345 u_char cval;
1346 int error;
1347
1348 if (sopt == NULL)
1349 return EINVAL;
1350
1351 switch (sopt->sopt_size) {
1352 case sizeof(u_char):
1353 error = sockopt_get(sopt, &cval, sizeof(u_char));
1354 tval = cval;
1355 break;
1356
1357 case sizeof(u_int):
1358 error = sockopt_get(sopt, &tval, sizeof(u_int));
1359 break;
1360
1361 default:
1362 error = EINVAL;
1363 }
1364
1365 if (error)
1366 return error;
1367
1368 if (tval > maxval)
1369 return EINVAL;
1370
1371 *val = tval;
1372 return 0;
1373 }
1374
1375 static int
1376 ip_get_membership(const struct sockopt *sopt, struct ifnet **ifp,
1377 struct in_addr *ia, bool add)
1378 {
1379 int error;
1380 struct ip_mreq mreq;
1381
1382 error = sockopt_get(sopt, &mreq, sizeof(mreq));
1383 if (error)
1384 return error;
1385
1386 if (!IN_MULTICAST(mreq.imr_multiaddr.s_addr))
1387 return EINVAL;
1388
1389 memcpy(ia, &mreq.imr_multiaddr, sizeof(*ia));
1390
1391 if (in_nullhost(mreq.imr_interface)) {
1392 union {
1393 struct sockaddr dst;
1394 struct sockaddr_in dst4;
1395 } u;
1396 struct route ro;
1397
1398 if (!add) {
1399 *ifp = NULL;
1400 return 0;
1401 }
1402 /*
1403 * If no interface address was provided, use the interface of
1404 * the route to the given multicast address.
1405 */
1406 struct rtentry *rt;
1407 memset(&ro, 0, sizeof(ro));
1408
1409 sockaddr_in_init(&u.dst4, ia, 0);
1410 rtcache_setdst(&ro, &u.dst);
1411 *ifp = (rt = rtcache_init(&ro)) != NULL ? rt->rt_ifp : NULL;
1412 rtcache_free(&ro);
1413 } else {
1414 *ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1415 if (!add && *ifp == NULL)
1416 return EADDRNOTAVAIL;
1417 }
1418 return 0;
1419 }
1420
1421 /*
1422 * Add a multicast group membership.
1423 * Group must be a valid IP multicast address.
1424 */
1425 static int
1426 ip_add_membership(struct ip_moptions *imo, const struct sockopt *sopt)
1427 {
1428 struct ifnet *ifp;
1429 struct in_addr ia;
1430 int i, error;
1431
1432 if (sopt->sopt_size == sizeof(struct ip_mreq))
1433 error = ip_get_membership(sopt, &ifp, &ia, true);
1434 else
1435 #ifdef INET6
1436 error = ip6_get_membership(sopt, &ifp, &ia, sizeof(ia));
1437 #else
1438 return EINVAL;
1439 #endif
1440
1441 if (error)
1442 return error;
1443
1444 /*
1445 * See if we found an interface, and confirm that it
1446 * supports multicast.
1447 */
1448 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1449 return EADDRNOTAVAIL;
1450
1451 /*
1452 * See if the membership already exists or if all the
1453 * membership slots are full.
1454 */
1455 for (i = 0; i < imo->imo_num_memberships; ++i) {
1456 if (imo->imo_membership[i]->inm_ifp == ifp &&
1457 in_hosteq(imo->imo_membership[i]->inm_addr, ia))
1458 break;
1459 }
1460 if (i < imo->imo_num_memberships)
1461 return EADDRINUSE;
1462
1463 if (i == IP_MAX_MEMBERSHIPS)
1464 return ETOOMANYREFS;
1465
1466 /*
1467 * Everything looks good; add a new record to the multicast
1468 * address list for the given interface.
1469 */
1470 if ((imo->imo_membership[i] = in_addmulti(&ia, ifp)) == NULL)
1471 return ENOBUFS;
1472
1473 ++imo->imo_num_memberships;
1474 return 0;
1475 }
1476
1477 /*
1478 * Drop a multicast group membership.
1479 * Group must be a valid IP multicast address.
1480 */
1481 static int
1482 ip_drop_membership(struct ip_moptions *imo, const struct sockopt *sopt)
1483 {
1484 struct in_addr ia;
1485 struct ifnet *ifp;
1486 int i, error;
1487
1488 if (sopt->sopt_size == sizeof(struct ip_mreq))
1489 error = ip_get_membership(sopt, &ifp, &ia, false);
1490 else
1491 #ifdef INET6
1492 error = ip6_get_membership(sopt, &ifp, &ia, sizeof(ia));
1493 #else
1494 return EINVAL;
1495 #endif
1496
1497 if (error)
1498 return error;
1499
1500 /*
1501 * Find the membership in the membership array.
1502 */
1503 for (i = 0; i < imo->imo_num_memberships; ++i) {
1504 if ((ifp == NULL ||
1505 imo->imo_membership[i]->inm_ifp == ifp) &&
1506 in_hosteq(imo->imo_membership[i]->inm_addr, ia))
1507 break;
1508 }
1509 if (i == imo->imo_num_memberships)
1510 return EADDRNOTAVAIL;
1511
1512 /*
1513 * Give up the multicast address record to which the
1514 * membership points.
1515 */
1516 in_delmulti(imo->imo_membership[i]);
1517
1518 /*
1519 * Remove the gap in the membership array.
1520 */
1521 for (++i; i < imo->imo_num_memberships; ++i)
1522 imo->imo_membership[i-1] = imo->imo_membership[i];
1523 --imo->imo_num_memberships;
1524 return 0;
1525 }
1526
1527 /*
1528 * Set the IP multicast options in response to user setsockopt().
1529 */
1530 int
1531 ip_setmoptions(struct ip_moptions **pimo, const struct sockopt *sopt)
1532 {
1533 struct ip_moptions *imo = *pimo;
1534 struct in_addr addr;
1535 struct ifnet *ifp;
1536 int ifindex, error = 0;
1537
1538 if (!imo) {
1539 /*
1540 * No multicast option buffer attached to the pcb;
1541 * allocate one and initialize to default values.
1542 */
1543 imo = kmem_intr_alloc(sizeof(*imo), KM_NOSLEEP);
1544 if (imo == NULL)
1545 return ENOBUFS;
1546
1547 imo->imo_multicast_ifp = NULL;
1548 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1549 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1550 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1551 imo->imo_num_memberships = 0;
1552 *pimo = imo;
1553 }
1554
1555 switch (sopt->sopt_name) {
1556 case IP_MULTICAST_IF:
1557 /*
1558 * Select the interface for outgoing multicast packets.
1559 */
1560 error = sockopt_get(sopt, &addr, sizeof(addr));
1561 if (error)
1562 break;
1563
1564 /*
1565 * INADDR_ANY is used to remove a previous selection.
1566 * When no interface is selected, a default one is
1567 * chosen every time a multicast packet is sent.
1568 */
1569 if (in_nullhost(addr)) {
1570 imo->imo_multicast_ifp = NULL;
1571 break;
1572 }
1573 /*
1574 * The selected interface is identified by its local
1575 * IP address. Find the interface and confirm that
1576 * it supports multicasting.
1577 */
1578 ifp = ip_multicast_if(&addr, &ifindex);
1579 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1580 error = EADDRNOTAVAIL;
1581 break;
1582 }
1583 imo->imo_multicast_ifp = ifp;
1584 if (ifindex)
1585 imo->imo_multicast_addr = addr;
1586 else
1587 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1588 break;
1589
1590 case IP_MULTICAST_TTL:
1591 /*
1592 * Set the IP time-to-live for outgoing multicast packets.
1593 */
1594 error = ip_getoptval(sopt, &imo->imo_multicast_ttl, MAXTTL);
1595 break;
1596
1597 case IP_MULTICAST_LOOP:
1598 /*
1599 * Set the loopback flag for outgoing multicast packets.
1600 * Must be zero or one.
1601 */
1602 error = ip_getoptval(sopt, &imo->imo_multicast_loop, 1);
1603 break;
1604
1605 case IP_ADD_MEMBERSHIP: /* IPV6_JOIN_GROUP */
1606 error = ip_add_membership(imo, sopt);
1607 break;
1608
1609 case IP_DROP_MEMBERSHIP: /* IPV6_LEAVE_GROUP */
1610 error = ip_drop_membership(imo, sopt);
1611 break;
1612
1613 default:
1614 error = EOPNOTSUPP;
1615 break;
1616 }
1617
1618 /*
1619 * If all options have default values, no need to keep the mbuf.
1620 */
1621 if (imo->imo_multicast_ifp == NULL &&
1622 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1623 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1624 imo->imo_num_memberships == 0) {
1625 kmem_free(imo, sizeof(*imo));
1626 *pimo = NULL;
1627 }
1628
1629 return error;
1630 }
1631
1632 /*
1633 * Return the IP multicast options in response to user getsockopt().
1634 */
1635 int
1636 ip_getmoptions(struct ip_moptions *imo, struct sockopt *sopt)
1637 {
1638 struct in_addr addr;
1639 struct in_ifaddr *ia;
1640 uint8_t optval;
1641 int error = 0;
1642
1643 switch (sopt->sopt_name) {
1644 case IP_MULTICAST_IF:
1645 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1646 addr = zeroin_addr;
1647 else if (imo->imo_multicast_addr.s_addr) {
1648 /* return the value user has set */
1649 addr = imo->imo_multicast_addr;
1650 } else {
1651 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1652 addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
1653 }
1654 error = sockopt_set(sopt, &addr, sizeof(addr));
1655 break;
1656
1657 case IP_MULTICAST_TTL:
1658 optval = imo ? imo->imo_multicast_ttl
1659 : IP_DEFAULT_MULTICAST_TTL;
1660
1661 error = sockopt_set(sopt, &optval, sizeof(optval));
1662 break;
1663
1664 case IP_MULTICAST_LOOP:
1665 optval = imo ? imo->imo_multicast_loop
1666 : IP_DEFAULT_MULTICAST_LOOP;
1667
1668 error = sockopt_set(sopt, &optval, sizeof(optval));
1669 break;
1670
1671 default:
1672 error = EOPNOTSUPP;
1673 }
1674
1675 return error;
1676 }
1677
1678 /*
1679 * Discard the IP multicast options.
1680 */
1681 void
1682 ip_freemoptions(struct ip_moptions *imo)
1683 {
1684 int i;
1685
1686 if (imo != NULL) {
1687 for (i = 0; i < imo->imo_num_memberships; ++i)
1688 in_delmulti(imo->imo_membership[i]);
1689 kmem_free(imo, sizeof(*imo));
1690 }
1691 }
1692
1693 /*
1694 * Routine called from ip_output() to loop back a copy of an IP multicast
1695 * packet to the input queue of a specified interface. Note that this
1696 * calls the output routine of the loopback "driver", but with an interface
1697 * pointer that might NOT be lo0ifp -- easier than replicating that code here.
1698 */
1699 static void
1700 ip_mloopback(struct ifnet *ifp, struct mbuf *m, const struct sockaddr_in *dst)
1701 {
1702 struct ip *ip;
1703 struct mbuf *copym;
1704
1705 copym = m_copypacket(m, M_DONTWAIT);
1706 if (copym != NULL
1707 && (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
1708 copym = m_pullup(copym, sizeof(struct ip));
1709 if (copym == NULL)
1710 return;
1711 /*
1712 * We don't bother to fragment if the IP length is greater
1713 * than the interface's MTU. Can this possibly matter?
1714 */
1715 ip = mtod(copym, struct ip *);
1716
1717 if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1718 in_delayed_cksum(copym);
1719 copym->m_pkthdr.csum_flags &=
1720 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
1721 }
1722
1723 ip->ip_sum = 0;
1724 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1725 KERNEL_LOCK(1, NULL);
1726 (void)looutput(ifp, copym, sintocsa(dst), NULL);
1727 KERNEL_UNLOCK_ONE(NULL);
1728 }
1729