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