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 | |
| 177 | skb->dev = rt->dst.dev; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 178 | ttl = ip4_dst_hoplimit(&rt->dst); |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 179 | 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] | 180 | dst->ipv4.s_addr, 0, ttl, 0, src->port, |
| 181 | dst->port, false, true); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 182 | #if IS_ENABLED(CONFIG_IPV6) |
| 183 | } else { |
| 184 | struct dst_entry *ndst; |
| 185 | struct flowi6 fl6 = { |
| 186 | .flowi6_oif = ub->ifindex, |
| 187 | .daddr = dst->ipv6, |
| 188 | .saddr = src->ipv6, |
| 189 | .flowi6_proto = IPPROTO_UDP |
| 190 | }; |
Roopa Prabhu | 343d60a | 2015-07-30 13:34:53 -0700 | [diff] [blame] | 191 | err = ipv6_stub->ipv6_dst_lookup(net, ub->ubsock->sk, &ndst, |
| 192 | &fl6); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 193 | if (err) |
| 194 | goto tx_error; |
| 195 | ttl = ip6_dst_hoplimit(ndst); |
Jon Paul Maloy | 7214bcf | 2015-10-22 08:51:45 -0400 | [diff] [blame] | 196 | err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb, |
David Miller | 79b16aa | 2015-04-05 22:19:09 -0400 | [diff] [blame] | 197 | ndst->dev, &src->ipv6, |
Richard Alpe | bc3a334 | 2016-06-27 13:34:07 +0200 | [diff] [blame] | 198 | &dst->ipv6, 0, ttl, 0, src->port, |
| 199 | dst->port, false); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 200 | #endif |
| 201 | } |
| 202 | return err; |
| 203 | |
| 204 | tx_error: |
Jon Paul Maloy | 7214bcf | 2015-10-22 08:51:45 -0400 | [diff] [blame] | 205 | kfree_skb(skb); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 206 | return err; |
| 207 | } |
| 208 | |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 209 | static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb, |
| 210 | struct tipc_bearer *b, |
| 211 | struct tipc_media_addr *addr) |
| 212 | { |
| 213 | struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value; |
| 214 | struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value; |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 215 | struct udp_replicast *rcast; |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 216 | struct udp_bearer *ub; |
| 217 | int err = 0; |
| 218 | |
| 219 | if (skb_headroom(skb) < UDP_MIN_HEADROOM) { |
| 220 | err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC); |
| 221 | if (err) |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 222 | goto out; |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | skb_set_inner_protocol(skb, htons(ETH_P_TIPC)); |
| 226 | ub = rcu_dereference_rtnl(b->media_ptr); |
| 227 | if (!ub) { |
| 228 | err = -ENODEV; |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 229 | goto out; |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 232 | if (!addr->broadcast || list_empty(&ub->rcast.list)) |
| 233 | return tipc_udp_xmit(net, skb, ub, src, dst); |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 234 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 235 | /* Replicast, send an skb to each configured IP address */ |
| 236 | list_for_each_entry_rcu(rcast, &ub->rcast.list, list) { |
| 237 | struct sk_buff *_skb; |
| 238 | |
| 239 | _skb = pskb_copy(skb, GFP_ATOMIC); |
| 240 | if (!_skb) { |
| 241 | err = -ENOMEM; |
| 242 | goto out; |
| 243 | } |
| 244 | |
| 245 | err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr); |
| 246 | if (err) { |
| 247 | kfree_skb(_skb); |
| 248 | goto out; |
| 249 | } |
| 250 | } |
| 251 | err = 0; |
| 252 | out: |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 253 | kfree_skb(skb); |
| 254 | return err; |
| 255 | } |
| 256 | |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame^] | 257 | static bool tipc_udp_is_known_peer(struct tipc_bearer *b, |
| 258 | struct udp_media_addr *addr) |
| 259 | { |
| 260 | struct udp_replicast *rcast, *tmp; |
| 261 | struct udp_bearer *ub; |
| 262 | |
| 263 | ub = rcu_dereference_rtnl(b->media_ptr); |
| 264 | if (!ub) { |
| 265 | pr_err_ratelimited("UDP bearer instance not found\n"); |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) { |
| 270 | if (!memcmp(&rcast->addr, addr, sizeof(struct udp_media_addr))) |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | return false; |
| 275 | } |
| 276 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 277 | static int tipc_udp_rcast_add(struct tipc_bearer *b, |
| 278 | struct udp_media_addr *addr) |
| 279 | { |
| 280 | struct udp_replicast *rcast; |
| 281 | struct udp_bearer *ub; |
| 282 | |
| 283 | ub = rcu_dereference_rtnl(b->media_ptr); |
| 284 | if (!ub) |
| 285 | return -ENODEV; |
| 286 | |
| 287 | rcast = kmalloc(sizeof(*rcast), GFP_ATOMIC); |
| 288 | if (!rcast) |
| 289 | return -ENOMEM; |
| 290 | |
| 291 | memcpy(&rcast->addr, addr, sizeof(struct udp_media_addr)); |
| 292 | |
| 293 | if (ntohs(addr->proto) == ETH_P_IP) |
| 294 | pr_info("New replicast peer: %pI4\n", &rcast->addr.ipv4); |
| 295 | #if IS_ENABLED(CONFIG_IPV6) |
| 296 | else if (ntohs(addr->proto) == ETH_P_IPV6) |
| 297 | pr_info("New replicast peer: %pI6\n", &rcast->addr.ipv6); |
| 298 | #endif |
| 299 | |
| 300 | list_add_rcu(&rcast->list, &ub->rcast.list); |
| 301 | return 0; |
| 302 | } |
| 303 | |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame^] | 304 | static int tipc_udp_rcast_disc(struct tipc_bearer *b, struct sk_buff *skb) |
| 305 | { |
| 306 | struct udp_media_addr src = {0}; |
| 307 | struct udp_media_addr *dst; |
| 308 | |
| 309 | dst = (struct udp_media_addr *)&b->bcast_addr.value; |
| 310 | if (tipc_udp_is_mcast_addr(dst)) |
| 311 | return 0; |
| 312 | |
| 313 | src.port = udp_hdr(skb)->source; |
| 314 | |
| 315 | if (ip_hdr(skb)->version == 4) { |
| 316 | struct iphdr *iphdr = ip_hdr(skb); |
| 317 | |
| 318 | src.proto = htons(ETH_P_IP); |
| 319 | src.ipv4.s_addr = iphdr->saddr; |
| 320 | if (ipv4_is_multicast(iphdr->daddr)) |
| 321 | return 0; |
| 322 | #if IS_ENABLED(CONFIG_IPV6) |
| 323 | } else if (ip_hdr(skb)->version == 6) { |
| 324 | struct ipv6hdr *iphdr = ipv6_hdr(skb); |
| 325 | |
| 326 | src.proto = htons(ETH_P_IPV6); |
| 327 | src.ipv6 = iphdr->saddr; |
| 328 | if (ipv6_addr_is_multicast(&iphdr->daddr)) |
| 329 | return 0; |
| 330 | #endif |
| 331 | } else { |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | if (likely(tipc_udp_is_known_peer(b, &src))) |
| 336 | return 0; |
| 337 | |
| 338 | return tipc_udp_rcast_add(b, &src); |
| 339 | } |
| 340 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 341 | /* tipc_udp_recv - read data from bearer socket */ |
| 342 | static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb) |
| 343 | { |
| 344 | struct udp_bearer *ub; |
| 345 | struct tipc_bearer *b; |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame^] | 346 | struct tipc_msg *hdr; |
| 347 | int err; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 348 | |
| 349 | ub = rcu_dereference_sk_user_data(sk); |
| 350 | if (!ub) { |
| 351 | pr_err_ratelimited("Failed to get UDP bearer reference"); |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame^] | 352 | goto out; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 353 | } |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 354 | skb_pull(skb, sizeof(struct udphdr)); |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame^] | 355 | hdr = buf_msg(skb); |
| 356 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 357 | rcu_read_lock(); |
| 358 | b = rcu_dereference_rtnl(ub->bearer); |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame^] | 359 | if (!b) |
| 360 | goto rcu_out; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 361 | |
Jon Paul Maloy | 0d051bf | 2016-08-16 11:53:50 -0400 | [diff] [blame] | 362 | if (b && test_bit(0, &b->up)) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 363 | tipc_rcv(sock_net(sk), skb, b); |
| 364 | rcu_read_unlock(); |
| 365 | return 0; |
| 366 | } |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame^] | 367 | |
| 368 | if (unlikely(msg_user(hdr) == LINK_CONFIG)) { |
| 369 | err = tipc_udp_rcast_disc(b, skb); |
| 370 | if (err) |
| 371 | goto rcu_out; |
| 372 | } |
| 373 | |
| 374 | tipc_rcv(sock_net(sk), skb, b); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 375 | rcu_read_unlock(); |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame^] | 376 | return 0; |
| 377 | |
| 378 | rcu_out: |
| 379 | rcu_read_unlock(); |
| 380 | out: |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 381 | kfree_skb(skb); |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote) |
| 386 | { |
| 387 | int err = 0; |
| 388 | struct ip_mreqn mreqn; |
| 389 | struct sock *sk = ub->ubsock->sk; |
| 390 | |
| 391 | if (ntohs(remote->proto) == ETH_P_IP) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 392 | mreqn.imr_multiaddr = remote->ipv4; |
| 393 | mreqn.imr_ifindex = ub->ifindex; |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 394 | err = ip_mc_join_group(sk, &mreqn); |
Marcelo Ricardo Leitner | 446981e | 2015-03-19 16:47:58 -0300 | [diff] [blame] | 395 | #if IS_ENABLED(CONFIG_IPV6) |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 396 | } else { |
Marcelo Ricardo Leitner | 446981e | 2015-03-19 16:47:58 -0300 | [diff] [blame] | 397 | err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex, |
| 398 | &remote->ipv6); |
| 399 | #endif |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 400 | } |
| 401 | return err; |
| 402 | } |
| 403 | |
| 404 | /** |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 405 | * tipc_parse_udp_addr - build udp media address from netlink data |
| 406 | * @nlattr: netlink attribute containing sockaddr storage aligned address |
| 407 | * @addr: tipc media address to fill with address, port and protocol type |
| 408 | * @scope_id: IPv6 scope id pointer, not NULL indicates it's required |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 409 | */ |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 410 | |
| 411 | static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr, |
| 412 | u32 *scope_id) |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 413 | { |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 414 | struct sockaddr_storage sa; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 415 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 416 | nla_memcpy(&sa, nla, sizeof(sa)); |
| 417 | if (sa.ss_family == AF_INET) { |
| 418 | struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 419 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 420 | addr->proto = htons(ETH_P_IP); |
| 421 | addr->port = ip4->sin_port; |
| 422 | addr->ipv4.s_addr = ip4->sin_addr.s_addr; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 423 | return 0; |
| 424 | |
| 425 | #if IS_ENABLED(CONFIG_IPV6) |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 426 | } else if (sa.ss_family == AF_INET6) { |
| 427 | struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 428 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 429 | addr->proto = htons(ETH_P_IPV6); |
| 430 | addr->port = ip6->sin6_port; |
| 431 | memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr)); |
Richard Alpe | 34f65db | 2016-03-03 14:20:43 +0100 | [diff] [blame] | 432 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 433 | /* Scope ID is only interesting for local addresses */ |
| 434 | if (scope_id) { |
| 435 | int atype; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 436 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 437 | atype = ipv6_addr_type(&ip6->sin6_addr); |
| 438 | if (__ipv6_addr_needs_scope_id(atype) && |
| 439 | !ip6->sin6_scope_id) { |
| 440 | return -EINVAL; |
| 441 | } |
| 442 | |
| 443 | *scope_id = ip6->sin6_scope_id ? : 0; |
| 444 | } |
| 445 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 446 | return 0; |
| 447 | #endif |
| 448 | } |
| 449 | return -EADDRNOTAVAIL; |
| 450 | } |
| 451 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 452 | int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr) |
| 453 | { |
| 454 | int err; |
| 455 | struct udp_media_addr addr = {0}; |
| 456 | struct nlattr *opts[TIPC_NLA_UDP_MAX + 1]; |
| 457 | struct udp_media_addr *dst; |
| 458 | |
| 459 | if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX, attr, tipc_nl_udp_policy)) |
| 460 | return -EINVAL; |
| 461 | |
| 462 | if (!opts[TIPC_NLA_UDP_REMOTE]) |
| 463 | return -EINVAL; |
| 464 | |
| 465 | err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL); |
| 466 | if (err) |
| 467 | return err; |
| 468 | |
| 469 | dst = (struct udp_media_addr *)&b->bcast_addr.value; |
| 470 | if (tipc_udp_is_mcast_addr(dst)) { |
| 471 | pr_err("Can't add remote ip to TIPC UDP multicast bearer\n"); |
| 472 | return -EINVAL; |
| 473 | } |
| 474 | |
Richard Alpe | c9b64d4 | 2016-08-26 10:52:54 +0200 | [diff] [blame^] | 475 | if (tipc_udp_is_known_peer(b, &addr)) |
| 476 | return 0; |
| 477 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 478 | return tipc_udp_rcast_add(b, &addr); |
| 479 | } |
| 480 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 481 | /** |
| 482 | * tipc_udp_enable - callback to create a new udp bearer instance |
| 483 | * @net: network namespace |
| 484 | * @b: pointer to generic tipc_bearer |
| 485 | * @attrs: netlink bearer configuration |
| 486 | * |
| 487 | * validate the bearer parameters and initialize the udp bearer |
| 488 | * rtnl_lock should be held |
| 489 | */ |
| 490 | static int tipc_udp_enable(struct net *net, struct tipc_bearer *b, |
| 491 | struct nlattr *attrs[]) |
| 492 | { |
| 493 | int err = -EINVAL; |
| 494 | struct udp_bearer *ub; |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 495 | struct udp_media_addr remote = {0}; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 496 | struct udp_media_addr local = {0}; |
| 497 | struct udp_port_cfg udp_conf = {0}; |
Erik Hugne | 4fee6be8 | 2015-03-09 10:19:31 +0100 | [diff] [blame] | 498 | struct udp_tunnel_sock_cfg tuncfg = {NULL}; |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 499 | struct nlattr *opts[TIPC_NLA_UDP_MAX + 1]; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 500 | |
| 501 | ub = kzalloc(sizeof(*ub), GFP_ATOMIC); |
| 502 | if (!ub) |
| 503 | return -ENOMEM; |
| 504 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 505 | INIT_LIST_HEAD(&ub->rcast.list); |
| 506 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 507 | if (!attrs[TIPC_NLA_BEARER_UDP_OPTS]) |
| 508 | goto err; |
| 509 | |
| 510 | if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX, |
| 511 | attrs[TIPC_NLA_BEARER_UDP_OPTS], |
| 512 | tipc_nl_udp_policy)) |
| 513 | goto err; |
| 514 | |
| 515 | if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) { |
| 516 | pr_err("Invalid UDP bearer configuration"); |
| 517 | return -EINVAL; |
| 518 | } |
| 519 | |
| 520 | err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local, |
| 521 | &ub->ifindex); |
| 522 | if (err) |
| 523 | goto err; |
| 524 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 525 | err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 526 | if (err) |
| 527 | goto err; |
| 528 | |
| 529 | b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP; |
| 530 | b->bcast_addr.broadcast = 1; |
| 531 | rcu_assign_pointer(b->media_ptr, ub); |
| 532 | rcu_assign_pointer(ub->bearer, b); |
| 533 | tipc_udp_media_addr_set(&b->addr, &local); |
Erik Hugne | 4fee6be8 | 2015-03-09 10:19:31 +0100 | [diff] [blame] | 534 | if (local.proto == htons(ETH_P_IP)) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 535 | struct net_device *dev; |
| 536 | |
| 537 | dev = __ip_dev_find(net, local.ipv4.s_addr, false); |
| 538 | if (!dev) { |
| 539 | err = -ENODEV; |
| 540 | goto err; |
| 541 | } |
| 542 | udp_conf.family = AF_INET; |
| 543 | udp_conf.local_ip.s_addr = htonl(INADDR_ANY); |
| 544 | udp_conf.use_udp_checksums = false; |
| 545 | ub->ifindex = dev->ifindex; |
| 546 | b->mtu = dev->mtu - sizeof(struct iphdr) |
| 547 | - sizeof(struct udphdr); |
| 548 | #if IS_ENABLED(CONFIG_IPV6) |
Erik Hugne | 4fee6be8 | 2015-03-09 10:19:31 +0100 | [diff] [blame] | 549 | } else if (local.proto == htons(ETH_P_IPV6)) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 550 | udp_conf.family = AF_INET6; |
| 551 | udp_conf.use_udp6_tx_checksums = true; |
| 552 | udp_conf.use_udp6_rx_checksums = true; |
| 553 | udp_conf.local_ip6 = in6addr_any; |
| 554 | b->mtu = 1280; |
| 555 | #endif |
| 556 | } else { |
| 557 | err = -EAFNOSUPPORT; |
| 558 | goto err; |
| 559 | } |
Richard Alpe | bc3a334 | 2016-06-27 13:34:07 +0200 | [diff] [blame] | 560 | udp_conf.local_udp_port = local.port; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 561 | err = udp_sock_create(net, &udp_conf, &ub->ubsock); |
| 562 | if (err) |
| 563 | goto err; |
| 564 | tuncfg.sk_user_data = ub; |
| 565 | tuncfg.encap_type = 1; |
| 566 | tuncfg.encap_rcv = tipc_udp_recv; |
| 567 | tuncfg.encap_destroy = NULL; |
| 568 | setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg); |
| 569 | |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 570 | /** |
| 571 | * The bcast media address port is used for all peers and the ip |
| 572 | * is used if it's a multicast address. |
| 573 | */ |
| 574 | memcpy(&b->bcast_addr.value, &remote, sizeof(remote)); |
| 575 | if (tipc_udp_is_mcast_addr(&remote)) |
| 576 | err = enable_mcast(ub, &remote); |
| 577 | else |
| 578 | err = tipc_udp_rcast_add(b, &remote); |
| 579 | if (err) |
| 580 | goto err; |
Richard Alpe | 1ca73e3 | 2016-08-26 10:52:52 +0200 | [diff] [blame] | 581 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 582 | return 0; |
| 583 | err: |
| 584 | kfree(ub); |
| 585 | return err; |
| 586 | } |
| 587 | |
| 588 | /* cleanup_bearer - break the socket/bearer association */ |
| 589 | static void cleanup_bearer(struct work_struct *work) |
| 590 | { |
| 591 | struct udp_bearer *ub = container_of(work, struct udp_bearer, work); |
Richard Alpe | ef20cd4 | 2016-08-26 10:52:53 +0200 | [diff] [blame] | 592 | struct udp_replicast *rcast, *tmp; |
| 593 | |
| 594 | list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) { |
| 595 | list_del_rcu(&rcast->list); |
| 596 | kfree_rcu(rcast, rcu); |
| 597 | } |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 598 | |
| 599 | if (ub->ubsock) |
| 600 | udp_tunnel_sock_release(ub->ubsock); |
| 601 | synchronize_net(); |
| 602 | kfree(ub); |
| 603 | } |
| 604 | |
| 605 | /* tipc_udp_disable - detach bearer from socket */ |
| 606 | static void tipc_udp_disable(struct tipc_bearer *b) |
| 607 | { |
| 608 | struct udp_bearer *ub; |
| 609 | |
| 610 | ub = rcu_dereference_rtnl(b->media_ptr); |
| 611 | if (!ub) { |
| 612 | pr_err("UDP bearer instance not found\n"); |
| 613 | return; |
| 614 | } |
| 615 | if (ub->ubsock) |
| 616 | sock_set_flag(ub->ubsock->sk, SOCK_DEAD); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 617 | RCU_INIT_POINTER(ub->bearer, NULL); |
| 618 | |
| 619 | /* sock_release need to be done outside of rtnl lock */ |
| 620 | INIT_WORK(&ub->work, cleanup_bearer); |
| 621 | schedule_work(&ub->work); |
| 622 | } |
| 623 | |
| 624 | struct tipc_media udp_media_info = { |
| 625 | .send_msg = tipc_udp_send_msg, |
| 626 | .enable_media = tipc_udp_enable, |
| 627 | .disable_media = tipc_udp_disable, |
| 628 | .addr2str = tipc_udp_addr2str, |
| 629 | .addr2msg = tipc_udp_addr2msg, |
| 630 | .msg2addr = tipc_udp_msg2addr, |
| 631 | .priority = TIPC_DEF_LINK_PRI, |
| 632 | .tolerance = TIPC_DEF_LINK_TOL, |
| 633 | .window = TIPC_DEF_LINK_WIN, |
| 634 | .type_id = TIPC_MEDIA_TYPE_UDP, |
| 635 | .hwaddr_len = 0, |
| 636 | .name = "udp" |
| 637 | }; |