hard-interface.c 14 KB

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