blob: 0888f1e39fdcc5a16242f59a52240839b36810b7 [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "routing.h"
22#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000023#include "soft-interface.h"
24#include "hard-interface.h"
25#include "icmp_socket.h"
26#include "translation-table.h"
27#include "originator.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000028#include "vis.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029#include "unicast.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010030#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +010032static int route_unicast_packet(struct sk_buff *skb,
33 struct hard_iface *recv_if);
34
Sven Eckelmann30d3c512012-05-12 02:09:36 +020035void batadv_slide_own_bcast_window(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036{
Marek Lindnere6c10f42011-02-18 12:33:20 +000037 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000039 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000040 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041 struct orig_node *orig_node;
42 unsigned long *word;
Antonio Quartullic90681b2011-10-05 17:05:25 +020043 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044 size_t word_index;
45
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046 for (i = 0; i < hash->size; i++) {
47 head = &hash->table[i];
48
Marek Lindnerfb778ea2011-01-19 20:01:40 +000049 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +000050 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +000051 spin_lock_bh(&orig_node->ogm_cnt_lock);
Marek Lindnere6c10f42011-02-18 12:33:20 +000052 word_index = hard_iface->if_num * NUM_WORDS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053 word = &(orig_node->bcast_own[word_index]);
54
Sven Eckelmann0f5f9322012-05-12 02:09:25 +020055 batadv_bit_get_packet(bat_priv, word, 1, 0);
Marek Lindnere6c10f42011-02-18 12:33:20 +000056 orig_node->bcast_own_sum[hard_iface->if_num] =
Sven Eckelmann0079d2c2012-02-04 17:34:52 +010057 bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
Marek Lindner2ae2daf2011-01-19 20:01:42 +000058 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +000060 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062}
63
Marek Lindnerfc957272011-07-30 12:04:12 +020064static void _update_route(struct bat_priv *bat_priv,
65 struct orig_node *orig_node,
66 struct neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000067{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000068 struct neigh_node *curr_router;
69
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020070 curr_router = batadv_orig_node_get_router(orig_node);
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000071
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000072 /* route deleted */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000073 if ((curr_router) && (!neigh_node)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000074 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
75 orig_node->orig);
Sven Eckelmann08c36d32012-05-12 02:09:39 +020076 batadv_tt_global_del_orig(bat_priv, orig_node,
77 "Deleted route towards originator");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000078
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000079 /* route added */
80 } else if ((!curr_router) && (neigh_node)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081
82 bat_dbg(DBG_ROUTES, bat_priv,
83 "Adding route towards: %pM (via %pM)\n",
84 orig_node->orig, neigh_node->addr);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000085 /* route changed */
Sven Eckelmannbb899b82011-05-10 11:22:37 +020086 } else if (neigh_node && curr_router) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000087 bat_dbg(DBG_ROUTES, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +010088 "Changing route towards: %pM (now via %pM - was via %pM)\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000089 orig_node->orig, neigh_node->addr,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000090 curr_router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000091 }
92
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000093 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020094 batadv_neigh_node_free_ref(curr_router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000095
96 /* increase refcount of new best neighbor */
Marek Lindner44524fc2011-02-10 14:33:53 +000097 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
98 neigh_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000099
100 spin_lock_bh(&orig_node->neigh_list_lock);
101 rcu_assign_pointer(orig_node->router, neigh_node);
102 spin_unlock_bh(&orig_node->neigh_list_lock);
103
104 /* decrease refcount of previous best neighbor */
105 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200106 batadv_neigh_node_free_ref(curr_router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000107}
108
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200109void batadv_update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
110 struct neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000112 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113
114 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000115 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200117 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000118
119 if (router != neigh_node)
Marek Lindnerfc957272011-07-30 12:04:12 +0200120 _update_route(bat_priv, orig_node, neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000121
122out:
123 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200124 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125}
126
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000127/* caller must hold the neigh_list_lock */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200128void batadv_bonding_candidate_del(struct orig_node *orig_node,
129 struct neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000130{
131 /* this neighbor is not part of our candidate list */
132 if (list_empty(&neigh_node->bonding_list))
133 goto out;
134
135 list_del_rcu(&neigh_node->bonding_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000136 INIT_LIST_HEAD(&neigh_node->bonding_list);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200137 batadv_neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000138 atomic_dec(&orig_node->bond_candidates);
139
140out:
141 return;
142}
143
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200144void batadv_bonding_candidate_add(struct orig_node *orig_node,
145 struct neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000146{
147 struct hlist_node *node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000148 struct neigh_node *tmp_neigh_node, *router = NULL;
149 uint8_t interference_candidate = 0;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000150
151 spin_lock_bh(&orig_node->neigh_list_lock);
152
153 /* only consider if it has the same primary address ... */
Marek Lindner39901e72011-02-18 12:28:08 +0000154 if (!compare_eth(orig_node->orig,
155 neigh_node->orig_node->primary_addr))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000156 goto candidate_del;
157
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200158 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000159 if (!router)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000160 goto candidate_del;
161
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000162 /* ... and is good enough to be considered */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000163 if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000164 goto candidate_del;
165
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200166 /* check if we have another candidate with the same mac address or
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000167 * interface. If we do, we won't select this candidate because of
168 * possible interference.
169 */
170 hlist_for_each_entry_rcu(tmp_neigh_node, node,
171 &orig_node->neigh_list, list) {
172
173 if (tmp_neigh_node == neigh_node)
174 continue;
175
176 /* we only care if the other candidate is even
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200177 * considered as candidate.
178 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000179 if (list_empty(&tmp_neigh_node->bonding_list))
180 continue;
181
182 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
Marek Lindner39901e72011-02-18 12:28:08 +0000183 (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000184 interference_candidate = 1;
185 break;
186 }
187 }
188
189 /* don't care further if it is an interference candidate */
190 if (interference_candidate)
191 goto candidate_del;
192
193 /* this neighbor already is part of our candidate list */
194 if (!list_empty(&neigh_node->bonding_list))
195 goto out;
196
Marek Lindner44524fc2011-02-10 14:33:53 +0000197 if (!atomic_inc_not_zero(&neigh_node->refcount))
198 goto out;
199
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000200 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000201 atomic_inc(&orig_node->bond_candidates);
202 goto out;
203
204candidate_del:
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200205 batadv_bonding_candidate_del(orig_node, neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000206
207out:
208 spin_unlock_bh(&orig_node->neigh_list_lock);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000209
210 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200211 batadv_neigh_node_free_ref(router);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000212}
213
214/* copy primary address for bonding */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200215void
216batadv_bonding_save_primary(const struct orig_node *orig_node,
217 struct orig_node *orig_neigh_node,
218 const struct batman_ogm_packet *batman_ogm_packet)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000219{
Marek Lindnerb6da4bf2011-07-29 17:31:50 +0200220 if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000221 return;
222
223 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
224}
225
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226/* checks whether the host restarted and is in the protection time.
227 * returns:
228 * 0 if the packet is to be accepted
229 * 1 if the packet is to be ignored.
230 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200231int batadv_window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
232 unsigned long *last_reset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233{
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100234 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE) ||
235 (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
Marek Lindner8c7bf242012-03-17 15:28:33 +0800236 if (!has_timed_out(*last_reset, RESET_PROTECTION_MS))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237 return 1;
Marek Lindner8c7bf242012-03-17 15:28:33 +0800238
239 *last_reset = jiffies;
240 bat_dbg(DBG_BATMAN, bat_priv,
241 "old packet received, start protection\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000242 }
Marek Lindner8c7bf242012-03-17 15:28:33 +0800243
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244 return 0;
245}
246
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200247bool batadv_check_management_packet(struct sk_buff *skb,
248 struct hard_iface *hard_iface,
249 int header_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251 struct ethhdr *ethhdr;
252
253 /* drop packet if it has not necessary minimum size */
Marek Lindnerc3e29312012-03-04 16:56:25 +0800254 if (unlikely(!pskb_may_pull(skb, header_len)))
255 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256
257 ethhdr = (struct ethhdr *)skb_mac_header(skb);
258
259 /* packet with broadcast indication but unicast recipient */
260 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800261 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262
263 /* packet with broadcast sender address */
264 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800265 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266
267 /* create a copy of the skb, if needed, to modify it. */
268 if (skb_cow(skb, 0) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800269 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270
271 /* keep skb linear */
272 if (skb_linearize(skb) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800273 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274
Marek Lindnerc3e29312012-03-04 16:56:25 +0800275 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276}
277
278static int recv_my_icmp_packet(struct bat_priv *bat_priv,
279 struct sk_buff *skb, size_t icmp_len)
280{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200281 struct hard_iface *primary_if = NULL;
Marek Lindner44524fc2011-02-10 14:33:53 +0000282 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000283 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000284 struct icmp_packet_rr *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000285 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000286
287 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288
289 /* add data to device queue */
290 if (icmp_packet->msg_type != ECHO_REQUEST) {
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200291 batadv_socket_receive_packet(icmp_packet, icmp_len);
Marek Lindner44524fc2011-02-10 14:33:53 +0000292 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293 }
294
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200295 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200296 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000297 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298
299 /* answer echo request (ping) */
300 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200301 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000302 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000303 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000304
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200305 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000306 if (!router)
307 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000308
Marek Lindner44524fc2011-02-10 14:33:53 +0000309 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100310 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000311 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312
Marek Lindner44524fc2011-02-10 14:33:53 +0000313 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314
Marek Lindner44524fc2011-02-10 14:33:53 +0000315 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200316 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Marek Lindner44524fc2011-02-10 14:33:53 +0000317 icmp_packet->msg_type = ECHO_REPLY;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100318 icmp_packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319
Sven Eckelmann9455e342012-05-12 02:09:37 +0200320 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000321 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000322
Marek Lindner44524fc2011-02-10 14:33:53 +0000323out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200324 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200325 batadv_hardif_free_ref(primary_if);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000326 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200327 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000328 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200329 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330 return ret;
331}
332
333static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
Simon Wunderlich74ef1152010-12-29 16:15:19 +0000334 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200336 struct hard_iface *primary_if = NULL;
Marek Lindner44524fc2011-02-10 14:33:53 +0000337 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000338 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339 struct icmp_packet *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000340 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341
342 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343
344 /* send TTL exceeded if packet is an echo request (traceroute) */
345 if (icmp_packet->msg_type != ECHO_REQUEST) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100346 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
347 icmp_packet->orig, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000348 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349 }
350
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200351 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200352 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000353 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354
355 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200356 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000357 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000358 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000359
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200360 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000361 if (!router)
362 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363
Marek Lindner44524fc2011-02-10 14:33:53 +0000364 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100365 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000366 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367
Marek Lindner44524fc2011-02-10 14:33:53 +0000368 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369
Marek Lindner44524fc2011-02-10 14:33:53 +0000370 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200371 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Marek Lindner44524fc2011-02-10 14:33:53 +0000372 icmp_packet->msg_type = TTL_EXCEEDED;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100373 icmp_packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374
Sven Eckelmann9455e342012-05-12 02:09:37 +0200375 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000376 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
Marek Lindner44524fc2011-02-10 14:33:53 +0000378out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200379 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200380 batadv_hardif_free_ref(primary_if);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000381 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200382 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000383 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200384 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385 return ret;
386}
387
388
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200389int batadv_recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390{
391 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
392 struct icmp_packet_rr *icmp_packet;
393 struct ethhdr *ethhdr;
Marek Lindner44524fc2011-02-10 14:33:53 +0000394 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000395 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396 int hdr_size = sizeof(struct icmp_packet);
Marek Lindner44524fc2011-02-10 14:33:53 +0000397 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200399 /* we truncate all incoming icmp packets if they don't match our size */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400 if (skb->len >= sizeof(struct icmp_packet_rr))
401 hdr_size = sizeof(struct icmp_packet_rr);
402
403 /* drop packet if it has not necessary minimum size */
404 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindner44524fc2011-02-10 14:33:53 +0000405 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406
407 ethhdr = (struct ethhdr *)skb_mac_header(skb);
408
409 /* packet with unicast indication but broadcast recipient */
410 if (is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000411 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000412
413 /* packet with broadcast sender address */
414 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindner44524fc2011-02-10 14:33:53 +0000415 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000416
417 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200418 if (!batadv_is_my_mac(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000419 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420
421 icmp_packet = (struct icmp_packet_rr *)skb->data;
422
423 /* add record route information if not full */
424 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
425 (icmp_packet->rr_cur < BAT_RR_LEN)) {
426 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100427 ethhdr->h_dest, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428 icmp_packet->rr_cur++;
429 }
430
431 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200432 if (batadv_is_my_mac(icmp_packet->dst))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
434
435 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100436 if (icmp_packet->header.ttl < 2)
Simon Wunderlich74ef1152010-12-29 16:15:19 +0000437 return recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200440 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000441 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000442 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000443
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200444 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000445 if (!router)
446 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447
Marek Lindner44524fc2011-02-10 14:33:53 +0000448 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100449 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000450 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000451
Marek Lindner44524fc2011-02-10 14:33:53 +0000452 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453
Marek Lindner44524fc2011-02-10 14:33:53 +0000454 /* decrement ttl */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100455 icmp_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456
Marek Lindner44524fc2011-02-10 14:33:53 +0000457 /* route it */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200458 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000459 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460
Marek Lindner44524fc2011-02-10 14:33:53 +0000461out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000462 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200463 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000464 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200465 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000466 return ret;
467}
468
Linus Lüssing55158622011-03-14 22:43:27 +0000469/* In the bonding case, send the packets in a round
470 * robin fashion over the remaining interfaces.
471 *
472 * This method rotates the bonding list and increases the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200473 * returned router's refcount.
474 */
Linus Lüssing55158622011-03-14 22:43:27 +0000475static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200476 const struct hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000477{
478 struct neigh_node *tmp_neigh_node;
479 struct neigh_node *router = NULL, *first_candidate = NULL;
480
481 rcu_read_lock();
482 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
483 bonding_list) {
484 if (!first_candidate)
485 first_candidate = tmp_neigh_node;
486
487 /* recv_if == NULL on the first node. */
488 if (tmp_neigh_node->if_incoming == recv_if)
489 continue;
490
491 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
492 continue;
493
494 router = tmp_neigh_node;
495 break;
496 }
497
498 /* use the first candidate if nothing was found. */
499 if (!router && first_candidate &&
500 atomic_inc_not_zero(&first_candidate->refcount))
501 router = first_candidate;
502
503 if (!router)
504 goto out;
505
506 /* selected should point to the next element
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200507 * after the current router
508 */
Linus Lüssing55158622011-03-14 22:43:27 +0000509 spin_lock_bh(&primary_orig->neigh_list_lock);
510 /* this is a list_move(), which unfortunately
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200511 * does not exist as rcu version
512 */
Linus Lüssing55158622011-03-14 22:43:27 +0000513 list_del_rcu(&primary_orig->bond_list);
514 list_add_rcu(&primary_orig->bond_list,
515 &router->bonding_list);
516 spin_unlock_bh(&primary_orig->neigh_list_lock);
517
518out:
519 rcu_read_unlock();
520 return router;
521}
522
523/* Interface Alternating: Use the best of the
524 * remaining candidates which are not using
525 * this interface.
526 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200527 * Increases the returned router's refcount
528 */
Linus Lüssing55158622011-03-14 22:43:27 +0000529static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200530 const struct hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000531{
532 struct neigh_node *tmp_neigh_node;
533 struct neigh_node *router = NULL, *first_candidate = NULL;
534
535 rcu_read_lock();
536 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
537 bonding_list) {
538 if (!first_candidate)
539 first_candidate = tmp_neigh_node;
540
541 /* recv_if == NULL on the first node. */
542 if (tmp_neigh_node->if_incoming == recv_if)
543 continue;
544
545 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
546 continue;
547
548 /* if we don't have a router yet
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200549 * or this one is better, choose it.
550 */
Linus Lüssing55158622011-03-14 22:43:27 +0000551 if ((!router) ||
552 (tmp_neigh_node->tq_avg > router->tq_avg)) {
553 /* decrement refcount of
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200554 * previously selected router
555 */
Linus Lüssing55158622011-03-14 22:43:27 +0000556 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200557 batadv_neigh_node_free_ref(router);
Linus Lüssing55158622011-03-14 22:43:27 +0000558
559 router = tmp_neigh_node;
560 atomic_inc_not_zero(&router->refcount);
561 }
562
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200563 batadv_neigh_node_free_ref(tmp_neigh_node);
Linus Lüssing55158622011-03-14 22:43:27 +0000564 }
565
566 /* use the first candidate if nothing was found. */
567 if (!router && first_candidate &&
568 atomic_inc_not_zero(&first_candidate->refcount))
569 router = first_candidate;
570
571 rcu_read_unlock();
572 return router;
573}
574
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200575int batadv_recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200576{
577 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
578 struct tt_query_packet *tt_query;
Al Virof25bd582012-04-22 07:44:27 +0100579 uint16_t tt_size;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200580 struct ethhdr *ethhdr;
581
582 /* drop packet if it has not necessary minimum size */
583 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
584 goto out;
585
586 /* I could need to modify it */
587 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
588 goto out;
589
590 ethhdr = (struct ethhdr *)skb_mac_header(skb);
591
592 /* packet with unicast indication but broadcast recipient */
593 if (is_broadcast_ether_addr(ethhdr->h_dest))
594 goto out;
595
596 /* packet with broadcast sender address */
597 if (is_broadcast_ether_addr(ethhdr->h_source))
598 goto out;
599
600 tt_query = (struct tt_query_packet *)skb->data;
601
Antonio Quartullia73105b2011-04-27 14:27:44 +0200602 switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
603 case TT_REQUEST:
Martin Hundebøllf8214862012-04-20 17:02:45 +0200604 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_RX);
605
Antonio Quartullia73105b2011-04-27 14:27:44 +0200606 /* If we cannot provide an answer the tt_request is
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200607 * forwarded
608 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200609 if (!batadv_send_tt_response(bat_priv, tt_query)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200610 bat_dbg(DBG_TT, bat_priv,
611 "Routing TT_REQUEST to %pM [%c]\n",
612 tt_query->dst,
613 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +0200614 return route_unicast_packet(skb, recv_if);
615 }
616 break;
617 case TT_RESPONSE:
Martin Hundebøllf8214862012-04-20 17:02:45 +0200618 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_RX);
619
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200620 if (batadv_is_my_mac(tt_query->dst)) {
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200621 /* packet needs to be linearized to access the TT
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200622 * changes
623 */
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200624 if (skb_linearize(skb) < 0)
625 goto out;
Antonio Quartullid2b6cc82012-06-14 22:21:28 +0200626 /* skb_linearize() possibly changed skb->data */
627 tt_query = (struct tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200628
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200629 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200630
631 /* Ensure we have all the claimed data */
632 if (unlikely(skb_headlen(skb) <
Al Virof25bd582012-04-22 07:44:27 +0100633 sizeof(struct tt_query_packet) + tt_size))
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200634 goto out;
635
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200636 batadv_handle_tt_response(bat_priv, tt_query);
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200637 } else {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200638 bat_dbg(DBG_TT, bat_priv,
639 "Routing TT_RESPONSE to %pM [%c]\n",
640 tt_query->dst,
641 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +0200642 return route_unicast_packet(skb, recv_if);
643 }
644 break;
645 }
646
647out:
648 /* returning NET_RX_DROP will make the caller function kfree the skb */
649 return NET_RX_DROP;
650}
651
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200652int batadv_recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200653{
654 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
655 struct roam_adv_packet *roam_adv_packet;
656 struct orig_node *orig_node;
657 struct ethhdr *ethhdr;
658
659 /* drop packet if it has not necessary minimum size */
660 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
661 goto out;
662
663 ethhdr = (struct ethhdr *)skb_mac_header(skb);
664
665 /* packet with unicast indication but broadcast recipient */
666 if (is_broadcast_ether_addr(ethhdr->h_dest))
667 goto out;
668
669 /* packet with broadcast sender address */
670 if (is_broadcast_ether_addr(ethhdr->h_source))
671 goto out;
672
Martin Hundebøllf8214862012-04-20 17:02:45 +0200673 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_RX);
674
Antonio Quartullicc47f662011-04-27 14:27:57 +0200675 roam_adv_packet = (struct roam_adv_packet *)skb->data;
676
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200677 if (!batadv_is_my_mac(roam_adv_packet->dst))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200678 return route_unicast_packet(skb, recv_if);
679
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100680 /* check if it is a backbone gateway. we don't accept
681 * roaming advertisement from it, as it has the same
682 * entries as we have.
683 */
Sven Eckelmann08adf152012-05-12 13:38:47 +0200684 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100685 goto out;
686
Sven Eckelmannda641192012-05-12 13:48:56 +0200687 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200688 if (!orig_node)
689 goto out;
690
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100691 bat_dbg(DBG_TT, bat_priv,
692 "Received ROAMING_ADV from %pM (client %pM)\n",
693 roam_adv_packet->src, roam_adv_packet->client);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200694
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200695 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
696 atomic_read(&orig_node->last_ttvn) + 1, true,
697 false);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200698
699 /* Roaming phase starts: I have new information but the ttvn has not
700 * been incremented yet. This flag will make me check all the incoming
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200701 * packets for the correct destination.
702 */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200703 bat_priv->tt_poss_change = true;
704
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200705 batadv_orig_node_free_ref(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200706out:
707 /* returning NET_RX_DROP will make the caller function kfree the skb */
708 return NET_RX_DROP;
709}
710
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000711/* find a suitable router for this originator, and use
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000712 * bonding if possible. increases the found neighbors
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200713 * refcount.
714 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200715struct neigh_node *batadv_find_router(struct bat_priv *bat_priv,
716 struct orig_node *orig_node,
717 const struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718{
719 struct orig_node *primary_orig_node;
720 struct orig_node *router_orig;
Linus Lüssing55158622011-03-14 22:43:27 +0000721 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000722 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
723 int bonding_enabled;
Sven Eckelmannda641192012-05-12 13:48:56 +0200724 uint8_t *primary_addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725
726 if (!orig_node)
727 return NULL;
728
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200729 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000730 if (!router)
Marek Lindner01df2b62011-05-05 14:14:46 +0200731 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000732
733 /* without bonding, the first node should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200734 * always choose the default router.
735 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000736 bonding_enabled = atomic_read(&bat_priv->bonding);
737
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000738 rcu_read_lock();
739 /* select default router to output */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000740 router_orig = router->orig_node;
Marek Lindner01df2b62011-05-05 14:14:46 +0200741 if (!router_orig)
742 goto err_unlock;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000743
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000744 if ((!recv_if) && (!bonding_enabled))
745 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000746
Sven Eckelmannda641192012-05-12 13:48:56 +0200747 primary_addr = router_orig->primary_addr;
748
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000749 /* if we have something in the primary_addr, we can search
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200750 * for a potential bonding candidate.
751 */
Sven Eckelmannda641192012-05-12 13:48:56 +0200752 if (compare_eth(primary_addr, zero_mac))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000753 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000754
755 /* find the orig_node which has the primary interface. might
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200756 * even be the same as our router_orig in many cases
757 */
Sven Eckelmannda641192012-05-12 13:48:56 +0200758 if (compare_eth(primary_addr, router_orig->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000759 primary_orig_node = router_orig;
760 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200761 primary_orig_node = batadv_orig_hash_find(bat_priv,
762 primary_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000763 if (!primary_orig_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000764 goto return_router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000765
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200766 batadv_orig_node_free_ref(primary_orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767 }
768
769 /* with less than 2 candidates, we can't do any
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200770 * bonding and prefer the original router.
771 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000772 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
773 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000774
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000775 /* all nodes between should choose a candidate which
776 * is is not on the interface where the packet came
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200777 * in.
778 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200779 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780
Linus Lüssing55158622011-03-14 22:43:27 +0000781 if (bonding_enabled)
782 router = find_bond_router(primary_orig_node, recv_if);
783 else
784 router = find_ifalter_router(primary_orig_node, recv_if);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000785
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000786return_router:
Antonio Quartullie2cbc112011-05-08 20:52:57 +0200787 if (router && router->if_incoming->if_status != IF_ACTIVE)
788 goto err_unlock;
789
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000790 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000791 return router;
Marek Lindner01df2b62011-05-05 14:14:46 +0200792err_unlock:
793 rcu_read_unlock();
794err:
795 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200796 batadv_neigh_node_free_ref(router);
Marek Lindner01df2b62011-05-05 14:14:46 +0200797 return NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000798}
799
800static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
801{
802 struct ethhdr *ethhdr;
803
804 /* drop packet if it has not necessary minimum size */
805 if (unlikely(!pskb_may_pull(skb, hdr_size)))
806 return -1;
807
808 ethhdr = (struct ethhdr *)skb_mac_header(skb);
809
810 /* packet with unicast indication but broadcast recipient */
811 if (is_broadcast_ether_addr(ethhdr->h_dest))
812 return -1;
813
814 /* packet with broadcast sender address */
815 if (is_broadcast_ether_addr(ethhdr->h_source))
816 return -1;
817
818 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200819 if (!batadv_is_my_mac(ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000820 return -1;
821
822 return 0;
823}
824
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +0100825static int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000826{
827 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindner44524fc2011-02-10 14:33:53 +0000828 struct orig_node *orig_node = NULL;
829 struct neigh_node *neigh_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000830 struct unicast_packet *unicast_packet;
831 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
Marek Lindner44524fc2011-02-10 14:33:53 +0000832 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000833 struct sk_buff *new_skb;
834
835 unicast_packet = (struct unicast_packet *)skb->data;
836
837 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100838 if (unicast_packet->header.ttl < 2) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100839 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
840 ethhdr->h_source, unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +0000841 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000842 }
843
844 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200845 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
Marek Lindner7aadf882011-02-18 12:28:09 +0000846
Marek Lindner44524fc2011-02-10 14:33:53 +0000847 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +0200848 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000849
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000850 /* find_router() increases neigh_nodes refcount if found. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200851 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000852
Marek Lindnerd0072602011-01-19 20:01:44 +0000853 if (!neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000854 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000855
856 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100857 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000858 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000859
860 unicast_packet = (struct unicast_packet *)skb->data;
861
Sven Eckelmann76543d12011-11-20 15:47:38 +0100862 if (unicast_packet->header.packet_type == BAT_UNICAST &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863 atomic_read(&bat_priv->fragmentation) &&
Marek Lindnerd0072602011-01-19 20:01:44 +0000864 skb->len > neigh_node->if_incoming->net_dev->mtu) {
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200865 ret = batadv_frag_send_skb(skb, bat_priv,
866 neigh_node->if_incoming,
867 neigh_node->addr);
Marek Lindnerd0072602011-01-19 20:01:44 +0000868 goto out;
869 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000870
Sven Eckelmann76543d12011-11-20 15:47:38 +0100871 if (unicast_packet->header.packet_type == BAT_UNICAST_FRAG &&
Marek Lindnerd0072602011-01-19 20:01:44 +0000872 frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000873
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200874 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000875
876 if (ret == NET_RX_DROP)
Marek Lindner44524fc2011-02-10 14:33:53 +0000877 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000878
879 /* packet was buffered for late merge */
Marek Lindner44524fc2011-02-10 14:33:53 +0000880 if (!new_skb) {
881 ret = NET_RX_SUCCESS;
882 goto out;
883 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000884
885 skb = new_skb;
886 unicast_packet = (struct unicast_packet *)skb->data;
887 }
888
889 /* decrement ttl */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100890 unicast_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000891
Martin Hundebøllf8214862012-04-20 17:02:45 +0200892 /* Update stats counter */
893 batadv_inc_counter(bat_priv, BAT_CNT_FORWARD);
894 batadv_add_counter(bat_priv, BAT_CNT_FORWARD_BYTES,
895 skb->len + ETH_HLEN);
896
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000897 /* route it */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200898 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000899 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000900
Marek Lindner44524fc2011-02-10 14:33:53 +0000901out:
902 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200903 batadv_neigh_node_free_ref(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000904 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200905 batadv_orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000906 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000907}
908
Antonio Quartullia73105b2011-04-27 14:27:44 +0200909static int check_unicast_ttvn(struct bat_priv *bat_priv,
910 struct sk_buff *skb) {
911 uint8_t curr_ttvn;
912 struct orig_node *orig_node;
913 struct ethhdr *ethhdr;
914 struct hard_iface *primary_if;
915 struct unicast_packet *unicast_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200916 bool tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200917
918 /* I could need to modify it */
919 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
920 return 0;
921
922 unicast_packet = (struct unicast_packet *)skb->data;
923
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200924 if (batadv_is_my_mac(unicast_packet->dest)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200925 tt_poss_change = bat_priv->tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200926 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200927 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200928 orig_node = batadv_orig_hash_find(bat_priv,
929 unicast_packet->dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200930
931 if (!orig_node)
932 return 0;
933
934 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200935 tt_poss_change = orig_node->tt_poss_change;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200936 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200937 }
938
939 /* Check whether I have to reroute the packet */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200940 if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
Antonio Quartulli8710e262012-03-16 11:52:31 +0100941 /* check if there is enough data before accessing it */
942 if (pskb_may_pull(skb, sizeof(struct unicast_packet) +
943 ETH_HLEN) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200944 return 0;
945
946 ethhdr = (struct ethhdr *)(skb->data +
947 sizeof(struct unicast_packet));
Antonio Quartulli3275e7c2012-03-16 18:03:28 +0100948
949 /* we don't have an updated route for this client, so we should
950 * not try to reroute the packet!!
951 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200952 if (batadv_tt_global_client_is_roaming(bat_priv,
953 ethhdr->h_dest))
Antonio Quartulli3275e7c2012-03-16 18:03:28 +0100954 return 1;
955
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200956 orig_node = batadv_transtable_search(bat_priv, NULL,
957 ethhdr->h_dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200958
959 if (!orig_node) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200960 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200961 return 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200962 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200963 if (!primary_if)
964 return 0;
965 memcpy(unicast_packet->dest,
966 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200967 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200968 } else {
969 memcpy(unicast_packet->dest, orig_node->orig,
970 ETH_ALEN);
971 curr_ttvn = (uint8_t)
972 atomic_read(&orig_node->last_ttvn);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200973 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200974 }
975
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100976 bat_dbg(DBG_ROUTES, bat_priv,
977 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
978 unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
979 unicast_packet->dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200980
981 unicast_packet->ttvn = curr_ttvn;
982 }
983 return 1;
984}
985
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200986int batadv_recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000987{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200988 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000989 struct unicast_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200990 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000991
992 if (check_unicast_packet(skb, hdr_size) < 0)
993 return NET_RX_DROP;
994
Antonio Quartullia73105b2011-04-27 14:27:44 +0200995 if (!check_unicast_ttvn(bat_priv, skb))
996 return NET_RX_DROP;
997
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000998 unicast_packet = (struct unicast_packet *)skb->data;
999
1000 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001001 if (batadv_is_my_mac(unicast_packet->dest)) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001002 batadv_interface_rx(recv_if->soft_iface, skb, recv_if,
1003 hdr_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001004 return NET_RX_SUCCESS;
1005 }
1006
Linus Lüssing7cefb142011-03-02 17:39:31 +00001007 return route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001008}
1009
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001010int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1011 struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001012{
1013 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1014 struct unicast_frag_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001015 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001016 struct sk_buff *new_skb = NULL;
1017 int ret;
1018
1019 if (check_unicast_packet(skb, hdr_size) < 0)
1020 return NET_RX_DROP;
1021
Antonio Quartullia73105b2011-04-27 14:27:44 +02001022 if (!check_unicast_ttvn(bat_priv, skb))
1023 return NET_RX_DROP;
1024
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001025 unicast_packet = (struct unicast_frag_packet *)skb->data;
1026
1027 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001028 if (batadv_is_my_mac(unicast_packet->dest)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001029
Sven Eckelmann88ed1e772012-05-12 02:09:40 +02001030 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001031
1032 if (ret == NET_RX_DROP)
1033 return NET_RX_DROP;
1034
1035 /* packet was buffered for late merge */
1036 if (!new_skb)
1037 return NET_RX_SUCCESS;
1038
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001039 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1040 sizeof(struct unicast_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001041 return NET_RX_SUCCESS;
1042 }
1043
Linus Lüssing7cefb142011-03-02 17:39:31 +00001044 return route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001045}
1046
1047
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001048int batadv_recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001049{
1050 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001051 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001052 struct bcast_packet *bcast_packet;
1053 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001054 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001055 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001056 int32_t seq_diff;
1057
1058 /* drop packet if it has not necessary minimum size */
1059 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001060 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001061
1062 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1063
1064 /* packet with broadcast indication but unicast recipient */
1065 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001066 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001067
1068 /* packet with broadcast sender address */
1069 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001070 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001071
1072 /* ignore broadcasts sent by myself */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001073 if (batadv_is_my_mac(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001074 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001075
1076 bcast_packet = (struct bcast_packet *)skb->data;
1077
1078 /* ignore broadcasts originated by myself */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001079 if (batadv_is_my_mac(bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001080 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001081
Sven Eckelmann76543d12011-11-20 15:47:38 +01001082 if (bcast_packet->header.ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001083 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001084
Sven Eckelmannda641192012-05-12 13:48:56 +02001085 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001086
1087 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001088 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001089
Marek Lindnerf3e00082011-01-25 21:52:11 +00001090 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001091
1092 /* check whether the packet is a duplicate */
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001093 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1094 ntohl(bcast_packet->seqno)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001095 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001096
1097 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1098
1099 /* check whether the packet is old and the host just restarted. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001100 if (batadv_window_protected(bat_priv, seq_diff,
1101 &orig_node->bcast_seqno_reset))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001102 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001103
1104 /* mark broadcast in flood history, update window position
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001105 * if required.
1106 */
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001107 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001108 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1109
Marek Lindnerf3e00082011-01-25 21:52:11 +00001110 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001111
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001112 /* check whether this has been sent by another originator before */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001113 if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001114 goto out;
1115
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001116 /* rebroadcast packet */
Sven Eckelmann9455e342012-05-12 02:09:37 +02001117 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001118
Simon Wunderlich23721382012-01-22 20:00:19 +01001119 /* don't hand the broadcast up if it is from an originator
1120 * from the same backbone.
1121 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001122 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
Simon Wunderlich23721382012-01-22 20:00:19 +01001123 goto out;
1124
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001125 /* broadcast for me */
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001126 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001127 ret = NET_RX_SUCCESS;
1128 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001129
Marek Lindnerf3e00082011-01-25 21:52:11 +00001130spin_unlock:
1131 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001132out:
1133 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001134 batadv_orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001135 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001136}
1137
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001138int batadv_recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001139{
1140 struct vis_packet *vis_packet;
1141 struct ethhdr *ethhdr;
1142 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann704509b2011-05-14 23:14:54 +02001143 int hdr_size = sizeof(*vis_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001144
1145 /* keep skb linear */
1146 if (skb_linearize(skb) < 0)
1147 return NET_RX_DROP;
1148
1149 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1150 return NET_RX_DROP;
1151
1152 vis_packet = (struct vis_packet *)skb->data;
1153 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1154
1155 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001156 if (!batadv_is_my_mac(ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001157 return NET_RX_DROP;
1158
1159 /* ignore own packets */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001160 if (batadv_is_my_mac(vis_packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001161 return NET_RX_DROP;
1162
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001163 if (batadv_is_my_mac(vis_packet->sender_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001164 return NET_RX_DROP;
1165
1166 switch (vis_packet->vis_type) {
1167 case VIS_TYPE_SERVER_SYNC:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001168 batadv_receive_server_sync_packet(bat_priv, vis_packet,
1169 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001170 break;
1171
1172 case VIS_TYPE_CLIENT_UPDATE:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001173 batadv_receive_client_update_packet(bat_priv, vis_packet,
1174 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001175 break;
1176
1177 default: /* ignore unknown packet */
1178 break;
1179 }
1180
1181 /* We take a copy of the data in the packet, so we should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001182 * always free the skbuf.
1183 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001184 return NET_RX_DROP;
1185}