hard-interface.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2007-2018 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/byteorder/generic.h>
  22. #include <linux/errno.h>
  23. #include <linux/gfp.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/mutex.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 (!parent_dev) {
  161. pr_err("Cannot find parent device\n");
  162. return false;
  163. }
  164. if (batadv_mutual_parents(net_dev, net, parent_dev, parent_net))
  165. return false;
  166. ret = batadv_is_on_batman_iface(parent_dev);
  167. return ret;
  168. }
  169. static bool batadv_is_valid_iface(const struct net_device *net_dev)
  170. {
  171. if (net_dev->flags & IFF_LOOPBACK)
  172. return false;
  173. if (net_dev->type != ARPHRD_ETHER)
  174. return false;
  175. if (net_dev->addr_len != ETH_ALEN)
  176. return false;
  177. /* no batman over batman */
  178. if (batadv_is_on_batman_iface(net_dev))
  179. return false;
  180. return true;
  181. }
  182. /**
  183. * batadv_get_real_netdevice() - check if the given netdev struct is a virtual
  184. * interface on top of another 'real' interface
  185. * @netdev: the device to check
  186. *
  187. * Callers must hold the rtnl semaphore. You may want batadv_get_real_netdev()
  188. * instead of this.
  189. *
  190. * Return: the 'real' net device or the original net device and NULL in case
  191. * of an error.
  192. */
  193. static struct net_device *batadv_get_real_netdevice(struct net_device *netdev)
  194. {
  195. struct batadv_hard_iface *hard_iface = NULL;
  196. struct net_device *real_netdev = NULL;
  197. struct net *real_net;
  198. struct net *net;
  199. int ifindex;
  200. ASSERT_RTNL();
  201. if (!netdev)
  202. return NULL;
  203. if (netdev->ifindex == dev_get_iflink(netdev)) {
  204. dev_hold(netdev);
  205. return netdev;
  206. }
  207. hard_iface = batadv_hardif_get_by_netdev(netdev);
  208. if (!hard_iface || !hard_iface->soft_iface)
  209. goto out;
  210. net = dev_net(hard_iface->soft_iface);
  211. ifindex = dev_get_iflink(netdev);
  212. real_net = batadv_getlink_net(netdev, net);
  213. real_netdev = dev_get_by_index(real_net, ifindex);
  214. out:
  215. if (hard_iface)
  216. batadv_hardif_put(hard_iface);
  217. return real_netdev;
  218. }
  219. /**
  220. * batadv_get_real_netdev() - check if the given net_device struct is a virtual
  221. * interface on top of another 'real' interface
  222. * @net_device: the device to check
  223. *
  224. * Return: the 'real' net device or the original net device and NULL in case
  225. * of an error.
  226. */
  227. struct net_device *batadv_get_real_netdev(struct net_device *net_device)
  228. {
  229. struct net_device *real_netdev;
  230. rtnl_lock();
  231. real_netdev = batadv_get_real_netdevice(net_device);
  232. rtnl_unlock();
  233. return real_netdev;
  234. }
  235. /**
  236. * batadv_is_wext_netdev() - check if the given net_device struct is a
  237. * wext wifi interface
  238. * @net_device: the device to check
  239. *
  240. * Return: true if the net device is a wext wireless device, false
  241. * otherwise.
  242. */
  243. static bool batadv_is_wext_netdev(struct net_device *net_device)
  244. {
  245. if (!net_device)
  246. return false;
  247. #ifdef CONFIG_WIRELESS_EXT
  248. /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
  249. * check for wireless_handlers != NULL
  250. */
  251. if (net_device->wireless_handlers)
  252. return true;
  253. #endif
  254. return false;
  255. }
  256. /**
  257. * batadv_is_cfg80211_netdev() - check if the given net_device struct is a
  258. * cfg80211 wifi interface
  259. * @net_device: the device to check
  260. *
  261. * Return: true if the net device is a cfg80211 wireless device, false
  262. * otherwise.
  263. */
  264. static bool batadv_is_cfg80211_netdev(struct net_device *net_device)
  265. {
  266. if (!net_device)
  267. return false;
  268. /* cfg80211 drivers have to set ieee80211_ptr */
  269. if (net_device->ieee80211_ptr)
  270. return true;
  271. return false;
  272. }
  273. /**
  274. * batadv_wifi_flags_evaluate() - calculate wifi flags for net_device
  275. * @net_device: the device to check
  276. *
  277. * Return: batadv_hard_iface_wifi_flags flags of the device
  278. */
  279. static u32 batadv_wifi_flags_evaluate(struct net_device *net_device)
  280. {
  281. u32 wifi_flags = 0;
  282. struct net_device *real_netdev;
  283. if (batadv_is_wext_netdev(net_device))
  284. wifi_flags |= BATADV_HARDIF_WIFI_WEXT_DIRECT;
  285. if (batadv_is_cfg80211_netdev(net_device))
  286. wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT;
  287. real_netdev = batadv_get_real_netdevice(net_device);
  288. if (!real_netdev)
  289. return wifi_flags;
  290. if (real_netdev == net_device)
  291. goto out;
  292. if (batadv_is_wext_netdev(real_netdev))
  293. wifi_flags |= BATADV_HARDIF_WIFI_WEXT_INDIRECT;
  294. if (batadv_is_cfg80211_netdev(real_netdev))
  295. wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT;
  296. out:
  297. dev_put(real_netdev);
  298. return wifi_flags;
  299. }
  300. /**
  301. * batadv_is_cfg80211_hardif() - check if the given hardif is a cfg80211 wifi
  302. * interface
  303. * @hard_iface: the device to check
  304. *
  305. * Return: true if the net device is a cfg80211 wireless device, false
  306. * otherwise.
  307. */
  308. bool batadv_is_cfg80211_hardif(struct batadv_hard_iface *hard_iface)
  309. {
  310. u32 allowed_flags = 0;
  311. allowed_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT;
  312. allowed_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT;
  313. return !!(hard_iface->wifi_flags & allowed_flags);
  314. }
  315. /**
  316. * batadv_is_wifi_hardif() - check if the given hardif is a wifi interface
  317. * @hard_iface: the device to check
  318. *
  319. * Return: true if the net device is a 802.11 wireless device, false otherwise.
  320. */
  321. bool batadv_is_wifi_hardif(struct batadv_hard_iface *hard_iface)
  322. {
  323. if (!hard_iface)
  324. return false;
  325. return hard_iface->wifi_flags != 0;
  326. }
  327. /**
  328. * batadv_hardif_no_broadcast() - check whether (re)broadcast is necessary
  329. * @if_outgoing: the outgoing interface checked and considered for (re)broadcast
  330. * @orig_addr: the originator of this packet
  331. * @orig_neigh: originator address of the forwarder we just got the packet from
  332. * (NULL if we originated)
  333. *
  334. * Checks whether a packet needs to be (re)broadcasted on the given interface.
  335. *
  336. * Return:
  337. * BATADV_HARDIF_BCAST_NORECIPIENT: No neighbor on interface
  338. * BATADV_HARDIF_BCAST_DUPFWD: Just one neighbor, but it is the forwarder
  339. * BATADV_HARDIF_BCAST_DUPORIG: Just one neighbor, but it is the originator
  340. * BATADV_HARDIF_BCAST_OK: Several neighbors, must broadcast
  341. */
  342. int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing,
  343. u8 *orig_addr, u8 *orig_neigh)
  344. {
  345. struct batadv_hardif_neigh_node *hardif_neigh;
  346. struct hlist_node *first;
  347. int ret = BATADV_HARDIF_BCAST_OK;
  348. rcu_read_lock();
  349. /* 0 neighbors -> no (re)broadcast */
  350. first = rcu_dereference(hlist_first_rcu(&if_outgoing->neigh_list));
  351. if (!first) {
  352. ret = BATADV_HARDIF_BCAST_NORECIPIENT;
  353. goto out;
  354. }
  355. /* >1 neighbors -> (re)brodcast */
  356. if (rcu_dereference(hlist_next_rcu(first)))
  357. goto out;
  358. hardif_neigh = hlist_entry(first, struct batadv_hardif_neigh_node,
  359. list);
  360. /* 1 neighbor, is the originator -> no rebroadcast */
  361. if (orig_addr && batadv_compare_eth(hardif_neigh->orig, orig_addr)) {
  362. ret = BATADV_HARDIF_BCAST_DUPORIG;
  363. /* 1 neighbor, is the one we received from -> no rebroadcast */
  364. } else if (orig_neigh &&
  365. batadv_compare_eth(hardif_neigh->orig, orig_neigh)) {
  366. ret = BATADV_HARDIF_BCAST_DUPFWD;
  367. }
  368. out:
  369. rcu_read_unlock();
  370. return ret;
  371. }
  372. static struct batadv_hard_iface *
  373. batadv_hardif_get_active(const struct net_device *soft_iface)
  374. {
  375. struct batadv_hard_iface *hard_iface;
  376. rcu_read_lock();
  377. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  378. if (hard_iface->soft_iface != soft_iface)
  379. continue;
  380. if (hard_iface->if_status == BATADV_IF_ACTIVE &&
  381. kref_get_unless_zero(&hard_iface->refcount))
  382. goto out;
  383. }
  384. hard_iface = NULL;
  385. out:
  386. rcu_read_unlock();
  387. return hard_iface;
  388. }
  389. static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
  390. struct batadv_hard_iface *oldif)
  391. {
  392. struct batadv_hard_iface *primary_if;
  393. primary_if = batadv_primary_if_get_selected(bat_priv);
  394. if (!primary_if)
  395. goto out;
  396. batadv_dat_init_own_addr(bat_priv, primary_if);
  397. batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
  398. out:
  399. if (primary_if)
  400. batadv_hardif_put(primary_if);
  401. }
  402. static void batadv_primary_if_select(struct batadv_priv *bat_priv,
  403. struct batadv_hard_iface *new_hard_iface)
  404. {
  405. struct batadv_hard_iface *curr_hard_iface;
  406. ASSERT_RTNL();
  407. if (new_hard_iface)
  408. kref_get(&new_hard_iface->refcount);
  409. curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
  410. rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
  411. if (!new_hard_iface)
  412. goto out;
  413. bat_priv->algo_ops->iface.primary_set(new_hard_iface);
  414. batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
  415. out:
  416. if (curr_hard_iface)
  417. batadv_hardif_put(curr_hard_iface);
  418. }
  419. static bool
  420. batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
  421. {
  422. if (hard_iface->net_dev->flags & IFF_UP)
  423. return true;
  424. return false;
  425. }
  426. static void batadv_check_known_mac_addr(const struct net_device *net_dev)
  427. {
  428. const struct batadv_hard_iface *hard_iface;
  429. rcu_read_lock();
  430. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  431. if (hard_iface->if_status != BATADV_IF_ACTIVE &&
  432. hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
  433. continue;
  434. if (hard_iface->net_dev == net_dev)
  435. continue;
  436. if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
  437. net_dev->dev_addr))
  438. continue;
  439. pr_warn("The newly added mac address (%pM) already exists on: %s\n",
  440. net_dev->dev_addr, hard_iface->net_dev->name);
  441. pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
  442. }
  443. rcu_read_unlock();
  444. }
  445. /**
  446. * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
  447. * @soft_iface: netdev struct of the mesh interface
  448. */
  449. static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
  450. {
  451. const struct batadv_hard_iface *hard_iface;
  452. unsigned short lower_header_len = ETH_HLEN;
  453. unsigned short lower_headroom = 0;
  454. unsigned short lower_tailroom = 0;
  455. unsigned short needed_headroom;
  456. rcu_read_lock();
  457. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  458. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  459. continue;
  460. if (hard_iface->soft_iface != soft_iface)
  461. continue;
  462. lower_header_len = max_t(unsigned short, lower_header_len,
  463. hard_iface->net_dev->hard_header_len);
  464. lower_headroom = max_t(unsigned short, lower_headroom,
  465. hard_iface->net_dev->needed_headroom);
  466. lower_tailroom = max_t(unsigned short, lower_tailroom,
  467. hard_iface->net_dev->needed_tailroom);
  468. }
  469. rcu_read_unlock();
  470. needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
  471. needed_headroom += batadv_max_header_len();
  472. soft_iface->needed_headroom = needed_headroom;
  473. soft_iface->needed_tailroom = lower_tailroom;
  474. }
  475. /**
  476. * batadv_hardif_min_mtu() - Calculate maximum MTU for soft interface
  477. * @soft_iface: netdev struct of the soft interface
  478. *
  479. * Return: MTU for the soft-interface (limited by the minimal MTU of all active
  480. * slave interfaces)
  481. */
  482. int batadv_hardif_min_mtu(struct net_device *soft_iface)
  483. {
  484. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  485. const struct batadv_hard_iface *hard_iface;
  486. int min_mtu = INT_MAX;
  487. rcu_read_lock();
  488. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  489. if (hard_iface->if_status != BATADV_IF_ACTIVE &&
  490. hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
  491. continue;
  492. if (hard_iface->soft_iface != soft_iface)
  493. continue;
  494. min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
  495. }
  496. rcu_read_unlock();
  497. if (atomic_read(&bat_priv->fragmentation) == 0)
  498. goto out;
  499. /* with fragmentation enabled the maximum size of internally generated
  500. * packets such as translation table exchanges or tvlv containers, etc
  501. * has to be calculated
  502. */
  503. min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
  504. min_mtu -= sizeof(struct batadv_frag_packet);
  505. min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
  506. out:
  507. /* report to the other components the maximum amount of bytes that
  508. * batman-adv can send over the wire (without considering the payload
  509. * overhead). For example, this value is used by TT to compute the
  510. * maximum local table table size
  511. */
  512. atomic_set(&bat_priv->packet_size_max, min_mtu);
  513. /* the real soft-interface MTU is computed by removing the payload
  514. * overhead from the maximum amount of bytes that was just computed.
  515. *
  516. * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
  517. */
  518. return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
  519. }
  520. /**
  521. * batadv_update_min_mtu() - Adjusts the MTU if a new interface with a smaller
  522. * MTU appeared
  523. * @soft_iface: netdev struct of the soft interface
  524. */
  525. void batadv_update_min_mtu(struct net_device *soft_iface)
  526. {
  527. soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
  528. /* Check if the local translate table should be cleaned up to match a
  529. * new (and smaller) MTU.
  530. */
  531. batadv_tt_local_resize_to_mtu(soft_iface);
  532. }
  533. static void
  534. batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
  535. {
  536. struct batadv_priv *bat_priv;
  537. struct batadv_hard_iface *primary_if = NULL;
  538. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  539. goto out;
  540. bat_priv = netdev_priv(hard_iface->soft_iface);
  541. bat_priv->algo_ops->iface.update_mac(hard_iface);
  542. hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
  543. /* the first active interface becomes our primary interface or
  544. * the next active interface after the old primary interface was removed
  545. */
  546. primary_if = batadv_primary_if_get_selected(bat_priv);
  547. if (!primary_if)
  548. batadv_primary_if_select(bat_priv, hard_iface);
  549. batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
  550. hard_iface->net_dev->name);
  551. batadv_update_min_mtu(hard_iface->soft_iface);
  552. if (bat_priv->algo_ops->iface.activate)
  553. bat_priv->algo_ops->iface.activate(hard_iface);
  554. out:
  555. if (primary_if)
  556. batadv_hardif_put(primary_if);
  557. }
  558. static void
  559. batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
  560. {
  561. if (hard_iface->if_status != BATADV_IF_ACTIVE &&
  562. hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
  563. return;
  564. hard_iface->if_status = BATADV_IF_INACTIVE;
  565. batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
  566. hard_iface->net_dev->name);
  567. batadv_update_min_mtu(hard_iface->soft_iface);
  568. }
  569. /**
  570. * batadv_master_del_slave() - remove hard_iface from the current master iface
  571. * @slave: the interface enslaved in another master
  572. * @master: the master from which slave has to be removed
  573. *
  574. * Invoke ndo_del_slave on master passing slave as argument. In this way slave
  575. * is free'd and master can correctly change its internal state.
  576. *
  577. * Return: 0 on success, a negative value representing the error otherwise
  578. */
  579. static int batadv_master_del_slave(struct batadv_hard_iface *slave,
  580. struct net_device *master)
  581. {
  582. int ret;
  583. if (!master)
  584. return 0;
  585. ret = -EBUSY;
  586. if (master->netdev_ops->ndo_del_slave)
  587. ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
  588. return ret;
  589. }
  590. /**
  591. * batadv_hardif_enable_interface() - Enslave hard interface to soft interface
  592. * @hard_iface: hard interface to add to soft interface
  593. * @net: the applicable net namespace
  594. * @iface_name: name of the soft interface
  595. *
  596. * Return: 0 on success or negative error number in case of failure
  597. */
  598. int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
  599. struct net *net, const char *iface_name)
  600. {
  601. struct batadv_priv *bat_priv;
  602. struct net_device *soft_iface, *master;
  603. __be16 ethertype = htons(ETH_P_BATMAN);
  604. int max_header_len = batadv_max_header_len();
  605. int ret;
  606. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  607. goto out;
  608. kref_get(&hard_iface->refcount);
  609. soft_iface = dev_get_by_name(net, iface_name);
  610. if (!soft_iface) {
  611. soft_iface = batadv_softif_create(net, iface_name);
  612. if (!soft_iface) {
  613. ret = -ENOMEM;
  614. goto err;
  615. }
  616. /* dev_get_by_name() increases the reference counter for us */
  617. dev_hold(soft_iface);
  618. }
  619. if (!batadv_softif_is_valid(soft_iface)) {
  620. pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
  621. soft_iface->name);
  622. ret = -EINVAL;
  623. goto err_dev;
  624. }
  625. /* check if the interface is enslaved in another virtual one and
  626. * in that case unlink it first
  627. */
  628. master = netdev_master_upper_dev_get(hard_iface->net_dev);
  629. ret = batadv_master_del_slave(hard_iface, master);
  630. if (ret)
  631. goto err_dev;
  632. hard_iface->soft_iface = soft_iface;
  633. bat_priv = netdev_priv(hard_iface->soft_iface);
  634. if (bat_priv->num_ifaces >= UINT_MAX) {
  635. ret = -ENOSPC;
  636. goto err_dev;
  637. }
  638. ret = netdev_master_upper_dev_link(hard_iface->net_dev,
  639. soft_iface, NULL, NULL, NULL);
  640. if (ret)
  641. goto err_dev;
  642. ret = bat_priv->algo_ops->iface.enable(hard_iface);
  643. if (ret < 0)
  644. goto err_upper;
  645. hard_iface->if_num = bat_priv->num_ifaces;
  646. bat_priv->num_ifaces++;
  647. hard_iface->if_status = BATADV_IF_INACTIVE;
  648. ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
  649. if (ret < 0) {
  650. bat_priv->algo_ops->iface.disable(hard_iface);
  651. bat_priv->num_ifaces--;
  652. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  653. goto err_upper;
  654. }
  655. kref_get(&hard_iface->refcount);
  656. hard_iface->batman_adv_ptype.type = ethertype;
  657. hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
  658. hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
  659. dev_add_pack(&hard_iface->batman_adv_ptype);
  660. batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
  661. hard_iface->net_dev->name);
  662. if (atomic_read(&bat_priv->fragmentation) &&
  663. hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
  664. batadv_info(hard_iface->soft_iface,
  665. "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",
  666. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  667. ETH_DATA_LEN + max_header_len);
  668. if (!atomic_read(&bat_priv->fragmentation) &&
  669. hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
  670. batadv_info(hard_iface->soft_iface,
  671. "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",
  672. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  673. ETH_DATA_LEN + max_header_len);
  674. if (batadv_hardif_is_iface_up(hard_iface))
  675. batadv_hardif_activate_interface(hard_iface);
  676. else
  677. batadv_err(hard_iface->soft_iface,
  678. "Not using interface %s (retrying later): interface not active\n",
  679. hard_iface->net_dev->name);
  680. batadv_hardif_recalc_extra_skbroom(soft_iface);
  681. if (bat_priv->algo_ops->iface.enabled)
  682. bat_priv->algo_ops->iface.enabled(hard_iface);
  683. out:
  684. return 0;
  685. err_upper:
  686. netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
  687. err_dev:
  688. hard_iface->soft_iface = NULL;
  689. dev_put(soft_iface);
  690. err:
  691. batadv_hardif_put(hard_iface);
  692. return ret;
  693. }
  694. /**
  695. * batadv_hardif_disable_interface() - Remove hard interface from soft interface
  696. * @hard_iface: hard interface to be removed
  697. * @autodel: whether to delete soft interface when it doesn't contain any other
  698. * slave interfaces
  699. */
  700. void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
  701. enum batadv_hard_if_cleanup autodel)
  702. {
  703. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  704. struct batadv_hard_iface *primary_if = NULL;
  705. batadv_hardif_deactivate_interface(hard_iface);
  706. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  707. goto out;
  708. batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
  709. hard_iface->net_dev->name);
  710. dev_remove_pack(&hard_iface->batman_adv_ptype);
  711. batadv_hardif_put(hard_iface);
  712. bat_priv->num_ifaces--;
  713. batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
  714. primary_if = batadv_primary_if_get_selected(bat_priv);
  715. if (hard_iface == primary_if) {
  716. struct batadv_hard_iface *new_if;
  717. new_if = batadv_hardif_get_active(hard_iface->soft_iface);
  718. batadv_primary_if_select(bat_priv, new_if);
  719. if (new_if)
  720. batadv_hardif_put(new_if);
  721. }
  722. bat_priv->algo_ops->iface.disable(hard_iface);
  723. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  724. /* delete all references to this hard_iface */
  725. batadv_purge_orig_ref(bat_priv);
  726. batadv_purge_outstanding_packets(bat_priv, hard_iface);
  727. dev_put(hard_iface->soft_iface);
  728. netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
  729. batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
  730. /* nobody uses this interface anymore */
  731. if (bat_priv->num_ifaces == 0) {
  732. batadv_gw_check_client_stop(bat_priv);
  733. if (autodel == BATADV_IF_CLEANUP_AUTO)
  734. batadv_softif_destroy_sysfs(hard_iface->soft_iface);
  735. }
  736. hard_iface->soft_iface = NULL;
  737. batadv_hardif_put(hard_iface);
  738. out:
  739. if (primary_if)
  740. batadv_hardif_put(primary_if);
  741. }
  742. static struct batadv_hard_iface *
  743. batadv_hardif_add_interface(struct net_device *net_dev)
  744. {
  745. struct batadv_hard_iface *hard_iface;
  746. int ret;
  747. ASSERT_RTNL();
  748. if (!batadv_is_valid_iface(net_dev))
  749. goto out;
  750. dev_hold(net_dev);
  751. hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
  752. if (!hard_iface)
  753. goto release_dev;
  754. ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
  755. if (ret)
  756. goto free_if;
  757. hard_iface->if_num = 0;
  758. hard_iface->net_dev = net_dev;
  759. hard_iface->soft_iface = NULL;
  760. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  761. ret = batadv_debugfs_add_hardif(hard_iface);
  762. if (ret)
  763. goto free_sysfs;
  764. INIT_LIST_HEAD(&hard_iface->list);
  765. INIT_HLIST_HEAD(&hard_iface->neigh_list);
  766. mutex_init(&hard_iface->bat_iv.ogm_buff_mutex);
  767. spin_lock_init(&hard_iface->neigh_list_lock);
  768. kref_init(&hard_iface->refcount);
  769. hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
  770. hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev);
  771. if (batadv_is_wifi_hardif(hard_iface))
  772. hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
  773. batadv_v_hardif_init(hard_iface);
  774. batadv_check_known_mac_addr(hard_iface->net_dev);
  775. kref_get(&hard_iface->refcount);
  776. list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
  777. return hard_iface;
  778. free_sysfs:
  779. batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
  780. free_if:
  781. kfree(hard_iface);
  782. release_dev:
  783. dev_put(net_dev);
  784. out:
  785. return NULL;
  786. }
  787. static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
  788. {
  789. ASSERT_RTNL();
  790. /* first deactivate interface */
  791. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  792. batadv_hardif_disable_interface(hard_iface,
  793. BATADV_IF_CLEANUP_KEEP);
  794. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  795. return;
  796. hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
  797. batadv_debugfs_del_hardif(hard_iface);
  798. batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
  799. batadv_hardif_put(hard_iface);
  800. }
  801. /**
  802. * batadv_hardif_remove_interfaces() - Remove all hard interfaces
  803. */
  804. void batadv_hardif_remove_interfaces(void)
  805. {
  806. struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
  807. rtnl_lock();
  808. list_for_each_entry_safe(hard_iface, hard_iface_tmp,
  809. &batadv_hardif_list, list) {
  810. list_del_rcu(&hard_iface->list);
  811. batadv_hardif_remove_interface(hard_iface);
  812. }
  813. rtnl_unlock();
  814. }
  815. /**
  816. * batadv_hard_if_event_softif() - Handle events for soft interfaces
  817. * @event: NETDEV_* event to handle
  818. * @net_dev: net_device which generated an event
  819. *
  820. * Return: NOTIFY_* result
  821. */
  822. static int batadv_hard_if_event_softif(unsigned long event,
  823. struct net_device *net_dev)
  824. {
  825. struct batadv_priv *bat_priv;
  826. switch (event) {
  827. case NETDEV_REGISTER:
  828. batadv_sysfs_add_meshif(net_dev);
  829. bat_priv = netdev_priv(net_dev);
  830. batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
  831. break;
  832. case NETDEV_CHANGENAME:
  833. batadv_debugfs_rename_meshif(net_dev);
  834. break;
  835. }
  836. return NOTIFY_DONE;
  837. }
  838. static int batadv_hard_if_event(struct notifier_block *this,
  839. unsigned long event, void *ptr)
  840. {
  841. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  842. struct batadv_hard_iface *hard_iface;
  843. struct batadv_hard_iface *primary_if = NULL;
  844. struct batadv_priv *bat_priv;
  845. if (batadv_softif_is_valid(net_dev))
  846. return batadv_hard_if_event_softif(event, net_dev);
  847. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  848. if (!hard_iface && (event == NETDEV_REGISTER ||
  849. event == NETDEV_POST_TYPE_CHANGE))
  850. hard_iface = batadv_hardif_add_interface(net_dev);
  851. if (!hard_iface)
  852. goto out;
  853. switch (event) {
  854. case NETDEV_UP:
  855. batadv_hardif_activate_interface(hard_iface);
  856. break;
  857. case NETDEV_GOING_DOWN:
  858. case NETDEV_DOWN:
  859. batadv_hardif_deactivate_interface(hard_iface);
  860. break;
  861. case NETDEV_UNREGISTER:
  862. case NETDEV_PRE_TYPE_CHANGE:
  863. list_del_rcu(&hard_iface->list);
  864. batadv_hardif_remove_interface(hard_iface);
  865. break;
  866. case NETDEV_CHANGEMTU:
  867. if (hard_iface->soft_iface)
  868. batadv_update_min_mtu(hard_iface->soft_iface);
  869. break;
  870. case NETDEV_CHANGEADDR:
  871. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  872. goto hardif_put;
  873. batadv_check_known_mac_addr(hard_iface->net_dev);
  874. bat_priv = netdev_priv(hard_iface->soft_iface);
  875. bat_priv->algo_ops->iface.update_mac(hard_iface);
  876. primary_if = batadv_primary_if_get_selected(bat_priv);
  877. if (!primary_if)
  878. goto hardif_put;
  879. if (hard_iface == primary_if)
  880. batadv_primary_if_update_addr(bat_priv, NULL);
  881. break;
  882. case NETDEV_CHANGEUPPER:
  883. hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev);
  884. if (batadv_is_wifi_hardif(hard_iface))
  885. hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
  886. break;
  887. case NETDEV_CHANGENAME:
  888. batadv_debugfs_rename_hardif(hard_iface);
  889. break;
  890. default:
  891. break;
  892. }
  893. hardif_put:
  894. batadv_hardif_put(hard_iface);
  895. out:
  896. if (primary_if)
  897. batadv_hardif_put(primary_if);
  898. return NOTIFY_DONE;
  899. }
  900. struct notifier_block batadv_hard_if_notifier = {
  901. .notifier_call = batadv_hard_if_event,
  902. };