blob: b6cc746e01a641bfb75eba219ffa0c29b1b20dd5 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmanncfa55c62021-01-01 00:00:01 +01002/* Copyright (C) B.A.T.M.A.N. contributors:
Matthias Schiffer09748a22016-05-09 18:41:08 +02003 *
4 * Matthias Schiffer
Matthias Schiffer09748a22016-05-09 18:41:08 +02005 */
6
7#include "netlink.h"
8#include "main.h"
9
Sven Eckelmannf32ed4b2016-07-03 13:31:38 +020010#include <linux/atomic.h>
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +010011#include <linux/bitops.h>
Sven Eckelmann5c55a402018-11-23 12:33:17 +010012#include <linux/bug.h>
Sven Eckelmann8dad6f0d2016-07-03 13:31:46 +020013#include <linux/byteorder/generic.h>
Sven Eckelmann9bcb94c2016-10-29 10:13:47 +020014#include <linux/cache.h>
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +010015#include <linux/err.h>
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020016#include <linux/errno.h>
Sven Eckelmann9bcb94c2016-10-29 10:13:47 +020017#include <linux/export.h>
Matthias Schiffer09748a22016-05-09 18:41:08 +020018#include <linux/genetlink.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010019#include <linux/gfp.h>
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020020#include <linux/if_ether.h>
Sven Eckelmann49e7e372018-11-23 12:41:08 +010021#include <linux/if_vlan.h>
Matthias Schiffer09748a22016-05-09 18:41:08 +020022#include <linux/init.h>
Sven Eckelmann9bcb94c2016-10-29 10:13:47 +020023#include <linux/kernel.h>
Sven Eckelmanne1928752019-05-24 16:28:50 +020024#include <linux/limits.h>
Sven Eckelmannfb69be62018-10-30 22:01:24 +010025#include <linux/list.h>
Sven Eckelmannfcd193e2020-10-26 21:01:59 +010026#include <linux/minmax.h>
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020027#include <linux/netdevice.h>
28#include <linux/netlink.h>
Matthias Schiffer09748a22016-05-09 18:41:08 +020029#include <linux/printk.h>
Sven Eckelmannfb69be62018-10-30 22:01:24 +010030#include <linux/rtnetlink.h>
Matthias Schifferb60620c2016-07-03 13:31:36 +020031#include <linux/skbuff.h>
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020032#include <linux/stddef.h>
Antonio Quartulli33a3bb42016-05-05 13:09:43 +020033#include <linux/types.h>
Matthias Schiffer09748a22016-05-09 18:41:08 +020034#include <net/genetlink.h>
Sven Eckelmann68a600d2019-05-24 20:11:17 +020035#include <net/net_namespace.h>
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020036#include <net/netlink.h>
Matthias Schifferb60620c2016-07-03 13:31:36 +020037#include <net/sock.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010038#include <uapi/linux/batadv_packet.h>
Matthias Schiffer09748a22016-05-09 18:41:08 +020039#include <uapi/linux/batman_adv.h>
40
Matthias Schiffer07a30612016-07-03 13:31:35 +020041#include "bat_algo.h"
Andrew Lunn04f3f5b2016-07-03 13:31:45 +020042#include "bridge_loop_avoidance.h"
Linus Lüssing41aeefc2018-03-13 11:41:12 +010043#include "distributed-arp-table.h"
Sven Eckelmannd7129da2016-07-03 13:31:42 +020044#include "gateway_client.h"
Sven Eckelmanne2d0d352018-11-23 13:15:00 +010045#include "gateway_common.h"
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020046#include "hard-interface.h"
Sven Eckelmannb85bd092018-11-23 13:22:33 +010047#include "log.h"
Linus Lüssing53dd9a62018-03-13 11:41:13 +010048#include "multicast.h"
Sven Eckelmann6c57cde2018-11-23 13:26:14 +010049#include "network-coding.h"
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020050#include "originator.h"
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020051#include "soft-interface.h"
Antonio Quartulli33a3bb42016-05-05 13:09:43 +020052#include "tp_meter.h"
Matthias Schifferd34f0552016-07-03 13:31:37 +020053#include "translation-table.h"
Matthias Schiffer5da0aef2016-05-09 18:41:09 +020054
Johannes Berg489111e2016-10-24 14:40:03 +020055struct genl_family batadv_netlink_family;
Matthias Schiffer09748a22016-05-09 18:41:08 +020056
Antonio Quartulli33a3bb42016-05-05 13:09:43 +020057/* multicast groups */
58enum batadv_netlink_multicast_groups {
Sven Eckelmann60040512018-11-23 12:14:56 +010059 BATADV_NL_MCGRP_CONFIG,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +020060 BATADV_NL_MCGRP_TPMETER,
61};
62
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +010063/**
64 * enum batadv_genl_ops_flags - flags for genl_ops's internal_flags
65 */
66enum batadv_genl_ops_flags {
67 /**
68 * @BATADV_FLAG_NEED_MESH: request requires valid soft interface in
69 * attribute BATADV_ATTR_MESH_IFINDEX and expects a pointer to it to be
70 * saved in info->user_ptr[0]
71 */
72 BATADV_FLAG_NEED_MESH = BIT(0),
Sven Eckelmann5c55a402018-11-23 12:33:17 +010073
74 /**
75 * @BATADV_FLAG_NEED_HARDIF: request requires valid hard interface in
76 * attribute BATADV_ATTR_HARD_IFINDEX and expects a pointer to it to be
77 * saved in info->user_ptr[1]
78 */
79 BATADV_FLAG_NEED_HARDIF = BIT(1),
Sven Eckelmann49e7e372018-11-23 12:41:08 +010080
81 /**
82 * @BATADV_FLAG_NEED_VLAN: request requires valid vlan in
83 * attribute BATADV_ATTR_VLANID and expects a pointer to it to be
84 * saved in info->user_ptr[1]
85 */
86 BATADV_FLAG_NEED_VLAN = BIT(2),
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +010087};
88
stephen hemmingerdeeb91f2016-08-31 15:17:00 -070089static const struct genl_multicast_group batadv_netlink_mcgrps[] = {
Sven Eckelmann60040512018-11-23 12:14:56 +010090 [BATADV_NL_MCGRP_CONFIG] = { .name = BATADV_NL_MCAST_GROUP_CONFIG },
Antonio Quartulli33a3bb42016-05-05 13:09:43 +020091 [BATADV_NL_MCGRP_TPMETER] = { .name = BATADV_NL_MCAST_GROUP_TPMETER },
92};
93
stephen hemmingerdeeb91f2016-08-31 15:17:00 -070094static const struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = {
Linus Lüssing41aeefc2018-03-13 11:41:12 +010095 [BATADV_ATTR_VERSION] = { .type = NLA_STRING },
96 [BATADV_ATTR_ALGO_NAME] = { .type = NLA_STRING },
97 [BATADV_ATTR_MESH_IFINDEX] = { .type = NLA_U32 },
98 [BATADV_ATTR_MESH_IFNAME] = { .type = NLA_STRING },
99 [BATADV_ATTR_MESH_ADDRESS] = { .len = ETH_ALEN },
100 [BATADV_ATTR_HARD_IFINDEX] = { .type = NLA_U32 },
101 [BATADV_ATTR_HARD_IFNAME] = { .type = NLA_STRING },
102 [BATADV_ATTR_HARD_ADDRESS] = { .len = ETH_ALEN },
103 [BATADV_ATTR_ORIG_ADDRESS] = { .len = ETH_ALEN },
104 [BATADV_ATTR_TPMETER_RESULT] = { .type = NLA_U8 },
105 [BATADV_ATTR_TPMETER_TEST_TIME] = { .type = NLA_U32 },
106 [BATADV_ATTR_TPMETER_BYTES] = { .type = NLA_U64 },
107 [BATADV_ATTR_TPMETER_COOKIE] = { .type = NLA_U32 },
108 [BATADV_ATTR_ACTIVE] = { .type = NLA_FLAG },
109 [BATADV_ATTR_TT_ADDRESS] = { .len = ETH_ALEN },
110 [BATADV_ATTR_TT_TTVN] = { .type = NLA_U8 },
111 [BATADV_ATTR_TT_LAST_TTVN] = { .type = NLA_U8 },
112 [BATADV_ATTR_TT_CRC32] = { .type = NLA_U32 },
113 [BATADV_ATTR_TT_VID] = { .type = NLA_U16 },
114 [BATADV_ATTR_TT_FLAGS] = { .type = NLA_U32 },
115 [BATADV_ATTR_FLAG_BEST] = { .type = NLA_FLAG },
116 [BATADV_ATTR_LAST_SEEN_MSECS] = { .type = NLA_U32 },
117 [BATADV_ATTR_NEIGH_ADDRESS] = { .len = ETH_ALEN },
118 [BATADV_ATTR_TQ] = { .type = NLA_U8 },
119 [BATADV_ATTR_THROUGHPUT] = { .type = NLA_U32 },
120 [BATADV_ATTR_BANDWIDTH_UP] = { .type = NLA_U32 },
121 [BATADV_ATTR_BANDWIDTH_DOWN] = { .type = NLA_U32 },
122 [BATADV_ATTR_ROUTER] = { .len = ETH_ALEN },
123 [BATADV_ATTR_BLA_OWN] = { .type = NLA_FLAG },
124 [BATADV_ATTR_BLA_ADDRESS] = { .len = ETH_ALEN },
125 [BATADV_ATTR_BLA_VID] = { .type = NLA_U16 },
126 [BATADV_ATTR_BLA_BACKBONE] = { .len = ETH_ALEN },
127 [BATADV_ATTR_BLA_CRC] = { .type = NLA_U16 },
128 [BATADV_ATTR_DAT_CACHE_IP4ADDRESS] = { .type = NLA_U32 },
129 [BATADV_ATTR_DAT_CACHE_HWADDRESS] = { .len = ETH_ALEN },
130 [BATADV_ATTR_DAT_CACHE_VID] = { .type = NLA_U16 },
Linus Lüssing53dd9a62018-03-13 11:41:13 +0100131 [BATADV_ATTR_MCAST_FLAGS] = { .type = NLA_U32 },
132 [BATADV_ATTR_MCAST_FLAGS_PRIV] = { .type = NLA_U32 },
Sven Eckelmann49e7e372018-11-23 12:41:08 +0100133 [BATADV_ATTR_VLANID] = { .type = NLA_U16 },
Sven Eckelmann9ab4cee2018-11-23 12:46:14 +0100134 [BATADV_ATTR_AGGREGATED_OGMS_ENABLED] = { .type = NLA_U8 },
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100135 [BATADV_ATTR_AP_ISOLATION_ENABLED] = { .type = NLA_U8 },
136 [BATADV_ATTR_ISOLATION_MARK] = { .type = NLA_U32 },
137 [BATADV_ATTR_ISOLATION_MASK] = { .type = NLA_U32 },
Sven Eckelmannd7e52502018-11-23 12:55:44 +0100138 [BATADV_ATTR_BONDING_ENABLED] = { .type = NLA_U8 },
Sven Eckelmann43ff6102018-11-23 13:03:39 +0100139 [BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED] = { .type = NLA_U8 },
Sven Eckelmanna1c8de82018-11-23 13:06:42 +0100140 [BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED] = { .type = NLA_U8 },
Sven Eckelmann3e15b062018-11-23 13:09:49 +0100141 [BATADV_ATTR_FRAGMENTATION_ENABLED] = { .type = NLA_U8 },
Sven Eckelmanne2d0d352018-11-23 13:15:00 +0100142 [BATADV_ATTR_GW_BANDWIDTH_DOWN] = { .type = NLA_U32 },
143 [BATADV_ATTR_GW_BANDWIDTH_UP] = { .type = NLA_U32 },
144 [BATADV_ATTR_GW_MODE] = { .type = NLA_U8 },
145 [BATADV_ATTR_GW_SEL_CLASS] = { .type = NLA_U32 },
Sven Eckelmannbfc7f1b2018-11-23 13:19:38 +0100146 [BATADV_ATTR_HOP_PENALTY] = { .type = NLA_U8 },
Sven Eckelmannb85bd092018-11-23 13:22:33 +0100147 [BATADV_ATTR_LOG_LEVEL] = { .type = NLA_U32 },
Sven Eckelmannf75b56b2018-11-23 13:25:05 +0100148 [BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED] = { .type = NLA_U8 },
Linus Lüssing32e72742019-03-23 05:47:41 +0100149 [BATADV_ATTR_MULTICAST_FANOUT] = { .type = NLA_U32 },
Sven Eckelmann6c57cde2018-11-23 13:26:14 +0100150 [BATADV_ATTR_NETWORK_CODING_ENABLED] = { .type = NLA_U8 },
Sven Eckelmann7b751b32018-11-23 13:28:02 +0100151 [BATADV_ATTR_ORIG_INTERVAL] = { .type = NLA_U32 },
Sven Eckelmanna1080082018-11-23 13:30:04 +0100152 [BATADV_ATTR_ELP_INTERVAL] = { .type = NLA_U32 },
Sven Eckelmann9a182242018-11-23 13:31:23 +0100153 [BATADV_ATTR_THROUGHPUT_OVERRIDE] = { .type = NLA_U32 },
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200154};
155
156/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100157 * batadv_netlink_get_ifindex() - Extract an interface index from a message
Matthias Schifferb60620c2016-07-03 13:31:36 +0200158 * @nlh: Message header
159 * @attrtype: Attribute which holds an interface index
160 *
161 * Return: interface index, or 0.
162 */
Matthias Schifferd34f0552016-07-03 13:31:37 +0200163int
Matthias Schifferb60620c2016-07-03 13:31:36 +0200164batadv_netlink_get_ifindex(const struct nlmsghdr *nlh, int attrtype)
165{
166 struct nlattr *attr = nlmsg_find_attr(nlh, GENL_HDRLEN, attrtype);
167
Eric Dumazet3ee1bb72019-08-12 04:57:27 -0700168 return (attr && nla_len(attr) == sizeof(u32)) ? nla_get_u32(attr) : 0;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200169}
170
171/**
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100172 * batadv_netlink_mesh_fill_ap_isolation() - Add ap_isolation softif attribute
173 * @msg: Netlink message to dump into
174 * @bat_priv: the bat priv with all the soft interface information
175 *
176 * Return: 0 on success or negative error number in case of failure
177 */
178static int batadv_netlink_mesh_fill_ap_isolation(struct sk_buff *msg,
179 struct batadv_priv *bat_priv)
180{
181 struct batadv_softif_vlan *vlan;
182 u8 ap_isolation;
183
184 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
185 if (!vlan)
186 return 0;
187
188 ap_isolation = atomic_read(&vlan->ap_isolation);
189 batadv_softif_vlan_put(vlan);
190
191 return nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
192 !!ap_isolation);
193}
194
195/**
Sven Eckelmann25d81f92021-01-20 20:50:35 +0100196 * batadv_netlink_set_mesh_ap_isolation() - Set ap_isolation from genl msg
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100197 * @attr: parsed BATADV_ATTR_AP_ISOLATION_ENABLED attribute
198 * @bat_priv: the bat priv with all the soft interface information
199 *
200 * Return: 0 on success or negative error number in case of failure
201 */
202static int batadv_netlink_set_mesh_ap_isolation(struct nlattr *attr,
203 struct batadv_priv *bat_priv)
204{
205 struct batadv_softif_vlan *vlan;
206
207 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
208 if (!vlan)
209 return -ENOENT;
210
211 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
212 batadv_softif_vlan_put(vlan);
213
214 return 0;
215}
216
217/**
Sven Eckelmann60040512018-11-23 12:14:56 +0100218 * batadv_netlink_mesh_fill() - Fill message with mesh attributes
219 * @msg: Netlink message to dump into
220 * @bat_priv: the bat priv with all the soft interface information
221 * @cmd: type of message to generate
222 * @portid: Port making netlink request
223 * @seq: sequence number for message
224 * @flags: Additional flags for message
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200225 *
Sven Eckelmann60040512018-11-23 12:14:56 +0100226 * Return: 0 on success or negative error number in case of failure
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200227 */
Sven Eckelmann60040512018-11-23 12:14:56 +0100228static int batadv_netlink_mesh_fill(struct sk_buff *msg,
229 struct batadv_priv *bat_priv,
230 enum batadv_nl_commands cmd,
231 u32 portid, u32 seq, int flags)
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200232{
Sven Eckelmann60040512018-11-23 12:14:56 +0100233 struct net_device *soft_iface = bat_priv->soft_iface;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200234 struct batadv_hard_iface *primary_if = NULL;
235 struct net_device *hard_iface;
Sven Eckelmann60040512018-11-23 12:14:56 +0100236 void *hdr;
237
238 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
239 if (!hdr)
240 return -ENOBUFS;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200241
242 if (nla_put_string(msg, BATADV_ATTR_VERSION, BATADV_SOURCE_VERSION) ||
243 nla_put_string(msg, BATADV_ATTR_ALGO_NAME,
Antonio Quartulli29824a52016-05-25 23:27:31 +0800244 bat_priv->algo_ops->name) ||
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200245 nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, soft_iface->ifindex) ||
246 nla_put_string(msg, BATADV_ATTR_MESH_IFNAME, soft_iface->name) ||
247 nla_put(msg, BATADV_ATTR_MESH_ADDRESS, ETH_ALEN,
Sven Eckelmannf32ed4b2016-07-03 13:31:38 +0200248 soft_iface->dev_addr) ||
249 nla_put_u8(msg, BATADV_ATTR_TT_TTVN,
250 (u8)atomic_read(&bat_priv->tt.vn)))
Sven Eckelmann60040512018-11-23 12:14:56 +0100251 goto nla_put_failure;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200252
Sven Eckelmann8dad6f0d2016-07-03 13:31:46 +0200253#ifdef CONFIG_BATMAN_ADV_BLA
254 if (nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
255 ntohs(bat_priv->bla.claim_dest.group)))
Sven Eckelmann60040512018-11-23 12:14:56 +0100256 goto nla_put_failure;
Sven Eckelmann8dad6f0d2016-07-03 13:31:46 +0200257#endif
258
Linus Lüssing53dd9a62018-03-13 11:41:13 +0100259 if (batadv_mcast_mesh_info_put(msg, bat_priv))
Sven Eckelmann60040512018-11-23 12:14:56 +0100260 goto nla_put_failure;
Linus Lüssing53dd9a62018-03-13 11:41:13 +0100261
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200262 primary_if = batadv_primary_if_get_selected(bat_priv);
263 if (primary_if && primary_if->if_status == BATADV_IF_ACTIVE) {
264 hard_iface = primary_if->net_dev;
265
266 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
267 hard_iface->ifindex) ||
268 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
269 hard_iface->name) ||
270 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
271 hard_iface->dev_addr))
Sven Eckelmann60040512018-11-23 12:14:56 +0100272 goto nla_put_failure;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200273 }
274
Sven Eckelmann9ab4cee2018-11-23 12:46:14 +0100275 if (nla_put_u8(msg, BATADV_ATTR_AGGREGATED_OGMS_ENABLED,
276 !!atomic_read(&bat_priv->aggregated_ogms)))
277 goto nla_put_failure;
278
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100279 if (batadv_netlink_mesh_fill_ap_isolation(msg, bat_priv))
280 goto nla_put_failure;
281
282 if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MARK,
283 bat_priv->isolation_mark))
284 goto nla_put_failure;
285
286 if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MASK,
287 bat_priv->isolation_mark_mask))
288 goto nla_put_failure;
289
Sven Eckelmannd7e52502018-11-23 12:55:44 +0100290 if (nla_put_u8(msg, BATADV_ATTR_BONDING_ENABLED,
291 !!atomic_read(&bat_priv->bonding)))
292 goto nla_put_failure;
293
Sven Eckelmann43ff6102018-11-23 13:03:39 +0100294#ifdef CONFIG_BATMAN_ADV_BLA
295 if (nla_put_u8(msg, BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED,
296 !!atomic_read(&bat_priv->bridge_loop_avoidance)))
297 goto nla_put_failure;
298#endif /* CONFIG_BATMAN_ADV_BLA */
299
Sven Eckelmanna1c8de82018-11-23 13:06:42 +0100300#ifdef CONFIG_BATMAN_ADV_DAT
301 if (nla_put_u8(msg, BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED,
302 !!atomic_read(&bat_priv->distributed_arp_table)))
303 goto nla_put_failure;
304#endif /* CONFIG_BATMAN_ADV_DAT */
305
Sven Eckelmann3e15b062018-11-23 13:09:49 +0100306 if (nla_put_u8(msg, BATADV_ATTR_FRAGMENTATION_ENABLED,
307 !!atomic_read(&bat_priv->fragmentation)))
308 goto nla_put_failure;
309
Sven Eckelmanne2d0d352018-11-23 13:15:00 +0100310 if (nla_put_u32(msg, BATADV_ATTR_GW_BANDWIDTH_DOWN,
311 atomic_read(&bat_priv->gw.bandwidth_down)))
312 goto nla_put_failure;
313
314 if (nla_put_u32(msg, BATADV_ATTR_GW_BANDWIDTH_UP,
315 atomic_read(&bat_priv->gw.bandwidth_up)))
316 goto nla_put_failure;
317
318 if (nla_put_u8(msg, BATADV_ATTR_GW_MODE,
319 atomic_read(&bat_priv->gw.mode)))
320 goto nla_put_failure;
321
322 if (bat_priv->algo_ops->gw.get_best_gw_node &&
323 bat_priv->algo_ops->gw.is_eligible) {
324 /* GW selection class is not available if the routing algorithm
325 * in use does not implement the GW API
326 */
327 if (nla_put_u32(msg, BATADV_ATTR_GW_SEL_CLASS,
328 atomic_read(&bat_priv->gw.sel_class)))
329 goto nla_put_failure;
330 }
331
Sven Eckelmannbfc7f1b2018-11-23 13:19:38 +0100332 if (nla_put_u8(msg, BATADV_ATTR_HOP_PENALTY,
333 atomic_read(&bat_priv->hop_penalty)))
334 goto nla_put_failure;
335
Sven Eckelmannb85bd092018-11-23 13:22:33 +0100336#ifdef CONFIG_BATMAN_ADV_DEBUG
337 if (nla_put_u32(msg, BATADV_ATTR_LOG_LEVEL,
338 atomic_read(&bat_priv->log_level)))
339 goto nla_put_failure;
340#endif /* CONFIG_BATMAN_ADV_DEBUG */
341
Sven Eckelmannf75b56b2018-11-23 13:25:05 +0100342#ifdef CONFIG_BATMAN_ADV_MCAST
343 if (nla_put_u8(msg, BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED,
344 !atomic_read(&bat_priv->multicast_mode)))
345 goto nla_put_failure;
Linus Lüssing32e72742019-03-23 05:47:41 +0100346
347 if (nla_put_u32(msg, BATADV_ATTR_MULTICAST_FANOUT,
348 atomic_read(&bat_priv->multicast_fanout)))
349 goto nla_put_failure;
Sven Eckelmannf75b56b2018-11-23 13:25:05 +0100350#endif /* CONFIG_BATMAN_ADV_MCAST */
351
Sven Eckelmann6c57cde2018-11-23 13:26:14 +0100352#ifdef CONFIG_BATMAN_ADV_NC
353 if (nla_put_u8(msg, BATADV_ATTR_NETWORK_CODING_ENABLED,
354 !!atomic_read(&bat_priv->network_coding)))
355 goto nla_put_failure;
356#endif /* CONFIG_BATMAN_ADV_NC */
357
Sven Eckelmann7b751b32018-11-23 13:28:02 +0100358 if (nla_put_u32(msg, BATADV_ATTR_ORIG_INTERVAL,
359 atomic_read(&bat_priv->orig_interval)))
360 goto nla_put_failure;
361
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200362 if (primary_if)
363 batadv_hardif_put(primary_if);
364
Sven Eckelmann60040512018-11-23 12:14:56 +0100365 genlmsg_end(msg, hdr);
366 return 0;
367
368nla_put_failure:
369 if (primary_if)
370 batadv_hardif_put(primary_if);
371
372 genlmsg_cancel(msg, hdr);
373 return -EMSGSIZE;
374}
375
376/**
377 * batadv_netlink_notify_mesh() - send softif attributes to listener
378 * @bat_priv: the bat priv with all the soft interface information
379 *
380 * Return: 0 on success, < 0 on error
381 */
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100382int batadv_netlink_notify_mesh(struct batadv_priv *bat_priv)
Sven Eckelmann60040512018-11-23 12:14:56 +0100383{
384 struct sk_buff *msg;
385 int ret;
386
387 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
388 if (!msg)
389 return -ENOMEM;
390
391 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_SET_MESH,
392 0, 0, 0);
393 if (ret < 0) {
394 nlmsg_free(msg);
395 return ret;
396 }
397
398 genlmsg_multicast_netns(&batadv_netlink_family,
399 dev_net(bat_priv->soft_iface), msg, 0,
400 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
401
402 return 0;
403}
404
405/**
406 * batadv_netlink_get_mesh() - Get softif attributes
407 * @skb: Netlink message with request data
408 * @info: receiver information
409 *
410 * Return: 0 on success or negative error number in case of failure
411 */
412static int batadv_netlink_get_mesh(struct sk_buff *skb, struct genl_info *info)
413{
414 struct batadv_priv *bat_priv = info->user_ptr[0];
415 struct sk_buff *msg;
416 int ret;
417
418 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
419 if (!msg)
420 return -ENOMEM;
421
422 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_GET_MESH,
423 info->snd_portid, info->snd_seq, 0);
424 if (ret < 0) {
425 nlmsg_free(msg);
426 return ret;
427 }
428
429 ret = genlmsg_reply(msg, info);
430
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200431 return ret;
432}
433
434/**
Sven Eckelmann60040512018-11-23 12:14:56 +0100435 * batadv_netlink_set_mesh() - Set softif attributes
436 * @skb: Netlink message with request data
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200437 * @info: receiver information
438 *
Sven Eckelmann60040512018-11-23 12:14:56 +0100439 * Return: 0 on success or negative error number in case of failure
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200440 */
Sven Eckelmann60040512018-11-23 12:14:56 +0100441static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info)
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200442{
Sven Eckelmann60040512018-11-23 12:14:56 +0100443 struct batadv_priv *bat_priv = info->user_ptr[0];
Sven Eckelmann9ab4cee2018-11-23 12:46:14 +0100444 struct nlattr *attr;
445
446 if (info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED]) {
447 attr = info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED];
448
449 atomic_set(&bat_priv->aggregated_ogms, !!nla_get_u8(attr));
450 }
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200451
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100452 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
453 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
454
455 batadv_netlink_set_mesh_ap_isolation(attr, bat_priv);
456 }
457
458 if (info->attrs[BATADV_ATTR_ISOLATION_MARK]) {
459 attr = info->attrs[BATADV_ATTR_ISOLATION_MARK];
460
461 bat_priv->isolation_mark = nla_get_u32(attr);
462 }
463
464 if (info->attrs[BATADV_ATTR_ISOLATION_MASK]) {
465 attr = info->attrs[BATADV_ATTR_ISOLATION_MASK];
466
467 bat_priv->isolation_mark_mask = nla_get_u32(attr);
468 }
469
Sven Eckelmannd7e52502018-11-23 12:55:44 +0100470 if (info->attrs[BATADV_ATTR_BONDING_ENABLED]) {
471 attr = info->attrs[BATADV_ATTR_BONDING_ENABLED];
472
473 atomic_set(&bat_priv->bonding, !!nla_get_u8(attr));
474 }
475
Sven Eckelmann43ff6102018-11-23 13:03:39 +0100476#ifdef CONFIG_BATMAN_ADV_BLA
477 if (info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED]) {
478 attr = info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED];
479
480 atomic_set(&bat_priv->bridge_loop_avoidance,
481 !!nla_get_u8(attr));
482 batadv_bla_status_update(bat_priv->soft_iface);
483 }
484#endif /* CONFIG_BATMAN_ADV_BLA */
485
Sven Eckelmanna1c8de82018-11-23 13:06:42 +0100486#ifdef CONFIG_BATMAN_ADV_DAT
487 if (info->attrs[BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED]) {
488 attr = info->attrs[BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED];
489
490 atomic_set(&bat_priv->distributed_arp_table,
491 !!nla_get_u8(attr));
492 batadv_dat_status_update(bat_priv->soft_iface);
493 }
494#endif /* CONFIG_BATMAN_ADV_DAT */
495
Sven Eckelmann3e15b062018-11-23 13:09:49 +0100496 if (info->attrs[BATADV_ATTR_FRAGMENTATION_ENABLED]) {
497 attr = info->attrs[BATADV_ATTR_FRAGMENTATION_ENABLED];
498
499 atomic_set(&bat_priv->fragmentation, !!nla_get_u8(attr));
500 batadv_update_min_mtu(bat_priv->soft_iface);
501 }
502
Sven Eckelmanne2d0d352018-11-23 13:15:00 +0100503 if (info->attrs[BATADV_ATTR_GW_BANDWIDTH_DOWN]) {
504 attr = info->attrs[BATADV_ATTR_GW_BANDWIDTH_DOWN];
505
506 atomic_set(&bat_priv->gw.bandwidth_down, nla_get_u32(attr));
507 batadv_gw_tvlv_container_update(bat_priv);
508 }
509
510 if (info->attrs[BATADV_ATTR_GW_BANDWIDTH_UP]) {
511 attr = info->attrs[BATADV_ATTR_GW_BANDWIDTH_UP];
512
513 atomic_set(&bat_priv->gw.bandwidth_up, nla_get_u32(attr));
514 batadv_gw_tvlv_container_update(bat_priv);
515 }
516
517 if (info->attrs[BATADV_ATTR_GW_MODE]) {
518 u8 gw_mode;
519
520 attr = info->attrs[BATADV_ATTR_GW_MODE];
521 gw_mode = nla_get_u8(attr);
522
523 if (gw_mode <= BATADV_GW_MODE_SERVER) {
524 /* Invoking batadv_gw_reselect() is not enough to really
525 * de-select the current GW. It will only instruct the
526 * gateway client code to perform a re-election the next
527 * time that this is needed.
528 *
529 * When gw client mode is being switched off the current
530 * GW must be de-selected explicitly otherwise no GW_ADD
531 * uevent is thrown on client mode re-activation. This
532 * is operation is performed in
533 * batadv_gw_check_client_stop().
534 */
535 batadv_gw_reselect(bat_priv);
536
537 /* always call batadv_gw_check_client_stop() before
538 * changing the gateway state
539 */
540 batadv_gw_check_client_stop(bat_priv);
541 atomic_set(&bat_priv->gw.mode, gw_mode);
542 batadv_gw_tvlv_container_update(bat_priv);
543 }
544 }
545
546 if (info->attrs[BATADV_ATTR_GW_SEL_CLASS] &&
547 bat_priv->algo_ops->gw.get_best_gw_node &&
548 bat_priv->algo_ops->gw.is_eligible) {
549 /* setting the GW selection class is allowed only if the routing
550 * algorithm in use implements the GW API
551 */
552
553 u32 sel_class_max = 0xffffffffu;
554 u32 sel_class;
555
556 attr = info->attrs[BATADV_ATTR_GW_SEL_CLASS];
557 sel_class = nla_get_u32(attr);
558
559 if (!bat_priv->algo_ops->gw.store_sel_class)
560 sel_class_max = BATADV_TQ_MAX_VALUE;
561
562 if (sel_class >= 1 && sel_class <= sel_class_max) {
563 atomic_set(&bat_priv->gw.sel_class, sel_class);
564 batadv_gw_reselect(bat_priv);
565 }
566 }
567
Sven Eckelmannbfc7f1b2018-11-23 13:19:38 +0100568 if (info->attrs[BATADV_ATTR_HOP_PENALTY]) {
569 attr = info->attrs[BATADV_ATTR_HOP_PENALTY];
570
571 atomic_set(&bat_priv->hop_penalty, nla_get_u8(attr));
572 }
573
Sven Eckelmannb85bd092018-11-23 13:22:33 +0100574#ifdef CONFIG_BATMAN_ADV_DEBUG
575 if (info->attrs[BATADV_ATTR_LOG_LEVEL]) {
576 attr = info->attrs[BATADV_ATTR_LOG_LEVEL];
577
578 atomic_set(&bat_priv->log_level,
579 nla_get_u32(attr) & BATADV_DBG_ALL);
580 }
581#endif /* CONFIG_BATMAN_ADV_DEBUG */
582
Sven Eckelmannf75b56b2018-11-23 13:25:05 +0100583#ifdef CONFIG_BATMAN_ADV_MCAST
584 if (info->attrs[BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED]) {
585 attr = info->attrs[BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED];
586
587 atomic_set(&bat_priv->multicast_mode, !nla_get_u8(attr));
588 }
Linus Lüssing32e72742019-03-23 05:47:41 +0100589
590 if (info->attrs[BATADV_ATTR_MULTICAST_FANOUT]) {
591 attr = info->attrs[BATADV_ATTR_MULTICAST_FANOUT];
592
593 atomic_set(&bat_priv->multicast_fanout, nla_get_u32(attr));
594 }
Sven Eckelmannf75b56b2018-11-23 13:25:05 +0100595#endif /* CONFIG_BATMAN_ADV_MCAST */
596
Sven Eckelmann6c57cde2018-11-23 13:26:14 +0100597#ifdef CONFIG_BATMAN_ADV_NC
598 if (info->attrs[BATADV_ATTR_NETWORK_CODING_ENABLED]) {
599 attr = info->attrs[BATADV_ATTR_NETWORK_CODING_ENABLED];
600
601 atomic_set(&bat_priv->network_coding, !!nla_get_u8(attr));
602 batadv_nc_status_update(bat_priv->soft_iface);
603 }
604#endif /* CONFIG_BATMAN_ADV_NC */
605
Sven Eckelmann7b751b32018-11-23 13:28:02 +0100606 if (info->attrs[BATADV_ATTR_ORIG_INTERVAL]) {
607 u32 orig_interval;
608
609 attr = info->attrs[BATADV_ATTR_ORIG_INTERVAL];
610 orig_interval = nla_get_u32(attr);
611
612 orig_interval = min_t(u32, orig_interval, INT_MAX);
613 orig_interval = max_t(u32, orig_interval, 2 * BATADV_JITTER);
614
615 atomic_set(&bat_priv->orig_interval, orig_interval);
616 }
617
Sven Eckelmann60040512018-11-23 12:14:56 +0100618 batadv_netlink_notify_mesh(bat_priv);
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200619
Sven Eckelmann60040512018-11-23 12:14:56 +0100620 return 0;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200621}
622
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200623/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100624 * batadv_netlink_tp_meter_put() - Fill information of started tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200625 * @msg: netlink message to be sent back
626 * @cookie: tp meter session cookie
627 *
628 * Return: 0 on success, < 0 on error
629 */
630static int
631batadv_netlink_tp_meter_put(struct sk_buff *msg, u32 cookie)
632{
633 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
634 return -ENOBUFS;
635
636 return 0;
637}
638
639/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100640 * batadv_netlink_tpmeter_notify() - send tp_meter result via netlink to client
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200641 * @bat_priv: the bat priv with all the soft interface information
642 * @dst: destination of tp_meter session
643 * @result: reason for tp meter session stop
Sven Eckelmannbccb48c2020-06-01 20:13:21 +0200644 * @test_time: total time of the tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200645 * @total_bytes: bytes acked to the receiver
646 * @cookie: cookie of tp_meter session
647 *
648 * Return: 0 on success, < 0 on error
649 */
650int batadv_netlink_tpmeter_notify(struct batadv_priv *bat_priv, const u8 *dst,
651 u8 result, u32 test_time, u64 total_bytes,
652 u32 cookie)
653{
654 struct sk_buff *msg;
655 void *hdr;
656 int ret;
657
658 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
659 if (!msg)
660 return -ENOMEM;
661
662 hdr = genlmsg_put(msg, 0, 0, &batadv_netlink_family, 0,
663 BATADV_CMD_TP_METER);
664 if (!hdr) {
665 ret = -ENOBUFS;
666 goto err_genlmsg;
667 }
668
669 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
670 goto nla_put_failure;
671
672 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, test_time))
673 goto nla_put_failure;
674
675 if (nla_put_u64_64bit(msg, BATADV_ATTR_TPMETER_BYTES, total_bytes,
676 BATADV_ATTR_PAD))
677 goto nla_put_failure;
678
679 if (nla_put_u8(msg, BATADV_ATTR_TPMETER_RESULT, result))
680 goto nla_put_failure;
681
682 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst))
683 goto nla_put_failure;
684
685 genlmsg_end(msg, hdr);
686
687 genlmsg_multicast_netns(&batadv_netlink_family,
688 dev_net(bat_priv->soft_iface), msg, 0,
689 BATADV_NL_MCGRP_TPMETER, GFP_KERNEL);
690
691 return 0;
692
693nla_put_failure:
694 genlmsg_cancel(msg, hdr);
695 ret = -EMSGSIZE;
696
697err_genlmsg:
698 nlmsg_free(msg);
699 return ret;
700}
701
702/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100703 * batadv_netlink_tp_meter_start() - Start a new tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200704 * @skb: received netlink message
705 * @info: receiver information
706 *
707 * Return: 0 on success, < 0 on error
708 */
709static int
710batadv_netlink_tp_meter_start(struct sk_buff *skb, struct genl_info *info)
711{
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100712 struct batadv_priv *bat_priv = info->user_ptr[0];
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200713 struct sk_buff *msg = NULL;
714 u32 test_length;
715 void *msg_head;
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200716 u32 cookie;
717 u8 *dst;
718 int ret;
719
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200720 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
721 return -EINVAL;
722
723 if (!info->attrs[BATADV_ATTR_TPMETER_TEST_TIME])
724 return -EINVAL;
725
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200726 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
727
728 test_length = nla_get_u32(info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]);
729
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200730 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
731 if (!msg) {
732 ret = -ENOMEM;
733 goto out;
734 }
735
736 msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq,
737 &batadv_netlink_family, 0,
738 BATADV_CMD_TP_METER);
739 if (!msg_head) {
740 ret = -ENOBUFS;
741 goto out;
742 }
743
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200744 batadv_tp_start(bat_priv, dst, test_length, &cookie);
745
746 ret = batadv_netlink_tp_meter_put(msg, cookie);
747
748 out:
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200749 if (ret) {
750 if (msg)
751 nlmsg_free(msg);
752 return ret;
753 }
754
755 genlmsg_end(msg, msg_head);
756 return genlmsg_reply(msg, info);
757}
758
759/**
Sven Eckelmann25d81f92021-01-20 20:50:35 +0100760 * batadv_netlink_tp_meter_cancel() - Cancel a running tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200761 * @skb: received netlink message
762 * @info: receiver information
763 *
764 * Return: 0 on success, < 0 on error
765 */
766static int
767batadv_netlink_tp_meter_cancel(struct sk_buff *skb, struct genl_info *info)
768{
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100769 struct batadv_priv *bat_priv = info->user_ptr[0];
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200770 u8 *dst;
771 int ret = 0;
772
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200773 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
774 return -EINVAL;
775
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200776 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
777
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200778 batadv_tp_stop(bat_priv, dst, BATADV_TP_REASON_CANCEL);
779
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200780 return ret;
781}
782
Matthias Schifferb60620c2016-07-03 13:31:36 +0200783/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100784 * batadv_netlink_hardif_fill() - Fill message with hardif attributes
Matthias Schifferb60620c2016-07-03 13:31:36 +0200785 * @msg: Netlink message to dump into
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100786 * @bat_priv: the bat priv with all the soft interface information
787 * @hard_iface: hard interface which was modified
788 * @cmd: type of message to generate
Matthias Schifferb60620c2016-07-03 13:31:36 +0200789 * @portid: Port making netlink request
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100790 * @seq: sequence number for message
791 * @flags: Additional flags for message
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100792 * @cb: Control block containing additional options
Matthias Schifferb60620c2016-07-03 13:31:36 +0200793 *
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100794 * Return: 0 on success or negative error number in case of failure
Matthias Schifferb60620c2016-07-03 13:31:36 +0200795 */
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100796static int batadv_netlink_hardif_fill(struct sk_buff *msg,
797 struct batadv_priv *bat_priv,
798 struct batadv_hard_iface *hard_iface,
799 enum batadv_nl_commands cmd,
800 u32 portid, u32 seq, int flags,
801 struct netlink_callback *cb)
Matthias Schifferb60620c2016-07-03 13:31:36 +0200802{
803 struct net_device *net_dev = hard_iface->net_dev;
804 void *hdr;
805
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100806 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
Matthias Schifferb60620c2016-07-03 13:31:36 +0200807 if (!hdr)
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100808 return -ENOBUFS;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200809
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100810 if (cb)
811 genl_dump_check_consistent(cb, hdr);
812
813 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
814 bat_priv->soft_iface->ifindex))
815 goto nla_put_failure;
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100816
Sven Eckelmannd2953452021-05-10 15:05:42 +0200817 if (nla_put_string(msg, BATADV_ATTR_MESH_IFNAME,
818 bat_priv->soft_iface->name))
819 goto nla_put_failure;
820
Matthias Schifferb60620c2016-07-03 13:31:36 +0200821 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
822 net_dev->ifindex) ||
823 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
824 net_dev->name) ||
825 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
826 net_dev->dev_addr))
827 goto nla_put_failure;
828
829 if (hard_iface->if_status == BATADV_IF_ACTIVE) {
830 if (nla_put_flag(msg, BATADV_ATTR_ACTIVE))
831 goto nla_put_failure;
832 }
833
Linus Lüssing3bda14d2020-06-01 22:35:22 +0200834 if (nla_put_u8(msg, BATADV_ATTR_HOP_PENALTY,
835 atomic_read(&hard_iface->hop_penalty)))
836 goto nla_put_failure;
837
Sven Eckelmanna1080082018-11-23 13:30:04 +0100838#ifdef CONFIG_BATMAN_ADV_BATMAN_V
839 if (nla_put_u32(msg, BATADV_ATTR_ELP_INTERVAL,
840 atomic_read(&hard_iface->bat_v.elp_interval)))
841 goto nla_put_failure;
Sven Eckelmann9a182242018-11-23 13:31:23 +0100842
843 if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT_OVERRIDE,
844 atomic_read(&hard_iface->bat_v.throughput_override)))
845 goto nla_put_failure;
Sven Eckelmanna1080082018-11-23 13:30:04 +0100846#endif /* CONFIG_BATMAN_ADV_BATMAN_V */
847
Matthias Schifferb60620c2016-07-03 13:31:36 +0200848 genlmsg_end(msg, hdr);
849 return 0;
850
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100851nla_put_failure:
Matthias Schifferb60620c2016-07-03 13:31:36 +0200852 genlmsg_cancel(msg, hdr);
853 return -EMSGSIZE;
854}
855
856/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100857 * batadv_netlink_notify_hardif() - send hardif attributes to listener
858 * @bat_priv: the bat priv with all the soft interface information
859 * @hard_iface: hard interface which was modified
860 *
861 * Return: 0 on success, < 0 on error
862 */
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100863int batadv_netlink_notify_hardif(struct batadv_priv *bat_priv,
864 struct batadv_hard_iface *hard_iface)
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100865{
866 struct sk_buff *msg;
867 int ret;
868
869 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
870 if (!msg)
871 return -ENOMEM;
872
873 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
874 BATADV_CMD_SET_HARDIF, 0, 0, 0, NULL);
875 if (ret < 0) {
876 nlmsg_free(msg);
877 return ret;
878 }
879
880 genlmsg_multicast_netns(&batadv_netlink_family,
881 dev_net(bat_priv->soft_iface), msg, 0,
882 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
883
884 return 0;
885}
886
887/**
888 * batadv_netlink_get_hardif() - Get hardif attributes
889 * @skb: Netlink message with request data
890 * @info: receiver information
891 *
892 * Return: 0 on success or negative error number in case of failure
893 */
894static int batadv_netlink_get_hardif(struct sk_buff *skb,
895 struct genl_info *info)
896{
897 struct batadv_hard_iface *hard_iface = info->user_ptr[1];
898 struct batadv_priv *bat_priv = info->user_ptr[0];
899 struct sk_buff *msg;
900 int ret;
901
902 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
903 if (!msg)
904 return -ENOMEM;
905
906 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
907 BATADV_CMD_GET_HARDIF,
908 info->snd_portid, info->snd_seq, 0,
909 NULL);
910 if (ret < 0) {
911 nlmsg_free(msg);
912 return ret;
913 }
914
915 ret = genlmsg_reply(msg, info);
916
917 return ret;
918}
919
920/**
921 * batadv_netlink_set_hardif() - Set hardif attributes
922 * @skb: Netlink message with request data
923 * @info: receiver information
924 *
925 * Return: 0 on success or negative error number in case of failure
926 */
927static int batadv_netlink_set_hardif(struct sk_buff *skb,
928 struct genl_info *info)
929{
930 struct batadv_hard_iface *hard_iface = info->user_ptr[1];
931 struct batadv_priv *bat_priv = info->user_ptr[0];
Linus Lüssing3bda14d2020-06-01 22:35:22 +0200932 struct nlattr *attr;
933
934 if (info->attrs[BATADV_ATTR_HOP_PENALTY]) {
935 attr = info->attrs[BATADV_ATTR_HOP_PENALTY];
936
937 atomic_set(&hard_iface->hop_penalty, nla_get_u8(attr));
938 }
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100939
Sven Eckelmanna1080082018-11-23 13:30:04 +0100940#ifdef CONFIG_BATMAN_ADV_BATMAN_V
Sven Eckelmanna1080082018-11-23 13:30:04 +0100941
942 if (info->attrs[BATADV_ATTR_ELP_INTERVAL]) {
943 attr = info->attrs[BATADV_ATTR_ELP_INTERVAL];
944
945 atomic_set(&hard_iface->bat_v.elp_interval, nla_get_u32(attr));
946 }
Sven Eckelmann9a182242018-11-23 13:31:23 +0100947
948 if (info->attrs[BATADV_ATTR_THROUGHPUT_OVERRIDE]) {
949 attr = info->attrs[BATADV_ATTR_THROUGHPUT_OVERRIDE];
950
951 atomic_set(&hard_iface->bat_v.throughput_override,
952 nla_get_u32(attr));
953 }
Sven Eckelmanna1080082018-11-23 13:30:04 +0100954#endif /* CONFIG_BATMAN_ADV_BATMAN_V */
955
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100956 batadv_netlink_notify_hardif(bat_priv, hard_iface);
957
958 return 0;
959}
960
961/**
962 * batadv_netlink_dump_hardif() - Dump all hard interface into a messages
Matthias Schifferb60620c2016-07-03 13:31:36 +0200963 * @msg: Netlink message to dump into
964 * @cb: Parameters from query
965 *
966 * Return: error code, or length of reply message on success
967 */
968static int
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100969batadv_netlink_dump_hardif(struct sk_buff *msg, struct netlink_callback *cb)
Matthias Schifferb60620c2016-07-03 13:31:36 +0200970{
971 struct net *net = sock_net(cb->skb->sk);
972 struct net_device *soft_iface;
973 struct batadv_hard_iface *hard_iface;
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100974 struct batadv_priv *bat_priv;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200975 int ifindex;
976 int portid = NETLINK_CB(cb->skb).portid;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200977 int skip = cb->args[0];
978 int i = 0;
979
980 ifindex = batadv_netlink_get_ifindex(cb->nlh,
981 BATADV_ATTR_MESH_IFINDEX);
982 if (!ifindex)
983 return -EINVAL;
984
985 soft_iface = dev_get_by_index(net, ifindex);
986 if (!soft_iface)
987 return -ENODEV;
988
989 if (!batadv_softif_is_valid(soft_iface)) {
990 dev_put(soft_iface);
991 return -ENODEV;
992 }
993
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100994 bat_priv = netdev_priv(soft_iface);
995
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100996 rtnl_lock();
997 cb->seq = batadv_hardif_generation << 1 | 1;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200998
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100999 list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
Matthias Schifferb60620c2016-07-03 13:31:36 +02001000 if (hard_iface->soft_iface != soft_iface)
1001 continue;
1002
1003 if (i++ < skip)
1004 continue;
1005
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001006 if (batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
1007 BATADV_CMD_GET_HARDIF,
1008 portid, cb->nlh->nlmsg_seq,
1009 NLM_F_MULTI, cb)) {
Matthias Schifferb60620c2016-07-03 13:31:36 +02001010 i--;
1011 break;
1012 }
1013 }
1014
Sven Eckelmannfb69be62018-10-30 22:01:24 +01001015 rtnl_unlock();
Matthias Schifferb60620c2016-07-03 13:31:36 +02001016
1017 dev_put(soft_iface);
1018
1019 cb->args[0] = i;
1020
1021 return msg->len;
1022}
1023
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001024/**
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001025 * batadv_netlink_vlan_fill() - Fill message with vlan attributes
1026 * @msg: Netlink message to dump into
1027 * @bat_priv: the bat priv with all the soft interface information
1028 * @vlan: vlan which was modified
1029 * @cmd: type of message to generate
1030 * @portid: Port making netlink request
1031 * @seq: sequence number for message
1032 * @flags: Additional flags for message
1033 *
1034 * Return: 0 on success or negative error number in case of failure
1035 */
1036static int batadv_netlink_vlan_fill(struct sk_buff *msg,
1037 struct batadv_priv *bat_priv,
1038 struct batadv_softif_vlan *vlan,
1039 enum batadv_nl_commands cmd,
1040 u32 portid, u32 seq, int flags)
1041{
1042 void *hdr;
1043
1044 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
1045 if (!hdr)
1046 return -ENOBUFS;
1047
1048 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
1049 bat_priv->soft_iface->ifindex))
1050 goto nla_put_failure;
1051
Sven Eckelmannd2953452021-05-10 15:05:42 +02001052 if (nla_put_string(msg, BATADV_ATTR_MESH_IFNAME,
1053 bat_priv->soft_iface->name))
1054 goto nla_put_failure;
1055
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001056 if (nla_put_u32(msg, BATADV_ATTR_VLANID, vlan->vid & VLAN_VID_MASK))
1057 goto nla_put_failure;
1058
Sven Eckelmanne43d16b2018-11-23 12:51:55 +01001059 if (nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
1060 !!atomic_read(&vlan->ap_isolation)))
1061 goto nla_put_failure;
1062
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001063 genlmsg_end(msg, hdr);
1064 return 0;
1065
1066nla_put_failure:
1067 genlmsg_cancel(msg, hdr);
1068 return -EMSGSIZE;
1069}
1070
1071/**
1072 * batadv_netlink_notify_vlan() - send vlan attributes to listener
1073 * @bat_priv: the bat priv with all the soft interface information
1074 * @vlan: vlan which was modified
1075 *
1076 * Return: 0 on success, < 0 on error
1077 */
Sven Eckelmann7e6f4612018-11-23 16:07:12 +01001078int batadv_netlink_notify_vlan(struct batadv_priv *bat_priv,
1079 struct batadv_softif_vlan *vlan)
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001080{
1081 struct sk_buff *msg;
1082 int ret;
1083
1084 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1085 if (!msg)
1086 return -ENOMEM;
1087
1088 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan,
1089 BATADV_CMD_SET_VLAN, 0, 0, 0);
1090 if (ret < 0) {
1091 nlmsg_free(msg);
1092 return ret;
1093 }
1094
1095 genlmsg_multicast_netns(&batadv_netlink_family,
1096 dev_net(bat_priv->soft_iface), msg, 0,
1097 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
1098
1099 return 0;
1100}
1101
1102/**
1103 * batadv_netlink_get_vlan() - Get vlan attributes
1104 * @skb: Netlink message with request data
1105 * @info: receiver information
1106 *
1107 * Return: 0 on success or negative error number in case of failure
1108 */
1109static int batadv_netlink_get_vlan(struct sk_buff *skb, struct genl_info *info)
1110{
1111 struct batadv_softif_vlan *vlan = info->user_ptr[1];
1112 struct batadv_priv *bat_priv = info->user_ptr[0];
1113 struct sk_buff *msg;
1114 int ret;
1115
1116 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1117 if (!msg)
1118 return -ENOMEM;
1119
1120 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan, BATADV_CMD_GET_VLAN,
1121 info->snd_portid, info->snd_seq, 0);
1122 if (ret < 0) {
1123 nlmsg_free(msg);
1124 return ret;
1125 }
1126
1127 ret = genlmsg_reply(msg, info);
1128
1129 return ret;
1130}
1131
1132/**
1133 * batadv_netlink_set_vlan() - Get vlan attributes
1134 * @skb: Netlink message with request data
1135 * @info: receiver information
1136 *
1137 * Return: 0 on success or negative error number in case of failure
1138 */
1139static int batadv_netlink_set_vlan(struct sk_buff *skb, struct genl_info *info)
1140{
1141 struct batadv_softif_vlan *vlan = info->user_ptr[1];
1142 struct batadv_priv *bat_priv = info->user_ptr[0];
Sven Eckelmanne43d16b2018-11-23 12:51:55 +01001143 struct nlattr *attr;
1144
1145 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
1146 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
1147
1148 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
1149 }
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001150
1151 batadv_netlink_notify_vlan(bat_priv, vlan);
1152
1153 return 0;
1154}
1155
1156/**
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001157 * batadv_get_softif_from_info() - Retrieve soft interface from genl attributes
1158 * @net: the applicable net namespace
1159 * @info: receiver information
1160 *
1161 * Return: Pointer to soft interface (with increased refcnt) on success, error
1162 * pointer on error
1163 */
1164static struct net_device *
1165batadv_get_softif_from_info(struct net *net, struct genl_info *info)
1166{
1167 struct net_device *soft_iface;
1168 int ifindex;
1169
1170 if (!info->attrs[BATADV_ATTR_MESH_IFINDEX])
1171 return ERR_PTR(-EINVAL);
1172
1173 ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
1174
1175 soft_iface = dev_get_by_index(net, ifindex);
1176 if (!soft_iface)
1177 return ERR_PTR(-ENODEV);
1178
1179 if (!batadv_softif_is_valid(soft_iface))
1180 goto err_put_softif;
1181
1182 return soft_iface;
1183
1184err_put_softif:
1185 dev_put(soft_iface);
1186
1187 return ERR_PTR(-EINVAL);
1188}
1189
1190/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001191 * batadv_get_hardif_from_info() - Retrieve hardif from genl attributes
1192 * @bat_priv: the bat priv with all the soft interface information
1193 * @net: the applicable net namespace
1194 * @info: receiver information
1195 *
1196 * Return: Pointer to hard interface (with increased refcnt) on success, error
1197 * pointer on error
1198 */
1199static struct batadv_hard_iface *
1200batadv_get_hardif_from_info(struct batadv_priv *bat_priv, struct net *net,
1201 struct genl_info *info)
1202{
1203 struct batadv_hard_iface *hard_iface;
1204 struct net_device *hard_dev;
1205 unsigned int hardif_index;
1206
1207 if (!info->attrs[BATADV_ATTR_HARD_IFINDEX])
1208 return ERR_PTR(-EINVAL);
1209
1210 hardif_index = nla_get_u32(info->attrs[BATADV_ATTR_HARD_IFINDEX]);
1211
1212 hard_dev = dev_get_by_index(net, hardif_index);
1213 if (!hard_dev)
1214 return ERR_PTR(-ENODEV);
1215
1216 hard_iface = batadv_hardif_get_by_netdev(hard_dev);
1217 if (!hard_iface)
1218 goto err_put_harddev;
1219
1220 if (hard_iface->soft_iface != bat_priv->soft_iface)
1221 goto err_put_hardif;
1222
1223 /* hard_dev is referenced by hard_iface and not needed here */
1224 dev_put(hard_dev);
1225
1226 return hard_iface;
1227
1228err_put_hardif:
1229 batadv_hardif_put(hard_iface);
1230err_put_harddev:
1231 dev_put(hard_dev);
1232
1233 return ERR_PTR(-EINVAL);
1234}
1235
1236/**
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001237 * batadv_get_vlan_from_info() - Retrieve vlan from genl attributes
1238 * @bat_priv: the bat priv with all the soft interface information
1239 * @net: the applicable net namespace
1240 * @info: receiver information
1241 *
1242 * Return: Pointer to vlan on success (with increased refcnt), error pointer
1243 * on error
1244 */
1245static struct batadv_softif_vlan *
1246batadv_get_vlan_from_info(struct batadv_priv *bat_priv, struct net *net,
1247 struct genl_info *info)
1248{
1249 struct batadv_softif_vlan *vlan;
1250 u16 vid;
1251
1252 if (!info->attrs[BATADV_ATTR_VLANID])
1253 return ERR_PTR(-EINVAL);
1254
1255 vid = nla_get_u16(info->attrs[BATADV_ATTR_VLANID]);
1256
1257 vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
1258 if (!vlan)
1259 return ERR_PTR(-ENOENT);
1260
1261 return vlan;
1262}
1263
1264/**
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001265 * batadv_pre_doit() - Prepare batman-adv genl doit request
1266 * @ops: requested netlink operation
1267 * @skb: Netlink message with request data
1268 * @info: receiver information
1269 *
1270 * Return: 0 on success or negative error number in case of failure
1271 */
1272static int batadv_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
1273 struct genl_info *info)
1274{
1275 struct net *net = genl_info_net(info);
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001276 struct batadv_hard_iface *hard_iface;
1277 struct batadv_priv *bat_priv = NULL;
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001278 struct batadv_softif_vlan *vlan;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001279 struct net_device *soft_iface;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001280 u8 user_ptr1_flags;
1281 u8 mesh_dep_flags;
1282 int ret;
1283
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001284 user_ptr1_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001285 if (WARN_ON(hweight8(ops->internal_flags & user_ptr1_flags) > 1))
1286 return -EINVAL;
1287
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001288 mesh_dep_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001289 if (WARN_ON((ops->internal_flags & mesh_dep_flags) &&
1290 (~ops->internal_flags & BATADV_FLAG_NEED_MESH)))
1291 return -EINVAL;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001292
1293 if (ops->internal_flags & BATADV_FLAG_NEED_MESH) {
1294 soft_iface = batadv_get_softif_from_info(net, info);
1295 if (IS_ERR(soft_iface))
1296 return PTR_ERR(soft_iface);
1297
1298 bat_priv = netdev_priv(soft_iface);
1299 info->user_ptr[0] = bat_priv;
1300 }
1301
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001302 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF) {
1303 hard_iface = batadv_get_hardif_from_info(bat_priv, net, info);
1304 if (IS_ERR(hard_iface)) {
1305 ret = PTR_ERR(hard_iface);
1306 goto err_put_softif;
1307 }
1308
1309 info->user_ptr[1] = hard_iface;
1310 }
1311
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001312 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN) {
1313 vlan = batadv_get_vlan_from_info(bat_priv, net, info);
1314 if (IS_ERR(vlan)) {
1315 ret = PTR_ERR(vlan);
1316 goto err_put_softif;
1317 }
1318
1319 info->user_ptr[1] = vlan;
1320 }
1321
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001322 return 0;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001323
1324err_put_softif:
1325 if (bat_priv)
1326 dev_put(bat_priv->soft_iface);
1327
1328 return ret;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001329}
1330
1331/**
1332 * batadv_post_doit() - End batman-adv genl doit request
1333 * @ops: requested netlink operation
1334 * @skb: Netlink message with request data
1335 * @info: receiver information
1336 */
1337static void batadv_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
1338 struct genl_info *info)
1339{
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001340 struct batadv_hard_iface *hard_iface;
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001341 struct batadv_softif_vlan *vlan;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001342 struct batadv_priv *bat_priv;
1343
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001344 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF &&
1345 info->user_ptr[1]) {
1346 hard_iface = info->user_ptr[1];
1347
1348 batadv_hardif_put(hard_iface);
1349 }
1350
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001351 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN && info->user_ptr[1]) {
1352 vlan = info->user_ptr[1];
1353 batadv_softif_vlan_put(vlan);
1354 }
1355
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001356 if (ops->internal_flags & BATADV_FLAG_NEED_MESH && info->user_ptr[0]) {
1357 bat_priv = info->user_ptr[0];
1358 dev_put(bat_priv->soft_iface);
1359 }
1360}
1361
Jakub Kicinski66a9b922020-10-02 14:49:54 -07001362static const struct genl_small_ops batadv_netlink_ops[] = {
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001363 {
Sven Eckelmann60040512018-11-23 12:14:56 +01001364 .cmd = BATADV_CMD_GET_MESH,
Johannes Bergef6243a2019-04-26 14:07:31 +02001365 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmann60040512018-11-23 12:14:56 +01001366 /* can be retrieved by unprivileged users */
Sven Eckelmann60040512018-11-23 12:14:56 +01001367 .doit = batadv_netlink_get_mesh,
1368 .internal_flags = BATADV_FLAG_NEED_MESH,
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001369 },
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001370 {
1371 .cmd = BATADV_CMD_TP_METER,
Johannes Bergef6243a2019-04-26 14:07:31 +02001372 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001373 .flags = GENL_ADMIN_PERM,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001374 .doit = batadv_netlink_tp_meter_start,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001375 .internal_flags = BATADV_FLAG_NEED_MESH,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001376 },
1377 {
1378 .cmd = BATADV_CMD_TP_METER_CANCEL,
Johannes Bergef6243a2019-04-26 14:07:31 +02001379 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001380 .flags = GENL_ADMIN_PERM,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001381 .doit = batadv_netlink_tp_meter_cancel,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001382 .internal_flags = BATADV_FLAG_NEED_MESH,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001383 },
Matthias Schiffer07a30612016-07-03 13:31:35 +02001384 {
1385 .cmd = BATADV_CMD_GET_ROUTING_ALGOS,
Johannes Bergef6243a2019-04-26 14:07:31 +02001386 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Matthias Schiffer07a30612016-07-03 13:31:35 +02001387 .flags = GENL_ADMIN_PERM,
Matthias Schiffer07a30612016-07-03 13:31:35 +02001388 .dumpit = batadv_algo_dump,
1389 },
Matthias Schifferb60620c2016-07-03 13:31:36 +02001390 {
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001391 .cmd = BATADV_CMD_GET_HARDIF,
Johannes Bergef6243a2019-04-26 14:07:31 +02001392 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001393 /* can be retrieved by unprivileged users */
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001394 .dumpit = batadv_netlink_dump_hardif,
1395 .doit = batadv_netlink_get_hardif,
1396 .internal_flags = BATADV_FLAG_NEED_MESH |
1397 BATADV_FLAG_NEED_HARDIF,
Matthias Schifferb60620c2016-07-03 13:31:36 +02001398 },
Matthias Schifferd34f0552016-07-03 13:31:37 +02001399 {
1400 .cmd = BATADV_CMD_GET_TRANSTABLE_LOCAL,
Johannes Bergef6243a2019-04-26 14:07:31 +02001401 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Matthias Schifferd34f0552016-07-03 13:31:37 +02001402 .flags = GENL_ADMIN_PERM,
Matthias Schifferd34f0552016-07-03 13:31:37 +02001403 .dumpit = batadv_tt_local_dump,
1404 },
1405 {
1406 .cmd = BATADV_CMD_GET_TRANSTABLE_GLOBAL,
Johannes Bergef6243a2019-04-26 14:07:31 +02001407 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Matthias Schifferd34f0552016-07-03 13:31:37 +02001408 .flags = GENL_ADMIN_PERM,
Matthias Schifferd34f0552016-07-03 13:31:37 +02001409 .dumpit = batadv_tt_global_dump,
1410 },
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001411 {
1412 .cmd = BATADV_CMD_GET_ORIGINATORS,
Johannes Bergef6243a2019-04-26 14:07:31 +02001413 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001414 .flags = GENL_ADMIN_PERM,
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001415 .dumpit = batadv_orig_dump,
1416 },
1417 {
1418 .cmd = BATADV_CMD_GET_NEIGHBORS,
Johannes Bergef6243a2019-04-26 14:07:31 +02001419 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001420 .flags = GENL_ADMIN_PERM,
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001421 .dumpit = batadv_hardif_neigh_dump,
1422 },
Sven Eckelmannd7129da2016-07-03 13:31:42 +02001423 {
1424 .cmd = BATADV_CMD_GET_GATEWAYS,
Johannes Bergef6243a2019-04-26 14:07:31 +02001425 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmannd7129da2016-07-03 13:31:42 +02001426 .flags = GENL_ADMIN_PERM,
Sven Eckelmannd7129da2016-07-03 13:31:42 +02001427 .dumpit = batadv_gw_dump,
1428 },
Andrew Lunn04f3f5b2016-07-03 13:31:45 +02001429 {
1430 .cmd = BATADV_CMD_GET_BLA_CLAIM,
Johannes Bergef6243a2019-04-26 14:07:31 +02001431 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andrew Lunn04f3f5b2016-07-03 13:31:45 +02001432 .flags = GENL_ADMIN_PERM,
Andrew Lunn04f3f5b2016-07-03 13:31:45 +02001433 .dumpit = batadv_bla_claim_dump,
1434 },
Simon Wunderlichea4152e2016-07-03 13:31:47 +02001435 {
1436 .cmd = BATADV_CMD_GET_BLA_BACKBONE,
Johannes Bergef6243a2019-04-26 14:07:31 +02001437 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Simon Wunderlichea4152e2016-07-03 13:31:47 +02001438 .flags = GENL_ADMIN_PERM,
Simon Wunderlichea4152e2016-07-03 13:31:47 +02001439 .dumpit = batadv_bla_backbone_dump,
1440 },
Linus Lüssing41aeefc2018-03-13 11:41:12 +01001441 {
1442 .cmd = BATADV_CMD_GET_DAT_CACHE,
Johannes Bergef6243a2019-04-26 14:07:31 +02001443 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Linus Lüssing41aeefc2018-03-13 11:41:12 +01001444 .flags = GENL_ADMIN_PERM,
Linus Lüssing41aeefc2018-03-13 11:41:12 +01001445 .dumpit = batadv_dat_cache_dump,
1446 },
Linus Lüssing53dd9a62018-03-13 11:41:13 +01001447 {
1448 .cmd = BATADV_CMD_GET_MCAST_FLAGS,
Johannes Bergef6243a2019-04-26 14:07:31 +02001449 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Linus Lüssing53dd9a62018-03-13 11:41:13 +01001450 .flags = GENL_ADMIN_PERM,
Linus Lüssing53dd9a62018-03-13 11:41:13 +01001451 .dumpit = batadv_mcast_flags_dump,
1452 },
Sven Eckelmann60040512018-11-23 12:14:56 +01001453 {
1454 .cmd = BATADV_CMD_SET_MESH,
Johannes Bergef6243a2019-04-26 14:07:31 +02001455 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmann60040512018-11-23 12:14:56 +01001456 .flags = GENL_ADMIN_PERM,
Sven Eckelmann60040512018-11-23 12:14:56 +01001457 .doit = batadv_netlink_set_mesh,
1458 .internal_flags = BATADV_FLAG_NEED_MESH,
1459 },
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001460 {
1461 .cmd = BATADV_CMD_SET_HARDIF,
Johannes Bergef6243a2019-04-26 14:07:31 +02001462 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001463 .flags = GENL_ADMIN_PERM,
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001464 .doit = batadv_netlink_set_hardif,
1465 .internal_flags = BATADV_FLAG_NEED_MESH |
1466 BATADV_FLAG_NEED_HARDIF,
1467 },
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001468 {
1469 .cmd = BATADV_CMD_GET_VLAN,
Johannes Bergef6243a2019-04-26 14:07:31 +02001470 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001471 /* can be retrieved by unprivileged users */
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001472 .doit = batadv_netlink_get_vlan,
1473 .internal_flags = BATADV_FLAG_NEED_MESH |
1474 BATADV_FLAG_NEED_VLAN,
1475 },
1476 {
1477 .cmd = BATADV_CMD_SET_VLAN,
Johannes Bergef6243a2019-04-26 14:07:31 +02001478 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001479 .flags = GENL_ADMIN_PERM,
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001480 .doit = batadv_netlink_set_vlan,
1481 .internal_flags = BATADV_FLAG_NEED_MESH |
1482 BATADV_FLAG_NEED_VLAN,
1483 },
Matthias Schiffer09748a22016-05-09 18:41:08 +02001484};
1485
Johannes Berg56989f62016-10-24 14:40:05 +02001486struct genl_family batadv_netlink_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001487 .hdrsize = 0,
1488 .name = BATADV_NL_NAME,
1489 .version = 1,
1490 .maxattr = BATADV_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01001491 .policy = batadv_netlink_policy,
Johannes Berg489111e2016-10-24 14:40:03 +02001492 .netnsok = true,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001493 .pre_doit = batadv_pre_doit,
1494 .post_doit = batadv_post_doit,
Johannes Berg489111e2016-10-24 14:40:03 +02001495 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -07001496 .small_ops = batadv_netlink_ops,
1497 .n_small_ops = ARRAY_SIZE(batadv_netlink_ops),
Johannes Berg489111e2016-10-24 14:40:03 +02001498 .mcgrps = batadv_netlink_mcgrps,
1499 .n_mcgrps = ARRAY_SIZE(batadv_netlink_mcgrps),
1500};
1501
Matthias Schiffer09748a22016-05-09 18:41:08 +02001502/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001503 * batadv_netlink_register() - register batadv genl netlink family
Matthias Schiffer09748a22016-05-09 18:41:08 +02001504 */
1505void __init batadv_netlink_register(void)
1506{
1507 int ret;
1508
Johannes Berg489111e2016-10-24 14:40:03 +02001509 ret = genl_register_family(&batadv_netlink_family);
Matthias Schiffer09748a22016-05-09 18:41:08 +02001510 if (ret)
1511 pr_warn("unable to register netlink family");
1512}
1513
1514/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001515 * batadv_netlink_unregister() - unregister batadv genl netlink family
Matthias Schiffer09748a22016-05-09 18:41:08 +02001516 */
1517void batadv_netlink_unregister(void)
1518{
1519 genl_unregister_family(&batadv_netlink_family);
1520}