req.c 7.4 KB
Newer Older
K
Kozlov Dmitry 已提交
1
#include <stdlib.h>
2
#include <stdio.h>
D
Dmitry Kozlov 已提交
3 4 5 6
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
K
Kozlov Dmitry 已提交
7
#include <sched.h>
D
Dmitry Kozlov 已提交
8 9 10
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
K
Kozlov Dmitry 已提交
11

D
Dmitry Kozlov 已提交
12
#include "log.h"
13
#include "radius_p.h"
K
Kozlov Dmitry 已提交
14

D
Dmitry Kozlov 已提交
15 16
#include "memdebug.h"

17
static int urandom_fd;
K
Kozlov Dmitry 已提交
18 19

static int rad_req_read(struct triton_md_handler_t *h);
D
Dmitry Kozlov 已提交
20
static void rad_req_timeout(struct triton_timer_t *t);
K
Kozlov Dmitry 已提交
21

22
struct rad_req_t *rad_req_alloc(struct radius_pd_t *rpd, int code, const char *username)
K
Kozlov Dmitry 已提交
23
{
K
Kozlov Dmitry 已提交
24
	struct rad_plugin_t *plugin;
D
Dmitry Kozlov 已提交
25
	struct rad_req_t *req = _malloc(sizeof(*req));
K
Kozlov Dmitry 已提交
26 27 28 29 30 31 32

	if (!req)
		return NULL;

	memset(req, 0, sizeof(*req));
	req->rpd = rpd;
	req->hnd.fd = -1;
33
	req->ctx.before_switch = log_switch;
34

35
	req->serv = rad_server_get(code == CODE_ACCESS_REQUEST ? RAD_SERV_AUTH : RAD_SERV_ACCT);
K
temp  
Kozlov Dmitry 已提交
36 37 38 39 40
	if (!req->serv)
		goto out_err;
	
	req->server_addr = req->serv->auth_addr;
	req->server_port = req->serv->auth_port;
K
Kozlov Dmitry 已提交
41

42 43 44 45
	while (1) {
		if (read(urandom_fd, req->RA, 16) != 16) {
			if (errno == EINTR)
				continue;
46
			log_ppp_error("radius:req:read urandom: %s\n", strerror(errno));
47 48 49 50 51 52 53 54 55
			goto out_err;
		}
		break;
	}

	req->pack = rad_packet_alloc(code);
	if (!req->pack)
		goto out_err;

K
Kozlov Dmitry 已提交
56
	if (rad_packet_add_str(req->pack, NULL, "User-Name", username))
K
Kozlov Dmitry 已提交
57 58
		goto out_err;
	if (conf_nas_identifier)
K
Kozlov Dmitry 已提交
59
		if (rad_packet_add_str(req->pack, NULL, "NAS-Identifier", conf_nas_identifier))
K
Kozlov Dmitry 已提交
60
			goto out_err;
61
	if (conf_nas_ip_address)
K
Kozlov Dmitry 已提交
62
		if (rad_packet_add_ipaddr(req->pack, NULL, "NAS-IP-Address", conf_nas_ip_address))
63
			goto out_err;
K
Kozlov Dmitry 已提交
64
	if (rad_packet_add_int(req->pack, NULL, "NAS-Port", rpd->ppp->unit_idx))
K
Kozlov Dmitry 已提交
65
		goto out_err;
K
Kozlov Dmitry 已提交
66
	if (rad_packet_add_val(req->pack, NULL, "NAS-Port-Type", "Virtual"))
K
Kozlov Dmitry 已提交
67
		goto out_err;
K
Kozlov Dmitry 已提交
68
	if (rad_packet_add_val(req->pack, NULL, "Service-Type", "Framed-User"))
K
Kozlov Dmitry 已提交
69
		goto out_err;
K
Kozlov Dmitry 已提交
70
	if (rad_packet_add_val(req->pack, NULL, "Framed-Protocol", "PPP"))
K
Kozlov Dmitry 已提交
71
		goto out_err;
72
	if (rpd->ppp->ctrl->calling_station_id)
K
Kozlov Dmitry 已提交
73
		if (rad_packet_add_str(req->pack, NULL, "Calling-Station-Id", rpd->ppp->ctrl->calling_station_id))
74 75
			goto out_err;
	if (rpd->ppp->ctrl->called_station_id)
K
Kozlov Dmitry 已提交
76
		if (rad_packet_add_str(req->pack, NULL, "Called-Station-Id", rpd->ppp->ctrl->called_station_id))
77
			goto out_err;
78
	if (rpd->attr_class)
K
Kozlov Dmitry 已提交
79
		if (rad_packet_add_octets(req->pack, NULL, "Class", rpd->attr_class, rpd->attr_class_len))
80
			goto out_err;
K
Kozlov Dmitry 已提交
81

K
Kozlov Dmitry 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94
	list_for_each_entry(plugin, &req->rpd->plugin_list, entry) {
		switch (code) {
			case CODE_ACCESS_REQUEST:
				if (plugin->send_access_request && plugin->send_access_request(plugin, req->pack))
					goto out_err;
				break;
			case CODE_ACCOUNTING_REQUEST:
				if (plugin->send_accounting_request && plugin->send_accounting_request(plugin, req->pack))
					goto out_err;
				break;
		}
	}

K
Kozlov Dmitry 已提交
95
	return req;
D
Dmitry Kozlov 已提交
96 97 98 99

out_err:
	rad_req_free(req);
	return NULL;
K
Kozlov Dmitry 已提交
100 101
}

