xref: /linux/net/tipc/udp_media.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /* 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>
47 #include <net/addrconf.h>
48 #include <linux/tipc_netlink.h>
49 #include "core.h"
50 #include "bearer.h"
51 
52 /* IANA assigned UDP port */
53 #define UDP_PORT_DEFAULT	6118
54 
55 #define UDP_MIN_HEADROOM        28
56 
57 static const struct nla_policy tipc_nl_udp_policy[TIPC_NLA_UDP_MAX + 1] = {
58 	[TIPC_NLA_UDP_UNSPEC]	= {.type = NLA_UNSPEC},
59 	[TIPC_NLA_UDP_LOCAL]	= {.type = NLA_BINARY,
60 				   .len = sizeof(struct sockaddr_storage)},
61 	[TIPC_NLA_UDP_REMOTE]	= {.type = NLA_BINARY,
62 				   .len = sizeof(struct sockaddr_storage)},
63 };
64 
65 /**
66  * struct udp_media_addr - IP/UDP addressing information
67  *
68  * This is the bearer level originating address used in neighbor discovery
69  * messages, and all fields should be in network byte order
70  */
71 struct udp_media_addr {
72 	__be16	proto;
73 	__be16	udp_port;
74 	union {
75 		struct in_addr ipv4;
76 		struct in6_addr ipv6;
77 	};
78 };
79 
80 /**
81  * struct udp_bearer - ip/udp bearer data structure
82  * @bearer:	associated generic tipc bearer
83  * @ubsock:	bearer associated socket
84  * @ifindex:	local address scope
85  * @work:	used to schedule deferred work on a bearer
86  */
87 struct udp_bearer {
88 	struct tipc_bearer __rcu *bearer;
89 	struct socket *ubsock;
90 	u32 ifindex;
91 	struct work_struct work;
92 };
93 
94 /* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
95 static void tipc_udp_media_addr_set(struct tipc_media_addr *addr,
96 				    struct udp_media_addr *ua)
97 {
98 	memset(addr, 0, sizeof(struct tipc_media_addr));
99 	addr->media_id = TIPC_MEDIA_TYPE_UDP;
100 	memcpy(addr->value, ua, sizeof(struct udp_media_addr));
101 	if (ntohs(ua->proto) == ETH_P_IP) {
102 		if (ipv4_is_multicast(ua->ipv4.s_addr))
103 			addr->broadcast = 1;
104 	} else if (ntohs(ua->proto) == ETH_P_IPV6) {
105 		if (ipv6_addr_type(&ua->ipv6) & IPV6_ADDR_MULTICAST)
106 			addr->broadcast = 1;
107 	} else {
108 		pr_err("Invalid UDP media address\n");
109 	}
110 }
111 
112 /* tipc_udp_addr2str - convert ip/udp address to string */
113 static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size)
114 {
115 	struct udp_media_addr *ua = (struct udp_media_addr *)&a->value;
116 
117 	if (ntohs(ua->proto) == ETH_P_IP)
118 		snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->udp_port));
119 	else if (ntohs(ua->proto) == ETH_P_IPV6)
120 		snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->udp_port));
121 	else
122 		pr_err("Invalid UDP media address\n");
123 	return 0;
124 }
125 
126 /* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
127 static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a,
128 			     char *msg)
129 {
130 	struct udp_media_addr *ua;
131 
132 	ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET);
133 	if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP)
134 		return -EINVAL;
135 	tipc_udp_media_addr_set(a, ua);
136 	return 0;
137 }
138 
139 /* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
140 static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a)
141 {
142 	memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
143 	msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP;
144 	memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value,
145 	       sizeof(struct udp_media_addr));
146 	return 0;
147 }
148 
149 /* tipc_send_msg - enqueue a send request */
150 static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
151 			     struct tipc_bearer *b,
152 			     struct tipc_media_addr *dest)
153 {
154 	int ttl, err = 0;
155 	struct udp_bearer *ub;
156 	struct udp_media_addr *dst = (struct udp_media_addr *)&dest->value;
157 	struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
158 	struct sk_buff *clone;
159 	struct rtable *rt;
160 
161 	if (skb_headroom(skb) < UDP_MIN_HEADROOM)
162 		pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC);
163 
164 	clone = skb_clone(skb, GFP_ATOMIC);
165 	skb_set_inner_protocol(clone, htons(ETH_P_TIPC));
166 	ub = rcu_dereference_rtnl(b->media_ptr);
167 	if (!ub) {
168 		err = -ENODEV;
169 		goto tx_error;
170 	}
171 	if (dst->proto == htons(ETH_P_IP)) {
172 		struct flowi4 fl = {
173 			.daddr = dst->ipv4.s_addr,
174 			.saddr = src->ipv4.s_addr,
175 			.flowi4_mark = clone->mark,
176 			.flowi4_proto = IPPROTO_UDP
177 		};
178 		rt = ip_route_output_key(net, &fl);
179 		if (IS_ERR(rt)) {
180 			err = PTR_ERR(rt);
181 			goto tx_error;
182 		}
183 		ttl = ip4_dst_hoplimit(&rt->dst);
184 		err = udp_tunnel_xmit_skb(rt, ub->ubsock->sk, clone,
185 					  src->ipv4.s_addr,
186 					  dst->ipv4.s_addr, 0, ttl, 0,
187 					  src->udp_port, dst->udp_port,
188 					  false, true);
189 		if (err < 0) {
190 			ip_rt_put(rt);
191 			goto tx_error;
192 		}
193 #if IS_ENABLED(CONFIG_IPV6)
194 	} else {
195 		struct dst_entry *ndst;
196 		struct flowi6 fl6 = {
197 			.flowi6_oif = ub->ifindex,
198 			.daddr = dst->ipv6,
199 			.saddr = src->ipv6,
200 			.flowi6_proto = IPPROTO_UDP
201 		};
202 		err = ipv6_stub->ipv6_dst_lookup(net, ub->ubsock->sk, &ndst,
203 						 &fl6);
204 		if (err)
205 			goto tx_error;
206 		ttl = ip6_dst_hoplimit(ndst);
207 		err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, clone,
208 					   ndst->dev, &src->ipv6,
209 					   &dst->ipv6, 0, ttl, src->udp_port,
210 					   dst->udp_port, false);
211 #endif
212 	}
213 	return err;
214 
215 tx_error:
216 	kfree_skb(clone);
217 	return err;
218 }
219 
220 /* tipc_udp_recv - read data from bearer socket */
221 static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
222 {
223 	struct udp_bearer *ub;
224 	struct tipc_bearer *b;
225 
226 	ub = rcu_dereference_sk_user_data(sk);
227 	if (!ub) {
228 		pr_err_ratelimited("Failed to get UDP bearer reference");
229 		kfree_skb(skb);
230 		return 0;
231 	}
232 
233 	skb_pull(skb, sizeof(struct udphdr));
234 	rcu_read_lock();
235 	b = rcu_dereference_rtnl(ub->bearer);
236 
237 	if (b) {
238 		tipc_rcv(sock_net(sk), skb, b);
239 		rcu_read_unlock();
240 		return 0;
241 	}
242 	rcu_read_unlock();
243 	kfree_skb(skb);
244 	return 0;
245 }
246 
247 static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
248 {
249 	int err = 0;
250 	struct ip_mreqn mreqn;
251 	struct sock *sk = ub->ubsock->sk;
252 
253 	if (ntohs(remote->proto) == ETH_P_IP) {
254 		if (!ipv4_is_multicast(remote->ipv4.s_addr))
255 			return 0;
256 		mreqn.imr_multiaddr = remote->ipv4;
257 		mreqn.imr_ifindex = ub->ifindex;
258 		err = ip_mc_join_group(sk, &mreqn);
259 #if IS_ENABLED(CONFIG_IPV6)
260 	} else {
261 		if (!ipv6_addr_is_multicast(&remote->ipv6))
262 			return 0;
263 		err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
264 						   &remote->ipv6);
265 #endif
266 	}
267 	return err;
268 }
269 
270 /**
271  * parse_options - build local/remote addresses from configuration
272  * @attrs:	netlink config data
273  * @ub:		UDP bearer instance
274  * @local:	local bearer IP address/port
275  * @remote:	peer or multicast IP/port
276  */
277 static int parse_options(struct nlattr *attrs[], struct udp_bearer *ub,
278 			 struct udp_media_addr *local,
279 			 struct udp_media_addr *remote)
280 {
281 	struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
282 	struct sockaddr_storage *sa_local, *sa_remote;
283 
284 	if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
285 		goto err;
286 	if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX,
287 			     attrs[TIPC_NLA_BEARER_UDP_OPTS],
288 			     tipc_nl_udp_policy))
289 		goto err;
290 	if (opts[TIPC_NLA_UDP_LOCAL] && opts[TIPC_NLA_UDP_REMOTE]) {
291 		sa_local = nla_data(opts[TIPC_NLA_UDP_LOCAL]);
292 		sa_remote = nla_data(opts[TIPC_NLA_UDP_REMOTE]);
293 	} else {
294 err:
295 		pr_err("Invalid UDP bearer configuration");
296 		return -EINVAL;
297 	}
298 	if ((sa_local->ss_family & sa_remote->ss_family) == AF_INET) {
299 		struct sockaddr_in *ip4;
300 
301 		ip4 = (struct sockaddr_in *)sa_local;
302 		local->proto = htons(ETH_P_IP);
303 		local->udp_port = ip4->sin_port;
304 		local->ipv4.s_addr = ip4->sin_addr.s_addr;
305 
306 		ip4 = (struct sockaddr_in *)sa_remote;
307 		remote->proto = htons(ETH_P_IP);
308 		remote->udp_port = ip4->sin_port;
309 		remote->ipv4.s_addr = ip4->sin_addr.s_addr;
310 		return 0;
311 
312 #if IS_ENABLED(CONFIG_IPV6)
313 	} else if ((sa_local->ss_family & sa_remote->ss_family) == AF_INET6) {
314 		struct sockaddr_in6 *ip6;
315 
316 		ip6 = (struct sockaddr_in6 *)sa_local;
317 		local->proto = htons(ETH_P_IPV6);
318 		local->udp_port = ip6->sin6_port;
319 		local->ipv6 = ip6->sin6_addr;
320 		ub->ifindex = ip6->sin6_scope_id;
321 
322 		ip6 = (struct sockaddr_in6 *)sa_remote;
323 		remote->proto = htons(ETH_P_IPV6);
324 		remote->udp_port = ip6->sin6_port;
325 		remote->ipv6 = ip6->sin6_addr;
326 		return 0;
327 #endif
328 	}
329 	return -EADDRNOTAVAIL;
330 }
331 
332 /**
333  * tipc_udp_enable - callback to create a new udp bearer instance
334  * @net:	network namespace
335  * @b:		pointer to generic tipc_bearer
336  * @attrs:	netlink bearer configuration
337  *
338  * validate the bearer parameters and initialize the udp bearer
339  * rtnl_lock should be held
340  */
341 static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
342 			   struct nlattr *attrs[])
343 {
344 	int err = -EINVAL;
345 	struct udp_bearer *ub;
346 	struct udp_media_addr *remote;
347 	struct udp_media_addr local = {0};
348 	struct udp_port_cfg udp_conf = {0};
349 	struct udp_tunnel_sock_cfg tuncfg = {NULL};
350 
351 	ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
352 	if (!ub)
353 		return -ENOMEM;
354 
355 	remote = (struct udp_media_addr *)&b->bcast_addr.value;
356 	memset(remote, 0, sizeof(struct udp_media_addr));
357 	err = parse_options(attrs, ub, &local, remote);
358 	if (err)
359 		goto err;
360 
361 	b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
362 	b->bcast_addr.broadcast = 1;
363 	rcu_assign_pointer(b->media_ptr, ub);
364 	rcu_assign_pointer(ub->bearer, b);
365 	tipc_udp_media_addr_set(&b->addr, &local);
366 	if (local.proto == htons(ETH_P_IP)) {
367 		struct net_device *dev;
368 
369 		dev = __ip_dev_find(net, local.ipv4.s_addr, false);
370 		if (!dev) {
371 			err = -ENODEV;
372 			goto err;
373 		}
374 		udp_conf.family = AF_INET;
375 		udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
376 		udp_conf.use_udp_checksums = false;
377 		ub->ifindex = dev->ifindex;
378 		b->mtu = dev->mtu - sizeof(struct iphdr)
379 			- sizeof(struct udphdr);
380 #if IS_ENABLED(CONFIG_IPV6)
381 	} else if (local.proto == htons(ETH_P_IPV6)) {
382 		udp_conf.family = AF_INET6;
383 		udp_conf.use_udp6_tx_checksums = true;
384 		udp_conf.use_udp6_rx_checksums = true;
385 		udp_conf.local_ip6 = in6addr_any;
386 		b->mtu = 1280;
387 #endif
388 	} else {
389 		err = -EAFNOSUPPORT;
390 		goto err;
391 	}
392 	udp_conf.local_udp_port = local.udp_port;
393 	err = udp_sock_create(net, &udp_conf, &ub->ubsock);
394 	if (err)
395 		goto err;
396 	tuncfg.sk_user_data = ub;
397 	tuncfg.encap_type = 1;
398 	tuncfg.encap_rcv = tipc_udp_recv;
399 	tuncfg.encap_destroy = NULL;
400 	setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
401 
402 	if (enable_mcast(ub, remote))
403 		goto err;
404 	return 0;
405 err:
406 	kfree(ub);
407 	return err;
408 }
409 
410 /* cleanup_bearer - break the socket/bearer association */
411 static void cleanup_bearer(struct work_struct *work)
412 {
413 	struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
414 
415 	if (ub->ubsock)
416 		udp_tunnel_sock_release(ub->ubsock);
417 	synchronize_net();
418 	kfree(ub);
419 }
420 
421 /* tipc_udp_disable - detach bearer from socket */
422 static void tipc_udp_disable(struct tipc_bearer *b)
423 {
424 	struct udp_bearer *ub;
425 
426 	ub = rcu_dereference_rtnl(b->media_ptr);
427 	if (!ub) {
428 		pr_err("UDP bearer instance not found\n");
429 		return;
430 	}
431 	if (ub->ubsock)
432 		sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
433 	RCU_INIT_POINTER(b->media_ptr, NULL);
434 	RCU_INIT_POINTER(ub->bearer, NULL);
435 
436 	/* sock_release need to be done outside of rtnl lock */
437 	INIT_WORK(&ub->work, cleanup_bearer);
438 	schedule_work(&ub->work);
439 }
440 
441 struct tipc_media udp_media_info = {
442 	.send_msg	= tipc_udp_send_msg,
443 	.enable_media	= tipc_udp_enable,
444 	.disable_media	= tipc_udp_disable,
445 	.addr2str	= tipc_udp_addr2str,
446 	.addr2msg	= tipc_udp_addr2msg,
447 	.msg2addr	= tipc_udp_msg2addr,
448 	.priority	= TIPC_DEF_LINK_PRI,
449 	.tolerance	= TIPC_DEF_LINK_TOL,
450 	.window		= TIPC_DEF_LINK_WIN,
451 	.type_id	= TIPC_MEDIA_TYPE_UDP,
452 	.hwaddr_len	= 0,
453 	.name		= "udp"
454 };
455