batman-adv: netlink: add translation table query
[cascardo/linux.git] / net / batman-adv / netlink.c
1 /* Copyright (C) 2016 B.A.T.M.A.N. contributors:
2  *
3  * Matthias Schiffer
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "netlink.h"
19 #include "main.h"
20
21 #include <linux/errno.h>
22 #include <linux/fs.h>
23 #include <linux/genetlink.h>
24 #include <linux/if_ether.h>
25 #include <linux/init.h>
26 #include <linux/netdevice.h>
27 #include <linux/netlink.h>
28 #include <linux/printk.h>
29 #include <linux/rculist.h>
30 #include <linux/rcupdate.h>
31 #include <linux/skbuff.h>
32 #include <linux/stddef.h>
33 #include <linux/types.h>
34 #include <net/genetlink.h>
35 #include <net/netlink.h>
36 #include <net/sock.h>
37 #include <uapi/linux/batman_adv.h>
38
39 #include "bat_algo.h"
40 #include "hard-interface.h"
41 #include "soft-interface.h"
42 #include "tp_meter.h"
43 #include "translation-table.h"
44
45 struct genl_family batadv_netlink_family = {
46         .id = GENL_ID_GENERATE,
47         .hdrsize = 0,
48         .name = BATADV_NL_NAME,
49         .version = 1,
50         .maxattr = BATADV_ATTR_MAX,
51 };
52
53 /* multicast groups */
54 enum batadv_netlink_multicast_groups {
55         BATADV_NL_MCGRP_TPMETER,
56 };
57
58 static struct genl_multicast_group batadv_netlink_mcgrps[] = {
59         [BATADV_NL_MCGRP_TPMETER] = { .name = BATADV_NL_MCAST_GROUP_TPMETER },
60 };
61
62 static struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = {
63         [BATADV_ATTR_VERSION]           = { .type = NLA_STRING },
64         [BATADV_ATTR_ALGO_NAME]         = { .type = NLA_STRING },
65         [BATADV_ATTR_MESH_IFINDEX]      = { .type = NLA_U32 },
66         [BATADV_ATTR_MESH_IFNAME]       = { .type = NLA_STRING },
67         [BATADV_ATTR_MESH_ADDRESS]      = { .len = ETH_ALEN },
68         [BATADV_ATTR_HARD_IFINDEX]      = { .type = NLA_U32 },
69         [BATADV_ATTR_HARD_IFNAME]       = { .type = NLA_STRING },
70         [BATADV_ATTR_HARD_ADDRESS]      = { .len = ETH_ALEN },
71         [BATADV_ATTR_ORIG_ADDRESS]      = { .len = ETH_ALEN },
72         [BATADV_ATTR_TPMETER_RESULT]    = { .type = NLA_U8 },
73         [BATADV_ATTR_TPMETER_TEST_TIME] = { .type = NLA_U32 },
74         [BATADV_ATTR_TPMETER_BYTES]     = { .type = NLA_U64 },
75         [BATADV_ATTR_TPMETER_COOKIE]    = { .type = NLA_U32 },
76         [BATADV_ATTR_ACTIVE]            = { .type = NLA_FLAG },
77         [BATADV_ATTR_TT_ADDRESS]        = { .len = ETH_ALEN },
78         [BATADV_ATTR_TT_TTVN]           = { .type = NLA_U8 },
79         [BATADV_ATTR_TT_LAST_TTVN]      = { .type = NLA_U8 },
80         [BATADV_ATTR_TT_CRC32]          = { .type = NLA_U32 },
81         [BATADV_ATTR_TT_VID]            = { .type = NLA_U16 },
82         [BATADV_ATTR_TT_FLAGS]          = { .type = NLA_U32 },
83         [BATADV_ATTR_FLAG_BEST]         = { .type = NLA_FLAG },
84         [BATADV_ATTR_LAST_SEEN_MSECS]   = { .type = NLA_U32 },
85 };
86
87 /**
88  * batadv_netlink_get_ifindex - Extract an interface index from a message
89  * @nlh: Message header
90  * @attrtype: Attribute which holds an interface index
91  *
92  * Return: interface index, or 0.
93  */
94 int
95 batadv_netlink_get_ifindex(const struct nlmsghdr *nlh, int attrtype)
96 {
97         struct nlattr *attr = nlmsg_find_attr(nlh, GENL_HDRLEN, attrtype);
98
99         return attr ? nla_get_u32(attr) : 0;
100 }
101
102 /**
103  * batadv_netlink_mesh_info_put - fill in generic information about mesh
104  *  interface
105  * @msg: netlink message to be sent back
106  * @soft_iface: interface for which the data should be taken
107  *
108  * Return: 0 on success, < 0 on error
109  */
110 static int
111 batadv_netlink_mesh_info_put(struct sk_buff *msg, struct net_device *soft_iface)
112 {
113         struct batadv_priv *bat_priv = netdev_priv(soft_iface);
114         struct batadv_hard_iface *primary_if = NULL;
115         struct net_device *hard_iface;
116         int ret = -ENOBUFS;
117
118         if (nla_put_string(msg, BATADV_ATTR_VERSION, BATADV_SOURCE_VERSION) ||
119             nla_put_string(msg, BATADV_ATTR_ALGO_NAME,
120                            bat_priv->algo_ops->name) ||
121             nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, soft_iface->ifindex) ||
122             nla_put_string(msg, BATADV_ATTR_MESH_IFNAME, soft_iface->name) ||
123             nla_put(msg, BATADV_ATTR_MESH_ADDRESS, ETH_ALEN,
124                     soft_iface->dev_addr))
125                 goto out;
126
127         primary_if = batadv_primary_if_get_selected(bat_priv);
128         if (primary_if && primary_if->if_status == BATADV_IF_ACTIVE) {
129                 hard_iface = primary_if->net_dev;
130
131                 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
132                                 hard_iface->ifindex) ||
133                     nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
134                                    hard_iface->name) ||
135                     nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
136                             hard_iface->dev_addr))
137                         goto out;
138         }
139
140         ret = 0;
141
142  out:
143         if (primary_if)
144                 batadv_hardif_put(primary_if);
145
146         return ret;
147 }
148
149 /**
150  * batadv_netlink_get_mesh_info - handle incoming BATADV_CMD_GET_MESH_INFO
151  *  netlink request
152  * @skb: received netlink message
153  * @info: receiver information
154  *
155  * Return: 0 on success, < 0 on error
156  */
157 static int
158 batadv_netlink_get_mesh_info(struct sk_buff *skb, struct genl_info *info)
159 {
160         struct net *net = genl_info_net(info);
161         struct net_device *soft_iface;
162         struct sk_buff *msg = NULL;
163         void *msg_head;
164         int ifindex;
165         int ret;
166
167         if (!info->attrs[BATADV_ATTR_MESH_IFINDEX])
168                 return -EINVAL;
169
170         ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
171         if (!ifindex)
172                 return -EINVAL;
173
174         soft_iface = dev_get_by_index(net, ifindex);
175         if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
176                 ret = -ENODEV;
177                 goto out;
178         }
179
180         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
181         if (!msg) {
182                 ret = -ENOMEM;
183                 goto out;
184         }
185
186         msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq,
187                                &batadv_netlink_family, 0,
188                                BATADV_CMD_GET_MESH_INFO);
189         if (!msg_head) {
190                 ret = -ENOBUFS;
191                 goto out;
192         }
193
194         ret = batadv_netlink_mesh_info_put(msg, soft_iface);
195
196  out:
197         if (soft_iface)
198                 dev_put(soft_iface);
199
200         if (ret) {
201                 if (msg)
202                         nlmsg_free(msg);
203                 return ret;
204         }
205
206         genlmsg_end(msg, msg_head);
207         return genlmsg_reply(msg, info);
208 }
209
210 /**
211  * batadv_netlink_tp_meter_put - Fill information of started tp_meter session
212  * @msg: netlink message to be sent back
213  * @cookie: tp meter session cookie
214  *
215  *  Return: 0 on success, < 0 on error
216  */
217 static int
218 batadv_netlink_tp_meter_put(struct sk_buff *msg, u32 cookie)
219 {
220         if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
221                 return -ENOBUFS;
222
223         return 0;
224 }
225
226 /**
227  * batadv_netlink_tpmeter_notify - send tp_meter result via netlink to client
228  * @bat_priv: the bat priv with all the soft interface information
229  * @dst: destination of tp_meter session
230  * @result: reason for tp meter session stop
231  * @test_time: total time ot the tp_meter session
232  * @total_bytes: bytes acked to the receiver
233  * @cookie: cookie of tp_meter session
234  *
235  * Return: 0 on success, < 0 on error
236  */
237 int batadv_netlink_tpmeter_notify(struct batadv_priv *bat_priv, const u8 *dst,
238                                   u8 result, u32 test_time, u64 total_bytes,
239                                   u32 cookie)
240 {
241         struct sk_buff *msg;
242         void *hdr;
243         int ret;
244
245         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
246         if (!msg)
247                 return -ENOMEM;
248
249         hdr = genlmsg_put(msg, 0, 0, &batadv_netlink_family, 0,
250                           BATADV_CMD_TP_METER);
251         if (!hdr) {
252                 ret = -ENOBUFS;
253                 goto err_genlmsg;
254         }
255
256         if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
257                 goto nla_put_failure;
258
259         if (nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, test_time))
260                 goto nla_put_failure;
261
262         if (nla_put_u64_64bit(msg, BATADV_ATTR_TPMETER_BYTES, total_bytes,
263                               BATADV_ATTR_PAD))
264                 goto nla_put_failure;
265
266         if (nla_put_u8(msg, BATADV_ATTR_TPMETER_RESULT, result))
267                 goto nla_put_failure;
268
269         if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst))
270                 goto nla_put_failure;
271
272         genlmsg_end(msg, hdr);
273
274         genlmsg_multicast_netns(&batadv_netlink_family,
275                                 dev_net(bat_priv->soft_iface), msg, 0,
276                                 BATADV_NL_MCGRP_TPMETER, GFP_KERNEL);
277
278         return 0;
279
280 nla_put_failure:
281         genlmsg_cancel(msg, hdr);
282         ret = -EMSGSIZE;
283
284 err_genlmsg:
285         nlmsg_free(msg);
286         return ret;
287 }
288
289 /**
290  * batadv_netlink_tp_meter_start - Start a new tp_meter session
291  * @skb: received netlink message
292  * @info: receiver information
293  *
294  * Return: 0 on success, < 0 on error
295  */
296 static int
297 batadv_netlink_tp_meter_start(struct sk_buff *skb, struct genl_info *info)
298 {
299         struct net *net = genl_info_net(info);
300         struct net_device *soft_iface;
301         struct batadv_priv *bat_priv;
302         struct sk_buff *msg = NULL;
303         u32 test_length;
304         void *msg_head;
305         int ifindex;
306         u32 cookie;
307         u8 *dst;
308         int ret;
309
310         if (!info->attrs[BATADV_ATTR_MESH_IFINDEX])
311                 return -EINVAL;
312
313         if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
314                 return -EINVAL;
315
316         if (!info->attrs[BATADV_ATTR_TPMETER_TEST_TIME])
317                 return -EINVAL;
318
319         ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
320         if (!ifindex)
321                 return -EINVAL;
322
323         dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
324
325         test_length = nla_get_u32(info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]);
326
327         soft_iface = dev_get_by_index(net, ifindex);
328         if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
329                 ret = -ENODEV;
330                 goto out;
331         }
332
333         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
334         if (!msg) {
335                 ret = -ENOMEM;
336                 goto out;
337         }
338
339         msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq,
340                                &batadv_netlink_family, 0,
341                                BATADV_CMD_TP_METER);
342         if (!msg_head) {
343                 ret = -ENOBUFS;
344                 goto out;
345         }
346
347         bat_priv = netdev_priv(soft_iface);
348         batadv_tp_start(bat_priv, dst, test_length, &cookie);
349
350         ret = batadv_netlink_tp_meter_put(msg, cookie);
351
352  out:
353         if (soft_iface)
354                 dev_put(soft_iface);
355
356         if (ret) {
357                 if (msg)
358                         nlmsg_free(msg);
359                 return ret;
360         }
361
362         genlmsg_end(msg, msg_head);
363         return genlmsg_reply(msg, info);
364 }
365
366 /**
367  * batadv_netlink_tp_meter_start - Cancel a running tp_meter session
368  * @skb: received netlink message
369  * @info: receiver information
370  *
371  * Return: 0 on success, < 0 on error
372  */
373 static int
374 batadv_netlink_tp_meter_cancel(struct sk_buff *skb, struct genl_info *info)
375 {
376         struct net *net = genl_info_net(info);
377         struct net_device *soft_iface;
378         struct batadv_priv *bat_priv;
379         int ifindex;
380         u8 *dst;
381         int ret = 0;
382
383         if (!info->attrs[BATADV_ATTR_MESH_IFINDEX])
384                 return -EINVAL;
385
386         if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
387                 return -EINVAL;
388
389         ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
390         if (!ifindex)
391                 return -EINVAL;
392
393         dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
394
395         soft_iface = dev_get_by_index(net, ifindex);
396         if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
397                 ret = -ENODEV;
398                 goto out;
399         }
400
401         bat_priv = netdev_priv(soft_iface);
402         batadv_tp_stop(bat_priv, dst, BATADV_TP_REASON_CANCEL);
403
404 out:
405         if (soft_iface)
406                 dev_put(soft_iface);
407
408         return ret;
409 }
410
411 /**
412  * batadv_netlink_dump_hardif_entry - Dump one hard interface into a message
413  * @msg: Netlink message to dump into
414  * @portid: Port making netlink request
415  * @seq: Sequence number of netlink message
416  * @hard_iface: Hard interface to dump
417  *
418  * Return: error code, or 0 on success
419  */
420 static int
421 batadv_netlink_dump_hardif_entry(struct sk_buff *msg, u32 portid, u32 seq,
422                                  struct batadv_hard_iface *hard_iface)
423 {
424         struct net_device *net_dev = hard_iface->net_dev;
425         void *hdr;
426
427         hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
428                           BATADV_CMD_GET_HARDIFS);
429         if (!hdr)
430                 return -EMSGSIZE;
431
432         if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
433                         net_dev->ifindex) ||
434             nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
435                            net_dev->name) ||
436             nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
437                     net_dev->dev_addr))
438                 goto nla_put_failure;
439
440         if (hard_iface->if_status == BATADV_IF_ACTIVE) {
441                 if (nla_put_flag(msg, BATADV_ATTR_ACTIVE))
442                         goto nla_put_failure;
443         }
444
445         genlmsg_end(msg, hdr);
446         return 0;
447
448  nla_put_failure:
449         genlmsg_cancel(msg, hdr);
450         return -EMSGSIZE;
451 }
452
453 /**
454  * batadv_netlink_dump_hardifs - Dump all hard interface into a messages
455  * @msg: Netlink message to dump into
456  * @cb: Parameters from query
457  *
458  * Return: error code, or length of reply message on success
459  */
460 static int
461 batadv_netlink_dump_hardifs(struct sk_buff *msg, struct netlink_callback *cb)
462 {
463         struct net *net = sock_net(cb->skb->sk);
464         struct net_device *soft_iface;
465         struct batadv_hard_iface *hard_iface;
466         int ifindex;
467         int portid = NETLINK_CB(cb->skb).portid;
468         int seq = cb->nlh->nlmsg_seq;
469         int skip = cb->args[0];
470         int i = 0;
471
472         ifindex = batadv_netlink_get_ifindex(cb->nlh,
473                                              BATADV_ATTR_MESH_IFINDEX);
474         if (!ifindex)
475                 return -EINVAL;
476
477         soft_iface = dev_get_by_index(net, ifindex);
478         if (!soft_iface)
479                 return -ENODEV;
480
481         if (!batadv_softif_is_valid(soft_iface)) {
482                 dev_put(soft_iface);
483                 return -ENODEV;
484         }
485
486         rcu_read_lock();
487
488         list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
489                 if (hard_iface->soft_iface != soft_iface)
490                         continue;
491
492                 if (i++ < skip)
493                         continue;
494
495                 if (batadv_netlink_dump_hardif_entry(msg, portid, seq,
496                                                      hard_iface)) {
497                         i--;
498                         break;
499                 }
500         }
501
502         rcu_read_unlock();
503
504         dev_put(soft_iface);
505
506         cb->args[0] = i;
507
508         return msg->len;
509 }
510
511 static struct genl_ops batadv_netlink_ops[] = {
512         {
513                 .cmd = BATADV_CMD_GET_MESH_INFO,
514                 .flags = GENL_ADMIN_PERM,
515                 .policy = batadv_netlink_policy,
516                 .doit = batadv_netlink_get_mesh_info,
517         },
518         {
519                 .cmd = BATADV_CMD_TP_METER,
520                 .flags = GENL_ADMIN_PERM,
521                 .policy = batadv_netlink_policy,
522                 .doit = batadv_netlink_tp_meter_start,
523         },
524         {
525                 .cmd = BATADV_CMD_TP_METER_CANCEL,
526                 .flags = GENL_ADMIN_PERM,
527                 .policy = batadv_netlink_policy,
528                 .doit = batadv_netlink_tp_meter_cancel,
529         },
530         {
531                 .cmd = BATADV_CMD_GET_ROUTING_ALGOS,
532                 .flags = GENL_ADMIN_PERM,
533                 .policy = batadv_netlink_policy,
534                 .dumpit = batadv_algo_dump,
535         },
536         {
537                 .cmd = BATADV_CMD_GET_HARDIFS,
538                 .flags = GENL_ADMIN_PERM,
539                 .policy = batadv_netlink_policy,
540                 .dumpit = batadv_netlink_dump_hardifs,
541         },
542         {
543                 .cmd = BATADV_CMD_GET_TRANSTABLE_LOCAL,
544                 .flags = GENL_ADMIN_PERM,
545                 .policy = batadv_netlink_policy,
546                 .dumpit = batadv_tt_local_dump,
547         },
548         {
549                 .cmd = BATADV_CMD_GET_TRANSTABLE_GLOBAL,
550                 .flags = GENL_ADMIN_PERM,
551                 .policy = batadv_netlink_policy,
552                 .dumpit = batadv_tt_global_dump,
553         },
554 };
555
556 /**
557  * batadv_netlink_register - register batadv genl netlink family
558  */
559 void __init batadv_netlink_register(void)
560 {
561         int ret;
562
563         ret = genl_register_family_with_ops_groups(&batadv_netlink_family,
564                                                    batadv_netlink_ops,
565                                                    batadv_netlink_mcgrps);
566         if (ret)
567                 pr_warn("unable to register netlink family");
568 }
569
570 /**
571  * batadv_netlink_unregister - unregister batadv genl netlink family
572  */
573 void batadv_netlink_unregister(void)
574 {
575         genl_unregister_family(&batadv_netlink_family);
576 }