ipoe.c 83.6 KB
Newer Older
K
Kozlov Dmitry 已提交
1 2 3 4 5 6 7
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
8
#include <assert.h>
K
Kozlov Dmitry 已提交
9 10 11
#include <time.h>
#include <arpa/inet.h>
#include <netinet/in.h>
12 13
#include <net/ethernet.h>
#include <netinet/ip.h>
K
Kozlov Dmitry 已提交
14 15 16
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
17
#include <linux/route.h>
K
Kozlov Dmitry 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31

#include <pcre.h>

#include "events.h"
#include "list.h"
#include "triton.h"
#include "log.h"
#include "mempool.h"
#include "utils.h"
#include "cli.h"
#include "ap_session.h"
#include "pwdb.h"
#include "ipdb.h"

32
#include "iputils.h"
33 34
#include "ipset.h"

K
Kozlov Dmitry 已提交
35
#include "connlimit.h"
36
#include "vlan_mon.h"
K
Kozlov Dmitry 已提交
37 38 39 40 41

#include "ipoe.h"

#include "memdebug.h"

42 43 44
#define USERNAME_UNSET 0
#define USERNAME_IFNAME 1
#define USERNAME_LUA 2
K
Kozlov Dmitry 已提交
45

K
Kozlov Dmitry 已提交
46 47 48
#define MODE_L2 0
#define MODE_L3 1

D
Dmitry Kozlov 已提交
49 50 51
struct ifaddr {
	struct list_head entry;
	in_addr_t addr;
52
	int mask;
D
Dmitry Kozlov 已提交
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
	int refs;
};

struct iplink_arg {
	pcre *re;
	const char *opt;
	long *arg1;
};

struct unit_cache {
	struct list_head entry;
	int ifindex;
};

struct l4_redirect {
	struct list_head entry;
	in_addr_t addr;
	time_t timeout;
};

struct gw_addr {
	struct list_head entry;
	in_addr_t addr;
	int mask;
	int mask1;
};

struct disc_item {
	struct list_head entry;
	struct dhcpv4_packet *pack;
	struct timespec ts;
};

struct delay {
	struct list_head entry;
	unsigned int conn_cnt;
	int delay;
};

92 93 94 95 96 97 98
struct request_item {
	struct list_head entry;
	uint32_t xid;
	time_t expire;
	int cnt;
};

99 100
enum {SID_MAC, SID_IP};

K
Kozlov Dmitry 已提交
101
static int conf_dhcpv4 = 1;
102 103
static int conf_up;
static int conf_mode;
K
Kozlov Dmitry 已提交
104
static int conf_shared = 1;
105
static int conf_ifcfg = 1;
106 107 108
static int conf_nat;
static int conf_arp;
static int conf_ipv6;
109
static uint32_t conf_src;
110
static const char *conf_ip_pool;
111
static const char *conf_l4_redirect_pool;
K
Kozlov Dmitry 已提交
112 113
//static int conf_dhcpv6;
static int conf_username;
114
static const char *conf_password;
K
Kozlov Dmitry 已提交
115
static int conf_unit_cache;
K
Kozlov Dmitry 已提交
116
static int conf_noauth;
117 118 119 120
#ifdef RADIUS
static int conf_attr_dhcp_client_ip;
static int conf_attr_dhcp_router_ip;
static int conf_attr_dhcp_mask;
121
static int conf_attr_dhcp_lease_time;
D
Dmitry Kozlov 已提交
122
static int conf_attr_dhcp_renew_time;
123
static int conf_attr_l4_redirect;
124 125
static int conf_attr_l4_redirect_table;
static int conf_attr_l4_redirect_ipset;
126
static const char *conf_attr_dhcp_opt82;
127 128
static const char *conf_attr_dhcp_opt82_remote_id;
static const char *conf_attr_dhcp_opt82_circuit_id;
129 130
#endif
static int conf_l4_redirect_table;
131
static int conf_l4_redirect_on_reject;
132
static const char *conf_l4_redirect_ipset;
D
Dmitry Kozlov 已提交
133
static int conf_vlan_timeout = 30;
134
static int conf_max_request = 3;
135 136
static int conf_session_timeout;
static int conf_idle_timeout;
137

K
Kozlov Dmitry 已提交
138
static const char *conf_relay;
K
Kozlov Dmitry 已提交
139 140 141 142 143

#ifdef USE_LUA
static const char *conf_lua_username_func;
#endif

144 145 146
static int conf_offer_timeout = 10;
static int conf_relay_timeout = 3;
static int conf_relay_retransmit = 3;
147
static LIST_HEAD(conf_gw_addr);
K
Kozlov Dmitry 已提交
148 149 150
static int conf_netmask = 24;
static int conf_lease_time = 600;
static int conf_lease_timeout = 660;
D
Dmitry Kozlov 已提交
151
static int conf_renew_time = 300;
K
Kozlov Dmitry 已提交
152
static int conf_verbose;
153
static const char *conf_agent_remote_id;
154
static int conf_proto;
D
Dmitry Kozlov 已提交
155
static LIST_HEAD(conf_offer_delay);
156
static const char *conf_vlan_name;
157
static int conf_ip_unnumbered;
158
static int conf_check_mac_change;
159
static int conf_soft_terminate;
160
static int conf_calling_sid = SID_MAC;
K
Kozlov Dmitry 已提交
161 162 163

static unsigned int stat_starting;
static unsigned int stat_active;
D
Dmitry Kozlov 已提交
164
static unsigned int stat_delayed_offer;
K
Kozlov Dmitry 已提交
165 166

static mempool_t ses_pool;
D
Dmitry Kozlov 已提交
167
static mempool_t disc_item_pool;
168
static mempool_t req_item_pool;
K
Kozlov Dmitry 已提交
169

170
static int connlimit_loaded;
171
static int radius_loaded;
172

K
Kozlov Dmitry 已提交
173
static LIST_HEAD(serv_list);
D
Dmitry Kozlov 已提交
174
static pthread_mutex_t serv_lock = PTHREAD_MUTEX_INITIALIZER;
D
Dmitry Kozlov 已提交
175

K
Kozlov Dmitry 已提交
176 177 178 179 180
static pthread_mutex_t uc_lock = PTHREAD_MUTEX_INITIALIZER;
static LIST_HEAD(uc_list);
static int uc_size;
static mempool_t uc_pool;

181 182 183 184 185
static pthread_rwlock_t l4_list_lock = PTHREAD_RWLOCK_INITIALIZER;
static LIST_HEAD(l4_redirect_list);
static struct triton_timer_t l4_redirect_timer;
static struct triton_context_t l4_redirect_ctx;

K
Kozlov Dmitry 已提交
186
static void ipoe_session_finished(struct ap_session *s);
K
Kozlov Dmitry 已提交
187
static void ipoe_drop_sessions(struct ipoe_serv *serv, struct ipoe_session *skip);
D
Dmitry Kozlov 已提交
188
static void ipoe_serv_release(struct ipoe_serv *serv);
K
Kozlov Dmitry 已提交
189
static void __ipoe_session_activate(struct ipoe_session *ses);
190
static void ipoe_ses_recv_dhcpv4(struct dhcpv4_serv *dhcpv4, struct dhcpv4_packet *pack);
D
Dmitry Kozlov 已提交
191
static void __ipoe_recv_dhcpv4(struct dhcpv4_serv *dhcpv4, struct dhcpv4_packet *pack, int force);
192
static void ipoe_session_keepalive(struct dhcpv4_packet *pack);
D
Dmitry Kozlov 已提交
193
static void add_interface(const char *ifname, int ifindex, const char *opt, int parent_ifindex, int vid);
D
Dmitry Kozlov 已提交
194
static int get_offer_delay();
195
static void __ipoe_session_start(struct ipoe_session *ses);
196 197
static int ipoe_rad_send_auth_request(struct rad_plugin_t *rad, struct rad_packet_t *pack);
static int ipoe_rad_send_acct_request(struct rad_plugin_t *rad, struct rad_packet_t *pack);
K
Kozlov Dmitry 已提交
198

D
Dmitry Kozlov 已提交
199 200 201
static void ipoe_ctx_switch(struct triton_context_t *ctx, void *arg)
{
	net = &def_net;
D
Dmitry Kozlov 已提交
202
	log_switch(ctx, arg);
D
Dmitry Kozlov 已提交
203 204
}

K
Kozlov Dmitry 已提交
205
static struct ipoe_session *ipoe_session_lookup(struct ipoe_serv *serv, struct dhcpv4_packet *pack, struct ipoe_session **opt82_ses)
K
Kozlov Dmitry 已提交
206
{
K
Kozlov Dmitry 已提交
207
	struct ipoe_session *ses, *res = NULL;
D
Dmitry Kozlov 已提交
208

K
Kozlov Dmitry 已提交
209 210
	uint8_t *agent_circuit_id = NULL;
	uint8_t *agent_remote_id = NULL;
K
Kozlov Dmitry 已提交
211 212 213 214
	int opt82_match;

	if (opt82_ses)
		*opt82_ses = NULL;
K
Kozlov Dmitry 已提交
215

216
	if (!conf_check_mac_change || (pack->relay_agent && dhcpv4_parse_opt82(pack->relay_agent, &agent_circuit_id, &agent_remote_id))) {
K
Kozlov Dmitry 已提交
217 218 219
		agent_circuit_id = NULL;
		agent_remote_id = NULL;
	}
K
Kozlov Dmitry 已提交
220

K
Kozlov Dmitry 已提交
221
	list_for_each_entry(ses, &serv->sessions, entry) {
222
		opt82_match = conf_check_mac_change && pack->relay_agent != NULL;
D
Dmitry Kozlov 已提交
223

224
		if (agent_circuit_id && !ses->agent_circuit_id)
K
Kozlov Dmitry 已提交
225
			opt82_match = 0;
D
Dmitry Kozlov 已提交
226

K
Kozlov Dmitry 已提交
227 228
		if (opt82_match && agent_remote_id && !ses->agent_remote_id)
			opt82_match = 0;
D
Dmitry Kozlov 已提交
229

K
Kozlov Dmitry 已提交
230 231
		if (opt82_match && !agent_circuit_id && ses->agent_circuit_id)
			opt82_match = 0;
D
Dmitry Kozlov 已提交
232

K
Kozlov Dmitry 已提交
233 234
		if (opt82_match && !agent_remote_id && ses->agent_remote_id)
			opt82_match = 0;
D
Dmitry Kozlov 已提交
235

K
Kozlov Dmitry 已提交
236
		if (opt82_match && agent_circuit_id) {
K
Kozlov Dmitry 已提交
237
			if (*agent_circuit_id != *ses->agent_circuit_id)
K
Kozlov Dmitry 已提交
238
				opt82_match = 0;
D
Dmitry Kozlov 已提交
239

K
Kozlov Dmitry 已提交
240
			if (memcmp(agent_circuit_id + 1, ses->agent_circuit_id + 1, *agent_circuit_id))
K
Kozlov Dmitry 已提交
241
				opt82_match = 0;
K
Kozlov Dmitry 已提交
242
		}
D
Dmitry Kozlov 已提交
243

K
Kozlov Dmitry 已提交
244
		if (opt82_match && agent_remote_id) {
K
Kozlov Dmitry 已提交
245
			if (*agent_remote_id != *ses->agent_remote_id)
K
Kozlov Dmitry 已提交
246 247
				opt82_match = 0;

K
Kozlov Dmitry 已提交
248
			if (memcmp(agent_remote_id + 1, ses->agent_remote_id + 1, *agent_remote_id))
K
Kozlov Dmitry 已提交
249
				opt82_match = 0;
K
Kozlov Dmitry 已提交
250
		}
K
Kozlov Dmitry 已提交
251 252 253

		if (opt82_match && opt82_ses)
			*opt82_ses = ses;
D
Dmitry Kozlov 已提交
254

D
Dmitry Kozlov 已提交
255
		if (memcmp(pack->hdr->chaddr, ses->hwaddr, ETH_ALEN))
K
Kozlov Dmitry 已提交
256
			continue;
D
Dmitry Kozlov 已提交
257

K
Kozlov Dmitry 已提交
258 259
		res = ses;
		break;
D
Dmitry Kozlov 已提交
260

K
Kozlov Dmitry 已提交
261
		/*if (pack->client_id && !ses->client_id)
262
			continue;
D
Dmitry Kozlov 已提交
263

264 265
		if (!pack->client_id && ses->client_id)
			continue;
D
Dmitry Kozlov 已提交
266

K
Kozlov Dmitry 已提交
267 268 269 270 271 272 273
		if (pack->client_id) {
			if (pack->client_id->len != ses->client_id->len)
				continue;
			if (memcmp(pack->client_id->data, ses->client_id->data, pack->client_id->len))
				continue;
		}

K
Kozlov Dmitry 已提交
274 275 276 277 278
		ses1 = ses;

		if (pack->hdr->xid != ses->xid)
			continue;

K
Kozlov Dmitry 已提交
279
		return ses;*/
K
Kozlov Dmitry 已提交
280 281
	}

K
Kozlov Dmitry 已提交
282 283
	if (!res || !pack->relay_agent || !opt82_ses || *opt82_ses)
		return res;
D
Dmitry Kozlov 已提交
284

K
Kozlov Dmitry 已提交
285 286 287
	list_for_each_entry(ses, &serv->sessions, entry) {
		if (agent_circuit_id && !ses->agent_circuit_id)
			continue;
D
Dmitry Kozlov 已提交
288

K
Kozlov Dmitry 已提交
289 290
		if (opt82_match && agent_remote_id && !ses->agent_remote_id)
			continue;
D
Dmitry Kozlov 已提交
291

K
Kozlov Dmitry 已提交
292 293
		if (opt82_match && !agent_circuit_id && ses->agent_circuit_id)
			continue;
D
Dmitry Kozlov 已提交
294

K
Kozlov Dmitry 已提交
295 296
		if (opt82_match && !agent_remote_id && ses->agent_remote_id)
			continue;
D
Dmitry Kozlov 已提交
297

K
Kozlov Dmitry 已提交
298 299 300
		if (opt82_match && agent_circuit_id) {
			if (*agent_circuit_id != *ses->agent_circuit_id)
				continue;
D
Dmitry Kozlov 已提交
301

K
Kozlov Dmitry 已提交
302 303 304
			if (memcmp(agent_circuit_id + 1, ses->agent_circuit_id + 1, *agent_circuit_id))
				continue;
		}
D
Dmitry Kozlov 已提交
305

K
Kozlov Dmitry 已提交
306 307 308 309 310 311 312 313 314 315 316
		if (opt82_match && agent_remote_id) {
			if (*agent_remote_id != *ses->agent_remote_id)
				continue;

			if (memcmp(agent_remote_id + 1, ses->agent_remote_id + 1, *agent_remote_id))
				continue;
		}

		*opt82_ses = ses;
		break;
	}
D
Dmitry Kozlov 已提交
317

K
Kozlov Dmitry 已提交
318
	return res;
K
Kozlov Dmitry 已提交
319 320 321 322 323 324 325 326
}

static void ipoe_session_timeout(struct triton_timer_t *t)
{
	struct ipoe_session *ses = container_of(t, typeof(*ses), timer);

	triton_timer_del(t);

K
Kozlov Dmitry 已提交
327
	log_ppp_info2("ipoe: session timed out\n");
K
Kozlov Dmitry 已提交
328

329
	ap_session_terminate(&ses->ses, TERM_LOST_CARRIER, 1);
K
Kozlov Dmitry 已提交
330 331
}

332 333 334 335 336 337 338 339
static void ipoe_session_l4_redirect_timeout(struct triton_timer_t *t)
{
	struct ipoe_session *ses = container_of(t, typeof(*ses), l4_redirect_timer);

	triton_timer_del(t);

	log_ppp_info2("ipoe: session timed out\n");

340
	ap_session_terminate(&ses->ses, TERM_NAS_REQUEST, 1);
341 342
}

343 344 345 346 347 348 349 350 351 352 353 354 355 356
static void ipoe_relay_timeout(struct triton_timer_t *t)
{
	struct ipoe_session *ses = container_of(t, typeof(*ses), timer);

	if (!ses->serv->dhcpv4_relay || !ses->dhcpv4_request) {
		triton_timer_del(t);
		return;
	}

	if (++ses->relay_retransmit > conf_relay_retransmit) {
		triton_timer_del(t);

		log_ppp_info2("ipoe: relay timed out\n");

357
		ap_session_terminate(&ses->ses, TERM_LOST_CARRIER, 1);
358 359 360 361 362
	} else
		dhcpv4_relay_send(ses->serv->dhcpv4_relay, ses->dhcpv4_request, ses->relay_server_id, ses->serv->ifname, conf_agent_remote_id);
}


363
static char *ipoe_session_get_username(struct ipoe_session *ses)
K
Kozlov Dmitry 已提交
364
{
365 366 367
	if (ses->username)
		return ses->username;

K
Kozlov Dmitry 已提交
368
#ifdef USE_LUA
369 370 371
	if (ses->serv->opt_username == USERNAME_LUA)
		return ipoe_lua_get_username(ses, ses->serv->opt_lua_username_func ? : conf_lua_username_func);
	else
K
Kozlov Dmitry 已提交
372
#endif
373 374
	if (!ses->dhcpv4_request)
		return _strdup(ses->ctrl.calling_station_id);
375

376
	return _strdup(ses->serv->ifname);
K
Kozlov Dmitry 已提交
377 378
}

379
static void l4_redirect_list_add(in_addr_t addr)
380 381 382 383 384 385 386 387 388 389 390 391
{
	struct l4_redirect *n = _malloc(sizeof(*n));
	struct timespec ts;

	if (!n)
		return;

	clock_gettime(CLOCK_MONOTONIC, &ts);

	memset(n, 0, sizeof(*n));
	n->addr = addr;
	n->timeout = ts.tv_sec + conf_l4_redirect_on_reject;
D
Dmitry Kozlov 已提交
392

393
	ipoe_nl_add_exclude(addr, 32);
394 395 396 397 398 399

	if (conf_l4_redirect_table)
		iprule_add(addr, conf_l4_redirect_table);

	if (conf_l4_redirect_ipset)
		ipset_add(conf_l4_redirect_ipset, addr);
400 401

	pthread_rwlock_wrlock(&l4_list_lock);
D
Dmitry Kozlov 已提交
402

403
	list_add_tail(&n->entry, &l4_redirect_list);
D
Dmitry Kozlov 已提交
404

405 406 407
	if (!l4_redirect_timer.tpd)
		triton_timer_add(&l4_redirect_ctx, &l4_redirect_timer, 0);

D
Dmitry Kozlov 已提交
408
	pthread_rwlock_unlock(&l4_list_lock);
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
}

static int l4_redirect_list_check(in_addr_t addr)
{
	struct l4_redirect *n;

	pthread_rwlock_rdlock(&l4_list_lock);
	list_for_each_entry(n, &l4_redirect_list, entry) {
		if (n->addr == addr) {
			pthread_rwlock_unlock(&l4_list_lock);
			return 1;
		}
	}
	pthread_rwlock_unlock(&l4_list_lock);
	return 0;
}

static void l4_redirect_list_timer(struct triton_timer_t *t)
{
	struct l4_redirect *n;
	struct timespec ts;

	clock_gettime(CLOCK_MONOTONIC, &ts);

	pthread_rwlock_wrlock(&l4_list_lock);
	while (!list_empty(&l4_redirect_list)) {
		n = list_entry(l4_redirect_list.next, typeof(*n), entry);
		if (ts.tv_sec > n->timeout) {
			list_del(&n->entry);
			pthread_rwlock_unlock(&l4_list_lock);
439 440 441

			if (conf_l4_redirect_table)
				iprule_del(n->addr, conf_l4_redirect_table);
D
Dmitry Kozlov 已提交
442

443 444
			if (conf_l4_redirect_ipset)
				ipset_del(conf_l4_redirect_ipset, n->addr);
D
Dmitry Kozlov 已提交
445

446
			ipoe_nl_del_exclude(n->addr);
447 448 449 450 451 452 453 454 455 456 457 458 459

			_free(n);
			pthread_rwlock_wrlock(&l4_list_lock);
		} else
			break;
	}

	if (list_empty(&l4_redirect_list) && l4_redirect_timer.tpd)
		triton_timer_del(&l4_redirect_timer);

	pthread_rwlock_unlock(&l4_list_lock);
}

