hard-interface.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2007-2017 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "hard-interface.h"
  19. #include "main.h"
  20. #include <linux/atomic.h>
  21. #include <linux/bug.h>
  22. #include <linux/byteorder/generic.h>
  23. #include <linux/errno.h>
  24. #include <linux/gfp.h>
  25. #include <linux/if.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/kernel.h>
  29. #include <linux/kref.h>
  30. #include <linux/list.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/printk.h>
  33. #include <linux/rculist.h>
  34. #include <linux/rtnetlink.h>
  35. #include <linux/slab.h>
  36. #include <linux/spinlock.h>
  37. #include <net/net_namespace.h>
  38. #include <net/rtnetlink.h>
  39. #include <uapi/linux/batadv_packet.h>
  40. #include "bat_v.h"
  41. #include "bridge_loop_avoidance.h"
  42. #include "debugfs.h"
  43. #include "distributed-arp-table.h"
  44. #include "gateway_client.h"
  45. #include "log.h"
  46. #include "originator.h"
  47. #include "send.h"
  48. #include "soft-interface.h"
  49. #include "sysfs.h"
  50. #include "translation-table.h"
  51. /**
  52. * batadv_hardif_release() - release hard interface from lists and queue for
  53. * free after rcu grace period
  54. * @ref: kref pointer of the hard interface
  55. */
  56. void batadv_hardif_release(struct kref *ref)
  57. {
  58. struct batadv_hard_iface *hard_iface;
  59. hard_iface = container_of(ref, struct batadv_hard_iface, refcount);
  60. dev_put(hard_iface->net_dev);
  61. kfree_rcu(hard_iface, rcu);
  62. }
  63. /**
  64. * batadv_hardif_get_by_netdev() - Get hard interface object of a net_device
  65. * @net_dev: net_device to search for
  66. *
  67. * Return: batadv_hard_iface of net_dev (with increased refcnt), NULL on errors
  68. */
  69. struct batadv_hard_iface *
  70. batadv_hardif_get_by_netdev(const struct net_device *net_dev)
  71. {
  72. struct batadv_hard_iface *hard_iface;
  73. rcu_read_lock();
  74. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  75. if (hard_iface->net_dev == net_dev &&
  76. kref_get_unless_zero(&hard_iface->refcount))
  77. goto out;
  78. }
  79. hard_iface = NULL;
  80. out:
  81. rcu_read_unlock();
  82. return hard_iface;
  83. }
  84. /**
  85. * batadv_getlink_net() - return link net namespace (of use fallback)
  86. * @netdev: net_device to check
  87. * @fallback_net: return in case get_link_net is not available for @netdev
  88. *
  89. * Return: result of rtnl_link_ops->get_link_net or @fallback_net
  90. */
  91. static struct net *batadv_getlink_net(const struct net_device *netdev,
  92. struct net *fallback_net)
  93. {
  94. if (!netdev->rtnl_link_ops)
  95. return fallback_net;
  96. if (!netdev->rtnl_link_ops->get_link_net)
  97. return fallback_net;
  98. return netdev->rtnl_link_ops->get_link_net(netdev);
  99. }
  100. /**
  101. * batadv_mutual_parents() - check if two devices are each others parent
  102. * @dev1: 1st net dev
  103. * @net1: 1st devices netns
  104. * @dev2: 2nd net dev
  105. * @net2: 2nd devices netns
  106. *
  107. * veth devices come in pairs and each is the parent of the other!
  108. *
  109. * Return: true if the devices are each others parent, otherwise false
  110. */
  111. static bool batadv_mutual_parents(const struct net_device *dev1,
  112. struct net *net1,
  113. const struct net_device *dev2,
  114. struct net *net2)
  115. {
  116. int dev1_parent_iflink = dev_get_iflink(dev1);
  117. int dev2_parent_iflink = dev_get_iflink(dev2);
  118. const struct net *dev1_parent_net;
  119. const struct net *dev2_parent_net;
  120. dev1_parent_net = batadv_getlink_net(dev1, net1);
  121. dev2_parent_net = batadv_getlink_net(dev2, net2);
  122. if (!dev1_parent_iflink || !dev2_parent_iflink)
  123. return false;
  124. return (dev1_parent_iflink == dev2->ifindex) &&
  125. (dev2_parent_iflink == dev1->ifindex) &&
  126. net_eq(dev1_parent_net, net2) &&
  127. net_eq(dev2_parent_net, net1);
  128. }
  129. /**
  130. * batadv_is_on_batman_iface() - check if a device is a batman iface descendant
  131. * @net_dev: the device to check
  132. *
  133. * If the user creates any virtual device on top of a batman-adv interface, it
  134. * is important to prevent this new interface to be used to create a new mesh
  135. * network (this behaviour would lead to a batman-over-batman configuration).
  136. * This function recursively checks all the fathers of the device passed as
  137. * argument looking for a batman-adv soft interface.
  138. *
  139. * Return: true if the device is descendant of a batman-adv mesh interface (or
  140. * if it is a batman-adv interface itself), false otherwise
  141. */
  142. static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
  143. {
  144. struct net *net = dev_net(net_dev);
  145. struct net_device *parent_dev;
  146. struct net *parent_net;
  147. bool ret;
  148. /* check if this is a batman-adv mesh interface */
  149. if (batadv_softif_is_valid(net_dev))
  150. return true;
  151. /* no more parents..stop recursion */
  152. if (dev_get_iflink(net_dev) == 0 ||
  153. dev_get_iflink(net_dev) == net_dev->ifindex)
  154. return false;
  155. parent_net = batadv_getlink_net(net_dev, net);
  156. /* recurse over the parent device */
  157. parent_dev = __dev_get_by_index((struct net *)parent_net,
  158. dev_get_iflink(net_dev));
  159. /* if we got a NULL parent_dev there is something broken.. */
  160. if (WARN(!parent_dev, "Cannot find parent device"))
  161. return false;
  162. if (batadv_mutual_parents(net_dev, net, parent_dev, parent_net))
  163. return false;
  164. ret = batadv_is_on_batman_iface(parent_dev);
  165. return ret;
  166. }
  167. static bool batadv_is_valid_iface(const struct net_device *net_dev)
  168. {
  169. if (net_dev->flags & IFF_LOOPBACK)
  170. return false;
  171. if (net_dev->type != ARPHRD_ETHER)
  172. return false;
  173. if (net_dev->addr_len != ETH_ALEN)
  174. return false;
  175. /* no batman over batman */
  176. if (batadv_is_on_batman_iface(net_dev))
  177. return false;
  178. return true;
  179. }
  180. /**
  181. * batadv_get_real_netdevice() - check if the given netdev struct is a virtual
  182. * interface on top of another 'real' interface
  183. * @netdev: the device to check
  184. *
  185. * Callers must hold the rtnl semaphore. You may want batadv_get_real_netdev()
  186. * instead of this.
  187. *
  188. * Return: the 'real' net device or the original net device and NULL in case
  189. * of an error.
  190. */
  191. static struct net_device *batadv_get_real_netdevice(struct net_device *netdev)
  192. {
  193. struct batadv_hard_iface *hard_iface = NULL;
  194. struct net_device *real_netdev = NULL;
  195. struct net *real_net;
  196. struct net *net;
  197. int ifindex;
  198. ASSERT_RTNL();
  199. if (!netdev)
  200. return NULL;
  201. if (netdev->ifindex == dev_get_iflink(netdev)) {
  202. dev_hold(netdev);
  203. return netdev;
  204. }
  205. hard_iface = batadv_hardif_get_by_netdev(netdev);
  206. if (!hard_iface || !hard_iface->soft_iface)
  207. goto out;
  208. net = dev_net(hard_iface->soft_iface);
  209. ifindex = dev_get_iflink(netdev);
  210. real_net = batadv_getlink_net(netdev, net);
  211. real_netdev = dev_get_by_index(real_net, ifindex);
  212. out:
  213. if (hard_iface)
  214. batadv_hardif_put(hard_iface);
  215. return real_netdev;
  216. }
  217. /**
  218. * batadv_get_real_netdev() - check if the given net_device struct is a virtual
  219. * interface on top of another 'real' interface
  220. * @net_device: the device to check
  221. *
  222. * Return: the 'real' net device or the original net device and NULL in case
  223. * of an error.
  224. */
  225. struct net_device *batadv_get_real_netdev(struct net_device *net_device)
  226. {
  227. struct net_device *real_netdev;
  228. rtnl_lock();
  229. real_netdev = batadv_get_real_netdevice(net_device);
  230. rtnl_unlock();
  231. return real_netdev;
  232. }
  233. /**
  234. * batadv_is_wext_netdev() - check if the given net_device struct is a
  235. * wext wifi interface
  236. * @net_device: the device to check
  237. *
  238. * Return: true if the net device is a wext wireless device, false
  239. * otherwise.
  240. */
  241. static bool batadv_is_wext_netdev(struct net_device *net_device)
  242. {
  243. if (!net_device)
  244. return false;
  245. #ifdef CONFIG_WIRELESS_EXT
  246. /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
  247. * check for wireless_handlers != NULL
  248. */
  249. if (net_device->wireless_handlers)
  250. return true;
  251. #endif
  252. return false;
  253. }
  254. /**
  255. * batadv_is_cfg80211_netdev() - check if the given net_device struct is a
  256. * cfg80211 wifi interface
  257. * @net_device: the device to check
  258. *
  259. * Return: true if the net device is a cfg80211 wireless device, false
  260. * otherwise.
  261. */
  262. static bool batadv_is_cfg80211_netdev(struct net_device *net_device)
  263. {
  264. if (!net_device)
  265. return false;
  266. /* cfg80211 drivers have to set ieee80211_ptr */
  267. if (net_device->ieee80211_ptr)
  268. return true;
  269. return false;
  270. }
  271. /**
  272. * batadv_wifi_flags_evaluate() - calculate wifi flags for net_device
  273. * @net_device: the device to check
  274. *
  275. * Return: batadv_hard_iface_wifi_flags flags of the device
  276. */
  277. static u32 batadv_wifi_flags_evaluate(struct net_device *net_device)
  278. {
  279. u32 wifi_flags = 0;
  280. struct net_device *real_netdev;
  281. if (batadv_is_wext_netdev(net_device))
  282. wifi_flags |= BATADV_HARDIF_WIFI_WEXT_DIRECT;
  283. if (batadv_is_cfg80211_netdev(net_device))
  284. wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT;
  285. real_netdev = batadv_get_real_netdevice(net_device);
  286. if (!real_netdev)
  287. return wifi_flags;
  288. if (real_netdev == net_device)
  289. goto out;
  290. if (batadv_is_wext_netdev(real_netdev))
  291. wifi_flags |= BATADV_HARDIF_WIFI_WEXT_INDIRECT;
  292. if (batadv_is_cfg80211_netdev(real_netdev))
  293. wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT;
  294. out:
  295. dev_put(real_netdev);
  296. return wifi_flags;
  297. }
  298. /**
  299. * batadv_is_cfg80211_hardif() - check if the given hardif is a cfg80211 wifi
  300. * interface
  301. * @hard_iface: the device to check
  302. *
  303. * Return: true if the net device is a cfg80211 wireless device, false
  304. * otherwise.
  305. */
  306. bool batadv_is_cfg80211_hardif(struct batadv_hard_iface *hard_iface)
  307. {
  308. u32 allowed_flags = 0;
  309. allowed_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT;
  310. allowed_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT;
  311. return !!(hard_iface->wifi_flags & allowed_flags);
  312. }
  313. /**
  314. * batadv_is_wifi_hardif() - check if the given hardif is a wifi interface
  315. * @hard_iface: the device to check
  316. *
  317. * Return: true if the net device is a 802.11 wireless device, false otherwise.
  318. */
  319. bool batadv_is_wifi_hardif(struct batadv_hard_iface *hard_iface)
  320. {
  321. if (!hard_iface)
  322. return false;
  323. return hard_iface->wifi_flags != 0;
  324. }
  325. /**
  326. * batadv_hardif_no_broadcast() - check whether (re)broadcast is necessary
  327. * @if_outgoing: the outgoing interface checked and considered for (re)broadcast
  328. * @orig_addr: the originator of this packet
  329. * @orig_neigh: originator address of the forwarder we just got the packet from
  330. * (NULL if we originated)
  331. *
  332. * Checks whether a packet needs to be (re)broadcasted on the given interface.
  333. *
  334. * Return:
  335. * BATADV_HARDIF_BCAST_NORECIPIENT: No neighbor on interface
  336. * BATADV_HARDIF_BCAST_DUPFWD: Just one neighbor, but it is the forwarder
  337. * BATADV_HARDIF_BCAST_DUPORIG: Just one neighbor, but it is the originator
  338. * BATADV_HARDIF_BCAST_OK: Several neighbors, must broadcast
  339. */
  340. int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing,
  341. u8 *orig_addr, u8 *orig_neigh)
  342. {
  343. struct batadv_hardif_neigh_node *hardif_neigh;
  344. struct hlist_node *first;
  345. int ret = BATADV_HARDIF_BCAST_OK;
  346. rcu_read_lock();
  347. /* 0 neighbors -> no (re)broadcast */
  348. first = rcu_dereference(hlist_first_rcu(&if_outgoing->neigh_list));
  349. if (!first) {
  350. ret = BATADV_HARDIF_BCAST_NORECIPIENT;
  351. goto out;
  352. }
  353. /* >1 neighbors -> (re)brodcast */
  354. if (rcu_dereference(hlist_next_rcu(first)))
  355. goto out;
  356. hardif_neigh = hlist_entry(first, struct batadv_hardif_neigh_node,
  357. list);
  358. /* 1 neighbor, is the originator -> no rebroadcast */
  359. if (orig_addr && batadv_compare_eth(hardif_neigh->orig, orig_addr)) {
  360. ret = BATADV_HARDIF_BCAST_DUPORIG;
  361. /* 1 neighbor, is the one we received from -> no rebroadcast */
  362. } else if (orig_neigh &&
  363. batadv_compare_eth(hardif_neigh->orig, orig_neigh)) {
  364. ret = BATADV_HARDIF_BCAST_DUPFWD;
  365. }
  366. out:
  367. rcu_read_unlock();
  368. return ret;
  369. }
  370. static struct batadv_hard_iface *
  371. batadv_hardif_get_active(const struct net_device *soft_iface)
  372. {
  373. struct batadv_hard_iface *hard_iface;
  374. rcu_read_lock();
  375. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  376. if (hard_iface->soft_iface != soft_iface)
  377. continue;
  378. if (hard_iface->if_status == BATADV_IF_ACTIVE &&
  379. kref_get_unless_zero(&hard_iface->refcount))
  380. goto out;
  381. }
  382. hard_iface = NULL;
  383. out:
  384. rcu_read_unlock();
  385. return hard_iface;
  386. }
  387. static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
  388. struct batadv_hard_iface *oldif)
  389. {
  390. struct batadv_hard_iface *primary_if;
  391. primary_if = batadv_primary_if_get_selected(bat_priv);
  392. if (!primary_if)
  393. goto out;
  394. batadv_dat_init_own_addr(bat_priv, primary_if);
  395. batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
  396. out:
  397. if (primary_if)
  398. batadv_hardif_put(primary_if);
  399. }
  400. static void batadv_primary_if_select(struct batadv_priv *bat_priv,
  401. struct batadv_hard_iface *new_hard_iface)
  402. {
  403. struct batadv_hard_iface *curr_hard_iface;
  404. ASSERT_RTNL();
  405. if (new_hard_iface)
  406. kref_get(&new_hard_iface->refcount);
  407. curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
  408. rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
  409. if (!new_hard_iface)
  410. goto out;
  411. bat_priv->algo_ops->iface.primary_set(new_hard_iface);
  412. batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
  413. out:
  414. if (curr_hard_iface)
  415. batadv_hardif_put(curr_hard_iface);
  416. }
  417. static bool
  418. batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
  419. {
  420. if (hard_iface->net_dev->flags & IFF_UP)
  421. return true;
  422. return false;
  423. }
  424. static void batadv_check_known_mac_addr(const struct net_device *net_dev)
  425. {
  426. const struct batadv_hard_iface *hard_iface;
  427. rcu_read_lock();
  428. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  429. if (hard_iface->if_status != BATADV_IF_ACTIVE &&
  430. hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
  431. continue;
  432. if (hard_iface->net_dev == net_dev)
  433. continue;
  434. if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
  435. net_dev->dev_addr))
  436. continue;
  437. pr_warn("The newly added mac address (%pM) already exists on: %s\n",
  438. net_dev->dev_addr, hard_iface->net_dev->name);
  439. pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
  440. }
  441. rcu_read_unlock();
  442. }
  443. /**
  444. * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
  445. * @soft_iface: netdev struct of the mesh interface
  446. */
  447. static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
  448. {
  449. const struct batadv_hard_iface *hard_iface;
  450. unsigned short lower_header_len = ETH_HLEN;
  451. unsigned short lower_headroom = 0;
  452. unsigned short lower_tailroom = 0;
  453. unsigned short needed_headroom;
  454. rcu_read_lock();
  455. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  456. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  457. continue;
  458. if (hard_iface->soft_iface != soft_iface)
  459. continue;
  460. lower_header_len = max_t(unsigned short, lower_header_len,
  461. hard_iface->net_dev->hard_header_len);
  462. lower_headroom = max_t(unsigned short, lower_headroom,
  463. hard_iface->net_dev->needed_headroom);
  464. lower_tailroom = max_t(unsigned short, lower_tailroom,
  465. hard_iface->net_dev->needed_tailroom);
  466. }
  467. rcu_read_unlock();
  468. needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
  469. needed_headroom += batadv_max_header_len();
  470. soft_iface->needed_headroom = needed_headroom;
  471. soft_iface->needed_tailroom = lower_tailroom;
  472. }
  473. /**
  474. * batadv_hardif_min_mtu() - Calculate maximum MTU for soft interface
  475. * @soft_iface: netdev struct of the soft interface
  476. *
  477. * Return: MTU for the soft-interface (limited by the minimal MTU of all active
  478. * slave interfaces)
  479. */
  480. int batadv_hardif_min_mtu(struct net_device *soft_iface)
  481. {
  482. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  483. const struct batadv_hard_iface *hard_iface;
  484. int min_mtu = INT_MAX;
  485. rcu_read_lock();
  486. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  487. if (hard_iface->if_status != BATADV_IF_ACTIVE &&
  488. hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
  489. continue;
  490. if (hard_iface->soft_iface != soft_iface)
  491. continue;
  492. min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
  493. }
  494. rcu_read_unlock();
  495. if (atomic_read(&bat_priv->fragmentation) == 0)
  496. goto out;
  497. /* with fragmentation enabled the maximum size of internally generated
  498. * packets such as translation table exchanges or tvlv containers, etc
  499. * has to be calculated
  500. */
  501. min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
  502. min_mtu -= sizeof(struct batadv_frag_packet);
  503. min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
  504. out:
  505. /* report to the other components the maximum amount of bytes that
  506. * batman-adv can send over the wire (without considering the payload
  507. * overhead). For example, this value is used by TT to compute the
  508. * maximum local table table size
  509. */
  510. atomic_set(&bat_priv->packet_size_max, min_mtu);
  511. /* the real soft-interface MTU is computed by removing the payload
  512. * overhead from the maximum amount of bytes that was just computed.
  513. *
  514. * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
  515. */
  516. return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
  517. }
  518. /**
  519. * batadv_update_min_mtu() - Adjusts the MTU if a new interface with a smaller
  520. * MTU appeared
  521. * @soft_iface: netdev struct of the soft interface
  522. */
  523. void batadv_update_min_mtu(struct net_device *soft_iface)
  524. {
  525. soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
  526. /* Check if the local translate table should be cleaned up to match a
  527. * new (and smaller) MTU.
  528. */
  529. batadv_tt_local_resize_to_mtu(soft_iface);
  530. }
  531. static void
  532. batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
  533. {
  534. struct batadv_priv *bat_priv;
  535. struct batadv_hard_iface *primary_if = NULL;
  536. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  537. goto out;
  538. bat_priv = netdev_priv(hard_iface->soft_iface);
  539. bat_priv->algo_ops->iface.update_mac(hard_iface);
  540. hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
  541. /* the first active interface becomes our primary interface or
  542. * the next active interface after the old primary interface was removed
  543. */
  544. primary_if = batadv_primary_if_get_selected(bat_priv);
  545. if (!primary_if)
  546. batadv_primary_if_select(bat_priv, hard_iface);
  547. batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
  548. hard_iface->net_dev->name);
  549. batadv_update_min_mtu(hard_iface->soft_iface);
  550. if (bat_priv->algo_ops->iface.activate)
  551. bat_priv->algo_ops->iface.activate(hard_iface);
  552. out:
  553. if (primary_if)
  554. batadv_hardif_put(primary_if);
  555. }
  556. static void
  557. batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
  558. {
  559. if (hard_iface->if_status != BATADV_IF_ACTIVE &&
  560. hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
  561. return;
  562. hard_iface->if_status = BATADV_IF_INACTIVE;
  563. batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
  564. hard_iface->net_dev->name);
  565. batadv_update_min_mtu(hard_iface->soft_iface);
  566. }
  567. /**
  568. * batadv_master_del_slave() - remove hard_iface from the current master iface
  569. * @slave: the interface enslaved in another master
  570. * @master: the master from which slave has to be removed
  571. *
  572. * Invoke ndo_del_slave on master passing slave as argument. In this way slave
  573. * is free'd and master can correctly change its internal state.
  574. *
  575. * Return: 0 on success, a negative value representing the error otherwise
  576. */
  577. static int batadv_master_del_slave(struct batadv_hard_iface *slave,
  578. struct net_device *master)
  579. {
  580. int ret;
  581. if (!master)
  582. return 0;
  583. ret = -EBUSY;
  584. if (master->netdev_ops->ndo_del_slave)
  585. ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
  586. return ret;
  587. }
  588. /**
  589. * batadv_hardif_enable_interface() - Enslave hard interface to soft interface
  590. * @hard_iface: hard interface to add to soft interface
  591. * @net: the applicable net namespace
  592. * @iface_name: name of the soft interface
  593. *
  594. * Return: 0 on success or negative error number in case of failure
  595. */
  596. int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
  597. struct net *net, const char *iface_name)
  598. {
  599. struct batadv_priv *bat_priv;
  600. struct net_device *soft_iface, *master;
  601. __be16 ethertype = htons(ETH_P_BATMAN);
  602. int max_header_len = batadv_max_header_len();
  603. int ret;
  604. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  605. goto out;
  606. kref_get(&hard_iface->refcount);
  607. soft_iface = dev_get_by_name(net, iface_name);
  608. if (!soft_iface) {
  609. soft_iface = batadv_softif_create(net, iface_name);
  610. if (!soft_iface) {
  611. ret = -ENOMEM;
  612. goto err;
  613. }
  614. /* dev_get_by_name() increases the reference counter for us */
  615. dev_hold(soft_iface);
  616. }
  617. if (!batadv_softif_is_valid(soft_iface)) {
  618. pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
  619. soft_iface->name);
  620. ret = -EINVAL;
  621. goto err_dev;
  622. }
  623. /* check if the interface is enslaved in another virtual one and
  624. * in that case unlink it first
  625. */
  626. master = netdev_master_upper_dev_get(hard_iface->net_dev);
  627. ret = batadv_master_del_slave(hard_iface, master);
  628. if (ret)
  629. goto err_dev;
  630. hard_iface->soft_iface = soft_iface;
  631. bat_priv = netdev_priv(hard_iface->soft_iface);
  632. if (bat_priv->num_ifaces >= UINT_MAX) {
  633. ret = -ENOSPC;
  634. goto err_dev;
  635. }
  636. ret = netdev_master_upper_dev_link(hard_iface->net_dev,
  637. soft_iface, NULL, NULL, NULL);
  638. if (ret)
  639. goto err_dev;
  640. ret = bat_priv->algo_ops->iface.enable(hard_iface);
  641. if (ret < 0)
  642. goto err_upper;
  643. hard_iface->if_num = bat_priv->num_ifaces;
  644. bat_priv->num_ifaces++;
  645. hard_iface->if_status = BATADV_IF_INACTIVE;
  646. ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
  647. if (ret < 0) {
  648. bat_priv->algo_ops->iface.disable(hard_iface);
  649. bat_priv->num_ifaces--;
  650. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  651. goto err_upper;
  652. }
  653. kref_get(&hard_iface->refcount);
  654. hard_iface->batman_adv_ptype.type = ethertype;
  655. hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
  656. hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
  657. dev_add_pack(&hard_iface->batman_adv_ptype);
  658. batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
  659. hard_iface->net_dev->name);
  660. if (atomic_read(&bat_priv->fragmentation) &&
  661. hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
  662. batadv_info(hard_iface->soft_iface,
  663. "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",
  664. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  665. ETH_DATA_LEN + max_header_len);
  666. if (!atomic_read(&bat_priv->fragmentation) &&
  667. hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
  668. batadv_info(hard_iface->soft_iface,
  669. "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",
  670. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  671. ETH_DATA_LEN + max_header_len);
  672. if (batadv_hardif_is_iface_up(hard_iface))
  673. batadv_hardif_activate_interface(hard_iface);
  674. else
  675. batadv_err(hard_iface->soft_iface,
  676. "Not using interface %s (retrying later): interface not active\n",
  677. hard_iface->net_dev->name);
  678. batadv_hardif_recalc_extra_skbroom(soft_iface);
  679. out:
  680. return 0;
  681. err_upper:
  682. netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
  683. err_dev:
  684. hard_iface->soft_iface = NULL;
  685. dev_put(soft_iface);
  686. err:
  687. batadv_hardif_put(hard_iface);
  688. return ret;
  689. }
  690. /**
  691. * batadv_hardif_disable_interface() - Remove hard interface from soft interface
  692. * @hard_iface: hard interface to be removed
  693. * @autodel: whether to delete soft interface when it doesn't contain any other
  694. * slave interfaces
  695. */
  696. void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
  697. enum batadv_hard_if_cleanup autodel)
  698. {
  699. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  700. struct batadv_hard_iface *primary_if = NULL;
  701. batadv_hardif_deactivate_interface(hard_iface);
  702. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  703. goto out;
  704. batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
  705. hard_iface->net_dev->name);
  706. dev_remove_pack(&hard_iface->batman_adv_ptype);
  707. batadv_hardif_put(hard_iface);
  708. bat_priv->num_ifaces--;
  709. batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
  710. primary_if = batadv_primary_if_get_selected(bat_priv);
  711. if (hard_iface == primary_if) {
  712. struct batadv_hard_iface *new_if;
  713. new_if = batadv_hardif_get_active(hard_iface->soft_iface);
  714. batadv_primary_if_select(bat_priv, new_if);
  715. if (new_if)
  716. batadv_hardif_put(new_if);
  717. }
  718. bat_priv->algo_ops->iface.disable(hard_iface);
  719. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  720. /* delete all references to this hard_iface */
  721. batadv_purge_orig_ref(bat_priv);
  722. batadv_purge_outstanding_packets(bat_priv, hard_iface);
  723. dev_put(hard_iface->soft_iface);
  724. netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
  725. batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
  726. /* nobody uses this interface anymore */
  727. if (bat_priv->num_ifaces == 0) {
  728. batadv_gw_check_client_stop(bat_priv);
  729. if (autodel == BATADV_IF_CLEANUP_AUTO)
  730. batadv_softif_destroy_sysfs(hard_iface->soft_iface);
  731. }
  732. hard_iface->soft_iface = NULL;
  733. batadv_hardif_put(hard_iface);
  734. out:
  735. if (primary_if)
  736. batadv_hardif_put(primary_if);
  737. }
  738. static struct batadv_hard_iface *
  739. batadv_hardif_add_interface(struct net_device *net_dev)
  740. {
  741. struct batadv_hard_iface *hard_iface;
  742. int ret;
  743. ASSERT_RTNL();
  744. if (!batadv_is_valid_iface(net_dev))
  745. goto out;
  746. dev_hold(net_dev);
  747. hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
  748. if (!hard_iface)
  749. goto release_dev;
  750. ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
  751. if (ret)
  752. goto free_if;
  753. hard_iface->if_num = 0;
  754. hard_iface->net_dev = net_dev;
  755. hard_iface->soft_iface = NULL;
  756. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  757. ret = batadv_debugfs_add_hardif(hard_iface);
  758. if (ret)
  759. goto free_sysfs;
  760. INIT_LIST_HEAD(&hard_iface->list);
  761. INIT_HLIST_HEAD(&hard_iface->neigh_list);
  762. spin_lock_init(&hard_iface->neigh_list_lock);
  763. kref_init(&hard_iface->refcount);
  764. hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
  765. hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev);
  766. if (batadv_is_wifi_hardif(hard_iface))
  767. hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
  768. batadv_v_hardif_init(hard_iface);
  769. batadv_check_known_mac_addr(hard_iface->net_dev);
  770. kref_get(&hard_iface->refcount);
  771. list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
  772. return hard_iface;
  773. free_sysfs:
  774. batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
  775. free_if:
  776. kfree(hard_iface);
  777. release_dev:
  778. dev_put(net_dev);
  779. out:
  780. return NULL;
  781. }
  782. static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
  783. {
  784. ASSERT_RTNL();
  785. /* first deactivate interface */
  786. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  787. batadv_hardif_disable_interface(hard_iface,
  788. BATADV_IF_CLEANUP_KEEP);
  789. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  790. return;
  791. hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
  792. batadv_debugfs_del_hardif(hard_iface);
  793. batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
  794. batadv_hardif_put(hard_iface);
  795. }
  796. /**
  797. * batadv_hardif_remove_interfaces() - Remove all hard interfaces
  798. */
  799. void batadv_hardif_remove_interfaces(void)
  800. {
  801. struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
  802. rtnl_lock();
  803. list_for_each_entry_safe(hard_iface, hard_iface_tmp,
  804. &batadv_hardif_list, list) {
  805. list_del_rcu(&hard_iface->list);
  806. batadv_hardif_remove_interface(hard_iface);
  807. }
  808. rtnl_unlock();
  809. }
  810. static int batadv_hard_if_event(struct notifier_block *this,
  811. unsigned long event, void *ptr)
  812. {
  813. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  814. struct batadv_hard_iface *hard_iface;
  815. struct batadv_hard_iface *primary_if = NULL;
  816. struct batadv_priv *bat_priv;
  817. if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
  818. batadv_sysfs_add_meshif(net_dev);
  819. bat_priv = netdev_priv(net_dev);
  820. batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
  821. return NOTIFY_DONE;
  822. }
  823. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  824. if (!hard_iface && (event == NETDEV_REGISTER ||
  825. event == NETDEV_POST_TYPE_CHANGE))
  826. hard_iface = batadv_hardif_add_interface(net_dev);
  827. if (!hard_iface)
  828. goto out;
  829. switch (event) {
  830. case NETDEV_UP:
  831. batadv_hardif_activate_interface(hard_iface);
  832. break;
  833. case NETDEV_GOING_DOWN:
  834. case NETDEV_DOWN:
  835. batadv_hardif_deactivate_interface(hard_iface);
  836. break;
  837. case NETDEV_UNREGISTER:
  838. case NETDEV_PRE_TYPE_CHANGE:
  839. list_del_rcu(&hard_iface->list);
  840. batadv_hardif_remove_interface(hard_iface);
  841. break;
  842. case NETDEV_CHANGEMTU:
  843. if (hard_iface->soft_iface)
  844. batadv_update_min_mtu(hard_iface->soft_iface);
  845. break;
  846. case NETDEV_CHANGEADDR:
  847. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  848. goto hardif_put;
  849. batadv_check_known_mac_addr(hard_iface->net_dev);
  850. bat_priv = netdev_priv(hard_iface->soft_iface);
  851. bat_priv->algo_ops->iface.update_mac(hard_iface);
  852. primary_if = batadv_primary_if_get_selected(bat_priv);
  853. if (!primary_if)
  854. goto hardif_put;
  855. if (hard_iface == primary_if)
  856. batadv_primary_if_update_addr(bat_priv, NULL);
  857. break;
  858. case NETDEV_CHANGEUPPER:
  859. hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev);
  860. if (batadv_is_wifi_hardif(hard_iface))
  861. hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
  862. break;
  863. default:
  864. break;
  865. }
  866. hardif_put:
  867. batadv_hardif_put(hard_iface);
  868. out:
  869. if (primary_if)
  870. batadv_hardif_put(primary_if);
  871. return NOTIFY_DONE;
  872. }
  873. struct notifier_block batadv_hard_if_notifier = {
  874. .notifier_call = batadv_hard_if_event,
  875. };