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