ipoe.c 71.5 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"
32 33
#include "ipset.h"

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

#include "ipoe.h"

#include "memdebug.h"

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

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

D
Dmitry Kozlov 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
struct ifaddr {
	struct list_head entry;
	in_addr_t addr;
	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;
	int ifindex;
	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;
};

K
Kozlov Dmitry 已提交
93
static int conf_dhcpv4 = 1;
K
Kozlov Dmitry 已提交
94 95 96
static int conf_up = 0;
static int conf_mode = 0;
static int conf_shared = 1;
97
static int conf_ifcfg = 1;
98
static int conf_nat = 0;
99
static int conf_arp = 0;
100
static uint32_t conf_src;
101
static const char *conf_ip_pool;
K
Kozlov Dmitry 已提交
102 103
//static int conf_dhcpv6;
static int conf_username;
104
static const char *conf_password;
K
Kozlov Dmitry 已提交
105
static int conf_unit_cache;
K
Kozlov Dmitry 已提交
106
static int conf_noauth;
107 108 109 110 111 112 113
#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;
114
static int conf_l4_redirect_on_reject;
115
static const char *conf_l4_redirect_ipset;
D
Dmitry Kozlov 已提交
116
static int conf_vlan_timeout = 30;
117

K
Kozlov Dmitry 已提交
118
static const char *conf_relay;
K
Kozlov Dmitry 已提交
119 120 121 122 123

#ifdef USE_LUA
static const char *conf_lua_username_func;
#endif

124 125 126
static int conf_offer_timeout = 10;
static int conf_relay_timeout = 3;
static int conf_relay_retransmit = 3;
127
static LIST_HEAD(conf_gw_addr);
K
Kozlov Dmitry 已提交
128 129 130 131
static int conf_netmask = 24;
static int conf_lease_time = 600;
static int conf_lease_timeout = 660;
static int conf_verbose;
132
static const char *conf_agent_remote_id;
133
static int conf_proto;
D
Dmitry Kozlov 已提交
134
static LIST_HEAD(conf_offer_delay);
135
static const char *conf_vlan_name;
K
Kozlov Dmitry 已提交
136 137 138

static unsigned int stat_starting;
static unsigned int stat_active;
D
Dmitry Kozlov 已提交
139
static unsigned int stat_delayed_offer;
K
Kozlov Dmitry 已提交
140 141

static mempool_t ses_pool;
D
Dmitry Kozlov 已提交
142
static mempool_t disc_item_pool;
K
Kozlov Dmitry 已提交
143

144 145
static int connlimit_loaded;

K
Kozlov Dmitry 已提交
146
static LIST_HEAD(serv_list);
D
Dmitry Kozlov 已提交
147
static pthread_mutex_t serv_lock = PTHREAD_MUTEX_INITIALIZER;
D
Dmitry Kozlov 已提交
148

K
Kozlov Dmitry 已提交
149 150 151 152 153
static pthread_mutex_t uc_lock = PTHREAD_MUTEX_INITIALIZER;
static LIST_HEAD(uc_list);
static int uc_size;
static mempool_t uc_pool;

154 155 156 157 158
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 已提交
159
static void ipoe_session_finished(struct ap_session *s);
K
Kozlov Dmitry 已提交
160
static void ipoe_drop_sessions(struct ipoe_serv *serv, struct ipoe_session *skip);
D
Dmitry Kozlov 已提交
161
static void ipoe_serv_release(struct ipoe_serv *serv);
K
Kozlov Dmitry 已提交
162
static void __ipoe_session_activate(struct ipoe_session *ses);
163
static void ipoe_ses_recv_dhcpv4(struct dhcpv4_serv *dhcpv4, struct dhcpv4_packet *pack);
D
Dmitry Kozlov 已提交
164
static void __ipoe_recv_dhcpv4(struct dhcpv4_serv *dhcpv4, struct dhcpv4_packet *pack, int force);
165
static void ipoe_session_keepalive(struct dhcpv4_packet *pack);
D
Dmitry Kozlov 已提交
166
static void add_interface(const char *ifname, int ifindex, const char *opt, int parent_ifindex, int vid);
D
Dmitry Kozlov 已提交
167
static int get_offer_delay();
168
static void __ipoe_session_start(struct ipoe_session *ses);
K
Kozlov Dmitry 已提交
169

K
Kozlov Dmitry 已提交
170
static struct ipoe_session *ipoe_session_lookup(struct ipoe_serv *serv, struct dhcpv4_packet *pack, struct ipoe_session **opt82_ses)
K
Kozlov Dmitry 已提交
171
{
K
Kozlov Dmitry 已提交
172
	struct ipoe_session *ses, *res = NULL;
K
Kozlov Dmitry 已提交
173 174 175
	
	uint8_t *agent_circuit_id = NULL;
	uint8_t *agent_remote_id = NULL;
K
Kozlov Dmitry 已提交
176 177 178 179
	int opt82_match;

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

K
Kozlov Dmitry 已提交
181 182 183 184
	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 已提交
185

K
Kozlov Dmitry 已提交
186
	list_for_each_entry(ses, &serv->sessions, entry) {
187
		opt82_match = pack->relay_agent != NULL;
K
Kozlov Dmitry 已提交
188
		
189
		if (agent_circuit_id && !ses->agent_circuit_id)
K
Kozlov Dmitry 已提交
190
			opt82_match = 0;
K
Kozlov Dmitry 已提交
191
		
K
Kozlov Dmitry 已提交
192 193
		if (opt82_match && agent_remote_id && !ses->agent_remote_id)
			opt82_match = 0;
K
Kozlov Dmitry 已提交
194
		
K
Kozlov Dmitry 已提交
195 196 197 198 199
		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 已提交
200
		
K
Kozlov Dmitry 已提交
201
		if (opt82_match && agent_circuit_id) {
K
Kozlov Dmitry 已提交
202
			if (*agent_circuit_id != *ses->agent_circuit_id)
K
Kozlov Dmitry 已提交
203 204
				opt82_match = 0;
		
K
Kozlov Dmitry 已提交
205
			if (memcmp(agent_circuit_id + 1, ses->agent_circuit_id + 1, *agent_circuit_id))
K
Kozlov Dmitry 已提交
206
				opt82_match = 0;
K
Kozlov Dmitry 已提交
207 208
		}
		
K
Kozlov Dmitry 已提交
209
		if (opt82_match && agent_remote_id) {
K
Kozlov Dmitry 已提交
210
			if (*agent_remote_id != *ses->agent_remote_id)
K
Kozlov Dmitry 已提交
211 212
				opt82_match = 0;

K
Kozlov Dmitry 已提交
213
			if (memcmp(agent_remote_id + 1, ses->agent_remote_id + 1, *agent_remote_id))
K
Kozlov Dmitry 已提交
214
				opt82_match = 0;
K
Kozlov Dmitry 已提交
215
		}
K
Kozlov Dmitry 已提交
216 217 218

		if (opt82_match && opt82_ses)
			*opt82_ses = ses;
K
Kozlov Dmitry 已提交
219
			
D
Dmitry Kozlov 已提交
220
		if (memcmp(pack->hdr->chaddr, ses->hwaddr, ETH_ALEN))
K
Kozlov Dmitry 已提交
221 222
			continue;
	
K
Kozlov Dmitry 已提交
223 224
		res = ses;
		break;
K
Kozlov Dmitry 已提交
225
		
K
Kozlov Dmitry 已提交
226
		/*if (pack->client_id && !ses->client_id)
227 228 229 230 231
			continue;
		
		if (!pack->client_id && ses->client_id)
			continue;
		
K
Kozlov Dmitry 已提交
232 233 234 235 236 237 238
		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 已提交
239 240 241 242 243
		ses1 = ses;

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

K
Kozlov Dmitry 已提交
244
		return ses;*/
K
Kozlov Dmitry 已提交
245 246
	}

K
Kozlov Dmitry 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	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 已提交
284 285 286 287 288 289 290 291
}

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 已提交
292
	log_ppp_info2("ipoe: session timed out\n");
K
Kozlov Dmitry 已提交
293 294 295 296

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

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
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");

		ap_session_terminate(&ses->ses, TERM_LOST_CARRIER, 0);
	} else
		dhcpv4_relay_send(ses->serv->dhcpv4_relay, ses->dhcpv4_request, ses->relay_server_id, ses->serv->ifname, conf_agent_remote_id);
}


K
Kozlov Dmitry 已提交
317 318 319
static void ipoe_session_set_username(struct ipoe_session *ses)
{
#ifdef USE_LUA
320 321
	if (ses->serv->opt_username == USERNAME_LUA) {
		ipoe_lua_set_username(ses, ses->serv->opt_lua_username_func ? : conf_lua_username_func);
K
Kozlov Dmitry 已提交
322 323
	} else
#endif
324 325

	ap_session_set_username(&ses->ses, _strdup(ses->ses.ifname));
K
Kozlov Dmitry 已提交
326 327
}

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
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);
344 345 346 347 348 349

	if (conf_l4_redirect_table)
		iprule_add(addr, conf_l4_redirect_table);

	if (conf_l4_redirect_ipset)
		ipset_add(conf_l4_redirect_ipset, addr);
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

	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);
390 391 392 393 394 395

			if (conf_l4_redirect_table)
				iprule_del(n->addr, conf_l4_redirect_table);
			
			if (conf_l4_redirect_ipset)
				ipset_del(conf_l4_redirect_ipset, n->addr);
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

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

