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