blob: 929b27bdeb79829801b4be02f9371d6538a161e8 [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 = {};
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000020 __be32 saddr = iph->saddr;
Julian Anastasov797fd392011-08-07 09:11:00 +000021 __u8 flags = skb->sk ? inet_sk_flowi_flags(skb->sk) : 0;
Harald Welte020b4c12005-08-09 19:39:00 -070022 unsigned int hh_len;
23
Julian Anastasov797fd392011-08-07 09:11:00 +000024 if (addr_type == RTN_UNSPEC)
25 addr_type = inet_addr_type(net, saddr);
26 if (addr_type == RTN_LOCAL || addr_type == RTN_UNICAST)
27 flags |= FLOWI_FLAG_ANYSRC;
28 else
29 saddr = 0;
Simon Hormanb4c4ed12006-10-02 16:11:13 -070030
Harald Welte020b4c12005-08-09 19:39:00 -070031 /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
Patrick McHardy6e23ae22007-11-19 18:53:30 -080032 * packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
Harald Welte020b4c12005-08-09 19:39:00 -070033 */
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000034 fl4.daddr = iph->daddr;
35 fl4.saddr = saddr;
36 fl4.flowi4_tos = RT_TOS(iph->tos);
37 fl4.flowi4_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0;
38 fl4.flowi4_mark = skb->mark;
Julian Anastasov797fd392011-08-07 09:11:00 +000039 fl4.flowi4_flags = flags;
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000040 rt = ip_route_output_key(net, &fl4);
41 if (IS_ERR(rt))
42 return -1;
Harald Welte020b4c12005-08-09 19:39:00 -070043
Julian Anastasoved6e4ef2011-06-18 07:53:59 +000044 /* Drop old route. */
45 skb_dst_drop(skb);
46 skb_dst_set(skb, &rt->dst);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090047
Eric Dumazetadf30902009-06-02 05:19:30 +000048 if (skb_dst(skb)->error)
Harald Welte020b4c12005-08-09 19:39:00 -070049 return -1;
50
Patrick McHardy3e3850e2006-01-06 23:04:54 -080051#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070052 if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
David S. Miller9d6ec932011-03-12 01:12:47 -050053 xfrm_decode_session(skb, flowi4_to_flowi(&fl4), AF_INET) == 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +000054 struct dst_entry *dst = skb_dst(skb);
55 skb_dst_set(skb, NULL);
David S. Miller9d6ec932011-03-12 01:12:47 -050056 dst = xfrm_lookup(net, dst, flowi4_to_flowi(&fl4), skb->sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -080057 if (IS_ERR(dst))
Patrick McHardy3e3850e2006-01-06 23:04:54 -080058 return -1;
Eric Dumazetadf30902009-06-02 05:19:30 +000059 skb_dst_set(skb, dst);
60 }
Patrick McHardy3e3850e2006-01-06 23:04:54 -080061#endif
62
Harald Welte020b4c12005-08-09 19:39:00 -070063 /* Change in oif may mean change in hh_len. */
Eric Dumazetadf30902009-06-02 05:19:30 +000064 hh_len = skb_dst(skb)->dev->hard_header_len;
Herbert Xu3db05fe2007-10-15 00:53:15 -070065 if (skb_headroom(skb) < hh_len &&
66 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -070067 return -1;
Harald Welte020b4c12005-08-09 19:39:00 -070068
69 return 0;
70}
71EXPORT_SYMBOL(ip_route_me_harder);
Harald Welte2cc7d572005-08-09 19:42:34 -070072
Patrick McHardyee68cea2006-02-15 01:34:23 -080073#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070074int ip_xfrm_me_harder(struct sk_buff *skb)
Patrick McHardyee68cea2006-02-15 01:34:23 -080075{
76 struct flowi fl;
77 unsigned int hh_len;
78 struct dst_entry *dst;
79
Herbert Xu3db05fe2007-10-15 00:53:15 -070080 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
Patrick McHardyee68cea2006-02-15 01:34:23 -080081 return 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -070082 if (xfrm_decode_session(skb, &fl, AF_INET) < 0)
Patrick McHardyee68cea2006-02-15 01:34:23 -080083 return -1;
84
Eric Dumazetadf30902009-06-02 05:19:30 +000085 dst = skb_dst(skb);
Patrick McHardyee68cea2006-02-15 01:34:23 -080086 if (dst->xfrm)
87 dst = ((struct xfrm_dst *)dst)->route;
88 dst_hold(dst);
89
David S. Miller452edd52011-03-02 13:27:41 -080090 dst = xfrm_lookup(dev_net(dst->dev), dst, &fl, skb->sk, 0);
91 if (IS_ERR(dst))
Patrick McHardyee68cea2006-02-15 01:34:23 -080092 return -1;
93
Eric Dumazetadf30902009-06-02 05:19:30 +000094 skb_dst_drop(skb);
95 skb_dst_set(skb, dst);
Patrick McHardyee68cea2006-02-15 01:34:23 -080096
97 /* Change in oif may mean change in hh_len. */
Eric Dumazetadf30902009-06-02 05:19:30 +000098 hh_len = skb_dst(skb)->dev->hard_header_len;
Herbert Xu3db05fe2007-10-15 00:53:15 -070099 if (skb_headroom(skb) < hh_len &&
100 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700101 return -1;
Patrick McHardyee68cea2006-02-15 01:34:23 -0800102 return 0;
103}
104EXPORT_SYMBOL(ip_xfrm_me_harder);
105#endif
106
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800107void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
108EXPORT_SYMBOL(ip_nat_decode_session);
109
Harald Welte2cc7d572005-08-09 19:42:34 -0700110/*
111 * Extra routing may needed on local out, as the QUEUE target never
112 * returns control to the table.
113 */
114
115struct ip_rt_info {
Al Viro59b8bfd2006-09-28 14:21:07 -0700116 __be32 daddr;
117 __be32 saddr;
Harald Welte2cc7d572005-08-09 19:42:34 -0700118 u_int8_t tos;
Eric Leblond5f145e42008-11-25 12:15:16 +0100119 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -0700120};
121
Patrick McHardy02f014d2007-12-05 01:26:33 -0800122static void nf_ip_saveroute(const struct sk_buff *skb,
123 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700124{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800125 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700126
Patrick McHardy02f014d2007-12-05 01:26:33 -0800127 if (entry->hook == NF_INET_LOCAL_OUT) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700128 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700129
130 rt_info->tos = iph->tos;
131 rt_info->daddr = iph->daddr;
132 rt_info->saddr = iph->saddr;
Eric Leblond5f145e42008-11-25 12:15:16 +0100133 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -0700134 }
135}
136
Patrick McHardy02f014d2007-12-05 01:26:33 -0800137static int nf_ip_reroute(struct sk_buff *skb,
138 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700139{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800140 const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700141
Patrick McHardy02f014d2007-12-05 01:26:33 -0800142 if (entry->hook == NF_INET_LOCAL_OUT) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700143 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700144
Joe Perchesf64f9e72009-11-29 16:55:45 -0800145 if (!(iph->tos == rt_info->tos &&
146 skb->mark == rt_info->mark &&
147 iph->daddr == rt_info->daddr &&
148 iph->saddr == rt_info->saddr))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700149 return ip_route_me_harder(skb, RTN_UNSPEC);
Harald Welte2cc7d572005-08-09 19:42:34 -0700150 }
151 return 0;
152}
153
Al Virob51655b2006-11-14 21:40:42 -0800154__sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700155 unsigned int dataoff, u_int8_t protocol)
156{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700157 const struct iphdr *iph = ip_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800158 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700159
160 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700161 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800162 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700163 break;
Al Virod3bc23e2006-11-14 21:24:49 -0800164 if ((protocol == 0 && !csum_fold(skb->csum)) ||
Patrick McHardy422c3462006-04-06 14:18:43 -0700165 !csum_tcpudp_magic(iph->saddr, iph->daddr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900166 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700167 skb->csum)) {
168 skb->ip_summed = CHECKSUM_UNNECESSARY;
169 break;
170 }
171 /* fall through */
172 case CHECKSUM_NONE:
173 if (protocol == 0)
174 skb->csum = 0;
175 else
176 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
177 skb->len - dataoff,
178 protocol, 0);
179 csum = __skb_checksum_complete(skb);
180 }
181 return csum;
182}
Patrick McHardy422c3462006-04-06 14:18:43 -0700183EXPORT_SYMBOL(nf_ip_checksum);
184
Patrick McHardyd63a6502008-03-20 15:15:53 +0100185static __sum16 nf_ip_checksum_partial(struct sk_buff *skb, unsigned int hook,
186 unsigned int dataoff, unsigned int len,
187 u_int8_t protocol)
188{
189 const struct iphdr *iph = ip_hdr(skb);
190 __sum16 csum = 0;
191
192 switch (skb->ip_summed) {
193 case CHECKSUM_COMPLETE:
194 if (len == skb->len - dataoff)
195 return nf_ip_checksum(skb, hook, dataoff, protocol);
196 /* fall through */
197 case CHECKSUM_NONE:
198 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, protocol,
199 skb->len - dataoff, 0);
200 skb->ip_summed = CHECKSUM_NONE;
Shan Weic86ee672010-06-14 16:20:02 +0200201 return __skb_checksum_complete_head(skb, dataoff + len);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100202 }
203 return csum;
204}
205
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200206static int nf_ip_route(struct net *net, struct dst_entry **dst,
Florian Westphal0fae2e72011-04-04 17:00:54 +0200207 struct flowi *fl, bool strict __always_unused)
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800208{
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200209 struct rtable *rt = ip_route_output_key(net, &fl->u.ip4);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800210 if (IS_ERR(rt))
211 return PTR_ERR(rt);
212 *dst = &rt->dst;
213 return 0;
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800214}
215
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800216static const struct nf_afinfo nf_ip_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100217 .family = AF_INET,
218 .checksum = nf_ip_checksum,
219 .checksum_partial = nf_ip_checksum_partial,
220 .route = nf_ip_route,
221 .saveroute = nf_ip_saveroute,
222 .reroute = nf_ip_reroute,
223 .route_key_size = sizeof(struct ip_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700224};
225
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800226static int ipv4_netfilter_init(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700227{
Patrick McHardybce80322006-04-06 14:18:09 -0700228 return nf_register_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700229}
230
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800231static void ipv4_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700232{
Patrick McHardybce80322006-04-06 14:18:09 -0700233 nf_unregister_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700234}
235
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800236module_init(ipv4_netfilter_init);
237module_exit(ipv4_netfilter_fini);
Patrick McHardy4f536522008-01-14 23:48:39 -0800238
239#ifdef CONFIG_SYSCTL
240struct ctl_path nf_net_ipv4_netfilter_sysctl_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800241 { .procname = "net", },
242 { .procname = "ipv4", },
243 { .procname = "netfilter", },
Patrick McHardy4f536522008-01-14 23:48:39 -0800244 { }
245};
246EXPORT_SYMBOL_GPL(nf_net_ipv4_netfilter_sysctl_path);
247#endif /* CONFIG_SYSCTL */