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

#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"

31
#include "iputils.h"
K
Kozlov Dmitry 已提交
32
#include "connlimit.h"
33 34 35
#ifdef RADIUS
#include "radius.h"
#endif
K
Kozlov Dmitry 已提交
36 37 38 39 40 41 42 43

#include "ipoe.h"

#include "memdebug.h"

#define USERNAME_IFNAME 0
#define USERNAME_LUA 1

K
Kozlov Dmitry 已提交
44 45 46
#define MODE_L2 0
#define MODE_L3 1

K
Kozlov Dmitry 已提交
47
static int conf_dhcpv4 = 1;
K
Kozlov Dmitry 已提交
48 49 50
static int conf_up = 0;
static int conf_mode = 0;
static int conf_shared = 1;
51
static int conf_ifcfg = 1;
52
static int conf_nat = 0;
53
static int conf_arp = 0;
54 55
static uint32_t conf_src;

K
Kozlov Dmitry 已提交
56 57
//static int conf_dhcpv6;
static int conf_username;
K
Kozlov Dmitry 已提交
58
static int conf_unit_cache;
K
Kozlov Dmitry 已提交
59
static int conf_noauth;
60 61 62 63 64 65 66
#ifdef RADIUS
static int conf_attr_dhcp_client_ip;
static int conf_attr_dhcp_router_ip;
static int conf_attr_dhcp_mask;
static int conf_attr_l4_redirect;
#endif
static int conf_l4_redirect_table;
67
static int conf_l4_redirect_on_reject;
K
Kozlov Dmitry 已提交
68
static const char *conf_relay;
K
Kozlov Dmitry 已提交
69 70 71 72 73 74

#ifdef USE_LUA
static const char *conf_lua_username_func;
#endif

static int conf_offer_timeout = 3;
75
static LIST_HEAD(conf_gw_addr);
K
Kozlov Dmitry 已提交
76 77 78 79
static int conf_netmask = 24;
static int conf_lease_time = 600;
static int conf_lease_timeout = 660;
static int conf_verbose;
80
static const char *conf_agent_remote_id;
81
static int conf_proto;
K
Kozlov Dmitry 已提交
82 83 84 85 86 87 88 89

static unsigned int stat_starting;
static unsigned int stat_active;

static mempool_t ses_pool;

static LIST_HEAD(serv_list);

90
struct ifaddr {
91 92 93 94 95
	struct list_head entry;
	in_addr_t addr;
	int refs;
};

96
struct iplink_arg {
K
Kozlov Dmitry 已提交
97 98 99 100
	pcre *re;
	const char *opt;
};

101
struct unit_cache {
K
Kozlov Dmitry 已提交
102 103 104 105
	struct list_head entry;
	int ifindex;
};

106
struct l4_redirect {
107 108 109 110 111 112
	struct list_head entry;
	int ifindex;
	in_addr_t addr;
	time_t timeout;
};

113 114 115 116
struct gw_addr {
	struct list_head entry;
	in_addr_t addr;
	int mask;
117
	int mask1;
118 119
};

K
Kozlov Dmitry 已提交
120 121 122 123 124
static pthread_mutex_t uc_lock = PTHREAD_MUTEX_INITIALIZER;
static LIST_HEAD(uc_list);
static int uc_size;
static mempool_t uc_pool;

125 126 127 128 129
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 已提交
130
static void ipoe_session_finished(struct ap_session *s);
K
Kozlov Dmitry 已提交
131
static void ipoe_drop_sessions(struct ipoe_serv *serv, struct ipoe_session *skip);
132
static void ipoe_serv_close(struct triton_context_t *ctx);
K
Kozlov Dmitry 已提交
133
static void __ipoe_session_activate(struct ipoe_session *ses);
134
static void ipoe_ses_recv_dhcpv4(struct dhcpv4_serv *dhcpv4, struct dhcpv4_packet *pack);
K
Kozlov Dmitry 已提交
135

K
Kozlov Dmitry 已提交
136
static struct ipoe_session *ipoe_session_lookup(struct ipoe_serv *serv, struct dhcpv4_packet *pack, struct ipoe_session **opt82_ses)
K
Kozlov Dmitry 已提交
137
{
K
Kozlov Dmitry 已提交
138
	struct ipoe_session *ses, *res = NULL;
K
Kozlov Dmitry 已提交
139 140 141
	
	uint8_t *agent_circuit_id = NULL;
	uint8_t *agent_remote_id = NULL;
K
Kozlov Dmitry 已提交
142 143 144 145
	int opt82_match;

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

K
Kozlov Dmitry 已提交
147 148 149 150
	if (pack->relay_agent && dhcpv4_parse_opt82(pack->relay_agent, &agent_circuit_id, &agent_remote_id)) {
		agent_circuit_id = NULL;
		agent_remote_id = NULL;
	}
K
Kozlov Dmitry 已提交
151

K
Kozlov Dmitry 已提交
152
	list_for_each_entry(ses, &serv->sessions, entry) {
153
		opt82_match = pack->relay_agent != NULL;
K
Kozlov Dmitry 已提交
154
		
K
Kozlov Dmitry 已提交
155 156
		if (opt82_match && agent_circuit_id && !ses->agent_circuit_id)
			opt82_match = 0;
K
Kozlov Dmitry 已提交
157
		
K
Kozlov Dmitry 已提交
158 159
		if (opt82_match && agent_remote_id && !ses->agent_remote_id)
			opt82_match = 0;
K
Kozlov Dmitry 已提交
160
		
K
Kozlov Dmitry 已提交
161 162 163 164 165
		if (opt82_match && !agent_circuit_id && ses->agent_circuit_id)
			opt82_match = 0;
		
		if (opt82_match && !agent_remote_id && ses->agent_remote_id)
			opt82_match = 0;
K
Kozlov Dmitry 已提交
166
		
K
Kozlov Dmitry 已提交
167
		if (opt82_match && agent_circuit_id) {
K
Kozlov Dmitry 已提交
168
			if (*agent_circuit_id != *ses->agent_circuit_id)
K
Kozlov Dmitry 已提交
169 170
				opt82_match = 0;
		
K
Kozlov Dmitry 已提交
171
			if (memcmp(agent_circuit_id + 1, ses->agent_circuit_id + 1, *agent_circuit_id))
K
Kozlov Dmitry 已提交
172
				opt82_match = 0;
K
Kozlov Dmitry 已提交
173 174
		}
		
K
Kozlov Dmitry 已提交
175
		if (opt82_match && agent_remote_id) {
K
Kozlov Dmitry 已提交
176
			if (*agent_remote_id != *ses->agent_remote_id)
K
Kozlov Dmitry 已提交
177 178
				opt82_match = 0;

K
Kozlov Dmitry 已提交
179
			if (memcmp(agent_remote_id + 1, ses->agent_remote_id + 1, *agent_remote_id))
K
Kozlov Dmitry 已提交
180
				opt82_match = 0;
K
Kozlov Dmitry 已提交
181
		}
K
Kozlov Dmitry 已提交
182 183 184

		if (opt82_match && opt82_ses)
			*opt82_ses = ses;
K
Kozlov Dmitry 已提交
185 186 187 188
			
		if (memcmp(pack->hdr->chaddr, ses->hwaddr, 6))
			continue;
	
K
Kozlov Dmitry 已提交
189 190
		res = ses;
		break;
K
Kozlov Dmitry 已提交
191
		
K
Kozlov Dmitry 已提交
192
		/*if (pack->client_id && !ses->client_id)
193 194 195 196 197
			continue;
		
		if (!pack->client_id && ses->client_id)
			continue;
		
K
Kozlov Dmitry 已提交
198 199 200 201 202 203 204
		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 已提交
205 206 207 208 209
		ses1 = ses;

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

K
Kozlov Dmitry 已提交
210
		return ses;*/
K
Kozlov Dmitry 已提交
211 212
	}

K
Kozlov Dmitry 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	if (!res || !pack->relay_agent || !opt82_ses || *opt82_ses)
		return res;
	
	list_for_each_entry(ses, &serv->sessions, entry) {
		if (agent_circuit_id && !ses->agent_circuit_id)
			continue;
		
		if (opt82_match && agent_remote_id && !ses->agent_remote_id)
			continue;
		
		if (opt82_match && !agent_circuit_id && ses->agent_circuit_id)
			continue;
		
		if (opt82_match && !agent_remote_id && ses->agent_remote_id)
			continue;
		
		if (opt82_match && agent_circuit_id) {
			if (*agent_circuit_id != *ses->agent_circuit_id)
				continue;
		
			if (memcmp(agent_circuit_id + 1, ses->agent_circuit_id + 1, *agent_circuit_id))
				continue;
		}
		
		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;
	}
			
	return res;
K
Kozlov Dmitry 已提交
250 251 252 253 254 255 256 257
}

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 已提交
258
	log_ppp_info2("ipoe: session timed out\n");
