blob: a236281082726ea2945275d06f6cf568c03230f6 [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"
Wang Hai5f3666e2020-09-18 21:18:19 +080055#include "udp_media.h"
Erik Hugned0f91932015-03-05 10:23:49 +010056
57/* IANA assigned UDP port */
58#define UDP_PORT_DEFAULT 6118
59
Richard Alpe9bd160b2016-03-14 09:43:52 +010060#define UDP_MIN_HEADROOM 48
Jon Paul Maloye5356792015-10-19 11:43:11 -040061
Erik Hugned0f91932015-03-05 10:23:49 +010062/**
63 * struct udp_media_addr - IP/UDP addressing information
64 *
65 * This is the bearer level originating address used in neighbor discovery
66 * messages, and all fields should be in network byte order
67 */
68struct udp_media_addr {
69 __be16 proto;
Richard Alpebc3a3342016-06-27 13:34:07 +020070 __be16 port;
Erik Hugned0f91932015-03-05 10:23:49 +010071 union {
72 struct in_addr ipv4;
73 struct in6_addr ipv6;
74 };
75};
76
Richard Alpeef20cd42016-08-26 10:52:53 +020077/* struct udp_replicast - container for UDP remote addresses */
78struct udp_replicast {
79 struct udp_media_addr addr;
Xin Longe9c1a792019-06-20 19:03:41 +080080 struct dst_cache dst_cache;
Richard Alpeef20cd42016-08-26 10:52:53 +020081 struct rcu_head rcu;
82 struct list_head list;
83};
84
Erik Hugned0f91932015-03-05 10:23:49 +010085/**
86 * struct udp_bearer - ip/udp bearer data structure
87 * @bearer: associated generic tipc bearer
88 * @ubsock: bearer associated socket
89 * @ifindex: local address scope
90 * @work: used to schedule deferred work on a bearer
91 */
92struct udp_bearer {
93 struct tipc_bearer __rcu *bearer;
94 struct socket *ubsock;
95 u32 ifindex;
96 struct work_struct work;
Richard Alpeef20cd42016-08-26 10:52:53 +020097 struct udp_replicast rcast;
Erik Hugned0f91932015-03-05 10:23:49 +010098};
99
Richard Alpe1ca73e32016-08-26 10:52:52 +0200100static int tipc_udp_is_mcast_addr(struct udp_media_addr *addr)
101{
102 if (ntohs(addr->proto) == ETH_P_IP)
103 return ipv4_is_multicast(addr->ipv4.s_addr);
104#if IS_ENABLED(CONFIG_IPV6)
105 else
106 return ipv6_addr_is_multicast(&addr->ipv6);
107#endif
108 return 0;
109}
110
Erik Hugned0f91932015-03-05 10:23:49 +0100111/* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
112static void tipc_udp_media_addr_set(struct tipc_media_addr *addr,
113 struct udp_media_addr *ua)
114{
115 memset(addr, 0, sizeof(struct tipc_media_addr));
116 addr->media_id = TIPC_MEDIA_TYPE_UDP;
117 memcpy(addr->value, ua, sizeof(struct udp_media_addr));
Richard Alpe1ca73e32016-08-26 10:52:52 +0200118
119 if (tipc_udp_is_mcast_addr(ua))
Jon Paul Maloy99999742017-01-18 13:50:50 -0500120 addr->broadcast = TIPC_BROADCAST_SUPPORT;
Erik Hugned0f91932015-03-05 10:23:49 +0100121}
122
123/* tipc_udp_addr2str - convert ip/udp address to string */
124static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size)
125{
126 struct udp_media_addr *ua = (struct udp_media_addr *)&a->value;
127
128 if (ntohs(ua->proto) == ETH_P_IP)
Richard Alpebc3a3342016-06-27 13:34:07 +0200129 snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port));
Erik Hugned0f91932015-03-05 10:23:49 +0100130 else if (ntohs(ua->proto) == ETH_P_IPV6)
Richard Alpebc3a3342016-06-27 13:34:07 +0200131 snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port));
Erik Hugned0f91932015-03-05 10:23:49 +0100132 else
133 pr_err("Invalid UDP media address\n");
134 return 0;
135}
136
137/* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
138static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a,
139 char *msg)
140{
141 struct udp_media_addr *ua;
142
143 ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET);
144 if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP)
145 return -EINVAL;
146 tipc_udp_media_addr_set(a, ua);
147 return 0;
148}
149
150/* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
151static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a)
152{
153 memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
154 msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP;
155 memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value,
156 sizeof(struct udp_media_addr));
157 return 0;
158}
159
160/* tipc_send_msg - enqueue a send request */
Richard Alpece984da2016-08-26 10:52:51 +0200161static int tipc_udp_xmit(struct net *net, struct sk_buff *skb,
162 struct udp_bearer *ub, struct udp_media_addr *src,
Xin Longe9c1a792019-06-20 19:03:41 +0800163 struct udp_media_addr *dst, struct dst_cache *cache)
Erik Hugned0f91932015-03-05 10:23:49 +0100164{
Eric Dumazet13788172020-05-21 11:29:58 -0700165 struct dst_entry *ndst;
Erik Hugned0f91932015-03-05 10:23:49 +0100166 int ttl, err = 0;
Erik Hugned0f91932015-03-05 10:23:49 +0100167
Eric Dumazet13788172020-05-21 11:29:58 -0700168 local_bh_disable();
169 ndst = dst_cache_get(cache);
Erik Hugne4fee6be82015-03-09 10:19:31 +0100170 if (dst->proto == htons(ETH_P_IP)) {
Xin Longe9c1a792019-06-20 19:03:41 +0800171 struct rtable *rt = (struct rtable *)ndst;
172
173 if (!rt) {
174 struct flowi4 fl = {
175 .daddr = dst->ipv4.s_addr,
176 .saddr = src->ipv4.s_addr,
177 .flowi4_mark = skb->mark,
178 .flowi4_proto = IPPROTO_UDP
179 };
180 rt = ip_route_output_key(net, &fl);
181 if (IS_ERR(rt)) {
182 err = PTR_ERR(rt);
183 goto tx_error;
184 }
185 dst_cache_set_ip4(cache, &rt->dst, fl.saddr);
Erik Hugned0f91932015-03-05 10:23:49 +0100186 }
Richard Alpe9b300962016-03-03 14:20:40 +0100187
Erik Hugned0f91932015-03-05 10:23:49 +0100188 ttl = ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar039f5062015-12-24 14:34:54 -0800189 udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr,
Richard Alpebc3a3342016-06-27 13:34:07 +0200190 dst->ipv4.s_addr, 0, ttl, 0, src->port,
191 dst->port, false, true);
Erik Hugned0f91932015-03-05 10:23:49 +0100192#if IS_ENABLED(CONFIG_IPV6)
193 } else {
Xin Longe9c1a792019-06-20 19:03:41 +0800194 if (!ndst) {
195 struct flowi6 fl6 = {
196 .flowi6_oif = ub->ifindex,
197 .daddr = dst->ipv6,
198 .saddr = src->ipv6,
199 .flowi6_proto = IPPROTO_UDP
200 };
Sabrina Dubroca6c8991f2019-12-04 15:35:53 +0100201 ndst = ipv6_stub->ipv6_dst_lookup_flow(net,
202 ub->ubsock->sk,
203 &fl6, NULL);
204 if (IS_ERR(ndst)) {
205 err = PTR_ERR(ndst);
Xin Longe9c1a792019-06-20 19:03:41 +0800206 goto tx_error;
Sabrina Dubroca6c8991f2019-12-04 15:35:53 +0100207 }
Xin Longe9c1a792019-06-20 19:03:41 +0800208 dst_cache_set_ip6(cache, ndst, &fl6.saddr);
209 }
Erik Hugned0f91932015-03-05 10:23:49 +0100210 ttl = ip6_dst_hoplimit(ndst);
Xin Longc3bcde02019-06-17 21:34:15 +0800211 err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb, NULL,
212 &src->ipv6, &dst->ipv6, 0, ttl, 0,
213 src->port, dst->port, false);
Erik Hugned0f91932015-03-05 10:23:49 +0100214#endif
215 }
Eric Dumazet13788172020-05-21 11:29:58 -0700216 local_bh_enable();
Erik Hugned0f91932015-03-05 10:23:49 +0100217 return err;
218
219tx_error:
Eric Dumazet13788172020-05-21 11:29:58 -0700220 local_bh_enable();
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400221 kfree_skb(skb);
Erik Hugned0f91932015-03-05 10:23:49 +0100222 return err;
223}
224
Richard Alpece984da2016-08-26 10:52:51 +0200225static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
226 struct tipc_bearer *b,
227 struct tipc_media_addr *addr)
228{
229 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
230 struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value;
Richard Alpeef20cd42016-08-26 10:52:53 +0200231 struct udp_replicast *rcast;
Richard Alpece984da2016-08-26 10:52:51 +0200232 struct udp_bearer *ub;
233 int err = 0;
234
235 if (skb_headroom(skb) < UDP_MIN_HEADROOM) {
236 err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC);
237 if (err)
Richard Alpeef20cd42016-08-26 10:52:53 +0200238 goto out;
Richard Alpece984da2016-08-26 10:52:51 +0200239 }
240
241 skb_set_inner_protocol(skb, htons(ETH_P_TIPC));
Xin Long30a46162019-07-02 00:54:55 +0800242 ub = rcu_dereference(b->media_ptr);
Richard Alpece984da2016-08-26 10:52:51 +0200243 if (!ub) {
244 err = -ENODEV;
Richard Alpeef20cd42016-08-26 10:52:53 +0200245 goto out;
Richard Alpece984da2016-08-26 10:52:51 +0200246 }
247
Jon Paul Maloy99999742017-01-18 13:50:50 -0500248 if (addr->broadcast != TIPC_REPLICAST_SUPPORT)
Xin Longe9c1a792019-06-20 19:03:41 +0800249 return tipc_udp_xmit(net, skb, ub, src, dst,
250 &ub->rcast.dst_cache);
Richard Alpece984da2016-08-26 10:52:51 +0200251
Richard Alpeef20cd42016-08-26 10:52:53 +0200252 /* Replicast, send an skb to each configured IP address */
253 list_for_each_entry_rcu(rcast, &ub->rcast.list, list) {
254 struct sk_buff *_skb;
255
256 _skb = pskb_copy(skb, GFP_ATOMIC);
257 if (!_skb) {
258 err = -ENOMEM;
259 goto out;
260 }
261
Xin Longe9c1a792019-06-20 19:03:41 +0800262 err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr,
263 &rcast->dst_cache);
Cong Wangacb4a332018-12-10 12:45:45 -0800264 if (err)
Richard Alpeef20cd42016-08-26 10:52:53 +0200265 goto out;
Richard Alpeef20cd42016-08-26 10:52:53 +0200266 }
267 err = 0;
268out:
Richard Alpece984da2016-08-26 10:52:51 +0200269 kfree_skb(skb);
270 return err;
271}
272
Richard Alpec9b64d42016-08-26 10:52:54 +0200273static bool tipc_udp_is_known_peer(struct tipc_bearer *b,
274 struct udp_media_addr *addr)
275{
276 struct udp_replicast *rcast, *tmp;
277 struct udp_bearer *ub;
278
279 ub = rcu_dereference_rtnl(b->media_ptr);
280 if (!ub) {
281 pr_err_ratelimited("UDP bearer instance not found\n");
282 return false;
283 }
284
285 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
286 if (!memcmp(&rcast->addr, addr, sizeof(struct udp_media_addr)))
287 return true;
288 }
289
290 return false;
291}
292
Richard Alpeef20cd42016-08-26 10:52:53 +0200293static int tipc_udp_rcast_add(struct tipc_bearer *b,
294 struct udp_media_addr *addr)
295{
296 struct udp_replicast *rcast;
297 struct udp_bearer *ub;
298
299 ub = rcu_dereference_rtnl(b->media_ptr);
300 if (!ub)
301 return -ENODEV;
302
303 rcast = kmalloc(sizeof(*rcast), GFP_ATOMIC);
304 if (!rcast)
305 return -ENOMEM;
306
Xin Longe9c1a792019-06-20 19:03:41 +0800307 if (dst_cache_init(&rcast->dst_cache, GFP_ATOMIC)) {
308 kfree(rcast);
309 return -ENOMEM;
310 }
311
Richard Alpeef20cd42016-08-26 10:52:53 +0200312 memcpy(&rcast->addr, addr, sizeof(struct udp_media_addr));
313
314 if (ntohs(addr->proto) == ETH_P_IP)
315 pr_info("New replicast peer: %pI4\n", &rcast->addr.ipv4);
316#if IS_ENABLED(CONFIG_IPV6)
317 else if (ntohs(addr->proto) == ETH_P_IPV6)
318 pr_info("New replicast peer: %pI6\n", &rcast->addr.ipv6);
319#endif
Jon Paul Maloy99999742017-01-18 13:50:50 -0500320 b->bcast_addr.broadcast = TIPC_REPLICAST_SUPPORT;
Richard Alpeef20cd42016-08-26 10:52:53 +0200321 list_add_rcu(&rcast->list, &ub->rcast.list);
322 return 0;
323}
324
Richard Alpec9b64d42016-08-26 10:52:54 +0200325static int tipc_udp_rcast_disc(struct tipc_bearer *b, struct sk_buff *skb)
326{
327 struct udp_media_addr src = {0};
328 struct udp_media_addr *dst;
329
330 dst = (struct udp_media_addr *)&b->bcast_addr.value;
331 if (tipc_udp_is_mcast_addr(dst))
332 return 0;
333
334 src.port = udp_hdr(skb)->source;
335
336 if (ip_hdr(skb)->version == 4) {
337 struct iphdr *iphdr = ip_hdr(skb);
338
339 src.proto = htons(ETH_P_IP);
340 src.ipv4.s_addr = iphdr->saddr;
341 if (ipv4_is_multicast(iphdr->daddr))
342 return 0;
343#if IS_ENABLED(CONFIG_IPV6)
344 } else if (ip_hdr(skb)->version == 6) {
345 struct ipv6hdr *iphdr = ipv6_hdr(skb);
346
347 src.proto = htons(ETH_P_IPV6);
348 src.ipv6 = iphdr->saddr;
349 if (ipv6_addr_is_multicast(&iphdr->daddr))
350 return 0;
351#endif
352 } else {
353 return 0;
354 }
355
356 if (likely(tipc_udp_is_known_peer(b, &src)))
357 return 0;
358
359 return tipc_udp_rcast_add(b, &src);
360}
361
Erik Hugned0f91932015-03-05 10:23:49 +0100362/* tipc_udp_recv - read data from bearer socket */
363static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
364{
365 struct udp_bearer *ub;
366 struct tipc_bearer *b;
Richard Alpec9b64d42016-08-26 10:52:54 +0200367 struct tipc_msg *hdr;
368 int err;
Erik Hugned0f91932015-03-05 10:23:49 +0100369
370 ub = rcu_dereference_sk_user_data(sk);
371 if (!ub) {
372 pr_err_ratelimited("Failed to get UDP bearer reference");
Richard Alpec9b64d42016-08-26 10:52:54 +0200373 goto out;
Erik Hugned0f91932015-03-05 10:23:49 +0100374 }
Erik Hugned0f91932015-03-05 10:23:49 +0100375 skb_pull(skb, sizeof(struct udphdr));
Richard Alpec9b64d42016-08-26 10:52:54 +0200376 hdr = buf_msg(skb);
377
Eric Dumazet4109a2c2019-04-23 09:24:46 -0700378 b = rcu_dereference(ub->bearer);
Richard Alpec9b64d42016-08-26 10:52:54 +0200379 if (!b)
Eric Dumazet4109a2c2019-04-23 09:24:46 -0700380 goto out;
Erik Hugned0f91932015-03-05 10:23:49 +0100381
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400382 if (b && test_bit(0, &b->up)) {
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700383 TIPC_SKB_CB(skb)->flags = 0;
Erik Hugned0f91932015-03-05 10:23:49 +0100384 tipc_rcv(sock_net(sk), skb, b);
Erik Hugned0f91932015-03-05 10:23:49 +0100385 return 0;
386 }
Richard Alpec9b64d42016-08-26 10:52:54 +0200387
388 if (unlikely(msg_user(hdr) == LINK_CONFIG)) {
389 err = tipc_udp_rcast_disc(b, skb);
390 if (err)
Eric Dumazet4109a2c2019-04-23 09:24:46 -0700391 goto out;
Richard Alpec9b64d42016-08-26 10:52:54 +0200392 }
393
Richard Alpec9b64d42016-08-26 10:52:54 +0200394out:
Erik Hugned0f91932015-03-05 10:23:49 +0100395 kfree_skb(skb);
396 return 0;
397}
398
399static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
400{
401 int err = 0;
402 struct ip_mreqn mreqn;
403 struct sock *sk = ub->ubsock->sk;
404
405 if (ntohs(remote->proto) == ETH_P_IP) {
Erik Hugned0f91932015-03-05 10:23:49 +0100406 mreqn.imr_multiaddr = remote->ipv4;
407 mreqn.imr_ifindex = ub->ifindex;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300408 err = ip_mc_join_group(sk, &mreqn);
Marcelo Ricardo Leitner446981e2015-03-19 16:47:58 -0300409#if IS_ENABLED(CONFIG_IPV6)
Erik Hugned0f91932015-03-05 10:23:49 +0100410 } else {
Marcelo Ricardo Leitner446981e2015-03-19 16:47:58 -0300411 err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
412 &remote->ipv6);
413#endif
Erik Hugned0f91932015-03-05 10:23:49 +0100414 }
415 return err;
416}
417
Richard Alpefdb3acc2016-08-26 10:52:55 +0200418static int __tipc_nl_add_udp_addr(struct sk_buff *skb,
419 struct udp_media_addr *addr, int nla_t)
420{
421 if (ntohs(addr->proto) == ETH_P_IP) {
422 struct sockaddr_in ip4;
423
Dan Carpenter73076162016-10-13 11:06:06 +0300424 memset(&ip4, 0, sizeof(ip4));
Richard Alpefdb3acc2016-08-26 10:52:55 +0200425 ip4.sin_family = AF_INET;
426 ip4.sin_port = addr->port;
427 ip4.sin_addr.s_addr = addr->ipv4.s_addr;
428 if (nla_put(skb, nla_t, sizeof(ip4), &ip4))
429 return -EMSGSIZE;
430
431#if IS_ENABLED(CONFIG_IPV6)
432 } else if (ntohs(addr->proto) == ETH_P_IPV6) {
433 struct sockaddr_in6 ip6;
434
Dan Carpenter73076162016-10-13 11:06:06 +0300435 memset(&ip6, 0, sizeof(ip6));
Richard Alpefdb3acc2016-08-26 10:52:55 +0200436 ip6.sin6_family = AF_INET6;
437 ip6.sin6_port = addr->port;
438 memcpy(&ip6.sin6_addr, &addr->ipv6, sizeof(struct in6_addr));
439 if (nla_put(skb, nla_t, sizeof(ip6), &ip6))
440 return -EMSGSIZE;
441#endif
442 }
443
444 return 0;
445}
446
Richard Alpe832629c2016-08-26 10:52:56 +0200447int tipc_udp_nl_dump_remoteip(struct sk_buff *skb, struct netlink_callback *cb)
448{
449 u32 bid = cb->args[0];
450 u32 skip_cnt = cb->args[1];
451 u32 portid = NETLINK_CB(cb->skb).portid;
452 struct udp_replicast *rcast, *tmp;
453 struct tipc_bearer *b;
454 struct udp_bearer *ub;
455 void *hdr;
456 int err;
457 int i;
458
459 if (!bid && !skip_cnt) {
Jiri Pirko057af702019-10-05 20:04:39 +0200460 struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
Richard Alpe832629c2016-08-26 10:52:56 +0200461 struct net *net = sock_net(skb->sk);
462 struct nlattr *battrs[TIPC_NLA_BEARER_MAX + 1];
Richard Alpe832629c2016-08-26 10:52:56 +0200463 char *bname;
464
Richard Alpe832629c2016-08-26 10:52:56 +0200465 if (!attrs[TIPC_NLA_BEARER])
466 return -EINVAL;
467
Johannes Berg8cb08172019-04-26 14:07:28 +0200468 err = nla_parse_nested_deprecated(battrs, TIPC_NLA_BEARER_MAX,
469 attrs[TIPC_NLA_BEARER],
470 tipc_nl_bearer_policy, NULL);
Richard Alpe832629c2016-08-26 10:52:56 +0200471 if (err)
472 return err;
473
474 if (!battrs[TIPC_NLA_BEARER_NAME])
475 return -EINVAL;
476
477 bname = nla_data(battrs[TIPC_NLA_BEARER_NAME]);
478
479 rtnl_lock();
480 b = tipc_bearer_find(net, bname);
481 if (!b) {
482 rtnl_unlock();
483 return -EINVAL;
484 }
485 bid = b->identity;
486 } else {
487 struct net *net = sock_net(skb->sk);
488 struct tipc_net *tn = net_generic(net, tipc_net_id);
489
490 rtnl_lock();
491 b = rtnl_dereference(tn->bearer_list[bid]);
492 if (!b) {
493 rtnl_unlock();
494 return -EINVAL;
495 }
496 }
497
Xin Long30a46162019-07-02 00:54:55 +0800498 ub = rtnl_dereference(b->media_ptr);
Richard Alpe832629c2016-08-26 10:52:56 +0200499 if (!ub) {
500 rtnl_unlock();
501 return -EINVAL;
502 }
503
504 i = 0;
505 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
506 if (i < skip_cnt)
507 goto count;
508
509 hdr = genlmsg_put(skb, portid, cb->nlh->nlmsg_seq,
510 &tipc_genl_family, NLM_F_MULTI,
511 TIPC_NL_BEARER_GET);
512 if (!hdr)
513 goto done;
514
515 err = __tipc_nl_add_udp_addr(skb, &rcast->addr,
516 TIPC_NLA_UDP_REMOTE);
517 if (err) {
518 genlmsg_cancel(skb, hdr);
519 goto done;
520 }
521 genlmsg_end(skb, hdr);
522count:
523 i++;
524 }
525done:
526 rtnl_unlock();
527 cb->args[0] = bid;
528 cb->args[1] = i;
529
530 return skb->len;
531}
532
Richard Alpefdb3acc2016-08-26 10:52:55 +0200533int tipc_udp_nl_add_bearer_data(struct tipc_nl_msg *msg, struct tipc_bearer *b)
534{
535 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
536 struct udp_media_addr *dst;
537 struct udp_bearer *ub;
538 struct nlattr *nest;
539
Xin Long30a46162019-07-02 00:54:55 +0800540 ub = rtnl_dereference(b->media_ptr);
Richard Alpefdb3acc2016-08-26 10:52:55 +0200541 if (!ub)
542 return -ENODEV;
543
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200544 nest = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_UDP_OPTS);
Richard Alpefdb3acc2016-08-26 10:52:55 +0200545 if (!nest)
546 goto msg_full;
547
548 if (__tipc_nl_add_udp_addr(msg->skb, src, TIPC_NLA_UDP_LOCAL))
549 goto msg_full;
550
551 dst = (struct udp_media_addr *)&b->bcast_addr.value;
552 if (__tipc_nl_add_udp_addr(msg->skb, dst, TIPC_NLA_UDP_REMOTE))
553 goto msg_full;
554
555 if (!list_empty(&ub->rcast.list)) {
556 if (nla_put_flag(msg->skb, TIPC_NLA_UDP_MULTI_REMOTEIP))
557 goto msg_full;
558 }
559
560 nla_nest_end(msg->skb, nest);
561 return 0;
562msg_full:
563 nla_nest_cancel(msg->skb, nest);
564 return -EMSGSIZE;
565}
566
Erik Hugned0f91932015-03-05 10:23:49 +0100567/**
Richard Alpeba5aa842016-08-26 10:52:50 +0200568 * tipc_parse_udp_addr - build udp media address from netlink data
Andrew Lunnd8141202020-07-13 01:15:14 +0200569 * @nla: netlink attribute containing sockaddr storage aligned address
Richard Alpeba5aa842016-08-26 10:52:50 +0200570 * @addr: tipc media address to fill with address, port and protocol type
571 * @scope_id: IPv6 scope id pointer, not NULL indicates it's required
Erik Hugned0f91932015-03-05 10:23:49 +0100572 */
Richard Alpeba5aa842016-08-26 10:52:50 +0200573
574static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr,
575 u32 *scope_id)
Erik Hugned0f91932015-03-05 10:23:49 +0100576{
Richard Alpeba5aa842016-08-26 10:52:50 +0200577 struct sockaddr_storage sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100578
Richard Alpeba5aa842016-08-26 10:52:50 +0200579 nla_memcpy(&sa, nla, sizeof(sa));
580 if (sa.ss_family == AF_INET) {
581 struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100582
Richard Alpeba5aa842016-08-26 10:52:50 +0200583 addr->proto = htons(ETH_P_IP);
584 addr->port = ip4->sin_port;
585 addr->ipv4.s_addr = ip4->sin_addr.s_addr;
Erik Hugned0f91932015-03-05 10:23:49 +0100586 return 0;
587
588#if IS_ENABLED(CONFIG_IPV6)
Richard Alpeba5aa842016-08-26 10:52:50 +0200589 } else if (sa.ss_family == AF_INET6) {
590 struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100591
Richard Alpeba5aa842016-08-26 10:52:50 +0200592 addr->proto = htons(ETH_P_IPV6);
593 addr->port = ip6->sin6_port;
594 memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr));
Richard Alpe34f65db2016-03-03 14:20:43 +0100595
Richard Alpeba5aa842016-08-26 10:52:50 +0200596 /* Scope ID is only interesting for local addresses */
597 if (scope_id) {
598 int atype;
Erik Hugned0f91932015-03-05 10:23:49 +0100599
Richard Alpeba5aa842016-08-26 10:52:50 +0200600 atype = ipv6_addr_type(&ip6->sin6_addr);
601 if (__ipv6_addr_needs_scope_id(atype) &&
602 !ip6->sin6_scope_id) {
603 return -EINVAL;
604 }
605
606 *scope_id = ip6->sin6_scope_id ? : 0;
607 }
608
Erik Hugned0f91932015-03-05 10:23:49 +0100609 return 0;
610#endif
611 }
612 return -EADDRNOTAVAIL;
613}
614
Richard Alpeef20cd42016-08-26 10:52:53 +0200615int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr)
616{
617 int err;
618 struct udp_media_addr addr = {0};
619 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
620 struct udp_media_addr *dst;
621
Johannes Berg8cb08172019-04-26 14:07:28 +0200622 if (nla_parse_nested_deprecated(opts, TIPC_NLA_UDP_MAX, attr, tipc_nl_udp_policy, NULL))
Richard Alpeef20cd42016-08-26 10:52:53 +0200623 return -EINVAL;
624
625 if (!opts[TIPC_NLA_UDP_REMOTE])
626 return -EINVAL;
627
628 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL);
629 if (err)
630 return err;
631
632 dst = (struct udp_media_addr *)&b->bcast_addr.value;
633 if (tipc_udp_is_mcast_addr(dst)) {
634 pr_err("Can't add remote ip to TIPC UDP multicast bearer\n");
635 return -EINVAL;
636 }
637
Richard Alpec9b64d42016-08-26 10:52:54 +0200638 if (tipc_udp_is_known_peer(b, &addr))
639 return 0;
640
Richard Alpeef20cd42016-08-26 10:52:53 +0200641 return tipc_udp_rcast_add(b, &addr);
642}
643
Erik Hugned0f91932015-03-05 10:23:49 +0100644/**
645 * tipc_udp_enable - callback to create a new udp bearer instance
646 * @net: network namespace
647 * @b: pointer to generic tipc_bearer
648 * @attrs: netlink bearer configuration
649 *
650 * validate the bearer parameters and initialize the udp bearer
651 * rtnl_lock should be held
652 */
653static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
654 struct nlattr *attrs[])
655{
656 int err = -EINVAL;
657 struct udp_bearer *ub;
Richard Alpeef20cd42016-08-26 10:52:53 +0200658 struct udp_media_addr remote = {0};
Erik Hugned0f91932015-03-05 10:23:49 +0100659 struct udp_media_addr local = {0};
660 struct udp_port_cfg udp_conf = {0};
Erik Hugne4fee6be82015-03-09 10:19:31 +0100661 struct udp_tunnel_sock_cfg tuncfg = {NULL};
Richard Alpeba5aa842016-08-26 10:52:50 +0200662 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
Jon Maloy52dfae52018-03-22 20:42:52 +0100663 u8 node_id[NODE_ID_LEN] = {0,};
Xin Long4ef1a7c2020-08-17 14:30:49 +0800664 struct net_device *dev;
Hoang Leacad76a2018-10-11 08:43:08 +0700665 int rmcast = 0;
Erik Hugned0f91932015-03-05 10:23:49 +0100666
667 ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
668 if (!ub)
669 return -ENOMEM;
670
Richard Alpeef20cd42016-08-26 10:52:53 +0200671 INIT_LIST_HEAD(&ub->rcast.list);
672
Richard Alpeba5aa842016-08-26 10:52:50 +0200673 if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
674 goto err;
675
Johannes Berg8cb08172019-04-26 14:07:28 +0200676 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 +0200677 goto err;
678
679 if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) {
680 pr_err("Invalid UDP bearer configuration");
Wei Yongjunc20cb812016-09-10 00:56:55 +0000681 err = -EINVAL;
682 goto err;
Richard Alpeba5aa842016-08-26 10:52:50 +0200683 }
684
685 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local,
686 &ub->ifindex);
687 if (err)
688 goto err;
689
Richard Alpeef20cd42016-08-26 10:52:53 +0200690 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL);
Erik Hugned0f91932015-03-05 10:23:49 +0100691 if (err)
692 goto err;
693
Cong Wangfb83ed42018-12-10 15:23:30 -0800694 if (remote.proto != local.proto) {
695 err = -EINVAL;
696 goto err;
697 }
698
Hoang Leacad76a2018-10-11 08:43:08 +0700699 /* Checking remote ip address */
700 rmcast = tipc_udp_is_mcast_addr(&remote);
701
Jon Maloy52dfae52018-03-22 20:42:52 +0100702 /* Autoconfigure own node identity if needed */
703 if (!tipc_own_id(net)) {
704 memcpy(node_id, local.ipv6.in6_u.u6_addr8, 16);
705 tipc_net_init(net, node_id, 0);
706 }
707 if (!tipc_own_id(net)) {
708 pr_warn("Failed to set node id, please configure manually\n");
Wei Yongjunc76f2482018-03-26 14:32:44 +0000709 err = -EINVAL;
710 goto err;
Jon Maloy52dfae52018-03-22 20:42:52 +0100711 }
712
Erik Hugned0f91932015-03-05 10:23:49 +0100713 b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
Jon Paul Maloy99999742017-01-18 13:50:50 -0500714 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
Erik Hugned0f91932015-03-05 10:23:49 +0100715 rcu_assign_pointer(b->media_ptr, ub);
716 rcu_assign_pointer(ub->bearer, b);
717 tipc_udp_media_addr_set(&b->addr, &local);
Erik Hugne4fee6be82015-03-09 10:19:31 +0100718 if (local.proto == htons(ETH_P_IP)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100719 dev = __ip_dev_find(net, local.ipv4.s_addr, false);
720 if (!dev) {
721 err = -ENODEV;
722 goto err;
723 }
724 udp_conf.family = AF_INET;
Hoang Leacad76a2018-10-11 08:43:08 +0700725
726 /* Switch to use ANY to receive packets from group */
727 if (rmcast)
728 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
729 else
730 udp_conf.local_ip.s_addr = local.ipv4.s_addr;
Erik Hugned0f91932015-03-05 10:23:49 +0100731 udp_conf.use_udp_checksums = false;
732 ub->ifindex = dev->ifindex;
Michal Kubeček3de81b72016-12-02 09:33:41 +0100733 if (tipc_mtu_bad(dev, sizeof(struct iphdr) +
734 sizeof(struct udphdr))) {
735 err = -EINVAL;
736 goto err;
737 }
GhantaKrishnamurthy MohanKrishnaa4dfa722018-04-19 11:06:18 +0200738 b->mtu = b->media->mtu;
Erik Hugned0f91932015-03-05 10:23:49 +0100739#if IS_ENABLED(CONFIG_IPV6)
Erik Hugne4fee6be82015-03-09 10:19:31 +0100740 } else if (local.proto == htons(ETH_P_IPV6)) {
Xin Long4ef1a7c2020-08-17 14:30:49 +0800741 dev = ub->ifindex ? __dev_get_by_index(net, ub->ifindex) : NULL;
742 dev = ipv6_dev_find(net, &local.ipv6, dev);
Xin Long5a6f6f52020-08-03 23:34:47 +0800743 if (!dev) {
744 err = -ENODEV;
745 goto err;
746 }
Erik Hugned0f91932015-03-05 10:23:49 +0100747 udp_conf.family = AF_INET6;
748 udp_conf.use_udp6_tx_checksums = true;
749 udp_conf.use_udp6_rx_checksums = true;
Hoang Leacad76a2018-10-11 08:43:08 +0700750 if (rmcast)
751 udp_conf.local_ip6 = in6addr_any;
752 else
753 udp_conf.local_ip6 = local.ipv6;
Xin Long5a6f6f52020-08-03 23:34:47 +0800754 ub->ifindex = dev->ifindex;
Erik Hugned0f91932015-03-05 10:23:49 +0100755 b->mtu = 1280;
756#endif
757 } else {
758 err = -EAFNOSUPPORT;
759 goto err;
760 }
Richard Alpebc3a3342016-06-27 13:34:07 +0200761 udp_conf.local_udp_port = local.port;
Erik Hugned0f91932015-03-05 10:23:49 +0100762 err = udp_sock_create(net, &udp_conf, &ub->ubsock);
763 if (err)
764 goto err;
765 tuncfg.sk_user_data = ub;
766 tuncfg.encap_type = 1;
767 tuncfg.encap_rcv = tipc_udp_recv;
768 tuncfg.encap_destroy = NULL;
769 setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
770
Xin Longe9c1a792019-06-20 19:03:41 +0800771 err = dst_cache_init(&ub->rcast.dst_cache, GFP_ATOMIC);
772 if (err)
Xin Longd2c3a4b2019-07-02 00:57:19 +0800773 goto free;
Xin Longe9c1a792019-06-20 19:03:41 +0800774
Richard Alpeef20cd42016-08-26 10:52:53 +0200775 /**
776 * The bcast media address port is used for all peers and the ip
777 * is used if it's a multicast address.
778 */
779 memcpy(&b->bcast_addr.value, &remote, sizeof(remote));
Hoang Leacad76a2018-10-11 08:43:08 +0700780 if (rmcast)
Richard Alpeef20cd42016-08-26 10:52:53 +0200781 err = enable_mcast(ub, &remote);
782 else
783 err = tipc_udp_rcast_add(b, &remote);
784 if (err)
Xin Longd2c3a4b2019-07-02 00:57:19 +0800785 goto free;
Richard Alpe1ca73e32016-08-26 10:52:52 +0200786
Erik Hugned0f91932015-03-05 10:23:49 +0100787 return 0;
Xin Longd2c3a4b2019-07-02 00:57:19 +0800788
789free:
Xin Longe9c1a792019-06-20 19:03:41 +0800790 dst_cache_destroy(&ub->rcast.dst_cache);
Xin Longd2c3a4b2019-07-02 00:57:19 +0800791 udp_tunnel_sock_release(ub->ubsock);
792err:
Erik Hugned0f91932015-03-05 10:23:49 +0100793 kfree(ub);
794 return err;
795}
796
797/* cleanup_bearer - break the socket/bearer association */
798static void cleanup_bearer(struct work_struct *work)
799{
800 struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
Richard Alpeef20cd42016-08-26 10:52:53 +0200801 struct udp_replicast *rcast, *tmp;
802
803 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
Xin Longe9c1a792019-06-20 19:03:41 +0800804 dst_cache_destroy(&rcast->dst_cache);
Richard Alpeef20cd42016-08-26 10:52:53 +0200805 list_del_rcu(&rcast->list);
806 kfree_rcu(rcast, rcu);
807 }
Erik Hugned0f91932015-03-05 10:23:49 +0100808
Xin Long5195ec52021-05-17 02:28:58 +0800809 atomic_dec(&tipc_net(sock_net(ub->ubsock->sk))->wq_count);
Xin Longe9c1a792019-06-20 19:03:41 +0800810 dst_cache_destroy(&ub->rcast.dst_cache);
Xin Longd2c3a4b2019-07-02 00:57:19 +0800811 udp_tunnel_sock_release(ub->ubsock);
Erik Hugned0f91932015-03-05 10:23:49 +0100812 synchronize_net();
813 kfree(ub);
814}
815
816/* tipc_udp_disable - detach bearer from socket */
817static void tipc_udp_disable(struct tipc_bearer *b)
818{
819 struct udp_bearer *ub;
820
Xin Long30a46162019-07-02 00:54:55 +0800821 ub = rtnl_dereference(b->media_ptr);
Erik Hugned0f91932015-03-05 10:23:49 +0100822 if (!ub) {
823 pr_err("UDP bearer instance not found\n");
824 return;
825 }
Xin Longd2c3a4b2019-07-02 00:57:19 +0800826 sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
Erik Hugned0f91932015-03-05 10:23:49 +0100827 RCU_INIT_POINTER(ub->bearer, NULL);
828
829 /* sock_release need to be done outside of rtnl lock */
Xin Long5195ec52021-05-17 02:28:58 +0800830 atomic_inc(&tipc_net(sock_net(ub->ubsock->sk))->wq_count);
Erik Hugned0f91932015-03-05 10:23:49 +0100831 INIT_WORK(&ub->work, cleanup_bearer);
832 schedule_work(&ub->work);
833}
834
835struct tipc_media udp_media_info = {
836 .send_msg = tipc_udp_send_msg,
837 .enable_media = tipc_udp_enable,
838 .disable_media = tipc_udp_disable,
839 .addr2str = tipc_udp_addr2str,
840 .addr2msg = tipc_udp_addr2msg,
841 .msg2addr = tipc_udp_msg2addr,
842 .priority = TIPC_DEF_LINK_PRI,
843 .tolerance = TIPC_DEF_LINK_TOL,
Jon Maloy16ad3f42019-12-10 00:52:46 +0100844 .min_win = TIPC_DEF_LINK_WIN,
845 .max_win = TIPC_DEF_LINK_WIN,
GhantaKrishnamurthy MohanKrishnaa4dfa722018-04-19 11:06:18 +0200846 .mtu = TIPC_DEF_LINK_UDP_MTU,
Erik Hugned0f91932015-03-05 10:23:49 +0100847 .type_id = TIPC_MEDIA_TYPE_UDP,
848 .hwaddr_len = 0,
849 .name = "udp"
850};