#include "opt_bootp.h"
#include "opt_inet.h"
#include "opt_ipstealth.h"
#include "opt_ipsec.h"
#include "opt_rss.h"
#include "opt_sctp.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/hhook.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/rmlock.h>
#include <sys/rwlock.h>
#include <sys/sdt.h>
#include <sys/syslog.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/if_var.h>
#include <net/if_dl.h>
#include <net/if_private.h>
#include <net/pfil.h>
#include <net/route.h>
#include <net/route/nhop.h>
#include <net/netisr.h>
#include <net/rss_config.h>
#include <net/vnet.h>
#include <netinet/in.h>
#include <netinet/in_kdtrace.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#include <netinet/in_fib.h>
#include <netinet/in_pcb.h>
#include <netinet/ip_var.h>
#include <netinet/ip_encap.h>
#include <netinet/ip_fw.h>
#include <netinet/ip_icmp.h>
#include <netinet/igmp_var.h>
#include <netinet/ip_options.h>
#include <machine/in_cksum.h>
#include <netinet/ip_carp.h>
#include <netinet/in_rss.h>
#include <netinet/ip_mroute.h>
#ifdef SCTP
#include <netinet/sctp_var.h>
#endif
#include <netipsec/ipsec_support.h>
#include <sys/socketvar.h>
#include <security/mac/mac_framework.h>
#ifdef CTASSERT
CTASSERT(sizeof(struct ip) == 20);
#endif
extern void ipreass_init(void);
extern void ipreass_vnet_init(void);
#ifdef VIMAGE
extern void ipreass_destroy(void);
#endif
VNET_DEFINE(int, rsvp_on);
VNET_DEFINE(int, ipforwarding);
SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(ipforwarding), 0,
"Enable IP forwarding between interfaces");
VNET_DEFINE(int, ipsendredirects) = 1;
SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(ipsendredirects), 0,
"Enable sending IP redirects");
VNET_DEFINE_STATIC(bool, ip_strong_es) = false;
#define V_ip_strong_es VNET(ip_strong_es)
SYSCTL_BOOL(_net_inet_ip, OID_AUTO, rfc1122_strong_es,
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_strong_es), false,
"Packet's IP destination address must match address on arrival interface");
VNET_DEFINE_STATIC(bool, ip_sav) = true;
#define V_ip_sav VNET(ip_sav)
SYSCTL_BOOL(_net_inet_ip, OID_AUTO, source_address_validation,
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_sav), true,
"Drop incoming packets with source address that is a local address");
VNET_DEFINE(pfil_head_t, inet_pfil_head);
VNET_DEFINE(pfil_head_t, inet_local_pfil_head);
static struct netisr_handler ip_nh = {
.nh_name = "ip",
.nh_handler = ip_input,
.nh_proto = NETISR_IP,
#ifdef RSS
.nh_m2cpuid = rss_soft_m2cpuid_v4,
.nh_policy = NETISR_POLICY_CPU,
.nh_dispatch = NETISR_DISPATCH_HYBRID,
#else
.nh_policy = NETISR_POLICY_FLOW,
#endif
};
#ifdef RSS
static struct netisr_handler ip_direct_nh = {
.nh_name = "ip_direct",
.nh_handler = ip_direct_input,
.nh_proto = NETISR_IP_DIRECT,
.nh_m2cpuid = rss_soft_m2cpuid_v4,
.nh_policy = NETISR_POLICY_CPU,
.nh_dispatch = NETISR_DISPATCH_HYBRID,
};
#endif
ipproto_input_t *ip_protox[IPPROTO_MAX] = {
[0 ... IPPROTO_MAX - 1] = rip_input };
ipproto_ctlinput_t *ip_ctlprotox[IPPROTO_MAX] = {
[0 ... IPPROTO_MAX - 1] = rip_ctlinput };
VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead);
VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl);
VNET_DEFINE(u_long, in_ifaddrhmask);
CTASSERT(sizeof(struct in_ifaddrhashhead) == sizeof(LIST_HEAD(, in_addr)));
#ifdef IPCTL_DEFMTU
SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
&ip_mtu, 0, "Default MTU");
#endif
#ifdef IPSTEALTH
VNET_DEFINE(int, ipstealth);
SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(ipstealth), 0,
"IP stealth mode, no TTL decrementation on forwarding");
#endif
VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat);
VNET_PCPUSTAT_SYSINIT(ipstat);
SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat,
"IP statistics (struct ipstat, netinet/ip_var.h)");
#ifdef VIMAGE
VNET_PCPUSTAT_SYSUNINIT(ipstat);
#endif
void
kmod_ipstat_inc(int statnum)
{
counter_u64_add(VNET(ipstat)[statnum], 1);
}
void
kmod_ipstat_dec(int statnum)
{
counter_u64_add(VNET(ipstat)[statnum], -1);
}
static int
sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
{
int error, qlimit;
netisr_getqlimit(&ip_nh, &qlimit);
error = sysctl_handle_int(oidp, &qlimit, 0, req);
if (error || !req->newptr)
return (error);
if (qlimit < 1)
return (EINVAL);
return (netisr_setqlimit(&ip_nh, qlimit));
}
SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen,
CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
sysctl_netinet_intr_queue_maxlen, "I",
"Maximum size of the IP input queue");
static int
sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
{
u_int64_t qdrops_long;
int error, qdrops;
netisr_getqdrops(&ip_nh, &qdrops_long);
qdrops = qdrops_long;
error = sysctl_handle_int(oidp, &qdrops, 0, req);
if (error || !req->newptr)
return (error);
if (qdrops != 0)
return (EINVAL);
netisr_clearqdrops(&ip_nh);
return (0);
}
SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops,
CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
0, 0, sysctl_netinet_intr_queue_drops, "I",
"Number of packets dropped from the IP input queue");
#ifdef RSS
static int
sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
{
int error, qlimit;
netisr_getqlimit(&ip_direct_nh, &qlimit);
error = sysctl_handle_int(oidp, &qlimit, 0, req);
if (error || !req->newptr)
return (error);
if (qlimit < 1)
return (EINVAL);
return (netisr_setqlimit(&ip_direct_nh, qlimit));
}
SYSCTL_PROC(_net_inet_ip, IPCTL_INTRDQMAXLEN, intr_direct_queue_maxlen,
CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
0, 0, sysctl_netinet_intr_direct_queue_maxlen,
"I", "Maximum size of the IP direct input queue");
static int
sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS)
{
u_int64_t qdrops_long;
int error, qdrops;
netisr_getqdrops(&ip_direct_nh, &qdrops_long);
qdrops = qdrops_long;
error = sysctl_handle_int(oidp, &qdrops, 0, req);
if (error || !req->newptr)
return (error);
if (qdrops != 0)
return (EINVAL);
netisr_clearqdrops(&ip_direct_nh);
return (0);
}
SYSCTL_PROC(_net_inet_ip, IPCTL_INTRDQDROPS, intr_direct_queue_drops,
CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
sysctl_netinet_intr_direct_queue_drops, "I",
"Number of packets dropped from the IP direct input queue");
#endif
static void
ip_vnet_init(void *arg __unused)
{
struct pfil_head_args args;
CK_STAILQ_INIT(&V_in_ifaddrhead);
V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
ipreass_vnet_init();
args.pa_version = PFIL_VERSION;
args.pa_flags = PFIL_IN | PFIL_OUT;
args.pa_type = PFIL_TYPE_IP4;
args.pa_headname = PFIL_INET_NAME;
V_inet_pfil_head = pfil_head_register(&args);
args.pa_flags = PFIL_OUT;
args.pa_headname = PFIL_INET_LOCAL_NAME;
V_inet_local_pfil_head = pfil_head_register(&args);
if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET,
&V_ipsec_hhh_in[HHOOK_IPSEC_INET],
HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
printf("%s: WARNING: unable to register input helper hook\n",
__func__);
if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET,
&V_ipsec_hhh_out[HHOOK_IPSEC_INET],
HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
printf("%s: WARNING: unable to register output helper hook\n",
__func__);
#ifdef VIMAGE
netisr_register_vnet(&ip_nh);
#ifdef RSS
netisr_register_vnet(&ip_direct_nh);
#endif
#endif
}
VNET_SYSINIT(ip_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
ip_vnet_init, NULL);
static void
ip_init(const void *unused __unused)
{
struct ifnet *ifp;
ipreass_init();
IPPROTO_REGISTER(IPPROTO_ICMP, icmp_input, NULL);
IPPROTO_REGISTER(IPPROTO_IGMP, igmp_input, NULL);
IPPROTO_REGISTER(IPPROTO_RSVP, rsvp_input, NULL);
IPPROTO_REGISTER(IPPROTO_IPV4, encap4_input, NULL);
IPPROTO_REGISTER(IPPROTO_MOBILE, encap4_input, NULL);
IPPROTO_REGISTER(IPPROTO_ETHERIP, encap4_input, NULL);
IPPROTO_REGISTER(IPPROTO_GRE, encap4_input, NULL);
IPPROTO_REGISTER(IPPROTO_IPV6, encap4_input, NULL);
IPPROTO_REGISTER(IPPROTO_PIM, encap4_input, NULL);
#ifdef SCTP
IPPROTO_REGISTER(IPPROTO_SCTP, sctp_input, sctp_ctlinput);
#endif
netisr_register(&ip_nh);
#ifdef RSS
netisr_register(&ip_direct_nh);
#endif
CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
in_ifattach(NULL, ifp);
}
SYSINIT(ip_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip_init, NULL);
#ifdef VIMAGE
static void
ip_destroy(void *unused __unused)
{
int error;
#ifdef RSS
netisr_unregister_vnet(&ip_direct_nh);
#endif
netisr_unregister_vnet(&ip_nh);
pfil_head_unregister(V_inet_pfil_head);
error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET]);
if (error != 0) {
printf("%s: WARNING: unable to deregister input helper hook "
"type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET: "
"error %d returned\n", __func__, error);
}
error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET]);
if (error != 0) {
printf("%s: WARNING: unable to deregister output helper hook "
"type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET: "
"error %d returned\n", __func__, error);
}
in_ifscrub_all();
rib_flush_routes_family(AF_INET);
ipreass_destroy();
hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
}
VNET_SYSUNINIT(ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip_destroy, NULL);
#endif
#ifdef RSS
void
ip_direct_input(struct mbuf *m)
{
struct ip *ip;
int hlen;
ip = mtod(m, struct ip *);
hlen = ip->ip_hl << 2;
#if defined(IPSEC) || defined(IPSEC_SUPPORT)
if (IPSEC_ENABLED(ipv4)) {
if (IPSEC_INPUT(ipv4, m, hlen, ip->ip_p) != 0)
return;
}
#endif
IPSTAT_INC(ips_delivered);
ip_protox[ip->ip_p](&m, &hlen, ip->ip_p);
}
#endif
void
ip_input(struct mbuf *m)
{
struct ip *ip = NULL;
struct in_ifaddr *ia = NULL;
struct ifaddr *ifa;
struct ifnet *ifp;
int hlen = 0;
uint16_t sum, ip_len;
int dchg = 0;
struct in_addr odst;
bool strong_es;
M_ASSERTPKTHDR(m);
NET_EPOCH_ASSERT();
if (m->m_flags & M_FASTFWD_OURS) {
m->m_flags &= ~M_FASTFWD_OURS;
ip = mtod(m, struct ip *);
hlen = ip->ip_hl << 2;
ip_len = ntohs(ip->ip_len);
goto ours;
}
IPSTAT_INC(ips_total);
if (__predict_false(m->m_pkthdr.len < sizeof(struct ip)))
goto tooshort;
if (m->m_len < sizeof(struct ip)) {
m = m_pullup(m, sizeof(struct ip));
if (__predict_false(m == NULL)) {
IPSTAT_INC(ips_toosmall);
return;
}
}
ip = mtod(m, struct ip *);
if (__predict_false(ip->ip_v != IPVERSION)) {
IPSTAT_INC(ips_badvers);
goto bad;
}
hlen = ip->ip_hl << 2;
if (__predict_false(hlen < sizeof(struct ip))) {
IPSTAT_INC(ips_badhlen);
goto bad;
}
if (hlen > m->m_len) {
m = m_pullup(m, hlen);
if (__predict_false(m == NULL)) {
IPSTAT_INC(ips_badhlen);
return;
}
ip = mtod(m, struct ip *);
}
IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL);
ifp = m->m_pkthdr.rcvif;
if (IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) ||
IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) {
if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
IPSTAT_INC(ips_badaddr);
goto bad;
}
}
if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
} else if (m->m_pkthdr.csum_flags & CSUM_IP) {
sum = 0;
} else {
if (hlen == sizeof(struct ip)) {
sum = in_cksum_hdr(ip);
} else {
sum = in_cksum(m, hlen);
}
}
if (__predict_false(sum)) {
IPSTAT_INC(ips_badsum);
goto bad;
}
ip_len = ntohs(ip->ip_len);
if (__predict_false(ip_len < hlen)) {
IPSTAT_INC(ips_badlen);
goto bad;
}
if (__predict_false(m->m_pkthdr.len < ip_len)) {
tooshort:
IPSTAT_INC(ips_tooshort);
goto bad;
}
if (m->m_pkthdr.len > ip_len) {
if (m->m_len == m->m_pkthdr.len) {
m->m_len = ip_len;
m->m_pkthdr.len = ip_len;
} else
m_adj(m, ip_len - m->m_pkthdr.len);
}
if (V_ipforwarding != 0
#if defined(IPSEC) || defined(IPSEC_SUPPORT)
&& (!IPSEC_ENABLED(ipv4) ||
IPSEC_CAPS(ipv4, m, IPSEC_CAP_OPERABLE) == 0)
#endif
) {
if ((m = ip_tryforward(m)) == NULL)
return;
if (m->m_flags & M_FASTFWD_OURS) {
m->m_flags &= ~M_FASTFWD_OURS;
ip = mtod(m, struct ip *);
goto ours;
}
}
#if defined(IPSEC) || defined(IPSEC_SUPPORT)
if (IPSEC_ENABLED(ipv4) &&
IPSEC_CAPS(ipv4, m, IPSEC_CAP_BYPASS_FILTER) != 0)
goto passin;
#endif
if (!PFIL_HOOKED_IN(V_inet_pfil_head))
goto passin;
odst = ip->ip_dst;
if (pfil_mbuf_in(V_inet_pfil_head, &m, ifp, NULL) !=
PFIL_PASS)
return;
ip = mtod(m, struct ip *);
dchg = (odst.s_addr != ip->ip_dst.s_addr);
if (m->m_flags & M_FASTFWD_OURS) {
m->m_flags &= ~M_FASTFWD_OURS;
goto ours;
}
if (m->m_flags & M_IP_NEXTHOP) {
if (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
ip_forward(m, 1);
return;
}
}
passin:
if (__predict_false(ntohl(ip->ip_dst.s_addr) == INADDR_ANY)) {
IPSTAT_INC(ips_badaddr);
goto bad;
}
if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
return;
if (ip->ip_p == IPPROTO_RSVP && V_rsvp_on)
goto ours;
if (CK_STAILQ_EMPTY(&V_in_ifaddrhead) &&
(m->m_flags & (M_MCAST|M_BCAST)) == 0)
goto ours;
strong_es = V_ip_strong_es && (V_ipforwarding == 0) &&
((ifp->if_flags & IFF_LOOPBACK) == 0) &&
ifp->if_carp == NULL && (dchg == 0);
CK_LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
if (IA_SIN(ia)->sin_addr.s_addr != ip->ip_dst.s_addr)
continue;
if (__predict_false(strong_es && ia->ia_ifp != ifp)) {
IPSTAT_INC(ips_badaddr);
goto bad;
}
if (V_ip_sav && !(ifp->if_flags & IFF_LOOPBACK) &&
__predict_false(in_localip_fib(ip->ip_src, ifp->if_fib))) {
IPSTAT_INC(ips_badaddr);
goto bad;
}
counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
goto ours;
}
if (ifp->if_flags & IFF_BROADCAST) {
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
ia = ifatoia(ifa);
if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
ip->ip_dst.s_addr) {
counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
counter_u64_add(ia->ia_ifa.ifa_ibytes,
m->m_pkthdr.len);
goto ours;
}
#ifdef BOOTP_COMPAT
if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
counter_u64_add(ia->ia_ifa.ifa_ibytes,
m->m_pkthdr.len);
goto ours;
}
#endif
}
ia = NULL;
}
if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
if (V_ip_mrouting_enabled &&
!IN_LINKLOCAL(ntohl(ip->ip_src.s_addr))) {
if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
IPSTAT_INC(ips_cantforward);
m_freem(m);
return;
}
if (ip->ip_p == IPPROTO_IGMP) {
goto ours;
}
IPSTAT_INC(ips_forward);
}
goto ours;
}
if (in_broadcast(ip->ip_dst))
goto ours;
if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
IN_LINKLOCAL(ntohl(ip->ip_src.s_addr))) {
IPSTAT_INC(ips_cantforward);
m_freem(m);
return;
}
if (V_ipforwarding == 0) {
IPSTAT_INC(ips_cantforward);
m_freem(m);
} else {
ip_forward(m, dchg);
}
return;
ours:
#ifdef IPSTEALTH
if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1))
return;
#endif
if (PFIL_HOOKED_OUT(V_inet_local_pfil_head)) {
if (pfil_mbuf_out(V_inet_local_pfil_head, &m, V_loif, NULL) !=
PFIL_PASS)
return;
ip = mtod(m, struct ip *);
}
if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
m = ip_reass(m);
if (m == NULL)
return;
ip = mtod(m, struct ip *);
hlen = ip->ip_hl << 2;
}
#if defined(IPSEC) || defined(IPSEC_SUPPORT)
if (IPSEC_ENABLED(ipv4)) {
if (IPSEC_INPUT(ipv4, m, hlen, ip->ip_p) != 0)
return;
}
#endif
IPSTAT_INC(ips_delivered);
ip_protox[ip->ip_p](&m, &hlen, ip->ip_p);
return;
bad:
m_freem(m);
}
int
ipproto_register(uint8_t proto, ipproto_input_t input, ipproto_ctlinput_t ctl)
{
MPASS(proto > 0);
if (ip_protox[proto] == rip_input) {
ip_protox[proto] = input;
ip_ctlprotox[proto] = ctl;
return (0);
} else
return (EEXIST);
}
int
ipproto_unregister(uint8_t proto)
{
MPASS(proto > 0);
if (ip_protox[proto] != rip_input) {
ip_protox[proto] = rip_input;
ip_ctlprotox[proto] = rip_ctlinput;
return (0);
} else
return (ENOENT);
}
void
ip_forward(struct mbuf *m, int srcrt)
{
struct ip *ip = mtod(m, struct ip *);
struct in_ifaddr *ia;
struct mbuf *mcopy;
struct sockaddr_in *sin;
struct in_addr dest;
struct route ro;
uint32_t flowid;
int error, type = 0, code = 0, mtu = 0;
NET_EPOCH_ASSERT();
if (m->m_flags & (M_BCAST|M_MCAST) || !in_canforward(ip->ip_dst)) {
IPSTAT_INC(ips_cantforward);
m_freem(m);
return;
}
if (
#ifdef IPSTEALTH
V_ipstealth == 0 &&
#endif
ip->ip_ttl <= IPTTLDEC) {
icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
return;
}
bzero(&ro, sizeof(ro));
sin = (struct sockaddr_in *)&ro.ro_dst;
sin->sin_family = AF_INET;
sin->sin_len = sizeof(*sin);
sin->sin_addr = ip->ip_dst;
flowid = m->m_pkthdr.flowid;
ro.ro_nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_REF, flowid);
if (ro.ro_nh != NULL) {
if (ro.ro_nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) {
IPSTAT_INC(ips_cantforward);
m_freem(m);
NH_FREE(ro.ro_nh);
return;
}
if (ro.ro_nh->nh_flags & NHF_REJECT) {
IPSTAT_INC(ips_cantforward);
NH_FREE(ro.ro_nh);
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
return;
}
ia = ifatoia(ro.ro_nh->nh_ifa);
} else
ia = NULL;
mcopy = m_gethdr(M_NOWAIT, m->m_type);
if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
m_free(mcopy);
mcopy = NULL;
}
if (mcopy != NULL) {
mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
mcopy->m_pkthdr.len = mcopy->m_len;
m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
}
#ifdef IPSTEALTH
if (V_ipstealth == 0)
#endif
ip->ip_ttl -= IPTTLDEC;
#if defined(IPSEC) || defined(IPSEC_SUPPORT)
if (IPSEC_ENABLED(ipv4)) {
if ((error = IPSEC_FORWARD(ipv4, m)) != 0) {
RO_NHFREE(&ro);
m_freem(mcopy);
if (error != EINPROGRESS)
IPSTAT_INC(ips_cantforward);
return;
}
}
#endif
dest.s_addr = 0;
if (!srcrt && V_ipsendredirects &&
ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
struct nhop_object *nh;
nh = ro.ro_nh;
if (nh != NULL && ((nh->nh_flags & (NHF_REDIRECT|NHF_DEFAULT)) == 0)) {
struct in_ifaddr *nh_ia = (struct in_ifaddr *)(nh->nh_ifa);
u_long src = ntohl(ip->ip_src.s_addr);
if (nh_ia != NULL &&
(src & nh_ia->ia_subnetmask) == nh_ia->ia_subnet) {
type = ICMP_REDIRECT;
code = ICMP_REDIRECT_HOST;
if (nh->nh_flags & NHF_GATEWAY) {
if (nh->gw_sa.sa_family == AF_INET)
dest.s_addr = nh->gw4_sa.sin_addr.s_addr;
else
type = 0;
} else
dest.s_addr = ip->ip_dst.s_addr;
}
}
}
error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
if (error == EMSGSIZE && ro.ro_nh)
mtu = ro.ro_nh->nh_mtu;
RO_NHFREE(&ro);
if (error)
IPSTAT_INC(ips_cantforward);
else {
IPSTAT_INC(ips_forward);
if (type)
IPSTAT_INC(ips_redirectsent);
else {
if (mcopy)
m_freem(mcopy);
return;
}
}
if (mcopy == NULL)
return;
switch (error) {
case 0:
break;
case ENETUNREACH:
case EHOSTUNREACH:
case ENETDOWN:
case EHOSTDOWN:
default:
type = ICMP_UNREACH;
code = ICMP_UNREACH_HOST;
break;
case EMSGSIZE:
type = ICMP_UNREACH;
code = ICMP_UNREACH_NEEDFRAG;
if (mtu != 0) {
if (ia != NULL)
mtu = min(mtu, ia->ia_ifp->if_mtu);
} else {
if (ia != NULL)
mtu = ia->ia_ifp->if_mtu;
else
mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
}
IPSTAT_INC(ips_cantfrag);
break;
case ENOBUFS:
case EACCES:
m_freem(mcopy);
return;
}
icmp_error(mcopy, type, code, dest.s_addr, mtu);
}
#define CHECK_SO_CT(sp, ct) \
(((sp->so_options & SO_TIMESTAMP) && (sp->so_ts_clock == ct)) ? 1 : 0)
void
ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
struct mbuf *m)
{
bool stamped;
stamped = false;
if ((inp->inp_socket->so_options & SO_BINTIME) ||
CHECK_SO_CT(inp->inp_socket, SO_TS_BINTIME)) {
struct bintime boottimebin, bt;
struct timespec ts1;
if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
M_TSTMP)) {
mbuf_tstmp2timespec(m, &ts1);
timespec2bintime(&ts1, &bt);
getboottimebin(&boottimebin);
bintime_add(&bt, &boottimebin);
} else {
bintime(&bt);
}
*mp = sbcreatecontrol(&bt, sizeof(bt), SCM_BINTIME,
SOL_SOCKET, M_NOWAIT);
if (*mp != NULL) {
mp = &(*mp)->m_next;
stamped = true;
}
}
if (CHECK_SO_CT(inp->inp_socket, SO_TS_REALTIME_MICRO)) {
struct bintime boottimebin, bt1;
struct timespec ts1;
struct timeval tv;
if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
M_TSTMP)) {
mbuf_tstmp2timespec(m, &ts1);
timespec2bintime(&ts1, &bt1);
getboottimebin(&boottimebin);
bintime_add(&bt1, &boottimebin);
bintime2timeval(&bt1, &tv);
} else {
microtime(&tv);
}
*mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv), SCM_TIMESTAMP,
SOL_SOCKET, M_NOWAIT);
if (*mp != NULL) {
mp = &(*mp)->m_next;
stamped = true;
}
} else if (CHECK_SO_CT(inp->inp_socket, SO_TS_REALTIME)) {
struct bintime boottimebin;
struct timespec ts, ts1;
if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
M_TSTMP)) {
mbuf_tstmp2timespec(m, &ts);
getboottimebin(&boottimebin);
bintime2timespec(&boottimebin, &ts1);
timespecadd(&ts, &ts1, &ts);
} else {
nanotime(&ts);
}
*mp = sbcreatecontrol(&ts, sizeof(ts), SCM_REALTIME,
SOL_SOCKET, M_NOWAIT);
if (*mp != NULL) {
mp = &(*mp)->m_next;
stamped = true;
}
} else if (CHECK_SO_CT(inp->inp_socket, SO_TS_MONOTONIC)) {
struct timespec ts;
if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
M_TSTMP))
mbuf_tstmp2timespec(m, &ts);
else
nanouptime(&ts);
*mp = sbcreatecontrol(&ts, sizeof(ts), SCM_MONOTONIC,
SOL_SOCKET, M_NOWAIT);
if (*mp != NULL) {
mp = &(*mp)->m_next;
stamped = true;
}
}
if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
M_TSTMP)) {
struct sock_timestamp_info sti;
bzero(&sti, sizeof(sti));
sti.st_info_flags = ST_INFO_HW;
if ((m->m_flags & M_TSTMP_HPREC) != 0)
sti.st_info_flags |= ST_INFO_HW_HPREC;
*mp = sbcreatecontrol(&sti, sizeof(sti), SCM_TIME_INFO,
SOL_SOCKET, M_NOWAIT);
if (*mp != NULL)
mp = &(*mp)->m_next;
}
if (inp->inp_flags & INP_RECVDSTADDR) {
*mp = sbcreatecontrol(&ip->ip_dst, sizeof(struct in_addr),
IP_RECVDSTADDR, IPPROTO_IP, M_NOWAIT);
if (*mp)
mp = &(*mp)->m_next;
}
if (inp->inp_flags & INP_RECVTTL) {
*mp = sbcreatecontrol(&ip->ip_ttl, sizeof(u_char), IP_RECVTTL,
IPPROTO_IP, M_NOWAIT);
if (*mp)
mp = &(*mp)->m_next;
}
#ifdef notyet
if (inp->inp_flags & INP_RECVOPTS) {
*mp = sbcreatecontrol(opts_deleted_above,
sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP, M_NOWAIT);
if (*mp)
mp = &(*mp)->m_next;
}
if (inp->inp_flags & INP_RECVRETOPTS) {
*mp = sbcreatecontrol(ip_srcroute(m), sizeof(struct in_addr),
IP_RECVRETOPTS, IPPROTO_IP, M_NOWAIT);
if (*mp)
mp = &(*mp)->m_next;
}
#endif
if (inp->inp_flags & INP_RECVIF) {
struct ifnet *ifp;
struct sdlbuf {
struct sockaddr_dl sdl;
u_char pad[32];
} sdlbuf;
struct sockaddr_dl *sdp;
struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
if ((ifp = m->m_pkthdr.rcvif)) {
sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
if (sdp->sdl_family != AF_LINK ||
sdp->sdl_len > sizeof(sdlbuf)) {
goto makedummy;
}
bcopy(sdp, sdl2, sdp->sdl_len);
} else {
makedummy:
sdl2->sdl_len =
offsetof(struct sockaddr_dl, sdl_data[0]);
sdl2->sdl_family = AF_LINK;
sdl2->sdl_index = 0;
sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
}
*mp = sbcreatecontrol(sdl2, sdl2->sdl_len, IP_RECVIF,
IPPROTO_IP, M_NOWAIT);
if (*mp)
mp = &(*mp)->m_next;
}
if (inp->inp_flags & INP_RECVTOS) {
*mp = sbcreatecontrol(&ip->ip_tos, sizeof(u_char), IP_RECVTOS,
IPPROTO_IP, M_NOWAIT);
if (*mp)
mp = &(*mp)->m_next;
}
if (inp->inp_flags2 & INP_RECVFLOWID) {
uint32_t flowid, flow_type;
flowid = m->m_pkthdr.flowid;
flow_type = M_HASHTYPE_GET(m);
*mp = sbcreatecontrol(&flowid, sizeof(uint32_t), IP_FLOWID,
IPPROTO_IP, M_NOWAIT);
if (*mp)
mp = &(*mp)->m_next;
*mp = sbcreatecontrol(&flow_type, sizeof(uint32_t),
IP_FLOWTYPE, IPPROTO_IP, M_NOWAIT);
if (*mp)
mp = &(*mp)->m_next;
}
#ifdef RSS
if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
uint32_t flowid, flow_type;
uint32_t rss_bucketid;
flowid = m->m_pkthdr.flowid;
flow_type = M_HASHTYPE_GET(m);
if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
*mp = sbcreatecontrol(&rss_bucketid, sizeof(uint32_t),
IP_RSSBUCKETID, IPPROTO_IP, M_NOWAIT);
if (*mp)
mp = &(*mp)->m_next;
}
}
#endif
}
VNET_DEFINE_STATIC(int, ip_rsvp_on);
VNET_DEFINE(struct socket *, ip_rsvpd);
#define V_ip_rsvp_on VNET(ip_rsvp_on)
int
ip_rsvp_init(struct socket *so)
{
if (V_ip_rsvpd != NULL)
return EADDRINUSE;
V_ip_rsvpd = so;
if (!V_ip_rsvp_on) {
V_ip_rsvp_on = 1;
V_rsvp_on++;
}
return 0;
}
int
ip_rsvp_done(void)
{
V_ip_rsvpd = NULL;
if (V_ip_rsvp_on) {
V_ip_rsvp_on = 0;
V_rsvp_on--;
}
return 0;
}
int
rsvp_input(struct mbuf **mp, int *offp, int proto)
{
struct mbuf *m;
m = *mp;
*mp = NULL;
if (rsvp_input_p) {
*mp = m;
rsvp_input_p(mp, offp, proto);
return (IPPROTO_DONE);
}
if (!V_rsvp_on) {
m_freem(m);
return (IPPROTO_DONE);
}
if (V_ip_rsvpd != NULL) {
*mp = m;
rip_input(mp, offp, proto);
return (IPPROTO_DONE);
}
m_freem(m);
return (IPPROTO_DONE);
}