hard-interface.c 19 KB

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