|
38 | 38 |
|
39 | 39 | #include "bat_algo.h" |
40 | 40 | #include "hard-interface.h" |
| 41 | +#include "hash.h" |
| 42 | +#include "originator.h" |
41 | 43 | #include "packet.h" |
| 44 | +#include "routing.h" |
42 | 45 | #include "send.h" |
43 | 46 |
|
44 | 47 | /** |
@@ -191,3 +194,152 @@ void batadv_v_elp_primary_iface_set(struct batadv_hard_iface *primary_iface) |
191 | 194 | } |
192 | 195 | rcu_read_unlock(); |
193 | 196 | } |
| 197 | + |
| 198 | +/** |
| 199 | + * batadv_v_ogm_orig_get - retrieve and possibly create an originator node |
| 200 | + * @bat_priv: the bat priv with all the soft interface information |
| 201 | + * @addr: the address of the originator |
| 202 | + * |
| 203 | + * Return: the orig_node corresponding to the specified address. If such object |
| 204 | + * does not exist it is allocated here. In case of allocation failure returns |
| 205 | + * NULL. |
| 206 | + */ |
| 207 | +static struct batadv_orig_node * |
| 208 | +batadv_v_ogm_orig_get(struct batadv_priv *bat_priv, |
| 209 | + const u8 *addr) |
| 210 | +{ |
| 211 | + struct batadv_orig_node *orig_node; |
| 212 | + int hash_added; |
| 213 | + |
| 214 | + orig_node = batadv_orig_hash_find(bat_priv, addr); |
| 215 | + if (orig_node) |
| 216 | + return orig_node; |
| 217 | + |
| 218 | + orig_node = batadv_orig_node_new(bat_priv, addr); |
| 219 | + if (!orig_node) |
| 220 | + return NULL; |
| 221 | + |
| 222 | + hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig, |
| 223 | + batadv_choose_orig, orig_node, |
| 224 | + &orig_node->hash_entry); |
| 225 | + if (hash_added != 0) { |
| 226 | + /* orig_node->refcounter is initialised to 2 by |
| 227 | + * batadv_orig_node_new() |
| 228 | + */ |
| 229 | + batadv_orig_node_put(orig_node); |
| 230 | + batadv_orig_node_put(orig_node); |
| 231 | + orig_node = NULL; |
| 232 | + } |
| 233 | + |
| 234 | + return orig_node; |
| 235 | +} |
| 236 | + |
| 237 | +/** |
| 238 | + * batadv_v_elp_neigh_update - update an ELP neighbour node |
| 239 | + * @bat_priv: the bat priv with all the soft interface information |
| 240 | + * @neigh_addr: the neighbour interface address |
| 241 | + * @if_incoming: the interface the packet was received through |
| 242 | + * @elp_packet: the received ELP packet |
| 243 | + * |
| 244 | + * Updates the ELP neighbour node state with the data received within the new |
| 245 | + * ELP packet. |
| 246 | + */ |
| 247 | +static void batadv_v_elp_neigh_update(struct batadv_priv *bat_priv, |
| 248 | + u8 *neigh_addr, |
| 249 | + struct batadv_hard_iface *if_incoming, |
| 250 | + struct batadv_elp_packet *elp_packet) |
| 251 | + |
| 252 | +{ |
| 253 | + struct batadv_neigh_node *neigh; |
| 254 | + struct batadv_orig_node *orig_neigh; |
| 255 | + struct batadv_hardif_neigh_node *hardif_neigh; |
| 256 | + s32 seqno_diff; |
| 257 | + s32 elp_latest_seqno; |
| 258 | + |
| 259 | + orig_neigh = batadv_v_ogm_orig_get(bat_priv, elp_packet->orig); |
| 260 | + if (!orig_neigh) |
| 261 | + return; |
| 262 | + |
| 263 | + neigh = batadv_neigh_node_new(orig_neigh, if_incoming, neigh_addr); |
| 264 | + if (!neigh) |
| 265 | + goto orig_free; |
| 266 | + |
| 267 | + hardif_neigh = batadv_hardif_neigh_get(if_incoming, neigh_addr); |
| 268 | + if (!hardif_neigh) |
| 269 | + goto neigh_free; |
| 270 | + |
| 271 | + elp_latest_seqno = hardif_neigh->bat_v.elp_latest_seqno; |
| 272 | + seqno_diff = ntohl(elp_packet->seqno) - elp_latest_seqno; |
| 273 | + |
| 274 | + /* known or older sequence numbers are ignored. However always adopt |
| 275 | + * if the router seems to have been restarted. |
| 276 | + */ |
| 277 | + if (seqno_diff < 1 && seqno_diff > -BATADV_ELP_MAX_AGE) |
| 278 | + goto hardif_free; |
| 279 | + |
| 280 | + neigh->last_seen = jiffies; |
| 281 | + hardif_neigh->last_seen = jiffies; |
| 282 | + hardif_neigh->bat_v.elp_latest_seqno = ntohl(elp_packet->seqno); |
| 283 | + hardif_neigh->bat_v.elp_interval = ntohl(elp_packet->elp_interval); |
| 284 | + |
| 285 | +hardif_free: |
| 286 | + if (hardif_neigh) |
| 287 | + batadv_hardif_neigh_put(hardif_neigh); |
| 288 | +neigh_free: |
| 289 | + if (neigh) |
| 290 | + batadv_neigh_node_put(neigh); |
| 291 | +orig_free: |
| 292 | + if (orig_neigh) |
| 293 | + batadv_orig_node_put(orig_neigh); |
| 294 | +} |
| 295 | + |
| 296 | +/** |
| 297 | + * batadv_v_elp_packet_recv - main ELP packet handler |
| 298 | + * @skb: the received packet |
| 299 | + * @if_incoming: the interface this packet was received through |
| 300 | + * |
| 301 | + * Return: NET_RX_SUCCESS and consumes the skb if the packet was peoperly |
| 302 | + * processed or NET_RX_DROP in case of failure. |
| 303 | + */ |
| 304 | +int batadv_v_elp_packet_recv(struct sk_buff *skb, |
| 305 | + struct batadv_hard_iface *if_incoming) |
| 306 | +{ |
| 307 | + struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 308 | + struct batadv_elp_packet *elp_packet; |
| 309 | + struct batadv_hard_iface *primary_if; |
| 310 | + struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 311 | + bool ret; |
| 312 | + |
| 313 | + ret = batadv_check_management_packet(skb, if_incoming, BATADV_ELP_HLEN); |
| 314 | + if (!ret) |
| 315 | + return NET_RX_DROP; |
| 316 | + |
| 317 | + if (batadv_is_my_mac(bat_priv, ethhdr->h_source)) |
| 318 | + return NET_RX_DROP; |
| 319 | + |
| 320 | + /* did we receive a B.A.T.M.A.N. V ELP packet on an interface |
| 321 | + * that does not have B.A.T.M.A.N. V ELP enabled ? |
| 322 | + */ |
| 323 | + if (strcmp(bat_priv->bat_algo_ops->name, "BATMAN_V") != 0) |
| 324 | + return NET_RX_DROP; |
| 325 | + |
| 326 | + elp_packet = (struct batadv_elp_packet *)skb->data; |
| 327 | + |
| 328 | + batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 329 | + "Received ELP packet from %pM seqno %u ORIG: %pM\n", |
| 330 | + ethhdr->h_source, ntohl(elp_packet->seqno), |
| 331 | + elp_packet->orig); |
| 332 | + |
| 333 | + primary_if = batadv_primary_if_get_selected(bat_priv); |
| 334 | + if (!primary_if) |
| 335 | + goto out; |
| 336 | + |
| 337 | + batadv_v_elp_neigh_update(bat_priv, ethhdr->h_source, if_incoming, |
| 338 | + elp_packet); |
| 339 | + |
| 340 | +out: |
| 341 | + if (primary_if) |
| 342 | + batadv_hardif_put(primary_if); |
| 343 | + consume_skb(skb); |
| 344 | + return NET_RX_SUCCESS; |
| 345 | +} |
0 commit comments