1 /* 2 * net/tipc/bearer.c: TIPC bearer code 3 * 4 * Copyright (c) 1996-2006, 2013-2016, Ericsson AB 5 * Copyright (c) 2004-2006, 2010-2013, Wind River Systems 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the names of the copyright holders nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * Alternatively, this software may be distributed under the terms of the 21 * GNU General Public License ("GPL") version 2 as published by the Free 22 * Software Foundation. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #include <net/sock.h> 38 #include "core.h" 39 #include "bearer.h" 40 #include "link.h" 41 #include "discover.h" 42 #include "monitor.h" 43 #include "bcast.h" 44 #include "netlink.h" 45 #include "udp_media.h" 46 47 #define MAX_ADDR_STR 60 48 49 static struct tipc_media * const media_info_array[] = { 50 ð_media_info, 51 #ifdef CONFIG_TIPC_MEDIA_IB 52 &ib_media_info, 53 #endif 54 #ifdef CONFIG_TIPC_MEDIA_UDP 55 &udp_media_info, 56 #endif 57 NULL 58 }; 59 60 static struct tipc_bearer *bearer_get(struct net *net, int bearer_id) 61 { 62 struct tipc_net *tn = tipc_net(net); 63 64 return rcu_dereference_rtnl(tn->bearer_list[bearer_id]); 65 } 66 67 static void bearer_disable(struct net *net, struct tipc_bearer *b); 68 69 /** 70 * tipc_media_find - locates specified media object by name 71 */ 72 struct tipc_media *tipc_media_find(const char *name) 73 { 74 u32 i; 75 76 for (i = 0; media_info_array[i] != NULL; i++) { 77 if (!strcmp(media_info_array[i]->name, name)) 78 break; 79 } 80 return media_info_array[i]; 81 } 82 83 /** 84 * media_find_id - locates specified media object by type identifier 85 */ 86 static struct tipc_media *media_find_id(u8 type) 87 { 88 u32 i; 89 90 for (i = 0; media_info_array[i] != NULL; i++) { 91 if (media_info_array[i]->type_id == type) 92 break; 93 } 94 return media_info_array[i]; 95 } 96 97 /** 98 * tipc_media_addr_printf - record media address in print buffer 99 */ 100 void tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a) 101 { 102 char addr_str[MAX_ADDR_STR]; 103 struct tipc_media *m; 104 int ret; 105 106 m = media_find_id(a->media_id); 107 108 if (m && !m->addr2str(a, addr_str, sizeof(addr_str))) 109 ret = scnprintf(buf, len, "%s(%s)", m->name, addr_str); 110 else { 111 u32 i; 112 113 ret = scnprintf(buf, len, "UNKNOWN(%u)", a->media_id); 114 for (i = 0; i < sizeof(a->value); i++) 115 ret += scnprintf(buf - ret, len + ret, 116 "-%02x", a->value[i]); 117 } 118 } 119 120 /** 121 * bearer_name_validate - validate & (optionally) deconstruct bearer name 122 * @name: ptr to bearer name string 123 * @name_parts: ptr to area for bearer name components (or NULL if not needed) 124 * 125 * Returns 1 if bearer name is valid, otherwise 0. 126 */ 127 static int bearer_name_validate(const char *name, 128 struct tipc_bearer_names *name_parts) 129 { 130 char name_copy[TIPC_MAX_BEARER_NAME]; 131 char *media_name; 132 char *if_name; 133 u32 media_len; 134 u32 if_len; 135 136 /* copy bearer name & ensure length is OK */ 137 name_copy[TIPC_MAX_BEARER_NAME - 1] = 0; 138 /* need above in case non-Posix strncpy() doesn't pad with nulls */ 139 strncpy(name_copy, name, TIPC_MAX_BEARER_NAME); 140 if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0) 141 return 0; 142 143 /* ensure all component parts of bearer name are present */ 144 media_name = name_copy; 145 if_name = strchr(media_name, ':'); 146 if (if_name == NULL) 147 return 0; 148 *(if_name++) = 0; 149 media_len = if_name - media_name; 150 if_len = strlen(if_name) + 1; 151 152 /* validate component parts of bearer name */ 153 if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) || 154 (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME)) 155 return 0; 156 157 /* return bearer name components, if necessary */ 158 if (name_parts) { 159 strcpy(name_parts->media_name, media_name); 160 strcpy(name_parts->if_name, if_name); 161 } 162 return 1; 163 } 164 165 /** 166 * tipc_bearer_find - locates bearer object with matching bearer name 167 */ 168 struct tipc_bearer *tipc_bearer_find(struct net *net, const char *name) 169 { 170 struct tipc_net *tn = net_generic(net, tipc_net_id); 171 struct tipc_bearer *b; 172 u32 i; 173 174 for (i = 0; i < MAX_BEARERS; i++) { 175 b = rtnl_dereference(tn->bearer_list[i]); 176 if (b && (!strcmp(b->name, name))) 177 return b; 178 } 179 return NULL; 180 } 181 182 /* tipc_bearer_get_name - get the bearer name from its id. 183 * @net: network namespace 184 * @name: a pointer to the buffer where the name will be stored. 185 * @bearer_id: the id to get the name from. 186 */ 187 int tipc_bearer_get_name(struct net *net, char *name, u32 bearer_id) 188 { 189 struct tipc_net *tn = tipc_net(net); 190 struct tipc_bearer *b; 191 192 if (bearer_id >= MAX_BEARERS) 193 return -EINVAL; 194 195 b = rtnl_dereference(tn->bearer_list[bearer_id]); 196 if (!b) 197 return -EINVAL; 198 199 strcpy(name, b->name); 200 return 0; 201 } 202 203 void tipc_bearer_add_dest(struct net *net, u32 bearer_id, u32 dest) 204 { 205 struct tipc_net *tn = net_generic(net, tipc_net_id); 206 struct tipc_bearer *b; 207 208 rcu_read_lock(); 209 b = rcu_dereference_rtnl(tn->bearer_list[bearer_id]); 210 if (b) 211 tipc_disc_add_dest(b->link_req); 212 rcu_read_unlock(); 213 } 214 215 void tipc_bearer_remove_dest(struct net *net, u32 bearer_id, u32 dest) 216 { 217 struct tipc_net *tn = net_generic(net, tipc_net_id); 218 struct tipc_bearer *b; 219 220 rcu_read_lock(); 221 b = rcu_dereference_rtnl(tn->bearer_list[bearer_id]); 222 if (b) 223 tipc_disc_remove_dest(b->link_req); 224 rcu_read_unlock(); 225 } 226 227 /** 228 * tipc_enable_bearer - enable bearer with the given name 229 */ 230 static int tipc_enable_bearer(struct net *net, const char *name, 231 u32 disc_domain, u32 priority, 232 struct nlattr *attr[]) 233 { 234 struct tipc_net *tn = net_generic(net, tipc_net_id); 235 struct tipc_bearer *b; 236 struct tipc_media *m; 237 struct tipc_bearer_names b_names; 238 struct sk_buff *skb; 239 char addr_string[16]; 240 u32 bearer_id; 241 u32 with_this_prio; 242 u32 i; 243 int res = -EINVAL; 244 245 if (!tn->own_addr) { 246 pr_warn("Bearer <%s> rejected, not supported in standalone mode\n", 247 name); 248 return -ENOPROTOOPT; 249 } 250 if (!bearer_name_validate(name, &b_names)) { 251 pr_warn("Bearer <%s> rejected, illegal name\n", name); 252 return -EINVAL; 253 } 254 if (tipc_addr_domain_valid(disc_domain) && 255 (disc_domain != tn->own_addr)) { 256 if (tipc_in_scope(disc_domain, tn->own_addr)) { 257 disc_domain = tn->own_addr & TIPC_ZONE_CLUSTER_MASK; 258 res = 0; /* accept any node in own cluster */ 259 } else if (in_own_cluster_exact(net, disc_domain)) 260 res = 0; /* accept specified node in own cluster */ 261 } 262 if (res) { 263 pr_warn("Bearer <%s> rejected, illegal discovery domain\n", 264 name); 265 return -EINVAL; 266 } 267 if ((priority > TIPC_MAX_LINK_PRI) && 268 (priority != TIPC_MEDIA_LINK_PRI)) { 269 pr_warn("Bearer <%s> rejected, illegal priority\n", name); 270 return -EINVAL; 271 } 272 273 m = tipc_media_find(b_names.media_name); 274 if (!m) { 275 pr_warn("Bearer <%s> rejected, media <%s> not registered\n", 276 name, b_names.media_name); 277 return -EINVAL; 278 } 279 280 if (priority == TIPC_MEDIA_LINK_PRI) 281 priority = m->priority; 282 283 restart: 284 bearer_id = MAX_BEARERS; 285 with_this_prio = 1; 286 for (i = MAX_BEARERS; i-- != 0; ) { 287 b = rtnl_dereference(tn->bearer_list[i]); 288 if (!b) { 289 bearer_id = i; 290 continue; 291 } 292 if (!strcmp(name, b->name)) { 293 pr_warn("Bearer <%s> rejected, already enabled\n", 294 name); 295 return -EINVAL; 296 } 297 if ((b->priority == priority) && 298 (++with_this_prio > 2)) { 299 if (priority-- == 0) { 300 pr_warn("Bearer <%s> rejected, duplicate priority\n", 301 name); 302 return -EINVAL; 303 } 304 pr_warn("Bearer <%s> priority adjustment required %u->%u\n", 305 name, priority + 1, priority); 306 goto restart; 307 } 308 } 309 if (bearer_id >= MAX_BEARERS) { 310 pr_warn("Bearer <%s> rejected, bearer limit reached (%u)\n", 311 name, MAX_BEARERS); 312 return -EINVAL; 313 } 314 315 b = kzalloc(sizeof(*b), GFP_ATOMIC); 316 if (!b) 317 return -ENOMEM; 318 319 strcpy(b->name, name); 320 b->media = m; 321 res = m->enable_media(net, b, attr); 322 if (res) { 323 pr_warn("Bearer <%s> rejected, enable failure (%d)\n", 324 name, -res); 325 return -EINVAL; 326 } 327 328 b->identity = bearer_id; 329 b->tolerance = m->tolerance; 330 b->window = m->window; 331 b->domain = disc_domain; 332 b->net_plane = bearer_id + 'A'; 333 b->priority = priority; 334 test_and_set_bit_lock(0, &b->up); 335 336 res = tipc_disc_create(net, b, &b->bcast_addr, &skb); 337 if (res) { 338 bearer_disable(net, b); 339 pr_warn("Bearer <%s> rejected, discovery object creation failed\n", 340 name); 341 return -EINVAL; 342 } 343 344 rcu_assign_pointer(tn->bearer_list[bearer_id], b); 345 if (skb) 346 tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr); 347 348 if (tipc_mon_create(net, bearer_id)) 349 return -ENOMEM; 350 351 pr_info("Enabled bearer <%s>, discovery domain %s, priority %u\n", 352 name, 353 tipc_addr_string_fill(addr_string, disc_domain), priority); 354 return res; 355 } 356 357 /** 358 * tipc_reset_bearer - Reset all links established over this bearer 359 */ 360 static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b) 361 { 362 pr_info("Resetting bearer <%s>\n", b->name); 363 tipc_node_delete_links(net, b->identity); 364 tipc_disc_reset(net, b); 365 return 0; 366 } 367 368 /** 369 * bearer_disable 370 * 371 * Note: This routine assumes caller holds RTNL lock. 372 */ 373 static void bearer_disable(struct net *net, struct tipc_bearer *b) 374 { 375 struct tipc_net *tn = tipc_net(net); 376 int bearer_id = b->identity; 377 378 pr_info("Disabling bearer <%s>\n", b->name); 379 clear_bit_unlock(0, &b->up); 380 tipc_node_delete_links(net, bearer_id); 381 b->media->disable_media(b); 382 RCU_INIT_POINTER(b->media_ptr, NULL); 383 if (b->link_req) 384 tipc_disc_delete(b->link_req); 385 RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL); 386 kfree_rcu(b, rcu); 387 tipc_mon_delete(net, bearer_id); 388 } 389 390 int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b, 391 struct nlattr *attr[]) 392 { 393 struct net_device *dev; 394 char *driver_name = strchr((const char *)b->name, ':') + 1; 395 396 /* Find device with specified name */ 397 dev = dev_get_by_name(net, driver_name); 398 if (!dev) 399 return -ENODEV; 400 if (tipc_mtu_bad(dev, 0)) { 401 dev_put(dev); 402 return -EINVAL; 403 } 404 405 /* Associate TIPC bearer with L2 bearer */ 406 rcu_assign_pointer(b->media_ptr, dev); 407 memset(&b->bcast_addr, 0, sizeof(b->bcast_addr)); 408 memcpy(b->bcast_addr.value, dev->broadcast, b->media->hwaddr_len); 409 b->bcast_addr.media_id = b->media->type_id; 410 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT; 411 b->mtu = dev->mtu; 412 b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr); 413 rcu_assign_pointer(dev->tipc_ptr, b); 414 return 0; 415 } 416 417 /* tipc_disable_l2_media - detach TIPC bearer from an L2 interface 418 * 419 * Mark L2 bearer as inactive so that incoming buffers are thrown away 420 */ 421 void tipc_disable_l2_media(struct tipc_bearer *b) 422 { 423 struct net_device *dev; 424 425 dev = (struct net_device *)rtnl_dereference(b->media_ptr); 426 RCU_INIT_POINTER(dev->tipc_ptr, NULL); 427 synchronize_net(); 428 dev_put(dev); 429 } 430 431 /** 432 * tipc_l2_send_msg - send a TIPC packet out over an L2 interface 433 * @skb: the packet to be sent 434 * @b: the bearer through which the packet is to be sent 435 * @dest: peer destination address 436 */ 437 int tipc_l2_send_msg(struct net *net, struct sk_buff *skb, 438 struct tipc_bearer *b, struct tipc_media_addr *dest) 439 { 440 struct net_device *dev; 441 int delta; 442 443 dev = (struct net_device *)rcu_dereference_rtnl(b->media_ptr); 444 if (!dev) 445 return 0; 446 447 delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb)); 448 if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) { 449 kfree_skb(skb); 450 return 0; 451 } 452 skb_reset_network_header(skb); 453 skb->dev = dev; 454 skb->protocol = htons(ETH_P_TIPC); 455 dev_hard_header(skb, dev, ETH_P_TIPC, dest->value, 456 dev->dev_addr, skb->len); 457 dev_queue_xmit(skb); 458 return 0; 459 } 460 461 bool tipc_bearer_bcast_support(struct net *net, u32 bearer_id) 462 { 463 bool supp = false; 464 struct tipc_bearer *b; 465 466 rcu_read_lock(); 467 b = bearer_get(net, bearer_id); 468 if (b) 469 supp = (b->bcast_addr.broadcast == TIPC_BROADCAST_SUPPORT); 470 rcu_read_unlock(); 471 return supp; 472 } 473 474 int tipc_bearer_mtu(struct net *net, u32 bearer_id) 475 { 476 int mtu = 0; 477 struct tipc_bearer *b; 478 479 rcu_read_lock(); 480 b = rcu_dereference_rtnl(tipc_net(net)->bearer_list[bearer_id]); 481 if (b) 482 mtu = b->mtu; 483 rcu_read_unlock(); 484 return mtu; 485 } 486 487 /* tipc_bearer_xmit_skb - sends buffer to destination over bearer 488 */ 489 void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id, 490 struct sk_buff *skb, 491 struct tipc_media_addr *dest) 492 { 493 struct tipc_msg *hdr = buf_msg(skb); 494 struct tipc_bearer *b; 495 496 rcu_read_lock(); 497 b = bearer_get(net, bearer_id); 498 if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr)))) 499 b->media->send_msg(net, skb, b, dest); 500 else 501 kfree_skb(skb); 502 rcu_read_unlock(); 503 } 504 505 /* tipc_bearer_xmit() -send buffer to destination over bearer 506 */ 507 void tipc_bearer_xmit(struct net *net, u32 bearer_id, 508 struct sk_buff_head *xmitq, 509 struct tipc_media_addr *dst) 510 { 511 struct tipc_bearer *b; 512 struct sk_buff *skb, *tmp; 513 514 if (skb_queue_empty(xmitq)) 515 return; 516 517 rcu_read_lock(); 518 b = bearer_get(net, bearer_id); 519 if (unlikely(!b)) 520 __skb_queue_purge(xmitq); 521 skb_queue_walk_safe(xmitq, skb, tmp) { 522 __skb_dequeue(xmitq); 523 if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb)))) 524 b->media->send_msg(net, skb, b, dst); 525 else 526 kfree_skb(skb); 527 } 528 rcu_read_unlock(); 529 } 530 531 /* tipc_bearer_bc_xmit() - broadcast buffers to all destinations 532 */ 533 void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id, 534 struct sk_buff_head *xmitq) 535 { 536 struct tipc_net *tn = tipc_net(net); 537 int net_id = tn->net_id; 538 struct tipc_bearer *b; 539 struct sk_buff *skb, *tmp; 540 struct tipc_msg *hdr; 541 542 rcu_read_lock(); 543 b = bearer_get(net, bearer_id); 544 if (unlikely(!b || !test_bit(0, &b->up))) 545 __skb_queue_purge(xmitq); 546 skb_queue_walk_safe(xmitq, skb, tmp) { 547 hdr = buf_msg(skb); 548 msg_set_non_seq(hdr, 1); 549 msg_set_mc_netid(hdr, net_id); 550 __skb_dequeue(xmitq); 551 b->media->send_msg(net, skb, b, &b->bcast_addr); 552 } 553 rcu_read_unlock(); 554 } 555 556 /** 557 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface 558 * @buf: the received packet 559 * @dev: the net device that the packet was received on 560 * @pt: the packet_type structure which was used to register this handler 561 * @orig_dev: the original receive net device in case the device is a bond 562 * 563 * Accept only packets explicitly sent to this node, or broadcast packets; 564 * ignores packets sent using interface multicast, and traffic sent to other 565 * nodes (which can happen if interface is running in promiscuous mode). 566 */ 567 static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev, 568 struct packet_type *pt, struct net_device *orig_dev) 569 { 570 struct tipc_bearer *b; 571 572 rcu_read_lock(); 573 b = rcu_dereference_rtnl(dev->tipc_ptr); 574 if (likely(b && test_bit(0, &b->up) && 575 (skb->pkt_type <= PACKET_MULTICAST))) { 576 skb->next = NULL; 577 tipc_rcv(dev_net(dev), skb, b); 578 rcu_read_unlock(); 579 return NET_RX_SUCCESS; 580 } 581 rcu_read_unlock(); 582 kfree_skb(skb); 583 return NET_RX_DROP; 584 } 585 586 /** 587 * tipc_l2_device_event - handle device events from network device 588 * @nb: the context of the notification 589 * @evt: the type of event 590 * @ptr: the net device that the event was on 591 * 592 * This function is called by the Ethernet driver in case of link 593 * change event. 594 */ 595 static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt, 596 void *ptr) 597 { 598 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 599 struct net *net = dev_net(dev); 600 struct tipc_bearer *b; 601 602 b = rtnl_dereference(dev->tipc_ptr); 603 if (!b) 604 return NOTIFY_DONE; 605 606 switch (evt) { 607 case NETDEV_CHANGE: 608 if (netif_carrier_ok(dev)) 609 break; 610 case NETDEV_UP: 611 test_and_set_bit_lock(0, &b->up); 612 break; 613 case NETDEV_GOING_DOWN: 614 clear_bit_unlock(0, &b->up); 615 tipc_reset_bearer(net, b); 616 break; 617 case NETDEV_CHANGEMTU: 618 if (tipc_mtu_bad(dev, 0)) { 619 bearer_disable(net, b); 620 break; 621 } 622 b->mtu = dev->mtu; 623 tipc_reset_bearer(net, b); 624 break; 625 case NETDEV_CHANGEADDR: 626 b->media->raw2addr(b, &b->addr, 627 (char *)dev->dev_addr); 628 tipc_reset_bearer(net, b); 629 break; 630 case NETDEV_UNREGISTER: 631 case NETDEV_CHANGENAME: 632 bearer_disable(dev_net(dev), b); 633 break; 634 } 635 return NOTIFY_OK; 636 } 637 638 static struct packet_type tipc_packet_type __read_mostly = { 639 .type = htons(ETH_P_TIPC), 640 .func = tipc_l2_rcv_msg, 641 }; 642 643 static struct notifier_block notifier = { 644 .notifier_call = tipc_l2_device_event, 645 .priority = 0, 646 }; 647 648 int tipc_bearer_setup(void) 649 { 650 int err; 651 652 err = register_netdevice_notifier(¬ifier); 653 if (err) 654 return err; 655 dev_add_pack(&tipc_packet_type); 656 return 0; 657 } 658 659 void tipc_bearer_cleanup(void) 660 { 661 unregister_netdevice_notifier(¬ifier); 662 dev_remove_pack(&tipc_packet_type); 663 } 664 665 void tipc_bearer_stop(struct net *net) 666 { 667 struct tipc_net *tn = net_generic(net, tipc_net_id); 668 struct tipc_bearer *b; 669 u32 i; 670 671 for (i = 0; i < MAX_BEARERS; i++) { 672 b = rtnl_dereference(tn->bearer_list[i]); 673 if (b) { 674 bearer_disable(net, b); 675 tn->bearer_list[i] = NULL; 676 } 677 } 678 } 679 680 /* Caller should hold rtnl_lock to protect the bearer */ 681 static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg, 682 struct tipc_bearer *bearer, int nlflags) 683 { 684 void *hdr; 685 struct nlattr *attrs; 686 struct nlattr *prop; 687 688 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family, 689 nlflags, TIPC_NL_BEARER_GET); 690 if (!hdr) 691 return -EMSGSIZE; 692 693 attrs = nla_nest_start(msg->skb, TIPC_NLA_BEARER); 694 if (!attrs) 695 goto msg_full; 696 697 if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name)) 698 goto attr_msg_full; 699 700 prop = nla_nest_start(msg->skb, TIPC_NLA_BEARER_PROP); 701 if (!prop) 702 goto prop_msg_full; 703 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority)) 704 goto prop_msg_full; 705 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance)) 706 goto prop_msg_full; 707 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->window)) 708 goto prop_msg_full; 709 710 nla_nest_end(msg->skb, prop); 711 712 #ifdef CONFIG_TIPC_MEDIA_UDP 713 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) { 714 if (tipc_udp_nl_add_bearer_data(msg, bearer)) 715 goto attr_msg_full; 716 } 717 #endif 718 719 nla_nest_end(msg->skb, attrs); 720 genlmsg_end(msg->skb, hdr); 721 722 return 0; 723 724 prop_msg_full: 725 nla_nest_cancel(msg->skb, prop); 726 attr_msg_full: 727 nla_nest_cancel(msg->skb, attrs); 728 msg_full: 729 genlmsg_cancel(msg->skb, hdr); 730 731 return -EMSGSIZE; 732 } 733 734 int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb) 735 { 736 int err; 737 int i = cb->args[0]; 738 struct tipc_bearer *bearer; 739 struct tipc_nl_msg msg; 740 struct net *net = sock_net(skb->sk); 741 struct tipc_net *tn = net_generic(net, tipc_net_id); 742 743 if (i == MAX_BEARERS) 744 return 0; 745 746 msg.skb = skb; 747 msg.portid = NETLINK_CB(cb->skb).portid; 748 msg.seq = cb->nlh->nlmsg_seq; 749 750 rtnl_lock(); 751 for (i = 0; i < MAX_BEARERS; i++) { 752 bearer = rtnl_dereference(tn->bearer_list[i]); 753 if (!bearer) 754 continue; 755 756 err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI); 757 if (err) 758 break; 759 } 760 rtnl_unlock(); 761 762 cb->args[0] = i; 763 return skb->len; 764 } 765 766 int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info) 767 { 768 int err; 769 char *name; 770 struct sk_buff *rep; 771 struct tipc_bearer *bearer; 772 struct tipc_nl_msg msg; 773 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; 774 struct net *net = genl_info_net(info); 775 776 if (!info->attrs[TIPC_NLA_BEARER]) 777 return -EINVAL; 778 779 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX, 780 info->attrs[TIPC_NLA_BEARER], 781 tipc_nl_bearer_policy, info->extack); 782 if (err) 783 return err; 784 785 if (!attrs[TIPC_NLA_BEARER_NAME]) 786 return -EINVAL; 787 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]); 788 789 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 790 if (!rep) 791 return -ENOMEM; 792 793 msg.skb = rep; 794 msg.portid = info->snd_portid; 795 msg.seq = info->snd_seq; 796 797 rtnl_lock(); 798 bearer = tipc_bearer_find(net, name); 799 if (!bearer) { 800 err = -EINVAL; 801 goto err_out; 802 } 803 804 err = __tipc_nl_add_bearer(&msg, bearer, 0); 805 if (err) 806 goto err_out; 807 rtnl_unlock(); 808 809 return genlmsg_reply(rep, info); 810 err_out: 811 rtnl_unlock(); 812 nlmsg_free(rep); 813 814 return err; 815 } 816 817 int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info) 818 { 819 int err; 820 char *name; 821 struct tipc_bearer *bearer; 822 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; 823 struct net *net = sock_net(skb->sk); 824 825 if (!info->attrs[TIPC_NLA_BEARER]) 826 return -EINVAL; 827 828 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX, 829 info->attrs[TIPC_NLA_BEARER], 830 tipc_nl_bearer_policy, info->extack); 831 if (err) 832 return err; 833 834 if (!attrs[TIPC_NLA_BEARER_NAME]) 835 return -EINVAL; 836 837 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]); 838 839 rtnl_lock(); 840 bearer = tipc_bearer_find(net, name); 841 if (!bearer) { 842 rtnl_unlock(); 843 return -EINVAL; 844 } 845 846 bearer_disable(net, bearer); 847 rtnl_unlock(); 848 849 return 0; 850 } 851 852 int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info) 853 { 854 int err; 855 char *bearer; 856 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; 857 struct net *net = sock_net(skb->sk); 858 struct tipc_net *tn = net_generic(net, tipc_net_id); 859 u32 domain; 860 u32 prio; 861 862 prio = TIPC_MEDIA_LINK_PRI; 863 domain = tn->own_addr & TIPC_ZONE_CLUSTER_MASK; 864 865 if (!info->attrs[TIPC_NLA_BEARER]) 866 return -EINVAL; 867 868 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX, 869 info->attrs[TIPC_NLA_BEARER], 870 tipc_nl_bearer_policy, info->extack); 871 if (err) 872 return err; 873 874 if (!attrs[TIPC_NLA_BEARER_NAME]) 875 return -EINVAL; 876 877 bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]); 878 879 if (attrs[TIPC_NLA_BEARER_DOMAIN]) 880 domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]); 881 882 if (attrs[TIPC_NLA_BEARER_PROP]) { 883 struct nlattr *props[TIPC_NLA_PROP_MAX + 1]; 884 885 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP], 886 props); 887 if (err) 888 return err; 889 890 if (props[TIPC_NLA_PROP_PRIO]) 891 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]); 892 } 893 894 rtnl_lock(); 895 err = tipc_enable_bearer(net, bearer, domain, prio, attrs); 896 if (err) { 897 rtnl_unlock(); 898 return err; 899 } 900 rtnl_unlock(); 901 902 return 0; 903 } 904 905 int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info) 906 { 907 int err; 908 char *name; 909 struct tipc_bearer *b; 910 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; 911 struct net *net = sock_net(skb->sk); 912 913 if (!info->attrs[TIPC_NLA_BEARER]) 914 return -EINVAL; 915 916 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX, 917 info->attrs[TIPC_NLA_BEARER], 918 tipc_nl_bearer_policy, info->extack); 919 if (err) 920 return err; 921 922 if (!attrs[TIPC_NLA_BEARER_NAME]) 923 return -EINVAL; 924 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]); 925 926 rtnl_lock(); 927 b = tipc_bearer_find(net, name); 928 if (!b) { 929 rtnl_unlock(); 930 return -EINVAL; 931 } 932 933 #ifdef CONFIG_TIPC_MEDIA_UDP 934 if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) { 935 err = tipc_udp_nl_bearer_add(b, 936 attrs[TIPC_NLA_BEARER_UDP_OPTS]); 937 if (err) { 938 rtnl_unlock(); 939 return err; 940 } 941 } 942 #endif 943 rtnl_unlock(); 944 945 return 0; 946 } 947 948 int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info) 949 { 950 int err; 951 char *name; 952 struct tipc_bearer *b; 953 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; 954 struct net *net = sock_net(skb->sk); 955 956 if (!info->attrs[TIPC_NLA_BEARER]) 957 return -EINVAL; 958 959 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX, 960 info->attrs[TIPC_NLA_BEARER], 961 tipc_nl_bearer_policy, info->extack); 962 if (err) 963 return err; 964 965 if (!attrs[TIPC_NLA_BEARER_NAME]) 966 return -EINVAL; 967 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]); 968 969 rtnl_lock(); 970 b = tipc_bearer_find(net, name); 971 if (!b) { 972 rtnl_unlock(); 973 return -EINVAL; 974 } 975 976 if (attrs[TIPC_NLA_BEARER_PROP]) { 977 struct nlattr *props[TIPC_NLA_PROP_MAX + 1]; 978 979 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP], 980 props); 981 if (err) { 982 rtnl_unlock(); 983 return err; 984 } 985 986 if (props[TIPC_NLA_PROP_TOL]) 987 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]); 988 if (props[TIPC_NLA_PROP_PRIO]) 989 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]); 990 if (props[TIPC_NLA_PROP_WIN]) 991 b->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]); 992 } 993 rtnl_unlock(); 994 995 return 0; 996 } 997 998 static int __tipc_nl_add_media(struct tipc_nl_msg *msg, 999 struct tipc_media *media, int nlflags) 1000 { 1001 void *hdr; 1002 struct nlattr *attrs; 1003 struct nlattr *prop; 1004 1005 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family, 1006 nlflags, TIPC_NL_MEDIA_GET); 1007 if (!hdr) 1008 return -EMSGSIZE; 1009 1010 attrs = nla_nest_start(msg->skb, TIPC_NLA_MEDIA); 1011 if (!attrs) 1012 goto msg_full; 1013 1014 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name)) 1015 goto attr_msg_full; 1016 1017 prop = nla_nest_start(msg->skb, TIPC_NLA_MEDIA_PROP); 1018 if (!prop) 1019 goto prop_msg_full; 1020 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority)) 1021 goto prop_msg_full; 1022 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance)) 1023 goto prop_msg_full; 1024 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->window)) 1025 goto prop_msg_full; 1026 1027 nla_nest_end(msg->skb, prop); 1028 nla_nest_end(msg->skb, attrs); 1029 genlmsg_end(msg->skb, hdr); 1030 1031 return 0; 1032 1033 prop_msg_full: 1034 nla_nest_cancel(msg->skb, prop); 1035 attr_msg_full: 1036 nla_nest_cancel(msg->skb, attrs); 1037 msg_full: 1038 genlmsg_cancel(msg->skb, hdr); 1039 1040 return -EMSGSIZE; 1041 } 1042 1043 int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb) 1044 { 1045 int err; 1046 int i = cb->args[0]; 1047 struct tipc_nl_msg msg; 1048 1049 if (i == MAX_MEDIA) 1050 return 0; 1051 1052 msg.skb = skb; 1053 msg.portid = NETLINK_CB(cb->skb).portid; 1054 msg.seq = cb->nlh->nlmsg_seq; 1055 1056 rtnl_lock(); 1057 for (; media_info_array[i] != NULL; i++) { 1058 err = __tipc_nl_add_media(&msg, media_info_array[i], 1059 NLM_F_MULTI); 1060 if (err) 1061 break; 1062 } 1063 rtnl_unlock(); 1064 1065 cb->args[0] = i; 1066 return skb->len; 1067 } 1068 1069 int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info) 1070 { 1071 int err; 1072 char *name; 1073 struct tipc_nl_msg msg; 1074 struct tipc_media *media; 1075 struct sk_buff *rep; 1076 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; 1077 1078 if (!info->attrs[TIPC_NLA_MEDIA]) 1079 return -EINVAL; 1080 1081 err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX, 1082 info->attrs[TIPC_NLA_MEDIA], 1083 tipc_nl_media_policy, info->extack); 1084 if (err) 1085 return err; 1086 1087 if (!attrs[TIPC_NLA_MEDIA_NAME]) 1088 return -EINVAL; 1089 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]); 1090 1091 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 1092 if (!rep) 1093 return -ENOMEM; 1094 1095 msg.skb = rep; 1096 msg.portid = info->snd_portid; 1097 msg.seq = info->snd_seq; 1098 1099 rtnl_lock(); 1100 media = tipc_media_find(name); 1101 if (!media) { 1102 err = -EINVAL; 1103 goto err_out; 1104 } 1105 1106 err = __tipc_nl_add_media(&msg, media, 0); 1107 if (err) 1108 goto err_out; 1109 rtnl_unlock(); 1110 1111 return genlmsg_reply(rep, info); 1112 err_out: 1113 rtnl_unlock(); 1114 nlmsg_free(rep); 1115 1116 return err; 1117 } 1118 1119 int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info) 1120 { 1121 int err; 1122 char *name; 1123 struct tipc_media *m; 1124 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; 1125 1126 if (!info->attrs[TIPC_NLA_MEDIA]) 1127 return -EINVAL; 1128 1129 err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX, 1130 info->attrs[TIPC_NLA_MEDIA], 1131 tipc_nl_media_policy, info->extack); 1132 1133 if (!attrs[TIPC_NLA_MEDIA_NAME]) 1134 return -EINVAL; 1135 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]); 1136 1137 rtnl_lock(); 1138 m = tipc_media_find(name); 1139 if (!m) { 1140 rtnl_unlock(); 1141 return -EINVAL; 1142 } 1143 1144 if (attrs[TIPC_NLA_MEDIA_PROP]) { 1145 struct nlattr *props[TIPC_NLA_PROP_MAX + 1]; 1146 1147 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP], 1148 props); 1149 if (err) { 1150 rtnl_unlock(); 1151 return err; 1152 } 1153 1154 if (props[TIPC_NLA_PROP_TOL]) 1155 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]); 1156 if (props[TIPC_NLA_PROP_PRIO]) 1157 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]); 1158 if (props[TIPC_NLA_PROP_WIN]) 1159 m->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]); 1160 } 1161 rtnl_unlock(); 1162 1163 return 0; 1164 } 1165