Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1 | /* |
Sven Eckelmann | 567db7b | 2012-01-01 00:41:38 +0100 | [diff] [blame] | 2 | * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 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" |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 23 | #include "translation-table.h" |
| 24 | #include "ring_buffer.h" |
| 25 | #include "originator.h" |
| 26 | #include "routing.h" |
| 27 | #include "gateway_common.h" |
| 28 | #include "gateway_client.h" |
| 29 | #include "hard-interface.h" |
| 30 | #include "send.h" |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 31 | #include "bat_algo.h" |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 32 | |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 33 | static struct neigh_node *bat_iv_ogm_neigh_new(struct hard_iface *hard_iface, |
| 34 | const uint8_t *neigh_addr, |
| 35 | struct orig_node *orig_node, |
| 36 | struct orig_node *orig_neigh, |
| 37 | uint32_t seqno) |
| 38 | { |
| 39 | struct neigh_node *neigh_node; |
| 40 | |
| 41 | neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, seqno); |
| 42 | if (!neigh_node) |
| 43 | goto out; |
| 44 | |
| 45 | INIT_LIST_HEAD(&neigh_node->bonding_list); |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 46 | |
| 47 | neigh_node->orig_node = orig_neigh; |
| 48 | neigh_node->if_incoming = hard_iface; |
| 49 | |
| 50 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 51 | hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list); |
| 52 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 53 | |
| 54 | out: |
| 55 | return neigh_node; |
| 56 | } |
| 57 | |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 58 | static int bat_iv_ogm_iface_enable(struct hard_iface *hard_iface) |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 59 | { |
| 60 | struct batman_ogm_packet *batman_ogm_packet; |
Marek Lindner | d7d32ec | 2012-02-07 17:20:46 +0800 | [diff] [blame] | 61 | uint32_t random_seqno; |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 62 | int res = -1; |
Marek Lindner | d7d32ec | 2012-02-07 17:20:46 +0800 | [diff] [blame] | 63 | |
| 64 | /* randomize initial seqno to avoid collision */ |
| 65 | get_random_bytes(&random_seqno, sizeof(random_seqno)); |
| 66 | atomic_set(&hard_iface->seqno, random_seqno); |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 67 | |
Marek Lindner | 76e3d7f | 2012-02-07 17:20:50 +0800 | [diff] [blame] | 68 | hard_iface->packet_len = BATMAN_OGM_HLEN; |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 69 | hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC); |
| 70 | |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 71 | if (!hard_iface->packet_buff) |
| 72 | goto out; |
| 73 | |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 74 | batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff; |
Marek Lindner | 1eeb479 | 2012-02-07 17:20:51 +0800 | [diff] [blame] | 75 | batman_ogm_packet->header.packet_type = BAT_IV_OGM; |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 76 | batman_ogm_packet->header.version = COMPAT_VERSION; |
| 77 | batman_ogm_packet->header.ttl = 2; |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 78 | batman_ogm_packet->flags = NO_FLAGS; |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 79 | batman_ogm_packet->tq = TQ_MAX_VALUE; |
| 80 | batman_ogm_packet->tt_num_changes = 0; |
| 81 | batman_ogm_packet->ttvn = 0; |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 82 | |
| 83 | res = 0; |
| 84 | |
| 85 | out: |
| 86 | return res; |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Marek Lindner | 00a5007 | 2012-02-07 17:20:47 +0800 | [diff] [blame] | 89 | static void bat_iv_ogm_iface_disable(struct hard_iface *hard_iface) |
| 90 | { |
| 91 | kfree(hard_iface->packet_buff); |
| 92 | hard_iface->packet_buff = NULL; |
| 93 | } |
| 94 | |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 95 | static void bat_iv_ogm_iface_update_mac(struct hard_iface *hard_iface) |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 96 | { |
| 97 | struct batman_ogm_packet *batman_ogm_packet; |
| 98 | |
| 99 | batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff; |
| 100 | memcpy(batman_ogm_packet->orig, |
| 101 | hard_iface->net_dev->dev_addr, ETH_ALEN); |
| 102 | memcpy(batman_ogm_packet->prev_sender, |
| 103 | hard_iface->net_dev->dev_addr, ETH_ALEN); |
| 104 | } |
| 105 | |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 106 | static void bat_iv_ogm_primary_iface_set(struct hard_iface *hard_iface) |
| 107 | { |
| 108 | struct batman_ogm_packet *batman_ogm_packet; |
| 109 | |
| 110 | batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff; |
| 111 | batman_ogm_packet->flags = PRIMARIES_FIRST_HOP; |
| 112 | batman_ogm_packet->header.ttl = TTL; |
| 113 | } |
| 114 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 115 | /* when do we schedule our own ogm to be sent */ |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 116 | static unsigned long bat_iv_ogm_emit_send_time(const struct bat_priv *bat_priv) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 117 | { |
| 118 | return jiffies + msecs_to_jiffies( |
| 119 | atomic_read(&bat_priv->orig_interval) - |
| 120 | JITTER + (random32() % 2*JITTER)); |
| 121 | } |
| 122 | |
| 123 | /* when do we schedule a ogm packet to be sent */ |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 124 | static unsigned long bat_iv_ogm_fwd_send_time(void) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 125 | { |
| 126 | return jiffies + msecs_to_jiffies(random32() % (JITTER/2)); |
| 127 | } |
| 128 | |
| 129 | /* apply hop penalty for a normal link */ |
| 130 | static uint8_t hop_penalty(uint8_t tq, const struct bat_priv *bat_priv) |
| 131 | { |
| 132 | int hop_penalty = atomic_read(&bat_priv->hop_penalty); |
| 133 | return (tq * (TQ_MAX_VALUE - hop_penalty)) / (TQ_MAX_VALUE); |
| 134 | } |
| 135 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 136 | /* is there another aggregated packet here? */ |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 137 | static int bat_iv_ogm_aggr_packet(int buff_pos, int packet_len, |
| 138 | int tt_num_changes) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 139 | { |
Marek Lindner | 76e3d7f | 2012-02-07 17:20:50 +0800 | [diff] [blame] | 140 | int next_buff_pos = buff_pos + BATMAN_OGM_HLEN + tt_len(tt_num_changes); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 141 | |
| 142 | return (next_buff_pos <= packet_len) && |
| 143 | (next_buff_pos <= MAX_AGGREGATION_BYTES); |
| 144 | } |
| 145 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 146 | /* send a batman ogm to a given interface */ |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 147 | static void bat_iv_ogm_send_to_if(struct forw_packet *forw_packet, |
| 148 | struct hard_iface *hard_iface) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 149 | { |
| 150 | struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
| 151 | char *fwd_str; |
| 152 | uint8_t packet_num; |
| 153 | int16_t buff_pos; |
| 154 | struct batman_ogm_packet *batman_ogm_packet; |
| 155 | struct sk_buff *skb; |
| 156 | |
| 157 | if (hard_iface->if_status != IF_ACTIVE) |
| 158 | return; |
| 159 | |
| 160 | packet_num = 0; |
| 161 | buff_pos = 0; |
| 162 | batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data; |
| 163 | |
| 164 | /* adjust all flags and log packets */ |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 165 | while (bat_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len, |
| 166 | batman_ogm_packet->tt_num_changes)) { |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 167 | |
| 168 | /* we might have aggregated direct link packets with an |
| 169 | * ordinary base packet */ |
| 170 | if ((forw_packet->direct_link_flags & (1 << packet_num)) && |
| 171 | (forw_packet->if_incoming == hard_iface)) |
| 172 | batman_ogm_packet->flags |= DIRECTLINK; |
| 173 | else |
| 174 | batman_ogm_packet->flags &= ~DIRECTLINK; |
| 175 | |
| 176 | fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ? |
| 177 | "Sending own" : |
| 178 | "Forwarding")); |
| 179 | bat_dbg(DBG_BATMAN, bat_priv, |
Antonio Quartulli | c97c72b | 2012-02-26 15:39:41 +0100 | [diff] [blame] | 180 | "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s, ttvn %d) on interface %s [%pM]\n", |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 181 | fwd_str, (packet_num > 0 ? "aggregated " : ""), |
| 182 | batman_ogm_packet->orig, |
| 183 | ntohl(batman_ogm_packet->seqno), |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 184 | batman_ogm_packet->tq, batman_ogm_packet->header.ttl, |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 185 | (batman_ogm_packet->flags & DIRECTLINK ? |
| 186 | "on" : "off"), |
| 187 | batman_ogm_packet->ttvn, hard_iface->net_dev->name, |
| 188 | hard_iface->net_dev->dev_addr); |
| 189 | |
Marek Lindner | 76e3d7f | 2012-02-07 17:20:50 +0800 | [diff] [blame] | 190 | buff_pos += BATMAN_OGM_HLEN + |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 191 | tt_len(batman_ogm_packet->tt_num_changes); |
| 192 | packet_num++; |
| 193 | batman_ogm_packet = (struct batman_ogm_packet *) |
| 194 | (forw_packet->skb->data + buff_pos); |
| 195 | } |
| 196 | |
| 197 | /* create clone because function is called more than once */ |
| 198 | skb = skb_clone(forw_packet->skb, GFP_ATOMIC); |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame^] | 199 | if (skb) { |
| 200 | batadv_inc_counter(bat_priv, BAT_CNT_MGMT_TX); |
| 201 | batadv_add_counter(bat_priv, BAT_CNT_MGMT_TX_BYTES, |
| 202 | skb->len + ETH_HLEN); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 203 | send_skb_packet(skb, hard_iface, broadcast_addr); |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame^] | 204 | } |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* send a batman ogm packet */ |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 208 | static void bat_iv_ogm_emit(struct forw_packet *forw_packet) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 209 | { |
| 210 | struct hard_iface *hard_iface; |
| 211 | struct net_device *soft_iface; |
| 212 | struct bat_priv *bat_priv; |
| 213 | struct hard_iface *primary_if = NULL; |
| 214 | struct batman_ogm_packet *batman_ogm_packet; |
| 215 | unsigned char directlink; |
| 216 | |
| 217 | batman_ogm_packet = (struct batman_ogm_packet *) |
| 218 | (forw_packet->skb->data); |
| 219 | directlink = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0); |
| 220 | |
| 221 | if (!forw_packet->if_incoming) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 222 | pr_err("Error - can't forward packet: incoming iface not specified\n"); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 223 | goto out; |
| 224 | } |
| 225 | |
| 226 | soft_iface = forw_packet->if_incoming->soft_iface; |
| 227 | bat_priv = netdev_priv(soft_iface); |
| 228 | |
| 229 | if (forw_packet->if_incoming->if_status != IF_ACTIVE) |
| 230 | goto out; |
| 231 | |
| 232 | primary_if = primary_if_get_selected(bat_priv); |
| 233 | if (!primary_if) |
| 234 | goto out; |
| 235 | |
| 236 | /* multihomed peer assumed */ |
| 237 | /* non-primary OGMs are only broadcasted on their interface */ |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 238 | if ((directlink && (batman_ogm_packet->header.ttl == 1)) || |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 239 | (forw_packet->own && (forw_packet->if_incoming != primary_if))) { |
| 240 | |
| 241 | /* FIXME: what about aggregated packets ? */ |
| 242 | bat_dbg(DBG_BATMAN, bat_priv, |
Antonio Quartulli | c97c72b | 2012-02-26 15:39:41 +0100 | [diff] [blame] | 243 | "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n", |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 244 | (forw_packet->own ? "Sending own" : "Forwarding"), |
| 245 | batman_ogm_packet->orig, |
| 246 | ntohl(batman_ogm_packet->seqno), |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 247 | batman_ogm_packet->header.ttl, |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 248 | forw_packet->if_incoming->net_dev->name, |
| 249 | forw_packet->if_incoming->net_dev->dev_addr); |
| 250 | |
| 251 | /* skb is only used once and than forw_packet is free'd */ |
| 252 | send_skb_packet(forw_packet->skb, forw_packet->if_incoming, |
| 253 | broadcast_addr); |
| 254 | forw_packet->skb = NULL; |
| 255 | |
| 256 | goto out; |
| 257 | } |
| 258 | |
| 259 | /* broadcast on every interface */ |
| 260 | rcu_read_lock(); |
| 261 | list_for_each_entry_rcu(hard_iface, &hardif_list, list) { |
| 262 | if (hard_iface->soft_iface != soft_iface) |
| 263 | continue; |
| 264 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 265 | bat_iv_ogm_send_to_if(forw_packet, hard_iface); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 266 | } |
| 267 | rcu_read_unlock(); |
| 268 | |
| 269 | out: |
| 270 | if (primary_if) |
| 271 | hardif_free_ref(primary_if); |
| 272 | } |
| 273 | |
| 274 | /* return true if new_packet can be aggregated with forw_packet */ |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 275 | static bool bat_iv_ogm_can_aggregate(const struct batman_ogm_packet |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 276 | *new_batman_ogm_packet, |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 277 | struct bat_priv *bat_priv, |
| 278 | int packet_len, unsigned long send_time, |
| 279 | bool directlink, |
| 280 | const struct hard_iface *if_incoming, |
| 281 | const struct forw_packet *forw_packet) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 282 | { |
| 283 | struct batman_ogm_packet *batman_ogm_packet; |
| 284 | int aggregated_bytes = forw_packet->packet_len + packet_len; |
| 285 | struct hard_iface *primary_if = NULL; |
| 286 | bool res = false; |
| 287 | |
| 288 | batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data; |
| 289 | |
| 290 | /** |
| 291 | * we can aggregate the current packet to this aggregated packet |
| 292 | * if: |
| 293 | * |
| 294 | * - the send time is within our MAX_AGGREGATION_MS time |
| 295 | * - the resulting packet wont be bigger than |
| 296 | * MAX_AGGREGATION_BYTES |
| 297 | */ |
| 298 | |
| 299 | if (time_before(send_time, forw_packet->send_time) && |
| 300 | time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS), |
| 301 | forw_packet->send_time) && |
| 302 | (aggregated_bytes <= MAX_AGGREGATION_BYTES)) { |
| 303 | |
| 304 | /** |
| 305 | * check aggregation compatibility |
| 306 | * -> direct link packets are broadcasted on |
| 307 | * their interface only |
| 308 | * -> aggregate packet if the current packet is |
| 309 | * a "global" packet as well as the base |
| 310 | * packet |
| 311 | */ |
| 312 | |
| 313 | primary_if = primary_if_get_selected(bat_priv); |
| 314 | if (!primary_if) |
| 315 | goto out; |
| 316 | |
| 317 | /* packets without direct link flag and high TTL |
| 318 | * are flooded through the net */ |
| 319 | if ((!directlink) && |
| 320 | (!(batman_ogm_packet->flags & DIRECTLINK)) && |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 321 | (batman_ogm_packet->header.ttl != 1) && |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 322 | |
| 323 | /* own packets originating non-primary |
| 324 | * interfaces leave only that interface */ |
| 325 | ((!forw_packet->own) || |
| 326 | (forw_packet->if_incoming == primary_if))) { |
| 327 | res = true; |
| 328 | goto out; |
| 329 | } |
| 330 | |
| 331 | /* if the incoming packet is sent via this one |
| 332 | * interface only - we still can aggregate */ |
| 333 | if ((directlink) && |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 334 | (new_batman_ogm_packet->header.ttl == 1) && |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 335 | (forw_packet->if_incoming == if_incoming) && |
| 336 | |
| 337 | /* packets from direct neighbors or |
| 338 | * own secondary interface packets |
| 339 | * (= secondary interface packets in general) */ |
| 340 | (batman_ogm_packet->flags & DIRECTLINK || |
| 341 | (forw_packet->own && |
| 342 | forw_packet->if_incoming != primary_if))) { |
| 343 | res = true; |
| 344 | goto out; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | out: |
| 349 | if (primary_if) |
| 350 | hardif_free_ref(primary_if); |
| 351 | return res; |
| 352 | } |
| 353 | |
| 354 | /* create a new aggregated packet and add this packet to it */ |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 355 | static void bat_iv_ogm_aggregate_new(const unsigned char *packet_buff, |
| 356 | int packet_len, unsigned long send_time, |
| 357 | bool direct_link, |
| 358 | struct hard_iface *if_incoming, |
| 359 | int own_packet) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 360 | { |
| 361 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 362 | struct forw_packet *forw_packet_aggr; |
| 363 | unsigned char *skb_buff; |
| 364 | |
| 365 | if (!atomic_inc_not_zero(&if_incoming->refcount)) |
| 366 | return; |
| 367 | |
| 368 | /* own packet should always be scheduled */ |
| 369 | if (!own_packet) { |
| 370 | if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) { |
| 371 | bat_dbg(DBG_BATMAN, bat_priv, |
| 372 | "batman packet queue full\n"); |
| 373 | goto out; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC); |
| 378 | if (!forw_packet_aggr) { |
| 379 | if (!own_packet) |
| 380 | atomic_inc(&bat_priv->batman_queue_left); |
| 381 | goto out; |
| 382 | } |
| 383 | |
| 384 | if ((atomic_read(&bat_priv->aggregated_ogms)) && |
| 385 | (packet_len < MAX_AGGREGATION_BYTES)) |
| 386 | forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES + |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 387 | ETH_HLEN); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 388 | else |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 389 | forw_packet_aggr->skb = dev_alloc_skb(packet_len + ETH_HLEN); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 390 | |
| 391 | if (!forw_packet_aggr->skb) { |
| 392 | if (!own_packet) |
| 393 | atomic_inc(&bat_priv->batman_queue_left); |
| 394 | kfree(forw_packet_aggr); |
| 395 | goto out; |
| 396 | } |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 397 | skb_reserve(forw_packet_aggr->skb, ETH_HLEN); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 398 | |
| 399 | INIT_HLIST_NODE(&forw_packet_aggr->list); |
| 400 | |
| 401 | skb_buff = skb_put(forw_packet_aggr->skb, packet_len); |
| 402 | forw_packet_aggr->packet_len = packet_len; |
| 403 | memcpy(skb_buff, packet_buff, packet_len); |
| 404 | |
| 405 | forw_packet_aggr->own = own_packet; |
| 406 | forw_packet_aggr->if_incoming = if_incoming; |
| 407 | forw_packet_aggr->num_packets = 0; |
| 408 | forw_packet_aggr->direct_link_flags = NO_FLAGS; |
| 409 | forw_packet_aggr->send_time = send_time; |
| 410 | |
| 411 | /* save packet direct link flag status */ |
| 412 | if (direct_link) |
| 413 | forw_packet_aggr->direct_link_flags |= 1; |
| 414 | |
| 415 | /* add new packet to packet list */ |
| 416 | spin_lock_bh(&bat_priv->forw_bat_list_lock); |
| 417 | hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list); |
| 418 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 419 | |
| 420 | /* start timer for this packet */ |
| 421 | INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work, |
| 422 | send_outstanding_bat_ogm_packet); |
| 423 | queue_delayed_work(bat_event_workqueue, |
| 424 | &forw_packet_aggr->delayed_work, |
| 425 | send_time - jiffies); |
| 426 | |
| 427 | return; |
| 428 | out: |
| 429 | hardif_free_ref(if_incoming); |
| 430 | } |
| 431 | |
| 432 | /* aggregate a new packet into the existing ogm packet */ |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 433 | static void bat_iv_ogm_aggregate(struct forw_packet *forw_packet_aggr, |
| 434 | const unsigned char *packet_buff, |
| 435 | int packet_len, bool direct_link) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 436 | { |
| 437 | unsigned char *skb_buff; |
| 438 | |
| 439 | skb_buff = skb_put(forw_packet_aggr->skb, packet_len); |
| 440 | memcpy(skb_buff, packet_buff, packet_len); |
| 441 | forw_packet_aggr->packet_len += packet_len; |
| 442 | forw_packet_aggr->num_packets++; |
| 443 | |
| 444 | /* save packet direct link flag status */ |
| 445 | if (direct_link) |
| 446 | forw_packet_aggr->direct_link_flags |= |
| 447 | (1 << forw_packet_aggr->num_packets); |
| 448 | } |
| 449 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 450 | static void bat_iv_ogm_queue_add(struct bat_priv *bat_priv, |
| 451 | unsigned char *packet_buff, |
| 452 | int packet_len, struct hard_iface *if_incoming, |
| 453 | int own_packet, unsigned long send_time) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 454 | { |
| 455 | /** |
| 456 | * _aggr -> pointer to the packet we want to aggregate with |
| 457 | * _pos -> pointer to the position in the queue |
| 458 | */ |
| 459 | struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL; |
| 460 | struct hlist_node *tmp_node; |
| 461 | struct batman_ogm_packet *batman_ogm_packet; |
| 462 | bool direct_link; |
| 463 | |
| 464 | batman_ogm_packet = (struct batman_ogm_packet *)packet_buff; |
| 465 | direct_link = batman_ogm_packet->flags & DIRECTLINK ? 1 : 0; |
| 466 | |
| 467 | /* find position for the packet in the forward queue */ |
| 468 | spin_lock_bh(&bat_priv->forw_bat_list_lock); |
| 469 | /* own packets are not to be aggregated */ |
| 470 | if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) { |
| 471 | hlist_for_each_entry(forw_packet_pos, tmp_node, |
| 472 | &bat_priv->forw_bat_list, list) { |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 473 | if (bat_iv_ogm_can_aggregate(batman_ogm_packet, |
| 474 | bat_priv, packet_len, |
| 475 | send_time, direct_link, |
| 476 | if_incoming, |
| 477 | forw_packet_pos)) { |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 478 | forw_packet_aggr = forw_packet_pos; |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /* nothing to aggregate with - either aggregation disabled or no |
| 485 | * suitable aggregation packet found */ |
| 486 | if (!forw_packet_aggr) { |
| 487 | /* the following section can run without the lock */ |
| 488 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 489 | |
| 490 | /** |
| 491 | * if we could not aggregate this packet with one of the others |
| 492 | * we hold it back for a while, so that it might be aggregated |
| 493 | * later on |
| 494 | */ |
| 495 | if ((!own_packet) && |
| 496 | (atomic_read(&bat_priv->aggregated_ogms))) |
| 497 | send_time += msecs_to_jiffies(MAX_AGGREGATION_MS); |
| 498 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 499 | bat_iv_ogm_aggregate_new(packet_buff, packet_len, |
| 500 | send_time, direct_link, |
| 501 | if_incoming, own_packet); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 502 | } else { |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 503 | bat_iv_ogm_aggregate(forw_packet_aggr, packet_buff, |
| 504 | packet_len, direct_link); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 505 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 506 | } |
| 507 | } |
| 508 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 509 | static void bat_iv_ogm_forward(struct orig_node *orig_node, |
| 510 | const struct ethhdr *ethhdr, |
| 511 | struct batman_ogm_packet *batman_ogm_packet, |
Marek Lindner | 75cd33f | 2012-03-01 15:35:16 +0800 | [diff] [blame] | 512 | bool is_single_hop_neigh, |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 513 | bool is_from_best_next_hop, |
Marek Lindner | 75cd33f | 2012-03-01 15:35:16 +0800 | [diff] [blame] | 514 | struct hard_iface *if_incoming) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 515 | { |
| 516 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 517 | uint8_t tt_num_changes; |
| 518 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 519 | if (batman_ogm_packet->header.ttl <= 1) { |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 520 | bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n"); |
| 521 | return; |
| 522 | } |
| 523 | |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 524 | if (!is_from_best_next_hop) { |
| 525 | /* Mark the forwarded packet when it is not coming from our |
| 526 | * best next hop. We still need to forward the packet for our |
| 527 | * neighbor link quality detection to work in case the packet |
| 528 | * originated from a single hop neighbor. Otherwise we can |
| 529 | * simply drop the ogm. |
| 530 | */ |
| 531 | if (is_single_hop_neigh) |
| 532 | batman_ogm_packet->flags |= NOT_BEST_NEXT_HOP; |
| 533 | else |
| 534 | return; |
| 535 | } |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 536 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 537 | tt_num_changes = batman_ogm_packet->tt_num_changes; |
| 538 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 539 | batman_ogm_packet->header.ttl--; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 540 | memcpy(batman_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN); |
| 541 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 542 | /* apply hop penalty */ |
| 543 | batman_ogm_packet->tq = hop_penalty(batman_ogm_packet->tq, bat_priv); |
| 544 | |
| 545 | bat_dbg(DBG_BATMAN, bat_priv, |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 546 | "Forwarding packet: tq: %i, ttl: %i\n", |
| 547 | batman_ogm_packet->tq, batman_ogm_packet->header.ttl); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 548 | |
| 549 | batman_ogm_packet->seqno = htonl(batman_ogm_packet->seqno); |
| 550 | batman_ogm_packet->tt_crc = htons(batman_ogm_packet->tt_crc); |
| 551 | |
| 552 | /* switch of primaries first hop flag when forwarding */ |
| 553 | batman_ogm_packet->flags &= ~PRIMARIES_FIRST_HOP; |
Marek Lindner | 75cd33f | 2012-03-01 15:35:16 +0800 | [diff] [blame] | 554 | if (is_single_hop_neigh) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 555 | batman_ogm_packet->flags |= DIRECTLINK; |
| 556 | else |
| 557 | batman_ogm_packet->flags &= ~DIRECTLINK; |
| 558 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 559 | bat_iv_ogm_queue_add(bat_priv, (unsigned char *)batman_ogm_packet, |
Marek Lindner | 76e3d7f | 2012-02-07 17:20:50 +0800 | [diff] [blame] | 560 | BATMAN_OGM_HLEN + tt_len(tt_num_changes), |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 561 | if_incoming, 0, bat_iv_ogm_fwd_send_time()); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 562 | } |
| 563 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 564 | static void bat_iv_ogm_schedule(struct hard_iface *hard_iface, |
| 565 | int tt_num_changes) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 566 | { |
| 567 | struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
| 568 | struct batman_ogm_packet *batman_ogm_packet; |
| 569 | struct hard_iface *primary_if; |
| 570 | int vis_server; |
| 571 | |
| 572 | vis_server = atomic_read(&bat_priv->vis_mode); |
| 573 | primary_if = primary_if_get_selected(bat_priv); |
| 574 | |
| 575 | batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff; |
| 576 | |
| 577 | /* change sequence number to network order */ |
| 578 | batman_ogm_packet->seqno = |
| 579 | htonl((uint32_t)atomic_read(&hard_iface->seqno)); |
| 580 | |
| 581 | batman_ogm_packet->ttvn = atomic_read(&bat_priv->ttvn); |
Antonio Quartulli | 66a1b2b | 2012-04-14 13:15:26 +0200 | [diff] [blame] | 582 | batman_ogm_packet->tt_crc = htons(bat_priv->tt_crc); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 583 | if (tt_num_changes >= 0) |
| 584 | batman_ogm_packet->tt_num_changes = tt_num_changes; |
| 585 | |
| 586 | if (vis_server == VIS_TYPE_SERVER_SYNC) |
| 587 | batman_ogm_packet->flags |= VIS_SERVER; |
| 588 | else |
| 589 | batman_ogm_packet->flags &= ~VIS_SERVER; |
| 590 | |
| 591 | if ((hard_iface == primary_if) && |
| 592 | (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER)) |
| 593 | batman_ogm_packet->gw_flags = |
| 594 | (uint8_t)atomic_read(&bat_priv->gw_bandwidth); |
| 595 | else |
| 596 | batman_ogm_packet->gw_flags = NO_FLAGS; |
| 597 | |
| 598 | atomic_inc(&hard_iface->seqno); |
| 599 | |
| 600 | slide_own_bcast_window(hard_iface); |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 601 | bat_iv_ogm_queue_add(bat_priv, hard_iface->packet_buff, |
| 602 | hard_iface->packet_len, hard_iface, 1, |
| 603 | bat_iv_ogm_emit_send_time(bat_priv)); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 604 | |
| 605 | if (primary_if) |
| 606 | hardif_free_ref(primary_if); |
| 607 | } |
| 608 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 609 | static void bat_iv_ogm_orig_update(struct bat_priv *bat_priv, |
| 610 | struct orig_node *orig_node, |
| 611 | const struct ethhdr *ethhdr, |
| 612 | const struct batman_ogm_packet |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 613 | *batman_ogm_packet, |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 614 | struct hard_iface *if_incoming, |
| 615 | const unsigned char *tt_buff, |
| 616 | int is_duplicate) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 617 | { |
| 618 | struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL; |
| 619 | struct neigh_node *router = NULL; |
| 620 | struct orig_node *orig_node_tmp; |
| 621 | struct hlist_node *node; |
| 622 | uint8_t bcast_own_sum_orig, bcast_own_sum_neigh; |
| 623 | |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 624 | bat_dbg(DBG_BATMAN, bat_priv, |
| 625 | "update_originator(): Searching and updating originator entry of received packet\n"); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 626 | |
| 627 | rcu_read_lock(); |
| 628 | hlist_for_each_entry_rcu(tmp_neigh_node, node, |
| 629 | &orig_node->neigh_list, list) { |
| 630 | if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) && |
| 631 | (tmp_neigh_node->if_incoming == if_incoming) && |
| 632 | atomic_inc_not_zero(&tmp_neigh_node->refcount)) { |
| 633 | if (neigh_node) |
| 634 | neigh_node_free_ref(neigh_node); |
| 635 | neigh_node = tmp_neigh_node; |
| 636 | continue; |
| 637 | } |
| 638 | |
| 639 | if (is_duplicate) |
| 640 | continue; |
| 641 | |
Marek Lindner | e3b0d0d | 2012-03-17 15:28:32 +0800 | [diff] [blame] | 642 | spin_lock_bh(&tmp_neigh_node->lq_update_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 643 | ring_buffer_set(tmp_neigh_node->tq_recv, |
| 644 | &tmp_neigh_node->tq_index, 0); |
| 645 | tmp_neigh_node->tq_avg = |
| 646 | ring_buffer_avg(tmp_neigh_node->tq_recv); |
Marek Lindner | e3b0d0d | 2012-03-17 15:28:32 +0800 | [diff] [blame] | 647 | spin_unlock_bh(&tmp_neigh_node->lq_update_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | if (!neigh_node) { |
| 651 | struct orig_node *orig_tmp; |
| 652 | |
| 653 | orig_tmp = get_orig_node(bat_priv, ethhdr->h_source); |
| 654 | if (!orig_tmp) |
| 655 | goto unlock; |
| 656 | |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 657 | neigh_node = bat_iv_ogm_neigh_new(if_incoming, ethhdr->h_source, |
| 658 | orig_node, orig_tmp, |
| 659 | batman_ogm_packet->seqno); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 660 | |
| 661 | orig_node_free_ref(orig_tmp); |
| 662 | if (!neigh_node) |
| 663 | goto unlock; |
| 664 | } else |
| 665 | bat_dbg(DBG_BATMAN, bat_priv, |
| 666 | "Updating existing last-hop neighbor of originator\n"); |
| 667 | |
| 668 | rcu_read_unlock(); |
| 669 | |
| 670 | orig_node->flags = batman_ogm_packet->flags; |
Marek Lindner | d7b2a97 | 2012-03-01 15:35:19 +0800 | [diff] [blame] | 671 | neigh_node->last_seen = jiffies; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 672 | |
Marek Lindner | e3b0d0d | 2012-03-17 15:28:32 +0800 | [diff] [blame] | 673 | spin_lock_bh(&neigh_node->lq_update_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 674 | ring_buffer_set(neigh_node->tq_recv, |
| 675 | &neigh_node->tq_index, |
| 676 | batman_ogm_packet->tq); |
| 677 | neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv); |
Marek Lindner | e3b0d0d | 2012-03-17 15:28:32 +0800 | [diff] [blame] | 678 | spin_unlock_bh(&neigh_node->lq_update_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 679 | |
| 680 | if (!is_duplicate) { |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 681 | orig_node->last_ttl = batman_ogm_packet->header.ttl; |
| 682 | neigh_node->last_ttl = batman_ogm_packet->header.ttl; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | bonding_candidate_add(orig_node, neigh_node); |
| 686 | |
| 687 | /* if this neighbor already is our next hop there is nothing |
| 688 | * to change */ |
| 689 | router = orig_node_get_router(orig_node); |
| 690 | if (router == neigh_node) |
| 691 | goto update_tt; |
| 692 | |
| 693 | /* if this neighbor does not offer a better TQ we won't consider it */ |
| 694 | if (router && (router->tq_avg > neigh_node->tq_avg)) |
| 695 | goto update_tt; |
| 696 | |
| 697 | /* if the TQ is the same and the link not more symmetric we |
| 698 | * won't consider it either */ |
| 699 | if (router && (neigh_node->tq_avg == router->tq_avg)) { |
| 700 | orig_node_tmp = router->orig_node; |
| 701 | spin_lock_bh(&orig_node_tmp->ogm_cnt_lock); |
| 702 | bcast_own_sum_orig = |
| 703 | orig_node_tmp->bcast_own_sum[if_incoming->if_num]; |
| 704 | spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock); |
| 705 | |
| 706 | orig_node_tmp = neigh_node->orig_node; |
| 707 | spin_lock_bh(&orig_node_tmp->ogm_cnt_lock); |
| 708 | bcast_own_sum_neigh = |
| 709 | orig_node_tmp->bcast_own_sum[if_incoming->if_num]; |
| 710 | spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock); |
| 711 | |
| 712 | if (bcast_own_sum_orig >= bcast_own_sum_neigh) |
| 713 | goto update_tt; |
| 714 | } |
| 715 | |
| 716 | update_route(bat_priv, orig_node, neigh_node); |
| 717 | |
| 718 | update_tt: |
| 719 | /* I have to check for transtable changes only if the OGM has been |
| 720 | * sent through a primary interface */ |
| 721 | if (((batman_ogm_packet->orig != ethhdr->h_source) && |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 722 | (batman_ogm_packet->header.ttl > 2)) || |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 723 | (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP)) |
| 724 | tt_update_orig(bat_priv, orig_node, tt_buff, |
| 725 | batman_ogm_packet->tt_num_changes, |
| 726 | batman_ogm_packet->ttvn, |
| 727 | batman_ogm_packet->tt_crc); |
| 728 | |
| 729 | if (orig_node->gw_flags != batman_ogm_packet->gw_flags) |
| 730 | gw_node_update(bat_priv, orig_node, |
| 731 | batman_ogm_packet->gw_flags); |
| 732 | |
| 733 | orig_node->gw_flags = batman_ogm_packet->gw_flags; |
| 734 | |
| 735 | /* restart gateway selection if fast or late switching was enabled */ |
| 736 | if ((orig_node->gw_flags) && |
| 737 | (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) && |
| 738 | (atomic_read(&bat_priv->gw_sel_class) > 2)) |
| 739 | gw_check_election(bat_priv, orig_node); |
| 740 | |
| 741 | goto out; |
| 742 | |
| 743 | unlock: |
| 744 | rcu_read_unlock(); |
| 745 | out: |
| 746 | if (neigh_node) |
| 747 | neigh_node_free_ref(neigh_node); |
| 748 | if (router) |
| 749 | neigh_node_free_ref(router); |
| 750 | } |
| 751 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 752 | static int bat_iv_ogm_calc_tq(struct orig_node *orig_node, |
| 753 | struct orig_node *orig_neigh_node, |
| 754 | struct batman_ogm_packet *batman_ogm_packet, |
| 755 | struct hard_iface *if_incoming) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 756 | { |
| 757 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 758 | struct neigh_node *neigh_node = NULL, *tmp_neigh_node; |
| 759 | struct hlist_node *node; |
| 760 | uint8_t total_count; |
| 761 | uint8_t orig_eq_count, neigh_rq_count, tq_own; |
| 762 | int tq_asym_penalty, ret = 0; |
| 763 | |
| 764 | /* find corresponding one hop neighbor */ |
| 765 | rcu_read_lock(); |
| 766 | hlist_for_each_entry_rcu(tmp_neigh_node, node, |
| 767 | &orig_neigh_node->neigh_list, list) { |
| 768 | |
| 769 | if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig)) |
| 770 | continue; |
| 771 | |
| 772 | if (tmp_neigh_node->if_incoming != if_incoming) |
| 773 | continue; |
| 774 | |
| 775 | if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) |
| 776 | continue; |
| 777 | |
| 778 | neigh_node = tmp_neigh_node; |
| 779 | break; |
| 780 | } |
| 781 | rcu_read_unlock(); |
| 782 | |
| 783 | if (!neigh_node) |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 784 | neigh_node = bat_iv_ogm_neigh_new(if_incoming, |
| 785 | orig_neigh_node->orig, |
| 786 | orig_neigh_node, |
| 787 | orig_neigh_node, |
| 788 | batman_ogm_packet->seqno); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 789 | |
| 790 | if (!neigh_node) |
| 791 | goto out; |
| 792 | |
Marek Lindner | d7b2a97 | 2012-03-01 15:35:19 +0800 | [diff] [blame] | 793 | /* if orig_node is direct neighbor update neigh_node last_seen */ |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 794 | if (orig_node == orig_neigh_node) |
Marek Lindner | d7b2a97 | 2012-03-01 15:35:19 +0800 | [diff] [blame] | 795 | neigh_node->last_seen = jiffies; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 796 | |
Marek Lindner | d7b2a97 | 2012-03-01 15:35:19 +0800 | [diff] [blame] | 797 | orig_node->last_seen = jiffies; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 798 | |
| 799 | /* find packet count of corresponding one hop neighbor */ |
| 800 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
| 801 | orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num]; |
| 802 | neigh_rq_count = neigh_node->real_packet_count; |
| 803 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
| 804 | |
| 805 | /* pay attention to not get a value bigger than 100 % */ |
| 806 | total_count = (orig_eq_count > neigh_rq_count ? |
| 807 | neigh_rq_count : orig_eq_count); |
| 808 | |
| 809 | /* if we have too few packets (too less data) we set tq_own to zero */ |
| 810 | /* if we receive too few packets it is not considered bidirectional */ |
| 811 | if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) || |
| 812 | (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM)) |
| 813 | tq_own = 0; |
| 814 | else |
| 815 | /* neigh_node->real_packet_count is never zero as we |
| 816 | * only purge old information when getting new |
| 817 | * information */ |
| 818 | tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count; |
| 819 | |
Sven Eckelmann | 21a1236 | 2012-03-07 09:07:46 +0100 | [diff] [blame] | 820 | /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 821 | * affect the nearly-symmetric links only a little, but |
| 822 | * punishes asymmetric links more. This will give a value |
| 823 | * between 0 and TQ_MAX_VALUE |
| 824 | */ |
| 825 | tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE * |
| 826 | (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) * |
| 827 | (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) * |
| 828 | (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) / |
| 829 | (TQ_LOCAL_WINDOW_SIZE * |
| 830 | TQ_LOCAL_WINDOW_SIZE * |
| 831 | TQ_LOCAL_WINDOW_SIZE); |
| 832 | |
| 833 | batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own |
| 834 | * tq_asym_penalty) / |
| 835 | (TQ_MAX_VALUE * TQ_MAX_VALUE)); |
| 836 | |
| 837 | bat_dbg(DBG_BATMAN, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 838 | "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n", |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 839 | orig_node->orig, orig_neigh_node->orig, total_count, |
| 840 | neigh_rq_count, tq_own, tq_asym_penalty, batman_ogm_packet->tq); |
| 841 | |
| 842 | /* if link has the minimum required transmission quality |
| 843 | * consider it bidirectional */ |
| 844 | if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT) |
| 845 | ret = 1; |
| 846 | |
| 847 | out: |
| 848 | if (neigh_node) |
| 849 | neigh_node_free_ref(neigh_node); |
| 850 | return ret; |
| 851 | } |
| 852 | |
| 853 | /* processes a batman packet for all interfaces, adjusts the sequence number and |
| 854 | * finds out whether it is a duplicate. |
| 855 | * returns: |
| 856 | * 1 the packet is a duplicate |
| 857 | * 0 the packet has not yet been received |
| 858 | * -1 the packet is old and has been received while the seqno window |
| 859 | * was protected. Caller should drop it. |
| 860 | */ |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 861 | static int bat_iv_ogm_update_seqnos(const struct ethhdr *ethhdr, |
| 862 | const struct batman_ogm_packet |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 863 | *batman_ogm_packet, |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 864 | const struct hard_iface *if_incoming) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 865 | { |
| 866 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 867 | struct orig_node *orig_node; |
| 868 | struct neigh_node *tmp_neigh_node; |
| 869 | struct hlist_node *node; |
| 870 | int is_duplicate = 0; |
| 871 | int32_t seq_diff; |
| 872 | int need_update = 0; |
| 873 | int set_mark, ret = -1; |
| 874 | |
| 875 | orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig); |
| 876 | if (!orig_node) |
| 877 | return 0; |
| 878 | |
| 879 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
| 880 | seq_diff = batman_ogm_packet->seqno - orig_node->last_real_seqno; |
| 881 | |
| 882 | /* signalize caller that the packet is to be dropped. */ |
Antonio Quartulli | 1e5cc26 | 2012-02-26 15:39:42 +0100 | [diff] [blame] | 883 | if (!hlist_empty(&orig_node->neigh_list) && |
| 884 | window_protected(bat_priv, seq_diff, |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 885 | &orig_node->batman_seqno_reset)) |
| 886 | goto out; |
| 887 | |
| 888 | rcu_read_lock(); |
| 889 | hlist_for_each_entry_rcu(tmp_neigh_node, node, |
| 890 | &orig_node->neigh_list, list) { |
| 891 | |
Sven Eckelmann | 0079d2c | 2012-02-04 17:34:52 +0100 | [diff] [blame] | 892 | is_duplicate |= bat_test_bit(tmp_neigh_node->real_bits, |
| 893 | orig_node->last_real_seqno, |
| 894 | batman_ogm_packet->seqno); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 895 | |
| 896 | if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) && |
| 897 | (tmp_neigh_node->if_incoming == if_incoming)) |
| 898 | set_mark = 1; |
| 899 | else |
| 900 | set_mark = 0; |
| 901 | |
| 902 | /* if the window moved, set the update flag. */ |
| 903 | need_update |= bit_get_packet(bat_priv, |
| 904 | tmp_neigh_node->real_bits, |
| 905 | seq_diff, set_mark); |
| 906 | |
| 907 | tmp_neigh_node->real_packet_count = |
Sven Eckelmann | 0079d2c | 2012-02-04 17:34:52 +0100 | [diff] [blame] | 908 | bitmap_weight(tmp_neigh_node->real_bits, |
| 909 | TQ_LOCAL_WINDOW_SIZE); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 910 | } |
| 911 | rcu_read_unlock(); |
| 912 | |
| 913 | if (need_update) { |
| 914 | bat_dbg(DBG_BATMAN, bat_priv, |
Antonio Quartulli | c97c72b | 2012-02-26 15:39:41 +0100 | [diff] [blame] | 915 | "updating last_seqno: old %u, new %u\n", |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 916 | orig_node->last_real_seqno, batman_ogm_packet->seqno); |
| 917 | orig_node->last_real_seqno = batman_ogm_packet->seqno; |
| 918 | } |
| 919 | |
| 920 | ret = is_duplicate; |
| 921 | |
| 922 | out: |
| 923 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
| 924 | orig_node_free_ref(orig_node); |
| 925 | return ret; |
| 926 | } |
| 927 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 928 | static void bat_iv_ogm_process(const struct ethhdr *ethhdr, |
| 929 | struct batman_ogm_packet *batman_ogm_packet, |
| 930 | const unsigned char *tt_buff, |
| 931 | struct hard_iface *if_incoming) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 932 | { |
| 933 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 934 | struct hard_iface *hard_iface; |
| 935 | struct orig_node *orig_neigh_node, *orig_node; |
| 936 | struct neigh_node *router = NULL, *router_router = NULL; |
| 937 | struct neigh_node *orig_neigh_router = NULL; |
| 938 | int has_directlink_flag; |
| 939 | int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0; |
Marek Lindner | 75cd33f | 2012-03-01 15:35:16 +0800 | [diff] [blame] | 940 | int is_broadcast = 0, is_bidirectional; |
| 941 | bool is_single_hop_neigh = false; |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 942 | bool is_from_best_next_hop = false; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 943 | int is_duplicate; |
| 944 | uint32_t if_incoming_seqno; |
| 945 | |
| 946 | /* Silently drop when the batman packet is actually not a |
| 947 | * correct packet. |
| 948 | * |
| 949 | * This might happen if a packet is padded (e.g. Ethernet has a |
| 950 | * minimum frame length of 64 byte) and the aggregation interprets |
| 951 | * it as an additional length. |
| 952 | * |
| 953 | * TODO: A more sane solution would be to have a bit in the |
| 954 | * batman_ogm_packet to detect whether the packet is the last |
| 955 | * packet in an aggregation. Here we expect that the padding |
| 956 | * is always zero (or not 0x01) |
| 957 | */ |
Marek Lindner | 1eeb479 | 2012-02-07 17:20:51 +0800 | [diff] [blame] | 958 | if (batman_ogm_packet->header.packet_type != BAT_IV_OGM) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 959 | return; |
| 960 | |
| 961 | /* could be changed by schedule_own_packet() */ |
| 962 | if_incoming_seqno = atomic_read(&if_incoming->seqno); |
| 963 | |
| 964 | has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0); |
| 965 | |
Marek Lindner | 75cd33f | 2012-03-01 15:35:16 +0800 | [diff] [blame] | 966 | if (compare_eth(ethhdr->h_source, batman_ogm_packet->orig)) |
| 967 | is_single_hop_neigh = true; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 968 | |
| 969 | bat_dbg(DBG_BATMAN, bat_priv, |
Antonio Quartulli | c97c72b | 2012-02-26 15:39:41 +0100 | [diff] [blame] | 970 | "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, ttvn %u, crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n", |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 971 | ethhdr->h_source, if_incoming->net_dev->name, |
| 972 | if_incoming->net_dev->dev_addr, batman_ogm_packet->orig, |
| 973 | batman_ogm_packet->prev_sender, batman_ogm_packet->seqno, |
| 974 | batman_ogm_packet->ttvn, batman_ogm_packet->tt_crc, |
| 975 | batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq, |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 976 | batman_ogm_packet->header.ttl, |
| 977 | batman_ogm_packet->header.version, has_directlink_flag); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 978 | |
| 979 | rcu_read_lock(); |
| 980 | list_for_each_entry_rcu(hard_iface, &hardif_list, list) { |
| 981 | if (hard_iface->if_status != IF_ACTIVE) |
| 982 | continue; |
| 983 | |
| 984 | if (hard_iface->soft_iface != if_incoming->soft_iface) |
| 985 | continue; |
| 986 | |
| 987 | if (compare_eth(ethhdr->h_source, |
| 988 | hard_iface->net_dev->dev_addr)) |
| 989 | is_my_addr = 1; |
| 990 | |
| 991 | if (compare_eth(batman_ogm_packet->orig, |
| 992 | hard_iface->net_dev->dev_addr)) |
| 993 | is_my_orig = 1; |
| 994 | |
| 995 | if (compare_eth(batman_ogm_packet->prev_sender, |
| 996 | hard_iface->net_dev->dev_addr)) |
| 997 | is_my_oldorig = 1; |
| 998 | |
| 999 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
| 1000 | is_broadcast = 1; |
| 1001 | } |
| 1002 | rcu_read_unlock(); |
| 1003 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1004 | if (batman_ogm_packet->header.version != COMPAT_VERSION) { |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1005 | bat_dbg(DBG_BATMAN, bat_priv, |
| 1006 | "Drop packet: incompatible batman version (%i)\n", |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1007 | batman_ogm_packet->header.version); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | if (is_my_addr) { |
| 1012 | bat_dbg(DBG_BATMAN, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 1013 | "Drop packet: received my own broadcast (sender: %pM)\n", |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1014 | ethhdr->h_source); |
| 1015 | return; |
| 1016 | } |
| 1017 | |
| 1018 | if (is_broadcast) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 1019 | bat_dbg(DBG_BATMAN, bat_priv, |
| 1020 | "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n", |
| 1021 | ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1022 | return; |
| 1023 | } |
| 1024 | |
| 1025 | if (is_my_orig) { |
| 1026 | unsigned long *word; |
| 1027 | int offset; |
| 1028 | |
| 1029 | orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source); |
| 1030 | if (!orig_neigh_node) |
| 1031 | return; |
| 1032 | |
| 1033 | /* neighbor has to indicate direct link and it has to |
| 1034 | * come via the corresponding interface */ |
| 1035 | /* save packet seqno for bidirectional check */ |
| 1036 | if (has_directlink_flag && |
| 1037 | compare_eth(if_incoming->net_dev->dev_addr, |
| 1038 | batman_ogm_packet->orig)) { |
| 1039 | offset = if_incoming->if_num * NUM_WORDS; |
| 1040 | |
| 1041 | spin_lock_bh(&orig_neigh_node->ogm_cnt_lock); |
| 1042 | word = &(orig_neigh_node->bcast_own[offset]); |
Sven Eckelmann | 0079d2c | 2012-02-04 17:34:52 +0100 | [diff] [blame] | 1043 | bat_set_bit(word, |
| 1044 | if_incoming_seqno - |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1045 | batman_ogm_packet->seqno - 2); |
| 1046 | orig_neigh_node->bcast_own_sum[if_incoming->if_num] = |
Sven Eckelmann | 0079d2c | 2012-02-04 17:34:52 +0100 | [diff] [blame] | 1047 | bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1048 | spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock); |
| 1049 | } |
| 1050 | |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 1051 | bat_dbg(DBG_BATMAN, bat_priv, |
| 1052 | "Drop packet: originator packet from myself (via neighbor)\n"); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1053 | orig_node_free_ref(orig_neigh_node); |
| 1054 | return; |
| 1055 | } |
| 1056 | |
| 1057 | if (is_my_oldorig) { |
| 1058 | bat_dbg(DBG_BATMAN, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 1059 | "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n", |
| 1060 | ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1061 | return; |
| 1062 | } |
| 1063 | |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 1064 | if (batman_ogm_packet->flags & NOT_BEST_NEXT_HOP) { |
| 1065 | bat_dbg(DBG_BATMAN, bat_priv, |
Marek Lindner | fefa532 | 2012-03-17 15:28:34 +0800 | [diff] [blame] | 1066 | "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n", |
| 1067 | ethhdr->h_source); |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 1068 | return; |
| 1069 | } |
| 1070 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1071 | orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig); |
| 1072 | if (!orig_node) |
| 1073 | return; |
| 1074 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 1075 | is_duplicate = bat_iv_ogm_update_seqnos(ethhdr, batman_ogm_packet, |
| 1076 | if_incoming); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1077 | |
| 1078 | if (is_duplicate == -1) { |
| 1079 | bat_dbg(DBG_BATMAN, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 1080 | "Drop packet: packet within seqno protection time (sender: %pM)\n", |
| 1081 | ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1082 | goto out; |
| 1083 | } |
| 1084 | |
| 1085 | if (batman_ogm_packet->tq == 0) { |
| 1086 | bat_dbg(DBG_BATMAN, bat_priv, |
| 1087 | "Drop packet: originator packet with tq equal 0\n"); |
| 1088 | goto out; |
| 1089 | } |
| 1090 | |
| 1091 | router = orig_node_get_router(orig_node); |
| 1092 | if (router) |
| 1093 | router_router = orig_node_get_router(router->orig_node); |
| 1094 | |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 1095 | if ((router && router->tq_avg != 0) && |
| 1096 | (compare_eth(router->addr, ethhdr->h_source))) |
| 1097 | is_from_best_next_hop = true; |
| 1098 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1099 | /* avoid temporary routing loops */ |
| 1100 | if (router && router_router && |
| 1101 | (compare_eth(router->addr, batman_ogm_packet->prev_sender)) && |
| 1102 | !(compare_eth(batman_ogm_packet->orig, |
| 1103 | batman_ogm_packet->prev_sender)) && |
| 1104 | (compare_eth(router->addr, router_router->addr))) { |
| 1105 | bat_dbg(DBG_BATMAN, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 1106 | "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n", |
| 1107 | ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1108 | goto out; |
| 1109 | } |
| 1110 | |
| 1111 | /* if sender is a direct neighbor the sender mac equals |
| 1112 | * originator mac */ |
| 1113 | orig_neigh_node = (is_single_hop_neigh ? |
| 1114 | orig_node : |
| 1115 | get_orig_node(bat_priv, ethhdr->h_source)); |
| 1116 | if (!orig_neigh_node) |
| 1117 | goto out; |
| 1118 | |
| 1119 | orig_neigh_router = orig_node_get_router(orig_neigh_node); |
| 1120 | |
| 1121 | /* drop packet if sender is not a direct neighbor and if we |
| 1122 | * don't route towards it */ |
| 1123 | if (!is_single_hop_neigh && (!orig_neigh_router)) { |
| 1124 | bat_dbg(DBG_BATMAN, bat_priv, |
| 1125 | "Drop packet: OGM via unknown neighbor!\n"); |
| 1126 | goto out_neigh; |
| 1127 | } |
| 1128 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 1129 | is_bidirectional = bat_iv_ogm_calc_tq(orig_node, orig_neigh_node, |
| 1130 | batman_ogm_packet, if_incoming); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1131 | |
| 1132 | bonding_save_primary(orig_node, orig_neigh_node, batman_ogm_packet); |
| 1133 | |
| 1134 | /* update ranking if it is not a duplicate or has the same |
| 1135 | * seqno and similar ttl as the non-duplicate */ |
| 1136 | if (is_bidirectional && |
| 1137 | (!is_duplicate || |
| 1138 | ((orig_node->last_real_seqno == batman_ogm_packet->seqno) && |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1139 | (orig_node->last_ttl - 3 <= batman_ogm_packet->header.ttl)))) |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 1140 | bat_iv_ogm_orig_update(bat_priv, orig_node, ethhdr, |
| 1141 | batman_ogm_packet, if_incoming, |
| 1142 | tt_buff, is_duplicate); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1143 | |
| 1144 | /* is single hop (direct) neighbor */ |
| 1145 | if (is_single_hop_neigh) { |
| 1146 | |
| 1147 | /* mark direct link on incoming interface */ |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 1148 | bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet, |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 1149 | is_single_hop_neigh, is_from_best_next_hop, |
| 1150 | if_incoming); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1151 | |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 1152 | bat_dbg(DBG_BATMAN, bat_priv, |
| 1153 | "Forwarding packet: rebroadcast neighbor packet with direct link flag\n"); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1154 | goto out_neigh; |
| 1155 | } |
| 1156 | |
| 1157 | /* multihop originator */ |
| 1158 | if (!is_bidirectional) { |
| 1159 | bat_dbg(DBG_BATMAN, bat_priv, |
| 1160 | "Drop packet: not received via bidirectional link\n"); |
| 1161 | goto out_neigh; |
| 1162 | } |
| 1163 | |
| 1164 | if (is_duplicate) { |
| 1165 | bat_dbg(DBG_BATMAN, bat_priv, |
| 1166 | "Drop packet: duplicate packet received\n"); |
| 1167 | goto out_neigh; |
| 1168 | } |
| 1169 | |
| 1170 | bat_dbg(DBG_BATMAN, bat_priv, |
| 1171 | "Forwarding packet: rebroadcast originator packet\n"); |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 1172 | bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet, |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 1173 | is_single_hop_neigh, is_from_best_next_hop, |
| 1174 | if_incoming); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1175 | |
| 1176 | out_neigh: |
| 1177 | if ((orig_neigh_node) && (!is_single_hop_neigh)) |
| 1178 | orig_node_free_ref(orig_neigh_node); |
| 1179 | out: |
| 1180 | if (router) |
| 1181 | neigh_node_free_ref(router); |
| 1182 | if (router_router) |
| 1183 | neigh_node_free_ref(router_router); |
| 1184 | if (orig_neigh_router) |
| 1185 | neigh_node_free_ref(orig_neigh_router); |
| 1186 | |
| 1187 | orig_node_free_ref(orig_node); |
| 1188 | } |
| 1189 | |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1190 | static int bat_iv_ogm_receive(struct sk_buff *skb, |
| 1191 | struct hard_iface *if_incoming) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1192 | { |
Marek Lindner | edbf12b | 2012-03-11 06:17:49 +0800 | [diff] [blame] | 1193 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1194 | struct batman_ogm_packet *batman_ogm_packet; |
Marek Lindner | 8780dad | 2011-12-05 04:01:51 +0800 | [diff] [blame] | 1195 | struct ethhdr *ethhdr; |
| 1196 | int buff_pos = 0, packet_len; |
| 1197 | unsigned char *tt_buff, *packet_buff; |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1198 | bool ret; |
| 1199 | |
| 1200 | ret = check_management_packet(skb, if_incoming, BATMAN_OGM_HLEN); |
| 1201 | if (!ret) |
| 1202 | return NET_RX_DROP; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1203 | |
Marek Lindner | edbf12b | 2012-03-11 06:17:49 +0800 | [diff] [blame] | 1204 | /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface |
| 1205 | * that does not have B.A.T.M.A.N. IV enabled ? |
| 1206 | */ |
| 1207 | if (bat_priv->bat_algo_ops->bat_ogm_emit != bat_iv_ogm_emit) |
| 1208 | return NET_RX_DROP; |
| 1209 | |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame^] | 1210 | batadv_inc_counter(bat_priv, BAT_CNT_MGMT_RX); |
| 1211 | batadv_add_counter(bat_priv, BAT_CNT_MGMT_RX_BYTES, |
| 1212 | skb->len + ETH_HLEN); |
| 1213 | |
Marek Lindner | 8780dad | 2011-12-05 04:01:51 +0800 | [diff] [blame] | 1214 | packet_len = skb_headlen(skb); |
| 1215 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 1216 | packet_buff = skb->data; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1217 | batman_ogm_packet = (struct batman_ogm_packet *)packet_buff; |
| 1218 | |
| 1219 | /* unpack the aggregated packets and process them one by one */ |
| 1220 | do { |
| 1221 | /* network to host order for our 32bit seqno and the |
| 1222 | orig_interval */ |
| 1223 | batman_ogm_packet->seqno = ntohl(batman_ogm_packet->seqno); |
| 1224 | batman_ogm_packet->tt_crc = ntohs(batman_ogm_packet->tt_crc); |
| 1225 | |
Marek Lindner | 76e3d7f | 2012-02-07 17:20:50 +0800 | [diff] [blame] | 1226 | tt_buff = packet_buff + buff_pos + BATMAN_OGM_HLEN; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1227 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 1228 | bat_iv_ogm_process(ethhdr, batman_ogm_packet, |
| 1229 | tt_buff, if_incoming); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1230 | |
Marek Lindner | 76e3d7f | 2012-02-07 17:20:50 +0800 | [diff] [blame] | 1231 | buff_pos += BATMAN_OGM_HLEN + |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1232 | tt_len(batman_ogm_packet->tt_num_changes); |
| 1233 | |
| 1234 | batman_ogm_packet = (struct batman_ogm_packet *) |
| 1235 | (packet_buff + buff_pos); |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 1236 | } while (bat_iv_ogm_aggr_packet(buff_pos, packet_len, |
| 1237 | batman_ogm_packet->tt_num_changes)); |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1238 | |
| 1239 | kfree_skb(skb); |
| 1240 | return NET_RX_SUCCESS; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1241 | } |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 1242 | |
| 1243 | static struct bat_algo_ops batman_iv __read_mostly = { |
| 1244 | .name = "BATMAN IV", |
Marek Lindner | c2aca02 | 2012-02-07 17:20:45 +0800 | [diff] [blame] | 1245 | .bat_iface_enable = bat_iv_ogm_iface_enable, |
Marek Lindner | 00a5007 | 2012-02-07 17:20:47 +0800 | [diff] [blame] | 1246 | .bat_iface_disable = bat_iv_ogm_iface_disable, |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 1247 | .bat_iface_update_mac = bat_iv_ogm_iface_update_mac, |
Marek Lindner | cd8b78e | 2012-02-07 17:20:49 +0800 | [diff] [blame] | 1248 | .bat_primary_iface_set = bat_iv_ogm_primary_iface_set, |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 1249 | .bat_ogm_schedule = bat_iv_ogm_schedule, |
| 1250 | .bat_ogm_emit = bat_iv_ogm_emit, |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 1251 | }; |
| 1252 | |
| 1253 | int __init bat_iv_init(void) |
| 1254 | { |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1255 | int ret; |
| 1256 | |
| 1257 | /* batman originator packet */ |
| 1258 | ret = recv_handler_register(BAT_IV_OGM, bat_iv_ogm_receive); |
| 1259 | if (ret < 0) |
| 1260 | goto out; |
| 1261 | |
| 1262 | ret = bat_algo_register(&batman_iv); |
| 1263 | if (ret < 0) |
| 1264 | goto handler_unregister; |
| 1265 | |
| 1266 | goto out; |
| 1267 | |
| 1268 | handler_unregister: |
| 1269 | recv_handler_unregister(BAT_IV_OGM); |
| 1270 | out: |
| 1271 | return ret; |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 1272 | } |