419 420 421
static void ipoe_change_l4_redirect(struct ipoe_session *ses, int del)
{
	in_addr_t addr;
422
		
423
	if (ses->ses.ipv4)
424
		addr = ses->ses.ipv4->peer_addr;
425 426
	else
		addr = ses->yiaddr;
427 428 429 430 431 432 433 434 435 436
	
	if (conf_l4_redirect_table) {
		if (del) {
			iprule_del(addr, conf_l4_redirect_table);
			ses->l4_redirect_set = 0;
		} else {
			iprule_add(addr, conf_l4_redirect_table);
			ses->l4_redirect_set = 1;
		}
	}
437

438 439 440 441 442 443 444 445
	if (conf_l4_redirect_ipset) {
		if (del) {
			ipset_del(conf_l4_redirect_ipset, addr);
			ses->l4_redirect_set = 0;
		} else {
			ipset_add(conf_l4_redirect_ipset, addr);
			ses->l4_redirect_set = 1;
		}
K
Kozlov Dmitry 已提交
446
	}
447 448 449 450 451 452 453
}

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

}

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
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;
	}

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

488 489 490 491 492 493 494 495
	strncpy(ses->ses.ifname, ifr.ifr_name, AP_IFNAME_LEN);
	ses->ses.ifindex = ses->ifindex;
	ses->ses.unit_idx = ses->ifindex;
	ses->ctrl.dont_ifcfg = 0;

	return 0;
}

K
Kozlov Dmitry 已提交
496 497 498 499 500
static void ipoe_session_start(struct ipoe_session *ses)
{
	int r;
	char *passwd;

D
Dmitry Kozlov 已提交
501 502
	__sync_add_and_fetch(&stat_starting, 1);
	
K
Kozlov Dmitry 已提交
503
	if (!ses->ses.username) {
504
		strncpy(ses->ses.ifname, ses->serv->ifname, AP_IFNAME_LEN);
505
		
506 507 508 509 510 511
		ipoe_session_set_username(ses);

		if (!ses->ses.username) {
			ipoe_session_finished(&ses->ses);
			return;
		}
K
Kozlov Dmitry 已提交
512
	}
513 514

	ses->ses.unit_idx = ses->serv->ifindex;
K
Kozlov Dmitry 已提交
515 516 517 518 519 520
	
	triton_event_fire(EV_CTRL_STARTING, &ses->ses);
	triton_event_fire(EV_CTRL_STARTED, &ses->ses);

	ap_session_starting(&ses->ses);
	
K
Kozlov Dmitry 已提交
521
	if (!conf_noauth) {
522 523 524
		if (ses->serv->opt_shared && ipoe_create_interface(ses))
			return;

525
		r = pwdb_check(&ses->ses, ses->ses.username, PPP_PAP, conf_password ? conf_password : ses->ses.username);
K
Kozlov Dmitry 已提交
526 527 528 529 530 531 532 533
		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 已提交
534 535
		}

K
Kozlov Dmitry 已提交
536 537 538 539 540 541 542 543 544
		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;
545
		}
K
Kozlov Dmitry 已提交
546
	}
547

548 549 550
	log_ppp_info1("%s: authentication succeeded\n", ses->ses.username);
	triton_event_fire(EV_SES_AUTHORIZED, &ses->ses);

551 552
	if (ses->serv->opt_nat)
		ses->ses.ipv4 = ipdb_get_ipv4(&ses->ses);
K
Kozlov Dmitry 已提交
553 554 555 556 557
	
	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) {
558
		if (ipoe_create_interface(ses))
K
Kozlov Dmitry 已提交
559 560 561 562
			return;
	}

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

K
Kozlov Dmitry 已提交
564
	if (ses->dhcpv4_request && ses->serv->dhcpv4_relay) {
565
		dhcpv4_relay_send(ses->serv->dhcpv4_relay, ses->dhcpv4_request, ses->relay_server_id, ses->serv->ifname, conf_agent_remote_id);
K
Kozlov Dmitry 已提交
566

567 568
		ses->timer.expire = ipoe_relay_timeout;
		ses->timer.period = conf_relay_timeout * 1000;
K
Kozlov Dmitry 已提交
569 570 571 572 573
		triton_timer_add(&ses->ctx, &ses->timer, 0);
	} else
		__ipoe_session_start(ses);
}

574 575 576 577 578
static void find_gw_addr(struct ipoe_session *ses)
{
	struct gw_addr *a;

	list_for_each_entry(a, &conf_gw_addr, entry) {
579
		if ((ntohl(ses->yiaddr) & (a->mask1)) == (ntohl(a->addr) & (a->mask1))) {
580 581 582 583 584 585 586
			ses->siaddr = a->addr;
			ses->mask = a->mask;
			return;
		}
	}
}

K
Kozlov Dmitry 已提交
587 588
static void __ipoe_session_start(struct ipoe_session *ses) 
{
589
	if (!ses->yiaddr) {
590
		dhcpv4_get_ip(ses->serv->dhcpv4, &ses->yiaddr, &ses->router, &ses->mask);
591 592 593
		if (ses->yiaddr)
			ses->dhcp_addr = 1;
	}
594

595 596 597 598 599 600
	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 已提交
601
	if (ses->ses.ipv4) {
602 603
		if (!ses->mask)
			ses->mask = ses->ses.ipv4->mask;
K
Kozlov Dmitry 已提交
604

K
Kozlov Dmitry 已提交
605 606
		if (!ses->yiaddr)
			ses->yiaddr = ses->ses.ipv4->peer_addr;
607 608 609
	
		if (!ses->router)
			ses->router = ses->ses.ipv4->addr;
K
Kozlov Dmitry 已提交
610
	} /*else if (ses->yiaddr) {
K
Kozlov Dmitry 已提交
611 612 613 614 615
		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 已提交
616
	}*/
617

K
Kozlov Dmitry 已提交
618 619 620 621 622 623 624 625 626
	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;
627
		
628 629 630
		if (!ses->siaddr)
			find_gw_addr(ses);
		
631 632 633
		if (!ses->siaddr)
			ses->siaddr = ses->serv->opt_src;		

K
Kozlov Dmitry 已提交
634 635
		if (!ses->siaddr && ses->serv->dhcpv4_relay)
			ses->siaddr = ses->serv->dhcpv4_relay->giaddr;
636

K
Kozlov Dmitry 已提交
637 638 639 640 641
		if (!ses->siaddr) {
			log_ppp_error("can't determine Server-ID\n");
			ap_session_terminate(&ses->ses, TERM_NAS_ERROR, 0);
			return;
		}
642

643 644 645
		if (ses->ses.ipv4 && !ses->ses.ipv4->addr)
			ses->ses.ipv4->addr = ses->siaddr;

646 647
		if (!ses->router)
			ses->router = ses->siaddr;
K
Kozlov Dmitry 已提交
648 649 650
				
		if (!ses->mask)
			ses->mask = 32;
651

652
		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 已提交
653 654 655

		dhcpv4_packet_free(ses->dhcpv4_request);
		ses->dhcpv4_request = NULL;
656 657
	
		ses->timer.expire = ipoe_session_timeout;
658
		ses->timer.period = 0;
659 660
		ses->timer.expire_tv.tv_sec = conf_offer_timeout;
		triton_timer_add(&ses->ctx, &ses->timer, 0);
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	} else {
		if (!ses->siaddr)
			find_gw_addr(ses);
		
		if (!ses->siaddr)
			ses->siaddr = ses->serv->opt_src;

		if (!ses->siaddr)
			ses->siaddr = iproute_get(ses->yiaddr);

		if (!ses->siaddr) {
			log_ppp_error("can't determine local address\n");
			ap_session_terminate(&ses->ses, TERM_NAS_ERROR, 0);
			return;
		}
		
		if (ses->ses.ipv4 && !ses->ses.ipv4->addr)
			ses->ses.ipv4->addr = ses->siaddr;

K
Kozlov Dmitry 已提交
680
		__ipoe_session_activate(ses);
681
	}
K
Kozlov Dmitry 已提交
682 683
}

K
Kozlov Dmitry 已提交
684
static void ipoe_serv_add_addr(struct ipoe_serv *serv, in_addr_t addr)
685 686 687 688 689
{
	struct ifaddr *a;

	pthread_mutex_lock(&serv->lock);
	
K
Kozlov Dmitry 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	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);
723
			}
K
Kozlov Dmitry 已提交
724
			break;
725
		}
K
Kozlov Dmitry 已提交
726 727 728 729 730 731 732 733 734
	}
	
	pthread_mutex_unlock(&serv->lock);
}

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

K
Kozlov Dmitry 已提交
735
	if (ses->serv->opt_ifcfg) {
K
Kozlov Dmitry 已提交
736 737 738 739 740 741 742 743
		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);
		}
744
		if (iproute_add(serv->ifindex, ses->serv->opt_src ? ses->serv->opt_src : ses->router, ses->yiaddr, conf_proto))
K
Kozlov Dmitry 已提交
745
			log_ppp_warn("ipoe: failed to add route to interface '%s'\n", serv->ifname);
746
	} else if (iproute_add(serv->ifindex, ses->serv->opt_src ? ses->serv->opt_src : ses->router, ses->yiaddr, conf_proto))
747 748 749 750 751
		log_ppp_warn("ipoe: failed to add route to interface '%s'\n", serv->ifname);

	ses->ifcfg = 1;
}

