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