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