net/mlx4_en: Fix mixed PFC and Global pause user control requests
[pandora-kernel.git] / net / batman-adv / routing.c
1 /*
2  * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA
19  *
20  */
21
22 #include "main.h"
23 #include "routing.h"
24 #include "send.h"
25 #include "soft-interface.h"
26 #include "hard-interface.h"
27 #include "icmp_socket.h"
28 #include "translation-table.h"
29 #include "originator.h"
30 #include "vis.h"
31 #include "unicast.h"
32 #include "bat_ogm.h"
33
34 void slide_own_bcast_window(struct hard_iface *hard_iface)
35 {
36         struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
37         struct hashtable_t *hash = bat_priv->orig_hash;
38         struct hlist_node *node;
39         struct hlist_head *head;
40         struct orig_node *orig_node;
41         unsigned long *word;
42         int i;
43         size_t word_index;
44
45         for (i = 0; i < hash->size; i++) {
46                 head = &hash->table[i];
47
48                 rcu_read_lock();
49                 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
50                         spin_lock_bh(&orig_node->ogm_cnt_lock);
51                         word_index = hard_iface->if_num * NUM_WORDS;
52                         word = &(orig_node->bcast_own[word_index]);
53
54                         bit_get_packet(bat_priv, word, 1, 0);
55                         orig_node->bcast_own_sum[hard_iface->if_num] =
56                                 bit_packet_count(word);
57                         spin_unlock_bh(&orig_node->ogm_cnt_lock);
58                 }
59                 rcu_read_unlock();
60         }
61 }
62
63 static void _update_route(struct bat_priv *bat_priv,
64                           struct orig_node *orig_node,
65                           struct neigh_node *neigh_node)
66 {
67         struct neigh_node *curr_router;
68
69         curr_router = orig_node_get_router(orig_node);
70
71         /* route deleted */
72         if ((curr_router) && (!neigh_node)) {
73                 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
74                         orig_node->orig);
75                 tt_global_del_orig(bat_priv, orig_node,
76                                     "Deleted route towards originator");
77
78         /* route added */
79         } else if ((!curr_router) && (neigh_node)) {
80
81                 bat_dbg(DBG_ROUTES, bat_priv,
82                         "Adding route towards: %pM (via %pM)\n",
83                         orig_node->orig, neigh_node->addr);
84         /* route changed */
85         } else if (neigh_node && curr_router) {
86                 bat_dbg(DBG_ROUTES, bat_priv,
87                         "Changing route towards: %pM "
88                         "(now via %pM - was via %pM)\n",
89                         orig_node->orig, neigh_node->addr,
90                         curr_router->addr);
91         }
92
93         if (curr_router)
94                 neigh_node_free_ref(curr_router);
95
96         /* increase refcount of new best neighbor */
97         if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
98                 neigh_node = NULL;
99
100         spin_lock_bh(&orig_node->neigh_list_lock);
101         /* curr_router used earlier may not be the current orig_node->router
102          * anymore because it was dereferenced outside of the neigh_list_lock
103          * protected region. After the new best neighbor has replace the current
104          * best neighbor the reference counter needs to decrease. Consequently,
105          * the code needs to ensure the curr_router variable contains a pointer
106          * to the replaced best neighbor.
107          */
108         curr_router = rcu_dereference_protected(orig_node->router, true);
109
110         rcu_assign_pointer(orig_node->router, neigh_node);
111         spin_unlock_bh(&orig_node->neigh_list_lock);
112
113         /* decrease refcount of previous best neighbor */
114         if (curr_router)
115                 neigh_node_free_ref(curr_router);
116 }
117
118 void update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
119                   struct neigh_node *neigh_node)
120 {
121         struct neigh_node *router = NULL;
122
123         if (!orig_node)
124                 goto out;
125
126         router = orig_node_get_router(orig_node);
127
128         if (router != neigh_node)
129                 _update_route(bat_priv, orig_node, neigh_node);
130
131 out:
132         if (router)
133                 neigh_node_free_ref(router);
134 }
135
136 /* caller must hold the neigh_list_lock */
137 void bonding_candidate_del(struct orig_node *orig_node,
138                            struct neigh_node *neigh_node)
139 {
140         /* this neighbor is not part of our candidate list */
141         if (list_empty(&neigh_node->bonding_list))
142                 goto out;
143
144         list_del_rcu(&neigh_node->bonding_list);
145         INIT_LIST_HEAD(&neigh_node->bonding_list);
146         neigh_node_free_ref(neigh_node);
147         atomic_dec(&orig_node->bond_candidates);
148
149 out:
150         return;
151 }
152
153 void bonding_candidate_add(struct orig_node *orig_node,
154                            struct neigh_node *neigh_node)
155 {
156         struct hlist_node *node;
157         struct neigh_node *tmp_neigh_node, *router = NULL;
158         uint8_t interference_candidate = 0;
159
160         spin_lock_bh(&orig_node->neigh_list_lock);
161
162         /* only consider if it has the same primary address ...  */
163         if (!compare_eth(orig_node->orig,
164                          neigh_node->orig_node->primary_addr))
165                 goto candidate_del;
166
167         router = orig_node_get_router(orig_node);
168         if (!router)
169                 goto candidate_del;
170
171         /* ... and is good enough to be considered */
172         if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
173                 goto candidate_del;
174
175         /**
176          * check if we have another candidate with the same mac address or
177          * interface. If we do, we won't select this candidate because of
178          * possible interference.
179          */
180         hlist_for_each_entry_rcu(tmp_neigh_node, node,
181                                  &orig_node->neigh_list, list) {
182
183                 if (tmp_neigh_node == neigh_node)
184                         continue;
185
186                 /* we only care if the other candidate is even
187                 * considered as candidate. */
188                 if (list_empty(&tmp_neigh_node->bonding_list))
189                         continue;
190
191                 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
192                     (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
193                         interference_candidate = 1;
194                         break;
195                 }
196         }
197
198         /* don't care further if it is an interference candidate */
199         if (interference_candidate)
200                 goto candidate_del;
201
202         /* this neighbor already is part of our candidate list */
203         if (!list_empty(&neigh_node->bonding_list))
204                 goto out;
205
206         if (!atomic_inc_not_zero(&neigh_node->refcount))
207                 goto out;
208
209         list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
210         atomic_inc(&orig_node->bond_candidates);
211         goto out;
212
213 candidate_del:
214         bonding_candidate_del(orig_node, neigh_node);
215
216 out:
217         spin_unlock_bh(&orig_node->neigh_list_lock);
218
219         if (router)
220                 neigh_node_free_ref(router);
221 }
222
223 /* copy primary address for bonding */
224 void bonding_save_primary(const struct orig_node *orig_node,
225                           struct orig_node *orig_neigh_node,
226                           const struct batman_ogm_packet *batman_ogm_packet)
227 {
228         if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
229                 return;
230
231         memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
232 }
233
234 /* checks whether the host restarted and is in the protection time.
235  * returns:
236  *  0 if the packet is to be accepted
237  *  1 if the packet is to be ignored.
238  */
239 int window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
240                      unsigned long *last_reset)
241 {
242         if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
243                 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
244                 if (time_after(jiffies, *last_reset +
245                         msecs_to_jiffies(RESET_PROTECTION_MS))) {
246
247                         *last_reset = jiffies;
248                         bat_dbg(DBG_BATMAN, bat_priv,
249                                 "old packet received, start protection\n");
250
251                         return 0;
252                 } else
253                         return 1;
254         }
255         return 0;
256 }
257
258 int recv_bat_ogm_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
259 {
260         struct ethhdr *ethhdr;
261
262         /* drop packet if it has not necessary minimum size */
263         if (unlikely(!pskb_may_pull(skb, BATMAN_OGM_LEN)))
264                 return NET_RX_DROP;
265
266         ethhdr = (struct ethhdr *)skb_mac_header(skb);
267
268         /* packet with broadcast indication but unicast recipient */
269         if (!is_broadcast_ether_addr(ethhdr->h_dest))
270                 return NET_RX_DROP;
271
272         /* packet with broadcast sender address */
273         if (is_broadcast_ether_addr(ethhdr->h_source))
274                 return NET_RX_DROP;
275
276         /* create a copy of the skb, if needed, to modify it. */
277         if (skb_cow(skb, 0) < 0)
278                 return NET_RX_DROP;
279
280         /* keep skb linear */
281         if (skb_linearize(skb) < 0)
282                 return NET_RX_DROP;
283
284         ethhdr = (struct ethhdr *)skb_mac_header(skb);
285
286         bat_ogm_receive(ethhdr, skb->data, skb_headlen(skb), hard_iface);
287
288         kfree_skb(skb);
289         return NET_RX_SUCCESS;
290 }
291
292 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
293                                struct sk_buff *skb, size_t icmp_len)
294 {
295         struct hard_iface *primary_if = NULL;
296         struct orig_node *orig_node = NULL;
297         struct neigh_node *router = NULL;
298         struct icmp_packet_rr *icmp_packet;
299         int ret = NET_RX_DROP;
300
301         icmp_packet = (struct icmp_packet_rr *)skb->data;
302
303         /* add data to device queue */
304         if (icmp_packet->msg_type != ECHO_REQUEST) {
305                 bat_socket_receive_packet(icmp_packet, icmp_len);
306                 goto out;
307         }
308
309         primary_if = primary_if_get_selected(bat_priv);
310         if (!primary_if)
311                 goto out;
312
313         /* answer echo request (ping) */
314         /* get routing information */
315         orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
316         if (!orig_node)
317                 goto out;
318
319         router = orig_node_get_router(orig_node);
320         if (!router)
321                 goto out;
322
323         /* create a copy of the skb, if needed, to modify it. */
324         if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
325                 goto out;
326
327         icmp_packet = (struct icmp_packet_rr *)skb->data;
328
329         memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
330         memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
331         icmp_packet->msg_type = ECHO_REPLY;
332         icmp_packet->ttl = TTL;
333
334         send_skb_packet(skb, router->if_incoming, router->addr);
335         ret = NET_RX_SUCCESS;
336
337 out:
338         if (primary_if)
339                 hardif_free_ref(primary_if);
340         if (router)
341                 neigh_node_free_ref(router);
342         if (orig_node)
343                 orig_node_free_ref(orig_node);
344         return ret;
345 }
346
347 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
348                                   struct sk_buff *skb)
349 {
350         struct hard_iface *primary_if = NULL;
351         struct orig_node *orig_node = NULL;
352         struct neigh_node *router = NULL;
353         struct icmp_packet *icmp_packet;
354         int ret = NET_RX_DROP;
355
356         icmp_packet = (struct icmp_packet *)skb->data;
357
358         /* send TTL exceeded if packet is an echo request (traceroute) */
359         if (icmp_packet->msg_type != ECHO_REQUEST) {
360                 pr_debug("Warning - can't forward icmp packet from %pM to "
361                          "%pM: ttl exceeded\n", icmp_packet->orig,
362                          icmp_packet->dst);
363                 goto out;
364         }
365
366         primary_if = primary_if_get_selected(bat_priv);
367         if (!primary_if)
368                 goto out;
369
370         /* get routing information */
371         orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
372         if (!orig_node)
373                 goto out;
374
375         router = orig_node_get_router(orig_node);
376         if (!router)
377                 goto out;
378
379         /* create a copy of the skb, if needed, to modify it. */
380         if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
381                 goto out;
382
383         icmp_packet = (struct icmp_packet *)skb->data;
384
385         memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
386         memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
387         icmp_packet->msg_type = TTL_EXCEEDED;
388         icmp_packet->ttl = TTL;
389
390         send_skb_packet(skb, router->if_incoming, router->addr);
391         ret = NET_RX_SUCCESS;
392
393 out:
394         if (primary_if)
395                 hardif_free_ref(primary_if);
396         if (router)
397                 neigh_node_free_ref(router);
398         if (orig_node)
399                 orig_node_free_ref(orig_node);
400         return ret;
401 }
402
403
404 int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
405 {
406         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
407         struct icmp_packet_rr *icmp_packet;
408         struct ethhdr *ethhdr;
409         struct orig_node *orig_node = NULL;
410         struct neigh_node *router = NULL;
411         int hdr_size = sizeof(struct icmp_packet);
412         int ret = NET_RX_DROP;
413
414         /**
415          * we truncate all incoming icmp packets if they don't match our size
416          */
417         if (skb->len >= sizeof(struct icmp_packet_rr))
418                 hdr_size = sizeof(struct icmp_packet_rr);
419
420         /* drop packet if it has not necessary minimum size */
421         if (unlikely(!pskb_may_pull(skb, hdr_size)))
422                 goto out;
423
424         ethhdr = (struct ethhdr *)skb_mac_header(skb);
425
426         /* packet with unicast indication but broadcast recipient */
427         if (is_broadcast_ether_addr(ethhdr->h_dest))
428                 goto out;
429
430         /* packet with broadcast sender address */
431         if (is_broadcast_ether_addr(ethhdr->h_source))
432                 goto out;
433
434         /* not for me */
435         if (!is_my_mac(ethhdr->h_dest))
436                 goto out;
437
438         icmp_packet = (struct icmp_packet_rr *)skb->data;
439
440         /* add record route information if not full */
441         if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
442             (icmp_packet->rr_cur < BAT_RR_LEN)) {
443                 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
444                         ethhdr->h_dest, ETH_ALEN);
445                 icmp_packet->rr_cur++;
446         }
447
448         /* packet for me */
449         if (is_my_mac(icmp_packet->dst))
450                 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
451
452         /* TTL exceeded */
453         if (icmp_packet->ttl < 2)
454                 return recv_icmp_ttl_exceeded(bat_priv, skb);
455
456         /* get routing information */
457         orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
458         if (!orig_node)
459                 goto out;
460
461         router = orig_node_get_router(orig_node);
462         if (!router)
463                 goto out;
464
465         /* create a copy of the skb, if needed, to modify it. */
466         if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
467                 goto out;
468
469         icmp_packet = (struct icmp_packet_rr *)skb->data;
470
471         /* decrement ttl */
472         icmp_packet->ttl--;
473
474         /* route it */
475         send_skb_packet(skb, router->if_incoming, router->addr);
476         ret = NET_RX_SUCCESS;
477
478 out:
479         if (router)
480                 neigh_node_free_ref(router);
481         if (orig_node)
482                 orig_node_free_ref(orig_node);
483         return ret;
484 }
485
486 /* In the bonding case, send the packets in a round
487  * robin fashion over the remaining interfaces.
488  *
489  * This method rotates the bonding list and increases the
490  * returned router's refcount. */
491 static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
492                                            const struct hard_iface *recv_if)
493 {
494         struct neigh_node *tmp_neigh_node;
495         struct neigh_node *router = NULL, *first_candidate = NULL;
496
497         rcu_read_lock();
498         list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
499                                 bonding_list) {
500                 if (!first_candidate)
501                         first_candidate = tmp_neigh_node;
502
503                 /* recv_if == NULL on the first node. */
504                 if (tmp_neigh_node->if_incoming == recv_if)
505                         continue;
506
507                 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
508                         continue;
509
510                 router = tmp_neigh_node;
511                 break;
512         }
513
514         /* use the first candidate if nothing was found. */
515         if (!router && first_candidate &&
516             atomic_inc_not_zero(&first_candidate->refcount))
517                 router = first_candidate;
518
519         if (!router)
520                 goto out;
521
522         /* selected should point to the next element
523          * after the current router */
524         spin_lock_bh(&primary_orig->neigh_list_lock);
525         /* this is a list_move(), which unfortunately
526          * does not exist as rcu version */
527         list_del_rcu(&primary_orig->bond_list);
528         list_add_rcu(&primary_orig->bond_list,
529                      &router->bonding_list);
530         spin_unlock_bh(&primary_orig->neigh_list_lock);
531
532 out:
533         rcu_read_unlock();
534         return router;
535 }
536
537 /* Interface Alternating: Use the best of the
538  * remaining candidates which are not using
539  * this interface.
540  *
541  * Increases the returned router's refcount */
542 static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
543                                               const struct hard_iface *recv_if)
544 {
545         struct neigh_node *tmp_neigh_node;
546         struct neigh_node *router = NULL, *first_candidate = NULL;
547
548         rcu_read_lock();
549         list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
550                                 bonding_list) {
551                 if (!first_candidate)
552                         first_candidate = tmp_neigh_node;
553
554                 /* recv_if == NULL on the first node. */
555                 if (tmp_neigh_node->if_incoming == recv_if)
556                         continue;
557
558                 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
559                         continue;
560
561                 /* if we don't have a router yet
562                  * or this one is better, choose it. */
563                 if ((!router) ||
564                     (tmp_neigh_node->tq_avg > router->tq_avg)) {
565                         /* decrement refcount of
566                          * previously selected router */
567                         if (router)
568                                 neigh_node_free_ref(router);
569
570                         router = tmp_neigh_node;
571                         atomic_inc_not_zero(&router->refcount);
572                 }
573
574                 neigh_node_free_ref(tmp_neigh_node);
575         }
576
577         /* use the first candidate if nothing was found. */
578         if (!router && first_candidate &&
579             atomic_inc_not_zero(&first_candidate->refcount))
580                 router = first_candidate;
581
582         rcu_read_unlock();
583         return router;
584 }
585
586 int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
587 {
588         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
589         struct tt_query_packet *tt_query;
590         struct ethhdr *ethhdr;
591
592         /* drop packet if it has not necessary minimum size */
593         if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
594                 goto out;
595
596         /* I could need to modify it */
597         if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
598                 goto out;
599
600         ethhdr = (struct ethhdr *)skb_mac_header(skb);
601
602         /* packet with unicast indication but broadcast recipient */
603         if (is_broadcast_ether_addr(ethhdr->h_dest))
604                 goto out;
605
606         /* packet with broadcast sender address */
607         if (is_broadcast_ether_addr(ethhdr->h_source))
608                 goto out;
609
610         tt_query = (struct tt_query_packet *)skb->data;
611
612         tt_query->tt_data = ntohs(tt_query->tt_data);
613
614         switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
615         case TT_REQUEST:
616                 /* If we cannot provide an answer the tt_request is
617                  * forwarded */
618                 if (!send_tt_response(bat_priv, tt_query)) {
619                         bat_dbg(DBG_TT, bat_priv,
620                                 "Routing TT_REQUEST to %pM [%c]\n",
621                                 tt_query->dst,
622                                 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
623                         tt_query->tt_data = htons(tt_query->tt_data);
624                         return route_unicast_packet(skb, recv_if);
625                 }
626                 break;
627         case TT_RESPONSE:
628                 /* packet needs to be linearized to access the TT changes */
629                 if (skb_linearize(skb) < 0)
630                         goto out;
631                 /* skb_linearize() possibly changed skb->data */
632                 tt_query = (struct tt_query_packet *)skb->data;
633
634                 if (is_my_mac(tt_query->dst))
635                         handle_tt_response(bat_priv, tt_query);
636                 else {
637                         bat_dbg(DBG_TT, bat_priv,
638                                 "Routing TT_RESPONSE to %pM [%c]\n",
639                                 tt_query->dst,
640                                 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
641                         tt_query->tt_data = htons(tt_query->tt_data);
642                         return route_unicast_packet(skb, recv_if);
643                 }
644                 break;
645         }
646
647 out:
648         /* returning NET_RX_DROP will make the caller function kfree the skb */
649         return NET_RX_DROP;
650 }
651
652 int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
653 {
654         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
655         struct roam_adv_packet *roam_adv_packet;
656         struct orig_node *orig_node;
657         struct ethhdr *ethhdr;
658
659         /* drop packet if it has not necessary minimum size */
660         if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
661                 goto out;
662
663         ethhdr = (struct ethhdr *)skb_mac_header(skb);
664
665         /* packet with unicast indication but broadcast recipient */
666         if (is_broadcast_ether_addr(ethhdr->h_dest))
667                 goto out;
668
669         /* packet with broadcast sender address */
670         if (is_broadcast_ether_addr(ethhdr->h_source))
671                 goto out;
672
673         roam_adv_packet = (struct roam_adv_packet *)skb->data;
674
675         if (!is_my_mac(roam_adv_packet->dst))
676                 return route_unicast_packet(skb, recv_if);
677
678         orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
679         if (!orig_node)
680                 goto out;
681
682         bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
683                 "(client %pM)\n", roam_adv_packet->src,
684                 roam_adv_packet->client);
685
686         tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
687                       atomic_read(&orig_node->last_ttvn) + 1, true, false);
688
689         /* Roaming phase starts: I have new information but the ttvn has not
690          * been incremented yet. This flag will make me check all the incoming
691          * packets for the correct destination. */
692         bat_priv->tt_poss_change = true;
693
694         orig_node_free_ref(orig_node);
695 out:
696         /* returning NET_RX_DROP will make the caller function kfree the skb */
697         return NET_RX_DROP;
698 }
699
700 /* find a suitable router for this originator, and use
701  * bonding if possible. increases the found neighbors
702  * refcount.*/
703 struct neigh_node *find_router(struct bat_priv *bat_priv,
704                                struct orig_node *orig_node,
705                                const struct hard_iface *recv_if)
706 {
707         struct orig_node *primary_orig_node;
708         struct orig_node *router_orig;
709         struct neigh_node *router;
710         static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
711         int bonding_enabled;
712
713         if (!orig_node)
714                 return NULL;
715
716         router = orig_node_get_router(orig_node);
717         if (!router)
718                 goto err;
719
720         /* without bonding, the first node should
721          * always choose the default router. */
722         bonding_enabled = atomic_read(&bat_priv->bonding);
723
724         rcu_read_lock();
725         /* select default router to output */
726         router_orig = router->orig_node;
727         if (!router_orig)
728                 goto err_unlock;
729
730         if ((!recv_if) && (!bonding_enabled))
731                 goto return_router;
732
733         /* if we have something in the primary_addr, we can search
734          * for a potential bonding candidate. */
735         if (compare_eth(router_orig->primary_addr, zero_mac))
736                 goto return_router;
737
738         /* find the orig_node which has the primary interface. might
739          * even be the same as our router_orig in many cases */
740
741         if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
742                 primary_orig_node = router_orig;
743         } else {
744                 primary_orig_node = orig_hash_find(bat_priv,
745                                                    router_orig->primary_addr);
746                 if (!primary_orig_node)
747                         goto return_router;
748
749                 orig_node_free_ref(primary_orig_node);
750         }
751
752         /* with less than 2 candidates, we can't do any
753          * bonding and prefer the original router. */
754         if (atomic_read(&primary_orig_node->bond_candidates) < 2)
755                 goto return_router;
756
757         /* all nodes between should choose a candidate which
758          * is is not on the interface where the packet came
759          * in. */
760
761         neigh_node_free_ref(router);
762
763         if (bonding_enabled)
764                 router = find_bond_router(primary_orig_node, recv_if);
765         else
766                 router = find_ifalter_router(primary_orig_node, recv_if);
767
768 return_router:
769         if (router && router->if_incoming->if_status != IF_ACTIVE)
770                 goto err_unlock;
771
772         rcu_read_unlock();
773         return router;
774 err_unlock:
775         rcu_read_unlock();
776 err:
777         if (router)
778                 neigh_node_free_ref(router);
779         return NULL;
780 }
781
782 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
783 {
784         struct ethhdr *ethhdr;
785
786         /* drop packet if it has not necessary minimum size */
787         if (unlikely(!pskb_may_pull(skb, hdr_size)))
788                 return -1;
789
790         ethhdr = (struct ethhdr *)skb_mac_header(skb);
791
792         /* packet with unicast indication but broadcast recipient */
793         if (is_broadcast_ether_addr(ethhdr->h_dest))
794                 return -1;
795
796         /* packet with broadcast sender address */
797         if (is_broadcast_ether_addr(ethhdr->h_source))
798                 return -1;
799
800         /* not for me */
801         if (!is_my_mac(ethhdr->h_dest))
802                 return -1;
803
804         return 0;
805 }
806
807 int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
808 {
809         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
810         struct orig_node *orig_node = NULL;
811         struct neigh_node *neigh_node = NULL;
812         struct unicast_packet *unicast_packet;
813         struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
814         int ret = NET_RX_DROP;
815         struct sk_buff *new_skb;
816
817         unicast_packet = (struct unicast_packet *)skb->data;
818
819         /* TTL exceeded */
820         if (unicast_packet->ttl < 2) {
821                 pr_debug("Warning - can't forward unicast packet from %pM to "
822                          "%pM: ttl exceeded\n", ethhdr->h_source,
823                          unicast_packet->dest);
824                 goto out;
825         }
826
827         /* get routing information */
828         orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
829
830         if (!orig_node)
831                 goto out;
832
833         /* find_router() increases neigh_nodes refcount if found. */
834         neigh_node = find_router(bat_priv, orig_node, recv_if);
835
836         if (!neigh_node)
837                 goto out;
838
839         /* create a copy of the skb, if needed, to modify it. */
840         if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
841                 goto out;
842
843         unicast_packet = (struct unicast_packet *)skb->data;
844
845         if (unicast_packet->packet_type == BAT_UNICAST &&
846             atomic_read(&bat_priv->fragmentation) &&
847             skb->len > neigh_node->if_incoming->net_dev->mtu) {
848                 ret = frag_send_skb(skb, bat_priv,
849                                     neigh_node->if_incoming, neigh_node->addr);
850                 goto out;
851         }
852
853         if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
854             frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
855
856                 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
857
858                 if (ret == NET_RX_DROP)
859                         goto out;
860
861                 /* packet was buffered for late merge */
862                 if (!new_skb) {
863                         ret = NET_RX_SUCCESS;
864                         goto out;
865                 }
866
867                 skb = new_skb;
868                 unicast_packet = (struct unicast_packet *)skb->data;
869         }
870
871         /* decrement ttl */
872         unicast_packet->ttl--;
873
874         /* route it */
875         send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
876         ret = NET_RX_SUCCESS;
877
878 out:
879         if (neigh_node)
880                 neigh_node_free_ref(neigh_node);
881         if (orig_node)
882                 orig_node_free_ref(orig_node);
883         return ret;
884 }
885
886 static int check_unicast_ttvn(struct bat_priv *bat_priv,
887                                struct sk_buff *skb) {
888         uint8_t curr_ttvn;
889         struct orig_node *orig_node;
890         struct ethhdr *ethhdr;
891         struct hard_iface *primary_if;
892         struct unicast_packet *unicast_packet;
893         bool tt_poss_change;
894
895         /* I could need to modify it */
896         if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
897                 return 0;
898
899         unicast_packet = (struct unicast_packet *)skb->data;
900
901         if (is_my_mac(unicast_packet->dest)) {
902                 tt_poss_change = bat_priv->tt_poss_change;
903                 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
904         } else {
905                 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
906
907                 if (!orig_node)
908                         return 0;
909
910                 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
911                 tt_poss_change = orig_node->tt_poss_change;
912                 orig_node_free_ref(orig_node);
913         }
914
915         /* Check whether I have to reroute the packet */
916         if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
917                 /* Linearize the skb before accessing it */
918                 if (skb_linearize(skb) < 0)
919                         return 0;
920
921                 ethhdr = (struct ethhdr *)(skb->data +
922                         sizeof(struct unicast_packet));
923                 orig_node = transtable_search(bat_priv, NULL, ethhdr->h_dest);
924
925                 if (!orig_node) {
926                         if (!is_my_client(bat_priv, ethhdr->h_dest))
927                                 return 0;
928                         primary_if = primary_if_get_selected(bat_priv);
929                         if (!primary_if)
930                                 return 0;
931                         memcpy(unicast_packet->dest,
932                                primary_if->net_dev->dev_addr, ETH_ALEN);
933                         hardif_free_ref(primary_if);
934                 } else {
935                         memcpy(unicast_packet->dest, orig_node->orig,
936                                ETH_ALEN);
937                         curr_ttvn = (uint8_t)
938                                 atomic_read(&orig_node->last_ttvn);
939                         orig_node_free_ref(orig_node);
940                 }
941
942                 bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
943                         "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
944                         "%pM\n", unicast_packet->ttvn, curr_ttvn,
945                         ethhdr->h_dest, unicast_packet->dest);
946
947                 unicast_packet->ttvn = curr_ttvn;
948         }
949         return 1;
950 }
951
952 int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
953 {
954         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
955         struct unicast_packet *unicast_packet;
956         int hdr_size = sizeof(*unicast_packet);
957
958         if (check_unicast_packet(skb, hdr_size) < 0)
959                 return NET_RX_DROP;
960
961         if (!check_unicast_ttvn(bat_priv, skb))
962                 return NET_RX_DROP;
963
964         unicast_packet = (struct unicast_packet *)skb->data;
965
966         /* packet for me */
967         if (is_my_mac(unicast_packet->dest)) {
968                 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
969                 return NET_RX_SUCCESS;
970         }
971
972         return route_unicast_packet(skb, recv_if);
973 }
974
975 int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
976 {
977         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
978         struct unicast_frag_packet *unicast_packet;
979         int hdr_size = sizeof(*unicast_packet);
980         struct sk_buff *new_skb = NULL;
981         int ret;
982
983         if (check_unicast_packet(skb, hdr_size) < 0)
984                 return NET_RX_DROP;
985
986         if (!check_unicast_ttvn(bat_priv, skb))
987                 return NET_RX_DROP;
988
989         unicast_packet = (struct unicast_frag_packet *)skb->data;
990
991         /* packet for me */
992         if (is_my_mac(unicast_packet->dest)) {
993
994                 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
995
996                 if (ret == NET_RX_DROP)
997                         return NET_RX_DROP;
998
999                 /* packet was buffered for late merge */
1000                 if (!new_skb)
1001                         return NET_RX_SUCCESS;
1002
1003                 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1004                              sizeof(struct unicast_packet));
1005                 return NET_RX_SUCCESS;
1006         }
1007
1008         return route_unicast_packet(skb, recv_if);
1009 }
1010
1011
1012 int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1013 {
1014         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1015         struct orig_node *orig_node = NULL;
1016         struct bcast_packet *bcast_packet;
1017         struct ethhdr *ethhdr;
1018         int hdr_size = sizeof(*bcast_packet);
1019         int ret = NET_RX_DROP;
1020         int32_t seq_diff;
1021
1022         /* drop packet if it has not necessary minimum size */
1023         if (unlikely(!pskb_may_pull(skb, hdr_size)))
1024                 goto out;
1025
1026         ethhdr = (struct ethhdr *)skb_mac_header(skb);
1027
1028         /* packet with broadcast indication but unicast recipient */
1029         if (!is_broadcast_ether_addr(ethhdr->h_dest))
1030                 goto out;
1031
1032         /* packet with broadcast sender address */
1033         if (is_broadcast_ether_addr(ethhdr->h_source))
1034                 goto out;
1035
1036         /* ignore broadcasts sent by myself */
1037         if (is_my_mac(ethhdr->h_source))
1038                 goto out;
1039
1040         bcast_packet = (struct bcast_packet *)skb->data;
1041
1042         /* ignore broadcasts originated by myself */
1043         if (is_my_mac(bcast_packet->orig))
1044                 goto out;
1045
1046         if (bcast_packet->ttl < 2)
1047                 goto out;
1048
1049         orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
1050
1051         if (!orig_node)
1052                 goto out;
1053
1054         spin_lock_bh(&orig_node->bcast_seqno_lock);
1055
1056         /* check whether the packet is a duplicate */
1057         if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1058                            ntohl(bcast_packet->seqno)))
1059                 goto spin_unlock;
1060
1061         seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1062
1063         /* check whether the packet is old and the host just restarted. */
1064         if (window_protected(bat_priv, seq_diff,
1065                              &orig_node->bcast_seqno_reset))
1066                 goto spin_unlock;
1067
1068         /* mark broadcast in flood history, update window position
1069          * if required. */
1070         if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1071                 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1072
1073         spin_unlock_bh(&orig_node->bcast_seqno_lock);
1074
1075         /* rebroadcast packet */
1076         add_bcast_packet_to_list(bat_priv, skb, 1);
1077
1078         /* broadcast for me */
1079         interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1080         ret = NET_RX_SUCCESS;
1081         goto out;
1082
1083 spin_unlock:
1084         spin_unlock_bh(&orig_node->bcast_seqno_lock);
1085 out:
1086         if (orig_node)
1087                 orig_node_free_ref(orig_node);
1088         return ret;
1089 }
1090
1091 int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1092 {
1093         struct vis_packet *vis_packet;
1094         struct ethhdr *ethhdr;
1095         struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1096         int hdr_size = sizeof(*vis_packet);
1097
1098         /* keep skb linear */
1099         if (skb_linearize(skb) < 0)
1100                 return NET_RX_DROP;
1101
1102         if (unlikely(!pskb_may_pull(skb, hdr_size)))
1103                 return NET_RX_DROP;
1104
1105         vis_packet = (struct vis_packet *)skb->data;
1106         ethhdr = (struct ethhdr *)skb_mac_header(skb);
1107
1108         /* not for me */
1109         if (!is_my_mac(ethhdr->h_dest))
1110                 return NET_RX_DROP;
1111
1112         /* ignore own packets */
1113         if (is_my_mac(vis_packet->vis_orig))
1114                 return NET_RX_DROP;
1115
1116         if (is_my_mac(vis_packet->sender_orig))
1117                 return NET_RX_DROP;
1118
1119         switch (vis_packet->vis_type) {
1120         case VIS_TYPE_SERVER_SYNC:
1121                 receive_server_sync_packet(bat_priv, vis_packet,
1122                                            skb_headlen(skb));
1123                 break;
1124
1125         case VIS_TYPE_CLIENT_UPDATE:
1126                 receive_client_update_packet(bat_priv, vis_packet,
1127                                              skb_headlen(skb));
1128                 break;
1129
1130         default:        /* ignore unknown packet */
1131                 break;
1132         }
1133
1134         /* We take a copy of the data in the packet, so we should
1135            always free the skbuf. */
1136         return NET_RX_DROP;
1137 }