net/netfilter/nf_conntrack_core: Fix net_conntrack_lock()
[linux/fpc-iii.git] / net / tipc / udp_media.c
blobecca64fc6a6f223bf8c0e09e3a299fe4fd62509d
1 /* net/tipc/udp_media.c: IP bearer support for TIPC
3 * Copyright (c) 2015, Ericsson AB
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
35 #include <linux/socket.h>
36 #include <linux/ip.h>
37 #include <linux/udp.h>
38 #include <linux/inet.h>
39 #include <linux/inetdevice.h>
40 #include <linux/igmp.h>
41 #include <linux/kernel.h>
42 #include <linux/workqueue.h>
43 #include <linux/list.h>
44 #include <net/sock.h>
45 #include <net/ip.h>
46 #include <net/udp_tunnel.h>
47 #include <net/addrconf.h>
48 #include <linux/tipc_netlink.h>
49 #include "core.h"
50 #include "bearer.h"
51 #include "netlink.h"
52 #include "msg.h"
54 /* IANA assigned UDP port */
55 #define UDP_PORT_DEFAULT 6118
57 #define UDP_MIN_HEADROOM 48
59 /**
60 * struct udp_media_addr - IP/UDP addressing information
62 * This is the bearer level originating address used in neighbor discovery
63 * messages, and all fields should be in network byte order
65 struct udp_media_addr {
66 __be16 proto;
67 __be16 port;
68 union {
69 struct in_addr ipv4;
70 struct in6_addr ipv6;
74 /* struct udp_replicast - container for UDP remote addresses */
75 struct udp_replicast {
76 struct udp_media_addr addr;
77 struct rcu_head rcu;
78 struct list_head list;
81 /**
82 * struct udp_bearer - ip/udp bearer data structure
83 * @bearer: associated generic tipc bearer
84 * @ubsock: bearer associated socket
85 * @ifindex: local address scope
86 * @work: used to schedule deferred work on a bearer
88 struct udp_bearer {
89 struct tipc_bearer __rcu *bearer;
90 struct socket *ubsock;
91 u32 ifindex;
92 struct work_struct work;
93 struct udp_replicast rcast;
96 static int tipc_udp_is_mcast_addr(struct udp_media_addr *addr)
98 if (ntohs(addr->proto) == ETH_P_IP)
99 return ipv4_is_multicast(addr->ipv4.s_addr);
100 #if IS_ENABLED(CONFIG_IPV6)
101 else
102 return ipv6_addr_is_multicast(&addr->ipv6);
103 #endif
104 return 0;
107 /* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
108 static void tipc_udp_media_addr_set(struct tipc_media_addr *addr,
109 struct udp_media_addr *ua)
111 memset(addr, 0, sizeof(struct tipc_media_addr));
112 addr->media_id = TIPC_MEDIA_TYPE_UDP;
113 memcpy(addr->value, ua, sizeof(struct udp_media_addr));
115 if (tipc_udp_is_mcast_addr(ua))
116 addr->broadcast = TIPC_BROADCAST_SUPPORT;
119 /* tipc_udp_addr2str - convert ip/udp address to string */
120 static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size)
122 struct udp_media_addr *ua = (struct udp_media_addr *)&a->value;
124 if (ntohs(ua->proto) == ETH_P_IP)
125 snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port));
126 else if (ntohs(ua->proto) == ETH_P_IPV6)
127 snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port));
128 else
129 pr_err("Invalid UDP media address\n");
130 return 0;
133 /* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
134 static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a,
135 char *msg)
137 struct udp_media_addr *ua;
139 ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET);
140 if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP)
141 return -EINVAL;
142 tipc_udp_media_addr_set(a, ua);
143 return 0;
146 /* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
147 static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a)
149 memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
150 msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP;
151 memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value,
152 sizeof(struct udp_media_addr));
153 return 0;
156 /* tipc_send_msg - enqueue a send request */
157 static int tipc_udp_xmit(struct net *net, struct sk_buff *skb,
158 struct udp_bearer *ub, struct udp_media_addr *src,
159 struct udp_media_addr *dst)
161 int ttl, err = 0;
162 struct rtable *rt;
164 if (dst->proto == htons(ETH_P_IP)) {
165 struct flowi4 fl = {
166 .daddr = dst->ipv4.s_addr,
167 .saddr = src->ipv4.s_addr,
168 .flowi4_mark = skb->mark,
169 .flowi4_proto = IPPROTO_UDP
171 rt = ip_route_output_key(net, &fl);
172 if (IS_ERR(rt)) {
173 err = PTR_ERR(rt);
174 goto tx_error;
177 skb->dev = rt->dst.dev;
178 ttl = ip4_dst_hoplimit(&rt->dst);
179 udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr,
180 dst->ipv4.s_addr, 0, ttl, 0, src->port,
181 dst->port, false, true);
182 #if IS_ENABLED(CONFIG_IPV6)
183 } else {
184 struct dst_entry *ndst;
185 struct flowi6 fl6 = {
186 .flowi6_oif = ub->ifindex,
187 .daddr = dst->ipv6,
188 .saddr = src->ipv6,
189 .flowi6_proto = IPPROTO_UDP
191 err = ipv6_stub->ipv6_dst_lookup(net, ub->ubsock->sk, &ndst,
192 &fl6);
193 if (err)
194 goto tx_error;
195 ttl = ip6_dst_hoplimit(ndst);
196 err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb,
197 ndst->dev, &src->ipv6,
198 &dst->ipv6, 0, ttl, 0, src->port,
199 dst->port, false);
200 #endif
202 return err;
204 tx_error:
205 kfree_skb(skb);
206 return err;
209 static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
210 struct tipc_bearer *b,
211 struct tipc_media_addr *addr)
213 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
214 struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value;
215 struct udp_replicast *rcast;
216 struct udp_bearer *ub;
217 int err = 0;
219 if (skb_headroom(skb) < UDP_MIN_HEADROOM) {
220 err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC);
221 if (err)
222 goto out;
225 skb_set_inner_protocol(skb, htons(ETH_P_TIPC));
226 ub = rcu_dereference_rtnl(b->media_ptr);
227 if (!ub) {
228 err = -ENODEV;
229 goto out;
232 if (addr->broadcast != TIPC_REPLICAST_SUPPORT)
233 return tipc_udp_xmit(net, skb, ub, src, dst);
235 /* Replicast, send an skb to each configured IP address */
236 list_for_each_entry_rcu(rcast, &ub->rcast.list, list) {
237 struct sk_buff *_skb;
239 _skb = pskb_copy(skb, GFP_ATOMIC);
240 if (!_skb) {
241 err = -ENOMEM;
242 goto out;
245 err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr);
246 if (err) {
247 kfree_skb(_skb);
248 goto out;
251 err = 0;
252 out:
253 kfree_skb(skb);
254 return err;
257 static bool tipc_udp_is_known_peer(struct tipc_bearer *b,
258 struct udp_media_addr *addr)
260 struct udp_replicast *rcast, *tmp;
261 struct udp_bearer *ub;
263 ub = rcu_dereference_rtnl(b->media_ptr);
264 if (!ub) {
265 pr_err_ratelimited("UDP bearer instance not found\n");
266 return false;
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;
274 return false;
277 static int tipc_udp_rcast_add(struct tipc_bearer *b,
278 struct udp_media_addr *addr)
280 struct udp_replicast *rcast;
281 struct udp_bearer *ub;
283 ub = rcu_dereference_rtnl(b->media_ptr);
284 if (!ub)
285 return -ENODEV;
287 rcast = kmalloc(sizeof(*rcast), GFP_ATOMIC);
288 if (!rcast)
289 return -ENOMEM;
291 memcpy(&rcast->addr, addr, sizeof(struct udp_media_addr));
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;
304 static int tipc_udp_rcast_disc(struct tipc_bearer *b, struct sk_buff *skb)
306 struct udp_media_addr src = {0};
307 struct udp_media_addr *dst;
309 dst = (struct udp_media_addr *)&b->bcast_addr.value;
310 if (tipc_udp_is_mcast_addr(dst))
311 return 0;
313 src.port = udp_hdr(skb)->source;
315 if (ip_hdr(skb)->version == 4) {
316 struct iphdr *iphdr = ip_hdr(skb);
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);
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;
335 if (likely(tipc_udp_is_known_peer(b, &src)))
336 return 0;
338 return tipc_udp_rcast_add(b, &src);
341 /* tipc_udp_recv - read data from bearer socket */
342 static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
344 struct udp_bearer *ub;
345 struct tipc_bearer *b;
346 struct tipc_msg *hdr;
347 int err;
349 ub = rcu_dereference_sk_user_data(sk);
350 if (!ub) {
351 pr_err_ratelimited("Failed to get UDP bearer reference");
352 goto out;
354 skb_pull(skb, sizeof(struct udphdr));
355 hdr = buf_msg(skb);
357 rcu_read_lock();
358 b = rcu_dereference_rtnl(ub->bearer);
359 if (!b)
360 goto rcu_out;
362 if (b && test_bit(0, &b->up)) {
363 tipc_rcv(sock_net(sk), skb, b);
364 rcu_read_unlock();
365 return 0;
368 if (unlikely(msg_user(hdr) == LINK_CONFIG)) {
369 err = tipc_udp_rcast_disc(b, skb);
370 if (err)
371 goto rcu_out;
374 tipc_rcv(sock_net(sk), skb, b);
375 rcu_read_unlock();
376 return 0;
378 rcu_out:
379 rcu_read_unlock();
380 out:
381 kfree_skb(skb);
382 return 0;
385 static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
387 int err = 0;
388 struct ip_mreqn mreqn;
389 struct sock *sk = ub->ubsock->sk;
391 if (ntohs(remote->proto) == ETH_P_IP) {
392 mreqn.imr_multiaddr = remote->ipv4;
393 mreqn.imr_ifindex = ub->ifindex;
394 err = ip_mc_join_group(sk, &mreqn);
395 #if IS_ENABLED(CONFIG_IPV6)
396 } else {
397 err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
398 &remote->ipv6);
399 #endif
401 return err;
404 static int __tipc_nl_add_udp_addr(struct sk_buff *skb,
405 struct udp_media_addr *addr, int nla_t)
407 if (ntohs(addr->proto) == ETH_P_IP) {
408 struct sockaddr_in ip4;
410 memset(&ip4, 0, sizeof(ip4));
411 ip4.sin_family = AF_INET;
412 ip4.sin_port = addr->port;
413 ip4.sin_addr.s_addr = addr->ipv4.s_addr;
414 if (nla_put(skb, nla_t, sizeof(ip4), &ip4))
415 return -EMSGSIZE;
417 #if IS_ENABLED(CONFIG_IPV6)
418 } else if (ntohs(addr->proto) == ETH_P_IPV6) {
419 struct sockaddr_in6 ip6;
421 memset(&ip6, 0, sizeof(ip6));
422 ip6.sin6_family = AF_INET6;
423 ip6.sin6_port = addr->port;
424 memcpy(&ip6.sin6_addr, &addr->ipv6, sizeof(struct in6_addr));
425 if (nla_put(skb, nla_t, sizeof(ip6), &ip6))
426 return -EMSGSIZE;
427 #endif
430 return 0;
433 int tipc_udp_nl_dump_remoteip(struct sk_buff *skb, struct netlink_callback *cb)
435 u32 bid = cb->args[0];
436 u32 skip_cnt = cb->args[1];
437 u32 portid = NETLINK_CB(cb->skb).portid;
438 struct udp_replicast *rcast, *tmp;
439 struct tipc_bearer *b;
440 struct udp_bearer *ub;
441 void *hdr;
442 int err;
443 int i;
445 if (!bid && !skip_cnt) {
446 struct net *net = sock_net(skb->sk);
447 struct nlattr *battrs[TIPC_NLA_BEARER_MAX + 1];
448 struct nlattr **attrs;
449 char *bname;
451 err = tipc_nlmsg_parse(cb->nlh, &attrs);
452 if (err)
453 return err;
455 if (!attrs[TIPC_NLA_BEARER])
456 return -EINVAL;
458 err = nla_parse_nested(battrs, TIPC_NLA_BEARER_MAX,
459 attrs[TIPC_NLA_BEARER],
460 tipc_nl_bearer_policy, NULL);
461 if (err)
462 return err;
464 if (!battrs[TIPC_NLA_BEARER_NAME])
465 return -EINVAL;
467 bname = nla_data(battrs[TIPC_NLA_BEARER_NAME]);
469 rtnl_lock();
470 b = tipc_bearer_find(net, bname);
471 if (!b) {
472 rtnl_unlock();
473 return -EINVAL;
475 bid = b->identity;
476 } else {
477 struct net *net = sock_net(skb->sk);
478 struct tipc_net *tn = net_generic(net, tipc_net_id);
480 rtnl_lock();
481 b = rtnl_dereference(tn->bearer_list[bid]);
482 if (!b) {
483 rtnl_unlock();
484 return -EINVAL;
488 ub = rcu_dereference_rtnl(b->media_ptr);
489 if (!ub) {
490 rtnl_unlock();
491 return -EINVAL;
494 i = 0;
495 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
496 if (i < skip_cnt)
497 goto count;
499 hdr = genlmsg_put(skb, portid, cb->nlh->nlmsg_seq,
500 &tipc_genl_family, NLM_F_MULTI,
501 TIPC_NL_BEARER_GET);
502 if (!hdr)
503 goto done;
505 err = __tipc_nl_add_udp_addr(skb, &rcast->addr,
506 TIPC_NLA_UDP_REMOTE);
507 if (err) {
508 genlmsg_cancel(skb, hdr);
509 goto done;
511 genlmsg_end(skb, hdr);
512 count:
513 i++;
515 done:
516 rtnl_unlock();
517 cb->args[0] = bid;
518 cb->args[1] = i;
520 return skb->len;
523 int tipc_udp_nl_add_bearer_data(struct tipc_nl_msg *msg, struct tipc_bearer *b)
525 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
526 struct udp_media_addr *dst;
527 struct udp_bearer *ub;
528 struct nlattr *nest;
530 ub = rcu_dereference_rtnl(b->media_ptr);
531 if (!ub)
532 return -ENODEV;
534 nest = nla_nest_start(msg->skb, TIPC_NLA_BEARER_UDP_OPTS);
535 if (!nest)
536 goto msg_full;
538 if (__tipc_nl_add_udp_addr(msg->skb, src, TIPC_NLA_UDP_LOCAL))
539 goto msg_full;
541 dst = (struct udp_media_addr *)&b->bcast_addr.value;
542 if (__tipc_nl_add_udp_addr(msg->skb, dst, TIPC_NLA_UDP_REMOTE))
543 goto msg_full;
545 if (!list_empty(&ub->rcast.list)) {
546 if (nla_put_flag(msg->skb, TIPC_NLA_UDP_MULTI_REMOTEIP))
547 goto msg_full;
550 nla_nest_end(msg->skb, nest);
551 return 0;
552 msg_full:
553 nla_nest_cancel(msg->skb, nest);
554 return -EMSGSIZE;
558 * tipc_parse_udp_addr - build udp media address from netlink data
559 * @nlattr: netlink attribute containing sockaddr storage aligned address
560 * @addr: tipc media address to fill with address, port and protocol type
561 * @scope_id: IPv6 scope id pointer, not NULL indicates it's required
564 static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr,
565 u32 *scope_id)
567 struct sockaddr_storage sa;
569 nla_memcpy(&sa, nla, sizeof(sa));
570 if (sa.ss_family == AF_INET) {
571 struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa;
573 addr->proto = htons(ETH_P_IP);
574 addr->port = ip4->sin_port;
575 addr->ipv4.s_addr = ip4->sin_addr.s_addr;
576 return 0;
578 #if IS_ENABLED(CONFIG_IPV6)
579 } else if (sa.ss_family == AF_INET6) {
580 struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa;
582 addr->proto = htons(ETH_P_IPV6);
583 addr->port = ip6->sin6_port;
584 memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr));
586 /* Scope ID is only interesting for local addresses */
587 if (scope_id) {
588 int atype;
590 atype = ipv6_addr_type(&ip6->sin6_addr);
591 if (__ipv6_addr_needs_scope_id(atype) &&
592 !ip6->sin6_scope_id) {
593 return -EINVAL;
596 *scope_id = ip6->sin6_scope_id ? : 0;
599 return 0;
600 #endif
602 return -EADDRNOTAVAIL;
605 int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr)
607 int err;
608 struct udp_media_addr addr = {0};
609 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
610 struct udp_media_addr *dst;
612 if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX, attr,
613 tipc_nl_udp_policy, NULL))
614 return -EINVAL;
616 if (!opts[TIPC_NLA_UDP_REMOTE])
617 return -EINVAL;
619 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL);
620 if (err)
621 return err;
623 dst = (struct udp_media_addr *)&b->bcast_addr.value;
624 if (tipc_udp_is_mcast_addr(dst)) {
625 pr_err("Can't add remote ip to TIPC UDP multicast bearer\n");
626 return -EINVAL;
629 if (tipc_udp_is_known_peer(b, &addr))
630 return 0;
632 return tipc_udp_rcast_add(b, &addr);
636 * tipc_udp_enable - callback to create a new udp bearer instance
637 * @net: network namespace
638 * @b: pointer to generic tipc_bearer
639 * @attrs: netlink bearer configuration
641 * validate the bearer parameters and initialize the udp bearer
642 * rtnl_lock should be held
644 static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
645 struct nlattr *attrs[])
647 int err = -EINVAL;
648 struct udp_bearer *ub;
649 struct udp_media_addr remote = {0};
650 struct udp_media_addr local = {0};
651 struct udp_port_cfg udp_conf = {0};
652 struct udp_tunnel_sock_cfg tuncfg = {NULL};
653 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
655 ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
656 if (!ub)
657 return -ENOMEM;
659 INIT_LIST_HEAD(&ub->rcast.list);
661 if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
662 goto err;
664 if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX,
665 attrs[TIPC_NLA_BEARER_UDP_OPTS],
666 tipc_nl_udp_policy, NULL))
667 goto err;
669 if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) {
670 pr_err("Invalid UDP bearer configuration");
671 err = -EINVAL;
672 goto err;
675 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local,
676 &ub->ifindex);
677 if (err)
678 goto err;
680 err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL);
681 if (err)
682 goto err;
684 b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
685 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
686 rcu_assign_pointer(b->media_ptr, ub);
687 rcu_assign_pointer(ub->bearer, b);
688 tipc_udp_media_addr_set(&b->addr, &local);
689 if (local.proto == htons(ETH_P_IP)) {
690 struct net_device *dev;
692 dev = __ip_dev_find(net, local.ipv4.s_addr, false);
693 if (!dev) {
694 err = -ENODEV;
695 goto err;
697 udp_conf.family = AF_INET;
698 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
699 udp_conf.use_udp_checksums = false;
700 ub->ifindex = dev->ifindex;
701 if (tipc_mtu_bad(dev, sizeof(struct iphdr) +
702 sizeof(struct udphdr))) {
703 err = -EINVAL;
704 goto err;
706 b->mtu = dev->mtu - sizeof(struct iphdr)
707 - sizeof(struct udphdr);
708 #if IS_ENABLED(CONFIG_IPV6)
709 } else if (local.proto == htons(ETH_P_IPV6)) {
710 udp_conf.family = AF_INET6;
711 udp_conf.use_udp6_tx_checksums = true;
712 udp_conf.use_udp6_rx_checksums = true;
713 udp_conf.local_ip6 = in6addr_any;
714 b->mtu = 1280;
715 #endif
716 } else {
717 err = -EAFNOSUPPORT;
718 goto err;
720 udp_conf.local_udp_port = local.port;
721 err = udp_sock_create(net, &udp_conf, &ub->ubsock);
722 if (err)
723 goto err;
724 tuncfg.sk_user_data = ub;
725 tuncfg.encap_type = 1;
726 tuncfg.encap_rcv = tipc_udp_recv;
727 tuncfg.encap_destroy = NULL;
728 setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
731 * The bcast media address port is used for all peers and the ip
732 * is used if it's a multicast address.
734 memcpy(&b->bcast_addr.value, &remote, sizeof(remote));
735 if (tipc_udp_is_mcast_addr(&remote))
736 err = enable_mcast(ub, &remote);
737 else
738 err = tipc_udp_rcast_add(b, &remote);
739 if (err)
740 goto err;
742 return 0;
743 err:
744 if (ub->ubsock)
745 udp_tunnel_sock_release(ub->ubsock);
746 kfree(ub);
747 return err;
750 /* cleanup_bearer - break the socket/bearer association */
751 static void cleanup_bearer(struct work_struct *work)
753 struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
754 struct udp_replicast *rcast, *tmp;
756 list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
757 list_del_rcu(&rcast->list);
758 kfree_rcu(rcast, rcu);
761 if (ub->ubsock)
762 udp_tunnel_sock_release(ub->ubsock);
763 synchronize_net();
764 kfree(ub);
767 /* tipc_udp_disable - detach bearer from socket */
768 static void tipc_udp_disable(struct tipc_bearer *b)
770 struct udp_bearer *ub;
772 ub = rcu_dereference_rtnl(b->media_ptr);
773 if (!ub) {
774 pr_err("UDP bearer instance not found\n");
775 return;
777 if (ub->ubsock)
778 sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
779 RCU_INIT_POINTER(ub->bearer, NULL);
781 /* sock_release need to be done outside of rtnl lock */
782 INIT_WORK(&ub->work, cleanup_bearer);
783 schedule_work(&ub->work);
786 struct tipc_media udp_media_info = {
787 .send_msg = tipc_udp_send_msg,
788 .enable_media = tipc_udp_enable,
789 .disable_media = tipc_udp_disable,
790 .addr2str = tipc_udp_addr2str,
791 .addr2msg = tipc_udp_addr2msg,
792 .msg2addr = tipc_udp_msg2addr,
793 .priority = TIPC_DEF_LINK_PRI,
794 .tolerance = TIPC_DEF_LINK_TOL,
795 .window = TIPC_DEF_LINK_WIN,
796 .type_id = TIPC_MEDIA_TYPE_UDP,
797 .hwaddr_len = 0,
798 .name = "udp"