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