blob: b3fd61c90f3268df427a8fe0a81715de5ac83cac [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
Sven Eckelmann63b01032012-05-16 20:23:13 +020032static int batadv_route_unicast_packet(struct sk_buff *skb,
33 struct hard_iface *recv_if);
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +010034
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;
Sven Eckelmann42d0b042012-06-03 22:19:17 +020045 uint8_t *w;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047 for (i = 0; i < hash->size; i++) {
48 head = &hash->table[i];
49
Marek Lindnerfb778ea2011-01-19 20:01:40 +000050 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +000051 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +000052 spin_lock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmann42d0b042012-06-03 22:19:17 +020053 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054 word = &(orig_node->bcast_own[word_index]);
55
Sven Eckelmann0f5f9322012-05-12 02:09:25 +020056 batadv_bit_get_packet(bat_priv, word, 1, 0);
Sven Eckelmann42d0b042012-06-03 22:19:17 +020057 w = &orig_node->bcast_own_sum[hard_iface->if_num];
58 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
Marek Lindner2ae2daf2011-01-19 20:01:42 +000059 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000060 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +000061 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063}
64
Sven Eckelmann63b01032012-05-16 20:23:13 +020065static void _batadv_update_route(struct bat_priv *bat_priv,
66 struct orig_node *orig_node,
67 struct neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000068{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000069 struct neigh_node *curr_router;
70
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020071 curr_router = batadv_orig_node_get_router(orig_node);
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000072
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000073 /* route deleted */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000074 if ((curr_router) && (!neigh_node)) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020075 batadv_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
76 orig_node->orig);
Sven Eckelmann08c36d32012-05-12 02:09:39 +020077 batadv_tt_global_del_orig(bat_priv, orig_node,
78 "Deleted route towards originator");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000079
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000080 /* route added */
81 } else if ((!curr_router) && (neigh_node)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000082
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020083 batadv_dbg(DBG_ROUTES, bat_priv,
84 "Adding route towards: %pM (via %pM)\n",
85 orig_node->orig, neigh_node->addr);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000086 /* route changed */
Sven Eckelmannbb899b82011-05-10 11:22:37 +020087 } else if (neigh_node && curr_router) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020088 batadv_dbg(DBG_ROUTES, bat_priv,
89 "Changing route towards: %pM (now via %pM - was via %pM)\n",
90 orig_node->orig, neigh_node->addr,
91 curr_router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000092 }
93
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000094 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020095 batadv_neigh_node_free_ref(curr_router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000096
97 /* increase refcount of new best neighbor */
Marek Lindner44524fc2011-02-10 14:33:53 +000098 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
99 neigh_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000100
101 spin_lock_bh(&orig_node->neigh_list_lock);
102 rcu_assign_pointer(orig_node->router, neigh_node);
103 spin_unlock_bh(&orig_node->neigh_list_lock);
104
105 /* decrease refcount of previous best neighbor */
106 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200107 batadv_neigh_node_free_ref(curr_router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000108}
109
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200110void batadv_update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
111 struct neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000112{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000113 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000114
115 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000116 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000117
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200118 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000119
120 if (router != neigh_node)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200121 _batadv_update_route(bat_priv, orig_node, neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000122
123out:
124 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200125 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000126}
127
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000128/* caller must hold the neigh_list_lock */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200129void batadv_bonding_candidate_del(struct orig_node *orig_node,
130 struct neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000131{
132 /* this neighbor is not part of our candidate list */
133 if (list_empty(&neigh_node->bonding_list))
134 goto out;
135
136 list_del_rcu(&neigh_node->bonding_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000137 INIT_LIST_HEAD(&neigh_node->bonding_list);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200138 batadv_neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000139 atomic_dec(&orig_node->bond_candidates);
140
141out:
142 return;
143}
144
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200145void batadv_bonding_candidate_add(struct orig_node *orig_node,
146 struct neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000147{
148 struct hlist_node *node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000149 struct neigh_node *tmp_neigh_node, *router = NULL;
150 uint8_t interference_candidate = 0;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000151
152 spin_lock_bh(&orig_node->neigh_list_lock);
153
154 /* only consider if it has the same primary address ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200155 if (!batadv_compare_eth(orig_node->orig,
156 neigh_node->orig_node->primary_addr))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000157 goto candidate_del;
158
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200159 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000160 if (!router)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000161 goto candidate_del;
162
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000163 /* ... and is good enough to be considered */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200164 if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000165 goto candidate_del;
166
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200167 /* check if we have another candidate with the same mac address or
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000168 * interface. If we do, we won't select this candidate because of
169 * possible interference.
170 */
171 hlist_for_each_entry_rcu(tmp_neigh_node, node,
172 &orig_node->neigh_list, list) {
173
174 if (tmp_neigh_node == neigh_node)
175 continue;
176
177 /* we only care if the other candidate is even
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200178 * considered as candidate.
179 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000180 if (list_empty(&tmp_neigh_node->bonding_list))
181 continue;
182
183 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200184 (batadv_compare_eth(neigh_node->addr,
185 tmp_neigh_node->addr))) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000186 interference_candidate = 1;
187 break;
188 }
189 }
190
191 /* don't care further if it is an interference candidate */
192 if (interference_candidate)
193 goto candidate_del;
194
195 /* this neighbor already is part of our candidate list */
196 if (!list_empty(&neigh_node->bonding_list))
197 goto out;
198
Marek Lindner44524fc2011-02-10 14:33:53 +0000199 if (!atomic_inc_not_zero(&neigh_node->refcount))
200 goto out;
201
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000202 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000203 atomic_inc(&orig_node->bond_candidates);
204 goto out;
205
206candidate_del:
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200207 batadv_bonding_candidate_del(orig_node, neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000208
209out:
210 spin_unlock_bh(&orig_node->neigh_list_lock);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000211
212 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200213 batadv_neigh_node_free_ref(router);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000214}
215
216/* copy primary address for bonding */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200217void
218batadv_bonding_save_primary(const struct orig_node *orig_node,
219 struct orig_node *orig_neigh_node,
220 const struct batman_ogm_packet *batman_ogm_packet)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000221{
Marek Lindnerb6da4bf2011-07-29 17:31:50 +0200222 if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000223 return;
224
225 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
226}
227
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228/* checks whether the host restarted and is in the protection time.
229 * returns:
230 * 0 if the packet is to be accepted
231 * 1 if the packet is to be ignored.
232 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200233int batadv_window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
234 unsigned long *last_reset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000235{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200236 if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
237 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
238 if (!batadv_has_timed_out(*last_reset,
239 BATADV_RESET_PROTECTION_MS))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240 return 1;
Marek Lindner8c7bf242012-03-17 15:28:33 +0800241
242 *last_reset = jiffies;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200243 batadv_dbg(DBG_BATMAN, bat_priv,
244 "old packet received, start protection\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245 }
Marek Lindner8c7bf242012-03-17 15:28:33 +0800246
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247 return 0;
248}
249
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200250bool batadv_check_management_packet(struct sk_buff *skb,
251 struct hard_iface *hard_iface,
252 int header_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254 struct ethhdr *ethhdr;
255
256 /* drop packet if it has not necessary minimum size */
Marek Lindnerc3e29312012-03-04 16:56:25 +0800257 if (unlikely(!pskb_may_pull(skb, header_len)))
258 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259
260 ethhdr = (struct ethhdr *)skb_mac_header(skb);
261
262 /* packet with broadcast indication but unicast recipient */
263 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800264 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265
266 /* packet with broadcast sender address */
267 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800268 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
270 /* create a copy of the skb, if needed, to modify it. */
271 if (skb_cow(skb, 0) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800272 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273
274 /* keep skb linear */
275 if (skb_linearize(skb) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800276 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277
Marek Lindnerc3e29312012-03-04 16:56:25 +0800278 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279}
280
Sven Eckelmann63b01032012-05-16 20:23:13 +0200281static int batadv_recv_my_icmp_packet(struct bat_priv *bat_priv,
282 struct sk_buff *skb, size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000283{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200284 struct hard_iface *primary_if = NULL;
Marek Lindner44524fc2011-02-10 14:33:53 +0000285 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000286 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287 struct icmp_packet_rr *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000288 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289
290 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000291
292 /* add data to device queue */
293 if (icmp_packet->msg_type != ECHO_REQUEST) {
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200294 batadv_socket_receive_packet(icmp_packet, icmp_len);
Marek Lindner44524fc2011-02-10 14:33:53 +0000295 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296 }
297
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200298 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200299 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000300 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301
302 /* answer echo request (ping) */
303 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200304 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000305 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000306 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000307
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200308 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000309 if (!router)
310 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311
Marek Lindner44524fc2011-02-10 14:33:53 +0000312 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100313 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000314 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000315
Marek Lindner44524fc2011-02-10 14:33:53 +0000316 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000317
Marek Lindner44524fc2011-02-10 14:33:53 +0000318 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200319 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Marek Lindner44524fc2011-02-10 14:33:53 +0000320 icmp_packet->msg_type = ECHO_REPLY;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200321 icmp_packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000322
Sven Eckelmann9455e342012-05-12 02:09:37 +0200323 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000324 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325
Marek Lindner44524fc2011-02-10 14:33:53 +0000326out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200327 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200328 batadv_hardif_free_ref(primary_if);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000329 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200330 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000331 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200332 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333 return ret;
334}
335
Sven Eckelmann63b01032012-05-16 20:23:13 +0200336static int batadv_recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
337 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200339 struct hard_iface *primary_if = NULL;
Marek Lindner44524fc2011-02-10 14:33:53 +0000340 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000341 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342 struct icmp_packet *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000343 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344
345 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346
347 /* send TTL exceeded if packet is an echo request (traceroute) */
348 if (icmp_packet->msg_type != ECHO_REQUEST) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100349 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
350 icmp_packet->orig, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000351 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352 }
353
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200354 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200355 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000356 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357
358 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200359 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000360 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000361 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000362
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200363 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000364 if (!router)
365 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366
Marek Lindner44524fc2011-02-10 14:33:53 +0000367 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100368 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000369 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
Marek Lindner44524fc2011-02-10 14:33:53 +0000371 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372
Marek Lindner44524fc2011-02-10 14:33:53 +0000373 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200374 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Marek Lindner44524fc2011-02-10 14:33:53 +0000375 icmp_packet->msg_type = TTL_EXCEEDED;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200376 icmp_packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
Sven Eckelmann9455e342012-05-12 02:09:37 +0200378 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000379 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380
Marek Lindner44524fc2011-02-10 14:33:53 +0000381out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200382 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200383 batadv_hardif_free_ref(primary_if);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000384 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200385 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000386 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200387 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388 return ret;
389}
390
391
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200392int batadv_recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393{
394 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
395 struct icmp_packet_rr *icmp_packet;
396 struct ethhdr *ethhdr;
Marek Lindner44524fc2011-02-10 14:33:53 +0000397 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000398 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399 int hdr_size = sizeof(struct icmp_packet);
Marek Lindner44524fc2011-02-10 14:33:53 +0000400 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200402 /* we truncate all incoming icmp packets if they don't match our size */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403 if (skb->len >= sizeof(struct icmp_packet_rr))
404 hdr_size = sizeof(struct icmp_packet_rr);
405
406 /* drop packet if it has not necessary minimum size */
407 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindner44524fc2011-02-10 14:33:53 +0000408 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409
410 ethhdr = (struct ethhdr *)skb_mac_header(skb);
411
412 /* packet with unicast indication but broadcast recipient */
413 if (is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000414 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415
416 /* packet with broadcast sender address */
417 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindner44524fc2011-02-10 14:33:53 +0000418 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000419
420 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200421 if (!batadv_is_my_mac(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000422 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423
424 icmp_packet = (struct icmp_packet_rr *)skb->data;
425
426 /* add record route information if not full */
427 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200428 (icmp_packet->rr_cur < BATADV_RR_LEN)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100430 ethhdr->h_dest, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431 icmp_packet->rr_cur++;
432 }
433
434 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200435 if (batadv_is_my_mac(icmp_packet->dst))
Sven Eckelmann63b01032012-05-16 20:23:13 +0200436 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437
438 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100439 if (icmp_packet->header.ttl < 2)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200440 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200443 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000444 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000445 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000446
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200447 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000448 if (!router)
449 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450
Marek Lindner44524fc2011-02-10 14:33:53 +0000451 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100452 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000453 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454
Marek Lindner44524fc2011-02-10 14:33:53 +0000455 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456
Marek Lindner44524fc2011-02-10 14:33:53 +0000457 /* decrement ttl */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100458 icmp_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459
Marek Lindner44524fc2011-02-10 14:33:53 +0000460 /* route it */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200461 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000462 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463
Marek Lindner44524fc2011-02-10 14:33:53 +0000464out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000465 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200466 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000467 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200468 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469 return ret;
470}
471
Linus Lüssing55158622011-03-14 22:43:27 +0000472/* In the bonding case, send the packets in a round
473 * robin fashion over the remaining interfaces.
474 *
475 * This method rotates the bonding list and increases the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200476 * returned router's refcount.
477 */
Sven Eckelmann63b01032012-05-16 20:23:13 +0200478static struct neigh_node *
479batadv_find_bond_router(struct orig_node *primary_orig,
480 const struct hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000481{
482 struct neigh_node *tmp_neigh_node;
483 struct neigh_node *router = NULL, *first_candidate = NULL;
484
485 rcu_read_lock();
486 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
487 bonding_list) {
488 if (!first_candidate)
489 first_candidate = tmp_neigh_node;
490
491 /* recv_if == NULL on the first node. */
492 if (tmp_neigh_node->if_incoming == recv_if)
493 continue;
494
495 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
496 continue;
497
498 router = tmp_neigh_node;
499 break;
500 }
501
502 /* use the first candidate if nothing was found. */
503 if (!router && first_candidate &&
504 atomic_inc_not_zero(&first_candidate->refcount))
505 router = first_candidate;
506
507 if (!router)
508 goto out;
509
510 /* selected should point to the next element
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200511 * after the current router
512 */
Linus Lüssing55158622011-03-14 22:43:27 +0000513 spin_lock_bh(&primary_orig->neigh_list_lock);
514 /* this is a list_move(), which unfortunately
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200515 * does not exist as rcu version
516 */
Linus Lüssing55158622011-03-14 22:43:27 +0000517 list_del_rcu(&primary_orig->bond_list);
518 list_add_rcu(&primary_orig->bond_list,
519 &router->bonding_list);
520 spin_unlock_bh(&primary_orig->neigh_list_lock);
521
522out:
523 rcu_read_unlock();
524 return router;
525}
526
527/* Interface Alternating: Use the best of the
528 * remaining candidates which are not using
529 * this interface.
530 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200531 * Increases the returned router's refcount
532 */
Sven Eckelmann63b01032012-05-16 20:23:13 +0200533static struct neigh_node *
534batadv_find_ifalter_router(struct orig_node *primary_orig,
535 const struct hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000536{
537 struct neigh_node *tmp_neigh_node;
538 struct neigh_node *router = NULL, *first_candidate = NULL;
539
540 rcu_read_lock();
541 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
542 bonding_list) {
543 if (!first_candidate)
544 first_candidate = tmp_neigh_node;
545
546 /* recv_if == NULL on the first node. */
547 if (tmp_neigh_node->if_incoming == recv_if)
548 continue;
549
550 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
551 continue;
552
553 /* if we don't have a router yet
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200554 * or this one is better, choose it.
555 */
Linus Lüssing55158622011-03-14 22:43:27 +0000556 if ((!router) ||
557 (tmp_neigh_node->tq_avg > router->tq_avg)) {
558 /* decrement refcount of
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200559 * previously selected router
560 */
Linus Lüssing55158622011-03-14 22:43:27 +0000561 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200562 batadv_neigh_node_free_ref(router);
Linus Lüssing55158622011-03-14 22:43:27 +0000563
564 router = tmp_neigh_node;
565 atomic_inc_not_zero(&router->refcount);
566 }
567
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200568 batadv_neigh_node_free_ref(tmp_neigh_node);
Linus Lüssing55158622011-03-14 22:43:27 +0000569 }
570
571 /* use the first candidate if nothing was found. */
572 if (!router && first_candidate &&
573 atomic_inc_not_zero(&first_candidate->refcount))
574 router = first_candidate;
575
576 rcu_read_unlock();
577 return router;
578}
579
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200580int batadv_recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200581{
582 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
583 struct tt_query_packet *tt_query;
Al Virof25bd582012-04-22 07:44:27 +0100584 uint16_t tt_size;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200585 struct ethhdr *ethhdr;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200586 char tt_flag;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200587
588 /* drop packet if it has not necessary minimum size */
589 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
590 goto out;
591
592 /* I could need to modify it */
593 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
594 goto out;
595
596 ethhdr = (struct ethhdr *)skb_mac_header(skb);
597
598 /* packet with unicast indication but broadcast recipient */
599 if (is_broadcast_ether_addr(ethhdr->h_dest))
600 goto out;
601
602 /* packet with broadcast sender address */
603 if (is_broadcast_ether_addr(ethhdr->h_source))
604 goto out;
605
606 tt_query = (struct tt_query_packet *)skb->data;
607
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200608 switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200609 case TT_REQUEST:
Martin Hundebøllf8214862012-04-20 17:02:45 +0200610 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_RX);
611
Antonio Quartullia73105b2011-04-27 14:27:44 +0200612 /* If we cannot provide an answer the tt_request is
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200613 * forwarded
614 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200615 if (!batadv_send_tt_response(bat_priv, tt_query)) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200616 tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.';
617 batadv_dbg(DBG_TT, bat_priv,
618 "Routing TT_REQUEST to %pM [%c]\n",
619 tt_query->dst,
620 tt_flag);
Sven Eckelmann63b01032012-05-16 20:23:13 +0200621 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200622 }
623 break;
624 case TT_RESPONSE:
Martin Hundebøllf8214862012-04-20 17:02:45 +0200625 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_RX);
626
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200627 if (batadv_is_my_mac(tt_query->dst)) {
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200628 /* packet needs to be linearized to access the TT
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200629 * changes
630 */
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200631 if (skb_linearize(skb) < 0)
632 goto out;
Antonio Quartullid2b6cc82012-06-14 22:21:28 +0200633 /* skb_linearize() possibly changed skb->data */
634 tt_query = (struct tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200635
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200636 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200637
638 /* Ensure we have all the claimed data */
639 if (unlikely(skb_headlen(skb) <
Al Virof25bd582012-04-22 07:44:27 +0100640 sizeof(struct tt_query_packet) + tt_size))
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200641 goto out;
642
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200643 batadv_handle_tt_response(bat_priv, tt_query);
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200644 } else {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200645 tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.';
646 batadv_dbg(DBG_TT, bat_priv,
647 "Routing TT_RESPONSE to %pM [%c]\n",
648 tt_query->dst,
649 tt_flag);
Sven Eckelmann63b01032012-05-16 20:23:13 +0200650 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200651 }
652 break;
653 }
654
655out:
656 /* returning NET_RX_DROP will make the caller function kfree the skb */
657 return NET_RX_DROP;
658}
659
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200660int batadv_recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200661{
662 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
663 struct roam_adv_packet *roam_adv_packet;
664 struct orig_node *orig_node;
665 struct ethhdr *ethhdr;
666
667 /* drop packet if it has not necessary minimum size */
668 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
669 goto out;
670
671 ethhdr = (struct ethhdr *)skb_mac_header(skb);
672
673 /* packet with unicast indication but broadcast recipient */
674 if (is_broadcast_ether_addr(ethhdr->h_dest))
675 goto out;
676
677 /* packet with broadcast sender address */
678 if (is_broadcast_ether_addr(ethhdr->h_source))
679 goto out;
680
Martin Hundebøllf8214862012-04-20 17:02:45 +0200681 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_RX);
682
Antonio Quartullicc47f662011-04-27 14:27:57 +0200683 roam_adv_packet = (struct roam_adv_packet *)skb->data;
684
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200685 if (!batadv_is_my_mac(roam_adv_packet->dst))
Sven Eckelmann63b01032012-05-16 20:23:13 +0200686 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200687
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100688 /* check if it is a backbone gateway. we don't accept
689 * roaming advertisement from it, as it has the same
690 * entries as we have.
691 */
Sven Eckelmann08adf152012-05-12 13:38:47 +0200692 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100693 goto out;
694
Sven Eckelmannda641192012-05-12 13:48:56 +0200695 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200696 if (!orig_node)
697 goto out;
698
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200699 batadv_dbg(DBG_TT, bat_priv,
700 "Received ROAMING_ADV from %pM (client %pM)\n",
701 roam_adv_packet->src, roam_adv_packet->client);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200702
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200703 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200704 TT_CLIENT_ROAM,
705 atomic_read(&orig_node->last_ttvn) + 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200706
707 /* Roaming phase starts: I have new information but the ttvn has not
708 * been incremented yet. This flag will make me check all the incoming
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200709 * packets for the correct destination.
710 */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200711 bat_priv->tt_poss_change = true;
712
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200713 batadv_orig_node_free_ref(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200714out:
715 /* returning NET_RX_DROP will make the caller function kfree the skb */
716 return NET_RX_DROP;
717}
718
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000719/* find a suitable router for this originator, and use
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000720 * bonding if possible. increases the found neighbors
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200721 * refcount.
722 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200723struct neigh_node *batadv_find_router(struct bat_priv *bat_priv,
724 struct orig_node *orig_node,
725 const struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000726{
727 struct orig_node *primary_orig_node;
728 struct orig_node *router_orig;
Linus Lüssing55158622011-03-14 22:43:27 +0000729 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000730 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
731 int bonding_enabled;
Sven Eckelmannda641192012-05-12 13:48:56 +0200732 uint8_t *primary_addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733
734 if (!orig_node)
735 return NULL;
736
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200737 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000738 if (!router)
Marek Lindner01df2b62011-05-05 14:14:46 +0200739 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000740
741 /* without bonding, the first node should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200742 * always choose the default router.
743 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744 bonding_enabled = atomic_read(&bat_priv->bonding);
745
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000746 rcu_read_lock();
747 /* select default router to output */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000748 router_orig = router->orig_node;
Marek Lindner01df2b62011-05-05 14:14:46 +0200749 if (!router_orig)
750 goto err_unlock;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000751
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000752 if ((!recv_if) && (!bonding_enabled))
753 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000754
Sven Eckelmannda641192012-05-12 13:48:56 +0200755 primary_addr = router_orig->primary_addr;
756
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000757 /* if we have something in the primary_addr, we can search
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200758 * for a potential bonding candidate.
759 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200760 if (batadv_compare_eth(primary_addr, zero_mac))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000761 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000762
763 /* find the orig_node which has the primary interface. might
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200764 * even be the same as our router_orig in many cases
765 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200766 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767 primary_orig_node = router_orig;
768 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200769 primary_orig_node = batadv_orig_hash_find(bat_priv,
770 primary_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000771 if (!primary_orig_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000772 goto return_router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000773
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200774 batadv_orig_node_free_ref(primary_orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000775 }
776
777 /* with less than 2 candidates, we can't do any
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200778 * bonding and prefer the original router.
779 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000780 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
781 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000782
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000783 /* all nodes between should choose a candidate which
784 * is is not on the interface where the packet came
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200785 * in.
786 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200787 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000788
Linus Lüssing55158622011-03-14 22:43:27 +0000789 if (bonding_enabled)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200790 router = batadv_find_bond_router(primary_orig_node, recv_if);
Linus Lüssing55158622011-03-14 22:43:27 +0000791 else
Sven Eckelmann63b01032012-05-16 20:23:13 +0200792 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000793
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000794return_router:
Antonio Quartullie2cbc112011-05-08 20:52:57 +0200795 if (router && router->if_incoming->if_status != IF_ACTIVE)
796 goto err_unlock;
797
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000798 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000799 return router;
Marek Lindner01df2b62011-05-05 14:14:46 +0200800err_unlock:
801 rcu_read_unlock();
802err:
803 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200804 batadv_neigh_node_free_ref(router);
Marek Lindner01df2b62011-05-05 14:14:46 +0200805 return NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000806}
807
Sven Eckelmann63b01032012-05-16 20:23:13 +0200808static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000809{
810 struct ethhdr *ethhdr;
811
812 /* drop packet if it has not necessary minimum size */
813 if (unlikely(!pskb_may_pull(skb, hdr_size)))
814 return -1;
815
816 ethhdr = (struct ethhdr *)skb_mac_header(skb);
817
818 /* packet with unicast indication but broadcast recipient */
819 if (is_broadcast_ether_addr(ethhdr->h_dest))
820 return -1;
821
822 /* packet with broadcast sender address */
823 if (is_broadcast_ether_addr(ethhdr->h_source))
824 return -1;
825
826 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200827 if (!batadv_is_my_mac(ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000828 return -1;
829
830 return 0;
831}
832
Sven Eckelmann63b01032012-05-16 20:23:13 +0200833static int batadv_route_unicast_packet(struct sk_buff *skb,
834 struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000835{
836 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindner44524fc2011-02-10 14:33:53 +0000837 struct orig_node *orig_node = NULL;
838 struct neigh_node *neigh_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000839 struct unicast_packet *unicast_packet;
840 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
Marek Lindner44524fc2011-02-10 14:33:53 +0000841 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000842 struct sk_buff *new_skb;
843
844 unicast_packet = (struct unicast_packet *)skb->data;
845
846 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100847 if (unicast_packet->header.ttl < 2) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100848 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
849 ethhdr->h_source, unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +0000850 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000851 }
852
853 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200854 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
Marek Lindner7aadf882011-02-18 12:28:09 +0000855
Marek Lindner44524fc2011-02-10 14:33:53 +0000856 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +0200857 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000858
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000859 /* find_router() increases neigh_nodes refcount if found. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200860 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000861
Marek Lindnerd0072602011-01-19 20:01:44 +0000862 if (!neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000863 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000864
865 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100866 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000867 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000868
869 unicast_packet = (struct unicast_packet *)skb->data;
870
Sven Eckelmann76543d12011-11-20 15:47:38 +0100871 if (unicast_packet->header.packet_type == BAT_UNICAST &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000872 atomic_read(&bat_priv->fragmentation) &&
Marek Lindnerd0072602011-01-19 20:01:44 +0000873 skb->len > neigh_node->if_incoming->net_dev->mtu) {
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200874 ret = batadv_frag_send_skb(skb, bat_priv,
875 neigh_node->if_incoming,
876 neigh_node->addr);
Marek Lindnerd0072602011-01-19 20:01:44 +0000877 goto out;
878 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000879
Sven Eckelmann76543d12011-11-20 15:47:38 +0100880 if (unicast_packet->header.packet_type == BAT_UNICAST_FRAG &&
Sven Eckelmannf0530ee2012-05-12 13:48:57 +0200881 batadv_frag_can_reassemble(skb,
882 neigh_node->if_incoming->net_dev->mtu)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000883
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200884 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000885
886 if (ret == NET_RX_DROP)
Marek Lindner44524fc2011-02-10 14:33:53 +0000887 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000888
889 /* packet was buffered for late merge */
Marek Lindner44524fc2011-02-10 14:33:53 +0000890 if (!new_skb) {
891 ret = NET_RX_SUCCESS;
892 goto out;
893 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894
895 skb = new_skb;
896 unicast_packet = (struct unicast_packet *)skb->data;
897 }
898
899 /* decrement ttl */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100900 unicast_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000901
Martin Hundebøllf8214862012-04-20 17:02:45 +0200902 /* Update stats counter */
903 batadv_inc_counter(bat_priv, BAT_CNT_FORWARD);
904 batadv_add_counter(bat_priv, BAT_CNT_FORWARD_BYTES,
905 skb->len + ETH_HLEN);
906
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000907 /* route it */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200908 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000909 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000910
Marek Lindner44524fc2011-02-10 14:33:53 +0000911out:
912 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200913 batadv_neigh_node_free_ref(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000914 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200915 batadv_orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000916 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000917}
918
Sven Eckelmann63b01032012-05-16 20:23:13 +0200919static int batadv_check_unicast_ttvn(struct bat_priv *bat_priv,
920 struct sk_buff *skb) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200921 uint8_t curr_ttvn;
922 struct orig_node *orig_node;
923 struct ethhdr *ethhdr;
924 struct hard_iface *primary_if;
925 struct unicast_packet *unicast_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200926 bool tt_poss_change;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200927 int is_old_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200928
929 /* I could need to modify it */
930 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
931 return 0;
932
933 unicast_packet = (struct unicast_packet *)skb->data;
934
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200935 if (batadv_is_my_mac(unicast_packet->dest)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200936 tt_poss_change = bat_priv->tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200937 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200938 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200939 orig_node = batadv_orig_hash_find(bat_priv,
940 unicast_packet->dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200941
942 if (!orig_node)
943 return 0;
944
945 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200946 tt_poss_change = orig_node->tt_poss_change;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200947 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200948 }
949
950 /* Check whether I have to reroute the packet */
Sven Eckelmann3e348192012-05-16 20:23:22 +0200951 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
952 if (is_old_ttvn || tt_poss_change) {
Antonio Quartulli8710e262012-03-16 11:52:31 +0100953 /* check if there is enough data before accessing it */
954 if (pskb_may_pull(skb, sizeof(struct unicast_packet) +
955 ETH_HLEN) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200956 return 0;
957
958 ethhdr = (struct ethhdr *)(skb->data +
959 sizeof(struct unicast_packet));
Antonio Quartulli3275e7c2012-03-16 18:03:28 +0100960
961 /* we don't have an updated route for this client, so we should
962 * not try to reroute the packet!!
963 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200964 if (batadv_tt_global_client_is_roaming(bat_priv,
965 ethhdr->h_dest))
Antonio Quartulli3275e7c2012-03-16 18:03:28 +0100966 return 1;
967
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200968 orig_node = batadv_transtable_search(bat_priv, NULL,
969 ethhdr->h_dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200970
971 if (!orig_node) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200972 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200973 return 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200974 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200975 if (!primary_if)
976 return 0;
977 memcpy(unicast_packet->dest,
978 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200979 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200980 } else {
981 memcpy(unicast_packet->dest, orig_node->orig,
982 ETH_ALEN);
983 curr_ttvn = (uint8_t)
984 atomic_read(&orig_node->last_ttvn);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200985 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200986 }
987
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200988 batadv_dbg(DBG_ROUTES, bat_priv,
989 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
990 unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
991 unicast_packet->dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200992
993 unicast_packet->ttvn = curr_ttvn;
994 }
995 return 1;
996}
997
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200998int batadv_recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000999{
Antonio Quartullia73105b2011-04-27 14:27:44 +02001000 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001001 struct unicast_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001002 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001003
Sven Eckelmann63b01032012-05-16 20:23:13 +02001004 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001005 return NET_RX_DROP;
1006
Sven Eckelmann63b01032012-05-16 20:23:13 +02001007 if (!batadv_check_unicast_ttvn(bat_priv, skb))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001008 return NET_RX_DROP;
1009
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001010 unicast_packet = (struct unicast_packet *)skb->data;
1011
1012 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001013 if (batadv_is_my_mac(unicast_packet->dest)) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001014 batadv_interface_rx(recv_if->soft_iface, skb, recv_if,
1015 hdr_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001016 return NET_RX_SUCCESS;
1017 }
1018
Sven Eckelmann63b01032012-05-16 20:23:13 +02001019 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001020}
1021
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001022int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1023 struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001024{
1025 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1026 struct unicast_frag_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001027 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001028 struct sk_buff *new_skb = NULL;
1029 int ret;
1030
Sven Eckelmann63b01032012-05-16 20:23:13 +02001031 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001032 return NET_RX_DROP;
1033
Sven Eckelmann63b01032012-05-16 20:23:13 +02001034 if (!batadv_check_unicast_ttvn(bat_priv, skb))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001035 return NET_RX_DROP;
1036
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001037 unicast_packet = (struct unicast_frag_packet *)skb->data;
1038
1039 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001040 if (batadv_is_my_mac(unicast_packet->dest)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001041
Sven Eckelmann88ed1e772012-05-12 02:09:40 +02001042 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001043
1044 if (ret == NET_RX_DROP)
1045 return NET_RX_DROP;
1046
1047 /* packet was buffered for late merge */
1048 if (!new_skb)
1049 return NET_RX_SUCCESS;
1050
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001051 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1052 sizeof(struct unicast_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001053 return NET_RX_SUCCESS;
1054 }
1055
Sven Eckelmann63b01032012-05-16 20:23:13 +02001056 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001057}
1058
1059
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001060int batadv_recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001061{
1062 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001063 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001064 struct bcast_packet *bcast_packet;
1065 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001066 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001067 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001068 int32_t seq_diff;
1069
1070 /* drop packet if it has not necessary minimum size */
1071 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001072 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001073
1074 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1075
1076 /* packet with broadcast indication but unicast recipient */
1077 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001078 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001079
1080 /* packet with broadcast sender address */
1081 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001082 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001083
1084 /* ignore broadcasts sent by myself */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001085 if (batadv_is_my_mac(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001086 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001087
1088 bcast_packet = (struct bcast_packet *)skb->data;
1089
1090 /* ignore broadcasts originated by myself */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001091 if (batadv_is_my_mac(bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001092 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001093
Sven Eckelmann76543d12011-11-20 15:47:38 +01001094 if (bcast_packet->header.ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001095 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001096
Sven Eckelmannda641192012-05-12 13:48:56 +02001097 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001098
1099 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001100 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001101
Marek Lindnerf3e00082011-01-25 21:52:11 +00001102 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001103
1104 /* check whether the packet is a duplicate */
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001105 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1106 ntohl(bcast_packet->seqno)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001107 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001108
1109 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1110
1111 /* check whether the packet is old and the host just restarted. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001112 if (batadv_window_protected(bat_priv, seq_diff,
1113 &orig_node->bcast_seqno_reset))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001114 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001115
1116 /* mark broadcast in flood history, update window position
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001117 * if required.
1118 */
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001119 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001120 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1121
Marek Lindnerf3e00082011-01-25 21:52:11 +00001122 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001123
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001124 /* check whether this has been sent by another originator before */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001125 if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001126 goto out;
1127
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001128 /* rebroadcast packet */
Sven Eckelmann9455e342012-05-12 02:09:37 +02001129 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001130
Simon Wunderlich23721382012-01-22 20:00:19 +01001131 /* don't hand the broadcast up if it is from an originator
1132 * from the same backbone.
1133 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001134 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
Simon Wunderlich23721382012-01-22 20:00:19 +01001135 goto out;
1136
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001137 /* broadcast for me */
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001138 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001139 ret = NET_RX_SUCCESS;
1140 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001141
Marek Lindnerf3e00082011-01-25 21:52:11 +00001142spin_unlock:
1143 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001144out:
1145 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001146 batadv_orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001147 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001148}
1149
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001150int batadv_recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001151{
1152 struct vis_packet *vis_packet;
1153 struct ethhdr *ethhdr;
1154 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann704509b2011-05-14 23:14:54 +02001155 int hdr_size = sizeof(*vis_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001156
1157 /* keep skb linear */
1158 if (skb_linearize(skb) < 0)
1159 return NET_RX_DROP;
1160
1161 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1162 return NET_RX_DROP;
1163
1164 vis_packet = (struct vis_packet *)skb->data;
1165 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1166
1167 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001168 if (!batadv_is_my_mac(ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001169 return NET_RX_DROP;
1170
1171 /* ignore own packets */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001172 if (batadv_is_my_mac(vis_packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001173 return NET_RX_DROP;
1174
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001175 if (batadv_is_my_mac(vis_packet->sender_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001176 return NET_RX_DROP;
1177
1178 switch (vis_packet->vis_type) {
1179 case VIS_TYPE_SERVER_SYNC:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001180 batadv_receive_server_sync_packet(bat_priv, vis_packet,
1181 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001182 break;
1183
1184 case VIS_TYPE_CLIENT_UPDATE:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001185 batadv_receive_client_update_packet(bat_priv, vis_packet,
1186 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001187 break;
1188
1189 default: /* ignore unknown packet */
1190 break;
1191 }
1192
1193 /* We take a copy of the data in the packet, so we should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001194 * always free the skbuf.
1195 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001196 return NET_RX_DROP;
1197}