blob: 3deabcab4882165b668f65319a3555027bf3b292 [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"
50#include "bearer.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010051#include "netlink.h"
Richard Alpeef20cd42016-08-26 10:52:53 +020052#include "msg.h"
Erik Hugned0f91932015-03-05 10:23:49 +010053
54/* IANA assigned UDP port */
55#define UDP_PORT_DEFAULT 6118
56
Richard Alpe9bd160b2016-03-14 09:43:52 +010057#define UDP_MIN_HEADROOM 48
Jon Paul Maloye5356792015-10-19 11:43:11 -040058
Erik Hugned0f91932015-03-05 10:23:49 +010059/**
60 * struct udp_media_addr - IP/UDP addressing information
61 *
62 * This is the bearer level originating address used in neighbor discovery
63 * messages, and all fields should be in network byte order
64 */
65struct udp_media_addr {
66 __be16 proto;
Richard Alpebc3a3342016-06-27 13:34:07 +020067 __be16 port;
Erik Hugned0f91932015-03-05 10:23:49 +010068 union {
69 struct in_addr ipv4;
70 struct in6_addr ipv6;
71 };
72};
73
Richard Alpeef20cd42016-08-26 10:52:53 +020074/* struct udp_replicast - container for UDP remote addresses */
75struct udp_replicast {
76 struct udp_media_addr addr;
77 struct rcu_head rcu;
78 struct list_head list;
79};
80
Erik Hugned0f91932015-03-05 10:23:49 +010081/**
82 * struct udp_bearer - ip/udp bearer data structure
83 * @bearer: associated generic tipc bearer
84 * @ubsock: bearer associated socket
85 * @ifindex: local address scope
86 * @work: used to schedule deferred work on a bearer
87 */
88struct udp_bearer {
89 struct tipc_bearer __rcu *bearer;
90 struct socket *ubsock;
91 u32 ifindex;
92 struct work_struct work;
Richard Alpeef20cd42016-08-26 10:52:53 +020093 struct udp_replicast rcast;
Erik Hugned0f91932015-03-05 10:23:49 +010094};
95
Richard Alpe1ca73e32016-08-26 10:52:52 +020096static int tipc_udp_is_mcast_addr(struct udp_media_addr *addr)
97{
98 if (ntohs(addr->proto) == ETH_P_IP)
99 return ipv4_is_multicast(addr->ipv4.s_addr);
100#if IS_ENABLED(CONFIG_IPV6)
101 else
102 return ipv6_addr_is_multicast(&addr->ipv6);
103#endif
104 return 0;
105}
106
Erik Hugned0f91932015-03-05 10:23:49 +0100107/* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
108static void tipc_udp_media_addr_set(struct tipc_media_addr *addr,
109 struct udp_media_addr *ua)
110{
111 memset(addr, 0, sizeof(struct tipc_media_addr));
112 addr->media_id = TIPC_MEDIA_TYPE_UDP;
113 memcpy(addr->value, ua, sizeof(struct udp_media_addr));
Richard Alpe1ca73e32016-08-26 10:52:52 +0200114
115 if (tipc_udp_is_mcast_addr(ua))
Jon Paul Maloy99999742017-01-18 13:50:50 -0500116 addr->broadcast = TIPC_BROADCAST_SUPPORT;
Erik Hugned0f91932015-03-05 10:23:49 +0100117}
118
119/* tipc_udp_addr2str - convert ip/udp address to string */
120static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size)
121{
122 struct udp_media_addr *ua = (struct udp_media_addr *)&a->value;
123
124 if (ntohs(ua->proto) == ETH_P_IP)
Richard Alpebc3a3342016-06-27 13:34:07 +0200125 snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port));
Erik Hugned0f91932015-03-05 10:23:49 +0100126 else if (ntohs(ua->proto) == ETH_P_IPV6)
Richard Alpebc3a3342016-06-27 13:34:07 +0200127 snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port));
Erik Hugned0f91932015-03-05 10:23:49 +0100128 else
129 pr_err("Invalid UDP media address\n");
130 return 0;
131}
132
133/* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
134static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a,
135 char *msg)
136{
137 struct udp_media_addr *ua;
138
139 ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET);
140 if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP)
141 return -EINVAL;
142 tipc_udp_media_addr_set(a, ua);
143 return 0;
144}
145
146/* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
147static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a)
148{
149 memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
150 msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP;
151 memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value,
152 sizeof(struct udp_media_addr));
153 return 0;
154}
155
156/* tipc_send_msg - enqueue a send request */
Richard Alpece984da2016-08-26 10:52:51 +0200157static int tipc_udp_xmit(struct net *net, struct sk_buff *skb,
158 struct udp_bearer *ub, struct udp_media_addr *src,
159 struct udp_media_addr *dst)
Erik Hugned0f91932015-03-05 10:23:49 +0100160{
161 int ttl, err = 0;
Erik Hugned0f91932015-03-05 10:23:49 +0100162 struct rtable *rt;
163
Erik Hugne4fee6be82015-03-09 10:19:31 +0100164 if (dst->proto == htons(ETH_P_IP)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100165 struct flowi4 fl = {
166 .daddr = dst->ipv4.s_addr,
167 .saddr = src->ipv4.s_addr,
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400168 .flowi4_mark = skb->mark,
Erik Hugned0f91932015-03-05 10:23:49 +0100169 .flowi4_proto = IPPROTO_UDP
170 };
171 rt = ip_route_output_key(net, &fl);
172 if (IS_ERR(rt)) {
173 err = PTR_ERR(rt);
174 goto tx_error;
175 }
Richard Alpe9b300962016-03-03 14:20:40 +0100176
177 skb->dev = rt->dst.dev;
Erik Hugned0f91932015-03-05 10:23:49 +0100178 ttl = ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar039f5062015-12-24 14:34:54 -0800179 udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr,
Richard Alpebc3a3342016-06-27 13:34:07 +0200180 dst->ipv4.s_addr, 0, ttl, 0, src->port,
181 dst->port, false, true);
Erik Hugned0f91932015-03-05 10:23:49 +0100182#if IS_ENABLED(CONFIG_IPV6)
183 } else {
184 struct dst_entry *ndst;
185 struct flowi6 fl6 = {
186 .flowi6_oif = ub->ifindex,
187 .daddr = dst->ipv6,
188 .saddr = src->ipv6,
189 .flowi6_proto = IPPROTO_UDP
190 };
Roopa Prabhu343d60a2015-07-30 13:34:53 -0700191 err = ipv6_stub->ipv6_dst_lookup(net, ub->ubsock->sk, &ndst,
192 &fl6);
Erik Hugned0f91932015-03-05 10:23:49 +0100193 if (err)
194 goto tx_error;
195 ttl = ip6_dst_hoplimit(ndst);
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400196 err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb,
David Miller79b16aa2015-04-05 22:19:09 -0400197 ndst->dev, &src->ipv6,
Richard Alpebc3a3342016-06-27 13:34:07 +0200198 &dst->ipv6, 0, ttl, 0, src->port,
199 dst->port, false);
Erik Hugned0f91932015-03-05 10:23:49 +0100200#endif
201 }
202 return err;
203
204tx_error:
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400205 kfree_skb(skb);
Erik Hugned0f91932015-03-05 10:23:49 +0100206 return err;
207}
208
Richard Alpece984da2016-08-26 10:52:51 +0200209static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
210 struct tipc_bearer *b,
211 struct tipc_media_addr *addr)
212{
213 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
214 struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value;
Richard Alpeef20cd42016-08-26 10:52:53 +0200215 struct udp_replicast *rcast;
Richard Alpece984da2016-08-26 10:52:51 +0200216 struct udp_bearer *ub;
217 int err = 0;
218
219 if (skb_headroom(skb) < UDP_MIN_HEADROOM) {
220 err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC);
221 if (err)
Richard Alpeef20cd42016-08-26 10:52:53 +0200222 goto out;
Richard Alpece984da2016-08-26 10:52:51 +0200223 }
224
225 skb_set_inner_protocol(skb, htons(ETH_P_TIPC));
226 ub = rcu_dereference_rtnl(b->media_ptr);
227 if (!ub) {
228 err = -ENODEV;
Richard Alpeef20cd42016-08-26 10:52:53 +0200229 goto out;
Richard Alpece984da2016-08-26 10:52:51 +0200230 }
231
Jon Paul Maloy99999742017-01-18 13:50:50 -0500232 if (addr->broadcast != TIPC_REPLICAST_SUPPORT)
Richard Alpeef20cd42016-08-26 10:52:53 +0200233 return tipc_udp_xmit(net, skb, ub, src, dst);
Richard Alpece984da2016-08-26 10:52:51 +0200234
Richard Alpeef20cd42016-08-26 10:52:53 +0200235 /* Replicast, send an skb to each configured IP address */
236 list_for_each_entry_rcu(rcast, &ub->rcast.list, list) {
237 struct sk_buff *_skb;
238
239 _skb = pskb_copy(skb, GFP_ATOMIC);
240 if (!_skb) {
241 err = -ENOMEM;
242 goto out;
243 }
244
245 err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr);
246 if (err) {
247 kfree_skb(_skb);
248 goto out;
249 }
250 }
251 err = 0;
252out:
Richard Alpece984da2016-08-26 10:52:51 +0200253 kfree_skb(skb);
254 return err;
255}
256
Richard Alpec9b64d42016-08-26 10:52:54 +0200257static bool tipc_udp_is_known_peer(struct tipc_bearer *b,
258 struct udp_media_addr *addr)
259{
260 struct udp_replicast *rcast, *tmp;
261 struct udp_bearer *ub;
262
263 ub = rcu_dereference_rtnl(b->media_ptr);
264 if (!ub) {
265 pr_err_ratelimited("UDP bearer instance not found\n");
266 return false;
267 }
268
269 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
270 if (!memcmp(&rcast->addr, addr, sizeof(struct udp_media_addr)))
271 return true;
272 }
273
274 return false;
275}
276
Richard Alpeef20cd42016-08-26 10:52:53 +0200277static int tipc_udp_rcast_add(struct tipc_bearer *b,
278 struct udp_media_addr *addr)
279{
280 struct udp_replicast *rcast;
281 struct udp_bearer *ub;
282
283 ub = rcu_dereference_rtnl(b->media_ptr);
284 if (!ub)
285 return -ENODEV;
286
287 rcast = kmalloc(sizeof(*rcast), GFP_ATOMIC);
288 if (!rcast)
289 return -ENOMEM;
290
291 memcpy(&rcast->addr, addr, sizeof(struct udp_media_addr));
292
293 if (ntohs(addr->proto) == ETH_P_IP)
294 pr_info("New replicast peer: %pI4\n", &rcast->addr.ipv4);
295#if IS_ENABLED(CONFIG_IPV6)
296 else if (ntohs(addr->proto) == ETH_P_IPV6)
297 pr_info("New replicast peer: %pI6\n", &rcast->addr.ipv6);
298#endif
Jon Paul Maloy99999742017-01-18 13:50:50 -0500299 b->bcast_addr.broadcast = TIPC_REPLICAST_SUPPORT;
Richard Alpeef20cd42016-08-26 10:52:53 +0200300 list_add_rcu(&rcast->list, &ub->rcast.list);
301 return 0;
302}
303
Richard Alpec9b64d42016-08-26 10:52:54 +0200304static int tipc_udp_rcast_disc(struct tipc_bearer *b, struct sk_buff *skb)
305{
306 struct udp_media_addr src = {0};
307 struct udp_media_addr *dst;
308
309 dst = (struct udp_media_addr *)&b->bcast_addr.value;
310 if (tipc_udp_is_mcast_addr(dst))
311 return 0;
312
313 src.port = udp_hdr(skb)->source;
314
315 if (ip_hdr(skb)->version == 4) {
316 struct iphdr *iphdr = ip_hdr(skb);
317
318 src.proto = htons(ETH_P_IP);
319 src.ipv4.s_addr = iphdr->saddr;
320 if (ipv4_is_multicast(iphdr->daddr))
321 return 0;
322#if IS_ENABLED(CONFIG_IPV6)
323 } else if (ip_hdr(skb)->version == 6) {
324 struct ipv6hdr *iphdr = ipv6_hdr(skb);
325
326 src.proto = htons(ETH_P_IPV6);
327 src.ipv6 = iphdr->saddr;
328 if (ipv6_addr_is_multicast(&iphdr->daddr))
329 return 0;
330#endif
331 } else {
332 return 0;
333 }
334
335 if (likely(tipc_udp_is_known_peer(b, &src)))
336 return 0;
337
338 return tipc_udp_rcast_add(b, &src);
339}
340
Erik Hugned0f91932015-03-05 10:23:49 +0100341/* tipc_udp_recv - read data from bearer socket */
342static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
343{
344 struct udp_bearer *ub;
345 struct tipc_bearer *b;
Richard Alpec9b64d42016-08-26 10:52:54 +0200346 struct tipc_msg *hdr;
347 int err;
Erik Hugned0f91932015-03-05 10:23:49 +0100348
349 ub = rcu_dereference_sk_user_data(sk);
350 if (!ub) {
351 pr_err_ratelimited("Failed to get UDP bearer reference");
Richard Alpec9b64d42016-08-26 10:52:54 +0200352 goto out;
Erik Hugned0f91932015-03-05 10:23:49 +0100353 }
Erik Hugned0f91932015-03-05 10:23:49 +0100354 skb_pull(skb, sizeof(struct udphdr));
Richard Alpec9b64d42016-08-26 10:52:54 +0200355 hdr = buf_msg(skb);
356
Erik Hugned0f91932015-03-05 10:23:49 +0100357 rcu_read_lock();
358 b = rcu_dereference_rtnl(ub->bearer);
Richard Alpec9b64d42016-08-26 10:52:54 +0200359 if (!b)
360 goto rcu_out;
Erik Hugned0f91932015-03-05 10:23:49 +0100361
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400362 if (b && test_bit(0, &b->up)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100363 tipc_rcv(sock_net(sk), skb, b);
364 rcu_read_unlock();
365 return 0;
366 }
Richard Alpec9b64d42016-08-26 10:52:54 +0200367
368 if (unlikely(msg_user(hdr) == LINK_CONFIG)) {
369 err = tipc_udp_rcast_disc(b, skb);
370 if (err)
371 goto rcu_out;
372 }
373
Richard Alpec9b64d42016-08-26 10:52:54 +0200374rcu_out:
375 rcu_read_unlock();
376out:
Erik Hugned0f91932015-03-05 10:23:49 +0100377 kfree_skb(skb);
378 return 0;
379}
380
381static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
382{
383 int err = 0;
384 struct ip_mreqn mreqn;
385 struct sock *sk = ub->ubsock->sk;
386
387 if (ntohs(remote->proto) == ETH_P_IP) {
Erik Hugned0f91932015-03-05 10:23:49 +0100388 mreqn.imr_multiaddr = remote->ipv4;
389 mreqn.imr_ifindex = ub->ifindex;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300390 err = ip_mc_join_group(sk, &mreqn);
Marcelo Ricardo Leitner446981e2015-03-19 16:47:58 -0300391#if IS_ENABLED(CONFIG_IPV6)
Erik Hugned0f91932015-03-05 10:23:49 +0100392 } else {
Marcelo Ricardo Leitner446981e2015-03-19 16:47:58 -0300393 err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
394 &remote->ipv6);
395#endif
Erik Hugned0f91932015-03-05 10:23:49 +0100396 }
397 return err;
398}
399
Richard Alpefdb3acc2016-08-26 10:52:55 +0200400static int __tipc_nl_add_udp_addr(struct sk_buff *skb,
401 struct udp_media_addr *addr, int nla_t)
402{
403 if (ntohs(addr->proto) == ETH_P_IP) {
404 struct sockaddr_in ip4;
405
Dan Carpenter73076162016-10-13 11:06:06 +0300406 memset(&ip4, 0, sizeof(ip4));
Richard Alpefdb3acc2016-08-26 10:52:55 +0200407 ip4.sin_family = AF_INET;
408 ip4.sin_port = addr->port;
409 ip4.sin_addr.s_addr = addr->ipv4.s_addr;
410 if (nla_put(skb, nla_t, sizeof(ip4), &ip4))
411 return -EMSGSIZE;
412
413#if IS_ENABLED(CONFIG_IPV6)
414 } else if (ntohs(addr->proto) == ETH_P_IPV6) {
415 struct sockaddr_in6 ip6;
416
Dan Carpenter73076162016-10-13 11:06:06 +0300417 memset(&ip6, 0, sizeof(ip6));
Richard Alpefdb3acc2016-08-26 10:52:55 +0200418 ip6.sin6_family = AF_INET6;
419 ip6.sin6_port = addr->port;
420 memcpy(&ip6.sin6_addr, &addr->ipv6, sizeof(struct in6_addr));
421 if (nla_put(skb, nla_t, sizeof(ip6), &ip6))
422 return -EMSGSIZE;
423#endif
424 }
425
426 return 0;
427}
428
Richard Alpe832629c2016-08-26 10:52:56 +0200429int tipc_udp_nl_dump_remoteip(struct sk_buff *skb, struct netlink_callback *cb)
430{
431 u32 bid = cb->args[0];
432 u32 skip_cnt = cb->args[1];
433 u32 portid = NETLINK_CB(cb->skb).portid;
434 struct udp_replicast *rcast, *tmp;
435 struct tipc_bearer *b;
436 struct udp_bearer *ub;
437 void *hdr;
438 int err;
439 int i;
440
441 if (!bid && !skip_cnt) {
442 struct net *net = sock_net(skb->sk);
443 struct nlattr *battrs[TIPC_NLA_BEARER_MAX + 1];
444 struct nlattr **attrs;
445 char *bname;
446
447 err = tipc_nlmsg_parse(cb->nlh, &attrs);
448 if (err)
449 return err;
450
451 if (!attrs[TIPC_NLA_BEARER])
452 return -EINVAL;
453
454 err = nla_parse_nested(battrs, TIPC_NLA_BEARER_MAX,
455 attrs[TIPC_NLA_BEARER],
Johannes Bergfceb6432017-04-12 14:34:07 +0200456 tipc_nl_bearer_policy, NULL);
Richard Alpe832629c2016-08-26 10:52:56 +0200457 if (err)
458 return err;
459
460 if (!battrs[TIPC_NLA_BEARER_NAME])
461 return -EINVAL;
462
463 bname = nla_data(battrs[TIPC_NLA_BEARER_NAME]);
464
465 rtnl_lock();
466 b = tipc_bearer_find(net, bname);
467 if (!b) {
468 rtnl_unlock();
469 return -EINVAL;
470 }
471 bid = b->identity;
472 } else {
473 struct net *net = sock_net(skb->sk);
474 struct tipc_net *tn = net_generic(net, tipc_net_id);
475
476 rtnl_lock();
477 b = rtnl_dereference(tn->bearer_list[bid]);
478 if (!b) {
479 rtnl_unlock();
480 return -EINVAL;
481 }
482 }
483
484 ub = rcu_dereference_rtnl(b->media_ptr);
485 if (!ub) {
486 rtnl_unlock();
487 return -EINVAL;
488 }
489
490 i = 0;
491 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
492 if (i < skip_cnt)
493 goto count;
494
495 hdr = genlmsg_put(skb, portid, cb->nlh->nlmsg_seq,
496 &tipc_genl_family, NLM_F_MULTI,
497 TIPC_NL_BEARER_GET);
498 if (!hdr)
499 goto done;
500
501 err = __tipc_nl_add_udp_addr(skb, &rcast->addr,
502 TIPC_NLA_UDP_REMOTE);
503 if (err) {
504 genlmsg_cancel(skb, hdr);
505 goto done;
506 }
507 genlmsg_end(skb, hdr);
508count:
509 i++;
510 }
511done:
512 rtnl_unlock();
513 cb->args[0] = bid;
514 cb->args[1] = i;
515
516 return skb->len;
517}
518
Richard Alpefdb3acc2016-08-26 10:52:55 +0200519int tipc_udp_nl_add_bearer_data(struct tipc_nl_msg *msg, struct tipc_bearer *b)
520{
521 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
522 struct udp_media_addr *dst;
523 struct udp_bearer *ub;
524 struct nlattr *nest;
525
526 ub = rcu_dereference_rtnl(b->media_ptr);
527 if (!ub)
528 return -ENODEV;
529
530 nest = nla_nest_start(msg->skb, TIPC_NLA_BEARER_UDP_OPTS);
531 if (!nest)
532 goto msg_full;
533
534 if (__tipc_nl_add_udp_addr(msg->skb, src, TIPC_NLA_UDP_LOCAL))
535 goto msg_full;
536
537 dst = (struct udp_media_addr *)&b->bcast_addr.value;
538 if (__tipc_nl_add_udp_addr(msg->skb, dst, TIPC_NLA_UDP_REMOTE))
539 goto msg_full;
540
541 if (!list_empty(&ub->rcast.list)) {
542 if (nla_put_flag(msg->skb, TIPC_NLA_UDP_MULTI_REMOTEIP))
543 goto msg_full;
544 }
545
546 nla_nest_end(msg->skb, nest);
547 return 0;
548msg_full:
549 nla_nest_cancel(msg->skb, nest);
550 return -EMSGSIZE;
551}
552
Erik Hugned0f91932015-03-05 10:23:49 +0100553/**
Richard Alpeba5aa842016-08-26 10:52:50 +0200554 * tipc_parse_udp_addr - build udp media address from netlink data
555 * @nlattr: netlink attribute containing sockaddr storage aligned address
556 * @addr: tipc media address to fill with address, port and protocol type
557 * @scope_id: IPv6 scope id pointer, not NULL indicates it's required
Erik Hugned0f91932015-03-05 10:23:49 +0100558 */
Richard Alpeba5aa842016-08-26 10:52:50 +0200559
560static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr,
561 u32 *scope_id)
Erik Hugned0f91932015-03-05 10:23:49 +0100562{
Richard Alpeba5aa842016-08-26 10:52:50 +0200563 struct sockaddr_storage sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100564
Richard Alpeba5aa842016-08-26 10:52:50 +0200565 nla_memcpy(&sa, nla, sizeof(sa));
566 if (sa.ss_family == AF_INET) {
567 struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100568
Richard Alpeba5aa842016-08-26 10:52:50 +0200569 addr->proto = htons(ETH_P_IP);
570 addr->port = ip4->sin_port;
571 addr->ipv4.s_addr = ip4->sin_addr.s_addr;
Erik Hugned0f91932015-03-05 10:23:49 +0100572 return 0;
573
574#if IS_ENABLED(CONFIG_IPV6)
Richard Alpeba5aa842016-08-26 10:52:50 +0200575 } else if (sa.ss_family == AF_INET6) {
576 struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100577
Richard Alpeba5aa842016-08-26 10:52:50 +0200578 addr->proto = htons(ETH_P_IPV6);
579 addr->port = ip6->sin6_port;
580 memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr));
Richard Alpe34f65db2016-03-03 14:20:43 +0100581
Richard Alpeba5aa842016-08-26 10:52:50 +0200582 /* Scope ID is only interesting for local addresses */
583 if (scope_id) {
584 int atype;
Erik Hugned0f91932015-03-05 10:23:49 +0100585
Richard Alpeba5aa842016-08-26 10:52:50 +0200586 atype = ipv6_addr_type(&ip6->sin6_addr);
587 if (__ipv6_addr_needs_scope_id(atype) &&
588 !ip6->sin6_scope_id) {
589 return -EINVAL;
590 }
591
592 *scope_id = ip6->sin6_scope_id ? : 0;
593 }
594
Erik Hugned0f91932015-03-05 10:23:49 +0100595 return 0;
596#endif
597 }
598 return -EADDRNOTAVAIL;
599}
600
Richard Alpeef20cd42016-08-26 10:52:53 +0200601int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr)
602{
603 int err;
604 struct udp_media_addr addr = {0};
605 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
606 struct udp_media_addr *dst;
607
Johannes Bergfceb6432017-04-12 14:34:07 +0200608 if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX, attr,
609 tipc_nl_udp_policy, NULL))
Richard Alpeef20cd42016-08-26 10:52:53 +0200610 return -EINVAL;
611
612 if (!opts[TIPC_NLA_UDP_REMOTE])
613 return -EINVAL;
614
615 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL);
616 if (err)
617 return err;
618
619 dst = (struct udp_media_addr *)&b->bcast_addr.value;
620 if (tipc_udp_is_mcast_addr(dst)) {
621 pr_err("Can't add remote ip to TIPC UDP multicast bearer\n");
622 return -EINVAL;
623 }
624
Richard Alpec9b64d42016-08-26 10:52:54 +0200625 if (tipc_udp_is_known_peer(b, &addr))
626 return 0;
627
Richard Alpeef20cd42016-08-26 10:52:53 +0200628 return tipc_udp_rcast_add(b, &addr);
629}
630
Erik Hugned0f91932015-03-05 10:23:49 +0100631/**
632 * tipc_udp_enable - callback to create a new udp bearer instance
633 * @net: network namespace
634 * @b: pointer to generic tipc_bearer
635 * @attrs: netlink bearer configuration
636 *
637 * validate the bearer parameters and initialize the udp bearer
638 * rtnl_lock should be held
639 */
640static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
641 struct nlattr *attrs[])
642{
643 int err = -EINVAL;
644 struct udp_bearer *ub;
Richard Alpeef20cd42016-08-26 10:52:53 +0200645 struct udp_media_addr remote = {0};
Erik Hugned0f91932015-03-05 10:23:49 +0100646 struct udp_media_addr local = {0};
647 struct udp_port_cfg udp_conf = {0};
Erik Hugne4fee6be82015-03-09 10:19:31 +0100648 struct udp_tunnel_sock_cfg tuncfg = {NULL};
Richard Alpeba5aa842016-08-26 10:52:50 +0200649 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
Erik Hugned0f91932015-03-05 10:23:49 +0100650
651 ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
652 if (!ub)
653 return -ENOMEM;
654
Richard Alpeef20cd42016-08-26 10:52:53 +0200655 INIT_LIST_HEAD(&ub->rcast.list);
656
Richard Alpeba5aa842016-08-26 10:52:50 +0200657 if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
658 goto err;
659
660 if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX,
661 attrs[TIPC_NLA_BEARER_UDP_OPTS],
Johannes Bergfceb6432017-04-12 14:34:07 +0200662 tipc_nl_udp_policy, NULL))
Richard Alpeba5aa842016-08-26 10:52:50 +0200663 goto err;
664
665 if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) {
666 pr_err("Invalid UDP bearer configuration");
Wei Yongjunc20cb812016-09-10 00:56:55 +0000667 err = -EINVAL;
668 goto err;
Richard Alpeba5aa842016-08-26 10:52:50 +0200669 }
670
671 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local,
672 &ub->ifindex);
673 if (err)
674 goto err;
675
Richard Alpeef20cd42016-08-26 10:52:53 +0200676 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL);
Erik Hugned0f91932015-03-05 10:23:49 +0100677 if (err)
678 goto err;
679
680 b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
Jon Paul Maloy99999742017-01-18 13:50:50 -0500681 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
Erik Hugned0f91932015-03-05 10:23:49 +0100682 rcu_assign_pointer(b->media_ptr, ub);
683 rcu_assign_pointer(ub->bearer, b);
684 tipc_udp_media_addr_set(&b->addr, &local);
Erik Hugne4fee6be82015-03-09 10:19:31 +0100685 if (local.proto == htons(ETH_P_IP)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100686 struct net_device *dev;
687
688 dev = __ip_dev_find(net, local.ipv4.s_addr, false);
689 if (!dev) {
690 err = -ENODEV;
691 goto err;
692 }
693 udp_conf.family = AF_INET;
694 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
695 udp_conf.use_udp_checksums = false;
696 ub->ifindex = dev->ifindex;
Michal Kubeček3de81b72016-12-02 09:33:41 +0100697 if (tipc_mtu_bad(dev, sizeof(struct iphdr) +
698 sizeof(struct udphdr))) {
699 err = -EINVAL;
700 goto err;
701 }
Erik Hugned0f91932015-03-05 10:23:49 +0100702 b->mtu = dev->mtu - sizeof(struct iphdr)
703 - sizeof(struct udphdr);
704#if IS_ENABLED(CONFIG_IPV6)
Erik Hugne4fee6be82015-03-09 10:19:31 +0100705 } else if (local.proto == htons(ETH_P_IPV6)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100706 udp_conf.family = AF_INET6;
707 udp_conf.use_udp6_tx_checksums = true;
708 udp_conf.use_udp6_rx_checksums = true;
709 udp_conf.local_ip6 = in6addr_any;
710 b->mtu = 1280;
711#endif
712 } else {
713 err = -EAFNOSUPPORT;
714 goto err;
715 }
Richard Alpebc3a3342016-06-27 13:34:07 +0200716 udp_conf.local_udp_port = local.port;
Erik Hugned0f91932015-03-05 10:23:49 +0100717 err = udp_sock_create(net, &udp_conf, &ub->ubsock);
718 if (err)
719 goto err;
720 tuncfg.sk_user_data = ub;
721 tuncfg.encap_type = 1;
722 tuncfg.encap_rcv = tipc_udp_recv;
723 tuncfg.encap_destroy = NULL;
724 setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
725
Richard Alpeef20cd42016-08-26 10:52:53 +0200726 /**
727 * The bcast media address port is used for all peers and the ip
728 * is used if it's a multicast address.
729 */
730 memcpy(&b->bcast_addr.value, &remote, sizeof(remote));
731 if (tipc_udp_is_mcast_addr(&remote))
732 err = enable_mcast(ub, &remote);
733 else
734 err = tipc_udp_rcast_add(b, &remote);
735 if (err)
736 goto err;
Richard Alpe1ca73e32016-08-26 10:52:52 +0200737
Erik Hugned0f91932015-03-05 10:23:49 +0100738 return 0;
739err:
Wei Yongjuna5de1252016-08-24 13:32:19 +0000740 if (ub->ubsock)
741 udp_tunnel_sock_release(ub->ubsock);
Erik Hugned0f91932015-03-05 10:23:49 +0100742 kfree(ub);
743 return err;
744}
745
746/* cleanup_bearer - break the socket/bearer association */
747static void cleanup_bearer(struct work_struct *work)
748{
749 struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
Richard Alpeef20cd42016-08-26 10:52:53 +0200750 struct udp_replicast *rcast, *tmp;
751
752 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
753 list_del_rcu(&rcast->list);
754 kfree_rcu(rcast, rcu);
755 }
Erik Hugned0f91932015-03-05 10:23:49 +0100756
757 if (ub->ubsock)
758 udp_tunnel_sock_release(ub->ubsock);
759 synchronize_net();
760 kfree(ub);
761}
762
763/* tipc_udp_disable - detach bearer from socket */
764static void tipc_udp_disable(struct tipc_bearer *b)
765{
766 struct udp_bearer *ub;
767
768 ub = rcu_dereference_rtnl(b->media_ptr);
769 if (!ub) {
770 pr_err("UDP bearer instance not found\n");
771 return;
772 }
773 if (ub->ubsock)
774 sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
Erik Hugned0f91932015-03-05 10:23:49 +0100775 RCU_INIT_POINTER(ub->bearer, NULL);
776
777 /* sock_release need to be done outside of rtnl lock */
778 INIT_WORK(&ub->work, cleanup_bearer);
779 schedule_work(&ub->work);
780}
781
782struct tipc_media udp_media_info = {
783 .send_msg = tipc_udp_send_msg,
784 .enable_media = tipc_udp_enable,
785 .disable_media = tipc_udp_disable,
786 .addr2str = tipc_udp_addr2str,
787 .addr2msg = tipc_udp_addr2msg,
788 .msg2addr = tipc_udp_msg2addr,
789 .priority = TIPC_DEF_LINK_PRI,
790 .tolerance = TIPC_DEF_LINK_TOL,
791 .window = TIPC_DEF_LINK_WIN,
792 .type_id = TIPC_MEDIA_TYPE_UDP,
793 .hwaddr_len = 0,
794 .name = "udp"
795};