hard-interface.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. };