• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2007-2014 Nicira, Inc.
4  */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/if_arp.h>
11 #include <linux/if_vlan.h>
12 #include <linux/in.h>
13 #include <linux/ip.h>
14 #include <linux/jhash.h>
15 #include <linux/delay.h>
16 #include <linux/time.h>
17 #include <linux/etherdevice.h>
18 #include <linux/genetlink.h>
19 #include <linux/kernel.h>
20 #include <linux/kthread.h>
21 #include <linux/mutex.h>
22 #include <linux/percpu.h>
23 #include <linux/rcupdate.h>
24 #include <linux/tcp.h>
25 #include <linux/udp.h>
26 #include <linux/ethtool.h>
27 #include <linux/wait.h>
28 #include <asm/div64.h>
29 #include <linux/highmem.h>
30 #include <linux/netfilter_bridge.h>
31 #include <linux/netfilter_ipv4.h>
32 #include <linux/inetdevice.h>
33 #include <linux/list.h>
34 #include <linux/openvswitch.h>
35 #include <linux/rculist.h>
36 #include <linux/dmi.h>
37 #include <net/genetlink.h>
38 #include <net/net_namespace.h>
39 #include <net/netns/generic.h>
40 
41 #include "datapath.h"
42 #include "flow.h"
43 #include "flow_table.h"
44 #include "flow_netlink.h"
45 #include "meter.h"
46 #include "openvswitch_trace.h"
47 #include "vport-internal_dev.h"
48 #include "vport-netdev.h"
49 
50 unsigned int ovs_net_id __read_mostly;
51 
52 static struct genl_family dp_packet_genl_family;
53 static struct genl_family dp_flow_genl_family;
54 static struct genl_family dp_datapath_genl_family;
55 
56 static const struct nla_policy flow_policy[];
57 
58 static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
59 	.name = OVS_FLOW_MCGROUP,
60 };
61 
62 static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
63 	.name = OVS_DATAPATH_MCGROUP,
64 };
65 
66 static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
67 	.name = OVS_VPORT_MCGROUP,
68 };
69 
70 /* Check if need to build a reply message.
71  * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
ovs_must_notify(struct genl_family * family,struct genl_info * info,unsigned int group)72 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
73 			    unsigned int group)
74 {
75 	return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
76 	       genl_has_listeners(family, genl_info_net(info), group);
77 }
78 
ovs_notify(struct genl_family * family,struct sk_buff * skb,struct genl_info * info)79 static void ovs_notify(struct genl_family *family,
80 		       struct sk_buff *skb, struct genl_info *info)
81 {
82 	genl_notify(family, skb, info, 0, GFP_KERNEL);
83 }
84 
85 /**
86  * DOC: Locking:
87  *
88  * All writes e.g. Writes to device state (add/remove datapath, port, set
89  * operations on vports, etc.), Writes to other state (flow table
90  * modifications, set miscellaneous datapath parameters, etc.) are protected
91  * by ovs_lock.
92  *
93  * Reads are protected by RCU.
94  *
95  * There are a few special cases (mostly stats) that have their own
96  * synchronization but they nest under all of above and don't interact with
97  * each other.
98  *
99  * The RTNL lock nests inside ovs_mutex.
100  */
101 
102 static DEFINE_MUTEX(ovs_mutex);
103 
ovs_lock(void)104 void ovs_lock(void)
105 {
106 	mutex_lock(&ovs_mutex);
107 }
108 
ovs_unlock(void)109 void ovs_unlock(void)
110 {
111 	mutex_unlock(&ovs_mutex);
112 }
113 
114 #ifdef CONFIG_LOCKDEP
lockdep_ovsl_is_held(void)115 int lockdep_ovsl_is_held(void)
116 {
117 	if (debug_locks)
118 		return lockdep_is_held(&ovs_mutex);
119 	else
120 		return 1;
121 }
122 #endif
123 
124 static struct vport *new_vport(const struct vport_parms *);
125 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
126 			     const struct sw_flow_key *,
127 			     const struct dp_upcall_info *,
128 			     uint32_t cutlen);
129 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
130 				  const struct sw_flow_key *,
131 				  const struct dp_upcall_info *,
132 				  uint32_t cutlen);
133 
134 static void ovs_dp_masks_rebalance(struct work_struct *work);
135 
136 static int ovs_dp_set_upcall_portids(struct datapath *, const struct nlattr *);
137 
138 /* Must be called with rcu_read_lock or ovs_mutex. */
ovs_dp_name(const struct datapath * dp)139 const char *ovs_dp_name(const struct datapath *dp)
140 {
141 	struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
142 	return ovs_vport_name(vport);
143 }
144 
get_dpifindex(const struct datapath * dp)145 static int get_dpifindex(const struct datapath *dp)
146 {
147 	struct vport *local;
148 	int ifindex;
149 
150 	rcu_read_lock();
151 
152 	local = ovs_vport_rcu(dp, OVSP_LOCAL);
153 	if (local)
154 		ifindex = local->dev->ifindex;
155 	else
156 		ifindex = 0;
157 
158 	rcu_read_unlock();
159 
160 	return ifindex;
161 }
162 
destroy_dp_rcu(struct rcu_head * rcu)163 static void destroy_dp_rcu(struct rcu_head *rcu)
164 {
165 	struct datapath *dp = container_of(rcu, struct datapath, rcu);
166 
167 	ovs_flow_tbl_destroy(&dp->table);
168 	free_percpu(dp->stats_percpu);
169 	kfree(dp->ports);
170 	ovs_meters_exit(dp);
171 	kfree(rcu_dereference_raw(dp->upcall_portids));
172 	kfree(dp);
173 }
174 
vport_hash_bucket(const struct datapath * dp,u16 port_no)175 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
176 					    u16 port_no)
177 {
178 	return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
179 }
180 
181 /* Called with ovs_mutex or RCU read lock. */
ovs_lookup_vport(const struct datapath * dp,u16 port_no)182 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
183 {
184 	struct vport *vport;
185 	struct hlist_head *head;
186 
187 	head = vport_hash_bucket(dp, port_no);
188 	hlist_for_each_entry_rcu(vport, head, dp_hash_node,
189 				 lockdep_ovsl_is_held()) {
190 		if (vport->port_no == port_no)
191 			return vport;
192 	}
193 	return NULL;
194 }
195 
196 /* Called with ovs_mutex. */
new_vport(const struct vport_parms * parms)197 static struct vport *new_vport(const struct vport_parms *parms)
198 {
199 	struct vport *vport;
200 
201 	vport = ovs_vport_add(parms);
202 	if (!IS_ERR(vport)) {
203 		struct datapath *dp = parms->dp;
204 		struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
205 
206 		hlist_add_head_rcu(&vport->dp_hash_node, head);
207 	}
208 	return vport;
209 }
210 
ovs_dp_detach_port(struct vport * p)211 void ovs_dp_detach_port(struct vport *p)
212 {
213 	ASSERT_OVSL();
214 
215 	/* First drop references to device. */
216 	hlist_del_rcu(&p->dp_hash_node);
217 
218 	/* Then destroy it. */
219 	ovs_vport_del(p);
220 }
221 
222 /* Must be called with rcu_read_lock. */
ovs_dp_process_packet(struct sk_buff * skb,struct sw_flow_key * key)223 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
224 {
225 	const struct vport *p = OVS_CB(skb)->input_vport;
226 	struct datapath *dp = p->dp;
227 	struct sw_flow *flow;
228 	struct sw_flow_actions *sf_acts;
229 	struct dp_stats_percpu *stats;
230 	u64 *stats_counter;
231 	u32 n_mask_hit;
232 	u32 n_cache_hit;
233 	int error;
234 
235 	stats = this_cpu_ptr(dp->stats_percpu);
236 
237 	/* Look up flow. */
238 	flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
239 					 &n_mask_hit, &n_cache_hit);
240 	if (unlikely(!flow)) {
241 		struct dp_upcall_info upcall;
242 
243 		memset(&upcall, 0, sizeof(upcall));
244 		upcall.cmd = OVS_PACKET_CMD_MISS;
245 
246 		if (dp->user_features & OVS_DP_F_DISPATCH_UPCALL_PER_CPU)
247 			upcall.portid =
248 			    ovs_dp_get_upcall_portid(dp, smp_processor_id());
249 		else
250 			upcall.portid = ovs_vport_find_upcall_portid(p, skb);
251 
252 		upcall.mru = OVS_CB(skb)->mru;
253 		error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
254 		switch (error) {
255 		case 0:
256 		case -EAGAIN:
257 		case -ERESTARTSYS:
258 		case -EINTR:
259 			consume_skb(skb);
260 			break;
261 		default:
262 			kfree_skb(skb);
263 			break;
264 		}
265 		stats_counter = &stats->n_missed;
266 		goto out;
267 	}
268 
269 	ovs_flow_stats_update(flow, key->tp.flags, skb);
270 	sf_acts = rcu_dereference(flow->sf_acts);
271 	error = ovs_execute_actions(dp, skb, sf_acts, key);
272 	if (unlikely(error))
273 		net_dbg_ratelimited("ovs: action execution error on datapath %s: %d\n",
274 				    ovs_dp_name(dp), error);
275 
276 	stats_counter = &stats->n_hit;
277 
278 out:
279 	/* Update datapath statistics. */
280 	u64_stats_update_begin(&stats->syncp);
281 	(*stats_counter)++;
282 	stats->n_mask_hit += n_mask_hit;
283 	stats->n_cache_hit += n_cache_hit;
284 	u64_stats_update_end(&stats->syncp);
285 }
286 
ovs_dp_upcall(struct datapath * dp,struct sk_buff * skb,const struct sw_flow_key * key,const struct dp_upcall_info * upcall_info,uint32_t cutlen)287 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
288 		  const struct sw_flow_key *key,
289 		  const struct dp_upcall_info *upcall_info,
290 		  uint32_t cutlen)
291 {
292 	struct dp_stats_percpu *stats;
293 	int err;
294 
295 	if (trace_ovs_dp_upcall_enabled())
296 		trace_ovs_dp_upcall(dp, skb, key, upcall_info);
297 
298 	if (upcall_info->portid == 0) {
299 		err = -ENOTCONN;
300 		goto err;
301 	}
302 
303 	if (!skb_is_gso(skb))
304 		err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
305 	else
306 		err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
307 	if (err)
308 		goto err;
309 
310 	return 0;
311 
312 err:
313 	stats = this_cpu_ptr(dp->stats_percpu);
314 
315 	u64_stats_update_begin(&stats->syncp);
316 	stats->n_lost++;
317 	u64_stats_update_end(&stats->syncp);
318 
319 	return err;
320 }
321 
queue_gso_packets(struct datapath * dp,struct sk_buff * skb,const struct sw_flow_key * key,const struct dp_upcall_info * upcall_info,uint32_t cutlen)322 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
323 			     const struct sw_flow_key *key,
324 			     const struct dp_upcall_info *upcall_info,
325 			     uint32_t cutlen)
326 {
327 	unsigned int gso_type = skb_shinfo(skb)->gso_type;
328 	struct sw_flow_key later_key;
329 	struct sk_buff *segs, *nskb;
330 	int err;
331 
332 	BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_GSO_CB_OFFSET);
333 	segs = __skb_gso_segment(skb, NETIF_F_SG, false);
334 	if (IS_ERR(segs))
335 		return PTR_ERR(segs);
336 	if (segs == NULL)
337 		return -EINVAL;
338 
339 	if (gso_type & SKB_GSO_UDP) {
340 		/* The initial flow key extracted by ovs_flow_key_extract()
341 		 * in this case is for a first fragment, so we need to
342 		 * properly mark later fragments.
343 		 */
344 		later_key = *key;
345 		later_key.ip.frag = OVS_FRAG_TYPE_LATER;
346 	}
347 
348 	/* Queue all of the segments. */
349 	skb_list_walk_safe(segs, skb, nskb) {
350 		if (gso_type & SKB_GSO_UDP && skb != segs)
351 			key = &later_key;
352 
353 		err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
354 		if (err)
355 			break;
356 
357 	}
358 
359 	/* Free all of the segments. */
360 	skb_list_walk_safe(segs, skb, nskb) {
361 		if (err)
362 			kfree_skb(skb);
363 		else
364 			consume_skb(skb);
365 	}
366 	return err;
367 }
368 
upcall_msg_size(const struct dp_upcall_info * upcall_info,unsigned int hdrlen,int actions_attrlen)369 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
370 			      unsigned int hdrlen, int actions_attrlen)
371 {
372 	size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
373 		+ nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
374 		+ nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
375 		+ nla_total_size(sizeof(unsigned int)) /* OVS_PACKET_ATTR_LEN */
376 		+ nla_total_size(sizeof(u64)); /* OVS_PACKET_ATTR_HASH */
377 
378 	/* OVS_PACKET_ATTR_USERDATA */
379 	if (upcall_info->userdata)
380 		size += NLA_ALIGN(upcall_info->userdata->nla_len);
381 
382 	/* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
383 	if (upcall_info->egress_tun_info)
384 		size += nla_total_size(ovs_tun_key_attr_size());
385 
386 	/* OVS_PACKET_ATTR_ACTIONS */
387 	if (upcall_info->actions_len)
388 		size += nla_total_size(actions_attrlen);
389 
390 	/* OVS_PACKET_ATTR_MRU */
391 	if (upcall_info->mru)
392 		size += nla_total_size(sizeof(upcall_info->mru));
393 
394 	return size;
395 }
396 
pad_packet(struct datapath * dp,struct sk_buff * skb)397 static void pad_packet(struct datapath *dp, struct sk_buff *skb)
398 {
399 	if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
400 		size_t plen = NLA_ALIGN(skb->len) - skb->len;
401 
402 		if (plen > 0)
403 			skb_put_zero(skb, plen);
404 	}
405 }
406 
queue_userspace_packet(struct datapath * dp,struct sk_buff * skb,const struct sw_flow_key * key,const struct dp_upcall_info * upcall_info,uint32_t cutlen)407 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
408 				  const struct sw_flow_key *key,
409 				  const struct dp_upcall_info *upcall_info,
410 				  uint32_t cutlen)
411 {
412 	struct ovs_header *upcall;
413 	struct sk_buff *nskb = NULL;
414 	struct sk_buff *user_skb = NULL; /* to be queued to userspace */
415 	struct nlattr *nla;
416 	size_t len;
417 	unsigned int hlen;
418 	int err, dp_ifindex;
419 	u64 hash;
420 
421 	dp_ifindex = get_dpifindex(dp);
422 	if (!dp_ifindex)
423 		return -ENODEV;
424 
425 	if (skb_vlan_tag_present(skb)) {
426 		nskb = skb_clone(skb, GFP_ATOMIC);
427 		if (!nskb)
428 			return -ENOMEM;
429 
430 		nskb = __vlan_hwaccel_push_inside(nskb);
431 		if (!nskb)
432 			return -ENOMEM;
433 
434 		skb = nskb;
435 	}
436 
437 	if (nla_attr_size(skb->len) > USHRT_MAX) {
438 		err = -EFBIG;
439 		goto out;
440 	}
441 
442 	/* Complete checksum if needed */
443 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
444 	    (err = skb_csum_hwoffload_help(skb, 0)))
445 		goto out;
446 
447 	/* Older versions of OVS user space enforce alignment of the last
448 	 * Netlink attribute to NLA_ALIGNTO which would require extensive
449 	 * padding logic. Only perform zerocopy if padding is not required.
450 	 */
451 	if (dp->user_features & OVS_DP_F_UNALIGNED)
452 		hlen = skb_zerocopy_headlen(skb);
453 	else
454 		hlen = skb->len;
455 
456 	len = upcall_msg_size(upcall_info, hlen - cutlen,
457 			      OVS_CB(skb)->acts_origlen);
458 	user_skb = genlmsg_new(len, GFP_ATOMIC);
459 	if (!user_skb) {
460 		err = -ENOMEM;
461 		goto out;
462 	}
463 
464 	upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
465 			     0, upcall_info->cmd);
466 	if (!upcall) {
467 		err = -EINVAL;
468 		goto out;
469 	}
470 	upcall->dp_ifindex = dp_ifindex;
471 
472 	err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
473 	if (err)
474 		goto out;
475 
476 	if (upcall_info->userdata)
477 		__nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
478 			  nla_len(upcall_info->userdata),
479 			  nla_data(upcall_info->userdata));
480 
481 	if (upcall_info->egress_tun_info) {
482 		nla = nla_nest_start_noflag(user_skb,
483 					    OVS_PACKET_ATTR_EGRESS_TUN_KEY);
484 		if (!nla) {
485 			err = -EMSGSIZE;
486 			goto out;
487 		}
488 		err = ovs_nla_put_tunnel_info(user_skb,
489 					      upcall_info->egress_tun_info);
490 		if (err)
491 			goto out;
492 
493 		nla_nest_end(user_skb, nla);
494 	}
495 
496 	if (upcall_info->actions_len) {
497 		nla = nla_nest_start_noflag(user_skb, OVS_PACKET_ATTR_ACTIONS);
498 		if (!nla) {
499 			err = -EMSGSIZE;
500 			goto out;
501 		}
502 		err = ovs_nla_put_actions(upcall_info->actions,
503 					  upcall_info->actions_len,
504 					  user_skb);
505 		if (!err)
506 			nla_nest_end(user_skb, nla);
507 		else
508 			nla_nest_cancel(user_skb, nla);
509 	}
510 
511 	/* Add OVS_PACKET_ATTR_MRU */
512 	if (upcall_info->mru &&
513 	    nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU, upcall_info->mru)) {
514 		err = -ENOBUFS;
515 		goto out;
516 	}
517 
518 	/* Add OVS_PACKET_ATTR_LEN when packet is truncated */
519 	if (cutlen > 0 &&
520 	    nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN, skb->len)) {
521 		err = -ENOBUFS;
522 		goto out;
523 	}
524 
525 	/* Add OVS_PACKET_ATTR_HASH */
526 	hash = skb_get_hash_raw(skb);
527 	if (skb->sw_hash)
528 		hash |= OVS_PACKET_HASH_SW_BIT;
529 
530 	if (skb->l4_hash)
531 		hash |= OVS_PACKET_HASH_L4_BIT;
532 
533 	if (nla_put(user_skb, OVS_PACKET_ATTR_HASH, sizeof (u64), &hash)) {
534 		err = -ENOBUFS;
535 		goto out;
536 	}
537 
538 	/* Only reserve room for attribute header, packet data is added
539 	 * in skb_zerocopy() */
540 	if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
541 		err = -ENOBUFS;
542 		goto out;
543 	}
544 	nla->nla_len = nla_attr_size(skb->len - cutlen);
545 
546 	err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
547 	if (err)
548 		goto out;
549 
550 	/* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
551 	pad_packet(dp, user_skb);
552 
553 	((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
554 
555 	err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
556 	user_skb = NULL;
557 out:
558 	if (err)
559 		skb_tx_error(skb);
560 	consume_skb(user_skb);
561 	consume_skb(nskb);
562 
563 	return err;
564 }
565 
ovs_packet_cmd_execute(struct sk_buff * skb,struct genl_info * info)566 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
567 {
568 	struct ovs_header *ovs_header = info->userhdr;
569 	struct net *net = sock_net(skb->sk);
570 	struct nlattr **a = info->attrs;
571 	struct sw_flow_actions *acts;
572 	struct sk_buff *packet;
573 	struct sw_flow *flow;
574 	struct sw_flow_actions *sf_acts;
575 	struct datapath *dp;
576 	struct vport *input_vport;
577 	u16 mru = 0;
578 	u64 hash;
579 	int len;
580 	int err;
581 	bool log = !a[OVS_PACKET_ATTR_PROBE];
582 
583 	err = -EINVAL;
584 	if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
585 	    !a[OVS_PACKET_ATTR_ACTIONS])
586 		goto err;
587 
588 	len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
589 	packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
590 	err = -ENOMEM;
591 	if (!packet)
592 		goto err;
593 	skb_reserve(packet, NET_IP_ALIGN);
594 
595 	nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
596 
597 	/* Set packet's mru */
598 	if (a[OVS_PACKET_ATTR_MRU]) {
599 		mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
600 		packet->ignore_df = 1;
601 	}
602 	OVS_CB(packet)->mru = mru;
603 
604 	if (a[OVS_PACKET_ATTR_HASH]) {
605 		hash = nla_get_u64(a[OVS_PACKET_ATTR_HASH]);
606 
607 		__skb_set_hash(packet, hash & 0xFFFFFFFFULL,
608 			       !!(hash & OVS_PACKET_HASH_SW_BIT),
609 			       !!(hash & OVS_PACKET_HASH_L4_BIT));
610 	}
611 
612 	/* Build an sw_flow for sending this packet. */
613 	flow = ovs_flow_alloc();
614 	err = PTR_ERR(flow);
615 	if (IS_ERR(flow))
616 		goto err_kfree_skb;
617 
618 	err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
619 					     packet, &flow->key, log);
620 	if (err)
621 		goto err_flow_free;
622 
623 	err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
624 				   &flow->key, &acts, log);
625 	if (err)
626 		goto err_flow_free;
627 
628 	rcu_assign_pointer(flow->sf_acts, acts);
629 	packet->priority = flow->key.phy.priority;
630 	packet->mark = flow->key.phy.skb_mark;
631 
632 	rcu_read_lock();
633 	dp = get_dp_rcu(net, ovs_header->dp_ifindex);
634 	err = -ENODEV;
635 	if (!dp)
636 		goto err_unlock;
637 
638 	input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
639 	if (!input_vport)
640 		input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
641 
642 	if (!input_vport)
643 		goto err_unlock;
644 
645 	packet->dev = input_vport->dev;
646 	OVS_CB(packet)->input_vport = input_vport;
647 	sf_acts = rcu_dereference(flow->sf_acts);
648 
649 	local_bh_disable();
650 	err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
651 	local_bh_enable();
652 	rcu_read_unlock();
653 
654 	ovs_flow_free(flow, false);
655 	return err;
656 
657 err_unlock:
658 	rcu_read_unlock();
659 err_flow_free:
660 	ovs_flow_free(flow, false);
661 err_kfree_skb:
662 	kfree_skb(packet);
663 err:
664 	return err;
665 }
666 
667 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
668 	[OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
669 	[OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
670 	[OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
671 	[OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
672 	[OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
673 	[OVS_PACKET_ATTR_HASH] = { .type = NLA_U64 },
674 };
675 
676 static const struct genl_small_ops dp_packet_genl_ops[] = {
677 	{ .cmd = OVS_PACKET_CMD_EXECUTE,
678 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
679 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
680 	  .doit = ovs_packet_cmd_execute
681 	}
682 };
683 
684 static struct genl_family dp_packet_genl_family __ro_after_init = {
685 	.hdrsize = sizeof(struct ovs_header),
686 	.name = OVS_PACKET_FAMILY,
687 	.version = OVS_PACKET_VERSION,
688 	.maxattr = OVS_PACKET_ATTR_MAX,
689 	.policy = packet_policy,
690 	.netnsok = true,
691 	.parallel_ops = true,
692 	.small_ops = dp_packet_genl_ops,
693 	.n_small_ops = ARRAY_SIZE(dp_packet_genl_ops),
694 	.module = THIS_MODULE,
695 };
696 
get_dp_stats(const struct datapath * dp,struct ovs_dp_stats * stats,struct ovs_dp_megaflow_stats * mega_stats)697 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
698 			 struct ovs_dp_megaflow_stats *mega_stats)
699 {
700 	int i;
701 
702 	memset(mega_stats, 0, sizeof(*mega_stats));
703 
704 	stats->n_flows = ovs_flow_tbl_count(&dp->table);
705 	mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
706 
707 	stats->n_hit = stats->n_missed = stats->n_lost = 0;
708 
709 	for_each_possible_cpu(i) {
710 		const struct dp_stats_percpu *percpu_stats;
711 		struct dp_stats_percpu local_stats;
712 		unsigned int start;
713 
714 		percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
715 
716 		do {
717 			start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
718 			local_stats = *percpu_stats;
719 		} while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
720 
721 		stats->n_hit += local_stats.n_hit;
722 		stats->n_missed += local_stats.n_missed;
723 		stats->n_lost += local_stats.n_lost;
724 		mega_stats->n_mask_hit += local_stats.n_mask_hit;
725 		mega_stats->n_cache_hit += local_stats.n_cache_hit;
726 	}
727 }
728 
should_fill_key(const struct sw_flow_id * sfid,uint32_t ufid_flags)729 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
730 {
731 	return ovs_identifier_is_ufid(sfid) &&
732 	       !(ufid_flags & OVS_UFID_F_OMIT_KEY);
733 }
734 
should_fill_mask(uint32_t ufid_flags)735 static bool should_fill_mask(uint32_t ufid_flags)
736 {
737 	return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
738 }
739 
should_fill_actions(uint32_t ufid_flags)740 static bool should_fill_actions(uint32_t ufid_flags)
741 {
742 	return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
743 }
744 
ovs_flow_cmd_msg_size(const struct sw_flow_actions * acts,const struct sw_flow_id * sfid,uint32_t ufid_flags)745 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
746 				    const struct sw_flow_id *sfid,
747 				    uint32_t ufid_flags)
748 {
749 	size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
750 
751 	/* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback
752 	 * see ovs_nla_put_identifier()
753 	 */
754 	if (sfid && ovs_identifier_is_ufid(sfid))
755 		len += nla_total_size(sfid->ufid_len);
756 	else
757 		len += nla_total_size(ovs_key_attr_size());
758 
759 	/* OVS_FLOW_ATTR_KEY */
760 	if (!sfid || should_fill_key(sfid, ufid_flags))
761 		len += nla_total_size(ovs_key_attr_size());
762 
763 	/* OVS_FLOW_ATTR_MASK */
764 	if (should_fill_mask(ufid_flags))
765 		len += nla_total_size(ovs_key_attr_size());
766 
767 	/* OVS_FLOW_ATTR_ACTIONS */
768 	if (should_fill_actions(ufid_flags))
769 		len += nla_total_size(acts->orig_len);
770 
771 	return len
772 		+ nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
773 		+ nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
774 		+ nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
775 }
776 
777 /* Called with ovs_mutex or RCU read lock. */
ovs_flow_cmd_fill_stats(const struct sw_flow * flow,struct sk_buff * skb)778 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
779 				   struct sk_buff *skb)
780 {
781 	struct ovs_flow_stats stats;
782 	__be16 tcp_flags;
783 	unsigned long used;
784 
785 	ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
786 
787 	if (used &&
788 	    nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
789 			      OVS_FLOW_ATTR_PAD))
790 		return -EMSGSIZE;
791 
792 	if (stats.n_packets &&
793 	    nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
794 			  sizeof(struct ovs_flow_stats), &stats,
795 			  OVS_FLOW_ATTR_PAD))
796 		return -EMSGSIZE;
797 
798 	if ((u8)ntohs(tcp_flags) &&
799 	     nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
800 		return -EMSGSIZE;
801 
802 	return 0;
803 }
804 
805 /* Called with ovs_mutex or RCU read lock. */
ovs_flow_cmd_fill_actions(const struct sw_flow * flow,struct sk_buff * skb,int skb_orig_len)806 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
807 				     struct sk_buff *skb, int skb_orig_len)
808 {
809 	struct nlattr *start;
810 	int err;
811 
812 	/* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
813 	 * this is the first flow to be dumped into 'skb'.  This is unusual for
814 	 * Netlink but individual action lists can be longer than
815 	 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
816 	 * The userspace caller can always fetch the actions separately if it
817 	 * really wants them.  (Most userspace callers in fact don't care.)
818 	 *
819 	 * This can only fail for dump operations because the skb is always
820 	 * properly sized for single flows.
821 	 */
822 	start = nla_nest_start_noflag(skb, OVS_FLOW_ATTR_ACTIONS);
823 	if (start) {
824 		const struct sw_flow_actions *sf_acts;
825 
826 		sf_acts = rcu_dereference_ovsl(flow->sf_acts);
827 		err = ovs_nla_put_actions(sf_acts->actions,
828 					  sf_acts->actions_len, skb);
829 
830 		if (!err)
831 			nla_nest_end(skb, start);
832 		else {
833 			if (skb_orig_len)
834 				return err;
835 
836 			nla_nest_cancel(skb, start);
837 		}
838 	} else if (skb_orig_len) {
839 		return -EMSGSIZE;
840 	}
841 
842 	return 0;
843 }
844 
845 /* Called with ovs_mutex or RCU read lock. */
ovs_flow_cmd_fill_info(const struct sw_flow * flow,int dp_ifindex,struct sk_buff * skb,u32 portid,u32 seq,u32 flags,u8 cmd,u32 ufid_flags)846 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
847 				  struct sk_buff *skb, u32 portid,
848 				  u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
849 {
850 	const int skb_orig_len = skb->len;
851 	struct ovs_header *ovs_header;
852 	int err;
853 
854 	ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
855 				 flags, cmd);
856 	if (!ovs_header)
857 		return -EMSGSIZE;
858 
859 	ovs_header->dp_ifindex = dp_ifindex;
860 
861 	err = ovs_nla_put_identifier(flow, skb);
862 	if (err)
863 		goto error;
864 
865 	if (should_fill_key(&flow->id, ufid_flags)) {
866 		err = ovs_nla_put_masked_key(flow, skb);
867 		if (err)
868 			goto error;
869 	}
870 
871 	if (should_fill_mask(ufid_flags)) {
872 		err = ovs_nla_put_mask(flow, skb);
873 		if (err)
874 			goto error;
875 	}
876 
877 	err = ovs_flow_cmd_fill_stats(flow, skb);
878 	if (err)
879 		goto error;
880 
881 	if (should_fill_actions(ufid_flags)) {
882 		err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
883 		if (err)
884 			goto error;
885 	}
886 
887 	genlmsg_end(skb, ovs_header);
888 	return 0;
889 
890 error:
891 	genlmsg_cancel(skb, ovs_header);
892 	return err;
893 }
894 
895 /* May not be called with RCU read lock. */
ovs_flow_cmd_alloc_info(const struct sw_flow_actions * acts,const struct sw_flow_id * sfid,struct genl_info * info,bool always,uint32_t ufid_flags)896 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
897 					       const struct sw_flow_id *sfid,
898 					       struct genl_info *info,
899 					       bool always,
900 					       uint32_t ufid_flags)
901 {
902 	struct sk_buff *skb;
903 	size_t len;
904 
905 	if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
906 		return NULL;
907 
908 	len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
909 	skb = genlmsg_new(len, GFP_KERNEL);
910 	if (!skb)
911 		return ERR_PTR(-ENOMEM);
912 
913 	return skb;
914 }
915 
916 /* Called with ovs_mutex. */
ovs_flow_cmd_build_info(const struct sw_flow * flow,int dp_ifindex,struct genl_info * info,u8 cmd,bool always,u32 ufid_flags)917 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
918 					       int dp_ifindex,
919 					       struct genl_info *info, u8 cmd,
920 					       bool always, u32 ufid_flags)
921 {
922 	struct sk_buff *skb;
923 	int retval;
924 
925 	skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
926 				      &flow->id, info, always, ufid_flags);
927 	if (IS_ERR_OR_NULL(skb))
928 		return skb;
929 
930 	retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
931 					info->snd_portid, info->snd_seq, 0,
932 					cmd, ufid_flags);
933 	if (WARN_ON_ONCE(retval < 0)) {
934 		kfree_skb(skb);
935 		skb = ERR_PTR(retval);
936 	}
937 	return skb;
938 }
939 
ovs_flow_cmd_new(struct sk_buff * skb,struct genl_info * info)940 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
941 {
942 	struct net *net = sock_net(skb->sk);
943 	struct nlattr **a = info->attrs;
944 	struct ovs_header *ovs_header = info->userhdr;
945 	struct sw_flow *flow = NULL, *new_flow;
946 	struct sw_flow_mask mask;
947 	struct sk_buff *reply;
948 	struct datapath *dp;
949 	struct sw_flow_key *key;
950 	struct sw_flow_actions *acts;
951 	struct sw_flow_match match;
952 	u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
953 	int error;
954 	bool log = !a[OVS_FLOW_ATTR_PROBE];
955 
956 	/* Must have key and actions. */
957 	error = -EINVAL;
958 	if (!a[OVS_FLOW_ATTR_KEY]) {
959 		OVS_NLERR(log, "Flow key attr not present in new flow.");
960 		goto error;
961 	}
962 	if (!a[OVS_FLOW_ATTR_ACTIONS]) {
963 		OVS_NLERR(log, "Flow actions attr not present in new flow.");
964 		goto error;
965 	}
966 
967 	/* Most of the time we need to allocate a new flow, do it before
968 	 * locking.
969 	 */
970 	new_flow = ovs_flow_alloc();
971 	if (IS_ERR(new_flow)) {
972 		error = PTR_ERR(new_flow);
973 		goto error;
974 	}
975 
976 	/* Extract key. */
977 	key = kzalloc(sizeof(*key), GFP_KERNEL);
978 	if (!key) {
979 		error = -ENOMEM;
980 		goto err_kfree_flow;
981 	}
982 
983 	ovs_match_init(&match, key, false, &mask);
984 	error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
985 				  a[OVS_FLOW_ATTR_MASK], log);
986 	if (error)
987 		goto err_kfree_key;
988 
989 	ovs_flow_mask_key(&new_flow->key, key, true, &mask);
990 
991 	/* Extract flow identifier. */
992 	error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
993 				       key, log);
994 	if (error)
995 		goto err_kfree_key;
996 
997 	/* Validate actions. */
998 	error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
999 				     &new_flow->key, &acts, log);
1000 	if (error) {
1001 		OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
1002 		goto err_kfree_key;
1003 	}
1004 
1005 	reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
1006 					ufid_flags);
1007 	if (IS_ERR(reply)) {
1008 		error = PTR_ERR(reply);
1009 		goto err_kfree_acts;
1010 	}
1011 
1012 	ovs_lock();
1013 	dp = get_dp(net, ovs_header->dp_ifindex);
1014 	if (unlikely(!dp)) {
1015 		error = -ENODEV;
1016 		goto err_unlock_ovs;
1017 	}
1018 
1019 	/* Check if this is a duplicate flow */
1020 	if (ovs_identifier_is_ufid(&new_flow->id))
1021 		flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
1022 	if (!flow)
1023 		flow = ovs_flow_tbl_lookup(&dp->table, key);
1024 	if (likely(!flow)) {
1025 		rcu_assign_pointer(new_flow->sf_acts, acts);
1026 
1027 		/* Put flow in bucket. */
1028 		error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
1029 		if (unlikely(error)) {
1030 			acts = NULL;
1031 			goto err_unlock_ovs;
1032 		}
1033 
1034 		if (unlikely(reply)) {
1035 			error = ovs_flow_cmd_fill_info(new_flow,
1036 						       ovs_header->dp_ifindex,
1037 						       reply, info->snd_portid,
1038 						       info->snd_seq, 0,
1039 						       OVS_FLOW_CMD_NEW,
1040 						       ufid_flags);
1041 			BUG_ON(error < 0);
1042 		}
1043 		ovs_unlock();
1044 	} else {
1045 		struct sw_flow_actions *old_acts;
1046 
1047 		/* Bail out if we're not allowed to modify an existing flow.
1048 		 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1049 		 * because Generic Netlink treats the latter as a dump
1050 		 * request.  We also accept NLM_F_EXCL in case that bug ever
1051 		 * gets fixed.
1052 		 */
1053 		if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1054 							 | NLM_F_EXCL))) {
1055 			error = -EEXIST;
1056 			goto err_unlock_ovs;
1057 		}
1058 		/* The flow identifier has to be the same for flow updates.
1059 		 * Look for any overlapping flow.
1060 		 */
1061 		if (unlikely(!ovs_flow_cmp(flow, &match))) {
1062 			if (ovs_identifier_is_key(&flow->id))
1063 				flow = ovs_flow_tbl_lookup_exact(&dp->table,
1064 								 &match);
1065 			else /* UFID matches but key is different */
1066 				flow = NULL;
1067 			if (!flow) {
1068 				error = -ENOENT;
1069 				goto err_unlock_ovs;
1070 			}
1071 		}
1072 		/* Update actions. */
1073 		old_acts = ovsl_dereference(flow->sf_acts);
1074 		rcu_assign_pointer(flow->sf_acts, acts);
1075 
1076 		if (unlikely(reply)) {
1077 			error = ovs_flow_cmd_fill_info(flow,
1078 						       ovs_header->dp_ifindex,
1079 						       reply, info->snd_portid,
1080 						       info->snd_seq, 0,
1081 						       OVS_FLOW_CMD_NEW,
1082 						       ufid_flags);
1083 			BUG_ON(error < 0);
1084 		}
1085 		ovs_unlock();
1086 
1087 		ovs_nla_free_flow_actions_rcu(old_acts);
1088 		ovs_flow_free(new_flow, false);
1089 	}
1090 
1091 	if (reply)
1092 		ovs_notify(&dp_flow_genl_family, reply, info);
1093 
1094 	kfree(key);
1095 	return 0;
1096 
1097 err_unlock_ovs:
1098 	ovs_unlock();
1099 	kfree_skb(reply);
1100 err_kfree_acts:
1101 	ovs_nla_free_flow_actions(acts);
1102 err_kfree_key:
1103 	kfree(key);
1104 err_kfree_flow:
1105 	ovs_flow_free(new_flow, false);
1106 error:
1107 	return error;
1108 }
1109 
1110 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1111 static noinline_for_stack
get_flow_actions(struct net * net,const struct nlattr * a,const struct sw_flow_key * key,const struct sw_flow_mask * mask,bool log)1112 struct sw_flow_actions *get_flow_actions(struct net *net,
1113 					 const struct nlattr *a,
1114 					 const struct sw_flow_key *key,
1115 					 const struct sw_flow_mask *mask,
1116 					 bool log)
1117 {
1118 	struct sw_flow_actions *acts;
1119 	struct sw_flow_key masked_key;
1120 	int error;
1121 
1122 	ovs_flow_mask_key(&masked_key, key, true, mask);
1123 	error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1124 	if (error) {
1125 		OVS_NLERR(log,
1126 			  "Actions may not be safe on all matching packets");
1127 		return ERR_PTR(error);
1128 	}
1129 
1130 	return acts;
1131 }
1132 
1133 /* Factor out match-init and action-copy to avoid
1134  * "Wframe-larger-than=1024" warning. Because mask is only
1135  * used to get actions, we new a function to save some
1136  * stack space.
1137  *
1138  * If there are not key and action attrs, we return 0
1139  * directly. In the case, the caller will also not use the
1140  * match as before. If there is action attr, we try to get
1141  * actions and save them to *acts. Before returning from
1142  * the function, we reset the match->mask pointer. Because
1143  * we should not to return match object with dangling reference
1144  * to mask.
1145  * */
1146 static noinline_for_stack int
ovs_nla_init_match_and_action(struct net * net,struct sw_flow_match * match,struct sw_flow_key * key,struct nlattr ** a,struct sw_flow_actions ** acts,bool log)1147 ovs_nla_init_match_and_action(struct net *net,
1148 			      struct sw_flow_match *match,
1149 			      struct sw_flow_key *key,
1150 			      struct nlattr **a,
1151 			      struct sw_flow_actions **acts,
1152 			      bool log)
1153 {
1154 	struct sw_flow_mask mask;
1155 	int error = 0;
1156 
1157 	if (a[OVS_FLOW_ATTR_KEY]) {
1158 		ovs_match_init(match, key, true, &mask);
1159 		error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1160 					  a[OVS_FLOW_ATTR_MASK], log);
1161 		if (error)
1162 			goto error;
1163 	}
1164 
1165 	if (a[OVS_FLOW_ATTR_ACTIONS]) {
1166 		if (!a[OVS_FLOW_ATTR_KEY]) {
1167 			OVS_NLERR(log,
1168 				  "Flow key attribute not present in set flow.");
1169 			error = -EINVAL;
1170 			goto error;
1171 		}
1172 
1173 		*acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1174 					 &mask, log);
1175 		if (IS_ERR(*acts)) {
1176 			error = PTR_ERR(*acts);
1177 			goto error;
1178 		}
1179 	}
1180 
1181 	/* On success, error is 0. */
1182 error:
1183 	match->mask = NULL;
1184 	return error;
1185 }
1186 
ovs_flow_cmd_set(struct sk_buff * skb,struct genl_info * info)1187 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1188 {
1189 	struct net *net = sock_net(skb->sk);
1190 	struct nlattr **a = info->attrs;
1191 	struct ovs_header *ovs_header = info->userhdr;
1192 	struct sw_flow_key key;
1193 	struct sw_flow *flow;
1194 	struct sk_buff *reply = NULL;
1195 	struct datapath *dp;
1196 	struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1197 	struct sw_flow_match match;
1198 	struct sw_flow_id sfid;
1199 	u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1200 	int error = 0;
1201 	bool log = !a[OVS_FLOW_ATTR_PROBE];
1202 	bool ufid_present;
1203 
1204 	ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1205 	if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
1206 		OVS_NLERR(log,
1207 			  "Flow set message rejected, Key attribute missing.");
1208 		return -EINVAL;
1209 	}
1210 
1211 	error = ovs_nla_init_match_and_action(net, &match, &key, a,
1212 					      &acts, log);
1213 	if (error)
1214 		goto error;
1215 
1216 	if (acts) {
1217 		/* Can allocate before locking if have acts. */
1218 		reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1219 						ufid_flags);
1220 		if (IS_ERR(reply)) {
1221 			error = PTR_ERR(reply);
1222 			goto err_kfree_acts;
1223 		}
1224 	}
1225 
1226 	ovs_lock();
1227 	dp = get_dp(net, ovs_header->dp_ifindex);
1228 	if (unlikely(!dp)) {
1229 		error = -ENODEV;
1230 		goto err_unlock_ovs;
1231 	}
1232 	/* Check that the flow exists. */
1233 	if (ufid_present)
1234 		flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1235 	else
1236 		flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1237 	if (unlikely(!flow)) {
1238 		error = -ENOENT;
1239 		goto err_unlock_ovs;
1240 	}
1241 
1242 	/* Update actions, if present. */
1243 	if (likely(acts)) {
1244 		old_acts = ovsl_dereference(flow->sf_acts);
1245 		rcu_assign_pointer(flow->sf_acts, acts);
1246 
1247 		if (unlikely(reply)) {
1248 			error = ovs_flow_cmd_fill_info(flow,
1249 						       ovs_header->dp_ifindex,
1250 						       reply, info->snd_portid,
1251 						       info->snd_seq, 0,
1252 						       OVS_FLOW_CMD_SET,
1253 						       ufid_flags);
1254 			BUG_ON(error < 0);
1255 		}
1256 	} else {
1257 		/* Could not alloc without acts before locking. */
1258 		reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1259 						info, OVS_FLOW_CMD_SET, false,
1260 						ufid_flags);
1261 
1262 		if (IS_ERR(reply)) {
1263 			error = PTR_ERR(reply);
1264 			goto err_unlock_ovs;
1265 		}
1266 	}
1267 
1268 	/* Clear stats. */
1269 	if (a[OVS_FLOW_ATTR_CLEAR])
1270 		ovs_flow_stats_clear(flow);
1271 	ovs_unlock();
1272 
1273 	if (reply)
1274 		ovs_notify(&dp_flow_genl_family, reply, info);
1275 	if (old_acts)
1276 		ovs_nla_free_flow_actions_rcu(old_acts);
1277 
1278 	return 0;
1279 
1280 err_unlock_ovs:
1281 	ovs_unlock();
1282 	kfree_skb(reply);
1283 err_kfree_acts:
1284 	ovs_nla_free_flow_actions(acts);
1285 error:
1286 	return error;
1287 }
1288 
ovs_flow_cmd_get(struct sk_buff * skb,struct genl_info * info)1289 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1290 {
1291 	struct nlattr **a = info->attrs;
1292 	struct ovs_header *ovs_header = info->userhdr;
1293 	struct net *net = sock_net(skb->sk);
1294 	struct sw_flow_key key;
1295 	struct sk_buff *reply;
1296 	struct sw_flow *flow;
1297 	struct datapath *dp;
1298 	struct sw_flow_match match;
1299 	struct sw_flow_id ufid;
1300 	u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1301 	int err = 0;
1302 	bool log = !a[OVS_FLOW_ATTR_PROBE];
1303 	bool ufid_present;
1304 
1305 	ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1306 	if (a[OVS_FLOW_ATTR_KEY]) {
1307 		ovs_match_init(&match, &key, true, NULL);
1308 		err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1309 					log);
1310 	} else if (!ufid_present) {
1311 		OVS_NLERR(log,
1312 			  "Flow get message rejected, Key attribute missing.");
1313 		err = -EINVAL;
1314 	}
1315 	if (err)
1316 		return err;
1317 
1318 	ovs_lock();
1319 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1320 	if (!dp) {
1321 		err = -ENODEV;
1322 		goto unlock;
1323 	}
1324 
1325 	if (ufid_present)
1326 		flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1327 	else
1328 		flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1329 	if (!flow) {
1330 		err = -ENOENT;
1331 		goto unlock;
1332 	}
1333 
1334 	reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1335 					OVS_FLOW_CMD_GET, true, ufid_flags);
1336 	if (IS_ERR(reply)) {
1337 		err = PTR_ERR(reply);
1338 		goto unlock;
1339 	}
1340 
1341 	ovs_unlock();
1342 	return genlmsg_reply(reply, info);
1343 unlock:
1344 	ovs_unlock();
1345 	return err;
1346 }
1347 
ovs_flow_cmd_del(struct sk_buff * skb,struct genl_info * info)1348 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1349 {
1350 	struct nlattr **a = info->attrs;
1351 	struct ovs_header *ovs_header = info->userhdr;
1352 	struct net *net = sock_net(skb->sk);
1353 	struct sw_flow_key key;
1354 	struct sk_buff *reply;
1355 	struct sw_flow *flow = NULL;
1356 	struct datapath *dp;
1357 	struct sw_flow_match match;
1358 	struct sw_flow_id ufid;
1359 	u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1360 	int err;
1361 	bool log = !a[OVS_FLOW_ATTR_PROBE];
1362 	bool ufid_present;
1363 
1364 	ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1365 	if (a[OVS_FLOW_ATTR_KEY]) {
1366 		ovs_match_init(&match, &key, true, NULL);
1367 		err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1368 					NULL, log);
1369 		if (unlikely(err))
1370 			return err;
1371 	}
1372 
1373 	ovs_lock();
1374 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1375 	if (unlikely(!dp)) {
1376 		err = -ENODEV;
1377 		goto unlock;
1378 	}
1379 
1380 	if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1381 		err = ovs_flow_tbl_flush(&dp->table);
1382 		goto unlock;
1383 	}
1384 
1385 	if (ufid_present)
1386 		flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1387 	else
1388 		flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1389 	if (unlikely(!flow)) {
1390 		err = -ENOENT;
1391 		goto unlock;
1392 	}
1393 
1394 	ovs_flow_tbl_remove(&dp->table, flow);
1395 	ovs_unlock();
1396 
1397 	reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1398 					&flow->id, info, false, ufid_flags);
1399 	if (likely(reply)) {
1400 		if (!IS_ERR(reply)) {
1401 			rcu_read_lock();	/*To keep RCU checker happy. */
1402 			err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1403 						     reply, info->snd_portid,
1404 						     info->snd_seq, 0,
1405 						     OVS_FLOW_CMD_DEL,
1406 						     ufid_flags);
1407 			rcu_read_unlock();
1408 			if (WARN_ON_ONCE(err < 0)) {
1409 				kfree_skb(reply);
1410 				goto out_free;
1411 			}
1412 
1413 			ovs_notify(&dp_flow_genl_family, reply, info);
1414 		} else {
1415 			netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0,
1416 					PTR_ERR(reply));
1417 		}
1418 	}
1419 
1420 out_free:
1421 	ovs_flow_free(flow, true);
1422 	return 0;
1423 unlock:
1424 	ovs_unlock();
1425 	return err;
1426 }
1427 
ovs_flow_cmd_dump(struct sk_buff * skb,struct netlink_callback * cb)1428 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1429 {
1430 	struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1431 	struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1432 	struct table_instance *ti;
1433 	struct datapath *dp;
1434 	u32 ufid_flags;
1435 	int err;
1436 
1437 	err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a,
1438 				       OVS_FLOW_ATTR_MAX, flow_policy, NULL);
1439 	if (err)
1440 		return err;
1441 	ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1442 
1443 	rcu_read_lock();
1444 	dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1445 	if (!dp) {
1446 		rcu_read_unlock();
1447 		return -ENODEV;
1448 	}
1449 
1450 	ti = rcu_dereference(dp->table.ti);
1451 	for (;;) {
1452 		struct sw_flow *flow;
1453 		u32 bucket, obj;
1454 
1455 		bucket = cb->args[0];
1456 		obj = cb->args[1];
1457 		flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1458 		if (!flow)
1459 			break;
1460 
1461 		if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1462 					   NETLINK_CB(cb->skb).portid,
1463 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
1464 					   OVS_FLOW_CMD_GET, ufid_flags) < 0)
1465 			break;
1466 
1467 		cb->args[0] = bucket;
1468 		cb->args[1] = obj;
1469 	}
1470 	rcu_read_unlock();
1471 	return skb->len;
1472 }
1473 
1474 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1475 	[OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1476 	[OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1477 	[OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1478 	[OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1479 	[OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1480 	[OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1481 	[OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1482 };
1483 
1484 static const struct genl_small_ops dp_flow_genl_ops[] = {
1485 	{ .cmd = OVS_FLOW_CMD_NEW,
1486 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1487 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1488 	  .doit = ovs_flow_cmd_new
1489 	},
1490 	{ .cmd = OVS_FLOW_CMD_DEL,
1491 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1492 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1493 	  .doit = ovs_flow_cmd_del
1494 	},
1495 	{ .cmd = OVS_FLOW_CMD_GET,
1496 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1497 	  .flags = 0,		    /* OK for unprivileged users. */
1498 	  .doit = ovs_flow_cmd_get,
1499 	  .dumpit = ovs_flow_cmd_dump
1500 	},
1501 	{ .cmd = OVS_FLOW_CMD_SET,
1502 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1503 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1504 	  .doit = ovs_flow_cmd_set,
1505 	},
1506 };
1507 
1508 static struct genl_family dp_flow_genl_family __ro_after_init = {
1509 	.hdrsize = sizeof(struct ovs_header),
1510 	.name = OVS_FLOW_FAMILY,
1511 	.version = OVS_FLOW_VERSION,
1512 	.maxattr = OVS_FLOW_ATTR_MAX,
1513 	.policy = flow_policy,
1514 	.netnsok = true,
1515 	.parallel_ops = true,
1516 	.small_ops = dp_flow_genl_ops,
1517 	.n_small_ops = ARRAY_SIZE(dp_flow_genl_ops),
1518 	.mcgrps = &ovs_dp_flow_multicast_group,
1519 	.n_mcgrps = 1,
1520 	.module = THIS_MODULE,
1521 };
1522 
ovs_dp_cmd_msg_size(void)1523 static size_t ovs_dp_cmd_msg_size(void)
1524 {
1525 	size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1526 
1527 	msgsize += nla_total_size(IFNAMSIZ);
1528 	msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1529 	msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
1530 	msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1531 	msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_MASKS_CACHE_SIZE */
1532 
1533 	return msgsize;
1534 }
1535 
1536 /* Called with ovs_mutex. */
ovs_dp_cmd_fill_info(struct datapath * dp,struct sk_buff * skb,u32 portid,u32 seq,u32 flags,u8 cmd)1537 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1538 				u32 portid, u32 seq, u32 flags, u8 cmd)
1539 {
1540 	struct ovs_header *ovs_header;
1541 	struct ovs_dp_stats dp_stats;
1542 	struct ovs_dp_megaflow_stats dp_megaflow_stats;
1543 	int err;
1544 
1545 	ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1546 				 flags, cmd);
1547 	if (!ovs_header)
1548 		goto error;
1549 
1550 	ovs_header->dp_ifindex = get_dpifindex(dp);
1551 
1552 	err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1553 	if (err)
1554 		goto nla_put_failure;
1555 
1556 	get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1557 	if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1558 			  &dp_stats, OVS_DP_ATTR_PAD))
1559 		goto nla_put_failure;
1560 
1561 	if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1562 			  sizeof(struct ovs_dp_megaflow_stats),
1563 			  &dp_megaflow_stats, OVS_DP_ATTR_PAD))
1564 		goto nla_put_failure;
1565 
1566 	if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1567 		goto nla_put_failure;
1568 
1569 	if (nla_put_u32(skb, OVS_DP_ATTR_MASKS_CACHE_SIZE,
1570 			ovs_flow_tbl_masks_cache_size(&dp->table)))
1571 		goto nla_put_failure;
1572 
1573 	genlmsg_end(skb, ovs_header);
1574 	return 0;
1575 
1576 nla_put_failure:
1577 	genlmsg_cancel(skb, ovs_header);
1578 error:
1579 	return -EMSGSIZE;
1580 }
1581 
ovs_dp_cmd_alloc_info(void)1582 static struct sk_buff *ovs_dp_cmd_alloc_info(void)
1583 {
1584 	return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1585 }
1586 
1587 /* Called with rcu_read_lock or ovs_mutex. */
lookup_datapath(struct net * net,const struct ovs_header * ovs_header,struct nlattr * a[OVS_DP_ATTR_MAX+1])1588 static struct datapath *lookup_datapath(struct net *net,
1589 					const struct ovs_header *ovs_header,
1590 					struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1591 {
1592 	struct datapath *dp;
1593 
1594 	if (!a[OVS_DP_ATTR_NAME])
1595 		dp = get_dp(net, ovs_header->dp_ifindex);
1596 	else {
1597 		struct vport *vport;
1598 
1599 		vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1600 		dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1601 	}
1602 	return dp ? dp : ERR_PTR(-ENODEV);
1603 }
1604 
ovs_dp_reset_user_features(struct sk_buff * skb,struct genl_info * info)1605 static void ovs_dp_reset_user_features(struct sk_buff *skb,
1606 				       struct genl_info *info)
1607 {
1608 	struct datapath *dp;
1609 
1610 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr,
1611 			     info->attrs);
1612 	if (IS_ERR(dp))
1613 		return;
1614 
1615 	pr_warn("%s: Dropping previously announced user features\n",
1616 		ovs_dp_name(dp));
1617 	dp->user_features = 0;
1618 }
1619 
1620 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support);
1621 
ovs_dp_set_upcall_portids(struct datapath * dp,const struct nlattr * ids)1622 static int ovs_dp_set_upcall_portids(struct datapath *dp,
1623 			      const struct nlattr *ids)
1624 {
1625 	struct dp_nlsk_pids *old, *dp_nlsk_pids;
1626 
1627 	if (!nla_len(ids) || nla_len(ids) % sizeof(u32))
1628 		return -EINVAL;
1629 
1630 	old = ovsl_dereference(dp->upcall_portids);
1631 
1632 	dp_nlsk_pids = kmalloc(sizeof(*dp_nlsk_pids) + nla_len(ids),
1633 			       GFP_KERNEL);
1634 	if (!dp_nlsk_pids)
1635 		return -ENOMEM;
1636 
1637 	dp_nlsk_pids->n_pids = nla_len(ids) / sizeof(u32);
1638 	nla_memcpy(dp_nlsk_pids->pids, ids, nla_len(ids));
1639 
1640 	rcu_assign_pointer(dp->upcall_portids, dp_nlsk_pids);
1641 
1642 	kfree_rcu(old, rcu);
1643 
1644 	return 0;
1645 }
1646 
ovs_dp_get_upcall_portid(const struct datapath * dp,uint32_t cpu_id)1647 u32 ovs_dp_get_upcall_portid(const struct datapath *dp, uint32_t cpu_id)
1648 {
1649 	struct dp_nlsk_pids *dp_nlsk_pids;
1650 
1651 	dp_nlsk_pids = rcu_dereference(dp->upcall_portids);
1652 
1653 	if (dp_nlsk_pids) {
1654 		if (cpu_id < dp_nlsk_pids->n_pids) {
1655 			return dp_nlsk_pids->pids[cpu_id];
1656 		} else if (dp_nlsk_pids->n_pids > 0 &&
1657 			   cpu_id >= dp_nlsk_pids->n_pids) {
1658 			/* If the number of netlink PIDs is mismatched with
1659 			 * the number of CPUs as seen by the kernel, log this
1660 			 * and send the upcall to an arbitrary socket (0) in
1661 			 * order to not drop packets
1662 			 */
1663 			pr_info_ratelimited("cpu_id mismatch with handler threads");
1664 			return dp_nlsk_pids->pids[cpu_id %
1665 						  dp_nlsk_pids->n_pids];
1666 		} else {
1667 			return 0;
1668 		}
1669 	} else {
1670 		return 0;
1671 	}
1672 }
1673 
ovs_dp_change(struct datapath * dp,struct nlattr * a[])1674 static int ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1675 {
1676 	u32 user_features = 0;
1677 	int err;
1678 
1679 	if (a[OVS_DP_ATTR_USER_FEATURES]) {
1680 		user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1681 
1682 		if (user_features & ~(OVS_DP_F_VPORT_PIDS |
1683 				      OVS_DP_F_UNALIGNED |
1684 				      OVS_DP_F_TC_RECIRC_SHARING |
1685 				      OVS_DP_F_DISPATCH_UPCALL_PER_CPU))
1686 			return -EOPNOTSUPP;
1687 
1688 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1689 		if (user_features & OVS_DP_F_TC_RECIRC_SHARING)
1690 			return -EOPNOTSUPP;
1691 #endif
1692 	}
1693 
1694 	if (a[OVS_DP_ATTR_MASKS_CACHE_SIZE]) {
1695 		int err;
1696 		u32 cache_size;
1697 
1698 		cache_size = nla_get_u32(a[OVS_DP_ATTR_MASKS_CACHE_SIZE]);
1699 		err = ovs_flow_tbl_masks_cache_resize(&dp->table, cache_size);
1700 		if (err)
1701 			return err;
1702 	}
1703 
1704 	dp->user_features = user_features;
1705 
1706 	if (dp->user_features & OVS_DP_F_DISPATCH_UPCALL_PER_CPU &&
1707 	    a[OVS_DP_ATTR_PER_CPU_PIDS]) {
1708 		/* Upcall Netlink Port IDs have been updated */
1709 		err = ovs_dp_set_upcall_portids(dp,
1710 						a[OVS_DP_ATTR_PER_CPU_PIDS]);
1711 		if (err)
1712 			return err;
1713 	}
1714 
1715 	if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
1716 		static_branch_enable(&tc_recirc_sharing_support);
1717 	else
1718 		static_branch_disable(&tc_recirc_sharing_support);
1719 
1720 	return 0;
1721 }
1722 
ovs_dp_stats_init(struct datapath * dp)1723 static int ovs_dp_stats_init(struct datapath *dp)
1724 {
1725 	dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1726 	if (!dp->stats_percpu)
1727 		return -ENOMEM;
1728 
1729 	return 0;
1730 }
1731 
ovs_dp_vport_init(struct datapath * dp)1732 static int ovs_dp_vport_init(struct datapath *dp)
1733 {
1734 	int i;
1735 
1736 	dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1737 				  sizeof(struct hlist_head),
1738 				  GFP_KERNEL);
1739 	if (!dp->ports)
1740 		return -ENOMEM;
1741 
1742 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1743 		INIT_HLIST_HEAD(&dp->ports[i]);
1744 
1745 	return 0;
1746 }
1747 
ovs_dp_cmd_new(struct sk_buff * skb,struct genl_info * info)1748 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1749 {
1750 	struct nlattr **a = info->attrs;
1751 	struct vport_parms parms;
1752 	struct sk_buff *reply;
1753 	struct datapath *dp;
1754 	struct vport *vport;
1755 	struct ovs_net *ovs_net;
1756 	int err;
1757 
1758 	err = -EINVAL;
1759 	if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1760 		goto err;
1761 
1762 	reply = ovs_dp_cmd_alloc_info();
1763 	if (!reply)
1764 		return -ENOMEM;
1765 
1766 	err = -ENOMEM;
1767 	dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1768 	if (dp == NULL)
1769 		goto err_destroy_reply;
1770 
1771 	ovs_dp_set_net(dp, sock_net(skb->sk));
1772 
1773 	/* Allocate table. */
1774 	err = ovs_flow_tbl_init(&dp->table);
1775 	if (err)
1776 		goto err_destroy_dp;
1777 
1778 	err = ovs_dp_stats_init(dp);
1779 	if (err)
1780 		goto err_destroy_table;
1781 
1782 	err = ovs_dp_vport_init(dp);
1783 	if (err)
1784 		goto err_destroy_stats;
1785 
1786 	err = ovs_meters_init(dp);
1787 	if (err)
1788 		goto err_destroy_ports;
1789 
1790 	/* Set up our datapath device. */
1791 	parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1792 	parms.type = OVS_VPORT_TYPE_INTERNAL;
1793 	parms.options = NULL;
1794 	parms.dp = dp;
1795 	parms.port_no = OVSP_LOCAL;
1796 	parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1797 
1798 	/* So far only local changes have been made, now need the lock. */
1799 	ovs_lock();
1800 
1801 	err = ovs_dp_change(dp, a);
1802 	if (err)
1803 		goto err_unlock_and_destroy_meters;
1804 
1805 	vport = new_vport(&parms);
1806 	if (IS_ERR(vport)) {
1807 		err = PTR_ERR(vport);
1808 		if (err == -EBUSY)
1809 			err = -EEXIST;
1810 
1811 		if (err == -EEXIST) {
1812 			/* An outdated user space instance that does not understand
1813 			 * the concept of user_features has attempted to create a new
1814 			 * datapath and is likely to reuse it. Drop all user features.
1815 			 */
1816 			if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1817 				ovs_dp_reset_user_features(skb, info);
1818 		}
1819 
1820 		goto err_destroy_portids;
1821 	}
1822 
1823 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1824 				   info->snd_seq, 0, OVS_DP_CMD_NEW);
1825 	BUG_ON(err < 0);
1826 
1827 	ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1828 	list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1829 
1830 	ovs_unlock();
1831 
1832 	ovs_notify(&dp_datapath_genl_family, reply, info);
1833 	return 0;
1834 
1835 err_destroy_portids:
1836 	kfree(rcu_dereference_raw(dp->upcall_portids));
1837 err_unlock_and_destroy_meters:
1838 	ovs_unlock();
1839 	ovs_meters_exit(dp);
1840 err_destroy_ports:
1841 	kfree(dp->ports);
1842 err_destroy_stats:
1843 	free_percpu(dp->stats_percpu);
1844 err_destroy_table:
1845 	ovs_flow_tbl_destroy(&dp->table);
1846 err_destroy_dp:
1847 	kfree(dp);
1848 err_destroy_reply:
1849 	kfree_skb(reply);
1850 err:
1851 	return err;
1852 }
1853 
1854 /* Called with ovs_mutex. */
__dp_destroy(struct datapath * dp)1855 static void __dp_destroy(struct datapath *dp)
1856 {
1857 	struct flow_table *table = &dp->table;
1858 	int i;
1859 
1860 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1861 		struct vport *vport;
1862 		struct hlist_node *n;
1863 
1864 		hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1865 			if (vport->port_no != OVSP_LOCAL)
1866 				ovs_dp_detach_port(vport);
1867 	}
1868 
1869 	list_del_rcu(&dp->list_node);
1870 
1871 	/* OVSP_LOCAL is datapath internal port. We need to make sure that
1872 	 * all ports in datapath are destroyed first before freeing datapath.
1873 	 */
1874 	ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1875 
1876 	/* Flush sw_flow in the tables. RCU cb only releases resource
1877 	 * such as dp, ports and tables. That may avoid some issues
1878 	 * such as RCU usage warning.
1879 	 */
1880 	table_instance_flow_flush(table, ovsl_dereference(table->ti),
1881 				  ovsl_dereference(table->ufid_ti));
1882 
1883 	/* RCU destroy the ports, meters and flow tables. */
1884 	call_rcu(&dp->rcu, destroy_dp_rcu);
1885 }
1886 
ovs_dp_cmd_del(struct sk_buff * skb,struct genl_info * info)1887 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1888 {
1889 	struct sk_buff *reply;
1890 	struct datapath *dp;
1891 	int err;
1892 
1893 	reply = ovs_dp_cmd_alloc_info();
1894 	if (!reply)
1895 		return -ENOMEM;
1896 
1897 	ovs_lock();
1898 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1899 	err = PTR_ERR(dp);
1900 	if (IS_ERR(dp))
1901 		goto err_unlock_free;
1902 
1903 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1904 				   info->snd_seq, 0, OVS_DP_CMD_DEL);
1905 	BUG_ON(err < 0);
1906 
1907 	__dp_destroy(dp);
1908 	ovs_unlock();
1909 
1910 	ovs_notify(&dp_datapath_genl_family, reply, info);
1911 
1912 	return 0;
1913 
1914 err_unlock_free:
1915 	ovs_unlock();
1916 	kfree_skb(reply);
1917 	return err;
1918 }
1919 
ovs_dp_cmd_set(struct sk_buff * skb,struct genl_info * info)1920 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1921 {
1922 	struct sk_buff *reply;
1923 	struct datapath *dp;
1924 	int err;
1925 
1926 	reply = ovs_dp_cmd_alloc_info();
1927 	if (!reply)
1928 		return -ENOMEM;
1929 
1930 	ovs_lock();
1931 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1932 	err = PTR_ERR(dp);
1933 	if (IS_ERR(dp))
1934 		goto err_unlock_free;
1935 
1936 	err = ovs_dp_change(dp, info->attrs);
1937 	if (err)
1938 		goto err_unlock_free;
1939 
1940 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1941 				   info->snd_seq, 0, OVS_DP_CMD_SET);
1942 	BUG_ON(err < 0);
1943 
1944 	ovs_unlock();
1945 	ovs_notify(&dp_datapath_genl_family, reply, info);
1946 
1947 	return 0;
1948 
1949 err_unlock_free:
1950 	ovs_unlock();
1951 	kfree_skb(reply);
1952 	return err;
1953 }
1954 
ovs_dp_cmd_get(struct sk_buff * skb,struct genl_info * info)1955 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1956 {
1957 	struct sk_buff *reply;
1958 	struct datapath *dp;
1959 	int err;
1960 
1961 	reply = ovs_dp_cmd_alloc_info();
1962 	if (!reply)
1963 		return -ENOMEM;
1964 
1965 	ovs_lock();
1966 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1967 	if (IS_ERR(dp)) {
1968 		err = PTR_ERR(dp);
1969 		goto err_unlock_free;
1970 	}
1971 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1972 				   info->snd_seq, 0, OVS_DP_CMD_GET);
1973 	BUG_ON(err < 0);
1974 	ovs_unlock();
1975 
1976 	return genlmsg_reply(reply, info);
1977 
1978 err_unlock_free:
1979 	ovs_unlock();
1980 	kfree_skb(reply);
1981 	return err;
1982 }
1983 
ovs_dp_cmd_dump(struct sk_buff * skb,struct netlink_callback * cb)1984 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1985 {
1986 	struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1987 	struct datapath *dp;
1988 	int skip = cb->args[0];
1989 	int i = 0;
1990 
1991 	ovs_lock();
1992 	list_for_each_entry(dp, &ovs_net->dps, list_node) {
1993 		if (i >= skip &&
1994 		    ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1995 					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1996 					 OVS_DP_CMD_GET) < 0)
1997 			break;
1998 		i++;
1999 	}
2000 	ovs_unlock();
2001 
2002 	cb->args[0] = i;
2003 
2004 	return skb->len;
2005 }
2006 
2007 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
2008 	[OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2009 	[OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2010 	[OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
2011 	[OVS_DP_ATTR_MASKS_CACHE_SIZE] =  NLA_POLICY_RANGE(NLA_U32, 0,
2012 		PCPU_MIN_UNIT_SIZE / sizeof(struct mask_cache_entry)),
2013 };
2014 
2015 static const struct genl_small_ops dp_datapath_genl_ops[] = {
2016 	{ .cmd = OVS_DP_CMD_NEW,
2017 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2018 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2019 	  .doit = ovs_dp_cmd_new
2020 	},
2021 	{ .cmd = OVS_DP_CMD_DEL,
2022 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2023 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2024 	  .doit = ovs_dp_cmd_del
2025 	},
2026 	{ .cmd = OVS_DP_CMD_GET,
2027 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2028 	  .flags = 0,		    /* OK for unprivileged users. */
2029 	  .doit = ovs_dp_cmd_get,
2030 	  .dumpit = ovs_dp_cmd_dump
2031 	},
2032 	{ .cmd = OVS_DP_CMD_SET,
2033 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2034 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2035 	  .doit = ovs_dp_cmd_set,
2036 	},
2037 };
2038 
2039 static struct genl_family dp_datapath_genl_family __ro_after_init = {
2040 	.hdrsize = sizeof(struct ovs_header),
2041 	.name = OVS_DATAPATH_FAMILY,
2042 	.version = OVS_DATAPATH_VERSION,
2043 	.maxattr = OVS_DP_ATTR_MAX,
2044 	.policy = datapath_policy,
2045 	.netnsok = true,
2046 	.parallel_ops = true,
2047 	.small_ops = dp_datapath_genl_ops,
2048 	.n_small_ops = ARRAY_SIZE(dp_datapath_genl_ops),
2049 	.mcgrps = &ovs_dp_datapath_multicast_group,
2050 	.n_mcgrps = 1,
2051 	.module = THIS_MODULE,
2052 };
2053 
2054 /* Called with ovs_mutex or RCU read lock. */
ovs_vport_cmd_fill_info(struct vport * vport,struct sk_buff * skb,struct net * net,u32 portid,u32 seq,u32 flags,u8 cmd,gfp_t gfp)2055 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
2056 				   struct net *net, u32 portid, u32 seq,
2057 				   u32 flags, u8 cmd, gfp_t gfp)
2058 {
2059 	struct ovs_header *ovs_header;
2060 	struct ovs_vport_stats vport_stats;
2061 	int err;
2062 
2063 	ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
2064 				 flags, cmd);
2065 	if (!ovs_header)
2066 		return -EMSGSIZE;
2067 
2068 	ovs_header->dp_ifindex = get_dpifindex(vport->dp);
2069 
2070 	if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
2071 	    nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
2072 	    nla_put_string(skb, OVS_VPORT_ATTR_NAME,
2073 			   ovs_vport_name(vport)) ||
2074 	    nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
2075 		goto nla_put_failure;
2076 
2077 	if (!net_eq(net, dev_net(vport->dev))) {
2078 		int id = peernet2id_alloc(net, dev_net(vport->dev), gfp);
2079 
2080 		if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
2081 			goto nla_put_failure;
2082 	}
2083 
2084 	ovs_vport_get_stats(vport, &vport_stats);
2085 	if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
2086 			  sizeof(struct ovs_vport_stats), &vport_stats,
2087 			  OVS_VPORT_ATTR_PAD))
2088 		goto nla_put_failure;
2089 
2090 	if (ovs_vport_get_upcall_portids(vport, skb))
2091 		goto nla_put_failure;
2092 
2093 	err = ovs_vport_get_options(vport, skb);
2094 	if (err == -EMSGSIZE)
2095 		goto error;
2096 
2097 	genlmsg_end(skb, ovs_header);
2098 	return 0;
2099 
2100 nla_put_failure:
2101 	err = -EMSGSIZE;
2102 error:
2103 	genlmsg_cancel(skb, ovs_header);
2104 	return err;
2105 }
2106 
ovs_vport_cmd_alloc_info(void)2107 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
2108 {
2109 	return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2110 }
2111 
2112 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
ovs_vport_cmd_build_info(struct vport * vport,struct net * net,u32 portid,u32 seq,u8 cmd)2113 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
2114 					 u32 portid, u32 seq, u8 cmd)
2115 {
2116 	struct sk_buff *skb;
2117 	int retval;
2118 
2119 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2120 	if (!skb)
2121 		return ERR_PTR(-ENOMEM);
2122 
2123 	retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd,
2124 					 GFP_KERNEL);
2125 	BUG_ON(retval < 0);
2126 
2127 	return skb;
2128 }
2129 
2130 /* Called with ovs_mutex or RCU read lock. */
lookup_vport(struct net * net,const struct ovs_header * ovs_header,struct nlattr * a[OVS_VPORT_ATTR_MAX+1])2131 static struct vport *lookup_vport(struct net *net,
2132 				  const struct ovs_header *ovs_header,
2133 				  struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2134 {
2135 	struct datapath *dp;
2136 	struct vport *vport;
2137 
2138 	if (a[OVS_VPORT_ATTR_IFINDEX])
2139 		return ERR_PTR(-EOPNOTSUPP);
2140 	if (a[OVS_VPORT_ATTR_NAME]) {
2141 		vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2142 		if (!vport)
2143 			return ERR_PTR(-ENODEV);
2144 		if (ovs_header->dp_ifindex &&
2145 		    ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2146 			return ERR_PTR(-ENODEV);
2147 		return vport;
2148 	} else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2149 		u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2150 
2151 		if (port_no >= DP_MAX_PORTS)
2152 			return ERR_PTR(-EFBIG);
2153 
2154 		dp = get_dp(net, ovs_header->dp_ifindex);
2155 		if (!dp)
2156 			return ERR_PTR(-ENODEV);
2157 
2158 		vport = ovs_vport_ovsl_rcu(dp, port_no);
2159 		if (!vport)
2160 			return ERR_PTR(-ENODEV);
2161 		return vport;
2162 	} else
2163 		return ERR_PTR(-EINVAL);
2164 
2165 }
2166 
ovs_get_max_headroom(struct datapath * dp)2167 static unsigned int ovs_get_max_headroom(struct datapath *dp)
2168 {
2169 	unsigned int dev_headroom, max_headroom = 0;
2170 	struct net_device *dev;
2171 	struct vport *vport;
2172 	int i;
2173 
2174 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2175 		hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node,
2176 					 lockdep_ovsl_is_held()) {
2177 			dev = vport->dev;
2178 			dev_headroom = netdev_get_fwd_headroom(dev);
2179 			if (dev_headroom > max_headroom)
2180 				max_headroom = dev_headroom;
2181 		}
2182 	}
2183 
2184 	return max_headroom;
2185 }
2186 
2187 /* Called with ovs_mutex */
ovs_update_headroom(struct datapath * dp,unsigned int new_headroom)2188 static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
2189 {
2190 	struct vport *vport;
2191 	int i;
2192 
2193 	dp->max_headroom = new_headroom;
2194 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2195 		hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node,
2196 					 lockdep_ovsl_is_held())
2197 			netdev_set_rx_headroom(vport->dev, new_headroom);
2198 	}
2199 }
2200 
ovs_vport_cmd_new(struct sk_buff * skb,struct genl_info * info)2201 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2202 {
2203 	struct nlattr **a = info->attrs;
2204 	struct ovs_header *ovs_header = info->userhdr;
2205 	struct vport_parms parms;
2206 	struct sk_buff *reply;
2207 	struct vport *vport;
2208 	struct datapath *dp;
2209 	unsigned int new_headroom;
2210 	u32 port_no;
2211 	int err;
2212 
2213 	if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2214 	    !a[OVS_VPORT_ATTR_UPCALL_PID])
2215 		return -EINVAL;
2216 	if (a[OVS_VPORT_ATTR_IFINDEX])
2217 		return -EOPNOTSUPP;
2218 
2219 	port_no = a[OVS_VPORT_ATTR_PORT_NO]
2220 		? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2221 	if (port_no >= DP_MAX_PORTS)
2222 		return -EFBIG;
2223 
2224 	reply = ovs_vport_cmd_alloc_info();
2225 	if (!reply)
2226 		return -ENOMEM;
2227 
2228 	ovs_lock();
2229 restart:
2230 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2231 	err = -ENODEV;
2232 	if (!dp)
2233 		goto exit_unlock_free;
2234 
2235 	if (port_no) {
2236 		vport = ovs_vport_ovsl(dp, port_no);
2237 		err = -EBUSY;
2238 		if (vport)
2239 			goto exit_unlock_free;
2240 	} else {
2241 		for (port_no = 1; ; port_no++) {
2242 			if (port_no >= DP_MAX_PORTS) {
2243 				err = -EFBIG;
2244 				goto exit_unlock_free;
2245 			}
2246 			vport = ovs_vport_ovsl(dp, port_no);
2247 			if (!vport)
2248 				break;
2249 		}
2250 	}
2251 
2252 	parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2253 	parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2254 	parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2255 	parms.dp = dp;
2256 	parms.port_no = port_no;
2257 	parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2258 
2259 	vport = new_vport(&parms);
2260 	err = PTR_ERR(vport);
2261 	if (IS_ERR(vport)) {
2262 		if (err == -EAGAIN)
2263 			goto restart;
2264 		goto exit_unlock_free;
2265 	}
2266 
2267 	err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2268 				      info->snd_portid, info->snd_seq, 0,
2269 				      OVS_VPORT_CMD_NEW, GFP_KERNEL);
2270 
2271 	new_headroom = netdev_get_fwd_headroom(vport->dev);
2272 
2273 	if (new_headroom > dp->max_headroom)
2274 		ovs_update_headroom(dp, new_headroom);
2275 	else
2276 		netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2277 
2278 	BUG_ON(err < 0);
2279 	ovs_unlock();
2280 
2281 	ovs_notify(&dp_vport_genl_family, reply, info);
2282 	return 0;
2283 
2284 exit_unlock_free:
2285 	ovs_unlock();
2286 	kfree_skb(reply);
2287 	return err;
2288 }
2289 
ovs_vport_cmd_set(struct sk_buff * skb,struct genl_info * info)2290 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2291 {
2292 	struct nlattr **a = info->attrs;
2293 	struct sk_buff *reply;
2294 	struct vport *vport;
2295 	int err;
2296 
2297 	reply = ovs_vport_cmd_alloc_info();
2298 	if (!reply)
2299 		return -ENOMEM;
2300 
2301 	ovs_lock();
2302 	vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2303 	err = PTR_ERR(vport);
2304 	if (IS_ERR(vport))
2305 		goto exit_unlock_free;
2306 
2307 	if (a[OVS_VPORT_ATTR_TYPE] &&
2308 	    nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2309 		err = -EINVAL;
2310 		goto exit_unlock_free;
2311 	}
2312 
2313 	if (a[OVS_VPORT_ATTR_OPTIONS]) {
2314 		err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2315 		if (err)
2316 			goto exit_unlock_free;
2317 	}
2318 
2319 
2320 	if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2321 		struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2322 
2323 		err = ovs_vport_set_upcall_portids(vport, ids);
2324 		if (err)
2325 			goto exit_unlock_free;
2326 	}
2327 
2328 	err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2329 				      info->snd_portid, info->snd_seq, 0,
2330 				      OVS_VPORT_CMD_SET, GFP_KERNEL);
2331 	BUG_ON(err < 0);
2332 
2333 	ovs_unlock();
2334 	ovs_notify(&dp_vport_genl_family, reply, info);
2335 	return 0;
2336 
2337 exit_unlock_free:
2338 	ovs_unlock();
2339 	kfree_skb(reply);
2340 	return err;
2341 }
2342 
ovs_vport_cmd_del(struct sk_buff * skb,struct genl_info * info)2343 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2344 {
2345 	bool update_headroom = false;
2346 	struct nlattr **a = info->attrs;
2347 	struct sk_buff *reply;
2348 	struct datapath *dp;
2349 	struct vport *vport;
2350 	unsigned int new_headroom;
2351 	int err;
2352 
2353 	reply = ovs_vport_cmd_alloc_info();
2354 	if (!reply)
2355 		return -ENOMEM;
2356 
2357 	ovs_lock();
2358 	vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2359 	err = PTR_ERR(vport);
2360 	if (IS_ERR(vport))
2361 		goto exit_unlock_free;
2362 
2363 	if (vport->port_no == OVSP_LOCAL) {
2364 		err = -EINVAL;
2365 		goto exit_unlock_free;
2366 	}
2367 
2368 	err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2369 				      info->snd_portid, info->snd_seq, 0,
2370 				      OVS_VPORT_CMD_DEL, GFP_KERNEL);
2371 	BUG_ON(err < 0);
2372 
2373 	/* the vport deletion may trigger dp headroom update */
2374 	dp = vport->dp;
2375 	if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2376 		update_headroom = true;
2377 
2378 	netdev_reset_rx_headroom(vport->dev);
2379 	ovs_dp_detach_port(vport);
2380 
2381 	if (update_headroom) {
2382 		new_headroom = ovs_get_max_headroom(dp);
2383 
2384 		if (new_headroom < dp->max_headroom)
2385 			ovs_update_headroom(dp, new_headroom);
2386 	}
2387 	ovs_unlock();
2388 
2389 	ovs_notify(&dp_vport_genl_family, reply, info);
2390 	return 0;
2391 
2392 exit_unlock_free:
2393 	ovs_unlock();
2394 	kfree_skb(reply);
2395 	return err;
2396 }
2397 
ovs_vport_cmd_get(struct sk_buff * skb,struct genl_info * info)2398 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2399 {
2400 	struct nlattr **a = info->attrs;
2401 	struct ovs_header *ovs_header = info->userhdr;
2402 	struct sk_buff *reply;
2403 	struct vport *vport;
2404 	int err;
2405 
2406 	reply = ovs_vport_cmd_alloc_info();
2407 	if (!reply)
2408 		return -ENOMEM;
2409 
2410 	rcu_read_lock();
2411 	vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2412 	err = PTR_ERR(vport);
2413 	if (IS_ERR(vport))
2414 		goto exit_unlock_free;
2415 	err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2416 				      info->snd_portid, info->snd_seq, 0,
2417 				      OVS_VPORT_CMD_GET, GFP_ATOMIC);
2418 	BUG_ON(err < 0);
2419 	rcu_read_unlock();
2420 
2421 	return genlmsg_reply(reply, info);
2422 
2423 exit_unlock_free:
2424 	rcu_read_unlock();
2425 	kfree_skb(reply);
2426 	return err;
2427 }
2428 
ovs_vport_cmd_dump(struct sk_buff * skb,struct netlink_callback * cb)2429 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2430 {
2431 	struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2432 	struct datapath *dp;
2433 	int bucket = cb->args[0], skip = cb->args[1];
2434 	int i, j = 0;
2435 
2436 	rcu_read_lock();
2437 	dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2438 	if (!dp) {
2439 		rcu_read_unlock();
2440 		return -ENODEV;
2441 	}
2442 	for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2443 		struct vport *vport;
2444 
2445 		j = 0;
2446 		hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2447 			if (j >= skip &&
2448 			    ovs_vport_cmd_fill_info(vport, skb,
2449 						    sock_net(skb->sk),
2450 						    NETLINK_CB(cb->skb).portid,
2451 						    cb->nlh->nlmsg_seq,
2452 						    NLM_F_MULTI,
2453 						    OVS_VPORT_CMD_GET,
2454 						    GFP_ATOMIC) < 0)
2455 				goto out;
2456 
2457 			j++;
2458 		}
2459 		skip = 0;
2460 	}
2461 out:
2462 	rcu_read_unlock();
2463 
2464 	cb->args[0] = i;
2465 	cb->args[1] = j;
2466 
2467 	return skb->len;
2468 }
2469 
ovs_dp_masks_rebalance(struct work_struct * work)2470 static void ovs_dp_masks_rebalance(struct work_struct *work)
2471 {
2472 	struct ovs_net *ovs_net = container_of(work, struct ovs_net,
2473 					       masks_rebalance.work);
2474 	struct datapath *dp;
2475 
2476 	ovs_lock();
2477 
2478 	list_for_each_entry(dp, &ovs_net->dps, list_node)
2479 		ovs_flow_masks_rebalance(&dp->table);
2480 
2481 	ovs_unlock();
2482 
2483 	schedule_delayed_work(&ovs_net->masks_rebalance,
2484 			      msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL));
2485 }
2486 
2487 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2488 	[OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2489 	[OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2490 	[OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2491 	[OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2492 	[OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC },
2493 	[OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2494 	[OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2495 	[OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2496 };
2497 
2498 static const struct genl_small_ops dp_vport_genl_ops[] = {
2499 	{ .cmd = OVS_VPORT_CMD_NEW,
2500 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2501 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2502 	  .doit = ovs_vport_cmd_new
2503 	},
2504 	{ .cmd = OVS_VPORT_CMD_DEL,
2505 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2506 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2507 	  .doit = ovs_vport_cmd_del
2508 	},
2509 	{ .cmd = OVS_VPORT_CMD_GET,
2510 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2511 	  .flags = 0,		    /* OK for unprivileged users. */
2512 	  .doit = ovs_vport_cmd_get,
2513 	  .dumpit = ovs_vport_cmd_dump
2514 	},
2515 	{ .cmd = OVS_VPORT_CMD_SET,
2516 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2517 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2518 	  .doit = ovs_vport_cmd_set,
2519 	},
2520 };
2521 
2522 struct genl_family dp_vport_genl_family __ro_after_init = {
2523 	.hdrsize = sizeof(struct ovs_header),
2524 	.name = OVS_VPORT_FAMILY,
2525 	.version = OVS_VPORT_VERSION,
2526 	.maxattr = OVS_VPORT_ATTR_MAX,
2527 	.policy = vport_policy,
2528 	.netnsok = true,
2529 	.parallel_ops = true,
2530 	.small_ops = dp_vport_genl_ops,
2531 	.n_small_ops = ARRAY_SIZE(dp_vport_genl_ops),
2532 	.mcgrps = &ovs_dp_vport_multicast_group,
2533 	.n_mcgrps = 1,
2534 	.module = THIS_MODULE,
2535 };
2536 
2537 static struct genl_family * const dp_genl_families[] = {
2538 	&dp_datapath_genl_family,
2539 	&dp_vport_genl_family,
2540 	&dp_flow_genl_family,
2541 	&dp_packet_genl_family,
2542 	&dp_meter_genl_family,
2543 #if	IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2544 	&dp_ct_limit_genl_family,
2545 #endif
2546 };
2547 
dp_unregister_genl(int n_families)2548 static void dp_unregister_genl(int n_families)
2549 {
2550 	int i;
2551 
2552 	for (i = 0; i < n_families; i++)
2553 		genl_unregister_family(dp_genl_families[i]);
2554 }
2555 
dp_register_genl(void)2556 static int __init dp_register_genl(void)
2557 {
2558 	int err;
2559 	int i;
2560 
2561 	for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2562 
2563 		err = genl_register_family(dp_genl_families[i]);
2564 		if (err)
2565 			goto error;
2566 	}
2567 
2568 	return 0;
2569 
2570 error:
2571 	dp_unregister_genl(i);
2572 	return err;
2573 }
2574 
ovs_init_net(struct net * net)2575 static int __net_init ovs_init_net(struct net *net)
2576 {
2577 	struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2578 	int err;
2579 
2580 	INIT_LIST_HEAD(&ovs_net->dps);
2581 	INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2582 	INIT_DELAYED_WORK(&ovs_net->masks_rebalance, ovs_dp_masks_rebalance);
2583 
2584 	err = ovs_ct_init(net);
2585 	if (err)
2586 		return err;
2587 
2588 	schedule_delayed_work(&ovs_net->masks_rebalance,
2589 			      msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL));
2590 	return 0;
2591 }
2592 
list_vports_from_net(struct net * net,struct net * dnet,struct list_head * head)2593 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2594 					    struct list_head *head)
2595 {
2596 	struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2597 	struct datapath *dp;
2598 
2599 	list_for_each_entry(dp, &ovs_net->dps, list_node) {
2600 		int i;
2601 
2602 		for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2603 			struct vport *vport;
2604 
2605 			hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2606 				if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2607 					continue;
2608 
2609 				if (dev_net(vport->dev) == dnet)
2610 					list_add(&vport->detach_list, head);
2611 			}
2612 		}
2613 	}
2614 }
2615 
ovs_exit_net(struct net * dnet)2616 static void __net_exit ovs_exit_net(struct net *dnet)
2617 {
2618 	struct datapath *dp, *dp_next;
2619 	struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2620 	struct vport *vport, *vport_next;
2621 	struct net *net;
2622 	LIST_HEAD(head);
2623 
2624 	ovs_lock();
2625 
2626 	ovs_ct_exit(dnet);
2627 
2628 	list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2629 		__dp_destroy(dp);
2630 
2631 	down_read(&net_rwsem);
2632 	for_each_net(net)
2633 		list_vports_from_net(net, dnet, &head);
2634 	up_read(&net_rwsem);
2635 
2636 	/* Detach all vports from given namespace. */
2637 	list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2638 		list_del(&vport->detach_list);
2639 		ovs_dp_detach_port(vport);
2640 	}
2641 
2642 	ovs_unlock();
2643 
2644 	cancel_delayed_work_sync(&ovs_net->masks_rebalance);
2645 	cancel_work_sync(&ovs_net->dp_notify_work);
2646 }
2647 
2648 static struct pernet_operations ovs_net_ops = {
2649 	.init = ovs_init_net,
2650 	.exit = ovs_exit_net,
2651 	.id   = &ovs_net_id,
2652 	.size = sizeof(struct ovs_net),
2653 };
2654 
dp_init(void)2655 static int __init dp_init(void)
2656 {
2657 	int err;
2658 
2659 	BUILD_BUG_ON(sizeof(struct ovs_skb_cb) >
2660 		     sizeof_field(struct sk_buff, cb));
2661 
2662 	pr_info("Open vSwitch switching datapath\n");
2663 
2664 	err = action_fifos_init();
2665 	if (err)
2666 		goto error;
2667 
2668 	err = ovs_internal_dev_rtnl_link_register();
2669 	if (err)
2670 		goto error_action_fifos_exit;
2671 
2672 	err = ovs_flow_init();
2673 	if (err)
2674 		goto error_unreg_rtnl_link;
2675 
2676 	err = ovs_vport_init();
2677 	if (err)
2678 		goto error_flow_exit;
2679 
2680 	err = register_pernet_device(&ovs_net_ops);
2681 	if (err)
2682 		goto error_vport_exit;
2683 
2684 	err = register_netdevice_notifier(&ovs_dp_device_notifier);
2685 	if (err)
2686 		goto error_netns_exit;
2687 
2688 	err = ovs_netdev_init();
2689 	if (err)
2690 		goto error_unreg_notifier;
2691 
2692 	err = dp_register_genl();
2693 	if (err < 0)
2694 		goto error_unreg_netdev;
2695 
2696 	return 0;
2697 
2698 error_unreg_netdev:
2699 	ovs_netdev_exit();
2700 error_unreg_notifier:
2701 	unregister_netdevice_notifier(&ovs_dp_device_notifier);
2702 error_netns_exit:
2703 	unregister_pernet_device(&ovs_net_ops);
2704 error_vport_exit:
2705 	ovs_vport_exit();
2706 error_flow_exit:
2707 	ovs_flow_exit();
2708 error_unreg_rtnl_link:
2709 	ovs_internal_dev_rtnl_link_unregister();
2710 error_action_fifos_exit:
2711 	action_fifos_exit();
2712 error:
2713 	return err;
2714 }
2715 
dp_cleanup(void)2716 static void dp_cleanup(void)
2717 {
2718 	dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2719 	ovs_netdev_exit();
2720 	unregister_netdevice_notifier(&ovs_dp_device_notifier);
2721 	unregister_pernet_device(&ovs_net_ops);
2722 	rcu_barrier();
2723 	ovs_vport_exit();
2724 	ovs_flow_exit();
2725 	ovs_internal_dev_rtnl_link_unregister();
2726 	action_fifos_exit();
2727 }
2728 
2729 module_init(dp_init);
2730 module_exit(dp_cleanup);
2731 
2732 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2733 MODULE_LICENSE("GPL");
2734 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2735 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2736 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2737 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
2738 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
2739 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);
2740