hard-interface.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * Copyright (C) 2007-2011 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 <linux/if_arp.h>
  31. /* protect update critical side of hardif_list - but not the content */
  32. static DEFINE_SPINLOCK(hardif_list_lock);
  33. static int batman_skb_recv(struct sk_buff *skb,
  34. struct net_device *dev,
  35. struct packet_type *ptype,
  36. struct net_device *orig_dev);
  37. void hardif_free_rcu(struct rcu_head *rcu)
  38. {
  39. struct hard_iface *hard_iface;
  40. hard_iface = container_of(rcu, struct hard_iface, rcu);
  41. dev_put(hard_iface->net_dev);
  42. kfree(hard_iface);
  43. }
  44. struct hard_iface *hardif_get_by_netdev(struct net_device *net_dev)
  45. {
  46. struct hard_iface *hard_iface;
  47. rcu_read_lock();
  48. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  49. if (hard_iface->net_dev == net_dev &&
  50. atomic_inc_not_zero(&hard_iface->refcount))
  51. goto out;
  52. }
  53. hard_iface = NULL;
  54. out:
  55. rcu_read_unlock();
  56. return hard_iface;
  57. }
  58. static int is_valid_iface(struct net_device *net_dev)
  59. {
  60. if (net_dev->flags & IFF_LOOPBACK)
  61. return 0;
  62. if (net_dev->type != ARPHRD_ETHER)
  63. return 0;
  64. if (net_dev->addr_len != ETH_ALEN)
  65. return 0;
  66. /* no batman over batman */
  67. if (softif_is_valid(net_dev))
  68. return 0;
  69. /* Device is being bridged */
  70. /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
  71. return 0; */
  72. return 1;
  73. }
  74. static struct hard_iface *hardif_get_active(struct net_device *soft_iface)
  75. {
  76. struct hard_iface *hard_iface;
  77. rcu_read_lock();
  78. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  79. if (hard_iface->soft_iface != soft_iface)
  80. continue;
  81. if (hard_iface->if_status == IF_ACTIVE &&
  82. atomic_inc_not_zero(&hard_iface->refcount))
  83. goto out;
  84. }
  85. hard_iface = NULL;
  86. out:
  87. rcu_read_unlock();
  88. return hard_iface;
  89. }
  90. static void update_primary_addr(struct bat_priv *bat_priv)
  91. {
  92. struct vis_packet *vis_packet;
  93. vis_packet = (struct vis_packet *)
  94. bat_priv->my_vis_info->skb_packet->data;
  95. memcpy(vis_packet->vis_orig,
  96. bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
  97. memcpy(vis_packet->sender_orig,
  98. bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
  99. }
  100. static void set_primary_if(struct bat_priv *bat_priv,
  101. struct hard_iface *hard_iface)
  102. {
  103. struct batman_packet *batman_packet;
  104. struct hard_iface *old_if;
  105. if (hard_iface && !atomic_inc_not_zero(&hard_iface->refcount))
  106. hard_iface = NULL;
  107. old_if = bat_priv->primary_if;
  108. bat_priv->primary_if = hard_iface;
  109. if (old_if)
  110. hardif_free_ref(old_if);
  111. if (!bat_priv->primary_if)
  112. return;
  113. batman_packet = (struct batman_packet *)(hard_iface->packet_buff);
  114. batman_packet->flags = PRIMARIES_FIRST_HOP;
  115. batman_packet->ttl = TTL;
  116. update_primary_addr(bat_priv);
  117. /***
  118. * hacky trick to make sure that we send the HNA information via
  119. * our new primary interface
  120. */
  121. atomic_set(&bat_priv->hna_local_changed, 1);
  122. }
  123. static bool hardif_is_iface_up(struct hard_iface *hard_iface)
  124. {
  125. if (hard_iface->net_dev->flags & IFF_UP)
  126. return true;
  127. return false;
  128. }
  129. static void update_mac_addresses(struct hard_iface *hard_iface)
  130. {
  131. memcpy(((struct batman_packet *)(hard_iface->packet_buff))->orig,
  132. hard_iface->net_dev->dev_addr, ETH_ALEN);
  133. memcpy(((struct batman_packet *)(hard_iface->packet_buff))->prev_sender,
  134. hard_iface->net_dev->dev_addr, ETH_ALEN);
  135. }
  136. static void check_known_mac_addr(struct net_device *net_dev)
  137. {
  138. struct hard_iface *hard_iface;
  139. rcu_read_lock();
  140. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  141. if ((hard_iface->if_status != IF_ACTIVE) &&
  142. (hard_iface->if_status != IF_TO_BE_ACTIVATED))
  143. continue;
  144. if (hard_iface->net_dev == net_dev)
  145. continue;
  146. if (!compare_eth(hard_iface->net_dev->dev_addr,
  147. net_dev->dev_addr))
  148. continue;
  149. pr_warning("The newly added mac address (%pM) already exists "
  150. "on: %s\n", net_dev->dev_addr,
  151. hard_iface->net_dev->name);
  152. pr_warning("It is strongly recommended to keep mac addresses "
  153. "unique to avoid problems!\n");
  154. }
  155. rcu_read_unlock();
  156. }
  157. int hardif_min_mtu(struct net_device *soft_iface)
  158. {
  159. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  160. struct hard_iface *hard_iface;
  161. /* allow big frames if all devices are capable to do so
  162. * (have MTU > 1500 + BAT_HEADER_LEN) */
  163. int min_mtu = ETH_DATA_LEN;
  164. if (atomic_read(&bat_priv->fragmentation))
  165. goto out;
  166. rcu_read_lock();
  167. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  168. if ((hard_iface->if_status != IF_ACTIVE) &&
  169. (hard_iface->if_status != IF_TO_BE_ACTIVATED))
  170. continue;
  171. if (hard_iface->soft_iface != soft_iface)
  172. continue;
  173. min_mtu = min_t(int, hard_iface->net_dev->mtu - BAT_HEADER_LEN,
  174. min_mtu);
  175. }
  176. rcu_read_unlock();
  177. out:
  178. return min_mtu;
  179. }
  180. /* adjusts the MTU if a new interface with a smaller MTU appeared. */
  181. void update_min_mtu(struct net_device *soft_iface)
  182. {
  183. int min_mtu;
  184. min_mtu = hardif_min_mtu(soft_iface);
  185. if (soft_iface->mtu != min_mtu)
  186. soft_iface->mtu = min_mtu;
  187. }
  188. static void hardif_activate_interface(struct hard_iface *hard_iface)
  189. {
  190. struct bat_priv *bat_priv;
  191. if (hard_iface->if_status != IF_INACTIVE)
  192. return;
  193. bat_priv = netdev_priv(hard_iface->soft_iface);
  194. update_mac_addresses(hard_iface);
  195. hard_iface->if_status = IF_TO_BE_ACTIVATED;
  196. /**
  197. * the first active interface becomes our primary interface or
  198. * the next active interface after the old primay interface was removed
  199. */
  200. if (!bat_priv->primary_if)
  201. set_primary_if(bat_priv, hard_iface);
  202. bat_info(hard_iface->soft_iface, "Interface activated: %s\n",
  203. hard_iface->net_dev->name);
  204. update_min_mtu(hard_iface->soft_iface);
  205. return;
  206. }
  207. static void hardif_deactivate_interface(struct hard_iface *hard_iface)
  208. {
  209. if ((hard_iface->if_status != IF_ACTIVE) &&
  210. (hard_iface->if_status != IF_TO_BE_ACTIVATED))
  211. return;
  212. hard_iface->if_status = IF_INACTIVE;
  213. bat_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
  214. hard_iface->net_dev->name);
  215. update_min_mtu(hard_iface->soft_iface);
  216. }
  217. int hardif_enable_interface(struct hard_iface *hard_iface, char *iface_name)
  218. {
  219. struct bat_priv *bat_priv;
  220. struct batman_packet *batman_packet;
  221. struct net_device *soft_iface;
  222. int ret;
  223. if (hard_iface->if_status != IF_NOT_IN_USE)
  224. goto out;
  225. if (!atomic_inc_not_zero(&hard_iface->refcount))
  226. goto out;
  227. soft_iface = dev_get_by_name(&init_net, iface_name);
  228. if (!soft_iface) {
  229. soft_iface = softif_create(iface_name);
  230. if (!soft_iface) {
  231. ret = -ENOMEM;
  232. goto err;
  233. }
  234. /* dev_get_by_name() increases the reference counter for us */
  235. dev_hold(soft_iface);
  236. }
  237. if (!softif_is_valid(soft_iface)) {
  238. pr_err("Can't create batman mesh interface %s: "
  239. "already exists as regular interface\n",
  240. soft_iface->name);
  241. dev_put(soft_iface);
  242. ret = -EINVAL;
  243. goto err;
  244. }
  245. hard_iface->soft_iface = soft_iface;
  246. bat_priv = netdev_priv(hard_iface->soft_iface);
  247. hard_iface->packet_len = BAT_PACKET_LEN;
  248. hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
  249. if (!hard_iface->packet_buff) {
  250. bat_err(hard_iface->soft_iface, "Can't add interface packet "
  251. "(%s): out of memory\n", hard_iface->net_dev->name);
  252. ret = -ENOMEM;
  253. goto err;
  254. }
  255. batman_packet = (struct batman_packet *)(hard_iface->packet_buff);
  256. batman_packet->packet_type = BAT_PACKET;
  257. batman_packet->version = COMPAT_VERSION;
  258. batman_packet->flags = 0;
  259. batman_packet->ttl = 2;
  260. batman_packet->tq = TQ_MAX_VALUE;
  261. batman_packet->num_hna = 0;
  262. hard_iface->if_num = bat_priv->num_ifaces;
  263. bat_priv->num_ifaces++;
  264. hard_iface->if_status = IF_INACTIVE;
  265. orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
  266. hard_iface->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
  267. hard_iface->batman_adv_ptype.func = batman_skb_recv;
  268. hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
  269. dev_add_pack(&hard_iface->batman_adv_ptype);
  270. atomic_set(&hard_iface->seqno, 1);
  271. atomic_set(&hard_iface->frag_seqno, 1);
  272. bat_info(hard_iface->soft_iface, "Adding interface: %s\n",
  273. hard_iface->net_dev->name);
  274. if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
  275. ETH_DATA_LEN + BAT_HEADER_LEN)
  276. bat_info(hard_iface->soft_iface,
  277. "The MTU of interface %s is too small (%i) to handle "
  278. "the transport of batman-adv packets. Packets going "
  279. "over this interface will be fragmented on layer2 "
  280. "which could impact the performance. Setting the MTU "
  281. "to %zi would solve the problem.\n",
  282. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  283. ETH_DATA_LEN + BAT_HEADER_LEN);
  284. if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
  285. ETH_DATA_LEN + BAT_HEADER_LEN)
  286. bat_info(hard_iface->soft_iface,
  287. "The MTU of interface %s is too small (%i) to handle "
  288. "the transport of batman-adv packets. If you experience"
  289. " problems getting traffic through try increasing the "
  290. "MTU to %zi.\n",
  291. hard_iface->net_dev->name, hard_iface->net_dev->mtu,
  292. ETH_DATA_LEN + BAT_HEADER_LEN);
  293. if (hardif_is_iface_up(hard_iface))
  294. hardif_activate_interface(hard_iface);
  295. else
  296. bat_err(hard_iface->soft_iface, "Not using interface %s "
  297. "(retrying later): interface not active\n",
  298. hard_iface->net_dev->name);
  299. /* begin scheduling originator messages on that interface */
  300. schedule_own_packet(hard_iface);
  301. out:
  302. return 0;
  303. err:
  304. hardif_free_ref(hard_iface);
  305. return ret;
  306. }
  307. void hardif_disable_interface(struct hard_iface *hard_iface)
  308. {
  309. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  310. if (hard_iface->if_status == IF_ACTIVE)
  311. hardif_deactivate_interface(hard_iface);
  312. if (hard_iface->if_status != IF_INACTIVE)
  313. return;
  314. bat_info(hard_iface->soft_iface, "Removing interface: %s\n",
  315. hard_iface->net_dev->name);
  316. dev_remove_pack(&hard_iface->batman_adv_ptype);
  317. bat_priv->num_ifaces--;
  318. orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
  319. if (hard_iface == bat_priv->primary_if) {
  320. struct hard_iface *new_if;
  321. new_if = hardif_get_active(hard_iface->soft_iface);
  322. set_primary_if(bat_priv, new_if);
  323. if (new_if)
  324. hardif_free_ref(new_if);
  325. }
  326. kfree(hard_iface->packet_buff);
  327. hard_iface->packet_buff = NULL;
  328. hard_iface->if_status = IF_NOT_IN_USE;
  329. /* delete all references to this hard_iface */
  330. purge_orig_ref(bat_priv);
  331. purge_outstanding_packets(bat_priv, hard_iface);
  332. dev_put(hard_iface->soft_iface);
  333. /* nobody uses this interface anymore */
  334. if (!bat_priv->num_ifaces)
  335. softif_destroy(hard_iface->soft_iface);
  336. hard_iface->soft_iface = NULL;
  337. hardif_free_ref(hard_iface);
  338. }
  339. static struct hard_iface *hardif_add_interface(struct net_device *net_dev)
  340. {
  341. struct hard_iface *hard_iface;
  342. int ret;
  343. ret = is_valid_iface(net_dev);
  344. if (ret != 1)
  345. goto out;
  346. dev_hold(net_dev);
  347. hard_iface = kmalloc(sizeof(struct hard_iface), GFP_ATOMIC);
  348. if (!hard_iface) {
  349. pr_err("Can't add interface (%s): out of memory\n",
  350. net_dev->name);
  351. goto release_dev;
  352. }
  353. ret = sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
  354. if (ret)
  355. goto free_if;
  356. hard_iface->if_num = -1;
  357. hard_iface->net_dev = net_dev;
  358. hard_iface->soft_iface = NULL;
  359. hard_iface->if_status = IF_NOT_IN_USE;
  360. INIT_LIST_HEAD(&hard_iface->list);
  361. /* extra reference for return */
  362. atomic_set(&hard_iface->refcount, 2);
  363. check_known_mac_addr(hard_iface->net_dev);
  364. spin_lock(&hardif_list_lock);
  365. list_add_tail_rcu(&hard_iface->list, &hardif_list);
  366. spin_unlock(&hardif_list_lock);
  367. return hard_iface;
  368. free_if:
  369. kfree(hard_iface);
  370. release_dev:
  371. dev_put(net_dev);
  372. out:
  373. return NULL;
  374. }
  375. static void hardif_remove_interface(struct hard_iface *hard_iface)
  376. {
  377. /* first deactivate interface */
  378. if (hard_iface->if_status != IF_NOT_IN_USE)
  379. hardif_disable_interface(hard_iface);
  380. if (hard_iface->if_status != IF_NOT_IN_USE)
  381. return;
  382. hard_iface->if_status = IF_TO_BE_REMOVED;
  383. sysfs_del_hardif(&hard_iface->hardif_obj);
  384. hardif_free_ref(hard_iface);
  385. }
  386. void hardif_remove_interfaces(void)
  387. {
  388. struct hard_iface *hard_iface, *hard_iface_tmp;
  389. struct list_head if_queue;
  390. INIT_LIST_HEAD(&if_queue);
  391. spin_lock(&hardif_list_lock);
  392. list_for_each_entry_safe(hard_iface, hard_iface_tmp,
  393. &hardif_list, list) {
  394. list_del_rcu(&hard_iface->list);
  395. list_add_tail(&hard_iface->list, &if_queue);
  396. }
  397. spin_unlock(&hardif_list_lock);
  398. rtnl_lock();
  399. list_for_each_entry_safe(hard_iface, hard_iface_tmp, &if_queue, list) {
  400. hardif_remove_interface(hard_iface);
  401. }
  402. rtnl_unlock();
  403. }
  404. static int hard_if_event(struct notifier_block *this,
  405. unsigned long event, void *ptr)
  406. {
  407. struct net_device *net_dev = (struct net_device *)ptr;
  408. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  409. struct bat_priv *bat_priv;
  410. if (!hard_iface && event == NETDEV_REGISTER)
  411. hard_iface = hardif_add_interface(net_dev);
  412. if (!hard_iface)
  413. goto out;
  414. switch (event) {
  415. case NETDEV_UP:
  416. hardif_activate_interface(hard_iface);
  417. break;
  418. case NETDEV_GOING_DOWN:
  419. case NETDEV_DOWN:
  420. hardif_deactivate_interface(hard_iface);
  421. break;
  422. case NETDEV_UNREGISTER:
  423. spin_lock(&hardif_list_lock);
  424. list_del_rcu(&hard_iface->list);
  425. spin_unlock(&hardif_list_lock);
  426. hardif_remove_interface(hard_iface);
  427. break;
  428. case NETDEV_CHANGEMTU:
  429. if (hard_iface->soft_iface)
  430. update_min_mtu(hard_iface->soft_iface);
  431. break;
  432. case NETDEV_CHANGEADDR:
  433. if (hard_iface->if_status == IF_NOT_IN_USE)
  434. goto hardif_put;
  435. check_known_mac_addr(hard_iface->net_dev);
  436. update_mac_addresses(hard_iface);
  437. bat_priv = netdev_priv(hard_iface->soft_iface);
  438. if (hard_iface == bat_priv->primary_if)
  439. update_primary_addr(bat_priv);
  440. break;
  441. default:
  442. break;
  443. };
  444. hardif_put:
  445. hardif_free_ref(hard_iface);
  446. out:
  447. return NOTIFY_DONE;
  448. }
  449. /* receive a packet with the batman ethertype coming on a hard
  450. * interface */
  451. static int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
  452. struct packet_type *ptype,
  453. struct net_device *orig_dev)
  454. {
  455. struct bat_priv *bat_priv;
  456. struct batman_packet *batman_packet;
  457. struct hard_iface *hard_iface;
  458. int ret;
  459. hard_iface = container_of(ptype, struct hard_iface, batman_adv_ptype);
  460. skb = skb_share_check(skb, GFP_ATOMIC);
  461. /* skb was released by skb_share_check() */
  462. if (!skb)
  463. goto err_out;
  464. /* packet should hold at least type and version */
  465. if (unlikely(!pskb_may_pull(skb, 2)))
  466. goto err_free;
  467. /* expect a valid ethernet header here. */
  468. if (unlikely(skb->mac_len != sizeof(struct ethhdr)
  469. || !skb_mac_header(skb)))
  470. goto err_free;
  471. if (!hard_iface->soft_iface)
  472. goto err_free;
  473. bat_priv = netdev_priv(hard_iface->soft_iface);
  474. if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
  475. goto err_free;
  476. /* discard frames on not active interfaces */
  477. if (hard_iface->if_status != IF_ACTIVE)
  478. goto err_free;
  479. batman_packet = (struct batman_packet *)skb->data;
  480. if (batman_packet->version != COMPAT_VERSION) {
  481. bat_dbg(DBG_BATMAN, bat_priv,
  482. "Drop packet: incompatible batman version (%i)\n",
  483. batman_packet->version);
  484. goto err_free;
  485. }
  486. /* all receive handlers return whether they received or reused
  487. * the supplied skb. if not, we have to free the skb. */
  488. switch (batman_packet->packet_type) {
  489. /* batman originator packet */
  490. case BAT_PACKET:
  491. ret = recv_bat_packet(skb, hard_iface);
  492. break;
  493. /* batman icmp packet */
  494. case BAT_ICMP:
  495. ret = recv_icmp_packet(skb, hard_iface);
  496. break;
  497. /* unicast packet */
  498. case BAT_UNICAST:
  499. ret = recv_unicast_packet(skb, hard_iface);
  500. break;
  501. /* fragmented unicast packet */
  502. case BAT_UNICAST_FRAG:
  503. ret = recv_ucast_frag_packet(skb, hard_iface);
  504. break;
  505. /* broadcast packet */
  506. case BAT_BCAST:
  507. ret = recv_bcast_packet(skb, hard_iface);
  508. break;
  509. /* vis packet */
  510. case BAT_VIS:
  511. ret = recv_vis_packet(skb, hard_iface);
  512. break;
  513. default:
  514. ret = NET_RX_DROP;
  515. }
  516. if (ret == NET_RX_DROP)
  517. kfree_skb(skb);
  518. /* return NET_RX_SUCCESS in any case as we
  519. * most probably dropped the packet for
  520. * routing-logical reasons. */
  521. return NET_RX_SUCCESS;
  522. err_free:
  523. kfree_skb(skb);
  524. err_out:
  525. return NET_RX_DROP;
  526. }
  527. struct notifier_block hard_if_notifier = {
  528. .notifier_call = hard_if_event,
  529. };