blob: e7ee40d6d6099be0f1c83de92421615a875d14db [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "routing.h"
22#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000023#include "soft-interface.h"
24#include "hard-interface.h"
25#include "icmp_socket.h"
26#include "translation-table.h"
27#include "originator.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000028#include "vis.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029#include "unicast.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010030#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031
Sven Eckelmann63b01032012-05-16 20:23:13 +020032static int batadv_route_unicast_packet(struct sk_buff *skb,
33 struct hard_iface *recv_if);
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +010034
Sven Eckelmann30d3c512012-05-12 02:09:36 +020035void batadv_slide_own_bcast_window(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036{
Marek Lindnere6c10f42011-02-18 12:33:20 +000037 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000039 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000040 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041 struct orig_node *orig_node;
42 unsigned long *word;
Antonio Quartullic90681b2011-10-05 17:05:25 +020043 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044 size_t word_index;
45
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046 for (i = 0; i < hash->size; i++) {
47 head = &hash->table[i];
48
Marek Lindnerfb778ea2011-01-19 20:01:40 +000049 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +000050 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +000051 spin_lock_bh(&orig_node->ogm_cnt_lock);
Marek Lindnere6c10f42011-02-18 12:33:20 +000052 word_index = hard_iface->if_num * NUM_WORDS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053 word = &(orig_node->bcast_own[word_index]);
54
Sven Eckelmann0f5f9322012-05-12 02:09:25 +020055 batadv_bit_get_packet(bat_priv, word, 1, 0);
Marek Lindnere6c10f42011-02-18 12:33:20 +000056 orig_node->bcast_own_sum[hard_iface->if_num] =
Sven Eckelmann0079d2c2012-02-04 17:34:52 +010057 bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
Marek Lindner2ae2daf2011-01-19 20:01:42 +000058 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +000060 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062}
63
Sven Eckelmann63b01032012-05-16 20:23:13 +020064static void _batadv_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)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200120 _batadv_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
Sven Eckelmann63b01032012-05-16 20:23:13 +0200279static int batadv_recv_my_icmp_packet(struct bat_priv *bat_priv,
280 struct sk_buff *skb, size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281{
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
Sven Eckelmann63b01032012-05-16 20:23:13 +0200334static int batadv_recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
335 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 Eckelmann63b01032012-05-16 20:23:13 +0200434 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435
436 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100437 if (icmp_packet->header.ttl < 2)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200438 return batadv_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 */
Sven Eckelmann63b01032012-05-16 20:23:13 +0200476static struct neigh_node *
477batadv_find_bond_router(struct orig_node *primary_orig,
478 const struct hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000479{
480 struct neigh_node *tmp_neigh_node;
481 struct neigh_node *router = NULL, *first_candidate = NULL;
482
483 rcu_read_lock();
484 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
485 bonding_list) {
486 if (!first_candidate)
487 first_candidate = tmp_neigh_node;
488
489 /* recv_if == NULL on the first node. */
490 if (tmp_neigh_node->if_incoming == recv_if)
491 continue;
492
493 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
494 continue;
495
496 router = tmp_neigh_node;
497 break;
498 }
499
500 /* use the first candidate if nothing was found. */
501 if (!router && first_candidate &&
502 atomic_inc_not_zero(&first_candidate->refcount))
503 router = first_candidate;
504
505 if (!router)
506 goto out;
507
508 /* selected should point to the next element
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200509 * after the current router
510 */
Linus Lüssing55158622011-03-14 22:43:27 +0000511 spin_lock_bh(&primary_orig->neigh_list_lock);
512 /* this is a list_move(), which unfortunately
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200513 * does not exist as rcu version
514 */
Linus Lüssing55158622011-03-14 22:43:27 +0000515 list_del_rcu(&primary_orig->bond_list);
516 list_add_rcu(&primary_orig->bond_list,
517 &router->bonding_list);
518 spin_unlock_bh(&primary_orig->neigh_list_lock);
519
520out:
521 rcu_read_unlock();
522 return router;
523}
524
525/* Interface Alternating: Use the best of the
526 * remaining candidates which are not using
527 * this interface.
528 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200529 * Increases the returned router's refcount
530 */
Sven Eckelmann63b01032012-05-16 20:23:13 +0200531static struct neigh_node *
532batadv_find_ifalter_router(struct orig_node *primary_orig,
533 const struct hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000534{
535 struct neigh_node *tmp_neigh_node;
536 struct neigh_node *router = NULL, *first_candidate = NULL;
537
538 rcu_read_lock();
539 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
540 bonding_list) {
541 if (!first_candidate)
542 first_candidate = tmp_neigh_node;
543
544 /* recv_if == NULL on the first node. */
545 if (tmp_neigh_node->if_incoming == recv_if)
546 continue;
547
548 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
549 continue;
550
551 /* if we don't have a router yet
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200552 * or this one is better, choose it.
553 */
Linus Lüssing55158622011-03-14 22:43:27 +0000554 if ((!router) ||
555 (tmp_neigh_node->tq_avg > router->tq_avg)) {
556 /* decrement refcount of
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200557 * previously selected router
558 */
Linus Lüssing55158622011-03-14 22:43:27 +0000559 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200560 batadv_neigh_node_free_ref(router);
Linus Lüssing55158622011-03-14 22:43:27 +0000561
562 router = tmp_neigh_node;
563 atomic_inc_not_zero(&router->refcount);
564 }
565
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200566 batadv_neigh_node_free_ref(tmp_neigh_node);
Linus Lüssing55158622011-03-14 22:43:27 +0000567 }
568
569 /* use the first candidate if nothing was found. */
570 if (!router && first_candidate &&
571 atomic_inc_not_zero(&first_candidate->refcount))
572 router = first_candidate;
573
574 rcu_read_unlock();
575 return router;
576}
577
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200578int batadv_recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200579{
580 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
581 struct tt_query_packet *tt_query;
Al Virof25bd582012-04-22 07:44:27 +0100582 uint16_t tt_size;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200583 struct ethhdr *ethhdr;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200584 char tt_flag;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200585
586 /* drop packet if it has not necessary minimum size */
587 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
588 goto out;
589
590 /* I could need to modify it */
591 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
592 goto out;
593
594 ethhdr = (struct ethhdr *)skb_mac_header(skb);
595
596 /* packet with unicast indication but broadcast recipient */
597 if (is_broadcast_ether_addr(ethhdr->h_dest))
598 goto out;
599
600 /* packet with broadcast sender address */
601 if (is_broadcast_ether_addr(ethhdr->h_source))
602 goto out;
603
604 tt_query = (struct tt_query_packet *)skb->data;
605
Antonio Quartullia73105b2011-04-27 14:27:44 +0200606 switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
607 case TT_REQUEST:
Martin Hundebøllf8214862012-04-20 17:02:45 +0200608 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_RX);
609
Antonio Quartullia73105b2011-04-27 14:27:44 +0200610 /* If we cannot provide an answer the tt_request is
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200611 * forwarded
612 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200613 if (!batadv_send_tt_response(bat_priv, tt_query)) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200614 tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.';
615 batadv_dbg(DBG_TT, bat_priv,
616 "Routing TT_REQUEST to %pM [%c]\n",
617 tt_query->dst,
618 tt_flag);
Sven Eckelmann63b01032012-05-16 20:23:13 +0200619 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200620 }
621 break;
622 case TT_RESPONSE:
Martin Hundebøllf8214862012-04-20 17:02:45 +0200623 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_RX);
624
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200625 if (batadv_is_my_mac(tt_query->dst)) {
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200626 /* packet needs to be linearized to access the TT
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200627 * changes
628 */
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200629 if (skb_linearize(skb) < 0)
630 goto out;
Antonio Quartullid2b6cc82012-06-14 22:21:28 +0200631 /* skb_linearize() possibly changed skb->data */
632 tt_query = (struct tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200633
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200634 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200635
636 /* Ensure we have all the claimed data */
637 if (unlikely(skb_headlen(skb) <
Al Virof25bd582012-04-22 07:44:27 +0100638 sizeof(struct tt_query_packet) + tt_size))
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200639 goto out;
640
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200641 batadv_handle_tt_response(bat_priv, tt_query);
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200642 } else {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200643 tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.';
644 batadv_dbg(DBG_TT, bat_priv,
645 "Routing TT_RESPONSE to %pM [%c]\n",
646 tt_query->dst,
647 tt_flag);
Sven Eckelmann63b01032012-05-16 20:23:13 +0200648 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200649 }
650 break;
651 }
652
653out:
654 /* returning NET_RX_DROP will make the caller function kfree the skb */
655 return NET_RX_DROP;
656}
657
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200658int batadv_recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200659{
660 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
661 struct roam_adv_packet *roam_adv_packet;
662 struct orig_node *orig_node;
663 struct ethhdr *ethhdr;
664
665 /* drop packet if it has not necessary minimum size */
666 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
667 goto out;
668
669 ethhdr = (struct ethhdr *)skb_mac_header(skb);
670
671 /* packet with unicast indication but broadcast recipient */
672 if (is_broadcast_ether_addr(ethhdr->h_dest))
673 goto out;
674
675 /* packet with broadcast sender address */
676 if (is_broadcast_ether_addr(ethhdr->h_source))
677 goto out;
678
Martin Hundebøllf8214862012-04-20 17:02:45 +0200679 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_RX);
680
Antonio Quartullicc47f662011-04-27 14:27:57 +0200681 roam_adv_packet = (struct roam_adv_packet *)skb->data;
682
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200683 if (!batadv_is_my_mac(roam_adv_packet->dst))
Sven Eckelmann63b01032012-05-16 20:23:13 +0200684 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200685
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100686 /* check if it is a backbone gateway. we don't accept
687 * roaming advertisement from it, as it has the same
688 * entries as we have.
689 */
Sven Eckelmann08adf152012-05-12 13:38:47 +0200690 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100691 goto out;
692
Sven Eckelmannda641192012-05-12 13:48:56 +0200693 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200694 if (!orig_node)
695 goto out;
696
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200697 batadv_dbg(DBG_TT, bat_priv,
698 "Received ROAMING_ADV from %pM (client %pM)\n",
699 roam_adv_packet->src, roam_adv_packet->client);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200700
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200701 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
702 atomic_read(&orig_node->last_ttvn) + 1, true,
703 false);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200704
705 /* Roaming phase starts: I have new information but the ttvn has not
706 * been incremented yet. This flag will make me check all the incoming
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200707 * packets for the correct destination.
708 */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200709 bat_priv->tt_poss_change = true;
710
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200711 batadv_orig_node_free_ref(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200712out:
713 /* returning NET_RX_DROP will make the caller function kfree the skb */
714 return NET_RX_DROP;
715}
716
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000717/* find a suitable router for this originator, and use
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000718 * bonding if possible. increases the found neighbors
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200719 * refcount.
720 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200721struct neigh_node *batadv_find_router(struct bat_priv *bat_priv,
722 struct orig_node *orig_node,
723 const struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000724{
725 struct orig_node *primary_orig_node;
726 struct orig_node *router_orig;
Linus Lüssing55158622011-03-14 22:43:27 +0000727 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000728 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
729 int bonding_enabled;
Sven Eckelmannda641192012-05-12 13:48:56 +0200730 uint8_t *primary_addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000731
732 if (!orig_node)
733 return NULL;
734
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200735 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000736 if (!router)
Marek Lindner01df2b62011-05-05 14:14:46 +0200737 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000738
739 /* without bonding, the first node should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200740 * always choose the default router.
741 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000742 bonding_enabled = atomic_read(&bat_priv->bonding);
743
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000744 rcu_read_lock();
745 /* select default router to output */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000746 router_orig = router->orig_node;
Marek Lindner01df2b62011-05-05 14:14:46 +0200747 if (!router_orig)
748 goto err_unlock;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000749
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000750 if ((!recv_if) && (!bonding_enabled))
751 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752
Sven Eckelmannda641192012-05-12 13:48:56 +0200753 primary_addr = router_orig->primary_addr;
754
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755 /* if we have something in the primary_addr, we can search
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200756 * for a potential bonding candidate.
757 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200758 if (batadv_compare_eth(primary_addr, zero_mac))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000759 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000760
761 /* find the orig_node which has the primary interface. might
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200762 * even be the same as our router_orig in many cases
763 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200764 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000765 primary_orig_node = router_orig;
766 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200767 primary_orig_node = batadv_orig_hash_find(bat_priv,
768 primary_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000769 if (!primary_orig_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000770 goto return_router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000771
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200772 batadv_orig_node_free_ref(primary_orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000773 }
774
775 /* with less than 2 candidates, we can't do any
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200776 * bonding and prefer the original router.
777 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000778 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
779 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000781 /* all nodes between should choose a candidate which
782 * is is not on the interface where the packet came
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200783 * in.
784 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200785 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000786
Linus Lüssing55158622011-03-14 22:43:27 +0000787 if (bonding_enabled)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200788 router = batadv_find_bond_router(primary_orig_node, recv_if);
Linus Lüssing55158622011-03-14 22:43:27 +0000789 else
Sven Eckelmann63b01032012-05-16 20:23:13 +0200790 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000791
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000792return_router:
Antonio Quartullie2cbc112011-05-08 20:52:57 +0200793 if (router && router->if_incoming->if_status != IF_ACTIVE)
794 goto err_unlock;
795
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000796 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000797 return router;
Marek Lindner01df2b62011-05-05 14:14:46 +0200798err_unlock:
799 rcu_read_unlock();
800err:
801 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200802 batadv_neigh_node_free_ref(router);
Marek Lindner01df2b62011-05-05 14:14:46 +0200803 return NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000804}
805
Sven Eckelmann63b01032012-05-16 20:23:13 +0200806static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000807{
808 struct ethhdr *ethhdr;
809
810 /* drop packet if it has not necessary minimum size */
811 if (unlikely(!pskb_may_pull(skb, hdr_size)))
812 return -1;
813
814 ethhdr = (struct ethhdr *)skb_mac_header(skb);
815
816 /* packet with unicast indication but broadcast recipient */
817 if (is_broadcast_ether_addr(ethhdr->h_dest))
818 return -1;
819
820 /* packet with broadcast sender address */
821 if (is_broadcast_ether_addr(ethhdr->h_source))
822 return -1;
823
824 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200825 if (!batadv_is_my_mac(ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000826 return -1;
827
828 return 0;
829}
830
Sven Eckelmann63b01032012-05-16 20:23:13 +0200831static int batadv_route_unicast_packet(struct sk_buff *skb,
832 struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000833{
834 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindner44524fc2011-02-10 14:33:53 +0000835 struct orig_node *orig_node = NULL;
836 struct neigh_node *neigh_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000837 struct unicast_packet *unicast_packet;
838 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
Marek Lindner44524fc2011-02-10 14:33:53 +0000839 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000840 struct sk_buff *new_skb;
841
842 unicast_packet = (struct unicast_packet *)skb->data;
843
844 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100845 if (unicast_packet->header.ttl < 2) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100846 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
847 ethhdr->h_source, unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +0000848 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000849 }
850
851 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200852 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
Marek Lindner7aadf882011-02-18 12:28:09 +0000853
Marek Lindner44524fc2011-02-10 14:33:53 +0000854 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +0200855 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000856
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000857 /* find_router() increases neigh_nodes refcount if found. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200858 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000859
Marek Lindnerd0072602011-01-19 20:01:44 +0000860 if (!neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000861 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000862
863 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100864 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000865 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000866
867 unicast_packet = (struct unicast_packet *)skb->data;
868
Sven Eckelmann76543d12011-11-20 15:47:38 +0100869 if (unicast_packet->header.packet_type == BAT_UNICAST &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000870 atomic_read(&bat_priv->fragmentation) &&
Marek Lindnerd0072602011-01-19 20:01:44 +0000871 skb->len > neigh_node->if_incoming->net_dev->mtu) {
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200872 ret = batadv_frag_send_skb(skb, bat_priv,
873 neigh_node->if_incoming,
874 neigh_node->addr);
Marek Lindnerd0072602011-01-19 20:01:44 +0000875 goto out;
876 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000877
Sven Eckelmann76543d12011-11-20 15:47:38 +0100878 if (unicast_packet->header.packet_type == BAT_UNICAST_FRAG &&
Sven Eckelmannf0530ee2012-05-12 13:48:57 +0200879 batadv_frag_can_reassemble(skb,
880 neigh_node->if_incoming->net_dev->mtu)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000881
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200882 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000883
884 if (ret == NET_RX_DROP)
Marek Lindner44524fc2011-02-10 14:33:53 +0000885 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000886
887 /* packet was buffered for late merge */
Marek Lindner44524fc2011-02-10 14:33:53 +0000888 if (!new_skb) {
889 ret = NET_RX_SUCCESS;
890 goto out;
891 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000892
893 skb = new_skb;
894 unicast_packet = (struct unicast_packet *)skb->data;
895 }
896
897 /* decrement ttl */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100898 unicast_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000899
Martin Hundebøllf8214862012-04-20 17:02:45 +0200900 /* Update stats counter */
901 batadv_inc_counter(bat_priv, BAT_CNT_FORWARD);
902 batadv_add_counter(bat_priv, BAT_CNT_FORWARD_BYTES,
903 skb->len + ETH_HLEN);
904
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000905 /* route it */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200906 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000907 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000908
Marek Lindner44524fc2011-02-10 14:33:53 +0000909out:
910 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200911 batadv_neigh_node_free_ref(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000912 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200913 batadv_orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000914 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000915}
916
Sven Eckelmann63b01032012-05-16 20:23:13 +0200917static int batadv_check_unicast_ttvn(struct bat_priv *bat_priv,
918 struct sk_buff *skb) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200919 uint8_t curr_ttvn;
920 struct orig_node *orig_node;
921 struct ethhdr *ethhdr;
922 struct hard_iface *primary_if;
923 struct unicast_packet *unicast_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200924 bool tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200925
926 /* I could need to modify it */
927 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
928 return 0;
929
930 unicast_packet = (struct unicast_packet *)skb->data;
931
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200932 if (batadv_is_my_mac(unicast_packet->dest)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200933 tt_poss_change = bat_priv->tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200934 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200935 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200936 orig_node = batadv_orig_hash_find(bat_priv,
937 unicast_packet->dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200938
939 if (!orig_node)
940 return 0;
941
942 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200943 tt_poss_change = orig_node->tt_poss_change;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200944 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200945 }
946
947 /* Check whether I have to reroute the packet */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200948 if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
Antonio Quartulli8710e262012-03-16 11:52:31 +0100949 /* check if there is enough data before accessing it */
950 if (pskb_may_pull(skb, sizeof(struct unicast_packet) +
951 ETH_HLEN) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200952 return 0;
953
954 ethhdr = (struct ethhdr *)(skb->data +
955 sizeof(struct unicast_packet));
Antonio Quartulli3275e7c2012-03-16 18:03:28 +0100956
957 /* we don't have an updated route for this client, so we should
958 * not try to reroute the packet!!
959 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200960 if (batadv_tt_global_client_is_roaming(bat_priv,
961 ethhdr->h_dest))
Antonio Quartulli3275e7c2012-03-16 18:03:28 +0100962 return 1;
963
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200964 orig_node = batadv_transtable_search(bat_priv, NULL,
965 ethhdr->h_dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200966
967 if (!orig_node) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200968 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200969 return 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200970 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200971 if (!primary_if)
972 return 0;
973 memcpy(unicast_packet->dest,
974 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200975 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200976 } else {
977 memcpy(unicast_packet->dest, orig_node->orig,
978 ETH_ALEN);
979 curr_ttvn = (uint8_t)
980 atomic_read(&orig_node->last_ttvn);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200981 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200982 }
983
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200984 batadv_dbg(DBG_ROUTES, bat_priv,
985 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
986 unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
987 unicast_packet->dest);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200988
989 unicast_packet->ttvn = curr_ttvn;
990 }
991 return 1;
992}
993
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200994int batadv_recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000995{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200996 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000997 struct unicast_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200998 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000999
Sven Eckelmann63b01032012-05-16 20:23:13 +02001000 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001001 return NET_RX_DROP;
1002
Sven Eckelmann63b01032012-05-16 20:23:13 +02001003 if (!batadv_check_unicast_ttvn(bat_priv, skb))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001004 return NET_RX_DROP;
1005
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001006 unicast_packet = (struct unicast_packet *)skb->data;
1007
1008 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001009 if (batadv_is_my_mac(unicast_packet->dest)) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001010 batadv_interface_rx(recv_if->soft_iface, skb, recv_if,
1011 hdr_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001012 return NET_RX_SUCCESS;
1013 }
1014
Sven Eckelmann63b01032012-05-16 20:23:13 +02001015 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001016}
1017
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001018int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1019 struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001020{
1021 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1022 struct unicast_frag_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001023 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001024 struct sk_buff *new_skb = NULL;
1025 int ret;
1026
Sven Eckelmann63b01032012-05-16 20:23:13 +02001027 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001028 return NET_RX_DROP;
1029
Sven Eckelmann63b01032012-05-16 20:23:13 +02001030 if (!batadv_check_unicast_ttvn(bat_priv, skb))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001031 return NET_RX_DROP;
1032
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001033 unicast_packet = (struct unicast_frag_packet *)skb->data;
1034
1035 /* packet for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001036 if (batadv_is_my_mac(unicast_packet->dest)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001037
Sven Eckelmann88ed1e772012-05-12 02:09:40 +02001038 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001039
1040 if (ret == NET_RX_DROP)
1041 return NET_RX_DROP;
1042
1043 /* packet was buffered for late merge */
1044 if (!new_skb)
1045 return NET_RX_SUCCESS;
1046
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001047 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1048 sizeof(struct unicast_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001049 return NET_RX_SUCCESS;
1050 }
1051
Sven Eckelmann63b01032012-05-16 20:23:13 +02001052 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001053}
1054
1055
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001056int batadv_recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001057{
1058 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001059 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001060 struct bcast_packet *bcast_packet;
1061 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001062 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001063 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001064 int32_t seq_diff;
1065
1066 /* drop packet if it has not necessary minimum size */
1067 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001068 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001069
1070 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1071
1072 /* packet with broadcast indication but unicast recipient */
1073 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001074 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001075
1076 /* packet with broadcast sender address */
1077 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001078 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001079
1080 /* ignore broadcasts sent by myself */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001081 if (batadv_is_my_mac(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001082 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001083
1084 bcast_packet = (struct bcast_packet *)skb->data;
1085
1086 /* ignore broadcasts originated by myself */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001087 if (batadv_is_my_mac(bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001088 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001089
Sven Eckelmann76543d12011-11-20 15:47:38 +01001090 if (bcast_packet->header.ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001091 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001092
Sven Eckelmannda641192012-05-12 13:48:56 +02001093 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001094
1095 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001096 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001097
Marek Lindnerf3e00082011-01-25 21:52:11 +00001098 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001099
1100 /* check whether the packet is a duplicate */
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001101 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1102 ntohl(bcast_packet->seqno)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001103 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001104
1105 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1106
1107 /* check whether the packet is old and the host just restarted. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001108 if (batadv_window_protected(bat_priv, seq_diff,
1109 &orig_node->bcast_seqno_reset))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001110 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001111
1112 /* mark broadcast in flood history, update window position
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001113 * if required.
1114 */
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001115 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001116 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1117
Marek Lindnerf3e00082011-01-25 21:52:11 +00001118 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001119
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001120 /* check whether this has been sent by another originator before */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001121 if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001122 goto out;
1123
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001124 /* rebroadcast packet */
Sven Eckelmann9455e342012-05-12 02:09:37 +02001125 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001126
Simon Wunderlich23721382012-01-22 20:00:19 +01001127 /* don't hand the broadcast up if it is from an originator
1128 * from the same backbone.
1129 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001130 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
Simon Wunderlich23721382012-01-22 20:00:19 +01001131 goto out;
1132
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001133 /* broadcast for me */
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001134 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001135 ret = NET_RX_SUCCESS;
1136 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001137
Marek Lindnerf3e00082011-01-25 21:52:11 +00001138spin_unlock:
1139 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001140out:
1141 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001142 batadv_orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001143 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001144}
1145
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001146int batadv_recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001147{
1148 struct vis_packet *vis_packet;
1149 struct ethhdr *ethhdr;
1150 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann704509b2011-05-14 23:14:54 +02001151 int hdr_size = sizeof(*vis_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001152
1153 /* keep skb linear */
1154 if (skb_linearize(skb) < 0)
1155 return NET_RX_DROP;
1156
1157 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1158 return NET_RX_DROP;
1159
1160 vis_packet = (struct vis_packet *)skb->data;
1161 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1162
1163 /* not for me */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001164 if (!batadv_is_my_mac(ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001165 return NET_RX_DROP;
1166
1167 /* ignore own packets */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001168 if (batadv_is_my_mac(vis_packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001169 return NET_RX_DROP;
1170
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001171 if (batadv_is_my_mac(vis_packet->sender_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001172 return NET_RX_DROP;
1173
1174 switch (vis_packet->vis_type) {
1175 case VIS_TYPE_SERVER_SYNC:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001176 batadv_receive_server_sync_packet(bat_priv, vis_packet,
1177 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001178 break;
1179
1180 case VIS_TYPE_CLIENT_UPDATE:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001181 batadv_receive_client_update_packet(bat_priv, vis_packet,
1182 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001183 break;
1184
1185 default: /* ignore unknown packet */
1186 break;
1187 }
1188
1189 /* We take a copy of the data in the packet, so we should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001190 * always free the skbuf.
1191 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001192 return NET_RX_DROP;
1193}