ipoe.c 23.7 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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>

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

#include "iplink.h"
#include "connlimit.h"

#include "ipoe.h"

#include "memdebug.h"

#define USERNAME_IFNAME 0
#define USERNAME_LUA 1

K
Kozlov Dmitry 已提交
40 41 42
#define MODE_L2 0
#define MODE_L3 1

K
Kozlov Dmitry 已提交
43
static int conf_dhcpv4 = 1;
K
Kozlov Dmitry 已提交
44 45 46
static int conf_up = 0;
static int conf_mode = 0;
static int conf_shared = 1;
K
Kozlov Dmitry 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
//static int conf_dhcpv6;
static int conf_username;

#ifdef USE_LUA
static const char *conf_lua_username_func;
#endif

static int conf_offer_timeout = 3;
static in_addr_t conf_gw_address;
static int conf_netmask = 24;
static int conf_lease_time = 600;
static int conf_lease_timeout = 660;
static int conf_verbose;

static unsigned int stat_starting;
static unsigned int stat_active;

static mempool_t ses_pool;

static LIST_HEAD(serv_list);

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

static void ipoe_session_finished(struct ap_session *s);
K
Kozlov Dmitry 已提交
75
static void ipoe_drop_sessions(struct ipoe_serv *serv, struct ipoe_session *skip);
K
Kozlov Dmitry 已提交
76 77 78 79

static struct ipoe_session *ipoe_session_lookup(struct ipoe_serv *serv, struct dhcpv4_packet *pack)
{
	struct ipoe_session *ses;
K
Kozlov Dmitry 已提交
80
	struct ipoe_session *ses1 = NULL;
K
Kozlov Dmitry 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

	list_for_each_entry(ses, &serv->sessions, entry) {
		if (pack->hdr->giaddr != ses->giaddr)
			continue;

		if (pack->agent_circuit_id && !ses->agent_circuit_id)
			continue;
		
		if (pack->agent_remote_id && !ses->agent_remote_id)
			continue;
		
		if (pack->client_id && !ses->client_id)
			continue;
		
		if (!pack->agent_circuit_id && ses->agent_circuit_id)
			continue;
		
		if (!pack->agent_remote_id && ses->agent_remote_id)
			continue;
		
		if (!pack->client_id && ses->client_id)
			continue;

		if (pack->agent_circuit_id) {
			if (pack->agent_circuit_id->len != ses->agent_circuit_id->len)
				continue;
			if (memcmp(pack->agent_circuit_id->data, ses->agent_circuit_id->data, pack->agent_circuit_id->len))
				continue;
		}
		
		if (pack->agent_remote_id) {
			if (pack->agent_remote_id->len != ses->agent_remote_id->len)
				continue;
			if (memcmp(pack->agent_remote_id->data, ses->agent_remote_id->data, pack->agent_remote_id->len))
				continue;
		}
		
		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;
		}

		if (memcmp(pack->hdr->chaddr, ses->hwaddr, 6))
			continue;

K
Kozlov Dmitry 已提交
128 129 130 131 132
		ses1 = ses;

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

K
Kozlov Dmitry 已提交
133 134 135
		return ses;
	}

K
Kozlov Dmitry 已提交
136
	return ses1;
K
Kozlov Dmitry 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
}

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

	triton_timer_del(t);

	log_ppp_info2("session timed out\n");

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

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

static void ipoe_session_start(struct ipoe_session *ses)
{
	int r;
	char *passwd;
164
	struct ifreq ifr;
K
Kozlov Dmitry 已提交
165

K
Kozlov Dmitry 已提交
166
	if (ses->serv->opt_shared == 0)
K
Kozlov Dmitry 已提交
167
		strncpy(ses->ses.ifname, ses->serv->ifname, AP_IFNAME_LEN);
168
	else {
K
Kozlov Dmitry 已提交
169
		ses->ifindex = ipoe_nl_create(0, 0, ses->dhcpv4_request ? ses->serv->ifname : NULL, ses->hwaddr);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		if (ses->ifindex == -1) {
			log_ppp_error("ipoe: failed to create interface\n");
			ipoe_session_finished(&ses->ses);
			return;
		}

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

		strncpy(ses->ses.ifname, ifr.ifr_name, AP_IFNAME_LEN);
K
Kozlov Dmitry 已提交
186
		ses->ses.ifindex = ses->ifindex;
187
	}
K
Kozlov Dmitry 已提交
188 189
	
	if (!ses->ses.username) {
190 191 192 193 194 195
		ipoe_session_set_username(ses);

		if (!ses->ses.username) {
			ipoe_session_finished(&ses->ses);
			return;
		}
K
Kozlov Dmitry 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	}
	
	triton_event_fire(EV_CTRL_STARTING, &ses->ses);
	triton_event_fire(EV_CTRL_STARTED, &ses->ses);

	ap_session_starting(&ses->ses);
	
	r = pwdb_check(&ses->ses, ses->ses.username, 0);
	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);
		}
	}

	if (r == PWDB_DENIED) {
		if (conf_ppp_verbose)
			log_ppp_warn("authentication failed\n");
		ap_session_terminate(&ses->ses, TERM_AUTH_ERROR, 0);
		return;
	}

