ip_output.c revision 1.210.6.3 1 /* $NetBSD: ip_output.c,v 1.210.6.3 2012/06/02 11:09:38 mrg 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.210.6.3 2012/06/02 11:09:38 mrg 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 FAST_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
129 #ifdef MROUTING
130 #include <netinet/ip_mroute.h>
131 #endif
132
133 #ifdef FAST_IPSEC
134 #include <netipsec/ipsec.h>
135 #include <netipsec/key.h>
136 #include <netipsec/xform.h>
137 #endif /* FAST_IPSEC*/
138
139 #ifdef IPSEC_NAT_T
140 #include <netinet/udp.h>
141 #endif
142
143 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
144 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
145 static void ip_mloopback(struct ifnet *, struct mbuf *,
146 const struct sockaddr_in *);
147
148 #ifdef PFIL_HOOKS
149 extern struct pfil_head inet_pfil_hook; /* XXX */
150 #endif
151
152 int ip_do_loopback_cksum = 0;
153
154 /*
155 * IP output. The packet in mbuf chain m contains a skeletal IP
156 * header (with len, off, ttl, proto, tos, src, dst).
157 * The mbuf chain containing the packet will be freed.
158 * The mbuf opt, if present, will not be freed.
159 */
160 int
161 ip_output(struct mbuf *m0, ...)
162 {
163 struct rtentry *rt;
164 struct ip *ip;
165 struct ifnet *ifp;
166 struct mbuf *m = m0;
167 int hlen = sizeof (struct ip);
168 int len, error = 0;
169 struct route iproute;
170 const struct sockaddr_in *dst;
171 struct in_ifaddr *ia;
172 struct ifaddr *xifa;
173 struct mbuf *opt;
174 struct route *ro;
175 int flags, sw_csum;
176 int *mtu_p;
177 u_long mtu;
178 struct ip_moptions *imo;
179 struct socket *so;
180 va_list ap;
181 #ifdef IPSEC_NAT_T
182 int natt_frag = 0;
183 #endif
184 #ifdef FAST_IPSEC
185 struct inpcb *inp;
186 struct secpolicy *sp = NULL;
187 int s;
188 #endif
189 u_int16_t ip_len;
190 union {
191 struct sockaddr dst;
192 struct sockaddr_in dst4;
193 } u;
194 struct sockaddr *rdst = &u.dst; /* real IP destination, as opposed
195 * to the nexthop
196 */
197
198 len = 0;
199 va_start(ap, m0);
200 opt = va_arg(ap, struct mbuf *);
201 ro = va_arg(ap, struct route *);
202 flags = va_arg(ap, int);
203 imo = va_arg(ap, struct ip_moptions *);
204 so = va_arg(ap, struct socket *);
205 if (flags & IP_RETURNMTU)
206 mtu_p = va_arg(ap, int *);
207 else
208 mtu_p = NULL;
209 va_end(ap);
210
211 MCLAIM(m, &ip_tx_mowner);
212 #ifdef FAST_IPSEC
213 if (so != NULL && so->so_proto->pr_domain->dom_family == AF_INET)
214 inp = (struct inpcb *)so->so_pcb;
215 else
216 inp = NULL;
217 #endif /* FAST_IPSEC */
218
219 #ifdef DIAGNOSTIC
220 if ((m->m_flags & M_PKTHDR) == 0)
221 panic("ip_output: no HDR");
222
223 if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) != 0) {
224 panic("ip_output: IPv6 checksum offload flags: %d",
225 m->m_pkthdr.csum_flags);
226 }
227
228 if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) ==
229 (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
230 panic("ip_output: conflicting checksum offload flags: %d",
231 m->m_pkthdr.csum_flags);
232 }
233 #endif
234 if (opt) {
235 m = ip_insertoptions(m, opt, &len);
236 if (len >= sizeof(struct ip))
237 hlen = len;
238 }
239 ip = mtod(m, struct ip *);
240 /*
241 * Fill in IP header.
242 */
243 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
244 ip->ip_v = IPVERSION;
245 ip->ip_off = htons(0);
246 /* ip->ip_id filled in after we find out source ia */
247 ip->ip_hl = hlen >> 2;
248 IP_STATINC(IP_STAT_LOCALOUT);
249 } else {
250 hlen = ip->ip_hl << 2;
251 }
252 /*
253 * Route packet.
254 */
255 memset(&iproute, 0, sizeof(iproute));
256 if (ro == NULL)
257 ro = &iproute;
258 sockaddr_in_init(&u.dst4, &ip->ip_dst, 0);
259 dst = satocsin(rtcache_getdst(ro));
260 /*
261 * If there is a cached route,
262 * check that it is to the same destination
263 * and is still up. If not, free it and try again.
264 * The address family should also be checked in case of sharing the
265 * cache with IPv6.
266 */
267 if (dst == NULL)
268 ;
269 else if (dst->sin_family != AF_INET ||
270 !in_hosteq(dst->sin_addr, ip->ip_dst))
271 rtcache_free(ro);
272
273 if ((rt = rtcache_validate(ro)) == NULL &&
274 (rt = rtcache_update(ro, 1)) == NULL) {
275 dst = &u.dst4;
276 rtcache_setdst(ro, &u.dst);
277 }
278 /*
279 * If routing to interface only,
280 * short circuit routing lookup.
281 */
282 if (flags & IP_ROUTETOIF) {
283 if ((ia = ifatoia(ifa_ifwithladdr(sintocsa(dst)))) == NULL) {
284 IP_STATINC(IP_STAT_NOROUTE);
285 error = ENETUNREACH;
286 goto bad;
287 }
288 ifp = ia->ia_ifp;
289 mtu = ifp->if_mtu;
290 ip->ip_ttl = 1;
291 } else if ((IN_MULTICAST(ip->ip_dst.s_addr) ||
292 ip->ip_dst.s_addr == INADDR_BROADCAST) &&
293 imo != NULL && imo->imo_multicast_ifp != NULL) {
294 ifp = imo->imo_multicast_ifp;
295 mtu = ifp->if_mtu;
296 IFP_TO_IA(ifp, ia);
297 } else {
298 if (rt == NULL)
299 rt = rtcache_init(ro);
300 if (rt == NULL) {
301 IP_STATINC(IP_STAT_NOROUTE);
302 error = EHOSTUNREACH;
303 goto bad;
304 }
305 ia = ifatoia(rt->rt_ifa);
306 ifp = rt->rt_ifp;
307 if ((mtu = rt->rt_rmx.rmx_mtu) == 0)
308 mtu = ifp->if_mtu;
309 rt->rt_use++;
310 if (rt->rt_flags & RTF_GATEWAY)
311 dst = satosin(rt->rt_gateway);
312 }
313 if (IN_MULTICAST(ip->ip_dst.s_addr) ||
314 (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
315 struct in_multi *inm;
316
317 m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
318 M_BCAST : M_MCAST;
319 /*
320 * See if the caller provided any multicast options
321 */
322 if (imo != NULL)
323 ip->ip_ttl = imo->imo_multicast_ttl;
324 else
325 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
326
327 /*
328 * if we don't know the outgoing ifp yet, we can't generate
329 * output
330 */
331 if (!ifp) {
332 IP_STATINC(IP_STAT_NOROUTE);
333 error = ENETUNREACH;
334 goto bad;
335 }
336
337 /*
338 * If the packet is multicast or broadcast, confirm that
339 * the outgoing interface can transmit it.
340 */
341 if (((m->m_flags & M_MCAST) &&
342 (ifp->if_flags & IFF_MULTICAST) == 0) ||
343 ((m->m_flags & M_BCAST) &&
344 (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)) {
345 IP_STATINC(IP_STAT_NOROUTE);
346 error = ENETUNREACH;
347 goto bad;
348 }
349 /*
350 * If source address not specified yet, use an address
351 * of outgoing interface.
352 */
353 if (in_nullhost(ip->ip_src)) {
354 struct in_ifaddr *xia;
355
356 IFP_TO_IA(ifp, xia);
357 if (!xia) {
358 error = EADDRNOTAVAIL;
359 goto bad;
360 }
361 xifa = &xia->ia_ifa;
362 if (xifa->ifa_getifa != NULL) {
363 xia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
364 }
365 ip->ip_src = xia->ia_addr.sin_addr;
366 }
367
368 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
369 if (inm != NULL &&
370 (imo == NULL || imo->imo_multicast_loop)) {
371 /*
372 * If we belong to the destination multicast group
373 * on the outgoing interface, and the caller did not
374 * forbid loopback, loop back a copy.
375 */
376 ip_mloopback(ifp, m, &u.dst4);
377 }
378 #ifdef MROUTING
379 else {
380 /*
381 * If we are acting as a multicast router, perform
382 * multicast forwarding as if the packet had just
383 * arrived on the interface to which we are about
384 * to send. The multicast forwarding function
385 * recursively calls this function, using the
386 * IP_FORWARDING flag to prevent infinite recursion.
387 *
388 * Multicasts that are looped back by ip_mloopback(),
389 * above, will be forwarded by the ip_input() routine,
390 * if necessary.
391 */
392 extern struct socket *ip_mrouter;
393
394 if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
395 if (ip_mforward(m, ifp) != 0) {
396 m_freem(m);
397 goto done;
398 }
399 }
400 }
401 #endif
402 /*
403 * Multicasts with a time-to-live of zero may be looped-
404 * back, above, but must not be transmitted on a network.
405 * Also, multicasts addressed to the loopback interface
406 * are not sent -- the above call to ip_mloopback() will
407 * loop back a copy if this host actually belongs to the
408 * destination group on the loopback interface.
409 */
410 if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
411 m_freem(m);
412 goto done;
413 }
414
415 goto sendit;
416 }
417 /*
418 * If source address not specified yet, use address
419 * of outgoing interface.
420 */
421 if (in_nullhost(ip->ip_src)) {
422 xifa = &ia->ia_ifa;
423 if (xifa->ifa_getifa != NULL)
424 ia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
425 ip->ip_src = ia->ia_addr.sin_addr;
426 }
427
428 /*
429 * packets with Class-D address as source are not valid per
430 * RFC 1112
431 */
432 if (IN_MULTICAST(ip->ip_src.s_addr)) {
433 IP_STATINC(IP_STAT_ODROPPED);
434 error = EADDRNOTAVAIL;
435 goto bad;
436 }
437
438 /*
439 * Look for broadcast address and
440 * and verify user is allowed to send
441 * such a packet.
442 */
443 if (in_broadcast(dst->sin_addr, ifp)) {
444 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
445 error = EADDRNOTAVAIL;
446 goto bad;
447 }
448 if ((flags & IP_ALLOWBROADCAST) == 0) {
449 error = EACCES;
450 goto bad;
451 }
452 /* don't allow broadcast messages to be fragmented */
453 if (ntohs(ip->ip_len) > ifp->if_mtu) {
454 error = EMSGSIZE;
455 goto bad;
456 }
457 m->m_flags |= M_BCAST;
458 } else
459 m->m_flags &= ~M_BCAST;
460
461 sendit:
462 if ((flags & (IP_FORWARDING|IP_NOIPNEWID)) == 0) {
463 if (m->m_pkthdr.len < IP_MINFRAGSIZE) {
464 ip->ip_id = 0;
465 } else if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
466 ip->ip_id = ip_newid(ia);
467 } else {
468
469 /*
470 * TSO capable interfaces (typically?) increment
471 * ip_id for each segment.
472 * "allocate" enough ids here to increase the chance
473 * for them to be unique.
474 *
475 * note that the following calculation is not
476 * needed to be precise. wasting some ip_id is fine.
477 */
478
479 unsigned int segsz = m->m_pkthdr.segsz;
480 unsigned int datasz = ntohs(ip->ip_len) - hlen;
481 unsigned int num = howmany(datasz, segsz);
482
483 ip->ip_id = ip_newid_range(ia, num);
484 }
485 }
486 /*
487 * If we're doing Path MTU Discovery, we need to set DF unless
488 * the route's MTU is locked.
489 */
490 if ((flags & IP_MTUDISC) != 0 && rt != NULL &&
491 (rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
492 ip->ip_off |= htons(IP_DF);
493
494 /* Remember the current ip_len */
495 ip_len = ntohs(ip->ip_len);
496
497 #ifdef FAST_IPSEC
498 /*
499 * Check the security policy (SP) for the packet and, if
500 * required, do IPsec-related processing. There are two
501 * cases here; the first time a packet is sent through
502 * it will be untagged and handled by ipsec4_checkpolicy.
503 * If the packet is resubmitted to ip_output (e.g. after
504 * AH, ESP, etc. processing), there will be a tag to bypass
505 * the lookup and related policy checking.
506 */
507 if (!ipsec_outdone(m)) {
508 s = splsoftnet();
509 if (inp != NULL &&
510 IPSEC_PCB_SKIP_IPSEC(inp->inp_sp, IPSEC_DIR_OUTBOUND)) {
511 splx(s);
512 goto spd_done;
513 }
514 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
515 &error, inp);
516 /*
517 * There are four return cases:
518 * sp != NULL apply IPsec policy
519 * sp == NULL, error == 0 no IPsec handling needed
520 * sp == NULL, error == -EINVAL discard packet w/o error
521 * sp == NULL, error != 0 discard packet, report error
522 */
523 if (sp != NULL) {
524 #ifdef IPSEC_NAT_T
525 /*
526 * NAT-T ESP fragmentation: don't do IPSec processing now,
527 * we'll do it on each fragmented packet.
528 */
529 if (sp->req->sav &&
530 ((sp->req->sav->natt_type & UDP_ENCAP_ESPINUDP) ||
531 (sp->req->sav->natt_type & UDP_ENCAP_ESPINUDP_NON_IKE))) {
532 if (ntohs(ip->ip_len) > sp->req->sav->esp_frag) {
533 natt_frag = 1;
534 mtu = sp->req->sav->esp_frag;
535 splx(s);
536 goto spd_done;
537 }
538 }
539 #endif /* IPSEC_NAT_T */
540
541 /*
542 * Do delayed checksums now because we send before
543 * this is done in the normal processing path.
544 */
545 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
546 in_delayed_cksum(m);
547 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
548 }
549
550 #ifdef __FreeBSD__
551 ip->ip_len = htons(ip->ip_len);
552 ip->ip_off = htons(ip->ip_off);
553 #endif
554
555 /* NB: callee frees mbuf */
556 error = ipsec4_process_packet(m, sp->req, flags, 0);
557 /*
558 * Preserve KAME behaviour: ENOENT can be returned
559 * when an SA acquire is in progress. Don't propagate
560 * this to user-level; it confuses applications.
561 *
562 * XXX this will go away when the SADB is redone.
563 */
564 if (error == ENOENT)
565 error = 0;
566 splx(s);
567 goto done;
568 } else {
569 splx(s);
570
571 if (error != 0) {
572 /*
573 * Hack: -EINVAL is used to signal that a packet
574 * should be silently discarded. This is typically
575 * because we asked key management for an SA and
576 * it was delayed (e.g. kicked up to IKE).
577 */
578 if (error == -EINVAL)
579 error = 0;
580 goto bad;
581 } else {
582 /* No IPsec processing for this packet. */
583 }
584 }
585 }
586 spd_done:
587 #endif /* FAST_IPSEC */
588
589 #ifdef PFIL_HOOKS
590 /*
591 * Run through list of hooks for output packets.
592 */
593 if ((error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT)) != 0)
594 goto done;
595 if (m == NULL)
596 goto done;
597
598 ip = mtod(m, struct ip *);
599 hlen = ip->ip_hl << 2;
600 ip_len = ntohs(ip->ip_len);
601 #endif /* PFIL_HOOKS */
602
603 m->m_pkthdr.csum_data |= hlen << 16;
604
605 #if IFA_STATS
606 /*
607 * search for the source address structure to
608 * maintain output statistics.
609 */
610 INADDR_TO_IA(ip->ip_src, ia);
611 #endif
612
613 /* Maybe skip checksums on loopback interfaces. */
614 if (IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) {
615 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
616 }
617 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
618 /*
619 * If small enough for mtu of path, or if using TCP segmentation
620 * offload, can just send directly.
621 */
622 if (ip_len <= mtu ||
623 (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0) {
624 #if IFA_STATS
625 if (ia)
626 ia->ia_ifa.ifa_data.ifad_outbytes += ip_len;
627 #endif
628 /*
629 * Always initialize the sum to 0! Some HW assisted
630 * checksumming requires this.
631 */
632 ip->ip_sum = 0;
633
634 if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
635 /*
636 * Perform any checksums that the hardware can't do
637 * for us.
638 *
639 * XXX Does any hardware require the {th,uh}_sum
640 * XXX fields to be 0?
641 */
642 if (sw_csum & M_CSUM_IPv4) {
643 KASSERT(IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4));
644 ip->ip_sum = in_cksum(m, hlen);
645 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
646 }
647 if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
648 if (IN_NEED_CHECKSUM(ifp,
649 sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
650 in_delayed_cksum(m);
651 }
652 m->m_pkthdr.csum_flags &=
653 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
654 }
655 }
656
657 if (__predict_true(
658 (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0 ||
659 (ifp->if_capenable & IFCAP_TSOv4) != 0)) {
660 KERNEL_LOCK(1, NULL);
661 error =
662 (*ifp->if_output)(ifp, m,
663 (m->m_flags & M_MCAST) ?
664 sintocsa(rdst) : sintocsa(dst),
665 rt);
666 KERNEL_UNLOCK_ONE(NULL);
667 } else {
668 error =
669 ip_tso_output(ifp, m,
670 (m->m_flags & M_MCAST) ?
671 sintocsa(rdst) : sintocsa(dst),
672 rt);
673 }
674 goto done;
675 }
676
677 /*
678 * We can't use HW checksumming if we're about to
679 * to fragment the packet.
680 *
681 * XXX Some hardware can do this.
682 */
683 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
684 if (IN_NEED_CHECKSUM(ifp,
685 m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
686 in_delayed_cksum(m);
687 }
688 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
689 }
690
691 /*
692 * Too large for interface; fragment if possible.
693 * Must be able to put at least 8 bytes per fragment.
694 */
695 if (ntohs(ip->ip_off) & IP_DF) {
696 if (flags & IP_RETURNMTU)
697 *mtu_p = mtu;
698 error = EMSGSIZE;
699 IP_STATINC(IP_STAT_CANTFRAG);
700 goto bad;
701 }
702
703 error = ip_fragment(m, ifp, mtu);
704 if (error) {
705 m = NULL;
706 goto bad;
707 }
708
709 for (; m; m = m0) {
710 m0 = m->m_nextpkt;
711 m->m_nextpkt = 0;
712 if (error == 0) {
713 #if IFA_STATS
714 if (ia)
715 ia->ia_ifa.ifa_data.ifad_outbytes +=
716 ntohs(ip->ip_len);
717 #endif
718 #ifdef IPSEC_NAT_T
719 /*
720 * If we get there, the packet has not been handeld by
721 * IPSec whereas it should have. Now that it has been
722 * fragmented, re-inject it in ip_output so that IPsec
723 * processing can occur.
724 */
725 if (natt_frag) {
726 error = ip_output(m, opt,
727 ro, flags | IP_RAWOUTPUT | IP_NOIPNEWID, imo, so, mtu_p);
728 } else
729 #endif /* IPSEC_NAT_T */
730 {
731 KASSERT((m->m_pkthdr.csum_flags &
732 (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
733 KERNEL_LOCK(1, NULL);
734 error = (*ifp->if_output)(ifp, m,
735 (m->m_flags & M_MCAST) ?
736 sintocsa(rdst) : sintocsa(dst),
737 rt);
738 KERNEL_UNLOCK_ONE(NULL);
739 }
740 } else
741 m_freem(m);
742 }
743
744 if (error == 0)
745 IP_STATINC(IP_STAT_FRAGMENTED);
746 done:
747 rtcache_free(&iproute);
748
749 #ifdef FAST_IPSEC
750 if (sp != NULL)
751 KEY_FREESP(&sp);
752 #endif /* FAST_IPSEC */
753
754 return (error);
755 bad:
756 m_freem(m);
757 goto done;
758 }
759
760 int
761 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
762 {
763 struct ip *ip, *mhip;
764 struct mbuf *m0;
765 int len, hlen, off;
766 int mhlen, firstlen;
767 struct mbuf **mnext;
768 int sw_csum = m->m_pkthdr.csum_flags;
769 int fragments = 0;
770 int s;
771 int error = 0;
772
773 ip = mtod(m, struct ip *);
774 hlen = ip->ip_hl << 2;
775 if (ifp != NULL)
776 sw_csum &= ~ifp->if_csum_flags_tx;
777
778 len = (mtu - hlen) &~ 7;
779 if (len < 8) {
780 m_freem(m);
781 return (EMSGSIZE);
782 }
783
784 firstlen = len;
785 mnext = &m->m_nextpkt;
786
787 /*
788 * Loop through length of segment after first fragment,
789 * make new header and copy data of each part and link onto chain.
790 */
791 m0 = m;
792 mhlen = sizeof (struct ip);
793 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
794 MGETHDR(m, M_DONTWAIT, MT_HEADER);
795 if (m == 0) {
796 error = ENOBUFS;
797 IP_STATINC(IP_STAT_ODROPPED);
798 goto sendorfree;
799 }
800 MCLAIM(m, m0->m_owner);
801 *mnext = m;
802 mnext = &m->m_nextpkt;
803 m->m_data += max_linkhdr;
804 mhip = mtod(m, struct ip *);
805 *mhip = *ip;
806 /* we must inherit MCAST and BCAST flags */
807 m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
808 if (hlen > sizeof (struct ip)) {
809 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
810 mhip->ip_hl = mhlen >> 2;
811 }
812 m->m_len = mhlen;
813 mhip->ip_off = ((off - hlen) >> 3) +
814 (ntohs(ip->ip_off) & ~IP_MF);
815 if (ip->ip_off & htons(IP_MF))
816 mhip->ip_off |= IP_MF;
817 if (off + len >= ntohs(ip->ip_len))
818 len = ntohs(ip->ip_len) - off;
819 else
820 mhip->ip_off |= IP_MF;
821 HTONS(mhip->ip_off);
822 mhip->ip_len = htons((u_int16_t)(len + mhlen));
823 m->m_next = m_copym(m0, off, len, M_DONTWAIT);
824 if (m->m_next == 0) {
825 error = ENOBUFS; /* ??? */
826 IP_STATINC(IP_STAT_ODROPPED);
827 goto sendorfree;
828 }
829 m->m_pkthdr.len = mhlen + len;
830 m->m_pkthdr.rcvif = NULL;
831 mhip->ip_sum = 0;
832 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
833 if (sw_csum & M_CSUM_IPv4) {
834 mhip->ip_sum = in_cksum(m, mhlen);
835 } else {
836 /*
837 * checksum is hw-offloaded or not necessary.
838 */
839 m->m_pkthdr.csum_flags |=
840 m0->m_pkthdr.csum_flags & M_CSUM_IPv4;
841 m->m_pkthdr.csum_data |= mhlen << 16;
842 KASSERT(!(ifp != NULL &&
843 IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4))
844 || (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
845 }
846 IP_STATINC(IP_STAT_OFRAGMENTS);
847 fragments++;
848 }
849 /*
850 * Update first fragment by trimming what's been copied out
851 * and updating header, then send each fragment (in order).
852 */
853 m = m0;
854 m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
855 m->m_pkthdr.len = hlen + firstlen;
856 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
857 ip->ip_off |= htons(IP_MF);
858 ip->ip_sum = 0;
859 if (sw_csum & M_CSUM_IPv4) {
860 ip->ip_sum = in_cksum(m, hlen);
861 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
862 } else {
863 /*
864 * checksum is hw-offloaded or not necessary.
865 */
866 KASSERT(!(ifp != NULL && IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4))
867 || (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
868 KASSERT(M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) >=
869 sizeof(struct ip));
870 }
871 sendorfree:
872 /*
873 * If there is no room for all the fragments, don't queue
874 * any of them.
875 */
876 if (ifp != NULL) {
877 s = splnet();
878 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments &&
879 error == 0) {
880 error = ENOBUFS;
881 IP_STATINC(IP_STAT_ODROPPED);
882 IFQ_INC_DROPS(&ifp->if_snd);
883 }
884 splx(s);
885 }
886 if (error) {
887 for (m = m0; m; m = m0) {
888 m0 = m->m_nextpkt;
889 m->m_nextpkt = NULL;
890 m_freem(m);
891 }
892 }
893 return (error);
894 }
895
896 /*
897 * Process a delayed payload checksum calculation.
898 */
899 void
900 in_delayed_cksum(struct mbuf *m)
901 {
902 struct ip *ip;
903 u_int16_t csum, offset;
904
905 ip = mtod(m, struct ip *);
906 offset = ip->ip_hl << 2;
907 csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset);
908 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0)
909 csum = 0xffff;
910
911 offset += M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data);
912
913 if ((offset + sizeof(u_int16_t)) > m->m_len) {
914 /* This happen when ip options were inserted
915 printf("in_delayed_cksum: pullup len %d off %d proto %d\n",
916 m->m_len, offset, ip->ip_p);
917 */
918 m_copyback(m, offset, sizeof(csum), (void *) &csum);
919 } else
920 *(u_int16_t *)(mtod(m, char *) + offset) = csum;
921 }
922
923 /*
924 * Determine the maximum length of the options to be inserted;
925 * we would far rather allocate too much space rather than too little.
926 */
927
928 u_int
929 ip_optlen(struct inpcb *inp)
930 {
931 struct mbuf *m = inp->inp_options;
932
933 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
934 return (m->m_len - offsetof(struct ipoption, ipopt_dst));
935 else
936 return 0;
937 }
938
939
940 /*
941 * Insert IP options into preformed packet.
942 * Adjust IP destination as required for IP source routing,
943 * as indicated by a non-zero in_addr at the start of the options.
944 */
945 static struct mbuf *
946 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
947 {
948 struct ipoption *p = mtod(opt, struct ipoption *);
949 struct mbuf *n;
950 struct ip *ip = mtod(m, struct ip *);
951 unsigned optlen;
952
953 optlen = opt->m_len - sizeof(p->ipopt_dst);
954 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
955 return (m); /* XXX should fail */
956 if (!in_nullhost(p->ipopt_dst))
957 ip->ip_dst = p->ipopt_dst;
958 if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) {
959 MGETHDR(n, M_DONTWAIT, MT_HEADER);
960 if (n == 0)
961 return (m);
962 MCLAIM(n, m->m_owner);
963 M_MOVE_PKTHDR(n, m);
964 m->m_len -= sizeof(struct ip);
965 m->m_data += sizeof(struct ip);
966 n->m_next = m;
967 m = n;
968 m->m_len = optlen + sizeof(struct ip);
969 m->m_data += max_linkhdr;
970 bcopy((void *)ip, mtod(m, void *), sizeof(struct ip));
971 } else {
972 m->m_data -= optlen;
973 m->m_len += optlen;
974 memmove(mtod(m, void *), ip, sizeof(struct ip));
975 }
976 m->m_pkthdr.len += optlen;
977 ip = mtod(m, struct ip *);
978 bcopy((void *)p->ipopt_list, (void *)(ip + 1), (unsigned)optlen);
979 *phlen = sizeof(struct ip) + optlen;
980 ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
981 return (m);
982 }
983
984 /*
985 * Copy options from ip to jp,
986 * omitting those not copied during fragmentation.
987 */
988 int
989 ip_optcopy(struct ip *ip, struct ip *jp)
990 {
991 u_char *cp, *dp;
992 int opt, optlen, cnt;
993
994 cp = (u_char *)(ip + 1);
995 dp = (u_char *)(jp + 1);
996 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
997 for (; cnt > 0; cnt -= optlen, cp += optlen) {
998 opt = cp[0];
999 if (opt == IPOPT_EOL)
1000 break;
1001 if (opt == IPOPT_NOP) {
1002 /* Preserve for IP mcast tunnel's LSRR alignment. */
1003 *dp++ = IPOPT_NOP;
1004 optlen = 1;
1005 continue;
1006 }
1007 #ifdef DIAGNOSTIC
1008 if (cnt < IPOPT_OLEN + sizeof(*cp))
1009 panic("malformed IPv4 option passed to ip_optcopy");
1010 #endif
1011 optlen = cp[IPOPT_OLEN];
1012 #ifdef DIAGNOSTIC
1013 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1014 panic("malformed IPv4 option passed to ip_optcopy");
1015 #endif
1016 /* bogus lengths should have been caught by ip_dooptions */
1017 if (optlen > cnt)
1018 optlen = cnt;
1019 if (IPOPT_COPIED(opt)) {
1020 bcopy((void *)cp, (void *)dp, (unsigned)optlen);
1021 dp += optlen;
1022 }
1023 }
1024 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1025 *dp++ = IPOPT_EOL;
1026 return (optlen);
1027 }
1028
1029 /*
1030 * IP socket option processing.
1031 */
1032 int
1033 ip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1034 {
1035 struct inpcb *inp = sotoinpcb(so);
1036 int optval = 0;
1037 int error = 0;
1038 #if defined(FAST_IPSEC)
1039 struct lwp *l = curlwp; /*XXX*/
1040 #endif
1041
1042 if (sopt->sopt_level != IPPROTO_IP) {
1043 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER)
1044 return 0;
1045 return ENOPROTOOPT;
1046 }
1047
1048 switch (op) {
1049 case PRCO_SETOPT:
1050 switch (sopt->sopt_name) {
1051 case IP_OPTIONS:
1052 #ifdef notyet
1053 case IP_RETOPTS:
1054 #endif
1055 error = ip_pcbopts(&inp->inp_options, sopt);
1056 break;
1057
1058 case IP_TOS:
1059 case IP_TTL:
1060 case IP_MINTTL:
1061 case IP_RECVOPTS:
1062 case IP_RECVRETOPTS:
1063 case IP_RECVDSTADDR:
1064 case IP_RECVIF:
1065 case IP_RECVTTL:
1066 error = sockopt_getint(sopt, &optval);
1067 if (error)
1068 break;
1069
1070 switch (sopt->sopt_name) {
1071 case IP_TOS:
1072 inp->inp_ip.ip_tos = optval;
1073 break;
1074
1075 case IP_TTL:
1076 inp->inp_ip.ip_ttl = optval;
1077 break;
1078
1079 case IP_MINTTL:
1080 if (optval > 0 && optval <= MAXTTL)
1081 inp->inp_ip_minttl = optval;
1082 else
1083 error = EINVAL;
1084 break;
1085 #define OPTSET(bit) \
1086 if (optval) \
1087 inp->inp_flags |= bit; \
1088 else \
1089 inp->inp_flags &= ~bit;
1090
1091 case IP_RECVOPTS:
1092 OPTSET(INP_RECVOPTS);
1093 break;
1094
1095 case IP_RECVRETOPTS:
1096 OPTSET(INP_RECVRETOPTS);
1097 break;
1098
1099 case IP_RECVDSTADDR:
1100 OPTSET(INP_RECVDSTADDR);
1101 break;
1102
1103 case IP_RECVIF:
1104 OPTSET(INP_RECVIF);
1105 break;
1106
1107 case IP_RECVTTL:
1108 OPTSET(INP_RECVTTL);
1109 break;
1110 }
1111 break;
1112 #undef OPTSET
1113
1114 case IP_MULTICAST_IF:
1115 case IP_MULTICAST_TTL:
1116 case IP_MULTICAST_LOOP:
1117 case IP_ADD_MEMBERSHIP:
1118 case IP_DROP_MEMBERSHIP:
1119 error = ip_setmoptions(&inp->inp_moptions, sopt);
1120 break;
1121
1122 case IP_PORTRANGE:
1123 error = sockopt_getint(sopt, &optval);
1124 if (error)
1125 break;
1126
1127 /* INP_LOCK(inp); */
1128 switch (optval) {
1129 case IP_PORTRANGE_DEFAULT:
1130 case IP_PORTRANGE_HIGH:
1131 inp->inp_flags &= ~(INP_LOWPORT);
1132 break;
1133
1134 case IP_PORTRANGE_LOW:
1135 inp->inp_flags |= INP_LOWPORT;
1136 break;
1137
1138 default:
1139 error = EINVAL;
1140 break;
1141 }
1142 /* INP_UNLOCK(inp); */
1143 break;
1144
1145 #if defined(FAST_IPSEC)
1146 case IP_IPSEC_POLICY:
1147 {
1148 error = ipsec4_set_policy(inp, sopt->sopt_name,
1149 sopt->sopt_data, sopt->sopt_size, l->l_cred);
1150 break;
1151 }
1152 #endif /*IPSEC*/
1153
1154 default:
1155 error = ENOPROTOOPT;
1156 break;
1157 }
1158 break;
1159
1160 case PRCO_GETOPT:
1161 switch (sopt->sopt_name) {
1162 case IP_OPTIONS:
1163 case IP_RETOPTS:
1164 if (inp->inp_options) {
1165 struct mbuf *m;
1166
1167 m = m_copym(inp->inp_options, 0, M_COPYALL,
1168 M_DONTWAIT);
1169 if (m == NULL) {
1170 error = ENOBUFS;
1171 break;
1172 }
1173
1174 error = sockopt_setmbuf(sopt, m);
1175 }
1176 break;
1177
1178 case IP_TOS:
1179 case IP_TTL:
1180 case IP_MINTTL:
1181 case IP_RECVOPTS:
1182 case IP_RECVRETOPTS:
1183 case IP_RECVDSTADDR:
1184 case IP_RECVIF:
1185 case IP_RECVTTL:
1186 case IP_ERRORMTU:
1187 switch (sopt->sopt_name) {
1188 case IP_TOS:
1189 optval = inp->inp_ip.ip_tos;
1190 break;
1191
1192 case IP_TTL:
1193 optval = inp->inp_ip.ip_ttl;
1194 break;
1195
1196 case IP_MINTTL:
1197 optval = inp->inp_ip_minttl;
1198 break;
1199
1200 case IP_ERRORMTU:
1201 optval = inp->inp_errormtu;
1202 break;
1203
1204 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1205
1206 case IP_RECVOPTS:
1207 optval = OPTBIT(INP_RECVOPTS);
1208 break;
1209
1210 case IP_RECVRETOPTS:
1211 optval = OPTBIT(INP_RECVRETOPTS);
1212 break;
1213
1214 case IP_RECVDSTADDR:
1215 optval = OPTBIT(INP_RECVDSTADDR);
1216 break;
1217
1218 case IP_RECVIF:
1219 optval = OPTBIT(INP_RECVIF);
1220 break;
1221
1222 case IP_RECVTTL:
1223 optval = OPTBIT(INP_RECVTTL);
1224 break;
1225 }
1226 error = sockopt_setint(sopt, optval);
1227 break;
1228
1229 #if 0 /* defined(FAST_IPSEC) */
1230 case IP_IPSEC_POLICY:
1231 {
1232 struct mbuf *m = NULL;
1233
1234 /* XXX this will return EINVAL as sopt is empty */
1235 error = ipsec4_get_policy(inp, sopt->sopt_data,
1236 sopt->sopt_size, &m);
1237 if (error == 0)
1238 error = sockopt_setmbuf(sopt, m);
1239 break;
1240 }
1241 #endif /*IPSEC*/
1242
1243 case IP_MULTICAST_IF:
1244 case IP_MULTICAST_TTL:
1245 case IP_MULTICAST_LOOP:
1246 case IP_ADD_MEMBERSHIP:
1247 case IP_DROP_MEMBERSHIP:
1248 error = ip_getmoptions(inp->inp_moptions, sopt);
1249 break;
1250
1251 case IP_PORTRANGE:
1252 if (inp->inp_flags & INP_LOWPORT)
1253 optval = IP_PORTRANGE_LOW;
1254 else
1255 optval = IP_PORTRANGE_DEFAULT;
1256
1257 error = sockopt_setint(sopt, optval);
1258
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