blob: 40c940da94988c7065eb5780d9dbb35d83ee35df [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann7a79d712018-12-31 23:59:59 +01002/* Copyright (C) 2016-2019 B.A.T.M.A.N. contributors:
Matthias Schiffer09748a22016-05-09 18:41:08 +02003 *
4 * Matthias Schiffer
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "netlink.h"
20#include "main.h"
21
Sven Eckelmannf32ed4b2016-07-03 13:31:38 +020022#include <linux/atomic.h>
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +010023#include <linux/bitops.h>
Sven Eckelmann5c55a402018-11-23 12:33:17 +010024#include <linux/bug.h>
Sven Eckelmann8dad6f0d2016-07-03 13:31:46 +020025#include <linux/byteorder/generic.h>
Sven Eckelmann9bcb94c2016-10-29 10:13:47 +020026#include <linux/cache.h>
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +010027#include <linux/err.h>
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020028#include <linux/errno.h>
Sven Eckelmann9bcb94c2016-10-29 10:13:47 +020029#include <linux/export.h>
Matthias Schiffer09748a22016-05-09 18:41:08 +020030#include <linux/genetlink.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010031#include <linux/gfp.h>
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020032#include <linux/if_ether.h>
Sven Eckelmann49e7e372018-11-23 12:41:08 +010033#include <linux/if_vlan.h>
Matthias Schiffer09748a22016-05-09 18:41:08 +020034#include <linux/init.h>
Sven Eckelmann9bcb94c2016-10-29 10:13:47 +020035#include <linux/kernel.h>
Sven Eckelmannfb69be62018-10-30 22:01:24 +010036#include <linux/list.h>
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020037#include <linux/netdevice.h>
38#include <linux/netlink.h>
Matthias Schiffer09748a22016-05-09 18:41:08 +020039#include <linux/printk.h>
Sven Eckelmannfb69be62018-10-30 22:01:24 +010040#include <linux/rtnetlink.h>
Matthias Schifferb60620c2016-07-03 13:31:36 +020041#include <linux/skbuff.h>
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020042#include <linux/stddef.h>
Antonio Quartulli33a3bb42016-05-05 13:09:43 +020043#include <linux/types.h>
Matthias Schiffer09748a22016-05-09 18:41:08 +020044#include <net/genetlink.h>
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020045#include <net/netlink.h>
Matthias Schifferb60620c2016-07-03 13:31:36 +020046#include <net/sock.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010047#include <uapi/linux/batadv_packet.h>
Matthias Schiffer09748a22016-05-09 18:41:08 +020048#include <uapi/linux/batman_adv.h>
49
Matthias Schiffer07a30612016-07-03 13:31:35 +020050#include "bat_algo.h"
Andrew Lunn04f3f5b2016-07-03 13:31:45 +020051#include "bridge_loop_avoidance.h"
Linus Lüssing41aeefc2018-03-13 11:41:12 +010052#include "distributed-arp-table.h"
Sven Eckelmannd7129da2016-07-03 13:31:42 +020053#include "gateway_client.h"
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020054#include "hard-interface.h"
Linus Lüssing53dd9a62018-03-13 11:41:13 +010055#include "multicast.h"
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020056#include "originator.h"
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020057#include "soft-interface.h"
Antonio Quartulli33a3bb42016-05-05 13:09:43 +020058#include "tp_meter.h"
Matthias Schifferd34f0552016-07-03 13:31:37 +020059#include "translation-table.h"
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020060
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +010061struct net;
62
Johannes Berg489111e2016-10-24 14:40:03 +020063struct genl_family batadv_netlink_family;
Matthias Schiffer09748a22016-05-09 18:41:08 +020064
Antonio Quartulli33a3bb42016-05-05 13:09:43 +020065/* multicast groups */
66enum batadv_netlink_multicast_groups {
Sven Eckelmann60040512018-11-23 12:14:56 +010067 BATADV_NL_MCGRP_CONFIG,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +020068 BATADV_NL_MCGRP_TPMETER,
69};
70
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +010071/**
72 * enum batadv_genl_ops_flags - flags for genl_ops's internal_flags
73 */
74enum batadv_genl_ops_flags {
75 /**
76 * @BATADV_FLAG_NEED_MESH: request requires valid soft interface in
77 * attribute BATADV_ATTR_MESH_IFINDEX and expects a pointer to it to be
78 * saved in info->user_ptr[0]
79 */
80 BATADV_FLAG_NEED_MESH = BIT(0),
Sven Eckelmann5c55a402018-11-23 12:33:17 +010081
82 /**
83 * @BATADV_FLAG_NEED_HARDIF: request requires valid hard interface in
84 * attribute BATADV_ATTR_HARD_IFINDEX and expects a pointer to it to be
85 * saved in info->user_ptr[1]
86 */
87 BATADV_FLAG_NEED_HARDIF = BIT(1),
Sven Eckelmann49e7e372018-11-23 12:41:08 +010088
89 /**
90 * @BATADV_FLAG_NEED_VLAN: request requires valid vlan in
91 * attribute BATADV_ATTR_VLANID and expects a pointer to it to be
92 * saved in info->user_ptr[1]
93 */
94 BATADV_FLAG_NEED_VLAN = BIT(2),
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +010095};
96
stephen hemmingerdeeb91f2016-08-31 15:17:00 -070097static const struct genl_multicast_group batadv_netlink_mcgrps[] = {
Sven Eckelmann60040512018-11-23 12:14:56 +010098 [BATADV_NL_MCGRP_CONFIG] = { .name = BATADV_NL_MCAST_GROUP_CONFIG },
Antonio Quartulli33a3bb42016-05-05 13:09:43 +020099 [BATADV_NL_MCGRP_TPMETER] = { .name = BATADV_NL_MCAST_GROUP_TPMETER },
100};
101
stephen hemmingerdeeb91f2016-08-31 15:17:00 -0700102static const struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = {
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100103 [BATADV_ATTR_VERSION] = { .type = NLA_STRING },
104 [BATADV_ATTR_ALGO_NAME] = { .type = NLA_STRING },
105 [BATADV_ATTR_MESH_IFINDEX] = { .type = NLA_U32 },
106 [BATADV_ATTR_MESH_IFNAME] = { .type = NLA_STRING },
107 [BATADV_ATTR_MESH_ADDRESS] = { .len = ETH_ALEN },
108 [BATADV_ATTR_HARD_IFINDEX] = { .type = NLA_U32 },
109 [BATADV_ATTR_HARD_IFNAME] = { .type = NLA_STRING },
110 [BATADV_ATTR_HARD_ADDRESS] = { .len = ETH_ALEN },
111 [BATADV_ATTR_ORIG_ADDRESS] = { .len = ETH_ALEN },
112 [BATADV_ATTR_TPMETER_RESULT] = { .type = NLA_U8 },
113 [BATADV_ATTR_TPMETER_TEST_TIME] = { .type = NLA_U32 },
114 [BATADV_ATTR_TPMETER_BYTES] = { .type = NLA_U64 },
115 [BATADV_ATTR_TPMETER_COOKIE] = { .type = NLA_U32 },
116 [BATADV_ATTR_ACTIVE] = { .type = NLA_FLAG },
117 [BATADV_ATTR_TT_ADDRESS] = { .len = ETH_ALEN },
118 [BATADV_ATTR_TT_TTVN] = { .type = NLA_U8 },
119 [BATADV_ATTR_TT_LAST_TTVN] = { .type = NLA_U8 },
120 [BATADV_ATTR_TT_CRC32] = { .type = NLA_U32 },
121 [BATADV_ATTR_TT_VID] = { .type = NLA_U16 },
122 [BATADV_ATTR_TT_FLAGS] = { .type = NLA_U32 },
123 [BATADV_ATTR_FLAG_BEST] = { .type = NLA_FLAG },
124 [BATADV_ATTR_LAST_SEEN_MSECS] = { .type = NLA_U32 },
125 [BATADV_ATTR_NEIGH_ADDRESS] = { .len = ETH_ALEN },
126 [BATADV_ATTR_TQ] = { .type = NLA_U8 },
127 [BATADV_ATTR_THROUGHPUT] = { .type = NLA_U32 },
128 [BATADV_ATTR_BANDWIDTH_UP] = { .type = NLA_U32 },
129 [BATADV_ATTR_BANDWIDTH_DOWN] = { .type = NLA_U32 },
130 [BATADV_ATTR_ROUTER] = { .len = ETH_ALEN },
131 [BATADV_ATTR_BLA_OWN] = { .type = NLA_FLAG },
132 [BATADV_ATTR_BLA_ADDRESS] = { .len = ETH_ALEN },
133 [BATADV_ATTR_BLA_VID] = { .type = NLA_U16 },
134 [BATADV_ATTR_BLA_BACKBONE] = { .len = ETH_ALEN },
135 [BATADV_ATTR_BLA_CRC] = { .type = NLA_U16 },
136 [BATADV_ATTR_DAT_CACHE_IP4ADDRESS] = { .type = NLA_U32 },
137 [BATADV_ATTR_DAT_CACHE_HWADDRESS] = { .len = ETH_ALEN },
138 [BATADV_ATTR_DAT_CACHE_VID] = { .type = NLA_U16 },
Linus Lüssing53dd9a62018-03-13 11:41:13 +0100139 [BATADV_ATTR_MCAST_FLAGS] = { .type = NLA_U32 },
140 [BATADV_ATTR_MCAST_FLAGS_PRIV] = { .type = NLA_U32 },
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100141 [BATADV_ATTR_VLANID] = { .type = NLA_U16 },
Sven Eckelmann9ab4cee2018-11-23 12:46:14 +0100142 [BATADV_ATTR_AGGREGATED_OGMS_ENABLED] = { .type = NLA_U8 },
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100143 [BATADV_ATTR_AP_ISOLATION_ENABLED] = { .type = NLA_U8 },
144 [BATADV_ATTR_ISOLATION_MARK] = { .type = NLA_U32 },
145 [BATADV_ATTR_ISOLATION_MASK] = { .type = NLA_U32 },
Sven Eckelmannd7e52502018-11-23 12:55:44 +0100146 [BATADV_ATTR_BONDING_ENABLED] = { .type = NLA_U8 },
Sven Eckelmann43ff6102018-11-23 13:03:39 +0100147 [BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED] = { .type = NLA_U8 },
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200148};
149
150/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100151 * batadv_netlink_get_ifindex() - Extract an interface index from a message
Matthias Schifferb60620c2016-07-03 13:31:36 +0200152 * @nlh: Message header
153 * @attrtype: Attribute which holds an interface index
154 *
155 * Return: interface index, or 0.
156 */
Matthias Schifferd34f0552016-07-03 13:31:37 +0200157int
Matthias Schifferb60620c2016-07-03 13:31:36 +0200158batadv_netlink_get_ifindex(const struct nlmsghdr *nlh, int attrtype)
159{
160 struct nlattr *attr = nlmsg_find_attr(nlh, GENL_HDRLEN, attrtype);
161
162 return attr ? nla_get_u32(attr) : 0;
163}
164
165/**
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100166 * batadv_netlink_mesh_fill_ap_isolation() - Add ap_isolation softif attribute
167 * @msg: Netlink message to dump into
168 * @bat_priv: the bat priv with all the soft interface information
169 *
170 * Return: 0 on success or negative error number in case of failure
171 */
172static int batadv_netlink_mesh_fill_ap_isolation(struct sk_buff *msg,
173 struct batadv_priv *bat_priv)
174{
175 struct batadv_softif_vlan *vlan;
176 u8 ap_isolation;
177
178 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
179 if (!vlan)
180 return 0;
181
182 ap_isolation = atomic_read(&vlan->ap_isolation);
183 batadv_softif_vlan_put(vlan);
184
185 return nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
186 !!ap_isolation);
187}
188
189/**
190 * batadv_option_set_ap_isolation() - Set ap_isolation from genl msg
191 * @attr: parsed BATADV_ATTR_AP_ISOLATION_ENABLED attribute
192 * @bat_priv: the bat priv with all the soft interface information
193 *
194 * Return: 0 on success or negative error number in case of failure
195 */
196static int batadv_netlink_set_mesh_ap_isolation(struct nlattr *attr,
197 struct batadv_priv *bat_priv)
198{
199 struct batadv_softif_vlan *vlan;
200
201 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
202 if (!vlan)
203 return -ENOENT;
204
205 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
206 batadv_softif_vlan_put(vlan);
207
208 return 0;
209}
210
211/**
Sven Eckelmann60040512018-11-23 12:14:56 +0100212 * batadv_netlink_mesh_fill() - Fill message with mesh attributes
213 * @msg: Netlink message to dump into
214 * @bat_priv: the bat priv with all the soft interface information
215 * @cmd: type of message to generate
216 * @portid: Port making netlink request
217 * @seq: sequence number for message
218 * @flags: Additional flags for message
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200219 *
Sven Eckelmann60040512018-11-23 12:14:56 +0100220 * Return: 0 on success or negative error number in case of failure
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200221 */
Sven Eckelmann60040512018-11-23 12:14:56 +0100222static int batadv_netlink_mesh_fill(struct sk_buff *msg,
223 struct batadv_priv *bat_priv,
224 enum batadv_nl_commands cmd,
225 u32 portid, u32 seq, int flags)
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200226{
Sven Eckelmann60040512018-11-23 12:14:56 +0100227 struct net_device *soft_iface = bat_priv->soft_iface;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200228 struct batadv_hard_iface *primary_if = NULL;
229 struct net_device *hard_iface;
Sven Eckelmann60040512018-11-23 12:14:56 +0100230 void *hdr;
231
232 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
233 if (!hdr)
234 return -ENOBUFS;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200235
236 if (nla_put_string(msg, BATADV_ATTR_VERSION, BATADV_SOURCE_VERSION) ||
237 nla_put_string(msg, BATADV_ATTR_ALGO_NAME,
Antonio Quartulli29824a52016-05-25 23:27:31 +0800238 bat_priv->algo_ops->name) ||
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200239 nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, soft_iface->ifindex) ||
240 nla_put_string(msg, BATADV_ATTR_MESH_IFNAME, soft_iface->name) ||
241 nla_put(msg, BATADV_ATTR_MESH_ADDRESS, ETH_ALEN,
Sven Eckelmannf32ed4b2016-07-03 13:31:38 +0200242 soft_iface->dev_addr) ||
243 nla_put_u8(msg, BATADV_ATTR_TT_TTVN,
244 (u8)atomic_read(&bat_priv->tt.vn)))
Sven Eckelmann60040512018-11-23 12:14:56 +0100245 goto nla_put_failure;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200246
Sven Eckelmann8dad6f0d2016-07-03 13:31:46 +0200247#ifdef CONFIG_BATMAN_ADV_BLA
248 if (nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
249 ntohs(bat_priv->bla.claim_dest.group)))
Sven Eckelmann60040512018-11-23 12:14:56 +0100250 goto nla_put_failure;
Sven Eckelmann8dad6f0d2016-07-03 13:31:46 +0200251#endif
252
Linus Lüssing53dd9a62018-03-13 11:41:13 +0100253 if (batadv_mcast_mesh_info_put(msg, bat_priv))
Sven Eckelmann60040512018-11-23 12:14:56 +0100254 goto nla_put_failure;
Linus Lüssing53dd9a62018-03-13 11:41:13 +0100255
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200256 primary_if = batadv_primary_if_get_selected(bat_priv);
257 if (primary_if && primary_if->if_status == BATADV_IF_ACTIVE) {
258 hard_iface = primary_if->net_dev;
259
260 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
261 hard_iface->ifindex) ||
262 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
263 hard_iface->name) ||
264 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
265 hard_iface->dev_addr))
Sven Eckelmann60040512018-11-23 12:14:56 +0100266 goto nla_put_failure;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200267 }
268
Sven Eckelmann9ab4cee2018-11-23 12:46:14 +0100269 if (nla_put_u8(msg, BATADV_ATTR_AGGREGATED_OGMS_ENABLED,
270 !!atomic_read(&bat_priv->aggregated_ogms)))
271 goto nla_put_failure;
272
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100273 if (batadv_netlink_mesh_fill_ap_isolation(msg, bat_priv))
274 goto nla_put_failure;
275
276 if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MARK,
277 bat_priv->isolation_mark))
278 goto nla_put_failure;
279
280 if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MASK,
281 bat_priv->isolation_mark_mask))
282 goto nla_put_failure;
283
Sven Eckelmannd7e52502018-11-23 12:55:44 +0100284 if (nla_put_u8(msg, BATADV_ATTR_BONDING_ENABLED,
285 !!atomic_read(&bat_priv->bonding)))
286 goto nla_put_failure;
287
Sven Eckelmann43ff6102018-11-23 13:03:39 +0100288#ifdef CONFIG_BATMAN_ADV_BLA
289 if (nla_put_u8(msg, BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED,
290 !!atomic_read(&bat_priv->bridge_loop_avoidance)))
291 goto nla_put_failure;
292#endif /* CONFIG_BATMAN_ADV_BLA */
293
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200294 if (primary_if)
295 batadv_hardif_put(primary_if);
296
Sven Eckelmann60040512018-11-23 12:14:56 +0100297 genlmsg_end(msg, hdr);
298 return 0;
299
300nla_put_failure:
301 if (primary_if)
302 batadv_hardif_put(primary_if);
303
304 genlmsg_cancel(msg, hdr);
305 return -EMSGSIZE;
306}
307
308/**
309 * batadv_netlink_notify_mesh() - send softif attributes to listener
310 * @bat_priv: the bat priv with all the soft interface information
311 *
312 * Return: 0 on success, < 0 on error
313 */
314static int batadv_netlink_notify_mesh(struct batadv_priv *bat_priv)
315{
316 struct sk_buff *msg;
317 int ret;
318
319 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
320 if (!msg)
321 return -ENOMEM;
322
323 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_SET_MESH,
324 0, 0, 0);
325 if (ret < 0) {
326 nlmsg_free(msg);
327 return ret;
328 }
329
330 genlmsg_multicast_netns(&batadv_netlink_family,
331 dev_net(bat_priv->soft_iface), msg, 0,
332 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
333
334 return 0;
335}
336
337/**
338 * batadv_netlink_get_mesh() - Get softif attributes
339 * @skb: Netlink message with request data
340 * @info: receiver information
341 *
342 * Return: 0 on success or negative error number in case of failure
343 */
344static int batadv_netlink_get_mesh(struct sk_buff *skb, struct genl_info *info)
345{
346 struct batadv_priv *bat_priv = info->user_ptr[0];
347 struct sk_buff *msg;
348 int ret;
349
350 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
351 if (!msg)
352 return -ENOMEM;
353
354 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_GET_MESH,
355 info->snd_portid, info->snd_seq, 0);
356 if (ret < 0) {
357 nlmsg_free(msg);
358 return ret;
359 }
360
361 ret = genlmsg_reply(msg, info);
362
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200363 return ret;
364}
365
366/**
Sven Eckelmann60040512018-11-23 12:14:56 +0100367 * batadv_netlink_set_mesh() - Set softif attributes
368 * @skb: Netlink message with request data
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200369 * @info: receiver information
370 *
Sven Eckelmann60040512018-11-23 12:14:56 +0100371 * Return: 0 on success or negative error number in case of failure
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200372 */
Sven Eckelmann60040512018-11-23 12:14:56 +0100373static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info)
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200374{
Sven Eckelmann60040512018-11-23 12:14:56 +0100375 struct batadv_priv *bat_priv = info->user_ptr[0];
Sven Eckelmann9ab4cee2018-11-23 12:46:14 +0100376 struct nlattr *attr;
377
378 if (info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED]) {
379 attr = info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED];
380
381 atomic_set(&bat_priv->aggregated_ogms, !!nla_get_u8(attr));
382 }
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200383
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100384 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
385 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
386
387 batadv_netlink_set_mesh_ap_isolation(attr, bat_priv);
388 }
389
390 if (info->attrs[BATADV_ATTR_ISOLATION_MARK]) {
391 attr = info->attrs[BATADV_ATTR_ISOLATION_MARK];
392
393 bat_priv->isolation_mark = nla_get_u32(attr);
394 }
395
396 if (info->attrs[BATADV_ATTR_ISOLATION_MASK]) {
397 attr = info->attrs[BATADV_ATTR_ISOLATION_MASK];
398
399 bat_priv->isolation_mark_mask = nla_get_u32(attr);
400 }
401
Sven Eckelmannd7e52502018-11-23 12:55:44 +0100402 if (info->attrs[BATADV_ATTR_BONDING_ENABLED]) {
403 attr = info->attrs[BATADV_ATTR_BONDING_ENABLED];
404
405 atomic_set(&bat_priv->bonding, !!nla_get_u8(attr));
406 }
407
Sven Eckelmann43ff6102018-11-23 13:03:39 +0100408#ifdef CONFIG_BATMAN_ADV_BLA
409 if (info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED]) {
410 attr = info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED];
411
412 atomic_set(&bat_priv->bridge_loop_avoidance,
413 !!nla_get_u8(attr));
414 batadv_bla_status_update(bat_priv->soft_iface);
415 }
416#endif /* CONFIG_BATMAN_ADV_BLA */
417
Sven Eckelmann60040512018-11-23 12:14:56 +0100418 batadv_netlink_notify_mesh(bat_priv);
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200419
Sven Eckelmann60040512018-11-23 12:14:56 +0100420 return 0;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200421}
422
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200423/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100424 * batadv_netlink_tp_meter_put() - Fill information of started tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200425 * @msg: netlink message to be sent back
426 * @cookie: tp meter session cookie
427 *
428 * Return: 0 on success, < 0 on error
429 */
430static int
431batadv_netlink_tp_meter_put(struct sk_buff *msg, u32 cookie)
432{
433 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
434 return -ENOBUFS;
435
436 return 0;
437}
438
439/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100440 * batadv_netlink_tpmeter_notify() - send tp_meter result via netlink to client
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200441 * @bat_priv: the bat priv with all the soft interface information
442 * @dst: destination of tp_meter session
443 * @result: reason for tp meter session stop
444 * @test_time: total time ot the tp_meter session
445 * @total_bytes: bytes acked to the receiver
446 * @cookie: cookie of tp_meter session
447 *
448 * Return: 0 on success, < 0 on error
449 */
450int batadv_netlink_tpmeter_notify(struct batadv_priv *bat_priv, const u8 *dst,
451 u8 result, u32 test_time, u64 total_bytes,
452 u32 cookie)
453{
454 struct sk_buff *msg;
455 void *hdr;
456 int ret;
457
458 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
459 if (!msg)
460 return -ENOMEM;
461
462 hdr = genlmsg_put(msg, 0, 0, &batadv_netlink_family, 0,
463 BATADV_CMD_TP_METER);
464 if (!hdr) {
465 ret = -ENOBUFS;
466 goto err_genlmsg;
467 }
468
469 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
470 goto nla_put_failure;
471
472 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, test_time))
473 goto nla_put_failure;
474
475 if (nla_put_u64_64bit(msg, BATADV_ATTR_TPMETER_BYTES, total_bytes,
476 BATADV_ATTR_PAD))
477 goto nla_put_failure;
478
479 if (nla_put_u8(msg, BATADV_ATTR_TPMETER_RESULT, result))
480 goto nla_put_failure;
481
482 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst))
483 goto nla_put_failure;
484
485 genlmsg_end(msg, hdr);
486
487 genlmsg_multicast_netns(&batadv_netlink_family,
488 dev_net(bat_priv->soft_iface), msg, 0,
489 BATADV_NL_MCGRP_TPMETER, GFP_KERNEL);
490
491 return 0;
492
493nla_put_failure:
494 genlmsg_cancel(msg, hdr);
495 ret = -EMSGSIZE;
496
497err_genlmsg:
498 nlmsg_free(msg);
499 return ret;
500}
501
502/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100503 * batadv_netlink_tp_meter_start() - Start a new tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200504 * @skb: received netlink message
505 * @info: receiver information
506 *
507 * Return: 0 on success, < 0 on error
508 */
509static int
510batadv_netlink_tp_meter_start(struct sk_buff *skb, struct genl_info *info)
511{
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100512 struct batadv_priv *bat_priv = info->user_ptr[0];
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200513 struct sk_buff *msg = NULL;
514 u32 test_length;
515 void *msg_head;
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200516 u32 cookie;
517 u8 *dst;
518 int ret;
519
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200520 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
521 return -EINVAL;
522
523 if (!info->attrs[BATADV_ATTR_TPMETER_TEST_TIME])
524 return -EINVAL;
525
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200526 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
527
528 test_length = nla_get_u32(info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]);
529
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200530 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
531 if (!msg) {
532 ret = -ENOMEM;
533 goto out;
534 }
535
536 msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq,
537 &batadv_netlink_family, 0,
538 BATADV_CMD_TP_METER);
539 if (!msg_head) {
540 ret = -ENOBUFS;
541 goto out;
542 }
543
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200544 batadv_tp_start(bat_priv, dst, test_length, &cookie);
545
546 ret = batadv_netlink_tp_meter_put(msg, cookie);
547
548 out:
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200549 if (ret) {
550 if (msg)
551 nlmsg_free(msg);
552 return ret;
553 }
554
555 genlmsg_end(msg, msg_head);
556 return genlmsg_reply(msg, info);
557}
558
559/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100560 * batadv_netlink_tp_meter_start() - Cancel a running tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200561 * @skb: received netlink message
562 * @info: receiver information
563 *
564 * Return: 0 on success, < 0 on error
565 */
566static int
567batadv_netlink_tp_meter_cancel(struct sk_buff *skb, struct genl_info *info)
568{
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100569 struct batadv_priv *bat_priv = info->user_ptr[0];
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200570 u8 *dst;
571 int ret = 0;
572
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200573 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
574 return -EINVAL;
575
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200576 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
577
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200578 batadv_tp_stop(bat_priv, dst, BATADV_TP_REASON_CANCEL);
579
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200580 return ret;
581}
582
Matthias Schifferb60620c2016-07-03 13:31:36 +0200583/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100584 * batadv_netlink_hardif_fill() - Fill message with hardif attributes
Matthias Schifferb60620c2016-07-03 13:31:36 +0200585 * @msg: Netlink message to dump into
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100586 * @bat_priv: the bat priv with all the soft interface information
587 * @hard_iface: hard interface which was modified
588 * @cmd: type of message to generate
Matthias Schifferb60620c2016-07-03 13:31:36 +0200589 * @portid: Port making netlink request
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100590 * @seq: sequence number for message
591 * @flags: Additional flags for message
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100592 * @cb: Control block containing additional options
Matthias Schifferb60620c2016-07-03 13:31:36 +0200593 *
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100594 * Return: 0 on success or negative error number in case of failure
Matthias Schifferb60620c2016-07-03 13:31:36 +0200595 */
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100596static int batadv_netlink_hardif_fill(struct sk_buff *msg,
597 struct batadv_priv *bat_priv,
598 struct batadv_hard_iface *hard_iface,
599 enum batadv_nl_commands cmd,
600 u32 portid, u32 seq, int flags,
601 struct netlink_callback *cb)
Matthias Schifferb60620c2016-07-03 13:31:36 +0200602{
603 struct net_device *net_dev = hard_iface->net_dev;
604 void *hdr;
605
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100606 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
Matthias Schifferb60620c2016-07-03 13:31:36 +0200607 if (!hdr)
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100608 return -ENOBUFS;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200609
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100610 if (cb)
611 genl_dump_check_consistent(cb, hdr);
612
613 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
614 bat_priv->soft_iface->ifindex))
615 goto nla_put_failure;
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100616
Matthias Schifferb60620c2016-07-03 13:31:36 +0200617 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
618 net_dev->ifindex) ||
619 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
620 net_dev->name) ||
621 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
622 net_dev->dev_addr))
623 goto nla_put_failure;
624
625 if (hard_iface->if_status == BATADV_IF_ACTIVE) {
626 if (nla_put_flag(msg, BATADV_ATTR_ACTIVE))
627 goto nla_put_failure;
628 }
629
630 genlmsg_end(msg, hdr);
631 return 0;
632
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100633nla_put_failure:
Matthias Schifferb60620c2016-07-03 13:31:36 +0200634 genlmsg_cancel(msg, hdr);
635 return -EMSGSIZE;
636}
637
638/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100639 * batadv_netlink_notify_hardif() - send hardif attributes to listener
640 * @bat_priv: the bat priv with all the soft interface information
641 * @hard_iface: hard interface which was modified
642 *
643 * Return: 0 on success, < 0 on error
644 */
645static int batadv_netlink_notify_hardif(struct batadv_priv *bat_priv,
646 struct batadv_hard_iface *hard_iface)
647{
648 struct sk_buff *msg;
649 int ret;
650
651 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
652 if (!msg)
653 return -ENOMEM;
654
655 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
656 BATADV_CMD_SET_HARDIF, 0, 0, 0, NULL);
657 if (ret < 0) {
658 nlmsg_free(msg);
659 return ret;
660 }
661
662 genlmsg_multicast_netns(&batadv_netlink_family,
663 dev_net(bat_priv->soft_iface), msg, 0,
664 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
665
666 return 0;
667}
668
669/**
670 * batadv_netlink_get_hardif() - Get hardif attributes
671 * @skb: Netlink message with request data
672 * @info: receiver information
673 *
674 * Return: 0 on success or negative error number in case of failure
675 */
676static int batadv_netlink_get_hardif(struct sk_buff *skb,
677 struct genl_info *info)
678{
679 struct batadv_hard_iface *hard_iface = info->user_ptr[1];
680 struct batadv_priv *bat_priv = info->user_ptr[0];
681 struct sk_buff *msg;
682 int ret;
683
684 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
685 if (!msg)
686 return -ENOMEM;
687
688 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
689 BATADV_CMD_GET_HARDIF,
690 info->snd_portid, info->snd_seq, 0,
691 NULL);
692 if (ret < 0) {
693 nlmsg_free(msg);
694 return ret;
695 }
696
697 ret = genlmsg_reply(msg, info);
698
699 return ret;
700}
701
702/**
703 * batadv_netlink_set_hardif() - Set hardif attributes
704 * @skb: Netlink message with request data
705 * @info: receiver information
706 *
707 * Return: 0 on success or negative error number in case of failure
708 */
709static int batadv_netlink_set_hardif(struct sk_buff *skb,
710 struct genl_info *info)
711{
712 struct batadv_hard_iface *hard_iface = info->user_ptr[1];
713 struct batadv_priv *bat_priv = info->user_ptr[0];
714
715 batadv_netlink_notify_hardif(bat_priv, hard_iface);
716
717 return 0;
718}
719
720/**
721 * batadv_netlink_dump_hardif() - Dump all hard interface into a messages
Matthias Schifferb60620c2016-07-03 13:31:36 +0200722 * @msg: Netlink message to dump into
723 * @cb: Parameters from query
724 *
725 * Return: error code, or length of reply message on success
726 */
727static int
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100728batadv_netlink_dump_hardif(struct sk_buff *msg, struct netlink_callback *cb)
Matthias Schifferb60620c2016-07-03 13:31:36 +0200729{
730 struct net *net = sock_net(cb->skb->sk);
731 struct net_device *soft_iface;
732 struct batadv_hard_iface *hard_iface;
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100733 struct batadv_priv *bat_priv;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200734 int ifindex;
735 int portid = NETLINK_CB(cb->skb).portid;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200736 int skip = cb->args[0];
737 int i = 0;
738
739 ifindex = batadv_netlink_get_ifindex(cb->nlh,
740 BATADV_ATTR_MESH_IFINDEX);
741 if (!ifindex)
742 return -EINVAL;
743
744 soft_iface = dev_get_by_index(net, ifindex);
745 if (!soft_iface)
746 return -ENODEV;
747
748 if (!batadv_softif_is_valid(soft_iface)) {
749 dev_put(soft_iface);
750 return -ENODEV;
751 }
752
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100753 bat_priv = netdev_priv(soft_iface);
754
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100755 rtnl_lock();
756 cb->seq = batadv_hardif_generation << 1 | 1;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200757
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100758 list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
Matthias Schifferb60620c2016-07-03 13:31:36 +0200759 if (hard_iface->soft_iface != soft_iface)
760 continue;
761
762 if (i++ < skip)
763 continue;
764
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100765 if (batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
766 BATADV_CMD_GET_HARDIF,
767 portid, cb->nlh->nlmsg_seq,
768 NLM_F_MULTI, cb)) {
Matthias Schifferb60620c2016-07-03 13:31:36 +0200769 i--;
770 break;
771 }
772 }
773
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100774 rtnl_unlock();
Matthias Schifferb60620c2016-07-03 13:31:36 +0200775
776 dev_put(soft_iface);
777
778 cb->args[0] = i;
779
780 return msg->len;
781}
782
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100783/**
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100784 * batadv_netlink_vlan_fill() - Fill message with vlan attributes
785 * @msg: Netlink message to dump into
786 * @bat_priv: the bat priv with all the soft interface information
787 * @vlan: vlan which was modified
788 * @cmd: type of message to generate
789 * @portid: Port making netlink request
790 * @seq: sequence number for message
791 * @flags: Additional flags for message
792 *
793 * Return: 0 on success or negative error number in case of failure
794 */
795static int batadv_netlink_vlan_fill(struct sk_buff *msg,
796 struct batadv_priv *bat_priv,
797 struct batadv_softif_vlan *vlan,
798 enum batadv_nl_commands cmd,
799 u32 portid, u32 seq, int flags)
800{
801 void *hdr;
802
803 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
804 if (!hdr)
805 return -ENOBUFS;
806
807 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
808 bat_priv->soft_iface->ifindex))
809 goto nla_put_failure;
810
811 if (nla_put_u32(msg, BATADV_ATTR_VLANID, vlan->vid & VLAN_VID_MASK))
812 goto nla_put_failure;
813
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100814 if (nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
815 !!atomic_read(&vlan->ap_isolation)))
816 goto nla_put_failure;
817
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100818 genlmsg_end(msg, hdr);
819 return 0;
820
821nla_put_failure:
822 genlmsg_cancel(msg, hdr);
823 return -EMSGSIZE;
824}
825
826/**
827 * batadv_netlink_notify_vlan() - send vlan attributes to listener
828 * @bat_priv: the bat priv with all the soft interface information
829 * @vlan: vlan which was modified
830 *
831 * Return: 0 on success, < 0 on error
832 */
833static int batadv_netlink_notify_vlan(struct batadv_priv *bat_priv,
834 struct batadv_softif_vlan *vlan)
835{
836 struct sk_buff *msg;
837 int ret;
838
839 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
840 if (!msg)
841 return -ENOMEM;
842
843 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan,
844 BATADV_CMD_SET_VLAN, 0, 0, 0);
845 if (ret < 0) {
846 nlmsg_free(msg);
847 return ret;
848 }
849
850 genlmsg_multicast_netns(&batadv_netlink_family,
851 dev_net(bat_priv->soft_iface), msg, 0,
852 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
853
854 return 0;
855}
856
857/**
858 * batadv_netlink_get_vlan() - Get vlan attributes
859 * @skb: Netlink message with request data
860 * @info: receiver information
861 *
862 * Return: 0 on success or negative error number in case of failure
863 */
864static int batadv_netlink_get_vlan(struct sk_buff *skb, struct genl_info *info)
865{
866 struct batadv_softif_vlan *vlan = info->user_ptr[1];
867 struct batadv_priv *bat_priv = info->user_ptr[0];
868 struct sk_buff *msg;
869 int ret;
870
871 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
872 if (!msg)
873 return -ENOMEM;
874
875 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan, BATADV_CMD_GET_VLAN,
876 info->snd_portid, info->snd_seq, 0);
877 if (ret < 0) {
878 nlmsg_free(msg);
879 return ret;
880 }
881
882 ret = genlmsg_reply(msg, info);
883
884 return ret;
885}
886
887/**
888 * batadv_netlink_set_vlan() - Get vlan attributes
889 * @skb: Netlink message with request data
890 * @info: receiver information
891 *
892 * Return: 0 on success or negative error number in case of failure
893 */
894static int batadv_netlink_set_vlan(struct sk_buff *skb, struct genl_info *info)
895{
896 struct batadv_softif_vlan *vlan = info->user_ptr[1];
897 struct batadv_priv *bat_priv = info->user_ptr[0];
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100898 struct nlattr *attr;
899
900 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
901 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
902
903 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
904 }
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100905
906 batadv_netlink_notify_vlan(bat_priv, vlan);
907
908 return 0;
909}
910
911/**
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100912 * batadv_get_softif_from_info() - Retrieve soft interface from genl attributes
913 * @net: the applicable net namespace
914 * @info: receiver information
915 *
916 * Return: Pointer to soft interface (with increased refcnt) on success, error
917 * pointer on error
918 */
919static struct net_device *
920batadv_get_softif_from_info(struct net *net, struct genl_info *info)
921{
922 struct net_device *soft_iface;
923 int ifindex;
924
925 if (!info->attrs[BATADV_ATTR_MESH_IFINDEX])
926 return ERR_PTR(-EINVAL);
927
928 ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
929
930 soft_iface = dev_get_by_index(net, ifindex);
931 if (!soft_iface)
932 return ERR_PTR(-ENODEV);
933
934 if (!batadv_softif_is_valid(soft_iface))
935 goto err_put_softif;
936
937 return soft_iface;
938
939err_put_softif:
940 dev_put(soft_iface);
941
942 return ERR_PTR(-EINVAL);
943}
944
945/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100946 * batadv_get_hardif_from_info() - Retrieve hardif from genl attributes
947 * @bat_priv: the bat priv with all the soft interface information
948 * @net: the applicable net namespace
949 * @info: receiver information
950 *
951 * Return: Pointer to hard interface (with increased refcnt) on success, error
952 * pointer on error
953 */
954static struct batadv_hard_iface *
955batadv_get_hardif_from_info(struct batadv_priv *bat_priv, struct net *net,
956 struct genl_info *info)
957{
958 struct batadv_hard_iface *hard_iface;
959 struct net_device *hard_dev;
960 unsigned int hardif_index;
961
962 if (!info->attrs[BATADV_ATTR_HARD_IFINDEX])
963 return ERR_PTR(-EINVAL);
964
965 hardif_index = nla_get_u32(info->attrs[BATADV_ATTR_HARD_IFINDEX]);
966
967 hard_dev = dev_get_by_index(net, hardif_index);
968 if (!hard_dev)
969 return ERR_PTR(-ENODEV);
970
971 hard_iface = batadv_hardif_get_by_netdev(hard_dev);
972 if (!hard_iface)
973 goto err_put_harddev;
974
975 if (hard_iface->soft_iface != bat_priv->soft_iface)
976 goto err_put_hardif;
977
978 /* hard_dev is referenced by hard_iface and not needed here */
979 dev_put(hard_dev);
980
981 return hard_iface;
982
983err_put_hardif:
984 batadv_hardif_put(hard_iface);
985err_put_harddev:
986 dev_put(hard_dev);
987
988 return ERR_PTR(-EINVAL);
989}
990
991/**
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100992 * batadv_get_vlan_from_info() - Retrieve vlan from genl attributes
993 * @bat_priv: the bat priv with all the soft interface information
994 * @net: the applicable net namespace
995 * @info: receiver information
996 *
997 * Return: Pointer to vlan on success (with increased refcnt), error pointer
998 * on error
999 */
1000static struct batadv_softif_vlan *
1001batadv_get_vlan_from_info(struct batadv_priv *bat_priv, struct net *net,
1002 struct genl_info *info)
1003{
1004 struct batadv_softif_vlan *vlan;
1005 u16 vid;
1006
1007 if (!info->attrs[BATADV_ATTR_VLANID])
1008 return ERR_PTR(-EINVAL);
1009
1010 vid = nla_get_u16(info->attrs[BATADV_ATTR_VLANID]);
1011
1012 vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
1013 if (!vlan)
1014 return ERR_PTR(-ENOENT);
1015
1016 return vlan;
1017}
1018
1019/**
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001020 * batadv_pre_doit() - Prepare batman-adv genl doit request
1021 * @ops: requested netlink operation
1022 * @skb: Netlink message with request data
1023 * @info: receiver information
1024 *
1025 * Return: 0 on success or negative error number in case of failure
1026 */
1027static int batadv_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
1028 struct genl_info *info)
1029{
1030 struct net *net = genl_info_net(info);
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001031 struct batadv_hard_iface *hard_iface;
1032 struct batadv_priv *bat_priv = NULL;
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001033 struct batadv_softif_vlan *vlan;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001034 struct net_device *soft_iface;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001035 u8 user_ptr1_flags;
1036 u8 mesh_dep_flags;
1037 int ret;
1038
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001039 user_ptr1_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001040 if (WARN_ON(hweight8(ops->internal_flags & user_ptr1_flags) > 1))
1041 return -EINVAL;
1042
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001043 mesh_dep_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001044 if (WARN_ON((ops->internal_flags & mesh_dep_flags) &&
1045 (~ops->internal_flags & BATADV_FLAG_NEED_MESH)))
1046 return -EINVAL;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001047
1048 if (ops->internal_flags & BATADV_FLAG_NEED_MESH) {
1049 soft_iface = batadv_get_softif_from_info(net, info);
1050 if (IS_ERR(soft_iface))
1051 return PTR_ERR(soft_iface);
1052
1053 bat_priv = netdev_priv(soft_iface);
1054 info->user_ptr[0] = bat_priv;
1055 }
1056
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001057 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF) {
1058 hard_iface = batadv_get_hardif_from_info(bat_priv, net, info);
1059 if (IS_ERR(hard_iface)) {
1060 ret = PTR_ERR(hard_iface);
1061 goto err_put_softif;
1062 }
1063
1064 info->user_ptr[1] = hard_iface;
1065 }
1066
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001067 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN) {
1068 vlan = batadv_get_vlan_from_info(bat_priv, net, info);
1069 if (IS_ERR(vlan)) {
1070 ret = PTR_ERR(vlan);
1071 goto err_put_softif;
1072 }
1073
1074 info->user_ptr[1] = vlan;
1075 }
1076
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001077 return 0;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001078
1079err_put_softif:
1080 if (bat_priv)
1081 dev_put(bat_priv->soft_iface);
1082
1083 return ret;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001084}
1085
1086/**
1087 * batadv_post_doit() - End batman-adv genl doit request
1088 * @ops: requested netlink operation
1089 * @skb: Netlink message with request data
1090 * @info: receiver information
1091 */
1092static void batadv_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
1093 struct genl_info *info)
1094{
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001095 struct batadv_hard_iface *hard_iface;
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001096 struct batadv_softif_vlan *vlan;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001097 struct batadv_priv *bat_priv;
1098
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001099 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF &&
1100 info->user_ptr[1]) {
1101 hard_iface = info->user_ptr[1];
1102
1103 batadv_hardif_put(hard_iface);
1104 }
1105
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001106 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN && info->user_ptr[1]) {
1107 vlan = info->user_ptr[1];
1108 batadv_softif_vlan_put(vlan);
1109 }
1110
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001111 if (ops->internal_flags & BATADV_FLAG_NEED_MESH && info->user_ptr[0]) {
1112 bat_priv = info->user_ptr[0];
1113 dev_put(bat_priv->soft_iface);
1114 }
1115}
1116
Sven Eckelmannce1a21d2016-10-18 15:57:36 +02001117static const struct genl_ops batadv_netlink_ops[] = {
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001118 {
Sven Eckelmann60040512018-11-23 12:14:56 +01001119 .cmd = BATADV_CMD_GET_MESH,
1120 /* can be retrieved by unprivileged users */
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001121 .policy = batadv_netlink_policy,
Sven Eckelmann60040512018-11-23 12:14:56 +01001122 .doit = batadv_netlink_get_mesh,
1123 .internal_flags = BATADV_FLAG_NEED_MESH,
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001124 },
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001125 {
1126 .cmd = BATADV_CMD_TP_METER,
1127 .flags = GENL_ADMIN_PERM,
1128 .policy = batadv_netlink_policy,
1129 .doit = batadv_netlink_tp_meter_start,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001130 .internal_flags = BATADV_FLAG_NEED_MESH,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001131 },
1132 {
1133 .cmd = BATADV_CMD_TP_METER_CANCEL,
1134 .flags = GENL_ADMIN_PERM,
1135 .policy = batadv_netlink_policy,
1136 .doit = batadv_netlink_tp_meter_cancel,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001137 .internal_flags = BATADV_FLAG_NEED_MESH,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001138 },
Matthias Schiffer07a30612016-07-03 13:31:35 +02001139 {
1140 .cmd = BATADV_CMD_GET_ROUTING_ALGOS,
1141 .flags = GENL_ADMIN_PERM,
1142 .policy = batadv_netlink_policy,
1143 .dumpit = batadv_algo_dump,
1144 },
Matthias Schifferb60620c2016-07-03 13:31:36 +02001145 {
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001146 .cmd = BATADV_CMD_GET_HARDIF,
1147 /* can be retrieved by unprivileged users */
Matthias Schifferb60620c2016-07-03 13:31:36 +02001148 .policy = batadv_netlink_policy,
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001149 .dumpit = batadv_netlink_dump_hardif,
1150 .doit = batadv_netlink_get_hardif,
1151 .internal_flags = BATADV_FLAG_NEED_MESH |
1152 BATADV_FLAG_NEED_HARDIF,
Matthias Schifferb60620c2016-07-03 13:31:36 +02001153 },
Matthias Schifferd34f0552016-07-03 13:31:37 +02001154 {
1155 .cmd = BATADV_CMD_GET_TRANSTABLE_LOCAL,
1156 .flags = GENL_ADMIN_PERM,
1157 .policy = batadv_netlink_policy,
1158 .dumpit = batadv_tt_local_dump,
1159 },
1160 {
1161 .cmd = BATADV_CMD_GET_TRANSTABLE_GLOBAL,
1162 .flags = GENL_ADMIN_PERM,
1163 .policy = batadv_netlink_policy,
1164 .dumpit = batadv_tt_global_dump,
1165 },
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001166 {
1167 .cmd = BATADV_CMD_GET_ORIGINATORS,
1168 .flags = GENL_ADMIN_PERM,
1169 .policy = batadv_netlink_policy,
1170 .dumpit = batadv_orig_dump,
1171 },
1172 {
1173 .cmd = BATADV_CMD_GET_NEIGHBORS,
1174 .flags = GENL_ADMIN_PERM,
1175 .policy = batadv_netlink_policy,
1176 .dumpit = batadv_hardif_neigh_dump,
1177 },
Sven Eckelmannd7129da2016-07-03 13:31:42 +02001178 {
1179 .cmd = BATADV_CMD_GET_GATEWAYS,
1180 .flags = GENL_ADMIN_PERM,
1181 .policy = batadv_netlink_policy,
1182 .dumpit = batadv_gw_dump,
1183 },
Andrew Lunn04f3f5b2016-07-03 13:31:45 +02001184 {
1185 .cmd = BATADV_CMD_GET_BLA_CLAIM,
1186 .flags = GENL_ADMIN_PERM,
1187 .policy = batadv_netlink_policy,
1188 .dumpit = batadv_bla_claim_dump,
1189 },
Simon Wunderlichea4152e2016-07-03 13:31:47 +02001190 {
1191 .cmd = BATADV_CMD_GET_BLA_BACKBONE,
1192 .flags = GENL_ADMIN_PERM,
1193 .policy = batadv_netlink_policy,
1194 .dumpit = batadv_bla_backbone_dump,
1195 },
Linus Lüssing41aeefc2018-03-13 11:41:12 +01001196 {
1197 .cmd = BATADV_CMD_GET_DAT_CACHE,
1198 .flags = GENL_ADMIN_PERM,
1199 .policy = batadv_netlink_policy,
1200 .dumpit = batadv_dat_cache_dump,
1201 },
Linus Lüssing53dd9a62018-03-13 11:41:13 +01001202 {
1203 .cmd = BATADV_CMD_GET_MCAST_FLAGS,
1204 .flags = GENL_ADMIN_PERM,
1205 .policy = batadv_netlink_policy,
1206 .dumpit = batadv_mcast_flags_dump,
1207 },
Sven Eckelmann60040512018-11-23 12:14:56 +01001208 {
1209 .cmd = BATADV_CMD_SET_MESH,
1210 .flags = GENL_ADMIN_PERM,
1211 .policy = batadv_netlink_policy,
1212 .doit = batadv_netlink_set_mesh,
1213 .internal_flags = BATADV_FLAG_NEED_MESH,
1214 },
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001215 {
1216 .cmd = BATADV_CMD_SET_HARDIF,
1217 .flags = GENL_ADMIN_PERM,
1218 .policy = batadv_netlink_policy,
1219 .doit = batadv_netlink_set_hardif,
1220 .internal_flags = BATADV_FLAG_NEED_MESH |
1221 BATADV_FLAG_NEED_HARDIF,
1222 },
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001223 {
1224 .cmd = BATADV_CMD_GET_VLAN,
1225 /* can be retrieved by unprivileged users */
1226 .policy = batadv_netlink_policy,
1227 .doit = batadv_netlink_get_vlan,
1228 .internal_flags = BATADV_FLAG_NEED_MESH |
1229 BATADV_FLAG_NEED_VLAN,
1230 },
1231 {
1232 .cmd = BATADV_CMD_SET_VLAN,
1233 .flags = GENL_ADMIN_PERM,
1234 .policy = batadv_netlink_policy,
1235 .doit = batadv_netlink_set_vlan,
1236 .internal_flags = BATADV_FLAG_NEED_MESH |
1237 BATADV_FLAG_NEED_VLAN,
1238 },
Matthias Schiffer09748a22016-05-09 18:41:08 +02001239};
1240
Johannes Berg56989f62016-10-24 14:40:05 +02001241struct genl_family batadv_netlink_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001242 .hdrsize = 0,
1243 .name = BATADV_NL_NAME,
1244 .version = 1,
1245 .maxattr = BATADV_ATTR_MAX,
1246 .netnsok = true,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001247 .pre_doit = batadv_pre_doit,
1248 .post_doit = batadv_post_doit,
Johannes Berg489111e2016-10-24 14:40:03 +02001249 .module = THIS_MODULE,
1250 .ops = batadv_netlink_ops,
1251 .n_ops = ARRAY_SIZE(batadv_netlink_ops),
1252 .mcgrps = batadv_netlink_mcgrps,
1253 .n_mcgrps = ARRAY_SIZE(batadv_netlink_mcgrps),
1254};
1255
Matthias Schiffer09748a22016-05-09 18:41:08 +02001256/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001257 * batadv_netlink_register() - register batadv genl netlink family
Matthias Schiffer09748a22016-05-09 18:41:08 +02001258 */
1259void __init batadv_netlink_register(void)
1260{
1261 int ret;
1262
Johannes Berg489111e2016-10-24 14:40:03 +02001263 ret = genl_register_family(&batadv_netlink_family);
Matthias Schiffer09748a22016-05-09 18:41:08 +02001264 if (ret)
1265 pr_warn("unable to register netlink family");
1266}
1267
1268/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001269 * batadv_netlink_unregister() - unregister batadv genl netlink family
Matthias Schiffer09748a22016-05-09 18:41:08 +02001270 */
1271void batadv_netlink_unregister(void)
1272{
1273 genl_unregister_family(&batadv_netlink_family);
1274}