xref: /linux/net/tipc/udp_media.c (revision 856e7c4b619af622d56b3b454f7bec32a170ac99)
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 "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 			kfree_skb(_skb);
250 			goto out;
251 		}
252 	}
253 	err = 0;
254 out:
255 	kfree_skb(skb);
256 	return err;
257 }
258 
259 static bool tipc_udp_is_known_peer(struct tipc_bearer *b,
260 				   struct udp_media_addr *addr)
261 {
262 	struct udp_replicast *rcast, *tmp;
263 	struct udp_bearer *ub;
264 
265 	ub = rcu_dereference_rtnl(b->media_ptr);
266 	if (!ub) {
267 		pr_err_ratelimited("UDP bearer instance not found\n");
268 		return false;
269 	}
270 
271 	list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
272 		if (!memcmp(&rcast->addr, addr, sizeof(struct udp_media_addr)))
273 			return true;
274 	}
275 
276 	return false;
277 }
278 
279 static int tipc_udp_rcast_add(struct tipc_bearer *b,
280 			      struct udp_media_addr *addr)
281 {
282 	struct udp_replicast *rcast;
283 	struct udp_bearer *ub;
284 
285 	ub = rcu_dereference_rtnl(b->media_ptr);
286 	if (!ub)
287 		return -ENODEV;
288 
289 	rcast = kmalloc(sizeof(*rcast), GFP_ATOMIC);
290 	if (!rcast)
291 		return -ENOMEM;
292 
293 	memcpy(&rcast->addr, addr, sizeof(struct udp_media_addr));
294 
295 	if (ntohs(addr->proto) == ETH_P_IP)
296 		pr_info("New replicast peer: %pI4\n", &rcast->addr.ipv4);
297 #if IS_ENABLED(CONFIG_IPV6)
298 	else if (ntohs(addr->proto) == ETH_P_IPV6)
299 		pr_info("New replicast peer: %pI6\n", &rcast->addr.ipv6);
300 #endif
301 	b->bcast_addr.broadcast = TIPC_REPLICAST_SUPPORT;
302 	list_add_rcu(&rcast->list, &ub->rcast.list);
303 	return 0;
304 }
305 
306 static int tipc_udp_rcast_disc(struct tipc_bearer *b, struct sk_buff *skb)
307 {
308 	struct udp_media_addr src = {0};
309 	struct udp_media_addr *dst;
310 
311 	dst = (struct udp_media_addr *)&b->bcast_addr.value;
312 	if (tipc_udp_is_mcast_addr(dst))
313 		return 0;
314 
315 	src.port = udp_hdr(skb)->source;
316 
317 	if (ip_hdr(skb)->version == 4) {
318 		struct iphdr *iphdr = ip_hdr(skb);
319 
320 		src.proto = htons(ETH_P_IP);
321 		src.ipv4.s_addr = iphdr->saddr;
322 		if (ipv4_is_multicast(iphdr->daddr))
323 			return 0;
324 #if IS_ENABLED(CONFIG_IPV6)
325 	} else if (ip_hdr(skb)->version == 6) {
326 		struct ipv6hdr *iphdr = ipv6_hdr(skb);
327 
328 		src.proto = htons(ETH_P_IPV6);
329 		src.ipv6 = iphdr->saddr;
330 		if (ipv6_addr_is_multicast(&iphdr->daddr))
331 			return 0;
332 #endif
333 	} else {
334 		return 0;
335 	}
336 
337 	if (likely(tipc_udp_is_known_peer(b, &src)))
338 		return 0;
339 
340 	return tipc_udp_rcast_add(b, &src);
341 }
342 
343 /* tipc_udp_recv - read data from bearer socket */
344 static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
345 {
346 	struct udp_bearer *ub;
347 	struct tipc_bearer *b;
348 	struct tipc_msg *hdr;
349 	int err;
350 
351 	ub = rcu_dereference_sk_user_data(sk);
352 	if (!ub) {
353 		pr_err_ratelimited("Failed to get UDP bearer reference");
354 		goto out;
355 	}
356 	skb_pull(skb, sizeof(struct udphdr));
357 	hdr = buf_msg(skb);
358 
359 	rcu_read_lock();
360 	b = rcu_dereference_rtnl(ub->bearer);
361 	if (!b)
362 		goto rcu_out;
363 
364 	if (b && test_bit(0, &b->up)) {
365 		tipc_rcv(sock_net(sk), skb, b);
366 		rcu_read_unlock();
367 		return 0;
368 	}
369 
370 	if (unlikely(msg_user(hdr) == LINK_CONFIG)) {
371 		err = tipc_udp_rcast_disc(b, skb);
372 		if (err)
373 			goto rcu_out;
374 	}
375 
376 rcu_out:
377 	rcu_read_unlock();
378 out:
379 	kfree_skb(skb);
380 	return 0;
381 }
382 
383 static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
384 {
385 	int err = 0;
386 	struct ip_mreqn mreqn;
387 	struct sock *sk = ub->ubsock->sk;
388 
389 	if (ntohs(remote->proto) == ETH_P_IP) {
390 		mreqn.imr_multiaddr = remote->ipv4;
391 		mreqn.imr_ifindex = ub->ifindex;
392 		err = ip_mc_join_group(sk, &mreqn);
393 #if IS_ENABLED(CONFIG_IPV6)
394 	} else {
395 		err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
396 						   &remote->ipv6);
397 #endif
398 	}
399 	return err;
400 }
401 
402 static int __tipc_nl_add_udp_addr(struct sk_buff *skb,
403 				  struct udp_media_addr *addr, int nla_t)
404 {
405 	if (ntohs(addr->proto) == ETH_P_IP) {
406 		struct sockaddr_in ip4;
407 
408 		memset(&ip4, 0, sizeof(ip4));
409 		ip4.sin_family = AF_INET;
410 		ip4.sin_port = addr->port;
411 		ip4.sin_addr.s_addr = addr->ipv4.s_addr;
412 		if (nla_put(skb, nla_t, sizeof(ip4), &ip4))
413 			return -EMSGSIZE;
414 
415 #if IS_ENABLED(CONFIG_IPV6)
416 	} else if (ntohs(addr->proto) == ETH_P_IPV6) {
417 		struct sockaddr_in6 ip6;
418 
419 		memset(&ip6, 0, sizeof(ip6));
420 		ip6.sin6_family = AF_INET6;
421 		ip6.sin6_port  = addr->port;
422 		memcpy(&ip6.sin6_addr, &addr->ipv6, sizeof(struct in6_addr));
423 		if (nla_put(skb, nla_t, sizeof(ip6), &ip6))
424 			return -EMSGSIZE;
425 #endif
426 	}
427 
428 	return 0;
429 }
430 
431 int tipc_udp_nl_dump_remoteip(struct sk_buff *skb, struct netlink_callback *cb)
432 {
433 	u32 bid = cb->args[0];
434 	u32 skip_cnt = cb->args[1];
435 	u32 portid = NETLINK_CB(cb->skb).portid;
436 	struct udp_replicast *rcast, *tmp;
437 	struct tipc_bearer *b;
438 	struct udp_bearer *ub;
439 	void *hdr;
440 	int err;
441 	int i;
442 
443 	if (!bid && !skip_cnt) {
444 		struct net *net = sock_net(skb->sk);
445 		struct nlattr *battrs[TIPC_NLA_BEARER_MAX + 1];
446 		struct nlattr **attrs;
447 		char *bname;
448 
449 		err = tipc_nlmsg_parse(cb->nlh, &attrs);
450 		if (err)
451 			return err;
452 
453 		if (!attrs[TIPC_NLA_BEARER])
454 			return -EINVAL;
455 
456 		err = nla_parse_nested(battrs, TIPC_NLA_BEARER_MAX,
457 				       attrs[TIPC_NLA_BEARER],
458 				       tipc_nl_bearer_policy, NULL);
459 		if (err)
460 			return err;
461 
462 		if (!battrs[TIPC_NLA_BEARER_NAME])
463 			return -EINVAL;
464 
465 		bname = nla_data(battrs[TIPC_NLA_BEARER_NAME]);
466 
467 		rtnl_lock();
468 		b = tipc_bearer_find(net, bname);
469 		if (!b) {
470 			rtnl_unlock();
471 			return -EINVAL;
472 		}
473 		bid = b->identity;
474 	} else {
475 		struct net *net = sock_net(skb->sk);
476 		struct tipc_net *tn = net_generic(net, tipc_net_id);
477 
478 		rtnl_lock();
479 		b = rtnl_dereference(tn->bearer_list[bid]);
480 		if (!b) {
481 			rtnl_unlock();
482 			return -EINVAL;
483 		}
484 	}
485 
486 	ub = rcu_dereference_rtnl(b->media_ptr);
487 	if (!ub) {
488 		rtnl_unlock();
489 		return -EINVAL;
490 	}
491 
492 	i = 0;
493 	list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
494 		if (i < skip_cnt)
495 			goto count;
496 
497 		hdr = genlmsg_put(skb, portid, cb->nlh->nlmsg_seq,
498 				  &tipc_genl_family, NLM_F_MULTI,
499 				  TIPC_NL_BEARER_GET);
500 		if (!hdr)
501 			goto done;
502 
503 		err = __tipc_nl_add_udp_addr(skb, &rcast->addr,
504 					     TIPC_NLA_UDP_REMOTE);
505 		if (err) {
506 			genlmsg_cancel(skb, hdr);
507 			goto done;
508 		}
509 		genlmsg_end(skb, hdr);
510 count:
511 		i++;
512 	}
513 done:
514 	rtnl_unlock();
515 	cb->args[0] = bid;
516 	cb->args[1] = i;
517 
518 	return skb->len;
519 }
520 
521 int tipc_udp_nl_add_bearer_data(struct tipc_nl_msg *msg, struct tipc_bearer *b)
522 {
523 	struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
524 	struct udp_media_addr *dst;
525 	struct udp_bearer *ub;
526 	struct nlattr *nest;
527 
528 	ub = rcu_dereference_rtnl(b->media_ptr);
529 	if (!ub)
530 		return -ENODEV;
531 
532 	nest = nla_nest_start(msg->skb, TIPC_NLA_BEARER_UDP_OPTS);
533 	if (!nest)
534 		goto msg_full;
535 
536 	if (__tipc_nl_add_udp_addr(msg->skb, src, TIPC_NLA_UDP_LOCAL))
537 		goto msg_full;
538 
539 	dst = (struct udp_media_addr *)&b->bcast_addr.value;
540 	if (__tipc_nl_add_udp_addr(msg->skb, dst, TIPC_NLA_UDP_REMOTE))
541 		goto msg_full;
542 
543 	if (!list_empty(&ub->rcast.list)) {
544 		if (nla_put_flag(msg->skb, TIPC_NLA_UDP_MULTI_REMOTEIP))
545 			goto msg_full;
546 	}
547 
548 	nla_nest_end(msg->skb, nest);
549 	return 0;
550 msg_full:
551 	nla_nest_cancel(msg->skb, nest);
552 	return -EMSGSIZE;
553 }
554 
555 /**
556  * tipc_parse_udp_addr - build udp media address from netlink data
557  * @nlattr:	netlink attribute containing sockaddr storage aligned address
558  * @addr:	tipc media address to fill with address, port and protocol type
559  * @scope_id:	IPv6 scope id pointer, not NULL indicates it's required
560  */
561 
562 static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr,
563 			       u32 *scope_id)
564 {
565 	struct sockaddr_storage sa;
566 
567 	nla_memcpy(&sa, nla, sizeof(sa));
568 	if (sa.ss_family == AF_INET) {
569 		struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa;
570 
571 		addr->proto = htons(ETH_P_IP);
572 		addr->port = ip4->sin_port;
573 		addr->ipv4.s_addr = ip4->sin_addr.s_addr;
574 		return 0;
575 
576 #if IS_ENABLED(CONFIG_IPV6)
577 	} else if (sa.ss_family == AF_INET6) {
578 		struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa;
579 
580 		addr->proto = htons(ETH_P_IPV6);
581 		addr->port = ip6->sin6_port;
582 		memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr));
583 
584 		/* Scope ID is only interesting for local addresses */
585 		if (scope_id) {
586 			int atype;
587 
588 			atype = ipv6_addr_type(&ip6->sin6_addr);
589 			if (__ipv6_addr_needs_scope_id(atype) &&
590 			    !ip6->sin6_scope_id) {
591 				return -EINVAL;
592 			}
593 
594 			*scope_id = ip6->sin6_scope_id ? : 0;
595 		}
596 
597 		return 0;
598 #endif
599 	}
600 	return -EADDRNOTAVAIL;
601 }
602 
603 int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr)
604 {
605 	int err;
606 	struct udp_media_addr addr = {0};
607 	struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
608 	struct udp_media_addr *dst;
609 
610 	if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX, attr,
611 			     tipc_nl_udp_policy, NULL))
612 		return -EINVAL;
613 
614 	if (!opts[TIPC_NLA_UDP_REMOTE])
615 		return -EINVAL;
616 
617 	err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL);
618 	if (err)
619 		return err;
620 
621 	dst = (struct udp_media_addr *)&b->bcast_addr.value;
622 	if (tipc_udp_is_mcast_addr(dst)) {
623 		pr_err("Can't add remote ip to TIPC UDP multicast bearer\n");
624 		return -EINVAL;
625 	}
626 
627 	if (tipc_udp_is_known_peer(b, &addr))
628 		return 0;
629 
630 	return tipc_udp_rcast_add(b, &addr);
631 }
632 
633 /**
634  * tipc_udp_enable - callback to create a new udp bearer instance
635  * @net:	network namespace
636  * @b:		pointer to generic tipc_bearer
637  * @attrs:	netlink bearer configuration
638  *
639  * validate the bearer parameters and initialize the udp bearer
640  * rtnl_lock should be held
641  */
642 static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
643 			   struct nlattr *attrs[])
644 {
645 	int err = -EINVAL;
646 	struct udp_bearer *ub;
647 	struct udp_media_addr remote = {0};
648 	struct udp_media_addr local = {0};
649 	struct udp_port_cfg udp_conf = {0};
650 	struct udp_tunnel_sock_cfg tuncfg = {NULL};
651 	struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
652 	u8 node_id[NODE_ID_LEN] = {0,};
653 
654 	ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
655 	if (!ub)
656 		return -ENOMEM;
657 
658 	INIT_LIST_HEAD(&ub->rcast.list);
659 
660 	if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
661 		goto err;
662 
663 	if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX,
664 			     attrs[TIPC_NLA_BEARER_UDP_OPTS],
665 			     tipc_nl_udp_policy, NULL))
666 		goto err;
667 
668 	if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) {
669 		pr_err("Invalid UDP bearer configuration");
670 		err = -EINVAL;
671 		goto err;
672 	}
673 
674 	err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local,
675 				  &ub->ifindex);
676 	if (err)
677 		goto err;
678 
679 	err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL);
680 	if (err)
681 		goto err;
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 		udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
709 		udp_conf.use_udp_checksums = false;
710 		ub->ifindex = dev->ifindex;
711 		if (tipc_mtu_bad(dev, sizeof(struct iphdr) +
712 				      sizeof(struct udphdr))) {
713 			err = -EINVAL;
714 			goto err;
715 		}
716 		b->mtu = b->media->mtu;
717 #if IS_ENABLED(CONFIG_IPV6)
718 	} else if (local.proto == htons(ETH_P_IPV6)) {
719 		udp_conf.family = AF_INET6;
720 		udp_conf.use_udp6_tx_checksums = true;
721 		udp_conf.use_udp6_rx_checksums = true;
722 		udp_conf.local_ip6 = in6addr_any;
723 		b->mtu = 1280;
724 #endif
725 	} else {
726 		err = -EAFNOSUPPORT;
727 		goto err;
728 	}
729 	udp_conf.local_udp_port = local.port;
730 	err = udp_sock_create(net, &udp_conf, &ub->ubsock);
731 	if (err)
732 		goto err;
733 	tuncfg.sk_user_data = ub;
734 	tuncfg.encap_type = 1;
735 	tuncfg.encap_rcv = tipc_udp_recv;
736 	tuncfg.encap_destroy = NULL;
737 	setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
738 
739 	/**
740 	 * The bcast media address port is used for all peers and the ip
741 	 * is used if it's a multicast address.
742 	 */
743 	memcpy(&b->bcast_addr.value, &remote, sizeof(remote));
744 	if (tipc_udp_is_mcast_addr(&remote))
745 		err = enable_mcast(ub, &remote);
746 	else
747 		err = tipc_udp_rcast_add(b, &remote);
748 	if (err)
749 		goto err;
750 
751 	return 0;
752 err:
753 	if (ub->ubsock)
754 		udp_tunnel_sock_release(ub->ubsock);
755 	kfree(ub);
756 	return err;
757 }
758 
759 /* cleanup_bearer - break the socket/bearer association */
760 static void cleanup_bearer(struct work_struct *work)
761 {
762 	struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
763 	struct udp_replicast *rcast, *tmp;
764 
765 	list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
766 		list_del_rcu(&rcast->list);
767 		kfree_rcu(rcast, rcu);
768 	}
769 
770 	if (ub->ubsock)
771 		udp_tunnel_sock_release(ub->ubsock);
772 	synchronize_net();
773 	kfree(ub);
774 }
775 
776 /* tipc_udp_disable - detach bearer from socket */
777 static void tipc_udp_disable(struct tipc_bearer *b)
778 {
779 	struct udp_bearer *ub;
780 
781 	ub = rcu_dereference_rtnl(b->media_ptr);
782 	if (!ub) {
783 		pr_err("UDP bearer instance not found\n");
784 		return;
785 	}
786 	if (ub->ubsock)
787 		sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
788 	RCU_INIT_POINTER(ub->bearer, NULL);
789 
790 	/* sock_release need to be done outside of rtnl lock */
791 	INIT_WORK(&ub->work, cleanup_bearer);
792 	schedule_work(&ub->work);
793 }
794 
795 struct tipc_media udp_media_info = {
796 	.send_msg	= tipc_udp_send_msg,
797 	.enable_media	= tipc_udp_enable,
798 	.disable_media	= tipc_udp_disable,
799 	.addr2str	= tipc_udp_addr2str,
800 	.addr2msg	= tipc_udp_addr2msg,
801 	.msg2addr	= tipc_udp_msg2addr,
802 	.priority	= TIPC_DEF_LINK_PRI,
803 	.tolerance	= TIPC_DEF_LINK_TOL,
804 	.window		= TIPC_DEF_LINK_WIN,
805 	.mtu		= TIPC_DEF_LINK_UDP_MTU,
806 	.type_id	= TIPC_MEDIA_TYPE_UDP,
807 	.hwaddr_len	= 0,
808 	.name		= "udp"
809 };
810