1f05cddf9SRui Paulo /*
2f05cddf9SRui Paulo * RADIUS Dynamic Authorization Server (DAS) (RFC 5176)
35b9c547cSRui Paulo * Copyright (c) 2012-2013, Jouni Malinen <j@w1.fi>
4f05cddf9SRui Paulo *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
7f05cddf9SRui Paulo */
8f05cddf9SRui Paulo
9f05cddf9SRui Paulo #include "includes.h"
10f05cddf9SRui Paulo #include <net/if.h>
11f05cddf9SRui Paulo
12f05cddf9SRui Paulo #include "utils/common.h"
13f05cddf9SRui Paulo #include "utils/eloop.h"
14f05cddf9SRui Paulo #include "utils/ip_addr.h"
15f05cddf9SRui Paulo #include "radius.h"
16f05cddf9SRui Paulo #include "radius_das.h"
17f05cddf9SRui Paulo
18f05cddf9SRui Paulo
19f05cddf9SRui Paulo struct radius_das_data {
20f05cddf9SRui Paulo int sock;
21f05cddf9SRui Paulo u8 *shared_secret;
22f05cddf9SRui Paulo size_t shared_secret_len;
23f05cddf9SRui Paulo struct hostapd_ip_addr client_addr;
24f05cddf9SRui Paulo unsigned int time_window;
25f05cddf9SRui Paulo int require_event_timestamp;
26780fb4a2SCy Schubert int require_message_authenticator;
27f05cddf9SRui Paulo void *ctx;
28f05cddf9SRui Paulo enum radius_das_res (*disconnect)(void *ctx,
29f05cddf9SRui Paulo struct radius_das_attrs *attr);
3085732ac8SCy Schubert enum radius_das_res (*coa)(void *ctx, struct radius_das_attrs *attr);
31f05cddf9SRui Paulo };
32f05cddf9SRui Paulo
33f05cddf9SRui Paulo
radius_das_disconnect(struct radius_das_data * das,struct radius_msg * msg,const char * abuf,int from_port)34f05cddf9SRui Paulo static struct radius_msg * radius_das_disconnect(struct radius_das_data *das,
35f05cddf9SRui Paulo struct radius_msg *msg,
36f05cddf9SRui Paulo const char *abuf,
37f05cddf9SRui Paulo int from_port)
38f05cddf9SRui Paulo {
39f05cddf9SRui Paulo struct radius_hdr *hdr;
40f05cddf9SRui Paulo struct radius_msg *reply;
41f05cddf9SRui Paulo u8 allowed[] = {
42f05cddf9SRui Paulo RADIUS_ATTR_USER_NAME,
435b9c547cSRui Paulo RADIUS_ATTR_NAS_IP_ADDRESS,
44f05cddf9SRui Paulo RADIUS_ATTR_CALLING_STATION_ID,
455b9c547cSRui Paulo RADIUS_ATTR_NAS_IDENTIFIER,
46f05cddf9SRui Paulo RADIUS_ATTR_ACCT_SESSION_ID,
475b9c547cSRui Paulo RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
48f05cddf9SRui Paulo RADIUS_ATTR_EVENT_TIMESTAMP,
49f05cddf9SRui Paulo RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
50f05cddf9SRui Paulo RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
515b9c547cSRui Paulo #ifdef CONFIG_IPV6
525b9c547cSRui Paulo RADIUS_ATTR_NAS_IPV6_ADDRESS,
535b9c547cSRui Paulo #endif /* CONFIG_IPV6 */
54f05cddf9SRui Paulo 0
55f05cddf9SRui Paulo };
56f05cddf9SRui Paulo int error = 405;
57f05cddf9SRui Paulo u8 attr;
58f05cddf9SRui Paulo enum radius_das_res res;
59f05cddf9SRui Paulo struct radius_das_attrs attrs;
60f05cddf9SRui Paulo u8 *buf;
61f05cddf9SRui Paulo size_t len;
62f05cddf9SRui Paulo char tmp[100];
63f05cddf9SRui Paulo u8 sta_addr[ETH_ALEN];
64f05cddf9SRui Paulo
65f05cddf9SRui Paulo hdr = radius_msg_get_hdr(msg);
66f05cddf9SRui Paulo
67f05cddf9SRui Paulo attr = radius_msg_find_unlisted_attr(msg, allowed);
68f05cddf9SRui Paulo if (attr) {
69f05cddf9SRui Paulo wpa_printf(MSG_INFO, "DAS: Unsupported attribute %u in "
70f05cddf9SRui Paulo "Disconnect-Request from %s:%d", attr,
71f05cddf9SRui Paulo abuf, from_port);
72f05cddf9SRui Paulo error = 401;
73f05cddf9SRui Paulo goto fail;
74f05cddf9SRui Paulo }
75f05cddf9SRui Paulo
76f05cddf9SRui Paulo os_memset(&attrs, 0, sizeof(attrs));
77f05cddf9SRui Paulo
785b9c547cSRui Paulo if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
795b9c547cSRui Paulo &buf, &len, NULL) == 0) {
805b9c547cSRui Paulo if (len != 4) {
815b9c547cSRui Paulo wpa_printf(MSG_INFO, "DAS: Invalid NAS-IP-Address from %s:%d",
825b9c547cSRui Paulo abuf, from_port);
835b9c547cSRui Paulo error = 407;
845b9c547cSRui Paulo goto fail;
855b9c547cSRui Paulo }
865b9c547cSRui Paulo attrs.nas_ip_addr = buf;
875b9c547cSRui Paulo }
885b9c547cSRui Paulo
895b9c547cSRui Paulo #ifdef CONFIG_IPV6
905b9c547cSRui Paulo if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
915b9c547cSRui Paulo &buf, &len, NULL) == 0) {
925b9c547cSRui Paulo if (len != 16) {
935b9c547cSRui Paulo wpa_printf(MSG_INFO, "DAS: Invalid NAS-IPv6-Address from %s:%d",
945b9c547cSRui Paulo abuf, from_port);
955b9c547cSRui Paulo error = 407;
965b9c547cSRui Paulo goto fail;
975b9c547cSRui Paulo }
985b9c547cSRui Paulo attrs.nas_ipv6_addr = buf;
995b9c547cSRui Paulo }
1005b9c547cSRui Paulo #endif /* CONFIG_IPV6 */
1015b9c547cSRui Paulo
1025b9c547cSRui Paulo if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
1035b9c547cSRui Paulo &buf, &len, NULL) == 0) {
1045b9c547cSRui Paulo attrs.nas_identifier = buf;
1055b9c547cSRui Paulo attrs.nas_identifier_len = len;
1065b9c547cSRui Paulo }
1075b9c547cSRui Paulo
108f05cddf9SRui Paulo if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CALLING_STATION_ID,
109f05cddf9SRui Paulo &buf, &len, NULL) == 0) {
110f05cddf9SRui Paulo if (len >= sizeof(tmp))
111f05cddf9SRui Paulo len = sizeof(tmp) - 1;
112f05cddf9SRui Paulo os_memcpy(tmp, buf, len);
113f05cddf9SRui Paulo tmp[len] = '\0';
114f05cddf9SRui Paulo if (hwaddr_aton2(tmp, sta_addr) < 0) {
115f05cddf9SRui Paulo wpa_printf(MSG_INFO, "DAS: Invalid Calling-Station-Id "
116f05cddf9SRui Paulo "'%s' from %s:%d", tmp, abuf, from_port);
117f05cddf9SRui Paulo error = 407;
118f05cddf9SRui Paulo goto fail;
119f05cddf9SRui Paulo }
120f05cddf9SRui Paulo attrs.sta_addr = sta_addr;
121f05cddf9SRui Paulo }
122f05cddf9SRui Paulo
123f05cddf9SRui Paulo if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME,
124f05cddf9SRui Paulo &buf, &len, NULL) == 0) {
125f05cddf9SRui Paulo attrs.user_name = buf;
126f05cddf9SRui Paulo attrs.user_name_len = len;
127f05cddf9SRui Paulo }
128f05cddf9SRui Paulo
129f05cddf9SRui Paulo if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
130f05cddf9SRui Paulo &buf, &len, NULL) == 0) {
131f05cddf9SRui Paulo attrs.acct_session_id = buf;
132f05cddf9SRui Paulo attrs.acct_session_id_len = len;
133f05cddf9SRui Paulo }
134f05cddf9SRui Paulo
1355b9c547cSRui Paulo if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
1365b9c547cSRui Paulo &buf, &len, NULL) == 0) {
1375b9c547cSRui Paulo attrs.acct_multi_session_id = buf;
1385b9c547cSRui Paulo attrs.acct_multi_session_id_len = len;
1395b9c547cSRui Paulo }
1405b9c547cSRui Paulo
141f05cddf9SRui Paulo if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
142f05cddf9SRui Paulo &buf, &len, NULL) == 0) {
143f05cddf9SRui Paulo attrs.cui = buf;
144f05cddf9SRui Paulo attrs.cui_len = len;
145f05cddf9SRui Paulo }
146f05cddf9SRui Paulo
147f05cddf9SRui Paulo res = das->disconnect(das->ctx, &attrs);
148f05cddf9SRui Paulo switch (res) {
149f05cddf9SRui Paulo case RADIUS_DAS_NAS_MISMATCH:
150f05cddf9SRui Paulo wpa_printf(MSG_INFO, "DAS: NAS mismatch from %s:%d",
151f05cddf9SRui Paulo abuf, from_port);
152f05cddf9SRui Paulo error = 403;
153f05cddf9SRui Paulo break;
154f05cddf9SRui Paulo case RADIUS_DAS_SESSION_NOT_FOUND:
155f05cddf9SRui Paulo wpa_printf(MSG_INFO, "DAS: Session not found for request from "
156f05cddf9SRui Paulo "%s:%d", abuf, from_port);
157f05cddf9SRui Paulo error = 503;
158f05cddf9SRui Paulo break;
1595b9c547cSRui Paulo case RADIUS_DAS_MULTI_SESSION_MATCH:
1605b9c547cSRui Paulo wpa_printf(MSG_INFO,
1615b9c547cSRui Paulo "DAS: Multiple sessions match for request from %s:%d",
1625b9c547cSRui Paulo abuf, from_port);
1635b9c547cSRui Paulo error = 508;
1645b9c547cSRui Paulo break;
16585732ac8SCy Schubert case RADIUS_DAS_COA_FAILED:
16685732ac8SCy Schubert /* not used with Disconnect-Request */
16785732ac8SCy Schubert error = 405;
16885732ac8SCy Schubert break;
169f05cddf9SRui Paulo case RADIUS_DAS_SUCCESS:
170f05cddf9SRui Paulo error = 0;
171f05cddf9SRui Paulo break;
172f05cddf9SRui Paulo }
173f05cddf9SRui Paulo
174f05cddf9SRui Paulo fail:
175f05cddf9SRui Paulo reply = radius_msg_new(error ? RADIUS_CODE_DISCONNECT_NAK :
176f05cddf9SRui Paulo RADIUS_CODE_DISCONNECT_ACK, hdr->identifier);
177f05cddf9SRui Paulo if (reply == NULL)
178f05cddf9SRui Paulo return NULL;
179f05cddf9SRui Paulo
180*a90b9d01SCy Schubert if (!radius_msg_add_msg_auth(reply)) {
181*a90b9d01SCy Schubert radius_msg_free(reply);
182*a90b9d01SCy Schubert return NULL;
183*a90b9d01SCy Schubert }
184*a90b9d01SCy Schubert
185f05cddf9SRui Paulo if (error) {
186f05cddf9SRui Paulo if (!radius_msg_add_attr_int32(reply, RADIUS_ATTR_ERROR_CAUSE,
187f05cddf9SRui Paulo error)) {
188f05cddf9SRui Paulo radius_msg_free(reply);
189f05cddf9SRui Paulo return NULL;
190f05cddf9SRui Paulo }
191f05cddf9SRui Paulo }
192f05cddf9SRui Paulo
193f05cddf9SRui Paulo return reply;
194f05cddf9SRui Paulo }
195f05cddf9SRui Paulo
196f05cddf9SRui Paulo
radius_das_coa(struct radius_das_data * das,struct radius_msg * msg,const char * abuf,int from_port)19785732ac8SCy Schubert static struct radius_msg * radius_das_coa(struct radius_das_data *das,
19885732ac8SCy Schubert struct radius_msg *msg,
19985732ac8SCy Schubert const char *abuf, int from_port)
20085732ac8SCy Schubert {
20185732ac8SCy Schubert struct radius_hdr *hdr;
20285732ac8SCy Schubert struct radius_msg *reply;
20385732ac8SCy Schubert u8 allowed[] = {
20485732ac8SCy Schubert RADIUS_ATTR_USER_NAME,
20585732ac8SCy Schubert RADIUS_ATTR_NAS_IP_ADDRESS,
20685732ac8SCy Schubert RADIUS_ATTR_CALLING_STATION_ID,
20785732ac8SCy Schubert RADIUS_ATTR_NAS_IDENTIFIER,
20885732ac8SCy Schubert RADIUS_ATTR_ACCT_SESSION_ID,
20985732ac8SCy Schubert RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
21085732ac8SCy Schubert RADIUS_ATTR_EVENT_TIMESTAMP,
21185732ac8SCy Schubert RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
21285732ac8SCy Schubert RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
21385732ac8SCy Schubert #ifdef CONFIG_HS20
21485732ac8SCy Schubert RADIUS_ATTR_VENDOR_SPECIFIC,
21585732ac8SCy Schubert #endif /* CONFIG_HS20 */
21685732ac8SCy Schubert #ifdef CONFIG_IPV6
21785732ac8SCy Schubert RADIUS_ATTR_NAS_IPV6_ADDRESS,
21885732ac8SCy Schubert #endif /* CONFIG_IPV6 */
21985732ac8SCy Schubert 0
22085732ac8SCy Schubert };
22185732ac8SCy Schubert int error = 405;
22285732ac8SCy Schubert u8 attr;
22385732ac8SCy Schubert enum radius_das_res res;
22485732ac8SCy Schubert struct radius_das_attrs attrs;
22585732ac8SCy Schubert u8 *buf;
22685732ac8SCy Schubert size_t len;
22785732ac8SCy Schubert char tmp[100];
22885732ac8SCy Schubert u8 sta_addr[ETH_ALEN];
22985732ac8SCy Schubert
23085732ac8SCy Schubert hdr = radius_msg_get_hdr(msg);
23185732ac8SCy Schubert
23285732ac8SCy Schubert if (!das->coa) {
23385732ac8SCy Schubert wpa_printf(MSG_INFO, "DAS: CoA not supported");
23485732ac8SCy Schubert goto fail;
23585732ac8SCy Schubert }
23685732ac8SCy Schubert
23785732ac8SCy Schubert attr = radius_msg_find_unlisted_attr(msg, allowed);
23885732ac8SCy Schubert if (attr) {
23985732ac8SCy Schubert wpa_printf(MSG_INFO,
24085732ac8SCy Schubert "DAS: Unsupported attribute %u in CoA-Request from %s:%d",
24185732ac8SCy Schubert attr, abuf, from_port);
24285732ac8SCy Schubert error = 401;
24385732ac8SCy Schubert goto fail;
24485732ac8SCy Schubert }
24585732ac8SCy Schubert
24685732ac8SCy Schubert os_memset(&attrs, 0, sizeof(attrs));
24785732ac8SCy Schubert
24885732ac8SCy Schubert if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
24985732ac8SCy Schubert &buf, &len, NULL) == 0) {
25085732ac8SCy Schubert if (len != 4) {
25185732ac8SCy Schubert wpa_printf(MSG_INFO, "DAS: Invalid NAS-IP-Address from %s:%d",
25285732ac8SCy Schubert abuf, from_port);
25385732ac8SCy Schubert error = 407;
25485732ac8SCy Schubert goto fail;
25585732ac8SCy Schubert }
25685732ac8SCy Schubert attrs.nas_ip_addr = buf;
25785732ac8SCy Schubert }
25885732ac8SCy Schubert
25985732ac8SCy Schubert #ifdef CONFIG_IPV6
26085732ac8SCy Schubert if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
26185732ac8SCy Schubert &buf, &len, NULL) == 0) {
26285732ac8SCy Schubert if (len != 16) {
26385732ac8SCy Schubert wpa_printf(MSG_INFO, "DAS: Invalid NAS-IPv6-Address from %s:%d",
26485732ac8SCy Schubert abuf, from_port);
26585732ac8SCy Schubert error = 407;
26685732ac8SCy Schubert goto fail;
26785732ac8SCy Schubert }
26885732ac8SCy Schubert attrs.nas_ipv6_addr = buf;
26985732ac8SCy Schubert }
27085732ac8SCy Schubert #endif /* CONFIG_IPV6 */
27185732ac8SCy Schubert
27285732ac8SCy Schubert if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
27385732ac8SCy Schubert &buf, &len, NULL) == 0) {
27485732ac8SCy Schubert attrs.nas_identifier = buf;
27585732ac8SCy Schubert attrs.nas_identifier_len = len;
27685732ac8SCy Schubert }
27785732ac8SCy Schubert
27885732ac8SCy Schubert if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CALLING_STATION_ID,
27985732ac8SCy Schubert &buf, &len, NULL) == 0) {
28085732ac8SCy Schubert if (len >= sizeof(tmp))
28185732ac8SCy Schubert len = sizeof(tmp) - 1;
28285732ac8SCy Schubert os_memcpy(tmp, buf, len);
28385732ac8SCy Schubert tmp[len] = '\0';
28485732ac8SCy Schubert if (hwaddr_aton2(tmp, sta_addr) < 0) {
28585732ac8SCy Schubert wpa_printf(MSG_INFO, "DAS: Invalid Calling-Station-Id "
28685732ac8SCy Schubert "'%s' from %s:%d", tmp, abuf, from_port);
28785732ac8SCy Schubert error = 407;
28885732ac8SCy Schubert goto fail;
28985732ac8SCy Schubert }
29085732ac8SCy Schubert attrs.sta_addr = sta_addr;
29185732ac8SCy Schubert }
29285732ac8SCy Schubert
29385732ac8SCy Schubert if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME,
29485732ac8SCy Schubert &buf, &len, NULL) == 0) {
29585732ac8SCy Schubert attrs.user_name = buf;
29685732ac8SCy Schubert attrs.user_name_len = len;
29785732ac8SCy Schubert }
29885732ac8SCy Schubert
29985732ac8SCy Schubert if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
30085732ac8SCy Schubert &buf, &len, NULL) == 0) {
30185732ac8SCy Schubert attrs.acct_session_id = buf;
30285732ac8SCy Schubert attrs.acct_session_id_len = len;
30385732ac8SCy Schubert }
30485732ac8SCy Schubert
30585732ac8SCy Schubert if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
30685732ac8SCy Schubert &buf, &len, NULL) == 0) {
30785732ac8SCy Schubert attrs.acct_multi_session_id = buf;
30885732ac8SCy Schubert attrs.acct_multi_session_id_len = len;
30985732ac8SCy Schubert }
31085732ac8SCy Schubert
31185732ac8SCy Schubert if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
31285732ac8SCy Schubert &buf, &len, NULL) == 0) {
31385732ac8SCy Schubert attrs.cui = buf;
31485732ac8SCy Schubert attrs.cui_len = len;
31585732ac8SCy Schubert }
31685732ac8SCy Schubert
31785732ac8SCy Schubert #ifdef CONFIG_HS20
31885732ac8SCy Schubert if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
31985732ac8SCy Schubert &buf, &len, NULL) == 0) {
32085732ac8SCy Schubert if (len < 10 || WPA_GET_BE32(buf) != RADIUS_VENDOR_ID_WFA ||
32185732ac8SCy Schubert buf[4] != RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILTERING ||
32285732ac8SCy Schubert buf[5] < 6) {
32385732ac8SCy Schubert wpa_printf(MSG_INFO,
32485732ac8SCy Schubert "DAS: Unsupported attribute %u in CoA-Request from %s:%d",
32585732ac8SCy Schubert attr, abuf, from_port);
32685732ac8SCy Schubert error = 401;
32785732ac8SCy Schubert goto fail;
32885732ac8SCy Schubert }
32985732ac8SCy Schubert attrs.hs20_t_c_filtering = &buf[6];
33085732ac8SCy Schubert }
33185732ac8SCy Schubert
33285732ac8SCy Schubert if (!attrs.hs20_t_c_filtering) {
33385732ac8SCy Schubert wpa_printf(MSG_INFO,
33485732ac8SCy Schubert "DAS: No supported authorization change attribute in CoA-Request from %s:%d",
33585732ac8SCy Schubert abuf, from_port);
33685732ac8SCy Schubert error = 402;
33785732ac8SCy Schubert goto fail;
33885732ac8SCy Schubert }
33985732ac8SCy Schubert #endif /* CONFIG_HS20 */
34085732ac8SCy Schubert
34185732ac8SCy Schubert res = das->coa(das->ctx, &attrs);
34285732ac8SCy Schubert switch (res) {
34385732ac8SCy Schubert case RADIUS_DAS_NAS_MISMATCH:
34485732ac8SCy Schubert wpa_printf(MSG_INFO, "DAS: NAS mismatch from %s:%d",
34585732ac8SCy Schubert abuf, from_port);
34685732ac8SCy Schubert error = 403;
34785732ac8SCy Schubert break;
34885732ac8SCy Schubert case RADIUS_DAS_SESSION_NOT_FOUND:
34985732ac8SCy Schubert wpa_printf(MSG_INFO,
35085732ac8SCy Schubert "DAS: Session not found for request from %s:%d",
35185732ac8SCy Schubert abuf, from_port);
35285732ac8SCy Schubert error = 503;
35385732ac8SCy Schubert break;
35485732ac8SCy Schubert case RADIUS_DAS_MULTI_SESSION_MATCH:
35585732ac8SCy Schubert wpa_printf(MSG_INFO,
35685732ac8SCy Schubert "DAS: Multiple sessions match for request from %s:%d",
35785732ac8SCy Schubert abuf, from_port);
35885732ac8SCy Schubert error = 508;
35985732ac8SCy Schubert break;
36085732ac8SCy Schubert case RADIUS_DAS_COA_FAILED:
36185732ac8SCy Schubert wpa_printf(MSG_INFO, "DAS: CoA failed for request from %s:%d",
36285732ac8SCy Schubert abuf, from_port);
36385732ac8SCy Schubert error = 407;
36485732ac8SCy Schubert break;
36585732ac8SCy Schubert case RADIUS_DAS_SUCCESS:
36685732ac8SCy Schubert error = 0;
36785732ac8SCy Schubert break;
36885732ac8SCy Schubert }
36985732ac8SCy Schubert
37085732ac8SCy Schubert fail:
37185732ac8SCy Schubert reply = radius_msg_new(error ? RADIUS_CODE_COA_NAK :
37285732ac8SCy Schubert RADIUS_CODE_COA_ACK, hdr->identifier);
37385732ac8SCy Schubert if (!reply)
37485732ac8SCy Schubert return NULL;
37585732ac8SCy Schubert
376*a90b9d01SCy Schubert if (!radius_msg_add_msg_auth(reply)) {
377*a90b9d01SCy Schubert radius_msg_free(reply);
378*a90b9d01SCy Schubert return NULL;
379*a90b9d01SCy Schubert }
380*a90b9d01SCy Schubert
38185732ac8SCy Schubert if (error &&
38285732ac8SCy Schubert !radius_msg_add_attr_int32(reply, RADIUS_ATTR_ERROR_CAUSE, error)) {
38385732ac8SCy Schubert radius_msg_free(reply);
38485732ac8SCy Schubert return NULL;
38585732ac8SCy Schubert }
38685732ac8SCy Schubert
38785732ac8SCy Schubert return reply;
38885732ac8SCy Schubert }
38985732ac8SCy Schubert
39085732ac8SCy Schubert
radius_das_receive(int sock,void * eloop_ctx,void * sock_ctx)391f05cddf9SRui Paulo static void radius_das_receive(int sock, void *eloop_ctx, void *sock_ctx)
392f05cddf9SRui Paulo {
393f05cddf9SRui Paulo struct radius_das_data *das = eloop_ctx;
394f05cddf9SRui Paulo u8 buf[1500];
395f05cddf9SRui Paulo union {
396f05cddf9SRui Paulo struct sockaddr_storage ss;
397f05cddf9SRui Paulo struct sockaddr_in sin;
398f05cddf9SRui Paulo #ifdef CONFIG_IPV6
399f05cddf9SRui Paulo struct sockaddr_in6 sin6;
400f05cddf9SRui Paulo #endif /* CONFIG_IPV6 */
401f05cddf9SRui Paulo } from;
402f05cddf9SRui Paulo char abuf[50];
403f05cddf9SRui Paulo int from_port = 0;
404f05cddf9SRui Paulo socklen_t fromlen;
405f05cddf9SRui Paulo int len;
406f05cddf9SRui Paulo struct radius_msg *msg, *reply = NULL;
407f05cddf9SRui Paulo struct radius_hdr *hdr;
408f05cddf9SRui Paulo struct wpabuf *rbuf;
409f05cddf9SRui Paulo u32 val;
410f05cddf9SRui Paulo int res;
411f05cddf9SRui Paulo struct os_time now;
412f05cddf9SRui Paulo
413f05cddf9SRui Paulo fromlen = sizeof(from);
414f05cddf9SRui Paulo len = recvfrom(sock, buf, sizeof(buf), 0,
415f05cddf9SRui Paulo (struct sockaddr *) &from.ss, &fromlen);
416f05cddf9SRui Paulo if (len < 0) {
417f05cddf9SRui Paulo wpa_printf(MSG_ERROR, "DAS: recvfrom: %s", strerror(errno));
418f05cddf9SRui Paulo return;
419f05cddf9SRui Paulo }
420f05cddf9SRui Paulo
421f05cddf9SRui Paulo os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
422f05cddf9SRui Paulo from_port = ntohs(from.sin.sin_port);
423f05cddf9SRui Paulo
424f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "DAS: Received %d bytes from %s:%d",
425f05cddf9SRui Paulo len, abuf, from_port);
42685732ac8SCy Schubert if (das->client_addr.u.v4.s_addr &&
42785732ac8SCy Schubert das->client_addr.u.v4.s_addr != from.sin.sin_addr.s_addr) {
428f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "DAS: Drop message from unknown client");
429f05cddf9SRui Paulo return;
430f05cddf9SRui Paulo }
431f05cddf9SRui Paulo
432f05cddf9SRui Paulo msg = radius_msg_parse(buf, len);
433f05cddf9SRui Paulo if (msg == NULL) {
434f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "DAS: Parsing incoming RADIUS packet "
435f05cddf9SRui Paulo "from %s:%d failed", abuf, from_port);
436f05cddf9SRui Paulo return;
437f05cddf9SRui Paulo }
438f05cddf9SRui Paulo
439f05cddf9SRui Paulo if (wpa_debug_level <= MSG_MSGDUMP)
440f05cddf9SRui Paulo radius_msg_dump(msg);
441f05cddf9SRui Paulo
442f05cddf9SRui Paulo if (radius_msg_verify_das_req(msg, das->shared_secret,
443780fb4a2SCy Schubert das->shared_secret_len,
444780fb4a2SCy Schubert das->require_message_authenticator)) {
445780fb4a2SCy Schubert wpa_printf(MSG_DEBUG,
446780fb4a2SCy Schubert "DAS: Invalid authenticator or Message-Authenticator in packet from %s:%d - drop",
447780fb4a2SCy Schubert abuf, from_port);
448f05cddf9SRui Paulo goto fail;
449f05cddf9SRui Paulo }
450f05cddf9SRui Paulo
451f05cddf9SRui Paulo os_get_time(&now);
452f05cddf9SRui Paulo res = radius_msg_get_attr(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
453f05cddf9SRui Paulo (u8 *) &val, 4);
454f05cddf9SRui Paulo if (res == 4) {
455f05cddf9SRui Paulo u32 timestamp = ntohl(val);
456325151a3SRui Paulo if ((unsigned int) abs((int) (now.sec - timestamp)) >
4575b9c547cSRui Paulo das->time_window) {
458f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "DAS: Unacceptable "
459f05cddf9SRui Paulo "Event-Timestamp (%u; local time %u) in "
460f05cddf9SRui Paulo "packet from %s:%d - drop",
461f05cddf9SRui Paulo timestamp, (unsigned int) now.sec,
462f05cddf9SRui Paulo abuf, from_port);
463f05cddf9SRui Paulo goto fail;
464f05cddf9SRui Paulo }
465f05cddf9SRui Paulo } else if (das->require_event_timestamp) {
466f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "DAS: Missing Event-Timestamp in packet "
467f05cddf9SRui Paulo "from %s:%d - drop", abuf, from_port);
468f05cddf9SRui Paulo goto fail;
469f05cddf9SRui Paulo }
470f05cddf9SRui Paulo
471f05cddf9SRui Paulo hdr = radius_msg_get_hdr(msg);
472f05cddf9SRui Paulo
473f05cddf9SRui Paulo switch (hdr->code) {
474f05cddf9SRui Paulo case RADIUS_CODE_DISCONNECT_REQUEST:
475f05cddf9SRui Paulo reply = radius_das_disconnect(das, msg, abuf, from_port);
476f05cddf9SRui Paulo break;
477f05cddf9SRui Paulo case RADIUS_CODE_COA_REQUEST:
47885732ac8SCy Schubert reply = radius_das_coa(das, msg, abuf, from_port);
479f05cddf9SRui Paulo break;
480f05cddf9SRui Paulo default:
481f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "DAS: Unexpected RADIUS code %u in "
482f05cddf9SRui Paulo "packet from %s:%d",
483f05cddf9SRui Paulo hdr->code, abuf, from_port);
484f05cddf9SRui Paulo }
485f05cddf9SRui Paulo
486f05cddf9SRui Paulo if (reply) {
487f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "DAS: Reply to %s:%d", abuf, from_port);
488f05cddf9SRui Paulo
489f05cddf9SRui Paulo if (!radius_msg_add_attr_int32(reply,
490f05cddf9SRui Paulo RADIUS_ATTR_EVENT_TIMESTAMP,
491f05cddf9SRui Paulo now.sec)) {
492f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "DAS: Failed to add "
493f05cddf9SRui Paulo "Event-Timestamp attribute");
494f05cddf9SRui Paulo }
495f05cddf9SRui Paulo
496f05cddf9SRui Paulo if (radius_msg_finish_das_resp(reply, das->shared_secret,
497f05cddf9SRui Paulo das->shared_secret_len, hdr) <
498f05cddf9SRui Paulo 0) {
499f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "DAS: Failed to add "
500f05cddf9SRui Paulo "Message-Authenticator attribute");
501f05cddf9SRui Paulo }
502f05cddf9SRui Paulo
503f05cddf9SRui Paulo if (wpa_debug_level <= MSG_MSGDUMP)
504f05cddf9SRui Paulo radius_msg_dump(reply);
505f05cddf9SRui Paulo
506f05cddf9SRui Paulo rbuf = radius_msg_get_buf(reply);
507f05cddf9SRui Paulo res = sendto(das->sock, wpabuf_head(rbuf),
508f05cddf9SRui Paulo wpabuf_len(rbuf), 0,
509f05cddf9SRui Paulo (struct sockaddr *) &from.ss, fromlen);
510f05cddf9SRui Paulo if (res < 0) {
511f05cddf9SRui Paulo wpa_printf(MSG_ERROR, "DAS: sendto(to %s:%d): %s",
512f05cddf9SRui Paulo abuf, from_port, strerror(errno));
513f05cddf9SRui Paulo }
514f05cddf9SRui Paulo }
515f05cddf9SRui Paulo
516f05cddf9SRui Paulo fail:
517f05cddf9SRui Paulo radius_msg_free(msg);
518f05cddf9SRui Paulo radius_msg_free(reply);
519f05cddf9SRui Paulo }
520f05cddf9SRui Paulo
521f05cddf9SRui Paulo
radius_das_open_socket(int port)522f05cddf9SRui Paulo static int radius_das_open_socket(int port)
523f05cddf9SRui Paulo {
524f05cddf9SRui Paulo int s;
525f05cddf9SRui Paulo struct sockaddr_in addr;
526f05cddf9SRui Paulo
527f05cddf9SRui Paulo s = socket(PF_INET, SOCK_DGRAM, 0);
528f05cddf9SRui Paulo if (s < 0) {
5295b9c547cSRui Paulo wpa_printf(MSG_INFO, "RADIUS DAS: socket: %s", strerror(errno));
530f05cddf9SRui Paulo return -1;
531f05cddf9SRui Paulo }
532f05cddf9SRui Paulo
533f05cddf9SRui Paulo os_memset(&addr, 0, sizeof(addr));
534f05cddf9SRui Paulo addr.sin_family = AF_INET;
535f05cddf9SRui Paulo addr.sin_port = htons(port);
536f05cddf9SRui Paulo if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5375b9c547cSRui Paulo wpa_printf(MSG_INFO, "RADIUS DAS: bind: %s", strerror(errno));
538f05cddf9SRui Paulo close(s);
539f05cddf9SRui Paulo return -1;
540f05cddf9SRui Paulo }
541f05cddf9SRui Paulo
542f05cddf9SRui Paulo return s;
543f05cddf9SRui Paulo }
544f05cddf9SRui Paulo
545f05cddf9SRui Paulo
546f05cddf9SRui Paulo struct radius_das_data *
radius_das_init(struct radius_das_conf * conf)547f05cddf9SRui Paulo radius_das_init(struct radius_das_conf *conf)
548f05cddf9SRui Paulo {
549f05cddf9SRui Paulo struct radius_das_data *das;
550f05cddf9SRui Paulo
551f05cddf9SRui Paulo if (conf->port == 0 || conf->shared_secret == NULL ||
552f05cddf9SRui Paulo conf->client_addr == NULL)
553f05cddf9SRui Paulo return NULL;
554f05cddf9SRui Paulo
555f05cddf9SRui Paulo das = os_zalloc(sizeof(*das));
556f05cddf9SRui Paulo if (das == NULL)
557f05cddf9SRui Paulo return NULL;
558f05cddf9SRui Paulo
559f05cddf9SRui Paulo das->time_window = conf->time_window;
560f05cddf9SRui Paulo das->require_event_timestamp = conf->require_event_timestamp;
561780fb4a2SCy Schubert das->require_message_authenticator =
562780fb4a2SCy Schubert conf->require_message_authenticator;
563f05cddf9SRui Paulo das->ctx = conf->ctx;
564f05cddf9SRui Paulo das->disconnect = conf->disconnect;
56585732ac8SCy Schubert das->coa = conf->coa;
566f05cddf9SRui Paulo
567f05cddf9SRui Paulo os_memcpy(&das->client_addr, conf->client_addr,
568f05cddf9SRui Paulo sizeof(das->client_addr));
569f05cddf9SRui Paulo
57085732ac8SCy Schubert das->shared_secret = os_memdup(conf->shared_secret,
57185732ac8SCy Schubert conf->shared_secret_len);
572f05cddf9SRui Paulo if (das->shared_secret == NULL) {
573f05cddf9SRui Paulo radius_das_deinit(das);
574f05cddf9SRui Paulo return NULL;
575f05cddf9SRui Paulo }
576f05cddf9SRui Paulo das->shared_secret_len = conf->shared_secret_len;
577f05cddf9SRui Paulo
578f05cddf9SRui Paulo das->sock = radius_das_open_socket(conf->port);
579f05cddf9SRui Paulo if (das->sock < 0) {
580f05cddf9SRui Paulo wpa_printf(MSG_ERROR, "Failed to open UDP socket for RADIUS "
581f05cddf9SRui Paulo "DAS");
582f05cddf9SRui Paulo radius_das_deinit(das);
583f05cddf9SRui Paulo return NULL;
584f05cddf9SRui Paulo }
585f05cddf9SRui Paulo
586f05cddf9SRui Paulo if (eloop_register_read_sock(das->sock, radius_das_receive, das, NULL))
587f05cddf9SRui Paulo {
588f05cddf9SRui Paulo radius_das_deinit(das);
589f05cddf9SRui Paulo return NULL;
590f05cddf9SRui Paulo }
591f05cddf9SRui Paulo
592f05cddf9SRui Paulo return das;
593f05cddf9SRui Paulo }
594f05cddf9SRui Paulo
595f05cddf9SRui Paulo
radius_das_deinit(struct radius_das_data * das)596f05cddf9SRui Paulo void radius_das_deinit(struct radius_das_data *das)
597f05cddf9SRui Paulo {
598f05cddf9SRui Paulo if (das == NULL)
599f05cddf9SRui Paulo return;
600f05cddf9SRui Paulo
601f05cddf9SRui Paulo if (das->sock >= 0) {
602f05cddf9SRui Paulo eloop_unregister_read_sock(das->sock);
603f05cddf9SRui Paulo close(das->sock);
604f05cddf9SRui Paulo }
605f05cddf9SRui Paulo
606f05cddf9SRui Paulo os_free(das->shared_secret);
607f05cddf9SRui Paulo os_free(das);
608f05cddf9SRui Paulo }
609