datapath: Remove compat support for NLA_NUL_STRING
[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         [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
951         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
952         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
953 };
954
955 static struct genl_ops dp_packet_genl_ops[] = {
956         { .cmd = OVS_PACKET_CMD_EXECUTE,
957           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
958           .policy = packet_policy,
959           .doit = ovs_packet_cmd_execute
960         }
961 };
962
963 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
964 {
965         struct flow_table *table;
966         int i;
967
968         table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
969         stats->n_flows = ovs_flow_tbl_count(table);
970
971         stats->n_hit = stats->n_missed = stats->n_lost = 0;
972         for_each_possible_cpu(i) {
973                 const struct dp_stats_percpu *percpu_stats;
974                 struct dp_stats_percpu local_stats;
975                 unsigned int start;
976
977                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
978
979                 do {
980                         start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
981                         local_stats = *percpu_stats;
982                 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
983
984                 stats->n_hit += local_stats.n_hit;
985                 stats->n_missed += local_stats.n_missed;
986                 stats->n_lost += local_stats.n_lost;
987         }
988 }
989
990 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
991         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
992         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
993         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
994 };
995
996 static struct genl_family dp_flow_genl_family = {
997         .id = GENL_ID_GENERATE,
998         .hdrsize = sizeof(struct ovs_header),
999         .name = OVS_FLOW_FAMILY,
1000         .version = OVS_FLOW_VERSION,
1001         .maxattr = OVS_FLOW_ATTR_MAX,
1002         .netnsok = true,
1003          SET_PARALLEL_OPS
1004 };
1005
1006 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
1007         .name = OVS_FLOW_MCGROUP
1008 };
1009
1010 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1011 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1012 {
1013         const struct nlattr *a;
1014         struct nlattr *start;
1015         int err = 0, rem;
1016
1017         start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1018         if (!start)
1019                 return -EMSGSIZE;
1020
1021         nla_for_each_nested(a, attr, rem) {
1022                 int type = nla_type(a);
1023                 struct nlattr *st_sample;
1024
1025                 switch (type) {
1026                 case OVS_SAMPLE_ATTR_PROBABILITY:
1027                         if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1028                                 return -EMSGSIZE;
1029                         break;
1030                 case OVS_SAMPLE_ATTR_ACTIONS:
1031                         st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1032                         if (!st_sample)
1033                                 return -EMSGSIZE;
1034                         err = actions_to_attr(nla_data(a), nla_len(a), skb);
1035                         if (err)
1036                                 return err;
1037                         nla_nest_end(skb, st_sample);
1038                         break;
1039                 }
1040         }
1041
1042         nla_nest_end(skb, start);
1043         return err;
1044 }
1045
1046 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1047 {
1048         const struct nlattr *ovs_key = nla_data(a);
1049         int key_type = nla_type(ovs_key);
1050         struct nlattr *start;
1051         int err;
1052
1053         switch (key_type) {
1054         case OVS_KEY_ATTR_IPV4_TUNNEL:
1055                 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1056                 if (!start)
1057                         return -EMSGSIZE;
1058
1059                 err = ovs_ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1060                                              nla_data(ovs_key));
1061                 if (err)
1062                         return err;
1063                 nla_nest_end(skb, start);
1064                 break;
1065         default:
1066                 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1067                         return -EMSGSIZE;
1068                 break;
1069         }
1070
1071         return 0;
1072 }
1073
1074 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1075 {
1076         const struct nlattr *a;
1077         int rem, err;
1078
1079         nla_for_each_attr(a, attr, len, rem) {
1080                 int type = nla_type(a);
1081
1082                 switch (type) {
1083                 case OVS_ACTION_ATTR_SET:
1084                         err = set_action_to_attr(a, skb);
1085                         if (err)
1086                                 return err;
1087                         break;
1088
1089                 case OVS_ACTION_ATTR_SAMPLE:
1090                         err = sample_action_to_attr(a, skb);
1091                         if (err)
1092                                 return err;
1093                         break;
1094                 default:
1095                         if (nla_put(skb, type, nla_len(a), nla_data(a)))
1096                                 return -EMSGSIZE;
1097                         break;
1098                 }
1099         }
1100
1101         return 0;
1102 }
1103
1104 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1105 {
1106         return NLMSG_ALIGN(sizeof(struct ovs_header))
1107                 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
1108                 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
1109                 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
1110                 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1111                 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1112                 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
1113 }
1114
1115 /* Called with ovs_mutex. */
1116 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1117                                   struct sk_buff *skb, u32 portid,
1118                                   u32 seq, u32 flags, u8 cmd)
1119 {
1120         const int skb_orig_len = skb->len;
1121         struct nlattr *start;
1122         struct ovs_flow_stats stats;
1123         struct ovs_header *ovs_header;
1124         struct nlattr *nla;
1125         unsigned long used;
1126         u8 tcp_flags;
1127         int err;
1128
1129         ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
1130         if (!ovs_header)
1131                 return -EMSGSIZE;
1132
1133         ovs_header->dp_ifindex = get_dpifindex(dp);
1134
1135         /* Fill flow key. */
1136         nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1137         if (!nla)
1138                 goto nla_put_failure;
1139
1140         err = ovs_flow_to_nlattrs(&flow->unmasked_key,
1141                         &flow->unmasked_key, skb);
1142         if (err)
1143                 goto error;
1144         nla_nest_end(skb, nla);
1145
1146         nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
1147         if (!nla)
1148                 goto nla_put_failure;
1149
1150         err = ovs_flow_to_nlattrs(&flow->key, &flow->mask->key, skb);
1151         if (err)
1152                 goto error;
1153
1154         nla_nest_end(skb, nla);
1155
1156         spin_lock_bh(&flow->lock);
1157         used = flow->used;
1158         stats.n_packets = flow->packet_count;
1159         stats.n_bytes = flow->byte_count;
1160         tcp_flags = flow->tcp_flags;
1161         spin_unlock_bh(&flow->lock);
1162
1163         if (used &&
1164             nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1165                 goto nla_put_failure;
1166
1167         if (stats.n_packets &&
1168             nla_put(skb, OVS_FLOW_ATTR_STATS,
1169                     sizeof(struct ovs_flow_stats), &stats))
1170                 goto nla_put_failure;
1171
1172         if (tcp_flags &&
1173             nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1174                 goto nla_put_failure;
1175
1176         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1177          * this is the first flow to be dumped into 'skb'.  This is unusual for
1178          * Netlink but individual action lists can be longer than
1179          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1180          * The userspace caller can always fetch the actions separately if it
1181          * really wants them.  (Most userspace callers in fact don't care.)
1182          *
1183          * This can only fail for dump operations because the skb is always
1184          * properly sized for single flows.
1185          */
1186         start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1187         if (start) {
1188                 const struct sw_flow_actions *sf_acts;
1189
1190                 sf_acts = rcu_dereference_check(flow->sf_acts,
1191                                                 lockdep_ovsl_is_held());
1192
1193                 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1194                 if (!err)
1195                         nla_nest_end(skb, start);
1196                 else {
1197                         if (skb_orig_len)
1198                                 goto error;
1199
1200                         nla_nest_cancel(skb, start);
1201                 }
1202         } else if (skb_orig_len)
1203                 goto nla_put_failure;
1204
1205         return genlmsg_end(skb, ovs_header);
1206
1207 nla_put_failure:
1208         err = -EMSGSIZE;
1209 error:
1210         genlmsg_cancel(skb, ovs_header);
1211         return err;
1212 }
1213
1214 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1215 {
1216         const struct sw_flow_actions *sf_acts;
1217
1218         sf_acts = ovsl_dereference(flow->sf_acts);
1219
1220         return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
1221 }
1222
1223 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1224                                                struct datapath *dp,
1225                                                u32 portid, u32 seq, u8 cmd)
1226 {
1227         struct sk_buff *skb;
1228         int retval;
1229
1230         skb = ovs_flow_cmd_alloc_info(flow);
1231         if (!skb)
1232                 return ERR_PTR(-ENOMEM);
1233
1234         retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
1235         BUG_ON(retval < 0);
1236         return skb;
1237 }
1238
1239 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1240 {
1241         struct nlattr **a = info->attrs;
1242         struct ovs_header *ovs_header = info->userhdr;
1243         struct sw_flow_key key, masked_key;
1244         struct sw_flow *flow = NULL;
1245         struct sw_flow_mask mask;
1246         struct sk_buff *reply;
1247         struct datapath *dp;
1248         struct flow_table *table;
1249         struct sw_flow_actions *acts = NULL;
1250         struct sw_flow_match match;
1251         int error;
1252
1253         /* Extract key. */
1254         error = -EINVAL;
1255         if (!a[OVS_FLOW_ATTR_KEY])
1256                 goto error;
1257
1258         ovs_match_init(&match, &key, &mask);
1259         error = ovs_match_from_nlattrs(&match,
1260                         a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1261         if (error)
1262                 goto error;
1263
1264         /* Validate actions. */
1265         if (a[OVS_FLOW_ATTR_ACTIONS]) {
1266                 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1267                 error = PTR_ERR(acts);
1268                 if (IS_ERR(acts))
1269                         goto error;
1270
1271                 ovs_flow_key_mask(&masked_key, &key, &mask);
1272                 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
1273                                                   &masked_key, 0, &acts);
1274                 if (error) {
1275                         OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
1276                         goto err_kfree;
1277                 }
1278         } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1279                 error = -EINVAL;
1280                 goto error;
1281         }
1282
1283         ovs_lock();
1284         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1285         error = -ENODEV;
1286         if (!dp)
1287                 goto err_unlock_ovs;
1288
1289         table = ovsl_dereference(dp->table);
1290
1291         /* Check if this is a duplicate flow */
1292         flow = ovs_flow_lookup(table, &key);
1293         if (!flow) {
1294                 struct sw_flow_mask *mask_p;
1295                 /* Bail out if we're not allowed to create a new flow. */
1296                 error = -ENOENT;
1297                 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1298                         goto err_unlock_ovs;
1299
1300                 /* Expand table, if necessary, to make room. */
1301                 if (ovs_flow_tbl_need_to_expand(table)) {
1302                         struct flow_table *new_table;
1303
1304                         new_table = ovs_flow_tbl_expand(table);
1305                         if (!IS_ERR(new_table)) {
1306                                 rcu_assign_pointer(dp->table, new_table);
1307                                 ovs_flow_tbl_destroy(table, true);
1308                                 table = ovsl_dereference(dp->table);
1309                         }
1310                 }
1311
1312                 /* Allocate flow. */
1313                 flow = ovs_flow_alloc();
1314                 if (IS_ERR(flow)) {
1315                         error = PTR_ERR(flow);
1316                         goto err_unlock_ovs;
1317                 }
1318                 clear_stats(flow);
1319
1320                 flow->key = masked_key;
1321                 flow->unmasked_key = key;
1322
1323                 /* Make sure mask is unique in the system */
1324                 mask_p = ovs_sw_flow_mask_find(table, &mask);
1325                 if (!mask_p) {
1326                         /* Allocate a new mask if none exsits. */
1327                         mask_p = ovs_sw_flow_mask_alloc();
1328                         if (!mask_p)
1329                                 goto err_flow_free;
1330                         mask_p->key = mask.key;
1331                         mask_p->range = mask.range;
1332                         ovs_sw_flow_mask_insert(table, mask_p);
1333                 }
1334
1335                 ovs_sw_flow_mask_add_ref(mask_p);
1336                 flow->mask = mask_p;
1337                 rcu_assign_pointer(flow->sf_acts, acts);
1338
1339                 /* Put flow in bucket. */
1340                 ovs_flow_insert(table, flow);
1341
1342                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1343                                                 info->snd_seq, OVS_FLOW_CMD_NEW);
1344         } else {
1345                 /* We found a matching flow. */
1346                 struct sw_flow_actions *old_acts;
1347
1348                 /* Bail out if we're not allowed to modify an existing flow.
1349                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1350                  * because Generic Netlink treats the latter as a dump
1351                  * request.  We also accept NLM_F_EXCL in case that bug ever
1352                  * gets fixed.
1353                  */
1354                 error = -EEXIST;
1355                 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1356                     info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1357                         goto err_unlock_ovs;
1358
1359                 /* The unmasked key has to be the same for flow updates. */
1360                 error = -EINVAL;
1361                 if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) {
1362                         OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
1363                         goto err_unlock_ovs;
1364                 }
1365
1366                 /* Update actions. */
1367                 old_acts = ovsl_dereference(flow->sf_acts);
1368                 rcu_assign_pointer(flow->sf_acts, acts);
1369                 ovs_flow_deferred_free_acts(old_acts);
1370
1371                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1372                                                info->snd_seq, OVS_FLOW_CMD_NEW);
1373
1374                 /* Clear stats. */
1375                 if (a[OVS_FLOW_ATTR_CLEAR]) {
1376                         spin_lock_bh(&flow->lock);
1377                         clear_stats(flow);
1378                         spin_unlock_bh(&flow->lock);
1379                 }
1380         }
1381         ovs_unlock();
1382
1383         if (!IS_ERR(reply))
1384                 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1385         else
1386                 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1387                                 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1388         return 0;
1389
1390 err_flow_free:
1391         ovs_flow_free(flow, false);
1392 err_unlock_ovs:
1393         ovs_unlock();
1394 err_kfree:
1395         kfree(acts);
1396 error:
1397         return error;
1398 }
1399
1400 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1401 {
1402         struct nlattr **a = info->attrs;
1403         struct ovs_header *ovs_header = info->userhdr;
1404         struct sw_flow_key key;
1405         struct sk_buff *reply;
1406         struct sw_flow *flow;
1407         struct datapath *dp;
1408         struct flow_table *table;
1409         struct sw_flow_match match;
1410         int err;
1411
1412         if (!a[OVS_FLOW_ATTR_KEY]) {
1413                 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1414                 return -EINVAL;
1415         }
1416
1417         ovs_match_init(&match, &key, NULL);
1418         err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1419         if (err)
1420                 return err;
1421
1422         ovs_lock();
1423         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1424         if (!dp) {
1425                 err = -ENODEV;
1426                 goto unlock;
1427         }
1428
1429         table = ovsl_dereference(dp->table);
1430         flow = ovs_flow_lookup_unmasked_key(table, &match);
1431         if (!flow) {
1432                 err = -ENOENT;
1433                 goto unlock;
1434         }
1435
1436         reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1437                                         info->snd_seq, OVS_FLOW_CMD_NEW);
1438         if (IS_ERR(reply)) {
1439                 err = PTR_ERR(reply);
1440                 goto unlock;
1441         }
1442
1443         ovs_unlock();
1444         return genlmsg_reply(reply, info);
1445 unlock:
1446         ovs_unlock();
1447         return err;
1448 }
1449
1450 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1451 {
1452         struct nlattr **a = info->attrs;
1453         struct ovs_header *ovs_header = info->userhdr;
1454         struct sw_flow_key key;
1455         struct sk_buff *reply;
1456         struct sw_flow *flow;
1457         struct datapath *dp;
1458         struct flow_table *table;
1459         struct sw_flow_match match;
1460         int err;
1461
1462         ovs_lock();
1463         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1464         if (!dp) {
1465                 err = -ENODEV;
1466                 goto unlock;
1467         }
1468
1469         if (!a[OVS_FLOW_ATTR_KEY]) {
1470                 err = flush_flows(dp);
1471                 goto unlock;
1472         }
1473
1474         ovs_match_init(&match, &key, NULL);
1475         err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1476         if (err)
1477                 goto unlock;
1478
1479         table = ovsl_dereference(dp->table);
1480         flow = ovs_flow_lookup_unmasked_key(table, &match);
1481         if (!flow) {
1482                 err = -ENOENT;
1483                 goto unlock;
1484         }
1485
1486         reply = ovs_flow_cmd_alloc_info(flow);
1487         if (!reply) {
1488                 err = -ENOMEM;
1489                 goto unlock;
1490         }
1491
1492         ovs_flow_remove(table, flow);
1493
1494         err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1495                                      info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1496         BUG_ON(err < 0);
1497
1498         ovs_flow_free(flow, true);
1499         ovs_unlock();
1500
1501         ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1502         return 0;
1503 unlock:
1504         ovs_unlock();
1505         return err;
1506 }
1507
1508 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1509 {
1510         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1511         struct datapath *dp;
1512         struct flow_table *table;
1513
1514         rcu_read_lock();
1515         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1516         if (!dp) {
1517                 rcu_read_unlock();
1518                 return -ENODEV;
1519         }
1520
1521         table = rcu_dereference(dp->table);
1522         for (;;) {
1523                 struct sw_flow *flow;
1524                 u32 bucket, obj;
1525
1526                 bucket = cb->args[0];
1527                 obj = cb->args[1];
1528                 flow = ovs_flow_dump_next(table, &bucket, &obj);
1529                 if (!flow)
1530                         break;
1531
1532                 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1533                                            NETLINK_CB(cb->skb).portid,
1534                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1535                                            OVS_FLOW_CMD_NEW) < 0)
1536                         break;
1537
1538                 cb->args[0] = bucket;
1539                 cb->args[1] = obj;
1540         }
1541         rcu_read_unlock();
1542         return skb->len;
1543 }
1544
1545 static struct genl_ops dp_flow_genl_ops[] = {
1546         { .cmd = OVS_FLOW_CMD_NEW,
1547           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1548           .policy = flow_policy,
1549           .doit = ovs_flow_cmd_new_or_set
1550         },
1551         { .cmd = OVS_FLOW_CMD_DEL,
1552           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1553           .policy = flow_policy,
1554           .doit = ovs_flow_cmd_del
1555         },
1556         { .cmd = OVS_FLOW_CMD_GET,
1557           .flags = 0,               /* OK for unprivileged users. */
1558           .policy = flow_policy,
1559           .doit = ovs_flow_cmd_get,
1560           .dumpit = ovs_flow_cmd_dump
1561         },
1562         { .cmd = OVS_FLOW_CMD_SET,
1563           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1564           .policy = flow_policy,
1565           .doit = ovs_flow_cmd_new_or_set,
1566         },
1567 };
1568
1569 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1570         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1571         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1572 };
1573
1574 static struct genl_family dp_datapath_genl_family = {
1575         .id = GENL_ID_GENERATE,
1576         .hdrsize = sizeof(struct ovs_header),
1577         .name = OVS_DATAPATH_FAMILY,
1578         .version = OVS_DATAPATH_VERSION,
1579         .maxattr = OVS_DP_ATTR_MAX,
1580         .netnsok = true,
1581          SET_PARALLEL_OPS
1582 };
1583
1584 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1585         .name = OVS_DATAPATH_MCGROUP
1586 };
1587
1588 static size_t ovs_dp_cmd_msg_size(void)
1589 {
1590         size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1591
1592         msgsize += nla_total_size(IFNAMSIZ);
1593         msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1594
1595         return msgsize;
1596 }
1597
1598 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1599                                 u32 portid, u32 seq, u32 flags, u8 cmd)
1600 {
1601         struct ovs_header *ovs_header;
1602         struct ovs_dp_stats dp_stats;
1603         int err;
1604
1605         ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1606                                    flags, cmd);
1607         if (!ovs_header)
1608                 goto error;
1609
1610         ovs_header->dp_ifindex = get_dpifindex(dp);
1611
1612         rcu_read_lock();
1613         err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1614         rcu_read_unlock();
1615         if (err)
1616                 goto nla_put_failure;
1617
1618         get_dp_stats(dp, &dp_stats);
1619         if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1620                 goto nla_put_failure;
1621
1622         return genlmsg_end(skb, ovs_header);
1623
1624 nla_put_failure:
1625         genlmsg_cancel(skb, ovs_header);
1626 error:
1627         return -EMSGSIZE;
1628 }
1629
1630 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1631                                              u32 seq, u8 cmd)
1632 {
1633         struct sk_buff *skb;
1634         int retval;
1635
1636         skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1637         if (!skb)
1638                 return ERR_PTR(-ENOMEM);
1639
1640         retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1641         if (retval < 0) {
1642                 kfree_skb(skb);
1643                 return ERR_PTR(retval);
1644         }
1645         return skb;
1646 }
1647
1648 /* Called with ovs_mutex. */
1649 static struct datapath *lookup_datapath(struct net *net,
1650                                         struct ovs_header *ovs_header,
1651                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1652 {
1653         struct datapath *dp;
1654
1655         if (!a[OVS_DP_ATTR_NAME])
1656                 dp = get_dp(net, ovs_header->dp_ifindex);
1657         else {
1658                 struct vport *vport;
1659
1660                 rcu_read_lock();
1661                 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1662                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1663                 rcu_read_unlock();
1664         }
1665         return dp ? dp : ERR_PTR(-ENODEV);
1666 }
1667
1668 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1669 {
1670         struct nlattr **a = info->attrs;
1671         struct vport_parms parms;
1672         struct sk_buff *reply;
1673         struct datapath *dp;
1674         struct vport *vport;
1675         struct ovs_net *ovs_net;
1676         int err, i;
1677
1678         err = -EINVAL;
1679         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1680                 goto err;
1681
1682         ovs_lock();
1683
1684         err = -ENOMEM;
1685         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1686         if (dp == NULL)
1687                 goto err_unlock_ovs;
1688
1689         ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1690
1691         /* Allocate table. */
1692         err = -ENOMEM;
1693         rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1694         if (!dp->table)
1695                 goto err_free_dp;
1696
1697         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1698         if (!dp->stats_percpu) {
1699                 err = -ENOMEM;
1700                 goto err_destroy_table;
1701         }
1702
1703         dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1704                             GFP_KERNEL);
1705         if (!dp->ports) {
1706                 err = -ENOMEM;
1707                 goto err_destroy_percpu;
1708         }
1709
1710         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1711                 INIT_HLIST_HEAD(&dp->ports[i]);
1712
1713         /* Set up our datapath device. */
1714         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1715         parms.type = OVS_VPORT_TYPE_INTERNAL;
1716         parms.options = NULL;
1717         parms.dp = dp;
1718         parms.port_no = OVSP_LOCAL;
1719         parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1720
1721         vport = new_vport(&parms);
1722         if (IS_ERR(vport)) {
1723                 err = PTR_ERR(vport);
1724                 if (err == -EBUSY)
1725                         err = -EEXIST;
1726
1727                 goto err_destroy_ports_array;
1728         }
1729
1730         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1731                                       info->snd_seq, OVS_DP_CMD_NEW);
1732         err = PTR_ERR(reply);
1733         if (IS_ERR(reply))
1734                 goto err_destroy_local_port;
1735
1736         ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1737         list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1738
1739         ovs_unlock();
1740
1741         ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1742         return 0;
1743
1744 err_destroy_local_port:
1745         ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1746 err_destroy_ports_array:
1747         kfree(dp->ports);
1748 err_destroy_percpu:
1749         free_percpu(dp->stats_percpu);
1750 err_destroy_table:
1751         ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
1752 err_free_dp:
1753         release_net(ovs_dp_get_net(dp));
1754         kfree(dp);
1755 err_unlock_ovs:
1756         ovs_unlock();
1757 err:
1758         return err;
1759 }
1760
1761 /* Called with ovs_mutex. */
1762 static void __dp_destroy(struct datapath *dp)
1763 {
1764         int i;
1765
1766         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1767                 struct vport *vport;
1768                 struct hlist_node *n;
1769
1770                 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1771                         if (vport->port_no != OVSP_LOCAL)
1772                                 ovs_dp_detach_port(vport);
1773         }
1774
1775         list_del_rcu(&dp->list_node);
1776
1777         /* OVSP_LOCAL is datapath internal port. We need to make sure that
1778          * all port in datapath are destroyed first before freeing datapath.
1779          */
1780         ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1781
1782         call_rcu(&dp->rcu, destroy_dp_rcu);
1783 }
1784
1785 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1786 {
1787         struct sk_buff *reply;
1788         struct datapath *dp;
1789         int err;
1790
1791         ovs_lock();
1792         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1793         err = PTR_ERR(dp);
1794         if (IS_ERR(dp))
1795                 goto unlock;
1796
1797         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1798                                       info->snd_seq, OVS_DP_CMD_DEL);
1799         err = PTR_ERR(reply);
1800         if (IS_ERR(reply))
1801                 goto unlock;
1802
1803         __dp_destroy(dp);
1804         ovs_unlock();
1805
1806         ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1807
1808         return 0;
1809 unlock:
1810         ovs_unlock();
1811         return err;
1812 }
1813
1814 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1815 {
1816         struct sk_buff *reply;
1817         struct datapath *dp;
1818         int err;
1819
1820         ovs_lock();
1821         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1822         err = PTR_ERR(dp);
1823         if (IS_ERR(dp))
1824                 goto unlock;
1825
1826         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1827                                       info->snd_seq, OVS_DP_CMD_NEW);
1828         if (IS_ERR(reply)) {
1829                 err = PTR_ERR(reply);
1830                 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1831                                 ovs_dp_datapath_multicast_group.id, err);
1832                 err = 0;
1833                 goto unlock;
1834         }
1835
1836         ovs_unlock();
1837         ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1838
1839         return 0;
1840 unlock:
1841         ovs_unlock();
1842         return err;
1843 }
1844
1845 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1846 {
1847         struct sk_buff *reply;
1848         struct datapath *dp;
1849         int err;
1850
1851         ovs_lock();
1852         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1853         if (IS_ERR(dp)) {
1854                 err = PTR_ERR(dp);
1855                 goto unlock;
1856         }
1857
1858         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1859                                       info->snd_seq, OVS_DP_CMD_NEW);
1860         if (IS_ERR(reply)) {
1861                 err = PTR_ERR(reply);
1862                 goto unlock;
1863         }
1864
1865         ovs_unlock();
1866         return genlmsg_reply(reply, info);
1867
1868 unlock:
1869         ovs_unlock();
1870         return err;
1871 }
1872
1873 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1874 {
1875         struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1876         struct datapath *dp;
1877         int skip = cb->args[0];
1878         int i = 0;
1879
1880         rcu_read_lock();
1881         list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1882                 if (i >= skip &&
1883                     ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1884                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1885                                          OVS_DP_CMD_NEW) < 0)
1886                         break;
1887                 i++;
1888         }
1889         rcu_read_unlock();
1890
1891         cb->args[0] = i;
1892
1893         return skb->len;
1894 }
1895
1896 static struct genl_ops dp_datapath_genl_ops[] = {
1897         { .cmd = OVS_DP_CMD_NEW,
1898           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1899           .policy = datapath_policy,
1900           .doit = ovs_dp_cmd_new
1901         },
1902         { .cmd = OVS_DP_CMD_DEL,
1903           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1904           .policy = datapath_policy,
1905           .doit = ovs_dp_cmd_del
1906         },
1907         { .cmd = OVS_DP_CMD_GET,
1908           .flags = 0,               /* OK for unprivileged users. */
1909           .policy = datapath_policy,
1910           .doit = ovs_dp_cmd_get,
1911           .dumpit = ovs_dp_cmd_dump
1912         },
1913         { .cmd = OVS_DP_CMD_SET,
1914           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1915           .policy = datapath_policy,
1916           .doit = ovs_dp_cmd_set,
1917         },
1918 };
1919
1920 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1921         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1922         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1923         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1924         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1925         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1926         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1927 };
1928
1929 static struct genl_family dp_vport_genl_family = {
1930         .id = GENL_ID_GENERATE,
1931         .hdrsize = sizeof(struct ovs_header),
1932         .name = OVS_VPORT_FAMILY,
1933         .version = OVS_VPORT_VERSION,
1934         .maxattr = OVS_VPORT_ATTR_MAX,
1935         .netnsok = true,
1936          SET_PARALLEL_OPS
1937 };
1938
1939 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1940         .name = OVS_VPORT_MCGROUP
1941 };
1942
1943 /* Called with ovs_mutex or RCU read lock. */
1944 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1945                                    u32 portid, u32 seq, u32 flags, u8 cmd)
1946 {
1947         struct ovs_header *ovs_header;
1948         struct ovs_vport_stats vport_stats;
1949         int err;
1950
1951         ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1952                                  flags, cmd);
1953         if (!ovs_header)
1954                 return -EMSGSIZE;
1955
1956         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1957
1958         if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1959             nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1960             nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1961             nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1962                 goto nla_put_failure;
1963
1964         ovs_vport_get_stats(vport, &vport_stats);
1965         if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1966                     &vport_stats))
1967                 goto nla_put_failure;
1968
1969         err = ovs_vport_get_options(vport, skb);
1970         if (err == -EMSGSIZE)
1971                 goto error;
1972
1973         return genlmsg_end(skb, ovs_header);
1974
1975 nla_put_failure:
1976         err = -EMSGSIZE;
1977 error:
1978         genlmsg_cancel(skb, ovs_header);
1979         return err;
1980 }
1981
1982 /* Called with ovs_mutex or RCU read lock. */
1983 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1984                                          u32 seq, u8 cmd)
1985 {
1986         struct sk_buff *skb;
1987         int retval;
1988
1989         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1990         if (!skb)
1991                 return ERR_PTR(-ENOMEM);
1992
1993         retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1994         BUG_ON(retval < 0);
1995
1996         return skb;
1997 }
1998
1999 /* Called with ovs_mutex or RCU read lock. */
2000 static struct vport *lookup_vport(struct net *net,
2001                                   struct ovs_header *ovs_header,
2002                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2003 {
2004         struct datapath *dp;
2005         struct vport *vport;
2006
2007         if (a[OVS_VPORT_ATTR_NAME]) {
2008                 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2009                 if (!vport)
2010                         return ERR_PTR(-ENODEV);
2011                 if (ovs_header->dp_ifindex &&
2012                     ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2013                         return ERR_PTR(-ENODEV);
2014                 return vport;
2015         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2016                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2017
2018                 if (port_no >= DP_MAX_PORTS)
2019                         return ERR_PTR(-EFBIG);
2020
2021                 dp = get_dp(net, ovs_header->dp_ifindex);
2022                 if (!dp)
2023                         return ERR_PTR(-ENODEV);
2024
2025                 vport = ovs_vport_ovsl_rcu(dp, port_no);
2026                 if (!vport)
2027                         return ERR_PTR(-ENODEV);
2028                 return vport;
2029         } else
2030                 return ERR_PTR(-EINVAL);
2031 }
2032
2033 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2034 {
2035         struct nlattr **a = info->attrs;
2036         struct ovs_header *ovs_header = info->userhdr;
2037         struct vport_parms parms;
2038         struct sk_buff *reply;
2039         struct vport *vport;
2040         struct datapath *dp;
2041         u32 port_no;
2042         int err;
2043
2044         err = -EINVAL;
2045         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2046             !a[OVS_VPORT_ATTR_UPCALL_PID])
2047                 goto exit;
2048
2049         ovs_lock();
2050         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2051         err = -ENODEV;
2052         if (!dp)
2053                 goto exit_unlock;
2054
2055         if (a[OVS_VPORT_ATTR_PORT_NO]) {
2056                 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2057
2058                 err = -EFBIG;
2059                 if (port_no >= DP_MAX_PORTS)
2060                         goto exit_unlock;
2061
2062                 vport = ovs_vport_ovsl(dp, port_no);
2063                 err = -EBUSY;
2064                 if (vport)
2065                         goto exit_unlock;
2066         } else {
2067                 for (port_no = 1; ; port_no++) {
2068                         if (port_no >= DP_MAX_PORTS) {
2069                                 err = -EFBIG;
2070                                 goto exit_unlock;
2071                         }
2072                         vport = ovs_vport_ovsl(dp, port_no);
2073                         if (!vport)
2074                                 break;
2075                 }
2076         }
2077
2078         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2079         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2080         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2081         parms.dp = dp;
2082         parms.port_no = port_no;
2083         parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2084
2085         vport = new_vport(&parms);
2086         err = PTR_ERR(vport);
2087         if (IS_ERR(vport))
2088                 goto exit_unlock;
2089
2090         err = 0;
2091         if (a[OVS_VPORT_ATTR_STATS])
2092                 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2093
2094         reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
2095                                          OVS_VPORT_CMD_NEW);
2096         if (IS_ERR(reply)) {
2097                 err = PTR_ERR(reply);
2098                 ovs_dp_detach_port(vport);
2099                 goto exit_unlock;
2100         }
2101
2102         ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2103
2104 exit_unlock:
2105         ovs_unlock();
2106 exit:
2107         return err;
2108 }
2109
2110 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2111 {
2112         struct nlattr **a = info->attrs;
2113         struct sk_buff *reply;
2114         struct vport *vport;
2115         int err;
2116
2117         ovs_lock();
2118         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2119         err = PTR_ERR(vport);
2120         if (IS_ERR(vport))
2121                 goto exit_unlock;
2122
2123         if (a[OVS_VPORT_ATTR_TYPE] &&
2124             nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2125                 err = -EINVAL;
2126                 goto exit_unlock;
2127         }
2128
2129         reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2130         if (!reply) {
2131                 err = -ENOMEM;
2132                 goto exit_unlock;
2133         }
2134
2135         if (a[OVS_VPORT_ATTR_OPTIONS]) {
2136                 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2137                 if (err)
2138                         goto exit_free;
2139         }
2140
2141         if (a[OVS_VPORT_ATTR_STATS])
2142                 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2143
2144         if (a[OVS_VPORT_ATTR_UPCALL_PID])
2145                 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2146
2147         err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2148                                       info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2149         BUG_ON(err < 0);
2150
2151         ovs_unlock();
2152         ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2153         return 0;
2154
2155 exit_free:
2156         kfree_skb(reply);
2157 exit_unlock:
2158         ovs_unlock();
2159         return err;
2160 }
2161
2162 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2163 {
2164         struct nlattr **a = info->attrs;
2165         struct sk_buff *reply;
2166         struct vport *vport;
2167         int err;
2168
2169         ovs_lock();
2170         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2171         err = PTR_ERR(vport);
2172         if (IS_ERR(vport))
2173                 goto exit_unlock;
2174
2175         if (vport->port_no == OVSP_LOCAL) {
2176                 err = -EINVAL;
2177                 goto exit_unlock;
2178         }
2179
2180         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2181                                          info->snd_seq, OVS_VPORT_CMD_DEL);
2182         err = PTR_ERR(reply);
2183         if (IS_ERR(reply))
2184                 goto exit_unlock;
2185
2186         err = 0;
2187         ovs_dp_detach_port(vport);
2188
2189         ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2190
2191 exit_unlock:
2192         ovs_unlock();
2193         return err;
2194 }
2195
2196 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2197 {
2198         struct nlattr **a = info->attrs;
2199         struct ovs_header *ovs_header = info->userhdr;
2200         struct sk_buff *reply;
2201         struct vport *vport;
2202         int err;
2203
2204         rcu_read_lock();
2205         vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2206         err = PTR_ERR(vport);
2207         if (IS_ERR(vport))
2208                 goto exit_unlock;
2209
2210         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2211                                          info->snd_seq, OVS_VPORT_CMD_NEW);
2212         err = PTR_ERR(reply);
2213         if (IS_ERR(reply))
2214                 goto exit_unlock;
2215
2216         rcu_read_unlock();
2217
2218         return genlmsg_reply(reply, info);
2219
2220 exit_unlock:
2221         rcu_read_unlock();
2222         return err;
2223 }
2224
2225 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2226 {
2227         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2228         struct datapath *dp;
2229         int bucket = cb->args[0], skip = cb->args[1];
2230         int i, j = 0;
2231
2232         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2233         if (!dp)
2234                 return -ENODEV;
2235
2236         rcu_read_lock();
2237         for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2238                 struct vport *vport;
2239
2240                 j = 0;
2241                 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2242                         if (j >= skip &&
2243                             ovs_vport_cmd_fill_info(vport, skb,
2244                                                     NETLINK_CB(cb->skb).portid,
2245                                                     cb->nlh->nlmsg_seq,
2246                                                     NLM_F_MULTI,
2247                                                     OVS_VPORT_CMD_NEW) < 0)
2248                                 goto out;
2249
2250                         j++;
2251                 }
2252                 skip = 0;
2253         }
2254 out:
2255         rcu_read_unlock();
2256
2257         cb->args[0] = i;
2258         cb->args[1] = j;
2259
2260         return skb->len;
2261 }
2262
2263 static struct genl_ops dp_vport_genl_ops[] = {
2264         { .cmd = OVS_VPORT_CMD_NEW,
2265           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2266           .policy = vport_policy,
2267           .doit = ovs_vport_cmd_new
2268         },
2269         { .cmd = OVS_VPORT_CMD_DEL,
2270           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2271           .policy = vport_policy,
2272           .doit = ovs_vport_cmd_del
2273         },
2274         { .cmd = OVS_VPORT_CMD_GET,
2275           .flags = 0,               /* OK for unprivileged users. */
2276           .policy = vport_policy,
2277           .doit = ovs_vport_cmd_get,
2278           .dumpit = ovs_vport_cmd_dump
2279         },
2280         { .cmd = OVS_VPORT_CMD_SET,
2281           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2282           .policy = vport_policy,
2283           .doit = ovs_vport_cmd_set,
2284         },
2285 };
2286
2287 struct genl_family_and_ops {
2288         struct genl_family *family;
2289         struct genl_ops *ops;
2290         int n_ops;
2291         struct genl_multicast_group *group;
2292 };
2293
2294 static const struct genl_family_and_ops dp_genl_families[] = {
2295         { &dp_datapath_genl_family,
2296           dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2297           &ovs_dp_datapath_multicast_group },
2298         { &dp_vport_genl_family,
2299           dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2300           &ovs_dp_vport_multicast_group },
2301         { &dp_flow_genl_family,
2302           dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2303           &ovs_dp_flow_multicast_group },
2304         { &dp_packet_genl_family,
2305           dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2306           NULL },
2307 };
2308
2309 static void dp_unregister_genl(int n_families)
2310 {
2311         int i;
2312
2313         for (i = 0; i < n_families; i++)
2314                 genl_unregister_family(dp_genl_families[i].family);
2315 }
2316
2317 static int dp_register_genl(void)
2318 {
2319         int n_registered;
2320         int err;
2321         int i;
2322
2323         n_registered = 0;
2324         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2325                 const struct genl_family_and_ops *f = &dp_genl_families[i];
2326
2327                 err = genl_register_family_with_ops(f->family, f->ops,
2328                                                     f->n_ops);
2329                 if (err)
2330                         goto error;
2331                 n_registered++;
2332
2333                 if (f->group) {
2334                         err = genl_register_mc_group(f->family, f->group);
2335                         if (err)
2336                                 goto error;
2337                 }
2338         }
2339
2340         return 0;
2341
2342 error:
2343         dp_unregister_genl(n_registered);
2344         return err;
2345 }
2346
2347 static void rehash_flow_table(struct work_struct *work)
2348 {
2349         struct datapath *dp;
2350         struct net *net;
2351
2352         ovs_lock();
2353         rtnl_lock();
2354         for_each_net(net) {
2355                 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2356
2357                 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2358                         struct flow_table *old_table = ovsl_dereference(dp->table);
2359                         struct flow_table *new_table;
2360
2361                         new_table = ovs_flow_tbl_rehash(old_table);
2362                         if (!IS_ERR(new_table)) {
2363                                 rcu_assign_pointer(dp->table, new_table);
2364                                 ovs_flow_tbl_destroy(old_table, true);
2365                         }
2366                 }
2367         }
2368         rtnl_unlock();
2369         ovs_unlock();
2370         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2371 }
2372
2373 static int __net_init ovs_init_net(struct net *net)
2374 {
2375         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2376
2377         INIT_LIST_HEAD(&ovs_net->dps);
2378         INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2379         return 0;
2380 }
2381
2382 static void __net_exit ovs_exit_net(struct net *net)
2383 {
2384         struct datapath *dp, *dp_next;
2385         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2386
2387         ovs_lock();
2388         list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2389                 __dp_destroy(dp);
2390         ovs_unlock();
2391
2392         cancel_work_sync(&ovs_net->dp_notify_work);
2393 }
2394
2395 static struct pernet_operations ovs_net_ops = {
2396         .init = ovs_init_net,
2397         .exit = ovs_exit_net,
2398         .id   = &ovs_net_id,
2399         .size = sizeof(struct ovs_net),
2400 };
2401
2402 DEFINE_COMPAT_PNET_REG_FUNC(device);
2403
2404 static int __init dp_init(void)
2405 {
2406         int err;
2407
2408         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2409
2410         pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2411                 VERSION);
2412
2413         err = ovs_workqueues_init();
2414         if (err)
2415                 goto error;
2416
2417         err = ovs_flow_init();
2418         if (err)
2419                 goto error_wq;
2420
2421         err = ovs_vport_init();
2422         if (err)
2423                 goto error_flow_exit;
2424
2425         err = register_pernet_device(&ovs_net_ops);
2426         if (err)
2427                 goto error_vport_exit;
2428
2429         err = register_netdevice_notifier(&ovs_dp_device_notifier);
2430         if (err)
2431                 goto error_netns_exit;
2432
2433         err = dp_register_genl();
2434         if (err < 0)
2435                 goto error_unreg_notifier;
2436
2437         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2438
2439         return 0;
2440
2441 error_unreg_notifier:
2442         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2443 error_netns_exit:
2444         unregister_pernet_device(&ovs_net_ops);
2445 error_vport_exit:
2446         ovs_vport_exit();
2447 error_flow_exit:
2448         ovs_flow_exit();
2449 error_wq:
2450         ovs_workqueues_exit();
2451 error:
2452         return err;
2453 }
2454
2455 static void dp_cleanup(void)
2456 {
2457         cancel_delayed_work_sync(&rehash_flow_wq);
2458         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2459         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2460         unregister_pernet_device(&ovs_net_ops);
2461         rcu_barrier();
2462         ovs_vport_exit();
2463         ovs_flow_exit();
2464         ovs_workqueues_exit();
2465 }
2466
2467 module_init(dp_init);
2468 module_exit(dp_cleanup);
2469
2470 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2471 MODULE_LICENSE("GPL");
2472 MODULE_VERSION(VERSION);