1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
3 *
4 * Matthias Schiffer
5 */
6
7 #include "netlink.h"
8 #include "main.h"
9
10 #include <linux/array_size.h>
11 #include <linux/atomic.h>
12 #include <linux/bitops.h>
13 #include <linux/bug.h>
14 #include <linux/byteorder/generic.h>
15 #include <linux/cache.h>
16 #include <linux/err.h>
17 #include <linux/errno.h>
18 #include <linux/gfp.h>
19 #include <linux/if_ether.h>
20 #include <linux/if_vlan.h>
21 #include <linux/init.h>
22 #include <linux/limits.h>
23 #include <linux/list.h>
24 #include <linux/minmax.h>
25 #include <linux/netdevice.h>
26 #include <linux/netlink.h>
27 #include <linux/printk.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/skbuff.h>
30 #include <linux/stddef.h>
31 #include <linux/types.h>
32 #include <net/genetlink.h>
33 #include <net/net_namespace.h>
34 #include <net/netlink.h>
35 #include <net/sock.h>
36 #include <uapi/linux/batadv_packet.h>
37 #include <uapi/linux/batman_adv.h>
38
39 #include "bat_algo.h"
40 #include "bridge_loop_avoidance.h"
41 #include "distributed-arp-table.h"
42 #include "gateway_client.h"
43 #include "gateway_common.h"
44 #include "hard-interface.h"
45 #include "log.h"
46 #include "mesh-interface.h"
47 #include "multicast.h"
48 #include "network-coding.h"
49 #include "originator.h"
50 #include "tp_meter.h"
51 #include "translation-table.h"
52
53 struct genl_family batadv_netlink_family;
54
55 /* multicast groups */
56 enum batadv_netlink_multicast_groups {
57 BATADV_NL_MCGRP_CONFIG,
58 BATADV_NL_MCGRP_TPMETER,
59 };
60
61 /**
62 * enum batadv_genl_ops_flags - flags for genl_ops's internal_flags
63 */
64 enum batadv_genl_ops_flags {
65 /**
66 * @BATADV_FLAG_NEED_MESH: request requires valid mesh interface in
67 * attribute BATADV_ATTR_MESH_IFINDEX and expects a pointer to it to be
68 * saved in info->user_ptr[0]
69 */
70 BATADV_FLAG_NEED_MESH = BIT(0),
71
72 /**
73 * @BATADV_FLAG_NEED_HARDIF: request requires valid hard interface in
74 * attribute BATADV_ATTR_HARD_IFINDEX and expects a pointer to it to be
75 * saved in info->user_ptr[1]
76 */
77 BATADV_FLAG_NEED_HARDIF = BIT(1),
78
79 /**
80 * @BATADV_FLAG_NEED_VLAN: request requires valid vlan in
81 * attribute BATADV_ATTR_VLANID and expects a pointer to it to be
82 * saved in info->user_ptr[1]
83 */
84 BATADV_FLAG_NEED_VLAN = BIT(2),
85 };
86
87 static const struct genl_multicast_group batadv_netlink_mcgrps[] = {
88 [BATADV_NL_MCGRP_CONFIG] = { .name = BATADV_NL_MCAST_GROUP_CONFIG },
89 [BATADV_NL_MCGRP_TPMETER] = { .name = BATADV_NL_MCAST_GROUP_TPMETER },
90 };
91
92 static const struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = {
93 [BATADV_ATTR_VERSION] = { .type = NLA_STRING },
94 [BATADV_ATTR_ALGO_NAME] = { .type = NLA_STRING },
95 [BATADV_ATTR_MESH_IFINDEX] = { .type = NLA_U32 },
96 [BATADV_ATTR_MESH_IFNAME] = { .type = NLA_STRING },
97 [BATADV_ATTR_MESH_ADDRESS] = { .len = ETH_ALEN },
98 [BATADV_ATTR_HARD_IFINDEX] = { .type = NLA_U32 },
99 [BATADV_ATTR_HARD_IFNAME] = { .type = NLA_STRING },
100 [BATADV_ATTR_HARD_ADDRESS] = { .len = ETH_ALEN },
101 [BATADV_ATTR_ORIG_ADDRESS] = { .len = ETH_ALEN },
102 [BATADV_ATTR_TPMETER_RESULT] = { .type = NLA_U8 },
103 [BATADV_ATTR_TPMETER_TEST_TIME] = { .type = NLA_U32 },
104 [BATADV_ATTR_TPMETER_BYTES] = { .type = NLA_U64 },
105 [BATADV_ATTR_TPMETER_COOKIE] = { .type = NLA_U32 },
106 [BATADV_ATTR_ACTIVE] = { .type = NLA_FLAG },
107 [BATADV_ATTR_TT_ADDRESS] = { .len = ETH_ALEN },
108 [BATADV_ATTR_TT_TTVN] = { .type = NLA_U8 },
109 [BATADV_ATTR_TT_LAST_TTVN] = { .type = NLA_U8 },
110 [BATADV_ATTR_TT_CRC32] = { .type = NLA_U32 },
111 [BATADV_ATTR_TT_VID] = { .type = NLA_U16 },
112 [BATADV_ATTR_TT_FLAGS] = { .type = NLA_U32 },
113 [BATADV_ATTR_FLAG_BEST] = { .type = NLA_FLAG },
114 [BATADV_ATTR_LAST_SEEN_MSECS] = { .type = NLA_U32 },
115 [BATADV_ATTR_NEIGH_ADDRESS] = { .len = ETH_ALEN },
116 [BATADV_ATTR_TQ] = { .type = NLA_U8 },
117 [BATADV_ATTR_THROUGHPUT] = { .type = NLA_U32 },
118 [BATADV_ATTR_BANDWIDTH_UP] = { .type = NLA_U32 },
119 [BATADV_ATTR_BANDWIDTH_DOWN] = { .type = NLA_U32 },
120 [BATADV_ATTR_ROUTER] = { .len = ETH_ALEN },
121 [BATADV_ATTR_BLA_OWN] = { .type = NLA_FLAG },
122 [BATADV_ATTR_BLA_ADDRESS] = { .len = ETH_ALEN },
123 [BATADV_ATTR_BLA_VID] = { .type = NLA_U16 },
124 [BATADV_ATTR_BLA_BACKBONE] = { .len = ETH_ALEN },
125 [BATADV_ATTR_BLA_CRC] = { .type = NLA_U16 },
126 [BATADV_ATTR_DAT_CACHE_IP4ADDRESS] = { .type = NLA_U32 },
127 [BATADV_ATTR_DAT_CACHE_HWADDRESS] = { .len = ETH_ALEN },
128 [BATADV_ATTR_DAT_CACHE_VID] = { .type = NLA_U16 },
129 [BATADV_ATTR_MCAST_FLAGS] = { .type = NLA_U32 },
130 [BATADV_ATTR_MCAST_FLAGS_PRIV] = { .type = NLA_U32 },
131 [BATADV_ATTR_VLANID] = { .type = NLA_U16 },
132 [BATADV_ATTR_AGGREGATED_OGMS_ENABLED] = { .type = NLA_U8 },
133 [BATADV_ATTR_AP_ISOLATION_ENABLED] = { .type = NLA_U8 },
134 [BATADV_ATTR_ISOLATION_MARK] = { .type = NLA_U32 },
135 [BATADV_ATTR_ISOLATION_MASK] = { .type = NLA_U32 },
136 [BATADV_ATTR_BONDING_ENABLED] = { .type = NLA_U8 },
137 [BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED] = { .type = NLA_U8 },
138 [BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED] = { .type = NLA_U8 },
139 [BATADV_ATTR_FRAGMENTATION_ENABLED] = { .type = NLA_U8 },
140 [BATADV_ATTR_GW_BANDWIDTH_DOWN] = { .type = NLA_U32 },
141 [BATADV_ATTR_GW_BANDWIDTH_UP] = { .type = NLA_U32 },
142 [BATADV_ATTR_GW_MODE] = { .type = NLA_U8 },
143 [BATADV_ATTR_GW_SEL_CLASS] = { .type = NLA_U32 },
144 [BATADV_ATTR_HOP_PENALTY] = { .type = NLA_U8 },
145 [BATADV_ATTR_LOG_LEVEL] = { .type = NLA_U32 },
146 [BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED] = { .type = NLA_U8 },
147 [BATADV_ATTR_MULTICAST_FANOUT] = { .type = NLA_U32 },
148 [BATADV_ATTR_NETWORK_CODING_ENABLED] = { .type = NLA_U8 },
149 [BATADV_ATTR_ORIG_INTERVAL] = { .type = NLA_U32 },
150 [BATADV_ATTR_ELP_INTERVAL] = { .type = NLA_U32 },
151 [BATADV_ATTR_THROUGHPUT_OVERRIDE] = { .type = NLA_U32 },
152 };
153
154 /**
155 * batadv_netlink_get_ifindex() - Extract an interface index from a message
156 * @nlh: Message header
157 * @attrtype: Attribute which holds an interface index
158 *
159 * Return: interface index, or 0.
160 */
batadv_netlink_get_ifindex(const struct nlmsghdr * nlh,int attrtype)161 static int batadv_netlink_get_ifindex(const struct nlmsghdr *nlh, int attrtype)
162 {
163 struct nlattr *attr = nlmsg_find_attr(nlh, GENL_HDRLEN, attrtype);
164
165 return (attr && nla_len(attr) == sizeof(u32)) ? nla_get_u32(attr) : 0;
166 }
167
168 /**
169 * batadv_netlink_mesh_fill_ap_isolation() - Add ap_isolation meshif attribute
170 * @msg: Netlink message to dump into
171 * @bat_priv: the bat priv with all the mesh interface information
172 *
173 * Return: 0 on success or negative error number in case of failure
174 */
batadv_netlink_mesh_fill_ap_isolation(struct sk_buff * msg,struct batadv_priv * bat_priv)175 static int batadv_netlink_mesh_fill_ap_isolation(struct sk_buff *msg,
176 struct batadv_priv *bat_priv)
177 {
178 struct batadv_meshif_vlan *vlan;
179 u8 ap_isolation;
180
181 vlan = batadv_meshif_vlan_get(bat_priv, BATADV_NO_FLAGS);
182 if (!vlan)
183 return 0;
184
185 ap_isolation = atomic_read(&vlan->ap_isolation);
186 batadv_meshif_vlan_put(vlan);
187
188 return nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
189 !!ap_isolation);
190 }
191
192 /**
193 * batadv_netlink_set_mesh_ap_isolation() - Set ap_isolation from genl msg
194 * @attr: parsed BATADV_ATTR_AP_ISOLATION_ENABLED attribute
195 * @bat_priv: the bat priv with all the mesh interface information
196 *
197 * Return: 0 on success or negative error number in case of failure
198 */
batadv_netlink_set_mesh_ap_isolation(struct nlattr * attr,struct batadv_priv * bat_priv)199 static int batadv_netlink_set_mesh_ap_isolation(struct nlattr *attr,
200 struct batadv_priv *bat_priv)
201 {
202 struct batadv_meshif_vlan *vlan;
203
204 vlan = batadv_meshif_vlan_get(bat_priv, BATADV_NO_FLAGS);
205 if (!vlan)
206 return -ENOENT;
207
208 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
209 batadv_meshif_vlan_put(vlan);
210
211 return 0;
212 }
213
214 /**
215 * batadv_netlink_mesh_fill() - Fill message with mesh attributes
216 * @msg: Netlink message to dump into
217 * @bat_priv: the bat priv with all the mesh interface information
218 * @cmd: type of message to generate
219 * @portid: Port making netlink request
220 * @seq: sequence number for message
221 * @flags: Additional flags for message
222 *
223 * Return: 0 on success or negative error number in case of failure
224 */
batadv_netlink_mesh_fill(struct sk_buff * msg,struct batadv_priv * bat_priv,enum batadv_nl_commands cmd,u32 portid,u32 seq,int flags)225 static int batadv_netlink_mesh_fill(struct sk_buff *msg,
226 struct batadv_priv *bat_priv,
227 enum batadv_nl_commands cmd,
228 u32 portid, u32 seq, int flags)
229 {
230 struct net_device *mesh_iface = bat_priv->mesh_iface;
231 struct batadv_hard_iface *primary_if = NULL;
232 struct net_device *hard_iface;
233 void *hdr;
234
235 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
236 if (!hdr)
237 return -ENOBUFS;
238
239 if (nla_put_string(msg, BATADV_ATTR_VERSION, BATADV_SOURCE_VERSION) ||
240 nla_put_string(msg, BATADV_ATTR_ALGO_NAME,
241 bat_priv->algo_ops->name) ||
242 nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, mesh_iface->ifindex) ||
243 nla_put_string(msg, BATADV_ATTR_MESH_IFNAME, mesh_iface->name) ||
244 nla_put(msg, BATADV_ATTR_MESH_ADDRESS, ETH_ALEN,
245 mesh_iface->dev_addr) ||
246 nla_put_u8(msg, BATADV_ATTR_TT_TTVN,
247 (u8)atomic_read(&bat_priv->tt.vn)))
248 goto nla_put_failure;
249
250 #ifdef CONFIG_BATMAN_ADV_BLA
251 if (nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
252 ntohs(bat_priv->bla.claim_dest.group)))
253 goto nla_put_failure;
254 #endif
255
256 if (batadv_mcast_mesh_info_put(msg, bat_priv))
257 goto nla_put_failure;
258
259 primary_if = batadv_primary_if_get_selected(bat_priv);
260 if (primary_if && primary_if->if_status == BATADV_IF_ACTIVE) {
261 hard_iface = primary_if->net_dev;
262
263 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
264 hard_iface->ifindex) ||
265 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
266 hard_iface->name) ||
267 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
268 hard_iface->dev_addr))
269 goto nla_put_failure;
270 }
271
272 if (nla_put_u8(msg, BATADV_ATTR_AGGREGATED_OGMS_ENABLED,
273 !!atomic_read(&bat_priv->aggregated_ogms)))
274 goto nla_put_failure;
275
276 if (batadv_netlink_mesh_fill_ap_isolation(msg, bat_priv))
277 goto nla_put_failure;
278
279 if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MARK,
280 bat_priv->isolation_mark))
281 goto nla_put_failure;
282
283 if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MASK,
284 bat_priv->isolation_mark_mask))
285 goto nla_put_failure;
286
287 if (nla_put_u8(msg, BATADV_ATTR_BONDING_ENABLED,
288 !!atomic_read(&bat_priv->bonding)))
289 goto nla_put_failure;
290
291 #ifdef CONFIG_BATMAN_ADV_BLA
292 if (nla_put_u8(msg, BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED,
293 !!atomic_read(&bat_priv->bridge_loop_avoidance)))
294 goto nla_put_failure;
295 #endif /* CONFIG_BATMAN_ADV_BLA */
296
297 #ifdef CONFIG_BATMAN_ADV_DAT
298 if (nla_put_u8(msg, BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED,
299 !!atomic_read(&bat_priv->distributed_arp_table)))
300 goto nla_put_failure;
301 #endif /* CONFIG_BATMAN_ADV_DAT */
302
303 if (nla_put_u8(msg, BATADV_ATTR_FRAGMENTATION_ENABLED,
304 !!atomic_read(&bat_priv->fragmentation)))
305 goto nla_put_failure;
306
307 if (nla_put_u32(msg, BATADV_ATTR_GW_BANDWIDTH_DOWN,
308 atomic_read(&bat_priv->gw.bandwidth_down)))
309 goto nla_put_failure;
310
311 if (nla_put_u32(msg, BATADV_ATTR_GW_BANDWIDTH_UP,
312 atomic_read(&bat_priv->gw.bandwidth_up)))
313 goto nla_put_failure;
314
315 if (nla_put_u8(msg, BATADV_ATTR_GW_MODE,
316 atomic_read(&bat_priv->gw.mode)))
317 goto nla_put_failure;
318
319 if (bat_priv->algo_ops->gw.get_best_gw_node &&
320 bat_priv->algo_ops->gw.is_eligible) {
321 /* GW selection class is not available if the routing algorithm
322 * in use does not implement the GW API
323 */
324 if (nla_put_u32(msg, BATADV_ATTR_GW_SEL_CLASS,
325 atomic_read(&bat_priv->gw.sel_class)))
326 goto nla_put_failure;
327 }
328
329 if (nla_put_u8(msg, BATADV_ATTR_HOP_PENALTY,
330 atomic_read(&bat_priv->hop_penalty)))
331 goto nla_put_failure;
332
333 #ifdef CONFIG_BATMAN_ADV_DEBUG
334 if (nla_put_u32(msg, BATADV_ATTR_LOG_LEVEL,
335 atomic_read(&bat_priv->log_level)))
336 goto nla_put_failure;
337 #endif /* CONFIG_BATMAN_ADV_DEBUG */
338
339 #ifdef CONFIG_BATMAN_ADV_MCAST
340 if (nla_put_u8(msg, BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED,
341 !atomic_read(&bat_priv->multicast_mode)))
342 goto nla_put_failure;
343
344 if (nla_put_u32(msg, BATADV_ATTR_MULTICAST_FANOUT,
345 atomic_read(&bat_priv->multicast_fanout)))
346 goto nla_put_failure;
347 #endif /* CONFIG_BATMAN_ADV_MCAST */
348
349 #ifdef CONFIG_BATMAN_ADV_NC
350 if (nla_put_u8(msg, BATADV_ATTR_NETWORK_CODING_ENABLED,
351 !!atomic_read(&bat_priv->network_coding)))
352 goto nla_put_failure;
353 #endif /* CONFIG_BATMAN_ADV_NC */
354
355 if (nla_put_u32(msg, BATADV_ATTR_ORIG_INTERVAL,
356 atomic_read(&bat_priv->orig_interval)))
357 goto nla_put_failure;
358
359 batadv_hardif_put(primary_if);
360
361 genlmsg_end(msg, hdr);
362 return 0;
363
364 nla_put_failure:
365 batadv_hardif_put(primary_if);
366
367 genlmsg_cancel(msg, hdr);
368 return -EMSGSIZE;
369 }
370
371 /**
372 * batadv_netlink_notify_mesh() - send meshif attributes to listener
373 * @bat_priv: the bat priv with all the mesh interface information
374 *
375 * Return: 0 on success, < 0 on error
376 */
batadv_netlink_notify_mesh(struct batadv_priv * bat_priv)377 static int batadv_netlink_notify_mesh(struct batadv_priv *bat_priv)
378 {
379 struct sk_buff *msg;
380 int ret;
381
382 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
383 if (!msg)
384 return -ENOMEM;
385
386 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_SET_MESH,
387 0, 0, 0);
388 if (ret < 0) {
389 nlmsg_free(msg);
390 return ret;
391 }
392
393 genlmsg_multicast_netns(&batadv_netlink_family,
394 dev_net(bat_priv->mesh_iface), msg, 0,
395 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
396
397 return 0;
398 }
399
400 /**
401 * batadv_netlink_get_mesh() - Get meshif attributes
402 * @skb: Netlink message with request data
403 * @info: receiver information
404 *
405 * Return: 0 on success or negative error number in case of failure
406 */
batadv_netlink_get_mesh(struct sk_buff * skb,struct genl_info * info)407 static int batadv_netlink_get_mesh(struct sk_buff *skb, struct genl_info *info)
408 {
409 struct batadv_priv *bat_priv = info->user_ptr[0];
410 struct sk_buff *msg;
411 int ret;
412
413 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
414 if (!msg)
415 return -ENOMEM;
416
417 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_GET_MESH,
418 info->snd_portid, info->snd_seq, 0);
419 if (ret < 0) {
420 nlmsg_free(msg);
421 return ret;
422 }
423
424 ret = genlmsg_reply(msg, info);
425
426 return ret;
427 }
428
429 /**
430 * batadv_netlink_set_mesh() - Set meshif attributes
431 * @skb: Netlink message with request data
432 * @info: receiver information
433 *
434 * Return: 0 on success or negative error number in case of failure
435 */
batadv_netlink_set_mesh(struct sk_buff * skb,struct genl_info * info)436 static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info)
437 {
438 struct batadv_priv *bat_priv = info->user_ptr[0];
439 struct nlattr *attr;
440
441 if (info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED]) {
442 attr = info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED];
443
444 atomic_set(&bat_priv->aggregated_ogms, !!nla_get_u8(attr));
445 }
446
447 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
448 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
449
450 batadv_netlink_set_mesh_ap_isolation(attr, bat_priv);
451 }
452
453 if (info->attrs[BATADV_ATTR_ISOLATION_MARK]) {
454 attr = info->attrs[BATADV_ATTR_ISOLATION_MARK];
455
456 bat_priv->isolation_mark = nla_get_u32(attr);
457 }
458
459 if (info->attrs[BATADV_ATTR_ISOLATION_MASK]) {
460 attr = info->attrs[BATADV_ATTR_ISOLATION_MASK];
461
462 bat_priv->isolation_mark_mask = nla_get_u32(attr);
463 }
464
465 if (info->attrs[BATADV_ATTR_BONDING_ENABLED]) {
466 attr = info->attrs[BATADV_ATTR_BONDING_ENABLED];
467
468 atomic_set(&bat_priv->bonding, !!nla_get_u8(attr));
469 }
470
471 #ifdef CONFIG_BATMAN_ADV_BLA
472 if (info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED]) {
473 attr = info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED];
474
475 atomic_set(&bat_priv->bridge_loop_avoidance,
476 !!nla_get_u8(attr));
477 batadv_bla_status_update(bat_priv->mesh_iface);
478 }
479 #endif /* CONFIG_BATMAN_ADV_BLA */
480
481 #ifdef CONFIG_BATMAN_ADV_DAT
482 if (info->attrs[BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED]) {
483 attr = info->attrs[BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED];
484
485 atomic_set(&bat_priv->distributed_arp_table,
486 !!nla_get_u8(attr));
487 batadv_dat_status_update(bat_priv->mesh_iface);
488 }
489 #endif /* CONFIG_BATMAN_ADV_DAT */
490
491 if (info->attrs[BATADV_ATTR_FRAGMENTATION_ENABLED]) {
492 attr = info->attrs[BATADV_ATTR_FRAGMENTATION_ENABLED];
493
494 atomic_set(&bat_priv->fragmentation, !!nla_get_u8(attr));
495
496 rtnl_lock();
497 batadv_update_min_mtu(bat_priv->mesh_iface);
498 rtnl_unlock();
499 }
500
501 if (info->attrs[BATADV_ATTR_GW_BANDWIDTH_DOWN]) {
502 attr = info->attrs[BATADV_ATTR_GW_BANDWIDTH_DOWN];
503
504 atomic_set(&bat_priv->gw.bandwidth_down, nla_get_u32(attr));
505 batadv_gw_tvlv_container_update(bat_priv);
506 }
507
508 if (info->attrs[BATADV_ATTR_GW_BANDWIDTH_UP]) {
509 attr = info->attrs[BATADV_ATTR_GW_BANDWIDTH_UP];
510
511 atomic_set(&bat_priv->gw.bandwidth_up, nla_get_u32(attr));
512 batadv_gw_tvlv_container_update(bat_priv);
513 }
514
515 if (info->attrs[BATADV_ATTR_GW_MODE]) {
516 u8 gw_mode;
517
518 attr = info->attrs[BATADV_ATTR_GW_MODE];
519 gw_mode = nla_get_u8(attr);
520
521 if (gw_mode <= BATADV_GW_MODE_SERVER) {
522 /* Invoking batadv_gw_reselect() is not enough to really
523 * de-select the current GW. It will only instruct the
524 * gateway client code to perform a re-election the next
525 * time that this is needed.
526 *
527 * When gw client mode is being switched off the current
528 * GW must be de-selected explicitly otherwise no GW_ADD
529 * uevent is thrown on client mode re-activation. This
530 * is operation is performed in
531 * batadv_gw_check_client_stop().
532 */
533 batadv_gw_reselect(bat_priv);
534
535 /* always call batadv_gw_check_client_stop() before
536 * changing the gateway state
537 */
538 batadv_gw_check_client_stop(bat_priv);
539 atomic_set(&bat_priv->gw.mode, gw_mode);
540 batadv_gw_tvlv_container_update(bat_priv);
541 }
542 }
543
544 if (info->attrs[BATADV_ATTR_GW_SEL_CLASS] &&
545 bat_priv->algo_ops->gw.get_best_gw_node &&
546 bat_priv->algo_ops->gw.is_eligible) {
547 /* setting the GW selection class is allowed only if the routing
548 * algorithm in use implements the GW API
549 */
550
551 u32 sel_class_max = bat_priv->algo_ops->gw.sel_class_max;
552 u32 sel_class;
553
554 attr = info->attrs[BATADV_ATTR_GW_SEL_CLASS];
555 sel_class = nla_get_u32(attr);
556
557 if (sel_class >= 1 && sel_class <= sel_class_max) {
558 atomic_set(&bat_priv->gw.sel_class, sel_class);
559 batadv_gw_reselect(bat_priv);
560 }
561 }
562
563 if (info->attrs[BATADV_ATTR_HOP_PENALTY]) {
564 attr = info->attrs[BATADV_ATTR_HOP_PENALTY];
565
566 atomic_set(&bat_priv->hop_penalty, nla_get_u8(attr));
567 }
568
569 #ifdef CONFIG_BATMAN_ADV_DEBUG
570 if (info->attrs[BATADV_ATTR_LOG_LEVEL]) {
571 attr = info->attrs[BATADV_ATTR_LOG_LEVEL];
572
573 atomic_set(&bat_priv->log_level,
574 nla_get_u32(attr) & BATADV_DBG_ALL);
575 }
576 #endif /* CONFIG_BATMAN_ADV_DEBUG */
577
578 #ifdef CONFIG_BATMAN_ADV_MCAST
579 if (info->attrs[BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED]) {
580 attr = info->attrs[BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED];
581
582 atomic_set(&bat_priv->multicast_mode, !nla_get_u8(attr));
583 }
584
585 if (info->attrs[BATADV_ATTR_MULTICAST_FANOUT]) {
586 attr = info->attrs[BATADV_ATTR_MULTICAST_FANOUT];
587
588 atomic_set(&bat_priv->multicast_fanout, nla_get_u32(attr));
589 }
590 #endif /* CONFIG_BATMAN_ADV_MCAST */
591
592 #ifdef CONFIG_BATMAN_ADV_NC
593 if (info->attrs[BATADV_ATTR_NETWORK_CODING_ENABLED]) {
594 attr = info->attrs[BATADV_ATTR_NETWORK_CODING_ENABLED];
595
596 atomic_set(&bat_priv->network_coding, !!nla_get_u8(attr));
597 batadv_nc_status_update(bat_priv->mesh_iface);
598 }
599 #endif /* CONFIG_BATMAN_ADV_NC */
600
601 if (info->attrs[BATADV_ATTR_ORIG_INTERVAL]) {
602 u32 orig_interval;
603
604 attr = info->attrs[BATADV_ATTR_ORIG_INTERVAL];
605 orig_interval = nla_get_u32(attr);
606
607 orig_interval = min_t(u32, orig_interval, INT_MAX);
608 orig_interval = max_t(u32, orig_interval, 2 * BATADV_JITTER);
609
610 atomic_set(&bat_priv->orig_interval, orig_interval);
611 }
612
613 batadv_netlink_notify_mesh(bat_priv);
614
615 return 0;
616 }
617
618 /**
619 * batadv_netlink_tp_meter_put() - Fill information of started tp_meter session
620 * @msg: netlink message to be sent back
621 * @cookie: tp meter session cookie
622 *
623 * Return: 0 on success, < 0 on error
624 */
625 static int
batadv_netlink_tp_meter_put(struct sk_buff * msg,u32 cookie)626 batadv_netlink_tp_meter_put(struct sk_buff *msg, u32 cookie)
627 {
628 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
629 return -ENOBUFS;
630
631 return 0;
632 }
633
634 /**
635 * batadv_netlink_tpmeter_notify() - send tp_meter result via netlink to client
636 * @bat_priv: the bat priv with all the mesh interface information
637 * @dst: destination of tp_meter session
638 * @result: reason for tp meter session stop
639 * @test_time: total time of the tp_meter session
640 * @total_bytes: bytes acked to the receiver
641 * @cookie: cookie of tp_meter session
642 *
643 * Return: 0 on success, < 0 on error
644 */
batadv_netlink_tpmeter_notify(struct batadv_priv * bat_priv,const u8 * dst,u8 result,u32 test_time,u64 total_bytes,u32 cookie)645 int batadv_netlink_tpmeter_notify(struct batadv_priv *bat_priv, const u8 *dst,
646 u8 result, u32 test_time, u64 total_bytes,
647 u32 cookie)
648 {
649 struct sk_buff *msg;
650 void *hdr;
651 int ret;
652
653 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
654 if (!msg)
655 return -ENOMEM;
656
657 hdr = genlmsg_put(msg, 0, 0, &batadv_netlink_family, 0,
658 BATADV_CMD_TP_METER);
659 if (!hdr) {
660 ret = -ENOBUFS;
661 goto err_genlmsg;
662 }
663
664 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
665 goto nla_put_failure;
666
667 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, test_time))
668 goto nla_put_failure;
669
670 if (nla_put_u64_64bit(msg, BATADV_ATTR_TPMETER_BYTES, total_bytes,
671 BATADV_ATTR_PAD))
672 goto nla_put_failure;
673
674 if (nla_put_u8(msg, BATADV_ATTR_TPMETER_RESULT, result))
675 goto nla_put_failure;
676
677 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst))
678 goto nla_put_failure;
679
680 genlmsg_end(msg, hdr);
681
682 genlmsg_multicast_netns(&batadv_netlink_family,
683 dev_net(bat_priv->mesh_iface), msg, 0,
684 BATADV_NL_MCGRP_TPMETER, GFP_KERNEL);
685
686 return 0;
687
688 nla_put_failure:
689 genlmsg_cancel(msg, hdr);
690 ret = -EMSGSIZE;
691
692 err_genlmsg:
693 nlmsg_free(msg);
694 return ret;
695 }
696
697 /**
698 * batadv_netlink_tp_meter_start() - Start a new tp_meter session
699 * @skb: received netlink message
700 * @info: receiver information
701 *
702 * Return: 0 on success, < 0 on error
703 */
704 static int
batadv_netlink_tp_meter_start(struct sk_buff * skb,struct genl_info * info)705 batadv_netlink_tp_meter_start(struct sk_buff *skb, struct genl_info *info)
706 {
707 struct batadv_priv *bat_priv = info->user_ptr[0];
708 struct sk_buff *msg = NULL;
709 u32 test_length;
710 void *msg_head;
711 u32 cookie;
712 u8 *dst;
713 int ret;
714
715 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
716 return -EINVAL;
717
718 if (!info->attrs[BATADV_ATTR_TPMETER_TEST_TIME])
719 return -EINVAL;
720
721 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
722
723 test_length = nla_get_u32(info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]);
724
725 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
726 if (!msg) {
727 ret = -ENOMEM;
728 goto out;
729 }
730
731 msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq,
732 &batadv_netlink_family, 0,
733 BATADV_CMD_TP_METER);
734 if (!msg_head) {
735 ret = -ENOBUFS;
736 goto out;
737 }
738
739 batadv_tp_start(bat_priv, dst, test_length, &cookie);
740
741 ret = batadv_netlink_tp_meter_put(msg, cookie);
742
743 out:
744 if (ret) {
745 if (msg)
746 nlmsg_free(msg);
747 return ret;
748 }
749
750 genlmsg_end(msg, msg_head);
751 return genlmsg_reply(msg, info);
752 }
753
754 /**
755 * batadv_netlink_tp_meter_cancel() - Cancel a running tp_meter session
756 * @skb: received netlink message
757 * @info: receiver information
758 *
759 * Return: 0 on success, < 0 on error
760 */
761 static int
batadv_netlink_tp_meter_cancel(struct sk_buff * skb,struct genl_info * info)762 batadv_netlink_tp_meter_cancel(struct sk_buff *skb, struct genl_info *info)
763 {
764 struct batadv_priv *bat_priv = info->user_ptr[0];
765 u8 *dst;
766 int ret = 0;
767
768 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
769 return -EINVAL;
770
771 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
772
773 batadv_tp_stop(bat_priv, dst, BATADV_TP_REASON_CANCEL);
774
775 return ret;
776 }
777
778 /**
779 * batadv_netlink_hardif_fill() - Fill message with hardif attributes
780 * @msg: Netlink message to dump into
781 * @bat_priv: the bat priv with all the mesh interface information
782 * @hard_iface: hard interface which was modified
783 * @cmd: type of message to generate
784 * @portid: Port making netlink request
785 * @seq: sequence number for message
786 * @flags: Additional flags for message
787 * @cb: Control block containing additional options
788 *
789 * Return: 0 on success or negative error number in case of failure
790 */
batadv_netlink_hardif_fill(struct sk_buff * msg,struct batadv_priv * bat_priv,struct batadv_hard_iface * hard_iface,enum batadv_nl_commands cmd,u32 portid,u32 seq,int flags,struct netlink_callback * cb)791 static int batadv_netlink_hardif_fill(struct sk_buff *msg,
792 struct batadv_priv *bat_priv,
793 struct batadv_hard_iface *hard_iface,
794 enum batadv_nl_commands cmd,
795 u32 portid, u32 seq, int flags,
796 struct netlink_callback *cb)
797 {
798 struct net_device *net_dev = hard_iface->net_dev;
799 void *hdr;
800
801 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
802 if (!hdr)
803 return -ENOBUFS;
804
805 if (cb)
806 genl_dump_check_consistent(cb, hdr);
807
808 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
809 bat_priv->mesh_iface->ifindex))
810 goto nla_put_failure;
811
812 if (nla_put_string(msg, BATADV_ATTR_MESH_IFNAME,
813 bat_priv->mesh_iface->name))
814 goto nla_put_failure;
815
816 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
817 net_dev->ifindex) ||
818 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
819 net_dev->name) ||
820 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
821 net_dev->dev_addr))
822 goto nla_put_failure;
823
824 if (hard_iface->if_status == BATADV_IF_ACTIVE) {
825 if (nla_put_flag(msg, BATADV_ATTR_ACTIVE))
826 goto nla_put_failure;
827 }
828
829 if (nla_put_u8(msg, BATADV_ATTR_HOP_PENALTY,
830 atomic_read(&hard_iface->hop_penalty)))
831 goto nla_put_failure;
832
833 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
834 if (nla_put_u32(msg, BATADV_ATTR_ELP_INTERVAL,
835 atomic_read(&hard_iface->bat_v.elp_interval)))
836 goto nla_put_failure;
837
838 if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT_OVERRIDE,
839 atomic_read(&hard_iface->bat_v.throughput_override)))
840 goto nla_put_failure;
841 #endif /* CONFIG_BATMAN_ADV_BATMAN_V */
842
843 genlmsg_end(msg, hdr);
844 return 0;
845
846 nla_put_failure:
847 genlmsg_cancel(msg, hdr);
848 return -EMSGSIZE;
849 }
850
851 /**
852 * batadv_netlink_notify_hardif() - send hardif attributes to listener
853 * @bat_priv: the bat priv with all the mesh interface information
854 * @hard_iface: hard interface which was modified
855 *
856 * Return: 0 on success, < 0 on error
857 */
batadv_netlink_notify_hardif(struct batadv_priv * bat_priv,struct batadv_hard_iface * hard_iface)858 static int batadv_netlink_notify_hardif(struct batadv_priv *bat_priv,
859 struct batadv_hard_iface *hard_iface)
860 {
861 struct sk_buff *msg;
862 int ret;
863
864 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
865 if (!msg)
866 return -ENOMEM;
867
868 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
869 BATADV_CMD_SET_HARDIF, 0, 0, 0, NULL);
870 if (ret < 0) {
871 nlmsg_free(msg);
872 return ret;
873 }
874
875 genlmsg_multicast_netns(&batadv_netlink_family,
876 dev_net(bat_priv->mesh_iface), msg, 0,
877 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
878
879 return 0;
880 }
881
882 /**
883 * batadv_netlink_cmd_get_hardif() - Get hardif attributes
884 * @skb: Netlink message with request data
885 * @info: receiver information
886 *
887 * Return: 0 on success or negative error number in case of failure
888 */
batadv_netlink_cmd_get_hardif(struct sk_buff * skb,struct genl_info * info)889 static int batadv_netlink_cmd_get_hardif(struct sk_buff *skb,
890 struct genl_info *info)
891 {
892 struct batadv_hard_iface *hard_iface = info->user_ptr[1];
893 struct batadv_priv *bat_priv = info->user_ptr[0];
894 struct sk_buff *msg;
895 int ret;
896
897 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
898 if (!msg)
899 return -ENOMEM;
900
901 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
902 BATADV_CMD_GET_HARDIF,
903 info->snd_portid, info->snd_seq, 0,
904 NULL);
905 if (ret < 0) {
906 nlmsg_free(msg);
907 return ret;
908 }
909
910 ret = genlmsg_reply(msg, info);
911
912 return ret;
913 }
914
915 /**
916 * batadv_netlink_set_hardif() - Set hardif attributes
917 * @skb: Netlink message with request data
918 * @info: receiver information
919 *
920 * Return: 0 on success or negative error number in case of failure
921 */
batadv_netlink_set_hardif(struct sk_buff * skb,struct genl_info * info)922 static int batadv_netlink_set_hardif(struct sk_buff *skb,
923 struct genl_info *info)
924 {
925 struct batadv_hard_iface *hard_iface = info->user_ptr[1];
926 struct batadv_priv *bat_priv = info->user_ptr[0];
927 struct nlattr *attr;
928
929 if (info->attrs[BATADV_ATTR_HOP_PENALTY]) {
930 attr = info->attrs[BATADV_ATTR_HOP_PENALTY];
931
932 atomic_set(&hard_iface->hop_penalty, nla_get_u8(attr));
933 }
934
935 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
936
937 if (info->attrs[BATADV_ATTR_ELP_INTERVAL]) {
938 attr = info->attrs[BATADV_ATTR_ELP_INTERVAL];
939
940 atomic_set(&hard_iface->bat_v.elp_interval, nla_get_u32(attr));
941 }
942
943 if (info->attrs[BATADV_ATTR_THROUGHPUT_OVERRIDE]) {
944 attr = info->attrs[BATADV_ATTR_THROUGHPUT_OVERRIDE];
945
946 atomic_set(&hard_iface->bat_v.throughput_override,
947 nla_get_u32(attr));
948 }
949 #endif /* CONFIG_BATMAN_ADV_BATMAN_V */
950
951 batadv_netlink_notify_hardif(bat_priv, hard_iface);
952
953 return 0;
954 }
955
956 /**
957 * batadv_netlink_dump_hardif() - Dump all hard interface into a messages
958 * @msg: Netlink message to dump into
959 * @cb: Parameters from query
960 *
961 * Return: error code, or length of reply message on success
962 */
963 static int
batadv_netlink_dump_hardif(struct sk_buff * msg,struct netlink_callback * cb)964 batadv_netlink_dump_hardif(struct sk_buff *msg, struct netlink_callback *cb)
965 {
966 struct net_device *mesh_iface;
967 struct batadv_hard_iface *hard_iface;
968 struct batadv_priv *bat_priv;
969 int portid = NETLINK_CB(cb->skb).portid;
970 int skip = cb->args[0];
971 int i = 0;
972
973 mesh_iface = batadv_netlink_get_meshif(cb);
974 if (IS_ERR(mesh_iface))
975 return PTR_ERR(mesh_iface);
976
977 bat_priv = netdev_priv(mesh_iface);
978
979 rtnl_lock();
980 cb->seq = batadv_hardif_generation << 1 | 1;
981
982 list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
983 if (hard_iface->mesh_iface != mesh_iface)
984 continue;
985
986 if (i++ < skip)
987 continue;
988
989 if (batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
990 BATADV_CMD_GET_HARDIF,
991 portid, cb->nlh->nlmsg_seq,
992 NLM_F_MULTI, cb)) {
993 i--;
994 break;
995 }
996 }
997
998 rtnl_unlock();
999
1000 dev_put(mesh_iface);
1001
1002 cb->args[0] = i;
1003
1004 return msg->len;
1005 }
1006
1007 /**
1008 * batadv_netlink_vlan_fill() - Fill message with vlan attributes
1009 * @msg: Netlink message to dump into
1010 * @bat_priv: the bat priv with all the mesh interface information
1011 * @vlan: vlan which was modified
1012 * @cmd: type of message to generate
1013 * @portid: Port making netlink request
1014 * @seq: sequence number for message
1015 * @flags: Additional flags for message
1016 *
1017 * Return: 0 on success or negative error number in case of failure
1018 */
batadv_netlink_vlan_fill(struct sk_buff * msg,struct batadv_priv * bat_priv,struct batadv_meshif_vlan * vlan,enum batadv_nl_commands cmd,u32 portid,u32 seq,int flags)1019 static int batadv_netlink_vlan_fill(struct sk_buff *msg,
1020 struct batadv_priv *bat_priv,
1021 struct batadv_meshif_vlan *vlan,
1022 enum batadv_nl_commands cmd,
1023 u32 portid, u32 seq, int flags)
1024 {
1025 void *hdr;
1026
1027 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
1028 if (!hdr)
1029 return -ENOBUFS;
1030
1031 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
1032 bat_priv->mesh_iface->ifindex))
1033 goto nla_put_failure;
1034
1035 if (nla_put_string(msg, BATADV_ATTR_MESH_IFNAME,
1036 bat_priv->mesh_iface->name))
1037 goto nla_put_failure;
1038
1039 if (nla_put_u32(msg, BATADV_ATTR_VLANID, vlan->vid & VLAN_VID_MASK))
1040 goto nla_put_failure;
1041
1042 if (nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
1043 !!atomic_read(&vlan->ap_isolation)))
1044 goto nla_put_failure;
1045
1046 genlmsg_end(msg, hdr);
1047 return 0;
1048
1049 nla_put_failure:
1050 genlmsg_cancel(msg, hdr);
1051 return -EMSGSIZE;
1052 }
1053
1054 /**
1055 * batadv_netlink_notify_vlan() - send vlan attributes to listener
1056 * @bat_priv: the bat priv with all the mesh interface information
1057 * @vlan: vlan which was modified
1058 *
1059 * Return: 0 on success, < 0 on error
1060 */
batadv_netlink_notify_vlan(struct batadv_priv * bat_priv,struct batadv_meshif_vlan * vlan)1061 static int batadv_netlink_notify_vlan(struct batadv_priv *bat_priv,
1062 struct batadv_meshif_vlan *vlan)
1063 {
1064 struct sk_buff *msg;
1065 int ret;
1066
1067 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1068 if (!msg)
1069 return -ENOMEM;
1070
1071 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan,
1072 BATADV_CMD_SET_VLAN, 0, 0, 0);
1073 if (ret < 0) {
1074 nlmsg_free(msg);
1075 return ret;
1076 }
1077
1078 genlmsg_multicast_netns(&batadv_netlink_family,
1079 dev_net(bat_priv->mesh_iface), msg, 0,
1080 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
1081
1082 return 0;
1083 }
1084
1085 /**
1086 * batadv_netlink_get_vlan() - Get vlan attributes
1087 * @skb: Netlink message with request data
1088 * @info: receiver information
1089 *
1090 * Return: 0 on success or negative error number in case of failure
1091 */
batadv_netlink_get_vlan(struct sk_buff * skb,struct genl_info * info)1092 static int batadv_netlink_get_vlan(struct sk_buff *skb, struct genl_info *info)
1093 {
1094 struct batadv_meshif_vlan *vlan = info->user_ptr[1];
1095 struct batadv_priv *bat_priv = info->user_ptr[0];
1096 struct sk_buff *msg;
1097 int ret;
1098
1099 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1100 if (!msg)
1101 return -ENOMEM;
1102
1103 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan, BATADV_CMD_GET_VLAN,
1104 info->snd_portid, info->snd_seq, 0);
1105 if (ret < 0) {
1106 nlmsg_free(msg);
1107 return ret;
1108 }
1109
1110 ret = genlmsg_reply(msg, info);
1111
1112 return ret;
1113 }
1114
1115 /**
1116 * batadv_netlink_set_vlan() - Get vlan attributes
1117 * @skb: Netlink message with request data
1118 * @info: receiver information
1119 *
1120 * Return: 0 on success or negative error number in case of failure
1121 */
batadv_netlink_set_vlan(struct sk_buff * skb,struct genl_info * info)1122 static int batadv_netlink_set_vlan(struct sk_buff *skb, struct genl_info *info)
1123 {
1124 struct batadv_meshif_vlan *vlan = info->user_ptr[1];
1125 struct batadv_priv *bat_priv = info->user_ptr[0];
1126 struct nlattr *attr;
1127
1128 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
1129 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
1130
1131 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
1132 }
1133
1134 batadv_netlink_notify_vlan(bat_priv, vlan);
1135
1136 return 0;
1137 }
1138
1139 /**
1140 * batadv_netlink_get_meshif_from_ifindex() - Get mesh-iface from ifindex
1141 * @net: the applicable net namespace
1142 * @ifindex: index of the mesh interface
1143 *
1144 * Return: Pointer to mesh interface (with increased refcnt) on success, error
1145 * pointer on error
1146 */
1147 static struct net_device *
batadv_netlink_get_meshif_from_ifindex(struct net * net,int ifindex)1148 batadv_netlink_get_meshif_from_ifindex(struct net *net, int ifindex)
1149 {
1150 struct net_device *mesh_iface;
1151
1152 mesh_iface = dev_get_by_index(net, ifindex);
1153 if (!mesh_iface)
1154 return ERR_PTR(-ENODEV);
1155
1156 if (!batadv_meshif_is_valid(mesh_iface))
1157 goto err_put_meshif;
1158
1159 return mesh_iface;
1160
1161 err_put_meshif:
1162 dev_put(mesh_iface);
1163
1164 return ERR_PTR(-EINVAL);
1165 }
1166
1167 /**
1168 * batadv_netlink_get_meshif_from_info() - Get mesh-iface from genl attributes
1169 * @net: the applicable net namespace
1170 * @info: receiver information
1171 *
1172 * Return: Pointer to mesh interface (with increased refcnt) on success, error
1173 * pointer on error
1174 */
1175 static struct net_device *
batadv_netlink_get_meshif_from_info(struct net * net,struct genl_info * info)1176 batadv_netlink_get_meshif_from_info(struct net *net, struct genl_info *info)
1177 {
1178 int ifindex;
1179
1180 if (!info->attrs[BATADV_ATTR_MESH_IFINDEX])
1181 return ERR_PTR(-EINVAL);
1182
1183 ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
1184
1185 return batadv_netlink_get_meshif_from_ifindex(net, ifindex);
1186 }
1187
1188 /**
1189 * batadv_netlink_get_meshif() - Retrieve mesh interface from netlink callback
1190 * @cb: callback structure containing arguments
1191 *
1192 * Return: Pointer to mesh interface (with increased refcnt) on success, error
1193 * pointer on error
1194 */
batadv_netlink_get_meshif(struct netlink_callback * cb)1195 struct net_device *batadv_netlink_get_meshif(struct netlink_callback *cb)
1196 {
1197 int ifindex = batadv_netlink_get_ifindex(cb->nlh,
1198 BATADV_ATTR_MESH_IFINDEX);
1199 if (!ifindex)
1200 return ERR_PTR(-ENONET);
1201
1202 return batadv_netlink_get_meshif_from_ifindex(sock_net(cb->skb->sk),
1203 ifindex);
1204 }
1205
1206 /**
1207 * batadv_netlink_get_hardif_from_ifindex() - Get hard-iface from ifindex
1208 * @bat_priv: the bat priv with all the mesh interface information
1209 * @net: the applicable net namespace
1210 * @ifindex: index of the hard interface
1211 *
1212 * Return: Pointer to hard interface (with increased refcnt) on success, error
1213 * pointer on error
1214 */
1215 static struct batadv_hard_iface *
batadv_netlink_get_hardif_from_ifindex(struct batadv_priv * bat_priv,struct net * net,int ifindex)1216 batadv_netlink_get_hardif_from_ifindex(struct batadv_priv *bat_priv,
1217 struct net *net, int ifindex)
1218 {
1219 struct batadv_hard_iface *hard_iface;
1220 struct net_device *hard_dev;
1221
1222 hard_dev = dev_get_by_index(net, ifindex);
1223 if (!hard_dev)
1224 return ERR_PTR(-ENODEV);
1225
1226 hard_iface = batadv_hardif_get_by_netdev(hard_dev);
1227 if (!hard_iface)
1228 goto err_put_harddev;
1229
1230 if (hard_iface->mesh_iface != bat_priv->mesh_iface)
1231 goto err_put_hardif;
1232
1233 /* hard_dev is referenced by hard_iface and not needed here */
1234 dev_put(hard_dev);
1235
1236 return hard_iface;
1237
1238 err_put_hardif:
1239 batadv_hardif_put(hard_iface);
1240 err_put_harddev:
1241 dev_put(hard_dev);
1242
1243 return ERR_PTR(-EINVAL);
1244 }
1245
1246 /**
1247 * batadv_netlink_get_hardif_from_info() - Get hard-iface from genl attributes
1248 * @bat_priv: the bat priv with all the mesh interface information
1249 * @net: the applicable net namespace
1250 * @info: receiver information
1251 *
1252 * Return: Pointer to hard interface (with increased refcnt) on success, error
1253 * pointer on error
1254 */
1255 static struct batadv_hard_iface *
batadv_netlink_get_hardif_from_info(struct batadv_priv * bat_priv,struct net * net,struct genl_info * info)1256 batadv_netlink_get_hardif_from_info(struct batadv_priv *bat_priv,
1257 struct net *net, struct genl_info *info)
1258 {
1259 int ifindex;
1260
1261 if (!info->attrs[BATADV_ATTR_HARD_IFINDEX])
1262 return ERR_PTR(-EINVAL);
1263
1264 ifindex = nla_get_u32(info->attrs[BATADV_ATTR_HARD_IFINDEX]);
1265
1266 return batadv_netlink_get_hardif_from_ifindex(bat_priv, net, ifindex);
1267 }
1268
1269 /**
1270 * batadv_netlink_get_hardif() - Retrieve hard interface from netlink callback
1271 * @bat_priv: the bat priv with all the mesh interface information
1272 * @cb: callback structure containing arguments
1273 *
1274 * Return: Pointer to hard interface (with increased refcnt) on success, error
1275 * pointer on error
1276 */
1277 struct batadv_hard_iface *
batadv_netlink_get_hardif(struct batadv_priv * bat_priv,struct netlink_callback * cb)1278 batadv_netlink_get_hardif(struct batadv_priv *bat_priv,
1279 struct netlink_callback *cb)
1280 {
1281 int ifindex = batadv_netlink_get_ifindex(cb->nlh,
1282 BATADV_ATTR_HARD_IFINDEX);
1283 if (!ifindex)
1284 return ERR_PTR(-ENONET);
1285
1286 return batadv_netlink_get_hardif_from_ifindex(bat_priv,
1287 sock_net(cb->skb->sk),
1288 ifindex);
1289 }
1290
1291 /**
1292 * batadv_get_vlan_from_info() - Retrieve vlan from genl attributes
1293 * @bat_priv: the bat priv with all the mesh interface information
1294 * @net: the applicable net namespace
1295 * @info: receiver information
1296 *
1297 * Return: Pointer to vlan on success (with increased refcnt), error pointer
1298 * on error
1299 */
1300 static struct batadv_meshif_vlan *
batadv_get_vlan_from_info(struct batadv_priv * bat_priv,struct net * net,struct genl_info * info)1301 batadv_get_vlan_from_info(struct batadv_priv *bat_priv, struct net *net,
1302 struct genl_info *info)
1303 {
1304 struct batadv_meshif_vlan *vlan;
1305 u16 vid;
1306
1307 if (!info->attrs[BATADV_ATTR_VLANID])
1308 return ERR_PTR(-EINVAL);
1309
1310 vid = nla_get_u16(info->attrs[BATADV_ATTR_VLANID]);
1311
1312 vlan = batadv_meshif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
1313 if (!vlan)
1314 return ERR_PTR(-ENOENT);
1315
1316 return vlan;
1317 }
1318
1319 /**
1320 * batadv_pre_doit() - Prepare batman-adv genl doit request
1321 * @ops: requested netlink operation
1322 * @skb: Netlink message with request data
1323 * @info: receiver information
1324 *
1325 * Return: 0 on success or negative error number in case of failure
1326 */
batadv_pre_doit(const struct genl_split_ops * ops,struct sk_buff * skb,struct genl_info * info)1327 static int batadv_pre_doit(const struct genl_split_ops *ops,
1328 struct sk_buff *skb,
1329 struct genl_info *info)
1330 {
1331 struct net *net = genl_info_net(info);
1332 struct batadv_hard_iface *hard_iface;
1333 struct batadv_priv *bat_priv = NULL;
1334 struct batadv_meshif_vlan *vlan;
1335 struct net_device *mesh_iface;
1336 u8 user_ptr1_flags;
1337 u8 mesh_dep_flags;
1338 int ret;
1339
1340 user_ptr1_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
1341 if (WARN_ON(hweight8(ops->internal_flags & user_ptr1_flags) > 1))
1342 return -EINVAL;
1343
1344 mesh_dep_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
1345 if (WARN_ON((ops->internal_flags & mesh_dep_flags) &&
1346 (~ops->internal_flags & BATADV_FLAG_NEED_MESH)))
1347 return -EINVAL;
1348
1349 if (ops->internal_flags & BATADV_FLAG_NEED_MESH) {
1350 mesh_iface = batadv_netlink_get_meshif_from_info(net, info);
1351 if (IS_ERR(mesh_iface))
1352 return PTR_ERR(mesh_iface);
1353
1354 bat_priv = netdev_priv(mesh_iface);
1355 info->user_ptr[0] = bat_priv;
1356 }
1357
1358 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF) {
1359 hard_iface = batadv_netlink_get_hardif_from_info(bat_priv, net,
1360 info);
1361 if (IS_ERR(hard_iface)) {
1362 ret = PTR_ERR(hard_iface);
1363 goto err_put_meshif;
1364 }
1365
1366 info->user_ptr[1] = hard_iface;
1367 }
1368
1369 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN) {
1370 vlan = batadv_get_vlan_from_info(bat_priv, net, info);
1371 if (IS_ERR(vlan)) {
1372 ret = PTR_ERR(vlan);
1373 goto err_put_meshif;
1374 }
1375
1376 info->user_ptr[1] = vlan;
1377 }
1378
1379 return 0;
1380
1381 err_put_meshif:
1382 if (bat_priv)
1383 dev_put(bat_priv->mesh_iface);
1384
1385 return ret;
1386 }
1387
1388 /**
1389 * batadv_post_doit() - End batman-adv genl doit request
1390 * @ops: requested netlink operation
1391 * @skb: Netlink message with request data
1392 * @info: receiver information
1393 */
batadv_post_doit(const struct genl_split_ops * ops,struct sk_buff * skb,struct genl_info * info)1394 static void batadv_post_doit(const struct genl_split_ops *ops,
1395 struct sk_buff *skb,
1396 struct genl_info *info)
1397 {
1398 struct batadv_hard_iface *hard_iface;
1399 struct batadv_meshif_vlan *vlan;
1400 struct batadv_priv *bat_priv;
1401
1402 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF &&
1403 info->user_ptr[1]) {
1404 hard_iface = info->user_ptr[1];
1405
1406 batadv_hardif_put(hard_iface);
1407 }
1408
1409 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN && info->user_ptr[1]) {
1410 vlan = info->user_ptr[1];
1411 batadv_meshif_vlan_put(vlan);
1412 }
1413
1414 if (ops->internal_flags & BATADV_FLAG_NEED_MESH && info->user_ptr[0]) {
1415 bat_priv = info->user_ptr[0];
1416 dev_put(bat_priv->mesh_iface);
1417 }
1418 }
1419
1420 static const struct genl_small_ops batadv_netlink_ops[] = {
1421 {
1422 .cmd = BATADV_CMD_GET_MESH,
1423 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1424 /* can be retrieved by unprivileged users */
1425 .doit = batadv_netlink_get_mesh,
1426 .internal_flags = BATADV_FLAG_NEED_MESH,
1427 },
1428 {
1429 .cmd = BATADV_CMD_TP_METER,
1430 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1431 .flags = GENL_UNS_ADMIN_PERM,
1432 .doit = batadv_netlink_tp_meter_start,
1433 .internal_flags = BATADV_FLAG_NEED_MESH,
1434 },
1435 {
1436 .cmd = BATADV_CMD_TP_METER_CANCEL,
1437 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1438 .flags = GENL_UNS_ADMIN_PERM,
1439 .doit = batadv_netlink_tp_meter_cancel,
1440 .internal_flags = BATADV_FLAG_NEED_MESH,
1441 },
1442 {
1443 .cmd = BATADV_CMD_GET_ROUTING_ALGOS,
1444 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1445 .flags = GENL_UNS_ADMIN_PERM,
1446 .dumpit = batadv_algo_dump,
1447 },
1448 {
1449 .cmd = BATADV_CMD_GET_HARDIF,
1450 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1451 /* can be retrieved by unprivileged users */
1452 .dumpit = batadv_netlink_dump_hardif,
1453 .doit = batadv_netlink_cmd_get_hardif,
1454 .internal_flags = BATADV_FLAG_NEED_MESH |
1455 BATADV_FLAG_NEED_HARDIF,
1456 },
1457 {
1458 .cmd = BATADV_CMD_GET_TRANSTABLE_LOCAL,
1459 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1460 .flags = GENL_UNS_ADMIN_PERM,
1461 .dumpit = batadv_tt_local_dump,
1462 },
1463 {
1464 .cmd = BATADV_CMD_GET_TRANSTABLE_GLOBAL,
1465 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1466 .flags = GENL_UNS_ADMIN_PERM,
1467 .dumpit = batadv_tt_global_dump,
1468 },
1469 {
1470 .cmd = BATADV_CMD_GET_ORIGINATORS,
1471 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1472 .flags = GENL_UNS_ADMIN_PERM,
1473 .dumpit = batadv_orig_dump,
1474 },
1475 {
1476 .cmd = BATADV_CMD_GET_NEIGHBORS,
1477 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1478 .flags = GENL_UNS_ADMIN_PERM,
1479 .dumpit = batadv_hardif_neigh_dump,
1480 },
1481 {
1482 .cmd = BATADV_CMD_GET_GATEWAYS,
1483 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1484 .flags = GENL_UNS_ADMIN_PERM,
1485 .dumpit = batadv_gw_dump,
1486 },
1487 {
1488 .cmd = BATADV_CMD_GET_BLA_CLAIM,
1489 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1490 .flags = GENL_UNS_ADMIN_PERM,
1491 .dumpit = batadv_bla_claim_dump,
1492 },
1493 {
1494 .cmd = BATADV_CMD_GET_BLA_BACKBONE,
1495 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1496 .flags = GENL_UNS_ADMIN_PERM,
1497 .dumpit = batadv_bla_backbone_dump,
1498 },
1499 {
1500 .cmd = BATADV_CMD_GET_DAT_CACHE,
1501 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1502 .flags = GENL_UNS_ADMIN_PERM,
1503 .dumpit = batadv_dat_cache_dump,
1504 },
1505 {
1506 .cmd = BATADV_CMD_GET_MCAST_FLAGS,
1507 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1508 .flags = GENL_UNS_ADMIN_PERM,
1509 .dumpit = batadv_mcast_flags_dump,
1510 },
1511 {
1512 .cmd = BATADV_CMD_SET_MESH,
1513 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1514 .flags = GENL_UNS_ADMIN_PERM,
1515 .doit = batadv_netlink_set_mesh,
1516 .internal_flags = BATADV_FLAG_NEED_MESH,
1517 },
1518 {
1519 .cmd = BATADV_CMD_SET_HARDIF,
1520 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1521 .flags = GENL_UNS_ADMIN_PERM,
1522 .doit = batadv_netlink_set_hardif,
1523 .internal_flags = BATADV_FLAG_NEED_MESH |
1524 BATADV_FLAG_NEED_HARDIF,
1525 },
1526 {
1527 .cmd = BATADV_CMD_GET_VLAN,
1528 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1529 /* can be retrieved by unprivileged users */
1530 .doit = batadv_netlink_get_vlan,
1531 .internal_flags = BATADV_FLAG_NEED_MESH |
1532 BATADV_FLAG_NEED_VLAN,
1533 },
1534 {
1535 .cmd = BATADV_CMD_SET_VLAN,
1536 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1537 .flags = GENL_UNS_ADMIN_PERM,
1538 .doit = batadv_netlink_set_vlan,
1539 .internal_flags = BATADV_FLAG_NEED_MESH |
1540 BATADV_FLAG_NEED_VLAN,
1541 },
1542 };
1543
1544 struct genl_family batadv_netlink_family __ro_after_init = {
1545 .hdrsize = 0,
1546 .name = BATADV_NL_NAME,
1547 .version = 1,
1548 .maxattr = BATADV_ATTR_MAX,
1549 .policy = batadv_netlink_policy,
1550 .netnsok = true,
1551 .pre_doit = batadv_pre_doit,
1552 .post_doit = batadv_post_doit,
1553 .module = THIS_MODULE,
1554 .small_ops = batadv_netlink_ops,
1555 .n_small_ops = ARRAY_SIZE(batadv_netlink_ops),
1556 .resv_start_op = BATADV_CMD_SET_VLAN + 1,
1557 .mcgrps = batadv_netlink_mcgrps,
1558 .n_mcgrps = ARRAY_SIZE(batadv_netlink_mcgrps),
1559 };
1560
1561 /**
1562 * batadv_netlink_register() - register batadv genl netlink family
1563 */
batadv_netlink_register(void)1564 void __init batadv_netlink_register(void)
1565 {
1566 int ret;
1567
1568 ret = genl_register_family(&batadv_netlink_family);
1569 if (ret)
1570 pr_warn("unable to register netlink family\n");
1571 }
1572
1573 /**
1574 * batadv_netlink_unregister() - unregister batadv genl netlink family
1575 */
batadv_netlink_unregister(void)1576 void batadv_netlink_unregister(void)
1577 {
1578 genl_unregister_family(&batadv_netlink_family);
1579 }
1580