ARC: [plat-hsdk]: unify memory apertures configuration
[linux-2.6-microblaze.git] / net / ipv4 / netfilter / nft_fib_ipv4.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation.
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/netlink.h>
11 #include <linux/netfilter.h>
12 #include <linux/netfilter/nf_tables.h>
13 #include <net/netfilter/nf_tables_core.h>
14 #include <net/netfilter/nf_tables.h>
15 #include <net/netfilter/nft_fib.h>
16
17 #include <net/ip_fib.h>
18 #include <net/route.h>
19
20 /* don't try to find route from mcast/bcast/zeronet */
21 static __be32 get_saddr(__be32 addr)
22 {
23         if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr) ||
24             ipv4_is_zeronet(addr))
25                 return 0;
26         return addr;
27 }
28
29 #define DSCP_BITS     0xfc
30
31 void nft_fib4_eval_type(const struct nft_expr *expr, struct nft_regs *regs,
32                         const struct nft_pktinfo *pkt)
33 {
34         const struct nft_fib *priv = nft_expr_priv(expr);
35         int noff = skb_network_offset(pkt->skb);
36         u32 *dst = &regs->data[priv->dreg];
37         const struct net_device *dev = NULL;
38         struct iphdr *iph, _iph;
39         __be32 addr;
40
41         if (priv->flags & NFTA_FIB_F_IIF)
42                 dev = nft_in(pkt);
43         else if (priv->flags & NFTA_FIB_F_OIF)
44                 dev = nft_out(pkt);
45
46         iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
47         if (!iph) {
48                 regs->verdict.code = NFT_BREAK;
49                 return;
50         }
51
52         if (priv->flags & NFTA_FIB_F_DADDR)
53                 addr = iph->daddr;
54         else
55                 addr = iph->saddr;
56
57         *dst = inet_dev_addr_type(nft_net(pkt), dev, addr);
58 }
59 EXPORT_SYMBOL_GPL(nft_fib4_eval_type);
60
61 void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
62                    const struct nft_pktinfo *pkt)
63 {
64         const struct nft_fib *priv = nft_expr_priv(expr);
65         int noff = skb_network_offset(pkt->skb);
66         u32 *dest = &regs->data[priv->dreg];
67         struct iphdr *iph, _iph;
68         struct fib_result res;
69         struct flowi4 fl4 = {
70                 .flowi4_scope = RT_SCOPE_UNIVERSE,
71                 .flowi4_iif = LOOPBACK_IFINDEX,
72         };
73         const struct net_device *oif;
74         const struct net_device *found;
75
76         /*
77          * Do not set flowi4_oif, it restricts results (for example, asking
78          * for oif 3 will get RTN_UNICAST result even if the daddr exits
79          * on another interface.
80          *
81          * Search results for the desired outinterface instead.
82          */
83         if (priv->flags & NFTA_FIB_F_OIF)
84                 oif = nft_out(pkt);
85         else if (priv->flags & NFTA_FIB_F_IIF)
86                 oif = nft_in(pkt);
87         else
88                 oif = NULL;
89
90         if (nft_hook(pkt) == NF_INET_PRE_ROUTING &&
91             nft_fib_is_loopback(pkt->skb, nft_in(pkt))) {
92                 nft_fib_store_result(dest, priv, nft_in(pkt));
93                 return;
94         }
95
96         iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
97         if (!iph) {
98                 regs->verdict.code = NFT_BREAK;
99                 return;
100         }
101
102         if (ipv4_is_zeronet(iph->saddr)) {
103                 if (ipv4_is_lbcast(iph->daddr) ||
104                     ipv4_is_local_multicast(iph->daddr)) {
105                         nft_fib_store_result(dest, priv, pkt->skb->dev);
106                         return;
107                 }
108         }
109
110         if (priv->flags & NFTA_FIB_F_MARK)
111                 fl4.flowi4_mark = pkt->skb->mark;
112
113         fl4.flowi4_tos = iph->tos & DSCP_BITS;
114
115         if (priv->flags & NFTA_FIB_F_DADDR) {
116                 fl4.daddr = iph->daddr;
117                 fl4.saddr = get_saddr(iph->saddr);
118         } else {
119                 fl4.daddr = iph->saddr;
120                 fl4.saddr = get_saddr(iph->daddr);
121         }
122
123         *dest = 0;
124
125         if (fib_lookup(nft_net(pkt), &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE))
126                 return;
127
128         switch (res.type) {
129         case RTN_UNICAST:
130                 break;
131         case RTN_LOCAL: /* Should not see RTN_LOCAL here */
132                 return;
133         default:
134                 break;
135         }
136
137        if (!oif) {
138                found = FIB_RES_DEV(res);
139         } else {
140                 if (!fib_info_nh_uses_dev(res.fi, oif))
141                         return;
142
143                 found = oif;
144         }
145
146         nft_fib_store_result(dest, priv, found);
147 }
148 EXPORT_SYMBOL_GPL(nft_fib4_eval);
149
150 static struct nft_expr_type nft_fib4_type;
151
152 static const struct nft_expr_ops nft_fib4_type_ops = {
153         .type           = &nft_fib4_type,
154         .size           = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
155         .eval           = nft_fib4_eval_type,
156         .init           = nft_fib_init,
157         .dump           = nft_fib_dump,
158         .validate       = nft_fib_validate,
159 };
160
161 static const struct nft_expr_ops nft_fib4_ops = {
162         .type           = &nft_fib4_type,
163         .size           = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
164         .eval           = nft_fib4_eval,
165         .init           = nft_fib_init,
166         .dump           = nft_fib_dump,
167         .validate       = nft_fib_validate,
168 };
169
170 static const struct nft_expr_ops *
171 nft_fib4_select_ops(const struct nft_ctx *ctx,
172                     const struct nlattr * const tb[])
173 {
174         enum nft_fib_result result;
175
176         if (!tb[NFTA_FIB_RESULT])
177                 return ERR_PTR(-EINVAL);
178
179         result = ntohl(nla_get_be32(tb[NFTA_FIB_RESULT]));
180
181         switch (result) {
182         case NFT_FIB_RESULT_OIF:
183                 return &nft_fib4_ops;
184         case NFT_FIB_RESULT_OIFNAME:
185                 return &nft_fib4_ops;
186         case NFT_FIB_RESULT_ADDRTYPE:
187                 return &nft_fib4_type_ops;
188         default:
189                 return ERR_PTR(-EOPNOTSUPP);
190         }
191 }
192
193 static struct nft_expr_type nft_fib4_type __read_mostly = {
194         .name           = "fib",
195         .select_ops     = nft_fib4_select_ops,
196         .policy         = nft_fib_policy,
197         .maxattr        = NFTA_FIB_MAX,
198         .family         = NFPROTO_IPV4,
199         .owner          = THIS_MODULE,
200 };
201
202 static int __init nft_fib4_module_init(void)
203 {
204         return nft_register_expr(&nft_fib4_type);
205 }
206
207 static void __exit nft_fib4_module_exit(void)
208 {
209         nft_unregister_expr(&nft_fib4_type);
210 }
211
212 module_init(nft_fib4_module_init);
213 module_exit(nft_fib4_module_exit);
214 MODULE_LICENSE("GPL");
215 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
216 MODULE_ALIAS_NFT_AF_EXPR(2, "fib");