2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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.
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.
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
25 #include "translation-table.h"
26 #include "soft-interface.h"
27 #include "hard-interface.h"
30 #include "aggregation.h"
33 static void send_outstanding_bcast_packet(struct work_struct *work);
35 /* apply hop penalty for a normal link */
36 static uint8_t hop_penalty(const uint8_t tq)
38 return (tq * (TQ_MAX_VALUE - TQ_HOP_PENALTY)) / (TQ_MAX_VALUE);
41 /* when do we schedule our own packet to be sent */
42 static unsigned long own_send_time(struct bat_priv *bat_priv)
44 return jiffies + msecs_to_jiffies(
45 atomic_read(&bat_priv->orig_interval) -
46 JITTER + (random32() % 2*JITTER));
49 /* when do we schedule a forwarded packet to be sent */
50 static unsigned long forward_send_time(struct bat_priv *bat_priv)
52 return jiffies + msecs_to_jiffies(random32() % (JITTER/2));
55 /* send out an already prepared packet to the given address via the
56 * specified batman interface */
57 int send_skb_packet(struct sk_buff *skb,
58 struct batman_if *batman_if,
61 struct ethhdr *ethhdr;
63 if (batman_if->if_status != IF_ACTIVE)
66 if (unlikely(!batman_if->net_dev))
69 if (!(batman_if->net_dev->flags & IFF_UP)) {
70 pr_warning("Interface %s is not up - can't send packet via "
71 "that interface!\n", batman_if->dev);
75 /* push to the ethernet header. */
76 if (my_skb_head_push(skb, sizeof(struct ethhdr)) < 0)
79 skb_reset_mac_header(skb);
81 ethhdr = (struct ethhdr *) skb_mac_header(skb);
82 memcpy(ethhdr->h_source, batman_if->net_dev->dev_addr, ETH_ALEN);
83 memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
84 ethhdr->h_proto = __constant_htons(ETH_P_BATMAN);
86 skb_set_network_header(skb, ETH_HLEN);
87 skb->priority = TC_PRIO_CONTROL;
88 skb->protocol = __constant_htons(ETH_P_BATMAN);
90 skb->dev = batman_if->net_dev;
92 /* dev_queue_xmit() returns a negative result on error. However on
93 * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
94 * (which is > 0). This will not be treated as an error. */
96 return dev_queue_xmit(skb);
102 /* Send a packet to a given interface */
103 static void send_packet_to_if(struct forw_packet *forw_packet,
104 struct batman_if *batman_if)
106 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
110 struct batman_packet *batman_packet;
113 if (batman_if->if_status != IF_ACTIVE)
118 batman_packet = (struct batman_packet *)forw_packet->skb->data;
120 /* adjust all flags and log packets */
121 while (aggregated_packet(buff_pos,
122 forw_packet->packet_len,
123 batman_packet->num_hna)) {
125 /* we might have aggregated direct link packets with an
126 * ordinary base packet */
127 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
128 (forw_packet->if_incoming == batman_if))
129 batman_packet->flags |= DIRECTLINK;
131 batman_packet->flags &= ~DIRECTLINK;
133 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
136 bat_dbg(DBG_BATMAN, bat_priv,
137 "%s %spacket (originator %pM, seqno %d, TQ %d, TTL %d,"
138 " IDF %s) on interface %s [%s]\n",
139 fwd_str, (packet_num > 0 ? "aggregated " : ""),
140 batman_packet->orig, ntohl(batman_packet->seqno),
141 batman_packet->tq, batman_packet->ttl,
142 (batman_packet->flags & DIRECTLINK ?
144 batman_if->dev, batman_if->addr_str);
146 buff_pos += sizeof(struct batman_packet) +
147 (batman_packet->num_hna * ETH_ALEN);
149 batman_packet = (struct batman_packet *)
150 (forw_packet->skb->data + buff_pos);
153 /* create clone because function is called more than once */
154 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
156 send_skb_packet(skb, batman_if, broadcast_addr);
159 /* send a batman packet */
160 static void send_packet(struct forw_packet *forw_packet)
162 struct batman_if *batman_if;
163 struct net_device *soft_iface = forw_packet->if_incoming->soft_iface;
164 struct bat_priv *bat_priv = netdev_priv(soft_iface);
165 struct batman_packet *batman_packet =
166 (struct batman_packet *)(forw_packet->skb->data);
167 unsigned char directlink = (batman_packet->flags & DIRECTLINK ? 1 : 0);
169 if (!forw_packet->if_incoming) {
170 pr_err("Error - can't forward packet: incoming iface not "
175 if (forw_packet->if_incoming->if_status != IF_ACTIVE)
178 /* multihomed peer assumed */
179 /* non-primary OGMs are only broadcasted on their interface */
180 if ((directlink && (batman_packet->ttl == 1)) ||
181 (forw_packet->own && (forw_packet->if_incoming->if_num > 0))) {
183 /* FIXME: what about aggregated packets ? */
184 bat_dbg(DBG_BATMAN, bat_priv,
185 "%s packet (originator %pM, seqno %d, TTL %d) "
186 "on interface %s [%s]\n",
187 (forw_packet->own ? "Sending own" : "Forwarding"),
188 batman_packet->orig, ntohl(batman_packet->seqno),
189 batman_packet->ttl, forw_packet->if_incoming->dev,
190 forw_packet->if_incoming->addr_str);
192 /* skb is only used once and than forw_packet is free'd */
193 send_skb_packet(forw_packet->skb, forw_packet->if_incoming,
195 forw_packet->skb = NULL;
200 /* broadcast on every interface */
202 list_for_each_entry_rcu(batman_if, &if_list, list) {
203 if (batman_if->soft_iface != soft_iface)
206 send_packet_to_if(forw_packet, batman_if);
211 static void rebuild_batman_packet(struct bat_priv *bat_priv,
212 struct batman_if *batman_if)
215 unsigned char *new_buff;
216 struct batman_packet *batman_packet;
218 new_len = sizeof(struct batman_packet) +
219 (bat_priv->num_local_hna * ETH_ALEN);
220 new_buff = kmalloc(new_len, GFP_ATOMIC);
222 /* keep old buffer if kmalloc should fail */
224 memcpy(new_buff, batman_if->packet_buff,
225 sizeof(struct batman_packet));
226 batman_packet = (struct batman_packet *)new_buff;
228 batman_packet->num_hna = hna_local_fill_buffer(bat_priv,
229 new_buff + sizeof(struct batman_packet),
230 new_len - sizeof(struct batman_packet));
232 kfree(batman_if->packet_buff);
233 batman_if->packet_buff = new_buff;
234 batman_if->packet_len = new_len;
238 void schedule_own_packet(struct batman_if *batman_if)
240 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
241 unsigned long send_time;
242 struct batman_packet *batman_packet;
245 if ((batman_if->if_status == IF_NOT_IN_USE) ||
246 (batman_if->if_status == IF_TO_BE_REMOVED))
249 vis_server = atomic_read(&bat_priv->vis_mode);
252 * the interface gets activated here to avoid race conditions between
253 * the moment of activating the interface in
254 * hardif_activate_interface() where the originator mac is set and
255 * outdated packets (especially uninitialized mac addresses) in the
258 if (batman_if->if_status == IF_TO_BE_ACTIVATED)
259 batman_if->if_status = IF_ACTIVE;
261 /* if local hna has changed and interface is a primary interface */
262 if ((atomic_read(&bat_priv->hna_local_changed)) &&
263 (batman_if == bat_priv->primary_if))
264 rebuild_batman_packet(bat_priv, batman_if);
267 * NOTE: packet_buff might just have been re-allocated in
268 * rebuild_batman_packet()
270 batman_packet = (struct batman_packet *)batman_if->packet_buff;
272 /* change sequence number to network order */
273 batman_packet->seqno =
274 htonl((uint32_t)atomic_read(&batman_if->seqno));
276 if (vis_server == VIS_TYPE_SERVER_SYNC)
277 batman_packet->flags |= VIS_SERVER;
279 batman_packet->flags &= ~VIS_SERVER;
281 atomic_inc(&batman_if->seqno);
283 slide_own_bcast_window(batman_if);
284 send_time = own_send_time(bat_priv);
285 add_bat_packet_to_list(bat_priv,
286 batman_if->packet_buff,
287 batman_if->packet_len,
288 batman_if, 1, send_time);
291 void schedule_forward_packet(struct orig_node *orig_node,
292 struct ethhdr *ethhdr,
293 struct batman_packet *batman_packet,
294 uint8_t directlink, int hna_buff_len,
295 struct batman_if *if_incoming)
297 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
298 unsigned char in_tq, in_ttl, tq_avg = 0;
299 unsigned long send_time;
301 if (batman_packet->ttl <= 1) {
302 bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
306 in_tq = batman_packet->tq;
307 in_ttl = batman_packet->ttl;
309 batman_packet->ttl--;
310 memcpy(batman_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
312 /* rebroadcast tq of our best ranking neighbor to ensure the rebroadcast
313 * of our best tq value */
314 if ((orig_node->router) && (orig_node->router->tq_avg != 0)) {
316 /* rebroadcast ogm of best ranking neighbor as is */
317 if (!compare_orig(orig_node->router->addr, ethhdr->h_source)) {
318 batman_packet->tq = orig_node->router->tq_avg;
320 if (orig_node->router->last_ttl)
321 batman_packet->ttl = orig_node->router->last_ttl
325 tq_avg = orig_node->router->tq_avg;
328 /* apply hop penalty */
329 batman_packet->tq = hop_penalty(batman_packet->tq);
331 bat_dbg(DBG_BATMAN, bat_priv,
332 "Forwarding packet: tq_orig: %i, tq_avg: %i, "
333 "tq_forw: %i, ttl_orig: %i, ttl_forw: %i\n",
334 in_tq, tq_avg, batman_packet->tq, in_ttl - 1,
337 batman_packet->seqno = htonl(batman_packet->seqno);
339 /* switch of primaries first hop flag when forwarding */
340 batman_packet->flags &= ~PRIMARIES_FIRST_HOP;
342 batman_packet->flags |= DIRECTLINK;
344 batman_packet->flags &= ~DIRECTLINK;
346 send_time = forward_send_time(bat_priv);
347 add_bat_packet_to_list(bat_priv,
348 (unsigned char *)batman_packet,
349 sizeof(struct batman_packet) + hna_buff_len,
350 if_incoming, 0, send_time);
353 static void forw_packet_free(struct forw_packet *forw_packet)
355 if (forw_packet->skb)
356 kfree_skb(forw_packet->skb);
360 static void _add_bcast_packet_to_list(struct bat_priv *bat_priv,
361 struct forw_packet *forw_packet,
362 unsigned long send_time)
365 INIT_HLIST_NODE(&forw_packet->list);
367 /* add new packet to packet list */
368 spin_lock_irqsave(&bat_priv->forw_bcast_list_lock, flags);
369 hlist_add_head(&forw_packet->list, &bat_priv->forw_bcast_list);
370 spin_unlock_irqrestore(&bat_priv->forw_bcast_list_lock, flags);
372 /* start timer for this packet */
373 INIT_DELAYED_WORK(&forw_packet->delayed_work,
374 send_outstanding_bcast_packet);
375 queue_delayed_work(bat_event_workqueue, &forw_packet->delayed_work,
379 #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
380 /* add a broadcast packet to the queue and setup timers. broadcast packets
381 * are sent multiple times to increase probability for beeing received.
383 * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
386 * The skb is not consumed, so the caller should make sure that the
388 int add_bcast_packet_to_list(struct bat_priv *bat_priv, struct sk_buff *skb)
390 struct forw_packet *forw_packet;
391 struct bcast_packet *bcast_packet;
393 if (!atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
394 bat_dbg(DBG_BATMAN, bat_priv, "bcast packet queue full\n");
398 if (!bat_priv->primary_if)
401 forw_packet = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
406 skb = skb_copy(skb, GFP_ATOMIC);
410 /* as we have a copy now, it is safe to decrease the TTL */
411 bcast_packet = (struct bcast_packet *)skb->data;
414 skb_reset_mac_header(skb);
416 forw_packet->skb = skb;
417 forw_packet->if_incoming = bat_priv->primary_if;
419 /* how often did we send the bcast packet ? */
420 forw_packet->num_packets = 0;
422 _add_bcast_packet_to_list(bat_priv, forw_packet, 1);
428 atomic_inc(&bat_priv->bcast_queue_left);
430 return NETDEV_TX_BUSY;
433 static void send_outstanding_bcast_packet(struct work_struct *work)
435 struct batman_if *batman_if;
436 struct delayed_work *delayed_work =
437 container_of(work, struct delayed_work, work);
438 struct forw_packet *forw_packet =
439 container_of(delayed_work, struct forw_packet, delayed_work);
441 struct sk_buff *skb1;
442 struct net_device *soft_iface = forw_packet->if_incoming->soft_iface;
443 struct bat_priv *bat_priv = netdev_priv(soft_iface);
445 spin_lock_irqsave(&bat_priv->forw_bcast_list_lock, flags);
446 hlist_del(&forw_packet->list);
447 spin_unlock_irqrestore(&bat_priv->forw_bcast_list_lock, flags);
449 if (atomic_read(&bat_priv->mesh_state) == MESH_DEACTIVATING)
452 /* rebroadcast packet */
454 list_for_each_entry_rcu(batman_if, &if_list, list) {
455 if (batman_if->soft_iface != soft_iface)
458 /* send a copy of the saved skb */
459 skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
461 send_skb_packet(skb1, batman_if, broadcast_addr);
465 forw_packet->num_packets++;
467 /* if we still have some more bcasts to send */
468 if (forw_packet->num_packets < 3) {
469 _add_bcast_packet_to_list(bat_priv, forw_packet,
475 forw_packet_free(forw_packet);
476 atomic_inc(&bat_priv->bcast_queue_left);
479 void send_outstanding_bat_packet(struct work_struct *work)
481 struct delayed_work *delayed_work =
482 container_of(work, struct delayed_work, work);
483 struct forw_packet *forw_packet =
484 container_of(delayed_work, struct forw_packet, delayed_work);
486 struct bat_priv *bat_priv;
488 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
489 spin_lock_irqsave(&bat_priv->forw_bat_list_lock, flags);
490 hlist_del(&forw_packet->list);
491 spin_unlock_irqrestore(&bat_priv->forw_bat_list_lock, flags);
493 if (atomic_read(&bat_priv->mesh_state) == MESH_DEACTIVATING)
496 send_packet(forw_packet);
499 * we have to have at least one packet in the queue
500 * to determine the queues wake up time unless we are
503 if (forw_packet->own)
504 schedule_own_packet(forw_packet->if_incoming);
507 /* don't count own packet */
508 if (!forw_packet->own)
509 atomic_inc(&bat_priv->batman_queue_left);
511 forw_packet_free(forw_packet);
514 void purge_outstanding_packets(struct bat_priv *bat_priv,
515 struct batman_if *batman_if)
517 struct forw_packet *forw_packet;
518 struct hlist_node *tmp_node, *safe_tmp_node;
522 bat_dbg(DBG_BATMAN, bat_priv,
523 "purge_outstanding_packets(): %s\n",
526 bat_dbg(DBG_BATMAN, bat_priv,
527 "purge_outstanding_packets()\n");
529 /* free bcast list */
530 spin_lock_irqsave(&bat_priv->forw_bcast_list_lock, flags);
531 hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
532 &bat_priv->forw_bcast_list, list) {
535 * if purge_outstanding_packets() was called with an argmument
536 * we delete only packets belonging to the given interface
539 (forw_packet->if_incoming != batman_if))
542 spin_unlock_irqrestore(&bat_priv->forw_bcast_list_lock, flags);
545 * send_outstanding_bcast_packet() will lock the list to
546 * delete the item from the list
548 cancel_delayed_work_sync(&forw_packet->delayed_work);
549 spin_lock_irqsave(&bat_priv->forw_bcast_list_lock, flags);
551 spin_unlock_irqrestore(&bat_priv->forw_bcast_list_lock, flags);
553 /* free batman packet list */
554 spin_lock_irqsave(&bat_priv->forw_bat_list_lock, flags);
555 hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
556 &bat_priv->forw_bat_list, list) {
559 * if purge_outstanding_packets() was called with an argmument
560 * we delete only packets belonging to the given interface
563 (forw_packet->if_incoming != batman_if))
566 spin_unlock_irqrestore(&bat_priv->forw_bat_list_lock, flags);
569 * send_outstanding_bat_packet() will lock the list to
570 * delete the item from the list
572 cancel_delayed_work_sync(&forw_packet->delayed_work);
573 spin_lock_irqsave(&bat_priv->forw_bat_list_lock, flags);
575 spin_unlock_irqrestore(&bat_priv->forw_bat_list_lock, flags);