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