hard-interface.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. /**
  209. * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
  210. * @soft_iface: netdev struct of the mesh interface
  211. */
  212. static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
  213. {
  214. const struct batadv_hard_iface *hard_iface;
  215. unsigned short lower_header_len = ETH_HLEN;
  216. unsigned short lower_headroom = 0;
  217. unsigned short lower_tailroom = 0;
  218. unsigned short needed_headroom;
  219. rcu_read_lock();
  220. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  221. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  222. continue;
  223. if (hard_iface->soft_iface != soft_iface)
  224. continue;
  225. lower_header_len = max_t(unsigned short, lower_header_len,
  226. hard_iface->net_dev->hard_header_len);
  227. lower_headroom = max_t(unsigned short, lower_headroom,
  228. hard_iface->net_dev->needed_headroom);
  229. lower_tailroom = max_t(unsigned short, lower_tailroom,
  230. hard_iface->net_dev->needed_tailroom);
  231. }
  232. rcu_read_unlock();
  233. needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
  234. needed_headroom += batadv_max_header_len();
  235. soft_iface->needed_headroom = needed_headroom;
  236. soft_iface->needed_tailroom = lower_tailroom;
  237. }
  238. int batadv_hardif_min_mtu(struct net_device *soft_iface)
  239. {
  240. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  241. const struct batadv_hard_iface *hard_iface;
  242. int min_mtu = INT_MAX;
  243. rcu_read_lock();
  244. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  245. if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
  246. (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
  247. continue;
  248. if (hard_iface->soft_iface != soft_iface)
  249. continue;
  250. min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
  251. }
  252. rcu_read_unlock();
  253. if (atomic_read(&bat_priv->fragmentation) == 0)
  254. goto out;
  255. /* with fragmentation enabled the maximum size of internally generated
  256. * packets such as translation table exchanges or tvlv containers, etc
  257. * has to be calculated
  258. */
  259. min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
  260. min_mtu -= sizeof(struct batadv_frag_packet);
  261. min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
  262. out:
  263. /* report to the other components the maximum amount of bytes that
  264. * batman-adv can send over the wire (without considering the payload
  265. * overhead). For example, this value is used by TT to compute the
  266. * maximum local table table size
  267. */
  268. atomic_set(&bat_priv->packet_size_max, min_mtu);
  269. /* the real soft-interface MTU is computed by removing the payload
  270. * overhead from the maximum amount of bytes that was just computed.
  271. *
  272. * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
  273. */
  274. return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
  275. }
  276. /* adjusts the MTU if a new interface with a smaller MTU appeared. */
  277. void batadv_update_min_mtu(struct net_device *soft_iface)
  278. {
  279. soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
  280. /* Check if the local translate table should be cleaned up to match a
  281. * new (and smaller) MTU.
  282. */
  283. batadv_tt_local_resize_to_mtu(soft_iface);
  284. }
  285. static void
  286. batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
  287. {
  288. struct batadv_priv *bat_priv;
  289. struct batadv_hard_iface *primary_if = NULL;
  290. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  291. goto out;
  292. bat_priv = netdev_priv(hard_iface->soft_iface);
  293. bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
  294. hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
  295. /* the first active interface becomes our primary interface or
  296. * the next active interface after the old primary interface was removed
  297. */
  298. primary_if = batadv_primary_if_get_selected(bat_priv);
  299. if (!primary_if)
  300. batadv_primary_if_select(bat_priv, hard_iface);
  301. batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
  302. hard_iface->net_dev->name);
  303. batadv_update_min_mtu(hard_iface->soft_iface);
  304. out:
  305. if (primary_if)
  306. batadv_hardif_free_ref(primary_if);
  307. }
  308. static void
  309. batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
  310. {
  311. if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
  312. (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
  313. return;
  314. hard_iface->if_status = BATADV_IF_INACTIVE;
  315. batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
  316. hard_iface->net_dev->name);
  317. batadv_update_min_mtu(hard_iface->soft_iface);
  318. }
  319. /**
  320. * batadv_master_del_slave - remove hard_iface from the current master interface
  321. * @slave: the interface enslaved in another master
  322. * @master: the master from which slave has to be removed
  323. *
  324. * Invoke ndo_del_slave on master passing slave as argument. In this way slave
  325. * is free'd and master can correctly change its internal state.
  326. * Return 0 on success, a negative value representing the error otherwise
  327. */
  328. static int batadv_master_del_slave(struct batadv_hard_iface *slave,
  329. struct net_device *master)
  330. {
  331. int ret;
  332. if (!master)
  333. return 0;
  334. ret = -EBUSY;
  335. if (master->netdev_ops->ndo_del_slave)
  336. ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
  337. return ret;
  338. }
  339. int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
  340. const char *iface_name)
  341. {
  342. struct batadv_priv *bat_priv;
  343. struct net_device *soft_iface, *master;
  344. __be16 ethertype = htons(ETH_P_BATMAN);
  345. int max_header_len = batadv_max_header_len();
  346. int ret;
  347. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  348. goto out;
  349. if (!atomic_inc_not_zero(&hard_iface->refcount))
  350. goto out;
  351. soft_iface = dev_get_by_name(&init_net, iface_name);
  352. if (!soft_iface) {
  353. soft_iface = batadv_softif_create(iface_name);
  354. if (!soft_iface) {
  355. ret = -ENOMEM;
  356. goto err;
  357. }
  358. /* dev_get_by_name() increases the reference counter for us */
  359. dev_hold(soft_iface);
  360. }
  361. if (!batadv_softif_is_valid(soft_iface)) {
  362. pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
  363. soft_iface->name);
  364. ret = -EINVAL;
  365. goto err_dev;
  366. }
  367. /* check if the interface is enslaved in another virtual one and
  368. * in that case unlink it first
  369. */
  370. master = netdev_master_upper_dev_get(hard_iface->net_dev);
  371. ret = batadv_master_del_slave(hard_iface, master);
  372. if (ret)
  373. goto err_dev;
  374. hard_iface->soft_iface = soft_iface;
  375. bat_priv = netdev_priv(hard_iface->soft_iface);
  376. ret = netdev_master_upper_dev_link(hard_iface->net_dev, soft_iface);
  377. if (ret)
  378. goto err_dev;
  379. ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
  380. if (ret < 0)
  381. goto err_upper;
  382. hard_iface->if_num = bat_priv->num_ifaces;
  383. bat_priv->num_ifaces++;
  384. hard_iface->if_status = BATADV_IF_INACTIVE;
  385. ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
  386. if (ret < 0) {
  387. bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
  388. bat_priv->num_ifaces--;
  389. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  390. goto err_upper;
  391. }
  392. hard_iface->batman_adv_ptype.type = ethertype;
  393. hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
  394. hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
  395. dev_add_pack(&hard_iface->batman_adv_ptype);
  396. batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
  397. hard_iface->net_dev->name);
  398. if (atomic_read(&bat_priv->fragmentation) &&
  399. hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
  400. batadv_info(hard_iface->soft_iface,
  401. "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",
  402. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  403. ETH_DATA_LEN + max_header_len);
  404. if (!atomic_read(&bat_priv->fragmentation) &&
  405. hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
  406. batadv_info(hard_iface->soft_iface,
  407. "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",
  408. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  409. ETH_DATA_LEN + max_header_len);
  410. if (batadv_hardif_is_iface_up(hard_iface))
  411. batadv_hardif_activate_interface(hard_iface);
  412. else
  413. batadv_err(hard_iface->soft_iface,
  414. "Not using interface %s (retrying later): interface not active\n",
  415. hard_iface->net_dev->name);
  416. batadv_hardif_recalc_extra_skbroom(soft_iface);
  417. /* begin scheduling originator messages on that interface */
  418. batadv_schedule_bat_ogm(hard_iface);
  419. out:
  420. return 0;
  421. err_upper:
  422. netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
  423. err_dev:
  424. hard_iface->soft_iface = NULL;
  425. dev_put(soft_iface);
  426. err:
  427. batadv_hardif_free_ref(hard_iface);
  428. return ret;
  429. }
  430. void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
  431. enum batadv_hard_if_cleanup autodel)
  432. {
  433. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  434. struct batadv_hard_iface *primary_if = NULL;
  435. if (hard_iface->if_status == BATADV_IF_ACTIVE)
  436. batadv_hardif_deactivate_interface(hard_iface);
  437. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  438. goto out;
  439. batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
  440. hard_iface->net_dev->name);
  441. dev_remove_pack(&hard_iface->batman_adv_ptype);
  442. bat_priv->num_ifaces--;
  443. batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
  444. primary_if = batadv_primary_if_get_selected(bat_priv);
  445. if (hard_iface == primary_if) {
  446. struct batadv_hard_iface *new_if;
  447. new_if = batadv_hardif_get_active(hard_iface->soft_iface);
  448. batadv_primary_if_select(bat_priv, new_if);
  449. if (new_if)
  450. batadv_hardif_free_ref(new_if);
  451. }
  452. bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
  453. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  454. /* delete all references to this hard_iface */
  455. batadv_purge_orig_ref(bat_priv);
  456. batadv_purge_outstanding_packets(bat_priv, hard_iface);
  457. dev_put(hard_iface->soft_iface);
  458. netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
  459. batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
  460. /* nobody uses this interface anymore */
  461. if (!bat_priv->num_ifaces) {
  462. batadv_gw_check_client_stop(bat_priv);
  463. if (autodel == BATADV_IF_CLEANUP_AUTO)
  464. batadv_softif_destroy_sysfs(hard_iface->soft_iface);
  465. }
  466. hard_iface->soft_iface = NULL;
  467. batadv_hardif_free_ref(hard_iface);
  468. out:
  469. if (primary_if)
  470. batadv_hardif_free_ref(primary_if);
  471. }
  472. /**
  473. * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
  474. * @work: work queue item
  475. *
  476. * Free the parts of the hard interface which can not be removed under
  477. * rtnl lock (to prevent deadlock situations).
  478. */
  479. static void batadv_hardif_remove_interface_finish(struct work_struct *work)
  480. {
  481. struct batadv_hard_iface *hard_iface;
  482. hard_iface = container_of(work, struct batadv_hard_iface,
  483. cleanup_work);
  484. batadv_debugfs_del_hardif(hard_iface);
  485. batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
  486. batadv_hardif_free_ref(hard_iface);
  487. }
  488. static struct batadv_hard_iface *
  489. batadv_hardif_add_interface(struct net_device *net_dev)
  490. {
  491. struct batadv_hard_iface *hard_iface;
  492. int ret;
  493. ASSERT_RTNL();
  494. ret = batadv_is_valid_iface(net_dev);
  495. if (ret != 1)
  496. goto out;
  497. dev_hold(net_dev);
  498. hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
  499. if (!hard_iface)
  500. goto release_dev;
  501. ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
  502. if (ret)
  503. goto free_if;
  504. hard_iface->if_num = -1;
  505. hard_iface->net_dev = net_dev;
  506. hard_iface->soft_iface = NULL;
  507. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  508. ret = batadv_debugfs_add_hardif(hard_iface);
  509. if (ret)
  510. goto free_sysfs;
  511. INIT_LIST_HEAD(&hard_iface->list);
  512. INIT_WORK(&hard_iface->cleanup_work,
  513. batadv_hardif_remove_interface_finish);
  514. hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
  515. if (batadv_is_wifi_netdev(net_dev))
  516. hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
  517. /* extra reference for return */
  518. atomic_set(&hard_iface->refcount, 2);
  519. batadv_check_known_mac_addr(hard_iface->net_dev);
  520. list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
  521. return hard_iface;
  522. free_sysfs:
  523. batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
  524. free_if:
  525. kfree(hard_iface);
  526. release_dev:
  527. dev_put(net_dev);
  528. out:
  529. return NULL;
  530. }
  531. static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
  532. {
  533. ASSERT_RTNL();
  534. /* first deactivate interface */
  535. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  536. batadv_hardif_disable_interface(hard_iface,
  537. BATADV_IF_CLEANUP_AUTO);
  538. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  539. return;
  540. hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
  541. queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
  542. }
  543. void batadv_hardif_remove_interfaces(void)
  544. {
  545. struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
  546. rtnl_lock();
  547. list_for_each_entry_safe(hard_iface, hard_iface_tmp,
  548. &batadv_hardif_list, list) {
  549. list_del_rcu(&hard_iface->list);
  550. batadv_hardif_remove_interface(hard_iface);
  551. }
  552. rtnl_unlock();
  553. }
  554. static int batadv_hard_if_event(struct notifier_block *this,
  555. unsigned long event, void *ptr)
  556. {
  557. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  558. struct batadv_hard_iface *hard_iface;
  559. struct batadv_hard_iface *primary_if = NULL;
  560. struct batadv_priv *bat_priv;
  561. if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
  562. batadv_sysfs_add_meshif(net_dev);
  563. bat_priv = netdev_priv(net_dev);
  564. batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
  565. return NOTIFY_DONE;
  566. }
  567. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  568. if (!hard_iface && event == NETDEV_REGISTER)
  569. hard_iface = batadv_hardif_add_interface(net_dev);
  570. if (!hard_iface)
  571. goto out;
  572. switch (event) {
  573. case NETDEV_UP:
  574. batadv_hardif_activate_interface(hard_iface);
  575. break;
  576. case NETDEV_GOING_DOWN:
  577. case NETDEV_DOWN:
  578. batadv_hardif_deactivate_interface(hard_iface);
  579. break;
  580. case NETDEV_UNREGISTER:
  581. list_del_rcu(&hard_iface->list);
  582. batadv_hardif_remove_interface(hard_iface);
  583. break;
  584. case NETDEV_CHANGEMTU:
  585. if (hard_iface->soft_iface)
  586. batadv_update_min_mtu(hard_iface->soft_iface);
  587. break;
  588. case NETDEV_CHANGEADDR:
  589. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  590. goto hardif_put;
  591. batadv_check_known_mac_addr(hard_iface->net_dev);
  592. bat_priv = netdev_priv(hard_iface->soft_iface);
  593. bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
  594. primary_if = batadv_primary_if_get_selected(bat_priv);
  595. if (!primary_if)
  596. goto hardif_put;
  597. if (hard_iface == primary_if)
  598. batadv_primary_if_update_addr(bat_priv, NULL);
  599. break;
  600. default:
  601. break;
  602. }
  603. hardif_put:
  604. batadv_hardif_free_ref(hard_iface);
  605. out:
  606. if (primary_if)
  607. batadv_hardif_free_ref(primary_if);
  608. return NOTIFY_DONE;
  609. }
  610. struct notifier_block batadv_hard_if_notifier = {
  611. .notifier_call = batadv_hard_if_event,
  612. };