1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) 2016-2019 B.A.T.M.A.N. contributors: 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 22 #include <linux/atomic.h> 23 #include <linux/bitops.h> 24 #include <linux/bug.h> 25 #include <linux/byteorder/generic.h> 26 #include <linux/cache.h> 27 #include <linux/err.h> 28 #include <linux/errno.h> 29 #include <linux/export.h> 30 #include <linux/genetlink.h> 31 #include <linux/gfp.h> 32 #include <linux/if_ether.h> 33 #include <linux/if_vlan.h> 34 #include <linux/init.h> 35 #include <linux/kernel.h> 36 #include <linux/list.h> 37 #include <linux/netdevice.h> 38 #include <linux/netlink.h> 39 #include <linux/printk.h> 40 #include <linux/rtnetlink.h> 41 #include <linux/skbuff.h> 42 #include <linux/stddef.h> 43 #include <linux/types.h> 44 #include <net/genetlink.h> 45 #include <net/netlink.h> 46 #include <net/sock.h> 47 #include <uapi/linux/batadv_packet.h> 48 #include <uapi/linux/batman_adv.h> 49 50 #include "bat_algo.h" 51 #include "bridge_loop_avoidance.h" 52 #include "distributed-arp-table.h" 53 #include "gateway_client.h" 54 #include "gateway_common.h" 55 #include "hard-interface.h" 56 #include "log.h" 57 #include "multicast.h" 58 #include "network-coding.h" 59 #include "originator.h" 60 #include "soft-interface.h" 61 #include "tp_meter.h" 62 #include "translation-table.h" 63 64 struct net; 65 66 struct genl_family batadv_netlink_family; 67 68 /* multicast groups */ 69 enum batadv_netlink_multicast_groups { 70 BATADV_NL_MCGRP_CONFIG, 71 BATADV_NL_MCGRP_TPMETER, 72 }; 73 74 /** 75 * enum batadv_genl_ops_flags - flags for genl_ops's internal_flags 76 */ 77 enum batadv_genl_ops_flags { 78 /** 79 * @BATADV_FLAG_NEED_MESH: request requires valid soft interface in 80 * attribute BATADV_ATTR_MESH_IFINDEX and expects a pointer to it to be 81 * saved in info->user_ptr[0] 82 */ 83 BATADV_FLAG_NEED_MESH = BIT(0), 84 85 /** 86 * @BATADV_FLAG_NEED_HARDIF: request requires valid hard interface in 87 * attribute BATADV_ATTR_HARD_IFINDEX and expects a pointer to it to be 88 * saved in info->user_ptr[1] 89 */ 90 BATADV_FLAG_NEED_HARDIF = BIT(1), 91 92 /** 93 * @BATADV_FLAG_NEED_VLAN: request requires valid vlan in 94 * attribute BATADV_ATTR_VLANID and expects a pointer to it to be 95 * saved in info->user_ptr[1] 96 */ 97 BATADV_FLAG_NEED_VLAN = BIT(2), 98 }; 99 100 static const struct genl_multicast_group batadv_netlink_mcgrps[] = { 101 [BATADV_NL_MCGRP_CONFIG] = { .name = BATADV_NL_MCAST_GROUP_CONFIG }, 102 [BATADV_NL_MCGRP_TPMETER] = { .name = BATADV_NL_MCAST_GROUP_TPMETER }, 103 }; 104 105 static const struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { 106 [BATADV_ATTR_VERSION] = { .type = NLA_STRING }, 107 [BATADV_ATTR_ALGO_NAME] = { .type = NLA_STRING }, 108 [BATADV_ATTR_MESH_IFINDEX] = { .type = NLA_U32 }, 109 [BATADV_ATTR_MESH_IFNAME] = { .type = NLA_STRING }, 110 [BATADV_ATTR_MESH_ADDRESS] = { .len = ETH_ALEN }, 111 [BATADV_ATTR_HARD_IFINDEX] = { .type = NLA_U32 }, 112 [BATADV_ATTR_HARD_IFNAME] = { .type = NLA_STRING }, 113 [BATADV_ATTR_HARD_ADDRESS] = { .len = ETH_ALEN }, 114 [BATADV_ATTR_ORIG_ADDRESS] = { .len = ETH_ALEN }, 115 [BATADV_ATTR_TPMETER_RESULT] = { .type = NLA_U8 }, 116 [BATADV_ATTR_TPMETER_TEST_TIME] = { .type = NLA_U32 }, 117 [BATADV_ATTR_TPMETER_BYTES] = { .type = NLA_U64 }, 118 [BATADV_ATTR_TPMETER_COOKIE] = { .type = NLA_U32 }, 119 [BATADV_ATTR_ACTIVE] = { .type = NLA_FLAG }, 120 [BATADV_ATTR_TT_ADDRESS] = { .len = ETH_ALEN }, 121 [BATADV_ATTR_TT_TTVN] = { .type = NLA_U8 }, 122 [BATADV_ATTR_TT_LAST_TTVN] = { .type = NLA_U8 }, 123 [BATADV_ATTR_TT_CRC32] = { .type = NLA_U32 }, 124 [BATADV_ATTR_TT_VID] = { .type = NLA_U16 }, 125 [BATADV_ATTR_TT_FLAGS] = { .type = NLA_U32 }, 126 [BATADV_ATTR_FLAG_BEST] = { .type = NLA_FLAG }, 127 [BATADV_ATTR_LAST_SEEN_MSECS] = { .type = NLA_U32 }, 128 [BATADV_ATTR_NEIGH_ADDRESS] = { .len = ETH_ALEN }, 129 [BATADV_ATTR_TQ] = { .type = NLA_U8 }, 130 [BATADV_ATTR_THROUGHPUT] = { .type = NLA_U32 }, 131 [BATADV_ATTR_BANDWIDTH_UP] = { .type = NLA_U32 }, 132 [BATADV_ATTR_BANDWIDTH_DOWN] = { .type = NLA_U32 }, 133 [BATADV_ATTR_ROUTER] = { .len = ETH_ALEN }, 134 [BATADV_ATTR_BLA_OWN] = { .type = NLA_FLAG }, 135 [BATADV_ATTR_BLA_ADDRESS] = { .len = ETH_ALEN }, 136 [BATADV_ATTR_BLA_VID] = { .type = NLA_U16 }, 137 [BATADV_ATTR_BLA_BACKBONE] = { .len = ETH_ALEN }, 138 [BATADV_ATTR_BLA_CRC] = { .type = NLA_U16 }, 139 [BATADV_ATTR_DAT_CACHE_IP4ADDRESS] = { .type = NLA_U32 }, 140 [BATADV_ATTR_DAT_CACHE_HWADDRESS] = { .len = ETH_ALEN }, 141 [BATADV_ATTR_DAT_CACHE_VID] = { .type = NLA_U16 }, 142 [BATADV_ATTR_MCAST_FLAGS] = { .type = NLA_U32 }, 143 [BATADV_ATTR_MCAST_FLAGS_PRIV] = { .type = NLA_U32 }, 144 [BATADV_ATTR_VLANID] = { .type = NLA_U16 }, 145 [BATADV_ATTR_AGGREGATED_OGMS_ENABLED] = { .type = NLA_U8 }, 146 [BATADV_ATTR_AP_ISOLATION_ENABLED] = { .type = NLA_U8 }, 147 [BATADV_ATTR_ISOLATION_MARK] = { .type = NLA_U32 }, 148 [BATADV_ATTR_ISOLATION_MASK] = { .type = NLA_U32 }, 149 [BATADV_ATTR_BONDING_ENABLED] = { .type = NLA_U8 }, 150 [BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED] = { .type = NLA_U8 }, 151 [BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED] = { .type = NLA_U8 }, 152 [BATADV_ATTR_FRAGMENTATION_ENABLED] = { .type = NLA_U8 }, 153 [BATADV_ATTR_GW_BANDWIDTH_DOWN] = { .type = NLA_U32 }, 154 [BATADV_ATTR_GW_BANDWIDTH_UP] = { .type = NLA_U32 }, 155 [BATADV_ATTR_GW_MODE] = { .type = NLA_U8 }, 156 [BATADV_ATTR_GW_SEL_CLASS] = { .type = NLA_U32 }, 157 [BATADV_ATTR_HOP_PENALTY] = { .type = NLA_U8 }, 158 [BATADV_ATTR_LOG_LEVEL] = { .type = NLA_U32 }, 159 [BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED] = { .type = NLA_U8 }, 160 [BATADV_ATTR_NETWORK_CODING_ENABLED] = { .type = NLA_U8 }, 161 [BATADV_ATTR_ORIG_INTERVAL] = { .type = NLA_U32 }, 162 [BATADV_ATTR_ELP_INTERVAL] = { .type = NLA_U32 }, 163 [BATADV_ATTR_THROUGHPUT_OVERRIDE] = { .type = NLA_U32 }, 164 }; 165 166 /** 167 * batadv_netlink_get_ifindex() - Extract an interface index from a message 168 * @nlh: Message header 169 * @attrtype: Attribute which holds an interface index 170 * 171 * Return: interface index, or 0. 172 */ 173 int 174 batadv_netlink_get_ifindex(const struct nlmsghdr *nlh, int attrtype) 175 { 176 struct nlattr *attr = nlmsg_find_attr(nlh, GENL_HDRLEN, attrtype); 177 178 return attr ? nla_get_u32(attr) : 0; 179 } 180 181 /** 182 * batadv_netlink_mesh_fill_ap_isolation() - Add ap_isolation softif attribute 183 * @msg: Netlink message to dump into 184 * @bat_priv: the bat priv with all the soft interface information 185 * 186 * Return: 0 on success or negative error number in case of failure 187 */ 188 static int batadv_netlink_mesh_fill_ap_isolation(struct sk_buff *msg, 189 struct batadv_priv *bat_priv) 190 { 191 struct batadv_softif_vlan *vlan; 192 u8 ap_isolation; 193 194 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS); 195 if (!vlan) 196 return 0; 197 198 ap_isolation = atomic_read(&vlan->ap_isolation); 199 batadv_softif_vlan_put(vlan); 200 201 return nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED, 202 !!ap_isolation); 203 } 204 205 /** 206 * batadv_option_set_ap_isolation() - Set ap_isolation from genl msg 207 * @attr: parsed BATADV_ATTR_AP_ISOLATION_ENABLED attribute 208 * @bat_priv: the bat priv with all the soft interface information 209 * 210 * Return: 0 on success or negative error number in case of failure 211 */ 212 static int batadv_netlink_set_mesh_ap_isolation(struct nlattr *attr, 213 struct batadv_priv *bat_priv) 214 { 215 struct batadv_softif_vlan *vlan; 216 217 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS); 218 if (!vlan) 219 return -ENOENT; 220 221 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr)); 222 batadv_softif_vlan_put(vlan); 223 224 return 0; 225 } 226 227 /** 228 * batadv_netlink_mesh_fill() - Fill message with mesh attributes 229 * @msg: Netlink message to dump into 230 * @bat_priv: the bat priv with all the soft interface information 231 * @cmd: type of message to generate 232 * @portid: Port making netlink request 233 * @seq: sequence number for message 234 * @flags: Additional flags for message 235 * 236 * Return: 0 on success or negative error number in case of failure 237 */ 238 static int batadv_netlink_mesh_fill(struct sk_buff *msg, 239 struct batadv_priv *bat_priv, 240 enum batadv_nl_commands cmd, 241 u32 portid, u32 seq, int flags) 242 { 243 struct net_device *soft_iface = bat_priv->soft_iface; 244 struct batadv_hard_iface *primary_if = NULL; 245 struct net_device *hard_iface; 246 void *hdr; 247 248 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd); 249 if (!hdr) 250 return -ENOBUFS; 251 252 if (nla_put_string(msg, BATADV_ATTR_VERSION, BATADV_SOURCE_VERSION) || 253 nla_put_string(msg, BATADV_ATTR_ALGO_NAME, 254 bat_priv->algo_ops->name) || 255 nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, soft_iface->ifindex) || 256 nla_put_string(msg, BATADV_ATTR_MESH_IFNAME, soft_iface->name) || 257 nla_put(msg, BATADV_ATTR_MESH_ADDRESS, ETH_ALEN, 258 soft_iface->dev_addr) || 259 nla_put_u8(msg, BATADV_ATTR_TT_TTVN, 260 (u8)atomic_read(&bat_priv->tt.vn))) 261 goto nla_put_failure; 262 263 #ifdef CONFIG_BATMAN_ADV_BLA 264 if (nla_put_u16(msg, BATADV_ATTR_BLA_CRC, 265 ntohs(bat_priv->bla.claim_dest.group))) 266 goto nla_put_failure; 267 #endif 268 269 if (batadv_mcast_mesh_info_put(msg, bat_priv)) 270 goto nla_put_failure; 271 272 primary_if = batadv_primary_if_get_selected(bat_priv); 273 if (primary_if && primary_if->if_status == BATADV_IF_ACTIVE) { 274 hard_iface = primary_if->net_dev; 275 276 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX, 277 hard_iface->ifindex) || 278 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME, 279 hard_iface->name) || 280 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN, 281 hard_iface->dev_addr)) 282 goto nla_put_failure; 283 } 284 285 if (nla_put_u8(msg, BATADV_ATTR_AGGREGATED_OGMS_ENABLED, 286 !!atomic_read(&bat_priv->aggregated_ogms))) 287 goto nla_put_failure; 288 289 if (batadv_netlink_mesh_fill_ap_isolation(msg, bat_priv)) 290 goto nla_put_failure; 291 292 if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MARK, 293 bat_priv->isolation_mark)) 294 goto nla_put_failure; 295 296 if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MASK, 297 bat_priv->isolation_mark_mask)) 298 goto nla_put_failure; 299 300 if (nla_put_u8(msg, BATADV_ATTR_BONDING_ENABLED, 301 !!atomic_read(&bat_priv->bonding))) 302 goto nla_put_failure; 303 304 #ifdef CONFIG_BATMAN_ADV_BLA 305 if (nla_put_u8(msg, BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED, 306 !!atomic_read(&bat_priv->bridge_loop_avoidance))) 307 goto nla_put_failure; 308 #endif /* CONFIG_BATMAN_ADV_BLA */ 309 310 #ifdef CONFIG_BATMAN_ADV_DAT 311 if (nla_put_u8(msg, BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED, 312 !!atomic_read(&bat_priv->distributed_arp_table))) 313 goto nla_put_failure; 314 #endif /* CONFIG_BATMAN_ADV_DAT */ 315 316 if (nla_put_u8(msg, BATADV_ATTR_FRAGMENTATION_ENABLED, 317 !!atomic_read(&bat_priv->fragmentation))) 318 goto nla_put_failure; 319 320 if (nla_put_u32(msg, BATADV_ATTR_GW_BANDWIDTH_DOWN, 321 atomic_read(&bat_priv->gw.bandwidth_down))) 322 goto nla_put_failure; 323 324 if (nla_put_u32(msg, BATADV_ATTR_GW_BANDWIDTH_UP, 325 atomic_read(&bat_priv->gw.bandwidth_up))) 326 goto nla_put_failure; 327 328 if (nla_put_u8(msg, BATADV_ATTR_GW_MODE, 329 atomic_read(&bat_priv->gw.mode))) 330 goto nla_put_failure; 331 332 if (bat_priv->algo_ops->gw.get_best_gw_node && 333 bat_priv->algo_ops->gw.is_eligible) { 334 /* GW selection class is not available if the routing algorithm 335 * in use does not implement the GW API 336 */ 337 if (nla_put_u32(msg, BATADV_ATTR_GW_SEL_CLASS, 338 atomic_read(&bat_priv->gw.sel_class))) 339 goto nla_put_failure; 340 } 341 342 if (nla_put_u8(msg, BATADV_ATTR_HOP_PENALTY, 343 atomic_read(&bat_priv->hop_penalty))) 344 goto nla_put_failure; 345 346 #ifdef CONFIG_BATMAN_ADV_DEBUG 347 if (nla_put_u32(msg, BATADV_ATTR_LOG_LEVEL, 348 atomic_read(&bat_priv->log_level))) 349 goto nla_put_failure; 350 #endif /* CONFIG_BATMAN_ADV_DEBUG */ 351 352 #ifdef CONFIG_BATMAN_ADV_MCAST 353 if (nla_put_u8(msg, BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED, 354 !atomic_read(&bat_priv->multicast_mode))) 355 goto nla_put_failure; 356 #endif /* CONFIG_BATMAN_ADV_MCAST */ 357 358 #ifdef CONFIG_BATMAN_ADV_NC 359 if (nla_put_u8(msg, BATADV_ATTR_NETWORK_CODING_ENABLED, 360 !!atomic_read(&bat_priv->network_coding))) 361 goto nla_put_failure; 362 #endif /* CONFIG_BATMAN_ADV_NC */ 363 364 if (nla_put_u32(msg, BATADV_ATTR_ORIG_INTERVAL, 365 atomic_read(&bat_priv->orig_interval))) 366 goto nla_put_failure; 367 368 if (primary_if) 369 batadv_hardif_put(primary_if); 370 371 genlmsg_end(msg, hdr); 372 return 0; 373 374 nla_put_failure: 375 if (primary_if) 376 batadv_hardif_put(primary_if); 377 378 genlmsg_cancel(msg, hdr); 379 return -EMSGSIZE; 380 } 381 382 /** 383 * batadv_netlink_notify_mesh() - send softif attributes to listener 384 * @bat_priv: the bat priv with all the soft interface information 385 * 386 * Return: 0 on success, < 0 on error 387 */ 388 int batadv_netlink_notify_mesh(struct batadv_priv *bat_priv) 389 { 390 struct sk_buff *msg; 391 int ret; 392 393 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 394 if (!msg) 395 return -ENOMEM; 396 397 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_SET_MESH, 398 0, 0, 0); 399 if (ret < 0) { 400 nlmsg_free(msg); 401 return ret; 402 } 403 404 genlmsg_multicast_netns(&batadv_netlink_family, 405 dev_net(bat_priv->soft_iface), msg, 0, 406 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL); 407 408 return 0; 409 } 410 411 /** 412 * batadv_netlink_get_mesh() - Get softif attributes 413 * @skb: Netlink message with request data 414 * @info: receiver information 415 * 416 * Return: 0 on success or negative error number in case of failure 417 */ 418 static int batadv_netlink_get_mesh(struct sk_buff *skb, struct genl_info *info) 419 { 420 struct batadv_priv *bat_priv = info->user_ptr[0]; 421 struct sk_buff *msg; 422 int ret; 423 424 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 425 if (!msg) 426 return -ENOMEM; 427 428 ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_GET_MESH, 429 info->snd_portid, info->snd_seq, 0); 430 if (ret < 0) { 431 nlmsg_free(msg); 432 return ret; 433 } 434 435 ret = genlmsg_reply(msg, info); 436 437 return ret; 438 } 439 440 /** 441 * batadv_netlink_set_mesh() - Set softif attributes 442 * @skb: Netlink message with request data 443 * @info: receiver information 444 * 445 * Return: 0 on success or negative error number in case of failure 446 */ 447 static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info) 448 { 449 struct batadv_priv *bat_priv = info->user_ptr[0]; 450 struct nlattr *attr; 451 452 if (info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED]) { 453 attr = info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED]; 454 455 atomic_set(&bat_priv->aggregated_ogms, !!nla_get_u8(attr)); 456 } 457 458 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) { 459 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]; 460 461 batadv_netlink_set_mesh_ap_isolation(attr, bat_priv); 462 } 463 464 if (info->attrs[BATADV_ATTR_ISOLATION_MARK]) { 465 attr = info->attrs[BATADV_ATTR_ISOLATION_MARK]; 466 467 bat_priv->isolation_mark = nla_get_u32(attr); 468 } 469 470 if (info->attrs[BATADV_ATTR_ISOLATION_MASK]) { 471 attr = info->attrs[BATADV_ATTR_ISOLATION_MASK]; 472 473 bat_priv->isolation_mark_mask = nla_get_u32(attr); 474 } 475 476 if (info->attrs[BATADV_ATTR_BONDING_ENABLED]) { 477 attr = info->attrs[BATADV_ATTR_BONDING_ENABLED]; 478 479 atomic_set(&bat_priv->bonding, !!nla_get_u8(attr)); 480 } 481 482 #ifdef CONFIG_BATMAN_ADV_BLA 483 if (info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED]) { 484 attr = info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED]; 485 486 atomic_set(&bat_priv->bridge_loop_avoidance, 487 !!nla_get_u8(attr)); 488 batadv_bla_status_update(bat_priv->soft_iface); 489 } 490 #endif /* CONFIG_BATMAN_ADV_BLA */ 491 492 #ifdef CONFIG_BATMAN_ADV_DAT 493 if (info->attrs[BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED]) { 494 attr = info->attrs[BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED]; 495 496 atomic_set(&bat_priv->distributed_arp_table, 497 !!nla_get_u8(attr)); 498 batadv_dat_status_update(bat_priv->soft_iface); 499 } 500 #endif /* CONFIG_BATMAN_ADV_DAT */ 501 502 if (info->attrs[BATADV_ATTR_FRAGMENTATION_ENABLED]) { 503 attr = info->attrs[BATADV_ATTR_FRAGMENTATION_ENABLED]; 504 505 atomic_set(&bat_priv->fragmentation, !!nla_get_u8(attr)); 506 batadv_update_min_mtu(bat_priv->soft_iface); 507 } 508 509 if (info->attrs[BATADV_ATTR_GW_BANDWIDTH_DOWN]) { 510 attr = info->attrs[BATADV_ATTR_GW_BANDWIDTH_DOWN]; 511 512 atomic_set(&bat_priv->gw.bandwidth_down, nla_get_u32(attr)); 513 batadv_gw_tvlv_container_update(bat_priv); 514 } 515 516 if (info->attrs[BATADV_ATTR_GW_BANDWIDTH_UP]) { 517 attr = info->attrs[BATADV_ATTR_GW_BANDWIDTH_UP]; 518 519 atomic_set(&bat_priv->gw.bandwidth_up, nla_get_u32(attr)); 520 batadv_gw_tvlv_container_update(bat_priv); 521 } 522 523 if (info->attrs[BATADV_ATTR_GW_MODE]) { 524 u8 gw_mode; 525 526 attr = info->attrs[BATADV_ATTR_GW_MODE]; 527 gw_mode = nla_get_u8(attr); 528 529 if (gw_mode <= BATADV_GW_MODE_SERVER) { 530 /* Invoking batadv_gw_reselect() is not enough to really 531 * de-select the current GW. It will only instruct the 532 * gateway client code to perform a re-election the next 533 * time that this is needed. 534 * 535 * When gw client mode is being switched off the current 536 * GW must be de-selected explicitly otherwise no GW_ADD 537 * uevent is thrown on client mode re-activation. This 538 * is operation is performed in 539 * batadv_gw_check_client_stop(). 540 */ 541 batadv_gw_reselect(bat_priv); 542 543 /* always call batadv_gw_check_client_stop() before 544 * changing the gateway state 545 */ 546 batadv_gw_check_client_stop(bat_priv); 547 atomic_set(&bat_priv->gw.mode, gw_mode); 548 batadv_gw_tvlv_container_update(bat_priv); 549 } 550 } 551 552 if (info->attrs[BATADV_ATTR_GW_SEL_CLASS] && 553 bat_priv->algo_ops->gw.get_best_gw_node && 554 bat_priv->algo_ops->gw.is_eligible) { 555 /* setting the GW selection class is allowed only if the routing 556 * algorithm in use implements the GW API 557 */ 558 559 u32 sel_class_max = 0xffffffffu; 560 u32 sel_class; 561 562 attr = info->attrs[BATADV_ATTR_GW_SEL_CLASS]; 563 sel_class = nla_get_u32(attr); 564 565 if (!bat_priv->algo_ops->gw.store_sel_class) 566 sel_class_max = BATADV_TQ_MAX_VALUE; 567 568 if (sel_class >= 1 && sel_class <= sel_class_max) { 569 atomic_set(&bat_priv->gw.sel_class, sel_class); 570 batadv_gw_reselect(bat_priv); 571 } 572 } 573 574 if (info->attrs[BATADV_ATTR_HOP_PENALTY]) { 575 attr = info->attrs[BATADV_ATTR_HOP_PENALTY]; 576 577 atomic_set(&bat_priv->hop_penalty, nla_get_u8(attr)); 578 } 579 580 #ifdef CONFIG_BATMAN_ADV_DEBUG 581 if (info->attrs[BATADV_ATTR_LOG_LEVEL]) { 582 attr = info->attrs[BATADV_ATTR_LOG_LEVEL]; 583 584 atomic_set(&bat_priv->log_level, 585 nla_get_u32(attr) & BATADV_DBG_ALL); 586 } 587 #endif /* CONFIG_BATMAN_ADV_DEBUG */ 588 589 #ifdef CONFIG_BATMAN_ADV_MCAST 590 if (info->attrs[BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED]) { 591 attr = info->attrs[BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED]; 592 593 atomic_set(&bat_priv->multicast_mode, !nla_get_u8(attr)); 594 } 595 #endif /* CONFIG_BATMAN_ADV_MCAST */ 596 597 #ifdef CONFIG_BATMAN_ADV_NC 598 if (info->attrs[BATADV_ATTR_NETWORK_CODING_ENABLED]) { 599 attr = info->attrs[BATADV_ATTR_NETWORK_CODING_ENABLED]; 600 601 atomic_set(&bat_priv->network_coding, !!nla_get_u8(attr)); 602 batadv_nc_status_update(bat_priv->soft_iface); 603 } 604 #endif /* CONFIG_BATMAN_ADV_NC */ 605 606 if (info->attrs[BATADV_ATTR_ORIG_INTERVAL]) { 607 u32 orig_interval; 608 609 attr = info->attrs[BATADV_ATTR_ORIG_INTERVAL]; 610 orig_interval = nla_get_u32(attr); 611 612 orig_interval = min_t(u32, orig_interval, INT_MAX); 613 orig_interval = max_t(u32, orig_interval, 2 * BATADV_JITTER); 614 615 atomic_set(&bat_priv->orig_interval, orig_interval); 616 } 617 618 batadv_netlink_notify_mesh(bat_priv); 619 620 return 0; 621 } 622 623 /** 624 * batadv_netlink_tp_meter_put() - Fill information of started tp_meter session 625 * @msg: netlink message to be sent back 626 * @cookie: tp meter session cookie 627 * 628 * Return: 0 on success, < 0 on error 629 */ 630 static int 631 batadv_netlink_tp_meter_put(struct sk_buff *msg, u32 cookie) 632 { 633 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie)) 634 return -ENOBUFS; 635 636 return 0; 637 } 638 639 /** 640 * batadv_netlink_tpmeter_notify() - send tp_meter result via netlink to client 641 * @bat_priv: the bat priv with all the soft interface information 642 * @dst: destination of tp_meter session 643 * @result: reason for tp meter session stop 644 * @test_time: total time ot the tp_meter session 645 * @total_bytes: bytes acked to the receiver 646 * @cookie: cookie of tp_meter session 647 * 648 * Return: 0 on success, < 0 on error 649 */ 650 int batadv_netlink_tpmeter_notify(struct batadv_priv *bat_priv, const u8 *dst, 651 u8 result, u32 test_time, u64 total_bytes, 652 u32 cookie) 653 { 654 struct sk_buff *msg; 655 void *hdr; 656 int ret; 657 658 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 659 if (!msg) 660 return -ENOMEM; 661 662 hdr = genlmsg_put(msg, 0, 0, &batadv_netlink_family, 0, 663 BATADV_CMD_TP_METER); 664 if (!hdr) { 665 ret = -ENOBUFS; 666 goto err_genlmsg; 667 } 668 669 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie)) 670 goto nla_put_failure; 671 672 if (nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, test_time)) 673 goto nla_put_failure; 674 675 if (nla_put_u64_64bit(msg, BATADV_ATTR_TPMETER_BYTES, total_bytes, 676 BATADV_ATTR_PAD)) 677 goto nla_put_failure; 678 679 if (nla_put_u8(msg, BATADV_ATTR_TPMETER_RESULT, result)) 680 goto nla_put_failure; 681 682 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst)) 683 goto nla_put_failure; 684 685 genlmsg_end(msg, hdr); 686 687 genlmsg_multicast_netns(&batadv_netlink_family, 688 dev_net(bat_priv->soft_iface), msg, 0, 689 BATADV_NL_MCGRP_TPMETER, GFP_KERNEL); 690 691 return 0; 692 693 nla_put_failure: 694 genlmsg_cancel(msg, hdr); 695 ret = -EMSGSIZE; 696 697 err_genlmsg: 698 nlmsg_free(msg); 699 return ret; 700 } 701 702 /** 703 * batadv_netlink_tp_meter_start() - Start a new tp_meter session 704 * @skb: received netlink message 705 * @info: receiver information 706 * 707 * Return: 0 on success, < 0 on error 708 */ 709 static int 710 batadv_netlink_tp_meter_start(struct sk_buff *skb, struct genl_info *info) 711 { 712 struct batadv_priv *bat_priv = info->user_ptr[0]; 713 struct sk_buff *msg = NULL; 714 u32 test_length; 715 void *msg_head; 716 u32 cookie; 717 u8 *dst; 718 int ret; 719 720 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS]) 721 return -EINVAL; 722 723 if (!info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]) 724 return -EINVAL; 725 726 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]); 727 728 test_length = nla_get_u32(info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]); 729 730 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 731 if (!msg) { 732 ret = -ENOMEM; 733 goto out; 734 } 735 736 msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq, 737 &batadv_netlink_family, 0, 738 BATADV_CMD_TP_METER); 739 if (!msg_head) { 740 ret = -ENOBUFS; 741 goto out; 742 } 743 744 batadv_tp_start(bat_priv, dst, test_length, &cookie); 745 746 ret = batadv_netlink_tp_meter_put(msg, cookie); 747 748 out: 749 if (ret) { 750 if (msg) 751 nlmsg_free(msg); 752 return ret; 753 } 754 755 genlmsg_end(msg, msg_head); 756 return genlmsg_reply(msg, info); 757 } 758 759 /** 760 * batadv_netlink_tp_meter_start() - Cancel a running tp_meter session 761 * @skb: received netlink message 762 * @info: receiver information 763 * 764 * Return: 0 on success, < 0 on error 765 */ 766 static int 767 batadv_netlink_tp_meter_cancel(struct sk_buff *skb, struct genl_info *info) 768 { 769 struct batadv_priv *bat_priv = info->user_ptr[0]; 770 u8 *dst; 771 int ret = 0; 772 773 if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS]) 774 return -EINVAL; 775 776 dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]); 777 778 batadv_tp_stop(bat_priv, dst, BATADV_TP_REASON_CANCEL); 779 780 return ret; 781 } 782 783 /** 784 * batadv_netlink_hardif_fill() - Fill message with hardif attributes 785 * @msg: Netlink message to dump into 786 * @bat_priv: the bat priv with all the soft interface information 787 * @hard_iface: hard interface which was modified 788 * @cmd: type of message to generate 789 * @portid: Port making netlink request 790 * @seq: sequence number for message 791 * @flags: Additional flags for message 792 * @cb: Control block containing additional options 793 * 794 * Return: 0 on success or negative error number in case of failure 795 */ 796 static int batadv_netlink_hardif_fill(struct sk_buff *msg, 797 struct batadv_priv *bat_priv, 798 struct batadv_hard_iface *hard_iface, 799 enum batadv_nl_commands cmd, 800 u32 portid, u32 seq, int flags, 801 struct netlink_callback *cb) 802 { 803 struct net_device *net_dev = hard_iface->net_dev; 804 void *hdr; 805 806 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd); 807 if (!hdr) 808 return -ENOBUFS; 809 810 if (cb) 811 genl_dump_check_consistent(cb, hdr); 812 813 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, 814 bat_priv->soft_iface->ifindex)) 815 goto nla_put_failure; 816 817 if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX, 818 net_dev->ifindex) || 819 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME, 820 net_dev->name) || 821 nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN, 822 net_dev->dev_addr)) 823 goto nla_put_failure; 824 825 if (hard_iface->if_status == BATADV_IF_ACTIVE) { 826 if (nla_put_flag(msg, BATADV_ATTR_ACTIVE)) 827 goto nla_put_failure; 828 } 829 830 #ifdef CONFIG_BATMAN_ADV_BATMAN_V 831 if (nla_put_u32(msg, BATADV_ATTR_ELP_INTERVAL, 832 atomic_read(&hard_iface->bat_v.elp_interval))) 833 goto nla_put_failure; 834 835 if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT_OVERRIDE, 836 atomic_read(&hard_iface->bat_v.throughput_override))) 837 goto nla_put_failure; 838 #endif /* CONFIG_BATMAN_ADV_BATMAN_V */ 839 840 genlmsg_end(msg, hdr); 841 return 0; 842 843 nla_put_failure: 844 genlmsg_cancel(msg, hdr); 845 return -EMSGSIZE; 846 } 847 848 /** 849 * batadv_netlink_notify_hardif() - send hardif attributes to listener 850 * @bat_priv: the bat priv with all the soft interface information 851 * @hard_iface: hard interface which was modified 852 * 853 * Return: 0 on success, < 0 on error 854 */ 855 int batadv_netlink_notify_hardif(struct batadv_priv *bat_priv, 856 struct batadv_hard_iface *hard_iface) 857 { 858 struct sk_buff *msg; 859 int ret; 860 861 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 862 if (!msg) 863 return -ENOMEM; 864 865 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface, 866 BATADV_CMD_SET_HARDIF, 0, 0, 0, NULL); 867 if (ret < 0) { 868 nlmsg_free(msg); 869 return ret; 870 } 871 872 genlmsg_multicast_netns(&batadv_netlink_family, 873 dev_net(bat_priv->soft_iface), msg, 0, 874 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL); 875 876 return 0; 877 } 878 879 /** 880 * batadv_netlink_get_hardif() - Get hardif attributes 881 * @skb: Netlink message with request data 882 * @info: receiver information 883 * 884 * Return: 0 on success or negative error number in case of failure 885 */ 886 static int batadv_netlink_get_hardif(struct sk_buff *skb, 887 struct genl_info *info) 888 { 889 struct batadv_hard_iface *hard_iface = info->user_ptr[1]; 890 struct batadv_priv *bat_priv = info->user_ptr[0]; 891 struct sk_buff *msg; 892 int ret; 893 894 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 895 if (!msg) 896 return -ENOMEM; 897 898 ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface, 899 BATADV_CMD_GET_HARDIF, 900 info->snd_portid, info->snd_seq, 0, 901 NULL); 902 if (ret < 0) { 903 nlmsg_free(msg); 904 return ret; 905 } 906 907 ret = genlmsg_reply(msg, info); 908 909 return ret; 910 } 911 912 /** 913 * batadv_netlink_set_hardif() - Set hardif attributes 914 * @skb: Netlink message with request data 915 * @info: receiver information 916 * 917 * Return: 0 on success or negative error number in case of failure 918 */ 919 static int batadv_netlink_set_hardif(struct sk_buff *skb, 920 struct genl_info *info) 921 { 922 struct batadv_hard_iface *hard_iface = info->user_ptr[1]; 923 struct batadv_priv *bat_priv = info->user_ptr[0]; 924 925 #ifdef CONFIG_BATMAN_ADV_BATMAN_V 926 struct nlattr *attr; 927 928 if (info->attrs[BATADV_ATTR_ELP_INTERVAL]) { 929 attr = info->attrs[BATADV_ATTR_ELP_INTERVAL]; 930 931 atomic_set(&hard_iface->bat_v.elp_interval, nla_get_u32(attr)); 932 } 933 934 if (info->attrs[BATADV_ATTR_THROUGHPUT_OVERRIDE]) { 935 attr = info->attrs[BATADV_ATTR_THROUGHPUT_OVERRIDE]; 936 937 atomic_set(&hard_iface->bat_v.throughput_override, 938 nla_get_u32(attr)); 939 } 940 #endif /* CONFIG_BATMAN_ADV_BATMAN_V */ 941 942 batadv_netlink_notify_hardif(bat_priv, hard_iface); 943 944 return 0; 945 } 946 947 /** 948 * batadv_netlink_dump_hardif() - Dump all hard interface into a messages 949 * @msg: Netlink message to dump into 950 * @cb: Parameters from query 951 * 952 * Return: error code, or length of reply message on success 953 */ 954 static int 955 batadv_netlink_dump_hardif(struct sk_buff *msg, struct netlink_callback *cb) 956 { 957 struct net *net = sock_net(cb->skb->sk); 958 struct net_device *soft_iface; 959 struct batadv_hard_iface *hard_iface; 960 struct batadv_priv *bat_priv; 961 int ifindex; 962 int portid = NETLINK_CB(cb->skb).portid; 963 int skip = cb->args[0]; 964 int i = 0; 965 966 ifindex = batadv_netlink_get_ifindex(cb->nlh, 967 BATADV_ATTR_MESH_IFINDEX); 968 if (!ifindex) 969 return -EINVAL; 970 971 soft_iface = dev_get_by_index(net, ifindex); 972 if (!soft_iface) 973 return -ENODEV; 974 975 if (!batadv_softif_is_valid(soft_iface)) { 976 dev_put(soft_iface); 977 return -ENODEV; 978 } 979 980 bat_priv = netdev_priv(soft_iface); 981 982 rtnl_lock(); 983 cb->seq = batadv_hardif_generation << 1 | 1; 984 985 list_for_each_entry(hard_iface, &batadv_hardif_list, list) { 986 if (hard_iface->soft_iface != soft_iface) 987 continue; 988 989 if (i++ < skip) 990 continue; 991 992 if (batadv_netlink_hardif_fill(msg, bat_priv, hard_iface, 993 BATADV_CMD_GET_HARDIF, 994 portid, cb->nlh->nlmsg_seq, 995 NLM_F_MULTI, cb)) { 996 i--; 997 break; 998 } 999 } 1000 1001 rtnl_unlock(); 1002 1003 dev_put(soft_iface); 1004 1005 cb->args[0] = i; 1006 1007 return msg->len; 1008 } 1009 1010 /** 1011 * batadv_netlink_vlan_fill() - Fill message with vlan attributes 1012 * @msg: Netlink message to dump into 1013 * @bat_priv: the bat priv with all the soft interface information 1014 * @vlan: vlan which was modified 1015 * @cmd: type of message to generate 1016 * @portid: Port making netlink request 1017 * @seq: sequence number for message 1018 * @flags: Additional flags for message 1019 * 1020 * Return: 0 on success or negative error number in case of failure 1021 */ 1022 static int batadv_netlink_vlan_fill(struct sk_buff *msg, 1023 struct batadv_priv *bat_priv, 1024 struct batadv_softif_vlan *vlan, 1025 enum batadv_nl_commands cmd, 1026 u32 portid, u32 seq, int flags) 1027 { 1028 void *hdr; 1029 1030 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd); 1031 if (!hdr) 1032 return -ENOBUFS; 1033 1034 if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, 1035 bat_priv->soft_iface->ifindex)) 1036 goto nla_put_failure; 1037 1038 if (nla_put_u32(msg, BATADV_ATTR_VLANID, vlan->vid & VLAN_VID_MASK)) 1039 goto nla_put_failure; 1040 1041 if (nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED, 1042 !!atomic_read(&vlan->ap_isolation))) 1043 goto nla_put_failure; 1044 1045 genlmsg_end(msg, hdr); 1046 return 0; 1047 1048 nla_put_failure: 1049 genlmsg_cancel(msg, hdr); 1050 return -EMSGSIZE; 1051 } 1052 1053 /** 1054 * batadv_netlink_notify_vlan() - send vlan attributes to listener 1055 * @bat_priv: the bat priv with all the soft interface information 1056 * @vlan: vlan which was modified 1057 * 1058 * Return: 0 on success, < 0 on error 1059 */ 1060 int batadv_netlink_notify_vlan(struct batadv_priv *bat_priv, 1061 struct batadv_softif_vlan *vlan) 1062 { 1063 struct sk_buff *msg; 1064 int ret; 1065 1066 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1067 if (!msg) 1068 return -ENOMEM; 1069 1070 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan, 1071 BATADV_CMD_SET_VLAN, 0, 0, 0); 1072 if (ret < 0) { 1073 nlmsg_free(msg); 1074 return ret; 1075 } 1076 1077 genlmsg_multicast_netns(&batadv_netlink_family, 1078 dev_net(bat_priv->soft_iface), msg, 0, 1079 BATADV_NL_MCGRP_CONFIG, GFP_KERNEL); 1080 1081 return 0; 1082 } 1083 1084 /** 1085 * batadv_netlink_get_vlan() - Get vlan attributes 1086 * @skb: Netlink message with request data 1087 * @info: receiver information 1088 * 1089 * Return: 0 on success or negative error number in case of failure 1090 */ 1091 static int batadv_netlink_get_vlan(struct sk_buff *skb, struct genl_info *info) 1092 { 1093 struct batadv_softif_vlan *vlan = info->user_ptr[1]; 1094 struct batadv_priv *bat_priv = info->user_ptr[0]; 1095 struct sk_buff *msg; 1096 int ret; 1097 1098 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1099 if (!msg) 1100 return -ENOMEM; 1101 1102 ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan, BATADV_CMD_GET_VLAN, 1103 info->snd_portid, info->snd_seq, 0); 1104 if (ret < 0) { 1105 nlmsg_free(msg); 1106 return ret; 1107 } 1108 1109 ret = genlmsg_reply(msg, info); 1110 1111 return ret; 1112 } 1113 1114 /** 1115 * batadv_netlink_set_vlan() - Get vlan attributes 1116 * @skb: Netlink message with request data 1117 * @info: receiver information 1118 * 1119 * Return: 0 on success or negative error number in case of failure 1120 */ 1121 static int batadv_netlink_set_vlan(struct sk_buff *skb, struct genl_info *info) 1122 { 1123 struct batadv_softif_vlan *vlan = info->user_ptr[1]; 1124 struct batadv_priv *bat_priv = info->user_ptr[0]; 1125 struct nlattr *attr; 1126 1127 if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) { 1128 attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]; 1129 1130 atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr)); 1131 } 1132 1133 batadv_netlink_notify_vlan(bat_priv, vlan); 1134 1135 return 0; 1136 } 1137 1138 /** 1139 * batadv_get_softif_from_info() - Retrieve soft interface from genl attributes 1140 * @net: the applicable net namespace 1141 * @info: receiver information 1142 * 1143 * Return: Pointer to soft interface (with increased refcnt) on success, error 1144 * pointer on error 1145 */ 1146 static struct net_device * 1147 batadv_get_softif_from_info(struct net *net, struct genl_info *info) 1148 { 1149 struct net_device *soft_iface; 1150 int ifindex; 1151 1152 if (!info->attrs[BATADV_ATTR_MESH_IFINDEX]) 1153 return ERR_PTR(-EINVAL); 1154 1155 ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]); 1156 1157 soft_iface = dev_get_by_index(net, ifindex); 1158 if (!soft_iface) 1159 return ERR_PTR(-ENODEV); 1160 1161 if (!batadv_softif_is_valid(soft_iface)) 1162 goto err_put_softif; 1163 1164 return soft_iface; 1165 1166 err_put_softif: 1167 dev_put(soft_iface); 1168 1169 return ERR_PTR(-EINVAL); 1170 } 1171 1172 /** 1173 * batadv_get_hardif_from_info() - Retrieve hardif from genl attributes 1174 * @bat_priv: the bat priv with all the soft interface information 1175 * @net: the applicable net namespace 1176 * @info: receiver information 1177 * 1178 * Return: Pointer to hard interface (with increased refcnt) on success, error 1179 * pointer on error 1180 */ 1181 static struct batadv_hard_iface * 1182 batadv_get_hardif_from_info(struct batadv_priv *bat_priv, struct net *net, 1183 struct genl_info *info) 1184 { 1185 struct batadv_hard_iface *hard_iface; 1186 struct net_device *hard_dev; 1187 unsigned int hardif_index; 1188 1189 if (!info->attrs[BATADV_ATTR_HARD_IFINDEX]) 1190 return ERR_PTR(-EINVAL); 1191 1192 hardif_index = nla_get_u32(info->attrs[BATADV_ATTR_HARD_IFINDEX]); 1193 1194 hard_dev = dev_get_by_index(net, hardif_index); 1195 if (!hard_dev) 1196 return ERR_PTR(-ENODEV); 1197 1198 hard_iface = batadv_hardif_get_by_netdev(hard_dev); 1199 if (!hard_iface) 1200 goto err_put_harddev; 1201 1202 if (hard_iface->soft_iface != bat_priv->soft_iface) 1203 goto err_put_hardif; 1204 1205 /* hard_dev is referenced by hard_iface and not needed here */ 1206 dev_put(hard_dev); 1207 1208 return hard_iface; 1209 1210 err_put_hardif: 1211 batadv_hardif_put(hard_iface); 1212 err_put_harddev: 1213 dev_put(hard_dev); 1214 1215 return ERR_PTR(-EINVAL); 1216 } 1217 1218 /** 1219 * batadv_get_vlan_from_info() - Retrieve vlan from genl attributes 1220 * @bat_priv: the bat priv with all the soft interface information 1221 * @net: the applicable net namespace 1222 * @info: receiver information 1223 * 1224 * Return: Pointer to vlan on success (with increased refcnt), error pointer 1225 * on error 1226 */ 1227 static struct batadv_softif_vlan * 1228 batadv_get_vlan_from_info(struct batadv_priv *bat_priv, struct net *net, 1229 struct genl_info *info) 1230 { 1231 struct batadv_softif_vlan *vlan; 1232 u16 vid; 1233 1234 if (!info->attrs[BATADV_ATTR_VLANID]) 1235 return ERR_PTR(-EINVAL); 1236 1237 vid = nla_get_u16(info->attrs[BATADV_ATTR_VLANID]); 1238 1239 vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG); 1240 if (!vlan) 1241 return ERR_PTR(-ENOENT); 1242 1243 return vlan; 1244 } 1245 1246 /** 1247 * batadv_pre_doit() - Prepare batman-adv genl doit request 1248 * @ops: requested netlink operation 1249 * @skb: Netlink message with request data 1250 * @info: receiver information 1251 * 1252 * Return: 0 on success or negative error number in case of failure 1253 */ 1254 static int batadv_pre_doit(const struct genl_ops *ops, struct sk_buff *skb, 1255 struct genl_info *info) 1256 { 1257 struct net *net = genl_info_net(info); 1258 struct batadv_hard_iface *hard_iface; 1259 struct batadv_priv *bat_priv = NULL; 1260 struct batadv_softif_vlan *vlan; 1261 struct net_device *soft_iface; 1262 u8 user_ptr1_flags; 1263 u8 mesh_dep_flags; 1264 int ret; 1265 1266 user_ptr1_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN; 1267 if (WARN_ON(hweight8(ops->internal_flags & user_ptr1_flags) > 1)) 1268 return -EINVAL; 1269 1270 mesh_dep_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN; 1271 if (WARN_ON((ops->internal_flags & mesh_dep_flags) && 1272 (~ops->internal_flags & BATADV_FLAG_NEED_MESH))) 1273 return -EINVAL; 1274 1275 if (ops->internal_flags & BATADV_FLAG_NEED_MESH) { 1276 soft_iface = batadv_get_softif_from_info(net, info); 1277 if (IS_ERR(soft_iface)) 1278 return PTR_ERR(soft_iface); 1279 1280 bat_priv = netdev_priv(soft_iface); 1281 info->user_ptr[0] = bat_priv; 1282 } 1283 1284 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF) { 1285 hard_iface = batadv_get_hardif_from_info(bat_priv, net, info); 1286 if (IS_ERR(hard_iface)) { 1287 ret = PTR_ERR(hard_iface); 1288 goto err_put_softif; 1289 } 1290 1291 info->user_ptr[1] = hard_iface; 1292 } 1293 1294 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN) { 1295 vlan = batadv_get_vlan_from_info(bat_priv, net, info); 1296 if (IS_ERR(vlan)) { 1297 ret = PTR_ERR(vlan); 1298 goto err_put_softif; 1299 } 1300 1301 info->user_ptr[1] = vlan; 1302 } 1303 1304 return 0; 1305 1306 err_put_softif: 1307 if (bat_priv) 1308 dev_put(bat_priv->soft_iface); 1309 1310 return ret; 1311 } 1312 1313 /** 1314 * batadv_post_doit() - End batman-adv genl doit request 1315 * @ops: requested netlink operation 1316 * @skb: Netlink message with request data 1317 * @info: receiver information 1318 */ 1319 static void batadv_post_doit(const struct genl_ops *ops, struct sk_buff *skb, 1320 struct genl_info *info) 1321 { 1322 struct batadv_hard_iface *hard_iface; 1323 struct batadv_softif_vlan *vlan; 1324 struct batadv_priv *bat_priv; 1325 1326 if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF && 1327 info->user_ptr[1]) { 1328 hard_iface = info->user_ptr[1]; 1329 1330 batadv_hardif_put(hard_iface); 1331 } 1332 1333 if (ops->internal_flags & BATADV_FLAG_NEED_VLAN && info->user_ptr[1]) { 1334 vlan = info->user_ptr[1]; 1335 batadv_softif_vlan_put(vlan); 1336 } 1337 1338 if (ops->internal_flags & BATADV_FLAG_NEED_MESH && info->user_ptr[0]) { 1339 bat_priv = info->user_ptr[0]; 1340 dev_put(bat_priv->soft_iface); 1341 } 1342 } 1343 1344 static const struct genl_ops batadv_netlink_ops[] = { 1345 { 1346 .cmd = BATADV_CMD_GET_MESH, 1347 /* can be retrieved by unprivileged users */ 1348 .policy = batadv_netlink_policy, 1349 .doit = batadv_netlink_get_mesh, 1350 .internal_flags = BATADV_FLAG_NEED_MESH, 1351 }, 1352 { 1353 .cmd = BATADV_CMD_TP_METER, 1354 .flags = GENL_ADMIN_PERM, 1355 .policy = batadv_netlink_policy, 1356 .doit = batadv_netlink_tp_meter_start, 1357 .internal_flags = BATADV_FLAG_NEED_MESH, 1358 }, 1359 { 1360 .cmd = BATADV_CMD_TP_METER_CANCEL, 1361 .flags = GENL_ADMIN_PERM, 1362 .policy = batadv_netlink_policy, 1363 .doit = batadv_netlink_tp_meter_cancel, 1364 .internal_flags = BATADV_FLAG_NEED_MESH, 1365 }, 1366 { 1367 .cmd = BATADV_CMD_GET_ROUTING_ALGOS, 1368 .flags = GENL_ADMIN_PERM, 1369 .policy = batadv_netlink_policy, 1370 .dumpit = batadv_algo_dump, 1371 }, 1372 { 1373 .cmd = BATADV_CMD_GET_HARDIF, 1374 /* can be retrieved by unprivileged users */ 1375 .policy = batadv_netlink_policy, 1376 .dumpit = batadv_netlink_dump_hardif, 1377 .doit = batadv_netlink_get_hardif, 1378 .internal_flags = BATADV_FLAG_NEED_MESH | 1379 BATADV_FLAG_NEED_HARDIF, 1380 }, 1381 { 1382 .cmd = BATADV_CMD_GET_TRANSTABLE_LOCAL, 1383 .flags = GENL_ADMIN_PERM, 1384 .policy = batadv_netlink_policy, 1385 .dumpit = batadv_tt_local_dump, 1386 }, 1387 { 1388 .cmd = BATADV_CMD_GET_TRANSTABLE_GLOBAL, 1389 .flags = GENL_ADMIN_PERM, 1390 .policy = batadv_netlink_policy, 1391 .dumpit = batadv_tt_global_dump, 1392 }, 1393 { 1394 .cmd = BATADV_CMD_GET_ORIGINATORS, 1395 .flags = GENL_ADMIN_PERM, 1396 .policy = batadv_netlink_policy, 1397 .dumpit = batadv_orig_dump, 1398 }, 1399 { 1400 .cmd = BATADV_CMD_GET_NEIGHBORS, 1401 .flags = GENL_ADMIN_PERM, 1402 .policy = batadv_netlink_policy, 1403 .dumpit = batadv_hardif_neigh_dump, 1404 }, 1405 { 1406 .cmd = BATADV_CMD_GET_GATEWAYS, 1407 .flags = GENL_ADMIN_PERM, 1408 .policy = batadv_netlink_policy, 1409 .dumpit = batadv_gw_dump, 1410 }, 1411 { 1412 .cmd = BATADV_CMD_GET_BLA_CLAIM, 1413 .flags = GENL_ADMIN_PERM, 1414 .policy = batadv_netlink_policy, 1415 .dumpit = batadv_bla_claim_dump, 1416 }, 1417 { 1418 .cmd = BATADV_CMD_GET_BLA_BACKBONE, 1419 .flags = GENL_ADMIN_PERM, 1420 .policy = batadv_netlink_policy, 1421 .dumpit = batadv_bla_backbone_dump, 1422 }, 1423 { 1424 .cmd = BATADV_CMD_GET_DAT_CACHE, 1425 .flags = GENL_ADMIN_PERM, 1426 .policy = batadv_netlink_policy, 1427 .dumpit = batadv_dat_cache_dump, 1428 }, 1429 { 1430 .cmd = BATADV_CMD_GET_MCAST_FLAGS, 1431 .flags = GENL_ADMIN_PERM, 1432 .policy = batadv_netlink_policy, 1433 .dumpit = batadv_mcast_flags_dump, 1434 }, 1435 { 1436 .cmd = BATADV_CMD_SET_MESH, 1437 .flags = GENL_ADMIN_PERM, 1438 .policy = batadv_netlink_policy, 1439 .doit = batadv_netlink_set_mesh, 1440 .internal_flags = BATADV_FLAG_NEED_MESH, 1441 }, 1442 { 1443 .cmd = BATADV_CMD_SET_HARDIF, 1444 .flags = GENL_ADMIN_PERM, 1445 .policy = batadv_netlink_policy, 1446 .doit = batadv_netlink_set_hardif, 1447 .internal_flags = BATADV_FLAG_NEED_MESH | 1448 BATADV_FLAG_NEED_HARDIF, 1449 }, 1450 { 1451 .cmd = BATADV_CMD_GET_VLAN, 1452 /* can be retrieved by unprivileged users */ 1453 .policy = batadv_netlink_policy, 1454 .doit = batadv_netlink_get_vlan, 1455 .internal_flags = BATADV_FLAG_NEED_MESH | 1456 BATADV_FLAG_NEED_VLAN, 1457 }, 1458 { 1459 .cmd = BATADV_CMD_SET_VLAN, 1460 .flags = GENL_ADMIN_PERM, 1461 .policy = batadv_netlink_policy, 1462 .doit = batadv_netlink_set_vlan, 1463 .internal_flags = BATADV_FLAG_NEED_MESH | 1464 BATADV_FLAG_NEED_VLAN, 1465 }, 1466 }; 1467 1468 struct genl_family batadv_netlink_family __ro_after_init = { 1469 .hdrsize = 0, 1470 .name = BATADV_NL_NAME, 1471 .version = 1, 1472 .maxattr = BATADV_ATTR_MAX, 1473 .netnsok = true, 1474 .pre_doit = batadv_pre_doit, 1475 .post_doit = batadv_post_doit, 1476 .module = THIS_MODULE, 1477 .ops = batadv_netlink_ops, 1478 .n_ops = ARRAY_SIZE(batadv_netlink_ops), 1479 .mcgrps = batadv_netlink_mcgrps, 1480 .n_mcgrps = ARRAY_SIZE(batadv_netlink_mcgrps), 1481 }; 1482 1483 /** 1484 * batadv_netlink_register() - register batadv genl netlink family 1485 */ 1486 void __init batadv_netlink_register(void) 1487 { 1488 int ret; 1489 1490 ret = genl_register_family(&batadv_netlink_family); 1491 if (ret) 1492 pr_warn("unable to register netlink family"); 1493 } 1494 1495 /** 1496 * batadv_netlink_unregister() - unregister batadv genl netlink family 1497 */ 1498 void batadv_netlink_unregister(void) 1499 { 1500 genl_unregister_family(&batadv_netlink_family); 1501 } 1502