1f229f6ceSPatrick McHardy /*
2f229f6ceSPatrick McHardy * IPv4 specific functions of netfilter core
3f229f6ceSPatrick McHardy *
4f229f6ceSPatrick McHardy * Rusty Russell (C) 2000 -- This code is GPL.
5f229f6ceSPatrick McHardy * Patrick McHardy (C) 2006-2012
6f229f6ceSPatrick McHardy */
7020b4c12SHarald Welte #include <linux/kernel.h>
8020b4c12SHarald Welte #include <linux/netfilter.h>
92cc7d573SHarald Welte #include <linux/netfilter_ipv4.h>
103e3850e9SPatrick McHardy #include <linux/ip.h>
112ca7b0acSHerbert Xu #include <linux/skbuff.h>
125a0e3ad6STejun Heo #include <linux/gfp.h>
13bc3b2d7fSPaul Gortmaker #include <linux/export.h>
14020b4c12SHarald Welte #include <net/route.h>
153e3850e9SPatrick McHardy #include <net/xfrm.h>
163e3850e9SPatrick McHardy #include <net/ip.h>
17*4f088076SIdo Schimmel #include <net/inet_dscp.h>
18c01cd429SPatrick McHardy #include <net/netfilter/nf_queue.h>
19020b4c12SHarald Welte
20020b4c12SHarald Welte /* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
ip_route_me_harder(struct net * net,struct sock * sk,struct sk_buff * skb,unsigned int addr_type)2146d6c5aeSJason A. Donenfeld int ip_route_me_harder(struct net *net, struct sock *sk, struct sk_buff *skb, unsigned int addr_type)
22020b4c12SHarald Welte {
233db05feaSHerbert Xu const struct iphdr *iph = ip_hdr(skb);
24020b4c12SHarald Welte struct rtable *rt;
259d6ec938SDavid S. Miller struct flowi4 fl4 = {};
26ed6e4ef8SJulian Anastasov __be32 saddr = iph->saddr;
2746d6c5aeSJason A. Donenfeld __u8 flags;
286d8b49c3SDavid Ahern struct net_device *dev = skb_dst(skb)->dev;
29812fa71fSIdo Schimmel struct flow_keys flkeys;
30020b4c12SHarald Welte unsigned int hh_len;
31020b4c12SHarald Welte
3246d6c5aeSJason A. Donenfeld sk = sk_to_full_sk(sk);
3346d6c5aeSJason A. Donenfeld flags = sk ? inet_sk_flowi_flags(sk) : 0;
3446d6c5aeSJason A. Donenfeld
35b4c4ed17SSimon Horman if (addr_type == RTN_UNSPEC)
366d8b49c3SDavid Ahern addr_type = inet_addr_type_dev_table(net, dev, saddr);
37ed6e4ef8SJulian Anastasov if (addr_type == RTN_LOCAL || addr_type == RTN_UNICAST)
38ed6e4ef8SJulian Anastasov flags |= FLOWI_FLAG_ANYSRC;
39ed6e4ef8SJulian Anastasov else
40ed6e4ef8SJulian Anastasov saddr = 0;
41b4c4ed17SSimon Horman
42020b4c12SHarald Welte /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
436e23ae2aSPatrick McHardy * packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
44020b4c12SHarald Welte */
459d6ec938SDavid S. Miller fl4.daddr = iph->daddr;
46ed6e4ef8SJulian Anastasov fl4.saddr = saddr;
47*4f088076SIdo Schimmel fl4.flowi4_tos = iph->tos & INET_DSCP_MASK;
4829e09229SFlorian Westphal fl4.flowi4_oif = sk ? sk->sk_bound_dev_if : 0;
492c50fc04SMartin Willi fl4.flowi4_l3mdev = l3mdev_master_ifindex(dev);
509d6ec938SDavid S. Miller fl4.flowi4_mark = skb->mark;
51797fd391SJulian Anastasov fl4.flowi4_flags = flags;
52812fa71fSIdo Schimmel fib4_rules_early_flow_dissect(net, skb, &fl4, &flkeys);
539d6ec938SDavid S. Miller rt = ip_route_output_key(net, &fl4);
54b23dd4feSDavid S. Miller if (IS_ERR(rt))
55c9e1673aSPatrick McHardy return PTR_ERR(rt);
56020b4c12SHarald Welte
57020b4c12SHarald Welte /* Drop old route. */
58adf30907SEric Dumazet skb_dst_drop(skb);
59d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst);
60020b4c12SHarald Welte
61adf30907SEric Dumazet if (skb_dst(skb)->error)
62c9e1673aSPatrick McHardy return skb_dst(skb)->error;
63020b4c12SHarald Welte
643e3850e9SPatrick McHardy #ifdef CONFIG_XFRM
653db05feaSHerbert Xu if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
662b1dc628SFlorian Westphal xfrm_decode_session(net, skb, flowi4_to_flowi(&fl4), AF_INET) == 0) {
67adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb);
68adf30907SEric Dumazet skb_dst_set(skb, NULL);
6929e09229SFlorian Westphal dst = xfrm_lookup(net, dst, flowi4_to_flowi(&fl4), sk, 0);
70452edd59SDavid S. Miller if (IS_ERR(dst))
713d0a0620Sstephen hemminger return PTR_ERR(dst);
72adf30907SEric Dumazet skb_dst_set(skb, dst);
73adf30907SEric Dumazet }
743e3850e9SPatrick McHardy #endif
753e3850e9SPatrick McHardy
76020b4c12SHarald Welte /* Change in oif may mean change in hh_len. */
77adf30907SEric Dumazet hh_len = skb_dst(skb)->dev->hard_header_len;
783db05feaSHerbert Xu if (skb_headroom(skb) < hh_len &&
795e2afba4SPaul Guo pskb_expand_head(skb, HH_DATA_ALIGN(hh_len - skb_headroom(skb)),
805e2afba4SPaul Guo 0, GFP_ATOMIC))
81c9e1673aSPatrick McHardy return -ENOMEM;
82020b4c12SHarald Welte
83020b4c12SHarald Welte return 0;
84020b4c12SHarald Welte }
85020b4c12SHarald Welte EXPORT_SYMBOL(ip_route_me_harder);
862cc7d573SHarald Welte
nf_ip_route(struct net * net,struct dst_entry ** dst,struct flowi * fl,bool strict __always_unused)873f87c08cSPablo Neira Ayuso int nf_ip_route(struct net *net, struct dst_entry **dst, struct flowi *fl,
883f87c08cSPablo Neira Ayuso bool strict __always_unused)
891841a4c7SPatrick McHardy {
9031ad3dd6SFlorian Westphal struct rtable *rt = ip_route_output_key(net, &fl->u.ip4);
91b23dd4feSDavid S. Miller if (IS_ERR(rt))
92b23dd4feSDavid S. Miller return PTR_ERR(rt);
93b23dd4feSDavid S. Miller *dst = &rt->dst;
94b23dd4feSDavid S. Miller return 0;
951841a4c7SPatrick McHardy }
963f87c08cSPablo Neira Ayuso EXPORT_SYMBOL_GPL(nf_ip_route);
97