Sven Eckelmann | 7db7d9f | 2017-11-19 15:05:11 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sven Eckelmann | 7a79d71 | 2018-12-31 23:59:59 +0100 | [diff] [blame] | 2 | /* Copyright (C) 2016-2019 B.A.T.M.A.N. contributors: |
Matthias Schiffer | 09748a2 | 2016-05-09 18:41:08 +0200 | [diff] [blame] | 3 | * |
| 4 | * Matthias Schiffer |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of version 2 of the GNU General Public |
| 8 | * License as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "netlink.h" |
| 20 | #include "main.h" |
| 21 | |
Sven Eckelmann | f32ed4b | 2016-07-03 13:31:38 +0200 | [diff] [blame] | 22 | #include <linux/atomic.h> |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 23 | #include <linux/bitops.h> |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 24 | #include <linux/bug.h> |
Sven Eckelmann | 8dad6f0d | 2016-07-03 13:31:46 +0200 | [diff] [blame] | 25 | #include <linux/byteorder/generic.h> |
Sven Eckelmann | 9bcb94c | 2016-10-29 10:13:47 +0200 | [diff] [blame] | 26 | #include <linux/cache.h> |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 27 | #include <linux/err.h> |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 28 | #include <linux/errno.h> |
Sven Eckelmann | 9bcb94c | 2016-10-29 10:13:47 +0200 | [diff] [blame] | 29 | #include <linux/export.h> |
Matthias Schiffer | 09748a2 | 2016-05-09 18:41:08 +0200 | [diff] [blame] | 30 | #include <linux/genetlink.h> |
Sven Eckelmann | b92b94a | 2017-11-19 17:12:02 +0100 | [diff] [blame] | 31 | #include <linux/gfp.h> |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 32 | #include <linux/if_ether.h> |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 33 | #include <linux/if_vlan.h> |
Matthias Schiffer | 09748a2 | 2016-05-09 18:41:08 +0200 | [diff] [blame] | 34 | #include <linux/init.h> |
Sven Eckelmann | 9bcb94c | 2016-10-29 10:13:47 +0200 | [diff] [blame] | 35 | #include <linux/kernel.h> |
Sven Eckelmann | fb69be6 | 2018-10-30 22:01:24 +0100 | [diff] [blame] | 36 | #include <linux/list.h> |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 37 | #include <linux/netdevice.h> |
| 38 | #include <linux/netlink.h> |
Matthias Schiffer | 09748a2 | 2016-05-09 18:41:08 +0200 | [diff] [blame] | 39 | #include <linux/printk.h> |
Sven Eckelmann | fb69be6 | 2018-10-30 22:01:24 +0100 | [diff] [blame] | 40 | #include <linux/rtnetlink.h> |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 41 | #include <linux/skbuff.h> |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 42 | #include <linux/stddef.h> |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 43 | #include <linux/types.h> |
Matthias Schiffer | 09748a2 | 2016-05-09 18:41:08 +0200 | [diff] [blame] | 44 | #include <net/genetlink.h> |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 45 | #include <net/netlink.h> |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 46 | #include <net/sock.h> |
Sven Eckelmann | fec149f | 2017-12-21 10:17:41 +0100 | [diff] [blame] | 47 | #include <uapi/linux/batadv_packet.h> |
Matthias Schiffer | 09748a2 | 2016-05-09 18:41:08 +0200 | [diff] [blame] | 48 | #include <uapi/linux/batman_adv.h> |
| 49 | |
Matthias Schiffer | 07a3061 | 2016-07-03 13:31:35 +0200 | [diff] [blame] | 50 | #include "bat_algo.h" |
Andrew Lunn | 04f3f5b | 2016-07-03 13:31:45 +0200 | [diff] [blame] | 51 | #include "bridge_loop_avoidance.h" |
Linus Lüssing | 41aeefc | 2018-03-13 11:41:12 +0100 | [diff] [blame] | 52 | #include "distributed-arp-table.h" |
Sven Eckelmann | d7129da | 2016-07-03 13:31:42 +0200 | [diff] [blame] | 53 | #include "gateway_client.h" |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 54 | #include "hard-interface.h" |
Linus Lüssing | 53dd9a6 | 2018-03-13 11:41:13 +0100 | [diff] [blame] | 55 | #include "multicast.h" |
Matthias Schiffer | 85cf8c8 | 2016-07-03 13:31:39 +0200 | [diff] [blame] | 56 | #include "originator.h" |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 57 | #include "soft-interface.h" |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 58 | #include "tp_meter.h" |
Matthias Schiffer | d34f055 | 2016-07-03 13:31:37 +0200 | [diff] [blame] | 59 | #include "translation-table.h" |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 60 | |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 61 | struct net; |
| 62 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 63 | struct genl_family batadv_netlink_family; |
Matthias Schiffer | 09748a2 | 2016-05-09 18:41:08 +0200 | [diff] [blame] | 64 | |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 65 | /* multicast groups */ |
| 66 | enum batadv_netlink_multicast_groups { |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 67 | BATADV_NL_MCGRP_CONFIG, |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 68 | BATADV_NL_MCGRP_TPMETER, |
| 69 | }; |
| 70 | |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 71 | /** |
| 72 | * enum batadv_genl_ops_flags - flags for genl_ops's internal_flags |
| 73 | */ |
| 74 | enum batadv_genl_ops_flags { |
| 75 | /** |
| 76 | * @BATADV_FLAG_NEED_MESH: request requires valid soft interface in |
| 77 | * attribute BATADV_ATTR_MESH_IFINDEX and expects a pointer to it to be |
| 78 | * saved in info->user_ptr[0] |
| 79 | */ |
| 80 | BATADV_FLAG_NEED_MESH = BIT(0), |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * @BATADV_FLAG_NEED_HARDIF: request requires valid hard interface in |
| 84 | * attribute BATADV_ATTR_HARD_IFINDEX and expects a pointer to it to be |
| 85 | * saved in info->user_ptr[1] |
| 86 | */ |
| 87 | BATADV_FLAG_NEED_HARDIF = BIT(1), |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * @BATADV_FLAG_NEED_VLAN: request requires valid vlan in |
| 91 | * attribute BATADV_ATTR_VLANID and expects a pointer to it to be |
| 92 | * saved in info->user_ptr[1] |
| 93 | */ |
| 94 | BATADV_FLAG_NEED_VLAN = BIT(2), |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
stephen hemminger | deeb91f | 2016-08-31 15:17:00 -0700 | [diff] [blame] | 97 | static const struct genl_multicast_group batadv_netlink_mcgrps[] = { |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 98 | [BATADV_NL_MCGRP_CONFIG] = { .name = BATADV_NL_MCAST_GROUP_CONFIG }, |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 99 | [BATADV_NL_MCGRP_TPMETER] = { .name = BATADV_NL_MCAST_GROUP_TPMETER }, |
| 100 | }; |
| 101 | |
stephen hemminger | deeb91f | 2016-08-31 15:17:00 -0700 | [diff] [blame] | 102 | static const struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { |
Linus Lüssing | 41aeefc | 2018-03-13 11:41:12 +0100 | [diff] [blame] | 103 | [BATADV_ATTR_VERSION] = { .type = NLA_STRING }, |
| 104 | [BATADV_ATTR_ALGO_NAME] = { .type = NLA_STRING }, |
| 105 | [BATADV_ATTR_MESH_IFINDEX] = { .type = NLA_U32 }, |
| 106 | [BATADV_ATTR_MESH_IFNAME] = { .type = NLA_STRING }, |
| 107 | [BATADV_ATTR_MESH_ADDRESS] = { .len = ETH_ALEN }, |
| 108 | [BATADV_ATTR_HARD_IFINDEX] = { .type = NLA_U32 }, |
| 109 | [BATADV_ATTR_HARD_IFNAME] = { .type = NLA_STRING }, |
| 110 | [BATADV_ATTR_HARD_ADDRESS] = { .len = ETH_ALEN }, |
| 111 | [BATADV_ATTR_ORIG_ADDRESS] = { .len = ETH_ALEN }, |
| 112 | [BATADV_ATTR_TPMETER_RESULT] = { .type = NLA_U8 }, |
| 113 | [BATADV_ATTR_TPMETER_TEST_TIME] = { .type = NLA_U32 }, |
| 114 | [BATADV_ATTR_TPMETER_BYTES] = { .type = NLA_U64 }, |
| 115 | [BATADV_ATTR_TPMETER_COOKIE] = { .type = NLA_U32 }, |
| 116 | [BATADV_ATTR_ACTIVE] = { .type = NLA_FLAG }, |
| 117 | [BATADV_ATTR_TT_ADDRESS] = { .len = ETH_ALEN }, |
| 118 | [BATADV_ATTR_TT_TTVN] = { .type = NLA_U8 }, |
| 119 | [BATADV_ATTR_TT_LAST_TTVN] = { .type = NLA_U8 }, |
| 120 | [BATADV_ATTR_TT_CRC32] = { .type = NLA_U32 }, |
| 121 | [BATADV_ATTR_TT_VID] = { .type = NLA_U16 }, |
| 122 | [BATADV_ATTR_TT_FLAGS] = { .type = NLA_U32 }, |
| 123 | [BATADV_ATTR_FLAG_BEST] = { .type = NLA_FLAG }, |
| 124 | [BATADV_ATTR_LAST_SEEN_MSECS] = { .type = NLA_U32 }, |
| 125 | [BATADV_ATTR_NEIGH_ADDRESS] = { .len = ETH_ALEN }, |
| 126 | [BATADV_ATTR_TQ] = { .type = NLA_U8 }, |
| 127 | [BATADV_ATTR_THROUGHPUT] = { .type = NLA_U32 }, |
| 128 | [BATADV_ATTR_BANDWIDTH_UP] = { .type = NLA_U32 }, |
| 129 | [BATADV_ATTR_BANDWIDTH_DOWN] = { .type = NLA_U32 }, |
| 130 | [BATADV_ATTR_ROUTER] = { .len = ETH_ALEN }, |
| 131 | [BATADV_ATTR_BLA_OWN] = { .type = NLA_FLAG }, |
| 132 | [BATADV_ATTR_BLA_ADDRESS] = { .len = ETH_ALEN }, |
| 133 | [BATADV_ATTR_BLA_VID] = { .type = NLA_U16 }, |
| 134 | [BATADV_ATTR_BLA_BACKBONE] = { .len = ETH_ALEN }, |
| 135 | [BATADV_ATTR_BLA_CRC] = { .type = NLA_U16 }, |
| 136 | [BATADV_ATTR_DAT_CACHE_IP4ADDRESS] = { .type = NLA_U32 }, |
| 137 | [BATADV_ATTR_DAT_CACHE_HWADDRESS] = { .len = ETH_ALEN }, |
| 138 | [BATADV_ATTR_DAT_CACHE_VID] = { .type = NLA_U16 }, |
Linus Lüssing | 53dd9a6 | 2018-03-13 11:41:13 +0100 | [diff] [blame] | 139 | [BATADV_ATTR_MCAST_FLAGS] = { .type = NLA_U32 }, |
| 140 | [BATADV_ATTR_MCAST_FLAGS_PRIV] = { .type = NLA_U32 }, |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 141 | [BATADV_ATTR_VLANID] = { .type = NLA_U16 }, |
Sven Eckelmann | 9ab4cee | 2018-11-23 12:46:14 +0100 | [diff] [blame] | 142 | [BATADV_ATTR_AGGREGATED_OGMS_ENABLED] = { .type = NLA_U8 }, |
Sven Eckelmann | e43d16b | 2018-11-23 12:51:55 +0100 | [diff] [blame] | 143 | [BATADV_ATTR_AP_ISOLATION_ENABLED] = { .type = NLA_U8 }, |
| 144 | [BATADV_ATTR_ISOLATION_MARK] = { .type = NLA_U32 }, |
| 145 | [BATADV_ATTR_ISOLATION_MASK] = { .type = NLA_U32 }, |
Sven Eckelmann | d7e5250 | 2018-11-23 12:55:44 +0100 | [diff] [blame] | 146 | [BATADV_ATTR_BONDING_ENABLED] = { .type = NLA_U8 }, |
Sven Eckelmann | 43ff610 | 2018-11-23 13:03:39 +0100 | [diff] [blame^] | 147 | [BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED] = { .type = NLA_U8 }, |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 151 | * batadv_netlink_get_ifindex() - Extract an interface index from a message |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 152 | * @nlh: Message header |
| 153 | * @attrtype: Attribute which holds an interface index |
| 154 | * |
| 155 | * Return: interface index, or 0. |
| 156 | */ |
Matthias Schiffer | d34f055 | 2016-07-03 13:31:37 +0200 | [diff] [blame] | 157 | int |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 158 | batadv_netlink_get_ifindex(const struct nlmsghdr *nlh, int attrtype) |
| 159 | { |
| 160 | struct nlattr *attr = nlmsg_find_attr(nlh, GENL_HDRLEN, attrtype); |
| 161 | |
| 162 | return attr ? nla_get_u32(attr) : 0; |
| 163 | } |
| 164 | |
| 165 | /** |
Sven Eckelmann | e43d16b | 2018-11-23 12:51:55 +0100 | [diff] [blame] | 166 | * batadv_netlink_mesh_fill_ap_isolation() - Add ap_isolation softif attribute |
| 167 | * @msg: Netlink message to dump into |
| 168 | * @bat_priv: the bat priv with all the soft interface information |
| 169 | * |
| 170 | * Return: 0 on success or negative error number in case of failure |
| 171 | */ |
| 172 | static int batadv_netlink_mesh_fill_ap_isolation(struct sk_buff *msg, |
| 173 | struct batadv_priv *bat_priv) |
| 174 | { |
| 175 | struct batadv_softif_vlan *vlan; |
| 176 | u8 ap_isolation; |
| 177 | |
| 178 | vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS); |
| 179 | if (!vlan) |
| 180 | return 0; |
| 181 | |
| 182 | ap_isolation = atomic_read(&vlan->ap_isolation); |
| 183 | batadv_softif_vlan_put(vlan); |
| 184 | |
| 185 | return nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED, |
| 186 | !!ap_isolation); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * batadv_option_set_ap_isolation() - Set ap_isolation from genl msg |
| 191 | * @attr: parsed BATADV_ATTR_AP_ISOLATION_ENABLED attribute |
| 192 | * @bat_priv: the bat priv with all the soft interface information |
| 193 | * |
| 194 | * Return: 0 on success or negative error number in case of failure |
| 195 | */ |
| 196 | static int batadv_netlink_set_mesh_ap_isolation(struct nlattr *attr, |
| 197 | struct batadv_priv *bat_priv) |
| 198 | { |
| 199 | struct batadv_softif_vlan *vlan; |
| 200 | |
| 201 | vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS); |
| 202 | if (!vlan) |
| 203 | return -ENOENT; |
| 204 | |
| 205 | atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr)); |
| 206 | batadv_softif_vlan_put(vlan); |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | /** |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 212 | * batadv_netlink_mesh_fill() - Fill message with mesh attributes |
| 213 | * @msg: Netlink message to dump into |
| 214 | * @bat_priv: the bat priv with all the soft interface information |
| 215 | * @cmd: type of message to generate |
| 216 | * @portid: Port making netlink request |
| 217 | * @seq: sequence number for message |
| 218 | * @flags: Additional flags for message |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 219 | * |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 220 | * Return: 0 on success or negative error number in case of failure |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 221 | */ |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 222 | static int batadv_netlink_mesh_fill(struct sk_buff *msg, |
| 223 | struct batadv_priv *bat_priv, |
| 224 | enum batadv_nl_commands cmd, |
| 225 | u32 portid, u32 seq, int flags) |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 226 | { |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 227 | struct net_device *soft_iface = bat_priv->soft_iface; |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 228 | struct batadv_hard_iface *primary_if = NULL; |
| 229 | struct net_device *hard_iface; |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 230 | void *hdr; |
| 231 | |
| 232 | hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd); |
| 233 | if (!hdr) |
| 234 | return -ENOBUFS; |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 235 | |
| 236 | if (nla_put_string(msg, BATADV_ATTR_VERSION, BATADV_SOURCE_VERSION) || |
| 237 | nla_put_string(msg, BATADV_ATTR_ALGO_NAME, |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 238 | bat_priv->algo_ops->name) || |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 239 | nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, soft_iface->ifindex) || |
| 240 | nla_put_string(msg, BATADV_ATTR_MESH_IFNAME, soft_iface->name) || |
| 241 | nla_put(msg, BATADV_ATTR_MESH_ADDRESS, ETH_ALEN, |
Sven Eckelmann | f32ed4b | 2016-07-03 13:31:38 +0200 | [diff] [blame] | 242 | soft_iface->dev_addr) || |
| 243 | nla_put_u8(msg, BATADV_ATTR_TT_TTVN, |
| 244 | (u8)atomic_read(&bat_priv->tt.vn))) |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 245 | goto nla_put_failure; |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 246 | |
Sven Eckelmann | 8dad6f0d | 2016-07-03 13:31:46 +0200 | [diff] [blame] | 247 | #ifdef CONFIG_BATMAN_ADV_BLA |
| 248 | if (nla_put_u16(msg, BATADV_ATTR_BLA_CRC, |
| 249 | ntohs(bat_priv->bla.claim_dest.group))) |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 250 | goto nla_put_failure; |
Sven Eckelmann | 8dad6f0d | 2016-07-03 13:31:46 +0200 | [diff] [blame] | 251 | #endif |
| 252 | |
Linus Lüssing | 53dd9a6 | 2018-03-13 11:41:13 +0100 | [diff] [blame] | 253 | if (batadv_mcast_mesh_info_put(msg, bat_priv)) |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 254 | goto nla_put_failure; |
Linus Lüssing | 53dd9a6 | 2018-03-13 11:41:13 +0100 | [diff] [blame] | 255 | |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 256 | primary_if = batadv_primary_if_get_selected(bat_priv); |
| 257 | if (primary_if && primary_if->if_status == BATADV_IF_ACTIVE) { |
| 258 | hard_iface = primary_if->net_dev; |
| 259 | |
| 260 | if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX, |
| 261 | hard_iface->ifindex) || |
| 262 | nla_put_string(msg, BATADV_ATTR_HARD_IFNAME, |
| 263 | hard_iface->name) || |
| 264 | nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN, |
| 265 | hard_iface->dev_addr)) |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 266 | goto nla_put_failure; |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Sven Eckelmann | 9ab4cee | 2018-11-23 12:46:14 +0100 | [diff] [blame] | 269 | if (nla_put_u8(msg, BATADV_ATTR_AGGREGATED_OGMS_ENABLED, |
| 270 | !!atomic_read(&bat_priv->aggregated_ogms))) |
| 271 | goto nla_put_failure; |
| 272 | |
Sven Eckelmann | e43d16b | 2018-11-23 12:51:55 +0100 | [diff] [blame] | 273 | if (batadv_netlink_mesh_fill_ap_isolation(msg, bat_priv)) |
| 274 | goto nla_put_failure; |
| 275 | |
| 276 | if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MARK, |
| 277 | bat_priv->isolation_mark)) |
| 278 | goto nla_put_failure; |
| 279 | |
| 280 | if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MASK, |
| 281 | bat_priv->isolation_mark_mask)) |
| 282 | goto nla_put_failure; |
| 283 | |
Sven Eckelmann | d7e5250 | 2018-11-23 12:55:44 +0100 | [diff] [blame] | 284 | if (nla_put_u8(msg, BATADV_ATTR_BONDING_ENABLED, |
| 285 | !!atomic_read(&bat_priv->bonding))) |
| 286 | goto nla_put_failure; |
| 287 | |
Sven Eckelmann | 43ff610 | 2018-11-23 13:03:39 +0100 | [diff] [blame^] | 288 | #ifdef CONFIG_BATMAN_ADV_BLA |
| 289 | if (nla_put_u8(msg, BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED, |
| 290 | !!atomic_read(&bat_priv->bridge_loop_avoidance))) |
| 291 | goto nla_put_failure; |
| 292 | #endif /* CONFIG_BATMAN_ADV_BLA */ |
| 293 | |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 294 | if (primary_if) |
| 295 | batadv_hardif_put(primary_if); |
| 296 | |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 297 | genlmsg_end(msg, hdr); |
| 298 | return 0; |
| 299 | |
| 300 | nla_put_failure: |
| 301 | if (primary_if) |
| 302 | batadv_hardif_put(primary_if); |
| 303 | |
| 304 | genlmsg_cancel(msg, hdr); |
| 305 | return -EMSGSIZE; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * batadv_netlink_notify_mesh() - send softif attributes to listener |
| 310 | * @bat_priv: the bat priv with all the soft interface information |
| 311 | * |
| 312 | * Return: 0 on success, < 0 on error |
| 313 | */ |
| 314 | static int batadv_netlink_notify_mesh(struct batadv_priv *bat_priv) |
| 315 | { |
| 316 | struct sk_buff *msg; |
| 317 | int ret; |
| 318 | |
| 319 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 320 | if (!msg) |
| 321 | return -ENOMEM; |
| 322 | |
| 323 | ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_SET_MESH, |
| 324 | 0, 0, 0); |
| 325 | if (ret < 0) { |
| 326 | nlmsg_free(msg); |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | genlmsg_multicast_netns(&batadv_netlink_family, |
| 331 | dev_net(bat_priv->soft_iface), msg, 0, |
| 332 | BATADV_NL_MCGRP_CONFIG, GFP_KERNEL); |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * batadv_netlink_get_mesh() - Get softif attributes |
| 339 | * @skb: Netlink message with request data |
| 340 | * @info: receiver information |
| 341 | * |
| 342 | * Return: 0 on success or negative error number in case of failure |
| 343 | */ |
| 344 | static int batadv_netlink_get_mesh(struct sk_buff *skb, struct genl_info *info) |
| 345 | { |
| 346 | struct batadv_priv *bat_priv = info->user_ptr[0]; |
| 347 | struct sk_buff *msg; |
| 348 | int ret; |
| 349 | |
| 350 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 351 | if (!msg) |
| 352 | return -ENOMEM; |
| 353 | |
| 354 | ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_GET_MESH, |
| 355 | info->snd_portid, info->snd_seq, 0); |
| 356 | if (ret < 0) { |
| 357 | nlmsg_free(msg); |
| 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | ret = genlmsg_reply(msg, info); |
| 362 | |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | /** |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 367 | * batadv_netlink_set_mesh() - Set softif attributes |
| 368 | * @skb: Netlink message with request data |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 369 | * @info: receiver information |
| 370 | * |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 371 | * Return: 0 on success or negative error number in case of failure |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 372 | */ |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 373 | static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info) |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 374 | { |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 375 | struct batadv_priv *bat_priv = info->user_ptr[0]; |
Sven Eckelmann | 9ab4cee | 2018-11-23 12:46:14 +0100 | [diff] [blame] | 376 | struct nlattr *attr; |
| 377 | |
| 378 | if (info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED]) { |
| 379 | attr = info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED]; |
| 380 | |
| 381 | atomic_set(&bat_priv->aggregated_ogms, !!nla_get_u8(attr)); |
| 382 | } |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 383 | |
Sven Eckelmann | e43d16b | 2018-11-23 12:51:55 +0100 | [diff] [blame] | 384 | if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) { |
| 385 | attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]; |
| 386 | |
| 387 | batadv_netlink_set_mesh_ap_isolation(attr, bat_priv); |
| 388 | } |
| 389 | |
| 390 | if (info->attrs[BATADV_ATTR_ISOLATION_MARK]) { |
| 391 | attr = info->attrs[BATADV_ATTR_ISOLATION_MARK]; |
| 392 | |
| 393 | bat_priv->isolation_mark = nla_get_u32(attr); |
| 394 | } |
| 395 | |
| 396 | if (info->attrs[BATADV_ATTR_ISOLATION_MASK]) { |
| 397 | attr = info->attrs[BATADV_ATTR_ISOLATION_MASK]; |
| 398 | |
| 399 | bat_priv->isolation_mark_mask = nla_get_u32(attr); |
| 400 | } |
| 401 | |
Sven Eckelmann | d7e5250 | 2018-11-23 12:55:44 +0100 | [diff] [blame] | 402 | if (info->attrs[BATADV_ATTR_BONDING_ENABLED]) { |
| 403 | attr = info->attrs[BATADV_ATTR_BONDING_ENABLED]; |
| 404 | |
| 405 | atomic_set(&bat_priv->bonding, !!nla_get_u8(attr)); |
| 406 | } |
| 407 | |
Sven Eckelmann | 43ff610 | 2018-11-23 13:03:39 +0100 | [diff] [blame^] | 408 | #ifdef CONFIG_BATMAN_ADV_BLA |
| 409 | if (info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED]) { |
| 410 | attr = info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED]; |
| 411 | |
| 412 | atomic_set(&bat_priv->bridge_loop_avoidance, |
| 413 | !!nla_get_u8(attr)); |
| 414 | batadv_bla_status_update(bat_priv->soft_iface); |
| 415 | } |
| 416 | #endif /* CONFIG_BATMAN_ADV_BLA */ |
| 417 | |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 418 | batadv_netlink_notify_mesh(bat_priv); |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 419 | |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 420 | return 0; |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 423 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 424 | * batadv_netlink_tp_meter_put() - Fill information of started tp_meter session |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 425 | * @msg: netlink message to be sent back |
| 426 | * @cookie: tp meter session cookie |
| 427 | * |
| 428 | * Return: 0 on success, < 0 on error |
| 429 | */ |
| 430 | static int |
| 431 | batadv_netlink_tp_meter_put(struct sk_buff *msg, u32 cookie) |
| 432 | { |
| 433 | if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie)) |
| 434 | return -ENOBUFS; |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 440 | * batadv_netlink_tpmeter_notify() - send tp_meter result via netlink to client |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 441 | * @bat_priv: the bat priv with all the soft interface information |
| 442 | * @dst: destination of tp_meter session |
| 443 | * @result: reason for tp meter session stop |
| 444 | * @test_time: total time ot the tp_meter session |
| 445 | * @total_bytes: bytes acked to the receiver |
| 446 | * @cookie: cookie of tp_meter session |
| 447 | * |
| 448 | * Return: 0 on success, < 0 on error |
| 449 | */ |
| 450 | int batadv_netlink_tpmeter_notify(struct batadv_priv *bat_priv, const u8 *dst, |
| 451 | u8 result, u32 test_time, u64 total_bytes, |
| 452 | u32 cookie) |
| 453 | { |
| 454 | struct sk_buff *msg; |
| 455 | void *hdr; |
| 456 | int ret; |
| 457 | |
| 458 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 459 | if (!msg) |
| 460 | return -ENOMEM; |
| 461 | |
| 462 | hdr = genlmsg_put(msg, 0, 0, &batadv_netlink_family, 0, |
| 463 | BATADV_CMD_TP_METER); |
| 464 | if (!hdr) { |
| 465 | ret = -ENOBUFS; |
| 466 | goto err_genlmsg; |
| 467 | } |
| 468 | |
| 469 | if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie)) |
| 470 | goto nla_put_failure; |
| 471 | |
| 472 | if (nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, test_time)) |
| 473 | goto nla_put_failure; |
| 474 | |
| 475 | if (nla_put_u64_64bit(msg, BATADV_ATTR_TPMETER_BYTES, total_bytes, |
| 476 | BATADV_ATTR_PAD)) |
| 477 | goto nla_put_failure; |
| 478 | |
| 479 | if (nla_put_u8(msg, BATADV_ATTR_TPMETER_RESULT, result)) |
| 480 | goto nla_put_failure; |
| 481 | |
| 482 | if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst)) |
| 483 | goto nla_put_failure; |
| 484 | |
| 485 | genlmsg_end(msg, hdr); |
| 486 | |
| 487 | genlmsg_multicast_netns(&batadv_netlink_family, |
| 488 | dev_net(bat_priv->soft_iface), msg, 0, |
| 489 | BATADV_NL_MCGRP_TPMETER, GFP_KERNEL); |
| 490 | |
| 491 | return 0; |
| 492 | |
| 493 | nla_put_failure: |
| 494 | genlmsg_cancel(msg, hdr); |
| 495 | ret = -EMSGSIZE; |
| 496 | |
| 497 | err_genlmsg: |
| 498 | nlmsg_free(msg); |
| 499 | return ret; |
| 500 | } |
| 501 | |
| 502 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 503 | * batadv_netlink_tp_meter_start() - Start a new tp_meter session |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 504 | * @skb: received netlink message |
| 505 | * @info: receiver information |
| 506 | * |
| 507 | * Return: 0 on success, < 0 on error |
| 508 | */ |
| 509 | static int |
| 510 | batadv_netlink_tp_meter_start(struct sk_buff *skb, struct genl_info *info) |
| 511 | { |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 512 | struct batadv_priv *bat_priv = info->user_ptr[0]; |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 513 | struct sk_buff *msg = NULL; |
| 514 | u32 test_length; |
| 515 | void *msg_head; |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 516 | u32 cookie; |
| 517 | u8 *dst; |
| 518 | int ret; |
| 519 | |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 520 | if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS]) |
| 521 | return -EINVAL; |
| 522 | |
| 523 | if (!info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]) |
| 524 | return -EINVAL; |
| 525 | |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 526 | dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]); |
| 527 | |
| 528 | test_length = nla_get_u32(info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]); |
| 529 | |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 530 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 531 | if (!msg) { |
| 532 | ret = -ENOMEM; |
| 533 | goto out; |
| 534 | } |
| 535 | |
| 536 | msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq, |
| 537 | &batadv_netlink_family, 0, |
| 538 | BATADV_CMD_TP_METER); |
| 539 | if (!msg_head) { |
| 540 | ret = -ENOBUFS; |
| 541 | goto out; |
| 542 | } |
| 543 | |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 544 | batadv_tp_start(bat_priv, dst, test_length, &cookie); |
| 545 | |
| 546 | ret = batadv_netlink_tp_meter_put(msg, cookie); |
| 547 | |
| 548 | out: |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 549 | if (ret) { |
| 550 | if (msg) |
| 551 | nlmsg_free(msg); |
| 552 | return ret; |
| 553 | } |
| 554 | |
| 555 | genlmsg_end(msg, msg_head); |
| 556 | return genlmsg_reply(msg, info); |
| 557 | } |
| 558 | |
| 559 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 560 | * batadv_netlink_tp_meter_start() - Cancel a running tp_meter session |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 561 | * @skb: received netlink message |
| 562 | * @info: receiver information |
| 563 | * |
| 564 | * Return: 0 on success, < 0 on error |
| 565 | */ |
| 566 | static int |
| 567 | batadv_netlink_tp_meter_cancel(struct sk_buff *skb, struct genl_info *info) |
| 568 | { |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 569 | struct batadv_priv *bat_priv = info->user_ptr[0]; |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 570 | u8 *dst; |
| 571 | int ret = 0; |
| 572 | |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 573 | if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS]) |
| 574 | return -EINVAL; |
| 575 | |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 576 | dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]); |
| 577 | |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 578 | batadv_tp_stop(bat_priv, dst, BATADV_TP_REASON_CANCEL); |
| 579 | |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 580 | return ret; |
| 581 | } |
| 582 | |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 583 | /** |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 584 | * batadv_netlink_hardif_fill() - Fill message with hardif attributes |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 585 | * @msg: Netlink message to dump into |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 586 | * @bat_priv: the bat priv with all the soft interface information |
| 587 | * @hard_iface: hard interface which was modified |
| 588 | * @cmd: type of message to generate |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 589 | * @portid: Port making netlink request |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 590 | * @seq: sequence number for message |
| 591 | * @flags: Additional flags for message |
Sven Eckelmann | fb69be6 | 2018-10-30 22:01:24 +0100 | [diff] [blame] | 592 | * @cb: Control block containing additional options |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 593 | * |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 594 | * Return: 0 on success or negative error number in case of failure |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 595 | */ |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 596 | static int batadv_netlink_hardif_fill(struct sk_buff *msg, |
| 597 | struct batadv_priv *bat_priv, |
| 598 | struct batadv_hard_iface *hard_iface, |
| 599 | enum batadv_nl_commands cmd, |
| 600 | u32 portid, u32 seq, int flags, |
| 601 | struct netlink_callback *cb) |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 602 | { |
| 603 | struct net_device *net_dev = hard_iface->net_dev; |
| 604 | void *hdr; |
| 605 | |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 606 | hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd); |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 607 | if (!hdr) |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 608 | return -ENOBUFS; |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 609 | |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 610 | if (cb) |
| 611 | genl_dump_check_consistent(cb, hdr); |
| 612 | |
| 613 | if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, |
| 614 | bat_priv->soft_iface->ifindex)) |
| 615 | goto nla_put_failure; |
Sven Eckelmann | fb69be6 | 2018-10-30 22:01:24 +0100 | [diff] [blame] | 616 | |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 617 | if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX, |
| 618 | net_dev->ifindex) || |
| 619 | nla_put_string(msg, BATADV_ATTR_HARD_IFNAME, |
| 620 | net_dev->name) || |
| 621 | nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN, |
| 622 | net_dev->dev_addr)) |
| 623 | goto nla_put_failure; |
| 624 | |
| 625 | if (hard_iface->if_status == BATADV_IF_ACTIVE) { |
| 626 | if (nla_put_flag(msg, BATADV_ATTR_ACTIVE)) |
| 627 | goto nla_put_failure; |
| 628 | } |
| 629 | |
| 630 | genlmsg_end(msg, hdr); |
| 631 | return 0; |
| 632 | |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 633 | nla_put_failure: |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 634 | genlmsg_cancel(msg, hdr); |
| 635 | return -EMSGSIZE; |
| 636 | } |
| 637 | |
| 638 | /** |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 639 | * batadv_netlink_notify_hardif() - send hardif attributes to listener |
| 640 | * @bat_priv: the bat priv with all the soft interface information |
| 641 | * @hard_iface: hard interface which was modified |
| 642 | * |
| 643 | * Return: 0 on success, < 0 on error |
| 644 | */ |
| 645 | static int batadv_netlink_notify_hardif(struct batadv_priv *bat_priv, |
| 646 | struct batadv_hard_iface *hard_iface) |
| 647 | { |
| 648 | struct sk_buff *msg; |
| 649 | int ret; |
| 650 | |
| 651 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 652 | if (!msg) |
| 653 | return -ENOMEM; |
| 654 | |
| 655 | ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface, |
| 656 | BATADV_CMD_SET_HARDIF, 0, 0, 0, NULL); |
| 657 | if (ret < 0) { |
| 658 | nlmsg_free(msg); |
| 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | genlmsg_multicast_netns(&batadv_netlink_family, |
| 663 | dev_net(bat_priv->soft_iface), msg, 0, |
| 664 | BATADV_NL_MCGRP_CONFIG, GFP_KERNEL); |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * batadv_netlink_get_hardif() - Get hardif attributes |
| 671 | * @skb: Netlink message with request data |
| 672 | * @info: receiver information |
| 673 | * |
| 674 | * Return: 0 on success or negative error number in case of failure |
| 675 | */ |
| 676 | static int batadv_netlink_get_hardif(struct sk_buff *skb, |
| 677 | struct genl_info *info) |
| 678 | { |
| 679 | struct batadv_hard_iface *hard_iface = info->user_ptr[1]; |
| 680 | struct batadv_priv *bat_priv = info->user_ptr[0]; |
| 681 | struct sk_buff *msg; |
| 682 | int ret; |
| 683 | |
| 684 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 685 | if (!msg) |
| 686 | return -ENOMEM; |
| 687 | |
| 688 | ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface, |
| 689 | BATADV_CMD_GET_HARDIF, |
| 690 | info->snd_portid, info->snd_seq, 0, |
| 691 | NULL); |
| 692 | if (ret < 0) { |
| 693 | nlmsg_free(msg); |
| 694 | return ret; |
| 695 | } |
| 696 | |
| 697 | ret = genlmsg_reply(msg, info); |
| 698 | |
| 699 | return ret; |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * batadv_netlink_set_hardif() - Set hardif attributes |
| 704 | * @skb: Netlink message with request data |
| 705 | * @info: receiver information |
| 706 | * |
| 707 | * Return: 0 on success or negative error number in case of failure |
| 708 | */ |
| 709 | static int batadv_netlink_set_hardif(struct sk_buff *skb, |
| 710 | struct genl_info *info) |
| 711 | { |
| 712 | struct batadv_hard_iface *hard_iface = info->user_ptr[1]; |
| 713 | struct batadv_priv *bat_priv = info->user_ptr[0]; |
| 714 | |
| 715 | batadv_netlink_notify_hardif(bat_priv, hard_iface); |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * batadv_netlink_dump_hardif() - Dump all hard interface into a messages |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 722 | * @msg: Netlink message to dump into |
| 723 | * @cb: Parameters from query |
| 724 | * |
| 725 | * Return: error code, or length of reply message on success |
| 726 | */ |
| 727 | static int |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 728 | batadv_netlink_dump_hardif(struct sk_buff *msg, struct netlink_callback *cb) |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 729 | { |
| 730 | struct net *net = sock_net(cb->skb->sk); |
| 731 | struct net_device *soft_iface; |
| 732 | struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 733 | struct batadv_priv *bat_priv; |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 734 | int ifindex; |
| 735 | int portid = NETLINK_CB(cb->skb).portid; |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 736 | int skip = cb->args[0]; |
| 737 | int i = 0; |
| 738 | |
| 739 | ifindex = batadv_netlink_get_ifindex(cb->nlh, |
| 740 | BATADV_ATTR_MESH_IFINDEX); |
| 741 | if (!ifindex) |
| 742 | return -EINVAL; |
| 743 | |
| 744 | soft_iface = dev_get_by_index(net, ifindex); |
| 745 | if (!soft_iface) |
| 746 | return -ENODEV; |
| 747 | |
| 748 | if (!batadv_softif_is_valid(soft_iface)) { |
| 749 | dev_put(soft_iface); |
| 750 | return -ENODEV; |
| 751 | } |
| 752 | |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 753 | bat_priv = netdev_priv(soft_iface); |
| 754 | |
Sven Eckelmann | fb69be6 | 2018-10-30 22:01:24 +0100 | [diff] [blame] | 755 | rtnl_lock(); |
| 756 | cb->seq = batadv_hardif_generation << 1 | 1; |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 757 | |
Sven Eckelmann | fb69be6 | 2018-10-30 22:01:24 +0100 | [diff] [blame] | 758 | list_for_each_entry(hard_iface, &batadv_hardif_list, list) { |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 759 | if (hard_iface->soft_iface != soft_iface) |
| 760 | continue; |
| 761 | |
| 762 | if (i++ < skip) |
| 763 | continue; |
| 764 | |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 765 | if (batadv_netlink_hardif_fill(msg, bat_priv, hard_iface, |
| 766 | BATADV_CMD_GET_HARDIF, |
| 767 | portid, cb->nlh->nlmsg_seq, |
| 768 | NLM_F_MULTI, cb)) { |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 769 | i--; |
| 770 | break; |
| 771 | } |
| 772 | } |
| 773 | |
Sven Eckelmann | fb69be6 | 2018-10-30 22:01:24 +0100 | [diff] [blame] | 774 | rtnl_unlock(); |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 775 | |
| 776 | dev_put(soft_iface); |
| 777 | |
| 778 | cb->args[0] = i; |
| 779 | |
| 780 | return msg->len; |
| 781 | } |
| 782 | |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 783 | /** |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 784 | * batadv_netlink_vlan_fill() - Fill message with vlan attributes |
| 785 | * @msg: Netlink message to dump into |
| 786 | * @bat_priv: the bat priv with all the soft interface information |
| 787 | * @vlan: vlan which was modified |
| 788 | * @cmd: type of message to generate |
| 789 | * @portid: Port making netlink request |
| 790 | * @seq: sequence number for message |
| 791 | * @flags: Additional flags for message |
| 792 | * |
| 793 | * Return: 0 on success or negative error number in case of failure |
| 794 | */ |
| 795 | static int batadv_netlink_vlan_fill(struct sk_buff *msg, |
| 796 | struct batadv_priv *bat_priv, |
| 797 | struct batadv_softif_vlan *vlan, |
| 798 | enum batadv_nl_commands cmd, |
| 799 | u32 portid, u32 seq, int flags) |
| 800 | { |
| 801 | void *hdr; |
| 802 | |
| 803 | hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd); |
| 804 | if (!hdr) |
| 805 | return -ENOBUFS; |
| 806 | |
| 807 | if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, |
| 808 | bat_priv->soft_iface->ifindex)) |
| 809 | goto nla_put_failure; |
| 810 | |
| 811 | if (nla_put_u32(msg, BATADV_ATTR_VLANID, vlan->vid & VLAN_VID_MASK)) |
| 812 | goto nla_put_failure; |
| 813 | |
Sven Eckelmann | e43d16b | 2018-11-23 12:51:55 +0100 | [diff] [blame] | 814 | if (nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED, |
| 815 | !!atomic_read(&vlan->ap_isolation))) |
| 816 | goto nla_put_failure; |
| 817 | |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 818 | genlmsg_end(msg, hdr); |
| 819 | return 0; |
| 820 | |
| 821 | nla_put_failure: |
| 822 | genlmsg_cancel(msg, hdr); |
| 823 | return -EMSGSIZE; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * batadv_netlink_notify_vlan() - send vlan attributes to listener |
| 828 | * @bat_priv: the bat priv with all the soft interface information |
| 829 | * @vlan: vlan which was modified |
| 830 | * |
| 831 | * Return: 0 on success, < 0 on error |
| 832 | */ |
| 833 | static int batadv_netlink_notify_vlan(struct batadv_priv *bat_priv, |
| 834 | struct batadv_softif_vlan *vlan) |
| 835 | { |
| 836 | struct sk_buff *msg; |
| 837 | int ret; |
| 838 | |
| 839 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 840 | if (!msg) |
| 841 | return -ENOMEM; |
| 842 | |
| 843 | ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan, |
| 844 | BATADV_CMD_SET_VLAN, 0, 0, 0); |
| 845 | if (ret < 0) { |
| 846 | nlmsg_free(msg); |
| 847 | return ret; |
| 848 | } |
| 849 | |
| 850 | genlmsg_multicast_netns(&batadv_netlink_family, |
| 851 | dev_net(bat_priv->soft_iface), msg, 0, |
| 852 | BATADV_NL_MCGRP_CONFIG, GFP_KERNEL); |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * batadv_netlink_get_vlan() - Get vlan attributes |
| 859 | * @skb: Netlink message with request data |
| 860 | * @info: receiver information |
| 861 | * |
| 862 | * Return: 0 on success or negative error number in case of failure |
| 863 | */ |
| 864 | static int batadv_netlink_get_vlan(struct sk_buff *skb, struct genl_info *info) |
| 865 | { |
| 866 | struct batadv_softif_vlan *vlan = info->user_ptr[1]; |
| 867 | struct batadv_priv *bat_priv = info->user_ptr[0]; |
| 868 | struct sk_buff *msg; |
| 869 | int ret; |
| 870 | |
| 871 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 872 | if (!msg) |
| 873 | return -ENOMEM; |
| 874 | |
| 875 | ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan, BATADV_CMD_GET_VLAN, |
| 876 | info->snd_portid, info->snd_seq, 0); |
| 877 | if (ret < 0) { |
| 878 | nlmsg_free(msg); |
| 879 | return ret; |
| 880 | } |
| 881 | |
| 882 | ret = genlmsg_reply(msg, info); |
| 883 | |
| 884 | return ret; |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * batadv_netlink_set_vlan() - Get vlan attributes |
| 889 | * @skb: Netlink message with request data |
| 890 | * @info: receiver information |
| 891 | * |
| 892 | * Return: 0 on success or negative error number in case of failure |
| 893 | */ |
| 894 | static int batadv_netlink_set_vlan(struct sk_buff *skb, struct genl_info *info) |
| 895 | { |
| 896 | struct batadv_softif_vlan *vlan = info->user_ptr[1]; |
| 897 | struct batadv_priv *bat_priv = info->user_ptr[0]; |
Sven Eckelmann | e43d16b | 2018-11-23 12:51:55 +0100 | [diff] [blame] | 898 | struct nlattr *attr; |
| 899 | |
| 900 | if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) { |
| 901 | attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]; |
| 902 | |
| 903 | atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr)); |
| 904 | } |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 905 | |
| 906 | batadv_netlink_notify_vlan(bat_priv, vlan); |
| 907 | |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | /** |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 912 | * batadv_get_softif_from_info() - Retrieve soft interface from genl attributes |
| 913 | * @net: the applicable net namespace |
| 914 | * @info: receiver information |
| 915 | * |
| 916 | * Return: Pointer to soft interface (with increased refcnt) on success, error |
| 917 | * pointer on error |
| 918 | */ |
| 919 | static struct net_device * |
| 920 | batadv_get_softif_from_info(struct net *net, struct genl_info *info) |
| 921 | { |
| 922 | struct net_device *soft_iface; |
| 923 | int ifindex; |
| 924 | |
| 925 | if (!info->attrs[BATADV_ATTR_MESH_IFINDEX]) |
| 926 | return ERR_PTR(-EINVAL); |
| 927 | |
| 928 | ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]); |
| 929 | |
| 930 | soft_iface = dev_get_by_index(net, ifindex); |
| 931 | if (!soft_iface) |
| 932 | return ERR_PTR(-ENODEV); |
| 933 | |
| 934 | if (!batadv_softif_is_valid(soft_iface)) |
| 935 | goto err_put_softif; |
| 936 | |
| 937 | return soft_iface; |
| 938 | |
| 939 | err_put_softif: |
| 940 | dev_put(soft_iface); |
| 941 | |
| 942 | return ERR_PTR(-EINVAL); |
| 943 | } |
| 944 | |
| 945 | /** |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 946 | * batadv_get_hardif_from_info() - Retrieve hardif from genl attributes |
| 947 | * @bat_priv: the bat priv with all the soft interface information |
| 948 | * @net: the applicable net namespace |
| 949 | * @info: receiver information |
| 950 | * |
| 951 | * Return: Pointer to hard interface (with increased refcnt) on success, error |
| 952 | * pointer on error |
| 953 | */ |
| 954 | static struct batadv_hard_iface * |
| 955 | batadv_get_hardif_from_info(struct batadv_priv *bat_priv, struct net *net, |
| 956 | struct genl_info *info) |
| 957 | { |
| 958 | struct batadv_hard_iface *hard_iface; |
| 959 | struct net_device *hard_dev; |
| 960 | unsigned int hardif_index; |
| 961 | |
| 962 | if (!info->attrs[BATADV_ATTR_HARD_IFINDEX]) |
| 963 | return ERR_PTR(-EINVAL); |
| 964 | |
| 965 | hardif_index = nla_get_u32(info->attrs[BATADV_ATTR_HARD_IFINDEX]); |
| 966 | |
| 967 | hard_dev = dev_get_by_index(net, hardif_index); |
| 968 | if (!hard_dev) |
| 969 | return ERR_PTR(-ENODEV); |
| 970 | |
| 971 | hard_iface = batadv_hardif_get_by_netdev(hard_dev); |
| 972 | if (!hard_iface) |
| 973 | goto err_put_harddev; |
| 974 | |
| 975 | if (hard_iface->soft_iface != bat_priv->soft_iface) |
| 976 | goto err_put_hardif; |
| 977 | |
| 978 | /* hard_dev is referenced by hard_iface and not needed here */ |
| 979 | dev_put(hard_dev); |
| 980 | |
| 981 | return hard_iface; |
| 982 | |
| 983 | err_put_hardif: |
| 984 | batadv_hardif_put(hard_iface); |
| 985 | err_put_harddev: |
| 986 | dev_put(hard_dev); |
| 987 | |
| 988 | return ERR_PTR(-EINVAL); |
| 989 | } |
| 990 | |
| 991 | /** |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 992 | * batadv_get_vlan_from_info() - Retrieve vlan from genl attributes |
| 993 | * @bat_priv: the bat priv with all the soft interface information |
| 994 | * @net: the applicable net namespace |
| 995 | * @info: receiver information |
| 996 | * |
| 997 | * Return: Pointer to vlan on success (with increased refcnt), error pointer |
| 998 | * on error |
| 999 | */ |
| 1000 | static struct batadv_softif_vlan * |
| 1001 | batadv_get_vlan_from_info(struct batadv_priv *bat_priv, struct net *net, |
| 1002 | struct genl_info *info) |
| 1003 | { |
| 1004 | struct batadv_softif_vlan *vlan; |
| 1005 | u16 vid; |
| 1006 | |
| 1007 | if (!info->attrs[BATADV_ATTR_VLANID]) |
| 1008 | return ERR_PTR(-EINVAL); |
| 1009 | |
| 1010 | vid = nla_get_u16(info->attrs[BATADV_ATTR_VLANID]); |
| 1011 | |
| 1012 | vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG); |
| 1013 | if (!vlan) |
| 1014 | return ERR_PTR(-ENOENT); |
| 1015 | |
| 1016 | return vlan; |
| 1017 | } |
| 1018 | |
| 1019 | /** |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 1020 | * batadv_pre_doit() - Prepare batman-adv genl doit request |
| 1021 | * @ops: requested netlink operation |
| 1022 | * @skb: Netlink message with request data |
| 1023 | * @info: receiver information |
| 1024 | * |
| 1025 | * Return: 0 on success or negative error number in case of failure |
| 1026 | */ |
| 1027 | static int batadv_pre_doit(const struct genl_ops *ops, struct sk_buff *skb, |
| 1028 | struct genl_info *info) |
| 1029 | { |
| 1030 | struct net *net = genl_info_net(info); |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 1031 | struct batadv_hard_iface *hard_iface; |
| 1032 | struct batadv_priv *bat_priv = NULL; |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 1033 | struct batadv_softif_vlan *vlan; |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 1034 | struct net_device *soft_iface; |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 1035 | u8 user_ptr1_flags; |
| 1036 | u8 mesh_dep_flags; |
| 1037 | int ret; |
| 1038 | |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 1039 | user_ptr1_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN; |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 1040 | if (WARN_ON(hweight8(ops->internal_flags & user_ptr1_flags) > 1)) |
| 1041 | return -EINVAL; |
| 1042 | |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 1043 | mesh_dep_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN; |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 1044 | if (WARN_ON((ops->internal_flags & mesh_dep_flags) && |
| 1045 | (~ops->internal_flags & BATADV_FLAG_NEED_MESH))) |
| 1046 | return -EINVAL; |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 1047 | |
| 1048 | if (ops->internal_flags & BATADV_FLAG_NEED_MESH) { |
| 1049 | soft_iface = batadv_get_softif_from_info(net, info); |
| 1050 | if (IS_ERR(soft_iface)) |
| 1051 | return PTR_ERR(soft_iface); |
| 1052 | |
| 1053 | bat_priv = netdev_priv(soft_iface); |
| 1054 | info->user_ptr[0] = bat_priv; |
| 1055 | } |
| 1056 | |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 1057 | if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF) { |
| 1058 | hard_iface = batadv_get_hardif_from_info(bat_priv, net, info); |
| 1059 | if (IS_ERR(hard_iface)) { |
| 1060 | ret = PTR_ERR(hard_iface); |
| 1061 | goto err_put_softif; |
| 1062 | } |
| 1063 | |
| 1064 | info->user_ptr[1] = hard_iface; |
| 1065 | } |
| 1066 | |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 1067 | if (ops->internal_flags & BATADV_FLAG_NEED_VLAN) { |
| 1068 | vlan = batadv_get_vlan_from_info(bat_priv, net, info); |
| 1069 | if (IS_ERR(vlan)) { |
| 1070 | ret = PTR_ERR(vlan); |
| 1071 | goto err_put_softif; |
| 1072 | } |
| 1073 | |
| 1074 | info->user_ptr[1] = vlan; |
| 1075 | } |
| 1076 | |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 1077 | return 0; |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 1078 | |
| 1079 | err_put_softif: |
| 1080 | if (bat_priv) |
| 1081 | dev_put(bat_priv->soft_iface); |
| 1082 | |
| 1083 | return ret; |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /** |
| 1087 | * batadv_post_doit() - End batman-adv genl doit request |
| 1088 | * @ops: requested netlink operation |
| 1089 | * @skb: Netlink message with request data |
| 1090 | * @info: receiver information |
| 1091 | */ |
| 1092 | static void batadv_post_doit(const struct genl_ops *ops, struct sk_buff *skb, |
| 1093 | struct genl_info *info) |
| 1094 | { |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 1095 | struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 1096 | struct batadv_softif_vlan *vlan; |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 1097 | struct batadv_priv *bat_priv; |
| 1098 | |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 1099 | if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF && |
| 1100 | info->user_ptr[1]) { |
| 1101 | hard_iface = info->user_ptr[1]; |
| 1102 | |
| 1103 | batadv_hardif_put(hard_iface); |
| 1104 | } |
| 1105 | |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 1106 | if (ops->internal_flags & BATADV_FLAG_NEED_VLAN && info->user_ptr[1]) { |
| 1107 | vlan = info->user_ptr[1]; |
| 1108 | batadv_softif_vlan_put(vlan); |
| 1109 | } |
| 1110 | |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 1111 | if (ops->internal_flags & BATADV_FLAG_NEED_MESH && info->user_ptr[0]) { |
| 1112 | bat_priv = info->user_ptr[0]; |
| 1113 | dev_put(bat_priv->soft_iface); |
| 1114 | } |
| 1115 | } |
| 1116 | |
Sven Eckelmann | ce1a21d | 2016-10-18 15:57:36 +0200 | [diff] [blame] | 1117 | static const struct genl_ops batadv_netlink_ops[] = { |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 1118 | { |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 1119 | .cmd = BATADV_CMD_GET_MESH, |
| 1120 | /* can be retrieved by unprivileged users */ |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 1121 | .policy = batadv_netlink_policy, |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 1122 | .doit = batadv_netlink_get_mesh, |
| 1123 | .internal_flags = BATADV_FLAG_NEED_MESH, |
Matthias Schiffer | 5da0aef | 2016-05-09 18:41:09 +0200 | [diff] [blame] | 1124 | }, |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 1125 | { |
| 1126 | .cmd = BATADV_CMD_TP_METER, |
| 1127 | .flags = GENL_ADMIN_PERM, |
| 1128 | .policy = batadv_netlink_policy, |
| 1129 | .doit = batadv_netlink_tp_meter_start, |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 1130 | .internal_flags = BATADV_FLAG_NEED_MESH, |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 1131 | }, |
| 1132 | { |
| 1133 | .cmd = BATADV_CMD_TP_METER_CANCEL, |
| 1134 | .flags = GENL_ADMIN_PERM, |
| 1135 | .policy = batadv_netlink_policy, |
| 1136 | .doit = batadv_netlink_tp_meter_cancel, |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 1137 | .internal_flags = BATADV_FLAG_NEED_MESH, |
Antonio Quartulli | 33a3bb4 | 2016-05-05 13:09:43 +0200 | [diff] [blame] | 1138 | }, |
Matthias Schiffer | 07a3061 | 2016-07-03 13:31:35 +0200 | [diff] [blame] | 1139 | { |
| 1140 | .cmd = BATADV_CMD_GET_ROUTING_ALGOS, |
| 1141 | .flags = GENL_ADMIN_PERM, |
| 1142 | .policy = batadv_netlink_policy, |
| 1143 | .dumpit = batadv_algo_dump, |
| 1144 | }, |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 1145 | { |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 1146 | .cmd = BATADV_CMD_GET_HARDIF, |
| 1147 | /* can be retrieved by unprivileged users */ |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 1148 | .policy = batadv_netlink_policy, |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 1149 | .dumpit = batadv_netlink_dump_hardif, |
| 1150 | .doit = batadv_netlink_get_hardif, |
| 1151 | .internal_flags = BATADV_FLAG_NEED_MESH | |
| 1152 | BATADV_FLAG_NEED_HARDIF, |
Matthias Schiffer | b60620c | 2016-07-03 13:31:36 +0200 | [diff] [blame] | 1153 | }, |
Matthias Schiffer | d34f055 | 2016-07-03 13:31:37 +0200 | [diff] [blame] | 1154 | { |
| 1155 | .cmd = BATADV_CMD_GET_TRANSTABLE_LOCAL, |
| 1156 | .flags = GENL_ADMIN_PERM, |
| 1157 | .policy = batadv_netlink_policy, |
| 1158 | .dumpit = batadv_tt_local_dump, |
| 1159 | }, |
| 1160 | { |
| 1161 | .cmd = BATADV_CMD_GET_TRANSTABLE_GLOBAL, |
| 1162 | .flags = GENL_ADMIN_PERM, |
| 1163 | .policy = batadv_netlink_policy, |
| 1164 | .dumpit = batadv_tt_global_dump, |
| 1165 | }, |
Matthias Schiffer | 85cf8c8 | 2016-07-03 13:31:39 +0200 | [diff] [blame] | 1166 | { |
| 1167 | .cmd = BATADV_CMD_GET_ORIGINATORS, |
| 1168 | .flags = GENL_ADMIN_PERM, |
| 1169 | .policy = batadv_netlink_policy, |
| 1170 | .dumpit = batadv_orig_dump, |
| 1171 | }, |
| 1172 | { |
| 1173 | .cmd = BATADV_CMD_GET_NEIGHBORS, |
| 1174 | .flags = GENL_ADMIN_PERM, |
| 1175 | .policy = batadv_netlink_policy, |
| 1176 | .dumpit = batadv_hardif_neigh_dump, |
| 1177 | }, |
Sven Eckelmann | d7129da | 2016-07-03 13:31:42 +0200 | [diff] [blame] | 1178 | { |
| 1179 | .cmd = BATADV_CMD_GET_GATEWAYS, |
| 1180 | .flags = GENL_ADMIN_PERM, |
| 1181 | .policy = batadv_netlink_policy, |
| 1182 | .dumpit = batadv_gw_dump, |
| 1183 | }, |
Andrew Lunn | 04f3f5b | 2016-07-03 13:31:45 +0200 | [diff] [blame] | 1184 | { |
| 1185 | .cmd = BATADV_CMD_GET_BLA_CLAIM, |
| 1186 | .flags = GENL_ADMIN_PERM, |
| 1187 | .policy = batadv_netlink_policy, |
| 1188 | .dumpit = batadv_bla_claim_dump, |
| 1189 | }, |
Simon Wunderlich | ea4152e | 2016-07-03 13:31:47 +0200 | [diff] [blame] | 1190 | { |
| 1191 | .cmd = BATADV_CMD_GET_BLA_BACKBONE, |
| 1192 | .flags = GENL_ADMIN_PERM, |
| 1193 | .policy = batadv_netlink_policy, |
| 1194 | .dumpit = batadv_bla_backbone_dump, |
| 1195 | }, |
Linus Lüssing | 41aeefc | 2018-03-13 11:41:12 +0100 | [diff] [blame] | 1196 | { |
| 1197 | .cmd = BATADV_CMD_GET_DAT_CACHE, |
| 1198 | .flags = GENL_ADMIN_PERM, |
| 1199 | .policy = batadv_netlink_policy, |
| 1200 | .dumpit = batadv_dat_cache_dump, |
| 1201 | }, |
Linus Lüssing | 53dd9a6 | 2018-03-13 11:41:13 +0100 | [diff] [blame] | 1202 | { |
| 1203 | .cmd = BATADV_CMD_GET_MCAST_FLAGS, |
| 1204 | .flags = GENL_ADMIN_PERM, |
| 1205 | .policy = batadv_netlink_policy, |
| 1206 | .dumpit = batadv_mcast_flags_dump, |
| 1207 | }, |
Sven Eckelmann | 6004051 | 2018-11-23 12:14:56 +0100 | [diff] [blame] | 1208 | { |
| 1209 | .cmd = BATADV_CMD_SET_MESH, |
| 1210 | .flags = GENL_ADMIN_PERM, |
| 1211 | .policy = batadv_netlink_policy, |
| 1212 | .doit = batadv_netlink_set_mesh, |
| 1213 | .internal_flags = BATADV_FLAG_NEED_MESH, |
| 1214 | }, |
Sven Eckelmann | 5c55a40 | 2018-11-23 12:33:17 +0100 | [diff] [blame] | 1215 | { |
| 1216 | .cmd = BATADV_CMD_SET_HARDIF, |
| 1217 | .flags = GENL_ADMIN_PERM, |
| 1218 | .policy = batadv_netlink_policy, |
| 1219 | .doit = batadv_netlink_set_hardif, |
| 1220 | .internal_flags = BATADV_FLAG_NEED_MESH | |
| 1221 | BATADV_FLAG_NEED_HARDIF, |
| 1222 | }, |
Sven Eckelmann | 49e7e37 | 2018-11-23 12:41:08 +0100 | [diff] [blame] | 1223 | { |
| 1224 | .cmd = BATADV_CMD_GET_VLAN, |
| 1225 | /* can be retrieved by unprivileged users */ |
| 1226 | .policy = batadv_netlink_policy, |
| 1227 | .doit = batadv_netlink_get_vlan, |
| 1228 | .internal_flags = BATADV_FLAG_NEED_MESH | |
| 1229 | BATADV_FLAG_NEED_VLAN, |
| 1230 | }, |
| 1231 | { |
| 1232 | .cmd = BATADV_CMD_SET_VLAN, |
| 1233 | .flags = GENL_ADMIN_PERM, |
| 1234 | .policy = batadv_netlink_policy, |
| 1235 | .doit = batadv_netlink_set_vlan, |
| 1236 | .internal_flags = BATADV_FLAG_NEED_MESH | |
| 1237 | BATADV_FLAG_NEED_VLAN, |
| 1238 | }, |
Matthias Schiffer | 09748a2 | 2016-05-09 18:41:08 +0200 | [diff] [blame] | 1239 | }; |
| 1240 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 1241 | struct genl_family batadv_netlink_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1242 | .hdrsize = 0, |
| 1243 | .name = BATADV_NL_NAME, |
| 1244 | .version = 1, |
| 1245 | .maxattr = BATADV_ATTR_MAX, |
| 1246 | .netnsok = true, |
Sven Eckelmann | c4a7a8d | 2018-11-23 12:00:28 +0100 | [diff] [blame] | 1247 | .pre_doit = batadv_pre_doit, |
| 1248 | .post_doit = batadv_post_doit, |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1249 | .module = THIS_MODULE, |
| 1250 | .ops = batadv_netlink_ops, |
| 1251 | .n_ops = ARRAY_SIZE(batadv_netlink_ops), |
| 1252 | .mcgrps = batadv_netlink_mcgrps, |
| 1253 | .n_mcgrps = ARRAY_SIZE(batadv_netlink_mcgrps), |
| 1254 | }; |
| 1255 | |
Matthias Schiffer | 09748a2 | 2016-05-09 18:41:08 +0200 | [diff] [blame] | 1256 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 1257 | * batadv_netlink_register() - register batadv genl netlink family |
Matthias Schiffer | 09748a2 | 2016-05-09 18:41:08 +0200 | [diff] [blame] | 1258 | */ |
| 1259 | void __init batadv_netlink_register(void) |
| 1260 | { |
| 1261 | int ret; |
| 1262 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1263 | ret = genl_register_family(&batadv_netlink_family); |
Matthias Schiffer | 09748a2 | 2016-05-09 18:41:08 +0200 | [diff] [blame] | 1264 | if (ret) |
| 1265 | pr_warn("unable to register netlink family"); |
| 1266 | } |
| 1267 | |
| 1268 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 1269 | * batadv_netlink_unregister() - unregister batadv genl netlink family |
Matthias Schiffer | 09748a2 | 2016-05-09 18:41:08 +0200 | [diff] [blame] | 1270 | */ |
| 1271 | void batadv_netlink_unregister(void) |
| 1272 | { |
| 1273 | genl_unregister_family(&batadv_netlink_family); |
| 1274 | } |