summaryrefslogtreecommitdiff
path: root/accel-pppd/ppp/ppp.c
blob: 2836f7c184e0d8859b7fc1058fc7a666875f8278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
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
566
567
568
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <features.h>
#include <signal.h>
#include "linux_ppp.h"

#include <openssl/md5.h>

#include "triton.h"

#include "events.h"
#include "ppp.h"
#include "ppp_fsm.h"
#include "log.h"
#include "spinlock.h"
#include "mempool.h"

#include "memdebug.h"

int __export conf_ppp_verbose;
int conf_sid_ucase;
int conf_single_session = -1;

pthread_rwlock_t __export ppp_lock = PTHREAD_RWLOCK_INITIALIZER;
__export LIST_HEAD(ppp_list);

int __export sock_fd;
int __export sock6_fd;

int __export ppp_shutdown;

static mempool_t buf_pool;

static LIST_HEAD(layers);

static unsigned long long seq;
#if __WORDSIZE == 32
static spinlock_t seq_lock;
#endif

__export struct ppp_stat_t ppp_stat;

struct layer_node_t
{
	struct list_head entry;
	int order;
	struct list_head items;
};

static int ppp_chan_read(struct triton_md_handler_t*);
static int ppp_unit_read(struct triton_md_handler_t*);
static void init_layers(struct ppp_t *);
static void _free_layers(struct ppp_t *);
static void start_first_layer(struct ppp_t *);

void __export ppp_init(struct ppp_t *ppp)
{
	memset(ppp,0,sizeof(*ppp));
	INIT_LIST_HEAD(&ppp->layers);
	INIT_LIST_HEAD(&ppp->chan_handlers);
	INIT_LIST_HEAD(&ppp->unit_handlers);
	INIT_LIST_HEAD(&ppp->pd_list);
}

static void generate_sessionid(struct ppp_t *ppp)
{
	unsigned long long sid;

#if __WORDSIZE == 32
	spin_lock(&seq_lock);
	sid = ++seq;
	spin_unlock(&seq_lock);
#else
	sid = __sync_add_and_fetch(&seq, 1);
#endif

	if (conf_sid_ucase)
		sprintf(ppp->sessionid, "%016llX", sid);
	else
		sprintf(ppp->sessionid, "%016llx", sid);
}

