summaryrefslogtreecommitdiff
path: root/accel-pppd/radius/req.c
blob: a15baec4667499fd67ed0eb8f60f2a4815bbd2c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "log.h"
#include "radius_p.h"
#include "mempool.h"

#include "memdebug.h"

static int make_socket(struct rad_req_t *req);
static mempool_t req_pool;

static struct rad_req_t *__rad_req_alloc(struct radius_pd_t *rpd, int code, const char *username, in_addr_t addr, int port)
{
	struct rad_plugin_t *plugin;
	struct ppp_t *ppp = NULL;
	struct rad_req_t *req = mempool_alloc(req_pool);
	struct timespec ts;

	if (!req) {
		log_emerg("radius: out of memory\n");
		return NULL;
	}

	if (rpd->ses->ctrl->ppp)
		ppp = container_of(rpd->ses, typeof(*ppp), ses);

	clock_gettime(CLOCK_MONOTONIC, &ts);

	memset(req, 0, sizeof(*req));
	req->rpd = rpd;
	req->hnd.fd = -1;
	req->hnd.read = rad_req_read;
	req->ts = ts.tv_sec;

	req->type = code == CODE_ACCESS_REQUEST ? RAD_SERV_AUTH : RAD_SERV_ACCT;

	if (addr)
		req->serv = rad_server_get2(req->type, addr, port);
	else
		req->serv = rad_server_get(req->type);

	if (!req->serv)
		goto out_err;
	
	req->server_addr = req->serv->addr;
	req->server_port = req->serv->auth_port;

	while (1) {
		if (read(urandom_fd, req->RA, 16) != 16) {
			if (errno == EINTR)
				continue;
			log_ppp_error("radius:req:read urandom: %s\n", strerror(errno));
			goto out_err;
		}
		break;
	}

	if (conf_verbose)
		req->log = log_ppp_info1;

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

	if (rad_packet_add_str(req->pack, NULL, "User-Name", username))
		goto out_err;
	
	if (conf_nas_identifier)
		if (rad_packet_add_str(req->pack, NULL, "NAS-Identifier", conf_nas_identifier))
			goto out_err;

	if (conf_nas_ip_address)
		if (rad_packet_add_ipaddr(req->pack, NULL, "NAS-IP-Address", conf_nas_ip_address))
			goto out_err;

	if (rad_packet_add_int(req->pack, NULL, "NAS-Port", rpd->ses->unit_idx))
		goto out_err;
	
	if (rad_packet_add_str(req->pack, NULL, "NAS-Port-Id", rpd->ses->ifname))
		goto out_err;
	
	if (req->rpd->ses->ctrl->type == CTRL_TYPE_IPOE) {
		if (rad_packet_add_val(req->pack, NULL, "NAS-Port-Type", "Ethernet"))
			goto out_err;
	} else {
		if (rad_packet_add_val(req->pack, NULL, "NAS-Port-Type", "Virtual"))
			goto out_err;

		if (rad_packet_add_val(req->pack, NULL, "Service-Type", "Framed-User"))
			goto out_err;

		if (rad_packet_add_val(req->pack, NULL, "Framed-Protocol", "PPP"))
			goto out_err;
	}

	if (rpd->ses->ctrl->calling_station_id)
		if (rad_packet_add_str(req->pack, NULL, "Calling-Station-Id", rpd->ses->ctrl->calling_station_id))
			goto out_err;
	
	if (rpd->ses->ctrl->called_station_id)
		if (rad_packet_add_str(req->pack, NULL, "Called-Station-Id", rpd->ses->ctrl->called_station_id))
			goto out_err;
	
	if (rpd->attr_class)
		if (rad_packet_add_octets(req->pack, NULL, "Class", rpd->attr_class, rpd->attr_class_len))
			goto out_err;
	
	if (conf_attr_tunnel_type)
		if (rad_packet_add_str(req->pack, NULL, conf_attr_tunnel_type, rpd->ses->ctrl->name))
			goto out_err;

	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;
		}
	}

	return req;

