hard-interface.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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, hard_iface->net_dev->mtu - BAT_HEADER_LEN,
  159. min_mtu);
  160. }
  161. rcu_read_unlock();
  162. out:
  163. return min_mtu;
  164. }
  165. /* adjusts the MTU if a new interface with a smaller MTU appeared. */
  166. void batadv_update_min_mtu(struct net_device *soft_iface)
  167. {
  168. int min_mtu;
  169. min_mtu = batadv_hardif_min_mtu(soft_iface);
  170. if (soft_iface->mtu != min_mtu)
  171. soft_iface->mtu = min_mtu;
  172. }
  173. static void batadv_hardif_activate_interface(struct hard_iface *hard_iface)
  174. {
  175. struct bat_priv *bat_priv;
  176. struct hard_iface *primary_if = NULL;
  177. if (hard_iface->if_status != IF_INACTIVE)
  178. goto out;
  179. bat_priv = netdev_priv(hard_iface->soft_iface);
  180. bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
  181. hard_iface->if_status = IF_TO_BE_ACTIVATED;
  182. /* the first active interface becomes our primary interface or
  183. * the next active interface after the old primary interface was removed
  184. */
  185. primary_if = batadv_primary_if_get_selected(bat_priv);
  186. if (!primary_if)
  187. batadv_primary_if_select(bat_priv, hard_iface);
  188. bat_info(hard_iface->soft_iface, "Interface activated: %s\n",
  189. hard_iface->net_dev->name);
  190. batadv_update_min_mtu(hard_iface->soft_iface);
  191. out:
  192. if (primary_if)
  193. batadv_hardif_free_ref(primary_if);
  194. }
  195. static void batadv_hardif_deactivate_interface(struct hard_iface *hard_iface)
  196. {
  197. if ((hard_iface->if_status != IF_ACTIVE) &&
  198. (hard_iface->if_status != IF_TO_BE_ACTIVATED))
  199. return;
  200. hard_iface->if_status = IF_INACTIVE;
  201. bat_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
  202. hard_iface->net_dev->name);
  203. batadv_update_min_mtu(hard_iface->soft_iface);
  204. }
  205. int batadv_hardif_enable_interface(struct hard_iface *hard_iface,
  206. const char *iface_name)
  207. {
  208. struct bat_priv *bat_priv;
  209. struct net_device *soft_iface;
  210. int ret;
  211. if (hard_iface->if_status != IF_NOT_IN_USE)
  212. goto out;
  213. if (!atomic_inc_not_zero(&hard_iface->refcount))
  214. goto out;
  215. /* hard-interface is part of a bridge */
  216. if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
  217. 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",
  218. hard_iface->net_dev->name);
  219. soft_iface = dev_get_by_name(&init_net, iface_name);
  220. if (!soft_iface) {
  221. soft_iface = batadv_softif_create(iface_name);
  222. if (!soft_iface) {
  223. ret = -ENOMEM;
  224. goto err;
  225. }
  226. /* dev_get_by_name() increases the reference counter for us */
  227. dev_hold(soft_iface);
  228. }
  229. if (!batadv_softif_is_valid(soft_iface)) {
  230. pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
  231. soft_iface->name);
  232. ret = -EINVAL;
  233. goto err_dev;
  234. }
  235. hard_iface->soft_iface = soft_iface;
  236. bat_priv = netdev_priv(hard_iface->soft_iface);
  237. ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
  238. if (ret < 0)
  239. goto err_dev;
  240. hard_iface->if_num = bat_priv->num_ifaces;
  241. bat_priv->num_ifaces++;
  242. hard_iface->if_status = IF_INACTIVE;
  243. batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
  244. hard_iface->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
  245. hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
  246. hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
  247. dev_add_pack(&hard_iface->batman_adv_ptype);
  248. atomic_set(&hard_iface->frag_seqno, 1);
  249. bat_info(hard_iface->soft_iface, "Adding interface: %s\n",
  250. hard_iface->net_dev->name);
  251. if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
  252. ETH_DATA_LEN + BAT_HEADER_LEN)
  253. bat_info(hard_iface->soft_iface,
  254. "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",
  255. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  256. ETH_DATA_LEN + BAT_HEADER_LEN);
  257. if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
  258. ETH_DATA_LEN + BAT_HEADER_LEN)
  259. bat_info(hard_iface->soft_iface,
  260. "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",
  261. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  262. ETH_DATA_LEN + BAT_HEADER_LEN);
  263. if (batadv_hardif_is_iface_up(hard_iface))
  264. batadv_hardif_activate_interface(hard_iface);
  265. else
  266. bat_err(hard_iface->soft_iface,
  267. "Not using interface %s (retrying later): interface not active\n",
  268. hard_iface->net_dev->name);
  269. /* begin scheduling originator messages on that interface */
  270. batadv_schedule_bat_ogm(hard_iface);
  271. out:
  272. return 0;
  273. err_dev:
  274. dev_put(soft_iface);
  275. err:
  276. batadv_hardif_free_ref(hard_iface);
  277. return ret;
  278. }
  279. void batadv_hardif_disable_interface(struct hard_iface *hard_iface)
  280. {
  281. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  282. struct hard_iface *primary_if = NULL;
  283. if (hard_iface->if_status == IF_ACTIVE)
  284. batadv_hardif_deactivate_interface(hard_iface);
  285. if (hard_iface->if_status != IF_INACTIVE)
  286. goto out;
  287. bat_info(hard_iface->soft_iface, "Removing interface: %s\n",
  288. hard_iface->net_dev->name);
  289. dev_remove_pack(&hard_iface->batman_adv_ptype);
  290. bat_priv->num_ifaces--;
  291. batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
  292. primary_if = batadv_primary_if_get_selected(bat_priv);
  293. if (hard_iface == primary_if) {
  294. struct hard_iface *new_if;
  295. new_if = batadv_hardif_get_active(hard_iface->soft_iface);
  296. batadv_primary_if_select(bat_priv, new_if);
  297. if (new_if)
  298. batadv_hardif_free_ref(new_if);
  299. }
  300. bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
  301. hard_iface->if_status = IF_NOT_IN_USE;
  302. /* delete all references to this hard_iface */
  303. batadv_purge_orig_ref(bat_priv);
  304. batadv_purge_outstanding_packets(bat_priv, hard_iface);
  305. dev_put(hard_iface->soft_iface);
  306. /* nobody uses this interface anymore */
  307. if (!bat_priv->num_ifaces)
  308. batadv_softif_destroy(hard_iface->soft_iface);
  309. hard_iface->soft_iface = NULL;
  310. batadv_hardif_free_ref(hard_iface);
  311. out:
  312. if (primary_if)
  313. batadv_hardif_free_ref(primary_if);
  314. }
  315. static struct hard_iface *
  316. batadv_hardif_add_interface(struct net_device *net_dev)
  317. {
  318. struct hard_iface *hard_iface;
  319. int ret;
  320. ASSERT_RTNL();
  321. ret = batadv_is_valid_iface(net_dev);
  322. if (ret != 1)
  323. goto out;
  324. dev_hold(net_dev);
  325. hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
  326. if (!hard_iface)
  327. goto release_dev;
  328. ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
  329. if (ret)
  330. goto free_if;
  331. hard_iface->if_num = -1;
  332. hard_iface->net_dev = net_dev;
  333. hard_iface->soft_iface = NULL;
  334. hard_iface->if_status = IF_NOT_IN_USE;
  335. INIT_LIST_HEAD(&hard_iface->list);
  336. /* extra reference for return */
  337. atomic_set(&hard_iface->refcount, 2);
  338. batadv_check_known_mac_addr(hard_iface->net_dev);
  339. list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
  340. /* This can't be called via a bat_priv callback because
  341. * we have no bat_priv yet.
  342. */
  343. atomic_set(&hard_iface->seqno, 1);
  344. hard_iface->packet_buff = NULL;
  345. return hard_iface;
  346. free_if:
  347. kfree(hard_iface);
  348. release_dev:
  349. dev_put(net_dev);
  350. out:
  351. return NULL;
  352. }
  353. static void batadv_hardif_remove_interface(struct hard_iface *hard_iface)
  354. {
  355. ASSERT_RTNL();
  356. /* first deactivate interface */
  357. if (hard_iface->if_status != IF_NOT_IN_USE)
  358. batadv_hardif_disable_interface(hard_iface);
  359. if (hard_iface->if_status != IF_NOT_IN_USE)
  360. return;
  361. hard_iface->if_status = IF_TO_BE_REMOVED;
  362. batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
  363. batadv_hardif_free_ref(hard_iface);
  364. }
  365. void batadv_hardif_remove_interfaces(void)
  366. {
  367. struct hard_iface *hard_iface, *hard_iface_tmp;
  368. rtnl_lock();
  369. list_for_each_entry_safe(hard_iface, hard_iface_tmp,
  370. &batadv_hardif_list, list) {
  371. list_del_rcu(&hard_iface->list);
  372. batadv_hardif_remove_interface(hard_iface);
  373. }
  374. rtnl_unlock();
  375. }
  376. static int batadv_hard_if_event(struct notifier_block *this,
  377. unsigned long event, void *ptr)
  378. {
  379. struct net_device *net_dev = ptr;
  380. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  381. struct hard_iface *primary_if = NULL;
  382. struct bat_priv *bat_priv;
  383. if (!hard_iface && event == NETDEV_REGISTER)
  384. hard_iface = batadv_hardif_add_interface(net_dev);
  385. if (!hard_iface)
  386. goto out;
  387. switch (event) {
  388. case NETDEV_UP:
  389. batadv_hardif_activate_interface(hard_iface);
  390. break;
  391. case NETDEV_GOING_DOWN:
  392. case NETDEV_DOWN:
  393. batadv_hardif_deactivate_interface(hard_iface);
  394. break;
  395. case NETDEV_UNREGISTER:
  396. list_del_rcu(&hard_iface->list);
  397. batadv_hardif_remove_interface(hard_iface);
  398. break;
  399. case NETDEV_CHANGEMTU:
  400. if (hard_iface->soft_iface)
  401. batadv_update_min_mtu(hard_iface->soft_iface);
  402. break;
  403. case NETDEV_CHANGEADDR:
  404. if (hard_iface->if_status == IF_NOT_IN_USE)
  405. goto hardif_put;
  406. batadv_check_known_mac_addr(hard_iface->net_dev);
  407. bat_priv = netdev_priv(hard_iface->soft_iface);
  408. bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
  409. primary_if = batadv_primary_if_get_selected(bat_priv);
  410. if (!primary_if)
  411. goto hardif_put;
  412. if (hard_iface == primary_if)
  413. batadv_primary_if_update_addr(bat_priv, NULL);
  414. break;
  415. default:
  416. break;
  417. }
  418. hardif_put:
  419. batadv_hardif_free_ref(hard_iface);
  420. out:
  421. if (primary_if)
  422. batadv_hardif_free_ref(primary_if);
  423. return NOTIFY_DONE;
  424. }
  425. /* This function returns true if the interface represented by ifindex is a
  426. * 802.11 wireless device
  427. */
  428. bool batadv_is_wifi_iface(int ifindex)
  429. {
  430. struct net_device *net_device = NULL;
  431. bool ret = false;
  432. if (ifindex == NULL_IFINDEX)
  433. goto out;
  434. net_device = dev_get_by_index(&init_net, ifindex);
  435. if (!net_device)
  436. goto out;
  437. #ifdef CONFIG_WIRELESS_EXT
  438. /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
  439. * check for wireless_handlers != NULL
  440. */
  441. if (net_device->wireless_handlers)
  442. ret = true;
  443. else
  444. #endif
  445. /* cfg80211 drivers have to set ieee80211_ptr */
  446. if (net_device->ieee80211_ptr)
  447. ret = true;
  448. out:
  449. if (net_device)
  450. dev_put(net_device);
  451. return ret;
  452. }
  453. struct notifier_block batadv_hard_if_notifier = {
  454. .notifier_call = batadv_hard_if_event,
  455. };