]>
Commit | Line | Data |
---|---|---|
9cfc7bd6 | 1 | /* Copyright (C) 2010-2012 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
2 | * |
3 | * Marek Lindner | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of version 2 of the GNU General Public | |
7 | * License as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
17 | * 02110-1301, USA | |
c6c8fea2 SE |
18 | */ |
19 | ||
20 | #include "main.h" | |
21 | #include "bat_sysfs.h" | |
22 | #include "translation-table.h" | |
23 | #include "originator.h" | |
24 | #include "hard-interface.h" | |
25 | #include "gateway_common.h" | |
26 | #include "gateway_client.h" | |
27 | #include "vis.h" | |
28 | ||
0ff9b86f | 29 | static struct net_device *batadv_kobj_to_netdev(struct kobject *obj) |
3d222bba SE |
30 | { |
31 | struct device *dev = container_of(obj->parent, struct device, kobj); | |
32 | return to_net_dev(dev); | |
33 | } | |
34 | ||
0ff9b86f | 35 | static struct bat_priv *batadv_kobj_to_batpriv(struct kobject *obj) |
3d222bba | 36 | { |
0ff9b86f | 37 | struct net_device *net_dev = batadv_kobj_to_netdev(obj); |
3d222bba SE |
38 | return netdev_priv(net_dev); |
39 | } | |
c6c8fea2 | 40 | |
347c80f0 SE |
41 | #define BATADV_UEV_TYPE_VAR "BATTYPE=" |
42 | #define BATADV_UEV_ACTION_VAR "BATACTION=" | |
43 | #define BATADV_UEV_DATA_VAR "BATDATA=" | |
c6bda689 | 44 | |
0ff9b86f | 45 | static char *batadv_uev_action_str[] = { |
c6bda689 AQ |
46 | "add", |
47 | "del", | |
48 | "change" | |
49 | }; | |
50 | ||
0ff9b86f | 51 | static char *batadv_uev_type_str[] = { |
c6bda689 AQ |
52 | "gw" |
53 | }; | |
54 | ||
c6c8fea2 | 55 | /* Use this, if you have customized show and store functions */ |
347c80f0 SE |
56 | #define BATADV_ATTR(_name, _mode, _show, _store) \ |
57 | struct bat_attribute batadv_attr_##_name = { \ | |
58 | .attr = {.name = __stringify(_name), \ | |
59 | .mode = _mode }, \ | |
60 | .show = _show, \ | |
61 | .store = _store, \ | |
c6c8fea2 SE |
62 | }; |
63 | ||
347c80f0 | 64 | #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \ |
0ff9b86f SE |
65 | ssize_t batadv_store_##_name(struct kobject *kobj, \ |
66 | struct attribute *attr, char *buff, \ | |
67 | size_t count) \ | |
c6c8fea2 | 68 | { \ |
0ff9b86f | 69 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \ |
c6c8fea2 | 70 | struct bat_priv *bat_priv = netdev_priv(net_dev); \ |
0ff9b86f SE |
71 | return __batadv_store_bool_attr(buff, count, _post_func, attr, \ |
72 | &bat_priv->_name, net_dev); \ | |
c6c8fea2 SE |
73 | } |
74 | ||
347c80f0 | 75 | #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \ |
0ff9b86f SE |
76 | ssize_t batadv_show_##_name(struct kobject *kobj, \ |
77 | struct attribute *attr, char *buff) \ | |
c6c8fea2 | 78 | { \ |
0ff9b86f | 79 | struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \ |
c6c8fea2 SE |
80 | return sprintf(buff, "%s\n", \ |
81 | atomic_read(&bat_priv->_name) == 0 ? \ | |
82 | "disabled" : "enabled"); \ | |
83 | } \ | |
84 | ||
f245c38b | 85 | /* Use this, if you are going to turn a [name] in the soft-interface |
9cfc7bd6 SE |
86 | * (bat_priv) on or off |
87 | */ | |
347c80f0 SE |
88 | #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \ |
89 | static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \ | |
90 | static BATADV_ATTR_SIF_SHOW_BOOL(_name) \ | |
91 | static BATADV_ATTR(_name, _mode, batadv_show_##_name, \ | |
92 | batadv_store_##_name) | |
c6c8fea2 SE |
93 | |
94 | ||
347c80f0 | 95 | #define BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \ |
0ff9b86f SE |
96 | ssize_t batadv_store_##_name(struct kobject *kobj, \ |
97 | struct attribute *attr, char *buff, \ | |
98 | size_t count) \ | |
c6c8fea2 | 99 | { \ |
0ff9b86f | 100 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \ |
c6c8fea2 | 101 | struct bat_priv *bat_priv = netdev_priv(net_dev); \ |
0ff9b86f SE |
102 | return __batadv_store_uint_attr(buff, count, _min, _max, \ |
103 | _post_func, attr, \ | |
104 | &bat_priv->_name, net_dev); \ | |
c6c8fea2 SE |
105 | } |
106 | ||
347c80f0 | 107 | #define BATADV_ATTR_SIF_SHOW_UINT(_name) \ |
0ff9b86f SE |
108 | ssize_t batadv_show_##_name(struct kobject *kobj, \ |
109 | struct attribute *attr, char *buff) \ | |
c6c8fea2 | 110 | { \ |
0ff9b86f | 111 | struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \ |
c6c8fea2 SE |
112 | return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \ |
113 | } \ | |
114 | ||
f245c38b | 115 | /* Use this, if you are going to set [name] in the soft-interface |
9cfc7bd6 SE |
116 | * (bat_priv) to an unsigned integer value |
117 | */ | |
347c80f0 SE |
118 | #define BATADV_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func) \ |
119 | static BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func)\ | |
120 | static BATADV_ATTR_SIF_SHOW_UINT(_name) \ | |
121 | static BATADV_ATTR(_name, _mode, batadv_show_##_name, \ | |
122 | batadv_store_##_name) | |
c6c8fea2 SE |
123 | |
124 | ||
347c80f0 | 125 | #define BATADV_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func) \ |
0ff9b86f SE |
126 | ssize_t batadv_store_##_name(struct kobject *kobj, \ |
127 | struct attribute *attr, char *buff, \ | |
128 | size_t count) \ | |
9d853f62 | 129 | { \ |
0ff9b86f | 130 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \ |
9563877e | 131 | struct hard_iface *hard_iface; \ |
9d853f62 LL |
132 | ssize_t length; \ |
133 | \ | |
9563877e | 134 | hard_iface = batadv_hardif_get_by_netdev(net_dev); \ |
9d853f62 LL |
135 | if (!hard_iface) \ |
136 | return 0; \ | |
137 | \ | |
0ff9b86f SE |
138 | length = __batadv_store_uint_attr(buff, count, _min, _max, \ |
139 | _post_func, attr, \ | |
140 | &hard_iface->_name, net_dev); \ | |
9d853f62 | 141 | \ |
e5d89254 | 142 | batadv_hardif_free_ref(hard_iface); \ |
9d853f62 LL |
143 | return length; \ |
144 | } | |
145 | ||
347c80f0 | 146 | #define BATADV_ATTR_HIF_SHOW_UINT(_name) \ |
0ff9b86f SE |
147 | ssize_t batadv_show_##_name(struct kobject *kobj, \ |
148 | struct attribute *attr, char *buff) \ | |
9d853f62 | 149 | { \ |
0ff9b86f | 150 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \ |
9563877e | 151 | struct hard_iface *hard_iface; \ |
9d853f62 LL |
152 | ssize_t length; \ |
153 | \ | |
9563877e | 154 | hard_iface = batadv_hardif_get_by_netdev(net_dev); \ |
9d853f62 LL |
155 | if (!hard_iface) \ |
156 | return 0; \ | |
157 | \ | |
158 | length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_name));\ | |
159 | \ | |
e5d89254 | 160 | batadv_hardif_free_ref(hard_iface); \ |
9d853f62 LL |
161 | return length; \ |
162 | } | |
163 | ||
164 | /* Use this, if you are going to set [name] in hard_iface to an | |
9cfc7bd6 SE |
165 | * unsigned integer value |
166 | */ | |
347c80f0 SE |
167 | #define BATADV_ATTR_HIF_UINT(_name, _mode, _min, _max, _post_func) \ |
168 | static BATADV_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func)\ | |
169 | static BATADV_ATTR_HIF_SHOW_UINT(_name) \ | |
170 | static BATADV_ATTR(_name, _mode, batadv_show_##_name, \ | |
171 | batadv_store_##_name) | |
9d853f62 LL |
172 | |
173 | ||
0ff9b86f SE |
174 | static int batadv_store_bool_attr(char *buff, size_t count, |
175 | struct net_device *net_dev, | |
176 | const char *attr_name, atomic_t *attr) | |
c6c8fea2 SE |
177 | { |
178 | int enabled = -1; | |
179 | ||
180 | if (buff[count - 1] == '\n') | |
181 | buff[count - 1] = '\0'; | |
182 | ||
183 | if ((strncmp(buff, "1", 2) == 0) || | |
184 | (strncmp(buff, "enable", 7) == 0) || | |
185 | (strncmp(buff, "enabled", 8) == 0)) | |
186 | enabled = 1; | |
187 | ||
188 | if ((strncmp(buff, "0", 2) == 0) || | |
189 | (strncmp(buff, "disable", 8) == 0) || | |
190 | (strncmp(buff, "disabled", 9) == 0)) | |
191 | enabled = 0; | |
192 | ||
193 | if (enabled < 0) { | |
3e34819e SE |
194 | batadv_info(net_dev, "%s: Invalid parameter received: %s\n", |
195 | attr_name, buff); | |
c6c8fea2 SE |
196 | return -EINVAL; |
197 | } | |
198 | ||
199 | if (atomic_read(attr) == enabled) | |
200 | return count; | |
201 | ||
3e34819e SE |
202 | batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name, |
203 | atomic_read(attr) == 1 ? "enabled" : "disabled", | |
204 | enabled == 1 ? "enabled" : "disabled"); | |
c6c8fea2 | 205 | |
95c96174 | 206 | atomic_set(attr, (unsigned int)enabled); |
c6c8fea2 SE |
207 | return count; |
208 | } | |
209 | ||
0ff9b86f SE |
210 | static inline ssize_t |
211 | __batadv_store_bool_attr(char *buff, size_t count, | |
212 | void (*post_func)(struct net_device *), | |
213 | struct attribute *attr, | |
214 | atomic_t *attr_store, struct net_device *net_dev) | |
c6c8fea2 SE |
215 | { |
216 | int ret; | |
217 | ||
0ff9b86f SE |
218 | ret = batadv_store_bool_attr(buff, count, net_dev, attr->name, |
219 | attr_store); | |
c6c8fea2 SE |
220 | if (post_func && ret) |
221 | post_func(net_dev); | |
222 | ||
223 | return ret; | |
224 | } | |
225 | ||
0ff9b86f SE |
226 | static int batadv_store_uint_attr(const char *buff, size_t count, |
227 | struct net_device *net_dev, | |
228 | const char *attr_name, | |
229 | unsigned int min, unsigned int max, | |
230 | atomic_t *attr) | |
c6c8fea2 SE |
231 | { |
232 | unsigned long uint_val; | |
233 | int ret; | |
234 | ||
25a92b13 | 235 | ret = kstrtoul(buff, 10, &uint_val); |
c6c8fea2 | 236 | if (ret) { |
3e34819e SE |
237 | batadv_info(net_dev, "%s: Invalid parameter received: %s\n", |
238 | attr_name, buff); | |
c6c8fea2 SE |
239 | return -EINVAL; |
240 | } | |
241 | ||
242 | if (uint_val < min) { | |
3e34819e SE |
243 | batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n", |
244 | attr_name, uint_val, min); | |
c6c8fea2 SE |
245 | return -EINVAL; |
246 | } | |
247 | ||
248 | if (uint_val > max) { | |
3e34819e SE |
249 | batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n", |
250 | attr_name, uint_val, max); | |
c6c8fea2 SE |
251 | return -EINVAL; |
252 | } | |
253 | ||
254 | if (atomic_read(attr) == uint_val) | |
255 | return count; | |
256 | ||
3e34819e SE |
257 | batadv_info(net_dev, "%s: Changing from: %i to: %lu\n", |
258 | attr_name, atomic_read(attr), uint_val); | |
c6c8fea2 SE |
259 | |
260 | atomic_set(attr, uint_val); | |
261 | return count; | |
262 | } | |
263 | ||
0ff9b86f SE |
264 | static inline ssize_t |
265 | __batadv_store_uint_attr(const char *buff, size_t count, | |
266 | int min, int max, | |
267 | void (*post_func)(struct net_device *), | |
268 | const struct attribute *attr, | |
269 | atomic_t *attr_store, struct net_device *net_dev) | |
c6c8fea2 SE |
270 | { |
271 | int ret; | |
272 | ||
0ff9b86f SE |
273 | ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max, |
274 | attr_store); | |
c6c8fea2 SE |
275 | if (post_func && ret) |
276 | post_func(net_dev); | |
277 | ||
278 | return ret; | |
279 | } | |
280 | ||
0ff9b86f SE |
281 | static ssize_t batadv_show_vis_mode(struct kobject *kobj, |
282 | struct attribute *attr, char *buff) | |
c6c8fea2 | 283 | { |
0ff9b86f | 284 | struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj); |
c6c8fea2 SE |
285 | int vis_mode = atomic_read(&bat_priv->vis_mode); |
286 | ||
287 | return sprintf(buff, "%s\n", | |
288 | vis_mode == VIS_TYPE_CLIENT_UPDATE ? | |
289 | "client" : "server"); | |
290 | } | |
291 | ||
0ff9b86f SE |
292 | static ssize_t batadv_store_vis_mode(struct kobject *kobj, |
293 | struct attribute *attr, char *buff, | |
294 | size_t count) | |
c6c8fea2 | 295 | { |
0ff9b86f | 296 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); |
c6c8fea2 SE |
297 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
298 | unsigned long val; | |
299 | int ret, vis_mode_tmp = -1; | |
3e34819e | 300 | const char *old_mode, *new_mode; |
c6c8fea2 | 301 | |
25a92b13 | 302 | ret = kstrtoul(buff, 10, &val); |
c6c8fea2 SE |
303 | |
304 | if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) || | |
305 | (strncmp(buff, "client", 6) == 0) || | |
306 | (strncmp(buff, "off", 3) == 0)) | |
307 | vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE; | |
308 | ||
309 | if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) || | |
310 | (strncmp(buff, "server", 6) == 0)) | |
311 | vis_mode_tmp = VIS_TYPE_SERVER_SYNC; | |
312 | ||
313 | if (vis_mode_tmp < 0) { | |
314 | if (buff[count - 1] == '\n') | |
315 | buff[count - 1] = '\0'; | |
316 | ||
3e34819e SE |
317 | batadv_info(net_dev, |
318 | "Invalid parameter for 'vis mode' setting received: %s\n", | |
319 | buff); | |
c6c8fea2 SE |
320 | return -EINVAL; |
321 | } | |
322 | ||
323 | if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp) | |
324 | return count; | |
325 | ||
3e34819e SE |
326 | if (atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE) |
327 | old_mode = "client"; | |
328 | else | |
329 | old_mode = "server"; | |
330 | ||
331 | if (vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE) | |
332 | new_mode = "client"; | |
333 | else | |
334 | new_mode = "server"; | |
335 | ||
336 | batadv_info(net_dev, "Changing vis mode from: %s to: %s\n", old_mode, | |
337 | new_mode); | |
c6c8fea2 | 338 | |
95c96174 | 339 | atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp); |
c6c8fea2 SE |
340 | return count; |
341 | } | |
342 | ||
0ff9b86f SE |
343 | static ssize_t batadv_show_bat_algo(struct kobject *kobj, |
344 | struct attribute *attr, char *buff) | |
ea3d2fd1 | 345 | { |
0ff9b86f | 346 | struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj); |
ea3d2fd1 ML |
347 | return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name); |
348 | } | |
349 | ||
0ff9b86f | 350 | static void batadv_post_gw_deselect(struct net_device *net_dev) |
c6c8fea2 SE |
351 | { |
352 | struct bat_priv *bat_priv = netdev_priv(net_dev); | |
7cf06bc6 | 353 | batadv_gw_deselect(bat_priv); |
c6c8fea2 SE |
354 | } |
355 | ||
0ff9b86f SE |
356 | static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr, |
357 | char *buff) | |
c6c8fea2 | 358 | { |
0ff9b86f | 359 | struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj); |
c6c8fea2 SE |
360 | int bytes_written; |
361 | ||
362 | switch (atomic_read(&bat_priv->gw_mode)) { | |
cd646ab1 | 363 | case BATADV_GW_MODE_CLIENT: |
97ea4ba1 SE |
364 | bytes_written = sprintf(buff, "%s\n", |
365 | BATADV_GW_MODE_CLIENT_NAME); | |
c6c8fea2 | 366 | break; |
cd646ab1 | 367 | case BATADV_GW_MODE_SERVER: |
97ea4ba1 SE |
368 | bytes_written = sprintf(buff, "%s\n", |
369 | BATADV_GW_MODE_SERVER_NAME); | |
c6c8fea2 SE |
370 | break; |
371 | default: | |
97ea4ba1 SE |
372 | bytes_written = sprintf(buff, "%s\n", |
373 | BATADV_GW_MODE_OFF_NAME); | |
c6c8fea2 SE |
374 | break; |
375 | } | |
376 | ||
377 | return bytes_written; | |
378 | } | |
379 | ||
0ff9b86f SE |
380 | static ssize_t batadv_store_gw_mode(struct kobject *kobj, |
381 | struct attribute *attr, char *buff, | |
382 | size_t count) | |
c6c8fea2 | 383 | { |
0ff9b86f | 384 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); |
c6c8fea2 SE |
385 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
386 | char *curr_gw_mode_str; | |
387 | int gw_mode_tmp = -1; | |
388 | ||
389 | if (buff[count - 1] == '\n') | |
390 | buff[count - 1] = '\0'; | |
391 | ||
97ea4ba1 SE |
392 | if (strncmp(buff, BATADV_GW_MODE_OFF_NAME, |
393 | strlen(BATADV_GW_MODE_OFF_NAME)) == 0) | |
cd646ab1 | 394 | gw_mode_tmp = BATADV_GW_MODE_OFF; |
c6c8fea2 | 395 | |
97ea4ba1 SE |
396 | if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME, |
397 | strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0) | |
cd646ab1 | 398 | gw_mode_tmp = BATADV_GW_MODE_CLIENT; |
c6c8fea2 | 399 | |
97ea4ba1 SE |
400 | if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME, |
401 | strlen(BATADV_GW_MODE_SERVER_NAME)) == 0) | |
cd646ab1 | 402 | gw_mode_tmp = BATADV_GW_MODE_SERVER; |
c6c8fea2 SE |
403 | |
404 | if (gw_mode_tmp < 0) { | |
3e34819e SE |
405 | batadv_info(net_dev, |
406 | "Invalid parameter for 'gw mode' setting received: %s\n", | |
407 | buff); | |
c6c8fea2 SE |
408 | return -EINVAL; |
409 | } | |
410 | ||
411 | if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp) | |
412 | return count; | |
413 | ||
414 | switch (atomic_read(&bat_priv->gw_mode)) { | |
cd646ab1 | 415 | case BATADV_GW_MODE_CLIENT: |
97ea4ba1 | 416 | curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME; |
c6c8fea2 | 417 | break; |
cd646ab1 | 418 | case BATADV_GW_MODE_SERVER: |
97ea4ba1 | 419 | curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME; |
c6c8fea2 SE |
420 | break; |
421 | default: | |
97ea4ba1 | 422 | curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME; |
c6c8fea2 SE |
423 | break; |
424 | } | |
425 | ||
3e34819e SE |
426 | batadv_info(net_dev, "Changing gw mode from: %s to: %s\n", |
427 | curr_gw_mode_str, buff); | |
c6c8fea2 | 428 | |
7cf06bc6 | 429 | batadv_gw_deselect(bat_priv); |
95c96174 | 430 | atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp); |
c6c8fea2 SE |
431 | return count; |
432 | } | |
433 | ||
0ff9b86f SE |
434 | static ssize_t batadv_show_gw_bwidth(struct kobject *kobj, |
435 | struct attribute *attr, char *buff) | |
c6c8fea2 | 436 | { |
0ff9b86f | 437 | struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj); |
c6c8fea2 SE |
438 | int down, up; |
439 | int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth); | |
440 | ||
84d5e5e0 | 441 | batadv_gw_bandwidth_to_kbit(gw_bandwidth, &down, &up); |
c6c8fea2 SE |
442 | return sprintf(buff, "%i%s/%i%s\n", |
443 | (down > 2048 ? down / 1024 : down), | |
444 | (down > 2048 ? "MBit" : "KBit"), | |
445 | (up > 2048 ? up / 1024 : up), | |
446 | (up > 2048 ? "MBit" : "KBit")); | |
447 | } | |
448 | ||
0ff9b86f SE |
449 | static ssize_t batadv_store_gw_bwidth(struct kobject *kobj, |
450 | struct attribute *attr, char *buff, | |
451 | size_t count) | |
c6c8fea2 | 452 | { |
0ff9b86f | 453 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); |
c6c8fea2 SE |
454 | |
455 | if (buff[count - 1] == '\n') | |
456 | buff[count - 1] = '\0'; | |
457 | ||
84d5e5e0 | 458 | return batadv_gw_bandwidth_set(net_dev, buff, count); |
c6c8fea2 SE |
459 | } |
460 | ||
347c80f0 SE |
461 | BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL); |
462 | BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL); | |
7a5cc242 | 463 | #ifdef CONFIG_BATMAN_ADV_BLA |
347c80f0 | 464 | BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL); |
7a5cc242 | 465 | #endif |
347c80f0 SE |
466 | BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu); |
467 | BATADV_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL); | |
468 | static BATADV_ATTR(vis_mode, S_IRUGO | S_IWUSR, batadv_show_vis_mode, | |
469 | batadv_store_vis_mode); | |
470 | static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL); | |
471 | static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode, | |
472 | batadv_store_gw_mode); | |
42d0b044 SE |
473 | BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * BATADV_JITTER, |
474 | INT_MAX, NULL); | |
475 | BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, BATADV_TQ_MAX_VALUE, | |
347c80f0 | 476 | NULL); |
42d0b044 | 477 | BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, BATADV_TQ_MAX_VALUE, |
347c80f0 SE |
478 | batadv_post_gw_deselect); |
479 | static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth, | |
480 | batadv_store_gw_bwidth); | |
c6c8fea2 | 481 | #ifdef CONFIG_BATMAN_ADV_DEBUG |
347c80f0 | 482 | BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, DBG_ALL, NULL); |
c6c8fea2 SE |
483 | #endif |
484 | ||
0ff9b86f SE |
485 | static struct bat_attribute *batadv_mesh_attrs[] = { |
486 | &batadv_attr_aggregated_ogms, | |
487 | &batadv_attr_bonding, | |
7a5cc242 | 488 | #ifdef CONFIG_BATMAN_ADV_BLA |
0ff9b86f | 489 | &batadv_attr_bridge_loop_avoidance, |
7a5cc242 | 490 | #endif |
0ff9b86f SE |
491 | &batadv_attr_fragmentation, |
492 | &batadv_attr_ap_isolation, | |
493 | &batadv_attr_vis_mode, | |
494 | &batadv_attr_routing_algo, | |
495 | &batadv_attr_gw_mode, | |
496 | &batadv_attr_orig_interval, | |
497 | &batadv_attr_hop_penalty, | |
498 | &batadv_attr_gw_sel_class, | |
499 | &batadv_attr_gw_bandwidth, | |
c6c8fea2 | 500 | #ifdef CONFIG_BATMAN_ADV_DEBUG |
0ff9b86f | 501 | &batadv_attr_log_level, |
c6c8fea2 SE |
502 | #endif |
503 | NULL, | |
504 | }; | |
505 | ||
5853e22c | 506 | int batadv_sysfs_add_meshif(struct net_device *dev) |
c6c8fea2 SE |
507 | { |
508 | struct kobject *batif_kobject = &dev->dev.kobj; | |
509 | struct bat_priv *bat_priv = netdev_priv(dev); | |
510 | struct bat_attribute **bat_attr; | |
511 | int err; | |
512 | ||
036cbfeb | 513 | bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR, |
c6c8fea2 SE |
514 | batif_kobject); |
515 | if (!bat_priv->mesh_obj) { | |
3e34819e | 516 | batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name, |
036cbfeb | 517 | BATADV_SYSFS_IF_MESH_SUBDIR); |
c6c8fea2 SE |
518 | goto out; |
519 | } | |
520 | ||
0ff9b86f | 521 | for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) { |
c6c8fea2 SE |
522 | err = sysfs_create_file(bat_priv->mesh_obj, |
523 | &((*bat_attr)->attr)); | |
524 | if (err) { | |
3e34819e | 525 | batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n", |
036cbfeb | 526 | dev->name, BATADV_SYSFS_IF_MESH_SUBDIR, |
3e34819e | 527 | ((*bat_attr)->attr).name); |
c6c8fea2 SE |
528 | goto rem_attr; |
529 | } | |
530 | } | |
531 | ||
532 | return 0; | |
533 | ||
534 | rem_attr: | |
0ff9b86f | 535 | for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) |
c6c8fea2 SE |
536 | sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr)); |
537 | ||
538 | kobject_put(bat_priv->mesh_obj); | |
539 | bat_priv->mesh_obj = NULL; | |
540 | out: | |
541 | return -ENOMEM; | |
542 | } | |
543 | ||
5853e22c | 544 | void batadv_sysfs_del_meshif(struct net_device *dev) |
c6c8fea2 SE |
545 | { |
546 | struct bat_priv *bat_priv = netdev_priv(dev); | |
547 | struct bat_attribute **bat_attr; | |
548 | ||
0ff9b86f | 549 | for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) |
c6c8fea2 SE |
550 | sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr)); |
551 | ||
552 | kobject_put(bat_priv->mesh_obj); | |
553 | bat_priv->mesh_obj = NULL; | |
554 | } | |
555 | ||
0ff9b86f SE |
556 | static ssize_t batadv_show_mesh_iface(struct kobject *kobj, |
557 | struct attribute *attr, char *buff) | |
c6c8fea2 | 558 | { |
0ff9b86f | 559 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); |
9563877e | 560 | struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev); |
c6c8fea2 | 561 | ssize_t length; |
e9a4f295 | 562 | const char *ifname; |
c6c8fea2 | 563 | |
e6c10f43 | 564 | if (!hard_iface) |
c6c8fea2 SE |
565 | return 0; |
566 | ||
e9a4f295 SE |
567 | if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) |
568 | ifname = "none"; | |
569 | else | |
570 | ifname = hard_iface->soft_iface->name; | |
571 | ||
572 | length = sprintf(buff, "%s\n", ifname); | |
c6c8fea2 | 573 | |
e5d89254 | 574 | batadv_hardif_free_ref(hard_iface); |
c6c8fea2 SE |
575 | |
576 | return length; | |
577 | } | |
578 | ||
0ff9b86f SE |
579 | static ssize_t batadv_store_mesh_iface(struct kobject *kobj, |
580 | struct attribute *attr, char *buff, | |
581 | size_t count) | |
c6c8fea2 | 582 | { |
0ff9b86f | 583 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); |
9563877e | 584 | struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev); |
c6c8fea2 | 585 | int status_tmp = -1; |
ed75ccbe | 586 | int ret = count; |
c6c8fea2 | 587 | |
e6c10f43 | 588 | if (!hard_iface) |
c6c8fea2 SE |
589 | return count; |
590 | ||
591 | if (buff[count - 1] == '\n') | |
592 | buff[count - 1] = '\0'; | |
593 | ||
594 | if (strlen(buff) >= IFNAMSIZ) { | |
86ceb360 SE |
595 | pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n", |
596 | buff); | |
e5d89254 | 597 | batadv_hardif_free_ref(hard_iface); |
c6c8fea2 SE |
598 | return -EINVAL; |
599 | } | |
600 | ||
601 | if (strncmp(buff, "none", 4) == 0) | |
e9a4f295 | 602 | status_tmp = BATADV_IF_NOT_IN_USE; |
c6c8fea2 | 603 | else |
e9a4f295 | 604 | status_tmp = BATADV_IF_I_WANT_YOU; |
c6c8fea2 | 605 | |
e6c10f43 ML |
606 | if (hard_iface->if_status == status_tmp) |
607 | goto out; | |
608 | ||
609 | if ((hard_iface->soft_iface) && | |
610 | (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0)) | |
ed75ccbe | 611 | goto out; |
c6c8fea2 | 612 | |
3a4375a9 SE |
613 | if (!rtnl_trylock()) { |
614 | ret = -ERESTARTSYS; | |
615 | goto out; | |
616 | } | |
617 | ||
e9a4f295 | 618 | if (status_tmp == BATADV_IF_NOT_IN_USE) { |
9563877e | 619 | batadv_hardif_disable_interface(hard_iface); |
3a4375a9 | 620 | goto unlock; |
c6c8fea2 SE |
621 | } |
622 | ||
623 | /* if the interface already is in use */ | |
e9a4f295 | 624 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
9563877e | 625 | batadv_hardif_disable_interface(hard_iface); |
c6c8fea2 | 626 | |
9563877e | 627 | ret = batadv_hardif_enable_interface(hard_iface, buff); |
c6c8fea2 | 628 | |
3a4375a9 SE |
629 | unlock: |
630 | rtnl_unlock(); | |
ed75ccbe | 631 | out: |
e5d89254 | 632 | batadv_hardif_free_ref(hard_iface); |
c6c8fea2 SE |
633 | return ret; |
634 | } | |
635 | ||
0ff9b86f SE |
636 | static ssize_t batadv_show_iface_status(struct kobject *kobj, |
637 | struct attribute *attr, char *buff) | |
c6c8fea2 | 638 | { |
0ff9b86f | 639 | struct net_device *net_dev = batadv_kobj_to_netdev(kobj); |
9563877e | 640 | struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev); |
c6c8fea2 SE |
641 | ssize_t length; |
642 | ||
e6c10f43 | 643 | if (!hard_iface) |
c6c8fea2 SE |
644 | return 0; |
645 | ||
e6c10f43 | 646 | switch (hard_iface->if_status) { |
e9a4f295 | 647 | case BATADV_IF_TO_BE_REMOVED: |
c6c8fea2 SE |
648 | length = sprintf(buff, "disabling\n"); |
649 | break; | |
e9a4f295 | 650 | case BATADV_IF_INACTIVE: |
c6c8fea2 SE |
651 | length = sprintf(buff, "inactive\n"); |
652 | break; | |
e9a4f295 | 653 | case BATADV_IF_ACTIVE: |
c6c8fea2 SE |
654 | length = sprintf(buff, "active\n"); |
655 | break; | |
e9a4f295 | 656 | case BATADV_IF_TO_BE_ACTIVATED: |
c6c8fea2 SE |
657 | length = sprintf(buff, "enabling\n"); |
658 | break; | |
e9a4f295 | 659 | case BATADV_IF_NOT_IN_USE: |
c6c8fea2 SE |
660 | default: |
661 | length = sprintf(buff, "not in use\n"); | |
662 | break; | |
663 | } | |
664 | ||
e5d89254 | 665 | batadv_hardif_free_ref(hard_iface); |
c6c8fea2 SE |
666 | |
667 | return length; | |
668 | } | |
669 | ||
347c80f0 SE |
670 | static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface, |
671 | batadv_store_mesh_iface); | |
672 | static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL); | |
c6c8fea2 | 673 | |
0ff9b86f SE |
674 | static struct bat_attribute *batadv_batman_attrs[] = { |
675 | &batadv_attr_mesh_iface, | |
676 | &batadv_attr_iface_status, | |
c6c8fea2 SE |
677 | NULL, |
678 | }; | |
679 | ||
5853e22c | 680 | int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev) |
c6c8fea2 SE |
681 | { |
682 | struct kobject *hardif_kobject = &dev->dev.kobj; | |
683 | struct bat_attribute **bat_attr; | |
684 | int err; | |
685 | ||
036cbfeb SE |
686 | *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR, |
687 | hardif_kobject); | |
c6c8fea2 SE |
688 | |
689 | if (!*hardif_obj) { | |
3e34819e | 690 | batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name, |
036cbfeb | 691 | BATADV_SYSFS_IF_BAT_SUBDIR); |
c6c8fea2 SE |
692 | goto out; |
693 | } | |
694 | ||
0ff9b86f | 695 | for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) { |
c6c8fea2 SE |
696 | err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr)); |
697 | if (err) { | |
3e34819e | 698 | batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n", |
036cbfeb | 699 | dev->name, BATADV_SYSFS_IF_BAT_SUBDIR, |
3e34819e | 700 | ((*bat_attr)->attr).name); |
c6c8fea2 SE |
701 | goto rem_attr; |
702 | } | |
703 | } | |
704 | ||
705 | return 0; | |
706 | ||
707 | rem_attr: | |
0ff9b86f | 708 | for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) |
c6c8fea2 SE |
709 | sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr)); |
710 | out: | |
711 | return -ENOMEM; | |
712 | } | |
713 | ||
5853e22c | 714 | void batadv_sysfs_del_hardif(struct kobject **hardif_obj) |
c6c8fea2 SE |
715 | { |
716 | kobject_put(*hardif_obj); | |
717 | *hardif_obj = NULL; | |
718 | } | |
c6bda689 | 719 | |
5853e22c SE |
720 | int batadv_throw_uevent(struct bat_priv *bat_priv, enum uev_type type, |
721 | enum uev_action action, const char *data) | |
c6bda689 | 722 | { |
5346c35e | 723 | int ret = -ENOMEM; |
c6bda689 AQ |
724 | struct hard_iface *primary_if = NULL; |
725 | struct kobject *bat_kobj; | |
726 | char *uevent_env[4] = { NULL, NULL, NULL, NULL }; | |
727 | ||
e5d89254 | 728 | primary_if = batadv_primary_if_get_selected(bat_priv); |
c6bda689 AQ |
729 | if (!primary_if) |
730 | goto out; | |
731 | ||
732 | bat_kobj = &primary_if->soft_iface->dev.kobj; | |
733 | ||
347c80f0 | 734 | uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) + |
0ff9b86f | 735 | strlen(batadv_uev_type_str[type]) + 1, |
c6bda689 AQ |
736 | GFP_ATOMIC); |
737 | if (!uevent_env[0]) | |
738 | goto out; | |
739 | ||
347c80f0 SE |
740 | sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR, |
741 | batadv_uev_type_str[type]); | |
c6bda689 | 742 | |
347c80f0 | 743 | uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) + |
0ff9b86f | 744 | strlen(batadv_uev_action_str[action]) + 1, |
c6bda689 AQ |
745 | GFP_ATOMIC); |
746 | if (!uevent_env[1]) | |
747 | goto out; | |
748 | ||
347c80f0 | 749 | sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR, |
0ff9b86f | 750 | batadv_uev_action_str[action]); |
c6bda689 AQ |
751 | |
752 | /* If the event is DEL, ignore the data field */ | |
753 | if (action != UEV_DEL) { | |
347c80f0 | 754 | uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) + |
c6bda689 AQ |
755 | strlen(data) + 1, GFP_ATOMIC); |
756 | if (!uevent_env[2]) | |
757 | goto out; | |
758 | ||
347c80f0 | 759 | sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data); |
c6bda689 AQ |
760 | } |
761 | ||
762 | ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env); | |
763 | out: | |
764 | kfree(uevent_env[0]); | |
765 | kfree(uevent_env[1]); | |
766 | kfree(uevent_env[2]); | |
767 | ||
768 | if (primary_if) | |
e5d89254 | 769 | batadv_hardif_free_ref(primary_if); |
c6bda689 AQ |
770 | |
771 | if (ret) | |
1eda58bf SE |
772 | batadv_dbg(DBG_BATMAN, bat_priv, |
773 | "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n", | |
0ff9b86f SE |
774 | batadv_uev_type_str[type], |
775 | batadv_uev_action_str[action], | |
1eda58bf | 776 | (action == UEV_DEL ? "NULL" : data), ret); |
c6bda689 AQ |
777 | return ret; |
778 | } |