blob: b8c47dcb5853dcdfc928795216d1575678ef311b [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 Eckelmann0f5f93222012-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 Eckelmann39c75a52012-06-03 22:19:22 +020075 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
76 "Deleting route towards: %pM\n", 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 Eckelmann39c75a52012-06-03 22:19:22 +020083 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020084 "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 Eckelmann39c75a52012-06-03 22:19:22 +020088 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020089 "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{
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200222 if (!(batman_ogm_packet->flags & BATADV_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 Eckelmann39c75a52012-06-03 22:19:22 +0200243 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200244 "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 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200293 if (icmp_packet->msg_type != BATADV_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);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200320 icmp_packet->msg_type = BATADV_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) */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200348 if (icmp_packet->msg_type != BATADV_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);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200375 icmp_packet->msg_type = BATADV_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) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200609 case BATADV_TT_REQUEST:
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200610 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200611
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 Eckelmannacd34af2012-06-03 22:19:21 +0200616 if (tt_query->flags & BATADV_TT_FULL_TABLE)
617 tt_flag = 'F';
618 else
619 tt_flag = '.';
620
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200621 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200622 "Routing TT_REQUEST to %pM [%c]\n",
623 tt_query->dst,
624 tt_flag);
Sven Eckelmann63b01032012-05-16 20:23:13 +0200625 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200626 }
627 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200628 case BATADV_TT_RESPONSE:
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200629 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200630
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200631 if (batadv_is_my_mac(tt_query->dst)) {
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200632 /* packet needs to be linearized to access the TT
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200633 * changes
634 */
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200635 if (skb_linearize(skb) < 0)
636 goto out;
Antonio Quartullid2b6cc82012-06-14 22:21:28 +0200637 /* skb_linearize() possibly changed skb->data */
638 tt_query = (struct tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200639
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200640 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200641
642 /* Ensure we have all the claimed data */
643 if (unlikely(skb_headlen(skb) <
Al Virof25bd582012-04-22 07:44:27 +0100644 sizeof(struct tt_query_packet) + tt_size))
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200645 goto out;
646
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200647 batadv_handle_tt_response(bat_priv, tt_query);
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200648 } else {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200649 if (tt_query->flags & BATADV_TT_FULL_TABLE)
650 tt_flag = 'F';
651 else
652 tt_flag = '.';
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200653 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200654 "Routing TT_RESPONSE to %pM [%c]\n",
655 tt_query->dst,
656 tt_flag);
Sven Eckelmann63b01032012-05-16 20:23:13 +0200657 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200658 }
659 break;
660 }
661
662out:
663 /* returning NET_RX_DROP will make the caller function kfree the skb */
664 return NET_RX_DROP;
665}
666
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200667int batadv_recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200668{
669 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
670 struct roam_adv_packet *roam_adv_packet;
671 struct orig_node *orig_node;
672 struct ethhdr *ethhdr;
673
674 /* drop packet if it has not necessary minimum size */
675 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
676 goto out;
677
678 ethhdr = (struct ethhdr *)skb_mac_header(skb);
679
680 /* packet with unicast indication but broadcast recipient */
681 if (is_broadcast_ether_addr(ethhdr->h_dest))
682 goto out;
683
684 /* packet with broadcast sender address */
685 if (is_broadcast_ether_addr(ethhdr->h_source))
686 goto out;
687
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200688 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200689
Antonio Quartullicc47f662011-04-27 14:27:57 +0200690 roam_adv_packet = (struct roam_adv_packet *)skb->data;
691
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200692 if (!batadv_is_my_mac(roam_adv_packet->dst))
Sven Eckelmann63b01032012-05-16 20:23:13 +0200693 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200694
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100695 /* check if it is a backbone gateway. we don't accept
696 * roaming advertisement from it, as it has the same
697 * entries as we have.
698 */
Sven Eckelmann08adf152012-05-12 13:38:47 +0200699 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100700 goto out;
701
Sven Eckelmannda641192012-05-12 13:48:56 +0200702 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200703 if (!orig_node)
704 goto out;
705
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200706 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200707 "Received ROAMING_ADV from %pM (client %pM)\n",
708 roam_adv_packet->src, roam_adv_packet->client);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200709
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200710 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200711 BATADV_TT_CLIENT_ROAM,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200712 atomic_read(&orig_node->last_ttvn) + 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200713
714 /* Roaming phase starts: I have new information but the ttvn has not
715 * been incremented yet. This flag will make me check all the incoming
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200716 * packets for the correct destination.
717 */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200718 bat_priv->tt_poss_change = true;
719
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200720 batadv_orig_node_free_ref(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200721out:
722 /* returning NET_RX_DROP will make the caller function kfree the skb */
723 return NET_RX_DROP;
724}
725
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000726/* find a suitable router for this originator, and use
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000727 * bonding if possible. increases the found neighbors
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200728 * refcount.
729 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200730struct neigh_node *batadv_find_router(struct bat_priv *bat_priv,
731 struct orig_node *orig_node,
732 const struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733{
734 struct orig_node *primary_orig_node;
735 struct orig_node *router_orig;
Linus Lüssing55158622011-03-14 22:43:27 +0000736 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000737 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
738 int bonding_enabled;
Sven Eckelmannda641192012-05-12 13:48:56 +0200739 uint8_t *primary_addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000740
741 if (!orig_node)
742 return NULL;
743
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200744 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000745 if (!router)
Marek Lindner01df2b62011-05-05 14:14:46 +0200746 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000747
748 /* without bonding, the first node should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200749 * always choose the default router.
750 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000751 bonding_enabled = atomic_read(&bat_priv->bonding);
752
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000753 rcu_read_lock();
754 /* select default router to output */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000755 router_orig = router->orig_node;
Marek Lindner01df2b62011-05-05 14:14:46 +0200756 if (!router_orig)
757 goto err_unlock;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000758
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000759 if ((!recv_if) && (!bonding_enabled))
760 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000761
Sven Eckelmannda641192012-05-12 13:48:56 +0200762 primary_addr = router_orig->primary_addr;
763
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000764 /* if we have something in the primary_addr, we can search
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200765 * for a potential bonding candidate.
766 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200767 if (batadv_compare_eth(primary_addr, zero_mac))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000768 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000769
770 /* find the orig_node which has the primary interface. might
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200771 * even be the same as our router_orig in many cases
772 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200773 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000774 primary_orig_node = router_orig;
775 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200776 primary_orig_node = batadv_orig_hash_find(bat_priv,
777 primary_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778 if (!primary_orig_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000779 goto return_router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000780
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200781 batadv_orig_node_free_ref(primary_orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000782 }
783
784 /* with less than 2 candidates, we can't do any
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200785 * bonding and prefer the original router.
786 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000787 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
788 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000789
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000790 /* all nodes between should choose a candidate which
791 * is is not on the interface where the packet came
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200792 * in.
793 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200794 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000795
Linus Lüssing55158622011-03-14 22:43:27 +0000796 if (bonding_enabled)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200797 router = batadv_find_bond_router(primary_orig_node, recv_if);
Linus Lüssing55158622011-03-14 22:43:27 +0000798 else
Sven Eckelmann63b01032012-05-16 20:23:13 +0200799 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000800
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000801return_router:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200802 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
Antonio Quartullie2cbc112011-05-08 20:52:57 +0200803 goto err_unlock;
804
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000805 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000806 return router;
Marek Lindner01df2b62011-05-05 14:14:46 +0200807err_unlock:
808 rcu_read_unlock();
809err:
810 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200811 batadv_neigh_node_free_ref(router);
Marek Lindner01df2b62011-05-05 14:14:46 +0200812 return NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000813}
814
Sven Eckelmann63b01032012-05-16 20:23:13 +0200815static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000816{
817 struct ethhdr *ethhdr;
818
819 /* drop packet if it has not necessary minimum size */
820 if (unlikely(!pskb_may_pull(skb, hdr_size)))
821 return -1;
822
823 ethhdr = (struct ethhdr *)skb_mac_header(skb);
824
825 /* packet with unicast indication but broadcast recipient */
826 if (is_broadcast_ether_addr(ethhdr->h_dest))
827 return -1;
828
829 /* packet with broadcast sender address */
830 if (is_broadcast_ether_addr(ethhdr->h_source))
831 return -1;
832
833 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200834 if (!batadv_is_my_mac(ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000835 return -1;
836
837 return 0;
838}
839
Sven Eckelmann63b01032012-05-16 20:23:13 +0200840static int batadv_route_unicast_packet(struct sk_buff *skb,
841 struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000842{
843 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindner44524fc2011-02-10 14:33:53 +0000844 struct orig_node *orig_node = NULL;
845 struct neigh_node *neigh_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000846 struct unicast_packet *unicast_packet;
847 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
Marek Lindner44524fc2011-02-10 14:33:53 +0000848 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000849 struct sk_buff *new_skb;
850
851 unicast_packet = (struct unicast_packet *)skb->data;
852
853 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100854 if (unicast_packet->header.ttl < 2) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100855 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
856 ethhdr->h_source, unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +0000857 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000858 }
859
860 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200861 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
Marek Lindner7aadf882011-02-18 12:28:09 +0000862
Marek Lindner44524fc2011-02-10 14:33:53 +0000863 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +0200864 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000865
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000866 /* find_router() increases neigh_nodes refcount if found. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200867 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000868
Marek Lindnerd0072602011-01-19 20:01:44 +0000869 if (!neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000870 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000871
872 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100873 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000874 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000875
876 unicast_packet = (struct unicast_packet *)skb->data;
877
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200878 if (unicast_packet->header.packet_type == BATADV_UNICAST &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000879 atomic_read(&bat_priv->fragmentation) &&
Marek Lindnerd0072602011-01-19 20:01:44 +0000880 skb->len > neigh_node->if_incoming->net_dev->mtu) {
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200881 ret = batadv_frag_send_skb(skb, bat_priv,
882 neigh_node->if_incoming,
883 neigh_node->addr);
Marek Lindnerd0072602011-01-19 20:01:44 +0000884 goto out;
885 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000886
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200887 if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
Sven Eckelmannf0530ee2012-05-12 13:48:57 +0200888 batadv_frag_can_reassemble(skb,
889 neigh_node->if_incoming->net_dev->mtu)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000890
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200891 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000892
893 if (ret == NET_RX_DROP)
Marek Lindner44524fc2011-02-10 14:33:53 +0000894 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000895
896 /* packet was buffered for late merge */
Marek Lindner44524fc2011-02-10 14:33:53 +0000897 if (!new_skb) {
898 ret = NET_RX_SUCCESS;
899 goto out;
900 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000901
902 skb = new_skb;
903 unicast_packet = (struct unicast_packet *)skb->data;
904 }
905
906 /* decrement ttl */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100907 unicast_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000908
Martin Hundebøllf8214862012-04-20 17:02:45 +0200909 /* Update stats counter */
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200910 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
911 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200912 skb->len + ETH_HLEN);
913
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000914 /* route it */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200915 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000916 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000917
Marek Lindner44524fc2011-02-10 14:33:53 +0000918out:
919 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200920 batadv_neigh_node_free_ref(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000921 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200922 batadv_orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000923 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000924}
925
Sven Eckelmann63b01032012-05-16 20:23:13 +0200926static int batadv_check_unicast_ttvn(struct bat_priv *bat_priv,
927 struct sk_buff *skb) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200928 uint8_t curr_ttvn;
929 struct orig_node *orig_node;
930 struct ethhdr *ethhdr;
931 struct hard_iface *primary_if;
932 struct unicast_packet *unicast_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200933 bool tt_poss_change;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200934 int is_old_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200935
936 /* I could need to modify it */
937 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
938 return 0;
939
940 unicast_packet = (struct unicast_packet *)skb->data;
941
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200942 if (batadv_is_my_mac(unicast_packet->dest)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200943 tt_poss_change = bat_priv->tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200944 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200945 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200946 orig_node = batadv_orig_hash_find(bat_priv,
947 unicast_packet->dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200948
949 if (!orig_node)
950 return 0;
951
952 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200953 tt_poss_change = orig_node->tt_poss_change;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200954 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200955 }
956
957 /* Check whether I have to reroute the packet */
Sven Eckelmann3e348192012-05-16 20:23:22 +0200958 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
959 if (is_old_ttvn || tt_poss_change) {
Antonio Quartulli8710e262012-03-16 11:52:31 +0100960 /* check if there is enough data before accessing it */
961 if (pskb_may_pull(skb, sizeof(struct unicast_packet) +
962 ETH_HLEN) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200963 return 0;
964
965 ethhdr = (struct ethhdr *)(skb->data +
966 sizeof(struct unicast_packet));
Antonio Quartulli3275e7c2012-03-16 18:03:28 +0100967
968 /* we don't have an updated route for this client, so we should
969 * not try to reroute the packet!!
970 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200971 if (batadv_tt_global_client_is_roaming(bat_priv,
972 ethhdr->h_dest))
Antonio Quartulli3275e7c2012-03-16 18:03:28 +0100973 return 1;
974
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200975 orig_node = batadv_transtable_search(bat_priv, NULL,
976 ethhdr->h_dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200977
978 if (!orig_node) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200979 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200980 return 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200981 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200982 if (!primary_if)
983 return 0;
984 memcpy(unicast_packet->dest,
985 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200986 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200987 } else {
988 memcpy(unicast_packet->dest, orig_node->orig,
989 ETH_ALEN);
990 curr_ttvn = (uint8_t)
991 atomic_read(&orig_node->last_ttvn);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200992 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200993 }
994
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200995 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200996 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
997 unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
998 unicast_packet->dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200999
1000 unicast_packet->ttvn = curr_ttvn;
1001 }
1002 return 1;
1003}
1004
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001005int batadv_recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001006{
Antonio Quartullia73105b2011-04-27 14:27:44 +02001007 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001008 struct unicast_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001009 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001010
Sven Eckelmann63b01032012-05-16 20:23:13 +02001011 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001012 return NET_RX_DROP;
1013
Sven Eckelmann63b01032012-05-16 20:23:13 +02001014 if (!batadv_check_unicast_ttvn(bat_priv, skb))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001015 return NET_RX_DROP;
1016
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017 unicast_packet = (struct unicast_packet *)skb->data;
1018
1019 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001020 if (batadv_is_my_mac(unicast_packet->dest)) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001021 batadv_interface_rx(recv_if->soft_iface, skb, recv_if,
1022 hdr_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001023 return NET_RX_SUCCESS;
1024 }
1025
Sven Eckelmann63b01032012-05-16 20:23:13 +02001026 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001027}
1028
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001029int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1030 struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001031{
1032 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1033 struct unicast_frag_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001034 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001035 struct sk_buff *new_skb = NULL;
1036 int ret;
1037
Sven Eckelmann63b01032012-05-16 20:23:13 +02001038 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001039 return NET_RX_DROP;
1040
Sven Eckelmann63b01032012-05-16 20:23:13 +02001041 if (!batadv_check_unicast_ttvn(bat_priv, skb))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001042 return NET_RX_DROP;
1043
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001044 unicast_packet = (struct unicast_frag_packet *)skb->data;
1045
1046 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001047 if (batadv_is_my_mac(unicast_packet->dest)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001048
Sven Eckelmann88ed1e772012-05-12 02:09:40 +02001049 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001050
1051 if (ret == NET_RX_DROP)
1052 return NET_RX_DROP;
1053
1054 /* packet was buffered for late merge */
1055 if (!new_skb)
1056 return NET_RX_SUCCESS;
1057
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001058 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1059 sizeof(struct unicast_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001060 return NET_RX_SUCCESS;
1061 }
1062
Sven Eckelmann63b01032012-05-16 20:23:13 +02001063 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001064}
1065
1066
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001067int batadv_recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001068{
1069 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001070 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001071 struct bcast_packet *bcast_packet;
1072 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001073 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001074 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001075 int32_t seq_diff;
1076
1077 /* drop packet if it has not necessary minimum size */
1078 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001079 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001080
1081 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1082
1083 /* packet with broadcast indication but unicast recipient */
1084 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001085 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001086
1087 /* packet with broadcast sender address */
1088 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001089 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001090
1091 /* ignore broadcasts sent by myself */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001092 if (batadv_is_my_mac(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001093 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001094
1095 bcast_packet = (struct bcast_packet *)skb->data;
1096
1097 /* ignore broadcasts originated by myself */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001098 if (batadv_is_my_mac(bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001099 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001100
Sven Eckelmann76543d12011-11-20 15:47:38 +01001101 if (bcast_packet->header.ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001102 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001103
Sven Eckelmannda641192012-05-12 13:48:56 +02001104 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001105
1106 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001107 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001108
Marek Lindnerf3e00082011-01-25 21:52:11 +00001109 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001110
1111 /* check whether the packet is a duplicate */
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001112 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1113 ntohl(bcast_packet->seqno)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001114 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001115
1116 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1117
1118 /* check whether the packet is old and the host just restarted. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001119 if (batadv_window_protected(bat_priv, seq_diff,
1120 &orig_node->bcast_seqno_reset))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001121 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001122
1123 /* mark broadcast in flood history, update window position
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001124 * if required.
1125 */
Sven Eckelmann0f5f93222012-05-12 02:09:25 +02001126 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001127 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1128
Marek Lindnerf3e00082011-01-25 21:52:11 +00001129 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001130
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001131 /* check whether this has been sent by another originator before */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001132 if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001133 goto out;
1134
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001135 /* rebroadcast packet */
Sven Eckelmann9455e342012-05-12 02:09:37 +02001136 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001137
Simon Wunderlich23721382012-01-22 20:00:19 +01001138 /* don't hand the broadcast up if it is from an originator
1139 * from the same backbone.
1140 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001141 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
Simon Wunderlich23721382012-01-22 20:00:19 +01001142 goto out;
1143
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001144 /* broadcast for me */
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001145 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001146 ret = NET_RX_SUCCESS;
1147 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001148
Marek Lindnerf3e00082011-01-25 21:52:11 +00001149spin_unlock:
1150 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001151out:
1152 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001153 batadv_orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001154 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001155}
1156
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001157int batadv_recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001158{
1159 struct vis_packet *vis_packet;
1160 struct ethhdr *ethhdr;
1161 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann704509b2011-05-14 23:14:54 +02001162 int hdr_size = sizeof(*vis_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001163
1164 /* keep skb linear */
1165 if (skb_linearize(skb) < 0)
1166 return NET_RX_DROP;
1167
1168 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1169 return NET_RX_DROP;
1170
1171 vis_packet = (struct vis_packet *)skb->data;
1172 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1173
1174 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001175 if (!batadv_is_my_mac(ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001176 return NET_RX_DROP;
1177
1178 /* ignore own packets */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001179 if (batadv_is_my_mac(vis_packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001180 return NET_RX_DROP;
1181
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001182 if (batadv_is_my_mac(vis_packet->sender_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001183 return NET_RX_DROP;
1184
1185 switch (vis_packet->vis_type) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001186 case BATADV_VIS_TYPE_SERVER_SYNC:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001187 batadv_receive_server_sync_packet(bat_priv, vis_packet,
1188 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001189 break;
1190
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001191 case BATADV_VIS_TYPE_CLIENT_UPDATE:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001192 batadv_receive_client_update_packet(bat_priv, vis_packet,
1193 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001194 break;
1195
1196 default: /* ignore unknown packet */
1197 break;
1198 }
1199
1200 /* We take a copy of the data in the packet, so we should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001201 * always free the skbuf.
1202 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001203 return NET_RX_DROP;
1204}