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.
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>
17 #include <net/ip_fib.h>
18 #include <net/route.h>
20 /* don't try to find route from mcast/bcast/zeronet */
21 static __be32 get_saddr(__be32 addr)
23 if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr) ||
24 ipv4_is_zeronet(addr))
29 static bool fib4_is_local(const struct sk_buff *skb)
31 const struct rtable *rt = skb_rtable(skb);
33 return rt && (rt->rt_flags & RTCF_LOCAL);
36 #define DSCP_BITS 0xfc
38 void nft_fib4_eval_type(const struct nft_expr *expr, struct nft_regs *regs,
39 const struct nft_pktinfo *pkt)
41 const struct nft_fib *priv = nft_expr_priv(expr);
42 u32 *dst = ®s->data[priv->dreg];
43 const struct net_device *dev = NULL;
44 const struct iphdr *iph;
47 if (priv->flags & NFTA_FIB_F_IIF)
49 else if (priv->flags & NFTA_FIB_F_OIF)
52 iph = ip_hdr(pkt->skb);
53 if (priv->flags & NFTA_FIB_F_DADDR)
58 *dst = inet_dev_addr_type(nft_net(pkt), dev, addr);
60 EXPORT_SYMBOL_GPL(nft_fib4_eval_type);
62 static int get_ifindex(const struct net_device *dev)
64 return dev ? dev->ifindex : 0;
67 void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
68 const struct nft_pktinfo *pkt)
70 const struct nft_fib *priv = nft_expr_priv(expr);
71 u32 *dest = ®s->data[priv->dreg];
72 const struct iphdr *iph;
73 struct fib_result res;
75 .flowi4_scope = RT_SCOPE_UNIVERSE,
76 .flowi4_iif = LOOPBACK_IFINDEX,
78 const struct net_device *oif;
79 struct net_device *found;
80 #ifdef CONFIG_IP_ROUTE_MULTIPATH
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.
89 * Search results for the desired outinterface instead.
91 if (priv->flags & NFTA_FIB_F_OIF)
93 else if (priv->flags & NFTA_FIB_F_IIF)
98 if (nft_hook(pkt) == NF_INET_PRE_ROUTING && fib4_is_local(pkt->skb)) {
99 nft_fib_store_result(dest, priv->result, pkt, LOOPBACK_IFINDEX);
103 iph = ip_hdr(pkt->skb);
104 if (ipv4_is_zeronet(iph->saddr)) {
105 if (ipv4_is_lbcast(iph->daddr) ||
106 ipv4_is_local_multicast(iph->daddr)) {
107 nft_fib_store_result(dest, priv->result, pkt,
108 get_ifindex(pkt->skb->dev));
113 if (priv->flags & NFTA_FIB_F_MARK)
114 fl4.flowi4_mark = pkt->skb->mark;
116 fl4.flowi4_tos = iph->tos & DSCP_BITS;
118 if (priv->flags & NFTA_FIB_F_DADDR) {
119 fl4.daddr = iph->daddr;
120 fl4.saddr = get_saddr(iph->saddr);
122 fl4.daddr = iph->saddr;
123 fl4.saddr = get_saddr(iph->daddr);
128 if (fib_lookup(nft_net(pkt), &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE))
134 case RTN_LOCAL: /* should not appear here, see fib4_is_local() above */
141 found = FIB_RES_DEV(res);
145 #ifdef CONFIG_IP_ROUTE_MULTIPATH
146 for (i = 0; i < res.fi->fib_nhs; i++) {
147 struct fib_nh *nh = &res.fi->fib_nh[i];
149 if (nh->nh_dev == oif) {
156 found = FIB_RES_DEV(res);
161 switch (priv->result) {
162 case NFT_FIB_RESULT_OIF:
163 *dest = found->ifindex;
165 case NFT_FIB_RESULT_OIFNAME:
166 strncpy((char *)dest, found->name, IFNAMSIZ);
173 EXPORT_SYMBOL_GPL(nft_fib4_eval);
175 static struct nft_expr_type nft_fib4_type;
177 static const struct nft_expr_ops nft_fib4_type_ops = {
178 .type = &nft_fib4_type,
179 .size = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
180 .eval = nft_fib4_eval_type,
181 .init = nft_fib_init,
182 .dump = nft_fib_dump,
183 .validate = nft_fib_validate,
186 static const struct nft_expr_ops nft_fib4_ops = {
187 .type = &nft_fib4_type,
188 .size = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
189 .eval = nft_fib4_eval,
190 .init = nft_fib_init,
191 .dump = nft_fib_dump,
192 .validate = nft_fib_validate,
195 static const struct nft_expr_ops *
196 nft_fib4_select_ops(const struct nft_ctx *ctx,
197 const struct nlattr * const tb[])
199 enum nft_fib_result result;
201 if (!tb[NFTA_FIB_RESULT])
202 return ERR_PTR(-EINVAL);
204 result = ntohl(nla_get_be32(tb[NFTA_FIB_RESULT]));
207 case NFT_FIB_RESULT_OIF:
208 return &nft_fib4_ops;
209 case NFT_FIB_RESULT_OIFNAME:
210 return &nft_fib4_ops;
211 case NFT_FIB_RESULT_ADDRTYPE:
212 return &nft_fib4_type_ops;
214 return ERR_PTR(-EOPNOTSUPP);
218 static struct nft_expr_type nft_fib4_type __read_mostly = {
220 .select_ops = &nft_fib4_select_ops,
221 .policy = nft_fib_policy,
222 .maxattr = NFTA_FIB_MAX,
223 .family = NFPROTO_IPV4,
224 .owner = THIS_MODULE,
227 static int __init nft_fib4_module_init(void)
229 return nft_register_expr(&nft_fib4_type);
232 static void __exit nft_fib4_module_exit(void)
234 nft_unregister_expr(&nft_fib4_type);
237 module_init(nft_fib4_module_init);
238 module_exit(nft_fib4_module_exit);
239 MODULE_LICENSE("GPL");
240 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
241 MODULE_ALIAS_NFT_AF_EXPR(2, "fib");