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