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