blob: 57ff8517821656f32417426cc1e391a19823e5da [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "routing.h"
22#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000023#include "soft-interface.h"
24#include "hard-interface.h"
25#include "icmp_socket.h"
26#include "translation-table.h"
27#include "originator.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000028#include "vis.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029#include "unicast.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010030#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +010032static int route_unicast_packet(struct sk_buff *skb,
33 struct hard_iface *recv_if);
34
Sven Eckelmann30d3c512012-05-12 02:09:36 +020035void batadv_slide_own_bcast_window(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036{
Marek Lindnere6c10f42011-02-18 12:33:20 +000037 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000039 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000040 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041 struct orig_node *orig_node;
42 unsigned long *word;
Antonio Quartullic90681b2011-10-05 17:05:25 +020043 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044 size_t word_index;
45
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046 for (i = 0; i < hash->size; i++) {
47 head = &hash->table[i];
48
Marek Lindnerfb778ea2011-01-19 20:01:40 +000049 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +000050 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +000051 spin_lock_bh(&orig_node->ogm_cnt_lock);
Marek Lindnere6c10f42011-02-18 12:33:20 +000052 word_index = hard_iface->if_num * NUM_WORDS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053 word = &(orig_node->bcast_own[word_index]);
54
Sven Eckelmann0f5f93222012-05-12 02:09:25 +020055 batadv_bit_get_packet(bat_priv, word, 1, 0);
Marek Lindnere6c10f42011-02-18 12:33:20 +000056 orig_node->bcast_own_sum[hard_iface->if_num] =
Sven Eckelmann0079d2c2012-02-04 17:34:52 +010057 bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
Marek Lindner2ae2daf2011-01-19 20:01:42 +000058 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +000060 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062}
63
Marek Lindnerfc957272011-07-30 12:04:12 +020064static void _update_route(struct bat_priv *bat_priv,
65 struct orig_node *orig_node,
66 struct neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000067{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000068 struct neigh_node *curr_router;
69
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020070 curr_router = batadv_orig_node_get_router(orig_node);
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000071
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000072 /* route deleted */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000073 if ((curr_router) && (!neigh_node)) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020074 batadv_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
75 orig_node->orig);
Sven Eckelmann08c36d32012-05-12 02:09:39 +020076 batadv_tt_global_del_orig(bat_priv, orig_node,
77 "Deleted route towards originator");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000078
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000079 /* route added */
80 } else if ((!curr_router) && (neigh_node)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020082 batadv_dbg(DBG_ROUTES, bat_priv,
83 "Adding route towards: %pM (via %pM)\n",
84 orig_node->orig, neigh_node->addr);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000085 /* route changed */
Sven Eckelmannbb899b82011-05-10 11:22:37 +020086 } else if (neigh_node && curr_router) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020087 batadv_dbg(DBG_ROUTES, bat_priv,
88 "Changing route towards: %pM (now via %pM - was via %pM)\n",
89 orig_node->orig, neigh_node->addr,
90 curr_router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000091 }
92
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000093 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020094 batadv_neigh_node_free_ref(curr_router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000095
96 /* increase refcount of new best neighbor */
Marek Lindner44524fc2011-02-10 14:33:53 +000097 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
98 neigh_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000099
100 spin_lock_bh(&orig_node->neigh_list_lock);
101 rcu_assign_pointer(orig_node->router, neigh_node);
102 spin_unlock_bh(&orig_node->neigh_list_lock);
103
104 /* decrease refcount of previous best neighbor */
105 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200106 batadv_neigh_node_free_ref(curr_router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000107}
108
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200109void batadv_update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
110 struct neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000112 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113
114 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000115 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200117 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000118
119 if (router != neigh_node)
Marek Lindnerfc957272011-07-30 12:04:12 +0200120 _update_route(bat_priv, orig_node, neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000121
122out:
123 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200124 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125}
126
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000127/* caller must hold the neigh_list_lock */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200128void batadv_bonding_candidate_del(struct orig_node *orig_node,
129 struct neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000130{
131 /* this neighbor is not part of our candidate list */
132 if (list_empty(&neigh_node->bonding_list))
133 goto out;
134
135 list_del_rcu(&neigh_node->bonding_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000136 INIT_LIST_HEAD(&neigh_node->bonding_list);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200137 batadv_neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000138 atomic_dec(&orig_node->bond_candidates);
139
140out:
141 return;
142}
143
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200144void batadv_bonding_candidate_add(struct orig_node *orig_node,
145 struct neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000146{
147 struct hlist_node *node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000148 struct neigh_node *tmp_neigh_node, *router = NULL;
149 uint8_t interference_candidate = 0;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000150
151 spin_lock_bh(&orig_node->neigh_list_lock);
152
153 /* only consider if it has the same primary address ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200154 if (!batadv_compare_eth(orig_node->orig,
155 neigh_node->orig_node->primary_addr))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000156 goto candidate_del;
157
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200158 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000159 if (!router)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000160 goto candidate_del;
161
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000162 /* ... and is good enough to be considered */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000163 if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000164 goto candidate_del;
165
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200166 /* check if we have another candidate with the same mac address or
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000167 * interface. If we do, we won't select this candidate because of
168 * possible interference.
169 */
170 hlist_for_each_entry_rcu(tmp_neigh_node, node,
171 &orig_node->neigh_list, list) {
172
173 if (tmp_neigh_node == neigh_node)
174 continue;
175
176 /* we only care if the other candidate is even
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200177 * considered as candidate.
178 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000179 if (list_empty(&tmp_neigh_node->bonding_list))
180 continue;
181
182 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200183 (batadv_compare_eth(neigh_node->addr,
184 tmp_neigh_node->addr))) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000185 interference_candidate = 1;
186 break;
187 }
188 }
189
190 /* don't care further if it is an interference candidate */
191 if (interference_candidate)
192 goto candidate_del;
193
194 /* this neighbor already is part of our candidate list */
195 if (!list_empty(&neigh_node->bonding_list))
196 goto out;
197
Marek Lindner44524fc2011-02-10 14:33:53 +0000198 if (!atomic_inc_not_zero(&neigh_node->refcount))
199 goto out;
200
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000201 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000202 atomic_inc(&orig_node->bond_candidates);
203 goto out;
204
205candidate_del:
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200206 batadv_bonding_candidate_del(orig_node, neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000207
208out:
209 spin_unlock_bh(&orig_node->neigh_list_lock);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000210
211 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200212 batadv_neigh_node_free_ref(router);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000213}
214
215/* copy primary address for bonding */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200216void
217batadv_bonding_save_primary(const struct orig_node *orig_node,
218 struct orig_node *orig_neigh_node,
219 const struct batman_ogm_packet *batman_ogm_packet)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000220{
Marek Lindnerb6da4bf2011-07-29 17:31:50 +0200221 if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000222 return;
223
224 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
225}
226
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227/* checks whether the host restarted and is in the protection time.
228 * returns:
229 * 0 if the packet is to be accepted
230 * 1 if the packet is to be ignored.
231 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200232int batadv_window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
233 unsigned long *last_reset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234{
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100235 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE) ||
236 (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200237 if (!batadv_has_timed_out(*last_reset, RESET_PROTECTION_MS))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000238 return 1;
Marek Lindner8c7bf242012-03-17 15:28:33 +0800239
240 *last_reset = jiffies;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200241 batadv_dbg(DBG_BATMAN, bat_priv,
242 "old packet received, start protection\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243 }
Marek Lindner8c7bf242012-03-17 15:28:33 +0800244
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245 return 0;
246}
247
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200248bool batadv_check_management_packet(struct sk_buff *skb,
249 struct hard_iface *hard_iface,
250 int header_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000252 struct ethhdr *ethhdr;
253
254 /* drop packet if it has not necessary minimum size */
Marek Lindnerc3e29312012-03-04 16:56:25 +0800255 if (unlikely(!pskb_may_pull(skb, header_len)))
256 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257
258 ethhdr = (struct ethhdr *)skb_mac_header(skb);
259
260 /* packet with broadcast indication but unicast recipient */
261 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800262 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263
264 /* packet with broadcast sender address */
265 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800266 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267
268 /* create a copy of the skb, if needed, to modify it. */
269 if (skb_cow(skb, 0) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800270 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000271
272 /* keep skb linear */
273 if (skb_linearize(skb) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800274 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000275
Marek Lindnerc3e29312012-03-04 16:56:25 +0800276 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277}
278
279static int recv_my_icmp_packet(struct bat_priv *bat_priv,
280 struct sk_buff *skb, size_t icmp_len)
281{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200282 struct hard_iface *primary_if = NULL;
Marek Lindner44524fc2011-02-10 14:33:53 +0000283 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000284 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285 struct icmp_packet_rr *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000286 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287
288 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289
290 /* add data to device queue */
291 if (icmp_packet->msg_type != ECHO_REQUEST) {
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200292 batadv_socket_receive_packet(icmp_packet, icmp_len);
Marek Lindner44524fc2011-02-10 14:33:53 +0000293 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294 }
295
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200296 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200297 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000298 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299
300 /* answer echo request (ping) */
301 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200302 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000303 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000304 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000305
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200306 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000307 if (!router)
308 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309
Marek Lindner44524fc2011-02-10 14:33:53 +0000310 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100311 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000312 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313
Marek Lindner44524fc2011-02-10 14:33:53 +0000314 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000315
Marek Lindner44524fc2011-02-10 14:33:53 +0000316 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200317 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Marek Lindner44524fc2011-02-10 14:33:53 +0000318 icmp_packet->msg_type = ECHO_REPLY;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100319 icmp_packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320
Sven Eckelmann9455e342012-05-12 02:09:37 +0200321 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000322 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323
Marek Lindner44524fc2011-02-10 14:33:53 +0000324out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200325 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200326 batadv_hardif_free_ref(primary_if);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000327 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200328 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000329 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200330 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331 return ret;
332}
333
334static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
Simon Wunderlich74ef1152010-12-29 16:15:19 +0000335 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200337 struct hard_iface *primary_if = NULL;
Marek Lindner44524fc2011-02-10 14:33:53 +0000338 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000339 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340 struct icmp_packet *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000341 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342
343 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344
345 /* send TTL exceeded if packet is an echo request (traceroute) */
346 if (icmp_packet->msg_type != ECHO_REQUEST) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100347 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
348 icmp_packet->orig, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000349 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350 }
351
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200352 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200353 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000354 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355
356 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200357 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000358 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000359 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000360
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200361 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000362 if (!router)
363 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364
Marek Lindner44524fc2011-02-10 14:33:53 +0000365 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100366 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000367 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368
Marek Lindner44524fc2011-02-10 14:33:53 +0000369 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
Marek Lindner44524fc2011-02-10 14:33:53 +0000371 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200372 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Marek Lindner44524fc2011-02-10 14:33:53 +0000373 icmp_packet->msg_type = TTL_EXCEEDED;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100374 icmp_packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375
Sven Eckelmann9455e342012-05-12 02:09:37 +0200376 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000377 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378
Marek Lindner44524fc2011-02-10 14:33:53 +0000379out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200380 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200381 batadv_hardif_free_ref(primary_if);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000382 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200383 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000384 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200385 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386 return ret;
387}
388
389
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200390int batadv_recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391{
392 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
393 struct icmp_packet_rr *icmp_packet;
394 struct ethhdr *ethhdr;
Marek Lindner44524fc2011-02-10 14:33:53 +0000395 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000396 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397 int hdr_size = sizeof(struct icmp_packet);
Marek Lindner44524fc2011-02-10 14:33:53 +0000398 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200400 /* we truncate all incoming icmp packets if they don't match our size */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401 if (skb->len >= sizeof(struct icmp_packet_rr))
402 hdr_size = sizeof(struct icmp_packet_rr);
403
404 /* drop packet if it has not necessary minimum size */
405 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindner44524fc2011-02-10 14:33:53 +0000406 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000407
408 ethhdr = (struct ethhdr *)skb_mac_header(skb);
409
410 /* packet with unicast indication but broadcast recipient */
411 if (is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000412 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413
414 /* packet with broadcast sender address */
415 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindner44524fc2011-02-10 14:33:53 +0000416 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000417
418 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200419 if (!batadv_is_my_mac(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000420 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421
422 icmp_packet = (struct icmp_packet_rr *)skb->data;
423
424 /* add record route information if not full */
425 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
426 (icmp_packet->rr_cur < BAT_RR_LEN)) {
427 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100428 ethhdr->h_dest, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429 icmp_packet->rr_cur++;
430 }
431
432 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200433 if (batadv_is_my_mac(icmp_packet->dst))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
435
436 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100437 if (icmp_packet->header.ttl < 2)
Simon Wunderlich74ef1152010-12-29 16:15:19 +0000438 return recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200441 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000442 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000443 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000444
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200445 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000446 if (!router)
447 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448
Marek Lindner44524fc2011-02-10 14:33:53 +0000449 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100450 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000451 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452
Marek Lindner44524fc2011-02-10 14:33:53 +0000453 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454
Marek Lindner44524fc2011-02-10 14:33:53 +0000455 /* decrement ttl */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100456 icmp_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457
Marek Lindner44524fc2011-02-10 14:33:53 +0000458 /* route it */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200459 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000460 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461
Marek Lindner44524fc2011-02-10 14:33:53 +0000462out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000463 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200464 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000465 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200466 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467 return ret;
468}
469
Linus Lüssing55158622011-03-14 22:43:27 +0000470/* In the bonding case, send the packets in a round
471 * robin fashion over the remaining interfaces.
472 *
473 * This method rotates the bonding list and increases the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200474 * returned router's refcount.
475 */
Linus Lüssing55158622011-03-14 22:43:27 +0000476static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200477 const struct hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000478{
479 struct neigh_node *tmp_neigh_node;
480 struct neigh_node *router = NULL, *first_candidate = NULL;
481
482 rcu_read_lock();
483 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
484 bonding_list) {
485 if (!first_candidate)
486 first_candidate = tmp_neigh_node;
487
488 /* recv_if == NULL on the first node. */
489 if (tmp_neigh_node->if_incoming == recv_if)
490 continue;
491
492 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
493 continue;
494
495 router = tmp_neigh_node;
496 break;
497 }
498
499 /* use the first candidate if nothing was found. */
500 if (!router && first_candidate &&
501 atomic_inc_not_zero(&first_candidate->refcount))
502 router = first_candidate;
503
504 if (!router)
505 goto out;
506
507 /* selected should point to the next element
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200508 * after the current router
509 */
Linus Lüssing55158622011-03-14 22:43:27 +0000510 spin_lock_bh(&primary_orig->neigh_list_lock);
511 /* this is a list_move(), which unfortunately
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200512 * does not exist as rcu version
513 */
Linus Lüssing55158622011-03-14 22:43:27 +0000514 list_del_rcu(&primary_orig->bond_list);
515 list_add_rcu(&primary_orig->bond_list,
516 &router->bonding_list);
517 spin_unlock_bh(&primary_orig->neigh_list_lock);
518
519out:
520 rcu_read_unlock();
521 return router;
522}
523
524/* Interface Alternating: Use the best of the
525 * remaining candidates which are not using
526 * this interface.
527 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200528 * Increases the returned router's refcount
529 */
Linus Lüssing55158622011-03-14 22:43:27 +0000530static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200531 const struct hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000532{
533 struct neigh_node *tmp_neigh_node;
534 struct neigh_node *router = NULL, *first_candidate = NULL;
535
536 rcu_read_lock();
537 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
538 bonding_list) {
539 if (!first_candidate)
540 first_candidate = tmp_neigh_node;
541
542 /* recv_if == NULL on the first node. */
543 if (tmp_neigh_node->if_incoming == recv_if)
544 continue;
545
546 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
547 continue;
548
549 /* if we don't have a router yet
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200550 * or this one is better, choose it.
551 */
Linus Lüssing55158622011-03-14 22:43:27 +0000552 if ((!router) ||
553 (tmp_neigh_node->tq_avg > router->tq_avg)) {
554 /* decrement refcount of
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200555 * previously selected router
556 */
Linus Lüssing55158622011-03-14 22:43:27 +0000557 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200558 batadv_neigh_node_free_ref(router);
Linus Lüssing55158622011-03-14 22:43:27 +0000559
560 router = tmp_neigh_node;
561 atomic_inc_not_zero(&router->refcount);
562 }
563
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200564 batadv_neigh_node_free_ref(tmp_neigh_node);
Linus Lüssing55158622011-03-14 22:43:27 +0000565 }
566
567 /* use the first candidate if nothing was found. */
568 if (!router && first_candidate &&
569 atomic_inc_not_zero(&first_candidate->refcount))
570 router = first_candidate;
571
572 rcu_read_unlock();
573 return router;
574}
575
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200576int batadv_recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200577{
578 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
579 struct tt_query_packet *tt_query;
Al Virof25bd582012-04-22 07:44:27 +0100580 uint16_t tt_size;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200581 struct ethhdr *ethhdr;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200582 char tt_flag;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200583
584 /* drop packet if it has not necessary minimum size */
585 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
586 goto out;
587
588 /* I could need to modify it */
589 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
590 goto out;
591
592 ethhdr = (struct ethhdr *)skb_mac_header(skb);
593
594 /* packet with unicast indication but broadcast recipient */
595 if (is_broadcast_ether_addr(ethhdr->h_dest))
596 goto out;
597
598 /* packet with broadcast sender address */
599 if (is_broadcast_ether_addr(ethhdr->h_source))
600 goto out;
601
602 tt_query = (struct tt_query_packet *)skb->data;
603
Antonio Quartullia73105b2011-04-27 14:27:44 +0200604 switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
605 case TT_REQUEST:
Martin Hundebøllf8214862012-04-20 17:02:45 +0200606 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_RX);
607
Antonio Quartullia73105b2011-04-27 14:27:44 +0200608 /* If we cannot provide an answer the tt_request is
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200609 * forwarded
610 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200611 if (!batadv_send_tt_response(bat_priv, tt_query)) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200612 tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.';
613 batadv_dbg(DBG_TT, bat_priv,
614 "Routing TT_REQUEST to %pM [%c]\n",
615 tt_query->dst,
616 tt_flag);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200617 return route_unicast_packet(skb, recv_if);
618 }
619 break;
620 case TT_RESPONSE:
Martin Hundebøllf8214862012-04-20 17:02:45 +0200621 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_RX);
622
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200623 if (batadv_is_my_mac(tt_query->dst)) {
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200624 /* packet needs to be linearized to access the TT
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200625 * changes
626 */
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200627 if (skb_linearize(skb) < 0)
628 goto out;
Antonio Quartullid2b6cc82012-06-14 22:21:28 +0200629 /* skb_linearize() possibly changed skb->data */
630 tt_query = (struct tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200631
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200632 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200633
634 /* Ensure we have all the claimed data */
635 if (unlikely(skb_headlen(skb) <
Al Virof25bd582012-04-22 07:44:27 +0100636 sizeof(struct tt_query_packet) + tt_size))
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200637 goto out;
638
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200639 batadv_handle_tt_response(bat_priv, tt_query);
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200640 } else {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200641 tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.';
642 batadv_dbg(DBG_TT, bat_priv,
643 "Routing TT_RESPONSE to %pM [%c]\n",
644 tt_query->dst,
645 tt_flag);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200646 return route_unicast_packet(skb, recv_if);
647 }
648 break;
649 }
650
651out:
652 /* returning NET_RX_DROP will make the caller function kfree the skb */
653 return NET_RX_DROP;
654}
655
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200656int batadv_recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200657{
658 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
659 struct roam_adv_packet *roam_adv_packet;
660 struct orig_node *orig_node;
661 struct ethhdr *ethhdr;
662
663 /* drop packet if it has not necessary minimum size */
664 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
665 goto out;
666
667 ethhdr = (struct ethhdr *)skb_mac_header(skb);
668
669 /* packet with unicast indication but broadcast recipient */
670 if (is_broadcast_ether_addr(ethhdr->h_dest))
671 goto out;
672
673 /* packet with broadcast sender address */
674 if (is_broadcast_ether_addr(ethhdr->h_source))
675 goto out;
676
Martin Hundebøllf8214862012-04-20 17:02:45 +0200677 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_RX);
678
Antonio Quartullicc47f662011-04-27 14:27:57 +0200679 roam_adv_packet = (struct roam_adv_packet *)skb->data;
680
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200681 if (!batadv_is_my_mac(roam_adv_packet->dst))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200682 return route_unicast_packet(skb, recv_if);
683
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100684 /* check if it is a backbone gateway. we don't accept
685 * roaming advertisement from it, as it has the same
686 * entries as we have.
687 */
Sven Eckelmann08adf152012-05-12 13:38:47 +0200688 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100689 goto out;
690
Sven Eckelmannda641192012-05-12 13:48:56 +0200691 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200692 if (!orig_node)
693 goto out;
694
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200695 batadv_dbg(DBG_TT, bat_priv,
696 "Received ROAMING_ADV from %pM (client %pM)\n",
697 roam_adv_packet->src, roam_adv_packet->client);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200698
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200699 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
700 atomic_read(&orig_node->last_ttvn) + 1, true,
701 false);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200702
703 /* Roaming phase starts: I have new information but the ttvn has not
704 * been incremented yet. This flag will make me check all the incoming
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200705 * packets for the correct destination.
706 */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200707 bat_priv->tt_poss_change = true;
708
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200709 batadv_orig_node_free_ref(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200710out:
711 /* returning NET_RX_DROP will make the caller function kfree the skb */
712 return NET_RX_DROP;
713}
714
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000715/* find a suitable router for this originator, and use
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000716 * bonding if possible. increases the found neighbors
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200717 * refcount.
718 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200719struct neigh_node *batadv_find_router(struct bat_priv *bat_priv,
720 struct orig_node *orig_node,
721 const struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000722{
723 struct orig_node *primary_orig_node;
724 struct orig_node *router_orig;
Linus Lüssing55158622011-03-14 22:43:27 +0000725 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000726 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
727 int bonding_enabled;
Sven Eckelmannda641192012-05-12 13:48:56 +0200728 uint8_t *primary_addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000729
730 if (!orig_node)
731 return NULL;
732
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200733 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000734 if (!router)
Marek Lindner01df2b62011-05-05 14:14:46 +0200735 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000736
737 /* without bonding, the first node should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200738 * always choose the default router.
739 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000740 bonding_enabled = atomic_read(&bat_priv->bonding);
741
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000742 rcu_read_lock();
743 /* select default router to output */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000744 router_orig = router->orig_node;
Marek Lindner01df2b62011-05-05 14:14:46 +0200745 if (!router_orig)
746 goto err_unlock;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000747
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000748 if ((!recv_if) && (!bonding_enabled))
749 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000750
Sven Eckelmannda641192012-05-12 13:48:56 +0200751 primary_addr = router_orig->primary_addr;
752
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000753 /* if we have something in the primary_addr, we can search
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200754 * for a potential bonding candidate.
755 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200756 if (batadv_compare_eth(primary_addr, zero_mac))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000757 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000758
759 /* find the orig_node which has the primary interface. might
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200760 * even be the same as our router_orig in many cases
761 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200762 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000763 primary_orig_node = router_orig;
764 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200765 primary_orig_node = batadv_orig_hash_find(bat_priv,
766 primary_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767 if (!primary_orig_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000768 goto return_router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000769
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200770 batadv_orig_node_free_ref(primary_orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000771 }
772
773 /* with less than 2 candidates, we can't do any
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200774 * bonding and prefer the original router.
775 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000776 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
777 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779 /* all nodes between should choose a candidate which
780 * is is not on the interface where the packet came
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200781 * in.
782 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200783 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000784
Linus Lüssing55158622011-03-14 22:43:27 +0000785 if (bonding_enabled)
786 router = find_bond_router(primary_orig_node, recv_if);
787 else
788 router = find_ifalter_router(primary_orig_node, recv_if);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000789
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000790return_router:
Antonio Quartullie2cbc112011-05-08 20:52:57 +0200791 if (router && router->if_incoming->if_status != IF_ACTIVE)
792 goto err_unlock;
793
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000794 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000795 return router;
Marek Lindner01df2b62011-05-05 14:14:46 +0200796err_unlock:
797 rcu_read_unlock();
798err:
799 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200800 batadv_neigh_node_free_ref(router);
Marek Lindner01df2b62011-05-05 14:14:46 +0200801 return NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000802}
803
804static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
805{
806 struct ethhdr *ethhdr;
807
808 /* drop packet if it has not necessary minimum size */
809 if (unlikely(!pskb_may_pull(skb, hdr_size)))
810 return -1;
811
812 ethhdr = (struct ethhdr *)skb_mac_header(skb);
813
814 /* packet with unicast indication but broadcast recipient */
815 if (is_broadcast_ether_addr(ethhdr->h_dest))
816 return -1;
817
818 /* packet with broadcast sender address */
819 if (is_broadcast_ether_addr(ethhdr->h_source))
820 return -1;
821
822 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200823 if (!batadv_is_my_mac(ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824 return -1;
825
826 return 0;
827}
828
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +0100829static int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000830{
831 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindner44524fc2011-02-10 14:33:53 +0000832 struct orig_node *orig_node = NULL;
833 struct neigh_node *neigh_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000834 struct unicast_packet *unicast_packet;
835 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
Marek Lindner44524fc2011-02-10 14:33:53 +0000836 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000837 struct sk_buff *new_skb;
838
839 unicast_packet = (struct unicast_packet *)skb->data;
840
841 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100842 if (unicast_packet->header.ttl < 2) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100843 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
844 ethhdr->h_source, unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +0000845 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000846 }
847
848 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200849 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
Marek Lindner7aadf882011-02-18 12:28:09 +0000850
Marek Lindner44524fc2011-02-10 14:33:53 +0000851 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +0200852 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000853
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000854 /* find_router() increases neigh_nodes refcount if found. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200855 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000856
Marek Lindnerd0072602011-01-19 20:01:44 +0000857 if (!neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000858 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000859
860 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100861 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000862 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863
864 unicast_packet = (struct unicast_packet *)skb->data;
865
Sven Eckelmann76543d12011-11-20 15:47:38 +0100866 if (unicast_packet->header.packet_type == BAT_UNICAST &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000867 atomic_read(&bat_priv->fragmentation) &&
Marek Lindnerd0072602011-01-19 20:01:44 +0000868 skb->len > neigh_node->if_incoming->net_dev->mtu) {
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200869 ret = batadv_frag_send_skb(skb, bat_priv,
870 neigh_node->if_incoming,
871 neigh_node->addr);
Marek Lindnerd0072602011-01-19 20:01:44 +0000872 goto out;
873 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000874
Sven Eckelmann76543d12011-11-20 15:47:38 +0100875 if (unicast_packet->header.packet_type == BAT_UNICAST_FRAG &&
Sven Eckelmannf0530ee2012-05-12 13:48:57 +0200876 batadv_frag_can_reassemble(skb,
877 neigh_node->if_incoming->net_dev->mtu)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000878
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200879 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000880
881 if (ret == NET_RX_DROP)
Marek Lindner44524fc2011-02-10 14:33:53 +0000882 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000883
884 /* packet was buffered for late merge */
Marek Lindner44524fc2011-02-10 14:33:53 +0000885 if (!new_skb) {
886 ret = NET_RX_SUCCESS;
887 goto out;
888 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000889
890 skb = new_skb;
891 unicast_packet = (struct unicast_packet *)skb->data;
892 }
893
894 /* decrement ttl */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100895 unicast_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000896
Martin Hundebøllf8214862012-04-20 17:02:45 +0200897 /* Update stats counter */
898 batadv_inc_counter(bat_priv, BAT_CNT_FORWARD);
899 batadv_add_counter(bat_priv, BAT_CNT_FORWARD_BYTES,
900 skb->len + ETH_HLEN);
901
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000902 /* route it */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200903 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000904 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000905
Marek Lindner44524fc2011-02-10 14:33:53 +0000906out:
907 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200908 batadv_neigh_node_free_ref(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000909 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200910 batadv_orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000911 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000912}
913
Antonio Quartullia73105b2011-04-27 14:27:44 +0200914static int check_unicast_ttvn(struct bat_priv *bat_priv,
915 struct sk_buff *skb) {
916 uint8_t curr_ttvn;
917 struct orig_node *orig_node;
918 struct ethhdr *ethhdr;
919 struct hard_iface *primary_if;
920 struct unicast_packet *unicast_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200921 bool tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200922
923 /* I could need to modify it */
924 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
925 return 0;
926
927 unicast_packet = (struct unicast_packet *)skb->data;
928
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200929 if (batadv_is_my_mac(unicast_packet->dest)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200930 tt_poss_change = bat_priv->tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200931 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200932 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200933 orig_node = batadv_orig_hash_find(bat_priv,
934 unicast_packet->dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200935
936 if (!orig_node)
937 return 0;
938
939 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200940 tt_poss_change = orig_node->tt_poss_change;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200941 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200942 }
943
944 /* Check whether I have to reroute the packet */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200945 if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
Antonio Quartulli8710e262012-03-16 11:52:31 +0100946 /* check if there is enough data before accessing it */
947 if (pskb_may_pull(skb, sizeof(struct unicast_packet) +
948 ETH_HLEN) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200949 return 0;
950
951 ethhdr = (struct ethhdr *)(skb->data +
952 sizeof(struct unicast_packet));
Antonio Quartulli3275e7c2012-03-16 18:03:28 +0100953
954 /* we don't have an updated route for this client, so we should
955 * not try to reroute the packet!!
956 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200957 if (batadv_tt_global_client_is_roaming(bat_priv,
958 ethhdr->h_dest))
Antonio Quartulli3275e7c2012-03-16 18:03:28 +0100959 return 1;
960
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200961 orig_node = batadv_transtable_search(bat_priv, NULL,
962 ethhdr->h_dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200963
964 if (!orig_node) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200965 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200966 return 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200967 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200968 if (!primary_if)
969 return 0;
970 memcpy(unicast_packet->dest,
971 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200972 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200973 } else {
974 memcpy(unicast_packet->dest, orig_node->orig,
975 ETH_ALEN);
976 curr_ttvn = (uint8_t)
977 atomic_read(&orig_node->last_ttvn);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200978 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200979 }
980
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200981 batadv_dbg(DBG_ROUTES, bat_priv,
982 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
983 unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
984 unicast_packet->dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200985
986 unicast_packet->ttvn = curr_ttvn;
987 }
988 return 1;
989}
990
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200991int batadv_recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000992{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200993 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000994 struct unicast_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200995 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000996
997 if (check_unicast_packet(skb, hdr_size) < 0)
998 return NET_RX_DROP;
999
Antonio Quartullia73105b2011-04-27 14:27:44 +02001000 if (!check_unicast_ttvn(bat_priv, skb))
1001 return NET_RX_DROP;
1002
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001003 unicast_packet = (struct unicast_packet *)skb->data;
1004
1005 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001006 if (batadv_is_my_mac(unicast_packet->dest)) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001007 batadv_interface_rx(recv_if->soft_iface, skb, recv_if,
1008 hdr_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001009 return NET_RX_SUCCESS;
1010 }
1011
Linus Lüssing7cefb142011-03-02 17:39:31 +00001012 return route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001013}
1014
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001015int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1016 struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017{
1018 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1019 struct unicast_frag_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001020 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001021 struct sk_buff *new_skb = NULL;
1022 int ret;
1023
1024 if (check_unicast_packet(skb, hdr_size) < 0)
1025 return NET_RX_DROP;
1026
Antonio Quartullia73105b2011-04-27 14:27:44 +02001027 if (!check_unicast_ttvn(bat_priv, skb))
1028 return NET_RX_DROP;
1029
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030 unicast_packet = (struct unicast_frag_packet *)skb->data;
1031
1032 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001033 if (batadv_is_my_mac(unicast_packet->dest)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001034
Sven Eckelmann88ed1e772012-05-12 02:09:40 +02001035 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001036
1037 if (ret == NET_RX_DROP)
1038 return NET_RX_DROP;
1039
1040 /* packet was buffered for late merge */
1041 if (!new_skb)
1042 return NET_RX_SUCCESS;
1043
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001044 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1045 sizeof(struct unicast_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001046 return NET_RX_SUCCESS;
1047 }
1048
Linus Lüssing7cefb142011-03-02 17:39:31 +00001049 return route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001050}
1051
1052
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001053int batadv_recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001054{
1055 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001056 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001057 struct bcast_packet *bcast_packet;
1058 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001059 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001060 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001061 int32_t seq_diff;
1062
1063 /* drop packet if it has not necessary minimum size */
1064 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001065 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001066
1067 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1068
1069 /* packet with broadcast indication but unicast recipient */
1070 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001071 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001072
1073 /* packet with broadcast sender address */
1074 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001075 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001076
1077 /* ignore broadcasts sent by myself */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001078 if (batadv_is_my_mac(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001079 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001080
1081 bcast_packet = (struct bcast_packet *)skb->data;
1082
1083 /* ignore broadcasts originated by myself */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001084 if (batadv_is_my_mac(bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001085 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001086
Sven Eckelmann76543d12011-11-20 15:47:38 +01001087 if (bcast_packet->header.ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001088 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001089
Sven Eckelmannda641192012-05-12 13:48:56 +02001090 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001091
1092 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001093 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001094
Marek Lindnerf3e00082011-01-25 21:52:11 +00001095 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001096
1097 /* check whether the packet is a duplicate */
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001098 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1099 ntohl(bcast_packet->seqno)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001100 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001101
1102 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1103
1104 /* check whether the packet is old and the host just restarted. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001105 if (batadv_window_protected(bat_priv, seq_diff,
1106 &orig_node->bcast_seqno_reset))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001107 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001108
1109 /* mark broadcast in flood history, update window position
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001110 * if required.
1111 */
Sven Eckelmann0f5f93222012-05-12 02:09:25 +02001112 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001113 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1114
Marek Lindnerf3e00082011-01-25 21:52:11 +00001115 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001116
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001117 /* check whether this has been sent by another originator before */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001118 if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001119 goto out;
1120
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001121 /* rebroadcast packet */
Sven Eckelmann9455e342012-05-12 02:09:37 +02001122 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001123
Simon Wunderlich23721382012-01-22 20:00:19 +01001124 /* don't hand the broadcast up if it is from an originator
1125 * from the same backbone.
1126 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001127 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
Simon Wunderlich23721382012-01-22 20:00:19 +01001128 goto out;
1129
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001130 /* broadcast for me */
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001131 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001132 ret = NET_RX_SUCCESS;
1133 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001134
Marek Lindnerf3e00082011-01-25 21:52:11 +00001135spin_unlock:
1136 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001137out:
1138 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001139 batadv_orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001140 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001141}
1142
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001143int batadv_recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001144{
1145 struct vis_packet *vis_packet;
1146 struct ethhdr *ethhdr;
1147 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann704509b2011-05-14 23:14:54 +02001148 int hdr_size = sizeof(*vis_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001149
1150 /* keep skb linear */
1151 if (skb_linearize(skb) < 0)
1152 return NET_RX_DROP;
1153
1154 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1155 return NET_RX_DROP;
1156
1157 vis_packet = (struct vis_packet *)skb->data;
1158 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1159
1160 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001161 if (!batadv_is_my_mac(ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001162 return NET_RX_DROP;
1163
1164 /* ignore own packets */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001165 if (batadv_is_my_mac(vis_packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001166 return NET_RX_DROP;
1167
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001168 if (batadv_is_my_mac(vis_packet->sender_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001169 return NET_RX_DROP;
1170
1171 switch (vis_packet->vis_type) {
1172 case VIS_TYPE_SERVER_SYNC:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001173 batadv_receive_server_sync_packet(bat_priv, vis_packet,
1174 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001175 break;
1176
1177 case VIS_TYPE_CLIENT_UPDATE:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001178 batadv_receive_client_update_packet(bat_priv, vis_packet,
1179 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001180 break;
1181
1182 default: /* ignore unknown packet */
1183 break;
1184 }
1185
1186 /* We take a copy of the data in the packet, so we should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001187 * always free the skbuf.
1188 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001189 return NET_RX_DROP;
1190}