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