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" |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 52 | |
| 53 | /* IANA assigned UDP port */ |
| 54 | #define UDP_PORT_DEFAULT 6118 |
| 55 | |
Richard Alpe | 9bd160b | 2016-03-14 09:43:52 +0100 | [diff] [blame] | 56 | #define UDP_MIN_HEADROOM 48 |
Jon Paul Maloy | e535679 | 2015-10-19 11:43:11 -0400 | [diff] [blame] | 57 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 58 | /** |
| 59 | * struct udp_media_addr - IP/UDP addressing information |
| 60 | * |
| 61 | * This is the bearer level originating address used in neighbor discovery |
| 62 | * messages, and all fields should be in network byte order |
| 63 | */ |
| 64 | struct udp_media_addr { |
| 65 | __be16 proto; |
Richard Alpe | bc3a334 | 2016-06-27 13:34:07 +0200 | [diff] [blame] | 66 | __be16 port; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 67 | union { |
| 68 | struct in_addr ipv4; |
| 69 | struct in6_addr ipv6; |
| 70 | }; |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * struct udp_bearer - ip/udp bearer data structure |
| 75 | * @bearer: associated generic tipc bearer |
| 76 | * @ubsock: bearer associated socket |
| 77 | * @ifindex: local address scope |
| 78 | * @work: used to schedule deferred work on a bearer |
| 79 | */ |
| 80 | struct udp_bearer { |
| 81 | struct tipc_bearer __rcu *bearer; |
| 82 | struct socket *ubsock; |
| 83 | u32 ifindex; |
| 84 | struct work_struct work; |
| 85 | }; |
| 86 | |
Richard Alpe | 1ca73e3 | 2016-08-26 10:52:52 +0200 | [diff] [blame^] | 87 | static int tipc_udp_is_mcast_addr(struct udp_media_addr *addr) |
| 88 | { |
| 89 | if (ntohs(addr->proto) == ETH_P_IP) |
| 90 | return ipv4_is_multicast(addr->ipv4.s_addr); |
| 91 | #if IS_ENABLED(CONFIG_IPV6) |
| 92 | else |
| 93 | return ipv6_addr_is_multicast(&addr->ipv6); |
| 94 | #endif |
| 95 | return 0; |
| 96 | } |
| 97 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 98 | /* udp_media_addr_set - convert a ip/udp address to a TIPC media address */ |
| 99 | static void tipc_udp_media_addr_set(struct tipc_media_addr *addr, |
| 100 | struct udp_media_addr *ua) |
| 101 | { |
| 102 | memset(addr, 0, sizeof(struct tipc_media_addr)); |
| 103 | addr->media_id = TIPC_MEDIA_TYPE_UDP; |
| 104 | memcpy(addr->value, ua, sizeof(struct udp_media_addr)); |
Richard Alpe | 1ca73e3 | 2016-08-26 10:52:52 +0200 | [diff] [blame^] | 105 | |
| 106 | if (tipc_udp_is_mcast_addr(ua)) |
| 107 | addr->broadcast = 1; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* tipc_udp_addr2str - convert ip/udp address to string */ |
| 111 | static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size) |
| 112 | { |
| 113 | struct udp_media_addr *ua = (struct udp_media_addr *)&a->value; |
| 114 | |
| 115 | if (ntohs(ua->proto) == ETH_P_IP) |
Richard Alpe | bc3a334 | 2016-06-27 13:34:07 +0200 | [diff] [blame] | 116 | snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port)); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 117 | else if (ntohs(ua->proto) == ETH_P_IPV6) |
Richard Alpe | bc3a334 | 2016-06-27 13:34:07 +0200 | [diff] [blame] | 118 | snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port)); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 119 | else |
| 120 | pr_err("Invalid UDP media address\n"); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */ |
| 125 | static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a, |
| 126 | char *msg) |
| 127 | { |
| 128 | struct udp_media_addr *ua; |
| 129 | |
| 130 | ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET); |
| 131 | if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP) |
| 132 | return -EINVAL; |
| 133 | tipc_udp_media_addr_set(a, ua); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */ |
| 138 | static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a) |
| 139 | { |
| 140 | memset(msg, 0, TIPC_MEDIA_INFO_SIZE); |
| 141 | msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP; |
| 142 | memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value, |
| 143 | sizeof(struct udp_media_addr)); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* tipc_send_msg - enqueue a send request */ |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 148 | static int tipc_udp_xmit(struct net *net, struct sk_buff *skb, |
| 149 | struct udp_bearer *ub, struct udp_media_addr *src, |
| 150 | struct udp_media_addr *dst) |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 151 | { |
| 152 | int ttl, err = 0; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 153 | struct rtable *rt; |
| 154 | |
Erik Hugne | 4fee6be8 | 2015-03-09 10:19:31 +0100 | [diff] [blame] | 155 | if (dst->proto == htons(ETH_P_IP)) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 156 | struct flowi4 fl = { |
| 157 | .daddr = dst->ipv4.s_addr, |
| 158 | .saddr = src->ipv4.s_addr, |
Jon Paul Maloy | 7214bcf | 2015-10-22 08:51:45 -0400 | [diff] [blame] | 159 | .flowi4_mark = skb->mark, |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 160 | .flowi4_proto = IPPROTO_UDP |
| 161 | }; |
| 162 | rt = ip_route_output_key(net, &fl); |
| 163 | if (IS_ERR(rt)) { |
| 164 | err = PTR_ERR(rt); |
| 165 | goto tx_error; |
| 166 | } |
Richard Alpe | 9b30096 | 2016-03-03 14:20:40 +0100 | [diff] [blame] | 167 | |
| 168 | skb->dev = rt->dst.dev; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 169 | ttl = ip4_dst_hoplimit(&rt->dst); |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 170 | 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] | 171 | dst->ipv4.s_addr, 0, ttl, 0, src->port, |
| 172 | dst->port, false, true); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 173 | #if IS_ENABLED(CONFIG_IPV6) |
| 174 | } else { |
| 175 | struct dst_entry *ndst; |
| 176 | struct flowi6 fl6 = { |
| 177 | .flowi6_oif = ub->ifindex, |
| 178 | .daddr = dst->ipv6, |
| 179 | .saddr = src->ipv6, |
| 180 | .flowi6_proto = IPPROTO_UDP |
| 181 | }; |
Roopa Prabhu | 343d60a | 2015-07-30 13:34:53 -0700 | [diff] [blame] | 182 | err = ipv6_stub->ipv6_dst_lookup(net, ub->ubsock->sk, &ndst, |
| 183 | &fl6); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 184 | if (err) |
| 185 | goto tx_error; |
| 186 | ttl = ip6_dst_hoplimit(ndst); |
Jon Paul Maloy | 7214bcf | 2015-10-22 08:51:45 -0400 | [diff] [blame] | 187 | err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb, |
David Miller | 79b16aa | 2015-04-05 22:19:09 -0400 | [diff] [blame] | 188 | ndst->dev, &src->ipv6, |
Richard Alpe | bc3a334 | 2016-06-27 13:34:07 +0200 | [diff] [blame] | 189 | &dst->ipv6, 0, ttl, 0, src->port, |
| 190 | dst->port, false); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 191 | #endif |
| 192 | } |
| 193 | return err; |
| 194 | |
| 195 | tx_error: |
Jon Paul Maloy | 7214bcf | 2015-10-22 08:51:45 -0400 | [diff] [blame] | 196 | kfree_skb(skb); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 197 | return err; |
| 198 | } |
| 199 | |
Richard Alpe | ce984da | 2016-08-26 10:52:51 +0200 | [diff] [blame] | 200 | static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb, |
| 201 | struct tipc_bearer *b, |
| 202 | struct tipc_media_addr *addr) |
| 203 | { |
| 204 | struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value; |
| 205 | struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value; |
| 206 | struct udp_bearer *ub; |
| 207 | int err = 0; |
| 208 | |
| 209 | if (skb_headroom(skb) < UDP_MIN_HEADROOM) { |
| 210 | err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC); |
| 211 | if (err) |
| 212 | goto tx_error; |
| 213 | } |
| 214 | |
| 215 | skb_set_inner_protocol(skb, htons(ETH_P_TIPC)); |
| 216 | ub = rcu_dereference_rtnl(b->media_ptr); |
| 217 | if (!ub) { |
| 218 | err = -ENODEV; |
| 219 | goto tx_error; |
| 220 | } |
| 221 | |
| 222 | return tipc_udp_xmit(net, skb, ub, src, dst); |
| 223 | |
| 224 | tx_error: |
| 225 | kfree_skb(skb); |
| 226 | return err; |
| 227 | } |
| 228 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 229 | /* tipc_udp_recv - read data from bearer socket */ |
| 230 | static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb) |
| 231 | { |
| 232 | struct udp_bearer *ub; |
| 233 | struct tipc_bearer *b; |
| 234 | |
| 235 | ub = rcu_dereference_sk_user_data(sk); |
| 236 | if (!ub) { |
| 237 | pr_err_ratelimited("Failed to get UDP bearer reference"); |
| 238 | kfree_skb(skb); |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | skb_pull(skb, sizeof(struct udphdr)); |
| 243 | rcu_read_lock(); |
| 244 | b = rcu_dereference_rtnl(ub->bearer); |
| 245 | |
Jon Paul Maloy | 0d051bf | 2016-08-16 11:53:50 -0400 | [diff] [blame] | 246 | if (b && test_bit(0, &b->up)) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 247 | tipc_rcv(sock_net(sk), skb, b); |
| 248 | rcu_read_unlock(); |
| 249 | return 0; |
| 250 | } |
| 251 | rcu_read_unlock(); |
| 252 | kfree_skb(skb); |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote) |
| 257 | { |
| 258 | int err = 0; |
| 259 | struct ip_mreqn mreqn; |
| 260 | struct sock *sk = ub->ubsock->sk; |
| 261 | |
| 262 | if (ntohs(remote->proto) == ETH_P_IP) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 263 | mreqn.imr_multiaddr = remote->ipv4; |
| 264 | mreqn.imr_ifindex = ub->ifindex; |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 265 | err = ip_mc_join_group(sk, &mreqn); |
Marcelo Ricardo Leitner | 446981e | 2015-03-19 16:47:58 -0300 | [diff] [blame] | 266 | #if IS_ENABLED(CONFIG_IPV6) |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 267 | } else { |
Marcelo Ricardo Leitner | 446981e | 2015-03-19 16:47:58 -0300 | [diff] [blame] | 268 | err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex, |
| 269 | &remote->ipv6); |
| 270 | #endif |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 271 | } |
| 272 | return err; |
| 273 | } |
| 274 | |
| 275 | /** |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 276 | * tipc_parse_udp_addr - build udp media address from netlink data |
| 277 | * @nlattr: netlink attribute containing sockaddr storage aligned address |
| 278 | * @addr: tipc media address to fill with address, port and protocol type |
| 279 | * @scope_id: IPv6 scope id pointer, not NULL indicates it's required |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 280 | */ |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 281 | |
| 282 | static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr, |
| 283 | u32 *scope_id) |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 284 | { |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 285 | struct sockaddr_storage sa; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 286 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 287 | nla_memcpy(&sa, nla, sizeof(sa)); |
| 288 | if (sa.ss_family == AF_INET) { |
| 289 | struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 290 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 291 | addr->proto = htons(ETH_P_IP); |
| 292 | addr->port = ip4->sin_port; |
| 293 | addr->ipv4.s_addr = ip4->sin_addr.s_addr; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 294 | return 0; |
| 295 | |
| 296 | #if IS_ENABLED(CONFIG_IPV6) |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 297 | } else if (sa.ss_family == AF_INET6) { |
| 298 | struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 299 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 300 | addr->proto = htons(ETH_P_IPV6); |
| 301 | addr->port = ip6->sin6_port; |
| 302 | memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr)); |
Richard Alpe | 34f65db | 2016-03-03 14:20:43 +0100 | [diff] [blame] | 303 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 304 | /* Scope ID is only interesting for local addresses */ |
| 305 | if (scope_id) { |
| 306 | int atype; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 307 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 308 | atype = ipv6_addr_type(&ip6->sin6_addr); |
| 309 | if (__ipv6_addr_needs_scope_id(atype) && |
| 310 | !ip6->sin6_scope_id) { |
| 311 | return -EINVAL; |
| 312 | } |
| 313 | |
| 314 | *scope_id = ip6->sin6_scope_id ? : 0; |
| 315 | } |
| 316 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 317 | return 0; |
| 318 | #endif |
| 319 | } |
| 320 | return -EADDRNOTAVAIL; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * tipc_udp_enable - callback to create a new udp bearer instance |
| 325 | * @net: network namespace |
| 326 | * @b: pointer to generic tipc_bearer |
| 327 | * @attrs: netlink bearer configuration |
| 328 | * |
| 329 | * validate the bearer parameters and initialize the udp bearer |
| 330 | * rtnl_lock should be held |
| 331 | */ |
| 332 | static int tipc_udp_enable(struct net *net, struct tipc_bearer *b, |
| 333 | struct nlattr *attrs[]) |
| 334 | { |
| 335 | int err = -EINVAL; |
| 336 | struct udp_bearer *ub; |
| 337 | struct udp_media_addr *remote; |
| 338 | struct udp_media_addr local = {0}; |
| 339 | struct udp_port_cfg udp_conf = {0}; |
Erik Hugne | 4fee6be8 | 2015-03-09 10:19:31 +0100 | [diff] [blame] | 340 | struct udp_tunnel_sock_cfg tuncfg = {NULL}; |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 341 | struct nlattr *opts[TIPC_NLA_UDP_MAX + 1]; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 342 | |
| 343 | ub = kzalloc(sizeof(*ub), GFP_ATOMIC); |
| 344 | if (!ub) |
| 345 | return -ENOMEM; |
| 346 | |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 347 | if (!attrs[TIPC_NLA_BEARER_UDP_OPTS]) |
| 348 | goto err; |
| 349 | |
| 350 | if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX, |
| 351 | attrs[TIPC_NLA_BEARER_UDP_OPTS], |
| 352 | tipc_nl_udp_policy)) |
| 353 | goto err; |
| 354 | |
| 355 | if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) { |
| 356 | pr_err("Invalid UDP bearer configuration"); |
| 357 | return -EINVAL; |
| 358 | } |
| 359 | |
| 360 | err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local, |
| 361 | &ub->ifindex); |
| 362 | if (err) |
| 363 | goto err; |
| 364 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 365 | remote = (struct udp_media_addr *)&b->bcast_addr.value; |
| 366 | memset(remote, 0, sizeof(struct udp_media_addr)); |
Richard Alpe | ba5aa84 | 2016-08-26 10:52:50 +0200 | [diff] [blame] | 367 | err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], remote, NULL); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 368 | if (err) |
| 369 | goto err; |
| 370 | |
| 371 | b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP; |
| 372 | b->bcast_addr.broadcast = 1; |
| 373 | rcu_assign_pointer(b->media_ptr, ub); |
| 374 | rcu_assign_pointer(ub->bearer, b); |
| 375 | tipc_udp_media_addr_set(&b->addr, &local); |
Erik Hugne | 4fee6be8 | 2015-03-09 10:19:31 +0100 | [diff] [blame] | 376 | if (local.proto == htons(ETH_P_IP)) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 377 | struct net_device *dev; |
| 378 | |
| 379 | dev = __ip_dev_find(net, local.ipv4.s_addr, false); |
| 380 | if (!dev) { |
| 381 | err = -ENODEV; |
| 382 | goto err; |
| 383 | } |
| 384 | udp_conf.family = AF_INET; |
| 385 | udp_conf.local_ip.s_addr = htonl(INADDR_ANY); |
| 386 | udp_conf.use_udp_checksums = false; |
| 387 | ub->ifindex = dev->ifindex; |
| 388 | b->mtu = dev->mtu - sizeof(struct iphdr) |
| 389 | - sizeof(struct udphdr); |
| 390 | #if IS_ENABLED(CONFIG_IPV6) |
Erik Hugne | 4fee6be8 | 2015-03-09 10:19:31 +0100 | [diff] [blame] | 391 | } else if (local.proto == htons(ETH_P_IPV6)) { |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 392 | udp_conf.family = AF_INET6; |
| 393 | udp_conf.use_udp6_tx_checksums = true; |
| 394 | udp_conf.use_udp6_rx_checksums = true; |
| 395 | udp_conf.local_ip6 = in6addr_any; |
| 396 | b->mtu = 1280; |
| 397 | #endif |
| 398 | } else { |
| 399 | err = -EAFNOSUPPORT; |
| 400 | goto err; |
| 401 | } |
Richard Alpe | bc3a334 | 2016-06-27 13:34:07 +0200 | [diff] [blame] | 402 | udp_conf.local_udp_port = local.port; |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 403 | err = udp_sock_create(net, &udp_conf, &ub->ubsock); |
| 404 | if (err) |
| 405 | goto err; |
| 406 | tuncfg.sk_user_data = ub; |
| 407 | tuncfg.encap_type = 1; |
| 408 | tuncfg.encap_rcv = tipc_udp_recv; |
| 409 | tuncfg.encap_destroy = NULL; |
| 410 | setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg); |
| 411 | |
Richard Alpe | 1ca73e3 | 2016-08-26 10:52:52 +0200 | [diff] [blame^] | 412 | if (tipc_udp_is_mcast_addr(remote)) { |
| 413 | if (enable_mcast(ub, remote)) |
| 414 | goto err; |
| 415 | } |
| 416 | |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 417 | return 0; |
| 418 | err: |
| 419 | kfree(ub); |
| 420 | return err; |
| 421 | } |
| 422 | |
| 423 | /* cleanup_bearer - break the socket/bearer association */ |
| 424 | static void cleanup_bearer(struct work_struct *work) |
| 425 | { |
| 426 | struct udp_bearer *ub = container_of(work, struct udp_bearer, work); |
| 427 | |
| 428 | if (ub->ubsock) |
| 429 | udp_tunnel_sock_release(ub->ubsock); |
| 430 | synchronize_net(); |
| 431 | kfree(ub); |
| 432 | } |
| 433 | |
| 434 | /* tipc_udp_disable - detach bearer from socket */ |
| 435 | static void tipc_udp_disable(struct tipc_bearer *b) |
| 436 | { |
| 437 | struct udp_bearer *ub; |
| 438 | |
| 439 | ub = rcu_dereference_rtnl(b->media_ptr); |
| 440 | if (!ub) { |
| 441 | pr_err("UDP bearer instance not found\n"); |
| 442 | return; |
| 443 | } |
| 444 | if (ub->ubsock) |
| 445 | sock_set_flag(ub->ubsock->sk, SOCK_DEAD); |
Erik Hugne | d0f9193 | 2015-03-05 10:23:49 +0100 | [diff] [blame] | 446 | RCU_INIT_POINTER(ub->bearer, NULL); |
| 447 | |
| 448 | /* sock_release need to be done outside of rtnl lock */ |
| 449 | INIT_WORK(&ub->work, cleanup_bearer); |
| 450 | schedule_work(&ub->work); |
| 451 | } |
| 452 | |
| 453 | struct tipc_media udp_media_info = { |
| 454 | .send_msg = tipc_udp_send_msg, |
| 455 | .enable_media = tipc_udp_enable, |
| 456 | .disable_media = tipc_udp_disable, |
| 457 | .addr2str = tipc_udp_addr2str, |
| 458 | .addr2msg = tipc_udp_addr2msg, |
| 459 | .msg2addr = tipc_udp_msg2addr, |
| 460 | .priority = TIPC_DEF_LINK_PRI, |
| 461 | .tolerance = TIPC_DEF_LINK_TOL, |
| 462 | .window = TIPC_DEF_LINK_WIN, |
| 463 | .type_id = TIPC_MEDIA_TYPE_UDP, |
| 464 | .hwaddr_len = 0, |
| 465 | .name = "udp" |
| 466 | }; |