D
Dmitry Kozlov 已提交
752
static void ipoe_ifcfg_del(struct ipoe_session *ses, int lock)
753 754
{
	struct ipoe_serv *serv = ses->serv;
K
Kozlov Dmitry 已提交
755
	
756
	if (iproute_del(serv->ifindex, ses->yiaddr, conf_proto))
K
Kozlov Dmitry 已提交
757
		log_ppp_warn("ipoe: failed to delete route from interface '%s'\n", serv->ifname);
758

K
Kozlov Dmitry 已提交
759
	if (ses->serv->opt_ifcfg) {
K
Kozlov Dmitry 已提交
760 761 762
		if (ses->serv->opt_shared) {
			ipoe_serv_del_addr(ses->serv, ses->siaddr);
		} else {
D
Dmitry Kozlov 已提交
763 764 765 766 767 768 769 770
			if (lock)
				pthread_mutex_lock(&serv->lock);
			if (ipaddr_del(serv->ifindex, ses->siaddr)) {
				if (lock)
					log_ppp_warn("ipoe: failed to remove addess from interface '%s'\n", serv->ifname);
			}
			if (lock)
				pthread_mutex_unlock(&serv->lock);
K
Kozlov Dmitry 已提交
771
		}
K
Kozlov Dmitry 已提交
772
	}
773 774
}

K
Kozlov Dmitry 已提交
775
static void __ipoe_session_activate(struct ipoe_session *ses)
K
Kozlov Dmitry 已提交
776
{
777 778
	uint32_t addr;

779 780
	if (ses->terminating)
		return;
781
	
782
	if (ses->ifindex != -1) {
K
Kozlov Dmitry 已提交
783
		addr = 0;
784 785 786 787 788 789
		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)
790
			addr = ses->ses.ipv4->peer_addr;
K
Kozlov Dmitry 已提交
791
		
792
		if (ipoe_nl_modify(ses->ifindex, ses->yiaddr, addr, NULL, NULL)) {
793 794 795
			ap_session_terminate(&ses->ses, TERM_NAS_ERROR, 0);
			return;
		}
K
Kozlov Dmitry 已提交
796
	}
797
	
798 799 800 801 802 803 804 805
	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)))
806
		ipoe_ifcfg_add(ses);
807 808 809
	
	if (ses->l4_redirect)
		ipoe_change_l4_redirect(ses, 0);
810

D
Dmitry Kozlov 已提交
811 812 813 814
	__sync_sub_and_fetch(&stat_starting, 1);
	__sync_add_and_fetch(&stat_active, 1);
	ses->started = 1;

K
Kozlov Dmitry 已提交
815 816
	ap_session_activate(&ses->ses);

817 818
	if (ses->dhcpv4_request) {
		if (ses->ses.state == AP_STATE_ACTIVE)
819
			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);
820 821
		else
			dhcpv4_send_nak(ses->serv->dhcpv4, ses->dhcpv4_request);
K
Kozlov Dmitry 已提交
822

823 824 825
		dhcpv4_packet_free(ses->dhcpv4_request);
		ses->dhcpv4_request = NULL;
	}
K
Kozlov Dmitry 已提交
826 827
	
	ses->timer.expire = ipoe_session_timeout;
828
	ses->timer.period = 0;
K
Kozlov Dmitry 已提交
829 830 831
	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 已提交
832 833
}

834
static void ipoe_session_activate(struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
835
{
836 837 838 839 840 841 842 843 844 845 846 847
	struct ipoe_session *ses = container_of(triton_context_self(), typeof(*ses), ctx);
	
	if (ses->ses.state == AP_STATE_ACTIVE) {
		ipoe_session_keepalive(pack);
		return;
	}

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

K
Kozlov Dmitry 已提交
848
	if (ses->serv->dhcpv4_relay)
849
		dhcpv4_relay_send(ses->serv->dhcpv4_relay, ses->dhcpv4_request, ses->relay_server_id, ses->serv->ifname, conf_agent_remote_id);
K
Kozlov Dmitry 已提交
850 851 852 853 854
	else
		__ipoe_session_activate(ses);
}

static void ipoe_session_keepalive(struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
855
{
K
Kozlov Dmitry 已提交
856 857 858 859 860 861 862
	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 已提交
863 864 865 866
	if (ses->timer.tpd)
		triton_timer_mod(&ses->timer, 0);

	ses->xid = ses->dhcpv4_request->hdr->xid;
K
Kozlov Dmitry 已提交
867
	
868
	if (/*ses->ses.state == AP_STATE_ACTIVE &&*/ ses->serv->dhcpv4_relay) {
869
		dhcpv4_relay_send(ses->serv->dhcpv4_relay, ses->dhcpv4_request, ses->relay_server_id, ses->serv->ifname, conf_agent_remote_id);
K
Kozlov Dmitry 已提交
870 871
		return;
	}
K
Kozlov Dmitry 已提交
872

K
Kozlov Dmitry 已提交
873
	if (ses->ses.state == AP_STATE_ACTIVE) {
874
		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 已提交
875
	} else
K
Kozlov Dmitry 已提交
876 877 878 879 880
		dhcpv4_send_nak(ses->serv->dhcpv4, ses->dhcpv4_request);

	dhcpv4_packet_free(ses->dhcpv4_request);
	ses->dhcpv4_request = NULL;
}
K
Kozlov Dmitry 已提交
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
			
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 已提交
898

K
Kozlov Dmitry 已提交
899 900 901 902
static void ipoe_session_started(struct ap_session *s)
{
	struct ipoe_session *ses = container_of(s, typeof(*ses), ses);
	
903
	log_ppp_info1("ipoe: session started\n");
K
Kozlov Dmitry 已提交
904

K
Kozlov Dmitry 已提交
905 906
	if (ses->timer.tpd)
		triton_timer_mod(&ses->timer, 0);
907 908 909 910 911 912 913 914 915
	
	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 已提交
916 917 918 919
}

static void ipoe_session_free(struct ipoe_session *ses)
{
D
Dmitry Kozlov 已提交
920 921 922 923 924
	if (ses->started)
		__sync_sub_and_fetch(&stat_active, 1);
	else
		__sync_sub_and_fetch(&stat_starting, 1);
	
K
Kozlov Dmitry 已提交
925 926 927
	if (ses->timer.tpd)
		triton_timer_del(&ses->timer);

K
Kozlov Dmitry 已提交
928 929 930
	if (ses->dhcpv4_request)
		dhcpv4_packet_free(ses->dhcpv4_request);
	
931 932 933
	if (ses->dhcpv4_relay_reply)
		dhcpv4_packet_free(ses->dhcpv4_relay_reply);
	
934 935 936 937 938 939
	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 已提交
940 941 942 943
	triton_context_unregister(&ses->ctx);
	
	if (ses->data)
		_free(ses->data);
944
	
K
Kozlov Dmitry 已提交
945 946 947 948 949 950
	mempool_free(ses);
}

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

953
	log_ppp_info1("ipoe: session finished\n");
K
Kozlov Dmitry 已提交
954 955 956

	pthread_mutex_lock(&ses->serv->lock);
	list_del(&ses->entry);
D
Dmitry Kozlov 已提交
957 958
	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);
K
Kozlov Dmitry 已提交
959 960
	pthread_mutex_unlock(&ses->serv->lock);

961 962 963 964 965 966 967 968 969 970 971 972
	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);
	}

973
	if (ses->dhcp_addr)
974
		dhcpv4_put_ip(ses->serv->dhcpv4, ses->yiaddr);
975

K
Kozlov Dmitry 已提交
976
	if (ses->relay_addr && ses->serv->dhcpv4_relay)
977
		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);
978

979
	if (s->ipv4 && s->ipv4->owner) {
980 981 982 983
		ipdb_put_ipv4(s, s->ipv4);
		s->ipv4 = NULL;
	}

984
	if (ses->ifcfg)
D
Dmitry Kozlov 已提交
985
		ipoe_ifcfg_del(ses, 1);
986 987 988
	
	if (ses->dhcpv4)
		dhcpv4_free(ses->dhcpv4);
989 990

	triton_event_fire(EV_CTRL_FINISHED, s);
K
Kozlov Dmitry 已提交
991
	
K
Kozlov Dmitry 已提交
992 993 994 995 996
	triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_free, ses);
}

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

K
Kozlov Dmitry 已提交
999
	if (ses->l4_redirect_set)
1000 1001
		ipoe_change_l4_redirect(ses, 1);

K
Kozlov Dmitry 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	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);
}

1016
static struct ipoe_session *ipoe_session_create_dhcpv4(struct ipoe_serv *serv, struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
1017 1018 1019
{
	struct ipoe_session *ses;
	int dlen = 0;
1020
	uint8_t *ptr = NULL;
K
Kozlov Dmitry 已提交
1021
	
1022 1023
	ses = ipoe_session_alloc();
	if (!ses)
K
Kozlov Dmitry 已提交
1024 1025 1026 1027 1028 1029 1030 1031
		return NULL;

	ses->serv = serv;
	ses->dhcpv4_request = pack;
	
	ses->xid = pack->hdr->xid;
	memcpy(ses->hwaddr, pack->hdr->chaddr, 6);
	ses->giaddr = pack->hdr->giaddr;
K
Kozlov Dmitry 已提交
1032
	ses->lease_time = conf_lease_time;
K
Kozlov Dmitry 已提交
1033 1034

	if (pack->client_id)
K
Kozlov Dmitry 已提交
1035 1036 1037 1038
		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 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	
	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 已提交
1051
		ses->client_id = (struct dhcpv4_option *)ptr;
K
Kozlov Dmitry 已提交
1052
		ses->client_id->len = pack->client_id->len;
1053
		ses->client_id->data = (uint8_t *)(ses->client_id + 1);
K
Kozlov Dmitry 已提交
1054
		memcpy(ses->client_id->data, pack->client_id->data, pack->client_id->len);
K
Kozlov Dmitry 已提交
1055 1056 1057 1058 1059 1060
		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;
1061
		ses->relay_agent->data = (uint8_t *)(ses->relay_agent + 1);
K
Kozlov Dmitry 已提交
1062 1063 1064 1065
		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 已提交
1066 1067
	}

1068
	ses->ctrl.dont_ifcfg = 1;
K
Kozlov Dmitry 已提交
1069 1070
	
	ses->ctrl.calling_station_id = _malloc(19);
1071
	ses->ctrl.called_station_id = _strdup(serv->ifname);
K
Kozlov Dmitry 已提交
1072 1073 1074 1075 1076 1077 1078 1079
	
	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;

1080 1081 1082
	if (conf_ip_pool)
		ses->ses.ipv4_pool_name = _strdup(conf_ip_pool);

K
Kozlov Dmitry 已提交
1083 1084 1085 1086 1087 1088 1089
	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 已提交
1090 1091 1092
	
	if (serv->timer.tpd)
		triton_timer_del(&serv->timer);
K
Kozlov Dmitry 已提交
1093 1094 1095 1096 1097 1098

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

	return ses;
}

