2 * Copyright (C) 2007-2009 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"
32 /* apply hop penalty for a normal link */
33 static uint8_t hop_penalty(const uint8_t tq)
35 return (tq * (TQ_MAX_VALUE - TQ_HOP_PENALTY)) / (TQ_MAX_VALUE);
38 /* when do we schedule our own packet to be sent */
39 static unsigned long own_send_time(void)
42 (((atomic_read(&originator_interval) - JITTER +
43 (random32() % 2*JITTER)) * HZ) / 1000);
46 /* when do we schedule a forwarded packet to be sent */
47 static unsigned long forward_send_time(void)
49 unsigned long send_time = jiffies; /* Starting now plus... */
51 if (atomic_read(&aggregation_enabled))
52 send_time += (((MAX_AGGREGATION_MS - (JITTER/2) +
53 (random32() % JITTER)) * HZ) / 1000);
55 send_time += (((random32() % (JITTER/2)) * HZ) / 1000);
60 /* send out an already prepared packet to the given address via the
61 * specified batman interface */
62 int send_skb_packet(struct sk_buff *skb,
63 struct batman_if *batman_if,
66 struct ethhdr *ethhdr;
68 if (batman_if->if_active != IF_ACTIVE)
71 if (unlikely(!batman_if->net_dev))
74 if (!(batman_if->net_dev->flags & IFF_UP)) {
76 "batman-adv:Interface %s is not up - can't send packet via that interface!\n",
81 /* push to the ethernet header. */
82 if (my_skb_push(skb, sizeof(struct ethhdr)) < 0)
85 skb_reset_mac_header(skb);
87 ethhdr = (struct ethhdr *) skb_mac_header(skb);
88 memcpy(ethhdr->h_source, batman_if->net_dev->dev_addr, ETH_ALEN);
89 memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
90 ethhdr->h_proto = __constant_htons(ETH_P_BATMAN);
92 skb_set_network_header(skb, ETH_HLEN);
93 skb->priority = TC_PRIO_CONTROL;
94 skb->protocol = __constant_htons(ETH_P_BATMAN);
96 skb->dev = batman_if->net_dev;
98 /* dev_queue_xmit() returns a negative result on error. However on
99 * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
100 * (which is > 0). This will not be treated as an error. */
102 return dev_queue_xmit(skb);
105 return NET_XMIT_DROP;
108 /* sends a raw packet. */
109 void send_raw_packet(unsigned char *pack_buff, int pack_buff_len,
110 struct batman_if *batman_if, uint8_t *dst_addr)
115 skb = dev_alloc_skb(pack_buff_len + sizeof(struct ethhdr));
118 data = skb_put(skb, pack_buff_len + sizeof(struct ethhdr));
119 memcpy(data + sizeof(struct ethhdr), pack_buff, pack_buff_len);
120 /* pull back to the batman "network header" */
121 skb_pull(skb, sizeof(struct ethhdr));
122 send_skb_packet(skb, batman_if, dst_addr);
125 /* Send a packet to a given interface */
126 static void send_packet_to_if(struct forw_packet *forw_packet,
127 struct batman_if *batman_if)
132 struct batman_packet *batman_packet;
134 if (batman_if->if_active != IF_ACTIVE)
139 batman_packet = (struct batman_packet *)
140 (forw_packet->packet_buff);
142 /* adjust all flags and log packets */
143 while (aggregated_packet(buff_pos,
144 forw_packet->packet_len,
145 batman_packet->num_hna)) {
147 /* we might have aggregated direct link packets with an
148 * ordinary base packet */
149 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
150 (forw_packet->if_incoming == batman_if))
151 batman_packet->flags |= DIRECTLINK;
153 batman_packet->flags &= ~DIRECTLINK;
155 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
159 "%s %spacket (originator %pM, seqno %d, TQ %d, TTL %d, IDF %s) on interface %s [%s]\n",
161 (packet_num > 0 ? "aggregated " : ""),
162 batman_packet->orig, ntohs(batman_packet->seqno),
163 batman_packet->tq, batman_packet->ttl,
164 (batman_packet->flags & DIRECTLINK ?
166 batman_if->dev, batman_if->addr_str);
168 buff_pos += sizeof(struct batman_packet) +
169 (batman_packet->num_hna * ETH_ALEN);
171 batman_packet = (struct batman_packet *)
172 (forw_packet->packet_buff + buff_pos);
175 send_raw_packet(forw_packet->packet_buff,
176 forw_packet->packet_len,
177 batman_if, broadcastAddr);
180 /* send a batman packet */
181 static void send_packet(struct forw_packet *forw_packet)
183 struct batman_if *batman_if;
184 struct batman_packet *batman_packet =
185 (struct batman_packet *)(forw_packet->packet_buff);
186 unsigned char directlink = (batman_packet->flags & DIRECTLINK ? 1 : 0);
188 if (!forw_packet->if_incoming) {
189 printk(KERN_ERR "batman-adv: Error - can't forward packet: incoming iface not specified\n");
193 if (forw_packet->if_incoming->if_active != IF_ACTIVE)
196 /* multihomed peer assumed */
197 /* non-primary OGMs are only broadcasted on their interface */
198 if ((directlink && (batman_packet->ttl == 1)) ||
199 (forw_packet->own && (forw_packet->if_incoming->if_num > 0))) {
201 /* FIXME: what about aggregated packets ? */
203 "%s packet (originator %pM, seqno %d, TTL %d) on interface %s [%s]\n",
204 (forw_packet->own ? "Sending own" : "Forwarding"),
205 batman_packet->orig, ntohs(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 unsigned long send_time;
251 struct batman_packet *batman_packet;
252 int vis_server = atomic_read(&vis_mode);
255 * the interface gets activated here to avoid race conditions between
256 * the moment of activating the interface in
257 * hardif_activate_interface() where the originator mac is set and
258 * outdated packets (especially uninitialized mac addresses) in the
261 if (batman_if->if_active == IF_TO_BE_ACTIVATED)
262 batman_if->if_active = IF_ACTIVE;
264 /* if local hna has changed and interface is a primary interface */
265 if ((atomic_read(&hna_local_changed)) && (batman_if->if_num == 0))
266 rebuild_batman_packet(batman_if);
269 * NOTE: packet_buff might just have been re-allocated in
270 * rebuild_batman_packet()
272 batman_packet = (struct batman_packet *)batman_if->packet_buff;
274 /* change sequence number to network order */
275 batman_packet->seqno = htons((uint16_t)atomic_read(&batman_if->seqno));
277 if (vis_server == VIS_TYPE_SERVER_SYNC)
278 batman_packet->flags = VIS_SERVER;
280 batman_packet->flags = 0;
282 /* could be read by receive_bat_packet() */
283 atomic_inc(&batman_if->seqno);
285 slide_own_bcast_window(batman_if);
286 send_time = own_send_time();
287 add_bat_packet_to_list(batman_if->packet_buff,
288 batman_if->packet_len, 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 unsigned char in_tq, in_ttl, tq_avg = 0;
298 unsigned long send_time;
300 if (batman_packet->ttl <= 1) {
301 bat_dbg(DBG_BATMAN, "ttl exceeded \n");
305 in_tq = batman_packet->tq;
306 in_ttl = batman_packet->ttl;
308 batman_packet->ttl--;
309 memcpy(batman_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
311 /* rebroadcast tq of our best ranking neighbor to ensure the rebroadcast
312 * of our best tq value */
313 if ((orig_node->router) && (orig_node->router->tq_avg != 0)) {
315 /* rebroadcast ogm of best ranking neighbor as is */
316 if (!compare_orig(orig_node->router->addr, ethhdr->h_source)) {
317 batman_packet->tq = orig_node->router->tq_avg;
319 if (orig_node->router->last_ttl)
320 batman_packet->ttl = orig_node->router->last_ttl - 1;
323 tq_avg = orig_node->router->tq_avg;
326 /* apply hop penalty */
327 batman_packet->tq = hop_penalty(batman_packet->tq);
329 bat_dbg(DBG_BATMAN, "Forwarding packet: tq_orig: %i, tq_avg: %i, tq_forw: %i, ttl_orig: %i, ttl_forw: %i \n",
330 in_tq, tq_avg, batman_packet->tq, in_ttl - 1,
333 batman_packet->seqno = htons(batman_packet->seqno);
336 batman_packet->flags |= DIRECTLINK;
338 batman_packet->flags &= ~DIRECTLINK;
340 send_time = forward_send_time();
341 add_bat_packet_to_list((unsigned char *)batman_packet,
342 sizeof(struct batman_packet) + hna_buff_len,
343 if_incoming, 0, send_time);
346 static void forw_packet_free(struct forw_packet *forw_packet)
348 if (forw_packet->skb)
349 kfree_skb(forw_packet->skb);
350 kfree(forw_packet->packet_buff);
354 static void _add_bcast_packet_to_list(struct forw_packet *forw_packet,
355 unsigned long send_time)
358 INIT_HLIST_NODE(&forw_packet->list);
360 /* add new packet to packet list */
361 spin_lock_irqsave(&forw_bcast_list_lock, flags);
362 hlist_add_head(&forw_packet->list, &forw_bcast_list);
363 spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
365 /* start timer for this packet */
366 INIT_DELAYED_WORK(&forw_packet->delayed_work,
367 send_outstanding_bcast_packet);
368 queue_delayed_work(bat_event_workqueue, &forw_packet->delayed_work,
372 void add_bcast_packet_to_list(struct sk_buff *skb)
374 struct forw_packet *forw_packet;
376 forw_packet = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
380 skb = skb_copy(skb, GFP_ATOMIC);
386 skb_reset_mac_header(skb);
388 forw_packet->skb = skb;
389 forw_packet->packet_buff = NULL;
391 /* how often did we send the bcast packet ? */
392 forw_packet->num_packets = 0;
394 _add_bcast_packet_to_list(forw_packet, 1);
397 void send_outstanding_bcast_packet(struct work_struct *work)
399 struct batman_if *batman_if;
400 struct delayed_work *delayed_work =
401 container_of(work, struct delayed_work, work);
402 struct forw_packet *forw_packet =
403 container_of(delayed_work, struct forw_packet, delayed_work);
405 struct sk_buff *skb1;
407 spin_lock_irqsave(&forw_bcast_list_lock, flags);
408 hlist_del(&forw_packet->list);
409 spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
411 /* rebroadcast packet */
413 list_for_each_entry_rcu(batman_if, &if_list, list) {
414 /* send a copy of the saved skb */
415 skb1 = skb_copy(forw_packet->skb, GFP_ATOMIC);
417 send_skb_packet(skb1,
418 batman_if, broadcastAddr);
422 forw_packet->num_packets++;
424 /* if we still have some more bcasts to send and we are not shutting
426 if ((forw_packet->num_packets < 3) &&
427 (atomic_read(&module_state) != MODULE_DEACTIVATING))
428 _add_bcast_packet_to_list(forw_packet, ((5 * HZ) / 1000));
430 forw_packet_free(forw_packet);
433 void send_outstanding_bat_packet(struct work_struct *work)
435 struct delayed_work *delayed_work =
436 container_of(work, struct delayed_work, work);
437 struct forw_packet *forw_packet =
438 container_of(delayed_work, struct forw_packet, delayed_work);
441 spin_lock_irqsave(&forw_bat_list_lock, flags);
442 hlist_del(&forw_packet->list);
443 spin_unlock_irqrestore(&forw_bat_list_lock, flags);
445 send_packet(forw_packet);
448 * we have to have at least one packet in the queue
449 * to determine the queues wake up time unless we are
452 if ((forw_packet->own) &&
453 (atomic_read(&module_state) != MODULE_DEACTIVATING))
454 schedule_own_packet(forw_packet->if_incoming);
456 forw_packet_free(forw_packet);
459 void purge_outstanding_packets(void)
461 struct forw_packet *forw_packet;
462 struct hlist_node *tmp_node, *safe_tmp_node;
465 bat_dbg(DBG_BATMAN, "purge_outstanding_packets()\n");
467 /* free bcast list */
468 spin_lock_irqsave(&forw_bcast_list_lock, flags);
469 hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
470 &forw_bcast_list, list) {
472 spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
475 * send_outstanding_bcast_packet() will lock the list to
476 * delete the item from the list
478 cancel_delayed_work_sync(&forw_packet->delayed_work);
479 spin_lock_irqsave(&forw_bcast_list_lock, flags);
481 spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
483 /* free batman packet list */
484 spin_lock_irqsave(&forw_bat_list_lock, flags);
485 hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
486 &forw_bat_list, list) {
488 spin_unlock_irqrestore(&forw_bat_list_lock, flags);
491 * send_outstanding_bat_packet() will lock the list to
492 * delete the item from the list
494 cancel_delayed_work_sync(&forw_packet->delayed_work);
495 spin_lock_irqsave(&forw_bat_list_lock, flags);
497 spin_unlock_irqrestore(&forw_bat_list_lock, flags);