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