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