hard-interface.c 19 KB

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