221 222 223 224 225 226
	ses->ses.ipv4 = ipdb_get_ipv4(&ses->ses);
	if (!ses->ses.ipv4) {
		log_ppp_warn("no free IPv4 address\n");
		ap_session_terminate(&ses->ses, TERM_AUTH_ERROR, 0);
		return;
	}
K
Kozlov Dmitry 已提交
227

228 229 230 231 232 233 234
	if (conf_gw_address)
		ses->ses.ipv4->addr = conf_gw_address;
	
	if (conf_netmask)
		ses->ses.ipv4->mask = conf_netmask;
	else if (!ses->ses.ipv4->mask)
		ses->ses.ipv4->mask = 24;
K
Kozlov Dmitry 已提交
235

236
	if (ses->dhcpv4_request) {
K
Kozlov Dmitry 已提交
237 238 239 240
		dhcpv4_send_reply(DHCPOFFER, ses->serv->dhcpv4, ses->dhcpv4_request, &ses->ses, conf_lease_time);

		dhcpv4_packet_free(ses->dhcpv4_request);
		ses->dhcpv4_request = NULL;
241 242 243 244
	
		ses->timer.expire = ipoe_session_timeout;
		ses->timer.expire_tv.tv_sec = conf_offer_timeout;
		triton_timer_add(&ses->ctx, &ses->timer, 0);
K
Kozlov Dmitry 已提交
245 246 247 248 249
	} else {
		if (ipoe_nl_modify(ses->ifindex, ses->giaddr, ses->ses.ipv4->peer_addr, NULL, NULL))
			ap_session_terminate(&ses->ses, TERM_NAS_ERROR, 0);
		else
			ap_session_activate(&ses->ses);
K
Kozlov Dmitry 已提交
250 251 252 253 254 255 256
	}
}

static void ipoe_session_activate(struct ipoe_session *ses)
{
	ap_session_activate(&ses->ses);

257 258 259 260 261
	if (ses->dhcpv4_request) {
		if (ses->ses.state == AP_STATE_ACTIVE)
			dhcpv4_send_reply(DHCPACK, ses->serv->dhcpv4, ses->dhcpv4_request, &ses->ses, conf_lease_time);
		else
			dhcpv4_send_nak(ses->serv->dhcpv4, ses->dhcpv4_request);
K
Kozlov Dmitry 已提交
262

263 264 265
		dhcpv4_packet_free(ses->dhcpv4_request);
		ses->dhcpv4_request = NULL;
	}
K
Kozlov Dmitry 已提交
266 267
}

K
Kozlov Dmitry 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
static void ipoe_session_keepalive(struct ipoe_session *ses)
{
	if (ses->timer.tpd)
		triton_timer_mod(&ses->timer, 0);

	ses->xid = ses->dhcpv4_request->hdr->xid;

	if (ses->ses.state == AP_STATE_ACTIVE)
		dhcpv4_send_reply(DHCPACK, ses->serv->dhcpv4, ses->dhcpv4_request, &ses->ses, conf_lease_time);
	else
		dhcpv4_send_nak(ses->serv->dhcpv4, ses->dhcpv4_request);

	dhcpv4_packet_free(ses->dhcpv4_request);
	ses->dhcpv4_request = NULL;
}

K
Kozlov Dmitry 已提交
284 285 286 287 288 289 290
static void ipoe_session_started(struct ap_session *s)
{
	struct ipoe_session *ses = container_of(s, typeof(*ses), ses);
	
	log_ppp_debug("ipoe: session started\n");

	ses->timer.expire = ipoe_session_timeout;
K
Kozlov Dmitry 已提交
291 292 293
	ses->timer.expire_tv.tv_sec = conf_lease_timeout;
	if (ses->timer.tpd)
		triton_timer_mod(&ses->timer, 0);
K
Kozlov Dmitry 已提交
294 295 296 297 298 299 300
}

