blob: c0cc6aa8cfaa9c3b1988d176e6a781e2a2e0b7c7 [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 W. Biedermane45f5062015-09-25 15:07:30 -050020int ip_route_me_harder(struct net *net, struct sk_buff *skb, unsigned int addr_type)
Harald Welte020b4c12005-08-09 19:39:00 -070021{
Herbert Xu3db05fe2007-10-15 00:53:15 -070022 const struct iphdr *iph = ip_hdr(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070023 struct rtable *rt;
David S. Miller9d6ec932011-03-12 01:12:47 -050024 struct flowi4 fl4 = {};
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000025 __be32 saddr = iph->saddr;
Florian Westphal42114422017-02-17 08:39:28 +010026 const struct sock *sk = skb_to_full_sk(skb);
27 __u8 flags = sk ? inet_sk_flowi_flags(sk) : 0;
David Ahern6d8b49c2016-11-09 10:24:40 -080028 struct net_device *dev = skb_dst(skb)->dev;
Harald Welte020b4c12005-08-09 19:39:00 -070029 unsigned int hh_len;
30
Julian Anastasov797fd392011-08-07 09:11:00 +000031 if (addr_type == RTN_UNSPEC)
David Ahern6d8b49c2016-11-09 10:24:40 -080032 addr_type = inet_addr_type_dev_table(net, dev, saddr);
Julian Anastasov797fd392011-08-07 09:11:00 +000033 if (addr_type == RTN_LOCAL || addr_type == RTN_UNICAST)
34 flags |= FLOWI_FLAG_ANYSRC;
35 else
36 saddr = 0;
Simon Hormanb4c4ed12006-10-02 16:11:13 -070037
Harald Welte020b4c12005-08-09 19:39:00 -070038 /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
Patrick McHardy6e23ae22007-11-19 18:53:30 -080039 * packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
Harald Welte020b4c12005-08-09 19:39:00 -070040 */
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000041 fl4.daddr = iph->daddr;
42 fl4.saddr = saddr;
43 fl4.flowi4_tos = RT_TOS(iph->tos);
Florian Westphal42114422017-02-17 08:39:28 +010044 fl4.flowi4_oif = sk ? sk->sk_bound_dev_if : 0;
David Ahern6d8b49c2016-11-09 10:24:40 -080045 if (!fl4.flowi4_oif)
46 fl4.flowi4_oif = l3mdev_master_ifindex(dev);
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000047 fl4.flowi4_mark = skb->mark;
Julian Anastasov797fd392011-08-07 09:11:00 +000048 fl4.flowi4_flags = flags;
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000049 rt = ip_route_output_key(net, &fl4);
50 if (IS_ERR(rt))
Patrick McHardyc9e16732013-04-05 06:41:10 +000051 return PTR_ERR(rt);
Harald Welte020b4c12005-08-09 19:39:00 -070052
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000053 /* Drop old route. */
54 skb_dst_drop(skb);
55 skb_dst_set(skb, &rt->dst);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090056
Eric Dumazetadf30902009-06-02 05:19:30 +000057 if (skb_dst(skb)->error)
Patrick McHardyc9e16732013-04-05 06:41:10 +000058 return skb_dst(skb)->error;
Harald Welte020b4c12005-08-09 19:39:00 -070059
Patrick McHardy3e3850e2006-01-06 23:04:54 -080060#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070061 if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
David S. Miller9d6ec932011-03-12 01:12:47 -050062 xfrm_decode_session(skb, flowi4_to_flowi(&fl4), AF_INET) == 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +000063 struct dst_entry *dst = skb_dst(skb);
64 skb_dst_set(skb, NULL);
Florian Westphal42114422017-02-17 08:39:28 +010065 dst = xfrm_lookup(net, dst, flowi4_to_flowi(&fl4), sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -080066 if (IS_ERR(dst))
stephen hemminger3d0a0622014-01-15 08:12:50 -080067 return PTR_ERR(dst);
Eric Dumazetadf30902009-06-02 05:19:30 +000068 skb_dst_set(skb, dst);
69 }
Patrick McHardy3e3850e2006-01-06 23:04:54 -080070#endif
71
Harald Welte020b4c12005-08-09 19:39:00 -070072 /* Change in oif may mean change in hh_len. */
Eric Dumazetadf30902009-06-02 05:19:30 +000073 hh_len = skb_dst(skb)->dev->hard_header_len;
Herbert Xu3db05fe2007-10-15 00:53:15 -070074 if (skb_headroom(skb) < hh_len &&
Paul Guo5e2afba2011-11-14 19:00:54 +080075 pskb_expand_head(skb, HH_DATA_ALIGN(hh_len - skb_headroom(skb)),
76 0, GFP_ATOMIC))
Patrick McHardyc9e16732013-04-05 06:41:10 +000077 return -ENOMEM;
Harald Welte020b4c12005-08-09 19:39:00 -070078
79 return 0;
80}
81EXPORT_SYMBOL(ip_route_me_harder);
Harald Welte2cc7d572005-08-09 19:42:34 -070082
83/*
84 * Extra routing may needed on local out, as the QUEUE target never
85 * returns control to the table.
86 */
87
88struct ip_rt_info {
Al Viro59b8bfd2006-09-28 14:21:07 -070089 __be32 daddr;
90 __be32 saddr;
Harald Welte2cc7d572005-08-09 19:42:34 -070091 u_int8_t tos;
Eric Leblond5f145e42008-11-25 12:15:16 +010092 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070093};
94
Patrick McHardy02f014d2007-12-05 01:26:33 -080095static void nf_ip_saveroute(const struct sk_buff *skb,
96 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070097{
Patrick McHardy02f014d2007-12-05 01:26:33 -080098 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070099
David S. Miller1d1de892015-04-03 16:31:01 -0400100 if (entry->state.hook == NF_INET_LOCAL_OUT) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700101 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700102
103 rt_info->tos = iph->tos;
104 rt_info->daddr = iph->daddr;
105 rt_info->saddr = iph->saddr;
Eric Leblond5f145e42008-11-25 12:15:16 +0100106 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -0700107 }
108}
109
Eric W. Biedermand815d902015-09-25 15:07:28 -0500110static int nf_ip_reroute(struct net *net, struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800111 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700112{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800113 const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700114
David S. Miller1d1de892015-04-03 16:31:01 -0400115 if (entry->state.hook == NF_INET_LOCAL_OUT) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700116 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700117
Joe Perchesf64f9e72009-11-29 16:55:45 -0800118 if (!(iph->tos == rt_info->tos &&
119 skb->mark == rt_info->mark &&
120 iph->daddr == rt_info->daddr &&
121 iph->saddr == rt_info->saddr))
Eric W. Biedermane45f5062015-09-25 15:07:30 -0500122 return ip_route_me_harder(net, skb, RTN_UNSPEC);
Harald Welte2cc7d572005-08-09 19:42:34 -0700123 }
124 return 0;
125}
126
Al Virob51655b2006-11-14 21:40:42 -0800127__sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700128 unsigned int dataoff, u_int8_t protocol)
129{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700130 const struct iphdr *iph = ip_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800131 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700132
133 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700134 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800135 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700136 break;
Al Virod3bc23e2006-11-14 21:24:49 -0800137 if ((protocol == 0 && !csum_fold(skb->csum)) ||
Patrick McHardy422c3462006-04-06 14:18:43 -0700138 !csum_tcpudp_magic(iph->saddr, iph->daddr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900139 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700140 skb->csum)) {
141 skb->ip_summed = CHECKSUM_UNNECESSARY;
142 break;
143 }
144 /* fall through */
145 case CHECKSUM_NONE:
146 if (protocol == 0)
147 skb->csum = 0;
148 else
149 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
150 skb->len - dataoff,
151 protocol, 0);
152 csum = __skb_checksum_complete(skb);
153 }
154 return csum;
155}
Patrick McHardy422c3462006-04-06 14:18:43 -0700156EXPORT_SYMBOL(nf_ip_checksum);
157
Patrick McHardyd63a6502008-03-20 15:15:53 +0100158static __sum16 nf_ip_checksum_partial(struct sk_buff *skb, unsigned int hook,
159 unsigned int dataoff, unsigned int len,
160 u_int8_t protocol)
161{
162 const struct iphdr *iph = ip_hdr(skb);
163 __sum16 csum = 0;
164
165 switch (skb->ip_summed) {
166 case CHECKSUM_COMPLETE:
167 if (len == skb->len - dataoff)
168 return nf_ip_checksum(skb, hook, dataoff, protocol);
169 /* fall through */
170 case CHECKSUM_NONE:
171 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, protocol,
172 skb->len - dataoff, 0);
173 skb->ip_summed = CHECKSUM_NONE;
Shan Weic86ee672010-06-14 16:20:02 +0200174 return __skb_checksum_complete_head(skb, dataoff + len);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100175 }
176 return csum;
177}
178
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200179static int nf_ip_route(struct net *net, struct dst_entry **dst,
Florian Westphal0fae2e72011-04-04 17:00:54 +0200180 struct flowi *fl, bool strict __always_unused)
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800181{
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200182 struct rtable *rt = ip_route_output_key(net, &fl->u.ip4);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800183 if (IS_ERR(rt))
184 return PTR_ERR(rt);
185 *dst = &rt->dst;
186 return 0;
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800187}
188
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800189static const struct nf_afinfo nf_ip_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100190 .family = AF_INET,
191 .checksum = nf_ip_checksum,
192 .checksum_partial = nf_ip_checksum_partial,
193 .route = nf_ip_route,
194 .saveroute = nf_ip_saveroute,
195 .reroute = nf_ip_reroute,
196 .route_key_size = sizeof(struct ip_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700197};
198
Jan Beulichce9f3f32012-06-18 00:18:31 +0000199static int __init ipv4_netfilter_init(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700200{
Patrick McHardybce80322006-04-06 14:18:09 -0700201 return nf_register_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700202}
Paul Gortmaker55331062015-05-01 20:08:20 -0400203subsys_initcall(ipv4_netfilter_init);