xref: /openbmc/linux/net/tipc/udp_media.c (revision f519f0be)
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 rcu_head rcu;
80 	struct list_head list;
81 };
82 
83 /**
84  * struct udp_bearer - ip/udp bearer data structure
85  * @bearer:	associated generic tipc bearer
86  * @ubsock:	bearer associated socket
87  * @ifindex:	local address scope
88  * @work:	used to schedule deferred work on a bearer
89  */
90 struct udp_bearer {
91 	struct tipc_bearer __rcu *bearer;
92 	struct socket *ubsock;
93 	u32 ifindex;
94 	struct work_struct work;
95 	struct udp_replicast rcast;
96 };
97 
98 static int tipc_udp_is_mcast_addr(struct udp_media_addr *addr)
99 {
100 	if (ntohs(addr->proto) == ETH_P_IP)
101 		return ipv4_is_multicast(addr->ipv4.s_addr);
102 #if IS_ENABLED(CONFIG_IPV6)
103 	else
104 		return ipv6_addr_is_multicast(&addr->ipv6);
105 #endif
106 	return 0;
107 }
108 
109 /* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
110 static void tipc_udp_media_addr_set(struct tipc_media_addr *addr,
111 				    struct udp_media_addr *ua)
112 {
113 	memset(addr, 0, sizeof(struct tipc_media_addr));
114 	addr->media_id = TIPC_MEDIA_TYPE_UDP;
115 	memcpy(addr->value, ua, sizeof(struct udp_media_addr));
116 
117 	if (tipc_udp_is_mcast_addr(ua))
118 		addr->broadcast = TIPC_BROADCAST_SUPPORT;
119 }
120 
121 /* tipc_udp_addr2str - convert ip/udp address to string */
122 static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size)
123 {
124 	struct udp_media_addr *ua = (struct udp_media_addr *)&a->value;
125 
126 	if (ntohs(ua->proto) == ETH_P_IP)
127 		snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port));
128 	else if (ntohs(ua->proto) == ETH_P_IPV6)
129 		snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port));
130 	else
131 		pr_err("Invalid UDP media address\n");
132 	return 0;
133 }
134 
135 /* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
136 static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a,
137 			     char *msg)
138 {
139 	struct udp_media_addr *ua;
140 
141 	ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET);
142 	if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP)
143 		return -EINVAL;
144 	tipc_udp_media_addr_set(a, ua);
145 	return 0;
146 }
147 
148 /* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
149 static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a)
150 {
151 	memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
152 	msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP;
153 	memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value,
154 	       sizeof(struct udp_media_addr));
155 	return 0;
156 }
157 
158 /* tipc_send_msg - enqueue a send request */
159 static int tipc_udp_xmit(struct net *net, struct sk_buff *skb,
160 			 struct udp_bearer *ub, struct udp_media_addr *src,
161 			 struct udp_media_addr *dst)
162 {
163 	int ttl, err = 0;
164 	struct rtable *rt;
165 
166 	if (dst->proto == htons(ETH_P_IP)) {
167 		struct flowi4 fl = {
168 			.daddr = dst->ipv4.s_addr,
169 			.saddr = src->ipv4.s_addr,
170 			.flowi4_mark = skb->mark,
171 			.flowi4_proto = IPPROTO_UDP
172 		};
173 		rt = ip_route_output_key(net, &fl);
174 		if (IS_ERR(rt)) {
175 			err = PTR_ERR(rt);
176 			goto tx_error;
177 		}
178 
179 		skb->dev = rt->dst.dev;
180 		ttl = ip4_dst_hoplimit(&rt->dst);
181 		udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr,
182 				    dst->ipv4.s_addr, 0, ttl, 0, src->port,
183 				    dst->port, false, true);
184 #if IS_ENABLED(CONFIG_IPV6)
185 	} else {
186 		struct dst_entry *ndst;
187 		struct flowi6 fl6 = {
188 			.flowi6_oif = ub->ifindex,
189 			.daddr = dst->ipv6,
190 			.saddr = src->ipv6,
191 			.flowi6_proto = IPPROTO_UDP
192 		};
193 		err = ipv6_stub->ipv6_dst_lookup(net, ub->ubsock->sk, &ndst,
194 						 &fl6);
195 		if (err)
196 			goto tx_error;
197 		ttl = ip6_dst_hoplimit(ndst);
198 		err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb,
199 					   ndst->dev, &src->ipv6,
200 					   &dst->ipv6, 0, ttl, 0, src->port,
201 					   dst->port, false);
202 #endif
203 	}
204 	return err;
205 
206 tx_error:
207 	kfree_skb(skb);
208 	return err;
209 }
210 
211 static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
212 			     struct tipc_bearer *b,
213 			     struct tipc_media_addr *addr)
214 {
215 	struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
216 	struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value;
217 	struct udp_replicast *rcast;
218 	struct udp_bearer *ub;
219 	int err = 0;
220 
221 	if (skb_headroom(skb) < UDP_MIN_HEADROOM) {
222 		err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC);
223 		if (err)
224 			goto out;
225 	}
226 
227 	skb_set_inner_protocol(skb, htons(ETH_P_TIPC));
228 	ub = rcu_dereference_rtnl(b->media_ptr);
229 	if (!ub) {
230 		err = -ENODEV;
231 		goto out;
232 	}
233 
234 	if (addr->broadcast != TIPC_REPLICAST_SUPPORT)
235 		return tipc_udp_xmit(net, skb, ub, src, dst);
236 
237 	/* Replicast, send an skb to each configured IP address */
238 	list_for_each_entry_rcu(rcast, &ub->rcast.list, list) {
239 		struct sk_buff *_skb;
240 
241 		_skb = pskb_copy(skb, GFP_ATOMIC);
242 		if (!_skb) {
243 			err = -ENOMEM;
244 			goto out;
245 		}
246 
247 		err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr);
248 		if (err)
249 			goto out;
250 	}
251 	err = 0;
252 out:
253 	kfree_skb(skb);
254 	return err;
255 }
256 
257 static 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 
277 static 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
299 	b->bcast_addr.broadcast = TIPC_REPLICAST_SUPPORT;
300 	list_add_rcu(&rcast->list, &ub->rcast.list);
301 	return 0;
302 }
303 
304 static 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 
341 /* tipc_udp_recv - read data from bearer socket */
342 static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
343 {
344 	struct udp_bearer *ub;
345 	struct tipc_bearer *b;
346 	struct tipc_msg *hdr;
347 	int err;
348 
349 	ub = rcu_dereference_sk_user_data(sk);
350 	if (!ub) {
351 		pr_err_ratelimited("Failed to get UDP bearer reference");
352 		goto out;
353 	}
354 	skb_pull(skb, sizeof(struct udphdr));
355 	hdr = buf_msg(skb);
356 
357 	b = rcu_dereference(ub->bearer);
358 	if (!b)
359 		goto out;
360 
361 	if (b && test_bit(0, &b->up)) {
362 		tipc_rcv(sock_net(sk), skb, b);
363 		return 0;
364 	}
365 
366 	if (unlikely(msg_user(hdr) == LINK_CONFIG)) {
367 		err = tipc_udp_rcast_disc(b, skb);
368 		if (err)
369 			goto out;
370 	}
371 
372 out:
373 	kfree_skb(skb);
374 	return 0;
375 }
376 
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 
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 
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_deprecated(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 
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_noflag(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 
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 
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_deprecated(opts, TIPC_NLA_UDP_MAX, attr, tipc_nl_udp_policy, NULL))
605 		return -EINVAL;
606 
607 	if (!opts[TIPC_NLA_UDP_REMOTE])
608 		return -EINVAL;
609 
610 	err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL);
611 	if (err)
612 		return err;
613 
614 	dst = (struct udp_media_addr *)&b->bcast_addr.value;
615 	if (tipc_udp_is_mcast_addr(dst)) {
616 		pr_err("Can't add remote ip to TIPC UDP multicast bearer\n");
617 		return -EINVAL;
618 	}
619 
620 	if (tipc_udp_is_known_peer(b, &addr))
621 		return 0;
622 
623 	return tipc_udp_rcast_add(b, &addr);
624 }
625 
626 /**
627  * tipc_udp_enable - callback to create a new udp bearer instance
628  * @net:	network namespace
629  * @b:		pointer to generic tipc_bearer
630  * @attrs:	netlink bearer configuration
631  *
632  * validate the bearer parameters and initialize the udp bearer
633  * rtnl_lock should be held
634  */
635 static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
636 			   struct nlattr *attrs[])
637 {
638 	int err = -EINVAL;
639 	struct udp_bearer *ub;
640 	struct udp_media_addr remote = {0};
641 	struct udp_media_addr local = {0};
642 	struct udp_port_cfg udp_conf = {0};
643 	struct udp_tunnel_sock_cfg tuncfg = {NULL};
644 	struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
645 	u8 node_id[NODE_ID_LEN] = {0,};
646 	int rmcast = 0;
647 
648 	ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
649 	if (!ub)
650 		return -ENOMEM;
651 
652 	INIT_LIST_HEAD(&ub->rcast.list);
653 
654 	if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
655 		goto err;
656 
657 	if (nla_parse_nested_deprecated(opts, TIPC_NLA_UDP_MAX, attrs[TIPC_NLA_BEARER_UDP_OPTS], tipc_nl_udp_policy, NULL))
658 		goto err;
659 
660 	if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) {
661 		pr_err("Invalid UDP bearer configuration");
662 		err = -EINVAL;
663 		goto err;
664 	}
665 
666 	err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local,
667 				  &ub->ifindex);
668 	if (err)
669 		goto err;
670 
671 	err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL);
672 	if (err)
673 		goto err;
674 
675 	if (remote.proto != local.proto) {
676 		err = -EINVAL;
677 		goto err;
678 	}
679 
680 	/* Checking remote ip address */
681 	rmcast = tipc_udp_is_mcast_addr(&remote);
682 
683 	/* Autoconfigure own node identity if needed */
684 	if (!tipc_own_id(net)) {
685 		memcpy(node_id, local.ipv6.in6_u.u6_addr8, 16);
686 		tipc_net_init(net, node_id, 0);
687 	}
688 	if (!tipc_own_id(net)) {
689 		pr_warn("Failed to set node id, please configure manually\n");
690 		err = -EINVAL;
691 		goto err;
692 	}
693 
694 	b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
695 	b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
696 	rcu_assign_pointer(b->media_ptr, ub);
697 	rcu_assign_pointer(ub->bearer, b);
698 	tipc_udp_media_addr_set(&b->addr, &local);
699 	if (local.proto == htons(ETH_P_IP)) {
700 		struct net_device *dev;
701 
702 		dev = __ip_dev_find(net, local.ipv4.s_addr, false);
703 		if (!dev) {
704 			err = -ENODEV;
705 			goto err;
706 		}
707 		udp_conf.family = AF_INET;
708 
709 		/* Switch to use ANY to receive packets from group */
710 		if (rmcast)
711 			udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
712 		else
713 			udp_conf.local_ip.s_addr = local.ipv4.s_addr;
714 		udp_conf.use_udp_checksums = false;
715 		ub->ifindex = dev->ifindex;
716 		if (tipc_mtu_bad(dev, sizeof(struct iphdr) +
717 				      sizeof(struct udphdr))) {
718 			err = -EINVAL;
719 			goto err;
720 		}
721 		b->mtu = b->media->mtu;
722 #if IS_ENABLED(CONFIG_IPV6)
723 	} else if (local.proto == htons(ETH_P_IPV6)) {
724 		udp_conf.family = AF_INET6;
725 		udp_conf.use_udp6_tx_checksums = true;
726 		udp_conf.use_udp6_rx_checksums = true;
727 		if (rmcast)
728 			udp_conf.local_ip6 = in6addr_any;
729 		else
730 			udp_conf.local_ip6 = local.ipv6;
731 		b->mtu = 1280;
732 #endif
733 	} else {
734 		err = -EAFNOSUPPORT;
735 		goto err;
736 	}
737 	udp_conf.local_udp_port = local.port;
738 	err = udp_sock_create(net, &udp_conf, &ub->ubsock);
739 	if (err)
740 		goto err;
741 	tuncfg.sk_user_data = ub;
742 	tuncfg.encap_type = 1;
743 	tuncfg.encap_rcv = tipc_udp_recv;
744 	tuncfg.encap_destroy = NULL;
745 	setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
746 
747 	/**
748 	 * The bcast media address port is used for all peers and the ip
749 	 * is used if it's a multicast address.
750 	 */
751 	memcpy(&b->bcast_addr.value, &remote, sizeof(remote));
752 	if (rmcast)
753 		err = enable_mcast(ub, &remote);
754 	else
755 		err = tipc_udp_rcast_add(b, &remote);
756 	if (err)
757 		goto err;
758 
759 	return 0;
760 err:
761 	if (ub->ubsock)
762 		udp_tunnel_sock_release(ub->ubsock);
763 	kfree(ub);
764 	return err;
765 }
766 
767 /* cleanup_bearer - break the socket/bearer association */
768 static void cleanup_bearer(struct work_struct *work)
769 {
770 	struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
771 	struct udp_replicast *rcast, *tmp;
772 
773 	list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
774 		list_del_rcu(&rcast->list);
775 		kfree_rcu(rcast, rcu);
776 	}
777 
778 	if (ub->ubsock)
779 		udp_tunnel_sock_release(ub->ubsock);
780 	synchronize_net();
781 	kfree(ub);
782 }
783 
784 /* tipc_udp_disable - detach bearer from socket */
785 static void tipc_udp_disable(struct tipc_bearer *b)
786 {
787 	struct udp_bearer *ub;
788 
789 	ub = rcu_dereference_rtnl(b->media_ptr);
790 	if (!ub) {
791 		pr_err("UDP bearer instance not found\n");
792 		return;
793 	}
794 	if (ub->ubsock)
795 		sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
796 	RCU_INIT_POINTER(ub->bearer, NULL);
797 
798 	/* sock_release need to be done outside of rtnl lock */
799 	INIT_WORK(&ub->work, cleanup_bearer);
800 	schedule_work(&ub->work);
801 }
802 
803 struct tipc_media udp_media_info = {
804 	.send_msg	= tipc_udp_send_msg,
805 	.enable_media	= tipc_udp_enable,
806 	.disable_media	= tipc_udp_disable,
807 	.addr2str	= tipc_udp_addr2str,
808 	.addr2msg	= tipc_udp_addr2msg,
809 	.msg2addr	= tipc_udp_msg2addr,
810 	.priority	= TIPC_DEF_LINK_PRI,
811 	.tolerance	= TIPC_DEF_LINK_TOL,
812 	.window		= TIPC_DEF_LINK_WIN,
813 	.mtu		= TIPC_DEF_LINK_UDP_MTU,
814 	.type_id	= TIPC_MEDIA_TYPE_UDP,
815 	.hwaddr_len	= 0,
816 	.name		= "udp"
817 };
818