int __export establish_ppp(struct ppp_t *ppp)
{
	struct ifreq ifr;

	/* Open an instance of /dev/ppp and connect the channel to it */
	if (ioctl(ppp->fd, PPPIOCGCHAN, &ppp->chan_idx) == -1) {
		log_ppp_error("ioctl(PPPIOCGCHAN): %s\n", strerror(errno));
		return -1;
	}

	ppp->chan_fd = open("/dev/ppp", O_RDWR);
	if (ppp->chan_fd < 0) {
		log_ppp_error("open(chan) /dev/ppp: %s\n", strerror(errno));
		return -1;
	}

	if (ioctl(ppp->chan_fd, PPPIOCATTCHAN, &ppp->chan_idx) < 0) {
		log_ppp_error("ioctl(PPPIOCATTCHAN): %s\n", strerror(errno));
		goto exit_close_chan;
	}

	ppp->unit_fd = open("/dev/ppp", O_RDWR);
	if (ppp->unit_fd < 0) {
		log_ppp_error("open(unit) /dev/ppp: %s\n", strerror(errno));
		goto exit_close_chan;
	}

	ppp->unit_idx = -1;
	if (ioctl(ppp->unit_fd, PPPIOCNEWUNIT, &ppp->unit_idx) < 0) {
		log_ppp_error("ioctl(PPPIOCNEWUNIT): %s\n", strerror(errno));
		goto exit_close_unit;
	}

  if (ioctl(ppp->chan_fd, PPPIOCCONNECT, &ppp->unit_idx) < 0) {
		log_ppp_error("ioctl(PPPIOCCONNECT): %s\n", strerror(errno));
		goto exit_close_unit;
	}

	if (fcntl(ppp->chan_fd, F_SETFL, O_NONBLOCK)) {
		log_ppp_error("ppp: cann't to set nonblocking mode: %s\n", strerror(errno));
		goto exit_close_unit;
	}
	
	if (fcntl(ppp->unit_fd, F_SETFL, O_NONBLOCK)) {
		log_ppp_error("ppp: cann't to set nonblocking mode: %s\n", strerror(errno));
		goto exit_close_unit;
	}

	ppp->start_time = time(NULL);
	generate_sessionid(ppp);
	sprintf(ppp->ifname, "ppp%i", ppp->unit_idx);

	memset(&ifr, 0, sizeof(ifr));
	strcpy(ifr.ifr_name, ppp->ifname);

	if (ioctl(sock_fd, SIOCGIFINDEX, &ifr)) {
		log_ppp_error("ppp: ioctl(SIOCGIFINDEX): %s\n", strerror(errno));
		goto exit_close_unit;
	}
	ppp->ifindex = ifr.ifr_ifindex;

	log_ppp_info1("connect: %s <--> %s(%s)\n", ppp->ifname, ppp->ctrl->name, ppp->chan_name);

	init_layers(ppp);

	if (list_empty(&ppp->layers)) {
		log_ppp_error("no layers to start\n");
		goto exit_close_unit;
	}

	ppp->buf = mempool_alloc(buf_pool);

	ppp->chan_hnd.fd = ppp->chan_fd;
	ppp->chan_hnd.read = ppp_chan_read;
	ppp->unit_hnd.fd = ppp->unit_fd;
	ppp->unit_hnd.read = ppp_unit_read;
	triton_md_register_handler(ppp->ctrl->ctx, &ppp->chan_hnd);
	triton_md_register_handler(ppp->ctrl->ctx, &ppp->unit_hnd);
	
	triton_md_enable_handler(&ppp->chan_hnd, MD_MODE_READ);
	triton_md_enable_handler(&ppp->unit_hnd, MD_MODE_READ);

	ppp->state = PPP_STATE_STARTING;
	__sync_add_and_fetch(&ppp_stat.starting, 1);

	pthread_rwlock_wrlock(&ppp_lock);
	list_add_tail(&ppp->entry, &ppp_list);
	pthread_rwlock_unlock(&ppp_lock);

	log_ppp_debug("ppp established\n");

	triton_event_fire(EV_PPP_STARTING, ppp);
	
	start_first_layer(ppp);

	return 0;

exit_close_unit:
	close(ppp->unit_fd);
exit_close_chan:
	close(ppp->chan_fd);

	if (ppp->buf)
		mempool_free(ppp->buf);

	return -1;
}

static void destablish_ppp(struct ppp_t *ppp)
{
	pthread_rwlock_wrlock(&ppp_lock);
	list_del(&ppp->entry);
	pthread_rwlock_unlock(&ppp_lock);

	switch (ppp->state) {
		case PPP_STATE_ACTIVE:
			__sync_sub_and_fetch(&ppp_stat.active, 1);
			break;
		case PPP_STATE_STARTING:
			__sync_sub_and_fetch(&ppp_stat.starting, 1);
			break;
		case PPP_STATE_FINISHING:
			__sync_sub_and_fetch(&ppp_stat.finishing, 1);
			break;
	}

	triton_md_unregister_handler(&ppp->chan_hnd);
	triton_md_unregister_handler(&ppp->unit_hnd);
	
	close(ppp->unit_fd);
	close(ppp->chan_fd);
	close(ppp->fd);

	ppp->unit_fd = -1;
	ppp->chan_fd = -1;
	ppp->fd = -1;

	_free_layers(ppp);
	
	ppp->terminated = 1;
	
	log_ppp_debug("ppp destablished\n");

	triton_event_fire(EV_PPP_FINISHED, ppp);
	ppp->ctrl->finished(ppp);

	mempool_free(ppp->buf);

	if (ppp->username) {
		_free(ppp->username);
		ppp->username = NULL;
	}
	
	if (ppp_shutdown && !ppp_stat.starting && !ppp_stat.active && !ppp_stat.finishing)
		kill(getpid(), SIGTERM);
}

