blob: 65de0684e22a17862663096da407eee16bc33a31 [file] [log] [blame]
Patrick McHardyf229f6c2013-04-06 15:24:29 +02001/*
2 * IPv4 specific functions of netfilter core
3 *
4 * Rusty Russell (C) 2000 -- This code is GPL.
5 * Patrick McHardy (C) 2006-2012
6 */
Harald Welte020b4c12005-08-09 19:39:00 -07007#include <linux/kernel.h>
8#include <linux/netfilter.h>
Harald Welte2cc7d572005-08-09 19:42:34 -07009#include <linux/netfilter_ipv4.h>
Patrick McHardy3e3850e2006-01-06 23:04:54 -080010#include <linux/ip.h>
Herbert Xu2ca7b0a2007-10-14 00:39:55 -070011#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040013#include <linux/export.h>
Harald Welte020b4c12005-08-09 19:39:00 -070014#include <net/route.h>
Patrick McHardy3e3850e2006-01-06 23:04:54 -080015#include <net/xfrm.h>
16#include <net/ip.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080017#include <net/netfilter/nf_queue.h>
Harald Welte020b4c12005-08-09 19:39:00 -070018
19/* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
Eric Dumazet95c96172012-04-15 05:58:06 +000020int ip_route_me_harder(struct sk_buff *skb, unsigned int addr_type)
Harald Welte020b4c12005-08-09 19:39:00 -070021{
Eric Dumazetadf30902009-06-02 05:19:30 +000022 struct net *net = dev_net(skb_dst(skb)->dev);
Herbert Xu3db05fe2007-10-15 00:53:15 -070023 const struct iphdr *iph = ip_hdr(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070024 struct rtable *rt;
David S. Miller9d6ec932011-03-12 01:12:47 -050025 struct flowi4 fl4 = {};
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000026 __be32 saddr = iph->saddr;
Julian Anastasov797fd392011-08-07 09:11:00 +000027 __u8 flags = skb->sk ? inet_sk_flowi_flags(skb->sk) : 0;
Harald Welte020b4c12005-08-09 19:39:00 -070028 unsigned int hh_len;
29
Julian Anastasov797fd392011-08-07 09:11:00 +000030 if (addr_type == RTN_UNSPEC)
31 addr_type = inet_addr_type(net, saddr);
32 if (addr_type == RTN_LOCAL || addr_type == RTN_UNICAST)
33 flags |= FLOWI_FLAG_ANYSRC;
34 else
35 saddr = 0;
Simon Hormanb4c4ed12006-10-02 16:11:13 -070036
Harald Welte020b4c12005-08-09 19:39:00 -070037 /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
Patrick McHardy6e23ae22007-11-19 18:53:30 -080038 * packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
Harald Welte020b4c12005-08-09 19:39:00 -070039 */
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000040 fl4.daddr = iph->daddr;
41 fl4.saddr = saddr;
42 fl4.flowi4_tos = RT_TOS(iph->tos);
43 fl4.flowi4_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0;
44 fl4.flowi4_mark = skb->mark;
Julian Anastasov797fd392011-08-07 09:11:00 +000045 fl4.flowi4_flags = flags;
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000046 rt = ip_route_output_key(net, &fl4);
47 if (IS_ERR(rt))
Patrick McHardyc9e16732013-04-05 06:41:10 +000048 return PTR_ERR(rt);
Harald Welte020b4c12005-08-09 19:39:00 -070049
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000050 /* Drop old route. */
51 skb_dst_drop(skb);
52 skb_dst_set(skb, &rt->dst);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090053
Eric Dumazetadf30902009-06-02 05:19:30 +000054 if (skb_dst(skb)->error)
Patrick McHardyc9e16732013-04-05 06:41:10 +000055 return skb_dst(skb)->error;
Harald Welte020b4c12005-08-09 19:39:00 -070056
Patrick McHardy3e3850e2006-01-06 23:04:54 -080057#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070058 if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
David S. Miller9d6ec932011-03-12 01:12:47 -050059 xfrm_decode_session(skb, flowi4_to_flowi(&fl4), AF_INET) == 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +000060 struct dst_entry *dst = skb_dst(skb);
61 skb_dst_set(skb, NULL);
David S. Miller9d6ec932011-03-12 01:12:47 -050062 dst = xfrm_lookup(net, dst, flowi4_to_flowi(&fl4), skb->sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -080063 if (IS_ERR(dst))
stephen hemminger3d0a0622014-01-15 08:12:50 -080064 return PTR_ERR(dst);
Eric Dumazetadf30902009-06-02 05:19:30 +000065 skb_dst_set(skb, dst);
66 }
Patrick McHardy3e3850e2006-01-06 23:04:54 -080067#endif
68
Harald Welte020b4c12005-08-09 19:39:00 -070069 /* Change in oif may mean change in hh_len. */
Eric Dumazetadf30902009-06-02 05:19:30 +000070 hh_len = skb_dst(skb)->dev->hard_header_len;
Herbert Xu3db05fe2007-10-15 00:53:15 -070071 if (skb_headroom(skb) < hh_len &&
Paul Guo5e2afba2011-11-14 19:00:54 +080072 pskb_expand_head(skb, HH_DATA_ALIGN(hh_len - skb_headroom(skb)),
73 0, GFP_ATOMIC))
Patrick McHardyc9e16732013-04-05 06:41:10 +000074 return -ENOMEM;
Harald Welte020b4c12005-08-09 19:39:00 -070075
76 return 0;
77}
78EXPORT_SYMBOL(ip_route_me_harder);
Harald Welte2cc7d572005-08-09 19:42:34 -070079
80/*
81 * Extra routing may needed on local out, as the QUEUE target never
82 * returns control to the table.
83 */
84
85struct ip_rt_info {
Al Viro59b8bfd2006-09-28 14:21:07 -070086 __be32 daddr;
87 __be32 saddr;
Harald Welte2cc7d572005-08-09 19:42:34 -070088 u_int8_t tos;
Eric Leblond5f145e42008-11-25 12:15:16 +010089 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070090};
91
Patrick McHardy02f014d2007-12-05 01:26:33 -080092static void nf_ip_saveroute(const struct sk_buff *skb,
93 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070094{
Patrick McHardy02f014d2007-12-05 01:26:33 -080095 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070096
David S. Miller1d1de892015-04-03 16:31:01 -040097 if (entry->state.hook == NF_INET_LOCAL_OUT) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070098 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070099
100 rt_info->tos = iph->tos;
101 rt_info->daddr = iph->daddr;
102 rt_info->saddr = iph->saddr;
Eric Leblond5f145e42008-11-25 12:15:16 +0100103 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -0700104 }
105}
106
Patrick McHardy02f014d2007-12-05 01:26:33 -0800107static int nf_ip_reroute(struct sk_buff *skb,
108 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700109{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800110 const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700111
David S. Miller1d1de892015-04-03 16:31:01 -0400112 if (entry->state.hook == NF_INET_LOCAL_OUT) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700113 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700114
Joe Perchesf64f9e72009-11-29 16:55:45 -0800115 if (!(iph->tos == rt_info->tos &&
116 skb->mark == rt_info->mark &&
117 iph->daddr == rt_info->daddr &&
118 iph->saddr == rt_info->saddr))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700119 return ip_route_me_harder(skb, RTN_UNSPEC);
Harald Welte2cc7d572005-08-09 19:42:34 -0700120 }
121 return 0;
122}
123
Al Virob51655b2006-11-14 21:40:42 -0800124__sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700125 unsigned int dataoff, u_int8_t protocol)
126{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700127 const struct iphdr *iph = ip_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800128 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700129
130 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700131 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800132 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700133 break;
Al Virod3bc23e2006-11-14 21:24:49 -0800134 if ((protocol == 0 && !csum_fold(skb->csum)) ||
Patrick McHardy422c3462006-04-06 14:18:43 -0700135 !csum_tcpudp_magic(iph->saddr, iph->daddr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900136 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700137 skb->csum)) {
138 skb->ip_summed = CHECKSUM_UNNECESSARY;
139 break;
140 }
141 /* fall through */
142 case CHECKSUM_NONE:
143 if (protocol == 0)
144 skb->csum = 0;
145 else
146 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
147 skb->len - dataoff,
148 protocol, 0);
149 csum = __skb_checksum_complete(skb);
150 }
151 return csum;
152}
Patrick McHardy422c3462006-04-06 14:18:43 -0700153EXPORT_SYMBOL(nf_ip_checksum);
154
Patrick McHardyd63a6502008-03-20 15:15:53 +0100155static __sum16 nf_ip_checksum_partial(struct sk_buff *skb, unsigned int hook,
156 unsigned int dataoff, unsigned int len,
157 u_int8_t protocol)
158{
159 const struct iphdr *iph = ip_hdr(skb);
160 __sum16 csum = 0;
161
162 switch (skb->ip_summed) {
163 case CHECKSUM_COMPLETE:
164 if (len == skb->len - dataoff)
165 return nf_ip_checksum(skb, hook, dataoff, protocol);
166 /* fall through */
167 case CHECKSUM_NONE:
168 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, protocol,
169 skb->len - dataoff, 0);
170 skb->ip_summed = CHECKSUM_NONE;
Shan Weic86ee672010-06-14 16:20:02 +0200171 return __skb_checksum_complete_head(skb, dataoff + len);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100172 }
173 return csum;
174}
175
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200176static int nf_ip_route(struct net *net, struct dst_entry **dst,
Florian Westphal0fae2e72011-04-04 17:00:54 +0200177 struct flowi *fl, bool strict __always_unused)
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800178{
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200179 struct rtable *rt = ip_route_output_key(net, &fl->u.ip4);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800180 if (IS_ERR(rt))
181 return PTR_ERR(rt);
182 *dst = &rt->dst;
183 return 0;
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800184}
185
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800186static const struct nf_afinfo nf_ip_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100187 .family = AF_INET,
188 .checksum = nf_ip_checksum,
189 .checksum_partial = nf_ip_checksum_partial,
190 .route = nf_ip_route,
191 .saveroute = nf_ip_saveroute,
192 .reroute = nf_ip_reroute,
193 .route_key_size = sizeof(struct ip_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700194};
195
Jan Beulichce9f3f32012-06-18 00:18:31 +0000196static int __init ipv4_netfilter_init(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700197{
Patrick McHardybce80322006-04-06 14:18:09 -0700198 return nf_register_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700199}
200
Jan Beulichce9f3f32012-06-18 00:18:31 +0000201static void __exit ipv4_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700202{
Patrick McHardybce80322006-04-06 14:18:09 -0700203 nf_unregister_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700204}
205
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800206module_init(ipv4_netfilter_init);
207module_exit(ipv4_netfilter_fini);