1 /* Copyright (C) 2016 B.A.T.M.A.N. contributors:
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.
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.
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/>.
21 #include <linux/errno.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>
37 #include <uapi/linux/batman_adv.h>
40 #include "hard-interface.h"
41 #include "soft-interface.h"
43 #include "translation-table.h"
45 struct genl_family batadv_netlink_family
= {
46 .id
= GENL_ID_GENERATE
,
48 .name
= BATADV_NL_NAME
,
50 .maxattr
= BATADV_ATTR_MAX
,
53 /* multicast groups */
54 enum batadv_netlink_multicast_groups
{
55 BATADV_NL_MCGRP_TPMETER
,
58 static struct genl_multicast_group batadv_netlink_mcgrps
[] = {
59 [BATADV_NL_MCGRP_TPMETER
] = { .name
= BATADV_NL_MCAST_GROUP_TPMETER
},
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
},
88 * batadv_netlink_get_ifindex - Extract an interface index from a message
89 * @nlh: Message header
90 * @attrtype: Attribute which holds an interface index
92 * Return: interface index, or 0.
95 batadv_netlink_get_ifindex(const struct nlmsghdr
*nlh
, int attrtype
)
97 struct nlattr
*attr
= nlmsg_find_attr(nlh
, GENL_HDRLEN
, attrtype
);
99 return attr
? nla_get_u32(attr
) : 0;
103 * batadv_netlink_mesh_info_put - fill in generic information about mesh
105 * @msg: netlink message to be sent back
106 * @soft_iface: interface for which the data should be taken
108 * Return: 0 on success, < 0 on error
111 batadv_netlink_mesh_info_put(struct sk_buff
*msg
, struct net_device
*soft_iface
)
113 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
114 struct batadv_hard_iface
*primary_if
= NULL
;
115 struct net_device
*hard_iface
;
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
))
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
;
131 if (nla_put_u32(msg
, BATADV_ATTR_HARD_IFINDEX
,
132 hard_iface
->ifindex
) ||
133 nla_put_string(msg
, BATADV_ATTR_HARD_IFNAME
,
135 nla_put(msg
, BATADV_ATTR_HARD_ADDRESS
, ETH_ALEN
,
136 hard_iface
->dev_addr
))
144 batadv_hardif_put(primary_if
);
150 * batadv_netlink_get_mesh_info - handle incoming BATADV_CMD_GET_MESH_INFO
152 * @skb: received netlink message
153 * @info: receiver information
155 * Return: 0 on success, < 0 on error
158 batadv_netlink_get_mesh_info(struct sk_buff
*skb
, struct genl_info
*info
)
160 struct net
*net
= genl_info_net(info
);
161 struct net_device
*soft_iface
;
162 struct sk_buff
*msg
= NULL
;
167 if (!info
->attrs
[BATADV_ATTR_MESH_IFINDEX
])
170 ifindex
= nla_get_u32(info
->attrs
[BATADV_ATTR_MESH_IFINDEX
]);
174 soft_iface
= dev_get_by_index(net
, ifindex
);
175 if (!soft_iface
|| !batadv_softif_is_valid(soft_iface
)) {
180 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
186 msg_head
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
,
187 &batadv_netlink_family
, 0,
188 BATADV_CMD_GET_MESH_INFO
);
194 ret
= batadv_netlink_mesh_info_put(msg
, soft_iface
);
206 genlmsg_end(msg
, msg_head
);
207 return genlmsg_reply(msg
, info
);
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
215 * Return: 0 on success, < 0 on error
218 batadv_netlink_tp_meter_put(struct sk_buff
*msg
, u32 cookie
)
220 if (nla_put_u32(msg
, BATADV_ATTR_TPMETER_COOKIE
, cookie
))
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
235 * Return: 0 on success, < 0 on error
237 int batadv_netlink_tpmeter_notify(struct batadv_priv
*bat_priv
, const u8
*dst
,
238 u8 result
, u32 test_time
, u64 total_bytes
,
245 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
249 hdr
= genlmsg_put(msg
, 0, 0, &batadv_netlink_family
, 0,
250 BATADV_CMD_TP_METER
);
256 if (nla_put_u32(msg
, BATADV_ATTR_TPMETER_COOKIE
, cookie
))
257 goto nla_put_failure
;
259 if (nla_put_u32(msg
, BATADV_ATTR_TPMETER_TEST_TIME
, test_time
))
260 goto nla_put_failure
;
262 if (nla_put_u64_64bit(msg
, BATADV_ATTR_TPMETER_BYTES
, total_bytes
,
264 goto nla_put_failure
;
266 if (nla_put_u8(msg
, BATADV_ATTR_TPMETER_RESULT
, result
))
267 goto nla_put_failure
;
269 if (nla_put(msg
, BATADV_ATTR_ORIG_ADDRESS
, ETH_ALEN
, dst
))
270 goto nla_put_failure
;
272 genlmsg_end(msg
, hdr
);
274 genlmsg_multicast_netns(&batadv_netlink_family
,
275 dev_net(bat_priv
->soft_iface
), msg
, 0,
276 BATADV_NL_MCGRP_TPMETER
, GFP_KERNEL
);
281 genlmsg_cancel(msg
, hdr
);
290 * batadv_netlink_tp_meter_start - Start a new tp_meter session
291 * @skb: received netlink message
292 * @info: receiver information
294 * Return: 0 on success, < 0 on error
297 batadv_netlink_tp_meter_start(struct sk_buff
*skb
, struct genl_info
*info
)
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
;
310 if (!info
->attrs
[BATADV_ATTR_MESH_IFINDEX
])
313 if (!info
->attrs
[BATADV_ATTR_ORIG_ADDRESS
])
316 if (!info
->attrs
[BATADV_ATTR_TPMETER_TEST_TIME
])
319 ifindex
= nla_get_u32(info
->attrs
[BATADV_ATTR_MESH_IFINDEX
]);
323 dst
= nla_data(info
->attrs
[BATADV_ATTR_ORIG_ADDRESS
]);
325 test_length
= nla_get_u32(info
->attrs
[BATADV_ATTR_TPMETER_TEST_TIME
]);
327 soft_iface
= dev_get_by_index(net
, ifindex
);
328 if (!soft_iface
|| !batadv_softif_is_valid(soft_iface
)) {
333 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
339 msg_head
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
,
340 &batadv_netlink_family
, 0,
341 BATADV_CMD_TP_METER
);
347 bat_priv
= netdev_priv(soft_iface
);
348 batadv_tp_start(bat_priv
, dst
, test_length
, &cookie
);
350 ret
= batadv_netlink_tp_meter_put(msg
, cookie
);
362 genlmsg_end(msg
, msg_head
);
363 return genlmsg_reply(msg
, info
);
367 * batadv_netlink_tp_meter_start - Cancel a running tp_meter session
368 * @skb: received netlink message
369 * @info: receiver information
371 * Return: 0 on success, < 0 on error
374 batadv_netlink_tp_meter_cancel(struct sk_buff
*skb
, struct genl_info
*info
)
376 struct net
*net
= genl_info_net(info
);
377 struct net_device
*soft_iface
;
378 struct batadv_priv
*bat_priv
;
383 if (!info
->attrs
[BATADV_ATTR_MESH_IFINDEX
])
386 if (!info
->attrs
[BATADV_ATTR_ORIG_ADDRESS
])
389 ifindex
= nla_get_u32(info
->attrs
[BATADV_ATTR_MESH_IFINDEX
]);
393 dst
= nla_data(info
->attrs
[BATADV_ATTR_ORIG_ADDRESS
]);
395 soft_iface
= dev_get_by_index(net
, ifindex
);
396 if (!soft_iface
|| !batadv_softif_is_valid(soft_iface
)) {
401 bat_priv
= netdev_priv(soft_iface
);
402 batadv_tp_stop(bat_priv
, dst
, BATADV_TP_REASON_CANCEL
);
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
418 * Return: error code, or 0 on success
421 batadv_netlink_dump_hardif_entry(struct sk_buff
*msg
, u32 portid
, u32 seq
,
422 struct batadv_hard_iface
*hard_iface
)
424 struct net_device
*net_dev
= hard_iface
->net_dev
;
427 hdr
= genlmsg_put(msg
, portid
, seq
, &batadv_netlink_family
, NLM_F_MULTI
,
428 BATADV_CMD_GET_HARDIFS
);
432 if (nla_put_u32(msg
, BATADV_ATTR_HARD_IFINDEX
,
434 nla_put_string(msg
, BATADV_ATTR_HARD_IFNAME
,
436 nla_put(msg
, BATADV_ATTR_HARD_ADDRESS
, ETH_ALEN
,
438 goto nla_put_failure
;
440 if (hard_iface
->if_status
== BATADV_IF_ACTIVE
) {
441 if (nla_put_flag(msg
, BATADV_ATTR_ACTIVE
))
442 goto nla_put_failure
;
445 genlmsg_end(msg
, hdr
);
449 genlmsg_cancel(msg
, hdr
);
454 * batadv_netlink_dump_hardifs - Dump all hard interface into a messages
455 * @msg: Netlink message to dump into
456 * @cb: Parameters from query
458 * Return: error code, or length of reply message on success
461 batadv_netlink_dump_hardifs(struct sk_buff
*msg
, struct netlink_callback
*cb
)
463 struct net
*net
= sock_net(cb
->skb
->sk
);
464 struct net_device
*soft_iface
;
465 struct batadv_hard_iface
*hard_iface
;
467 int portid
= NETLINK_CB(cb
->skb
).portid
;
468 int seq
= cb
->nlh
->nlmsg_seq
;
469 int skip
= cb
->args
[0];
472 ifindex
= batadv_netlink_get_ifindex(cb
->nlh
,
473 BATADV_ATTR_MESH_IFINDEX
);
477 soft_iface
= dev_get_by_index(net
, ifindex
);
481 if (!batadv_softif_is_valid(soft_iface
)) {
488 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
489 if (hard_iface
->soft_iface
!= soft_iface
)
495 if (batadv_netlink_dump_hardif_entry(msg
, portid
, seq
,
511 static struct genl_ops batadv_netlink_ops
[] = {
513 .cmd
= BATADV_CMD_GET_MESH_INFO
,
514 .flags
= GENL_ADMIN_PERM
,
515 .policy
= batadv_netlink_policy
,
516 .doit
= batadv_netlink_get_mesh_info
,
519 .cmd
= BATADV_CMD_TP_METER
,
520 .flags
= GENL_ADMIN_PERM
,
521 .policy
= batadv_netlink_policy
,
522 .doit
= batadv_netlink_tp_meter_start
,
525 .cmd
= BATADV_CMD_TP_METER_CANCEL
,
526 .flags
= GENL_ADMIN_PERM
,
527 .policy
= batadv_netlink_policy
,
528 .doit
= batadv_netlink_tp_meter_cancel
,
531 .cmd
= BATADV_CMD_GET_ROUTING_ALGOS
,
532 .flags
= GENL_ADMIN_PERM
,
533 .policy
= batadv_netlink_policy
,
534 .dumpit
= batadv_algo_dump
,
537 .cmd
= BATADV_CMD_GET_HARDIFS
,
538 .flags
= GENL_ADMIN_PERM
,
539 .policy
= batadv_netlink_policy
,
540 .dumpit
= batadv_netlink_dump_hardifs
,
543 .cmd
= BATADV_CMD_GET_TRANSTABLE_LOCAL
,
544 .flags
= GENL_ADMIN_PERM
,
545 .policy
= batadv_netlink_policy
,
546 .dumpit
= batadv_tt_local_dump
,
549 .cmd
= BATADV_CMD_GET_TRANSTABLE_GLOBAL
,
550 .flags
= GENL_ADMIN_PERM
,
551 .policy
= batadv_netlink_policy
,
552 .dumpit
= batadv_tt_global_dump
,
557 * batadv_netlink_register - register batadv genl netlink family
559 void __init
batadv_netlink_register(void)
563 ret
= genl_register_family_with_ops_groups(&batadv_netlink_family
,
565 batadv_netlink_mcgrps
);
567 pr_warn("unable to register netlink family");
571 * batadv_netlink_unregister - unregister batadv genl netlink family
573 void batadv_netlink_unregister(void)
575 genl_unregister_family(&batadv_netlink_family
);