blob: 01671ad51ed34d784bcec2f8d620b2283a8941dc [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{
Herbert Xu3db05fe2007-10-15 00:53:15 -070015 const struct iphdr *iph = ip_hdr(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070016 struct rtable *rt;
17 struct flowi fl = {};
18 struct dst_entry *odst;
19 unsigned int hh_len;
Patrick McHardyc68b8b62007-01-04 12:15:34 -080020 unsigned int type;
Harald Welte020b4c12005-08-09 19:39:00 -070021
Eric W. Biederman6b175b22008-01-10 03:25:28 -080022 type = inet_addr_type(&init_net, iph->saddr);
KOVACS Krisztian86b08d82008-10-01 07:44:42 -070023 if (skb->sk && inet_sk(skb->sk)->transparent)
24 type = RTN_LOCAL;
Simon Hormanb4c4ed12006-10-02 16:11:13 -070025 if (addr_type == RTN_UNSPEC)
Patrick McHardyc68b8b62007-01-04 12:15:34 -080026 addr_type = type;
Simon Hormanb4c4ed12006-10-02 16:11:13 -070027
Harald Welte020b4c12005-08-09 19:39:00 -070028 /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
Patrick McHardy6e23ae22007-11-19 18:53:30 -080029 * packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
Harald Welte020b4c12005-08-09 19:39:00 -070030 */
Simon Hormanb4c4ed12006-10-02 16:11:13 -070031 if (addr_type == RTN_LOCAL) {
Harald Welte020b4c12005-08-09 19:39:00 -070032 fl.nl_u.ip4_u.daddr = iph->daddr;
Patrick McHardyc68b8b62007-01-04 12:15:34 -080033 if (type == RTN_LOCAL)
34 fl.nl_u.ip4_u.saddr = iph->saddr;
Harald Welte020b4c12005-08-09 19:39:00 -070035 fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
Herbert Xu3db05fe2007-10-15 00:53:15 -070036 fl.oif = skb->sk ? skb->sk->sk_bound_dev_if : 0;
37 fl.mark = skb->mark;
KOVACS Krisztian86b08d82008-10-01 07:44:42 -070038 fl.flags = skb->sk ? inet_sk_flowi_flags(skb->sk) : 0;
Denis V. Lunevf2063512008-01-22 22:07:34 -080039 if (ip_route_output_key(&init_net, &rt, &fl) != 0)
Harald Welte020b4c12005-08-09 19:39:00 -070040 return -1;
41
42 /* Drop old route. */
Herbert Xu3db05fe2007-10-15 00:53:15 -070043 dst_release(skb->dst);
44 skb->dst = &rt->u.dst;
Harald Welte020b4c12005-08-09 19:39:00 -070045 } else {
46 /* non-local src, find valid iif to satisfy
47 * rp-filter when calling ip_route_input. */
48 fl.nl_u.ip4_u.daddr = iph->saddr;
Denis V. Lunevf2063512008-01-22 22:07:34 -080049 if (ip_route_output_key(&init_net, &rt, &fl) != 0)
Harald Welte020b4c12005-08-09 19:39:00 -070050 return -1;
51
Herbert Xu3db05fe2007-10-15 00:53:15 -070052 odst = skb->dst;
53 if (ip_route_input(skb, iph->daddr, iph->saddr,
Harald Welte020b4c12005-08-09 19:39:00 -070054 RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
55 dst_release(&rt->u.dst);
56 return -1;
57 }
58 dst_release(&rt->u.dst);
59 dst_release(odst);
60 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090061
Herbert Xu3db05fe2007-10-15 00:53:15 -070062 if (skb->dst->error)
Harald Welte020b4c12005-08-09 19:39:00 -070063 return -1;
64
Patrick McHardy3e3850e2006-01-06 23:04:54 -080065#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070066 if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
67 xfrm_decode_session(skb, &fl, AF_INET) == 0)
68 if (xfrm_lookup(&skb->dst, &fl, skb->sk, 0))
Patrick McHardy3e3850e2006-01-06 23:04:54 -080069 return -1;
70#endif
71
Harald Welte020b4c12005-08-09 19:39:00 -070072 /* Change in oif may mean change in hh_len. */
Herbert Xu3db05fe2007-10-15 00:53:15 -070073 hh_len = skb->dst->dev->hard_header_len;
74 if (skb_headroom(skb) < hh_len &&
75 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -070076 return -1;
Harald Welte020b4c12005-08-09 19:39:00 -070077
78 return 0;
79}
80EXPORT_SYMBOL(ip_route_me_harder);
Harald Welte2cc7d572005-08-09 19:42:34 -070081
Patrick McHardyee68cea2006-02-15 01:34:23 -080082#ifdef CONFIG_XFRM
Herbert Xu3db05fe2007-10-15 00:53:15 -070083int ip_xfrm_me_harder(struct sk_buff *skb)
Patrick McHardyee68cea2006-02-15 01:34:23 -080084{
85 struct flowi fl;
86 unsigned int hh_len;
87 struct dst_entry *dst;
88
Herbert Xu3db05fe2007-10-15 00:53:15 -070089 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
Patrick McHardyee68cea2006-02-15 01:34:23 -080090 return 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -070091 if (xfrm_decode_session(skb, &fl, AF_INET) < 0)
Patrick McHardyee68cea2006-02-15 01:34:23 -080092 return -1;
93
Herbert Xu3db05fe2007-10-15 00:53:15 -070094 dst = skb->dst;
Patrick McHardyee68cea2006-02-15 01:34:23 -080095 if (dst->xfrm)
96 dst = ((struct xfrm_dst *)dst)->route;
97 dst_hold(dst);
98
Herbert Xu3db05fe2007-10-15 00:53:15 -070099 if (xfrm_lookup(&dst, &fl, skb->sk, 0) < 0)
Patrick McHardyee68cea2006-02-15 01:34:23 -0800100 return -1;
101
Herbert Xu3db05fe2007-10-15 00:53:15 -0700102 dst_release(skb->dst);
103 skb->dst = dst;
Patrick McHardyee68cea2006-02-15 01:34:23 -0800104
105 /* Change in oif may mean change in hh_len. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700106 hh_len = skb->dst->dev->hard_header_len;
107 if (skb_headroom(skb) < hh_len &&
108 pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700109 return -1;
Patrick McHardyee68cea2006-02-15 01:34:23 -0800110 return 0;
111}
112EXPORT_SYMBOL(ip_xfrm_me_harder);
113#endif
114
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800115void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
116EXPORT_SYMBOL(ip_nat_decode_session);
117
Harald Welte2cc7d572005-08-09 19:42:34 -0700118/*
119 * Extra routing may needed on local out, as the QUEUE target never
120 * returns control to the table.
121 */
122
123struct ip_rt_info {
Al Viro59b8bfd2006-09-28 14:21:07 -0700124 __be32 daddr;
125 __be32 saddr;
Harald Welte2cc7d572005-08-09 19:42:34 -0700126 u_int8_t tos;
127};
128
Patrick McHardy02f014d2007-12-05 01:26:33 -0800129static void nf_ip_saveroute(const struct sk_buff *skb,
130 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700131{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800132 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700133
Patrick McHardy02f014d2007-12-05 01:26:33 -0800134 if (entry->hook == NF_INET_LOCAL_OUT) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700135 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700136
137 rt_info->tos = iph->tos;
138 rt_info->daddr = iph->daddr;
139 rt_info->saddr = iph->saddr;
140 }
141}
142
Patrick McHardy02f014d2007-12-05 01:26:33 -0800143static int nf_ip_reroute(struct sk_buff *skb,
144 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -0700145{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800146 const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -0700147
Patrick McHardy02f014d2007-12-05 01:26:33 -0800148 if (entry->hook == NF_INET_LOCAL_OUT) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700149 const struct iphdr *iph = ip_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700150
151 if (!(iph->tos == rt_info->tos
152 && iph->daddr == rt_info->daddr
153 && iph->saddr == rt_info->saddr))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700154 return ip_route_me_harder(skb, RTN_UNSPEC);
Harald Welte2cc7d572005-08-09 19:42:34 -0700155 }
156 return 0;
157}
158
Al Virob51655b2006-11-14 21:40:42 -0800159__sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700160 unsigned int dataoff, u_int8_t protocol)
161{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700162 const struct iphdr *iph = ip_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800163 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700164
165 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700166 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800167 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700168 break;
Al Virod3bc23e2006-11-14 21:24:49 -0800169 if ((protocol == 0 && !csum_fold(skb->csum)) ||
Patrick McHardy422c3462006-04-06 14:18:43 -0700170 !csum_tcpudp_magic(iph->saddr, iph->daddr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900171 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700172 skb->csum)) {
173 skb->ip_summed = CHECKSUM_UNNECESSARY;
174 break;
175 }
176 /* fall through */
177 case CHECKSUM_NONE:
178 if (protocol == 0)
179 skb->csum = 0;
180 else
181 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
182 skb->len - dataoff,
183 protocol, 0);
184 csum = __skb_checksum_complete(skb);
185 }
186 return csum;
187}
Patrick McHardy422c3462006-04-06 14:18:43 -0700188EXPORT_SYMBOL(nf_ip_checksum);
189
Patrick McHardyd63a6502008-03-20 15:15:53 +0100190static __sum16 nf_ip_checksum_partial(struct sk_buff *skb, unsigned int hook,
191 unsigned int dataoff, unsigned int len,
192 u_int8_t protocol)
193{
194 const struct iphdr *iph = ip_hdr(skb);
195 __sum16 csum = 0;
196
197 switch (skb->ip_summed) {
198 case CHECKSUM_COMPLETE:
199 if (len == skb->len - dataoff)
200 return nf_ip_checksum(skb, hook, dataoff, protocol);
201 /* fall through */
202 case CHECKSUM_NONE:
203 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, protocol,
204 skb->len - dataoff, 0);
205 skb->ip_summed = CHECKSUM_NONE;
206 csum = __skb_checksum_complete_head(skb, dataoff + len);
207 if (!csum)
208 skb->ip_summed = CHECKSUM_UNNECESSARY;
209 }
210 return csum;
211}
212
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800213static int nf_ip_route(struct dst_entry **dst, struct flowi *fl)
214{
Denis V. Lunevf2063512008-01-22 22:07:34 -0800215 return ip_route_output_key(&init_net, (struct rtable **)dst, fl);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800216}
217
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800218static const struct nf_afinfo nf_ip_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100219 .family = AF_INET,
220 .checksum = nf_ip_checksum,
221 .checksum_partial = nf_ip_checksum_partial,
222 .route = nf_ip_route,
223 .saveroute = nf_ip_saveroute,
224 .reroute = nf_ip_reroute,
225 .route_key_size = sizeof(struct ip_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700226};
227
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800228static int ipv4_netfilter_init(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700229{
Patrick McHardybce80322006-04-06 14:18:09 -0700230 return nf_register_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700231}
232
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800233static void ipv4_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700234{
Patrick McHardybce80322006-04-06 14:18:09 -0700235 nf_unregister_afinfo(&nf_ip_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700236}
237
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800238module_init(ipv4_netfilter_init);
239module_exit(ipv4_netfilter_fini);
Patrick McHardy4f536522008-01-14 23:48:39 -0800240
241#ifdef CONFIG_SYSCTL
242struct ctl_path nf_net_ipv4_netfilter_sysctl_path[] = {
243 { .procname = "net", .ctl_name = CTL_NET, },
244 { .procname = "ipv4", .ctl_name = NET_IPV4, },
245 { .procname = "netfilter", .ctl_name = NET_IPV4_NETFILTER, },
246 { }
247};
248EXPORT_SYMBOL_GPL(nf_net_ipv4_netfilter_sysctl_path);
249#endif /* CONFIG_SYSCTL */