1 /* 2 * Copyright (c) 2007-2013 Nicira, Inc. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of version 2 of the GNU General Public 6 * License as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program; if not, write to the Free Software 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16 * 02110-1301, USA 17 */ 18 19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 20 21 #include <linux/init.h> 22 #include <linux/module.h> 23 #include <linux/if_arp.h> 24 #include <linux/if_vlan.h> 25 #include <linux/in.h> 26 #include <linux/ip.h> 27 #include <linux/jhash.h> 28 #include <linux/delay.h> 29 #include <linux/time.h> 30 #include <linux/etherdevice.h> 31 #include <linux/genetlink.h> 32 #include <linux/kernel.h> 33 #include <linux/kthread.h> 34 #include <linux/mutex.h> 35 #include <linux/percpu.h> 36 #include <linux/rcupdate.h> 37 #include <linux/tcp.h> 38 #include <linux/udp.h> 39 #include <linux/ethtool.h> 40 #include <linux/wait.h> 41 #include <asm/div64.h> 42 #include <linux/highmem.h> 43 #include <linux/netfilter_bridge.h> 44 #include <linux/netfilter_ipv4.h> 45 #include <linux/inetdevice.h> 46 #include <linux/list.h> 47 #include <linux/lockdep.h> 48 #include <linux/openvswitch.h> 49 #include <linux/rculist.h> 50 #include <linux/dmi.h> 51 #include <linux/workqueue.h> 52 #include <net/genetlink.h> 53 #include <net/net_namespace.h> 54 #include <net/netns/generic.h> 55 56 #include "datapath.h" 57 #include "flow.h" 58 #include "flow_netlink.h" 59 #include "vport-internal_dev.h" 60 #include "vport-netdev.h" 61 62 int ovs_net_id __read_mostly; 63 64 static void ovs_notify(struct sk_buff *skb, struct genl_info *info, 65 struct genl_multicast_group *grp) 66 { 67 genl_notify(skb, genl_info_net(info), info->snd_portid, 68 grp->id, info->nlhdr, GFP_KERNEL); 69 } 70 71 /** 72 * DOC: Locking: 73 * 74 * All writes e.g. Writes to device state (add/remove datapath, port, set 75 * operations on vports, etc.), Writes to other state (flow table 76 * modifications, set miscellaneous datapath parameters, etc.) are protected 77 * by ovs_lock. 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. 84 * 85 * The RTNL lock nests inside ovs_mutex. 86 */ 87 88 static DEFINE_MUTEX(ovs_mutex); 89 90 void ovs_lock(void) 91 { 92 mutex_lock(&ovs_mutex); 93 } 94 95 void ovs_unlock(void) 96 { 97 mutex_unlock(&ovs_mutex); 98 } 99 100 #ifdef CONFIG_LOCKDEP 101 int lockdep_ovsl_is_held(void) 102 { 103 if (debug_locks) 104 return lockdep_is_held(&ovs_mutex); 105 else 106 return 1; 107 } 108 #endif 109 110 static struct vport *new_vport(const struct vport_parms *); 111 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *, 112 const struct dp_upcall_info *); 113 static int queue_userspace_packet(struct net *, int dp_ifindex, 114 struct sk_buff *, 115 const struct dp_upcall_info *); 116 117 /* Must be called with rcu_read_lock or ovs_mutex. */ 118 static struct datapath *get_dp(struct net *net, int dp_ifindex) 119 { 120 struct datapath *dp = NULL; 121 struct net_device *dev; 122 123 rcu_read_lock(); 124 dev = dev_get_by_index_rcu(net, dp_ifindex); 125 if (dev) { 126 struct vport *vport = ovs_internal_dev_get_vport(dev); 127 if (vport) 128 dp = vport->dp; 129 } 130 rcu_read_unlock(); 131 132 return dp; 133 } 134 135 /* Must be called with rcu_read_lock or ovs_mutex. */ 136 const char *ovs_dp_name(const struct datapath *dp) 137 { 138 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL); 139 return vport->ops->get_name(vport); 140 } 141 142 static int get_dpifindex(struct datapath *dp) 143 { 144 struct vport *local; 145 int ifindex; 146 147 rcu_read_lock(); 148 149 local = ovs_vport_rcu(dp, OVSP_LOCAL); 150 if (local) 151 ifindex = netdev_vport_priv(local)->dev->ifindex; 152 else 153 ifindex = 0; 154 155 rcu_read_unlock(); 156 157 return ifindex; 158 } 159 160 static void destroy_dp_rcu(struct rcu_head *rcu) 161 { 162 struct datapath *dp = container_of(rcu, struct datapath, rcu); 163 164 ovs_flow_tbl_destroy(&dp->table); 165 free_percpu(dp->stats_percpu); 166 release_net(ovs_dp_get_net(dp)); 167 kfree(dp->ports); 168 kfree(dp); 169 } 170 171 static struct hlist_head *vport_hash_bucket(const struct datapath *dp, 172 u16 port_no) 173 { 174 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)]; 175 } 176 177 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no) 178 { 179 struct vport *vport; 180 struct hlist_head *head; 181 182 head = vport_hash_bucket(dp, port_no); 183 hlist_for_each_entry_rcu(vport, head, dp_hash_node) { 184 if (vport->port_no == port_no) 185 return vport; 186 } 187 return NULL; 188 } 189 190 /* Called with ovs_mutex. */ 191 static struct vport *new_vport(const struct vport_parms *parms) 192 { 193 struct vport *vport; 194 195 vport = ovs_vport_add(parms); 196 if (!IS_ERR(vport)) { 197 struct datapath *dp = parms->dp; 198 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no); 199 200 hlist_add_head_rcu(&vport->dp_hash_node, head); 201 } 202 return vport; 203 } 204 205 void ovs_dp_detach_port(struct vport *p) 206 { 207 ASSERT_OVSL(); 208 209 /* First drop references to device. */ 210 hlist_del_rcu(&p->dp_hash_node); 211 212 /* Then destroy it. */ 213 ovs_vport_del(p); 214 } 215 216 /* Must be called with rcu_read_lock. */ 217 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb) 218 { 219 struct datapath *dp = p->dp; 220 struct sw_flow *flow; 221 struct dp_stats_percpu *stats; 222 struct sw_flow_key key; 223 u64 *stats_counter; 224 int error; 225 226 stats = this_cpu_ptr(dp->stats_percpu); 227 228 /* Extract flow from 'skb' into 'key'. */ 229 error = ovs_flow_extract(skb, p->port_no, &key); 230 if (unlikely(error)) { 231 kfree_skb(skb); 232 return; 233 } 234 235 /* Look up flow. */ 236 flow = ovs_flow_tbl_lookup(&dp->table, &key); 237 if (unlikely(!flow)) { 238 struct dp_upcall_info upcall; 239 240 upcall.cmd = OVS_PACKET_CMD_MISS; 241 upcall.key = &key; 242 upcall.userdata = NULL; 243 upcall.portid = p->upcall_portid; 244 ovs_dp_upcall(dp, skb, &upcall); 245 consume_skb(skb); 246 stats_counter = &stats->n_missed; 247 goto out; 248 } 249 250 OVS_CB(skb)->flow = flow; 251 OVS_CB(skb)->pkt_key = &key; 252 253 stats_counter = &stats->n_hit; 254 ovs_flow_used(OVS_CB(skb)->flow, skb); 255 ovs_execute_actions(dp, skb); 256 257 out: 258 /* Update datapath statistics. */ 259 u64_stats_update_begin(&stats->sync); 260 (*stats_counter)++; 261 u64_stats_update_end(&stats->sync); 262 } 263 264 static struct genl_family dp_packet_genl_family = { 265 .id = GENL_ID_GENERATE, 266 .hdrsize = sizeof(struct ovs_header), 267 .name = OVS_PACKET_FAMILY, 268 .version = OVS_PACKET_VERSION, 269 .maxattr = OVS_PACKET_ATTR_MAX, 270 .netnsok = true, 271 .parallel_ops = true, 272 }; 273 274 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, 275 const struct dp_upcall_info *upcall_info) 276 { 277 struct dp_stats_percpu *stats; 278 int dp_ifindex; 279 int err; 280 281 if (upcall_info->portid == 0) { 282 err = -ENOTCONN; 283 goto err; 284 } 285 286 dp_ifindex = get_dpifindex(dp); 287 if (!dp_ifindex) { 288 err = -ENODEV; 289 goto err; 290 } 291 292 if (!skb_is_gso(skb)) 293 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info); 294 else 295 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info); 296 if (err) 297 goto err; 298 299 return 0; 300 301 err: 302 stats = this_cpu_ptr(dp->stats_percpu); 303 304 u64_stats_update_begin(&stats->sync); 305 stats->n_lost++; 306 u64_stats_update_end(&stats->sync); 307 308 return err; 309 } 310 311 static int queue_gso_packets(struct net *net, int dp_ifindex, 312 struct sk_buff *skb, 313 const struct dp_upcall_info *upcall_info) 314 { 315 unsigned short gso_type = skb_shinfo(skb)->gso_type; 316 struct dp_upcall_info later_info; 317 struct sw_flow_key later_key; 318 struct sk_buff *segs, *nskb; 319 int err; 320 321 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false); 322 if (IS_ERR(segs)) 323 return PTR_ERR(segs); 324 325 /* Queue all of the segments. */ 326 skb = segs; 327 do { 328 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info); 329 if (err) 330 break; 331 332 if (skb == segs && gso_type & SKB_GSO_UDP) { 333 /* The initial flow key extracted by ovs_flow_extract() 334 * in this case is for a first fragment, so we need to 335 * properly mark later fragments. 336 */ 337 later_key = *upcall_info->key; 338 later_key.ip.frag = OVS_FRAG_TYPE_LATER; 339 340 later_info = *upcall_info; 341 later_info.key = &later_key; 342 upcall_info = &later_info; 343 } 344 } while ((skb = skb->next)); 345 346 /* Free all of the segments. */ 347 skb = segs; 348 do { 349 nskb = skb->next; 350 if (err) 351 kfree_skb(skb); 352 else 353 consume_skb(skb); 354 } while ((skb = nskb)); 355 return err; 356 } 357 358 static size_t key_attr_size(void) 359 { 360 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */ 361 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */ 362 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */ 363 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */ 364 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */ 365 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */ 366 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */ 367 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */ 368 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */ 369 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */ 370 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */ 371 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */ 372 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ 373 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */ 374 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */ 375 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ 376 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */ 377 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */ 378 + nla_total_size(28); /* OVS_KEY_ATTR_ND */ 379 } 380 381 static size_t upcall_msg_size(const struct sk_buff *skb, 382 const struct nlattr *userdata) 383 { 384 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header)) 385 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */ 386 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */ 387 388 /* OVS_PACKET_ATTR_USERDATA */ 389 if (userdata) 390 size += NLA_ALIGN(userdata->nla_len); 391 392 return size; 393 } 394 395 static int queue_userspace_packet(struct net *net, int dp_ifindex, 396 struct sk_buff *skb, 397 const struct dp_upcall_info *upcall_info) 398 { 399 struct ovs_header *upcall; 400 struct sk_buff *nskb = NULL; 401 struct sk_buff *user_skb; /* to be queued to userspace */ 402 struct nlattr *nla; 403 int err; 404 405 if (vlan_tx_tag_present(skb)) { 406 nskb = skb_clone(skb, GFP_ATOMIC); 407 if (!nskb) 408 return -ENOMEM; 409 410 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb)); 411 if (!nskb) 412 return -ENOMEM; 413 414 nskb->vlan_tci = 0; 415 skb = nskb; 416 } 417 418 if (nla_attr_size(skb->len) > USHRT_MAX) { 419 err = -EFBIG; 420 goto out; 421 } 422 423 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC); 424 if (!user_skb) { 425 err = -ENOMEM; 426 goto out; 427 } 428 429 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, 430 0, upcall_info->cmd); 431 upcall->dp_ifindex = dp_ifindex; 432 433 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY); 434 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb); 435 nla_nest_end(user_skb, nla); 436 437 if (upcall_info->userdata) 438 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA, 439 nla_len(upcall_info->userdata), 440 nla_data(upcall_info->userdata)); 441 442 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len); 443 444 skb_copy_and_csum_dev(skb, nla_data(nla)); 445 446 genlmsg_end(user_skb, upcall); 447 err = genlmsg_unicast(net, user_skb, upcall_info->portid); 448 449 out: 450 kfree_skb(nskb); 451 return err; 452 } 453 454 static void clear_stats(struct sw_flow *flow) 455 { 456 flow->used = 0; 457 flow->tcp_flags = 0; 458 flow->packet_count = 0; 459 flow->byte_count = 0; 460 } 461 462 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info) 463 { 464 struct ovs_header *ovs_header = info->userhdr; 465 struct nlattr **a = info->attrs; 466 struct sw_flow_actions *acts; 467 struct sk_buff *packet; 468 struct sw_flow *flow; 469 struct datapath *dp; 470 struct ethhdr *eth; 471 int len; 472 int err; 473 474 err = -EINVAL; 475 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] || 476 !a[OVS_PACKET_ATTR_ACTIONS]) 477 goto err; 478 479 len = nla_len(a[OVS_PACKET_ATTR_PACKET]); 480 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL); 481 err = -ENOMEM; 482 if (!packet) 483 goto err; 484 skb_reserve(packet, NET_IP_ALIGN); 485 486 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len); 487 488 skb_reset_mac_header(packet); 489 eth = eth_hdr(packet); 490 491 /* Normally, setting the skb 'protocol' field would be handled by a 492 * call to eth_type_trans(), but it assumes there's a sending 493 * device, which we may not have. */ 494 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) 495 packet->protocol = eth->h_proto; 496 else 497 packet->protocol = htons(ETH_P_802_2); 498 499 /* Build an sw_flow for sending this packet. */ 500 flow = ovs_flow_alloc(); 501 err = PTR_ERR(flow); 502 if (IS_ERR(flow)) 503 goto err_kfree_skb; 504 505 err = ovs_flow_extract(packet, -1, &flow->key); 506 if (err) 507 goto err_flow_free; 508 509 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]); 510 if (err) 511 goto err_flow_free; 512 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS])); 513 err = PTR_ERR(acts); 514 if (IS_ERR(acts)) 515 goto err_flow_free; 516 517 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], 518 &flow->key, 0, &acts); 519 rcu_assign_pointer(flow->sf_acts, acts); 520 if (err) 521 goto err_flow_free; 522 523 OVS_CB(packet)->flow = flow; 524 OVS_CB(packet)->pkt_key = &flow->key; 525 packet->priority = flow->key.phy.priority; 526 packet->mark = flow->key.phy.skb_mark; 527 528 rcu_read_lock(); 529 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 530 err = -ENODEV; 531 if (!dp) 532 goto err_unlock; 533 534 local_bh_disable(); 535 err = ovs_execute_actions(dp, packet); 536 local_bh_enable(); 537 rcu_read_unlock(); 538 539 ovs_flow_free(flow, false); 540 return err; 541 542 err_unlock: 543 rcu_read_unlock(); 544 err_flow_free: 545 ovs_flow_free(flow, false); 546 err_kfree_skb: 547 kfree_skb(packet); 548 err: 549 return err; 550 } 551 552 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = { 553 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN }, 554 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED }, 555 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED }, 556 }; 557 558 static struct genl_ops dp_packet_genl_ops[] = { 559 { .cmd = OVS_PACKET_CMD_EXECUTE, 560 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 561 .policy = packet_policy, 562 .doit = ovs_packet_cmd_execute 563 } 564 }; 565 566 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats) 567 { 568 int i; 569 570 stats->n_flows = ovs_flow_tbl_count(&dp->table); 571 572 stats->n_hit = stats->n_missed = stats->n_lost = 0; 573 for_each_possible_cpu(i) { 574 const struct dp_stats_percpu *percpu_stats; 575 struct dp_stats_percpu local_stats; 576 unsigned int start; 577 578 percpu_stats = per_cpu_ptr(dp->stats_percpu, i); 579 580 do { 581 start = u64_stats_fetch_begin_bh(&percpu_stats->sync); 582 local_stats = *percpu_stats; 583 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start)); 584 585 stats->n_hit += local_stats.n_hit; 586 stats->n_missed += local_stats.n_missed; 587 stats->n_lost += local_stats.n_lost; 588 } 589 } 590 591 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { 592 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED }, 593 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED }, 594 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG }, 595 }; 596 597 static struct genl_family dp_flow_genl_family = { 598 .id = GENL_ID_GENERATE, 599 .hdrsize = sizeof(struct ovs_header), 600 .name = OVS_FLOW_FAMILY, 601 .version = OVS_FLOW_VERSION, 602 .maxattr = OVS_FLOW_ATTR_MAX, 603 .netnsok = true, 604 .parallel_ops = true, 605 }; 606 607 static struct genl_multicast_group ovs_dp_flow_multicast_group = { 608 .name = OVS_FLOW_MCGROUP 609 }; 610 611 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts) 612 { 613 return NLMSG_ALIGN(sizeof(struct ovs_header)) 614 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */ 615 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */ 616 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */ 617 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */ 618 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */ 619 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */ 620 } 621 622 /* Called with ovs_mutex. */ 623 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp, 624 struct sk_buff *skb, u32 portid, 625 u32 seq, u32 flags, u8 cmd) 626 { 627 const int skb_orig_len = skb->len; 628 struct nlattr *start; 629 struct ovs_flow_stats stats; 630 struct ovs_header *ovs_header; 631 struct nlattr *nla; 632 unsigned long used; 633 u8 tcp_flags; 634 int err; 635 636 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd); 637 if (!ovs_header) 638 return -EMSGSIZE; 639 640 ovs_header->dp_ifindex = get_dpifindex(dp); 641 642 /* Fill flow key. */ 643 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY); 644 if (!nla) 645 goto nla_put_failure; 646 647 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb); 648 if (err) 649 goto error; 650 nla_nest_end(skb, nla); 651 652 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK); 653 if (!nla) 654 goto nla_put_failure; 655 656 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb); 657 if (err) 658 goto error; 659 660 nla_nest_end(skb, nla); 661 662 spin_lock_bh(&flow->lock); 663 used = flow->used; 664 stats.n_packets = flow->packet_count; 665 stats.n_bytes = flow->byte_count; 666 tcp_flags = flow->tcp_flags; 667 spin_unlock_bh(&flow->lock); 668 669 if (used && 670 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used))) 671 goto nla_put_failure; 672 673 if (stats.n_packets && 674 nla_put(skb, OVS_FLOW_ATTR_STATS, 675 sizeof(struct ovs_flow_stats), &stats)) 676 goto nla_put_failure; 677 678 if (tcp_flags && 679 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags)) 680 goto nla_put_failure; 681 682 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if 683 * this is the first flow to be dumped into 'skb'. This is unusual for 684 * Netlink but individual action lists can be longer than 685 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this. 686 * The userspace caller can always fetch the actions separately if it 687 * really wants them. (Most userspace callers in fact don't care.) 688 * 689 * This can only fail for dump operations because the skb is always 690 * properly sized for single flows. 691 */ 692 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS); 693 if (start) { 694 const struct sw_flow_actions *sf_acts; 695 696 sf_acts = rcu_dereference_check(flow->sf_acts, 697 lockdep_ovsl_is_held()); 698 699 err = ovs_nla_put_actions(sf_acts->actions, 700 sf_acts->actions_len, skb); 701 if (!err) 702 nla_nest_end(skb, start); 703 else { 704 if (skb_orig_len) 705 goto error; 706 707 nla_nest_cancel(skb, start); 708 } 709 } else if (skb_orig_len) 710 goto nla_put_failure; 711 712 return genlmsg_end(skb, ovs_header); 713 714 nla_put_failure: 715 err = -EMSGSIZE; 716 error: 717 genlmsg_cancel(skb, ovs_header); 718 return err; 719 } 720 721 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow) 722 { 723 const struct sw_flow_actions *sf_acts; 724 725 sf_acts = ovsl_dereference(flow->sf_acts); 726 727 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL); 728 } 729 730 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow, 731 struct datapath *dp, 732 u32 portid, u32 seq, u8 cmd) 733 { 734 struct sk_buff *skb; 735 int retval; 736 737 skb = ovs_flow_cmd_alloc_info(flow); 738 if (!skb) 739 return ERR_PTR(-ENOMEM); 740 741 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd); 742 BUG_ON(retval < 0); 743 return skb; 744 } 745 746 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info) 747 { 748 struct nlattr **a = info->attrs; 749 struct ovs_header *ovs_header = info->userhdr; 750 struct sw_flow_key key, masked_key; 751 struct sw_flow *flow = NULL; 752 struct sw_flow_mask mask; 753 struct sk_buff *reply; 754 struct datapath *dp; 755 struct sw_flow_actions *acts = NULL; 756 struct sw_flow_match match; 757 int error; 758 759 /* Extract key. */ 760 error = -EINVAL; 761 if (!a[OVS_FLOW_ATTR_KEY]) 762 goto error; 763 764 ovs_match_init(&match, &key, &mask); 765 error = ovs_nla_get_match(&match, 766 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]); 767 if (error) 768 goto error; 769 770 /* Validate actions. */ 771 if (a[OVS_FLOW_ATTR_ACTIONS]) { 772 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS])); 773 error = PTR_ERR(acts); 774 if (IS_ERR(acts)) 775 goto error; 776 777 ovs_flow_mask_key(&masked_key, &key, &mask); 778 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], 779 &masked_key, 0, &acts); 780 if (error) { 781 OVS_NLERR("Flow actions may not be safe on all matching packets.\n"); 782 goto err_kfree; 783 } 784 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) { 785 error = -EINVAL; 786 goto error; 787 } 788 789 ovs_lock(); 790 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 791 error = -ENODEV; 792 if (!dp) 793 goto err_unlock_ovs; 794 795 /* Check if this is a duplicate flow */ 796 flow = ovs_flow_tbl_lookup(&dp->table, &key); 797 if (!flow) { 798 /* Bail out if we're not allowed to create a new flow. */ 799 error = -ENOENT; 800 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET) 801 goto err_unlock_ovs; 802 803 /* Allocate flow. */ 804 flow = ovs_flow_alloc(); 805 if (IS_ERR(flow)) { 806 error = PTR_ERR(flow); 807 goto err_unlock_ovs; 808 } 809 clear_stats(flow); 810 811 flow->key = masked_key; 812 flow->unmasked_key = key; 813 rcu_assign_pointer(flow->sf_acts, acts); 814 815 /* Put flow in bucket. */ 816 error = ovs_flow_tbl_insert(&dp->table, flow, &mask); 817 if (error) { 818 acts = NULL; 819 goto err_flow_free; 820 } 821 822 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, 823 info->snd_seq, OVS_FLOW_CMD_NEW); 824 } else { 825 /* We found a matching flow. */ 826 struct sw_flow_actions *old_acts; 827 828 /* Bail out if we're not allowed to modify an existing flow. 829 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL 830 * because Generic Netlink treats the latter as a dump 831 * request. We also accept NLM_F_EXCL in case that bug ever 832 * gets fixed. 833 */ 834 error = -EEXIST; 835 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW && 836 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) 837 goto err_unlock_ovs; 838 839 /* The unmasked key has to be the same for flow updates. */ 840 error = -EINVAL; 841 if (!ovs_flow_cmp_unmasked_key(flow, &match)) { 842 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n"); 843 goto err_unlock_ovs; 844 } 845 846 /* Update actions. */ 847 old_acts = ovsl_dereference(flow->sf_acts); 848 rcu_assign_pointer(flow->sf_acts, acts); 849 ovs_nla_free_flow_actions(old_acts); 850 851 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, 852 info->snd_seq, OVS_FLOW_CMD_NEW); 853 854 /* Clear stats. */ 855 if (a[OVS_FLOW_ATTR_CLEAR]) { 856 spin_lock_bh(&flow->lock); 857 clear_stats(flow); 858 spin_unlock_bh(&flow->lock); 859 } 860 } 861 ovs_unlock(); 862 863 if (!IS_ERR(reply)) 864 ovs_notify(reply, info, &ovs_dp_flow_multicast_group); 865 else 866 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 867 ovs_dp_flow_multicast_group.id, PTR_ERR(reply)); 868 return 0; 869 870 err_flow_free: 871 ovs_flow_free(flow, false); 872 err_unlock_ovs: 873 ovs_unlock(); 874 err_kfree: 875 kfree(acts); 876 error: 877 return error; 878 } 879 880 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info) 881 { 882 struct nlattr **a = info->attrs; 883 struct ovs_header *ovs_header = info->userhdr; 884 struct sw_flow_key key; 885 struct sk_buff *reply; 886 struct sw_flow *flow; 887 struct datapath *dp; 888 struct sw_flow_match match; 889 int err; 890 891 if (!a[OVS_FLOW_ATTR_KEY]) { 892 OVS_NLERR("Flow get message rejected, Key attribute missing.\n"); 893 return -EINVAL; 894 } 895 896 ovs_match_init(&match, &key, NULL); 897 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL); 898 if (err) 899 return err; 900 901 ovs_lock(); 902 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 903 if (!dp) { 904 err = -ENODEV; 905 goto unlock; 906 } 907 908 flow = ovs_flow_tbl_lookup(&dp->table, &key); 909 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) { 910 err = -ENOENT; 911 goto unlock; 912 } 913 914 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, 915 info->snd_seq, OVS_FLOW_CMD_NEW); 916 if (IS_ERR(reply)) { 917 err = PTR_ERR(reply); 918 goto unlock; 919 } 920 921 ovs_unlock(); 922 return genlmsg_reply(reply, info); 923 unlock: 924 ovs_unlock(); 925 return err; 926 } 927 928 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) 929 { 930 struct nlattr **a = info->attrs; 931 struct ovs_header *ovs_header = info->userhdr; 932 struct sw_flow_key key; 933 struct sk_buff *reply; 934 struct sw_flow *flow; 935 struct datapath *dp; 936 struct sw_flow_match match; 937 int err; 938 939 ovs_lock(); 940 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 941 if (!dp) { 942 err = -ENODEV; 943 goto unlock; 944 } 945 946 if (!a[OVS_FLOW_ATTR_KEY]) { 947 err = ovs_flow_tbl_flush(&dp->table); 948 goto unlock; 949 } 950 951 ovs_match_init(&match, &key, NULL); 952 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL); 953 if (err) 954 goto unlock; 955 956 flow = ovs_flow_tbl_lookup(&dp->table, &key); 957 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) { 958 err = -ENOENT; 959 goto unlock; 960 } 961 962 reply = ovs_flow_cmd_alloc_info(flow); 963 if (!reply) { 964 err = -ENOMEM; 965 goto unlock; 966 } 967 968 ovs_flow_tbl_remove(&dp->table, flow); 969 970 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid, 971 info->snd_seq, 0, OVS_FLOW_CMD_DEL); 972 BUG_ON(err < 0); 973 974 ovs_flow_free(flow, true); 975 ovs_unlock(); 976 977 ovs_notify(reply, info, &ovs_dp_flow_multicast_group); 978 return 0; 979 unlock: 980 ovs_unlock(); 981 return err; 982 } 983 984 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 985 { 986 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); 987 struct table_instance *ti; 988 struct datapath *dp; 989 990 rcu_read_lock(); 991 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 992 if (!dp) { 993 rcu_read_unlock(); 994 return -ENODEV; 995 } 996 997 ti = rcu_dereference(dp->table.ti); 998 for (;;) { 999 struct sw_flow *flow; 1000 u32 bucket, obj; 1001 1002 bucket = cb->args[0]; 1003 obj = cb->args[1]; 1004 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj); 1005 if (!flow) 1006 break; 1007 1008 if (ovs_flow_cmd_fill_info(flow, dp, skb, 1009 NETLINK_CB(cb->skb).portid, 1010 cb->nlh->nlmsg_seq, NLM_F_MULTI, 1011 OVS_FLOW_CMD_NEW) < 0) 1012 break; 1013 1014 cb->args[0] = bucket; 1015 cb->args[1] = obj; 1016 } 1017 rcu_read_unlock(); 1018 return skb->len; 1019 } 1020 1021 static struct genl_ops dp_flow_genl_ops[] = { 1022 { .cmd = OVS_FLOW_CMD_NEW, 1023 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1024 .policy = flow_policy, 1025 .doit = ovs_flow_cmd_new_or_set 1026 }, 1027 { .cmd = OVS_FLOW_CMD_DEL, 1028 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1029 .policy = flow_policy, 1030 .doit = ovs_flow_cmd_del 1031 }, 1032 { .cmd = OVS_FLOW_CMD_GET, 1033 .flags = 0, /* OK for unprivileged users. */ 1034 .policy = flow_policy, 1035 .doit = ovs_flow_cmd_get, 1036 .dumpit = ovs_flow_cmd_dump 1037 }, 1038 { .cmd = OVS_FLOW_CMD_SET, 1039 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1040 .policy = flow_policy, 1041 .doit = ovs_flow_cmd_new_or_set, 1042 }, 1043 }; 1044 1045 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { 1046 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, 1047 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 }, 1048 }; 1049 1050 static struct genl_family dp_datapath_genl_family = { 1051 .id = GENL_ID_GENERATE, 1052 .hdrsize = sizeof(struct ovs_header), 1053 .name = OVS_DATAPATH_FAMILY, 1054 .version = OVS_DATAPATH_VERSION, 1055 .maxattr = OVS_DP_ATTR_MAX, 1056 .netnsok = true, 1057 .parallel_ops = true, 1058 }; 1059 1060 static struct genl_multicast_group ovs_dp_datapath_multicast_group = { 1061 .name = OVS_DATAPATH_MCGROUP 1062 }; 1063 1064 static size_t ovs_dp_cmd_msg_size(void) 1065 { 1066 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header)); 1067 1068 msgsize += nla_total_size(IFNAMSIZ); 1069 msgsize += nla_total_size(sizeof(struct ovs_dp_stats)); 1070 1071 return msgsize; 1072 } 1073 1074 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb, 1075 u32 portid, u32 seq, u32 flags, u8 cmd) 1076 { 1077 struct ovs_header *ovs_header; 1078 struct ovs_dp_stats dp_stats; 1079 int err; 1080 1081 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, 1082 flags, cmd); 1083 if (!ovs_header) 1084 goto error; 1085 1086 ovs_header->dp_ifindex = get_dpifindex(dp); 1087 1088 rcu_read_lock(); 1089 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp)); 1090 rcu_read_unlock(); 1091 if (err) 1092 goto nla_put_failure; 1093 1094 get_dp_stats(dp, &dp_stats); 1095 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats)) 1096 goto nla_put_failure; 1097 1098 return genlmsg_end(skb, ovs_header); 1099 1100 nla_put_failure: 1101 genlmsg_cancel(skb, ovs_header); 1102 error: 1103 return -EMSGSIZE; 1104 } 1105 1106 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid, 1107 u32 seq, u8 cmd) 1108 { 1109 struct sk_buff *skb; 1110 int retval; 1111 1112 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL); 1113 if (!skb) 1114 return ERR_PTR(-ENOMEM); 1115 1116 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd); 1117 if (retval < 0) { 1118 kfree_skb(skb); 1119 return ERR_PTR(retval); 1120 } 1121 return skb; 1122 } 1123 1124 /* Called with ovs_mutex. */ 1125 static struct datapath *lookup_datapath(struct net *net, 1126 struct ovs_header *ovs_header, 1127 struct nlattr *a[OVS_DP_ATTR_MAX + 1]) 1128 { 1129 struct datapath *dp; 1130 1131 if (!a[OVS_DP_ATTR_NAME]) 1132 dp = get_dp(net, ovs_header->dp_ifindex); 1133 else { 1134 struct vport *vport; 1135 1136 rcu_read_lock(); 1137 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME])); 1138 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL; 1139 rcu_read_unlock(); 1140 } 1141 return dp ? dp : ERR_PTR(-ENODEV); 1142 } 1143 1144 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) 1145 { 1146 struct nlattr **a = info->attrs; 1147 struct vport_parms parms; 1148 struct sk_buff *reply; 1149 struct datapath *dp; 1150 struct vport *vport; 1151 struct ovs_net *ovs_net; 1152 int err, i; 1153 1154 err = -EINVAL; 1155 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) 1156 goto err; 1157 1158 ovs_lock(); 1159 1160 err = -ENOMEM; 1161 dp = kzalloc(sizeof(*dp), GFP_KERNEL); 1162 if (dp == NULL) 1163 goto err_unlock_ovs; 1164 1165 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk))); 1166 1167 /* Allocate table. */ 1168 err = ovs_flow_tbl_init(&dp->table); 1169 if (err) 1170 goto err_free_dp; 1171 1172 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu); 1173 if (!dp->stats_percpu) { 1174 err = -ENOMEM; 1175 goto err_destroy_table; 1176 } 1177 1178 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head), 1179 GFP_KERNEL); 1180 if (!dp->ports) { 1181 err = -ENOMEM; 1182 goto err_destroy_percpu; 1183 } 1184 1185 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) 1186 INIT_HLIST_HEAD(&dp->ports[i]); 1187 1188 /* Set up our datapath device. */ 1189 parms.name = nla_data(a[OVS_DP_ATTR_NAME]); 1190 parms.type = OVS_VPORT_TYPE_INTERNAL; 1191 parms.options = NULL; 1192 parms.dp = dp; 1193 parms.port_no = OVSP_LOCAL; 1194 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]); 1195 1196 vport = new_vport(&parms); 1197 if (IS_ERR(vport)) { 1198 err = PTR_ERR(vport); 1199 if (err == -EBUSY) 1200 err = -EEXIST; 1201 1202 goto err_destroy_ports_array; 1203 } 1204 1205 reply = ovs_dp_cmd_build_info(dp, info->snd_portid, 1206 info->snd_seq, OVS_DP_CMD_NEW); 1207 err = PTR_ERR(reply); 1208 if (IS_ERR(reply)) 1209 goto err_destroy_local_port; 1210 1211 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id); 1212 list_add_tail_rcu(&dp->list_node, &ovs_net->dps); 1213 1214 ovs_unlock(); 1215 1216 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); 1217 return 0; 1218 1219 err_destroy_local_port: 1220 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); 1221 err_destroy_ports_array: 1222 kfree(dp->ports); 1223 err_destroy_percpu: 1224 free_percpu(dp->stats_percpu); 1225 err_destroy_table: 1226 ovs_flow_tbl_destroy(&dp->table); 1227 err_free_dp: 1228 release_net(ovs_dp_get_net(dp)); 1229 kfree(dp); 1230 err_unlock_ovs: 1231 ovs_unlock(); 1232 err: 1233 return err; 1234 } 1235 1236 /* Called with ovs_mutex. */ 1237 static void __dp_destroy(struct datapath *dp) 1238 { 1239 int i; 1240 1241 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { 1242 struct vport *vport; 1243 struct hlist_node *n; 1244 1245 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node) 1246 if (vport->port_no != OVSP_LOCAL) 1247 ovs_dp_detach_port(vport); 1248 } 1249 1250 list_del_rcu(&dp->list_node); 1251 1252 /* OVSP_LOCAL is datapath internal port. We need to make sure that 1253 * all port in datapath are destroyed first before freeing datapath. 1254 */ 1255 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); 1256 1257 call_rcu(&dp->rcu, destroy_dp_rcu); 1258 } 1259 1260 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) 1261 { 1262 struct sk_buff *reply; 1263 struct datapath *dp; 1264 int err; 1265 1266 ovs_lock(); 1267 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); 1268 err = PTR_ERR(dp); 1269 if (IS_ERR(dp)) 1270 goto unlock; 1271 1272 reply = ovs_dp_cmd_build_info(dp, info->snd_portid, 1273 info->snd_seq, OVS_DP_CMD_DEL); 1274 err = PTR_ERR(reply); 1275 if (IS_ERR(reply)) 1276 goto unlock; 1277 1278 __dp_destroy(dp); 1279 ovs_unlock(); 1280 1281 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); 1282 1283 return 0; 1284 unlock: 1285 ovs_unlock(); 1286 return err; 1287 } 1288 1289 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) 1290 { 1291 struct sk_buff *reply; 1292 struct datapath *dp; 1293 int err; 1294 1295 ovs_lock(); 1296 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); 1297 err = PTR_ERR(dp); 1298 if (IS_ERR(dp)) 1299 goto unlock; 1300 1301 reply = ovs_dp_cmd_build_info(dp, info->snd_portid, 1302 info->snd_seq, OVS_DP_CMD_NEW); 1303 if (IS_ERR(reply)) { 1304 err = PTR_ERR(reply); 1305 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 1306 ovs_dp_datapath_multicast_group.id, err); 1307 err = 0; 1308 goto unlock; 1309 } 1310 1311 ovs_unlock(); 1312 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); 1313 1314 return 0; 1315 unlock: 1316 ovs_unlock(); 1317 return err; 1318 } 1319 1320 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) 1321 { 1322 struct sk_buff *reply; 1323 struct datapath *dp; 1324 int err; 1325 1326 ovs_lock(); 1327 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); 1328 if (IS_ERR(dp)) { 1329 err = PTR_ERR(dp); 1330 goto unlock; 1331 } 1332 1333 reply = ovs_dp_cmd_build_info(dp, info->snd_portid, 1334 info->snd_seq, OVS_DP_CMD_NEW); 1335 if (IS_ERR(reply)) { 1336 err = PTR_ERR(reply); 1337 goto unlock; 1338 } 1339 1340 ovs_unlock(); 1341 return genlmsg_reply(reply, info); 1342 1343 unlock: 1344 ovs_unlock(); 1345 return err; 1346 } 1347 1348 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 1349 { 1350 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); 1351 struct datapath *dp; 1352 int skip = cb->args[0]; 1353 int i = 0; 1354 1355 rcu_read_lock(); 1356 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) { 1357 if (i >= skip && 1358 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid, 1359 cb->nlh->nlmsg_seq, NLM_F_MULTI, 1360 OVS_DP_CMD_NEW) < 0) 1361 break; 1362 i++; 1363 } 1364 rcu_read_unlock(); 1365 1366 cb->args[0] = i; 1367 1368 return skb->len; 1369 } 1370 1371 static struct genl_ops dp_datapath_genl_ops[] = { 1372 { .cmd = OVS_DP_CMD_NEW, 1373 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1374 .policy = datapath_policy, 1375 .doit = ovs_dp_cmd_new 1376 }, 1377 { .cmd = OVS_DP_CMD_DEL, 1378 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1379 .policy = datapath_policy, 1380 .doit = ovs_dp_cmd_del 1381 }, 1382 { .cmd = OVS_DP_CMD_GET, 1383 .flags = 0, /* OK for unprivileged users. */ 1384 .policy = datapath_policy, 1385 .doit = ovs_dp_cmd_get, 1386 .dumpit = ovs_dp_cmd_dump 1387 }, 1388 { .cmd = OVS_DP_CMD_SET, 1389 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1390 .policy = datapath_policy, 1391 .doit = ovs_dp_cmd_set, 1392 }, 1393 }; 1394 1395 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { 1396 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, 1397 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) }, 1398 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 }, 1399 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 }, 1400 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 }, 1401 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, 1402 }; 1403 1404 static struct genl_family dp_vport_genl_family = { 1405 .id = GENL_ID_GENERATE, 1406 .hdrsize = sizeof(struct ovs_header), 1407 .name = OVS_VPORT_FAMILY, 1408 .version = OVS_VPORT_VERSION, 1409 .maxattr = OVS_VPORT_ATTR_MAX, 1410 .netnsok = true, 1411 .parallel_ops = true, 1412 }; 1413 1414 struct genl_multicast_group ovs_dp_vport_multicast_group = { 1415 .name = OVS_VPORT_MCGROUP 1416 }; 1417 1418 /* Called with ovs_mutex or RCU read lock. */ 1419 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, 1420 u32 portid, u32 seq, u32 flags, u8 cmd) 1421 { 1422 struct ovs_header *ovs_header; 1423 struct ovs_vport_stats vport_stats; 1424 int err; 1425 1426 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, 1427 flags, cmd); 1428 if (!ovs_header) 1429 return -EMSGSIZE; 1430 1431 ovs_header->dp_ifindex = get_dpifindex(vport->dp); 1432 1433 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) || 1434 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) || 1435 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) || 1436 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid)) 1437 goto nla_put_failure; 1438 1439 ovs_vport_get_stats(vport, &vport_stats); 1440 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats), 1441 &vport_stats)) 1442 goto nla_put_failure; 1443 1444 err = ovs_vport_get_options(vport, skb); 1445 if (err == -EMSGSIZE) 1446 goto error; 1447 1448 return genlmsg_end(skb, ovs_header); 1449 1450 nla_put_failure: 1451 err = -EMSGSIZE; 1452 error: 1453 genlmsg_cancel(skb, ovs_header); 1454 return err; 1455 } 1456 1457 /* Called with ovs_mutex or RCU read lock. */ 1458 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid, 1459 u32 seq, u8 cmd) 1460 { 1461 struct sk_buff *skb; 1462 int retval; 1463 1464 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 1465 if (!skb) 1466 return ERR_PTR(-ENOMEM); 1467 1468 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd); 1469 BUG_ON(retval < 0); 1470 1471 return skb; 1472 } 1473 1474 /* Called with ovs_mutex or RCU read lock. */ 1475 static struct vport *lookup_vport(struct net *net, 1476 struct ovs_header *ovs_header, 1477 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) 1478 { 1479 struct datapath *dp; 1480 struct vport *vport; 1481 1482 if (a[OVS_VPORT_ATTR_NAME]) { 1483 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME])); 1484 if (!vport) 1485 return ERR_PTR(-ENODEV); 1486 if (ovs_header->dp_ifindex && 1487 ovs_header->dp_ifindex != get_dpifindex(vport->dp)) 1488 return ERR_PTR(-ENODEV); 1489 return vport; 1490 } else if (a[OVS_VPORT_ATTR_PORT_NO]) { 1491 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); 1492 1493 if (port_no >= DP_MAX_PORTS) 1494 return ERR_PTR(-EFBIG); 1495 1496 dp = get_dp(net, ovs_header->dp_ifindex); 1497 if (!dp) 1498 return ERR_PTR(-ENODEV); 1499 1500 vport = ovs_vport_ovsl_rcu(dp, port_no); 1501 if (!vport) 1502 return ERR_PTR(-ENODEV); 1503 return vport; 1504 } else 1505 return ERR_PTR(-EINVAL); 1506 } 1507 1508 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) 1509 { 1510 struct nlattr **a = info->attrs; 1511 struct ovs_header *ovs_header = info->userhdr; 1512 struct vport_parms parms; 1513 struct sk_buff *reply; 1514 struct vport *vport; 1515 struct datapath *dp; 1516 u32 port_no; 1517 int err; 1518 1519 err = -EINVAL; 1520 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || 1521 !a[OVS_VPORT_ATTR_UPCALL_PID]) 1522 goto exit; 1523 1524 ovs_lock(); 1525 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 1526 err = -ENODEV; 1527 if (!dp) 1528 goto exit_unlock; 1529 1530 if (a[OVS_VPORT_ATTR_PORT_NO]) { 1531 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); 1532 1533 err = -EFBIG; 1534 if (port_no >= DP_MAX_PORTS) 1535 goto exit_unlock; 1536 1537 vport = ovs_vport_ovsl(dp, port_no); 1538 err = -EBUSY; 1539 if (vport) 1540 goto exit_unlock; 1541 } else { 1542 for (port_no = 1; ; port_no++) { 1543 if (port_no >= DP_MAX_PORTS) { 1544 err = -EFBIG; 1545 goto exit_unlock; 1546 } 1547 vport = ovs_vport_ovsl(dp, port_no); 1548 if (!vport) 1549 break; 1550 } 1551 } 1552 1553 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]); 1554 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]); 1555 parms.options = a[OVS_VPORT_ATTR_OPTIONS]; 1556 parms.dp = dp; 1557 parms.port_no = port_no; 1558 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); 1559 1560 vport = new_vport(&parms); 1561 err = PTR_ERR(vport); 1562 if (IS_ERR(vport)) 1563 goto exit_unlock; 1564 1565 err = 0; 1566 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq, 1567 OVS_VPORT_CMD_NEW); 1568 if (IS_ERR(reply)) { 1569 err = PTR_ERR(reply); 1570 ovs_dp_detach_port(vport); 1571 goto exit_unlock; 1572 } 1573 1574 ovs_notify(reply, info, &ovs_dp_vport_multicast_group); 1575 1576 exit_unlock: 1577 ovs_unlock(); 1578 exit: 1579 return err; 1580 } 1581 1582 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) 1583 { 1584 struct nlattr **a = info->attrs; 1585 struct sk_buff *reply; 1586 struct vport *vport; 1587 int err; 1588 1589 ovs_lock(); 1590 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); 1591 err = PTR_ERR(vport); 1592 if (IS_ERR(vport)) 1593 goto exit_unlock; 1594 1595 if (a[OVS_VPORT_ATTR_TYPE] && 1596 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) { 1597 err = -EINVAL; 1598 goto exit_unlock; 1599 } 1600 1601 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1602 if (!reply) { 1603 err = -ENOMEM; 1604 goto exit_unlock; 1605 } 1606 1607 if (a[OVS_VPORT_ATTR_OPTIONS]) { 1608 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]); 1609 if (err) 1610 goto exit_free; 1611 } 1612 1613 if (a[OVS_VPORT_ATTR_UPCALL_PID]) 1614 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); 1615 1616 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, 1617 info->snd_seq, 0, OVS_VPORT_CMD_NEW); 1618 BUG_ON(err < 0); 1619 1620 ovs_unlock(); 1621 ovs_notify(reply, info, &ovs_dp_vport_multicast_group); 1622 return 0; 1623 1624 exit_free: 1625 kfree_skb(reply); 1626 exit_unlock: 1627 ovs_unlock(); 1628 return err; 1629 } 1630 1631 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) 1632 { 1633 struct nlattr **a = info->attrs; 1634 struct sk_buff *reply; 1635 struct vport *vport; 1636 int err; 1637 1638 ovs_lock(); 1639 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); 1640 err = PTR_ERR(vport); 1641 if (IS_ERR(vport)) 1642 goto exit_unlock; 1643 1644 if (vport->port_no == OVSP_LOCAL) { 1645 err = -EINVAL; 1646 goto exit_unlock; 1647 } 1648 1649 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, 1650 info->snd_seq, OVS_VPORT_CMD_DEL); 1651 err = PTR_ERR(reply); 1652 if (IS_ERR(reply)) 1653 goto exit_unlock; 1654 1655 err = 0; 1656 ovs_dp_detach_port(vport); 1657 1658 ovs_notify(reply, info, &ovs_dp_vport_multicast_group); 1659 1660 exit_unlock: 1661 ovs_unlock(); 1662 return err; 1663 } 1664 1665 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info) 1666 { 1667 struct nlattr **a = info->attrs; 1668 struct ovs_header *ovs_header = info->userhdr; 1669 struct sk_buff *reply; 1670 struct vport *vport; 1671 int err; 1672 1673 rcu_read_lock(); 1674 vport = lookup_vport(sock_net(skb->sk), ovs_header, a); 1675 err = PTR_ERR(vport); 1676 if (IS_ERR(vport)) 1677 goto exit_unlock; 1678 1679 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, 1680 info->snd_seq, OVS_VPORT_CMD_NEW); 1681 err = PTR_ERR(reply); 1682 if (IS_ERR(reply)) 1683 goto exit_unlock; 1684 1685 rcu_read_unlock(); 1686 1687 return genlmsg_reply(reply, info); 1688 1689 exit_unlock: 1690 rcu_read_unlock(); 1691 return err; 1692 } 1693 1694 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 1695 { 1696 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); 1697 struct datapath *dp; 1698 int bucket = cb->args[0], skip = cb->args[1]; 1699 int i, j = 0; 1700 1701 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 1702 if (!dp) 1703 return -ENODEV; 1704 1705 rcu_read_lock(); 1706 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) { 1707 struct vport *vport; 1708 1709 j = 0; 1710 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) { 1711 if (j >= skip && 1712 ovs_vport_cmd_fill_info(vport, skb, 1713 NETLINK_CB(cb->skb).portid, 1714 cb->nlh->nlmsg_seq, 1715 NLM_F_MULTI, 1716 OVS_VPORT_CMD_NEW) < 0) 1717 goto out; 1718 1719 j++; 1720 } 1721 skip = 0; 1722 } 1723 out: 1724 rcu_read_unlock(); 1725 1726 cb->args[0] = i; 1727 cb->args[1] = j; 1728 1729 return skb->len; 1730 } 1731 1732 static struct genl_ops dp_vport_genl_ops[] = { 1733 { .cmd = OVS_VPORT_CMD_NEW, 1734 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1735 .policy = vport_policy, 1736 .doit = ovs_vport_cmd_new 1737 }, 1738 { .cmd = OVS_VPORT_CMD_DEL, 1739 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1740 .policy = vport_policy, 1741 .doit = ovs_vport_cmd_del 1742 }, 1743 { .cmd = OVS_VPORT_CMD_GET, 1744 .flags = 0, /* OK for unprivileged users. */ 1745 .policy = vport_policy, 1746 .doit = ovs_vport_cmd_get, 1747 .dumpit = ovs_vport_cmd_dump 1748 }, 1749 { .cmd = OVS_VPORT_CMD_SET, 1750 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1751 .policy = vport_policy, 1752 .doit = ovs_vport_cmd_set, 1753 }, 1754 }; 1755 1756 struct genl_family_and_ops { 1757 struct genl_family *family; 1758 struct genl_ops *ops; 1759 int n_ops; 1760 struct genl_multicast_group *group; 1761 }; 1762 1763 static const struct genl_family_and_ops dp_genl_families[] = { 1764 { &dp_datapath_genl_family, 1765 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops), 1766 &ovs_dp_datapath_multicast_group }, 1767 { &dp_vport_genl_family, 1768 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops), 1769 &ovs_dp_vport_multicast_group }, 1770 { &dp_flow_genl_family, 1771 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops), 1772 &ovs_dp_flow_multicast_group }, 1773 { &dp_packet_genl_family, 1774 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops), 1775 NULL }, 1776 }; 1777 1778 static void dp_unregister_genl(int n_families) 1779 { 1780 int i; 1781 1782 for (i = 0; i < n_families; i++) 1783 genl_unregister_family(dp_genl_families[i].family); 1784 } 1785 1786 static int dp_register_genl(void) 1787 { 1788 int n_registered; 1789 int err; 1790 int i; 1791 1792 n_registered = 0; 1793 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { 1794 const struct genl_family_and_ops *f = &dp_genl_families[i]; 1795 1796 err = genl_register_family_with_ops(f->family, f->ops, 1797 f->n_ops); 1798 if (err) 1799 goto error; 1800 n_registered++; 1801 1802 if (f->group) { 1803 err = genl_register_mc_group(f->family, f->group); 1804 if (err) 1805 goto error; 1806 } 1807 } 1808 1809 return 0; 1810 1811 error: 1812 dp_unregister_genl(n_registered); 1813 return err; 1814 } 1815 1816 static int __net_init ovs_init_net(struct net *net) 1817 { 1818 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 1819 1820 INIT_LIST_HEAD(&ovs_net->dps); 1821 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq); 1822 return 0; 1823 } 1824 1825 static void __net_exit ovs_exit_net(struct net *net) 1826 { 1827 struct datapath *dp, *dp_next; 1828 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 1829 1830 ovs_lock(); 1831 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node) 1832 __dp_destroy(dp); 1833 ovs_unlock(); 1834 1835 cancel_work_sync(&ovs_net->dp_notify_work); 1836 } 1837 1838 static struct pernet_operations ovs_net_ops = { 1839 .init = ovs_init_net, 1840 .exit = ovs_exit_net, 1841 .id = &ovs_net_id, 1842 .size = sizeof(struct ovs_net), 1843 }; 1844 1845 static int __init dp_init(void) 1846 { 1847 int err; 1848 1849 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb)); 1850 1851 pr_info("Open vSwitch switching datapath\n"); 1852 1853 err = ovs_flow_init(); 1854 if (err) 1855 goto error; 1856 1857 err = ovs_vport_init(); 1858 if (err) 1859 goto error_flow_exit; 1860 1861 err = register_pernet_device(&ovs_net_ops); 1862 if (err) 1863 goto error_vport_exit; 1864 1865 err = register_netdevice_notifier(&ovs_dp_device_notifier); 1866 if (err) 1867 goto error_netns_exit; 1868 1869 err = dp_register_genl(); 1870 if (err < 0) 1871 goto error_unreg_notifier; 1872 1873 return 0; 1874 1875 error_unreg_notifier: 1876 unregister_netdevice_notifier(&ovs_dp_device_notifier); 1877 error_netns_exit: 1878 unregister_pernet_device(&ovs_net_ops); 1879 error_vport_exit: 1880 ovs_vport_exit(); 1881 error_flow_exit: 1882 ovs_flow_exit(); 1883 error: 1884 return err; 1885 } 1886 1887 static void dp_cleanup(void) 1888 { 1889 dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); 1890 unregister_netdevice_notifier(&ovs_dp_device_notifier); 1891 unregister_pernet_device(&ovs_net_ops); 1892 rcu_barrier(); 1893 ovs_vport_exit(); 1894 ovs_flow_exit(); 1895 } 1896 1897 module_init(dp_init); 1898 module_exit(dp_cleanup); 1899 1900 MODULE_DESCRIPTION("Open vSwitch switching datapath"); 1901 MODULE_LICENSE("GPL"); 1902