K
Kozlov Dmitry 已提交
1099 1100 1101 1102 1103
static void __ipoe_session_terminate(struct ap_session *ses)
{
	ap_session_terminate(ses, TERM_USER_REQUEST, 0);
}

1104 1105 1106
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 已提交
1107 1108 1109
	int opt82_match;
	uint8_t *agent_circuit_id = NULL;
	uint8_t *agent_remote_id = NULL;
1110 1111 1112

	if (ap_shutdown)
		return;
1113
	
1114 1115 1116 1117
	if (conf_verbose) {
		log_ppp_info2("recv ");
		dhcpv4_print_packet(pack, 0, log_info2);
	}
K
Kozlov Dmitry 已提交
1118 1119 1120 1121 1122 1123

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

1124
	opt82_match = pack->relay_agent != NULL;
K
Kozlov Dmitry 已提交
1125
	
1126
	if (agent_circuit_id && !ses->agent_circuit_id)
K
Kozlov Dmitry 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
		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;
	}

1154
	if (pack->relay_agent && !opt82_match) {
K
Kozlov Dmitry 已提交
1155 1156 1157 1158 1159 1160
		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 已提交
1161

1162 1163 1164 1165
	if (pack->msg_type == DHCPDISCOVER) {
		if (ses->yiaddr) {
			if (ses->serv->dhcpv4_relay) {
				dhcpv4_packet_ref(pack);
K
Kozlov Dmitry 已提交
1166
				ipoe_session_keepalive(pack);
1167 1168 1169 1170
			} 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) {
D
Dmitry Kozlov 已提交
1171
		ses->xid = pack->hdr->xid;
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
		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 已提交
1182
			triton_context_call(ses->ctrl.ctx, (triton_event_func)__ipoe_session_terminate, &ses->ses);
1183 1184 1185 1186 1187 1188
		} 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 已提交
1189
		triton_context_call(ses->ctrl.ctx, (triton_event_func)ipoe_session_decline, pack);
1190 1191 1192
	}
}

D
Dmitry Kozlov 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
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);
	
	while (!list_empty(&serv->disc_list)) {	
	  d = list_entry(serv->disc_list.next, typeof(*d), entry);
			
		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;
		}
	
		__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;
		
	__sync_add_and_fetch(&stat_delayed_offer, 1);
	
	dhcpv4_packet_ref(pack);
	d->pack = pack;
	clock_gettime(CLOCK_MONOTONIC, &d->ts);
	list_add_tail(&d->entry, &serv->disc_list);
		
	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;
		
		list_del(&d->entry);
		dhcpv4_packet_free(d->pack);
		mempool_free(d);
		
		__sync_sub_and_fetch(&stat_delayed_offer, 1);
		
		break;
	}
}

static void __ipoe_recv_dhcpv4(struct dhcpv4_serv *dhcpv4, struct dhcpv4_packet *pack, int force)
K
Kozlov Dmitry 已提交
1271 1272
{
	struct ipoe_serv *serv = container_of(dhcpv4->ctx, typeof(*serv), ctx);
K
Kozlov Dmitry 已提交
1273
	struct ipoe_session *ses, *opt82_ses;
D
Dmitry Kozlov 已提交
1274
	int offer_delay;
K
Kozlov Dmitry 已提交
1275 1276
	//struct dhcpv4_packet *reply;

D
Dmitry Kozlov 已提交
1277 1278 1279
	if (serv->timer.tpd)
		triton_timer_mod(&serv->timer, 0);

1280 1281
	if (ap_shutdown)
		return;
1282
	
1283
	if (connlimit_loaded && pack->msg_type == DHCPDISCOVER && connlimit_check(serv->opt_shared ? cl_key_from_mac(pack->hdr->chaddr) : serv->ifindex))
1284
		return;
1285

K
Kozlov Dmitry 已提交
1286 1287
	pthread_mutex_lock(&serv->lock);
	if (pack->msg_type == DHCPDISCOVER) {
K
Kozlov Dmitry 已提交
1288
		ses = ipoe_session_lookup(serv, pack, &opt82_ses);
K
Kozlov Dmitry 已提交
1289
		if (!ses) {
1290 1291
			if (serv->opt_shared == 0)
				ipoe_drop_sessions(serv, NULL);
K
Kozlov Dmitry 已提交
1292 1293 1294 1295 1296 1297
			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);
			}
1298

D
Dmitry Kozlov 已提交
1299 1300 1301 1302 1303 1304 1305 1306 1307
			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;
			}

1308
			ses = ipoe_session_create_dhcpv4(serv, pack);
K
Kozlov Dmitry 已提交
1309 1310
			if (ses) {
				dhcpv4_packet_ref(pack);
K
Kozlov Dmitry 已提交
1311

K
Kozlov Dmitry 已提交
1312 1313 1314 1315 1316
				if (conf_verbose) {
					log_switch(dhcpv4->ctx, &ses->ses);
					log_ppp_info2("recv ");
					dhcpv4_print_packet(pack, 0, log_ppp_info2);
				}
K
Kozlov Dmitry 已提交
1317 1318
			}
		}	else {
K
Kozlov Dmitry 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
			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 已提交
1331 1332 1333 1334
			log_switch(dhcpv4->ctx, &ses->ses);

			if (conf_verbose) {
				log_ppp_info2("recv ");
K
Kozlov Dmitry 已提交
1335
				dhcpv4_print_packet(pack, 0, log_ppp_info2);
K
Kozlov Dmitry 已提交
1336 1337
			}

1338 1339
			if (ses->yiaddr)
				dhcpv4_send_reply(DHCPOFFER, dhcpv4, pack, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->dhcpv4_relay_reply);
K
Kozlov Dmitry 已提交
1340 1341
		}
	} else if (pack->msg_type == DHCPREQUEST) {
D
Dmitry Kozlov 已提交
1342 1343
		ipoe_serv_check_disc(serv, pack);
		
K
Kozlov Dmitry 已提交
1344
		ses = ipoe_session_lookup(serv, pack, &opt82_ses);
K
Kozlov Dmitry 已提交
1345 1346 1347

		if (!ses) {
			if (conf_verbose) {
D
Dmitry Kozlov 已提交
1348 1349
				log_debug("recv ");
				dhcpv4_print_packet(pack, 0, log_debug);
K
Kozlov Dmitry 已提交
1350
			}
D
Dmitry Kozlov 已提交
1351

1352 1353 1354 1355
			if (!pack->server_id)
				dhcpv4_send_nak(dhcpv4, pack);

			if (serv->opt_shared == 0)
1356
				ipoe_drop_sessions(serv, NULL);
1357
			else if (opt82_ses) {
K
Kozlov Dmitry 已提交
1358 1359 1360 1361 1362 1363 1364
				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 已提交
1365
		} else {
D
Dmitry Kozlov 已提交
1366 1367
			ses->xid = pack->hdr->xid;

1368
			if ((pack->server_id && (pack->server_id != ses->siaddr || pack->request_ip != ses->yiaddr)) ||
K
Kozlov Dmitry 已提交
1369 1370
				(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 已提交
1371

K
Kozlov Dmitry 已提交
1372
				if (conf_verbose) {
K
Kozlov Dmitry 已提交
1373 1374
					log_switch(dhcpv4->ctx, &ses->ses);
					log_ppp_info2("recv ");
K
Kozlov Dmitry 已提交
1375
					dhcpv4_print_packet(pack, 0, log_info2);
K
Kozlov Dmitry 已提交
1376 1377
					if ((opt82_ses && ses != opt82_ses) || (!opt82_ses && pack->relay_agent))
						log_ppp_warn("port change detected\n");
K
Kozlov Dmitry 已提交
1378 1379
				}

K
Kozlov Dmitry 已提交
1380
				if (pack->server_id == ses->siaddr)
K
Kozlov Dmitry 已提交
1381
					dhcpv4_send_nak(dhcpv4, pack);
D
Dmitry Kozlov 已提交
1382 1383

				if (ses->serv->dhcpv4_relay)
1384
					dhcpv4_relay_send(ses->serv->dhcpv4_relay, pack, 0, ses->serv->ifname, conf_agent_remote_id);
K
Kozlov Dmitry 已提交
1385 1386
				
				triton_context_call(&ses->ctx, (triton_event_func)__ipoe_session_terminate, &ses->ses);
K
Kozlov Dmitry 已提交
1387 1388 1389 1390
			} else {
				if (conf_verbose) {
					log_switch(dhcpv4->ctx, &ses->ses);
					log_ppp_info2("recv ");
K
Kozlov Dmitry 已提交
1391
					dhcpv4_print_packet(pack, 0, log_ppp_info2);
K
Kozlov Dmitry 已提交
1392 1393
				}

K
Kozlov Dmitry 已提交
1394
				if (serv->opt_shared == 0)
K
Kozlov Dmitry 已提交
1395 1396
					ipoe_drop_sessions(serv, ses);

1397
				if (ses->ses.state == AP_STATE_STARTING && ses->yiaddr) {
K
Kozlov Dmitry 已提交
1398
					dhcpv4_packet_ref(pack);
1399
					triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_activate, pack);
K
Kozlov Dmitry 已提交
1400 1401 1402
				} 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 已提交
1403 1404 1405 1406
				}
			}
		}
	} else if (pack->msg_type == DHCPDECLINE || pack->msg_type == DHCPRELEASE) {
K
Kozlov Dmitry 已提交
1407
		ses = ipoe_session_lookup(serv, pack, &opt82_ses);
K
Kozlov Dmitry 已提交
1408
		if (ses) {
D
Dmitry Kozlov 已提交
1409
			ses->xid = pack->hdr->xid;
K
Kozlov Dmitry 已提交
1410 1411
			dhcpv4_packet_ref(pack);
			triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_decline, pack);
K
Kozlov Dmitry 已提交
1412
		}