K
Kozlov Dmitry 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272

	ap_session_terminate(&ses->ses, TERM_LOST_CARRIER, 0);
}

static void ipoe_session_set_username(struct ipoe_session *ses)
{
#ifdef USE_LUA
	if (conf_username == USERNAME_LUA) {
		ipoe_lua_set_username(ses, conf_lua_username_func);
	} else
#endif
	ses->ses.username = _strdup(ses->ses.ifname);
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
static void l4_redirect_list_add(in_addr_t addr, int ifindex)
{
	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->ifindex = ifindex;
	n->timeout = ts.tv_sec + conf_l4_redirect_on_reject;
	
	ipoe_nl_modify(ifindex, addr, 1, NULL, NULL);
	iprule_add(addr, conf_l4_redirect_table);

	pthread_rwlock_wrlock(&l4_list_lock);
	
	list_add_tail(&n->entry, &l4_redirect_list);
	
	if (!l4_redirect_timer.tpd)
		triton_timer_add(&l4_redirect_ctx, &l4_redirect_timer, 0);

	pthread_rwlock_unlock(&l4_list_lock);	
}

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;
	struct unit_cache *uc;

	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);
			iprule_del(n->addr, conf_l4_redirect_table);

			if (uc_size < conf_unit_cache && ipoe_nl_modify(n->ifindex, 0, 0, "", NULL)) {
				uc = mempool_alloc(uc_pool);
				uc->ifindex = n->ifindex;
				pthread_mutex_lock(&uc_lock);
				list_add_tail(&uc->entry, &uc_list);
				++uc_size;
				pthread_mutex_unlock(&uc_lock);
			} else
				ipoe_nl_delete(n->ifindex);

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

354 355 356 357 358 359 360 361 362 363 364 365
static void ipoe_change_l4_redirect(struct ipoe_session *ses, int del)
{
	in_addr_t addr;
	
	if (conf_l4_redirect_table <= 0)
		return;

	if (ses->ses.ipv4)
		addr = ses->ses.ipv4->addr;
	else
		addr = ses->yiaddr;

K
Kozlov Dmitry 已提交
366
	if (del) {
367
		iprule_del(addr, conf_l4_redirect_table);
K
Kozlov Dmitry 已提交
368 369
		ses->l4_redirect_set = 0;
	} else {
370
		iprule_add(addr, conf_l4_redirect_table);
K
Kozlov Dmitry 已提交
371 372
		ses->l4_redirect_set = 1;
	}
373 374 375 376 377 378 379
}

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

}

K
Kozlov Dmitry 已提交
380
static void __ipoe_session_start(struct ipoe_session *ses);
K
Kozlov Dmitry 已提交
381 382 383 384
static void ipoe_session_start(struct ipoe_session *ses)
{
	int r;
	char *passwd;
385
	struct ifreq ifr;
K
Kozlov Dmitry 已提交
386
	struct unit_cache *uc;
K
Kozlov Dmitry 已提交
387 388

	if (!ses->ses.username) {
389
		strncpy(ses->ses.ifname, ses->serv->ifname, AP_IFNAME_LEN);
390
		
391 392 393 394 395 396
		ipoe_session_set_username(ses);

		if (!ses->ses.username) {
			ipoe_session_finished(&ses->ses);
			return;
		}
K
Kozlov Dmitry 已提交
397
	}
398 399

	ses->ses.unit_idx = ses->serv->ifindex;
K
Kozlov Dmitry 已提交
400 401 402 403 404 405
	
	triton_event_fire(EV_CTRL_STARTING, &ses->ses);
	triton_event_fire(EV_CTRL_STARTED, &ses->ses);

	ap_session_starting(&ses->ses);
	
K
Kozlov Dmitry 已提交
406 407 408 409 410 411 412 413 414 415
	if (!conf_noauth) {
		r = pwdb_check(&ses->ses, ses->ses.username, PPP_PAP, ses->ses.username);
		if (r == PWDB_NO_IMPL) {
			passwd = pwdb_get_passwd(&ses->ses, ses->ses.username);
			if (!passwd)
				r = PWDB_DENIED;
			else {
				r = PWDB_SUCCESS;
				_free(passwd);
			}
K
Kozlov Dmitry 已提交
416 417
		}

K
Kozlov Dmitry 已提交
418 419 420 421 422 423 424 425 426
		if (r == PWDB_DENIED) {
			if (conf_ppp_verbose)
				log_ppp_warn("authentication failed\n");
			if (conf_l4_redirect_on_reject && !ses->dhcpv4_request && ses->ifindex != -1) {
				l4_redirect_list_add(ses->yiaddr, ses->ifindex);
				ses->ifindex = -1;
			}
			ap_session_terminate(&ses->ses, TERM_AUTH_ERROR, 0);
			return;
427
		}
K
Kozlov Dmitry 已提交
428
	}
429 430 431

	if (ses->serv->opt_nat)
		ses->ses.ipv4 = ipdb_get_ipv4(&ses->ses);
K
Kozlov Dmitry 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	
	if (ses->serv->opt_shared == 0 && (!ses->ses.ipv4 || ses->ses.ipv4->peer_addr == ses->yiaddr)) {
		strncpy(ses->ses.ifname, ses->serv->ifname, AP_IFNAME_LEN);
		ses->ses.ifindex = ses->serv->ifindex;
	} else if (ses->ifindex == -1) {
		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");
				ipoe_session_finished(&ses->ses);
				return;
			}
		}

		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;
			ipoe_session_finished(&ses->ses);
			return;
		}

		strncpy(ses->ses.ifname, ifr.ifr_name, AP_IFNAME_LEN);
		ses->ses.ifindex = ses->ifindex;
466
		ses->ses.unit_idx = ses->ifindex;
467
		ses->ctrl.dont_ifcfg = 0;
K
Kozlov Dmitry 已提交
468 469 470
	}

	ap_session_set_ifindex(&ses->ses);
K
Kozlov Dmitry 已提交
471

K
Kozlov Dmitry 已提交
472
	if (ses->dhcpv4_request && ses->serv->dhcpv4_relay) {
473
		dhcpv4_relay_send(ses->serv->dhcpv4_relay, ses->dhcpv4_request, ses->relay_server_id, ses->serv->ifname, conf_agent_remote_id);
K
Kozlov Dmitry 已提交
474 475 476 477 478 479 480 481

		ses->timer.expire = ipoe_session_timeout;
		ses->timer.expire_tv.tv_sec = conf_offer_timeout;
		triton_timer_add(&ses->ctx, &ses->timer, 0);
	} else
		__ipoe_session_start(ses);
}

482 483 484 485 486
static void find_gw_addr(struct ipoe_session *ses)
{
	struct gw_addr *a;

	list_for_each_entry(a, &conf_gw_addr, entry) {
487
		if ((ntohl(ses->yiaddr) & (a->mask1)) == (ntohl(a->addr) & (a->mask1))) {
488 489 490 491 492 493 494
			ses->siaddr = a->addr;
			ses->mask = a->mask;
			return;
		}
	}
}

K
Kozlov Dmitry 已提交
495 496
static void __ipoe_session_start(struct ipoe_session *ses) 
{
497
	if (!ses->yiaddr) {
498
		dhcpv4_get_ip(ses->serv->dhcpv4, &ses->yiaddr, &ses->router, &ses->mask);
499 500 501
		if (ses->yiaddr)
			ses->dhcp_addr = 1;
	}
502

503 504 505 506 507 508
	if (!ses->yiaddr && !ses->serv->opt_nat)
		ses->ses.ipv4 = ipdb_get_ipv4(&ses->ses);

	if (!ses->mask)
		ses->mask = conf_netmask;

K
Kozlov Dmitry 已提交
509
	if (ses->ses.ipv4) {
510 511
		if (!ses->mask)
			ses->mask = ses->ses.ipv4->mask;
K
Kozlov Dmitry 已提交
512

K
Kozlov Dmitry 已提交
513 514
		if (!ses->yiaddr)
			ses->yiaddr = ses->ses.ipv4->peer_addr;
515 516 517
	
		if (!ses->router)
			ses->router = ses->ses.ipv4->addr;
K
Kozlov Dmitry 已提交
518
	} /*else if (ses->yiaddr) {
K
Kozlov Dmitry 已提交
519 520 521 522 523
		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 已提交
524
	}*/
525

K
Kozlov Dmitry 已提交
526 527 528 529 530 531 532 533 534
	if (ses->dhcpv4_request) {
		if (!ses->yiaddr) {
			log_ppp_error("no free IPv4 address\n");
			ap_session_terminate(&ses->ses, TERM_NAS_REQUEST, 0);
			return;
		}
			
		if (!ses->siaddr && ses->router != ses->yiaddr)
			ses->siaddr = ses->router;
535
		
536 537 538
		if (!ses->siaddr)
			find_gw_addr(ses);
		
539 540 541
		if (!ses->siaddr)
			ses->siaddr = ses->serv->opt_src;		

K
Kozlov Dmitry 已提交
542 543
		if (!ses->siaddr && ses->serv->dhcpv4_relay)
			ses->siaddr = ses->serv->dhcpv4_relay->giaddr;
544

K
Kozlov Dmitry 已提交
545 546 547 548 549
		if (!ses->siaddr) {
			log_ppp_error("can't determine Server-ID\n");
			ap_session_terminate(&ses->ses, TERM_NAS_ERROR, 0);
			return;
		}
550 551 552

		if (!ses->router)
			ses->router = ses->siaddr;
K
Kozlov Dmitry 已提交
553 554 555
				
		if (!ses->mask)
			ses->mask = 32;
K
Kozlov Dmitry 已提交
556
	
557
		dhcpv4_send_reply(DHCPOFFER, ses->serv->dhcpv4, ses->dhcpv4_request, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->dhcpv4_relay_reply);
K
Kozlov Dmitry 已提交
558 559 560

		dhcpv4_packet_free(ses->dhcpv4_request);
		ses->dhcpv4_request = NULL;
561 562 563 564
	
		ses->timer.expire = ipoe_session_timeout;
		ses->timer.expire_tv.tv_sec = conf_offer_timeout;
		triton_timer_add(&ses->ctx, &ses->timer, 0);
K
Kozlov Dmitry 已提交
565 566
	} else
		__ipoe_session_activate(ses);
K
Kozlov Dmitry 已提交
567 568
}

