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