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 $
31 * $DragonFly: src/sys/netinet/ip_output.c,v 1.63 2008/09/13 12:57:07 sephe Exp $
38 #include "opt_ipdivert.h"
39 #include "opt_ipfilter.h"
40 #include "opt_ipsec.h"
41 #include "opt_mbuf_stress_test.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/thread2.h>
55 #include <sys/in_cksum.h>
58 #include <net/netisr.h>
60 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/in_pcb.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip_var.h>
69 #include <netproto/mpls/mpls_var.h>
71 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
74 #include <netinet6/ipsec.h>
75 #include <netproto/key/key.h>
77 #include <netproto/key/key_debug.h>
79 #define KEYDEBUG(lev,arg)
84 #include <netproto/ipsec/ipsec.h>
85 #include <netproto/ipsec/xform.h>
86 #include <netproto/ipsec/key.h>
89 #include <net/ipfw/ip_fw.h>
90 #include <net/dummynet/ip_dummynet.h>
92 #define print_ip(x, a, y) kprintf("%s %d.%d.%d.%d%s",\
93 x, (ntohl(a.s_addr)>>24)&0xFF,\
94 (ntohl(a.s_addr)>>16)&0xFF,\
95 (ntohl(a.s_addr)>>8)&0xFF,\
96 (ntohl(a.s_addr))&0xFF, y);
100 #ifdef MBUF_STRESS_TEST
101 int mbuf_frag_size = 0;
102 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
103 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
106 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
107 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
108 static void ip_mloopback
109 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
110 static int ip_getmoptions
111 (struct sockopt *, struct ip_moptions *);
112 static int ip_pcbopts(int, struct mbuf **, struct mbuf *);
113 static int ip_setmoptions
114 (struct sockopt *, struct ip_moptions **);
116 int ip_optcopy(struct ip *, struct ip *);
118 extern int route_assert_owner_access;
119 extern void db_print_backtrace(void);
121 extern struct protosw inetsw[];
124 ip_localforward(struct mbuf *m, const struct sockaddr_in *dst)
126 struct in_ifaddr_container *iac;
129 * We need to figure out if we have been forwarded to a local
130 * socket. If so, then we should somehow "loop back" to
131 * ip_input(), and get directed to the PCB as if we had received
132 * this packet. This is because it may be difficult to identify
133 * the packets you want to forward until they are being output
134 * and have selected an interface (e.g. locally initiated
135 * packets). If we used the loopback inteface, we would not be
136 * able to control what happens as the packet runs through
137 * ip_input() as it is done through a ISR.
139 LIST_FOREACH(iac, INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
141 * If the addr to forward to is one of ours, we pretend
142 * to be the destination for this packet.
144 if (IA_SIN(iac->ia)->sin_addr.s_addr == dst->sin_addr.s_addr)
148 struct ip *ip = mtod(m, struct ip *);
150 if (m->m_pkthdr.rcvif == NULL)
151 m->m_pkthdr.rcvif = ifunit("lo0");
152 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
153 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
155 m->m_pkthdr.csum_data = 0xffff;
157 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED | CSUM_IP_VALID;
159 ip->ip_len = htons(ip->ip_len);
160 ip->ip_off = htons(ip->ip_off);
163 return 1; /* Packet gets forwarded locally */
169 * IP output. The packet in mbuf chain m contains a skeletal IP
170 * header (with len, off, ttl, proto, tos, src, dst).
171 * The mbuf chain containing the packet will be freed.
172 * The mbuf opt, if present, will not be freed.
175 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro,
176 int flags, struct ip_moptions *imo, struct inpcb *inp)
179 struct ifnet *ifp = NULL; /* keep compiler happy */
181 int hlen = sizeof(struct ip);
183 struct sockaddr_in *dst = NULL; /* keep compiler happy */
184 struct in_ifaddr *ia = NULL;
185 int isbroadcast, sw_csum;
186 struct in_addr pkt_dst;
187 struct route iproute;
190 struct secpolicy *sp = NULL;
191 struct socket *so = inp ? inp->inp_socket : NULL;
194 struct secpolicy *sp = NULL;
195 struct tdb_ident *tdbi;
196 #endif /* FAST_IPSEC */
197 struct sockaddr_in *next_hop = NULL;
198 int src_was_INADDR_ANY = 0; /* as the name says... */
205 bzero(ro, sizeof *ro);
206 } else if (ro->ro_rt != NULL && ro->ro_rt->rt_cpuid != mycpuid) {
207 if (flags & IP_DEBUGROUTE) {
208 if (route_assert_owner_access) {
210 "rt rt_cpuid %d accessed on cpu %d\n",
211 ro->ro_rt->rt_cpuid, mycpuid);
213 kprintf("ip_output: "
214 "rt rt_cpuid %d accessed on cpu %d\n",
215 ro->ro_rt->rt_cpuid, mycpuid);
216 db_print_backtrace();
222 * If the cached rtentry's owner CPU is not the current CPU,
223 * then don't touch the cached rtentry (remote free is too
224 * expensive in this context); just relocate the route.
227 bzero(ro, sizeof *ro);
230 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
232 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
233 KKASSERT(mtag != NULL);
234 next_hop = m_tag_data(mtag);
237 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
238 struct dn_pkt *dn_pkt;
240 /* Extract info from dummynet tag */
241 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
242 KKASSERT(mtag != NULL);
243 dn_pkt = m_tag_data(mtag);
246 * The packet was already tagged, so part of the
247 * processing was already done, and we need to go down.
248 * Get the calculated parameters from the tag.
252 KKASSERT(ro == &iproute);
253 *ro = dn_pkt->ro; /* structure copy */
254 KKASSERT(ro->ro_rt == NULL || ro->ro_rt->rt_cpuid == mycpuid);
256 dst = dn_pkt->dn_dst;
257 if (dst == (struct sockaddr_in *)&(dn_pkt->ro.ro_dst)) {
258 /* If 'dst' points into dummynet tag, adjust it */
259 dst = (struct sockaddr_in *)&(ro->ro_dst);
262 ip = mtod(m, struct ip *);
263 hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
265 ia = ifatoia(ro->ro_rt->rt_ifa);
271 m = ip_insertoptions(m, opt, &len);
275 ip = mtod(m, struct ip *);
280 if (!(flags & (IP_FORWARDING|IP_RAWOUTPUT))) {
281 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
283 ip->ip_id = ip_newid();
284 ipstat.ips_localout++;
286 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
290 pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst;
292 dst = (struct sockaddr_in *)&ro->ro_dst;
294 * If there is a cached route,
295 * check that it is to the same destination
296 * and is still up. If not, free it and try again.
297 * The address family should also be checked in case of sharing the
301 (!(ro->ro_rt->rt_flags & RTF_UP) ||
302 dst->sin_family != AF_INET ||
303 dst->sin_addr.s_addr != pkt_dst.s_addr)) {
305 ro->ro_rt = (struct rtentry *)NULL;
307 if (ro->ro_rt == NULL) {
308 bzero(dst, sizeof *dst);
309 dst->sin_family = AF_INET;
310 dst->sin_len = sizeof *dst;
311 dst->sin_addr = pkt_dst;
314 * If routing to interface only,
315 * short circuit routing lookup.
317 if (flags & IP_ROUTETOIF) {
318 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
319 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
320 ipstat.ips_noroute++;
326 isbroadcast = in_broadcast(dst->sin_addr, ifp);
327 } else if (IN_MULTICAST(ntohl(pkt_dst.s_addr)) &&
328 imo != NULL && imo->imo_multicast_ifp != NULL) {
330 * Bypass the normal routing lookup for multicast
331 * packets if the interface is specified.
333 ifp = imo->imo_multicast_ifp;
335 isbroadcast = 0; /* fool gcc */
338 * If this is the case, we probably don't want to allocate
339 * a protocol-cloned route since we didn't get one from the
340 * ULP. This lets TCP do its thing, while not burdening
341 * forwarding or ICMP with the overhead of cloning a route.
342 * Of course, we still want to do any cloning requested by
343 * the link layer, as this is probably required in all cases
344 * for correct operation (as it is for ARP).
346 if (ro->ro_rt == NULL)
347 rtalloc_ign(ro, RTF_PRCLONING);
348 if (ro->ro_rt == NULL) {
349 ipstat.ips_noroute++;
350 error = EHOSTUNREACH;
353 ia = ifatoia(ro->ro_rt->rt_ifa);
354 ifp = ro->ro_rt->rt_ifp;
356 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
357 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
358 if (ro->ro_rt->rt_flags & RTF_HOST)
359 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
361 isbroadcast = in_broadcast(dst->sin_addr, ifp);
363 if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
364 struct in_multi *inm;
366 m->m_flags |= M_MCAST;
368 * IP destination address is multicast. Make sure "dst"
369 * still points to the address in "ro". (It may have been
370 * changed to point to a gateway address, above.)
372 dst = (struct sockaddr_in *)&ro->ro_dst;
374 * See if the caller provided any multicast options
377 ip->ip_ttl = imo->imo_multicast_ttl;
378 if (imo->imo_multicast_vif != -1)
381 ip_mcast_src(imo->imo_multicast_vif) :
384 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
386 * Confirm that the outgoing interface supports multicast.
388 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
389 if (!(ifp->if_flags & IFF_MULTICAST)) {
390 ipstat.ips_noroute++;
396 * If source address not specified yet, use address
397 * of outgoing interface.
399 if (ip->ip_src.s_addr == INADDR_ANY) {
400 /* Interface may have no addresses. */
402 ip->ip_src = IA_SIN(ia)->sin_addr;
405 IN_LOOKUP_MULTI(pkt_dst, ifp, inm);
407 (imo == NULL || imo->imo_multicast_loop)) {
409 * If we belong to the destination multicast group
410 * on the outgoing interface, and the caller did not
411 * forbid loopback, loop back a copy.
413 ip_mloopback(ifp, m, dst, hlen);
417 * If we are acting as a multicast router, perform
418 * multicast forwarding as if the packet had just
419 * arrived on the interface to which we are about
420 * to send. The multicast forwarding function
421 * recursively calls this function, using the
422 * IP_FORWARDING flag to prevent infinite recursion.
424 * Multicasts that are looped back by ip_mloopback(),
425 * above, will be forwarded by the ip_input() routine,
428 if (ip_mrouter && !(flags & IP_FORWARDING)) {
430 * If rsvp daemon is not running, do not
431 * set ip_moptions. This ensures that the packet
432 * is multicast and not just sent down one link
433 * as prescribed by rsvpd.
438 ip_mforward(ip, ifp, m, imo) != 0) {
446 * Multicasts with a time-to-live of zero may be looped-
447 * back, above, but must not be transmitted on a network.
448 * Also, multicasts addressed to the loopback interface
449 * are not sent -- the above call to ip_mloopback() will
450 * loop back a copy if this host actually belongs to the
451 * destination group on the loopback interface.
453 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
460 m->m_flags &= ~M_MCAST;
464 * If the source address is not specified yet, use the address
465 * of the outoing interface. In case, keep note we did that, so
466 * if the the firewall changes the next-hop causing the output
467 * interface to change, we can fix that.
469 if (ip->ip_src.s_addr == INADDR_ANY || src_was_INADDR_ANY) {
470 /* Interface may have no addresses. */
472 ip->ip_src = IA_SIN(ia)->sin_addr;
473 src_was_INADDR_ANY = 1;
479 * Disable packet drop hack.
480 * Packetdrop should be done by queueing.
484 * Verify that we have any chance at all of being able to queue
485 * the packet or packet fragments
487 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
488 ifp->if_snd.ifq_maxlen) {
490 ipstat.ips_odropped++;
496 * Look for broadcast address and
497 * verify user is allowed to send
501 if (!(ifp->if_flags & IFF_BROADCAST)) {
502 error = EADDRNOTAVAIL;
505 if (!(flags & IP_ALLOWBROADCAST)) {
509 /* don't allow broadcast messages to be fragmented */
510 if (ip->ip_len > ifp->if_mtu) {
514 m->m_flags |= M_BCAST;
516 m->m_flags &= ~M_BCAST;
521 /* get SP for this packet */
523 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
525 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
528 ipsecstat.out_inval++;
535 switch (sp->policy) {
536 case IPSEC_POLICY_DISCARD:
538 * This packet is just discarded.
540 ipsecstat.out_polvio++;
543 case IPSEC_POLICY_BYPASS:
544 case IPSEC_POLICY_NONE:
545 /* no need to do IPsec. */
548 case IPSEC_POLICY_IPSEC:
549 if (sp->req == NULL) {
550 /* acquire a policy */
551 error = key_spdacquire(sp);
556 case IPSEC_POLICY_ENTRUST:
558 kprintf("ip_output: Invalid policy found. %d\n", sp->policy);
561 struct ipsec_output_state state;
562 bzero(&state, sizeof state);
564 if (flags & IP_ROUTETOIF) {
566 bzero(&iproute, sizeof iproute);
569 state.dst = (struct sockaddr *)dst;
575 * delayed checksums are not currently compatible with IPsec
577 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
579 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
582 ip->ip_len = htons(ip->ip_len);
583 ip->ip_off = htons(ip->ip_off);
585 error = ipsec4_output(&state, sp, flags);
588 if (flags & IP_ROUTETOIF) {
590 * if we have tunnel mode SA, we may need to ignore
593 if (state.ro != &iproute || state.ro->ro_rt != NULL) {
594 flags &= ~IP_ROUTETOIF;
599 dst = (struct sockaddr_in *)state.dst;
601 /* mbuf is already reclaimed in ipsec4_output. */
611 kprintf("ip4_output (ipsec): error code %d\n", error);
614 /* don't show these error codes to the user */
622 /* be sure to update variables that are affected by ipsec4_output() */
623 ip = mtod(m, struct ip *);
625 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
627 hlen = ip->ip_hl << 2;
629 if (ro->ro_rt == NULL) {
630 if (!(flags & IP_ROUTETOIF)) {
631 kprintf("ip_output: "
632 "can't update route after IPsec processing\n");
633 error = EHOSTUNREACH; /*XXX*/
637 ia = ifatoia(ro->ro_rt->rt_ifa);
638 ifp = ro->ro_rt->rt_ifp;
641 /* make it flipped, again. */
642 ip->ip_len = ntohs(ip->ip_len);
643 ip->ip_off = ntohs(ip->ip_off);
648 * Check the security policy (SP) for the packet and, if
649 * required, do IPsec-related processing. There are two
650 * cases here; the first time a packet is sent through
651 * it will be untagged and handled by ipsec4_checkpolicy.
652 * If the packet is resubmitted to ip_output (e.g. after
653 * AH, ESP, etc. processing), there will be a tag to bypass
654 * the lookup and related policy checking.
656 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
659 tdbi = (struct tdb_ident *)m_tag_data(mtag);
660 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
662 error = -EINVAL; /* force silent drop */
663 m_tag_delete(m, mtag);
665 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
669 * There are four return cases:
670 * sp != NULL apply IPsec policy
671 * sp == NULL, error == 0 no IPsec handling needed
672 * sp == NULL, error == -EINVAL discard packet w/o error
673 * sp == NULL, error != 0 discard packet, report error
676 /* Loop detection, check if ipsec processing already done */
677 KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
678 for (mtag = m_tag_first(m); mtag != NULL;
679 mtag = m_tag_next(m, mtag)) {
680 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
682 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
683 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
686 * Check if policy has an SA associated with it.
687 * This can happen when an SP has yet to acquire
688 * an SA; e.g. on first reference. If it occurs,
689 * then we let ipsec4_process_packet do its thing.
691 if (sp->req->sav == NULL)
693 tdbi = (struct tdb_ident *)m_tag_data(mtag);
694 if (tdbi->spi == sp->req->sav->spi &&
695 tdbi->proto == sp->req->sav->sah->saidx.proto &&
696 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
697 sizeof(union sockaddr_union)) == 0) {
699 * No IPsec processing is needed, free
702 * NB: null pointer to avoid free at
705 KEY_FREESP(&sp), sp = NULL;
712 * Do delayed checksums now because we send before
713 * this is done in the normal processing path.
715 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
717 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
720 ip->ip_len = htons(ip->ip_len);
721 ip->ip_off = htons(ip->ip_off);
723 /* NB: callee frees mbuf */
724 error = ipsec4_process_packet(m, sp->req, flags, 0);
726 * Preserve KAME behaviour: ENOENT can be returned
727 * when an SA acquire is in progress. Don't propagate
728 * this to user-level; it confuses applications.
730 * XXX this will go away when the SADB is redone.
741 * Hack: -EINVAL is used to signal that a packet
742 * should be silently discarded. This is typically
743 * because we asked key management for an SA and
744 * it was delayed (e.g. kicked up to IKE).
746 if (error == -EINVAL)
750 /* No IPsec processing for this packet. */
754 * If deferred crypto processing is needed, check that
755 * the interface supports it.
757 mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
758 if (mtag != NULL && !(ifp->if_capenable & IFCAP_IPSEC)) {
759 /* notify IPsec to do its own crypto */
760 ipsp_skipcrypto_unmark((struct tdb_ident *)m_tag_data(mtag));
761 error = EHOSTUNREACH;
767 #endif /* FAST_IPSEC */
769 /* We are already being fwd'd from a firewall. */
770 if (next_hop != NULL)
775 * - Xlate: translate packet's addr/port (NAT).
776 * - Firewall: deny/allow/etc.
777 * - Wrap: fake packet's addr/port <unimpl.>
778 * - Encapsulate: put it in another IP and send out. <unimp.>
782 * Run through list of hooks for output packets.
784 if (pfil_has_hooks(&inet_pfil_hook)) {
785 error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT);
786 if (error != 0 || m == NULL)
788 ip = mtod(m, struct ip *);
791 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
793 * Check dst to make sure it is directly reachable on the
794 * interface we previously thought it was.
795 * If it isn't (which may be likely in some situations) we have
796 * to re-route it (ie, find a route for the next-hop and the
797 * associated interface) and set them here. This is nested
798 * forwarding which in most cases is undesirable, except where
799 * such control is nigh impossible. So we do it here.
802 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
803 KKASSERT(mtag != NULL);
804 next_hop = m_tag_data(mtag);
807 * Try local forwarding first
809 if (ip_localforward(m, next_hop))
813 * Relocate the route based on next_hop.
814 * If the current route is inp's cache, keep it untouched.
816 if (ro == &iproute && ro->ro_rt != NULL) {
821 bzero(ro, sizeof *ro);
824 * Forwarding to broadcast address is not allowed.
825 * XXX Should we follow IP_ROUTETOIF?
827 flags &= ~(IP_ALLOWBROADCAST | IP_ROUTETOIF);
829 /* We are doing forwarding now */
830 flags |= IP_FORWARDING;
835 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
836 struct dn_pkt *dn_pkt;
838 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
839 KKASSERT(mtag != NULL);
840 dn_pkt = m_tag_data(mtag);
843 * Under certain cases it is not possible to recalculate
844 * 'ro' and 'dst', let alone 'flags', so just save them in
845 * dummynet tag and avoid the possible wrong reculcalation
846 * when we come back to ip_output() again.
848 * All other parameters have been already used and so they
849 * are not needed anymore.
850 * XXX if the ifp is deleted while a pkt is in dummynet,
851 * we are in trouble! (TODO use ifnet_detach_event)
853 * We need to copy *ro because for ICMP pkts (and maybe
854 * others) the caller passed a pointer into the stack;
855 * dst might also be a pointer into *ro so it needs to
860 ro->ro_rt->rt_refcnt++;
861 if (dst == (struct sockaddr_in *)&ro->ro_dst) {
862 /* 'dst' points into 'ro' */
863 dst = (struct sockaddr_in *)&(dn_pkt->ro.ro_dst);
865 dn_pkt->dn_dst = dst;
866 dn_pkt->flags = flags;
872 /* 127/8 must not appear on wire - RFC1122. */
873 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
874 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
875 if (!(ifp->if_flags & IFF_LOOPBACK)) {
876 ipstat.ips_badaddr++;
877 error = EADDRNOTAVAIL;
882 m->m_pkthdr.csum_flags |= CSUM_IP;
883 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
884 if (sw_csum & CSUM_DELAY_DATA) {
886 sw_csum &= ~CSUM_DELAY_DATA;
888 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
891 * If small enough for interface, or the interface will take
892 * care of the fragmentation for us, can just send directly.
894 if (ip->ip_len <= ifp->if_mtu || ((ifp->if_hwassist & CSUM_FRAGMENT) &&
895 !(ip->ip_off & IP_DF))) {
896 ip->ip_len = htons(ip->ip_len);
897 ip->ip_off = htons(ip->ip_off);
899 if (sw_csum & CSUM_DELAY_IP) {
900 if (ip->ip_vhl == IP_VHL_BORING) {
901 ip->ip_sum = in_cksum_hdr(ip);
903 ip->ip_sum = in_cksum(m, hlen);
907 /* Record statistics for this interface address. */
908 if (!(flags & IP_FORWARDING) && ia) {
909 ia->ia_ifa.if_opackets++;
910 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
914 /* clean ipsec history once it goes out of the node */
918 #ifdef MBUF_STRESS_TEST
919 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) {
920 struct mbuf *m1, *m2;
923 tmp = length = m->m_pkthdr.len;
925 while ((length -= mbuf_frag_size) >= 1) {
926 m1 = m_split(m, length, MB_DONTWAIT);
930 while (m2->m_next != NULL)
934 m->m_pkthdr.len = tmp;
939 if (!mpls_output_process(m, ro->ro_rt))
942 error = ifp->if_output(ifp, m, (struct sockaddr *)dst,
947 if (ip->ip_off & IP_DF) {
950 * This case can happen if the user changed the MTU
951 * of an interface after enabling IP on it. Because
952 * most netifs don't keep track of routes pointing to
953 * them, there is no way for one to update all its
954 * routes when the MTU is changed.
956 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
957 !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) &&
958 (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
959 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
961 ipstat.ips_cantfrag++;
966 * Too large for interface; fragment if possible. If successful,
967 * on return, m will point to a list of packets to be sent.
969 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
976 /* clean ipsec history once it goes out of the node */
980 /* Record statistics for this interface address. */
982 ia->ia_ifa.if_opackets++;
983 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
986 if (!mpls_output_process(m, ro->ro_rt))
989 error = ifp->if_output(ifp, m, (struct sockaddr *)dst,
997 ipstat.ips_fragmented++;
1000 if (ro == &iproute && ro->ro_rt != NULL) {
1006 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1007 kprintf("DP ip_output call free SP:%p\n", sp));
1022 * Create a chain of fragments which fit the given mtu. m_frag points to the
1023 * mbuf to be fragmented; on return it points to the chain with the fragments.
1024 * Return 0 if no error. If error, m_frag may contain a partially built
1025 * chain of fragments that should be freed by the caller.
1027 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
1028 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
1031 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
1032 u_long if_hwassist_flags, int sw_csum)
1035 int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
1036 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */
1038 struct mbuf *m0 = *m_frag; /* the original packet */
1040 struct mbuf **mnext;
1043 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */
1044 ipstat.ips_cantfrag++;
1049 * Must be able to put at least 8 bytes per fragment.
1055 * If the interface will not calculate checksums on
1056 * fragmented packets, then do it here.
1058 if ((m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) &&
1059 !(if_hwassist_flags & CSUM_IP_FRAGS)) {
1060 in_delayed_cksum(m0);
1061 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1064 if (len > PAGE_SIZE) {
1066 * Fragment large datagrams such that each segment
1067 * contains a multiple of PAGE_SIZE amount of data,
1068 * plus headers. This enables a receiver to perform
1069 * page-flipping zero-copy optimizations.
1071 * XXX When does this help given that sender and receiver
1072 * could have different page sizes, and also mtu could
1073 * be less than the receiver's page size ?
1078 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
1082 * firstlen (off - hlen) must be aligned on an
1086 goto smart_frag_failure;
1087 off = ((off - hlen) & ~7) + hlen;
1088 newlen = (~PAGE_MASK) & mtu;
1089 if ((newlen + sizeof(struct ip)) > mtu) {
1090 /* we failed, go back the default */
1101 firstlen = off - hlen;
1102 mnext = &m0->m_nextpkt; /* pointer to next packet */
1105 * Loop through length of segment after first fragment,
1106 * make new header and copy data of each part and link onto chain.
1107 * Here, m0 is the original packet, m is the fragment being created.
1108 * The fragments are linked off the m_nextpkt of the original
1109 * packet, which after processing serves as the first fragment.
1111 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
1112 struct ip *mhip; /* ip header on the fragment */
1114 int mhlen = sizeof(struct ip);
1116 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1119 ipstat.ips_odropped++;
1122 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
1124 * In the first mbuf, leave room for the link header, then
1125 * copy the original IP header including options. The payload
1126 * goes into an additional mbuf chain returned by m_copy().
1128 m->m_data += max_linkhdr;
1129 mhip = mtod(m, struct ip *);
1131 if (hlen > sizeof(struct ip)) {
1132 mhlen = ip_optcopy(ip, mhip) + sizeof(struct ip);
1133 mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
1136 /* XXX do we need to add ip->ip_off below ? */
1137 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
1138 if (off + len >= ip->ip_len) { /* last fragment */
1139 len = ip->ip_len - off;
1140 m->m_flags |= M_LASTFRAG;
1142 mhip->ip_off |= IP_MF;
1143 mhip->ip_len = htons((u_short)(len + mhlen));
1144 m->m_next = m_copy(m0, off, len);
1145 if (m->m_next == NULL) { /* copy failed */
1147 error = ENOBUFS; /* ??? */
1148 ipstat.ips_odropped++;
1151 m->m_pkthdr.len = mhlen + len;
1152 m->m_pkthdr.rcvif = (struct ifnet *)NULL;
1153 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
1154 mhip->ip_off = htons(mhip->ip_off);
1156 if (sw_csum & CSUM_DELAY_IP)
1157 mhip->ip_sum = in_cksum(m, mhlen);
1159 mnext = &m->m_nextpkt;
1161 ipstat.ips_ofragments += nfrags;
1163 /* set first marker for fragment chain */
1164 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1165 m0->m_pkthdr.csum_data = nfrags;
1168 * Update first fragment by trimming what's been copied out
1169 * and updating header.
1171 m_adj(m0, hlen + firstlen - ip->ip_len);
1172 m0->m_pkthdr.len = hlen + firstlen;
1173 ip->ip_len = htons((u_short)m0->m_pkthdr.len);
1174 ip->ip_off |= IP_MF;
1175 ip->ip_off = htons(ip->ip_off);
1177 if (sw_csum & CSUM_DELAY_IP)
1178 ip->ip_sum = in_cksum(m0, hlen);
1186 in_delayed_cksum(struct mbuf *m)
1189 u_short csum, offset;
1191 ip = mtod(m, struct ip *);
1192 offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
1193 csum = in_cksum_skip(m, ip->ip_len, offset);
1194 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1196 offset += m->m_pkthdr.csum_data; /* checksum offset */
1198 if (offset + sizeof(u_short) > m->m_len) {
1199 kprintf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1200 m->m_len, offset, ip->ip_p);
1203 * this shouldn't happen, but if it does, the
1204 * correct behavior may be to insert the checksum
1205 * in the existing chain instead of rearranging it.
1207 m = m_pullup(m, offset + sizeof(u_short));
1209 *(u_short *)(m->m_data + offset) = csum;
1213 * Insert IP options into preformed packet.
1214 * Adjust IP destination as required for IP source routing,
1215 * as indicated by a non-zero in_addr at the start of the options.
1217 * XXX This routine assumes that the packet has no options in place.
1219 static struct mbuf *
1220 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
1222 struct ipoption *p = mtod(opt, struct ipoption *);
1224 struct ip *ip = mtod(m, struct ip *);
1227 optlen = opt->m_len - sizeof p->ipopt_dst;
1228 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) {
1230 return (m); /* XXX should fail */
1232 if (p->ipopt_dst.s_addr)
1233 ip->ip_dst = p->ipopt_dst;
1234 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1235 MGETHDR(n, MB_DONTWAIT, MT_HEADER);
1240 n->m_pkthdr.rcvif = (struct ifnet *)NULL;
1241 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1242 m->m_len -= sizeof(struct ip);
1243 m->m_data += sizeof(struct ip);
1246 m->m_len = optlen + sizeof(struct ip);
1247 m->m_data += max_linkhdr;
1248 memcpy(mtod(m, void *), ip, sizeof(struct ip));
1250 m->m_data -= optlen;
1252 m->m_pkthdr.len += optlen;
1253 ovbcopy(ip, mtod(m, caddr_t), sizeof(struct ip));
1255 ip = mtod(m, struct ip *);
1256 bcopy(p->ipopt_list, ip + 1, optlen);
1257 *phlen = sizeof(struct ip) + optlen;
1258 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1259 ip->ip_len += optlen;
1264 * Copy options from ip to jp,
1265 * omitting those not copied during fragmentation.
1268 ip_optcopy(struct ip *ip, struct ip *jp)
1271 int opt, optlen, cnt;
1273 cp = (u_char *)(ip + 1);
1274 dp = (u_char *)(jp + 1);
1275 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip);
1276 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1278 if (opt == IPOPT_EOL)
1280 if (opt == IPOPT_NOP) {
1281 /* Preserve for IP mcast tunnel's LSRR alignment. */
1287 KASSERT(cnt >= IPOPT_OLEN + sizeof *cp,
1288 ("ip_optcopy: malformed ipv4 option"));
1289 optlen = cp[IPOPT_OLEN];
1290 KASSERT(optlen >= IPOPT_OLEN + sizeof *cp && optlen <= cnt,
1291 ("ip_optcopy: malformed ipv4 option"));
1293 /* bogus lengths should have been caught by ip_dooptions */
1296 if (IPOPT_COPIED(opt)) {
1297 bcopy(cp, dp, optlen);
1301 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1307 * IP socket option processing.
1310 ip_ctloutput(struct socket *so, struct sockopt *sopt)
1312 struct inpcb *inp = so->so_pcb;
1316 if (sopt->sopt_level != IPPROTO_IP) {
1320 switch (sopt->sopt_dir) {
1322 switch (sopt->sopt_name) {
1329 if (sopt->sopt_valsize > MLEN) {
1333 MGET(m, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_HEADER);
1338 m->m_len = sopt->sopt_valsize;
1339 error = soopt_to_kbuf(sopt, mtod(m, void *), m->m_len,
1341 return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1349 case IP_RECVRETOPTS:
1350 case IP_RECVDSTADDR:
1354 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1358 switch (sopt->sopt_name) {
1360 inp->inp_ip_tos = optval;
1364 inp->inp_ip_ttl = optval;
1367 if (optval > 0 && optval <= MAXTTL)
1368 inp->inp_ip_minttl = optval;
1372 #define OPTSET(bit) \
1374 inp->inp_flags |= bit; \
1376 inp->inp_flags &= ~bit;
1379 OPTSET(INP_RECVOPTS);
1382 case IP_RECVRETOPTS:
1383 OPTSET(INP_RECVRETOPTS);
1386 case IP_RECVDSTADDR:
1387 OPTSET(INP_RECVDSTADDR);
1395 OPTSET(INP_RECVTTL);
1405 case IP_MULTICAST_IF:
1406 case IP_MULTICAST_VIF:
1407 case IP_MULTICAST_TTL:
1408 case IP_MULTICAST_LOOP:
1409 case IP_ADD_MEMBERSHIP:
1410 case IP_DROP_MEMBERSHIP:
1411 error = ip_setmoptions(sopt, &inp->inp_moptions);
1415 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1421 case IP_PORTRANGE_DEFAULT:
1422 inp->inp_flags &= ~(INP_LOWPORT);
1423 inp->inp_flags &= ~(INP_HIGHPORT);
1426 case IP_PORTRANGE_HIGH:
1427 inp->inp_flags &= ~(INP_LOWPORT);
1428 inp->inp_flags |= INP_HIGHPORT;
1431 case IP_PORTRANGE_LOW:
1432 inp->inp_flags &= ~(INP_HIGHPORT);
1433 inp->inp_flags |= INP_LOWPORT;
1442 #if defined(IPSEC) || defined(FAST_IPSEC)
1443 case IP_IPSEC_POLICY:
1451 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1453 soopt_to_mbuf(sopt, m);
1454 priv = (sopt->sopt_td != NULL &&
1455 suser(sopt->sopt_td) != 0) ? 0 : 1;
1456 req = mtod(m, caddr_t);
1458 optname = sopt->sopt_name;
1459 error = ipsec4_set_policy(inp, optname, req, len, priv);
1466 error = ENOPROTOOPT;
1472 switch (sopt->sopt_name) {
1475 if (inp->inp_options)
1476 soopt_from_kbuf(sopt, mtod(inp->inp_options,
1478 inp->inp_options->m_len);
1480 sopt->sopt_valsize = 0;
1487 case IP_RECVRETOPTS:
1488 case IP_RECVDSTADDR:
1493 switch (sopt->sopt_name) {
1496 optval = inp->inp_ip_tos;
1500 optval = inp->inp_ip_ttl;
1503 optval = inp->inp_ip_minttl;
1506 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1509 optval = OPTBIT(INP_RECVOPTS);
1512 case IP_RECVRETOPTS:
1513 optval = OPTBIT(INP_RECVRETOPTS);
1516 case IP_RECVDSTADDR:
1517 optval = OPTBIT(INP_RECVDSTADDR);
1521 optval = OPTBIT(INP_RECVTTL);
1525 optval = OPTBIT(INP_RECVIF);
1529 if (inp->inp_flags & INP_HIGHPORT)
1530 optval = IP_PORTRANGE_HIGH;
1531 else if (inp->inp_flags & INP_LOWPORT)
1532 optval = IP_PORTRANGE_LOW;
1538 optval = OPTBIT(INP_FAITH);
1541 soopt_from_kbuf(sopt, &optval, sizeof optval);
1544 case IP_MULTICAST_IF:
1545 case IP_MULTICAST_VIF:
1546 case IP_MULTICAST_TTL:
1547 case IP_MULTICAST_LOOP:
1548 case IP_ADD_MEMBERSHIP:
1549 case IP_DROP_MEMBERSHIP:
1550 error = ip_getmoptions(sopt, inp->inp_moptions);
1553 #if defined(IPSEC) || defined(FAST_IPSEC)
1554 case IP_IPSEC_POLICY:
1556 struct mbuf *m = NULL;
1561 req = mtod(m, caddr_t);
1564 error = ipsec4_get_policy(so->so_pcb, req, len, &m);
1566 error = soopt_from_mbuf(sopt, m); /* XXX */
1574 error = ENOPROTOOPT;
1583 * Set up IP options in pcb for insertion in output packets.
1584 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1585 * with destination address if source routed.
1588 ip_pcbopts(int optname, struct mbuf **pcbopt, struct mbuf *m)
1594 /* turn off any old options */
1598 if (m == NULL || m->m_len == 0) {
1600 * Only turning off any previous options.
1607 if (m->m_len % sizeof(int32_t))
1610 * IP first-hop destination address will be stored before
1611 * actual options; move other options back
1612 * and clear it when none present.
1614 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1617 m->m_len += sizeof(struct in_addr);
1618 cp = mtod(m, u_char *) + sizeof(struct in_addr);
1619 ovbcopy(mtod(m, caddr_t), cp, cnt);
1620 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1622 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1623 opt = cp[IPOPT_OPTVAL];
1624 if (opt == IPOPT_EOL)
1626 if (opt == IPOPT_NOP)
1629 if (cnt < IPOPT_OLEN + sizeof *cp)
1631 optlen = cp[IPOPT_OLEN];
1632 if (optlen < IPOPT_OLEN + sizeof *cp || optlen > cnt)
1643 * user process specifies route as:
1645 * D must be our final destination (but we can't
1646 * check that since we may not have connected yet).
1647 * A is first hop destination, which doesn't appear in
1648 * actual IP option, but is stored before the options.
1650 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1652 m->m_len -= sizeof(struct in_addr);
1653 cnt -= sizeof(struct in_addr);
1654 optlen -= sizeof(struct in_addr);
1655 cp[IPOPT_OLEN] = optlen;
1657 * Move first hop before start of options.
1659 bcopy(&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1660 sizeof(struct in_addr));
1662 * Then copy rest of options back
1663 * to close up the deleted entry.
1665 ovbcopy(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr),
1666 &cp[IPOPT_OFFSET+1],
1667 cnt - (IPOPT_MINOFF - 1));
1671 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1683 * The whole multicast option thing needs to be re-thought.
1684 * Several of these options are equally applicable to non-multicast
1685 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1686 * standard option (IP_TTL).
1690 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1692 static struct ifnet *
1693 ip_multicast_if(struct in_addr *a, int *ifindexp)
1700 if (ntohl(a->s_addr) >> 24 == 0) {
1701 ifindex = ntohl(a->s_addr) & 0xffffff;
1702 if (ifindex < 0 || if_index < ifindex)
1704 ifp = ifindex2ifnet[ifindex];
1706 *ifindexp = ifindex;
1708 ifp = INADDR_TO_IFP(a);
1714 * Set the IP multicast options in response to user setsockopt().
1717 ip_setmoptions(struct sockopt *sopt, struct ip_moptions **imop)
1721 struct in_addr addr;
1722 struct ip_mreq mreq;
1724 struct ip_moptions *imo = *imop;
1729 * No multicast option buffer attached to the pcb;
1730 * allocate one and initialize to default values.
1732 imo = kmalloc(sizeof *imo, M_IPMOPTS, M_WAITOK);
1735 imo->imo_multicast_ifp = NULL;
1736 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1737 imo->imo_multicast_vif = -1;
1738 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1739 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1740 imo->imo_num_memberships = 0;
1742 switch (sopt->sopt_name) {
1743 /* store an index number for the vif you wanna use in the send */
1744 case IP_MULTICAST_VIF:
1745 if (legal_vif_num == 0) {
1749 error = soopt_to_kbuf(sopt, &i, sizeof i, sizeof i);
1752 if (!legal_vif_num(i) && (i != -1)) {
1756 imo->imo_multicast_vif = i;
1759 case IP_MULTICAST_IF:
1761 * Select the interface for outgoing multicast packets.
1763 error = soopt_to_kbuf(sopt, &addr, sizeof addr, sizeof addr);
1768 * INADDR_ANY is used to remove a previous selection.
1769 * When no interface is selected, a default one is
1770 * chosen every time a multicast packet is sent.
1772 if (addr.s_addr == INADDR_ANY) {
1773 imo->imo_multicast_ifp = NULL;
1777 * The selected interface is identified by its local
1778 * IP address. Find the interface and confirm that
1779 * it supports multicasting.
1782 ifp = ip_multicast_if(&addr, &ifindex);
1783 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
1785 error = EADDRNOTAVAIL;
1788 imo->imo_multicast_ifp = ifp;
1790 imo->imo_multicast_addr = addr;
1792 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1796 case IP_MULTICAST_TTL:
1798 * Set the IP time-to-live for outgoing multicast packets.
1799 * The original multicast API required a char argument,
1800 * which is inconsistent with the rest of the socket API.
1801 * We allow either a char or an int.
1803 if (sopt->sopt_valsize == 1) {
1805 error = soopt_to_kbuf(sopt, &ttl, 1, 1);
1808 imo->imo_multicast_ttl = ttl;
1811 error = soopt_to_kbuf(sopt, &ttl, sizeof ttl, sizeof ttl);
1817 imo->imo_multicast_ttl = ttl;
1821 case IP_MULTICAST_LOOP:
1823 * Set the loopback flag for outgoing multicast packets.
1824 * Must be zero or one. The original multicast API required a
1825 * char argument, which is inconsistent with the rest
1826 * of the socket API. We allow either a char or an int.
1828 if (sopt->sopt_valsize == 1) {
1831 error = soopt_to_kbuf(sopt, &loop, 1, 1);
1834 imo->imo_multicast_loop = !!loop;
1838 error = soopt_to_kbuf(sopt, &loop, sizeof loop,
1842 imo->imo_multicast_loop = !!loop;
1846 case IP_ADD_MEMBERSHIP:
1848 * Add a multicast group membership.
1849 * Group must be a valid IP multicast address.
1851 error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq);
1855 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1861 * If no interface address was provided, use the interface of
1862 * the route to the given multicast address.
1864 if (mreq.imr_interface.s_addr == INADDR_ANY) {
1865 struct sockaddr_in dst;
1868 bzero(&dst, sizeof(struct sockaddr_in));
1869 dst.sin_len = sizeof(struct sockaddr_in);
1870 dst.sin_family = AF_INET;
1871 dst.sin_addr = mreq.imr_multiaddr;
1872 rt = rtlookup((struct sockaddr *)&dst);
1874 error = EADDRNOTAVAIL;
1881 ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1885 * See if we found an interface, and confirm that it
1886 * supports multicast.
1888 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
1889 error = EADDRNOTAVAIL;
1894 * See if the membership already exists or if all the
1895 * membership slots are full.
1897 for (i = 0; i < imo->imo_num_memberships; ++i) {
1898 if (imo->imo_membership[i]->inm_ifp == ifp &&
1899 imo->imo_membership[i]->inm_addr.s_addr
1900 == mreq.imr_multiaddr.s_addr)
1903 if (i < imo->imo_num_memberships) {
1908 if (i == IP_MAX_MEMBERSHIPS) {
1909 error = ETOOMANYREFS;
1914 * Everything looks good; add a new record to the multicast
1915 * address list for the given interface.
1917 if ((imo->imo_membership[i] =
1918 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1923 ++imo->imo_num_memberships;
1927 case IP_DROP_MEMBERSHIP:
1929 * Drop a multicast group membership.
1930 * Group must be a valid IP multicast address.
1932 error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq);
1936 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1943 * If an interface address was specified, get a pointer
1944 * to its ifnet structure.
1946 if (mreq.imr_interface.s_addr == INADDR_ANY)
1949 ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1951 error = EADDRNOTAVAIL;
1957 * Find the membership in the membership array.
1959 for (i = 0; i < imo->imo_num_memberships; ++i) {
1961 imo->imo_membership[i]->inm_ifp == ifp) &&
1962 imo->imo_membership[i]->inm_addr.s_addr ==
1963 mreq.imr_multiaddr.s_addr)
1966 if (i == imo->imo_num_memberships) {
1967 error = EADDRNOTAVAIL;
1972 * Give up the multicast address record to which the
1973 * membership points.
1975 in_delmulti(imo->imo_membership[i]);
1977 * Remove the gap in the membership array.
1979 for (++i; i < imo->imo_num_memberships; ++i)
1980 imo->imo_membership[i-1] = imo->imo_membership[i];
1981 --imo->imo_num_memberships;
1991 * If all options have default values, no need to keep the mbuf.
1993 if (imo->imo_multicast_ifp == NULL &&
1994 imo->imo_multicast_vif == -1 &&
1995 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1996 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1997 imo->imo_num_memberships == 0) {
1998 kfree(*imop, M_IPMOPTS);
2006 * Return the IP multicast options in response to user getsockopt().
2009 ip_getmoptions(struct sockopt *sopt, struct ip_moptions *imo)
2011 struct in_addr addr;
2012 struct in_ifaddr *ia;
2017 switch (sopt->sopt_name) {
2018 case IP_MULTICAST_VIF:
2020 optval = imo->imo_multicast_vif;
2023 soopt_from_kbuf(sopt, &optval, sizeof optval);
2026 case IP_MULTICAST_IF:
2027 if (imo == NULL || imo->imo_multicast_ifp == NULL)
2028 addr.s_addr = INADDR_ANY;
2029 else if (imo->imo_multicast_addr.s_addr) {
2030 /* return the value user has set */
2031 addr = imo->imo_multicast_addr;
2033 ia = IFP_TO_IA(imo->imo_multicast_ifp);
2034 addr.s_addr = (ia == NULL) ? INADDR_ANY
2035 : IA_SIN(ia)->sin_addr.s_addr;
2037 soopt_from_kbuf(sopt, &addr, sizeof addr);
2040 case IP_MULTICAST_TTL:
2042 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
2044 optval = coptval = imo->imo_multicast_ttl;
2045 if (sopt->sopt_valsize == 1)
2046 soopt_from_kbuf(sopt, &coptval, 1);
2048 soopt_from_kbuf(sopt, &optval, sizeof optval);
2051 case IP_MULTICAST_LOOP:
2053 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
2055 optval = coptval = imo->imo_multicast_loop;
2056 if (sopt->sopt_valsize == 1)
2057 soopt_from_kbuf(sopt, &coptval, 1);
2059 soopt_from_kbuf(sopt, &optval, sizeof optval);
2063 error = ENOPROTOOPT;
2070 * Discard the IP multicast options.
2073 ip_freemoptions(struct ip_moptions *imo)
2078 for (i = 0; i < imo->imo_num_memberships; ++i)
2079 in_delmulti(imo->imo_membership[i]);
2080 kfree(imo, M_IPMOPTS);
2085 * Routine called from ip_output() to loop back a copy of an IP multicast
2086 * packet to the input queue of a specified interface. Note that this
2087 * calls the output routine of the loopback "driver", but with an interface
2088 * pointer that might NOT be a loopback interface -- evil, but easier than
2089 * replicating that code here.
2092 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
2098 copym = m_copypacket(m, MB_DONTWAIT);
2099 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
2100 copym = m_pullup(copym, hlen);
2101 if (copym != NULL) {
2103 * if the checksum hasn't been computed, mark it as valid
2105 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2106 in_delayed_cksum(copym);
2107 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2108 copym->m_pkthdr.csum_flags |=
2109 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2110 copym->m_pkthdr.csum_data = 0xffff;
2113 * We don't bother to fragment if the IP length is greater
2114 * than the interface's MTU. Can this possibly matter?
2116 ip = mtod(copym, struct ip *);
2117 ip->ip_len = htons(ip->ip_len);
2118 ip->ip_off = htons(ip->ip_off);
2120 if (ip->ip_vhl == IP_VHL_BORING) {
2121 ip->ip_sum = in_cksum_hdr(ip);
2123 ip->ip_sum = in_cksum(copym, hlen);
2127 * It's not clear whether there are any lingering
2128 * reentrancy problems in other areas which might
2129 * be exposed by using ip_input directly (in
2130 * particular, everything which modifies the packet
2131 * in-place). Yet another option is using the
2132 * protosw directly to deliver the looped back
2133 * packet. For the moment, we'll err on the side
2134 * of safety by using if_simloop().
2137 if (dst->sin_family != AF_INET) {
2138 kprintf("ip_mloopback: bad address family %d\n",
2140 dst->sin_family = AF_INET;
2145 copym->m_pkthdr.rcvif = ifp;
2148 if_simloop(ifp, copym, dst->sin_family, 0);