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