1// SPDX-License-Identifier: GPL-2.0 2/* Copyright (C) 2009-2020 B.A.T.M.A.N. contributors: 3 * 4 * Marek Lindner 5 */ 6 7#include "gateway_client.h" 8#include "main.h" 9 10#include <linux/atomic.h> 11#include <linux/byteorder/generic.h> 12#include <linux/errno.h> 13#include <linux/etherdevice.h> 14#include <linux/gfp.h> 15#include <linux/if_ether.h> 16#include <linux/if_vlan.h> 17#include <linux/in.h> 18#include <linux/ip.h> 19#include <linux/ipv6.h> 20#include <linux/kernel.h> 21#include <linux/kref.h> 22#include <linux/list.h> 23#include <linux/lockdep.h> 24#include <linux/netdevice.h> 25#include <linux/netlink.h> 26#include <linux/rculist.h> 27#include <linux/rcupdate.h> 28#include <linux/seq_file.h> 29#include <linux/skbuff.h> 30#include <linux/slab.h> 31#include <linux/spinlock.h> 32#include <linux/stddef.h> 33#include <linux/udp.h> 34#include <net/sock.h> 35#include <uapi/linux/batadv_packet.h> 36#include <uapi/linux/batman_adv.h> 37 38#include "hard-interface.h" 39#include "log.h" 40#include "netlink.h" 41#include "originator.h" 42#include "routing.h" 43#include "soft-interface.h" 44#include "translation-table.h" 45 46/* These are the offsets of the "hw type" and "hw address length" in the dhcp 47 * packet starting at the beginning of the dhcp header 48 */ 49#define BATADV_DHCP_HTYPE_OFFSET 1 50#define BATADV_DHCP_HLEN_OFFSET 2 51/* Value of htype representing Ethernet */ 52#define BATADV_DHCP_HTYPE_ETHERNET 0x01 53/* This is the offset of the "chaddr" field in the dhcp packet starting at the 54 * beginning of the dhcp header 55 */ 56#define BATADV_DHCP_CHADDR_OFFSET 28 57 58/** 59 * batadv_gw_node_release() - release gw_node from lists and queue for free 60 * after rcu grace period 61 * @ref: kref pointer of the gw_node 62 */ 63void batadv_gw_node_release(struct kref *ref) 64{ 65 struct batadv_gw_node *gw_node; 66 67 gw_node = container_of(ref, struct batadv_gw_node, refcount); 68 69 batadv_orig_node_put(gw_node->orig_node); 70 kfree_rcu(gw_node, rcu); 71} 72 73/** 74 * batadv_gw_get_selected_gw_node() - Get currently selected gateway 75 * @bat_priv: the bat priv with all the soft interface information 76 * 77 * Return: selected gateway (with increased refcnt), NULL on errors 78 */ 79struct batadv_gw_node * 80batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv) 81{ 82 struct batadv_gw_node *gw_node; 83 84 rcu_read_lock(); 85 gw_node = rcu_dereference(bat_priv->gw.curr_gw); 86 if (!gw_node) 87 goto out; 88 89 if (!kref_get_unless_zero(&gw_node->refcount)) 90 gw_node = NULL; 91 92out: 93 rcu_read_unlock(); 94 return gw_node; 95} 96 97/** 98 * batadv_gw_get_selected_orig() - Get originator of currently selected gateway 99 * @bat_priv: the bat priv with all the soft interface information 100 * 101 * Return: orig_node of selected gateway (with increased refcnt), NULL on errors 102 */ 103struct batadv_orig_node * 104batadv_gw_get_selected_orig(struct batadv_priv *bat_priv) 105{ 106 struct batadv_gw_node *gw_node; 107 struct batadv_orig_node *orig_node = NULL; 108 109 gw_node = batadv_gw_get_selected_gw_node(bat_priv); 110 if (!gw_node) 111 goto out; 112 113 rcu_read_lock(); 114 orig_node = gw_node->orig_node; 115 if (!orig_node) 116 goto unlock; 117 118 if (!kref_get_unless_zero(&orig_node->refcount)) 119 orig_node = NULL; 120 121unlock: 122 rcu_read_unlock(); 123out: 124 if (gw_node) 125 batadv_gw_node_put(gw_node); 126 return orig_node; 127} 128 129static void batadv_gw_select(struct batadv_priv *bat_priv, 130 struct batadv_gw_node *new_gw_node) 131{ 132 struct batadv_gw_node *curr_gw_node; 133 134 spin_lock_bh(&bat_priv->gw.list_lock); 135 136 if (new_gw_node) 137 kref_get(&new_gw_node->refcount); 138 139 curr_gw_node = rcu_replace_pointer(bat_priv->gw.curr_gw, new_gw_node, 140 true); 141 142 if (curr_gw_node) 143 batadv_gw_node_put(curr_gw_node); 144 145 spin_unlock_bh(&bat_priv->gw.list_lock); 146} 147 148/** 149 * batadv_gw_reselect() - force a gateway reselection 150 * @bat_priv: the bat priv with all the soft interface information 151 * 152 * Set a flag to remind the GW component to perform a new gateway reselection. 153 * However this function does not ensure that the current gateway is going to be 154 * deselected. The reselection mechanism may elect the same gateway once again. 155 * 156 * This means that invoking batadv_gw_reselect() does not guarantee a gateway 157 * change and therefore a uevent is not necessarily expected. 158 */ 159void batadv_gw_reselect(struct batadv_priv *bat_priv) 160{ 161 atomic_set(&bat_priv->gw.reselect, 1); 162} 163 164/** 165 * batadv_gw_check_client_stop() - check if client mode has been switched off 166 * @bat_priv: the bat priv with all the soft interface information 167 * 168 * This function assumes the caller has checked that the gw state *is actually 169 * changing*. This function is not supposed to be called when there is no state 170 * change. 171 */ 172void batadv_gw_check_client_stop(struct batadv_priv *bat_priv) 173{ 174 struct batadv_gw_node *curr_gw; 175 176 if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT) 177 return; 178 179 curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 180 if (!curr_gw) 181 return; 182 183 /* deselect the current gateway so that next time that client mode is 184 * enabled a proper GW_ADD event can be sent 185 */ 186 batadv_gw_select(bat_priv, NULL); 187 188 /* if batman-adv is switching the gw client mode off and a gateway was 189 * already selected, send a DEL uevent 190 */ 191 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL); 192 193 batadv_gw_node_put(curr_gw); 194} 195 196/** 197 * batadv_gw_election() - Elect the best gateway 198 * @bat_priv: the bat priv with all the soft interface information 199 */ 200void batadv_gw_election(struct batadv_priv *bat_priv) 201{ 202 struct batadv_gw_node *curr_gw = NULL; 203 struct batadv_gw_node *next_gw = NULL; 204 struct batadv_neigh_node *router = NULL; 205 struct batadv_neigh_ifinfo *router_ifinfo = NULL; 206 char gw_addr[18] = { '\0' }; 207 208 if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT) 209 goto out; 210 211 if (!bat_priv->algo_ops->gw.get_best_gw_node) 212 goto out; 213 214 curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 215 216 if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw) 217 goto out; 218 219 /* if gw.reselect is set to 1 it means that a previous call to 220 * gw.is_eligible() said that we have a new best GW, therefore it can 221 * now be picked from the list and selected 222 */ 223 next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv); 224 225 if (curr_gw == next_gw) 226 goto out; 227 228 if (next_gw) { 229 sprintf(gw_addr, "%pM", next_gw->orig_node->orig); 230 231 router = batadv_orig_router_get(next_gw->orig_node, 232 BATADV_IF_DEFAULT); 233 if (!router) { 234 batadv_gw_reselect(bat_priv); 235 goto out; 236 } 237 238 router_ifinfo = batadv_neigh_ifinfo_get(router, 239 BATADV_IF_DEFAULT); 240 if (!router_ifinfo) { 241 batadv_gw_reselect(bat_priv); 242 goto out; 243 } 244 } 245 246 if (curr_gw && !next_gw) { 247 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 248 "Removing selected gateway - no gateway in range\n"); 249 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, 250 NULL); 251 } else if (!curr_gw && next_gw) { 252 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 253 "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", 254 next_gw->orig_node->orig, 255 next_gw->bandwidth_down / 10, 256 next_gw->bandwidth_down % 10, 257 next_gw->bandwidth_up / 10, 258 next_gw->bandwidth_up % 10, 259 router_ifinfo->bat_iv.tq_avg); 260 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD, 261 gw_addr); 262 } else { 263 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 264 "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", 265 next_gw->orig_node->orig, 266 next_gw->bandwidth_down / 10, 267 next_gw->bandwidth_down % 10, 268 next_gw->bandwidth_up / 10, 269 next_gw->bandwidth_up % 10, 270 router_ifinfo->bat_iv.tq_avg); 271 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE, 272 gw_addr); 273 } 274 275 batadv_gw_select(bat_priv, next_gw); 276 277out: 278 if (curr_gw) 279 batadv_gw_node_put(curr_gw); 280 if (next_gw) 281 batadv_gw_node_put(next_gw); 282 if (router) 283 batadv_neigh_node_put(router); 284 if (router_ifinfo) 285 batadv_neigh_ifinfo_put(router_ifinfo); 286} 287 288/** 289 * batadv_gw_check_election() - Elect orig node as best gateway when eligible 290 * @bat_priv: the bat priv with all the soft interface information 291 * @orig_node: orig node which is to be checked 292 */ 293void batadv_gw_check_election(struct batadv_priv *bat_priv, 294 struct batadv_orig_node *orig_node) 295{ 296 struct batadv_orig_node *curr_gw_orig; 297 298 /* abort immediately if the routing algorithm does not support gateway 299 * election 300 */ 301 if (!bat_priv->algo_ops->gw.is_eligible) 302 return; 303 304 curr_gw_orig = batadv_gw_get_selected_orig(bat_priv); 305 if (!curr_gw_orig) 306 goto reselect; 307 308 /* this node already is the gateway */ 309 if (curr_gw_orig == orig_node) 310 goto out; 311 312 if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig, 313 orig_node)) 314 goto out; 315 316reselect: 317 batadv_gw_reselect(bat_priv); 318out: 319 if (curr_gw_orig) 320 batadv_orig_node_put(curr_gw_orig); 321} 322 323/** 324 * batadv_gw_node_add() - add gateway node to list of available gateways 325 * @bat_priv: the bat priv with all the soft interface information 326 * @orig_node: originator announcing gateway capabilities 327 * @gateway: announced bandwidth information 328 * 329 * Has to be called with the appropriate locks being acquired 330 * (gw.list_lock). 331 */ 332static void batadv_gw_node_add(struct batadv_priv *bat_priv, 333 struct batadv_orig_node *orig_node, 334 struct batadv_tvlv_gateway_data *gateway) 335{ 336 struct batadv_gw_node *gw_node; 337 338 lockdep_assert_held(&bat_priv->gw.list_lock); 339 340 if (gateway->bandwidth_down == 0) 341 return; 342 343 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC); 344 if (!gw_node) 345 return; 346 347 kref_init(&gw_node->refcount); 348 INIT_HLIST_NODE(&gw_node->list); 349 kref_get(&orig_node->refcount); 350 gw_node->orig_node = orig_node; 351 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); 352 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); 353 354 kref_get(&gw_node->refcount); 355 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list); 356 bat_priv->gw.generation++; 357 358 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 359 "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n", 360 orig_node->orig, 361 ntohl(gateway->bandwidth_down) / 10, 362 ntohl(gateway->bandwidth_down) % 10, 363 ntohl(gateway->bandwidth_up) / 10, 364 ntohl(gateway->bandwidth_up) % 10); 365 366 /* don't return reference to new gw_node */ 367 batadv_gw_node_put(gw_node); 368} 369 370/** 371 * batadv_gw_node_get() - retrieve gateway node from list of available gateways 372 * @bat_priv: the bat priv with all the soft interface information 373 * @orig_node: originator announcing gateway capabilities 374 * 375 * Return: gateway node if found or NULL otherwise. 376 */ 377struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv, 378 struct batadv_orig_node *orig_node) 379{ 380 struct batadv_gw_node *gw_node_tmp, *gw_node = NULL; 381 382 rcu_read_lock(); 383 hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.gateway_list, 384 list) { 385 if (gw_node_tmp->orig_node != orig_node) 386 continue; 387 388 if (!kref_get_unless_zero(&gw_node_tmp->refcount)) 389 continue; 390 391 gw_node = gw_node_tmp; 392 break; 393 } 394 rcu_read_unlock(); 395 396 return gw_node; 397} 398 399/** 400 * batadv_gw_node_update() - update list of available gateways with changed 401 * bandwidth information 402 * @bat_priv: the bat priv with all the soft interface information 403 * @orig_node: originator announcing gateway capabilities 404 * @gateway: announced bandwidth information 405 */ 406void batadv_gw_node_update(struct batadv_priv *bat_priv, 407 struct batadv_orig_node *orig_node, 408 struct batadv_tvlv_gateway_data *gateway) 409{ 410 struct batadv_gw_node *gw_node, *curr_gw = NULL; 411 412 spin_lock_bh(&bat_priv->gw.list_lock); 413 gw_node = batadv_gw_node_get(bat_priv, orig_node); 414 if (!gw_node) { 415 batadv_gw_node_add(bat_priv, orig_node, gateway); 416 spin_unlock_bh(&bat_priv->gw.list_lock); 417 goto out; 418 } 419 spin_unlock_bh(&bat_priv->gw.list_lock); 420 421 if (gw_node->bandwidth_down == ntohl(gateway->bandwidth_down) && 422 gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)) 423 goto out; 424 425 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 426 "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n", 427 orig_node->orig, 428 gw_node->bandwidth_down / 10, 429 gw_node->bandwidth_down % 10, 430 gw_node->bandwidth_up / 10, 431 gw_node->bandwidth_up % 10, 432 ntohl(gateway->bandwidth_down) / 10, 433 ntohl(gateway->bandwidth_down) % 10, 434 ntohl(gateway->bandwidth_up) / 10, 435 ntohl(gateway->bandwidth_up) % 10); 436 437 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); 438 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); 439 440 if (ntohl(gateway->bandwidth_down) == 0) { 441 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 442 "Gateway %pM removed from gateway list\n", 443 orig_node->orig); 444 445 /* Note: We don't need a NULL check here, since curr_gw never 446 * gets dereferenced. 447 */ 448 spin_lock_bh(&bat_priv->gw.list_lock); 449 if (!hlist_unhashed(&gw_node->list)) { 450 hlist_del_init_rcu(&gw_node->list); 451 batadv_gw_node_put(gw_node); 452 bat_priv->gw.generation++; 453 } 454 spin_unlock_bh(&bat_priv->gw.list_lock); 455 456 curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 457 if (gw_node == curr_gw) 458 batadv_gw_reselect(bat_priv); 459 460 if (curr_gw) 461 batadv_gw_node_put(curr_gw); 462 } 463 464out: 465 if (gw_node) 466 batadv_gw_node_put(gw_node); 467} 468 469/** 470 * batadv_gw_node_delete() - Remove orig_node from gateway list 471 * @bat_priv: the bat priv with all the soft interface information 472 * @orig_node: orig node which is currently in process of being removed 473 */ 474void batadv_gw_node_delete(struct batadv_priv *bat_priv, 475 struct batadv_orig_node *orig_node) 476{ 477 struct batadv_tvlv_gateway_data gateway; 478 479 gateway.bandwidth_down = 0; 480 gateway.bandwidth_up = 0; 481 482 batadv_gw_node_update(bat_priv, orig_node, &gateway); 483} 484 485/** 486 * batadv_gw_node_free() - Free gateway information from soft interface 487 * @bat_priv: the bat priv with all the soft interface information 488 */ 489void batadv_gw_node_free(struct batadv_priv *bat_priv) 490{ 491 struct batadv_gw_node *gw_node; 492 struct hlist_node *node_tmp; 493 494 spin_lock_bh(&bat_priv->gw.list_lock); 495 hlist_for_each_entry_safe(gw_node, node_tmp, 496 &bat_priv->gw.gateway_list, list) { 497 hlist_del_init_rcu(&gw_node->list); 498 batadv_gw_node_put(gw_node); 499 bat_priv->gw.generation++; 500 } 501 spin_unlock_bh(&bat_priv->gw.list_lock); 502} 503 504#ifdef CONFIG_BATMAN_ADV_DEBUGFS 505 506/** 507 * batadv_gw_client_seq_print_text() - Print the gateway table in a seq file 508 * @seq: seq file to print on 509 * @offset: not used 510 * 511 * Return: always 0 512 */ 513int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset) 514{ 515 struct net_device *net_dev = (struct net_device *)seq->private; 516 struct batadv_priv *bat_priv = netdev_priv(net_dev); 517 struct batadv_hard_iface *primary_if; 518 519 primary_if = batadv_seq_print_text_primary_if_get(seq); 520 if (!primary_if) 521 return 0; 522 523 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n", 524 BATADV_SOURCE_VERSION, primary_if->net_dev->name, 525 primary_if->net_dev->dev_addr, net_dev->name, 526 bat_priv->algo_ops->name); 527 528 batadv_hardif_put(primary_if); 529 530 if (!bat_priv->algo_ops->gw.print) { 531 seq_puts(seq, 532 "No printing function for this routing protocol\n"); 533 return 0; 534 } 535 536 bat_priv->algo_ops->gw.print(bat_priv, seq); 537 538 return 0; 539} 540#endif 541 542/** 543 * batadv_gw_dump() - Dump gateways into a message 544 * @msg: Netlink message to dump into 545 * @cb: Control block containing additional options 546 * 547 * Return: Error code, or length of message 548 */ 549int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb) 550{ 551 struct batadv_hard_iface *primary_if = NULL; 552 struct net *net = sock_net(cb->skb->sk); 553 struct net_device *soft_iface; 554 struct batadv_priv *bat_priv; 555 int ifindex; 556 int ret; 557 558 ifindex = batadv_netlink_get_ifindex(cb->nlh, 559 BATADV_ATTR_MESH_IFINDEX); 560 if (!ifindex) 561 return -EINVAL; 562 563 soft_iface = dev_get_by_index(net, ifindex); 564 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) { 565 ret = -ENODEV; 566 goto out; 567 } 568 569 bat_priv = netdev_priv(soft_iface); 570 571 primary_if = batadv_primary_if_get_selected(bat_priv); 572 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) { 573 ret = -ENOENT; 574 goto out; 575 } 576 577 if (!bat_priv->algo_ops->gw.dump) { 578 ret = -EOPNOTSUPP; 579 goto out; 580 } 581 582 bat_priv->algo_ops->gw.dump(msg, cb, bat_priv); 583 584 ret = msg->len; 585 586out: 587 if (primary_if) 588 batadv_hardif_put(primary_if); 589 if (soft_iface) 590 dev_put(soft_iface); 591 592 return ret; 593} 594 595/** 596 * batadv_gw_dhcp_recipient_get() - check if a packet is a DHCP message 597 * @skb: the packet to check 598 * @header_len: a pointer to the batman-adv header size 599 * @chaddr: buffer where the client address will be stored. Valid 600 * only if the function returns BATADV_DHCP_TO_CLIENT 601 * 602 * This function may re-allocate the data buffer of the skb passed as argument. 603 * 604 * Return: 605 * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error 606 * while parsing it 607 * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server 608 * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client 609 */ 610enum batadv_dhcp_recipient 611batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len, 612 u8 *chaddr) 613{ 614 enum batadv_dhcp_recipient ret = BATADV_DHCP_NO; 615 struct ethhdr *ethhdr; 616 struct iphdr *iphdr; 617 struct ipv6hdr *ipv6hdr; 618 struct udphdr *udphdr; 619 struct vlan_ethhdr *vhdr; 620 int chaddr_offset; 621 __be16 proto; 622 u8 *p; 623 624 /* check for ethernet header */ 625 if (!pskb_may_pull(skb, *header_len + ETH_HLEN)) 626 return BATADV_DHCP_NO; 627 628 ethhdr = eth_hdr(skb); 629 proto = ethhdr->h_proto; 630 *header_len += ETH_HLEN; 631 632 /* check for initial vlan header */ 633 if (proto == htons(ETH_P_8021Q)) { 634 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN)) 635 return BATADV_DHCP_NO; 636 637 vhdr = vlan_eth_hdr(skb); 638 proto = vhdr->h_vlan_encapsulated_proto; 639 *header_len += VLAN_HLEN; 640 } 641 642 /* check for ip header */ 643 switch (proto) { 644 case htons(ETH_P_IP): 645 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr))) 646 return BATADV_DHCP_NO; 647 648 iphdr = (struct iphdr *)(skb->data + *header_len); 649 *header_len += iphdr->ihl * 4; 650 651 /* check for udp header */ 652 if (iphdr->protocol != IPPROTO_UDP) 653 return BATADV_DHCP_NO; 654 655 break; 656 case htons(ETH_P_IPV6): 657 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr))) 658 return BATADV_DHCP_NO; 659 660 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len); 661 *header_len += sizeof(*ipv6hdr); 662 663 /* check for udp header */ 664 if (ipv6hdr->nexthdr != IPPROTO_UDP) 665 return BATADV_DHCP_NO; 666 667 break; 668 default: 669 return BATADV_DHCP_NO; 670 } 671 672 if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr))) 673 return BATADV_DHCP_NO; 674 675 udphdr = (struct udphdr *)(skb->data + *header_len); 676 *header_len += sizeof(*udphdr); 677 678 /* check for bootp port */ 679 switch (proto) { 680 case htons(ETH_P_IP): 681 if (udphdr->dest == htons(67)) 682 ret = BATADV_DHCP_TO_SERVER; 683 else if (udphdr->source == htons(67)) 684 ret = BATADV_DHCP_TO_CLIENT; 685 break; 686 case htons(ETH_P_IPV6): 687 if (udphdr->dest == htons(547)) 688 ret = BATADV_DHCP_TO_SERVER; 689 else if (udphdr->source == htons(547)) 690 ret = BATADV_DHCP_TO_CLIENT; 691 break; 692 } 693 694 chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET; 695 /* store the client address if the message is going to a client */ 696 if (ret == BATADV_DHCP_TO_CLIENT) { 697 if (!pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) 698 return BATADV_DHCP_NO; 699 700 /* check if the DHCP packet carries an Ethernet DHCP */ 701 p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET; 702 if (*p != BATADV_DHCP_HTYPE_ETHERNET) 703 return BATADV_DHCP_NO; 704 705 /* check if the DHCP packet carries a valid Ethernet address */ 706 p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET; 707 if (*p != ETH_ALEN) 708 return BATADV_DHCP_NO; 709 710 ether_addr_copy(chaddr, skb->data + chaddr_offset); 711 } 712 713 return ret; 714} 715 716/** 717 * batadv_gw_out_of_range() - check if the dhcp request destination is the best 718 * gateway 719 * @bat_priv: the bat priv with all the soft interface information 720 * @skb: the outgoing packet 721 * 722 * Check if the skb is a DHCP request and if it is sent to the current best GW 723 * server. Due to topology changes it may be the case that the GW server 724 * previously selected is not the best one anymore. 725 * 726 * This call might reallocate skb data. 727 * Must be invoked only when the DHCP packet is going TO a DHCP SERVER. 728 * 729 * Return: true if the packet destination is unicast and it is not the best gw, 730 * false otherwise. 731 */ 732bool batadv_gw_out_of_range(struct batadv_priv *bat_priv, 733 struct sk_buff *skb) 734{ 735 struct batadv_neigh_node *neigh_curr = NULL; 736 struct batadv_neigh_node *neigh_old = NULL; 737 struct batadv_orig_node *orig_dst_node = NULL; 738 struct batadv_gw_node *gw_node = NULL; 739 struct batadv_gw_node *curr_gw = NULL; 740 struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo; 741 struct ethhdr *ethhdr = (struct ethhdr *)skb->data; 742 bool out_of_range = false; 743 u8 curr_tq_avg; 744 unsigned short vid; 745 746 vid = batadv_get_vid(skb, 0); 747 748 if (is_multicast_ether_addr(ethhdr->h_dest)) 749 goto out; 750 751 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source, 752 ethhdr->h_dest, vid); 753 if (!orig_dst_node) 754 goto out; 755 756 gw_node = batadv_gw_node_get(bat_priv, orig_dst_node); 757 if (!gw_node) 758 goto out; 759 760 switch (atomic_read(&bat_priv->gw.mode)) { 761 case BATADV_GW_MODE_SERVER: 762 /* If we are a GW then we are our best GW. We can artificially 763 * set the tq towards ourself as the maximum value 764 */ 765 curr_tq_avg = BATADV_TQ_MAX_VALUE; 766 break; 767 case BATADV_GW_MODE_CLIENT: 768 curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 769 if (!curr_gw) 770 goto out; 771 772 /* packet is going to our gateway */ 773 if (curr_gw->orig_node == orig_dst_node) 774 goto out; 775 776 /* If the dhcp packet has been sent to a different gw, 777 * we have to evaluate whether the old gw is still 778 * reliable enough 779 */ 780 neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node, 781 NULL); 782 if (!neigh_curr) 783 goto out; 784 785 curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr, 786 BATADV_IF_DEFAULT); 787 if (!curr_ifinfo) 788 goto out; 789 790 curr_tq_avg = curr_ifinfo->bat_iv.tq_avg; 791 batadv_neigh_ifinfo_put(curr_ifinfo); 792 793 break; 794 case BATADV_GW_MODE_OFF: 795 default: 796 goto out; 797 } 798 799 neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL); 800 if (!neigh_old) 801 goto out; 802 803 old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT); 804 if (!old_ifinfo) 805 goto out; 806 807 if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD) 808 out_of_range = true; 809 batadv_neigh_ifinfo_put(old_ifinfo); 810 811out: 812 if (orig_dst_node) 813 batadv_orig_node_put(orig_dst_node); 814 if (curr_gw) 815 batadv_gw_node_put(curr_gw); 816 if (gw_node) 817 batadv_gw_node_put(gw_node); 818 if (neigh_old) 819 batadv_neigh_node_put(neigh_old); 820 if (neigh_curr) 821 batadv_neigh_node_put(neigh_curr); 822 return out_of_range; 823} 824