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.67 2008/10/28 03:07:28 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>
54 #include <sys/sysctl.h>
55 #include <sys/in_cksum.h>
58 #include <sys/thread2.h>
59 #include <sys/mplock2.h>
60 #include <sys/msgport2.h>
63 #include <net/netisr.h>
65 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/in_pcb.h>
71 #include <netinet/in_var.h>
72 #include <netinet/ip_var.h>
74 #include <netproto/mpls/mpls_var.h>
76 static MALLOC_DEFINE(M_IPMOPTS
, "ip_moptions", "internet multicast options");
79 #include <netinet6/ipsec.h>
80 #include <netproto/key/key.h>
82 #include <netproto/key/key_debug.h>
84 #define KEYDEBUG(lev,arg)
89 #include <netproto/ipsec/ipsec.h>
90 #include <netproto/ipsec/xform.h>
91 #include <netproto/ipsec/key.h>
94 #include <net/ipfw/ip_fw.h>
95 #include <net/dummynet/ip_dummynet.h>
97 #define print_ip(x, a, y) kprintf("%s %d.%d.%d.%d%s",\
98 x, (ntohl(a.s_addr)>>24)&0xFF,\
99 (ntohl(a.s_addr)>>16)&0xFF,\
100 (ntohl(a.s_addr)>>8)&0xFF,\
101 (ntohl(a.s_addr))&0xFF, y);
105 #ifdef MBUF_STRESS_TEST
106 int mbuf_frag_size
= 0;
107 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, mbuf_frag_size
, CTLFLAG_RW
,
108 &mbuf_frag_size
, 0, "Fragment outgoing mbufs to this size");
111 static struct mbuf
*ip_insertoptions(struct mbuf
*, struct mbuf
*, int *);
112 static struct ifnet
*ip_multicast_if(struct in_addr
*, int *);
113 static void ip_mloopback
114 (struct ifnet
*, struct mbuf
*, struct sockaddr_in
*, int);
115 static int ip_getmoptions
116 (struct sockopt
*, struct ip_moptions
*);
117 static int ip_pcbopts(int, struct mbuf
**, struct mbuf
*);
118 static int ip_setmoptions
119 (struct sockopt
*, struct ip_moptions
**);
121 int ip_optcopy(struct ip
*, struct ip
*);
123 extern int route_assert_owner_access
;
125 extern struct protosw inetsw
[];
128 ip_localforward(struct mbuf
*m
, const struct sockaddr_in
*dst
, int hlen
)
130 struct in_ifaddr_container
*iac
;
133 * We need to figure out if we have been forwarded to a local
134 * socket. If so, then we should somehow "loop back" to
135 * ip_input(), and get directed to the PCB as if we had received
136 * this packet. This is because it may be difficult to identify
137 * the packets you want to forward until they are being output
138 * and have selected an interface (e.g. locally initiated
139 * packets). If we used the loopback inteface, we would not be
140 * able to control what happens as the packet runs through
141 * ip_input() as it is done through a ISR.
143 LIST_FOREACH(iac
, INADDR_HASH(dst
->sin_addr
.s_addr
), ia_hash
) {
145 * If the addr to forward to is one of ours, we pretend
146 * to be the destination for this packet.
148 if (IA_SIN(iac
->ia
)->sin_addr
.s_addr
== dst
->sin_addr
.s_addr
)
154 if (m
->m_pkthdr
.rcvif
== NULL
)
155 m
->m_pkthdr
.rcvif
= ifunit("lo0");
156 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
157 m
->m_pkthdr
.csum_flags
|= CSUM_DATA_VALID
|
159 m
->m_pkthdr
.csum_data
= 0xffff;
161 m
->m_pkthdr
.csum_flags
|= CSUM_IP_CHECKED
| CSUM_IP_VALID
;
164 * Make sure that the IP header is in one mbuf,
165 * required by ip_input
167 if (m
->m_len
< hlen
) {
168 m
= m_pullup(m
, hlen
);
170 /* The packet was freed; we are done */
174 ip
= mtod(m
, struct ip
*);
176 ip
->ip_len
= htons(ip
->ip_len
);
177 ip
->ip_off
= htons(ip
->ip_off
);
180 return 1; /* The packet gets forwarded locally */
186 * IP output. The packet in mbuf chain m contains a skeletal IP
187 * header (with len, off, ttl, proto, tos, src, dst).
188 * The mbuf chain containing the packet will be freed.
189 * The mbuf opt, if present, will not be freed.
192 ip_output(struct mbuf
*m0
, struct mbuf
*opt
, struct route
*ro
,
193 int flags
, struct ip_moptions
*imo
, struct inpcb
*inp
)
196 struct ifnet
*ifp
= NULL
; /* keep compiler happy */
198 int hlen
= sizeof(struct ip
);
200 struct sockaddr_in
*dst
= NULL
; /* keep compiler happy */
201 struct in_ifaddr
*ia
= NULL
;
202 int isbroadcast
, sw_csum
;
203 struct in_addr pkt_dst
;
204 struct route iproute
;
207 struct secpolicy
*sp
= NULL
;
208 struct socket
*so
= inp
? inp
->inp_socket
: NULL
;
211 struct secpolicy
*sp
= NULL
;
212 struct tdb_ident
*tdbi
;
213 #endif /* FAST_IPSEC */
214 struct sockaddr_in
*next_hop
= NULL
;
215 int src_was_INADDR_ANY
= 0; /* as the name says... */
222 bzero(ro
, sizeof *ro
);
223 } else if (ro
->ro_rt
!= NULL
&& ro
->ro_rt
->rt_cpuid
!= mycpuid
) {
224 if (flags
& IP_DEBUGROUTE
) {
225 if (route_assert_owner_access
) {
227 "rt rt_cpuid %d accessed on cpu %d\n",
228 ro
->ro_rt
->rt_cpuid
, mycpuid
);
230 kprintf("ip_output: "
231 "rt rt_cpuid %d accessed on cpu %d\n",
232 ro
->ro_rt
->rt_cpuid
, mycpuid
);
239 * If the cached rtentry's owner CPU is not the current CPU,
240 * then don't touch the cached rtentry (remote free is too
241 * expensive in this context); just relocate the route.
244 bzero(ro
, sizeof *ro
);
247 if (m
->m_pkthdr
.fw_flags
& IPFORWARD_MBUF_TAGGED
) {
249 mtag
= m_tag_find(m
, PACKET_TAG_IPFORWARD
, NULL
);
250 KKASSERT(mtag
!= NULL
);
251 next_hop
= m_tag_data(mtag
);
254 if (m
->m_pkthdr
.fw_flags
& DUMMYNET_MBUF_TAGGED
) {
255 struct dn_pkt
*dn_pkt
;
257 /* Extract info from dummynet tag */
258 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
259 KKASSERT(mtag
!= NULL
);
260 dn_pkt
= m_tag_data(mtag
);
263 * The packet was already tagged, so part of the
264 * processing was already done, and we need to go down.
265 * Get the calculated parameters from the tag.
269 KKASSERT(ro
== &iproute
);
270 *ro
= dn_pkt
->ro
; /* structure copy */
271 KKASSERT(ro
->ro_rt
== NULL
|| ro
->ro_rt
->rt_cpuid
== mycpuid
);
273 dst
= dn_pkt
->dn_dst
;
274 if (dst
== (struct sockaddr_in
*)&(dn_pkt
->ro
.ro_dst
)) {
275 /* If 'dst' points into dummynet tag, adjust it */
276 dst
= (struct sockaddr_in
*)&(ro
->ro_dst
);
279 ip
= mtod(m
, struct ip
*);
280 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
282 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
288 m
= ip_insertoptions(m
, opt
, &len
);
292 ip
= mtod(m
, struct ip
*);
297 if (!(flags
& (IP_FORWARDING
|IP_RAWOUTPUT
))) {
298 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, hlen
>> 2);
300 ip
->ip_id
= ip_newid();
301 ipstat
.ips_localout
++;
303 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
307 pkt_dst
= next_hop
? next_hop
->sin_addr
: ip
->ip_dst
;
309 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
311 * If there is a cached route,
312 * check that it is to the same destination
313 * and is still up. If not, free it and try again.
314 * The address family should also be checked in case of sharing the
318 (!(ro
->ro_rt
->rt_flags
& RTF_UP
) ||
319 dst
->sin_family
!= AF_INET
||
320 dst
->sin_addr
.s_addr
!= pkt_dst
.s_addr
)) {
324 if (ro
->ro_rt
== NULL
) {
325 bzero(dst
, sizeof *dst
);
326 dst
->sin_family
= AF_INET
;
327 dst
->sin_len
= sizeof *dst
;
328 dst
->sin_addr
= pkt_dst
;
331 * If routing to interface only,
332 * short circuit routing lookup.
334 if (flags
& IP_ROUTETOIF
) {
335 if ((ia
= ifatoia(ifa_ifwithdstaddr(sintosa(dst
)))) == NULL
&&
336 (ia
= ifatoia(ifa_ifwithnet(sintosa(dst
)))) == NULL
) {
337 ipstat
.ips_noroute
++;
343 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
344 } else if (IN_MULTICAST(ntohl(pkt_dst
.s_addr
)) &&
345 imo
!= NULL
&& imo
->imo_multicast_ifp
!= NULL
) {
347 * Bypass the normal routing lookup for multicast
348 * packets if the interface is specified.
350 ifp
= imo
->imo_multicast_ifp
;
352 isbroadcast
= 0; /* fool gcc */
355 * If this is the case, we probably don't want to allocate
356 * a protocol-cloned route since we didn't get one from the
357 * ULP. This lets TCP do its thing, while not burdening
358 * forwarding or ICMP with the overhead of cloning a route.
359 * Of course, we still want to do any cloning requested by
360 * the link layer, as this is probably required in all cases
361 * for correct operation (as it is for ARP).
363 if (ro
->ro_rt
== NULL
)
364 rtalloc_ign(ro
, RTF_PRCLONING
);
365 if (ro
->ro_rt
== NULL
) {
366 ipstat
.ips_noroute
++;
367 error
= EHOSTUNREACH
;
370 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
371 ifp
= ro
->ro_rt
->rt_ifp
;
373 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
374 dst
= (struct sockaddr_in
*)ro
->ro_rt
->rt_gateway
;
375 if (ro
->ro_rt
->rt_flags
& RTF_HOST
)
376 isbroadcast
= (ro
->ro_rt
->rt_flags
& RTF_BROADCAST
);
378 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
380 if (IN_MULTICAST(ntohl(pkt_dst
.s_addr
))) {
381 struct in_multi
*inm
;
383 m
->m_flags
|= M_MCAST
;
385 * IP destination address is multicast. Make sure "dst"
386 * still points to the address in "ro". (It may have been
387 * changed to point to a gateway address, above.)
389 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
391 * See if the caller provided any multicast options
394 ip
->ip_ttl
= imo
->imo_multicast_ttl
;
395 if (imo
->imo_multicast_vif
!= -1) {
398 ip_mcast_src(imo
->imo_multicast_vif
) :
402 ip
->ip_ttl
= IP_DEFAULT_MULTICAST_TTL
;
405 * Confirm that the outgoing interface supports multicast.
407 if ((imo
== NULL
) || (imo
->imo_multicast_vif
== -1)) {
408 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
409 ipstat
.ips_noroute
++;
415 * If source address not specified yet, use address
416 * of outgoing interface.
418 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
419 /* Interface may have no addresses. */
421 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
424 IN_LOOKUP_MULTI(pkt_dst
, ifp
, inm
);
426 (imo
== NULL
|| imo
->imo_multicast_loop
)) {
428 * If we belong to the destination multicast group
429 * on the outgoing interface, and the caller did not
430 * forbid loopback, loop back a copy.
432 ip_mloopback(ifp
, m
, dst
, hlen
);
435 * If we are acting as a multicast router, perform
436 * multicast forwarding as if the packet had just
437 * arrived on the interface to which we are about
438 * to send. The multicast forwarding function
439 * recursively calls this function, using the
440 * IP_FORWARDING flag to prevent infinite recursion.
442 * Multicasts that are looped back by ip_mloopback(),
443 * above, will be forwarded by the ip_input() routine,
446 if (ip_mrouter
&& !(flags
& IP_FORWARDING
)) {
448 * If rsvp daemon is not running, do not
449 * set ip_moptions. This ensures that the packet
450 * is multicast and not just sent down one link
451 * as prescribed by rsvpd.
457 if (ip_mforward(ip
, ifp
, m
, imo
) != 0) {
468 * Multicasts with a time-to-live of zero may be looped-
469 * back, above, but must not be transmitted on a network.
470 * Also, multicasts addressed to the loopback interface
471 * are not sent -- the above call to ip_mloopback() will
472 * loop back a copy if this host actually belongs to the
473 * destination group on the loopback interface.
475 if (ip
->ip_ttl
== 0 || ifp
->if_flags
& IFF_LOOPBACK
) {
482 m
->m_flags
&= ~M_MCAST
;
486 * If the source address is not specified yet, use the address
487 * of the outoing interface. In case, keep note we did that, so
488 * if the the firewall changes the next-hop causing the output
489 * interface to change, we can fix that.
491 if (ip
->ip_src
.s_addr
== INADDR_ANY
|| src_was_INADDR_ANY
) {
492 /* Interface may have no addresses. */
494 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
495 src_was_INADDR_ANY
= 1;
501 * Disable packet drop hack.
502 * Packetdrop should be done by queueing.
506 * Verify that we have any chance at all of being able to queue
507 * the packet or packet fragments
509 if ((ifp
->if_snd
.ifq_len
+ ip
->ip_len
/ ifp
->if_mtu
+ 1) >=
510 ifp
->if_snd
.ifq_maxlen
) {
512 ipstat
.ips_odropped
++;
518 * Look for broadcast address and
519 * verify user is allowed to send
523 if (!(ifp
->if_flags
& IFF_BROADCAST
)) {
524 error
= EADDRNOTAVAIL
;
527 if (!(flags
& IP_ALLOWBROADCAST
)) {
531 /* don't allow broadcast messages to be fragmented */
532 if (ip
->ip_len
> ifp
->if_mtu
) {
536 m
->m_flags
|= M_BCAST
;
538 m
->m_flags
&= ~M_BCAST
;
543 /* get SP for this packet */
545 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, flags
, &error
);
547 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
550 ipsecstat
.out_inval
++;
557 switch (sp
->policy
) {
558 case IPSEC_POLICY_DISCARD
:
560 * This packet is just discarded.
562 ipsecstat
.out_polvio
++;
565 case IPSEC_POLICY_BYPASS
:
566 case IPSEC_POLICY_NONE
:
567 case IPSEC_POLICY_TCP
:
568 /* no need to do IPsec. */
571 case IPSEC_POLICY_IPSEC
:
572 if (sp
->req
== NULL
) {
573 /* acquire a policy */
574 error
= key_spdacquire(sp
);
579 case IPSEC_POLICY_ENTRUST
:
581 kprintf("ip_output: Invalid policy found. %d\n", sp
->policy
);
584 struct ipsec_output_state state
;
585 bzero(&state
, sizeof state
);
587 if (flags
& IP_ROUTETOIF
) {
589 bzero(&iproute
, sizeof iproute
);
592 state
.dst
= (struct sockaddr
*)dst
;
598 * delayed checksums are not currently compatible with IPsec
600 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
602 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
605 ip
->ip_len
= htons(ip
->ip_len
);
606 ip
->ip_off
= htons(ip
->ip_off
);
608 error
= ipsec4_output(&state
, sp
, flags
);
611 if (flags
& IP_ROUTETOIF
) {
613 * if we have tunnel mode SA, we may need to ignore
616 if (state
.ro
!= &iproute
|| state
.ro
->ro_rt
!= NULL
) {
617 flags
&= ~IP_ROUTETOIF
;
622 dst
= (struct sockaddr_in
*)state
.dst
;
624 /* mbuf is already reclaimed in ipsec4_output. */
634 kprintf("ip4_output (ipsec): error code %d\n", error
);
637 /* don't show these error codes to the user */
645 /* be sure to update variables that are affected by ipsec4_output() */
646 ip
= mtod(m
, struct ip
*);
648 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
650 hlen
= ip
->ip_hl
<< 2;
652 if (ro
->ro_rt
== NULL
) {
653 if (!(flags
& IP_ROUTETOIF
)) {
654 kprintf("ip_output: "
655 "can't update route after IPsec processing\n");
656 error
= EHOSTUNREACH
; /*XXX*/
660 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
661 ifp
= ro
->ro_rt
->rt_ifp
;
664 /* make it flipped, again. */
665 ip
->ip_len
= ntohs(ip
->ip_len
);
666 ip
->ip_off
= ntohs(ip
->ip_off
);
671 * Check the security policy (SP) for the packet and, if
672 * required, do IPsec-related processing. There are two
673 * cases here; the first time a packet is sent through
674 * it will be untagged and handled by ipsec4_checkpolicy.
675 * If the packet is resubmitted to ip_output (e.g. after
676 * AH, ESP, etc. processing), there will be a tag to bypass
677 * the lookup and related policy checking.
679 mtag
= m_tag_find(m
, PACKET_TAG_IPSEC_PENDING_TDB
, NULL
);
682 tdbi
= (struct tdb_ident
*)m_tag_data(mtag
);
683 sp
= ipsec_getpolicy(tdbi
, IPSEC_DIR_OUTBOUND
);
685 error
= -EINVAL
; /* force silent drop */
686 m_tag_delete(m
, mtag
);
688 sp
= ipsec4_checkpolicy(m
, IPSEC_DIR_OUTBOUND
, flags
,
692 * There are four return cases:
693 * sp != NULL apply IPsec policy
694 * sp == NULL, error == 0 no IPsec handling needed
695 * sp == NULL, error == -EINVAL discard packet w/o error
696 * sp == NULL, error != 0 discard packet, report error
699 /* Loop detection, check if ipsec processing already done */
700 KASSERT(sp
->req
!= NULL
, ("ip_output: no ipsec request"));
701 for (mtag
= m_tag_first(m
); mtag
!= NULL
;
702 mtag
= m_tag_next(m
, mtag
)) {
703 if (mtag
->m_tag_cookie
!= MTAG_ABI_COMPAT
)
705 if (mtag
->m_tag_id
!= PACKET_TAG_IPSEC_OUT_DONE
&&
706 mtag
->m_tag_id
!= PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED
)
709 * Check if policy has an SA associated with it.
710 * This can happen when an SP has yet to acquire
711 * an SA; e.g. on first reference. If it occurs,
712 * then we let ipsec4_process_packet do its thing.
714 if (sp
->req
->sav
== NULL
)
716 tdbi
= (struct tdb_ident
*)m_tag_data(mtag
);
717 if (tdbi
->spi
== sp
->req
->sav
->spi
&&
718 tdbi
->proto
== sp
->req
->sav
->sah
->saidx
.proto
&&
719 bcmp(&tdbi
->dst
, &sp
->req
->sav
->sah
->saidx
.dst
,
720 sizeof(union sockaddr_union
)) == 0) {
722 * No IPsec processing is needed, free
725 * NB: null pointer to avoid free at
728 KEY_FREESP(&sp
), sp
= NULL
;
735 * Do delayed checksums now because we send before
736 * this is done in the normal processing path.
738 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
740 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
743 ip
->ip_len
= htons(ip
->ip_len
);
744 ip
->ip_off
= htons(ip
->ip_off
);
746 /* NB: callee frees mbuf */
747 error
= ipsec4_process_packet(m
, sp
->req
, flags
, 0);
749 * Preserve KAME behaviour: ENOENT can be returned
750 * when an SA acquire is in progress. Don't propagate
751 * this to user-level; it confuses applications.
753 * XXX this will go away when the SADB is redone.
764 * Hack: -EINVAL is used to signal that a packet
765 * should be silently discarded. This is typically
766 * because we asked key management for an SA and
767 * it was delayed (e.g. kicked up to IKE).
769 if (error
== -EINVAL
)
773 /* No IPsec processing for this packet. */
777 * If deferred crypto processing is needed, check that
778 * the interface supports it.
780 mtag
= m_tag_find(m
, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED
, NULL
);
781 if (mtag
!= NULL
&& !(ifp
->if_capenable
& IFCAP_IPSEC
)) {
782 /* notify IPsec to do its own crypto */
783 ipsp_skipcrypto_unmark((struct tdb_ident
*)m_tag_data(mtag
));
784 error
= EHOSTUNREACH
;
790 #endif /* FAST_IPSEC */
792 /* We are already being fwd'd from a firewall. */
793 if (next_hop
!= NULL
)
797 if (!pfil_has_hooks(&inet_pfil_hook
)) {
798 if (m
->m_pkthdr
.fw_flags
& DUMMYNET_MBUF_TAGGED
) {
800 * Strip dummynet tags from stranded packets
802 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
803 KKASSERT(mtag
!= NULL
);
804 m_tag_delete(m
, mtag
);
805 m
->m_pkthdr
.fw_flags
&= ~DUMMYNET_MBUF_TAGGED
;
812 * - Xlate: translate packet's addr/port (NAT).
813 * - Firewall: deny/allow/etc.
814 * - Wrap: fake packet's addr/port <unimpl.>
815 * - Encapsulate: put it in another IP and send out. <unimp.>
819 * Run through list of hooks for output packets.
821 error
= pfil_run_hooks(&inet_pfil_hook
, &m
, ifp
, PFIL_OUT
);
822 if (error
!= 0 || m
== NULL
)
824 ip
= mtod(m
, struct ip
*);
826 if (m
->m_pkthdr
.fw_flags
& IPFORWARD_MBUF_TAGGED
) {
828 * Check dst to make sure it is directly reachable on the
829 * interface we previously thought it was.
830 * If it isn't (which may be likely in some situations) we have
831 * to re-route it (ie, find a route for the next-hop and the
832 * associated interface) and set them here. This is nested
833 * forwarding which in most cases is undesirable, except where
834 * such control is nigh impossible. So we do it here.
837 mtag
= m_tag_find(m
, PACKET_TAG_IPFORWARD
, NULL
);
838 KKASSERT(mtag
!= NULL
);
839 next_hop
= m_tag_data(mtag
);
842 * Try local forwarding first
844 if (ip_localforward(m
, next_hop
, hlen
))
848 * Relocate the route based on next_hop.
849 * If the current route is inp's cache, keep it untouched.
851 if (ro
== &iproute
&& ro
->ro_rt
!= NULL
) {
856 bzero(ro
, sizeof *ro
);
859 * Forwarding to broadcast address is not allowed.
860 * XXX Should we follow IP_ROUTETOIF?
862 flags
&= ~(IP_ALLOWBROADCAST
| IP_ROUTETOIF
);
864 /* We are doing forwarding now */
865 flags
|= IP_FORWARDING
;
870 if (m
->m_pkthdr
.fw_flags
& DUMMYNET_MBUF_TAGGED
) {
871 struct dn_pkt
*dn_pkt
;
873 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
874 KKASSERT(mtag
!= NULL
);
875 dn_pkt
= m_tag_data(mtag
);
878 * Under certain cases it is not possible to recalculate
879 * 'ro' and 'dst', let alone 'flags', so just save them in
880 * dummynet tag and avoid the possible wrong reculcalation
881 * when we come back to ip_output() again.
883 * All other parameters have been already used and so they
884 * are not needed anymore.
885 * XXX if the ifp is deleted while a pkt is in dummynet,
886 * we are in trouble! (TODO use ifnet_detach_event)
888 * We need to copy *ro because for ICMP pkts (and maybe
889 * others) the caller passed a pointer into the stack;
890 * dst might also be a pointer into *ro so it needs to
895 ro
->ro_rt
->rt_refcnt
++;
896 if (dst
== (struct sockaddr_in
*)&ro
->ro_dst
) {
897 /* 'dst' points into 'ro' */
898 dst
= (struct sockaddr_in
*)&(dn_pkt
->ro
.ro_dst
);
900 dn_pkt
->dn_dst
= dst
;
901 dn_pkt
->flags
= flags
;
907 /* 127/8 must not appear on wire - RFC1122. */
908 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
909 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
910 if (!(ifp
->if_flags
& IFF_LOOPBACK
)) {
911 ipstat
.ips_badaddr
++;
912 error
= EADDRNOTAVAIL
;
917 m
->m_pkthdr
.csum_flags
|= CSUM_IP
;
918 sw_csum
= m
->m_pkthdr
.csum_flags
& ~ifp
->if_hwassist
;
919 if (sw_csum
& CSUM_DELAY_DATA
) {
921 sw_csum
&= ~CSUM_DELAY_DATA
;
923 m
->m_pkthdr
.csum_flags
&= ifp
->if_hwassist
;
926 * If small enough for interface, or the interface will take
927 * care of the fragmentation for us, can just send directly.
929 if (ip
->ip_len
<= ifp
->if_mtu
|| ((ifp
->if_hwassist
& CSUM_FRAGMENT
) &&
930 !(ip
->ip_off
& IP_DF
))) {
931 ip
->ip_len
= htons(ip
->ip_len
);
932 ip
->ip_off
= htons(ip
->ip_off
);
934 if (sw_csum
& CSUM_DELAY_IP
) {
935 if (ip
->ip_vhl
== IP_VHL_BORING
)
936 ip
->ip_sum
= in_cksum_hdr(ip
);
938 ip
->ip_sum
= in_cksum(m
, hlen
);
941 /* Record statistics for this interface address. */
942 if (!(flags
& IP_FORWARDING
) && ia
) {
943 ia
->ia_ifa
.if_opackets
++;
944 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
948 /* clean ipsec history once it goes out of the node */
952 #ifdef MBUF_STRESS_TEST
953 if (mbuf_frag_size
&& m
->m_pkthdr
.len
> mbuf_frag_size
) {
954 struct mbuf
*m1
, *m2
;
957 tmp
= length
= m
->m_pkthdr
.len
;
959 while ((length
-= mbuf_frag_size
) >= 1) {
960 m1
= m_split(m
, length
, MB_DONTWAIT
);
964 while (m2
->m_next
!= NULL
)
968 m
->m_pkthdr
.len
= tmp
;
973 if (!mpls_output_process(m
, ro
->ro_rt
))
976 error
= ifp
->if_output(ifp
, m
, (struct sockaddr
*)dst
,
981 if (ip
->ip_off
& IP_DF
) {
984 * This case can happen if the user changed the MTU
985 * of an interface after enabling IP on it. Because
986 * most netifs don't keep track of routes pointing to
987 * them, there is no way for one to update all its
988 * routes when the MTU is changed.
990 if ((ro
->ro_rt
->rt_flags
& (RTF_UP
| RTF_HOST
)) &&
991 !(ro
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
) &&
992 (ro
->ro_rt
->rt_rmx
.rmx_mtu
> ifp
->if_mtu
)) {
993 ro
->ro_rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
995 ipstat
.ips_cantfrag
++;
1000 * Too large for interface; fragment if possible. If successful,
1001 * on return, m will point to a list of packets to be sent.
1003 error
= ip_fragment(ip
, &m
, ifp
->if_mtu
, ifp
->if_hwassist
, sw_csum
);
1008 m
->m_nextpkt
= NULL
;
1010 /* clean ipsec history once it goes out of the node */
1014 /* Record statistics for this interface address. */
1016 ia
->ia_ifa
.if_opackets
++;
1017 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1020 if (!mpls_output_process(m
, ro
->ro_rt
))
1023 error
= ifp
->if_output(ifp
, m
, (struct sockaddr
*)dst
,
1031 ipstat
.ips_fragmented
++;
1034 if (ro
== &iproute
&& ro
->ro_rt
!= NULL
) {
1040 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1041 kprintf("DP ip_output call free SP:%p\n", sp
));
1056 * Create a chain of fragments which fit the given mtu. m_frag points to the
1057 * mbuf to be fragmented; on return it points to the chain with the fragments.
1058 * Return 0 if no error. If error, m_frag may contain a partially built
1059 * chain of fragments that should be freed by the caller.
1061 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
1062 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
1065 ip_fragment(struct ip
*ip
, struct mbuf
**m_frag
, int mtu
,
1066 u_long if_hwassist_flags
, int sw_csum
)
1069 int hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1070 int len
= (mtu
- hlen
) & ~7; /* size of payload in each fragment */
1072 struct mbuf
*m0
= *m_frag
; /* the original packet */
1074 struct mbuf
**mnext
;
1077 if (ip
->ip_off
& IP_DF
) { /* Fragmentation not allowed */
1078 ipstat
.ips_cantfrag
++;
1083 * Must be able to put at least 8 bytes per fragment.
1089 * If the interface will not calculate checksums on
1090 * fragmented packets, then do it here.
1092 if ((m0
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) &&
1093 !(if_hwassist_flags
& CSUM_IP_FRAGS
)) {
1094 in_delayed_cksum(m0
);
1095 m0
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1098 if (len
> PAGE_SIZE
) {
1100 * Fragment large datagrams such that each segment
1101 * contains a multiple of PAGE_SIZE amount of data,
1102 * plus headers. This enables a receiver to perform
1103 * page-flipping zero-copy optimizations.
1105 * XXX When does this help given that sender and receiver
1106 * could have different page sizes, and also mtu could
1107 * be less than the receiver's page size ?
1112 for (m
= m0
, off
= 0; m
&& (off
+m
->m_len
) <= mtu
; m
= m
->m_next
)
1116 * firstlen (off - hlen) must be aligned on an
1120 goto smart_frag_failure
;
1121 off
= ((off
- hlen
) & ~7) + hlen
;
1122 newlen
= (~PAGE_MASK
) & mtu
;
1123 if ((newlen
+ sizeof(struct ip
)) > mtu
) {
1124 /* we failed, go back the default */
1135 firstlen
= off
- hlen
;
1136 mnext
= &m0
->m_nextpkt
; /* pointer to next packet */
1139 * Loop through length of segment after first fragment,
1140 * make new header and copy data of each part and link onto chain.
1141 * Here, m0 is the original packet, m is the fragment being created.
1142 * The fragments are linked off the m_nextpkt of the original
1143 * packet, which after processing serves as the first fragment.
1145 for (nfrags
= 1; off
< ip
->ip_len
; off
+= len
, nfrags
++) {
1146 struct ip
*mhip
; /* ip header on the fragment */
1148 int mhlen
= sizeof(struct ip
);
1150 MGETHDR(m
, MB_DONTWAIT
, MT_HEADER
);
1153 ipstat
.ips_odropped
++;
1156 m
->m_flags
|= (m0
->m_flags
& M_MCAST
) | M_FRAG
;
1158 * In the first mbuf, leave room for the link header, then
1159 * copy the original IP header including options. The payload
1160 * goes into an additional mbuf chain returned by m_copy().
1162 m
->m_data
+= max_linkhdr
;
1163 mhip
= mtod(m
, struct ip
*);
1165 if (hlen
> sizeof(struct ip
)) {
1166 mhlen
= ip_optcopy(ip
, mhip
) + sizeof(struct ip
);
1167 mhip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, mhlen
>> 2);
1170 /* XXX do we need to add ip->ip_off below ? */
1171 mhip
->ip_off
= ((off
- hlen
) >> 3) + ip
->ip_off
;
1172 if (off
+ len
>= ip
->ip_len
) { /* last fragment */
1173 len
= ip
->ip_len
- off
;
1174 m
->m_flags
|= M_LASTFRAG
;
1176 mhip
->ip_off
|= IP_MF
;
1177 mhip
->ip_len
= htons((u_short
)(len
+ mhlen
));
1178 m
->m_next
= m_copy(m0
, off
, len
);
1179 if (m
->m_next
== NULL
) { /* copy failed */
1181 error
= ENOBUFS
; /* ??? */
1182 ipstat
.ips_odropped
++;
1185 m
->m_pkthdr
.len
= mhlen
+ len
;
1186 m
->m_pkthdr
.rcvif
= NULL
;
1187 m
->m_pkthdr
.csum_flags
= m0
->m_pkthdr
.csum_flags
;
1188 mhip
->ip_off
= htons(mhip
->ip_off
);
1190 if (sw_csum
& CSUM_DELAY_IP
)
1191 mhip
->ip_sum
= in_cksum(m
, mhlen
);
1193 mnext
= &m
->m_nextpkt
;
1195 ipstat
.ips_ofragments
+= nfrags
;
1197 /* set first marker for fragment chain */
1198 m0
->m_flags
|= M_FIRSTFRAG
| M_FRAG
;
1199 m0
->m_pkthdr
.csum_data
= nfrags
;
1202 * Update first fragment by trimming what's been copied out
1203 * and updating header.
1205 m_adj(m0
, hlen
+ firstlen
- ip
->ip_len
);
1206 m0
->m_pkthdr
.len
= hlen
+ firstlen
;
1207 ip
->ip_len
= htons((u_short
)m0
->m_pkthdr
.len
);
1208 ip
->ip_off
|= IP_MF
;
1209 ip
->ip_off
= htons(ip
->ip_off
);
1211 if (sw_csum
& CSUM_DELAY_IP
)
1212 ip
->ip_sum
= in_cksum(m0
, hlen
);
1220 in_delayed_cksum(struct mbuf
*m
)
1223 u_short csum
, offset
;
1225 ip
= mtod(m
, struct ip
*);
1226 offset
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
1227 csum
= in_cksum_skip(m
, ip
->ip_len
, offset
);
1228 if (m
->m_pkthdr
.csum_flags
& CSUM_UDP
&& csum
== 0)
1230 offset
+= m
->m_pkthdr
.csum_data
; /* checksum offset */
1232 if (offset
+ sizeof(u_short
) > m
->m_len
) {
1233 kprintf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1234 m
->m_len
, offset
, ip
->ip_p
);
1237 * this shouldn't happen, but if it does, the
1238 * correct behavior may be to insert the checksum
1239 * in the existing chain instead of rearranging it.
1241 m
= m_pullup(m
, offset
+ sizeof(u_short
));
1243 *(u_short
*)(m
->m_data
+ offset
) = csum
;
1247 * Insert IP options into preformed packet.
1248 * Adjust IP destination as required for IP source routing,
1249 * as indicated by a non-zero in_addr at the start of the options.
1251 * XXX This routine assumes that the packet has no options in place.
1253 static struct mbuf
*
1254 ip_insertoptions(struct mbuf
*m
, struct mbuf
*opt
, int *phlen
)
1256 struct ipoption
*p
= mtod(opt
, struct ipoption
*);
1258 struct ip
*ip
= mtod(m
, struct ip
*);
1261 optlen
= opt
->m_len
- sizeof p
->ipopt_dst
;
1262 if (optlen
+ (u_short
)ip
->ip_len
> IP_MAXPACKET
) {
1264 return (m
); /* XXX should fail */
1266 if (p
->ipopt_dst
.s_addr
)
1267 ip
->ip_dst
= p
->ipopt_dst
;
1268 if (m
->m_flags
& M_EXT
|| m
->m_data
- optlen
< m
->m_pktdat
) {
1269 MGETHDR(n
, MB_DONTWAIT
, MT_HEADER
);
1274 n
->m_pkthdr
.rcvif
= NULL
;
1275 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
1276 m
->m_len
-= sizeof(struct ip
);
1277 m
->m_data
+= sizeof(struct ip
);
1280 m
->m_len
= optlen
+ sizeof(struct ip
);
1281 m
->m_data
+= max_linkhdr
;
1282 memcpy(mtod(m
, void *), ip
, sizeof(struct ip
));
1284 m
->m_data
-= optlen
;
1286 m
->m_pkthdr
.len
+= optlen
;
1287 ovbcopy(ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
1289 ip
= mtod(m
, struct ip
*);
1290 bcopy(p
->ipopt_list
, ip
+ 1, optlen
);
1291 *phlen
= sizeof(struct ip
) + optlen
;
1292 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, *phlen
>> 2);
1293 ip
->ip_len
+= optlen
;
1298 * Copy options from ip to jp,
1299 * omitting those not copied during fragmentation.
1302 ip_optcopy(struct ip
*ip
, struct ip
*jp
)
1305 int opt
, optlen
, cnt
;
1307 cp
= (u_char
*)(ip
+ 1);
1308 dp
= (u_char
*)(jp
+ 1);
1309 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof(struct ip
);
1310 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1312 if (opt
== IPOPT_EOL
)
1314 if (opt
== IPOPT_NOP
) {
1315 /* Preserve for IP mcast tunnel's LSRR alignment. */
1321 KASSERT(cnt
>= IPOPT_OLEN
+ sizeof *cp
,
1322 ("ip_optcopy: malformed ipv4 option"));
1323 optlen
= cp
[IPOPT_OLEN
];
1324 KASSERT(optlen
>= IPOPT_OLEN
+ sizeof *cp
&& optlen
<= cnt
,
1325 ("ip_optcopy: malformed ipv4 option"));
1327 /* bogus lengths should have been caught by ip_dooptions */
1330 if (IPOPT_COPIED(opt
)) {
1331 bcopy(cp
, dp
, optlen
);
1335 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
1341 * IP socket option processing.
1344 ip_ctloutput(netmsg_t msg
)
1346 struct socket
*so
= msg
->base
.nm_so
;
1347 struct sockopt
*sopt
= msg
->ctloutput
.nm_sopt
;
1348 struct inpcb
*inp
= so
->so_pcb
;
1352 if (sopt
->sopt_level
!= IPPROTO_IP
) {
1357 switch (sopt
->sopt_dir
) {
1359 switch (sopt
->sopt_name
) {
1366 if (sopt
->sopt_valsize
> MLEN
) {
1370 MGET(m
, sopt
->sopt_td
? MB_WAIT
: MB_DONTWAIT
, MT_HEADER
);
1375 m
->m_len
= sopt
->sopt_valsize
;
1376 error
= soopt_to_kbuf(sopt
, mtod(m
, void *), m
->m_len
,
1378 error
= ip_pcbopts(sopt
->sopt_name
,
1379 &inp
->inp_options
, m
);
1387 case IP_RECVRETOPTS
:
1388 case IP_RECVDSTADDR
:
1392 error
= soopt_to_kbuf(sopt
, &optval
, sizeof optval
,
1396 switch (sopt
->sopt_name
) {
1398 inp
->inp_ip_tos
= optval
;
1402 inp
->inp_ip_ttl
= optval
;
1405 if (optval
>= 0 && optval
<= MAXTTL
)
1406 inp
->inp_ip_minttl
= optval
;
1410 #define OPTSET(bit) \
1412 inp->inp_flags |= bit; \
1414 inp->inp_flags &= ~bit;
1417 OPTSET(INP_RECVOPTS
);
1420 case IP_RECVRETOPTS
:
1421 OPTSET(INP_RECVRETOPTS
);
1424 case IP_RECVDSTADDR
:
1425 OPTSET(INP_RECVDSTADDR
);
1433 OPTSET(INP_RECVTTL
);
1443 case IP_MULTICAST_IF
:
1444 case IP_MULTICAST_VIF
:
1445 case IP_MULTICAST_TTL
:
1446 case IP_MULTICAST_LOOP
:
1447 case IP_ADD_MEMBERSHIP
:
1448 case IP_DROP_MEMBERSHIP
:
1449 error
= ip_setmoptions(sopt
, &inp
->inp_moptions
);
1453 error
= soopt_to_kbuf(sopt
, &optval
, sizeof optval
,
1459 case IP_PORTRANGE_DEFAULT
:
1460 inp
->inp_flags
&= ~(INP_LOWPORT
);
1461 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1464 case IP_PORTRANGE_HIGH
:
1465 inp
->inp_flags
&= ~(INP_LOWPORT
);
1466 inp
->inp_flags
|= INP_HIGHPORT
;
1469 case IP_PORTRANGE_LOW
:
1470 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1471 inp
->inp_flags
|= INP_LOWPORT
;
1480 #if defined(IPSEC) || defined(FAST_IPSEC)
1481 case IP_IPSEC_POLICY
:
1489 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
1491 soopt_to_mbuf(sopt
, m
);
1492 priv
= (sopt
->sopt_td
!= NULL
&&
1493 priv_check(sopt
->sopt_td
, PRIV_ROOT
) != 0) ? 0 : 1;
1494 req
= mtod(m
, caddr_t
);
1496 optname
= sopt
->sopt_name
;
1497 error
= ipsec4_set_policy(inp
, optname
, req
, len
, priv
);
1504 error
= ENOPROTOOPT
;
1510 switch (sopt
->sopt_name
) {
1513 if (inp
->inp_options
)
1514 soopt_from_kbuf(sopt
, mtod(inp
->inp_options
,
1516 inp
->inp_options
->m_len
);
1518 sopt
->sopt_valsize
= 0;
1525 case IP_RECVRETOPTS
:
1526 case IP_RECVDSTADDR
:
1531 switch (sopt
->sopt_name
) {
1534 optval
= inp
->inp_ip_tos
;
1538 optval
= inp
->inp_ip_ttl
;
1541 optval
= inp
->inp_ip_minttl
;
1544 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1547 optval
= OPTBIT(INP_RECVOPTS
);
1550 case IP_RECVRETOPTS
:
1551 optval
= OPTBIT(INP_RECVRETOPTS
);
1554 case IP_RECVDSTADDR
:
1555 optval
= OPTBIT(INP_RECVDSTADDR
);
1559 optval
= OPTBIT(INP_RECVTTL
);
1563 optval
= OPTBIT(INP_RECVIF
);
1567 if (inp
->inp_flags
& INP_HIGHPORT
)
1568 optval
= IP_PORTRANGE_HIGH
;
1569 else if (inp
->inp_flags
& INP_LOWPORT
)
1570 optval
= IP_PORTRANGE_LOW
;
1576 optval
= OPTBIT(INP_FAITH
);
1579 soopt_from_kbuf(sopt
, &optval
, sizeof optval
);
1582 case IP_MULTICAST_IF
:
1583 case IP_MULTICAST_VIF
:
1584 case IP_MULTICAST_TTL
:
1585 case IP_MULTICAST_LOOP
:
1586 case IP_ADD_MEMBERSHIP
:
1587 case IP_DROP_MEMBERSHIP
:
1588 error
= ip_getmoptions(sopt
, inp
->inp_moptions
);
1591 #if defined(IPSEC) || defined(FAST_IPSEC)
1592 case IP_IPSEC_POLICY
:
1594 struct mbuf
*m
= NULL
;
1599 req
= mtod(m
, caddr_t
);
1602 error
= ipsec4_get_policy(so
->so_pcb
, req
, len
, &m
);
1604 error
= soopt_from_mbuf(sopt
, m
); /* XXX */
1612 error
= ENOPROTOOPT
;
1618 lwkt_replymsg(&msg
->lmsg
, error
);
1622 * Set up IP options in pcb for insertion in output packets.
1623 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1624 * with destination address if source routed.
1627 ip_pcbopts(int optname
, struct mbuf
**pcbopt
, struct mbuf
*m
)
1633 /* turn off any old options */
1637 if (m
== NULL
|| m
->m_len
== 0) {
1639 * Only turning off any previous options.
1646 if (m
->m_len
% sizeof(int32_t))
1649 * IP first-hop destination address will be stored before
1650 * actual options; move other options back
1651 * and clear it when none present.
1653 if (m
->m_data
+ m
->m_len
+ sizeof(struct in_addr
) >= &m
->m_dat
[MLEN
])
1656 m
->m_len
+= sizeof(struct in_addr
);
1657 cp
= mtod(m
, u_char
*) + sizeof(struct in_addr
);
1658 ovbcopy(mtod(m
, caddr_t
), cp
, cnt
);
1659 bzero(mtod(m
, caddr_t
), sizeof(struct in_addr
));
1661 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1662 opt
= cp
[IPOPT_OPTVAL
];
1663 if (opt
== IPOPT_EOL
)
1665 if (opt
== IPOPT_NOP
)
1668 if (cnt
< IPOPT_OLEN
+ sizeof *cp
)
1670 optlen
= cp
[IPOPT_OLEN
];
1671 if (optlen
< IPOPT_OLEN
+ sizeof *cp
|| optlen
> cnt
)
1682 * user process specifies route as:
1684 * D must be our final destination (but we can't
1685 * check that since we may not have connected yet).
1686 * A is first hop destination, which doesn't appear in
1687 * actual IP option, but is stored before the options.
1689 if (optlen
< IPOPT_MINOFF
- 1 + sizeof(struct in_addr
))
1691 m
->m_len
-= sizeof(struct in_addr
);
1692 cnt
-= sizeof(struct in_addr
);
1693 optlen
-= sizeof(struct in_addr
);
1694 cp
[IPOPT_OLEN
] = optlen
;
1696 * Move first hop before start of options.
1698 bcopy(&cp
[IPOPT_OFFSET
+1], mtod(m
, caddr_t
),
1699 sizeof(struct in_addr
));
1701 * Then copy rest of options back
1702 * to close up the deleted entry.
1704 ovbcopy(&cp
[IPOPT_OFFSET
+1] + sizeof(struct in_addr
),
1705 &cp
[IPOPT_OFFSET
+1],
1706 cnt
- (IPOPT_MINOFF
- 1));
1710 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
1722 * The whole multicast option thing needs to be re-thought.
1723 * Several of these options are equally applicable to non-multicast
1724 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1725 * standard option (IP_TTL).
1729 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1731 static struct ifnet
*
1732 ip_multicast_if(struct in_addr
*a
, int *ifindexp
)
1739 if (ntohl(a
->s_addr
) >> 24 == 0) {
1740 ifindex
= ntohl(a
->s_addr
) & 0xffffff;
1741 if (ifindex
< 0 || if_index
< ifindex
)
1743 ifp
= ifindex2ifnet
[ifindex
];
1745 *ifindexp
= ifindex
;
1747 ifp
= INADDR_TO_IFP(a
);
1753 * Set the IP multicast options in response to user setsockopt().
1756 ip_setmoptions(struct sockopt
*sopt
, struct ip_moptions
**imop
)
1760 struct in_addr addr
;
1761 struct ip_mreq mreq
;
1763 struct ip_moptions
*imo
= *imop
;
1768 * No multicast option buffer attached to the pcb;
1769 * allocate one and initialize to default values.
1771 imo
= kmalloc(sizeof *imo
, M_IPMOPTS
, M_WAITOK
);
1774 imo
->imo_multicast_ifp
= NULL
;
1775 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
1776 imo
->imo_multicast_vif
= -1;
1777 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
1778 imo
->imo_multicast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
1779 imo
->imo_num_memberships
= 0;
1781 switch (sopt
->sopt_name
) {
1782 /* store an index number for the vif you wanna use in the send */
1783 case IP_MULTICAST_VIF
:
1784 if (legal_vif_num
== 0) {
1788 error
= soopt_to_kbuf(sopt
, &i
, sizeof i
, sizeof i
);
1791 if (!legal_vif_num(i
) && (i
!= -1)) {
1795 imo
->imo_multicast_vif
= i
;
1798 case IP_MULTICAST_IF
:
1800 * Select the interface for outgoing multicast packets.
1802 error
= soopt_to_kbuf(sopt
, &addr
, sizeof addr
, sizeof addr
);
1807 * INADDR_ANY is used to remove a previous selection.
1808 * When no interface is selected, a default one is
1809 * chosen every time a multicast packet is sent.
1811 if (addr
.s_addr
== INADDR_ANY
) {
1812 imo
->imo_multicast_ifp
= NULL
;
1816 * The selected interface is identified by its local
1817 * IP address. Find the interface and confirm that
1818 * it supports multicasting.
1821 ifp
= ip_multicast_if(&addr
, &ifindex
);
1822 if (ifp
== NULL
|| !(ifp
->if_flags
& IFF_MULTICAST
)) {
1824 error
= EADDRNOTAVAIL
;
1827 imo
->imo_multicast_ifp
= ifp
;
1829 imo
->imo_multicast_addr
= addr
;
1831 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
1835 case IP_MULTICAST_TTL
:
1837 * Set the IP time-to-live for outgoing multicast packets.
1838 * The original multicast API required a char argument,
1839 * which is inconsistent with the rest of the socket API.
1840 * We allow either a char or an int.
1842 if (sopt
->sopt_valsize
== 1) {
1844 error
= soopt_to_kbuf(sopt
, &ttl
, 1, 1);
1847 imo
->imo_multicast_ttl
= ttl
;
1850 error
= soopt_to_kbuf(sopt
, &ttl
, sizeof ttl
, sizeof ttl
);
1856 imo
->imo_multicast_ttl
= ttl
;
1860 case IP_MULTICAST_LOOP
:
1862 * Set the loopback flag for outgoing multicast packets.
1863 * Must be zero or one. The original multicast API required a
1864 * char argument, which is inconsistent with the rest
1865 * of the socket API. We allow either a char or an int.
1867 if (sopt
->sopt_valsize
== 1) {
1870 error
= soopt_to_kbuf(sopt
, &loop
, 1, 1);
1873 imo
->imo_multicast_loop
= !!loop
;
1877 error
= soopt_to_kbuf(sopt
, &loop
, sizeof loop
,
1881 imo
->imo_multicast_loop
= !!loop
;
1885 case IP_ADD_MEMBERSHIP
:
1887 * Add a multicast group membership.
1888 * Group must be a valid IP multicast address.
1890 error
= soopt_to_kbuf(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
1894 if (!IN_MULTICAST(ntohl(mreq
.imr_multiaddr
.s_addr
))) {
1900 * If no interface address was provided, use the interface of
1901 * the route to the given multicast address.
1903 if (mreq
.imr_interface
.s_addr
== INADDR_ANY
) {
1904 struct sockaddr_in dst
;
1907 bzero(&dst
, sizeof(struct sockaddr_in
));
1908 dst
.sin_len
= sizeof(struct sockaddr_in
);
1909 dst
.sin_family
= AF_INET
;
1910 dst
.sin_addr
= mreq
.imr_multiaddr
;
1911 rt
= rtlookup((struct sockaddr
*)&dst
);
1913 error
= EADDRNOTAVAIL
;
1920 ifp
= ip_multicast_if(&mreq
.imr_interface
, NULL
);
1924 * See if we found an interface, and confirm that it
1925 * supports multicast.
1927 if (ifp
== NULL
|| !(ifp
->if_flags
& IFF_MULTICAST
)) {
1928 error
= EADDRNOTAVAIL
;
1933 * See if the membership already exists or if all the
1934 * membership slots are full.
1936 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
1937 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
&&
1938 imo
->imo_membership
[i
]->inm_addr
.s_addr
1939 == mreq
.imr_multiaddr
.s_addr
)
1942 if (i
< imo
->imo_num_memberships
) {
1947 if (i
== IP_MAX_MEMBERSHIPS
) {
1948 error
= ETOOMANYREFS
;
1953 * Everything looks good; add a new record to the multicast
1954 * address list for the given interface.
1956 if ((imo
->imo_membership
[i
] =
1957 in_addmulti(&mreq
.imr_multiaddr
, ifp
)) == NULL
) {
1962 ++imo
->imo_num_memberships
;
1966 case IP_DROP_MEMBERSHIP
:
1968 * Drop a multicast group membership.
1969 * Group must be a valid IP multicast address.
1971 error
= soopt_to_kbuf(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
1975 if (!IN_MULTICAST(ntohl(mreq
.imr_multiaddr
.s_addr
))) {
1982 * If an interface address was specified, get a pointer
1983 * to its ifnet structure.
1985 if (mreq
.imr_interface
.s_addr
== INADDR_ANY
)
1988 ifp
= ip_multicast_if(&mreq
.imr_interface
, NULL
);
1990 error
= EADDRNOTAVAIL
;
1996 * Find the membership in the membership array.
1998 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
2000 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
2001 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
2002 mreq
.imr_multiaddr
.s_addr
)
2005 if (i
== imo
->imo_num_memberships
) {
2006 error
= EADDRNOTAVAIL
;
2011 * Give up the multicast address record to which the
2012 * membership points.
2014 in_delmulti(imo
->imo_membership
[i
]);
2016 * Remove the gap in the membership array.
2018 for (++i
; i
< imo
->imo_num_memberships
; ++i
)
2019 imo
->imo_membership
[i
-1] = imo
->imo_membership
[i
];
2020 --imo
->imo_num_memberships
;
2030 * If all options have default values, no need to keep the mbuf.
2032 if (imo
->imo_multicast_ifp
== NULL
&&
2033 imo
->imo_multicast_vif
== -1 &&
2034 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
2035 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
2036 imo
->imo_num_memberships
== 0) {
2037 kfree(*imop
, M_IPMOPTS
);
2045 * Return the IP multicast options in response to user getsockopt().
2048 ip_getmoptions(struct sockopt
*sopt
, struct ip_moptions
*imo
)
2050 struct in_addr addr
;
2051 struct in_ifaddr
*ia
;
2056 switch (sopt
->sopt_name
) {
2057 case IP_MULTICAST_VIF
:
2059 optval
= imo
->imo_multicast_vif
;
2062 soopt_from_kbuf(sopt
, &optval
, sizeof optval
);
2065 case IP_MULTICAST_IF
:
2066 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
)
2067 addr
.s_addr
= INADDR_ANY
;
2068 else if (imo
->imo_multicast_addr
.s_addr
) {
2069 /* return the value user has set */
2070 addr
= imo
->imo_multicast_addr
;
2072 ia
= IFP_TO_IA(imo
->imo_multicast_ifp
);
2073 addr
.s_addr
= (ia
== NULL
) ? INADDR_ANY
2074 : IA_SIN(ia
)->sin_addr
.s_addr
;
2076 soopt_from_kbuf(sopt
, &addr
, sizeof addr
);
2079 case IP_MULTICAST_TTL
:
2081 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
2083 optval
= coptval
= imo
->imo_multicast_ttl
;
2084 if (sopt
->sopt_valsize
== 1)
2085 soopt_from_kbuf(sopt
, &coptval
, 1);
2087 soopt_from_kbuf(sopt
, &optval
, sizeof optval
);
2090 case IP_MULTICAST_LOOP
:
2092 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
2094 optval
= coptval
= imo
->imo_multicast_loop
;
2095 if (sopt
->sopt_valsize
== 1)
2096 soopt_from_kbuf(sopt
, &coptval
, 1);
2098 soopt_from_kbuf(sopt
, &optval
, sizeof optval
);
2102 error
= ENOPROTOOPT
;
2109 * Discard the IP multicast options.
2112 ip_freemoptions(struct ip_moptions
*imo
)
2117 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
)
2118 in_delmulti(imo
->imo_membership
[i
]);
2119 kfree(imo
, M_IPMOPTS
);
2124 * Routine called from ip_output() to loop back a copy of an IP multicast
2125 * packet to the input queue of a specified interface. Note that this
2126 * calls the output routine of the loopback "driver", but with an interface
2127 * pointer that might NOT be a loopback interface -- evil, but easier than
2128 * replicating that code here.
2131 ip_mloopback(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr_in
*dst
,
2137 copym
= m_copypacket(m
, MB_DONTWAIT
);
2138 if (copym
!= NULL
&& (copym
->m_flags
& M_EXT
|| copym
->m_len
< hlen
))
2139 copym
= m_pullup(copym
, hlen
);
2140 if (copym
!= NULL
) {
2142 * if the checksum hasn't been computed, mark it as valid
2144 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
2145 in_delayed_cksum(copym
);
2146 copym
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
2147 copym
->m_pkthdr
.csum_flags
|=
2148 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
2149 copym
->m_pkthdr
.csum_data
= 0xffff;
2152 * We don't bother to fragment if the IP length is greater
2153 * than the interface's MTU. Can this possibly matter?
2155 ip
= mtod(copym
, struct ip
*);
2156 ip
->ip_len
= htons(ip
->ip_len
);
2157 ip
->ip_off
= htons(ip
->ip_off
);
2159 if (ip
->ip_vhl
== IP_VHL_BORING
) {
2160 ip
->ip_sum
= in_cksum_hdr(ip
);
2162 ip
->ip_sum
= in_cksum(copym
, hlen
);
2166 * It's not clear whether there are any lingering
2167 * reentrancy problems in other areas which might
2168 * be exposed by using ip_input directly (in
2169 * particular, everything which modifies the packet
2170 * in-place). Yet another option is using the
2171 * protosw directly to deliver the looped back
2172 * packet. For the moment, we'll err on the side
2173 * of safety by using if_simloop().
2176 if (dst
->sin_family
!= AF_INET
) {
2177 kprintf("ip_mloopback: bad address family %d\n",
2179 dst
->sin_family
= AF_INET
;
2182 get_mplock(); /* is if_simloop() mpsafe yet? */
2183 if_simloop(ifp
, copym
, dst
->sin_family
, 0);