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