hard-interface.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. */
  6. #include "hard-interface.h"
  7. #include "main.h"
  8. #include <linux/atomic.h>
  9. #include <linux/byteorder/generic.h>
  10. #include <linux/compiler.h>
  11. #include <linux/container_of.h>
  12. #include <linux/errno.h>
  13. #include <linux/gfp.h>
  14. #include <linux/if.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/kref.h>
  18. #include <linux/limits.h>
  19. #include <linux/list.h>
  20. #include <linux/minmax.h>
  21. #include <linux/mutex.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/printk.h>
  24. #include <linux/rculist.h>
  25. #include <linux/rtnetlink.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <net/net_namespace.h>
  29. #include <net/rtnetlink.h>
  30. #include <uapi/linux/batadv_packet.h>
  31. #include "bat_v.h"
  32. #include "bridge_loop_avoidance.h"
  33. #include "distributed-arp-table.h"
  34. #include "gateway_client.h"
  35. #include "log.h"
  36. #include "originator.h"
  37. #include "send.h"
  38. #include "soft-interface.h"
  39. #include "translation-table.h"
  40. /**
  41. * batadv_hardif_release() - release hard interface from lists and queue for
  42. * free after rcu grace period
  43. * @ref: kref pointer of the hard interface
  44. */
  45. void batadv_hardif_release(struct kref *ref)
  46. {
  47. struct batadv_hard_iface *hard_iface;
  48. hard_iface = container_of(ref, struct batadv_hard_iface, refcount);
  49. dev_put(hard_iface->net_dev);
  50. kfree_rcu(hard_iface, rcu);
  51. }
  52. /**
  53. * batadv_hardif_get_by_netdev() - Get hard interface object of a net_device
  54. * @net_dev: net_device to search for
  55. *
  56. * Return: batadv_hard_iface of net_dev (with increased refcnt), NULL on errors
  57. */
  58. struct batadv_hard_iface *
  59. batadv_hardif_get_by_netdev(const struct net_device *net_dev)
  60. {
  61. struct batadv_hard_iface *hard_iface;
  62. rcu_read_lock();
  63. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  64. if (hard_iface->net_dev == net_dev &&
  65. kref_get_unless_zero(&hard_iface->refcount))
  66. goto out;
  67. }
  68. hard_iface = NULL;
  69. out:
  70. rcu_read_unlock();
  71. return hard_iface;
  72. }
  73. /**
  74. * batadv_getlink_net() - return link net namespace (of use fallback)
  75. * @netdev: net_device to check
  76. * @fallback_net: return in case get_link_net is not available for @netdev
  77. *
  78. * Return: result of rtnl_link_ops->get_link_net or @fallback_net
  79. */
  80. static struct net *batadv_getlink_net(const struct net_device *netdev,
  81. struct net *fallback_net)
  82. {
  83. if (!netdev->rtnl_link_ops)
  84. return fallback_net;
  85. if (!netdev->rtnl_link_ops->get_link_net)
  86. return fallback_net;
  87. return netdev->rtnl_link_ops->get_link_net(netdev);
  88. }
  89. /**
  90. * batadv_mutual_parents() - check if two devices are each others parent
  91. * @dev1: 1st net dev
  92. * @net1: 1st devices netns
  93. * @dev2: 2nd net dev
  94. * @net2: 2nd devices netns
  95. *
  96. * veth devices come in pairs and each is the parent of the other!
  97. *
  98. * Return: true if the devices are each others parent, otherwise false
  99. */
  100. static bool batadv_mutual_parents(const struct net_device *dev1,
  101. struct net *net1,
  102. const struct net_device *dev2,
  103. struct net *net2)
  104. {
  105. int dev1_parent_iflink = dev_get_iflink(dev1);
  106. int dev2_parent_iflink = dev_get_iflink(dev2);
  107. const struct net *dev1_parent_net;
  108. const struct net *dev2_parent_net;
  109. dev1_parent_net = batadv_getlink_net(dev1, net1);
  110. dev2_parent_net = batadv_getlink_net(dev2, net2);
  111. if (!dev1_parent_iflink || !dev2_parent_iflink)
  112. return false;
  113. return (dev1_parent_iflink == dev2->ifindex) &&
  114. (dev2_parent_iflink == dev1->ifindex) &&
  115. net_eq(dev1_parent_net, net2) &&
  116. net_eq(dev2_parent_net, net1);
  117. }
  118. /**
  119. * batadv_is_on_batman_iface() - check if a device is a batman iface descendant
  120. * @net_dev: the device to check
  121. *
  122. * If the user creates any virtual device on top of a batman-adv interface, it
  123. * is important to prevent this new interface from being used to create a new
  124. * mesh network (this behaviour would lead to a batman-over-batman
  125. * configuration). This function recursively checks all the fathers of the
  126. * device passed as argument looking for a batman-adv soft interface.
  127. *
  128. * Return: true if the device is descendant of a batman-adv mesh interface (or
  129. * if it is a batman-adv interface itself), false otherwise
  130. */
  131. static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
  132. {
  133. struct net *net = dev_net(net_dev);
  134. struct net_device *parent_dev;
  135. struct net *parent_net;
  136. int iflink;
  137. bool ret;
  138. /* check if this is a batman-adv mesh interface */
  139. if (batadv_softif_is_valid(net_dev))
  140. return true;
  141. iflink = dev_get_iflink(net_dev);
  142. if (iflink == 0)
  143. return false;
  144. parent_net = batadv_getlink_net(net_dev, net);
  145. /* iflink to itself, most likely physical device */
  146. if (net == parent_net && iflink == net_dev->ifindex)
  147. return false;
  148. /* recurse over the parent device */
  149. parent_dev = __dev_get_by_index((struct net *)parent_net, iflink);
  150. if (!parent_dev) {
  151. pr_warn("Cannot find parent device. Skipping batadv-on-batadv check for %s\n",
  152. net_dev->name);
  153. return false;
  154. }
  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_get_real_netdevice() - check if the given netdev struct is a virtual
  175. * interface on top of another 'real' interface
  176. * @netdev: the device to check
  177. *
  178. * Callers must hold the rtnl semaphore. You may want batadv_get_real_netdev()
  179. * instead of this.
  180. *
  181. * Return: the 'real' net device or the original net device and NULL in case
  182. * of an error.
  183. */
  184. static struct net_device *batadv_get_real_netdevice(struct net_device *netdev)
  185. {
  186. struct batadv_hard_iface *hard_iface = NULL;
  187. struct net_device *real_netdev = NULL;
  188. struct net *real_net;
  189. struct net *net;
  190. int iflink;
  191. ASSERT_RTNL();
  192. if (!netdev)
  193. return NULL;
  194. iflink = dev_get_iflink(netdev);
  195. if (iflink == 0) {
  196. dev_hold(netdev);
  197. return netdev;
  198. }
  199. hard_iface = batadv_hardif_get_by_netdev(netdev);
  200. if (!hard_iface || !hard_iface->soft_iface)
  201. goto out;
  202. net = dev_net(hard_iface->soft_iface);
  203. real_net = batadv_getlink_net(netdev, net);
  204. /* iflink to itself, most likely physical device */
  205. if (net == real_net && netdev->ifindex == iflink) {
  206. real_netdev = netdev;
  207. dev_hold(real_netdev);
  208. goto out;
  209. }
  210. real_netdev = dev_get_by_index(real_net, iflink);
  211. out:
  212. batadv_hardif_put(hard_iface);
  213. return real_netdev;
  214. }
  215. /**
  216. * batadv_get_real_netdev() - check if the given net_device struct is a virtual
  217. * interface on top of another 'real' interface
  218. * @net_device: the device to check
  219. *
  220. * Return: the 'real' net device or the original net device and NULL in case
  221. * of an error.
  222. */
  223. struct net_device *batadv_get_real_netdev(struct net_device *net_device)
  224. {
  225. struct net_device *real_netdev;
  226. rtnl_lock();
  227. real_netdev = batadv_get_real_netdevice(net_device);
  228. rtnl_unlock();
  229. return real_netdev;
  230. }
  231. /**
  232. * batadv_is_wext_netdev() - check if the given net_device struct is a
  233. * wext wifi interface
  234. * @net_device: the device to check
  235. *
  236. * Return: true if the net device is a wext wireless device, false
  237. * otherwise.
  238. */
  239. static bool batadv_is_wext_netdev(struct net_device *net_device)
  240. {
  241. if (!net_device)
  242. return false;
  243. #ifdef CONFIG_WIRELESS_EXT
  244. /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
  245. * check for wireless_handlers != NULL
  246. */
  247. if (net_device->wireless_handlers)
  248. return true;
  249. #endif
  250. return false;
  251. }
  252. /**
  253. * batadv_is_cfg80211_netdev() - check if the given net_device struct is a
  254. * cfg80211 wifi interface
  255. * @net_device: the device to check
  256. *
  257. * Return: true if the net device is a cfg80211 wireless device, false
  258. * otherwise.
  259. */
  260. static bool batadv_is_cfg80211_netdev(struct net_device *net_device)
  261. {
  262. if (!net_device)
  263. return false;
  264. #if IS_ENABLED(CONFIG_CFG80211)
  265. /* cfg80211 drivers have to set ieee80211_ptr */
  266. if (net_device->ieee80211_ptr)
  267. return true;
  268. #endif
  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)broadcast */
  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. batadv_hardif_put(primary_if);
  398. }
  399. static void batadv_primary_if_select(struct batadv_priv *bat_priv,
  400. struct batadv_hard_iface *new_hard_iface)
  401. {
  402. struct batadv_hard_iface *curr_hard_iface;
  403. ASSERT_RTNL();
  404. if (new_hard_iface)
  405. kref_get(&new_hard_iface->refcount);
  406. curr_hard_iface = rcu_replace_pointer(bat_priv->primary_if,
  407. new_hard_iface, 1);
  408. if (!new_hard_iface)
  409. goto out;
  410. bat_priv->algo_ops->iface.primary_set(new_hard_iface);
  411. batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
  412. out:
  413. batadv_hardif_put(curr_hard_iface);
  414. }
  415. static bool
  416. batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
  417. {
  418. if (hard_iface->net_dev->flags & IFF_UP)
  419. return true;
  420. return false;
  421. }
  422. static void batadv_check_known_mac_addr(const struct net_device *net_dev)
  423. {
  424. const struct batadv_hard_iface *hard_iface;
  425. rcu_read_lock();
  426. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  427. if (hard_iface->if_status != BATADV_IF_ACTIVE &&
  428. hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
  429. continue;
  430. if (hard_iface->net_dev == net_dev)
  431. continue;
  432. if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
  433. net_dev->dev_addr))
  434. continue;
  435. pr_warn("The newly added mac address (%pM) already exists on: %s\n",
  436. net_dev->dev_addr, hard_iface->net_dev->name);
  437. pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
  438. }
  439. rcu_read_unlock();
  440. }
  441. /**
  442. * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
  443. * @soft_iface: netdev struct of the mesh interface
  444. */
  445. static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
  446. {
  447. const struct batadv_hard_iface *hard_iface;
  448. unsigned short lower_header_len = ETH_HLEN;
  449. unsigned short lower_headroom = 0;
  450. unsigned short lower_tailroom = 0;
  451. unsigned short needed_headroom;
  452. rcu_read_lock();
  453. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  454. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  455. continue;
  456. if (hard_iface->soft_iface != soft_iface)
  457. continue;
  458. lower_header_len = max_t(unsigned short, lower_header_len,
  459. hard_iface->net_dev->hard_header_len);
  460. lower_headroom = max_t(unsigned short, lower_headroom,
  461. hard_iface->net_dev->needed_headroom);
  462. lower_tailroom = max_t(unsigned short, lower_tailroom,
  463. hard_iface->net_dev->needed_tailroom);
  464. }
  465. rcu_read_unlock();
  466. needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
  467. needed_headroom += batadv_max_header_len();
  468. /* fragmentation headers don't strip the unicast/... header */
  469. needed_headroom += sizeof(struct batadv_frag_packet);
  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 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. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  526. int limit_mtu;
  527. int mtu;
  528. mtu = batadv_hardif_min_mtu(soft_iface);
  529. if (bat_priv->mtu_set_by_user)
  530. limit_mtu = bat_priv->mtu_set_by_user;
  531. else
  532. limit_mtu = ETH_DATA_LEN;
  533. mtu = min(mtu, limit_mtu);
  534. dev_set_mtu(soft_iface, mtu);
  535. /* Check if the local translate table should be cleaned up to match a
  536. * new (and smaller) MTU.
  537. */
  538. batadv_tt_local_resize_to_mtu(soft_iface);
  539. }
  540. static void
  541. batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
  542. {
  543. struct batadv_priv *bat_priv;
  544. struct batadv_hard_iface *primary_if = NULL;
  545. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  546. goto out;
  547. bat_priv = netdev_priv(hard_iface->soft_iface);
  548. bat_priv->algo_ops->iface.update_mac(hard_iface);
  549. hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
  550. /* the first active interface becomes our primary interface or
  551. * the next active interface after the old primary interface was removed
  552. */
  553. primary_if = batadv_primary_if_get_selected(bat_priv);
  554. if (!primary_if)
  555. batadv_primary_if_select(bat_priv, hard_iface);
  556. batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
  557. hard_iface->net_dev->name);
  558. batadv_update_min_mtu(hard_iface->soft_iface);
  559. if (bat_priv->algo_ops->iface.activate)
  560. bat_priv->algo_ops->iface.activate(hard_iface);
  561. out:
  562. batadv_hardif_put(primary_if);
  563. }
  564. static void
  565. batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
  566. {
  567. if (hard_iface->if_status != BATADV_IF_ACTIVE &&
  568. hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
  569. return;
  570. hard_iface->if_status = BATADV_IF_INACTIVE;
  571. batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
  572. hard_iface->net_dev->name);
  573. batadv_update_min_mtu(hard_iface->soft_iface);
  574. }
  575. /**
  576. * batadv_hardif_enable_interface() - Enslave hard interface to soft interface
  577. * @hard_iface: hard interface to add to soft interface
  578. * @soft_iface: netdev struct of the mesh interface
  579. *
  580. * Return: 0 on success or negative error number in case of failure
  581. */
  582. int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
  583. struct net_device *soft_iface)
  584. {
  585. struct batadv_priv *bat_priv;
  586. __be16 ethertype = htons(ETH_P_BATMAN);
  587. int max_header_len = batadv_max_header_len();
  588. unsigned int required_mtu;
  589. unsigned int hardif_mtu;
  590. int ret;
  591. hardif_mtu = READ_ONCE(hard_iface->net_dev->mtu);
  592. required_mtu = READ_ONCE(soft_iface->mtu) + max_header_len;
  593. if (hardif_mtu < ETH_MIN_MTU + max_header_len)
  594. return -EINVAL;
  595. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  596. goto out;
  597. kref_get(&hard_iface->refcount);
  598. dev_hold(soft_iface);
  599. hard_iface->soft_iface = soft_iface;
  600. bat_priv = netdev_priv(hard_iface->soft_iface);
  601. ret = netdev_master_upper_dev_link(hard_iface->net_dev,
  602. soft_iface, NULL, NULL, NULL);
  603. if (ret)
  604. goto err_dev;
  605. ret = bat_priv->algo_ops->iface.enable(hard_iface);
  606. if (ret < 0)
  607. goto err_upper;
  608. hard_iface->if_status = BATADV_IF_INACTIVE;
  609. kref_get(&hard_iface->refcount);
  610. hard_iface->batman_adv_ptype.type = ethertype;
  611. hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
  612. hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
  613. dev_add_pack(&hard_iface->batman_adv_ptype);
  614. batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
  615. hard_iface->net_dev->name);
  616. if (atomic_read(&bat_priv->fragmentation) &&
  617. hardif_mtu < required_mtu)
  618. batadv_info(hard_iface->soft_iface,
  619. "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",
  620. hard_iface->net_dev->name, hardif_mtu,
  621. required_mtu);
  622. if (!atomic_read(&bat_priv->fragmentation) &&
  623. hardif_mtu < required_mtu)
  624. batadv_info(hard_iface->soft_iface,
  625. "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",
  626. hard_iface->net_dev->name, hardif_mtu,
  627. required_mtu);
  628. if (batadv_hardif_is_iface_up(hard_iface))
  629. batadv_hardif_activate_interface(hard_iface);
  630. else
  631. batadv_err(hard_iface->soft_iface,
  632. "Not using interface %s (retrying later): interface not active\n",
  633. hard_iface->net_dev->name);
  634. batadv_hardif_recalc_extra_skbroom(soft_iface);
  635. if (bat_priv->algo_ops->iface.enabled)
  636. bat_priv->algo_ops->iface.enabled(hard_iface);
  637. out:
  638. return 0;
  639. err_upper:
  640. netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
  641. err_dev:
  642. hard_iface->soft_iface = NULL;
  643. dev_put(soft_iface);
  644. batadv_hardif_put(hard_iface);
  645. return ret;
  646. }
  647. /**
  648. * batadv_hardif_cnt() - get number of interfaces enslaved to soft interface
  649. * @soft_iface: soft interface to check
  650. *
  651. * This function is only using RCU for locking - the result can therefore be
  652. * off when another function is modifying the list at the same time. The
  653. * caller can use the rtnl_lock to make sure that the count is accurate.
  654. *
  655. * Return: number of connected/enslaved hard interfaces
  656. */
  657. static size_t batadv_hardif_cnt(const struct net_device *soft_iface)
  658. {
  659. struct batadv_hard_iface *hard_iface;
  660. size_t count = 0;
  661. rcu_read_lock();
  662. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  663. if (hard_iface->soft_iface != soft_iface)
  664. continue;
  665. count++;
  666. }
  667. rcu_read_unlock();
  668. return count;
  669. }
  670. /**
  671. * batadv_hardif_disable_interface() - Remove hard interface from soft interface
  672. * @hard_iface: hard interface to be removed
  673. */
  674. void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface)
  675. {
  676. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  677. struct batadv_hard_iface *primary_if = NULL;
  678. batadv_hardif_deactivate_interface(hard_iface);
  679. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  680. goto out;
  681. batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
  682. hard_iface->net_dev->name);
  683. dev_remove_pack(&hard_iface->batman_adv_ptype);
  684. batadv_hardif_put(hard_iface);
  685. primary_if = batadv_primary_if_get_selected(bat_priv);
  686. if (hard_iface == primary_if) {
  687. struct batadv_hard_iface *new_if;
  688. new_if = batadv_hardif_get_active(hard_iface->soft_iface);
  689. batadv_primary_if_select(bat_priv, new_if);
  690. batadv_hardif_put(new_if);
  691. }
  692. bat_priv->algo_ops->iface.disable(hard_iface);
  693. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  694. /* delete all references to this hard_iface */
  695. batadv_purge_orig_ref(bat_priv);
  696. batadv_purge_outstanding_packets(bat_priv, hard_iface);
  697. dev_put(hard_iface->soft_iface);
  698. netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
  699. batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
  700. /* nobody uses this interface anymore */
  701. if (batadv_hardif_cnt(hard_iface->soft_iface) <= 1)
  702. batadv_gw_check_client_stop(bat_priv);
  703. hard_iface->soft_iface = NULL;
  704. batadv_hardif_put(hard_iface);
  705. out:
  706. batadv_hardif_put(primary_if);
  707. }
  708. static struct batadv_hard_iface *
  709. batadv_hardif_add_interface(struct net_device *net_dev)
  710. {
  711. struct batadv_hard_iface *hard_iface;
  712. ASSERT_RTNL();
  713. if (!batadv_is_valid_iface(net_dev))
  714. goto out;
  715. dev_hold(net_dev);
  716. hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
  717. if (!hard_iface)
  718. goto release_dev;
  719. hard_iface->net_dev = net_dev;
  720. hard_iface->soft_iface = NULL;
  721. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  722. INIT_LIST_HEAD(&hard_iface->list);
  723. INIT_HLIST_HEAD(&hard_iface->neigh_list);
  724. mutex_init(&hard_iface->bat_iv.ogm_buff_mutex);
  725. spin_lock_init(&hard_iface->neigh_list_lock);
  726. kref_init(&hard_iface->refcount);
  727. hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
  728. hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev);
  729. if (batadv_is_wifi_hardif(hard_iface))
  730. hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
  731. atomic_set(&hard_iface->hop_penalty, 0);
  732. batadv_v_hardif_init(hard_iface);
  733. batadv_check_known_mac_addr(hard_iface->net_dev);
  734. kref_get(&hard_iface->refcount);
  735. list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
  736. batadv_hardif_generation++;
  737. return hard_iface;
  738. release_dev:
  739. dev_put(net_dev);
  740. out:
  741. return NULL;
  742. }
  743. static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
  744. {
  745. ASSERT_RTNL();
  746. /* first deactivate interface */
  747. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  748. batadv_hardif_disable_interface(hard_iface);
  749. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  750. return;
  751. hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
  752. batadv_hardif_put(hard_iface);
  753. }
  754. /**
  755. * batadv_hard_if_event_softif() - Handle events for soft interfaces
  756. * @event: NETDEV_* event to handle
  757. * @net_dev: net_device which generated an event
  758. *
  759. * Return: NOTIFY_* result
  760. */
  761. static int batadv_hard_if_event_softif(unsigned long event,
  762. struct net_device *net_dev)
  763. {
  764. struct batadv_priv *bat_priv;
  765. switch (event) {
  766. case NETDEV_REGISTER:
  767. bat_priv = netdev_priv(net_dev);
  768. batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
  769. break;
  770. }
  771. return NOTIFY_DONE;
  772. }
  773. static int batadv_hard_if_event(struct notifier_block *this,
  774. unsigned long event, void *ptr)
  775. {
  776. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  777. struct batadv_hard_iface *hard_iface;
  778. struct batadv_hard_iface *primary_if = NULL;
  779. struct batadv_priv *bat_priv;
  780. if (batadv_softif_is_valid(net_dev))
  781. return batadv_hard_if_event_softif(event, net_dev);
  782. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  783. if (!hard_iface && (event == NETDEV_REGISTER ||
  784. event == NETDEV_POST_TYPE_CHANGE))
  785. hard_iface = batadv_hardif_add_interface(net_dev);
  786. if (!hard_iface)
  787. goto out;
  788. switch (event) {
  789. case NETDEV_UP:
  790. batadv_hardif_activate_interface(hard_iface);
  791. break;
  792. case NETDEV_GOING_DOWN:
  793. case NETDEV_DOWN:
  794. batadv_hardif_deactivate_interface(hard_iface);
  795. break;
  796. case NETDEV_UNREGISTER:
  797. case NETDEV_PRE_TYPE_CHANGE:
  798. list_del_rcu(&hard_iface->list);
  799. batadv_hardif_generation++;
  800. batadv_hardif_remove_interface(hard_iface);
  801. break;
  802. case NETDEV_CHANGEMTU:
  803. if (hard_iface->soft_iface)
  804. batadv_update_min_mtu(hard_iface->soft_iface);
  805. break;
  806. case NETDEV_CHANGEADDR:
  807. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  808. goto hardif_put;
  809. batadv_check_known_mac_addr(hard_iface->net_dev);
  810. bat_priv = netdev_priv(hard_iface->soft_iface);
  811. bat_priv->algo_ops->iface.update_mac(hard_iface);
  812. primary_if = batadv_primary_if_get_selected(bat_priv);
  813. if (!primary_if)
  814. goto hardif_put;
  815. if (hard_iface == primary_if)
  816. batadv_primary_if_update_addr(bat_priv, NULL);
  817. break;
  818. case NETDEV_CHANGEUPPER:
  819. hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev);
  820. if (batadv_is_wifi_hardif(hard_iface))
  821. hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
  822. break;
  823. default:
  824. break;
  825. }
  826. hardif_put:
  827. batadv_hardif_put(hard_iface);
  828. out:
  829. batadv_hardif_put(primary_if);
  830. return NOTIFY_DONE;
  831. }
  832. struct notifier_block batadv_hard_if_notifier = {
  833. .notifier_call = batadv_hard_if_event,
  834. };