K
Kozlov Dmitry 已提交
1413
	}
K
Kozlov Dmitry 已提交
1414 1415

out:
K
Kozlov Dmitry 已提交
1416 1417 1418
	pthread_mutex_unlock(&serv->lock);
}

D
Dmitry Kozlov 已提交
1419 1420 1421 1422 1423
static void ipoe_recv_dhcpv4(struct dhcpv4_serv *dhcpv4, struct dhcpv4_packet *pack)
{
	__ipoe_recv_dhcpv4(dhcpv4, pack, 0);
}

K
Kozlov Dmitry 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432
static int parse_dhcpv4_mask(uint32_t mask)
{
	int i;

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

	return 32 - (i + 1);
}

1433
static void ipoe_ses_recv_dhcpv4_relay(struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
1434
{
1435
	struct ipoe_session *ses = container_of(triton_context_self(), typeof(*ses), ctx);
K
Kozlov Dmitry 已提交
1436 1437
	struct dhcpv4_option *opt;

1438 1439 1440
	if (ses->dhcpv4_relay_reply)
		dhcpv4_packet_free(ses->dhcpv4_relay_reply);
	
1441 1442 1443 1444 1445
	if (!ses->dhcpv4_request) {
		ses->dhcpv4_relay_reply = NULL;
		return;
	}

1446 1447
	ses->dhcpv4_relay_reply = pack;

K
Kozlov Dmitry 已提交
1448 1449 1450 1451 1452
	if (conf_verbose) {
		log_ppp_info2("recv ");
		dhcpv4_print_packet(pack, 1, log_ppp_info2);
	}

1453 1454 1455
	opt = dhcpv4_packet_find_opt(pack, 51);
	if (opt)
		ses->lease_time = ntohl(*(uint32_t *)opt->data);
K
Kozlov Dmitry 已提交
1456

1457 1458 1459
	opt = dhcpv4_packet_find_opt(pack, 1);
	if (opt)
		ses->mask = parse_dhcpv4_mask(ntohl(*(uint32_t *)opt->data));
K
Kozlov Dmitry 已提交
1460

1461 1462 1463 1464
	opt = dhcpv4_packet_find_opt(pack, 3);
	if (opt)
		ses->router = *(uint32_t *)opt->data;

1465 1466 1467
	if (pack->msg_type == DHCPOFFER) {
		if (ses->ses.state == AP_STATE_STARTING) {
			triton_timer_del(&ses->timer);
K
Kozlov Dmitry 已提交
1468

1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
			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 已提交
1479 1480 1481 1482
	} else if (pack->msg_type == DHCPACK) {
		if (ses->ses.state == AP_STATE_STARTING)
			__ipoe_session_activate(ses);
		else
1483
			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 已提交
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502

	} 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 已提交
1503
		dhcpv4_packet_free(pack);
K
Kozlov Dmitry 已提交
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
		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 已提交
1516
	}
K
Kozlov Dmitry 已提交
1517
	
1518 1519
	if (found) {
		triton_context_call(&ses->ctx, (triton_event_func)ipoe_ses_recv_dhcpv4_relay, pack);
K
Kozlov Dmitry 已提交
1520 1521 1522
	} else
		dhcpv4_packet_free(pack);

K
Kozlov Dmitry 已提交
1523 1524 1525
	pthread_mutex_unlock(&serv->lock);
}

K
Kozlov Dmitry 已提交
1526

1527 1528 1529 1530
static struct ipoe_session *ipoe_session_create_up(struct ipoe_serv *serv, struct ethhdr *eth, struct iphdr *iph)
{
	struct ipoe_session *ses;

1531 1532
	if (ap_shutdown)
		return NULL;
1533 1534 1535 1536
	
	if (l4_redirect_list_check(iph->saddr))
		return NULL;
	
1537 1538
	ses = ipoe_session_alloc();
	if (!ses)
1539 1540 1541 1542
		return NULL;

	ses->serv = serv;
	memcpy(ses->hwaddr, eth->h_source, 6);
1543
	ses->yiaddr = iph->saddr;
K
Kozlov Dmitry 已提交
1544

1545
	ses->ctrl.calling_station_id = _malloc(17);
1546
	ses->ctrl.called_station_id = _strdup(serv->ifname);
1547 1548

	u_inet_ntoa(iph->saddr, ses->ctrl.calling_station_id);
1549 1550
	
	ses->ses.chan_name = ses->ctrl.calling_station_id;
1551 1552

	if (conf_username == USERNAME_UNSET)
1553
		ap_session_set_username(&ses->ses, _strdup(ses->ctrl.calling_station_id));
1554
	
1555 1556 1557
	if (conf_ip_pool)
		ses->ses.ipv4_pool_name = _strdup(conf_ip_pool);
	
1558 1559 1560 1561 1562 1563 1564 1565
	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 已提交
1566 1567 1568
	if (serv->timer.tpd)
		triton_timer_del(&serv->timer);

1569 1570 1571 1572 1573
	triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_start, ses);

	return ses;
}

1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
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;
1600
	
1601 1602 1603
	return ses;
}

1604 1605 1606 1607 1608 1609 1610 1611
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 已提交
1612 1613 1614

		if (!serv->opt_up)
			return;
1615 1616 1617
		
		pthread_mutex_lock(&serv->lock);
		list_for_each_entry(ses, &serv->sessions, entry) {
1618
			if (ses->yiaddr == iph->saddr) {
1619 1620 1621 1622 1623 1624 1625
				pthread_mutex_unlock(&serv->lock);
				return;
			}
		}
		pthread_mutex_unlock(&serv->lock);
		
		ipoe_session_create_up(serv, eth, iph);
1626 1627

		break;
1628 1629 1630
	}
}

1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
#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)
1644
			ses->router = attr->val.ipaddr;
1645
		else if (attr->attr->id == conf_attr_dhcp_mask) {
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
			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
			}
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
		} 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 已提交
1690
	if (l4_redirect != ses->l4_redirect && ev->ses->state == AP_STATE_ACTIVE)
1691 1692 1693 1694
		ipoe_change_l4_redirect(ses, l4_redirect);
}
#endif

D
Dmitry Kozlov 已提交
1695
static void ipoe_serv_release(struct ipoe_serv *serv)
K
Kozlov Dmitry 已提交
1696
{
1697 1698 1699 1700 1701 1702
	pthread_mutex_lock(&serv->lock);
	if (!list_empty(&serv->sessions)) {
		pthread_mutex_unlock(&serv->lock);
		return;
	}
	pthread_mutex_unlock(&serv->lock);
D
Dmitry Kozlov 已提交
1703

D
Dmitry Kozlov 已提交
1704
	if (serv->vid && !serv->need_close && !ap_shutdown) {
D
Dmitry Kozlov 已提交
1705 1706 1707 1708 1709 1710 1711 1712
		if (serv->timer.tpd)
			triton_timer_mod(&serv->timer, 0);
		else
			triton_timer_add(&serv->ctx, &serv->timer, 0);

		return;
	}
	
D
Dmitry Kozlov 已提交
1713 1714 1715 1716 1717
	log_info2("ipoe: stop interface %s\n", serv->ifname);

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

K
Kozlov Dmitry 已提交
1719 1720
	if (serv->dhcpv4)
		dhcpv4_free(serv->dhcpv4);
K
Kozlov Dmitry 已提交
1721 1722 1723 1724 1725
	
	if (serv->dhcpv4_relay) {
		ipoe_serv_del_addr(serv, serv->dhcpv4_relay->giaddr);
		dhcpv4_relay_free(serv->dhcpv4_relay, &serv->ctx);
	}
K
Kozlov Dmitry 已提交
1726

1727 1728 1729
	if (serv->arp)
		arpd_stop(serv->arp);

D
Dmitry Kozlov 已提交
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
	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);
	}

	if (serv->disc_timer.tpd)
		triton_timer_del(&serv->disc_timer);
D
Dmitry Kozlov 已提交
1740 1741 1742 1743 1744 1745 1746 1747 1748
	
	if (serv->timer.tpd)
		triton_timer_del(&serv->timer);

	if (serv->vid) {
		log_info2("ipoe: remove vlan %s\n", serv->ifname);
		iplink_vlan_del(serv->ifindex);
		ipoe_nl_add_vlan_mon_vid(serv->parent_ifindex, serv->vid);
	}
