Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * IPv4 specific functions of netfilter core
0003  *
0004  * Rusty Russell (C) 2000 -- This code is GPL.
0005  * Patrick McHardy (C) 2006-2012
0006  */
0007 #include <linux/kernel.h>
0008 #include <linux/netfilter.h>
0009 #include <linux/netfilter_ipv4.h>
0010 #include <linux/ip.h>
0011 #include <linux/skbuff.h>
0012 #include <linux/gfp.h>
0013 #include <linux/export.h>
0014 #include <net/route.h>
0015 #include <net/xfrm.h>
0016 #include <net/ip.h>
0017 #include <net/netfilter/nf_queue.h>
0018 
0019 /* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
0020 int ip_route_me_harder(struct net *net, struct sock *sk, struct sk_buff *skb, unsigned int addr_type)
0021 {
0022     const struct iphdr *iph = ip_hdr(skb);
0023     struct rtable *rt;
0024     struct flowi4 fl4 = {};
0025     __be32 saddr = iph->saddr;
0026     __u8 flags;
0027     struct net_device *dev = skb_dst(skb)->dev;
0028     struct flow_keys flkeys;
0029     unsigned int hh_len;
0030 
0031     sk = sk_to_full_sk(sk);
0032     flags = sk ? inet_sk_flowi_flags(sk) : 0;
0033 
0034     if (addr_type == RTN_UNSPEC)
0035         addr_type = inet_addr_type_dev_table(net, dev, saddr);
0036     if (addr_type == RTN_LOCAL || addr_type == RTN_UNICAST)
0037         flags |= FLOWI_FLAG_ANYSRC;
0038     else
0039         saddr = 0;
0040 
0041     /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
0042      * packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
0043      */
0044     fl4.daddr = iph->daddr;
0045     fl4.saddr = saddr;
0046     fl4.flowi4_tos = RT_TOS(iph->tos);
0047     fl4.flowi4_oif = sk ? sk->sk_bound_dev_if : 0;
0048     fl4.flowi4_l3mdev = l3mdev_master_ifindex(dev);
0049     fl4.flowi4_mark = skb->mark;
0050     fl4.flowi4_flags = flags;
0051     fib4_rules_early_flow_dissect(net, skb, &fl4, &flkeys);
0052     rt = ip_route_output_key(net, &fl4);
0053     if (IS_ERR(rt))
0054         return PTR_ERR(rt);
0055 
0056     /* Drop old route. */
0057     skb_dst_drop(skb);
0058     skb_dst_set(skb, &rt->dst);
0059 
0060     if (skb_dst(skb)->error)
0061         return skb_dst(skb)->error;
0062 
0063 #ifdef CONFIG_XFRM
0064     if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
0065         xfrm_decode_session(skb, flowi4_to_flowi(&fl4), AF_INET) == 0) {
0066         struct dst_entry *dst = skb_dst(skb);
0067         skb_dst_set(skb, NULL);
0068         dst = xfrm_lookup(net, dst, flowi4_to_flowi(&fl4), sk, 0);
0069         if (IS_ERR(dst))
0070             return PTR_ERR(dst);
0071         skb_dst_set(skb, dst);
0072     }
0073 #endif
0074 
0075     /* Change in oif may mean change in hh_len. */
0076     hh_len = skb_dst(skb)->dev->hard_header_len;
0077     if (skb_headroom(skb) < hh_len &&
0078         pskb_expand_head(skb, HH_DATA_ALIGN(hh_len - skb_headroom(skb)),
0079                 0, GFP_ATOMIC))
0080         return -ENOMEM;
0081 
0082     return 0;
0083 }
0084 EXPORT_SYMBOL(ip_route_me_harder);
0085 
0086 int nf_ip_route(struct net *net, struct dst_entry **dst, struct flowi *fl,
0087         bool strict __always_unused)
0088 {
0089     struct rtable *rt = ip_route_output_key(net, &fl->u.ip4);
0090     if (IS_ERR(rt))
0091         return PTR_ERR(rt);
0092     *dst = &rt->dst;
0093     return 0;
0094 }
0095 EXPORT_SYMBOL_GPL(nf_ip_route);