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