/*void print_buf(uint8_t *buf, int size)
{
	int i;
	for(i=0;i<size;i++)
		printf("%x ",buf[i]);
	printf("\n");
}*/

int __export ppp_chan_send(struct ppp_t *ppp, void *data, int size)
{
	int n;

	//printf("ppp_chan_send: ");
	//print_buf((uint8_t*)data,size);
	
	n = write(ppp->chan_fd,data,size);
	if (n < size)
		log_ppp_error("ppp_chan_send: short write %i, excpected %i\n", n, size);
	return n;
}

int __export ppp_unit_send(struct ppp_t *ppp, void *data, int size)
{
	int n;

	//printf("ppp_unit_send: ");
	//print_buf((uint8_t*)data,size);
	
	n=write(ppp->unit_fd, data, size);
	if (n < size)
		log_ppp_error("ppp_unit_send: short write %i, excpected %i\n",n,size);
	return n;
}

static int ppp_chan_read(struct triton_md_handler_t *h)
{
	struct ppp_t *ppp = container_of(h, typeof(*ppp), chan_hnd);
	struct ppp_handler_t *ppp_h;
	uint16_t proto;

	while(1) {
cont:
		ppp->buf_size = read(h->fd, ppp->buf, PPP_MRU);
		if (ppp->buf_size < 0) {
			if (errno == EAGAIN)
				return 0;
			log_ppp_error("ppp_chan_read: %s\n", strerror(errno));
			return 0;
		}

		//printf("ppp_chan_read: ");
		//print_buf(ppp->buf,ppp->buf_size);
		if (ppp->buf_size == 0) {
			ppp_terminate(ppp, TERM_NAS_ERROR, 1);
			return 1;
		}

		if (ppp->buf_size < 2) {
			log_ppp_error("ppp_chan_read: short read %i\n", ppp->buf_size);
			continue;
		}

		proto = ntohs(*(uint16_t*)ppp->buf);
		list_for_each_entry(ppp_h, &ppp->chan_handlers, entry) {
			if (ppp_h->proto == proto) {
				ppp_h->recv(ppp_h);
				if (ppp->chan_fd == -1) {
					//ppp->ctrl->finished(ppp);
					return 1;
				}
				goto cont;
			}
		}

		lcp_send_proto_rej(ppp, proto);
		//log_ppp_warn("ppp_chan_read: discarding unknown packet %x\n", proto);
	}
}

static int ppp_unit_read(struct triton_md_handler_t *h)
{
	struct ppp_t *ppp = container_of(h, typeof(*ppp), unit_hnd);
	struct ppp_handler_t *ppp_h;
	uint16_t proto;

	while (1) {
cont:
		ppp->buf_size = read(h->fd, ppp->buf, PPP_MRU);
		if (ppp->buf_size < 0) {
			if (errno == EAGAIN)
				return 0;
			log_ppp_error("ppp_unit_read: %s\n",strerror(errno));
			return 0;
		}

		//printf("ppp_unit_read: ");
		//print_buf(ppp->buf,ppp->buf_size);

		if (ppp->buf_size == 0) {
			ppp_terminate(ppp, TERM_NAS_ERROR, 1);
			return 1;
		}

		if (ppp->buf_size < 2) {
			log_ppp_error("ppp_unit_read: short read %i\n", ppp->buf_size);
			continue;
		}

		proto=ntohs(*(uint16_t*)ppp->buf);
		list_for_each_entry(ppp_h, &ppp->unit_handlers, entry) {
			if (ppp_h->proto == proto) {
				ppp_h->recv(ppp_h);
				if (ppp->unit_fd == -1) {
					//ppp->ctrl->finished(ppp);
					return 1;
				}
				goto cont;
			}
		}
		lcp_send_proto_rej(ppp, proto);
		//log_ppp_warn("ppp_unit_read: discarding unknown packet %x\n", proto);
	}
}

