hard-interface.c 22 KB

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