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