1// SPDX-License-Identifier: GPL-2.0-only
2
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>
13
14#include <net/ip6_fib.h>
15#include <net/ip6_route.h>
16
17static int get_ifindex(const struct net_device *dev)
18{
19 return dev ? dev->ifindex : 0;
20}
21
22static 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,
25 struct ipv6hdr *iph)
26{
27 int lookup_flags = 0;
28
29 if (priv->flags & NFTA_FIB_F_DADDR) {
30 fl6->daddr = iph->daddr;
31 fl6->saddr = iph->saddr;
32 } else {
33 if (nft_hook(pkt) == NF_INET_FORWARD &&
34 priv->flags & NFTA_FIB_F_IIF)
35 fl6->flowi6_iif = nft_out(pkt)->ifindex;
36
37 fl6->daddr = iph->saddr;
38 fl6->saddr = iph->daddr;
39 }
40
41 if (ipv6_addr_type(addr: &fl6->daddr) & IPV6_ADDR_LINKLOCAL) {
42 lookup_flags |= RT6_LOOKUP_F_IFACE;
43 fl6->flowi6_oif = get_ifindex(dev: dev ? dev : pkt->skb->dev);
44 }
45
46 if (ipv6_addr_type(addr: &fl6->saddr) & IPV6_ADDR_UNICAST)
47 lookup_flags |= RT6_LOOKUP_F_HAS_SADDR;
48
49 if (priv->flags & NFTA_FIB_F_MARK)
50 fl6->flowi6_mark = pkt->skb->mark;
51
52 fl6->flowlabel = (*(__be32 *)iph) & IPV6_FLOWINFO_MASK;
53 fl6->flowi6_l3mdev = nft_fib_l3mdev_master_ifindex_rcu(pkt, iif: dev);
54
55 return lookup_flags;
56}
57
58static u32 __nft_fib6_eval_type(const struct nft_fib *priv,
59 const struct nft_pktinfo *pkt,
60 struct ipv6hdr *iph)
61{
62 const struct net_device *dev = NULL;
63 int route_err, addrtype;
64 struct rt6_info *rt;
65 struct flowi6 fl6 = {
66 .flowi6_iif = LOOPBACK_IFINDEX,
67 .flowi6_proto = pkt->tprot,
68 .flowi6_uid = sock_net_uid(net: nft_net(pkt), NULL),
69 };
70 u32 ret = 0;
71
72 if (priv->flags & NFTA_FIB_F_IIF)
73 dev = nft_in(pkt);
74 else if (priv->flags & NFTA_FIB_F_OIF)
75 dev = nft_out(pkt);
76
77 nft_fib6_flowi_init(fl6: &fl6, priv, pkt, dev, iph);
78
79 if (dev && nf_ipv6_chk_addr(net: nft_net(pkt), addr: &fl6.daddr, dev, strict: true))
80 ret = RTN_LOCAL;
81
82 route_err = nf_ip6_route(net: nft_net(pkt), dst: (struct dst_entry **)&rt,
83 fl: flowi6_to_flowi(fl6: &fl6), strict: false);
84 if (route_err)
85 goto err;
86
87 if (rt->rt6i_flags & RTF_REJECT) {
88 route_err = rt->dst.error;
89 dst_release(dst: &rt->dst);
90 goto err;
91 }
92
93 if (ipv6_anycast_destination(dst: (struct dst_entry *)rt, daddr: &fl6.daddr))
94 ret = RTN_ANYCAST;
95 else if (!dev && rt->rt6i_flags & RTF_LOCAL)
96 ret = RTN_LOCAL;
97
98 dst_release(dst: &rt->dst);
99
100 if (ret)
101 return ret;
102
103 addrtype = ipv6_addr_type(addr: &fl6.daddr);
104
105 if (addrtype & IPV6_ADDR_MULTICAST)
106 return RTN_MULTICAST;
107 if (addrtype & IPV6_ADDR_UNICAST)
108 return RTN_UNICAST;
109
110 return RTN_UNSPEC;
111 err:
112 switch (route_err) {
113 case -EINVAL:
114 return RTN_BLACKHOLE;
115 case -EACCES:
116 return RTN_PROHIBIT;
117 case -EAGAIN:
118 return RTN_THROW;
119 default:
120 break;
121 }
122
123 return RTN_UNREACHABLE;
124}
125
126void nft_fib6_eval_type(const struct nft_expr *expr, struct nft_regs *regs,
127 const struct nft_pktinfo *pkt)
128{
129 const struct nft_fib *priv = nft_expr_priv(expr);
130 int noff = skb_network_offset(skb: pkt->skb);
131 u32 *dest = &regs->data[priv->dreg];
132 struct ipv6hdr *iph, _iph;
133
134 iph = skb_header_pointer(skb: pkt->skb, offset: noff, len: sizeof(_iph), buffer: &_iph);
135 if (!iph) {
136 regs->verdict.code = NFT_BREAK;
137 return;
138 }
139
140 *dest = __nft_fib6_eval_type(priv, pkt, iph);
141}
142EXPORT_SYMBOL_GPL(nft_fib6_eval_type);
143
144static bool nft_fib_v6_skip_icmpv6(const struct sk_buff *skb, u8 next, const struct ipv6hdr *iph)
145{
146 if (likely(next != IPPROTO_ICMPV6))
147 return false;
148
149 if (ipv6_addr_type(addr: &iph->saddr) != IPV6_ADDR_ANY)
150 return false;
151
152 return ipv6_addr_type(addr: &iph->daddr) & IPV6_ADDR_LINKLOCAL;
153}
154
155void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,
156 const struct nft_pktinfo *pkt)
157{
158 const struct nft_fib *priv = nft_expr_priv(expr);
159 int noff = skb_network_offset(skb: pkt->skb);
160 const struct net_device *found = NULL;
161 const struct net_device *oif = NULL;
162 u32 *dest = &regs->data[priv->dreg];
163 struct ipv6hdr *iph, _iph;
164 struct flowi6 fl6 = {
165 .flowi6_iif = LOOPBACK_IFINDEX,
166 .flowi6_proto = pkt->tprot,
167 .flowi6_uid = sock_net_uid(net: nft_net(pkt), NULL),
168 };
169 struct rt6_info *rt;
170 int lookup_flags;
171
172 if (nft_fib_can_skip(pkt)) {
173 nft_fib_store_result(reg: dest, priv, dev: nft_in(pkt));
174 return;
175 }
176
177 if (priv->flags & NFTA_FIB_F_IIF)
178 oif = nft_in(pkt);
179 else if (priv->flags & NFTA_FIB_F_OIF)
180 oif = nft_out(pkt);
181
182 iph = skb_header_pointer(skb: pkt->skb, offset: noff, len: sizeof(_iph), buffer: &_iph);
183 if (!iph) {
184 regs->verdict.code = NFT_BREAK;
185 return;
186 }
187
188 if (nft_fib_v6_skip_icmpv6(skb: pkt->skb, next: pkt->tprot, iph)) {
189 nft_fib_store_result(reg: dest, priv, dev: nft_in(pkt));
190 return;
191 }
192
193 lookup_flags = nft_fib6_flowi_init(fl6: &fl6, priv, pkt, dev: oif, iph);
194
195 *dest = 0;
196 rt = (void *)ip6_route_lookup(net: nft_net(pkt), fl6: &fl6, skb: pkt->skb,
197 flags: lookup_flags);
198 if (rt->dst.error)
199 goto put_rt_err;
200
201 /* Should not see RTF_LOCAL here */
202 if (rt->rt6i_flags & (RTF_REJECT | RTF_ANYCAST | RTF_LOCAL))
203 goto put_rt_err;
204
205 if (!oif) {
206 found = rt->rt6i_idev->dev;
207 } else {
208 if (oif == rt->rt6i_idev->dev ||
209 l3mdev_master_ifindex_rcu(dev: rt->rt6i_idev->dev) == oif->ifindex)
210 found = oif;
211 }
212
213 nft_fib_store_result(reg: dest, priv, dev: found);
214 put_rt_err:
215 ip6_rt_put(rt);
216}
217EXPORT_SYMBOL_GPL(nft_fib6_eval);
218
219static struct nft_expr_type nft_fib6_type;
220
221static const struct nft_expr_ops nft_fib6_type_ops = {
222 .type = &nft_fib6_type,
223 .size = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
224 .eval = nft_fib6_eval_type,
225 .init = nft_fib_init,
226 .dump = nft_fib_dump,
227 .validate = nft_fib_validate,
228 .reduce = nft_fib_reduce,
229};
230
231static const struct nft_expr_ops nft_fib6_ops = {
232 .type = &nft_fib6_type,
233 .size = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
234 .eval = nft_fib6_eval,
235 .init = nft_fib_init,
236 .dump = nft_fib_dump,
237 .validate = nft_fib_validate,
238 .reduce = nft_fib_reduce,
239};
240
241static const struct nft_expr_ops *
242nft_fib6_select_ops(const struct nft_ctx *ctx,
243 const struct nlattr * const tb[])
244{
245 enum nft_fib_result result;
246
247 if (!tb[NFTA_FIB_RESULT])
248 return ERR_PTR(error: -EINVAL);
249
250 result = ntohl(nla_get_be32(tb[NFTA_FIB_RESULT]));
251
252 switch (result) {
253 case NFT_FIB_RESULT_OIF:
254 return &nft_fib6_ops;
255 case NFT_FIB_RESULT_OIFNAME:
256 return &nft_fib6_ops;
257 case NFT_FIB_RESULT_ADDRTYPE:
258 return &nft_fib6_type_ops;
259 default:
260 return ERR_PTR(error: -EOPNOTSUPP);
261 }
262}
263
264static struct nft_expr_type nft_fib6_type __read_mostly = {
265 .name = "fib",
266 .select_ops = nft_fib6_select_ops,
267 .policy = nft_fib_policy,
268 .maxattr = NFTA_FIB_MAX,
269 .family = NFPROTO_IPV6,
270 .owner = THIS_MODULE,
271};
272
273static int __init nft_fib6_module_init(void)
274{
275 return nft_register_expr(&nft_fib6_type);
276}
277
278static void __exit nft_fib6_module_exit(void)
279{
280 nft_unregister_expr(&nft_fib6_type);
281}
282module_init(nft_fib6_module_init);
283module_exit(nft_fib6_module_exit);
284
285MODULE_LICENSE("GPL");
286MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
287MODULE_ALIAS_NFT_AF_EXPR(10, "fib");
288MODULE_DESCRIPTION("nftables fib / ipv6 route lookup support");
289

source code of linux/net/ipv6/netfilter/nft_fib_ipv6.c