K
Kozlov Dmitry 已提交
569
static void ipoe_serv_add_addr(struct ipoe_serv *serv, in_addr_t addr)
570 571 572 573 574
{
	struct ifaddr *a;

	pthread_mutex_lock(&serv->lock);
	
K
Kozlov Dmitry 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	list_for_each_entry(a, &serv->addr_list, entry) {
		if (a->addr == addr) {
			a->refs++;
			pthread_mutex_unlock(&serv->lock);

			return;
		}
	}

	a = _malloc(sizeof(*a));
	a->addr = addr;
	a->refs = 1;
	list_add_tail(&a->entry, &serv->addr_list);

	if (ipaddr_add(serv->ifindex, a->addr, 32))
		log_warn("ipoe: failed to add addess to interface '%s'\n", serv->ifname);

	pthread_mutex_unlock(&serv->lock);
}

static void ipoe_serv_del_addr(struct ipoe_serv *serv, in_addr_t addr)
{
	struct ifaddr *a;

	pthread_mutex_lock(&serv->lock);

	list_for_each_entry(a, &serv->addr_list, entry) {
		if (a->addr == addr) {
			if (--a->refs == 0) {
				if (ipaddr_del(serv->ifindex, a->addr))
					log_warn("ipoe: failed to delete addess from interface '%s'\n", serv->ifname);
				list_del(&a->entry);
				_free(a);
608
			}
K
Kozlov Dmitry 已提交
609
			break;
610
		}
K
Kozlov Dmitry 已提交
611 612 613 614 615 616 617 618 619
	}
	
	pthread_mutex_unlock(&serv->lock);
}

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

K
Kozlov Dmitry 已提交
620
	if (ses->serv->opt_ifcfg) {
K
Kozlov Dmitry 已提交
621 622 623 624 625 626 627 628
		if (ses->serv->opt_shared)
			ipoe_serv_add_addr(ses->serv, ses->siaddr);
		else {
			pthread_mutex_lock(&serv->lock);
			if (ipaddr_add(serv->ifindex, ses->siaddr, 32))
				log_ppp_warn("ipoe: failed to add addess to interface '%s'\n", serv->ifname);
			pthread_mutex_unlock(&serv->lock);
		}
629
		if (iproute_add(serv->ifindex, ses->serv->opt_src ? ses->serv->opt_src : ses->router, ses->yiaddr, conf_proto))
K
Kozlov Dmitry 已提交
630
			log_ppp_warn("ipoe: failed to add route to interface '%s'\n", serv->ifname);
631
	} else if (iproute_add(serv->ifindex, ses->serv->opt_src ? ses->serv->opt_src : ses->router, ses->yiaddr, conf_proto))
632 633 634 635 636 637 638 639
		log_ppp_warn("ipoe: failed to add route to interface '%s'\n", serv->ifname);

	ses->ifcfg = 1;
}

static void ipoe_ifcfg_del(struct ipoe_session *ses)
{
	struct ipoe_serv *serv = ses->serv;
K
Kozlov Dmitry 已提交
640 641 642
	
	if (iproute_del(serv->ifindex, ses->yiaddr))
		log_ppp_warn("ipoe: failed to delete route from interface '%s'\n", serv->ifname);
643

K
Kozlov Dmitry 已提交
644
	if (ses->serv->opt_ifcfg) {
K
Kozlov Dmitry 已提交
645 646 647 648 649 650 651 652
		if (ses->serv->opt_shared) {
			ipoe_serv_del_addr(ses->serv, ses->siaddr);
		} else {
			pthread_mutex_lock(&serv->lock);
			if (ipaddr_del(serv->ifindex, ses->siaddr))
				log_ppp_warn("ipoe: failed to remove addess from interface '%s'\n", serv->ifname);
			pthread_mutex_unlock(&serv->lock);
		}
K
Kozlov Dmitry 已提交
653
	}
654 655
}

K
Kozlov Dmitry 已提交
656
static void __ipoe_session_activate(struct ipoe_session *ses)
K
Kozlov Dmitry 已提交
657
{
658 659
	uint32_t addr;

660 661
	if (ses->terminating)
		return;
662
	
663
	if (ses->ifindex != -1) {
K
Kozlov Dmitry 已提交
664
		addr = 0;
665 666 667 668 669 670
		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)
671
			addr = ses->ses.ipv4->peer_addr;
K
Kozlov Dmitry 已提交
672
		
673
		if (ipoe_nl_modify(ses->ifindex, ses->yiaddr, addr, NULL, NULL)) {
674 675 676
			ap_session_terminate(&ses->ses, TERM_NAS_ERROR, 0);
			return;
		}
K
Kozlov Dmitry 已提交
677
	}
678
	
679 680 681 682 683 684 685 686
	if (!ses->ses.ipv4) {
		ses->ses.ipv4 = &ses->ipv4;
		ses->ipv4.owner = NULL;
		ses->ipv4.peer_addr = ses->yiaddr;
		ses->ipv4.addr = ses->siaddr;
	}
	
	if (ses->ifindex == -1 && (ses->serv->opt_ifcfg || (ses->serv->opt_mode == MODE_L2)))
687
		ipoe_ifcfg_add(ses);
688 689 690
	
	if (ses->l4_redirect)
		ipoe_change_l4_redirect(ses, 0);
691

K
Kozlov Dmitry 已提交
692 693
	ap_session_activate(&ses->ses);

694 695
	if (ses->dhcpv4_request) {
		if (ses->ses.state == AP_STATE_ACTIVE)
696
			dhcpv4_send_reply(DHCPACK, ses->serv->dhcpv4, ses->dhcpv4_request, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->dhcpv4_relay_reply);
697 698
		else
			dhcpv4_send_nak(ses->serv->dhcpv4, ses->dhcpv4_request);
K
Kozlov Dmitry 已提交
699

700 701 702
		dhcpv4_packet_free(ses->dhcpv4_request);
		ses->dhcpv4_request = NULL;
	}
K
Kozlov Dmitry 已提交
703 704 705 706 707
	
	ses->timer.expire = ipoe_session_timeout;
	ses->timer.expire_tv.tv_sec = conf_lease_timeout ? conf_lease_timeout : ses->lease_time;
	if (ses->timer.tpd)
		triton_timer_mod(&ses->timer, 0);
K
Kozlov Dmitry 已提交
708 709
}

K
Kozlov Dmitry 已提交
710 711 712
static void ipoe_session_activate(struct ipoe_session *ses)
{
	if (ses->serv->dhcpv4_relay)
713
		dhcpv4_relay_send(ses->serv->dhcpv4_relay, ses->dhcpv4_request, ses->relay_server_id, ses->serv->ifname, conf_agent_remote_id);
K
Kozlov Dmitry 已提交
714 715 716 717 718
	else
		__ipoe_session_activate(ses);
}

