hard-interface.c 16 KB

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