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