Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 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> |
Marcelo Ricardo Leitner | 446981e | 2015-03-19 16:47:58 -0300 | [diff] [blame] | 47 | #include <net/addrconf.h> |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 48 | #include <linux/tipc_netlink.h> |
| 49 | #include "core.h" |
| 50 | #include "bearer.h" |
Richard Alpe | 49cc66e | 2016-03-04 17:04:42 +0100 | [diff] [blame] | 51 | #include "netlink.h" |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 52 | #include "msg.h" |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 53 | |
| 54 | /* IANA assigned UDP port */ |
| 55 | #define UDP_PORT_DEFAULT 6118 |
| 56 | |
Richard Alpe | 9bd160b | 2016-03-14 09:43:52 +0100 | [diff] [blame] | 57 | #define UDP_MIN_HEADROOM 48 |
Jon Paul Maloy | e535679 | 2015-10-19 11:43:11 -0400 | [diff] [blame] | 58 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 59 | /** |
| 60 | * struct udp_media_addr - IP/UDP addressing information |
| 61 | * |
| 62 | * This is the bearer level originating address used in neighbor discovery |
| 63 | * messages, and all fields should be in network byte order |
| 64 | */ |
| 65 | struct udp_media_addr { |
| 66 | __be16 proto; |
Richard Alpe | bc3a334 | 2016-06-27 13:34:07 +0200 | [diff] [blame] | 67 | __be16 port; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 68 | union { |
| 69 | struct in_addr ipv4; |
| 70 | struct in6_addr ipv6; |
| 71 | }; |
| 72 | }; |
| 73 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 74 | /* struct udp_replicast - container for UDP remote addresses */ |
| 75 | struct udp_replicast { |
| 76 | struct udp_media_addr addr; |
| 77 | struct rcu_head rcu; |
| 78 | struct list_head list; |
| 79 | }; |
| 80 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 81 | /** |
| 82 | * struct udp_bearer - ip/udp bearer data structure |
| 83 | * @bearer: associated generic tipc bearer |
| 84 | * @ubsock: bearer associated socket |
| 85 | * @ifindex: local address scope |
| 86 | * @work: used to schedule deferred work on a bearer |
| 87 | */ |
| 88 | struct udp_bearer { |
| 89 | struct tipc_bearer __rcu *bearer; |
| 90 | struct socket *ubsock; |
| 91 | u32 ifindex; |
| 92 | struct work_struct work; |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 93 | struct udp_replicast rcast; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
Richard Alpe | 1ca73e3 | 2016-08-26 10:52:52 +0200 | [diff] [blame] | 96 | static int tipc_udp_is_mcast_addr(struct udp_media_addr *addr) |
| 97 | { |
| 98 | if (ntohs(addr->proto) == ETH_P_IP) |
| 99 | return ipv4_is_multicast(addr->ipv4.s_addr); |
| 100 | #if IS_ENABLED(CONFIG_IPV6) |
| 101 | else |
| 102 | return ipv6_addr_is_multicast(&addr->ipv6); |
| 103 | #endif |
| 104 | return 0; |
| 105 | } |
| 106 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 107 | /* udp_media_addr_set - convert a ip/udp address to a TIPC media address */ |
| 108 | static void tipc_udp_media_addr_set(struct tipc_media_addr *addr, |
| 109 | struct udp_media_addr *ua) |
| 110 | { |
| 111 | memset(addr, 0, sizeof(struct tipc_media_addr)); |
| 112 | addr->media_id = TIPC_MEDIA_TYPE_UDP; |
| 113 | memcpy(addr->value, ua, sizeof(struct udp_media_addr)); |
Richard Alpe | 1ca73e3 | 2016-08-26 10:52:52 +0200 | [diff] [blame] | 114 | |
| 115 | if (tipc_udp_is_mcast_addr(ua)) |
| 116 | addr->broadcast = 1; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* tipc_udp_addr2str - convert ip/udp address to string */ |
| 120 | static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size) |
| 121 | { |
| 122 | struct udp_media_addr *ua = (struct udp_media_addr *)&a->value; |
| 123 | |
| 124 | if (ntohs(ua->proto) == ETH_P_IP) |
Richard Alpe | bc3a334 | 2016-06-27 13:34:07 +0200 | [diff] [blame] | 125 | snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port)); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 126 | else if (ntohs(ua->proto) == ETH_P_IPV6) |
Richard Alpe | bc3a334 | 2016-06-27 13:34:07 +0200 | [diff] [blame] | 127 | snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port)); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 128 | else |
| 129 | pr_err("Invalid UDP media address\n"); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */ |
| 134 | static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a, |
| 135 | char *msg) |
| 136 | { |
| 137 | struct udp_media_addr *ua; |
| 138 | |
| 139 | ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET); |
| 140 | if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP) |
| 141 | return -EINVAL; |
| 142 | tipc_udp_media_addr_set(a, ua); |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */ |
| 147 | static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a) |
| 148 | { |
| 149 | memset(msg, 0, TIPC_MEDIA_INFO_SIZE); |
| 150 | msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP; |
| 151 | memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value, |
| 152 | sizeof(struct udp_media_addr)); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /* tipc_send_msg - enqueue a send request */ |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 157 | static int tipc_udp_xmit(struct net *net, struct sk_buff *skb, |
| 158 | struct udp_bearer *ub, struct udp_media_addr *src, |
| 159 | struct udp_media_addr *dst) |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 160 | { |
| 161 | int ttl, err = 0; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 162 | struct rtable *rt; |
| 163 | |
Erik Hugne | 4fee6be8 | 2015-03-09 10:19:31 +0100 | [diff] [blame] | 164 | if (dst->proto == htons(ETH_P_IP)) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 165 | struct flowi4 fl = { |
| 166 | .daddr = dst->ipv4.s_addr, |
| 167 | .saddr = src->ipv4.s_addr, |
Jon Paul Maloy | 7214bcf | 2015-10-22 08:51:45 -0400 | [diff] [blame] | 168 | .flowi4_mark = skb->mark, |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 169 | .flowi4_proto = IPPROTO_UDP |
| 170 | }; |
| 171 | rt = ip_route_output_key(net, &fl); |
| 172 | if (IS_ERR(rt)) { |
| 173 | err = PTR_ERR(rt); |
| 174 | goto tx_error; |
| 175 | } |
Richard Alpe | 9b30096 | 2016-03-03 14:20:40 +0100 | [diff] [blame] | 176 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 177 | ttl = ip4_dst_hoplimit(&rt->dst); |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 178 | udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr, |
Richard Alpe | bc3a334 | 2016-06-27 13:34:07 +0200 | [diff] [blame] | 179 | dst->ipv4.s_addr, 0, ttl, 0, src->port, |
| 180 | dst->port, false, true); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 181 | #if IS_ENABLED(CONFIG_IPV6) |
| 182 | } else { |
| 183 | struct dst_entry *ndst; |
| 184 | struct flowi6 fl6 = { |
| 185 | .flowi6_oif = ub->ifindex, |
| 186 | .daddr = dst->ipv6, |
| 187 | .saddr = src->ipv6, |
| 188 | .flowi6_proto = IPPROTO_UDP |
| 189 | }; |
Sabrina Dubroca | badbe56 | 2019-12-04 15:35:53 +0100 | [diff] [blame] | 190 | ndst = ipv6_stub->ipv6_dst_lookup_flow(net, |
| 191 | ub->ubsock->sk, |
| 192 | &fl6, NULL); |
| 193 | if (IS_ERR(ndst)) { |
| 194 | err = PTR_ERR(ndst); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 195 | goto tx_error; |
Sabrina Dubroca | badbe56 | 2019-12-04 15:35:53 +0100 | [diff] [blame] | 196 | } |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 197 | ttl = ip6_dst_hoplimit(ndst); |
Xin Long | 2f973fe | 2019-06-17 21:34:15 +0800 | [diff] [blame] | 198 | err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb, NULL, |
| 199 | &src->ipv6, &dst->ipv6, 0, ttl, 0, |
| 200 | src->port, dst->port, false); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 201 | #endif |
| 202 | } |
| 203 | return err; |
| 204 | |
| 205 | tx_error: |
Jon Paul Maloy | 7214bcf | 2015-10-22 08:51:45 -0400 | [diff] [blame] | 206 | kfree_skb(skb); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 207 | return err; |
| 208 | } |
| 209 | |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 210 | static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb, |
| 211 | struct tipc_bearer *b, |
| 212 | struct tipc_media_addr *addr) |
| 213 | { |
| 214 | struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value; |
| 215 | struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value; |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 216 | struct udp_replicast *rcast; |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 217 | struct udp_bearer *ub; |
| 218 | int err = 0; |
| 219 | |
| 220 | if (skb_headroom(skb) < UDP_MIN_HEADROOM) { |
| 221 | err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC); |
| 222 | if (err) |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 223 | goto out; |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | skb_set_inner_protocol(skb, htons(ETH_P_TIPC)); |
| 227 | ub = rcu_dereference_rtnl(b->media_ptr); |
| 228 | if (!ub) { |
| 229 | err = -ENODEV; |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 230 | goto out; |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 233 | if (!addr->broadcast || list_empty(&ub->rcast.list)) |
| 234 | return tipc_udp_xmit(net, skb, ub, src, dst); |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 235 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 236 | /* Replicast, send an skb to each configured IP address */ |
| 237 | list_for_each_entry_rcu(rcast, &ub->rcast.list, list) { |
| 238 | struct sk_buff *_skb; |
| 239 | |
| 240 | _skb = pskb_copy(skb, GFP_ATOMIC); |
| 241 | if (!_skb) { |
| 242 | err = -ENOMEM; |
| 243 | goto out; |
| 244 | } |
| 245 | |
| 246 | err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr); |
Cong Wang | 336e822 | 2018-12-10 12:45:45 -0800 | [diff] [blame] | 247 | if (err) |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 248 | goto out; |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 249 | } |
| 250 | err = 0; |
| 251 | out: |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 252 | kfree_skb(skb); |
| 253 | return err; |
| 254 | } |
| 255 | |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame] | 256 | static bool tipc_udp_is_known_peer(struct tipc_bearer *b, |
| 257 | struct udp_media_addr *addr) |
| 258 | { |
| 259 | struct udp_replicast *rcast, *tmp; |
| 260 | struct udp_bearer *ub; |
| 261 | |
| 262 | ub = rcu_dereference_rtnl(b->media_ptr); |
| 263 | if (!ub) { |
| 264 | pr_err_ratelimited("UDP bearer instance not found\n"); |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) { |
| 269 | if (!memcmp(&rcast->addr, addr, sizeof(struct udp_media_addr))) |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | return false; |
| 274 | } |
| 275 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 276 | static int tipc_udp_rcast_add(struct tipc_bearer *b, |
| 277 | struct udp_media_addr *addr) |
| 278 | { |
| 279 | struct udp_replicast *rcast; |
| 280 | struct udp_bearer *ub; |
| 281 | |
| 282 | ub = rcu_dereference_rtnl(b->media_ptr); |
| 283 | if (!ub) |
| 284 | return -ENODEV; |
| 285 | |
| 286 | rcast = kmalloc(sizeof(*rcast), GFP_ATOMIC); |
| 287 | if (!rcast) |
| 288 | return -ENOMEM; |
| 289 | |
| 290 | memcpy(&rcast->addr, addr, sizeof(struct udp_media_addr)); |
| 291 | |
| 292 | if (ntohs(addr->proto) == ETH_P_IP) |
| 293 | pr_info("New replicast peer: %pI4\n", &rcast->addr.ipv4); |
| 294 | #if IS_ENABLED(CONFIG_IPV6) |
| 295 | else if (ntohs(addr->proto) == ETH_P_IPV6) |
| 296 | pr_info("New replicast peer: %pI6\n", &rcast->addr.ipv6); |
| 297 | #endif |
| 298 | |
| 299 | list_add_rcu(&rcast->list, &ub->rcast.list); |
| 300 | return 0; |
| 301 | } |
| 302 | |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame] | 303 | static int tipc_udp_rcast_disc(struct tipc_bearer *b, struct sk_buff *skb) |
| 304 | { |
| 305 | struct udp_media_addr src = {0}; |
| 306 | struct udp_media_addr *dst; |
| 307 | |
| 308 | dst = (struct udp_media_addr *)&b->bcast_addr.value; |
| 309 | if (tipc_udp_is_mcast_addr(dst)) |
| 310 | return 0; |
| 311 | |
| 312 | src.port = udp_hdr(skb)->source; |
| 313 | |
| 314 | if (ip_hdr(skb)->version == 4) { |
| 315 | struct iphdr *iphdr = ip_hdr(skb); |
| 316 | |
| 317 | src.proto = htons(ETH_P_IP); |
| 318 | src.ipv4.s_addr = iphdr->saddr; |
| 319 | if (ipv4_is_multicast(iphdr->daddr)) |
| 320 | return 0; |
| 321 | #if IS_ENABLED(CONFIG_IPV6) |
| 322 | } else if (ip_hdr(skb)->version == 6) { |
| 323 | struct ipv6hdr *iphdr = ipv6_hdr(skb); |
| 324 | |
| 325 | src.proto = htons(ETH_P_IPV6); |
| 326 | src.ipv6 = iphdr->saddr; |
| 327 | if (ipv6_addr_is_multicast(&iphdr->daddr)) |
| 328 | return 0; |
| 329 | #endif |
| 330 | } else { |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | if (likely(tipc_udp_is_known_peer(b, &src))) |
| 335 | return 0; |
| 336 | |
| 337 | return tipc_udp_rcast_add(b, &src); |
| 338 | } |
| 339 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 340 | /* tipc_udp_recv - read data from bearer socket */ |
| 341 | static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb) |
| 342 | { |
| 343 | struct udp_bearer *ub; |
| 344 | struct tipc_bearer *b; |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame] | 345 | struct tipc_msg *hdr; |
| 346 | int err; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 347 | |
| 348 | ub = rcu_dereference_sk_user_data(sk); |
| 349 | if (!ub) { |
| 350 | pr_err_ratelimited("Failed to get UDP bearer reference"); |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame] | 351 | goto out; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 352 | } |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 353 | skb_pull(skb, sizeof(struct udphdr)); |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame] | 354 | hdr = buf_msg(skb); |
| 355 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 356 | rcu_read_lock(); |
| 357 | b = rcu_dereference_rtnl(ub->bearer); |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame] | 358 | if (!b) |
| 359 | goto rcu_out; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 360 | |
Jon Paul Maloy | 0d051bf | 2016-08-16 11:53:50 -0400 | [diff] [blame] | 361 | if (b && test_bit(0, &b->up)) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 362 | tipc_rcv(sock_net(sk), skb, b); |
| 363 | rcu_read_unlock(); |
| 364 | return 0; |
| 365 | } |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame] | 366 | |
| 367 | if (unlikely(msg_user(hdr) == LINK_CONFIG)) { |
| 368 | err = tipc_udp_rcast_disc(b, skb); |
| 369 | if (err) |
| 370 | goto rcu_out; |
| 371 | } |
| 372 | |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame] | 373 | rcu_out: |
| 374 | rcu_read_unlock(); |
| 375 | out: |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 376 | kfree_skb(skb); |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote) |
| 381 | { |
| 382 | int err = 0; |
| 383 | struct ip_mreqn mreqn; |
| 384 | struct sock *sk = ub->ubsock->sk; |
| 385 | |
| 386 | if (ntohs(remote->proto) == ETH_P_IP) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 387 | mreqn.imr_multiaddr = remote->ipv4; |
| 388 | mreqn.imr_ifindex = ub->ifindex; |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 389 | err = ip_mc_join_group(sk, &mreqn); |
Marcelo Ricardo Leitner | 446981e | 2015-03-19 16:47:58 -0300 | [diff] [blame] | 390 | #if IS_ENABLED(CONFIG_IPV6) |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 391 | } else { |
Marcelo Ricardo Leitner | 446981e | 2015-03-19 16:47:58 -0300 | [diff] [blame] | 392 | err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex, |
| 393 | &remote->ipv6); |
| 394 | #endif |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 395 | } |
| 396 | return err; |
| 397 | } |
| 398 | |
Richard Alpe | fdb3acc | 2016-08-26 10:52:55 +0200 | [diff] [blame] | 399 | static int __tipc_nl_add_udp_addr(struct sk_buff *skb, |
| 400 | struct udp_media_addr *addr, int nla_t) |
| 401 | { |
| 402 | if (ntohs(addr->proto) == ETH_P_IP) { |
| 403 | struct sockaddr_in ip4; |
| 404 | |
Dan Carpenter | 7307616 | 2016-10-13 11:06:06 +0300 | [diff] [blame] | 405 | memset(&ip4, 0, sizeof(ip4)); |
Richard Alpe | fdb3acc | 2016-08-26 10:52:55 +0200 | [diff] [blame] | 406 | ip4.sin_family = AF_INET; |
| 407 | ip4.sin_port = addr->port; |
| 408 | ip4.sin_addr.s_addr = addr->ipv4.s_addr; |
| 409 | if (nla_put(skb, nla_t, sizeof(ip4), &ip4)) |
| 410 | return -EMSGSIZE; |
| 411 | |
| 412 | #if IS_ENABLED(CONFIG_IPV6) |
| 413 | } else if (ntohs(addr->proto) == ETH_P_IPV6) { |
| 414 | struct sockaddr_in6 ip6; |
| 415 | |
Dan Carpenter | 7307616 | 2016-10-13 11:06:06 +0300 | [diff] [blame] | 416 | memset(&ip6, 0, sizeof(ip6)); |
Richard Alpe | fdb3acc | 2016-08-26 10:52:55 +0200 | [diff] [blame] | 417 | ip6.sin6_family = AF_INET6; |
| 418 | ip6.sin6_port = addr->port; |
| 419 | memcpy(&ip6.sin6_addr, &addr->ipv6, sizeof(struct in6_addr)); |
| 420 | if (nla_put(skb, nla_t, sizeof(ip6), &ip6)) |
| 421 | return -EMSGSIZE; |
| 422 | #endif |
| 423 | } |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
Richard Alpe | 832629c | 2016-08-26 10:52:56 +0200 | [diff] [blame] | 428 | int tipc_udp_nl_dump_remoteip(struct sk_buff *skb, struct netlink_callback *cb) |
| 429 | { |
| 430 | u32 bid = cb->args[0]; |
| 431 | u32 skip_cnt = cb->args[1]; |
| 432 | u32 portid = NETLINK_CB(cb->skb).portid; |
| 433 | struct udp_replicast *rcast, *tmp; |
| 434 | struct tipc_bearer *b; |
| 435 | struct udp_bearer *ub; |
| 436 | void *hdr; |
| 437 | int err; |
| 438 | int i; |
| 439 | |
| 440 | if (!bid && !skip_cnt) { |
| 441 | struct net *net = sock_net(skb->sk); |
| 442 | struct nlattr *battrs[TIPC_NLA_BEARER_MAX + 1]; |
| 443 | struct nlattr **attrs; |
| 444 | char *bname; |
| 445 | |
| 446 | err = tipc_nlmsg_parse(cb->nlh, &attrs); |
| 447 | if (err) |
| 448 | return err; |
| 449 | |
| 450 | if (!attrs[TIPC_NLA_BEARER]) |
| 451 | return -EINVAL; |
| 452 | |
| 453 | err = nla_parse_nested(battrs, TIPC_NLA_BEARER_MAX, |
| 454 | attrs[TIPC_NLA_BEARER], |
| 455 | tipc_nl_bearer_policy); |
| 456 | if (err) |
| 457 | return err; |
| 458 | |
| 459 | if (!battrs[TIPC_NLA_BEARER_NAME]) |
| 460 | return -EINVAL; |
| 461 | |
| 462 | bname = nla_data(battrs[TIPC_NLA_BEARER_NAME]); |
| 463 | |
| 464 | rtnl_lock(); |
| 465 | b = tipc_bearer_find(net, bname); |
| 466 | if (!b) { |
| 467 | rtnl_unlock(); |
| 468 | return -EINVAL; |
| 469 | } |
| 470 | bid = b->identity; |
| 471 | } else { |
| 472 | struct net *net = sock_net(skb->sk); |
| 473 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
| 474 | |
| 475 | rtnl_lock(); |
| 476 | b = rtnl_dereference(tn->bearer_list[bid]); |
| 477 | if (!b) { |
| 478 | rtnl_unlock(); |
| 479 | return -EINVAL; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | ub = rcu_dereference_rtnl(b->media_ptr); |
| 484 | if (!ub) { |
| 485 | rtnl_unlock(); |
| 486 | return -EINVAL; |
| 487 | } |
| 488 | |
| 489 | i = 0; |
| 490 | list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) { |
| 491 | if (i < skip_cnt) |
| 492 | goto count; |
| 493 | |
| 494 | hdr = genlmsg_put(skb, portid, cb->nlh->nlmsg_seq, |
| 495 | &tipc_genl_family, NLM_F_MULTI, |
| 496 | TIPC_NL_BEARER_GET); |
| 497 | if (!hdr) |
| 498 | goto done; |
| 499 | |
| 500 | err = __tipc_nl_add_udp_addr(skb, &rcast->addr, |
| 501 | TIPC_NLA_UDP_REMOTE); |
| 502 | if (err) { |
| 503 | genlmsg_cancel(skb, hdr); |
| 504 | goto done; |
| 505 | } |
| 506 | genlmsg_end(skb, hdr); |
| 507 | count: |
| 508 | i++; |
| 509 | } |
| 510 | done: |
| 511 | rtnl_unlock(); |
| 512 | cb->args[0] = bid; |
| 513 | cb->args[1] = i; |
| 514 | |
| 515 | return skb->len; |
| 516 | } |
| 517 | |
Richard Alpe | fdb3acc | 2016-08-26 10:52:55 +0200 | [diff] [blame] | 518 | int tipc_udp_nl_add_bearer_data(struct tipc_nl_msg *msg, struct tipc_bearer *b) |
| 519 | { |
| 520 | struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value; |
| 521 | struct udp_media_addr *dst; |
| 522 | struct udp_bearer *ub; |
| 523 | struct nlattr *nest; |
| 524 | |
| 525 | ub = rcu_dereference_rtnl(b->media_ptr); |
| 526 | if (!ub) |
| 527 | return -ENODEV; |
| 528 | |
| 529 | nest = nla_nest_start(msg->skb, TIPC_NLA_BEARER_UDP_OPTS); |
| 530 | if (!nest) |
| 531 | goto msg_full; |
| 532 | |
| 533 | if (__tipc_nl_add_udp_addr(msg->skb, src, TIPC_NLA_UDP_LOCAL)) |
| 534 | goto msg_full; |
| 535 | |
| 536 | dst = (struct udp_media_addr *)&b->bcast_addr.value; |
| 537 | if (__tipc_nl_add_udp_addr(msg->skb, dst, TIPC_NLA_UDP_REMOTE)) |
| 538 | goto msg_full; |
| 539 | |
| 540 | if (!list_empty(&ub->rcast.list)) { |
| 541 | if (nla_put_flag(msg->skb, TIPC_NLA_UDP_MULTI_REMOTEIP)) |
| 542 | goto msg_full; |
| 543 | } |
| 544 | |
| 545 | nla_nest_end(msg->skb, nest); |
| 546 | return 0; |
| 547 | msg_full: |
| 548 | nla_nest_cancel(msg->skb, nest); |
| 549 | return -EMSGSIZE; |
| 550 | } |
| 551 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 552 | /** |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 553 | * tipc_parse_udp_addr - build udp media address from netlink data |
| 554 | * @nlattr: netlink attribute containing sockaddr storage aligned address |
| 555 | * @addr: tipc media address to fill with address, port and protocol type |
| 556 | * @scope_id: IPv6 scope id pointer, not NULL indicates it's required |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 557 | */ |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 558 | |
| 559 | static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr, |
| 560 | u32 *scope_id) |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 561 | { |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 562 | struct sockaddr_storage sa; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 563 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 564 | nla_memcpy(&sa, nla, sizeof(sa)); |
| 565 | if (sa.ss_family == AF_INET) { |
| 566 | struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 567 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 568 | addr->proto = htons(ETH_P_IP); |
| 569 | addr->port = ip4->sin_port; |
| 570 | addr->ipv4.s_addr = ip4->sin_addr.s_addr; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 571 | return 0; |
| 572 | |
| 573 | #if IS_ENABLED(CONFIG_IPV6) |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 574 | } else if (sa.ss_family == AF_INET6) { |
| 575 | struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 576 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 577 | addr->proto = htons(ETH_P_IPV6); |
| 578 | addr->port = ip6->sin6_port; |
| 579 | memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr)); |
Richard Alpe | 34f65db | 2016-03-03 14:20:43 +0100 | [diff] [blame] | 580 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 581 | /* Scope ID is only interesting for local addresses */ |
| 582 | if (scope_id) { |
| 583 | int atype; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 584 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 585 | atype = ipv6_addr_type(&ip6->sin6_addr); |
| 586 | if (__ipv6_addr_needs_scope_id(atype) && |
| 587 | !ip6->sin6_scope_id) { |
| 588 | return -EINVAL; |
| 589 | } |
| 590 | |
| 591 | *scope_id = ip6->sin6_scope_id ? : 0; |
| 592 | } |
| 593 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 594 | return 0; |
| 595 | #endif |
| 596 | } |
| 597 | return -EADDRNOTAVAIL; |
| 598 | } |
| 599 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 600 | int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr) |
| 601 | { |
| 602 | int err; |
| 603 | struct udp_media_addr addr = {0}; |
| 604 | struct nlattr *opts[TIPC_NLA_UDP_MAX + 1]; |
| 605 | struct udp_media_addr *dst; |
| 606 | |
| 607 | if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX, attr, tipc_nl_udp_policy)) |
| 608 | return -EINVAL; |
| 609 | |
| 610 | if (!opts[TIPC_NLA_UDP_REMOTE]) |
| 611 | return -EINVAL; |
| 612 | |
| 613 | err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL); |
| 614 | if (err) |
| 615 | return err; |
| 616 | |
| 617 | dst = (struct udp_media_addr *)&b->bcast_addr.value; |
| 618 | if (tipc_udp_is_mcast_addr(dst)) { |
| 619 | pr_err("Can't add remote ip to TIPC UDP multicast bearer\n"); |
| 620 | return -EINVAL; |
| 621 | } |
| 622 | |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame] | 623 | if (tipc_udp_is_known_peer(b, &addr)) |
| 624 | return 0; |
| 625 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 626 | return tipc_udp_rcast_add(b, &addr); |
| 627 | } |
| 628 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 629 | /** |
| 630 | * tipc_udp_enable - callback to create a new udp bearer instance |
| 631 | * @net: network namespace |
| 632 | * @b: pointer to generic tipc_bearer |
| 633 | * @attrs: netlink bearer configuration |
| 634 | * |
| 635 | * validate the bearer parameters and initialize the udp bearer |
| 636 | * rtnl_lock should be held |
| 637 | */ |
| 638 | static int tipc_udp_enable(struct net *net, struct tipc_bearer *b, |
| 639 | struct nlattr *attrs[]) |
| 640 | { |
| 641 | int err = -EINVAL; |
| 642 | struct udp_bearer *ub; |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 643 | struct udp_media_addr remote = {0}; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 644 | struct udp_media_addr local = {0}; |
| 645 | struct udp_port_cfg udp_conf = {0}; |
Erik Hugne | 4fee6be8 | 2015-03-09 10:19:31 +0100 | [diff] [blame] | 646 | struct udp_tunnel_sock_cfg tuncfg = {NULL}; |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 647 | struct nlattr *opts[TIPC_NLA_UDP_MAX + 1]; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 648 | |
| 649 | ub = kzalloc(sizeof(*ub), GFP_ATOMIC); |
| 650 | if (!ub) |
| 651 | return -ENOMEM; |
| 652 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 653 | INIT_LIST_HEAD(&ub->rcast.list); |
| 654 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 655 | if (!attrs[TIPC_NLA_BEARER_UDP_OPTS]) |
| 656 | goto err; |
| 657 | |
| 658 | if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX, |
| 659 | attrs[TIPC_NLA_BEARER_UDP_OPTS], |
| 660 | tipc_nl_udp_policy)) |
| 661 | goto err; |
| 662 | |
| 663 | if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) { |
| 664 | pr_err("Invalid UDP bearer configuration"); |
Wei Yongjun | c20cb81 | 2016-09-10 00:56:55 +0000 | [diff] [blame] | 665 | err = -EINVAL; |
| 666 | goto err; |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local, |
| 670 | &ub->ifindex); |
| 671 | if (err) |
| 672 | goto err; |
| 673 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 674 | err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 675 | if (err) |
| 676 | goto err; |
| 677 | |
Cong Wang | 6021678 | 2018-12-10 15:23:30 -0800 | [diff] [blame] | 678 | if (remote.proto != local.proto) { |
| 679 | err = -EINVAL; |
| 680 | goto err; |
| 681 | } |
| 682 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 683 | b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP; |
| 684 | b->bcast_addr.broadcast = 1; |
| 685 | rcu_assign_pointer(b->media_ptr, ub); |
| 686 | rcu_assign_pointer(ub->bearer, b); |
| 687 | tipc_udp_media_addr_set(&b->addr, &local); |
Erik Hugne | 4fee6be8 | 2015-03-09 10:19:31 +0100 | [diff] [blame] | 688 | if (local.proto == htons(ETH_P_IP)) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 689 | struct net_device *dev; |
| 690 | |
| 691 | dev = __ip_dev_find(net, local.ipv4.s_addr, false); |
| 692 | if (!dev) { |
| 693 | err = -ENODEV; |
| 694 | goto err; |
| 695 | } |
| 696 | udp_conf.family = AF_INET; |
| 697 | udp_conf.local_ip.s_addr = htonl(INADDR_ANY); |
| 698 | udp_conf.use_udp_checksums = false; |
| 699 | ub->ifindex = dev->ifindex; |
Michal Kubeček | 3de81b7 | 2016-12-02 09:33:41 +0100 | [diff] [blame] | 700 | if (tipc_mtu_bad(dev, sizeof(struct iphdr) + |
| 701 | sizeof(struct udphdr))) { |
| 702 | err = -EINVAL; |
| 703 | goto err; |
| 704 | } |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 705 | b->mtu = dev->mtu - sizeof(struct iphdr) |
| 706 | - sizeof(struct udphdr); |
| 707 | #if IS_ENABLED(CONFIG_IPV6) |
Erik Hugne | 4fee6be8 | 2015-03-09 10:19:31 +0100 | [diff] [blame] | 708 | } else if (local.proto == htons(ETH_P_IPV6)) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 709 | udp_conf.family = AF_INET6; |
| 710 | udp_conf.use_udp6_tx_checksums = true; |
| 711 | udp_conf.use_udp6_rx_checksums = true; |
| 712 | udp_conf.local_ip6 = in6addr_any; |
| 713 | b->mtu = 1280; |
| 714 | #endif |
| 715 | } else { |
| 716 | err = -EAFNOSUPPORT; |
| 717 | goto err; |
| 718 | } |
Richard Alpe | bc3a334 | 2016-06-27 13:34:07 +0200 | [diff] [blame] | 719 | udp_conf.local_udp_port = local.port; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 720 | err = udp_sock_create(net, &udp_conf, &ub->ubsock); |
| 721 | if (err) |
| 722 | goto err; |
| 723 | tuncfg.sk_user_data = ub; |
| 724 | tuncfg.encap_type = 1; |
| 725 | tuncfg.encap_rcv = tipc_udp_recv; |
| 726 | tuncfg.encap_destroy = NULL; |
| 727 | setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg); |
| 728 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 729 | /** |
| 730 | * The bcast media address port is used for all peers and the ip |
| 731 | * is used if it's a multicast address. |
| 732 | */ |
| 733 | memcpy(&b->bcast_addr.value, &remote, sizeof(remote)); |
| 734 | if (tipc_udp_is_mcast_addr(&remote)) |
| 735 | err = enable_mcast(ub, &remote); |
| 736 | else |
| 737 | err = tipc_udp_rcast_add(b, &remote); |
| 738 | if (err) |
| 739 | goto err; |
Richard Alpe | 1ca73e3 | 2016-08-26 10:52:52 +0200 | [diff] [blame] | 740 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 741 | return 0; |
| 742 | err: |
Wei Yongjun | a5de125 | 2016-08-24 13:32:19 +0000 | [diff] [blame] | 743 | if (ub->ubsock) |
| 744 | udp_tunnel_sock_release(ub->ubsock); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 745 | kfree(ub); |
| 746 | return err; |
| 747 | } |
| 748 | |
| 749 | /* cleanup_bearer - break the socket/bearer association */ |
| 750 | static void cleanup_bearer(struct work_struct *work) |
| 751 | { |
| 752 | struct udp_bearer *ub = container_of(work, struct udp_bearer, work); |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 753 | struct udp_replicast *rcast, *tmp; |
| 754 | |
| 755 | list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) { |
| 756 | list_del_rcu(&rcast->list); |
| 757 | kfree_rcu(rcast, rcu); |
| 758 | } |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 759 | |
| 760 | if (ub->ubsock) |
| 761 | udp_tunnel_sock_release(ub->ubsock); |
| 762 | synchronize_net(); |
| 763 | kfree(ub); |
| 764 | } |
| 765 | |
| 766 | /* tipc_udp_disable - detach bearer from socket */ |
| 767 | static void tipc_udp_disable(struct tipc_bearer *b) |
| 768 | { |
| 769 | struct udp_bearer *ub; |
| 770 | |
| 771 | ub = rcu_dereference_rtnl(b->media_ptr); |
| 772 | if (!ub) { |
| 773 | pr_err("UDP bearer instance not found\n"); |
| 774 | return; |
| 775 | } |
| 776 | if (ub->ubsock) |
| 777 | sock_set_flag(ub->ubsock->sk, SOCK_DEAD); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 778 | RCU_INIT_POINTER(ub->bearer, NULL); |
| 779 | |
| 780 | /* sock_release need to be done outside of rtnl lock */ |
| 781 | INIT_WORK(&ub->work, cleanup_bearer); |
| 782 | schedule_work(&ub->work); |
| 783 | } |
| 784 | |
| 785 | struct tipc_media udp_media_info = { |
| 786 | .send_msg = tipc_udp_send_msg, |
| 787 | .enable_media = tipc_udp_enable, |
| 788 | .disable_media = tipc_udp_disable, |
| 789 | .addr2str = tipc_udp_addr2str, |
| 790 | .addr2msg = tipc_udp_addr2msg, |
| 791 | .msg2addr = tipc_udp_msg2addr, |
| 792 | .priority = TIPC_DEF_LINK_PRI, |
| 793 | .tolerance = TIPC_DEF_LINK_TOL, |
| 794 | .window = TIPC_DEF_LINK_WIN, |
| 795 | .type_id = TIPC_MEDIA_TYPE_UDP, |
| 796 | .hwaddr_len = 0, |
| 797 | .name = "udp" |
| 798 | }; |