blob: fdf6811c31a246df891495fdc76b46c00e5f783b [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>
Harald Welte020b4c12005-08-09 19:39:00 -07007#include <net/route.h>
Patrick McHardy3e3850e2006-01-06 23:04:54 -08008#include <net/xfrm.h>
9#include <net/ip.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080010#include <net/netfilter/nf_queue.h>
Harald Welte020b4c12005-08-09 19:39:00 -070011
12/* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
Herbert Xu3db05fe2007-10-15 00:53:15 -070013int ip_route_me_harder(struct sk_buff *skb, unsigned addr_type)
Harald Welte020b4c12005-08-09 19:39:00 -070014{
Alexey Dobriyanb21f8902008-10-08 11:35:03 +020015 struct net *net = dev_net(skb->dst->dev);
Herbert Xu3db05fe2007-10-15 00:53:15 -070016 const struct iphdr *iph = ip_hdr(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070017 struct rtable *rt;
18 struct flowi fl = {};
19 struct dst_entry *odst;
20 unsigned int hh_len;
Patrick McHardyc68b8b62007-01-04 12:15:34 -080021 unsigned int type;
Harald Welte020b4c12005-08-09 19:39:00 -070022
Alexey Dobriyanb21f8902008-10-08 11:35:03 +020023 type = inet_addr_type(net, iph->saddr);
KOVACS Krisztian86b08d82008-10-01 07:44:42 -070024 if (skb->sk && inet_sk(skb->sk)->transparent)
25 type = RTN_LOCAL;
Simon Hormanb4c4ed12006-10-02 16:11:13 -070026 if (addr_type == RTN_UNSPEC)
Patrick McHardyc68b8b62007-01-04 12:15:34 -080027 addr_type = type;
Simon Hormanb4c4ed12006-10-02 16:11:13 -070028
Harald Welte020b4c12005-08-09 19:39:00 -070029 /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
Patrick McHardy6e23ae22007-11-19 18:53:30 -080030 * packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
Harald Welte020b4c12005-08-09 19:39:00 -070031 */
Simon Hormanb4c4ed12006-10-02 16:11:13 -070032 if (addr_type == RTN_LOCAL) {
Harald Welte020b4c12005-08-09 19:39:00 -070033 fl.nl_u.ip4_u.daddr = iph->daddr;
Patrick McHardyc68b8b62007-01-04 12:15:34 -080034 if (type == RTN_LOCAL)
35 fl.nl_u.ip4_u.saddr = iph->saddr;
Harald Welte020b4c12005-08-09 19:39:00 -070036 fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
Herbert Xu3db05fe2007-10-15 00:53:15 -070037 fl.oif = skb->sk ? skb->sk->sk_bound_dev_if : 0;
38 fl.mark = skb->mark;
KOVACS Krisztian86b08d82008-10-01 07:44:42 -070039 fl.flags = skb->sk ? inet_sk_flowi_flags(skb->sk) : 0;
Alexey Dobriyanb21f8902008-10-08 11:35:03 +020040 if (ip_route_output_key(net, &rt, &fl) != 0)
Harald Welte020b4c12005-08-09 19:39:00 -070041 return -1;
42
43 /* Drop old route. */
Herbert Xu3db05fe2007-10-15 00:53:15 -070044 dst_release(skb->dst);
45 skb->dst = &rt->u.dst;
Harald Welte020b4c12005-08-09 19:39:00 -070046 } else {
47 /* non-local src, find valid iif to satisfy
48 * rp-filter when calling ip_route_input. */
49 fl.nl_u.ip4_u.daddr = iph->saddr;
Alexey Dobriyanb21f8902008-10-08 11:35:03 +020050 if (ip_route_output_key(net, &rt, &fl) != 0)
Harald Welte020b4c12005-08-09 19:39:00 -070051 return -1;
52
Herbert Xu3db05fe2007-10-15 00:53:15 -070053 odst = skb->dst;
54 if (ip_route_input(skb, iph->daddr, iph->saddr,
Harald Welte020b4c12005-08-09 19:39:00 -070055 RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
56 dst_release(&rt->u.dst);
57 return -1;
58 }
59 dst_release(&rt->u.dst);
60 dst_release(odst);
61 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090062
Herbert Xu3db05fe2007-10-15 00:53:15 -070063 if (skb->dst->error)
Harald Welte020b4c12005-08-09 19:39:00 -070064 return -1;
65
Patrick McHardy3e3850e2006-01-06 23:04:54 -080066#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070067 if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
68 xfrm_decode_session(skb, &fl, AF_INET) == 0)
Alexey Dobriyan52479b62008-11-25 17:35:18 -080069 if (xfrm_lookup(net, &skb->dst, &fl, skb->sk, 0))
Patrick McHardy3e3850e2006-01-06 23:04:54 -080070 return -1;
71#endif
72
Harald Welte020b4c12005-08-09 19:39:00 -070073 /* Change in oif may mean change in hh_len. */
Herbert Xu3db05fe2007-10-15 00:53:15 -070074 hh_len = skb->dst->dev->hard_header_len;
75 if (skb_headroom(skb) < hh_len &&
76 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -070077 return -1;
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
Patrick McHardyee68cea2006-02-15 01:34:23 -080083#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070084int ip_xfrm_me_harder(struct sk_buff *skb)
Patrick McHardyee68cea2006-02-15 01:34:23 -080085{
86 struct flowi fl;
87 unsigned int hh_len;
88 struct dst_entry *dst;
89
Herbert Xu3db05fe2007-10-15 00:53:15 -070090 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
Patrick McHardyee68cea2006-02-15 01:34:23 -080091 return 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -070092 if (xfrm_decode_session(skb, &fl, AF_INET) < 0)
Patrick McHardyee68cea2006-02-15 01:34:23 -080093 return -1;
94
Herbert Xu3db05fe2007-10-15 00:53:15 -070095 dst = skb->dst;
Patrick McHardyee68cea2006-02-15 01:34:23 -080096 if (dst->xfrm)
97 dst = ((struct xfrm_dst *)dst)->route;
98 dst_hold(dst);
99
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800100 if (xfrm_lookup(dev_net(dst->dev), &dst, &fl, skb->sk, 0) < 0)
Patrick McHardyee68cea2006-02-15 01:34:23 -0800101 return -1;
102
Herbert Xu3db05fe2007-10-15 00:53:15 -0700103 dst_release(skb->dst);
104 skb->dst = dst;
Patrick McHardyee68cea2006-02-15 01:34:23 -0800105
106 /* Change in oif may mean change in hh_len. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700107 hh_len = skb->dst->dev->hard_header_len;
108 if (skb_headroom(skb) < hh_len &&
109 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700110 return -1;
Patrick McHardyee68cea2006-02-15 01:34:23 -0800111 return 0;
112}
113EXPORT_SYMBOL(ip_xfrm_me_harder);
114#endif
115
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800116void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
117EXPORT_SYMBOL(ip_nat_decode_session);
118
Harald Welte2cc7d572005-08-09 19:42:34 -0700119/*
120 * Extra routing may needed on local out, as the QUEUE target never
121 * returns control to the table.
122 */
123
124struct ip_rt_info {
Al Viro59b8bfd2006-09-28 14:21:07 -0700125 __be32 daddr;
126 __be32 saddr;
Harald Welte2cc7d572005-08-09 19:42:34 -0700127 u_int8_t tos;
Eric Leblond5f145e42008-11-25 12:15:16 +0100128 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -0700129};
130
Patrick McHardy02f014d2007-12-05 01:26:33 -0800131static void nf_ip_saveroute(const struct sk_buff *skb,
132 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700133{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800134 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700135
Patrick McHardy02f014d2007-12-05 01:26:33 -0800136 if (entry->hook == NF_INET_LOCAL_OUT) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700137 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700138
139 rt_info->tos = iph->tos;
140 rt_info->daddr = iph->daddr;
141 rt_info->saddr = iph->saddr;
Eric Leblond5f145e42008-11-25 12:15:16 +0100142 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -0700143 }
144}
145
Patrick McHardy02f014d2007-12-05 01:26:33 -0800146static int nf_ip_reroute(struct sk_buff *skb,
147 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700148{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800149 const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700150
Patrick McHardy02f014d2007-12-05 01:26:33 -0800151 if (entry->hook == NF_INET_LOCAL_OUT) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700152 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700153
154 if (!(iph->tos == rt_info->tos
Eric Leblond5f145e42008-11-25 12:15:16 +0100155 && skb->mark == rt_info->mark
Harald Welte2cc7d572005-08-09 19:42:34 -0700156 && iph->daddr == rt_info->daddr
157 && iph->saddr == rt_info->saddr))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700158 return ip_route_me_harder(skb, RTN_UNSPEC);
Harald Welte2cc7d572005-08-09 19:42:34 -0700159 }
160 return 0;
161}
162
Al Virob51655b2006-11-14 21:40:42 -0800163__sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700164 unsigned int dataoff, u_int8_t protocol)
165{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700166 const struct iphdr *iph = ip_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800167 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700168
169 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700170 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800171 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700172 break;
Al Virod3bc23e2006-11-14 21:24:49 -0800173 if ((protocol == 0 && !csum_fold(skb->csum)) ||
Patrick McHardy422c3462006-04-06 14:18:43 -0700174 !csum_tcpudp_magic(iph->saddr, iph->daddr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900175 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700176 skb->csum)) {
177 skb->ip_summed = CHECKSUM_UNNECESSARY;
178 break;
179 }
180 /* fall through */
181 case CHECKSUM_NONE:
182 if (protocol == 0)
183 skb->csum = 0;
184 else
185 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
186 skb->len - dataoff,
187 protocol, 0);
188 csum = __skb_checksum_complete(skb);
189 }
190 return csum;
191}
Patrick McHardy422c3462006-04-06 14:18:43 -0700192EXPORT_SYMBOL(nf_ip_checksum);
193
Patrick McHardyd63a6502008-03-20 15:15:53 +0100194static __sum16 nf_ip_checksum_partial(struct sk_buff *skb, unsigned int hook,
195 unsigned int dataoff, unsigned int len,
196 u_int8_t protocol)
197{
198 const struct iphdr *iph = ip_hdr(skb);
199 __sum16 csum = 0;
200
201 switch (skb->ip_summed) {
202 case CHECKSUM_COMPLETE:
203 if (len == skb->len - dataoff)
204 return nf_ip_checksum(skb, hook, dataoff, protocol);
205 /* fall through */
206 case CHECKSUM_NONE:
207 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, protocol,
208 skb->len - dataoff, 0);
209 skb->ip_summed = CHECKSUM_NONE;
210 csum = __skb_checksum_complete_head(skb, dataoff + len);
211 if (!csum)
212 skb->ip_summed = CHECKSUM_UNNECESSARY;
213 }
214 return csum;
215}
216
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800217static int nf_ip_route(struct dst_entry **dst, struct flowi *fl)
218{
Denis V. Lunevf2063512008-01-22 22:07:34 -0800219 return ip_route_output_key(&init_net, (struct rtable **)dst, fl);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800220}
221
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800222static const struct nf_afinfo nf_ip_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100223 .family = AF_INET,
224 .checksum = nf_ip_checksum,
225 .checksum_partial = nf_ip_checksum_partial,
226 .route = nf_ip_route,
227 .saveroute = nf_ip_saveroute,
228 .reroute = nf_ip_reroute,
229 .route_key_size = sizeof(struct ip_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700230};
231
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800232static int ipv4_netfilter_init(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700233{
Patrick McHardybce80322006-04-06 14:18:09 -0700234 return nf_register_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700235}
236
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800237static void ipv4_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700238{
Patrick McHardybce80322006-04-06 14:18:09 -0700239 nf_unregister_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700240}
241
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800242module_init(ipv4_netfilter_init);
243module_exit(ipv4_netfilter_fini);
Patrick McHardy4f536522008-01-14 23:48:39 -0800244
245#ifdef CONFIG_SYSCTL
246struct ctl_path nf_net_ipv4_netfilter_sysctl_path[] = {
247 { .procname = "net", .ctl_name = CTL_NET, },
248 { .procname = "ipv4", .ctl_name = NET_IPV4, },
249 { .procname = "netfilter", .ctl_name = NET_IPV4_NETFILTER, },
250 { }
251};
252EXPORT_SYMBOL_GPL(nf_net_ipv4_netfilter_sysctl_path);
253#endif /* CONFIG_SYSCTL */