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