hard-interface.c 15 KB

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