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
26 #include "soft-interface.h"
27 #include "hard-interface.h"
28 #include "icmp_socket.h"
29 #include "translation-table.h"
30 #include "originator.h"
32 #include "ring_buffer.h"
34 #include "aggregation.h"
36 static DECLARE_WAIT_QUEUE_HEAD(thread_wait
);
38 void slide_own_bcast_window(struct batman_if
*batman_if
)
41 struct orig_node
*orig_node
;
45 spin_lock_irqsave(&orig_hash_lock
, flags
);
47 while (hash_iterate(orig_hash
, &hashit
)) {
48 orig_node
= hashit
.bucket
->data
;
49 word
= &(orig_node
->bcast_own
[batman_if
->if_num
* NUM_WORDS
]);
51 bit_get_packet(word
, 1, 0);
52 orig_node
->bcast_own_sum
[batman_if
->if_num
] =
53 bit_packet_count(word
);
56 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
59 static void update_HNA(struct orig_node
*orig_node
,
60 unsigned char *hna_buff
, int hna_buff_len
)
62 if ((hna_buff_len
!= orig_node
->hna_buff_len
) ||
63 ((hna_buff_len
> 0) &&
64 (orig_node
->hna_buff_len
> 0) &&
65 (memcmp(orig_node
->hna_buff
, hna_buff
, hna_buff_len
) != 0))) {
67 if (orig_node
->hna_buff_len
> 0)
68 hna_global_del_orig(orig_node
,
69 "originator changed hna");
71 if ((hna_buff_len
> 0) && (hna_buff
!= NULL
))
72 hna_global_add_orig(orig_node
, hna_buff
, hna_buff_len
);
76 static void update_route(struct orig_node
*orig_node
,
77 struct neigh_node
*neigh_node
,
78 unsigned char *hna_buff
, int hna_buff_len
)
81 if ((orig_node
->router
!= NULL
) && (neigh_node
== NULL
)) {
83 bat_dbg(DBG_ROUTES
, "Deleting route towards: %pM\n",
85 hna_global_del_orig(orig_node
, "originator timed out");
88 } else if ((orig_node
->router
== NULL
) && (neigh_node
!= NULL
)) {
91 "Adding route towards: %pM (via %pM)\n",
92 orig_node
->orig
, neigh_node
->addr
);
93 hna_global_add_orig(orig_node
, hna_buff
, hna_buff_len
);
98 "Changing route towards: %pM "
99 "(now via %pM - was via %pM)\n",
100 orig_node
->orig
, neigh_node
->addr
,
101 orig_node
->router
->addr
);
104 orig_node
->router
= neigh_node
;
108 void update_routes(struct orig_node
*orig_node
,
109 struct neigh_node
*neigh_node
,
110 unsigned char *hna_buff
, int hna_buff_len
)
113 if (orig_node
== NULL
)
116 if (orig_node
->router
!= neigh_node
)
117 update_route(orig_node
, neigh_node
, hna_buff
, hna_buff_len
);
118 /* may be just HNA changed */
120 update_HNA(orig_node
, hna_buff
, hna_buff_len
);
123 static int isBidirectionalNeigh(struct orig_node
*orig_node
,
124 struct orig_node
*orig_neigh_node
,
125 struct batman_packet
*batman_packet
,
126 struct batman_if
*if_incoming
)
128 struct neigh_node
*neigh_node
= NULL
, *tmp_neigh_node
= NULL
;
129 unsigned char total_count
;
131 if (orig_node
== orig_neigh_node
) {
132 list_for_each_entry(tmp_neigh_node
,
133 &orig_node
->neigh_list
,
136 if (compare_orig(tmp_neigh_node
->addr
,
137 orig_neigh_node
->orig
) &&
138 (tmp_neigh_node
->if_incoming
== if_incoming
))
139 neigh_node
= tmp_neigh_node
;
143 neigh_node
= create_neighbor(orig_node
,
145 orig_neigh_node
->orig
,
147 /* create_neighbor failed, return 0 */
151 neigh_node
->last_valid
= jiffies
;
153 /* find packet count of corresponding one hop neighbor */
154 list_for_each_entry(tmp_neigh_node
,
155 &orig_neigh_node
->neigh_list
, list
) {
157 if (compare_orig(tmp_neigh_node
->addr
,
158 orig_neigh_node
->orig
) &&
159 (tmp_neigh_node
->if_incoming
== if_incoming
))
160 neigh_node
= tmp_neigh_node
;
164 neigh_node
= create_neighbor(orig_neigh_node
,
166 orig_neigh_node
->orig
,
168 /* create_neighbor failed, return 0 */
173 orig_node
->last_valid
= jiffies
;
175 /* pay attention to not get a value bigger than 100 % */
176 total_count
= (orig_neigh_node
->bcast_own_sum
[if_incoming
->if_num
] >
177 neigh_node
->real_packet_count
?
178 neigh_node
->real_packet_count
:
179 orig_neigh_node
->bcast_own_sum
[if_incoming
->if_num
]);
181 /* if we have too few packets (too less data) we set tq_own to zero */
182 /* if we receive too few packets it is not considered bidirectional */
183 if ((total_count
< TQ_LOCAL_BIDRECT_SEND_MINIMUM
) ||
184 (neigh_node
->real_packet_count
< TQ_LOCAL_BIDRECT_RECV_MINIMUM
))
185 orig_neigh_node
->tq_own
= 0;
187 /* neigh_node->real_packet_count is never zero as we
188 * only purge old information when getting new
190 orig_neigh_node
->tq_own
= (TQ_MAX_VALUE
* total_count
) /
191 neigh_node
->real_packet_count
;
194 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
195 * affect the nearly-symmetric links only a little, but
196 * punishes asymmetric links more. This will give a value
197 * between 0 and TQ_MAX_VALUE
199 orig_neigh_node
->tq_asym_penalty
=
202 (TQ_LOCAL_WINDOW_SIZE
- neigh_node
->real_packet_count
) *
203 (TQ_LOCAL_WINDOW_SIZE
- neigh_node
->real_packet_count
) *
204 (TQ_LOCAL_WINDOW_SIZE
- neigh_node
->real_packet_count
)) /
205 (TQ_LOCAL_WINDOW_SIZE
*
206 TQ_LOCAL_WINDOW_SIZE
*
207 TQ_LOCAL_WINDOW_SIZE
);
209 batman_packet
->tq
= ((batman_packet
->tq
*
210 orig_neigh_node
->tq_own
*
211 orig_neigh_node
->tq_asym_penalty
) /
212 (TQ_MAX_VALUE
* TQ_MAX_VALUE
));
216 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
217 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
219 orig_node
->orig
, orig_neigh_node
->orig
, total_count
,
220 neigh_node
->real_packet_count
, orig_neigh_node
->tq_own
,
221 orig_neigh_node
->tq_asym_penalty
, batman_packet
->tq
);
223 /* if link has the minimum required transmission quality
224 * consider it bidirectional */
225 if (batman_packet
->tq
>= TQ_TOTAL_BIDRECT_LIMIT
)
231 static void update_orig(struct orig_node
*orig_node
, struct ethhdr
*ethhdr
,
232 struct batman_packet
*batman_packet
,
233 struct batman_if
*if_incoming
,
234 unsigned char *hna_buff
, int hna_buff_len
,
237 struct neigh_node
*neigh_node
= NULL
, *tmp_neigh_node
= NULL
;
238 int tmp_hna_buff_len
;
240 bat_dbg(DBG_BATMAN
, "update_originator(): "
241 "Searching and updating originator entry of received packet\n");
243 list_for_each_entry(tmp_neigh_node
, &orig_node
->neigh_list
, list
) {
244 if (compare_orig(tmp_neigh_node
->addr
, ethhdr
->h_source
) &&
245 (tmp_neigh_node
->if_incoming
== if_incoming
)) {
246 neigh_node
= tmp_neigh_node
;
253 ring_buffer_set(tmp_neigh_node
->tq_recv
,
254 &tmp_neigh_node
->tq_index
, 0);
255 tmp_neigh_node
->tq_avg
=
256 ring_buffer_avg(tmp_neigh_node
->tq_recv
);
260 struct orig_node
*orig_tmp
;
262 orig_tmp
= get_orig_node(ethhdr
->h_source
);
266 neigh_node
= create_neighbor(orig_node
,
268 ethhdr
->h_source
, if_incoming
);
273 "Updating existing last-hop neighbor of originator\n");
275 orig_node
->flags
= batman_packet
->flags
;
276 neigh_node
->last_valid
= jiffies
;
278 ring_buffer_set(neigh_node
->tq_recv
,
279 &neigh_node
->tq_index
,
281 neigh_node
->tq_avg
= ring_buffer_avg(neigh_node
->tq_recv
);
284 orig_node
->last_ttl
= batman_packet
->ttl
;
285 neigh_node
->last_ttl
= batman_packet
->ttl
;
288 tmp_hna_buff_len
= (hna_buff_len
> batman_packet
->num_hna
* ETH_ALEN
?
289 batman_packet
->num_hna
* ETH_ALEN
: hna_buff_len
);
291 /* if this neighbor already is our next hop there is nothing
293 if (orig_node
->router
== neigh_node
)
296 /* if this neighbor does not offer a better TQ we won't consider it */
297 if ((orig_node
->router
) &&
298 (orig_node
->router
->tq_avg
> neigh_node
->tq_avg
))
301 /* if the TQ is the same and the link not more symetric we
302 * won't consider it either */
303 if ((orig_node
->router
) &&
304 ((neigh_node
->tq_avg
== orig_node
->router
->tq_avg
) &&
305 (orig_node
->router
->orig_node
->bcast_own_sum
[if_incoming
->if_num
]
306 >= neigh_node
->orig_node
->bcast_own_sum
[if_incoming
->if_num
])))
309 update_routes(orig_node
, neigh_node
, hna_buff
, tmp_hna_buff_len
);
313 update_routes(orig_node
, orig_node
->router
, hna_buff
, tmp_hna_buff_len
);
316 /* checks whether the host restarted and is in the protection time.
318 * 0 if the packet is to be accepted
319 * 1 if the packet is to be ignored.
321 static int window_protected(int16_t seq_num_diff
,
322 unsigned long *last_reset
)
324 if ((seq_num_diff
<= -TQ_LOCAL_WINDOW_SIZE
)
325 || (seq_num_diff
>= EXPECTED_SEQNO_RANGE
)) {
326 if (time_after(jiffies
, *last_reset
+
327 msecs_to_jiffies(RESET_PROTECTION_MS
))) {
329 *last_reset
= jiffies
;
331 "old packet received, start protection\n");
340 /* processes a batman packet for all interfaces, adjusts the sequence number and
341 * finds out whether it is a duplicate.
343 * 1 the packet is a duplicate
344 * 0 the packet has not yet been received
345 * -1 the packet is old and has been received while the seqno window
346 * was protected. Caller should drop it.
348 static char count_real_packets(struct ethhdr
*ethhdr
,
349 struct batman_packet
*batman_packet
,
350 struct batman_if
*if_incoming
)
352 struct orig_node
*orig_node
;
353 struct neigh_node
*tmp_neigh_node
;
354 char is_duplicate
= 0;
359 orig_node
= get_orig_node(batman_packet
->orig
);
360 if (orig_node
== NULL
)
363 seq_diff
= batman_packet
->seqno
- orig_node
->last_real_seqno
;
365 /* signalize caller that the packet is to be dropped. */
366 if (window_protected(seq_diff
, &orig_node
->batman_seqno_reset
))
369 list_for_each_entry(tmp_neigh_node
, &orig_node
->neigh_list
, list
) {
371 is_duplicate
|= get_bit_status(tmp_neigh_node
->real_bits
,
372 orig_node
->last_real_seqno
,
373 batman_packet
->seqno
);
375 if (compare_orig(tmp_neigh_node
->addr
, ethhdr
->h_source
) &&
376 (tmp_neigh_node
->if_incoming
== if_incoming
))
381 /* if the window moved, set the update flag. */
382 need_update
|= bit_get_packet(tmp_neigh_node
->real_bits
,
385 tmp_neigh_node
->real_packet_count
=
386 bit_packet_count(tmp_neigh_node
->real_bits
);
390 bat_dbg(DBG_BATMAN
, "updating last_seqno: old %d, new %d\n",
391 orig_node
->last_real_seqno
, batman_packet
->seqno
);
392 orig_node
->last_real_seqno
= batman_packet
->seqno
;
398 void receive_bat_packet(struct ethhdr
*ethhdr
,
399 struct batman_packet
*batman_packet
,
400 unsigned char *hna_buff
, int hna_buff_len
,
401 struct batman_if
*if_incoming
)
403 struct batman_if
*batman_if
;
404 struct orig_node
*orig_neigh_node
, *orig_node
;
405 char has_directlink_flag
;
406 char is_my_addr
= 0, is_my_orig
= 0, is_my_oldorig
= 0;
407 char is_broadcast
= 0, is_bidirectional
, is_single_hop_neigh
;
409 unsigned short if_incoming_seqno
;
411 /* Silently drop when the batman packet is actually not a
414 * This might happen if a packet is padded (e.g. Ethernet has a
415 * minimum frame length of 64 byte) and the aggregation interprets
416 * it as an additional length.
418 * TODO: A more sane solution would be to have a bit in the
419 * batman_packet to detect whether the packet is the last
420 * packet in an aggregation. Here we expect that the padding
421 * is always zero (or not 0x01)
423 if (batman_packet
->packet_type
!= BAT_PACKET
)
426 /* could be changed by schedule_own_packet() */
427 if_incoming_seqno
= atomic_read(&if_incoming
->seqno
);
429 has_directlink_flag
= (batman_packet
->flags
& DIRECTLINK
? 1 : 0);
431 is_single_hop_neigh
= (compare_orig(ethhdr
->h_source
,
432 batman_packet
->orig
) ? 1 : 0);
434 bat_dbg(DBG_BATMAN
, "Received BATMAN packet via NB: %pM, IF: %s [%s] "
435 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
436 "TTL %d, V %d, IDF %d)\n",
437 ethhdr
->h_source
, if_incoming
->dev
, if_incoming
->addr_str
,
438 batman_packet
->orig
, batman_packet
->prev_sender
,
439 batman_packet
->seqno
, batman_packet
->tq
, batman_packet
->ttl
,
440 batman_packet
->version
, has_directlink_flag
);
442 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
443 if (batman_if
->if_status
!= IF_ACTIVE
)
446 if (compare_orig(ethhdr
->h_source
,
447 batman_if
->net_dev
->dev_addr
))
450 if (compare_orig(batman_packet
->orig
,
451 batman_if
->net_dev
->dev_addr
))
454 if (compare_orig(batman_packet
->prev_sender
,
455 batman_if
->net_dev
->dev_addr
))
458 if (compare_orig(ethhdr
->h_source
, broadcastAddr
))
462 if (batman_packet
->version
!= COMPAT_VERSION
) {
464 "Drop packet: incompatible batman version (%i)\n",
465 batman_packet
->version
);
471 "Drop packet: received my own broadcast (sender: %pM"
478 bat_dbg(DBG_BATMAN
, "Drop packet: "
479 "ignoring all packets with broadcast source addr (sender: %pM"
480 ")\n", ethhdr
->h_source
);
488 orig_neigh_node
= get_orig_node(ethhdr
->h_source
);
490 if (!orig_neigh_node
)
493 /* neighbor has to indicate direct link and it has to
494 * come via the corresponding interface */
495 /* if received seqno equals last send seqno save new
496 * seqno for bidirectional check */
497 if (has_directlink_flag
&&
498 compare_orig(if_incoming
->net_dev
->dev_addr
,
499 batman_packet
->orig
) &&
500 (batman_packet
->seqno
- if_incoming_seqno
+ 2 == 0)) {
501 offset
= if_incoming
->if_num
* NUM_WORDS
;
502 word
= &(orig_neigh_node
->bcast_own
[offset
]);
504 orig_neigh_node
->bcast_own_sum
[if_incoming
->if_num
] =
505 bit_packet_count(word
);
508 bat_dbg(DBG_BATMAN
, "Drop packet: "
509 "originator packet from myself (via neighbor)\n");
515 "Drop packet: ignoring all rebroadcast echos (sender: "
516 "%pM)\n", ethhdr
->h_source
);
520 orig_node
= get_orig_node(batman_packet
->orig
);
521 if (orig_node
== NULL
)
524 is_duplicate
= count_real_packets(ethhdr
, batman_packet
, if_incoming
);
526 if (is_duplicate
== -1) {
528 "Drop packet: packet within seqno protection time "
529 "(sender: %pM)\n", ethhdr
->h_source
);
533 if (batman_packet
->tq
== 0) {
535 "Drop packet: originator packet with tq equal 0\n");
539 /* avoid temporary routing loops */
540 if ((orig_node
->router
) &&
541 (orig_node
->router
->orig_node
->router
) &&
542 (compare_orig(orig_node
->router
->addr
,
543 batman_packet
->prev_sender
)) &&
544 !(compare_orig(batman_packet
->orig
, batman_packet
->prev_sender
)) &&
545 (compare_orig(orig_node
->router
->addr
,
546 orig_node
->router
->orig_node
->router
->addr
))) {
548 "Drop packet: ignoring all rebroadcast packets that "
549 "may make me loop (sender: %pM)\n", ethhdr
->h_source
);
553 /* if sender is a direct neighbor the sender mac equals
555 orig_neigh_node
= (is_single_hop_neigh
?
556 orig_node
: get_orig_node(ethhdr
->h_source
));
557 if (orig_neigh_node
== NULL
)
560 /* drop packet if sender is not a direct neighbor and if we
561 * don't route towards it */
562 if (!is_single_hop_neigh
&&
563 (orig_neigh_node
->router
== NULL
)) {
564 bat_dbg(DBG_BATMAN
, "Drop packet: OGM via unknown neighbor!\n");
568 is_bidirectional
= isBidirectionalNeigh(orig_node
, orig_neigh_node
,
569 batman_packet
, if_incoming
);
571 /* update ranking if it is not a duplicate or has the same
572 * seqno and similar ttl as the non-duplicate */
573 if (is_bidirectional
&&
575 ((orig_node
->last_real_seqno
== batman_packet
->seqno
) &&
576 (orig_node
->last_ttl
- 3 <= batman_packet
->ttl
))))
577 update_orig(orig_node
, ethhdr
, batman_packet
,
578 if_incoming
, hna_buff
, hna_buff_len
, is_duplicate
);
580 /* is single hop (direct) neighbor */
581 if (is_single_hop_neigh
) {
583 /* mark direct link on incoming interface */
584 schedule_forward_packet(orig_node
, ethhdr
, batman_packet
,
585 1, hna_buff_len
, if_incoming
);
587 bat_dbg(DBG_BATMAN
, "Forwarding packet: "
588 "rebroadcast neighbor packet with direct link flag\n");
592 /* multihop originator */
593 if (!is_bidirectional
) {
595 "Drop packet: not received via bidirectional link\n");
600 bat_dbg(DBG_BATMAN
, "Drop packet: duplicate packet received\n");
605 "Forwarding packet: rebroadcast originator packet\n");
606 schedule_forward_packet(orig_node
, ethhdr
, batman_packet
,
607 0, hna_buff_len
, if_incoming
);
610 int recv_bat_packet(struct sk_buff
*skb
,
611 struct batman_if
*batman_if
)
613 struct ethhdr
*ethhdr
;
615 struct sk_buff
*skb_old
;
617 /* drop packet if it has not necessary minimum size */
618 if (skb_headlen(skb
) < sizeof(struct batman_packet
))
621 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
623 /* packet with broadcast indication but unicast recipient */
624 if (!is_bcast(ethhdr
->h_dest
))
627 /* packet with broadcast sender address */
628 if (is_bcast(ethhdr
->h_source
))
631 /* TODO: we use headlen instead of "length", because
632 * only this data is paged in. */
634 /* create a copy of the skb, if needed, to modify it. */
635 if (!skb_clone_writable(skb
, skb_headlen(skb
))) {
637 skb
= skb_copy(skb
, GFP_ATOMIC
);
640 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
644 spin_lock_irqsave(&orig_hash_lock
, flags
);
645 receive_aggr_bat_packet(ethhdr
,
649 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
652 return NET_RX_SUCCESS
;
655 static int recv_my_icmp_packet(struct sk_buff
*skb
)
657 struct orig_node
*orig_node
;
658 struct icmp_packet
*icmp_packet
;
659 struct ethhdr
*ethhdr
;
660 struct sk_buff
*skb_old
;
661 struct batman_if
*batman_if
;
664 uint8_t dstaddr
[ETH_ALEN
];
666 icmp_packet
= (struct icmp_packet
*)skb
->data
;
667 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
669 /* add data to device queue */
670 if (icmp_packet
->msg_type
!= ECHO_REQUEST
) {
671 bat_socket_receive_packet(icmp_packet
);
675 /* answer echo request (ping) */
676 /* get routing information */
677 spin_lock_irqsave(&orig_hash_lock
, flags
);
678 orig_node
= ((struct orig_node
*)hash_find(orig_hash
,
682 if ((orig_node
!= NULL
) &&
683 (orig_node
->router
!= NULL
)) {
685 /* don't lock while sending the packets ... we therefore
686 * copy the required data before sending */
687 batman_if
= orig_node
->router
->if_incoming
;
688 memcpy(dstaddr
, orig_node
->router
->addr
, ETH_ALEN
);
689 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
691 /* create a copy of the skb, if needed, to modify it. */
693 if (!skb_clone_writable(skb
, sizeof(struct icmp_packet
))) {
695 skb
= skb_copy(skb
, GFP_ATOMIC
);
699 icmp_packet
= (struct icmp_packet
*)skb
->data
;
700 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
704 memcpy(icmp_packet
->dst
, icmp_packet
->orig
, ETH_ALEN
);
705 memcpy(icmp_packet
->orig
, ethhdr
->h_dest
, ETH_ALEN
);
706 icmp_packet
->msg_type
= ECHO_REPLY
;
707 icmp_packet
->ttl
= TTL
;
709 send_skb_packet(skb
, batman_if
, dstaddr
);
710 ret
= NET_RX_SUCCESS
;
713 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
718 static int recv_icmp_ttl_exceeded(struct sk_buff
*skb
)
720 struct orig_node
*orig_node
;
721 struct icmp_packet
*icmp_packet
;
722 struct ethhdr
*ethhdr
;
723 struct sk_buff
*skb_old
;
724 struct batman_if
*batman_if
;
727 uint8_t dstaddr
[ETH_ALEN
];
729 icmp_packet
= (struct icmp_packet
*)skb
->data
;
730 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
732 /* send TTL exceeded if packet is an echo request (traceroute) */
733 if (icmp_packet
->msg_type
!= ECHO_REQUEST
) {
734 printk(KERN_WARNING
"batman-adv:"
735 "Warning - can't forward icmp packet from %pM to %pM: "
737 icmp_packet
->orig
, icmp_packet
->dst
);
741 /* get routing information */
742 spin_lock_irqsave(&orig_hash_lock
, flags
);
743 orig_node
= ((struct orig_node
*)
744 hash_find(orig_hash
, icmp_packet
->orig
));
747 if ((orig_node
!= NULL
) &&
748 (orig_node
->router
!= NULL
)) {
750 /* don't lock while sending the packets ... we therefore
751 * copy the required data before sending */
752 batman_if
= orig_node
->router
->if_incoming
;
753 memcpy(dstaddr
, orig_node
->router
->addr
, ETH_ALEN
);
754 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
756 /* create a copy of the skb, if needed, to modify it. */
757 if (!skb_clone_writable(skb
, sizeof(struct icmp_packet
))) {
759 skb
= skb_copy(skb
, GFP_ATOMIC
);
762 icmp_packet
= (struct icmp_packet
*) skb
->data
;
763 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
767 memcpy(icmp_packet
->dst
, icmp_packet
->orig
, ETH_ALEN
);
768 memcpy(icmp_packet
->orig
, ethhdr
->h_dest
, ETH_ALEN
);
769 icmp_packet
->msg_type
= TTL_EXCEEDED
;
770 icmp_packet
->ttl
= TTL
;
772 send_skb_packet(skb
, batman_if
, dstaddr
);
773 ret
= NET_RX_SUCCESS
;
776 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
782 int recv_icmp_packet(struct sk_buff
*skb
)
784 struct icmp_packet
*icmp_packet
;
785 struct ethhdr
*ethhdr
;
786 struct orig_node
*orig_node
;
787 struct sk_buff
*skb_old
;
788 struct batman_if
*batman_if
;
789 int hdr_size
= sizeof(struct icmp_packet
);
792 uint8_t dstaddr
[ETH_ALEN
];
794 /* drop packet if it has not necessary minimum size */
795 if (skb_headlen(skb
) < hdr_size
)
798 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
800 /* packet with unicast indication but broadcast recipient */
801 if (is_bcast(ethhdr
->h_dest
))
804 /* packet with broadcast sender address */
805 if (is_bcast(ethhdr
->h_source
))
809 if (!is_my_mac(ethhdr
->h_dest
))
812 icmp_packet
= (struct icmp_packet
*)skb
->data
;
815 if (is_my_mac(icmp_packet
->dst
))
816 return recv_my_icmp_packet(skb
);
819 if (icmp_packet
->ttl
< 2)
820 return recv_icmp_ttl_exceeded(skb
);
824 /* get routing information */
825 spin_lock_irqsave(&orig_hash_lock
, flags
);
826 orig_node
= ((struct orig_node
*)
827 hash_find(orig_hash
, icmp_packet
->dst
));
829 if ((orig_node
!= NULL
) &&
830 (orig_node
->router
!= NULL
)) {
832 /* don't lock while sending the packets ... we therefore
833 * copy the required data before sending */
834 batman_if
= orig_node
->router
->if_incoming
;
835 memcpy(dstaddr
, orig_node
->router
->addr
, ETH_ALEN
);
836 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
838 /* create a copy of the skb, if needed, to modify it. */
839 if (!skb_clone_writable(skb
, sizeof(struct icmp_packet
))) {
841 skb
= skb_copy(skb
, GFP_ATOMIC
);
844 icmp_packet
= (struct icmp_packet
*)skb
->data
;
845 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
853 send_skb_packet(skb
, batman_if
, dstaddr
);
854 ret
= NET_RX_SUCCESS
;
857 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
862 int recv_unicast_packet(struct sk_buff
*skb
)
864 struct unicast_packet
*unicast_packet
;
865 struct orig_node
*orig_node
;
866 struct ethhdr
*ethhdr
;
867 struct batman_if
*batman_if
;
868 struct sk_buff
*skb_old
;
869 uint8_t dstaddr
[ETH_ALEN
];
870 int hdr_size
= sizeof(struct unicast_packet
);
874 /* drop packet if it has not necessary minimum size */
875 if (skb_headlen(skb
) < hdr_size
)
878 ethhdr
= (struct ethhdr
*) skb_mac_header(skb
);
880 /* packet with unicast indication but broadcast recipient */
881 if (is_bcast(ethhdr
->h_dest
))
884 /* packet with broadcast sender address */
885 if (is_bcast(ethhdr
->h_source
))
889 if (!is_my_mac(ethhdr
->h_dest
))
892 unicast_packet
= (struct unicast_packet
*) skb
->data
;
895 if (is_my_mac(unicast_packet
->dest
)) {
896 interface_rx(skb
, hdr_size
);
897 return NET_RX_SUCCESS
;
901 if (unicast_packet
->ttl
< 2) {
902 printk(KERN_WARNING
"batman-adv:Warning - "
903 "can't forward unicast packet from %pM to %pM: "
905 ethhdr
->h_source
, unicast_packet
->dest
);
910 /* get routing information */
911 spin_lock_irqsave(&orig_hash_lock
, flags
);
912 orig_node
= ((struct orig_node
*)
913 hash_find(orig_hash
, unicast_packet
->dest
));
915 if ((orig_node
!= NULL
) &&
916 (orig_node
->router
!= NULL
)) {
918 /* don't lock while sending the packets ... we therefore
919 * copy the required data before sending */
920 batman_if
= orig_node
->router
->if_incoming
;
921 memcpy(dstaddr
, orig_node
->router
->addr
, ETH_ALEN
);
922 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
924 /* create a copy of the skb, if needed, to modify it. */
925 if (!skb_clone_writable(skb
, sizeof(struct unicast_packet
))) {
927 skb
= skb_copy(skb
, GFP_ATOMIC
);
930 unicast_packet
= (struct unicast_packet
*)skb
->data
;
931 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
935 unicast_packet
->ttl
--;
938 send_skb_packet(skb
, batman_if
, dstaddr
);
939 ret
= NET_RX_SUCCESS
;
942 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
947 int recv_bcast_packet(struct sk_buff
*skb
)
949 struct orig_node
*orig_node
;
950 struct bcast_packet
*bcast_packet
;
951 struct ethhdr
*ethhdr
;
952 int hdr_size
= sizeof(struct bcast_packet
);
956 /* drop packet if it has not necessary minimum size */
957 if (skb_headlen(skb
) < hdr_size
)
960 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
962 /* packet with broadcast indication but unicast recipient */
963 if (!is_bcast(ethhdr
->h_dest
))
966 /* packet with broadcast sender address */
967 if (is_bcast(ethhdr
->h_source
))
970 /* ignore broadcasts sent by myself */
971 if (is_my_mac(ethhdr
->h_source
))
974 bcast_packet
= (struct bcast_packet
*)skb
->data
;
976 /* ignore broadcasts originated by myself */
977 if (is_my_mac(bcast_packet
->orig
))
980 spin_lock_irqsave(&orig_hash_lock
, flags
);
981 orig_node
= ((struct orig_node
*)
982 hash_find(orig_hash
, bcast_packet
->orig
));
984 if (orig_node
== NULL
) {
985 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
989 /* check whether the packet is a duplicate */
990 if (get_bit_status(orig_node
->bcast_bits
,
991 orig_node
->last_bcast_seqno
,
992 ntohs(bcast_packet
->seqno
))) {
993 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
997 seq_diff
= ntohs(bcast_packet
->seqno
) - orig_node
->last_bcast_seqno
;
999 /* check whether the packet is old and the host just restarted. */
1000 if (window_protected(seq_diff
, &orig_node
->bcast_seqno_reset
)) {
1001 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
1005 /* mark broadcast in flood history, update window position
1007 if (bit_get_packet(orig_node
->bcast_bits
, seq_diff
, 1))
1008 orig_node
->last_bcast_seqno
= ntohs(bcast_packet
->seqno
);
1010 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
1011 /* rebroadcast packet */
1012 add_bcast_packet_to_list(skb
);
1014 /* broadcast for me */
1015 interface_rx(skb
, hdr_size
);
1017 return NET_RX_SUCCESS
;
1020 int recv_vis_packet(struct sk_buff
*skb
)
1022 struct vis_packet
*vis_packet
;
1023 struct ethhdr
*ethhdr
;
1024 struct bat_priv
*bat_priv
;
1025 int hdr_size
= sizeof(struct vis_packet
);
1027 if (skb_headlen(skb
) < hdr_size
)
1030 vis_packet
= (struct vis_packet
*) skb
->data
;
1031 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1034 if (!is_my_mac(ethhdr
->h_dest
))
1037 /* ignore own packets */
1038 if (is_my_mac(vis_packet
->vis_orig
))
1041 if (is_my_mac(vis_packet
->sender_orig
))
1044 /* FIXME: each batman_if will be attached to a softif */
1045 bat_priv
= netdev_priv(soft_device
);
1047 switch (vis_packet
->vis_type
) {
1048 case VIS_TYPE_SERVER_SYNC
:
1049 /* TODO: handle fragmented skbs properly */
1050 receive_server_sync_packet(bat_priv
, vis_packet
,
1054 case VIS_TYPE_CLIENT_UPDATE
:
1055 /* TODO: handle fragmented skbs properly */
1056 receive_client_update_packet(bat_priv
, vis_packet
,
1060 default: /* ignore unknown packet */
1064 /* We take a copy of the data in the packet, so we should
1065 always free the skbuf. */