static void ipoe_session_free(struct ipoe_session *ses)
{
	if (ses->timer.tpd)
		triton_timer_del(&ses->timer);

K
Kozlov Dmitry 已提交
301 302 303
	if (ses->dhcpv4_request)
		dhcpv4_packet_free(ses->dhcpv4_request);
	
304 305 306 307 308 309
	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 已提交
310 311 312 313
	triton_context_unregister(&ses->ctx);
	
	if (ses->data)
		_free(ses->data);
314 315 316
	
	if (ses->ifindex != -1)
		ipoe_nl_delete(ses->ifindex);
K
Kozlov Dmitry 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

	mempool_free(ses);
}

static void ipoe_session_finished(struct ap_session *s)
{
	struct ipoe_session *ses = container_of(s, typeof(*ses), ses);

	log_ppp_debug("ipoe: session finished\n");

	pthread_mutex_lock(&ses->serv->lock);
	list_del(&ses->entry);
	pthread_mutex_unlock(&ses->serv->lock);

	triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_free, ses);
}

static void ipoe_session_terminate(struct ap_session *s, int hard)
{
	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);
}

350
static struct ipoe_session *ipoe_session_create_dhcpv4(struct ipoe_serv *serv, struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
{
	struct ipoe_session *ses;
	int dlen = 0;
	uint8_t *ptr;

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

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

	ap_session_init(&ses->ses);

	ses->serv = serv;
367
	ses->ifindex = -1;
K
Kozlov Dmitry 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
	ses->dhcpv4_request = pack;
	
	ses->xid = pack->hdr->xid;
	memcpy(ses->hwaddr, pack->hdr->chaddr, 6);
	ses->giaddr = pack->hdr->giaddr;

	if (pack->agent_circuit_id)
		dlen += sizeof(struct dhcp_opt) + pack->agent_circuit_id->len;
	
	if (pack->agent_remote_id)
		dlen += sizeof(struct dhcp_opt) + pack->agent_remote_id->len;
	
	if (pack->client_id)
		dlen += sizeof(struct dhcp_opt) + pack->client_id->len;
	
	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->agent_circuit_id) {
		ses->agent_circuit_id = (struct dhcp_opt *)ptr;
		ses->agent_circuit_id->len = pack->agent_circuit_id->len;
		memcpy(ses->agent_circuit_id->data, pack->agent_circuit_id->data, pack->agent_circuit_id->len);
		ptr += sizeof(struct dhcp_opt) + pack->agent_circuit_id->len;
	}

	if (pack->agent_remote_id) {
		ses->agent_remote_id = (struct dhcp_opt *)ptr;
		ses->agent_remote_id->len = pack->agent_remote_id->len;
		memcpy(ses->agent_remote_id->data, pack->agent_remote_id->data, pack->agent_remote_id->len);
		ptr += sizeof(struct dhcp_opt) + pack->agent_remote_id->len;
	}
	
	if (pack->client_id) {
		ses->client_id = (struct dhcp_opt *)ptr;
		ses->client_id->len = pack->client_id->len;
		memcpy(ses->client_id->data, pack->client_id->data, pack->client_id->len);
		ptr += sizeof(struct dhcp_opt) + pack->client_id->len;
	}

	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->ctrl.calling_station_id = _malloc(19);
424
	ses->ctrl.called_station_id = _strdup(serv->ifname);
K
Kozlov Dmitry 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	
	ptr = ses->hwaddr;
	sprintf(ses->ctrl.calling_station_id, "%02x:%02x:%02x:%02x:%02x:%02x",
		ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
	
	ses->ses.ctrl = &ses->ctrl;
	ses->ses.chan_name = ses->ctrl.calling_station_id;

	triton_context_register(&ses->ctx, &ses->ses);

	triton_context_wakeup(&ses->ctx);

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

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

	return ses;
}

446
static void ipoe_recv_dhcpv4(struct dhcpv4_serv *dhcpv4, struct dhcpv4_packet *pack)
K
Kozlov Dmitry 已提交
447 448 449 450 451 452 453 454 455
{
	struct ipoe_serv *serv = container_of(dhcpv4->ctx, typeof(*serv), ctx);
	struct ipoe_session *ses;
	//struct dhcpv4_packet *reply;

	pthread_mutex_lock(&serv->lock);
	if (pack->msg_type == DHCPDISCOVER) {
		ses = ipoe_session_lookup(serv, pack);
		if (!ses) {
456
			ses = ipoe_session_create_dhcpv4(serv, pack);
K
Kozlov Dmitry 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470

			if (conf_verbose &&  ses) {
				log_switch(dhcpv4->ctx, &ses->ses);
				log_ppp_info2("recv ");
				dhcpv4_print_packet(pack, log_ppp_info2);
			}
		}	else {
			log_switch(dhcpv4->ctx, &ses->ses);

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

K
Kozlov Dmitry 已提交
471
			if (ses->ses.ipv4 && ses->ses.state == AP_STATE_ACTIVE && pack->request_ip == ses->ses.ipv4->peer_addr)
K
Kozlov Dmitry 已提交
472 473 474 475 476
				dhcpv4_send_reply(DHCPOFFER, dhcpv4, pack, &ses->ses, conf_lease_time);

			dhcpv4_packet_free(pack);
		}
	} else if (pack->msg_type == DHCPREQUEST) {
K
Kozlov Dmitry 已提交
477
		ses = ipoe_session_lookup(serv, pack);
K
Kozlov Dmitry 已提交
478 479 480 481 482 483 484 485 486

		if (!ses) {
			if (conf_verbose) {
				log_info2("recv ");
				dhcpv4_print_packet(pack, log_info2);
			}

			dhcpv4_send_nak(dhcpv4, pack);
		} else {
K
Kozlov Dmitry 已提交
487 488 489 490
			if (!ses->ses.ipv4 || 
				(pack->server_id && (pack->server_id != ses->ses.ipv4->addr || pack->request_ip != ses->ses.ipv4->peer_addr)) ||
				(pack->hdr->ciaddr && (pack->hdr->xid != ses->xid || pack->hdr->ciaddr != ses->ses.ipv4->peer_addr))) {

K
Kozlov Dmitry 已提交
491 492 493 494 495
				if (conf_verbose) {
					log_info2("recv ");
					dhcpv4_print_packet(pack, log_info2);
				}

K
Kozlov Dmitry 已提交
496
				if (ses->ses.ipv4 && pack->server_id == ses->ses.ipv4->addr && pack->request_ip && pack->request_ip != ses->ses.ipv4->peer_addr)
K
Kozlov Dmitry 已提交
497
					dhcpv4_send_nak(dhcpv4, pack);
K
Kozlov Dmitry 已提交
498

K
Kozlov Dmitry 已提交
499 500 501 502 503 504 505 506
				ap_session_terminate(&ses->ses, TERM_USER_REQUEST, 0);
			} else {
				if (conf_verbose) {
					log_switch(dhcpv4->ctx, &ses->ses);
					log_ppp_info2("recv ");
					dhcpv4_print_packet(pack, log_ppp_info2);
				}

K
Kozlov Dmitry 已提交
507
				if (serv->opt_shared == 0)
K
Kozlov Dmitry 已提交
508 509
					ipoe_drop_sessions(serv, ses);

K
Kozlov Dmitry 已提交
510 511 512 513
				if (ses->ses.state == AP_STATE_STARTING && !ses->dhcpv4_request) {
					ses->dhcpv4_request = pack;
					pack = NULL;
					triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_activate, ses);
K
Kozlov Dmitry 已提交
514 515 516 517
				} else if (ses->ses.state == AP_STATE_ACTIVE && !ses->dhcpv4_request) {
					ses->dhcpv4_request = pack;
					pack = NULL;
					triton_context_call(&ses->ctx, (triton_event_func)ipoe_session_keepalive, ses);
K
Kozlov Dmitry 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
				}
			}
		}
		if (pack)
			dhcpv4_packet_free(pack);
	} else if (pack->msg_type == DHCPDECLINE || pack->msg_type == DHCPRELEASE) {
		ses = ipoe_session_lookup(serv, pack);
		if (ses) {
			if (conf_verbose) {
				log_switch(dhcpv4->ctx, &ses->ses);
				log_ppp_info2("recv ");
				dhcpv4_print_packet(pack, log_ppp_info2);
			}

			ap_session_terminate(&ses->ses, TERM_USER_REQUEST, 0);
		}
		dhcpv4_packet_free(pack);
	}
	pthread_mutex_unlock(&serv->lock);
}

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
static struct ipoe_session *ipoe_session_create_up(struct ipoe_serv *serv, struct ethhdr *eth, struct iphdr *iph)
{
	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->serv = serv;
	ses->ifindex = -1;
	
