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