blob: 29276284d281cb80fb9021e404542d87ed6a38fd [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
Sven Eckelmann79a0bff2021-08-08 19:11:08 +0200362 batadv_hardif_put(primary_if);
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200363
Sven Eckelmann60040512018-11-23 12:14:56 +0100364 genlmsg_end(msg, hdr);
365 return 0;
366
367nla_put_failure:
Sven Eckelmann79a0bff2021-08-08 19:11:08 +0200368 batadv_hardif_put(primary_if);
Sven Eckelmann60040512018-11-23 12:14:56 +0100369
370 genlmsg_cancel(msg, hdr);
371 return -EMSGSIZE;
372}
373
374/**
375 * batadv_netlink_notify_mesh() - send softif attributes to listener
376 * @bat_priv: the bat priv with all the soft interface information
377 *
378 * Return: 0 on success, < 0 on error
379 */
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100380int batadv_netlink_notify_mesh(struct batadv_priv *bat_priv)
Sven Eckelmann60040512018-11-23 12:14:56 +0100381{
382 struct sk_buff *msg;
383 int ret;
384
385 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
386 if (!msg)
387 return -ENOMEM;
388
389 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_SET_MESH,
390 0, 0, 0);
391 if (ret < 0) {
392 nlmsg_free(msg);
393 return ret;
394 }
395
396 genlmsg_multicast_netns(&batadv_netlink_family,
397 dev_net(bat_priv->soft_iface), msg, 0,
398 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
399
400 return 0;
401}
402
403/**
404 * batadv_netlink_get_mesh() - Get softif attributes
405 * @skb: Netlink message with request data
406 * @info: receiver information
407 *
408 * Return: 0 on success or negative error number in case of failure
409 */
410static int batadv_netlink_get_mesh(struct sk_buff *skb, struct genl_info *info)
411{
412 struct batadv_priv *bat_priv = info->user_ptr[0];
413 struct sk_buff *msg;
414 int ret;
415
416 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
417 if (!msg)
418 return -ENOMEM;
419
420 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_GET_MESH,
421 info->snd_portid, info->snd_seq, 0);
422 if (ret < 0) {
423 nlmsg_free(msg);
424 return ret;
425 }
426
427 ret = genlmsg_reply(msg, info);
428
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200429 return ret;
430}
431
432/**
Sven Eckelmann60040512018-11-23 12:14:56 +0100433 * batadv_netlink_set_mesh() - Set softif attributes
434 * @skb: Netlink message with request data
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200435 * @info: receiver information
436 *
Sven Eckelmann60040512018-11-23 12:14:56 +0100437 * Return: 0 on success or negative error number in case of failure
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200438 */
Sven Eckelmann60040512018-11-23 12:14:56 +0100439static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info)
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200440{
Sven Eckelmann60040512018-11-23 12:14:56 +0100441 struct batadv_priv *bat_priv = info->user_ptr[0];
Sven Eckelmann9ab4cee2018-11-23 12:46:14 +0100442 struct nlattr *attr;
443
444 if (info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED]) {
445 attr = info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED];
446
447 atomic_set(&bat_priv->aggregated_ogms, !!nla_get_u8(attr));
448 }
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200449
Sven Eckelmanne43d16b2018-11-23 12:51:55 +0100450 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
451 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
452
453 batadv_netlink_set_mesh_ap_isolation(attr, bat_priv);
454 }
455
456 if (info->attrs[BATADV_ATTR_ISOLATION_MARK]) {
457 attr = info->attrs[BATADV_ATTR_ISOLATION_MARK];
458
459 bat_priv->isolation_mark = nla_get_u32(attr);
460 }
461
462 if (info->attrs[BATADV_ATTR_ISOLATION_MASK]) {
463 attr = info->attrs[BATADV_ATTR_ISOLATION_MASK];
464
465 bat_priv->isolation_mark_mask = nla_get_u32(attr);
466 }
467
Sven Eckelmannd7e52502018-11-23 12:55:44 +0100468 if (info->attrs[BATADV_ATTR_BONDING_ENABLED]) {
469 attr = info->attrs[BATADV_ATTR_BONDING_ENABLED];
470
471 atomic_set(&bat_priv->bonding, !!nla_get_u8(attr));
472 }
473
Sven Eckelmann43ff6102018-11-23 13:03:39 +0100474#ifdef CONFIG_BATMAN_ADV_BLA
475 if (info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED]) {
476 attr = info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED];
477
478 atomic_set(&bat_priv->bridge_loop_avoidance,
479 !!nla_get_u8(attr));
480 batadv_bla_status_update(bat_priv->soft_iface);
481 }
482#endif /* CONFIG_BATMAN_ADV_BLA */
483
Sven Eckelmanna1c8de82018-11-23 13:06:42 +0100484#ifdef CONFIG_BATMAN_ADV_DAT
485 if (info->attrs[BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED]) {
486 attr = info->attrs[BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED];
487
488 atomic_set(&bat_priv->distributed_arp_table,
489 !!nla_get_u8(attr));
490 batadv_dat_status_update(bat_priv->soft_iface);
491 }
492#endif /* CONFIG_BATMAN_ADV_DAT */
493
Sven Eckelmann3e15b062018-11-23 13:09:49 +0100494 if (info->attrs[BATADV_ATTR_FRAGMENTATION_ENABLED]) {
495 attr = info->attrs[BATADV_ATTR_FRAGMENTATION_ENABLED];
496
497 atomic_set(&bat_priv->fragmentation, !!nla_get_u8(attr));
498 batadv_update_min_mtu(bat_priv->soft_iface);
499 }
500
Sven Eckelmanne2d0d352018-11-23 13:15:00 +0100501 if (info->attrs[BATADV_ATTR_GW_BANDWIDTH_DOWN]) {
502 attr = info->attrs[BATADV_ATTR_GW_BANDWIDTH_DOWN];
503
504 atomic_set(&bat_priv->gw.bandwidth_down, nla_get_u32(attr));
505 batadv_gw_tvlv_container_update(bat_priv);
506 }
507
508 if (info->attrs[BATADV_ATTR_GW_BANDWIDTH_UP]) {
509 attr = info->attrs[BATADV_ATTR_GW_BANDWIDTH_UP];
510
511 atomic_set(&bat_priv->gw.bandwidth_up, nla_get_u32(attr));
512 batadv_gw_tvlv_container_update(bat_priv);
513 }
514
515 if (info->attrs[BATADV_ATTR_GW_MODE]) {
516 u8 gw_mode;
517
518 attr = info->attrs[BATADV_ATTR_GW_MODE];
519 gw_mode = nla_get_u8(attr);
520
521 if (gw_mode <= BATADV_GW_MODE_SERVER) {
522 /* Invoking batadv_gw_reselect() is not enough to really
523 * de-select the current GW. It will only instruct the
524 * gateway client code to perform a re-election the next
525 * time that this is needed.
526 *
527 * When gw client mode is being switched off the current
528 * GW must be de-selected explicitly otherwise no GW_ADD
529 * uevent is thrown on client mode re-activation. This
530 * is operation is performed in
531 * batadv_gw_check_client_stop().
532 */
533 batadv_gw_reselect(bat_priv);
534
535 /* always call batadv_gw_check_client_stop() before
536 * changing the gateway state
537 */
538 batadv_gw_check_client_stop(bat_priv);
539 atomic_set(&bat_priv->gw.mode, gw_mode);
540 batadv_gw_tvlv_container_update(bat_priv);
541 }
542 }
543
544 if (info->attrs[BATADV_ATTR_GW_SEL_CLASS] &&
545 bat_priv->algo_ops->gw.get_best_gw_node &&
546 bat_priv->algo_ops->gw.is_eligible) {
547 /* setting the GW selection class is allowed only if the routing
548 * algorithm in use implements the GW API
549 */
550
551 u32 sel_class_max = 0xffffffffu;
552 u32 sel_class;
553
554 attr = info->attrs[BATADV_ATTR_GW_SEL_CLASS];
555 sel_class = nla_get_u32(attr);
556
557 if (!bat_priv->algo_ops->gw.store_sel_class)
558 sel_class_max = BATADV_TQ_MAX_VALUE;
559
560 if (sel_class >= 1 && sel_class <= sel_class_max) {
561 atomic_set(&bat_priv->gw.sel_class, sel_class);
562 batadv_gw_reselect(bat_priv);
563 }
564 }
565
Sven Eckelmannbfc7f1b2018-11-23 13:19:38 +0100566 if (info->attrs[BATADV_ATTR_HOP_PENALTY]) {
567 attr = info->attrs[BATADV_ATTR_HOP_PENALTY];
568
569 atomic_set(&bat_priv->hop_penalty, nla_get_u8(attr));
570 }
571
Sven Eckelmannb85bd092018-11-23 13:22:33 +0100572#ifdef CONFIG_BATMAN_ADV_DEBUG
573 if (info->attrs[BATADV_ATTR_LOG_LEVEL]) {
574 attr = info->attrs[BATADV_ATTR_LOG_LEVEL];
575
576 atomic_set(&bat_priv->log_level,
577 nla_get_u32(attr) & BATADV_DBG_ALL);
578 }
579#endif /* CONFIG_BATMAN_ADV_DEBUG */
580
Sven Eckelmannf75b56b2018-11-23 13:25:05 +0100581#ifdef CONFIG_BATMAN_ADV_MCAST
582 if (info->attrs[BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED]) {
583 attr = info->attrs[BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED];
584
585 atomic_set(&bat_priv->multicast_mode, !nla_get_u8(attr));
586 }
Linus Lüssing32e72742019-03-23 05:47:41 +0100587
588 if (info->attrs[BATADV_ATTR_MULTICAST_FANOUT]) {
589 attr = info->attrs[BATADV_ATTR_MULTICAST_FANOUT];
590
591 atomic_set(&bat_priv->multicast_fanout, nla_get_u32(attr));
592 }
Sven Eckelmannf75b56b2018-11-23 13:25:05 +0100593#endif /* CONFIG_BATMAN_ADV_MCAST */
594
Sven Eckelmann6c57cde2018-11-23 13:26:14 +0100595#ifdef CONFIG_BATMAN_ADV_NC
596 if (info->attrs[BATADV_ATTR_NETWORK_CODING_ENABLED]) {
597 attr = info->attrs[BATADV_ATTR_NETWORK_CODING_ENABLED];
598
599 atomic_set(&bat_priv->network_coding, !!nla_get_u8(attr));
600 batadv_nc_status_update(bat_priv->soft_iface);
601 }
602#endif /* CONFIG_BATMAN_ADV_NC */
603
Sven Eckelmann7b751b32018-11-23 13:28:02 +0100604 if (info->attrs[BATADV_ATTR_ORIG_INTERVAL]) {
605 u32 orig_interval;
606
607 attr = info->attrs[BATADV_ATTR_ORIG_INTERVAL];
608 orig_interval = nla_get_u32(attr);
609
610 orig_interval = min_t(u32, orig_interval, INT_MAX);
611 orig_interval = max_t(u32, orig_interval, 2 * BATADV_JITTER);
612
613 atomic_set(&bat_priv->orig_interval, orig_interval);
614 }
615
Sven Eckelmann60040512018-11-23 12:14:56 +0100616 batadv_netlink_notify_mesh(bat_priv);
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200617
Sven Eckelmann60040512018-11-23 12:14:56 +0100618 return 0;
Matthias Schiffer5da0aef2016-05-09 18:41:09 +0200619}
620
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200621/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100622 * batadv_netlink_tp_meter_put() - Fill information of started tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200623 * @msg: netlink message to be sent back
624 * @cookie: tp meter session cookie
625 *
626 * Return: 0 on success, < 0 on error
627 */
628static int
629batadv_netlink_tp_meter_put(struct sk_buff *msg, u32 cookie)
630{
631 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
632 return -ENOBUFS;
633
634 return 0;
635}
636
637/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100638 * batadv_netlink_tpmeter_notify() - send tp_meter result via netlink to client
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200639 * @bat_priv: the bat priv with all the soft interface information
640 * @dst: destination of tp_meter session
641 * @result: reason for tp meter session stop
Sven Eckelmannbccb48c2020-06-01 20:13:21 +0200642 * @test_time: total time of the tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200643 * @total_bytes: bytes acked to the receiver
644 * @cookie: cookie of tp_meter session
645 *
646 * Return: 0 on success, < 0 on error
647 */
648int batadv_netlink_tpmeter_notify(struct batadv_priv *bat_priv, const u8 *dst,
649 u8 result, u32 test_time, u64 total_bytes,
650 u32 cookie)
651{
652 struct sk_buff *msg;
653 void *hdr;
654 int ret;
655
656 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
657 if (!msg)
658 return -ENOMEM;
659
660 hdr = genlmsg_put(msg, 0, 0, &batadv_netlink_family, 0,
661 BATADV_CMD_TP_METER);
662 if (!hdr) {
663 ret = -ENOBUFS;
664 goto err_genlmsg;
665 }
666
667 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
668 goto nla_put_failure;
669
670 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, test_time))
671 goto nla_put_failure;
672
673 if (nla_put_u64_64bit(msg, BATADV_ATTR_TPMETER_BYTES, total_bytes,
674 BATADV_ATTR_PAD))
675 goto nla_put_failure;
676
677 if (nla_put_u8(msg, BATADV_ATTR_TPMETER_RESULT, result))
678 goto nla_put_failure;
679
680 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst))
681 goto nla_put_failure;
682
683 genlmsg_end(msg, hdr);
684
685 genlmsg_multicast_netns(&batadv_netlink_family,
686 dev_net(bat_priv->soft_iface), msg, 0,
687 BATADV_NL_MCGRP_TPMETER, GFP_KERNEL);
688
689 return 0;
690
691nla_put_failure:
692 genlmsg_cancel(msg, hdr);
693 ret = -EMSGSIZE;
694
695err_genlmsg:
696 nlmsg_free(msg);
697 return ret;
698}
699
700/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100701 * batadv_netlink_tp_meter_start() - Start a new tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200702 * @skb: received netlink message
703 * @info: receiver information
704 *
705 * Return: 0 on success, < 0 on error
706 */
707static int
708batadv_netlink_tp_meter_start(struct sk_buff *skb, struct genl_info *info)
709{
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100710 struct batadv_priv *bat_priv = info->user_ptr[0];
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200711 struct sk_buff *msg = NULL;
712 u32 test_length;
713 void *msg_head;
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200714 u32 cookie;
715 u8 *dst;
716 int ret;
717
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200718 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
719 return -EINVAL;
720
721 if (!info->attrs[BATADV_ATTR_TPMETER_TEST_TIME])
722 return -EINVAL;
723
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200724 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
725
726 test_length = nla_get_u32(info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]);
727
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200728 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
729 if (!msg) {
730 ret = -ENOMEM;
731 goto out;
732 }
733
734 msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq,
735 &batadv_netlink_family, 0,
736 BATADV_CMD_TP_METER);
737 if (!msg_head) {
738 ret = -ENOBUFS;
739 goto out;
740 }
741
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200742 batadv_tp_start(bat_priv, dst, test_length, &cookie);
743
744 ret = batadv_netlink_tp_meter_put(msg, cookie);
745
746 out:
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200747 if (ret) {
748 if (msg)
749 nlmsg_free(msg);
750 return ret;
751 }
752
753 genlmsg_end(msg, msg_head);
754 return genlmsg_reply(msg, info);
755}
756
757/**
Sven Eckelmann25d81f92021-01-20 20:50:35 +0100758 * batadv_netlink_tp_meter_cancel() - Cancel a running tp_meter session
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200759 * @skb: received netlink message
760 * @info: receiver information
761 *
762 * Return: 0 on success, < 0 on error
763 */
764static int
765batadv_netlink_tp_meter_cancel(struct sk_buff *skb, struct genl_info *info)
766{
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +0100767 struct batadv_priv *bat_priv = info->user_ptr[0];
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200768 u8 *dst;
769 int ret = 0;
770
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200771 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
772 return -EINVAL;
773
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200774 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
775
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200776 batadv_tp_stop(bat_priv, dst, BATADV_TP_REASON_CANCEL);
777
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200778 return ret;
779}
780
Matthias Schifferb60620c2016-07-03 13:31:36 +0200781/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100782 * batadv_netlink_hardif_fill() - Fill message with hardif attributes
Matthias Schifferb60620c2016-07-03 13:31:36 +0200783 * @msg: Netlink message to dump into
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100784 * @bat_priv: the bat priv with all the soft interface information
785 * @hard_iface: hard interface which was modified
786 * @cmd: type of message to generate
Matthias Schifferb60620c2016-07-03 13:31:36 +0200787 * @portid: Port making netlink request
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100788 * @seq: sequence number for message
789 * @flags: Additional flags for message
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100790 * @cb: Control block containing additional options
Matthias Schifferb60620c2016-07-03 13:31:36 +0200791 *
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100792 * Return: 0 on success or negative error number in case of failure
Matthias Schifferb60620c2016-07-03 13:31:36 +0200793 */
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100794static int batadv_netlink_hardif_fill(struct sk_buff *msg,
795 struct batadv_priv *bat_priv,
796 struct batadv_hard_iface *hard_iface,
797 enum batadv_nl_commands cmd,
798 u32 portid, u32 seq, int flags,
799 struct netlink_callback *cb)
Matthias Schifferb60620c2016-07-03 13:31:36 +0200800{
801 struct net_device *net_dev = hard_iface->net_dev;
802 void *hdr;
803
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100804 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
Matthias Schifferb60620c2016-07-03 13:31:36 +0200805 if (!hdr)
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100806 return -ENOBUFS;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200807
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100808 if (cb)
809 genl_dump_check_consistent(cb, hdr);
810
811 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
812 bat_priv->soft_iface->ifindex))
813 goto nla_put_failure;
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100814
Sven Eckelmannd2953452021-05-10 15:05:42 +0200815 if (nla_put_string(msg, BATADV_ATTR_MESH_IFNAME,
816 bat_priv->soft_iface->name))
817 goto nla_put_failure;
818
Matthias Schifferb60620c2016-07-03 13:31:36 +0200819 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
820 net_dev->ifindex) ||
821 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
822 net_dev->name) ||
823 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
824 net_dev->dev_addr))
825 goto nla_put_failure;
826
827 if (hard_iface->if_status == BATADV_IF_ACTIVE) {
828 if (nla_put_flag(msg, BATADV_ATTR_ACTIVE))
829 goto nla_put_failure;
830 }
831
Linus Lüssing3bda14d2020-06-01 22:35:22 +0200832 if (nla_put_u8(msg, BATADV_ATTR_HOP_PENALTY,
833 atomic_read(&hard_iface->hop_penalty)))
834 goto nla_put_failure;
835
Sven Eckelmanna1080082018-11-23 13:30:04 +0100836#ifdef CONFIG_BATMAN_ADV_BATMAN_V
837 if (nla_put_u32(msg, BATADV_ATTR_ELP_INTERVAL,
838 atomic_read(&hard_iface->bat_v.elp_interval)))
839 goto nla_put_failure;
Sven Eckelmann9a182242018-11-23 13:31:23 +0100840
841 if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT_OVERRIDE,
842 atomic_read(&hard_iface->bat_v.throughput_override)))
843 goto nla_put_failure;
Sven Eckelmanna1080082018-11-23 13:30:04 +0100844#endif /* CONFIG_BATMAN_ADV_BATMAN_V */
845
Matthias Schifferb60620c2016-07-03 13:31:36 +0200846 genlmsg_end(msg, hdr);
847 return 0;
848
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100849nla_put_failure:
Matthias Schifferb60620c2016-07-03 13:31:36 +0200850 genlmsg_cancel(msg, hdr);
851 return -EMSGSIZE;
852}
853
854/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100855 * batadv_netlink_notify_hardif() - send hardif attributes to listener
856 * @bat_priv: the bat priv with all the soft interface information
857 * @hard_iface: hard interface which was modified
858 *
859 * Return: 0 on success, < 0 on error
860 */
Sven Eckelmann7e6f4612018-11-23 16:07:12 +0100861int batadv_netlink_notify_hardif(struct batadv_priv *bat_priv,
862 struct batadv_hard_iface *hard_iface)
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100863{
864 struct sk_buff *msg;
865 int ret;
866
867 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
868 if (!msg)
869 return -ENOMEM;
870
871 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
872 BATADV_CMD_SET_HARDIF, 0, 0, 0, NULL);
873 if (ret < 0) {
874 nlmsg_free(msg);
875 return ret;
876 }
877
878 genlmsg_multicast_netns(&batadv_netlink_family,
879 dev_net(bat_priv->soft_iface), msg, 0,
880 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
881
882 return 0;
883}
884
885/**
886 * batadv_netlink_get_hardif() - Get hardif attributes
887 * @skb: Netlink message with request data
888 * @info: receiver information
889 *
890 * Return: 0 on success or negative error number in case of failure
891 */
892static int batadv_netlink_get_hardif(struct sk_buff *skb,
893 struct genl_info *info)
894{
895 struct batadv_hard_iface *hard_iface = info->user_ptr[1];
896 struct batadv_priv *bat_priv = info->user_ptr[0];
897 struct sk_buff *msg;
898 int ret;
899
900 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
901 if (!msg)
902 return -ENOMEM;
903
904 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
905 BATADV_CMD_GET_HARDIF,
906 info->snd_portid, info->snd_seq, 0,
907 NULL);
908 if (ret < 0) {
909 nlmsg_free(msg);
910 return ret;
911 }
912
913 ret = genlmsg_reply(msg, info);
914
915 return ret;
916}
917
918/**
919 * batadv_netlink_set_hardif() - Set hardif attributes
920 * @skb: Netlink message with request data
921 * @info: receiver information
922 *
923 * Return: 0 on success or negative error number in case of failure
924 */
925static int batadv_netlink_set_hardif(struct sk_buff *skb,
926 struct genl_info *info)
927{
928 struct batadv_hard_iface *hard_iface = info->user_ptr[1];
929 struct batadv_priv *bat_priv = info->user_ptr[0];
Linus Lüssing3bda14d2020-06-01 22:35:22 +0200930 struct nlattr *attr;
931
932 if (info->attrs[BATADV_ATTR_HOP_PENALTY]) {
933 attr = info->attrs[BATADV_ATTR_HOP_PENALTY];
934
935 atomic_set(&hard_iface->hop_penalty, nla_get_u8(attr));
936 }
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100937
Sven Eckelmanna1080082018-11-23 13:30:04 +0100938#ifdef CONFIG_BATMAN_ADV_BATMAN_V
Sven Eckelmanna1080082018-11-23 13:30:04 +0100939
940 if (info->attrs[BATADV_ATTR_ELP_INTERVAL]) {
941 attr = info->attrs[BATADV_ATTR_ELP_INTERVAL];
942
943 atomic_set(&hard_iface->bat_v.elp_interval, nla_get_u32(attr));
944 }
Sven Eckelmann9a182242018-11-23 13:31:23 +0100945
946 if (info->attrs[BATADV_ATTR_THROUGHPUT_OVERRIDE]) {
947 attr = info->attrs[BATADV_ATTR_THROUGHPUT_OVERRIDE];
948
949 atomic_set(&hard_iface->bat_v.throughput_override,
950 nla_get_u32(attr));
951 }
Sven Eckelmanna1080082018-11-23 13:30:04 +0100952#endif /* CONFIG_BATMAN_ADV_BATMAN_V */
953
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100954 batadv_netlink_notify_hardif(bat_priv, hard_iface);
955
956 return 0;
957}
958
959/**
960 * batadv_netlink_dump_hardif() - Dump all hard interface into a messages
Matthias Schifferb60620c2016-07-03 13:31:36 +0200961 * @msg: Netlink message to dump into
962 * @cb: Parameters from query
963 *
964 * Return: error code, or length of reply message on success
965 */
966static int
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100967batadv_netlink_dump_hardif(struct sk_buff *msg, struct netlink_callback *cb)
Matthias Schifferb60620c2016-07-03 13:31:36 +0200968{
969 struct net *net = sock_net(cb->skb->sk);
970 struct net_device *soft_iface;
971 struct batadv_hard_iface *hard_iface;
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100972 struct batadv_priv *bat_priv;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200973 int ifindex;
974 int portid = NETLINK_CB(cb->skb).portid;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200975 int skip = cb->args[0];
976 int i = 0;
977
978 ifindex = batadv_netlink_get_ifindex(cb->nlh,
979 BATADV_ATTR_MESH_IFINDEX);
980 if (!ifindex)
981 return -EINVAL;
982
983 soft_iface = dev_get_by_index(net, ifindex);
984 if (!soft_iface)
985 return -ENODEV;
986
987 if (!batadv_softif_is_valid(soft_iface)) {
988 dev_put(soft_iface);
989 return -ENODEV;
990 }
991
Sven Eckelmann5c55a402018-11-23 12:33:17 +0100992 bat_priv = netdev_priv(soft_iface);
993
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100994 rtnl_lock();
995 cb->seq = batadv_hardif_generation << 1 | 1;
Matthias Schifferb60620c2016-07-03 13:31:36 +0200996
Sven Eckelmannfb69be62018-10-30 22:01:24 +0100997 list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
Matthias Schifferb60620c2016-07-03 13:31:36 +0200998 if (hard_iface->soft_iface != soft_iface)
999 continue;
1000
1001 if (i++ < skip)
1002 continue;
1003
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001004 if (batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
1005 BATADV_CMD_GET_HARDIF,
1006 portid, cb->nlh->nlmsg_seq,
1007 NLM_F_MULTI, cb)) {
Matthias Schifferb60620c2016-07-03 13:31:36 +02001008 i--;
1009 break;
1010 }
1011 }
1012
Sven Eckelmannfb69be62018-10-30 22:01:24 +01001013 rtnl_unlock();
Matthias Schifferb60620c2016-07-03 13:31:36 +02001014
1015 dev_put(soft_iface);
1016
1017 cb->args[0] = i;
1018
1019 return msg->len;
1020}
1021
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001022/**
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001023 * batadv_netlink_vlan_fill() - Fill message with vlan attributes
1024 * @msg: Netlink message to dump into
1025 * @bat_priv: the bat priv with all the soft interface information
1026 * @vlan: vlan which was modified
1027 * @cmd: type of message to generate
1028 * @portid: Port making netlink request
1029 * @seq: sequence number for message
1030 * @flags: Additional flags for message
1031 *
1032 * Return: 0 on success or negative error number in case of failure
1033 */
1034static int batadv_netlink_vlan_fill(struct sk_buff *msg,
1035 struct batadv_priv *bat_priv,
1036 struct batadv_softif_vlan *vlan,
1037 enum batadv_nl_commands cmd,
1038 u32 portid, u32 seq, int flags)
1039{
1040 void *hdr;
1041
1042 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
1043 if (!hdr)
1044 return -ENOBUFS;
1045
1046 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
1047 bat_priv->soft_iface->ifindex))
1048 goto nla_put_failure;
1049
Sven Eckelmannd2953452021-05-10 15:05:42 +02001050 if (nla_put_string(msg, BATADV_ATTR_MESH_IFNAME,
1051 bat_priv->soft_iface->name))
1052 goto nla_put_failure;
1053
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001054 if (nla_put_u32(msg, BATADV_ATTR_VLANID, vlan->vid & VLAN_VID_MASK))
1055 goto nla_put_failure;
1056
Sven Eckelmanne43d16b2018-11-23 12:51:55 +01001057 if (nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
1058 !!atomic_read(&vlan->ap_isolation)))
1059 goto nla_put_failure;
1060
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001061 genlmsg_end(msg, hdr);
1062 return 0;
1063
1064nla_put_failure:
1065 genlmsg_cancel(msg, hdr);
1066 return -EMSGSIZE;
1067}
1068
1069/**
1070 * batadv_netlink_notify_vlan() - send vlan attributes to listener
1071 * @bat_priv: the bat priv with all the soft interface information
1072 * @vlan: vlan which was modified
1073 *
1074 * Return: 0 on success, < 0 on error
1075 */
Sven Eckelmann7e6f4612018-11-23 16:07:12 +01001076int batadv_netlink_notify_vlan(struct batadv_priv *bat_priv,
1077 struct batadv_softif_vlan *vlan)
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001078{
1079 struct sk_buff *msg;
1080 int ret;
1081
1082 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1083 if (!msg)
1084 return -ENOMEM;
1085
1086 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan,
1087 BATADV_CMD_SET_VLAN, 0, 0, 0);
1088 if (ret < 0) {
1089 nlmsg_free(msg);
1090 return ret;
1091 }
1092
1093 genlmsg_multicast_netns(&batadv_netlink_family,
1094 dev_net(bat_priv->soft_iface), msg, 0,
1095 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
1096
1097 return 0;
1098}
1099
1100/**
1101 * batadv_netlink_get_vlan() - Get vlan attributes
1102 * @skb: Netlink message with request data
1103 * @info: receiver information
1104 *
1105 * Return: 0 on success or negative error number in case of failure
1106 */
1107static int batadv_netlink_get_vlan(struct sk_buff *skb, struct genl_info *info)
1108{
1109 struct batadv_softif_vlan *vlan = info->user_ptr[1];
1110 struct batadv_priv *bat_priv = info->user_ptr[0];
1111 struct sk_buff *msg;
1112 int ret;
1113
1114 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1115 if (!msg)
1116 return -ENOMEM;
1117
1118 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan, BATADV_CMD_GET_VLAN,
1119 info->snd_portid, info->snd_seq, 0);
1120 if (ret < 0) {
1121 nlmsg_free(msg);
1122 return ret;
1123 }
1124
1125 ret = genlmsg_reply(msg, info);
1126
1127 return ret;
1128}
1129
1130/**
1131 * batadv_netlink_set_vlan() - Get vlan attributes
1132 * @skb: Netlink message with request data
1133 * @info: receiver information
1134 *
1135 * Return: 0 on success or negative error number in case of failure
1136 */
1137static int batadv_netlink_set_vlan(struct sk_buff *skb, struct genl_info *info)
1138{
1139 struct batadv_softif_vlan *vlan = info->user_ptr[1];
1140 struct batadv_priv *bat_priv = info->user_ptr[0];
Sven Eckelmanne43d16b2018-11-23 12:51:55 +01001141 struct nlattr *attr;
1142
1143 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
1144 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
1145
1146 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
1147 }
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001148
1149 batadv_netlink_notify_vlan(bat_priv, vlan);
1150
1151 return 0;
1152}
1153
1154/**
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001155 * batadv_get_softif_from_info() - Retrieve soft interface from genl attributes
1156 * @net: the applicable net namespace
1157 * @info: receiver information
1158 *
1159 * Return: Pointer to soft interface (with increased refcnt) on success, error
1160 * pointer on error
1161 */
1162static struct net_device *
1163batadv_get_softif_from_info(struct net *net, struct genl_info *info)
1164{
1165 struct net_device *soft_iface;
1166 int ifindex;
1167
1168 if (!info->attrs[BATADV_ATTR_MESH_IFINDEX])
1169 return ERR_PTR(-EINVAL);
1170
1171 ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
1172
1173 soft_iface = dev_get_by_index(net, ifindex);
1174 if (!soft_iface)
1175 return ERR_PTR(-ENODEV);
1176
1177 if (!batadv_softif_is_valid(soft_iface))
1178 goto err_put_softif;
1179
1180 return soft_iface;
1181
1182err_put_softif:
1183 dev_put(soft_iface);
1184
1185 return ERR_PTR(-EINVAL);
1186}
1187
1188/**
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001189 * batadv_get_hardif_from_info() - Retrieve hardif from genl attributes
1190 * @bat_priv: the bat priv with all the soft interface information
1191 * @net: the applicable net namespace
1192 * @info: receiver information
1193 *
1194 * Return: Pointer to hard interface (with increased refcnt) on success, error
1195 * pointer on error
1196 */
1197static struct batadv_hard_iface *
1198batadv_get_hardif_from_info(struct batadv_priv *bat_priv, struct net *net,
1199 struct genl_info *info)
1200{
1201 struct batadv_hard_iface *hard_iface;
1202 struct net_device *hard_dev;
1203 unsigned int hardif_index;
1204
1205 if (!info->attrs[BATADV_ATTR_HARD_IFINDEX])
1206 return ERR_PTR(-EINVAL);
1207
1208 hardif_index = nla_get_u32(info->attrs[BATADV_ATTR_HARD_IFINDEX]);
1209
1210 hard_dev = dev_get_by_index(net, hardif_index);
1211 if (!hard_dev)
1212 return ERR_PTR(-ENODEV);
1213
1214 hard_iface = batadv_hardif_get_by_netdev(hard_dev);
1215 if (!hard_iface)
1216 goto err_put_harddev;
1217
1218 if (hard_iface->soft_iface != bat_priv->soft_iface)
1219 goto err_put_hardif;
1220
1221 /* hard_dev is referenced by hard_iface and not needed here */
1222 dev_put(hard_dev);
1223
1224 return hard_iface;
1225
1226err_put_hardif:
1227 batadv_hardif_put(hard_iface);
1228err_put_harddev:
1229 dev_put(hard_dev);
1230
1231 return ERR_PTR(-EINVAL);
1232}
1233
1234/**
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001235 * batadv_get_vlan_from_info() - Retrieve vlan from genl attributes
1236 * @bat_priv: the bat priv with all the soft interface information
1237 * @net: the applicable net namespace
1238 * @info: receiver information
1239 *
1240 * Return: Pointer to vlan on success (with increased refcnt), error pointer
1241 * on error
1242 */
1243static struct batadv_softif_vlan *
1244batadv_get_vlan_from_info(struct batadv_priv *bat_priv, struct net *net,
1245 struct genl_info *info)
1246{
1247 struct batadv_softif_vlan *vlan;
1248 u16 vid;
1249
1250 if (!info->attrs[BATADV_ATTR_VLANID])
1251 return ERR_PTR(-EINVAL);
1252
1253 vid = nla_get_u16(info->attrs[BATADV_ATTR_VLANID]);
1254
1255 vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
1256 if (!vlan)
1257 return ERR_PTR(-ENOENT);
1258
1259 return vlan;
1260}
1261
1262/**
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001263 * batadv_pre_doit() - Prepare batman-adv genl doit request
1264 * @ops: requested netlink operation
1265 * @skb: Netlink message with request data
1266 * @info: receiver information
1267 *
1268 * Return: 0 on success or negative error number in case of failure
1269 */
1270static int batadv_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
1271 struct genl_info *info)
1272{
1273 struct net *net = genl_info_net(info);
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001274 struct batadv_hard_iface *hard_iface;
1275 struct batadv_priv *bat_priv = NULL;
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001276 struct batadv_softif_vlan *vlan;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001277 struct net_device *soft_iface;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001278 u8 user_ptr1_flags;
1279 u8 mesh_dep_flags;
1280 int ret;
1281
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001282 user_ptr1_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001283 if (WARN_ON(hweight8(ops->internal_flags & user_ptr1_flags) > 1))
1284 return -EINVAL;
1285
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001286 mesh_dep_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001287 if (WARN_ON((ops->internal_flags & mesh_dep_flags) &&
1288 (~ops->internal_flags & BATADV_FLAG_NEED_MESH)))
1289 return -EINVAL;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001290
1291 if (ops->internal_flags & BATADV_FLAG_NEED_MESH) {
1292 soft_iface = batadv_get_softif_from_info(net, info);
1293 if (IS_ERR(soft_iface))
1294 return PTR_ERR(soft_iface);
1295
1296 bat_priv = netdev_priv(soft_iface);
1297 info->user_ptr[0] = bat_priv;
1298 }
1299
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001300 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF) {
1301 hard_iface = batadv_get_hardif_from_info(bat_priv, net, info);
1302 if (IS_ERR(hard_iface)) {
1303 ret = PTR_ERR(hard_iface);
1304 goto err_put_softif;
1305 }
1306
1307 info->user_ptr[1] = hard_iface;
1308 }
1309
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001310 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN) {
1311 vlan = batadv_get_vlan_from_info(bat_priv, net, info);
1312 if (IS_ERR(vlan)) {
1313 ret = PTR_ERR(vlan);
1314 goto err_put_softif;
1315 }
1316
1317 info->user_ptr[1] = vlan;
1318 }
1319
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001320 return 0;
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001321
1322err_put_softif:
1323 if (bat_priv)
1324 dev_put(bat_priv->soft_iface);
1325
1326 return ret;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001327}
1328
1329/**
1330 * batadv_post_doit() - End batman-adv genl doit request
1331 * @ops: requested netlink operation
1332 * @skb: Netlink message with request data
1333 * @info: receiver information
1334 */
1335static void batadv_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
1336 struct genl_info *info)
1337{
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001338 struct batadv_hard_iface *hard_iface;
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001339 struct batadv_softif_vlan *vlan;
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001340 struct batadv_priv *bat_priv;
1341
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001342 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF &&
1343 info->user_ptr[1]) {
1344 hard_iface = info->user_ptr[1];
1345
1346 batadv_hardif_put(hard_iface);
1347 }
1348
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001349 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN && info->user_ptr[1]) {
1350 vlan = info->user_ptr[1];
1351 batadv_softif_vlan_put(vlan);
1352 }
1353
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001354 if (ops->internal_flags & BATADV_FLAG_NEED_MESH && info->user_ptr[0]) {
1355 bat_priv = info->user_ptr[0];
1356 dev_put(bat_priv->soft_iface);
1357 }
1358}
1359
Jakub Kicinski66a9b922020-10-02 14:49:54 -07001360static const struct genl_small_ops batadv_netlink_ops[] = {
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001361 {
Sven Eckelmann60040512018-11-23 12:14:56 +01001362 .cmd = BATADV_CMD_GET_MESH,
Johannes Bergef6243a2019-04-26 14:07:31 +02001363 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmann60040512018-11-23 12:14:56 +01001364 /* can be retrieved by unprivileged users */
Sven Eckelmann60040512018-11-23 12:14:56 +01001365 .doit = batadv_netlink_get_mesh,
1366 .internal_flags = BATADV_FLAG_NEED_MESH,
Matthias Schiffer5da0aef2016-05-09 18:41:09 +02001367 },
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001368 {
1369 .cmd = BATADV_CMD_TP_METER,
Johannes Bergef6243a2019-04-26 14:07:31 +02001370 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001371 .flags = GENL_ADMIN_PERM,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001372 .doit = batadv_netlink_tp_meter_start,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001373 .internal_flags = BATADV_FLAG_NEED_MESH,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001374 },
1375 {
1376 .cmd = BATADV_CMD_TP_METER_CANCEL,
Johannes Bergef6243a2019-04-26 14:07:31 +02001377 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001378 .flags = GENL_ADMIN_PERM,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001379 .doit = batadv_netlink_tp_meter_cancel,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001380 .internal_flags = BATADV_FLAG_NEED_MESH,
Antonio Quartulli33a3bb42016-05-05 13:09:43 +02001381 },
Matthias Schiffer07a30612016-07-03 13:31:35 +02001382 {
1383 .cmd = BATADV_CMD_GET_ROUTING_ALGOS,
Johannes Bergef6243a2019-04-26 14:07:31 +02001384 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Matthias Schiffer07a30612016-07-03 13:31:35 +02001385 .flags = GENL_ADMIN_PERM,
Matthias Schiffer07a30612016-07-03 13:31:35 +02001386 .dumpit = batadv_algo_dump,
1387 },
Matthias Schifferb60620c2016-07-03 13:31:36 +02001388 {
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001389 .cmd = BATADV_CMD_GET_HARDIF,
Johannes Bergef6243a2019-04-26 14:07:31 +02001390 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001391 /* can be retrieved by unprivileged users */
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001392 .dumpit = batadv_netlink_dump_hardif,
1393 .doit = batadv_netlink_get_hardif,
1394 .internal_flags = BATADV_FLAG_NEED_MESH |
1395 BATADV_FLAG_NEED_HARDIF,
Matthias Schifferb60620c2016-07-03 13:31:36 +02001396 },
Matthias Schifferd34f0552016-07-03 13:31:37 +02001397 {
1398 .cmd = BATADV_CMD_GET_TRANSTABLE_LOCAL,
Johannes Bergef6243a2019-04-26 14:07:31 +02001399 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Matthias Schifferd34f0552016-07-03 13:31:37 +02001400 .flags = GENL_ADMIN_PERM,
Matthias Schifferd34f0552016-07-03 13:31:37 +02001401 .dumpit = batadv_tt_local_dump,
1402 },
1403 {
1404 .cmd = BATADV_CMD_GET_TRANSTABLE_GLOBAL,
Johannes Bergef6243a2019-04-26 14:07:31 +02001405 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Matthias Schifferd34f0552016-07-03 13:31:37 +02001406 .flags = GENL_ADMIN_PERM,
Matthias Schifferd34f0552016-07-03 13:31:37 +02001407 .dumpit = batadv_tt_global_dump,
1408 },
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001409 {
1410 .cmd = BATADV_CMD_GET_ORIGINATORS,
Johannes Bergef6243a2019-04-26 14:07:31 +02001411 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001412 .flags = GENL_ADMIN_PERM,
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001413 .dumpit = batadv_orig_dump,
1414 },
1415 {
1416 .cmd = BATADV_CMD_GET_NEIGHBORS,
Johannes Bergef6243a2019-04-26 14:07:31 +02001417 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001418 .flags = GENL_ADMIN_PERM,
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001419 .dumpit = batadv_hardif_neigh_dump,
1420 },
Sven Eckelmannd7129da2016-07-03 13:31:42 +02001421 {
1422 .cmd = BATADV_CMD_GET_GATEWAYS,
Johannes Bergef6243a2019-04-26 14:07:31 +02001423 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmannd7129da2016-07-03 13:31:42 +02001424 .flags = GENL_ADMIN_PERM,
Sven Eckelmannd7129da2016-07-03 13:31:42 +02001425 .dumpit = batadv_gw_dump,
1426 },
Andrew Lunn04f3f5b2016-07-03 13:31:45 +02001427 {
1428 .cmd = BATADV_CMD_GET_BLA_CLAIM,
Johannes Bergef6243a2019-04-26 14:07:31 +02001429 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andrew Lunn04f3f5b2016-07-03 13:31:45 +02001430 .flags = GENL_ADMIN_PERM,
Andrew Lunn04f3f5b2016-07-03 13:31:45 +02001431 .dumpit = batadv_bla_claim_dump,
1432 },
Simon Wunderlichea4152e2016-07-03 13:31:47 +02001433 {
1434 .cmd = BATADV_CMD_GET_BLA_BACKBONE,
Johannes Bergef6243a2019-04-26 14:07:31 +02001435 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Simon Wunderlichea4152e2016-07-03 13:31:47 +02001436 .flags = GENL_ADMIN_PERM,
Simon Wunderlichea4152e2016-07-03 13:31:47 +02001437 .dumpit = batadv_bla_backbone_dump,
1438 },
Linus Lüssing41aeefc2018-03-13 11:41:12 +01001439 {
1440 .cmd = BATADV_CMD_GET_DAT_CACHE,
Johannes Bergef6243a2019-04-26 14:07:31 +02001441 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Linus Lüssing41aeefc2018-03-13 11:41:12 +01001442 .flags = GENL_ADMIN_PERM,
Linus Lüssing41aeefc2018-03-13 11:41:12 +01001443 .dumpit = batadv_dat_cache_dump,
1444 },
Linus Lüssing53dd9a62018-03-13 11:41:13 +01001445 {
1446 .cmd = BATADV_CMD_GET_MCAST_FLAGS,
Johannes Bergef6243a2019-04-26 14:07:31 +02001447 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Linus Lüssing53dd9a62018-03-13 11:41:13 +01001448 .flags = GENL_ADMIN_PERM,
Linus Lüssing53dd9a62018-03-13 11:41:13 +01001449 .dumpit = batadv_mcast_flags_dump,
1450 },
Sven Eckelmann60040512018-11-23 12:14:56 +01001451 {
1452 .cmd = BATADV_CMD_SET_MESH,
Johannes Bergef6243a2019-04-26 14:07:31 +02001453 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmann60040512018-11-23 12:14:56 +01001454 .flags = GENL_ADMIN_PERM,
Sven Eckelmann60040512018-11-23 12:14:56 +01001455 .doit = batadv_netlink_set_mesh,
1456 .internal_flags = BATADV_FLAG_NEED_MESH,
1457 },
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001458 {
1459 .cmd = BATADV_CMD_SET_HARDIF,
Johannes Bergef6243a2019-04-26 14:07:31 +02001460 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001461 .flags = GENL_ADMIN_PERM,
Sven Eckelmann5c55a402018-11-23 12:33:17 +01001462 .doit = batadv_netlink_set_hardif,
1463 .internal_flags = BATADV_FLAG_NEED_MESH |
1464 BATADV_FLAG_NEED_HARDIF,
1465 },
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001466 {
1467 .cmd = BATADV_CMD_GET_VLAN,
Johannes Bergef6243a2019-04-26 14:07:31 +02001468 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001469 /* can be retrieved by unprivileged users */
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001470 .doit = batadv_netlink_get_vlan,
1471 .internal_flags = BATADV_FLAG_NEED_MESH |
1472 BATADV_FLAG_NEED_VLAN,
1473 },
1474 {
1475 .cmd = BATADV_CMD_SET_VLAN,
Johannes Bergef6243a2019-04-26 14:07:31 +02001476 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001477 .flags = GENL_ADMIN_PERM,
Sven Eckelmann49e7e372018-11-23 12:41:08 +01001478 .doit = batadv_netlink_set_vlan,
1479 .internal_flags = BATADV_FLAG_NEED_MESH |
1480 BATADV_FLAG_NEED_VLAN,
1481 },
Matthias Schiffer09748a22016-05-09 18:41:08 +02001482};
1483
Johannes Berg56989f62016-10-24 14:40:05 +02001484struct genl_family batadv_netlink_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001485 .hdrsize = 0,
1486 .name = BATADV_NL_NAME,
1487 .version = 1,
1488 .maxattr = BATADV_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01001489 .policy = batadv_netlink_policy,
Johannes Berg489111e2016-10-24 14:40:03 +02001490 .netnsok = true,
Sven Eckelmannc4a7a8d2018-11-23 12:00:28 +01001491 .pre_doit = batadv_pre_doit,
1492 .post_doit = batadv_post_doit,
Johannes Berg489111e2016-10-24 14:40:03 +02001493 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -07001494 .small_ops = batadv_netlink_ops,
1495 .n_small_ops = ARRAY_SIZE(batadv_netlink_ops),
Johannes Berg489111e2016-10-24 14:40:03 +02001496 .mcgrps = batadv_netlink_mcgrps,
1497 .n_mcgrps = ARRAY_SIZE(batadv_netlink_mcgrps),
1498};
1499
Matthias Schiffer09748a22016-05-09 18:41:08 +02001500/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001501 * batadv_netlink_register() - register batadv genl netlink family
Matthias Schiffer09748a22016-05-09 18:41:08 +02001502 */
1503void __init batadv_netlink_register(void)
1504{
1505 int ret;
1506
Johannes Berg489111e2016-10-24 14:40:03 +02001507 ret = genl_register_family(&batadv_netlink_family);
Matthias Schiffer09748a22016-05-09 18:41:08 +02001508 if (ret)
1509 pr_warn("unable to register netlink family");
1510}
1511
1512/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001513 * batadv_netlink_unregister() - unregister batadv genl netlink family
Matthias Schiffer09748a22016-05-09 18:41:08 +02001514 */
1515void batadv_netlink_unregister(void)
1516{
1517 genl_unregister_family(&batadv_netlink_family);
1518}