hard-interface.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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/notifier.h>
  24. #include <linux/printk.h>
  25. #include <linux/rculist.h>
  26. #include <linux/rtnetlink.h>
  27. #include <linux/slab.h>
  28. #include <linux/spinlock.h>
  29. #include <net/net_namespace.h>
  30. #include <net/rtnetlink.h>
  31. #include <uapi/linux/batadv_packet.h>
  32. #include "bat_v.h"
  33. #include "bridge_loop_avoidance.h"
  34. #include "distributed-arp-table.h"
  35. #include "gateway_client.h"
  36. #include "log.h"
  37. #include "mesh-interface.h"
  38. #include "originator.h"
  39. #include "send.h"
  40. #include "translation-table.h"
  41. /**
  42. * batadv_hardif_release() - release hard interface from lists and queue for
  43. * free after rcu grace period
  44. * @ref: kref pointer of the hard interface
  45. */
  46. void batadv_hardif_release(struct kref *ref)
  47. {
  48. struct batadv_hard_iface *hard_iface;
  49. hard_iface = container_of(ref, struct batadv_hard_iface, refcount);
  50. netdev_put(hard_iface->net_dev, &hard_iface->dev_tracker);
  51. kfree_rcu(hard_iface, rcu);
  52. }
  53. /**
  54. * batadv_hardif_get_by_netdev() - Get hard interface object of a net_device
  55. * @net_dev: net_device to search for
  56. *
  57. * Return: batadv_hard_iface of net_dev (with increased refcnt), NULL on errors
  58. */
  59. struct batadv_hard_iface *
  60. batadv_hardif_get_by_netdev(const struct net_device *net_dev)
  61. {
  62. struct batadv_hard_iface *hard_iface;
  63. rcu_read_lock();
  64. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  65. if (hard_iface->net_dev == net_dev &&
  66. kref_get_unless_zero(&hard_iface->refcount))
  67. goto out;
  68. }
  69. hard_iface = NULL;
  70. out:
  71. rcu_read_unlock();
  72. return hard_iface;
  73. }
  74. /**
  75. * batadv_getlink_net() - return link net namespace (of use fallback)
  76. * @netdev: net_device to check
  77. * @fallback_net: return in case get_link_net is not available for @netdev
  78. *
  79. * Return: result of rtnl_link_ops->get_link_net or @fallback_net
  80. */
  81. static struct net *batadv_getlink_net(const struct net_device *netdev,
  82. struct net *fallback_net)
  83. {
  84. if (!netdev->rtnl_link_ops)
  85. return fallback_net;
  86. if (!netdev->rtnl_link_ops->get_link_net)
  87. return fallback_net;
  88. return netdev->rtnl_link_ops->get_link_net(netdev);
  89. }
  90. /**
  91. * batadv_mutual_parents() - check if two devices are each others parent
  92. * @dev1: 1st net dev
  93. * @net1: 1st devices netns
  94. * @dev2: 2nd net dev
  95. * @net2: 2nd devices netns
  96. *
  97. * veth devices come in pairs and each is the parent of the other!
  98. *
  99. * Return: true if the devices are each others parent, otherwise false
  100. */
  101. static bool batadv_mutual_parents(const struct net_device *dev1,
  102. struct net *net1,
  103. const struct net_device *dev2,
  104. struct net *net2)
  105. {
  106. int dev1_parent_iflink = dev_get_iflink(dev1);
  107. int dev2_parent_iflink = dev_get_iflink(dev2);
  108. const struct net *dev1_parent_net;
  109. const struct net *dev2_parent_net;
  110. dev1_parent_net = batadv_getlink_net(dev1, net1);
  111. dev2_parent_net = batadv_getlink_net(dev2, net2);
  112. if (!dev1_parent_iflink || !dev2_parent_iflink)
  113. return false;
  114. return (dev1_parent_iflink == dev2->ifindex) &&
  115. (dev2_parent_iflink == dev1->ifindex) &&
  116. net_eq(dev1_parent_net, net2) &&
  117. net_eq(dev2_parent_net, net1);
  118. }
  119. /**
  120. * batadv_is_on_batman_iface() - check if a device is a batman iface descendant
  121. * @net_dev: the device to check
  122. *
  123. * If the user creates any virtual device on top of a batman-adv interface, it
  124. * is important to prevent this new interface from being used to create a new
  125. * mesh network (this behaviour would lead to a batman-over-batman
  126. * configuration). This function recursively checks all the fathers of the
  127. * device passed as argument looking for a batman-adv mesh interface.
  128. *
  129. * Return: true if the device is descendant of a batman-adv mesh interface (or
  130. * if it is a batman-adv interface itself), false otherwise
  131. */
  132. static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
  133. {
  134. struct net *net = dev_net(net_dev);
  135. struct net_device *parent_dev;
  136. struct net *parent_net;
  137. int iflink;
  138. bool ret;
  139. /* check if this is a batman-adv mesh interface */
  140. if (batadv_meshif_is_valid(net_dev))
  141. return true;
  142. iflink = dev_get_iflink(net_dev);
  143. if (iflink == 0)
  144. return false;
  145. parent_net = batadv_getlink_net(net_dev, net);
  146. /* iflink to itself, most likely physical device */
  147. if (net == parent_net && iflink == net_dev->ifindex)
  148. return false;
  149. /* recurse over the parent device */
  150. parent_dev = __dev_get_by_index((struct net *)parent_net, iflink);
  151. if (!parent_dev) {
  152. pr_warn("Cannot find parent device. Skipping batadv-on-batadv check for %s\n",
  153. net_dev->name);
  154. return false;
  155. }
  156. if (batadv_mutual_parents(net_dev, net, parent_dev, parent_net))
  157. return false;
  158. ret = batadv_is_on_batman_iface(parent_dev);
  159. return ret;
  160. }
  161. static bool batadv_is_valid_iface(const struct net_device *net_dev)
  162. {
  163. if (net_dev->flags & IFF_LOOPBACK)
  164. return false;
  165. if (net_dev->type != ARPHRD_ETHER)
  166. return false;
  167. if (net_dev->addr_len != ETH_ALEN)
  168. return false;
  169. /* no batman over batman */
  170. if (batadv_is_on_batman_iface(net_dev))
  171. return false;
  172. return true;
  173. }
  174. /**
  175. * __batadv_get_real_netdev() - check if the given netdev struct is a virtual
  176. * interface on top of another 'real' interface
  177. * @netdev: the device to check
  178. *
  179. * Callers must hold the rtnl semaphore. You may want batadv_get_real_netdev()
  180. * instead of this.
  181. *
  182. * Return: the 'real' net device or the original net device and NULL in case
  183. * of an error.
  184. */
  185. struct net_device *__batadv_get_real_netdev(struct net_device *netdev)
  186. {
  187. struct batadv_hard_iface *hard_iface = NULL;
  188. struct net_device *real_netdev = NULL;
  189. struct net *real_net;
  190. struct net *net;
  191. int iflink;
  192. ASSERT_RTNL();
  193. if (!netdev)
  194. return NULL;
  195. iflink = dev_get_iflink(netdev);
  196. if (iflink == 0) {
  197. dev_hold(netdev);
  198. return netdev;
  199. }
  200. hard_iface = batadv_hardif_get_by_netdev(netdev);
  201. if (!hard_iface || !hard_iface->mesh_iface)
  202. goto out;
  203. net = dev_net(hard_iface->mesh_iface);
  204. real_net = batadv_getlink_net(netdev, net);
  205. /* iflink to itself, most likely physical device */
  206. if (net == real_net && netdev->ifindex == iflink) {
  207. real_netdev = netdev;
  208. dev_hold(real_netdev);
  209. goto out;
  210. }
  211. real_netdev = dev_get_by_index(real_net, iflink);
  212. out:
  213. batadv_hardif_put(hard_iface);
  214. return real_netdev;
  215. }
  216. /**
  217. * batadv_get_real_netdev() - check if the given net_device struct is a virtual
  218. * interface on top of another 'real' interface
  219. * @net_device: the device to check
  220. *
  221. * Return: the 'real' net device or the original net device and NULL in case
  222. * of an error.
  223. */
  224. struct net_device *batadv_get_real_netdev(struct net_device *net_device)
  225. {
  226. struct net_device *real_netdev;
  227. rtnl_lock();
  228. real_netdev = __batadv_get_real_netdev(net_device);
  229. rtnl_unlock();
  230. return real_netdev;
  231. }
  232. /**
  233. * batadv_is_wext_netdev() - check if the given net_device struct is a
  234. * wext wifi interface
  235. * @net_device: the device to check
  236. *
  237. * Return: true if the net device is a wext wireless device, false
  238. * otherwise.
  239. */
  240. static bool batadv_is_wext_netdev(struct net_device *net_device)
  241. {
  242. if (!net_device)
  243. return false;
  244. #ifdef CONFIG_WIRELESS_EXT
  245. /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
  246. * check for wireless_handlers != NULL
  247. */
  248. if (net_device->wireless_handlers)
  249. return true;
  250. #endif
  251. return false;
  252. }
  253. /**
  254. * batadv_is_cfg80211_netdev() - check if the given net_device struct is a
  255. * cfg80211 wifi interface
  256. * @net_device: the device to check
  257. *
  258. * Return: true if the net device is a cfg80211 wireless device, false
  259. * otherwise.
  260. */
  261. static bool batadv_is_cfg80211_netdev(struct net_device *net_device)
  262. {
  263. if (!net_device)
  264. return false;
  265. #if IS_ENABLED(CONFIG_CFG80211)
  266. /* cfg80211 drivers have to set ieee80211_ptr */
  267. if (net_device->ieee80211_ptr)
  268. return true;
  269. #endif
  270. return false;
  271. }
  272. /**
  273. * batadv_wifi_flags_evaluate() - calculate wifi flags for net_device
  274. * @net_device: the device to check
  275. *
  276. * Return: batadv_hard_iface_wifi_flags flags of the device
  277. */
  278. static u32 batadv_wifi_flags_evaluate(struct net_device *net_device)
  279. {
  280. u32 wifi_flags = 0;
  281. struct net_device *real_netdev;
  282. if (batadv_is_wext_netdev(net_device))
  283. wifi_flags |= BATADV_HARDIF_WIFI_WEXT_DIRECT;
  284. if (batadv_is_cfg80211_netdev(net_device))
  285. wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT;
  286. real_netdev = __batadv_get_real_netdev(net_device);
  287. if (!real_netdev)
  288. return wifi_flags;
  289. if (real_netdev == net_device)
  290. goto out;
  291. if (batadv_is_wext_netdev(real_netdev))
  292. wifi_flags |= BATADV_HARDIF_WIFI_WEXT_INDIRECT;
  293. if (batadv_is_cfg80211_netdev(real_netdev))
  294. wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT;
  295. out:
  296. dev_put(real_netdev);
  297. return wifi_flags;
  298. }
  299. /**
  300. * batadv_is_cfg80211_hardif() - check if the given hardif is a cfg80211 wifi
  301. * interface
  302. * @hard_iface: the device to check
  303. *
  304. * Return: true if the net device is a cfg80211 wireless device, false
  305. * otherwise.
  306. */
  307. bool batadv_is_cfg80211_hardif(struct batadv_hard_iface *hard_iface)
  308. {
  309. u32 allowed_flags = 0;
  310. allowed_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT;
  311. allowed_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT;
  312. return !!(hard_iface->wifi_flags & allowed_flags);
  313. }
  314. /**
  315. * batadv_is_wifi_hardif() - check if the given hardif is a wifi interface
  316. * @hard_iface: the device to check
  317. *
  318. * Return: true if the net device is a 802.11 wireless device, false otherwise.
  319. */
  320. bool batadv_is_wifi_hardif(struct batadv_hard_iface *hard_iface)
  321. {
  322. if (!hard_iface)
  323. return false;
  324. return hard_iface->wifi_flags != 0;
  325. }
  326. /**
  327. * batadv_hardif_no_broadcast() - check whether (re)broadcast is necessary
  328. * @if_outgoing: the outgoing interface checked and considered for (re)broadcast
  329. * @orig_addr: the originator of this packet
  330. * @orig_neigh: originator address of the forwarder we just got the packet from
  331. * (NULL if we originated)
  332. *
  333. * Checks whether a packet needs to be (re)broadcasted on the given interface.
  334. *
  335. * Return:
  336. * BATADV_HARDIF_BCAST_NORECIPIENT: No neighbor on interface
  337. * BATADV_HARDIF_BCAST_DUPFWD: Just one neighbor, but it is the forwarder
  338. * BATADV_HARDIF_BCAST_DUPORIG: Just one neighbor, but it is the originator
  339. * BATADV_HARDIF_BCAST_OK: Several neighbors, must broadcast
  340. */
  341. int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing,
  342. u8 *orig_addr, u8 *orig_neigh)
  343. {
  344. struct batadv_hardif_neigh_node *hardif_neigh;
  345. struct hlist_node *first;
  346. int ret = BATADV_HARDIF_BCAST_OK;
  347. rcu_read_lock();
  348. /* 0 neighbors -> no (re)broadcast */
  349. first = rcu_dereference(hlist_first_rcu(&if_outgoing->neigh_list));
  350. if (!first) {
  351. ret = BATADV_HARDIF_BCAST_NORECIPIENT;
  352. goto out;
  353. }
  354. /* >1 neighbors -> (re)broadcast */
  355. if (rcu_dereference(hlist_next_rcu(first)))
  356. goto out;
  357. hardif_neigh = hlist_entry(first, struct batadv_hardif_neigh_node,
  358. list);
  359. /* 1 neighbor, is the originator -> no rebroadcast */
  360. if (orig_addr && batadv_compare_eth(hardif_neigh->orig, orig_addr)) {
  361. ret = BATADV_HARDIF_BCAST_DUPORIG;
  362. /* 1 neighbor, is the one we received from -> no rebroadcast */
  363. } else if (orig_neigh &&
  364. batadv_compare_eth(hardif_neigh->orig, orig_neigh)) {
  365. ret = BATADV_HARDIF_BCAST_DUPFWD;
  366. }
  367. out:
  368. rcu_read_unlock();
  369. return ret;
  370. }
  371. static struct batadv_hard_iface *
  372. batadv_hardif_get_active(struct net_device *mesh_iface)
  373. {
  374. struct batadv_hard_iface *hard_iface;
  375. struct list_head *iter;
  376. rcu_read_lock();
  377. netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) {
  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 batadv_hard_iface *hard_iface)
  423. {
  424. struct net_device *mesh_iface = hard_iface->mesh_iface;
  425. const struct batadv_hard_iface *tmp_hard_iface;
  426. struct list_head *iter;
  427. if (!mesh_iface)
  428. return;
  429. netdev_for_each_lower_private(mesh_iface, tmp_hard_iface, iter) {
  430. if (tmp_hard_iface == hard_iface)
  431. continue;
  432. if (tmp_hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  433. continue;
  434. if (!batadv_compare_eth(tmp_hard_iface->net_dev->dev_addr,
  435. hard_iface->net_dev->dev_addr))
  436. continue;
  437. pr_warn("The newly added mac address (%pM) already exists on: %s\n",
  438. hard_iface->net_dev->dev_addr, tmp_hard_iface->net_dev->name);
  439. pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
  440. }
  441. }
  442. /**
  443. * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
  444. * @mesh_iface: netdev struct of the mesh interface
  445. */
  446. static void batadv_hardif_recalc_extra_skbroom(struct net_device *mesh_iface)
  447. {
  448. const struct batadv_hard_iface *hard_iface;
  449. unsigned short lower_header_len = ETH_HLEN;
  450. unsigned short lower_headroom = 0;
  451. unsigned short lower_tailroom = 0;
  452. unsigned short needed_headroom;
  453. struct list_head *iter;
  454. rcu_read_lock();
  455. netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) {
  456. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  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. mesh_iface->needed_headroom = needed_headroom;
  471. mesh_iface->needed_tailroom = lower_tailroom;
  472. }
  473. /**
  474. * batadv_hardif_min_mtu() - Calculate maximum MTU for mesh interface
  475. * @mesh_iface: netdev struct of the mesh interface
  476. *
  477. * Return: MTU for the mesh-interface (limited by the minimal MTU of all active
  478. * slave interfaces)
  479. */
  480. int batadv_hardif_min_mtu(struct net_device *mesh_iface)
  481. {
  482. struct batadv_priv *bat_priv = netdev_priv(mesh_iface);
  483. const struct batadv_hard_iface *hard_iface;
  484. struct list_head *iter;
  485. int min_mtu = INT_MAX;
  486. rcu_read_lock();
  487. netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) {
  488. if (hard_iface->if_status != BATADV_IF_ACTIVE &&
  489. hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
  490. continue;
  491. min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
  492. }
  493. rcu_read_unlock();
  494. if (atomic_read(&bat_priv->fragmentation) == 0)
  495. goto out;
  496. /* with fragmentation enabled the maximum size of internally generated
  497. * packets such as translation table exchanges or tvlv containers, etc
  498. * has to be calculated
  499. */
  500. min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
  501. min_mtu -= sizeof(struct batadv_frag_packet);
  502. min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
  503. out:
  504. /* report to the other components the maximum amount of bytes that
  505. * batman-adv can send over the wire (without considering the payload
  506. * overhead). For example, this value is used by TT to compute the
  507. * maximum local table size
  508. */
  509. atomic_set(&bat_priv->packet_size_max, min_mtu);
  510. /* the real mesh-interface MTU is computed by removing the payload
  511. * overhead from the maximum amount of bytes that was just computed.
  512. */
  513. return min_t(int, min_mtu - batadv_max_header_len(), BATADV_MAX_MTU);
  514. }
  515. /**
  516. * batadv_update_min_mtu() - Adjusts the MTU if a new interface with a smaller
  517. * MTU appeared
  518. * @mesh_iface: netdev struct of the mesh interface
  519. */
  520. void batadv_update_min_mtu(struct net_device *mesh_iface)
  521. {
  522. struct batadv_priv *bat_priv = netdev_priv(mesh_iface);
  523. int limit_mtu;
  524. int mtu;
  525. mtu = batadv_hardif_min_mtu(mesh_iface);
  526. if (bat_priv->mtu_set_by_user)
  527. limit_mtu = bat_priv->mtu_set_by_user;
  528. else
  529. limit_mtu = ETH_DATA_LEN;
  530. mtu = min(mtu, limit_mtu);
  531. dev_set_mtu(mesh_iface, mtu);
  532. /* Check if the local translate table should be cleaned up to match a
  533. * new (and smaller) MTU.
  534. */
  535. batadv_tt_local_resize_to_mtu(mesh_iface);
  536. }
  537. static void
  538. batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
  539. {
  540. struct batadv_priv *bat_priv;
  541. struct batadv_hard_iface *primary_if = NULL;
  542. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  543. goto out;
  544. bat_priv = netdev_priv(hard_iface->mesh_iface);
  545. bat_priv->algo_ops->iface.update_mac(hard_iface);
  546. hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
  547. /* the first active interface becomes our primary interface or
  548. * the next active interface after the old primary interface was removed
  549. */
  550. primary_if = batadv_primary_if_get_selected(bat_priv);
  551. if (!primary_if)
  552. batadv_primary_if_select(bat_priv, hard_iface);
  553. batadv_info(hard_iface->mesh_iface, "Interface activated: %s\n",
  554. hard_iface->net_dev->name);
  555. batadv_update_min_mtu(hard_iface->mesh_iface);
  556. if (bat_priv->algo_ops->iface.activate)
  557. bat_priv->algo_ops->iface.activate(hard_iface);
  558. out:
  559. batadv_hardif_put(primary_if);
  560. }
  561. static void
  562. batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
  563. {
  564. if (hard_iface->if_status != BATADV_IF_ACTIVE &&
  565. hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
  566. return;
  567. hard_iface->if_status = BATADV_IF_INACTIVE;
  568. batadv_info(hard_iface->mesh_iface, "Interface deactivated: %s\n",
  569. hard_iface->net_dev->name);
  570. batadv_update_min_mtu(hard_iface->mesh_iface);
  571. }
  572. /**
  573. * batadv_hardif_enable_interface() - Enslave hard interface to mesh interface
  574. * @hard_iface: hard interface to add to mesh interface
  575. * @mesh_iface: netdev struct of the mesh interface
  576. *
  577. * Return: 0 on success or negative error number in case of failure
  578. */
  579. int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
  580. struct net_device *mesh_iface)
  581. {
  582. struct batadv_priv *bat_priv;
  583. __be16 ethertype = htons(ETH_P_BATMAN);
  584. int max_header_len = batadv_max_header_len();
  585. unsigned int required_mtu;
  586. unsigned int hardif_mtu;
  587. int ret;
  588. hardif_mtu = READ_ONCE(hard_iface->net_dev->mtu);
  589. required_mtu = READ_ONCE(mesh_iface->mtu) + max_header_len;
  590. if (hardif_mtu < ETH_MIN_MTU + max_header_len)
  591. return -EINVAL;
  592. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  593. goto out;
  594. kref_get(&hard_iface->refcount);
  595. netdev_hold(mesh_iface, &hard_iface->meshif_dev_tracker, GFP_ATOMIC);
  596. hard_iface->mesh_iface = mesh_iface;
  597. bat_priv = netdev_priv(hard_iface->mesh_iface);
  598. ret = netdev_master_upper_dev_link(hard_iface->net_dev,
  599. mesh_iface, hard_iface, NULL, NULL);
  600. if (ret)
  601. goto err_dev;
  602. ret = bat_priv->algo_ops->iface.enable(hard_iface);
  603. if (ret < 0)
  604. goto err_upper;
  605. hard_iface->if_status = BATADV_IF_INACTIVE;
  606. kref_get(&hard_iface->refcount);
  607. hard_iface->batman_adv_ptype.type = ethertype;
  608. hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
  609. hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
  610. dev_add_pack(&hard_iface->batman_adv_ptype);
  611. batadv_info(hard_iface->mesh_iface, "Adding interface: %s\n",
  612. hard_iface->net_dev->name);
  613. if (atomic_read(&bat_priv->fragmentation) &&
  614. hardif_mtu < required_mtu)
  615. batadv_info(hard_iface->mesh_iface,
  616. "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",
  617. hard_iface->net_dev->name, hardif_mtu,
  618. required_mtu);
  619. if (!atomic_read(&bat_priv->fragmentation) &&
  620. hardif_mtu < required_mtu)
  621. batadv_info(hard_iface->mesh_iface,
  622. "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",
  623. hard_iface->net_dev->name, hardif_mtu,
  624. required_mtu);
  625. batadv_check_known_mac_addr(hard_iface);
  626. if (batadv_hardif_is_iface_up(hard_iface))
  627. batadv_hardif_activate_interface(hard_iface);
  628. else
  629. batadv_err(hard_iface->mesh_iface,
  630. "Not using interface %s (retrying later): interface not active\n",
  631. hard_iface->net_dev->name);
  632. batadv_hardif_recalc_extra_skbroom(mesh_iface);
  633. if (bat_priv->algo_ops->iface.enabled)
  634. bat_priv->algo_ops->iface.enabled(hard_iface);
  635. out:
  636. return 0;
  637. err_upper:
  638. netdev_upper_dev_unlink(hard_iface->net_dev, mesh_iface);
  639. err_dev:
  640. hard_iface->mesh_iface = NULL;
  641. netdev_put(mesh_iface, &hard_iface->meshif_dev_tracker);
  642. batadv_hardif_put(hard_iface);
  643. return ret;
  644. }
  645. /**
  646. * batadv_hardif_cnt() - get number of interfaces enslaved to mesh interface
  647. * @mesh_iface: mesh interface to check
  648. *
  649. * This function is only using RCU for locking - the result can therefore be
  650. * off when another function is modifying the list at the same time. The
  651. * caller can use the rtnl_lock to make sure that the count is accurate.
  652. *
  653. * Return: number of connected/enslaved hard interfaces
  654. */
  655. static size_t batadv_hardif_cnt(struct net_device *mesh_iface)
  656. {
  657. struct batadv_hard_iface *hard_iface;
  658. struct list_head *iter;
  659. size_t count = 0;
  660. rcu_read_lock();
  661. netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter)
  662. count++;
  663. rcu_read_unlock();
  664. return count;
  665. }
  666. /**
  667. * batadv_hardif_disable_interface() - Remove hard interface from mesh interface
  668. * @hard_iface: hard interface to be removed
  669. */
  670. void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface)
  671. {
  672. struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
  673. struct batadv_hard_iface *primary_if = NULL;
  674. batadv_hardif_deactivate_interface(hard_iface);
  675. if (hard_iface->if_status != BATADV_IF_INACTIVE)
  676. goto out;
  677. batadv_info(hard_iface->mesh_iface, "Removing interface: %s\n",
  678. hard_iface->net_dev->name);
  679. dev_remove_pack(&hard_iface->batman_adv_ptype);
  680. batadv_hardif_put(hard_iface);
  681. primary_if = batadv_primary_if_get_selected(bat_priv);
  682. if (hard_iface == primary_if) {
  683. struct batadv_hard_iface *new_if;
  684. new_if = batadv_hardif_get_active(hard_iface->mesh_iface);
  685. batadv_primary_if_select(bat_priv, new_if);
  686. batadv_hardif_put(new_if);
  687. }
  688. bat_priv->algo_ops->iface.disable(hard_iface);
  689. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  690. /* delete all references to this hard_iface */
  691. batadv_purge_orig_ref(bat_priv);
  692. batadv_purge_outstanding_packets(bat_priv, hard_iface);
  693. netdev_put(hard_iface->mesh_iface, &hard_iface->meshif_dev_tracker);
  694. netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->mesh_iface);
  695. batadv_hardif_recalc_extra_skbroom(hard_iface->mesh_iface);
  696. /* nobody uses this interface anymore */
  697. if (batadv_hardif_cnt(hard_iface->mesh_iface) <= 1)
  698. batadv_gw_check_client_stop(bat_priv);
  699. hard_iface->mesh_iface = NULL;
  700. batadv_hardif_put(hard_iface);
  701. out:
  702. batadv_hardif_put(primary_if);
  703. }
  704. static struct batadv_hard_iface *
  705. batadv_hardif_add_interface(struct net_device *net_dev)
  706. {
  707. struct batadv_hard_iface *hard_iface;
  708. ASSERT_RTNL();
  709. if (!batadv_is_valid_iface(net_dev))
  710. return NULL;
  711. hard_iface = kzalloc_obj(*hard_iface, GFP_ATOMIC);
  712. if (!hard_iface)
  713. return NULL;
  714. netdev_hold(net_dev, &hard_iface->dev_tracker, GFP_ATOMIC);
  715. hard_iface->net_dev = net_dev;
  716. hard_iface->mesh_iface = NULL;
  717. hard_iface->if_status = BATADV_IF_NOT_IN_USE;
  718. INIT_LIST_HEAD(&hard_iface->list);
  719. INIT_HLIST_HEAD(&hard_iface->neigh_list);
  720. mutex_init(&hard_iface->bat_iv.ogm_buff_mutex);
  721. spin_lock_init(&hard_iface->neigh_list_lock);
  722. kref_init(&hard_iface->refcount);
  723. hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
  724. hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev);
  725. if (batadv_is_wifi_hardif(hard_iface))
  726. hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
  727. atomic_set(&hard_iface->hop_penalty, 0);
  728. batadv_v_hardif_init(hard_iface);
  729. kref_get(&hard_iface->refcount);
  730. list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
  731. batadv_hardif_generation++;
  732. return hard_iface;
  733. }
  734. static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
  735. {
  736. ASSERT_RTNL();
  737. /* first deactivate interface */
  738. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  739. batadv_hardif_disable_interface(hard_iface);
  740. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  741. return;
  742. hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
  743. batadv_hardif_put(hard_iface);
  744. }
  745. /**
  746. * batadv_hard_if_event_meshif() - Handle events for mesh interfaces
  747. * @event: NETDEV_* event to handle
  748. * @net_dev: net_device which generated an event
  749. *
  750. * Return: NOTIFY_* result
  751. */
  752. static int batadv_hard_if_event_meshif(unsigned long event,
  753. struct net_device *net_dev)
  754. {
  755. struct batadv_priv *bat_priv;
  756. switch (event) {
  757. case NETDEV_REGISTER:
  758. bat_priv = netdev_priv(net_dev);
  759. batadv_meshif_create_vlan(bat_priv, BATADV_NO_FLAGS);
  760. break;
  761. }
  762. return NOTIFY_DONE;
  763. }
  764. static int batadv_hard_if_event(struct notifier_block *this,
  765. unsigned long event, void *ptr)
  766. {
  767. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  768. struct batadv_hard_iface *hard_iface;
  769. struct batadv_hard_iface *primary_if = NULL;
  770. struct batadv_priv *bat_priv;
  771. if (batadv_meshif_is_valid(net_dev))
  772. return batadv_hard_if_event_meshif(event, net_dev);
  773. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  774. if (!hard_iface && (event == NETDEV_REGISTER ||
  775. event == NETDEV_POST_TYPE_CHANGE))
  776. hard_iface = batadv_hardif_add_interface(net_dev);
  777. if (!hard_iface)
  778. goto out;
  779. switch (event) {
  780. case NETDEV_UP:
  781. batadv_hardif_activate_interface(hard_iface);
  782. break;
  783. case NETDEV_GOING_DOWN:
  784. case NETDEV_DOWN:
  785. batadv_hardif_deactivate_interface(hard_iface);
  786. break;
  787. case NETDEV_UNREGISTER:
  788. case NETDEV_PRE_TYPE_CHANGE:
  789. list_del_rcu(&hard_iface->list);
  790. batadv_hardif_generation++;
  791. batadv_hardif_remove_interface(hard_iface);
  792. break;
  793. case NETDEV_CHANGEMTU:
  794. if (hard_iface->mesh_iface)
  795. batadv_update_min_mtu(hard_iface->mesh_iface);
  796. break;
  797. case NETDEV_CHANGEADDR:
  798. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  799. goto hardif_put;
  800. batadv_check_known_mac_addr(hard_iface);
  801. bat_priv = netdev_priv(hard_iface->mesh_iface);
  802. bat_priv->algo_ops->iface.update_mac(hard_iface);
  803. primary_if = batadv_primary_if_get_selected(bat_priv);
  804. if (!primary_if)
  805. goto hardif_put;
  806. if (hard_iface == primary_if)
  807. batadv_primary_if_update_addr(bat_priv, NULL);
  808. break;
  809. case NETDEV_CHANGEUPPER:
  810. hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev);
  811. if (batadv_is_wifi_hardif(hard_iface))
  812. hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
  813. break;
  814. default:
  815. break;
  816. }
  817. hardif_put:
  818. batadv_hardif_put(hard_iface);
  819. out:
  820. batadv_hardif_put(primary_if);
  821. return NOTIFY_DONE;
  822. }
  823. struct notifier_block batadv_hard_if_notifier = {
  824. .notifier_call = batadv_hard_if_event,
  825. };