ip_output.c revision 1.112 1 /* $NetBSD: ip_output.c,v 1.112 2003/08/19 00:54:41 christos 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.112 2003/08/19 00:54:41 christos 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 static int ip_fragment __P((struct mbuf *, struct ifnet *, u_long));
151
152 #ifdef PFIL_HOOKS
153 extern struct pfil_head inet_pfil_hook; /* XXX */
154 #endif
155
156 /*
157 * IP output. The packet in mbuf chain m contains a skeletal IP
158 * header (with len, off, ttl, proto, tos, src, dst).
159 * The mbuf chain containing the packet will be freed.
160 * The mbuf opt, if present, will not be freed.
161 */
162 int
163 #if __STDC__
164 ip_output(struct mbuf *m0, ...)
165 #else
166 ip_output(m0, va_alist)
167 struct mbuf *m0;
168 va_dcl
169 #endif
170 {
171 struct ip *ip;
172 struct ifnet *ifp;
173 struct mbuf *m = m0;
174 int hlen = sizeof (struct ip);
175 int len, error = 0;
176 struct route iproute;
177 struct sockaddr_in *dst;
178 struct in_ifaddr *ia;
179 struct mbuf *opt;
180 struct route *ro;
181 int flags, sw_csum;
182 int *mtu_p;
183 u_long mtu;
184 struct ip_moptions *imo;
185 struct inpcb *inp;
186 va_list ap;
187 #ifdef IPSEC
188 struct socket *so;
189 struct secpolicy *sp = NULL;
190 #endif /*IPSEC*/
191 #ifdef FAST_IPSEC
192 struct m_tag *mtag;
193 struct secpolicy *sp = NULL;
194 struct tdb_ident *tdbi;
195 int s;
196 #endif
197 u_int16_t ip_len;
198
199 len = 0;
200 va_start(ap, m0);
201 opt = va_arg(ap, struct mbuf *);
202 ro = va_arg(ap, struct route *);
203 flags = va_arg(ap, int);
204 imo = va_arg(ap, struct ip_moptions *);
205 inp = va_arg(ap, struct inpcb *);
206 if (flags & IP_RETURNMTU)
207 mtu_p = va_arg(ap, int *);
208 else
209 mtu_p = NULL;
210 va_end(ap);
211
212 MCLAIM(m, &ip_tx_mowner);
213 #ifdef IPSEC /* XXX so = ((inp == NULL) ? NULL : inp->inp_socket; */
214 so = ipsec_getsocket(m);
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 static 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 #ifdef __NetBSD__
1116 struct proc *p = curproc; /*XXX*/
1117 #endif
1118 #endif
1119
1120 if (level != IPPROTO_IP) {
1121 error = EINVAL;
1122 if (op == PRCO_SETOPT && *mp)
1123 (void) m_free(*mp);
1124 } else switch (op) {
1125
1126 case PRCO_SETOPT:
1127 switch (optname) {
1128 case IP_OPTIONS:
1129 #ifdef notyet
1130 case IP_RETOPTS:
1131 return (ip_pcbopts(optname, &inp->inp_options, m));
1132 #else
1133 return (ip_pcbopts(&inp->inp_options, m));
1134 #endif
1135
1136 case IP_TOS:
1137 case IP_TTL:
1138 case IP_RECVOPTS:
1139 case IP_RECVRETOPTS:
1140 case IP_RECVDSTADDR:
1141 case IP_RECVIF:
1142 if (m == NULL || m->m_len != sizeof(int))
1143 error = EINVAL;
1144 else {
1145 optval = *mtod(m, int *);
1146 switch (optname) {
1147
1148 case IP_TOS:
1149 inp->inp_ip.ip_tos = optval;
1150 break;
1151
1152 case IP_TTL:
1153 inp->inp_ip.ip_ttl = optval;
1154 break;
1155 #define OPTSET(bit) \
1156 if (optval) \
1157 inp->inp_flags |= bit; \
1158 else \
1159 inp->inp_flags &= ~bit;
1160
1161 case IP_RECVOPTS:
1162 OPTSET(INP_RECVOPTS);
1163 break;
1164
1165 case IP_RECVRETOPTS:
1166 OPTSET(INP_RECVRETOPTS);
1167 break;
1168
1169 case IP_RECVDSTADDR:
1170 OPTSET(INP_RECVDSTADDR);
1171 break;
1172
1173 case IP_RECVIF:
1174 OPTSET(INP_RECVIF);
1175 break;
1176 }
1177 }
1178 break;
1179 #undef OPTSET
1180
1181 case IP_MULTICAST_IF:
1182 case IP_MULTICAST_TTL:
1183 case IP_MULTICAST_LOOP:
1184 case IP_ADD_MEMBERSHIP:
1185 case IP_DROP_MEMBERSHIP:
1186 error = ip_setmoptions(optname, &inp->inp_moptions, m);
1187 break;
1188
1189 case IP_PORTRANGE:
1190 if (m == 0 || m->m_len != sizeof(int))
1191 error = EINVAL;
1192 else {
1193 optval = *mtod(m, int *);
1194
1195 switch (optval) {
1196
1197 case IP_PORTRANGE_DEFAULT:
1198 case IP_PORTRANGE_HIGH:
1199 inp->inp_flags &= ~(INP_LOWPORT);
1200 break;
1201
1202 case IP_PORTRANGE_LOW:
1203 inp->inp_flags |= INP_LOWPORT;
1204 break;
1205
1206 default:
1207 error = EINVAL;
1208 break;
1209 }
1210 }
1211 break;
1212
1213 #if defined(IPSEC) || defined(FAST_IPSEC)
1214 case IP_IPSEC_POLICY:
1215 {
1216 caddr_t req = NULL;
1217 size_t len = 0;
1218 int priv = 0;
1219
1220 #ifdef __NetBSD__
1221 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
1222 priv = 0;
1223 else
1224 priv = 1;
1225 #else
1226 priv = (in6p->in6p_socket->so_state & SS_PRIV);
1227 #endif
1228 if (m) {
1229 req = mtod(m, caddr_t);
1230 len = m->m_len;
1231 }
1232 error = ipsec4_set_policy(inp, optname, req, len, priv);
1233 break;
1234 }
1235 #endif /*IPSEC*/
1236
1237 default:
1238 error = ENOPROTOOPT;
1239 break;
1240 }
1241 if (m)
1242 (void)m_free(m);
1243 break;
1244
1245 case PRCO_GETOPT:
1246 switch (optname) {
1247 case IP_OPTIONS:
1248 case IP_RETOPTS:
1249 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1250 MCLAIM(m, so->so_mowner);
1251 if (inp->inp_options) {
1252 m->m_len = inp->inp_options->m_len;
1253 bcopy(mtod(inp->inp_options, caddr_t),
1254 mtod(m, caddr_t), (unsigned)m->m_len);
1255 } else
1256 m->m_len = 0;
1257 break;
1258
1259 case IP_TOS:
1260 case IP_TTL:
1261 case IP_RECVOPTS:
1262 case IP_RECVRETOPTS:
1263 case IP_RECVDSTADDR:
1264 case IP_RECVIF:
1265 case IP_ERRORMTU:
1266 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1267 MCLAIM(m, so->so_mowner);
1268 m->m_len = sizeof(int);
1269 switch (optname) {
1270
1271 case IP_TOS:
1272 optval = inp->inp_ip.ip_tos;
1273 break;
1274
1275 case IP_TTL:
1276 optval = inp->inp_ip.ip_ttl;
1277 break;
1278
1279 case IP_ERRORMTU:
1280 optval = inp->inp_errormtu;
1281 break;
1282
1283 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1284
1285 case IP_RECVOPTS:
1286 optval = OPTBIT(INP_RECVOPTS);
1287 break;
1288
1289 case IP_RECVRETOPTS:
1290 optval = OPTBIT(INP_RECVRETOPTS);
1291 break;
1292
1293 case IP_RECVDSTADDR:
1294 optval = OPTBIT(INP_RECVDSTADDR);
1295 break;
1296
1297 case IP_RECVIF:
1298 optval = OPTBIT(INP_RECVIF);
1299 break;
1300 }
1301 *mtod(m, int *) = optval;
1302 break;
1303
1304 #if defined(IPSEC) || defined(FAST_IPSEC)
1305 case IP_IPSEC_POLICY:
1306 {
1307 caddr_t req = NULL;
1308 size_t len = 0;
1309
1310 if (m) {
1311 req = mtod(m, caddr_t);
1312 len = m->m_len;
1313 }
1314 error = ipsec4_get_policy(inp, req, len, mp);
1315 break;
1316 }
1317 #endif /*IPSEC*/
1318
1319 case IP_MULTICAST_IF:
1320 case IP_MULTICAST_TTL:
1321 case IP_MULTICAST_LOOP:
1322 case IP_ADD_MEMBERSHIP:
1323 case IP_DROP_MEMBERSHIP:
1324 error = ip_getmoptions(optname, inp->inp_moptions, mp);
1325 if (*mp)
1326 MCLAIM(*mp, so->so_mowner);
1327 break;
1328
1329 case IP_PORTRANGE:
1330 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1331 MCLAIM(m, so->so_mowner);
1332 m->m_len = sizeof(int);
1333
1334 if (inp->inp_flags & INP_LOWPORT)
1335 optval = IP_PORTRANGE_LOW;
1336 else
1337 optval = IP_PORTRANGE_DEFAULT;
1338
1339 *mtod(m, int *) = optval;
1340 break;
1341
1342 default:
1343 error = ENOPROTOOPT;
1344 break;
1345 }
1346 break;
1347 }
1348 return (error);
1349 }
1350
1351 /*
1352 * Set up IP options in pcb for insertion in output packets.
1353 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1354 * with destination address if source routed.
1355 */
1356 int
1357 #ifdef notyet
1358 ip_pcbopts(optname, pcbopt, m)
1359 int optname;
1360 #else
1361 ip_pcbopts(pcbopt, m)
1362 #endif
1363 struct mbuf **pcbopt;
1364 struct mbuf *m;
1365 {
1366 int cnt, optlen;
1367 u_char *cp;
1368 u_char opt;
1369
1370 /* turn off any old options */
1371 if (*pcbopt)
1372 (void)m_free(*pcbopt);
1373 *pcbopt = 0;
1374 if (m == (struct mbuf *)0 || m->m_len == 0) {
1375 /*
1376 * Only turning off any previous options.
1377 */
1378 if (m)
1379 (void)m_free(m);
1380 return (0);
1381 }
1382
1383 #ifndef __vax__
1384 if (m->m_len % sizeof(int32_t))
1385 goto bad;
1386 #endif
1387 /*
1388 * IP first-hop destination address will be stored before
1389 * actual options; move other options back
1390 * and clear it when none present.
1391 */
1392 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1393 goto bad;
1394 cnt = m->m_len;
1395 m->m_len += sizeof(struct in_addr);
1396 cp = mtod(m, u_char *) + sizeof(struct in_addr);
1397 memmove(cp, mtod(m, caddr_t), (unsigned)cnt);
1398 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1399
1400 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1401 opt = cp[IPOPT_OPTVAL];
1402 if (opt == IPOPT_EOL)
1403 break;
1404 if (opt == IPOPT_NOP)
1405 optlen = 1;
1406 else {
1407 if (cnt < IPOPT_OLEN + sizeof(*cp))
1408 goto bad;
1409 optlen = cp[IPOPT_OLEN];
1410 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1411 goto bad;
1412 }
1413 switch (opt) {
1414
1415 default:
1416 break;
1417
1418 case IPOPT_LSRR:
1419 case IPOPT_SSRR:
1420 /*
1421 * user process specifies route as:
1422 * ->A->B->C->D
1423 * D must be our final destination (but we can't
1424 * check that since we may not have connected yet).
1425 * A is first hop destination, which doesn't appear in
1426 * actual IP option, but is stored before the options.
1427 */
1428 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1429 goto bad;
1430 m->m_len -= sizeof(struct in_addr);
1431 cnt -= sizeof(struct in_addr);
1432 optlen -= sizeof(struct in_addr);
1433 cp[IPOPT_OLEN] = optlen;
1434 /*
1435 * Move first hop before start of options.
1436 */
1437 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1438 sizeof(struct in_addr));
1439 /*
1440 * Then copy rest of options back
1441 * to close up the deleted entry.
1442 */
1443 memmove(&cp[IPOPT_OFFSET+1],
1444 (caddr_t)(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1445 (unsigned)cnt + sizeof(struct in_addr));
1446 break;
1447 }
1448 }
1449 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1450 goto bad;
1451 *pcbopt = m;
1452 return (0);
1453
1454 bad:
1455 (void)m_free(m);
1456 return (EINVAL);
1457 }
1458
1459 /*
1460 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1461 */
1462 static struct ifnet *
1463 ip_multicast_if(a, ifindexp)
1464 struct in_addr *a;
1465 int *ifindexp;
1466 {
1467 int ifindex;
1468 struct ifnet *ifp = NULL;
1469 struct in_ifaddr *ia;
1470
1471 if (ifindexp)
1472 *ifindexp = 0;
1473 if (ntohl(a->s_addr) >> 24 == 0) {
1474 ifindex = ntohl(a->s_addr) & 0xffffff;
1475 if (ifindex < 0 || if_index < ifindex)
1476 return NULL;
1477 ifp = ifindex2ifnet[ifindex];
1478 if (ifindexp)
1479 *ifindexp = ifindex;
1480 } else {
1481 LIST_FOREACH(ia, &IN_IFADDR_HASH(a->s_addr), ia_hash) {
1482 if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
1483 (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
1484 ifp = ia->ia_ifp;
1485 break;
1486 }
1487 }
1488 }
1489 return ifp;
1490 }
1491
1492 /*
1493 * Set the IP multicast options in response to user setsockopt().
1494 */
1495 int
1496 ip_setmoptions(optname, imop, m)
1497 int optname;
1498 struct ip_moptions **imop;
1499 struct mbuf *m;
1500 {
1501 int error = 0;
1502 u_char loop;
1503 int i;
1504 struct in_addr addr;
1505 struct ip_mreq *mreq;
1506 struct ifnet *ifp;
1507 struct ip_moptions *imo = *imop;
1508 struct route ro;
1509 struct sockaddr_in *dst;
1510 int ifindex;
1511
1512 if (imo == NULL) {
1513 /*
1514 * No multicast option buffer attached to the pcb;
1515 * allocate one and initialize to default values.
1516 */
1517 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
1518 M_WAITOK);
1519
1520 if (imo == NULL)
1521 return (ENOBUFS);
1522 *imop = imo;
1523 imo->imo_multicast_ifp = NULL;
1524 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1525 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1526 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1527 imo->imo_num_memberships = 0;
1528 }
1529
1530 switch (optname) {
1531
1532 case IP_MULTICAST_IF:
1533 /*
1534 * Select the interface for outgoing multicast packets.
1535 */
1536 if (m == NULL || m->m_len != sizeof(struct in_addr)) {
1537 error = EINVAL;
1538 break;
1539 }
1540 addr = *(mtod(m, struct in_addr *));
1541 /*
1542 * INADDR_ANY is used to remove a previous selection.
1543 * When no interface is selected, a default one is
1544 * chosen every time a multicast packet is sent.
1545 */
1546 if (in_nullhost(addr)) {
1547 imo->imo_multicast_ifp = NULL;
1548 break;
1549 }
1550 /*
1551 * The selected interface is identified by its local
1552 * IP address. Find the interface and confirm that
1553 * it supports multicasting.
1554 */
1555 ifp = ip_multicast_if(&addr, &ifindex);
1556 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1557 error = EADDRNOTAVAIL;
1558 break;
1559 }
1560 imo->imo_multicast_ifp = ifp;
1561 if (ifindex)
1562 imo->imo_multicast_addr = addr;
1563 else
1564 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1565 break;
1566
1567 case IP_MULTICAST_TTL:
1568 /*
1569 * Set the IP time-to-live for outgoing multicast packets.
1570 */
1571 if (m == NULL || m->m_len != 1) {
1572 error = EINVAL;
1573 break;
1574 }
1575 imo->imo_multicast_ttl = *(mtod(m, u_char *));
1576 break;
1577
1578 case IP_MULTICAST_LOOP:
1579 /*
1580 * Set the loopback flag for outgoing multicast packets.
1581 * Must be zero or one.
1582 */
1583 if (m == NULL || m->m_len != 1 ||
1584 (loop = *(mtod(m, u_char *))) > 1) {
1585 error = EINVAL;
1586 break;
1587 }
1588 imo->imo_multicast_loop = loop;
1589 break;
1590
1591 case IP_ADD_MEMBERSHIP:
1592 /*
1593 * Add a multicast group membership.
1594 * Group must be a valid IP multicast address.
1595 */
1596 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1597 error = EINVAL;
1598 break;
1599 }
1600 mreq = mtod(m, struct ip_mreq *);
1601 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1602 error = EINVAL;
1603 break;
1604 }
1605 /*
1606 * If no interface address was provided, use the interface of
1607 * the route to the given multicast address.
1608 */
1609 if (in_nullhost(mreq->imr_interface)) {
1610 bzero((caddr_t)&ro, sizeof(ro));
1611 ro.ro_rt = NULL;
1612 dst = satosin(&ro.ro_dst);
1613 dst->sin_len = sizeof(*dst);
1614 dst->sin_family = AF_INET;
1615 dst->sin_addr = mreq->imr_multiaddr;
1616 rtalloc(&ro);
1617 if (ro.ro_rt == NULL) {
1618 error = EADDRNOTAVAIL;
1619 break;
1620 }
1621 ifp = ro.ro_rt->rt_ifp;
1622 rtfree(ro.ro_rt);
1623 } else {
1624 ifp = ip_multicast_if(&mreq->imr_interface, NULL);
1625 }
1626 /*
1627 * See if we found an interface, and confirm that it
1628 * supports multicast.
1629 */
1630 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1631 error = EADDRNOTAVAIL;
1632 break;
1633 }
1634 /*
1635 * See if the membership already exists or if all the
1636 * membership slots are full.
1637 */
1638 for (i = 0; i < imo->imo_num_memberships; ++i) {
1639 if (imo->imo_membership[i]->inm_ifp == ifp &&
1640 in_hosteq(imo->imo_membership[i]->inm_addr,
1641 mreq->imr_multiaddr))
1642 break;
1643 }
1644 if (i < imo->imo_num_memberships) {
1645 error = EADDRINUSE;
1646 break;
1647 }
1648 if (i == IP_MAX_MEMBERSHIPS) {
1649 error = ETOOMANYREFS;
1650 break;
1651 }
1652 /*
1653 * Everything looks good; add a new record to the multicast
1654 * address list for the given interface.
1655 */
1656 if ((imo->imo_membership[i] =
1657 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1658 error = ENOBUFS;
1659 break;
1660 }
1661 ++imo->imo_num_memberships;
1662 break;
1663
1664 case IP_DROP_MEMBERSHIP:
1665 /*
1666 * Drop a multicast group membership.
1667 * Group must be a valid IP multicast address.
1668 */
1669 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1670 error = EINVAL;
1671 break;
1672 }
1673 mreq = mtod(m, struct ip_mreq *);
1674 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1675 error = EINVAL;
1676 break;
1677 }
1678 /*
1679 * If an interface address was specified, get a pointer
1680 * to its ifnet structure.
1681 */
1682 if (in_nullhost(mreq->imr_interface))
1683 ifp = NULL;
1684 else {
1685 ifp = ip_multicast_if(&mreq->imr_interface, NULL);
1686 if (ifp == NULL) {
1687 error = EADDRNOTAVAIL;
1688 break;
1689 }
1690 }
1691 /*
1692 * Find the membership in the membership array.
1693 */
1694 for (i = 0; i < imo->imo_num_memberships; ++i) {
1695 if ((ifp == NULL ||
1696 imo->imo_membership[i]->inm_ifp == ifp) &&
1697 in_hosteq(imo->imo_membership[i]->inm_addr,
1698 mreq->imr_multiaddr))
1699 break;
1700 }
1701 if (i == imo->imo_num_memberships) {
1702 error = EADDRNOTAVAIL;
1703 break;
1704 }
1705 /*
1706 * Give up the multicast address record to which the
1707 * membership points.
1708 */
1709 in_delmulti(imo->imo_membership[i]);
1710 /*
1711 * Remove the gap in the membership array.
1712 */
1713 for (++i; i < imo->imo_num_memberships; ++i)
1714 imo->imo_membership[i-1] = imo->imo_membership[i];
1715 --imo->imo_num_memberships;
1716 break;
1717
1718 default:
1719 error = EOPNOTSUPP;
1720 break;
1721 }
1722
1723 /*
1724 * If all options have default values, no need to keep the mbuf.
1725 */
1726 if (imo->imo_multicast_ifp == NULL &&
1727 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1728 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1729 imo->imo_num_memberships == 0) {
1730 free(*imop, M_IPMOPTS);
1731 *imop = NULL;
1732 }
1733
1734 return (error);
1735 }
1736
1737 /*
1738 * Return the IP multicast options in response to user getsockopt().
1739 */
1740 int
1741 ip_getmoptions(optname, imo, mp)
1742 int optname;
1743 struct ip_moptions *imo;
1744 struct mbuf **mp;
1745 {
1746 u_char *ttl;
1747 u_char *loop;
1748 struct in_addr *addr;
1749 struct in_ifaddr *ia;
1750
1751 *mp = m_get(M_WAIT, MT_SOOPTS);
1752
1753 switch (optname) {
1754
1755 case IP_MULTICAST_IF:
1756 addr = mtod(*mp, struct in_addr *);
1757 (*mp)->m_len = sizeof(struct in_addr);
1758 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1759 *addr = zeroin_addr;
1760 else if (imo->imo_multicast_addr.s_addr) {
1761 /* return the value user has set */
1762 *addr = imo->imo_multicast_addr;
1763 } else {
1764 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1765 *addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
1766 }
1767 return (0);
1768
1769 case IP_MULTICAST_TTL:
1770 ttl = mtod(*mp, u_char *);
1771 (*mp)->m_len = 1;
1772 *ttl = imo ? imo->imo_multicast_ttl
1773 : IP_DEFAULT_MULTICAST_TTL;
1774 return (0);
1775
1776 case IP_MULTICAST_LOOP:
1777 loop = mtod(*mp, u_char *);
1778 (*mp)->m_len = 1;
1779 *loop = imo ? imo->imo_multicast_loop
1780 : IP_DEFAULT_MULTICAST_LOOP;
1781 return (0);
1782
1783 default:
1784 return (EOPNOTSUPP);
1785 }
1786 }
1787
1788 /*
1789 * Discard the IP multicast options.
1790 */
1791 void
1792 ip_freemoptions(imo)
1793 struct ip_moptions *imo;
1794 {
1795 int i;
1796
1797 if (imo != NULL) {
1798 for (i = 0; i < imo->imo_num_memberships; ++i)
1799 in_delmulti(imo->imo_membership[i]);
1800 free(imo, M_IPMOPTS);
1801 }
1802 }
1803
1804 /*
1805 * Routine called from ip_output() to loop back a copy of an IP multicast
1806 * packet to the input queue of a specified interface. Note that this
1807 * calls the output routine of the loopback "driver", but with an interface
1808 * pointer that might NOT be &loif -- easier than replicating that code here.
1809 */
1810 static void
1811 ip_mloopback(ifp, m, dst)
1812 struct ifnet *ifp;
1813 struct mbuf *m;
1814 struct sockaddr_in *dst;
1815 {
1816 struct ip *ip;
1817 struct mbuf *copym;
1818
1819 copym = m_copy(m, 0, M_COPYALL);
1820 if (copym != NULL
1821 && (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
1822 copym = m_pullup(copym, sizeof(struct ip));
1823 if (copym != NULL) {
1824 /*
1825 * We don't bother to fragment if the IP length is greater
1826 * than the interface's MTU. Can this possibly matter?
1827 */
1828 ip = mtod(copym, struct ip *);
1829
1830 if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1831 in_delayed_cksum(copym);
1832 copym->m_pkthdr.csum_flags &=
1833 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
1834 }
1835
1836 ip->ip_sum = 0;
1837 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1838 (void) looutput(ifp, copym, sintosa(dst), NULL);
1839 }
1840 }
1841