2 * Copyright (C) 2007-2008 BalaBit IT Ltd.
3 * Author: Krisztian Kovacs
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
17 #include <net/inet_sock.h>
18 #include <net/netfilter/nf_socket.h>
19 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
20 #include <net/netfilter/nf_conntrack.h>
24 extract_icmp4_fields(const struct sk_buff *skb, u8 *protocol,
25 __be32 *raddr, __be32 *laddr,
26 __be16 *rport, __be16 *lport)
28 unsigned int outside_hdrlen = ip_hdrlen(skb);
29 struct iphdr *inside_iph, _inside_iph;
30 struct icmphdr *icmph, _icmph;
31 __be16 *ports, _ports[2];
33 icmph = skb_header_pointer(skb, outside_hdrlen,
34 sizeof(_icmph), &_icmph);
38 switch (icmph->type) {
39 case ICMP_DEST_UNREACH:
40 case ICMP_SOURCE_QUENCH:
42 case ICMP_TIME_EXCEEDED:
43 case ICMP_PARAMETERPROB:
49 inside_iph = skb_header_pointer(skb, outside_hdrlen +
50 sizeof(struct icmphdr),
51 sizeof(_inside_iph), &_inside_iph);
52 if (inside_iph == NULL)
55 if (inside_iph->protocol != IPPROTO_TCP &&
56 inside_iph->protocol != IPPROTO_UDP)
59 ports = skb_header_pointer(skb, outside_hdrlen +
60 sizeof(struct icmphdr) +
61 (inside_iph->ihl << 2),
62 sizeof(_ports), &_ports);
66 /* the inside IP packet is the one quoted from our side, thus
67 * its saddr is the local address */
68 *protocol = inside_iph->protocol;
69 *laddr = inside_iph->saddr;
71 *raddr = inside_iph->daddr;
78 nf_socket_get_sock_v4(struct net *net, struct sk_buff *skb, const int doff,
80 const __be32 saddr, const __be32 daddr,
81 const __be16 sport, const __be16 dport,
82 const struct net_device *in)
86 return inet_lookup(net, &tcp_hashinfo, skb, doff,
87 saddr, sport, daddr, dport,
90 return udp4_lib_lookup(net, saddr, sport, daddr, dport,
96 struct sock *nf_sk_lookup_slow_v4(struct net *net, const struct sk_buff *skb,
97 const struct net_device *indev)
99 __be32 uninitialized_var(daddr), uninitialized_var(saddr);
100 __be16 uninitialized_var(dport), uninitialized_var(sport);
101 const struct iphdr *iph = ip_hdr(skb);
102 struct sk_buff *data_skb = NULL;
103 u8 uninitialized_var(protocol);
104 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
105 enum ip_conntrack_info ctinfo;
106 struct nf_conn const *ct;
110 if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) {
114 hp = skb_header_pointer(skb, ip_hdrlen(skb),
115 iph->protocol == IPPROTO_UDP ?
116 sizeof(*hp) : sizeof(_hdr), &_hdr);
120 protocol = iph->protocol;
125 data_skb = (struct sk_buff *)skb;
126 doff = iph->protocol == IPPROTO_TCP ?
127 ip_hdrlen(skb) + __tcp_hdrlen((struct tcphdr *)hp) :
128 ip_hdrlen(skb) + sizeof(*hp);
130 } else if (iph->protocol == IPPROTO_ICMP) {
131 if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr,
138 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
139 /* Do the lookup with the original socket address in
140 * case this is a reply packet of an established
141 * SNAT-ted connection.
143 ct = nf_ct_get(skb, &ctinfo);
145 ((iph->protocol != IPPROTO_ICMP &&
146 ctinfo == IP_CT_ESTABLISHED_REPLY) ||
147 (iph->protocol == IPPROTO_ICMP &&
148 ctinfo == IP_CT_RELATED_REPLY)) &&
149 (ct->status & IPS_SRC_NAT_DONE)) {
151 daddr = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
152 dport = (iph->protocol == IPPROTO_TCP) ?
153 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port :
154 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
158 return nf_socket_get_sock_v4(net, data_skb, doff, protocol, saddr,
159 daddr, sport, dport, indev);
161 EXPORT_SYMBOL_GPL(nf_sk_lookup_slow_v4);
163 MODULE_LICENSE("GPL");
164 MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
165 MODULE_DESCRIPTION("Netfilter IPv4 socket lookup infrastructure");