void ppp_recv_proto_rej(struct ppp_t *ppp, uint16_t proto)
{
	struct ppp_handler_t *ppp_h;

	list_for_each_entry(ppp_h, &ppp->chan_handlers, entry) {
		if (ppp_h->proto == proto) {
			if (ppp_h->recv_proto_rej)
				ppp_h->recv_proto_rej(ppp_h);
			return;
		}
	}
	
	list_for_each_entry(ppp_h, &ppp->unit_handlers, entry) {
		if (ppp_h->proto == proto) {
			if (ppp_h->recv_proto_rej)
				ppp_h->recv_proto_rej(ppp_h);
			return;
		}
	}
}

static void ppp_ifup(struct ppp_t *ppp)
{
	struct ifreq ifr;
	struct npioctl np;
	
	triton_event_fire(EV_PPP_ACCT_START, ppp);
	if (ppp->stop_time)
		return;

	triton_event_fire(EV_PPP_PRE_UP, ppp);
	if (ppp->stop_time)
		return;

	memset(&ifr, 0, sizeof(ifr));
	strcpy(ifr.ifr_name, ppp->ifname);

	if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr))
		log_ppp_error("ppp: failed to get interface flags: %s\n", strerror(errno));

	ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;

	if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr))
		log_ppp_error("ppp: failed to set interface flags: %s\n", strerror(errno));

	if (ppp->ipv4) {
		np.protocol = PPP_IP;
		np.mode = NPMODE_PASS;

		if (ioctl(ppp->unit_fd, PPPIOCSNPMODE, &np))
			log_ppp_error("ppp: failed to set NP (IPv4) mode: %s\n", strerror(errno));
	}
	
	if (ppp->ipv6) {
		np.protocol = PPP_IPV6;
		np.mode = NPMODE_PASS;

		if (ioctl(ppp->unit_fd, PPPIOCSNPMODE, &np))
			log_ppp_error("ppp: failed to set NP (IPv6) mode: %s\n", strerror(errno));
	}
	
	ppp->ctrl->started(ppp);

	triton_event_fire(EV_PPP_STARTED, ppp);
}

static void __ppp_layer_started(struct ppp_t *ppp, struct ppp_layer_data_t *d)
{
	struct layer_node_t *n = d->node;
	int f = 0;

	list_for_each_entry(d, &n->items, entry) {
		if (!d->started && !d->passive) return;
		if (d->started && !d->optional)
			f = 1;
	}

	if (!f)
		return;
	

	if (n->entry.next == &ppp->layers) {
		ppp->state = PPP_STATE_ACTIVE;
		__sync_sub_and_fetch(&ppp_stat.starting, 1);
		__sync_add_and_fetch(&ppp_stat.active, 1);
		ppp_ifup(ppp);
	} else {
		n = list_entry(n->entry.next, typeof(*n), entry);
		list_for_each_entry(d, &n->items, entry) {
			d->starting = 1;
			if (d->layer->start(d)) {
				ppp_terminate(ppp, TERM_NAS_ERROR, 0);
				return;
			}
		}
	}
}

void __export ppp_layer_started(struct ppp_t *ppp, struct ppp_layer_data_t *d)
{
	if (d->started)
		return;

	d->started = 1;

	__ppp_layer_started(ppp, d);
}

void __export ppp_layer_passive(struct ppp_t *ppp, struct ppp_layer_data_t *d)
{
	if (d->started)
		return;

	d->passive = 1;
	
	__ppp_layer_started(ppp, d);
}

