hard-interface.c 16 KB

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