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