D
Dmitry Kozlov 已提交
1749

D
Dmitry Kozlov 已提交
1750
	triton_context_unregister(&serv->ctx);
K
Kozlov Dmitry 已提交
1751 1752 1753 1754 1755

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

D
Dmitry Kozlov 已提交
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
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);
}

1771 1772 1773 1774 1775 1776 1777 1778
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);
1779 1780 1781 1782 1783 1784 1785

		if (conf_l4_redirect_table)
			iprule_del(n->addr, conf_l4_redirect_table);
		
		if (conf_l4_redirect_ipset)
			ipset_del(conf_l4_redirect_ipset, n->addr);
		
1786
		ipoe_nl_delete(n->ifindex);
1787
		
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
		_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 已提交
1798 1799 1800 1801 1802
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 已提交
1803
	cli_sendv(client,"  delayed: %u\r\n", stat_delayed_offer);
K
Kozlov Dmitry 已提交
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813

	return CLI_CMD_OK;
}

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

K
Kozlov Dmitry 已提交
1814 1815 1816 1817 1818 1819
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 已提交
1820
{
K
Kozlov Dmitry 已提交
1821 1822 1823 1824 1825 1826
	struct ipoe_session *ses;

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

1827 1828
		ses->terminating = 1;
		if (ses->ifcfg) {
D
Dmitry Kozlov 已提交
1829
			ipoe_ifcfg_del(ses, 0);
1830 1831 1832
			ses->ifcfg = 0;
		}

K
Kozlov Dmitry 已提交
1833 1834
		if (ses->ses.state == AP_STATE_ACTIVE)
			ap_session_ifdown(&ses->ses);
K
Kozlov Dmitry 已提交
1835

K
Kozlov Dmitry 已提交
1836 1837
		triton_context_call(&ses->ctx, (triton_event_func)__terminate, &ses->ses);
	}
K
Kozlov Dmitry 已提交
1838 1839
}

1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
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 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
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;
	
	return 0;
}

1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
static int make_vlan_name(const char *parent, int vid, char *name)
{
	char *ptr1 = name, *endptr = name + IFNAMSIZ - 1;
	const char *ptr2 = conf_vlan_name;
	char num[5], *ptr3 = num;

	sprintf(num, "%i", vid);

	while (ptr1 < endptr && *ptr2) {
		if (ptr2[0] == '%' && ptr2[1] == 'I') {
			while (ptr1 < endptr && *parent)
				*ptr1++ = *parent++;
			ptr2 += 2;
		} else if (ptr2[0] == '%' && ptr2[1] == 'N') {
			while (ptr1 < endptr && *ptr3)
				*ptr1++ = *ptr3++;
			ptr2 += 2;
		} else
			*ptr1++ = *ptr2++;
	}

	*ptr1 = 0;

	return ptr1 == endptr;
}

D
Dmitry Kozlov 已提交
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
void ipoe_vlan_notify(int ifindex, int vid)
{
	struct conf_sect_t *sect = conf_get_section("ipoe");
	struct conf_option_t *opt;
	struct ifreq ifr;
	char *ptr;
	int len, r;
	pcre *re = NULL;
	const char *pcre_err;
	char *pattern;
	int pcre_offset;
1907
	char ifname[IFNAMSIZ];
D
Dmitry Kozlov 已提交
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918

	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;
	}
	
1919
	if (make_vlan_name(ifr.ifr_name, vid, ifname)) {
D
Dmitry Kozlov 已提交
1920 1921 1922
		log_error("ipoe: vlan-mon: %s.%i: interface name is too long\n", ifr.ifr_name, vid);
		return;
	}
1923 1924

	strcpy(ifr.ifr_name, ifname);
D
Dmitry Kozlov 已提交
1925 1926
	len = strlen(ifr.ifr_name);

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

D
Dmitry Kozlov 已提交
1929
	if (iplink_vlan_add(ifr.ifr_name, ifindex, vid)) {
D
Dmitry Kozlov 已提交
1930
		log_warn("ipoe: vlan-mon: %s: failed to add vlan\n", ifr.ifr_name);
D
Dmitry Kozlov 已提交
1931 1932
		return;
	}
D
Dmitry Kozlov 已提交
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
	
	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;
		
		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;
			
			re = pcre_compile2(pattern, 0, NULL, &pcre_err, &pcre_offset, NULL);
			
			_free(pattern);
				
			if (!re)
				continue;

			r = pcre_exec(re, NULL, ifr.ifr_name, len, 0, 0, NULL, 0);
			pcre_free(re);
			
			if (r < 0)
				continue;
			
			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 已提交
1977 1978 1979
	serv->need_close = 1;
	
	ipoe_serv_release(serv);
D
Dmitry Kozlov 已提交
1980 1981 1982
}

static void add_interface(const char *ifname, int ifindex, const char *opt, int parent_ifindex, int vid)
K
Kozlov Dmitry 已提交
1983
{
K
Kozlov Dmitry 已提交
1984
	char *str0 = NULL, *str, *ptr1, *ptr2;
K
Kozlov Dmitry 已提交
1985
	int end;
K
Kozlov Dmitry 已提交
1986
	struct ipoe_serv *serv;
K
Kozlov Dmitry 已提交
1987 1988 1989 1990
	int opt_shared = conf_shared;
	int opt_dhcpv4 = 0;
	int opt_up = 0;
	int opt_mode = conf_mode;
1991
	int opt_ifcfg = conf_ifcfg;
1992
	int opt_nat = conf_nat;
1993 1994 1995 1996
	int opt_username = conf_username;
#ifdef USE_LUA
	char *opt_lua_username_func = NULL;
#endif
K
Kozlov Dmitry 已提交
1997
	const char *opt_relay = conf_relay;
1998 1999
	in_addr_t relay_addr = conf_relay ? inet_addr(conf_relay) : 0;
	in_addr_t opt_giaddr = 0;
2000
	in_addr_t opt_src = conf_src;
2001 2002
	int opt_arp = conf_arp;
	struct ifreq ifr;
K
Kozlov Dmitry 已提交
2003 2004 2005 2006 2007 2008 2009 2010

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

K
Kozlov Dmitry 已提交
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
			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;
2043 2044
			} else if (strcmp(str, "ifcfg") == 0) {
				opt_ifcfg = atoi(ptr1);
K
Kozlov Dmitry 已提交
2045 2046 2047 2048
			} else if (strcmp(str, "relay") == 0) {
				opt_relay = ptr1;
				relay_addr = inet_addr(ptr1);
			} else if (strcmp(str, "giaddr") == 0) {
2049
				opt_giaddr = inet_addr(ptr1);
2050 2051
			} else if (strcmp(str, "nat") == 0) {
				opt_nat = atoi(ptr1);
2052 2053
			} else if (strcmp(str, "src") == 0) {
				opt_src = inet_addr(ptr1);
2054 2055
			} else if (strcmp(str, "proxy-arp") == 0) {
				opt_arp = atoi(ptr1);
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
			} 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);
				} 
#endif
				else
					log_error("ipoe: unknown username value '%s'\n", ptr1);
2067
			}
K
Kozlov Dmitry 已提交
2068 2069 2070 2071 2072 2073

			if (end)
				break;

			str = ptr2 + 1;
		}
D
Dmitry Kozlov 已提交
2074
	}
K
Kozlov Dmitry 已提交
2075 2076 2077 2078 2079 2080

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

2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
	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);
		
		sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
		
		if (connect(sock, &addr, sizeof(addr))) {
			log_error("dhcpv4: relay: %s: connect: %s\n", opt_relay, strerror(errno));
			goto out_err;
		}
		
		getsockname(sock, &addr, &len);
		opt_giaddr = addr.sin_addr.s_addr;

		close(sock);
	}

2104 2105 2106
	if (opt_up)
		ipoe_nl_add_interface(ifindex);

D
Dmitry Kozlov 已提交
2107
	pthread_mutex_lock(&serv_lock);
K
Kozlov Dmitry 已提交
2108
	list_for_each_entry(serv, &serv_list, entry) {
2109
		if (strcmp(ifname, serv->ifname))
K
Kozlov Dmitry 已提交
2110 2111 2112 2113 2114 2115 2116 2117
			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 已提交
2118
		}
K
Kozlov Dmitry 已提交
2119 2120

		if (opt_dhcpv4 && !serv->dhcpv4) {
2121
			serv->dhcpv4 = dhcpv4_create(&serv->ctx, serv->ifname, opt);
K
Kozlov Dmitry 已提交
2122 2123 2124 2125 2126 2127 2128
			if (serv->dhcpv4)
				serv->dhcpv4->recv = ipoe_recv_dhcpv4;
		} else if (!opt_dhcpv4 && serv->dhcpv4) {
			dhcpv4_free(serv->dhcpv4);
			serv->dhcpv4 = NULL;
		}

2129 2130
		if (serv->dhcpv4_relay &&  
				(serv->dhcpv4_relay->addr != relay_addr || serv->dhcpv4_relay->giaddr != opt_giaddr)) {
2131 2132
			if (serv->opt_ifcfg)
				ipoe_serv_del_addr(serv, serv->dhcpv4_relay->giaddr);
K
Kozlov Dmitry 已提交
2133 2134 2135 2136
			dhcpv4_relay_free(serv->dhcpv4_relay, &serv->ctx);
			serv->dhcpv4_relay = NULL;
		}

