hard-interface.c 19 KB

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