xref: /linux/net/netfilter/nf_flow_table_path.c (revision 6574f01ef95dd9029a0230f4f56a62f93fdd8319)
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 	enum flow_offload_xmit_type xmit_type;
90 };
91 
nft_dev_path_info(const struct net_device_path_stack * stack,struct nft_forward_info * info,unsigned char * ha,struct nf_flowtable * flowtable)92 static void nft_dev_path_info(const struct net_device_path_stack *stack,
93 			      struct nft_forward_info *info,
94 			      unsigned char *ha, struct nf_flowtable *flowtable)
95 {
96 	const struct net_device_path *path;
97 	int i;
98 
99 	memcpy(info->h_dest, ha, ETH_ALEN);
100 
101 	for (i = 0; i < stack->num_paths; i++) {
102 		path = &stack->path[i];
103 		switch (path->type) {
104 		case DEV_PATH_ETHERNET:
105 		case DEV_PATH_DSA:
106 		case DEV_PATH_VLAN:
107 		case DEV_PATH_PPPOE:
108 		case DEV_PATH_TUN:
109 			info->indev = path->dev;
110 			if (is_zero_ether_addr(info->h_source))
111 				memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
112 
113 			if (path->type == DEV_PATH_ETHERNET)
114 				break;
115 			if (path->type == DEV_PATH_DSA) {
116 				i = stack->num_paths;
117 				break;
118 			}
119 
120 			/* DEV_PATH_VLAN, DEV_PATH_PPPOE and DEV_PATH_TUN */
121 			if (path->type == DEV_PATH_TUN) {
122 				if (info->num_tuns) {
123 					info->indev = NULL;
124 					break;
125 				}
126 				info->tun.src_v6 = path->tun.src_v6;
127 				info->tun.dst_v6 = path->tun.dst_v6;
128 				info->tun.l3_proto = path->tun.l3_proto;
129 				info->num_tuns++;
130 			} else {
131 				if (info->num_encaps >= NF_FLOW_TABLE_ENCAP_MAX) {
132 					info->indev = NULL;
133 					break;
134 				}
135 				info->encap[info->num_encaps].id =
136 					path->encap.id;
137 				info->encap[info->num_encaps].proto =
138 					path->encap.proto;
139 				info->num_encaps++;
140 			}
141 			if (path->type == DEV_PATH_PPPOE)
142 				memcpy(info->h_dest, path->encap.h_dest, ETH_ALEN);
143 			break;
144 		case DEV_PATH_BRIDGE:
145 			if (is_zero_ether_addr(info->h_source))
146 				memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
147 
148 			switch (path->bridge.vlan_mode) {
149 			case DEV_PATH_BR_VLAN_UNTAG_HW:
150 				info->ingress_vlans |= BIT(info->num_encaps - 1);
151 				break;
152 			case DEV_PATH_BR_VLAN_TAG:
153 				if (info->num_encaps >= NF_FLOW_TABLE_ENCAP_MAX) {
154 					info->indev = NULL;
155 					break;
156 				}
157 				info->encap[info->num_encaps].id = path->bridge.vlan_id;
158 				info->encap[info->num_encaps].proto = path->bridge.vlan_proto;
159 				info->num_encaps++;
160 				break;
161 			case DEV_PATH_BR_VLAN_UNTAG:
162 				if (WARN_ON_ONCE(info->num_encaps-- == 0)) {
163 					info->indev = NULL;
164 					break;
165 				}
166 				break;
167 			case DEV_PATH_BR_VLAN_KEEP:
168 				break;
169 			}
170 			info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
171 			break;
172 		default:
173 			info->indev = NULL;
174 			break;
175 		}
176 	}
177 	info->outdev = info->indev;
178 
179 	if (nf_flowtable_hw_offload(flowtable) &&
180 	    nft_is_valid_ether_device(info->indev))
181 		info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
182 }
183 
nft_flowtable_find_dev(const struct net_device * dev,struct nft_flowtable * ft)184 static bool nft_flowtable_find_dev(const struct net_device *dev,
185 				   struct nft_flowtable *ft)
186 {
187 	struct nft_hook *hook;
188 	bool found = false;
189 
190 	list_for_each_entry_rcu(hook, &ft->hook_list, list) {
191 		if (!nft_hook_find_ops_rcu(hook, dev))
192 			continue;
193 
194 		found = true;
195 		break;
196 	}
197 
198 	return found;
199 }
200 
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)201 static int nft_flow_tunnel_update_route(const struct nft_pktinfo *pkt,
202 					struct flow_offload_tunnel *tun,
203 					struct nf_flow_route *route,
204 					enum ip_conntrack_dir dir)
205 {
206 	struct dst_entry *cur_dst = route->tuple[dir].dst;
207 	struct dst_entry *tun_dst = NULL;
208 	struct flowi fl = {};
209 
210 	switch (nft_pf(pkt)) {
211 	case NFPROTO_IPV4:
212 		fl.u.ip4.daddr = tun->dst_v4.s_addr;
213 		fl.u.ip4.saddr = tun->src_v4.s_addr;
214 		fl.u.ip4.flowi4_iif = nft_in(pkt)->ifindex;
215 		fl.u.ip4.flowi4_dscp = ip4h_dscp(ip_hdr(pkt->skb));
216 		fl.u.ip4.flowi4_mark = pkt->skb->mark;
217 		fl.u.ip4.flowi4_flags = FLOWI_FLAG_ANYSRC;
218 		break;
219 	case NFPROTO_IPV6:
220 		fl.u.ip6.daddr = tun->dst_v6;
221 		fl.u.ip6.saddr = tun->src_v6;
222 		fl.u.ip6.flowi6_iif = nft_in(pkt)->ifindex;
223 		fl.u.ip6.flowlabel = ip6_flowinfo(ipv6_hdr(pkt->skb));
224 		fl.u.ip6.flowi6_mark = pkt->skb->mark;
225 		fl.u.ip6.flowi6_flags = FLOWI_FLAG_ANYSRC;
226 		break;
227 	}
228 
229 	nf_route(nft_net(pkt), &tun_dst, &fl, false, nft_pf(pkt));
230 	if (!tun_dst)
231 		return -ENOENT;
232 
233 	route->tuple[dir].dst = tun_dst;
234 	dst_release(cur_dst);
235 
236 	return 0;
237 }
238 
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)239 static void nft_dev_forward_path(const struct nft_pktinfo *pkt,
240 				 struct nf_flow_route *route,
241 				 const struct nf_conn *ct,
242 				 enum ip_conntrack_dir dir,
243 				 struct nft_flowtable *ft)
244 {
245 	const struct dst_entry *dst = route->tuple[dir].dst;
246 	struct net_device_path_stack stack;
247 	struct nft_forward_info info = {};
248 	unsigned char ha[ETH_ALEN];
249 	int i;
250 
251 	if (nft_dev_fill_forward_path(route, dst, ct, dir, ha, &stack) >= 0)
252 		nft_dev_path_info(&stack, &info, ha, &ft->data);
253 
254 	if (info.outdev)
255 		route->tuple[dir].out.ifindex = info.outdev->ifindex;
256 
257 	if (!info.indev || !nft_flowtable_find_dev(info.indev, ft))
258 		return;
259 
260 	route->tuple[!dir].in.ifindex = info.indev->ifindex;
261 	for (i = 0; i < info.num_encaps; i++) {
262 		route->tuple[!dir].in.encap[i].id = info.encap[i].id;
263 		route->tuple[!dir].in.encap[i].proto = info.encap[i].proto;
264 	}
265 
266 	if (info.num_tuns &&
267 	    !nft_flow_tunnel_update_route(pkt, &info.tun, route, dir)) {
268 		route->tuple[!dir].in.tun.src_v6 = info.tun.dst_v6;
269 		route->tuple[!dir].in.tun.dst_v6 = info.tun.src_v6;
270 		route->tuple[!dir].in.tun.l3_proto = info.tun.l3_proto;
271 		route->tuple[!dir].in.num_tuns = info.num_tuns;
272 	}
273 
274 	route->tuple[!dir].in.num_encaps = info.num_encaps;
275 	route->tuple[!dir].in.ingress_vlans = info.ingress_vlans;
276 
277 	if (info.xmit_type == FLOW_OFFLOAD_XMIT_DIRECT) {
278 		memcpy(route->tuple[dir].out.h_source, info.h_source, ETH_ALEN);
279 		memcpy(route->tuple[dir].out.h_dest, info.h_dest, ETH_ALEN);
280 		route->tuple[dir].xmit_type = info.xmit_type;
281 	}
282 }
283 
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)284 int nft_flow_route(const struct nft_pktinfo *pkt, const struct nf_conn *ct,
285 		   struct nf_flow_route *route, enum ip_conntrack_dir dir,
286 		   struct nft_flowtable *ft)
287 {
288 	struct dst_entry *this_dst = skb_dst(pkt->skb);
289 	struct dst_entry *other_dst = NULL;
290 	struct flowi fl;
291 
292 	memset(&fl, 0, sizeof(fl));
293 	switch (nft_pf(pkt)) {
294 	case NFPROTO_IPV4:
295 		fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
296 		fl.u.ip4.saddr = ct->tuplehash[!dir].tuple.src.u3.ip;
297 		fl.u.ip4.flowi4_oif = nft_in(pkt)->ifindex;
298 		fl.u.ip4.flowi4_iif = this_dst->dev->ifindex;
299 		fl.u.ip4.flowi4_dscp = ip4h_dscp(ip_hdr(pkt->skb));
300 		fl.u.ip4.flowi4_mark = pkt->skb->mark;
301 		fl.u.ip4.flowi4_flags = FLOWI_FLAG_ANYSRC;
302 		break;
303 	case NFPROTO_IPV6:
304 		fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
305 		fl.u.ip6.saddr = ct->tuplehash[!dir].tuple.src.u3.in6;
306 		fl.u.ip6.flowi6_oif = nft_in(pkt)->ifindex;
307 		fl.u.ip6.flowi6_iif = this_dst->dev->ifindex;
308 		fl.u.ip6.flowlabel = ip6_flowinfo(ipv6_hdr(pkt->skb));
309 		fl.u.ip6.flowi6_mark = pkt->skb->mark;
310 		fl.u.ip6.flowi6_flags = FLOWI_FLAG_ANYSRC;
311 		break;
312 	}
313 
314 	if (!dst_hold_safe(this_dst))
315 		return -ENOENT;
316 
317 	nf_route(nft_net(pkt), &other_dst, &fl, false, nft_pf(pkt));
318 	if (!other_dst) {
319 		dst_release(this_dst);
320 		return -ENOENT;
321 	}
322 
323 	nft_default_forward_path(route, this_dst, dir);
324 	nft_default_forward_path(route, other_dst, !dir);
325 
326 	if (route->tuple[dir].xmit_type	== FLOW_OFFLOAD_XMIT_NEIGH)
327 		nft_dev_forward_path(pkt, route, ct, dir, ft);
328 	if (route->tuple[!dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH)
329 		nft_dev_forward_path(pkt, route, ct, !dir, ft);
330 
331 	return 0;
332 }
333 EXPORT_SYMBOL_GPL(nft_flow_route);
334