1 // SPDX-License-Identifier: GPL-2.0 2 /* OpenVPN data channel offload 3 * 4 * Copyright (C) 2020-2025 OpenVPN, Inc. 5 * 6 * Author: James Yonan <james@openvpn.net> 7 * Antonio Quartulli <antonio@openvpn.net> 8 */ 9 10 #include <linux/skbuff.h> 11 #include <linux/list.h> 12 #include <linux/hashtable.h> 13 #include <net/ip6_route.h> 14 15 #include "ovpnpriv.h" 16 #include "bind.h" 17 #include "pktid.h" 18 #include "crypto.h" 19 #include "io.h" 20 #include "main.h" 21 #include "netlink.h" 22 #include "peer.h" 23 #include "socket.h" 24 25 static void unlock_ovpn(struct ovpn_priv *ovpn, 26 struct llist_head *release_list) 27 __releases(&ovpn->lock) 28 { 29 struct ovpn_peer *peer, *next; 30 31 spin_unlock_bh(&ovpn->lock); 32 33 llist_for_each_entry_safe(peer, next, release_list->first, 34 release_entry) { 35 ovpn_socket_release(peer); 36 ovpn_peer_put(peer); 37 } 38 } 39 40 /** 41 * ovpn_peer_keepalive_set - configure keepalive values for peer 42 * @peer: the peer to configure 43 * @interval: outgoing keepalive interval 44 * @timeout: incoming keepalive timeout 45 */ 46 void ovpn_peer_keepalive_set(struct ovpn_peer *peer, u32 interval, u32 timeout) 47 { 48 time64_t now = ktime_get_boottime_seconds(); 49 50 netdev_dbg(peer->ovpn->dev, 51 "scheduling keepalive for peer %u: interval=%u timeout=%u\n", 52 peer->id, interval, timeout); 53 54 peer->keepalive_interval = interval; 55 WRITE_ONCE(peer->last_sent, now); 56 peer->keepalive_xmit_exp = now + interval; 57 58 peer->keepalive_timeout = timeout; 59 WRITE_ONCE(peer->last_recv, now); 60 peer->keepalive_recv_exp = now + timeout; 61 62 /* now that interval and timeout have been changed, kick 63 * off the worker so that the next delay can be recomputed 64 */ 65 mod_delayed_work(system_percpu_wq, &peer->ovpn->keepalive_work, 0); 66 } 67 68 /** 69 * ovpn_peer_keepalive_send - periodic worker sending keepalive packets 70 * @work: pointer to the work member of the related peer object 71 * 72 * NOTE: the reference to peer is not dropped because it gets inherited 73 * by ovpn_xmit_special() 74 */ 75 static void ovpn_peer_keepalive_send(struct work_struct *work) 76 { 77 struct ovpn_peer *peer = container_of(work, struct ovpn_peer, 78 keepalive_work); 79 80 local_bh_disable(); 81 ovpn_xmit_special(peer, ovpn_keepalive_message, 82 sizeof(ovpn_keepalive_message)); 83 local_bh_enable(); 84 } 85 86 /** 87 * ovpn_peer_new - allocate and initialize a new peer object 88 * @ovpn: the openvpn instance inside which the peer should be created 89 * @id: the ID assigned to this peer 90 * 91 * Return: a pointer to the new peer on success or an error code otherwise 92 */ 93 struct ovpn_peer *ovpn_peer_new(struct ovpn_priv *ovpn, u32 id) 94 { 95 struct ovpn_peer *peer; 96 int ret; 97 98 /* alloc and init peer object */ 99 peer = kzalloc_obj(*peer); 100 if (!peer) 101 return ERR_PTR(-ENOMEM); 102 103 /* in the default case TX and RX IDs are the same. 104 * the user may set a different TX ID via netlink 105 */ 106 peer->id = id; 107 peer->tx_id = id; 108 peer->ovpn = ovpn; 109 110 peer->vpn_addrs.ipv4.s_addr = htonl(INADDR_ANY); 111 peer->vpn_addrs.ipv6 = in6addr_any; 112 113 RCU_INIT_POINTER(peer->bind, NULL); 114 ovpn_crypto_state_init(&peer->crypto); 115 spin_lock_init(&peer->lock); 116 kref_init(&peer->refcount); 117 ovpn_peer_stats_init(&peer->vpn_stats); 118 ovpn_peer_stats_init(&peer->link_stats); 119 INIT_WORK(&peer->keepalive_work, ovpn_peer_keepalive_send); 120 121 ret = dst_cache_init(&peer->dst_cache, GFP_KERNEL); 122 if (ret < 0) { 123 netdev_err(ovpn->dev, 124 "cannot initialize dst cache for peer %u\n", 125 peer->id); 126 kfree(peer); 127 return ERR_PTR(ret); 128 } 129 130 netdev_hold(ovpn->dev, &peer->dev_tracker, GFP_KERNEL); 131 132 return peer; 133 } 134 135 /** 136 * ovpn_peer_reset_sockaddr - recreate binding for peer 137 * @peer: peer to recreate the binding for 138 * @ss: sockaddr to use as remote endpoint for the binding 139 * @local_ip: local IP for the binding 140 * 141 * Return: 0 on success or a negative error code otherwise 142 */ 143 int ovpn_peer_reset_sockaddr(struct ovpn_peer *peer, 144 const struct sockaddr_storage *ss, 145 const void *local_ip) 146 { 147 struct ovpn_bind *bind; 148 size_t ip_len; 149 150 lockdep_assert_held(&peer->lock); 151 152 /* create new ovpn_bind object */ 153 bind = ovpn_bind_from_sockaddr(ss); 154 if (IS_ERR(bind)) 155 return PTR_ERR(bind); 156 157 if (local_ip) { 158 if (ss->ss_family == AF_INET) { 159 ip_len = sizeof(struct in_addr); 160 } else if (ss->ss_family == AF_INET6) { 161 ip_len = sizeof(struct in6_addr); 162 } else { 163 net_dbg_ratelimited("%s: invalid family %u for remote endpoint for peer %u\n", 164 netdev_name(peer->ovpn->dev), 165 ss->ss_family, peer->id); 166 kfree(bind); 167 return -EINVAL; 168 } 169 170 memcpy(&bind->local, local_ip, ip_len); 171 } 172 173 /* set binding */ 174 ovpn_bind_reset(peer, bind); 175 176 return 0; 177 } 178 179 /* variable name __tbl2 needs to be different from __tbl1 180 * in the macro below to avoid confusing clang 181 */ 182 #define ovpn_get_hash_slot(_tbl, _key, _key_len) ({ \ 183 typeof(_tbl) *__tbl2 = &(_tbl); \ 184 jhash(_key, _key_len, 0) % HASH_SIZE(*__tbl2); \ 185 }) 186 187 #define ovpn_get_hash_head(_tbl, _key, _key_len) ({ \ 188 typeof(_tbl) *__tbl1 = &(_tbl); \ 189 &(*__tbl1)[ovpn_get_hash_slot(*__tbl1, _key, _key_len)];\ 190 }) 191 192 static void __ovpn_peer_hash_transp_addr(struct ovpn_peer *peer, 193 const struct ovpn_bind *bind); 194 195 /** 196 * ovpn_peer_endpoints_update - update remote or local endpoint for peer 197 * @peer: peer to update the remote endpoint for 198 * @skb: incoming packet to retrieve the source/destination address from 199 */ 200 void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb) 201 { 202 struct sockaddr_storage ss; 203 struct sockaddr_in6 *sa6; 204 bool reset_cache = false; 205 struct sockaddr_in *sa; 206 struct ovpn_bind *bind; 207 const void *local_ip; 208 size_t salen = 0; 209 210 spin_lock_bh(&peer->lock); 211 bind = rcu_dereference_protected(peer->bind, 212 lockdep_is_held(&peer->lock)); 213 if (unlikely(!bind)) 214 goto unlock; 215 216 switch (skb->protocol) { 217 case htons(ETH_P_IP): 218 /* float check */ 219 if (unlikely(!ovpn_bind_skb_src_match(bind, skb))) { 220 /* unconditionally save local endpoint in case 221 * of float, as it may have changed as well 222 */ 223 local_ip = &ip_hdr(skb)->daddr; 224 sa = (struct sockaddr_in *)&ss; 225 /* use a designated initializer so the sin_zero padding 226 * is zeroed (it ends up in the by_transp_addr hash key) 227 * without memset-ing the whole sockaddr_storage on the 228 * RX fast path 229 */ 230 *sa = (struct sockaddr_in) { 231 .sin_family = AF_INET, 232 .sin_addr.s_addr = ip_hdr(skb)->saddr, 233 .sin_port = udp_hdr(skb)->source, 234 }; 235 salen = sizeof(*sa); 236 reset_cache = true; 237 break; 238 } 239 240 /* if no float happened, let's double check if the local endpoint 241 * has changed 242 */ 243 if (unlikely(bind->local.ipv4.s_addr != ip_hdr(skb)->daddr)) { 244 net_dbg_ratelimited("%s: learning local IPv4 for peer %d (%pI4 -> %pI4)\n", 245 netdev_name(peer->ovpn->dev), 246 peer->id, &bind->local.ipv4.s_addr, 247 &ip_hdr(skb)->daddr); 248 bind->local.ipv4.s_addr = ip_hdr(skb)->daddr; 249 reset_cache = true; 250 } 251 break; 252 case htons(ETH_P_IPV6): 253 /* float check */ 254 if (unlikely(!ovpn_bind_skb_src_match(bind, skb))) { 255 /* unconditionally save local endpoint in case 256 * of float, as it may have changed as well 257 */ 258 local_ip = &ipv6_hdr(skb)->daddr; 259 sa6 = (struct sockaddr_in6 *)&ss; 260 /* use a designated initializer so the sin6_flowinfo 261 * padding is zeroed (it ends up in the by_transp_addr 262 * hash key) without memset-ing the whole 263 * sockaddr_storage on the RX fast path 264 */ 265 *sa6 = (struct sockaddr_in6) { 266 .sin6_family = AF_INET6, 267 .sin6_addr = ipv6_hdr(skb)->saddr, 268 .sin6_port = udp_hdr(skb)->source, 269 .sin6_scope_id = 270 ipv6_iface_scope_id(&ipv6_hdr(skb)->saddr, 271 skb->skb_iif), 272 }; 273 salen = sizeof(*sa6); 274 reset_cache = true; 275 break; 276 } 277 278 /* if no float happened, let's double check if the local endpoint 279 * has changed 280 */ 281 if (unlikely(!ipv6_addr_equal(&bind->local.ipv6, 282 &ipv6_hdr(skb)->daddr))) { 283 net_dbg_ratelimited("%s: learning local IPv6 for peer %d (%pI6c -> %pI6c)\n", 284 netdev_name(peer->ovpn->dev), 285 peer->id, &bind->local.ipv6, 286 &ipv6_hdr(skb)->daddr); 287 bind->local.ipv6 = ipv6_hdr(skb)->daddr; 288 reset_cache = true; 289 } 290 break; 291 default: 292 goto unlock; 293 } 294 295 if (unlikely(reset_cache)) 296 dst_cache_reset(&peer->dst_cache); 297 298 /* if the peer did not float, we can bail out now */ 299 if (likely(!salen)) 300 goto unlock; 301 302 if (unlikely(ovpn_peer_reset_sockaddr(peer, 303 (struct sockaddr_storage *)&ss, 304 local_ip) < 0)) 305 goto unlock; 306 307 net_dbg_ratelimited("%s: peer %d floated to %pIScp", 308 netdev_name(peer->ovpn->dev), peer->id, &ss); 309 310 spin_unlock_bh(&peer->lock); 311 312 ovpn_nl_peer_float_notify(peer, &ss); 313 314 /* rehashing is required only in MP mode as P2P has one peer 315 * only and thus there is no hashtable. 316 * 317 * This function may be invoked concurrently, so re-read peer->bind 318 * under the proper locks and rehash against its current value. 319 */ 320 if (peer->ovpn->mode != OVPN_MODE_MP) 321 return; 322 323 /* This function may be invoked concurrently, therefore another 324 * float may have happened in parallel: re-acquire the locks and 325 * rehash using the peer->bind->remote directly as key 326 */ 327 spin_lock_bh(&peer->ovpn->lock); 328 spin_lock_bh(&peer->lock); 329 bind = rcu_dereference_protected(peer->bind, 330 lockdep_is_held(&peer->lock)); 331 __ovpn_peer_hash_transp_addr(peer, bind); 332 spin_unlock_bh(&peer->lock); 333 spin_unlock_bh(&peer->ovpn->lock); 334 return; 335 unlock: 336 spin_unlock_bh(&peer->lock); 337 } 338 339 /** 340 * ovpn_peer_release_rcu - RCU callback performing last peer release steps 341 * @head: RCU member of the ovpn_peer 342 */ 343 static void ovpn_peer_release_rcu(struct rcu_head *head) 344 { 345 struct ovpn_peer *peer = container_of(head, struct ovpn_peer, rcu); 346 347 /* this call will immediately free the dst_cache, therefore we 348 * perform it in the RCU callback, when all contexts are done 349 */ 350 dst_cache_destroy(&peer->dst_cache); 351 kfree(peer); 352 } 353 354 /** 355 * ovpn_peer_release - release peer private members 356 * @peer: the peer to release 357 */ 358 static void ovpn_peer_release(struct ovpn_peer *peer) 359 { 360 ovpn_crypto_state_release(&peer->crypto); 361 spin_lock_bh(&peer->lock); 362 ovpn_bind_reset(peer, NULL); 363 spin_unlock_bh(&peer->lock); 364 call_rcu(&peer->rcu, ovpn_peer_release_rcu); 365 netdev_put(peer->ovpn->dev, &peer->dev_tracker); 366 } 367 368 /** 369 * ovpn_peer_release_kref - callback for kref_put 370 * @kref: the kref object belonging to the peer 371 */ 372 void ovpn_peer_release_kref(struct kref *kref) 373 { 374 struct ovpn_peer *peer = container_of(kref, struct ovpn_peer, refcount); 375 376 ovpn_peer_release(peer); 377 } 378 379 /** 380 * ovpn_peer_skb_to_sockaddr - fill sockaddr with skb source address 381 * @skb: the packet to extract data from 382 * @ss: the sockaddr to fill 383 * 384 * Return: sockaddr length on success or -1 otherwise 385 */ 386 static int ovpn_peer_skb_to_sockaddr(struct sk_buff *skb, 387 struct sockaddr_storage *ss) 388 { 389 struct sockaddr_in6 *sa6; 390 struct sockaddr_in *sa4; 391 392 switch (skb->protocol) { 393 case htons(ETH_P_IP): 394 sa4 = (struct sockaddr_in *)ss; 395 sa4->sin_family = AF_INET; 396 sa4->sin_addr.s_addr = ip_hdr(skb)->saddr; 397 sa4->sin_port = udp_hdr(skb)->source; 398 return sizeof(*sa4); 399 case htons(ETH_P_IPV6): 400 sa6 = (struct sockaddr_in6 *)ss; 401 sa6->sin6_family = AF_INET6; 402 sa6->sin6_addr = ipv6_hdr(skb)->saddr; 403 sa6->sin6_port = udp_hdr(skb)->source; 404 return sizeof(*sa6); 405 } 406 407 return -1; 408 } 409 410 /** 411 * ovpn_nexthop_from_skb4 - retrieve IPv4 nexthop for outgoing skb 412 * @skb: the outgoing packet 413 * 414 * Return: the IPv4 of the nexthop 415 */ 416 static __be32 ovpn_nexthop_from_skb4(struct sk_buff *skb) 417 { 418 const struct rtable *rt = skb_rtable(skb); 419 420 if (rt && rt->rt_uses_gateway) 421 return rt->rt_gw4; 422 423 return ip_hdr(skb)->daddr; 424 } 425 426 /** 427 * ovpn_nexthop_from_skb6 - retrieve IPv6 nexthop for outgoing skb 428 * @skb: the outgoing packet 429 * 430 * Return: the IPv6 of the nexthop 431 */ 432 static struct in6_addr ovpn_nexthop_from_skb6(struct sk_buff *skb) 433 { 434 const struct rt6_info *rt = skb_rt6_info(skb); 435 436 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 437 return ipv6_hdr(skb)->daddr; 438 439 return rt->rt6i_gateway; 440 } 441 442 /** 443 * ovpn_peer_get_by_vpn_addr4 - retrieve peer by its VPN IPv4 address 444 * @ovpn: the openvpn instance to search 445 * @addr: VPN IPv4 to use as search key 446 * 447 * Refcounter is not increased for the returned peer. 448 * 449 * Return: the peer if found or NULL otherwise 450 */ 451 static struct ovpn_peer *ovpn_peer_get_by_vpn_addr4(struct ovpn_priv *ovpn, 452 __be32 addr) 453 { 454 struct hlist_nulls_head *nhead; 455 struct hlist_nulls_node *ntmp; 456 struct ovpn_peer *tmp; 457 unsigned int slot; 458 459 begin: 460 slot = ovpn_get_hash_slot(ovpn->peers->by_vpn_addr4, &addr, 461 sizeof(addr)); 462 nhead = &ovpn->peers->by_vpn_addr4[slot]; 463 464 hlist_nulls_for_each_entry_rcu(tmp, ntmp, nhead, hash_entry_addr4) 465 if (addr == tmp->vpn_addrs.ipv4.s_addr) 466 return tmp; 467 468 /* item may have moved during lookup - check nulls and restart 469 * if that's the case 470 */ 471 if (get_nulls_value(ntmp) != slot) 472 goto begin; 473 474 return NULL; 475 } 476 477 /** 478 * ovpn_peer_get_by_vpn_addr6 - retrieve peer by its VPN IPv6 address 479 * @ovpn: the openvpn instance to search 480 * @addr: VPN IPv6 to use as search key 481 * 482 * Refcounter is not increased for the returned peer. 483 * 484 * Return: the peer if found or NULL otherwise 485 */ 486 static struct ovpn_peer *ovpn_peer_get_by_vpn_addr6(struct ovpn_priv *ovpn, 487 struct in6_addr *addr) 488 { 489 struct hlist_nulls_head *nhead; 490 struct hlist_nulls_node *ntmp; 491 struct ovpn_peer *tmp; 492 unsigned int slot; 493 494 begin: 495 slot = ovpn_get_hash_slot(ovpn->peers->by_vpn_addr6, addr, 496 sizeof(*addr)); 497 nhead = &ovpn->peers->by_vpn_addr6[slot]; 498 499 hlist_nulls_for_each_entry_rcu(tmp, ntmp, nhead, hash_entry_addr6) 500 if (ipv6_addr_equal(addr, &tmp->vpn_addrs.ipv6)) 501 return tmp; 502 503 /* item may have moved during lookup - check nulls and restart 504 * if that's the case 505 */ 506 if (get_nulls_value(ntmp) != slot) 507 goto begin; 508 509 return NULL; 510 } 511 512 /** 513 * ovpn_peer_transp_match - check if sockaddr and peer binding match 514 * @peer: the peer to get the binding from 515 * @ss: the sockaddr to match 516 * 517 * Return: true if sockaddr and binding match or false otherwise 518 */ 519 static bool ovpn_peer_transp_match(const struct ovpn_peer *peer, 520 const struct sockaddr_storage *ss) 521 { 522 struct ovpn_bind *bind = rcu_dereference(peer->bind); 523 struct sockaddr_in6 *sa6; 524 struct sockaddr_in *sa4; 525 526 if (unlikely(!bind)) 527 return false; 528 529 if (ss->ss_family != bind->remote.in4.sin_family) 530 return false; 531 532 switch (ss->ss_family) { 533 case AF_INET: 534 sa4 = (struct sockaddr_in *)ss; 535 if (sa4->sin_addr.s_addr != bind->remote.in4.sin_addr.s_addr) 536 return false; 537 if (sa4->sin_port != bind->remote.in4.sin_port) 538 return false; 539 break; 540 case AF_INET6: 541 sa6 = (struct sockaddr_in6 *)ss; 542 if (!ipv6_addr_equal(&sa6->sin6_addr, 543 &bind->remote.in6.sin6_addr)) 544 return false; 545 if (sa6->sin6_port != bind->remote.in6.sin6_port) 546 return false; 547 break; 548 default: 549 return false; 550 } 551 552 return true; 553 } 554 555 /** 556 * ovpn_peer_get_by_transp_addr_p2p - get peer by transport address in a P2P 557 * instance 558 * @ovpn: the openvpn instance to search 559 * @ss: the transport socket address 560 * 561 * Return: the peer if found or NULL otherwise 562 */ 563 static struct ovpn_peer * 564 ovpn_peer_get_by_transp_addr_p2p(struct ovpn_priv *ovpn, 565 struct sockaddr_storage *ss) 566 { 567 struct ovpn_peer *tmp, *peer = NULL; 568 569 rcu_read_lock(); 570 tmp = rcu_dereference(ovpn->peer); 571 if (likely(tmp && ovpn_peer_transp_match(tmp, ss) && 572 ovpn_peer_hold(tmp))) 573 peer = tmp; 574 rcu_read_unlock(); 575 576 return peer; 577 } 578 579 /** 580 * ovpn_peer_get_by_transp_addr - retrieve peer by transport address 581 * @ovpn: the openvpn instance to search 582 * @skb: the skb to retrieve the source transport address from 583 * 584 * Return: a pointer to the peer if found or NULL otherwise 585 */ 586 struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_priv *ovpn, 587 struct sk_buff *skb) 588 { 589 struct ovpn_peer *tmp, *peer = NULL; 590 struct sockaddr_storage ss = { 0 }; 591 struct hlist_nulls_head *nhead; 592 struct hlist_nulls_node *ntmp; 593 unsigned int slot; 594 ssize_t sa_len; 595 596 sa_len = ovpn_peer_skb_to_sockaddr(skb, &ss); 597 if (unlikely(sa_len < 0)) 598 return NULL; 599 600 if (ovpn->mode == OVPN_MODE_P2P) 601 return ovpn_peer_get_by_transp_addr_p2p(ovpn, &ss); 602 603 rcu_read_lock(); 604 begin: 605 slot = ovpn_get_hash_slot(ovpn->peers->by_transp_addr, &ss, sa_len); 606 nhead = &ovpn->peers->by_transp_addr[slot]; 607 608 hlist_nulls_for_each_entry_rcu(tmp, ntmp, nhead, 609 hash_entry_transp_addr) { 610 if (!ovpn_peer_transp_match(tmp, &ss)) 611 continue; 612 613 if (!ovpn_peer_hold(tmp)) 614 continue; 615 616 peer = tmp; 617 break; 618 } 619 620 /* item may have moved during lookup - check nulls and restart 621 * if that's the case 622 */ 623 if (!peer && get_nulls_value(ntmp) != slot) 624 goto begin; 625 rcu_read_unlock(); 626 627 return peer; 628 } 629 630 /** 631 * ovpn_peer_get_by_id_p2p - get peer by ID in a P2P instance 632 * @ovpn: the openvpn instance to search 633 * @peer_id: the ID of the peer to find 634 * 635 * Return: the peer if found or NULL otherwise 636 */ 637 static struct ovpn_peer *ovpn_peer_get_by_id_p2p(struct ovpn_priv *ovpn, 638 u32 peer_id) 639 { 640 struct ovpn_peer *tmp, *peer = NULL; 641 642 rcu_read_lock(); 643 tmp = rcu_dereference(ovpn->peer); 644 if (likely(tmp && tmp->id == peer_id && ovpn_peer_hold(tmp))) 645 peer = tmp; 646 rcu_read_unlock(); 647 648 return peer; 649 } 650 651 /** 652 * ovpn_peer_get_by_id - retrieve peer by ID 653 * @ovpn: the openvpn instance to search 654 * @peer_id: the unique peer identifier to match 655 * 656 * Return: a pointer to the peer if found or NULL otherwise 657 */ 658 struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_priv *ovpn, u32 peer_id) 659 { 660 struct ovpn_peer *tmp, *peer = NULL; 661 struct hlist_head *head; 662 663 if (ovpn->mode == OVPN_MODE_P2P) 664 return ovpn_peer_get_by_id_p2p(ovpn, peer_id); 665 666 head = ovpn_get_hash_head(ovpn->peers->by_id, &peer_id, 667 sizeof(peer_id)); 668 669 rcu_read_lock(); 670 hlist_for_each_entry_rcu(tmp, head, hash_entry_id) { 671 if (tmp->id != peer_id) 672 continue; 673 674 if (!ovpn_peer_hold(tmp)) 675 continue; 676 677 peer = tmp; 678 break; 679 } 680 rcu_read_unlock(); 681 682 return peer; 683 } 684 685 static void ovpn_peer_remove(struct ovpn_peer *peer, 686 enum ovpn_del_peer_reason reason, 687 struct llist_head *release_list) 688 { 689 lockdep_assert_held(&peer->ovpn->lock); 690 691 switch (peer->ovpn->mode) { 692 case OVPN_MODE_MP: 693 /* prevent double remove */ 694 if (hlist_unhashed(&peer->hash_entry_id)) 695 return; 696 697 hlist_del_init_rcu(&peer->hash_entry_id); 698 hlist_nulls_del_init_rcu(&peer->hash_entry_addr4); 699 hlist_nulls_del_init_rcu(&peer->hash_entry_addr6); 700 hlist_nulls_del_init_rcu(&peer->hash_entry_transp_addr); 701 break; 702 case OVPN_MODE_P2P: 703 /* prevent double remove */ 704 if (peer != rcu_access_pointer(peer->ovpn->peer)) 705 return; 706 707 RCU_INIT_POINTER(peer->ovpn->peer, NULL); 708 /* in P2P mode the carrier is switched off when the peer is 709 * deleted so that third party protocols can react accordingly 710 */ 711 netif_carrier_off(peer->ovpn->dev); 712 break; 713 } 714 715 peer->delete_reason = reason; 716 ovpn_nl_peer_del_notify(peer); 717 718 /* append to provided list for later socket release and ref drop */ 719 llist_add(&peer->release_entry, release_list); 720 } 721 722 /** 723 * ovpn_peer_get_by_dst - Lookup peer to send skb to 724 * @ovpn: the private data representing the current VPN session 725 * @skb: the skb to extract the destination address from 726 * 727 * This function takes a tunnel packet and looks up the peer to send it to 728 * after encapsulation. The skb is expected to be the in-tunnel packet, without 729 * any OpenVPN related header. 730 * 731 * Assume that the IP header is accessible in the skb data. 732 * 733 * Return: the peer if found or NULL otherwise. 734 */ 735 struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_priv *ovpn, 736 struct sk_buff *skb) 737 { 738 struct ovpn_peer *peer = NULL; 739 struct in6_addr addr6; 740 __be32 addr4; 741 742 /* in P2P mode, no matter the destination, packets are always sent to 743 * the single peer listening on the other side 744 */ 745 if (ovpn->mode == OVPN_MODE_P2P) { 746 rcu_read_lock(); 747 peer = rcu_dereference(ovpn->peer); 748 if (unlikely(peer && !ovpn_peer_hold(peer))) 749 peer = NULL; 750 rcu_read_unlock(); 751 return peer; 752 } 753 754 rcu_read_lock(); 755 switch (skb->protocol) { 756 case htons(ETH_P_IP): 757 addr4 = ovpn_nexthop_from_skb4(skb); 758 peer = ovpn_peer_get_by_vpn_addr4(ovpn, addr4); 759 break; 760 case htons(ETH_P_IPV6): 761 addr6 = ovpn_nexthop_from_skb6(skb); 762 peer = ovpn_peer_get_by_vpn_addr6(ovpn, &addr6); 763 break; 764 } 765 766 if (unlikely(peer && !ovpn_peer_hold(peer))) 767 peer = NULL; 768 rcu_read_unlock(); 769 770 return peer; 771 } 772 773 /** 774 * ovpn_nexthop_from_rt4 - look up the IPv4 nexthop for the given destination 775 * @ovpn: the private data representing the current VPN session 776 * @dest: the destination to be looked up 777 * 778 * Looks up in the IPv4 system routing table the IP of the nexthop to be used 779 * to reach the destination passed as argument. If no nexthop can be found, the 780 * destination itself is returned as it probably has to be used as nexthop. 781 * 782 * Return: the IP of the next hop if found or dest itself otherwise 783 */ 784 static __be32 ovpn_nexthop_from_rt4(struct ovpn_priv *ovpn, __be32 dest) 785 { 786 struct rtable *rt; 787 struct flowi4 fl = { 788 .daddr = dest 789 }; 790 791 rt = ip_route_output_flow(dev_net(ovpn->dev), &fl, NULL); 792 if (IS_ERR(rt)) { 793 net_dbg_ratelimited("%s: no route to host %pI4\n", 794 netdev_name(ovpn->dev), &dest); 795 /* if we end up here this packet is probably going to be 796 * thrown away later 797 */ 798 return dest; 799 } 800 801 if (!rt->rt_uses_gateway) 802 goto out; 803 804 dest = rt->rt_gw4; 805 out: 806 ip_rt_put(rt); 807 return dest; 808 } 809 810 /** 811 * ovpn_nexthop_from_rt6 - look up the IPv6 nexthop for the given destination 812 * @ovpn: the private data representing the current VPN session 813 * @dest: the destination to be looked up 814 * 815 * Looks up in the IPv6 system routing table the IP of the nexthop to be used 816 * to reach the destination passed as argument. If no nexthop can be found, the 817 * destination itself is returned as it probably has to be used as nexthop. 818 * 819 * Return: the IP of the next hop if found or dest itself otherwise 820 */ 821 static struct in6_addr ovpn_nexthop_from_rt6(struct ovpn_priv *ovpn, 822 struct in6_addr dest) 823 { 824 #if IS_ENABLED(CONFIG_IPV6) 825 struct dst_entry *entry; 826 struct rt6_info *rt; 827 struct flowi6 fl = { 828 .daddr = dest, 829 }; 830 831 entry = ip6_dst_lookup_flow(dev_net(ovpn->dev), NULL, &fl, NULL); 832 if (IS_ERR(entry)) { 833 net_dbg_ratelimited("%s: no route to host %pI6c\n", 834 netdev_name(ovpn->dev), &dest); 835 /* if we end up here this packet is probably going to be 836 * thrown away later 837 */ 838 return dest; 839 } 840 841 rt = dst_rt6_info(entry); 842 843 if (!(rt->rt6i_flags & RTF_GATEWAY)) 844 goto out; 845 846 dest = rt->rt6i_gateway; 847 out: 848 dst_release((struct dst_entry *)rt); 849 #endif 850 return dest; 851 } 852 853 /** 854 * ovpn_peer_check_by_src - check that skb source is routed via peer 855 * @ovpn: the openvpn instance to search 856 * @skb: the packet to extract source address from 857 * @peer: the peer to check against the source address 858 * 859 * Return: true if the peer is matching or false otherwise 860 */ 861 bool ovpn_peer_check_by_src(struct ovpn_priv *ovpn, struct sk_buff *skb, 862 struct ovpn_peer *peer) 863 { 864 bool match = false; 865 struct in6_addr addr6; 866 __be32 addr4; 867 868 if (ovpn->mode == OVPN_MODE_P2P) { 869 /* in P2P mode, no matter the destination, packets are always 870 * sent to the single peer listening on the other side 871 */ 872 return peer == rcu_access_pointer(ovpn->peer); 873 } 874 875 /* This function performs a reverse path check, therefore we now 876 * lookup the nexthop we would use if we wanted to route a packet 877 * to the source IP. If the nexthop matches the sender we know the 878 * latter is valid and we allow the packet to come in 879 */ 880 881 switch (skb->protocol) { 882 case htons(ETH_P_IP): 883 addr4 = ovpn_nexthop_from_rt4(ovpn, ip_hdr(skb)->saddr); 884 rcu_read_lock(); 885 match = (peer == ovpn_peer_get_by_vpn_addr4(ovpn, addr4)); 886 rcu_read_unlock(); 887 break; 888 case htons(ETH_P_IPV6): 889 addr6 = ovpn_nexthop_from_rt6(ovpn, ipv6_hdr(skb)->saddr); 890 rcu_read_lock(); 891 match = (peer == ovpn_peer_get_by_vpn_addr6(ovpn, &addr6)); 892 rcu_read_unlock(); 893 break; 894 } 895 896 return match; 897 } 898 899 /* Move @peer to the by_transp_addr bucket matching its current bind. 900 * 901 * Caller must hold both peer->ovpn->lock and peer->lock, and must have 902 * already dereferenced a valid (non-NULL) peer->bind, passed in as @bind. 903 */ 904 static void __ovpn_peer_hash_transp_addr(struct ovpn_peer *peer, 905 const struct ovpn_bind *bind) 906 { 907 struct sockaddr_storage sa = {}; 908 struct hlist_nulls_head *nhead; 909 struct sockaddr_in6 *sa6; 910 struct sockaddr_in *sa4; 911 size_t salen; 912 913 lockdep_assert_held(&peer->ovpn->lock); 914 lockdep_assert_held(&peer->lock); 915 916 if (WARN_ON_ONCE(!bind)) 917 return; 918 919 /* peer may have been concurrently removed between the caller's 920 * initial lookup and our acquisition of ovpn->lock; skip the 921 * rehash so we don't re-insert a removed peer 922 */ 923 if (unlikely(hlist_unhashed(&peer->hash_entry_id))) 924 return; 925 926 /* Build the hash key from the transport identity only 927 * (family/address/port), matching ovpn_peer_add_mp() and the lookup 928 * in ovpn_peer_get_by_transp_addr(). Hashing bind->remote directly 929 * would fold in sin6_scope_id (set on the float path but never by the 930 * lookup), scattering the peer into a bucket lookups cannot reach. 931 */ 932 switch (bind->remote.in4.sin_family) { 933 case AF_INET: 934 sa4 = (struct sockaddr_in *)&sa; 935 sa4->sin_family = AF_INET; 936 sa4->sin_addr.s_addr = bind->remote.in4.sin_addr.s_addr; 937 sa4->sin_port = bind->remote.in4.sin_port; 938 salen = sizeof(*sa4); 939 break; 940 case AF_INET6: 941 sa6 = (struct sockaddr_in6 *)&sa; 942 sa6->sin6_family = AF_INET6; 943 sa6->sin6_addr = bind->remote.in6.sin6_addr; 944 sa6->sin6_port = bind->remote.in6.sin6_port; 945 salen = sizeof(*sa6); 946 break; 947 default: 948 return; 949 } 950 951 /* remove old hashing (no-op if entry is not currently linked) */ 952 hlist_nulls_del_init_rcu(&peer->hash_entry_transp_addr); 953 /* re-add with current transport address */ 954 nhead = ovpn_get_hash_head(peer->ovpn->peers->by_transp_addr, &sa, 955 salen); 956 hlist_nulls_add_head_rcu(&peer->hash_entry_transp_addr, nhead); 957 } 958 959 void ovpn_peer_hash_transp_addr(struct ovpn_peer *peer) 960 { 961 struct ovpn_bind *bind; 962 963 lockdep_assert_held(&peer->ovpn->lock); 964 965 /* rehashing makes sense only in multipeer mode */ 966 if (peer->ovpn->mode != OVPN_MODE_MP) 967 return; 968 969 spin_lock_bh(&peer->lock); 970 bind = rcu_dereference_protected(peer->bind, 971 lockdep_is_held(&peer->lock)); 972 __ovpn_peer_hash_transp_addr(peer, bind); 973 spin_unlock_bh(&peer->lock); 974 } 975 976 void ovpn_peer_hash_vpn_ip(struct ovpn_peer *peer) 977 { 978 struct hlist_nulls_head *nhead; 979 980 lockdep_assert_held(&peer->ovpn->lock); 981 982 /* rehashing makes sense only in multipeer mode */ 983 if (peer->ovpn->mode != OVPN_MODE_MP) 984 return; 985 986 /* peer may have been concurrently removed between the caller's 987 * initial lookup and our acquisition of ovpn->lock; skip the 988 * rehash so we don't re-insert a removed peer 989 */ 990 if (hlist_unhashed(&peer->hash_entry_id)) 991 return; 992 993 if (peer->vpn_addrs.ipv4.s_addr != htonl(INADDR_ANY)) { 994 /* remove potential old hashing */ 995 hlist_nulls_del_init_rcu(&peer->hash_entry_addr4); 996 997 nhead = ovpn_get_hash_head(peer->ovpn->peers->by_vpn_addr4, 998 &peer->vpn_addrs.ipv4, 999 sizeof(peer->vpn_addrs.ipv4)); 1000 hlist_nulls_add_head_rcu(&peer->hash_entry_addr4, nhead); 1001 } 1002 1003 if (!ipv6_addr_any(&peer->vpn_addrs.ipv6)) { 1004 /* remove potential old hashing */ 1005 hlist_nulls_del_init_rcu(&peer->hash_entry_addr6); 1006 1007 nhead = ovpn_get_hash_head(peer->ovpn->peers->by_vpn_addr6, 1008 &peer->vpn_addrs.ipv6, 1009 sizeof(peer->vpn_addrs.ipv6)); 1010 hlist_nulls_add_head_rcu(&peer->hash_entry_addr6, nhead); 1011 } 1012 } 1013 1014 /** 1015 * ovpn_peer_add_mp - add peer to related tables in a MP instance 1016 * @ovpn: the instance to add the peer to 1017 * @peer: the peer to add 1018 * 1019 * Return: 0 on success or a negative error code otherwise 1020 */ 1021 static int ovpn_peer_add_mp(struct ovpn_priv *ovpn, struct ovpn_peer *peer) 1022 { 1023 struct sockaddr_storage sa = { 0 }; 1024 struct hlist_nulls_head *nhead; 1025 struct sockaddr_in6 *sa6; 1026 struct sockaddr_in *sa4; 1027 struct ovpn_bind *bind; 1028 struct ovpn_peer *tmp; 1029 size_t salen; 1030 int ret = 0; 1031 1032 spin_lock_bh(&ovpn->lock); 1033 /* do not add duplicates */ 1034 tmp = ovpn_peer_get_by_id(ovpn, peer->id); 1035 if (tmp) { 1036 ovpn_peer_put(tmp); 1037 ret = -EEXIST; 1038 goto out; 1039 } 1040 1041 bind = rcu_dereference_protected(peer->bind, true); 1042 /* peers connected via TCP have bind == NULL */ 1043 if (bind) { 1044 switch (bind->remote.in4.sin_family) { 1045 case AF_INET: 1046 sa4 = (struct sockaddr_in *)&sa; 1047 1048 sa4->sin_family = AF_INET; 1049 sa4->sin_addr.s_addr = bind->remote.in4.sin_addr.s_addr; 1050 sa4->sin_port = bind->remote.in4.sin_port; 1051 salen = sizeof(*sa4); 1052 break; 1053 case AF_INET6: 1054 sa6 = (struct sockaddr_in6 *)&sa; 1055 1056 sa6->sin6_family = AF_INET6; 1057 sa6->sin6_addr = bind->remote.in6.sin6_addr; 1058 sa6->sin6_port = bind->remote.in6.sin6_port; 1059 salen = sizeof(*sa6); 1060 break; 1061 default: 1062 ret = -EPROTONOSUPPORT; 1063 goto out; 1064 } 1065 1066 nhead = ovpn_get_hash_head(ovpn->peers->by_transp_addr, &sa, 1067 salen); 1068 hlist_nulls_add_head_rcu(&peer->hash_entry_transp_addr, nhead); 1069 } 1070 1071 hlist_add_head_rcu(&peer->hash_entry_id, 1072 ovpn_get_hash_head(ovpn->peers->by_id, &peer->id, 1073 sizeof(peer->id))); 1074 1075 ovpn_peer_hash_vpn_ip(peer); 1076 out: 1077 spin_unlock_bh(&ovpn->lock); 1078 return ret; 1079 } 1080 1081 /** 1082 * ovpn_peer_add_p2p - add peer to related tables in a P2P instance 1083 * @ovpn: the instance to add the peer to 1084 * @peer: the peer to add 1085 * 1086 * Return: 0 on success or a negative error code otherwise 1087 */ 1088 static int ovpn_peer_add_p2p(struct ovpn_priv *ovpn, struct ovpn_peer *peer) 1089 { 1090 LLIST_HEAD(release_list); 1091 struct ovpn_peer *tmp; 1092 1093 spin_lock_bh(&ovpn->lock); 1094 /* in p2p mode it is possible to have a single peer only, therefore the 1095 * old one is released and substituted by the new one 1096 */ 1097 tmp = rcu_dereference_protected(ovpn->peer, 1098 lockdep_is_held(&ovpn->lock)); 1099 if (tmp) 1100 ovpn_peer_remove(tmp, OVPN_DEL_PEER_REASON_TEARDOWN, 1101 &release_list); 1102 1103 rcu_assign_pointer(ovpn->peer, peer); 1104 /* in P2P mode the carrier is switched on when the peer is added */ 1105 netif_carrier_on(ovpn->dev); 1106 unlock_ovpn(ovpn, &release_list); 1107 1108 return 0; 1109 } 1110 1111 /** 1112 * ovpn_peer_add - add peer to the related tables 1113 * @ovpn: the openvpn instance the peer belongs to 1114 * @peer: the peer object to add 1115 * 1116 * Assume refcounter was increased by caller 1117 * 1118 * Return: 0 on success or a negative error code otherwise 1119 */ 1120 int ovpn_peer_add(struct ovpn_priv *ovpn, struct ovpn_peer *peer) 1121 { 1122 int ret = -ENODEV; 1123 1124 /* Prevent adding new peers while destroying the ovpn interface. 1125 * Failing to do so would end up holding the device reference 1126 * endlessly hostage of the new peer object with no chance of 1127 * release.. 1128 */ 1129 netdev_lock(ovpn->dev); 1130 if (ovpn->dev->reg_state != NETREG_REGISTERED) 1131 goto out; 1132 1133 switch (ovpn->mode) { 1134 case OVPN_MODE_MP: 1135 ret = ovpn_peer_add_mp(ovpn, peer); 1136 break; 1137 case OVPN_MODE_P2P: 1138 ret = ovpn_peer_add_p2p(ovpn, peer); 1139 break; 1140 } 1141 out: 1142 netdev_unlock(ovpn->dev); 1143 1144 return ret; 1145 } 1146 1147 /** 1148 * ovpn_peer_del_mp - delete peer from related tables in a MP instance 1149 * @peer: the peer to delete 1150 * @reason: reason why the peer was deleted (sent to userspace) 1151 * @release_list: list where delete peer should be appended 1152 * 1153 * Return: 0 on success or a negative error code otherwise 1154 */ 1155 static int ovpn_peer_del_mp(struct ovpn_peer *peer, 1156 enum ovpn_del_peer_reason reason, 1157 struct llist_head *release_list) 1158 { 1159 struct ovpn_peer *tmp; 1160 int ret = -ENOENT; 1161 1162 lockdep_assert_held(&peer->ovpn->lock); 1163 1164 tmp = ovpn_peer_get_by_id(peer->ovpn, peer->id); 1165 if (tmp == peer) { 1166 ovpn_peer_remove(peer, reason, release_list); 1167 ret = 0; 1168 } 1169 1170 if (tmp) 1171 ovpn_peer_put(tmp); 1172 1173 return ret; 1174 } 1175 1176 /** 1177 * ovpn_peer_del_p2p - delete peer from related tables in a P2P instance 1178 * @peer: the peer to delete 1179 * @reason: reason why the peer was deleted (sent to userspace) 1180 * @release_list: list where delete peer should be appended 1181 * 1182 * Return: 0 on success or a negative error code otherwise 1183 */ 1184 static int ovpn_peer_del_p2p(struct ovpn_peer *peer, 1185 enum ovpn_del_peer_reason reason, 1186 struct llist_head *release_list) 1187 { 1188 struct ovpn_peer *tmp; 1189 1190 lockdep_assert_held(&peer->ovpn->lock); 1191 1192 tmp = rcu_dereference_protected(peer->ovpn->peer, 1193 lockdep_is_held(&peer->ovpn->lock)); 1194 if (tmp != peer) 1195 return -ENOENT; 1196 1197 ovpn_peer_remove(peer, reason, release_list); 1198 1199 return 0; 1200 } 1201 1202 /** 1203 * ovpn_peer_del - delete peer from related tables 1204 * @peer: the peer object to delete 1205 * @reason: reason for deleting peer (will be sent to userspace) 1206 * 1207 * Return: 0 on success or a negative error code otherwise 1208 */ 1209 int ovpn_peer_del(struct ovpn_peer *peer, enum ovpn_del_peer_reason reason) 1210 { 1211 LLIST_HEAD(release_list); 1212 int ret = -EOPNOTSUPP; 1213 1214 spin_lock_bh(&peer->ovpn->lock); 1215 switch (peer->ovpn->mode) { 1216 case OVPN_MODE_MP: 1217 ret = ovpn_peer_del_mp(peer, reason, &release_list); 1218 break; 1219 case OVPN_MODE_P2P: 1220 ret = ovpn_peer_del_p2p(peer, reason, &release_list); 1221 break; 1222 default: 1223 break; 1224 } 1225 unlock_ovpn(peer->ovpn, &release_list); 1226 1227 return ret; 1228 } 1229 1230 /** 1231 * ovpn_peer_release_p2p - release peer upon P2P device teardown 1232 * @ovpn: the instance being torn down 1233 * @sk: if not NULL, release peer only if it's using this specific socket 1234 * @reason: the reason for releasing the peer 1235 */ 1236 static void ovpn_peer_release_p2p(struct ovpn_priv *ovpn, struct sock *sk, 1237 enum ovpn_del_peer_reason reason) 1238 { 1239 struct ovpn_socket *ovpn_sock; 1240 LLIST_HEAD(release_list); 1241 struct ovpn_peer *peer; 1242 1243 spin_lock_bh(&ovpn->lock); 1244 peer = rcu_dereference_protected(ovpn->peer, 1245 lockdep_is_held(&ovpn->lock)); 1246 if (!peer) { 1247 spin_unlock_bh(&ovpn->lock); 1248 return; 1249 } 1250 1251 if (sk) { 1252 ovpn_sock = rcu_dereference_bh(peer->sock); 1253 if (!ovpn_sock || ovpn_sock->sk != sk) { 1254 spin_unlock_bh(&ovpn->lock); 1255 return; 1256 } 1257 } 1258 1259 ovpn_peer_remove(peer, reason, &release_list); 1260 unlock_ovpn(ovpn, &release_list); 1261 } 1262 1263 static void ovpn_peers_release_mp(struct ovpn_priv *ovpn, struct sock *sk, 1264 enum ovpn_del_peer_reason reason) 1265 { 1266 struct ovpn_socket *ovpn_sock; 1267 LLIST_HEAD(release_list); 1268 struct ovpn_peer *peer; 1269 struct hlist_node *tmp; 1270 int bkt; 1271 1272 spin_lock_bh(&ovpn->lock); 1273 hash_for_each_safe(ovpn->peers->by_id, bkt, tmp, peer, hash_entry_id) { 1274 bool remove = true; 1275 1276 /* if a socket was passed as argument, skip all peers except 1277 * those using it 1278 */ 1279 if (sk) { 1280 rcu_read_lock(); 1281 ovpn_sock = rcu_dereference(peer->sock); 1282 remove = ovpn_sock && ovpn_sock->sk == sk; 1283 rcu_read_unlock(); 1284 } 1285 1286 if (remove) 1287 ovpn_peer_remove(peer, reason, &release_list); 1288 } 1289 unlock_ovpn(ovpn, &release_list); 1290 } 1291 1292 /** 1293 * ovpn_peers_free - free all peers in the instance 1294 * @ovpn: the instance whose peers should be released 1295 * @sk: if not NULL, only peers using this socket are removed and the socket 1296 * is released immediately 1297 * @reason: the reason for releasing all peers 1298 */ 1299 void ovpn_peers_free(struct ovpn_priv *ovpn, struct sock *sk, 1300 enum ovpn_del_peer_reason reason) 1301 { 1302 switch (ovpn->mode) { 1303 case OVPN_MODE_P2P: 1304 ovpn_peer_release_p2p(ovpn, sk, reason); 1305 break; 1306 case OVPN_MODE_MP: 1307 ovpn_peers_release_mp(ovpn, sk, reason); 1308 break; 1309 } 1310 } 1311 1312 static time64_t ovpn_peer_keepalive_work_single(struct ovpn_peer *peer, 1313 time64_t now, 1314 struct llist_head *release_list) 1315 { 1316 time64_t last_recv, last_sent, next_run1, next_run2; 1317 unsigned long timeout, interval; 1318 bool expired; 1319 1320 spin_lock_bh(&peer->lock); 1321 /* we expect both timers to be configured at the same time, 1322 * therefore bail out if either is not set 1323 */ 1324 if (!peer->keepalive_timeout || !peer->keepalive_interval) { 1325 spin_unlock_bh(&peer->lock); 1326 return 0; 1327 } 1328 1329 /* check for peer timeout */ 1330 expired = false; 1331 timeout = peer->keepalive_timeout; 1332 last_recv = READ_ONCE(peer->last_recv); 1333 if (now < last_recv + timeout) { 1334 peer->keepalive_recv_exp = last_recv + timeout; 1335 next_run1 = peer->keepalive_recv_exp; 1336 } else if (peer->keepalive_recv_exp > now) { 1337 next_run1 = peer->keepalive_recv_exp; 1338 } else { 1339 expired = true; 1340 } 1341 1342 if (expired) { 1343 /* peer is dead -> kill it and move on */ 1344 spin_unlock_bh(&peer->lock); 1345 netdev_dbg(peer->ovpn->dev, "peer %u expired\n", 1346 peer->id); 1347 ovpn_peer_remove(peer, OVPN_DEL_PEER_REASON_EXPIRED, 1348 release_list); 1349 return 0; 1350 } 1351 1352 /* check for peer keepalive */ 1353 expired = false; 1354 interval = peer->keepalive_interval; 1355 last_sent = READ_ONCE(peer->last_sent); 1356 if (now < last_sent + interval) { 1357 peer->keepalive_xmit_exp = last_sent + interval; 1358 next_run2 = peer->keepalive_xmit_exp; 1359 } else if (peer->keepalive_xmit_exp > now) { 1360 next_run2 = peer->keepalive_xmit_exp; 1361 } else { 1362 expired = true; 1363 next_run2 = now + interval; 1364 } 1365 spin_unlock_bh(&peer->lock); 1366 1367 if (expired) { 1368 /* a keepalive packet is required */ 1369 netdev_dbg(peer->ovpn->dev, 1370 "sending keepalive to peer %u\n", 1371 peer->id); 1372 if (WARN_ON(!ovpn_peer_hold(peer))) 1373 return 0; 1374 if (!schedule_work(&peer->keepalive_work)) 1375 ovpn_peer_put(peer); 1376 } 1377 1378 if (next_run1 < next_run2) 1379 return next_run1; 1380 1381 return next_run2; 1382 } 1383 1384 static time64_t ovpn_peer_keepalive_work_mp(struct ovpn_priv *ovpn, 1385 time64_t now, 1386 struct llist_head *release_list) 1387 { 1388 time64_t tmp_next_run, next_run = 0; 1389 struct hlist_node *tmp; 1390 struct ovpn_peer *peer; 1391 int bkt; 1392 1393 lockdep_assert_held(&ovpn->lock); 1394 1395 hash_for_each_safe(ovpn->peers->by_id, bkt, tmp, peer, hash_entry_id) { 1396 tmp_next_run = ovpn_peer_keepalive_work_single(peer, now, 1397 release_list); 1398 if (!tmp_next_run) 1399 continue; 1400 1401 /* the next worker run will be scheduled based on the shortest 1402 * required interval across all peers 1403 */ 1404 if (!next_run || tmp_next_run < next_run) 1405 next_run = tmp_next_run; 1406 } 1407 1408 return next_run; 1409 } 1410 1411 static time64_t ovpn_peer_keepalive_work_p2p(struct ovpn_priv *ovpn, 1412 time64_t now, 1413 struct llist_head *release_list) 1414 { 1415 struct ovpn_peer *peer; 1416 time64_t next_run = 0; 1417 1418 lockdep_assert_held(&ovpn->lock); 1419 1420 peer = rcu_dereference_protected(ovpn->peer, 1421 lockdep_is_held(&ovpn->lock)); 1422 if (peer) 1423 next_run = ovpn_peer_keepalive_work_single(peer, now, 1424 release_list); 1425 1426 return next_run; 1427 } 1428 1429 /** 1430 * ovpn_peer_keepalive_work - run keepalive logic on each known peer 1431 * @work: pointer to the work member of the related ovpn object 1432 * 1433 * Each peer has two timers (if configured): 1434 * 1. peer timeout: when no data is received for a certain interval, 1435 * the peer is considered dead and it gets killed. 1436 * 2. peer keepalive: when no data is sent to a certain peer for a 1437 * certain interval, a special 'keepalive' packet is explicitly sent. 1438 * 1439 * This function iterates across the whole peer collection while 1440 * checking the timers described above. 1441 */ 1442 void ovpn_peer_keepalive_work(struct work_struct *work) 1443 { 1444 struct ovpn_priv *ovpn = container_of(work, struct ovpn_priv, 1445 keepalive_work.work); 1446 time64_t next_run = 0, now = ktime_get_boottime_seconds(); 1447 LLIST_HEAD(release_list); 1448 1449 spin_lock_bh(&ovpn->lock); 1450 switch (ovpn->mode) { 1451 case OVPN_MODE_MP: 1452 next_run = ovpn_peer_keepalive_work_mp(ovpn, now, 1453 &release_list); 1454 break; 1455 case OVPN_MODE_P2P: 1456 next_run = ovpn_peer_keepalive_work_p2p(ovpn, now, 1457 &release_list); 1458 break; 1459 } 1460 1461 /* prevent rearming if the interface is being destroyed */ 1462 if (next_run > 0) { 1463 netdev_dbg(ovpn->dev, 1464 "scheduling keepalive work: now=%llu next_run=%llu delta=%llu\n", 1465 next_run, now, next_run - now); 1466 schedule_delayed_work(&ovpn->keepalive_work, 1467 (next_run - now) * HZ); 1468 } 1469 unlock_ovpn(ovpn, &release_list); 1470 } 1471