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