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