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