hard-interface.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /* Copyright (C) 2007-2015 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "hard-interface.h"
  18. #include "main.h"
  19. #include <linux/byteorder/generic.h>
  20. #include <linux/errno.h>
  21. #include <linux/fs.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/if.h>
  25. #include <linux/kernel.h>
  26. #include <linux/list.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/printk.h>
  29. #include <linux/rculist.h>
  30. #include <linux/rtnetlink.h>
  31. #include <linux/slab.h>
  32. #include <linux/workqueue.h>
  33. #include <net/net_namespace.h>
  34. #include "bridge_loop_avoidance.h"
  35. #include "debugfs.h"
  36. #include "distributed-arp-table.h"
  37. #include "gateway_client.h"
  38. #include "originator.h"
  39. #include "packet.h"
  40. #include "send.h"
  41. #include "soft-interface.h"
  42. #include "sysfs.h"
  43. #include "translation-table.h"
  44. void batadv_hardif_free_rcu(struct rcu_head *rcu)
  45. {
  46. struct batadv_hard_iface *hard_iface;
  47. hard_iface = container_of(rcu, struct batadv_hard_iface, rcu);
  48. dev_put(hard_iface->net_dev);
  49. kfree(hard_iface);
  50. }
  51. struct batadv_hard_iface *
  52. batadv_hardif_get_by_netdev(const struct net_device *net_dev)
  53. {
  54. struct batadv_hard_iface *hard_iface;
  55. rcu_read_lock();
  56. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  57. if (hard_iface->net_dev == net_dev &&
  58. atomic_inc_not_zero(&hard_iface->refcount))
  59. goto out;
  60. }
  61. hard_iface = NULL;
  62. out:
  63. rcu_read_unlock();
  64. return hard_iface;
  65. }
  66. /**
  67. * batadv_is_on_batman_iface - check if a device is a batman iface descendant
  68. * @net_dev: the device to check
  69. *
  70. * If the user creates any virtual device on top of a batman-adv interface, it
  71. * is important to prevent this new interface to be used to create a new mesh
  72. * network (this behaviour would lead to a batman-over-batman configuration).
  73. * This function recursively checks all the fathers of the device passed as
  74. * argument looking for a batman-adv soft interface.
  75. *
  76. * Returns true if the device is descendant of a batman-adv mesh interface (or
  77. * if it is a batman-adv interface itself), false otherwise
  78. */
  79. static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
  80. {
  81. struct net_device *parent_dev;
  82. bool ret;
  83. /* check if this is a batman-adv mesh interface */
  84. if (batadv_softif_is_valid(net_dev))
  85. return true;
  86. /* no more parents..stop recursion */
  87. if (dev_get_iflink(net_dev) == 0 ||
  88. dev_get_iflink(net_dev) == net_dev->ifindex)
  89. return false;
  90. /* recurse over the parent device */
  91. parent_dev = __dev_get_by_index(&init_net, dev_get_iflink(net_dev));
  92. /* if we got a NULL parent_dev there is something broken.. */
  93. if (!parent_dev) {
  94. pr_err("Cannot find parent device\n");
  95. return false;
  96. }
  97. ret = batadv_is_on_batman_iface(parent_dev);
  98. return ret;
  99. }
  100. static int batadv_is_valid_iface(const struct net_device *net_dev)
  101. {
  102. if (net_dev->flags & IFF_LOOPBACK)
  103. return 0;
  104. if (net_dev->type != ARPHRD_ETHER)
  105. return 0;
  106. if (net_dev->addr_len != ETH_ALEN)
  107. return 0;
  108. /* no batman over batman */
  109. if (batadv_is_on_batman_iface(net_dev))
  110. return 0;
  111. return 1;
  112. }
  113. /**
  114. * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
  115. * interface
  116. * @net_device: the device to check
  117. *
  118. * Returns true if the net device is a 802.11 wireless device, false otherwise.
  119. */
  120. bool batadv_is_wifi_netdev(struct net_device *net_device)
  121. {
  122. if (!net_device)
  123. return false;
  124. #ifdef CONFIG_WIRELESS_EXT
  125. /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
  126. * check for wireless_handlers != NULL
  127. */
  128. if (net_device->wireless_handlers)
  129. return true;
  130. #endif
  131. /* cfg80211 drivers have to set ieee80211_ptr */
  132. if (net_device->ieee80211_ptr)
  133. return true;
  134. return false;
  135. }
  136. static struct batadv_hard_iface *
  137. batadv_hardif_get_active(const struct net_device *soft_iface)
  138. {
  139. struct batadv_hard_iface *hard_iface;
  140. rcu_read_lock();
  141. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  142. if (hard_iface->soft_iface != soft_iface)
  143. continue;
  144. if (hard_iface->if_status == BATADV_IF_ACTIVE &&
  145. atomic_inc_not_zero(&hard_iface->refcount))
  146. goto out;
  147. }
  148. hard_iface = NULL;
  149. out:
  150. rcu_read_unlock();
  151. return hard_iface;
  152. }
  153. static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
  154. struct batadv_hard_iface *oldif)
  155. {
  156. struct batadv_hard_iface *primary_if;
  157. primary_if = batadv_primary_if_get_selected(bat_priv);
  158. if (!primary_if)
  159. goto out;
  160. batadv_dat_init_own_addr(bat_priv, primary_if);
  161. batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
  162. out:
  163. if (primary_if)
  164. batadv_hardif_free_ref(primary_if);
  165. }
  166. static void batadv_primary_if_select(struct batadv_priv *bat_priv,
  167. struct batadv_hard_iface *new_hard_iface)
  168. {
  169. struct batadv_hard_iface *curr_hard_iface;
  170. ASSERT_RTNL();
  171. if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
  172. new_hard_iface = NULL;
  173. curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
  174. rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
  175. if (!new_hard_iface)
  176. goto out;
  177. bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
  178. batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
  179. out:
  180. if (curr_hard_iface)
  181. batadv_hardif_free_ref(curr_hard_iface);
  182. }
  183. static bool
  184. batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
  185. {
  186. if (hard_iface->net_dev->flags & IFF_UP)
  187. return true;
  188. return false;
  189. }
  190. static void batadv_check_known_mac_addr(const struct net_device *net_dev)
  191. {
  192. const struct batadv_hard_iface *hard_iface;
  193. rcu_read_lock();
  194. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  195. if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
  196. (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
  197. continue;
  198. if (hard_iface->net_dev == net_dev)
  199. continue;
  200. if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
  201. net_dev->dev_addr))
  202. continue;
  203. pr_warn("The newly added mac address (%pM) already exists on: %s\n",
  204. net_dev->dev_addr, hard_iface->net_dev->name);
  205. pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
  206. }
  207. rcu_read_unlock();
  208. }
  209. /**
  210. * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
  211. * @soft_iface: netdev struct of the mesh interface
  212. */
  213. static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
  214. {
  215. const struct batadv_hard_iface *hard_iface;
  216. unsigned short lower_header_len = ETH_HLEN;
  217. unsigned short lower_headroom = 0;
  218. unsigned short lower_tailroom = 0;
  219. unsigned short needed_headroom;
  220. rcu_read_lock();
  221. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  222. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  223. continue;
  224. if (hard_iface->soft_iface != soft_iface)
  225. continue;
  226. lower_header_len = max_t(unsigned short, lower_header_len,
  227. hard_iface->net_dev->hard_header_len);
  228. lower_headroom = max_t(unsigned short, lower_headroom,
  229. hard_iface->net_dev->needed_headroom);
  230. lower_tailroom = max_t(unsigned short, lower_tailroom,
  231. hard_iface->net_dev->needed_tailroom);
  232. }
  233. rcu_read_unlock();
  234. needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
  235. needed_headroom += batadv_max_header_len();
  236. soft_iface->needed_headroom = needed_headroom;
  237. soft_iface->needed_tailroom = lower_tailroom;
  238. }
  239. int batadv_hardif_min_mtu(struct net_device *soft_iface)
  240. {
  241. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  242. const struct batadv_hard_iface *hard_iface;
  243. int min_mtu = INT_MAX;
  244. rcu_read_lock();
  245. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  246. if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
  247. (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
  248. continue;
  249. if (hard_iface->soft_iface != soft_iface)
  250. continue;
  251. min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
  252. }
  253. rcu_read_unlock();
  254. if (atomic_read(&bat_priv->fragmentation) == 0)
  255. goto out;
  256. /* with fragmentation enabled the maximum size of internally generated
  257. * packets such as translation table exchanges or tvlv containers, etc
  258. * has to be calculated
  259. */
  260. min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
  261. min_mtu -= sizeof(struct batadv_frag_packet);
  262. min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
  263. out:
  264. /* report to the other components the maximum amount of bytes that
  265. * batman-adv can send over the wire (without considering the payload
  266. * overhead). For example, this value is used by TT to compute the
  267. * maximum local table table size
  268. */
  269. atomic_set(&bat_priv->packet_size_max, min_mtu);
  270. /* the real soft-interface MTU is computed by removing the payload
  271. * overhead from the maximum amount of bytes that was just computed.
  272. *
  273. * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
  274. */
  275. return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
  276. }
  277. /* adjusts the MTU if a new interface with a smaller MTU appeared. */
  278. void batadv_update_min_mtu(struct net_device *soft_iface)
  279. {
  280. soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
  281. /* Check if the local translate table should be cleaned up to match a
  282. * new (and smaller) MTU.
  283. */
  284. batadv_tt_local_resize_to_mtu(soft_iface);
  285. }
  286. static void
  287. batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
  288. {
  289. struct batadv_priv *bat_priv;
  290. struct batadv_hard_iface *primary_if = NULL;
  291. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  292. goto out;
  293. bat_priv = netdev_priv(hard_iface->soft_iface);
  294. bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
  295. hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
  296. /* the first active interface becomes our primary interface or
  297. * the next active interface after the old primary interface was removed
  298. */
  299. primary_if = batadv_primary_if_get_selected(bat_priv);
  300. if (!primary_if)
  301. batadv_primary_if_select(bat_priv, hard_iface);
  302. batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
  303. hard_iface->net_dev->name);
  304. batadv_update_min_mtu(hard_iface->soft_iface);
  305. out:
  306. if (primary_if)
  307. batadv_hardif_free_ref(primary_if);
  308. }
  309. static void
  310. batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
  311. {
  312. if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
  313. (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
  314. return;
  315. hard_iface->if_status = BATADV_IF_INACTIVE;
  316. batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
  317. hard_iface->net_dev->name);
  318. batadv_update_min_mtu(hard_iface->soft_iface);
  319. }
  320. /**
  321. * batadv_master_del_slave - remove hard_iface from the current master interface
  322. * @slave: the interface enslaved in another master
  323. * @master: the master from which slave has to be removed
  324. *
  325. * Invoke ndo_del_slave on master passing slave as argument. In this way slave
  326. * is free'd and master can correctly change its internal state.
  327. * Return 0 on success, a negative value representing the error otherwise
  328. */
  329. static int batadv_master_del_slave(struct batadv_hard_iface *slave,
  330. struct net_device *master)
  331. {
  332. int ret;
  333. if (!master)
  334. return 0;
  335. ret = -EBUSY;
  336. if (master->netdev_ops->ndo_del_slave)
  337. ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
  338. return ret;
  339. }
  340. int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
  341. const char *iface_name)
  342. {
  343. struct batadv_priv *bat_priv;
  344. struct net_device *soft_iface, *master;
  345. __be16 ethertype = htons(ETH_P_BATMAN);
  346. int max_header_len = batadv_max_header_len();
  347. int ret;
  348. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  349. goto out;
  350. if (!atomic_inc_not_zero(&hard_iface->refcount))
  351. goto out;
  352. soft_iface = dev_get_by_name(&init_net, iface_name);
  353. if (!soft_iface) {
  354. soft_iface = batadv_softif_create(iface_name);
  355. if (!soft_iface) {
  356. ret = -ENOMEM;
  357. goto err;
  358. }
  359. /* dev_get_by_name() increases the reference counter for us */
  360. dev_hold(soft_iface);
  361. }
  362. if (!batadv_softif_is_valid(soft_iface)) {
  363. pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
  364. soft_iface->name);
  365. ret = -EINVAL;
  366. goto err_dev;
  367. }
  368. /* check if the interface is enslaved in another virtual one and
  369. * in that case unlink it first
  370. */
  371. master = netdev_master_upper_dev_get(hard_iface->net_dev);
  372. ret = batadv_master_del_slave(hard_iface, master);
  373. if (ret)
  374. goto err_dev;
  375. hard_iface->soft_iface = soft_iface;
  376. bat_priv = netdev_priv(hard_iface->soft_iface);
  377. ret = netdev_master_upper_dev_link(hard_iface->net_dev, soft_iface);
  378. if (ret)
  379. goto err_dev;
  380. ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
  381. if (ret < 0)
  382. goto err_upper;
  383. hard_iface->if_num = bat_priv->num_ifaces;
  384. bat_priv->num_ifaces++;
  385. hard_iface->if_status = BATADV_IF_INACTIVE;
  386. ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
  387. if (ret < 0) {
  388. bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
  389. bat_priv->num_ifaces--;
  390. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  391. goto err_upper;
  392. }
  393. hard_iface->batman_adv_ptype.type = ethertype;
  394. hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
  395. hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
  396. dev_add_pack(&hard_iface->batman_adv_ptype);
  397. batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
  398. hard_iface->net_dev->name);
  399. if (atomic_read(&bat_priv->fragmentation) &&
  400. hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
  401. batadv_info(hard_iface->soft_iface,
  402. "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. Packets going over this interface will be fragmented on layer2 which could impact the performance. Setting the MTU to %i would solve the problem.\n",
  403. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  404. ETH_DATA_LEN + max_header_len);
  405. if (!atomic_read(&bat_priv->fragmentation) &&
  406. hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
  407. batadv_info(hard_iface->soft_iface,
  408. "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. If you experience problems getting traffic through try increasing the MTU to %i.\n",
  409. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  410. ETH_DATA_LEN + max_header_len);
  411. if (batadv_hardif_is_iface_up(hard_iface))
  412. batadv_hardif_activate_interface(hard_iface);
  413. else
  414. batadv_err(hard_iface->soft_iface,
  415. "Not using interface %s (retrying later): interface not active\n",
  416. hard_iface->net_dev->name);
  417. batadv_hardif_recalc_extra_skbroom(soft_iface);
  418. /* begin scheduling originator messages on that interface */
  419. batadv_schedule_bat_ogm(hard_iface);
  420. out:
  421. return 0;
  422. err_upper:
  423. netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
  424. err_dev:
  425. hard_iface->soft_iface = NULL;
  426. dev_put(soft_iface);
  427. err:
  428. batadv_hardif_free_ref(hard_iface);
  429. return ret;
  430. }
  431. void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
  432. enum batadv_hard_if_cleanup autodel)
  433. {
  434. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  435. struct batadv_hard_iface *primary_if = NULL;
  436. if (hard_iface->if_status == BATADV_IF_ACTIVE)
  437. batadv_hardif_deactivate_interface(hard_iface);
  438. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  439. goto out;
  440. batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
  441. hard_iface->net_dev->name);
  442. dev_remove_pack(&hard_iface->batman_adv_ptype);
  443. bat_priv->num_ifaces--;
  444. batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
  445. primary_if = batadv_primary_if_get_selected(bat_priv);
  446. if (hard_iface == primary_if) {
  447. struct batadv_hard_iface *new_if;
  448. new_if = batadv_hardif_get_active(hard_iface->soft_iface);
  449. batadv_primary_if_select(bat_priv, new_if);
  450. if (new_if)
  451. batadv_hardif_free_ref(new_if);
  452. }
  453. bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
  454. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  455. /* delete all references to this hard_iface */
  456. batadv_purge_orig_ref(bat_priv);
  457. batadv_purge_outstanding_packets(bat_priv, hard_iface);
  458. dev_put(hard_iface->soft_iface);
  459. netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
  460. batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
  461. /* nobody uses this interface anymore */
  462. if (!bat_priv->num_ifaces) {
  463. batadv_gw_check_client_stop(bat_priv);
  464. if (autodel == BATADV_IF_CLEANUP_AUTO)
  465. batadv_softif_destroy_sysfs(hard_iface->soft_iface);
  466. }
  467. hard_iface->soft_iface = NULL;
  468. batadv_hardif_free_ref(hard_iface);
  469. out:
  470. if (primary_if)
  471. batadv_hardif_free_ref(primary_if);
  472. }
  473. /**
  474. * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
  475. * @work: work queue item
  476. *
  477. * Free the parts of the hard interface which can not be removed under
  478. * rtnl lock (to prevent deadlock situations).
  479. */
  480. static void batadv_hardif_remove_interface_finish(struct work_struct *work)
  481. {
  482. struct batadv_hard_iface *hard_iface;
  483. hard_iface = container_of(work, struct batadv_hard_iface,
  484. cleanup_work);
  485. batadv_debugfs_del_hardif(hard_iface);
  486. batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
  487. batadv_hardif_free_ref(hard_iface);
  488. }
  489. static struct batadv_hard_iface *
  490. batadv_hardif_add_interface(struct net_device *net_dev)
  491. {
  492. struct batadv_hard_iface *hard_iface;
  493. int ret;
  494. ASSERT_RTNL();
  495. ret = batadv_is_valid_iface(net_dev);
  496. if (ret != 1)
  497. goto out;
  498. dev_hold(net_dev);
  499. hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
  500. if (!hard_iface)
  501. goto release_dev;
  502. ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
  503. if (ret)
  504. goto free_if;
  505. hard_iface->if_num = -1;
  506. hard_iface->net_dev = net_dev;
  507. hard_iface->soft_iface = NULL;
  508. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  509. ret = batadv_debugfs_add_hardif(hard_iface);
  510. if (ret)
  511. goto free_sysfs;
  512. INIT_LIST_HEAD(&hard_iface->list);
  513. INIT_WORK(&hard_iface->cleanup_work,
  514. batadv_hardif_remove_interface_finish);
  515. hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
  516. if (batadv_is_wifi_netdev(net_dev))
  517. hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
  518. /* extra reference for return */
  519. atomic_set(&hard_iface->refcount, 2);
  520. batadv_check_known_mac_addr(hard_iface->net_dev);
  521. list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
  522. return hard_iface;
  523. free_sysfs:
  524. batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
  525. free_if:
  526. kfree(hard_iface);
  527. release_dev:
  528. dev_put(net_dev);
  529. out:
  530. return NULL;
  531. }
  532. static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
  533. {
  534. ASSERT_RTNL();
  535. /* first deactivate interface */
  536. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  537. batadv_hardif_disable_interface(hard_iface,
  538. BATADV_IF_CLEANUP_AUTO);
  539. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  540. return;
  541. hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
  542. queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
  543. }
  544. void batadv_hardif_remove_interfaces(void)
  545. {
  546. struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
  547. rtnl_lock();
  548. list_for_each_entry_safe(hard_iface, hard_iface_tmp,
  549. &batadv_hardif_list, list) {
  550. list_del_rcu(&hard_iface->list);
  551. batadv_hardif_remove_interface(hard_iface);
  552. }
  553. rtnl_unlock();
  554. }
  555. static int batadv_hard_if_event(struct notifier_block *this,
  556. unsigned long event, void *ptr)
  557. {
  558. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  559. struct batadv_hard_iface *hard_iface;
  560. struct batadv_hard_iface *primary_if = NULL;
  561. struct batadv_priv *bat_priv;
  562. if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
  563. batadv_sysfs_add_meshif(net_dev);
  564. bat_priv = netdev_priv(net_dev);
  565. batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
  566. return NOTIFY_DONE;
  567. }
  568. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  569. if (!hard_iface && event == NETDEV_REGISTER)
  570. hard_iface = batadv_hardif_add_interface(net_dev);
  571. if (!hard_iface)
  572. goto out;
  573. switch (event) {
  574. case NETDEV_UP:
  575. batadv_hardif_activate_interface(hard_iface);
  576. break;
  577. case NETDEV_GOING_DOWN:
  578. case NETDEV_DOWN:
  579. batadv_hardif_deactivate_interface(hard_iface);
  580. break;
  581. case NETDEV_UNREGISTER:
  582. list_del_rcu(&hard_iface->list);
  583. batadv_hardif_remove_interface(hard_iface);
  584. break;
  585. case NETDEV_CHANGEMTU:
  586. if (hard_iface->soft_iface)
  587. batadv_update_min_mtu(hard_iface->soft_iface);
  588. break;
  589. case NETDEV_CHANGEADDR:
  590. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  591. goto hardif_put;
  592. batadv_check_known_mac_addr(hard_iface->net_dev);
  593. bat_priv = netdev_priv(hard_iface->soft_iface);
  594. bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
  595. primary_if = batadv_primary_if_get_selected(bat_priv);
  596. if (!primary_if)
  597. goto hardif_put;
  598. if (hard_iface == primary_if)
  599. batadv_primary_if_update_addr(bat_priv, NULL);
  600. break;
  601. default:
  602. break;
  603. }
  604. hardif_put:
  605. batadv_hardif_free_ref(hard_iface);
  606. out:
  607. if (primary_if)
  608. batadv_hardif_free_ref(primary_if);
  609. return NOTIFY_DONE;
  610. }
  611. struct notifier_block batadv_hard_if_notifier = {
  612. .notifier_call = batadv_hard_if_event,
  613. };