hard-interface.c 28 KB

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