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