hard-interface.c 20 KB

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