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