static void ipoe_session_keepalive(struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
719
{
K
Kozlov Dmitry 已提交
720 721 722 723 724 725 726
	struct ipoe_session *ses = container_of(triton_context_self(), typeof(*ses), ctx);

	if (ses->dhcpv4_request)
		dhcpv4_packet_free(ses->dhcpv4_request);
	
	ses->dhcpv4_request = pack;

K
Kozlov Dmitry 已提交
727 728 729 730
	if (ses->timer.tpd)
		triton_timer_mod(&ses->timer, 0);

	ses->xid = ses->dhcpv4_request->hdr->xid;
K
Kozlov Dmitry 已提交
731
	
732
	if (/*ses->ses.state == AP_STATE_ACTIVE &&*/ ses->serv->dhcpv4_relay) {
733
		dhcpv4_relay_send(ses->serv->dhcpv4_relay, ses->dhcpv4_request, ses->relay_server_id, ses->serv->ifname, conf_agent_remote_id);
K
Kozlov Dmitry 已提交
734 735
		return;
	}
K
Kozlov Dmitry 已提交
736

K
Kozlov Dmitry 已提交
737
	if (ses->ses.state == AP_STATE_ACTIVE) {
738
		dhcpv4_send_reply(DHCPACK, ses->serv->dhcpv4, ses->dhcpv4_request, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->dhcpv4_relay_reply);
K
Kozlov Dmitry 已提交
739
	} else
K
Kozlov Dmitry 已提交
740 741 742 743 744
		dhcpv4_send_nak(ses->serv->dhcpv4, ses->dhcpv4_request);

	dhcpv4_packet_free(ses->dhcpv4_request);
	ses->dhcpv4_request = NULL;
}
K
Kozlov Dmitry 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
			
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);
	}
	
	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);

	ap_session_terminate(&ses->ses, TERM_USER_REQUEST, 0);
}
K
Kozlov Dmitry 已提交
762

K
Kozlov Dmitry 已提交
763 764 765 766
static void ipoe_session_started(struct ap_session *s)
{
	struct ipoe_session *ses = container_of(s, typeof(*ses), ses);
	
767
	log_ppp_info1("ipoe: session started\n");
K
Kozlov Dmitry 已提交
768

K
Kozlov Dmitry 已提交
769 770
	if (ses->timer.tpd)
		triton_timer_mod(&ses->timer, 0);
771 772 773 774 775 776 777 778 779
	
	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 已提交
780 781 782 783
}

static void ipoe_session_free(struct ipoe_session *ses)
{
K
Kozlov Dmitry 已提交
784 785
	struct unit_cache *uc;

K
Kozlov Dmitry 已提交
786 787 788
	if (ses->timer.tpd)
		triton_timer_del(&ses->timer);

K
Kozlov Dmitry 已提交
789 790 791
	if (ses->dhcpv4_request)
		dhcpv4_packet_free(ses->dhcpv4_request);
	
792 793 794 795 796 797
	if (ses->ctrl.called_station_id)
		_free(ses->ctrl.called_station_id);
	
	if (ses->ctrl.calling_station_id)
		_free(ses->ctrl.calling_station_id);

K
Kozlov Dmitry 已提交
798 799 800 801
	triton_context_unregister(&ses->ctx);
	
	if (ses->data)
		_free(ses->data);
802
	
K
Kozlov Dmitry 已提交
803 804 805 806 807 808 809 810 811 812 813
	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);
	}
K
Kozlov Dmitry 已提交
814 815 816 817 818 819 820

	mempool_free(ses);
}

static void ipoe_session_finished(struct ap_session *s)
{
	struct ipoe_session *ses = container_of(s, typeof(*ses), ses);
821
	int serv_close;
K
Kozlov Dmitry 已提交
822

823
	log_ppp_info1("ipoe: session finished\n");
K
Kozlov Dmitry 已提交
824 825 826

	pthread_mutex_lock(&ses->serv->lock);
	list_del(&ses->entry);
827
	serv_close = ses->serv->need_close && list_empty(&ses->serv->sessions);
K
Kozlov Dmitry 已提交
828 829
	pthread_mutex_unlock(&ses->serv->lock);

830
	if (ses->dhcp_addr)
831
		dhcpv4_put_ip(ses->serv->dhcpv4, ses->yiaddr);
K
Kozlov Dmitry 已提交
832 833
	
	if (ses->relay_addr && ses->serv->dhcpv4_relay)
834
		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);
835

836 837 838
	if (ses->ifcfg)
		ipoe_ifcfg_del(ses);

839 840
	if (serv_close)
		ipoe_serv_close(&ses->serv->ctx);
841 842 843
	
	if (ses->dhcpv4)
		dhcpv4_free(ses->dhcpv4);
K
Kozlov Dmitry 已提交
844
	
K
Kozlov Dmitry 已提交
845 846 847 848 849
	triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_free, ses);
}

static void ipoe_session_terminate(struct ap_session *s, int hard)
{
850 851
	struct ipoe_session *ses = container_of(s, typeof(*ses), ses);

K
Kozlov Dmitry 已提交
852
	if (ses->l4_redirect_set)
853 854
		ipoe_change_l4_redirect(ses, 1);

K
Kozlov Dmitry 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868
	ap_session_finished(s);
}


static void ipoe_session_close(struct triton_context_t *ctx)
{
	struct ipoe_session *ses = container_of(ctx, typeof(*ses), ctx);
	
	if (ses->ses.state)
		ap_session_terminate(&ses->ses, TERM_ADMIN_RESET, 1);
	else
		ipoe_session_finished(&ses->ses);
}

869
static struct ipoe_session *ipoe_session_create_dhcpv4(struct ipoe_serv *serv, struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
870 871 872 873
{
	struct ipoe_session *ses;
	int dlen = 0;
	uint8_t *ptr;
K
Kozlov Dmitry 已提交
874
	
K
Kozlov Dmitry 已提交
875 876 877 878 879 880 881 882 883 884 885
	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->serv = serv;
886
	ses->ifindex = -1;
K
Kozlov Dmitry 已提交
887 888 889 890 891
	ses->dhcpv4_request = pack;
	
	ses->xid = pack->hdr->xid;
	memcpy(ses->hwaddr, pack->hdr->chaddr, 6);
	ses->giaddr = pack->hdr->giaddr;
K
Kozlov Dmitry 已提交
892
	ses->lease_time = conf_lease_time;
K
Kozlov Dmitry 已提交
893 894

	if (pack->client_id)
K
Kozlov Dmitry 已提交
895 896 897 898
		dlen += sizeof(struct dhcpv4_option) + pack->client_id->len;
	
	if (pack->relay_agent)
		dlen += sizeof(struct dhcpv4_option) + pack->relay_agent->len;
K
Kozlov Dmitry 已提交
899 900 901 902 903 904 905 906 907 908 909 910
	
	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 已提交
911
		ses->client_id = (struct dhcpv4_option *)ptr;
K
Kozlov Dmitry 已提交
912
		ses->client_id->len = pack->client_id->len;
913
		ses->client_id->data = (uint8_t *)(ses->client_id + 1);
K
Kozlov Dmitry 已提交
914
		memcpy(ses->client_id->data, pack->client_id->data, pack->client_id->len);
K
Kozlov Dmitry 已提交
915 916 917 918 919 920
		ptr += sizeof(struct dhcpv4_option) + pack->client_id->len;
	}
	
	if (pack->relay_agent) {
		ses->relay_agent = (struct dhcpv4_option *)ptr;
		ses->relay_agent->len = pack->relay_agent->len;
921
		ses->relay_agent->data = (uint8_t *)(ses->relay_agent + 1);
K
Kozlov Dmitry 已提交
922 923 924 925
		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 已提交
926 927 928 929 930 931 932 933 934 935
	}

	ses->ctx.before_switch = log_switch;
	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";
936
	ses->ctrl.dont_ifcfg = 1;
K
Kozlov Dmitry 已提交
937 938
	
	ses->ctrl.calling_station_id = _malloc(19);
939
	ses->ctrl.called_station_id = _strdup(serv->ifname);
K
Kozlov Dmitry 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
	
	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]);
	
	ses->ses.ctrl = &ses->ctrl;
	ses->ses.chan_name = ses->ctrl.calling_station_id;

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

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

	return ses;
}

K
Kozlov Dmitry 已提交
961 962 963 964 965
static void __ipoe_session_terminate(struct ap_session *ses)
{
	ap_session_terminate(ses, TERM_USER_REQUEST, 0);
}

966 967 968
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 已提交
969 970 971
	int opt82_match;
	uint8_t *agent_circuit_id = NULL;
	uint8_t *agent_remote_id = NULL;
972 973 974 975 976 977 978 979

	if (ap_shutdown)
		return;
			
	if (conf_verbose) {
		log_ppp_info2("recv ");
		dhcpv4_print_packet(pack, 0, log_info2);
	}
K
Kozlov Dmitry 已提交
980 981 982 983 984 985

	if (pack->relay_agent && dhcpv4_parse_opt82(pack->relay_agent, &agent_circuit_id, &agent_remote_id)) {
		agent_circuit_id = NULL;
		agent_remote_id = NULL;
	}

986
	opt82_match = pack->relay_agent == NULL;
K
Kozlov Dmitry 已提交
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	
	if (opt82_match && agent_circuit_id && !ses->agent_circuit_id)
		opt82_match = 0;
	
	if (opt82_match && agent_remote_id && !ses->agent_remote_id)
		opt82_match = 0;
	
	if (opt82_match && !agent_circuit_id && ses->agent_circuit_id)
		opt82_match = 0;
	
	if (opt82_match && !agent_remote_id && ses->agent_remote_id)
		opt82_match = 0;
	
	if (opt82_match && agent_circuit_id) {
		if (*agent_circuit_id != *ses->agent_circuit_id)
			opt82_match = 0;
	
		if (memcmp(agent_circuit_id + 1, ses->agent_circuit_id + 1, *agent_circuit_id))
			opt82_match = 0;
	}
	
	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;
	}

	if (!opt82_match) {
		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;
	}
