hard-interface.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /* Copyright (C) 2007-2012 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, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "hard-interface.h"
  21. #include "soft-interface.h"
  22. #include "send.h"
  23. #include "translation-table.h"
  24. #include "routing.h"
  25. #include "bat_sysfs.h"
  26. #include "originator.h"
  27. #include "hash.h"
  28. #include "bridge_loop_avoidance.h"
  29. #include <linux/if_arp.h>
  30. void batadv_hardif_free_rcu(struct rcu_head *rcu)
  31. {
  32. struct hard_iface *hard_iface;
  33. hard_iface = container_of(rcu, struct hard_iface, rcu);
  34. dev_put(hard_iface->net_dev);
  35. kfree(hard_iface);
  36. }
  37. struct hard_iface *batadv_hardif_get_by_netdev(const struct net_device *net_dev)
  38. {
  39. struct hard_iface *hard_iface;
  40. rcu_read_lock();
  41. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  42. if (hard_iface->net_dev == net_dev &&
  43. atomic_inc_not_zero(&hard_iface->refcount))
  44. goto out;
  45. }
  46. hard_iface = NULL;
  47. out:
  48. rcu_read_unlock();
  49. return hard_iface;
  50. }
  51. static int batadv_is_valid_iface(const struct net_device *net_dev)
  52. {
  53. if (net_dev->flags & IFF_LOOPBACK)
  54. return 0;
  55. if (net_dev->type != ARPHRD_ETHER)
  56. return 0;
  57. if (net_dev->addr_len != ETH_ALEN)
  58. return 0;
  59. /* no batman over batman */
  60. if (batadv_softif_is_valid(net_dev))
  61. return 0;
  62. return 1;
  63. }
  64. static struct hard_iface *
  65. batadv_hardif_get_active(const struct net_device *soft_iface)
  66. {
  67. struct hard_iface *hard_iface;
  68. rcu_read_lock();
  69. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  70. if (hard_iface->soft_iface != soft_iface)
  71. continue;
  72. if (hard_iface->if_status == IF_ACTIVE &&
  73. atomic_inc_not_zero(&hard_iface->refcount))
  74. goto out;
  75. }
  76. hard_iface = NULL;
  77. out:
  78. rcu_read_unlock();
  79. return hard_iface;
  80. }
  81. static void batadv_primary_if_update_addr(struct bat_priv *bat_priv,
  82. struct hard_iface *oldif)
  83. {
  84. struct vis_packet *vis_packet;
  85. struct hard_iface *primary_if;
  86. primary_if = batadv_primary_if_get_selected(bat_priv);
  87. if (!primary_if)
  88. goto out;
  89. vis_packet = (struct vis_packet *)
  90. bat_priv->my_vis_info->skb_packet->data;
  91. memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  92. memcpy(vis_packet->sender_orig,
  93. primary_if->net_dev->dev_addr, ETH_ALEN);
  94. batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
  95. out:
  96. if (primary_if)
  97. batadv_hardif_free_ref(primary_if);
  98. }
  99. static void batadv_primary_if_select(struct bat_priv *bat_priv,
  100. struct hard_iface *new_hard_iface)
  101. {
  102. struct hard_iface *curr_hard_iface;
  103. ASSERT_RTNL();
  104. if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
  105. new_hard_iface = NULL;
  106. curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
  107. rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
  108. if (!new_hard_iface)
  109. goto out;
  110. bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
  111. batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
  112. out:
  113. if (curr_hard_iface)
  114. batadv_hardif_free_ref(curr_hard_iface);
  115. }
  116. static bool batadv_hardif_is_iface_up(const struct hard_iface *hard_iface)
  117. {
  118. if (hard_iface->net_dev->flags & IFF_UP)
  119. return true;
  120. return false;
  121. }
  122. static void batadv_check_known_mac_addr(const struct net_device *net_dev)
  123. {
  124. const struct hard_iface *hard_iface;
  125. rcu_read_lock();
  126. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  127. if ((hard_iface->if_status != IF_ACTIVE) &&
  128. (hard_iface->if_status != IF_TO_BE_ACTIVATED))
  129. continue;
  130. if (hard_iface->net_dev == net_dev)
  131. continue;
  132. if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
  133. net_dev->dev_addr))
  134. continue;
  135. pr_warn("The newly added mac address (%pM) already exists on: %s\n",
  136. net_dev->dev_addr, hard_iface->net_dev->name);
  137. pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
  138. }
  139. rcu_read_unlock();
  140. }
  141. int batadv_hardif_min_mtu(struct net_device *soft_iface)
  142. {
  143. const struct bat_priv *bat_priv = netdev_priv(soft_iface);
  144. const struct hard_iface *hard_iface;
  145. /* allow big frames if all devices are capable to do so
  146. * (have MTU > 1500 + BAT_HEADER_LEN)
  147. */
  148. int min_mtu = ETH_DATA_LEN;
  149. if (atomic_read(&bat_priv->fragmentation))
  150. goto out;
  151. rcu_read_lock();
  152. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  153. if ((hard_iface->if_status != IF_ACTIVE) &&
  154. (hard_iface->if_status != IF_TO_BE_ACTIVATED))
  155. continue;
  156. if (hard_iface->soft_iface != soft_iface)
  157. continue;
  158. min_mtu = min_t(int,
  159. hard_iface->net_dev->mtu - BATADV_HEADER_LEN,
  160. min_mtu);
  161. }
  162. rcu_read_unlock();
  163. out:
  164. return min_mtu;
  165. }
  166. /* adjusts the MTU if a new interface with a smaller MTU appeared. */
  167. void batadv_update_min_mtu(struct net_device *soft_iface)
  168. {
  169. int min_mtu;
  170. min_mtu = batadv_hardif_min_mtu(soft_iface);
  171. if (soft_iface->mtu != min_mtu)
  172. soft_iface->mtu = min_mtu;
  173. }
  174. static void batadv_hardif_activate_interface(struct hard_iface *hard_iface)
  175. {
  176. struct bat_priv *bat_priv;
  177. struct hard_iface *primary_if = NULL;
  178. if (hard_iface->if_status != IF_INACTIVE)
  179. goto out;
  180. bat_priv = netdev_priv(hard_iface->soft_iface);
  181. bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
  182. hard_iface->if_status = IF_TO_BE_ACTIVATED;
  183. /* the first active interface becomes our primary interface or
  184. * the next active interface after the old primary interface was removed
  185. */
  186. primary_if = batadv_primary_if_get_selected(bat_priv);
  187. if (!primary_if)
  188. batadv_primary_if_select(bat_priv, hard_iface);
  189. batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
  190. hard_iface->net_dev->name);
  191. batadv_update_min_mtu(hard_iface->soft_iface);
  192. out:
  193. if (primary_if)
  194. batadv_hardif_free_ref(primary_if);
  195. }
  196. static void batadv_hardif_deactivate_interface(struct hard_iface *hard_iface)
  197. {
  198. if ((hard_iface->if_status != IF_ACTIVE) &&
  199. (hard_iface->if_status != IF_TO_BE_ACTIVATED))
  200. return;
  201. hard_iface->if_status = IF_INACTIVE;
  202. batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
  203. hard_iface->net_dev->name);
  204. batadv_update_min_mtu(hard_iface->soft_iface);
  205. }
  206. int batadv_hardif_enable_interface(struct hard_iface *hard_iface,
  207. const char *iface_name)
  208. {
  209. struct bat_priv *bat_priv;
  210. struct net_device *soft_iface;
  211. __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN);
  212. int ret;
  213. if (hard_iface->if_status != IF_NOT_IN_USE)
  214. goto out;
  215. if (!atomic_inc_not_zero(&hard_iface->refcount))
  216. goto out;
  217. /* hard-interface is part of a bridge */
  218. if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
  219. pr_err("You are about to enable batman-adv on '%s' which already is part of a bridge. Unless you know exactly what you are doing this is probably wrong and won't work the way you think it would.\n",
  220. hard_iface->net_dev->name);
  221. soft_iface = dev_get_by_name(&init_net, iface_name);
  222. if (!soft_iface) {
  223. soft_iface = batadv_softif_create(iface_name);
  224. if (!soft_iface) {
  225. ret = -ENOMEM;
  226. goto err;
  227. }
  228. /* dev_get_by_name() increases the reference counter for us */
  229. dev_hold(soft_iface);
  230. }
  231. if (!batadv_softif_is_valid(soft_iface)) {
  232. pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
  233. soft_iface->name);
  234. ret = -EINVAL;
  235. goto err_dev;
  236. }
  237. hard_iface->soft_iface = soft_iface;
  238. bat_priv = netdev_priv(hard_iface->soft_iface);
  239. ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
  240. if (ret < 0)
  241. goto err_dev;
  242. hard_iface->if_num = bat_priv->num_ifaces;
  243. bat_priv->num_ifaces++;
  244. hard_iface->if_status = IF_INACTIVE;
  245. batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
  246. hard_iface->batman_adv_ptype.type = ethertype;
  247. hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
  248. hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
  249. dev_add_pack(&hard_iface->batman_adv_ptype);
  250. atomic_set(&hard_iface->frag_seqno, 1);
  251. batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
  252. hard_iface->net_dev->name);
  253. if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
  254. ETH_DATA_LEN + BATADV_HEADER_LEN)
  255. batadv_info(hard_iface->soft_iface,
  256. "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 %zi would solve the problem.\n",
  257. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  258. ETH_DATA_LEN + BATADV_HEADER_LEN);
  259. if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
  260. ETH_DATA_LEN + BATADV_HEADER_LEN)
  261. batadv_info(hard_iface->soft_iface,
  262. "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 %zi.\n",
  263. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  264. ETH_DATA_LEN + BATADV_HEADER_LEN);
  265. if (batadv_hardif_is_iface_up(hard_iface))
  266. batadv_hardif_activate_interface(hard_iface);
  267. else
  268. batadv_err(hard_iface->soft_iface,
  269. "Not using interface %s (retrying later): interface not active\n",
  270. hard_iface->net_dev->name);
  271. /* begin scheduling originator messages on that interface */
  272. batadv_schedule_bat_ogm(hard_iface);
  273. out:
  274. return 0;
  275. err_dev:
  276. dev_put(soft_iface);
  277. err:
  278. batadv_hardif_free_ref(hard_iface);
  279. return ret;
  280. }
  281. void batadv_hardif_disable_interface(struct hard_iface *hard_iface)
  282. {
  283. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  284. struct hard_iface *primary_if = NULL;
  285. if (hard_iface->if_status == IF_ACTIVE)
  286. batadv_hardif_deactivate_interface(hard_iface);
  287. if (hard_iface->if_status != IF_INACTIVE)
  288. goto out;
  289. batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
  290. hard_iface->net_dev->name);
  291. dev_remove_pack(&hard_iface->batman_adv_ptype);
  292. bat_priv->num_ifaces--;
  293. batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
  294. primary_if = batadv_primary_if_get_selected(bat_priv);
  295. if (hard_iface == primary_if) {
  296. struct hard_iface *new_if;
  297. new_if = batadv_hardif_get_active(hard_iface->soft_iface);
  298. batadv_primary_if_select(bat_priv, new_if);
  299. if (new_if)
  300. batadv_hardif_free_ref(new_if);
  301. }
  302. bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
  303. hard_iface->if_status = IF_NOT_IN_USE;
  304. /* delete all references to this hard_iface */
  305. batadv_purge_orig_ref(bat_priv);
  306. batadv_purge_outstanding_packets(bat_priv, hard_iface);
  307. dev_put(hard_iface->soft_iface);
  308. /* nobody uses this interface anymore */
  309. if (!bat_priv->num_ifaces)
  310. batadv_softif_destroy(hard_iface->soft_iface);
  311. hard_iface->soft_iface = NULL;
  312. batadv_hardif_free_ref(hard_iface);
  313. out:
  314. if (primary_if)
  315. batadv_hardif_free_ref(primary_if);
  316. }
  317. static struct hard_iface *
  318. batadv_hardif_add_interface(struct net_device *net_dev)
  319. {
  320. struct hard_iface *hard_iface;
  321. int ret;
  322. ASSERT_RTNL();
  323. ret = batadv_is_valid_iface(net_dev);
  324. if (ret != 1)
  325. goto out;
  326. dev_hold(net_dev);
  327. hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
  328. if (!hard_iface)
  329. goto release_dev;
  330. ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
  331. if (ret)
  332. goto free_if;
  333. hard_iface->if_num = -1;
  334. hard_iface->net_dev = net_dev;
  335. hard_iface->soft_iface = NULL;
  336. hard_iface->if_status = IF_NOT_IN_USE;
  337. INIT_LIST_HEAD(&hard_iface->list);
  338. /* extra reference for return */
  339. atomic_set(&hard_iface->refcount, 2);
  340. batadv_check_known_mac_addr(hard_iface->net_dev);
  341. list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
  342. /* This can't be called via a bat_priv callback because
  343. * we have no bat_priv yet.
  344. */
  345. atomic_set(&hard_iface->seqno, 1);
  346. hard_iface->packet_buff = NULL;
  347. return hard_iface;
  348. free_if:
  349. kfree(hard_iface);
  350. release_dev:
  351. dev_put(net_dev);
  352. out:
  353. return NULL;
  354. }
  355. static void batadv_hardif_remove_interface(struct hard_iface *hard_iface)
  356. {
  357. ASSERT_RTNL();
  358. /* first deactivate interface */
  359. if (hard_iface->if_status != IF_NOT_IN_USE)
  360. batadv_hardif_disable_interface(hard_iface);
  361. if (hard_iface->if_status != IF_NOT_IN_USE)
  362. return;
  363. hard_iface->if_status = IF_TO_BE_REMOVED;
  364. batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
  365. batadv_hardif_free_ref(hard_iface);
  366. }
  367. void batadv_hardif_remove_interfaces(void)
  368. {
  369. struct hard_iface *hard_iface, *hard_iface_tmp;
  370. rtnl_lock();
  371. list_for_each_entry_safe(hard_iface, hard_iface_tmp,
  372. &batadv_hardif_list, list) {
  373. list_del_rcu(&hard_iface->list);
  374. batadv_hardif_remove_interface(hard_iface);
  375. }
  376. rtnl_unlock();
  377. }
  378. static int batadv_hard_if_event(struct notifier_block *this,
  379. unsigned long event, void *ptr)
  380. {
  381. struct net_device *net_dev = ptr;
  382. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  383. struct hard_iface *primary_if = NULL;
  384. struct bat_priv *bat_priv;
  385. if (!hard_iface && event == NETDEV_REGISTER)
  386. hard_iface = batadv_hardif_add_interface(net_dev);
  387. if (!hard_iface)
  388. goto out;
  389. switch (event) {
  390. case NETDEV_UP:
  391. batadv_hardif_activate_interface(hard_iface);
  392. break;
  393. case NETDEV_GOING_DOWN:
  394. case NETDEV_DOWN:
  395. batadv_hardif_deactivate_interface(hard_iface);
  396. break;
  397. case NETDEV_UNREGISTER:
  398. list_del_rcu(&hard_iface->list);
  399. batadv_hardif_remove_interface(hard_iface);
  400. break;
  401. case NETDEV_CHANGEMTU:
  402. if (hard_iface->soft_iface)
  403. batadv_update_min_mtu(hard_iface->soft_iface);
  404. break;
  405. case NETDEV_CHANGEADDR:
  406. if (hard_iface->if_status == IF_NOT_IN_USE)
  407. goto hardif_put;
  408. batadv_check_known_mac_addr(hard_iface->net_dev);
  409. bat_priv = netdev_priv(hard_iface->soft_iface);
  410. bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
  411. primary_if = batadv_primary_if_get_selected(bat_priv);
  412. if (!primary_if)
  413. goto hardif_put;
  414. if (hard_iface == primary_if)
  415. batadv_primary_if_update_addr(bat_priv, NULL);
  416. break;
  417. default:
  418. break;
  419. }
  420. hardif_put:
  421. batadv_hardif_free_ref(hard_iface);
  422. out:
  423. if (primary_if)
  424. batadv_hardif_free_ref(primary_if);
  425. return NOTIFY_DONE;
  426. }
  427. /* This function returns true if the interface represented by ifindex is a
  428. * 802.11 wireless device
  429. */
  430. bool batadv_is_wifi_iface(int ifindex)
  431. {
  432. struct net_device *net_device = NULL;
  433. bool ret = false;
  434. if (ifindex == BATADV_NULL_IFINDEX)
  435. goto out;
  436. net_device = dev_get_by_index(&init_net, ifindex);
  437. if (!net_device)
  438. goto out;
  439. #ifdef CONFIG_WIRELESS_EXT
  440. /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
  441. * check for wireless_handlers != NULL
  442. */
  443. if (net_device->wireless_handlers)
  444. ret = true;
  445. else
  446. #endif
  447. /* cfg80211 drivers have to set ieee80211_ptr */
  448. if (net_device->ieee80211_ptr)
  449. ret = true;
  450. out:
  451. if (net_device)
  452. dev_put(net_device);
  453. return ret;
  454. }
  455. struct notifier_block batadv_hard_if_notifier = {
  456. .notifier_call = batadv_hard_if_event,
  457. };