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_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 /* sends a raw packet. */
103 void send_raw_packet(unsigned char *pack_buff, int pack_buff_len,
104 struct batman_if *batman_if, uint8_t *dst_addr)
109 skb = dev_alloc_skb(pack_buff_len + sizeof(struct ethhdr));
112 data = skb_put(skb, pack_buff_len + sizeof(struct ethhdr));
113 memcpy(data + sizeof(struct ethhdr), pack_buff, pack_buff_len);
114 /* pull back to the batman "network header" */
115 skb_pull(skb, sizeof(struct ethhdr));
116 send_skb_packet(skb, batman_if, dst_addr);
119 /* Send a packet to a given interface */
120 static void send_packet_to_if(struct forw_packet *forw_packet,
121 struct batman_if *batman_if)
123 /* FIXME: each batman_if will be attached to a softif */
124 struct bat_priv *bat_priv = netdev_priv(soft_device);
128 struct batman_packet *batman_packet;
130 if (batman_if->if_status != IF_ACTIVE)
135 batman_packet = (struct batman_packet *)
136 (forw_packet->packet_buff);
138 /* adjust all flags and log packets */
139 while (aggregated_packet(buff_pos,
140 forw_packet->packet_len,
141 batman_packet->num_hna)) {
143 /* we might have aggregated direct link packets with an
144 * ordinary base packet */
145 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
146 (forw_packet->if_incoming == batman_if))
147 batman_packet->flags |= DIRECTLINK;
149 batman_packet->flags &= ~DIRECTLINK;
151 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
154 bat_dbg(DBG_BATMAN, bat_priv,
155 "%s %spacket (originator %pM, seqno %d, TQ %d, TTL %d,"
156 " IDF %s) on interface %s [%s]\n",
157 fwd_str, (packet_num > 0 ? "aggregated " : ""),
158 batman_packet->orig, ntohl(batman_packet->seqno),
159 batman_packet->tq, batman_packet->ttl,
160 (batman_packet->flags & DIRECTLINK ?
162 batman_if->dev, batman_if->addr_str);
164 buff_pos += sizeof(struct batman_packet) +
165 (batman_packet->num_hna * ETH_ALEN);
167 batman_packet = (struct batman_packet *)
168 (forw_packet->packet_buff + buff_pos);
171 send_raw_packet(forw_packet->packet_buff,
172 forw_packet->packet_len,
173 batman_if, broadcast_addr);
176 /* send a batman packet */
177 static void send_packet(struct forw_packet *forw_packet)
179 /* FIXME: each batman_if will be attached to a softif */
180 struct bat_priv *bat_priv = netdev_priv(soft_device);
181 struct batman_if *batman_if;
182 struct batman_packet *batman_packet =
183 (struct batman_packet *)(forw_packet->packet_buff);
184 unsigned char directlink = (batman_packet->flags & DIRECTLINK ? 1 : 0);
186 if (!forw_packet->if_incoming) {
187 pr_err("Error - can't forward packet: incoming iface not "
192 if (forw_packet->if_incoming->if_status != IF_ACTIVE)
195 /* multihomed peer assumed */
196 /* non-primary OGMs are only broadcasted on their interface */
197 if ((directlink && (batman_packet->ttl == 1)) ||
198 (forw_packet->own && (forw_packet->if_incoming->if_num > 0))) {
200 /* FIXME: what about aggregated packets ? */
201 bat_dbg(DBG_BATMAN, bat_priv,
202 "%s packet (originator %pM, seqno %d, TTL %d) "
203 "on interface %s [%s]\n",
204 (forw_packet->own ? "Sending own" : "Forwarding"),
205 batman_packet->orig, ntohl(batman_packet->seqno),
206 batman_packet->ttl, forw_packet->if_incoming->dev,
207 forw_packet->if_incoming->addr_str);
209 send_raw_packet(forw_packet->packet_buff,
210 forw_packet->packet_len,
211 forw_packet->if_incoming,
216 /* broadcast on every interface */
218 list_for_each_entry_rcu(batman_if, &if_list, list)
219 send_packet_to_if(forw_packet, batman_if);
223 static void rebuild_batman_packet(struct batman_if *batman_if)
226 unsigned char *new_buff;
227 struct batman_packet *batman_packet;
229 new_len = sizeof(struct batman_packet) + (num_hna * ETH_ALEN);
230 new_buff = kmalloc(new_len, GFP_ATOMIC);
232 /* keep old buffer if kmalloc should fail */
234 memcpy(new_buff, batman_if->packet_buff,
235 sizeof(struct batman_packet));
236 batman_packet = (struct batman_packet *)new_buff;
238 batman_packet->num_hna = hna_local_fill_buffer(
239 new_buff + sizeof(struct batman_packet),
240 new_len - sizeof(struct batman_packet));
242 kfree(batman_if->packet_buff);
243 batman_if->packet_buff = new_buff;
244 batman_if->packet_len = new_len;
248 void schedule_own_packet(struct batman_if *batman_if)
250 /* FIXME: each batman_if will be attached to a softif */
251 struct bat_priv *bat_priv = netdev_priv(soft_device);
252 unsigned long send_time;
253 struct batman_packet *batman_packet;
256 if ((batman_if->if_status == IF_NOT_IN_USE) ||
257 (batman_if->if_status == IF_TO_BE_REMOVED))
260 vis_server = atomic_read(&bat_priv->vis_mode);
263 * the interface gets activated here to avoid race conditions between
264 * the moment of activating the interface in
265 * hardif_activate_interface() where the originator mac is set and
266 * outdated packets (especially uninitialized mac addresses) in the
269 if (batman_if->if_status == IF_TO_BE_ACTIVATED)
270 batman_if->if_status = IF_ACTIVE;
272 /* if local hna has changed and interface is a primary interface */
273 if ((atomic_read(&hna_local_changed)) &&
274 (batman_if == bat_priv->primary_if))
275 rebuild_batman_packet(batman_if);
278 * NOTE: packet_buff might just have been re-allocated in
279 * rebuild_batman_packet()
281 batman_packet = (struct batman_packet *)batman_if->packet_buff;
283 /* change sequence number to network order */
284 batman_packet->seqno =
285 htonl((uint32_t)atomic_read(&batman_if->seqno));
287 if (vis_server == VIS_TYPE_SERVER_SYNC)
288 batman_packet->flags |= VIS_SERVER;
290 batman_packet->flags &= ~VIS_SERVER;
292 atomic_inc(&batman_if->seqno);
294 slide_own_bcast_window(batman_if);
295 send_time = own_send_time(bat_priv);
296 add_bat_packet_to_list(bat_priv,
297 batman_if->packet_buff,
298 batman_if->packet_len,
299 batman_if, 1, send_time);
302 void schedule_forward_packet(struct orig_node *orig_node,
303 struct ethhdr *ethhdr,
304 struct batman_packet *batman_packet,
305 uint8_t directlink, int hna_buff_len,
306 struct batman_if *if_incoming)
308 /* FIXME: each batman_if will be attached to a softif */
309 struct bat_priv *bat_priv = netdev_priv(soft_device);
310 unsigned char in_tq, in_ttl, tq_avg = 0;
311 unsigned long send_time;
313 if (batman_packet->ttl <= 1) {
314 bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
318 in_tq = batman_packet->tq;
319 in_ttl = batman_packet->ttl;
321 batman_packet->ttl--;
322 memcpy(batman_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
324 /* rebroadcast tq of our best ranking neighbor to ensure the rebroadcast
325 * of our best tq value */
326 if ((orig_node->router) && (orig_node->router->tq_avg != 0)) {
328 /* rebroadcast ogm of best ranking neighbor as is */
329 if (!compare_orig(orig_node->router->addr, ethhdr->h_source)) {
330 batman_packet->tq = orig_node->router->tq_avg;
332 if (orig_node->router->last_ttl)
333 batman_packet->ttl = orig_node->router->last_ttl
337 tq_avg = orig_node->router->tq_avg;
340 /* apply hop penalty */
341 batman_packet->tq = hop_penalty(batman_packet->tq);
343 bat_dbg(DBG_BATMAN, bat_priv,
344 "Forwarding packet: tq_orig: %i, tq_avg: %i, "
345 "tq_forw: %i, ttl_orig: %i, ttl_forw: %i\n",
346 in_tq, tq_avg, batman_packet->tq, in_ttl - 1,
349 batman_packet->seqno = htonl(batman_packet->seqno);
351 /* switch of primaries first hop flag when forwarding */
352 batman_packet->flags &= ~PRIMARIES_FIRST_HOP;
354 batman_packet->flags |= DIRECTLINK;
356 batman_packet->flags &= ~DIRECTLINK;
358 send_time = forward_send_time(bat_priv);
359 add_bat_packet_to_list(bat_priv,
360 (unsigned char *)batman_packet,
361 sizeof(struct batman_packet) + hna_buff_len,
362 if_incoming, 0, send_time);
365 static void forw_packet_free(struct forw_packet *forw_packet)
367 if (forw_packet->skb)
368 kfree_skb(forw_packet->skb);
369 kfree(forw_packet->packet_buff);
373 static void _add_bcast_packet_to_list(struct forw_packet *forw_packet,
374 unsigned long send_time)
377 INIT_HLIST_NODE(&forw_packet->list);
379 /* add new packet to packet list */
380 spin_lock_irqsave(&forw_bcast_list_lock, flags);
381 hlist_add_head(&forw_packet->list, &forw_bcast_list);
382 spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
384 /* start timer for this packet */
385 INIT_DELAYED_WORK(&forw_packet->delayed_work,
386 send_outstanding_bcast_packet);
387 queue_delayed_work(bat_event_workqueue, &forw_packet->delayed_work,
391 #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
392 /* add a broadcast packet to the queue and setup timers. broadcast packets
393 * are sent multiple times to increase probability for beeing received.
395 * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
398 * The skb is not consumed, so the caller should make sure that the
400 int add_bcast_packet_to_list(struct sk_buff *skb)
402 struct forw_packet *forw_packet;
403 struct bcast_packet *bcast_packet;
404 /* FIXME: each batman_if will be attached to a softif */
405 struct bat_priv *bat_priv = netdev_priv(soft_device);
407 if (!atomic_dec_not_zero(&bcast_queue_left)) {
408 bat_dbg(DBG_BATMAN, bat_priv, "bcast packet queue full\n");
412 forw_packet = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
417 skb = skb_copy(skb, GFP_ATOMIC);
421 /* as we have a copy now, it is safe to decrease the TTL */
422 bcast_packet = (struct bcast_packet *)skb->data;
425 skb_reset_mac_header(skb);
427 forw_packet->skb = skb;
428 forw_packet->packet_buff = NULL;
430 /* how often did we send the bcast packet ? */
431 forw_packet->num_packets = 0;
433 _add_bcast_packet_to_list(forw_packet, 1);
439 atomic_inc(&bcast_queue_left);
441 return NETDEV_TX_BUSY;
444 static void send_outstanding_bcast_packet(struct work_struct *work)
446 struct batman_if *batman_if;
447 struct delayed_work *delayed_work =
448 container_of(work, struct delayed_work, work);
449 struct forw_packet *forw_packet =
450 container_of(delayed_work, struct forw_packet, delayed_work);
452 struct sk_buff *skb1;
454 spin_lock_irqsave(&forw_bcast_list_lock, flags);
455 hlist_del(&forw_packet->list);
456 spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
458 if (atomic_read(&module_state) == MODULE_DEACTIVATING)
461 /* rebroadcast packet */
463 list_for_each_entry_rcu(batman_if, &if_list, list) {
464 /* send a copy of the saved skb */
465 skb1 = skb_copy(forw_packet->skb, GFP_ATOMIC);
467 send_skb_packet(skb1,
468 batman_if, broadcast_addr);
472 forw_packet->num_packets++;
474 /* if we still have some more bcasts to send */
475 if (forw_packet->num_packets < 3) {
476 _add_bcast_packet_to_list(forw_packet, ((5 * HZ) / 1000));
481 forw_packet_free(forw_packet);
482 atomic_inc(&bcast_queue_left);
485 void send_outstanding_bat_packet(struct work_struct *work)
487 struct delayed_work *delayed_work =
488 container_of(work, struct delayed_work, work);
489 struct forw_packet *forw_packet =
490 container_of(delayed_work, struct forw_packet, delayed_work);
493 spin_lock_irqsave(&forw_bat_list_lock, flags);
494 hlist_del(&forw_packet->list);
495 spin_unlock_irqrestore(&forw_bat_list_lock, flags);
497 if (atomic_read(&module_state) == MODULE_DEACTIVATING)
500 send_packet(forw_packet);
503 * we have to have at least one packet in the queue
504 * to determine the queues wake up time unless we are
507 if (forw_packet->own)
508 schedule_own_packet(forw_packet->if_incoming);
511 /* don't count own packet */
512 if (!forw_packet->own)
513 atomic_inc(&batman_queue_left);
515 forw_packet_free(forw_packet);
518 void purge_outstanding_packets(struct batman_if *batman_if)
520 /* FIXME: each batman_if will be attached to a softif */
521 struct bat_priv *bat_priv = netdev_priv(soft_device);
522 struct forw_packet *forw_packet;
523 struct hlist_node *tmp_node, *safe_tmp_node;
527 bat_dbg(DBG_BATMAN, bat_priv,
528 "purge_outstanding_packets(): %s\n",
531 bat_dbg(DBG_BATMAN, bat_priv,
532 "purge_outstanding_packets()\n");
534 /* free bcast list */
535 spin_lock_irqsave(&forw_bcast_list_lock, flags);
536 hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
537 &forw_bcast_list, list) {
540 * if purge_outstanding_packets() was called with an argmument
541 * we delete only packets belonging to the given interface
544 (forw_packet->if_incoming != batman_if))
547 spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
550 * send_outstanding_bcast_packet() will lock the list to
551 * delete the item from the list
553 cancel_delayed_work_sync(&forw_packet->delayed_work);
554 spin_lock_irqsave(&forw_bcast_list_lock, flags);
556 spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
558 /* free batman packet list */
559 spin_lock_irqsave(&forw_bat_list_lock, flags);
560 hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
561 &forw_bat_list, list) {
564 * if purge_outstanding_packets() was called with an argmument
565 * we delete only packets belonging to the given interface
568 (forw_packet->if_incoming != batman_if))
571 spin_unlock_irqrestore(&forw_bat_list_lock, flags);
574 * send_outstanding_bat_packet() will lock the list to
575 * delete the item from the list
577 cancel_delayed_work_sync(&forw_packet->delayed_work);
578 spin_lock_irqsave(&forw_bat_list_lock, flags);
580 spin_unlock_irqrestore(&forw_bat_list_lock, flags);