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