Linux Audio

Check our new training course

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