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