ip_output.c revision 1.58.6.3 1 /* $NetBSD: ip_output.c,v 1.58.6.3 1999/11/30 13:35:33 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Public Access Networks Corporation ("Panix"). It was developed under
38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the NetBSD
51 * Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * Copyright (c) 1982, 1986, 1988, 1990, 1993
71 * The Regents of the University of California. All rights reserved.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. All advertising materials mentioning features or use of this software
82 * must display the following acknowledgement:
83 * This product includes software developed by the University of
84 * California, Berkeley and its contributors.
85 * 4. Neither the name of the University nor the names of its contributors
86 * may be used to endorse or promote products derived from this software
87 * without specific prior written permission.
88 *
89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 * SUCH DAMAGE.
100 *
101 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
102 */
103
104 #include "opt_pfil_hooks.h"
105 #include "opt_ipsec.h"
106 #include "opt_mrouting.h"
107
108 #include <sys/param.h>
109 #include <sys/malloc.h>
110 #include <sys/mbuf.h>
111 #include <sys/errno.h>
112 #include <sys/protosw.h>
113 #include <sys/socket.h>
114 #include <sys/socketvar.h>
115 #include <sys/systm.h>
116
117 #include <vm/vm.h>
118 #include <sys/proc.h>
119
120 #include <net/if.h>
121 #include <net/route.h>
122 #include <net/pfil.h>
123
124 #include <netinet/in.h>
125 #include <netinet/in_systm.h>
126 #include <netinet/ip.h>
127 #include <netinet/in_pcb.h>
128 #include <netinet/in_var.h>
129 #include <netinet/ip_var.h>
130
131 #ifdef __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 * Look for broadcast address and
377 * and verify user is allowed to send
378 * such a packet.
379 */
380 if (in_broadcast(dst->sin_addr, ifp)) {
381 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
382 error = EADDRNOTAVAIL;
383 goto bad;
384 }
385 if ((flags & IP_ALLOWBROADCAST) == 0) {
386 error = EACCES;
387 goto bad;
388 }
389 /* don't allow broadcast messages to be fragmented */
390 if ((u_int16_t)ip->ip_len > ifp->if_mtu) {
391 error = EMSGSIZE;
392 goto bad;
393 }
394 m->m_flags |= M_BCAST;
395 } else
396 m->m_flags &= ~M_BCAST;
397
398 sendit:
399 #ifdef PFIL_HOOKS
400 /*
401 * Run through list of hooks for output packets.
402 */
403 m1 = m;
404 for (pfh = pfil_hook_get(PFIL_OUT); pfh; pfh = pfh->pfil_link.tqe_next)
405 if (pfh->pfil_func) {
406 rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1);
407 if (rv) {
408 error = EHOSTUNREACH;
409 goto done;
410 }
411 m = m1;
412 if (m == NULL)
413 goto done;
414 ip = mtod(m, struct ip *);
415 }
416 #endif /* PFIL_HOOKS */
417
418 #ifdef IPSEC
419 /* get SP for this packet */
420 if (so == NULL)
421 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
422 else
423 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
424
425 if (sp == NULL) {
426 ipsecstat.out_inval++;
427 goto bad;
428 }
429
430 error = 0;
431
432 /* check policy */
433 switch (sp->policy) {
434 case IPSEC_POLICY_DISCARD:
435 /*
436 * This packet is just discarded.
437 */
438 ipsecstat.out_polvio++;
439 goto bad;
440
441 case IPSEC_POLICY_BYPASS:
442 case IPSEC_POLICY_NONE:
443 /* no need to do IPsec. */
444 goto skip_ipsec;
445
446 case IPSEC_POLICY_IPSEC:
447 if (sp->req == NULL) {
448 /* XXX should be panic ? */
449 printf("ip_output: No IPsec request specified.\n");
450 error = EINVAL;
451 goto bad;
452 }
453 break;
454
455 case IPSEC_POLICY_ENTRUST:
456 default:
457 printf("ip_output: Invalid policy found. %d\n", sp->policy);
458 }
459
460 ip->ip_len = htons((u_short)ip->ip_len);
461 ip->ip_off = htons((u_short)ip->ip_off);
462 ip->ip_sum = 0;
463
464 {
465 struct ipsec_output_state state;
466 bzero(&state, sizeof(state));
467 state.m = m;
468 if (flags & IP_ROUTETOIF) {
469 state.ro = &iproute;
470 bzero(&iproute, sizeof(iproute));
471 } else
472 state.ro = ro;
473 state.dst = (struct sockaddr *)dst;
474
475 error = ipsec4_output(&state, sp, flags);
476
477 m = state.m;
478 if (flags & IP_ROUTETOIF) {
479 /*
480 * if we have tunnel mode SA, we may need to ignore
481 * IP_ROUTETOIF.
482 */
483 if (state.ro != &iproute || state.ro->ro_rt != NULL) {
484 flags &= ~IP_ROUTETOIF;
485 ro = state.ro;
486 }
487 } else
488 ro = state.ro;
489 dst = (struct sockaddr_in *)state.dst;
490 if (error) {
491 /* mbuf is already reclaimed in ipsec4_output. */
492 m0 = NULL;
493 switch (error) {
494 case EHOSTUNREACH:
495 case ENETUNREACH:
496 case EMSGSIZE:
497 case ENOBUFS:
498 case ENOMEM:
499 break;
500 default:
501 printf("ip4_output (ipsec): error code %d\n", error);
502 /*fall through*/
503 case ENOENT:
504 /* don't show these error codes to the user */
505 error = 0;
506 break;
507 }
508 goto bad;
509 }
510 }
511
512 /* be sure to update variables that are affected by ipsec4_output() */
513 ip = mtod(m, struct ip *);
514 #ifdef _IP_VHL
515 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
516 #else
517 hlen = ip->ip_hl << 2;
518 #endif
519 if (ro->ro_rt == NULL) {
520 if ((flags & IP_ROUTETOIF) == 0) {
521 printf("ip_output: "
522 "can't update route after IPsec processing\n");
523 error = EHOSTUNREACH; /*XXX*/
524 goto bad;
525 }
526 } else {
527 /* nobody uses ia beyond here */
528 ifp = ro->ro_rt->rt_ifp;
529 }
530
531 /* make it flipped, again. */
532 ip->ip_len = ntohs((u_short)ip->ip_len);
533 ip->ip_off = ntohs((u_short)ip->ip_off);
534 skip_ipsec:
535 #endif /*IPSEC*/
536
537 /*
538 * If small enough for mtu of path, can just send directly.
539 */
540 if ((u_int16_t)ip->ip_len <= mtu) {
541 #if IFA_STATS
542 /*
543 * search for the source address structure to
544 * maintain output statistics.
545 */
546 bzero((caddr_t*) &src, sizeof(src));
547 src.sin_family = AF_INET;
548 src.sin_addr.s_addr = ip->ip_src.s_addr;
549 src.sin_len = sizeof(src);
550 ia = ifatoia(ifa_ifwithladdr(sintosa(&src)));
551 if (ia)
552 ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
553 #endif
554 HTONS(ip->ip_len);
555 HTONS(ip->ip_off);
556 ip->ip_sum = 0;
557 ip->ip_sum = in_cksum(m, hlen);
558 error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt);
559 goto done;
560 }
561
562 /*
563 * Too large for interface; fragment if possible.
564 * Must be able to put at least 8 bytes per fragment.
565 */
566 #if 0
567 /*
568 * If IPsec packet is too big for the interface, try fragment it.
569 * XXX This really is a quickhack. May be inappropriate.
570 * XXX fails if somebody is sending AH'ed packet, with:
571 * sizeof(packet without AH) < mtu < sizeof(packet with AH)
572 */
573 if (sab && ip->ip_p != IPPROTO_AH && (flags & IP_FORWARDING) == 0)
574 ip->ip_off &= ~IP_DF;
575 #endif /*IPSEC*/
576 if (ip->ip_off & IP_DF) {
577 if (flags & IP_RETURNMTU)
578 *mtu_p = mtu;
579 error = EMSGSIZE;
580 ipstat.ips_cantfrag++;
581 goto bad;
582 }
583 len = (mtu - hlen) &~ 7;
584 if (len < 8) {
585 error = EMSGSIZE;
586 goto bad;
587 }
588
589 {
590 int mhlen, firstlen = len;
591 struct mbuf **mnext = &m->m_nextpkt;
592 int fragments = 0;
593 int s;
594
595 /*
596 * Loop through length of segment after first fragment,
597 * make new header and copy data of each part and link onto chain.
598 */
599 m0 = m;
600 mhlen = sizeof (struct ip);
601 for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len) {
602 MGETHDR(m, M_DONTWAIT, MT_HEADER);
603 if (m == 0) {
604 error = ENOBUFS;
605 ipstat.ips_odropped++;
606 goto sendorfree;
607 }
608 *mnext = m;
609 mnext = &m->m_nextpkt;
610 m->m_data += max_linkhdr;
611 mhip = mtod(m, struct ip *);
612 *mhip = *ip;
613 if (hlen > sizeof (struct ip)) {
614 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
615 mhip->ip_hl = mhlen >> 2;
616 }
617 m->m_len = mhlen;
618 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
619 if (ip->ip_off & IP_MF)
620 mhip->ip_off |= IP_MF;
621 if (off + len >= (u_int16_t)ip->ip_len)
622 len = (u_int16_t)ip->ip_len - off;
623 else
624 mhip->ip_off |= IP_MF;
625 mhip->ip_len = htons((u_int16_t)(len + mhlen));
626 m->m_next = m_copy(m0, off, len);
627 if (m->m_next == 0) {
628 error = ENOBUFS; /* ??? */
629 ipstat.ips_odropped++;
630 goto sendorfree;
631 }
632 m->m_pkthdr.len = mhlen + len;
633 m->m_pkthdr.rcvif = (struct ifnet *)0;
634 HTONS(mhip->ip_off);
635 mhip->ip_sum = 0;
636 mhip->ip_sum = in_cksum(m, mhlen);
637 ipstat.ips_ofragments++;
638 fragments++;
639 }
640 /*
641 * Update first fragment by trimming what's been copied out
642 * and updating header, then send each fragment (in order).
643 */
644 m = m0;
645 m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
646 m->m_pkthdr.len = hlen + firstlen;
647 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
648 ip->ip_off |= IP_MF;
649 HTONS(ip->ip_off);
650 ip->ip_sum = 0;
651 ip->ip_sum = in_cksum(m, hlen);
652 sendorfree:
653 /*
654 * If there is no room for all the fragments, don't queue
655 * any of them.
656 */
657 s = splimp();
658 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments)
659 error = ENOBUFS;
660 splx(s);
661 for (m = m0; m; m = m0) {
662 m0 = m->m_nextpkt;
663 m->m_nextpkt = 0;
664 if (error == 0) {
665 #if IFA_STATS
666 /*
667 * search for the source address structure to
668 * maintain output statistics.
669 */
670 bzero((caddr_t*) &src, sizeof(src));
671 src.sin_family = AF_INET;
672 src.sin_addr.s_addr = ip->ip_src.s_addr;
673 src.sin_len = sizeof(src);
674 ia = ifatoia(ifa_ifwithladdr(sintosa(&src)));
675 if (ia) {
676 ia->ia_ifa.ifa_data.ifad_outbytes +=
677 ntohs(ip->ip_len);
678 }
679 #endif
680 error = (*ifp->if_output)(ifp, m, sintosa(dst),
681 ro->ro_rt);
682 } else
683 m_freem(m);
684 }
685
686 if (error == 0)
687 ipstat.ips_fragmented++;
688 }
689 done:
690 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
691 RTFREE(ro->ro_rt);
692 ro->ro_rt = 0;
693 }
694
695 #ifdef IPSEC
696 if (sp != NULL) {
697 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
698 printf("DP ip_output call free SP:%p\n", sp));
699 key_freesp(sp);
700 }
701 #endif /* IPSEC */
702
703 return (error);
704 bad:
705 m_freem(m);
706 goto done;
707 }
708
709 /*
710 * Determine the maximum length of the options to be inserted;
711 * we would far rather allocate too much space rather than too little.
712 */
713
714 u_int
715 ip_optlen(inp)
716 struct inpcb *inp;
717 {
718 struct mbuf *m = inp->inp_options;
719
720 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
721 return(m->m_len - offsetof(struct ipoption, ipopt_dst));
722 else
723 return 0;
724 }
725
726
727 /*
728 * Insert IP options into preformed packet.
729 * Adjust IP destination as required for IP source routing,
730 * as indicated by a non-zero in_addr at the start of the options.
731 */
732 static struct mbuf *
733 ip_insertoptions(m, opt, phlen)
734 register struct mbuf *m;
735 struct mbuf *opt;
736 int *phlen;
737 {
738 register struct ipoption *p = mtod(opt, struct ipoption *);
739 struct mbuf *n;
740 register struct ip *ip = mtod(m, struct ip *);
741 unsigned optlen;
742
743 optlen = opt->m_len - sizeof(p->ipopt_dst);
744 if (optlen + (u_int16_t)ip->ip_len > IP_MAXPACKET)
745 return (m); /* XXX should fail */
746 if (!in_nullhost(p->ipopt_dst))
747 ip->ip_dst = p->ipopt_dst;
748 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
749 MGETHDR(n, M_DONTWAIT, MT_HEADER);
750 if (n == 0)
751 return (m);
752 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
753 m->m_len -= sizeof(struct ip);
754 m->m_data += sizeof(struct ip);
755 n->m_next = m;
756 m = n;
757 m->m_len = optlen + sizeof(struct ip);
758 m->m_data += max_linkhdr;
759 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
760 } else {
761 m->m_data -= optlen;
762 m->m_len += optlen;
763 m->m_pkthdr.len += optlen;
764 memmove(mtod(m, caddr_t), ip, sizeof(struct ip));
765 }
766 ip = mtod(m, struct ip *);
767 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
768 *phlen = sizeof(struct ip) + optlen;
769 ip->ip_len += optlen;
770 return (m);
771 }
772
773 /*
774 * Copy options from ip to jp,
775 * omitting those not copied during fragmentation.
776 */
777 int
778 ip_optcopy(ip, jp)
779 struct ip *ip, *jp;
780 {
781 register u_char *cp, *dp;
782 int opt, optlen, cnt;
783
784 cp = (u_char *)(ip + 1);
785 dp = (u_char *)(jp + 1);
786 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
787 for (; cnt > 0; cnt -= optlen, cp += optlen) {
788 opt = cp[0];
789 if (opt == IPOPT_EOL)
790 break;
791 if (opt == IPOPT_NOP) {
792 /* Preserve for IP mcast tunnel's LSRR alignment. */
793 *dp++ = IPOPT_NOP;
794 optlen = 1;
795 continue;
796 } else
797 optlen = cp[IPOPT_OLEN];
798 /* bogus lengths should have been caught by ip_dooptions */
799 if (optlen > cnt)
800 optlen = cnt;
801 if (IPOPT_COPIED(opt)) {
802 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
803 dp += optlen;
804 }
805 }
806 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
807 *dp++ = IPOPT_EOL;
808 return (optlen);
809 }
810
811 /*
812 * IP socket option processing.
813 */
814 int
815 ip_ctloutput(op, so, level, optname, mp)
816 int op;
817 struct socket *so;
818 int level, optname;
819 struct mbuf **mp;
820 {
821 register struct inpcb *inp = sotoinpcb(so);
822 register struct mbuf *m = *mp;
823 register int optval = 0;
824 int error = 0;
825 #ifdef IPSEC
826 #ifdef __NetBSD__
827 struct proc *p = curproc; /*XXX*/
828 #endif
829 #endif
830
831 if (level != IPPROTO_IP) {
832 error = EINVAL;
833 if (op == PRCO_SETOPT && *mp)
834 (void) m_free(*mp);
835 } else switch (op) {
836
837 case PRCO_SETOPT:
838 switch (optname) {
839 case IP_OPTIONS:
840 #ifdef notyet
841 case IP_RETOPTS:
842 return (ip_pcbopts(optname, &inp->inp_options, m));
843 #else
844 return (ip_pcbopts(&inp->inp_options, m));
845 #endif
846
847 case IP_TOS:
848 case IP_TTL:
849 case IP_RECVOPTS:
850 case IP_RECVRETOPTS:
851 case IP_RECVDSTADDR:
852 case IP_RECVIF:
853 if (m == NULL || m->m_len != sizeof(int))
854 error = EINVAL;
855 else {
856 optval = *mtod(m, int *);
857 switch (optname) {
858
859 case IP_TOS:
860 inp->inp_ip.ip_tos = optval;
861 break;
862
863 case IP_TTL:
864 inp->inp_ip.ip_ttl = optval;
865 break;
866 #define OPTSET(bit) \
867 if (optval) \
868 inp->inp_flags |= bit; \
869 else \
870 inp->inp_flags &= ~bit;
871
872 case IP_RECVOPTS:
873 OPTSET(INP_RECVOPTS);
874 break;
875
876 case IP_RECVRETOPTS:
877 OPTSET(INP_RECVRETOPTS);
878 break;
879
880 case IP_RECVDSTADDR:
881 OPTSET(INP_RECVDSTADDR);
882 break;
883
884 case IP_RECVIF:
885 OPTSET(INP_RECVIF);
886 break;
887 }
888 }
889 break;
890 #undef OPTSET
891
892 case IP_MULTICAST_IF:
893 case IP_MULTICAST_TTL:
894 case IP_MULTICAST_LOOP:
895 case IP_ADD_MEMBERSHIP:
896 case IP_DROP_MEMBERSHIP:
897 error = ip_setmoptions(optname, &inp->inp_moptions, m);
898 break;
899
900 case IP_PORTRANGE:
901 if (m == 0 || m->m_len != sizeof(int))
902 error = EINVAL;
903 else {
904 optval = *mtod(m, int *);
905
906 switch (optval) {
907
908 case IP_PORTRANGE_DEFAULT:
909 case IP_PORTRANGE_HIGH:
910 inp->inp_flags &= ~(INP_LOWPORT);
911 break;
912
913 case IP_PORTRANGE_LOW:
914 inp->inp_flags |= INP_LOWPORT;
915 break;
916
917 default:
918 error = EINVAL;
919 break;
920 }
921 }
922 break;
923
924 #ifdef IPSEC
925 case IP_IPSEC_POLICY:
926 {
927 caddr_t req = NULL;
928 int priv = 0;
929 #ifdef __NetBSD__
930 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
931 priv = 0;
932 else
933 priv = 1;
934 #else
935 priv = (in6p->in6p_socket->so_state & SS_PRIV);
936 #endif
937 if (m != 0)
938 req = mtod(m, caddr_t);
939 error = ipsec4_set_policy(inp, optname, req, 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 {
1012 caddr_t req = NULL;
1013
1014 if (m != 0)
1015 req = mtod(m, caddr_t);
1016 error = ipsec4_get_policy(inp, req, mp);
1017 break;
1018 }
1019 #endif /*IPSEC*/
1020
1021 case IP_MULTICAST_IF:
1022 case IP_MULTICAST_TTL:
1023 case IP_MULTICAST_LOOP:
1024 case IP_ADD_MEMBERSHIP:
1025 case IP_DROP_MEMBERSHIP:
1026 error = ip_getmoptions(optname, inp->inp_moptions, mp);
1027 break;
1028
1029 case IP_PORTRANGE:
1030 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1031 m->m_len = sizeof(int);
1032
1033 if (inp->inp_flags & INP_LOWPORT)
1034 optval = IP_PORTRANGE_LOW;
1035 else
1036 optval = IP_PORTRANGE_DEFAULT;
1037
1038 *mtod(m, int *) = optval;
1039 break;
1040
1041 default:
1042 error = ENOPROTOOPT;
1043 break;
1044 }
1045 break;
1046 }
1047 return (error);
1048 }
1049
1050 /*
1051 * Set up IP options in pcb for insertion in output packets.
1052 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1053 * with destination address if source routed.
1054 */
1055 int
1056 #ifdef notyet
1057 ip_pcbopts(optname, pcbopt, m)
1058 int optname;
1059 #else
1060 ip_pcbopts(pcbopt, m)
1061 #endif
1062 struct mbuf **pcbopt;
1063 register struct mbuf *m;
1064 {
1065 register int cnt, optlen;
1066 register u_char *cp;
1067 u_char opt;
1068
1069 /* turn off any old options */
1070 if (*pcbopt)
1071 (void)m_free(*pcbopt);
1072 *pcbopt = 0;
1073 if (m == (struct mbuf *)0 || m->m_len == 0) {
1074 /*
1075 * Only turning off any previous options.
1076 */
1077 if (m)
1078 (void)m_free(m);
1079 return (0);
1080 }
1081
1082 #ifndef vax
1083 if (m->m_len % sizeof(int32_t))
1084 goto bad;
1085 #endif
1086 /*
1087 * IP first-hop destination address will be stored before
1088 * actual options; move other options back
1089 * and clear it when none present.
1090 */
1091 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1092 goto bad;
1093 cnt = m->m_len;
1094 m->m_len += sizeof(struct in_addr);
1095 cp = mtod(m, u_char *) + sizeof(struct in_addr);
1096 memmove(cp, mtod(m, caddr_t), (unsigned)cnt);
1097 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1098
1099 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1100 opt = cp[IPOPT_OPTVAL];
1101 if (opt == IPOPT_EOL)
1102 break;
1103 if (opt == IPOPT_NOP)
1104 optlen = 1;
1105 else {
1106 optlen = cp[IPOPT_OLEN];
1107 if (optlen <= IPOPT_OLEN || optlen > cnt)
1108 goto bad;
1109 }
1110 switch (opt) {
1111
1112 default:
1113 break;
1114
1115 case IPOPT_LSRR:
1116 case IPOPT_SSRR:
1117 /*
1118 * user process specifies route as:
1119 * ->A->B->C->D
1120 * D must be our final destination (but we can't
1121 * check that since we may not have connected yet).
1122 * A is first hop destination, which doesn't appear in
1123 * actual IP option, but is stored before the options.
1124 */
1125 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1126 goto bad;
1127 m->m_len -= sizeof(struct in_addr);
1128 cnt -= sizeof(struct in_addr);
1129 optlen -= sizeof(struct in_addr);
1130 cp[IPOPT_OLEN] = optlen;
1131 /*
1132 * Move first hop before start of options.
1133 */
1134 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1135 sizeof(struct in_addr));
1136 /*
1137 * Then copy rest of options back
1138 * to close up the deleted entry.
1139 */
1140 memmove(&cp[IPOPT_OFFSET+1],
1141 (caddr_t)(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1142 (unsigned)cnt + sizeof(struct in_addr));
1143 break;
1144 }
1145 }
1146 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1147 goto bad;
1148 *pcbopt = m;
1149 return (0);
1150
1151 bad:
1152 (void)m_free(m);
1153 return (EINVAL);
1154 }
1155
1156 /*
1157 * Set the IP multicast options in response to user setsockopt().
1158 */
1159 int
1160 ip_setmoptions(optname, imop, m)
1161 int optname;
1162 struct ip_moptions **imop;
1163 struct mbuf *m;
1164 {
1165 register int error = 0;
1166 u_char loop;
1167 register int i;
1168 struct in_addr addr;
1169 register struct ip_mreq *mreq;
1170 register struct ifnet *ifp;
1171 register struct ip_moptions *imo = *imop;
1172 struct route ro;
1173 register struct sockaddr_in *dst;
1174
1175 if (imo == NULL) {
1176 /*
1177 * No multicast option buffer attached to the pcb;
1178 * allocate one and initialize to default values.
1179 */
1180 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
1181 M_WAITOK);
1182
1183 if (imo == NULL)
1184 return (ENOBUFS);
1185 *imop = imo;
1186 imo->imo_multicast_ifp = NULL;
1187 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1188 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1189 imo->imo_num_memberships = 0;
1190 }
1191
1192 switch (optname) {
1193
1194 case IP_MULTICAST_IF:
1195 /*
1196 * Select the interface for outgoing multicast packets.
1197 */
1198 if (m == NULL || m->m_len != sizeof(struct in_addr)) {
1199 error = EINVAL;
1200 break;
1201 }
1202 addr = *(mtod(m, struct in_addr *));
1203 /*
1204 * INADDR_ANY is used to remove a previous selection.
1205 * When no interface is selected, a default one is
1206 * chosen every time a multicast packet is sent.
1207 */
1208 if (in_nullhost(addr)) {
1209 imo->imo_multicast_ifp = NULL;
1210 break;
1211 }
1212 /*
1213 * The selected interface is identified by its local
1214 * IP address. Find the interface and confirm that
1215 * it supports multicasting.
1216 */
1217 INADDR_TO_IFP(addr, ifp);
1218 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1219 error = EADDRNOTAVAIL;
1220 break;
1221 }
1222 imo->imo_multicast_ifp = ifp;
1223 break;
1224
1225 case IP_MULTICAST_TTL:
1226 /*
1227 * Set the IP time-to-live for outgoing multicast packets.
1228 */
1229 if (m == NULL || m->m_len != 1) {
1230 error = EINVAL;
1231 break;
1232 }
1233 imo->imo_multicast_ttl = *(mtod(m, u_char *));
1234 break;
1235
1236 case IP_MULTICAST_LOOP:
1237 /*
1238 * Set the loopback flag for outgoing multicast packets.
1239 * Must be zero or one.
1240 */
1241 if (m == NULL || m->m_len != 1 ||
1242 (loop = *(mtod(m, u_char *))) > 1) {
1243 error = EINVAL;
1244 break;
1245 }
1246 imo->imo_multicast_loop = loop;
1247 break;
1248
1249 case IP_ADD_MEMBERSHIP:
1250 /*
1251 * Add a multicast group membership.
1252 * Group must be a valid IP multicast address.
1253 */
1254 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1255 error = EINVAL;
1256 break;
1257 }
1258 mreq = mtod(m, struct ip_mreq *);
1259 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1260 error = EINVAL;
1261 break;
1262 }
1263 /*
1264 * If no interface address was provided, use the interface of
1265 * the route to the given multicast address.
1266 */
1267 if (in_nullhost(mreq->imr_interface)) {
1268 bzero((caddr_t)&ro, sizeof(ro));
1269 ro.ro_rt = NULL;
1270 dst = satosin(&ro.ro_dst);
1271 dst->sin_len = sizeof(*dst);
1272 dst->sin_family = AF_INET;
1273 dst->sin_addr = mreq->imr_multiaddr;
1274 rtalloc(&ro);
1275 if (ro.ro_rt == NULL) {
1276 error = EADDRNOTAVAIL;
1277 break;
1278 }
1279 ifp = ro.ro_rt->rt_ifp;
1280 rtfree(ro.ro_rt);
1281 } else {
1282 INADDR_TO_IFP(mreq->imr_interface, ifp);
1283 }
1284 /*
1285 * See if we found an interface, and confirm that it
1286 * supports multicast.
1287 */
1288 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1289 error = EADDRNOTAVAIL;
1290 break;
1291 }
1292 /*
1293 * See if the membership already exists or if all the
1294 * membership slots are full.
1295 */
1296 for (i = 0; i < imo->imo_num_memberships; ++i) {
1297 if (imo->imo_membership[i]->inm_ifp == ifp &&
1298 in_hosteq(imo->imo_membership[i]->inm_addr,
1299 mreq->imr_multiaddr))
1300 break;
1301 }
1302 if (i < imo->imo_num_memberships) {
1303 error = EADDRINUSE;
1304 break;
1305 }
1306 if (i == IP_MAX_MEMBERSHIPS) {
1307 error = ETOOMANYREFS;
1308 break;
1309 }
1310 /*
1311 * Everything looks good; add a new record to the multicast
1312 * address list for the given interface.
1313 */
1314 if ((imo->imo_membership[i] =
1315 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1316 error = ENOBUFS;
1317 break;
1318 }
1319 ++imo->imo_num_memberships;
1320 break;
1321
1322 case IP_DROP_MEMBERSHIP:
1323 /*
1324 * Drop a multicast group membership.
1325 * Group must be a valid IP multicast address.
1326 */
1327 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1328 error = EINVAL;
1329 break;
1330 }
1331 mreq = mtod(m, struct ip_mreq *);
1332 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1333 error = EINVAL;
1334 break;
1335 }
1336 /*
1337 * If an interface address was specified, get a pointer
1338 * to its ifnet structure.
1339 */
1340 if (in_nullhost(mreq->imr_interface))
1341 ifp = NULL;
1342 else {
1343 INADDR_TO_IFP(mreq->imr_interface, ifp);
1344 if (ifp == NULL) {
1345 error = EADDRNOTAVAIL;
1346 break;
1347 }
1348 }
1349 /*
1350 * Find the membership in the membership array.
1351 */
1352 for (i = 0; i < imo->imo_num_memberships; ++i) {
1353 if ((ifp == NULL ||
1354 imo->imo_membership[i]->inm_ifp == ifp) &&
1355 in_hosteq(imo->imo_membership[i]->inm_addr,
1356 mreq->imr_multiaddr))
1357 break;
1358 }
1359 if (i == imo->imo_num_memberships) {
1360 error = EADDRNOTAVAIL;
1361 break;
1362 }
1363 /*
1364 * Give up the multicast address record to which the
1365 * membership points.
1366 */
1367 in_delmulti(imo->imo_membership[i]);
1368 /*
1369 * Remove the gap in the membership array.
1370 */
1371 for (++i; i < imo->imo_num_memberships; ++i)
1372 imo->imo_membership[i-1] = imo->imo_membership[i];
1373 --imo->imo_num_memberships;
1374 break;
1375
1376 default:
1377 error = EOPNOTSUPP;
1378 break;
1379 }
1380
1381 /*
1382 * If all options have default values, no need to keep the mbuf.
1383 */
1384 if (imo->imo_multicast_ifp == NULL &&
1385 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1386 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1387 imo->imo_num_memberships == 0) {
1388 free(*imop, M_IPMOPTS);
1389 *imop = NULL;
1390 }
1391
1392 return (error);
1393 }
1394
1395 /*
1396 * Return the IP multicast options in response to user getsockopt().
1397 */
1398 int
1399 ip_getmoptions(optname, imo, mp)
1400 int optname;
1401 register struct ip_moptions *imo;
1402 register struct mbuf **mp;
1403 {
1404 u_char *ttl;
1405 u_char *loop;
1406 struct in_addr *addr;
1407 struct in_ifaddr *ia;
1408
1409 *mp = m_get(M_WAIT, MT_SOOPTS);
1410
1411 switch (optname) {
1412
1413 case IP_MULTICAST_IF:
1414 addr = mtod(*mp, struct in_addr *);
1415 (*mp)->m_len = sizeof(struct in_addr);
1416 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1417 *addr = zeroin_addr;
1418 else {
1419 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1420 *addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
1421 }
1422 return (0);
1423
1424 case IP_MULTICAST_TTL:
1425 ttl = mtod(*mp, u_char *);
1426 (*mp)->m_len = 1;
1427 *ttl = imo ? imo->imo_multicast_ttl
1428 : IP_DEFAULT_MULTICAST_TTL;
1429 return (0);
1430
1431 case IP_MULTICAST_LOOP:
1432 loop = mtod(*mp, u_char *);
1433 (*mp)->m_len = 1;
1434 *loop = imo ? imo->imo_multicast_loop
1435 : IP_DEFAULT_MULTICAST_LOOP;
1436 return (0);
1437
1438 default:
1439 return (EOPNOTSUPP);
1440 }
1441 }
1442
1443 /*
1444 * Discard the IP multicast options.
1445 */
1446 void
1447 ip_freemoptions(imo)
1448 register struct ip_moptions *imo;
1449 {
1450 register int i;
1451
1452 if (imo != NULL) {
1453 for (i = 0; i < imo->imo_num_memberships; ++i)
1454 in_delmulti(imo->imo_membership[i]);
1455 free(imo, M_IPMOPTS);
1456 }
1457 }
1458
1459 /*
1460 * Routine called from ip_output() to loop back a copy of an IP multicast
1461 * packet to the input queue of a specified interface. Note that this
1462 * calls the output routine of the loopback "driver", but with an interface
1463 * pointer that might NOT be &loif -- easier than replicating that code here.
1464 */
1465 static void
1466 ip_mloopback(ifp, m, dst)
1467 struct ifnet *ifp;
1468 register struct mbuf *m;
1469 register struct sockaddr_in *dst;
1470 {
1471 register struct ip *ip;
1472 struct mbuf *copym;
1473
1474 copym = m_copy(m, 0, M_COPYALL);
1475 if (copym != NULL) {
1476 /*
1477 * We don't bother to fragment if the IP length is greater
1478 * than the interface's MTU. Can this possibly matter?
1479 */
1480 ip = mtod(copym, struct ip *);
1481 HTONS(ip->ip_len);
1482 HTONS(ip->ip_off);
1483 ip->ip_sum = 0;
1484 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1485 (void) looutput(ifp, copym, sintosa(dst), NULL);
1486 }
1487 }
1488