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