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