460 461 462
static void ipoe_change_l4_redirect(struct ipoe_session *ses, int del)
{
	in_addr_t addr;
D
Dmitry Kozlov 已提交
463

464
	if (ses->ses.ipv4)
465
		addr = ses->ses.ipv4->peer_addr;
466 467
	else
		addr = ses->yiaddr;
D
Dmitry Kozlov 已提交
468

469
	if (ses->l4_redirect_table) {
470
		if (del) {
471
			iprule_del(addr, ses->l4_redirect_table);
472 473
			ses->l4_redirect_set = 0;
		} else {
474
			iprule_add(addr, ses->l4_redirect_table);
475 476 477
			ses->l4_redirect_set = 1;
		}
	}
478

479
	if (conf_l4_redirect_ipset || ses->l4_redirect_ipset) {
480
		if (del) {
481
			ipset_del(ses->l4_redirect_ipset ?: conf_l4_redirect_ipset, addr);
482 483
			ses->l4_redirect_set = 0;
		} else {
484
			ipset_add(ses->l4_redirect_ipset ?: conf_l4_redirect_ipset, addr);
485 486
			ses->l4_redirect_set = 1;
		}
K
Kozlov Dmitry 已提交
487
	}
488 489 490

	if (del && ses->l4_redirect_timer.tpd)
		triton_timer_del(&ses->l4_redirect_timer);
491 492 493 494 495 496 497
}

static void ipoe_change_addr(struct ipoe_session *ses, in_addr_t newaddr)
{

}

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
static int ipoe_create_interface(struct ipoe_session *ses)
{
	struct unit_cache *uc;
	struct ifreq ifr;

	pthread_mutex_lock(&uc_lock);
	if (!list_empty(&uc_list)) {
		uc = list_entry(uc_list.next, typeof(*uc), entry);
		ses->ifindex = uc->ifindex;
		list_del(&uc->entry);
		--uc_size;
		pthread_mutex_unlock(&uc_lock);
		mempool_free(uc);
	} else {
		pthread_mutex_unlock(&uc_lock);
		ses->ifindex = ipoe_nl_create(0, 0, ses->serv->opt_mode == MODE_L2 ? ses->serv->ifname : NULL, ses->hwaddr);
		if (ses->ifindex == -1) {
			log_ppp_error("ipoe: failed to create interface\n");
			ap_session_terminate(&ses->ses, TERM_NAS_ERROR, 1);
			return -1;
		}
	}

	memset(&ifr, 0, sizeof(ifr));
	ifr.ifr_ifindex = ses->ifindex;
	if (ioctl(sock_fd, SIOCGIFNAME, &ifr, sizeof(ifr))) {
		log_ppp_error("ipoe: failed to get interface name\n");
		ses->ifindex = -1;
		ap_session_terminate(&ses->ses, TERM_NAS_ERROR, 1);
		return -1;
	}

	strncpy(ses->ses.ifname, ifr.ifr_name, AP_IFNAME_LEN);
	ses->ses.ifindex = ses->ifindex;
	ses->ses.unit_idx = ses->ifindex;
533
	ses->ctrl.dont_ifcfg = !conf_ip_unnumbered;
534

535 536
	log_ppp_info2("create interface %s parent %s\n", ifr.ifr_name, ses->serv->ifname);

537 538 539
	return 0;
}

540 541 542 543 544 545 546
static void auth_result(struct ipoe_session *ses, int r)
{
	char *username = ses->username;

	ses->username = NULL;

	if (r == PWDB_DENIED) {
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
		if (conf_l4_redirect_on_reject && ses->dhcpv4_request) {
			ses->l4_redirect = 1;
			if (conf_l4_redirect_pool) {
				if (ses->ses.ipv4_pool_name)
					_free(ses->ses.ipv4_pool_name);
				ses->ses.ipv4_pool_name = _strdup(conf_l4_redirect_pool);
			}

			ses->l4_redirect_timer.expire = ipoe_session_l4_redirect_timeout;
			ses->l4_redirect_timer.expire_tv.tv_sec = conf_l4_redirect_on_reject;
			triton_timer_add(&ses->ctx, &ses->l4_redirect_timer, 0);

			ap_session_set_username(&ses->ses, username);
			log_ppp_info1("%s: authentication failed\n", ses->ses.username);
			log_ppp_info1("%s: start temporary session (l4-redirect)\n", ses->ses.username);
			goto cont;
		}

565 566 567 568 569 570 571 572
		pthread_rwlock_wrlock(&ses_lock);
		ses->ses.username = username;
		ses->ses.terminate_cause = TERM_AUTH_ERROR;
		pthread_rwlock_unlock(&ses_lock);
		if (conf_ppp_verbose)
			log_ppp_warn("authentication failed\n");
		if (conf_l4_redirect_on_reject && !ses->dhcpv4_request)
			l4_redirect_list_add(ses->yiaddr);
573
		ap_session_terminate(&ses->ses, TERM_AUTH_ERROR, 1);
574 575 576 577 578
		return;
	}

	ap_session_set_username(&ses->ses, username);
	log_ppp_info1("%s: authentication succeeded\n", ses->ses.username);
579 580

cont:
581 582 583 584
	triton_event_fire(EV_SES_AUTHORIZED, &ses->ses);

	if (ses->serv->opt_nat)
		ses->ses.ipv4 = ipdb_get_ipv4(&ses->ses);
D
Dmitry Kozlov 已提交
585

586
	if (ses->serv->opt_shared == 0 && ses->ses.ipv4 && ses->ses.ipv4->peer_addr != ses->yiaddr) {
587 588 589
		if (ipoe_create_interface(ses))
			return;

590 591
		ap_session_set_ifindex(&ses->ses);
	}
592 593 594 595 596 597 598 599 600 601 602

	if (ses->dhcpv4_request && ses->serv->dhcpv4_relay) {
		dhcpv4_relay_send(ses->serv->dhcpv4_relay, ses->dhcpv4_request, ses->relay_server_id, ses->serv->ifname, conf_agent_remote_id);

		ses->timer.expire = ipoe_relay_timeout;
		ses->timer.period = conf_relay_timeout * 1000;
		triton_timer_add(&ses->ctx, &ses->timer, 0);
	} else
		__ipoe_session_start(ses);
}

K
Kozlov Dmitry 已提交
603 604 605 606
static void ipoe_session_start(struct ipoe_session *ses)
{
	int r;
	char *passwd;
607
	char *username;
608
	const char *pass;
D
Dmitry Kozlov 已提交
609

610 611 612 613
	if (ses->dhcpv4_request && conf_verbose) {
		log_ppp_info2("recv ");
		dhcpv4_print_packet(ses->dhcpv4_request, 0, log_ppp_info2);
	}
K
Kozlov Dmitry 已提交
614

D
Dmitry Kozlov 已提交
615
	__sync_add_and_fetch(&stat_starting, 1);
D
Dmitry Kozlov 已提交
616

617
	assert(!ses->ses.username);
618

619 620 621 622 623
	username = ipoe_session_get_username(ses);

	if (!username) {
		ipoe_session_finished(&ses->ses);
		return;
K
Kozlov Dmitry 已提交
624
	}
625 626

	ses->ses.unit_idx = ses->serv->ifindex;
D
Dmitry Kozlov 已提交
627

K
Kozlov Dmitry 已提交
628 629 630 631
	triton_event_fire(EV_CTRL_STARTING, &ses->ses);
	triton_event_fire(EV_CTRL_STARTED, &ses->ses);

	ap_session_starting(&ses->ses);
D
Dmitry Kozlov 已提交
632 633

	if (conf_noauth)
634 635
		r = PWDB_SUCCESS;
	else {
636 637 638
		if (ses->serv->opt_shared && ipoe_create_interface(ses))
			return;

639
#ifdef RADIUS
640
		if (radius_loaded) {
641 642
			ses->radius.send_access_request = ipoe_rad_send_auth_request;
			ses->radius.send_accounting_request = ipoe_rad_send_acct_request;
643 644 645 646
			rad_register_plugin(&ses->ses, &ses->radius);
		}
#endif

647 648 649 650 651 652 653 654
		if (conf_password) {
			if (!strcmp(conf_password, "csid"))
				pass = ses->ctrl.calling_station_id;
			else
				pass = conf_password;
		} else
			pass = username;

655
		ses->username = username;
656
		r = pwdb_check(&ses->ses, (pwdb_callback)auth_result, ses, username, PPP_PAP, pass);
D
Dmitry Kozlov 已提交
657

658 659 660
		if (r == PWDB_WAIT)
			return;

K
Kozlov Dmitry 已提交
661
		if (r == PWDB_NO_IMPL) {
662
			passwd = pwdb_get_passwd(&ses->ses, username);
663
			if (!passwd || strcmp(passwd, pass))
K
Kozlov Dmitry 已提交
664 665 666 667 668
				r = PWDB_DENIED;
			else {
				r = PWDB_SUCCESS;
				_free(passwd);
			}
K
Kozlov Dmitry 已提交
669 670
		}
	}
D
Dmitry Kozlov 已提交
671

672
	auth_result(ses, r);
K
Kozlov Dmitry 已提交
673 674
}

675 676 677 678 679
static void find_gw_addr(struct ipoe_session *ses)
{
	struct gw_addr *a;

	list_for_each_entry(a, &conf_gw_addr, entry) {
680
		if ((ntohl(ses->yiaddr) & (a->mask1)) == (ntohl(a->addr) & (a->mask1))) {
681
			ses->router = a->addr;
682 683 684 685 686 687
			ses->mask = a->mask;
			return;
		}
	}
}

D
Dmitry Kozlov 已提交
688
static void __ipoe_session_start(struct ipoe_session *ses)
K
Kozlov Dmitry 已提交
689
{
690
	if (!ses->yiaddr) {
691
		dhcpv4_get_ip(ses->serv->dhcpv4, &ses->yiaddr, &ses->router, &ses->mask);
692 693 694
		if (ses->yiaddr)
			ses->dhcp_addr = 1;
	}
695

696 697 698
	if (!ses->yiaddr && !ses->serv->opt_nat)
		ses->ses.ipv4 = ipdb_get_ipv4(&ses->ses);

K
Kozlov Dmitry 已提交
699
	if (ses->ses.ipv4) {
700 701
		if (!ses->mask)
			ses->mask = ses->ses.ipv4->mask;
K
Kozlov Dmitry 已提交
702

K
Kozlov Dmitry 已提交
703 704
		if (!ses->yiaddr)
			ses->yiaddr = ses->ses.ipv4->peer_addr;
D
Dmitry Kozlov 已提交
705

706 707
		if (!ses->router)
			ses->router = ses->ses.ipv4->addr;
K
Kozlov Dmitry 已提交
708
	} /*else if (ses->yiaddr) {
K
Kozlov Dmitry 已提交
709 710 711 712 713
		ses->ses.ipv4 = &ses->ipv4;
		ses->ipv4.addr = ses->siaddr;
		ses->ipv4.peer_addr = ses->yiaddr;
		ses->ipv4.mask = ses->mask;
		ses->ipv4.owner = NULL;
K
Kozlov Dmitry 已提交
714
	}*/
D
Dmitry Kozlov 已提交
715

K
Kozlov Dmitry 已提交
716 717 718
	if (ses->dhcpv4_request) {
		if (!ses->yiaddr) {
			log_ppp_error("no free IPv4 address\n");
719
			ap_session_terminate(&ses->ses, TERM_NAS_REQUEST, 1);
K
Kozlov Dmitry 已提交
720 721
			return;
		}
722 723 724

		if (!ses->router)
			find_gw_addr(ses);
D
Dmitry Kozlov 已提交
725

726 727
		if (!ses->mask)
			ses->mask = conf_netmask;
D
Dmitry Kozlov 已提交
728

729 730
		if (!ses->mask)
			ses->mask = 32;
D
Dmitry Kozlov 已提交
731

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
		if (ses->dhcpv4_request->hdr->giaddr) {
			/*uint32_t mask = ses->mask == 32 ? 0xffffffff : (((1 << ses->mask) - 1) << (32 - ses->mask));

			ses->siaddr = iproute_get(ses->dhcpv4_request->hdr->giaddr);
			if ((ntohl(ses->router) & mask) == (ntohl(ses->siaddr) & mask))
				ses->siaddr = ses->router;
			else if (!ses->router)
				ses->router = ses->dhcpv4_request->hdr->giaddr;*/
			if (ses->serv->opt_mode == MODE_L2)
				ses->siaddr = ses->router;
			else {
				ses->siaddr = iproute_get(ses->dhcpv4_request->hdr->giaddr, NULL);
				if (!ses->router)
					ses->router = ses->dhcpv4_request->hdr->giaddr;
			}
		}
D
Dmitry Kozlov 已提交
748

749 750
		if (!ses->router) {
			log_ppp_error("can't determine router address\n");
751
			ap_session_terminate(&ses->ses, TERM_NAS_REQUEST, 1);
752
			return;
D
Dmitry Kozlov 已提交
753 754
		}

K
Kozlov Dmitry 已提交
755 756
		if (!ses->siaddr && ses->router != ses->yiaddr)
			ses->siaddr = ses->router;
D
Dmitry Kozlov 已提交
757

758
		if (!ses->siaddr)
D
Dmitry Kozlov 已提交
759
			ses->siaddr = ses->serv->opt_src;
760

K
Kozlov Dmitry 已提交
761 762
		if (!ses->siaddr && ses->serv->dhcpv4_relay)
			ses->siaddr = ses->serv->dhcpv4_relay->giaddr;
763

K
Kozlov Dmitry 已提交
764 765
		if (!ses->siaddr) {
			log_ppp_error("can't determine Server-ID\n");
766
			ap_session_terminate(&ses->ses, TERM_NAS_ERROR, 1);
K
Kozlov Dmitry 已提交
767 768
			return;
		}
769

770 771 772
		if (ses->ses.ipv4 && !ses->ses.ipv4->addr)
			ses->ses.ipv4->addr = ses->siaddr;

D
Dmitry Kozlov 已提交
773
		dhcpv4_send_reply(DHCPOFFER, ses->serv->dhcpv4, ses->dhcpv4_request, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->renew_time, ses->dhcpv4_relay_reply);
K
Kozlov Dmitry 已提交
774 775 776

		dhcpv4_packet_free(ses->dhcpv4_request);
		ses->dhcpv4_request = NULL;
D
Dmitry Kozlov 已提交
777

778
		ses->timer.expire = ipoe_session_timeout;
779
		ses->timer.period = 0;
780 781
		ses->timer.expire_tv.tv_sec = conf_offer_timeout;
		triton_timer_add(&ses->ctx, &ses->timer, 0);
782 783 784
	} else {
		if (!ses->siaddr)
			find_gw_addr(ses);
D
Dmitry Kozlov 已提交
785

786 787 788 789
		if (!ses->siaddr)
			ses->siaddr = ses->serv->opt_src;

		if (!ses->siaddr)
790
			ses->siaddr = iproute_get(ses->yiaddr, NULL);
791 792 793

		if (!ses->siaddr) {
			log_ppp_error("can't determine local address\n");
794
			ap_session_terminate(&ses->ses, TERM_NAS_ERROR, 1);
795 796
			return;
		}
D
Dmitry Kozlov 已提交
797

798 799 800
		if (ses->ses.ipv4 && !ses->ses.ipv4->addr)
			ses->ses.ipv4->addr = ses->siaddr;

K
Kozlov Dmitry 已提交
801
		__ipoe_session_activate(ses);
802
	}
K
Kozlov Dmitry 已提交
803 804
}

805
static void ipoe_serv_add_addr(struct ipoe_serv *serv, in_addr_t addr, int mask)
806 807 808 809
{
	struct ifaddr *a;

	pthread_mutex_lock(&serv->lock);
D
Dmitry Kozlov 已提交
810

811 812 813 814 815
	if (serv->opt_shared) {
		list_for_each_entry(a, &serv->addr_list, entry) {
			if (a->addr == addr) {
				a->refs++;
				pthread_mutex_unlock(&serv->lock);
K
Kozlov Dmitry 已提交
816

817 818
				return;
			}
K
Kozlov Dmitry 已提交
819 820 821 822 823
		}
	}

	a = _malloc(sizeof(*a));
	a->addr = addr;
824
	a->mask = mask;
K
Kozlov Dmitry 已提交
825 826 827
	a->refs = 1;
	list_add_tail(&a->entry, &serv->addr_list);

828
	if (ipaddr_add(serv->ifindex, a->addr, mask))
K
Kozlov Dmitry 已提交
829 830 831 832 833
		log_warn("ipoe: failed to add addess to interface '%s'\n", serv->ifname);

	pthread_mutex_unlock(&serv->lock);
}

834
static void ipoe_serv_del_addr(struct ipoe_serv *serv, in_addr_t addr, int lock)
K
Kozlov Dmitry 已提交
835 836 837
{
	struct ifaddr *a;

838 839
	if (lock)
		pthread_mutex_lock(&serv->lock);
K
Kozlov Dmitry 已提交
840 841 842 843

	list_for_each_entry(a, &serv->addr_list, entry) {
		if (a->addr == addr) {
			if (--a->refs == 0) {
844
				if (ipaddr_del(serv->ifindex, a->addr, a->mask))
K
Kozlov Dmitry 已提交
845 846 847
					log_warn("ipoe: failed to delete addess from interface '%s'\n", serv->ifname);
				list_del(&a->entry);
				_free(a);
848
			}
K
Kozlov Dmitry 已提交
849
			break;
850
		}
K
Kozlov Dmitry 已提交
851
	}
D
Dmitry Kozlov 已提交
852

853 854
	if (lock)
		pthread_mutex_unlock(&serv->lock);
K
Kozlov Dmitry 已提交
855 856 857 858 859 860
}

static void ipoe_ifcfg_add(struct ipoe_session *ses)
{
	struct ipoe_serv *serv = ses->serv;

861 862
	if (ses->serv->opt_ifcfg)
		ipoe_serv_add_addr(ses->serv, ses->siaddr, conf_ip_unnumbered ? 32 : ses->mask);
D
Dmitry Kozlov 已提交
863

864
	if (conf_ip_unnumbered) {
865
		if (iproute_add(serv->ifindex, ses->serv->opt_src ? ses->serv->opt_src : ses->router, ses->yiaddr, 0, conf_proto, 32))
K
Kozlov Dmitry 已提交
866
			log_ppp_warn("ipoe: failed to add route to interface '%s'\n", serv->ifname);
867
	}
868 869 870 871

	ses->ifcfg = 1;
}

D
Dmitry Kozlov 已提交
872
static void ipoe_ifcfg_del(struct ipoe_session *ses, int lock)
873 874
{
	struct ipoe_serv *serv = ses->serv;
D
Dmitry Kozlov 已提交
875

876
	if (conf_ip_unnumbered) {
877
		if (iproute_del(serv->ifindex, ses->yiaddr, conf_proto, 32))
878
			log_ppp_warn("ipoe: failed to delete route from interface '%s'\n", serv->ifname);
K
Kozlov Dmitry 已提交
879
	}
880 881

	if (ses->serv->opt_ifcfg)
882
		ipoe_serv_del_addr(ses->serv, ses->siaddr, lock);
883 884
}

