Blame view
net/ipv4/netfilter.c
6.22 KB
2cc7d5730 [NETFILTER]: Move... |
1 |
/* IPv4 specific functions of netfilter core */ |
020b4c12d [NETFILTER]: Move... |
2 3 |
#include <linux/kernel.h> #include <linux/netfilter.h> |
2cc7d5730 [NETFILTER]: Move... |
4 |
#include <linux/netfilter_ipv4.h> |
3e3850e98 [NETFILTER]: Fix ... |
5 |
#include <linux/ip.h> |
2ca7b0ac0 [NETFILTER]: Avoi... |
6 |
#include <linux/skbuff.h> |
5a0e3ad6a include cleanup: ... |
7 |
#include <linux/gfp.h> |
bc3b2d7fb net: Add export.h... |
8 |
#include <linux/export.h> |
020b4c12d [NETFILTER]: Move... |
9 |
#include <net/route.h> |
3e3850e98 [NETFILTER]: Fix ... |
10 11 |
#include <net/xfrm.h> #include <net/ip.h> |
c01cd429f [NETFILTER]: nf_q... |
12 |
#include <net/netfilter/nf_queue.h> |
020b4c12d [NETFILTER]: Move... |
13 14 |
/* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */ |
3db05fea5 [NETFILTER]: Repl... |
15 |
int ip_route_me_harder(struct sk_buff *skb, unsigned addr_type) |
020b4c12d [NETFILTER]: Move... |
16 |
{ |
adf30907d net: skb->dst acc... |
17 |
struct net *net = dev_net(skb_dst(skb)->dev); |
3db05fea5 [NETFILTER]: Repl... |
18 |
const struct iphdr *iph = ip_hdr(skb); |
020b4c12d [NETFILTER]: Move... |
19 |
struct rtable *rt; |
9d6ec9380 ipv4: Use flowi4 ... |
20 |
struct flowi4 fl4 = {}; |
ed6e4ef83 netfilter: Fix ip... |
21 |
__be32 saddr = iph->saddr; |
797fd3913 netfilter: TCP an... |
22 |
__u8 flags = skb->sk ? inet_sk_flowi_flags(skb->sk) : 0; |
020b4c12d [NETFILTER]: Move... |
23 |
unsigned int hh_len; |
797fd3913 netfilter: TCP an... |
24 25 26 27 28 29 |
if (addr_type == RTN_UNSPEC) addr_type = inet_addr_type(net, saddr); if (addr_type == RTN_LOCAL || addr_type == RTN_UNICAST) flags |= FLOWI_FLAG_ANYSRC; else saddr = 0; |
b4c4ed175 [NETFILTER]: add ... |
30 |
|
020b4c12d [NETFILTER]: Move... |
31 |
/* some non-standard hacks like ipt_REJECT.c:send_reset() can cause |
6e23ae2a4 [NETFILTER]: Intr... |
32 |
* packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook. |
020b4c12d [NETFILTER]: Move... |
33 |
*/ |
ed6e4ef83 netfilter: Fix ip... |
34 35 36 37 38 |
fl4.daddr = iph->daddr; fl4.saddr = saddr; fl4.flowi4_tos = RT_TOS(iph->tos); fl4.flowi4_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0; fl4.flowi4_mark = skb->mark; |
797fd3913 netfilter: TCP an... |
39 |
fl4.flowi4_flags = flags; |
ed6e4ef83 netfilter: Fix ip... |
40 41 42 |
rt = ip_route_output_key(net, &fl4); if (IS_ERR(rt)) return -1; |
020b4c12d [NETFILTER]: Move... |
43 |
|
ed6e4ef83 netfilter: Fix ip... |
44 45 46 |
/* Drop old route. */ skb_dst_drop(skb); skb_dst_set(skb, &rt->dst); |
e905a9eda [NET] IPV4: Fix w... |
47 |
|
adf30907d net: skb->dst acc... |
48 |
if (skb_dst(skb)->error) |
020b4c12d [NETFILTER]: Move... |
49 |
return -1; |
3e3850e98 [NETFILTER]: Fix ... |
50 |
#ifdef CONFIG_XFRM |
3db05fea5 [NETFILTER]: Repl... |
51 |
if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) && |
9d6ec9380 ipv4: Use flowi4 ... |
52 |
xfrm_decode_session(skb, flowi4_to_flowi(&fl4), AF_INET) == 0) { |
adf30907d net: skb->dst acc... |
53 54 |
struct dst_entry *dst = skb_dst(skb); skb_dst_set(skb, NULL); |
9d6ec9380 ipv4: Use flowi4 ... |
55 |
dst = xfrm_lookup(net, dst, flowi4_to_flowi(&fl4), skb->sk, 0); |
452edd598 xfrm: Return dst ... |
56 |
if (IS_ERR(dst)) |
3e3850e98 [NETFILTER]: Fix ... |
57 |
return -1; |
adf30907d net: skb->dst acc... |
58 59 |
skb_dst_set(skb, dst); } |
3e3850e98 [NETFILTER]: Fix ... |
60 |
#endif |
020b4c12d [NETFILTER]: Move... |
61 |
/* Change in oif may mean change in hh_len. */ |
adf30907d net: skb->dst acc... |
62 |
hh_len = skb_dst(skb)->dev->hard_header_len; |
3db05fea5 [NETFILTER]: Repl... |
63 |
if (skb_headroom(skb) < hh_len && |
5e2afba4e netfilter: possib... |
64 65 |
pskb_expand_head(skb, HH_DATA_ALIGN(hh_len - skb_headroom(skb)), 0, GFP_ATOMIC)) |
2ca7b0ac0 [NETFILTER]: Avoi... |
66 |
return -1; |
020b4c12d [NETFILTER]: Move... |
67 68 69 70 |
return 0; } EXPORT_SYMBOL(ip_route_me_harder); |
2cc7d5730 [NETFILTER]: Move... |
71 |
|
ee68cea2c [NETFILTER]: Fix ... |
72 |
#ifdef CONFIG_XFRM |
3db05fea5 [NETFILTER]: Repl... |
73 |
int ip_xfrm_me_harder(struct sk_buff *skb) |
ee68cea2c [NETFILTER]: Fix ... |
74 75 76 77 |
{ struct flowi fl; unsigned int hh_len; struct dst_entry *dst; |
3db05fea5 [NETFILTER]: Repl... |
78 |
if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) |
ee68cea2c [NETFILTER]: Fix ... |
79 |
return 0; |
3db05fea5 [NETFILTER]: Repl... |
80 |
if (xfrm_decode_session(skb, &fl, AF_INET) < 0) |
ee68cea2c [NETFILTER]: Fix ... |
81 |
return -1; |
adf30907d net: skb->dst acc... |
82 |
dst = skb_dst(skb); |
ee68cea2c [NETFILTER]: Fix ... |
83 84 85 |
if (dst->xfrm) dst = ((struct xfrm_dst *)dst)->route; dst_hold(dst); |
452edd598 xfrm: Return dst ... |
86 87 |
dst = xfrm_lookup(dev_net(dst->dev), dst, &fl, skb->sk, 0); if (IS_ERR(dst)) |
ee68cea2c [NETFILTER]: Fix ... |
88 |
return -1; |
adf30907d net: skb->dst acc... |
89 90 |
skb_dst_drop(skb); skb_dst_set(skb, dst); |
ee68cea2c [NETFILTER]: Fix ... |
91 92 |
/* Change in oif may mean change in hh_len. */ |
adf30907d net: skb->dst acc... |
93 |
hh_len = skb_dst(skb)->dev->hard_header_len; |
3db05fea5 [NETFILTER]: Repl... |
94 95 |
if (skb_headroom(skb) < hh_len && pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC)) |
2ca7b0ac0 [NETFILTER]: Avoi... |
96 |
return -1; |
ee68cea2c [NETFILTER]: Fix ... |
97 98 99 100 |
return 0; } EXPORT_SYMBOL(ip_xfrm_me_harder); #endif |
eb9c7ebe6 [NETFILTER]: Hand... |
101 102 |
void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *); EXPORT_SYMBOL(ip_nat_decode_session); |
2cc7d5730 [NETFILTER]: Move... |
103 104 105 106 107 108 |
/* * Extra routing may needed on local out, as the QUEUE target never * returns control to the table. */ struct ip_rt_info { |
59b8bfd8f [NETFILTER]: netf... |
109 110 |
__be32 daddr; __be32 saddr; |
2cc7d5730 [NETFILTER]: Move... |
111 |
u_int8_t tos; |
5f145e44a netfilter: nfmark... |
112 |
u_int32_t mark; |
2cc7d5730 [NETFILTER]: Move... |
113 |
}; |
02f014d88 [NETFILTER]: nf_q... |
114 115 |
static void nf_ip_saveroute(const struct sk_buff *skb, struct nf_queue_entry *entry) |
2cc7d5730 [NETFILTER]: Move... |
116 |
{ |
02f014d88 [NETFILTER]: nf_q... |
117 |
struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry); |
2cc7d5730 [NETFILTER]: Move... |
118 |
|
02f014d88 [NETFILTER]: nf_q... |
119 |
if (entry->hook == NF_INET_LOCAL_OUT) { |
eddc9ec53 [SK_BUFF]: Introd... |
120 |
const struct iphdr *iph = ip_hdr(skb); |
2cc7d5730 [NETFILTER]: Move... |
121 122 123 124 |
rt_info->tos = iph->tos; rt_info->daddr = iph->daddr; rt_info->saddr = iph->saddr; |
5f145e44a netfilter: nfmark... |
125 |
rt_info->mark = skb->mark; |
2cc7d5730 [NETFILTER]: Move... |
126 127 |
} } |
02f014d88 [NETFILTER]: nf_q... |
128 129 |
static int nf_ip_reroute(struct sk_buff *skb, const struct nf_queue_entry *entry) |
2cc7d5730 [NETFILTER]: Move... |
130 |
{ |
02f014d88 [NETFILTER]: nf_q... |
131 |
const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry); |
2cc7d5730 [NETFILTER]: Move... |
132 |
|
02f014d88 [NETFILTER]: nf_q... |
133 |
if (entry->hook == NF_INET_LOCAL_OUT) { |
3db05fea5 [NETFILTER]: Repl... |
134 |
const struct iphdr *iph = ip_hdr(skb); |
2cc7d5730 [NETFILTER]: Move... |
135 |
|
f64f9e719 net: Move && and ... |
136 137 138 139 |
if (!(iph->tos == rt_info->tos && skb->mark == rt_info->mark && iph->daddr == rt_info->daddr && iph->saddr == rt_info->saddr)) |
3db05fea5 [NETFILTER]: Repl... |
140 |
return ip_route_me_harder(skb, RTN_UNSPEC); |
2cc7d5730 [NETFILTER]: Move... |
141 142 143 |
} return 0; } |
b51655b95 [NET]: Annotate _... |
144 |
__sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook, |
422c346fa [NETFILTER]: Add ... |
145 146 |
unsigned int dataoff, u_int8_t protocol) { |
eddc9ec53 [SK_BUFF]: Introd... |
147 |
const struct iphdr *iph = ip_hdr(skb); |
b51655b95 [NET]: Annotate _... |
148 |
__sum16 csum = 0; |
422c346fa [NETFILTER]: Add ... |
149 150 |
switch (skb->ip_summed) { |
84fa7933a [NET]: Replace CH... |
151 |
case CHECKSUM_COMPLETE: |
6e23ae2a4 [NETFILTER]: Intr... |
152 |
if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN) |
422c346fa [NETFILTER]: Add ... |
153 |
break; |
d3bc23e7e [NET]: Annotate c... |
154 |
if ((protocol == 0 && !csum_fold(skb->csum)) || |
422c346fa [NETFILTER]: Add ... |
155 |
!csum_tcpudp_magic(iph->saddr, iph->daddr, |
e905a9eda [NET] IPV4: Fix w... |
156 |
skb->len - dataoff, protocol, |
422c346fa [NETFILTER]: Add ... |
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
skb->csum)) { skb->ip_summed = CHECKSUM_UNNECESSARY; break; } /* fall through */ case CHECKSUM_NONE: if (protocol == 0) skb->csum = 0; else skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, skb->len - dataoff, protocol, 0); csum = __skb_checksum_complete(skb); } return csum; } |
422c346fa [NETFILTER]: Add ... |
173 |
EXPORT_SYMBOL(nf_ip_checksum); |
d63a65073 [NETFILTER]: Add ... |
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
static __sum16 nf_ip_checksum_partial(struct sk_buff *skb, unsigned int hook, unsigned int dataoff, unsigned int len, u_int8_t protocol) { const struct iphdr *iph = ip_hdr(skb); __sum16 csum = 0; switch (skb->ip_summed) { case CHECKSUM_COMPLETE: if (len == skb->len - dataoff) return nf_ip_checksum(skb, hook, dataoff, protocol); /* fall through */ case CHECKSUM_NONE: skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, protocol, skb->len - dataoff, 0); skb->ip_summed = CHECKSUM_NONE; |
c86ee67c7 netfilter: kill r... |
190 |
return __skb_checksum_complete_head(skb, dataoff + len); |
d63a65073 [NETFILTER]: Add ... |
191 192 193 |
} return csum; } |
31ad3dd64 netfilter: af_inf... |
194 |
static int nf_ip_route(struct net *net, struct dst_entry **dst, |
0fae2e774 netfilter: af_inf... |
195 |
struct flowi *fl, bool strict __always_unused) |
1841a4c7a [NETFILTER]: nf_c... |
196 |
{ |
31ad3dd64 netfilter: af_inf... |
197 |
struct rtable *rt = ip_route_output_key(net, &fl->u.ip4); |
b23dd4fe4 ipv4: Make output... |
198 199 200 201 |
if (IS_ERR(rt)) return PTR_ERR(rt); *dst = &rt->dst; return 0; |
1841a4c7a [NETFILTER]: nf_c... |
202 |
} |
1e796fda0 [NETFILTER]: cons... |
203 |
static const struct nf_afinfo nf_ip_afinfo = { |
d63a65073 [NETFILTER]: Add ... |
204 205 206 207 208 209 210 |
.family = AF_INET, .checksum = nf_ip_checksum, .checksum_partial = nf_ip_checksum_partial, .route = nf_ip_route, .saveroute = nf_ip_saveroute, .reroute = nf_ip_reroute, .route_key_size = sizeof(struct ip_rt_info), |
2cc7d5730 [NETFILTER]: Move... |
211 |
}; |
65b4b4e81 [NETFILTER]: Rena... |
212 |
static int ipv4_netfilter_init(void) |
2cc7d5730 [NETFILTER]: Move... |
213 |
{ |
bce8032ef [NETFILTER]: Intr... |
214 |
return nf_register_afinfo(&nf_ip_afinfo); |
2cc7d5730 [NETFILTER]: Move... |
215 |
} |
65b4b4e81 [NETFILTER]: Rena... |
216 |
static void ipv4_netfilter_fini(void) |
2cc7d5730 [NETFILTER]: Move... |
217 |
{ |
bce8032ef [NETFILTER]: Intr... |
218 |
nf_unregister_afinfo(&nf_ip_afinfo); |
2cc7d5730 [NETFILTER]: Move... |
219 |
} |
65b4b4e81 [NETFILTER]: Rena... |
220 221 |
module_init(ipv4_netfilter_init); module_exit(ipv4_netfilter_fini); |
4f536522d [NETFILTER]: kill... |
222 223 224 |
#ifdef CONFIG_SYSCTL struct ctl_path nf_net_ipv4_netfilter_sysctl_path[] = { |
f8572d8f2 sysctl net: Remov... |
225 226 227 |
{ .procname = "net", }, { .procname = "ipv4", }, { .procname = "netfilter", }, |
4f536522d [NETFILTER]: kill... |
228 229 230 231 |
{ } }; EXPORT_SYMBOL_GPL(nf_net_ipv4_netfilter_sysctl_path); #endif /* CONFIG_SYSCTL */ |