2137
		if (!serv->dhcpv4_relay && serv->opt_dhcpv4 && opt_relay) {
2138
			if (opt_ifcfg)
2139
				ipoe_serv_add_addr(serv, opt_giaddr);
K
Kozlov Dmitry 已提交
2140
			serv->dhcpv4_relay = dhcpv4_relay_create(opt_relay, opt_giaddr, &serv->ctx, (triton_event_func)ipoe_recv_dhcpv4_relay);
K
Kozlov Dmitry 已提交
2141
		}
2142 2143 2144 2145 2146 2147

		if (serv->arp && !conf_arp) {
			arpd_stop(serv->arp);
			serv->arp = NULL;
		} else if (!serv->arp && conf_arp)
			serv->arp = arpd_start(serv);
2148 2149 2150 2151
		
		serv->opt_up = opt_up;
		serv->opt_mode = opt_mode;
		serv->opt_ifcfg = opt_ifcfg;
2152
		serv->opt_nat = opt_nat;
2153
		serv->opt_src = opt_src;
2154
		serv->opt_arp = opt_arp;
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
		serv->opt_username = opt_username;
#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;
		}
		
		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 已提交
2167 2168 2169 2170

		if (str0)
			_free(str0);

D
Dmitry Kozlov 已提交
2171
		pthread_mutex_unlock(&serv_lock);
K
Kozlov Dmitry 已提交
2172
		return;
K
Kozlov Dmitry 已提交
2173
	}
D
Dmitry Kozlov 已提交
2174
	pthread_mutex_unlock(&serv_lock);
K
Kozlov Dmitry 已提交
2175

2176 2177 2178 2179 2180
	opt = strchr(opt, ',');
	if (opt)
		opt++;

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

2182 2183 2184 2185 2186 2187 2188
	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;
	}
D
Dmitry Kozlov 已提交
2189 2190 2191 2192 2193 2194 2195 2196
	
	ioctl(sock_fd, SIOCGIFFLAGS, &ifr);
		
	if (!(ifr.ifr_flags & IFF_UP)) {
		ifr.ifr_flags |= IFF_UP;

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

K
Kozlov Dmitry 已提交
2198 2199
	serv = _malloc(sizeof(*serv));
	memset(serv, 0, sizeof(*serv));
2200
	serv->ctx.close = ipoe_serv_close;
D
Dmitry Kozlov 已提交
2201
	pthread_mutex_init(&serv->lock, NULL);
K
Kozlov Dmitry 已提交
2202 2203
	serv->ifname = _strdup(ifname);
	serv->ifindex = ifindex;
K
Kozlov Dmitry 已提交
2204 2205 2206 2207
	serv->opt_shared = opt_shared;
	serv->opt_dhcpv4 = opt_dhcpv4;
	serv->opt_up = opt_up;
	serv->opt_mode = opt_mode;
2208
	serv->opt_ifcfg = opt_ifcfg;
2209
	serv->opt_nat = opt_nat;
2210
	serv->opt_src = opt_src;
2211
	serv->opt_arp = opt_arp;
2212 2213 2214 2215
	serv->opt_username = opt_username;
#ifdef USE_LUA
	serv->opt_lua_username_func = opt_lua_username_func;
#endif
D
Dmitry Kozlov 已提交
2216 2217
	serv->parent_ifindex = parent_ifindex = parent_ifindex;
	serv->vid = vid;
2218
	serv->active = 1;
K
Kozlov Dmitry 已提交
2219
	INIT_LIST_HEAD(&serv->sessions);
2220
	INIT_LIST_HEAD(&serv->addr_list);
D
Dmitry Kozlov 已提交
2221
	INIT_LIST_HEAD(&serv->disc_list);
2222
	memcpy(serv->hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
D
Dmitry Kozlov 已提交
2223
	serv->disc_timer.expire = ipoe_serv_disc_timer;
D
Dmitry Kozlov 已提交
2224
	
K
Kozlov Dmitry 已提交
2225 2226 2227
	triton_context_register(&serv->ctx, NULL);

	if (serv->opt_dhcpv4) {
2228
		serv->dhcpv4 = dhcpv4_create(&serv->ctx, serv->ifname, opt);
K
Kozlov Dmitry 已提交
2229
		if (serv->dhcpv4)
2230
			serv->dhcpv4->recv = ipoe_recv_dhcpv4;
K
Kozlov Dmitry 已提交
2231
	
2232
		if (opt_relay) {
2233
			if (opt_ifcfg)
2234
				ipoe_serv_add_addr(serv, opt_giaddr);
K
Kozlov Dmitry 已提交
2235 2236
			serv->dhcpv4_relay = dhcpv4_relay_create(opt_relay, opt_giaddr, &serv->ctx, (triton_event_func)ipoe_recv_dhcpv4_relay);
		}
K
Kozlov Dmitry 已提交
2237 2238
	}

2239 2240
	if (serv->opt_arp)
		serv->arp = arpd_start(serv);
D
Dmitry Kozlov 已提交
2241 2242 2243 2244 2245 2246
	
	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);
	}
2247

K
Kozlov Dmitry 已提交
2248 2249
	triton_context_wakeup(&serv->ctx);

D
Dmitry Kozlov 已提交
2250
	pthread_mutex_lock(&serv_lock);
2251
	list_add_tail(&serv->entry, &serv_list);
D
Dmitry Kozlov 已提交
2252
	pthread_mutex_unlock(&serv_lock);
2253

K
Kozlov Dmitry 已提交
2254 2255 2256
	if (str0)
		_free(str0);

K
Kozlov Dmitry 已提交
2257 2258
	return;

K
Kozlov Dmitry 已提交
2259
parse_err:
K
Kozlov Dmitry 已提交
2260
	log_error("ipoe: failed to parse '%s'\n", opt);
2261
out_err:
K
Kozlov Dmitry 已提交
2262
	_free(str0);
K
Kozlov Dmitry 已提交
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
}

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;
	}
2282
	
D
Dmitry Kozlov 已提交
2283
	add_interface(ifr.ifr_name, ifr.ifr_ifindex, opt, 0, 0);
K
Kozlov Dmitry 已提交
2284 2285 2286 2287 2288 2289
}

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

D
Dmitry Kozlov 已提交
2291
	add_interface(name, index, arg->opt, 0, 0);
K
Kozlov Dmitry 已提交
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313

	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) {
D
Dmitry Kozlov 已提交
2314
		log_error("ipoe: '%s': %s at %i\r\n", pattern, pcre_err, pcre_offset);
K
Kozlov Dmitry 已提交
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
		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;

2332 2333
	ipoe_nl_delete_interfaces();

K
Kozlov Dmitry 已提交
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
	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 已提交
2349
	list_for_each_entry(serv, &serv_list, entry) {
D
Dmitry Kozlov 已提交
2350
		if (!serv->active && !serv->vid) {
2351
			ipoe_drop_sessions(serv, NULL);
D
Dmitry Kozlov 已提交
2352
			triton_context_call(&serv->ctx, (triton_event_func)ipoe_serv_release, serv);
K
Kozlov Dmitry 已提交
2353 2354 2355 2356
		}
	}
}

2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
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;
	}

2382 2383 2384
	if (mask == 32)
		mask = 0xffffffff;
	else
K
Kozlov Dmitry 已提交
2385 2386 2387 2388 2389 2390
		mask = (1 << (32-mask)) - 1;

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

	//printf("%x/%x %x\n", htonl(addr), ~mask, htonl(addr)&(~mask));
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414

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

2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
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;
		}
2451 2452

		a->mask1 = ((1 << a->mask) - 1) << (32 - a->mask);
2453 2454 2455 2456
		list_add_tail(&a->entry, &conf_gw_addr);
	}
}

2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476
#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 已提交
2477

2478 2479 2480 2481 2482 2483 2484 2485 2486
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

D
Dmitry Kozlov 已提交
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
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 已提交
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
static int parse_vlan_mon(const char *opt, long *mask)
{
	char *ptr, *ptr2;
	int vid, vid2;

	ptr = strchr(opt, ',');
	if (!ptr)
		ptr = strchr(opt, 0);

	if (*ptr == ',')
D
Dmitry Kozlov 已提交
2572
		memset(mask, 0xff, 4096/8);
D
Dmitry Kozlov 已提交
2573
	else if (*ptr == 0) {
D
Dmitry Kozlov 已提交
2574
		memset(mask, 0, 4096/8);
D
Dmitry Kozlov 已提交
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
		return 0;
	} else
		goto out_err;

	while (1) {
		vid = strtol(ptr + 1, &ptr2, 10);
		if (vid <= 0 || vid >= 4096) {
			log_error("ipoe: vlan-mon=%s: invalid vlan %i\n", opt, vid);
			return -1;
		}

		if (*ptr2 == '-') {
			vid2 = strtol(ptr2 + 1, &ptr2, 10);
			if (vid2 <= 0 || vid2 >= 4096) {
				log_error("ipoe: vlan-mon=%s: invalid vlan %i\n", opt, vid2);
				return -1;
			}
			
			for (; vid < vid2; vid++)
D
Dmitry Kozlov 已提交
2594
				mask[vid / (8*sizeof(long))] &= ~(1lu << (vid % (8*sizeof(long))));
D
Dmitry Kozlov 已提交
2595 2596
		}
			
D
Dmitry Kozlov 已提交
2597
		mask[vid / (8*sizeof(long))] &= ~(1lu << (vid % (8*sizeof(long))));
D
Dmitry Kozlov 已提交
2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614

		if (*ptr2 == 0)
			break;

		if (*ptr2 != ',')
			goto out_err;

		ptr = ptr2;
	}

	return 0;
		
out_err:
	log_error("ipoe: vlan-mon=%s: failed to parse\n", opt);
	return -1;
}

