blob: 4c0cf63dd92e9ab94bf155c8e0d497c2029cebde [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>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -04008#include <linux/export.h>
Harald Welte020b4c12005-08-09 19:39:00 -07009#include <net/route.h>
Patrick McHardy3e3850e2006-01-06 23:04:54 -080010#include <net/xfrm.h>
11#include <net/ip.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080012#include <net/netfilter/nf_queue.h>
Harald Welte020b4c12005-08-09 19:39:00 -070013
14/* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
Eric Dumazet95c96172012-04-15 05:58:06 +000015int ip_route_me_harder(struct sk_buff *skb, unsigned int addr_type)
Harald Welte020b4c12005-08-09 19:39:00 -070016{
Eric Dumazetadf30902009-06-02 05:19:30 +000017 struct net *net = dev_net(skb_dst(skb)->dev);
Herbert Xu3db05fe2007-10-15 00:53:15 -070018 const struct iphdr *iph = ip_hdr(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070019 struct rtable *rt;
David S. Miller9d6ec932011-03-12 01:12:47 -050020 struct flowi4 fl4 = {};
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000021 __be32 saddr = iph->saddr;
Julian Anastasov797fd392011-08-07 09:11:00 +000022 __u8 flags = skb->sk ? inet_sk_flowi_flags(skb->sk) : 0;
Harald Welte020b4c12005-08-09 19:39:00 -070023 unsigned int hh_len;
24
Julian Anastasov797fd392011-08-07 09:11:00 +000025 if (addr_type == RTN_UNSPEC)
26 addr_type = inet_addr_type(net, saddr);
27 if (addr_type == RTN_LOCAL || addr_type == RTN_UNICAST)
28 flags |= FLOWI_FLAG_ANYSRC;
29 else
30 saddr = 0;
Simon Hormanb4c4ed12006-10-02 16:11:13 -070031
Harald Welte020b4c12005-08-09 19:39:00 -070032 /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
Patrick McHardy6e23ae22007-11-19 18:53:30 -080033 * packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
Harald Welte020b4c12005-08-09 19:39:00 -070034 */
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000035 fl4.daddr = iph->daddr;
36 fl4.saddr = 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;
Julian Anastasov797fd392011-08-07 09:11:00 +000040 fl4.flowi4_flags = flags;
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000041 rt = ip_route_output_key(net, &fl4);
42 if (IS_ERR(rt))
43 return -1;
Harald Welte020b4c12005-08-09 19:39:00 -070044
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000045 /* Drop old route. */
46 skb_dst_drop(skb);
47 skb_dst_set(skb, &rt->dst);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090048
Eric Dumazetadf30902009-06-02 05:19:30 +000049 if (skb_dst(skb)->error)
Harald Welte020b4c12005-08-09 19:39:00 -070050 return -1;
51
Patrick McHardy3e3850e2006-01-06 23:04:54 -080052#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070053 if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
David S. Miller9d6ec932011-03-12 01:12:47 -050054 xfrm_decode_session(skb, flowi4_to_flowi(&fl4), AF_INET) == 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +000055 struct dst_entry *dst = skb_dst(skb);
56 skb_dst_set(skb, NULL);
David S. Miller9d6ec932011-03-12 01:12:47 -050057 dst = xfrm_lookup(net, dst, flowi4_to_flowi(&fl4), skb->sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -080058 if (IS_ERR(dst))
Patrick McHardy3e3850e2006-01-06 23:04:54 -080059 return -1;
Eric Dumazetadf30902009-06-02 05:19:30 +000060 skb_dst_set(skb, dst);
61 }
Patrick McHardy3e3850e2006-01-06 23:04:54 -080062#endif
63
Harald Welte020b4c12005-08-09 19:39:00 -070064 /* Change in oif may mean change in hh_len. */
Eric Dumazetadf30902009-06-02 05:19:30 +000065 hh_len = skb_dst(skb)->dev->hard_header_len;
Herbert Xu3db05fe2007-10-15 00:53:15 -070066 if (skb_headroom(skb) < hh_len &&
Paul Guo5e2afba2011-11-14 19:00:54 +080067 pskb_expand_head(skb, HH_DATA_ALIGN(hh_len - skb_headroom(skb)),
68 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -070069 return -1;
Harald Welte020b4c12005-08-09 19:39:00 -070070
71 return 0;
72}
73EXPORT_SYMBOL(ip_route_me_harder);
Harald Welte2cc7d572005-08-09 19:42:34 -070074
75/*
76 * Extra routing may needed on local out, as the QUEUE target never
77 * returns control to the table.
78 */
79
80struct ip_rt_info {
Al Viro59b8bfd2006-09-28 14:21:07 -070081 __be32 daddr;
82 __be32 saddr;
Harald Welte2cc7d572005-08-09 19:42:34 -070083 u_int8_t tos;
Eric Leblond5f145e42008-11-25 12:15:16 +010084 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070085};
86
Patrick McHardy02f014d2007-12-05 01:26:33 -080087static void nf_ip_saveroute(const struct sk_buff *skb,
88 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070089{
Patrick McHardy02f014d2007-12-05 01:26:33 -080090 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070091
Patrick McHardy02f014d2007-12-05 01:26:33 -080092 if (entry->hook == NF_INET_LOCAL_OUT) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070093 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070094
95 rt_info->tos = iph->tos;
96 rt_info->daddr = iph->daddr;
97 rt_info->saddr = iph->saddr;
Eric Leblond5f145e42008-11-25 12:15:16 +010098 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070099 }
100}
101
Patrick McHardy02f014d2007-12-05 01:26:33 -0800102static int nf_ip_reroute(struct sk_buff *skb,
103 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700104{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800105 const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700106
Patrick McHardy02f014d2007-12-05 01:26:33 -0800107 if (entry->hook == NF_INET_LOCAL_OUT) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700108 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700109
Joe Perchesf64f9e72009-11-29 16:55:45 -0800110 if (!(iph->tos == rt_info->tos &&
111 skb->mark == rt_info->mark &&
112 iph->daddr == rt_info->daddr &&
113 iph->saddr == rt_info->saddr))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700114 return ip_route_me_harder(skb, RTN_UNSPEC);
Harald Welte2cc7d572005-08-09 19:42:34 -0700115 }
116 return 0;
117}
118
Al Virob51655b2006-11-14 21:40:42 -0800119__sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700120 unsigned int dataoff, u_int8_t protocol)
121{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700122 const struct iphdr *iph = ip_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800123 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700124
125 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700126 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800127 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700128 break;
Al Virod3bc23e2006-11-14 21:24:49 -0800129 if ((protocol == 0 && !csum_fold(skb->csum)) ||
Patrick McHardy422c3462006-04-06 14:18:43 -0700130 !csum_tcpudp_magic(iph->saddr, iph->daddr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900131 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700132 skb->csum)) {
133 skb->ip_summed = CHECKSUM_UNNECESSARY;
134 break;
135 }
136 /* fall through */
137 case CHECKSUM_NONE:
138 if (protocol == 0)
139 skb->csum = 0;
140 else
141 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
142 skb->len - dataoff,
143 protocol, 0);
144 csum = __skb_checksum_complete(skb);
145 }
146 return csum;
147}
Patrick McHardy422c3462006-04-06 14:18:43 -0700148EXPORT_SYMBOL(nf_ip_checksum);
149
Patrick McHardyd63a6502008-03-20 15:15:53 +0100150static __sum16 nf_ip_checksum_partial(struct sk_buff *skb, unsigned int hook,
151 unsigned int dataoff, unsigned int len,
152 u_int8_t protocol)
153{
154 const struct iphdr *iph = ip_hdr(skb);
155 __sum16 csum = 0;
156
157 switch (skb->ip_summed) {
158 case CHECKSUM_COMPLETE:
159 if (len == skb->len - dataoff)
160 return nf_ip_checksum(skb, hook, dataoff, protocol);
161 /* fall through */
162 case CHECKSUM_NONE:
163 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, protocol,
164 skb->len - dataoff, 0);
165 skb->ip_summed = CHECKSUM_NONE;
Shan Weic86ee672010-06-14 16:20:02 +0200166 return __skb_checksum_complete_head(skb, dataoff + len);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100167 }
168 return csum;
169}
170
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200171static int nf_ip_route(struct net *net, struct dst_entry **dst,
Florian Westphal0fae2e72011-04-04 17:00:54 +0200172 struct flowi *fl, bool strict __always_unused)
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800173{
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200174 struct rtable *rt = ip_route_output_key(net, &fl->u.ip4);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800175 if (IS_ERR(rt))
176 return PTR_ERR(rt);
177 *dst = &rt->dst;
178 return 0;
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800179}
180
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800181static const struct nf_afinfo nf_ip_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100182 .family = AF_INET,
183 .checksum = nf_ip_checksum,
184 .checksum_partial = nf_ip_checksum_partial,
185 .route = nf_ip_route,
186 .saveroute = nf_ip_saveroute,
187 .reroute = nf_ip_reroute,
188 .route_key_size = sizeof(struct ip_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700189};
190
Jan Beulichce9f3f32012-06-18 00:18:31 +0000191static int __init ipv4_netfilter_init(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700192{
Patrick McHardybce80322006-04-06 14:18:09 -0700193 return nf_register_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700194}
195
Jan Beulichce9f3f32012-06-18 00:18:31 +0000196static void __exit ipv4_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700197{
Patrick McHardybce80322006-04-06 14:18:09 -0700198 nf_unregister_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700199}
200
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800201module_init(ipv4_netfilter_init);
202module_exit(ipv4_netfilter_fini);