• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ipv6_stubs.h>
48 #include <linux/tipc_netlink.h>
49 #include "core.h"
50 #include "addr.h"
51 #include "net.h"
52 #include "bearer.h"
53 #include "netlink.h"
54 #include "msg.h"
55 
56 /* IANA assigned UDP port */
57 #define UDP_PORT_DEFAULT	6118
58 
59 #define UDP_MIN_HEADROOM        48
60 
61 /**
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  */
67 struct udp_media_addr {
68 	__be16	proto;
69 	__be16	port;
70 	union {
71 		struct in_addr ipv4;
72 		struct in6_addr ipv6;
73 	};
74 };
75 
76 /* struct udp_replicast - container for UDP remote addresses */
77 struct udp_replicast {
78 	struct udp_media_addr addr;
79 	struct dst_cache dst_cache;
80 	struct rcu_head rcu;
81 	struct list_head list;
82 };
83 
84 /**
85  * struct udp_bearer - ip/udp bearer data structure
86  * @bearer:	associated generic tipc bearer
87  * @ubsock:	bearer associated socket
88  * @ifindex:	local address scope
89  * @work:	used to schedule deferred work on a bearer
90  */
91 struct udp_bearer {
92 	struct tipc_bearer __rcu *bearer;
93 	struct socket *ubsock;
94 	u32 ifindex;
95 	struct work_struct work;
96 	struct udp_replicast rcast;
97 };
98 
tipc_udp_is_mcast_addr(struct udp_media_addr * addr)99 static int tipc_udp_is_mcast_addr(struct udp_media_addr *addr)
100 {
101 	if (ntohs(addr->proto) == ETH_P_IP)
102 		return ipv4_is_multicast(addr->ipv4.s_addr);
103 #if IS_ENABLED(CONFIG_IPV6)
104 	else
105 		return ipv6_addr_is_multicast(&addr->ipv6);
106 #endif
107 	return 0;
108 }
109 
110 /* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
tipc_udp_media_addr_set(struct tipc_media_addr * addr,struct udp_media_addr * ua)111 static void tipc_udp_media_addr_set(struct tipc_media_addr *addr,
112 				    struct udp_media_addr *ua)
113 {
114 	memset(addr, 0, sizeof(struct tipc_media_addr));
115 	addr->media_id = TIPC_MEDIA_TYPE_UDP;
116 	memcpy(addr->value, ua, sizeof(struct udp_media_addr));
117 
118 	if (tipc_udp_is_mcast_addr(ua))
119 		addr->broadcast = TIPC_BROADCAST_SUPPORT;
120 }
121 
122 /* tipc_udp_addr2str - convert ip/udp address to string */
tipc_udp_addr2str(struct tipc_media_addr * a,char * buf,int size)123 static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size)
124 {
125 	struct udp_media_addr *ua = (struct udp_media_addr *)&a->value;
126 
127 	if (ntohs(ua->proto) == ETH_P_IP)
128 		snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port));
129 	else if (ntohs(ua->proto) == ETH_P_IPV6)
130 		snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port));
131 	else
132 		pr_err("Invalid UDP media address\n");
133 	return 0;
134 }
135 
136 /* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
tipc_udp_msg2addr(struct tipc_bearer * b,struct tipc_media_addr * a,char * msg)137 static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a,
138 			     char *msg)
139 {
140 	struct udp_media_addr *ua;
141 
142 	ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET);
143 	if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP)
144 		return -EINVAL;
145 	tipc_udp_media_addr_set(a, ua);
146 	return 0;
147 }
148 
149 /* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
tipc_udp_addr2msg(char * msg,struct tipc_media_addr * a)150 static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a)
151 {
152 	memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
153 	msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP;
154 	memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value,
155 	       sizeof(struct udp_media_addr));
156 	return 0;
157 }
158 
159 /* tipc_send_msg - enqueue a send request */
tipc_udp_xmit(struct net * net,struct sk_buff * skb,struct udp_bearer * ub,struct udp_media_addr * src,struct udp_media_addr * dst,struct dst_cache * cache)160 static int tipc_udp_xmit(struct net *net, struct sk_buff *skb,
161 			 struct udp_bearer *ub, struct udp_media_addr *src,
162 			 struct udp_media_addr *dst, struct dst_cache *cache)
163 {
164 	struct dst_entry *ndst = dst_cache_get(cache);
165 	int ttl, err = 0;
166 
167 	if (dst->proto == htons(ETH_P_IP)) {
168 		struct rtable *rt = (struct rtable *)ndst;
169 
170 		if (!rt) {
171 			struct flowi4 fl = {
172 				.daddr = dst->ipv4.s_addr,
173 				.saddr = src->ipv4.s_addr,
174 				.flowi4_mark = skb->mark,
175 				.flowi4_proto = IPPROTO_UDP
176 			};
177 			rt = ip_route_output_key(net, &fl);
178 			if (IS_ERR(rt)) {
179 				err = PTR_ERR(rt);
180 				goto tx_error;
181 			}
182 			dst_cache_set_ip4(cache, &rt->dst, fl.saddr);
183 		}
184 
185 		ttl = ip4_dst_hoplimit(&rt->dst);
186 		udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr,
187 				    dst->ipv4.s_addr, 0, ttl, 0, src->port,
188 				    dst->port, false, true);
189 #if IS_ENABLED(CONFIG_IPV6)
190 	} else {
191 		if (!ndst) {
192 			struct flowi6 fl6 = {
193 				.flowi6_oif = ub->ifindex,
194 				.daddr = dst->ipv6,
195 				.saddr = src->ipv6,
196 				.flowi6_proto = IPPROTO_UDP
197 			};
198 			ndst = ipv6_stub->ipv6_dst_lookup_flow(net,
199 							       ub->ubsock->sk,
200 							       &fl6, NULL);
201 			if (IS_ERR(ndst)) {
202 				err = PTR_ERR(ndst);
203 				goto tx_error;
204 			}
205 			dst_cache_set_ip6(cache, ndst, &fl6.saddr);
206 		}
207 		ttl = ip6_dst_hoplimit(ndst);
208 		err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb, NULL,
209 					   &src->ipv6, &dst->ipv6, 0, ttl, 0,
210 					   src->port, dst->port, false);
211 #endif
212 	}
213 	return err;
214 
215 tx_error:
216 	kfree_skb(skb);
217 	return err;
218 }
219 
tipc_udp_send_msg(struct net * net,struct sk_buff * skb,struct tipc_bearer * b,struct tipc_media_addr * addr)220 static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
221 			     struct tipc_bearer *b,
222 			     struct tipc_media_addr *addr)
223 {
224 	struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
225 	struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value;
226 	struct udp_replicast *rcast;
227 	struct udp_bearer *ub;
228 	int err = 0;
229 
230 	if (skb_headroom(skb) < UDP_MIN_HEADROOM) {
231 		err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC);
232 		if (err)
233 			goto out;
234 	}
235 
236 	skb_set_inner_protocol(skb, htons(ETH_P_TIPC));
237 	ub = rcu_dereference(b->media_ptr);
238 	if (!ub) {
239 		err = -ENODEV;
240 		goto out;
241 	}
242 
243 	if (addr->broadcast != TIPC_REPLICAST_SUPPORT)
244 		return tipc_udp_xmit(net, skb, ub, src, dst,
245 				     &ub->rcast.dst_cache);
246 
247 	/* Replicast, send an skb to each configured IP address */
248 	list_for_each_entry_rcu(rcast, &ub->rcast.list, list) {
249 		struct sk_buff *_skb;
250 
251 		_skb = pskb_copy(skb, GFP_ATOMIC);
252 		if (!_skb) {
253 			err = -ENOMEM;
254 			goto out;
255 		}
256 
257 		err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr,
258 				    &rcast->dst_cache);
259 		if (err)
260 			goto out;
261 	}
262 	err = 0;
263 out:
264 	kfree_skb(skb);
265 	return err;
266 }
267 
tipc_udp_is_known_peer(struct tipc_bearer * b,struct udp_media_addr * addr)268 static bool tipc_udp_is_known_peer(struct tipc_bearer *b,
269 				   struct udp_media_addr *addr)
270 {
271 	struct udp_replicast *rcast, *tmp;
272 	struct udp_bearer *ub;
273 
274 	ub = rcu_dereference_rtnl(b->media_ptr);
275 	if (!ub) {
276 		pr_err_ratelimited("UDP bearer instance not found\n");
277 		return false;
278 	}
279 
280 	list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
281 		if (!memcmp(&rcast->addr, addr, sizeof(struct udp_media_addr)))
282 			return true;
283 	}
284 
285 	return false;
286 }
287 
tipc_udp_rcast_add(struct tipc_bearer * b,struct udp_media_addr * addr)288 static int tipc_udp_rcast_add(struct tipc_bearer *b,
289 			      struct udp_media_addr *addr)
290 {
291 	struct udp_replicast *rcast;
292 	struct udp_bearer *ub;
293 
294 	ub = rcu_dereference_rtnl(b->media_ptr);
295 	if (!ub)
296 		return -ENODEV;
297 
298 	rcast = kmalloc(sizeof(*rcast), GFP_ATOMIC);
299 	if (!rcast)
300 		return -ENOMEM;
301 
302 	if (dst_cache_init(&rcast->dst_cache, GFP_ATOMIC)) {
303 		kfree(rcast);
304 		return -ENOMEM;
305 	}
306 
307 	memcpy(&rcast->addr, addr, sizeof(struct udp_media_addr));
308 
309 	if (ntohs(addr->proto) == ETH_P_IP)
310 		pr_info("New replicast peer: %pI4\n", &rcast->addr.ipv4);
311 #if IS_ENABLED(CONFIG_IPV6)
312 	else if (ntohs(addr->proto) == ETH_P_IPV6)
313 		pr_info("New replicast peer: %pI6\n", &rcast->addr.ipv6);
314 #endif
315 	b->bcast_addr.broadcast = TIPC_REPLICAST_SUPPORT;
316 	list_add_rcu(&rcast->list, &ub->rcast.list);
317 	return 0;
318 }
319 
tipc_udp_rcast_disc(struct tipc_bearer * b,struct sk_buff * skb)320 static int tipc_udp_rcast_disc(struct tipc_bearer *b, struct sk_buff *skb)
321 {
322 	struct udp_media_addr src = {0};
323 	struct udp_media_addr *dst;
324 
325 	dst = (struct udp_media_addr *)&b->bcast_addr.value;
326 	if (tipc_udp_is_mcast_addr(dst))
327 		return 0;
328 
329 	src.port = udp_hdr(skb)->source;
330 
331 	if (ip_hdr(skb)->version == 4) {
332 		struct iphdr *iphdr = ip_hdr(skb);
333 
334 		src.proto = htons(ETH_P_IP);
335 		src.ipv4.s_addr = iphdr->saddr;
336 		if (ipv4_is_multicast(iphdr->daddr))
337 			return 0;
338 #if IS_ENABLED(CONFIG_IPV6)
339 	} else if (ip_hdr(skb)->version == 6) {
340 		struct ipv6hdr *iphdr = ipv6_hdr(skb);
341 
342 		src.proto = htons(ETH_P_IPV6);
343 		src.ipv6 = iphdr->saddr;
344 		if (ipv6_addr_is_multicast(&iphdr->daddr))
345 			return 0;
346 #endif
347 	} else {
348 		return 0;
349 	}
350 
351 	if (likely(tipc_udp_is_known_peer(b, &src)))
352 		return 0;
353 
354 	return tipc_udp_rcast_add(b, &src);
355 }
356 
357 /* tipc_udp_recv - read data from bearer socket */
tipc_udp_recv(struct sock * sk,struct sk_buff * skb)358 static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
359 {
360 	struct udp_bearer *ub;
361 	struct tipc_bearer *b;
362 	struct tipc_msg *hdr;
363 	int err;
364 
365 	ub = rcu_dereference_sk_user_data(sk);
366 	if (!ub) {
367 		pr_err_ratelimited("Failed to get UDP bearer reference");
368 		goto out;
369 	}
370 	skb_pull(skb, sizeof(struct udphdr));
371 	hdr = buf_msg(skb);
372 
373 	b = rcu_dereference(ub->bearer);
374 	if (!b)
375 		goto out;
376 
377 	if (b && test_bit(0, &b->up)) {
378 		tipc_rcv(sock_net(sk), skb, b);
379 		return 0;
380 	}
381 
382 	if (unlikely(msg_user(hdr) == LINK_CONFIG)) {
383 		err = tipc_udp_rcast_disc(b, skb);
384 		if (err)
385 			goto out;
386 	}
387 
388 out:
389 	kfree_skb(skb);
390 	return 0;
391 }
392 
enable_mcast(struct udp_bearer * ub,struct udp_media_addr * remote)393 static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
394 {
395 	int err = 0;
396 	struct ip_mreqn mreqn;
397 	struct sock *sk = ub->ubsock->sk;
398 
399 	if (ntohs(remote->proto) == ETH_P_IP) {
400 		mreqn.imr_multiaddr = remote->ipv4;
401 		mreqn.imr_ifindex = ub->ifindex;
402 		err = ip_mc_join_group(sk, &mreqn);
403 #if IS_ENABLED(CONFIG_IPV6)
404 	} else {
405 		err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
406 						   &remote->ipv6);
407 #endif
408 	}
409 	return err;
410 }
411 
__tipc_nl_add_udp_addr(struct sk_buff * skb,struct udp_media_addr * addr,int nla_t)412 static int __tipc_nl_add_udp_addr(struct sk_buff *skb,
413 				  struct udp_media_addr *addr, int nla_t)
414 {
415 	if (ntohs(addr->proto) == ETH_P_IP) {
416 		struct sockaddr_in ip4;
417 
418 		memset(&ip4, 0, sizeof(ip4));
419 		ip4.sin_family = AF_INET;
420 		ip4.sin_port = addr->port;
421 		ip4.sin_addr.s_addr = addr->ipv4.s_addr;
422 		if (nla_put(skb, nla_t, sizeof(ip4), &ip4))
423 			return -EMSGSIZE;
424 
425 #if IS_ENABLED(CONFIG_IPV6)
426 	} else if (ntohs(addr->proto) == ETH_P_IPV6) {
427 		struct sockaddr_in6 ip6;
428 
429 		memset(&ip6, 0, sizeof(ip6));
430 		ip6.sin6_family = AF_INET6;
431 		ip6.sin6_port  = addr->port;
432 		memcpy(&ip6.sin6_addr, &addr->ipv6, sizeof(struct in6_addr));
433 		if (nla_put(skb, nla_t, sizeof(ip6), &ip6))
434 			return -EMSGSIZE;
435 #endif
436 	}
437 
438 	return 0;
439 }
440 
tipc_udp_nl_dump_remoteip(struct sk_buff * skb,struct netlink_callback * cb)441 int tipc_udp_nl_dump_remoteip(struct sk_buff *skb, struct netlink_callback *cb)
442 {
443 	u32 bid = cb->args[0];
444 	u32 skip_cnt = cb->args[1];
445 	u32 portid = NETLINK_CB(cb->skb).portid;
446 	struct udp_replicast *rcast, *tmp;
447 	struct tipc_bearer *b;
448 	struct udp_bearer *ub;
449 	void *hdr;
450 	int err;
451 	int i;
452 
453 	if (!bid && !skip_cnt) {
454 		struct net *net = sock_net(skb->sk);
455 		struct nlattr *battrs[TIPC_NLA_BEARER_MAX + 1];
456 		struct nlattr **attrs;
457 		char *bname;
458 
459 		err = tipc_nlmsg_parse(cb->nlh, &attrs);
460 		if (err)
461 			return err;
462 
463 		if (!attrs[TIPC_NLA_BEARER])
464 			return -EINVAL;
465 
466 		err = nla_parse_nested_deprecated(battrs, TIPC_NLA_BEARER_MAX,
467 						  attrs[TIPC_NLA_BEARER],
468 						  tipc_nl_bearer_policy, NULL);
469 		if (err)
470 			return err;
471 
472 		if (!battrs[TIPC_NLA_BEARER_NAME])
473 			return -EINVAL;
474 
475 		bname = nla_data(battrs[TIPC_NLA_BEARER_NAME]);
476 
477 		rtnl_lock();
478 		b = tipc_bearer_find(net, bname);
479 		if (!b) {
480 			rtnl_unlock();
481 			return -EINVAL;
482 		}
483 		bid = b->identity;
484 	} else {
485 		struct net *net = sock_net(skb->sk);
486 		struct tipc_net *tn = net_generic(net, tipc_net_id);
487 
488 		rtnl_lock();
489 		b = rtnl_dereference(tn->bearer_list[bid]);
490 		if (!b) {
491 			rtnl_unlock();
492 			return -EINVAL;
493 		}
494 	}
495 
496 	ub = rtnl_dereference(b->media_ptr);
497 	if (!ub) {
498 		rtnl_unlock();
499 		return -EINVAL;
500 	}
501 
502 	i = 0;
503 	list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
504 		if (i < skip_cnt)
505 			goto count;
506 
507 		hdr = genlmsg_put(skb, portid, cb->nlh->nlmsg_seq,
508 				  &tipc_genl_family, NLM_F_MULTI,
509 				  TIPC_NL_BEARER_GET);
510 		if (!hdr)
511 			goto done;
512 
513 		err = __tipc_nl_add_udp_addr(skb, &rcast->addr,
514 					     TIPC_NLA_UDP_REMOTE);
515 		if (err) {
516 			genlmsg_cancel(skb, hdr);
517 			goto done;
518 		}
519 		genlmsg_end(skb, hdr);
520 count:
521 		i++;
522 	}
523 done:
524 	rtnl_unlock();
525 	cb->args[0] = bid;
526 	cb->args[1] = i;
527 
528 	return skb->len;
529 }
530 
tipc_udp_nl_add_bearer_data(struct tipc_nl_msg * msg,struct tipc_bearer * b)531 int tipc_udp_nl_add_bearer_data(struct tipc_nl_msg *msg, struct tipc_bearer *b)
532 {
533 	struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
534 	struct udp_media_addr *dst;
535 	struct udp_bearer *ub;
536 	struct nlattr *nest;
537 
538 	ub = rtnl_dereference(b->media_ptr);
539 	if (!ub)
540 		return -ENODEV;
541 
542 	nest = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_UDP_OPTS);
543 	if (!nest)
544 		goto msg_full;
545 
546 	if (__tipc_nl_add_udp_addr(msg->skb, src, TIPC_NLA_UDP_LOCAL))
547 		goto msg_full;
548 
549 	dst = (struct udp_media_addr *)&b->bcast_addr.value;
550 	if (__tipc_nl_add_udp_addr(msg->skb, dst, TIPC_NLA_UDP_REMOTE))
551 		goto msg_full;
552 
553 	if (!list_empty(&ub->rcast.list)) {
554 		if (nla_put_flag(msg->skb, TIPC_NLA_UDP_MULTI_REMOTEIP))
555 			goto msg_full;
556 	}
557 
558 	nla_nest_end(msg->skb, nest);
559 	return 0;
560 msg_full:
561 	nla_nest_cancel(msg->skb, nest);
562 	return -EMSGSIZE;
563 }
564 
565 /**
566  * tipc_parse_udp_addr - build udp media address from netlink data
567  * @nlattr:	netlink attribute containing sockaddr storage aligned address
568  * @addr:	tipc media address to fill with address, port and protocol type
569  * @scope_id:	IPv6 scope id pointer, not NULL indicates it's required
570  */
571 
tipc_parse_udp_addr(struct nlattr * nla,struct udp_media_addr * addr,u32 * scope_id)572 static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr,
573 			       u32 *scope_id)
574 {
575 	struct sockaddr_storage sa;
576 
577 	nla_memcpy(&sa, nla, sizeof(sa));
578 	if (sa.ss_family == AF_INET) {
579 		struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa;
580 
581 		addr->proto = htons(ETH_P_IP);
582 		addr->port = ip4->sin_port;
583 		addr->ipv4.s_addr = ip4->sin_addr.s_addr;
584 		return 0;
585 
586 #if IS_ENABLED(CONFIG_IPV6)
587 	} else if (sa.ss_family == AF_INET6) {
588 		struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa;
589 
590 		addr->proto = htons(ETH_P_IPV6);
591 		addr->port = ip6->sin6_port;
592 		memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr));
593 
594 		/* Scope ID is only interesting for local addresses */
595 		if (scope_id) {
596 			int atype;
597 
598 			atype = ipv6_addr_type(&ip6->sin6_addr);
599 			if (__ipv6_addr_needs_scope_id(atype) &&
600 			    !ip6->sin6_scope_id) {
601 				return -EINVAL;
602 			}
603 
604 			*scope_id = ip6->sin6_scope_id ? : 0;
605 		}
606 
607 		return 0;
608 #endif
609 	}
610 	return -EADDRNOTAVAIL;
611 }
612 
tipc_udp_nl_bearer_add(struct tipc_bearer * b,struct nlattr * attr)613 int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr)
614 {
615 	int err;
616 	struct udp_media_addr addr = {0};
617 	struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
618 	struct udp_media_addr *dst;
619 
620 	if (nla_parse_nested_deprecated(opts, TIPC_NLA_UDP_MAX, attr, tipc_nl_udp_policy, NULL))
621 		return -EINVAL;
622 
623 	if (!opts[TIPC_NLA_UDP_REMOTE])
624 		return -EINVAL;
625 
626 	err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL);
627 	if (err)
628 		return err;
629 
630 	dst = (struct udp_media_addr *)&b->bcast_addr.value;
631 	if (tipc_udp_is_mcast_addr(dst)) {
632 		pr_err("Can't add remote ip to TIPC UDP multicast bearer\n");
633 		return -EINVAL;
634 	}
635 
636 	if (tipc_udp_is_known_peer(b, &addr))
637 		return 0;
638 
639 	return tipc_udp_rcast_add(b, &addr);
640 }
641 
642 /**
643  * tipc_udp_enable - callback to create a new udp bearer instance
644  * @net:	network namespace
645  * @b:		pointer to generic tipc_bearer
646  * @attrs:	netlink bearer configuration
647  *
648  * validate the bearer parameters and initialize the udp bearer
649  * rtnl_lock should be held
650  */
tipc_udp_enable(struct net * net,struct tipc_bearer * b,struct nlattr * attrs[])651 static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
652 			   struct nlattr *attrs[])
653 {
654 	int err = -EINVAL;
655 	struct udp_bearer *ub;
656 	struct udp_media_addr remote = {0};
657 	struct udp_media_addr local = {0};
658 	struct udp_port_cfg udp_conf = {0};
659 	struct udp_tunnel_sock_cfg tuncfg = {NULL};
660 	struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
661 	u8 node_id[NODE_ID_LEN] = {0,};
662 	int rmcast = 0;
663 
664 	ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
665 	if (!ub)
666 		return -ENOMEM;
667 
668 	INIT_LIST_HEAD(&ub->rcast.list);
669 
670 	if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
671 		goto err;
672 
673 	if (nla_parse_nested_deprecated(opts, TIPC_NLA_UDP_MAX, attrs[TIPC_NLA_BEARER_UDP_OPTS], tipc_nl_udp_policy, NULL))
674 		goto err;
675 
676 	if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) {
677 		pr_err("Invalid UDP bearer configuration");
678 		err = -EINVAL;
679 		goto err;
680 	}
681 
682 	err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local,
683 				  &ub->ifindex);
684 	if (err)
685 		goto err;
686 
687 	err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL);
688 	if (err)
689 		goto err;
690 
691 	if (remote.proto != local.proto) {
692 		err = -EINVAL;
693 		goto err;
694 	}
695 
696 	/* Checking remote ip address */
697 	rmcast = tipc_udp_is_mcast_addr(&remote);
698 
699 	/* Autoconfigure own node identity if needed */
700 	if (!tipc_own_id(net)) {
701 		memcpy(node_id, local.ipv6.in6_u.u6_addr8, 16);
702 		tipc_net_init(net, node_id, 0);
703 	}
704 	if (!tipc_own_id(net)) {
705 		pr_warn("Failed to set node id, please configure manually\n");
706 		err = -EINVAL;
707 		goto err;
708 	}
709 
710 	b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
711 	b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
712 	rcu_assign_pointer(b->media_ptr, ub);
713 	rcu_assign_pointer(ub->bearer, b);
714 	tipc_udp_media_addr_set(&b->addr, &local);
715 	if (local.proto == htons(ETH_P_IP)) {
716 		struct net_device *dev;
717 
718 		dev = __ip_dev_find(net, local.ipv4.s_addr, false);
719 		if (!dev) {
720 			err = -ENODEV;
721 			goto err;
722 		}
723 		udp_conf.family = AF_INET;
724 
725 		/* Switch to use ANY to receive packets from group */
726 		if (rmcast)
727 			udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
728 		else
729 			udp_conf.local_ip.s_addr = local.ipv4.s_addr;
730 		udp_conf.use_udp_checksums = false;
731 		ub->ifindex = dev->ifindex;
732 		if (tipc_mtu_bad(dev, sizeof(struct iphdr) +
733 				      sizeof(struct udphdr))) {
734 			err = -EINVAL;
735 			goto err;
736 		}
737 		b->mtu = b->media->mtu;
738 #if IS_ENABLED(CONFIG_IPV6)
739 	} else if (local.proto == htons(ETH_P_IPV6)) {
740 		udp_conf.family = AF_INET6;
741 		udp_conf.use_udp6_tx_checksums = true;
742 		udp_conf.use_udp6_rx_checksums = true;
743 		if (rmcast)
744 			udp_conf.local_ip6 = in6addr_any;
745 		else
746 			udp_conf.local_ip6 = local.ipv6;
747 		b->mtu = 1280;
748 #endif
749 	} else {
750 		err = -EAFNOSUPPORT;
751 		goto err;
752 	}
753 	udp_conf.local_udp_port = local.port;
754 	err = udp_sock_create(net, &udp_conf, &ub->ubsock);
755 	if (err)
756 		goto err;
757 	tuncfg.sk_user_data = ub;
758 	tuncfg.encap_type = 1;
759 	tuncfg.encap_rcv = tipc_udp_recv;
760 	tuncfg.encap_destroy = NULL;
761 	setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
762 
763 	err = dst_cache_init(&ub->rcast.dst_cache, GFP_ATOMIC);
764 	if (err)
765 		goto free;
766 
767 	/**
768 	 * The bcast media address port is used for all peers and the ip
769 	 * is used if it's a multicast address.
770 	 */
771 	memcpy(&b->bcast_addr.value, &remote, sizeof(remote));
772 	if (rmcast)
773 		err = enable_mcast(ub, &remote);
774 	else
775 		err = tipc_udp_rcast_add(b, &remote);
776 	if (err)
777 		goto free;
778 
779 	return 0;
780 
781 free:
782 	dst_cache_destroy(&ub->rcast.dst_cache);
783 	udp_tunnel_sock_release(ub->ubsock);
784 err:
785 	kfree(ub);
786 	return err;
787 }
788 
789 /* cleanup_bearer - break the socket/bearer association */
cleanup_bearer(struct work_struct * work)790 static void cleanup_bearer(struct work_struct *work)
791 {
792 	struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
793 	struct udp_replicast *rcast, *tmp;
794 
795 	list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
796 		dst_cache_destroy(&rcast->dst_cache);
797 		list_del_rcu(&rcast->list);
798 		kfree_rcu(rcast, rcu);
799 	}
800 
801 	dst_cache_destroy(&ub->rcast.dst_cache);
802 	udp_tunnel_sock_release(ub->ubsock);
803 	synchronize_net();
804 	kfree(ub);
805 }
806 
807 /* tipc_udp_disable - detach bearer from socket */
tipc_udp_disable(struct tipc_bearer * b)808 static void tipc_udp_disable(struct tipc_bearer *b)
809 {
810 	struct udp_bearer *ub;
811 
812 	ub = rtnl_dereference(b->media_ptr);
813 	if (!ub) {
814 		pr_err("UDP bearer instance not found\n");
815 		return;
816 	}
817 	sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
818 	RCU_INIT_POINTER(ub->bearer, NULL);
819 
820 	/* sock_release need to be done outside of rtnl lock */
821 	INIT_WORK(&ub->work, cleanup_bearer);
822 	schedule_work(&ub->work);
823 }
824 
825 struct tipc_media udp_media_info = {
826 	.send_msg	= tipc_udp_send_msg,
827 	.enable_media	= tipc_udp_enable,
828 	.disable_media	= tipc_udp_disable,
829 	.addr2str	= tipc_udp_addr2str,
830 	.addr2msg	= tipc_udp_addr2msg,
831 	.msg2addr	= tipc_udp_msg2addr,
832 	.priority	= TIPC_DEF_LINK_PRI,
833 	.tolerance	= TIPC_DEF_LINK_TOL,
834 	.window		= TIPC_DEF_LINK_WIN,
835 	.mtu		= TIPC_DEF_LINK_UDP_MTU,
836 	.type_id	= TIPC_MEDIA_TYPE_UDP,
837 	.hwaddr_len	= 0,
838 	.name		= "udp"
839 };
840