blob: 82fb43c5c59ef9cf058646516521406437b70b87 [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;
19 struct flowi fl = {};
20 struct dst_entry *odst;
21 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) {
Harald Welte020b4c12005-08-09 19:39:00 -070034 fl.nl_u.ip4_u.daddr = iph->daddr;
Patrick McHardyc68b8b62007-01-04 12:15:34 -080035 if (type == RTN_LOCAL)
36 fl.nl_u.ip4_u.saddr = iph->saddr;
Harald Welte020b4c12005-08-09 19:39:00 -070037 fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
Herbert Xu3db05fe2007-10-15 00:53:15 -070038 fl.oif = skb->sk ? skb->sk->sk_bound_dev_if : 0;
39 fl.mark = skb->mark;
KOVACS Krisztian86b08d82008-10-01 07:44:42 -070040 fl.flags = skb->sk ? inet_sk_flowi_flags(skb->sk) : 0;
Alexey Dobriyanb21f8902008-10-08 11:35:03 +020041 if (ip_route_output_key(net, &rt, &fl) != 0)
Harald Welte020b4c12005-08-09 19:39:00 -070042 return -1;
43
44 /* Drop old route. */
Eric Dumazetadf30902009-06-02 05:19:30 +000045 skb_dst_drop(skb);
46 skb_dst_set(skb, &rt->u.dst);
Harald Welte020b4c12005-08-09 19:39:00 -070047 } else {
48 /* non-local src, find valid iif to satisfy
49 * rp-filter when calling ip_route_input. */
50 fl.nl_u.ip4_u.daddr = iph->saddr;
Alexey Dobriyanb21f8902008-10-08 11:35:03 +020051 if (ip_route_output_key(net, &rt, &fl) != 0)
Harald Welte020b4c12005-08-09 19:39:00 -070052 return -1;
53
Eric Dumazetadf30902009-06-02 05:19:30 +000054 odst = skb_dst(skb);
Herbert Xu3db05fe2007-10-15 00:53:15 -070055 if (ip_route_input(skb, iph->daddr, iph->saddr,
Harald Welte020b4c12005-08-09 19:39:00 -070056 RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
57 dst_release(&rt->u.dst);
58 return -1;
59 }
60 dst_release(&rt->u.dst);
61 dst_release(odst);
62 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090063
Eric Dumazetadf30902009-06-02 05:19:30 +000064 if (skb_dst(skb)->error)
Harald Welte020b4c12005-08-09 19:39:00 -070065 return -1;
66
Patrick McHardy3e3850e2006-01-06 23:04:54 -080067#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070068 if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
Eric Dumazetadf30902009-06-02 05:19:30 +000069 xfrm_decode_session(skb, &fl, AF_INET) == 0) {
70 struct dst_entry *dst = skb_dst(skb);
71 skb_dst_set(skb, NULL);
72 if (xfrm_lookup(net, &dst, &fl, skb->sk, 0))
Patrick McHardy3e3850e2006-01-06 23:04:54 -080073 return -1;
Eric Dumazetadf30902009-06-02 05:19:30 +000074 skb_dst_set(skb, dst);
75 }
Patrick McHardy3e3850e2006-01-06 23:04:54 -080076#endif
77
Harald Welte020b4c12005-08-09 19:39:00 -070078 /* Change in oif may mean change in hh_len. */
Eric Dumazetadf30902009-06-02 05:19:30 +000079 hh_len = skb_dst(skb)->dev->hard_header_len;
Herbert Xu3db05fe2007-10-15 00:53:15 -070080 if (skb_headroom(skb) < hh_len &&
81 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -070082 return -1;
Harald Welte020b4c12005-08-09 19:39:00 -070083
84 return 0;
85}
86EXPORT_SYMBOL(ip_route_me_harder);
Harald Welte2cc7d572005-08-09 19:42:34 -070087
Patrick McHardyee68cea2006-02-15 01:34:23 -080088#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070089int ip_xfrm_me_harder(struct sk_buff *skb)
Patrick McHardyee68cea2006-02-15 01:34:23 -080090{
91 struct flowi fl;
92 unsigned int hh_len;
93 struct dst_entry *dst;
94
Herbert Xu3db05fe2007-10-15 00:53:15 -070095 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
Patrick McHardyee68cea2006-02-15 01:34:23 -080096 return 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -070097 if (xfrm_decode_session(skb, &fl, AF_INET) < 0)
Patrick McHardyee68cea2006-02-15 01:34:23 -080098 return -1;
99
Eric Dumazetadf30902009-06-02 05:19:30 +0000100 dst = skb_dst(skb);
Patrick McHardyee68cea2006-02-15 01:34:23 -0800101 if (dst->xfrm)
102 dst = ((struct xfrm_dst *)dst)->route;
103 dst_hold(dst);
104
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800105 if (xfrm_lookup(dev_net(dst->dev), &dst, &fl, skb->sk, 0) < 0)
Patrick McHardyee68cea2006-02-15 01:34:23 -0800106 return -1;
107
Eric Dumazetadf30902009-06-02 05:19:30 +0000108 skb_dst_drop(skb);
109 skb_dst_set(skb, dst);
Patrick McHardyee68cea2006-02-15 01:34:23 -0800110
111 /* Change in oif may mean change in hh_len. */
Eric Dumazetadf30902009-06-02 05:19:30 +0000112 hh_len = skb_dst(skb)->dev->hard_header_len;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700113 if (skb_headroom(skb) < hh_len &&
114 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700115 return -1;
Patrick McHardyee68cea2006-02-15 01:34:23 -0800116 return 0;
117}
118EXPORT_SYMBOL(ip_xfrm_me_harder);
119#endif
120
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800121void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
122EXPORT_SYMBOL(ip_nat_decode_session);
123
Harald Welte2cc7d572005-08-09 19:42:34 -0700124/*
125 * Extra routing may needed on local out, as the QUEUE target never
126 * returns control to the table.
127 */
128
129struct ip_rt_info {
Al Viro59b8bfd2006-09-28 14:21:07 -0700130 __be32 daddr;
131 __be32 saddr;
Harald Welte2cc7d572005-08-09 19:42:34 -0700132 u_int8_t tos;
Eric Leblond5f145e42008-11-25 12:15:16 +0100133 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -0700134};
135
Patrick McHardy02f014d2007-12-05 01:26:33 -0800136static void nf_ip_saveroute(const struct sk_buff *skb,
137 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700138{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800139 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700140
Patrick McHardy02f014d2007-12-05 01:26:33 -0800141 if (entry->hook == NF_INET_LOCAL_OUT) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700142 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700143
144 rt_info->tos = iph->tos;
145 rt_info->daddr = iph->daddr;
146 rt_info->saddr = iph->saddr;
Eric Leblond5f145e42008-11-25 12:15:16 +0100147 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -0700148 }
149}
150
Patrick McHardy02f014d2007-12-05 01:26:33 -0800151static int nf_ip_reroute(struct sk_buff *skb,
152 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700153{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800154 const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700155
Patrick McHardy02f014d2007-12-05 01:26:33 -0800156 if (entry->hook == NF_INET_LOCAL_OUT) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700157 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700158
Joe Perchesf64f9e72009-11-29 16:55:45 -0800159 if (!(iph->tos == rt_info->tos &&
160 skb->mark == rt_info->mark &&
161 iph->daddr == rt_info->daddr &&
162 iph->saddr == rt_info->saddr))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700163 return ip_route_me_harder(skb, RTN_UNSPEC);
Harald Welte2cc7d572005-08-09 19:42:34 -0700164 }
165 return 0;
166}
167
Al Virob51655b2006-11-14 21:40:42 -0800168__sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700169 unsigned int dataoff, u_int8_t protocol)
170{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700171 const struct iphdr *iph = ip_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800172 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700173
174 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700175 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800176 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700177 break;
Al Virod3bc23e2006-11-14 21:24:49 -0800178 if ((protocol == 0 && !csum_fold(skb->csum)) ||
Patrick McHardy422c3462006-04-06 14:18:43 -0700179 !csum_tcpudp_magic(iph->saddr, iph->daddr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900180 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700181 skb->csum)) {
182 skb->ip_summed = CHECKSUM_UNNECESSARY;
183 break;
184 }
185 /* fall through */
186 case CHECKSUM_NONE:
187 if (protocol == 0)
188 skb->csum = 0;
189 else
190 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
191 skb->len - dataoff,
192 protocol, 0);
193 csum = __skb_checksum_complete(skb);
194 }
195 return csum;
196}
Patrick McHardy422c3462006-04-06 14:18:43 -0700197EXPORT_SYMBOL(nf_ip_checksum);
198
Patrick McHardyd63a6502008-03-20 15:15:53 +0100199static __sum16 nf_ip_checksum_partial(struct sk_buff *skb, unsigned int hook,
200 unsigned int dataoff, unsigned int len,
201 u_int8_t protocol)
202{
203 const struct iphdr *iph = ip_hdr(skb);
204 __sum16 csum = 0;
205
206 switch (skb->ip_summed) {
207 case CHECKSUM_COMPLETE:
208 if (len == skb->len - dataoff)
209 return nf_ip_checksum(skb, hook, dataoff, protocol);
210 /* fall through */
211 case CHECKSUM_NONE:
212 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, protocol,
213 skb->len - dataoff, 0);
214 skb->ip_summed = CHECKSUM_NONE;
215 csum = __skb_checksum_complete_head(skb, dataoff + len);
216 if (!csum)
217 skb->ip_summed = CHECKSUM_UNNECESSARY;
218 }
219 return csum;
220}
221
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800222static int nf_ip_route(struct dst_entry **dst, struct flowi *fl)
223{
Denis V. Lunevf2063512008-01-22 22:07:34 -0800224 return ip_route_output_key(&init_net, (struct rtable **)dst, fl);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800225}
226
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800227static const struct nf_afinfo nf_ip_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100228 .family = AF_INET,
229 .checksum = nf_ip_checksum,
230 .checksum_partial = nf_ip_checksum_partial,
231 .route = nf_ip_route,
232 .saveroute = nf_ip_saveroute,
233 .reroute = nf_ip_reroute,
234 .route_key_size = sizeof(struct ip_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700235};
236
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800237static int ipv4_netfilter_init(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700238{
Patrick McHardybce80322006-04-06 14:18:09 -0700239 return nf_register_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700240}
241
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800242static void ipv4_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700243{
Patrick McHardybce80322006-04-06 14:18:09 -0700244 nf_unregister_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700245}
246
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800247module_init(ipv4_netfilter_init);
248module_exit(ipv4_netfilter_fini);
Patrick McHardy4f536522008-01-14 23:48:39 -0800249
250#ifdef CONFIG_SYSCTL
251struct ctl_path nf_net_ipv4_netfilter_sysctl_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800252 { .procname = "net", },
253 { .procname = "ipv4", },
254 { .procname = "netfilter", },
Patrick McHardy4f536522008-01-14 23:48:39 -0800255 { }
256};
257EXPORT_SYMBOL_GPL(nf_net_ipv4_netfilter_sysctl_path);
258#endif /* CONFIG_SYSCTL */