send.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 "send.h"
  23. #include "routing.h"
  24. #include "translation-table.h"
  25. #include "soft-interface.h"
  26. #include "hard-interface.h"
  27. #include "vis.h"
  28. #include "aggregation.h"
  29. #include "gateway_common.h"
  30. #include "originator.h"
  31. static void send_outstanding_bcast_packet(struct work_struct *work);
  32. /* apply hop penalty for a normal link */
  33. static uint8_t hop_penalty(const uint8_t tq, struct bat_priv *bat_priv)
  34. {
  35. int hop_penalty = atomic_read(&bat_priv->hop_penalty);
  36. return (tq * (TQ_MAX_VALUE - hop_penalty)) / (TQ_MAX_VALUE);
  37. }
  38. /* when do we schedule our own packet to be sent */
  39. static unsigned long own_send_time(struct bat_priv *bat_priv)
  40. {
  41. return jiffies + msecs_to_jiffies(
  42. atomic_read(&bat_priv->orig_interval) -
  43. JITTER + (random32() % 2*JITTER));
  44. }
  45. /* when do we schedule a forwarded packet to be sent */
  46. static unsigned long forward_send_time(void)
  47. {
  48. return jiffies + msecs_to_jiffies(random32() % (JITTER/2));
  49. }
  50. /* send out an already prepared packet to the given address via the
  51. * specified batman interface */
  52. int send_skb_packet(struct sk_buff *skb,
  53. struct hard_iface *hard_iface,
  54. uint8_t *dst_addr)
  55. {
  56. struct ethhdr *ethhdr;
  57. if (hard_iface->if_status != IF_ACTIVE)
  58. goto send_skb_err;
  59. if (unlikely(!hard_iface->net_dev))
  60. goto send_skb_err;
  61. if (!(hard_iface->net_dev->flags & IFF_UP)) {
  62. pr_warning("Interface %s is not up - can't send packet via "
  63. "that interface!\n", hard_iface->net_dev->name);
  64. goto send_skb_err;
  65. }
  66. /* push to the ethernet header. */
  67. if (my_skb_head_push(skb, sizeof(struct ethhdr)) < 0)
  68. goto send_skb_err;
  69. skb_reset_mac_header(skb);
  70. ethhdr = (struct ethhdr *) skb_mac_header(skb);
  71. memcpy(ethhdr->h_source, hard_iface->net_dev->dev_addr, ETH_ALEN);
  72. memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
  73. ethhdr->h_proto = __constant_htons(ETH_P_BATMAN);
  74. skb_set_network_header(skb, ETH_HLEN);
  75. skb->priority = TC_PRIO_CONTROL;
  76. skb->protocol = __constant_htons(ETH_P_BATMAN);
  77. skb->dev = hard_iface->net_dev;
  78. /* dev_queue_xmit() returns a negative result on error. However on
  79. * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
  80. * (which is > 0). This will not be treated as an error. */
  81. return dev_queue_xmit(skb);
  82. send_skb_err:
  83. kfree_skb(skb);
  84. return NET_XMIT_DROP;
  85. }
  86. /* Send a packet to a given interface */
  87. static void send_packet_to_if(struct forw_packet *forw_packet,
  88. struct hard_iface *hard_iface)
  89. {
  90. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  91. char *fwd_str;
  92. uint8_t packet_num;
  93. int16_t buff_pos;
  94. struct batman_packet *batman_packet;
  95. struct sk_buff *skb;
  96. if (hard_iface->if_status != IF_ACTIVE)
  97. return;
  98. packet_num = 0;
  99. buff_pos = 0;
  100. batman_packet = (struct batman_packet *)forw_packet->skb->data;
  101. /* adjust all flags and log packets */
  102. while (aggregated_packet(buff_pos,
  103. forw_packet->packet_len,
  104. batman_packet->num_hna)) {
  105. /* we might have aggregated direct link packets with an
  106. * ordinary base packet */
  107. if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
  108. (forw_packet->if_incoming == hard_iface))
  109. batman_packet->flags |= DIRECTLINK;
  110. else
  111. batman_packet->flags &= ~DIRECTLINK;
  112. fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
  113. "Sending own" :
  114. "Forwarding"));
  115. bat_dbg(DBG_BATMAN, bat_priv,
  116. "%s %spacket (originator %pM, seqno %d, TQ %d, TTL %d,"
  117. " IDF %s) on interface %s [%pM]\n",
  118. fwd_str, (packet_num > 0 ? "aggregated " : ""),
  119. batman_packet->orig, ntohl(batman_packet->seqno),
  120. batman_packet->tq, batman_packet->ttl,
  121. (batman_packet->flags & DIRECTLINK ?
  122. "on" : "off"),
  123. hard_iface->net_dev->name,
  124. hard_iface->net_dev->dev_addr);
  125. buff_pos += sizeof(struct batman_packet) +
  126. (batman_packet->num_hna * ETH_ALEN);
  127. packet_num++;
  128. batman_packet = (struct batman_packet *)
  129. (forw_packet->skb->data + buff_pos);
  130. }
  131. /* create clone because function is called more than once */
  132. skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
  133. if (skb)
  134. send_skb_packet(skb, hard_iface, broadcast_addr);
  135. }
  136. /* send a batman packet */
  137. static void send_packet(struct forw_packet *forw_packet)
  138. {
  139. struct hard_iface *hard_iface;
  140. struct net_device *soft_iface;
  141. struct bat_priv *bat_priv;
  142. struct batman_packet *batman_packet =
  143. (struct batman_packet *)(forw_packet->skb->data);
  144. unsigned char directlink = (batman_packet->flags & DIRECTLINK ? 1 : 0);
  145. if (!forw_packet->if_incoming) {
  146. pr_err("Error - can't forward packet: incoming iface not "
  147. "specified\n");
  148. return;
  149. }
  150. soft_iface = forw_packet->if_incoming->soft_iface;
  151. bat_priv = netdev_priv(soft_iface);
  152. if (forw_packet->if_incoming->if_status != IF_ACTIVE)
  153. return;
  154. /* multihomed peer assumed */
  155. /* non-primary OGMs are only broadcasted on their interface */
  156. if ((directlink && (batman_packet->ttl == 1)) ||
  157. (forw_packet->own && (forw_packet->if_incoming->if_num > 0))) {
  158. /* FIXME: what about aggregated packets ? */
  159. bat_dbg(DBG_BATMAN, bat_priv,
  160. "%s packet (originator %pM, seqno %d, TTL %d) "
  161. "on interface %s [%pM]\n",
  162. (forw_packet->own ? "Sending own" : "Forwarding"),
  163. batman_packet->orig, ntohl(batman_packet->seqno),
  164. batman_packet->ttl,
  165. forw_packet->if_incoming->net_dev->name,
  166. forw_packet->if_incoming->net_dev->dev_addr);
  167. /* skb is only used once and than forw_packet is free'd */
  168. send_skb_packet(forw_packet->skb, forw_packet->if_incoming,
  169. broadcast_addr);
  170. forw_packet->skb = NULL;
  171. return;
  172. }
  173. /* broadcast on every interface */
  174. rcu_read_lock();
  175. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  176. if (hard_iface->soft_iface != soft_iface)
  177. continue;
  178. send_packet_to_if(forw_packet, hard_iface);
  179. }
  180. rcu_read_unlock();
  181. }
  182. static void rebuild_batman_packet(struct bat_priv *bat_priv,
  183. struct hard_iface *hard_iface)
  184. {
  185. int new_len;
  186. unsigned char *new_buff;
  187. struct batman_packet *batman_packet;
  188. new_len = sizeof(struct batman_packet) +
  189. (bat_priv->num_local_hna * ETH_ALEN);
  190. new_buff = kmalloc(new_len, GFP_ATOMIC);
  191. /* keep old buffer if kmalloc should fail */
  192. if (new_buff) {
  193. memcpy(new_buff, hard_iface->packet_buff,
  194. sizeof(struct batman_packet));
  195. batman_packet = (struct batman_packet *)new_buff;
  196. batman_packet->num_hna = hna_local_fill_buffer(bat_priv,
  197. new_buff + sizeof(struct batman_packet),
  198. new_len - sizeof(struct batman_packet));
  199. kfree(hard_iface->packet_buff);
  200. hard_iface->packet_buff = new_buff;
  201. hard_iface->packet_len = new_len;
  202. }
  203. }
  204. void schedule_own_packet(struct hard_iface *hard_iface)
  205. {
  206. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  207. unsigned long send_time;
  208. struct batman_packet *batman_packet;
  209. int vis_server;
  210. if ((hard_iface->if_status == IF_NOT_IN_USE) ||
  211. (hard_iface->if_status == IF_TO_BE_REMOVED))
  212. return;
  213. vis_server = atomic_read(&bat_priv->vis_mode);
  214. /**
  215. * the interface gets activated here to avoid race conditions between
  216. * the moment of activating the interface in
  217. * hardif_activate_interface() where the originator mac is set and
  218. * outdated packets (especially uninitialized mac addresses) in the
  219. * packet queue
  220. */
  221. if (hard_iface->if_status == IF_TO_BE_ACTIVATED)
  222. hard_iface->if_status = IF_ACTIVE;
  223. /* if local hna has changed and interface is a primary interface */
  224. if ((atomic_read(&bat_priv->hna_local_changed)) &&
  225. (hard_iface == bat_priv->primary_if))
  226. rebuild_batman_packet(bat_priv, hard_iface);
  227. /**
  228. * NOTE: packet_buff might just have been re-allocated in
  229. * rebuild_batman_packet()
  230. */
  231. batman_packet = (struct batman_packet *)hard_iface->packet_buff;
  232. /* change sequence number to network order */
  233. batman_packet->seqno =
  234. htonl((uint32_t)atomic_read(&hard_iface->seqno));
  235. if (vis_server == VIS_TYPE_SERVER_SYNC)
  236. batman_packet->flags |= VIS_SERVER;
  237. else
  238. batman_packet->flags &= ~VIS_SERVER;
  239. if ((hard_iface == bat_priv->primary_if) &&
  240. (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER))
  241. batman_packet->gw_flags =
  242. (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
  243. else
  244. batman_packet->gw_flags = 0;
  245. atomic_inc(&hard_iface->seqno);
  246. slide_own_bcast_window(hard_iface);
  247. send_time = own_send_time(bat_priv);
  248. add_bat_packet_to_list(bat_priv,
  249. hard_iface->packet_buff,
  250. hard_iface->packet_len,
  251. hard_iface, 1, send_time);
  252. }
  253. void schedule_forward_packet(struct orig_node *orig_node,
  254. struct ethhdr *ethhdr,
  255. struct batman_packet *batman_packet,
  256. uint8_t directlink, int hna_buff_len,
  257. struct hard_iface *if_incoming)
  258. {
  259. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  260. struct neigh_node *router;
  261. unsigned char in_tq, in_ttl, tq_avg = 0;
  262. unsigned long send_time;
  263. if (batman_packet->ttl <= 1) {
  264. bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
  265. return;
  266. }
  267. router = orig_node_get_router(orig_node);
  268. in_tq = batman_packet->tq;
  269. in_ttl = batman_packet->ttl;
  270. batman_packet->ttl--;
  271. memcpy(batman_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
  272. /* rebroadcast tq of our best ranking neighbor to ensure the rebroadcast
  273. * of our best tq value */
  274. if (router && router->tq_avg != 0) {
  275. /* rebroadcast ogm of best ranking neighbor as is */
  276. if (!compare_eth(router->addr, ethhdr->h_source)) {
  277. batman_packet->tq = router->tq_avg;
  278. if (router->last_ttl)
  279. batman_packet->ttl = router->last_ttl - 1;
  280. }
  281. tq_avg = router->tq_avg;
  282. }
  283. if (router)
  284. neigh_node_free_ref(router);
  285. /* apply hop penalty */
  286. batman_packet->tq = hop_penalty(batman_packet->tq, bat_priv);
  287. bat_dbg(DBG_BATMAN, bat_priv,
  288. "Forwarding packet: tq_orig: %i, tq_avg: %i, "
  289. "tq_forw: %i, ttl_orig: %i, ttl_forw: %i\n",
  290. in_tq, tq_avg, batman_packet->tq, in_ttl - 1,
  291. batman_packet->ttl);
  292. batman_packet->seqno = htonl(batman_packet->seqno);
  293. /* switch of primaries first hop flag when forwarding */
  294. batman_packet->flags &= ~PRIMARIES_FIRST_HOP;
  295. if (directlink)
  296. batman_packet->flags |= DIRECTLINK;
  297. else
  298. batman_packet->flags &= ~DIRECTLINK;
  299. send_time = forward_send_time();
  300. add_bat_packet_to_list(bat_priv,
  301. (unsigned char *)batman_packet,
  302. sizeof(struct batman_packet) + hna_buff_len,
  303. if_incoming, 0, send_time);
  304. }
  305. static void forw_packet_free(struct forw_packet *forw_packet)
  306. {
  307. if (forw_packet->skb)
  308. kfree_skb(forw_packet->skb);
  309. kfree(forw_packet);
  310. }
  311. static void _add_bcast_packet_to_list(struct bat_priv *bat_priv,
  312. struct forw_packet *forw_packet,
  313. unsigned long send_time)
  314. {
  315. INIT_HLIST_NODE(&forw_packet->list);
  316. /* add new packet to packet list */
  317. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  318. hlist_add_head(&forw_packet->list, &bat_priv->forw_bcast_list);
  319. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  320. /* start timer for this packet */
  321. INIT_DELAYED_WORK(&forw_packet->delayed_work,
  322. send_outstanding_bcast_packet);
  323. queue_delayed_work(bat_event_workqueue, &forw_packet->delayed_work,
  324. send_time);
  325. }
  326. /* add a broadcast packet to the queue and setup timers. broadcast packets
  327. * are sent multiple times to increase probability for beeing received.
  328. *
  329. * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
  330. * errors.
  331. *
  332. * The skb is not consumed, so the caller should make sure that the
  333. * skb is freed. */
  334. int add_bcast_packet_to_list(struct bat_priv *bat_priv, struct sk_buff *skb)
  335. {
  336. struct forw_packet *forw_packet;
  337. struct bcast_packet *bcast_packet;
  338. if (!atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
  339. bat_dbg(DBG_BATMAN, bat_priv, "bcast packet queue full\n");
  340. goto out;
  341. }
  342. if (!bat_priv->primary_if)
  343. goto out;
  344. forw_packet = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
  345. if (!forw_packet)
  346. goto out_and_inc;
  347. skb = skb_copy(skb, GFP_ATOMIC);
  348. if (!skb)
  349. goto packet_free;
  350. /* as we have a copy now, it is safe to decrease the TTL */
  351. bcast_packet = (struct bcast_packet *)skb->data;
  352. bcast_packet->ttl--;
  353. skb_reset_mac_header(skb);
  354. forw_packet->skb = skb;
  355. forw_packet->if_incoming = bat_priv->primary_if;
  356. /* how often did we send the bcast packet ? */
  357. forw_packet->num_packets = 0;
  358. _add_bcast_packet_to_list(bat_priv, forw_packet, 1);
  359. return NETDEV_TX_OK;
  360. packet_free:
  361. kfree(forw_packet);
  362. out_and_inc:
  363. atomic_inc(&bat_priv->bcast_queue_left);
  364. out:
  365. return NETDEV_TX_BUSY;
  366. }
  367. static void send_outstanding_bcast_packet(struct work_struct *work)
  368. {
  369. struct hard_iface *hard_iface;
  370. struct delayed_work *delayed_work =
  371. container_of(work, struct delayed_work, work);
  372. struct forw_packet *forw_packet =
  373. container_of(delayed_work, struct forw_packet, delayed_work);
  374. struct sk_buff *skb1;
  375. struct net_device *soft_iface = forw_packet->if_incoming->soft_iface;
  376. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  377. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  378. hlist_del(&forw_packet->list);
  379. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  380. if (atomic_read(&bat_priv->mesh_state) == MESH_DEACTIVATING)
  381. goto out;
  382. /* rebroadcast packet */
  383. rcu_read_lock();
  384. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  385. if (hard_iface->soft_iface != soft_iface)
  386. continue;
  387. /* send a copy of the saved skb */
  388. skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
  389. if (skb1)
  390. send_skb_packet(skb1, hard_iface, broadcast_addr);
  391. }
  392. rcu_read_unlock();
  393. forw_packet->num_packets++;
  394. /* if we still have some more bcasts to send */
  395. if (forw_packet->num_packets < 3) {
  396. _add_bcast_packet_to_list(bat_priv, forw_packet,
  397. ((5 * HZ) / 1000));
  398. return;
  399. }
  400. out:
  401. forw_packet_free(forw_packet);
  402. atomic_inc(&bat_priv->bcast_queue_left);
  403. }
  404. void send_outstanding_bat_packet(struct work_struct *work)
  405. {
  406. struct delayed_work *delayed_work =
  407. container_of(work, struct delayed_work, work);
  408. struct forw_packet *forw_packet =
  409. container_of(delayed_work, struct forw_packet, delayed_work);
  410. struct bat_priv *bat_priv;
  411. bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
  412. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  413. hlist_del(&forw_packet->list);
  414. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  415. if (atomic_read(&bat_priv->mesh_state) == MESH_DEACTIVATING)
  416. goto out;
  417. send_packet(forw_packet);
  418. /**
  419. * we have to have at least one packet in the queue
  420. * to determine the queues wake up time unless we are
  421. * shutting down
  422. */
  423. if (forw_packet->own)
  424. schedule_own_packet(forw_packet->if_incoming);
  425. out:
  426. /* don't count own packet */
  427. if (!forw_packet->own)
  428. atomic_inc(&bat_priv->batman_queue_left);
  429. forw_packet_free(forw_packet);
  430. }
  431. void purge_outstanding_packets(struct bat_priv *bat_priv,
  432. struct hard_iface *hard_iface)
  433. {
  434. struct forw_packet *forw_packet;
  435. struct hlist_node *tmp_node, *safe_tmp_node;
  436. if (hard_iface)
  437. bat_dbg(DBG_BATMAN, bat_priv,
  438. "purge_outstanding_packets(): %s\n",
  439. hard_iface->net_dev->name);
  440. else
  441. bat_dbg(DBG_BATMAN, bat_priv,
  442. "purge_outstanding_packets()\n");
  443. /* free bcast list */
  444. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  445. hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
  446. &bat_priv->forw_bcast_list, list) {
  447. /**
  448. * if purge_outstanding_packets() was called with an argmument
  449. * we delete only packets belonging to the given interface
  450. */
  451. if ((hard_iface) &&
  452. (forw_packet->if_incoming != hard_iface))
  453. continue;
  454. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  455. /**
  456. * send_outstanding_bcast_packet() will lock the list to
  457. * delete the item from the list
  458. */
  459. cancel_delayed_work_sync(&forw_packet->delayed_work);
  460. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  461. }
  462. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  463. /* free batman packet list */
  464. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  465. hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
  466. &bat_priv->forw_bat_list, list) {
  467. /**
  468. * if purge_outstanding_packets() was called with an argmument
  469. * we delete only packets belonging to the given interface
  470. */
  471. if ((hard_iface) &&
  472. (forw_packet->if_incoming != hard_iface))
  473. continue;
  474. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  475. /**
  476. * send_outstanding_bat_packet() will lock the list to
  477. * delete the item from the list
  478. */
  479. cancel_delayed_work_sync(&forw_packet->delayed_work);
  480. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  481. }
  482. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  483. }