ip_output.c revision 1.76 1 /* $NetBSD: ip_output.c,v 1.76 2000/10/17 02:57:01 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 #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 /*
419 * If we're doing Path MTU Discovery, we need to set DF unless
420 * the route's MTU is locked.
421 */
422 if ((flags & IP_MTUDISC) != 0 && ro->ro_rt != NULL &&
423 (ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
424 ip->ip_off |= IP_DF;
425
426 #ifdef PFIL_HOOKS
427 /*
428 * Run through list of hooks for output packets.
429 */
430 m1 = m;
431 pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
432 for (; pfh; pfh = pfh->pfil_link.tqe_next)
433 if (pfh->pfil_func) {
434 rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1);
435 if (rv) {
436 error = EHOSTUNREACH;
437 goto done;
438 }
439 m = m1;
440 if (m == NULL)
441 goto done;
442 ip = mtod(m, struct ip *);
443 }
444 #endif /* PFIL_HOOKS */
445
446 #ifdef IPSEC
447 /* get SP for this packet */
448 if (so == NULL)
449 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
450 else
451 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
452
453 if (sp == NULL) {
454 ipsecstat.out_inval++;
455 goto bad;
456 }
457
458 error = 0;
459
460 /* check policy */
461 switch (sp->policy) {
462 case IPSEC_POLICY_DISCARD:
463 /*
464 * This packet is just discarded.
465 */
466 ipsecstat.out_polvio++;
467 goto bad;
468
469 case IPSEC_POLICY_BYPASS:
470 case IPSEC_POLICY_NONE:
471 /* no need to do IPsec. */
472 goto skip_ipsec;
473
474 case IPSEC_POLICY_IPSEC:
475 if (sp->req == NULL) {
476 /* XXX should be panic ? */
477 printf("ip_output: No IPsec request specified.\n");
478 error = EINVAL;
479 goto bad;
480 }
481 break;
482
483 case IPSEC_POLICY_ENTRUST:
484 default:
485 printf("ip_output: Invalid policy found. %d\n", sp->policy);
486 }
487
488 ip->ip_len = htons((u_short)ip->ip_len);
489 ip->ip_off = htons((u_short)ip->ip_off);
490 ip->ip_sum = 0;
491
492 {
493 struct ipsec_output_state state;
494 bzero(&state, sizeof(state));
495 state.m = m;
496 if (flags & IP_ROUTETOIF) {
497 state.ro = &iproute;
498 bzero(&iproute, sizeof(iproute));
499 } else
500 state.ro = ro;
501 state.dst = (struct sockaddr *)dst;
502
503 error = ipsec4_output(&state, sp, flags);
504
505 m = state.m;
506 if (flags & IP_ROUTETOIF) {
507 /*
508 * if we have tunnel mode SA, we may need to ignore
509 * IP_ROUTETOIF.
510 */
511 if (state.ro != &iproute || state.ro->ro_rt != NULL) {
512 flags &= ~IP_ROUTETOIF;
513 ro = state.ro;
514 }
515 } else
516 ro = state.ro;
517 dst = (struct sockaddr_in *)state.dst;
518 if (error) {
519 /* mbuf is already reclaimed in ipsec4_output. */
520 m0 = NULL;
521 switch (error) {
522 case EHOSTUNREACH:
523 case ENETUNREACH:
524 case EMSGSIZE:
525 case ENOBUFS:
526 case ENOMEM:
527 break;
528 default:
529 printf("ip4_output (ipsec): error code %d\n", error);
530 /*fall through*/
531 case ENOENT:
532 /* don't show these error codes to the user */
533 error = 0;
534 break;
535 }
536 goto bad;
537 }
538 }
539
540 /* be sure to update variables that are affected by ipsec4_output() */
541 ip = mtod(m, struct ip *);
542 #ifdef _IP_VHL
543 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
544 #else
545 hlen = ip->ip_hl << 2;
546 #endif
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 /* make it flipped, again. */
560 ip->ip_len = ntohs((u_short)ip->ip_len);
561 ip->ip_off = ntohs((u_short)ip->ip_off);
562 skip_ipsec:
563 #endif /*IPSEC*/
564
565 /*
566 * If small enough for mtu of path, can just send directly.
567 */
568 if ((u_int16_t)ip->ip_len <= mtu) {
569 #if IFA_STATS
570 /*
571 * search for the source address structure to
572 * maintain output statistics.
573 */
574 bzero((caddr_t*) &src, sizeof(src));
575 src.sin_family = AF_INET;
576 src.sin_addr.s_addr = ip->ip_src.s_addr;
577 src.sin_len = sizeof(src);
578 ia = ifatoia(ifa_ifwithladdr(sintosa(&src)));
579 if (ia)
580 ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
581 #endif
582 HTONS(ip->ip_len);
583 HTONS(ip->ip_off);
584 ip->ip_sum = 0;
585 ip->ip_sum = in_cksum(m, hlen);
586 error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt);
587 goto done;
588 }
589
590 /*
591 * Too large for interface; fragment if possible.
592 * Must be able to put at least 8 bytes per fragment.
593 */
594 #if 0
595 /*
596 * If IPsec packet is too big for the interface, try fragment it.
597 * XXX This really is a quickhack. May be inappropriate.
598 * XXX fails if somebody is sending AH'ed packet, with:
599 * sizeof(packet without AH) < mtu < sizeof(packet with AH)
600 */
601 if (sab && ip->ip_p != IPPROTO_AH && (flags & IP_FORWARDING) == 0)
602 ip->ip_off &= ~IP_DF;
603 #endif /*IPSEC*/
604 if (ip->ip_off & IP_DF) {
605 if (flags & IP_RETURNMTU)
606 *mtu_p = mtu;
607 error = EMSGSIZE;
608 ipstat.ips_cantfrag++;
609 goto bad;
610 }
611 len = (mtu - hlen) &~ 7;
612 if (len < 8) {
613 error = EMSGSIZE;
614 goto bad;
615 }
616
617 {
618 int mhlen, firstlen = len;
619 struct mbuf **mnext = &m->m_nextpkt;
620 int fragments = 0;
621 int s;
622
623 /*
624 * Loop through length of segment after first fragment,
625 * make new header and copy data of each part and link onto chain.
626 */
627 m0 = m;
628 mhlen = sizeof (struct ip);
629 for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len) {
630 MGETHDR(m, M_DONTWAIT, MT_HEADER);
631 if (m == 0) {
632 error = ENOBUFS;
633 ipstat.ips_odropped++;
634 goto sendorfree;
635 }
636 *mnext = m;
637 mnext = &m->m_nextpkt;
638 m->m_data += max_linkhdr;
639 mhip = mtod(m, struct ip *);
640 *mhip = *ip;
641 /* we must inherit MCAST and BCAST flags */
642 m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
643 if (hlen > sizeof (struct ip)) {
644 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
645 mhip->ip_hl = mhlen >> 2;
646 }
647 m->m_len = mhlen;
648 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
649 if (ip->ip_off & IP_MF)
650 mhip->ip_off |= IP_MF;
651 if (off + len >= (u_int16_t)ip->ip_len)
652 len = (u_int16_t)ip->ip_len - off;
653 else
654 mhip->ip_off |= IP_MF;
655 mhip->ip_len = htons((u_int16_t)(len + mhlen));
656 m->m_next = m_copy(m0, off, len);
657 if (m->m_next == 0) {
658 error = ENOBUFS; /* ??? */
659 ipstat.ips_odropped++;
660 goto sendorfree;
661 }
662 m->m_pkthdr.len = mhlen + len;
663 m->m_pkthdr.rcvif = (struct ifnet *)0;
664 HTONS(mhip->ip_off);
665 mhip->ip_sum = 0;
666 mhip->ip_sum = in_cksum(m, mhlen);
667 ipstat.ips_ofragments++;
668 fragments++;
669 }
670 /*
671 * Update first fragment by trimming what's been copied out
672 * and updating header, then send each fragment (in order).
673 */
674 m = m0;
675 m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
676 m->m_pkthdr.len = hlen + firstlen;
677 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
678 ip->ip_off |= IP_MF;
679 HTONS(ip->ip_off);
680 ip->ip_sum = 0;
681 ip->ip_sum = in_cksum(m, hlen);
682 sendorfree:
683 /*
684 * If there is no room for all the fragments, don't queue
685 * any of them.
686 */
687 s = splimp();
688 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments)
689 error = ENOBUFS;
690 splx(s);
691 for (m = m0; m; m = m0) {
692 m0 = m->m_nextpkt;
693 m->m_nextpkt = 0;
694 if (error == 0) {
695 #if IFA_STATS
696 /*
697 * search for the source address structure to
698 * maintain output statistics.
699 */
700 bzero((caddr_t*) &src, sizeof(src));
701 src.sin_family = AF_INET;
702 src.sin_addr.s_addr = ip->ip_src.s_addr;
703 src.sin_len = sizeof(src);
704 ia = ifatoia(ifa_ifwithladdr(sintosa(&src)));
705 if (ia) {
706 ia->ia_ifa.ifa_data.ifad_outbytes +=
707 ntohs(ip->ip_len);
708 }
709 #endif
710 error = (*ifp->if_output)(ifp, m, sintosa(dst),
711 ro->ro_rt);
712 } else
713 m_freem(m);
714 }
715
716 if (error == 0)
717 ipstat.ips_fragmented++;
718 }
719 done:
720 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
721 RTFREE(ro->ro_rt);
722 ro->ro_rt = 0;
723 }
724
725 #ifdef IPSEC
726 if (sp != NULL) {
727 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
728 printf("DP ip_output call free SP:%p\n", sp));
729 key_freesp(sp);
730 }
731 #endif /* IPSEC */
732
733 return (error);
734 bad:
735 m_freem(m);
736 goto done;
737 }
738
739 /*
740 * Determine the maximum length of the options to be inserted;
741 * we would far rather allocate too much space rather than too little.
742 */
743
744 u_int
745 ip_optlen(inp)
746 struct inpcb *inp;
747 {
748 struct mbuf *m = inp->inp_options;
749
750 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
751 return(m->m_len - offsetof(struct ipoption, ipopt_dst));
752 else
753 return 0;
754 }
755
756
757 /*
758 * Insert IP options into preformed packet.
759 * Adjust IP destination as required for IP source routing,
760 * as indicated by a non-zero in_addr at the start of the options.
761 */
762 static struct mbuf *
763 ip_insertoptions(m, opt, phlen)
764 struct mbuf *m;
765 struct mbuf *opt;
766 int *phlen;
767 {
768 struct ipoption *p = mtod(opt, struct ipoption *);
769 struct mbuf *n;
770 struct ip *ip = mtod(m, struct ip *);
771 unsigned optlen;
772
773 optlen = opt->m_len - sizeof(p->ipopt_dst);
774 if (optlen + (u_int16_t)ip->ip_len > IP_MAXPACKET)
775 return (m); /* XXX should fail */
776 if (!in_nullhost(p->ipopt_dst))
777 ip->ip_dst = p->ipopt_dst;
778 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
779 MGETHDR(n, M_DONTWAIT, MT_HEADER);
780 if (n == 0)
781 return (m);
782 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
783 m->m_len -= sizeof(struct ip);
784 m->m_data += sizeof(struct ip);
785 n->m_next = m;
786 m = n;
787 m->m_len = optlen + sizeof(struct ip);
788 m->m_data += max_linkhdr;
789 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
790 } else {
791 m->m_data -= optlen;
792 m->m_len += optlen;
793 m->m_pkthdr.len += optlen;
794 memmove(mtod(m, caddr_t), ip, sizeof(struct ip));
795 }
796 ip = mtod(m, struct ip *);
797 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
798 *phlen = sizeof(struct ip) + optlen;
799 ip->ip_len += optlen;
800 return (m);
801 }
802
803 /*
804 * Copy options from ip to jp,
805 * omitting those not copied during fragmentation.
806 */
807 int
808 ip_optcopy(ip, jp)
809 struct ip *ip, *jp;
810 {
811 u_char *cp, *dp;
812 int opt, optlen, cnt;
813
814 cp = (u_char *)(ip + 1);
815 dp = (u_char *)(jp + 1);
816 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
817 for (; cnt > 0; cnt -= optlen, cp += optlen) {
818 opt = cp[0];
819 if (opt == IPOPT_EOL)
820 break;
821 if (opt == IPOPT_NOP) {
822 /* Preserve for IP mcast tunnel's LSRR alignment. */
823 *dp++ = IPOPT_NOP;
824 optlen = 1;
825 continue;
826 }
827 #ifdef DIAGNOSTIC
828 if (cnt < IPOPT_OLEN + sizeof(*cp))
829 panic("malformed IPv4 option passed to ip_optcopy");
830 #endif
831 optlen = cp[IPOPT_OLEN];
832 #ifdef DIAGNOSTIC
833 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
834 panic("malformed IPv4 option passed to ip_optcopy");
835 #endif
836 /* bogus lengths should have been caught by ip_dooptions */
837 if (optlen > cnt)
838 optlen = cnt;
839 if (IPOPT_COPIED(opt)) {
840 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
841 dp += optlen;
842 }
843 }
844 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
845 *dp++ = IPOPT_EOL;
846 return (optlen);
847 }
848
849 /*
850 * IP socket option processing.
851 */
852 int
853 ip_ctloutput(op, so, level, optname, mp)
854 int op;
855 struct socket *so;
856 int level, optname;
857 struct mbuf **mp;
858 {
859 struct inpcb *inp = sotoinpcb(so);
860 struct mbuf *m = *mp;
861 int optval = 0;
862 int error = 0;
863 #ifdef IPSEC
864 #ifdef __NetBSD__
865 struct proc *p = curproc; /*XXX*/
866 #endif
867 #endif
868
869 if (level != IPPROTO_IP) {
870 error = EINVAL;
871 if (op == PRCO_SETOPT && *mp)
872 (void) m_free(*mp);
873 } else switch (op) {
874
875 case PRCO_SETOPT:
876 switch (optname) {
877 case IP_OPTIONS:
878 #ifdef notyet
879 case IP_RETOPTS:
880 return (ip_pcbopts(optname, &inp->inp_options, m));
881 #else
882 return (ip_pcbopts(&inp->inp_options, m));
883 #endif
884
885 case IP_TOS:
886 case IP_TTL:
887 case IP_RECVOPTS:
888 case IP_RECVRETOPTS:
889 case IP_RECVDSTADDR:
890 case IP_RECVIF:
891 if (m == NULL || m->m_len != sizeof(int))
892 error = EINVAL;
893 else {
894 optval = *mtod(m, int *);
895 switch (optname) {
896
897 case IP_TOS:
898 inp->inp_ip.ip_tos = optval;
899 break;
900
901 case IP_TTL:
902 inp->inp_ip.ip_ttl = optval;
903 break;
904 #define OPTSET(bit) \
905 if (optval) \
906 inp->inp_flags |= bit; \
907 else \
908 inp->inp_flags &= ~bit;
909
910 case IP_RECVOPTS:
911 OPTSET(INP_RECVOPTS);
912 break;
913
914 case IP_RECVRETOPTS:
915 OPTSET(INP_RECVRETOPTS);
916 break;
917
918 case IP_RECVDSTADDR:
919 OPTSET(INP_RECVDSTADDR);
920 break;
921
922 case IP_RECVIF:
923 OPTSET(INP_RECVIF);
924 break;
925 }
926 }
927 break;
928 #undef OPTSET
929
930 case IP_MULTICAST_IF:
931 case IP_MULTICAST_TTL:
932 case IP_MULTICAST_LOOP:
933 case IP_ADD_MEMBERSHIP:
934 case IP_DROP_MEMBERSHIP:
935 error = ip_setmoptions(optname, &inp->inp_moptions, m);
936 break;
937
938 case IP_PORTRANGE:
939 if (m == 0 || m->m_len != sizeof(int))
940 error = EINVAL;
941 else {
942 optval = *mtod(m, int *);
943
944 switch (optval) {
945
946 case IP_PORTRANGE_DEFAULT:
947 case IP_PORTRANGE_HIGH:
948 inp->inp_flags &= ~(INP_LOWPORT);
949 break;
950
951 case IP_PORTRANGE_LOW:
952 inp->inp_flags |= INP_LOWPORT;
953 break;
954
955 default:
956 error = EINVAL;
957 break;
958 }
959 }
960 break;
961
962 #ifdef IPSEC
963 case IP_IPSEC_POLICY:
964 {
965 caddr_t req = NULL;
966 size_t len = 0;
967 int priv = 0;
968
969 #ifdef __NetBSD__
970 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
971 priv = 0;
972 else
973 priv = 1;
974 #else
975 priv = (in6p->in6p_socket->so_state & SS_PRIV);
976 #endif
977 if (m) {
978 req = mtod(m, caddr_t);
979 len = m->m_len;
980 }
981 error = ipsec4_set_policy(inp, optname, req, len, priv);
982 break;
983 }
984 #endif /*IPSEC*/
985
986 default:
987 error = ENOPROTOOPT;
988 break;
989 }
990 if (m)
991 (void)m_free(m);
992 break;
993
994 case PRCO_GETOPT:
995 switch (optname) {
996 case IP_OPTIONS:
997 case IP_RETOPTS:
998 *mp = m = m_get(M_WAIT, MT_SOOPTS);
999 if (inp->inp_options) {
1000 m->m_len = inp->inp_options->m_len;
1001 bcopy(mtod(inp->inp_options, caddr_t),
1002 mtod(m, caddr_t), (unsigned)m->m_len);
1003 } else
1004 m->m_len = 0;
1005 break;
1006
1007 case IP_TOS:
1008 case IP_TTL:
1009 case IP_RECVOPTS:
1010 case IP_RECVRETOPTS:
1011 case IP_RECVDSTADDR:
1012 case IP_RECVIF:
1013 case IP_ERRORMTU:
1014 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1015 m->m_len = sizeof(int);
1016 switch (optname) {
1017
1018 case IP_TOS:
1019 optval = inp->inp_ip.ip_tos;
1020 break;
1021
1022 case IP_TTL:
1023 optval = inp->inp_ip.ip_ttl;
1024 break;
1025
1026 case IP_ERRORMTU:
1027 optval = inp->inp_errormtu;
1028 break;
1029
1030 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1031
1032 case IP_RECVOPTS:
1033 optval = OPTBIT(INP_RECVOPTS);
1034 break;
1035
1036 case IP_RECVRETOPTS:
1037 optval = OPTBIT(INP_RECVRETOPTS);
1038 break;
1039
1040 case IP_RECVDSTADDR:
1041 optval = OPTBIT(INP_RECVDSTADDR);
1042 break;
1043
1044 case IP_RECVIF:
1045 optval = OPTBIT(INP_RECVIF);
1046 break;
1047 }
1048 *mtod(m, int *) = optval;
1049 break;
1050
1051 #ifdef IPSEC
1052 case IP_IPSEC_POLICY:
1053 {
1054 caddr_t req = NULL;
1055 size_t len;
1056
1057 if (m) {
1058 req = mtod(m, caddr_t);
1059 len = m->m_len;
1060 }
1061 error = ipsec4_get_policy(inp, req, len, mp);
1062 break;
1063 }
1064 #endif /*IPSEC*/
1065
1066 case IP_MULTICAST_IF:
1067 case IP_MULTICAST_TTL:
1068 case IP_MULTICAST_LOOP:
1069 case IP_ADD_MEMBERSHIP:
1070 case IP_DROP_MEMBERSHIP:
1071 error = ip_getmoptions(optname, inp->inp_moptions, mp);
1072 break;
1073
1074 case IP_PORTRANGE:
1075 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1076 m->m_len = sizeof(int);
1077
1078 if (inp->inp_flags & INP_LOWPORT)
1079 optval = IP_PORTRANGE_LOW;
1080 else
1081 optval = IP_PORTRANGE_DEFAULT;
1082
1083 *mtod(m, int *) = optval;
1084 break;
1085
1086 default:
1087 error = ENOPROTOOPT;
1088 break;
1089 }
1090 break;
1091 }
1092 return (error);
1093 }
1094
1095 /*
1096 * Set up IP options in pcb for insertion in output packets.
1097 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1098 * with destination address if source routed.
1099 */
1100 int
1101 #ifdef notyet
1102 ip_pcbopts(optname, pcbopt, m)
1103 int optname;
1104 #else
1105 ip_pcbopts(pcbopt, m)
1106 #endif
1107 struct mbuf **pcbopt;
1108 struct mbuf *m;
1109 {
1110 int cnt, optlen;
1111 u_char *cp;
1112 u_char opt;
1113
1114 /* turn off any old options */
1115 if (*pcbopt)
1116 (void)m_free(*pcbopt);
1117 *pcbopt = 0;
1118 if (m == (struct mbuf *)0 || m->m_len == 0) {
1119 /*
1120 * Only turning off any previous options.
1121 */
1122 if (m)
1123 (void)m_free(m);
1124 return (0);
1125 }
1126
1127 #ifndef vax
1128 if (m->m_len % sizeof(int32_t))
1129 goto bad;
1130 #endif
1131 /*
1132 * IP first-hop destination address will be stored before
1133 * actual options; move other options back
1134 * and clear it when none present.
1135 */
1136 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1137 goto bad;
1138 cnt = m->m_len;
1139 m->m_len += sizeof(struct in_addr);
1140 cp = mtod(m, u_char *) + sizeof(struct in_addr);
1141 memmove(cp, mtod(m, caddr_t), (unsigned)cnt);
1142 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1143
1144 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1145 opt = cp[IPOPT_OPTVAL];
1146 if (opt == IPOPT_EOL)
1147 break;
1148 if (opt == IPOPT_NOP)
1149 optlen = 1;
1150 else {
1151 if (cnt < IPOPT_OLEN + sizeof(*cp))
1152 goto bad;
1153 optlen = cp[IPOPT_OLEN];
1154 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1155 goto bad;
1156 }
1157 switch (opt) {
1158
1159 default:
1160 break;
1161
1162 case IPOPT_LSRR:
1163 case IPOPT_SSRR:
1164 /*
1165 * user process specifies route as:
1166 * ->A->B->C->D
1167 * D must be our final destination (but we can't
1168 * check that since we may not have connected yet).
1169 * A is first hop destination, which doesn't appear in
1170 * actual IP option, but is stored before the options.
1171 */
1172 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1173 goto bad;
1174 m->m_len -= sizeof(struct in_addr);
1175 cnt -= sizeof(struct in_addr);
1176 optlen -= sizeof(struct in_addr);
1177 cp[IPOPT_OLEN] = optlen;
1178 /*
1179 * Move first hop before start of options.
1180 */
1181 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1182 sizeof(struct in_addr));
1183 /*
1184 * Then copy rest of options back
1185 * to close up the deleted entry.
1186 */
1187 memmove(&cp[IPOPT_OFFSET+1],
1188 (caddr_t)(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1189 (unsigned)cnt + sizeof(struct in_addr));
1190 break;
1191 }
1192 }
1193 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1194 goto bad;
1195 *pcbopt = m;
1196 return (0);
1197
1198 bad:
1199 (void)m_free(m);
1200 return (EINVAL);
1201 }
1202
1203 /*
1204 * Set the IP multicast options in response to user setsockopt().
1205 */
1206 int
1207 ip_setmoptions(optname, imop, m)
1208 int optname;
1209 struct ip_moptions **imop;
1210 struct mbuf *m;
1211 {
1212 int error = 0;
1213 u_char loop;
1214 int i;
1215 struct in_addr addr;
1216 struct ip_mreq *mreq;
1217 struct ifnet *ifp;
1218 struct ip_moptions *imo = *imop;
1219 struct route ro;
1220 struct sockaddr_in *dst;
1221
1222 if (imo == NULL) {
1223 /*
1224 * No multicast option buffer attached to the pcb;
1225 * allocate one and initialize to default values.
1226 */
1227 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
1228 M_WAITOK);
1229
1230 if (imo == NULL)
1231 return (ENOBUFS);
1232 *imop = imo;
1233 imo->imo_multicast_ifp = NULL;
1234 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1235 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1236 imo->imo_num_memberships = 0;
1237 }
1238
1239 switch (optname) {
1240
1241 case IP_MULTICAST_IF:
1242 /*
1243 * Select the interface for outgoing multicast packets.
1244 */
1245 if (m == NULL || m->m_len != sizeof(struct in_addr)) {
1246 error = EINVAL;
1247 break;
1248 }
1249 addr = *(mtod(m, struct in_addr *));
1250 /*
1251 * INADDR_ANY is used to remove a previous selection.
1252 * When no interface is selected, a default one is
1253 * chosen every time a multicast packet is sent.
1254 */
1255 if (in_nullhost(addr)) {
1256 imo->imo_multicast_ifp = NULL;
1257 break;
1258 }
1259 /*
1260 * The selected interface is identified by its local
1261 * IP address. Find the interface and confirm that
1262 * it supports multicasting.
1263 */
1264 INADDR_TO_IFP(addr, ifp);
1265 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1266 error = EADDRNOTAVAIL;
1267 break;
1268 }
1269 imo->imo_multicast_ifp = ifp;
1270 break;
1271
1272 case IP_MULTICAST_TTL:
1273 /*
1274 * Set the IP time-to-live for outgoing multicast packets.
1275 */
1276 if (m == NULL || m->m_len != 1) {
1277 error = EINVAL;
1278 break;
1279 }
1280 imo->imo_multicast_ttl = *(mtod(m, u_char *));
1281 break;
1282
1283 case IP_MULTICAST_LOOP:
1284 /*
1285 * Set the loopback flag for outgoing multicast packets.
1286 * Must be zero or one.
1287 */
1288 if (m == NULL || m->m_len != 1 ||
1289 (loop = *(mtod(m, u_char *))) > 1) {
1290 error = EINVAL;
1291 break;
1292 }
1293 imo->imo_multicast_loop = loop;
1294 break;
1295
1296 case IP_ADD_MEMBERSHIP:
1297 /*
1298 * Add a multicast group membership.
1299 * Group must be a valid IP multicast address.
1300 */
1301 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1302 error = EINVAL;
1303 break;
1304 }
1305 mreq = mtod(m, struct ip_mreq *);
1306 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1307 error = EINVAL;
1308 break;
1309 }
1310 /*
1311 * If no interface address was provided, use the interface of
1312 * the route to the given multicast address.
1313 */
1314 if (in_nullhost(mreq->imr_interface)) {
1315 bzero((caddr_t)&ro, sizeof(ro));
1316 ro.ro_rt = NULL;
1317 dst = satosin(&ro.ro_dst);
1318 dst->sin_len = sizeof(*dst);
1319 dst->sin_family = AF_INET;
1320 dst->sin_addr = mreq->imr_multiaddr;
1321 rtalloc(&ro);
1322 if (ro.ro_rt == NULL) {
1323 error = EADDRNOTAVAIL;
1324 break;
1325 }
1326 ifp = ro.ro_rt->rt_ifp;
1327 rtfree(ro.ro_rt);
1328 } else {
1329 INADDR_TO_IFP(mreq->imr_interface, ifp);
1330 }
1331 /*
1332 * See if we found an interface, and confirm that it
1333 * supports multicast.
1334 */
1335 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1336 error = EADDRNOTAVAIL;
1337 break;
1338 }
1339 /*
1340 * See if the membership already exists or if all the
1341 * membership slots are full.
1342 */
1343 for (i = 0; i < imo->imo_num_memberships; ++i) {
1344 if (imo->imo_membership[i]->inm_ifp == ifp &&
1345 in_hosteq(imo->imo_membership[i]->inm_addr,
1346 mreq->imr_multiaddr))
1347 break;
1348 }
1349 if (i < imo->imo_num_memberships) {
1350 error = EADDRINUSE;
1351 break;
1352 }
1353 if (i == IP_MAX_MEMBERSHIPS) {
1354 error = ETOOMANYREFS;
1355 break;
1356 }
1357 /*
1358 * Everything looks good; add a new record to the multicast
1359 * address list for the given interface.
1360 */
1361 if ((imo->imo_membership[i] =
1362 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1363 error = ENOBUFS;
1364 break;
1365 }
1366 ++imo->imo_num_memberships;
1367 break;
1368
1369 case IP_DROP_MEMBERSHIP:
1370 /*
1371 * Drop a multicast group membership.
1372 * Group must be a valid IP multicast address.
1373 */
1374 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1375 error = EINVAL;
1376 break;
1377 }
1378 mreq = mtod(m, struct ip_mreq *);
1379 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1380 error = EINVAL;
1381 break;
1382 }
1383 /*
1384 * If an interface address was specified, get a pointer
1385 * to its ifnet structure.
1386 */
1387 if (in_nullhost(mreq->imr_interface))
1388 ifp = NULL;
1389 else {
1390 INADDR_TO_IFP(mreq->imr_interface, ifp);
1391 if (ifp == NULL) {
1392 error = EADDRNOTAVAIL;
1393 break;
1394 }
1395 }
1396 /*
1397 * Find the membership in the membership array.
1398 */
1399 for (i = 0; i < imo->imo_num_memberships; ++i) {
1400 if ((ifp == NULL ||
1401 imo->imo_membership[i]->inm_ifp == ifp) &&
1402 in_hosteq(imo->imo_membership[i]->inm_addr,
1403 mreq->imr_multiaddr))
1404 break;
1405 }
1406 if (i == imo->imo_num_memberships) {
1407 error = EADDRNOTAVAIL;
1408 break;
1409 }
1410 /*
1411 * Give up the multicast address record to which the
1412 * membership points.
1413 */
1414 in_delmulti(imo->imo_membership[i]);
1415 /*
1416 * Remove the gap in the membership array.
1417 */
1418 for (++i; i < imo->imo_num_memberships; ++i)
1419 imo->imo_membership[i-1] = imo->imo_membership[i];
1420 --imo->imo_num_memberships;
1421 break;
1422
1423 default:
1424 error = EOPNOTSUPP;
1425 break;
1426 }
1427
1428 /*
1429 * If all options have default values, no need to keep the mbuf.
1430 */
1431 if (imo->imo_multicast_ifp == NULL &&
1432 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1433 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1434 imo->imo_num_memberships == 0) {
1435 free(*imop, M_IPMOPTS);
1436 *imop = NULL;
1437 }
1438
1439 return (error);
1440 }
1441
1442 /*
1443 * Return the IP multicast options in response to user getsockopt().
1444 */
1445 int
1446 ip_getmoptions(optname, imo, mp)
1447 int optname;
1448 struct ip_moptions *imo;
1449 struct mbuf **mp;
1450 {
1451 u_char *ttl;
1452 u_char *loop;
1453 struct in_addr *addr;
1454 struct in_ifaddr *ia;
1455
1456 *mp = m_get(M_WAIT, MT_SOOPTS);
1457
1458 switch (optname) {
1459
1460 case IP_MULTICAST_IF:
1461 addr = mtod(*mp, struct in_addr *);
1462 (*mp)->m_len = sizeof(struct in_addr);
1463 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1464 *addr = zeroin_addr;
1465 else {
1466 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1467 *addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
1468 }
1469 return (0);
1470
1471 case IP_MULTICAST_TTL:
1472 ttl = mtod(*mp, u_char *);
1473 (*mp)->m_len = 1;
1474 *ttl = imo ? imo->imo_multicast_ttl
1475 : IP_DEFAULT_MULTICAST_TTL;
1476 return (0);
1477
1478 case IP_MULTICAST_LOOP:
1479 loop = mtod(*mp, u_char *);
1480 (*mp)->m_len = 1;
1481 *loop = imo ? imo->imo_multicast_loop
1482 : IP_DEFAULT_MULTICAST_LOOP;
1483 return (0);
1484
1485 default:
1486 return (EOPNOTSUPP);
1487 }
1488 }
1489
1490 /*
1491 * Discard the IP multicast options.
1492 */
1493 void
1494 ip_freemoptions(imo)
1495 struct ip_moptions *imo;
1496 {
1497 int i;
1498
1499 if (imo != NULL) {
1500 for (i = 0; i < imo->imo_num_memberships; ++i)
1501 in_delmulti(imo->imo_membership[i]);
1502 free(imo, M_IPMOPTS);
1503 }
1504 }
1505
1506 /*
1507 * Routine called from ip_output() to loop back a copy of an IP multicast
1508 * packet to the input queue of a specified interface. Note that this
1509 * calls the output routine of the loopback "driver", but with an interface
1510 * pointer that might NOT be &loif -- easier than replicating that code here.
1511 */
1512 static void
1513 ip_mloopback(ifp, m, dst)
1514 struct ifnet *ifp;
1515 struct mbuf *m;
1516 struct sockaddr_in *dst;
1517 {
1518 struct ip *ip;
1519 struct mbuf *copym;
1520
1521 copym = m_copy(m, 0, M_COPYALL);
1522 if (copym != NULL
1523 && (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
1524 copym = m_pullup(copym, sizeof(struct ip));
1525 if (copym != NULL) {
1526 /*
1527 * We don't bother to fragment if the IP length is greater
1528 * than the interface's MTU. Can this possibly matter?
1529 */
1530 ip = mtod(copym, struct ip *);
1531 HTONS(ip->ip_len);
1532 HTONS(ip->ip_off);
1533 ip->ip_sum = 0;
1534 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1535 (void) looutput(ifp, copym, sintosa(dst), NULL);
1536 }
1537 }
1538