1 /* net/tipc/udp_media.c: IP bearer support for TIPC 2 * 3 * Copyright (c) 2015, Ericsson AB 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the names of the copyright holders nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * Alternatively, this software may be distributed under the terms of the 19 * GNU General Public License ("GPL") version 2 as published by the Free 20 * Software Foundation. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <linux/socket.h> 36 #include <linux/ip.h> 37 #include <linux/udp.h> 38 #include <linux/inet.h> 39 #include <linux/inetdevice.h> 40 #include <linux/igmp.h> 41 #include <linux/kernel.h> 42 #include <linux/workqueue.h> 43 #include <linux/list.h> 44 #include <net/sock.h> 45 #include <net/ip.h> 46 #include <net/udp_tunnel.h> 47 #include <net/ipv6_stubs.h> 48 #include <linux/tipc_netlink.h> 49 #include "core.h" 50 #include "addr.h" 51 #include "net.h" 52 #include "bearer.h" 53 #include "netlink.h" 54 #include "msg.h" 55 #include "udp_media.h" 56 57 /* IANA assigned UDP port */ 58 #define UDP_PORT_DEFAULT 6118 59 60 #define UDP_MIN_HEADROOM 48 61 62 /** 63 * struct udp_media_addr - IP/UDP addressing information 64 * 65 * This is the bearer level originating address used in neighbor discovery 66 * messages, and all fields should be in network byte order 67 * 68 * @proto: Ethernet protocol in use 69 * @port: port being used 70 * @ipv4: IPv4 address of neighbor 71 * @ipv6: IPv6 address of neighbor 72 */ 73 struct udp_media_addr { 74 __be16 proto; 75 __be16 port; 76 union { 77 struct in_addr ipv4; 78 struct in6_addr ipv6; 79 }; 80 }; 81 82 /* struct udp_replicast - container for UDP remote addresses */ 83 struct udp_replicast { 84 struct udp_media_addr addr; 85 struct dst_cache dst_cache; 86 struct rcu_head rcu; 87 struct list_head list; 88 }; 89 90 /** 91 * struct udp_bearer - ip/udp bearer data structure 92 * @bearer: associated generic tipc bearer 93 * @ubsock: bearer associated socket 94 * @ifindex: local address scope 95 * @work: used to schedule deferred work on a bearer 96 * @rcast: associated udp_replicast container 97 */ 98 struct udp_bearer { 99 struct tipc_bearer __rcu *bearer; 100 struct socket *ubsock; 101 u32 ifindex; 102 struct work_struct work; 103 struct udp_replicast rcast; 104 }; 105 106 static int tipc_udp_is_mcast_addr(struct udp_media_addr *addr) 107 { 108 if (ntohs(addr->proto) == ETH_P_IP) 109 return ipv4_is_multicast(addr->ipv4.s_addr); 110 #if IS_ENABLED(CONFIG_IPV6) 111 else 112 return ipv6_addr_is_multicast(&addr->ipv6); 113 #endif 114 return 0; 115 } 116 117 /* udp_media_addr_set - convert a ip/udp address to a TIPC media address */ 118 static void tipc_udp_media_addr_set(struct tipc_media_addr *addr, 119 struct udp_media_addr *ua) 120 { 121 memset(addr, 0, sizeof(struct tipc_media_addr)); 122 addr->media_id = TIPC_MEDIA_TYPE_UDP; 123 memcpy(addr->value, ua, sizeof(struct udp_media_addr)); 124 125 if (tipc_udp_is_mcast_addr(ua)) 126 addr->broadcast = TIPC_BROADCAST_SUPPORT; 127 } 128 129 /* tipc_udp_addr2str - convert ip/udp address to string */ 130 static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size) 131 { 132 struct udp_media_addr *ua = (struct udp_media_addr *)&a->value; 133 134 if (ntohs(ua->proto) == ETH_P_IP) 135 snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port)); 136 else if (ntohs(ua->proto) == ETH_P_IPV6) 137 snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port)); 138 else { 139 pr_err("Invalid UDP media address\n"); 140 return 1; 141 } 142 143 return 0; 144 } 145 146 /* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */ 147 static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a, 148 char *msg) 149 { 150 struct udp_media_addr *ua; 151 152 ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET); 153 if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP) 154 return -EINVAL; 155 tipc_udp_media_addr_set(a, ua); 156 return 0; 157 } 158 159 /* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */ 160 static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a) 161 { 162 memset(msg, 0, TIPC_MEDIA_INFO_SIZE); 163 msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP; 164 memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value, 165 sizeof(struct udp_media_addr)); 166 return 0; 167 } 168 169 /* tipc_send_msg - enqueue a send request */ 170 static int tipc_udp_xmit(struct net *net, struct sk_buff *skb, 171 struct udp_bearer *ub, struct udp_media_addr *src, 172 struct udp_media_addr *dst, struct dst_cache *cache) 173 { 174 struct dst_entry *ndst; 175 int ttl, err = 0; 176 177 local_bh_disable(); 178 ndst = dst_cache_get(cache); 179 if (dst->proto == htons(ETH_P_IP)) { 180 struct rtable *rt = dst_rtable(ndst); 181 182 if (!rt) { 183 struct flowi4 fl = { 184 .daddr = dst->ipv4.s_addr, 185 .saddr = src->ipv4.s_addr, 186 .flowi4_mark = skb->mark, 187 .flowi4_proto = IPPROTO_UDP 188 }; 189 rt = ip_route_output_key(net, &fl); 190 if (IS_ERR(rt)) { 191 err = PTR_ERR(rt); 192 goto tx_error; 193 } 194 dst_cache_set_ip4(cache, &rt->dst, fl.saddr); 195 } 196 197 ttl = ip4_dst_hoplimit(&rt->dst); 198 udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr, 199 dst->ipv4.s_addr, 0, ttl, 0, src->port, 200 dst->port, false, true); 201 #if IS_ENABLED(CONFIG_IPV6) 202 } else { 203 if (!ndst) { 204 struct flowi6 fl6 = { 205 .flowi6_oif = ub->ifindex, 206 .daddr = dst->ipv6, 207 .saddr = src->ipv6, 208 .flowi6_proto = IPPROTO_UDP 209 }; 210 ndst = ipv6_stub->ipv6_dst_lookup_flow(net, 211 ub->ubsock->sk, 212 &fl6, NULL); 213 if (IS_ERR(ndst)) { 214 err = PTR_ERR(ndst); 215 goto tx_error; 216 } 217 dst_cache_set_ip6(cache, ndst, &fl6.saddr); 218 } 219 ttl = ip6_dst_hoplimit(ndst); 220 err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb, NULL, 221 &src->ipv6, &dst->ipv6, 0, ttl, 0, 222 src->port, dst->port, false); 223 #endif 224 } 225 local_bh_enable(); 226 return err; 227 228 tx_error: 229 local_bh_enable(); 230 kfree_skb(skb); 231 return err; 232 } 233 234 static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb, 235 struct tipc_bearer *b, 236 struct tipc_media_addr *addr) 237 { 238 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value; 239 struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value; 240 struct udp_replicast *rcast; 241 struct udp_bearer *ub; 242 int err = 0; 243 244 if (skb_headroom(skb) < UDP_MIN_HEADROOM) { 245 err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC); 246 if (err) 247 goto out; 248 } 249 250 skb_set_inner_protocol(skb, htons(ETH_P_TIPC)); 251 ub = rcu_dereference(b->media_ptr); 252 if (!ub) { 253 err = -ENODEV; 254 goto out; 255 } 256 257 if (addr->broadcast != TIPC_REPLICAST_SUPPORT) 258 return tipc_udp_xmit(net, skb, ub, src, dst, 259 &ub->rcast.dst_cache); 260 261 /* Replicast, send an skb to each configured IP address */ 262 list_for_each_entry_rcu(rcast, &ub->rcast.list, list) { 263 struct sk_buff *_skb; 264 265 _skb = pskb_copy(skb, GFP_ATOMIC); 266 if (!_skb) { 267 err = -ENOMEM; 268 goto out; 269 } 270 271 err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr, 272 &rcast->dst_cache); 273 if (err) 274 goto out; 275 } 276 err = 0; 277 out: 278 kfree_skb(skb); 279 return err; 280 } 281 282 static bool tipc_udp_is_known_peer(struct tipc_bearer *b, 283 struct udp_media_addr *addr) 284 { 285 struct udp_replicast *rcast, *tmp; 286 struct udp_bearer *ub; 287 288 ub = rcu_dereference_rtnl(b->media_ptr); 289 if (!ub) { 290 pr_err_ratelimited("UDP bearer instance not found\n"); 291 return false; 292 } 293 294 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) { 295 if (!memcmp(&rcast->addr, addr, sizeof(struct udp_media_addr))) 296 return true; 297 } 298 299 return false; 300 } 301 302 static int tipc_udp_rcast_add(struct tipc_bearer *b, 303 struct udp_media_addr *addr) 304 { 305 struct udp_replicast *rcast; 306 struct udp_bearer *ub; 307 308 ub = rcu_dereference_rtnl(b->media_ptr); 309 if (!ub) 310 return -ENODEV; 311 312 rcast = kmalloc(sizeof(*rcast), GFP_ATOMIC); 313 if (!rcast) 314 return -ENOMEM; 315 316 if (dst_cache_init(&rcast->dst_cache, GFP_ATOMIC)) { 317 kfree(rcast); 318 return -ENOMEM; 319 } 320 321 memcpy(&rcast->addr, addr, sizeof(struct udp_media_addr)); 322 323 if (ntohs(addr->proto) == ETH_P_IP) 324 pr_info("New replicast peer: %pI4\n", &rcast->addr.ipv4); 325 #if IS_ENABLED(CONFIG_IPV6) 326 else if (ntohs(addr->proto) == ETH_P_IPV6) 327 pr_info("New replicast peer: %pI6\n", &rcast->addr.ipv6); 328 #endif 329 b->bcast_addr.broadcast = TIPC_REPLICAST_SUPPORT; 330 list_add_rcu(&rcast->list, &ub->rcast.list); 331 return 0; 332 } 333 334 static int tipc_udp_rcast_disc(struct tipc_bearer *b, struct sk_buff *skb) 335 { 336 struct udp_media_addr src = {0}; 337 struct udp_media_addr *dst; 338 339 dst = (struct udp_media_addr *)&b->bcast_addr.value; 340 if (tipc_udp_is_mcast_addr(dst)) 341 return 0; 342 343 src.port = udp_hdr(skb)->source; 344 345 if (ip_hdr(skb)->version == 4) { 346 struct iphdr *iphdr = ip_hdr(skb); 347 348 src.proto = htons(ETH_P_IP); 349 src.ipv4.s_addr = iphdr->saddr; 350 if (ipv4_is_multicast(iphdr->daddr)) 351 return 0; 352 #if IS_ENABLED(CONFIG_IPV6) 353 } else if (ip_hdr(skb)->version == 6) { 354 struct ipv6hdr *iphdr = ipv6_hdr(skb); 355 356 src.proto = htons(ETH_P_IPV6); 357 src.ipv6 = iphdr->saddr; 358 if (ipv6_addr_is_multicast(&iphdr->daddr)) 359 return 0; 360 #endif 361 } else { 362 return 0; 363 } 364 365 if (likely(tipc_udp_is_known_peer(b, &src))) 366 return 0; 367 368 return tipc_udp_rcast_add(b, &src); 369 } 370 371 /* tipc_udp_recv - read data from bearer socket */ 372 static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb) 373 { 374 struct udp_bearer *ub; 375 struct tipc_bearer *b; 376 struct tipc_msg *hdr; 377 int err; 378 379 ub = rcu_dereference_sk_user_data(sk); 380 if (!ub) { 381 pr_err_ratelimited("Failed to get UDP bearer reference"); 382 goto out; 383 } 384 skb_pull(skb, sizeof(struct udphdr)); 385 hdr = buf_msg(skb); 386 387 b = rcu_dereference(ub->bearer); 388 if (!b) 389 goto out; 390 391 if (b && test_bit(0, &b->up)) { 392 TIPC_SKB_CB(skb)->flags = 0; 393 tipc_rcv(sock_net(sk), skb, b); 394 return 0; 395 } 396 397 if (unlikely(msg_user(hdr) == LINK_CONFIG)) { 398 err = tipc_udp_rcast_disc(b, skb); 399 if (err) 400 goto out; 401 } 402 403 out: 404 kfree_skb(skb); 405 return 0; 406 } 407 408 static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote) 409 { 410 int err = 0; 411 struct ip_mreqn mreqn; 412 struct sock *sk = ub->ubsock->sk; 413 414 if (ntohs(remote->proto) == ETH_P_IP) { 415 mreqn.imr_multiaddr = remote->ipv4; 416 mreqn.imr_ifindex = ub->ifindex; 417 err = ip_mc_join_group(sk, &mreqn); 418 #if IS_ENABLED(CONFIG_IPV6) 419 } else { 420 lock_sock(sk); 421 err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex, 422 &remote->ipv6); 423 release_sock(sk); 424 #endif 425 } 426 return err; 427 } 428 429 static int __tipc_nl_add_udp_addr(struct sk_buff *skb, 430 struct udp_media_addr *addr, int nla_t) 431 { 432 if (ntohs(addr->proto) == ETH_P_IP) { 433 struct sockaddr_in ip4; 434 435 memset(&ip4, 0, sizeof(ip4)); 436 ip4.sin_family = AF_INET; 437 ip4.sin_port = addr->port; 438 ip4.sin_addr.s_addr = addr->ipv4.s_addr; 439 if (nla_put(skb, nla_t, sizeof(ip4), &ip4)) 440 return -EMSGSIZE; 441 442 #if IS_ENABLED(CONFIG_IPV6) 443 } else if (ntohs(addr->proto) == ETH_P_IPV6) { 444 struct sockaddr_in6 ip6; 445 446 memset(&ip6, 0, sizeof(ip6)); 447 ip6.sin6_family = AF_INET6; 448 ip6.sin6_port = addr->port; 449 memcpy(&ip6.sin6_addr, &addr->ipv6, sizeof(struct in6_addr)); 450 if (nla_put(skb, nla_t, sizeof(ip6), &ip6)) 451 return -EMSGSIZE; 452 #endif 453 } 454 455 return 0; 456 } 457 458 int tipc_udp_nl_dump_remoteip(struct sk_buff *skb, struct netlink_callback *cb) 459 { 460 u32 bid = cb->args[0]; 461 u32 skip_cnt = cb->args[1]; 462 u32 portid = NETLINK_CB(cb->skb).portid; 463 struct udp_replicast *rcast, *tmp; 464 struct tipc_bearer *b; 465 struct udp_bearer *ub; 466 void *hdr; 467 int err; 468 int i; 469 470 if (!bid && !skip_cnt) { 471 struct nlattr **attrs = genl_dumpit_info(cb)->info.attrs; 472 struct net *net = sock_net(skb->sk); 473 struct nlattr *battrs[TIPC_NLA_BEARER_MAX + 1]; 474 char *bname; 475 476 if (!attrs[TIPC_NLA_BEARER]) 477 return -EINVAL; 478 479 err = nla_parse_nested_deprecated(battrs, TIPC_NLA_BEARER_MAX, 480 attrs[TIPC_NLA_BEARER], 481 tipc_nl_bearer_policy, NULL); 482 if (err) 483 return err; 484 485 if (!battrs[TIPC_NLA_BEARER_NAME]) 486 return -EINVAL; 487 488 bname = nla_data(battrs[TIPC_NLA_BEARER_NAME]); 489 490 rtnl_lock(); 491 b = tipc_bearer_find(net, bname); 492 if (!b) { 493 rtnl_unlock(); 494 return -EINVAL; 495 } 496 bid = b->identity; 497 } else { 498 struct net *net = sock_net(skb->sk); 499 struct tipc_net *tn = net_generic(net, tipc_net_id); 500 501 rtnl_lock(); 502 b = rtnl_dereference(tn->bearer_list[bid]); 503 if (!b) { 504 rtnl_unlock(); 505 return -EINVAL; 506 } 507 } 508 509 ub = rtnl_dereference(b->media_ptr); 510 if (!ub) { 511 rtnl_unlock(); 512 return -EINVAL; 513 } 514 515 i = 0; 516 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) { 517 if (i < skip_cnt) 518 goto count; 519 520 hdr = genlmsg_put(skb, portid, cb->nlh->nlmsg_seq, 521 &tipc_genl_family, NLM_F_MULTI, 522 TIPC_NL_BEARER_GET); 523 if (!hdr) 524 goto done; 525 526 err = __tipc_nl_add_udp_addr(skb, &rcast->addr, 527 TIPC_NLA_UDP_REMOTE); 528 if (err) { 529 genlmsg_cancel(skb, hdr); 530 goto done; 531 } 532 genlmsg_end(skb, hdr); 533 count: 534 i++; 535 } 536 done: 537 rtnl_unlock(); 538 cb->args[0] = bid; 539 cb->args[1] = i; 540 541 return skb->len; 542 } 543 544 int tipc_udp_nl_add_bearer_data(struct tipc_nl_msg *msg, struct tipc_bearer *b) 545 { 546 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value; 547 struct udp_media_addr *dst; 548 struct udp_bearer *ub; 549 struct nlattr *nest; 550 551 ub = rtnl_dereference(b->media_ptr); 552 if (!ub) 553 return -ENODEV; 554 555 nest = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_UDP_OPTS); 556 if (!nest) 557 goto msg_full; 558 559 if (__tipc_nl_add_udp_addr(msg->skb, src, TIPC_NLA_UDP_LOCAL)) 560 goto msg_full; 561 562 dst = (struct udp_media_addr *)&b->bcast_addr.value; 563 if (__tipc_nl_add_udp_addr(msg->skb, dst, TIPC_NLA_UDP_REMOTE)) 564 goto msg_full; 565 566 if (!list_empty(&ub->rcast.list)) { 567 if (nla_put_flag(msg->skb, TIPC_NLA_UDP_MULTI_REMOTEIP)) 568 goto msg_full; 569 } 570 571 nla_nest_end(msg->skb, nest); 572 return 0; 573 msg_full: 574 nla_nest_cancel(msg->skb, nest); 575 return -EMSGSIZE; 576 } 577 578 /** 579 * tipc_parse_udp_addr - build udp media address from netlink data 580 * @nla: netlink attribute containing sockaddr storage aligned address 581 * @addr: tipc media address to fill with address, port and protocol type 582 * @scope_id: IPv6 scope id pointer, not NULL indicates it's required 583 */ 584 585 static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr, 586 u32 *scope_id) 587 { 588 struct sockaddr_storage sa; 589 590 nla_memcpy(&sa, nla, sizeof(sa)); 591 if (sa.ss_family == AF_INET) { 592 struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa; 593 594 addr->proto = htons(ETH_P_IP); 595 addr->port = ip4->sin_port; 596 addr->ipv4.s_addr = ip4->sin_addr.s_addr; 597 return 0; 598 599 #if IS_ENABLED(CONFIG_IPV6) 600 } else if (sa.ss_family == AF_INET6) { 601 struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa; 602 603 addr->proto = htons(ETH_P_IPV6); 604 addr->port = ip6->sin6_port; 605 memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr)); 606 607 /* Scope ID is only interesting for local addresses */ 608 if (scope_id) { 609 int atype; 610 611 atype = ipv6_addr_type(&ip6->sin6_addr); 612 if (__ipv6_addr_needs_scope_id(atype) && 613 !ip6->sin6_scope_id) { 614 return -EINVAL; 615 } 616 617 *scope_id = ip6->sin6_scope_id ? : 0; 618 } 619 620 return 0; 621 #endif 622 } 623 return -EADDRNOTAVAIL; 624 } 625 626 int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr) 627 { 628 int err; 629 struct udp_media_addr addr = {0}; 630 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1]; 631 struct udp_media_addr *dst; 632 633 if (nla_parse_nested_deprecated(opts, TIPC_NLA_UDP_MAX, attr, tipc_nl_udp_policy, NULL)) 634 return -EINVAL; 635 636 if (!opts[TIPC_NLA_UDP_REMOTE]) 637 return -EINVAL; 638 639 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL); 640 if (err) 641 return err; 642 643 dst = (struct udp_media_addr *)&b->bcast_addr.value; 644 if (tipc_udp_is_mcast_addr(dst)) { 645 pr_err("Can't add remote ip to TIPC UDP multicast bearer\n"); 646 return -EINVAL; 647 } 648 649 if (tipc_udp_is_known_peer(b, &addr)) 650 return 0; 651 652 return tipc_udp_rcast_add(b, &addr); 653 } 654 655 /** 656 * tipc_udp_enable - callback to create a new udp bearer instance 657 * @net: network namespace 658 * @b: pointer to generic tipc_bearer 659 * @attrs: netlink bearer configuration 660 * 661 * validate the bearer parameters and initialize the udp bearer 662 * rtnl_lock should be held 663 */ 664 static int tipc_udp_enable(struct net *net, struct tipc_bearer *b, 665 struct nlattr *attrs[]) 666 { 667 int err = -EINVAL; 668 struct udp_bearer *ub; 669 struct udp_media_addr remote = {0}; 670 struct udp_media_addr local = {0}; 671 struct udp_port_cfg udp_conf = {0}; 672 struct udp_tunnel_sock_cfg tuncfg = {NULL}; 673 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1]; 674 u8 node_id[NODE_ID_LEN] = {0,}; 675 struct net_device *dev; 676 int rmcast = 0; 677 678 ub = kzalloc(sizeof(*ub), GFP_ATOMIC); 679 if (!ub) 680 return -ENOMEM; 681 682 INIT_LIST_HEAD(&ub->rcast.list); 683 684 if (!attrs[TIPC_NLA_BEARER_UDP_OPTS]) 685 goto err; 686 687 if (nla_parse_nested_deprecated(opts, TIPC_NLA_UDP_MAX, attrs[TIPC_NLA_BEARER_UDP_OPTS], tipc_nl_udp_policy, NULL)) 688 goto err; 689 690 if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) { 691 pr_err("Invalid UDP bearer configuration"); 692 err = -EINVAL; 693 goto err; 694 } 695 696 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local, 697 &ub->ifindex); 698 if (err) 699 goto err; 700 701 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL); 702 if (err) 703 goto err; 704 705 if (remote.proto != local.proto) { 706 err = -EINVAL; 707 goto err; 708 } 709 710 /* Checking remote ip address */ 711 rmcast = tipc_udp_is_mcast_addr(&remote); 712 713 /* Autoconfigure own node identity if needed */ 714 if (!tipc_own_id(net)) { 715 memcpy(node_id, local.ipv6.in6_u.u6_addr8, 16); 716 tipc_net_init(net, node_id, 0); 717 } 718 if (!tipc_own_id(net)) { 719 pr_warn("Failed to set node id, please configure manually\n"); 720 err = -EINVAL; 721 goto err; 722 } 723 724 b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP; 725 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT; 726 rcu_assign_pointer(b->media_ptr, ub); 727 rcu_assign_pointer(ub->bearer, b); 728 tipc_udp_media_addr_set(&b->addr, &local); 729 if (local.proto == htons(ETH_P_IP)) { 730 dev = __ip_dev_find(net, local.ipv4.s_addr, false); 731 if (!dev) { 732 err = -ENODEV; 733 goto err; 734 } 735 udp_conf.family = AF_INET; 736 737 /* Switch to use ANY to receive packets from group */ 738 if (rmcast) 739 udp_conf.local_ip.s_addr = htonl(INADDR_ANY); 740 else 741 udp_conf.local_ip.s_addr = local.ipv4.s_addr; 742 udp_conf.use_udp_checksums = false; 743 ub->ifindex = dev->ifindex; 744 b->encap_hlen = sizeof(struct iphdr) + sizeof(struct udphdr); 745 b->mtu = b->media->mtu; 746 #if IS_ENABLED(CONFIG_IPV6) 747 } else if (local.proto == htons(ETH_P_IPV6)) { 748 dev = ub->ifindex ? __dev_get_by_index(net, ub->ifindex) : NULL; 749 dev = ipv6_dev_find(net, &local.ipv6, dev); 750 if (!dev) { 751 err = -ENODEV; 752 goto err; 753 } 754 udp_conf.family = AF_INET6; 755 udp_conf.use_udp6_tx_checksums = true; 756 udp_conf.use_udp6_rx_checksums = true; 757 if (rmcast) 758 udp_conf.local_ip6 = in6addr_any; 759 else 760 udp_conf.local_ip6 = local.ipv6; 761 ub->ifindex = dev->ifindex; 762 b->encap_hlen = sizeof(struct ipv6hdr) + sizeof(struct udphdr); 763 b->mtu = 1280; 764 #endif 765 } else { 766 err = -EAFNOSUPPORT; 767 goto err; 768 } 769 udp_conf.local_udp_port = local.port; 770 err = udp_sock_create(net, &udp_conf, &ub->ubsock); 771 if (err) 772 goto err; 773 tuncfg.sk_user_data = ub; 774 tuncfg.encap_type = 1; 775 tuncfg.encap_rcv = tipc_udp_recv; 776 tuncfg.encap_destroy = NULL; 777 setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg); 778 779 err = dst_cache_init(&ub->rcast.dst_cache, GFP_ATOMIC); 780 if (err) 781 goto free; 782 783 /* 784 * The bcast media address port is used for all peers and the ip 785 * is used if it's a multicast address. 786 */ 787 memcpy(&b->bcast_addr.value, &remote, sizeof(remote)); 788 if (rmcast) 789 err = enable_mcast(ub, &remote); 790 else 791 err = tipc_udp_rcast_add(b, &remote); 792 if (err) 793 goto free; 794 795 return 0; 796 797 free: 798 dst_cache_destroy(&ub->rcast.dst_cache); 799 udp_tunnel_sock_release(ub->ubsock); 800 err: 801 kfree(ub); 802 return err; 803 } 804 805 /* cleanup_bearer - break the socket/bearer association */ 806 static void cleanup_bearer(struct work_struct *work) 807 { 808 struct udp_bearer *ub = container_of(work, struct udp_bearer, work); 809 struct udp_replicast *rcast, *tmp; 810 811 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) { 812 dst_cache_destroy(&rcast->dst_cache); 813 list_del_rcu(&rcast->list); 814 kfree_rcu(rcast, rcu); 815 } 816 817 atomic_dec(&tipc_net(sock_net(ub->ubsock->sk))->wq_count); 818 dst_cache_destroy(&ub->rcast.dst_cache); 819 udp_tunnel_sock_release(ub->ubsock); 820 synchronize_net(); 821 kfree(ub); 822 } 823 824 /* tipc_udp_disable - detach bearer from socket */ 825 static void tipc_udp_disable(struct tipc_bearer *b) 826 { 827 struct udp_bearer *ub; 828 829 ub = rtnl_dereference(b->media_ptr); 830 if (!ub) { 831 pr_err("UDP bearer instance not found\n"); 832 return; 833 } 834 sock_set_flag(ub->ubsock->sk, SOCK_DEAD); 835 RCU_INIT_POINTER(ub->bearer, NULL); 836 837 /* sock_release need to be done outside of rtnl lock */ 838 atomic_inc(&tipc_net(sock_net(ub->ubsock->sk))->wq_count); 839 INIT_WORK(&ub->work, cleanup_bearer); 840 schedule_work(&ub->work); 841 } 842 843 struct tipc_media udp_media_info = { 844 .send_msg = tipc_udp_send_msg, 845 .enable_media = tipc_udp_enable, 846 .disable_media = tipc_udp_disable, 847 .addr2str = tipc_udp_addr2str, 848 .addr2msg = tipc_udp_addr2msg, 849 .msg2addr = tipc_udp_msg2addr, 850 .priority = TIPC_DEF_LINK_PRI, 851 .tolerance = TIPC_DEF_LINK_TOL, 852 .min_win = TIPC_DEF_LINK_WIN, 853 .max_win = TIPC_DEF_LINK_WIN, 854 .mtu = TIPC_DEF_LINK_UDP_MTU, 855 .type_id = TIPC_MEDIA_TYPE_UDP, 856 .hwaddr_len = 0, 857 .name = "udp" 858 }; 859