Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1 | /* |
Sven Eckelmann | 64afe35 | 2011-01-27 10:38:15 +0100 | [diff] [blame] | 2 | * Copyright (C) 2010-2011 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 3 | * |
| 4 | * Marek Lindner |
| 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, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | * 02110-1301, USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "main.h" |
| 23 | #include "bat_sysfs.h" |
| 24 | #include "translation-table.h" |
| 25 | #include "originator.h" |
| 26 | #include "hard-interface.h" |
| 27 | #include "gateway_common.h" |
| 28 | #include "gateway_client.h" |
| 29 | #include "vis.h" |
| 30 | |
| 31 | #define to_dev(obj) container_of(obj, struct device, kobj) |
| 32 | #define kobj_to_netdev(obj) to_net_dev(to_dev(obj->parent)) |
| 33 | #define kobj_to_batpriv(obj) netdev_priv(kobj_to_netdev(obj)) |
| 34 | |
| 35 | /* Use this, if you have customized show and store functions */ |
| 36 | #define BAT_ATTR(_name, _mode, _show, _store) \ |
| 37 | struct bat_attribute bat_attr_##_name = { \ |
| 38 | .attr = {.name = __stringify(_name), \ |
| 39 | .mode = _mode }, \ |
| 40 | .show = _show, \ |
| 41 | .store = _store, \ |
| 42 | }; |
| 43 | |
| 44 | #define BAT_ATTR_STORE_BOOL(_name, _post_func) \ |
| 45 | ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \ |
| 46 | char *buff, size_t count) \ |
| 47 | { \ |
| 48 | struct net_device *net_dev = kobj_to_netdev(kobj); \ |
| 49 | struct bat_priv *bat_priv = netdev_priv(net_dev); \ |
| 50 | return __store_bool_attr(buff, count, _post_func, attr, \ |
| 51 | &bat_priv->_name, net_dev); \ |
| 52 | } |
| 53 | |
| 54 | #define BAT_ATTR_SHOW_BOOL(_name) \ |
| 55 | ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \ |
| 56 | char *buff) \ |
| 57 | { \ |
| 58 | struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \ |
| 59 | return sprintf(buff, "%s\n", \ |
| 60 | atomic_read(&bat_priv->_name) == 0 ? \ |
| 61 | "disabled" : "enabled"); \ |
| 62 | } \ |
| 63 | |
| 64 | /* Use this, if you are going to turn a [name] in bat_priv on or off */ |
| 65 | #define BAT_ATTR_BOOL(_name, _mode, _post_func) \ |
| 66 | static BAT_ATTR_STORE_BOOL(_name, _post_func) \ |
| 67 | static BAT_ATTR_SHOW_BOOL(_name) \ |
| 68 | static BAT_ATTR(_name, _mode, show_##_name, store_##_name) |
| 69 | |
| 70 | |
| 71 | #define BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func) \ |
| 72 | ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \ |
| 73 | char *buff, size_t count) \ |
| 74 | { \ |
| 75 | struct net_device *net_dev = kobj_to_netdev(kobj); \ |
| 76 | struct bat_priv *bat_priv = netdev_priv(net_dev); \ |
| 77 | return __store_uint_attr(buff, count, _min, _max, _post_func, \ |
| 78 | attr, &bat_priv->_name, net_dev); \ |
| 79 | } |
| 80 | |
| 81 | #define BAT_ATTR_SHOW_UINT(_name) \ |
| 82 | ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \ |
| 83 | char *buff) \ |
| 84 | { \ |
| 85 | struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \ |
| 86 | return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \ |
| 87 | } \ |
| 88 | |
| 89 | /* Use this, if you are going to set [name] in bat_priv to unsigned integer |
| 90 | * values only */ |
| 91 | #define BAT_ATTR_UINT(_name, _mode, _min, _max, _post_func) \ |
| 92 | static BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func) \ |
| 93 | static BAT_ATTR_SHOW_UINT(_name) \ |
| 94 | static BAT_ATTR(_name, _mode, show_##_name, store_##_name) |
| 95 | |
| 96 | |
| 97 | static int store_bool_attr(char *buff, size_t count, |
| 98 | struct net_device *net_dev, |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame^] | 99 | const char *attr_name, atomic_t *attr) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 100 | { |
| 101 | int enabled = -1; |
| 102 | |
| 103 | if (buff[count - 1] == '\n') |
| 104 | buff[count - 1] = '\0'; |
| 105 | |
| 106 | if ((strncmp(buff, "1", 2) == 0) || |
| 107 | (strncmp(buff, "enable", 7) == 0) || |
| 108 | (strncmp(buff, "enabled", 8) == 0)) |
| 109 | enabled = 1; |
| 110 | |
| 111 | if ((strncmp(buff, "0", 2) == 0) || |
| 112 | (strncmp(buff, "disable", 8) == 0) || |
| 113 | (strncmp(buff, "disabled", 9) == 0)) |
| 114 | enabled = 0; |
| 115 | |
| 116 | if (enabled < 0) { |
| 117 | bat_info(net_dev, |
| 118 | "%s: Invalid parameter received: %s\n", |
| 119 | attr_name, buff); |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | |
| 123 | if (atomic_read(attr) == enabled) |
| 124 | return count; |
| 125 | |
| 126 | bat_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name, |
| 127 | atomic_read(attr) == 1 ? "enabled" : "disabled", |
| 128 | enabled == 1 ? "enabled" : "disabled"); |
| 129 | |
| 130 | atomic_set(attr, (unsigned)enabled); |
| 131 | return count; |
| 132 | } |
| 133 | |
| 134 | static inline ssize_t __store_bool_attr(char *buff, size_t count, |
| 135 | void (*post_func)(struct net_device *), |
| 136 | struct attribute *attr, |
| 137 | atomic_t *attr_store, struct net_device *net_dev) |
| 138 | { |
| 139 | int ret; |
| 140 | |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame^] | 141 | ret = store_bool_attr(buff, count, net_dev, attr->name, attr_store); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 142 | if (post_func && ret) |
| 143 | post_func(net_dev); |
| 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame^] | 148 | static int store_uint_attr(const char *buff, size_t count, |
| 149 | struct net_device *net_dev, const char *attr_name, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 150 | unsigned int min, unsigned int max, atomic_t *attr) |
| 151 | { |
| 152 | unsigned long uint_val; |
| 153 | int ret; |
| 154 | |
| 155 | ret = strict_strtoul(buff, 10, &uint_val); |
| 156 | if (ret) { |
| 157 | bat_info(net_dev, |
| 158 | "%s: Invalid parameter received: %s\n", |
| 159 | attr_name, buff); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | |
| 163 | if (uint_val < min) { |
| 164 | bat_info(net_dev, "%s: Value is too small: %lu min: %u\n", |
| 165 | attr_name, uint_val, min); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | if (uint_val > max) { |
| 170 | bat_info(net_dev, "%s: Value is too big: %lu max: %u\n", |
| 171 | attr_name, uint_val, max); |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | if (atomic_read(attr) == uint_val) |
| 176 | return count; |
| 177 | |
| 178 | bat_info(net_dev, "%s: Changing from: %i to: %lu\n", |
| 179 | attr_name, atomic_read(attr), uint_val); |
| 180 | |
| 181 | atomic_set(attr, uint_val); |
| 182 | return count; |
| 183 | } |
| 184 | |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame^] | 185 | static inline ssize_t __store_uint_attr(const char *buff, size_t count, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 186 | int min, int max, |
| 187 | void (*post_func)(struct net_device *), |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame^] | 188 | const struct attribute *attr, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 189 | atomic_t *attr_store, struct net_device *net_dev) |
| 190 | { |
| 191 | int ret; |
| 192 | |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame^] | 193 | ret = store_uint_attr(buff, count, net_dev, attr->name, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 194 | min, max, attr_store); |
| 195 | if (post_func && ret) |
| 196 | post_func(net_dev); |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr, |
| 202 | char *buff) |
| 203 | { |
| 204 | struct bat_priv *bat_priv = kobj_to_batpriv(kobj); |
| 205 | int vis_mode = atomic_read(&bat_priv->vis_mode); |
| 206 | |
| 207 | return sprintf(buff, "%s\n", |
| 208 | vis_mode == VIS_TYPE_CLIENT_UPDATE ? |
| 209 | "client" : "server"); |
| 210 | } |
| 211 | |
| 212 | static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr, |
| 213 | char *buff, size_t count) |
| 214 | { |
| 215 | struct net_device *net_dev = kobj_to_netdev(kobj); |
| 216 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
| 217 | unsigned long val; |
| 218 | int ret, vis_mode_tmp = -1; |
| 219 | |
| 220 | ret = strict_strtoul(buff, 10, &val); |
| 221 | |
| 222 | if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) || |
| 223 | (strncmp(buff, "client", 6) == 0) || |
| 224 | (strncmp(buff, "off", 3) == 0)) |
| 225 | vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE; |
| 226 | |
| 227 | if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) || |
| 228 | (strncmp(buff, "server", 6) == 0)) |
| 229 | vis_mode_tmp = VIS_TYPE_SERVER_SYNC; |
| 230 | |
| 231 | if (vis_mode_tmp < 0) { |
| 232 | if (buff[count - 1] == '\n') |
| 233 | buff[count - 1] = '\0'; |
| 234 | |
| 235 | bat_info(net_dev, |
| 236 | "Invalid parameter for 'vis mode' setting received: " |
| 237 | "%s\n", buff); |
| 238 | return -EINVAL; |
| 239 | } |
| 240 | |
| 241 | if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp) |
| 242 | return count; |
| 243 | |
| 244 | bat_info(net_dev, "Changing vis mode from: %s to: %s\n", |
| 245 | atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE ? |
| 246 | "client" : "server", vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE ? |
| 247 | "client" : "server"); |
| 248 | |
| 249 | atomic_set(&bat_priv->vis_mode, (unsigned)vis_mode_tmp); |
| 250 | return count; |
| 251 | } |
| 252 | |
| 253 | static void post_gw_deselect(struct net_device *net_dev) |
| 254 | { |
| 255 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
| 256 | gw_deselect(bat_priv); |
| 257 | } |
| 258 | |
| 259 | static ssize_t show_gw_mode(struct kobject *kobj, struct attribute *attr, |
| 260 | char *buff) |
| 261 | { |
| 262 | struct bat_priv *bat_priv = kobj_to_batpriv(kobj); |
| 263 | int bytes_written; |
| 264 | |
| 265 | switch (atomic_read(&bat_priv->gw_mode)) { |
| 266 | case GW_MODE_CLIENT: |
| 267 | bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME); |
| 268 | break; |
| 269 | case GW_MODE_SERVER: |
| 270 | bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME); |
| 271 | break; |
| 272 | default: |
| 273 | bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME); |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | return bytes_written; |
| 278 | } |
| 279 | |
| 280 | static ssize_t store_gw_mode(struct kobject *kobj, struct attribute *attr, |
| 281 | char *buff, size_t count) |
| 282 | { |
| 283 | struct net_device *net_dev = kobj_to_netdev(kobj); |
| 284 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
| 285 | char *curr_gw_mode_str; |
| 286 | int gw_mode_tmp = -1; |
| 287 | |
| 288 | if (buff[count - 1] == '\n') |
| 289 | buff[count - 1] = '\0'; |
| 290 | |
| 291 | if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0) |
| 292 | gw_mode_tmp = GW_MODE_OFF; |
| 293 | |
| 294 | if (strncmp(buff, GW_MODE_CLIENT_NAME, |
| 295 | strlen(GW_MODE_CLIENT_NAME)) == 0) |
| 296 | gw_mode_tmp = GW_MODE_CLIENT; |
| 297 | |
| 298 | if (strncmp(buff, GW_MODE_SERVER_NAME, |
| 299 | strlen(GW_MODE_SERVER_NAME)) == 0) |
| 300 | gw_mode_tmp = GW_MODE_SERVER; |
| 301 | |
| 302 | if (gw_mode_tmp < 0) { |
| 303 | bat_info(net_dev, |
| 304 | "Invalid parameter for 'gw mode' setting received: " |
| 305 | "%s\n", buff); |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | |
| 309 | if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp) |
| 310 | return count; |
| 311 | |
| 312 | switch (atomic_read(&bat_priv->gw_mode)) { |
| 313 | case GW_MODE_CLIENT: |
| 314 | curr_gw_mode_str = GW_MODE_CLIENT_NAME; |
| 315 | break; |
| 316 | case GW_MODE_SERVER: |
| 317 | curr_gw_mode_str = GW_MODE_SERVER_NAME; |
| 318 | break; |
| 319 | default: |
| 320 | curr_gw_mode_str = GW_MODE_OFF_NAME; |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | bat_info(net_dev, "Changing gw mode from: %s to: %s\n", |
| 325 | curr_gw_mode_str, buff); |
| 326 | |
| 327 | gw_deselect(bat_priv); |
| 328 | atomic_set(&bat_priv->gw_mode, (unsigned)gw_mode_tmp); |
| 329 | return count; |
| 330 | } |
| 331 | |
| 332 | static ssize_t show_gw_bwidth(struct kobject *kobj, struct attribute *attr, |
| 333 | char *buff) |
| 334 | { |
| 335 | struct bat_priv *bat_priv = kobj_to_batpriv(kobj); |
| 336 | int down, up; |
| 337 | int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth); |
| 338 | |
| 339 | gw_bandwidth_to_kbit(gw_bandwidth, &down, &up); |
| 340 | return sprintf(buff, "%i%s/%i%s\n", |
| 341 | (down > 2048 ? down / 1024 : down), |
| 342 | (down > 2048 ? "MBit" : "KBit"), |
| 343 | (up > 2048 ? up / 1024 : up), |
| 344 | (up > 2048 ? "MBit" : "KBit")); |
| 345 | } |
| 346 | |
| 347 | static ssize_t store_gw_bwidth(struct kobject *kobj, struct attribute *attr, |
| 348 | char *buff, size_t count) |
| 349 | { |
| 350 | struct net_device *net_dev = kobj_to_netdev(kobj); |
| 351 | |
| 352 | if (buff[count - 1] == '\n') |
| 353 | buff[count - 1] = '\0'; |
| 354 | |
| 355 | return gw_bandwidth_set(net_dev, buff, count); |
| 356 | } |
| 357 | |
| 358 | BAT_ATTR_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL); |
| 359 | BAT_ATTR_BOOL(bonding, S_IRUGO | S_IWUSR, NULL); |
| 360 | BAT_ATTR_BOOL(fragmentation, S_IRUGO | S_IWUSR, update_min_mtu); |
| 361 | static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, show_vis_mode, store_vis_mode); |
| 362 | static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, show_gw_mode, store_gw_mode); |
| 363 | BAT_ATTR_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL); |
| 364 | BAT_ATTR_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL); |
| 365 | BAT_ATTR_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE, |
| 366 | post_gw_deselect); |
| 367 | static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, show_gw_bwidth, |
| 368 | store_gw_bwidth); |
| 369 | #ifdef CONFIG_BATMAN_ADV_DEBUG |
| 370 | BAT_ATTR_UINT(log_level, S_IRUGO | S_IWUSR, 0, 3, NULL); |
| 371 | #endif |
| 372 | |
| 373 | static struct bat_attribute *mesh_attrs[] = { |
| 374 | &bat_attr_aggregated_ogms, |
| 375 | &bat_attr_bonding, |
| 376 | &bat_attr_fragmentation, |
| 377 | &bat_attr_vis_mode, |
| 378 | &bat_attr_gw_mode, |
| 379 | &bat_attr_orig_interval, |
| 380 | &bat_attr_hop_penalty, |
| 381 | &bat_attr_gw_sel_class, |
| 382 | &bat_attr_gw_bandwidth, |
| 383 | #ifdef CONFIG_BATMAN_ADV_DEBUG |
| 384 | &bat_attr_log_level, |
| 385 | #endif |
| 386 | NULL, |
| 387 | }; |
| 388 | |
| 389 | int sysfs_add_meshif(struct net_device *dev) |
| 390 | { |
| 391 | struct kobject *batif_kobject = &dev->dev.kobj; |
| 392 | struct bat_priv *bat_priv = netdev_priv(dev); |
| 393 | struct bat_attribute **bat_attr; |
| 394 | int err; |
| 395 | |
| 396 | bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR, |
| 397 | batif_kobject); |
| 398 | if (!bat_priv->mesh_obj) { |
| 399 | bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name, |
| 400 | SYSFS_IF_MESH_SUBDIR); |
| 401 | goto out; |
| 402 | } |
| 403 | |
| 404 | for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) { |
| 405 | err = sysfs_create_file(bat_priv->mesh_obj, |
| 406 | &((*bat_attr)->attr)); |
| 407 | if (err) { |
| 408 | bat_err(dev, "Can't add sysfs file: %s/%s/%s\n", |
| 409 | dev->name, SYSFS_IF_MESH_SUBDIR, |
| 410 | ((*bat_attr)->attr).name); |
| 411 | goto rem_attr; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | |
| 417 | rem_attr: |
| 418 | for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) |
| 419 | sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr)); |
| 420 | |
| 421 | kobject_put(bat_priv->mesh_obj); |
| 422 | bat_priv->mesh_obj = NULL; |
| 423 | out: |
| 424 | return -ENOMEM; |
| 425 | } |
| 426 | |
| 427 | void sysfs_del_meshif(struct net_device *dev) |
| 428 | { |
| 429 | struct bat_priv *bat_priv = netdev_priv(dev); |
| 430 | struct bat_attribute **bat_attr; |
| 431 | |
| 432 | for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) |
| 433 | sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr)); |
| 434 | |
| 435 | kobject_put(bat_priv->mesh_obj); |
| 436 | bat_priv->mesh_obj = NULL; |
| 437 | } |
| 438 | |
| 439 | static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr, |
| 440 | char *buff) |
| 441 | { |
| 442 | struct net_device *net_dev = kobj_to_netdev(kobj); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 443 | struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 444 | ssize_t length; |
| 445 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 446 | if (!hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 447 | return 0; |
| 448 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 449 | length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ? |
| 450 | "none" : hard_iface->soft_iface->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 451 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 452 | hardif_free_ref(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 453 | |
| 454 | return length; |
| 455 | } |
| 456 | |
| 457 | static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr, |
| 458 | char *buff, size_t count) |
| 459 | { |
| 460 | struct net_device *net_dev = kobj_to_netdev(kobj); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 461 | struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 462 | int status_tmp = -1; |
Marek Lindner | ed75ccb | 2011-02-10 14:33:51 +0000 | [diff] [blame] | 463 | int ret = count; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 464 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 465 | if (!hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 466 | return count; |
| 467 | |
| 468 | if (buff[count - 1] == '\n') |
| 469 | buff[count - 1] = '\0'; |
| 470 | |
| 471 | if (strlen(buff) >= IFNAMSIZ) { |
| 472 | pr_err("Invalid parameter for 'mesh_iface' setting received: " |
| 473 | "interface name too long '%s'\n", buff); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 474 | hardif_free_ref(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 475 | return -EINVAL; |
| 476 | } |
| 477 | |
| 478 | if (strncmp(buff, "none", 4) == 0) |
| 479 | status_tmp = IF_NOT_IN_USE; |
| 480 | else |
| 481 | status_tmp = IF_I_WANT_YOU; |
| 482 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 483 | if (hard_iface->if_status == status_tmp) |
| 484 | goto out; |
| 485 | |
| 486 | if ((hard_iface->soft_iface) && |
| 487 | (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0)) |
Marek Lindner | ed75ccb | 2011-02-10 14:33:51 +0000 | [diff] [blame] | 488 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 489 | |
Sven Eckelmann | 3a4375a | 2011-05-03 13:10:06 +0200 | [diff] [blame] | 490 | if (!rtnl_trylock()) { |
| 491 | ret = -ERESTARTSYS; |
Marek Lindner | ed75ccb | 2011-02-10 14:33:51 +0000 | [diff] [blame] | 492 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Sven Eckelmann | 3a4375a | 2011-05-03 13:10:06 +0200 | [diff] [blame] | 495 | if (status_tmp == IF_NOT_IN_USE) { |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 496 | hardif_disable_interface(hard_iface); |
Sven Eckelmann | 3a4375a | 2011-05-03 13:10:06 +0200 | [diff] [blame] | 497 | goto unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Sven Eckelmann | 3a4375a | 2011-05-03 13:10:06 +0200 | [diff] [blame] | 500 | /* if the interface already is in use */ |
| 501 | if (hard_iface->if_status != IF_NOT_IN_USE) |
| 502 | hardif_disable_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 503 | |
Sven Eckelmann | 3a4375a | 2011-05-03 13:10:06 +0200 | [diff] [blame] | 504 | ret = hardif_enable_interface(hard_iface, buff); |
| 505 | |
| 506 | unlock: |
| 507 | rtnl_unlock(); |
Marek Lindner | ed75ccb | 2011-02-10 14:33:51 +0000 | [diff] [blame] | 508 | out: |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 509 | hardif_free_ref(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr, |
| 514 | char *buff) |
| 515 | { |
| 516 | struct net_device *net_dev = kobj_to_netdev(kobj); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 517 | struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 518 | ssize_t length; |
| 519 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 520 | if (!hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 521 | return 0; |
| 522 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 523 | switch (hard_iface->if_status) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 524 | case IF_TO_BE_REMOVED: |
| 525 | length = sprintf(buff, "disabling\n"); |
| 526 | break; |
| 527 | case IF_INACTIVE: |
| 528 | length = sprintf(buff, "inactive\n"); |
| 529 | break; |
| 530 | case IF_ACTIVE: |
| 531 | length = sprintf(buff, "active\n"); |
| 532 | break; |
| 533 | case IF_TO_BE_ACTIVATED: |
| 534 | length = sprintf(buff, "enabling\n"); |
| 535 | break; |
| 536 | case IF_NOT_IN_USE: |
| 537 | default: |
| 538 | length = sprintf(buff, "not in use\n"); |
| 539 | break; |
| 540 | } |
| 541 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 542 | hardif_free_ref(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 543 | |
| 544 | return length; |
| 545 | } |
| 546 | |
| 547 | static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR, |
| 548 | show_mesh_iface, store_mesh_iface); |
| 549 | static BAT_ATTR(iface_status, S_IRUGO, show_iface_status, NULL); |
| 550 | |
| 551 | static struct bat_attribute *batman_attrs[] = { |
| 552 | &bat_attr_mesh_iface, |
| 553 | &bat_attr_iface_status, |
| 554 | NULL, |
| 555 | }; |
| 556 | |
| 557 | int sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev) |
| 558 | { |
| 559 | struct kobject *hardif_kobject = &dev->dev.kobj; |
| 560 | struct bat_attribute **bat_attr; |
| 561 | int err; |
| 562 | |
| 563 | *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR, |
| 564 | hardif_kobject); |
| 565 | |
| 566 | if (!*hardif_obj) { |
| 567 | bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name, |
| 568 | SYSFS_IF_BAT_SUBDIR); |
| 569 | goto out; |
| 570 | } |
| 571 | |
| 572 | for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) { |
| 573 | err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr)); |
| 574 | if (err) { |
| 575 | bat_err(dev, "Can't add sysfs file: %s/%s/%s\n", |
| 576 | dev->name, SYSFS_IF_BAT_SUBDIR, |
| 577 | ((*bat_attr)->attr).name); |
| 578 | goto rem_attr; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | return 0; |
| 583 | |
| 584 | rem_attr: |
| 585 | for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) |
| 586 | sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr)); |
| 587 | out: |
| 588 | return -ENOMEM; |
| 589 | } |
| 590 | |
| 591 | void sysfs_del_hardif(struct kobject **hardif_obj) |
| 592 | { |
| 593 | kobject_put(*hardif_obj); |
| 594 | *hardif_obj = NULL; |
| 595 | } |