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