]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/datapath.c
datapath: Better handle vlan packets sent to userspace.
[mirror_ovs.git] / datapath / datapath.c
CommitLineData
064af421 1/*
a9a29d22 2 * Copyright (c) 2007-2011 Nicira Networks.
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
BP
41#include <linux/wait.h>
42#include <asm/system.h>
43#include <asm/div64.h>
656a0e37 44#include <linux/highmem.h>
064af421
BP
45#include <linux/netfilter_bridge.h>
46#include <linux/netfilter_ipv4.h>
47#include <linux/inetdevice.h>
48#include <linux/list.h>
077257b8 49#include <linux/openvswitch.h>
064af421 50#include <linux/rculist.h>
064af421 51#include <linux/dmi.h>
36956a7d 52#include <net/genetlink.h>
064af421 53
dd8d6b8c 54#include "checksum.h"
064af421 55#include "datapath.h"
064af421 56#include "flow.h"
303708cc 57#include "vlan.h"
3544358a 58#include "tunnel.h"
f2459fe7 59#include "vport-internal_dev.h"
064af421 60
4cf41591 61#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
143af30e
PS
62 LINUX_VERSION_CODE > KERNEL_VERSION(3,2,0)
63#error Kernels before 2.6.18 or after 3.2 are not supported by this version of Open vSwitch.
4cf41591
JG
64#endif
65
064af421
BP
66int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
67EXPORT_SYMBOL(dp_ioctl_hook);
68
ed099e92
BP
69/**
70 * DOC: Locking:
064af421 71 *
ed099e92
BP
72 * Writes to device state (add/remove datapath, port, set operations on vports,
73 * etc.) are protected by RTNL.
064af421 74 *
ed099e92 75 * Writes to other state (flow table modifications, set miscellaneous datapath
7257b535
BP
76 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
77 * genl_mutex.
ed099e92
BP
78 *
79 * Reads are protected by RCU.
80 *
81 * There are a few special cases (mostly stats) that have their own
82 * synchronization but they nest under all of above and don't interact with
83 * each other.
064af421 84 */
ed099e92 85
254f2dc8
BP
86/* Global list of datapaths to enable dumping them all out.
87 * Protected by genl_mutex.
88 */
89static LIST_HEAD(dps);
064af421 90
c19e6535 91static struct vport *new_vport(const struct vport_parms *);
7257b535
BP
92static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
93 const struct dp_upcall_info *);
94static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
95 const struct dp_upcall_info *);
064af421 96
ed099e92 97/* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
df2a06ab 98static struct datapath *get_dp(int dp_ifindex)
064af421 99{
254f2dc8
BP
100 struct datapath *dp = NULL;
101 struct net_device *dev;
ed099e92 102
254f2dc8
BP
103 rcu_read_lock();
104 dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
105 if (dev) {
106 struct vport *vport = internal_dev_get_vport(dev);
107 if (vport)
108 dp = vport->dp;
109 }
110 rcu_read_unlock();
111
112 return dp;
064af421 113}
064af421 114
ed099e92 115/* Must be called with genl_mutex. */
3544358a 116static struct flow_table *get_table_protected(struct datapath *dp)
9abaf6b3 117{
ed099e92 118 return rcu_dereference_protected(dp->table, lockdep_genl_is_held());
1452b28c
JG
119}
120
ed099e92 121/* Must be called with rcu_read_lock or RTNL lock. */
027f9007 122static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
1452b28c 123{
ed099e92 124 return rcu_dereference_rtnl(dp->ports[port_no]);
9abaf6b3
JG
125}
126
f2459fe7
JG
127/* Must be called with rcu_read_lock or RTNL lock. */
128const char *dp_name(const struct datapath *dp)
129{
16b82e84
JG
130 struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
131 return vport->ops->get_name(vport);
f2459fe7
JG
132}
133
99769a40
JG
134static int get_dpifindex(struct datapath *dp)
135{
136 struct vport *local;
137 int ifindex;
138
139 rcu_read_lock();
140
141 local = get_vport_protected(dp, OVSP_LOCAL);
142 if (local)
16b82e84 143 ifindex = local->ops->get_ifindex(local);
99769a40
JG
144 else
145 ifindex = 0;
146
147 rcu_read_unlock();
148
149 return ifindex;
150}
151
6455100f 152static size_t br_nlmsg_size(void)
064af421
BP
153{
154 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
155 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
156 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
157 + nla_total_size(4) /* IFLA_MASTER */
158 + nla_total_size(4) /* IFLA_MTU */
064af421
BP
159 + nla_total_size(1); /* IFLA_OPERSTATE */
160}
161
ed099e92 162/* Caller must hold RTNL lock. */
064af421 163static int dp_fill_ifinfo(struct sk_buff *skb,
e779d8d9 164 const struct vport *port,
064af421
BP
165 int event, unsigned int flags)
166{
027f9007 167 struct datapath *dp = port->dp;
064af421
BP
168 struct ifinfomsg *hdr;
169 struct nlmsghdr *nlh;
170
16b82e84
JG
171 if (!port->ops->get_ifindex)
172 return -ENODEV;
f2459fe7 173
064af421
BP
174 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
175 if (nlh == NULL)
176 return -EMSGSIZE;
177
178 hdr = nlmsg_data(nlh);
179 hdr->ifi_family = AF_BRIDGE;
180 hdr->__ifi_pad = 0;
f2459fe7 181 hdr->ifi_type = ARPHRD_ETHER;
16b82e84
JG
182 hdr->ifi_index = port->ops->get_ifindex(port);
183 hdr->ifi_flags = port->ops->get_dev_flags(port);
064af421
BP
184 hdr->ifi_change = 0;
185
16b82e84 186 NLA_PUT_STRING(skb, IFLA_IFNAME, port->ops->get_name(port));
99769a40 187 NLA_PUT_U32(skb, IFLA_MASTER, get_dpifindex(dp));
16b82e84 188 NLA_PUT_U32(skb, IFLA_MTU, port->ops->get_mtu(port));
064af421
BP
189#ifdef IFLA_OPERSTATE
190 NLA_PUT_U8(skb, IFLA_OPERSTATE,
16b82e84
JG
191 port->ops->is_running(port)
192 ? port->ops->get_operstate(port)
f2459fe7 193 : IF_OPER_DOWN);
064af421
BP
194#endif
195
16b82e84 196 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port));
064af421 197
064af421
BP
198 return nlmsg_end(skb, nlh);
199
200nla_put_failure:
201 nlmsg_cancel(skb, nlh);
202 return -EMSGSIZE;
203}
204
ed099e92 205/* Caller must hold RTNL lock. */
e779d8d9 206static void dp_ifinfo_notify(int event, struct vport *port)
064af421 207{
064af421 208 struct sk_buff *skb;
16b82e84 209 int err;
064af421
BP
210
211 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
16b82e84
JG
212 if (!skb) {
213 err = -ENOBUFS;
214 goto err;
215 }
064af421
BP
216
217 err = dp_fill_ifinfo(skb, port, event, 0);
218 if (err < 0) {
16b82e84
JG
219 if (err == -ENODEV) {
220 goto out;
221 } else {
222 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
223 WARN_ON(err == -EMSGSIZE);
224 goto err;
225 }
064af421 226 }
16b82e84 227
f2459fe7 228 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
16b82e84 229
cfe7c1f5 230 return;
16b82e84
JG
231err:
232 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
233out:
234 kfree_skb(skb);
064af421
BP
235}
236
58c342f6
BP
237static void release_dp(struct kobject *kobj)
238{
239 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
240 kfree(dp);
241}
242
35f7605b 243static struct kobj_type dp_ktype = {
58c342f6
BP
244 .release = release_dp
245};
246
46c6a11d
JG
247static void destroy_dp_rcu(struct rcu_head *rcu)
248{
249 struct datapath *dp = container_of(rcu, struct datapath, rcu);
46c6a11d 250
be2ba156 251 flow_tbl_destroy((__force struct flow_table *)dp->table);
46c6a11d
JG
252 free_percpu(dp->stats_percpu);
253 kobject_put(&dp->ifobj);
254}
255
ed099e92 256/* Called with RTNL lock and genl_lock. */
c19e6535 257static struct vport *new_vport(const struct vport_parms *parms)
064af421 258{
f2459fe7 259 struct vport *vport;
f2459fe7 260
c19e6535
BP
261 vport = vport_add(parms);
262 if (!IS_ERR(vport)) {
263 struct datapath *dp = parms->dp;
064af421 264
c19e6535 265 rcu_assign_pointer(dp->ports[parms->port_no], vport);
ed099e92 266 list_add(&vport->node, &dp->port_list);
064af421 267
c19e6535
BP
268 dp_ifinfo_notify(RTM_NEWLINK, vport);
269 }
064af421 270
c19e6535 271 return vport;
064af421
BP
272}
273
ed099e92 274/* Called with RTNL lock. */
3544358a 275void dp_detach_port(struct vport *p)
064af421
BP
276{
277 ASSERT_RTNL();
278
df2c07f4 279 if (p->port_no != OVSP_LOCAL)
0515ceb3 280 dp_sysfs_del_if(p);
064af421
BP
281 dp_ifinfo_notify(RTM_DELLINK, p);
282
064af421 283 /* First drop references to device. */
ed099e92 284 list_del(&p->node);
064af421 285 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
f2459fe7 286
7237e4f4 287 /* Then destroy it. */
3544358a 288 vport_del(p);
064af421
BP
289}
290
8819fac7 291/* Must be called with rcu_read_lock. */
e779d8d9 292void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
064af421
BP
293{
294 struct datapath *dp = p->dp;
3544358a 295 struct sw_flow *flow;
064af421 296 struct dp_stats_percpu *stats;
e9141eec 297 u64 *stats_counter;
4c1ad233 298 int error;
064af421 299
e9141eec 300 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
a063b0df 301
3976f6d5 302 if (!OVS_CB(skb)->flow) {
36956a7d 303 struct sw_flow_key key;
76abe283 304 int key_len;
4c1ad233 305
3976f6d5 306 /* Extract flow from 'skb' into 'key'. */
7257b535 307 error = flow_extract(skb, p->port_no, &key, &key_len);
3976f6d5
JG
308 if (unlikely(error)) {
309 kfree_skb(skb);
310 return;
311 }
064af421 312
3976f6d5 313 /* Look up flow. */
6455100f
PS
314 flow = flow_tbl_lookup(rcu_dereference(dp->table),
315 &key, key_len);
3544358a 316 if (unlikely(!flow)) {
856081f6
BP
317 struct dp_upcall_info upcall;
318
df2c07f4 319 upcall.cmd = OVS_PACKET_CMD_MISS;
856081f6 320 upcall.key = &key;
98403001
BP
321 upcall.userdata = NULL;
322 upcall.pid = p->upcall_pid;
856081f6 323 dp_upcall(dp, skb, &upcall);
d9e214da 324 consume_skb(skb);
e9141eec 325 stats_counter = &stats->n_missed;
3976f6d5
JG
326 goto out;
327 }
328
3544358a 329 OVS_CB(skb)->flow = flow;
55574bb0
BP
330 }
331
e9141eec 332 stats_counter = &stats->n_hit;
3976f6d5 333 flow_used(OVS_CB(skb)->flow, skb);
a4af2475 334 execute_actions(dp, skb);
55574bb0 335
8819fac7 336out:
55574bb0 337 /* Update datapath statistics. */
38c6ecbc
JG
338
339 write_seqcount_begin(&stats->seqlock);
e9141eec 340 (*stats_counter)++;
38c6ecbc 341 write_seqcount_end(&stats->seqlock);
064af421
BP
342}
343
aa5a8fdc
JG
344static struct genl_family dp_packet_genl_family = {
345 .id = GENL_ID_GENERATE,
df2c07f4
JP
346 .hdrsize = sizeof(struct ovs_header),
347 .name = OVS_PACKET_FAMILY,
69685a88 348 .version = OVS_PACKET_VERSION,
df2c07f4 349 .maxattr = OVS_PACKET_ATTR_MAX
aa5a8fdc
JG
350};
351
36ce148c
PS
352int dp_upcall(struct datapath *dp, struct sk_buff *skb,
353 const struct dp_upcall_info *upcall_info)
aa5a8fdc
JG
354{
355 struct dp_stats_percpu *stats;
7257b535 356 int dp_ifindex;
aa5a8fdc
JG
357 int err;
358
98403001 359 if (upcall_info->pid == 0) {
b063d9f0 360 err = -ENOTCONN;
b063d9f0
JG
361 goto err;
362 }
363
7257b535
BP
364 dp_ifindex = get_dpifindex(dp);
365 if (!dp_ifindex) {
366 err = -ENODEV;
367 goto err;
aa5a8fdc
JG
368 }
369
7257b535 370 forward_ip_summed(skb, true);
36ce148c 371
7257b535
BP
372 if (!skb_is_gso(skb))
373 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
374 else
375 err = queue_gso_packets(dp_ifindex, skb, upcall_info);
d76195db
JG
376 if (err)
377 goto err;
378
379 return 0;
aa5a8fdc 380
aa5a8fdc 381err:
aa5a8fdc
JG
382 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
383
384 write_seqcount_begin(&stats->seqlock);
385 stats->n_lost++;
386 write_seqcount_end(&stats->seqlock);
387
aa5a8fdc 388 return err;
982b8810
BP
389}
390
7257b535
BP
391static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
392 const struct dp_upcall_info *upcall_info)
cb5087ca 393{
7257b535
BP
394 struct dp_upcall_info later_info;
395 struct sw_flow_key later_key;
396 struct sk_buff *segs, *nskb;
397 int err;
cb5087ca 398
7257b535
BP
399 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
400 if (IS_ERR(skb))
401 return PTR_ERR(skb);
99769a40 402
7257b535
BP
403 /* Queue all of the segments. */
404 skb = segs;
cb5087ca 405 do {
7257b535 406 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
982b8810 407 if (err)
7257b535 408 break;
856081f6 409
7257b535
BP
410 if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
411 /* The initial flow key extracted by flow_extract() in
412 * this case is for a first fragment, so we need to
413 * properly mark later fragments.
414 */
415 later_key = *upcall_info->key;
9e44d715 416 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
7257b535
BP
417
418 later_info = *upcall_info;
419 later_info.key = &later_key;
420 upcall_info = &later_info;
421 }
36ce148c 422 } while ((skb = skb->next));
cb5087ca 423
7257b535
BP
424 /* Free all of the segments. */
425 skb = segs;
426 do {
427 nskb = skb->next;
428 if (err)
429 kfree_skb(skb);
430 else
431 consume_skb(skb);
432 } while ((skb = nskb));
433 return err;
434}
435
436static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
437 const struct dp_upcall_info *upcall_info)
438{
439 struct ovs_header *upcall;
6161d3fd 440 struct sk_buff *nskb = NULL;
7257b535
BP
441 struct sk_buff *user_skb; /* to be queued to userspace */
442 struct nlattr *nla;
443 unsigned int len;
444 int err;
445
6161d3fd
JG
446 if (vlan_tx_tag_present(skb)) {
447 nskb = skb_clone(skb, GFP_ATOMIC);
448 if (!nskb)
449 return -ENOMEM;
450
451 err = vlan_deaccel_tag(nskb);
452 if (err)
453 return err;
7257b535 454
6161d3fd
JG
455 skb = nskb;
456 }
457
458 if (nla_attr_size(skb->len) > USHRT_MAX) {
459 err = -EFBIG;
460 goto out;
461 }
7257b535
BP
462
463 len = sizeof(struct ovs_header);
464 len += nla_total_size(skb->len);
465 len += nla_total_size(FLOW_BUFSIZE);
466 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
467 len += nla_total_size(8);
468
469 user_skb = genlmsg_new(len, GFP_ATOMIC);
6161d3fd
JG
470 if (!user_skb) {
471 err = -ENOMEM;
472 goto out;
473 }
7257b535
BP
474
475 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
476 0, upcall_info->cmd);
477 upcall->dp_ifindex = dp_ifindex;
478
479 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
480 flow_to_nlattrs(upcall_info->key, user_skb);
481 nla_nest_end(user_skb, nla);
482
483 if (upcall_info->userdata)
484 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
485 nla_get_u64(upcall_info->userdata));
486
487 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
bed53bd1
PS
488
489 skb_copy_and_csum_dev(skb, nla_data(nla));
7257b535 490
6161d3fd
JG
491 err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
492
493out:
494 kfree_skb(nskb);
495 return err;
cb5087ca
BP
496}
497
ed099e92 498/* Called with genl_mutex. */
254f2dc8 499static int flush_flows(int dp_ifindex)
064af421 500{
3544358a
PS
501 struct flow_table *old_table;
502 struct flow_table *new_table;
9c52546b 503 struct datapath *dp;
9c52546b 504
254f2dc8 505 dp = get_dp(dp_ifindex);
9c52546b 506 if (!dp)
ed099e92 507 return -ENODEV;
8d5ebd83 508
9c52546b 509 old_table = get_table_protected(dp);
3544358a 510 new_table = flow_tbl_alloc(TBL_MIN_BUCKETS);
8d5ebd83 511 if (!new_table)
ed099e92 512 return -ENOMEM;
8d5ebd83
JG
513
514 rcu_assign_pointer(dp->table, new_table);
515
3544358a 516 flow_tbl_deferred_destroy(old_table);
ed099e92 517 return 0;
064af421
BP
518}
519
4edb9ae9
PS
520static int validate_actions(const struct nlattr *attr,
521 const struct sw_flow_key *key, int depth);
6ff686f2 522
4edb9ae9
PS
523static int validate_sample(const struct nlattr *attr,
524 const struct sw_flow_key *key, int depth)
6ff686f2 525{
4be00e48
BP
526 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
527 const struct nlattr *probability, *actions;
528 const struct nlattr *a;
529 int rem;
530
531 memset(attrs, 0, sizeof(attrs));
6455100f 532 nla_for_each_nested(a, attr, rem) {
4be00e48
BP
533 int type = nla_type(a);
534 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
535 return -EINVAL;
536 attrs[type] = a;
537 }
538 if (rem)
6ff686f2 539 return -EINVAL;
4be00e48
BP
540
541 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
542 if (!probability || nla_len(probability) != sizeof(u32))
6ff686f2
PS
543 return -EINVAL;
544
4be00e48
BP
545 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
546 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
547 return -EINVAL;
548 return validate_actions(actions, key, depth + 1);
4edb9ae9
PS
549}
550
fea393b1
BP
551static int validate_set(const struct nlattr *a,
552 const struct sw_flow_key *flow_key)
4edb9ae9 553{
4edb9ae9
PS
554 const struct nlattr *ovs_key = nla_data(a);
555 int key_type = nla_type(ovs_key);
556
557 /* There can be only one key in a action */
558 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
559 return -EINVAL;
560
561 if (key_type > OVS_KEY_ATTR_MAX ||
562 nla_len(ovs_key) != ovs_key_lens[key_type])
563 return -EINVAL;
564
fea393b1 565 switch (key_type) {
4edb9ae9 566 const struct ovs_key_ipv4 *ipv4_key;
4edb9ae9 567
fea393b1
BP
568 case OVS_KEY_ATTR_PRIORITY:
569 case OVS_KEY_ATTR_TUN_ID:
570 case OVS_KEY_ATTR_ETHERNET:
4edb9ae9
PS
571 break;
572
fea393b1 573 case OVS_KEY_ATTR_IPV4:
4edb9ae9
PS
574 if (flow_key->eth.type != htons(ETH_P_IP))
575 return -EINVAL;
576
577 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
578 return -EINVAL;
579
580 ipv4_key = nla_data(ovs_key);
581 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
582 return -EINVAL;
583
9e44d715 584 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
7257b535
BP
585 return -EINVAL;
586
4edb9ae9
PS
587 break;
588
fea393b1 589 case OVS_KEY_ATTR_TCP:
4edb9ae9
PS
590 if (flow_key->ip.proto != IPPROTO_TCP)
591 return -EINVAL;
592
593 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
594 return -EINVAL;
595
596 break;
597
fea393b1 598 case OVS_KEY_ATTR_UDP:
4edb9ae9
PS
599 if (flow_key->ip.proto != IPPROTO_UDP)
600 return -EINVAL;
601
602 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
603 return -EINVAL;
604 break;
605
606 default:
607 return -EINVAL;
608 }
fea393b1 609
4edb9ae9 610 return 0;
6ff686f2
PS
611}
612
98403001
BP
613static int validate_userspace(const struct nlattr *attr)
614{
6455100f 615 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
98403001
BP
616 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
617 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
618 };
619 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
620 int error;
621
6455100f
PS
622 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
623 attr, userspace_policy);
98403001
BP
624 if (error)
625 return error;
626
6455100f
PS
627 if (!a[OVS_USERSPACE_ATTR_PID] ||
628 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
98403001
BP
629 return -EINVAL;
630
631 return 0;
632}
633
4edb9ae9
PS
634static int validate_actions(const struct nlattr *attr,
635 const struct sw_flow_key *key, int depth)
064af421 636{
23cad98c 637 const struct nlattr *a;
6ff686f2
PS
638 int rem, err;
639
640 if (depth >= SAMPLE_ACTION_DEPTH)
641 return -EOVERFLOW;
23cad98c 642
37a1300c 643 nla_for_each_nested(a, attr, rem) {
98403001 644 /* Expected argument lengths, (u32)-1 for variable length. */
df2c07f4 645 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
fea393b1 646 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
98403001 647 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
fea393b1
BP
648 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
649 [OVS_ACTION_ATTR_POP_VLAN] = 0,
4edb9ae9 650 [OVS_ACTION_ATTR_SET] = (u32)-1,
98403001 651 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
23cad98c 652 };
fea393b1 653 const struct ovs_action_push_vlan *vlan;
23cad98c
BP
654 int type = nla_type(a);
655
6ff686f2 656 if (type > OVS_ACTION_ATTR_MAX ||
98403001
BP
657 (action_lens[type] != nla_len(a) &&
658 action_lens[type] != (u32)-1))
23cad98c
BP
659 return -EINVAL;
660
661 switch (type) {
df2c07f4 662 case OVS_ACTION_ATTR_UNSPEC:
cdee00fd 663 return -EINVAL;
064af421 664
98403001
BP
665 case OVS_ACTION_ATTR_USERSPACE:
666 err = validate_userspace(a);
667 if (err)
668 return err;
669 break;
670
df2c07f4 671 case OVS_ACTION_ATTR_OUTPUT:
23cad98c
BP
672 if (nla_get_u32(a) >= DP_MAX_PORTS)
673 return -EINVAL;
3b1fc5f3 674 break;
cdee00fd 675
4edb9ae9 676
fea393b1
BP
677 case OVS_ACTION_ATTR_POP_VLAN:
678 break;
679
680 case OVS_ACTION_ATTR_PUSH_VLAN:
681 vlan = nla_data(a);
682 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
683 return -EINVAL;
8ddc056d 684 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
064af421 685 return -EINVAL;
23cad98c 686 break;
064af421 687
4edb9ae9 688 case OVS_ACTION_ATTR_SET:
fea393b1 689 err = validate_set(a, key);
4edb9ae9
PS
690 if (err)
691 return err;
23cad98c 692 break;
064af421 693
6ff686f2 694 case OVS_ACTION_ATTR_SAMPLE:
4edb9ae9 695 err = validate_sample(a, key, depth);
6ff686f2
PS
696 if (err)
697 return err;
698 break;
699
23cad98c 700 default:
4edb9ae9 701 return -EINVAL;
23cad98c
BP
702 }
703 }
3c5f6de3 704
23cad98c
BP
705 if (rem > 0)
706 return -EINVAL;
064af421 707
23cad98c 708 return 0;
064af421 709}
4edb9ae9 710
064af421
BP
711static void clear_stats(struct sw_flow *flow)
712{
6bfafa55 713 flow->used = 0;
064af421 714 flow->tcp_flags = 0;
064af421
BP
715 flow->packet_count = 0;
716 flow->byte_count = 0;
717}
718
df2c07f4 719static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
064af421 720{
df2c07f4 721 struct ovs_header *ovs_header = info->userhdr;
982b8810 722 struct nlattr **a = info->attrs;
e0e57990 723 struct sw_flow_actions *acts;
982b8810 724 struct sk_buff *packet;
e0e57990 725 struct sw_flow *flow;
f7cd0081 726 struct datapath *dp;
d6569377 727 struct ethhdr *eth;
3f19d399 728 int len;
d6569377 729 int err;
76abe283 730 int key_len;
064af421 731
f7cd0081 732 err = -EINVAL;
df2c07f4
JP
733 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
734 !a[OVS_PACKET_ATTR_ACTIONS] ||
735 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
e5cad958 736 goto err;
064af421 737
df2c07f4 738 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
3f19d399 739 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
f7cd0081
BP
740 err = -ENOMEM;
741 if (!packet)
e5cad958 742 goto err;
3f19d399
BP
743 skb_reserve(packet, NET_IP_ALIGN);
744
df2c07f4 745 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
8d5ebd83 746
f7cd0081
BP
747 skb_reset_mac_header(packet);
748 eth = eth_hdr(packet);
064af421 749
d6569377
BP
750 /* Normally, setting the skb 'protocol' field would be handled by a
751 * call to eth_type_trans(), but it assumes there's a sending
752 * device, which we may not have. */
753 if (ntohs(eth->h_proto) >= 1536)
f7cd0081 754 packet->protocol = eth->h_proto;
d6569377 755 else
f7cd0081 756 packet->protocol = htons(ETH_P_802_2);
d3c54451 757
e0e57990
BP
758 /* Build an sw_flow for sending this packet. */
759 flow = flow_alloc();
760 err = PTR_ERR(flow);
761 if (IS_ERR(flow))
e5cad958 762 goto err_kfree_skb;
064af421 763
7257b535 764 err = flow_extract(packet, -1, &flow->key, &key_len);
e0e57990
BP
765 if (err)
766 goto err_flow_put;
e0e57990 767
abff858b
PS
768 err = flow_metadata_from_nlattrs(&flow->key.phy.priority,
769 &flow->key.phy.in_port,
770 &flow->key.phy.tun_id,
df2c07f4 771 a[OVS_PACKET_ATTR_KEY]);
80e5eed9
BP
772 if (err)
773 goto err_flow_put;
774
4edb9ae9
PS
775 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
776 if (err)
777 goto err_flow_put;
778
3544358a 779 flow->hash = flow_hash(&flow->key, key_len);
0fe255df 780
df2c07f4 781 acts = flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
e0e57990
BP
782 err = PTR_ERR(acts);
783 if (IS_ERR(acts))
784 goto err_flow_put;
785 rcu_assign_pointer(flow->sf_acts, acts);
786
787 OVS_CB(packet)->flow = flow;
abff858b 788 packet->priority = flow->key.phy.priority;
e0e57990 789
d6569377 790 rcu_read_lock();
df2c07f4 791 dp = get_dp(ovs_header->dp_ifindex);
f7cd0081 792 err = -ENODEV;
e5cad958
BP
793 if (!dp)
794 goto err_unlock;
cc4015df 795
e9141eec 796 local_bh_disable();
a4af2475 797 err = execute_actions(dp, packet);
e9141eec 798 local_bh_enable();
d6569377 799 rcu_read_unlock();
e0e57990
BP
800
801 flow_put(flow);
e5cad958 802 return err;
064af421 803
e5cad958
BP
804err_unlock:
805 rcu_read_unlock();
e0e57990
BP
806err_flow_put:
807 flow_put(flow);
e5cad958
BP
808err_kfree_skb:
809 kfree_skb(packet);
810err:
d6569377 811 return err;
064af421
BP
812}
813
df2c07f4
JP
814static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
815 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
816 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
817 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
982b8810
BP
818};
819
820static struct genl_ops dp_packet_genl_ops[] = {
df2c07f4 821 { .cmd = OVS_PACKET_CMD_EXECUTE,
982b8810
BP
822 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
823 .policy = packet_policy,
df2c07f4 824 .doit = ovs_packet_cmd_execute
982b8810
BP
825 }
826};
827
df2c07f4 828static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
064af421 829{
d6569377 830 int i;
3544358a 831 struct flow_table *table = get_table_protected(dp);
f180c2e2 832
3544358a 833 stats->n_flows = flow_tbl_count(table);
064af421 834
7257b535 835 stats->n_hit = stats->n_missed = stats->n_lost = 0;
d6569377
BP
836 for_each_possible_cpu(i) {
837 const struct dp_stats_percpu *percpu_stats;
838 struct dp_stats_percpu local_stats;
839 unsigned seqcount;
44e05eca 840
d6569377 841 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
064af421 842
d6569377
BP
843 do {
844 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
845 local_stats = *percpu_stats;
846 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
064af421 847
d6569377
BP
848 stats->n_hit += local_stats.n_hit;
849 stats->n_missed += local_stats.n_missed;
850 stats->n_lost += local_stats.n_lost;
851 }
852}
064af421 853
df2c07f4
JP
854static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
855 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
856 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
857 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
d6569377 858};
36956a7d 859
37a1300c
BP
860static struct genl_family dp_flow_genl_family = {
861 .id = GENL_ID_GENERATE,
df2c07f4
JP
862 .hdrsize = sizeof(struct ovs_header),
863 .name = OVS_FLOW_FAMILY,
69685a88 864 .version = OVS_FLOW_VERSION,
df2c07f4 865 .maxattr = OVS_FLOW_ATTR_MAX
37a1300c 866};
ed099e92 867
37a1300c 868static struct genl_multicast_group dp_flow_multicast_group = {
df2c07f4 869 .name = OVS_FLOW_MCGROUP
37a1300c
BP
870};
871
872/* Called with genl_lock. */
df2c07f4 873static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
6455100f
PS
874 struct sk_buff *skb, u32 pid,
875 u32 seq, u32 flags, u8 cmd)
d6569377 876{
37a1300c 877 const int skb_orig_len = skb->len;
d6569377 878 const struct sw_flow_actions *sf_acts;
df2c07f4
JP
879 struct ovs_flow_stats stats;
880 struct ovs_header *ovs_header;
d6569377
BP
881 struct nlattr *nla;
882 unsigned long used;
883 u8 tcp_flags;
884 int err;
064af421 885
d6569377 886 sf_acts = rcu_dereference_protected(flow->sf_acts,
ed099e92 887 lockdep_genl_is_held());
064af421 888
df2c07f4
JP
889 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
890 if (!ovs_header)
37a1300c 891 return -EMSGSIZE;
d6569377 892
99769a40 893 ovs_header->dp_ifindex = get_dpifindex(dp);
d6569377 894
df2c07f4 895 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
d6569377
BP
896 if (!nla)
897 goto nla_put_failure;
898 err = flow_to_nlattrs(&flow->key, skb);
899 if (err)
37a1300c 900 goto error;
d6569377
BP
901 nla_nest_end(skb, nla);
902
903 spin_lock_bh(&flow->lock);
904 used = flow->used;
905 stats.n_packets = flow->packet_count;
906 stats.n_bytes = flow->byte_count;
907 tcp_flags = flow->tcp_flags;
908 spin_unlock_bh(&flow->lock);
909
910 if (used)
df2c07f4 911 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, flow_used_time(used));
d6569377
BP
912
913 if (stats.n_packets)
6455100f
PS
914 NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
915 sizeof(struct ovs_flow_stats), &stats);
d6569377
BP
916
917 if (tcp_flags)
df2c07f4 918 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
d6569377 919
df2c07f4 920 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
30053024
BP
921 * this is the first flow to be dumped into 'skb'. This is unusual for
922 * Netlink but individual action lists can be longer than
923 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
924 * The userspace caller can always fetch the actions separately if it
925 * really wants them. (Most userspace callers in fact don't care.)
926 *
927 * This can only fail for dump operations because the skb is always
928 * properly sized for single flows.
929 */
df2c07f4 930 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
30053024
BP
931 sf_acts->actions);
932 if (err < 0 && skb_orig_len)
933 goto error;
37a1300c 934
df2c07f4 935 return genlmsg_end(skb, ovs_header);
d6569377
BP
936
937nla_put_failure:
938 err = -EMSGSIZE;
37a1300c 939error:
df2c07f4 940 genlmsg_cancel(skb, ovs_header);
d6569377 941 return err;
44e05eca
BP
942}
943
df2c07f4 944static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
44e05eca 945{
37a1300c
BP
946 const struct sw_flow_actions *sf_acts;
947 int len;
d6569377 948
37a1300c
BP
949 sf_acts = rcu_dereference_protected(flow->sf_acts,
950 lockdep_genl_is_held());
d6569377 951
6455100f
PS
952 /* OVS_FLOW_ATTR_KEY */
953 len = nla_total_size(FLOW_BUFSIZE);
954 /* OVS_FLOW_ATTR_ACTIONS */
955 len += nla_total_size(sf_acts->actions_len);
956 /* OVS_FLOW_ATTR_STATS */
957 len += nla_total_size(sizeof(struct ovs_flow_stats));
958 /* OVS_FLOW_ATTR_TCP_FLAGS */
959 len += nla_total_size(1);
960 /* OVS_FLOW_ATTR_USED */
961 len += nla_total_size(8);
962
963 len += NLMSG_ALIGN(sizeof(struct ovs_header));
964
965 return genlmsg_new(len, GFP_KERNEL);
37a1300c 966}
8d5ebd83 967
6455100f
PS
968static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
969 struct datapath *dp,
37a1300c
BP
970 u32 pid, u32 seq, u8 cmd)
971{
972 struct sk_buff *skb;
973 int retval;
d6569377 974
df2c07f4 975 skb = ovs_flow_cmd_alloc_info(flow);
37a1300c
BP
976 if (!skb)
977 return ERR_PTR(-ENOMEM);
d6569377 978
df2c07f4 979 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
37a1300c 980 BUG_ON(retval < 0);
d6569377 981 return skb;
064af421
BP
982}
983
df2c07f4 984static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
064af421 985{
37a1300c 986 struct nlattr **a = info->attrs;
df2c07f4 987 struct ovs_header *ovs_header = info->userhdr;
37a1300c 988 struct sw_flow_key key;
d6569377 989 struct sw_flow *flow;
37a1300c 990 struct sk_buff *reply;
9c52546b 991 struct datapath *dp;
3544358a 992 struct flow_table *table;
bc4a05c6 993 int error;
76abe283 994 int key_len;
064af421 995
37a1300c
BP
996 /* Extract key. */
997 error = -EINVAL;
df2c07f4 998 if (!a[OVS_FLOW_ATTR_KEY])
37a1300c 999 goto error;
df2c07f4 1000 error = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
37a1300c
BP
1001 if (error)
1002 goto error;
064af421 1003
37a1300c 1004 /* Validate actions. */
df2c07f4 1005 if (a[OVS_FLOW_ATTR_ACTIONS]) {
4edb9ae9 1006 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
37a1300c
BP
1007 if (error)
1008 goto error;
df2c07f4 1009 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
37a1300c
BP
1010 error = -EINVAL;
1011 goto error;
1012 }
1013
df2c07f4 1014 dp = get_dp(ovs_header->dp_ifindex);
d6569377 1015 error = -ENODEV;
9c52546b 1016 if (!dp)
37a1300c 1017 goto error;
704a1e09 1018
d6569377 1019 table = get_table_protected(dp);
3544358a
PS
1020 flow = flow_tbl_lookup(table, &key, key_len);
1021 if (!flow) {
d6569377
BP
1022 struct sw_flow_actions *acts;
1023
1024 /* Bail out if we're not allowed to create a new flow. */
1025 error = -ENOENT;
df2c07f4 1026 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
37a1300c 1027 goto error;
d6569377
BP
1028
1029 /* Expand table, if necessary, to make room. */
3544358a
PS
1030 if (flow_tbl_need_to_expand(table)) {
1031 struct flow_table *new_table;
1032
1033 new_table = flow_tbl_expand(table);
1034 if (!IS_ERR(new_table)) {
1035 rcu_assign_pointer(dp->table, new_table);
1036 flow_tbl_deferred_destroy(table);
1037 table = get_table_protected(dp);
1038 }
d6569377
BP
1039 }
1040
1041 /* Allocate flow. */
1042 flow = flow_alloc();
1043 if (IS_ERR(flow)) {
1044 error = PTR_ERR(flow);
37a1300c 1045 goto error;
d6569377 1046 }
37a1300c 1047 flow->key = key;
d6569377
BP
1048 clear_stats(flow);
1049
1050 /* Obtain actions. */
df2c07f4 1051 acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
d6569377
BP
1052 error = PTR_ERR(acts);
1053 if (IS_ERR(acts))
1054 goto error_free_flow;
1055 rcu_assign_pointer(flow->sf_acts, acts);
1056
d6569377 1057 /* Put flow in bucket. */
3544358a
PS
1058 flow->hash = flow_hash(&key, key_len);
1059 flow_tbl_insert(table, flow);
37a1300c 1060
df2c07f4 1061 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
6455100f
PS
1062 info->snd_seq,
1063 OVS_FLOW_CMD_NEW);
d6569377
BP
1064 } else {
1065 /* We found a matching flow. */
1066 struct sw_flow_actions *old_acts;
6455100f 1067 struct nlattr *acts_attrs;
d6569377
BP
1068
1069 /* Bail out if we're not allowed to modify an existing flow.
1070 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1071 * because Generic Netlink treats the latter as a dump
1072 * request. We also accept NLM_F_EXCL in case that bug ever
1073 * gets fixed.
1074 */
1075 error = -EEXIST;
df2c07f4 1076 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
37a1300c
BP
1077 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1078 goto error;
d6569377
BP
1079
1080 /* Update actions. */
d6569377 1081 old_acts = rcu_dereference_protected(flow->sf_acts,
ed099e92 1082 lockdep_genl_is_held());
6455100f
PS
1083 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1084 if (acts_attrs &&
1085 (old_acts->actions_len != nla_len(acts_attrs) ||
1086 memcmp(old_acts->actions, nla_data(acts_attrs),
1087 old_acts->actions_len))) {
d6569377
BP
1088 struct sw_flow_actions *new_acts;
1089
6455100f 1090 new_acts = flow_actions_alloc(acts_attrs);
d6569377
BP
1091 error = PTR_ERR(new_acts);
1092 if (IS_ERR(new_acts))
37a1300c 1093 goto error;
d6569377
BP
1094
1095 rcu_assign_pointer(flow->sf_acts, new_acts);
1096 flow_deferred_free_acts(old_acts);
1097 }
1098
df2c07f4 1099 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
6455100f 1100 info->snd_seq, OVS_FLOW_CMD_NEW);
d6569377
BP
1101
1102 /* Clear stats. */
df2c07f4 1103 if (a[OVS_FLOW_ATTR_CLEAR]) {
d6569377
BP
1104 spin_lock_bh(&flow->lock);
1105 clear_stats(flow);
1106 spin_unlock_bh(&flow->lock);
1107 }
9c52546b 1108 }
37a1300c
BP
1109
1110 if (!IS_ERR(reply))
1111 genl_notify(reply, genl_info_net(info), info->snd_pid,
6455100f 1112 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
37a1300c
BP
1113 else
1114 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1115 dp_flow_multicast_group.id, PTR_ERR(reply));
d6569377 1116 return 0;
704a1e09 1117
d6569377
BP
1118error_free_flow:
1119 flow_put(flow);
37a1300c 1120error:
9c52546b 1121 return error;
704a1e09
BP
1122}
1123
df2c07f4 1124static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
704a1e09 1125{
37a1300c 1126 struct nlattr **a = info->attrs;
df2c07f4 1127 struct ovs_header *ovs_header = info->userhdr;
37a1300c 1128 struct sw_flow_key key;
37a1300c 1129 struct sk_buff *reply;
704a1e09 1130 struct sw_flow *flow;
9c52546b 1131 struct datapath *dp;
3544358a 1132 struct flow_table *table;
9c52546b 1133 int err;
76abe283 1134 int key_len;
704a1e09 1135
df2c07f4 1136 if (!a[OVS_FLOW_ATTR_KEY])
37a1300c 1137 return -EINVAL;
df2c07f4 1138 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
37a1300c
BP
1139 if (err)
1140 return err;
704a1e09 1141
df2c07f4 1142 dp = get_dp(ovs_header->dp_ifindex);
9c52546b 1143 if (!dp)
ed099e92 1144 return -ENODEV;
704a1e09 1145
9c52546b 1146 table = get_table_protected(dp);
3544358a
PS
1147 flow = flow_tbl_lookup(table, &key, key_len);
1148 if (!flow)
ed099e92 1149 return -ENOENT;
d6569377 1150
6455100f
PS
1151 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1152 info->snd_seq, OVS_FLOW_CMD_NEW);
37a1300c
BP
1153 if (IS_ERR(reply))
1154 return PTR_ERR(reply);
36956a7d 1155
37a1300c 1156 return genlmsg_reply(reply, info);
d6569377 1157}
9c52546b 1158
df2c07f4 1159static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
d6569377 1160{
37a1300c 1161 struct nlattr **a = info->attrs;
df2c07f4 1162 struct ovs_header *ovs_header = info->userhdr;
37a1300c 1163 struct sw_flow_key key;
37a1300c 1164 struct sk_buff *reply;
d6569377 1165 struct sw_flow *flow;
d6569377 1166 struct datapath *dp;
3544358a 1167 struct flow_table *table;
d6569377 1168 int err;
76abe283 1169 int key_len;
36956a7d 1170
df2c07f4
JP
1171 if (!a[OVS_FLOW_ATTR_KEY])
1172 return flush_flows(ovs_header->dp_ifindex);
1173 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
37a1300c
BP
1174 if (err)
1175 return err;
d6569377 1176
df2c07f4 1177 dp = get_dp(ovs_header->dp_ifindex);
d6569377 1178 if (!dp)
6455100f 1179 return -ENODEV;
d6569377 1180
37a1300c 1181 table = get_table_protected(dp);
3544358a
PS
1182 flow = flow_tbl_lookup(table, &key, key_len);
1183 if (!flow)
37a1300c 1184 return -ENOENT;
d6569377 1185
df2c07f4 1186 reply = ovs_flow_cmd_alloc_info(flow);
37a1300c
BP
1187 if (!reply)
1188 return -ENOMEM;
1189
3544358a 1190 flow_tbl_remove(table, flow);
37a1300c 1191
df2c07f4
JP
1192 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1193 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
37a1300c
BP
1194 BUG_ON(err < 0);
1195
1196 flow_deferred_free(flow);
1197
1198 genl_notify(reply, genl_info_net(info), info->snd_pid,
1199 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1200 return 0;
1201}
1202
df2c07f4 1203static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
37a1300c 1204{
df2c07f4 1205 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
37a1300c
BP
1206 struct datapath *dp;
1207
df2c07f4 1208 dp = get_dp(ovs_header->dp_ifindex);
37a1300c
BP
1209 if (!dp)
1210 return -ENODEV;
1211
1212 for (;;) {
37a1300c
BP
1213 struct sw_flow *flow;
1214 u32 bucket, obj;
1215
1216 bucket = cb->args[0];
1217 obj = cb->args[1];
3544358a
PS
1218 flow = flow_tbl_next(get_table_protected(dp), &bucket, &obj);
1219 if (!flow)
37a1300c
BP
1220 break;
1221
6455100f
PS
1222 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1223 NETLINK_CB(cb->skb).pid,
37a1300c 1224 cb->nlh->nlmsg_seq, NLM_F_MULTI,
df2c07f4 1225 OVS_FLOW_CMD_NEW) < 0)
37a1300c
BP
1226 break;
1227
1228 cb->args[0] = bucket;
1229 cb->args[1] = obj;
1230 }
1231 return skb->len;
704a1e09
BP
1232}
1233
37a1300c 1234static struct genl_ops dp_flow_genl_ops[] = {
df2c07f4 1235 { .cmd = OVS_FLOW_CMD_NEW,
37a1300c
BP
1236 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1237 .policy = flow_policy,
df2c07f4 1238 .doit = ovs_flow_cmd_new_or_set
37a1300c 1239 },
df2c07f4 1240 { .cmd = OVS_FLOW_CMD_DEL,
37a1300c
BP
1241 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1242 .policy = flow_policy,
df2c07f4 1243 .doit = ovs_flow_cmd_del
37a1300c 1244 },
df2c07f4 1245 { .cmd = OVS_FLOW_CMD_GET,
37a1300c
BP
1246 .flags = 0, /* OK for unprivileged users. */
1247 .policy = flow_policy,
df2c07f4
JP
1248 .doit = ovs_flow_cmd_get,
1249 .dumpit = ovs_flow_cmd_dump
37a1300c 1250 },
df2c07f4 1251 { .cmd = OVS_FLOW_CMD_SET,
37a1300c
BP
1252 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1253 .policy = flow_policy,
df2c07f4 1254 .doit = ovs_flow_cmd_new_or_set,
37a1300c
BP
1255 },
1256};
1257
df2c07f4 1258static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
aaff4b55 1259#ifdef HAVE_NLA_NUL_STRING
df2c07f4 1260 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
aaff4b55 1261#endif
b063d9f0 1262 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
d6569377
BP
1263};
1264
aaff4b55
BP
1265static struct genl_family dp_datapath_genl_family = {
1266 .id = GENL_ID_GENERATE,
df2c07f4
JP
1267 .hdrsize = sizeof(struct ovs_header),
1268 .name = OVS_DATAPATH_FAMILY,
69685a88 1269 .version = OVS_DATAPATH_VERSION,
df2c07f4 1270 .maxattr = OVS_DP_ATTR_MAX
aaff4b55
BP
1271};
1272
1273static struct genl_multicast_group dp_datapath_multicast_group = {
df2c07f4 1274 .name = OVS_DATAPATH_MCGROUP
aaff4b55
BP
1275};
1276
df2c07f4 1277static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
aaff4b55 1278 u32 pid, u32 seq, u32 flags, u8 cmd)
064af421 1279{
df2c07f4 1280 struct ovs_header *ovs_header;
e926dfe3 1281 struct ovs_dp_stats dp_stats;
064af421
BP
1282 int err;
1283
df2c07f4 1284 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
aaff4b55 1285 flags, cmd);
df2c07f4 1286 if (!ovs_header)
aaff4b55 1287 goto error;
064af421 1288
b063d9f0 1289 ovs_header->dp_ifindex = get_dpifindex(dp);
064af421 1290
d6569377 1291 rcu_read_lock();
df2c07f4 1292 err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
d6569377 1293 rcu_read_unlock();
064af421 1294 if (err)
d6569377 1295 goto nla_put_failure;
064af421 1296
e926dfe3
JG
1297 get_dp_stats(dp, &dp_stats);
1298 NLA_PUT(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats);
d6569377 1299
df2c07f4 1300 return genlmsg_end(skb, ovs_header);
d6569377
BP
1301
1302nla_put_failure:
df2c07f4 1303 genlmsg_cancel(skb, ovs_header);
aaff4b55
BP
1304error:
1305 return -EMSGSIZE;
d6569377
BP
1306}
1307
df2c07f4 1308static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
aaff4b55 1309 u32 seq, u8 cmd)
d6569377 1310{
d6569377 1311 struct sk_buff *skb;
aaff4b55 1312 int retval;
d6569377 1313
aaff4b55 1314 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
064af421 1315 if (!skb)
d6569377 1316 return ERR_PTR(-ENOMEM);
659586ef 1317
df2c07f4 1318 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
aaff4b55
BP
1319 if (retval < 0) {
1320 kfree_skb(skb);
1321 return ERR_PTR(retval);
1322 }
1323 return skb;
1324}
9dca7bd5 1325
df2c07f4 1326static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
aaff4b55 1327{
df2c07f4 1328 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
d6569377
BP
1329}
1330
ed099e92 1331/* Called with genl_mutex and optionally with RTNL lock also. */
6455100f
PS
1332static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
1333 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
d6569377 1334{
254f2dc8
BP
1335 struct datapath *dp;
1336
df2c07f4
JP
1337 if (!a[OVS_DP_ATTR_NAME])
1338 dp = get_dp(ovs_header->dp_ifindex);
254f2dc8 1339 else {
d6569377 1340 struct vport *vport;
d6569377 1341
057dd6d2 1342 rcu_read_lock();
df2c07f4
JP
1343 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1344 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
057dd6d2 1345 rcu_read_unlock();
d6569377 1346 }
254f2dc8 1347 return dp ? dp : ERR_PTR(-ENODEV);
d6569377
BP
1348}
1349
df2c07f4 1350static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
d6569377 1351{
aaff4b55 1352 struct nlattr **a = info->attrs;
d6569377 1353 struct vport_parms parms;
aaff4b55 1354 struct sk_buff *reply;
d6569377
BP
1355 struct datapath *dp;
1356 struct vport *vport;
d6569377 1357 int err;
d6569377 1358
d6569377 1359 err = -EINVAL;
ea36840f 1360 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
aaff4b55
BP
1361 goto err;
1362
df2c07f4 1363 err = ovs_dp_cmd_validate(a);
aaff4b55
BP
1364 if (err)
1365 goto err;
d6569377
BP
1366
1367 rtnl_lock();
d6569377
BP
1368 err = -ENODEV;
1369 if (!try_module_get(THIS_MODULE))
ed099e92 1370 goto err_unlock_rtnl;
d6569377 1371
d6569377
BP
1372 err = -ENOMEM;
1373 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1374 if (dp == NULL)
1375 goto err_put_module;
1376 INIT_LIST_HEAD(&dp->port_list);
d6569377
BP
1377
1378 /* Initialize kobject for bridge. This will be added as
1379 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1380 dp->ifobj.kset = NULL;
1381 kobject_init(&dp->ifobj, &dp_ktype);
1382
1383 /* Allocate table. */
1384 err = -ENOMEM;
3544358a 1385 rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
d6569377
BP
1386 if (!dp->table)
1387 goto err_free_dp;
1388
99769a40
JG
1389 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1390 if (!dp->stats_percpu) {
1391 err = -ENOMEM;
1392 goto err_destroy_table;
1393 }
1394
d6569377 1395 /* Set up our datapath device. */
df2c07f4
JP
1396 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1397 parms.type = OVS_VPORT_TYPE_INTERNAL;
d6569377
BP
1398 parms.options = NULL;
1399 parms.dp = dp;
df2c07f4 1400 parms.port_no = OVSP_LOCAL;
ea36840f 1401 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
b063d9f0 1402
d6569377
BP
1403 vport = new_vport(&parms);
1404 if (IS_ERR(vport)) {
1405 err = PTR_ERR(vport);
1406 if (err == -EBUSY)
1407 err = -EEXIST;
1408
99769a40 1409 goto err_destroy_percpu;
d6569377 1410 }
d6569377 1411
6455100f
PS
1412 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1413 info->snd_seq, OVS_DP_CMD_NEW);
aaff4b55
BP
1414 err = PTR_ERR(reply);
1415 if (IS_ERR(reply))
1416 goto err_destroy_local_port;
1417
254f2dc8 1418 list_add_tail(&dp->list_node, &dps);
d6569377
BP
1419 dp_sysfs_add_dp(dp);
1420
d6569377
BP
1421 rtnl_unlock();
1422
aaff4b55
BP
1423 genl_notify(reply, genl_info_net(info), info->snd_pid,
1424 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
d6569377
BP
1425 return 0;
1426
1427err_destroy_local_port:
df2c07f4 1428 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
99769a40
JG
1429err_destroy_percpu:
1430 free_percpu(dp->stats_percpu);
d6569377 1431err_destroy_table:
3544358a 1432 flow_tbl_destroy(get_table_protected(dp));
d6569377 1433err_free_dp:
d6569377
BP
1434 kfree(dp);
1435err_put_module:
1436 module_put(THIS_MODULE);
ed099e92 1437err_unlock_rtnl:
d6569377 1438 rtnl_unlock();
d6569377 1439err:
064af421
BP
1440 return err;
1441}
1442
df2c07f4 1443static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
44e05eca 1444{
ed099e92 1445 struct vport *vport, *next_vport;
aaff4b55 1446 struct sk_buff *reply;
9c52546b 1447 struct datapath *dp;
d6569377 1448 int err;
44e05eca 1449
df2c07f4 1450 err = ovs_dp_cmd_validate(info->attrs);
aaff4b55 1451 if (err)
d6569377 1452 goto exit;
44e05eca 1453
d6569377 1454 rtnl_lock();
aaff4b55 1455 dp = lookup_datapath(info->userhdr, info->attrs);
d6569377
BP
1456 err = PTR_ERR(dp);
1457 if (IS_ERR(dp))
aaff4b55
BP
1458 goto exit_unlock;
1459
6455100f
PS
1460 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1461 info->snd_seq, OVS_DP_CMD_DEL);
aaff4b55
BP
1462 err = PTR_ERR(reply);
1463 if (IS_ERR(reply))
1464 goto exit_unlock;
9c52546b 1465
6455100f 1466 list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
df2c07f4 1467 if (vport->port_no != OVSP_LOCAL)
ed099e92
BP
1468 dp_detach_port(vport);
1469
1470 dp_sysfs_del_dp(dp);
254f2dc8 1471 list_del(&dp->list_node);
df2c07f4 1472 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
ed099e92 1473
99620d2c
JG
1474 /* rtnl_unlock() will wait until all the references to devices that
1475 * are pending unregistration have been dropped. We do it here to
1476 * ensure that any internal devices (which contain DP pointers) are
1477 * fully destroyed before freeing the datapath.
1478 */
1479 rtnl_unlock();
1480
ed099e92
BP
1481 call_rcu(&dp->rcu, destroy_dp_rcu);
1482 module_put(THIS_MODULE);
1483
aaff4b55
BP
1484 genl_notify(reply, genl_info_net(info), info->snd_pid,
1485 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
99620d2c
JG
1486
1487 return 0;
d6569377 1488
aaff4b55 1489exit_unlock:
d6569377
BP
1490 rtnl_unlock();
1491exit:
1492 return err;
44e05eca
BP
1493}
1494
df2c07f4 1495static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
064af421 1496{
aaff4b55 1497 struct sk_buff *reply;
d6569377 1498 struct datapath *dp;
d6569377 1499 int err;
064af421 1500
df2c07f4 1501 err = ovs_dp_cmd_validate(info->attrs);
aaff4b55
BP
1502 if (err)
1503 return err;
38c6ecbc 1504
aaff4b55 1505 dp = lookup_datapath(info->userhdr, info->attrs);
d6569377 1506 if (IS_ERR(dp))
aaff4b55 1507 return PTR_ERR(dp);
38c6ecbc 1508
6455100f
PS
1509 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1510 info->snd_seq, OVS_DP_CMD_NEW);
aaff4b55
BP
1511 if (IS_ERR(reply)) {
1512 err = PTR_ERR(reply);
1513 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1514 dp_datapath_multicast_group.id, err);
1515 return 0;
1516 }
1517
1518 genl_notify(reply, genl_info_net(info), info->snd_pid,
1519 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1520 return 0;
064af421
BP
1521}
1522
df2c07f4 1523static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1dcf111b 1524{
aaff4b55 1525 struct sk_buff *reply;
d6569377 1526 struct datapath *dp;
d6569377 1527 int err;
1dcf111b 1528
df2c07f4 1529 err = ovs_dp_cmd_validate(info->attrs);
aaff4b55
BP
1530 if (err)
1531 return err;
1dcf111b 1532
aaff4b55 1533 dp = lookup_datapath(info->userhdr, info->attrs);
d6569377 1534 if (IS_ERR(dp))
aaff4b55 1535 return PTR_ERR(dp);
1dcf111b 1536
6455100f
PS
1537 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1538 info->snd_seq, OVS_DP_CMD_NEW);
aaff4b55
BP
1539 if (IS_ERR(reply))
1540 return PTR_ERR(reply);
1541
1542 return genlmsg_reply(reply, info);
1dcf111b
JP
1543}
1544
df2c07f4 1545static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
a7786963 1546{
254f2dc8
BP
1547 struct datapath *dp;
1548 int skip = cb->args[0];
1549 int i = 0;
a7786963 1550
6455100f 1551 list_for_each_entry(dp, &dps, list_node) {
254f2dc8 1552 if (i < skip)
d6569377 1553 continue;
df2c07f4 1554 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
aaff4b55 1555 cb->nlh->nlmsg_seq, NLM_F_MULTI,
df2c07f4 1556 OVS_DP_CMD_NEW) < 0)
aaff4b55 1557 break;
254f2dc8 1558 i++;
a7786963 1559 }
aaff4b55 1560
254f2dc8
BP
1561 cb->args[0] = i;
1562
aaff4b55 1563 return skb->len;
c19e6535
BP
1564}
1565
aaff4b55 1566static struct genl_ops dp_datapath_genl_ops[] = {
df2c07f4 1567 { .cmd = OVS_DP_CMD_NEW,
aaff4b55
BP
1568 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1569 .policy = datapath_policy,
df2c07f4 1570 .doit = ovs_dp_cmd_new
aaff4b55 1571 },
df2c07f4 1572 { .cmd = OVS_DP_CMD_DEL,
aaff4b55
BP
1573 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1574 .policy = datapath_policy,
df2c07f4 1575 .doit = ovs_dp_cmd_del
aaff4b55 1576 },
df2c07f4 1577 { .cmd = OVS_DP_CMD_GET,
aaff4b55
BP
1578 .flags = 0, /* OK for unprivileged users. */
1579 .policy = datapath_policy,
df2c07f4
JP
1580 .doit = ovs_dp_cmd_get,
1581 .dumpit = ovs_dp_cmd_dump
aaff4b55 1582 },
df2c07f4 1583 { .cmd = OVS_DP_CMD_SET,
aaff4b55
BP
1584 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1585 .policy = datapath_policy,
df2c07f4 1586 .doit = ovs_dp_cmd_set,
aaff4b55
BP
1587 },
1588};
1589
df2c07f4 1590static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
f0fef760 1591#ifdef HAVE_NLA_NUL_STRING
df2c07f4 1592 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
f613a0d7 1593 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
df2c07f4 1594 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
f0fef760 1595#else
f613a0d7 1596 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
df2c07f4 1597 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
f0fef760 1598#endif
d48c88ec
JG
1599 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1600 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
b063d9f0 1601 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
df2c07f4 1602 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
c19e6535
BP
1603};
1604
f0fef760
BP
1605static struct genl_family dp_vport_genl_family = {
1606 .id = GENL_ID_GENERATE,
df2c07f4
JP
1607 .hdrsize = sizeof(struct ovs_header),
1608 .name = OVS_VPORT_FAMILY,
69685a88 1609 .version = OVS_VPORT_VERSION,
df2c07f4 1610 .maxattr = OVS_VPORT_ATTR_MAX
f0fef760
BP
1611};
1612
f14d8083 1613struct genl_multicast_group dp_vport_multicast_group = {
df2c07f4 1614 .name = OVS_VPORT_MCGROUP
f0fef760
BP
1615};
1616
1617/* Called with RTNL lock or RCU read lock. */
df2c07f4 1618static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
f0fef760 1619 u32 pid, u32 seq, u32 flags, u8 cmd)
064af421 1620{
df2c07f4 1621 struct ovs_header *ovs_header;
e926dfe3 1622 struct ovs_vport_stats vport_stats;
c19e6535
BP
1623 int err;
1624
df2c07f4 1625 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
f0fef760 1626 flags, cmd);
df2c07f4 1627 if (!ovs_header)
f0fef760 1628 return -EMSGSIZE;
c19e6535 1629
99769a40 1630 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
c19e6535 1631
df2c07f4 1632 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
16b82e84
JG
1633 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1634 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
b063d9f0 1635 NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
c19e6535 1636
e926dfe3
JG
1637 vport_get_stats(vport, &vport_stats);
1638 NLA_PUT(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1639 &vport_stats);
c19e6535 1640
16b82e84
JG
1641 NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1642 vport->ops->get_addr(vport));
c19e6535 1643
c19e6535 1644 err = vport_get_options(vport, skb);
f0fef760
BP
1645 if (err == -EMSGSIZE)
1646 goto error;
c19e6535 1647
df2c07f4 1648 return genlmsg_end(skb, ovs_header);
c19e6535
BP
1649
1650nla_put_failure:
1651 err = -EMSGSIZE;
f0fef760 1652error:
df2c07f4 1653 genlmsg_cancel(skb, ovs_header);
f0fef760 1654 return err;
064af421
BP
1655}
1656
f0fef760 1657/* Called with RTNL lock or RCU read lock. */
f14d8083
EJ
1658struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1659 u32 seq, u8 cmd)
064af421 1660{
c19e6535 1661 struct sk_buff *skb;
f0fef760 1662 int retval;
c19e6535 1663
f0fef760 1664 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
c19e6535
BP
1665 if (!skb)
1666 return ERR_PTR(-ENOMEM);
1667
df2c07f4 1668 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
f0fef760
BP
1669 if (retval < 0) {
1670 kfree_skb(skb);
1671 return ERR_PTR(retval);
1672 }
c19e6535 1673 return skb;
f0fef760 1674}
c19e6535 1675
df2c07f4 1676static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
f0fef760 1677{
df2c07f4 1678 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
c19e6535 1679}
51d4d598 1680
ed099e92 1681/* Called with RTNL lock or RCU read lock. */
df2c07f4
JP
1682static struct vport *lookup_vport(struct ovs_header *ovs_header,
1683 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
c19e6535
BP
1684{
1685 struct datapath *dp;
1686 struct vport *vport;
1687
df2c07f4
JP
1688 if (a[OVS_VPORT_ATTR_NAME]) {
1689 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
ed099e92 1690 if (!vport)
c19e6535 1691 return ERR_PTR(-ENODEV);
c19e6535 1692 return vport;
df2c07f4
JP
1693 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1694 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
c19e6535
BP
1695
1696 if (port_no >= DP_MAX_PORTS)
f0fef760 1697 return ERR_PTR(-EFBIG);
c19e6535 1698
df2c07f4 1699 dp = get_dp(ovs_header->dp_ifindex);
c19e6535
BP
1700 if (!dp)
1701 return ERR_PTR(-ENODEV);
f2459fe7 1702
c19e6535 1703 vport = get_vport_protected(dp, port_no);
ed099e92 1704 if (!vport)
c19e6535 1705 return ERR_PTR(-ENOENT);
c19e6535
BP
1706 return vport;
1707 } else
1708 return ERR_PTR(-EINVAL);
064af421
BP
1709}
1710
ed099e92 1711/* Called with RTNL lock. */
6455100f
PS
1712static int change_vport(struct vport *vport,
1713 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
064af421 1714{
c19e6535 1715 int err = 0;
f613a0d7 1716
df2c07f4 1717 if (a[OVS_VPORT_ATTR_STATS])
f613a0d7
PS
1718 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1719
1720 if (a[OVS_VPORT_ATTR_ADDRESS])
df2c07f4 1721 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
f613a0d7 1722
c19e6535
BP
1723 return err;
1724}
1725
df2c07f4 1726static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
c19e6535 1727{
f0fef760 1728 struct nlattr **a = info->attrs;
df2c07f4 1729 struct ovs_header *ovs_header = info->userhdr;
c19e6535 1730 struct vport_parms parms;
ed099e92 1731 struct sk_buff *reply;
c19e6535 1732 struct vport *vport;
c19e6535 1733 struct datapath *dp;
b0ec0f27 1734 u32 port_no;
c19e6535 1735 int err;
b0ec0f27 1736
c19e6535 1737 err = -EINVAL;
ea36840f
BP
1738 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1739 !a[OVS_VPORT_ATTR_UPCALL_PID])
f0fef760
BP
1740 goto exit;
1741
df2c07f4 1742 err = ovs_vport_cmd_validate(a);
f0fef760
BP
1743 if (err)
1744 goto exit;
51d4d598 1745
c19e6535 1746 rtnl_lock();
df2c07f4 1747 dp = get_dp(ovs_header->dp_ifindex);
c19e6535
BP
1748 err = -ENODEV;
1749 if (!dp)
ed099e92 1750 goto exit_unlock;
c19e6535 1751
df2c07f4
JP
1752 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1753 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
c19e6535
BP
1754
1755 err = -EFBIG;
1756 if (port_no >= DP_MAX_PORTS)
ed099e92 1757 goto exit_unlock;
c19e6535
BP
1758
1759 vport = get_vport_protected(dp, port_no);
1760 err = -EBUSY;
1761 if (vport)
ed099e92 1762 goto exit_unlock;
c19e6535
BP
1763 } else {
1764 for (port_no = 1; ; port_no++) {
1765 if (port_no >= DP_MAX_PORTS) {
1766 err = -EFBIG;
ed099e92 1767 goto exit_unlock;
c19e6535
BP
1768 }
1769 vport = get_vport_protected(dp, port_no);
1770 if (!vport)
1771 break;
51d4d598 1772 }
064af421 1773 }
b0ec0f27 1774
df2c07f4
JP
1775 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1776 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1777 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
c19e6535
BP
1778 parms.dp = dp;
1779 parms.port_no = port_no;
ea36840f 1780 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
c19e6535
BP
1781
1782 vport = new_vport(&parms);
1783 err = PTR_ERR(vport);
1784 if (IS_ERR(vport))
ed099e92 1785 goto exit_unlock;
c19e6535 1786
6455100f 1787 dp_sysfs_add_if(vport);
c19e6535
BP
1788
1789 err = change_vport(vport, a);
f0fef760 1790 if (!err) {
df2c07f4 1791 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
6455100f
PS
1792 info->snd_seq,
1793 OVS_VPORT_CMD_NEW);
f0fef760
BP
1794 if (IS_ERR(reply))
1795 err = PTR_ERR(reply);
1796 }
c19e6535
BP
1797 if (err) {
1798 dp_detach_port(vport);
ed099e92 1799 goto exit_unlock;
c19e6535 1800 }
f0fef760
BP
1801 genl_notify(reply, genl_info_net(info), info->snd_pid,
1802 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
c19e6535 1803
c19e6535 1804
ed099e92 1805exit_unlock:
c19e6535 1806 rtnl_unlock();
c19e6535
BP
1807exit:
1808 return err;
44e05eca
BP
1809}
1810
df2c07f4 1811static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
44e05eca 1812{
f0fef760
BP
1813 struct nlattr **a = info->attrs;
1814 struct sk_buff *reply;
c19e6535 1815 struct vport *vport;
c19e6535 1816 int err;
44e05eca 1817
df2c07f4 1818 err = ovs_vport_cmd_validate(a);
f0fef760 1819 if (err)
c19e6535
BP
1820 goto exit;
1821
1822 rtnl_lock();
f0fef760 1823 vport = lookup_vport(info->userhdr, a);
c19e6535
BP
1824 err = PTR_ERR(vport);
1825 if (IS_ERR(vport))
f0fef760 1826 goto exit_unlock;
44e05eca 1827
c19e6535 1828 err = 0;
6455100f 1829 if (a[OVS_VPORT_ATTR_TYPE] &&
16b82e84 1830 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
4879d4c7 1831 err = -EINVAL;
6455100f 1832
4879d4c7 1833 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
df2c07f4 1834 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
c19e6535
BP
1835 if (!err)
1836 err = change_vport(vport, a);
b063d9f0
JG
1837 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1838 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
c19e6535 1839
df2c07f4
JP
1840 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1841 OVS_VPORT_CMD_NEW);
f0fef760
BP
1842 if (IS_ERR(reply)) {
1843 err = PTR_ERR(reply);
1844 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1845 dp_vport_multicast_group.id, err);
1846 return 0;
1847 }
1848
1849 genl_notify(reply, genl_info_net(info), info->snd_pid,
1850 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1851
1852exit_unlock:
c19e6535
BP
1853 rtnl_unlock();
1854exit:
1855 return err;
064af421
BP
1856}
1857
df2c07f4 1858static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
7c40efc9 1859{
f0fef760
BP
1860 struct nlattr **a = info->attrs;
1861 struct sk_buff *reply;
c19e6535 1862 struct vport *vport;
c19e6535
BP
1863 int err;
1864
df2c07f4 1865 err = ovs_vport_cmd_validate(a);
f0fef760 1866 if (err)
c19e6535
BP
1867 goto exit;
1868
1869 rtnl_lock();
f0fef760 1870 vport = lookup_vport(info->userhdr, a);
c19e6535 1871 err = PTR_ERR(vport);
f0fef760
BP
1872 if (IS_ERR(vport))
1873 goto exit_unlock;
c19e6535 1874
df2c07f4 1875 if (vport->port_no == OVSP_LOCAL) {
f0fef760
BP
1876 err = -EINVAL;
1877 goto exit_unlock;
1878 }
1879
df2c07f4
JP
1880 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1881 OVS_VPORT_CMD_DEL);
f0fef760
BP
1882 err = PTR_ERR(reply);
1883 if (IS_ERR(reply))
1884 goto exit_unlock;
1885
3544358a 1886 dp_detach_port(vport);
f0fef760
BP
1887
1888 genl_notify(reply, genl_info_net(info), info->snd_pid,
1889 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1890
1891exit_unlock:
c19e6535
BP
1892 rtnl_unlock();
1893exit:
1894 return err;
7c40efc9
BP
1895}
1896
df2c07f4 1897static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
7c40efc9 1898{
f0fef760 1899 struct nlattr **a = info->attrs;
df2c07f4 1900 struct ovs_header *ovs_header = info->userhdr;
ed099e92 1901 struct sk_buff *reply;
c19e6535 1902 struct vport *vport;
c19e6535
BP
1903 int err;
1904
df2c07f4 1905 err = ovs_vport_cmd_validate(a);
f0fef760
BP
1906 if (err)
1907 goto exit;
c19e6535 1908
ed099e92 1909 rcu_read_lock();
df2c07f4 1910 vport = lookup_vport(ovs_header, a);
c19e6535
BP
1911 err = PTR_ERR(vport);
1912 if (IS_ERR(vport))
f0fef760 1913 goto exit_unlock;
c19e6535 1914
df2c07f4
JP
1915 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1916 OVS_VPORT_CMD_NEW);
ed099e92
BP
1917 err = PTR_ERR(reply);
1918 if (IS_ERR(reply))
f0fef760 1919 goto exit_unlock;
ed099e92 1920
df2fa9b5
JG
1921 rcu_read_unlock();
1922
1923 return genlmsg_reply(reply, info);
ed099e92 1924
f0fef760 1925exit_unlock:
ed099e92 1926 rcu_read_unlock();
f0fef760 1927exit:
c19e6535
BP
1928 return err;
1929}
1930
df2c07f4 1931static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
c19e6535 1932{
df2c07f4 1933 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
c19e6535
BP
1934 struct datapath *dp;
1935 u32 port_no;
f0fef760 1936 int retval;
c19e6535 1937
df2c07f4 1938 dp = get_dp(ovs_header->dp_ifindex);
c19e6535 1939 if (!dp)
f0fef760 1940 return -ENODEV;
ed099e92
BP
1941
1942 rcu_read_lock();
f0fef760 1943 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
ed099e92 1944 struct vport *vport;
ed099e92
BP
1945
1946 vport = get_vport_protected(dp, port_no);
1947 if (!vport)
1948 continue;
1949
df2c07f4 1950 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
f0fef760 1951 cb->nlh->nlmsg_seq, NLM_F_MULTI,
df2c07f4 1952 OVS_VPORT_CMD_NEW) < 0)
f0fef760 1953 break;
c19e6535 1954 }
ed099e92 1955 rcu_read_unlock();
c19e6535 1956
f0fef760
BP
1957 cb->args[0] = port_no;
1958 retval = skb->len;
1959
1960 return retval;
7c40efc9
BP
1961}
1962
f0fef760 1963static struct genl_ops dp_vport_genl_ops[] = {
df2c07f4 1964 { .cmd = OVS_VPORT_CMD_NEW,
f0fef760
BP
1965 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1966 .policy = vport_policy,
df2c07f4 1967 .doit = ovs_vport_cmd_new
f0fef760 1968 },
df2c07f4 1969 { .cmd = OVS_VPORT_CMD_DEL,
f0fef760
BP
1970 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1971 .policy = vport_policy,
df2c07f4 1972 .doit = ovs_vport_cmd_del
f0fef760 1973 },
df2c07f4 1974 { .cmd = OVS_VPORT_CMD_GET,
f0fef760
BP
1975 .flags = 0, /* OK for unprivileged users. */
1976 .policy = vport_policy,
df2c07f4
JP
1977 .doit = ovs_vport_cmd_get,
1978 .dumpit = ovs_vport_cmd_dump
f0fef760 1979 },
df2c07f4 1980 { .cmd = OVS_VPORT_CMD_SET,
f0fef760
BP
1981 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1982 .policy = vport_policy,
df2c07f4 1983 .doit = ovs_vport_cmd_set,
f0fef760
BP
1984 },
1985};
1986
982b8810
BP
1987struct genl_family_and_ops {
1988 struct genl_family *family;
1989 struct genl_ops *ops;
1990 int n_ops;
1991 struct genl_multicast_group *group;
1992};
ed099e92 1993
982b8810 1994static const struct genl_family_and_ops dp_genl_families[] = {
aaff4b55
BP
1995 { &dp_datapath_genl_family,
1996 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1997 &dp_datapath_multicast_group },
f0fef760
BP
1998 { &dp_vport_genl_family,
1999 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2000 &dp_vport_multicast_group },
37a1300c
BP
2001 { &dp_flow_genl_family,
2002 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2003 &dp_flow_multicast_group },
982b8810
BP
2004 { &dp_packet_genl_family,
2005 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2006 NULL },
2007};
ed099e92 2008
982b8810
BP
2009static void dp_unregister_genl(int n_families)
2010{
2011 int i;
ed099e92 2012
b867ca75 2013 for (i = 0; i < n_families; i++)
982b8810 2014 genl_unregister_family(dp_genl_families[i].family);
ed099e92
BP
2015}
2016
982b8810 2017static int dp_register_genl(void)
064af421 2018{
982b8810
BP
2019 int n_registered;
2020 int err;
2021 int i;
064af421 2022
982b8810
BP
2023 n_registered = 0;
2024 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2025 const struct genl_family_and_ops *f = &dp_genl_families[i];
064af421 2026
982b8810
BP
2027 err = genl_register_family_with_ops(f->family, f->ops,
2028 f->n_ops);
2029 if (err)
2030 goto error;
2031 n_registered++;
e22d4953 2032
982b8810
BP
2033 if (f->group) {
2034 err = genl_register_mc_group(f->family, f->group);
2035 if (err)
2036 goto error;
2037 }
2038 }
9cc8b4e4 2039
982b8810 2040 return 0;
064af421
BP
2041
2042error:
982b8810
BP
2043 dp_unregister_genl(n_registered);
2044 return err;
064af421
BP
2045}
2046
22d24ebf
BP
2047static int __init dp_init(void)
2048{
f2459fe7 2049 struct sk_buff *dummy_skb;
22d24ebf
BP
2050 int err;
2051
f2459fe7 2052 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
22d24ebf 2053
dc5f3fef 2054 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
6455100f 2055 VERSION BUILDNR);
064af421 2056
3544358a 2057 err = tnl_init();
064af421
BP
2058 if (err)
2059 goto error;
2060
3544358a
PS
2061 err = flow_init();
2062 if (err)
2063 goto error_tnl_exit;
2064
f2459fe7 2065 err = vport_init();
064af421
BP
2066 if (err)
2067 goto error_flow_exit;
2068
f2459fe7
JG
2069 err = register_netdevice_notifier(&dp_device_notifier);
2070 if (err)
2071 goto error_vport_exit;
2072
982b8810
BP
2073 err = dp_register_genl();
2074 if (err < 0)
37a1300c 2075 goto error_unreg_notifier;
982b8810 2076
064af421
BP
2077 return 0;
2078
2079error_unreg_notifier:
2080 unregister_netdevice_notifier(&dp_device_notifier);
f2459fe7
JG
2081error_vport_exit:
2082 vport_exit();
064af421
BP
2083error_flow_exit:
2084 flow_exit();
3544358a
PS
2085error_tnl_exit:
2086 tnl_exit();
064af421
BP
2087error:
2088 return err;
2089}
2090
2091static void dp_cleanup(void)
2092{
2093 rcu_barrier();
982b8810 2094 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
064af421 2095 unregister_netdevice_notifier(&dp_device_notifier);
f2459fe7 2096 vport_exit();
064af421 2097 flow_exit();
3544358a 2098 tnl_exit();
064af421
BP
2099}
2100
2101module_init(dp_init);
2102module_exit(dp_cleanup);
2103
2104MODULE_DESCRIPTION("Open vSwitch switching datapath");
2105MODULE_LICENSE("GPL");