102 103
int rad_req_acct_fill(struct rad_req_t *req)
{
D
Dmitry Kozlov 已提交
104 105
	struct ipv6db_addr_t *a;

K
temp  
Kozlov Dmitry 已提交
106 107
	req->server_addr = req->serv->acct_addr;
	req->server_port = req->serv->acct_port;
108 109 110

	memset(req->RA, 0, sizeof(req->RA));

K
Kozlov Dmitry 已提交
111
	if (rad_packet_add_val(req->pack, NULL, "Acct-Status-Type", "Start"))
112
		return -1;
K
Kozlov Dmitry 已提交
113
	if (rad_packet_add_val(req->pack, NULL, "Acct-Authentic", "RADIUS"))
114
		return -1;
K
Kozlov Dmitry 已提交
115
	if (rad_packet_add_str(req->pack, NULL, "Acct-Session-Id", req->rpd->ppp->sessionid))
116
		return -1;
K
Kozlov Dmitry 已提交
117
	if (rad_packet_add_int(req->pack, NULL, "Acct-Session-Time", 0))
118
		return -1;
K
Kozlov Dmitry 已提交
119
	if (rad_packet_add_int(req->pack, NULL, "Acct-Input-Octets", 0))
120
		return -1;
K
Kozlov Dmitry 已提交
121
	if (rad_packet_add_int(req->pack, NULL, "Acct-Output-Octets", 0))
122
		return -1;
K
Kozlov Dmitry 已提交
123
	if (rad_packet_add_int(req->pack, NULL, "Acct-Input-Packets", 0))
124
		return -1;
K
Kozlov Dmitry 已提交
125
	if (rad_packet_add_int(req->pack, NULL, "Acct-Output-Packets", 0))
126
		return -1;
K
Kozlov Dmitry 已提交
127
	if (rad_packet_add_int(req->pack, NULL, "Acct-Input-Gigawords", 0))
128
		return -1;
K
Kozlov Dmitry 已提交
129
	if (rad_packet_add_int(req->pack, NULL, "Acct-Output-Gigawords", 0))
130
		return -1;
131 132 133 134
	if (conf_acct_delay_time) {
		if (rad_packet_add_int(req->pack, NULL, "Acct-Delay-Time", 0))
			return -1;
	}
135 136 137 138
	if (req->rpd->ppp->ipv4) {
		if (rad_packet_add_ipaddr(req->pack, NULL, "Framed-IP-Address", req->rpd->ppp->ipv4->peer_addr))
			return -1;
	}
D
Dmitry Kozlov 已提交
139 140 141 142 143 144 145 146
	if (req->rpd->ppp->ipv6) {
		if (rad_packet_add_ifid(req->pack, NULL, "Framed-Interface-Id", req->rpd->ppp->ipv6->intf_id))
			return -1;
		list_for_each_entry(a, &req->rpd->ppp->ipv6->addr_list, entry) {
			if (rad_packet_add_ipv6prefix(req->pack, NULL, "Framed-IPv6-Prefix", &a->addr, a->prefix_len))
				return -1;
		}
	}
147 148 149 150

	return 0;
}

151
void rad_req_free(struct rad_req_t *req)
K
Kozlov Dmitry 已提交
152
{
K
temp  
Kozlov Dmitry 已提交
153 154
	if (req->serv)
		rad_server_put(req->serv);
155 156 157 158 159 160
	if (req->hnd.fd >= 0 )
		close(req->hnd.fd);
	if (req->pack)
		rad_packet_free(req->pack);
	if (req->reply)
		rad_packet_free(req->reply);
D
Dmitry Kozlov 已提交
161
	_free(req);
K
Kozlov Dmitry 已提交
162 163
}

