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