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