]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/datapath.c
datapath: Fix typos in Netlink debugging messages.
[mirror_ovs.git] / datapath / datapath.c
1 /*
2 * Copyright (c) 2007-2013 Nicira, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
25 #include <linux/in.h>
26 #include <linux/ip.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <linux/genetlink.h>
52 #include <net/genetlink.h>
53 #include <net/genetlink.h>
54 #include <net/net_namespace.h>
55 #include <net/netns/generic.h>
56
57 #include "checksum.h"
58 #include "datapath.h"
59 #include "flow.h"
60 #include "vlan.h"
61 #include "vport-internal_dev.h"
62 #include "vport-netdev.h"
63
64 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
65 LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
66 #error Kernels before 2.6.18 or after 3.9 are not supported by this version of Open vSwitch.
67 #endif
68
69 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
70 static void rehash_flow_table(struct work_struct *work);
71 static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
72
73 int ovs_net_id __read_mostly;
74
75 static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
76 struct genl_multicast_group *grp)
77 {
78 genl_notify(skb, genl_info_net(info), info->snd_portid,
79 grp->id, info->nlhdr, GFP_KERNEL);
80 }
81
82 /**
83 * DOC: Locking:
84 *
85 * All writes e.g. Writes to device state (add/remove datapath, port, set
86 * operations on vports, etc.), Writes to other state (flow table
87 * modifications, set miscellaneous datapath parameters, etc.) are protected
88 * by ovs_lock.
89 *
90 * Reads are protected by RCU.
91 *
92 * There are a few special cases (mostly stats) that have their own
93 * synchronization but they nest under all of above and don't interact with
94 * each other.
95 *
96 * The RTNL lock nests inside ovs_mutex.
97 */
98
99 static DEFINE_MUTEX(ovs_mutex);
100
101 void ovs_lock(void)
102 {
103 mutex_lock(&ovs_mutex);
104 }
105
106 void ovs_unlock(void)
107 {
108 mutex_unlock(&ovs_mutex);
109 }
110
111 #ifdef CONFIG_LOCKDEP
112 int lockdep_ovsl_is_held(void)
113 {
114 if (debug_locks)
115 return lockdep_is_held(&ovs_mutex);
116 else
117 return 1;
118 }
119 #endif
120
121 static struct vport *new_vport(const struct vport_parms *);
122 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
123 const struct dp_upcall_info *);
124 static int queue_userspace_packet(struct net *, int dp_ifindex,
125 struct sk_buff *,
126 const struct dp_upcall_info *);
127
128 /* Must be called with rcu_read_lock or ovs_mutex. */
129 static struct datapath *get_dp(struct net *net, int dp_ifindex)
130 {
131 struct datapath *dp = NULL;
132 struct net_device *dev;
133
134 rcu_read_lock();
135 dev = dev_get_by_index_rcu(net, dp_ifindex);
136 if (dev) {
137 struct vport *vport = ovs_internal_dev_get_vport(dev);
138 if (vport)
139 dp = vport->dp;
140 }
141 rcu_read_unlock();
142
143 return dp;
144 }
145
146 /* Must be called with rcu_read_lock or ovs_mutex. */
147 const char *ovs_dp_name(const struct datapath *dp)
148 {
149 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
150 return vport->ops->get_name(vport);
151 }
152
153 static int get_dpifindex(struct datapath *dp)
154 {
155 struct vport *local;
156 int ifindex;
157
158 rcu_read_lock();
159
160 local = ovs_vport_rcu(dp, OVSP_LOCAL);
161 if (local)
162 ifindex = netdev_vport_priv(local)->dev->ifindex;
163 else
164 ifindex = 0;
165
166 rcu_read_unlock();
167
168 return ifindex;
169 }
170
171 static void destroy_dp_rcu(struct rcu_head *rcu)
172 {
173 struct datapath *dp = container_of(rcu, struct datapath, rcu);
174
175 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false);
176 free_percpu(dp->stats_percpu);
177 release_net(ovs_dp_get_net(dp));
178 kfree(dp->ports);
179 kfree(dp);
180 }
181
182 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
183 u16 port_no)
184 {
185 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
186 }
187
188 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
189 {
190 struct vport *vport;
191 struct hlist_head *head;
192
193 head = vport_hash_bucket(dp, port_no);
194 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
195 if (vport->port_no == port_no)
196 return vport;
197 }
198 return NULL;
199 }
200
201 /* Called with ovs_mutex. */
202 static struct vport *new_vport(const struct vport_parms *parms)
203 {
204 struct vport *vport;
205
206 vport = ovs_vport_add(parms);
207 if (!IS_ERR(vport)) {
208 struct datapath *dp = parms->dp;
209 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
210
211 hlist_add_head_rcu(&vport->dp_hash_node, head);
212 }
213 return vport;
214 }
215
216 void ovs_dp_detach_port(struct vport *p)
217 {
218 ASSERT_OVSL();
219
220 /* First drop references to device. */
221 hlist_del_rcu(&p->dp_hash_node);
222
223 /* Then destroy it. */
224 ovs_vport_del(p);
225 }
226
227 /* Must be called with rcu_read_lock. */
228 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
229 {
230 struct datapath *dp = p->dp;
231 struct sw_flow *flow;
232 struct dp_stats_percpu *stats;
233 struct sw_flow_key key;
234 u64 *stats_counter;
235 int error;
236
237 stats = this_cpu_ptr(dp->stats_percpu);
238
239 /* Extract flow from 'skb' into 'key'. */
240 error = ovs_flow_extract(skb, p->port_no, &key);
241 if (unlikely(error)) {
242 kfree_skb(skb);
243 return;
244 }
245
246 /* Look up flow. */
247 flow = ovs_flow_lookup(rcu_dereference(dp->table), &key);
248 if (unlikely(!flow)) {
249 struct dp_upcall_info upcall;
250
251 upcall.cmd = OVS_PACKET_CMD_MISS;
252 upcall.key = &key;
253 upcall.userdata = NULL;
254 upcall.portid = p->upcall_portid;
255 ovs_dp_upcall(dp, skb, &upcall);
256 consume_skb(skb);
257 stats_counter = &stats->n_missed;
258 goto out;
259 }
260
261 OVS_CB(skb)->flow = flow;
262 OVS_CB(skb)->pkt_key = &key;
263
264 stats_counter = &stats->n_hit;
265 ovs_flow_used(OVS_CB(skb)->flow, skb);
266 ovs_execute_actions(dp, skb);
267
268 out:
269 /* Update datapath statistics. */
270 u64_stats_update_begin(&stats->sync);
271 (*stats_counter)++;
272 u64_stats_update_end(&stats->sync);
273 }
274
275 static struct genl_family dp_packet_genl_family = {
276 .id = GENL_ID_GENERATE,
277 .hdrsize = sizeof(struct ovs_header),
278 .name = OVS_PACKET_FAMILY,
279 .version = OVS_PACKET_VERSION,
280 .maxattr = OVS_PACKET_ATTR_MAX,
281 SET_NETNSOK
282 SET_PARALLEL_OPS
283 };
284
285 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
286 const struct dp_upcall_info *upcall_info)
287 {
288 struct dp_stats_percpu *stats;
289 int dp_ifindex;
290 int err;
291
292 if (upcall_info->portid == 0) {
293 err = -ENOTCONN;
294 goto err;
295 }
296
297 dp_ifindex = get_dpifindex(dp);
298 if (!dp_ifindex) {
299 err = -ENODEV;
300 goto err;
301 }
302
303 forward_ip_summed(skb, true);
304
305 if (!skb_is_gso(skb))
306 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
307 else
308 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
309 if (err)
310 goto err;
311
312 return 0;
313
314 err:
315 stats = this_cpu_ptr(dp->stats_percpu);
316
317 u64_stats_update_begin(&stats->sync);
318 stats->n_lost++;
319 u64_stats_update_end(&stats->sync);
320
321 return err;
322 }
323
324 static int queue_gso_packets(struct net *net, int dp_ifindex,
325 struct sk_buff *skb,
326 const struct dp_upcall_info *upcall_info)
327 {
328 unsigned short gso_type = skb_shinfo(skb)->gso_type;
329 struct dp_upcall_info later_info;
330 struct sw_flow_key later_key;
331 struct sk_buff *segs, *nskb;
332 int err;
333
334 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
335 if (IS_ERR(segs))
336 return PTR_ERR(segs);
337
338 /* Queue all of the segments. */
339 skb = segs;
340 do {
341 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
342 if (err)
343 break;
344
345 if (skb == segs && gso_type & SKB_GSO_UDP) {
346 /* The initial flow key extracted by ovs_flow_extract()
347 * in this case is for a first fragment, so we need to
348 * properly mark later fragments.
349 */
350 later_key = *upcall_info->key;
351 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
352
353 later_info = *upcall_info;
354 later_info.key = &later_key;
355 upcall_info = &later_info;
356 }
357 } while ((skb = skb->next));
358
359 /* Free all of the segments. */
360 skb = segs;
361 do {
362 nskb = skb->next;
363 if (err)
364 kfree_skb(skb);
365 else
366 consume_skb(skb);
367 } while ((skb = nskb));
368 return err;
369 }
370
371 static size_t key_attr_size(void)
372 {
373 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
374 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
375 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
376 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
377 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
378 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
379 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
380 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
381 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
382 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
383 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
384 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
385 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
386 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
387 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
388 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
389 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
390 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
391 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
392 }
393
394 static size_t upcall_msg_size(const struct sk_buff *skb,
395 const struct nlattr *userdata)
396 {
397 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
398 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
399 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
400
401 /* OVS_PACKET_ATTR_USERDATA */
402 if (userdata)
403 size += NLA_ALIGN(userdata->nla_len);
404
405 return size;
406 }
407
408 static int queue_userspace_packet(struct net *net, int dp_ifindex,
409 struct sk_buff *skb,
410 const struct dp_upcall_info *upcall_info)
411 {
412 struct ovs_header *upcall;
413 struct sk_buff *nskb = NULL;
414 struct sk_buff *user_skb; /* to be queued to userspace */
415 struct nlattr *nla;
416 int err;
417
418 if (vlan_tx_tag_present(skb)) {
419 nskb = skb_clone(skb, GFP_ATOMIC);
420 if (!nskb)
421 return -ENOMEM;
422
423 err = vlan_deaccel_tag(nskb);
424 if (err)
425 return err;
426
427 skb = nskb;
428 }
429
430 if (nla_attr_size(skb->len) > USHRT_MAX) {
431 err = -EFBIG;
432 goto out;
433 }
434
435 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
436 if (!user_skb) {
437 err = -ENOMEM;
438 goto out;
439 }
440
441 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
442 0, upcall_info->cmd);
443 upcall->dp_ifindex = dp_ifindex;
444
445 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
446 ovs_flow_to_nlattrs(upcall_info->key, upcall_info->key, user_skb);
447 nla_nest_end(user_skb, nla);
448
449 if (upcall_info->userdata)
450 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
451 nla_len(upcall_info->userdata),
452 nla_data(upcall_info->userdata));
453
454 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
455
456 skb_copy_and_csum_dev(skb, nla_data(nla));
457
458 genlmsg_end(user_skb, upcall);
459 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
460
461 out:
462 kfree_skb(nskb);
463 return err;
464 }
465
466 /* Called with ovs_mutex. */
467 static int flush_flows(struct datapath *dp)
468 {
469 struct flow_table *old_table;
470 struct flow_table *new_table;
471
472 old_table = ovsl_dereference(dp->table);
473 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
474 if (!new_table)
475 return -ENOMEM;
476
477 rcu_assign_pointer(dp->table, new_table);
478
479 ovs_flow_tbl_destroy(old_table, true);
480 return 0;
481 }
482
483 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
484 {
485
486 struct sw_flow_actions *acts;
487 int new_acts_size;
488 int req_size = NLA_ALIGN(attr_len);
489 int next_offset = offsetof(struct sw_flow_actions, actions) +
490 (*sfa)->actions_len;
491
492 if (req_size <= (ksize(*sfa) - next_offset))
493 goto out;
494
495 new_acts_size = ksize(*sfa) * 2;
496
497 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
498 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
499 return ERR_PTR(-EMSGSIZE);
500 new_acts_size = MAX_ACTIONS_BUFSIZE;
501 }
502
503 acts = ovs_flow_actions_alloc(new_acts_size);
504 if (IS_ERR(acts))
505 return (void *)acts;
506
507 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
508 acts->actions_len = (*sfa)->actions_len;
509 kfree(*sfa);
510 *sfa = acts;
511
512 out:
513 (*sfa)->actions_len += req_size;
514 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
515 }
516
517 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
518 {
519 struct nlattr *a;
520
521 a = reserve_sfa_size(sfa, nla_attr_size(len));
522 if (IS_ERR(a))
523 return PTR_ERR(a);
524
525 a->nla_type = attrtype;
526 a->nla_len = nla_attr_size(len);
527
528 if (data)
529 memcpy(nla_data(a), data, len);
530 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
531
532 return 0;
533 }
534
535 static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
536 {
537 int used = (*sfa)->actions_len;
538 int err;
539
540 err = add_action(sfa, attrtype, NULL, 0);
541 if (err)
542 return err;
543
544 return used;
545 }
546
547 static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
548 {
549 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
550
551 a->nla_len = sfa->actions_len - st_offset;
552 }
553
554 static int validate_and_copy_actions(const struct nlattr *attr,
555 const struct sw_flow_key *key, int depth,
556 struct sw_flow_actions **sfa);
557
558 static int validate_and_copy_sample(const struct nlattr *attr,
559 const struct sw_flow_key *key, int depth,
560 struct sw_flow_actions **sfa)
561 {
562 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
563 const struct nlattr *probability, *actions;
564 const struct nlattr *a;
565 int rem, start, err, st_acts;
566
567 memset(attrs, 0, sizeof(attrs));
568 nla_for_each_nested(a, attr, rem) {
569 int type = nla_type(a);
570 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
571 return -EINVAL;
572 attrs[type] = a;
573 }
574 if (rem)
575 return -EINVAL;
576
577 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
578 if (!probability || nla_len(probability) != sizeof(u32))
579 return -EINVAL;
580
581 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
582 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
583 return -EINVAL;
584
585 /* validation done, copy sample action. */
586 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
587 if (start < 0)
588 return start;
589 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
590 if (err)
591 return err;
592 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
593 if (st_acts < 0)
594 return st_acts;
595
596 err = validate_and_copy_actions(actions, key, depth + 1, sfa);
597 if (err)
598 return err;
599
600 add_nested_action_end(*sfa, st_acts);
601 add_nested_action_end(*sfa, start);
602
603 return 0;
604 }
605
606 static int validate_tp_port(const struct sw_flow_key *flow_key)
607 {
608 if (flow_key->eth.type == htons(ETH_P_IP)) {
609 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
610 return 0;
611 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
612 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
613 return 0;
614 }
615
616 return -EINVAL;
617 }
618
619 static int validate_and_copy_set_tun(const struct nlattr *attr,
620 struct sw_flow_actions **sfa)
621 {
622 struct sw_flow_match match;
623 struct sw_flow_key key;
624 int err, start;
625
626 ovs_match_init(&match, &key, NULL);
627 err = ovs_ipv4_tun_from_nlattr(nla_data(attr), &match, false);
628 if (err)
629 return err;
630
631 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
632 if (start < 0)
633 return start;
634
635 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
636 sizeof(match.key->tun_key));
637 add_nested_action_end(*sfa, start);
638
639 return err;
640 }
641
642 static int validate_set(const struct nlattr *a,
643 const struct sw_flow_key *flow_key,
644 struct sw_flow_actions **sfa,
645 bool *set_tun)
646 {
647 const struct nlattr *ovs_key = nla_data(a);
648 int key_type = nla_type(ovs_key);
649
650 /* There can be only one key in a action */
651 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
652 return -EINVAL;
653
654 if (key_type > OVS_KEY_ATTR_MAX ||
655 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
656 ovs_key_lens[key_type] != -1))
657 return -EINVAL;
658
659 switch (key_type) {
660 const struct ovs_key_ipv4 *ipv4_key;
661 const struct ovs_key_ipv6 *ipv6_key;
662 int err;
663
664 case OVS_KEY_ATTR_PRIORITY:
665 case OVS_KEY_ATTR_ETHERNET:
666 break;
667
668 case OVS_KEY_ATTR_SKB_MARK:
669 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
670 if (nla_get_u32(ovs_key) != 0)
671 return -EINVAL;
672 #endif
673 break;
674
675 case OVS_KEY_ATTR_TUNNEL:
676 *set_tun = true;
677 err = validate_and_copy_set_tun(a, sfa);
678 if (err)
679 return err;
680 break;
681
682 case OVS_KEY_ATTR_IPV4:
683 if (flow_key->eth.type != htons(ETH_P_IP))
684 return -EINVAL;
685
686 if (!flow_key->ip.proto)
687 return -EINVAL;
688
689 ipv4_key = nla_data(ovs_key);
690 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
691 return -EINVAL;
692
693 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
694 return -EINVAL;
695
696 break;
697
698 case OVS_KEY_ATTR_IPV6:
699 if (flow_key->eth.type != htons(ETH_P_IPV6))
700 return -EINVAL;
701
702 if (!flow_key->ip.proto)
703 return -EINVAL;
704
705 ipv6_key = nla_data(ovs_key);
706 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
707 return -EINVAL;
708
709 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
710 return -EINVAL;
711
712 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
713 return -EINVAL;
714
715 break;
716
717 case OVS_KEY_ATTR_TCP:
718 if (flow_key->ip.proto != IPPROTO_TCP)
719 return -EINVAL;
720
721 return validate_tp_port(flow_key);
722
723 case OVS_KEY_ATTR_UDP:
724 if (flow_key->ip.proto != IPPROTO_UDP)
725 return -EINVAL;
726
727 return validate_tp_port(flow_key);
728
729 default:
730 return -EINVAL;
731 }
732
733 return 0;
734 }
735
736 static int validate_userspace(const struct nlattr *attr)
737 {
738 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
739 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
740 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
741 };
742 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
743 int error;
744
745 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
746 attr, userspace_policy);
747 if (error)
748 return error;
749
750 if (!a[OVS_USERSPACE_ATTR_PID] ||
751 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
752 return -EINVAL;
753
754 return 0;
755 }
756
757 static int copy_action(const struct nlattr *from,
758 struct sw_flow_actions **sfa)
759 {
760 int totlen = NLA_ALIGN(from->nla_len);
761 struct nlattr *to;
762
763 to = reserve_sfa_size(sfa, from->nla_len);
764 if (IS_ERR(to))
765 return PTR_ERR(to);
766
767 memcpy(to, from, totlen);
768 return 0;
769 }
770
771 static int validate_and_copy_actions(const struct nlattr *attr,
772 const struct sw_flow_key *key,
773 int depth,
774 struct sw_flow_actions **sfa)
775 {
776 const struct nlattr *a;
777 int rem, err;
778
779 if (depth >= SAMPLE_ACTION_DEPTH)
780 return -EOVERFLOW;
781
782 nla_for_each_nested(a, attr, rem) {
783 /* Expected argument lengths, (u32)-1 for variable length. */
784 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
785 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
786 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
787 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
788 [OVS_ACTION_ATTR_POP_VLAN] = 0,
789 [OVS_ACTION_ATTR_SET] = (u32)-1,
790 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
791 };
792 const struct ovs_action_push_vlan *vlan;
793 int type = nla_type(a);
794 bool skip_copy;
795
796 if (type > OVS_ACTION_ATTR_MAX ||
797 (action_lens[type] != nla_len(a) &&
798 action_lens[type] != (u32)-1))
799 return -EINVAL;
800
801 skip_copy = false;
802 switch (type) {
803 case OVS_ACTION_ATTR_UNSPEC:
804 return -EINVAL;
805
806 case OVS_ACTION_ATTR_USERSPACE:
807 err = validate_userspace(a);
808 if (err)
809 return err;
810 break;
811
812 case OVS_ACTION_ATTR_OUTPUT:
813 if (nla_get_u32(a) >= DP_MAX_PORTS)
814 return -EINVAL;
815 break;
816
817
818 case OVS_ACTION_ATTR_POP_VLAN:
819 break;
820
821 case OVS_ACTION_ATTR_PUSH_VLAN:
822 vlan = nla_data(a);
823 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
824 return -EINVAL;
825 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
826 return -EINVAL;
827 break;
828
829 case OVS_ACTION_ATTR_SET:
830 err = validate_set(a, key, sfa, &skip_copy);
831 if (err)
832 return err;
833 break;
834
835 case OVS_ACTION_ATTR_SAMPLE:
836 err = validate_and_copy_sample(a, key, depth, sfa);
837 if (err)
838 return err;
839 skip_copy = true;
840 break;
841
842 default:
843 return -EINVAL;
844 }
845 if (!skip_copy) {
846 err = copy_action(a, sfa);
847 if (err)
848 return err;
849 }
850 }
851
852 if (rem > 0)
853 return -EINVAL;
854
855 return 0;
856 }
857
858 static void clear_stats(struct sw_flow *flow)
859 {
860 flow->used = 0;
861 flow->tcp_flags = 0;
862 flow->packet_count = 0;
863 flow->byte_count = 0;
864 }
865
866 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
867 {
868 struct ovs_header *ovs_header = info->userhdr;
869 struct nlattr **a = info->attrs;
870 struct sw_flow_actions *acts;
871 struct sk_buff *packet;
872 struct sw_flow *flow;
873 struct datapath *dp;
874 struct ethhdr *eth;
875 int len;
876 int err;
877
878 err = -EINVAL;
879 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
880 !a[OVS_PACKET_ATTR_ACTIONS])
881 goto err;
882
883 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
884 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
885 err = -ENOMEM;
886 if (!packet)
887 goto err;
888 skb_reserve(packet, NET_IP_ALIGN);
889
890 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
891
892 skb_reset_mac_header(packet);
893 eth = eth_hdr(packet);
894
895 /* Normally, setting the skb 'protocol' field would be handled by a
896 * call to eth_type_trans(), but it assumes there's a sending
897 * device, which we may not have. */
898 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
899 packet->protocol = eth->h_proto;
900 else
901 packet->protocol = htons(ETH_P_802_2);
902
903 /* Build an sw_flow for sending this packet. */
904 flow = ovs_flow_alloc();
905 err = PTR_ERR(flow);
906 if (IS_ERR(flow))
907 goto err_kfree_skb;
908
909 err = ovs_flow_extract(packet, -1, &flow->key);
910 if (err)
911 goto err_flow_free;
912
913 err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]);
914 if (err)
915 goto err_flow_free;
916 acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
917 err = PTR_ERR(acts);
918 if (IS_ERR(acts))
919 goto err_flow_free;
920
921 err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
922 rcu_assign_pointer(flow->sf_acts, acts);
923 if (err)
924 goto err_flow_free;
925
926 OVS_CB(packet)->flow = flow;
927 OVS_CB(packet)->pkt_key = &flow->key;
928 packet->priority = flow->key.phy.priority;
929 skb_set_mark(packet, flow->key.phy.skb_mark);
930
931 rcu_read_lock();
932 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
933 err = -ENODEV;
934 if (!dp)
935 goto err_unlock;
936
937 local_bh_disable();
938 err = ovs_execute_actions(dp, packet);
939 local_bh_enable();
940 rcu_read_unlock();
941
942 ovs_flow_free(flow, false);
943 return err;
944
945 err_unlock:
946 rcu_read_unlock();
947 err_flow_free:
948 ovs_flow_free(flow, false);
949 err_kfree_skb:
950 kfree_skb(packet);
951 err:
952 return err;
953 }
954
955 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
956 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
957 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
958 #else
959 [OVS_PACKET_ATTR_PACKET] = { .minlen = ETH_HLEN },
960 #endif
961 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
962 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
963 };
964
965 static struct genl_ops dp_packet_genl_ops[] = {
966 { .cmd = OVS_PACKET_CMD_EXECUTE,
967 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
968 .policy = packet_policy,
969 .doit = ovs_packet_cmd_execute
970 }
971 };
972
973 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
974 {
975 struct flow_table *table;
976 int i;
977
978 table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
979 stats->n_flows = ovs_flow_tbl_count(table);
980
981 stats->n_hit = stats->n_missed = stats->n_lost = 0;
982 for_each_possible_cpu(i) {
983 const struct dp_stats_percpu *percpu_stats;
984 struct dp_stats_percpu local_stats;
985 unsigned int start;
986
987 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
988
989 do {
990 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
991 local_stats = *percpu_stats;
992 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
993
994 stats->n_hit += local_stats.n_hit;
995 stats->n_missed += local_stats.n_missed;
996 stats->n_lost += local_stats.n_lost;
997 }
998 }
999
1000 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1001 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1002 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1003 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1004 };
1005
1006 static struct genl_family dp_flow_genl_family = {
1007 .id = GENL_ID_GENERATE,
1008 .hdrsize = sizeof(struct ovs_header),
1009 .name = OVS_FLOW_FAMILY,
1010 .version = OVS_FLOW_VERSION,
1011 .maxattr = OVS_FLOW_ATTR_MAX,
1012 SET_NETNSOK
1013 SET_PARALLEL_OPS
1014 };
1015
1016 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
1017 .name = OVS_FLOW_MCGROUP
1018 };
1019
1020 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1021 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1022 {
1023 const struct nlattr *a;
1024 struct nlattr *start;
1025 int err = 0, rem;
1026
1027 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1028 if (!start)
1029 return -EMSGSIZE;
1030
1031 nla_for_each_nested(a, attr, rem) {
1032 int type = nla_type(a);
1033 struct nlattr *st_sample;
1034
1035 switch (type) {
1036 case OVS_SAMPLE_ATTR_PROBABILITY:
1037 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1038 return -EMSGSIZE;
1039 break;
1040 case OVS_SAMPLE_ATTR_ACTIONS:
1041 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1042 if (!st_sample)
1043 return -EMSGSIZE;
1044 err = actions_to_attr(nla_data(a), nla_len(a), skb);
1045 if (err)
1046 return err;
1047 nla_nest_end(skb, st_sample);
1048 break;
1049 }
1050 }
1051
1052 nla_nest_end(skb, start);
1053 return err;
1054 }
1055
1056 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1057 {
1058 const struct nlattr *ovs_key = nla_data(a);
1059 int key_type = nla_type(ovs_key);
1060 struct nlattr *start;
1061 int err;
1062
1063 switch (key_type) {
1064 case OVS_KEY_ATTR_IPV4_TUNNEL:
1065 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1066 if (!start)
1067 return -EMSGSIZE;
1068
1069 err = ovs_ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1070 nla_data(ovs_key));
1071 if (err)
1072 return err;
1073 nla_nest_end(skb, start);
1074 break;
1075 default:
1076 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1077 return -EMSGSIZE;
1078 break;
1079 }
1080
1081 return 0;
1082 }
1083
1084 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1085 {
1086 const struct nlattr *a;
1087 int rem, err;
1088
1089 nla_for_each_attr(a, attr, len, rem) {
1090 int type = nla_type(a);
1091
1092 switch (type) {
1093 case OVS_ACTION_ATTR_SET:
1094 err = set_action_to_attr(a, skb);
1095 if (err)
1096 return err;
1097 break;
1098
1099 case OVS_ACTION_ATTR_SAMPLE:
1100 err = sample_action_to_attr(a, skb);
1101 if (err)
1102 return err;
1103 break;
1104 default:
1105 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1106 return -EMSGSIZE;
1107 break;
1108 }
1109 }
1110
1111 return 0;
1112 }
1113
1114 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1115 {
1116 return NLMSG_ALIGN(sizeof(struct ovs_header))
1117 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
1118 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
1119 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
1120 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1121 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1122 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
1123 }
1124
1125 /* Called with ovs_mutex. */
1126 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1127 struct sk_buff *skb, u32 portid,
1128 u32 seq, u32 flags, u8 cmd)
1129 {
1130 const int skb_orig_len = skb->len;
1131 struct nlattr *start;
1132 struct ovs_flow_stats stats;
1133 struct ovs_header *ovs_header;
1134 struct nlattr *nla;
1135 unsigned long used;
1136 u8 tcp_flags;
1137 int err;
1138
1139 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
1140 if (!ovs_header)
1141 return -EMSGSIZE;
1142
1143 ovs_header->dp_ifindex = get_dpifindex(dp);
1144
1145 /* Fill flow key. */
1146 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1147 if (!nla)
1148 goto nla_put_failure;
1149
1150 err = ovs_flow_to_nlattrs(&flow->unmasked_key,
1151 &flow->unmasked_key, skb);
1152 if (err)
1153 goto error;
1154 nla_nest_end(skb, nla);
1155
1156 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
1157 if (!nla)
1158 goto nla_put_failure;
1159
1160 err = ovs_flow_to_nlattrs(&flow->key, &flow->mask->key, skb);
1161 if (err)
1162 goto error;
1163
1164 nla_nest_end(skb, nla);
1165
1166 spin_lock_bh(&flow->lock);
1167 used = flow->used;
1168 stats.n_packets = flow->packet_count;
1169 stats.n_bytes = flow->byte_count;
1170 tcp_flags = flow->tcp_flags;
1171 spin_unlock_bh(&flow->lock);
1172
1173 if (used &&
1174 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1175 goto nla_put_failure;
1176
1177 if (stats.n_packets &&
1178 nla_put(skb, OVS_FLOW_ATTR_STATS,
1179 sizeof(struct ovs_flow_stats), &stats))
1180 goto nla_put_failure;
1181
1182 if (tcp_flags &&
1183 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1184 goto nla_put_failure;
1185
1186 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1187 * this is the first flow to be dumped into 'skb'. This is unusual for
1188 * Netlink but individual action lists can be longer than
1189 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1190 * The userspace caller can always fetch the actions separately if it
1191 * really wants them. (Most userspace callers in fact don't care.)
1192 *
1193 * This can only fail for dump operations because the skb is always
1194 * properly sized for single flows.
1195 */
1196 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1197 if (start) {
1198 const struct sw_flow_actions *sf_acts;
1199
1200 sf_acts = rcu_dereference_check(flow->sf_acts,
1201 lockdep_ovsl_is_held());
1202
1203 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1204 if (!err)
1205 nla_nest_end(skb, start);
1206 else {
1207 if (skb_orig_len)
1208 goto error;
1209
1210 nla_nest_cancel(skb, start);
1211 }
1212 } else if (skb_orig_len)
1213 goto nla_put_failure;
1214
1215 return genlmsg_end(skb, ovs_header);
1216
1217 nla_put_failure:
1218 err = -EMSGSIZE;
1219 error:
1220 genlmsg_cancel(skb, ovs_header);
1221 return err;
1222 }
1223
1224 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1225 {
1226 const struct sw_flow_actions *sf_acts;
1227
1228 sf_acts = ovsl_dereference(flow->sf_acts);
1229
1230 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
1231 }
1232
1233 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1234 struct datapath *dp,
1235 u32 portid, u32 seq, u8 cmd)
1236 {
1237 struct sk_buff *skb;
1238 int retval;
1239
1240 skb = ovs_flow_cmd_alloc_info(flow);
1241 if (!skb)
1242 return ERR_PTR(-ENOMEM);
1243
1244 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
1245 BUG_ON(retval < 0);
1246 return skb;
1247 }
1248
1249 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1250 {
1251 struct nlattr **a = info->attrs;
1252 struct ovs_header *ovs_header = info->userhdr;
1253 struct sw_flow_key key, masked_key;
1254 struct sw_flow *flow = NULL;
1255 struct sw_flow_mask mask;
1256 struct sk_buff *reply;
1257 struct datapath *dp;
1258 struct flow_table *table;
1259 struct sw_flow_actions *acts = NULL;
1260 struct sw_flow_match match;
1261 int error;
1262
1263 /* Extract key. */
1264 error = -EINVAL;
1265 if (!a[OVS_FLOW_ATTR_KEY])
1266 goto error;
1267
1268 ovs_match_init(&match, &key, &mask);
1269 error = ovs_match_from_nlattrs(&match,
1270 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1271 if (error)
1272 goto error;
1273
1274 /* Validate actions. */
1275 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1276 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1277 error = PTR_ERR(acts);
1278 if (IS_ERR(acts))
1279 goto error;
1280
1281 ovs_flow_key_mask(&masked_key, &key, &mask);
1282 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
1283 &masked_key, 0, &acts);
1284 if (error) {
1285 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
1286 goto err_kfree;
1287 }
1288 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1289 error = -EINVAL;
1290 goto error;
1291 }
1292
1293 ovs_lock();
1294 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1295 error = -ENODEV;
1296 if (!dp)
1297 goto err_unlock_ovs;
1298
1299 table = ovsl_dereference(dp->table);
1300
1301 /* Check if this is a duplicate flow */
1302 flow = ovs_flow_lookup(table, &key);
1303 if (!flow) {
1304 struct sw_flow_mask *mask_p;
1305 /* Bail out if we're not allowed to create a new flow. */
1306 error = -ENOENT;
1307 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1308 goto err_unlock_ovs;
1309
1310 /* Expand table, if necessary, to make room. */
1311 if (ovs_flow_tbl_need_to_expand(table)) {
1312 struct flow_table *new_table;
1313
1314 new_table = ovs_flow_tbl_expand(table);
1315 if (!IS_ERR(new_table)) {
1316 rcu_assign_pointer(dp->table, new_table);
1317 ovs_flow_tbl_destroy(table, true);
1318 table = ovsl_dereference(dp->table);
1319 }
1320 }
1321
1322 /* Allocate flow. */
1323 flow = ovs_flow_alloc();
1324 if (IS_ERR(flow)) {
1325 error = PTR_ERR(flow);
1326 goto err_unlock_ovs;
1327 }
1328 clear_stats(flow);
1329
1330 flow->key = masked_key;
1331 flow->unmasked_key = key;
1332
1333 /* Make sure mask is unique in the system */
1334 mask_p = ovs_sw_flow_mask_find(table, &mask);
1335 if (!mask_p) {
1336 /* Allocate a new mask if none exsits. */
1337 mask_p = ovs_sw_flow_mask_alloc();
1338 if (!mask_p)
1339 goto err_flow_free;
1340 mask_p->key = mask.key;
1341 mask_p->range = mask.range;
1342 ovs_sw_flow_mask_insert(table, mask_p);
1343 }
1344
1345 ovs_sw_flow_mask_add_ref(mask_p);
1346 flow->mask = mask_p;
1347 rcu_assign_pointer(flow->sf_acts, acts);
1348
1349 /* Put flow in bucket. */
1350 ovs_flow_insert(table, flow);
1351
1352 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1353 info->snd_seq, OVS_FLOW_CMD_NEW);
1354 } else {
1355 /* We found a matching flow. */
1356 struct sw_flow_actions *old_acts;
1357
1358 /* Bail out if we're not allowed to modify an existing flow.
1359 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1360 * because Generic Netlink treats the latter as a dump
1361 * request. We also accept NLM_F_EXCL in case that bug ever
1362 * gets fixed.
1363 */
1364 error = -EEXIST;
1365 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1366 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1367 goto err_unlock_ovs;
1368
1369 /* The unmasked key has to be the same for flow updates. */
1370 error = -EINVAL;
1371 if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) {
1372 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
1373 goto err_unlock_ovs;
1374 }
1375
1376 /* Update actions. */
1377 old_acts = ovsl_dereference(flow->sf_acts);
1378 rcu_assign_pointer(flow->sf_acts, acts);
1379 ovs_flow_deferred_free_acts(old_acts);
1380
1381 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1382 info->snd_seq, OVS_FLOW_CMD_NEW);
1383
1384 /* Clear stats. */
1385 if (a[OVS_FLOW_ATTR_CLEAR]) {
1386 spin_lock_bh(&flow->lock);
1387 clear_stats(flow);
1388 spin_unlock_bh(&flow->lock);
1389 }
1390 }
1391 ovs_unlock();
1392
1393 if (!IS_ERR(reply))
1394 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1395 else
1396 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1397 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1398 return 0;
1399
1400 err_flow_free:
1401 ovs_flow_free(flow, false);
1402 err_unlock_ovs:
1403 ovs_unlock();
1404 err_kfree:
1405 kfree(acts);
1406 error:
1407 return error;
1408 }
1409
1410 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1411 {
1412 struct nlattr **a = info->attrs;
1413 struct ovs_header *ovs_header = info->userhdr;
1414 struct sw_flow_key key;
1415 struct sk_buff *reply;
1416 struct sw_flow *flow;
1417 struct datapath *dp;
1418 struct flow_table *table;
1419 struct sw_flow_match match;
1420 int err;
1421
1422 if (!a[OVS_FLOW_ATTR_KEY]) {
1423 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1424 return -EINVAL;
1425 }
1426
1427 ovs_match_init(&match, &key, NULL);
1428 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1429 if (err)
1430 return err;
1431
1432 ovs_lock();
1433 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1434 if (!dp) {
1435 err = -ENODEV;
1436 goto unlock;
1437 }
1438
1439 table = ovsl_dereference(dp->table);
1440 flow = ovs_flow_lookup_unmasked_key(table, &match);
1441 if (!flow) {
1442 err = -ENOENT;
1443 goto unlock;
1444 }
1445
1446 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1447 info->snd_seq, OVS_FLOW_CMD_NEW);
1448 if (IS_ERR(reply)) {
1449 err = PTR_ERR(reply);
1450 goto unlock;
1451 }
1452
1453 ovs_unlock();
1454 return genlmsg_reply(reply, info);
1455 unlock:
1456 ovs_unlock();
1457 return err;
1458 }
1459
1460 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1461 {
1462 struct nlattr **a = info->attrs;
1463 struct ovs_header *ovs_header = info->userhdr;
1464 struct sw_flow_key key;
1465 struct sk_buff *reply;
1466 struct sw_flow *flow;
1467 struct datapath *dp;
1468 struct flow_table *table;
1469 struct sw_flow_match match;
1470 int err;
1471
1472 ovs_lock();
1473 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1474 if (!dp) {
1475 err = -ENODEV;
1476 goto unlock;
1477 }
1478
1479 if (!a[OVS_FLOW_ATTR_KEY]) {
1480 err = flush_flows(dp);
1481 goto unlock;
1482 }
1483
1484 ovs_match_init(&match, &key, NULL);
1485 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1486 if (err)
1487 goto unlock;
1488
1489 table = ovsl_dereference(dp->table);
1490 flow = ovs_flow_lookup_unmasked_key(table, &match);
1491 if (!flow) {
1492 err = -ENOENT;
1493 goto unlock;
1494 }
1495
1496 reply = ovs_flow_cmd_alloc_info(flow);
1497 if (!reply) {
1498 err = -ENOMEM;
1499 goto unlock;
1500 }
1501
1502 ovs_flow_remove(table, flow);
1503
1504 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1505 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1506 BUG_ON(err < 0);
1507
1508 ovs_flow_free(flow, true);
1509 ovs_unlock();
1510
1511 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1512 return 0;
1513 unlock:
1514 ovs_unlock();
1515 return err;
1516 }
1517
1518 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1519 {
1520 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1521 struct datapath *dp;
1522 struct flow_table *table;
1523
1524 rcu_read_lock();
1525 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1526 if (!dp) {
1527 rcu_read_unlock();
1528 return -ENODEV;
1529 }
1530
1531 table = rcu_dereference(dp->table);
1532 for (;;) {
1533 struct sw_flow *flow;
1534 u32 bucket, obj;
1535
1536 bucket = cb->args[0];
1537 obj = cb->args[1];
1538 flow = ovs_flow_dump_next(table, &bucket, &obj);
1539 if (!flow)
1540 break;
1541
1542 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1543 NETLINK_CB(cb->skb).portid,
1544 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1545 OVS_FLOW_CMD_NEW) < 0)
1546 break;
1547
1548 cb->args[0] = bucket;
1549 cb->args[1] = obj;
1550 }
1551 rcu_read_unlock();
1552 return skb->len;
1553 }
1554
1555 static struct genl_ops dp_flow_genl_ops[] = {
1556 { .cmd = OVS_FLOW_CMD_NEW,
1557 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1558 .policy = flow_policy,
1559 .doit = ovs_flow_cmd_new_or_set
1560 },
1561 { .cmd = OVS_FLOW_CMD_DEL,
1562 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1563 .policy = flow_policy,
1564 .doit = ovs_flow_cmd_del
1565 },
1566 { .cmd = OVS_FLOW_CMD_GET,
1567 .flags = 0, /* OK for unprivileged users. */
1568 .policy = flow_policy,
1569 .doit = ovs_flow_cmd_get,
1570 .dumpit = ovs_flow_cmd_dump
1571 },
1572 { .cmd = OVS_FLOW_CMD_SET,
1573 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1574 .policy = flow_policy,
1575 .doit = ovs_flow_cmd_new_or_set,
1576 },
1577 };
1578
1579 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1580 #ifdef HAVE_NLA_NUL_STRING
1581 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1582 #endif
1583 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1584 };
1585
1586 static struct genl_family dp_datapath_genl_family = {
1587 .id = GENL_ID_GENERATE,
1588 .hdrsize = sizeof(struct ovs_header),
1589 .name = OVS_DATAPATH_FAMILY,
1590 .version = OVS_DATAPATH_VERSION,
1591 .maxattr = OVS_DP_ATTR_MAX,
1592 SET_NETNSOK
1593 SET_PARALLEL_OPS
1594 };
1595
1596 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1597 .name = OVS_DATAPATH_MCGROUP
1598 };
1599
1600 static size_t ovs_dp_cmd_msg_size(void)
1601 {
1602 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1603
1604 msgsize += nla_total_size(IFNAMSIZ);
1605 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1606
1607 return msgsize;
1608 }
1609
1610 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1611 u32 portid, u32 seq, u32 flags, u8 cmd)
1612 {
1613 struct ovs_header *ovs_header;
1614 struct ovs_dp_stats dp_stats;
1615 int err;
1616
1617 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1618 flags, cmd);
1619 if (!ovs_header)
1620 goto error;
1621
1622 ovs_header->dp_ifindex = get_dpifindex(dp);
1623
1624 rcu_read_lock();
1625 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1626 rcu_read_unlock();
1627 if (err)
1628 goto nla_put_failure;
1629
1630 get_dp_stats(dp, &dp_stats);
1631 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1632 goto nla_put_failure;
1633
1634 return genlmsg_end(skb, ovs_header);
1635
1636 nla_put_failure:
1637 genlmsg_cancel(skb, ovs_header);
1638 error:
1639 return -EMSGSIZE;
1640 }
1641
1642 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1643 u32 seq, u8 cmd)
1644 {
1645 struct sk_buff *skb;
1646 int retval;
1647
1648 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1649 if (!skb)
1650 return ERR_PTR(-ENOMEM);
1651
1652 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1653 if (retval < 0) {
1654 kfree_skb(skb);
1655 return ERR_PTR(retval);
1656 }
1657 return skb;
1658 }
1659
1660 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1661 {
1662 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1663 }
1664
1665 /* Called with ovs_mutex. */
1666 static struct datapath *lookup_datapath(struct net *net,
1667 struct ovs_header *ovs_header,
1668 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1669 {
1670 struct datapath *dp;
1671
1672 if (!a[OVS_DP_ATTR_NAME])
1673 dp = get_dp(net, ovs_header->dp_ifindex);
1674 else {
1675 struct vport *vport;
1676
1677 rcu_read_lock();
1678 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1679 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1680 rcu_read_unlock();
1681 }
1682 return dp ? dp : ERR_PTR(-ENODEV);
1683 }
1684
1685 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1686 {
1687 struct nlattr **a = info->attrs;
1688 struct vport_parms parms;
1689 struct sk_buff *reply;
1690 struct datapath *dp;
1691 struct vport *vport;
1692 struct ovs_net *ovs_net;
1693 int err, i;
1694
1695 err = -EINVAL;
1696 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1697 goto err;
1698
1699 err = ovs_dp_cmd_validate(a);
1700 if (err)
1701 goto err;
1702
1703 ovs_lock();
1704
1705 err = -ENOMEM;
1706 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1707 if (dp == NULL)
1708 goto err_unlock_ovs;
1709
1710 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1711
1712 /* Allocate table. */
1713 err = -ENOMEM;
1714 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1715 if (!dp->table)
1716 goto err_free_dp;
1717
1718 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1719 if (!dp->stats_percpu) {
1720 err = -ENOMEM;
1721 goto err_destroy_table;
1722 }
1723
1724 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1725 GFP_KERNEL);
1726 if (!dp->ports) {
1727 err = -ENOMEM;
1728 goto err_destroy_percpu;
1729 }
1730
1731 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1732 INIT_HLIST_HEAD(&dp->ports[i]);
1733
1734 /* Set up our datapath device. */
1735 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1736 parms.type = OVS_VPORT_TYPE_INTERNAL;
1737 parms.options = NULL;
1738 parms.dp = dp;
1739 parms.port_no = OVSP_LOCAL;
1740 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1741
1742 vport = new_vport(&parms);
1743 if (IS_ERR(vport)) {
1744 err = PTR_ERR(vport);
1745 if (err == -EBUSY)
1746 err = -EEXIST;
1747
1748 goto err_destroy_ports_array;
1749 }
1750
1751 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1752 info->snd_seq, OVS_DP_CMD_NEW);
1753 err = PTR_ERR(reply);
1754 if (IS_ERR(reply))
1755 goto err_destroy_local_port;
1756
1757 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1758 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1759
1760 ovs_unlock();
1761
1762 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1763 return 0;
1764
1765 err_destroy_local_port:
1766 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1767 err_destroy_ports_array:
1768 kfree(dp->ports);
1769 err_destroy_percpu:
1770 free_percpu(dp->stats_percpu);
1771 err_destroy_table:
1772 ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
1773 err_free_dp:
1774 release_net(ovs_dp_get_net(dp));
1775 kfree(dp);
1776 err_unlock_ovs:
1777 ovs_unlock();
1778 err:
1779 return err;
1780 }
1781
1782 /* Called with ovs_mutex. */
1783 static void __dp_destroy(struct datapath *dp)
1784 {
1785 int i;
1786
1787 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1788 struct vport *vport;
1789 struct hlist_node *n;
1790
1791 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1792 if (vport->port_no != OVSP_LOCAL)
1793 ovs_dp_detach_port(vport);
1794 }
1795
1796 list_del_rcu(&dp->list_node);
1797
1798 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1799 * all port in datapath are destroyed first before freeing datapath.
1800 */
1801 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1802
1803 call_rcu(&dp->rcu, destroy_dp_rcu);
1804 }
1805
1806 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1807 {
1808 struct sk_buff *reply;
1809 struct datapath *dp;
1810 int err;
1811
1812 err = ovs_dp_cmd_validate(info->attrs);
1813 if (err)
1814 return err;
1815
1816 ovs_lock();
1817 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1818 err = PTR_ERR(dp);
1819 if (IS_ERR(dp))
1820 goto unlock;
1821
1822 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1823 info->snd_seq, OVS_DP_CMD_DEL);
1824 err = PTR_ERR(reply);
1825 if (IS_ERR(reply))
1826 goto unlock;
1827
1828 __dp_destroy(dp);
1829 ovs_unlock();
1830
1831 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1832
1833 return 0;
1834 unlock:
1835 ovs_unlock();
1836 return err;
1837 }
1838
1839 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1840 {
1841 struct sk_buff *reply;
1842 struct datapath *dp;
1843 int err;
1844
1845 err = ovs_dp_cmd_validate(info->attrs);
1846 if (err)
1847 return err;
1848
1849 ovs_lock();
1850 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1851 err = PTR_ERR(dp);
1852 if (IS_ERR(dp))
1853 goto unlock;
1854
1855 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1856 info->snd_seq, OVS_DP_CMD_NEW);
1857 if (IS_ERR(reply)) {
1858 err = PTR_ERR(reply);
1859 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1860 ovs_dp_datapath_multicast_group.id, err);
1861 err = 0;
1862 goto unlock;
1863 }
1864
1865 ovs_unlock();
1866 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1867
1868 return 0;
1869 unlock:
1870 ovs_unlock();
1871 return err;
1872 }
1873
1874 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1875 {
1876 struct sk_buff *reply;
1877 struct datapath *dp;
1878 int err;
1879
1880 err = ovs_dp_cmd_validate(info->attrs);
1881 if (err)
1882 return err;
1883
1884 ovs_lock();
1885 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1886 if (IS_ERR(dp)) {
1887 err = PTR_ERR(dp);
1888 goto unlock;
1889 }
1890
1891 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1892 info->snd_seq, OVS_DP_CMD_NEW);
1893 if (IS_ERR(reply)) {
1894 err = PTR_ERR(reply);
1895 goto unlock;
1896 }
1897
1898 ovs_unlock();
1899 return genlmsg_reply(reply, info);
1900
1901 unlock:
1902 ovs_unlock();
1903 return err;
1904 }
1905
1906 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1907 {
1908 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1909 struct datapath *dp;
1910 int skip = cb->args[0];
1911 int i = 0;
1912
1913 rcu_read_lock();
1914 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1915 if (i >= skip &&
1916 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1917 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1918 OVS_DP_CMD_NEW) < 0)
1919 break;
1920 i++;
1921 }
1922 rcu_read_unlock();
1923
1924 cb->args[0] = i;
1925
1926 return skb->len;
1927 }
1928
1929 static struct genl_ops dp_datapath_genl_ops[] = {
1930 { .cmd = OVS_DP_CMD_NEW,
1931 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1932 .policy = datapath_policy,
1933 .doit = ovs_dp_cmd_new
1934 },
1935 { .cmd = OVS_DP_CMD_DEL,
1936 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1937 .policy = datapath_policy,
1938 .doit = ovs_dp_cmd_del
1939 },
1940 { .cmd = OVS_DP_CMD_GET,
1941 .flags = 0, /* OK for unprivileged users. */
1942 .policy = datapath_policy,
1943 .doit = ovs_dp_cmd_get,
1944 .dumpit = ovs_dp_cmd_dump
1945 },
1946 { .cmd = OVS_DP_CMD_SET,
1947 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1948 .policy = datapath_policy,
1949 .doit = ovs_dp_cmd_set,
1950 },
1951 };
1952
1953 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1954 #ifdef HAVE_NLA_NUL_STRING
1955 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1956 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1957 #else
1958 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1959 #endif
1960 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1961 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1962 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1963 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1964 };
1965
1966 static struct genl_family dp_vport_genl_family = {
1967 .id = GENL_ID_GENERATE,
1968 .hdrsize = sizeof(struct ovs_header),
1969 .name = OVS_VPORT_FAMILY,
1970 .version = OVS_VPORT_VERSION,
1971 .maxattr = OVS_VPORT_ATTR_MAX,
1972 SET_NETNSOK
1973 SET_PARALLEL_OPS
1974 };
1975
1976 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1977 .name = OVS_VPORT_MCGROUP
1978 };
1979
1980 /* Called with ovs_mutex or RCU read lock. */
1981 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1982 u32 portid, u32 seq, u32 flags, u8 cmd)
1983 {
1984 struct ovs_header *ovs_header;
1985 struct ovs_vport_stats vport_stats;
1986 int err;
1987
1988 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1989 flags, cmd);
1990 if (!ovs_header)
1991 return -EMSGSIZE;
1992
1993 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1994
1995 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1996 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1997 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1998 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1999 goto nla_put_failure;
2000
2001 ovs_vport_get_stats(vport, &vport_stats);
2002 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
2003 &vport_stats))
2004 goto nla_put_failure;
2005
2006 err = ovs_vport_get_options(vport, skb);
2007 if (err == -EMSGSIZE)
2008 goto error;
2009
2010 return genlmsg_end(skb, ovs_header);
2011
2012 nla_put_failure:
2013 err = -EMSGSIZE;
2014 error:
2015 genlmsg_cancel(skb, ovs_header);
2016 return err;
2017 }
2018
2019 /* Called with ovs_mutex or RCU read lock. */
2020 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
2021 u32 seq, u8 cmd)
2022 {
2023 struct sk_buff *skb;
2024 int retval;
2025
2026 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2027 if (!skb)
2028 return ERR_PTR(-ENOMEM);
2029
2030 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
2031 BUG_ON(retval < 0);
2032
2033 return skb;
2034 }
2035
2036 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2037 {
2038 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
2039 }
2040
2041 /* Called with ovs_mutex or RCU read lock. */
2042 static struct vport *lookup_vport(struct net *net,
2043 struct ovs_header *ovs_header,
2044 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2045 {
2046 struct datapath *dp;
2047 struct vport *vport;
2048
2049 if (a[OVS_VPORT_ATTR_NAME]) {
2050 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2051 if (!vport)
2052 return ERR_PTR(-ENODEV);
2053 if (ovs_header->dp_ifindex &&
2054 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2055 return ERR_PTR(-ENODEV);
2056 return vport;
2057 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2058 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2059
2060 if (port_no >= DP_MAX_PORTS)
2061 return ERR_PTR(-EFBIG);
2062
2063 dp = get_dp(net, ovs_header->dp_ifindex);
2064 if (!dp)
2065 return ERR_PTR(-ENODEV);
2066
2067 vport = ovs_vport_ovsl_rcu(dp, port_no);
2068 if (!vport)
2069 return ERR_PTR(-ENODEV);
2070 return vport;
2071 } else
2072 return ERR_PTR(-EINVAL);
2073 }
2074
2075 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2076 {
2077 struct nlattr **a = info->attrs;
2078 struct ovs_header *ovs_header = info->userhdr;
2079 struct vport_parms parms;
2080 struct sk_buff *reply;
2081 struct vport *vport;
2082 struct datapath *dp;
2083 u32 port_no;
2084 int err;
2085
2086 err = -EINVAL;
2087 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2088 !a[OVS_VPORT_ATTR_UPCALL_PID])
2089 goto exit;
2090
2091 err = ovs_vport_cmd_validate(a);
2092 if (err)
2093 goto exit;
2094
2095 ovs_lock();
2096 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2097 err = -ENODEV;
2098 if (!dp)
2099 goto exit_unlock;
2100
2101 if (a[OVS_VPORT_ATTR_PORT_NO]) {
2102 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2103
2104 err = -EFBIG;
2105 if (port_no >= DP_MAX_PORTS)
2106 goto exit_unlock;
2107
2108 vport = ovs_vport_ovsl(dp, port_no);
2109 err = -EBUSY;
2110 if (vport)
2111 goto exit_unlock;
2112 } else {
2113 for (port_no = 1; ; port_no++) {
2114 if (port_no >= DP_MAX_PORTS) {
2115 err = -EFBIG;
2116 goto exit_unlock;
2117 }
2118 vport = ovs_vport_ovsl(dp, port_no);
2119 if (!vport)
2120 break;
2121 }
2122 }
2123
2124 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2125 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2126 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2127 parms.dp = dp;
2128 parms.port_no = port_no;
2129 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2130
2131 vport = new_vport(&parms);
2132 err = PTR_ERR(vport);
2133 if (IS_ERR(vport))
2134 goto exit_unlock;
2135
2136 err = 0;
2137 if (a[OVS_VPORT_ATTR_STATS])
2138 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2139
2140 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
2141 OVS_VPORT_CMD_NEW);
2142 if (IS_ERR(reply)) {
2143 err = PTR_ERR(reply);
2144 ovs_dp_detach_port(vport);
2145 goto exit_unlock;
2146 }
2147
2148 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2149
2150 exit_unlock:
2151 ovs_unlock();
2152 exit:
2153 return err;
2154 }
2155
2156 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2157 {
2158 struct nlattr **a = info->attrs;
2159 struct sk_buff *reply;
2160 struct vport *vport;
2161 int err;
2162
2163 err = ovs_vport_cmd_validate(a);
2164 if (err)
2165 goto exit;
2166
2167 ovs_lock();
2168 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2169 err = PTR_ERR(vport);
2170 if (IS_ERR(vport))
2171 goto exit_unlock;
2172
2173 if (a[OVS_VPORT_ATTR_TYPE] &&
2174 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2175 err = -EINVAL;
2176 goto exit_unlock;
2177 }
2178
2179 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2180 if (!reply) {
2181 err = -ENOMEM;
2182 goto exit_unlock;
2183 }
2184
2185 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2186 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2187 if (err)
2188 goto exit_free;
2189 }
2190
2191 if (a[OVS_VPORT_ATTR_STATS])
2192 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2193
2194 if (a[OVS_VPORT_ATTR_UPCALL_PID])
2195 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2196
2197 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2198 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2199 BUG_ON(err < 0);
2200
2201 ovs_unlock();
2202 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2203 return 0;
2204
2205 exit_free:
2206 kfree_skb(reply);
2207 exit_unlock:
2208 ovs_unlock();
2209 exit:
2210 return err;
2211 }
2212
2213 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2214 {
2215 struct nlattr **a = info->attrs;
2216 struct sk_buff *reply;
2217 struct vport *vport;
2218 int err;
2219
2220 err = ovs_vport_cmd_validate(a);
2221 if (err)
2222 goto exit;
2223
2224 ovs_lock();
2225 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2226 err = PTR_ERR(vport);
2227 if (IS_ERR(vport))
2228 goto exit_unlock;
2229
2230 if (vport->port_no == OVSP_LOCAL) {
2231 err = -EINVAL;
2232 goto exit_unlock;
2233 }
2234
2235 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2236 info->snd_seq, OVS_VPORT_CMD_DEL);
2237 err = PTR_ERR(reply);
2238 if (IS_ERR(reply))
2239 goto exit_unlock;
2240
2241 err = 0;
2242 ovs_dp_detach_port(vport);
2243
2244 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2245
2246 exit_unlock:
2247 ovs_unlock();
2248 exit:
2249 return err;
2250 }
2251
2252 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2253 {
2254 struct nlattr **a = info->attrs;
2255 struct ovs_header *ovs_header = info->userhdr;
2256 struct sk_buff *reply;
2257 struct vport *vport;
2258 int err;
2259
2260 err = ovs_vport_cmd_validate(a);
2261 if (err)
2262 goto exit;
2263
2264 rcu_read_lock();
2265 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2266 err = PTR_ERR(vport);
2267 if (IS_ERR(vport))
2268 goto exit_unlock;
2269
2270 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2271 info->snd_seq, OVS_VPORT_CMD_NEW);
2272 err = PTR_ERR(reply);
2273 if (IS_ERR(reply))
2274 goto exit_unlock;
2275
2276 rcu_read_unlock();
2277
2278 return genlmsg_reply(reply, info);
2279
2280 exit_unlock:
2281 rcu_read_unlock();
2282 exit:
2283 return err;
2284 }
2285
2286 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2287 {
2288 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2289 struct datapath *dp;
2290 int bucket = cb->args[0], skip = cb->args[1];
2291 int i, j = 0;
2292
2293 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2294 if (!dp)
2295 return -ENODEV;
2296
2297 rcu_read_lock();
2298 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2299 struct vport *vport;
2300
2301 j = 0;
2302 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2303 if (j >= skip &&
2304 ovs_vport_cmd_fill_info(vport, skb,
2305 NETLINK_CB(cb->skb).portid,
2306 cb->nlh->nlmsg_seq,
2307 NLM_F_MULTI,
2308 OVS_VPORT_CMD_NEW) < 0)
2309 goto out;
2310
2311 j++;
2312 }
2313 skip = 0;
2314 }
2315 out:
2316 rcu_read_unlock();
2317
2318 cb->args[0] = i;
2319 cb->args[1] = j;
2320
2321 return skb->len;
2322 }
2323
2324 static struct genl_ops dp_vport_genl_ops[] = {
2325 { .cmd = OVS_VPORT_CMD_NEW,
2326 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2327 .policy = vport_policy,
2328 .doit = ovs_vport_cmd_new
2329 },
2330 { .cmd = OVS_VPORT_CMD_DEL,
2331 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2332 .policy = vport_policy,
2333 .doit = ovs_vport_cmd_del
2334 },
2335 { .cmd = OVS_VPORT_CMD_GET,
2336 .flags = 0, /* OK for unprivileged users. */
2337 .policy = vport_policy,
2338 .doit = ovs_vport_cmd_get,
2339 .dumpit = ovs_vport_cmd_dump
2340 },
2341 { .cmd = OVS_VPORT_CMD_SET,
2342 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2343 .policy = vport_policy,
2344 .doit = ovs_vport_cmd_set,
2345 },
2346 };
2347
2348 struct genl_family_and_ops {
2349 struct genl_family *family;
2350 struct genl_ops *ops;
2351 int n_ops;
2352 struct genl_multicast_group *group;
2353 };
2354
2355 static const struct genl_family_and_ops dp_genl_families[] = {
2356 { &dp_datapath_genl_family,
2357 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2358 &ovs_dp_datapath_multicast_group },
2359 { &dp_vport_genl_family,
2360 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2361 &ovs_dp_vport_multicast_group },
2362 { &dp_flow_genl_family,
2363 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2364 &ovs_dp_flow_multicast_group },
2365 { &dp_packet_genl_family,
2366 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2367 NULL },
2368 };
2369
2370 static void dp_unregister_genl(int n_families)
2371 {
2372 int i;
2373
2374 for (i = 0; i < n_families; i++)
2375 genl_unregister_family(dp_genl_families[i].family);
2376 }
2377
2378 static int dp_register_genl(void)
2379 {
2380 int n_registered;
2381 int err;
2382 int i;
2383
2384 n_registered = 0;
2385 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2386 const struct genl_family_and_ops *f = &dp_genl_families[i];
2387
2388 err = genl_register_family_with_ops(f->family, f->ops,
2389 f->n_ops);
2390 if (err)
2391 goto error;
2392 n_registered++;
2393
2394 if (f->group) {
2395 err = genl_register_mc_group(f->family, f->group);
2396 if (err)
2397 goto error;
2398 }
2399 }
2400
2401 return 0;
2402
2403 error:
2404 dp_unregister_genl(n_registered);
2405 return err;
2406 }
2407
2408 static void rehash_flow_table(struct work_struct *work)
2409 {
2410 struct datapath *dp;
2411 struct net *net;
2412
2413 ovs_lock();
2414 rtnl_lock();
2415 for_each_net(net) {
2416 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2417
2418 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2419 struct flow_table *old_table = ovsl_dereference(dp->table);
2420 struct flow_table *new_table;
2421
2422 new_table = ovs_flow_tbl_rehash(old_table);
2423 if (!IS_ERR(new_table)) {
2424 rcu_assign_pointer(dp->table, new_table);
2425 ovs_flow_tbl_destroy(old_table, true);
2426 }
2427 }
2428 }
2429 rtnl_unlock();
2430 ovs_unlock();
2431 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2432 }
2433
2434 static int __net_init ovs_init_net(struct net *net)
2435 {
2436 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2437
2438 INIT_LIST_HEAD(&ovs_net->dps);
2439 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2440 return 0;
2441 }
2442
2443 static void __net_exit ovs_exit_net(struct net *net)
2444 {
2445 struct datapath *dp, *dp_next;
2446 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2447
2448 ovs_lock();
2449 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2450 __dp_destroy(dp);
2451 ovs_unlock();
2452
2453 cancel_work_sync(&ovs_net->dp_notify_work);
2454 }
2455
2456 static struct pernet_operations ovs_net_ops = {
2457 .init = ovs_init_net,
2458 .exit = ovs_exit_net,
2459 .id = &ovs_net_id,
2460 .size = sizeof(struct ovs_net),
2461 };
2462
2463 DEFINE_COMPAT_PNET_REG_FUNC(device);
2464
2465 static int __init dp_init(void)
2466 {
2467 int err;
2468
2469 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2470
2471 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2472 VERSION);
2473
2474 err = ovs_workqueues_init();
2475 if (err)
2476 goto error;
2477
2478 err = ovs_flow_init();
2479 if (err)
2480 goto error_wq;
2481
2482 err = ovs_vport_init();
2483 if (err)
2484 goto error_flow_exit;
2485
2486 err = register_pernet_device(&ovs_net_ops);
2487 if (err)
2488 goto error_vport_exit;
2489
2490 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2491 if (err)
2492 goto error_netns_exit;
2493
2494 err = dp_register_genl();
2495 if (err < 0)
2496 goto error_unreg_notifier;
2497
2498 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2499
2500 return 0;
2501
2502 error_unreg_notifier:
2503 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2504 error_netns_exit:
2505 unregister_pernet_device(&ovs_net_ops);
2506 error_vport_exit:
2507 ovs_vport_exit();
2508 error_flow_exit:
2509 ovs_flow_exit();
2510 error_wq:
2511 ovs_workqueues_exit();
2512 error:
2513 return err;
2514 }
2515
2516 static void dp_cleanup(void)
2517 {
2518 cancel_delayed_work_sync(&rehash_flow_wq);
2519 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2520 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2521 unregister_pernet_device(&ovs_net_ops);
2522 rcu_barrier();
2523 ovs_vport_exit();
2524 ovs_flow_exit();
2525 ovs_workqueues_exit();
2526 }
2527
2528 module_init(dp_init);
2529 module_exit(dp_cleanup);
2530
2531 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2532 MODULE_LICENSE("GPL");
2533 MODULE_VERSION(VERSION);