2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
30 * $FreeBSD: src/sys/netinet/ip_output.c,v 1.99.2.37 2003/04/15 06:44:45 silby Exp $
36 #include "opt_ipdivert.h"
37 #include "opt_mbuf_stress_test.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/in_cksum.h>
54 #include <sys/thread2.h>
55 #include <sys/mplock2.h>
56 #include <sys/msgport2.h>
59 #include <net/netisr.h>
61 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip_var.h>
70 #include <netproto/mpls/mpls_var.h>
72 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
74 #include <net/ipfw/ip_fw.h>
75 #include <net/dummynet/ip_dummynet.h>
77 #define print_ip(x, a, y) kprintf("%s %d.%d.%d.%d%s",\
78 x, (ntohl(a.s_addr)>>24)&0xFF,\
79 (ntohl(a.s_addr)>>16)&0xFF,\
80 (ntohl(a.s_addr)>>8)&0xFF,\
81 (ntohl(a.s_addr))&0xFF, y);
85 #ifdef MBUF_STRESS_TEST
86 int mbuf_frag_size = 0;
87 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
88 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
91 static int ip_do_rfc6864 = 1;
92 SYSCTL_INT(_net_inet_ip, OID_AUTO, rfc6864, CTLFLAG_RW, &ip_do_rfc6864, 0,
93 "Don't generate IP ID for DF IP datagrams");
95 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
96 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
97 static void ip_mloopback
98 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
99 static int ip_getmoptions
100 (struct sockopt *, struct ip_moptions *);
101 static int ip_pcbopts(int, struct mbuf **, struct mbuf *);
102 static int ip_setmoptions
103 (struct sockopt *, struct ip_moptions **);
105 int ip_optcopy(struct ip *, struct ip *);
107 extern struct protosw inetsw[];
110 ip_localforward(struct mbuf *m, const struct sockaddr_in *dst, int hlen)
112 struct in_ifaddr_container *iac;
115 * We need to figure out if we have been forwarded to a local
116 * socket. If so, then we should somehow "loop back" to
117 * ip_input(), and get directed to the PCB as if we had received
118 * this packet. This is because it may be difficult to identify
119 * the packets you want to forward until they are being output
120 * and have selected an interface (e.g. locally initiated
121 * packets). If we used the loopback inteface, we would not be
122 * able to control what happens as the packet runs through
123 * ip_input() as it is done through a ISR.
125 LIST_FOREACH(iac, INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
127 * If the addr to forward to is one of ours, we pretend
128 * to be the destination for this packet.
130 if (IA_SIN(iac->ia)->sin_addr.s_addr == dst->sin_addr.s_addr)
136 if (m->m_pkthdr.rcvif == NULL)
137 m->m_pkthdr.rcvif = loif;
138 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
139 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
141 m->m_pkthdr.csum_data = 0xffff;
143 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED | CSUM_IP_VALID;
146 * Make sure that the IP header is in one mbuf,
147 * required by ip_input
149 if (m->m_len < hlen) {
150 m = m_pullup(m, hlen);
152 /* The packet was freed; we are done */
156 ip = mtod(m, struct ip *);
158 ip->ip_len = htons(ip->ip_len);
159 ip->ip_off = htons(ip->ip_off);
162 return 1; /* The packet gets forwarded locally */
168 * IP output. The packet in mbuf chain m contains a skeletal IP
169 * header (with len, off, ttl, proto, tos, src, dst).
170 * The mbuf chain containing the packet will be freed.
171 * The mbuf opt, if present, will not be freed.
174 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro,
175 int flags, struct ip_moptions *imo, struct inpcb *inp)
178 struct ifnet *ifp = NULL; /* keep compiler happy */
180 int hlen = sizeof(struct ip);
182 struct sockaddr_in *dst = NULL; /* keep compiler happy */
183 struct in_ifaddr *ia = NULL;
184 int isbroadcast, sw_csum;
185 struct in_addr pkt_dst;
186 struct route iproute;
188 struct sockaddr_in *next_hop = NULL;
189 int src_was_INADDR_ANY = 0; /* as the name says... */
191 ASSERT_NETISR_NCPUS(mycpuid);
198 bzero(ro, sizeof *ro);
199 } else if (ro->ro_rt != NULL && ro->ro_rt->rt_cpuid != mycpuid) {
200 if (flags & IP_DEBUGROUTE) {
201 panic("ip_output: rt rt_cpuid %d accessed on cpu %d\n",
202 ro->ro_rt->rt_cpuid, mycpuid);
207 * If the cached rtentry's owner CPU is not the current CPU,
208 * then don't touch the cached rtentry (remote free is too
209 * expensive in this context); just relocate the route.
212 bzero(ro, sizeof *ro);
215 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
217 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
218 KKASSERT(mtag != NULL);
219 next_hop = m_tag_data(mtag);
222 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
223 struct dn_pkt *dn_pkt;
225 /* Extract info from dummynet tag */
226 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
227 KKASSERT(mtag != NULL);
228 dn_pkt = m_tag_data(mtag);
231 * The packet was already tagged, so part of the
232 * processing was already done, and we need to go down.
233 * Get the calculated parameters from the tag.
237 KKASSERT(ro == &iproute);
238 *ro = dn_pkt->ro; /* structure copy */
239 KKASSERT(ro->ro_rt == NULL || ro->ro_rt->rt_cpuid == mycpuid);
241 dst = dn_pkt->dn_dst;
242 if (dst == (struct sockaddr_in *)&(dn_pkt->ro.ro_dst)) {
243 /* If 'dst' points into dummynet tag, adjust it */
244 dst = (struct sockaddr_in *)&(ro->ro_dst);
247 ip = mtod(m, struct ip *);
248 hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
250 ia = ifatoia(ro->ro_rt->rt_ifa);
256 m = ip_insertoptions(m, opt, &len);
260 ip = mtod(m, struct ip *);
265 if (!(flags & (IP_FORWARDING|IP_RAWOUTPUT))) {
266 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
268 if (ip_do_rfc6864 && (ip->ip_off & IP_DF))
271 ip->ip_id = ip_newid();
272 ipstat.ips_localout++;
274 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
278 pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst;
280 dst = (struct sockaddr_in *)&ro->ro_dst;
282 * If there is a cached route,
283 * check that it is to the same destination
284 * and is still up. If not, free it and try again.
285 * The address family should also be checked in case of sharing the
289 (!(ro->ro_rt->rt_flags & RTF_UP) ||
290 dst->sin_family != AF_INET ||
291 dst->sin_addr.s_addr != pkt_dst.s_addr)) {
295 if (ro->ro_rt == NULL) {
296 bzero(dst, sizeof *dst);
297 dst->sin_family = AF_INET;
298 dst->sin_len = sizeof *dst;
299 dst->sin_addr = pkt_dst;
302 * If routing to interface only,
303 * short circuit routing lookup.
305 if (flags & IP_ROUTETOIF) {
306 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
307 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
308 ipstat.ips_noroute++;
314 isbroadcast = in_broadcast(dst->sin_addr, ifp);
315 } else if (IN_MULTICAST(ntohl(pkt_dst.s_addr)) &&
316 imo != NULL && imo->imo_multicast_ifp != NULL) {
318 * Bypass the normal routing lookup for multicast
319 * packets if the interface is specified.
321 ifp = imo->imo_multicast_ifp;
323 isbroadcast = 0; /* fool gcc */
326 * If this is the case, we probably don't want to allocate
327 * a protocol-cloned route since we didn't get one from the
328 * ULP. This lets TCP do its thing, while not burdening
329 * forwarding or ICMP with the overhead of cloning a route.
330 * Of course, we still want to do any cloning requested by
331 * the link layer, as this is probably required in all cases
332 * for correct operation (as it is for ARP).
334 if (ro->ro_rt == NULL)
335 rtalloc_ign(ro, RTF_PRCLONING);
336 if (ro->ro_rt == NULL) {
337 ipstat.ips_noroute++;
338 error = EHOSTUNREACH;
341 ia = ifatoia(ro->ro_rt->rt_ifa);
342 ifp = ro->ro_rt->rt_ifp;
344 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
345 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
346 if (ro->ro_rt->rt_flags & RTF_HOST)
347 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
349 isbroadcast = in_broadcast(dst->sin_addr, ifp);
351 if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
352 m->m_flags |= M_MCAST;
354 * IP destination address is multicast. Make sure "dst"
355 * still points to the address in "ro". (It may have been
356 * changed to point to a gateway address, above.)
358 dst = (struct sockaddr_in *)&ro->ro_dst;
360 * See if the caller provided any multicast options
363 ip->ip_ttl = imo->imo_multicast_ttl;
364 if (imo->imo_multicast_vif != -1) {
367 ip_mcast_src(imo->imo_multicast_vif) :
371 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
374 * Confirm that the outgoing interface supports multicast.
376 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
377 if (!(ifp->if_flags & IFF_MULTICAST)) {
378 ipstat.ips_noroute++;
384 * If source address not specified yet, use address of the
385 * outgoing interface. In case, keep note we did that, so
386 * if the the firewall changes the next-hop causing the
387 * output interface to change, we can fix that.
389 if (ip->ip_src.s_addr == INADDR_ANY || src_was_INADDR_ANY) {
390 /* Interface may have no addresses. */
392 ip->ip_src = IA_SIN(ia)->sin_addr;
393 src_was_INADDR_ANY = 1;
397 if (ip->ip_src.s_addr != INADDR_ANY) {
398 struct in_multi *inm;
400 inm = IN_LOOKUP_MULTI(&pkt_dst, ifp);
402 (imo == NULL || imo->imo_multicast_loop)) {
404 * If we belong to the destination multicast
405 * group on the outgoing interface, and the
406 * caller did not forbid loopback, loop back
409 ip_mloopback(ifp, m, dst, hlen);
412 * If we are acting as a multicast router,
413 * perform multicast forwarding as if the
414 * packet had just arrived on the interface
415 * to which we are about to send. The
416 * multicast forwarding function recursively
417 * calls this function, using the IP_FORWARDING
418 * flag to prevent infinite recursion.
420 * Multicasts that are looped back by
421 * ip_mloopback(), above, will be forwarded by
422 * the ip_input() routine, if necessary.
424 if (ip_mrouter && !(flags & IP_FORWARDING)) {
426 * If rsvp daemon is not running, do
427 * not set ip_moptions. This ensures
428 * that the packet is multicast and
429 * not just sent down one link as
430 * prescribed by rsvpd.
436 if (ip_mforward(ip, ifp,
449 * Multicasts with a time-to-live of zero may be looped-
450 * back, above, but must not be transmitted on a network.
451 * Also, multicasts addressed to the loopback interface
452 * are not sent -- the above call to ip_mloopback() will
453 * loop back a copy if this host actually belongs to the
454 * destination group on the loopback interface.
456 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
463 m->m_flags &= ~M_MCAST;
467 * If the source address is not specified yet, use the address
468 * of the outgoing interface. In case, keep note we did that,
469 * so if the the firewall changes the next-hop causing the output
470 * interface to change, we can fix that.
472 if (ip->ip_src.s_addr == INADDR_ANY || src_was_INADDR_ANY) {
473 /* Interface may have no addresses. */
475 ip->ip_src = IA_SIN(ia)->sin_addr;
476 src_was_INADDR_ANY = 1;
481 * Look for broadcast address and
482 * verify user is allowed to send
486 if (!(ifp->if_flags & IFF_BROADCAST)) {
487 error = EADDRNOTAVAIL;
490 if (!(flags & IP_ALLOWBROADCAST)) {
494 /* don't allow broadcast messages to be fragmented */
495 if (ip->ip_len > ifp->if_mtu) {
499 m->m_flags |= M_BCAST;
501 m->m_flags &= ~M_BCAST;
506 /* We are already being fwd'd from a firewall. */
507 if (next_hop != NULL)
511 if (!pfil_has_hooks(&inet_pfil_hook)) {
512 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
514 * Strip dummynet tags from stranded packets
516 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
517 KKASSERT(mtag != NULL);
518 m_tag_delete(m, mtag);
519 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
526 * - Xlate: translate packet's addr/port (NAT).
527 * - Firewall: deny/allow/etc.
528 * - Wrap: fake packet's addr/port <unimpl.>
529 * - Encapsulate: put it in another IP and send out. <unimp.>
533 * Run through list of hooks for output packets.
535 error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT);
536 if (error != 0 || m == NULL)
538 ip = mtod(m, struct ip *);
540 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
542 * Check dst to make sure it is directly reachable on the
543 * interface we previously thought it was.
544 * If it isn't (which may be likely in some situations) we have
545 * to re-route it (ie, find a route for the next-hop and the
546 * associated interface) and set them here. This is nested
547 * forwarding which in most cases is undesirable, except where
548 * such control is nigh impossible. So we do it here.
551 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
552 KKASSERT(mtag != NULL);
553 next_hop = m_tag_data(mtag);
556 * Try local forwarding first
558 if (ip_localforward(m, next_hop, hlen))
562 * Relocate the route based on next_hop.
563 * If the current route is inp's cache, keep it untouched.
565 if (ro == &iproute && ro->ro_rt != NULL) {
570 bzero(ro, sizeof *ro);
573 * Forwarding to broadcast address is not allowed.
574 * XXX Should we follow IP_ROUTETOIF?
576 flags &= ~(IP_ALLOWBROADCAST | IP_ROUTETOIF);
578 /* We are doing forwarding now */
579 flags |= IP_FORWARDING;
584 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
585 struct dn_pkt *dn_pkt;
587 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
588 KKASSERT(mtag != NULL);
589 dn_pkt = m_tag_data(mtag);
592 * Under certain cases it is not possible to recalculate
593 * 'ro' and 'dst', let alone 'flags', so just save them in
594 * dummynet tag and avoid the possible wrong reculcalation
595 * when we come back to ip_output() again.
597 * All other parameters have been already used and so they
598 * are not needed anymore.
599 * XXX if the ifp is deleted while a pkt is in dummynet,
600 * we are in trouble! (TODO use ifnet_detach_event)
602 * We need to copy *ro because for ICMP pkts (and maybe
603 * others) the caller passed a pointer into the stack;
604 * dst might also be a pointer into *ro so it needs to
609 ro->ro_rt->rt_refcnt++;
610 if (dst == (struct sockaddr_in *)&ro->ro_dst) {
611 /* 'dst' points into 'ro' */
612 dst = (struct sockaddr_in *)&(dn_pkt->ro.ro_dst);
614 dn_pkt->dn_dst = dst;
615 dn_pkt->flags = flags;
621 if (m->m_pkthdr.fw_flags & IPFW_MBUF_CONTINUE) {
622 /* ipfw was disabled/unloaded. */
627 /* 127/8 must not appear on wire - RFC1122. */
628 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
629 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
630 if (!(ifp->if_flags & IFF_LOOPBACK)) {
631 ipstat.ips_badaddr++;
632 error = EADDRNOTAVAIL;
636 if (ip->ip_src.s_addr == INADDR_ANY ||
637 IN_MULTICAST(ntohl(ip->ip_src.s_addr))) {
638 ipstat.ips_badaddr++;
639 error = EADDRNOTAVAIL;
643 if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
644 m->m_pkthdr.csum_flags |= CSUM_IP;
645 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
646 if (sw_csum & CSUM_DELAY_DATA) {
648 sw_csum &= ~CSUM_DELAY_DATA;
650 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
654 m->m_pkthdr.csum_iphlen = hlen;
657 * If small enough for interface, or the interface will take
658 * care of the fragmentation or segmentation for us, can just
661 if (ip->ip_len <= ifp->if_mtu ||
662 ((ifp->if_hwassist & CSUM_FRAGMENT) && !(ip->ip_off & IP_DF)) ||
663 (m->m_pkthdr.csum_flags & CSUM_TSO)) {
664 ip->ip_len = htons(ip->ip_len);
665 ip->ip_off = htons(ip->ip_off);
667 if (sw_csum & CSUM_DELAY_IP) {
668 if (ip->ip_vhl == IP_VHL_BORING)
669 ip->ip_sum = in_cksum_hdr(ip);
671 ip->ip_sum = in_cksum(m, hlen);
674 /* Record statistics for this interface address. */
675 if (!(flags & IP_FORWARDING) && ia) {
676 IFA_STAT_INC(&ia->ia_ifa, opackets, 1);
677 IFA_STAT_INC(&ia->ia_ifa, obytes, m->m_pkthdr.len);
680 #ifdef MBUF_STRESS_TEST
681 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) {
682 struct mbuf *m1, *m2;
685 tmp = length = m->m_pkthdr.len;
687 while ((length -= mbuf_frag_size) >= 1) {
688 m1 = m_split(m, length, M_NOWAIT);
692 while (m2->m_next != NULL)
696 m->m_pkthdr.len = tmp;
701 if (!mpls_output_process(m, ro->ro_rt))
704 error = ifp->if_output(ifp, m, (struct sockaddr *)dst,
709 if (ip->ip_off & IP_DF) {
712 * This case can happen if the user changed the MTU
713 * of an interface after enabling IP on it. Because
714 * most netifs don't keep track of routes pointing to
715 * them, there is no way for one to update all its
716 * routes when the MTU is changed.
718 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
719 !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) &&
720 (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
721 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
723 ipstat.ips_cantfrag++;
728 * Too large for interface; fragment if possible. If successful,
729 * on return, m will point to a list of packets to be sent.
731 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
738 /* Record statistics for this interface address. */
740 IFA_STAT_INC(&ia->ia_ifa, opackets, 1);
741 IFA_STAT_INC(&ia->ia_ifa, obytes,
745 if (!mpls_output_process(m, ro->ro_rt))
748 error = ifp->if_output(ifp, m, (struct sockaddr *)dst,
756 ipstat.ips_fragmented++;
759 if (ro == &iproute && ro->ro_rt != NULL) {
770 * Create a chain of fragments which fit the given mtu. m_frag points to the
771 * mbuf to be fragmented; on return it points to the chain with the fragments.
772 * Return 0 if no error. If error, m_frag may contain a partially built
773 * chain of fragments that should be freed by the caller.
775 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
776 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
779 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
780 u_long if_hwassist_flags, int sw_csum)
783 int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
784 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */
786 struct mbuf *m0 = *m_frag; /* the original packet */
791 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */
792 ipstat.ips_cantfrag++;
797 * Must be able to put at least 8 bytes per fragment.
803 * If the interface will not calculate checksums on
804 * fragmented packets, then do it here.
806 if ((m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) &&
807 !(if_hwassist_flags & CSUM_IP_FRAGS)) {
808 in_delayed_cksum(m0);
809 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
812 if (len > PAGE_SIZE) {
814 * Fragment large datagrams such that each segment
815 * contains a multiple of PAGE_SIZE amount of data,
816 * plus headers. This enables a receiver to perform
817 * page-flipping zero-copy optimizations.
819 * XXX When does this help given that sender and receiver
820 * could have different page sizes, and also mtu could
821 * be less than the receiver's page size ?
826 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
830 * firstlen (off - hlen) must be aligned on an
834 goto smart_frag_failure;
835 off = ((off - hlen) & ~7) + hlen;
836 newlen = (~PAGE_MASK) & mtu;
837 if ((newlen + sizeof(struct ip)) > mtu) {
838 /* we failed, go back the default */
849 firstlen = off - hlen;
850 mnext = &m0->m_nextpkt; /* pointer to next packet */
853 * Loop through length of segment after first fragment,
854 * make new header and copy data of each part and link onto chain.
855 * Here, m0 is the original packet, m is the fragment being created.
856 * The fragments are linked off the m_nextpkt of the original
857 * packet, which after processing serves as the first fragment.
859 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
860 struct ip *mhip; /* ip header on the fragment */
862 int mhlen = sizeof(struct ip);
864 MGETHDR(m, M_NOWAIT, MT_HEADER);
867 ipstat.ips_odropped++;
870 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
872 * In the first mbuf, leave room for the link header, then
873 * copy the original IP header including options. The payload
874 * goes into an additional mbuf chain returned by m_copy().
876 m->m_data += max_linkhdr;
877 mhip = mtod(m, struct ip *);
879 if (hlen > sizeof(struct ip)) {
880 mhlen = ip_optcopy(ip, mhip) + sizeof(struct ip);
881 mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
884 /* XXX do we need to add ip->ip_off below ? */
885 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
886 if (off + len >= ip->ip_len) { /* last fragment */
887 len = ip->ip_len - off;
888 m->m_flags |= M_LASTFRAG;
890 mhip->ip_off |= IP_MF;
891 mhip->ip_len = htons((u_short)(len + mhlen));
892 m->m_next = m_copy(m0, off, len);
893 if (m->m_next == NULL) { /* copy failed */
895 error = ENOBUFS; /* ??? */
896 ipstat.ips_odropped++;
899 m->m_pkthdr.len = mhlen + len;
900 m->m_pkthdr.rcvif = NULL;
901 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
902 m->m_pkthdr.csum_iphlen = mhlen;
903 mhip->ip_off = htons(mhip->ip_off);
905 if (sw_csum & CSUM_DELAY_IP)
906 mhip->ip_sum = in_cksum(m, mhlen);
908 mnext = &m->m_nextpkt;
910 ipstat.ips_ofragments += nfrags;
912 /* set first marker for fragment chain */
913 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
914 m0->m_pkthdr.csum_data = nfrags;
917 * Update first fragment by trimming what's been copied out
918 * and updating header.
920 m_adj(m0, hlen + firstlen - ip->ip_len);
921 m0->m_pkthdr.len = hlen + firstlen;
922 ip->ip_len = htons((u_short)m0->m_pkthdr.len);
924 ip->ip_off = htons(ip->ip_off);
926 if (sw_csum & CSUM_DELAY_IP)
927 ip->ip_sum = in_cksum(m0, hlen);
935 in_delayed_cksum(struct mbuf *m)
938 u_short csum, offset;
940 ip = mtod(m, struct ip *);
941 offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
942 csum = in_cksum_skip(m, ip->ip_len, offset);
943 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
945 offset += m->m_pkthdr.csum_data; /* checksum offset */
947 if (offset + sizeof(u_short) > m->m_len) {
948 kprintf("delayed m_pullup, m->len: %d off: %d p: %d\n",
949 m->m_len, offset, ip->ip_p);
952 * this shouldn't happen, but if it does, the
953 * correct behavior may be to insert the checksum
954 * in the existing chain instead of rearranging it.
956 m = m_pullup(m, offset + sizeof(u_short));
958 *(u_short *)(m->m_data + offset) = csum;
962 * Insert IP options into preformed packet.
963 * Adjust IP destination as required for IP source routing,
964 * as indicated by a non-zero in_addr at the start of the options.
966 * XXX This routine assumes that the packet has no options in place.
969 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
971 struct ipoption *p = mtod(opt, struct ipoption *);
973 struct ip *ip = mtod(m, struct ip *);
976 optlen = opt->m_len - sizeof p->ipopt_dst;
977 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) {
979 return (m); /* XXX should fail */
981 if (p->ipopt_dst.s_addr)
982 ip->ip_dst = p->ipopt_dst;
983 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
984 MGETHDR(n, M_NOWAIT, MT_HEADER);
989 n->m_pkthdr.rcvif = NULL;
990 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
991 m->m_len -= sizeof(struct ip);
992 m->m_data += sizeof(struct ip);
995 m->m_len = optlen + sizeof(struct ip);
996 m->m_data += max_linkhdr;
997 memcpy(mtod(m, void *), ip, sizeof(struct ip));
1001 m->m_pkthdr.len += optlen;
1002 bcopy(ip, mtod(m, caddr_t), sizeof(struct ip));
1004 ip = mtod(m, struct ip *);
1005 bcopy(p->ipopt_list, ip + 1, optlen);
1006 *phlen = sizeof(struct ip) + optlen;
1007 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1008 ip->ip_len += optlen;
1013 * Copy options from ip to jp,
1014 * omitting those not copied during fragmentation.
1017 ip_optcopy(struct ip *ip, struct ip *jp)
1020 int opt, optlen, cnt;
1022 cp = (u_char *)(ip + 1);
1023 dp = (u_char *)(jp + 1);
1024 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip);
1025 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1027 if (opt == IPOPT_EOL)
1029 if (opt == IPOPT_NOP) {
1030 /* Preserve for IP mcast tunnel's LSRR alignment. */
1036 KASSERT(cnt >= IPOPT_OLEN + sizeof *cp,
1037 ("ip_optcopy: malformed ipv4 option"));
1038 optlen = cp[IPOPT_OLEN];
1039 KASSERT(optlen >= IPOPT_OLEN + sizeof *cp && optlen <= cnt,
1040 ("ip_optcopy: malformed ipv4 option"));
1042 /* bogus lengths should have been caught by ip_dooptions */
1045 if (IPOPT_COPIED(opt)) {
1046 bcopy(cp, dp, optlen);
1050 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1056 * IP socket option processing.
1059 ip_ctloutput(netmsg_t msg)
1061 struct socket *so = msg->base.nm_so;
1062 struct sockopt *sopt = msg->ctloutput.nm_sopt;
1063 struct inpcb *inp = so->so_pcb;
1068 /* Get socket's owner cpuid hint */
1069 if (sopt->sopt_level == SOL_SOCKET &&
1070 sopt->sopt_dir == SOPT_GET &&
1071 sopt->sopt_name == SO_CPUHINT) {
1073 soopt_from_kbuf(sopt, &optval, sizeof(optval));
1077 if (sopt->sopt_level != IPPROTO_IP) {
1082 switch (sopt->sopt_name) {
1083 case IP_MULTICAST_IF:
1084 case IP_MULTICAST_VIF:
1085 case IP_MULTICAST_TTL:
1086 case IP_MULTICAST_LOOP:
1087 case IP_ADD_MEMBERSHIP:
1088 case IP_DROP_MEMBERSHIP:
1090 * Handle multicast options in netisr0
1092 if (&curthread->td_msgport != netisr_cpuport(0)) {
1093 /* NOTE: so_port MUST NOT be checked in netisr0 */
1094 msg->lmsg.ms_flags |= MSGF_IGNSOPORT;
1095 lwkt_forwardmsg(netisr_cpuport(0), &msg->lmsg);
1101 switch (sopt->sopt_dir) {
1103 switch (sopt->sopt_name) {
1110 if (sopt->sopt_valsize > MLEN) {
1114 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_HEADER);
1119 m->m_len = sopt->sopt_valsize;
1120 error = soopt_to_kbuf(sopt, mtod(m, void *), m->m_len,
1122 error = ip_pcbopts(sopt->sopt_name,
1123 &inp->inp_options, m);
1131 case IP_RECVRETOPTS:
1132 case IP_RECVDSTADDR:
1135 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1139 switch (sopt->sopt_name) {
1141 inp->inp_ip_tos = optval;
1145 inp->inp_ip_ttl = optval;
1148 if (optval >= 0 && optval <= MAXTTL)
1149 inp->inp_ip_minttl = optval;
1153 #define OPTSET(bit) \
1155 inp->inp_flags |= bit; \
1157 inp->inp_flags &= ~bit;
1160 OPTSET(INP_RECVOPTS);
1163 case IP_RECVRETOPTS:
1164 OPTSET(INP_RECVRETOPTS);
1167 case IP_RECVDSTADDR:
1168 OPTSET(INP_RECVDSTADDR);
1176 OPTSET(INP_RECVTTL);
1182 case IP_MULTICAST_IF:
1183 case IP_MULTICAST_VIF:
1184 case IP_MULTICAST_TTL:
1185 case IP_MULTICAST_LOOP:
1186 case IP_ADD_MEMBERSHIP:
1187 case IP_DROP_MEMBERSHIP:
1188 error = ip_setmoptions(sopt, &inp->inp_moptions);
1192 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1198 case IP_PORTRANGE_DEFAULT:
1199 inp->inp_flags &= ~(INP_LOWPORT);
1200 inp->inp_flags &= ~(INP_HIGHPORT);
1203 case IP_PORTRANGE_HIGH:
1204 inp->inp_flags &= ~(INP_LOWPORT);
1205 inp->inp_flags |= INP_HIGHPORT;
1208 case IP_PORTRANGE_LOW:
1209 inp->inp_flags &= ~(INP_HIGHPORT);
1210 inp->inp_flags |= INP_LOWPORT;
1221 error = ENOPROTOOPT;
1227 switch (sopt->sopt_name) {
1230 if (inp->inp_options)
1231 soopt_from_kbuf(sopt, mtod(inp->inp_options,
1233 inp->inp_options->m_len);
1235 sopt->sopt_valsize = 0;
1242 case IP_RECVRETOPTS:
1243 case IP_RECVDSTADDR:
1247 switch (sopt->sopt_name) {
1250 optval = inp->inp_ip_tos;
1254 optval = inp->inp_ip_ttl;
1257 optval = inp->inp_ip_minttl;
1260 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1263 optval = OPTBIT(INP_RECVOPTS);
1266 case IP_RECVRETOPTS:
1267 optval = OPTBIT(INP_RECVRETOPTS);
1270 case IP_RECVDSTADDR:
1271 optval = OPTBIT(INP_RECVDSTADDR);
1275 optval = OPTBIT(INP_RECVTTL);
1279 optval = OPTBIT(INP_RECVIF);
1283 if (inp->inp_flags & INP_HIGHPORT)
1284 optval = IP_PORTRANGE_HIGH;
1285 else if (inp->inp_flags & INP_LOWPORT)
1286 optval = IP_PORTRANGE_LOW;
1291 soopt_from_kbuf(sopt, &optval, sizeof optval);
1294 case IP_MULTICAST_IF:
1295 case IP_MULTICAST_VIF:
1296 case IP_MULTICAST_TTL:
1297 case IP_MULTICAST_LOOP:
1298 case IP_ADD_MEMBERSHIP:
1299 case IP_DROP_MEMBERSHIP:
1300 error = ip_getmoptions(sopt, inp->inp_moptions);
1304 error = ENOPROTOOPT;
1310 lwkt_replymsg(&msg->lmsg, error);
1314 * Set up IP options in pcb for insertion in output packets.
1315 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1316 * with destination address if source routed.
1319 ip_pcbopts(int optname, struct mbuf **pcbopt, struct mbuf *m)
1325 /* turn off any old options */
1329 if (m == NULL || m->m_len == 0) {
1331 * Only turning off any previous options.
1338 if (m->m_len % sizeof(int32_t))
1341 * IP first-hop destination address will be stored before
1342 * actual options; move other options back
1343 * and clear it when none present.
1345 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1348 m->m_len += sizeof(struct in_addr);
1349 cp = mtod(m, u_char *) + sizeof(struct in_addr);
1350 bcopy(mtod(m, caddr_t), cp, cnt);
1351 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1353 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1354 opt = cp[IPOPT_OPTVAL];
1355 if (opt == IPOPT_EOL)
1357 if (opt == IPOPT_NOP)
1360 if (cnt < IPOPT_OLEN + sizeof *cp)
1362 optlen = cp[IPOPT_OLEN];
1363 if (optlen < IPOPT_OLEN + sizeof *cp || optlen > cnt)
1374 * user process specifies route as:
1376 * D must be our final destination (but we can't
1377 * check that since we may not have connected yet).
1378 * A is first hop destination, which doesn't appear in
1379 * actual IP option, but is stored before the options.
1381 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1383 m->m_len -= sizeof(struct in_addr);
1384 cnt -= sizeof(struct in_addr);
1385 optlen -= sizeof(struct in_addr);
1386 cp[IPOPT_OLEN] = optlen;
1388 * Move first hop before start of options.
1390 bcopy(&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1391 sizeof(struct in_addr));
1393 * Then copy rest of options back
1394 * to close up the deleted entry.
1396 bcopy(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr),
1397 &cp[IPOPT_OFFSET+1],
1398 cnt - (IPOPT_MINOFF - 1));
1402 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1414 * The whole multicast option thing needs to be re-thought.
1415 * Several of these options are equally applicable to non-multicast
1416 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1417 * standard option (IP_TTL).
1421 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1423 static struct ifnet *
1424 ip_multicast_if(struct in_addr *a, int *ifindexp)
1431 if (ntohl(a->s_addr) >> 24 == 0) {
1432 ifindex = ntohl(a->s_addr) & 0xffffff;
1433 if (ifindex < 0 || if_index < ifindex)
1435 ifp = ifindex2ifnet[ifindex];
1437 *ifindexp = ifindex;
1439 ifp = INADDR_TO_IFP(a);
1445 * Set the IP multicast options in response to user setsockopt().
1448 ip_setmoptions(struct sockopt *sopt, struct ip_moptions **imop)
1452 struct in_addr addr;
1453 struct ip_mreq mreq;
1455 struct ip_moptions *imo = *imop;
1460 * No multicast option buffer attached to the pcb;
1461 * allocate one and initialize to default values.
1463 imo = kmalloc(sizeof *imo, M_IPMOPTS, M_WAITOK);
1465 imo->imo_multicast_ifp = NULL;
1466 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1467 imo->imo_multicast_vif = -1;
1468 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1469 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1470 imo->imo_num_memberships = 0;
1471 /* Assign imo to imop after all fields are setup */
1475 switch (sopt->sopt_name) {
1476 /* store an index number for the vif you wanna use in the send */
1477 case IP_MULTICAST_VIF:
1478 if (legal_vif_num == 0) {
1482 error = soopt_to_kbuf(sopt, &i, sizeof i, sizeof i);
1485 if (!legal_vif_num(i) && (i != -1)) {
1489 imo->imo_multicast_vif = i;
1492 case IP_MULTICAST_IF:
1494 * Select the interface for outgoing multicast packets.
1496 error = soopt_to_kbuf(sopt, &addr, sizeof addr, sizeof addr);
1501 * INADDR_ANY is used to remove a previous selection.
1502 * When no interface is selected, a default one is
1503 * chosen every time a multicast packet is sent.
1505 if (addr.s_addr == INADDR_ANY) {
1506 imo->imo_multicast_ifp = NULL;
1510 * The selected interface is identified by its local
1511 * IP address. Find the interface and confirm that
1512 * it supports multicasting.
1515 ifp = ip_multicast_if(&addr, &ifindex);
1516 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
1518 error = EADDRNOTAVAIL;
1521 imo->imo_multicast_ifp = ifp;
1523 imo->imo_multicast_addr = addr;
1525 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1529 case IP_MULTICAST_TTL:
1531 * Set the IP time-to-live for outgoing multicast packets.
1532 * The original multicast API required a char argument,
1533 * which is inconsistent with the rest of the socket API.
1534 * We allow either a char or an int.
1536 if (sopt->sopt_valsize == 1) {
1538 error = soopt_to_kbuf(sopt, &ttl, 1, 1);
1541 imo->imo_multicast_ttl = ttl;
1544 error = soopt_to_kbuf(sopt, &ttl, sizeof ttl, sizeof ttl);
1550 imo->imo_multicast_ttl = ttl;
1554 case IP_MULTICAST_LOOP:
1556 * Set the loopback flag for outgoing multicast packets.
1557 * Must be zero or one. The original multicast API required a
1558 * char argument, which is inconsistent with the rest
1559 * of the socket API. We allow either a char or an int.
1561 if (sopt->sopt_valsize == 1) {
1564 error = soopt_to_kbuf(sopt, &loop, 1, 1);
1567 imo->imo_multicast_loop = !!loop;
1571 error = soopt_to_kbuf(sopt, &loop, sizeof loop,
1575 imo->imo_multicast_loop = !!loop;
1579 case IP_ADD_MEMBERSHIP:
1581 * Add a multicast group membership.
1582 * Group must be a valid IP multicast address.
1584 error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq);
1588 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1594 * If no interface address was provided, use the interface of
1595 * the route to the given multicast address.
1597 if (mreq.imr_interface.s_addr == INADDR_ANY) {
1598 struct sockaddr_in dst;
1601 bzero(&dst, sizeof(struct sockaddr_in));
1602 dst.sin_len = sizeof(struct sockaddr_in);
1603 dst.sin_family = AF_INET;
1604 dst.sin_addr = mreq.imr_multiaddr;
1605 rt = rtlookup((struct sockaddr *)&dst);
1607 error = EADDRNOTAVAIL;
1614 ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1618 * See if we found an interface, and confirm that it
1619 * supports multicast.
1621 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
1622 error = EADDRNOTAVAIL;
1627 * See if the membership already exists or if all the
1628 * membership slots are full.
1630 for (i = 0; i < imo->imo_num_memberships; ++i) {
1631 if (imo->imo_membership[i]->inm_ifp == ifp &&
1632 imo->imo_membership[i]->inm_addr.s_addr
1633 == mreq.imr_multiaddr.s_addr)
1636 if (i < imo->imo_num_memberships) {
1641 if (i == IP_MAX_MEMBERSHIPS) {
1642 error = ETOOMANYREFS;
1647 * Everything looks good; add a new record to the multicast
1648 * address list for the given interface.
1650 if ((imo->imo_membership[i] =
1651 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1656 ++imo->imo_num_memberships;
1660 case IP_DROP_MEMBERSHIP:
1662 * Drop a multicast group membership.
1663 * Group must be a valid IP multicast address.
1665 error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq);
1669 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1676 * If an interface address was specified, get a pointer
1677 * to its ifnet structure.
1679 if (mreq.imr_interface.s_addr == INADDR_ANY)
1682 ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1684 error = EADDRNOTAVAIL;
1690 * Find the membership in the membership array.
1692 for (i = 0; i < imo->imo_num_memberships; ++i) {
1694 imo->imo_membership[i]->inm_ifp == ifp) &&
1695 imo->imo_membership[i]->inm_addr.s_addr ==
1696 mreq.imr_multiaddr.s_addr)
1699 if (i == imo->imo_num_memberships) {
1700 error = EADDRNOTAVAIL;
1705 * Give up the multicast address record to which the
1706 * membership points.
1708 in_delmulti(imo->imo_membership[i]);
1710 * Remove the gap in the membership array.
1712 for (++i; i < imo->imo_num_memberships; ++i)
1713 imo->imo_membership[i-1] = imo->imo_membership[i];
1714 --imo->imo_num_memberships;
1727 * Return the IP multicast options in response to user getsockopt().
1730 ip_getmoptions(struct sockopt *sopt, struct ip_moptions *imo)
1732 struct in_addr addr;
1733 struct in_ifaddr *ia;
1738 switch (sopt->sopt_name) {
1739 case IP_MULTICAST_VIF:
1741 optval = imo->imo_multicast_vif;
1744 soopt_from_kbuf(sopt, &optval, sizeof optval);
1747 case IP_MULTICAST_IF:
1748 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1749 addr.s_addr = INADDR_ANY;
1750 else if (imo->imo_multicast_addr.s_addr) {
1751 /* return the value user has set */
1752 addr = imo->imo_multicast_addr;
1754 ia = IFP_TO_IA(imo->imo_multicast_ifp);
1755 addr.s_addr = (ia == NULL) ? INADDR_ANY
1756 : IA_SIN(ia)->sin_addr.s_addr;
1758 soopt_from_kbuf(sopt, &addr, sizeof addr);
1761 case IP_MULTICAST_TTL:
1763 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1765 optval = coptval = imo->imo_multicast_ttl;
1766 if (sopt->sopt_valsize == 1)
1767 soopt_from_kbuf(sopt, &coptval, 1);
1769 soopt_from_kbuf(sopt, &optval, sizeof optval);
1772 case IP_MULTICAST_LOOP:
1774 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1776 optval = coptval = imo->imo_multicast_loop;
1777 if (sopt->sopt_valsize == 1)
1778 soopt_from_kbuf(sopt, &coptval, 1);
1780 soopt_from_kbuf(sopt, &optval, sizeof optval);
1784 error = ENOPROTOOPT;
1791 * Discard the IP multicast options.
1794 ip_freemoptions(struct ip_moptions *imo)
1799 for (i = 0; i < imo->imo_num_memberships; ++i)
1800 in_delmulti(imo->imo_membership[i]);
1801 kfree(imo, M_IPMOPTS);
1806 * Routine called from ip_output() to loop back a copy of an IP multicast
1807 * packet to the input queue of a specified interface. Note that this
1808 * calls the output routine of the loopback "driver", but with an interface
1809 * pointer that might NOT be a loopback interface -- evil, but easier than
1810 * replicating that code here.
1813 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1819 copym = m_copypacket(m, M_NOWAIT);
1820 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1821 copym = m_pullup(copym, hlen);
1822 if (copym != NULL) {
1824 * if the checksum hasn't been computed, mark it as valid
1826 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1827 in_delayed_cksum(copym);
1828 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1829 copym->m_pkthdr.csum_flags |=
1830 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1831 copym->m_pkthdr.csum_data = 0xffff;
1834 * We don't bother to fragment if the IP length is greater
1835 * than the interface's MTU. Can this possibly matter?
1837 ip = mtod(copym, struct ip *);
1838 ip->ip_len = htons(ip->ip_len);
1839 ip->ip_off = htons(ip->ip_off);
1841 if (ip->ip_vhl == IP_VHL_BORING) {
1842 ip->ip_sum = in_cksum_hdr(ip);
1844 ip->ip_sum = in_cksum(copym, hlen);
1848 * It's not clear whether there are any lingering
1849 * reentrancy problems in other areas which might
1850 * be exposed by using ip_input directly (in
1851 * particular, everything which modifies the packet
1852 * in-place). Yet another option is using the
1853 * protosw directly to deliver the looped back
1854 * packet. For the moment, we'll err on the side
1855 * of safety by using if_simloop().
1858 if (dst->sin_family != AF_INET) {
1859 kprintf("ip_mloopback: bad address family %d\n",
1861 dst->sin_family = AF_INET;
1864 if_simloop(ifp, copym, dst->sin_family, 0);