out_err:
	if (!req->serv)
		log_ppp_error("radius: no servers available\n");
	else
		log_emerg("radius: out of memory\n");
	rad_req_free(req);
	return NULL;
}

struct rad_req_t *rad_req_alloc(struct radius_pd_t *rpd, int code, const char *username)
{
	return __rad_req_alloc(rpd, code, username, 0, 0);
}

struct rad_req_t *rad_req_alloc2(struct radius_pd_t *rpd, int code, const char *username, in_addr_t addr, int port)
{
	struct rad_req_t *req = __rad_req_alloc(rpd, code, username, addr, port);

	if (!req)
		return NULL;

	if (code == CODE_ACCOUNTING_REQUEST)
		req->server_port = req->serv->acct_port;

	if (make_socket(req)) {
		rad_req_free(req);
		req = NULL;
	}

	return req;
}

struct rad_req_t *rad_req_alloc_empty()
{
	struct rad_req_t *req = mempool_alloc(req_pool);

	if (!req) {
		log_emerg("radius: out of memory\n");
		return NULL;
	}

	memset(req, 0, sizeof(*req));
	req->hnd.fd = -1;

	return req;
}

int rad_req_acct_fill(struct rad_req_t *req)
{
	struct ipv6db_addr_t *a;

	req->server_port = req->serv->acct_port;

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

	if (rad_packet_add_val(req->pack, NULL, "Acct-Status-Type", "Start"))
		return -1;
	if (rad_packet_add_val(req->pack, NULL, "Acct-Authentic", "RADIUS"))
		return -1;
	if (rad_packet_add_str(req->pack, NULL, "Acct-Session-Id", req->rpd->ses->sessionid))
		return -1;
	if (rad_packet_add_int(req->pack, NULL, "Acct-Session-Time", 0))
		return -1;
	if (rad_packet_add_int(req->pack, NULL, "Acct-Input-Octets", 0))
		return -1;
	if (rad_packet_add_int(req->pack, NULL, "Acct-Output-Octets", 0))
		return -1;
	if (rad_packet_add_int(req->pack, NULL, "Acct-Input-Packets", 0))
		return -1;
	if (rad_packet_add_int(req->pack, NULL, "Acct-Output-Packets", 0))
		return -1;
	if (rad_packet_add_int(req->pack, NULL, "Acct-Input-Gigawords", 0))
		return -1;
	if (rad_packet_add_int(req->pack, NULL, "Acct-Output-Gigawords", 0))
		return -1;
	if (conf_acct_delay_time) {
		if (rad_packet_add_int(req->pack, NULL, "Acct-Delay-Time", 0))
			return -1;
	}
	if (req->rpd->ses->ipv4) {
		if (rad_packet_add_ipaddr(req->pack, NULL, "Framed-IP-Address", req->rpd->ses->ipv4->peer_addr))
			return -1;
	}
	if (req->rpd->ses->ipv6) {
		if (rad_packet_add_ifid(req->pack, NULL, "Framed-Interface-Id", req->rpd->ses->ipv6->peer_intf_id))
			return -1;
		list_for_each_entry(a, &req->rpd->ses->ipv6->addr_list, entry) {
			if (rad_packet_add_ipv6prefix(req->pack, NULL, "Framed-IPv6-Prefix", &a->addr, a->prefix_len))
				return -1;
		}
	}

	return 0;
}

void rad_req_free(struct rad_req_t *req)
{
	assert(!req->active);
	assert(!req->entry.next);

	if (req->serv)
		rad_server_put(req->serv, req->type);

	if (req->hnd.tpd)
		triton_md_unregister_handler(&req->hnd, 1);
	else if (req->hnd.fd != -1)
		close(req->hnd.fd);
	
	if (req->timeout.tpd)
		triton_timer_del(&req->timeout);

	if (req->pack)
		rad_packet_free(req->pack);

	if (req->reply)
		rad_packet_free(req->reply);

	mempool_free(req);
}