K
Kozlov Dmitry 已提交
885
static void __ipoe_session_activate(struct ipoe_session *ses)
K
Kozlov Dmitry 已提交
886
{
887 888
	uint32_t addr;

889 890
	if (ses->terminating)
		return;
D
Dmitry Kozlov 已提交
891

892
	if (ses->ifindex != -1) {
K
Kozlov Dmitry 已提交
893
		addr = 0;
894 895 896 897 898 899
		if (!ses->ses.ipv4) {
			if (ses->serv->opt_mode == MODE_L3) {
				addr = 1;
				ses->ctrl.dont_ifcfg = 1;
			}
		} else if (ses->ses.ipv4->peer_addr != ses->yiaddr)
900
			addr = ses->ses.ipv4->peer_addr;
901 902
		else if (!conf_ip_unnumbered)
			ses->ctrl.dont_ifcfg = 1;
903 904 905 906 907

		if (ses->dhcpv4_request && ses->serv->opt_mode == MODE_L3) {
			in_addr_t gw;
			iproute_get(ses->router, &gw);
			if (gw)
908
				iproute_add(0, ses->siaddr, ses->yiaddr, gw, conf_proto, 32);
909
			else
910
				iproute_add(0, ses->siaddr, ses->router, gw, conf_proto, 32);
911 912
		}

913
		if (ipoe_nl_modify(ses->ifindex, ses->yiaddr, addr, NULL, NULL)) {
914
			ap_session_terminate(&ses->ses, TERM_NAS_ERROR, 1);
915 916
			return;
		}
K
Kozlov Dmitry 已提交
917
	}
D
Dmitry Kozlov 已提交
918

919 920 921 922 923 924
	if (!ses->ses.ipv4) {
		ses->ses.ipv4 = &ses->ipv4;
		ses->ipv4.owner = NULL;
		ses->ipv4.peer_addr = ses->yiaddr;
		ses->ipv4.addr = ses->siaddr;
	}
D
Dmitry Kozlov 已提交
925

926 927 928
	if (ses->ifindex == -1) {
		if (ses->serv->opt_ifcfg || (ses->serv->opt_mode == MODE_L2))
			ipoe_ifcfg_add(ses);
D
Dmitry Kozlov 已提交
929

930 931 932
		ipoe_nl_add_exclude(ses->yiaddr, 32);

		ses->ctrl.dont_ifcfg = 1;
933
	} else if (ses->ctrl.dont_ifcfg && ses->serv->opt_mode == MODE_L2)
934
		ipaddr_add(ses->ifindex, ses->siaddr, ses->mask);
D
Dmitry Kozlov 已提交
935

936 937
	if (ses->l4_redirect)
		ipoe_change_l4_redirect(ses, 0);
D
Dmitry Kozlov 已提交
938

939 940
	if (ses->serv->opt_mode == MODE_L2 && ses->serv->opt_ipv6 && sock6_fd != -1) {
		ses->ses.ipv6 = ipdb_get_ipv6(&ses->ses);
D
Dmitry Kozlov 已提交
941
		if (!ses->ses.ipv6)
942
			log_ppp_warn("ipoe: no free IPv6 address\n");
D
Dmitry Kozlov 已提交
943
		else if (!ses->ses.ipv6->peer_intf_id)
D
Dmitry Kozlov 已提交
944
			ses->ses.ipv6->peer_intf_id = htobe64(1);
945
	}
946

D
Dmitry Kozlov 已提交
947 948 949 950
	__sync_sub_and_fetch(&stat_starting, 1);
	__sync_add_and_fetch(&stat_active, 1);
	ses->started = 1;

K
Kozlov Dmitry 已提交
951 952
	ap_session_activate(&ses->ses);

953 954
	if (ses->dhcpv4_request) {
		if (ses->ses.state == AP_STATE_ACTIVE)
D
Dmitry Kozlov 已提交
955
			dhcpv4_send_reply(DHCPACK, ses->dhcpv4 ?: ses->serv->dhcpv4, ses->dhcpv4_request, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->renew_time, ses->dhcpv4_relay_reply);
956 957
		else
			dhcpv4_send_nak(ses->serv->dhcpv4, ses->dhcpv4_request);
K
Kozlov Dmitry 已提交
958

959 960 961
		dhcpv4_packet_free(ses->dhcpv4_request);
		ses->dhcpv4_request = NULL;
	}
D
Dmitry Kozlov 已提交
962

K
Kozlov Dmitry 已提交
963
	ses->timer.expire = ipoe_session_timeout;
964
	ses->timer.period = 0;
965
	ses->timer.expire_tv.tv_sec = ses->lease_time;
K
Kozlov Dmitry 已提交
966 967
	if (ses->timer.tpd)
		triton_timer_mod(&ses->timer, 0);
K
Kozlov Dmitry 已提交
968 969
}

