blob: 1725dc0ef68875253d02e4e6bdd777ead267d9a3 [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{
Eric Dumazetadf30902009-06-02 05:19:30 +000015 struct net *net = dev_net(skb_dst(skb)->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. */
Eric Dumazetadf30902009-06-02 05:19:30 +000044 skb_dst_drop(skb);
45 skb_dst_set(skb, &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
Eric Dumazetadf30902009-06-02 05:19:30 +000053 odst = skb_dst(skb);
Herbert Xu3db05fe2007-10-15 00:53:15 -070054 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
Eric Dumazetadf30902009-06-02 05:19:30 +000063 if (skb_dst(skb)->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) &&
Eric Dumazetadf30902009-06-02 05:19:30 +000068 xfrm_decode_session(skb, &fl, AF_INET) == 0) {
69 struct dst_entry *dst = skb_dst(skb);
70 skb_dst_set(skb, NULL);
71 if (xfrm_lookup(net, &dst, &fl, skb->sk, 0))
Patrick McHardy3e3850e2006-01-06 23:04:54 -080072 return -1;
Eric Dumazetadf30902009-06-02 05:19:30 +000073 skb_dst_set(skb, dst);
74 }
Patrick McHardy3e3850e2006-01-06 23:04:54 -080075#endif
76
Harald Welte020b4c12005-08-09 19:39:00 -070077 /* Change in oif may mean change in hh_len. */
Eric Dumazetadf30902009-06-02 05:19:30 +000078 hh_len = skb_dst(skb)->dev->hard_header_len;
Herbert Xu3db05fe2007-10-15 00:53:15 -070079 if (skb_headroom(skb) < hh_len &&
80 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -070081 return -1;
Harald Welte020b4c12005-08-09 19:39:00 -070082
83 return 0;
84}
85EXPORT_SYMBOL(ip_route_me_harder);
Harald Welte2cc7d572005-08-09 19:42:34 -070086
Patrick McHardyee68cea2006-02-15 01:34:23 -080087#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070088int ip_xfrm_me_harder(struct sk_buff *skb)
Patrick McHardyee68cea2006-02-15 01:34:23 -080089{
90 struct flowi fl;
91 unsigned int hh_len;
92 struct dst_entry *dst;
93
Herbert Xu3db05fe2007-10-15 00:53:15 -070094 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
Patrick McHardyee68cea2006-02-15 01:34:23 -080095 return 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -070096 if (xfrm_decode_session(skb, &fl, AF_INET) < 0)
Patrick McHardyee68cea2006-02-15 01:34:23 -080097 return -1;
98
Eric Dumazetadf30902009-06-02 05:19:30 +000099 dst = skb_dst(skb);
Patrick McHardyee68cea2006-02-15 01:34:23 -0800100 if (dst->xfrm)
101 dst = ((struct xfrm_dst *)dst)->route;
102 dst_hold(dst);
103
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800104 if (xfrm_lookup(dev_net(dst->dev), &dst, &fl, skb->sk, 0) < 0)
Patrick McHardyee68cea2006-02-15 01:34:23 -0800105 return -1;
106
Eric Dumazetadf30902009-06-02 05:19:30 +0000107 skb_dst_drop(skb);
108 skb_dst_set(skb, dst);
Patrick McHardyee68cea2006-02-15 01:34:23 -0800109
110 /* Change in oif may mean change in hh_len. */
Eric Dumazetadf30902009-06-02 05:19:30 +0000111 hh_len = skb_dst(skb)->dev->hard_header_len;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700112 if (skb_headroom(skb) < hh_len &&
113 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700114 return -1;
Patrick McHardyee68cea2006-02-15 01:34:23 -0800115 return 0;
116}
117EXPORT_SYMBOL(ip_xfrm_me_harder);
118#endif
119
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800120void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
121EXPORT_SYMBOL(ip_nat_decode_session);
122
Harald Welte2cc7d572005-08-09 19:42:34 -0700123/*
124 * Extra routing may needed on local out, as the QUEUE target never
125 * returns control to the table.
126 */
127
128struct ip_rt_info {
Al Viro59b8bfd2006-09-28 14:21:07 -0700129 __be32 daddr;
130 __be32 saddr;
Harald Welte2cc7d572005-08-09 19:42:34 -0700131 u_int8_t tos;
Eric Leblond5f145e42008-11-25 12:15:16 +0100132 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -0700133};
134
Patrick McHardy02f014d2007-12-05 01:26:33 -0800135static void nf_ip_saveroute(const struct sk_buff *skb,
136 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700137{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800138 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700139
Patrick McHardy02f014d2007-12-05 01:26:33 -0800140 if (entry->hook == NF_INET_LOCAL_OUT) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700141 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700142
143 rt_info->tos = iph->tos;
144 rt_info->daddr = iph->daddr;
145 rt_info->saddr = iph->saddr;
Eric Leblond5f145e42008-11-25 12:15:16 +0100146 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -0700147 }
148}
149
Patrick McHardy02f014d2007-12-05 01:26:33 -0800150static int nf_ip_reroute(struct sk_buff *skb,
151 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700152{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800153 const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700154
Patrick McHardy02f014d2007-12-05 01:26:33 -0800155 if (entry->hook == NF_INET_LOCAL_OUT) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700156 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700157
158 if (!(iph->tos == rt_info->tos
Eric Leblond5f145e42008-11-25 12:15:16 +0100159 && skb->mark == rt_info->mark
Harald Welte2cc7d572005-08-09 19:42:34 -0700160 && iph->daddr == rt_info->daddr
161 && iph->saddr == rt_info->saddr))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700162 return ip_route_me_harder(skb, RTN_UNSPEC);
Harald Welte2cc7d572005-08-09 19:42:34 -0700163 }
164 return 0;
165}
166
Al Virob51655b2006-11-14 21:40:42 -0800167__sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700168 unsigned int dataoff, u_int8_t protocol)
169{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700170 const struct iphdr *iph = ip_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800171 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700172
173 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700174 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800175 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700176 break;
Al Virod3bc23e2006-11-14 21:24:49 -0800177 if ((protocol == 0 && !csum_fold(skb->csum)) ||
Patrick McHardy422c3462006-04-06 14:18:43 -0700178 !csum_tcpudp_magic(iph->saddr, iph->daddr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900179 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700180 skb->csum)) {
181 skb->ip_summed = CHECKSUM_UNNECESSARY;
182 break;
183 }
184 /* fall through */
185 case CHECKSUM_NONE:
186 if (protocol == 0)
187 skb->csum = 0;
188 else
189 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
190 skb->len - dataoff,
191 protocol, 0);
192 csum = __skb_checksum_complete(skb);
193 }
194 return csum;
195}
Patrick McHardy422c3462006-04-06 14:18:43 -0700196EXPORT_SYMBOL(nf_ip_checksum);
197
Patrick McHardyd63a6502008-03-20 15:15:53 +0100198static __sum16 nf_ip_checksum_partial(struct sk_buff *skb, unsigned int hook,
199 unsigned int dataoff, unsigned int len,
200 u_int8_t protocol)
201{
202 const struct iphdr *iph = ip_hdr(skb);
203 __sum16 csum = 0;
204
205 switch (skb->ip_summed) {
206 case CHECKSUM_COMPLETE:
207 if (len == skb->len - dataoff)
208 return nf_ip_checksum(skb, hook, dataoff, protocol);
209 /* fall through */
210 case CHECKSUM_NONE:
211 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, protocol,
212 skb->len - dataoff, 0);
213 skb->ip_summed = CHECKSUM_NONE;
214 csum = __skb_checksum_complete_head(skb, dataoff + len);
215 if (!csum)
216 skb->ip_summed = CHECKSUM_UNNECESSARY;
217 }
218 return csum;
219}
220
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800221static int nf_ip_route(struct dst_entry **dst, struct flowi *fl)
222{
Denis V. Lunevf2063512008-01-22 22:07:34 -0800223 return ip_route_output_key(&init_net, (struct rtable **)dst, fl);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800224}
225
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800226static const struct nf_afinfo nf_ip_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100227 .family = AF_INET,
228 .checksum = nf_ip_checksum,
229 .checksum_partial = nf_ip_checksum_partial,
230 .route = nf_ip_route,
231 .saveroute = nf_ip_saveroute,
232 .reroute = nf_ip_reroute,
233 .route_key_size = sizeof(struct ip_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700234};
235
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800236static int ipv4_netfilter_init(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700237{
Patrick McHardybce80322006-04-06 14:18:09 -0700238 return nf_register_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700239}
240
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800241static void ipv4_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700242{
Patrick McHardybce80322006-04-06 14:18:09 -0700243 nf_unregister_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700244}
245
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800246module_init(ipv4_netfilter_init);
247module_exit(ipv4_netfilter_fini);
Patrick McHardy4f536522008-01-14 23:48:39 -0800248
249#ifdef CONFIG_SYSCTL
250struct ctl_path nf_net_ipv4_netfilter_sysctl_path[] = {
251 { .procname = "net", .ctl_name = CTL_NET, },
252 { .procname = "ipv4", .ctl_name = NET_IPV4, },
253 { .procname = "netfilter", .ctl_name = NET_IPV4_NETFILTER, },
254 { }
255};
256EXPORT_SYMBOL_GPL(nf_net_ipv4_netfilter_sysctl_path);
257#endif /* CONFIG_SYSCTL */