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