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