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