	memcpy(ses->hwaddr, eth->h_source, 6);

	ses->ctx.before_switch = log_switch;
	ses->ctx.close = ipoe_session_close;
	ses->ctrl.ctx = &ses->ctx;
	ses->ctrl.started = ipoe_session_started;
	ses->ctrl.finished = ipoe_session_finished;
	ses->ctrl.terminate = ipoe_session_terminate;
	ses->ctrl.type = CTRL_TYPE_IPOE;
	ses->ctrl.name = "ipoe";
K
Kozlov Dmitry 已提交
566 567 568

	ses->giaddr = iph->saddr;

569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
	ses->ctrl.calling_station_id = _malloc(17);
	ses->ctrl.called_station_id = _malloc(17);

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

	triton_context_register(&ses->ctx, &ses->ses);

	triton_context_wakeup(&ses->ctx);

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

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

	return ses;
}

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 已提交
601 602 603

		if (!serv->opt_up)
			return;
604 605 606
		
		pthread_mutex_lock(&serv->lock);
		list_for_each_entry(ses, &serv->sessions, entry) {
K
Kozlov Dmitry 已提交
607
			if (ses->giaddr == iph->saddr) {
608 609 610 611 612 613 614 615 616 617
				pthread_mutex_unlock(&serv->lock);
				return;
			}
		}
		pthread_mutex_unlock(&serv->lock);
		
		ipoe_session_create_up(serv, eth, iph);
	}
}

