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