760-dynamic_own_ip.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. From: Felix Fietkau <[email protected]>
  2. Date: Thu, 15 Dec 2022 13:57:04 +0100
  3. Subject: [PATCH] hostapd: add support for automatically setting RADIUS own-ip
  4. dynamically
  5. Some servers use the NAS-IP-Address attribute as a destination address
  6. --- a/hostapd/config_file.c
  7. +++ b/hostapd/config_file.c
  8. @@ -2819,6 +2819,8 @@ static int hostapd_config_fill(struct ho
  9. } else if (os_strcmp(buf, "iapp_interface") == 0) {
  10. wpa_printf(MSG_INFO, "DEPRECATED: iapp_interface not used");
  11. #endif /* CONFIG_IAPP */
  12. + } else if (os_strcmp(buf, "dynamic_own_ip_addr") == 0) {
  13. + bss->dynamic_own_ip_addr = atoi(pos);
  14. } else if (os_strcmp(buf, "own_ip_addr") == 0) {
  15. if (hostapd_parse_ip_addr(pos, &bss->own_ip_addr)) {
  16. wpa_printf(MSG_ERROR,
  17. --- a/src/ap/ap_config.h
  18. +++ b/src/ap/ap_config.h
  19. @@ -310,6 +310,7 @@ struct hostapd_bss_config {
  20. unsigned int eap_sim_db_timeout;
  21. int eap_server_erp; /* Whether ERP is enabled on internal EAP server */
  22. struct hostapd_ip_addr own_ip_addr;
  23. + int dynamic_own_ip_addr;
  24. char *nas_identifier;
  25. struct hostapd_radius_servers *radius;
  26. int acct_interim_interval;
  27. --- a/src/ap/ieee802_1x.c
  28. +++ b/src/ap/ieee802_1x.c
  29. @@ -601,6 +601,10 @@ int add_common_radius_attr(struct hostap
  30. struct hostapd_radius_attr *attr;
  31. int len;
  32. + if (hapd->conf->dynamic_own_ip_addr)
  33. + radius_client_get_local_addr(hapd->radius,
  34. + &hapd->conf->own_ip_addr);
  35. +
  36. if (!hostapd_config_get_radius_attr(req_attr,
  37. RADIUS_ATTR_NAS_IP_ADDRESS) &&
  38. hapd->conf->own_ip_addr.af == AF_INET &&
  39. --- a/src/radius/radius_client.c
  40. +++ b/src/radius/radius_client.c
  41. @@ -165,6 +165,8 @@ struct radius_client_data {
  42. */
  43. void *ctx;
  44. + struct hostapd_ip_addr local_ip;
  45. +
  46. /**
  47. * conf - RADIUS client configuration (list of RADIUS servers to use)
  48. */
  49. @@ -819,6 +821,30 @@ static void radius_close_acct_socket(str
  50. /**
  51. + * radius_client_send - Get local address for the RADIUS auth socket
  52. + * @radius: RADIUS client context from radius_client_init()
  53. + * @addr: pointer to store the address
  54. + *
  55. + * This function returns the local address for the connection to the RADIUS
  56. + * auth server. It also opens the socket if it's not available yet.
  57. + */
  58. +int radius_client_get_local_addr(struct radius_client_data *radius,
  59. + struct hostapd_ip_addr *addr)
  60. +{
  61. + struct hostapd_radius_servers *conf = radius->conf;
  62. +
  63. + if (conf->auth_server && radius->auth_sock < 0)
  64. + radius_client_init_auth(radius);
  65. +
  66. + if (radius->auth_sock < 0)
  67. + return -1;
  68. +
  69. + memcpy(addr, &radius->local_ip, sizeof(*addr));
  70. +
  71. + return 0;
  72. +}
  73. +
  74. +/**
  75. * radius_client_send - Send a RADIUS request
  76. * @radius: RADIUS client context from radius_client_init()
  77. * @msg: RADIUS message to be sent
  78. @@ -1711,6 +1737,10 @@ radius_change_server(struct radius_clien
  79. wpa_printf(MSG_DEBUG, "RADIUS local address: %s:%u",
  80. inet_ntoa(claddr.sin_addr),
  81. ntohs(claddr.sin_port));
  82. + if (auth) {
  83. + radius->local_ip.af = AF_INET;
  84. + radius->local_ip.u.v4 = claddr.sin_addr;
  85. + }
  86. }
  87. break;
  88. #ifdef CONFIG_IPV6
  89. @@ -1722,6 +1752,10 @@ radius_change_server(struct radius_clien
  90. inet_ntop(AF_INET6, &claddr6.sin6_addr,
  91. abuf, sizeof(abuf)),
  92. ntohs(claddr6.sin6_port));
  93. + if (auth) {
  94. + radius->local_ip.af = AF_INET6;
  95. + radius->local_ip.u.v6 = claddr6.sin6_addr;
  96. + }
  97. }
  98. break;
  99. }
  100. --- a/src/radius/radius_client.h
  101. +++ b/src/radius/radius_client.h
  102. @@ -274,6 +274,8 @@ int radius_client_register(struct radius
  103. void radius_client_set_interim_error_cb(struct radius_client_data *radius,
  104. void (*cb)(const u8 *addr, void *ctx),
  105. void *ctx);
  106. +int radius_client_get_local_addr(struct radius_client_data *radius,
  107. + struct hostapd_ip_addr * addr);
  108. int radius_client_send(struct radius_client_data *radius,
  109. struct radius_msg *msg,
  110. RadiusType msg_type, const u8 *addr);