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_ipsec.h"
38 #include "opt_mbuf_stress_test.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/in_cksum.h>
55 #include <sys/thread2.h>
56 #include <sys/mplock2.h>
57 #include <sys/msgport2.h>
60 #include <net/netisr.h>
62 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/in_pcb.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip_var.h>
71 #include <netproto/mpls/mpls_var.h>
73 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
76 #include <netinet6/ipsec.h>
77 #include <netproto/key/key.h>
79 #include <netproto/key/key_debug.h>
81 #define KEYDEBUG(lev,arg)
86 #include <netproto/ipsec/ipsec.h>
87 #include <netproto/ipsec/xform.h>
88 #include <netproto/ipsec/key.h>
91 #include <net/ipfw/ip_fw.h>
92 #include <net/dummynet/ip_dummynet.h>
94 #define print_ip(x, a, y) kprintf("%s %d.%d.%d.%d%s",\
95 x, (ntohl(a.s_addr)>>24)&0xFF,\
96 (ntohl(a.s_addr)>>16)&0xFF,\
97 (ntohl(a.s_addr)>>8)&0xFF,\
98 (ntohl(a.s_addr))&0xFF, y);
102 #ifdef MBUF_STRESS_TEST
103 int mbuf_frag_size = 0;
104 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
105 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
108 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
109 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
110 static void ip_mloopback
111 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
112 static int ip_getmoptions
113 (struct sockopt *, struct ip_moptions *);
114 static int ip_pcbopts(int, struct mbuf **, struct mbuf *);
115 static int ip_setmoptions
116 (struct sockopt *, struct ip_moptions **);
118 int ip_optcopy(struct ip *, struct ip *);
120 extern int route_assert_owner_access;
122 extern struct protosw inetsw[];
125 ip_localforward(struct mbuf *m, const struct sockaddr_in *dst, int hlen)
127 struct in_ifaddr_container *iac;
130 * We need to figure out if we have been forwarded to a local
131 * socket. If so, then we should somehow "loop back" to
132 * ip_input(), and get directed to the PCB as if we had received
133 * this packet. This is because it may be difficult to identify
134 * the packets you want to forward until they are being output
135 * and have selected an interface (e.g. locally initiated
136 * packets). If we used the loopback inteface, we would not be
137 * able to control what happens as the packet runs through
138 * ip_input() as it is done through a ISR.
140 LIST_FOREACH(iac, INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
142 * If the addr to forward to is one of ours, we pretend
143 * to be the destination for this packet.
145 if (IA_SIN(iac->ia)->sin_addr.s_addr == dst->sin_addr.s_addr)
151 if (m->m_pkthdr.rcvif == NULL)
152 m->m_pkthdr.rcvif = ifunit("lo0");
153 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
154 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
156 m->m_pkthdr.csum_data = 0xffff;
158 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED | CSUM_IP_VALID;
161 * Make sure that the IP header is in one mbuf,
162 * required by ip_input
164 if (m->m_len < hlen) {
165 m = m_pullup(m, hlen);
167 /* The packet was freed; we are done */
171 ip = mtod(m, struct ip *);
173 ip->ip_len = htons(ip->ip_len);
174 ip->ip_off = htons(ip->ip_off);
177 return 1; /* The packet gets forwarded locally */
183 * IP output. The packet in mbuf chain m contains a skeletal IP
184 * header (with len, off, ttl, proto, tos, src, dst).
185 * The mbuf chain containing the packet will be freed.
186 * The mbuf opt, if present, will not be freed.
189 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro,
190 int flags, struct ip_moptions *imo, struct inpcb *inp)
193 struct ifnet *ifp = NULL; /* keep compiler happy */
195 int hlen = sizeof(struct ip);
197 struct sockaddr_in *dst = NULL; /* keep compiler happy */
198 struct in_ifaddr *ia = NULL;
199 int isbroadcast, sw_csum;
200 struct in_addr pkt_dst;
201 struct route iproute;
204 struct secpolicy *sp = NULL;
205 struct socket *so = inp ? inp->inp_socket : NULL;
208 struct secpolicy *sp = NULL;
209 struct tdb_ident *tdbi;
210 #endif /* FAST_IPSEC */
211 struct sockaddr_in *next_hop = NULL;
212 int src_was_INADDR_ANY = 0; /* as the name says... */
219 bzero(ro, sizeof *ro);
220 } else if (ro->ro_rt != NULL && ro->ro_rt->rt_cpuid != mycpuid) {
221 if (flags & IP_DEBUGROUTE) {
222 if (route_assert_owner_access) {
224 "rt rt_cpuid %d accessed on cpu %d\n",
225 ro->ro_rt->rt_cpuid, mycpuid);
227 kprintf("ip_output: "
228 "rt rt_cpuid %d accessed on cpu %d\n",
229 ro->ro_rt->rt_cpuid, mycpuid);
236 * If the cached rtentry's owner CPU is not the current CPU,
237 * then don't touch the cached rtentry (remote free is too
238 * expensive in this context); just relocate the route.
241 bzero(ro, sizeof *ro);
244 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
246 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
247 KKASSERT(mtag != NULL);
248 next_hop = m_tag_data(mtag);
251 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
252 struct dn_pkt *dn_pkt;
254 /* Extract info from dummynet tag */
255 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
256 KKASSERT(mtag != NULL);
257 dn_pkt = m_tag_data(mtag);
260 * The packet was already tagged, so part of the
261 * processing was already done, and we need to go down.
262 * Get the calculated parameters from the tag.
266 KKASSERT(ro == &iproute);
267 *ro = dn_pkt->ro; /* structure copy */
268 KKASSERT(ro->ro_rt == NULL || ro->ro_rt->rt_cpuid == mycpuid);
270 dst = dn_pkt->dn_dst;
271 if (dst == (struct sockaddr_in *)&(dn_pkt->ro.ro_dst)) {
272 /* If 'dst' points into dummynet tag, adjust it */
273 dst = (struct sockaddr_in *)&(ro->ro_dst);
276 ip = mtod(m, struct ip *);
277 hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
279 ia = ifatoia(ro->ro_rt->rt_ifa);
285 m = ip_insertoptions(m, opt, &len);
289 ip = mtod(m, struct ip *);
294 if (!(flags & (IP_FORWARDING|IP_RAWOUTPUT))) {
295 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
297 ip->ip_id = ip_newid();
298 ipstat.ips_localout++;
300 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
304 pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst;
306 dst = (struct sockaddr_in *)&ro->ro_dst;
308 * If there is a cached route,
309 * check that it is to the same destination
310 * and is still up. If not, free it and try again.
311 * The address family should also be checked in case of sharing the
315 (!(ro->ro_rt->rt_flags & RTF_UP) ||
316 dst->sin_family != AF_INET ||
317 dst->sin_addr.s_addr != pkt_dst.s_addr)) {
321 if (ro->ro_rt == NULL) {
322 bzero(dst, sizeof *dst);
323 dst->sin_family = AF_INET;
324 dst->sin_len = sizeof *dst;
325 dst->sin_addr = pkt_dst;
328 * If routing to interface only,
329 * short circuit routing lookup.
331 if (flags & IP_ROUTETOIF) {
332 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
333 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
334 ipstat.ips_noroute++;
340 isbroadcast = in_broadcast(dst->sin_addr, ifp);
341 } else if (IN_MULTICAST(ntohl(pkt_dst.s_addr)) &&
342 imo != NULL && imo->imo_multicast_ifp != NULL) {
344 * Bypass the normal routing lookup for multicast
345 * packets if the interface is specified.
347 ifp = imo->imo_multicast_ifp;
349 isbroadcast = 0; /* fool gcc */
352 * If this is the case, we probably don't want to allocate
353 * a protocol-cloned route since we didn't get one from the
354 * ULP. This lets TCP do its thing, while not burdening
355 * forwarding or ICMP with the overhead of cloning a route.
356 * Of course, we still want to do any cloning requested by
357 * the link layer, as this is probably required in all cases
358 * for correct operation (as it is for ARP).
360 if (ro->ro_rt == NULL)
361 rtalloc_ign(ro, RTF_PRCLONING);
362 if (ro->ro_rt == NULL) {
363 ipstat.ips_noroute++;
364 error = EHOSTUNREACH;
367 ia = ifatoia(ro->ro_rt->rt_ifa);
368 ifp = ro->ro_rt->rt_ifp;
370 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
371 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
372 if (ro->ro_rt->rt_flags & RTF_HOST)
373 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
375 isbroadcast = in_broadcast(dst->sin_addr, ifp);
377 if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
378 struct in_multi *inm;
380 m->m_flags |= M_MCAST;
382 * IP destination address is multicast. Make sure "dst"
383 * still points to the address in "ro". (It may have been
384 * changed to point to a gateway address, above.)
386 dst = (struct sockaddr_in *)&ro->ro_dst;
388 * See if the caller provided any multicast options
391 ip->ip_ttl = imo->imo_multicast_ttl;
392 if (imo->imo_multicast_vif != -1) {
395 ip_mcast_src(imo->imo_multicast_vif) :
399 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
402 * Confirm that the outgoing interface supports multicast.
404 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
405 if (!(ifp->if_flags & IFF_MULTICAST)) {
406 ipstat.ips_noroute++;
412 * If source address not specified yet, use address
413 * of outgoing interface.
415 if (ip->ip_src.s_addr == INADDR_ANY) {
416 /* Interface may have no addresses. */
418 ip->ip_src = IA_SIN(ia)->sin_addr;
421 IN_LOOKUP_MULTI(pkt_dst, ifp, inm);
423 (imo == NULL || imo->imo_multicast_loop)) {
425 * If we belong to the destination multicast group
426 * on the outgoing interface, and the caller did not
427 * forbid loopback, loop back a copy.
429 ip_mloopback(ifp, m, dst, hlen);
432 * If we are acting as a multicast router, perform
433 * multicast forwarding as if the packet had just
434 * arrived on the interface to which we are about
435 * to send. The multicast forwarding function
436 * recursively calls this function, using the
437 * IP_FORWARDING flag to prevent infinite recursion.
439 * Multicasts that are looped back by ip_mloopback(),
440 * above, will be forwarded by the ip_input() routine,
443 if (ip_mrouter && !(flags & IP_FORWARDING)) {
445 * If rsvp daemon is not running, do not
446 * set ip_moptions. This ensures that the packet
447 * is multicast and not just sent down one link
448 * as prescribed by rsvpd.
454 if (ip_mforward(ip, ifp, m, imo) != 0) {
465 * Multicasts with a time-to-live of zero may be looped-
466 * back, above, but must not be transmitted on a network.
467 * Also, multicasts addressed to the loopback interface
468 * are not sent -- the above call to ip_mloopback() will
469 * loop back a copy if this host actually belongs to the
470 * destination group on the loopback interface.
472 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
479 m->m_flags &= ~M_MCAST;
483 * If the source address is not specified yet, use the address
484 * of the outoing interface. In case, keep note we did that, so
485 * if the the firewall changes the next-hop causing the output
486 * interface to change, we can fix that.
488 if (ip->ip_src.s_addr == INADDR_ANY || src_was_INADDR_ANY) {
489 /* Interface may have no addresses. */
491 ip->ip_src = IA_SIN(ia)->sin_addr;
492 src_was_INADDR_ANY = 1;
498 * Disable packet drop hack.
499 * Packetdrop should be done by queueing.
503 * Verify that we have any chance at all of being able to queue
504 * the packet or packet fragments
506 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
507 ifp->if_snd.ifq_maxlen) {
509 ipstat.ips_odropped++;
515 * Look for broadcast address and
516 * verify user is allowed to send
520 if (!(ifp->if_flags & IFF_BROADCAST)) {
521 error = EADDRNOTAVAIL;
524 if (!(flags & IP_ALLOWBROADCAST)) {
528 /* don't allow broadcast messages to be fragmented */
529 if (ip->ip_len > ifp->if_mtu) {
533 m->m_flags |= M_BCAST;
535 m->m_flags &= ~M_BCAST;
540 /* get SP for this packet */
542 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
544 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
547 ipsecstat.out_inval++;
554 switch (sp->policy) {
555 case IPSEC_POLICY_DISCARD:
557 * This packet is just discarded.
559 ipsecstat.out_polvio++;
562 case IPSEC_POLICY_BYPASS:
563 case IPSEC_POLICY_NONE:
564 case IPSEC_POLICY_TCP:
565 /* no need to do IPsec. */
568 case IPSEC_POLICY_IPSEC:
569 if (sp->req == NULL) {
570 /* acquire a policy */
571 error = key_spdacquire(sp);
576 case IPSEC_POLICY_ENTRUST:
578 kprintf("ip_output: Invalid policy found. %d\n", sp->policy);
581 struct ipsec_output_state state;
582 bzero(&state, sizeof state);
584 if (flags & IP_ROUTETOIF) {
586 bzero(&iproute, sizeof iproute);
589 state.dst = (struct sockaddr *)dst;
595 * delayed checksums are not currently compatible with IPsec
597 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
599 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
602 ip->ip_len = htons(ip->ip_len);
603 ip->ip_off = htons(ip->ip_off);
605 error = ipsec4_output(&state, sp, flags);
608 if (flags & IP_ROUTETOIF) {
610 * if we have tunnel mode SA, we may need to ignore
613 if (state.ro != &iproute || state.ro->ro_rt != NULL) {
614 flags &= ~IP_ROUTETOIF;
619 dst = (struct sockaddr_in *)state.dst;
621 /* mbuf is already reclaimed in ipsec4_output. */
631 kprintf("ip4_output (ipsec): error code %d\n", error);
634 /* don't show these error codes to the user */
642 /* be sure to update variables that are affected by ipsec4_output() */
643 ip = mtod(m, struct ip *);
645 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
647 hlen = ip->ip_hl << 2;
649 if (ro->ro_rt == NULL) {
650 if (!(flags & IP_ROUTETOIF)) {
651 kprintf("ip_output: "
652 "can't update route after IPsec processing\n");
653 error = EHOSTUNREACH; /*XXX*/
657 ia = ifatoia(ro->ro_rt->rt_ifa);
658 ifp = ro->ro_rt->rt_ifp;
661 /* make it flipped, again. */
662 ip->ip_len = ntohs(ip->ip_len);
663 ip->ip_off = ntohs(ip->ip_off);
668 * Check the security policy (SP) for the packet and, if
669 * required, do IPsec-related processing. There are two
670 * cases here; the first time a packet is sent through
671 * it will be untagged and handled by ipsec4_checkpolicy.
672 * If the packet is resubmitted to ip_output (e.g. after
673 * AH, ESP, etc. processing), there will be a tag to bypass
674 * the lookup and related policy checking.
676 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
679 tdbi = (struct tdb_ident *)m_tag_data(mtag);
680 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
682 error = -EINVAL; /* force silent drop */
683 m_tag_delete(m, mtag);
685 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
689 * There are four return cases:
690 * sp != NULL apply IPsec policy
691 * sp == NULL, error == 0 no IPsec handling needed
692 * sp == NULL, error == -EINVAL discard packet w/o error
693 * sp == NULL, error != 0 discard packet, report error
696 /* Loop detection, check if ipsec processing already done */
697 KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
698 for (mtag = m_tag_first(m); mtag != NULL;
699 mtag = m_tag_next(m, mtag)) {
700 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
702 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
703 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
706 * Check if policy has an SA associated with it.
707 * This can happen when an SP has yet to acquire
708 * an SA; e.g. on first reference. If it occurs,
709 * then we let ipsec4_process_packet do its thing.
711 if (sp->req->sav == NULL)
713 tdbi = (struct tdb_ident *)m_tag_data(mtag);
714 if (tdbi->spi == sp->req->sav->spi &&
715 tdbi->proto == sp->req->sav->sah->saidx.proto &&
716 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
717 sizeof(union sockaddr_union)) == 0) {
719 * No IPsec processing is needed, free
722 * NB: null pointer to avoid free at
725 KEY_FREESP(&sp), sp = NULL;
732 * Do delayed checksums now because we send before
733 * this is done in the normal processing path.
735 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
737 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
740 ip->ip_len = htons(ip->ip_len);
741 ip->ip_off = htons(ip->ip_off);
743 /* NB: callee frees mbuf */
744 error = ipsec4_process_packet(m, sp->req, flags, 0);
746 * Preserve KAME behaviour: ENOENT can be returned
747 * when an SA acquire is in progress. Don't propagate
748 * this to user-level; it confuses applications.
750 * XXX this will go away when the SADB is redone.
761 * Hack: -EINVAL is used to signal that a packet
762 * should be silently discarded. This is typically
763 * because we asked key management for an SA and
764 * it was delayed (e.g. kicked up to IKE).
766 if (error == -EINVAL)
770 /* No IPsec processing for this packet. */
774 * If deferred crypto processing is needed, check that
775 * the interface supports it.
777 mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
778 if (mtag != NULL && !(ifp->if_capenable & IFCAP_IPSEC)) {
779 /* notify IPsec to do its own crypto */
780 ipsp_skipcrypto_unmark((struct tdb_ident *)m_tag_data(mtag));
781 error = EHOSTUNREACH;
787 #endif /* FAST_IPSEC */
789 /* We are already being fwd'd from a firewall. */
790 if (next_hop != NULL)
794 if (!pfil_has_hooks(&inet_pfil_hook)) {
795 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
797 * Strip dummynet tags from stranded packets
799 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
800 KKASSERT(mtag != NULL);
801 m_tag_delete(m, mtag);
802 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
809 * - Xlate: translate packet's addr/port (NAT).
810 * - Firewall: deny/allow/etc.
811 * - Wrap: fake packet's addr/port <unimpl.>
812 * - Encapsulate: put it in another IP and send out. <unimp.>
816 * Run through list of hooks for output packets.
818 error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT);
819 if (error != 0 || m == NULL)
821 ip = mtod(m, struct ip *);
823 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
825 * Check dst to make sure it is directly reachable on the
826 * interface we previously thought it was.
827 * If it isn't (which may be likely in some situations) we have
828 * to re-route it (ie, find a route for the next-hop and the
829 * associated interface) and set them here. This is nested
830 * forwarding which in most cases is undesirable, except where
831 * such control is nigh impossible. So we do it here.
834 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
835 KKASSERT(mtag != NULL);
836 next_hop = m_tag_data(mtag);
839 * Try local forwarding first
841 if (ip_localforward(m, next_hop, hlen))
845 * Relocate the route based on next_hop.
846 * If the current route is inp's cache, keep it untouched.
848 if (ro == &iproute && ro->ro_rt != NULL) {
853 bzero(ro, sizeof *ro);
856 * Forwarding to broadcast address is not allowed.
857 * XXX Should we follow IP_ROUTETOIF?
859 flags &= ~(IP_ALLOWBROADCAST | IP_ROUTETOIF);
861 /* We are doing forwarding now */
862 flags |= IP_FORWARDING;
867 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
868 struct dn_pkt *dn_pkt;
870 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
871 KKASSERT(mtag != NULL);
872 dn_pkt = m_tag_data(mtag);
875 * Under certain cases it is not possible to recalculate
876 * 'ro' and 'dst', let alone 'flags', so just save them in
877 * dummynet tag and avoid the possible wrong reculcalation
878 * when we come back to ip_output() again.
880 * All other parameters have been already used and so they
881 * are not needed anymore.
882 * XXX if the ifp is deleted while a pkt is in dummynet,
883 * we are in trouble! (TODO use ifnet_detach_event)
885 * We need to copy *ro because for ICMP pkts (and maybe
886 * others) the caller passed a pointer into the stack;
887 * dst might also be a pointer into *ro so it needs to
892 ro->ro_rt->rt_refcnt++;
893 if (dst == (struct sockaddr_in *)&ro->ro_dst) {
894 /* 'dst' points into 'ro' */
895 dst = (struct sockaddr_in *)&(dn_pkt->ro.ro_dst);
897 dn_pkt->dn_dst = dst;
898 dn_pkt->flags = flags;
904 /* 127/8 must not appear on wire - RFC1122. */
905 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
906 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
907 if (!(ifp->if_flags & IFF_LOOPBACK)) {
908 ipstat.ips_badaddr++;
909 error = EADDRNOTAVAIL;
914 if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
915 m->m_pkthdr.csum_flags |= CSUM_IP;
916 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
917 if (sw_csum & CSUM_DELAY_DATA) {
919 sw_csum &= ~CSUM_DELAY_DATA;
921 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
925 m->m_pkthdr.csum_iphlen = hlen;
928 * If small enough for interface, or the interface will take
929 * care of the fragmentation or segmentation for us, can just
932 if (ip->ip_len <= ifp->if_mtu ||
933 ((ifp->if_hwassist & CSUM_FRAGMENT) && !(ip->ip_off & IP_DF)) ||
934 (m->m_pkthdr.csum_flags & CSUM_TSO)) {
935 ip->ip_len = htons(ip->ip_len);
936 ip->ip_off = htons(ip->ip_off);
938 if (sw_csum & CSUM_DELAY_IP) {
939 if (ip->ip_vhl == IP_VHL_BORING)
940 ip->ip_sum = in_cksum_hdr(ip);
942 ip->ip_sum = in_cksum(m, hlen);
945 /* Record statistics for this interface address. */
946 if (!(flags & IP_FORWARDING) && ia) {
947 ia->ia_ifa.if_opackets++;
948 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
952 /* clean ipsec history once it goes out of the node */
956 #ifdef MBUF_STRESS_TEST
957 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) {
958 struct mbuf *m1, *m2;
961 tmp = length = m->m_pkthdr.len;
963 while ((length -= mbuf_frag_size) >= 1) {
964 m1 = m_split(m, length, MB_DONTWAIT);
968 while (m2->m_next != NULL)
972 m->m_pkthdr.len = tmp;
977 if (!mpls_output_process(m, ro->ro_rt))
980 error = ifp->if_output(ifp, m, (struct sockaddr *)dst,
985 if (ip->ip_off & IP_DF) {
988 * This case can happen if the user changed the MTU
989 * of an interface after enabling IP on it. Because
990 * most netifs don't keep track of routes pointing to
991 * them, there is no way for one to update all its
992 * routes when the MTU is changed.
994 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
995 !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) &&
996 (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
997 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
999 ipstat.ips_cantfrag++;
1004 * Too large for interface; fragment if possible. If successful,
1005 * on return, m will point to a list of packets to be sent.
1007 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
1012 m->m_nextpkt = NULL;
1014 /* clean ipsec history once it goes out of the node */
1018 /* Record statistics for this interface address. */
1020 ia->ia_ifa.if_opackets++;
1021 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1024 if (!mpls_output_process(m, ro->ro_rt))
1027 error = ifp->if_output(ifp, m, (struct sockaddr *)dst,
1035 ipstat.ips_fragmented++;
1038 if (ro == &iproute && ro->ro_rt != NULL) {
1044 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1045 kprintf("DP ip_output call free SP:%p\n", sp));
1060 * Create a chain of fragments which fit the given mtu. m_frag points to the
1061 * mbuf to be fragmented; on return it points to the chain with the fragments.
1062 * Return 0 if no error. If error, m_frag may contain a partially built
1063 * chain of fragments that should be freed by the caller.
1065 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
1066 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
1069 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
1070 u_long if_hwassist_flags, int sw_csum)
1073 int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
1074 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */
1076 struct mbuf *m0 = *m_frag; /* the original packet */
1078 struct mbuf **mnext;
1081 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */
1082 ipstat.ips_cantfrag++;
1087 * Must be able to put at least 8 bytes per fragment.
1093 * If the interface will not calculate checksums on
1094 * fragmented packets, then do it here.
1096 if ((m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) &&
1097 !(if_hwassist_flags & CSUM_IP_FRAGS)) {
1098 in_delayed_cksum(m0);
1099 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1102 if (len > PAGE_SIZE) {
1104 * Fragment large datagrams such that each segment
1105 * contains a multiple of PAGE_SIZE amount of data,
1106 * plus headers. This enables a receiver to perform
1107 * page-flipping zero-copy optimizations.
1109 * XXX When does this help given that sender and receiver
1110 * could have different page sizes, and also mtu could
1111 * be less than the receiver's page size ?
1116 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
1120 * firstlen (off - hlen) must be aligned on an
1124 goto smart_frag_failure;
1125 off = ((off - hlen) & ~7) + hlen;
1126 newlen = (~PAGE_MASK) & mtu;
1127 if ((newlen + sizeof(struct ip)) > mtu) {
1128 /* we failed, go back the default */
1139 firstlen = off - hlen;
1140 mnext = &m0->m_nextpkt; /* pointer to next packet */
1143 * Loop through length of segment after first fragment,
1144 * make new header and copy data of each part and link onto chain.
1145 * Here, m0 is the original packet, m is the fragment being created.
1146 * The fragments are linked off the m_nextpkt of the original
1147 * packet, which after processing serves as the first fragment.
1149 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
1150 struct ip *mhip; /* ip header on the fragment */
1152 int mhlen = sizeof(struct ip);
1154 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1157 ipstat.ips_odropped++;
1160 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
1162 * In the first mbuf, leave room for the link header, then
1163 * copy the original IP header including options. The payload
1164 * goes into an additional mbuf chain returned by m_copy().
1166 m->m_data += max_linkhdr;
1167 mhip = mtod(m, struct ip *);
1169 if (hlen > sizeof(struct ip)) {
1170 mhlen = ip_optcopy(ip, mhip) + sizeof(struct ip);
1171 mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
1174 /* XXX do we need to add ip->ip_off below ? */
1175 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
1176 if (off + len >= ip->ip_len) { /* last fragment */
1177 len = ip->ip_len - off;
1178 m->m_flags |= M_LASTFRAG;
1180 mhip->ip_off |= IP_MF;
1181 mhip->ip_len = htons((u_short)(len + mhlen));
1182 m->m_next = m_copy(m0, off, len);
1183 if (m->m_next == NULL) { /* copy failed */
1185 error = ENOBUFS; /* ??? */
1186 ipstat.ips_odropped++;
1189 m->m_pkthdr.len = mhlen + len;
1190 m->m_pkthdr.rcvif = NULL;
1191 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
1192 m->m_pkthdr.csum_iphlen = mhlen;
1193 mhip->ip_off = htons(mhip->ip_off);
1195 if (sw_csum & CSUM_DELAY_IP)
1196 mhip->ip_sum = in_cksum(m, mhlen);
1198 mnext = &m->m_nextpkt;
1200 ipstat.ips_ofragments += nfrags;
1202 /* set first marker for fragment chain */
1203 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1204 m0->m_pkthdr.csum_data = nfrags;
1207 * Update first fragment by trimming what's been copied out
1208 * and updating header.
1210 m_adj(m0, hlen + firstlen - ip->ip_len);
1211 m0->m_pkthdr.len = hlen + firstlen;
1212 ip->ip_len = htons((u_short)m0->m_pkthdr.len);
1213 ip->ip_off |= IP_MF;
1214 ip->ip_off = htons(ip->ip_off);
1216 if (sw_csum & CSUM_DELAY_IP)
1217 ip->ip_sum = in_cksum(m0, hlen);
1225 in_delayed_cksum(struct mbuf *m)
1228 u_short csum, offset;
1230 ip = mtod(m, struct ip *);
1231 offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
1232 csum = in_cksum_skip(m, ip->ip_len, offset);
1233 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1235 offset += m->m_pkthdr.csum_data; /* checksum offset */
1237 if (offset + sizeof(u_short) > m->m_len) {
1238 kprintf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1239 m->m_len, offset, ip->ip_p);
1242 * this shouldn't happen, but if it does, the
1243 * correct behavior may be to insert the checksum
1244 * in the existing chain instead of rearranging it.
1246 m = m_pullup(m, offset + sizeof(u_short));
1248 *(u_short *)(m->m_data + offset) = csum;
1252 * Insert IP options into preformed packet.
1253 * Adjust IP destination as required for IP source routing,
1254 * as indicated by a non-zero in_addr at the start of the options.
1256 * XXX This routine assumes that the packet has no options in place.
1258 static struct mbuf *
1259 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
1261 struct ipoption *p = mtod(opt, struct ipoption *);
1263 struct ip *ip = mtod(m, struct ip *);
1266 optlen = opt->m_len - sizeof p->ipopt_dst;
1267 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) {
1269 return (m); /* XXX should fail */
1271 if (p->ipopt_dst.s_addr)
1272 ip->ip_dst = p->ipopt_dst;
1273 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1274 MGETHDR(n, MB_DONTWAIT, MT_HEADER);
1279 n->m_pkthdr.rcvif = NULL;
1280 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1281 m->m_len -= sizeof(struct ip);
1282 m->m_data += sizeof(struct ip);
1285 m->m_len = optlen + sizeof(struct ip);
1286 m->m_data += max_linkhdr;
1287 memcpy(mtod(m, void *), ip, sizeof(struct ip));
1289 m->m_data -= optlen;
1291 m->m_pkthdr.len += optlen;
1292 ovbcopy(ip, mtod(m, caddr_t), sizeof(struct ip));
1294 ip = mtod(m, struct ip *);
1295 bcopy(p->ipopt_list, ip + 1, optlen);
1296 *phlen = sizeof(struct ip) + optlen;
1297 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1298 ip->ip_len += optlen;
1303 * Copy options from ip to jp,
1304 * omitting those not copied during fragmentation.
1307 ip_optcopy(struct ip *ip, struct ip *jp)
1310 int opt, optlen, cnt;
1312 cp = (u_char *)(ip + 1);
1313 dp = (u_char *)(jp + 1);
1314 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip);
1315 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1317 if (opt == IPOPT_EOL)
1319 if (opt == IPOPT_NOP) {
1320 /* Preserve for IP mcast tunnel's LSRR alignment. */
1326 KASSERT(cnt >= IPOPT_OLEN + sizeof *cp,
1327 ("ip_optcopy: malformed ipv4 option"));
1328 optlen = cp[IPOPT_OLEN];
1329 KASSERT(optlen >= IPOPT_OLEN + sizeof *cp && optlen <= cnt,
1330 ("ip_optcopy: malformed ipv4 option"));
1332 /* bogus lengths should have been caught by ip_dooptions */
1335 if (IPOPT_COPIED(opt)) {
1336 bcopy(cp, dp, optlen);
1340 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1346 * IP socket option processing.
1349 ip_ctloutput(netmsg_t msg)
1351 struct socket *so = msg->base.nm_so;
1352 struct sockopt *sopt = msg->ctloutput.nm_sopt;
1353 struct inpcb *inp = so->so_pcb;
1357 if (sopt->sopt_level != IPPROTO_IP) {
1362 switch (sopt->sopt_dir) {
1364 switch (sopt->sopt_name) {
1371 if (sopt->sopt_valsize > MLEN) {
1375 MGET(m, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_HEADER);
1380 m->m_len = sopt->sopt_valsize;
1381 error = soopt_to_kbuf(sopt, mtod(m, void *), m->m_len,
1383 error = ip_pcbopts(sopt->sopt_name,
1384 &inp->inp_options, m);
1392 case IP_RECVRETOPTS:
1393 case IP_RECVDSTADDR:
1397 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1401 switch (sopt->sopt_name) {
1403 inp->inp_ip_tos = optval;
1407 inp->inp_ip_ttl = optval;
1410 if (optval >= 0 && optval <= MAXTTL)
1411 inp->inp_ip_minttl = optval;
1415 #define OPTSET(bit) \
1417 inp->inp_flags |= bit; \
1419 inp->inp_flags &= ~bit;
1422 OPTSET(INP_RECVOPTS);
1425 case IP_RECVRETOPTS:
1426 OPTSET(INP_RECVRETOPTS);
1429 case IP_RECVDSTADDR:
1430 OPTSET(INP_RECVDSTADDR);
1438 OPTSET(INP_RECVTTL);
1448 case IP_MULTICAST_IF:
1449 case IP_MULTICAST_VIF:
1450 case IP_MULTICAST_TTL:
1451 case IP_MULTICAST_LOOP:
1452 case IP_ADD_MEMBERSHIP:
1453 case IP_DROP_MEMBERSHIP:
1454 error = ip_setmoptions(sopt, &inp->inp_moptions);
1458 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1464 case IP_PORTRANGE_DEFAULT:
1465 inp->inp_flags &= ~(INP_LOWPORT);
1466 inp->inp_flags &= ~(INP_HIGHPORT);
1469 case IP_PORTRANGE_HIGH:
1470 inp->inp_flags &= ~(INP_LOWPORT);
1471 inp->inp_flags |= INP_HIGHPORT;
1474 case IP_PORTRANGE_LOW:
1475 inp->inp_flags &= ~(INP_HIGHPORT);
1476 inp->inp_flags |= INP_LOWPORT;
1485 #if defined(IPSEC) || defined(FAST_IPSEC)
1486 case IP_IPSEC_POLICY:
1494 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1496 soopt_to_mbuf(sopt, m);
1497 priv = (sopt->sopt_td != NULL &&
1498 priv_check(sopt->sopt_td, PRIV_ROOT) != 0) ? 0 : 1;
1499 req = mtod(m, caddr_t);
1501 optname = sopt->sopt_name;
1502 error = ipsec4_set_policy(inp, optname, req, len, priv);
1509 error = ENOPROTOOPT;
1515 switch (sopt->sopt_name) {
1518 if (inp->inp_options)
1519 soopt_from_kbuf(sopt, mtod(inp->inp_options,
1521 inp->inp_options->m_len);
1523 sopt->sopt_valsize = 0;
1530 case IP_RECVRETOPTS:
1531 case IP_RECVDSTADDR:
1536 switch (sopt->sopt_name) {
1539 optval = inp->inp_ip_tos;
1543 optval = inp->inp_ip_ttl;
1546 optval = inp->inp_ip_minttl;
1549 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1552 optval = OPTBIT(INP_RECVOPTS);
1555 case IP_RECVRETOPTS:
1556 optval = OPTBIT(INP_RECVRETOPTS);
1559 case IP_RECVDSTADDR:
1560 optval = OPTBIT(INP_RECVDSTADDR);
1564 optval = OPTBIT(INP_RECVTTL);
1568 optval = OPTBIT(INP_RECVIF);
1572 if (inp->inp_flags & INP_HIGHPORT)
1573 optval = IP_PORTRANGE_HIGH;
1574 else if (inp->inp_flags & INP_LOWPORT)
1575 optval = IP_PORTRANGE_LOW;
1581 optval = OPTBIT(INP_FAITH);
1584 soopt_from_kbuf(sopt, &optval, sizeof optval);
1587 case IP_MULTICAST_IF:
1588 case IP_MULTICAST_VIF:
1589 case IP_MULTICAST_TTL:
1590 case IP_MULTICAST_LOOP:
1591 case IP_ADD_MEMBERSHIP:
1592 case IP_DROP_MEMBERSHIP:
1593 error = ip_getmoptions(sopt, inp->inp_moptions);
1596 #if defined(IPSEC) || defined(FAST_IPSEC)
1597 case IP_IPSEC_POLICY:
1599 struct mbuf *m = NULL;
1604 req = mtod(m, caddr_t);
1607 error = ipsec4_get_policy(so->so_pcb, req, len, &m);
1609 error = soopt_from_mbuf(sopt, m); /* XXX */
1617 error = ENOPROTOOPT;
1623 lwkt_replymsg(&msg->lmsg, error);
1627 * Set up IP options in pcb for insertion in output packets.
1628 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1629 * with destination address if source routed.
1632 ip_pcbopts(int optname, struct mbuf **pcbopt, struct mbuf *m)
1638 /* turn off any old options */
1642 if (m == NULL || m->m_len == 0) {
1644 * Only turning off any previous options.
1651 if (m->m_len % sizeof(int32_t))
1654 * IP first-hop destination address will be stored before
1655 * actual options; move other options back
1656 * and clear it when none present.
1658 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1661 m->m_len += sizeof(struct in_addr);
1662 cp = mtod(m, u_char *) + sizeof(struct in_addr);
1663 ovbcopy(mtod(m, caddr_t), cp, cnt);
1664 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1666 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1667 opt = cp[IPOPT_OPTVAL];
1668 if (opt == IPOPT_EOL)
1670 if (opt == IPOPT_NOP)
1673 if (cnt < IPOPT_OLEN + sizeof *cp)
1675 optlen = cp[IPOPT_OLEN];
1676 if (optlen < IPOPT_OLEN + sizeof *cp || optlen > cnt)
1687 * user process specifies route as:
1689 * D must be our final destination (but we can't
1690 * check that since we may not have connected yet).
1691 * A is first hop destination, which doesn't appear in
1692 * actual IP option, but is stored before the options.
1694 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1696 m->m_len -= sizeof(struct in_addr);
1697 cnt -= sizeof(struct in_addr);
1698 optlen -= sizeof(struct in_addr);
1699 cp[IPOPT_OLEN] = optlen;
1701 * Move first hop before start of options.
1703 bcopy(&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1704 sizeof(struct in_addr));
1706 * Then copy rest of options back
1707 * to close up the deleted entry.
1709 ovbcopy(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr),
1710 &cp[IPOPT_OFFSET+1],
1711 cnt - (IPOPT_MINOFF - 1));
1715 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1727 * The whole multicast option thing needs to be re-thought.
1728 * Several of these options are equally applicable to non-multicast
1729 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1730 * standard option (IP_TTL).
1734 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1736 static struct ifnet *
1737 ip_multicast_if(struct in_addr *a, int *ifindexp)
1744 if (ntohl(a->s_addr) >> 24 == 0) {
1745 ifindex = ntohl(a->s_addr) & 0xffffff;
1746 if (ifindex < 0 || if_index < ifindex)
1748 ifp = ifindex2ifnet[ifindex];
1750 *ifindexp = ifindex;
1752 ifp = INADDR_TO_IFP(a);
1758 * Set the IP multicast options in response to user setsockopt().
1761 ip_setmoptions(struct sockopt *sopt, struct ip_moptions **imop)
1765 struct in_addr addr;
1766 struct ip_mreq mreq;
1768 struct ip_moptions *imo = *imop;
1773 * No multicast option buffer attached to the pcb;
1774 * allocate one and initialize to default values.
1776 imo = kmalloc(sizeof *imo, M_IPMOPTS, M_WAITOK);
1779 imo->imo_multicast_ifp = NULL;
1780 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1781 imo->imo_multicast_vif = -1;
1782 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1783 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1784 imo->imo_num_memberships = 0;
1786 switch (sopt->sopt_name) {
1787 /* store an index number for the vif you wanna use in the send */
1788 case IP_MULTICAST_VIF:
1789 if (legal_vif_num == 0) {
1793 error = soopt_to_kbuf(sopt, &i, sizeof i, sizeof i);
1796 if (!legal_vif_num(i) && (i != -1)) {
1800 imo->imo_multicast_vif = i;
1803 case IP_MULTICAST_IF:
1805 * Select the interface for outgoing multicast packets.
1807 error = soopt_to_kbuf(sopt, &addr, sizeof addr, sizeof addr);
1812 * INADDR_ANY is used to remove a previous selection.
1813 * When no interface is selected, a default one is
1814 * chosen every time a multicast packet is sent.
1816 if (addr.s_addr == INADDR_ANY) {
1817 imo->imo_multicast_ifp = NULL;
1821 * The selected interface is identified by its local
1822 * IP address. Find the interface and confirm that
1823 * it supports multicasting.
1826 ifp = ip_multicast_if(&addr, &ifindex);
1827 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
1829 error = EADDRNOTAVAIL;
1832 imo->imo_multicast_ifp = ifp;
1834 imo->imo_multicast_addr = addr;
1836 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1840 case IP_MULTICAST_TTL:
1842 * Set the IP time-to-live for outgoing multicast packets.
1843 * The original multicast API required a char argument,
1844 * which is inconsistent with the rest of the socket API.
1845 * We allow either a char or an int.
1847 if (sopt->sopt_valsize == 1) {
1849 error = soopt_to_kbuf(sopt, &ttl, 1, 1);
1852 imo->imo_multicast_ttl = ttl;
1855 error = soopt_to_kbuf(sopt, &ttl, sizeof ttl, sizeof ttl);
1861 imo->imo_multicast_ttl = ttl;
1865 case IP_MULTICAST_LOOP:
1867 * Set the loopback flag for outgoing multicast packets.
1868 * Must be zero or one. The original multicast API required a
1869 * char argument, which is inconsistent with the rest
1870 * of the socket API. We allow either a char or an int.
1872 if (sopt->sopt_valsize == 1) {
1875 error = soopt_to_kbuf(sopt, &loop, 1, 1);
1878 imo->imo_multicast_loop = !!loop;
1882 error = soopt_to_kbuf(sopt, &loop, sizeof loop,
1886 imo->imo_multicast_loop = !!loop;
1890 case IP_ADD_MEMBERSHIP:
1892 * Add a multicast group membership.
1893 * Group must be a valid IP multicast address.
1895 error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq);
1899 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1905 * If no interface address was provided, use the interface of
1906 * the route to the given multicast address.
1908 if (mreq.imr_interface.s_addr == INADDR_ANY) {
1909 struct sockaddr_in dst;
1912 bzero(&dst, sizeof(struct sockaddr_in));
1913 dst.sin_len = sizeof(struct sockaddr_in);
1914 dst.sin_family = AF_INET;
1915 dst.sin_addr = mreq.imr_multiaddr;
1916 rt = rtlookup((struct sockaddr *)&dst);
1918 error = EADDRNOTAVAIL;
1925 ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1929 * See if we found an interface, and confirm that it
1930 * supports multicast.
1932 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
1933 error = EADDRNOTAVAIL;
1938 * See if the membership already exists or if all the
1939 * membership slots are full.
1941 for (i = 0; i < imo->imo_num_memberships; ++i) {
1942 if (imo->imo_membership[i]->inm_ifp == ifp &&
1943 imo->imo_membership[i]->inm_addr.s_addr
1944 == mreq.imr_multiaddr.s_addr)
1947 if (i < imo->imo_num_memberships) {
1952 if (i == IP_MAX_MEMBERSHIPS) {
1953 error = ETOOMANYREFS;
1958 * Everything looks good; add a new record to the multicast
1959 * address list for the given interface.
1961 if ((imo->imo_membership[i] =
1962 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1967 ++imo->imo_num_memberships;
1971 case IP_DROP_MEMBERSHIP:
1973 * Drop a multicast group membership.
1974 * Group must be a valid IP multicast address.
1976 error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq);
1980 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1987 * If an interface address was specified, get a pointer
1988 * to its ifnet structure.
1990 if (mreq.imr_interface.s_addr == INADDR_ANY)
1993 ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1995 error = EADDRNOTAVAIL;
2001 * Find the membership in the membership array.
2003 for (i = 0; i < imo->imo_num_memberships; ++i) {
2005 imo->imo_membership[i]->inm_ifp == ifp) &&
2006 imo->imo_membership[i]->inm_addr.s_addr ==
2007 mreq.imr_multiaddr.s_addr)
2010 if (i == imo->imo_num_memberships) {
2011 error = EADDRNOTAVAIL;
2016 * Give up the multicast address record to which the
2017 * membership points.
2019 in_delmulti(imo->imo_membership[i]);
2021 * Remove the gap in the membership array.
2023 for (++i; i < imo->imo_num_memberships; ++i)
2024 imo->imo_membership[i-1] = imo->imo_membership[i];
2025 --imo->imo_num_memberships;
2035 * If all options have default values, no need to keep the mbuf.
2037 if (imo->imo_multicast_ifp == NULL &&
2038 imo->imo_multicast_vif == -1 &&
2039 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
2040 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
2041 imo->imo_num_memberships == 0) {
2042 kfree(*imop, M_IPMOPTS);
2050 * Return the IP multicast options in response to user getsockopt().
2053 ip_getmoptions(struct sockopt *sopt, struct ip_moptions *imo)
2055 struct in_addr addr;
2056 struct in_ifaddr *ia;
2061 switch (sopt->sopt_name) {
2062 case IP_MULTICAST_VIF:
2064 optval = imo->imo_multicast_vif;
2067 soopt_from_kbuf(sopt, &optval, sizeof optval);
2070 case IP_MULTICAST_IF:
2071 if (imo == NULL || imo->imo_multicast_ifp == NULL)
2072 addr.s_addr = INADDR_ANY;
2073 else if (imo->imo_multicast_addr.s_addr) {
2074 /* return the value user has set */
2075 addr = imo->imo_multicast_addr;
2077 ia = IFP_TO_IA(imo->imo_multicast_ifp);
2078 addr.s_addr = (ia == NULL) ? INADDR_ANY
2079 : IA_SIN(ia)->sin_addr.s_addr;
2081 soopt_from_kbuf(sopt, &addr, sizeof addr);
2084 case IP_MULTICAST_TTL:
2086 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
2088 optval = coptval = imo->imo_multicast_ttl;
2089 if (sopt->sopt_valsize == 1)
2090 soopt_from_kbuf(sopt, &coptval, 1);
2092 soopt_from_kbuf(sopt, &optval, sizeof optval);
2095 case IP_MULTICAST_LOOP:
2097 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
2099 optval = coptval = imo->imo_multicast_loop;
2100 if (sopt->sopt_valsize == 1)
2101 soopt_from_kbuf(sopt, &coptval, 1);
2103 soopt_from_kbuf(sopt, &optval, sizeof optval);
2107 error = ENOPROTOOPT;
2114 * Discard the IP multicast options.
2117 ip_freemoptions(struct ip_moptions *imo)
2122 for (i = 0; i < imo->imo_num_memberships; ++i)
2123 in_delmulti(imo->imo_membership[i]);
2124 kfree(imo, M_IPMOPTS);
2129 * Routine called from ip_output() to loop back a copy of an IP multicast
2130 * packet to the input queue of a specified interface. Note that this
2131 * calls the output routine of the loopback "driver", but with an interface
2132 * pointer that might NOT be a loopback interface -- evil, but easier than
2133 * replicating that code here.
2136 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
2142 copym = m_copypacket(m, MB_DONTWAIT);
2143 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
2144 copym = m_pullup(copym, hlen);
2145 if (copym != NULL) {
2147 * if the checksum hasn't been computed, mark it as valid
2149 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2150 in_delayed_cksum(copym);
2151 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2152 copym->m_pkthdr.csum_flags |=
2153 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2154 copym->m_pkthdr.csum_data = 0xffff;
2157 * We don't bother to fragment if the IP length is greater
2158 * than the interface's MTU. Can this possibly matter?
2160 ip = mtod(copym, struct ip *);
2161 ip->ip_len = htons(ip->ip_len);
2162 ip->ip_off = htons(ip->ip_off);
2164 if (ip->ip_vhl == IP_VHL_BORING) {
2165 ip->ip_sum = in_cksum_hdr(ip);
2167 ip->ip_sum = in_cksum(copym, hlen);
2171 * It's not clear whether there are any lingering
2172 * reentrancy problems in other areas which might
2173 * be exposed by using ip_input directly (in
2174 * particular, everything which modifies the packet
2175 * in-place). Yet another option is using the
2176 * protosw directly to deliver the looped back
2177 * packet. For the moment, we'll err on the side
2178 * of safety by using if_simloop().
2181 if (dst->sin_family != AF_INET) {
2182 kprintf("ip_mloopback: bad address family %d\n",
2184 dst->sin_family = AF_INET;
2187 get_mplock(); /* is if_simloop() mpsafe yet? */
2188 if_simloop(ifp, copym, dst->sin_family, 0);