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