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