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