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