blob: e1579076110578f1edbc1c6ed95a2a36c712974a [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 Eckelmann5bf74e92012-06-05 22:31:28 +020038 struct batadv_hashtable *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 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,
Sven Eckelmann96412692012-06-05 22:31:30 +0200220 const struct batadv_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 Eckelmann96412692012-06-05 22:31:30 +0200287 struct batadv_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
Sven Eckelmann96412692012-06-05 22:31:30 +0200290 icmp_packet = (struct batadv_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
Sven Eckelmann96412692012-06-05 22:31:30 +0200316 icmp_packet = (struct batadv_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 Eckelmann96412692012-06-05 22:31:30 +0200342 struct batadv_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
Sven Eckelmann96412692012-06-05 22:31:30 +0200345 icmp_packet = (struct batadv_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
Sven Eckelmann96412692012-06-05 22:31:30 +0200371 icmp_packet = (struct batadv_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);
Sven Eckelmann96412692012-06-05 22:31:30 +0200395 struct batadv_icmp_packet_rr *icmp_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396 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 Eckelmann96412692012-06-05 22:31:30 +0200399 int hdr_size = sizeof(struct batadv_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 Eckelmann96412692012-06-05 22:31:30 +0200403 if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
404 hdr_size = sizeof(struct batadv_icmp_packet_rr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405
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
Sven Eckelmann96412692012-06-05 22:31:30 +0200424 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000425
426 /* add record route information if not full */
Sven Eckelmann96412692012-06-05 22:31:30 +0200427 if ((hdr_size == sizeof(struct batadv_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
Sven Eckelmann96412692012-06-05 22:31:30 +0200455 icmp_packet = (struct batadv_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);
Sven Eckelmann96412692012-06-05 22:31:30 +0200583 struct batadv_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;
Sven Eckelmann96412692012-06-05 22:31:30 +0200587 size_t packet_size;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200588
589 /* drop packet if it has not necessary minimum size */
Sven Eckelmann96412692012-06-05 22:31:30 +0200590 if (unlikely(!pskb_may_pull(skb,
591 sizeof(struct batadv_tt_query_packet))))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200592 goto out;
593
594 /* I could need to modify it */
Sven Eckelmann96412692012-06-05 22:31:30 +0200595 if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200596 goto out;
597
598 ethhdr = (struct ethhdr *)skb_mac_header(skb);
599
600 /* packet with unicast indication but broadcast recipient */
601 if (is_broadcast_ether_addr(ethhdr->h_dest))
602 goto out;
603
604 /* packet with broadcast sender address */
605 if (is_broadcast_ether_addr(ethhdr->h_source))
606 goto out;
607
Sven Eckelmann96412692012-06-05 22:31:30 +0200608 tt_query = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200609
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200610 switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200611 case BATADV_TT_REQUEST:
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200612 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200613
Antonio Quartullia73105b2011-04-27 14:27:44 +0200614 /* If we cannot provide an answer the tt_request is
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200615 * forwarded
616 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200617 if (!batadv_send_tt_response(bat_priv, tt_query)) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200618 if (tt_query->flags & BATADV_TT_FULL_TABLE)
619 tt_flag = 'F';
620 else
621 tt_flag = '.';
622
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200623 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200624 "Routing TT_REQUEST to %pM [%c]\n",
625 tt_query->dst,
626 tt_flag);
Sven Eckelmann63b01032012-05-16 20:23:13 +0200627 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200628 }
629 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200630 case BATADV_TT_RESPONSE:
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200631 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200632
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200633 if (batadv_is_my_mac(tt_query->dst)) {
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200634 /* packet needs to be linearized to access the TT
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200635 * changes
636 */
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200637 if (skb_linearize(skb) < 0)
638 goto out;
Antonio Quartullid2b6cc82012-06-14 22:21:28 +0200639 /* skb_linearize() possibly changed skb->data */
Sven Eckelmann96412692012-06-05 22:31:30 +0200640 tt_query = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200641
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200642 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200643
644 /* Ensure we have all the claimed data */
Sven Eckelmann96412692012-06-05 22:31:30 +0200645 packet_size = sizeof(struct batadv_tt_query_packet);
646 packet_size += tt_size;
647 if (unlikely(skb_headlen(skb) < packet_size))
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200648 goto out;
649
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200650 batadv_handle_tt_response(bat_priv, tt_query);
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200651 } else {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200652 if (tt_query->flags & BATADV_TT_FULL_TABLE)
653 tt_flag = 'F';
654 else
655 tt_flag = '.';
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200656 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200657 "Routing TT_RESPONSE to %pM [%c]\n",
658 tt_query->dst,
659 tt_flag);
Sven Eckelmann63b01032012-05-16 20:23:13 +0200660 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200661 }
662 break;
663 }
664
665out:
666 /* returning NET_RX_DROP will make the caller function kfree the skb */
667 return NET_RX_DROP;
668}
669
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200670int batadv_recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200671{
672 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +0200673 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200674 struct orig_node *orig_node;
675 struct ethhdr *ethhdr;
676
677 /* drop packet if it has not necessary minimum size */
Sven Eckelmann96412692012-06-05 22:31:30 +0200678 if (unlikely(!pskb_may_pull(skb,
679 sizeof(struct batadv_roam_adv_packet))))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200680 goto out;
681
682 ethhdr = (struct ethhdr *)skb_mac_header(skb);
683
684 /* packet with unicast indication but broadcast recipient */
685 if (is_broadcast_ether_addr(ethhdr->h_dest))
686 goto out;
687
688 /* packet with broadcast sender address */
689 if (is_broadcast_ether_addr(ethhdr->h_source))
690 goto out;
691
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200692 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200693
Sven Eckelmann96412692012-06-05 22:31:30 +0200694 roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200695
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200696 if (!batadv_is_my_mac(roam_adv_packet->dst))
Sven Eckelmann63b01032012-05-16 20:23:13 +0200697 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200698
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100699 /* check if it is a backbone gateway. we don't accept
700 * roaming advertisement from it, as it has the same
701 * entries as we have.
702 */
Sven Eckelmann08adf152012-05-12 13:38:47 +0200703 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100704 goto out;
705
Sven Eckelmannda641192012-05-12 13:48:56 +0200706 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200707 if (!orig_node)
708 goto out;
709
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200710 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200711 "Received ROAMING_ADV from %pM (client %pM)\n",
712 roam_adv_packet->src, roam_adv_packet->client);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200713
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200714 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200715 BATADV_TT_CLIENT_ROAM,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200716 atomic_read(&orig_node->last_ttvn) + 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200717
718 /* Roaming phase starts: I have new information but the ttvn has not
719 * been incremented yet. This flag will make me check all the incoming
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200720 * packets for the correct destination.
721 */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200722 bat_priv->tt_poss_change = true;
723
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200724 batadv_orig_node_free_ref(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200725out:
726 /* returning NET_RX_DROP will make the caller function kfree the skb */
727 return NET_RX_DROP;
728}
729
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000730/* find a suitable router for this originator, and use
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000731 * bonding if possible. increases the found neighbors
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200732 * refcount.
733 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200734struct neigh_node *batadv_find_router(struct bat_priv *bat_priv,
735 struct orig_node *orig_node,
736 const struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000737{
738 struct orig_node *primary_orig_node;
739 struct orig_node *router_orig;
Linus Lüssing55158622011-03-14 22:43:27 +0000740 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000741 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
742 int bonding_enabled;
Sven Eckelmannda641192012-05-12 13:48:56 +0200743 uint8_t *primary_addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744
745 if (!orig_node)
746 return NULL;
747
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200748 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000749 if (!router)
Marek Lindner01df2b62011-05-05 14:14:46 +0200750 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000751
752 /* without bonding, the first node should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200753 * always choose the default router.
754 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755 bonding_enabled = atomic_read(&bat_priv->bonding);
756
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000757 rcu_read_lock();
758 /* select default router to output */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000759 router_orig = router->orig_node;
Marek Lindner01df2b62011-05-05 14:14:46 +0200760 if (!router_orig)
761 goto err_unlock;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000762
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000763 if ((!recv_if) && (!bonding_enabled))
764 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000765
Sven Eckelmannda641192012-05-12 13:48:56 +0200766 primary_addr = router_orig->primary_addr;
767
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000768 /* if we have something in the primary_addr, we can search
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200769 * for a potential bonding candidate.
770 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200771 if (batadv_compare_eth(primary_addr, zero_mac))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000772 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000773
774 /* find the orig_node which has the primary interface. might
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200775 * even be the same as our router_orig in many cases
776 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200777 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778 primary_orig_node = router_orig;
779 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200780 primary_orig_node = batadv_orig_hash_find(bat_priv,
781 primary_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000782 if (!primary_orig_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000783 goto return_router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000784
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200785 batadv_orig_node_free_ref(primary_orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000786 }
787
788 /* with less than 2 candidates, we can't do any
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200789 * bonding and prefer the original router.
790 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000791 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
792 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000793
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794 /* all nodes between should choose a candidate which
795 * is is not on the interface where the packet came
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200796 * in.
797 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200798 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000799
Linus Lüssing55158622011-03-14 22:43:27 +0000800 if (bonding_enabled)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200801 router = batadv_find_bond_router(primary_orig_node, recv_if);
Linus Lüssing55158622011-03-14 22:43:27 +0000802 else
Sven Eckelmann63b01032012-05-16 20:23:13 +0200803 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000804
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000805return_router:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200806 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
Antonio Quartullie2cbc112011-05-08 20:52:57 +0200807 goto err_unlock;
808
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000809 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000810 return router;
Marek Lindner01df2b62011-05-05 14:14:46 +0200811err_unlock:
812 rcu_read_unlock();
813err:
814 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200815 batadv_neigh_node_free_ref(router);
Marek Lindner01df2b62011-05-05 14:14:46 +0200816 return NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000817}
818
Sven Eckelmann63b01032012-05-16 20:23:13 +0200819static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000820{
821 struct ethhdr *ethhdr;
822
823 /* drop packet if it has not necessary minimum size */
824 if (unlikely(!pskb_may_pull(skb, hdr_size)))
825 return -1;
826
827 ethhdr = (struct ethhdr *)skb_mac_header(skb);
828
829 /* packet with unicast indication but broadcast recipient */
830 if (is_broadcast_ether_addr(ethhdr->h_dest))
831 return -1;
832
833 /* packet with broadcast sender address */
834 if (is_broadcast_ether_addr(ethhdr->h_source))
835 return -1;
836
837 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200838 if (!batadv_is_my_mac(ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000839 return -1;
840
841 return 0;
842}
843
Sven Eckelmann63b01032012-05-16 20:23:13 +0200844static int batadv_route_unicast_packet(struct sk_buff *skb,
845 struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000846{
847 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindner44524fc2011-02-10 14:33:53 +0000848 struct orig_node *orig_node = NULL;
849 struct neigh_node *neigh_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200850 struct batadv_unicast_packet *unicast_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000851 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
Marek Lindner44524fc2011-02-10 14:33:53 +0000852 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000853 struct sk_buff *new_skb;
854
Sven Eckelmann96412692012-06-05 22:31:30 +0200855 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000856
857 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100858 if (unicast_packet->header.ttl < 2) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100859 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
860 ethhdr->h_source, unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +0000861 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000862 }
863
864 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200865 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
Marek Lindner7aadf882011-02-18 12:28:09 +0000866
Marek Lindner44524fc2011-02-10 14:33:53 +0000867 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +0200868 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000869
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000870 /* find_router() increases neigh_nodes refcount if found. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200871 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000872
Marek Lindnerd0072602011-01-19 20:01:44 +0000873 if (!neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000874 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000875
876 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100877 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000878 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000879
Sven Eckelmann96412692012-06-05 22:31:30 +0200880 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000881
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200882 if (unicast_packet->header.packet_type == BATADV_UNICAST &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000883 atomic_read(&bat_priv->fragmentation) &&
Marek Lindnerd0072602011-01-19 20:01:44 +0000884 skb->len > neigh_node->if_incoming->net_dev->mtu) {
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200885 ret = batadv_frag_send_skb(skb, bat_priv,
886 neigh_node->if_incoming,
887 neigh_node->addr);
Marek Lindnerd0072602011-01-19 20:01:44 +0000888 goto out;
889 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000890
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200891 if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
Sven Eckelmannf0530ee2012-05-12 13:48:57 +0200892 batadv_frag_can_reassemble(skb,
893 neigh_node->if_incoming->net_dev->mtu)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200895 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000896
897 if (ret == NET_RX_DROP)
Marek Lindner44524fc2011-02-10 14:33:53 +0000898 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000899
900 /* packet was buffered for late merge */
Marek Lindner44524fc2011-02-10 14:33:53 +0000901 if (!new_skb) {
902 ret = NET_RX_SUCCESS;
903 goto out;
904 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000905
906 skb = new_skb;
Sven Eckelmann96412692012-06-05 22:31:30 +0200907 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000908 }
909
910 /* decrement ttl */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100911 unicast_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000912
Martin Hundebøllf8214862012-04-20 17:02:45 +0200913 /* Update stats counter */
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200914 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
915 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200916 skb->len + ETH_HLEN);
917
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000918 /* route it */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200919 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000920 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000921
Marek Lindner44524fc2011-02-10 14:33:53 +0000922out:
923 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200924 batadv_neigh_node_free_ref(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000925 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200926 batadv_orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000927 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000928}
929
Sven Eckelmann63b01032012-05-16 20:23:13 +0200930static int batadv_check_unicast_ttvn(struct bat_priv *bat_priv,
931 struct sk_buff *skb) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200932 uint8_t curr_ttvn;
933 struct orig_node *orig_node;
934 struct ethhdr *ethhdr;
935 struct hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +0200936 struct batadv_unicast_packet *unicast_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200937 bool tt_poss_change;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200938 int is_old_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200939
940 /* I could need to modify it */
Sven Eckelmann96412692012-06-05 22:31:30 +0200941 if (skb_cow(skb, sizeof(struct batadv_unicast_packet)) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200942 return 0;
943
Sven Eckelmann96412692012-06-05 22:31:30 +0200944 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200945
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200946 if (batadv_is_my_mac(unicast_packet->dest)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200947 tt_poss_change = bat_priv->tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200948 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200949 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200950 orig_node = batadv_orig_hash_find(bat_priv,
951 unicast_packet->dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200952
953 if (!orig_node)
954 return 0;
955
956 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200957 tt_poss_change = orig_node->tt_poss_change;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200958 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200959 }
960
961 /* Check whether I have to reroute the packet */
Sven Eckelmann3e348192012-05-16 20:23:22 +0200962 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
963 if (is_old_ttvn || tt_poss_change) {
Antonio Quartulli8710e262012-03-16 11:52:31 +0100964 /* check if there is enough data before accessing it */
Sven Eckelmann96412692012-06-05 22:31:30 +0200965 if (pskb_may_pull(skb, sizeof(struct batadv_unicast_packet) +
Antonio Quartulli8710e262012-03-16 11:52:31 +0100966 ETH_HLEN) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200967 return 0;
968
969 ethhdr = (struct ethhdr *)(skb->data +
Sven Eckelmann96412692012-06-05 22:31:30 +0200970 sizeof(struct batadv_unicast_packet));
Antonio Quartulli3275e7c2012-03-16 18:03:28 +0100971
972 /* we don't have an updated route for this client, so we should
973 * not try to reroute the packet!!
974 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200975 if (batadv_tt_global_client_is_roaming(bat_priv,
976 ethhdr->h_dest))
Antonio Quartulli3275e7c2012-03-16 18:03:28 +0100977 return 1;
978
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200979 orig_node = batadv_transtable_search(bat_priv, NULL,
980 ethhdr->h_dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200981
982 if (!orig_node) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200983 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200984 return 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200985 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200986 if (!primary_if)
987 return 0;
988 memcpy(unicast_packet->dest,
989 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200990 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200991 } else {
992 memcpy(unicast_packet->dest, orig_node->orig,
993 ETH_ALEN);
994 curr_ttvn = (uint8_t)
995 atomic_read(&orig_node->last_ttvn);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200996 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200997 }
998
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200999 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001000 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
1001 unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
1002 unicast_packet->dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001003
1004 unicast_packet->ttvn = curr_ttvn;
1005 }
1006 return 1;
1007}
1008
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001009int batadv_recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001010{
Antonio Quartullia73105b2011-04-27 14:27:44 +02001011 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +02001012 struct batadv_unicast_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001013 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001014
Sven Eckelmann63b01032012-05-16 20:23:13 +02001015 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001016 return NET_RX_DROP;
1017
Sven Eckelmann63b01032012-05-16 20:23:13 +02001018 if (!batadv_check_unicast_ttvn(bat_priv, skb))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001019 return NET_RX_DROP;
1020
Sven Eckelmann96412692012-06-05 22:31:30 +02001021 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001022
1023 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001024 if (batadv_is_my_mac(unicast_packet->dest)) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001025 batadv_interface_rx(recv_if->soft_iface, skb, recv_if,
1026 hdr_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001027 return NET_RX_SUCCESS;
1028 }
1029
Sven Eckelmann63b01032012-05-16 20:23:13 +02001030 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001031}
1032
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001033int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1034 struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001035{
1036 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +02001037 struct batadv_unicast_frag_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001038 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001039 struct sk_buff *new_skb = NULL;
1040 int ret;
1041
Sven Eckelmann63b01032012-05-16 20:23:13 +02001042 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001043 return NET_RX_DROP;
1044
Sven Eckelmann63b01032012-05-16 20:23:13 +02001045 if (!batadv_check_unicast_ttvn(bat_priv, skb))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001046 return NET_RX_DROP;
1047
Sven Eckelmann96412692012-06-05 22:31:30 +02001048 unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001049
1050 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001051 if (batadv_is_my_mac(unicast_packet->dest)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001052
Sven Eckelmann88ed1e772012-05-12 02:09:40 +02001053 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001054
1055 if (ret == NET_RX_DROP)
1056 return NET_RX_DROP;
1057
1058 /* packet was buffered for late merge */
1059 if (!new_skb)
1060 return NET_RX_SUCCESS;
1061
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001062 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
Sven Eckelmann96412692012-06-05 22:31:30 +02001063 sizeof(struct batadv_unicast_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001064 return NET_RX_SUCCESS;
1065 }
1066
Sven Eckelmann63b01032012-05-16 20:23:13 +02001067 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001068}
1069
1070
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001071int batadv_recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001072{
1073 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001074 struct orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001075 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001076 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001077 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001078 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001079 int32_t seq_diff;
1080
1081 /* drop packet if it has not necessary minimum size */
1082 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001083 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001084
1085 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1086
1087 /* packet with broadcast indication but unicast recipient */
1088 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001089 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001090
1091 /* packet with broadcast sender address */
1092 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001093 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001094
1095 /* ignore broadcasts sent by myself */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001096 if (batadv_is_my_mac(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001097 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001098
Sven Eckelmann96412692012-06-05 22:31:30 +02001099 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001100
1101 /* ignore broadcasts originated by myself */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001102 if (batadv_is_my_mac(bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001103 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001104
Sven Eckelmann76543d12011-11-20 15:47:38 +01001105 if (bcast_packet->header.ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001106 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001107
Sven Eckelmannda641192012-05-12 13:48:56 +02001108 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001109
1110 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001111 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001112
Marek Lindnerf3e00082011-01-25 21:52:11 +00001113 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001114
1115 /* check whether the packet is a duplicate */
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001116 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1117 ntohl(bcast_packet->seqno)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001118 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001119
1120 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1121
1122 /* check whether the packet is old and the host just restarted. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001123 if (batadv_window_protected(bat_priv, seq_diff,
1124 &orig_node->bcast_seqno_reset))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001125 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001126
1127 /* mark broadcast in flood history, update window position
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001128 * if required.
1129 */
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001130 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001131 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1132
Marek Lindnerf3e00082011-01-25 21:52:11 +00001133 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001134
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001135 /* check whether this has been sent by another originator before */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001136 if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001137 goto out;
1138
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001139 /* rebroadcast packet */
Sven Eckelmann9455e342012-05-12 02:09:37 +02001140 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001141
Simon Wunderlich23721382012-01-22 20:00:19 +01001142 /* don't hand the broadcast up if it is from an originator
1143 * from the same backbone.
1144 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001145 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
Simon Wunderlich23721382012-01-22 20:00:19 +01001146 goto out;
1147
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001148 /* broadcast for me */
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001149 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001150 ret = NET_RX_SUCCESS;
1151 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001152
Marek Lindnerf3e00082011-01-25 21:52:11 +00001153spin_unlock:
1154 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001155out:
1156 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001157 batadv_orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001158 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001159}
1160
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001161int batadv_recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001162{
Sven Eckelmann96412692012-06-05 22:31:30 +02001163 struct batadv_vis_packet *vis_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001164 struct ethhdr *ethhdr;
1165 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann704509b2011-05-14 23:14:54 +02001166 int hdr_size = sizeof(*vis_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001167
1168 /* keep skb linear */
1169 if (skb_linearize(skb) < 0)
1170 return NET_RX_DROP;
1171
1172 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1173 return NET_RX_DROP;
1174
Sven Eckelmann96412692012-06-05 22:31:30 +02001175 vis_packet = (struct batadv_vis_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001176 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1177
1178 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001179 if (!batadv_is_my_mac(ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001180 return NET_RX_DROP;
1181
1182 /* ignore own packets */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001183 if (batadv_is_my_mac(vis_packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001184 return NET_RX_DROP;
1185
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001186 if (batadv_is_my_mac(vis_packet->sender_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001187 return NET_RX_DROP;
1188
1189 switch (vis_packet->vis_type) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001190 case BATADV_VIS_TYPE_SERVER_SYNC:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001191 batadv_receive_server_sync_packet(bat_priv, vis_packet,
1192 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001193 break;
1194
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001195 case BATADV_VIS_TYPE_CLIENT_UPDATE:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001196 batadv_receive_client_update_packet(bat_priv, vis_packet,
1197 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001198 break;
1199
1200 default: /* ignore unknown packet */
1201 break;
1202 }
1203
1204 /* We take a copy of the data in the packet, so we should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001205 * always free the skbuf.
1206 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001207 return NET_RX_DROP;
1208}