1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2008 BalaBit IT Ltd.
4 * Author: Krisztian Kovacs
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/skbuff.h>
13 #include <net/inet_sock.h>
14 #include <net/netfilter/nf_socket.h>
15 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
16 #include <net/netfilter/nf_conntrack.h>
20 extract_icmp4_fields(const struct sk_buff *skb, u8 *protocol,
21 __be32 *raddr, __be32 *laddr,
22 __be16 *rport, __be16 *lport)
24 unsigned int outside_hdrlen = ip_hdrlen(skb);
25 struct iphdr *inside_iph, _inside_iph;
26 struct icmphdr *icmph, _icmph;
27 __be16 *ports, _ports[2];
29 icmph = skb_header_pointer(skb, outside_hdrlen,
30 sizeof(_icmph), &_icmph);
34 if (!icmp_is_err(icmph->type))
37 inside_iph = skb_header_pointer(skb, outside_hdrlen +
38 sizeof(struct icmphdr),
39 sizeof(_inside_iph), &_inside_iph);
40 if (inside_iph == NULL)
43 if (inside_iph->protocol != IPPROTO_TCP &&
44 inside_iph->protocol != IPPROTO_UDP)
47 ports = skb_header_pointer(skb, outside_hdrlen +
48 sizeof(struct icmphdr) +
49 (inside_iph->ihl << 2),
50 sizeof(_ports), &_ports);
54 /* the inside IP packet is the one quoted from our side, thus
55 * its saddr is the local address */
56 *protocol = inside_iph->protocol;
57 *laddr = inside_iph->saddr;
59 *raddr = inside_iph->daddr;
66 nf_socket_get_sock_v4(struct net *net, struct sk_buff *skb, const int doff,
68 const __be32 saddr, const __be32 daddr,
69 const __be16 sport, const __be16 dport,
70 const struct net_device *in)
74 return inet_lookup(net, &tcp_hashinfo, skb, doff,
75 saddr, sport, daddr, dport,
78 return udp4_lib_lookup(net, saddr, sport, daddr, dport,
84 struct sock *nf_sk_lookup_slow_v4(struct net *net, const struct sk_buff *skb,
85 const struct net_device *indev)
89 const struct iphdr *iph = ip_hdr(skb);
90 struct sk_buff *data_skb = NULL;
92 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
93 enum ip_conntrack_info ctinfo;
94 struct nf_conn const *ct;
98 if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) {
102 hp = skb_header_pointer(skb, ip_hdrlen(skb),
103 iph->protocol == IPPROTO_UDP ?
104 sizeof(*hp) : sizeof(_hdr), &_hdr);
108 protocol = iph->protocol;
113 data_skb = (struct sk_buff *)skb;
114 doff = iph->protocol == IPPROTO_TCP ?
115 ip_hdrlen(skb) + __tcp_hdrlen((struct tcphdr *)hp) :
116 ip_hdrlen(skb) + sizeof(*hp);
118 } else if (iph->protocol == IPPROTO_ICMP) {
119 if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr,
126 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
127 /* Do the lookup with the original socket address in
128 * case this is a reply packet of an established
129 * SNAT-ted connection.
131 ct = nf_ct_get(skb, &ctinfo);
133 ((iph->protocol != IPPROTO_ICMP &&
134 ctinfo == IP_CT_ESTABLISHED_REPLY) ||
135 (iph->protocol == IPPROTO_ICMP &&
136 ctinfo == IP_CT_RELATED_REPLY)) &&
137 (ct->status & IPS_SRC_NAT_DONE)) {
139 daddr = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
140 dport = (iph->protocol == IPPROTO_TCP) ?
141 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port :
142 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
146 return nf_socket_get_sock_v4(net, data_skb, doff, protocol, saddr,
147 daddr, sport, dport, indev);
149 EXPORT_SYMBOL_GPL(nf_sk_lookup_slow_v4);
151 MODULE_LICENSE("GPL");
152 MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
153 MODULE_DESCRIPTION("Netfilter IPv4 socket lookup infrastructure");