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