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