hard-interface.c 22 KB

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