void __export ppp_layer_finished(struct ppp_t *ppp, struct ppp_layer_data_t *d)
{
	struct layer_node_t *n = d->node;

	d->finished = 1;
	d->starting = 0;

	list_for_each_entry(n, &ppp->layers, entry) {
		list_for_each_entry(d, &n->items, entry) {
			if (d->starting && !d->finished)
				return;
		}
	}

	destablish_ppp(ppp);
}

void __export ppp_terminate(struct ppp_t *ppp, int cause, int hard)
{
	struct layer_node_t *n;
	struct ppp_layer_data_t *d;
	int s = 0;

	if (ppp->terminated)
		return;

	if (!ppp->stop_time)
		time(&ppp->stop_time);

	if (!ppp->terminate_cause)
		ppp->terminate_cause = cause;

	if (ppp->terminating) {
		if (hard)
			destablish_ppp(ppp);
		return;
	}
	
	ppp->terminating = 1;
	if (ppp->state == PPP_STATE_ACTIVE)
		__sync_sub_and_fetch(&ppp_stat.active, 1);
	else
		__sync_sub_and_fetch(&ppp_stat.starting, 1);
	__sync_add_and_fetch(&ppp_stat.finishing, 1);
	ppp->state = PPP_STATE_FINISHING;

	log_ppp_debug("ppp_terminate\n");

	triton_event_fire(EV_PPP_FINISHING, ppp);

	if (hard) {
		destablish_ppp(ppp);
		return;
	}
	
	list_for_each_entry(n,&ppp->layers,entry) {
		list_for_each_entry(d,&n->items,entry) {
			if (d->starting) {
				s = 1;
				d->layer->finish(d);
			}
		}
	}
	if (s)
		return;
	destablish_ppp(ppp);
}

void __export ppp_register_chan_handler(struct ppp_t *ppp,struct ppp_handler_t *h)
{
	list_add_tail(&h->entry,&ppp->chan_handlers);
}
void __export ppp_register_unit_handler(struct ppp_t *ppp,struct ppp_handler_t *h)
{
	list_add_tail(&h->entry,&ppp->unit_handlers);
}
void __export ppp_unregister_handler(struct ppp_t *ppp,struct ppp_handler_t *h)
{
	list_del(&h->entry);
}

static int get_layer_order(const char *name)
{
	if (!strcmp(name,"lcp")) return 0;
	if (!strcmp(name,"auth")) return 1;
	if (!strcmp(name,"ccp")) return 2;
	if (!strcmp(name,"ipcp")) return 2;
	if (!strcmp(name,"ipv6cp")) return 2;
	return -1;
}

int __export ppp_register_layer(const char *name, struct ppp_layer_t *layer)
{
	int order;
	struct layer_node_t *n,*n1;

	order = get_layer_order(name);

	if (order < 0)
		return order;

	list_for_each_entry(n, &layers, entry) {
		if (order > n->order)
			continue;
		if (order < n->order) {
			n1 = _malloc(sizeof(*n1));
			memset(n1, 0, sizeof(*n1));
			n1->order = order;
			INIT_LIST_HEAD(&n1->items);
			list_add_tail(&n1->entry, &n->entry);
			n = n1;
		}
		goto insert;
	}
	n1 = _malloc(sizeof(*n1));
	memset(n1, 0, sizeof(*n1));
	n1->order = order;
	INIT_LIST_HEAD(&n1->items);
	list_add_tail(&n1->entry, &layers);
	n = n1;
insert:
	list_add_tail(&layer->entry, &n->items);

	return 0;
}
void __export ppp_unregister_layer(struct ppp_layer_t *layer)
{
	list_del(&layer->entry);
}

