33#include <radcli/radcli.h>
41static int restart_session(rc_session *s);
44static rc_session *sess_of(rc_handle *rh)
59static int sess_get_fd(
void *ptr,
struct sockaddr *our_sockaddr)
63 if (s->conn.need_restart != 0) {
64 if (restart_session(s) < 0)
67 return s->conn.sockfd;
76static int sess_get_active_fd(
void *ptr)
80 return s->conn.sockfd;
92static int sess_wait_or_give_up(rc_session *s,
short events,
const char *what)
94 int64_t start_ms = rc_getmtime_ms();
95 int timeout = rc_conf_int_id(s->rh, OPT_RADIUS_TIMEOUT);
100 for (; timeout > 0; timeout -= (int)((rc_getmtime_ms() - start_ms) / 1000)) {
101 struct pollfd pfd = { s->conn.sockfd, events, 0 };
102 int ret = poll(&pfd, 1, timeout * 1000);
108 if (errno != EINTR) {
109 rc_log(LOG_ERR,
"%s: poll: %s", __func__, strerror(errno));
115 rc_log(LOG_ERR,
"%s: timeout waiting to %s data", __func__, what);
118 s->conn.need_restart = 1;
132static int sess_recv_packet(rc_session *s,
void *buf,
size_t cap)
134 rc_sess_conn *c = &s->conn;
135 size_t want = AUTH_HDR_LENGTH_END;
138 if (s->ops->datagram)
139 return s->ops->recv(c, buf, cap);
142 if (c->rlen >= AUTH_HDR_LENGTH_END) {
143 want = ntohs(((AUTH_HDR *)c->rbuf)->length);
145 if (want < AUTH_HDR_LEN || want > RC_MAX_PACKET_LEN || want > cap) {
146 rc_log(LOG_ERR,
"%s: invalid RADIUS packet length %u on the connection",
147 __func__, (
unsigned)want);
150 if (c->rlen == want) {
151 memcpy(buf, c->rbuf, want);
157 ret = s->ops->recv(c, c->rbuf + c->rlen, want - c->rlen);
168static void sendq_clear(rc_session *s)
181static void sendq_remove(rc_session *s,
unsigned i)
183 if (s->sendq[i].copy >= 0)
184 s->copies_used &= ~(1u << s->sendq[i].copy);
188 memmove(&s->sendq[i], &s->sendq[i + 1], (s->sendq_len - i) *
sizeof(s->sendq[0]));
192static int sendq_has_copy(rc_session *s)
194 return s->copies_used != (1u << SESS_SENDQ_SIZE) - 1;
206static void sendq_append(rc_session *s,
const void *buf,
size_t len,
int kind,
int slot)
208 struct sess_pending_send *p = &s->sendq[s->sendq_len++];
218 while (s->copies_used & (1u << ++p->copy))
220 s->copies_used |= 1u << p->copy;
221 memcpy(s->copies[p->copy], buf, len);
222 p->data = s->copies[p->copy];
232static int sendq_try_head(rc_session *s)
234 int ret = s->ops->send(&s->conn, s->sendq[0].data, s->sendq[0].len);
236 if (ret == SESS_AGAIN) {
241 s->conn.need_restart = 1;
246 s->conn.last_msg = time(0);
264static ssize_t sess_sendto(
void *ptr,
int sockfd,
265 const void *buf,
size_t len,
266 int flags,
const struct sockaddr *dest_addr,
272 if (s->conn.need_restart != 0) {
273 if (restart_session(s) < 0) {
281 while (s->sendq_len > 0) {
282 int r = sendq_try_head(s);
288 if (r == 0 && sess_wait_or_give_up(s, POLLOUT,
"send") < 0)
293 ret = s->ops->send(&s->conn, buf, len);
294 if (ret == SESS_AGAIN) {
295 if (sess_wait_or_give_up(s, POLLOUT,
"send") < 0)
302 s->conn.need_restart = 1;
309 s->conn.last_msg = time(0);
318static int sess_lock(
void *ptr)
322 return pthread_mutex_lock(&s->lock);
330static int sess_unlock(
void *ptr)
334 return pthread_mutex_unlock(&s->lock);
354static ssize_t sess_recvfrom(
void *ptr,
int sockfd,
355 void *buf,
size_t len,
356 int flags,
struct sockaddr *src_addr,
362 ret = sess_recv_packet(s, buf, len);
363 if (ret == SESS_AGAIN) {
368 if (ret == SESS_ALERT) {
375 rc_log(LOG_ERR,
"%s: connection closed by the server", __func__);
377 s->conn.need_restart = 1;
381 s->conn.last_msg = time(0);
382 s->conn.last_recv = s->conn.last_msg;
391static void conn_close(rc_session *s, rc_sess_conn *c)
407static int attempt_open(rc_session *s)
409 rc_sess_conn *c = &s->next;
410 struct sockaddr_storage our_sockaddr;
411 struct addrinfo *info;
412 int sockfd, ret, e, sock_flags, timeout;
416 memset(c, 0,
sizeof(*c));
421 memcpy(&our_sockaddr, &s->our_sockaddr,
sizeof(our_sockaddr));
422 if (our_sockaddr.ss_family == AF_INET)
423 ((
struct sockaddr_in *)&our_sockaddr)->sin_port = 0;
425 ((
struct sockaddr_in6 *)&our_sockaddr)->sin6_port = 0;
427 sockfd = socket(our_sockaddr.ss_family, s->ops->datagram ? SOCK_DGRAM : SOCK_STREAM, 0);
429 rc_log(LOG_ERR,
"%s: cannot open socket", __func__);
435 if (bind(sockfd, SA(&our_sockaddr), SS_LEN(&our_sockaddr)) < 0) {
436 rc_log(LOG_ERR,
"%s: bind: %s", __func__, strerror(errno));
439 if (our_sockaddr.ss_family == AF_INET6 &&
440 radcli2_priv_prefer_public_addr(s->rh, sockfd) < 0)
446 sock_flags = fcntl(sockfd, F_GETFL, 0);
447 if (sock_flags == -1 ||
448 fcntl(sockfd, F_SETFL, sock_flags | O_NONBLOCK) == -1) {
450 rc_log(LOG_CRIT,
"%s: cannot set socket non-blocking: %s",
451 __func__, strerror(e));
455 info = rc_getaddrinfo(s->hostname, PW_AI_AUTH);
457 rc_log(LOG_ERR,
"%s: cannot resolve %s", __func__, s->hostname);
462 rc_log(LOG_ERR,
"%s: no port specified for server %s", __func__, s->hostname);
466 if (info->ai_addr->sa_family == AF_INET)
467 ((
struct sockaddr_in *)info->ai_addr)->sin_port = htons(s->port);
469 ((
struct sockaddr_in6 *)info->ai_addr)->sin6_port = htons(s->port);
472 ret = connect(sockfd, info->ai_addr, info->ai_addrlen);
475 if (ret == -1 && e != EINPROGRESS) {
476 rc_log(LOG_CRIT,
"%s: cannot connect to %s: %s",
477 __func__, s->hostname, strerror(e));
481 timeout = rc_conf_int_id(s->rh, OPT_RADIUS_TIMEOUT);
482 s->deadline_ms = rc_getmtime_ms() + rc_timeout_sec_to_ms((timeout > 0) ? timeout : 1);
486 s->phase = SESS_CONNECTING;
501static int attempt_begin(rc_session *s)
507 if (s->phase != SESS_IDLE)
512 ns = rc_conf_str_id(s->rh, OPT_NAMESPACE);
513 if (ns != NULL && rc_set_netns(ns, &ns_def_hdl) == -1) {
514 rc_log(LOG_ERR,
"%s: namespace %s set failed", __func__, ns);
517 ret = attempt_open(s);
518 if (ns != NULL && rc_reset_netns(&ns_def_hdl) == -1)
519 rc_log(LOG_ERR,
"%s: namespace %s reset failed", __func__, ns);
528static int attempt_connected(rc_session *s)
530 struct pollfd pfd = { s->next.sockfd, POLLOUT, 0 };
531 socklen_t len =
sizeof(int);
534 if (poll(&pfd, 1, 0) <= 0)
536 if (getsockopt(s->next.sockfd, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
539 rc_log(LOG_CRIT,
"%s: cannot connect to %s: %s",
540 __func__, s->hostname, strerror(err));
553static int attempt_step(rc_session *s)
555 rc_sess_conn *c = &s->next;
558 if (s->phase == SESS_IDLE)
560 if (rc_getmtime_ms() >= s->deadline_ms) {
561 rc_log(LOG_ERR,
"%s: timeout connecting to %s", __func__, s->hostname);
565 if (s->phase == SESS_CONNECTING) {
566 ret = attempt_connected(s);
572 s->ops->start(s, c, rc_conf_int_id(s->rh, OPT_RADIUS_TIMEOUT)) < 0)
574 s->phase = SESS_HANDSHAKING;
577 if (s->ops->handshake) {
578 ret = s->ops->handshake(c);
579 if (ret == SESS_AGAIN)
590 c->last_msg = time(0);
591 c->last_recv = c->last_msg;
593 if (c->sockfd == s->conn.sockfd)
595 conn_close(s, &s->conn);
596 memcpy(&s->conn, c,
sizeof(*c));
598 s->conn.need_restart = 0;
599 s->phase = SESS_IDLE;
602 rc_log(LOG_ERR,
"%s: error in re-initializing the connection", __func__);
604 s->phase = SESS_IDLE;
615static int attempt_wait(rc_session *s,
short *events)
617 int ms = rc_ms_until(s->deadline_ms);
621 if (s->phase == SESS_HANDSHAKING && s->ops->handshake_wait) {
622 hs_ms = s->ops->handshake_wait(&s->next, events);
623 if (hs_ms >= 0 && hs_ms < ms)
639static int restart_session(rc_session *s)
643 if (attempt_begin(s) < 0)
646 while ((ret = attempt_step(s)) == 0) {
647 struct pollfd pfd = { s->next.sockfd, 0, 0 };
648 int ms = attempt_wait(s, &pfd.events);
653 return ret > 0 ? 0 : -1;
662int radcli2_priv_sess_fd(rc_handle *rh)
664 rc_session *s = sess_of(rh);
666 if (s != NULL && s->conn.init != 0 && s->conn.need_restart == 0)
667 return s->conn.sockfd;
680int radcli2_priv_sess_poll(rc_handle *rh,
short *events,
int *timeout_ms)
682 rc_session *s = sess_of(rh);
688 if (s->phase != SESS_IDLE) {
689 *timeout_ms = attempt_wait(s, events);
690 return s->next.sockfd;
692 if (s->conn.init == 0 || s->conn.need_restart != 0)
698 if (s->sendq_len > 0)
701 if (s->ops->pending && s->ops->pending(&s->conn))
703 return s->conn.sockfd;
711void radcli2_priv_sess_progress(rc_handle *rh)
713 rc_session *s = sess_of(rh);
715 if (s != NULL && s->phase != SESS_IDLE)
727unsigned radcli2_priv_sess_gen(rc_handle *rh)
729 rc_session *s = sess_of(rh);
731 if (s == NULL || s->ops->datagram)
744int radcli2_priv_sess_holds(rc_handle *rh,
unsigned gen)
746 rc_session *s = sess_of(rh);
748 return s != NULL && gen != 0 && s->phase == SESS_IDLE && s->conn.init != 0 &&
749 s->conn.need_restart == 0 && s->conn.gen == gen;
760int radcli2_priv_tls_fd(rc_handle *rh)
767 if (s != NULL && s->conn.init != 0)
768 return s->conn.sockfd;
780time_t radcli2_priv_sess_last_msg(rc_handle *rh)
782 rc_session *s = sess_of(rh);
784 if (s != NULL && s->conn.init != 0)
785 return s->conn.last_msg;
799time_t radcli2_priv_sess_last_recv(rc_handle *rh)
801 rc_session *s = sess_of(rh);
803 if (s != NULL && s->conn.init != 0)
804 return s->conn.last_recv;
818int radcli2_priv_sess_force_reconnect(rc_handle *rh)
820 rc_session *s = sess_of(rh);
824 s->conn.need_restart = 1;
825 return attempt_begin(s);
841int radcli2_priv_check_tls(rc_handle *rh)
851 if (s->conn.init == 0)
854 if (s->conn.need_restart != 0)
855 return restart_session(s) < 0 ? -1 : 0;
857 interval = rc_conf_int_id(rh, OPT_WATCHDOG_INTERVAL);
858 if (interval > 0 && time(0) - s->conn.last_msg >= interval)
859 radcli2_priv_dae_send_watchdog((radcli_ctx *)rh);
873int radcli2_priv_sess_ensure_connected(rc_handle *rh)
875 rc_session *s = sess_of(rh);
880 if (s->conn.init != 0 && s->conn.need_restart == 0)
883 return restart_session(s);
891void radcli2_priv_sess_flush(rc_handle *rh)
893 rc_session *s = sess_of(rh);
895 if (s == NULL || s->conn.init == 0 || s->conn.need_restart != 0)
897 while (s->sendq_len > 0 && sendq_try_head(s) == 1)
921int radcli2_priv_sess_send_queued(rc_handle *rh,
const void *buf,
size_t len,
int kind,
int slot)
923 rc_session *s = sess_of(rh);
927 if (s == NULL || len > RC_MAX_PACKET_LEN)
930 if (s->conn.init == 0 || s->conn.need_restart != 0) {
931 if (kind == RADSEC_SEND_DAE_REPLY || attempt_begin(s) < 0)
934 radcli2_priv_sess_flush(rh);
935 if (s->conn.need_restart != 0)
939 if (s->phase == SESS_IDLE && s->sendq_len == 0) {
940 ret = s->ops->send(&s->conn, buf, len);
941 if (ret == SESS_AGAIN) {
942 sendq_append(s, buf, len, kind, slot);
947 s->conn.need_restart = 1;
950 s->conn.last_msg = time(0);
954 if (slot >= 0 || sendq_has_copy(s)) {
955 sendq_append(s, buf, len, kind, slot);
959 if (kind == RADSEC_SEND_DAE_REPLY) {
960 for (i = s->head_started; i < s->sendq_len; i++) {
961 if (s->sendq[i].kind == RADSEC_SEND_DAE_REPLY)
964 if (i < s->sendq_len) {
965 rc_log(LOG_WARNING,
"%s: send queue full (%u), dropping the "
966 "oldest unsent dynamic-authorization reply", __func__,
967 (
unsigned)SESS_SENDQ_SIZE);
969 sendq_append(s, buf, len, kind, slot);
974 rc_log(LOG_WARNING,
"%s: send queue full (%u), skipping this %s", __func__,
975 (
unsigned)SESS_SENDQ_SIZE,
976 kind == RADSEC_SEND_WATCHDOG ?
"watchdog" :
"dynamic-authorization reply");
987int radcli2_priv_sess_queued(rc_handle *rh,
int slot)
989 rc_session *s = sess_of(rh);
994 for (i = 0; i < s->sendq_len; i++) {
995 if (s->sendq[i].slot == slot)
1009void radcli2_priv_sess_unqueue(rc_handle *rh,
int slot)
1011 rc_session *s = sess_of(rh);
1016 for (i = 0; i < s->sendq_len; i++) {
1017 if (s->sendq[i].slot != slot)
1019 if (i == 0 && s->head_started) {
1020 memcpy(s->head_buf, s->sendq[0].data, s->sendq[0].len);
1021 s->sendq[0].data = s->head_buf;
1022 s->sendq[0].slot = -1;
1042int radcli2_priv_sess_try_recv(rc_handle *rh, uint8_t *buf,
size_t cap)
1044 rc_session *s = sess_of(rh);
1049 if (s->conn.init == 0 || s->conn.need_restart != 0)
1052 ret = sess_recv_packet(s, buf, cap);
1055 if (ret == SESS_AGAIN || ret == SESS_ALERT)
1060 rc_log(LOG_ERR,
"%s: connection closed by the server", __func__);
1061 s->conn.need_restart = 1;
1065 s->conn.last_msg = time(0);
1066 s->conn.last_recv = s->conn.last_msg;
1076int radcli2_priv_sess_pending(rc_handle *rh)
1078 rc_session *s = sess_of(rh);
1080 if (s == NULL || s->conn.init == 0 || s->conn.need_restart != 0 ||
1081 s->ops->pending == NULL)
1083 return s->ops->pending(&s->conn);
1090void radcli2_priv_sess_deinit(rc_handle *rh)
1092 rc_session *s = sess_of(rh);
1099 ns = rc_conf_str_id(rh, OPT_NAMESPACE);
1100 if (ns != NULL && rc_set_netns(ns, &ns_def_hdl) == -1) {
1101 rc_log(LOG_ERR,
"%s: namespace %s set failed", __func__, ns);
1104 if (s->phase != SESS_IDLE)
1105 conn_close(s, &s->next);
1106 conn_close(s, &s->conn);
1107 if (ns != NULL && rc_reset_netns(&ns_def_hdl) == -1)
1108 rc_log(LOG_ERR,
"%s: namespace %s reset failed", __func__, ns);
1112 pthread_mutex_destroy(&s->lock);
1126int radcli2_priv_sess_init(rc_handle *rh,
const rc_sess_ops *ops,
void *backend)
1128 SERVER *authservers;
1131 authservers = rc_conf_srv_id(rh, OPT_AUTHSERVER);
1132 if (authservers == NULL || authservers->max == 0) {
1133 rc_log(LOG_ERR,
"%s: cannot find authserver", __func__);
1136 if (authservers->max > 1) {
1137 rc_log(LOG_ERR,
"%s: too many auth servers for TCP/TLS/DTLS; only one is allowed",
1142 s = calloc(1,
sizeof(*s));
1147 s->backend = backend;
1149 strlcpy(s->hostname, authservers->name[0],
sizeof(s->hostname));
1150 s->port = authservers->port[0];
1151 rc_own_bind_addr(rh, &s->our_sockaddr);
1152 pthread_mutex_init(&s->lock, NULL);
1155 s->conn.sockfd = -1;
1157 s->conn.need_restart = 1;
1160 rh->so.get_fd = sess_get_fd;
1161 rh->so.get_active_fd = sess_get_active_fd;
1162 rh->so.sendto = sess_sendto;
1163 rh->so.recvfrom = sess_recvfrom;
1164 rh->so.lock = sess_lock;
1165 rh->so.unlock = sess_unlock;
1172static int tcp_send(rc_sess_conn *c,
const void *buf,
size_t len)
1174 while (c->wpos < len) {
1175 ssize_t n = send(c->sockfd, (
const uint8_t *)buf + c->wpos, len - c->wpos, 0);
1178 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
1180 rc_log(LOG_ERR,
"%s: send: %s", __func__, strerror(errno));
1183 c->wpos += (size_t)n;
1190static int tcp_recv(rc_sess_conn *c,
void *buf,
size_t len)
1192 ssize_t n = recv(c->sockfd, buf, len, 0);
1195 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
1197 rc_log(LOG_ERR,
"%s: recv: %s", __func__, strerror(errno));
1203static const rc_sess_ops tcp_ops = {
1214int radcli2_priv_sess_init_tcp(rc_handle *rh)
1216 memset(&rh->so, 0,
sizeof(rh->so));
1218 if (radcli2_priv_sess_init(rh, &tcp_ops, NULL) < 0)
1220 ((rc_session *)rh->so.ptr)->bind_local = 1;
@ RC_SOCKET_TCP
Plain TCP socket.
@ RC_SOCKET_DTLS
DTLS socket.
@ RC_SOCKET_TLS
TLS socket.