]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/netfilter.c
[NETFILTER]: xt_hashlimit: remove ip6tables module dependency
[net-next-2.6.git] / net / ipv4 / netfilter.c
CommitLineData
2cc7d573 1/* IPv4 specific functions of netfilter core */
020b4c12
HW
2#include <linux/kernel.h>
3#include <linux/netfilter.h>
2cc7d573 4#include <linux/netfilter_ipv4.h>
3e3850e9 5#include <linux/ip.h>
2ca7b0ac 6#include <linux/skbuff.h>
020b4c12 7#include <net/route.h>
3e3850e9
PM
8#include <net/xfrm.h>
9#include <net/ip.h>
020b4c12
HW
10
11/* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
3db05fea 12int ip_route_me_harder(struct sk_buff *skb, unsigned addr_type)
020b4c12 13{
3db05fea 14 const struct iphdr *iph = ip_hdr(skb);
020b4c12
HW
15 struct rtable *rt;
16 struct flowi fl = {};
17 struct dst_entry *odst;
18 unsigned int hh_len;
c68b8b68 19 unsigned int type;
020b4c12 20
c68b8b68 21 type = inet_addr_type(iph->saddr);
b4c4ed17 22 if (addr_type == RTN_UNSPEC)
c68b8b68 23 addr_type = type;
b4c4ed17 24
020b4c12 25 /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
6e23ae2a 26 * packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
020b4c12 27 */
b4c4ed17 28 if (addr_type == RTN_LOCAL) {
020b4c12 29 fl.nl_u.ip4_u.daddr = iph->daddr;
c68b8b68
PM
30 if (type == RTN_LOCAL)
31 fl.nl_u.ip4_u.saddr = iph->saddr;
020b4c12 32 fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
3db05fea
HX
33 fl.oif = skb->sk ? skb->sk->sk_bound_dev_if : 0;
34 fl.mark = skb->mark;
020b4c12
HW
35 if (ip_route_output_key(&rt, &fl) != 0)
36 return -1;
37
38 /* Drop old route. */
3db05fea
HX
39 dst_release(skb->dst);
40 skb->dst = &rt->u.dst;
020b4c12
HW
41 } else {
42 /* non-local src, find valid iif to satisfy
43 * rp-filter when calling ip_route_input. */
44 fl.nl_u.ip4_u.daddr = iph->saddr;
45 if (ip_route_output_key(&rt, &fl) != 0)
46 return -1;
47
3db05fea
HX
48 odst = skb->dst;
49 if (ip_route_input(skb, iph->daddr, iph->saddr,
020b4c12
HW
50 RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
51 dst_release(&rt->u.dst);
52 return -1;
53 }
54 dst_release(&rt->u.dst);
55 dst_release(odst);
56 }
e905a9ed 57
3db05fea 58 if (skb->dst->error)
020b4c12
HW
59 return -1;
60
3e3850e9 61#ifdef CONFIG_XFRM
3db05fea
HX
62 if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
63 xfrm_decode_session(skb, &fl, AF_INET) == 0)
64 if (xfrm_lookup(&skb->dst, &fl, skb->sk, 0))
3e3850e9
PM
65 return -1;
66#endif
67
020b4c12 68 /* Change in oif may mean change in hh_len. */
3db05fea
HX
69 hh_len = skb->dst->dev->hard_header_len;
70 if (skb_headroom(skb) < hh_len &&
71 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
2ca7b0ac 72 return -1;
020b4c12
HW
73
74 return 0;
75}
76EXPORT_SYMBOL(ip_route_me_harder);
2cc7d573 77
ee68cea2 78#ifdef CONFIG_XFRM
3db05fea 79int ip_xfrm_me_harder(struct sk_buff *skb)
ee68cea2
PM
80{
81 struct flowi fl;
82 unsigned int hh_len;
83 struct dst_entry *dst;
84
3db05fea 85 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
ee68cea2 86 return 0;
3db05fea 87 if (xfrm_decode_session(skb, &fl, AF_INET) < 0)
ee68cea2
PM
88 return -1;
89
3db05fea 90 dst = skb->dst;
ee68cea2
PM
91 if (dst->xfrm)
92 dst = ((struct xfrm_dst *)dst)->route;
93 dst_hold(dst);
94
3db05fea 95 if (xfrm_lookup(&dst, &fl, skb->sk, 0) < 0)
ee68cea2
PM
96 return -1;
97
3db05fea
HX
98 dst_release(skb->dst);
99 skb->dst = dst;
ee68cea2
PM
100
101 /* Change in oif may mean change in hh_len. */
3db05fea
HX
102 hh_len = skb->dst->dev->hard_header_len;
103 if (skb_headroom(skb) < hh_len &&
104 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
2ca7b0ac 105 return -1;
ee68cea2
PM
106 return 0;
107}
108EXPORT_SYMBOL(ip_xfrm_me_harder);
109#endif
110
eb9c7ebe
PM
111void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
112EXPORT_SYMBOL(ip_nat_decode_session);
113
2cc7d573
HW
114/*
115 * Extra routing may needed on local out, as the QUEUE target never
116 * returns control to the table.
117 */
118
119struct ip_rt_info {
59b8bfd8
AV
120 __be32 daddr;
121 __be32 saddr;
2cc7d573
HW
122 u_int8_t tos;
123};
124
bce8032e 125static void nf_ip_saveroute(const struct sk_buff *skb, struct nf_info *info)
2cc7d573
HW
126{
127 struct ip_rt_info *rt_info = nf_info_reroute(info);
128
6e23ae2a 129 if (info->hook == NF_INET_LOCAL_OUT) {
eddc9ec5 130 const struct iphdr *iph = ip_hdr(skb);
2cc7d573
HW
131
132 rt_info->tos = iph->tos;
133 rt_info->daddr = iph->daddr;
134 rt_info->saddr = iph->saddr;
135 }
136}
137
3db05fea 138static int nf_ip_reroute(struct sk_buff *skb, const struct nf_info *info)
2cc7d573
HW
139{
140 const struct ip_rt_info *rt_info = nf_info_reroute(info);
141
6e23ae2a 142 if (info->hook == NF_INET_LOCAL_OUT) {
3db05fea 143 const struct iphdr *iph = ip_hdr(skb);
2cc7d573
HW
144
145 if (!(iph->tos == rt_info->tos
146 && iph->daddr == rt_info->daddr
147 && iph->saddr == rt_info->saddr))
3db05fea 148 return ip_route_me_harder(skb, RTN_UNSPEC);
2cc7d573
HW
149 }
150 return 0;
151}
152
b51655b9 153__sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
422c346f
PM
154 unsigned int dataoff, u_int8_t protocol)
155{
eddc9ec5 156 const struct iphdr *iph = ip_hdr(skb);
b51655b9 157 __sum16 csum = 0;
422c346f
PM
158
159 switch (skb->ip_summed) {
84fa7933 160 case CHECKSUM_COMPLETE:
6e23ae2a 161 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
422c346f 162 break;
d3bc23e7 163 if ((protocol == 0 && !csum_fold(skb->csum)) ||
422c346f 164 !csum_tcpudp_magic(iph->saddr, iph->daddr,
e905a9ed 165 skb->len - dataoff, protocol,
422c346f
PM
166 skb->csum)) {
167 skb->ip_summed = CHECKSUM_UNNECESSARY;
168 break;
169 }
170 /* fall through */
171 case CHECKSUM_NONE:
172 if (protocol == 0)
173 skb->csum = 0;
174 else
175 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
176 skb->len - dataoff,
177 protocol, 0);
178 csum = __skb_checksum_complete(skb);
179 }
180 return csum;
181}
182
183EXPORT_SYMBOL(nf_ip_checksum);
184
bce8032e
PM
185static struct nf_afinfo nf_ip_afinfo = {
186 .family = AF_INET,
422c346f 187 .checksum = nf_ip_checksum,
bce8032e
PM
188 .saveroute = nf_ip_saveroute,
189 .reroute = nf_ip_reroute,
190 .route_key_size = sizeof(struct ip_rt_info),
2cc7d573
HW
191};
192
65b4b4e8 193static int ipv4_netfilter_init(void)
2cc7d573 194{
bce8032e 195 return nf_register_afinfo(&nf_ip_afinfo);
2cc7d573
HW
196}
197
65b4b4e8 198static void ipv4_netfilter_fini(void)
2cc7d573 199{
bce8032e 200 nf_unregister_afinfo(&nf_ip_afinfo);
2cc7d573
HW
201}
202
65b4b4e8
AM
203module_init(ipv4_netfilter_init);
204module_exit(ipv4_netfilter_fini);