1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/netlink.h>
7 #include <linux/netfilter.h>
8 #include <linux/netfilter/nf_tables.h>
9 #include <linux/netfilter_ipv6.h>
10 #include <net/netfilter/nf_tables_core.h>
11 #include <net/netfilter/nf_tables.h>
12 #include <net/netfilter/nft_fib.h>
14 #include <net/ip6_fib.h>
15 #include <net/ip6_route.h>
17 static int get_ifindex(const struct net_device *dev)
19 return dev ? dev->ifindex : 0;
22 static int nft_fib6_flowi_init(struct flowi6 *fl6, const struct nft_fib *priv,
23 const struct nft_pktinfo *pkt,
24 const struct net_device *dev,
29 if (priv->flags & NFTA_FIB_F_DADDR) {
30 fl6->daddr = iph->daddr;
31 fl6->saddr = iph->saddr;
33 fl6->daddr = iph->saddr;
34 fl6->saddr = iph->daddr;
37 if (ipv6_addr_type(&fl6->daddr) & IPV6_ADDR_LINKLOCAL) {
38 lookup_flags |= RT6_LOOKUP_F_IFACE;
39 fl6->flowi6_oif = get_ifindex(dev ? dev : pkt->skb->dev);
40 } else if ((priv->flags & NFTA_FIB_F_IIF) &&
41 (netif_is_l3_master(dev) || netif_is_l3_slave(dev))) {
42 fl6->flowi6_oif = dev->ifindex;
45 if (ipv6_addr_type(&fl6->saddr) & IPV6_ADDR_UNICAST)
46 lookup_flags |= RT6_LOOKUP_F_HAS_SADDR;
48 if (priv->flags & NFTA_FIB_F_MARK)
49 fl6->flowi6_mark = pkt->skb->mark;
51 fl6->flowlabel = (*(__be32 *)iph) & IPV6_FLOWINFO_MASK;
56 static u32 __nft_fib6_eval_type(const struct nft_fib *priv,
57 const struct nft_pktinfo *pkt,
60 const struct net_device *dev = NULL;
61 int route_err, addrtype;
64 .flowi6_iif = LOOPBACK_IFINDEX,
65 .flowi6_proto = pkt->tprot,
69 if (priv->flags & NFTA_FIB_F_IIF)
71 else if (priv->flags & NFTA_FIB_F_OIF)
74 nft_fib6_flowi_init(&fl6, priv, pkt, dev, iph);
76 if (dev && nf_ipv6_chk_addr(nft_net(pkt), &fl6.daddr, dev, true))
79 route_err = nf_ip6_route(nft_net(pkt), (struct dst_entry **)&rt,
80 flowi6_to_flowi(&fl6), false);
84 if (rt->rt6i_flags & RTF_REJECT) {
85 route_err = rt->dst.error;
86 dst_release(&rt->dst);
90 if (ipv6_anycast_destination((struct dst_entry *)rt, &fl6.daddr))
92 else if (!dev && rt->rt6i_flags & RTF_LOCAL)
95 dst_release(&rt->dst);
100 addrtype = ipv6_addr_type(&fl6.daddr);
102 if (addrtype & IPV6_ADDR_MULTICAST)
103 return RTN_MULTICAST;
104 if (addrtype & IPV6_ADDR_UNICAST)
111 return RTN_BLACKHOLE;
120 return RTN_UNREACHABLE;
123 void nft_fib6_eval_type(const struct nft_expr *expr, struct nft_regs *regs,
124 const struct nft_pktinfo *pkt)
126 const struct nft_fib *priv = nft_expr_priv(expr);
127 int noff = skb_network_offset(pkt->skb);
128 u32 *dest = ®s->data[priv->dreg];
129 struct ipv6hdr *iph, _iph;
131 iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
133 regs->verdict.code = NFT_BREAK;
137 *dest = __nft_fib6_eval_type(priv, pkt, iph);
139 EXPORT_SYMBOL_GPL(nft_fib6_eval_type);
141 static bool nft_fib_v6_skip_icmpv6(const struct sk_buff *skb, u8 next, const struct ipv6hdr *iph)
143 if (likely(next != IPPROTO_ICMPV6))
146 if (ipv6_addr_type(&iph->saddr) != IPV6_ADDR_ANY)
149 return ipv6_addr_type(&iph->daddr) & IPV6_ADDR_LINKLOCAL;
152 void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,
153 const struct nft_pktinfo *pkt)
155 const struct nft_fib *priv = nft_expr_priv(expr);
156 int noff = skb_network_offset(pkt->skb);
157 const struct net_device *oif = NULL;
158 u32 *dest = ®s->data[priv->dreg];
159 struct ipv6hdr *iph, _iph;
160 struct flowi6 fl6 = {
161 .flowi6_iif = LOOPBACK_IFINDEX,
162 .flowi6_proto = pkt->tprot,
167 if (priv->flags & NFTA_FIB_F_IIF)
169 else if (priv->flags & NFTA_FIB_F_OIF)
172 iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
174 regs->verdict.code = NFT_BREAK;
178 lookup_flags = nft_fib6_flowi_init(&fl6, priv, pkt, oif, iph);
180 if (nft_hook(pkt) == NF_INET_PRE_ROUTING ||
181 nft_hook(pkt) == NF_INET_INGRESS) {
182 if (nft_fib_is_loopback(pkt->skb, nft_in(pkt)) ||
183 nft_fib_v6_skip_icmpv6(pkt->skb, pkt->tprot, iph)) {
184 nft_fib_store_result(dest, priv, nft_in(pkt));
190 rt = (void *)ip6_route_lookup(nft_net(pkt), &fl6, pkt->skb,
195 /* Should not see RTF_LOCAL here */
196 if (rt->rt6i_flags & (RTF_REJECT | RTF_ANYCAST | RTF_LOCAL))
199 if (oif && oif != rt->rt6i_idev->dev &&
200 l3mdev_master_ifindex_rcu(rt->rt6i_idev->dev) != oif->ifindex)
203 nft_fib_store_result(dest, priv, rt->rt6i_idev->dev);
207 EXPORT_SYMBOL_GPL(nft_fib6_eval);
209 static struct nft_expr_type nft_fib6_type;
211 static const struct nft_expr_ops nft_fib6_type_ops = {
212 .type = &nft_fib6_type,
213 .size = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
214 .eval = nft_fib6_eval_type,
215 .init = nft_fib_init,
216 .dump = nft_fib_dump,
217 .validate = nft_fib_validate,
220 static const struct nft_expr_ops nft_fib6_ops = {
221 .type = &nft_fib6_type,
222 .size = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
223 .eval = nft_fib6_eval,
224 .init = nft_fib_init,
225 .dump = nft_fib_dump,
226 .validate = nft_fib_validate,
229 static const struct nft_expr_ops *
230 nft_fib6_select_ops(const struct nft_ctx *ctx,
231 const struct nlattr * const tb[])
233 enum nft_fib_result result;
235 if (!tb[NFTA_FIB_RESULT])
236 return ERR_PTR(-EINVAL);
238 result = ntohl(nla_get_be32(tb[NFTA_FIB_RESULT]));
241 case NFT_FIB_RESULT_OIF:
242 return &nft_fib6_ops;
243 case NFT_FIB_RESULT_OIFNAME:
244 return &nft_fib6_ops;
245 case NFT_FIB_RESULT_ADDRTYPE:
246 return &nft_fib6_type_ops;
248 return ERR_PTR(-EOPNOTSUPP);
252 static struct nft_expr_type nft_fib6_type __read_mostly = {
254 .select_ops = nft_fib6_select_ops,
255 .policy = nft_fib_policy,
256 .maxattr = NFTA_FIB_MAX,
257 .family = NFPROTO_IPV6,
258 .owner = THIS_MODULE,
261 static int __init nft_fib6_module_init(void)
263 return nft_register_expr(&nft_fib6_type);
266 static void __exit nft_fib6_module_exit(void)
268 nft_unregister_expr(&nft_fib6_type);
270 module_init(nft_fib6_module_init);
271 module_exit(nft_fib6_module_exit);
273 MODULE_LICENSE("GPL");
274 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
275 MODULE_ALIAS_NFT_AF_EXPR(10, "fib");
276 MODULE_DESCRIPTION("nftables fib / ipv6 route lookup support");