Blame view
net/ipv4/netfilter.c
5.32 KB
f229f6ce4 netfilter: add my... |
1 2 3 4 5 6 |
/* * IPv4 specific functions of netfilter core * * Rusty Russell (C) 2000 -- This code is GPL. * Patrick McHardy (C) 2006-2012 */ |
020b4c12d [NETFILTER]: Move... |
7 8 |
#include <linux/kernel.h> #include <linux/netfilter.h> |
2cc7d5730 [NETFILTER]: Move... |
9 |
#include <linux/netfilter_ipv4.h> |
3e3850e98 [NETFILTER]: Fix ... |
10 |
#include <linux/ip.h> |
2ca7b0ac0 [NETFILTER]: Avoi... |
11 |
#include <linux/skbuff.h> |
5a0e3ad6a include cleanup: ... |
12 |
#include <linux/gfp.h> |
bc3b2d7fb net: Add export.h... |
13 |
#include <linux/export.h> |
020b4c12d [NETFILTER]: Move... |
14 |
#include <net/route.h> |
3e3850e98 [NETFILTER]: Fix ... |
15 16 |
#include <net/xfrm.h> #include <net/ip.h> |
c01cd429f [NETFILTER]: nf_q... |
17 |
#include <net/netfilter/nf_queue.h> |
020b4c12d [NETFILTER]: Move... |
18 19 |
/* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */ |
e45f50660 ipv4: Pass struct... |
20 |
int ip_route_me_harder(struct net *net, struct sk_buff *skb, unsigned int addr_type) |
020b4c12d [NETFILTER]: Move... |
21 |
{ |
3db05fea5 [NETFILTER]: Repl... |
22 |
const struct iphdr *iph = ip_hdr(skb); |
020b4c12d [NETFILTER]: Move... |
23 |
struct rtable *rt; |
9d6ec9380 ipv4: Use flowi4 ... |
24 |
struct flowi4 fl4 = {}; |
ed6e4ef83 netfilter: Fix ip... |
25 |
__be32 saddr = iph->saddr; |
29e09229d netfilter: use sk... |
26 27 |
const struct sock *sk = skb_to_full_sk(skb); __u8 flags = sk ? inet_sk_flowi_flags(sk) : 0; |
6d8b49c3a netfilter: Update... |
28 |
struct net_device *dev = skb_dst(skb)->dev; |
020b4c12d [NETFILTER]: Move... |
29 |
unsigned int hh_len; |
797fd3913 netfilter: TCP an... |
30 |
if (addr_type == RTN_UNSPEC) |
6d8b49c3a netfilter: Update... |
31 |
addr_type = inet_addr_type_dev_table(net, dev, saddr); |
797fd3913 netfilter: TCP an... |
32 33 34 35 |
if (addr_type == RTN_LOCAL || addr_type == RTN_UNICAST) flags |= FLOWI_FLAG_ANYSRC; else saddr = 0; |
b4c4ed175 [NETFILTER]: add ... |
36 |
|
020b4c12d [NETFILTER]: Move... |
37 |
/* some non-standard hacks like ipt_REJECT.c:send_reset() can cause |
6e23ae2a4 [NETFILTER]: Intr... |
38 |
* packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook. |
020b4c12d [NETFILTER]: Move... |
39 |
*/ |
ed6e4ef83 netfilter: Fix ip... |
40 41 42 |
fl4.daddr = iph->daddr; fl4.saddr = saddr; fl4.flowi4_tos = RT_TOS(iph->tos); |
29e09229d netfilter: use sk... |
43 |
fl4.flowi4_oif = sk ? sk->sk_bound_dev_if : 0; |
6d8b49c3a netfilter: Update... |
44 45 |
if (!fl4.flowi4_oif) fl4.flowi4_oif = l3mdev_master_ifindex(dev); |
ed6e4ef83 netfilter: Fix ip... |
46 |
fl4.flowi4_mark = skb->mark; |
797fd3913 netfilter: TCP an... |
47 |
fl4.flowi4_flags = flags; |
ed6e4ef83 netfilter: Fix ip... |
48 49 |
rt = ip_route_output_key(net, &fl4); if (IS_ERR(rt)) |
c9e1673a0 netfilter: ipv4: ... |
50 |
return PTR_ERR(rt); |
020b4c12d [NETFILTER]: Move... |
51 |
|
ed6e4ef83 netfilter: Fix ip... |
52 53 54 |
/* Drop old route. */ skb_dst_drop(skb); skb_dst_set(skb, &rt->dst); |
e905a9eda [NET] IPV4: Fix w... |
55 |
|
adf30907d net: skb->dst acc... |
56 |
if (skb_dst(skb)->error) |
c9e1673a0 netfilter: ipv4: ... |
57 |
return skb_dst(skb)->error; |
020b4c12d [NETFILTER]: Move... |
58 |
|
3e3850e98 [NETFILTER]: Fix ... |
59 |
#ifdef CONFIG_XFRM |
3db05fea5 [NETFILTER]: Repl... |
60 |
if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) && |
9d6ec9380 ipv4: Use flowi4 ... |
61 |
xfrm_decode_session(skb, flowi4_to_flowi(&fl4), AF_INET) == 0) { |
adf30907d net: skb->dst acc... |
62 63 |
struct dst_entry *dst = skb_dst(skb); skb_dst_set(skb, NULL); |
29e09229d netfilter: use sk... |
64 |
dst = xfrm_lookup(net, dst, flowi4_to_flowi(&fl4), sk, 0); |
452edd598 xfrm: Return dst ... |
65 |
if (IS_ERR(dst)) |
3d0a06207 netfilter: remove... |
66 |
return PTR_ERR(dst); |
adf30907d net: skb->dst acc... |
67 68 |
skb_dst_set(skb, dst); } |
3e3850e98 [NETFILTER]: Fix ... |
69 |
#endif |
020b4c12d [NETFILTER]: Move... |
70 |
/* Change in oif may mean change in hh_len. */ |
adf30907d net: skb->dst acc... |
71 |
hh_len = skb_dst(skb)->dev->hard_header_len; |
3db05fea5 [NETFILTER]: Repl... |
72 |
if (skb_headroom(skb) < hh_len && |
5e2afba4e netfilter: possib... |
73 74 |
pskb_expand_head(skb, HH_DATA_ALIGN(hh_len - skb_headroom(skb)), 0, GFP_ATOMIC)) |
c9e1673a0 netfilter: ipv4: ... |
75 |
return -ENOMEM; |
020b4c12d [NETFILTER]: Move... |
76 77 78 79 |
return 0; } EXPORT_SYMBOL(ip_route_me_harder); |
2cc7d5730 [NETFILTER]: Move... |
80 81 82 83 84 85 86 |
/* * Extra routing may needed on local out, as the QUEUE target never * returns control to the table. */ struct ip_rt_info { |
59b8bfd8f [NETFILTER]: netf... |
87 88 |
__be32 daddr; __be32 saddr; |
2cc7d5730 [NETFILTER]: Move... |
89 |
u_int8_t tos; |
5f145e44a netfilter: nfmark... |
90 |
u_int32_t mark; |
2cc7d5730 [NETFILTER]: Move... |
91 |
}; |
02f014d88 [NETFILTER]: nf_q... |
92 93 |
static void nf_ip_saveroute(const struct sk_buff *skb, struct nf_queue_entry *entry) |
2cc7d5730 [NETFILTER]: Move... |
94 |
{ |
02f014d88 [NETFILTER]: nf_q... |
95 |
struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry); |
2cc7d5730 [NETFILTER]: Move... |
96 |
|
1d1de89b9 netfilter: Use nf... |
97 |
if (entry->state.hook == NF_INET_LOCAL_OUT) { |
eddc9ec53 [SK_BUFF]: Introd... |
98 |
const struct iphdr *iph = ip_hdr(skb); |
2cc7d5730 [NETFILTER]: Move... |
99 100 101 102 |
rt_info->tos = iph->tos; rt_info->daddr = iph->daddr; rt_info->saddr = iph->saddr; |
5f145e44a netfilter: nfmark... |
103 |
rt_info->mark = skb->mark; |
2cc7d5730 [NETFILTER]: Move... |
104 105 |
} } |
d815d90bb netfilter: Push s... |
106 |
static int nf_ip_reroute(struct net *net, struct sk_buff *skb, |
02f014d88 [NETFILTER]: nf_q... |
107 |
const struct nf_queue_entry *entry) |
2cc7d5730 [NETFILTER]: Move... |
108 |
{ |
02f014d88 [NETFILTER]: nf_q... |
109 |
const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry); |
2cc7d5730 [NETFILTER]: Move... |
110 |
|
1d1de89b9 netfilter: Use nf... |
111 |
if (entry->state.hook == NF_INET_LOCAL_OUT) { |
3db05fea5 [NETFILTER]: Repl... |
112 |
const struct iphdr *iph = ip_hdr(skb); |
2cc7d5730 [NETFILTER]: Move... |
113 |
|
f64f9e719 net: Move && and ... |
114 115 116 117 |
if (!(iph->tos == rt_info->tos && skb->mark == rt_info->mark && iph->daddr == rt_info->daddr && iph->saddr == rt_info->saddr)) |
e45f50660 ipv4: Pass struct... |
118 |
return ip_route_me_harder(net, skb, RTN_UNSPEC); |
2cc7d5730 [NETFILTER]: Move... |
119 120 121 |
} return 0; } |
b51655b95 [NET]: Annotate _... |
122 |
__sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook, |
422c346fa [NETFILTER]: Add ... |
123 124 |
unsigned int dataoff, u_int8_t protocol) { |
eddc9ec53 [SK_BUFF]: Introd... |
125 |
const struct iphdr *iph = ip_hdr(skb); |
b51655b95 [NET]: Annotate _... |
126 |
__sum16 csum = 0; |
422c346fa [NETFILTER]: Add ... |
127 128 |
switch (skb->ip_summed) { |
84fa7933a [NET]: Replace CH... |
129 |
case CHECKSUM_COMPLETE: |
6e23ae2a4 [NETFILTER]: Intr... |
130 |
if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN) |
422c346fa [NETFILTER]: Add ... |
131 |
break; |
d3bc23e7e [NET]: Annotate c... |
132 |
if ((protocol == 0 && !csum_fold(skb->csum)) || |
422c346fa [NETFILTER]: Add ... |
133 |
!csum_tcpudp_magic(iph->saddr, iph->daddr, |
e905a9eda [NET] IPV4: Fix w... |
134 |
skb->len - dataoff, protocol, |
422c346fa [NETFILTER]: Add ... |
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
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 ... |
151 |
EXPORT_SYMBOL(nf_ip_checksum); |
d63a65073 [NETFILTER]: Add ... |
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
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... |
168 |
return __skb_checksum_complete_head(skb, dataoff + len); |
d63a65073 [NETFILTER]: Add ... |
169 170 171 |
} return csum; } |
31ad3dd64 netfilter: af_inf... |
172 |
static int nf_ip_route(struct net *net, struct dst_entry **dst, |
0fae2e774 netfilter: af_inf... |
173 |
struct flowi *fl, bool strict __always_unused) |
1841a4c7a [NETFILTER]: nf_c... |
174 |
{ |
31ad3dd64 netfilter: af_inf... |
175 |
struct rtable *rt = ip_route_output_key(net, &fl->u.ip4); |
b23dd4fe4 ipv4: Make output... |
176 177 178 179 |
if (IS_ERR(rt)) return PTR_ERR(rt); *dst = &rt->dst; return 0; |
1841a4c7a [NETFILTER]: nf_c... |
180 |
} |
1e796fda0 [NETFILTER]: cons... |
181 |
static const struct nf_afinfo nf_ip_afinfo = { |
d63a65073 [NETFILTER]: Add ... |
182 183 184 185 186 187 188 |
.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... |
189 |
}; |
ce9f3f31e netfilter: proper... |
190 |
static int __init ipv4_netfilter_init(void) |
2cc7d5730 [NETFILTER]: Move... |
191 |
{ |
bce8032ef [NETFILTER]: Intr... |
192 |
return nf_register_afinfo(&nf_ip_afinfo); |
2cc7d5730 [NETFILTER]: Move... |
193 |
} |
553310600 netfilter: don't ... |
194 |
subsys_initcall(ipv4_netfilter_init); |