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