]>
Commit | Line | Data |
---|---|---|
d0f91938 EH |
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> | |
3616d08b | 47 | #include <net/ipv6_stubs.h> |
d0f91938 EH |
48 | #include <linux/tipc_netlink.h> |
49 | #include "core.h" | |
52dfae5c JM |
50 | #include "addr.h" |
51 | #include "net.h" | |
d0f91938 | 52 | #include "bearer.h" |
49cc66ea | 53 | #include "netlink.h" |
ef20cd4d | 54 | #include "msg.h" |
d0f91938 EH |
55 | |
56 | /* IANA assigned UDP port */ | |
57 | #define UDP_PORT_DEFAULT 6118 | |
58 | ||
9bd160bf | 59 | #define UDP_MIN_HEADROOM 48 |
e5356794 | 60 | |
d0f91938 EH |
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; | |
bc3a334c | 69 | __be16 port; |
d0f91938 EH |
70 | union { |
71 | struct in_addr ipv4; | |
72 | struct in6_addr ipv6; | |
73 | }; | |
74 | }; | |
75 | ||
ef20cd4d RA |
76 | /* struct udp_replicast - container for UDP remote addresses */ |
77 | struct udp_replicast { | |
78 | struct udp_media_addr addr; | |
e9c1a793 | 79 | struct dst_cache dst_cache; |
ef20cd4d RA |
80 | struct rcu_head rcu; |
81 | struct list_head list; | |
82 | }; | |
83 | ||
d0f91938 EH |
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; | |
ef20cd4d | 96 | struct udp_replicast rcast; |
d0f91938 EH |
97 | }; |
98 | ||
1ca73e3f RA |
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 | ||
d0f91938 EH |
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)); | |
1ca73e3f RA |
117 | |
118 | if (tipc_udp_is_mcast_addr(ua)) | |
9999974a | 119 | addr->broadcast = TIPC_BROADCAST_SUPPORT; |
d0f91938 EH |
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) | |
bc3a334c | 128 | snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port)); |
d0f91938 | 129 | else if (ntohs(ua->proto) == ETH_P_IPV6) |
bc3a334c | 130 | snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port)); |
d0f91938 EH |
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 */ | |
ce984da3 RA |
160 | static int tipc_udp_xmit(struct net *net, struct sk_buff *skb, |
161 | struct udp_bearer *ub, struct udp_media_addr *src, | |
e9c1a793 | 162 | struct udp_media_addr *dst, struct dst_cache *cache) |
d0f91938 | 163 | { |
e9c1a793 | 164 | struct dst_entry *ndst = dst_cache_get(cache); |
d0f91938 | 165 | int ttl, err = 0; |
d0f91938 | 166 | |
4fee6be8 | 167 | if (dst->proto == htons(ETH_P_IP)) { |
e9c1a793 XL |
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); | |
d0f91938 | 183 | } |
9b300960 | 184 | |
d0f91938 | 185 | ttl = ip4_dst_hoplimit(&rt->dst); |
039f5062 | 186 | udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr, |
bc3a334c RA |
187 | dst->ipv4.s_addr, 0, ttl, 0, src->port, |
188 | dst->port, false, true); | |
d0f91938 EH |
189 | #if IS_ENABLED(CONFIG_IPV6) |
190 | } else { | |
e9c1a793 XL |
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 | } | |
d0f91938 | 204 | ttl = ip6_dst_hoplimit(ndst); |
c3bcde02 XL |
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); | |
d0f91938 EH |
208 | #endif |
209 | } | |
210 | return err; | |
211 | ||
212 | tx_error: | |
7214bcf8 | 213 | kfree_skb(skb); |
d0f91938 EH |
214 | return err; |
215 | } | |
216 | ||
ce984da3 RA |
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; | |
ef20cd4d | 223 | struct udp_replicast *rcast; |
ce984da3 RA |
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) | |
ef20cd4d | 230 | goto out; |
ce984da3 RA |
231 | } |
232 | ||
233 | skb_set_inner_protocol(skb, htons(ETH_P_TIPC)); | |
30a4616c | 234 | ub = rcu_dereference(b->media_ptr); |
ce984da3 RA |
235 | if (!ub) { |
236 | err = -ENODEV; | |
ef20cd4d | 237 | goto out; |
ce984da3 RA |
238 | } |
239 | ||
9999974a | 240 | if (addr->broadcast != TIPC_REPLICAST_SUPPORT) |
e9c1a793 XL |
241 | return tipc_udp_xmit(net, skb, ub, src, dst, |
242 | &ub->rcast.dst_cache); | |
ce984da3 | 243 | |
ef20cd4d RA |
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 | ||
e9c1a793 XL |
254 | err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr, |
255 | &rcast->dst_cache); | |
acb4a33e | 256 | if (err) |
ef20cd4d | 257 | goto out; |
ef20cd4d RA |
258 | } |
259 | err = 0; | |
260 | out: | |
ce984da3 RA |
261 | kfree_skb(skb); |
262 | return err; | |
263 | } | |
264 | ||
c9b64d49 RA |
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 | ||
ef20cd4d RA |
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 | ||
e9c1a793 XL |
299 | if (dst_cache_init(&rcast->dst_cache, GFP_ATOMIC)) { |
300 | kfree(rcast); | |
301 | return -ENOMEM; | |
302 | } | |
303 | ||
ef20cd4d RA |
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 | |
9999974a | 312 | b->bcast_addr.broadcast = TIPC_REPLICAST_SUPPORT; |
ef20cd4d RA |
313 | list_add_rcu(&rcast->list, &ub->rcast.list); |
314 | return 0; | |
315 | } | |
316 | ||
c9b64d49 RA |
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 | ||
d0f91938 EH |
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; | |
c9b64d49 RA |
359 | struct tipc_msg *hdr; |
360 | int err; | |
d0f91938 EH |
361 | |
362 | ub = rcu_dereference_sk_user_data(sk); | |
363 | if (!ub) { | |
364 | pr_err_ratelimited("Failed to get UDP bearer reference"); | |
c9b64d49 | 365 | goto out; |
d0f91938 | 366 | } |
d0f91938 | 367 | skb_pull(skb, sizeof(struct udphdr)); |
c9b64d49 RA |
368 | hdr = buf_msg(skb); |
369 | ||
4109a2c3 | 370 | b = rcu_dereference(ub->bearer); |
c9b64d49 | 371 | if (!b) |
4109a2c3 | 372 | goto out; |
d0f91938 | 373 | |
0d051bf9 | 374 | if (b && test_bit(0, &b->up)) { |
fc1b6d6d | 375 | TIPC_SKB_CB(skb)->flags = 0; |
d0f91938 | 376 | tipc_rcv(sock_net(sk), skb, b); |
d0f91938 EH |
377 | return 0; |
378 | } | |
c9b64d49 RA |
379 | |
380 | if (unlikely(msg_user(hdr) == LINK_CONFIG)) { | |
381 | err = tipc_udp_rcast_disc(b, skb); | |
382 | if (err) | |
4109a2c3 | 383 | goto out; |
c9b64d49 RA |
384 | } |
385 | ||
c9b64d49 | 386 | out: |
d0f91938 EH |
387 | kfree_skb(skb); |
388 | return 0; | |
389 | } | |
390 | ||
391 | static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote) | |
392 | { | |
393 | int err = 0; | |
394 | struct ip_mreqn mreqn; | |
395 | struct sock *sk = ub->ubsock->sk; | |
396 | ||
397 | if (ntohs(remote->proto) == ETH_P_IP) { | |
d0f91938 EH |
398 | mreqn.imr_multiaddr = remote->ipv4; |
399 | mreqn.imr_ifindex = ub->ifindex; | |
54ff9ef3 | 400 | err = ip_mc_join_group(sk, &mreqn); |
446981e5 | 401 | #if IS_ENABLED(CONFIG_IPV6) |
d0f91938 | 402 | } else { |
446981e5 MRL |
403 | err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex, |
404 | &remote->ipv6); | |
405 | #endif | |
d0f91938 EH |
406 | } |
407 | return err; | |
408 | } | |
409 | ||
fdb3accc RA |
410 | static int __tipc_nl_add_udp_addr(struct sk_buff *skb, |
411 | struct udp_media_addr *addr, int nla_t) | |
412 | { | |
413 | if (ntohs(addr->proto) == ETH_P_IP) { | |
414 | struct sockaddr_in ip4; | |
415 | ||
73076162 | 416 | memset(&ip4, 0, sizeof(ip4)); |
fdb3accc RA |
417 | ip4.sin_family = AF_INET; |
418 | ip4.sin_port = addr->port; | |
419 | ip4.sin_addr.s_addr = addr->ipv4.s_addr; | |
420 | if (nla_put(skb, nla_t, sizeof(ip4), &ip4)) | |
421 | return -EMSGSIZE; | |
422 | ||
423 | #if IS_ENABLED(CONFIG_IPV6) | |
424 | } else if (ntohs(addr->proto) == ETH_P_IPV6) { | |
425 | struct sockaddr_in6 ip6; | |
426 | ||
73076162 | 427 | memset(&ip6, 0, sizeof(ip6)); |
fdb3accc RA |
428 | ip6.sin6_family = AF_INET6; |
429 | ip6.sin6_port = addr->port; | |
430 | memcpy(&ip6.sin6_addr, &addr->ipv6, sizeof(struct in6_addr)); | |
431 | if (nla_put(skb, nla_t, sizeof(ip6), &ip6)) | |
432 | return -EMSGSIZE; | |
433 | #endif | |
434 | } | |
435 | ||
436 | return 0; | |
437 | } | |
438 | ||
832629ca RA |
439 | int tipc_udp_nl_dump_remoteip(struct sk_buff *skb, struct netlink_callback *cb) |
440 | { | |
441 | u32 bid = cb->args[0]; | |
442 | u32 skip_cnt = cb->args[1]; | |
443 | u32 portid = NETLINK_CB(cb->skb).portid; | |
444 | struct udp_replicast *rcast, *tmp; | |
445 | struct tipc_bearer *b; | |
446 | struct udp_bearer *ub; | |
447 | void *hdr; | |
448 | int err; | |
449 | int i; | |
450 | ||
451 | if (!bid && !skip_cnt) { | |
057af707 | 452 | struct nlattr **attrs = genl_dumpit_info(cb)->attrs; |
832629ca RA |
453 | struct net *net = sock_net(skb->sk); |
454 | struct nlattr *battrs[TIPC_NLA_BEARER_MAX + 1]; | |
832629ca RA |
455 | char *bname; |
456 | ||
832629ca RA |
457 | if (!attrs[TIPC_NLA_BEARER]) |
458 | return -EINVAL; | |
459 | ||
8cb08174 JB |
460 | err = nla_parse_nested_deprecated(battrs, TIPC_NLA_BEARER_MAX, |
461 | attrs[TIPC_NLA_BEARER], | |
462 | tipc_nl_bearer_policy, NULL); | |
832629ca RA |
463 | if (err) |
464 | return err; | |
465 | ||
466 | if (!battrs[TIPC_NLA_BEARER_NAME]) | |
467 | return -EINVAL; | |
468 | ||
469 | bname = nla_data(battrs[TIPC_NLA_BEARER_NAME]); | |
470 | ||
471 | rtnl_lock(); | |
472 | b = tipc_bearer_find(net, bname); | |
473 | if (!b) { | |
474 | rtnl_unlock(); | |
475 | return -EINVAL; | |
476 | } | |
477 | bid = b->identity; | |
478 | } else { | |
479 | struct net *net = sock_net(skb->sk); | |
480 | struct tipc_net *tn = net_generic(net, tipc_net_id); | |
481 | ||
482 | rtnl_lock(); | |
483 | b = rtnl_dereference(tn->bearer_list[bid]); | |
484 | if (!b) { | |
485 | rtnl_unlock(); | |
486 | return -EINVAL; | |
487 | } | |
488 | } | |
489 | ||
30a4616c | 490 | ub = rtnl_dereference(b->media_ptr); |
832629ca RA |
491 | if (!ub) { |
492 | rtnl_unlock(); | |
493 | return -EINVAL; | |
494 | } | |
495 | ||
496 | i = 0; | |
497 | list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) { | |
498 | if (i < skip_cnt) | |
499 | goto count; | |
500 | ||
501 | hdr = genlmsg_put(skb, portid, cb->nlh->nlmsg_seq, | |
502 | &tipc_genl_family, NLM_F_MULTI, | |
503 | TIPC_NL_BEARER_GET); | |
504 | if (!hdr) | |
505 | goto done; | |
506 | ||
507 | err = __tipc_nl_add_udp_addr(skb, &rcast->addr, | |
508 | TIPC_NLA_UDP_REMOTE); | |
509 | if (err) { | |
510 | genlmsg_cancel(skb, hdr); | |
511 | goto done; | |
512 | } | |
513 | genlmsg_end(skb, hdr); | |
514 | count: | |
515 | i++; | |
516 | } | |
517 | done: | |
518 | rtnl_unlock(); | |
519 | cb->args[0] = bid; | |
520 | cb->args[1] = i; | |
521 | ||
522 | return skb->len; | |
523 | } | |
524 | ||
fdb3accc RA |
525 | int tipc_udp_nl_add_bearer_data(struct tipc_nl_msg *msg, struct tipc_bearer *b) |
526 | { | |
527 | struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value; | |
528 | struct udp_media_addr *dst; | |
529 | struct udp_bearer *ub; | |
530 | struct nlattr *nest; | |
531 | ||
30a4616c | 532 | ub = rtnl_dereference(b->media_ptr); |
fdb3accc RA |
533 | if (!ub) |
534 | return -ENODEV; | |
535 | ||
ae0be8de | 536 | nest = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_UDP_OPTS); |
fdb3accc RA |
537 | if (!nest) |
538 | goto msg_full; | |
539 | ||
540 | if (__tipc_nl_add_udp_addr(msg->skb, src, TIPC_NLA_UDP_LOCAL)) | |
541 | goto msg_full; | |
542 | ||
543 | dst = (struct udp_media_addr *)&b->bcast_addr.value; | |
544 | if (__tipc_nl_add_udp_addr(msg->skb, dst, TIPC_NLA_UDP_REMOTE)) | |
545 | goto msg_full; | |
546 | ||
547 | if (!list_empty(&ub->rcast.list)) { | |
548 | if (nla_put_flag(msg->skb, TIPC_NLA_UDP_MULTI_REMOTEIP)) | |
549 | goto msg_full; | |
550 | } | |
551 | ||
552 | nla_nest_end(msg->skb, nest); | |
553 | return 0; | |
554 | msg_full: | |
555 | nla_nest_cancel(msg->skb, nest); | |
556 | return -EMSGSIZE; | |
557 | } | |
558 | ||
d0f91938 | 559 | /** |
ba5aa84a RA |
560 | * tipc_parse_udp_addr - build udp media address from netlink data |
561 | * @nlattr: netlink attribute containing sockaddr storage aligned address | |
562 | * @addr: tipc media address to fill with address, port and protocol type | |
563 | * @scope_id: IPv6 scope id pointer, not NULL indicates it's required | |
d0f91938 | 564 | */ |
ba5aa84a RA |
565 | |
566 | static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr, | |
567 | u32 *scope_id) | |
d0f91938 | 568 | { |
ba5aa84a | 569 | struct sockaddr_storage sa; |
d0f91938 | 570 | |
ba5aa84a RA |
571 | nla_memcpy(&sa, nla, sizeof(sa)); |
572 | if (sa.ss_family == AF_INET) { | |
573 | struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa; | |
574 | ||
575 | addr->proto = htons(ETH_P_IP); | |
576 | addr->port = ip4->sin_port; | |
577 | addr->ipv4.s_addr = ip4->sin_addr.s_addr; | |
d0f91938 EH |
578 | return 0; |
579 | ||
580 | #if IS_ENABLED(CONFIG_IPV6) | |
ba5aa84a RA |
581 | } else if (sa.ss_family == AF_INET6) { |
582 | struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa; | |
583 | ||
584 | addr->proto = htons(ETH_P_IPV6); | |
585 | addr->port = ip6->sin6_port; | |
586 | memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr)); | |
587 | ||
588 | /* Scope ID is only interesting for local addresses */ | |
589 | if (scope_id) { | |
590 | int atype; | |
591 | ||
592 | atype = ipv6_addr_type(&ip6->sin6_addr); | |
593 | if (__ipv6_addr_needs_scope_id(atype) && | |
594 | !ip6->sin6_scope_id) { | |
595 | return -EINVAL; | |
596 | } | |
597 | ||
598 | *scope_id = ip6->sin6_scope_id ? : 0; | |
599 | } | |
600 | ||
d0f91938 EH |
601 | return 0; |
602 | #endif | |
603 | } | |
604 | return -EADDRNOTAVAIL; | |
605 | } | |
606 | ||
ef20cd4d RA |
607 | int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr) |
608 | { | |
609 | int err; | |
610 | struct udp_media_addr addr = {0}; | |
611 | struct nlattr *opts[TIPC_NLA_UDP_MAX + 1]; | |
612 | struct udp_media_addr *dst; | |
613 | ||
8cb08174 | 614 | if (nla_parse_nested_deprecated(opts, TIPC_NLA_UDP_MAX, attr, tipc_nl_udp_policy, NULL)) |
ef20cd4d RA |
615 | return -EINVAL; |
616 | ||
617 | if (!opts[TIPC_NLA_UDP_REMOTE]) | |
618 | return -EINVAL; | |
619 | ||
620 | err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL); | |
621 | if (err) | |
622 | return err; | |
623 | ||
624 | dst = (struct udp_media_addr *)&b->bcast_addr.value; | |
625 | if (tipc_udp_is_mcast_addr(dst)) { | |
626 | pr_err("Can't add remote ip to TIPC UDP multicast bearer\n"); | |
627 | return -EINVAL; | |
628 | } | |
629 | ||
c9b64d49 RA |
630 | if (tipc_udp_is_known_peer(b, &addr)) |
631 | return 0; | |
632 | ||
ef20cd4d RA |
633 | return tipc_udp_rcast_add(b, &addr); |
634 | } | |
635 | ||
d0f91938 EH |
636 | /** |
637 | * tipc_udp_enable - callback to create a new udp bearer instance | |
638 | * @net: network namespace | |
639 | * @b: pointer to generic tipc_bearer | |
640 | * @attrs: netlink bearer configuration | |
641 | * | |
642 | * validate the bearer parameters and initialize the udp bearer | |
643 | * rtnl_lock should be held | |
644 | */ | |
645 | static int tipc_udp_enable(struct net *net, struct tipc_bearer *b, | |
646 | struct nlattr *attrs[]) | |
647 | { | |
648 | int err = -EINVAL; | |
649 | struct udp_bearer *ub; | |
ef20cd4d | 650 | struct udp_media_addr remote = {0}; |
d0f91938 EH |
651 | struct udp_media_addr local = {0}; |
652 | struct udp_port_cfg udp_conf = {0}; | |
4fee6be8 | 653 | struct udp_tunnel_sock_cfg tuncfg = {NULL}; |
ba5aa84a | 654 | struct nlattr *opts[TIPC_NLA_UDP_MAX + 1]; |
52dfae5c | 655 | u8 node_id[NODE_ID_LEN] = {0,}; |
acad76a5 | 656 | int rmcast = 0; |
d0f91938 EH |
657 | |
658 | ub = kzalloc(sizeof(*ub), GFP_ATOMIC); | |
659 | if (!ub) | |
660 | return -ENOMEM; | |
661 | ||
ef20cd4d RA |
662 | INIT_LIST_HEAD(&ub->rcast.list); |
663 | ||
ba5aa84a RA |
664 | if (!attrs[TIPC_NLA_BEARER_UDP_OPTS]) |
665 | goto err; | |
666 | ||
8cb08174 | 667 | if (nla_parse_nested_deprecated(opts, TIPC_NLA_UDP_MAX, attrs[TIPC_NLA_BEARER_UDP_OPTS], tipc_nl_udp_policy, NULL)) |
ba5aa84a RA |
668 | goto err; |
669 | ||
670 | if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) { | |
671 | pr_err("Invalid UDP bearer configuration"); | |
c20cb811 WY |
672 | err = -EINVAL; |
673 | goto err; | |
ba5aa84a RA |
674 | } |
675 | ||
676 | err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local, | |
677 | &ub->ifindex); | |
678 | if (err) | |
679 | goto err; | |
680 | ||
ef20cd4d | 681 | err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL); |
d0f91938 EH |
682 | if (err) |
683 | goto err; | |
684 | ||
fb83ed49 CW |
685 | if (remote.proto != local.proto) { |
686 | err = -EINVAL; | |
687 | goto err; | |
688 | } | |
689 | ||
acad76a5 HL |
690 | /* Checking remote ip address */ |
691 | rmcast = tipc_udp_is_mcast_addr(&remote); | |
692 | ||
52dfae5c JM |
693 | /* Autoconfigure own node identity if needed */ |
694 | if (!tipc_own_id(net)) { | |
695 | memcpy(node_id, local.ipv6.in6_u.u6_addr8, 16); | |
696 | tipc_net_init(net, node_id, 0); | |
697 | } | |
698 | if (!tipc_own_id(net)) { | |
699 | pr_warn("Failed to set node id, please configure manually\n"); | |
c76f2481 WY |
700 | err = -EINVAL; |
701 | goto err; | |
52dfae5c JM |
702 | } |
703 | ||
d0f91938 | 704 | b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP; |
9999974a | 705 | b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT; |
d0f91938 EH |
706 | rcu_assign_pointer(b->media_ptr, ub); |
707 | rcu_assign_pointer(ub->bearer, b); | |
708 | tipc_udp_media_addr_set(&b->addr, &local); | |
4fee6be8 | 709 | if (local.proto == htons(ETH_P_IP)) { |
d0f91938 EH |
710 | struct net_device *dev; |
711 | ||
712 | dev = __ip_dev_find(net, local.ipv4.s_addr, false); | |
713 | if (!dev) { | |
714 | err = -ENODEV; | |
715 | goto err; | |
716 | } | |
717 | udp_conf.family = AF_INET; | |
acad76a5 HL |
718 | |
719 | /* Switch to use ANY to receive packets from group */ | |
720 | if (rmcast) | |
721 | udp_conf.local_ip.s_addr = htonl(INADDR_ANY); | |
722 | else | |
723 | udp_conf.local_ip.s_addr = local.ipv4.s_addr; | |
d0f91938 EH |
724 | udp_conf.use_udp_checksums = false; |
725 | ub->ifindex = dev->ifindex; | |
3de81b75 MK |
726 | if (tipc_mtu_bad(dev, sizeof(struct iphdr) + |
727 | sizeof(struct udphdr))) { | |
728 | err = -EINVAL; | |
729 | goto err; | |
730 | } | |
a4dfa72d | 731 | b->mtu = b->media->mtu; |
d0f91938 | 732 | #if IS_ENABLED(CONFIG_IPV6) |
4fee6be8 | 733 | } else if (local.proto == htons(ETH_P_IPV6)) { |
d0f91938 EH |
734 | udp_conf.family = AF_INET6; |
735 | udp_conf.use_udp6_tx_checksums = true; | |
736 | udp_conf.use_udp6_rx_checksums = true; | |
acad76a5 HL |
737 | if (rmcast) |
738 | udp_conf.local_ip6 = in6addr_any; | |
739 | else | |
740 | udp_conf.local_ip6 = local.ipv6; | |
d0f91938 EH |
741 | b->mtu = 1280; |
742 | #endif | |
743 | } else { | |
744 | err = -EAFNOSUPPORT; | |
745 | goto err; | |
746 | } | |
bc3a334c | 747 | udp_conf.local_udp_port = local.port; |
d0f91938 EH |
748 | err = udp_sock_create(net, &udp_conf, &ub->ubsock); |
749 | if (err) | |
750 | goto err; | |
751 | tuncfg.sk_user_data = ub; | |
752 | tuncfg.encap_type = 1; | |
753 | tuncfg.encap_rcv = tipc_udp_recv; | |
754 | tuncfg.encap_destroy = NULL; | |
755 | setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg); | |
756 | ||
e9c1a793 XL |
757 | err = dst_cache_init(&ub->rcast.dst_cache, GFP_ATOMIC); |
758 | if (err) | |
d2c3a4ba | 759 | goto free; |
e9c1a793 | 760 | |
ef20cd4d RA |
761 | /** |
762 | * The bcast media address port is used for all peers and the ip | |
763 | * is used if it's a multicast address. | |
764 | */ | |
765 | memcpy(&b->bcast_addr.value, &remote, sizeof(remote)); | |
acad76a5 | 766 | if (rmcast) |
ef20cd4d RA |
767 | err = enable_mcast(ub, &remote); |
768 | else | |
769 | err = tipc_udp_rcast_add(b, &remote); | |
770 | if (err) | |
d2c3a4ba | 771 | goto free; |
1ca73e3f | 772 | |
d0f91938 | 773 | return 0; |
d2c3a4ba XL |
774 | |
775 | free: | |
e9c1a793 | 776 | dst_cache_destroy(&ub->rcast.dst_cache); |
d2c3a4ba XL |
777 | udp_tunnel_sock_release(ub->ubsock); |
778 | err: | |
d0f91938 EH |
779 | kfree(ub); |
780 | return err; | |
781 | } | |
782 | ||
783 | /* cleanup_bearer - break the socket/bearer association */ | |
784 | static void cleanup_bearer(struct work_struct *work) | |
785 | { | |
786 | struct udp_bearer *ub = container_of(work, struct udp_bearer, work); | |
ef20cd4d RA |
787 | struct udp_replicast *rcast, *tmp; |
788 | ||
789 | list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) { | |
e9c1a793 | 790 | dst_cache_destroy(&rcast->dst_cache); |
ef20cd4d RA |
791 | list_del_rcu(&rcast->list); |
792 | kfree_rcu(rcast, rcu); | |
793 | } | |
d0f91938 | 794 | |
e9c1a793 | 795 | dst_cache_destroy(&ub->rcast.dst_cache); |
d2c3a4ba | 796 | udp_tunnel_sock_release(ub->ubsock); |
d0f91938 EH |
797 | synchronize_net(); |
798 | kfree(ub); | |
799 | } | |
800 | ||
801 | /* tipc_udp_disable - detach bearer from socket */ | |
802 | static void tipc_udp_disable(struct tipc_bearer *b) | |
803 | { | |
804 | struct udp_bearer *ub; | |
805 | ||
30a4616c | 806 | ub = rtnl_dereference(b->media_ptr); |
d0f91938 EH |
807 | if (!ub) { |
808 | pr_err("UDP bearer instance not found\n"); | |
809 | return; | |
810 | } | |
d2c3a4ba | 811 | sock_set_flag(ub->ubsock->sk, SOCK_DEAD); |
d0f91938 EH |
812 | RCU_INIT_POINTER(ub->bearer, NULL); |
813 | ||
814 | /* sock_release need to be done outside of rtnl lock */ | |
815 | INIT_WORK(&ub->work, cleanup_bearer); | |
816 | schedule_work(&ub->work); | |
817 | } | |
818 | ||
819 | struct tipc_media udp_media_info = { | |
820 | .send_msg = tipc_udp_send_msg, | |
821 | .enable_media = tipc_udp_enable, | |
822 | .disable_media = tipc_udp_disable, | |
823 | .addr2str = tipc_udp_addr2str, | |
824 | .addr2msg = tipc_udp_addr2msg, | |
825 | .msg2addr = tipc_udp_msg2addr, | |
826 | .priority = TIPC_DEF_LINK_PRI, | |
827 | .tolerance = TIPC_DEF_LINK_TOL, | |
828 | .window = TIPC_DEF_LINK_WIN, | |
a4dfa72d | 829 | .mtu = TIPC_DEF_LINK_UDP_MTU, |
d0f91938 EH |
830 | .type_id = TIPC_MEDIA_TYPE_UDP, |
831 | .hwaddr_len = 0, | |
832 | .name = "udp" | |
833 | }; |