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