K
Kozlov Dmitry 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
static void ipoe_serv_close(struct triton_context_t *ctx)
{
	struct ipoe_serv *serv = container_of(ctx, typeof(*serv), ctx);

	if (serv->dhcpv4)
		dhcpv4_free(serv->dhcpv4);

	triton_context_unregister(ctx);

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

static int show_stat_exec(const char *cmd, char * const *fields, int fields_cnt, void *client)
{
	cli_send(client, "ipoe:\r\n");
	cli_sendv(client,"  starting: %u\r\n", stat_starting);
	cli_sendv(client,"  active: %u\r\n", stat_active);

	return CLI_CMD_OK;
}

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

K
Kozlov Dmitry 已提交
646 647 648 649 650 651
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 已提交
652
{
K
Kozlov Dmitry 已提交
653 654 655 656 657 658 659 660
	struct ipoe_session *ses;

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

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

K
Kozlov Dmitry 已提交
662 663
		triton_context_call(&ses->ctx, (triton_event_func)__terminate, &ses->ses);
	}
K
Kozlov Dmitry 已提交
664 665 666 667
}

static void add_interface(const char *ifname, int ifindex, const char *opt)
{
K
Kozlov Dmitry 已提交
668 669
	char *str0, *str, *ptr1, *ptr2;
	int end;
K
Kozlov Dmitry 已提交
670
	struct ipoe_serv *serv;
K
Kozlov Dmitry 已提交
671 672 673 674 675 676 677 678 679 680 681 682
	int opt_shared = conf_shared;
	int opt_dhcpv4 = 0;
	int opt_up = 0;
	int opt_mode = conf_mode;

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

K
Kozlov Dmitry 已提交
684 685 686 687 688 689 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 723 724
			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;
			} else
				goto parse_err;

			if (end)
				break;

			str = ptr2 + 1;
		}
		
		_free(str0);
725
	}
K
Kozlov Dmitry 已提交
726 727 728 729 730 731

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

K
Kozlov Dmitry 已提交
732
	list_for_each_entry(serv, &serv_list, entry) {
K
Kozlov Dmitry 已提交
733 734 735 736 737 738 739 740 741
		if (!strcmp(ifname, serv->ifname))
			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 已提交
742
		}
K
Kozlov Dmitry 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756

		if (opt_dhcpv4 && !serv->dhcpv4) {
			serv->dhcpv4 = dhcpv4_create(&serv->ctx, serv->ifname);
			if (serv->dhcpv4)
				serv->dhcpv4->recv = ipoe_recv_dhcpv4;
		} else if (!opt_dhcpv4 && serv->dhcpv4) {
			dhcpv4_free(serv->dhcpv4);
			serv->dhcpv4 = NULL;
		}

		serv->opt_up = opt_up;
		serv->opt_mode = conf_mode;

		return;
