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