1023 1024 1025 1026 1027
			
	if (pack->msg_type == DHCPDISCOVER) {
		if (ses->yiaddr) {
			if (ses->serv->dhcpv4_relay) {
				dhcpv4_packet_ref(pack);
K
Kozlov Dmitry 已提交
1028
				ipoe_session_keepalive(pack);
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
			} else
				dhcpv4_send_reply(DHCPOFFER, dhcpv4, pack, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->dhcpv4_relay_reply);
		}
	} else if (pack->msg_type == DHCPREQUEST) {
		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);
			
K
Kozlov Dmitry 已提交
1043
			triton_context_call(ses->ctrl.ctx, (triton_event_func)__ipoe_session_terminate, &ses->ses);
1044 1045 1046 1047 1048 1049
		} 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 已提交
1050
		triton_context_call(ses->ctrl.ctx, (triton_event_func)ipoe_session_decline, pack);
1051 1052 1053
	}
}

1054
static void ipoe_recv_dhcpv4(struct dhcpv4_serv *dhcpv4, struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
1055 1056
{
	struct ipoe_serv *serv = container_of(dhcpv4->ctx, typeof(*serv), ctx);
K
Kozlov Dmitry 已提交
1057
	struct ipoe_session *ses, *opt82_ses;
K
Kozlov Dmitry 已提交
1058 1059
	//struct dhcpv4_packet *reply;

1060 1061 1062
	if (ap_shutdown)
		return;

K
Kozlov Dmitry 已提交
1063 1064
	pthread_mutex_lock(&serv->lock);
	if (pack->msg_type == DHCPDISCOVER) {
K
Kozlov Dmitry 已提交
1065
		ses = ipoe_session_lookup(serv, pack, &opt82_ses);
K
Kozlov Dmitry 已提交
1066
		if (!ses) {
1067 1068
			if (serv->opt_shared == 0)
				ipoe_drop_sessions(serv, NULL);
K
Kozlov Dmitry 已提交
1069 1070 1071 1072 1073 1074
			else if (opt82_ses) {
				if (conf_verbose)
					log_ppp_warn("mac change detected\n");

				triton_context_call(&opt82_ses->ctx, (triton_event_func)__ipoe_session_terminate, &opt82_ses->ses);
			}
1075

1076
			ses = ipoe_session_create_dhcpv4(serv, pack);
K
Kozlov Dmitry 已提交
1077 1078
			if (ses) {
				dhcpv4_packet_ref(pack);
K
Kozlov Dmitry 已提交
1079

K
Kozlov Dmitry 已提交
1080 1081 1082 1083 1084
				if (conf_verbose) {
					log_switch(dhcpv4->ctx, &ses->ses);
					log_ppp_info2("recv ");
					dhcpv4_print_packet(pack, 0, log_ppp_info2);
				}
K
Kozlov Dmitry 已提交
1085 1086
			}
		}	else {
K
Kozlov Dmitry 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
			if ((opt82_ses && ses != opt82_ses) || (!opt82_ses && pack->relay_agent)) {
				if (conf_verbose) {
					log_switch(dhcpv4->ctx, &ses->ses);
					log_ppp_info2("recv ");
					dhcpv4_print_packet(pack, 0, log_ppp_info2);
					log_ppp_warn("port change detected\n");
				}

				triton_context_call(&ses->ctx, (triton_event_func)__ipoe_session_terminate, &ses->ses);
				goto out;
			}

K
Kozlov Dmitry 已提交
1099 1100 1101 1102
			log_switch(dhcpv4->ctx, &ses->ses);

			if (conf_verbose) {
				log_ppp_info2("recv ");
K
Kozlov Dmitry 已提交
1103
				dhcpv4_print_packet(pack, 0, log_ppp_info2);
K
Kozlov Dmitry 已提交
1104 1105
			}

1106 1107 1108 1109 1110 1111 1112
			if (ses->yiaddr) {
				if (ses->serv->dhcpv4_relay) {
					dhcpv4_packet_ref(pack);
					triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_keepalive, pack);
				} else
					dhcpv4_send_reply(DHCPOFFER, dhcpv4, pack, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->dhcpv4_relay_reply);
			}
K
Kozlov Dmitry 已提交
1113 1114
		}
	} else if (pack->msg_type == DHCPREQUEST) {
K
Kozlov Dmitry 已提交
1115
		ses = ipoe_session_lookup(serv, pack, &opt82_ses);
K
Kozlov Dmitry 已提交
1116 1117 1118 1119

		if (!ses) {
			if (conf_verbose) {
				log_info2("recv ");
K
Kozlov Dmitry 已提交
1120
				dhcpv4_print_packet(pack, 0, log_info2);
K
Kozlov Dmitry 已提交
1121
			}
1122 1123 1124
				
			if (serv->opt_shared == 0)
				ipoe_drop_sessions(serv, NULL);
K
Kozlov Dmitry 已提交
1125 1126 1127 1128 1129 1130 1131 1132
			else if (opt82_ses) {
				if (conf_verbose) {
					log_switch(dhcpv4->ctx, &opt82_ses->ses);
					log_ppp_warn("mac change detected\n");
				}
				
				triton_context_call(&opt82_ses->ctx, (triton_event_func)__ipoe_session_terminate, &opt82_ses->ses);
			}
K
Kozlov Dmitry 已提交
1133 1134 1135

			dhcpv4_send_nak(dhcpv4, pack);
		} else {
K
Kozlov Dmitry 已提交
1136 1137
			if (pack->hdr->ciaddr == ses->yiaddr && pack->hdr->xid != ses->xid)
				ses->xid = pack->hdr->xid;
1138
			if ((pack->server_id && (pack->server_id != ses->siaddr || pack->request_ip != ses->yiaddr)) ||
K
Kozlov Dmitry 已提交
1139 1140
				(pack->hdr->ciaddr && (pack->hdr->xid != ses->xid || pack->hdr->ciaddr != ses->yiaddr)) ||
				(opt82_ses && ses != opt82_ses) || (!opt82_ses && pack->relay_agent)) {
K
Kozlov Dmitry 已提交
1141

K
Kozlov Dmitry 已提交
1142
				if (conf_verbose) {
K
Kozlov Dmitry 已提交
1143 1144
					log_switch(dhcpv4->ctx, &ses->ses);
					log_ppp_info2("recv ");
K
Kozlov Dmitry 已提交
1145
					dhcpv4_print_packet(pack, 0, log_info2);
K
Kozlov Dmitry 已提交
1146 1147
					if ((opt82_ses && ses != opt82_ses) || (!opt82_ses && pack->relay_agent))
						log_ppp_warn("port change detected\n");
K
Kozlov Dmitry 已提交
1148 1149
				}

K
Kozlov Dmitry 已提交
1150
				if (pack->server_id == ses->siaddr)
K
Kozlov Dmitry 已提交
1151
					dhcpv4_send_nak(dhcpv4, pack);
K
Kozlov Dmitry 已提交
1152
				else if (ses->serv->dhcpv4_relay)
1153
					dhcpv4_relay_send(ses->serv->dhcpv4_relay, pack, 0, ses->serv->ifname, conf_agent_remote_id);
K
Kozlov Dmitry 已提交
1154 1155
				
				triton_context_call(&ses->ctx, (triton_event_func)__ipoe_session_terminate, &ses->ses);
K
Kozlov Dmitry 已提交
1156 1157 1158 1159
			} else {
				if (conf_verbose) {
					log_switch(dhcpv4->ctx, &ses->ses);
					log_ppp_info2("recv ");
K
Kozlov Dmitry 已提交
1160
					dhcpv4_print_packet(pack, 0, log_ppp_info2);
K
Kozlov Dmitry 已提交
1161 1162
				}

K
Kozlov Dmitry 已提交
1163
				if (serv->opt_shared == 0)
K
Kozlov Dmitry 已提交
1164 1165
					ipoe_drop_sessions(serv, ses);

K
Kozlov Dmitry 已提交
1166
				if (ses->ses.state == AP_STATE_STARTING && ses->yiaddr && !ses->dhcpv4_request) {
K
Kozlov Dmitry 已提交
1167
					ses->dhcpv4_request = pack;
K
Kozlov Dmitry 已提交
1168
					dhcpv4_packet_ref(pack);
K
Kozlov Dmitry 已提交
1169
					triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_activate, ses);
K
Kozlov Dmitry 已提交
1170 1171 1172
				} else if (ses->ses.state == AP_STATE_ACTIVE) {
					dhcpv4_packet_ref(pack);
					triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_keepalive, pack);
K
Kozlov Dmitry 已提交
1173 1174 1175 1176
				}
			}
		}
	} else if (pack->msg_type == DHCPDECLINE || pack->msg_type == DHCPRELEASE) {
K
Kozlov Dmitry 已提交
1177
		ses = ipoe_session_lookup(serv, pack, &opt82_ses);
K
Kozlov Dmitry 已提交
1178
		if (ses) {
K
Kozlov Dmitry 已提交
1179 1180
			dhcpv4_packet_ref(pack);
			triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_decline, pack);
K
Kozlov Dmitry 已提交
1181
		}
