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