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