K
Kozlov Dmitry 已提交
1182
	}
K
Kozlov Dmitry 已提交
1183 1184

out:
K
Kozlov Dmitry 已提交
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
	pthread_mutex_unlock(&serv->lock);
}

static int parse_dhcpv4_mask(uint32_t mask)
{
	int i;

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

	return 32 - (i + 1);
}

static void ipoe_ses_recv_dhcpv4_relay(struct ipoe_session *ses)
{
	struct dhcpv4_packet *pack = ses->dhcpv4_relay_reply;
	struct dhcpv4_option *opt;

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

1207 1208 1209
	opt = dhcpv4_packet_find_opt(pack, 51);
	if (opt)
		ses->lease_time = ntohl(*(uint32_t *)opt->data);
K
Kozlov Dmitry 已提交
1210

1211 1212 1213
	opt = dhcpv4_packet_find_opt(pack, 1);
	if (opt)
		ses->mask = parse_dhcpv4_mask(ntohl(*(uint32_t *)opt->data));
K
Kozlov Dmitry 已提交
1214

1215 1216 1217
	if (pack->msg_type == DHCPOFFER) {
		if (ses->ses.state == AP_STATE_STARTING) {
			triton_timer_del(&ses->timer);
K
Kozlov Dmitry 已提交
1218

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
			ses->relay_server_id = pack->server_id;

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

			__ipoe_session_start(ses);
		} else
			dhcpv4_send_reply(DHCPOFFER, ses->serv->dhcpv4, ses->dhcpv4_request, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->dhcpv4_relay_reply);
K
Kozlov Dmitry 已提交
1229 1230 1231 1232
	} else if (pack->msg_type == DHCPACK) {
		if (ses->ses.state == AP_STATE_STARTING)
			__ipoe_session_activate(ses);
		else
1233
			dhcpv4_send_reply(DHCPACK, ses->serv->dhcpv4, ses->dhcpv4_request, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->dhcpv4_relay_reply);
K
Kozlov Dmitry 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252

	} else if (pack->msg_type == DHCPNAK) {
		dhcpv4_send_nak(ses->serv->dhcpv4, ses->dhcpv4_request);
		ap_session_terminate(&ses->ses, TERM_NAS_REQUEST, 0);
		return;
	}
		
	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 已提交
1253
		dhcpv4_packet_free(pack);
K
Kozlov Dmitry 已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
		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 已提交
1266
	}
K
Kozlov Dmitry 已提交
1267 1268 1269 1270 1271 1272 1273
	
	if (found && !ses->dhcpv4_relay_reply) {
		ses->dhcpv4_relay_reply = pack;
		triton_context_call(&ses->ctx, (triton_event_func)ipoe_ses_recv_dhcpv4_relay, ses);
	} else
		dhcpv4_packet_free(pack);

K
Kozlov Dmitry 已提交
1274 1275 1276
	pthread_mutex_unlock(&serv->lock);
}

K
Kozlov Dmitry 已提交
1277

1278 1279 1280 1281
static struct ipoe_session *ipoe_session_create_up(struct ipoe_serv *serv, struct ethhdr *eth, struct iphdr *iph)
{
	struct ipoe_session *ses;

1282 1283
	if (ap_shutdown)
		return NULL;
1284 1285 1286 1287
	
	if (l4_redirect_list_check(iph->saddr))
		return NULL;
	
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
	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->serv = serv;
	ses->ifindex = -1;
	
	memcpy(ses->hwaddr, eth->h_source, 6);

	ses->ctx.before_switch = log_switch;
	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";
K
Kozlov Dmitry 已提交
1311

1312
	ses->yiaddr = iph->saddr;
K
Kozlov Dmitry 已提交
1313

1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
	ses->ctrl.calling_station_id = _malloc(17);
	ses->ctrl.called_station_id = _malloc(17);

	u_inet_ntoa(iph->saddr, ses->ctrl.calling_station_id);
	u_inet_ntoa(iph->daddr, ses->ctrl.called_station_id);
	
	ses->ses.username = _strdup(ses->ctrl.calling_station_id);
	
	ses->ses.ctrl = &ses->ctrl;
	ses->ses.chan_name = ses->ctrl.calling_station_id;

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

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

	return ses;
}

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
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;
	
	ses->ctx.before_switch = log_switch;
	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";

	ses->ses.ctrl = &ses->ctrl;
	ses->ses.chan_name = ses->ctrl.calling_station_id;

	return ses;
}

1369 1370 1371 1372 1373 1374 1375 1376
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 已提交
1377 1378 1379

		if (!serv->opt_up)
			return;
1380 1381 1382
		
		pthread_mutex_lock(&serv->lock);
		list_for_each_entry(ses, &serv->sessions, entry) {
1383
			if (ses->yiaddr == iph->saddr) {
1384 1385 1386 1387 1388 1389 1390
				pthread_mutex_unlock(&serv->lock);
				return;
			}
		}
		pthread_mutex_unlock(&serv->lock);
		
		ipoe_session_create_up(serv, eth, iph);
1391 1392

		break;
1393 1394 1395
	}
}

1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
#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;

	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)
1409
			ses->router = attr->val.ipaddr;
1410
		else if (attr->attr->id == conf_attr_dhcp_mask) {
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
			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) {
#if __BYTE_ORDER == __LITTLE_ENDIAN
				ses->mask = ffs(~attr->val.ipaddr) - 1;
#else
				ses->mask = ffs(~htole32(attr->val.ipaddr)) - 1;
#endif
			}
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
		} 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;
		}
	}
}

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;
	
	if (ev->ses->ctrl->type != CTRL_TYPE_IPOE)
		return;
	
	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);
		}
	}

	//if (l4_redirect && !ses->l4_redirect) || (!l4_redirect && ses->l4_redirect))
K
Kozlov Dmitry 已提交
1455
	if (l4_redirect != ses->l4_redirect && ev->ses->state == AP_STATE_ACTIVE)
1456 1457 1458 1459
		ipoe_change_l4_redirect(ses, l4_redirect);
}
#endif

K
Kozlov Dmitry 已提交
1460 1461 1462 1463
static void ipoe_serv_close(struct triton_context_t *ctx)
{
	struct ipoe_serv *serv = container_of(ctx, typeof(*serv), ctx);

1464 1465 1466 1467 1468 1469 1470 1471
	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);

K
Kozlov Dmitry 已提交
1472 1473
	if (serv->dhcpv4)
		dhcpv4_free(serv->dhcpv4);
K
Kozlov Dmitry 已提交
1474 1475 1476 1477 1478
	
	if (serv->dhcpv4_relay) {
		ipoe_serv_del_addr(serv, serv->dhcpv4_relay->giaddr);
		dhcpv4_relay_free(serv->dhcpv4_relay, &serv->ctx);
	}
K
Kozlov Dmitry 已提交
1479

1480 1481 1482
	if (serv->arp)
		arpd_stop(serv->arp);

K
Kozlov Dmitry 已提交
1483 1484 1485 1486 1487 1488
	triton_context_unregister(ctx);

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

1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
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);
		iprule_del(n->addr, conf_l4_redirect_table);
		ipoe_nl_delete(n->ifindex);
		_free(n);
	}
	pthread_rwlock_unlock(&l4_list_lock);

	if (l4_redirect_timer.tpd)
		triton_timer_del(&l4_redirect_timer);
	
	triton_context_unregister(&l4_redirect_ctx);
}

K
Kozlov Dmitry 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
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);

	return CLI_CMD_OK;
}

void __export ipoe_get_stat(unsigned int **starting, unsigned int **active)
{
	*starting = &stat_starting;
	*active = &stat_active;
}

K
Kozlov Dmitry 已提交
1524 1525 1526 1527 1528 1529
static void __terminate(struct ap_session *ses)
{
	ap_session_terminate(ses, TERM_NAS_REQUEST, 0);
}

static void ipoe_drop_sessions(struct ipoe_serv *serv, struct ipoe_session *skip)
K
Kozlov Dmitry 已提交
1530
{
K
Kozlov Dmitry 已提交
1531 1532 1533 1534 1535 1536
	struct ipoe_session *ses;

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

1537 1538 1539 1540 1541 1542
		ses->terminating = 1;
		if (ses->ifcfg) {
			ipoe_ifcfg_del(ses);
			ses->ifcfg = 0;
		}

K
Kozlov Dmitry 已提交
1543 1544
		if (ses->ses.state == AP_STATE_ACTIVE)
			ap_session_ifdown(&ses->ses);
K
Kozlov Dmitry 已提交
1545

K
Kozlov Dmitry 已提交
1546 1547
		triton_context_call(&ses->ctx, (triton_event_func)__terminate, &ses->ses);
	}
