blob: 3028c2a5c782d9107bf90fdfa7db180a41ba43fc [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 },
Sven Eckelmanna1c8de82018-11-23 13:06:42 +0100148 [BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED] = { .type = NLA_U8 },
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200149};
150
151/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100152 * batadv_netlink_get_ifindex() - Extract an interface index from a message
Matthias Schifferb60620c2016-07-03 13:31:36 +0200153 * @nlh: Message header
154 * @attrtype: Attribute which holds an interface index
155 *
156 * Return: interface index, or 0.
157 */
Matthias Schifferd34f0552016-07-03 13:31:37 +0200158int
Matthias Schifferb60620c2016-07-03 13:31:36 +0200159batadv_netlink_get_ifindex(const struct nlmsghdr *nlh, int attrtype)
160{
161 struct nlattr *attr = nlmsg_find_attr(nlh, GENL_HDRLEN, attrtype);
162
163 return attr ? nla_get_u32(attr) : 0;
164}
165
166/**
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100167 * batadv_netlink_mesh_fill_ap_isolation() - Add ap_isolation softif attribute
168 * @msg: Netlink message to dump into
169 * @bat_priv: the bat priv with all the soft interface information
170 *
171 * Return: 0 on success or negative error number in case of failure
172 */
173static int batadv_netlink_mesh_fill_ap_isolation(struct sk_buff *msg,
174 struct batadv_priv *bat_priv)
175{
176 struct batadv_softif_vlan *vlan;
177 u8 ap_isolation;
178
179 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
180 if (!vlan)
181 return 0;
182
183 ap_isolation = atomic_read(&vlan->ap_isolation);
184 batadv_softif_vlan_put(vlan);
185
186 return nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
187 !!ap_isolation);
188}
189
190/**
191 * batadv_option_set_ap_isolation() - Set ap_isolation from genl msg
192 * @attr: parsed BATADV_ATTR_AP_ISOLATION_ENABLED attribute
193 * @bat_priv: the bat priv with all the soft interface information
194 *
195 * Return: 0 on success or negative error number in case of failure
196 */
197static int batadv_netlink_set_mesh_ap_isolation(struct nlattr *attr,
198 struct batadv_priv *bat_priv)
199{
200 struct batadv_softif_vlan *vlan;
201
202 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
203 if (!vlan)
204 return -ENOENT;
205
206 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
207 batadv_softif_vlan_put(vlan);
208
209 return 0;
210}
211
212/**
Sven Eckelmann60040512018-11-23 12:14:56 +0100213 * batadv_netlink_mesh_fill() - Fill message with mesh attributes
214 * @msg: Netlink message to dump into
215 * @bat_priv: the bat priv with all the soft interface information
216 * @cmd: type of message to generate
217 * @portid: Port making netlink request
218 * @seq: sequence number for message
219 * @flags: Additional flags for message
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200220 *
Sven Eckelmann60040512018-11-23 12:14:56 +0100221 * Return: 0 on success or negative error number in case of failure
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200222 */
Sven Eckelmann60040512018-11-23 12:14:56 +0100223static int batadv_netlink_mesh_fill(struct sk_buff *msg,
224 struct batadv_priv *bat_priv,
225 enum batadv_nl_commands cmd,
226 u32 portid, u32 seq, int flags)
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200227{
Sven Eckelmann60040512018-11-23 12:14:56 +0100228 struct net_device *soft_iface = bat_priv->soft_iface;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200229 struct batadv_hard_iface *primary_if = NULL;
230 struct net_device *hard_iface;
Sven Eckelmann60040512018-11-23 12:14:56 +0100231 void *hdr;
232
233 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
234 if (!hdr)
235 return -ENOBUFS;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200236
237 if (nla_put_string(msg, BATADV_ATTR_VERSION, BATADV_SOURCE_VERSION) ||
238 nla_put_string(msg, BATADV_ATTR_ALGO_NAME,
Antonio Quartulli29824a52016-05-25 23:27:31 +0800239 bat_priv->algo_ops->name) ||
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200240 nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, soft_iface->ifindex) ||
241 nla_put_string(msg, BATADV_ATTR_MESH_IFNAME, soft_iface->name) ||
242 nla_put(msg, BATADV_ATTR_MESH_ADDRESS, ETH_ALEN,
Sven Eckelmannf32ed4b2016-07-03 13:31:38 +0200243 soft_iface->dev_addr) ||
244 nla_put_u8(msg, BATADV_ATTR_TT_TTVN,
245 (u8)atomic_read(&bat_priv->tt.vn)))
Sven Eckelmann60040512018-11-23 12:14:56 +0100246 goto nla_put_failure;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200247
Sven Eckelmann8dad6f0d2016-07-03 13:31:46 +0200248#ifdef CONFIG_BATMAN_ADV_BLA
249 if (nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
250 ntohs(bat_priv->bla.claim_dest.group)))
Sven Eckelmann60040512018-11-23 12:14:56 +0100251 goto nla_put_failure;
Sven Eckelmann8dad6f0d2016-07-03 13:31:46 +0200252#endif
253
Linus Lüssing53dd9a62018-03-13 11:41:13 +0100254 if (batadv_mcast_mesh_info_put(msg, bat_priv))
Sven Eckelmann60040512018-11-23 12:14:56 +0100255 goto nla_put_failure;
Linus Lüssing53dd9a62018-03-13 11:41:13 +0100256
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200257 primary_if = batadv_primary_if_get_selected(bat_priv);
258 if (primary_if && primary_if->if_status == BATADV_IF_ACTIVE) {
259 hard_iface = primary_if->net_dev;
260
261 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
262 hard_iface->ifindex) ||
263 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
264 hard_iface->name) ||
265 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
266 hard_iface->dev_addr))
Sven Eckelmann60040512018-11-23 12:14:56 +0100267 goto nla_put_failure;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200268 }
269
Sven Eckelmann9ab4cee2018-11-23 12:46:14 +0100270 if (nla_put_u8(msg, BATADV_ATTR_AGGREGATED_OGMS_ENABLED,
271 !!atomic_read(&bat_priv->aggregated_ogms)))
272 goto nla_put_failure;
273
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100274 if (batadv_netlink_mesh_fill_ap_isolation(msg, bat_priv))
275 goto nla_put_failure;
276
277 if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MARK,
278 bat_priv->isolation_mark))
279 goto nla_put_failure;
280
281 if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MASK,
282 bat_priv->isolation_mark_mask))
283 goto nla_put_failure;
284
Sven Eckelmannd7e52502018-11-23 12:55:44 +0100285 if (nla_put_u8(msg, BATADV_ATTR_BONDING_ENABLED,
286 !!atomic_read(&bat_priv->bonding)))
287 goto nla_put_failure;
288
Sven Eckelmann43ff6102018-11-23 13:03:39 +0100289#ifdef CONFIG_BATMAN_ADV_BLA
290 if (nla_put_u8(msg, BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED,
291 !!atomic_read(&bat_priv->bridge_loop_avoidance)))
292 goto nla_put_failure;
293#endif /* CONFIG_BATMAN_ADV_BLA */
294
Sven Eckelmanna1c8de82018-11-23 13:06:42 +0100295#ifdef CONFIG_BATMAN_ADV_DAT
296 if (nla_put_u8(msg, BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED,
297 !!atomic_read(&bat_priv->distributed_arp_table)))
298 goto nla_put_failure;
299#endif /* CONFIG_BATMAN_ADV_DAT */
300
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200301 if (primary_if)
302 batadv_hardif_put(primary_if);
303
Sven Eckelmann60040512018-11-23 12:14:56 +0100304 genlmsg_end(msg, hdr);
305 return 0;
306
307nla_put_failure:
308 if (primary_if)
309 batadv_hardif_put(primary_if);
310
311 genlmsg_cancel(msg, hdr);
312 return -EMSGSIZE;
313}
314
315/**
316 * batadv_netlink_notify_mesh() - send softif attributes to listener
317 * @bat_priv: the bat priv with all the soft interface information
318 *
319 * Return: 0 on success, < 0 on error
320 */
321static int batadv_netlink_notify_mesh(struct batadv_priv *bat_priv)
322{
323 struct sk_buff *msg;
324 int ret;
325
326 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
327 if (!msg)
328 return -ENOMEM;
329
330 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_SET_MESH,
331 0, 0, 0);
332 if (ret < 0) {
333 nlmsg_free(msg);
334 return ret;
335 }
336
337 genlmsg_multicast_netns(&batadv_netlink_family,
338 dev_net(bat_priv->soft_iface), msg, 0,
339 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
340
341 return 0;
342}
343
344/**
345 * batadv_netlink_get_mesh() - Get softif attributes
346 * @skb: Netlink message with request data
347 * @info: receiver information
348 *
349 * Return: 0 on success or negative error number in case of failure
350 */
351static int batadv_netlink_get_mesh(struct sk_buff *skb, struct genl_info *info)
352{
353 struct batadv_priv *bat_priv = info->user_ptr[0];
354 struct sk_buff *msg;
355 int ret;
356
357 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
358 if (!msg)
359 return -ENOMEM;
360
361 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_GET_MESH,
362 info->snd_portid, info->snd_seq, 0);
363 if (ret < 0) {
364 nlmsg_free(msg);
365 return ret;
366 }
367
368 ret = genlmsg_reply(msg, info);
369
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200370 return ret;
371}
372
373/**
Sven Eckelmann60040512018-11-23 12:14:56 +0100374 * batadv_netlink_set_mesh() - Set softif attributes
375 * @skb: Netlink message with request data
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200376 * @info: receiver information
377 *
Sven Eckelmann60040512018-11-23 12:14:56 +0100378 * Return: 0 on success or negative error number in case of failure
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200379 */
Sven Eckelmann60040512018-11-23 12:14:56 +0100380static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info)
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200381{
Sven Eckelmann60040512018-11-23 12:14:56 +0100382 struct batadv_priv *bat_priv = info->user_ptr[0];
Sven Eckelmann9ab4cee2018-11-23 12:46:14 +0100383 struct nlattr *attr;
384
385 if (info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED]) {
386 attr = info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED];
387
388 atomic_set(&bat_priv->aggregated_ogms, !!nla_get_u8(attr));
389 }
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200390
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100391 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
392 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
393
394 batadv_netlink_set_mesh_ap_isolation(attr, bat_priv);
395 }
396
397 if (info->attrs[BATADV_ATTR_ISOLATION_MARK]) {
398 attr = info->attrs[BATADV_ATTR_ISOLATION_MARK];
399
400 bat_priv->isolation_mark = nla_get_u32(attr);
401 }
402
403 if (info->attrs[BATADV_ATTR_ISOLATION_MASK]) {
404 attr = info->attrs[BATADV_ATTR_ISOLATION_MASK];
405
406 bat_priv->isolation_mark_mask = nla_get_u32(attr);
407 }
408
Sven Eckelmannd7e52502018-11-23 12:55:44 +0100409 if (info->attrs[BATADV_ATTR_BONDING_ENABLED]) {
410 attr = info->attrs[BATADV_ATTR_BONDING_ENABLED];
411
412 atomic_set(&bat_priv->bonding, !!nla_get_u8(attr));
413 }
414
Sven Eckelmann43ff6102018-11-23 13:03:39 +0100415#ifdef CONFIG_BATMAN_ADV_BLA
416 if (info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED]) {
417 attr = info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED];
418
419 atomic_set(&bat_priv->bridge_loop_avoidance,
420 !!nla_get_u8(attr));
421 batadv_bla_status_update(bat_priv->soft_iface);
422 }
423#endif /* CONFIG_BATMAN_ADV_BLA */
424
Sven Eckelmanna1c8de82018-11-23 13:06:42 +0100425#ifdef CONFIG_BATMAN_ADV_DAT
426 if (info->attrs[BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED]) {
427 attr = info->attrs[BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED];
428
429 atomic_set(&bat_priv->distributed_arp_table,
430 !!nla_get_u8(attr));
431 batadv_dat_status_update(bat_priv->soft_iface);
432 }
433#endif /* CONFIG_BATMAN_ADV_DAT */
434
Sven Eckelmann60040512018-11-23 12:14:56 +0100435 batadv_netlink_notify_mesh(bat_priv);
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200436
Sven Eckelmann60040512018-11-23 12:14:56 +0100437 return 0;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200438}
439
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200440/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100441 * batadv_netlink_tp_meter_put() - Fill information of started tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200442 * @msg: netlink message to be sent back
443 * @cookie: tp meter session cookie
444 *
445 * Return: 0 on success, < 0 on error
446 */
447static int
448batadv_netlink_tp_meter_put(struct sk_buff *msg, u32 cookie)
449{
450 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
451 return -ENOBUFS;
452
453 return 0;
454}
455
456/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100457 * batadv_netlink_tpmeter_notify() - send tp_meter result via netlink to client
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200458 * @bat_priv: the bat priv with all the soft interface information
459 * @dst: destination of tp_meter session
460 * @result: reason for tp meter session stop
461 * @test_time: total time ot the tp_meter session
462 * @total_bytes: bytes acked to the receiver
463 * @cookie: cookie of tp_meter session
464 *
465 * Return: 0 on success, < 0 on error
466 */
467int batadv_netlink_tpmeter_notify(struct batadv_priv *bat_priv, const u8 *dst,
468 u8 result, u32 test_time, u64 total_bytes,
469 u32 cookie)
470{
471 struct sk_buff *msg;
472 void *hdr;
473 int ret;
474
475 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
476 if (!msg)
477 return -ENOMEM;
478
479 hdr = genlmsg_put(msg, 0, 0, &batadv_netlink_family, 0,
480 BATADV_CMD_TP_METER);
481 if (!hdr) {
482 ret = -ENOBUFS;
483 goto err_genlmsg;
484 }
485
486 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
487 goto nla_put_failure;
488
489 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, test_time))
490 goto nla_put_failure;
491
492 if (nla_put_u64_64bit(msg, BATADV_ATTR_TPMETER_BYTES, total_bytes,
493 BATADV_ATTR_PAD))
494 goto nla_put_failure;
495
496 if (nla_put_u8(msg, BATADV_ATTR_TPMETER_RESULT, result))
497 goto nla_put_failure;
498
499 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst))
500 goto nla_put_failure;
501
502 genlmsg_end(msg, hdr);
503
504 genlmsg_multicast_netns(&batadv_netlink_family,
505 dev_net(bat_priv->soft_iface), msg, 0,
506 BATADV_NL_MCGRP_TPMETER, GFP_KERNEL);
507
508 return 0;
509
510nla_put_failure:
511 genlmsg_cancel(msg, hdr);
512 ret = -EMSGSIZE;
513
514err_genlmsg:
515 nlmsg_free(msg);
516 return ret;
517}
518
519/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100520 * batadv_netlink_tp_meter_start() - Start a new tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200521 * @skb: received netlink message
522 * @info: receiver information
523 *
524 * Return: 0 on success, < 0 on error
525 */
526static int
527batadv_netlink_tp_meter_start(struct sk_buff *skb, struct genl_info *info)
528{
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100529 struct batadv_priv *bat_priv = info->user_ptr[0];
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200530 struct sk_buff *msg = NULL;
531 u32 test_length;
532 void *msg_head;
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200533 u32 cookie;
534 u8 *dst;
535 int ret;
536
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200537 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
538 return -EINVAL;
539
540 if (!info->attrs[BATADV_ATTR_TPMETER_TEST_TIME])
541 return -EINVAL;
542
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200543 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
544
545 test_length = nla_get_u32(info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]);
546
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200547 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
548 if (!msg) {
549 ret = -ENOMEM;
550 goto out;
551 }
552
553 msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq,
554 &batadv_netlink_family, 0,
555 BATADV_CMD_TP_METER);
556 if (!msg_head) {
557 ret = -ENOBUFS;
558 goto out;
559 }
560
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200561 batadv_tp_start(bat_priv, dst, test_length, &cookie);
562
563 ret = batadv_netlink_tp_meter_put(msg, cookie);
564
565 out:
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200566 if (ret) {
567 if (msg)
568 nlmsg_free(msg);
569 return ret;
570 }
571
572 genlmsg_end(msg, msg_head);
573 return genlmsg_reply(msg, info);
574}
575
576/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100577 * batadv_netlink_tp_meter_start() - Cancel a running tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200578 * @skb: received netlink message
579 * @info: receiver information
580 *
581 * Return: 0 on success, < 0 on error
582 */
583static int
584batadv_netlink_tp_meter_cancel(struct sk_buff *skb, struct genl_info *info)
585{
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100586 struct batadv_priv *bat_priv = info->user_ptr[0];
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200587 u8 *dst;
588 int ret = 0;
589
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200590 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
591 return -EINVAL;
592
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200593 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
594
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200595 batadv_tp_stop(bat_priv, dst, BATADV_TP_REASON_CANCEL);
596
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200597 return ret;
598}
599
Matthias Schifferb60620c2016-07-03 13:31:36 +0200600/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100601 * batadv_netlink_hardif_fill() - Fill message with hardif attributes
Matthias Schifferb60620c2016-07-03 13:31:36 +0200602 * @msg: Netlink message to dump into
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100603 * @bat_priv: the bat priv with all the soft interface information
604 * @hard_iface: hard interface which was modified
605 * @cmd: type of message to generate
Matthias Schifferb60620c2016-07-03 13:31:36 +0200606 * @portid: Port making netlink request
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100607 * @seq: sequence number for message
608 * @flags: Additional flags for message
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100609 * @cb: Control block containing additional options
Matthias Schifferb60620c2016-07-03 13:31:36 +0200610 *
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100611 * Return: 0 on success or negative error number in case of failure
Matthias Schifferb60620c2016-07-03 13:31:36 +0200612 */
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100613static int batadv_netlink_hardif_fill(struct sk_buff *msg,
614 struct batadv_priv *bat_priv,
615 struct batadv_hard_iface *hard_iface,
616 enum batadv_nl_commands cmd,
617 u32 portid, u32 seq, int flags,
618 struct netlink_callback *cb)
Matthias Schifferb60620c2016-07-03 13:31:36 +0200619{
620 struct net_device *net_dev = hard_iface->net_dev;
621 void *hdr;
622
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100623 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
Matthias Schifferb60620c2016-07-03 13:31:36 +0200624 if (!hdr)
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100625 return -ENOBUFS;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200626
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100627 if (cb)
628 genl_dump_check_consistent(cb, hdr);
629
630 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
631 bat_priv->soft_iface->ifindex))
632 goto nla_put_failure;
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100633
Matthias Schifferb60620c2016-07-03 13:31:36 +0200634 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
635 net_dev->ifindex) ||
636 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
637 net_dev->name) ||
638 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
639 net_dev->dev_addr))
640 goto nla_put_failure;
641
642 if (hard_iface->if_status == BATADV_IF_ACTIVE) {
643 if (nla_put_flag(msg, BATADV_ATTR_ACTIVE))
644 goto nla_put_failure;
645 }
646
647 genlmsg_end(msg, hdr);
648 return 0;
649
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100650nla_put_failure:
Matthias Schifferb60620c2016-07-03 13:31:36 +0200651 genlmsg_cancel(msg, hdr);
652 return -EMSGSIZE;
653}
654
655/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100656 * batadv_netlink_notify_hardif() - send hardif attributes to listener
657 * @bat_priv: the bat priv with all the soft interface information
658 * @hard_iface: hard interface which was modified
659 *
660 * Return: 0 on success, < 0 on error
661 */
662static int batadv_netlink_notify_hardif(struct batadv_priv *bat_priv,
663 struct batadv_hard_iface *hard_iface)
664{
665 struct sk_buff *msg;
666 int ret;
667
668 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
669 if (!msg)
670 return -ENOMEM;
671
672 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
673 BATADV_CMD_SET_HARDIF, 0, 0, 0, NULL);
674 if (ret < 0) {
675 nlmsg_free(msg);
676 return ret;
677 }
678
679 genlmsg_multicast_netns(&batadv_netlink_family,
680 dev_net(bat_priv->soft_iface), msg, 0,
681 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
682
683 return 0;
684}
685
686/**
687 * batadv_netlink_get_hardif() - Get hardif attributes
688 * @skb: Netlink message with request data
689 * @info: receiver information
690 *
691 * Return: 0 on success or negative error number in case of failure
692 */
693static int batadv_netlink_get_hardif(struct sk_buff *skb,
694 struct genl_info *info)
695{
696 struct batadv_hard_iface *hard_iface = info->user_ptr[1];
697 struct batadv_priv *bat_priv = info->user_ptr[0];
698 struct sk_buff *msg;
699 int ret;
700
701 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
702 if (!msg)
703 return -ENOMEM;
704
705 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
706 BATADV_CMD_GET_HARDIF,
707 info->snd_portid, info->snd_seq, 0,
708 NULL);
709 if (ret < 0) {
710 nlmsg_free(msg);
711 return ret;
712 }
713
714 ret = genlmsg_reply(msg, info);
715
716 return ret;
717}
718
719/**
720 * batadv_netlink_set_hardif() - Set hardif attributes
721 * @skb: Netlink message with request data
722 * @info: receiver information
723 *
724 * Return: 0 on success or negative error number in case of failure
725 */
726static int batadv_netlink_set_hardif(struct sk_buff *skb,
727 struct genl_info *info)
728{
729 struct batadv_hard_iface *hard_iface = info->user_ptr[1];
730 struct batadv_priv *bat_priv = info->user_ptr[0];
731
732 batadv_netlink_notify_hardif(bat_priv, hard_iface);
733
734 return 0;
735}
736
737/**
738 * batadv_netlink_dump_hardif() - Dump all hard interface into a messages
Matthias Schifferb60620c2016-07-03 13:31:36 +0200739 * @msg: Netlink message to dump into
740 * @cb: Parameters from query
741 *
742 * Return: error code, or length of reply message on success
743 */
744static int
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100745batadv_netlink_dump_hardif(struct sk_buff *msg, struct netlink_callback *cb)
Matthias Schifferb60620c2016-07-03 13:31:36 +0200746{
747 struct net *net = sock_net(cb->skb->sk);
748 struct net_device *soft_iface;
749 struct batadv_hard_iface *hard_iface;
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100750 struct batadv_priv *bat_priv;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200751 int ifindex;
752 int portid = NETLINK_CB(cb->skb).portid;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200753 int skip = cb->args[0];
754 int i = 0;
755
756 ifindex = batadv_netlink_get_ifindex(cb->nlh,
757 BATADV_ATTR_MESH_IFINDEX);
758 if (!ifindex)
759 return -EINVAL;
760
761 soft_iface = dev_get_by_index(net, ifindex);
762 if (!soft_iface)
763 return -ENODEV;
764
765 if (!batadv_softif_is_valid(soft_iface)) {
766 dev_put(soft_iface);
767 return -ENODEV;
768 }
769
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100770 bat_priv = netdev_priv(soft_iface);
771
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100772 rtnl_lock();
773 cb->seq = batadv_hardif_generation << 1 | 1;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200774
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100775 list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
Matthias Schifferb60620c2016-07-03 13:31:36 +0200776 if (hard_iface->soft_iface != soft_iface)
777 continue;
778
779 if (i++ < skip)
780 continue;
781
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100782 if (batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
783 BATADV_CMD_GET_HARDIF,
784 portid, cb->nlh->nlmsg_seq,
785 NLM_F_MULTI, cb)) {
Matthias Schifferb60620c2016-07-03 13:31:36 +0200786 i--;
787 break;
788 }
789 }
790
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100791 rtnl_unlock();
Matthias Schifferb60620c2016-07-03 13:31:36 +0200792
793 dev_put(soft_iface);
794
795 cb->args[0] = i;
796
797 return msg->len;
798}
799
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100800/**
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100801 * batadv_netlink_vlan_fill() - Fill message with vlan attributes
802 * @msg: Netlink message to dump into
803 * @bat_priv: the bat priv with all the soft interface information
804 * @vlan: vlan which was modified
805 * @cmd: type of message to generate
806 * @portid: Port making netlink request
807 * @seq: sequence number for message
808 * @flags: Additional flags for message
809 *
810 * Return: 0 on success or negative error number in case of failure
811 */
812static int batadv_netlink_vlan_fill(struct sk_buff *msg,
813 struct batadv_priv *bat_priv,
814 struct batadv_softif_vlan *vlan,
815 enum batadv_nl_commands cmd,
816 u32 portid, u32 seq, int flags)
817{
818 void *hdr;
819
820 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
821 if (!hdr)
822 return -ENOBUFS;
823
824 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
825 bat_priv->soft_iface->ifindex))
826 goto nla_put_failure;
827
828 if (nla_put_u32(msg, BATADV_ATTR_VLANID, vlan->vid & VLAN_VID_MASK))
829 goto nla_put_failure;
830
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100831 if (nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
832 !!atomic_read(&vlan->ap_isolation)))
833 goto nla_put_failure;
834
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100835 genlmsg_end(msg, hdr);
836 return 0;
837
838nla_put_failure:
839 genlmsg_cancel(msg, hdr);
840 return -EMSGSIZE;
841}
842
843/**
844 * batadv_netlink_notify_vlan() - send vlan attributes to listener
845 * @bat_priv: the bat priv with all the soft interface information
846 * @vlan: vlan which was modified
847 *
848 * Return: 0 on success, < 0 on error
849 */
850static int batadv_netlink_notify_vlan(struct batadv_priv *bat_priv,
851 struct batadv_softif_vlan *vlan)
852{
853 struct sk_buff *msg;
854 int ret;
855
856 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
857 if (!msg)
858 return -ENOMEM;
859
860 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan,
861 BATADV_CMD_SET_VLAN, 0, 0, 0);
862 if (ret < 0) {
863 nlmsg_free(msg);
864 return ret;
865 }
866
867 genlmsg_multicast_netns(&batadv_netlink_family,
868 dev_net(bat_priv->soft_iface), msg, 0,
869 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
870
871 return 0;
872}
873
874/**
875 * batadv_netlink_get_vlan() - Get vlan attributes
876 * @skb: Netlink message with request data
877 * @info: receiver information
878 *
879 * Return: 0 on success or negative error number in case of failure
880 */
881static int batadv_netlink_get_vlan(struct sk_buff *skb, struct genl_info *info)
882{
883 struct batadv_softif_vlan *vlan = info->user_ptr[1];
884 struct batadv_priv *bat_priv = info->user_ptr[0];
885 struct sk_buff *msg;
886 int ret;
887
888 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
889 if (!msg)
890 return -ENOMEM;
891
892 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan, BATADV_CMD_GET_VLAN,
893 info->snd_portid, info->snd_seq, 0);
894 if (ret < 0) {
895 nlmsg_free(msg);
896 return ret;
897 }
898
899 ret = genlmsg_reply(msg, info);
900
901 return ret;
902}
903
904/**
905 * batadv_netlink_set_vlan() - Get vlan attributes
906 * @skb: Netlink message with request data
907 * @info: receiver information
908 *
909 * Return: 0 on success or negative error number in case of failure
910 */
911static int batadv_netlink_set_vlan(struct sk_buff *skb, struct genl_info *info)
912{
913 struct batadv_softif_vlan *vlan = info->user_ptr[1];
914 struct batadv_priv *bat_priv = info->user_ptr[0];
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100915 struct nlattr *attr;
916
917 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
918 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
919
920 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
921 }
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100922
923 batadv_netlink_notify_vlan(bat_priv, vlan);
924
925 return 0;
926}
927
928/**
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100929 * batadv_get_softif_from_info() - Retrieve soft interface from genl attributes
930 * @net: the applicable net namespace
931 * @info: receiver information
932 *
933 * Return: Pointer to soft interface (with increased refcnt) on success, error
934 * pointer on error
935 */
936static struct net_device *
937batadv_get_softif_from_info(struct net *net, struct genl_info *info)
938{
939 struct net_device *soft_iface;
940 int ifindex;
941
942 if (!info->attrs[BATADV_ATTR_MESH_IFINDEX])
943 return ERR_PTR(-EINVAL);
944
945 ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
946
947 soft_iface = dev_get_by_index(net, ifindex);
948 if (!soft_iface)
949 return ERR_PTR(-ENODEV);
950
951 if (!batadv_softif_is_valid(soft_iface))
952 goto err_put_softif;
953
954 return soft_iface;
955
956err_put_softif:
957 dev_put(soft_iface);
958
959 return ERR_PTR(-EINVAL);
960}
961
962/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100963 * batadv_get_hardif_from_info() - Retrieve hardif from genl attributes
964 * @bat_priv: the bat priv with all the soft interface information
965 * @net: the applicable net namespace
966 * @info: receiver information
967 *
968 * Return: Pointer to hard interface (with increased refcnt) on success, error
969 * pointer on error
970 */
971static struct batadv_hard_iface *
972batadv_get_hardif_from_info(struct batadv_priv *bat_priv, struct net *net,
973 struct genl_info *info)
974{
975 struct batadv_hard_iface *hard_iface;
976 struct net_device *hard_dev;
977 unsigned int hardif_index;
978
979 if (!info->attrs[BATADV_ATTR_HARD_IFINDEX])
980 return ERR_PTR(-EINVAL);
981
982 hardif_index = nla_get_u32(info->attrs[BATADV_ATTR_HARD_IFINDEX]);
983
984 hard_dev = dev_get_by_index(net, hardif_index);
985 if (!hard_dev)
986 return ERR_PTR(-ENODEV);
987
988 hard_iface = batadv_hardif_get_by_netdev(hard_dev);
989 if (!hard_iface)
990 goto err_put_harddev;
991
992 if (hard_iface->soft_iface != bat_priv->soft_iface)
993 goto err_put_hardif;
994
995 /* hard_dev is referenced by hard_iface and not needed here */
996 dev_put(hard_dev);
997
998 return hard_iface;
999
1000err_put_hardif:
1001 batadv_hardif_put(hard_iface);
1002err_put_harddev:
1003 dev_put(hard_dev);
1004
1005 return ERR_PTR(-EINVAL);
1006}
1007
1008/**
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001009 * batadv_get_vlan_from_info() - Retrieve vlan from genl attributes
1010 * @bat_priv: the bat priv with all the soft interface information
1011 * @net: the applicable net namespace
1012 * @info: receiver information
1013 *
1014 * Return: Pointer to vlan on success (with increased refcnt), error pointer
1015 * on error
1016 */
1017static struct batadv_softif_vlan *
1018batadv_get_vlan_from_info(struct batadv_priv *bat_priv, struct net *net,
1019 struct genl_info *info)
1020{
1021 struct batadv_softif_vlan *vlan;
1022 u16 vid;
1023
1024 if (!info->attrs[BATADV_ATTR_VLANID])
1025 return ERR_PTR(-EINVAL);
1026
1027 vid = nla_get_u16(info->attrs[BATADV_ATTR_VLANID]);
1028
1029 vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
1030 if (!vlan)
1031 return ERR_PTR(-ENOENT);
1032
1033 return vlan;
1034}
1035
1036/**
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001037 * batadv_pre_doit() - Prepare batman-adv genl doit request
1038 * @ops: requested netlink operation
1039 * @skb: Netlink message with request data
1040 * @info: receiver information
1041 *
1042 * Return: 0 on success or negative error number in case of failure
1043 */
1044static int batadv_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
1045 struct genl_info *info)
1046{
1047 struct net *net = genl_info_net(info);
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001048 struct batadv_hard_iface *hard_iface;
1049 struct batadv_priv *bat_priv = NULL;
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001050 struct batadv_softif_vlan *vlan;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001051 struct net_device *soft_iface;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001052 u8 user_ptr1_flags;
1053 u8 mesh_dep_flags;
1054 int ret;
1055
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001056 user_ptr1_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001057 if (WARN_ON(hweight8(ops->internal_flags & user_ptr1_flags) > 1))
1058 return -EINVAL;
1059
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001060 mesh_dep_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001061 if (WARN_ON((ops->internal_flags & mesh_dep_flags) &&
1062 (~ops->internal_flags & BATADV_FLAG_NEED_MESH)))
1063 return -EINVAL;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001064
1065 if (ops->internal_flags & BATADV_FLAG_NEED_MESH) {
1066 soft_iface = batadv_get_softif_from_info(net, info);
1067 if (IS_ERR(soft_iface))
1068 return PTR_ERR(soft_iface);
1069
1070 bat_priv = netdev_priv(soft_iface);
1071 info->user_ptr[0] = bat_priv;
1072 }
1073
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001074 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF) {
1075 hard_iface = batadv_get_hardif_from_info(bat_priv, net, info);
1076 if (IS_ERR(hard_iface)) {
1077 ret = PTR_ERR(hard_iface);
1078 goto err_put_softif;
1079 }
1080
1081 info->user_ptr[1] = hard_iface;
1082 }
1083
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001084 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN) {
1085 vlan = batadv_get_vlan_from_info(bat_priv, net, info);
1086 if (IS_ERR(vlan)) {
1087 ret = PTR_ERR(vlan);
1088 goto err_put_softif;
1089 }
1090
1091 info->user_ptr[1] = vlan;
1092 }
1093
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001094 return 0;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001095
1096err_put_softif:
1097 if (bat_priv)
1098 dev_put(bat_priv->soft_iface);
1099
1100 return ret;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001101}
1102
1103/**
1104 * batadv_post_doit() - End batman-adv genl doit request
1105 * @ops: requested netlink operation
1106 * @skb: Netlink message with request data
1107 * @info: receiver information
1108 */
1109static void batadv_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
1110 struct genl_info *info)
1111{
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001112 struct batadv_hard_iface *hard_iface;
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001113 struct batadv_softif_vlan *vlan;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001114 struct batadv_priv *bat_priv;
1115
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001116 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF &&
1117 info->user_ptr[1]) {
1118 hard_iface = info->user_ptr[1];
1119
1120 batadv_hardif_put(hard_iface);
1121 }
1122
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001123 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN && info->user_ptr[1]) {
1124 vlan = info->user_ptr[1];
1125 batadv_softif_vlan_put(vlan);
1126 }
1127
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001128 if (ops->internal_flags & BATADV_FLAG_NEED_MESH && info->user_ptr[0]) {
1129 bat_priv = info->user_ptr[0];
1130 dev_put(bat_priv->soft_iface);
1131 }
1132}
1133
Sven Eckelmannce1a21d2016-10-18 15:57:36 +02001134static const struct genl_ops batadv_netlink_ops[] = {
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001135 {
Sven Eckelmann60040512018-11-23 12:14:56 +01001136 .cmd = BATADV_CMD_GET_MESH,
1137 /* can be retrieved by unprivileged users */
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001138 .policy = batadv_netlink_policy,
Sven Eckelmann60040512018-11-23 12:14:56 +01001139 .doit = batadv_netlink_get_mesh,
1140 .internal_flags = BATADV_FLAG_NEED_MESH,
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001141 },
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001142 {
1143 .cmd = BATADV_CMD_TP_METER,
1144 .flags = GENL_ADMIN_PERM,
1145 .policy = batadv_netlink_policy,
1146 .doit = batadv_netlink_tp_meter_start,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001147 .internal_flags = BATADV_FLAG_NEED_MESH,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001148 },
1149 {
1150 .cmd = BATADV_CMD_TP_METER_CANCEL,
1151 .flags = GENL_ADMIN_PERM,
1152 .policy = batadv_netlink_policy,
1153 .doit = batadv_netlink_tp_meter_cancel,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001154 .internal_flags = BATADV_FLAG_NEED_MESH,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001155 },
Matthias Schiffer07a30612016-07-03 13:31:35 +02001156 {
1157 .cmd = BATADV_CMD_GET_ROUTING_ALGOS,
1158 .flags = GENL_ADMIN_PERM,
1159 .policy = batadv_netlink_policy,
1160 .dumpit = batadv_algo_dump,
1161 },
Matthias Schifferb60620c2016-07-03 13:31:36 +02001162 {
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001163 .cmd = BATADV_CMD_GET_HARDIF,
1164 /* can be retrieved by unprivileged users */
Matthias Schifferb60620c2016-07-03 13:31:36 +02001165 .policy = batadv_netlink_policy,
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001166 .dumpit = batadv_netlink_dump_hardif,
1167 .doit = batadv_netlink_get_hardif,
1168 .internal_flags = BATADV_FLAG_NEED_MESH |
1169 BATADV_FLAG_NEED_HARDIF,
Matthias Schifferb60620c2016-07-03 13:31:36 +02001170 },
Matthias Schifferd34f0552016-07-03 13:31:37 +02001171 {
1172 .cmd = BATADV_CMD_GET_TRANSTABLE_LOCAL,
1173 .flags = GENL_ADMIN_PERM,
1174 .policy = batadv_netlink_policy,
1175 .dumpit = batadv_tt_local_dump,
1176 },
1177 {
1178 .cmd = BATADV_CMD_GET_TRANSTABLE_GLOBAL,
1179 .flags = GENL_ADMIN_PERM,
1180 .policy = batadv_netlink_policy,
1181 .dumpit = batadv_tt_global_dump,
1182 },
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001183 {
1184 .cmd = BATADV_CMD_GET_ORIGINATORS,
1185 .flags = GENL_ADMIN_PERM,
1186 .policy = batadv_netlink_policy,
1187 .dumpit = batadv_orig_dump,
1188 },
1189 {
1190 .cmd = BATADV_CMD_GET_NEIGHBORS,
1191 .flags = GENL_ADMIN_PERM,
1192 .policy = batadv_netlink_policy,
1193 .dumpit = batadv_hardif_neigh_dump,
1194 },
Sven Eckelmannd7129da2016-07-03 13:31:42 +02001195 {
1196 .cmd = BATADV_CMD_GET_GATEWAYS,
1197 .flags = GENL_ADMIN_PERM,
1198 .policy = batadv_netlink_policy,
1199 .dumpit = batadv_gw_dump,
1200 },
Andrew Lunn04f3f5b2016-07-03 13:31:45 +02001201 {
1202 .cmd = BATADV_CMD_GET_BLA_CLAIM,
1203 .flags = GENL_ADMIN_PERM,
1204 .policy = batadv_netlink_policy,
1205 .dumpit = batadv_bla_claim_dump,
1206 },
Simon Wunderlichea4152e2016-07-03 13:31:47 +02001207 {
1208 .cmd = BATADV_CMD_GET_BLA_BACKBONE,
1209 .flags = GENL_ADMIN_PERM,
1210 .policy = batadv_netlink_policy,
1211 .dumpit = batadv_bla_backbone_dump,
1212 },
Linus Lüssing41aeefc2018-03-13 11:41:12 +01001213 {
1214 .cmd = BATADV_CMD_GET_DAT_CACHE,
1215 .flags = GENL_ADMIN_PERM,
1216 .policy = batadv_netlink_policy,
1217 .dumpit = batadv_dat_cache_dump,
1218 },
Linus Lüssing53dd9a62018-03-13 11:41:13 +01001219 {
1220 .cmd = BATADV_CMD_GET_MCAST_FLAGS,
1221 .flags = GENL_ADMIN_PERM,
1222 .policy = batadv_netlink_policy,
1223 .dumpit = batadv_mcast_flags_dump,
1224 },
Sven Eckelmann60040512018-11-23 12:14:56 +01001225 {
1226 .cmd = BATADV_CMD_SET_MESH,
1227 .flags = GENL_ADMIN_PERM,
1228 .policy = batadv_netlink_policy,
1229 .doit = batadv_netlink_set_mesh,
1230 .internal_flags = BATADV_FLAG_NEED_MESH,
1231 },
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001232 {
1233 .cmd = BATADV_CMD_SET_HARDIF,
1234 .flags = GENL_ADMIN_PERM,
1235 .policy = batadv_netlink_policy,
1236 .doit = batadv_netlink_set_hardif,
1237 .internal_flags = BATADV_FLAG_NEED_MESH |
1238 BATADV_FLAG_NEED_HARDIF,
1239 },
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001240 {
1241 .cmd = BATADV_CMD_GET_VLAN,
1242 /* can be retrieved by unprivileged users */
1243 .policy = batadv_netlink_policy,
1244 .doit = batadv_netlink_get_vlan,
1245 .internal_flags = BATADV_FLAG_NEED_MESH |
1246 BATADV_FLAG_NEED_VLAN,
1247 },
1248 {
1249 .cmd = BATADV_CMD_SET_VLAN,
1250 .flags = GENL_ADMIN_PERM,
1251 .policy = batadv_netlink_policy,
1252 .doit = batadv_netlink_set_vlan,
1253 .internal_flags = BATADV_FLAG_NEED_MESH |
1254 BATADV_FLAG_NEED_VLAN,
1255 },
Matthias Schiffer09748a22016-05-09 18:41:08 +02001256};
1257
Johannes Berg56989f62016-10-24 14:40:05 +02001258struct genl_family batadv_netlink_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001259 .hdrsize = 0,
1260 .name = BATADV_NL_NAME,
1261 .version = 1,
1262 .maxattr = BATADV_ATTR_MAX,
1263 .netnsok = true,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001264 .pre_doit = batadv_pre_doit,
1265 .post_doit = batadv_post_doit,
Johannes Berg489111e2016-10-24 14:40:03 +02001266 .module = THIS_MODULE,
1267 .ops = batadv_netlink_ops,
1268 .n_ops = ARRAY_SIZE(batadv_netlink_ops),
1269 .mcgrps = batadv_netlink_mcgrps,
1270 .n_mcgrps = ARRAY_SIZE(batadv_netlink_mcgrps),
1271};
1272
Matthias Schiffer09748a22016-05-09 18:41:08 +02001273/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001274 * batadv_netlink_register() - register batadv genl netlink family
Matthias Schiffer09748a22016-05-09 18:41:08 +02001275 */
1276void __init batadv_netlink_register(void)
1277{
1278 int ret;
1279
Johannes Berg489111e2016-10-24 14:40:03 +02001280 ret = genl_register_family(&batadv_netlink_family);
Matthias Schiffer09748a22016-05-09 18:41:08 +02001281 if (ret)
1282 pr_warn("unable to register netlink family");
1283}
1284
1285/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001286 * batadv_netlink_unregister() - unregister batadv genl netlink family
Matthias Schiffer09748a22016-05-09 18:41:08 +02001287 */
1288void batadv_netlink_unregister(void)
1289{
1290 genl_unregister_family(&batadv_netlink_family);
1291}