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