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