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