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