1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2016-2018 B.A.T.M.A.N. contributors:
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <linux/atomic.h>
23 #include <linux/byteorder/generic.h>
24 #include <linux/cache.h>
25 #include <linux/errno.h>
26 #include <linux/export.h>
27 #include <linux/genetlink.h>
28 #include <linux/gfp.h>
29 #include <linux/if_ether.h>
30 #include <linux/init.h>
31 #include <linux/kernel.h>
32 #include <linux/netdevice.h>
33 #include <linux/netlink.h>
34 #include <linux/printk.h>
35 #include <linux/rculist.h>
36 #include <linux/rcupdate.h>
37 #include <linux/skbuff.h>
38 #include <linux/stddef.h>
39 #include <linux/types.h>
40 #include <net/genetlink.h>
41 #include <net/netlink.h>
43 #include <uapi/linux/batadv_packet.h>
44 #include <uapi/linux/batman_adv.h>
47 #include "bridge_loop_avoidance.h"
48 #include "distributed-arp-table.h"
49 #include "gateway_client.h"
50 #include "hard-interface.h"
51 #include "multicast.h"
52 #include "originator.h"
53 #include "soft-interface.h"
55 #include "translation-table.h"
57 struct genl_family batadv_netlink_family
;
59 /* multicast groups */
60 enum batadv_netlink_multicast_groups
{
61 BATADV_NL_MCGRP_TPMETER
,
64 static const struct genl_multicast_group batadv_netlink_mcgrps
[] = {
65 [BATADV_NL_MCGRP_TPMETER
] = { .name
= BATADV_NL_MCAST_GROUP_TPMETER
},
68 static const 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 [BATADV_ATTR_DAT_CACHE_IP4ADDRESS
] = { .type
= NLA_U32
},
103 [BATADV_ATTR_DAT_CACHE_HWADDRESS
] = { .len
= ETH_ALEN
},
104 [BATADV_ATTR_DAT_CACHE_VID
] = { .type
= NLA_U16
},
105 [BATADV_ATTR_MCAST_FLAGS
] = { .type
= NLA_U32
},
106 [BATADV_ATTR_MCAST_FLAGS_PRIV
] = { .type
= NLA_U32
},
110 * batadv_netlink_get_ifindex() - Extract an interface index from a message
111 * @nlh: Message header
112 * @attrtype: Attribute which holds an interface index
114 * Return: interface index, or 0.
117 batadv_netlink_get_ifindex(const struct nlmsghdr
*nlh
, int attrtype
)
119 struct nlattr
*attr
= nlmsg_find_attr(nlh
, GENL_HDRLEN
, attrtype
);
121 return attr
? nla_get_u32(attr
) : 0;
125 * batadv_netlink_mesh_info_put() - fill in generic information about mesh
127 * @msg: netlink message to be sent back
128 * @soft_iface: interface for which the data should be taken
130 * Return: 0 on success, < 0 on error
133 batadv_netlink_mesh_info_put(struct sk_buff
*msg
, struct net_device
*soft_iface
)
135 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
136 struct batadv_hard_iface
*primary_if
= NULL
;
137 struct net_device
*hard_iface
;
140 if (nla_put_string(msg
, BATADV_ATTR_VERSION
, BATADV_SOURCE_VERSION
) ||
141 nla_put_string(msg
, BATADV_ATTR_ALGO_NAME
,
142 bat_priv
->algo_ops
->name
) ||
143 nla_put_u32(msg
, BATADV_ATTR_MESH_IFINDEX
, soft_iface
->ifindex
) ||
144 nla_put_string(msg
, BATADV_ATTR_MESH_IFNAME
, soft_iface
->name
) ||
145 nla_put(msg
, BATADV_ATTR_MESH_ADDRESS
, ETH_ALEN
,
146 soft_iface
->dev_addr
) ||
147 nla_put_u8(msg
, BATADV_ATTR_TT_TTVN
,
148 (u8
)atomic_read(&bat_priv
->tt
.vn
)))
151 #ifdef CONFIG_BATMAN_ADV_BLA
152 if (nla_put_u16(msg
, BATADV_ATTR_BLA_CRC
,
153 ntohs(bat_priv
->bla
.claim_dest
.group
)))
157 if (batadv_mcast_mesh_info_put(msg
, bat_priv
))
160 primary_if
= batadv_primary_if_get_selected(bat_priv
);
161 if (primary_if
&& primary_if
->if_status
== BATADV_IF_ACTIVE
) {
162 hard_iface
= primary_if
->net_dev
;
164 if (nla_put_u32(msg
, BATADV_ATTR_HARD_IFINDEX
,
165 hard_iface
->ifindex
) ||
166 nla_put_string(msg
, BATADV_ATTR_HARD_IFNAME
,
168 nla_put(msg
, BATADV_ATTR_HARD_ADDRESS
, ETH_ALEN
,
169 hard_iface
->dev_addr
))
177 batadv_hardif_put(primary_if
);
183 * batadv_netlink_get_mesh_info() - handle incoming BATADV_CMD_GET_MESH_INFO
185 * @skb: received netlink message
186 * @info: receiver information
188 * Return: 0 on success, < 0 on error
191 batadv_netlink_get_mesh_info(struct sk_buff
*skb
, struct genl_info
*info
)
193 struct net
*net
= genl_info_net(info
);
194 struct net_device
*soft_iface
;
195 struct sk_buff
*msg
= NULL
;
200 if (!info
->attrs
[BATADV_ATTR_MESH_IFINDEX
])
203 ifindex
= nla_get_u32(info
->attrs
[BATADV_ATTR_MESH_IFINDEX
]);
207 soft_iface
= dev_get_by_index(net
, ifindex
);
208 if (!soft_iface
|| !batadv_softif_is_valid(soft_iface
)) {
213 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
219 msg_head
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
,
220 &batadv_netlink_family
, 0,
221 BATADV_CMD_GET_MESH_INFO
);
227 ret
= batadv_netlink_mesh_info_put(msg
, soft_iface
);
239 genlmsg_end(msg
, msg_head
);
240 return genlmsg_reply(msg
, info
);
244 * batadv_netlink_tp_meter_put() - Fill information of started tp_meter session
245 * @msg: netlink message to be sent back
246 * @cookie: tp meter session cookie
248 * Return: 0 on success, < 0 on error
251 batadv_netlink_tp_meter_put(struct sk_buff
*msg
, u32 cookie
)
253 if (nla_put_u32(msg
, BATADV_ATTR_TPMETER_COOKIE
, cookie
))
260 * batadv_netlink_tpmeter_notify() - send tp_meter result via netlink to client
261 * @bat_priv: the bat priv with all the soft interface information
262 * @dst: destination of tp_meter session
263 * @result: reason for tp meter session stop
264 * @test_time: total time ot the tp_meter session
265 * @total_bytes: bytes acked to the receiver
266 * @cookie: cookie of tp_meter session
268 * Return: 0 on success, < 0 on error
270 int batadv_netlink_tpmeter_notify(struct batadv_priv
*bat_priv
, const u8
*dst
,
271 u8 result
, u32 test_time
, u64 total_bytes
,
278 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
282 hdr
= genlmsg_put(msg
, 0, 0, &batadv_netlink_family
, 0,
283 BATADV_CMD_TP_METER
);
289 if (nla_put_u32(msg
, BATADV_ATTR_TPMETER_COOKIE
, cookie
))
290 goto nla_put_failure
;
292 if (nla_put_u32(msg
, BATADV_ATTR_TPMETER_TEST_TIME
, test_time
))
293 goto nla_put_failure
;
295 if (nla_put_u64_64bit(msg
, BATADV_ATTR_TPMETER_BYTES
, total_bytes
,
297 goto nla_put_failure
;
299 if (nla_put_u8(msg
, BATADV_ATTR_TPMETER_RESULT
, result
))
300 goto nla_put_failure
;
302 if (nla_put(msg
, BATADV_ATTR_ORIG_ADDRESS
, ETH_ALEN
, dst
))
303 goto nla_put_failure
;
305 genlmsg_end(msg
, hdr
);
307 genlmsg_multicast_netns(&batadv_netlink_family
,
308 dev_net(bat_priv
->soft_iface
), msg
, 0,
309 BATADV_NL_MCGRP_TPMETER
, GFP_KERNEL
);
314 genlmsg_cancel(msg
, hdr
);
323 * batadv_netlink_tp_meter_start() - Start a new tp_meter session
324 * @skb: received netlink message
325 * @info: receiver information
327 * Return: 0 on success, < 0 on error
330 batadv_netlink_tp_meter_start(struct sk_buff
*skb
, struct genl_info
*info
)
332 struct net
*net
= genl_info_net(info
);
333 struct net_device
*soft_iface
;
334 struct batadv_priv
*bat_priv
;
335 struct sk_buff
*msg
= NULL
;
343 if (!info
->attrs
[BATADV_ATTR_MESH_IFINDEX
])
346 if (!info
->attrs
[BATADV_ATTR_ORIG_ADDRESS
])
349 if (!info
->attrs
[BATADV_ATTR_TPMETER_TEST_TIME
])
352 ifindex
= nla_get_u32(info
->attrs
[BATADV_ATTR_MESH_IFINDEX
]);
356 dst
= nla_data(info
->attrs
[BATADV_ATTR_ORIG_ADDRESS
]);
358 test_length
= nla_get_u32(info
->attrs
[BATADV_ATTR_TPMETER_TEST_TIME
]);
360 soft_iface
= dev_get_by_index(net
, ifindex
);
361 if (!soft_iface
|| !batadv_softif_is_valid(soft_iface
)) {
366 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
372 msg_head
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
,
373 &batadv_netlink_family
, 0,
374 BATADV_CMD_TP_METER
);
380 bat_priv
= netdev_priv(soft_iface
);
381 batadv_tp_start(bat_priv
, dst
, test_length
, &cookie
);
383 ret
= batadv_netlink_tp_meter_put(msg
, cookie
);
395 genlmsg_end(msg
, msg_head
);
396 return genlmsg_reply(msg
, info
);
400 * batadv_netlink_tp_meter_start() - Cancel a running tp_meter session
401 * @skb: received netlink message
402 * @info: receiver information
404 * Return: 0 on success, < 0 on error
407 batadv_netlink_tp_meter_cancel(struct sk_buff
*skb
, struct genl_info
*info
)
409 struct net
*net
= genl_info_net(info
);
410 struct net_device
*soft_iface
;
411 struct batadv_priv
*bat_priv
;
416 if (!info
->attrs
[BATADV_ATTR_MESH_IFINDEX
])
419 if (!info
->attrs
[BATADV_ATTR_ORIG_ADDRESS
])
422 ifindex
= nla_get_u32(info
->attrs
[BATADV_ATTR_MESH_IFINDEX
]);
426 dst
= nla_data(info
->attrs
[BATADV_ATTR_ORIG_ADDRESS
]);
428 soft_iface
= dev_get_by_index(net
, ifindex
);
429 if (!soft_iface
|| !batadv_softif_is_valid(soft_iface
)) {
434 bat_priv
= netdev_priv(soft_iface
);
435 batadv_tp_stop(bat_priv
, dst
, BATADV_TP_REASON_CANCEL
);
445 * batadv_netlink_dump_hardif_entry() - Dump one hard interface into a message
446 * @msg: Netlink message to dump into
447 * @portid: Port making netlink request
448 * @seq: Sequence number of netlink message
449 * @hard_iface: Hard interface to dump
451 * Return: error code, or 0 on success
454 batadv_netlink_dump_hardif_entry(struct sk_buff
*msg
, u32 portid
, u32 seq
,
455 struct batadv_hard_iface
*hard_iface
)
457 struct net_device
*net_dev
= hard_iface
->net_dev
;
460 hdr
= genlmsg_put(msg
, portid
, seq
, &batadv_netlink_family
, NLM_F_MULTI
,
461 BATADV_CMD_GET_HARDIFS
);
465 if (nla_put_u32(msg
, BATADV_ATTR_HARD_IFINDEX
,
467 nla_put_string(msg
, BATADV_ATTR_HARD_IFNAME
,
469 nla_put(msg
, BATADV_ATTR_HARD_ADDRESS
, ETH_ALEN
,
471 goto nla_put_failure
;
473 if (hard_iface
->if_status
== BATADV_IF_ACTIVE
) {
474 if (nla_put_flag(msg
, BATADV_ATTR_ACTIVE
))
475 goto nla_put_failure
;
478 genlmsg_end(msg
, hdr
);
482 genlmsg_cancel(msg
, hdr
);
487 * batadv_netlink_dump_hardifs() - Dump all hard interface into a messages
488 * @msg: Netlink message to dump into
489 * @cb: Parameters from query
491 * Return: error code, or length of reply message on success
494 batadv_netlink_dump_hardifs(struct sk_buff
*msg
, struct netlink_callback
*cb
)
496 struct net
*net
= sock_net(cb
->skb
->sk
);
497 struct net_device
*soft_iface
;
498 struct batadv_hard_iface
*hard_iface
;
500 int portid
= NETLINK_CB(cb
->skb
).portid
;
501 int seq
= cb
->nlh
->nlmsg_seq
;
502 int skip
= cb
->args
[0];
505 ifindex
= batadv_netlink_get_ifindex(cb
->nlh
,
506 BATADV_ATTR_MESH_IFINDEX
);
510 soft_iface
= dev_get_by_index(net
, ifindex
);
514 if (!batadv_softif_is_valid(soft_iface
)) {
521 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
522 if (hard_iface
->soft_iface
!= soft_iface
)
528 if (batadv_netlink_dump_hardif_entry(msg
, portid
, seq
,
544 static const struct genl_ops batadv_netlink_ops
[] = {
546 .cmd
= BATADV_CMD_GET_MESH_INFO
,
547 .flags
= GENL_ADMIN_PERM
,
548 .policy
= batadv_netlink_policy
,
549 .doit
= batadv_netlink_get_mesh_info
,
552 .cmd
= BATADV_CMD_TP_METER
,
553 .flags
= GENL_ADMIN_PERM
,
554 .policy
= batadv_netlink_policy
,
555 .doit
= batadv_netlink_tp_meter_start
,
558 .cmd
= BATADV_CMD_TP_METER_CANCEL
,
559 .flags
= GENL_ADMIN_PERM
,
560 .policy
= batadv_netlink_policy
,
561 .doit
= batadv_netlink_tp_meter_cancel
,
564 .cmd
= BATADV_CMD_GET_ROUTING_ALGOS
,
565 .flags
= GENL_ADMIN_PERM
,
566 .policy
= batadv_netlink_policy
,
567 .dumpit
= batadv_algo_dump
,
570 .cmd
= BATADV_CMD_GET_HARDIFS
,
571 .flags
= GENL_ADMIN_PERM
,
572 .policy
= batadv_netlink_policy
,
573 .dumpit
= batadv_netlink_dump_hardifs
,
576 .cmd
= BATADV_CMD_GET_TRANSTABLE_LOCAL
,
577 .flags
= GENL_ADMIN_PERM
,
578 .policy
= batadv_netlink_policy
,
579 .dumpit
= batadv_tt_local_dump
,
582 .cmd
= BATADV_CMD_GET_TRANSTABLE_GLOBAL
,
583 .flags
= GENL_ADMIN_PERM
,
584 .policy
= batadv_netlink_policy
,
585 .dumpit
= batadv_tt_global_dump
,
588 .cmd
= BATADV_CMD_GET_ORIGINATORS
,
589 .flags
= GENL_ADMIN_PERM
,
590 .policy
= batadv_netlink_policy
,
591 .dumpit
= batadv_orig_dump
,
594 .cmd
= BATADV_CMD_GET_NEIGHBORS
,
595 .flags
= GENL_ADMIN_PERM
,
596 .policy
= batadv_netlink_policy
,
597 .dumpit
= batadv_hardif_neigh_dump
,
600 .cmd
= BATADV_CMD_GET_GATEWAYS
,
601 .flags
= GENL_ADMIN_PERM
,
602 .policy
= batadv_netlink_policy
,
603 .dumpit
= batadv_gw_dump
,
606 .cmd
= BATADV_CMD_GET_BLA_CLAIM
,
607 .flags
= GENL_ADMIN_PERM
,
608 .policy
= batadv_netlink_policy
,
609 .dumpit
= batadv_bla_claim_dump
,
612 .cmd
= BATADV_CMD_GET_BLA_BACKBONE
,
613 .flags
= GENL_ADMIN_PERM
,
614 .policy
= batadv_netlink_policy
,
615 .dumpit
= batadv_bla_backbone_dump
,
618 .cmd
= BATADV_CMD_GET_DAT_CACHE
,
619 .flags
= GENL_ADMIN_PERM
,
620 .policy
= batadv_netlink_policy
,
621 .dumpit
= batadv_dat_cache_dump
,
624 .cmd
= BATADV_CMD_GET_MCAST_FLAGS
,
625 .flags
= GENL_ADMIN_PERM
,
626 .policy
= batadv_netlink_policy
,
627 .dumpit
= batadv_mcast_flags_dump
,
632 struct genl_family batadv_netlink_family __ro_after_init
= {
634 .name
= BATADV_NL_NAME
,
636 .maxattr
= BATADV_ATTR_MAX
,
638 .module
= THIS_MODULE
,
639 .ops
= batadv_netlink_ops
,
640 .n_ops
= ARRAY_SIZE(batadv_netlink_ops
),
641 .mcgrps
= batadv_netlink_mcgrps
,
642 .n_mcgrps
= ARRAY_SIZE(batadv_netlink_mcgrps
),
646 * batadv_netlink_register() - register batadv genl netlink family
648 void __init
batadv_netlink_register(void)
652 ret
= genl_register_family(&batadv_netlink_family
);
654 pr_warn("unable to register netlink family");
658 * batadv_netlink_unregister() - unregister batadv genl netlink family
660 void batadv_netlink_unregister(void)
662 genl_unregister_family(&batadv_netlink_family
);