K
Kozlov Dmitry 已提交
1548 1549
}

1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
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;
}

K
Kozlov Dmitry 已提交
1562 1563
static void add_interface(const char *ifname, int ifindex, const char *opt)
{
K
Kozlov Dmitry 已提交
1564
	char *str0 = NULL, *str, *ptr1, *ptr2;
K
Kozlov Dmitry 已提交
1565
	int end;
K
Kozlov Dmitry 已提交
1566
	struct ipoe_serv *serv;
K
Kozlov Dmitry 已提交
1567 1568 1569 1570
	int opt_shared = conf_shared;
	int opt_dhcpv4 = 0;
	int opt_up = 0;
	int opt_mode = conf_mode;
1571
	int opt_ifcfg = conf_ifcfg;
1572
	int opt_nat = conf_nat;
K
Kozlov Dmitry 已提交
1573 1574 1575 1576
	const char *opt_relay = conf_relay;
	const char *opt_giaddr = NULL;
	in_addr_t relay_addr = 0;
	in_addr_t giaddr = 0;
1577
	in_addr_t opt_src = conf_src;
1578 1579
	int opt_arp = conf_arp;
	struct ifreq ifr;
K
Kozlov Dmitry 已提交
1580 1581 1582 1583 1584 1585 1586 1587

	str0 = strchr(opt, ',');
	if (str0) {
		str0 = _strdup(str0 + 1);
		str = str0;
	
		while (1) {
			for (ptr1 = str + 1; *ptr1 && *ptr1 != '='; ptr1++);
K
Kozlov Dmitry 已提交
1588

K
Kozlov Dmitry 已提交
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
			if (!*ptr1)
				goto parse_err;
		
			*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;
1620 1621
			} else if (strcmp(str, "ifcfg") == 0) {
				opt_ifcfg = atoi(ptr1);
K
Kozlov Dmitry 已提交
1622 1623 1624 1625 1626 1627
			} else if (strcmp(str, "relay") == 0) {
				opt_relay = ptr1;
				relay_addr = inet_addr(ptr1);
			} else if (strcmp(str, "giaddr") == 0) {
				opt_giaddr = ptr1;
				giaddr = inet_addr(ptr1);
1628 1629
			} else if (strcmp(str, "nat") == 0) {
				opt_nat = atoi(ptr1);
1630 1631
			} else if (strcmp(str, "src") == 0) {
				opt_src = inet_addr(ptr1);
1632 1633
			} else if (strcmp(str, "proxy-arp") == 0) {
				opt_arp = atoi(ptr1);
1634
			}
K
Kozlov Dmitry 已提交
1635 1636 1637 1638 1639 1640

			if (end)
				break;

			str = ptr2 + 1;
		}
K
Kozlov Dmitry 已提交
1641
	}		
K
Kozlov Dmitry 已提交
1642 1643 1644 1645 1646 1647

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

1648 1649 1650
	if (opt_up)
		ipoe_nl_add_interface(ifindex);

K
Kozlov Dmitry 已提交
1651
	list_for_each_entry(serv, &serv_list, entry) {
1652
		if (strcmp(ifname, serv->ifname))
K
Kozlov Dmitry 已提交
1653 1654 1655 1656 1657 1658 1659 1660
			continue;

		serv->active = 1;
		serv->ifindex = ifindex;
		
		if ((opt_shared && !serv->opt_shared) || (!opt_shared && serv->opt_shared)) {
			ipoe_drop_sessions(serv, NULL);
			serv->opt_shared = opt_shared;
K
Kozlov Dmitry 已提交
1661
		}
K
Kozlov Dmitry 已提交
1662 1663

		if (opt_dhcpv4 && !serv->dhcpv4) {
1664
			serv->dhcpv4 = dhcpv4_create(&serv->ctx, serv->ifname, opt);
K
Kozlov Dmitry 已提交
1665 1666 1667 1668 1669 1670 1671
			if (serv->dhcpv4)
				serv->dhcpv4->recv = ipoe_recv_dhcpv4;
		} else if (!opt_dhcpv4 && serv->dhcpv4) {
			dhcpv4_free(serv->dhcpv4);
			serv->dhcpv4 = NULL;
		}

K
Kozlov Dmitry 已提交
1672 1673
		if (serv->dhcpv4_relay && 
				(serv->dhcpv4_relay->addr != relay_addr || serv->dhcpv4_relay->giaddr != giaddr)) {
1674 1675
			if (serv->opt_ifcfg)
				ipoe_serv_del_addr(serv, serv->dhcpv4_relay->giaddr);
K
Kozlov Dmitry 已提交
1676 1677 1678 1679
			dhcpv4_relay_free(serv->dhcpv4_relay, &serv->ctx);
			serv->dhcpv4_relay = NULL;
		}

1680
		if (serv->opt_dhcpv4 && opt_relay) {
1681
			if (opt_ifcfg)
K
Kozlov Dmitry 已提交
1682
				ipoe_serv_add_addr(serv, giaddr);
K
Kozlov Dmitry 已提交
1683
			serv->dhcpv4_relay = dhcpv4_relay_create(opt_relay, opt_giaddr, &serv->ctx, (triton_event_func)ipoe_recv_dhcpv4_relay);
K
Kozlov Dmitry 已提交
1684
		}
1685 1686 1687 1688 1689 1690

		if (serv->arp && !conf_arp) {
			arpd_stop(serv->arp);
			serv->arp = NULL;
		} else if (!serv->arp && conf_arp)
			serv->arp = arpd_start(serv);
1691 1692 1693 1694
		
		serv->opt_up = opt_up;
		serv->opt_mode = opt_mode;
		serv->opt_ifcfg = opt_ifcfg;
1695
		serv->opt_nat = opt_nat;
1696
		serv->opt_src = opt_src;
1697
		serv->opt_arp = opt_arp;
K
Kozlov Dmitry 已提交
1698 1699 1700 1701

		if (str0)
			_free(str0);

K
Kozlov Dmitry 已提交
1702
		return;
K
Kozlov Dmitry 已提交
1703 1704
	}

1705 1706 1707 1708 1709 1710 1711 1712
	memset(&ifr, 0, sizeof(ifr));
	strcpy(ifr.ifr_name, ifname);
	
	if (ioctl(sock_fd, SIOCGIFHWADDR, &ifr)) {
		log_error("ipoe: '%s': ioctl(SIOCGIFHWADDR): %s\n", ifname, strerror(errno));
		return;
	}

K
Kozlov Dmitry 已提交
1713 1714
	serv = _malloc(sizeof(*serv));
	memset(serv, 0, sizeof(*serv));
1715
	serv->ctx.close = ipoe_serv_close;
K
Kozlov Dmitry 已提交
1716 1717
	serv->ifname = _strdup(ifname);
	serv->ifindex = ifindex;
K
Kozlov Dmitry 已提交
1718 1719 1720 1721
	serv->opt_shared = opt_shared;
	serv->opt_dhcpv4 = opt_dhcpv4;
	serv->opt_up = opt_up;
	serv->opt_mode = opt_mode;
1722
	serv->opt_ifcfg = opt_ifcfg;
1723
	serv->opt_nat = opt_nat;
1724
	serv->opt_src = opt_src;
1725
	serv->opt_arp = opt_arp;
1726
	serv->active = 1;
K
Kozlov Dmitry 已提交
1727
	INIT_LIST_HEAD(&serv->sessions);
1728
	INIT_LIST_HEAD(&serv->addr_list);
1729
	memcpy(serv->hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
K
Kozlov Dmitry 已提交
1730 1731 1732 1733 1734
	pthread_mutex_init(&serv->lock, NULL);

	triton_context_register(&serv->ctx, NULL);

	if (serv->opt_dhcpv4) {
1735
		serv->dhcpv4 = dhcpv4_create(&serv->ctx, serv->ifname, opt);
K
Kozlov Dmitry 已提交
1736
		if (serv->dhcpv4)
1737
			serv->dhcpv4->recv = ipoe_recv_dhcpv4;
K
Kozlov Dmitry 已提交
1738
	
1739
		if (opt_relay) {
1740 1741
			if (opt_ifcfg)
				ipoe_serv_add_addr(serv, giaddr);
K
Kozlov Dmitry 已提交
1742 1743
			serv->dhcpv4_relay = dhcpv4_relay_create(opt_relay, opt_giaddr, &serv->ctx, (triton_event_func)ipoe_recv_dhcpv4_relay);
		}
K
Kozlov Dmitry 已提交
1744 1745
	}

1746 1747 1748
	if (serv->opt_arp)
		serv->arp = arpd_start(serv);

K
Kozlov Dmitry 已提交
1749 1750
	triton_context_wakeup(&serv->ctx);

1751 1752
	list_add_tail(&serv->entry, &serv_list);

K
Kozlov Dmitry 已提交
1753 1754 1755
	if (str0)
		_free(str0);

K
Kozlov Dmitry 已提交
1756 1757
	return;

K
Kozlov Dmitry 已提交
1758
parse_err:
K
Kozlov Dmitry 已提交
1759
	log_error("ipoe: failed to parse '%s'\n", opt);
K
Kozlov Dmitry 已提交
1760
	_free(str0);
K
Kozlov Dmitry 已提交
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
}

