hard-interface.c 19 KB

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