blob: adb3c2117a1c7de9d863a39fe396432fced1843a [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"
Erik Hugned0f91932015-03-05 10:23:49 +010052
53/* IANA assigned UDP port */
54#define UDP_PORT_DEFAULT 6118
55
Richard Alpe9bd160b2016-03-14 09:43:52 +010056#define UDP_MIN_HEADROOM 48
Jon Paul Maloye5356792015-10-19 11:43:11 -040057
Erik Hugned0f91932015-03-05 10:23:49 +010058/**
59 * struct udp_media_addr - IP/UDP addressing information
60 *
61 * This is the bearer level originating address used in neighbor discovery
62 * messages, and all fields should be in network byte order
63 */
64struct udp_media_addr {
65 __be16 proto;
Richard Alpebc3a3342016-06-27 13:34:07 +020066 __be16 port;
Erik Hugned0f91932015-03-05 10:23:49 +010067 union {
68 struct in_addr ipv4;
69 struct in6_addr ipv6;
70 };
71};
72
73/**
74 * struct udp_bearer - ip/udp bearer data structure
75 * @bearer: associated generic tipc bearer
76 * @ubsock: bearer associated socket
77 * @ifindex: local address scope
78 * @work: used to schedule deferred work on a bearer
79 */
80struct udp_bearer {
81 struct tipc_bearer __rcu *bearer;
82 struct socket *ubsock;
83 u32 ifindex;
84 struct work_struct work;
85};
86
87/* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
88static void tipc_udp_media_addr_set(struct tipc_media_addr *addr,
89 struct udp_media_addr *ua)
90{
91 memset(addr, 0, sizeof(struct tipc_media_addr));
92 addr->media_id = TIPC_MEDIA_TYPE_UDP;
93 memcpy(addr->value, ua, sizeof(struct udp_media_addr));
94 if (ntohs(ua->proto) == ETH_P_IP) {
95 if (ipv4_is_multicast(ua->ipv4.s_addr))
96 addr->broadcast = 1;
97 } else if (ntohs(ua->proto) == ETH_P_IPV6) {
98 if (ipv6_addr_type(&ua->ipv6) & IPV6_ADDR_MULTICAST)
99 addr->broadcast = 1;
100 } else {
101 pr_err("Invalid UDP media address\n");
102 }
103}
104
105/* tipc_udp_addr2str - convert ip/udp address to string */
106static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size)
107{
108 struct udp_media_addr *ua = (struct udp_media_addr *)&a->value;
109
110 if (ntohs(ua->proto) == ETH_P_IP)
Richard Alpebc3a3342016-06-27 13:34:07 +0200111 snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port));
Erik Hugned0f91932015-03-05 10:23:49 +0100112 else if (ntohs(ua->proto) == ETH_P_IPV6)
Richard Alpebc3a3342016-06-27 13:34:07 +0200113 snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port));
Erik Hugned0f91932015-03-05 10:23:49 +0100114 else
115 pr_err("Invalid UDP media address\n");
116 return 0;
117}
118
119/* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
120static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a,
121 char *msg)
122{
123 struct udp_media_addr *ua;
124
125 ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET);
126 if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP)
127 return -EINVAL;
128 tipc_udp_media_addr_set(a, ua);
129 return 0;
130}
131
132/* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
133static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a)
134{
135 memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
136 msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP;
137 memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value,
138 sizeof(struct udp_media_addr));
139 return 0;
140}
141
142/* tipc_send_msg - enqueue a send request */
143static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
144 struct tipc_bearer *b,
145 struct tipc_media_addr *dest)
146{
147 int ttl, err = 0;
148 struct udp_bearer *ub;
149 struct udp_media_addr *dst = (struct udp_media_addr *)&dest->value;
150 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
Erik Hugned0f91932015-03-05 10:23:49 +0100151 struct rtable *rt;
152
Ying Xue70983562015-11-24 13:57:57 +0800153 if (skb_headroom(skb) < UDP_MIN_HEADROOM) {
154 err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC);
155 if (err)
156 goto tx_error;
157 }
Jon Paul Maloye5356792015-10-19 11:43:11 -0400158
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400159 skb_set_inner_protocol(skb, htons(ETH_P_TIPC));
Erik Hugned0f91932015-03-05 10:23:49 +0100160 ub = rcu_dereference_rtnl(b->media_ptr);
161 if (!ub) {
162 err = -ENODEV;
163 goto tx_error;
164 }
Erik Hugne4fee6be82015-03-09 10:19:31 +0100165 if (dst->proto == htons(ETH_P_IP)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100166 struct flowi4 fl = {
167 .daddr = dst->ipv4.s_addr,
168 .saddr = src->ipv4.s_addr,
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400169 .flowi4_mark = skb->mark,
Erik Hugned0f91932015-03-05 10:23:49 +0100170 .flowi4_proto = IPPROTO_UDP
171 };
172 rt = ip_route_output_key(net, &fl);
173 if (IS_ERR(rt)) {
174 err = PTR_ERR(rt);
175 goto tx_error;
176 }
Richard Alpe9b300962016-03-03 14:20:40 +0100177
178 skb->dev = rt->dst.dev;
Erik Hugned0f91932015-03-05 10:23:49 +0100179 ttl = ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar039f5062015-12-24 14:34:54 -0800180 udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr,
Richard Alpebc3a3342016-06-27 13:34:07 +0200181 dst->ipv4.s_addr, 0, ttl, 0, src->port,
182 dst->port, false, true);
Erik Hugned0f91932015-03-05 10:23:49 +0100183#if IS_ENABLED(CONFIG_IPV6)
184 } else {
185 struct dst_entry *ndst;
186 struct flowi6 fl6 = {
187 .flowi6_oif = ub->ifindex,
188 .daddr = dst->ipv6,
189 .saddr = src->ipv6,
190 .flowi6_proto = IPPROTO_UDP
191 };
Roopa Prabhu343d60a2015-07-30 13:34:53 -0700192 err = ipv6_stub->ipv6_dst_lookup(net, ub->ubsock->sk, &ndst,
193 &fl6);
Erik Hugned0f91932015-03-05 10:23:49 +0100194 if (err)
195 goto tx_error;
196 ttl = ip6_dst_hoplimit(ndst);
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400197 err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb,
David Miller79b16aa2015-04-05 22:19:09 -0400198 ndst->dev, &src->ipv6,
Richard Alpebc3a3342016-06-27 13:34:07 +0200199 &dst->ipv6, 0, ttl, 0, src->port,
200 dst->port, false);
Erik Hugned0f91932015-03-05 10:23:49 +0100201#endif
202 }
203 return err;
204
205tx_error:
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400206 kfree_skb(skb);
Erik Hugned0f91932015-03-05 10:23:49 +0100207 return err;
208}
209
210/* tipc_udp_recv - read data from bearer socket */
211static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
212{
213 struct udp_bearer *ub;
214 struct tipc_bearer *b;
215
216 ub = rcu_dereference_sk_user_data(sk);
217 if (!ub) {
218 pr_err_ratelimited("Failed to get UDP bearer reference");
219 kfree_skb(skb);
220 return 0;
221 }
222
223 skb_pull(skb, sizeof(struct udphdr));
224 rcu_read_lock();
225 b = rcu_dereference_rtnl(ub->bearer);
226
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400227 if (b && test_bit(0, &b->up)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100228 tipc_rcv(sock_net(sk), skb, b);
229 rcu_read_unlock();
230 return 0;
231 }
232 rcu_read_unlock();
233 kfree_skb(skb);
234 return 0;
235}
236
237static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
238{
239 int err = 0;
240 struct ip_mreqn mreqn;
241 struct sock *sk = ub->ubsock->sk;
242
243 if (ntohs(remote->proto) == ETH_P_IP) {
244 if (!ipv4_is_multicast(remote->ipv4.s_addr))
245 return 0;
246 mreqn.imr_multiaddr = remote->ipv4;
247 mreqn.imr_ifindex = ub->ifindex;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300248 err = ip_mc_join_group(sk, &mreqn);
Marcelo Ricardo Leitner446981e2015-03-19 16:47:58 -0300249#if IS_ENABLED(CONFIG_IPV6)
Erik Hugned0f91932015-03-05 10:23:49 +0100250 } else {
251 if (!ipv6_addr_is_multicast(&remote->ipv6))
252 return 0;
Marcelo Ricardo Leitner446981e2015-03-19 16:47:58 -0300253 err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
254 &remote->ipv6);
255#endif
Erik Hugned0f91932015-03-05 10:23:49 +0100256 }
257 return err;
258}
259
260/**
Richard Alpeba5aa842016-08-26 10:52:50 +0200261 * tipc_parse_udp_addr - build udp media address from netlink data
262 * @nlattr: netlink attribute containing sockaddr storage aligned address
263 * @addr: tipc media address to fill with address, port and protocol type
264 * @scope_id: IPv6 scope id pointer, not NULL indicates it's required
Erik Hugned0f91932015-03-05 10:23:49 +0100265 */
Richard Alpeba5aa842016-08-26 10:52:50 +0200266
267static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr,
268 u32 *scope_id)
Erik Hugned0f91932015-03-05 10:23:49 +0100269{
Richard Alpeba5aa842016-08-26 10:52:50 +0200270 struct sockaddr_storage sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100271
Richard Alpeba5aa842016-08-26 10:52:50 +0200272 nla_memcpy(&sa, nla, sizeof(sa));
273 if (sa.ss_family == AF_INET) {
274 struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100275
Richard Alpeba5aa842016-08-26 10:52:50 +0200276 addr->proto = htons(ETH_P_IP);
277 addr->port = ip4->sin_port;
278 addr->ipv4.s_addr = ip4->sin_addr.s_addr;
Erik Hugned0f91932015-03-05 10:23:49 +0100279 return 0;
280
281#if IS_ENABLED(CONFIG_IPV6)
Richard Alpeba5aa842016-08-26 10:52:50 +0200282 } else if (sa.ss_family == AF_INET6) {
283 struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa;
Erik Hugned0f91932015-03-05 10:23:49 +0100284
Richard Alpeba5aa842016-08-26 10:52:50 +0200285 addr->proto = htons(ETH_P_IPV6);
286 addr->port = ip6->sin6_port;
287 memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr));
Richard Alpe34f65db2016-03-03 14:20:43 +0100288
Richard Alpeba5aa842016-08-26 10:52:50 +0200289 /* Scope ID is only interesting for local addresses */
290 if (scope_id) {
291 int atype;
Erik Hugned0f91932015-03-05 10:23:49 +0100292
Richard Alpeba5aa842016-08-26 10:52:50 +0200293 atype = ipv6_addr_type(&ip6->sin6_addr);
294 if (__ipv6_addr_needs_scope_id(atype) &&
295 !ip6->sin6_scope_id) {
296 return -EINVAL;
297 }
298
299 *scope_id = ip6->sin6_scope_id ? : 0;
300 }
301
Erik Hugned0f91932015-03-05 10:23:49 +0100302 return 0;
303#endif
304 }
305 return -EADDRNOTAVAIL;
306}
307
308/**
309 * tipc_udp_enable - callback to create a new udp bearer instance
310 * @net: network namespace
311 * @b: pointer to generic tipc_bearer
312 * @attrs: netlink bearer configuration
313 *
314 * validate the bearer parameters and initialize the udp bearer
315 * rtnl_lock should be held
316 */
317static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
318 struct nlattr *attrs[])
319{
320 int err = -EINVAL;
321 struct udp_bearer *ub;
322 struct udp_media_addr *remote;
323 struct udp_media_addr local = {0};
324 struct udp_port_cfg udp_conf = {0};
Erik Hugne4fee6be82015-03-09 10:19:31 +0100325 struct udp_tunnel_sock_cfg tuncfg = {NULL};
Richard Alpeba5aa842016-08-26 10:52:50 +0200326 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
Erik Hugned0f91932015-03-05 10:23:49 +0100327
328 ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
329 if (!ub)
330 return -ENOMEM;
331
Richard Alpeba5aa842016-08-26 10:52:50 +0200332 if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
333 goto err;
334
335 if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX,
336 attrs[TIPC_NLA_BEARER_UDP_OPTS],
337 tipc_nl_udp_policy))
338 goto err;
339
340 if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) {
341 pr_err("Invalid UDP bearer configuration");
342 return -EINVAL;
343 }
344
345 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local,
346 &ub->ifindex);
347 if (err)
348 goto err;
349
Erik Hugned0f91932015-03-05 10:23:49 +0100350 remote = (struct udp_media_addr *)&b->bcast_addr.value;
351 memset(remote, 0, sizeof(struct udp_media_addr));
Richard Alpeba5aa842016-08-26 10:52:50 +0200352 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], remote, NULL);
Erik Hugned0f91932015-03-05 10:23:49 +0100353 if (err)
354 goto err;
355
356 b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
357 b->bcast_addr.broadcast = 1;
358 rcu_assign_pointer(b->media_ptr, ub);
359 rcu_assign_pointer(ub->bearer, b);
360 tipc_udp_media_addr_set(&b->addr, &local);
Erik Hugne4fee6be82015-03-09 10:19:31 +0100361 if (local.proto == htons(ETH_P_IP)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100362 struct net_device *dev;
363
364 dev = __ip_dev_find(net, local.ipv4.s_addr, false);
365 if (!dev) {
366 err = -ENODEV;
367 goto err;
368 }
369 udp_conf.family = AF_INET;
370 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
371 udp_conf.use_udp_checksums = false;
372 ub->ifindex = dev->ifindex;
373 b->mtu = dev->mtu - sizeof(struct iphdr)
374 - sizeof(struct udphdr);
375#if IS_ENABLED(CONFIG_IPV6)
Erik Hugne4fee6be82015-03-09 10:19:31 +0100376 } else if (local.proto == htons(ETH_P_IPV6)) {
Erik Hugned0f91932015-03-05 10:23:49 +0100377 udp_conf.family = AF_INET6;
378 udp_conf.use_udp6_tx_checksums = true;
379 udp_conf.use_udp6_rx_checksums = true;
380 udp_conf.local_ip6 = in6addr_any;
381 b->mtu = 1280;
382#endif
383 } else {
384 err = -EAFNOSUPPORT;
385 goto err;
386 }
Richard Alpebc3a3342016-06-27 13:34:07 +0200387 udp_conf.local_udp_port = local.port;
Erik Hugned0f91932015-03-05 10:23:49 +0100388 err = udp_sock_create(net, &udp_conf, &ub->ubsock);
389 if (err)
390 goto err;
391 tuncfg.sk_user_data = ub;
392 tuncfg.encap_type = 1;
393 tuncfg.encap_rcv = tipc_udp_recv;
394 tuncfg.encap_destroy = NULL;
395 setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
396
397 if (enable_mcast(ub, remote))
398 goto err;
399 return 0;
400err:
401 kfree(ub);
402 return err;
403}
404
405/* cleanup_bearer - break the socket/bearer association */
406static void cleanup_bearer(struct work_struct *work)
407{
408 struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
409
410 if (ub->ubsock)
411 udp_tunnel_sock_release(ub->ubsock);
412 synchronize_net();
413 kfree(ub);
414}
415
416/* tipc_udp_disable - detach bearer from socket */
417static void tipc_udp_disable(struct tipc_bearer *b)
418{
419 struct udp_bearer *ub;
420
421 ub = rcu_dereference_rtnl(b->media_ptr);
422 if (!ub) {
423 pr_err("UDP bearer instance not found\n");
424 return;
425 }
426 if (ub->ubsock)
427 sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
Erik Hugned0f91932015-03-05 10:23:49 +0100428 RCU_INIT_POINTER(ub->bearer, NULL);
429
430 /* sock_release need to be done outside of rtnl lock */
431 INIT_WORK(&ub->work, cleanup_bearer);
432 schedule_work(&ub->work);
433}
434
435struct tipc_media udp_media_info = {
436 .send_msg = tipc_udp_send_msg,
437 .enable_media = tipc_udp_enable,
438 .disable_media = tipc_udp_disable,
439 .addr2str = tipc_udp_addr2str,
440 .addr2msg = tipc_udp_addr2msg,
441 .msg2addr = tipc_udp_msg2addr,
442 .priority = TIPC_DEF_LINK_PRI,
443 .tolerance = TIPC_DEF_LINK_TOL,
444 .window = TIPC_DEF_LINK_WIN,
445 .type_id = TIPC_MEDIA_TYPE_UDP,
446 .hwaddr_len = 0,
447 .name = "udp"
448};