970
static void ipoe_session_activate(struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
971
{
972
	struct ipoe_session *ses = container_of(triton_context_self(), typeof(*ses), ctx);
D
Dmitry Kozlov 已提交
973

974 975 976 977 978 979 980
	if (ses->ses.state == AP_STATE_ACTIVE) {
		ipoe_session_keepalive(pack);
		return;
	}

	if (ses->dhcpv4_request)
		dhcpv4_packet_free(ses->dhcpv4_request);
D
Dmitry Kozlov 已提交
981

982 983
	ses->dhcpv4_request = pack;

K
Kozlov Dmitry 已提交
984
	if (ses->serv->dhcpv4_relay)
985
		dhcpv4_relay_send(ses->serv->dhcpv4_relay, ses->dhcpv4_request, ses->relay_server_id, ses->serv->ifname, conf_agent_remote_id);
K
Kozlov Dmitry 已提交
986 987 988 989 990
	else
		__ipoe_session_activate(ses);
}

static void ipoe_session_keepalive(struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
991
{
K
Kozlov Dmitry 已提交
992 993 994 995
	struct ipoe_session *ses = container_of(triton_context_self(), typeof(*ses), ctx);

	if (ses->dhcpv4_request)
		dhcpv4_packet_free(ses->dhcpv4_request);
D
Dmitry Kozlov 已提交
996

K
Kozlov Dmitry 已提交
997 998
	ses->dhcpv4_request = pack;

K
Kozlov Dmitry 已提交
999 1000 1001 1002
	if (ses->timer.tpd)
		triton_timer_mod(&ses->timer, 0);

	ses->xid = ses->dhcpv4_request->hdr->xid;
D
Dmitry Kozlov 已提交
1003

1004
	if (/*ses->ses.state == AP_STATE_ACTIVE &&*/ ses->serv->dhcpv4_relay) {
1005
		dhcpv4_relay_send(ses->serv->dhcpv4_relay, ses->dhcpv4_request, ses->relay_server_id, ses->serv->ifname, conf_agent_remote_id);
K
Kozlov Dmitry 已提交
1006 1007
		return;
	}
K
Kozlov Dmitry 已提交
1008

K
Kozlov Dmitry 已提交
1009
	if (ses->ses.state == AP_STATE_ACTIVE) {
D
Dmitry Kozlov 已提交
1010
		dhcpv4_send_reply(DHCPACK, ses->dhcpv4 ?: ses->serv->dhcpv4, ses->dhcpv4_request, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->renew_time, ses->dhcpv4_relay_reply);
K
Kozlov Dmitry 已提交
1011
	} else
1012
		dhcpv4_send_nak(ses->dhcpv4 ?: ses->serv->dhcpv4, ses->dhcpv4_request);
K
Kozlov Dmitry 已提交
1013 1014 1015 1016

	dhcpv4_packet_free(ses->dhcpv4_request);
	ses->dhcpv4_request = NULL;
}
D
Dmitry Kozlov 已提交
1017

K
Kozlov Dmitry 已提交
1018 1019 1020 1021 1022 1023 1024 1025
static void ipoe_session_decline(struct dhcpv4_packet *pack)
{
	struct ipoe_session *ses = container_of(triton_context_self(), typeof(*ses), ctx);

	if (conf_verbose) {
		log_ppp_info2("recv ");
		dhcpv4_print_packet(pack, 0, log_ppp_info2);
	}
D
Dmitry Kozlov 已提交
1026

K
Kozlov Dmitry 已提交
1027 1028 1029 1030 1031
	if (pack->msg_type == DHCPDECLINE && ses->serv->dhcpv4_relay)
		dhcpv4_relay_send(ses->serv->dhcpv4_relay, pack, 0, ses->serv->ifname, conf_agent_remote_id);

	dhcpv4_packet_free(pack);

1032
	ap_session_terminate(&ses->ses, TERM_USER_REQUEST, 1);
K
Kozlov Dmitry 已提交
1033
}
K
Kozlov Dmitry 已提交
1034

K
Kozlov Dmitry 已提交
1035 1036 1037
static void ipoe_session_started(struct ap_session *s)
{
	struct ipoe_session *ses = container_of(s, typeof(*ses), ses);
D
Dmitry Kozlov 已提交
1038

1039
	log_ppp_info1("ipoe: session started\n");
K
Kozlov Dmitry 已提交
1040

K
Kozlov Dmitry 已提交
1041 1042
	if (ses->timer.tpd)
		triton_timer_mod(&ses->timer, 0);
D
Dmitry Kozlov 已提交
1043

1044 1045 1046 1047 1048 1049 1050 1051
	if (ses->ifindex != -1 && ses->xid) {
		ses->dhcpv4 = dhcpv4_create(ses->ctrl.ctx, ses->ses.ifname, "");
		if (!ses->dhcpv4) {
			//terminate
			return;
		}
		ses->dhcpv4->recv = ipoe_ses_recv_dhcpv4;
	}
K
Kozlov Dmitry 已提交
1052 1053 1054 1055
}

static void ipoe_session_free(struct ipoe_session *ses)
{
D
Dmitry Kozlov 已提交
1056 1057 1058 1059
	if (ses->started)
		__sync_sub_and_fetch(&stat_active, 1);
	else
		__sync_sub_and_fetch(&stat_starting, 1);
D
Dmitry Kozlov 已提交
1060

K
Kozlov Dmitry 已提交
1061 1062
	if (ses->timer.tpd)
		triton_timer_del(&ses->timer);
D
Dmitry Kozlov 已提交
1063

1064
	if (ses->l4_redirect_timer.tpd)
1065
		triton_timer_del(&ses->l4_redirect_timer);
K
Kozlov Dmitry 已提交
1066

K
Kozlov Dmitry 已提交
1067 1068
	if (ses->dhcpv4_request)
		dhcpv4_packet_free(ses->dhcpv4_request);
D
Dmitry Kozlov 已提交
1069

1070 1071
	if (ses->dhcpv4_relay_reply)
		dhcpv4_packet_free(ses->dhcpv4_relay_reply);
D
Dmitry Kozlov 已提交
1072

1073 1074
	if (ses->ctrl.called_station_id)
		_free(ses->ctrl.called_station_id);
D
Dmitry Kozlov 已提交
1075

1076 1077
	if (ses->ctrl.calling_station_id)
		_free(ses->ctrl.calling_station_id);
D
Dmitry Kozlov 已提交
1078

1079 1080
	if (ses->l4_redirect_ipset)
		_free(ses->l4_redirect_ipset);
1081

K
Kozlov Dmitry 已提交
1082
	triton_context_unregister(&ses->ctx);
D
Dmitry Kozlov 已提交
1083

K
Kozlov Dmitry 已提交
1084 1085
	if (ses->data)
		_free(ses->data);
D
Dmitry Kozlov 已提交
1086

K
Kozlov Dmitry 已提交
1087 1088 1089 1090 1091 1092
	mempool_free(ses);
}

static void ipoe_session_finished(struct ap_session *s)
{
	struct ipoe_session *ses = container_of(s, typeof(*ses), ses);
1093
	struct unit_cache *uc;
K
Kozlov Dmitry 已提交
1094

1095
	log_ppp_info1("ipoe: session finished\n");
K
Kozlov Dmitry 已提交
1096

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
	if (ses->ifindex != -1) {
		if (uc_size < conf_unit_cache && ipoe_nl_modify(ses->ifindex, 0, 0, "", NULL)) {
			uc = mempool_alloc(uc_pool);
			uc->ifindex = ses->ifindex;
			pthread_mutex_lock(&uc_lock);
			list_add_tail(&uc->entry, &uc_list);
			++uc_size;
			pthread_mutex_unlock(&uc_lock);
		} else
			ipoe_nl_delete(ses->ifindex);
1107 1108
	} else
		ipoe_nl_del_exclude(ses->yiaddr);
1109

1110
	if (ses->dhcp_addr)
1111
		dhcpv4_put_ip(ses->serv->dhcpv4, ses->yiaddr);
1112

K
Kozlov Dmitry 已提交
1113
	if (ses->relay_addr && ses->serv->dhcpv4_relay)
1114
		dhcpv4_relay_send_release(ses->serv->dhcpv4_relay, ses->hwaddr, ses->xid, ses->yiaddr, ses->client_id, ses->relay_agent, ses->serv->ifname, conf_agent_remote_id);
1115

1116
	if (ses->ifcfg)
D
Dmitry Kozlov 已提交
1117
		ipoe_ifcfg_del(ses, 1);
D
Dmitry Kozlov 已提交
1118

1119 1120
	if (ses->dhcpv4)
		dhcpv4_free(ses->dhcpv4);
1121 1122

	triton_event_fire(EV_CTRL_FINISHED, s);
D
Dmitry Kozlov 已提交
1123

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
	if (s->ifindex == ses->serv->ifindex && strcmp(s->ifname, ses->serv->ifname)) {
		struct ifreq ifr;

		strcpy(ifr.ifr_name, s->ifname);

		ioctl(sock_fd, SIOCGIFFLAGS, &ifr);
		ifr.ifr_flags &= ~IFF_UP;
		ioctl(sock_fd, SIOCSIFFLAGS, &ifr);

		strcpy(ifr.ifr_newname, ses->serv->ifname);
		ioctl(sock_fd, SIOCSIFNAME, &ifr);

		strcpy(ifr.ifr_name, ses->serv->ifname);
		ifr.ifr_flags |= IFF_UP;
		ioctl(sock_fd, SIOCSIFFLAGS, &ifr);
	}

	pthread_mutex_lock(&ses->serv->lock);
	list_del(&ses->entry);
	if  ((ses->serv->vid || ses->serv->need_close) && list_empty(&ses->serv->sessions))
		triton_context_call(&ses->serv->ctx, (triton_event_func)ipoe_serv_release, ses->serv);
	pthread_mutex_unlock(&ses->serv->lock);

K
Kozlov Dmitry 已提交
1147 1148 1149
	triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_free, ses);
}

1150
static void ipoe_session_terminated(struct ipoe_session *ses)
K
Kozlov Dmitry 已提交
1151
{
K
Kozlov Dmitry 已提交
1152
	if (ses->l4_redirect_set)
1153 1154
		ipoe_change_l4_redirect(ses, 1);

1155 1156 1157
	if (!ses->serv->opt_shared)
		ses->ctrl.dont_ifcfg = 1;

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
	ap_session_finished(&ses->ses);
}

static void ipoe_session_terminated_pkt(struct dhcpv4_packet *pack)
{
	struct ipoe_session *ses = container_of(triton_context_self(), typeof(*ses), ctx);

	if (conf_verbose) {
		log_ppp_info2("recv ");
		dhcpv4_print_packet(pack, 0, log_ppp_info2);
	}

	dhcpv4_send_nak(ses->serv->dhcpv4, pack);

	dhcpv4_packet_free(pack);

	ipoe_session_terminated(ses);
}

1177
static int ipoe_session_terminate(struct ap_session *s, int hard)
1178 1179 1180
{
	struct ipoe_session *ses = container_of(s, typeof(*ses), ses);

1181
	if (hard || !conf_soft_terminate || ses->UP)
1182 1183 1184
		ipoe_session_terminated(ses);
	else
		ses->terminate = 1;
1185 1186

	return 0;
K
Kozlov Dmitry 已提交
1187 1188 1189 1190 1191 1192
}


static void ipoe_session_close(struct triton_context_t *ctx)
{
	struct ipoe_session *ses = container_of(ctx, typeof(*ses), ctx);
D
Dmitry Kozlov 已提交
1193

K
Kozlov Dmitry 已提交
1194 1195 1196 1197 1198 1199
	if (ses->ses.state)
		ap_session_terminate(&ses->ses, TERM_ADMIN_RESET, 1);
	else
		ipoe_session_finished(&ses->ses);
}

1200
static struct ipoe_session *ipoe_session_create_dhcpv4(struct ipoe_serv *serv, struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
1201 1202 1203
{
	struct ipoe_session *ses;
	int dlen = 0;
1204
	uint8_t *ptr = NULL;
D
Dmitry Kozlov 已提交
1205

1206 1207
	ses = ipoe_session_alloc();
	if (!ses)
K
Kozlov Dmitry 已提交
1208 1209 1210 1211
		return NULL;

	ses->serv = serv;
	ses->dhcpv4_request = pack;
1212 1213 1214

	if (!serv->opt_shared)
		strncpy(ses->ses.ifname, serv->ifname, AP_IFNAME_LEN);
D
Dmitry Kozlov 已提交
1215

K
Kozlov Dmitry 已提交
1216 1217 1218
	ses->xid = pack->hdr->xid;
	memcpy(ses->hwaddr, pack->hdr->chaddr, 6);
	ses->giaddr = pack->hdr->giaddr;
K
Kozlov Dmitry 已提交
1219
	ses->lease_time = conf_lease_time;
D
Dmitry Kozlov 已提交
1220
	ses->renew_time = conf_renew_time;
K
Kozlov Dmitry 已提交
1221 1222

	if (pack->client_id)
K
Kozlov Dmitry 已提交
1223
		dlen += sizeof(struct dhcpv4_option) + pack->client_id->len;
D
Dmitry Kozlov 已提交
1224

K
Kozlov Dmitry 已提交
1225 1226
	if (pack->relay_agent)
		dlen += sizeof(struct dhcpv4_option) + pack->relay_agent->len;
D
Dmitry Kozlov 已提交
1227

K
Kozlov Dmitry 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
	if (dlen) {
		ses->data = _malloc(dlen);
		if (!ses->data) {
			log_emerg("out of memery\n");
			mempool_free(ses);
			return NULL;
		}
		ptr = ses->data;
	}

	if (pack->client_id) {
K
Kozlov Dmitry 已提交
1239
		ses->client_id = (struct dhcpv4_option *)ptr;
K
Kozlov Dmitry 已提交
1240
		ses->client_id->len = pack->client_id->len;
1241
		ses->client_id->data = (uint8_t *)(ses->client_id + 1);
K
Kozlov Dmitry 已提交
1242
		memcpy(ses->client_id->data, pack->client_id->data, pack->client_id->len);
K
Kozlov Dmitry 已提交
1243 1244
		ptr += sizeof(struct dhcpv4_option) + pack->client_id->len;
	}
D
Dmitry Kozlov 已提交
1245

K
Kozlov Dmitry 已提交
1246 1247 1248
	if (pack->relay_agent) {
		ses->relay_agent = (struct dhcpv4_option *)ptr;
		ses->relay_agent->len = pack->relay_agent->len;
1249
		ses->relay_agent->data = (uint8_t *)(ses->relay_agent + 1);
K
Kozlov Dmitry 已提交
1250 1251 1252 1253
		memcpy(ses->relay_agent->data, pack->relay_agent->data, pack->relay_agent->len);
		ptr += sizeof(struct dhcpv4_option) + pack->relay_agent->len;
		if (dhcpv4_parse_opt82(ses->relay_agent, &ses->agent_circuit_id, &ses->agent_remote_id))
			ses->relay_agent = NULL;
K
Kozlov Dmitry 已提交
1254 1255
	}

1256
	ses->ctrl.dont_ifcfg = 1;
D
Dmitry Kozlov 已提交
1257

K
Kozlov Dmitry 已提交
1258
	ses->ctrl.calling_station_id = _malloc(19);
1259
	ses->ctrl.called_station_id = _strdup(serv->ifname);
D
Dmitry Kozlov 已提交
1260

K
Kozlov Dmitry 已提交
1261 1262 1263
	ptr = ses->hwaddr;
	sprintf(ses->ctrl.calling_station_id, "%02x:%02x:%02x:%02x:%02x:%02x",
		ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
D
Dmitry Kozlov 已提交
1264

K
Kozlov Dmitry 已提交
1265 1266 1267
	ses->ses.ctrl = &ses->ctrl;
	ses->ses.chan_name = ses->ctrl.calling_station_id;

1268 1269 1270
	if (conf_ip_pool)
		ses->ses.ipv4_pool_name = _strdup(conf_ip_pool);

K
Kozlov Dmitry 已提交
1271 1272 1273 1274 1275 1276 1277
	triton_context_register(&ses->ctx, &ses->ses);

	triton_context_wakeup(&ses->ctx);

	//pthread_mutex_lock(&serv->lock);
	list_add_tail(&ses->entry, &serv->sessions);
	//pthread_mutex_unlock(&serv->lock);
D
Dmitry Kozlov 已提交
1278

D
Dmitry Kozlov 已提交
1279 1280
	if (serv->timer.tpd)
		triton_timer_del(&serv->timer);
D
Dmitry Kozlov 已提交
1281

1282
	dhcpv4_packet_ref(pack);
K
Kozlov Dmitry 已提交
1283 1284 1285 1286 1287 1288

	triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_start, ses);

	return ses;
}

1289
static void __ipoe_session_terminate(struct ap_session *s)
K
Kozlov Dmitry 已提交
1290
{
1291 1292 1293 1294 1295 1296
	struct ipoe_session *ses = container_of(s, typeof(*ses), ses);

	if (ses->terminate)
		ipoe_session_terminated(ses);
	else
		ap_session_terminate(s, TERM_USER_REQUEST, 1);
K
Kozlov Dmitry 已提交
1297 1298
}

1299 1300 1301
static void ipoe_ses_recv_dhcpv4(struct dhcpv4_serv *dhcpv4, struct dhcpv4_packet *pack)
{
	struct ipoe_session *ses = container_of(dhcpv4->ctx, typeof(*ses), ctx);
K
Kozlov Dmitry 已提交
1302 1303 1304
	int opt82_match;
	uint8_t *agent_circuit_id = NULL;
	uint8_t *agent_remote_id = NULL;
1305 1306 1307

	if (ap_shutdown)
		return;
D
Dmitry Kozlov 已提交
1308

1309 1310
	if (conf_verbose) {
		log_ppp_info2("recv ");
1311
		dhcpv4_print_packet(pack, 0, log_ppp_info2);
1312
	}
K
Kozlov Dmitry 已提交
1313

1314 1315 1316 1317 1318 1319 1320
	if (ses->terminate) {
		if (pack->msg_type != DHCPDISCOVER)
			dhcpv4_send_nak(dhcpv4, pack);
		triton_context_call(ses->ctrl.ctx, (triton_event_func)ipoe_session_terminated, ses);
		return;
	}

K
Kozlov Dmitry 已提交
1321 1322 1323 1324 1325
	if (pack->relay_agent && dhcpv4_parse_opt82(pack->relay_agent, &agent_circuit_id, &agent_remote_id)) {
		agent_circuit_id = NULL;
		agent_remote_id = NULL;
	}

1326
	opt82_match = pack->relay_agent != NULL;
D
Dmitry Kozlov 已提交
1327

1328
	if (agent_circuit_id && !ses->agent_circuit_id)
K
Kozlov Dmitry 已提交
1329
		opt82_match = 0;
D
Dmitry Kozlov 已提交
1330

K
Kozlov Dmitry 已提交
1331 1332
	if (opt82_match && agent_remote_id && !ses->agent_remote_id)
		opt82_match = 0;
D
Dmitry Kozlov 已提交
1333

K
Kozlov Dmitry 已提交
1334 1335
	if (opt82_match && !agent_circuit_id && ses->agent_circuit_id)
		opt82_match = 0;
D
Dmitry Kozlov 已提交
1336

K
Kozlov Dmitry 已提交
1337 1338
	if (opt82_match && !agent_remote_id && ses->agent_remote_id)
		opt82_match = 0;
D
Dmitry Kozlov 已提交
1339

K
Kozlov Dmitry 已提交
1340 1341 1342
	if (opt82_match && agent_circuit_id) {
		if (*agent_circuit_id != *ses->agent_circuit_id)
			opt82_match = 0;
D
Dmitry Kozlov 已提交
1343

K
Kozlov Dmitry 已提交
1344 1345 1346
		if (memcmp(agent_circuit_id + 1, ses->agent_circuit_id + 1, *agent_circuit_id))
			opt82_match = 0;
	}
D
Dmitry Kozlov 已提交
1347

K
Kozlov Dmitry 已提交
1348 1349 1350 1351 1352 1353 1354 1355
	if (opt82_match && agent_remote_id) {
		if (*agent_remote_id != *ses->agent_remote_id)
			opt82_match = 0;

		if (memcmp(agent_remote_id + 1, ses->agent_remote_id + 1, *agent_remote_id))
			opt82_match = 0;
	}

1356
	if (conf_check_mac_change && pack->relay_agent && !opt82_match) {
K
Kozlov Dmitry 已提交
1357 1358 1359 1360 1361 1362
		log_ppp_info2("port change detected\n");
		if (pack->msg_type == DHCPREQUEST)
			dhcpv4_send_nak(dhcpv4, pack);
		triton_context_call(ses->ctrl.ctx, (triton_event_func)__ipoe_session_terminate, &ses->ses);
		return;
	}
D
Dmitry Kozlov 已提交
1363

1364 1365 1366 1367
	if (pack->msg_type == DHCPDISCOVER) {
		if (ses->yiaddr) {
			if (ses->serv->dhcpv4_relay) {
				dhcpv4_packet_ref(pack);
K
Kozlov Dmitry 已提交
1368
				ipoe_session_keepalive(pack);
1369
			} else
D
Dmitry Kozlov 已提交
1370
				dhcpv4_send_reply(DHCPOFFER, dhcpv4, pack, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->renew_time, ses->dhcpv4_relay_reply);
1371 1372
		}
	} else if (pack->msg_type == DHCPREQUEST) {
D
Dmitry Kozlov 已提交
1373
		ses->xid = pack->hdr->xid;
1374 1375 1376 1377 1378 1379 1380 1381 1382
		if (pack->hdr->ciaddr == ses->yiaddr && pack->hdr->xid != ses->xid)
			ses->xid = pack->hdr->xid;
		if ((pack->server_id && (pack->server_id != ses->siaddr || pack->request_ip != ses->yiaddr)) ||
			(pack->hdr->ciaddr && (pack->hdr->xid != ses->xid || pack->hdr->ciaddr != ses->yiaddr))) {

			if (pack->server_id == ses->siaddr)
				dhcpv4_send_nak(dhcpv4, pack);
			else if (ses->serv->dhcpv4_relay)
				dhcpv4_relay_send(ses->serv->dhcpv4_relay, pack, 0, ses->serv->ifname, conf_agent_remote_id);
D
Dmitry Kozlov 已提交
1383

K
Kozlov Dmitry 已提交
1384
			triton_context_call(ses->ctrl.ctx, (triton_event_func)__ipoe_session_terminate, &ses->ses);
1385 1386 1387 1388 1389 1390
		} else {
			dhcpv4_packet_ref(pack);
			ipoe_session_keepalive(pack);
		}
	} else if (pack->msg_type == DHCPDECLINE || pack->msg_type == DHCPRELEASE) {
		dhcpv4_packet_ref(pack);
K
Kozlov Dmitry 已提交
1391
		triton_context_call(ses->ctrl.ctx, (triton_event_func)ipoe_session_decline, pack);
1392 1393 1394
	}
}

1395 1396 1397 1398 1399 1400 1401 1402
static void ipoe_ses_recv_dhcpv4_discover(struct dhcpv4_packet *pack)
{
	struct ipoe_session *ses = container_of(triton_context_self(), typeof(*ses), ctx);

	if (conf_verbose) {
		log_ppp_info2("recv ");
		dhcpv4_print_packet(pack, 0, log_ppp_info2);
	}
D
Dmitry Kozlov 已提交
1403

1404
	if (ses->yiaddr)
D
Dmitry Kozlov 已提交
1405
		dhcpv4_send_reply(DHCPOFFER, ses->dhcpv4 ?: ses->serv->dhcpv4, pack, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->renew_time, ses->dhcpv4_relay_reply);
D
Dmitry Kozlov 已提交
1406

1407 1408 1409 1410 1411 1412 1413 1414
	dhcpv4_packet_free(pack);
}

static void ipoe_ses_recv_dhcpv4_request(struct dhcpv4_packet *pack)
{
	struct ipoe_session *ses = container_of(triton_context_self(), typeof(*ses), ctx);

	ses->xid = pack->hdr->xid;
D
Dmitry Kozlov 已提交
1415

1416 1417 1418 1419
	if (conf_verbose) {
		log_ppp_info2("recv ");
		dhcpv4_print_packet(pack, 0, log_ppp_info2);
	}
D
Dmitry Kozlov 已提交
1420

1421 1422 1423 1424 1425 1426
	if ((pack->server_id && (pack->server_id != ses->siaddr || pack->request_ip != ses->yiaddr)) ||
		(pack->hdr->ciaddr && (pack->hdr->ciaddr != ses->yiaddr))) {

		if (pack->server_id == ses->siaddr)
			dhcpv4_send_nak(ses->serv->dhcpv4, pack);

1427
		ap_session_terminate(&ses->ses, TERM_USER_REQUEST, 1);
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440

		dhcpv4_packet_free(pack);
		return;
	}

	if (ses->ses.state == AP_STATE_STARTING && ses->yiaddr)
		ipoe_session_activate(pack);
	else if (ses->ses.state == AP_STATE_ACTIVE)
		ipoe_session_keepalive(pack);
	else
		dhcpv4_packet_free(pack);
}

D
Dmitry Kozlov 已提交
1441 1442 1443 1444 1445 1446 1447 1448
static void ipoe_serv_disc_timer(struct triton_timer_t *t)
{
	struct ipoe_serv *serv = container_of(t, typeof(*serv), disc_timer);
	struct disc_item *d;
	struct timespec ts;
	int delay, offer_delay;

	clock_gettime(CLOCK_MONOTONIC, &ts);
D
Dmitry Kozlov 已提交
1449 1450

	while (!list_empty(&serv->disc_list)) {
D
Dmitry Kozlov 已提交
1451
	  d = list_entry(serv->disc_list.next, typeof(*d), entry);
D
Dmitry Kozlov 已提交
1452

D
Dmitry Kozlov 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
		delay = (ts.tv_sec - d->ts.tv_sec) * 1000 + (ts.tv_nsec - d->ts.tv_nsec) / 1000000;
		offer_delay = get_offer_delay();

		if (delay < offer_delay - 1) {
			delay = offer_delay - delay;
			t->expire_tv.tv_sec = delay / 1000;
			t->expire_tv.tv_usec = (delay % 1000) * 1000;
			triton_timer_mod(t, 0);
			return;
		}
D
Dmitry Kozlov 已提交
1463

D
Dmitry Kozlov 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
		__ipoe_recv_dhcpv4(serv->dhcpv4, d->pack, 1);

		list_del(&d->entry);
		dhcpv4_packet_free(d->pack);
		mempool_free(d);

		__sync_sub_and_fetch(&stat_delayed_offer, 1);
	}

	triton_timer_del(t);
}

static void ipoe_serv_add_disc(struct ipoe_serv *serv, struct dhcpv4_packet *pack, int offer_delay)
{
	struct disc_item *d = mempool_alloc(disc_item_pool);

	if (!d)
		return;
D
Dmitry Kozlov 已提交
1482

D
Dmitry Kozlov 已提交
1483
	__sync_add_and_fetch(&stat_delayed_offer, 1);
D
Dmitry Kozlov 已提交
1484

D
Dmitry Kozlov 已提交
1485 1486 1487 1488
	dhcpv4_packet_ref(pack);
	d->pack = pack;
	clock_gettime(CLOCK_MONOTONIC, &d->ts);
	list_add_tail(&d->entry, &serv->disc_list);
D
Dmitry Kozlov 已提交
1489

D
Dmitry Kozlov 已提交
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
	if (!serv->disc_timer.tpd) {
		serv->disc_timer.expire_tv.tv_sec = offer_delay / 1000;
		serv->disc_timer.expire_tv.tv_usec = (offer_delay % 1000) * 1000;
		triton_timer_add(&serv->ctx, &serv->disc_timer, 0);
	}
}

static void ipoe_serv_check_disc(struct ipoe_serv *serv, struct dhcpv4_packet *pack)
{
	struct disc_item *d;

	list_for_each_entry(d, &serv->disc_list, entry) {
		if (d->pack->hdr->xid != pack->hdr->xid)
			continue;

		if (memcmp(d->pack->hdr->chaddr, pack->hdr->chaddr, ETH_ALEN))
			continue;
D
Dmitry Kozlov 已提交
1507

D
Dmitry Kozlov 已提交
1508 1509 1510
		list_del(&d->entry);
		dhcpv4_packet_free(d->pack);
		mempool_free(d);
D
Dmitry Kozlov 已提交
1511

D
Dmitry Kozlov 已提交
1512
		__sync_sub_and_fetch(&stat_delayed_offer, 1);
D
Dmitry Kozlov 已提交
1513

D
Dmitry Kozlov 已提交
1514 1515 1516 1517
		break;
	}
}

1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
static int ipoe_serv_request_check(struct ipoe_serv *serv, uint32_t xid)
{
	struct request_item *r;
	struct list_head *pos, *n;
	struct timespec ts;

	clock_gettime(CLOCK_MONOTONIC, &ts);

	list_for_each_safe(pos, n, &serv->req_list) {
		r = list_entry(pos, typeof(*r), entry);
		if (r->xid == xid) {
			if (++r->cnt == conf_max_request) {
				list_del(&r->entry);
				mempool_free(r);
				return 1;
			}

			r->expire = ts.tv_sec + 30;
			return 0;
		}

		if (ts.tv_sec > r->expire) {
			list_del(&r->entry);
			mempool_free(r);
		}
	}
D
Dmitry Kozlov 已提交
1544

1545 1546 1547 1548 1549 1550 1551 1552 1553
	r = mempool_alloc(req_item_pool);
	r->xid = xid;
	r->expire = ts.tv_sec + 30;
	r->cnt = 0;
	list_add_tail(&r->entry, &serv->req_list);

	return 0;
}

1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
static void port_change_detected(struct dhcpv4_packet *pack)
{
	struct ipoe_session *ses = container_of(triton_context_self(), typeof(*ses), ctx);

	if (conf_verbose) {
		log_ppp_info2("recv ");
		dhcpv4_print_packet(pack, 0, log_ppp_info2);
	}

	dhcpv4_packet_free(pack);

	log_ppp_warn("port change detected\n");

1567
	ap_session_terminate(&ses->ses, TERM_USER_REQUEST, 1);
1568
}
D
Dmitry Kozlov 已提交
1569

1570 1571 1572 1573 1574 1575 1576 1577
static void mac_change_detected(struct dhcpv4_packet *pack)
{
	struct ipoe_session *ses = container_of(triton_context_self(), typeof(*ses), ctx);

	if (conf_verbose) {
		log_ppp_info2("recv ");
		dhcpv4_print_packet(pack, 0, log_ppp_info2);
	}
D
Dmitry Kozlov 已提交
1578

1579
	dhcpv4_packet_free(pack);
D
Dmitry Kozlov 已提交
1580

1581
	log_ppp_warn("mac change detected\n");
D
Dmitry Kozlov 已提交
1582

1583
	ap_session_terminate(&ses->ses, TERM_USER_REQUEST, 1);
1584 1585
}

D
Dmitry Kozlov 已提交
1586
static void __ipoe_recv_dhcpv4(struct dhcpv4_serv *dhcpv4, struct dhcpv4_packet *pack, int force)
K
Kozlov Dmitry 已提交
1587 1588
{
	struct ipoe_serv *serv = container_of(dhcpv4->ctx, typeof(*serv), ctx);
K
Kozlov Dmitry 已提交
1589
	struct ipoe_session *ses, *opt82_ses;
D
Dmitry Kozlov 已提交
1590
	int offer_delay;
K
Kozlov Dmitry 已提交
1591 1592
	//struct dhcpv4_packet *reply;

D
Dmitry Kozlov 已提交
1593 1594 1595
	if (serv->timer.tpd)
		triton_timer_mod(&serv->timer, 0);

1596 1597
	if (ap_shutdown)
		return;
D
Dmitry Kozlov 已提交
1598

1599
	if (connlimit_loaded && pack->msg_type == DHCPDISCOVER && connlimit_check(serv->opt_shared ? cl_key_from_mac(pack->hdr->chaddr) : serv->ifindex))
1600
		return;
1601

K
Kozlov Dmitry 已提交
1602 1603
	pthread_mutex_lock(&serv->lock);
	if (pack->msg_type == DHCPDISCOVER) {
K
Kozlov Dmitry 已提交
1604
		ses = ipoe_session_lookup(serv, pack, &opt82_ses);
K
Kozlov Dmitry 已提交
1605
		if (!ses) {
1606 1607
			if (serv->opt_shared == 0)
				ipoe_drop_sessions(serv, NULL);
K
Kozlov Dmitry 已提交
1608
			else if (opt82_ses) {
1609 1610
				dhcpv4_packet_ref(pack);
				triton_context_call(&opt82_ses->ctx, (triton_event_func)mac_change_detected, pack);
K
Kozlov Dmitry 已提交
1611
			}
1612

D
Dmitry Kozlov 已提交
1613 1614 1615 1616 1617 1618 1619 1620 1621
			offer_delay = get_offer_delay();
			if (offer_delay == -1)
				goto out;

			if (offer_delay && !force) {
				ipoe_serv_add_disc(serv, pack, offer_delay);
				goto out;
			}

1622
			ses = ipoe_session_create_dhcpv4(serv, pack);
K
Kozlov Dmitry 已提交
1623
		}	else {
1624 1625 1626 1627 1628
			if (ses->terminate) {
				triton_context_call(ses->ctrl.ctx, (triton_event_func)ipoe_session_terminated, ses);
				goto out;
			}

1629
			if (conf_check_mac_change && ((opt82_ses && ses != opt82_ses) || (!opt82_ses && pack->relay_agent))) {
1630 1631 1632 1633
				dhcpv4_packet_ref(pack);
				triton_context_call(&ses->ctx, (triton_event_func)port_change_detected, pack);
				if (opt82_ses)
					triton_context_call(&opt82_ses->ctx, (triton_event_func)__ipoe_session_terminate, &opt82_ses->ses);
K
Kozlov Dmitry 已提交
1634 1635
				goto out;
			}
D
Dmitry Kozlov 已提交
1636

1637 1638
			dhcpv4_packet_ref(pack);
			triton_context_call(&ses->ctx, (triton_event_func)ipoe_ses_recv_dhcpv4_discover, pack);
K
Kozlov Dmitry 已提交
1639 1640
		}
	} else if (pack->msg_type == DHCPREQUEST) {
D
Dmitry Kozlov 已提交
1641
		ipoe_serv_check_disc(serv, pack);
D
Dmitry Kozlov 已提交
1642

K
Kozlov Dmitry 已提交
1643
		ses = ipoe_session_lookup(serv, pack, &opt82_ses);
K
Kozlov Dmitry 已提交
1644 1645 1646

		if (!ses) {
			if (conf_verbose) {
1647
				log_debug("%s: recv ", serv->ifname);
D
Dmitry Kozlov 已提交
1648
				dhcpv4_print_packet(pack, 0, log_debug);
K
Kozlov Dmitry 已提交
1649
			}
D
Dmitry Kozlov 已提交
1650

1651 1652 1653 1654
			if (!pack->server_id)
				dhcpv4_send_nak(dhcpv4, pack);

			if (serv->opt_shared == 0)
1655
				ipoe_drop_sessions(serv, NULL);
1656
			else if (opt82_ses) {
1657 1658
				dhcpv4_packet_ref(pack);
				triton_context_call(&opt82_ses->ctx, (triton_event_func)mac_change_detected, pack);
1659 1660
			} else if (list_empty(&conf_offer_delay) || ipoe_serv_request_check(serv, pack->hdr->xid))
				dhcpv4_send_nak(dhcpv4, pack);
K
Kozlov Dmitry 已提交
1661
		} else {
1662 1663 1664 1665 1666 1667
			if (ses->terminate) {
				dhcpv4_packet_ref(pack);
				triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_terminated_pkt, pack);
				goto out;
			}

1668
			if (conf_check_mac_change && ((opt82_ses && ses != opt82_ses) || (!opt82_ses && pack->relay_agent))) {
1669 1670 1671 1672 1673
				dhcpv4_packet_ref(pack);
				triton_context_call(&ses->ctx, (triton_event_func)port_change_detected, pack);
				if (opt82_ses)
					triton_context_call(&opt82_ses->ctx, (triton_event_func)__ipoe_session_terminate, &opt82_ses->ses);
				goto out;
K
Kozlov Dmitry 已提交
1674
			}
D
Dmitry Kozlov 已提交
1675

1676 1677 1678 1679 1680
			if (serv->opt_shared == 0)
				ipoe_drop_sessions(serv, ses);

			dhcpv4_packet_ref(pack);
			triton_context_call(&ses->ctx, (triton_event_func)ipoe_ses_recv_dhcpv4_request, pack);
K
Kozlov Dmitry 已提交
1681 1682
		}
	} else if (pack->msg_type == DHCPDECLINE || pack->msg_type == DHCPRELEASE) {
K
Kozlov Dmitry 已提交
1683
		ses = ipoe_session_lookup(serv, pack, &opt82_ses);
K
Kozlov Dmitry 已提交
1684
		if (ses) {
D
Dmitry Kozlov 已提交
1685
			ses->xid = pack->hdr->xid;
K
Kozlov Dmitry 已提交
1686 1687
			dhcpv4_packet_ref(pack);
			triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_decline, pack);
K
Kozlov Dmitry 已提交
1688
		}
K
Kozlov Dmitry 已提交
1689
	}
K
Kozlov Dmitry 已提交
1690 1691

out:
K
Kozlov Dmitry 已提交
1692 1693 1694
	pthread_mutex_unlock(&serv->lock);
}

