blob: f1035f056503ba588c99bb448bfa7dd4efc96e1f [file] [log] [blame]
Harald Welte2cc7d572005-08-09 19:42:34 -07001/* IPv4 specific functions of netfilter core */
Harald Welte020b4c12005-08-09 19:39:00 -07002#include <linux/kernel.h>
3#include <linux/netfilter.h>
Harald Welte2cc7d572005-08-09 19:42:34 -07004#include <linux/netfilter_ipv4.h>
Patrick McHardy3e3850e2006-01-06 23:04:54 -08005#include <linux/ip.h>
Herbert Xu2ca7b0a2007-10-14 00:39:55 -07006#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/gfp.h>
Harald Welte020b4c12005-08-09 19:39:00 -07008#include <net/route.h>
Patrick McHardy3e3850e2006-01-06 23:04:54 -08009#include <net/xfrm.h>
10#include <net/ip.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080011#include <net/netfilter/nf_queue.h>
Harald Welte020b4c12005-08-09 19:39:00 -070012
13/* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
Herbert Xu3db05fe2007-10-15 00:53:15 -070014int ip_route_me_harder(struct sk_buff *skb, unsigned addr_type)
Harald Welte020b4c12005-08-09 19:39:00 -070015{
Eric Dumazetadf30902009-06-02 05:19:30 +000016 struct net *net = dev_net(skb_dst(skb)->dev);
Herbert Xu3db05fe2007-10-15 00:53:15 -070017 const struct iphdr *iph = ip_hdr(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070018 struct rtable *rt;
David S. Miller9d6ec932011-03-12 01:12:47 -050019 struct flowi4 fl4 = {};
Eric Dumazet7fee2262010-05-11 23:19:48 +000020 unsigned long orefdst;
Harald Welte020b4c12005-08-09 19:39:00 -070021 unsigned int hh_len;
Patrick McHardyc68b8b62007-01-04 12:15:34 -080022 unsigned int type;
Harald Welte020b4c12005-08-09 19:39:00 -070023
Alexey Dobriyanb21f8902008-10-08 11:35:03 +020024 type = inet_addr_type(net, iph->saddr);
KOVACS Krisztian86b08d82008-10-01 07:44:42 -070025 if (skb->sk && inet_sk(skb->sk)->transparent)
26 type = RTN_LOCAL;
Simon Hormanb4c4ed12006-10-02 16:11:13 -070027 if (addr_type == RTN_UNSPEC)
Patrick McHardyc68b8b62007-01-04 12:15:34 -080028 addr_type = type;
Simon Hormanb4c4ed12006-10-02 16:11:13 -070029
Harald Welte020b4c12005-08-09 19:39:00 -070030 /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
Patrick McHardy6e23ae22007-11-19 18:53:30 -080031 * packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
Harald Welte020b4c12005-08-09 19:39:00 -070032 */
Simon Hormanb4c4ed12006-10-02 16:11:13 -070033 if (addr_type == RTN_LOCAL) {
David S. Miller9d6ec932011-03-12 01:12:47 -050034 fl4.daddr = iph->daddr;
Patrick McHardyc68b8b62007-01-04 12:15:34 -080035 if (type == RTN_LOCAL)
David S. Miller9d6ec932011-03-12 01:12:47 -050036 fl4.saddr = iph->saddr;
37 fl4.flowi4_tos = RT_TOS(iph->tos);
38 fl4.flowi4_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0;
39 fl4.flowi4_mark = skb->mark;
40 fl4.flowi4_flags = skb->sk ? inet_sk_flowi_flags(skb->sk) : 0;
41 rt = ip_route_output_key(net, &fl4);
David S. Millerb23dd4f2011-03-02 14:31:35 -080042 if (IS_ERR(rt))
Harald Welte020b4c12005-08-09 19:39:00 -070043 return -1;
44
45 /* Drop old route. */
Eric Dumazetadf30902009-06-02 05:19:30 +000046 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -070047 skb_dst_set(skb, &rt->dst);
Harald Welte020b4c12005-08-09 19:39:00 -070048 } else {
49 /* non-local src, find valid iif to satisfy
50 * rp-filter when calling ip_route_input. */
David S. Miller9d6ec932011-03-12 01:12:47 -050051 fl4.daddr = iph->saddr;
52 rt = ip_route_output_key(net, &fl4);
David S. Millerb23dd4f2011-03-02 14:31:35 -080053 if (IS_ERR(rt))
Harald Welte020b4c12005-08-09 19:39:00 -070054 return -1;
55
Eric Dumazet7fee2262010-05-11 23:19:48 +000056 orefdst = skb->_skb_refdst;
Herbert Xu3db05fe2007-10-15 00:53:15 -070057 if (ip_route_input(skb, iph->daddr, iph->saddr,
Changli Gaod8d1f302010-06-10 23:31:35 -070058 RT_TOS(iph->tos), rt->dst.dev) != 0) {
59 dst_release(&rt->dst);
Harald Welte020b4c12005-08-09 19:39:00 -070060 return -1;
61 }
Changli Gaod8d1f302010-06-10 23:31:35 -070062 dst_release(&rt->dst);
Eric Dumazet7fee2262010-05-11 23:19:48 +000063 refdst_drop(orefdst);
Harald Welte020b4c12005-08-09 19:39:00 -070064 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090065
Eric Dumazetadf30902009-06-02 05:19:30 +000066 if (skb_dst(skb)->error)
Harald Welte020b4c12005-08-09 19:39:00 -070067 return -1;
68
Patrick McHardy3e3850e2006-01-06 23:04:54 -080069#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070070 if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
David S. Miller9d6ec932011-03-12 01:12:47 -050071 xfrm_decode_session(skb, flowi4_to_flowi(&fl4), AF_INET) == 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +000072 struct dst_entry *dst = skb_dst(skb);
73 skb_dst_set(skb, NULL);
David S. Miller9d6ec932011-03-12 01:12:47 -050074 dst = xfrm_lookup(net, dst, flowi4_to_flowi(&fl4), skb->sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -080075 if (IS_ERR(dst))
Patrick McHardy3e3850e2006-01-06 23:04:54 -080076 return -1;
Eric Dumazetadf30902009-06-02 05:19:30 +000077 skb_dst_set(skb, dst);
78 }
Patrick McHardy3e3850e2006-01-06 23:04:54 -080079#endif
80
Harald Welte020b4c12005-08-09 19:39:00 -070081 /* Change in oif may mean change in hh_len. */
Eric Dumazetadf30902009-06-02 05:19:30 +000082 hh_len = skb_dst(skb)->dev->hard_header_len;
Herbert Xu3db05fe2007-10-15 00:53:15 -070083 if (skb_headroom(skb) < hh_len &&
84 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -070085 return -1;
Harald Welte020b4c12005-08-09 19:39:00 -070086
87 return 0;
88}
89EXPORT_SYMBOL(ip_route_me_harder);
Harald Welte2cc7d572005-08-09 19:42:34 -070090
Patrick McHardyee68cea2006-02-15 01:34:23 -080091#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070092int ip_xfrm_me_harder(struct sk_buff *skb)
Patrick McHardyee68cea2006-02-15 01:34:23 -080093{
94 struct flowi fl;
95 unsigned int hh_len;
96 struct dst_entry *dst;
97
Herbert Xu3db05fe2007-10-15 00:53:15 -070098 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
Patrick McHardyee68cea2006-02-15 01:34:23 -080099 return 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700100 if (xfrm_decode_session(skb, &fl, AF_INET) < 0)
Patrick McHardyee68cea2006-02-15 01:34:23 -0800101 return -1;
102
Eric Dumazetadf30902009-06-02 05:19:30 +0000103 dst = skb_dst(skb);
Patrick McHardyee68cea2006-02-15 01:34:23 -0800104 if (dst->xfrm)
105 dst = ((struct xfrm_dst *)dst)->route;
106 dst_hold(dst);
107
David S. Miller452edd52011-03-02 13:27:41 -0800108 dst = xfrm_lookup(dev_net(dst->dev), dst, &fl, skb->sk, 0);
109 if (IS_ERR(dst))
Patrick McHardyee68cea2006-02-15 01:34:23 -0800110 return -1;
111
Eric Dumazetadf30902009-06-02 05:19:30 +0000112 skb_dst_drop(skb);
113 skb_dst_set(skb, dst);
Patrick McHardyee68cea2006-02-15 01:34:23 -0800114
115 /* Change in oif may mean change in hh_len. */
Eric Dumazetadf30902009-06-02 05:19:30 +0000116 hh_len = skb_dst(skb)->dev->hard_header_len;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700117 if (skb_headroom(skb) < hh_len &&
118 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700119 return -1;
Patrick McHardyee68cea2006-02-15 01:34:23 -0800120 return 0;
121}
122EXPORT_SYMBOL(ip_xfrm_me_harder);
123#endif
124
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800125void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
126EXPORT_SYMBOL(ip_nat_decode_session);
127
Harald Welte2cc7d572005-08-09 19:42:34 -0700128/*
129 * Extra routing may needed on local out, as the QUEUE target never
130 * returns control to the table.
131 */
132
133struct ip_rt_info {
Al Viro59b8bfd2006-09-28 14:21:07 -0700134 __be32 daddr;
135 __be32 saddr;
Harald Welte2cc7d572005-08-09 19:42:34 -0700136 u_int8_t tos;
Eric Leblond5f145e42008-11-25 12:15:16 +0100137 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -0700138};
139
Patrick McHardy02f014d2007-12-05 01:26:33 -0800140static void nf_ip_saveroute(const struct sk_buff *skb,
141 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700142{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800143 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700144
Patrick McHardy02f014d2007-12-05 01:26:33 -0800145 if (entry->hook == NF_INET_LOCAL_OUT) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700146 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700147
148 rt_info->tos = iph->tos;
149 rt_info->daddr = iph->daddr;
150 rt_info->saddr = iph->saddr;
Eric Leblond5f145e42008-11-25 12:15:16 +0100151 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -0700152 }
153}
154
Patrick McHardy02f014d2007-12-05 01:26:33 -0800155static int nf_ip_reroute(struct sk_buff *skb,
156 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700157{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800158 const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700159
Patrick McHardy02f014d2007-12-05 01:26:33 -0800160 if (entry->hook == NF_INET_LOCAL_OUT) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700161 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700162
Joe Perchesf64f9e72009-11-29 16:55:45 -0800163 if (!(iph->tos == rt_info->tos &&
164 skb->mark == rt_info->mark &&
165 iph->daddr == rt_info->daddr &&
166 iph->saddr == rt_info->saddr))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700167 return ip_route_me_harder(skb, RTN_UNSPEC);
Harald Welte2cc7d572005-08-09 19:42:34 -0700168 }
169 return 0;
170}
171
Al Virob51655b2006-11-14 21:40:42 -0800172__sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700173 unsigned int dataoff, u_int8_t protocol)
174{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700175 const struct iphdr *iph = ip_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800176 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700177
178 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700179 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800180 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700181 break;
Al Virod3bc23e2006-11-14 21:24:49 -0800182 if ((protocol == 0 && !csum_fold(skb->csum)) ||
Patrick McHardy422c3462006-04-06 14:18:43 -0700183 !csum_tcpudp_magic(iph->saddr, iph->daddr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900184 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700185 skb->csum)) {
186 skb->ip_summed = CHECKSUM_UNNECESSARY;
187 break;
188 }
189 /* fall through */
190 case CHECKSUM_NONE:
191 if (protocol == 0)
192 skb->csum = 0;
193 else
194 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
195 skb->len - dataoff,
196 protocol, 0);
197 csum = __skb_checksum_complete(skb);
198 }
199 return csum;
200}
Patrick McHardy422c3462006-04-06 14:18:43 -0700201EXPORT_SYMBOL(nf_ip_checksum);
202
Patrick McHardyd63a6502008-03-20 15:15:53 +0100203static __sum16 nf_ip_checksum_partial(struct sk_buff *skb, unsigned int hook,
204 unsigned int dataoff, unsigned int len,
205 u_int8_t protocol)
206{
207 const struct iphdr *iph = ip_hdr(skb);
208 __sum16 csum = 0;
209
210 switch (skb->ip_summed) {
211 case CHECKSUM_COMPLETE:
212 if (len == skb->len - dataoff)
213 return nf_ip_checksum(skb, hook, dataoff, protocol);
214 /* fall through */
215 case CHECKSUM_NONE:
216 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, protocol,
217 skb->len - dataoff, 0);
218 skb->ip_summed = CHECKSUM_NONE;
Shan Weic86ee672010-06-14 16:20:02 +0200219 return __skb_checksum_complete_head(skb, dataoff + len);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100220 }
221 return csum;
222}
223
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200224static int nf_ip_route(struct net *net, struct dst_entry **dst,
225 struct flowi *fl)
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800226{
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200227 struct rtable *rt = ip_route_output_key(net, &fl->u.ip4);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800228 if (IS_ERR(rt))
229 return PTR_ERR(rt);
230 *dst = &rt->dst;
231 return 0;
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800232}
233
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800234static const struct nf_afinfo nf_ip_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100235 .family = AF_INET,
236 .checksum = nf_ip_checksum,
237 .checksum_partial = nf_ip_checksum_partial,
238 .route = nf_ip_route,
239 .saveroute = nf_ip_saveroute,
240 .reroute = nf_ip_reroute,
241 .route_key_size = sizeof(struct ip_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700242};
243
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800244static int ipv4_netfilter_init(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700245{
Patrick McHardybce80322006-04-06 14:18:09 -0700246 return nf_register_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700247}
248
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800249static void ipv4_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700250{
Patrick McHardybce80322006-04-06 14:18:09 -0700251 nf_unregister_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700252}
253
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800254module_init(ipv4_netfilter_init);
255module_exit(ipv4_netfilter_fini);
Patrick McHardy4f536522008-01-14 23:48:39 -0800256
257#ifdef CONFIG_SYSCTL
258struct ctl_path nf_net_ipv4_netfilter_sysctl_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800259 { .procname = "net", },
260 { .procname = "ipv4", },
261 { .procname = "netfilter", },
Patrick McHardy4f536522008-01-14 23:48:39 -0800262 { }
263};
264EXPORT_SYMBOL_GPL(nf_net_ipv4_netfilter_sysctl_path);
265#endif /* CONFIG_SYSCTL */