K
Kozlov Dmitry 已提交
757 758 759 760 761 762
	}

	serv = _malloc(sizeof(*serv));
	memset(serv, 0, sizeof(*serv));
	serv->ifname = _strdup(ifname);
	serv->ifindex = ifindex;
K
Kozlov Dmitry 已提交
763 764 765 766
	serv->opt_shared = opt_shared;
	serv->opt_dhcpv4 = opt_dhcpv4;
	serv->opt_up = opt_up;
	serv->opt_mode = opt_mode;
767
	serv->active = 1;
K
Kozlov Dmitry 已提交
768 769 770 771 772 773 774 775
	INIT_LIST_HEAD(&serv->sessions);
	pthread_mutex_init(&serv->lock, NULL);

	triton_context_register(&serv->ctx, NULL);

	if (serv->opt_dhcpv4) {
		serv->dhcpv4 = dhcpv4_create(&serv->ctx, serv->ifname);
		if (serv->dhcpv4)
776
			serv->dhcpv4->recv = ipoe_recv_dhcpv4;
K
Kozlov Dmitry 已提交
777 778 779 780
	}

	triton_context_wakeup(&serv->ctx);

781 782
	list_add_tail(&serv->entry, &serv_list);

K
Kozlov Dmitry 已提交
783 784
	return;

K
Kozlov Dmitry 已提交
785
parse_err:
K
Kozlov Dmitry 已提交
786
	log_error("ipoe: failed to parse '%s'\n", opt);
K
Kozlov Dmitry 已提交
787
	_free(str0);
K
Kozlov Dmitry 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
}

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

	add_interface(ifr.ifr_name, ifr.ifr_ifindex, opt);
}

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

	return 0;
}

static void load_interface_re(const char *opt)
{
	pcre *re = NULL;
	const char *pcre_err;
	char *pattern;
	const char *ptr;
	int pcre_offset;
	struct iplink_arg arg;

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

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

	iplink_list((iplink_list_func)__load_interface_re, &arg);

	pcre_free(re);
	_free(pattern);
}

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

	list_for_each_entry(serv, &serv_list, entry)
		serv->active = 0;

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

		if (strlen(opt->val) > 3 && memcmp(opt->val, "re:", 3) == 0)
			load_interface_re(opt->val);
		else
			load_interface(opt->val);
	}
	
	list_for_each_safe(pos, n, &serv_list) {
		serv = list_entry(pos, typeof(*serv), entry);
		if (!serv->active) {
			list_del(&serv->entry);
			triton_context_call(&serv->ctx, (triton_event_func)ipoe_serv_close, &serv->ctx);
		}
	}
}

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
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;
	}

	mask = (1 << mask) - 1;

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

K
Kozlov Dmitry 已提交
932 933 934 935
static void load_config(void)
{
	const char *opt;
	struct conf_sect_t *s = conf_get_section("ipoe");
K
Kozlov Dmitry 已提交
936
	struct conf_option_t *opt1;
K
Kozlov Dmitry 已提交
937 938 939 940 941 942 943 944 945 946 947 948

	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 已提交
949
		}
K
Kozlov Dmitry 已提交
950
#endif
K
Kozlov Dmitry 已提交
951
		else
K
Kozlov Dmitry 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
			log_emerg("ipoe: unknown username value '%s'\n", opt);
	}

	opt = conf_get_opt("ipoe", "gw-ip-address");
	if (opt)
		conf_gw_address = inet_addr(opt);
	else
		conf_gw_address = 0;

	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 已提交
974 975 976 977 978 979 980 981

	opt = conf_get_opt("ipoe", "lease-time");
	if (opt)
		conf_lease_time = atoi(opt);
	
	opt = conf_get_opt("ipoe", "lease-timeout");
	if (opt)
		conf_lease_timeout = atoi(opt);
K
Kozlov Dmitry 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
	
	opt = conf_get_opt("ipoe", "shared");
	if (opt)
		conf_shared = atoi(opt);
	else
		conf_shared = 1;
	
	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;

	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;
	
	load_interfaces(s);
	load_local_nets(s);
K
Kozlov Dmitry 已提交
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
}

static void ipoe_init(void)
{
	ses_pool = mempool_create(sizeof(struct ipoe_session));

	load_config();

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

DEFINE_INIT(20, ipoe_init);