datapath: Remove checksum compat support
[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 static void rehash_flow_table(struct work_struct *work);
65 static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
66
67 int ovs_net_id __read_mostly;
68
69 static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
70                        struct genl_multicast_group *grp)
71 {
72         genl_notify(skb, genl_info_net(info), info->snd_portid,
73                     grp->id, info->nlhdr, GFP_KERNEL);
74 }
75
76 /**
77  * DOC: Locking:
78  *
79  * All writes e.g. Writes to device state (add/remove datapath, port, set
80  * operations on vports, etc.), Writes to other state (flow table
81  * modifications, set miscellaneous datapath parameters, etc.) are protected
82  * by ovs_lock.
83  *
84  * Reads are protected by RCU.
85  *
86  * There are a few special cases (mostly stats) that have their own
87  * synchronization but they nest under all of above and don't interact with
88  * each other.
89  *
90  * The RTNL lock nests inside ovs_mutex.
91  */
92
93 static DEFINE_MUTEX(ovs_mutex);
94
95 void ovs_lock(void)
96 {
97         mutex_lock(&ovs_mutex);
98 }
99
100 void ovs_unlock(void)
101 {
102         mutex_unlock(&ovs_mutex);
103 }
104
105 #ifdef CONFIG_LOCKDEP
106 int lockdep_ovsl_is_held(void)
107 {
108         if (debug_locks)
109                 return lockdep_is_held(&ovs_mutex);
110         else
111                 return 1;
112 }
113 #endif
114
115 static struct vport *new_vport(const struct vport_parms *);
116 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
117                              const struct dp_upcall_info *);
118 static int queue_userspace_packet(struct net *, int dp_ifindex,
119                                   struct sk_buff *,
120                                   const struct dp_upcall_info *);
121
122 /* Must be called with rcu_read_lock or ovs_mutex. */
123 static struct datapath *get_dp(struct net *net, int dp_ifindex)
124 {
125         struct datapath *dp = NULL;
126         struct net_device *dev;
127
128         rcu_read_lock();
129         dev = dev_get_by_index_rcu(net, dp_ifindex);
130         if (dev) {
131                 struct vport *vport = ovs_internal_dev_get_vport(dev);
132                 if (vport)
133                         dp = vport->dp;
134         }
135         rcu_read_unlock();
136
137         return dp;
138 }
139
140 /* Must be called with rcu_read_lock or ovs_mutex. */
141 const char *ovs_dp_name(const struct datapath *dp)
142 {
143         struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
144         return vport->ops->get_name(vport);
145 }
146
147 static int get_dpifindex(struct datapath *dp)
148 {
149         struct vport *local;
150         int ifindex;
151
152         rcu_read_lock();
153
154         local = ovs_vport_rcu(dp, OVSP_LOCAL);
155         if (local)
156                 ifindex = netdev_vport_priv(local)->dev->ifindex;
157         else
158                 ifindex = 0;
159
160         rcu_read_unlock();
161
162         return ifindex;
163 }
164
165 static void destroy_dp_rcu(struct rcu_head *rcu)
166 {
167         struct datapath *dp = container_of(rcu, struct datapath, rcu);
168
169         ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false);
170         free_percpu(dp->stats_percpu);
171         release_net(ovs_dp_get_net(dp));
172         kfree(dp->ports);
173         kfree(dp);
174 }
175
176 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
177                                             u16 port_no)
178 {
179         return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
180 }
181
182 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
183 {
184         struct vport *vport;
185         struct hlist_head *head;
186
187         head = vport_hash_bucket(dp, port_no);
188         hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
189                 if (vport->port_no == port_no)
190                         return vport;
191         }
192         return NULL;
193 }
194
195 /* Called with ovs_mutex. */
196 static struct vport *new_vport(const struct vport_parms *parms)
197 {
198         struct vport *vport;
199
200         vport = ovs_vport_add(parms);
201         if (!IS_ERR(vport)) {
202                 struct datapath *dp = parms->dp;
203                 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
204
205                 hlist_add_head_rcu(&vport->dp_hash_node, head);
206         }
207         return vport;
208 }
209
210 void ovs_dp_detach_port(struct vport *p)
211 {
212         ASSERT_OVSL();
213
214         /* First drop references to device. */
215         hlist_del_rcu(&p->dp_hash_node);
216
217         /* Then destroy it. */
218         ovs_vport_del(p);
219 }
220
221 /* Must be called with rcu_read_lock. */
222 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
223 {
224         struct datapath *dp = p->dp;
225         struct sw_flow *flow;
226         struct dp_stats_percpu *stats;
227         struct sw_flow_key key;
228         u64 *stats_counter;
229         int error;
230
231         stats = this_cpu_ptr(dp->stats_percpu);
232
233         /* Extract flow from 'skb' into 'key'. */
234         error = ovs_flow_extract(skb, p->port_no, &key);
235         if (unlikely(error)) {
236                 kfree_skb(skb);
237                 return;
238         }
239
240         /* Look up flow. */
241         flow = ovs_flow_lookup(rcu_dereference(dp->table), &key);
242         if (unlikely(!flow)) {
243                 struct dp_upcall_info upcall;
244
245                 upcall.cmd = OVS_PACKET_CMD_MISS;
246                 upcall.key = &key;
247                 upcall.userdata = NULL;
248                 upcall.portid = p->upcall_portid;
249                 ovs_dp_upcall(dp, skb, &upcall);
250                 consume_skb(skb);
251                 stats_counter = &stats->n_missed;
252                 goto out;
253         }
254
255         OVS_CB(skb)->flow = flow;
256         OVS_CB(skb)->pkt_key = &key;
257
258         stats_counter = &stats->n_hit;
259         ovs_flow_used(OVS_CB(skb)->flow, skb);
260         ovs_execute_actions(dp, skb);
261
262 out:
263         /* Update datapath statistics. */
264         u64_stats_update_begin(&stats->sync);
265         (*stats_counter)++;
266         u64_stats_update_end(&stats->sync);
267 }
268
269 static struct genl_family dp_packet_genl_family = {
270         .id = GENL_ID_GENERATE,
271         .hdrsize = sizeof(struct ovs_header),
272         .name = OVS_PACKET_FAMILY,
273         .version = OVS_PACKET_VERSION,
274         .maxattr = OVS_PACKET_ATTR_MAX,
275          SET_NETNSOK
276          SET_PARALLEL_OPS
277 };
278
279 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
280                   const struct dp_upcall_info *upcall_info)
281 {
282         struct dp_stats_percpu *stats;
283         int dp_ifindex;
284         int err;
285
286         if (upcall_info->portid == 0) {
287                 err = -ENOTCONN;
288                 goto err;
289         }
290
291         dp_ifindex = get_dpifindex(dp);
292         if (!dp_ifindex) {
293                 err = -ENODEV;
294                 goto err;
295         }
296
297         if (!skb_is_gso(skb))
298                 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
299         else
300                 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
301         if (err)
302                 goto err;
303
304         return 0;
305
306 err:
307         stats = this_cpu_ptr(dp->stats_percpu);
308
309         u64_stats_update_begin(&stats->sync);
310         stats->n_lost++;
311         u64_stats_update_end(&stats->sync);
312
313         return err;
314 }
315
316 static int queue_gso_packets(struct net *net, int dp_ifindex,
317                              struct sk_buff *skb,
318                              const struct dp_upcall_info *upcall_info)
319 {
320         unsigned short gso_type = skb_shinfo(skb)->gso_type;
321         struct dp_upcall_info later_info;
322         struct sw_flow_key later_key;
323         struct sk_buff *segs, *nskb;
324         int err;
325
326         segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
327         if (IS_ERR(segs))
328                 return PTR_ERR(segs);
329
330         /* Queue all of the segments. */
331         skb = segs;
332         do {
333                 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
334                 if (err)
335                         break;
336
337                 if (skb == segs && gso_type & SKB_GSO_UDP) {
338                         /* The initial flow key extracted by ovs_flow_extract()
339                          * in this case is for a first fragment, so we need to
340                          * properly mark later fragments.
341                          */
342                         later_key = *upcall_info->key;
343                         later_key.ip.frag = OVS_FRAG_TYPE_LATER;
344
345                         later_info = *upcall_info;
346                         later_info.key = &later_key;
347                         upcall_info = &later_info;
348                 }
349         } while ((skb = skb->next));
350
351         /* Free all of the segments. */
352         skb = segs;
353         do {
354                 nskb = skb->next;
355                 if (err)
356                         kfree_skb(skb);
357                 else
358                         consume_skb(skb);
359         } while ((skb = nskb));
360         return err;
361 }
362
363 static size_t key_attr_size(void)
364 {
365         return    nla_total_size(4)   /* OVS_KEY_ATTR_PRIORITY */
366                 + nla_total_size(0)   /* OVS_KEY_ATTR_TUNNEL */
367                   + nla_total_size(8)   /* OVS_TUNNEL_KEY_ATTR_ID */
368                   + nla_total_size(4)   /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
369                   + nla_total_size(4)   /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
370                   + nla_total_size(1)   /* OVS_TUNNEL_KEY_ATTR_TOS */
371                   + nla_total_size(1)   /* OVS_TUNNEL_KEY_ATTR_TTL */
372                   + nla_total_size(0)   /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
373                   + nla_total_size(0)   /* OVS_TUNNEL_KEY_ATTR_CSUM */
374                 + nla_total_size(4)   /* OVS_KEY_ATTR_IN_PORT */
375                 + nla_total_size(4)   /* OVS_KEY_ATTR_SKB_MARK */
376                 + nla_total_size(12)  /* OVS_KEY_ATTR_ETHERNET */
377                 + nla_total_size(2)   /* OVS_KEY_ATTR_ETHERTYPE */
378                 + nla_total_size(4)   /* OVS_KEY_ATTR_8021Q */
379                 + nla_total_size(0)   /* OVS_KEY_ATTR_ENCAP */
380                 + nla_total_size(2)   /* OVS_KEY_ATTR_ETHERTYPE */
381                 + nla_total_size(40)  /* OVS_KEY_ATTR_IPV6 */
382                 + nla_total_size(2)   /* OVS_KEY_ATTR_ICMPV6 */
383                 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
384 }
385
386 static size_t upcall_msg_size(const struct sk_buff *skb,
387                               const struct nlattr *userdata)
388 {
389         size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
390                 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
391                 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
392
393         /* OVS_PACKET_ATTR_USERDATA */
394         if (userdata)
395                 size += NLA_ALIGN(userdata->nla_len);
396
397         return size;
398 }
399
400 static int queue_userspace_packet(struct net *net, int dp_ifindex,
401                                   struct sk_buff *skb,
402                                   const struct dp_upcall_info *upcall_info)
403 {
404         struct ovs_header *upcall;
405         struct sk_buff *nskb = NULL;
406         struct sk_buff *user_skb; /* to be queued to userspace */
407         struct nlattr *nla;
408         int err;
409
410         if (vlan_tx_tag_present(skb)) {
411                 nskb = skb_clone(skb, GFP_ATOMIC);
412                 if (!nskb)
413                         return -ENOMEM;
414                 
415                 err = vlan_deaccel_tag(nskb);
416                 if (err)
417                         return err;
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 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
949         [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
950 #else
951         [OVS_PACKET_ATTR_PACKET] = { .minlen = ETH_HLEN },
952 #endif
953         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
954         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
955 };
956
957 static struct genl_ops dp_packet_genl_ops[] = {
958         { .cmd = OVS_PACKET_CMD_EXECUTE,
959           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
960           .policy = packet_policy,
961           .doit = ovs_packet_cmd_execute
962         }
963 };
964
965 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
966 {
967         struct flow_table *table;
968         int i;
969
970         table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
971         stats->n_flows = ovs_flow_tbl_count(table);
972
973         stats->n_hit = stats->n_missed = stats->n_lost = 0;
974         for_each_possible_cpu(i) {
975                 const struct dp_stats_percpu *percpu_stats;
976                 struct dp_stats_percpu local_stats;
977                 unsigned int start;
978
979                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
980
981                 do {
982                         start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
983                         local_stats = *percpu_stats;
984                 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
985
986                 stats->n_hit += local_stats.n_hit;
987                 stats->n_missed += local_stats.n_missed;
988                 stats->n_lost += local_stats.n_lost;
989         }
990 }
991
992 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
993         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
994         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
995         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
996 };
997
998 static struct genl_family dp_flow_genl_family = {
999         .id = GENL_ID_GENERATE,
1000         .hdrsize = sizeof(struct ovs_header),
1001         .name = OVS_FLOW_FAMILY,
1002         .version = OVS_FLOW_VERSION,
1003         .maxattr = OVS_FLOW_ATTR_MAX,
1004          SET_NETNSOK
1005          SET_PARALLEL_OPS
1006 };
1007
1008 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
1009         .name = OVS_FLOW_MCGROUP
1010 };
1011
1012 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1013 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1014 {
1015         const struct nlattr *a;
1016         struct nlattr *start;
1017         int err = 0, rem;
1018
1019         start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1020         if (!start)
1021                 return -EMSGSIZE;
1022
1023         nla_for_each_nested(a, attr, rem) {
1024                 int type = nla_type(a);
1025                 struct nlattr *st_sample;
1026
1027                 switch (type) {
1028                 case OVS_SAMPLE_ATTR_PROBABILITY:
1029                         if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1030                                 return -EMSGSIZE;
1031                         break;
1032                 case OVS_SAMPLE_ATTR_ACTIONS:
1033                         st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1034                         if (!st_sample)
1035                                 return -EMSGSIZE;
1036                         err = actions_to_attr(nla_data(a), nla_len(a), skb);
1037                         if (err)
1038                                 return err;
1039                         nla_nest_end(skb, st_sample);
1040                         break;
1041                 }
1042         }
1043
1044         nla_nest_end(skb, start);
1045         return err;
1046 }
1047
1048 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1049 {
1050         const struct nlattr *ovs_key = nla_data(a);
1051         int key_type = nla_type(ovs_key);
1052         struct nlattr *start;
1053         int err;
1054
1055         switch (key_type) {
1056         case OVS_KEY_ATTR_IPV4_TUNNEL:
1057                 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1058                 if (!start)
1059                         return -EMSGSIZE;
1060
1061                 err = ovs_ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1062                                              nla_data(ovs_key));
1063                 if (err)
1064                         return err;
1065                 nla_nest_end(skb, start);
1066                 break;
1067         default:
1068                 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1069                         return -EMSGSIZE;
1070                 break;
1071         }
1072
1073         return 0;
1074 }
1075
1076 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1077 {
1078         const struct nlattr *a;
1079         int rem, err;
1080
1081         nla_for_each_attr(a, attr, len, rem) {
1082                 int type = nla_type(a);
1083
1084                 switch (type) {
1085                 case OVS_ACTION_ATTR_SET:
1086                         err = set_action_to_attr(a, skb);
1087                         if (err)
1088                                 return err;
1089                         break;
1090
1091                 case OVS_ACTION_ATTR_SAMPLE:
1092                         err = sample_action_to_attr(a, skb);
1093                         if (err)
1094                                 return err;
1095                         break;
1096                 default:
1097                         if (nla_put(skb, type, nla_len(a), nla_data(a)))
1098                                 return -EMSGSIZE;
1099                         break;
1100                 }
1101         }
1102
1103         return 0;
1104 }
1105
1106 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1107 {
1108         return NLMSG_ALIGN(sizeof(struct ovs_header))
1109                 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
1110                 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
1111                 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
1112                 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1113                 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1114                 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
1115 }
1116
1117 /* Called with ovs_mutex. */
1118 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1119                                   struct sk_buff *skb, u32 portid,
1120                                   u32 seq, u32 flags, u8 cmd)
1121 {
1122         const int skb_orig_len = skb->len;
1123         struct nlattr *start;
1124         struct ovs_flow_stats stats;
1125         struct ovs_header *ovs_header;
1126         struct nlattr *nla;
1127         unsigned long used;
1128         u8 tcp_flags;
1129         int err;
1130
1131         ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
1132         if (!ovs_header)
1133                 return -EMSGSIZE;
1134
1135         ovs_header->dp_ifindex = get_dpifindex(dp);
1136
1137         /* Fill flow key. */
1138         nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1139         if (!nla)
1140                 goto nla_put_failure;
1141
1142         err = ovs_flow_to_nlattrs(&flow->unmasked_key,
1143                         &flow->unmasked_key, skb);
1144         if (err)
1145                 goto error;
1146         nla_nest_end(skb, nla);
1147
1148         nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
1149         if (!nla)
1150                 goto nla_put_failure;
1151
1152         err = ovs_flow_to_nlattrs(&flow->key, &flow->mask->key, skb);
1153         if (err)
1154                 goto error;
1155
1156         nla_nest_end(skb, nla);
1157
1158         spin_lock_bh(&flow->lock);
1159         used = flow->used;
1160         stats.n_packets = flow->packet_count;
1161         stats.n_bytes = flow->byte_count;
1162         tcp_flags = flow->tcp_flags;
1163         spin_unlock_bh(&flow->lock);
1164
1165         if (used &&
1166             nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1167                 goto nla_put_failure;
1168
1169         if (stats.n_packets &&
1170             nla_put(skb, OVS_FLOW_ATTR_STATS,
1171                     sizeof(struct ovs_flow_stats), &stats))
1172                 goto nla_put_failure;
1173
1174         if (tcp_flags &&
1175             nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1176                 goto nla_put_failure;
1177
1178         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1179          * this is the first flow to be dumped into 'skb'.  This is unusual for
1180          * Netlink but individual action lists can be longer than
1181          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1182          * The userspace caller can always fetch the actions separately if it
1183          * really wants them.  (Most userspace callers in fact don't care.)
1184          *
1185          * This can only fail for dump operations because the skb is always
1186          * properly sized for single flows.
1187          */
1188         start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1189         if (start) {
1190                 const struct sw_flow_actions *sf_acts;
1191
1192                 sf_acts = rcu_dereference_check(flow->sf_acts,
1193                                                 lockdep_ovsl_is_held());
1194
1195                 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1196                 if (!err)
1197                         nla_nest_end(skb, start);
1198                 else {
1199                         if (skb_orig_len)
1200                                 goto error;
1201
1202                         nla_nest_cancel(skb, start);
1203                 }
1204         } else if (skb_orig_len)
1205                 goto nla_put_failure;
1206
1207         return genlmsg_end(skb, ovs_header);
1208
1209 nla_put_failure:
1210         err = -EMSGSIZE;
1211 error:
1212         genlmsg_cancel(skb, ovs_header);
1213         return err;
1214 }
1215
1216 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1217 {
1218         const struct sw_flow_actions *sf_acts;
1219
1220         sf_acts = ovsl_dereference(flow->sf_acts);
1221
1222         return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
1223 }
1224
1225 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1226                                                struct datapath *dp,
1227                                                u32 portid, u32 seq, u8 cmd)
1228 {
1229         struct sk_buff *skb;
1230         int retval;
1231
1232         skb = ovs_flow_cmd_alloc_info(flow);
1233         if (!skb)
1234                 return ERR_PTR(-ENOMEM);
1235
1236         retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
1237         BUG_ON(retval < 0);
1238         return skb;
1239 }
1240
1241 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1242 {
1243         struct nlattr **a = info->attrs;
1244         struct ovs_header *ovs_header = info->userhdr;
1245         struct sw_flow_key key, masked_key;
1246         struct sw_flow *flow = NULL;
1247         struct sw_flow_mask mask;
1248         struct sk_buff *reply;
1249         struct datapath *dp;
1250         struct flow_table *table;
1251         struct sw_flow_actions *acts = NULL;
1252         struct sw_flow_match match;
1253         int error;
1254
1255         /* Extract key. */
1256         error = -EINVAL;
1257         if (!a[OVS_FLOW_ATTR_KEY])
1258                 goto error;
1259
1260         ovs_match_init(&match, &key, &mask);
1261         error = ovs_match_from_nlattrs(&match,
1262                         a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1263         if (error)
1264                 goto error;
1265
1266         /* Validate actions. */
1267         if (a[OVS_FLOW_ATTR_ACTIONS]) {
1268                 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1269                 error = PTR_ERR(acts);
1270                 if (IS_ERR(acts))
1271                         goto error;
1272
1273                 ovs_flow_key_mask(&masked_key, &key, &mask);
1274                 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
1275                                                   &masked_key, 0, &acts);
1276                 if (error) {
1277                         OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
1278                         goto err_kfree;
1279                 }
1280         } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1281                 error = -EINVAL;
1282                 goto error;
1283         }
1284
1285         ovs_lock();
1286         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1287         error = -ENODEV;
1288         if (!dp)
1289                 goto err_unlock_ovs;
1290
1291         table = ovsl_dereference(dp->table);
1292
1293         /* Check if this is a duplicate flow */
1294         flow = ovs_flow_lookup(table, &key);
1295         if (!flow) {
1296                 struct sw_flow_mask *mask_p;
1297                 /* Bail out if we're not allowed to create a new flow. */
1298                 error = -ENOENT;
1299                 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1300                         goto err_unlock_ovs;
1301
1302                 /* Expand table, if necessary, to make room. */
1303                 if (ovs_flow_tbl_need_to_expand(table)) {
1304                         struct flow_table *new_table;
1305
1306                         new_table = ovs_flow_tbl_expand(table);
1307                         if (!IS_ERR(new_table)) {
1308                                 rcu_assign_pointer(dp->table, new_table);
1309                                 ovs_flow_tbl_destroy(table, true);
1310                                 table = ovsl_dereference(dp->table);
1311                         }
1312                 }
1313
1314                 /* Allocate flow. */
1315                 flow = ovs_flow_alloc();
1316                 if (IS_ERR(flow)) {
1317                         error = PTR_ERR(flow);
1318                         goto err_unlock_ovs;
1319                 }
1320                 clear_stats(flow);
1321
1322                 flow->key = masked_key;
1323                 flow->unmasked_key = key;
1324
1325                 /* Make sure mask is unique in the system */
1326                 mask_p = ovs_sw_flow_mask_find(table, &mask);
1327                 if (!mask_p) {
1328                         /* Allocate a new mask if none exsits. */
1329                         mask_p = ovs_sw_flow_mask_alloc();
1330                         if (!mask_p)
1331                                 goto err_flow_free;
1332                         mask_p->key = mask.key;
1333                         mask_p->range = mask.range;
1334                         ovs_sw_flow_mask_insert(table, mask_p);
1335                 }
1336
1337                 ovs_sw_flow_mask_add_ref(mask_p);
1338                 flow->mask = mask_p;
1339                 rcu_assign_pointer(flow->sf_acts, acts);
1340
1341                 /* Put flow in bucket. */
1342                 ovs_flow_insert(table, flow);
1343
1344                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1345                                                 info->snd_seq, OVS_FLOW_CMD_NEW);
1346         } else {
1347                 /* We found a matching flow. */
1348                 struct sw_flow_actions *old_acts;
1349
1350                 /* Bail out if we're not allowed to modify an existing flow.
1351                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1352                  * because Generic Netlink treats the latter as a dump
1353                  * request.  We also accept NLM_F_EXCL in case that bug ever
1354                  * gets fixed.
1355                  */
1356                 error = -EEXIST;
1357                 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1358                     info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1359                         goto err_unlock_ovs;
1360
1361                 /* The unmasked key has to be the same for flow updates. */
1362                 error = -EINVAL;
1363                 if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) {
1364                         OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
1365                         goto err_unlock_ovs;
1366                 }
1367
1368                 /* Update actions. */
1369                 old_acts = ovsl_dereference(flow->sf_acts);
1370                 rcu_assign_pointer(flow->sf_acts, acts);
1371                 ovs_flow_deferred_free_acts(old_acts);
1372
1373                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1374                                                info->snd_seq, OVS_FLOW_CMD_NEW);
1375
1376                 /* Clear stats. */
1377                 if (a[OVS_FLOW_ATTR_CLEAR]) {
1378                         spin_lock_bh(&flow->lock);
1379                         clear_stats(flow);
1380                         spin_unlock_bh(&flow->lock);
1381                 }
1382         }
1383         ovs_unlock();
1384
1385         if (!IS_ERR(reply))
1386                 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1387         else
1388                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1389                                 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1390         return 0;
1391
1392 err_flow_free:
1393         ovs_flow_free(flow, false);
1394 err_unlock_ovs:
1395         ovs_unlock();
1396 err_kfree:
1397         kfree(acts);
1398 error:
1399         return error;
1400 }
1401
1402 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1403 {
1404         struct nlattr **a = info->attrs;
1405         struct ovs_header *ovs_header = info->userhdr;
1406         struct sw_flow_key key;
1407         struct sk_buff *reply;
1408         struct sw_flow *flow;
1409         struct datapath *dp;
1410         struct flow_table *table;
1411         struct sw_flow_match match;
1412         int err;
1413
1414         if (!a[OVS_FLOW_ATTR_KEY]) {
1415                 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1416                 return -EINVAL;
1417         }
1418
1419         ovs_match_init(&match, &key, NULL);
1420         err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1421         if (err)
1422                 return err;
1423
1424         ovs_lock();
1425         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1426         if (!dp) {
1427                 err = -ENODEV;
1428                 goto unlock;
1429         }
1430
1431         table = ovsl_dereference(dp->table);
1432         flow = ovs_flow_lookup_unmasked_key(table, &match);
1433         if (!flow) {
1434                 err = -ENOENT;
1435                 goto unlock;
1436         }
1437
1438         reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1439                                         info->snd_seq, OVS_FLOW_CMD_NEW);
1440         if (IS_ERR(reply)) {
1441                 err = PTR_ERR(reply);
1442                 goto unlock;
1443         }
1444
1445         ovs_unlock();
1446         return genlmsg_reply(reply, info);
1447 unlock:
1448         ovs_unlock();
1449         return err;
1450 }
1451
1452 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1453 {
1454         struct nlattr **a = info->attrs;
1455         struct ovs_header *ovs_header = info->userhdr;
1456         struct sw_flow_key key;
1457         struct sk_buff *reply;
1458         struct sw_flow *flow;
1459         struct datapath *dp;
1460         struct flow_table *table;
1461         struct sw_flow_match match;
1462         int err;
1463
1464         ovs_lock();
1465         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1466         if (!dp) {
1467                 err = -ENODEV;
1468                 goto unlock;
1469         }
1470
1471         if (!a[OVS_FLOW_ATTR_KEY]) {
1472                 err = flush_flows(dp);
1473                 goto unlock;
1474         }
1475
1476         ovs_match_init(&match, &key, NULL);
1477         err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1478         if (err)
1479                 goto unlock;
1480
1481         table = ovsl_dereference(dp->table);
1482         flow = ovs_flow_lookup_unmasked_key(table, &match);
1483         if (!flow) {
1484                 err = -ENOENT;
1485                 goto unlock;
1486         }
1487
1488         reply = ovs_flow_cmd_alloc_info(flow);
1489         if (!reply) {
1490                 err = -ENOMEM;
1491                 goto unlock;
1492         }
1493
1494         ovs_flow_remove(table, flow);
1495
1496         err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1497                                      info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1498         BUG_ON(err < 0);
1499
1500         ovs_flow_free(flow, true);
1501         ovs_unlock();
1502
1503         ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1504         return 0;
1505 unlock:
1506         ovs_unlock();
1507         return err;
1508 }
1509
1510 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1511 {
1512         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1513         struct datapath *dp;
1514         struct flow_table *table;
1515
1516         rcu_read_lock();
1517         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1518         if (!dp) {
1519                 rcu_read_unlock();
1520                 return -ENODEV;
1521         }
1522
1523         table = rcu_dereference(dp->table);
1524         for (;;) {
1525                 struct sw_flow *flow;
1526                 u32 bucket, obj;
1527
1528                 bucket = cb->args[0];
1529                 obj = cb->args[1];
1530                 flow = ovs_flow_dump_next(table, &bucket, &obj);
1531                 if (!flow)
1532                         break;
1533
1534                 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1535                                            NETLINK_CB(cb->skb).portid,
1536                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1537                                            OVS_FLOW_CMD_NEW) < 0)
1538                         break;
1539
1540                 cb->args[0] = bucket;
1541                 cb->args[1] = obj;
1542         }
1543         rcu_read_unlock();
1544         return skb->len;
1545 }
1546
1547 static struct genl_ops dp_flow_genl_ops[] = {
1548         { .cmd = OVS_FLOW_CMD_NEW,
1549           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1550           .policy = flow_policy,
1551           .doit = ovs_flow_cmd_new_or_set
1552         },
1553         { .cmd = OVS_FLOW_CMD_DEL,
1554           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1555           .policy = flow_policy,
1556           .doit = ovs_flow_cmd_del
1557         },
1558         { .cmd = OVS_FLOW_CMD_GET,
1559           .flags = 0,               /* OK for unprivileged users. */
1560           .policy = flow_policy,
1561           .doit = ovs_flow_cmd_get,
1562           .dumpit = ovs_flow_cmd_dump
1563         },
1564         { .cmd = OVS_FLOW_CMD_SET,
1565           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1566           .policy = flow_policy,
1567           .doit = ovs_flow_cmd_new_or_set,
1568         },
1569 };
1570
1571 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1572 #ifdef HAVE_NLA_NUL_STRING
1573         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1574 #endif
1575         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1576 };
1577
1578 static struct genl_family dp_datapath_genl_family = {
1579         .id = GENL_ID_GENERATE,
1580         .hdrsize = sizeof(struct ovs_header),
1581         .name = OVS_DATAPATH_FAMILY,
1582         .version = OVS_DATAPATH_VERSION,
1583         .maxattr = OVS_DP_ATTR_MAX,
1584          SET_NETNSOK
1585          SET_PARALLEL_OPS
1586 };
1587
1588 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1589         .name = OVS_DATAPATH_MCGROUP
1590 };
1591
1592 static size_t ovs_dp_cmd_msg_size(void)
1593 {
1594         size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1595
1596         msgsize += nla_total_size(IFNAMSIZ);
1597         msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1598
1599         return msgsize;
1600 }
1601
1602 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1603                                 u32 portid, u32 seq, u32 flags, u8 cmd)
1604 {
1605         struct ovs_header *ovs_header;
1606         struct ovs_dp_stats dp_stats;
1607         int err;
1608
1609         ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1610                                    flags, cmd);
1611         if (!ovs_header)
1612                 goto error;
1613
1614         ovs_header->dp_ifindex = get_dpifindex(dp);
1615
1616         rcu_read_lock();
1617         err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1618         rcu_read_unlock();
1619         if (err)
1620                 goto nla_put_failure;
1621
1622         get_dp_stats(dp, &dp_stats);
1623         if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1624                 goto nla_put_failure;
1625
1626         return genlmsg_end(skb, ovs_header);
1627
1628 nla_put_failure:
1629         genlmsg_cancel(skb, ovs_header);
1630 error:
1631         return -EMSGSIZE;
1632 }
1633
1634 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1635                                              u32 seq, u8 cmd)
1636 {
1637         struct sk_buff *skb;
1638         int retval;
1639
1640         skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1641         if (!skb)
1642                 return ERR_PTR(-ENOMEM);
1643
1644         retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1645         if (retval < 0) {
1646                 kfree_skb(skb);
1647                 return ERR_PTR(retval);
1648         }
1649         return skb;
1650 }
1651
1652 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1653 {
1654         return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1655 }
1656
1657 /* Called with ovs_mutex. */
1658 static struct datapath *lookup_datapath(struct net *net,
1659                                         struct ovs_header *ovs_header,
1660                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1661 {
1662         struct datapath *dp;
1663
1664         if (!a[OVS_DP_ATTR_NAME])
1665                 dp = get_dp(net, ovs_header->dp_ifindex);
1666         else {
1667                 struct vport *vport;
1668
1669                 rcu_read_lock();
1670                 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1671                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1672                 rcu_read_unlock();
1673         }
1674         return dp ? dp : ERR_PTR(-ENODEV);
1675 }
1676
1677 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1678 {
1679         struct nlattr **a = info->attrs;
1680         struct vport_parms parms;
1681         struct sk_buff *reply;
1682         struct datapath *dp;
1683         struct vport *vport;
1684         struct ovs_net *ovs_net;
1685         int err, i;
1686
1687         err = -EINVAL;
1688         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1689                 goto err;
1690
1691         err = ovs_dp_cmd_validate(a);
1692         if (err)
1693                 goto err;
1694
1695         ovs_lock();
1696
1697         err = -ENOMEM;
1698         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1699         if (dp == NULL)
1700                 goto err_unlock_ovs;
1701
1702         ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1703
1704         /* Allocate table. */
1705         err = -ENOMEM;
1706         rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1707         if (!dp->table)
1708                 goto err_free_dp;
1709
1710         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1711         if (!dp->stats_percpu) {
1712                 err = -ENOMEM;
1713                 goto err_destroy_table;
1714         }
1715
1716         dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1717                             GFP_KERNEL);
1718         if (!dp->ports) {
1719                 err = -ENOMEM;
1720                 goto err_destroy_percpu;
1721         }
1722
1723         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1724                 INIT_HLIST_HEAD(&dp->ports[i]);
1725
1726         /* Set up our datapath device. */
1727         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1728         parms.type = OVS_VPORT_TYPE_INTERNAL;
1729         parms.options = NULL;
1730         parms.dp = dp;
1731         parms.port_no = OVSP_LOCAL;
1732         parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1733
1734         vport = new_vport(&parms);
1735         if (IS_ERR(vport)) {
1736                 err = PTR_ERR(vport);
1737                 if (err == -EBUSY)
1738                         err = -EEXIST;
1739
1740                 goto err_destroy_ports_array;
1741         }
1742
1743         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1744                                       info->snd_seq, OVS_DP_CMD_NEW);
1745         err = PTR_ERR(reply);
1746         if (IS_ERR(reply))
1747                 goto err_destroy_local_port;
1748
1749         ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1750         list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1751
1752         ovs_unlock();
1753
1754         ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1755         return 0;
1756
1757 err_destroy_local_port:
1758         ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1759 err_destroy_ports_array:
1760         kfree(dp->ports);
1761 err_destroy_percpu:
1762         free_percpu(dp->stats_percpu);
1763 err_destroy_table:
1764         ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
1765 err_free_dp:
1766         release_net(ovs_dp_get_net(dp));
1767         kfree(dp);
1768 err_unlock_ovs:
1769         ovs_unlock();
1770 err:
1771         return err;
1772 }
1773
1774 /* Called with ovs_mutex. */
1775 static void __dp_destroy(struct datapath *dp)
1776 {
1777         int i;
1778
1779         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1780                 struct vport *vport;
1781                 struct hlist_node *n;
1782
1783                 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1784                         if (vport->port_no != OVSP_LOCAL)
1785                                 ovs_dp_detach_port(vport);
1786         }
1787
1788         list_del_rcu(&dp->list_node);
1789
1790         /* OVSP_LOCAL is datapath internal port. We need to make sure that
1791          * all port in datapath are destroyed first before freeing datapath.
1792          */
1793         ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1794
1795         call_rcu(&dp->rcu, destroy_dp_rcu);
1796 }
1797
1798 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1799 {
1800         struct sk_buff *reply;
1801         struct datapath *dp;
1802         int err;
1803
1804         err = ovs_dp_cmd_validate(info->attrs);
1805         if (err)
1806                 return err;
1807
1808         ovs_lock();
1809         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1810         err = PTR_ERR(dp);
1811         if (IS_ERR(dp))
1812                 goto unlock;
1813
1814         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1815                                       info->snd_seq, OVS_DP_CMD_DEL);
1816         err = PTR_ERR(reply);
1817         if (IS_ERR(reply))
1818                 goto unlock;
1819
1820         __dp_destroy(dp);
1821         ovs_unlock();
1822
1823         ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1824
1825         return 0;
1826 unlock:
1827         ovs_unlock();
1828         return err;
1829 }
1830
1831 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1832 {
1833         struct sk_buff *reply;
1834         struct datapath *dp;
1835         int err;
1836
1837         err = ovs_dp_cmd_validate(info->attrs);
1838         if (err)
1839                 return err;
1840
1841         ovs_lock();
1842         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1843         err = PTR_ERR(dp);
1844         if (IS_ERR(dp))
1845                 goto unlock;
1846
1847         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1848                                       info->snd_seq, OVS_DP_CMD_NEW);
1849         if (IS_ERR(reply)) {
1850                 err = PTR_ERR(reply);
1851                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1852                                 ovs_dp_datapath_multicast_group.id, err);
1853                 err = 0;
1854                 goto unlock;
1855         }
1856
1857         ovs_unlock();
1858         ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1859
1860         return 0;
1861 unlock:
1862         ovs_unlock();
1863         return err;
1864 }
1865
1866 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1867 {
1868         struct sk_buff *reply;
1869         struct datapath *dp;
1870         int err;
1871
1872         err = ovs_dp_cmd_validate(info->attrs);
1873         if (err)
1874                 return err;
1875
1876         ovs_lock();
1877         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1878         if (IS_ERR(dp)) {
1879                 err = PTR_ERR(dp);
1880                 goto unlock;
1881         }
1882
1883         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1884                                       info->snd_seq, OVS_DP_CMD_NEW);
1885         if (IS_ERR(reply)) {
1886                 err = PTR_ERR(reply);
1887                 goto unlock;
1888         }
1889
1890         ovs_unlock();
1891         return genlmsg_reply(reply, info);
1892
1893 unlock:
1894         ovs_unlock();
1895         return err;
1896 }
1897
1898 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1899 {
1900         struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1901         struct datapath *dp;
1902         int skip = cb->args[0];
1903         int i = 0;
1904
1905         rcu_read_lock();
1906         list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1907                 if (i >= skip &&
1908                     ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1909                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1910                                          OVS_DP_CMD_NEW) < 0)
1911                         break;
1912                 i++;
1913         }
1914         rcu_read_unlock();
1915
1916         cb->args[0] = i;
1917
1918         return skb->len;
1919 }
1920
1921 static struct genl_ops dp_datapath_genl_ops[] = {
1922         { .cmd = OVS_DP_CMD_NEW,
1923           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1924           .policy = datapath_policy,
1925           .doit = ovs_dp_cmd_new
1926         },
1927         { .cmd = OVS_DP_CMD_DEL,
1928           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1929           .policy = datapath_policy,
1930           .doit = ovs_dp_cmd_del
1931         },
1932         { .cmd = OVS_DP_CMD_GET,
1933           .flags = 0,               /* OK for unprivileged users. */
1934           .policy = datapath_policy,
1935           .doit = ovs_dp_cmd_get,
1936           .dumpit = ovs_dp_cmd_dump
1937         },
1938         { .cmd = OVS_DP_CMD_SET,
1939           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1940           .policy = datapath_policy,
1941           .doit = ovs_dp_cmd_set,
1942         },
1943 };
1944
1945 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1946 #ifdef HAVE_NLA_NUL_STRING
1947         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1948         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1949 #else
1950         [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1951 #endif
1952         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1953         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1954         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1955         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1956 };
1957
1958 static struct genl_family dp_vport_genl_family = {
1959         .id = GENL_ID_GENERATE,
1960         .hdrsize = sizeof(struct ovs_header),
1961         .name = OVS_VPORT_FAMILY,
1962         .version = OVS_VPORT_VERSION,
1963         .maxattr = OVS_VPORT_ATTR_MAX,
1964          SET_NETNSOK
1965          SET_PARALLEL_OPS
1966 };
1967
1968 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1969         .name = OVS_VPORT_MCGROUP
1970 };
1971
1972 /* Called with ovs_mutex or RCU read lock. */
1973 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1974                                    u32 portid, u32 seq, u32 flags, u8 cmd)
1975 {
1976         struct ovs_header *ovs_header;
1977         struct ovs_vport_stats vport_stats;
1978         int err;
1979
1980         ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1981                                  flags, cmd);
1982         if (!ovs_header)
1983                 return -EMSGSIZE;
1984
1985         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1986
1987         if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1988             nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1989             nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1990             nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1991                 goto nla_put_failure;
1992
1993         ovs_vport_get_stats(vport, &vport_stats);
1994         if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1995                     &vport_stats))
1996                 goto nla_put_failure;
1997
1998         err = ovs_vport_get_options(vport, skb);
1999         if (err == -EMSGSIZE)
2000                 goto error;
2001
2002         return genlmsg_end(skb, ovs_header);
2003
2004 nla_put_failure:
2005         err = -EMSGSIZE;
2006 error:
2007         genlmsg_cancel(skb, ovs_header);
2008         return err;
2009 }
2010
2011 /* Called with ovs_mutex or RCU read lock. */
2012 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
2013                                          u32 seq, u8 cmd)
2014 {
2015         struct sk_buff *skb;
2016         int retval;
2017
2018         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2019         if (!skb)
2020                 return ERR_PTR(-ENOMEM);
2021
2022         retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
2023         BUG_ON(retval < 0);
2024
2025         return skb;
2026 }
2027
2028 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2029 {
2030         return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
2031 }
2032
2033 /* Called with ovs_mutex or RCU read lock. */
2034 static struct vport *lookup_vport(struct net *net,
2035                                   struct ovs_header *ovs_header,
2036                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2037 {
2038         struct datapath *dp;
2039         struct vport *vport;
2040
2041         if (a[OVS_VPORT_ATTR_NAME]) {
2042                 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2043                 if (!vport)
2044                         return ERR_PTR(-ENODEV);
2045                 if (ovs_header->dp_ifindex &&
2046                     ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2047                         return ERR_PTR(-ENODEV);
2048                 return vport;
2049         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2050                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2051
2052                 if (port_no >= DP_MAX_PORTS)
2053                         return ERR_PTR(-EFBIG);
2054
2055                 dp = get_dp(net, ovs_header->dp_ifindex);
2056                 if (!dp)
2057                         return ERR_PTR(-ENODEV);
2058
2059                 vport = ovs_vport_ovsl_rcu(dp, port_no);
2060                 if (!vport)
2061                         return ERR_PTR(-ENODEV);
2062                 return vport;
2063         } else
2064                 return ERR_PTR(-EINVAL);
2065 }
2066
2067 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2068 {
2069         struct nlattr **a = info->attrs;
2070         struct ovs_header *ovs_header = info->userhdr;
2071         struct vport_parms parms;
2072         struct sk_buff *reply;
2073         struct vport *vport;
2074         struct datapath *dp;
2075         u32 port_no;
2076         int err;
2077
2078         err = -EINVAL;
2079         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2080             !a[OVS_VPORT_ATTR_UPCALL_PID])
2081                 goto exit;
2082
2083         err = ovs_vport_cmd_validate(a);
2084         if (err)
2085                 goto exit;
2086
2087         ovs_lock();
2088         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2089         err = -ENODEV;
2090         if (!dp)
2091                 goto exit_unlock;
2092
2093         if (a[OVS_VPORT_ATTR_PORT_NO]) {
2094                 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2095
2096                 err = -EFBIG;
2097                 if (port_no >= DP_MAX_PORTS)
2098                         goto exit_unlock;
2099
2100                 vport = ovs_vport_ovsl(dp, port_no);
2101                 err = -EBUSY;
2102                 if (vport)
2103                         goto exit_unlock;
2104         } else {
2105                 for (port_no = 1; ; port_no++) {
2106                         if (port_no >= DP_MAX_PORTS) {
2107                                 err = -EFBIG;
2108                                 goto exit_unlock;
2109                         }
2110                         vport = ovs_vport_ovsl(dp, port_no);
2111                         if (!vport)
2112                                 break;
2113                 }
2114         }
2115
2116         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2117         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2118         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2119         parms.dp = dp;
2120         parms.port_no = port_no;
2121         parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2122
2123         vport = new_vport(&parms);
2124         err = PTR_ERR(vport);
2125         if (IS_ERR(vport))
2126                 goto exit_unlock;
2127
2128         err = 0;
2129         if (a[OVS_VPORT_ATTR_STATS])
2130                 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2131
2132         reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
2133                                          OVS_VPORT_CMD_NEW);
2134         if (IS_ERR(reply)) {
2135                 err = PTR_ERR(reply);
2136                 ovs_dp_detach_port(vport);
2137                 goto exit_unlock;
2138         }
2139
2140         ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2141
2142 exit_unlock:
2143         ovs_unlock();
2144 exit:
2145         return err;
2146 }
2147
2148 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2149 {
2150         struct nlattr **a = info->attrs;
2151         struct sk_buff *reply;
2152         struct vport *vport;
2153         int err;
2154
2155         err = ovs_vport_cmd_validate(a);
2156         if (err)
2157                 goto exit;
2158
2159         ovs_lock();
2160         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2161         err = PTR_ERR(vport);
2162         if (IS_ERR(vport))
2163                 goto exit_unlock;
2164
2165         if (a[OVS_VPORT_ATTR_TYPE] &&
2166             nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2167                 err = -EINVAL;
2168                 goto exit_unlock;
2169         }
2170
2171         reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2172         if (!reply) {
2173                 err = -ENOMEM;
2174                 goto exit_unlock;
2175         }
2176
2177         if (a[OVS_VPORT_ATTR_OPTIONS]) {
2178                 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2179                 if (err)
2180                         goto exit_free;
2181         }
2182
2183         if (a[OVS_VPORT_ATTR_STATS])
2184                 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2185
2186         if (a[OVS_VPORT_ATTR_UPCALL_PID])
2187                 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2188
2189         err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2190                                       info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2191         BUG_ON(err < 0);
2192
2193         ovs_unlock();
2194         ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2195         return 0;
2196
2197 exit_free:
2198         kfree_skb(reply);
2199 exit_unlock:
2200         ovs_unlock();
2201 exit:
2202         return err;
2203 }
2204
2205 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2206 {
2207         struct nlattr **a = info->attrs;
2208         struct sk_buff *reply;
2209         struct vport *vport;
2210         int err;
2211
2212         err = ovs_vport_cmd_validate(a);
2213         if (err)
2214                 goto exit;
2215
2216         ovs_lock();
2217         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2218         err = PTR_ERR(vport);
2219         if (IS_ERR(vport))
2220                 goto exit_unlock;
2221
2222         if (vport->port_no == OVSP_LOCAL) {
2223                 err = -EINVAL;
2224                 goto exit_unlock;
2225         }
2226
2227         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2228                                          info->snd_seq, OVS_VPORT_CMD_DEL);
2229         err = PTR_ERR(reply);
2230         if (IS_ERR(reply))
2231                 goto exit_unlock;
2232
2233         err = 0;
2234         ovs_dp_detach_port(vport);
2235
2236         ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2237
2238 exit_unlock:
2239         ovs_unlock();
2240 exit:
2241         return err;
2242 }
2243
2244 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2245 {
2246         struct nlattr **a = info->attrs;
2247         struct ovs_header *ovs_header = info->userhdr;
2248         struct sk_buff *reply;
2249         struct vport *vport;
2250         int err;
2251
2252         err = ovs_vport_cmd_validate(a);
2253         if (err)
2254                 goto exit;
2255
2256         rcu_read_lock();
2257         vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2258         err = PTR_ERR(vport);
2259         if (IS_ERR(vport))
2260                 goto exit_unlock;
2261
2262         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2263                                          info->snd_seq, OVS_VPORT_CMD_NEW);
2264         err = PTR_ERR(reply);
2265         if (IS_ERR(reply))
2266                 goto exit_unlock;
2267
2268         rcu_read_unlock();
2269
2270         return genlmsg_reply(reply, info);
2271
2272 exit_unlock:
2273         rcu_read_unlock();
2274 exit:
2275         return err;
2276 }
2277
2278 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2279 {
2280         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2281         struct datapath *dp;
2282         int bucket = cb->args[0], skip = cb->args[1];
2283         int i, j = 0;
2284
2285         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2286         if (!dp)
2287                 return -ENODEV;
2288
2289         rcu_read_lock();
2290         for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2291                 struct vport *vport;
2292
2293                 j = 0;
2294                 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2295                         if (j >= skip &&
2296                             ovs_vport_cmd_fill_info(vport, skb,
2297                                                     NETLINK_CB(cb->skb).portid,
2298                                                     cb->nlh->nlmsg_seq,
2299                                                     NLM_F_MULTI,
2300                                                     OVS_VPORT_CMD_NEW) < 0)
2301                                 goto out;
2302
2303                         j++;
2304                 }
2305                 skip = 0;
2306         }
2307 out:
2308         rcu_read_unlock();
2309
2310         cb->args[0] = i;
2311         cb->args[1] = j;
2312
2313         return skb->len;
2314 }
2315
2316 static struct genl_ops dp_vport_genl_ops[] = {
2317         { .cmd = OVS_VPORT_CMD_NEW,
2318           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2319           .policy = vport_policy,
2320           .doit = ovs_vport_cmd_new
2321         },
2322         { .cmd = OVS_VPORT_CMD_DEL,
2323           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2324           .policy = vport_policy,
2325           .doit = ovs_vport_cmd_del
2326         },
2327         { .cmd = OVS_VPORT_CMD_GET,
2328           .flags = 0,               /* OK for unprivileged users. */
2329           .policy = vport_policy,
2330           .doit = ovs_vport_cmd_get,
2331           .dumpit = ovs_vport_cmd_dump
2332         },
2333         { .cmd = OVS_VPORT_CMD_SET,
2334           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2335           .policy = vport_policy,
2336           .doit = ovs_vport_cmd_set,
2337         },
2338 };
2339
2340 struct genl_family_and_ops {
2341         struct genl_family *family;
2342         struct genl_ops *ops;
2343         int n_ops;
2344         struct genl_multicast_group *group;
2345 };
2346
2347 static const struct genl_family_and_ops dp_genl_families[] = {
2348         { &dp_datapath_genl_family,
2349           dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2350           &ovs_dp_datapath_multicast_group },
2351         { &dp_vport_genl_family,
2352           dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2353           &ovs_dp_vport_multicast_group },
2354         { &dp_flow_genl_family,
2355           dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2356           &ovs_dp_flow_multicast_group },
2357         { &dp_packet_genl_family,
2358           dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2359           NULL },
2360 };
2361
2362 static void dp_unregister_genl(int n_families)
2363 {
2364         int i;
2365
2366         for (i = 0; i < n_families; i++)
2367                 genl_unregister_family(dp_genl_families[i].family);
2368 }
2369
2370 static int dp_register_genl(void)
2371 {
2372         int n_registered;
2373         int err;
2374         int i;
2375
2376         n_registered = 0;
2377         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2378                 const struct genl_family_and_ops *f = &dp_genl_families[i];
2379
2380                 err = genl_register_family_with_ops(f->family, f->ops,
2381                                                     f->n_ops);
2382                 if (err)
2383                         goto error;
2384                 n_registered++;
2385
2386                 if (f->group) {
2387                         err = genl_register_mc_group(f->family, f->group);
2388                         if (err)
2389                                 goto error;
2390                 }
2391         }
2392
2393         return 0;
2394
2395 error:
2396         dp_unregister_genl(n_registered);
2397         return err;
2398 }
2399
2400 static void rehash_flow_table(struct work_struct *work)
2401 {
2402         struct datapath *dp;
2403         struct net *net;
2404
2405         ovs_lock();
2406         rtnl_lock();
2407         for_each_net(net) {
2408                 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2409
2410                 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2411                         struct flow_table *old_table = ovsl_dereference(dp->table);
2412                         struct flow_table *new_table;
2413
2414                         new_table = ovs_flow_tbl_rehash(old_table);
2415                         if (!IS_ERR(new_table)) {
2416                                 rcu_assign_pointer(dp->table, new_table);
2417                                 ovs_flow_tbl_destroy(old_table, true);
2418                         }
2419                 }
2420         }
2421         rtnl_unlock();
2422         ovs_unlock();
2423         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2424 }
2425
2426 static int __net_init ovs_init_net(struct net *net)
2427 {
2428         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2429
2430         INIT_LIST_HEAD(&ovs_net->dps);
2431         INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2432         return 0;
2433 }
2434
2435 static void __net_exit ovs_exit_net(struct net *net)
2436 {
2437         struct datapath *dp, *dp_next;
2438         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2439
2440         ovs_lock();
2441         list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2442                 __dp_destroy(dp);
2443         ovs_unlock();
2444
2445         cancel_work_sync(&ovs_net->dp_notify_work);
2446 }
2447
2448 static struct pernet_operations ovs_net_ops = {
2449         .init = ovs_init_net,
2450         .exit = ovs_exit_net,
2451         .id   = &ovs_net_id,
2452         .size = sizeof(struct ovs_net),
2453 };
2454
2455 DEFINE_COMPAT_PNET_REG_FUNC(device);
2456
2457 static int __init dp_init(void)
2458 {
2459         int err;
2460
2461         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2462
2463         pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2464                 VERSION);
2465
2466         err = ovs_workqueues_init();
2467         if (err)
2468                 goto error;
2469
2470         err = ovs_flow_init();
2471         if (err)
2472                 goto error_wq;
2473
2474         err = ovs_vport_init();
2475         if (err)
2476                 goto error_flow_exit;
2477
2478         err = register_pernet_device(&ovs_net_ops);
2479         if (err)
2480                 goto error_vport_exit;
2481
2482         err = register_netdevice_notifier(&ovs_dp_device_notifier);
2483         if (err)
2484                 goto error_netns_exit;
2485
2486         err = dp_register_genl();
2487         if (err < 0)
2488                 goto error_unreg_notifier;
2489
2490         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2491
2492         return 0;
2493
2494 error_unreg_notifier:
2495         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2496 error_netns_exit:
2497         unregister_pernet_device(&ovs_net_ops);
2498 error_vport_exit:
2499         ovs_vport_exit();
2500 error_flow_exit:
2501         ovs_flow_exit();
2502 error_wq:
2503         ovs_workqueues_exit();
2504 error:
2505         return err;
2506 }
2507
2508 static void dp_cleanup(void)
2509 {
2510         cancel_delayed_work_sync(&rehash_flow_wq);
2511         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2512         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2513         unregister_pernet_device(&ovs_net_ops);
2514         rcu_barrier();
2515         ovs_vport_exit();
2516         ovs_flow_exit();
2517         ovs_workqueues_exit();
2518 }
2519
2520 module_init(dp_init);
2521 module_exit(dp_cleanup);
2522
2523 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2524 MODULE_LICENSE("GPL");
2525 MODULE_VERSION(VERSION);