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