ip_output.c revision 1.131 1 /* $NetBSD: ip_output.c,v 1.131 2004/04/26 01:31:57 matt 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.131 2004/04/26 01:31:57 matt 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 }
591 }
592 skip_ipsec:
593 #endif /*IPSEC*/
594 #ifdef FAST_IPSEC
595 /*
596 * Check the security policy (SP) for the packet and, if
597 * required, do IPsec-related processing. There are two
598 * cases here; the first time a packet is sent through
599 * it will be untagged and handled by ipsec4_checkpolicy.
600 * If the packet is resubmitted to ip_output (e.g. after
601 * AH, ESP, etc. processing), there will be a tag to bypass
602 * the lookup and related policy checking.
603 */
604 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
605 s = splsoftnet();
606 if (mtag != NULL) {
607 tdbi = (struct tdb_ident *)(mtag + 1);
608 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
609 if (sp == NULL)
610 error = -EINVAL; /* force silent drop */
611 m_tag_delete(m, mtag);
612 } else {
613 if (inp != NULL &&
614 IPSEC_PCB_SKIP_IPSEC(inp->inp_sp, IPSEC_DIR_OUTBOUND))
615 goto spd_done;
616 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
617 &error, inp);
618 }
619 /*
620 * There are four return cases:
621 * sp != NULL apply IPsec policy
622 * sp == NULL, error == 0 no IPsec handling needed
623 * sp == NULL, error == -EINVAL discard packet w/o error
624 * sp == NULL, error != 0 discard packet, report error
625 */
626 if (sp != NULL) {
627 /* Loop detection, check if ipsec processing already done */
628 IPSEC_ASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
629 for (mtag = m_tag_first(m); mtag != NULL;
630 mtag = m_tag_next(m, mtag)) {
631 #ifdef MTAG_ABI_COMPAT
632 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
633 continue;
634 #endif
635 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
636 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
637 continue;
638 /*
639 * Check if policy has an SA associated with it.
640 * This can happen when an SP has yet to acquire
641 * an SA; e.g. on first reference. If it occurs,
642 * then we let ipsec4_process_packet do its thing.
643 */
644 if (sp->req->sav == NULL)
645 break;
646 tdbi = (struct tdb_ident *)(mtag + 1);
647 if (tdbi->spi == sp->req->sav->spi &&
648 tdbi->proto == sp->req->sav->sah->saidx.proto &&
649 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
650 sizeof (union sockaddr_union)) == 0) {
651 /*
652 * No IPsec processing is needed, free
653 * reference to SP.
654 *
655 * NB: null pointer to avoid free at
656 * done: below.
657 */
658 KEY_FREESP(&sp), sp = NULL;
659 splx(s);
660 goto spd_done;
661 }
662 }
663
664 /*
665 * Do delayed checksums now because we send before
666 * this is done in the normal processing path.
667 */
668 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
669 in_delayed_cksum(m);
670 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
671 }
672
673 #ifdef __FreeBSD__
674 ip->ip_len = htons(ip->ip_len);
675 ip->ip_off = htons(ip->ip_off);
676 #endif
677
678 /* NB: callee frees mbuf */
679 error = ipsec4_process_packet(m, sp->req, flags, 0);
680 /*
681 * Preserve KAME behaviour: ENOENT can be returned
682 * when an SA acquire is in progress. Don't propagate
683 * this to user-level; it confuses applications.
684 *
685 * XXX this will go away when the SADB is redone.
686 */
687 if (error == ENOENT)
688 error = 0;
689 splx(s);
690 goto done;
691 } else {
692 splx(s);
693
694 if (error != 0) {
695 /*
696 * Hack: -EINVAL is used to signal that a packet
697 * should be silently discarded. This is typically
698 * because we asked key management for an SA and
699 * it was delayed (e.g. kicked up to IKE).
700 */
701 if (error == -EINVAL)
702 error = 0;
703 goto bad;
704 } else {
705 /* No IPsec processing for this packet. */
706 }
707 #ifdef notyet
708 /*
709 * If deferred crypto processing is needed, check that
710 * the interface supports it.
711 */
712 mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
713 if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
714 /* notify IPsec to do its own crypto */
715 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
716 error = EHOSTUNREACH;
717 goto bad;
718 }
719 #endif
720 }
721 spd_done:
722 #endif /* FAST_IPSEC */
723
724 #ifdef PFIL_HOOKS
725 /*
726 * Run through list of hooks for output packets.
727 */
728 if ((error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT)) != 0)
729 goto done;
730 if (m == NULL)
731 goto done;
732
733 ip = mtod(m, struct ip *);
734 hlen = ip->ip_hl << 2;
735 #endif /* PFIL_HOOKS */
736
737 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
738 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
739 /*
740 * If small enough for mtu of path, can just send directly.
741 */
742 if (ip_len <= mtu) {
743 #if IFA_STATS
744 /*
745 * search for the source address structure to
746 * maintain output statistics.
747 */
748 INADDR_TO_IA(ip->ip_src, ia);
749 if (ia)
750 ia->ia_ifa.ifa_data.ifad_outbytes += ip_len;
751 #endif
752 /*
753 * Always initialize the sum to 0! Some HW assisted
754 * checksumming requires this.
755 */
756 ip->ip_sum = 0;
757
758 /*
759 * Perform any checksums that the hardware can't do
760 * for us.
761 *
762 * XXX Does any hardware require the {th,uh}_sum
763 * XXX fields to be 0?
764 */
765 if (sw_csum & M_CSUM_IPv4) {
766 ip->ip_sum = in_cksum(m, hlen);
767 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
768 }
769 if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
770 in_delayed_cksum(m);
771 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
772 }
773
774 #ifdef IPSEC
775 /* clean ipsec history once it goes out of the node */
776 ipsec_delaux(m);
777 #endif
778 error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt);
779 goto done;
780 }
781
782 /*
783 * We can't use HW checksumming if we're about to
784 * to fragment the packet.
785 *
786 * XXX Some hardware can do this.
787 */
788 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
789 in_delayed_cksum(m);
790 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
791 }
792
793 /*
794 * Too large for interface; fragment if possible.
795 * Must be able to put at least 8 bytes per fragment.
796 */
797 if (ntohs(ip->ip_off) & IP_DF) {
798 if (flags & IP_RETURNMTU)
799 *mtu_p = mtu;
800 error = EMSGSIZE;
801 ipstat.ips_cantfrag++;
802 goto bad;
803 }
804
805 error = ip_fragment(m, ifp, mtu);
806 if (error) {
807 m = NULL;
808 goto bad;
809 }
810
811 for (; m; m = m0) {
812 m0 = m->m_nextpkt;
813 m->m_nextpkt = 0;
814 if (error == 0) {
815 #if IFA_STATS
816 /*
817 * search for the source address structure to
818 * maintain output statistics.
819 */
820 INADDR_TO_IA(ip->ip_src, ia);
821 if (ia) {
822 ia->ia_ifa.ifa_data.ifad_outbytes +=
823 ntohs(ip->ip_len);
824 }
825 #endif
826 #ifdef IPSEC
827 /* clean ipsec history once it goes out of the node */
828 ipsec_delaux(m);
829 #endif
830 KASSERT((m->m_pkthdr.csum_flags &
831 (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
832 error = (*ifp->if_output)(ifp, m, sintosa(dst),
833 ro->ro_rt);
834 } else
835 m_freem(m);
836 }
837
838 if (error == 0)
839 ipstat.ips_fragmented++;
840 done:
841 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
842 RTFREE(ro->ro_rt);
843 ro->ro_rt = 0;
844 }
845
846 #ifdef IPSEC
847 if (sp != NULL) {
848 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
849 printf("DP ip_output call free SP:%p\n", sp));
850 key_freesp(sp);
851 }
852 #endif /* IPSEC */
853 #ifdef FAST_IPSEC
854 if (sp != NULL)
855 KEY_FREESP(&sp);
856 #endif /* FAST_IPSEC */
857
858 return (error);
859 bad:
860 m_freem(m);
861 goto done;
862 }
863
864 int
865 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
866 {
867 struct ip *ip, *mhip;
868 struct mbuf *m0;
869 int len, hlen, off;
870 int mhlen, firstlen;
871 struct mbuf **mnext;
872 int sw_csum;
873 int fragments = 0;
874 int s;
875 int error = 0;
876
877 ip = mtod(m, struct ip *);
878 hlen = ip->ip_hl << 2;
879 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
880
881 len = (mtu - hlen) &~ 7;
882 if (len < 8) {
883 m_freem(m);
884 return (EMSGSIZE);
885 }
886
887 firstlen = len;
888 mnext = &m->m_nextpkt;
889
890 /*
891 * Loop through length of segment after first fragment,
892 * make new header and copy data of each part and link onto chain.
893 */
894 m0 = m;
895 mhlen = sizeof (struct ip);
896 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
897 MGETHDR(m, M_DONTWAIT, MT_HEADER);
898 if (m == 0) {
899 error = ENOBUFS;
900 ipstat.ips_odropped++;
901 goto sendorfree;
902 }
903 MCLAIM(m, m0->m_owner);
904 *mnext = m;
905 mnext = &m->m_nextpkt;
906 m->m_data += max_linkhdr;
907 mhip = mtod(m, struct ip *);
908 *mhip = *ip;
909 /* we must inherit MCAST and BCAST flags */
910 m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
911 if (hlen > sizeof (struct ip)) {
912 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
913 mhip->ip_hl = mhlen >> 2;
914 }
915 m->m_len = mhlen;
916 mhip->ip_off = ((off - hlen) >> 3) +
917 (ntohs(ip->ip_off) & ~IP_MF);
918 if (ip->ip_off & htons(IP_MF))
919 mhip->ip_off |= IP_MF;
920 if (off + len >= ntohs(ip->ip_len))
921 len = ntohs(ip->ip_len) - off;
922 else
923 mhip->ip_off |= IP_MF;
924 HTONS(mhip->ip_off);
925 mhip->ip_len = htons((u_int16_t)(len + mhlen));
926 m->m_next = m_copy(m0, off, len);
927 if (m->m_next == 0) {
928 error = ENOBUFS; /* ??? */
929 ipstat.ips_odropped++;
930 goto sendorfree;
931 }
932 m->m_pkthdr.len = mhlen + len;
933 m->m_pkthdr.rcvif = (struct ifnet *)0;
934 mhip->ip_sum = 0;
935 if (sw_csum & M_CSUM_IPv4) {
936 mhip->ip_sum = in_cksum(m, mhlen);
937 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
938 } else {
939 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
940 }
941 ipstat.ips_ofragments++;
942 fragments++;
943 }
944 /*
945 * Update first fragment by trimming what's been copied out
946 * and updating header, then send each fragment (in order).
947 */
948 m = m0;
949 m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
950 m->m_pkthdr.len = hlen + firstlen;
951 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
952 ip->ip_off |= htons(IP_MF);
953 ip->ip_sum = 0;
954 if (sw_csum & M_CSUM_IPv4) {
955 ip->ip_sum = in_cksum(m, hlen);
956 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
957 } else {
958 KASSERT(m->m_pkthdr.csum_flags & M_CSUM_IPv4);
959 }
960 sendorfree:
961 /*
962 * If there is no room for all the fragments, don't queue
963 * any of them.
964 */
965 s = splnet();
966 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments &&
967 error == 0) {
968 error = ENOBUFS;
969 ipstat.ips_odropped++;
970 IFQ_INC_DROPS(&ifp->if_snd);
971 }
972 splx(s);
973 if (error) {
974 for (m = m0; m; m = m0) {
975 m0 = m->m_nextpkt;
976 m->m_nextpkt = NULL;
977 m_freem(m);
978 }
979 }
980 return (error);
981 }
982
983 /*
984 * Process a delayed payload checksum calculation.
985 */
986 void
987 in_delayed_cksum(struct mbuf *m)
988 {
989 struct ip *ip;
990 u_int16_t csum, offset;
991
992 ip = mtod(m, struct ip *);
993 offset = ip->ip_hl << 2;
994 csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset);
995 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0)
996 csum = 0xffff;
997
998 offset += m->m_pkthdr.csum_data; /* checksum offset */
999
1000 if ((offset + sizeof(u_int16_t)) > m->m_len) {
1001 /* This happen when ip options were inserted
1002 printf("in_delayed_cksum: pullup len %d off %d proto %d\n",
1003 m->m_len, offset, ip->ip_p);
1004 */
1005 m_copyback(m, offset, sizeof(csum), (caddr_t) &csum);
1006 } else
1007 *(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
1008 }
1009
1010 /*
1011 * Determine the maximum length of the options to be inserted;
1012 * we would far rather allocate too much space rather than too little.
1013 */
1014
1015 u_int
1016 ip_optlen(inp)
1017 struct inpcb *inp;
1018 {
1019 struct mbuf *m = inp->inp_options;
1020
1021 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
1022 return (m->m_len - offsetof(struct ipoption, ipopt_dst));
1023 else
1024 return 0;
1025 }
1026
1027
1028 /*
1029 * Insert IP options into preformed packet.
1030 * Adjust IP destination as required for IP source routing,
1031 * as indicated by a non-zero in_addr at the start of the options.
1032 */
1033 static struct mbuf *
1034 ip_insertoptions(m, opt, phlen)
1035 struct mbuf *m;
1036 struct mbuf *opt;
1037 int *phlen;
1038 {
1039 struct ipoption *p = mtod(opt, struct ipoption *);
1040 struct mbuf *n;
1041 struct ip *ip = mtod(m, struct ip *);
1042 unsigned optlen;
1043
1044 optlen = opt->m_len - sizeof(p->ipopt_dst);
1045 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
1046 return (m); /* XXX should fail */
1047 if (!in_nullhost(p->ipopt_dst))
1048 ip->ip_dst = p->ipopt_dst;
1049 if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) {
1050 MGETHDR(n, M_DONTWAIT, MT_HEADER);
1051 if (n == 0)
1052 return (m);
1053 MCLAIM(n, m->m_owner);
1054 M_COPY_PKTHDR(n, m);
1055 m_tag_delete_chain(m, NULL);
1056 m->m_flags &= ~M_PKTHDR;
1057 m->m_len -= sizeof(struct ip);
1058 m->m_data += sizeof(struct ip);
1059 n->m_next = m;
1060 m = n;
1061 m->m_len = optlen + sizeof(struct ip);
1062 m->m_data += max_linkhdr;
1063 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1064 } else {
1065 m->m_data -= optlen;
1066 m->m_len += optlen;
1067 memmove(mtod(m, caddr_t), ip, sizeof(struct ip));
1068 }
1069 m->m_pkthdr.len += optlen;
1070 ip = mtod(m, struct ip *);
1071 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
1072 *phlen = sizeof(struct ip) + optlen;
1073 ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
1074 return (m);
1075 }
1076
1077 /*
1078 * Copy options from ip to jp,
1079 * omitting those not copied during fragmentation.
1080 */
1081 int
1082 ip_optcopy(ip, jp)
1083 struct ip *ip, *jp;
1084 {
1085 u_char *cp, *dp;
1086 int opt, optlen, cnt;
1087
1088 cp = (u_char *)(ip + 1);
1089 dp = (u_char *)(jp + 1);
1090 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1091 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1092 opt = cp[0];
1093 if (opt == IPOPT_EOL)
1094 break;
1095 if (opt == IPOPT_NOP) {
1096 /* Preserve for IP mcast tunnel's LSRR alignment. */
1097 *dp++ = IPOPT_NOP;
1098 optlen = 1;
1099 continue;
1100 }
1101 #ifdef DIAGNOSTIC
1102 if (cnt < IPOPT_OLEN + sizeof(*cp))
1103 panic("malformed IPv4 option passed to ip_optcopy");
1104 #endif
1105 optlen = cp[IPOPT_OLEN];
1106 #ifdef DIAGNOSTIC
1107 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1108 panic("malformed IPv4 option passed to ip_optcopy");
1109 #endif
1110 /* bogus lengths should have been caught by ip_dooptions */
1111 if (optlen > cnt)
1112 optlen = cnt;
1113 if (IPOPT_COPIED(opt)) {
1114 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
1115 dp += optlen;
1116 }
1117 }
1118 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1119 *dp++ = IPOPT_EOL;
1120 return (optlen);
1121 }
1122
1123 /*
1124 * IP socket option processing.
1125 */
1126 int
1127 ip_ctloutput(op, so, level, optname, mp)
1128 int op;
1129 struct socket *so;
1130 int level, optname;
1131 struct mbuf **mp;
1132 {
1133 struct inpcb *inp = sotoinpcb(so);
1134 struct mbuf *m = *mp;
1135 int optval = 0;
1136 int error = 0;
1137 #if defined(IPSEC) || defined(FAST_IPSEC)
1138 struct proc *p = curproc; /*XXX*/
1139 #endif
1140
1141 if (level != IPPROTO_IP) {
1142 error = EINVAL;
1143 if (op == PRCO_SETOPT && *mp)
1144 (void) m_free(*mp);
1145 } else switch (op) {
1146
1147 case PRCO_SETOPT:
1148 switch (optname) {
1149 case IP_OPTIONS:
1150 #ifdef notyet
1151 case IP_RETOPTS:
1152 return (ip_pcbopts(optname, &inp->inp_options, m));
1153 #else
1154 return (ip_pcbopts(&inp->inp_options, m));
1155 #endif
1156
1157 case IP_TOS:
1158 case IP_TTL:
1159 case IP_RECVOPTS:
1160 case IP_RECVRETOPTS:
1161 case IP_RECVDSTADDR:
1162 case IP_RECVIF:
1163 if (m == NULL || m->m_len != sizeof(int))
1164 error = EINVAL;
1165 else {
1166 optval = *mtod(m, int *);
1167 switch (optname) {
1168
1169 case IP_TOS:
1170 inp->inp_ip.ip_tos = optval;
1171 break;
1172
1173 case IP_TTL:
1174 inp->inp_ip.ip_ttl = optval;
1175 break;
1176 #define OPTSET(bit) \
1177 if (optval) \
1178 inp->inp_flags |= bit; \
1179 else \
1180 inp->inp_flags &= ~bit;
1181
1182 case IP_RECVOPTS:
1183 OPTSET(INP_RECVOPTS);
1184 break;
1185
1186 case IP_RECVRETOPTS:
1187 OPTSET(INP_RECVRETOPTS);
1188 break;
1189
1190 case IP_RECVDSTADDR:
1191 OPTSET(INP_RECVDSTADDR);
1192 break;
1193
1194 case IP_RECVIF:
1195 OPTSET(INP_RECVIF);
1196 break;
1197 }
1198 }
1199 break;
1200 #undef OPTSET
1201
1202 case IP_MULTICAST_IF:
1203 case IP_MULTICAST_TTL:
1204 case IP_MULTICAST_LOOP:
1205 case IP_ADD_MEMBERSHIP:
1206 case IP_DROP_MEMBERSHIP:
1207 error = ip_setmoptions(optname, &inp->inp_moptions, m);
1208 break;
1209
1210 case IP_PORTRANGE:
1211 if (m == 0 || m->m_len != sizeof(int))
1212 error = EINVAL;
1213 else {
1214 optval = *mtod(m, int *);
1215
1216 switch (optval) {
1217
1218 case IP_PORTRANGE_DEFAULT:
1219 case IP_PORTRANGE_HIGH:
1220 inp->inp_flags &= ~(INP_LOWPORT);
1221 break;
1222
1223 case IP_PORTRANGE_LOW:
1224 inp->inp_flags |= INP_LOWPORT;
1225 break;
1226
1227 default:
1228 error = EINVAL;
1229 break;
1230 }
1231 }
1232 break;
1233
1234 #if defined(IPSEC) || defined(FAST_IPSEC)
1235 case IP_IPSEC_POLICY:
1236 {
1237 caddr_t req = NULL;
1238 size_t len = 0;
1239 int priv = 0;
1240
1241 #ifdef __NetBSD__
1242 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
1243 priv = 0;
1244 else
1245 priv = 1;
1246 #else
1247 priv = (in6p->in6p_socket->so_state & SS_PRIV);
1248 #endif
1249 if (m) {
1250 req = mtod(m, caddr_t);
1251 len = m->m_len;
1252 }
1253 error = ipsec4_set_policy(inp, optname, req, len, priv);
1254 break;
1255 }
1256 #endif /*IPSEC*/
1257
1258 default:
1259 error = ENOPROTOOPT;
1260 break;
1261 }
1262 if (m)
1263 (void)m_free(m);
1264 break;
1265
1266 case PRCO_GETOPT:
1267 switch (optname) {
1268 case IP_OPTIONS:
1269 case IP_RETOPTS:
1270 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1271 MCLAIM(m, so->so_mowner);
1272 if (inp->inp_options) {
1273 m->m_len = inp->inp_options->m_len;
1274 bcopy(mtod(inp->inp_options, caddr_t),
1275 mtod(m, caddr_t), (unsigned)m->m_len);
1276 } else
1277 m->m_len = 0;
1278 break;
1279
1280 case IP_TOS:
1281 case IP_TTL:
1282 case IP_RECVOPTS:
1283 case IP_RECVRETOPTS:
1284 case IP_RECVDSTADDR:
1285 case IP_RECVIF:
1286 case IP_ERRORMTU:
1287 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1288 MCLAIM(m, so->so_mowner);
1289 m->m_len = sizeof(int);
1290 switch (optname) {
1291
1292 case IP_TOS:
1293 optval = inp->inp_ip.ip_tos;
1294 break;
1295
1296 case IP_TTL:
1297 optval = inp->inp_ip.ip_ttl;
1298 break;
1299
1300 case IP_ERRORMTU:
1301 optval = inp->inp_errormtu;
1302 break;
1303
1304 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1305
1306 case IP_RECVOPTS:
1307 optval = OPTBIT(INP_RECVOPTS);
1308 break;
1309
1310 case IP_RECVRETOPTS:
1311 optval = OPTBIT(INP_RECVRETOPTS);
1312 break;
1313
1314 case IP_RECVDSTADDR:
1315 optval = OPTBIT(INP_RECVDSTADDR);
1316 break;
1317
1318 case IP_RECVIF:
1319 optval = OPTBIT(INP_RECVIF);
1320 break;
1321 }
1322 *mtod(m, int *) = optval;
1323 break;
1324
1325 #if defined(IPSEC) || defined(FAST_IPSEC)
1326 case IP_IPSEC_POLICY:
1327 {
1328 caddr_t req = NULL;
1329 size_t len = 0;
1330
1331 if (m) {
1332 req = mtod(m, caddr_t);
1333 len = m->m_len;
1334 }
1335 error = ipsec4_get_policy(inp, req, len, mp);
1336 break;
1337 }
1338 #endif /*IPSEC*/
1339
1340 case IP_MULTICAST_IF:
1341 case IP_MULTICAST_TTL:
1342 case IP_MULTICAST_LOOP:
1343 case IP_ADD_MEMBERSHIP:
1344 case IP_DROP_MEMBERSHIP:
1345 error = ip_getmoptions(optname, inp->inp_moptions, mp);
1346 if (*mp)
1347 MCLAIM(*mp, so->so_mowner);
1348 break;
1349
1350 case IP_PORTRANGE:
1351 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1352 MCLAIM(m, so->so_mowner);
1353 m->m_len = sizeof(int);
1354
1355 if (inp->inp_flags & INP_LOWPORT)
1356 optval = IP_PORTRANGE_LOW;
1357 else
1358 optval = IP_PORTRANGE_DEFAULT;
1359
1360 *mtod(m, int *) = optval;
1361 break;
1362
1363 default:
1364 error = ENOPROTOOPT;
1365 break;
1366 }
1367 break;
1368 }
1369 return (error);
1370 }
1371
1372 /*
1373 * Set up IP options in pcb for insertion in output packets.
1374 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1375 * with destination address if source routed.
1376 */
1377 int
1378 #ifdef notyet
1379 ip_pcbopts(optname, pcbopt, m)
1380 int optname;
1381 #else
1382 ip_pcbopts(pcbopt, m)
1383 #endif
1384 struct mbuf **pcbopt;
1385 struct mbuf *m;
1386 {
1387 int cnt, optlen;
1388 u_char *cp;
1389 u_char opt;
1390
1391 /* turn off any old options */
1392 if (*pcbopt)
1393 (void)m_free(*pcbopt);
1394 *pcbopt = 0;
1395 if (m == (struct mbuf *)0 || m->m_len == 0) {
1396 /*
1397 * Only turning off any previous options.
1398 */
1399 if (m)
1400 (void)m_free(m);
1401 return (0);
1402 }
1403
1404 #ifndef __vax__
1405 if (m->m_len % sizeof(int32_t))
1406 goto bad;
1407 #endif
1408 /*
1409 * IP first-hop destination address will be stored before
1410 * actual options; move other options back
1411 * and clear it when none present.
1412 */
1413 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1414 goto bad;
1415 cnt = m->m_len;
1416 m->m_len += sizeof(struct in_addr);
1417 cp = mtod(m, u_char *) + sizeof(struct in_addr);
1418 memmove(cp, mtod(m, caddr_t), (unsigned)cnt);
1419 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1420
1421 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1422 opt = cp[IPOPT_OPTVAL];
1423 if (opt == IPOPT_EOL)
1424 break;
1425 if (opt == IPOPT_NOP)
1426 optlen = 1;
1427 else {
1428 if (cnt < IPOPT_OLEN + sizeof(*cp))
1429 goto bad;
1430 optlen = cp[IPOPT_OLEN];
1431 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1432 goto bad;
1433 }
1434 switch (opt) {
1435
1436 default:
1437 break;
1438
1439 case IPOPT_LSRR:
1440 case IPOPT_SSRR:
1441 /*
1442 * user process specifies route as:
1443 * ->A->B->C->D
1444 * D must be our final destination (but we can't
1445 * check that since we may not have connected yet).
1446 * A is first hop destination, which doesn't appear in
1447 * actual IP option, but is stored before the options.
1448 */
1449 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1450 goto bad;
1451 m->m_len -= sizeof(struct in_addr);
1452 cnt -= sizeof(struct in_addr);
1453 optlen -= sizeof(struct in_addr);
1454 cp[IPOPT_OLEN] = optlen;
1455 /*
1456 * Move first hop before start of options.
1457 */
1458 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1459 sizeof(struct in_addr));
1460 /*
1461 * Then copy rest of options back
1462 * to close up the deleted entry.
1463 */
1464 memmove(&cp[IPOPT_OFFSET+1],
1465 (caddr_t)(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1466 (unsigned)cnt + sizeof(struct in_addr));
1467 break;
1468 }
1469 }
1470 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1471 goto bad;
1472 *pcbopt = m;
1473 return (0);
1474
1475 bad:
1476 (void)m_free(m);
1477 return (EINVAL);
1478 }
1479
1480 /*
1481 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1482 */
1483 static struct ifnet *
1484 ip_multicast_if(a, ifindexp)
1485 struct in_addr *a;
1486 int *ifindexp;
1487 {
1488 int ifindex;
1489 struct ifnet *ifp = NULL;
1490 struct in_ifaddr *ia;
1491
1492 if (ifindexp)
1493 *ifindexp = 0;
1494 if (ntohl(a->s_addr) >> 24 == 0) {
1495 ifindex = ntohl(a->s_addr) & 0xffffff;
1496 if (ifindex < 0 || if_indexlim <= ifindex)
1497 return NULL;
1498 ifp = ifindex2ifnet[ifindex];
1499 if (!ifp)
1500 return NULL;
1501 if (ifindexp)
1502 *ifindexp = ifindex;
1503 } else {
1504 LIST_FOREACH(ia, &IN_IFADDR_HASH(a->s_addr), ia_hash) {
1505 if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
1506 (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
1507 ifp = ia->ia_ifp;
1508 break;
1509 }
1510 }
1511 }
1512 return ifp;
1513 }
1514
1515 /*
1516 * Set the IP multicast options in response to user setsockopt().
1517 */
1518 int
1519 ip_setmoptions(optname, imop, m)
1520 int optname;
1521 struct ip_moptions **imop;
1522 struct mbuf *m;
1523 {
1524 int error = 0;
1525 u_char loop;
1526 int i;
1527 struct in_addr addr;
1528 struct ip_mreq *mreq;
1529 struct ifnet *ifp;
1530 struct ip_moptions *imo = *imop;
1531 struct route ro;
1532 struct sockaddr_in *dst;
1533 int ifindex;
1534
1535 if (imo == NULL) {
1536 /*
1537 * No multicast option buffer attached to the pcb;
1538 * allocate one and initialize to default values.
1539 */
1540 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
1541 M_WAITOK);
1542
1543 if (imo == NULL)
1544 return (ENOBUFS);
1545 *imop = imo;
1546 imo->imo_multicast_ifp = NULL;
1547 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1548 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1549 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1550 imo->imo_num_memberships = 0;
1551 }
1552
1553 switch (optname) {
1554
1555 case IP_MULTICAST_IF:
1556 /*
1557 * Select the interface for outgoing multicast packets.
1558 */
1559 if (m == NULL || m->m_len != sizeof(struct in_addr)) {
1560 error = EINVAL;
1561 break;
1562 }
1563 addr = *(mtod(m, struct in_addr *));
1564 /*
1565 * INADDR_ANY is used to remove a previous selection.
1566 * When no interface is selected, a default one is
1567 * chosen every time a multicast packet is sent.
1568 */
1569 if (in_nullhost(addr)) {
1570 imo->imo_multicast_ifp = NULL;
1571 break;
1572 }
1573 /*
1574 * The selected interface is identified by its local
1575 * IP address. Find the interface and confirm that
1576 * it supports multicasting.
1577 */
1578 ifp = ip_multicast_if(&addr, &ifindex);
1579 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1580 error = EADDRNOTAVAIL;
1581 break;
1582 }
1583 imo->imo_multicast_ifp = ifp;
1584 if (ifindex)
1585 imo->imo_multicast_addr = addr;
1586 else
1587 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1588 break;
1589
1590 case IP_MULTICAST_TTL:
1591 /*
1592 * Set the IP time-to-live for outgoing multicast packets.
1593 */
1594 if (m == NULL || m->m_len != 1) {
1595 error = EINVAL;
1596 break;
1597 }
1598 imo->imo_multicast_ttl = *(mtod(m, u_char *));
1599 break;
1600
1601 case IP_MULTICAST_LOOP:
1602 /*
1603 * Set the loopback flag for outgoing multicast packets.
1604 * Must be zero or one.
1605 */
1606 if (m == NULL || m->m_len != 1 ||
1607 (loop = *(mtod(m, u_char *))) > 1) {
1608 error = EINVAL;
1609 break;
1610 }
1611 imo->imo_multicast_loop = loop;
1612 break;
1613
1614 case IP_ADD_MEMBERSHIP:
1615 /*
1616 * Add a multicast group membership.
1617 * Group must be a valid IP multicast address.
1618 */
1619 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1620 error = EINVAL;
1621 break;
1622 }
1623 mreq = mtod(m, struct ip_mreq *);
1624 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1625 error = EINVAL;
1626 break;
1627 }
1628 /*
1629 * If no interface address was provided, use the interface of
1630 * the route to the given multicast address.
1631 */
1632 if (in_nullhost(mreq->imr_interface)) {
1633 bzero((caddr_t)&ro, sizeof(ro));
1634 ro.ro_rt = NULL;
1635 dst = satosin(&ro.ro_dst);
1636 dst->sin_len = sizeof(*dst);
1637 dst->sin_family = AF_INET;
1638 dst->sin_addr = mreq->imr_multiaddr;
1639 rtalloc(&ro);
1640 if (ro.ro_rt == NULL) {
1641 error = EADDRNOTAVAIL;
1642 break;
1643 }
1644 ifp = ro.ro_rt->rt_ifp;
1645 rtfree(ro.ro_rt);
1646 } else {
1647 ifp = ip_multicast_if(&mreq->imr_interface, NULL);
1648 }
1649 /*
1650 * See if we found an interface, and confirm that it
1651 * supports multicast.
1652 */
1653 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1654 error = EADDRNOTAVAIL;
1655 break;
1656 }
1657 /*
1658 * See if the membership already exists or if all the
1659 * membership slots are full.
1660 */
1661 for (i = 0; i < imo->imo_num_memberships; ++i) {
1662 if (imo->imo_membership[i]->inm_ifp == ifp &&
1663 in_hosteq(imo->imo_membership[i]->inm_addr,
1664 mreq->imr_multiaddr))
1665 break;
1666 }
1667 if (i < imo->imo_num_memberships) {
1668 error = EADDRINUSE;
1669 break;
1670 }
1671 if (i == IP_MAX_MEMBERSHIPS) {
1672 error = ETOOMANYREFS;
1673 break;
1674 }
1675 /*
1676 * Everything looks good; add a new record to the multicast
1677 * address list for the given interface.
1678 */
1679 if ((imo->imo_membership[i] =
1680 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1681 error = ENOBUFS;
1682 break;
1683 }
1684 ++imo->imo_num_memberships;
1685 break;
1686
1687 case IP_DROP_MEMBERSHIP:
1688 /*
1689 * Drop a multicast group membership.
1690 * Group must be a valid IP multicast address.
1691 */
1692 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1693 error = EINVAL;
1694 break;
1695 }
1696 mreq = mtod(m, struct ip_mreq *);
1697 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1698 error = EINVAL;
1699 break;
1700 }
1701 /*
1702 * If an interface address was specified, get a pointer
1703 * to its ifnet structure.
1704 */
1705 if (in_nullhost(mreq->imr_interface))
1706 ifp = NULL;
1707 else {
1708 ifp = ip_multicast_if(&mreq->imr_interface, NULL);
1709 if (ifp == NULL) {
1710 error = EADDRNOTAVAIL;
1711 break;
1712 }
1713 }
1714 /*
1715 * Find the membership in the membership array.
1716 */
1717 for (i = 0; i < imo->imo_num_memberships; ++i) {
1718 if ((ifp == NULL ||
1719 imo->imo_membership[i]->inm_ifp == ifp) &&
1720 in_hosteq(imo->imo_membership[i]->inm_addr,
1721 mreq->imr_multiaddr))
1722 break;
1723 }
1724 if (i == imo->imo_num_memberships) {
1725 error = EADDRNOTAVAIL;
1726 break;
1727 }
1728 /*
1729 * Give up the multicast address record to which the
1730 * membership points.
1731 */
1732 in_delmulti(imo->imo_membership[i]);
1733 /*
1734 * Remove the gap in the membership array.
1735 */
1736 for (++i; i < imo->imo_num_memberships; ++i)
1737 imo->imo_membership[i-1] = imo->imo_membership[i];
1738 --imo->imo_num_memberships;
1739 break;
1740
1741 default:
1742 error = EOPNOTSUPP;
1743 break;
1744 }
1745
1746 /*
1747 * If all options have default values, no need to keep the mbuf.
1748 */
1749 if (imo->imo_multicast_ifp == NULL &&
1750 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1751 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1752 imo->imo_num_memberships == 0) {
1753 free(*imop, M_IPMOPTS);
1754 *imop = NULL;
1755 }
1756
1757 return (error);
1758 }
1759
1760 /*
1761 * Return the IP multicast options in response to user getsockopt().
1762 */
1763 int
1764 ip_getmoptions(optname, imo, mp)
1765 int optname;
1766 struct ip_moptions *imo;
1767 struct mbuf **mp;
1768 {
1769 u_char *ttl;
1770 u_char *loop;
1771 struct in_addr *addr;
1772 struct in_ifaddr *ia;
1773
1774 *mp = m_get(M_WAIT, MT_SOOPTS);
1775
1776 switch (optname) {
1777
1778 case IP_MULTICAST_IF:
1779 addr = mtod(*mp, struct in_addr *);
1780 (*mp)->m_len = sizeof(struct in_addr);
1781 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1782 *addr = zeroin_addr;
1783 else if (imo->imo_multicast_addr.s_addr) {
1784 /* return the value user has set */
1785 *addr = imo->imo_multicast_addr;
1786 } else {
1787 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1788 *addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
1789 }
1790 return (0);
1791
1792 case IP_MULTICAST_TTL:
1793 ttl = mtod(*mp, u_char *);
1794 (*mp)->m_len = 1;
1795 *ttl = imo ? imo->imo_multicast_ttl
1796 : IP_DEFAULT_MULTICAST_TTL;
1797 return (0);
1798
1799 case IP_MULTICAST_LOOP:
1800 loop = mtod(*mp, u_char *);
1801 (*mp)->m_len = 1;
1802 *loop = imo ? imo->imo_multicast_loop
1803 : IP_DEFAULT_MULTICAST_LOOP;
1804 return (0);
1805
1806 default:
1807 return (EOPNOTSUPP);
1808 }
1809 }
1810
1811 /*
1812 * Discard the IP multicast options.
1813 */
1814 void
1815 ip_freemoptions(imo)
1816 struct ip_moptions *imo;
1817 {
1818 int i;
1819
1820 if (imo != NULL) {
1821 for (i = 0; i < imo->imo_num_memberships; ++i)
1822 in_delmulti(imo->imo_membership[i]);
1823 free(imo, M_IPMOPTS);
1824 }
1825 }
1826
1827 /*
1828 * Routine called from ip_output() to loop back a copy of an IP multicast
1829 * packet to the input queue of a specified interface. Note that this
1830 * calls the output routine of the loopback "driver", but with an interface
1831 * pointer that might NOT be &loif -- easier than replicating that code here.
1832 */
1833 static void
1834 ip_mloopback(ifp, m, dst)
1835 struct ifnet *ifp;
1836 struct mbuf *m;
1837 struct sockaddr_in *dst;
1838 {
1839 struct ip *ip;
1840 struct mbuf *copym;
1841
1842 copym = m_copy(m, 0, M_COPYALL);
1843 if (copym != NULL
1844 && (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
1845 copym = m_pullup(copym, sizeof(struct ip));
1846 if (copym != NULL) {
1847 /*
1848 * We don't bother to fragment if the IP length is greater
1849 * than the interface's MTU. Can this possibly matter?
1850 */
1851 ip = mtod(copym, struct ip *);
1852
1853 if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1854 in_delayed_cksum(copym);
1855 copym->m_pkthdr.csum_flags &=
1856 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
1857 }
1858
1859 ip->ip_sum = 0;
1860 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1861 (void) looutput(ifp, copym, sintosa(dst), NULL);
1862 }
1863 }
1864