GNU Linux-libre 4.19.286-gnu1
[releases.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 static int get_ifindex(const struct net_device *dev)
62 {
63         return dev ? dev->ifindex : 0;
64 }
65
66 void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
67                    const struct nft_pktinfo *pkt)
68 {
69         const struct nft_fib *priv = nft_expr_priv(expr);
70         int noff = skb_network_offset(pkt->skb);
71         u32 *dest = &regs->data[priv->dreg];
72         struct iphdr *iph, _iph;
73         struct fib_result res;
74         struct flowi4 fl4 = {
75                 .flowi4_scope = RT_SCOPE_UNIVERSE,
76                 .flowi4_iif = LOOPBACK_IFINDEX,
77         };
78         const struct net_device *oif;
79         struct net_device *found;
80 #ifdef CONFIG_IP_ROUTE_MULTIPATH
81         int i;
82 #endif
83
84         /*
85          * Do not set flowi4_oif, it restricts results (for example, asking
86          * for oif 3 will get RTN_UNICAST result even if the daddr exits
87          * on another interface.
88          *
89          * Search results for the desired outinterface instead.
90          */
91         if (priv->flags & NFTA_FIB_F_OIF)
92                 oif = nft_out(pkt);
93         else if (priv->flags & NFTA_FIB_F_IIF)
94                 oif = nft_in(pkt);
95         else
96                 oif = NULL;
97
98         if (priv->flags & NFTA_FIB_F_IIF)
99                 fl4.flowi4_oif = l3mdev_master_ifindex_rcu(oif);
100
101         if (nft_hook(pkt) == NF_INET_PRE_ROUTING &&
102             nft_fib_is_loopback(pkt->skb, nft_in(pkt))) {
103                 nft_fib_store_result(dest, priv, pkt,
104                                      nft_in(pkt)->ifindex);
105                 return;
106         }
107
108         iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
109         if (!iph) {
110                 regs->verdict.code = NFT_BREAK;
111                 return;
112         }
113
114         if (ipv4_is_zeronet(iph->saddr)) {
115                 if (ipv4_is_lbcast(iph->daddr) ||
116                     ipv4_is_local_multicast(iph->daddr)) {
117                         nft_fib_store_result(dest, priv, pkt,
118                                              get_ifindex(pkt->skb->dev));
119                         return;
120                 }
121         }
122
123         if (priv->flags & NFTA_FIB_F_MARK)
124                 fl4.flowi4_mark = pkt->skb->mark;
125
126         fl4.flowi4_tos = iph->tos & DSCP_BITS;
127
128         if (priv->flags & NFTA_FIB_F_DADDR) {
129                 fl4.daddr = iph->daddr;
130                 fl4.saddr = get_saddr(iph->saddr);
131         } else {
132                 fl4.daddr = iph->saddr;
133                 fl4.saddr = get_saddr(iph->daddr);
134         }
135
136         *dest = 0;
137
138         if (fib_lookup(nft_net(pkt), &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE))
139                 return;
140
141         switch (res.type) {
142         case RTN_UNICAST:
143                 break;
144         case RTN_LOCAL: /* Should not see RTN_LOCAL here */
145                 return;
146         default:
147                 break;
148         }
149
150        if (!oif) {
151                found = FIB_RES_DEV(res);
152                goto ok;
153        }
154
155 #ifdef CONFIG_IP_ROUTE_MULTIPATH
156         for (i = 0; i < res.fi->fib_nhs; i++) {
157                 struct fib_nh *nh = &res.fi->fib_nh[i];
158
159                 if (nh->nh_dev == oif) {
160                         found = nh->nh_dev;
161                         goto ok;
162                 }
163         }
164         return;
165 #else
166         found = FIB_RES_DEV(res);
167         if (found != oif)
168                 return;
169 #endif
170 ok:
171         switch (priv->result) {
172         case NFT_FIB_RESULT_OIF:
173                 *dest = found->ifindex;
174                 break;
175         case NFT_FIB_RESULT_OIFNAME:
176                 strncpy((char *)dest, found->name, IFNAMSIZ);
177                 break;
178         default:
179                 WARN_ON_ONCE(1);
180                 break;
181         }
182 }
183 EXPORT_SYMBOL_GPL(nft_fib4_eval);
184
185 static struct nft_expr_type nft_fib4_type;
186
187 static const struct nft_expr_ops nft_fib4_type_ops = {
188         .type           = &nft_fib4_type,
189         .size           = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
190         .eval           = nft_fib4_eval_type,
191         .init           = nft_fib_init,
192         .dump           = nft_fib_dump,
193         .validate       = nft_fib_validate,
194 };
195
196 static const struct nft_expr_ops nft_fib4_ops = {
197         .type           = &nft_fib4_type,
198         .size           = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
199         .eval           = nft_fib4_eval,
200         .init           = nft_fib_init,
201         .dump           = nft_fib_dump,
202         .validate       = nft_fib_validate,
203 };
204
205 static const struct nft_expr_ops *
206 nft_fib4_select_ops(const struct nft_ctx *ctx,
207                     const struct nlattr * const tb[])
208 {
209         enum nft_fib_result result;
210
211         if (!tb[NFTA_FIB_RESULT])
212                 return ERR_PTR(-EINVAL);
213
214         result = ntohl(nla_get_be32(tb[NFTA_FIB_RESULT]));
215
216         switch (result) {
217         case NFT_FIB_RESULT_OIF:
218                 return &nft_fib4_ops;
219         case NFT_FIB_RESULT_OIFNAME:
220                 return &nft_fib4_ops;
221         case NFT_FIB_RESULT_ADDRTYPE:
222                 return &nft_fib4_type_ops;
223         default:
224                 return ERR_PTR(-EOPNOTSUPP);
225         }
226 }
227
228 static struct nft_expr_type nft_fib4_type __read_mostly = {
229         .name           = "fib",
230         .select_ops     = nft_fib4_select_ops,
231         .policy         = nft_fib_policy,
232         .maxattr        = NFTA_FIB_MAX,
233         .family         = NFPROTO_IPV4,
234         .owner          = THIS_MODULE,
235 };
236
237 static int __init nft_fib4_module_init(void)
238 {
239         return nft_register_expr(&nft_fib4_type);
240 }
241
242 static void __exit nft_fib4_module_exit(void)
243 {
244         nft_unregister_expr(&nft_fib4_type);
245 }
246
247 module_init(nft_fib4_module_init);
248 module_exit(nft_fib4_module_exit);
249 MODULE_LICENSE("GPL");
250 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
251 MODULE_ALIAS_NFT_AF_EXPR(2, "fib");