hard-interface.c 15 KB

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