blob: 310a2c339fd1a3c4f1e15131148d79071c409510 [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 },
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200147};
148
149/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100150 * batadv_netlink_get_ifindex() - Extract an interface index from a message
Matthias Schifferb60620c2016-07-03 13:31:36 +0200151 * @nlh: Message header
152 * @attrtype: Attribute which holds an interface index
153 *
154 * Return: interface index, or 0.
155 */
Matthias Schifferd34f0552016-07-03 13:31:37 +0200156int
Matthias Schifferb60620c2016-07-03 13:31:36 +0200157batadv_netlink_get_ifindex(const struct nlmsghdr *nlh, int attrtype)
158{
159 struct nlattr *attr = nlmsg_find_attr(nlh, GENL_HDRLEN, attrtype);
160
161 return attr ? nla_get_u32(attr) : 0;
162}
163
164/**
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100165 * batadv_netlink_mesh_fill_ap_isolation() - Add ap_isolation softif attribute
166 * @msg: Netlink message to dump into
167 * @bat_priv: the bat priv with all the soft interface information
168 *
169 * Return: 0 on success or negative error number in case of failure
170 */
171static int batadv_netlink_mesh_fill_ap_isolation(struct sk_buff *msg,
172 struct batadv_priv *bat_priv)
173{
174 struct batadv_softif_vlan *vlan;
175 u8 ap_isolation;
176
177 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
178 if (!vlan)
179 return 0;
180
181 ap_isolation = atomic_read(&vlan->ap_isolation);
182 batadv_softif_vlan_put(vlan);
183
184 return nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
185 !!ap_isolation);
186}
187
188/**
189 * batadv_option_set_ap_isolation() - Set ap_isolation from genl msg
190 * @attr: parsed BATADV_ATTR_AP_ISOLATION_ENABLED attribute
191 * @bat_priv: the bat priv with all the soft interface information
192 *
193 * Return: 0 on success or negative error number in case of failure
194 */
195static int batadv_netlink_set_mesh_ap_isolation(struct nlattr *attr,
196 struct batadv_priv *bat_priv)
197{
198 struct batadv_softif_vlan *vlan;
199
200 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
201 if (!vlan)
202 return -ENOENT;
203
204 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
205 batadv_softif_vlan_put(vlan);
206
207 return 0;
208}
209
210/**
Sven Eckelmann60040512018-11-23 12:14:56 +0100211 * batadv_netlink_mesh_fill() - Fill message with mesh attributes
212 * @msg: Netlink message to dump into
213 * @bat_priv: the bat priv with all the soft interface information
214 * @cmd: type of message to generate
215 * @portid: Port making netlink request
216 * @seq: sequence number for message
217 * @flags: Additional flags for message
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200218 *
Sven Eckelmann60040512018-11-23 12:14:56 +0100219 * Return: 0 on success or negative error number in case of failure
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200220 */
Sven Eckelmann60040512018-11-23 12:14:56 +0100221static int batadv_netlink_mesh_fill(struct sk_buff *msg,
222 struct batadv_priv *bat_priv,
223 enum batadv_nl_commands cmd,
224 u32 portid, u32 seq, int flags)
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200225{
Sven Eckelmann60040512018-11-23 12:14:56 +0100226 struct net_device *soft_iface = bat_priv->soft_iface;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200227 struct batadv_hard_iface *primary_if = NULL;
228 struct net_device *hard_iface;
Sven Eckelmann60040512018-11-23 12:14:56 +0100229 void *hdr;
230
231 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
232 if (!hdr)
233 return -ENOBUFS;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200234
235 if (nla_put_string(msg, BATADV_ATTR_VERSION, BATADV_SOURCE_VERSION) ||
236 nla_put_string(msg, BATADV_ATTR_ALGO_NAME,
Antonio Quartulli29824a52016-05-25 23:27:31 +0800237 bat_priv->algo_ops->name) ||
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200238 nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, soft_iface->ifindex) ||
239 nla_put_string(msg, BATADV_ATTR_MESH_IFNAME, soft_iface->name) ||
240 nla_put(msg, BATADV_ATTR_MESH_ADDRESS, ETH_ALEN,
Sven Eckelmannf32ed4b2016-07-03 13:31:38 +0200241 soft_iface->dev_addr) ||
242 nla_put_u8(msg, BATADV_ATTR_TT_TTVN,
243 (u8)atomic_read(&bat_priv->tt.vn)))
Sven Eckelmann60040512018-11-23 12:14:56 +0100244 goto nla_put_failure;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200245
Sven Eckelmann8dad6f0d2016-07-03 13:31:46 +0200246#ifdef CONFIG_BATMAN_ADV_BLA
247 if (nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
248 ntohs(bat_priv->bla.claim_dest.group)))
Sven Eckelmann60040512018-11-23 12:14:56 +0100249 goto nla_put_failure;
Sven Eckelmann8dad6f0d2016-07-03 13:31:46 +0200250#endif
251
Linus Lüssing53dd9a62018-03-13 11:41:13 +0100252 if (batadv_mcast_mesh_info_put(msg, bat_priv))
Sven Eckelmann60040512018-11-23 12:14:56 +0100253 goto nla_put_failure;
Linus Lüssing53dd9a62018-03-13 11:41:13 +0100254
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200255 primary_if = batadv_primary_if_get_selected(bat_priv);
256 if (primary_if && primary_if->if_status == BATADV_IF_ACTIVE) {
257 hard_iface = primary_if->net_dev;
258
259 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
260 hard_iface->ifindex) ||
261 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
262 hard_iface->name) ||
263 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
264 hard_iface->dev_addr))
Sven Eckelmann60040512018-11-23 12:14:56 +0100265 goto nla_put_failure;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200266 }
267
Sven Eckelmann9ab4cee2018-11-23 12:46:14 +0100268 if (nla_put_u8(msg, BATADV_ATTR_AGGREGATED_OGMS_ENABLED,
269 !!atomic_read(&bat_priv->aggregated_ogms)))
270 goto nla_put_failure;
271
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100272 if (batadv_netlink_mesh_fill_ap_isolation(msg, bat_priv))
273 goto nla_put_failure;
274
275 if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MARK,
276 bat_priv->isolation_mark))
277 goto nla_put_failure;
278
279 if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MASK,
280 bat_priv->isolation_mark_mask))
281 goto nla_put_failure;
282
Sven Eckelmannd7e52502018-11-23 12:55:44 +0100283 if (nla_put_u8(msg, BATADV_ATTR_BONDING_ENABLED,
284 !!atomic_read(&bat_priv->bonding)))
285 goto nla_put_failure;
286
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200287 if (primary_if)
288 batadv_hardif_put(primary_if);
289
Sven Eckelmann60040512018-11-23 12:14:56 +0100290 genlmsg_end(msg, hdr);
291 return 0;
292
293nla_put_failure:
294 if (primary_if)
295 batadv_hardif_put(primary_if);
296
297 genlmsg_cancel(msg, hdr);
298 return -EMSGSIZE;
299}
300
301/**
302 * batadv_netlink_notify_mesh() - send softif attributes to listener
303 * @bat_priv: the bat priv with all the soft interface information
304 *
305 * Return: 0 on success, < 0 on error
306 */
307static int batadv_netlink_notify_mesh(struct batadv_priv *bat_priv)
308{
309 struct sk_buff *msg;
310 int ret;
311
312 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
313 if (!msg)
314 return -ENOMEM;
315
316 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_SET_MESH,
317 0, 0, 0);
318 if (ret < 0) {
319 nlmsg_free(msg);
320 return ret;
321 }
322
323 genlmsg_multicast_netns(&batadv_netlink_family,
324 dev_net(bat_priv->soft_iface), msg, 0,
325 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
326
327 return 0;
328}
329
330/**
331 * batadv_netlink_get_mesh() - Get softif attributes
332 * @skb: Netlink message with request data
333 * @info: receiver information
334 *
335 * Return: 0 on success or negative error number in case of failure
336 */
337static int batadv_netlink_get_mesh(struct sk_buff *skb, struct genl_info *info)
338{
339 struct batadv_priv *bat_priv = info->user_ptr[0];
340 struct sk_buff *msg;
341 int ret;
342
343 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
344 if (!msg)
345 return -ENOMEM;
346
347 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_GET_MESH,
348 info->snd_portid, info->snd_seq, 0);
349 if (ret < 0) {
350 nlmsg_free(msg);
351 return ret;
352 }
353
354 ret = genlmsg_reply(msg, info);
355
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200356 return ret;
357}
358
359/**
Sven Eckelmann60040512018-11-23 12:14:56 +0100360 * batadv_netlink_set_mesh() - Set softif attributes
361 * @skb: Netlink message with request data
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200362 * @info: receiver information
363 *
Sven Eckelmann60040512018-11-23 12:14:56 +0100364 * Return: 0 on success or negative error number in case of failure
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200365 */
Sven Eckelmann60040512018-11-23 12:14:56 +0100366static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info)
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200367{
Sven Eckelmann60040512018-11-23 12:14:56 +0100368 struct batadv_priv *bat_priv = info->user_ptr[0];
Sven Eckelmann9ab4cee2018-11-23 12:46:14 +0100369 struct nlattr *attr;
370
371 if (info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED]) {
372 attr = info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED];
373
374 atomic_set(&bat_priv->aggregated_ogms, !!nla_get_u8(attr));
375 }
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200376
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100377 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
378 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
379
380 batadv_netlink_set_mesh_ap_isolation(attr, bat_priv);
381 }
382
383 if (info->attrs[BATADV_ATTR_ISOLATION_MARK]) {
384 attr = info->attrs[BATADV_ATTR_ISOLATION_MARK];
385
386 bat_priv->isolation_mark = nla_get_u32(attr);
387 }
388
389 if (info->attrs[BATADV_ATTR_ISOLATION_MASK]) {
390 attr = info->attrs[BATADV_ATTR_ISOLATION_MASK];
391
392 bat_priv->isolation_mark_mask = nla_get_u32(attr);
393 }
394
Sven Eckelmannd7e52502018-11-23 12:55:44 +0100395 if (info->attrs[BATADV_ATTR_BONDING_ENABLED]) {
396 attr = info->attrs[BATADV_ATTR_BONDING_ENABLED];
397
398 atomic_set(&bat_priv->bonding, !!nla_get_u8(attr));
399 }
400
Sven Eckelmann60040512018-11-23 12:14:56 +0100401 batadv_netlink_notify_mesh(bat_priv);
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200402
Sven Eckelmann60040512018-11-23 12:14:56 +0100403 return 0;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200404}
405
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200406/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100407 * batadv_netlink_tp_meter_put() - Fill information of started tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200408 * @msg: netlink message to be sent back
409 * @cookie: tp meter session cookie
410 *
411 * Return: 0 on success, < 0 on error
412 */
413static int
414batadv_netlink_tp_meter_put(struct sk_buff *msg, u32 cookie)
415{
416 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
417 return -ENOBUFS;
418
419 return 0;
420}
421
422/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100423 * batadv_netlink_tpmeter_notify() - send tp_meter result via netlink to client
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200424 * @bat_priv: the bat priv with all the soft interface information
425 * @dst: destination of tp_meter session
426 * @result: reason for tp meter session stop
427 * @test_time: total time ot the tp_meter session
428 * @total_bytes: bytes acked to the receiver
429 * @cookie: cookie of tp_meter session
430 *
431 * Return: 0 on success, < 0 on error
432 */
433int batadv_netlink_tpmeter_notify(struct batadv_priv *bat_priv, const u8 *dst,
434 u8 result, u32 test_time, u64 total_bytes,
435 u32 cookie)
436{
437 struct sk_buff *msg;
438 void *hdr;
439 int ret;
440
441 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
442 if (!msg)
443 return -ENOMEM;
444
445 hdr = genlmsg_put(msg, 0, 0, &batadv_netlink_family, 0,
446 BATADV_CMD_TP_METER);
447 if (!hdr) {
448 ret = -ENOBUFS;
449 goto err_genlmsg;
450 }
451
452 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
453 goto nla_put_failure;
454
455 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, test_time))
456 goto nla_put_failure;
457
458 if (nla_put_u64_64bit(msg, BATADV_ATTR_TPMETER_BYTES, total_bytes,
459 BATADV_ATTR_PAD))
460 goto nla_put_failure;
461
462 if (nla_put_u8(msg, BATADV_ATTR_TPMETER_RESULT, result))
463 goto nla_put_failure;
464
465 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst))
466 goto nla_put_failure;
467
468 genlmsg_end(msg, hdr);
469
470 genlmsg_multicast_netns(&batadv_netlink_family,
471 dev_net(bat_priv->soft_iface), msg, 0,
472 BATADV_NL_MCGRP_TPMETER, GFP_KERNEL);
473
474 return 0;
475
476nla_put_failure:
477 genlmsg_cancel(msg, hdr);
478 ret = -EMSGSIZE;
479
480err_genlmsg:
481 nlmsg_free(msg);
482 return ret;
483}
484
485/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100486 * batadv_netlink_tp_meter_start() - Start a new tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200487 * @skb: received netlink message
488 * @info: receiver information
489 *
490 * Return: 0 on success, < 0 on error
491 */
492static int
493batadv_netlink_tp_meter_start(struct sk_buff *skb, struct genl_info *info)
494{
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100495 struct batadv_priv *bat_priv = info->user_ptr[0];
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200496 struct sk_buff *msg = NULL;
497 u32 test_length;
498 void *msg_head;
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200499 u32 cookie;
500 u8 *dst;
501 int ret;
502
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200503 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
504 return -EINVAL;
505
506 if (!info->attrs[BATADV_ATTR_TPMETER_TEST_TIME])
507 return -EINVAL;
508
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200509 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
510
511 test_length = nla_get_u32(info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]);
512
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200513 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
514 if (!msg) {
515 ret = -ENOMEM;
516 goto out;
517 }
518
519 msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq,
520 &batadv_netlink_family, 0,
521 BATADV_CMD_TP_METER);
522 if (!msg_head) {
523 ret = -ENOBUFS;
524 goto out;
525 }
526
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200527 batadv_tp_start(bat_priv, dst, test_length, &cookie);
528
529 ret = batadv_netlink_tp_meter_put(msg, cookie);
530
531 out:
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200532 if (ret) {
533 if (msg)
534 nlmsg_free(msg);
535 return ret;
536 }
537
538 genlmsg_end(msg, msg_head);
539 return genlmsg_reply(msg, info);
540}
541
542/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100543 * batadv_netlink_tp_meter_start() - Cancel a running tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200544 * @skb: received netlink message
545 * @info: receiver information
546 *
547 * Return: 0 on success, < 0 on error
548 */
549static int
550batadv_netlink_tp_meter_cancel(struct sk_buff *skb, struct genl_info *info)
551{
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100552 struct batadv_priv *bat_priv = info->user_ptr[0];
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200553 u8 *dst;
554 int ret = 0;
555
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200556 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
557 return -EINVAL;
558
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200559 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
560
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200561 batadv_tp_stop(bat_priv, dst, BATADV_TP_REASON_CANCEL);
562
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200563 return ret;
564}
565
Matthias Schifferb60620c2016-07-03 13:31:36 +0200566/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100567 * batadv_netlink_hardif_fill() - Fill message with hardif attributes
Matthias Schifferb60620c2016-07-03 13:31:36 +0200568 * @msg: Netlink message to dump into
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100569 * @bat_priv: the bat priv with all the soft interface information
570 * @hard_iface: hard interface which was modified
571 * @cmd: type of message to generate
Matthias Schifferb60620c2016-07-03 13:31:36 +0200572 * @portid: Port making netlink request
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100573 * @seq: sequence number for message
574 * @flags: Additional flags for message
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100575 * @cb: Control block containing additional options
Matthias Schifferb60620c2016-07-03 13:31:36 +0200576 *
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100577 * Return: 0 on success or negative error number in case of failure
Matthias Schifferb60620c2016-07-03 13:31:36 +0200578 */
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100579static int batadv_netlink_hardif_fill(struct sk_buff *msg,
580 struct batadv_priv *bat_priv,
581 struct batadv_hard_iface *hard_iface,
582 enum batadv_nl_commands cmd,
583 u32 portid, u32 seq, int flags,
584 struct netlink_callback *cb)
Matthias Schifferb60620c2016-07-03 13:31:36 +0200585{
586 struct net_device *net_dev = hard_iface->net_dev;
587 void *hdr;
588
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100589 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
Matthias Schifferb60620c2016-07-03 13:31:36 +0200590 if (!hdr)
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100591 return -ENOBUFS;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200592
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100593 if (cb)
594 genl_dump_check_consistent(cb, hdr);
595
596 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
597 bat_priv->soft_iface->ifindex))
598 goto nla_put_failure;
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100599
Matthias Schifferb60620c2016-07-03 13:31:36 +0200600 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
601 net_dev->ifindex) ||
602 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
603 net_dev->name) ||
604 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
605 net_dev->dev_addr))
606 goto nla_put_failure;
607
608 if (hard_iface->if_status == BATADV_IF_ACTIVE) {
609 if (nla_put_flag(msg, BATADV_ATTR_ACTIVE))
610 goto nla_put_failure;
611 }
612
613 genlmsg_end(msg, hdr);
614 return 0;
615
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100616nla_put_failure:
Matthias Schifferb60620c2016-07-03 13:31:36 +0200617 genlmsg_cancel(msg, hdr);
618 return -EMSGSIZE;
619}
620
621/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100622 * batadv_netlink_notify_hardif() - send hardif attributes to listener
623 * @bat_priv: the bat priv with all the soft interface information
624 * @hard_iface: hard interface which was modified
625 *
626 * Return: 0 on success, < 0 on error
627 */
628static int batadv_netlink_notify_hardif(struct batadv_priv *bat_priv,
629 struct batadv_hard_iface *hard_iface)
630{
631 struct sk_buff *msg;
632 int ret;
633
634 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
635 if (!msg)
636 return -ENOMEM;
637
638 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
639 BATADV_CMD_SET_HARDIF, 0, 0, 0, NULL);
640 if (ret < 0) {
641 nlmsg_free(msg);
642 return ret;
643 }
644
645 genlmsg_multicast_netns(&batadv_netlink_family,
646 dev_net(bat_priv->soft_iface), msg, 0,
647 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
648
649 return 0;
650}
651
652/**
653 * batadv_netlink_get_hardif() - Get hardif attributes
654 * @skb: Netlink message with request data
655 * @info: receiver information
656 *
657 * Return: 0 on success or negative error number in case of failure
658 */
659static int batadv_netlink_get_hardif(struct sk_buff *skb,
660 struct genl_info *info)
661{
662 struct batadv_hard_iface *hard_iface = info->user_ptr[1];
663 struct batadv_priv *bat_priv = info->user_ptr[0];
664 struct sk_buff *msg;
665 int ret;
666
667 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
668 if (!msg)
669 return -ENOMEM;
670
671 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
672 BATADV_CMD_GET_HARDIF,
673 info->snd_portid, info->snd_seq, 0,
674 NULL);
675 if (ret < 0) {
676 nlmsg_free(msg);
677 return ret;
678 }
679
680 ret = genlmsg_reply(msg, info);
681
682 return ret;
683}
684
685/**
686 * batadv_netlink_set_hardif() - Set hardif attributes
687 * @skb: Netlink message with request data
688 * @info: receiver information
689 *
690 * Return: 0 on success or negative error number in case of failure
691 */
692static int batadv_netlink_set_hardif(struct sk_buff *skb,
693 struct genl_info *info)
694{
695 struct batadv_hard_iface *hard_iface = info->user_ptr[1];
696 struct batadv_priv *bat_priv = info->user_ptr[0];
697
698 batadv_netlink_notify_hardif(bat_priv, hard_iface);
699
700 return 0;
701}
702
703/**
704 * batadv_netlink_dump_hardif() - Dump all hard interface into a messages
Matthias Schifferb60620c2016-07-03 13:31:36 +0200705 * @msg: Netlink message to dump into
706 * @cb: Parameters from query
707 *
708 * Return: error code, or length of reply message on success
709 */
710static int
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100711batadv_netlink_dump_hardif(struct sk_buff *msg, struct netlink_callback *cb)
Matthias Schifferb60620c2016-07-03 13:31:36 +0200712{
713 struct net *net = sock_net(cb->skb->sk);
714 struct net_device *soft_iface;
715 struct batadv_hard_iface *hard_iface;
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100716 struct batadv_priv *bat_priv;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200717 int ifindex;
718 int portid = NETLINK_CB(cb->skb).portid;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200719 int skip = cb->args[0];
720 int i = 0;
721
722 ifindex = batadv_netlink_get_ifindex(cb->nlh,
723 BATADV_ATTR_MESH_IFINDEX);
724 if (!ifindex)
725 return -EINVAL;
726
727 soft_iface = dev_get_by_index(net, ifindex);
728 if (!soft_iface)
729 return -ENODEV;
730
731 if (!batadv_softif_is_valid(soft_iface)) {
732 dev_put(soft_iface);
733 return -ENODEV;
734 }
735
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100736 bat_priv = netdev_priv(soft_iface);
737
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100738 rtnl_lock();
739 cb->seq = batadv_hardif_generation << 1 | 1;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200740
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100741 list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
Matthias Schifferb60620c2016-07-03 13:31:36 +0200742 if (hard_iface->soft_iface != soft_iface)
743 continue;
744
745 if (i++ < skip)
746 continue;
747
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100748 if (batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
749 BATADV_CMD_GET_HARDIF,
750 portid, cb->nlh->nlmsg_seq,
751 NLM_F_MULTI, cb)) {
Matthias Schifferb60620c2016-07-03 13:31:36 +0200752 i--;
753 break;
754 }
755 }
756
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100757 rtnl_unlock();
Matthias Schifferb60620c2016-07-03 13:31:36 +0200758
759 dev_put(soft_iface);
760
761 cb->args[0] = i;
762
763 return msg->len;
764}
765
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100766/**
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100767 * batadv_netlink_vlan_fill() - Fill message with vlan attributes
768 * @msg: Netlink message to dump into
769 * @bat_priv: the bat priv with all the soft interface information
770 * @vlan: vlan which was modified
771 * @cmd: type of message to generate
772 * @portid: Port making netlink request
773 * @seq: sequence number for message
774 * @flags: Additional flags for message
775 *
776 * Return: 0 on success or negative error number in case of failure
777 */
778static int batadv_netlink_vlan_fill(struct sk_buff *msg,
779 struct batadv_priv *bat_priv,
780 struct batadv_softif_vlan *vlan,
781 enum batadv_nl_commands cmd,
782 u32 portid, u32 seq, int flags)
783{
784 void *hdr;
785
786 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
787 if (!hdr)
788 return -ENOBUFS;
789
790 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
791 bat_priv->soft_iface->ifindex))
792 goto nla_put_failure;
793
794 if (nla_put_u32(msg, BATADV_ATTR_VLANID, vlan->vid & VLAN_VID_MASK))
795 goto nla_put_failure;
796
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100797 if (nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
798 !!atomic_read(&vlan->ap_isolation)))
799 goto nla_put_failure;
800
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100801 genlmsg_end(msg, hdr);
802 return 0;
803
804nla_put_failure:
805 genlmsg_cancel(msg, hdr);
806 return -EMSGSIZE;
807}
808
809/**
810 * batadv_netlink_notify_vlan() - send vlan attributes to listener
811 * @bat_priv: the bat priv with all the soft interface information
812 * @vlan: vlan which was modified
813 *
814 * Return: 0 on success, < 0 on error
815 */
816static int batadv_netlink_notify_vlan(struct batadv_priv *bat_priv,
817 struct batadv_softif_vlan *vlan)
818{
819 struct sk_buff *msg;
820 int ret;
821
822 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
823 if (!msg)
824 return -ENOMEM;
825
826 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan,
827 BATADV_CMD_SET_VLAN, 0, 0, 0);
828 if (ret < 0) {
829 nlmsg_free(msg);
830 return ret;
831 }
832
833 genlmsg_multicast_netns(&batadv_netlink_family,
834 dev_net(bat_priv->soft_iface), msg, 0,
835 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
836
837 return 0;
838}
839
840/**
841 * batadv_netlink_get_vlan() - Get vlan attributes
842 * @skb: Netlink message with request data
843 * @info: receiver information
844 *
845 * Return: 0 on success or negative error number in case of failure
846 */
847static int batadv_netlink_get_vlan(struct sk_buff *skb, struct genl_info *info)
848{
849 struct batadv_softif_vlan *vlan = info->user_ptr[1];
850 struct batadv_priv *bat_priv = info->user_ptr[0];
851 struct sk_buff *msg;
852 int ret;
853
854 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
855 if (!msg)
856 return -ENOMEM;
857
858 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan, BATADV_CMD_GET_VLAN,
859 info->snd_portid, info->snd_seq, 0);
860 if (ret < 0) {
861 nlmsg_free(msg);
862 return ret;
863 }
864
865 ret = genlmsg_reply(msg, info);
866
867 return ret;
868}
869
870/**
871 * batadv_netlink_set_vlan() - Get vlan attributes
872 * @skb: Netlink message with request data
873 * @info: receiver information
874 *
875 * Return: 0 on success or negative error number in case of failure
876 */
877static int batadv_netlink_set_vlan(struct sk_buff *skb, struct genl_info *info)
878{
879 struct batadv_softif_vlan *vlan = info->user_ptr[1];
880 struct batadv_priv *bat_priv = info->user_ptr[0];
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100881 struct nlattr *attr;
882
883 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
884 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
885
886 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
887 }
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100888
889 batadv_netlink_notify_vlan(bat_priv, vlan);
890
891 return 0;
892}
893
894/**
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100895 * batadv_get_softif_from_info() - Retrieve soft interface from genl attributes
896 * @net: the applicable net namespace
897 * @info: receiver information
898 *
899 * Return: Pointer to soft interface (with increased refcnt) on success, error
900 * pointer on error
901 */
902static struct net_device *
903batadv_get_softif_from_info(struct net *net, struct genl_info *info)
904{
905 struct net_device *soft_iface;
906 int ifindex;
907
908 if (!info->attrs[BATADV_ATTR_MESH_IFINDEX])
909 return ERR_PTR(-EINVAL);
910
911 ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
912
913 soft_iface = dev_get_by_index(net, ifindex);
914 if (!soft_iface)
915 return ERR_PTR(-ENODEV);
916
917 if (!batadv_softif_is_valid(soft_iface))
918 goto err_put_softif;
919
920 return soft_iface;
921
922err_put_softif:
923 dev_put(soft_iface);
924
925 return ERR_PTR(-EINVAL);
926}
927
928/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100929 * batadv_get_hardif_from_info() - Retrieve hardif from genl attributes
930 * @bat_priv: the bat priv with all the soft interface information
931 * @net: the applicable net namespace
932 * @info: receiver information
933 *
934 * Return: Pointer to hard interface (with increased refcnt) on success, error
935 * pointer on error
936 */
937static struct batadv_hard_iface *
938batadv_get_hardif_from_info(struct batadv_priv *bat_priv, struct net *net,
939 struct genl_info *info)
940{
941 struct batadv_hard_iface *hard_iface;
942 struct net_device *hard_dev;
943 unsigned int hardif_index;
944
945 if (!info->attrs[BATADV_ATTR_HARD_IFINDEX])
946 return ERR_PTR(-EINVAL);
947
948 hardif_index = nla_get_u32(info->attrs[BATADV_ATTR_HARD_IFINDEX]);
949
950 hard_dev = dev_get_by_index(net, hardif_index);
951 if (!hard_dev)
952 return ERR_PTR(-ENODEV);
953
954 hard_iface = batadv_hardif_get_by_netdev(hard_dev);
955 if (!hard_iface)
956 goto err_put_harddev;
957
958 if (hard_iface->soft_iface != bat_priv->soft_iface)
959 goto err_put_hardif;
960
961 /* hard_dev is referenced by hard_iface and not needed here */
962 dev_put(hard_dev);
963
964 return hard_iface;
965
966err_put_hardif:
967 batadv_hardif_put(hard_iface);
968err_put_harddev:
969 dev_put(hard_dev);
970
971 return ERR_PTR(-EINVAL);
972}
973
974/**
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100975 * batadv_get_vlan_from_info() - Retrieve vlan from genl attributes
976 * @bat_priv: the bat priv with all the soft interface information
977 * @net: the applicable net namespace
978 * @info: receiver information
979 *
980 * Return: Pointer to vlan on success (with increased refcnt), error pointer
981 * on error
982 */
983static struct batadv_softif_vlan *
984batadv_get_vlan_from_info(struct batadv_priv *bat_priv, struct net *net,
985 struct genl_info *info)
986{
987 struct batadv_softif_vlan *vlan;
988 u16 vid;
989
990 if (!info->attrs[BATADV_ATTR_VLANID])
991 return ERR_PTR(-EINVAL);
992
993 vid = nla_get_u16(info->attrs[BATADV_ATTR_VLANID]);
994
995 vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
996 if (!vlan)
997 return ERR_PTR(-ENOENT);
998
999 return vlan;
1000}
1001
1002/**
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001003 * batadv_pre_doit() - Prepare batman-adv genl doit request
1004 * @ops: requested netlink operation
1005 * @skb: Netlink message with request data
1006 * @info: receiver information
1007 *
1008 * Return: 0 on success or negative error number in case of failure
1009 */
1010static int batadv_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
1011 struct genl_info *info)
1012{
1013 struct net *net = genl_info_net(info);
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001014 struct batadv_hard_iface *hard_iface;
1015 struct batadv_priv *bat_priv = NULL;
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001016 struct batadv_softif_vlan *vlan;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001017 struct net_device *soft_iface;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001018 u8 user_ptr1_flags;
1019 u8 mesh_dep_flags;
1020 int ret;
1021
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001022 user_ptr1_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001023 if (WARN_ON(hweight8(ops->internal_flags & user_ptr1_flags) > 1))
1024 return -EINVAL;
1025
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001026 mesh_dep_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001027 if (WARN_ON((ops->internal_flags & mesh_dep_flags) &&
1028 (~ops->internal_flags & BATADV_FLAG_NEED_MESH)))
1029 return -EINVAL;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001030
1031 if (ops->internal_flags & BATADV_FLAG_NEED_MESH) {
1032 soft_iface = batadv_get_softif_from_info(net, info);
1033 if (IS_ERR(soft_iface))
1034 return PTR_ERR(soft_iface);
1035
1036 bat_priv = netdev_priv(soft_iface);
1037 info->user_ptr[0] = bat_priv;
1038 }
1039
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001040 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF) {
1041 hard_iface = batadv_get_hardif_from_info(bat_priv, net, info);
1042 if (IS_ERR(hard_iface)) {
1043 ret = PTR_ERR(hard_iface);
1044 goto err_put_softif;
1045 }
1046
1047 info->user_ptr[1] = hard_iface;
1048 }
1049
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001050 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN) {
1051 vlan = batadv_get_vlan_from_info(bat_priv, net, info);
1052 if (IS_ERR(vlan)) {
1053 ret = PTR_ERR(vlan);
1054 goto err_put_softif;
1055 }
1056
1057 info->user_ptr[1] = vlan;
1058 }
1059
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001060 return 0;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001061
1062err_put_softif:
1063 if (bat_priv)
1064 dev_put(bat_priv->soft_iface);
1065
1066 return ret;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001067}
1068
1069/**
1070 * batadv_post_doit() - End batman-adv genl doit request
1071 * @ops: requested netlink operation
1072 * @skb: Netlink message with request data
1073 * @info: receiver information
1074 */
1075static void batadv_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
1076 struct genl_info *info)
1077{
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001078 struct batadv_hard_iface *hard_iface;
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001079 struct batadv_softif_vlan *vlan;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001080 struct batadv_priv *bat_priv;
1081
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001082 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF &&
1083 info->user_ptr[1]) {
1084 hard_iface = info->user_ptr[1];
1085
1086 batadv_hardif_put(hard_iface);
1087 }
1088
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001089 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN && info->user_ptr[1]) {
1090 vlan = info->user_ptr[1];
1091 batadv_softif_vlan_put(vlan);
1092 }
1093
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001094 if (ops->internal_flags & BATADV_FLAG_NEED_MESH && info->user_ptr[0]) {
1095 bat_priv = info->user_ptr[0];
1096 dev_put(bat_priv->soft_iface);
1097 }
1098}
1099
Sven Eckelmannce1a21d2016-10-18 15:57:36 +02001100static const struct genl_ops batadv_netlink_ops[] = {
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001101 {
Sven Eckelmann60040512018-11-23 12:14:56 +01001102 .cmd = BATADV_CMD_GET_MESH,
1103 /* can be retrieved by unprivileged users */
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001104 .policy = batadv_netlink_policy,
Sven Eckelmann60040512018-11-23 12:14:56 +01001105 .doit = batadv_netlink_get_mesh,
1106 .internal_flags = BATADV_FLAG_NEED_MESH,
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001107 },
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001108 {
1109 .cmd = BATADV_CMD_TP_METER,
1110 .flags = GENL_ADMIN_PERM,
1111 .policy = batadv_netlink_policy,
1112 .doit = batadv_netlink_tp_meter_start,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001113 .internal_flags = BATADV_FLAG_NEED_MESH,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001114 },
1115 {
1116 .cmd = BATADV_CMD_TP_METER_CANCEL,
1117 .flags = GENL_ADMIN_PERM,
1118 .policy = batadv_netlink_policy,
1119 .doit = batadv_netlink_tp_meter_cancel,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001120 .internal_flags = BATADV_FLAG_NEED_MESH,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001121 },
Matthias Schiffer07a30612016-07-03 13:31:35 +02001122 {
1123 .cmd = BATADV_CMD_GET_ROUTING_ALGOS,
1124 .flags = GENL_ADMIN_PERM,
1125 .policy = batadv_netlink_policy,
1126 .dumpit = batadv_algo_dump,
1127 },
Matthias Schifferb60620c2016-07-03 13:31:36 +02001128 {
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001129 .cmd = BATADV_CMD_GET_HARDIF,
1130 /* can be retrieved by unprivileged users */
Matthias Schifferb60620c2016-07-03 13:31:36 +02001131 .policy = batadv_netlink_policy,
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001132 .dumpit = batadv_netlink_dump_hardif,
1133 .doit = batadv_netlink_get_hardif,
1134 .internal_flags = BATADV_FLAG_NEED_MESH |
1135 BATADV_FLAG_NEED_HARDIF,
Matthias Schifferb60620c2016-07-03 13:31:36 +02001136 },
Matthias Schifferd34f0552016-07-03 13:31:37 +02001137 {
1138 .cmd = BATADV_CMD_GET_TRANSTABLE_LOCAL,
1139 .flags = GENL_ADMIN_PERM,
1140 .policy = batadv_netlink_policy,
1141 .dumpit = batadv_tt_local_dump,
1142 },
1143 {
1144 .cmd = BATADV_CMD_GET_TRANSTABLE_GLOBAL,
1145 .flags = GENL_ADMIN_PERM,
1146 .policy = batadv_netlink_policy,
1147 .dumpit = batadv_tt_global_dump,
1148 },
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001149 {
1150 .cmd = BATADV_CMD_GET_ORIGINATORS,
1151 .flags = GENL_ADMIN_PERM,
1152 .policy = batadv_netlink_policy,
1153 .dumpit = batadv_orig_dump,
1154 },
1155 {
1156 .cmd = BATADV_CMD_GET_NEIGHBORS,
1157 .flags = GENL_ADMIN_PERM,
1158 .policy = batadv_netlink_policy,
1159 .dumpit = batadv_hardif_neigh_dump,
1160 },
Sven Eckelmannd7129da2016-07-03 13:31:42 +02001161 {
1162 .cmd = BATADV_CMD_GET_GATEWAYS,
1163 .flags = GENL_ADMIN_PERM,
1164 .policy = batadv_netlink_policy,
1165 .dumpit = batadv_gw_dump,
1166 },
Andrew Lunn04f3f5b2016-07-03 13:31:45 +02001167 {
1168 .cmd = BATADV_CMD_GET_BLA_CLAIM,
1169 .flags = GENL_ADMIN_PERM,
1170 .policy = batadv_netlink_policy,
1171 .dumpit = batadv_bla_claim_dump,
1172 },
Simon Wunderlichea4152e2016-07-03 13:31:47 +02001173 {
1174 .cmd = BATADV_CMD_GET_BLA_BACKBONE,
1175 .flags = GENL_ADMIN_PERM,
1176 .policy = batadv_netlink_policy,
1177 .dumpit = batadv_bla_backbone_dump,
1178 },
Linus Lüssing41aeefc2018-03-13 11:41:12 +01001179 {
1180 .cmd = BATADV_CMD_GET_DAT_CACHE,
1181 .flags = GENL_ADMIN_PERM,
1182 .policy = batadv_netlink_policy,
1183 .dumpit = batadv_dat_cache_dump,
1184 },
Linus Lüssing53dd9a62018-03-13 11:41:13 +01001185 {
1186 .cmd = BATADV_CMD_GET_MCAST_FLAGS,
1187 .flags = GENL_ADMIN_PERM,
1188 .policy = batadv_netlink_policy,
1189 .dumpit = batadv_mcast_flags_dump,
1190 },
Sven Eckelmann60040512018-11-23 12:14:56 +01001191 {
1192 .cmd = BATADV_CMD_SET_MESH,
1193 .flags = GENL_ADMIN_PERM,
1194 .policy = batadv_netlink_policy,
1195 .doit = batadv_netlink_set_mesh,
1196 .internal_flags = BATADV_FLAG_NEED_MESH,
1197 },
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001198 {
1199 .cmd = BATADV_CMD_SET_HARDIF,
1200 .flags = GENL_ADMIN_PERM,
1201 .policy = batadv_netlink_policy,
1202 .doit = batadv_netlink_set_hardif,
1203 .internal_flags = BATADV_FLAG_NEED_MESH |
1204 BATADV_FLAG_NEED_HARDIF,
1205 },
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001206 {
1207 .cmd = BATADV_CMD_GET_VLAN,
1208 /* can be retrieved by unprivileged users */
1209 .policy = batadv_netlink_policy,
1210 .doit = batadv_netlink_get_vlan,
1211 .internal_flags = BATADV_FLAG_NEED_MESH |
1212 BATADV_FLAG_NEED_VLAN,
1213 },
1214 {
1215 .cmd = BATADV_CMD_SET_VLAN,
1216 .flags = GENL_ADMIN_PERM,
1217 .policy = batadv_netlink_policy,
1218 .doit = batadv_netlink_set_vlan,
1219 .internal_flags = BATADV_FLAG_NEED_MESH |
1220 BATADV_FLAG_NEED_VLAN,
1221 },
Matthias Schiffer09748a22016-05-09 18:41:08 +02001222};
1223
Johannes Berg56989f62016-10-24 14:40:05 +02001224struct genl_family batadv_netlink_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001225 .hdrsize = 0,
1226 .name = BATADV_NL_NAME,
1227 .version = 1,
1228 .maxattr = BATADV_ATTR_MAX,
1229 .netnsok = true,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001230 .pre_doit = batadv_pre_doit,
1231 .post_doit = batadv_post_doit,
Johannes Berg489111e2016-10-24 14:40:03 +02001232 .module = THIS_MODULE,
1233 .ops = batadv_netlink_ops,
1234 .n_ops = ARRAY_SIZE(batadv_netlink_ops),
1235 .mcgrps = batadv_netlink_mcgrps,
1236 .n_mcgrps = ARRAY_SIZE(batadv_netlink_mcgrps),
1237};
1238
Matthias Schiffer09748a22016-05-09 18:41:08 +02001239/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001240 * batadv_netlink_register() - register batadv genl netlink family
Matthias Schiffer09748a22016-05-09 18:41:08 +02001241 */
1242void __init batadv_netlink_register(void)
1243{
1244 int ret;
1245
Johannes Berg489111e2016-10-24 14:40:03 +02001246 ret = genl_register_family(&batadv_netlink_family);
Matthias Schiffer09748a22016-05-09 18:41:08 +02001247 if (ret)
1248 pr_warn("unable to register netlink family");
1249}
1250
1251/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001252 * batadv_netlink_unregister() - unregister batadv genl netlink family
Matthias Schiffer09748a22016-05-09 18:41:08 +02001253 */
1254void batadv_netlink_unregister(void)
1255{
1256 genl_unregister_family(&batadv_netlink_family);
1257}