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