Antonio Quartulli | 0b87393 | 2013-01-04 03:05:31 +0100 | [diff] [blame] | 1 | /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 2 | * |
| 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 |
Antonio Quartulli | ebf38fb | 2013-11-03 20:40:48 +0100 | [diff] [blame] | 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include "main.h" |
| 19 | #include "routing.h" |
| 20 | #include "send.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 21 | #include "soft-interface.h" |
| 22 | #include "hard-interface.h" |
| 23 | #include "icmp_socket.h" |
| 24 | #include "translation-table.h" |
| 25 | #include "originator.h" |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 26 | #include "bridge_loop_avoidance.h" |
Antonio Quartulli | c384ea3 | 2011-06-26 03:37:18 +0200 | [diff] [blame] | 27 | #include "distributed-arp-table.h" |
Martin Hundebøll | 9533247 | 2013-01-25 11:12:40 +0100 | [diff] [blame] | 28 | #include "network-coding.h" |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 29 | #include "fragmentation.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 30 | |
Antonio Quartulli | c018ad3 | 2013-06-04 12:11:39 +0200 | [diff] [blame] | 31 | #include <linux/if_vlan.h> |
| 32 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 33 | static int batadv_route_unicast_packet(struct sk_buff *skb, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 34 | struct batadv_hard_iface *recv_if); |
Simon Wunderlich | a7f6ee9 | 2012-01-22 20:00:18 +0100 | [diff] [blame] | 35 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 36 | static void _batadv_update_route(struct batadv_priv *bat_priv, |
| 37 | struct batadv_orig_node *orig_node, |
| 38 | struct batadv_neigh_node *neigh_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 39 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 40 | struct batadv_neigh_node *curr_router; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 41 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 42 | curr_router = batadv_orig_node_get_router(orig_node); |
Marek Lindner | a8e7f4b | 2010-12-12 21:57:10 +0000 | [diff] [blame] | 43 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 44 | /* route deleted */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 45 | if ((curr_router) && (!neigh_node)) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 46 | batadv_dbg(BATADV_DBG_ROUTES, bat_priv, |
| 47 | "Deleting route towards: %pM\n", orig_node->orig); |
Antonio Quartulli | 95fb130 | 2013-08-07 18:28:55 +0200 | [diff] [blame] | 48 | batadv_tt_global_del_orig(bat_priv, orig_node, -1, |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 49 | "Deleted route towards originator"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 50 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 51 | /* route added */ |
| 52 | } else if ((!curr_router) && (neigh_node)) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 53 | batadv_dbg(BATADV_DBG_ROUTES, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 54 | "Adding route towards: %pM (via %pM)\n", |
| 55 | orig_node->orig, neigh_node->addr); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 56 | /* route changed */ |
Sven Eckelmann | bb899b8 | 2011-05-10 11:22:37 +0200 | [diff] [blame] | 57 | } else if (neigh_node && curr_router) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 58 | batadv_dbg(BATADV_DBG_ROUTES, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 59 | "Changing route towards: %pM (now via %pM - was via %pM)\n", |
| 60 | orig_node->orig, neigh_node->addr, |
| 61 | curr_router->addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 64 | if (curr_router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 65 | batadv_neigh_node_free_ref(curr_router); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 66 | |
| 67 | /* increase refcount of new best neighbor */ |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 68 | if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount)) |
| 69 | neigh_node = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 70 | |
| 71 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 72 | rcu_assign_pointer(orig_node->router, neigh_node); |
| 73 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 74 | |
| 75 | /* decrease refcount of previous best neighbor */ |
| 76 | if (curr_router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 77 | batadv_neigh_node_free_ref(curr_router); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 80 | void batadv_update_route(struct batadv_priv *bat_priv, |
| 81 | struct batadv_orig_node *orig_node, |
| 82 | struct batadv_neigh_node *neigh_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 83 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 84 | struct batadv_neigh_node *router = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 85 | |
| 86 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 87 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 88 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 89 | router = batadv_orig_node_get_router(orig_node); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 90 | |
| 91 | if (router != neigh_node) |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 92 | _batadv_update_route(bat_priv, orig_node, neigh_node); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 93 | |
| 94 | out: |
| 95 | if (router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 96 | batadv_neigh_node_free_ref(router); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 99 | /* checks whether the host restarted and is in the protection time. |
| 100 | * returns: |
| 101 | * 0 if the packet is to be accepted |
| 102 | * 1 if the packet is to be ignored. |
| 103 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 104 | int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff, |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 105 | unsigned long *last_reset) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 106 | { |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 107 | if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE || |
| 108 | seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) { |
| 109 | if (!batadv_has_timed_out(*last_reset, |
| 110 | BATADV_RESET_PROTECTION_MS)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 111 | return 1; |
Marek Lindner | 8c7bf24 | 2012-03-17 15:28:33 +0800 | [diff] [blame] | 112 | |
| 113 | *last_reset = jiffies; |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 114 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 115 | "old packet received, start protection\n"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 116 | } |
Marek Lindner | 8c7bf24 | 2012-03-17 15:28:33 +0800 | [diff] [blame] | 117 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 121 | bool batadv_check_management_packet(struct sk_buff *skb, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 122 | struct batadv_hard_iface *hard_iface, |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 123 | int header_len) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 124 | { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 125 | struct ethhdr *ethhdr; |
| 126 | |
| 127 | /* drop packet if it has not necessary minimum size */ |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 128 | if (unlikely(!pskb_may_pull(skb, header_len))) |
| 129 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 130 | |
Antonio Quartulli | 7ed4be9 | 2013-04-08 15:08:18 +0200 | [diff] [blame] | 131 | ethhdr = eth_hdr(skb); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 132 | |
| 133 | /* packet with broadcast indication but unicast recipient */ |
| 134 | if (!is_broadcast_ether_addr(ethhdr->h_dest)) |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 135 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 136 | |
| 137 | /* packet with broadcast sender address */ |
| 138 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 139 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 140 | |
| 141 | /* create a copy of the skb, if needed, to modify it. */ |
| 142 | if (skb_cow(skb, 0) < 0) |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 143 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 144 | |
| 145 | /* keep skb linear */ |
| 146 | if (skb_linearize(skb) < 0) |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 147 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 148 | |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 149 | return true; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 152 | /** |
| 153 | * batadv_recv_my_icmp_packet - receive an icmp packet locally |
| 154 | * @bat_priv: the bat priv with all the soft interface information |
| 155 | * @skb: icmp packet to process |
| 156 | * |
| 157 | * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP |
| 158 | * otherwise. |
| 159 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 160 | static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv, |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 161 | struct sk_buff *skb) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 162 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 163 | struct batadv_hard_iface *primary_if = NULL; |
| 164 | struct batadv_orig_node *orig_node = NULL; |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 165 | struct batadv_icmp_header *icmph; |
| 166 | int res, ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 167 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 168 | icmph = (struct batadv_icmp_header *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 169 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 170 | switch (icmph->msg_type) { |
| 171 | case BATADV_ECHO_REPLY: |
| 172 | case BATADV_DESTINATION_UNREACHABLE: |
| 173 | case BATADV_TTL_EXCEEDED: |
| 174 | /* receive the packet */ |
| 175 | if (skb_linearize(skb) < 0) |
| 176 | break; |
| 177 | |
| 178 | batadv_socket_receive_packet(icmph, skb->len); |
| 179 | break; |
| 180 | case BATADV_ECHO_REQUEST: |
| 181 | /* answer echo request (ping) */ |
| 182 | primary_if = batadv_primary_if_get_selected(bat_priv); |
| 183 | if (!primary_if) |
| 184 | goto out; |
| 185 | |
| 186 | /* get routing information */ |
| 187 | orig_node = batadv_orig_hash_find(bat_priv, icmph->orig); |
| 188 | if (!orig_node) |
| 189 | goto out; |
| 190 | |
| 191 | /* create a copy of the skb, if needed, to modify it. */ |
| 192 | if (skb_cow(skb, ETH_HLEN) < 0) |
| 193 | goto out; |
| 194 | |
| 195 | icmph = (struct batadv_icmp_header *)skb->data; |
| 196 | |
| 197 | memcpy(icmph->dst, icmph->orig, ETH_ALEN); |
| 198 | memcpy(icmph->orig, primary_if->net_dev->dev_addr, ETH_ALEN); |
| 199 | icmph->msg_type = BATADV_ECHO_REPLY; |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 200 | icmph->ttl = BATADV_TTL; |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 201 | |
| 202 | res = batadv_send_skb_to_orig(skb, orig_node, NULL); |
| 203 | if (res != NET_XMIT_DROP) |
| 204 | ret = NET_RX_SUCCESS; |
| 205 | |
| 206 | break; |
| 207 | default: |
| 208 | /* drop unknown type */ |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 209 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 210 | } |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 211 | out: |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 212 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 213 | batadv_hardif_free_ref(primary_if); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 214 | if (orig_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 215 | batadv_orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 216 | return ret; |
| 217 | } |
| 218 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 219 | static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv, |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 220 | struct sk_buff *skb) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 221 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 222 | struct batadv_hard_iface *primary_if = NULL; |
| 223 | struct batadv_orig_node *orig_node = NULL; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 224 | struct batadv_icmp_packet *icmp_packet; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 225 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 226 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 227 | icmp_packet = (struct batadv_icmp_packet *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 228 | |
| 229 | /* send TTL exceeded if packet is an echo request (traceroute) */ |
Antonio Quartulli | 27a417e | 2013-12-05 15:33:00 +0100 | [diff] [blame] | 230 | if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 231 | pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n", |
Antonio Quartulli | 27a417e | 2013-12-05 15:33:00 +0100 | [diff] [blame] | 232 | icmp_packet->orig, icmp_packet->dst); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 233 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 236 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 237 | if (!primary_if) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 238 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 239 | |
| 240 | /* get routing information */ |
Antonio Quartulli | 27a417e | 2013-12-05 15:33:00 +0100 | [diff] [blame] | 241 | orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 242 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 243 | goto out; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 244 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 245 | /* create a copy of the skb, if needed, to modify it. */ |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 246 | if (skb_cow(skb, ETH_HLEN) < 0) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 247 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 248 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 249 | icmp_packet = (struct batadv_icmp_packet *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 250 | |
Antonio Quartulli | 27a417e | 2013-12-05 15:33:00 +0100 | [diff] [blame] | 251 | memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); |
| 252 | memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, |
Antonio Quartulli | 0bf84c1 | 2013-05-18 14:56:57 +0200 | [diff] [blame] | 253 | ETH_ALEN); |
Antonio Quartulli | 27a417e | 2013-12-05 15:33:00 +0100 | [diff] [blame] | 254 | icmp_packet->msg_type = BATADV_TTL_EXCEEDED; |
| 255 | icmp_packet->ttl = BATADV_TTL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 256 | |
Martin Hundebøll | e91ecfc | 2013-04-20 13:54:39 +0200 | [diff] [blame] | 257 | if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP) |
Martin Hundebøll | bb351ba | 2012-10-16 16:13:48 +0200 | [diff] [blame] | 258 | ret = NET_RX_SUCCESS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 259 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 260 | out: |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 261 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 262 | batadv_hardif_free_ref(primary_if); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 263 | if (orig_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 264 | batadv_orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 269 | int batadv_recv_icmp_packet(struct sk_buff *skb, |
| 270 | struct batadv_hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 271 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 272 | struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 273 | struct batadv_icmp_header *icmph; |
| 274 | struct batadv_icmp_packet_rr *icmp_packet_rr; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 275 | struct ethhdr *ethhdr; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 276 | struct batadv_orig_node *orig_node = NULL; |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 277 | int hdr_size = sizeof(struct batadv_icmp_header); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 278 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 279 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 280 | /* drop packet if it has not necessary minimum size */ |
| 281 | if (unlikely(!pskb_may_pull(skb, hdr_size))) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 282 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 283 | |
Antonio Quartulli | 7ed4be9 | 2013-04-08 15:08:18 +0200 | [diff] [blame] | 284 | ethhdr = eth_hdr(skb); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 285 | |
| 286 | /* packet with unicast indication but broadcast recipient */ |
| 287 | if (is_broadcast_ether_addr(ethhdr->h_dest)) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 288 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 289 | |
| 290 | /* packet with broadcast sender address */ |
| 291 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 292 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 293 | |
| 294 | /* not for me */ |
Antonio Quartulli | fe8a93b | 2013-04-03 19:10:26 +0200 | [diff] [blame] | 295 | if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest)) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 296 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 297 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 298 | icmph = (struct batadv_icmp_header *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 299 | |
| 300 | /* add record route information if not full */ |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 301 | if ((icmph->msg_type == BATADV_ECHO_REPLY || |
| 302 | icmph->msg_type == BATADV_ECHO_REQUEST) && |
| 303 | (skb->len >= sizeof(struct batadv_icmp_packet_rr))) { |
| 304 | if (skb_linearize(skb) < 0) |
| 305 | goto out; |
| 306 | |
| 307 | /* create a copy of the skb, if needed, to modify it. */ |
| 308 | if (skb_cow(skb, ETH_HLEN) < 0) |
| 309 | goto out; |
| 310 | |
| 311 | icmph = (struct batadv_icmp_header *)skb->data; |
| 312 | icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph; |
| 313 | if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN) |
| 314 | goto out; |
| 315 | |
| 316 | memcpy(&(icmp_packet_rr->rr[icmp_packet_rr->rr_cur]), |
Sven Eckelmann | 7c64fd9 | 2012-02-28 10:55:36 +0100 | [diff] [blame] | 317 | ethhdr->h_dest, ETH_ALEN); |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 318 | icmp_packet_rr->rr_cur++; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* packet for me */ |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 322 | if (batadv_is_my_mac(bat_priv, icmph->dst)) |
| 323 | return batadv_recv_my_icmp_packet(bat_priv, skb); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 324 | |
| 325 | /* TTL exceeded */ |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 326 | if (icmph->ttl < 2) |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 327 | return batadv_recv_icmp_ttl_exceeded(bat_priv, skb); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 328 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 329 | /* get routing information */ |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 330 | orig_node = batadv_orig_hash_find(bat_priv, icmph->dst); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 331 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 332 | goto out; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 333 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 334 | /* create a copy of the skb, if needed, to modify it. */ |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 335 | if (skb_cow(skb, ETH_HLEN) < 0) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 336 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 337 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 338 | icmph = (struct batadv_icmp_header *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 339 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 340 | /* decrement ttl */ |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 341 | icmph->ttl--; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 342 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 343 | /* route it */ |
Martin Hundebøll | e91ecfc | 2013-04-20 13:54:39 +0200 | [diff] [blame] | 344 | if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP) |
Martin Hundebøll | bb351ba | 2012-10-16 16:13:48 +0200 | [diff] [blame] | 345 | ret = NET_RX_SUCCESS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 346 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 347 | out: |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 348 | if (orig_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 349 | batadv_orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 350 | return ret; |
| 351 | } |
| 352 | |
Martin Hundebøll | f86ce0a | 2013-01-14 00:20:32 +0100 | [diff] [blame] | 353 | /** |
| 354 | * batadv_check_unicast_packet - Check for malformed unicast packets |
David S. Miller | 6e0895c | 2013-04-22 20:32:51 -0400 | [diff] [blame] | 355 | * @bat_priv: the bat priv with all the soft interface information |
Martin Hundebøll | f86ce0a | 2013-01-14 00:20:32 +0100 | [diff] [blame] | 356 | * @skb: packet to check |
| 357 | * @hdr_size: size of header to pull |
| 358 | * |
| 359 | * Check for short header and bad addresses in given packet. Returns negative |
| 360 | * value when check fails and 0 otherwise. The negative value depends on the |
| 361 | * reason: -ENODATA for bad header, -EBADR for broadcast destination or source, |
| 362 | * and -EREMOTE for non-local (other host) destination. |
| 363 | */ |
Antonio Quartulli | fe8a93b | 2013-04-03 19:10:26 +0200 | [diff] [blame] | 364 | static int batadv_check_unicast_packet(struct batadv_priv *bat_priv, |
| 365 | struct sk_buff *skb, int hdr_size) |
Martin Hundebøll | ff51fd7 | 2012-07-05 11:34:27 +0200 | [diff] [blame] | 366 | { |
| 367 | struct ethhdr *ethhdr; |
| 368 | |
| 369 | /* drop packet if it has not necessary minimum size */ |
| 370 | if (unlikely(!pskb_may_pull(skb, hdr_size))) |
Martin Hundebøll | f86ce0a | 2013-01-14 00:20:32 +0100 | [diff] [blame] | 371 | return -ENODATA; |
Martin Hundebøll | ff51fd7 | 2012-07-05 11:34:27 +0200 | [diff] [blame] | 372 | |
Antonio Quartulli | 7ed4be9 | 2013-04-08 15:08:18 +0200 | [diff] [blame] | 373 | ethhdr = eth_hdr(skb); |
Martin Hundebøll | ff51fd7 | 2012-07-05 11:34:27 +0200 | [diff] [blame] | 374 | |
| 375 | /* packet with unicast indication but broadcast recipient */ |
| 376 | if (is_broadcast_ether_addr(ethhdr->h_dest)) |
Martin Hundebøll | f86ce0a | 2013-01-14 00:20:32 +0100 | [diff] [blame] | 377 | return -EBADR; |
Martin Hundebøll | ff51fd7 | 2012-07-05 11:34:27 +0200 | [diff] [blame] | 378 | |
| 379 | /* packet with broadcast sender address */ |
| 380 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
Martin Hundebøll | f86ce0a | 2013-01-14 00:20:32 +0100 | [diff] [blame] | 381 | return -EBADR; |
Martin Hundebøll | ff51fd7 | 2012-07-05 11:34:27 +0200 | [diff] [blame] | 382 | |
| 383 | /* not for me */ |
Antonio Quartulli | fe8a93b | 2013-04-03 19:10:26 +0200 | [diff] [blame] | 384 | if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest)) |
Martin Hundebøll | f86ce0a | 2013-01-14 00:20:32 +0100 | [diff] [blame] | 385 | return -EREMOTE; |
Martin Hundebøll | ff51fd7 | 2012-07-05 11:34:27 +0200 | [diff] [blame] | 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
Simon Wunderlich | f6c8b71 | 2013-11-13 19:14:45 +0100 | [diff] [blame^] | 390 | /** |
| 391 | * batadv_find_router - find a suitable router for this originator |
| 392 | * @bat_priv: the bat priv with all the soft interface information |
| 393 | * @orig_node: the destination node |
| 394 | * @recv_if: pointer to interface this packet was received on |
| 395 | * |
| 396 | * Returns the router which should be used for this orig_node on |
| 397 | * this interface, or NULL if not available. |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 398 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 399 | struct batadv_neigh_node * |
| 400 | batadv_find_router(struct batadv_priv *bat_priv, |
| 401 | struct batadv_orig_node *orig_node, |
| 402 | const struct batadv_hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 403 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 404 | struct batadv_neigh_node *router; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 405 | |
| 406 | if (!orig_node) |
| 407 | return NULL; |
| 408 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 409 | router = batadv_orig_node_get_router(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 410 | |
Simon Wunderlich | f6c8b71 | 2013-11-13 19:14:45 +0100 | [diff] [blame^] | 411 | /* TODO: fill this later with new bonding mechanism */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 412 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 413 | return router; |
| 414 | } |
| 415 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 416 | static int batadv_route_unicast_packet(struct sk_buff *skb, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 417 | struct batadv_hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 418 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 419 | struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 420 | struct batadv_orig_node *orig_node = NULL; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 421 | struct batadv_unicast_packet *unicast_packet; |
Antonio Quartulli | 7ed4be9 | 2013-04-08 15:08:18 +0200 | [diff] [blame] | 422 | struct ethhdr *ethhdr = eth_hdr(skb); |
Simon Wunderlich | c54f38c | 2013-07-29 17:56:44 +0200 | [diff] [blame] | 423 | int res, hdr_len, ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 424 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 425 | unicast_packet = (struct batadv_unicast_packet *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 426 | |
| 427 | /* TTL exceeded */ |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 428 | if (unicast_packet->ttl < 2) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 429 | pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n", |
| 430 | ethhdr->h_source, unicast_packet->dest); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 431 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* get routing information */ |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 435 | orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 436 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 437 | if (!orig_node) |
Antonio Quartulli | b5a6f69 | 2011-04-16 11:30:57 +0200 | [diff] [blame] | 438 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 439 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 440 | /* create a copy of the skb, if needed, to modify it. */ |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 441 | if (skb_cow(skb, ETH_HLEN) < 0) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 442 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 443 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 444 | /* decrement ttl */ |
Martin Hundebøll | f097e25 | 2013-05-23 16:53:01 +0200 | [diff] [blame] | 445 | unicast_packet = (struct batadv_unicast_packet *)skb->data; |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 446 | unicast_packet->ttl--; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 447 | |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 448 | switch (unicast_packet->packet_type) { |
Simon Wunderlich | c54f38c | 2013-07-29 17:56:44 +0200 | [diff] [blame] | 449 | case BATADV_UNICAST_4ADDR: |
| 450 | hdr_len = sizeof(struct batadv_unicast_4addr_packet); |
| 451 | break; |
| 452 | case BATADV_UNICAST: |
| 453 | hdr_len = sizeof(struct batadv_unicast_packet); |
| 454 | break; |
| 455 | default: |
| 456 | /* other packet types not supported - yet */ |
| 457 | hdr_len = -1; |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | if (hdr_len > 0) |
| 462 | batadv_skb_set_priority(skb, hdr_len); |
| 463 | |
Martin Hundebøll | e91ecfc | 2013-04-20 13:54:39 +0200 | [diff] [blame] | 464 | res = batadv_send_skb_to_orig(skb, orig_node, recv_if); |
Martin Hundebøll | 9533247 | 2013-01-25 11:12:40 +0100 | [diff] [blame] | 465 | |
Martin Hundebøll | e91ecfc | 2013-04-20 13:54:39 +0200 | [diff] [blame] | 466 | /* translate transmit result into receive result */ |
| 467 | if (res == NET_XMIT_SUCCESS) { |
| 468 | /* skb was transmitted and consumed */ |
Martin Hundebøll | 9533247 | 2013-01-25 11:12:40 +0100 | [diff] [blame] | 469 | batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD); |
| 470 | batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES, |
| 471 | skb->len + ETH_HLEN); |
Martin Hundebøll | e91ecfc | 2013-04-20 13:54:39 +0200 | [diff] [blame] | 472 | |
| 473 | ret = NET_RX_SUCCESS; |
| 474 | } else if (res == NET_XMIT_POLICED) { |
| 475 | /* skb was buffered and consumed */ |
| 476 | ret = NET_RX_SUCCESS; |
Martin Hundebøll | 9533247 | 2013-01-25 11:12:40 +0100 | [diff] [blame] | 477 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 478 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 479 | out: |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 480 | if (orig_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 481 | batadv_orig_node_free_ref(orig_node); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 482 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 485 | /** |
| 486 | * batadv_reroute_unicast_packet - update the unicast header for re-routing |
| 487 | * @bat_priv: the bat priv with all the soft interface information |
| 488 | * @unicast_packet: the unicast header to be updated |
| 489 | * @dst_addr: the payload destination |
Antonio Quartulli | c018ad3 | 2013-06-04 12:11:39 +0200 | [diff] [blame] | 490 | * @vid: VLAN identifier |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 491 | * |
| 492 | * Search the translation table for dst_addr and update the unicast header with |
| 493 | * the new corresponding information (originator address where the destination |
| 494 | * client currently is and its known TTVN) |
| 495 | * |
| 496 | * Returns true if the packet header has been updated, false otherwise |
| 497 | */ |
| 498 | static bool |
| 499 | batadv_reroute_unicast_packet(struct batadv_priv *bat_priv, |
| 500 | struct batadv_unicast_packet *unicast_packet, |
Antonio Quartulli | c018ad3 | 2013-06-04 12:11:39 +0200 | [diff] [blame] | 501 | uint8_t *dst_addr, unsigned short vid) |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 502 | { |
| 503 | struct batadv_orig_node *orig_node = NULL; |
| 504 | struct batadv_hard_iface *primary_if = NULL; |
| 505 | bool ret = false; |
| 506 | uint8_t *orig_addr, orig_ttvn; |
| 507 | |
Antonio Quartulli | c018ad3 | 2013-06-04 12:11:39 +0200 | [diff] [blame] | 508 | if (batadv_is_my_client(bat_priv, dst_addr, vid)) { |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 509 | primary_if = batadv_primary_if_get_selected(bat_priv); |
| 510 | if (!primary_if) |
| 511 | goto out; |
| 512 | orig_addr = primary_if->net_dev->dev_addr; |
| 513 | orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn); |
| 514 | } else { |
Antonio Quartulli | c018ad3 | 2013-06-04 12:11:39 +0200 | [diff] [blame] | 515 | orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr, |
| 516 | vid); |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 517 | if (!orig_node) |
| 518 | goto out; |
| 519 | |
| 520 | if (batadv_compare_eth(orig_node->orig, unicast_packet->dest)) |
| 521 | goto out; |
| 522 | |
| 523 | orig_addr = orig_node->orig; |
| 524 | orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn); |
| 525 | } |
| 526 | |
| 527 | /* update the packet header */ |
| 528 | memcpy(unicast_packet->dest, orig_addr, ETH_ALEN); |
| 529 | unicast_packet->ttvn = orig_ttvn; |
| 530 | |
| 531 | ret = true; |
| 532 | out: |
| 533 | if (primary_if) |
| 534 | batadv_hardif_free_ref(primary_if); |
| 535 | if (orig_node) |
| 536 | batadv_orig_node_free_ref(orig_node); |
| 537 | |
| 538 | return ret; |
| 539 | } |
| 540 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 541 | static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv, |
Antonio Quartulli | dd981ab | 2013-04-03 10:14:20 +0200 | [diff] [blame] | 542 | struct sk_buff *skb, int hdr_len) { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 543 | struct batadv_unicast_packet *unicast_packet; |
Antonio Quartulli | c018ad3 | 2013-06-04 12:11:39 +0200 | [diff] [blame] | 544 | struct batadv_hard_iface *primary_if; |
| 545 | struct batadv_orig_node *orig_node; |
| 546 | uint8_t curr_ttvn, old_ttvn; |
| 547 | struct ethhdr *ethhdr; |
| 548 | unsigned short vid; |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 549 | int is_old_ttvn; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 550 | |
Antonio Quartulli | b8fcfa4 | 2012-08-26 23:25:59 +0200 | [diff] [blame] | 551 | /* check if there is enough data before accessing it */ |
Antonio Quartulli | dd981ab | 2013-04-03 10:14:20 +0200 | [diff] [blame] | 552 | if (pskb_may_pull(skb, hdr_len + ETH_HLEN) < 0) |
Antonio Quartulli | b8fcfa4 | 2012-08-26 23:25:59 +0200 | [diff] [blame] | 553 | return 0; |
| 554 | |
| 555 | /* create a copy of the skb (in case of for re-routing) to modify it. */ |
| 556 | if (skb_cow(skb, sizeof(*unicast_packet)) < 0) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 557 | return 0; |
| 558 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 559 | unicast_packet = (struct batadv_unicast_packet *)skb->data; |
Antonio Quartulli | c018ad3 | 2013-06-04 12:11:39 +0200 | [diff] [blame] | 560 | vid = batadv_get_vid(skb, hdr_len); |
Antonio Quartulli | dd981ab | 2013-04-03 10:14:20 +0200 | [diff] [blame] | 561 | ethhdr = (struct ethhdr *)(skb->data + hdr_len); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 562 | |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 563 | /* check if the destination client was served by this node and it is now |
| 564 | * roaming. In this case, it means that the node has got a ROAM_ADV |
| 565 | * message and that it knows the new destination in the mesh to re-route |
| 566 | * the packet to |
| 567 | */ |
Antonio Quartulli | c018ad3 | 2013-06-04 12:11:39 +0200 | [diff] [blame] | 568 | if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) { |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 569 | if (batadv_reroute_unicast_packet(bat_priv, unicast_packet, |
Antonio Quartulli | c018ad3 | 2013-06-04 12:11:39 +0200 | [diff] [blame] | 570 | ethhdr->h_dest, vid)) |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 571 | net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, |
| 572 | bat_priv, |
| 573 | "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n", |
| 574 | unicast_packet->dest, |
| 575 | ethhdr->h_dest); |
| 576 | /* at this point the mesh destination should have been |
| 577 | * substituted with the originator address found in the global |
| 578 | * table. If not, let the packet go untouched anyway because |
| 579 | * there is nothing the node can do |
| 580 | */ |
| 581 | return 1; |
| 582 | } |
| 583 | |
| 584 | /* retrieve the TTVN known by this node for the packet destination. This |
| 585 | * value is used later to check if the node which sent (or re-routed |
| 586 | * last time) the packet had an updated information or not |
| 587 | */ |
| 588 | curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn); |
Antonio Quartulli | fe8a93b | 2013-04-03 19:10:26 +0200 | [diff] [blame] | 589 | if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) { |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 590 | orig_node = batadv_orig_hash_find(bat_priv, |
| 591 | unicast_packet->dest); |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 592 | /* if it is not possible to find the orig_node representing the |
| 593 | * destination, the packet can immediately be dropped as it will |
| 594 | * not be possible to deliver it |
| 595 | */ |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 596 | if (!orig_node) |
| 597 | return 0; |
| 598 | |
| 599 | curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn); |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 600 | batadv_orig_node_free_ref(orig_node); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 601 | } |
| 602 | |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 603 | /* check if the TTVN contained in the packet is fresher than what the |
| 604 | * node knows |
| 605 | */ |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 606 | is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn); |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 607 | if (!is_old_ttvn) |
| 608 | return 1; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 609 | |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 610 | old_ttvn = unicast_packet->ttvn; |
| 611 | /* the packet was forged based on outdated network information. Its |
| 612 | * destination can possibly be updated and forwarded towards the new |
| 613 | * target host |
| 614 | */ |
| 615 | if (batadv_reroute_unicast_packet(bat_priv, unicast_packet, |
Antonio Quartulli | c018ad3 | 2013-06-04 12:11:39 +0200 | [diff] [blame] | 616 | ethhdr->h_dest, vid)) { |
Antonio Quartulli | 60c39c7 | 2012-08-26 01:05:56 +0200 | [diff] [blame] | 617 | net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv, |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 618 | "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n", |
| 619 | unicast_packet->dest, ethhdr->h_dest, |
| 620 | old_ttvn, curr_ttvn); |
| 621 | return 1; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 622 | } |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 623 | |
| 624 | /* the packet has not been re-routed: either the destination is |
| 625 | * currently served by this node or there is no destination at all and |
| 626 | * it is possible to drop the packet |
| 627 | */ |
Antonio Quartulli | c018ad3 | 2013-06-04 12:11:39 +0200 | [diff] [blame] | 628 | if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid)) |
Antonio Quartulli | 7c1fd91 | 2012-09-23 22:38:34 +0200 | [diff] [blame] | 629 | return 0; |
| 630 | |
| 631 | /* update the header in order to let the packet be delivered to this |
| 632 | * node's soft interface |
| 633 | */ |
| 634 | primary_if = batadv_primary_if_get_selected(bat_priv); |
| 635 | if (!primary_if) |
| 636 | return 0; |
| 637 | |
| 638 | memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN); |
| 639 | |
| 640 | batadv_hardif_free_ref(primary_if); |
| 641 | |
| 642 | unicast_packet->ttvn = curr_ttvn; |
| 643 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 644 | return 1; |
| 645 | } |
| 646 | |
Simon Wunderlich | a1f1ac5 | 2013-04-25 10:37:23 +0200 | [diff] [blame] | 647 | /** |
| 648 | * batadv_recv_unhandled_unicast_packet - receive and process packets which |
| 649 | * are in the unicast number space but not yet known to the implementation |
| 650 | * @skb: unicast tvlv packet to process |
| 651 | * @recv_if: pointer to interface this packet was received on |
| 652 | * |
| 653 | * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP |
| 654 | * otherwise. |
| 655 | */ |
| 656 | int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb, |
| 657 | struct batadv_hard_iface *recv_if) |
| 658 | { |
| 659 | struct batadv_unicast_packet *unicast_packet; |
| 660 | struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 661 | int check, hdr_size = sizeof(*unicast_packet); |
| 662 | |
| 663 | check = batadv_check_unicast_packet(bat_priv, skb, hdr_size); |
| 664 | if (check < 0) |
| 665 | return NET_RX_DROP; |
| 666 | |
| 667 | /* we don't know about this type, drop it. */ |
| 668 | unicast_packet = (struct batadv_unicast_packet *)skb->data; |
| 669 | if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) |
| 670 | return NET_RX_DROP; |
| 671 | |
| 672 | return batadv_route_unicast_packet(skb, recv_if); |
| 673 | } |
| 674 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 675 | int batadv_recv_unicast_packet(struct sk_buff *skb, |
| 676 | struct batadv_hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 677 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 678 | struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 679 | struct batadv_unicast_packet *unicast_packet; |
Martin Hundebøll | 4046b24 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 680 | struct batadv_unicast_4addr_packet *unicast_4addr_packet; |
Antonio Quartulli | 9affec6 | 2012-10-14 17:19:19 +0200 | [diff] [blame] | 681 | uint8_t *orig_addr; |
| 682 | struct batadv_orig_node *orig_node = NULL; |
Martin Hundebøll | 612d2b4 | 2013-01-25 11:12:42 +0100 | [diff] [blame] | 683 | int check, hdr_size = sizeof(*unicast_packet); |
Antonio Quartulli | c384ea3 | 2011-06-26 03:37:18 +0200 | [diff] [blame] | 684 | bool is4addr; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 685 | |
Antonio Quartulli | 7cdcf6d | 2012-10-01 09:57:35 +0200 | [diff] [blame] | 686 | unicast_packet = (struct batadv_unicast_packet *)skb->data; |
Martin Hundebøll | 4046b24 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 687 | unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data; |
Antonio Quartulli | 7cdcf6d | 2012-10-01 09:57:35 +0200 | [diff] [blame] | 688 | |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 689 | is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR; |
Antonio Quartulli | 7cdcf6d | 2012-10-01 09:57:35 +0200 | [diff] [blame] | 690 | /* the caller function should have already pulled 2 bytes */ |
Antonio Quartulli | c384ea3 | 2011-06-26 03:37:18 +0200 | [diff] [blame] | 691 | if (is4addr) |
Martin Hundebøll | 4046b24 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 692 | hdr_size = sizeof(*unicast_4addr_packet); |
Antonio Quartulli | 7cdcf6d | 2012-10-01 09:57:35 +0200 | [diff] [blame] | 693 | |
Martin Hundebøll | 612d2b4 | 2013-01-25 11:12:42 +0100 | [diff] [blame] | 694 | /* function returns -EREMOTE for promiscuous packets */ |
David S. Miller | 6e0895c | 2013-04-22 20:32:51 -0400 | [diff] [blame] | 695 | check = batadv_check_unicast_packet(bat_priv, skb, hdr_size); |
Martin Hundebøll | 612d2b4 | 2013-01-25 11:12:42 +0100 | [diff] [blame] | 696 | |
| 697 | /* Even though the packet is not for us, we might save it to use for |
| 698 | * decoding a later received coded packet |
| 699 | */ |
| 700 | if (check == -EREMOTE) |
| 701 | batadv_nc_skb_store_sniffed_unicast(bat_priv, skb); |
| 702 | |
| 703 | if (check < 0) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 704 | return NET_RX_DROP; |
Antonio Quartulli | dd981ab | 2013-04-03 10:14:20 +0200 | [diff] [blame] | 705 | if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size)) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 706 | return NET_RX_DROP; |
| 707 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 708 | /* packet for me */ |
Antonio Quartulli | fe8a93b | 2013-04-03 19:10:26 +0200 | [diff] [blame] | 709 | if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) { |
Antonio Quartulli | 9affec6 | 2012-10-14 17:19:19 +0200 | [diff] [blame] | 710 | if (is4addr) { |
Martin Hundebøll | 4046b24 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 711 | batadv_dat_inc_counter(bat_priv, |
| 712 | unicast_4addr_packet->subtype); |
Antonio Quartulli | 9affec6 | 2012-10-14 17:19:19 +0200 | [diff] [blame] | 713 | orig_addr = unicast_4addr_packet->src; |
| 714 | orig_node = batadv_orig_hash_find(bat_priv, orig_addr); |
| 715 | } |
Martin Hundebøll | 4046b24 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 716 | |
Antonio Quartulli | c384ea3 | 2011-06-26 03:37:18 +0200 | [diff] [blame] | 717 | if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, |
| 718 | hdr_size)) |
| 719 | goto rx_success; |
| 720 | if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, |
| 721 | hdr_size)) |
| 722 | goto rx_success; |
| 723 | |
Antonio Quartulli | 3713517 | 2012-07-05 23:38:30 +0200 | [diff] [blame] | 724 | batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size, |
Antonio Quartulli | 9affec6 | 2012-10-14 17:19:19 +0200 | [diff] [blame] | 725 | orig_node); |
Antonio Quartulli | 3713517 | 2012-07-05 23:38:30 +0200 | [diff] [blame] | 726 | |
Antonio Quartulli | c384ea3 | 2011-06-26 03:37:18 +0200 | [diff] [blame] | 727 | rx_success: |
Antonio Quartulli | 9affec6 | 2012-10-14 17:19:19 +0200 | [diff] [blame] | 728 | if (orig_node) |
| 729 | batadv_orig_node_free_ref(orig_node); |
| 730 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 731 | return NET_RX_SUCCESS; |
| 732 | } |
| 733 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 734 | return batadv_route_unicast_packet(skb, recv_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 737 | /** |
| 738 | * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets |
| 739 | * @skb: unicast tvlv packet to process |
| 740 | * @recv_if: pointer to interface this packet was received on |
| 741 | * @dst_addr: the payload destination |
| 742 | * |
| 743 | * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP |
| 744 | * otherwise. |
| 745 | */ |
| 746 | int batadv_recv_unicast_tvlv(struct sk_buff *skb, |
| 747 | struct batadv_hard_iface *recv_if) |
| 748 | { |
| 749 | struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 750 | struct batadv_unicast_tvlv_packet *unicast_tvlv_packet; |
| 751 | unsigned char *tvlv_buff; |
| 752 | uint16_t tvlv_buff_len; |
| 753 | int hdr_size = sizeof(*unicast_tvlv_packet); |
| 754 | int ret = NET_RX_DROP; |
| 755 | |
| 756 | if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0) |
| 757 | return NET_RX_DROP; |
| 758 | |
| 759 | /* the header is likely to be modified while forwarding */ |
| 760 | if (skb_cow(skb, hdr_size) < 0) |
| 761 | return NET_RX_DROP; |
| 762 | |
| 763 | /* packet needs to be linearized to access the tvlv content */ |
| 764 | if (skb_linearize(skb) < 0) |
| 765 | return NET_RX_DROP; |
| 766 | |
| 767 | unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data; |
| 768 | |
| 769 | tvlv_buff = (unsigned char *)(skb->data + hdr_size); |
| 770 | tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len); |
| 771 | |
| 772 | if (tvlv_buff_len > skb->len - hdr_size) |
| 773 | return NET_RX_DROP; |
| 774 | |
| 775 | ret = batadv_tvlv_containers_process(bat_priv, false, NULL, |
| 776 | unicast_tvlv_packet->src, |
| 777 | unicast_tvlv_packet->dst, |
| 778 | tvlv_buff, tvlv_buff_len); |
| 779 | |
| 780 | if (ret != NET_RX_SUCCESS) |
| 781 | ret = batadv_route_unicast_packet(skb, recv_if); |
| 782 | |
| 783 | return ret; |
| 784 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 785 | |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 786 | /** |
| 787 | * batadv_recv_frag_packet - process received fragment |
| 788 | * @skb: the received fragment |
| 789 | * @recv_if: interface that the skb is received on |
| 790 | * |
| 791 | * This function does one of the three following things: 1) Forward fragment, if |
| 792 | * the assembled packet will exceed our MTU; 2) Buffer fragment, if we till |
| 793 | * lack further fragments; 3) Merge fragments, if we have all needed parts. |
| 794 | * |
| 795 | * Return NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise. |
| 796 | */ |
| 797 | int batadv_recv_frag_packet(struct sk_buff *skb, |
| 798 | struct batadv_hard_iface *recv_if) |
| 799 | { |
| 800 | struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 801 | struct batadv_orig_node *orig_node_src = NULL; |
| 802 | struct batadv_frag_packet *frag_packet; |
| 803 | int ret = NET_RX_DROP; |
| 804 | |
| 805 | if (batadv_check_unicast_packet(bat_priv, skb, |
| 806 | sizeof(*frag_packet)) < 0) |
| 807 | goto out; |
| 808 | |
| 809 | frag_packet = (struct batadv_frag_packet *)skb->data; |
| 810 | orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig); |
| 811 | if (!orig_node_src) |
| 812 | goto out; |
| 813 | |
| 814 | /* Route the fragment if it is not for us and too big to be merged. */ |
| 815 | if (!batadv_is_my_mac(bat_priv, frag_packet->dest) && |
| 816 | batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) { |
| 817 | ret = NET_RX_SUCCESS; |
| 818 | goto out; |
| 819 | } |
| 820 | |
| 821 | batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX); |
| 822 | batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len); |
| 823 | |
| 824 | /* Add fragment to buffer and merge if possible. */ |
| 825 | if (!batadv_frag_skb_buffer(&skb, orig_node_src)) |
| 826 | goto out; |
| 827 | |
| 828 | /* Deliver merged packet to the appropriate handler, if it was |
| 829 | * merged |
| 830 | */ |
| 831 | if (skb) |
| 832 | batadv_batman_skb_recv(skb, recv_if->net_dev, |
| 833 | &recv_if->batman_adv_ptype, NULL); |
| 834 | |
| 835 | ret = NET_RX_SUCCESS; |
| 836 | |
| 837 | out: |
| 838 | if (orig_node_src) |
| 839 | batadv_orig_node_free_ref(orig_node_src); |
| 840 | |
| 841 | return ret; |
| 842 | } |
| 843 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 844 | int batadv_recv_bcast_packet(struct sk_buff *skb, |
| 845 | struct batadv_hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 846 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 847 | struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 848 | struct batadv_orig_node *orig_node = NULL; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 849 | struct batadv_bcast_packet *bcast_packet; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 850 | struct ethhdr *ethhdr; |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 851 | int hdr_size = sizeof(*bcast_packet); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 852 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 853 | int32_t seq_diff; |
Antonio Quartulli | 3fba732 | 2013-10-13 02:50:17 +0200 | [diff] [blame] | 854 | uint32_t seqno; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 855 | |
| 856 | /* drop packet if it has not necessary minimum size */ |
| 857 | if (unlikely(!pskb_may_pull(skb, hdr_size))) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 858 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 859 | |
Antonio Quartulli | 7ed4be9 | 2013-04-08 15:08:18 +0200 | [diff] [blame] | 860 | ethhdr = eth_hdr(skb); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 861 | |
| 862 | /* packet with broadcast indication but unicast recipient */ |
| 863 | if (!is_broadcast_ether_addr(ethhdr->h_dest)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 864 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 865 | |
| 866 | /* packet with broadcast sender address */ |
| 867 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 868 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 869 | |
| 870 | /* ignore broadcasts sent by myself */ |
Antonio Quartulli | fe8a93b | 2013-04-03 19:10:26 +0200 | [diff] [blame] | 871 | if (batadv_is_my_mac(bat_priv, ethhdr->h_source)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 872 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 873 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 874 | bcast_packet = (struct batadv_bcast_packet *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 875 | |
| 876 | /* ignore broadcasts originated by myself */ |
Antonio Quartulli | fe8a93b | 2013-04-03 19:10:26 +0200 | [diff] [blame] | 877 | if (batadv_is_my_mac(bat_priv, bcast_packet->orig)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 878 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 879 | |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 880 | if (bcast_packet->ttl < 2) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 881 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 882 | |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 883 | orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 884 | |
| 885 | if (!orig_node) |
Antonio Quartulli | b5a6f69 | 2011-04-16 11:30:57 +0200 | [diff] [blame] | 886 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 887 | |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 888 | spin_lock_bh(&orig_node->bcast_seqno_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 889 | |
Antonio Quartulli | 3fba732 | 2013-10-13 02:50:17 +0200 | [diff] [blame] | 890 | seqno = ntohl(bcast_packet->seqno); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 891 | /* check whether the packet is a duplicate */ |
Sven Eckelmann | 9b4a115 | 2012-05-12 13:48:53 +0200 | [diff] [blame] | 892 | if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno, |
Antonio Quartulli | 3fba732 | 2013-10-13 02:50:17 +0200 | [diff] [blame] | 893 | seqno)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 894 | goto spin_unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 895 | |
Antonio Quartulli | 3fba732 | 2013-10-13 02:50:17 +0200 | [diff] [blame] | 896 | seq_diff = seqno - orig_node->last_bcast_seqno; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 897 | |
| 898 | /* check whether the packet is old and the host just restarted. */ |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 899 | if (batadv_window_protected(bat_priv, seq_diff, |
| 900 | &orig_node->bcast_seqno_reset)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 901 | goto spin_unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 902 | |
| 903 | /* mark broadcast in flood history, update window position |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 904 | * if required. |
| 905 | */ |
Sven Eckelmann | 0f5f932 | 2012-05-12 02:09:25 +0200 | [diff] [blame] | 906 | if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1)) |
Antonio Quartulli | 3fba732 | 2013-10-13 02:50:17 +0200 | [diff] [blame] | 907 | orig_node->last_bcast_seqno = seqno; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 908 | |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 909 | spin_unlock_bh(&orig_node->bcast_seqno_lock); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 910 | |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 911 | /* check whether this has been sent by another originator before */ |
Simon Wunderlich | 004e86f | 2012-10-18 13:47:42 +0200 | [diff] [blame] | 912 | if (batadv_bla_check_bcast_duplist(bat_priv, skb)) |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 913 | goto out; |
| 914 | |
Simon Wunderlich | c54f38c | 2013-07-29 17:56:44 +0200 | [diff] [blame] | 915 | batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet)); |
| 916 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 917 | /* rebroadcast packet */ |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 918 | batadv_add_bcast_packet_to_list(bat_priv, skb, 1); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 919 | |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 920 | /* don't hand the broadcast up if it is from an originator |
| 921 | * from the same backbone. |
| 922 | */ |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 923 | if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 924 | goto out; |
| 925 | |
Antonio Quartulli | c384ea3 | 2011-06-26 03:37:18 +0200 | [diff] [blame] | 926 | if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size)) |
| 927 | goto rx_success; |
| 928 | if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size)) |
| 929 | goto rx_success; |
| 930 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 931 | /* broadcast for me */ |
Antonio Quartulli | 3713517 | 2012-07-05 23:38:30 +0200 | [diff] [blame] | 932 | batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size, |
| 933 | orig_node); |
Antonio Quartulli | c384ea3 | 2011-06-26 03:37:18 +0200 | [diff] [blame] | 934 | |
| 935 | rx_success: |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 936 | ret = NET_RX_SUCCESS; |
| 937 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 938 | |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 939 | spin_unlock: |
| 940 | spin_unlock_bh(&orig_node->bcast_seqno_lock); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 941 | out: |
| 942 | if (orig_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 943 | batadv_orig_node_free_ref(orig_node); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 944 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 945 | } |