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