static int make_socket(struct rad_req_t *req)
{
  struct sockaddr_in addr;

	req->hnd.fd = socket(PF_INET, SOCK_DGRAM, 0);
	if (req->hnd.fd < 0) {
		log_ppp_error("radius:socket: %s\n", strerror(errno));
		return -1;
	}

	fcntl(req->hnd.fd, F_SETFD, fcntl(req->hnd.fd, F_GETFD) | FD_CLOEXEC);

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;

	if (conf_bind) {
		addr.sin_addr.s_addr = conf_bind;
		if (bind(req->hnd.fd, (struct sockaddr *) &addr, sizeof(addr))) {
			log_ppp_error("radius:bind: %s\n", strerror(errno));
			goto out_err;
		}
	}

	addr.sin_addr.s_addr = req->server_addr;
	addr.sin_port = htons(req->server_port);

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

	if (fcntl(req->hnd.fd, F_SETFL, O_NONBLOCK)) {
		log_ppp_error("radius: failed to set nonblocking mode: %s\n", strerror(errno));
		goto out_err;
	}
	
	return 0;

out_err:
	if (req->hnd.tpd)
		triton_md_unregister_handler(&req->hnd, 1);
	else {
		close(req->hnd.fd);
		req->hnd.fd = -1;
	}
	return -1;
}

int __rad_req_send(struct rad_req_t *req, int async)
{
	if (async == -1) {
		if (req->active)
			req->try = conf_max_try;
		if (rad_req_send(req))
			req->sent(req, -1);
		return 0;
	}

	if (req->hnd.fd == -1 && make_socket(req))
		return -1;
	
	if (req->before_send && req->before_send(req))
		goto out_err;

	if (!req->pack->buf && rad_packet_build(req->pack, req->RA))
		goto out_err;
	
	if (req->log) {
		req->log("send ");
		rad_packet_print(req->pack, req->serv, req->log);
	}
	
	if (req->sent)
		req->sent(req, 0);

	rad_packet_send(req->pack, req->hnd.fd, NULL);

	return 0;

out_err:
	if (req->hnd.tpd)
		triton_md_unregister_handler(&req->hnd, 1);
	else {
		close(req->hnd.fd);
		req->hnd.fd = -1;
	}
	
	if (async && req->sent)
		req->sent(req, -1);

	return -1;
}

int rad_req_send(struct rad_req_t *req)
{
	int r;

	req->send = __rad_req_send;

	if (req->try++ == conf_max_try) {
		if (req->active)
			rad_server_req_exit(req);
				
		log_ppp_warn("radius: server(%i) not responding\n", req->serv->id);
				
		if (rad_server_realloc(req)) {
			if (req->rpd)
				log_ppp_warn("radius: no available servers\n");
			return -1;
		}

		req->try = 1;
	}

	if (!req->active) {
		while (1) {
			r = rad_server_req_enter(req);

			if (r >= 0)
				break;
			
			if (rad_server_realloc(req)) {
				if (req->rpd)
					log_ppp_warn("radius: no available servers\n");
				return -1;
			}
		}
	} else
		r = __rad_req_send(req, 0);

	return r;
}

int rad_req_read(struct triton_md_handler_t *h)
{
	struct rad_req_t *req = container_of(h, typeof(*req), hnd);
	struct rad_packet_t *pack;

	if (!req->rpd)
	    log_switch(triton_context_self(), NULL);

	while (1) {
		if (rad_packet_recv(h->fd, &pack, NULL))
			return 0;

		rad_server_reply(req->serv);
		
		if (pack->id == req->pack->id)
			break;
		
		rad_packet_free(req->reply);
	}
	
	req->reply = pack;

	if (req->active)
		rad_server_req_exit(req);
	else
		rad_server_req_cancel(req, 0);

	if (req->log) {
		req->log("recv ");
		rad_packet_print(req->reply, req->serv, req->log);
	}

	if (req->recv)
		req->recv(req);

	return 1;
}

static void req_init(void)
{
	req_pool = mempool_create(sizeof(struct rad_req_t));
}

DEFINE_INIT(50, req_init);