blob: 9783101bc4a9ad5fb80cc014490bfa3da5ed1c8b [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>
Marcelo Ricardo Leitner446981e2015-03-19 16:47:58 -030047#include <net/addrconf.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;
79 struct rcu_head rcu;
80 struct list_head list;
81};
82
Erik Hugned0f91932015-03-05 10:23:49 +010083/**
84 * struct udp_bearer - ip/udp bearer data structure
85 * @bearer: associated generic tipc bearer
86 * @ubsock: bearer associated socket
87 * @ifindex: local address scope
88 * @work: used to schedule deferred work on a bearer
89 */
90struct udp_bearer {
91 struct tipc_bearer __rcu *bearer;
92 struct socket *ubsock;
93 u32 ifindex;
94 struct work_struct work;
Richard Alpeef20cd42016-08-26 10:52:53 +020095 struct udp_replicast rcast;
Erik Hugned0f91932015-03-05 10:23:49 +010096};
97
Richard Alpe1ca73e32016-08-26 10:52:52 +020098static int tipc_udp_is_mcast_addr(struct udp_media_addr *addr)
99{
100 if (ntohs(addr->proto) == ETH_P_IP)
101 return ipv4_is_multicast(addr->ipv4.s_addr);
102#if IS_ENABLED(CONFIG_IPV6)
103 else
104 return ipv6_addr_is_multicast(&addr->ipv6);
105#endif
106 return 0;
107}
108
Erik Hugned0f91932015-03-05 10:23:49 +0100109/* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
110static void tipc_udp_media_addr_set(struct tipc_media_addr *addr,
111 struct udp_media_addr *ua)
112{
113 memset(addr, 0, sizeof(struct tipc_media_addr));
114 addr->media_id = TIPC_MEDIA_TYPE_UDP;
115 memcpy(addr->value, ua, sizeof(struct udp_media_addr));
Richard Alpe1ca73e32016-08-26 10:52:52 +0200116
117 if (tipc_udp_is_mcast_addr(ua))
Jon Paul Maloy99999742017-01-18 13:50:50 -0500118 addr->broadcast = TIPC_BROADCAST_SUPPORT;
Erik Hugned0f91932015-03-05 10:23:49 +0100119}
120
121/* tipc_udp_addr2str - convert ip/udp address to string */
122static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size)
123{
124 struct udp_media_addr *ua = (struct udp_media_addr *)&a->value;
125
126 if (ntohs(ua->proto) == ETH_P_IP)
Richard Alpebc3a3342016-06-27 13:34:07 +0200127 snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port));
Erik Hugned0f91932015-03-05 10:23:49 +0100128 else if (ntohs(ua->proto) == ETH_P_IPV6)
Richard Alpebc3a3342016-06-27 13:34:07 +0200129 snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port));
Erik Hugned0f91932015-03-05 10:23:49 +0100130 else
131 pr_err("Invalid UDP media address\n");
132 return 0;
133}
134
135/* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
136static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a,
137 char *msg)
138{
139 struct udp_media_addr *ua;
140
141 ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET);
142 if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP)
143 return -EINVAL;
144 tipc_udp_media_addr_set(a, ua);
145 return 0;
146}
147
148/* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
149static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a)
150{
151 memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
152 msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP;
153 memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value,
154 sizeof(struct udp_media_addr));
155 return 0;
156}
157
158/* tipc_send_msg - enqueue a send request */
Richard Alpece984da2016-08-26 10:52:51 +0200159static int tipc_udp_xmit(struct net *net, struct sk_buff *skb,
160 struct udp_bearer *ub, struct udp_media_addr *src,
161 struct udp_media_addr *dst)
Erik Hugned0f91932015-03-05 10:23:49 +0100162{
163 int ttl, err = 0;
Erik Hugned0f91932015-03-05 10:23:49 +0100164 struct rtable *rt;
165
Erik Hugne4fee6be82015-03-09 10:19:31 +0100166 if (dst->proto == htons(ETH_P_IP)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100167 struct flowi4 fl = {
168 .daddr = dst->ipv4.s_addr,
169 .saddr = src->ipv4.s_addr,
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400170 .flowi4_mark = skb->mark,
Erik Hugned0f91932015-03-05 10:23:49 +0100171 .flowi4_proto = IPPROTO_UDP
172 };
173 rt = ip_route_output_key(net, &fl);
174 if (IS_ERR(rt)) {
175 err = PTR_ERR(rt);
176 goto tx_error;
177 }
Richard Alpe9b300962016-03-03 14:20:40 +0100178
179 skb->dev = rt->dst.dev;
Erik Hugned0f91932015-03-05 10:23:49 +0100180 ttl = ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar039f5062015-12-24 14:34:54 -0800181 udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr,
Richard Alpebc3a3342016-06-27 13:34:07 +0200182 dst->ipv4.s_addr, 0, ttl, 0, src->port,
183 dst->port, false, true);
Erik Hugned0f91932015-03-05 10:23:49 +0100184#if IS_ENABLED(CONFIG_IPV6)
185 } else {
186 struct dst_entry *ndst;
187 struct flowi6 fl6 = {
188 .flowi6_oif = ub->ifindex,
189 .daddr = dst->ipv6,
190 .saddr = src->ipv6,
191 .flowi6_proto = IPPROTO_UDP
192 };
Roopa Prabhu343d60a2015-07-30 13:34:53 -0700193 err = ipv6_stub->ipv6_dst_lookup(net, ub->ubsock->sk, &ndst,
194 &fl6);
Erik Hugned0f91932015-03-05 10:23:49 +0100195 if (err)
196 goto tx_error;
197 ttl = ip6_dst_hoplimit(ndst);
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400198 err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb,
David Miller79b16aa2015-04-05 22:19:09 -0400199 ndst->dev, &src->ipv6,
Richard Alpebc3a3342016-06-27 13:34:07 +0200200 &dst->ipv6, 0, ttl, 0, src->port,
201 dst->port, false);
Erik Hugned0f91932015-03-05 10:23:49 +0100202#endif
203 }
204 return err;
205
206tx_error:
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400207 kfree_skb(skb);
Erik Hugned0f91932015-03-05 10:23:49 +0100208 return err;
209}
210
Richard Alpece984da2016-08-26 10:52:51 +0200211static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
212 struct tipc_bearer *b,
213 struct tipc_media_addr *addr)
214{
215 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
216 struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value;
Richard Alpeef20cd42016-08-26 10:52:53 +0200217 struct udp_replicast *rcast;
Richard Alpece984da2016-08-26 10:52:51 +0200218 struct udp_bearer *ub;
219 int err = 0;
220
221 if (skb_headroom(skb) < UDP_MIN_HEADROOM) {
222 err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC);
223 if (err)
Richard Alpeef20cd42016-08-26 10:52:53 +0200224 goto out;
Richard Alpece984da2016-08-26 10:52:51 +0200225 }
226
227 skb_set_inner_protocol(skb, htons(ETH_P_TIPC));
228 ub = rcu_dereference_rtnl(b->media_ptr);
229 if (!ub) {
230 err = -ENODEV;
Richard Alpeef20cd42016-08-26 10:52:53 +0200231 goto out;
Richard Alpece984da2016-08-26 10:52:51 +0200232 }
233
Jon Paul Maloy99999742017-01-18 13:50:50 -0500234 if (addr->broadcast != TIPC_REPLICAST_SUPPORT)
Richard Alpeef20cd42016-08-26 10:52:53 +0200235 return tipc_udp_xmit(net, skb, ub, src, dst);
Richard Alpece984da2016-08-26 10:52:51 +0200236
Richard Alpeef20cd42016-08-26 10:52:53 +0200237 /* Replicast, send an skb to each configured IP address */
238 list_for_each_entry_rcu(rcast, &ub->rcast.list, list) {
239 struct sk_buff *_skb;
240
241 _skb = pskb_copy(skb, GFP_ATOMIC);
242 if (!_skb) {
243 err = -ENOMEM;
244 goto out;
245 }
246
247 err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr);
248 if (err) {
249 kfree_skb(_skb);
250 goto out;
251 }
252 }
253 err = 0;
254out:
Richard Alpece984da2016-08-26 10:52:51 +0200255 kfree_skb(skb);
256 return err;
257}
258
Richard Alpec9b64d42016-08-26 10:52:54 +0200259static bool tipc_udp_is_known_peer(struct tipc_bearer *b,
260 struct udp_media_addr *addr)
261{
262 struct udp_replicast *rcast, *tmp;
263 struct udp_bearer *ub;
264
265 ub = rcu_dereference_rtnl(b->media_ptr);
266 if (!ub) {
267 pr_err_ratelimited("UDP bearer instance not found\n");
268 return false;
269 }
270
271 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
272 if (!memcmp(&rcast->addr, addr, sizeof(struct udp_media_addr)))
273 return true;
274 }
275
276 return false;
277}
278
Richard Alpeef20cd42016-08-26 10:52:53 +0200279static int tipc_udp_rcast_add(struct tipc_bearer *b,
280 struct udp_media_addr *addr)
281{
282 struct udp_replicast *rcast;
283 struct udp_bearer *ub;
284
285 ub = rcu_dereference_rtnl(b->media_ptr);
286 if (!ub)
287 return -ENODEV;
288
289 rcast = kmalloc(sizeof(*rcast), GFP_ATOMIC);
290 if (!rcast)
291 return -ENOMEM;
292
293 memcpy(&rcast->addr, addr, sizeof(struct udp_media_addr));
294
295 if (ntohs(addr->proto) == ETH_P_IP)
296 pr_info("New replicast peer: %pI4\n", &rcast->addr.ipv4);
297#if IS_ENABLED(CONFIG_IPV6)
298 else if (ntohs(addr->proto) == ETH_P_IPV6)
299 pr_info("New replicast peer: %pI6\n", &rcast->addr.ipv6);
300#endif
Jon Paul Maloy99999742017-01-18 13:50:50 -0500301 b->bcast_addr.broadcast = TIPC_REPLICAST_SUPPORT;
Richard Alpeef20cd42016-08-26 10:52:53 +0200302 list_add_rcu(&rcast->list, &ub->rcast.list);
303 return 0;
304}
305
Richard Alpec9b64d42016-08-26 10:52:54 +0200306static int tipc_udp_rcast_disc(struct tipc_bearer *b, struct sk_buff *skb)
307{
308 struct udp_media_addr src = {0};
309 struct udp_media_addr *dst;
310
311 dst = (struct udp_media_addr *)&b->bcast_addr.value;
312 if (tipc_udp_is_mcast_addr(dst))
313 return 0;
314
315 src.port = udp_hdr(skb)->source;
316
317 if (ip_hdr(skb)->version == 4) {
318 struct iphdr *iphdr = ip_hdr(skb);
319
320 src.proto = htons(ETH_P_IP);
321 src.ipv4.s_addr = iphdr->saddr;
322 if (ipv4_is_multicast(iphdr->daddr))
323 return 0;
324#if IS_ENABLED(CONFIG_IPV6)
325 } else if (ip_hdr(skb)->version == 6) {
326 struct ipv6hdr *iphdr = ipv6_hdr(skb);
327
328 src.proto = htons(ETH_P_IPV6);
329 src.ipv6 = iphdr->saddr;
330 if (ipv6_addr_is_multicast(&iphdr->daddr))
331 return 0;
332#endif
333 } else {
334 return 0;
335 }
336
337 if (likely(tipc_udp_is_known_peer(b, &src)))
338 return 0;
339
340 return tipc_udp_rcast_add(b, &src);
341}
342
Erik Hugned0f91932015-03-05 10:23:49 +0100343/* tipc_udp_recv - read data from bearer socket */
344static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
345{
346 struct udp_bearer *ub;
347 struct tipc_bearer *b;
Richard Alpec9b64d42016-08-26 10:52:54 +0200348 struct tipc_msg *hdr;
349 int err;
Erik Hugned0f91932015-03-05 10:23:49 +0100350
351 ub = rcu_dereference_sk_user_data(sk);
352 if (!ub) {
353 pr_err_ratelimited("Failed to get UDP bearer reference");
Richard Alpec9b64d42016-08-26 10:52:54 +0200354 goto out;
Erik Hugned0f91932015-03-05 10:23:49 +0100355 }
Erik Hugned0f91932015-03-05 10:23:49 +0100356 skb_pull(skb, sizeof(struct udphdr));
Richard Alpec9b64d42016-08-26 10:52:54 +0200357 hdr = buf_msg(skb);
358
Erik Hugned0f91932015-03-05 10:23:49 +0100359 rcu_read_lock();
360 b = rcu_dereference_rtnl(ub->bearer);
Richard Alpec9b64d42016-08-26 10:52:54 +0200361 if (!b)
362 goto rcu_out;
Erik Hugned0f91932015-03-05 10:23:49 +0100363
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400364 if (b && test_bit(0, &b->up)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100365 tipc_rcv(sock_net(sk), skb, b);
366 rcu_read_unlock();
367 return 0;
368 }
Richard Alpec9b64d42016-08-26 10:52:54 +0200369
370 if (unlikely(msg_user(hdr) == LINK_CONFIG)) {
371 err = tipc_udp_rcast_disc(b, skb);
372 if (err)
373 goto rcu_out;
374 }
375
Richard Alpec9b64d42016-08-26 10:52:54 +0200376rcu_out:
377 rcu_read_unlock();
378out:
Erik Hugned0f91932015-03-05 10:23:49 +0100379 kfree_skb(skb);
380 return 0;
381}
382
383static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
384{
385 int err = 0;
386 struct ip_mreqn mreqn;
387 struct sock *sk = ub->ubsock->sk;
388
389 if (ntohs(remote->proto) == ETH_P_IP) {
Erik Hugned0f91932015-03-05 10:23:49 +0100390 mreqn.imr_multiaddr = remote->ipv4;
391 mreqn.imr_ifindex = ub->ifindex;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300392 err = ip_mc_join_group(sk, &mreqn);
Marcelo Ricardo Leitner446981e2015-03-19 16:47:58 -0300393#if IS_ENABLED(CONFIG_IPV6)
Erik Hugned0f91932015-03-05 10:23:49 +0100394 } else {
Marcelo Ricardo Leitner446981e2015-03-19 16:47:58 -0300395 err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
396 &remote->ipv6);
397#endif
Erik Hugned0f91932015-03-05 10:23:49 +0100398 }
399 return err;
400}
401
Richard Alpefdb3acc2016-08-26 10:52:55 +0200402static int __tipc_nl_add_udp_addr(struct sk_buff *skb,
403 struct udp_media_addr *addr, int nla_t)
404{
405 if (ntohs(addr->proto) == ETH_P_IP) {
406 struct sockaddr_in ip4;
407
Dan Carpenter73076162016-10-13 11:06:06 +0300408 memset(&ip4, 0, sizeof(ip4));
Richard Alpefdb3acc2016-08-26 10:52:55 +0200409 ip4.sin_family = AF_INET;
410 ip4.sin_port = addr->port;
411 ip4.sin_addr.s_addr = addr->ipv4.s_addr;
412 if (nla_put(skb, nla_t, sizeof(ip4), &ip4))
413 return -EMSGSIZE;
414
415#if IS_ENABLED(CONFIG_IPV6)
416 } else if (ntohs(addr->proto) == ETH_P_IPV6) {
417 struct sockaddr_in6 ip6;
418
Dan Carpenter73076162016-10-13 11:06:06 +0300419 memset(&ip6, 0, sizeof(ip6));
Richard Alpefdb3acc2016-08-26 10:52:55 +0200420 ip6.sin6_family = AF_INET6;
421 ip6.sin6_port = addr->port;
422 memcpy(&ip6.sin6_addr, &addr->ipv6, sizeof(struct in6_addr));
423 if (nla_put(skb, nla_t, sizeof(ip6), &ip6))
424 return -EMSGSIZE;
425#endif
426 }
427
428 return 0;
429}
430
Richard Alpe832629c2016-08-26 10:52:56 +0200431int tipc_udp_nl_dump_remoteip(struct sk_buff *skb, struct netlink_callback *cb)
432{
433 u32 bid = cb->args[0];
434 u32 skip_cnt = cb->args[1];
435 u32 portid = NETLINK_CB(cb->skb).portid;
436 struct udp_replicast *rcast, *tmp;
437 struct tipc_bearer *b;
438 struct udp_bearer *ub;
439 void *hdr;
440 int err;
441 int i;
442
443 if (!bid && !skip_cnt) {
444 struct net *net = sock_net(skb->sk);
445 struct nlattr *battrs[TIPC_NLA_BEARER_MAX + 1];
446 struct nlattr **attrs;
447 char *bname;
448
449 err = tipc_nlmsg_parse(cb->nlh, &attrs);
450 if (err)
451 return err;
452
453 if (!attrs[TIPC_NLA_BEARER])
454 return -EINVAL;
455
456 err = nla_parse_nested(battrs, TIPC_NLA_BEARER_MAX,
457 attrs[TIPC_NLA_BEARER],
Johannes Bergfceb6432017-04-12 14:34:07 +0200458 tipc_nl_bearer_policy, NULL);
Richard Alpe832629c2016-08-26 10:52:56 +0200459 if (err)
460 return err;
461
462 if (!battrs[TIPC_NLA_BEARER_NAME])
463 return -EINVAL;
464
465 bname = nla_data(battrs[TIPC_NLA_BEARER_NAME]);
466
467 rtnl_lock();
468 b = tipc_bearer_find(net, bname);
469 if (!b) {
470 rtnl_unlock();
471 return -EINVAL;
472 }
473 bid = b->identity;
474 } else {
475 struct net *net = sock_net(skb->sk);
476 struct tipc_net *tn = net_generic(net, tipc_net_id);
477
478 rtnl_lock();
479 b = rtnl_dereference(tn->bearer_list[bid]);
480 if (!b) {
481 rtnl_unlock();
482 return -EINVAL;
483 }
484 }
485
486 ub = rcu_dereference_rtnl(b->media_ptr);
487 if (!ub) {
488 rtnl_unlock();
489 return -EINVAL;
490 }
491
492 i = 0;
493 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
494 if (i < skip_cnt)
495 goto count;
496
497 hdr = genlmsg_put(skb, portid, cb->nlh->nlmsg_seq,
498 &tipc_genl_family, NLM_F_MULTI,
499 TIPC_NL_BEARER_GET);
500 if (!hdr)
501 goto done;
502
503 err = __tipc_nl_add_udp_addr(skb, &rcast->addr,
504 TIPC_NLA_UDP_REMOTE);
505 if (err) {
506 genlmsg_cancel(skb, hdr);
507 goto done;
508 }
509 genlmsg_end(skb, hdr);
510count:
511 i++;
512 }
513done:
514 rtnl_unlock();
515 cb->args[0] = bid;
516 cb->args[1] = i;
517
518 return skb->len;
519}
520
Richard Alpefdb3acc2016-08-26 10:52:55 +0200521int tipc_udp_nl_add_bearer_data(struct tipc_nl_msg *msg, struct tipc_bearer *b)
522{
523 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
524 struct udp_media_addr *dst;
525 struct udp_bearer *ub;
526 struct nlattr *nest;
527
528 ub = rcu_dereference_rtnl(b->media_ptr);
529 if (!ub)
530 return -ENODEV;
531
532 nest = nla_nest_start(msg->skb, TIPC_NLA_BEARER_UDP_OPTS);
533 if (!nest)
534 goto msg_full;
535
536 if (__tipc_nl_add_udp_addr(msg->skb, src, TIPC_NLA_UDP_LOCAL))
537 goto msg_full;
538
539 dst = (struct udp_media_addr *)&b->bcast_addr.value;
540 if (__tipc_nl_add_udp_addr(msg->skb, dst, TIPC_NLA_UDP_REMOTE))
541 goto msg_full;
542
543 if (!list_empty(&ub->rcast.list)) {
544 if (nla_put_flag(msg->skb, TIPC_NLA_UDP_MULTI_REMOTEIP))
545 goto msg_full;
546 }
547
548 nla_nest_end(msg->skb, nest);
549 return 0;
550msg_full:
551 nla_nest_cancel(msg->skb, nest);
552 return -EMSGSIZE;
553}
554
Erik Hugned0f91932015-03-05 10:23:49 +0100555/**
Richard Alpeba5aa842016-08-26 10:52:50 +0200556 * tipc_parse_udp_addr - build udp media address from netlink data
557 * @nlattr: netlink attribute containing sockaddr storage aligned address
558 * @addr: tipc media address to fill with address, port and protocol type
559 * @scope_id: IPv6 scope id pointer, not NULL indicates it's required
Erik Hugned0f91932015-03-05 10:23:49 +0100560 */
Richard Alpeba5aa842016-08-26 10:52:50 +0200561
562static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr,
563 u32 *scope_id)
Erik Hugned0f91932015-03-05 10:23:49 +0100564{
Richard Alpeba5aa842016-08-26 10:52:50 +0200565 struct sockaddr_storage sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100566
Richard Alpeba5aa842016-08-26 10:52:50 +0200567 nla_memcpy(&sa, nla, sizeof(sa));
568 if (sa.ss_family == AF_INET) {
569 struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100570
Richard Alpeba5aa842016-08-26 10:52:50 +0200571 addr->proto = htons(ETH_P_IP);
572 addr->port = ip4->sin_port;
573 addr->ipv4.s_addr = ip4->sin_addr.s_addr;
Erik Hugned0f91932015-03-05 10:23:49 +0100574 return 0;
575
576#if IS_ENABLED(CONFIG_IPV6)
Richard Alpeba5aa842016-08-26 10:52:50 +0200577 } else if (sa.ss_family == AF_INET6) {
578 struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100579
Richard Alpeba5aa842016-08-26 10:52:50 +0200580 addr->proto = htons(ETH_P_IPV6);
581 addr->port = ip6->sin6_port;
582 memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr));
Richard Alpe34f65db2016-03-03 14:20:43 +0100583
Richard Alpeba5aa842016-08-26 10:52:50 +0200584 /* Scope ID is only interesting for local addresses */
585 if (scope_id) {
586 int atype;
Erik Hugned0f91932015-03-05 10:23:49 +0100587
Richard Alpeba5aa842016-08-26 10:52:50 +0200588 atype = ipv6_addr_type(&ip6->sin6_addr);
589 if (__ipv6_addr_needs_scope_id(atype) &&
590 !ip6->sin6_scope_id) {
591 return -EINVAL;
592 }
593
594 *scope_id = ip6->sin6_scope_id ? : 0;
595 }
596
Erik Hugned0f91932015-03-05 10:23:49 +0100597 return 0;
598#endif
599 }
600 return -EADDRNOTAVAIL;
601}
602
Richard Alpeef20cd42016-08-26 10:52:53 +0200603int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr)
604{
605 int err;
606 struct udp_media_addr addr = {0};
607 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
608 struct udp_media_addr *dst;
609
Johannes Bergfceb6432017-04-12 14:34:07 +0200610 if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX, attr,
611 tipc_nl_udp_policy, NULL))
Richard Alpeef20cd42016-08-26 10:52:53 +0200612 return -EINVAL;
613
614 if (!opts[TIPC_NLA_UDP_REMOTE])
615 return -EINVAL;
616
617 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL);
618 if (err)
619 return err;
620
621 dst = (struct udp_media_addr *)&b->bcast_addr.value;
622 if (tipc_udp_is_mcast_addr(dst)) {
623 pr_err("Can't add remote ip to TIPC UDP multicast bearer\n");
624 return -EINVAL;
625 }
626
Richard Alpec9b64d42016-08-26 10:52:54 +0200627 if (tipc_udp_is_known_peer(b, &addr))
628 return 0;
629
Richard Alpeef20cd42016-08-26 10:52:53 +0200630 return tipc_udp_rcast_add(b, &addr);
631}
632
Erik Hugned0f91932015-03-05 10:23:49 +0100633/**
634 * tipc_udp_enable - callback to create a new udp bearer instance
635 * @net: network namespace
636 * @b: pointer to generic tipc_bearer
637 * @attrs: netlink bearer configuration
638 *
639 * validate the bearer parameters and initialize the udp bearer
640 * rtnl_lock should be held
641 */
642static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
643 struct nlattr *attrs[])
644{
645 int err = -EINVAL;
646 struct udp_bearer *ub;
Richard Alpeef20cd42016-08-26 10:52:53 +0200647 struct udp_media_addr remote = {0};
Erik Hugned0f91932015-03-05 10:23:49 +0100648 struct udp_media_addr local = {0};
649 struct udp_port_cfg udp_conf = {0};
Erik Hugne4fee6be82015-03-09 10:19:31 +0100650 struct udp_tunnel_sock_cfg tuncfg = {NULL};
Richard Alpeba5aa842016-08-26 10:52:50 +0200651 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
Jon Maloy52dfae52018-03-22 20:42:52 +0100652 u8 node_id[NODE_ID_LEN] = {0,};
Erik Hugned0f91932015-03-05 10:23:49 +0100653
654 ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
655 if (!ub)
656 return -ENOMEM;
657
Richard Alpeef20cd42016-08-26 10:52:53 +0200658 INIT_LIST_HEAD(&ub->rcast.list);
659
Richard Alpeba5aa842016-08-26 10:52:50 +0200660 if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
661 goto err;
662
663 if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX,
664 attrs[TIPC_NLA_BEARER_UDP_OPTS],
Johannes Bergfceb6432017-04-12 14:34:07 +0200665 tipc_nl_udp_policy, NULL))
Richard Alpeba5aa842016-08-26 10:52:50 +0200666 goto err;
667
668 if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) {
669 pr_err("Invalid UDP bearer configuration");
Wei Yongjunc20cb812016-09-10 00:56:55 +0000670 err = -EINVAL;
671 goto err;
Richard Alpeba5aa842016-08-26 10:52:50 +0200672 }
673
674 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local,
675 &ub->ifindex);
676 if (err)
677 goto err;
678
Richard Alpeef20cd42016-08-26 10:52:53 +0200679 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL);
Erik Hugned0f91932015-03-05 10:23:49 +0100680 if (err)
681 goto err;
682
Jon Maloy52dfae52018-03-22 20:42:52 +0100683 /* Autoconfigure own node identity if needed */
684 if (!tipc_own_id(net)) {
685 memcpy(node_id, local.ipv6.in6_u.u6_addr8, 16);
686 tipc_net_init(net, node_id, 0);
687 }
688 if (!tipc_own_id(net)) {
689 pr_warn("Failed to set node id, please configure manually\n");
Wei Yongjunc76f2482018-03-26 14:32:44 +0000690 err = -EINVAL;
691 goto err;
Jon Maloy52dfae52018-03-22 20:42:52 +0100692 }
693
Erik Hugned0f91932015-03-05 10:23:49 +0100694 b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
Jon Paul Maloy99999742017-01-18 13:50:50 -0500695 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
Erik Hugned0f91932015-03-05 10:23:49 +0100696 rcu_assign_pointer(b->media_ptr, ub);
697 rcu_assign_pointer(ub->bearer, b);
698 tipc_udp_media_addr_set(&b->addr, &local);
Erik Hugne4fee6be82015-03-09 10:19:31 +0100699 if (local.proto == htons(ETH_P_IP)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100700 struct net_device *dev;
701
702 dev = __ip_dev_find(net, local.ipv4.s_addr, false);
703 if (!dev) {
704 err = -ENODEV;
705 goto err;
706 }
707 udp_conf.family = AF_INET;
708 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
709 udp_conf.use_udp_checksums = false;
710 ub->ifindex = dev->ifindex;
Michal Kubeček3de81b72016-12-02 09:33:41 +0100711 if (tipc_mtu_bad(dev, sizeof(struct iphdr) +
712 sizeof(struct udphdr))) {
713 err = -EINVAL;
714 goto err;
715 }
GhantaKrishnamurthy MohanKrishnaa4dfa722018-04-19 11:06:18 +0200716 b->mtu = b->media->mtu;
Erik Hugned0f91932015-03-05 10:23:49 +0100717#if IS_ENABLED(CONFIG_IPV6)
Erik Hugne4fee6be82015-03-09 10:19:31 +0100718 } else if (local.proto == htons(ETH_P_IPV6)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100719 udp_conf.family = AF_INET6;
720 udp_conf.use_udp6_tx_checksums = true;
721 udp_conf.use_udp6_rx_checksums = true;
722 udp_conf.local_ip6 = in6addr_any;
723 b->mtu = 1280;
724#endif
725 } else {
726 err = -EAFNOSUPPORT;
727 goto err;
728 }
Richard Alpebc3a3342016-06-27 13:34:07 +0200729 udp_conf.local_udp_port = local.port;
Erik Hugned0f91932015-03-05 10:23:49 +0100730 err = udp_sock_create(net, &udp_conf, &ub->ubsock);
731 if (err)
732 goto err;
733 tuncfg.sk_user_data = ub;
734 tuncfg.encap_type = 1;
735 tuncfg.encap_rcv = tipc_udp_recv;
736 tuncfg.encap_destroy = NULL;
737 setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
738
Richard Alpeef20cd42016-08-26 10:52:53 +0200739 /**
740 * The bcast media address port is used for all peers and the ip
741 * is used if it's a multicast address.
742 */
743 memcpy(&b->bcast_addr.value, &remote, sizeof(remote));
744 if (tipc_udp_is_mcast_addr(&remote))
745 err = enable_mcast(ub, &remote);
746 else
747 err = tipc_udp_rcast_add(b, &remote);
748 if (err)
749 goto err;
Richard Alpe1ca73e32016-08-26 10:52:52 +0200750
Erik Hugned0f91932015-03-05 10:23:49 +0100751 return 0;
752err:
Wei Yongjuna5de1252016-08-24 13:32:19 +0000753 if (ub->ubsock)
754 udp_tunnel_sock_release(ub->ubsock);
Erik Hugned0f91932015-03-05 10:23:49 +0100755 kfree(ub);
756 return err;
757}
758
759/* cleanup_bearer - break the socket/bearer association */
760static void cleanup_bearer(struct work_struct *work)
761{
762 struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
Richard Alpeef20cd42016-08-26 10:52:53 +0200763 struct udp_replicast *rcast, *tmp;
764
765 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
766 list_del_rcu(&rcast->list);
767 kfree_rcu(rcast, rcu);
768 }
Erik Hugned0f91932015-03-05 10:23:49 +0100769
770 if (ub->ubsock)
771 udp_tunnel_sock_release(ub->ubsock);
772 synchronize_net();
773 kfree(ub);
774}
775
776/* tipc_udp_disable - detach bearer from socket */
777static void tipc_udp_disable(struct tipc_bearer *b)
778{
779 struct udp_bearer *ub;
780
781 ub = rcu_dereference_rtnl(b->media_ptr);
782 if (!ub) {
783 pr_err("UDP bearer instance not found\n");
784 return;
785 }
786 if (ub->ubsock)
787 sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
Erik Hugned0f91932015-03-05 10:23:49 +0100788 RCU_INIT_POINTER(ub->bearer, NULL);
789
790 /* sock_release need to be done outside of rtnl lock */
791 INIT_WORK(&ub->work, cleanup_bearer);
792 schedule_work(&ub->work);
793}
794
795struct tipc_media udp_media_info = {
796 .send_msg = tipc_udp_send_msg,
797 .enable_media = tipc_udp_enable,
798 .disable_media = tipc_udp_disable,
799 .addr2str = tipc_udp_addr2str,
800 .addr2msg = tipc_udp_addr2msg,
801 .msg2addr = tipc_udp_msg2addr,
802 .priority = TIPC_DEF_LINK_PRI,
803 .tolerance = TIPC_DEF_LINK_TOL,
804 .window = TIPC_DEF_LINK_WIN,
GhantaKrishnamurthy MohanKrishnaa4dfa722018-04-19 11:06:18 +0200805 .mtu = TIPC_DEF_LINK_UDP_MTU,
Erik Hugned0f91932015-03-05 10:23:49 +0100806 .type_id = TIPC_MEDIA_TYPE_UDP,
807 .hwaddr_len = 0,
808 .name = "udp"
809};