static void load_interface(const char *opt)
{
	const char *ptr;
	struct ifreq ifr;

	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;
	
	if (ioctl(sock_fd, SIOCGIFINDEX, &ifr)) {
		log_error("ipoe: '%s': ioctl(SIOCGIFINDEX): %s\n", ifr.ifr_name, strerror(errno));
		return;
	}
1780
	
K
Kozlov Dmitry 已提交
1781 1782 1783 1784 1785 1786 1787
	add_interface(ifr.ifr_name, ifr.ifr_ifindex, opt);
}

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

K
Kozlov Dmitry 已提交
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
	add_interface(name, index, arg->opt);

	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;

	for (ptr = opt; *ptr && *ptr != ','; ptr++);
	
	pattern = _malloc(ptr - (opt + 3) + 1);
	memcpy(pattern, opt + 3, ptr - (opt + 3));
	pattern[ptr - (opt + 3)] = 0;
	
	re = pcre_compile2(pattern, 0, NULL, &pcre_err, &pcre_offset, NULL);
		
	if (!re) {
		log_error("ipoe: %s at %i\r\n", pcre_err, pcre_offset);
		return;
	}

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

	iplink_list((iplink_list_func)__load_interface_re, &arg);

	pcre_free(re);
	_free(pattern);
}

static void load_interfaces(struct conf_sect_t *sect)
{
	struct ipoe_serv *serv;
	struct conf_option_t *opt;
	struct list_head *pos, *n;

1831 1832
	ipoe_nl_delete_interfaces();

K
Kozlov Dmitry 已提交
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
	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);
	}
	
	list_for_each_safe(pos, n, &serv_list) {
		serv = list_entry(pos, typeof(*serv), entry);
		if (!serv->active) {
1851
			ipoe_drop_sessions(serv, NULL);
K
Kozlov Dmitry 已提交
1852 1853 1854 1855 1856 1857
			list_del(&serv->entry);
			triton_context_call(&serv->ctx, (triton_event_func)ipoe_serv_close, &serv->ctx);
		}
	}
}

1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
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;
	}

1883 1884 1885
	if (mask == 32)
		mask = 0xffffffff;
	else
K
Kozlov Dmitry 已提交
1886 1887 1888 1889 1890 1891
		mask = (1 << (32-mask)) - 1;

	addr = ntohl(addr);
	mask = ~mask;

	//printf("%x/%x %x\n", htonl(addr), ~mask, htonl(addr)&(~mask));
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915

	ipoe_nl_add_net(addr & mask, mask);

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

1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
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;
		}
1952 1953

		a->mask1 = ((1 << a->mask) - 1) << (32 - a->mask);
1954 1955 1956 1957
		list_add_tail(&a->entry, &conf_gw_addr);
	}
}

1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
#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);

	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);
		}
	} else
		*val = -1;
}
K
Kozlov Dmitry 已提交
1978

1979 1980 1981 1982 1983 1984 1985 1986 1987
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);
	parse_conf_rad_attr("attr-l4-redirect", &conf_attr_l4_redirect);
}
#endif

K
Kozlov Dmitry 已提交
1988 1989 1990 1991
static void load_config(void)
{
	const char *opt;
	struct conf_sect_t *s = conf_get_section("ipoe");
K
Kozlov Dmitry 已提交
1992
	struct conf_option_t *opt1;
K
Kozlov Dmitry 已提交
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004

	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 已提交
2005
		}
K
Kozlov Dmitry 已提交
2006
#endif
K
Kozlov Dmitry 已提交
2007
		else
K
Kozlov Dmitry 已提交
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
			log_emerg("ipoe: unknown username value '%s'\n", opt);
	}

	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;
	
	opt = conf_get_opt("ipoe", "verbose");
	if (opt)
		conf_verbose = atoi(opt);
K
Kozlov Dmitry 已提交
2024 2025 2026 2027

	opt = conf_get_opt("ipoe", "lease-time");
	if (opt)
		conf_lease_time = atoi(opt);
K
Kozlov Dmitry 已提交
2028 2029
	else
		conf_lease_time = 600;
K
Kozlov Dmitry 已提交
2030
	
K
Kozlov Dmitry 已提交
2031
	opt = conf_get_opt("ipoe", "max-lease-time");
K
Kozlov Dmitry 已提交
2032 2033
	if (opt)
		conf_lease_timeout = atoi(opt);
K
Kozlov Dmitry 已提交
2034 2035
	else
		conf_lease_timeout = 660;
K
Kozlov Dmitry 已提交
2036
	
K
Kozlov Dmitry 已提交
2037 2038 2039 2040
	opt = conf_get_opt("ipoe", "unit-cache");
	if (opt)
		conf_unit_cache = atoi(opt);
	
2041 2042 2043 2044
	opt = conf_get_opt("ipoe", "l4-redirect-table");
	if (opt)
		conf_l4_redirect_table = atoi(opt);
	else
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
		conf_l4_redirect_table = 1;
	
	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;
		
	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);
	}
2058
	
K
Kozlov Dmitry 已提交
2059 2060 2061 2062 2063 2064
	opt = conf_get_opt("ipoe", "shared");
	if (opt)
		conf_shared = atoi(opt);
	else
		conf_shared = 1;
	
2065 2066 2067 2068 2069 2070
	opt = conf_get_opt("ipoe", "ifcfg");
	if (opt)
		conf_ifcfg = atoi(opt);
	else
		conf_ifcfg = 1;
	
2071 2072 2073 2074 2075
	opt = conf_get_opt("ipoe", "nat");
	if (opt)
		conf_nat = atoi(opt);
	else
		conf_nat = 0;
2076 2077 2078 2079 2080 2081

	opt = conf_get_opt("ipoe", "src");
	if (opt)
		conf_src = inet_addr(opt);
	else
		conf_src = 0;
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092

	opt = conf_get_opt("ipoe", "proxy-arp");
	if (opt)
		conf_arp = atoi(opt);
	else
		conf_arp = 0;
	
	if (conf_arp < 0 || conf_arp > 2) {
		log_error("ipoe: arp=%s: invalid value\n", opt);
		conf_arp = 0;
	}
2093
	
K
Kozlov Dmitry 已提交
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
	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;
K
Kozlov Dmitry 已提交
2104 2105
	
	conf_relay = conf_get_opt("ipoe", "relay");
2106 2107 2108 2109 2110 2111
	
	opt = conf_get_opt("ipoe", "agent-remote-id");
	if (opt)
		conf_agent_remote_id = opt;
	else
		conf_agent_remote_id = "accel-pppd";
K
Kozlov Dmitry 已提交
2112 2113 2114 2115 2116 2117
	
	opt = conf_get_opt("ipoe", "noauth");
	if (opt)
		conf_noauth = atoi(opt);
	else
		conf_noauth = 0;
K
Kozlov Dmitry 已提交
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133

	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;
	
2134 2135 2136 2137 2138 2139
	opt = conf_get_opt("ipoe", "proto");
	if (opt && atoi(opt) > 0)
		conf_proto = atoi(opt);
	else
		conf_proto = 0;
	
2140 2141 2142 2143 2144
#ifdef RADIUS
	if (triton_module_loaded("radius"))
		load_radius_attrs();
#endif
	
K
Kozlov Dmitry 已提交
2145 2146
	load_interfaces(s);
	load_local_nets(s);
2147
	load_gw_addr(s);
K
Kozlov Dmitry 已提交
2148 2149
}

2150 2151 2152 2153 2154 2155 2156 2157
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 已提交
2158 2159 2160
static void ipoe_init(void)
{
	ses_pool = mempool_create(sizeof(struct ipoe_session));
K
Kozlov Dmitry 已提交
2161
	uc_pool = mempool_create(sizeof(struct unit_cache));
K
Kozlov Dmitry 已提交
2162

2163 2164 2165
	triton_context_register(&l4_redirect_ctx, NULL);
	triton_context_wakeup(&l4_redirect_ctx);

K
Kozlov Dmitry 已提交
2166 2167 2168 2169 2170
	load_config();

	cli_register_simple_cmd2(show_stat_exec, NULL, 2, "show", "stat");
	
	triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);
2171 2172

#ifdef RADIUS
2173
	if (triton_module_loaded("radius")) {
2174 2175
		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);
2176
	}
2177
#endif
K
Kozlov Dmitry 已提交
2178 2179
}

2180
DEFINE_INIT(52, ipoe_init);