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