xref: /linux/net/netfilter/nf_flow_table_path.c (revision fcee7d82f27d6a8b1ddc5bbefda59b4e441e9bc0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/init.h>
5 #include <linux/etherdevice.h>
6 #include <linux/netlink.h>
7 #include <linux/netfilter.h>
8 #include <linux/spinlock.h>
9 #include <linux/netfilter/nf_conntrack_common.h>
10 #include <linux/netfilter/nf_tables.h>
11 #include <net/ip.h>
12 #include <net/inet_dscp.h>
13 #include <net/netfilter/nf_tables.h>
14 #include <net/netfilter/nf_tables_core.h>
15 #include <net/netfilter/nf_conntrack_core.h>
16 #include <net/netfilter/nf_conntrack_extend.h>
17 #include <net/netfilter/nf_flow_table.h>
18 
nft_xmit_type(struct dst_entry * dst)19 static enum flow_offload_xmit_type nft_xmit_type(struct dst_entry *dst)
20 {
21 	if (dst_xfrm(dst))
22 		return FLOW_OFFLOAD_XMIT_XFRM;
23 
24 	return FLOW_OFFLOAD_XMIT_NEIGH;
25 }
26 
nft_default_forward_path(struct nf_flow_route * route,struct dst_entry * dst_cache,enum ip_conntrack_dir dir)27 static void nft_default_forward_path(struct nf_flow_route *route,
28 				     struct dst_entry *dst_cache,
29 				     enum ip_conntrack_dir dir)
30 {
31 	route->tuple[!dir].in.ifindex	= dst_cache->dev->ifindex;
32 	route->tuple[dir].dst		= dst_cache;
33 	route->tuple[dir].xmit_type	= nft_xmit_type(dst_cache);
34 }
35 
nft_is_valid_ether_device(const struct net_device * dev)36 static bool nft_is_valid_ether_device(const struct net_device *dev)
37 {
38 	if (!dev || (dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
39 	    dev->addr_len != ETH_ALEN || !is_valid_ether_addr(dev->dev_addr))
40 		return false;
41 
42 	return true;
43 }
44 
nft_dev_fill_forward_path(const struct nf_flow_route * route,const struct dst_entry * dst_cache,const struct nf_conn * ct,enum ip_conntrack_dir dir,u8 * ha,struct net_device_path_stack * stack)45 static int nft_dev_fill_forward_path(const struct nf_flow_route *route,
46 				     const struct dst_entry *dst_cache,
47 				     const struct nf_conn *ct,
48 				     enum ip_conntrack_dir dir, u8 *ha,
49 				     struct net_device_path_stack *stack)
50 {
51 	const void *daddr = &ct->tuplehash[!dir].tuple.src.u3;
52 	struct net_device *dev = dst_cache->dev;
53 	struct neighbour *n;
54 	u8 nud_state;
55 
56 	if (!nft_is_valid_ether_device(dev))
57 		goto out;
58 
59 	n = dst_neigh_lookup(dst_cache, daddr);
60 	if (!n)
61 		return -1;
62 
63 	read_lock_bh(&n->lock);
64 	nud_state = n->nud_state;
65 	ether_addr_copy(ha, n->ha);
66 	read_unlock_bh(&n->lock);
67 	neigh_release(n);
68 
69 	if (!(nud_state & NUD_VALID))
70 		return -1;
71 
72 out:
73 	return dev_fill_forward_path(dev, ha, stack);
74 }
75 
76 struct nft_forward_info {
77 	const struct net_device *indev;
78 	const struct net_device *outdev;
79 	struct id {
80 		__u16	id;
81 		__be16	proto;
82 	} encap[NF_FLOW_TABLE_ENCAP_MAX];
83 	u8 num_encaps;
84 	struct flow_offload_tunnel tun;
85 	u8 num_tuns;
86 	u8 ingress_vlans;
87 	u8 h_source[ETH_ALEN];
88 	u8 h_dest[ETH_ALEN];
89 	bool needs_gso_segment;
90 	enum flow_offload_xmit_type xmit_type;
91 };
92 
nft_dev_path_info(const struct net_device_path_stack * stack,struct nft_forward_info * info,unsigned char * ha,struct nf_flowtable * flowtable)93 static void nft_dev_path_info(const struct net_device_path_stack *stack,
94 			      struct nft_forward_info *info,
95 			      unsigned char *ha, struct nf_flowtable *flowtable)
96 {
97 	const struct net_device_path *path;
98 	int i;
99 
100 	memcpy(info->h_dest, ha, ETH_ALEN);
101 
102 	for (i = 0; i < stack->num_paths; i++) {
103 		path = &stack->path[i];
104 		switch (path->type) {
105 		case DEV_PATH_ETHERNET:
106 		case DEV_PATH_DSA:
107 		case DEV_PATH_VLAN:
108 		case DEV_PATH_PPPOE:
109 		case DEV_PATH_TUN:
110 			info->indev = path->dev;
111 			if (is_zero_ether_addr(info->h_source))
112 				memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
113 
114 			if (path->type == DEV_PATH_ETHERNET)
115 				break;
116 			if (path->type == DEV_PATH_DSA) {
117 				i = stack->num_paths;
118 				break;
119 			}
120 
121 			/* DEV_PATH_VLAN, DEV_PATH_PPPOE and DEV_PATH_TUN */
122 			if (path->type == DEV_PATH_TUN) {
123 				if (info->num_tuns) {
124 					info->indev = NULL;
125 					break;
126 				}
127 				info->tun.src_v6 = path->tun.src_v6;
128 				info->tun.dst_v6 = path->tun.dst_v6;
129 				info->tun.l3_proto = path->tun.l3_proto;
130 				info->num_tuns++;
131 			} else {
132 				if (info->num_encaps >= NF_FLOW_TABLE_ENCAP_MAX) {
133 					info->indev = NULL;
134 					break;
135 				}
136 				info->encap[info->num_encaps].id =
137 					path->encap.id;
138 				info->encap[info->num_encaps].proto =
139 					path->encap.proto;
140 				info->num_encaps++;
141 			}
142 			if (path->type == DEV_PATH_PPPOE) {
143 				memcpy(info->h_dest, path->encap.h_dest, ETH_ALEN);
144 				info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
145 				info->needs_gso_segment = 1;
146 			}
147 			break;
148 		case DEV_PATH_BRIDGE:
149 			if (is_zero_ether_addr(info->h_source))
150 				memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
151 
152 			switch (path->bridge.vlan_mode) {
153 			case DEV_PATH_BR_VLAN_UNTAG_HW:
154 				info->ingress_vlans |= BIT(info->num_encaps - 1);
155 				break;
156 			case DEV_PATH_BR_VLAN_TAG:
157 				if (info->num_encaps >= NF_FLOW_TABLE_ENCAP_MAX) {
158 					info->indev = NULL;
159 					break;
160 				}
161 				info->encap[info->num_encaps].id = path->bridge.vlan_id;
162 				info->encap[info->num_encaps].proto = path->bridge.vlan_proto;
163 				info->num_encaps++;
164 				break;
165 			case DEV_PATH_BR_VLAN_UNTAG:
166 				if (WARN_ON_ONCE(info->num_encaps-- == 0)) {
167 					info->indev = NULL;
168 					break;
169 				}
170 				break;
171 			case DEV_PATH_BR_VLAN_KEEP:
172 				break;
173 			}
174 			info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
175 			break;
176 		default:
177 			info->indev = NULL;
178 			break;
179 		}
180 	}
181 	info->outdev = info->indev;
182 
183 	if (nf_flowtable_hw_offload(flowtable) &&
184 	    nft_is_valid_ether_device(info->indev))
185 		info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
186 }
187 
nft_flowtable_find_dev(const struct net_device * dev,struct nft_flowtable * ft)188 static bool nft_flowtable_find_dev(const struct net_device *dev,
189 				   struct nft_flowtable *ft)
190 {
191 	struct nft_hook *hook;
192 	bool found = false;
193 
194 	list_for_each_entry_rcu(hook, &ft->hook_list, list) {
195 		if (!nft_hook_find_ops_rcu(hook, dev))
196 			continue;
197 
198 		found = true;
199 		break;
200 	}
201 
202 	return found;
203 }
204 
nft_flow_tunnel_update_route(const struct nft_pktinfo * pkt,struct flow_offload_tunnel * tun,struct nf_flow_route * route,enum ip_conntrack_dir dir)205 static int nft_flow_tunnel_update_route(const struct nft_pktinfo *pkt,
206 					struct flow_offload_tunnel *tun,
207 					struct nf_flow_route *route,
208 					enum ip_conntrack_dir dir)
209 {
210 	struct dst_entry *cur_dst = route->tuple[dir].dst;
211 	struct dst_entry *tun_dst = NULL;
212 	struct flowi fl = {};
213 
214 	switch (nft_pf(pkt)) {
215 	case NFPROTO_IPV4:
216 		fl.u.ip4.daddr = tun->dst_v4.s_addr;
217 		fl.u.ip4.saddr = tun->src_v4.s_addr;
218 		fl.u.ip4.flowi4_iif = nft_in(pkt)->ifindex;
219 		fl.u.ip4.flowi4_dscp = ip4h_dscp(ip_hdr(pkt->skb));
220 		fl.u.ip4.flowi4_mark = pkt->skb->mark;
221 		fl.u.ip4.flowi4_flags = FLOWI_FLAG_ANYSRC;
222 		break;
223 	case NFPROTO_IPV6:
224 		fl.u.ip6.daddr = tun->dst_v6;
225 		fl.u.ip6.saddr = tun->src_v6;
226 		fl.u.ip6.flowi6_iif = nft_in(pkt)->ifindex;
227 		fl.u.ip6.flowlabel = ip6_flowinfo(ipv6_hdr(pkt->skb));
228 		fl.u.ip6.flowi6_mark = pkt->skb->mark;
229 		fl.u.ip6.flowi6_flags = FLOWI_FLAG_ANYSRC;
230 		break;
231 	}
232 
233 	nf_route(nft_net(pkt), &tun_dst, &fl, false, nft_pf(pkt));
234 	if (!tun_dst)
235 		return -ENOENT;
236 
237 	route->tuple[dir].dst = tun_dst;
238 	dst_release(cur_dst);
239 
240 	return 0;
241 }
242 
nft_dev_forward_path(const struct nft_pktinfo * pkt,struct nf_flow_route * route,const struct nf_conn * ct,enum ip_conntrack_dir dir,struct nft_flowtable * ft)243 static void nft_dev_forward_path(const struct nft_pktinfo *pkt,
244 				 struct nf_flow_route *route,
245 				 const struct nf_conn *ct,
246 				 enum ip_conntrack_dir dir,
247 				 struct nft_flowtable *ft)
248 {
249 	const struct dst_entry *dst = route->tuple[dir].dst;
250 	struct net_device_path_stack stack;
251 	struct nft_forward_info info = {};
252 	unsigned char ha[ETH_ALEN];
253 	int i;
254 
255 	if (nft_dev_fill_forward_path(route, dst, ct, dir, ha, &stack) >= 0)
256 		nft_dev_path_info(&stack, &info, ha, &ft->data);
257 
258 	if (info.outdev)
259 		route->tuple[dir].out.ifindex = info.outdev->ifindex;
260 
261 	if (!info.indev || !nft_flowtable_find_dev(info.indev, ft))
262 		return;
263 
264 	route->tuple[!dir].in.ifindex = info.indev->ifindex;
265 	for (i = 0; i < info.num_encaps; i++) {
266 		route->tuple[!dir].in.encap[i].id = info.encap[i].id;
267 		route->tuple[!dir].in.encap[i].proto = info.encap[i].proto;
268 	}
269 
270 	if (info.num_tuns &&
271 	    !nft_flow_tunnel_update_route(pkt, &info.tun, route, dir)) {
272 		route->tuple[!dir].in.tun.src_v6 = info.tun.dst_v6;
273 		route->tuple[!dir].in.tun.dst_v6 = info.tun.src_v6;
274 		route->tuple[!dir].in.tun.l3_proto = info.tun.l3_proto;
275 		route->tuple[!dir].in.num_tuns = info.num_tuns;
276 	}
277 
278 	route->tuple[!dir].in.num_encaps = info.num_encaps;
279 	route->tuple[!dir].in.ingress_vlans = info.ingress_vlans;
280 
281 	if (info.xmit_type == FLOW_OFFLOAD_XMIT_DIRECT) {
282 		memcpy(route->tuple[dir].out.h_source, info.h_source, ETH_ALEN);
283 		memcpy(route->tuple[dir].out.h_dest, info.h_dest, ETH_ALEN);
284 		route->tuple[dir].xmit_type = info.xmit_type;
285 	}
286 	route->tuple[dir].out.needs_gso_segment = info.needs_gso_segment;
287 }
288 
nft_flow_route(const struct nft_pktinfo * pkt,const struct nf_conn * ct,struct nf_flow_route * route,enum ip_conntrack_dir dir,struct nft_flowtable * ft)289 int nft_flow_route(const struct nft_pktinfo *pkt, const struct nf_conn *ct,
290 		   struct nf_flow_route *route, enum ip_conntrack_dir dir,
291 		   struct nft_flowtable *ft)
292 {
293 	struct dst_entry *this_dst = skb_dst(pkt->skb);
294 	struct dst_entry *other_dst = NULL;
295 	struct flowi fl;
296 
297 	memset(&fl, 0, sizeof(fl));
298 	switch (nft_pf(pkt)) {
299 	case NFPROTO_IPV4:
300 		fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
301 		fl.u.ip4.saddr = ct->tuplehash[!dir].tuple.src.u3.ip;
302 		fl.u.ip4.flowi4_oif = nft_in(pkt)->ifindex;
303 		fl.u.ip4.flowi4_iif = this_dst->dev->ifindex;
304 		fl.u.ip4.flowi4_dscp = ip4h_dscp(ip_hdr(pkt->skb));
305 		fl.u.ip4.flowi4_mark = pkt->skb->mark;
306 		fl.u.ip4.flowi4_flags = FLOWI_FLAG_ANYSRC;
307 		break;
308 	case NFPROTO_IPV6:
309 		fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
310 		fl.u.ip6.saddr = ct->tuplehash[!dir].tuple.src.u3.in6;
311 		fl.u.ip6.flowi6_oif = nft_in(pkt)->ifindex;
312 		fl.u.ip6.flowi6_iif = this_dst->dev->ifindex;
313 		fl.u.ip6.flowlabel = ip6_flowinfo(ipv6_hdr(pkt->skb));
314 		fl.u.ip6.flowi6_mark = pkt->skb->mark;
315 		fl.u.ip6.flowi6_flags = FLOWI_FLAG_ANYSRC;
316 		break;
317 	}
318 
319 	if (!dst_hold_safe(this_dst))
320 		return -ENOENT;
321 
322 	nf_route(nft_net(pkt), &other_dst, &fl, false, nft_pf(pkt));
323 	if (!other_dst) {
324 		dst_release(this_dst);
325 		return -ENOENT;
326 	}
327 
328 	nft_default_forward_path(route, this_dst, dir);
329 	nft_default_forward_path(route, other_dst, !dir);
330 
331 	if (route->tuple[dir].xmit_type	== FLOW_OFFLOAD_XMIT_NEIGH)
332 		nft_dev_forward_path(pkt, route, ct, dir, ft);
333 	if (route->tuple[!dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH)
334 		nft_dev_forward_path(pkt, route, ct, !dir, ft);
335 
336 	return 0;
337 }
338 EXPORT_SYMBOL_GPL(nft_flow_route);
339