164
static int make_socket(struct rad_req_t *req)
K
Kozlov Dmitry 已提交
165 166
{
  struct sockaddr_in addr;
167

168 169
	req->hnd.fd = socket(PF_INET, SOCK_DGRAM, 0);
	if (req->hnd.fd < 0) {
170
		log_ppp_error("radius:socket: %s\n", strerror(errno));
171 172
		return -1;
	}
K
Kozlov Dmitry 已提交
173

174 175
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
K
Kozlov Dmitry 已提交
176

K
Kozlov Dmitry 已提交
177 178
	if (conf_bind) {
		addr.sin_addr.s_addr = conf_bind;
179
		if (bind(req->hnd.fd, (struct sockaddr *) &addr, sizeof(addr))) {
180
			log_ppp_error("radius:bind: %s\n", strerror(errno));
K
Kozlov Dmitry 已提交
181 182
			goto out_err;
		}
183
	}
K
Kozlov Dmitry 已提交
184

185
	addr.sin_addr.s_addr = req->server_addr;
186
	addr.sin_port = htons(req->server_port);
K
Kozlov Dmitry 已提交
187

188
	if (connect(req->hnd.fd, (struct sockaddr *) &addr, sizeof(addr))) {
189
		log_ppp_error("radius:connect: %s\n", strerror(errno));
190 191 192 193
		goto out_err;
	}

	if (fcntl(req->hnd.fd, F_SETFL, O_NONBLOCK)) {
194
		log_ppp_error("radius: failed to set nonblocking mode: %s\n", strerror(errno));
195
		goto out_err;
K
Kozlov Dmitry 已提交
196 197
	}
	
198 199 200 201 202 203 204 205
	return 0;

out_err:
	close(req->hnd.fd);
	req->hnd.fd = -1;
	return -1;
}

206
int rad_req_send(struct rad_req_t *req, int verbose)
207 208 209 210 211 212 213
{
	if (req->hnd.fd == -1 && make_socket(req))
		return -1;

	if (!req->pack->buf && rad_packet_build(req->pack, req->RA))
		goto out_err;
	
214
	if (verbose) {
215
		log_ppp_info1("send ");
216
		rad_packet_print(req->pack, req->serv, log_ppp_info1);
217 218
	}

219
	rad_packet_send(req->pack, req->hnd.fd, NULL);
K
Kozlov Dmitry 已提交
220 221 222 223 224 225 226 227 228

	return 0;

out_err:
	close(req->hnd.fd);
	req->hnd.fd = -1;
	return -1;
}

229 230
static void req_wakeup(struct rad_req_t *req)
{
231
	struct triton_context_t *ctx = req->rpd->ppp->ctrl->ctx;
K
Kozlov Dmitry 已提交
232 233
	if (req->timeout.tpd)
		triton_timer_del(&req->timeout);
234 235
	triton_md_unregister_handler(&req->hnd);
	triton_context_unregister(&req->ctx);
236
	triton_context_wakeup(ctx);
237
}
K
Kozlov Dmitry 已提交
238 239 240
static int rad_req_read(struct triton_md_handler_t *h)
{
	struct rad_req_t *req = container_of(h, typeof(*req), hnd);
K
Kozlov Dmitry 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	struct rad_packet_t *pack;
	int r;

	while (1) {
		r = rad_packet_recv(h->fd, &pack, NULL);
		
		if (pack) {
			if (req->reply)
				rad_packet_free(req->reply);
			req->reply = pack;
		}

		if (r)
			break;
	}
K
Kozlov Dmitry 已提交
256

257 258
	req_wakeup(req);
	
D
Dmitry Kozlov 已提交
259
	return 1;
K
Kozlov Dmitry 已提交
260
}
D
Dmitry Kozlov 已提交
261
static void rad_req_timeout(struct triton_timer_t *t)
K
Kozlov Dmitry 已提交
262
{
263 264 265
	struct rad_req_t *req = container_of(t, typeof(*req), timeout);
	
	req_wakeup(req);
K
Kozlov Dmitry 已提交
266 267
}

D
Dmitry Kozlov 已提交
268
int rad_req_wait(struct rad_req_t *req, int timeout)
K
Kozlov Dmitry 已提交
269
{
270 271 272
	req->hnd.read = rad_req_read;
	req->timeout.expire = rad_req_timeout;

273
	triton_context_register(&req->ctx, req->rpd->ppp);
274
	triton_md_register_handler(&req->ctx, &req->hnd);
K
Kozlov Dmitry 已提交
275
	triton_md_enable_handler(&req->hnd, MD_MODE_READ);
K
Kozlov Dmitry 已提交
276

D
Dmitry Kozlov 已提交
277
	req->timeout.period = timeout * 1000;
K
Kozlov Dmitry 已提交
278 279 280
	triton_timer_add(&req->ctx, &req->timeout, 0);
	
	triton_context_wakeup(&req->ctx);
K
Kozlov Dmitry 已提交
281

282
	triton_context_schedule();
K
Kozlov Dmitry 已提交
283

284
	if (conf_verbose && req->reply) {
285
		log_ppp_info1("recv ");
286
		rad_packet_print(req->reply, req->serv, log_ppp_info1);
287
	}
K
Kozlov Dmitry 已提交
288 289 290
	return 0;
}

291
static void req_init(void)
292 293 294
{
	urandom_fd = open("/dev/urandom", O_RDONLY);
	if (!urandom_fd) {
295
		log_emerg("radius:req: open /dev/urandom: %s\n", strerror(errno));
296 297 298
		_exit(EXIT_FAILURE);
	}
}
299

K
Kozlov Dmitry 已提交
300
DEFINE_INIT(50, req_init);