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