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