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