]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/tipc/udp_media.c
arp: correct return value of arp_rcv
[mirror_ubuntu-bionic-kernel.git] / net / tipc / udp_media.c
CommitLineData
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>
446981e5 47#include <net/addrconf.h>
d0f91938
EH
48#include <linux/tipc_netlink.h>
49#include "core.h"
50#include "bearer.h"
51
52/* IANA assigned UDP port */
53#define UDP_PORT_DEFAULT 6118
54
e5356794
JPM
55#define UDP_MIN_HEADROOM 28
56
d0f91938
EH
57static const struct nla_policy tipc_nl_udp_policy[TIPC_NLA_UDP_MAX + 1] = {
58 [TIPC_NLA_UDP_UNSPEC] = {.type = NLA_UNSPEC},
59 [TIPC_NLA_UDP_LOCAL] = {.type = NLA_BINARY,
60 .len = sizeof(struct sockaddr_storage)},
61 [TIPC_NLA_UDP_REMOTE] = {.type = NLA_BINARY,
62 .len = sizeof(struct sockaddr_storage)},
63};
64
65/**
66 * struct udp_media_addr - IP/UDP addressing information
67 *
68 * This is the bearer level originating address used in neighbor discovery
69 * messages, and all fields should be in network byte order
70 */
71struct udp_media_addr {
72 __be16 proto;
73 __be16 udp_port;
74 union {
75 struct in_addr ipv4;
76 struct in6_addr ipv6;
77 };
78};
79
80/**
81 * struct udp_bearer - ip/udp bearer data structure
82 * @bearer: associated generic tipc bearer
83 * @ubsock: bearer associated socket
84 * @ifindex: local address scope
85 * @work: used to schedule deferred work on a bearer
86 */
87struct udp_bearer {
88 struct tipc_bearer __rcu *bearer;
89 struct socket *ubsock;
90 u32 ifindex;
91 struct work_struct work;
92};
93
94/* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
95static void tipc_udp_media_addr_set(struct tipc_media_addr *addr,
96 struct udp_media_addr *ua)
97{
98 memset(addr, 0, sizeof(struct tipc_media_addr));
99 addr->media_id = TIPC_MEDIA_TYPE_UDP;
100 memcpy(addr->value, ua, sizeof(struct udp_media_addr));
101 if (ntohs(ua->proto) == ETH_P_IP) {
102 if (ipv4_is_multicast(ua->ipv4.s_addr))
103 addr->broadcast = 1;
104 } else if (ntohs(ua->proto) == ETH_P_IPV6) {
105 if (ipv6_addr_type(&ua->ipv6) & IPV6_ADDR_MULTICAST)
106 addr->broadcast = 1;
107 } else {
108 pr_err("Invalid UDP media address\n");
109 }
110}
111
112/* tipc_udp_addr2str - convert ip/udp address to string */
113static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size)
114{
115 struct udp_media_addr *ua = (struct udp_media_addr *)&a->value;
116
117 if (ntohs(ua->proto) == ETH_P_IP)
118 snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->udp_port));
119 else if (ntohs(ua->proto) == ETH_P_IPV6)
120 snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->udp_port));
121 else
122 pr_err("Invalid UDP media address\n");
123 return 0;
124}
125
126/* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
127static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a,
128 char *msg)
129{
130 struct udp_media_addr *ua;
131
132 ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET);
133 if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP)
134 return -EINVAL;
135 tipc_udp_media_addr_set(a, ua);
136 return 0;
137}
138
139/* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
140static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a)
141{
142 memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
143 msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP;
144 memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value,
145 sizeof(struct udp_media_addr));
146 return 0;
147}
148
149/* tipc_send_msg - enqueue a send request */
150static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
151 struct tipc_bearer *b,
152 struct tipc_media_addr *dest)
153{
154 int ttl, err = 0;
155 struct udp_bearer *ub;
156 struct udp_media_addr *dst = (struct udp_media_addr *)&dest->value;
157 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
d0f91938
EH
158 struct rtable *rt;
159
7098356b
YX
160 if (skb_headroom(skb) < UDP_MIN_HEADROOM) {
161 err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC);
162 if (err)
163 goto tx_error;
164 }
e5356794 165
7214bcf8 166 skb_set_inner_protocol(skb, htons(ETH_P_TIPC));
d0f91938
EH
167 ub = rcu_dereference_rtnl(b->media_ptr);
168 if (!ub) {
169 err = -ENODEV;
170 goto tx_error;
171 }
4fee6be8 172 if (dst->proto == htons(ETH_P_IP)) {
d0f91938
EH
173 struct flowi4 fl = {
174 .daddr = dst->ipv4.s_addr,
175 .saddr = src->ipv4.s_addr,
7214bcf8 176 .flowi4_mark = skb->mark,
d0f91938
EH
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 }
9b300960
RA
184
185 skb->dev = rt->dst.dev;
d0f91938 186 ttl = ip4_dst_hoplimit(&rt->dst);
039f5062
PS
187 udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr,
188 dst->ipv4.s_addr, 0, ttl, 0, src->udp_port,
189 dst->udp_port, false, true);
d0f91938
EH
190#if IS_ENABLED(CONFIG_IPV6)
191 } else {
192 struct dst_entry *ndst;
193 struct flowi6 fl6 = {
194 .flowi6_oif = ub->ifindex,
195 .daddr = dst->ipv6,
196 .saddr = src->ipv6,
197 .flowi6_proto = IPPROTO_UDP
198 };
343d60aa
RP
199 err = ipv6_stub->ipv6_dst_lookup(net, ub->ubsock->sk, &ndst,
200 &fl6);
d0f91938
EH
201 if (err)
202 goto tx_error;
203 ttl = ip6_dst_hoplimit(ndst);
7214bcf8 204 err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb,
79b16aad 205 ndst->dev, &src->ipv6,
d0f91938
EH
206 &dst->ipv6, 0, ttl, src->udp_port,
207 dst->udp_port, false);
208#endif
209 }
210 return err;
211
212tx_error:
7214bcf8 213 kfree_skb(skb);
d0f91938
EH
214 return err;
215}
216
217/* tipc_udp_recv - read data from bearer socket */
218static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
219{
220 struct udp_bearer *ub;
221 struct tipc_bearer *b;
222
223 ub = rcu_dereference_sk_user_data(sk);
224 if (!ub) {
225 pr_err_ratelimited("Failed to get UDP bearer reference");
226 kfree_skb(skb);
227 return 0;
228 }
229
230 skb_pull(skb, sizeof(struct udphdr));
231 rcu_read_lock();
232 b = rcu_dereference_rtnl(ub->bearer);
233
234 if (b) {
235 tipc_rcv(sock_net(sk), skb, b);
236 rcu_read_unlock();
237 return 0;
238 }
239 rcu_read_unlock();
240 kfree_skb(skb);
241 return 0;
242}
243
244static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
245{
246 int err = 0;
247 struct ip_mreqn mreqn;
248 struct sock *sk = ub->ubsock->sk;
249
250 if (ntohs(remote->proto) == ETH_P_IP) {
251 if (!ipv4_is_multicast(remote->ipv4.s_addr))
252 return 0;
253 mreqn.imr_multiaddr = remote->ipv4;
254 mreqn.imr_ifindex = ub->ifindex;
54ff9ef3 255 err = ip_mc_join_group(sk, &mreqn);
446981e5 256#if IS_ENABLED(CONFIG_IPV6)
d0f91938
EH
257 } else {
258 if (!ipv6_addr_is_multicast(&remote->ipv6))
259 return 0;
446981e5
MRL
260 err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
261 &remote->ipv6);
262#endif
d0f91938
EH
263 }
264 return err;
265}
266
267/**
268 * parse_options - build local/remote addresses from configuration
269 * @attrs: netlink config data
270 * @ub: UDP bearer instance
271 * @local: local bearer IP address/port
272 * @remote: peer or multicast IP/port
273 */
274static int parse_options(struct nlattr *attrs[], struct udp_bearer *ub,
275 struct udp_media_addr *local,
276 struct udp_media_addr *remote)
277{
278 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
ddb37125 279 struct sockaddr_storage sa_local, sa_remote;
d0f91938
EH
280
281 if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
282 goto err;
283 if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX,
284 attrs[TIPC_NLA_BEARER_UDP_OPTS],
285 tipc_nl_udp_policy))
286 goto err;
287 if (opts[TIPC_NLA_UDP_LOCAL] && opts[TIPC_NLA_UDP_REMOTE]) {
ddb37125
RA
288 nla_memcpy(&sa_local, opts[TIPC_NLA_UDP_LOCAL],
289 sizeof(sa_local));
290 nla_memcpy(&sa_remote, opts[TIPC_NLA_UDP_REMOTE],
291 sizeof(sa_remote));
d0f91938
EH
292 } else {
293err:
294 pr_err("Invalid UDP bearer configuration");
295 return -EINVAL;
296 }
ddb37125 297 if ((sa_local.ss_family & sa_remote.ss_family) == AF_INET) {
d0f91938
EH
298 struct sockaddr_in *ip4;
299
ddb37125 300 ip4 = (struct sockaddr_in *)&sa_local;
d0f91938
EH
301 local->proto = htons(ETH_P_IP);
302 local->udp_port = ip4->sin_port;
303 local->ipv4.s_addr = ip4->sin_addr.s_addr;
304
ddb37125 305 ip4 = (struct sockaddr_in *)&sa_remote;
d0f91938
EH
306 remote->proto = htons(ETH_P_IP);
307 remote->udp_port = ip4->sin_port;
308 remote->ipv4.s_addr = ip4->sin_addr.s_addr;
309 return 0;
310
311#if IS_ENABLED(CONFIG_IPV6)
ddb37125 312 } else if ((sa_local.ss_family & sa_remote.ss_family) == AF_INET6) {
34f65dbb 313 int atype;
d0f91938
EH
314 struct sockaddr_in6 *ip6;
315
ddb37125 316 ip6 = (struct sockaddr_in6 *)&sa_local;
34f65dbb
RA
317 atype = ipv6_addr_type(&ip6->sin6_addr);
318 if (__ipv6_addr_needs_scope_id(atype) && !ip6->sin6_scope_id)
319 return -EINVAL;
320
d0f91938
EH
321 local->proto = htons(ETH_P_IPV6);
322 local->udp_port = ip6->sin6_port;
ddb37125 323 memcpy(&local->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr));
d0f91938
EH
324 ub->ifindex = ip6->sin6_scope_id;
325
ddb37125 326 ip6 = (struct sockaddr_in6 *)&sa_remote;
d0f91938
EH
327 remote->proto = htons(ETH_P_IPV6);
328 remote->udp_port = ip6->sin6_port;
ddb37125 329 memcpy(&remote->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr));
d0f91938
EH
330 return 0;
331#endif
332 }
333 return -EADDRNOTAVAIL;
334}
335
336/**
337 * tipc_udp_enable - callback to create a new udp bearer instance
338 * @net: network namespace
339 * @b: pointer to generic tipc_bearer
340 * @attrs: netlink bearer configuration
341 *
342 * validate the bearer parameters and initialize the udp bearer
343 * rtnl_lock should be held
344 */
345static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
346 struct nlattr *attrs[])
347{
348 int err = -EINVAL;
349 struct udp_bearer *ub;
350 struct udp_media_addr *remote;
351 struct udp_media_addr local = {0};
352 struct udp_port_cfg udp_conf = {0};
4fee6be8 353 struct udp_tunnel_sock_cfg tuncfg = {NULL};
d0f91938
EH
354
355 ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
356 if (!ub)
357 return -ENOMEM;
358
359 remote = (struct udp_media_addr *)&b->bcast_addr.value;
360 memset(remote, 0, sizeof(struct udp_media_addr));
361 err = parse_options(attrs, ub, &local, remote);
362 if (err)
363 goto err;
364
365 b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
366 b->bcast_addr.broadcast = 1;
367 rcu_assign_pointer(b->media_ptr, ub);
368 rcu_assign_pointer(ub->bearer, b);
369 tipc_udp_media_addr_set(&b->addr, &local);
4fee6be8 370 if (local.proto == htons(ETH_P_IP)) {
d0f91938
EH
371 struct net_device *dev;
372
373 dev = __ip_dev_find(net, local.ipv4.s_addr, false);
374 if (!dev) {
375 err = -ENODEV;
376 goto err;
377 }
378 udp_conf.family = AF_INET;
379 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
380 udp_conf.use_udp_checksums = false;
381 ub->ifindex = dev->ifindex;
382 b->mtu = dev->mtu - sizeof(struct iphdr)
383 - sizeof(struct udphdr);
384#if IS_ENABLED(CONFIG_IPV6)
4fee6be8 385 } else if (local.proto == htons(ETH_P_IPV6)) {
d0f91938
EH
386 udp_conf.family = AF_INET6;
387 udp_conf.use_udp6_tx_checksums = true;
388 udp_conf.use_udp6_rx_checksums = true;
389 udp_conf.local_ip6 = in6addr_any;
390 b->mtu = 1280;
391#endif
392 } else {
393 err = -EAFNOSUPPORT;
394 goto err;
395 }
396 udp_conf.local_udp_port = local.udp_port;
397 err = udp_sock_create(net, &udp_conf, &ub->ubsock);
398 if (err)
399 goto err;
400 tuncfg.sk_user_data = ub;
401 tuncfg.encap_type = 1;
402 tuncfg.encap_rcv = tipc_udp_recv;
403 tuncfg.encap_destroy = NULL;
404 setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
405
406 if (enable_mcast(ub, remote))
407 goto err;
408 return 0;
409err:
410 kfree(ub);
411 return err;
412}
413
414/* cleanup_bearer - break the socket/bearer association */
415static void cleanup_bearer(struct work_struct *work)
416{
417 struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
418
419 if (ub->ubsock)
420 udp_tunnel_sock_release(ub->ubsock);
421 synchronize_net();
422 kfree(ub);
423}
424
425/* tipc_udp_disable - detach bearer from socket */
426static void tipc_udp_disable(struct tipc_bearer *b)
427{
428 struct udp_bearer *ub;
429
430 ub = rcu_dereference_rtnl(b->media_ptr);
431 if (!ub) {
432 pr_err("UDP bearer instance not found\n");
433 return;
434 }
435 if (ub->ubsock)
436 sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
d0f91938
EH
437 RCU_INIT_POINTER(ub->bearer, NULL);
438
439 /* sock_release need to be done outside of rtnl lock */
440 INIT_WORK(&ub->work, cleanup_bearer);
441 schedule_work(&ub->work);
442}
443
444struct tipc_media udp_media_info = {
445 .send_msg = tipc_udp_send_msg,
446 .enable_media = tipc_udp_enable,
447 .disable_media = tipc_udp_disable,
448 .addr2str = tipc_udp_addr2str,
449 .addr2msg = tipc_udp_addr2msg,
450 .msg2addr = tipc_udp_msg2addr,
451 .priority = TIPC_DEF_LINK_PRI,
452 .tolerance = TIPC_DEF_LINK_TOL,
453 .window = TIPC_DEF_LINK_WIN,
454 .type_id = TIPC_MEDIA_TYPE_UDP,
455 .hwaddr_len = 0,
456 .name = "udp"
457};