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 int ip_do_rfc6864
= 1;
109 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, rfc6864
, CTLFLAG_RW
, &ip_do_rfc6864
, 0,
110 "Don't generate IP ID for DF IP datagrams");
112 static struct mbuf
*ip_insertoptions(struct mbuf
*, struct mbuf
*, int *);
113 static struct ifnet
*ip_multicast_if(struct in_addr
*, int *);
114 static void ip_mloopback
115 (struct ifnet
*, struct mbuf
*, struct sockaddr_in
*, int);
116 static int ip_getmoptions
117 (struct sockopt
*, struct ip_moptions
*);
118 static int ip_pcbopts(int, struct mbuf
**, struct mbuf
*);
119 static int ip_setmoptions
120 (struct sockopt
*, struct ip_moptions
**);
122 int ip_optcopy(struct ip
*, struct ip
*);
124 extern struct protosw inetsw
[];
127 ip_localforward(struct mbuf
*m
, const struct sockaddr_in
*dst
, int hlen
)
129 struct in_ifaddr_container
*iac
;
132 * We need to figure out if we have been forwarded to a local
133 * socket. If so, then we should somehow "loop back" to
134 * ip_input(), and get directed to the PCB as if we had received
135 * this packet. This is because it may be difficult to identify
136 * the packets you want to forward until they are being output
137 * and have selected an interface (e.g. locally initiated
138 * packets). If we used the loopback inteface, we would not be
139 * able to control what happens as the packet runs through
140 * ip_input() as it is done through a ISR.
142 LIST_FOREACH(iac
, INADDR_HASH(dst
->sin_addr
.s_addr
), ia_hash
) {
144 * If the addr to forward to is one of ours, we pretend
145 * to be the destination for this packet.
147 if (IA_SIN(iac
->ia
)->sin_addr
.s_addr
== dst
->sin_addr
.s_addr
)
153 if (m
->m_pkthdr
.rcvif
== NULL
)
154 m
->m_pkthdr
.rcvif
= ifunit_netisr("lo0");
155 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
156 m
->m_pkthdr
.csum_flags
|= CSUM_DATA_VALID
|
158 m
->m_pkthdr
.csum_data
= 0xffff;
160 m
->m_pkthdr
.csum_flags
|= CSUM_IP_CHECKED
| CSUM_IP_VALID
;
163 * Make sure that the IP header is in one mbuf,
164 * required by ip_input
166 if (m
->m_len
< hlen
) {
167 m
= m_pullup(m
, hlen
);
169 /* The packet was freed; we are done */
173 ip
= mtod(m
, struct ip
*);
175 ip
->ip_len
= htons(ip
->ip_len
);
176 ip
->ip_off
= htons(ip
->ip_off
);
179 return 1; /* The packet gets forwarded locally */
185 * IP output. The packet in mbuf chain m contains a skeletal IP
186 * header (with len, off, ttl, proto, tos, src, dst).
187 * The mbuf chain containing the packet will be freed.
188 * The mbuf opt, if present, will not be freed.
191 ip_output(struct mbuf
*m0
, struct mbuf
*opt
, struct route
*ro
,
192 int flags
, struct ip_moptions
*imo
, struct inpcb
*inp
)
195 struct ifnet
*ifp
= NULL
; /* keep compiler happy */
197 int hlen
= sizeof(struct ip
);
199 struct sockaddr_in
*dst
= NULL
; /* keep compiler happy */
200 struct in_ifaddr
*ia
= NULL
;
201 int isbroadcast
, sw_csum
;
202 struct in_addr pkt_dst
;
203 struct route iproute
;
206 struct secpolicy
*sp
= NULL
;
207 struct socket
*so
= inp
? inp
->inp_socket
: NULL
;
210 struct secpolicy
*sp
= NULL
;
211 struct tdb_ident
*tdbi
;
212 #endif /* FAST_IPSEC */
213 struct sockaddr_in
*next_hop
= NULL
;
214 int src_was_INADDR_ANY
= 0; /* as the name says... */
221 bzero(ro
, sizeof *ro
);
222 } else if (ro
->ro_rt
!= NULL
&& ro
->ro_rt
->rt_cpuid
!= mycpuid
) {
223 if (flags
& IP_DEBUGROUTE
) {
224 panic("ip_output: rt rt_cpuid %d accessed on cpu %d\n",
225 ro
->ro_rt
->rt_cpuid
, mycpuid
);
230 * If the cached rtentry's owner CPU is not the current CPU,
231 * then don't touch the cached rtentry (remote free is too
232 * expensive in this context); just relocate the route.
235 bzero(ro
, sizeof *ro
);
238 if (m
->m_pkthdr
.fw_flags
& IPFORWARD_MBUF_TAGGED
) {
240 mtag
= m_tag_find(m
, PACKET_TAG_IPFORWARD
, NULL
);
241 KKASSERT(mtag
!= NULL
);
242 next_hop
= m_tag_data(mtag
);
245 if (m
->m_pkthdr
.fw_flags
& DUMMYNET_MBUF_TAGGED
) {
246 struct dn_pkt
*dn_pkt
;
248 /* Extract info from dummynet tag */
249 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
250 KKASSERT(mtag
!= NULL
);
251 dn_pkt
= m_tag_data(mtag
);
254 * The packet was already tagged, so part of the
255 * processing was already done, and we need to go down.
256 * Get the calculated parameters from the tag.
260 KKASSERT(ro
== &iproute
);
261 *ro
= dn_pkt
->ro
; /* structure copy */
262 KKASSERT(ro
->ro_rt
== NULL
|| ro
->ro_rt
->rt_cpuid
== mycpuid
);
264 dst
= dn_pkt
->dn_dst
;
265 if (dst
== (struct sockaddr_in
*)&(dn_pkt
->ro
.ro_dst
)) {
266 /* If 'dst' points into dummynet tag, adjust it */
267 dst
= (struct sockaddr_in
*)&(ro
->ro_dst
);
270 ip
= mtod(m
, struct ip
*);
271 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
273 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
279 m
= ip_insertoptions(m
, opt
, &len
);
283 ip
= mtod(m
, struct ip
*);
288 if (!(flags
& (IP_FORWARDING
|IP_RAWOUTPUT
))) {
289 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, hlen
>> 2);
291 if (ip_do_rfc6864
&& (ip
->ip_off
& IP_DF
))
294 ip
->ip_id
= ip_newid();
295 ipstat
.ips_localout
++;
297 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
301 pkt_dst
= next_hop
? next_hop
->sin_addr
: ip
->ip_dst
;
303 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
305 * If there is a cached route,
306 * check that it is to the same destination
307 * and is still up. If not, free it and try again.
308 * The address family should also be checked in case of sharing the
312 (!(ro
->ro_rt
->rt_flags
& RTF_UP
) ||
313 dst
->sin_family
!= AF_INET
||
314 dst
->sin_addr
.s_addr
!= pkt_dst
.s_addr
)) {
318 if (ro
->ro_rt
== NULL
) {
319 bzero(dst
, sizeof *dst
);
320 dst
->sin_family
= AF_INET
;
321 dst
->sin_len
= sizeof *dst
;
322 dst
->sin_addr
= pkt_dst
;
325 * If routing to interface only,
326 * short circuit routing lookup.
328 if (flags
& IP_ROUTETOIF
) {
329 if ((ia
= ifatoia(ifa_ifwithdstaddr(sintosa(dst
)))) == NULL
&&
330 (ia
= ifatoia(ifa_ifwithnet(sintosa(dst
)))) == NULL
) {
331 ipstat
.ips_noroute
++;
337 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
338 } else if (IN_MULTICAST(ntohl(pkt_dst
.s_addr
)) &&
339 imo
!= NULL
&& imo
->imo_multicast_ifp
!= NULL
) {
341 * Bypass the normal routing lookup for multicast
342 * packets if the interface is specified.
344 ifp
= imo
->imo_multicast_ifp
;
346 isbroadcast
= 0; /* fool gcc */
349 * If this is the case, we probably don't want to allocate
350 * a protocol-cloned route since we didn't get one from the
351 * ULP. This lets TCP do its thing, while not burdening
352 * forwarding or ICMP with the overhead of cloning a route.
353 * Of course, we still want to do any cloning requested by
354 * the link layer, as this is probably required in all cases
355 * for correct operation (as it is for ARP).
357 if (ro
->ro_rt
== NULL
)
358 rtalloc_ign(ro
, RTF_PRCLONING
);
359 if (ro
->ro_rt
== NULL
) {
360 ipstat
.ips_noroute
++;
361 error
= EHOSTUNREACH
;
364 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
365 ifp
= ro
->ro_rt
->rt_ifp
;
367 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
368 dst
= (struct sockaddr_in
*)ro
->ro_rt
->rt_gateway
;
369 if (ro
->ro_rt
->rt_flags
& RTF_HOST
)
370 isbroadcast
= (ro
->ro_rt
->rt_flags
& RTF_BROADCAST
);
372 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
374 if (IN_MULTICAST(ntohl(pkt_dst
.s_addr
))) {
375 m
->m_flags
|= M_MCAST
;
377 * IP destination address is multicast. Make sure "dst"
378 * still points to the address in "ro". (It may have been
379 * changed to point to a gateway address, above.)
381 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
383 * See if the caller provided any multicast options
386 ip
->ip_ttl
= imo
->imo_multicast_ttl
;
387 if (imo
->imo_multicast_vif
!= -1) {
390 ip_mcast_src(imo
->imo_multicast_vif
) :
394 ip
->ip_ttl
= IP_DEFAULT_MULTICAST_TTL
;
397 * Confirm that the outgoing interface supports multicast.
399 if ((imo
== NULL
) || (imo
->imo_multicast_vif
== -1)) {
400 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
401 ipstat
.ips_noroute
++;
407 * If source address not specified yet, use address of the
408 * outgoing interface. In case, keep note we did that, so
409 * if the the firewall changes the next-hop causing the
410 * output interface to change, we can fix that.
412 if (ip
->ip_src
.s_addr
== INADDR_ANY
|| src_was_INADDR_ANY
) {
413 /* Interface may have no addresses. */
415 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
416 src_was_INADDR_ANY
= 1;
420 if (ip
->ip_src
.s_addr
!= INADDR_ANY
) {
421 struct in_multi
*inm
;
423 inm
= IN_LOOKUP_MULTI(&pkt_dst
, ifp
);
425 (imo
== NULL
|| imo
->imo_multicast_loop
)) {
427 * If we belong to the destination multicast
428 * group on the outgoing interface, and the
429 * caller did not forbid loopback, loop back
432 ip_mloopback(ifp
, m
, dst
, hlen
);
435 * If we are acting as a multicast router,
436 * perform multicast forwarding as if the
437 * packet had just arrived on the interface
438 * to which we are about to send. The
439 * multicast forwarding function recursively
440 * calls this function, using the IP_FORWARDING
441 * flag to prevent infinite recursion.
443 * Multicasts that are looped back by
444 * ip_mloopback(), above, will be forwarded by
445 * the ip_input() routine, if necessary.
447 if (ip_mrouter
&& !(flags
& IP_FORWARDING
)) {
449 * If rsvp daemon is not running, do
450 * not set ip_moptions. This ensures
451 * that the packet is multicast and
452 * not just sent down one link as
453 * prescribed by rsvpd.
459 if (ip_mforward(ip
, ifp
,
472 * Multicasts with a time-to-live of zero may be looped-
473 * back, above, but must not be transmitted on a network.
474 * Also, multicasts addressed to the loopback interface
475 * are not sent -- the above call to ip_mloopback() will
476 * loop back a copy if this host actually belongs to the
477 * destination group on the loopback interface.
479 if (ip
->ip_ttl
== 0 || ifp
->if_flags
& IFF_LOOPBACK
) {
486 m
->m_flags
&= ~M_MCAST
;
490 * If the source address is not specified yet, use the address
491 * of the outgoing interface. In case, keep note we did that,
492 * so if the the firewall changes the next-hop causing the output
493 * interface to change, we can fix that.
495 if (ip
->ip_src
.s_addr
== INADDR_ANY
|| src_was_INADDR_ANY
) {
496 /* Interface may have no addresses. */
498 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
499 src_was_INADDR_ANY
= 1;
504 * Look for broadcast address and
505 * verify user is allowed to send
509 if (!(ifp
->if_flags
& IFF_BROADCAST
)) {
510 error
= EADDRNOTAVAIL
;
513 if (!(flags
& IP_ALLOWBROADCAST
)) {
517 /* don't allow broadcast messages to be fragmented */
518 if (ip
->ip_len
> ifp
->if_mtu
) {
522 m
->m_flags
|= M_BCAST
;
524 m
->m_flags
&= ~M_BCAST
;
529 /* get SP for this packet */
531 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, flags
, &error
);
533 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
536 ipsecstat
.out_inval
++;
543 switch (sp
->policy
) {
544 case IPSEC_POLICY_DISCARD
:
546 * This packet is just discarded.
548 ipsecstat
.out_polvio
++;
551 case IPSEC_POLICY_BYPASS
:
552 case IPSEC_POLICY_NONE
:
553 case IPSEC_POLICY_TCP
:
554 /* no need to do IPsec. */
557 case IPSEC_POLICY_IPSEC
:
558 if (sp
->req
== NULL
) {
559 /* acquire a policy */
560 error
= key_spdacquire(sp
);
565 case IPSEC_POLICY_ENTRUST
:
567 kprintf("ip_output: Invalid policy found. %d\n", sp
->policy
);
570 struct ipsec_output_state state
;
571 bzero(&state
, sizeof state
);
573 if (flags
& IP_ROUTETOIF
) {
575 bzero(&iproute
, sizeof iproute
);
578 state
.dst
= (struct sockaddr
*)dst
;
584 * delayed checksums are not currently compatible with IPsec
586 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
588 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
591 ip
->ip_len
= htons(ip
->ip_len
);
592 ip
->ip_off
= htons(ip
->ip_off
);
594 error
= ipsec4_output(&state
, sp
, flags
);
597 if (flags
& IP_ROUTETOIF
) {
599 * if we have tunnel mode SA, we may need to ignore
602 if (state
.ro
!= &iproute
|| state
.ro
->ro_rt
!= NULL
) {
603 flags
&= ~IP_ROUTETOIF
;
608 dst
= (struct sockaddr_in
*)state
.dst
;
610 /* mbuf is already reclaimed in ipsec4_output. */
620 kprintf("ip4_output (ipsec): error code %d\n", error
);
623 /* don't show these error codes to the user */
631 /* be sure to update variables that are affected by ipsec4_output() */
632 ip
= mtod(m
, struct ip
*);
634 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
636 hlen
= ip
->ip_hl
<< 2;
638 if (ro
->ro_rt
== NULL
) {
639 if (!(flags
& IP_ROUTETOIF
)) {
640 kprintf("ip_output: "
641 "can't update route after IPsec processing\n");
642 error
= EHOSTUNREACH
; /*XXX*/
646 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
647 ifp
= ro
->ro_rt
->rt_ifp
;
650 /* make it flipped, again. */
651 ip
->ip_len
= ntohs(ip
->ip_len
);
652 ip
->ip_off
= ntohs(ip
->ip_off
);
657 * Check the security policy (SP) for the packet and, if
658 * required, do IPsec-related processing. There are two
659 * cases here; the first time a packet is sent through
660 * it will be untagged and handled by ipsec4_checkpolicy.
661 * If the packet is resubmitted to ip_output (e.g. after
662 * AH, ESP, etc. processing), there will be a tag to bypass
663 * the lookup and related policy checking.
665 mtag
= m_tag_find(m
, PACKET_TAG_IPSEC_PENDING_TDB
, NULL
);
668 tdbi
= (struct tdb_ident
*)m_tag_data(mtag
);
669 sp
= ipsec_getpolicy(tdbi
, IPSEC_DIR_OUTBOUND
);
671 error
= -EINVAL
; /* force silent drop */
672 m_tag_delete(m
, mtag
);
674 sp
= ipsec4_checkpolicy(m
, IPSEC_DIR_OUTBOUND
, flags
,
678 * There are four return cases:
679 * sp != NULL apply IPsec policy
680 * sp == NULL, error == 0 no IPsec handling needed
681 * sp == NULL, error == -EINVAL discard packet w/o error
682 * sp == NULL, error != 0 discard packet, report error
685 /* Loop detection, check if ipsec processing already done */
686 KASSERT(sp
->req
!= NULL
, ("ip_output: no ipsec request"));
687 for (mtag
= m_tag_first(m
); mtag
!= NULL
;
688 mtag
= m_tag_next(m
, mtag
)) {
689 if (mtag
->m_tag_cookie
!= MTAG_ABI_COMPAT
)
691 if (mtag
->m_tag_id
!= PACKET_TAG_IPSEC_OUT_DONE
&&
692 mtag
->m_tag_id
!= PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED
)
695 * Check if policy has an SA associated with it.
696 * This can happen when an SP has yet to acquire
697 * an SA; e.g. on first reference. If it occurs,
698 * then we let ipsec4_process_packet do its thing.
700 if (sp
->req
->sav
== NULL
)
702 tdbi
= (struct tdb_ident
*)m_tag_data(mtag
);
703 if (tdbi
->spi
== sp
->req
->sav
->spi
&&
704 tdbi
->proto
== sp
->req
->sav
->sah
->saidx
.proto
&&
705 bcmp(&tdbi
->dst
, &sp
->req
->sav
->sah
->saidx
.dst
,
706 sizeof(union sockaddr_union
)) == 0) {
708 * No IPsec processing is needed, free
711 * NB: null pointer to avoid free at
714 KEY_FREESP(&sp
), sp
= NULL
;
721 * Do delayed checksums now because we send before
722 * this is done in the normal processing path.
724 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
726 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
729 ip
->ip_len
= htons(ip
->ip_len
);
730 ip
->ip_off
= htons(ip
->ip_off
);
732 /* NB: callee frees mbuf */
733 error
= ipsec4_process_packet(m
, sp
->req
, flags
, 0);
735 * Preserve KAME behaviour: ENOENT can be returned
736 * when an SA acquire is in progress. Don't propagate
737 * this to user-level; it confuses applications.
739 * XXX this will go away when the SADB is redone.
750 * Hack: -EINVAL is used to signal that a packet
751 * should be silently discarded. This is typically
752 * because we asked key management for an SA and
753 * it was delayed (e.g. kicked up to IKE).
755 if (error
== -EINVAL
)
759 /* No IPsec processing for this packet. */
763 * If deferred crypto processing is needed, check that
764 * the interface supports it.
766 mtag
= m_tag_find(m
, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED
, NULL
);
767 if (mtag
!= NULL
&& !(ifp
->if_capenable
& IFCAP_IPSEC
)) {
768 /* notify IPsec to do its own crypto */
769 ipsp_skipcrypto_unmark((struct tdb_ident
*)m_tag_data(mtag
));
770 error
= EHOSTUNREACH
;
776 #endif /* FAST_IPSEC */
778 /* We are already being fwd'd from a firewall. */
779 if (next_hop
!= NULL
)
783 if (!pfil_has_hooks(&inet_pfil_hook
)) {
784 if (m
->m_pkthdr
.fw_flags
& DUMMYNET_MBUF_TAGGED
) {
786 * Strip dummynet tags from stranded packets
788 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
789 KKASSERT(mtag
!= NULL
);
790 m_tag_delete(m
, mtag
);
791 m
->m_pkthdr
.fw_flags
&= ~DUMMYNET_MBUF_TAGGED
;
798 * - Xlate: translate packet's addr/port (NAT).
799 * - Firewall: deny/allow/etc.
800 * - Wrap: fake packet's addr/port <unimpl.>
801 * - Encapsulate: put it in another IP and send out. <unimp.>
805 * Run through list of hooks for output packets.
807 error
= pfil_run_hooks(&inet_pfil_hook
, &m
, ifp
, PFIL_OUT
);
808 if (error
!= 0 || m
== NULL
)
810 ip
= mtod(m
, struct ip
*);
812 if (m
->m_pkthdr
.fw_flags
& IPFORWARD_MBUF_TAGGED
) {
814 * Check dst to make sure it is directly reachable on the
815 * interface we previously thought it was.
816 * If it isn't (which may be likely in some situations) we have
817 * to re-route it (ie, find a route for the next-hop and the
818 * associated interface) and set them here. This is nested
819 * forwarding which in most cases is undesirable, except where
820 * such control is nigh impossible. So we do it here.
823 mtag
= m_tag_find(m
, PACKET_TAG_IPFORWARD
, NULL
);
824 KKASSERT(mtag
!= NULL
);
825 next_hop
= m_tag_data(mtag
);
828 * Try local forwarding first
830 if (ip_localforward(m
, next_hop
, hlen
))
834 * Relocate the route based on next_hop.
835 * If the current route is inp's cache, keep it untouched.
837 if (ro
== &iproute
&& ro
->ro_rt
!= NULL
) {
842 bzero(ro
, sizeof *ro
);
845 * Forwarding to broadcast address is not allowed.
846 * XXX Should we follow IP_ROUTETOIF?
848 flags
&= ~(IP_ALLOWBROADCAST
| IP_ROUTETOIF
);
850 /* We are doing forwarding now */
851 flags
|= IP_FORWARDING
;
856 if (m
->m_pkthdr
.fw_flags
& DUMMYNET_MBUF_TAGGED
) {
857 struct dn_pkt
*dn_pkt
;
859 mtag
= m_tag_find(m
, PACKET_TAG_DUMMYNET
, NULL
);
860 KKASSERT(mtag
!= NULL
);
861 dn_pkt
= m_tag_data(mtag
);
864 * Under certain cases it is not possible to recalculate
865 * 'ro' and 'dst', let alone 'flags', so just save them in
866 * dummynet tag and avoid the possible wrong reculcalation
867 * when we come back to ip_output() again.
869 * All other parameters have been already used and so they
870 * are not needed anymore.
871 * XXX if the ifp is deleted while a pkt is in dummynet,
872 * we are in trouble! (TODO use ifnet_detach_event)
874 * We need to copy *ro because for ICMP pkts (and maybe
875 * others) the caller passed a pointer into the stack;
876 * dst might also be a pointer into *ro so it needs to
881 ro
->ro_rt
->rt_refcnt
++;
882 if (dst
== (struct sockaddr_in
*)&ro
->ro_dst
) {
883 /* 'dst' points into 'ro' */
884 dst
= (struct sockaddr_in
*)&(dn_pkt
->ro
.ro_dst
);
886 dn_pkt
->dn_dst
= dst
;
887 dn_pkt
->flags
= flags
;
893 /* 127/8 must not appear on wire - RFC1122. */
894 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
895 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
896 if (!(ifp
->if_flags
& IFF_LOOPBACK
)) {
897 ipstat
.ips_badaddr
++;
898 error
= EADDRNOTAVAIL
;
902 if (ip
->ip_src
.s_addr
== INADDR_ANY
||
903 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
))) {
904 ipstat
.ips_badaddr
++;
905 error
= EADDRNOTAVAIL
;
909 if ((m
->m_pkthdr
.csum_flags
& CSUM_TSO
) == 0) {
910 m
->m_pkthdr
.csum_flags
|= CSUM_IP
;
911 sw_csum
= m
->m_pkthdr
.csum_flags
& ~ifp
->if_hwassist
;
912 if (sw_csum
& CSUM_DELAY_DATA
) {
914 sw_csum
&= ~CSUM_DELAY_DATA
;
916 m
->m_pkthdr
.csum_flags
&= ifp
->if_hwassist
;
920 m
->m_pkthdr
.csum_iphlen
= hlen
;
923 * If small enough for interface, or the interface will take
924 * care of the fragmentation or segmentation for us, can just
927 if (ip
->ip_len
<= ifp
->if_mtu
||
928 ((ifp
->if_hwassist
& CSUM_FRAGMENT
) && !(ip
->ip_off
& IP_DF
)) ||
929 (m
->m_pkthdr
.csum_flags
& CSUM_TSO
)) {
930 ip
->ip_len
= htons(ip
->ip_len
);
931 ip
->ip_off
= htons(ip
->ip_off
);
933 if (sw_csum
& CSUM_DELAY_IP
) {
934 if (ip
->ip_vhl
== IP_VHL_BORING
)
935 ip
->ip_sum
= in_cksum_hdr(ip
);
937 ip
->ip_sum
= in_cksum(m
, hlen
);
940 /* Record statistics for this interface address. */
941 if (!(flags
& IP_FORWARDING
) && ia
) {
942 IFA_STAT_INC(&ia
->ia_ifa
, opackets
, 1);
943 IFA_STAT_INC(&ia
->ia_ifa
, obytes
, m
->m_pkthdr
.len
);
947 /* clean ipsec history once it goes out of the node */
951 #ifdef MBUF_STRESS_TEST
952 if (mbuf_frag_size
&& m
->m_pkthdr
.len
> mbuf_frag_size
) {
953 struct mbuf
*m1
, *m2
;
956 tmp
= length
= m
->m_pkthdr
.len
;
958 while ((length
-= mbuf_frag_size
) >= 1) {
959 m1
= m_split(m
, length
, M_NOWAIT
);
963 while (m2
->m_next
!= NULL
)
967 m
->m_pkthdr
.len
= tmp
;
972 if (!mpls_output_process(m
, ro
->ro_rt
))
975 error
= ifp
->if_output(ifp
, m
, (struct sockaddr
*)dst
,
980 if (ip
->ip_off
& IP_DF
) {
983 * This case can happen if the user changed the MTU
984 * of an interface after enabling IP on it. Because
985 * most netifs don't keep track of routes pointing to
986 * them, there is no way for one to update all its
987 * routes when the MTU is changed.
989 if ((ro
->ro_rt
->rt_flags
& (RTF_UP
| RTF_HOST
)) &&
990 !(ro
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
) &&
991 (ro
->ro_rt
->rt_rmx
.rmx_mtu
> ifp
->if_mtu
)) {
992 ro
->ro_rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
994 ipstat
.ips_cantfrag
++;
999 * Too large for interface; fragment if possible. If successful,
1000 * on return, m will point to a list of packets to be sent.
1002 error
= ip_fragment(ip
, &m
, ifp
->if_mtu
, ifp
->if_hwassist
, sw_csum
);
1007 m
->m_nextpkt
= NULL
;
1009 /* clean ipsec history once it goes out of the node */
1013 /* Record statistics for this interface address. */
1015 IFA_STAT_INC(&ia
->ia_ifa
, opackets
, 1);
1016 IFA_STAT_INC(&ia
->ia_ifa
, obytes
,
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
, M_NOWAIT
, 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 m
->m_pkthdr
.csum_iphlen
= mhlen
;
1189 mhip
->ip_off
= htons(mhip
->ip_off
);
1191 if (sw_csum
& CSUM_DELAY_IP
)
1192 mhip
->ip_sum
= in_cksum(m
, mhlen
);
1194 mnext
= &m
->m_nextpkt
;
1196 ipstat
.ips_ofragments
+= nfrags
;
1198 /* set first marker for fragment chain */
1199 m0
->m_flags
|= M_FIRSTFRAG
| M_FRAG
;
1200 m0
->m_pkthdr
.csum_data
= nfrags
;
1203 * Update first fragment by trimming what's been copied out
1204 * and updating header.
1206 m_adj(m0
, hlen
+ firstlen
- ip
->ip_len
);
1207 m0
->m_pkthdr
.len
= hlen
+ firstlen
;
1208 ip
->ip_len
= htons((u_short
)m0
->m_pkthdr
.len
);
1209 ip
->ip_off
|= IP_MF
;
1210 ip
->ip_off
= htons(ip
->ip_off
);
1212 if (sw_csum
& CSUM_DELAY_IP
)
1213 ip
->ip_sum
= in_cksum(m0
, hlen
);
1221 in_delayed_cksum(struct mbuf
*m
)
1224 u_short csum
, offset
;
1226 ip
= mtod(m
, struct ip
*);
1227 offset
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
1228 csum
= in_cksum_skip(m
, ip
->ip_len
, offset
);
1229 if (m
->m_pkthdr
.csum_flags
& CSUM_UDP
&& csum
== 0)
1231 offset
+= m
->m_pkthdr
.csum_data
; /* checksum offset */
1233 if (offset
+ sizeof(u_short
) > m
->m_len
) {
1234 kprintf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1235 m
->m_len
, offset
, ip
->ip_p
);
1238 * this shouldn't happen, but if it does, the
1239 * correct behavior may be to insert the checksum
1240 * in the existing chain instead of rearranging it.
1242 m
= m_pullup(m
, offset
+ sizeof(u_short
));
1244 *(u_short
*)(m
->m_data
+ offset
) = csum
;
1248 * Insert IP options into preformed packet.
1249 * Adjust IP destination as required for IP source routing,
1250 * as indicated by a non-zero in_addr at the start of the options.
1252 * XXX This routine assumes that the packet has no options in place.
1254 static struct mbuf
*
1255 ip_insertoptions(struct mbuf
*m
, struct mbuf
*opt
, int *phlen
)
1257 struct ipoption
*p
= mtod(opt
, struct ipoption
*);
1259 struct ip
*ip
= mtod(m
, struct ip
*);
1262 optlen
= opt
->m_len
- sizeof p
->ipopt_dst
;
1263 if (optlen
+ (u_short
)ip
->ip_len
> IP_MAXPACKET
) {
1265 return (m
); /* XXX should fail */
1267 if (p
->ipopt_dst
.s_addr
)
1268 ip
->ip_dst
= p
->ipopt_dst
;
1269 if (m
->m_flags
& M_EXT
|| m
->m_data
- optlen
< m
->m_pktdat
) {
1270 MGETHDR(n
, M_NOWAIT
, MT_HEADER
);
1275 n
->m_pkthdr
.rcvif
= NULL
;
1276 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
1277 m
->m_len
-= sizeof(struct ip
);
1278 m
->m_data
+= sizeof(struct ip
);
1281 m
->m_len
= optlen
+ sizeof(struct ip
);
1282 m
->m_data
+= max_linkhdr
;
1283 memcpy(mtod(m
, void *), ip
, sizeof(struct ip
));
1285 m
->m_data
-= optlen
;
1287 m
->m_pkthdr
.len
+= optlen
;
1288 bcopy(ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
1290 ip
= mtod(m
, struct ip
*);
1291 bcopy(p
->ipopt_list
, ip
+ 1, optlen
);
1292 *phlen
= sizeof(struct ip
) + optlen
;
1293 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, *phlen
>> 2);
1294 ip
->ip_len
+= optlen
;
1299 * Copy options from ip to jp,
1300 * omitting those not copied during fragmentation.
1303 ip_optcopy(struct ip
*ip
, struct ip
*jp
)
1306 int opt
, optlen
, cnt
;
1308 cp
= (u_char
*)(ip
+ 1);
1309 dp
= (u_char
*)(jp
+ 1);
1310 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof(struct ip
);
1311 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1313 if (opt
== IPOPT_EOL
)
1315 if (opt
== IPOPT_NOP
) {
1316 /* Preserve for IP mcast tunnel's LSRR alignment. */
1322 KASSERT(cnt
>= IPOPT_OLEN
+ sizeof *cp
,
1323 ("ip_optcopy: malformed ipv4 option"));
1324 optlen
= cp
[IPOPT_OLEN
];
1325 KASSERT(optlen
>= IPOPT_OLEN
+ sizeof *cp
&& optlen
<= cnt
,
1326 ("ip_optcopy: malformed ipv4 option"));
1328 /* bogus lengths should have been caught by ip_dooptions */
1331 if (IPOPT_COPIED(opt
)) {
1332 bcopy(cp
, dp
, optlen
);
1336 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
1342 * IP socket option processing.
1345 ip_ctloutput(netmsg_t msg
)
1347 struct socket
*so
= msg
->base
.nm_so
;
1348 struct sockopt
*sopt
= msg
->ctloutput
.nm_sopt
;
1349 struct inpcb
*inp
= so
->so_pcb
;
1354 /* Get socket's owner cpuid hint */
1355 if (sopt
->sopt_level
== SOL_SOCKET
&&
1356 sopt
->sopt_dir
== SOPT_GET
&&
1357 sopt
->sopt_name
== SO_CPUHINT
) {
1359 soopt_from_kbuf(sopt
, &optval
, sizeof(optval
));
1363 if (sopt
->sopt_level
!= IPPROTO_IP
) {
1368 switch (sopt
->sopt_name
) {
1369 case IP_MULTICAST_IF
:
1370 case IP_MULTICAST_VIF
:
1371 case IP_MULTICAST_TTL
:
1372 case IP_MULTICAST_LOOP
:
1373 case IP_ADD_MEMBERSHIP
:
1374 case IP_DROP_MEMBERSHIP
:
1376 * Handle multicast options in netisr0
1378 if (&curthread
->td_msgport
!= netisr_cpuport(0)) {
1379 /* NOTE: so_port MUST NOT be checked in netisr0 */
1380 msg
->lmsg
.ms_flags
|= MSGF_IGNSOPORT
;
1381 lwkt_forwardmsg(netisr_cpuport(0), &msg
->lmsg
);
1387 switch (sopt
->sopt_dir
) {
1389 switch (sopt
->sopt_name
) {
1396 if (sopt
->sopt_valsize
> MLEN
) {
1400 MGET(m
, sopt
->sopt_td
? M_WAITOK
: M_NOWAIT
, MT_HEADER
);
1405 m
->m_len
= sopt
->sopt_valsize
;
1406 error
= soopt_to_kbuf(sopt
, mtod(m
, void *), m
->m_len
,
1408 error
= ip_pcbopts(sopt
->sopt_name
,
1409 &inp
->inp_options
, m
);
1417 case IP_RECVRETOPTS
:
1418 case IP_RECVDSTADDR
:
1422 error
= soopt_to_kbuf(sopt
, &optval
, sizeof optval
,
1426 switch (sopt
->sopt_name
) {
1428 inp
->inp_ip_tos
= optval
;
1432 inp
->inp_ip_ttl
= optval
;
1435 if (optval
>= 0 && optval
<= MAXTTL
)
1436 inp
->inp_ip_minttl
= optval
;
1440 #define OPTSET(bit) \
1442 inp->inp_flags |= bit; \
1444 inp->inp_flags &= ~bit;
1447 OPTSET(INP_RECVOPTS
);
1450 case IP_RECVRETOPTS
:
1451 OPTSET(INP_RECVRETOPTS
);
1454 case IP_RECVDSTADDR
:
1455 OPTSET(INP_RECVDSTADDR
);
1463 OPTSET(INP_RECVTTL
);
1473 case IP_MULTICAST_IF
:
1474 case IP_MULTICAST_VIF
:
1475 case IP_MULTICAST_TTL
:
1476 case IP_MULTICAST_LOOP
:
1477 case IP_ADD_MEMBERSHIP
:
1478 case IP_DROP_MEMBERSHIP
:
1479 error
= ip_setmoptions(sopt
, &inp
->inp_moptions
);
1483 error
= soopt_to_kbuf(sopt
, &optval
, sizeof optval
,
1489 case IP_PORTRANGE_DEFAULT
:
1490 inp
->inp_flags
&= ~(INP_LOWPORT
);
1491 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1494 case IP_PORTRANGE_HIGH
:
1495 inp
->inp_flags
&= ~(INP_LOWPORT
);
1496 inp
->inp_flags
|= INP_HIGHPORT
;
1499 case IP_PORTRANGE_LOW
:
1500 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1501 inp
->inp_flags
|= INP_LOWPORT
;
1510 #if defined(IPSEC) || defined(FAST_IPSEC)
1511 case IP_IPSEC_POLICY
:
1519 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
1521 soopt_to_mbuf(sopt
, m
);
1522 priv
= (sopt
->sopt_td
!= NULL
&&
1523 priv_check(sopt
->sopt_td
, PRIV_ROOT
) != 0) ? 0 : 1;
1524 req
= mtod(m
, caddr_t
);
1526 optname
= sopt
->sopt_name
;
1527 error
= ipsec4_set_policy(inp
, optname
, req
, len
, priv
);
1534 error
= ENOPROTOOPT
;
1540 switch (sopt
->sopt_name
) {
1543 if (inp
->inp_options
)
1544 soopt_from_kbuf(sopt
, mtod(inp
->inp_options
,
1546 inp
->inp_options
->m_len
);
1548 sopt
->sopt_valsize
= 0;
1555 case IP_RECVRETOPTS
:
1556 case IP_RECVDSTADDR
:
1561 switch (sopt
->sopt_name
) {
1564 optval
= inp
->inp_ip_tos
;
1568 optval
= inp
->inp_ip_ttl
;
1571 optval
= inp
->inp_ip_minttl
;
1574 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1577 optval
= OPTBIT(INP_RECVOPTS
);
1580 case IP_RECVRETOPTS
:
1581 optval
= OPTBIT(INP_RECVRETOPTS
);
1584 case IP_RECVDSTADDR
:
1585 optval
= OPTBIT(INP_RECVDSTADDR
);
1589 optval
= OPTBIT(INP_RECVTTL
);
1593 optval
= OPTBIT(INP_RECVIF
);
1597 if (inp
->inp_flags
& INP_HIGHPORT
)
1598 optval
= IP_PORTRANGE_HIGH
;
1599 else if (inp
->inp_flags
& INP_LOWPORT
)
1600 optval
= IP_PORTRANGE_LOW
;
1606 optval
= OPTBIT(INP_FAITH
);
1609 soopt_from_kbuf(sopt
, &optval
, sizeof optval
);
1612 case IP_MULTICAST_IF
:
1613 case IP_MULTICAST_VIF
:
1614 case IP_MULTICAST_TTL
:
1615 case IP_MULTICAST_LOOP
:
1616 case IP_ADD_MEMBERSHIP
:
1617 case IP_DROP_MEMBERSHIP
:
1618 error
= ip_getmoptions(sopt
, inp
->inp_moptions
);
1621 #if defined(IPSEC) || defined(FAST_IPSEC)
1622 case IP_IPSEC_POLICY
:
1624 struct mbuf
*m
= NULL
;
1629 req
= mtod(m
, caddr_t
);
1632 error
= ipsec4_get_policy(so
->so_pcb
, req
, len
, &m
);
1634 error
= soopt_from_mbuf(sopt
, m
); /* XXX */
1642 error
= ENOPROTOOPT
;
1648 lwkt_replymsg(&msg
->lmsg
, error
);
1652 * Set up IP options in pcb for insertion in output packets.
1653 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1654 * with destination address if source routed.
1657 ip_pcbopts(int optname
, struct mbuf
**pcbopt
, struct mbuf
*m
)
1663 /* turn off any old options */
1667 if (m
== NULL
|| m
->m_len
== 0) {
1669 * Only turning off any previous options.
1676 if (m
->m_len
% sizeof(int32_t))
1679 * IP first-hop destination address will be stored before
1680 * actual options; move other options back
1681 * and clear it when none present.
1683 if (m
->m_data
+ m
->m_len
+ sizeof(struct in_addr
) >= &m
->m_dat
[MLEN
])
1686 m
->m_len
+= sizeof(struct in_addr
);
1687 cp
= mtod(m
, u_char
*) + sizeof(struct in_addr
);
1688 bcopy(mtod(m
, caddr_t
), cp
, cnt
);
1689 bzero(mtod(m
, caddr_t
), sizeof(struct in_addr
));
1691 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1692 opt
= cp
[IPOPT_OPTVAL
];
1693 if (opt
== IPOPT_EOL
)
1695 if (opt
== IPOPT_NOP
)
1698 if (cnt
< IPOPT_OLEN
+ sizeof *cp
)
1700 optlen
= cp
[IPOPT_OLEN
];
1701 if (optlen
< IPOPT_OLEN
+ sizeof *cp
|| optlen
> cnt
)
1712 * user process specifies route as:
1714 * D must be our final destination (but we can't
1715 * check that since we may not have connected yet).
1716 * A is first hop destination, which doesn't appear in
1717 * actual IP option, but is stored before the options.
1719 if (optlen
< IPOPT_MINOFF
- 1 + sizeof(struct in_addr
))
1721 m
->m_len
-= sizeof(struct in_addr
);
1722 cnt
-= sizeof(struct in_addr
);
1723 optlen
-= sizeof(struct in_addr
);
1724 cp
[IPOPT_OLEN
] = optlen
;
1726 * Move first hop before start of options.
1728 bcopy(&cp
[IPOPT_OFFSET
+1], mtod(m
, caddr_t
),
1729 sizeof(struct in_addr
));
1731 * Then copy rest of options back
1732 * to close up the deleted entry.
1734 bcopy(&cp
[IPOPT_OFFSET
+1] + sizeof(struct in_addr
),
1735 &cp
[IPOPT_OFFSET
+1],
1736 cnt
- (IPOPT_MINOFF
- 1));
1740 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
1752 * The whole multicast option thing needs to be re-thought.
1753 * Several of these options are equally applicable to non-multicast
1754 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1755 * standard option (IP_TTL).
1759 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1761 static struct ifnet
*
1762 ip_multicast_if(struct in_addr
*a
, int *ifindexp
)
1769 if (ntohl(a
->s_addr
) >> 24 == 0) {
1770 ifindex
= ntohl(a
->s_addr
) & 0xffffff;
1771 if (ifindex
< 0 || if_index
< ifindex
)
1773 ifp
= ifindex2ifnet
[ifindex
];
1775 *ifindexp
= ifindex
;
1777 ifp
= INADDR_TO_IFP(a
);
1783 * Set the IP multicast options in response to user setsockopt().
1786 ip_setmoptions(struct sockopt
*sopt
, struct ip_moptions
**imop
)
1790 struct in_addr addr
;
1791 struct ip_mreq mreq
;
1793 struct ip_moptions
*imo
= *imop
;
1798 * No multicast option buffer attached to the pcb;
1799 * allocate one and initialize to default values.
1801 imo
= kmalloc(sizeof *imo
, M_IPMOPTS
, M_WAITOK
);
1803 imo
->imo_multicast_ifp
= NULL
;
1804 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
1805 imo
->imo_multicast_vif
= -1;
1806 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
1807 imo
->imo_multicast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
1808 imo
->imo_num_memberships
= 0;
1809 /* Assign imo to imop after all fields are setup */
1813 switch (sopt
->sopt_name
) {
1814 /* store an index number for the vif you wanna use in the send */
1815 case IP_MULTICAST_VIF
:
1816 if (legal_vif_num
== 0) {
1820 error
= soopt_to_kbuf(sopt
, &i
, sizeof i
, sizeof i
);
1823 if (!legal_vif_num(i
) && (i
!= -1)) {
1827 imo
->imo_multicast_vif
= i
;
1830 case IP_MULTICAST_IF
:
1832 * Select the interface for outgoing multicast packets.
1834 error
= soopt_to_kbuf(sopt
, &addr
, sizeof addr
, sizeof addr
);
1839 * INADDR_ANY is used to remove a previous selection.
1840 * When no interface is selected, a default one is
1841 * chosen every time a multicast packet is sent.
1843 if (addr
.s_addr
== INADDR_ANY
) {
1844 imo
->imo_multicast_ifp
= NULL
;
1848 * The selected interface is identified by its local
1849 * IP address. Find the interface and confirm that
1850 * it supports multicasting.
1853 ifp
= ip_multicast_if(&addr
, &ifindex
);
1854 if (ifp
== NULL
|| !(ifp
->if_flags
& IFF_MULTICAST
)) {
1856 error
= EADDRNOTAVAIL
;
1859 imo
->imo_multicast_ifp
= ifp
;
1861 imo
->imo_multicast_addr
= addr
;
1863 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
1867 case IP_MULTICAST_TTL
:
1869 * Set the IP time-to-live for outgoing multicast packets.
1870 * The original multicast API required a char argument,
1871 * which is inconsistent with the rest of the socket API.
1872 * We allow either a char or an int.
1874 if (sopt
->sopt_valsize
== 1) {
1876 error
= soopt_to_kbuf(sopt
, &ttl
, 1, 1);
1879 imo
->imo_multicast_ttl
= ttl
;
1882 error
= soopt_to_kbuf(sopt
, &ttl
, sizeof ttl
, sizeof ttl
);
1888 imo
->imo_multicast_ttl
= ttl
;
1892 case IP_MULTICAST_LOOP
:
1894 * Set the loopback flag for outgoing multicast packets.
1895 * Must be zero or one. The original multicast API required a
1896 * char argument, which is inconsistent with the rest
1897 * of the socket API. We allow either a char or an int.
1899 if (sopt
->sopt_valsize
== 1) {
1902 error
= soopt_to_kbuf(sopt
, &loop
, 1, 1);
1905 imo
->imo_multicast_loop
= !!loop
;
1909 error
= soopt_to_kbuf(sopt
, &loop
, sizeof loop
,
1913 imo
->imo_multicast_loop
= !!loop
;
1917 case IP_ADD_MEMBERSHIP
:
1919 * Add a multicast group membership.
1920 * Group must be a valid IP multicast address.
1922 error
= soopt_to_kbuf(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
1926 if (!IN_MULTICAST(ntohl(mreq
.imr_multiaddr
.s_addr
))) {
1932 * If no interface address was provided, use the interface of
1933 * the route to the given multicast address.
1935 if (mreq
.imr_interface
.s_addr
== INADDR_ANY
) {
1936 struct sockaddr_in dst
;
1939 bzero(&dst
, sizeof(struct sockaddr_in
));
1940 dst
.sin_len
= sizeof(struct sockaddr_in
);
1941 dst
.sin_family
= AF_INET
;
1942 dst
.sin_addr
= mreq
.imr_multiaddr
;
1943 rt
= rtlookup((struct sockaddr
*)&dst
);
1945 error
= EADDRNOTAVAIL
;
1952 ifp
= ip_multicast_if(&mreq
.imr_interface
, NULL
);
1956 * See if we found an interface, and confirm that it
1957 * supports multicast.
1959 if (ifp
== NULL
|| !(ifp
->if_flags
& IFF_MULTICAST
)) {
1960 error
= EADDRNOTAVAIL
;
1965 * See if the membership already exists or if all the
1966 * membership slots are full.
1968 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
1969 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
&&
1970 imo
->imo_membership
[i
]->inm_addr
.s_addr
1971 == mreq
.imr_multiaddr
.s_addr
)
1974 if (i
< imo
->imo_num_memberships
) {
1979 if (i
== IP_MAX_MEMBERSHIPS
) {
1980 error
= ETOOMANYREFS
;
1985 * Everything looks good; add a new record to the multicast
1986 * address list for the given interface.
1988 if ((imo
->imo_membership
[i
] =
1989 in_addmulti(&mreq
.imr_multiaddr
, ifp
)) == NULL
) {
1994 ++imo
->imo_num_memberships
;
1998 case IP_DROP_MEMBERSHIP
:
2000 * Drop a multicast group membership.
2001 * Group must be a valid IP multicast address.
2003 error
= soopt_to_kbuf(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
2007 if (!IN_MULTICAST(ntohl(mreq
.imr_multiaddr
.s_addr
))) {
2014 * If an interface address was specified, get a pointer
2015 * to its ifnet structure.
2017 if (mreq
.imr_interface
.s_addr
== INADDR_ANY
)
2020 ifp
= ip_multicast_if(&mreq
.imr_interface
, NULL
);
2022 error
= EADDRNOTAVAIL
;
2028 * Find the membership in the membership array.
2030 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
2032 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
2033 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
2034 mreq
.imr_multiaddr
.s_addr
)
2037 if (i
== imo
->imo_num_memberships
) {
2038 error
= EADDRNOTAVAIL
;
2043 * Give up the multicast address record to which the
2044 * membership points.
2046 in_delmulti(imo
->imo_membership
[i
]);
2048 * Remove the gap in the membership array.
2050 for (++i
; i
< imo
->imo_num_memberships
; ++i
)
2051 imo
->imo_membership
[i
-1] = imo
->imo_membership
[i
];
2052 --imo
->imo_num_memberships
;
2065 * Return the IP multicast options in response to user getsockopt().
2068 ip_getmoptions(struct sockopt
*sopt
, struct ip_moptions
*imo
)
2070 struct in_addr addr
;
2071 struct in_ifaddr
*ia
;
2076 switch (sopt
->sopt_name
) {
2077 case IP_MULTICAST_VIF
:
2079 optval
= imo
->imo_multicast_vif
;
2082 soopt_from_kbuf(sopt
, &optval
, sizeof optval
);
2085 case IP_MULTICAST_IF
:
2086 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
)
2087 addr
.s_addr
= INADDR_ANY
;
2088 else if (imo
->imo_multicast_addr
.s_addr
) {
2089 /* return the value user has set */
2090 addr
= imo
->imo_multicast_addr
;
2092 ia
= IFP_TO_IA(imo
->imo_multicast_ifp
);
2093 addr
.s_addr
= (ia
== NULL
) ? INADDR_ANY
2094 : IA_SIN(ia
)->sin_addr
.s_addr
;
2096 soopt_from_kbuf(sopt
, &addr
, sizeof addr
);
2099 case IP_MULTICAST_TTL
:
2101 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
2103 optval
= coptval
= imo
->imo_multicast_ttl
;
2104 if (sopt
->sopt_valsize
== 1)
2105 soopt_from_kbuf(sopt
, &coptval
, 1);
2107 soopt_from_kbuf(sopt
, &optval
, sizeof optval
);
2110 case IP_MULTICAST_LOOP
:
2112 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
2114 optval
= coptval
= imo
->imo_multicast_loop
;
2115 if (sopt
->sopt_valsize
== 1)
2116 soopt_from_kbuf(sopt
, &coptval
, 1);
2118 soopt_from_kbuf(sopt
, &optval
, sizeof optval
);
2122 error
= ENOPROTOOPT
;
2129 * Discard the IP multicast options.
2132 ip_freemoptions(struct ip_moptions
*imo
)
2137 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
)
2138 in_delmulti(imo
->imo_membership
[i
]);
2139 kfree(imo
, M_IPMOPTS
);
2144 * Routine called from ip_output() to loop back a copy of an IP multicast
2145 * packet to the input queue of a specified interface. Note that this
2146 * calls the output routine of the loopback "driver", but with an interface
2147 * pointer that might NOT be a loopback interface -- evil, but easier than
2148 * replicating that code here.
2151 ip_mloopback(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr_in
*dst
,
2157 copym
= m_copypacket(m
, M_NOWAIT
);
2158 if (copym
!= NULL
&& (copym
->m_flags
& M_EXT
|| copym
->m_len
< hlen
))
2159 copym
= m_pullup(copym
, hlen
);
2160 if (copym
!= NULL
) {
2162 * if the checksum hasn't been computed, mark it as valid
2164 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
2165 in_delayed_cksum(copym
);
2166 copym
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
2167 copym
->m_pkthdr
.csum_flags
|=
2168 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
2169 copym
->m_pkthdr
.csum_data
= 0xffff;
2172 * We don't bother to fragment if the IP length is greater
2173 * than the interface's MTU. Can this possibly matter?
2175 ip
= mtod(copym
, struct ip
*);
2176 ip
->ip_len
= htons(ip
->ip_len
);
2177 ip
->ip_off
= htons(ip
->ip_off
);
2179 if (ip
->ip_vhl
== IP_VHL_BORING
) {
2180 ip
->ip_sum
= in_cksum_hdr(ip
);
2182 ip
->ip_sum
= in_cksum(copym
, hlen
);
2186 * It's not clear whether there are any lingering
2187 * reentrancy problems in other areas which might
2188 * be exposed by using ip_input directly (in
2189 * particular, everything which modifies the packet
2190 * in-place). Yet another option is using the
2191 * protosw directly to deliver the looped back
2192 * packet. For the moment, we'll err on the side
2193 * of safety by using if_simloop().
2196 if (dst
->sin_family
!= AF_INET
) {
2197 kprintf("ip_mloopback: bad address family %d\n",
2199 dst
->sin_family
= AF_INET
;
2202 if_simloop(ifp
, copym
, dst
->sin_family
, 0);