D
Dmitry Kozlov 已提交
2615
static void add_vlan_mon(const char *opt, long *mask)
D
Dmitry Kozlov 已提交
2616 2617 2618 2619
{
	const char *ptr;
	struct ifreq ifr;
	int ifindex;
D
Dmitry Kozlov 已提交
2620 2621
	long mask1[4096/8/sizeof(long)];
	struct ipoe_serv *serv;
D
Dmitry Kozlov 已提交
2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649
	
	for (ptr = opt; *ptr && *ptr != ','; ptr++);
	
	if (ptr - opt >= sizeof(ifr.ifr_name)) {
		log_error("ipoe: vlan-mon=%s: interface name is too long\n", opt);
		return;
	}

	memset(&ifr, 0, sizeof(ifr));
	
	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;
	
	ioctl(sock_fd, SIOCGIFFLAGS, &ifr);
	
	if (!(ifr.ifr_flags & IFF_UP)) {
		ifr.ifr_flags |= IFF_UP;

		ioctl(sock_fd, SIOCSIFFLAGS, &ifr);
	}

D
Dmitry Kozlov 已提交
2650 2651 2652 2653 2654 2655 2656
	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)));
	}

	ipoe_nl_add_vlan_mon(ifindex, mask1, sizeof(mask1));
D
Dmitry Kozlov 已提交
2657 2658 2659 2660 2661
}

static int __load_vlan_mon_re(int index, int flags, const char *name, struct iplink_arg *arg)
{
	struct ifreq ifr;
D
Dmitry Kozlov 已提交
2662 2663
	long mask1[4096/8/sizeof(long)];
	struct ipoe_serv *serv;
D
Dmitry Kozlov 已提交
2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677

	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);
	
	ioctl(sock_fd, SIOCGIFFLAGS, &ifr);
	
	if (!(ifr.ifr_flags & IFF_UP)) {
		ifr.ifr_flags |= IFF_UP;

		ioctl(sock_fd, SIOCSIFFLAGS, &ifr);
	}
D
Dmitry Kozlov 已提交
2678 2679 2680 2681 2682 2683
	
	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 已提交
2684

D
Dmitry Kozlov 已提交
2685
	ipoe_nl_add_vlan_mon(index, mask1, sizeof(mask1));
D
Dmitry Kozlov 已提交
2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742

	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++);
	
	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': %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)];

	ipoe_nl_del_vlan_mon(-1);

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

		if (!opt->val)
			continue;
	
		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 已提交
2743
			add_vlan_mon(opt->val, mask);
D
Dmitry Kozlov 已提交
2744 2745 2746 2747
	}
}


K
Kozlov Dmitry 已提交
2748 2749 2750 2751
static void load_config(void)
{
	const char *opt;
	struct conf_sect_t *s = conf_get_section("ipoe");
K
Kozlov Dmitry 已提交
2752
	struct conf_option_t *opt1;
K
Kozlov Dmitry 已提交
2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764

	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 已提交
2765
		}
K
Kozlov Dmitry 已提交
2766
#endif
K
Kozlov Dmitry 已提交
2767
		else
K
Kozlov Dmitry 已提交
2768
			log_emerg("ipoe: unknown username value '%s'\n", opt);
2769 2770
	} else
		conf_username = USERNAME_UNSET;
2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
	
	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 已提交
2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795

	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 已提交
2796 2797 2798 2799

	opt = conf_get_opt("ipoe", "lease-time");
	if (opt)
		conf_lease_time = atoi(opt);
K
Kozlov Dmitry 已提交
2800 2801
	else
		conf_lease_time = 600;
K
Kozlov Dmitry 已提交
2802
	
K
Kozlov Dmitry 已提交
2803
	opt = conf_get_opt("ipoe", "max-lease-time");
K
Kozlov Dmitry 已提交
2804 2805
	if (opt)
		conf_lease_timeout = atoi(opt);
K
Kozlov Dmitry 已提交
2806 2807
	else
		conf_lease_timeout = 660;
K
Kozlov Dmitry 已提交
2808
	
K
Kozlov Dmitry 已提交
2809 2810 2811 2812
	opt = conf_get_opt("ipoe", "unit-cache");
	if (opt)
		conf_unit_cache = atoi(opt);
	
2813
	opt = conf_get_opt("ipoe", "l4-redirect-table");
2814
	if (opt && atoi(opt) > 0)
2815 2816
		conf_l4_redirect_table = atoi(opt);
	else
2817 2818 2819
		conf_l4_redirect_table = 0;
	
	conf_l4_redirect_ipset = conf_get_opt("ipoe", "l4-redirect-ipset");
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
	
	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);
	}
2832
	
K
Kozlov Dmitry 已提交
2833 2834 2835 2836 2837 2838
	opt = conf_get_opt("ipoe", "shared");
	if (opt)
		conf_shared = atoi(opt);
	else
		conf_shared = 1;
	
2839 2840 2841 2842 2843 2844
	opt = conf_get_opt("ipoe", "ifcfg");
	if (opt)
		conf_ifcfg = atoi(opt);
	else
		conf_ifcfg = 1;
	
2845 2846 2847 2848 2849
	opt = conf_get_opt("ipoe", "nat");
	if (opt)
		conf_nat = atoi(opt);
	else
		conf_nat = 0;
2850 2851 2852 2853 2854 2855

	opt = conf_get_opt("ipoe", "src");
	if (opt)
		conf_src = inet_addr(opt);
	else
		conf_src = 0;
2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866

	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;
	}
2867
	
K
Kozlov Dmitry 已提交
2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
	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 已提交
2878 2879
	
	conf_relay = conf_get_opt("ipoe", "relay");
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891

	opt = conf_get_opt("ipoe", "relay-timeout");
	if (opt && atoi(opt) > 0)
		conf_relay_timeout = atoi(opt);
	else
		conf_relay_timeout = 3;
	
	opt = conf_get_opt("ipoe", "relay-retransmit");
	if (opt && atoi(opt) > 0)
		conf_relay_retransmit = atoi(opt);
	else
		conf_relay_retransmit = 3;
2892 2893 2894 2895 2896 2897
	
	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 已提交
2898 2899 2900 2901 2902 2903
	
	opt = conf_get_opt("ipoe", "noauth");
	if (opt)
		conf_noauth = atoi(opt);
	else
		conf_noauth = 0;
K
Kozlov Dmitry 已提交
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919

	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;
	
2920 2921 2922 2923
	opt = conf_get_opt("ipoe", "proto");
	if (opt && atoi(opt) > 0)
		conf_proto = atoi(opt);
	else
2924
		conf_proto = 3;
2925
	
D
Dmitry Kozlov 已提交
2926 2927 2928 2929 2930 2931
	opt = conf_get_opt("ipoe", "vlan-timeout");
	if (opt && atoi(opt) > 0)
		conf_vlan_timeout = atoi(opt);
	else
		conf_vlan_timeout = 60;
	
2932 2933 2934 2935 2936 2937
	opt = conf_get_opt("ipoe", "offer-timeout");
	if (opt && atoi(opt) > 0)
		conf_offer_timeout = atoi(opt);
	else
		conf_offer_timeout = 10;
	
2938
	conf_ip_pool = conf_get_opt("ipoe", "ip-pool");
2939 2940 2941 2942

	conf_vlan_name = conf_get_opt("ipoe", "vlan-name");
	if (!conf_vlan_name)
		conf_vlan_name = "%I.%N";
2943
	
2944 2945 2946 2947
#ifdef RADIUS
	if (triton_module_loaded("radius"))
		load_radius_attrs();
#endif
D
Dmitry Kozlov 已提交
2948 2949

	parse_offer_delay(conf_get_opt("ipoe", "offer-delay"));
2950
	
K
Kozlov Dmitry 已提交
2951 2952
	load_interfaces(s);
	load_local_nets(s);
D
Dmitry Kozlov 已提交
2953
	load_vlan_mon(s);
2954
	load_gw_addr(s);
K
Kozlov Dmitry 已提交
2955 2956
}

2957 2958 2959 2960 2961 2962 2963 2964
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 已提交
2965 2966 2967
static void ipoe_init(void)
{
	ses_pool = mempool_create(sizeof(struct ipoe_session));
D
Dmitry Kozlov 已提交
2968
	disc_item_pool = mempool_create(sizeof(struct disc_item));
K
Kozlov Dmitry 已提交
2969
	uc_pool = mempool_create(sizeof(struct unit_cache));
D
Dmitry Kozlov 已提交
2970
	
2971 2972 2973
	triton_context_register(&l4_redirect_ctx, NULL);
	triton_context_wakeup(&l4_redirect_ctx);

K
Kozlov Dmitry 已提交
2974 2975
	load_config();

2976 2977 2978
	if (conf_l4_redirect_ipset)
		ipset_flush(conf_l4_redirect_ipset);

K
Kozlov Dmitry 已提交
2979 2980 2981
	cli_register_simple_cmd2(show_stat_exec, NULL, 2, "show", "stat");
	
	triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);
2982 2983

#ifdef RADIUS
2984
	if (triton_module_loaded("radius")) {
2985 2986
		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);
2987
	}
2988
#endif
2989 2990
	
	connlimit_loaded = triton_module_loaded("connlimit");
K
Kozlov Dmitry 已提交
2991 2992
}

2993
DEFINE_INIT(52, ipoe_init);