static void init_layers(struct ppp_t *ppp)
{
	struct layer_node_t *n, *n1;
	struct ppp_layer_t *l;
	struct ppp_layer_data_t *d;

	list_for_each_entry(n,&layers,entry) {
		n1 = _malloc(sizeof(*n1));
		memset(n1, 0, sizeof(*n1));
		INIT_LIST_HEAD(&n1->items);
		list_add_tail(&n1->entry, &ppp->layers);
		list_for_each_entry(l, &n->items, entry) {
			d = l->init(ppp);
			d->layer = l;
			d->started = 0;
			d->node = n1;
			list_add_tail(&d->entry, &n1->items);
		}
	}
}

static void _free_layers(struct ppp_t *ppp)
{
	struct layer_node_t *n;
	struct ppp_layer_data_t *d;
	
	while (!list_empty(&ppp->layers)) {
		n = list_entry(ppp->layers.next, typeof(*n), entry);
		while (!list_empty(&n->items)) {
			d = list_entry(n->items.next, typeof(*d), entry);
			list_del(&d->entry);
			d->layer->free(d);
		}
		list_del(&n->entry);
		_free(n);
	}
}

static void start_first_layer(struct ppp_t *ppp)
{
	struct layer_node_t *n;
	struct ppp_layer_data_t *d;

	n = list_entry(ppp->layers.next, typeof(*n), entry);
	list_for_each_entry(d, &n->items, entry) {
		d->starting = 1;
		if (d->layer->start(d)) {
			ppp_terminate(ppp, TERM_NAS_ERROR, 0);
			return;
		}
	}
}

struct ppp_layer_data_t *ppp_find_layer_data(struct ppp_t *ppp, struct ppp_layer_t *layer)
{
	struct layer_node_t *n;
	struct ppp_layer_data_t *d;

	list_for_each_entry(n,&ppp->layers,entry) {
		list_for_each_entry(d,&n->items,entry) {
			if (d->layer == layer)
				return d;
		}
	}
	
	return NULL;
}

void ppp_shutdown_soft(void)
{
	ppp_shutdown = 1;

	if (!ppp_stat.starting && !ppp_stat.active && !ppp_stat.finishing)
		kill(getpid(), SIGTERM);
}

static void save_seq(void)
{
	FILE *f;
	char *opt = conf_get_opt("ppp", "seq-file");
	if (!opt)
		opt = "/var/run/accel-ppp/seq";

	f = fopen(opt, "w");
	if (f) {
		fprintf(f, "%llu", seq);
		fclose(f);
	}
}

static void load_config(void)
{
	char *opt;

	opt = conf_get_opt("ppp", "verbose");
	if (opt && atoi(opt) > 0)
		conf_ppp_verbose = 1;

	opt = conf_get_opt("ppp", "sid-case");
	if (opt) {
		if (!strcmp(opt, "upper"))
			conf_sid_ucase = 1;
		else if (strcmp(opt, "lower"))
			log_emerg("ppp: sid-case: invalid format\n");
	}
	
	opt = conf_get_opt("ppp", "single-session");
	if (opt) {
		if (!strcmp(opt, "deny"))
			conf_single_session = 0;
		else if (!strcmp(opt, "replace"))
			conf_single_session = 1;
	} else 
		conf_single_session = -1;
}

static void init(void)
{
	char *opt;
	FILE *f;

	buf_pool = mempool_create(PPP_MRU);

	sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock_fd < 0) {
		perror("socket");
		_exit(EXIT_FAILURE);
	}

	sock6_fd = socket(AF_INET6, SOCK_DGRAM, 0);
	if (sock6_fd < 0)
		log_warn("ppp: kernel doesn't support ipv6\n");

	opt = conf_get_opt("ppp", "seq-file");
	if (!opt)
		opt = "/var/run/accel-ppp/seq";
	
	f = fopen(opt, "r");
	if (f) {
		fscanf(f, "%llu", &seq);
		fclose(f);
	} else
		seq = (unsigned long long)random() * (unsigned long long)random();

	load_config();
	triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);

	atexit(save_seq);
}

DEFINE_INIT(2, init);