hard-interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "hard-interface.h"
  23. #include "soft-interface.h"
  24. #include "send.h"
  25. #include "translation-table.h"
  26. #include "routing.h"
  27. #include "bat_sysfs.h"
  28. #include "originator.h"
  29. #include "hash.h"
  30. #include "bridge_loop_avoidance.h"
  31. #include <linux/if_arp.h>
  32. void hardif_free_rcu(struct rcu_head *rcu)
  33. {
  34. struct hard_iface *hard_iface;
  35. hard_iface = container_of(rcu, struct hard_iface, rcu);
  36. dev_put(hard_iface->net_dev);
  37. kfree(hard_iface);
  38. }
  39. struct hard_iface *hardif_get_by_netdev(const struct net_device *net_dev)
  40. {
  41. struct hard_iface *hard_iface;
  42. rcu_read_lock();
  43. list_for_each_entry_rcu(hard_iface, &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 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 (softif_is_valid(net_dev))
  63. return 0;
  64. /* Device is being bridged */
  65. /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
  66. return 0; */
  67. return 1;
  68. }
  69. static struct hard_iface *hardif_get_active(const struct net_device *soft_iface)
  70. {
  71. struct hard_iface *hard_iface;
  72. rcu_read_lock();
  73. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  74. if (hard_iface->soft_iface != soft_iface)
  75. continue;
  76. if (hard_iface->if_status == IF_ACTIVE &&
  77. atomic_inc_not_zero(&hard_iface->refcount))
  78. goto out;
  79. }
  80. hard_iface = NULL;
  81. out:
  82. rcu_read_unlock();
  83. return hard_iface;
  84. }
  85. static void primary_if_update_addr(struct bat_priv *bat_priv,
  86. struct hard_iface *oldif)
  87. {
  88. struct vis_packet *vis_packet;
  89. struct hard_iface *primary_if;
  90. primary_if = primary_if_get_selected(bat_priv);
  91. if (!primary_if)
  92. goto out;
  93. vis_packet = (struct vis_packet *)
  94. bat_priv->my_vis_info->skb_packet->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. bla_update_orig_address(bat_priv, primary_if, oldif);
  99. out:
  100. if (primary_if)
  101. hardif_free_ref(primary_if);
  102. }
  103. static void primary_if_select(struct bat_priv *bat_priv,
  104. struct hard_iface *new_hard_iface)
  105. {
  106. struct 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. primary_if_update_addr(bat_priv, curr_hard_iface);
  116. out:
  117. if (curr_hard_iface)
  118. hardif_free_ref(curr_hard_iface);
  119. }
  120. static bool hardif_is_iface_up(const struct hard_iface *hard_iface)
  121. {
  122. if (hard_iface->net_dev->flags & IFF_UP)
  123. return true;
  124. return false;
  125. }
  126. static void check_known_mac_addr(const struct net_device *net_dev)
  127. {
  128. const struct hard_iface *hard_iface;
  129. rcu_read_lock();
  130. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  131. if ((hard_iface->if_status != IF_ACTIVE) &&
  132. (hard_iface->if_status != IF_TO_BE_ACTIVATED))
  133. continue;
  134. if (hard_iface->net_dev == net_dev)
  135. continue;
  136. if (!compare_eth(hard_iface->net_dev->dev_addr,
  137. net_dev->dev_addr))
  138. continue;
  139. pr_warn("The newly added mac address (%pM) already exists on: %s\n",
  140. net_dev->dev_addr, hard_iface->net_dev->name);
  141. pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
  142. }
  143. rcu_read_unlock();
  144. }
  145. int hardif_min_mtu(struct net_device *soft_iface)
  146. {
  147. const struct bat_priv *bat_priv = netdev_priv(soft_iface);
  148. const struct hard_iface *hard_iface;
  149. /* allow big frames if all devices are capable to do so
  150. * (have MTU > 1500 + BAT_HEADER_LEN) */
  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, &hardif_list, list) {
  156. if ((hard_iface->if_status != IF_ACTIVE) &&
  157. (hard_iface->if_status != IF_TO_BE_ACTIVATED))
  158. continue;
  159. if (hard_iface->soft_iface != soft_iface)
  160. continue;
  161. min_mtu = min_t(int, hard_iface->net_dev->mtu - BAT_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 update_min_mtu(struct net_device *soft_iface)
  170. {
  171. int min_mtu;
  172. min_mtu = hardif_min_mtu(soft_iface);
  173. if (soft_iface->mtu != min_mtu)
  174. soft_iface->mtu = min_mtu;
  175. }
  176. static void hardif_activate_interface(struct hard_iface *hard_iface)
  177. {
  178. struct bat_priv *bat_priv;
  179. struct hard_iface *primary_if = NULL;
  180. if (hard_iface->if_status != IF_INACTIVE)
  181. goto out;
  182. bat_priv = netdev_priv(hard_iface->soft_iface);
  183. bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
  184. hard_iface->if_status = IF_TO_BE_ACTIVATED;
  185. /**
  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 = primary_if_get_selected(bat_priv);
  190. if (!primary_if)
  191. primary_if_select(bat_priv, hard_iface);
  192. bat_info(hard_iface->soft_iface, "Interface activated: %s\n",
  193. hard_iface->net_dev->name);
  194. update_min_mtu(hard_iface->soft_iface);
  195. out:
  196. if (primary_if)
  197. hardif_free_ref(primary_if);
  198. }
  199. static void hardif_deactivate_interface(struct hard_iface *hard_iface)
  200. {
  201. if ((hard_iface->if_status != IF_ACTIVE) &&
  202. (hard_iface->if_status != IF_TO_BE_ACTIVATED))
  203. return;
  204. hard_iface->if_status = IF_INACTIVE;
  205. bat_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
  206. hard_iface->net_dev->name);
  207. update_min_mtu(hard_iface->soft_iface);
  208. }
  209. int hardif_enable_interface(struct hard_iface *hard_iface,
  210. const char *iface_name)
  211. {
  212. struct bat_priv *bat_priv;
  213. struct net_device *soft_iface;
  214. int ret;
  215. if (hard_iface->if_status != IF_NOT_IN_USE)
  216. goto out;
  217. if (!atomic_inc_not_zero(&hard_iface->refcount))
  218. goto out;
  219. /* hard-interface is part of a bridge */
  220. if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
  221. 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",
  222. hard_iface->net_dev->name);
  223. soft_iface = dev_get_by_name(&init_net, iface_name);
  224. if (!soft_iface) {
  225. soft_iface = softif_create(iface_name);
  226. if (!soft_iface) {
  227. ret = -ENOMEM;
  228. goto err;
  229. }
  230. /* dev_get_by_name() increases the reference counter for us */
  231. dev_hold(soft_iface);
  232. }
  233. if (!softif_is_valid(soft_iface)) {
  234. pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
  235. soft_iface->name);
  236. ret = -EINVAL;
  237. goto err_dev;
  238. }
  239. hard_iface->soft_iface = soft_iface;
  240. bat_priv = netdev_priv(hard_iface->soft_iface);
  241. ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
  242. if (ret < 0) {
  243. ret = -ENOMEM;
  244. goto err_dev;
  245. }
  246. hard_iface->if_num = bat_priv->num_ifaces;
  247. bat_priv->num_ifaces++;
  248. hard_iface->if_status = IF_INACTIVE;
  249. orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
  250. hard_iface->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
  251. hard_iface->batman_adv_ptype.func = batman_skb_recv;
  252. hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
  253. dev_add_pack(&hard_iface->batman_adv_ptype);
  254. atomic_set(&hard_iface->frag_seqno, 1);
  255. bat_info(hard_iface->soft_iface, "Adding interface: %s\n",
  256. hard_iface->net_dev->name);
  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. 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",
  261. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  262. ETH_DATA_LEN + BAT_HEADER_LEN);
  263. if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
  264. ETH_DATA_LEN + BAT_HEADER_LEN)
  265. bat_info(hard_iface->soft_iface,
  266. "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",
  267. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  268. ETH_DATA_LEN + BAT_HEADER_LEN);
  269. if (hardif_is_iface_up(hard_iface))
  270. hardif_activate_interface(hard_iface);
  271. else
  272. bat_err(hard_iface->soft_iface,
  273. "Not using interface %s (retrying later): interface not active\n",
  274. hard_iface->net_dev->name);
  275. /* begin scheduling originator messages on that interface */
  276. schedule_bat_ogm(hard_iface);
  277. out:
  278. return 0;
  279. err_dev:
  280. dev_put(soft_iface);
  281. err:
  282. hardif_free_ref(hard_iface);
  283. return ret;
  284. }
  285. void hardif_disable_interface(struct hard_iface *hard_iface)
  286. {
  287. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  288. struct hard_iface *primary_if = NULL;
  289. if (hard_iface->if_status == IF_ACTIVE)
  290. hardif_deactivate_interface(hard_iface);
  291. if (hard_iface->if_status != IF_INACTIVE)
  292. goto out;
  293. bat_info(hard_iface->soft_iface, "Removing interface: %s\n",
  294. hard_iface->net_dev->name);
  295. dev_remove_pack(&hard_iface->batman_adv_ptype);
  296. bat_priv->num_ifaces--;
  297. orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
  298. primary_if = primary_if_get_selected(bat_priv);
  299. if (hard_iface == primary_if) {
  300. struct hard_iface *new_if;
  301. new_if = hardif_get_active(hard_iface->soft_iface);
  302. primary_if_select(bat_priv, new_if);
  303. if (new_if)
  304. hardif_free_ref(new_if);
  305. }
  306. bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
  307. hard_iface->if_status = IF_NOT_IN_USE;
  308. /* delete all references to this hard_iface */
  309. purge_orig_ref(bat_priv);
  310. purge_outstanding_packets(bat_priv, hard_iface);
  311. dev_put(hard_iface->soft_iface);
  312. /* nobody uses this interface anymore */
  313. if (!bat_priv->num_ifaces)
  314. softif_destroy(hard_iface->soft_iface);
  315. hard_iface->soft_iface = NULL;
  316. hardif_free_ref(hard_iface);
  317. out:
  318. if (primary_if)
  319. hardif_free_ref(primary_if);
  320. }
  321. static struct hard_iface *hardif_add_interface(struct net_device *net_dev)
  322. {
  323. struct hard_iface *hard_iface;
  324. int ret;
  325. ASSERT_RTNL();
  326. ret = is_valid_iface(net_dev);
  327. if (ret != 1)
  328. goto out;
  329. dev_hold(net_dev);
  330. hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
  331. if (!hard_iface)
  332. goto release_dev;
  333. ret = sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
  334. if (ret)
  335. goto free_if;
  336. hard_iface->if_num = -1;
  337. hard_iface->net_dev = net_dev;
  338. hard_iface->soft_iface = NULL;
  339. hard_iface->if_status = IF_NOT_IN_USE;
  340. INIT_LIST_HEAD(&hard_iface->list);
  341. /* extra reference for return */
  342. atomic_set(&hard_iface->refcount, 2);
  343. check_known_mac_addr(hard_iface->net_dev);
  344. list_add_tail_rcu(&hard_iface->list, &hardif_list);
  345. /**
  346. * This can't be called via a bat_priv callback because
  347. * we have no bat_priv yet.
  348. */
  349. atomic_set(&hard_iface->seqno, 1);
  350. hard_iface->packet_buff = NULL;
  351. return hard_iface;
  352. free_if:
  353. kfree(hard_iface);
  354. release_dev:
  355. dev_put(net_dev);
  356. out:
  357. return NULL;
  358. }
  359. static void hardif_remove_interface(struct hard_iface *hard_iface)
  360. {
  361. ASSERT_RTNL();
  362. /* first deactivate interface */
  363. if (hard_iface->if_status != IF_NOT_IN_USE)
  364. hardif_disable_interface(hard_iface);
  365. if (hard_iface->if_status != IF_NOT_IN_USE)
  366. return;
  367. hard_iface->if_status = IF_TO_BE_REMOVED;
  368. sysfs_del_hardif(&hard_iface->hardif_obj);
  369. hardif_free_ref(hard_iface);
  370. }
  371. void hardif_remove_interfaces(void)
  372. {
  373. struct hard_iface *hard_iface, *hard_iface_tmp;
  374. rtnl_lock();
  375. list_for_each_entry_safe(hard_iface, hard_iface_tmp,
  376. &hardif_list, list) {
  377. list_del_rcu(&hard_iface->list);
  378. hardif_remove_interface(hard_iface);
  379. }
  380. rtnl_unlock();
  381. }
  382. static int hard_if_event(struct notifier_block *this,
  383. unsigned long event, void *ptr)
  384. {
  385. struct net_device *net_dev = ptr;
  386. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  387. struct hard_iface *primary_if = NULL;
  388. struct bat_priv *bat_priv;
  389. if (!hard_iface && event == NETDEV_REGISTER)
  390. hard_iface = hardif_add_interface(net_dev);
  391. if (!hard_iface)
  392. goto out;
  393. switch (event) {
  394. case NETDEV_UP:
  395. hardif_activate_interface(hard_iface);
  396. break;
  397. case NETDEV_GOING_DOWN:
  398. case NETDEV_DOWN:
  399. hardif_deactivate_interface(hard_iface);
  400. break;
  401. case NETDEV_UNREGISTER:
  402. list_del_rcu(&hard_iface->list);
  403. hardif_remove_interface(hard_iface);
  404. break;
  405. case NETDEV_CHANGEMTU:
  406. if (hard_iface->soft_iface)
  407. update_min_mtu(hard_iface->soft_iface);
  408. break;
  409. case NETDEV_CHANGEADDR:
  410. if (hard_iface->if_status == IF_NOT_IN_USE)
  411. goto hardif_put;
  412. check_known_mac_addr(hard_iface->net_dev);
  413. bat_priv = netdev_priv(hard_iface->soft_iface);
  414. bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
  415. primary_if = primary_if_get_selected(bat_priv);
  416. if (!primary_if)
  417. goto hardif_put;
  418. if (hard_iface == primary_if)
  419. primary_if_update_addr(bat_priv, NULL);
  420. break;
  421. default:
  422. break;
  423. }
  424. hardif_put:
  425. hardif_free_ref(hard_iface);
  426. out:
  427. if (primary_if)
  428. hardif_free_ref(primary_if);
  429. return NOTIFY_DONE;
  430. }
  431. /* This function returns true if the interface represented by ifindex is a
  432. * 802.11 wireless device */
  433. bool is_wifi_iface(int ifindex)
  434. {
  435. struct net_device *net_device = NULL;
  436. bool ret = false;
  437. if (ifindex == NULL_IFINDEX)
  438. goto out;
  439. net_device = dev_get_by_index(&init_net, ifindex);
  440. if (!net_device)
  441. goto out;
  442. #ifdef CONFIG_WIRELESS_EXT
  443. /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
  444. * check for wireless_handlers != NULL */
  445. if (net_device->wireless_handlers)
  446. ret = true;
  447. else
  448. #endif
  449. /* cfg80211 drivers have to set ieee80211_ptr */
  450. if (net_device->ieee80211_ptr)
  451. ret = true;
  452. out:
  453. if (net_device)
  454. dev_put(net_device);
  455. return ret;
  456. }
  457. struct notifier_block hard_if_notifier = {
  458. .notifier_call = hard_if_event,
  459. };