D
Dmitry Kozlov 已提交
1695 1696 1697 1698 1699
static void ipoe_recv_dhcpv4(struct dhcpv4_serv *dhcpv4, struct dhcpv4_packet *pack)
{
	__ipoe_recv_dhcpv4(dhcpv4, pack, 0);
}

K
Kozlov Dmitry 已提交
1700 1701 1702 1703 1704 1705 1706 1707 1708
static int parse_dhcpv4_mask(uint32_t mask)
{
	int i;

	for (i = 31; i >= 0 && (mask & (1 << i)); i--);

	return 32 - (i + 1);
}

1709
static void ipoe_ses_recv_dhcpv4_relay(struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
1710
{
1711
	struct ipoe_session *ses = container_of(triton_context_self(), typeof(*ses), ctx);
K
Kozlov Dmitry 已提交
1712 1713
	struct dhcpv4_option *opt;

1714 1715
	if (ses->dhcpv4_relay_reply)
		dhcpv4_packet_free(ses->dhcpv4_relay_reply);
D
Dmitry Kozlov 已提交
1716

1717 1718 1719 1720 1721
	if (!ses->dhcpv4_request) {
		ses->dhcpv4_relay_reply = NULL;
		return;
	}

1722 1723
	ses->dhcpv4_relay_reply = pack;

K
Kozlov Dmitry 已提交
1724 1725 1726 1727 1728
	if (conf_verbose) {
		log_ppp_info2("recv ");
		dhcpv4_print_packet(pack, 1, log_ppp_info2);
	}

1729 1730 1731
	opt = dhcpv4_packet_find_opt(pack, 51);
	if (opt)
		ses->lease_time = ntohl(*(uint32_t *)opt->data);
K
Kozlov Dmitry 已提交
1732

D
Dmitry Kozlov 已提交
1733 1734 1735 1736
	opt = dhcpv4_packet_find_opt(pack, 58);
	if (opt)
		ses->renew_time = ntohl(*(uint32_t *)opt->data);

1737 1738 1739
	opt = dhcpv4_packet_find_opt(pack, 1);
	if (opt)
		ses->mask = parse_dhcpv4_mask(ntohl(*(uint32_t *)opt->data));
K
Kozlov Dmitry 已提交
1740

1741 1742 1743 1744
	opt = dhcpv4_packet_find_opt(pack, 3);
	if (opt)
		ses->router = *(uint32_t *)opt->data;

1745 1746 1747
	if (pack->msg_type == DHCPOFFER) {
		if (ses->ses.state == AP_STATE_STARTING) {
			triton_timer_del(&ses->timer);
K
Kozlov Dmitry 已提交
1748

1749 1750 1751 1752 1753 1754 1755 1756 1757
			ses->relay_server_id = pack->server_id;

			if (!ses->yiaddr) {
				ses->yiaddr = pack->hdr->yiaddr;
				ses->relay_addr = 1;
			}

			__ipoe_session_start(ses);
		} else
D
Dmitry Kozlov 已提交
1758
			dhcpv4_send_reply(DHCPOFFER, ses->dhcpv4 ?: ses->serv->dhcpv4, ses->dhcpv4_request, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->renew_time, ses->dhcpv4_relay_reply);
K
Kozlov Dmitry 已提交
1759 1760 1761 1762
	} else if (pack->msg_type == DHCPACK) {
		if (ses->ses.state == AP_STATE_STARTING)
			__ipoe_session_activate(ses);
		else
D
Dmitry Kozlov 已提交
1763
			dhcpv4_send_reply(DHCPACK, ses->dhcpv4 ?: ses->serv->dhcpv4, ses->dhcpv4_request, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->renew_time, ses->dhcpv4_relay_reply);
K
Kozlov Dmitry 已提交
1764 1765

	} else if (pack->msg_type == DHCPNAK) {
1766
		dhcpv4_send_nak(ses->dhcpv4 ?: ses->serv->dhcpv4, ses->dhcpv4_request);
1767
		ap_session_terminate(&ses->ses, TERM_NAS_REQUEST, 1);
K
Kozlov Dmitry 已提交
1768 1769
		return;
	}
D
Dmitry Kozlov 已提交
1770

K
Kozlov Dmitry 已提交
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
	dhcpv4_packet_free(ses->dhcpv4_relay_reply);
	ses->dhcpv4_relay_reply = NULL;
}

static void ipoe_recv_dhcpv4_relay(struct dhcpv4_packet *pack)
{
	struct ipoe_serv *serv = container_of(triton_context_self(), typeof(*serv), ctx);
	struct ipoe_session *ses;
	int found = 0;
	//struct dhcpv4_packet *reply;

	if (ap_shutdown) {
K
Kozlov Dmitry 已提交
1783
		dhcpv4_packet_free(pack);
K
Kozlov Dmitry 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
		return;
	}

	pthread_mutex_lock(&serv->lock);
	list_for_each_entry(ses, &serv->sessions, entry) {
		if (ses->xid != pack->hdr->xid)
			continue;
		if (memcmp(ses->hwaddr, pack->hdr->chaddr, 6))
			continue;

		found = 1;
		break;
K
Kozlov Dmitry 已提交
1796
	}
D
Dmitry Kozlov 已提交
1797

1798 1799
	if (found) {
		triton_context_call(&ses->ctx, (triton_event_func)ipoe_ses_recv_dhcpv4_relay, pack);
K
Kozlov Dmitry 已提交
1800 1801 1802
	} else
		dhcpv4_packet_free(pack);

K
Kozlov Dmitry 已提交
1803 1804 1805
	pthread_mutex_unlock(&serv->lock);
}

K
Kozlov Dmitry 已提交
1806

1807 1808 1809
static struct ipoe_session *ipoe_session_create_up(struct ipoe_serv *serv, struct ethhdr *eth, struct iphdr *iph)
{
	struct ipoe_session *ses;
1810
	uint8_t *hwaddr = eth->h_source;
1811

1812 1813
	if (ap_shutdown)
		return NULL;
D
Dmitry Kozlov 已提交
1814

1815 1816
	if (l4_redirect_list_check(iph->saddr))
		return NULL;
D
Dmitry Kozlov 已提交
1817

1818 1819
	ses = ipoe_session_alloc();
	if (!ses)
1820 1821 1822 1823
		return NULL;

	ses->serv = serv;
	memcpy(ses->hwaddr, eth->h_source, 6);
1824
	ses->yiaddr = iph->saddr;
1825
	ses->UP = 1;
K
Kozlov Dmitry 已提交
1826

1827 1828 1829
	if (!serv->opt_shared)
		strncpy(ses->ses.ifname, serv->ifname, AP_IFNAME_LEN);

1830
	ses->ctrl.called_station_id = _strdup(serv->ifname);
1831

1832 1833 1834 1835 1836 1837 1838 1839
	if (conf_calling_sid == SID_MAC) {
		ses->ctrl.calling_station_id = _malloc(19);
		sprintf(ses->ctrl.calling_station_id, "%02x:%02x:%02x:%02x:%02x:%02x",
				hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
	} else {
		ses->ctrl.calling_station_id = _malloc(17);
		u_inet_ntoa(iph->saddr, ses->ctrl.calling_station_id);
	}
1840

1841 1842 1843 1844 1845 1846
	if (ses->serv->opt_username == USERNAME_IFNAME)
		ses->username = _strdup(serv->ifname);
	else {
		ses->username = _malloc(17);
		u_inet_ntoa(iph->saddr, ses->username);
	}
D
Dmitry Kozlov 已提交
1847

1848
	ses->ses.chan_name = ses->ctrl.calling_station_id;
D
Dmitry Kozlov 已提交
1849

1850 1851
	if (conf_ip_pool)
		ses->ses.ipv4_pool_name = _strdup(conf_ip_pool);
D
Dmitry Kozlov 已提交
1852

1853 1854 1855 1856 1857 1858 1859 1860
	triton_context_register(&ses->ctx, &ses->ses);

	triton_context_wakeup(&ses->ctx);

	//pthread_mutex_lock(&serv->lock);
	list_add_tail(&ses->entry, &serv->sessions);
	//pthread_mutex_unlock(&serv->lock);

D
Dmitry Kozlov 已提交
1861 1862 1863
	if (serv->timer.tpd)
		triton_timer_del(&serv->timer);

1864 1865 1866 1867 1868
	triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_start, ses);

	return ses;
}

1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
struct ipoe_session *ipoe_session_alloc(void)
{
	struct ipoe_session *ses;

	ses = mempool_alloc(ses_pool);
	if (!ses) {
		log_emerg("out of memery\n");
		return NULL;
	}

	memset(ses, 0, sizeof(*ses));

	ap_session_init(&ses->ses);

	ses->ifindex = -1;
D
Dmitry Kozlov 已提交
1884

D
Dmitry Kozlov 已提交
1885
	ses->ctx.before_switch = ipoe_ctx_switch;
1886 1887 1888 1889 1890 1891 1892
	ses->ctx.close = ipoe_session_close;
	ses->ctrl.ctx = &ses->ctx;
	ses->ctrl.started = ipoe_session_started;
	ses->ctrl.finished = ipoe_session_finished;
	ses->ctrl.terminate = ipoe_session_terminate;
	ses->ctrl.type = CTRL_TYPE_IPOE;
	ses->ctrl.name = "ipoe";
1893
	ses->l4_redirect_table = conf_l4_redirect_table;
1894 1895

	ses->ses.ctrl = &ses->ctrl;
D
Dmitry Kozlov 已提交
1896

1897 1898 1899
	ses->ses.idle_timeout = conf_idle_timeout;
	ses->ses.session_timeout = conf_session_timeout;

1900 1901 1902
	return ses;
}

1903 1904 1905 1906 1907 1908 1909 1910
void ipoe_recv_up(int ifindex, struct ethhdr *eth, struct iphdr *iph)
{
	struct ipoe_serv *serv;
	struct ipoe_session *ses;

	list_for_each_entry(serv, &serv_list, entry) {
		if (serv->ifindex != ifindex)
			continue;
K
Kozlov Dmitry 已提交
1911 1912 1913

		if (!serv->opt_up)
			return;
D
Dmitry Kozlov 已提交
1914

1915 1916
		pthread_mutex_lock(&serv->lock);
		list_for_each_entry(ses, &serv->sessions, entry) {
1917
			if (ses->yiaddr == iph->saddr) {
1918 1919 1920 1921 1922
				pthread_mutex_unlock(&serv->lock);
				return;
			}
		}
		pthread_mutex_unlock(&serv->lock);
D
Dmitry Kozlov 已提交
1923

1924
		ipoe_session_create_up(serv, eth, iph);
1925 1926

		break;
1927 1928 1929
	}
}

1930 1931 1932 1933 1934
#ifdef RADIUS
static void ev_radius_access_accept(struct ev_radius_t *ev)
{
	struct ipoe_session *ses = container_of(ev->ses, typeof(*ses), ses);
	struct rad_attr_t *attr;
1935
	int lease_time_set = 0, renew_time_set = 0;
1936 1937 1938 1939 1940 1941 1942 1943

	if (ev->ses->ctrl->type != CTRL_TYPE_IPOE)
		return;

	list_for_each_entry(attr, &ev->reply->attrs, entry) {
		if (attr->attr->id == conf_attr_dhcp_client_ip)
			ses->yiaddr = attr->val.ipaddr;
		else if (attr->attr->id == conf_attr_dhcp_router_ip)
1944
			ses->router = attr->val.ipaddr;
1945
		else if (attr->attr->id == conf_attr_dhcp_mask) {
1946 1947 1948 1949
			if (attr->attr->type == ATTR_TYPE_INTEGER) {
				if (attr->val.integer > 0 && attr->val.integer < 31)
					ses->mask = attr->val.integer;
			} else if (attr->attr->type == ATTR_TYPE_IPADDR) {
1950 1951 1952
				if (attr->val.ipaddr == 0xffffffff)
					ses->mask = 32;
				else
1953
#if __BYTE_ORDER == __LITTLE_ENDIAN
1954
				ses->mask = 31 - ffs(htonl(attr->val.ipaddr));
1955
#else
1956
				ses->mask = 31 - ffs(attr->val.ipaddr);
1957 1958
#endif
			}
1959 1960 1961 1962 1963 1964
		} else if (attr->attr->id == conf_attr_l4_redirect) {
			if (attr->attr->type == ATTR_TYPE_STRING) {
				if (attr->len && attr->val.string[0] != '0')
					ses->l4_redirect = 1;
			} else if (attr->val.integer != 0)
				ses->l4_redirect = 1;
1965
		} else if (attr->attr->id == conf_attr_dhcp_lease_time) {
1966
			ses->lease_time = attr->val.integer;
1967 1968
			lease_time_set = 1;
		}	else if (attr->attr->id == conf_attr_dhcp_renew_time) {
D
Dmitry Kozlov 已提交
1969
			ses->renew_time = attr->val.integer;
1970 1971
			renew_time_set = 1;
		} else if (attr->attr->id == conf_attr_l4_redirect_table)
1972 1973 1974 1975 1976
			ses->l4_redirect_table = attr->val.integer;
		else if (attr->attr->id == conf_attr_l4_redirect_ipset) {
			if (attr->attr->type == ATTR_TYPE_STRING)
				ses->l4_redirect_ipset = _strdup(attr->val.string);
		}
1977
	}
1978 1979 1980 1981 1982 1983 1984

	if (lease_time_set && !renew_time_set)
		ses->renew_time = ses->lease_time / 2;
	else if (renew_time_set && ses->renew_time > ses->lease_time) {
		log_ppp_warn("ipoe: overriding renew time\n");
		ses->renew_time = ses->lease_time / 2;
	}
1985 1986 1987 1988 1989 1990 1991
}

static void ev_radius_coa(struct ev_radius_t *ev)
{
	struct ipoe_session *ses = container_of(ev->ses, typeof(*ses), ses);
	struct rad_attr_t *attr;
	int l4_redirect;
1992
	int lease_time_set = 0, renew_time_set = 0;
D
Dmitry Kozlov 已提交
1993

1994 1995
	if (ev->ses->ctrl->type != CTRL_TYPE_IPOE)
		return;
D
Dmitry Kozlov 已提交
1996

1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
	l4_redirect = ses->l4_redirect;

	list_for_each_entry(attr, &ev->request->attrs, entry) {
		if (attr->attr->id == conf_attr_l4_redirect) {
			if (attr->attr->type == ATTR_TYPE_STRING)
				ses->l4_redirect = attr->len && attr->val.string[0] != '0';
			else
				ses->l4_redirect = ((unsigned int)attr->val.integer) > 0;
		} else if (strcmp(attr->attr->name, "Framed-IP-Address") == 0) {
			if (ses->ses.ipv4 && ses->ses.ipv4->peer_addr != attr->val.ipaddr)
				ipoe_change_addr(ses, attr->val.ipaddr);
2008
		} else if (attr->attr->id == conf_attr_dhcp_lease_time) {
2009
			ses->lease_time = attr->val.integer;
2010 2011
			lease_time_set = 1;
		} else if (attr->attr->id == conf_attr_dhcp_renew_time) {
D
Dmitry Kozlov 已提交
2012
			ses->renew_time = attr->val.integer;
2013 2014
			renew_time_set = 1;
		} else if (attr->attr->id == conf_attr_l4_redirect_table)
2015 2016 2017 2018 2019 2020 2021 2022 2023
			ses->l4_redirect_table = attr->val.integer;
		else if (attr->attr->id == conf_attr_l4_redirect_ipset) {
			if (attr->attr->type == ATTR_TYPE_STRING) {
				if (ses->l4_redirect_ipset && strcmp(ses->l4_redirect_ipset, attr->val.string)) {
					_free(ses->l4_redirect_ipset);
					ses->l4_redirect_ipset = _strdup(attr->val.string);
				}
			}
		}
2024 2025
	}

2026 2027 2028 2029 2030 2031 2032
	if (lease_time_set && !renew_time_set)
		ses->renew_time = ses->lease_time / 2;
	else if (renew_time_set && ses->renew_time > ses->lease_time) {
		log_ppp_warn("ipoe: overriding renew time\n");
		ses->renew_time = ses->lease_time / 2;
	}

2033
	//if (l4_redirect && !ses->l4_redirect) || (!l4_redirect && ses->l4_redirect))
K
Kozlov Dmitry 已提交
2034
	if (l4_redirect != ses->l4_redirect && ev->ses->state == AP_STATE_ACTIVE)
2035 2036
		ipoe_change_l4_redirect(ses, l4_redirect);
}
2037

2038
static int ipoe_rad_send_acct_request(struct rad_plugin_t *rad, struct rad_packet_t *pack)
2039 2040 2041 2042 2043
{
	struct ipoe_session *ses = container_of(rad, typeof(*ses), radius);

	if (!ses->relay_agent)
		return 0;
D
Dmitry Kozlov 已提交
2044

2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
	if (conf_attr_dhcp_opt82 &&
		rad_packet_add_octets(pack, NULL, conf_attr_dhcp_opt82, ses->relay_agent->data, ses->relay_agent->len))
		return -1;

	if (conf_attr_dhcp_opt82_remote_id && ses->agent_remote_id &&
		rad_packet_add_octets(pack, NULL, conf_attr_dhcp_opt82_remote_id, ses->agent_remote_id + 1, *ses->agent_remote_id))
		return -1;

	if (conf_attr_dhcp_opt82_circuit_id && ses->agent_circuit_id &&
		rad_packet_add_octets(pack, NULL, conf_attr_dhcp_opt82_circuit_id, ses->agent_circuit_id + 1, *ses->agent_circuit_id))
		return -1;

	return 0;
2058 2059
}

2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
static int ipoe_rad_send_auth_request(struct rad_plugin_t *rad, struct rad_packet_t *pack)
{
	struct ipoe_session *ses = container_of(rad, typeof(*ses), radius);

	if (ipoe_rad_send_acct_request(rad, pack))
		return -1;

	if (ses->yiaddr)
		rad_packet_add_ipaddr(pack, NULL, "Framed-IP-Address", ses->yiaddr);

	return 0;
}
2072 2073
#endif

D
Dmitry Kozlov 已提交
2074
static void ipoe_serv_release(struct ipoe_serv *serv)
K
Kozlov Dmitry 已提交
2075
{
2076 2077 2078 2079 2080 2081
	pthread_mutex_lock(&serv->lock);
	if (!list_empty(&serv->sessions)) {
		pthread_mutex_unlock(&serv->lock);
		return;
	}
	pthread_mutex_unlock(&serv->lock);
D
Dmitry Kozlov 已提交
2082

D
Dmitry Kozlov 已提交
2083
	if (serv->vid && !serv->need_close && !ap_shutdown) {
D
Dmitry Kozlov 已提交
2084 2085 2086 2087 2088 2089 2090
		if (serv->timer.tpd)
			triton_timer_mod(&serv->timer, 0);
		else
			triton_timer_add(&serv->ctx, &serv->timer, 0);

		return;
	}
D
Dmitry Kozlov 已提交
2091

D
Dmitry Kozlov 已提交
2092 2093 2094 2095 2096
	log_info2("ipoe: stop interface %s\n", serv->ifname);

	pthread_mutex_lock(&serv_lock);
	list_del(&serv->entry);
	pthread_mutex_unlock(&serv_lock);
2097

K
Kozlov Dmitry 已提交
2098 2099
	if (serv->dhcpv4)
		dhcpv4_free(serv->dhcpv4);
D
Dmitry Kozlov 已提交
2100

K
Kozlov Dmitry 已提交
2101
	if (serv->dhcpv4_relay) {
2102
		ipoe_serv_del_addr(serv, serv->dhcpv4_relay->giaddr, 0);
K
Kozlov Dmitry 已提交
2103 2104
		dhcpv4_relay_free(serv->dhcpv4_relay, &serv->ctx);
	}
K
Kozlov Dmitry 已提交
2105

2106 2107 2108
	if (serv->arp)
		arpd_stop(serv->arp);

D
Dmitry Kozlov 已提交
2109 2110 2111 2112 2113 2114 2115
	while (!list_empty(&serv->disc_list)) {
		struct disc_item *d = list_entry(serv->disc_list.next, typeof(*d), entry);
		list_del(&d->entry);
		dhcpv4_packet_free(d->pack);
		mempool_free(d);
		__sync_sub_and_fetch(&stat_delayed_offer, 1);
	}
D
Dmitry Kozlov 已提交
2116

2117 2118 2119 2120 2121
	while (!list_empty(&serv->req_list)) {
		struct request_item *r = list_first_entry(&serv->req_list, typeof(*r), entry);
		list_del(&r->entry);
		mempool_free(r);
	}
D
Dmitry Kozlov 已提交
2122 2123 2124

	if (serv->disc_timer.tpd)
		triton_timer_del(&serv->disc_timer);
D
Dmitry Kozlov 已提交
2125

D
Dmitry Kozlov 已提交
2126 2127 2128
	if (serv->timer.tpd)
		triton_timer_del(&serv->timer);

2129 2130 2131
	if (serv->opt_up)
		ipoe_nl_del_interface(serv->ifindex);

D
Dmitry Kozlov 已提交
2132 2133 2134
	if (serv->vid) {
		log_info2("ipoe: remove vlan %s\n", serv->ifname);
		iplink_vlan_del(serv->ifindex);
2135
		vlan_mon_add_vid(serv->parent_ifindex, ETH_P_IP, serv->vid);
D
Dmitry Kozlov 已提交
2136
	}
D
Dmitry Kozlov 已提交
2137

D
Dmitry Kozlov 已提交
2138
	triton_context_unregister(&serv->ctx);
K
Kozlov Dmitry 已提交
2139 2140 2141 2142 2143

	_free(serv->ifname);
	_free(serv);
}

D
Dmitry Kozlov 已提交
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
static void ipoe_serv_close(struct triton_context_t *ctx)
{
	struct ipoe_serv *serv = container_of(ctx, typeof(*serv), ctx);

	pthread_mutex_lock(&serv->lock);
	if (!list_empty(&serv->sessions)) {
		serv->need_close = 1;
		pthread_mutex_unlock(&serv->lock);
		return;
	}
	pthread_mutex_unlock(&serv->lock);

	ipoe_serv_release(serv);
}

2159 2160 2161 2162 2163 2164 2165 2166
static void l4_redirect_ctx_close(struct triton_context_t *ctx)
{
	struct l4_redirect *n;

	pthread_rwlock_wrlock(&l4_list_lock);
	while (!list_empty(&l4_redirect_list)) {
		n = list_entry(l4_redirect_list.next, typeof(*n), entry);
		list_del(&n->entry);
2167 2168 2169

		if (conf_l4_redirect_table)
			iprule_del(n->addr, conf_l4_redirect_table);
D
Dmitry Kozlov 已提交
2170

2171 2172
		if (conf_l4_redirect_ipset)
			ipset_del(conf_l4_redirect_ipset, n->addr);
D
Dmitry Kozlov 已提交
2173

2174
		ipoe_nl_del_exclude(n->addr);
D
Dmitry Kozlov 已提交
2175

2176 2177 2178 2179 2180 2181
		_free(n);
	}
	pthread_rwlock_unlock(&l4_list_lock);

	if (l4_redirect_timer.tpd)
		triton_timer_del(&l4_redirect_timer);
D
Dmitry Kozlov 已提交
2182

2183 2184 2185
	triton_context_unregister(&l4_redirect_ctx);
}

K
Kozlov Dmitry 已提交
2186 2187 2188 2189 2190
static int show_stat_exec(const char *cmd, char * const *fields, int fields_cnt, void *client)
{
	cli_send(client, "ipoe:\r\n");
	cli_sendv(client,"  starting: %u\r\n", stat_starting);
	cli_sendv(client,"  active: %u\r\n", stat_active);
D
Dmitry Kozlov 已提交
2191
	cli_sendv(client,"  delayed: %u\r\n", stat_delayed_offer);
K
Kozlov Dmitry 已提交
2192 2193 2194 2195

	return CLI_CMD_OK;
}

2196 2197
static void print_session_type(struct ap_session *s, char *buf)
{
2198 2199
	if (s->ctrl->type == CTRL_TYPE_IPOE) {
		struct ipoe_session *ses = container_of(s, typeof(*ses), ses);
2200

2201 2202 2203 2204 2205 2206
		if (ses->UP)
			strcpy(buf, "up");
		else
			strcpy(buf, "dhcp");
	} else
		*buf = 0;
2207 2208
}

K
Kozlov Dmitry 已提交
2209 2210 2211 2212 2213 2214
void __export ipoe_get_stat(unsigned int **starting, unsigned int **active)
{
	*starting = &stat_starting;
	*active = &stat_active;
}

K
Kozlov Dmitry 已提交
2215 2216
static void __terminate(struct ap_session *ses)
{
2217
	ap_session_terminate(ses, TERM_NAS_REQUEST, 1);
K
Kozlov Dmitry 已提交
2218 2219 2220
}

static void ipoe_drop_sessions(struct ipoe_serv *serv, struct ipoe_session *skip)
K
Kozlov Dmitry 已提交
2221
{
K
Kozlov Dmitry 已提交
2222 2223 2224 2225 2226 2227
	struct ipoe_session *ses;

	list_for_each_entry(ses, &serv->sessions, entry) {
		if (ses == skip)
			continue;

2228 2229
		ses->terminating = 1;
		if (ses->ifcfg) {
D
Dmitry Kozlov 已提交
2230
			ipoe_ifcfg_del(ses, 0);
2231 2232 2233
			ses->ifcfg = 0;
		}

K
Kozlov Dmitry 已提交
2234 2235
		if (ses->ses.state == AP_STATE_ACTIVE)
			ap_session_ifdown(&ses->ses);
K
Kozlov Dmitry 已提交
2236

K
Kozlov Dmitry 已提交
2237 2238
		triton_context_call(&ses->ctx, (triton_event_func)__terminate, &ses->ses);
	}
K
Kozlov Dmitry 已提交
2239 2240
}

2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
struct ipoe_serv *ipoe_find_serv(const char *ifname)
{
	struct ipoe_serv *serv;

	list_for_each_entry(serv, &serv_list, entry) {
		if (strcmp(serv->ifname, ifname) == 0)
			return serv;
	}

	return NULL;
}

D
Dmitry Kozlov 已提交
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
static int get_offer_delay()
{
	struct delay *r, *prev = NULL;

	list_for_each_entry(r, &conf_offer_delay, entry) {
		if (!prev || stat_active >= r->conn_cnt) {
			prev = r;
			continue;
		}
		break;
	}

	if (prev)
		return prev->delay;
D
Dmitry Kozlov 已提交
2267

D
Dmitry Kozlov 已提交
2268 2269 2270
	return 0;
}

2271
void ipoe_vlan_mon_notify(int ifindex, int vid)
D
Dmitry Kozlov 已提交
2272 2273 2274 2275 2276
{
	struct conf_sect_t *sect = conf_get_section("ipoe");
	struct conf_option_t *opt;
	struct ifreq ifr;
	char *ptr;
2277
	int len, r, svid;
D
Dmitry Kozlov 已提交
2278 2279 2280 2281
	pcre *re = NULL;
	const char *pcre_err;
	char *pattern;
	int pcre_offset;
2282
	char ifname[IFNAMSIZ];
D
Dmitry Kozlov 已提交
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292

	if (!sect)
		return;

	memset(&ifr, 0, sizeof(ifr));
	ifr.ifr_ifindex = ifindex;
	if (ioctl(sock_fd, SIOCGIFNAME, &ifr, sizeof(ifr))) {
		log_error("ipoe: vlan-mon: failed to get interface name, ifindex=%i\n", ifindex);
		return;
	}
D
Dmitry Kozlov 已提交
2293

2294 2295
	svid = iplink_vlan_get_vid(ifindex);

D
Dmitry Kozlov 已提交
2296 2297
#ifdef USE_LUA
	if (!memcmp(conf_vlan_name, "lua:", 4))
2298
		r = ipoe_lua_make_vlan_name(conf_vlan_name + 4, ifr.ifr_name, svid, vid, ifname);
D
Dmitry Kozlov 已提交
2299 2300 2301 2302
	else
#endif
	r = make_vlan_name(conf_vlan_name, ifr.ifr_name, svid, vid, ifname);
	if (r) {
D
Dmitry Kozlov 已提交
2303 2304 2305
		log_error("ipoe: vlan-mon: %s.%i: interface name is too long\n", ifr.ifr_name, vid);
		return;
	}
D
Dmitry Kozlov 已提交
2306

2307
	strcpy(ifr.ifr_name, ifname);
D
Dmitry Kozlov 已提交
2308 2309
	len = strlen(ifr.ifr_name);

2310
	if (iplink_vlan_add(ifr.ifr_name, ifindex, vid))
D
Dmitry Kozlov 已提交
2311
		return;
2312 2313

	log_info2("ipoe: create vlan %s parent %s\n", ifname, ifr.ifr_name);
D
Dmitry Kozlov 已提交
2314

D
Dmitry Kozlov 已提交
2315 2316 2317
	ioctl(sock_fd, SIOCGIFFLAGS, &ifr, sizeof(ifr));
	ifr.ifr_flags |= IFF_UP;
	ioctl(sock_fd, SIOCSIFFLAGS, &ifr, sizeof(ifr));
D
Dmitry Kozlov 已提交
2318

D
Dmitry Kozlov 已提交
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
	if (ioctl(sock_fd, SIOCGIFINDEX, &ifr, sizeof(ifr))) {
		log_error("ipoe: vlan-mon: %s: failed to get interface index\n", ifr.ifr_name);
		return;
	}

	list_for_each_entry(opt, &sect->items, entry) {
		if (strcmp(opt->name, "interface"))
			continue;
		if (!opt->val)
			continue;
D
Dmitry Kozlov 已提交
2329

D
Dmitry Kozlov 已提交
2330 2331 2332 2333 2334 2335 2336 2337
		ptr = strchr(opt->val, ',');
		if (!ptr)
			ptr = strchr(opt->val, 0);

		if (ptr - opt->val > 3 && memcmp(opt->val, "re:", 3) == 0) {
			pattern = _malloc(ptr - (opt->val + 3) + 1);
			memcpy(pattern, opt->val + 3, ptr - (opt->val + 3));
			pattern[ptr - (opt->val + 3)] = 0;
D
Dmitry Kozlov 已提交
2338

D
Dmitry Kozlov 已提交
2339
			re = pcre_compile2(pattern, 0, NULL, &pcre_err, &pcre_offset, NULL);
D
Dmitry Kozlov 已提交
2340

D
Dmitry Kozlov 已提交
2341
			_free(pattern);
D
Dmitry Kozlov 已提交
2342

D
Dmitry Kozlov 已提交
2343 2344 2345 2346 2347
			if (!re)
				continue;

			r = pcre_exec(re, NULL, ifr.ifr_name, len, 0, 0, NULL, 0);
			pcre_free(re);
D
Dmitry Kozlov 已提交
2348

D
Dmitry Kozlov 已提交
2349 2350
			if (r < 0)
				continue;
D
Dmitry Kozlov 已提交
2351

D
Dmitry Kozlov 已提交
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361
			add_interface(ifr.ifr_name, ifr.ifr_ifindex, opt->val, ifindex, vid);
		} else if (ptr - opt->val == len && memcmp(opt->val, ifr.ifr_name, len) == 0)
			add_interface(ifr.ifr_name, ifr.ifr_ifindex, opt->val, ifindex, vid);
	}
}

static void ipoe_serv_timeout(struct triton_timer_t *t)
{
	struct ipoe_serv *serv = container_of(t, typeof(*serv), timer);

D
Dmitry Kozlov 已提交
2362
	serv->need_close = 1;
D
Dmitry Kozlov 已提交
2363

D
Dmitry Kozlov 已提交
2364
	ipoe_serv_release(serv);
D
Dmitry Kozlov 已提交
2365 2366 2367
}

static void add_interface(const char *ifname, int ifindex, const char *opt, int parent_ifindex, int vid)
K
Kozlov Dmitry 已提交
2368
{
K
Kozlov Dmitry 已提交
2369
	char *str0 = NULL, *str, *ptr1, *ptr2;
K
Kozlov Dmitry 已提交
2370
	int end;
K
Kozlov Dmitry 已提交
2371
	struct ipoe_serv *serv;
K
Kozlov Dmitry 已提交
2372 2373 2374 2375
	int opt_shared = conf_shared;
	int opt_dhcpv4 = 0;
	int opt_up = 0;
	int opt_mode = conf_mode;
2376
	int opt_ifcfg = conf_ifcfg;
2377
	int opt_nat = conf_nat;
2378
	int opt_username = conf_username;
2379
	int opt_ipv6 = conf_ipv6;
2380 2381 2382
#ifdef USE_LUA
	char *opt_lua_username_func = NULL;
#endif
K
Kozlov Dmitry 已提交
2383
	const char *opt_relay = conf_relay;
2384 2385
	in_addr_t relay_addr = conf_relay ? inet_addr(conf_relay) : 0;
	in_addr_t opt_giaddr = 0;
2386
	in_addr_t opt_src = conf_src;
2387 2388
	int opt_arp = conf_arp;
	struct ifreq ifr;
K
Kozlov Dmitry 已提交
2389 2390 2391 2392 2393

	str0 = strchr(opt, ',');
	if (str0) {
		str0 = _strdup(str0 + 1);
		str = str0;
D
Dmitry Kozlov 已提交
2394

K
Kozlov Dmitry 已提交
2395 2396
		while (1) {
			for (ptr1 = str + 1; *ptr1 && *ptr1 != '='; ptr1++);
K
Kozlov Dmitry 已提交
2397

K
Kozlov Dmitry 已提交
2398 2399
			if (!*ptr1)
				goto parse_err;
D
Dmitry Kozlov 已提交
2400

K
Kozlov Dmitry 已提交
2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
			*ptr1 = 0;

			for (ptr2 = ++ptr1; *ptr2 && *ptr2 != ','; ptr2++);

			end = *ptr2 == 0;

			if (!end)
				*ptr2 = 0;

			if (ptr2 == ptr1)
				goto parse_err;

			if (strcmp(str, "start") == 0) {
				if (!strcmp(ptr1, "up"))
					opt_up = 1;
				else if (!strcmp(ptr1, "dhcpv4"))
					opt_dhcpv4 = 1;
				else
					goto parse_err;
			} else if (strcmp(str, "shared") == 0) {
				opt_shared = atoi(ptr1);
			} else if (strcmp(str, "mode") == 0) {
				if (!strcmp(ptr1, "L2"))
					opt_mode = MODE_L2;
				else if (!strcmp(ptr1, "L3"))
					opt_mode = MODE_L3;
				else
					goto parse_err;
2429 2430
			} else if (strcmp(str, "ifcfg") == 0) {
				opt_ifcfg = atoi(ptr1);
K
Kozlov Dmitry 已提交
2431 2432 2433 2434
			} else if (strcmp(str, "relay") == 0) {
				opt_relay = ptr1;
				relay_addr = inet_addr(ptr1);
			} else if (strcmp(str, "giaddr") == 0) {
2435
				opt_giaddr = inet_addr(ptr1);
2436 2437
			} else if (strcmp(str, "nat") == 0) {
				opt_nat = atoi(ptr1);
2438 2439
			} else if (strcmp(str, "src") == 0) {
				opt_src = inet_addr(ptr1);
2440 2441
			} else if (strcmp(str, "proxy-arp") == 0) {
				opt_arp = atoi(ptr1);
2442 2443
			} else if (strcmp(str, "ipv6") == 0) {
				opt_ipv6 = atoi(ptr1);
2444 2445 2446 2447 2448 2449 2450
			} else if (strcmp(str, "username") == 0) {
				if (strcmp(ptr1, "ifname") == 0)
					opt_username = USERNAME_IFNAME;
#ifdef USE_LUA
				else if (strlen(ptr1) > 4 && memcmp(ptr1, "lua:", 4) == 0) {
					opt_username = USERNAME_LUA;
					opt_lua_username_func = _strdup(ptr1 + 4);
D
Dmitry Kozlov 已提交
2451
				}
2452 2453 2454
#endif
				else
					log_error("ipoe: unknown username value '%s'\n", ptr1);
2455
			}
K
Kozlov Dmitry 已提交
2456 2457 2458 2459 2460 2461

			if (end)
				break;

			str = ptr2 + 1;
		}
D
Dmitry Kozlov 已提交
2462
	}
K
Kozlov Dmitry 已提交
2463 2464 2465 2466 2467 2468

	if (!opt_up && !opt_dhcpv4) {
		opt_up = conf_up;
		opt_dhcpv4 = conf_dhcpv4;
	}

2469 2470 2471 2472 2473 2474 2475 2476 2477
	if (opt_relay && !opt_giaddr && opt_dhcpv4) {
		struct sockaddr_in addr;
		int sock;
		socklen_t len = sizeof(addr);

		memset(&addr, 0, sizeof(addr));
		addr.sin_family = AF_INET;
		addr.sin_addr.s_addr = relay_addr;
		addr.sin_port = htons(DHCP_SERV_PORT);
D
Dmitry Kozlov 已提交
2478

2479
		sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
D
Dmitry Kozlov 已提交
2480

2481 2482 2483 2484
		if (connect(sock, &addr, sizeof(addr))) {
			log_error("dhcpv4: relay: %s: connect: %s\n", opt_relay, strerror(errno));
			goto out_err;
		}
D
Dmitry Kozlov 已提交
2485

2486 2487 2488 2489 2490 2491
		getsockname(sock, &addr, &len);
		opt_giaddr = addr.sin_addr.s_addr;

		close(sock);
	}

2492 2493 2494
	if (opt_up)
		ipoe_nl_add_interface(ifindex);

D
Dmitry Kozlov 已提交
2495
	pthread_mutex_lock(&serv_lock);
K
Kozlov Dmitry 已提交
2496
	list_for_each_entry(serv, &serv_list, entry) {
2497
		if (strcmp(ifname, serv->ifname))
K
Kozlov Dmitry 已提交
2498 2499 2500 2501
			continue;

		serv->active = 1;
		serv->ifindex = ifindex;
D
Dmitry Kozlov 已提交
2502

K
Kozlov Dmitry 已提交
2503 2504 2505
		if ((opt_shared && !serv->opt_shared) || (!opt_shared && serv->opt_shared)) {
			ipoe_drop_sessions(serv, NULL);
			serv->opt_shared = opt_shared;
K
Kozlov Dmitry 已提交
2506
		}
K
Kozlov Dmitry 已提交
2507 2508

		if (opt_dhcpv4 && !serv->dhcpv4) {
2509
			serv->dhcpv4 = dhcpv4_create(&serv->ctx, serv->ifname, opt);
K
Kozlov Dmitry 已提交
2510 2511 2512 2513 2514 2515 2516
			if (serv->dhcpv4)
				serv->dhcpv4->recv = ipoe_recv_dhcpv4;
		} else if (!opt_dhcpv4 && serv->dhcpv4) {
			dhcpv4_free(serv->dhcpv4);
			serv->dhcpv4 = NULL;
		}

D
Dmitry Kozlov 已提交
2517
		if (serv->dhcpv4_relay &&
2518
				(serv->dhcpv4_relay->addr != relay_addr || serv->dhcpv4_relay->giaddr != opt_giaddr)) {
2519
			if (serv->opt_ifcfg)
2520
				ipoe_serv_del_addr(serv, serv->dhcpv4_relay->giaddr, 0);
K
Kozlov Dmitry 已提交
2521 2522 2523 2524
			dhcpv4_relay_free(serv->dhcpv4_relay, &serv->ctx);
			serv->dhcpv4_relay = NULL;
		}

2525
		if (!serv->dhcpv4_relay && serv->opt_dhcpv4 && opt_relay) {
2526
			if (opt_ifcfg)
2527
				ipoe_serv_add_addr(serv, opt_giaddr, 32);
K
Kozlov Dmitry 已提交
2528
			serv->dhcpv4_relay = dhcpv4_relay_create(opt_relay, opt_giaddr, &serv->ctx, (triton_event_func)ipoe_recv_dhcpv4_relay);
K
Kozlov Dmitry 已提交
2529
		}
2530 2531 2532 2533 2534 2535

		if (serv->arp && !conf_arp) {
			arpd_stop(serv->arp);
			serv->arp = NULL;
		} else if (!serv->arp && conf_arp)
			serv->arp = arpd_start(serv);
D
Dmitry Kozlov 已提交
2536

2537 2538 2539
		serv->opt_up = opt_up;
		serv->opt_mode = opt_mode;
		serv->opt_ifcfg = opt_ifcfg;
2540
		serv->opt_nat = opt_nat;
2541
		serv->opt_src = opt_src;
2542
		serv->opt_arp = opt_arp;
2543
		serv->opt_username = opt_username;
2544
		serv->opt_ipv6 = opt_ipv6;
2545 2546 2547 2548 2549
#ifdef USE_LUA
		if (serv->opt_lua_username_func && (!opt_lua_username_func || strcmp(serv->opt_lua_username_func, opt_lua_username_func))) {
			_free(serv->opt_lua_username_func);
			serv->opt_lua_username_func = NULL;
		}
D
Dmitry Kozlov 已提交
2550

2551 2552 2553 2554 2555
		if (!serv->opt_lua_username_func && opt_lua_username_func)
			serv->opt_lua_username_func = opt_lua_username_func;
		else if (opt_lua_username_func)
			_free(opt_lua_username_func);
#endif
K
Kozlov Dmitry 已提交
2556 2557 2558 2559

		if (str0)
			_free(str0);

D
Dmitry Kozlov 已提交
2560
		pthread_mutex_unlock(&serv_lock);
K
Kozlov Dmitry 已提交
2561
		return;
K
Kozlov Dmitry 已提交
2562
	}
D
Dmitry Kozlov 已提交
2563
	pthread_mutex_unlock(&serv_lock);
K
Kozlov Dmitry 已提交
2564

2565 2566 2567 2568 2569
	opt = strchr(opt, ',');
	if (opt)
		opt++;

	log_info2("ipoe: start interface %s (%s)\n", ifname, opt ? opt : "");
D
Dmitry Kozlov 已提交
2570

2571 2572
	memset(&ifr, 0, sizeof(ifr));
	strcpy(ifr.ifr_name, ifname);
D
Dmitry Kozlov 已提交
2573

2574 2575 2576 2577
	if (ioctl(sock_fd, SIOCGIFHWADDR, &ifr)) {
		log_error("ipoe: '%s': ioctl(SIOCGIFHWADDR): %s\n", ifname, strerror(errno));
		return;
	}
D
Dmitry Kozlov 已提交
2578

D
Dmitry Kozlov 已提交
2579
	ioctl(sock_fd, SIOCGIFFLAGS, &ifr);
D
Dmitry Kozlov 已提交
2580

D
Dmitry Kozlov 已提交
2581 2582 2583 2584 2585
	if (!(ifr.ifr_flags & IFF_UP)) {
		ifr.ifr_flags |= IFF_UP;

		ioctl(sock_fd, SIOCSIFFLAGS, &ifr);
	}
2586

K
Kozlov Dmitry 已提交
2587 2588
	serv = _malloc(sizeof(*serv));
	memset(serv, 0, sizeof(*serv));
2589
	serv->ctx.close = ipoe_serv_close;
D
Dmitry Kozlov 已提交
2590
	serv->ctx.before_switch = ipoe_ctx_switch;
D
Dmitry Kozlov 已提交
2591
	pthread_mutex_init(&serv->lock, NULL);
K
Kozlov Dmitry 已提交
2592 2593
	serv->ifname = _strdup(ifname);
	serv->ifindex = ifindex;
K
Kozlov Dmitry 已提交
2594 2595 2596 2597
	serv->opt_shared = opt_shared;
	serv->opt_dhcpv4 = opt_dhcpv4;
	serv->opt_up = opt_up;
	serv->opt_mode = opt_mode;
2598
	serv->opt_ifcfg = opt_ifcfg;
2599
	serv->opt_nat = opt_nat;
2600
	serv->opt_src = opt_src;
2601
	serv->opt_arp = opt_arp;
2602
	serv->opt_username = opt_username;
2603
	serv->opt_ipv6 = opt_ipv6;
2604 2605 2606
#ifdef USE_LUA
	serv->opt_lua_username_func = opt_lua_username_func;
#endif
D
Dmitry Kozlov 已提交
2607 2608
	serv->parent_ifindex = parent_ifindex = parent_ifindex;
	serv->vid = vid;
2609
	serv->active = 1;
K
Kozlov Dmitry 已提交
2610
	INIT_LIST_HEAD(&serv->sessions);
2611
	INIT_LIST_HEAD(&serv->addr_list);
D
Dmitry Kozlov 已提交
2612
	INIT_LIST_HEAD(&serv->disc_list);
2613
	INIT_LIST_HEAD(&serv->req_list);
2614
	memcpy(serv->hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
D
Dmitry Kozlov 已提交
2615
	serv->disc_timer.expire = ipoe_serv_disc_timer;
D
Dmitry Kozlov 已提交
2616

K
Kozlov Dmitry 已提交
2617 2618 2619
	triton_context_register(&serv->ctx, NULL);

	if (serv->opt_dhcpv4) {
2620
		serv->dhcpv4 = dhcpv4_create(&serv->ctx, serv->ifname, opt);
K
Kozlov Dmitry 已提交
2621
		if (serv->dhcpv4)
2622
			serv->dhcpv4->recv = ipoe_recv_dhcpv4;
D
Dmitry Kozlov 已提交
2623

2624
		if (opt_relay) {
2625
			if (opt_ifcfg)
2626
				ipoe_serv_add_addr(serv, opt_giaddr, 32);
K
Kozlov Dmitry 已提交
2627 2628
			serv->dhcpv4_relay = dhcpv4_relay_create(opt_relay, opt_giaddr, &serv->ctx, (triton_event_func)ipoe_recv_dhcpv4_relay);
		}
K
Kozlov Dmitry 已提交
2629 2630
	}

2631 2632
	if (serv->opt_arp)
		serv->arp = arpd_start(serv);
D
Dmitry Kozlov 已提交
2633

D
Dmitry Kozlov 已提交
2634 2635 2636 2637 2638
	if (vid) {
		serv->timer.expire = ipoe_serv_timeout;
		serv->timer.expire_tv.tv_sec = conf_vlan_timeout;
		triton_timer_add(&serv->ctx, &serv->timer, 0);
	}
2639

K
Kozlov Dmitry 已提交
2640 2641
	triton_context_wakeup(&serv->ctx);

D
Dmitry Kozlov 已提交
2642
	pthread_mutex_lock(&serv_lock);
2643
	list_add_tail(&serv->entry, &serv_list);
D
Dmitry Kozlov 已提交
2644
	pthread_mutex_unlock(&serv_lock);
2645

K
Kozlov Dmitry 已提交
2646 2647 2648
	if (str0)
		_free(str0);

K
Kozlov Dmitry 已提交
2649 2650
	return;

K
Kozlov Dmitry 已提交
2651
parse_err:
K
Kozlov Dmitry 已提交
2652
	log_error("ipoe: failed to parse '%s'\n", opt);
2653
out_err:
K
Kozlov Dmitry 已提交
2654
	_free(str0);
K
Kozlov Dmitry 已提交
2655 2656 2657 2658 2659 2660
}

static void load_interface(const char *opt)
{
	const char *ptr;
	struct ifreq ifr;
2661
	struct ipoe_serv *serv;
K
Kozlov Dmitry 已提交
2662 2663 2664 2665 2666 2667 2668 2669

	for (ptr = opt; *ptr && *ptr != ','; ptr++);

	if (ptr - opt >= sizeof(ifr.ifr_name))
		return;

	memcpy(ifr.ifr_name, opt, ptr - opt);
	ifr.ifr_name[ptr - opt] = 0;
D
Dmitry Kozlov 已提交
2670

2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
	list_for_each_entry(serv, &serv_list, entry) {
		if (serv->active)
			continue;

		if (!strcmp(serv->ifname, ifr.ifr_name)) {
			add_interface(serv->ifname, serv->ifindex, opt, 0, 0);
			return;
		}
	}

K
Kozlov Dmitry 已提交
2681 2682 2683 2684
	if (ioctl(sock_fd, SIOCGIFINDEX, &ifr)) {
		log_error("ipoe: '%s': ioctl(SIOCGIFINDEX): %s\n", ifr.ifr_name, strerror(errno));
		return;
	}
D
Dmitry Kozlov 已提交
2685

D
Dmitry Kozlov 已提交
2686
	add_interface(ifr.ifr_name, ifr.ifr_ifindex, opt, 0, 0);
K
Kozlov Dmitry 已提交
2687 2688 2689 2690 2691 2692
}

static int __load_interface_re(int index, int flags, const char *name, struct iplink_arg *arg)
{
	if (pcre_exec(arg->re, NULL, name, strlen(name), 0, 0, NULL, 0) < 0)
		return 0;
2693

D
Dmitry Kozlov 已提交
2694
	add_interface(name, index, arg->opt, 0, 0);
K
Kozlov Dmitry 已提交
2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706

	return 0;
}

static void load_interface_re(const char *opt)
{
	pcre *re = NULL;
	const char *pcre_err;
	char *pattern;
	const char *ptr;
	int pcre_offset;
	struct iplink_arg arg;
2707
	struct ipoe_serv *serv;
K
Kozlov Dmitry 已提交
2708 2709

	for (ptr = opt; *ptr && *ptr != ','; ptr++);
D
Dmitry Kozlov 已提交
2710

K
Kozlov Dmitry 已提交
2711 2712 2713
	pattern = _malloc(ptr - (opt + 3) + 1);
	memcpy(pattern, opt + 3, ptr - (opt + 3));
	pattern[ptr - (opt + 3)] = 0;
D
Dmitry Kozlov 已提交
2714

K
Kozlov Dmitry 已提交
2715
	re = pcre_compile2(pattern, 0, NULL, &pcre_err, &pcre_offset, NULL);
D
Dmitry Kozlov 已提交
2716

K
Kozlov Dmitry 已提交
2717
	if (!re) {
D
Dmitry Kozlov 已提交
2718
		log_error("ipoe: '%s': %s at %i\r\n", pattern, pcre_err, pcre_offset);
K
Kozlov Dmitry 已提交
2719 2720 2721 2722 2723 2724 2725 2726
		return;
	}

	arg.re = re;
	arg.opt = opt;

	iplink_list((iplink_list_func)__load_interface_re, &arg);

2727 2728 2729 2730 2731 2732 2733 2734
	list_for_each_entry(serv, &serv_list, entry) {
		if (serv->active)
			continue;

		if (pcre_exec(re, NULL, serv->ifname, strlen(serv->ifname), 0, 0, NULL, 0) >= 0)
			add_interface(serv->ifname, serv->ifindex, opt, 0, 0);
	}

K
Kozlov Dmitry 已提交
2735 2736 2737 2738 2739 2740 2741 2742 2743
	pcre_free(re);
	_free(pattern);
}

static void load_interfaces(struct conf_sect_t *sect)
{
	struct ipoe_serv *serv;
	struct conf_option_t *opt;

2744 2745
	ipoe_nl_delete_interfaces();

K
Kozlov Dmitry 已提交
2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
	list_for_each_entry(serv, &serv_list, entry)
		serv->active = 0;

	list_for_each_entry(opt, &sect->items, entry) {
		if (strcmp(opt->name, "interface"))
			continue;
		if (!opt->val)
			continue;

		if (strlen(opt->val) > 3 && memcmp(opt->val, "re:", 3) == 0)
			load_interface_re(opt->val);
		else
			load_interface(opt->val);
	}
D
Dmitry Kozlov 已提交
2760

D
Dmitry Kozlov 已提交
2761
	list_for_each_entry(serv, &serv_list, entry) {
D
Dmitry Kozlov 已提交
2762
		if (!serv->active && !serv->vid) {
2763
			ipoe_drop_sessions(serv, NULL);
D
Dmitry Kozlov 已提交
2764
			triton_context_call(&serv->ctx, (triton_event_func)ipoe_serv_release, serv);
K
Kozlov Dmitry 已提交
2765 2766 2767 2768
		}
	}
}

2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
static void parse_local_net(const char *opt)
{
	const char *ptr;
	char str[17];
	in_addr_t addr;
	int mask;
	char *endptr;

	ptr = strchr(opt, '/');
	if (ptr) {
		memcpy(str, opt, ptr - opt);
		str[ptr - opt] = 0;
		addr = inet_addr(str);
		if (addr == INADDR_NONE)
			goto out_err;
		mask = strtoul(ptr + 1, &endptr, 10);
		if (mask > 32)
			goto out_err;
	} else {
		addr = inet_addr(opt);
		if (addr == INADDR_NONE)
			goto out_err;
		mask = 24;
	}

2794
	ipoe_nl_add_net(addr, mask);
2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816

	return;

out_err:
	log_error("ipoe: failed to parse 'local-net=%s'\n", opt);
}

static void load_local_nets(struct conf_sect_t *sect)
{
	struct conf_option_t *opt;

	ipoe_nl_delete_nets();

	list_for_each_entry(opt, &sect->items, entry) {
		if (strcmp(opt->name, "local-net"))
			continue;
		if (!opt->val)
			continue;
		parse_local_net(opt->val);
	}
}

2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852
static void load_gw_addr(struct conf_sect_t *sect)
{
	struct conf_option_t *opt;
	struct gw_addr *a;
	char addr[17];
	char *ptr;

	while (!list_empty(&conf_gw_addr)) {
		a = list_entry(conf_gw_addr.next, typeof(*a), entry);
		list_del(&a->entry);
		_free(a);
	}

	list_for_each_entry(opt, &sect->items, entry) {
		if (strcmp(opt->name, "gw-ip-address"))
			continue;
		if (!opt->val)
			continue;

		a = _malloc(sizeof(*a));
		ptr = strchr(opt->val, '/');
		if (ptr) {
			memcpy(addr, opt->val, ptr - opt->val);
			addr[ptr - opt->val] = 0;
			a->addr = inet_addr(addr);
			a->mask = atoi(ptr + 1);
		} else {
			a->addr = inet_addr(opt->val);
			a->mask = 32;
		}

		if (a->addr == 0xffffffff || a->mask < 1 || a->mask > 32) {
			log_error("ipoe: failed to parse '%s=%s'\n", opt->name, opt->val);
			_free(a);
			continue;
		}
2853 2854

		a->mask1 = ((1 << a->mask) - 1) << (32 - a->mask);
2855 2856 2857 2858
		list_add_tail(&a->entry, &conf_gw_addr);
	}
}

2859 2860 2861 2862 2863 2864
#ifdef RADIUS
static void parse_conf_rad_attr(const char *opt, int *val)
{
	struct rad_dict_attr_t *attr;

	opt = conf_get_opt("ipoe", opt);
D
Dmitry Kozlov 已提交
2865

2866
	*val = 0;
2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877

	if (opt) {
		if (atoi(opt) > 0)
			*val = atoi(opt);
		else {
			attr = rad_dict_find_attr(opt);
			if (attr)
				*val = attr->id;
			else
				log_emerg("ipoe: couldn't find '%s' in dictionary\n", opt);
		}
2878
	}
2879
}
K
Kozlov Dmitry 已提交
2880

2881 2882 2883 2884 2885
static void load_radius_attrs(void)
{
	parse_conf_rad_attr("attr-dhcp-client-ip", &conf_attr_dhcp_client_ip);
	parse_conf_rad_attr("attr-dhcp-router-ip", &conf_attr_dhcp_router_ip);
	parse_conf_rad_attr("attr-dhcp-mask", &conf_attr_dhcp_mask);
2886
	parse_conf_rad_attr("attr-dhcp-lease-time", &conf_attr_dhcp_lease_time);
D
Dmitry Kozlov 已提交
2887
	parse_conf_rad_attr("attr-dhcp-renew-time", &conf_attr_dhcp_renew_time);
2888
	parse_conf_rad_attr("attr-l4-redirect", &conf_attr_l4_redirect);
2889 2890
	parse_conf_rad_attr("attr-l4-redirect-table", &conf_attr_l4_redirect_table);
	parse_conf_rad_attr("attr-l4-redirect-ipset", &conf_attr_l4_redirect_ipset);
2891
	conf_attr_dhcp_opt82 = conf_get_opt("ipoe", "attr-dhcp-opt82");
2892 2893
	conf_attr_dhcp_opt82_remote_id = conf_get_opt("ipoe", "attr-dhcp-opt82-remote-id");
	conf_attr_dhcp_opt82_circuit_id = conf_get_opt("ipoe", "attr-dhcp-opt82-circuit-id");
2894 2895 2896
}
#endif

D
Dmitry Kozlov 已提交
2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971
static void strip(char *str)
{
	char *ptr = str;
	char *endptr = strchr(str, 0);
	while (1) {
		ptr = strchr(ptr, ' ');
		if (ptr)
			memmove(ptr, ptr + 1, endptr - ptr - 1);
		else
			break;
	}
}

int parse_offer_delay(const char *str)
{
	char *str1;
	char *ptr1, *ptr2, *ptr3, *endptr;
	struct delay *r;

	while (!list_empty(&conf_offer_delay)) {
		r = list_entry(conf_offer_delay.next, typeof(*r), entry);
		list_del(&r->entry);
		_free(r);
	}

	if (!str)
		return 0;

	str1 = _strdup(str);
	strip(str1);

	ptr1 = str1;

	while (1) {
		ptr2 = strchr(ptr1, ',');
		if (ptr2)
			*ptr2 = 0;
		ptr3 = strchr(ptr1, ':');
		if (ptr3)
			*ptr3 = 0;

		r = _malloc(sizeof(*r));
		memset(r, 0, sizeof(*r));

		r->delay = strtol(ptr1, &endptr, 10);
		if (*endptr)
			goto out_err;

		if (list_empty(&conf_offer_delay))
			r->conn_cnt = 0;
		else {
			if (!ptr3)
				goto out_err;
			r->conn_cnt = strtol(ptr3 + 1, &endptr, 10);
			if (*endptr)
				goto out_err;
		}

		list_add_tail(&r->entry, &conf_offer_delay);

		if (!ptr2)
			break;

		ptr1 = ptr2 + 1;
	}

	_free(str1);
	return 0;

out_err:
	_free(str1);
	log_error("ipoe: failed to parse offer-delay\n");
	return -1;
}

D
Dmitry Kozlov 已提交
2972
static void add_vlan_mon(const char *opt, long *mask)
D
Dmitry Kozlov 已提交
2973 2974 2975 2976
{
	const char *ptr;
	struct ifreq ifr;
	int ifindex;
D
Dmitry Kozlov 已提交
2977 2978
	long mask1[4096/8/sizeof(long)];
	struct ipoe_serv *serv;
D
Dmitry Kozlov 已提交
2979

D
Dmitry Kozlov 已提交
2980
	for (ptr = opt; *ptr && *ptr != ','; ptr++);
D
Dmitry Kozlov 已提交
2981

2982
	if (ptr - opt >= IFNAMSIZ) {
D
Dmitry Kozlov 已提交
2983 2984 2985 2986 2987
		log_error("ipoe: vlan-mon=%s: interface name is too long\n", opt);
		return;
	}

	memset(&ifr, 0, sizeof(ifr));
D
Dmitry Kozlov 已提交
2988

D
Dmitry Kozlov 已提交
2989 2990 2991 2992 2993 2994 2995 2996 2997
	memcpy(ifr.ifr_name, opt, ptr - opt);
	ifr.ifr_name[ptr - opt] = 0;

	if (ioctl(sock_fd, SIOCGIFINDEX, &ifr)) {
		log_error("ipoe: '%s': ioctl(SIOCGIFINDEX): %s\n", ifr.ifr_name, strerror(errno));
		return;
	}

	ifindex = ifr.ifr_ifindex;
D
Dmitry Kozlov 已提交
2998

D
Dmitry Kozlov 已提交
2999
	ioctl(sock_fd, SIOCGIFFLAGS, &ifr);
D
Dmitry Kozlov 已提交
3000

D
Dmitry Kozlov 已提交
3001 3002 3003 3004 3005 3006
	if (!(ifr.ifr_flags & IFF_UP)) {
		ifr.ifr_flags |= IFF_UP;

		ioctl(sock_fd, SIOCSIFFLAGS, &ifr);
	}

D
Dmitry Kozlov 已提交
3007 3008 3009 3010 3011 3012
	memcpy(mask1, mask, sizeof(mask1));
	list_for_each_entry(serv, &serv_list, entry) {
		if (serv->vid && serv->parent_ifindex == ifindex)
			mask1[serv->vid / (8*sizeof(long))] |= 1lu << (serv->vid % (8*sizeof(long)));
	}

3013
	vlan_mon_add(ifindex, ETH_P_IP, mask1, sizeof(mask1));
D
Dmitry Kozlov 已提交
3014 3015 3016 3017 3018
}

static int __load_vlan_mon_re(int index, int flags, const char *name, struct iplink_arg *arg)
{
	struct ifreq ifr;
D
Dmitry Kozlov 已提交
3019 3020
	long mask1[4096/8/sizeof(long)];
	struct ipoe_serv *serv;
D
Dmitry Kozlov 已提交
3021 3022 3023 3024 3025 3026

	if (pcre_exec(arg->re, NULL, name, strlen(name), 0, 0, NULL, 0) < 0)
		return 0;

	memset(&ifr, 0, sizeof(ifr));
	strcpy(ifr.ifr_name, name);
D
Dmitry Kozlov 已提交
3027

D
Dmitry Kozlov 已提交
3028
	ioctl(sock_fd, SIOCGIFFLAGS, &ifr);
D
Dmitry Kozlov 已提交
3029

D
Dmitry Kozlov 已提交
3030 3031 3032 3033 3034
	if (!(ifr.ifr_flags & IFF_UP)) {
		ifr.ifr_flags |= IFF_UP;

		ioctl(sock_fd, SIOCSIFFLAGS, &ifr);
	}
D
Dmitry Kozlov 已提交
3035

D
Dmitry Kozlov 已提交
3036 3037 3038 3039 3040
	memcpy(mask1, arg->arg1, sizeof(mask1));
	list_for_each_entry(serv, &serv_list, entry) {
		if (serv->vid && serv->parent_ifindex == index)
			mask1[serv->vid / (8*sizeof(long))] |= 1lu << (serv->vid % (8*sizeof(long)));
	}
D
Dmitry Kozlov 已提交
3041

3042
	vlan_mon_add(index, ETH_P_IP,  mask1, sizeof(mask1));
D
Dmitry Kozlov 已提交
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056

	return 0;
}

static void load_vlan_mon_re(const char *opt, long *mask, int len)
{
	pcre *re = NULL;
	const char *pcre_err;
	char *pattern;
	const char *ptr;
	int pcre_offset;
	struct iplink_arg arg;

	for (ptr = opt; *ptr && *ptr != ','; ptr++);
D
Dmitry Kozlov 已提交
3057

D
Dmitry Kozlov 已提交
3058 3059 3060
	pattern = _malloc(ptr - (opt + 3) + 1);
	memcpy(pattern, opt + 3, ptr - (opt + 3));
	pattern[ptr - (opt + 3)] = 0;
D
Dmitry Kozlov 已提交
3061

D
Dmitry Kozlov 已提交
3062
	re = pcre_compile2(pattern, 0, NULL, &pcre_err, &pcre_offset, NULL);
D
Dmitry Kozlov 已提交
3063

D
Dmitry Kozlov 已提交
3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
	if (!re) {
		log_error("ipoe: '%s': %s at %i\r\n", pattern, pcre_err, pcre_offset);
		return;
	}

	arg.re = re;
	arg.opt = opt;
	arg.arg1 = mask;

	iplink_list((iplink_list_func)__load_vlan_mon_re, &arg);

	pcre_free(re);
	_free(pattern);

}

static void load_vlan_mon(struct conf_sect_t *sect)
{
	struct conf_option_t *opt;
	long mask[4096/8/sizeof(long)];
3084 3085 3086 3087 3088 3089 3090 3091 3092
	static int registered = 0;

	if (!triton_module_loaded("vlan-mon"))
		return;

	if (!registered) {
		vlan_mon_register_proto(ETH_P_IP, ipoe_vlan_mon_notify);
		registered = 1;
	}
D
Dmitry Kozlov 已提交
3093

3094
	vlan_mon_del(-1, ETH_P_IP);
D
Dmitry Kozlov 已提交
3095 3096 3097 3098 3099 3100 3101

	list_for_each_entry(opt, &sect->items, entry) {
		if (strcmp(opt->name, "vlan-mon"))
			continue;

		if (!opt->val)
			continue;
D
Dmitry Kozlov 已提交
3102

D
Dmitry Kozlov 已提交
3103 3104 3105 3106 3107 3108
		if (parse_vlan_mon(opt->val, mask))
			continue;

		if (strlen(opt->val) > 3 && !memcmp(opt->val, "re:", 3))
			load_vlan_mon_re(opt->val, mask, sizeof(mask));
		else
D
Dmitry Kozlov 已提交
3109
			add_vlan_mon(opt->val, mask);
D
Dmitry Kozlov 已提交
3110 3111 3112 3113
	}
}


K
Kozlov Dmitry 已提交
3114 3115 3116 3117
static void load_config(void)
{
	const char *opt;
	struct conf_sect_t *s = conf_get_section("ipoe");
K
Kozlov Dmitry 已提交
3118
	struct conf_option_t *opt1;
K
Kozlov Dmitry 已提交
3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130

	if (!s)
		return;

	opt = conf_get_opt("ipoe", "username");
	if (opt) {
		if (strcmp(opt, "ifname") == 0)
			conf_username = USERNAME_IFNAME;
#ifdef USE_LUA
		else if (strlen(opt) > 4 && memcmp(opt, "lua:", 4) == 0) {
			conf_username = USERNAME_LUA;
			conf_lua_username_func = opt + 4;
K
Kozlov Dmitry 已提交
3131
		}
K
Kozlov Dmitry 已提交
3132
#endif
K
Kozlov Dmitry 已提交
3133
		else
K
Kozlov Dmitry 已提交
3134
			log_emerg("ipoe: unknown username value '%s'\n", opt);
3135 3136
	} else
		conf_username = USERNAME_UNSET;
D
Dmitry Kozlov 已提交
3137

3138 3139 3140 3141 3142 3143 3144 3145 3146 3147
	opt = conf_get_opt("ipoe", "password");
	if (opt) {
		if (!strcmp(opt, "username"))
			conf_password = NULL;
		else if (!strcmp(opt, "empty"))
			conf_password = "";
		else
			conf_password = opt;
	} else
		conf_password = NULL;
K
Kozlov Dmitry 已提交
3148 3149 3150 3151 3152 3153 3154 3155 3156 3157

	opt = conf_get_opt("ipoe", "netmask");
	if (opt) {
		conf_netmask = atoi(opt);
		if (conf_netmask <= 0 || conf_netmask > 32) {
			log_error("ipoe: invalid netmask %s\n", opt);
			conf_netmask = 0;
		}
	} else
		conf_netmask = 0;
D
Dmitry Kozlov 已提交
3158

K
Kozlov Dmitry 已提交
3159 3160 3161
	opt = conf_get_opt("ipoe", "verbose");
	if (opt)
		conf_verbose = atoi(opt);
K
Kozlov Dmitry 已提交
3162 3163 3164 3165

	opt = conf_get_opt("ipoe", "lease-time");
	if (opt)
		conf_lease_time = atoi(opt);
K
Kozlov Dmitry 已提交
3166 3167
	else
		conf_lease_time = 600;
D
Dmitry Kozlov 已提交
3168

D
Dmitry Kozlov 已提交
3169 3170 3171 3172 3173 3174
	opt = conf_get_opt("ipoe", "renew-time");
	if (opt)
		conf_renew_time = atoi(opt);
	else
		conf_renew_time = conf_lease_time/2;

K
Kozlov Dmitry 已提交
3175
	opt = conf_get_opt("ipoe", "max-lease-time");
K
Kozlov Dmitry 已提交
3176 3177
	if (opt)
		conf_lease_timeout = atoi(opt);
K
Kozlov Dmitry 已提交
3178
	else
3179
		conf_lease_timeout = conf_lease_time;
D
Dmitry Kozlov 已提交
3180

K
Kozlov Dmitry 已提交
3181 3182 3183
	opt = conf_get_opt("ipoe", "unit-cache");
	if (opt)
		conf_unit_cache = atoi(opt);
D
Dmitry Kozlov 已提交
3184

3185
	opt = conf_get_opt("ipoe", "l4-redirect-table");
3186
	if (opt && atoi(opt) > 0)
3187 3188
		conf_l4_redirect_table = atoi(opt);
	else
3189
		conf_l4_redirect_table = 0;
D
Dmitry Kozlov 已提交
3190

3191
	conf_l4_redirect_ipset = conf_get_opt("ipoe", "l4-redirect-ipset");
D
Dmitry Kozlov 已提交
3192

3193 3194 3195 3196 3197
	opt = conf_get_opt("ipoe", "l4-redirect-on-reject");
	if (opt) {
		conf_l4_redirect_on_reject = atoi(opt);
	} else
		conf_l4_redirect_on_reject = 0;
D
Dmitry Kozlov 已提交
3198

3199 3200 3201 3202 3203
	if (conf_l4_redirect_on_reject) {
		l4_redirect_timer.period = conf_l4_redirect_on_reject / 10 * 1000;
		if (l4_redirect_timer.tpd)
			triton_timer_mod(&l4_redirect_timer, 0);
	}
D
Dmitry Kozlov 已提交
3204

K
Kozlov Dmitry 已提交
3205 3206 3207 3208 3209
	opt = conf_get_opt("ipoe", "shared");
	if (opt)
		conf_shared = atoi(opt);
	else
		conf_shared = 1;
D
Dmitry Kozlov 已提交
3210

3211 3212 3213 3214 3215
	opt = conf_get_opt("ipoe", "ifcfg");
	if (opt)
		conf_ifcfg = atoi(opt);
	else
		conf_ifcfg = 1;
D
Dmitry Kozlov 已提交
3216

3217 3218 3219 3220 3221
	opt = conf_get_opt("ipoe", "nat");
	if (opt)
		conf_nat = atoi(opt);
	else
		conf_nat = 0;
3222 3223 3224 3225 3226 3227

	opt = conf_get_opt("ipoe", "src");
	if (opt)
		conf_src = inet_addr(opt);
	else
		conf_src = 0;
3228 3229 3230 3231 3232 3233

	opt = conf_get_opt("ipoe", "proxy-arp");
	if (opt)
		conf_arp = atoi(opt);
	else
		conf_arp = 0;
D
Dmitry Kozlov 已提交
3234

3235 3236 3237 3238
	if (conf_arp < 0 || conf_arp > 2) {
		log_error("ipoe: arp=%s: invalid value\n", opt);
		conf_arp = 0;
	}
D
Dmitry Kozlov 已提交
3239

K
Kozlov Dmitry 已提交
3240 3241 3242 3243 3244 3245 3246 3247 3248 3249
	opt = conf_get_opt("ipoe", "mode");
	if (opt) {
		if (!strcmp(opt, "L2"))
			conf_mode = MODE_L2;
		else if (!strcmp(opt, "L3"))
			conf_mode = MODE_L3;
		else
			log_emerg("ipoe: failed to parse 'mode=%s'\n", opt);
	} else
		conf_mode = MODE_L2;
D
Dmitry Kozlov 已提交
3250

K
Kozlov Dmitry 已提交
3251
	conf_relay = conf_get_opt("ipoe", "relay");
3252 3253 3254 3255 3256 3257

	opt = conf_get_opt("ipoe", "relay-timeout");
	if (opt && atoi(opt) > 0)
		conf_relay_timeout = atoi(opt);
	else
		conf_relay_timeout = 3;
D
Dmitry Kozlov 已提交
3258

3259 3260 3261 3262 3263
	opt = conf_get_opt("ipoe", "relay-retransmit");
	if (opt && atoi(opt) > 0)
		conf_relay_retransmit = atoi(opt);
	else
		conf_relay_retransmit = 3;
D
Dmitry Kozlov 已提交
3264

3265 3266 3267 3268
	opt = conf_get_opt("ipoe", "agent-remote-id");
	if (opt)
		conf_agent_remote_id = opt;
	else
3269
		conf_agent_remote_id = NULL;
D
Dmitry Kozlov 已提交
3270

3271 3272 3273 3274 3275
	opt = conf_get_opt("ipoe", "ipv6");
	if (opt)
		conf_ipv6 = atoi(opt);
	else
		conf_ipv6 = 0;
D
Dmitry Kozlov 已提交
3276

K
Kozlov Dmitry 已提交
3277 3278 3279 3280 3281
	opt = conf_get_opt("ipoe", "noauth");
	if (opt)
		conf_noauth = atoi(opt);
	else
		conf_noauth = 0;
K
Kozlov Dmitry 已提交
3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296

	conf_dhcpv4 = 0;
	conf_up = 0;

	list_for_each_entry(opt1, &s->items, entry) {
		if (strcmp(opt1->name, "start"))
			continue;
		if (!strcmp(opt1->val, "dhcpv4"))
			conf_dhcpv4 = 1;
		else if (!strcmp(opt1->val, "up"))
			conf_up = 1;
	}

	if (!conf_dhcpv4 && !conf_up)
		conf_dhcpv4 = 1;
D
Dmitry Kozlov 已提交
3297

3298 3299 3300 3301
	opt = conf_get_opt("ipoe", "proto");
	if (opt && atoi(opt) > 0)
		conf_proto = atoi(opt);
	else
3302
		conf_proto = 3;
D
Dmitry Kozlov 已提交
3303

D
Dmitry Kozlov 已提交
3304 3305 3306 3307 3308
	opt = conf_get_opt("ipoe", "vlan-timeout");
	if (opt && atoi(opt) > 0)
		conf_vlan_timeout = atoi(opt);
	else
		conf_vlan_timeout = 60;
D
Dmitry Kozlov 已提交
3309

3310 3311 3312 3313 3314
	opt = conf_get_opt("ipoe", "offer-timeout");
	if (opt && atoi(opt) > 0)
		conf_offer_timeout = atoi(opt);
	else
		conf_offer_timeout = 10;
D
Dmitry Kozlov 已提交
3315

3316
	conf_ip_pool = conf_get_opt("ipoe", "ip-pool");
3317
	conf_l4_redirect_pool = conf_get_opt("ipoe", "l4-redirect-ip-pool");
3318 3319 3320 3321

	conf_vlan_name = conf_get_opt("ipoe", "vlan-name");
	if (!conf_vlan_name)
		conf_vlan_name = "%I.%N";
D
Dmitry Kozlov 已提交
3322

3323 3324 3325 3326 3327
	opt = conf_get_opt("ipoe", "ip-unnumbered");
	if (opt)
		conf_ip_unnumbered = atoi(opt);
	else
		conf_ip_unnumbered = 1;
D
Dmitry Kozlov 已提交
3328

3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340
	opt = conf_get_opt("ipoe", "idle-timeout");
	if (opt)
		conf_idle_timeout = atoi(opt);
	else
		conf_idle_timeout = 0;

	opt = conf_get_opt("ipoe", "session-timeout");
	if (opt)
		conf_session_timeout = atoi(opt);
	else
		conf_session_timeout = 0;

3341 3342 3343 3344 3345 3346
	opt = conf_get_opt("ipoe", "soft-terminate");
	if (opt)
		conf_soft_terminate = atoi(opt);
	else
		conf_soft_terminate = 0;

3347 3348 3349 3350 3351 3352
	opt = conf_get_opt("ipoe", "check-mac-change");
	if (opt)
		conf_check_mac_change = atoi(opt);
	else
		conf_check_mac_change = 1;

3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363
	opt = conf_get_opt("ipoe", "calling-sid");
	if (opt) {
		if (!strcmp(opt, "mac"))
			conf_calling_sid = SID_MAC;
		else if (!strcmp(opt, "ip"))
			conf_calling_sid = SID_IP;
		else
			log_error("ipoe: failed to parse 'calling-sid=%s'\n", opt);
	} else
		conf_calling_sid = SID_MAC;

3364 3365 3366 3367
#ifdef RADIUS
	if (triton_module_loaded("radius"))
		load_radius_attrs();
#endif
D
Dmitry Kozlov 已提交
3368 3369

	parse_offer_delay(conf_get_opt("ipoe", "offer-delay"));
D
Dmitry Kozlov 已提交
3370

K
Kozlov Dmitry 已提交
3371 3372
	load_interfaces(s);
	load_local_nets(s);
D
Dmitry Kozlov 已提交
3373
	load_vlan_mon(s);
3374
	load_gw_addr(s);
K
Kozlov Dmitry 已提交
3375 3376
}

3377 3378 3379 3380 3381 3382 3383 3384
static struct triton_context_t l4_redirect_ctx = {
	.close = l4_redirect_ctx_close,
};

static struct triton_timer_t l4_redirect_timer = {
	.expire = l4_redirect_list_timer,
};

K
Kozlov Dmitry 已提交
3385 3386 3387
static void ipoe_init(void)
{
	ses_pool = mempool_create(sizeof(struct ipoe_session));
D
Dmitry Kozlov 已提交
3388
	disc_item_pool = mempool_create(sizeof(struct disc_item));
3389
	req_item_pool = mempool_create(sizeof(struct request_item));
K
Kozlov Dmitry 已提交
3390
	uc_pool = mempool_create(sizeof(struct unit_cache));
3391

3392 3393 3394
	triton_context_register(&l4_redirect_ctx, NULL);
	triton_context_wakeup(&l4_redirect_ctx);

K
Kozlov Dmitry 已提交
3395 3396
	load_config();

3397 3398 3399
	if (conf_l4_redirect_ipset)
		ipset_flush(conf_l4_redirect_ipset);

K
Kozlov Dmitry 已提交
3400
	cli_register_simple_cmd2(show_stat_exec, NULL, 2, "show", "stat");
3401
	cli_show_ses_register("ipoe-type", "IPoE session type", print_session_type);
D
Dmitry Kozlov 已提交
3402

K
Kozlov Dmitry 已提交
3403
	triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);
3404 3405

#ifdef RADIUS
3406
	if (triton_module_loaded("radius")) {
3407 3408
		triton_event_register_handler(EV_RADIUS_ACCESS_ACCEPT, (triton_event_func)ev_radius_access_accept);
		triton_event_register_handler(EV_RADIUS_COA, (triton_event_func)ev_radius_coa);
3409
	}
3410
#endif
D
Dmitry Kozlov 已提交
3411

3412
	connlimit_loaded = triton_module_loaded("connlimit");
3413
	radius_loaded = triton_module_loaded("radius");
K
Kozlov Dmitry 已提交
3414 3415
}

3416
DEFINE_INIT(52, ipoe_init);