blob: 43ca5fd6574d41ae38be588f18b75f8d2afb4b78 [file] [log] [blame]
Erik Hugned0f91932015-03-05 10:23:49 +01001/* 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 Ahern3616d082019-03-22 06:06:09 -070047#include <net/ipv6_stubs.h>
Erik Hugned0f91932015-03-05 10:23:49 +010048#include <linux/tipc_netlink.h>
49#include "core.h"
Jon Maloy52dfae52018-03-22 20:42:52 +010050#include "addr.h"
51#include "net.h"
Erik Hugned0f91932015-03-05 10:23:49 +010052#include "bearer.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010053#include "netlink.h"
Richard Alpeef20cd42016-08-26 10:52:53 +020054#include "msg.h"
Erik Hugned0f91932015-03-05 10:23:49 +010055
56/* IANA assigned UDP port */
57#define UDP_PORT_DEFAULT 6118
58
Richard Alpe9bd160b2016-03-14 09:43:52 +010059#define UDP_MIN_HEADROOM 48
Jon Paul Maloye5356792015-10-19 11:43:11 -040060
Erik Hugned0f91932015-03-05 10:23:49 +010061/**
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 */
67struct udp_media_addr {
68 __be16 proto;
Richard Alpebc3a3342016-06-27 13:34:07 +020069 __be16 port;
Erik Hugned0f91932015-03-05 10:23:49 +010070 union {
71 struct in_addr ipv4;
72 struct in6_addr ipv6;
73 };
74};
75
Richard Alpeef20cd42016-08-26 10:52:53 +020076/* struct udp_replicast - container for UDP remote addresses */
77struct udp_replicast {
78 struct udp_media_addr addr;
Xin Longe9c1a792019-06-20 19:03:41 +080079 struct dst_cache dst_cache;
Richard Alpeef20cd42016-08-26 10:52:53 +020080 struct rcu_head rcu;
81 struct list_head list;
82};
83
Erik Hugned0f91932015-03-05 10:23:49 +010084/**
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 */
91struct udp_bearer {
92 struct tipc_bearer __rcu *bearer;
93 struct socket *ubsock;
94 u32 ifindex;
95 struct work_struct work;
Richard Alpeef20cd42016-08-26 10:52:53 +020096 struct udp_replicast rcast;
Erik Hugned0f91932015-03-05 10:23:49 +010097};
98
Richard Alpe1ca73e32016-08-26 10:52:52 +020099static 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 Hugned0f91932015-03-05 10:23:49 +0100110/* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
111static 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 Alpe1ca73e32016-08-26 10:52:52 +0200117
118 if (tipc_udp_is_mcast_addr(ua))
Jon Paul Maloy99999742017-01-18 13:50:50 -0500119 addr->broadcast = TIPC_BROADCAST_SUPPORT;
Erik Hugned0f91932015-03-05 10:23:49 +0100120}
121
122/* tipc_udp_addr2str - convert ip/udp address to string */
123static 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 Alpebc3a3342016-06-27 13:34:07 +0200128 snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port));
Erik Hugned0f91932015-03-05 10:23:49 +0100129 else if (ntohs(ua->proto) == ETH_P_IPV6)
Richard Alpebc3a3342016-06-27 13:34:07 +0200130 snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port));
Erik Hugned0f91932015-03-05 10:23:49 +0100131 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 */
137static 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 */
150static 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 Alpece984da2016-08-26 10:52:51 +0200160static int tipc_udp_xmit(struct net *net, struct sk_buff *skb,
161 struct udp_bearer *ub, struct udp_media_addr *src,
Xin Longe9c1a792019-06-20 19:03:41 +0800162 struct udp_media_addr *dst, struct dst_cache *cache)
Erik Hugned0f91932015-03-05 10:23:49 +0100163{
Xin Longe9c1a792019-06-20 19:03:41 +0800164 struct dst_entry *ndst = dst_cache_get(cache);
Erik Hugned0f91932015-03-05 10:23:49 +0100165 int ttl, err = 0;
Erik Hugned0f91932015-03-05 10:23:49 +0100166
Erik Hugne4fee6be82015-03-09 10:19:31 +0100167 if (dst->proto == htons(ETH_P_IP)) {
Xin Longe9c1a792019-06-20 19:03:41 +0800168 struct rtable *rt = (struct rtable *)ndst;
169
170 if (!rt) {
171 struct flowi4 fl = {
172 .daddr = dst->ipv4.s_addr,
173 .saddr = src->ipv4.s_addr,
174 .flowi4_mark = skb->mark,
175 .flowi4_proto = IPPROTO_UDP
176 };
177 rt = ip_route_output_key(net, &fl);
178 if (IS_ERR(rt)) {
179 err = PTR_ERR(rt);
180 goto tx_error;
181 }
182 dst_cache_set_ip4(cache, &rt->dst, fl.saddr);
Erik Hugned0f91932015-03-05 10:23:49 +0100183 }
Richard Alpe9b300962016-03-03 14:20:40 +0100184
Erik Hugned0f91932015-03-05 10:23:49 +0100185 ttl = ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar039f5062015-12-24 14:34:54 -0800186 udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr,
Richard Alpebc3a3342016-06-27 13:34:07 +0200187 dst->ipv4.s_addr, 0, ttl, 0, src->port,
188 dst->port, false, true);
Erik Hugned0f91932015-03-05 10:23:49 +0100189#if IS_ENABLED(CONFIG_IPV6)
190 } else {
Xin Longe9c1a792019-06-20 19:03:41 +0800191 if (!ndst) {
192 struct flowi6 fl6 = {
193 .flowi6_oif = ub->ifindex,
194 .daddr = dst->ipv6,
195 .saddr = src->ipv6,
196 .flowi6_proto = IPPROTO_UDP
197 };
198 err = ipv6_stub->ipv6_dst_lookup(net, ub->ubsock->sk,
199 &ndst, &fl6);
200 if (err)
201 goto tx_error;
202 dst_cache_set_ip6(cache, ndst, &fl6.saddr);
203 }
Erik Hugned0f91932015-03-05 10:23:49 +0100204 ttl = ip6_dst_hoplimit(ndst);
Xin Longc3bcde02019-06-17 21:34:15 +0800205 err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb, NULL,
206 &src->ipv6, &dst->ipv6, 0, ttl, 0,
207 src->port, dst->port, false);
Erik Hugned0f91932015-03-05 10:23:49 +0100208#endif
209 }
210 return err;
211
212tx_error:
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400213 kfree_skb(skb);
Erik Hugned0f91932015-03-05 10:23:49 +0100214 return err;
215}
216
Richard Alpece984da2016-08-26 10:52:51 +0200217static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
218 struct tipc_bearer *b,
219 struct tipc_media_addr *addr)
220{
221 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
222 struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value;
Richard Alpeef20cd42016-08-26 10:52:53 +0200223 struct udp_replicast *rcast;
Richard Alpece984da2016-08-26 10:52:51 +0200224 struct udp_bearer *ub;
225 int err = 0;
226
227 if (skb_headroom(skb) < UDP_MIN_HEADROOM) {
228 err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC);
229 if (err)
Richard Alpeef20cd42016-08-26 10:52:53 +0200230 goto out;
Richard Alpece984da2016-08-26 10:52:51 +0200231 }
232
233 skb_set_inner_protocol(skb, htons(ETH_P_TIPC));
Xin Long30a46162019-07-02 00:54:55 +0800234 ub = rcu_dereference(b->media_ptr);
Richard Alpece984da2016-08-26 10:52:51 +0200235 if (!ub) {
236 err = -ENODEV;
Richard Alpeef20cd42016-08-26 10:52:53 +0200237 goto out;
Richard Alpece984da2016-08-26 10:52:51 +0200238 }
239
Jon Paul Maloy99999742017-01-18 13:50:50 -0500240 if (addr->broadcast != TIPC_REPLICAST_SUPPORT)
Xin Longe9c1a792019-06-20 19:03:41 +0800241 return tipc_udp_xmit(net, skb, ub, src, dst,
242 &ub->rcast.dst_cache);
Richard Alpece984da2016-08-26 10:52:51 +0200243
Richard Alpeef20cd42016-08-26 10:52:53 +0200244 /* Replicast, send an skb to each configured IP address */
245 list_for_each_entry_rcu(rcast, &ub->rcast.list, list) {
246 struct sk_buff *_skb;
247
248 _skb = pskb_copy(skb, GFP_ATOMIC);
249 if (!_skb) {
250 err = -ENOMEM;
251 goto out;
252 }
253
Xin Longe9c1a792019-06-20 19:03:41 +0800254 err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr,
255 &rcast->dst_cache);
Cong Wangacb4a332018-12-10 12:45:45 -0800256 if (err)
Richard Alpeef20cd42016-08-26 10:52:53 +0200257 goto out;
Richard Alpeef20cd42016-08-26 10:52:53 +0200258 }
259 err = 0;
260out:
Richard Alpece984da2016-08-26 10:52:51 +0200261 kfree_skb(skb);
262 return err;
263}
264
Richard Alpec9b64d42016-08-26 10:52:54 +0200265static bool tipc_udp_is_known_peer(struct tipc_bearer *b,
266 struct udp_media_addr *addr)
267{
268 struct udp_replicast *rcast, *tmp;
269 struct udp_bearer *ub;
270
271 ub = rcu_dereference_rtnl(b->media_ptr);
272 if (!ub) {
273 pr_err_ratelimited("UDP bearer instance not found\n");
274 return false;
275 }
276
277 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
278 if (!memcmp(&rcast->addr, addr, sizeof(struct udp_media_addr)))
279 return true;
280 }
281
282 return false;
283}
284
Richard Alpeef20cd42016-08-26 10:52:53 +0200285static int tipc_udp_rcast_add(struct tipc_bearer *b,
286 struct udp_media_addr *addr)
287{
288 struct udp_replicast *rcast;
289 struct udp_bearer *ub;
290
291 ub = rcu_dereference_rtnl(b->media_ptr);
292 if (!ub)
293 return -ENODEV;
294
295 rcast = kmalloc(sizeof(*rcast), GFP_ATOMIC);
296 if (!rcast)
297 return -ENOMEM;
298
Xin Longe9c1a792019-06-20 19:03:41 +0800299 if (dst_cache_init(&rcast->dst_cache, GFP_ATOMIC)) {
300 kfree(rcast);
301 return -ENOMEM;
302 }
303
Richard Alpeef20cd42016-08-26 10:52:53 +0200304 memcpy(&rcast->addr, addr, sizeof(struct udp_media_addr));
305
306 if (ntohs(addr->proto) == ETH_P_IP)
307 pr_info("New replicast peer: %pI4\n", &rcast->addr.ipv4);
308#if IS_ENABLED(CONFIG_IPV6)
309 else if (ntohs(addr->proto) == ETH_P_IPV6)
310 pr_info("New replicast peer: %pI6\n", &rcast->addr.ipv6);
311#endif
Jon Paul Maloy99999742017-01-18 13:50:50 -0500312 b->bcast_addr.broadcast = TIPC_REPLICAST_SUPPORT;
Richard Alpeef20cd42016-08-26 10:52:53 +0200313 list_add_rcu(&rcast->list, &ub->rcast.list);
314 return 0;
315}
316
Richard Alpec9b64d42016-08-26 10:52:54 +0200317static int tipc_udp_rcast_disc(struct tipc_bearer *b, struct sk_buff *skb)
318{
319 struct udp_media_addr src = {0};
320 struct udp_media_addr *dst;
321
322 dst = (struct udp_media_addr *)&b->bcast_addr.value;
323 if (tipc_udp_is_mcast_addr(dst))
324 return 0;
325
326 src.port = udp_hdr(skb)->source;
327
328 if (ip_hdr(skb)->version == 4) {
329 struct iphdr *iphdr = ip_hdr(skb);
330
331 src.proto = htons(ETH_P_IP);
332 src.ipv4.s_addr = iphdr->saddr;
333 if (ipv4_is_multicast(iphdr->daddr))
334 return 0;
335#if IS_ENABLED(CONFIG_IPV6)
336 } else if (ip_hdr(skb)->version == 6) {
337 struct ipv6hdr *iphdr = ipv6_hdr(skb);
338
339 src.proto = htons(ETH_P_IPV6);
340 src.ipv6 = iphdr->saddr;
341 if (ipv6_addr_is_multicast(&iphdr->daddr))
342 return 0;
343#endif
344 } else {
345 return 0;
346 }
347
348 if (likely(tipc_udp_is_known_peer(b, &src)))
349 return 0;
350
351 return tipc_udp_rcast_add(b, &src);
352}
353
Erik Hugned0f91932015-03-05 10:23:49 +0100354/* tipc_udp_recv - read data from bearer socket */
355static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
356{
357 struct udp_bearer *ub;
358 struct tipc_bearer *b;
Richard Alpec9b64d42016-08-26 10:52:54 +0200359 struct tipc_msg *hdr;
360 int err;
Erik Hugned0f91932015-03-05 10:23:49 +0100361
362 ub = rcu_dereference_sk_user_data(sk);
363 if (!ub) {
364 pr_err_ratelimited("Failed to get UDP bearer reference");
Richard Alpec9b64d42016-08-26 10:52:54 +0200365 goto out;
Erik Hugned0f91932015-03-05 10:23:49 +0100366 }
Erik Hugned0f91932015-03-05 10:23:49 +0100367 skb_pull(skb, sizeof(struct udphdr));
Richard Alpec9b64d42016-08-26 10:52:54 +0200368 hdr = buf_msg(skb);
369
Eric Dumazet4109a2c2019-04-23 09:24:46 -0700370 b = rcu_dereference(ub->bearer);
Richard Alpec9b64d42016-08-26 10:52:54 +0200371 if (!b)
Eric Dumazet4109a2c2019-04-23 09:24:46 -0700372 goto out;
Erik Hugned0f91932015-03-05 10:23:49 +0100373
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400374 if (b && test_bit(0, &b->up)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100375 tipc_rcv(sock_net(sk), skb, b);
Erik Hugned0f91932015-03-05 10:23:49 +0100376 return 0;
377 }
Richard Alpec9b64d42016-08-26 10:52:54 +0200378
379 if (unlikely(msg_user(hdr) == LINK_CONFIG)) {
380 err = tipc_udp_rcast_disc(b, skb);
381 if (err)
Eric Dumazet4109a2c2019-04-23 09:24:46 -0700382 goto out;
Richard Alpec9b64d42016-08-26 10:52:54 +0200383 }
384
Richard Alpec9b64d42016-08-26 10:52:54 +0200385out:
Erik Hugned0f91932015-03-05 10:23:49 +0100386 kfree_skb(skb);
387 return 0;
388}
389
390static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
391{
392 int err = 0;
393 struct ip_mreqn mreqn;
394 struct sock *sk = ub->ubsock->sk;
395
396 if (ntohs(remote->proto) == ETH_P_IP) {
Erik Hugned0f91932015-03-05 10:23:49 +0100397 mreqn.imr_multiaddr = remote->ipv4;
398 mreqn.imr_ifindex = ub->ifindex;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300399 err = ip_mc_join_group(sk, &mreqn);
Marcelo Ricardo Leitner446981e2015-03-19 16:47:58 -0300400#if IS_ENABLED(CONFIG_IPV6)
Erik Hugned0f91932015-03-05 10:23:49 +0100401 } else {
Marcelo Ricardo Leitner446981e2015-03-19 16:47:58 -0300402 err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
403 &remote->ipv6);
404#endif
Erik Hugned0f91932015-03-05 10:23:49 +0100405 }
406 return err;
407}
408
Richard Alpefdb3acc2016-08-26 10:52:55 +0200409static int __tipc_nl_add_udp_addr(struct sk_buff *skb,
410 struct udp_media_addr *addr, int nla_t)
411{
412 if (ntohs(addr->proto) == ETH_P_IP) {
413 struct sockaddr_in ip4;
414
Dan Carpenter73076162016-10-13 11:06:06 +0300415 memset(&ip4, 0, sizeof(ip4));
Richard Alpefdb3acc2016-08-26 10:52:55 +0200416 ip4.sin_family = AF_INET;
417 ip4.sin_port = addr->port;
418 ip4.sin_addr.s_addr = addr->ipv4.s_addr;
419 if (nla_put(skb, nla_t, sizeof(ip4), &ip4))
420 return -EMSGSIZE;
421
422#if IS_ENABLED(CONFIG_IPV6)
423 } else if (ntohs(addr->proto) == ETH_P_IPV6) {
424 struct sockaddr_in6 ip6;
425
Dan Carpenter73076162016-10-13 11:06:06 +0300426 memset(&ip6, 0, sizeof(ip6));
Richard Alpefdb3acc2016-08-26 10:52:55 +0200427 ip6.sin6_family = AF_INET6;
428 ip6.sin6_port = addr->port;
429 memcpy(&ip6.sin6_addr, &addr->ipv6, sizeof(struct in6_addr));
430 if (nla_put(skb, nla_t, sizeof(ip6), &ip6))
431 return -EMSGSIZE;
432#endif
433 }
434
435 return 0;
436}
437
Richard Alpe832629c2016-08-26 10:52:56 +0200438int tipc_udp_nl_dump_remoteip(struct sk_buff *skb, struct netlink_callback *cb)
439{
440 u32 bid = cb->args[0];
441 u32 skip_cnt = cb->args[1];
442 u32 portid = NETLINK_CB(cb->skb).portid;
443 struct udp_replicast *rcast, *tmp;
444 struct tipc_bearer *b;
445 struct udp_bearer *ub;
446 void *hdr;
447 int err;
448 int i;
449
450 if (!bid && !skip_cnt) {
Jiri Pirko057af702019-10-05 20:04:39 +0200451 struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
Richard Alpe832629c2016-08-26 10:52:56 +0200452 struct net *net = sock_net(skb->sk);
453 struct nlattr *battrs[TIPC_NLA_BEARER_MAX + 1];
Richard Alpe832629c2016-08-26 10:52:56 +0200454 char *bname;
455
Richard Alpe832629c2016-08-26 10:52:56 +0200456 if (!attrs[TIPC_NLA_BEARER])
457 return -EINVAL;
458
Johannes Berg8cb08172019-04-26 14:07:28 +0200459 err = nla_parse_nested_deprecated(battrs, TIPC_NLA_BEARER_MAX,
460 attrs[TIPC_NLA_BEARER],
461 tipc_nl_bearer_policy, NULL);
Richard Alpe832629c2016-08-26 10:52:56 +0200462 if (err)
463 return err;
464
465 if (!battrs[TIPC_NLA_BEARER_NAME])
466 return -EINVAL;
467
468 bname = nla_data(battrs[TIPC_NLA_BEARER_NAME]);
469
470 rtnl_lock();
471 b = tipc_bearer_find(net, bname);
472 if (!b) {
473 rtnl_unlock();
474 return -EINVAL;
475 }
476 bid = b->identity;
477 } else {
478 struct net *net = sock_net(skb->sk);
479 struct tipc_net *tn = net_generic(net, tipc_net_id);
480
481 rtnl_lock();
482 b = rtnl_dereference(tn->bearer_list[bid]);
483 if (!b) {
484 rtnl_unlock();
485 return -EINVAL;
486 }
487 }
488
Xin Long30a46162019-07-02 00:54:55 +0800489 ub = rtnl_dereference(b->media_ptr);
Richard Alpe832629c2016-08-26 10:52:56 +0200490 if (!ub) {
491 rtnl_unlock();
492 return -EINVAL;
493 }
494
495 i = 0;
496 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
497 if (i < skip_cnt)
498 goto count;
499
500 hdr = genlmsg_put(skb, portid, cb->nlh->nlmsg_seq,
501 &tipc_genl_family, NLM_F_MULTI,
502 TIPC_NL_BEARER_GET);
503 if (!hdr)
504 goto done;
505
506 err = __tipc_nl_add_udp_addr(skb, &rcast->addr,
507 TIPC_NLA_UDP_REMOTE);
508 if (err) {
509 genlmsg_cancel(skb, hdr);
510 goto done;
511 }
512 genlmsg_end(skb, hdr);
513count:
514 i++;
515 }
516done:
517 rtnl_unlock();
518 cb->args[0] = bid;
519 cb->args[1] = i;
520
521 return skb->len;
522}
523
Richard Alpefdb3acc2016-08-26 10:52:55 +0200524int tipc_udp_nl_add_bearer_data(struct tipc_nl_msg *msg, struct tipc_bearer *b)
525{
526 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
527 struct udp_media_addr *dst;
528 struct udp_bearer *ub;
529 struct nlattr *nest;
530
Xin Long30a46162019-07-02 00:54:55 +0800531 ub = rtnl_dereference(b->media_ptr);
Richard Alpefdb3acc2016-08-26 10:52:55 +0200532 if (!ub)
533 return -ENODEV;
534
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200535 nest = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_UDP_OPTS);
Richard Alpefdb3acc2016-08-26 10:52:55 +0200536 if (!nest)
537 goto msg_full;
538
539 if (__tipc_nl_add_udp_addr(msg->skb, src, TIPC_NLA_UDP_LOCAL))
540 goto msg_full;
541
542 dst = (struct udp_media_addr *)&b->bcast_addr.value;
543 if (__tipc_nl_add_udp_addr(msg->skb, dst, TIPC_NLA_UDP_REMOTE))
544 goto msg_full;
545
546 if (!list_empty(&ub->rcast.list)) {
547 if (nla_put_flag(msg->skb, TIPC_NLA_UDP_MULTI_REMOTEIP))
548 goto msg_full;
549 }
550
551 nla_nest_end(msg->skb, nest);
552 return 0;
553msg_full:
554 nla_nest_cancel(msg->skb, nest);
555 return -EMSGSIZE;
556}
557
Erik Hugned0f91932015-03-05 10:23:49 +0100558/**
Richard Alpeba5aa842016-08-26 10:52:50 +0200559 * tipc_parse_udp_addr - build udp media address from netlink data
560 * @nlattr: netlink attribute containing sockaddr storage aligned address
561 * @addr: tipc media address to fill with address, port and protocol type
562 * @scope_id: IPv6 scope id pointer, not NULL indicates it's required
Erik Hugned0f91932015-03-05 10:23:49 +0100563 */
Richard Alpeba5aa842016-08-26 10:52:50 +0200564
565static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr,
566 u32 *scope_id)
Erik Hugned0f91932015-03-05 10:23:49 +0100567{
Richard Alpeba5aa842016-08-26 10:52:50 +0200568 struct sockaddr_storage sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100569
Richard Alpeba5aa842016-08-26 10:52:50 +0200570 nla_memcpy(&sa, nla, sizeof(sa));
571 if (sa.ss_family == AF_INET) {
572 struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100573
Richard Alpeba5aa842016-08-26 10:52:50 +0200574 addr->proto = htons(ETH_P_IP);
575 addr->port = ip4->sin_port;
576 addr->ipv4.s_addr = ip4->sin_addr.s_addr;
Erik Hugned0f91932015-03-05 10:23:49 +0100577 return 0;
578
579#if IS_ENABLED(CONFIG_IPV6)
Richard Alpeba5aa842016-08-26 10:52:50 +0200580 } else if (sa.ss_family == AF_INET6) {
581 struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100582
Richard Alpeba5aa842016-08-26 10:52:50 +0200583 addr->proto = htons(ETH_P_IPV6);
584 addr->port = ip6->sin6_port;
585 memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr));
Richard Alpe34f65db2016-03-03 14:20:43 +0100586
Richard Alpeba5aa842016-08-26 10:52:50 +0200587 /* Scope ID is only interesting for local addresses */
588 if (scope_id) {
589 int atype;
Erik Hugned0f91932015-03-05 10:23:49 +0100590
Richard Alpeba5aa842016-08-26 10:52:50 +0200591 atype = ipv6_addr_type(&ip6->sin6_addr);
592 if (__ipv6_addr_needs_scope_id(atype) &&
593 !ip6->sin6_scope_id) {
594 return -EINVAL;
595 }
596
597 *scope_id = ip6->sin6_scope_id ? : 0;
598 }
599
Erik Hugned0f91932015-03-05 10:23:49 +0100600 return 0;
601#endif
602 }
603 return -EADDRNOTAVAIL;
604}
605
Richard Alpeef20cd42016-08-26 10:52:53 +0200606int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr)
607{
608 int err;
609 struct udp_media_addr addr = {0};
610 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
611 struct udp_media_addr *dst;
612
Johannes Berg8cb08172019-04-26 14:07:28 +0200613 if (nla_parse_nested_deprecated(opts, TIPC_NLA_UDP_MAX, attr, tipc_nl_udp_policy, NULL))
Richard Alpeef20cd42016-08-26 10:52:53 +0200614 return -EINVAL;
615
616 if (!opts[TIPC_NLA_UDP_REMOTE])
617 return -EINVAL;
618
619 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL);
620 if (err)
621 return err;
622
623 dst = (struct udp_media_addr *)&b->bcast_addr.value;
624 if (tipc_udp_is_mcast_addr(dst)) {
625 pr_err("Can't add remote ip to TIPC UDP multicast bearer\n");
626 return -EINVAL;
627 }
628
Richard Alpec9b64d42016-08-26 10:52:54 +0200629 if (tipc_udp_is_known_peer(b, &addr))
630 return 0;
631
Richard Alpeef20cd42016-08-26 10:52:53 +0200632 return tipc_udp_rcast_add(b, &addr);
633}
634
Erik Hugned0f91932015-03-05 10:23:49 +0100635/**
636 * tipc_udp_enable - callback to create a new udp bearer instance
637 * @net: network namespace
638 * @b: pointer to generic tipc_bearer
639 * @attrs: netlink bearer configuration
640 *
641 * validate the bearer parameters and initialize the udp bearer
642 * rtnl_lock should be held
643 */
644static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
645 struct nlattr *attrs[])
646{
647 int err = -EINVAL;
648 struct udp_bearer *ub;
Richard Alpeef20cd42016-08-26 10:52:53 +0200649 struct udp_media_addr remote = {0};
Erik Hugned0f91932015-03-05 10:23:49 +0100650 struct udp_media_addr local = {0};
651 struct udp_port_cfg udp_conf = {0};
Erik Hugne4fee6be82015-03-09 10:19:31 +0100652 struct udp_tunnel_sock_cfg tuncfg = {NULL};
Richard Alpeba5aa842016-08-26 10:52:50 +0200653 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
Jon Maloy52dfae52018-03-22 20:42:52 +0100654 u8 node_id[NODE_ID_LEN] = {0,};
Hoang Leacad76a2018-10-11 08:43:08 +0700655 int rmcast = 0;
Erik Hugned0f91932015-03-05 10:23:49 +0100656
657 ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
658 if (!ub)
659 return -ENOMEM;
660
Richard Alpeef20cd42016-08-26 10:52:53 +0200661 INIT_LIST_HEAD(&ub->rcast.list);
662
Richard Alpeba5aa842016-08-26 10:52:50 +0200663 if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
664 goto err;
665
Johannes Berg8cb08172019-04-26 14:07:28 +0200666 if (nla_parse_nested_deprecated(opts, TIPC_NLA_UDP_MAX, attrs[TIPC_NLA_BEARER_UDP_OPTS], tipc_nl_udp_policy, NULL))
Richard Alpeba5aa842016-08-26 10:52:50 +0200667 goto err;
668
669 if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) {
670 pr_err("Invalid UDP bearer configuration");
Wei Yongjunc20cb812016-09-10 00:56:55 +0000671 err = -EINVAL;
672 goto err;
Richard Alpeba5aa842016-08-26 10:52:50 +0200673 }
674
675 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local,
676 &ub->ifindex);
677 if (err)
678 goto err;
679
Richard Alpeef20cd42016-08-26 10:52:53 +0200680 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL);
Erik Hugned0f91932015-03-05 10:23:49 +0100681 if (err)
682 goto err;
683
Cong Wangfb83ed42018-12-10 15:23:30 -0800684 if (remote.proto != local.proto) {
685 err = -EINVAL;
686 goto err;
687 }
688
Hoang Leacad76a2018-10-11 08:43:08 +0700689 /* Checking remote ip address */
690 rmcast = tipc_udp_is_mcast_addr(&remote);
691
Jon Maloy52dfae52018-03-22 20:42:52 +0100692 /* Autoconfigure own node identity if needed */
693 if (!tipc_own_id(net)) {
694 memcpy(node_id, local.ipv6.in6_u.u6_addr8, 16);
695 tipc_net_init(net, node_id, 0);
696 }
697 if (!tipc_own_id(net)) {
698 pr_warn("Failed to set node id, please configure manually\n");
Wei Yongjunc76f2482018-03-26 14:32:44 +0000699 err = -EINVAL;
700 goto err;
Jon Maloy52dfae52018-03-22 20:42:52 +0100701 }
702
Erik Hugned0f91932015-03-05 10:23:49 +0100703 b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
Jon Paul Maloy99999742017-01-18 13:50:50 -0500704 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
Erik Hugned0f91932015-03-05 10:23:49 +0100705 rcu_assign_pointer(b->media_ptr, ub);
706 rcu_assign_pointer(ub->bearer, b);
707 tipc_udp_media_addr_set(&b->addr, &local);
Erik Hugne4fee6be82015-03-09 10:19:31 +0100708 if (local.proto == htons(ETH_P_IP)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100709 struct net_device *dev;
710
711 dev = __ip_dev_find(net, local.ipv4.s_addr, false);
712 if (!dev) {
713 err = -ENODEV;
714 goto err;
715 }
716 udp_conf.family = AF_INET;
Hoang Leacad76a2018-10-11 08:43:08 +0700717
718 /* Switch to use ANY to receive packets from group */
719 if (rmcast)
720 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
721 else
722 udp_conf.local_ip.s_addr = local.ipv4.s_addr;
Erik Hugned0f91932015-03-05 10:23:49 +0100723 udp_conf.use_udp_checksums = false;
724 ub->ifindex = dev->ifindex;
Michal Kubeček3de81b72016-12-02 09:33:41 +0100725 if (tipc_mtu_bad(dev, sizeof(struct iphdr) +
726 sizeof(struct udphdr))) {
727 err = -EINVAL;
728 goto err;
729 }
GhantaKrishnamurthy MohanKrishnaa4dfa722018-04-19 11:06:18 +0200730 b->mtu = b->media->mtu;
Erik Hugned0f91932015-03-05 10:23:49 +0100731#if IS_ENABLED(CONFIG_IPV6)
Erik Hugne4fee6be82015-03-09 10:19:31 +0100732 } else if (local.proto == htons(ETH_P_IPV6)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100733 udp_conf.family = AF_INET6;
734 udp_conf.use_udp6_tx_checksums = true;
735 udp_conf.use_udp6_rx_checksums = true;
Hoang Leacad76a2018-10-11 08:43:08 +0700736 if (rmcast)
737 udp_conf.local_ip6 = in6addr_any;
738 else
739 udp_conf.local_ip6 = local.ipv6;
Erik Hugned0f91932015-03-05 10:23:49 +0100740 b->mtu = 1280;
741#endif
742 } else {
743 err = -EAFNOSUPPORT;
744 goto err;
745 }
Richard Alpebc3a3342016-06-27 13:34:07 +0200746 udp_conf.local_udp_port = local.port;
Erik Hugned0f91932015-03-05 10:23:49 +0100747 err = udp_sock_create(net, &udp_conf, &ub->ubsock);
748 if (err)
749 goto err;
750 tuncfg.sk_user_data = ub;
751 tuncfg.encap_type = 1;
752 tuncfg.encap_rcv = tipc_udp_recv;
753 tuncfg.encap_destroy = NULL;
754 setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
755
Xin Longe9c1a792019-06-20 19:03:41 +0800756 err = dst_cache_init(&ub->rcast.dst_cache, GFP_ATOMIC);
757 if (err)
Xin Longd2c3a4b2019-07-02 00:57:19 +0800758 goto free;
Xin Longe9c1a792019-06-20 19:03:41 +0800759
Richard Alpeef20cd42016-08-26 10:52:53 +0200760 /**
761 * The bcast media address port is used for all peers and the ip
762 * is used if it's a multicast address.
763 */
764 memcpy(&b->bcast_addr.value, &remote, sizeof(remote));
Hoang Leacad76a2018-10-11 08:43:08 +0700765 if (rmcast)
Richard Alpeef20cd42016-08-26 10:52:53 +0200766 err = enable_mcast(ub, &remote);
767 else
768 err = tipc_udp_rcast_add(b, &remote);
769 if (err)
Xin Longd2c3a4b2019-07-02 00:57:19 +0800770 goto free;
Richard Alpe1ca73e32016-08-26 10:52:52 +0200771
Erik Hugned0f91932015-03-05 10:23:49 +0100772 return 0;
Xin Longd2c3a4b2019-07-02 00:57:19 +0800773
774free:
Xin Longe9c1a792019-06-20 19:03:41 +0800775 dst_cache_destroy(&ub->rcast.dst_cache);
Xin Longd2c3a4b2019-07-02 00:57:19 +0800776 udp_tunnel_sock_release(ub->ubsock);
777err:
Erik Hugned0f91932015-03-05 10:23:49 +0100778 kfree(ub);
779 return err;
780}
781
782/* cleanup_bearer - break the socket/bearer association */
783static void cleanup_bearer(struct work_struct *work)
784{
785 struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
Richard Alpeef20cd42016-08-26 10:52:53 +0200786 struct udp_replicast *rcast, *tmp;
787
788 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
Xin Longe9c1a792019-06-20 19:03:41 +0800789 dst_cache_destroy(&rcast->dst_cache);
Richard Alpeef20cd42016-08-26 10:52:53 +0200790 list_del_rcu(&rcast->list);
791 kfree_rcu(rcast, rcu);
792 }
Erik Hugned0f91932015-03-05 10:23:49 +0100793
Xin Longe9c1a792019-06-20 19:03:41 +0800794 dst_cache_destroy(&ub->rcast.dst_cache);
Xin Longd2c3a4b2019-07-02 00:57:19 +0800795 udp_tunnel_sock_release(ub->ubsock);
Erik Hugned0f91932015-03-05 10:23:49 +0100796 synchronize_net();
797 kfree(ub);
798}
799
800/* tipc_udp_disable - detach bearer from socket */
801static void tipc_udp_disable(struct tipc_bearer *b)
802{
803 struct udp_bearer *ub;
804
Xin Long30a46162019-07-02 00:54:55 +0800805 ub = rtnl_dereference(b->media_ptr);
Erik Hugned0f91932015-03-05 10:23:49 +0100806 if (!ub) {
807 pr_err("UDP bearer instance not found\n");
808 return;
809 }
Xin Longd2c3a4b2019-07-02 00:57:19 +0800810 sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
Erik Hugned0f91932015-03-05 10:23:49 +0100811 RCU_INIT_POINTER(ub->bearer, NULL);
812
813 /* sock_release need to be done outside of rtnl lock */
814 INIT_WORK(&ub->work, cleanup_bearer);
815 schedule_work(&ub->work);
816}
817
818struct tipc_media udp_media_info = {
819 .send_msg = tipc_udp_send_msg,
820 .enable_media = tipc_udp_enable,
821 .disable_media = tipc_udp_disable,
822 .addr2str = tipc_udp_addr2str,
823 .addr2msg = tipc_udp_addr2msg,
824 .msg2addr = tipc_udp_msg2addr,
825 .priority = TIPC_DEF_LINK_PRI,
826 .tolerance = TIPC_DEF_LINK_TOL,
827 .window = TIPC_DEF_LINK_WIN,
GhantaKrishnamurthy MohanKrishnaa4dfa722018-04-19 11:06:18 +0200828 .mtu = TIPC_DEF_LINK_UDP_MTU,
Erik Hugned0f91932015-03-05 10:23:49 +0100829 .type_id = TIPC_MEDIA_TYPE_UDP,
830 .hwaddr_len = 0,
831 .name = "udp"
832};