hard-interface.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. /**
  197. * batadv_hardif_no_broadcast - check whether (re)broadcast is necessary
  198. * @if_outgoing: the outgoing interface checked and considered for (re)broadcast
  199. * @orig_addr: the originator of this packet
  200. * @orig_neigh: originator address of the forwarder we just got the packet from
  201. * (NULL if we originated)
  202. *
  203. * Checks whether a packet needs to be (re)broadcasted on the given interface.
  204. *
  205. * Return:
  206. * BATADV_HARDIF_BCAST_NORECIPIENT: No neighbor on interface
  207. * BATADV_HARDIF_BCAST_DUPFWD: Just one neighbor, but it is the forwarder
  208. * BATADV_HARDIF_BCAST_DUPORIG: Just one neighbor, but it is the originator
  209. * BATADV_HARDIF_BCAST_OK: Several neighbors, must broadcast
  210. */
  211. int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing,
  212. u8 *orig_addr, u8 *orig_neigh)
  213. {
  214. struct batadv_hardif_neigh_node *hardif_neigh;
  215. struct hlist_node *first;
  216. int ret = BATADV_HARDIF_BCAST_OK;
  217. rcu_read_lock();
  218. /* 0 neighbors -> no (re)broadcast */
  219. first = rcu_dereference(hlist_first_rcu(&if_outgoing->neigh_list));
  220. if (!first) {
  221. ret = BATADV_HARDIF_BCAST_NORECIPIENT;
  222. goto out;
  223. }
  224. /* >1 neighbors -> (re)brodcast */
  225. if (rcu_dereference(hlist_next_rcu(first)))
  226. goto out;
  227. hardif_neigh = hlist_entry(first, struct batadv_hardif_neigh_node,
  228. list);
  229. /* 1 neighbor, is the originator -> no rebroadcast */
  230. if (orig_addr && batadv_compare_eth(hardif_neigh->orig, orig_addr)) {
  231. ret = BATADV_HARDIF_BCAST_DUPORIG;
  232. /* 1 neighbor, is the one we received from -> no rebroadcast */
  233. } else if (orig_neigh &&
  234. batadv_compare_eth(hardif_neigh->orig, orig_neigh)) {
  235. ret = BATADV_HARDIF_BCAST_DUPFWD;
  236. }
  237. out:
  238. rcu_read_unlock();
  239. return ret;
  240. }
  241. static struct batadv_hard_iface *
  242. batadv_hardif_get_active(const struct net_device *soft_iface)
  243. {
  244. struct batadv_hard_iface *hard_iface;
  245. rcu_read_lock();
  246. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  247. if (hard_iface->soft_iface != soft_iface)
  248. continue;
  249. if (hard_iface->if_status == BATADV_IF_ACTIVE &&
  250. kref_get_unless_zero(&hard_iface->refcount))
  251. goto out;
  252. }
  253. hard_iface = NULL;
  254. out:
  255. rcu_read_unlock();
  256. return hard_iface;
  257. }
  258. static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
  259. struct batadv_hard_iface *oldif)
  260. {
  261. struct batadv_hard_iface *primary_if;
  262. primary_if = batadv_primary_if_get_selected(bat_priv);
  263. if (!primary_if)
  264. goto out;
  265. batadv_dat_init_own_addr(bat_priv, primary_if);
  266. batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
  267. out:
  268. if (primary_if)
  269. batadv_hardif_put(primary_if);
  270. }
  271. static void batadv_primary_if_select(struct batadv_priv *bat_priv,
  272. struct batadv_hard_iface *new_hard_iface)
  273. {
  274. struct batadv_hard_iface *curr_hard_iface;
  275. ASSERT_RTNL();
  276. if (new_hard_iface)
  277. kref_get(&new_hard_iface->refcount);
  278. curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
  279. rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
  280. if (!new_hard_iface)
  281. goto out;
  282. bat_priv->algo_ops->iface.primary_set(new_hard_iface);
  283. batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
  284. out:
  285. if (curr_hard_iface)
  286. batadv_hardif_put(curr_hard_iface);
  287. }
  288. static bool
  289. batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
  290. {
  291. if (hard_iface->net_dev->flags & IFF_UP)
  292. return true;
  293. return false;
  294. }
  295. static void batadv_check_known_mac_addr(const struct net_device *net_dev)
  296. {
  297. const struct batadv_hard_iface *hard_iface;
  298. rcu_read_lock();
  299. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  300. if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
  301. (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
  302. continue;
  303. if (hard_iface->net_dev == net_dev)
  304. continue;
  305. if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
  306. net_dev->dev_addr))
  307. continue;
  308. pr_warn("The newly added mac address (%pM) already exists on: %s\n",
  309. net_dev->dev_addr, hard_iface->net_dev->name);
  310. pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
  311. }
  312. rcu_read_unlock();
  313. }
  314. /**
  315. * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
  316. * @soft_iface: netdev struct of the mesh interface
  317. */
  318. static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
  319. {
  320. const struct batadv_hard_iface *hard_iface;
  321. unsigned short lower_header_len = ETH_HLEN;
  322. unsigned short lower_headroom = 0;
  323. unsigned short lower_tailroom = 0;
  324. unsigned short needed_headroom;
  325. rcu_read_lock();
  326. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  327. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  328. continue;
  329. if (hard_iface->soft_iface != soft_iface)
  330. continue;
  331. lower_header_len = max_t(unsigned short, lower_header_len,
  332. hard_iface->net_dev->hard_header_len);
  333. lower_headroom = max_t(unsigned short, lower_headroom,
  334. hard_iface->net_dev->needed_headroom);
  335. lower_tailroom = max_t(unsigned short, lower_tailroom,
  336. hard_iface->net_dev->needed_tailroom);
  337. }
  338. rcu_read_unlock();
  339. needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
  340. needed_headroom += batadv_max_header_len();
  341. soft_iface->needed_headroom = needed_headroom;
  342. soft_iface->needed_tailroom = lower_tailroom;
  343. }
  344. int batadv_hardif_min_mtu(struct net_device *soft_iface)
  345. {
  346. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  347. const struct batadv_hard_iface *hard_iface;
  348. int min_mtu = INT_MAX;
  349. rcu_read_lock();
  350. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  351. if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
  352. (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
  353. continue;
  354. if (hard_iface->soft_iface != soft_iface)
  355. continue;
  356. min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
  357. }
  358. rcu_read_unlock();
  359. if (atomic_read(&bat_priv->fragmentation) == 0)
  360. goto out;
  361. /* with fragmentation enabled the maximum size of internally generated
  362. * packets such as translation table exchanges or tvlv containers, etc
  363. * has to be calculated
  364. */
  365. min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
  366. min_mtu -= sizeof(struct batadv_frag_packet);
  367. min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
  368. out:
  369. /* report to the other components the maximum amount of bytes that
  370. * batman-adv can send over the wire (without considering the payload
  371. * overhead). For example, this value is used by TT to compute the
  372. * maximum local table table size
  373. */
  374. atomic_set(&bat_priv->packet_size_max, min_mtu);
  375. /* the real soft-interface MTU is computed by removing the payload
  376. * overhead from the maximum amount of bytes that was just computed.
  377. *
  378. * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
  379. */
  380. return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
  381. }
  382. /* adjusts the MTU if a new interface with a smaller MTU appeared. */
  383. void batadv_update_min_mtu(struct net_device *soft_iface)
  384. {
  385. soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
  386. /* Check if the local translate table should be cleaned up to match a
  387. * new (and smaller) MTU.
  388. */
  389. batadv_tt_local_resize_to_mtu(soft_iface);
  390. }
  391. static void
  392. batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
  393. {
  394. struct batadv_priv *bat_priv;
  395. struct batadv_hard_iface *primary_if = NULL;
  396. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  397. goto out;
  398. bat_priv = netdev_priv(hard_iface->soft_iface);
  399. bat_priv->algo_ops->iface.update_mac(hard_iface);
  400. hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
  401. /* the first active interface becomes our primary interface or
  402. * the next active interface after the old primary interface was removed
  403. */
  404. primary_if = batadv_primary_if_get_selected(bat_priv);
  405. if (!primary_if)
  406. batadv_primary_if_select(bat_priv, hard_iface);
  407. batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
  408. hard_iface->net_dev->name);
  409. batadv_update_min_mtu(hard_iface->soft_iface);
  410. if (bat_priv->algo_ops->iface.activate)
  411. bat_priv->algo_ops->iface.activate(hard_iface);
  412. out:
  413. if (primary_if)
  414. batadv_hardif_put(primary_if);
  415. }
  416. static void
  417. batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
  418. {
  419. if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
  420. (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
  421. return;
  422. hard_iface->if_status = BATADV_IF_INACTIVE;
  423. batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
  424. hard_iface->net_dev->name);
  425. batadv_update_min_mtu(hard_iface->soft_iface);
  426. }
  427. /**
  428. * batadv_master_del_slave - remove hard_iface from the current master interface
  429. * @slave: the interface enslaved in another master
  430. * @master: the master from which slave has to be removed
  431. *
  432. * Invoke ndo_del_slave on master passing slave as argument. In this way slave
  433. * is free'd and master can correctly change its internal state.
  434. *
  435. * Return: 0 on success, a negative value representing the error otherwise
  436. */
  437. static int batadv_master_del_slave(struct batadv_hard_iface *slave,
  438. struct net_device *master)
  439. {
  440. int ret;
  441. if (!master)
  442. return 0;
  443. ret = -EBUSY;
  444. if (master->netdev_ops->ndo_del_slave)
  445. ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
  446. return ret;
  447. }
  448. int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
  449. struct net *net, const char *iface_name)
  450. {
  451. struct batadv_priv *bat_priv;
  452. struct net_device *soft_iface, *master;
  453. __be16 ethertype = htons(ETH_P_BATMAN);
  454. int max_header_len = batadv_max_header_len();
  455. int ret;
  456. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  457. goto out;
  458. kref_get(&hard_iface->refcount);
  459. soft_iface = dev_get_by_name(net, iface_name);
  460. if (!soft_iface) {
  461. soft_iface = batadv_softif_create(net, iface_name);
  462. if (!soft_iface) {
  463. ret = -ENOMEM;
  464. goto err;
  465. }
  466. /* dev_get_by_name() increases the reference counter for us */
  467. dev_hold(soft_iface);
  468. }
  469. if (!batadv_softif_is_valid(soft_iface)) {
  470. pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
  471. soft_iface->name);
  472. ret = -EINVAL;
  473. goto err_dev;
  474. }
  475. /* check if the interface is enslaved in another virtual one and
  476. * in that case unlink it first
  477. */
  478. master = netdev_master_upper_dev_get(hard_iface->net_dev);
  479. ret = batadv_master_del_slave(hard_iface, master);
  480. if (ret)
  481. goto err_dev;
  482. hard_iface->soft_iface = soft_iface;
  483. bat_priv = netdev_priv(hard_iface->soft_iface);
  484. ret = netdev_master_upper_dev_link(hard_iface->net_dev,
  485. soft_iface, NULL, NULL);
  486. if (ret)
  487. goto err_dev;
  488. ret = bat_priv->algo_ops->iface.enable(hard_iface);
  489. if (ret < 0)
  490. goto err_upper;
  491. hard_iface->if_num = bat_priv->num_ifaces;
  492. bat_priv->num_ifaces++;
  493. hard_iface->if_status = BATADV_IF_INACTIVE;
  494. ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
  495. if (ret < 0) {
  496. bat_priv->algo_ops->iface.disable(hard_iface);
  497. bat_priv->num_ifaces--;
  498. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  499. goto err_upper;
  500. }
  501. kref_get(&hard_iface->refcount);
  502. hard_iface->batman_adv_ptype.type = ethertype;
  503. hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
  504. hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
  505. dev_add_pack(&hard_iface->batman_adv_ptype);
  506. batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
  507. hard_iface->net_dev->name);
  508. if (atomic_read(&bat_priv->fragmentation) &&
  509. hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
  510. batadv_info(hard_iface->soft_iface,
  511. "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",
  512. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  513. ETH_DATA_LEN + max_header_len);
  514. if (!atomic_read(&bat_priv->fragmentation) &&
  515. hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
  516. batadv_info(hard_iface->soft_iface,
  517. "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",
  518. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  519. ETH_DATA_LEN + max_header_len);
  520. if (batadv_hardif_is_iface_up(hard_iface))
  521. batadv_hardif_activate_interface(hard_iface);
  522. else
  523. batadv_err(hard_iface->soft_iface,
  524. "Not using interface %s (retrying later): interface not active\n",
  525. hard_iface->net_dev->name);
  526. batadv_hardif_recalc_extra_skbroom(soft_iface);
  527. out:
  528. return 0;
  529. err_upper:
  530. netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
  531. err_dev:
  532. hard_iface->soft_iface = NULL;
  533. dev_put(soft_iface);
  534. err:
  535. batadv_hardif_put(hard_iface);
  536. return ret;
  537. }
  538. void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
  539. enum batadv_hard_if_cleanup autodel)
  540. {
  541. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  542. struct batadv_hard_iface *primary_if = NULL;
  543. batadv_hardif_deactivate_interface(hard_iface);
  544. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  545. goto out;
  546. batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
  547. hard_iface->net_dev->name);
  548. dev_remove_pack(&hard_iface->batman_adv_ptype);
  549. batadv_hardif_put(hard_iface);
  550. bat_priv->num_ifaces--;
  551. batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
  552. primary_if = batadv_primary_if_get_selected(bat_priv);
  553. if (hard_iface == primary_if) {
  554. struct batadv_hard_iface *new_if;
  555. new_if = batadv_hardif_get_active(hard_iface->soft_iface);
  556. batadv_primary_if_select(bat_priv, new_if);
  557. if (new_if)
  558. batadv_hardif_put(new_if);
  559. }
  560. bat_priv->algo_ops->iface.disable(hard_iface);
  561. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  562. /* delete all references to this hard_iface */
  563. batadv_purge_orig_ref(bat_priv);
  564. batadv_purge_outstanding_packets(bat_priv, hard_iface);
  565. dev_put(hard_iface->soft_iface);
  566. netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
  567. batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
  568. /* nobody uses this interface anymore */
  569. if (!bat_priv->num_ifaces) {
  570. batadv_gw_check_client_stop(bat_priv);
  571. if (autodel == BATADV_IF_CLEANUP_AUTO)
  572. batadv_softif_destroy_sysfs(hard_iface->soft_iface);
  573. }
  574. batadv_hardif_put(hard_iface);
  575. out:
  576. if (primary_if)
  577. batadv_hardif_put(primary_if);
  578. }
  579. static struct batadv_hard_iface *
  580. batadv_hardif_add_interface(struct net_device *net_dev)
  581. {
  582. struct batadv_hard_iface *hard_iface;
  583. int ret;
  584. ASSERT_RTNL();
  585. if (!batadv_is_valid_iface(net_dev))
  586. goto out;
  587. dev_hold(net_dev);
  588. hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
  589. if (!hard_iface)
  590. goto release_dev;
  591. ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
  592. if (ret)
  593. goto free_if;
  594. hard_iface->if_num = -1;
  595. hard_iface->net_dev = net_dev;
  596. hard_iface->soft_iface = NULL;
  597. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  598. ret = batadv_debugfs_add_hardif(hard_iface);
  599. if (ret)
  600. goto free_sysfs;
  601. INIT_LIST_HEAD(&hard_iface->list);
  602. INIT_HLIST_HEAD(&hard_iface->neigh_list);
  603. spin_lock_init(&hard_iface->neigh_list_lock);
  604. kref_init(&hard_iface->refcount);
  605. hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
  606. if (batadv_is_wifi_netdev(net_dev))
  607. hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
  608. batadv_v_hardif_init(hard_iface);
  609. batadv_check_known_mac_addr(hard_iface->net_dev);
  610. kref_get(&hard_iface->refcount);
  611. list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
  612. return hard_iface;
  613. free_sysfs:
  614. batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
  615. free_if:
  616. kfree(hard_iface);
  617. release_dev:
  618. dev_put(net_dev);
  619. out:
  620. return NULL;
  621. }
  622. static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
  623. {
  624. ASSERT_RTNL();
  625. /* first deactivate interface */
  626. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  627. batadv_hardif_disable_interface(hard_iface,
  628. BATADV_IF_CLEANUP_KEEP);
  629. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  630. return;
  631. hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
  632. batadv_debugfs_del_hardif(hard_iface);
  633. batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
  634. batadv_hardif_put(hard_iface);
  635. }
  636. void batadv_hardif_remove_interfaces(void)
  637. {
  638. struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
  639. rtnl_lock();
  640. list_for_each_entry_safe(hard_iface, hard_iface_tmp,
  641. &batadv_hardif_list, list) {
  642. list_del_rcu(&hard_iface->list);
  643. batadv_hardif_remove_interface(hard_iface);
  644. }
  645. rtnl_unlock();
  646. }
  647. static int batadv_hard_if_event(struct notifier_block *this,
  648. unsigned long event, void *ptr)
  649. {
  650. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  651. struct batadv_hard_iface *hard_iface;
  652. struct batadv_hard_iface *primary_if = NULL;
  653. struct batadv_priv *bat_priv;
  654. if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
  655. batadv_sysfs_add_meshif(net_dev);
  656. bat_priv = netdev_priv(net_dev);
  657. batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
  658. return NOTIFY_DONE;
  659. }
  660. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  661. if (!hard_iface && (event == NETDEV_REGISTER ||
  662. event == NETDEV_POST_TYPE_CHANGE))
  663. hard_iface = batadv_hardif_add_interface(net_dev);
  664. if (!hard_iface)
  665. goto out;
  666. switch (event) {
  667. case NETDEV_UP:
  668. batadv_hardif_activate_interface(hard_iface);
  669. break;
  670. case NETDEV_GOING_DOWN:
  671. case NETDEV_DOWN:
  672. batadv_hardif_deactivate_interface(hard_iface);
  673. break;
  674. case NETDEV_UNREGISTER:
  675. case NETDEV_PRE_TYPE_CHANGE:
  676. list_del_rcu(&hard_iface->list);
  677. batadv_hardif_remove_interface(hard_iface);
  678. break;
  679. case NETDEV_CHANGEMTU:
  680. if (hard_iface->soft_iface)
  681. batadv_update_min_mtu(hard_iface->soft_iface);
  682. break;
  683. case NETDEV_CHANGEADDR:
  684. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  685. goto hardif_put;
  686. batadv_check_known_mac_addr(hard_iface->net_dev);
  687. bat_priv = netdev_priv(hard_iface->soft_iface);
  688. bat_priv->algo_ops->iface.update_mac(hard_iface);
  689. primary_if = batadv_primary_if_get_selected(bat_priv);
  690. if (!primary_if)
  691. goto hardif_put;
  692. if (hard_iface == primary_if)
  693. batadv_primary_if_update_addr(bat_priv, NULL);
  694. break;
  695. default:
  696. break;
  697. }
  698. hardif_put:
  699. batadv_hardif_put(hard_iface);
  700. out:
  701. if (primary_if)
  702. batadv_hardif_put(primary_if);
  703. return NOTIFY_DONE;
  704. }
  705. struct notifier_block batadv_hard_if_notifier = {
  706. .notifier_call = batadv_hard_if_event,
  707. };