summaryrefslogtreecommitdiff
path: root/accel-pppd/ppp/ppp_ipv6cp.c
blob: 816abc42ec993d189f4e4244a9964ec6ef53531e (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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
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
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "linux_ppp.h"

#include "triton.h"

#include "log.h"
#include "events.h"

#include "ppp.h"
#include "ppp_ipv6cp.h"

#include "memdebug.h"

struct recv_opt_t
{
	struct list_head entry;
	struct ipv6cp_opt_hdr_t *hdr;
	int len;
	int state;
	struct ipv6cp_option_t *lopt;
};

#define IPV6_DENY 0
#define IPV6_ALLOW 1
#define IPV6_PREFERE 2
#define IPV6_REQUIRE 3

#define START_TIMEOUT 60

static int conf_ipv6 = IPV6_DENY;

static LIST_HEAD(option_handlers);
static struct ppp_layer_t ipv6cp_layer;

static void ipv6cp_layer_up(struct ppp_fsm_t*);
static void ipv6cp_layer_down(struct ppp_fsm_t*);
static void ipv6cp_layer_finished(struct ppp_fsm_t*);
static int send_conf_req(struct ppp_fsm_t*);
static void send_conf_ack(struct ppp_fsm_t*);
static void send_conf_nak(struct ppp_fsm_t*);
static void send_conf_rej(struct ppp_fsm_t*);
static void ipv6cp_recv(struct ppp_handler_t*);
static void ipv6cp_recv_proto_rej(struct ppp_handler_t*);
static void send_term_req(struct ppp_fsm_t *fsm);
static void send_term_ack(struct ppp_fsm_t *fsm);

static void ipv6cp_options_init(struct ppp_ipv6cp_t *ipv6cp)
{
	struct ipv6cp_option_t *lopt;
	struct ipv6cp_option_handler_t *h;

	ipv6cp->conf_req_len = sizeof(struct ipv6cp_hdr_t);
	
	list_for_each_entry(h,&option_handlers,entry) {
		lopt = h->init(ipv6cp);
		if (lopt) {
			lopt->h = h;
			list_add_tail(&lopt->entry, &ipv6cp->options);
			ipv6cp->conf_req_len += lopt->len;
		}
	}
}

static void ipv6cp_options_free(struct ppp_ipv6cp_t *ipv6cp)
{
	struct ipv6cp_option_t *lopt;

	while (!list_empty(&ipv6cp->options)) {
		lopt = list_entry(ipv6cp->options.next, typeof(*lopt), entry);
		list_del(&lopt->entry);
		lopt->h->free(ipv6cp, lopt);
	}
}

static struct ppp_layer_data_t *ipv6cp_layer_init(struct ppp_t *ppp)
{
	struct ppp_ipv6cp_t *ipv6cp = _malloc(sizeof(*ipv6cp));
	memset(ipv6cp, 0, sizeof(*ipv6cp));
	
	log_ppp_debug("ipv6cp_layer_init\n");

	ipv6cp->ppp = ppp;
	ipv6cp->fsm.ppp = ppp;
	
	ipv6cp->hnd.proto = PPP_IPV6CP;
	ipv6cp->hnd.recv = ipv6cp_recv;
	ipv6cp->hnd.recv_proto_rej = ipv6cp_recv_proto_rej;
	
	ppp_register_unit_handler(ppp, &ipv6cp->hnd);

	ipv6cp->fsm.proto = PPP_IPV6CP;
	ppp_fsm_init(&ipv6cp->fsm);

	ipv6cp->fsm.layer_up = ipv6cp_layer_up;
	ipv6cp->fsm.layer_finished = ipv6cp_layer_finished;
	ipv6cp->fsm.layer_down = ipv6cp_layer_down;
	ipv6cp->fsm.send_conf_req = send_conf_req;
	ipv6cp->fsm.send_conf_ack = send_conf_ack;
	ipv6cp->fsm.send_conf_nak = send_conf_nak;
	ipv6cp->fsm.send_conf_rej = send_conf_rej;
	ipv6cp->fsm.send_term_req = send_term_req;
	ipv6cp->fsm.send_term_ack = send_term_ack;

	INIT_LIST_HEAD(&ipv6cp->options);
	INIT_LIST_HEAD(&ipv6cp->ropt_list);

	ipv6cp->ld.passive = conf_ipv6 == IPV6_ALLOW || conf_ipv6 == IPV6_DENY;
	
	return &ipv6cp->ld;
}

static void ipv6cp_start_timeout(struct triton_timer_t *t)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(t, typeof(*ipv6cp), timeout);

	triton_timer_del(t);
		
	if (ipv6cp->ppp->state == PPP_STATE_STARTING)
		ppp_terminate(ipv6cp->ppp, TERM_USER_ERROR, 0);
}

int ipv6cp_layer_start(struct ppp_layer_data_t *ld)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(ld, typeof(*ipv6cp), ld);
	
	log_ppp_debug("ipv6cp_layer_start\n");

	ipv6cp_options_init(ipv6cp);

	ipv6cp->starting = 1;

	if (conf_ipv6 != IPV6_DENY) {
		if (ipv6cp->ld.passive) {
			ipv6cp->timeout.expire = ipv6cp_start_timeout;
			ipv6cp->timeout.expire_tv.tv_sec = START_TIMEOUT;
			triton_timer_add(ipv6cp->ppp->ctrl->ctx, &ipv6cp->timeout, 0);
		} else {
			ppp_fsm_lower_up(&ipv6cp->fsm);
			if (ppp_fsm_open(&ipv6cp->fsm))
				return -1;
		}
	}
	
	return 0;
}

void ipv6cp_layer_finish(struct ppp_layer_data_t *ld)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(ld, typeof(*ipv6cp), ld);
	
	log_ppp_debug("ipv6cp_layer_finish\n");

	ipv6cp->fsm.fsm_state = FSM_Closed;
	
	log_ppp_debug("ipv6cp_layer_finished\n");
	ppp_layer_finished(ipv6cp->ppp, &ipv6cp->ld);
}

void ipv6cp_layer_free(struct ppp_layer_data_t *ld)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(ld, typeof(*ipv6cp), ld);
	
	log_ppp_debug("ipv6cp_layer_free\n");
		
	ppp_unregister_handler(ipv6cp->ppp, &ipv6cp->hnd);
	ipv6cp_options_free(ipv6cp);
	ppp_fsm_free(&ipv6cp->fsm);

	if (ipv6cp->timeout.tpd)
		triton_timer_del(&ipv6cp->timeout);

	_free(ipv6cp);
}

static void __ipv6cp_layer_up(struct ppp_ipv6cp_t *ipv6cp)
{
	log_ppp_debug("ipv6cp_layer_started\n");

	if (!ipv6cp->started) {
		ipv6cp->started = 1;
		ppp_layer_started(ipv6cp->ppp, &ipv6cp->ld);
	}
}

static void ipv6cp_layer_up(struct ppp_fsm_t *fsm)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(fsm, typeof(*ipv6cp), fsm);
	
	if (!ipv6cp->delay_ack)
		__ipv6cp_layer_up(ipv6cp);
}

static void ipv6cp_layer_finished(struct ppp_fsm_t *fsm)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(fsm, typeof(*ipv6cp), fsm);

	log_ppp_debug("ipv6cp_layer_finished\n");

	if (!ipv6cp->started) {
		if (conf_ipv6 == IPV6_REQUIRE)
			ppp_terminate(ipv6cp->ppp, TERM_USER_ERROR, 0);
		else
			ppp_layer_passive(ipv6cp->ppp, &ipv6cp->ld);
	} else if (!ipv6cp->ppp->terminating)
		ppp_terminate(ipv6cp->ppp, TERM_USER_ERROR, 0);
}

static void ipv6cp_layer_down(struct ppp_fsm_t *fsm)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(fsm, typeof(*ipv6cp), fsm);

	log_ppp_debug("ipv6cp_layer_down\n");

	ppp_fsm_close(fsm);
}

static void print_ropt(struct recv_opt_t *ropt)
{
	int i;
	uint8_t *ptr = (uint8_t*)ropt->hdr;

	log_ppp_info2("<");
	for (i = 0; i < ropt->len; i++) {
		log_ppp_info2(" %x", ptr[i]);
	}
	log_ppp_info2(" >");
}

static int send_conf_req(struct ppp_fsm_t *fsm)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(fsm, typeof(*ipv6cp), fsm);
	uint8_t *buf = _malloc(ipv6cp->conf_req_len), *ptr = buf;
	struct ipv6cp_hdr_t *ipv6cp_hdr = (struct ipv6cp_hdr_t*)ptr;
	struct ipv6cp_option_t *lopt;
	int n;

	ipv6cp_hdr->proto = htons(PPP_IPV6CP);
	ipv6cp_hdr->code = CONFREQ;
	ipv6cp_hdr->id = ipv6cp->fsm.id;
	ipv6cp_hdr->len = 0;
	
	ptr += sizeof(*ipv6cp_hdr);

	list_for_each_entry(lopt, &ipv6cp->options, entry) {
		n = lopt->h->send_conf_req(ipv6cp, lopt, ptr);
		if (n < 0) {
			if (n == IPV6CP_OPT_TERMACK)
				goto out;
			if (n == IPV6CP_OPT_CLOSE && conf_ipv6 != IPV6_REQUIRE) {
				ppp_fsm_close2(fsm);
				goto out;
			}
			_free(buf);
			return -1;
		}
		if (n) {
			ptr += n;
			lopt->print = 1;
		} else
			lopt->print = 0;
	}

	if (conf_ppp_verbose) {
		log_ppp_info2("send [IPV6CP ConfReq id=%x", ipv6cp_hdr->id);
		list_for_each_entry(lopt,&ipv6cp->options,entry) {
			if (lopt->print) {
				log_ppp_info2(" ");
				lopt->h->print(log_ppp_info2, lopt, NULL);
			}
		}
		log_ppp_info2("]\n");
	}

	ipv6cp_hdr->len = htons(ptr - buf - 2);
	ppp_unit_send(ipv6cp->ppp, ipv6cp_hdr, ptr - buf);

out:
	_free(buf);

	return 0;
}

static void send_conf_ack(struct ppp_fsm_t *fsm)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(fsm, typeof(*ipv6cp), fsm);
	struct ipv6cp_hdr_t *hdr = (struct ipv6cp_hdr_t*)ipv6cp->ppp->buf;

	if (ipv6cp->delay_ack) {
		send_term_ack(fsm);
		return;
	}

	hdr->code = CONFACK;

	if (conf_ppp_verbose)
		log_ppp_info2("send [IPV6CP ConfAck id=%x]\n", ipv6cp->fsm.recv_id);

	ppp_unit_send(ipv6cp->ppp, hdr, ntohs(hdr->len) + 2);
}

static void send_conf_nak(struct ppp_fsm_t *fsm)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(fsm, typeof(*ipv6cp), fsm);
	uint8_t *buf = _malloc(ipv6cp->conf_req_len), *ptr = buf, *ptr1;
	struct ipv6cp_hdr_t *ipv6cp_hdr = (struct ipv6cp_hdr_t*)ptr;
	struct recv_opt_t *ropt;

	if (conf_ppp_verbose)
		log_ppp_info2("send [IPV6CP ConfNak id=%x", ipv6cp->fsm.recv_id);

	ipv6cp_hdr->proto = htons(PPP_IPV6CP);
	ipv6cp_hdr->code = CONFNAK;
	ipv6cp_hdr->id = ipv6cp->fsm.recv_id;
	ipv6cp_hdr->len = 0;
	
	ptr += sizeof(*ipv6cp_hdr);

	list_for_each_entry(ropt, &ipv6cp->ropt_list, entry) {
		if (ropt->state == IPV6CP_OPT_NAK) {
			ptr1 = ptr;
			ptr += ropt->lopt->h->send_conf_nak(ipv6cp, ropt->lopt, ptr);
		}
		if (conf_ppp_verbose) {
			log_ppp_info2(" ");
			ropt->lopt->h->print(log_ppp_info2, ropt->lopt, ptr1);
		}
	}
	
	if (conf_ppp_verbose)
		log_ppp_info2("]\n");

	ipv6cp_hdr->len = htons(ptr-buf-2);
	ppp_unit_send(ipv6cp->ppp, ipv6cp_hdr, ptr - buf);

	_free(buf);
}

static void send_conf_rej(struct ppp_fsm_t *fsm)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(fsm, typeof(*ipv6cp), fsm);
	uint8_t *buf = _malloc(ipv6cp->ropt_len + sizeof(struct ipv6cp_hdr_t)), *ptr = buf;
	struct ipv6cp_hdr_t *ipv6cp_hdr = (struct ipv6cp_hdr_t*)ptr;
	struct recv_opt_t *ropt;

	if (conf_ppp_verbose)
		log_ppp_info2("send [IPV6CP ConfRej id=%x", ipv6cp->fsm.recv_id);

	ipv6cp_hdr->proto = htons(PPP_IPV6CP);
	ipv6cp_hdr->code = CONFREJ;
	ipv6cp_hdr->id = ipv6cp->fsm.recv_id;
	ipv6cp_hdr->len = 0;

	ptr += sizeof(*ipv6cp_hdr);

	list_for_each_entry(ropt, &ipv6cp->ropt_list, entry) {
		if (ropt->state == IPV6CP_OPT_REJ) {
			if (conf_ppp_verbose) {
				log_ppp_info2(" ");
				if (ropt->lopt)
					ropt->lopt->h->print(log_ppp_info2, ropt->lopt, (uint8_t*)ropt->hdr);
				else
					print_ropt(ropt);
			}
			memcpy(ptr, ropt->hdr, ropt->len);
			ptr += ropt->len;
		}
	}

	if (conf_ppp_verbose)
		log_ppp_info2("]\n");

	ipv6cp_hdr->len = htons(ptr - buf - 2);
	ppp_unit_send(ipv6cp->ppp, ipv6cp_hdr, ptr-buf);

	_free(buf);
}

static int ipv6cp_recv_conf_req(struct ppp_ipv6cp_t *ipv6cp, uint8_t *data, int size)
{
	struct ipv6cp_opt_hdr_t *hdr;
	struct recv_opt_t *ropt;
	struct ipv6cp_option_t *lopt;
	int r,ret = 1;

	ipv6cp->ropt_len = size;

	while (size > 0) {
		hdr = (struct ipv6cp_opt_hdr_t *)data;

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

		if (hdr->len > size)
			ropt->len = size;
		else
			ropt->len = hdr->len;
		ropt->hdr = hdr;
		ropt->state = IPV6CP_OPT_NONE;
		list_add_tail(&ropt->entry, &ipv6cp->ropt_list);

		data += ropt->len;
		size -= ropt->len;
	}
	
	list_for_each_entry(lopt, &ipv6cp->options, entry)
		lopt->state=IPV6CP_OPT_NONE;

	if (conf_ppp_verbose) {
		log_ppp_info2("recv [IPV6CP ConfReq id=%x", ipv6cp->fsm.recv_id);

		list_for_each_entry(ropt, &ipv6cp->ropt_list, entry) {
			list_for_each_entry(lopt, &ipv6cp->options, entry) {
				if (lopt->id == ropt->hdr->id) {
					ropt->lopt = lopt;
					log_ppp_info2(" ");
					lopt->h->print(log_ppp_info2, lopt, (uint8_t*)ropt->hdr);
					break;
				}
			}
			if (!ropt->lopt) {
				log_ppp_info2(" ");
				print_ropt(ropt);
			}
		}
		log_ppp_info2("]\n");
	}

	list_for_each_entry(ropt, &ipv6cp->ropt_list, entry) {
		list_for_each_entry(lopt, &ipv6cp->options, entry) {
			if (lopt->id == ropt->hdr->id) {
				r = lopt->h->recv_conf_req(ipv6cp, lopt, (uint8_t*)ropt->hdr);
				if (r == IPV6CP_OPT_TERMACK) {
					send_term_ack(&ipv6cp->fsm);
					return 0;
				}
				if (r == IPV6CP_OPT_CLOSE) {
					if (conf_ipv6 == IPV6_REQUIRE)
						ppp_terminate(ipv6cp->ppp, TERM_NAS_ERROR, 0);
					else
						lcp_send_proto_rej(ipv6cp->ppp, PPP_IPV6CP);
					return 0;
				}
				if (ipv6cp->ppp->stop_time)
					return -1;
				lopt->state = r;
				ropt->state = r;
				ropt->lopt = lopt;
				if (r < ret)
					ret = r;
				break;
			}	
		}
		if (!ropt->lopt) {
			ropt->state = IPV6CP_OPT_REJ;
			ret = IPV6CP_OPT_REJ;
		}
	}


	/*list_for_each_entry(lopt,&ipv6cp->options,entry)
	{
		if (lopt->state==IPV6CP_OPT_NONE)
		{
			r=lopt->h->recv_conf_req(ipv6cp,lopt,NULL);
			lopt->state=r;
			if (r<ret) ret=r;
		}
	}*/

	return ret;
}

static void ipv6cp_free_conf_req(struct ppp_ipv6cp_t *ipv6cp)
{
	struct recv_opt_t *ropt;

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

static int ipv6cp_recv_conf_rej(struct ppp_ipv6cp_t *ipv6cp, uint8_t *data, int size)
{
	struct ipv6cp_opt_hdr_t *hdr;
	struct ipv6cp_option_t *lopt;
	int res = 0;

	if (conf_ppp_verbose)
		log_ppp_info2("recv [IPV6CP ConfRej id=%x", ipv6cp->fsm.recv_id);

	/*if (ipv6cp->fsm.recv_id != ipv6cp->fsm.id) {
		if (conf_ppp_verbose)
			log_ppp_info2(": id mismatch ]\n");
		return 0;
	}*/

	while (size > 0) {
		hdr = (struct ipv6cp_opt_hdr_t *)data;
		
		list_for_each_entry(lopt, &ipv6cp->options, entry) {
			if (lopt->id == hdr->id) {
				if (!lopt->h->recv_conf_rej)
					res = -1;
				else if (lopt->h->recv_conf_rej(ipv6cp, lopt, data))
					res = -1;
				break;
			}
		}

		data += hdr->len;
		size -= hdr->len;
	}

	if (conf_ppp_verbose)
		log_ppp_info2("]\n");

	return res;
}

static int ipv6cp_recv_conf_nak(struct ppp_ipv6cp_t *ipv6cp, uint8_t *data, int size)
{
	struct ipv6cp_opt_hdr_t *hdr;
	struct ipv6cp_option_t *lopt;
	int res = 0;

	if (conf_ppp_verbose)
		log_ppp_info2("recv [IPV6CP ConfNak id=%x", ipv6cp->fsm.recv_id);

	/*if (ipv6cp->fsm.recv_id != ipv6cp->fsm.id) {
		if (conf_ppp_verbose)
			log_ppp_info2(": id mismatch ]\n");
		return 0;
	}*/

	while (size > 0) {
		hdr = (struct ipv6cp_opt_hdr_t *)data;
		
		list_for_each_entry(lopt, &ipv6cp->options, entry) {
			if (lopt->id == hdr->id) {
				if (conf_ppp_verbose) {
					log_ppp_info2(" ");
					lopt->h->print(log_ppp_info2,lopt,data);
				}
				if (lopt->h->recv_conf_nak && lopt->h->recv_conf_nak(ipv6cp, lopt, data))
					res =- 1;
				break;
			}
		}

		data += hdr->len;
		size -= hdr->len;
	}

	if (conf_ppp_verbose)
		log_ppp_info2("]\n");

	return res;
}

static int ipv6cp_recv_conf_ack(struct ppp_ipv6cp_t *ipv6cp, uint8_t *data, int size)
{
	struct ipv6cp_opt_hdr_t *hdr;
	struct ipv6cp_option_t *lopt;
	int res = 0;

	if (conf_ppp_verbose)
		log_ppp_info2("recv [IPV6CP ConfAck id=%x", ipv6cp->fsm.recv_id);

	/*if (ipv6cp->fsm.recv_id != ipv6cp->fsm.id) {
		if (conf_ppp_verbose)
			log_ppp_info2(": id mismatch ]\n");
		return 0;
	}*/

	while (size > 0) {
		hdr = (struct ipv6cp_opt_hdr_t *)data;
		
		list_for_each_entry(lopt, &ipv6cp->options, entry) {
			if (lopt->id == hdr->id) {
				if (conf_ppp_verbose) {
					log_ppp_info2(" ");
					lopt->h->print(log_ppp_info2, lopt, data);
				}
				if (!lopt->h->recv_conf_ack)
					break;
				if (lopt->h->recv_conf_ack(ipv6cp, lopt, data))
					res = -1;
				break;
			}
		}

		data += hdr->len;
		size -= hdr->len;
	}

	if (conf_ppp_verbose)
		log_ppp_info2("]\n");

	return res;
}

static void send_term_req(struct ppp_fsm_t *fsm)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(fsm, typeof(*ipv6cp), fsm);
	struct ipv6cp_hdr_t hdr = {
		.proto = htons(PPP_IPV6CP),
		.code = TERMREQ,
		.id = ++ipv6cp->fsm.id,
		.len = htons(4),
	};

	if (conf_ppp_verbose)
		log_ppp_info2("send [IPV6CP TermReq id=%i]\n", hdr.id);

	ppp_unit_send(ipv6cp->ppp, &hdr, 6);
}

static void send_term_ack(struct ppp_fsm_t *fsm)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(fsm, typeof(*ipv6cp), fsm);
	struct ipv6cp_hdr_t hdr = {
		.proto = htons(PPP_IPV6CP),
		.code = TERMACK,
		.id = ipv6cp->fsm.recv_id,
		.len = htons(4),
	};

	if (conf_ppp_verbose)
		log_ppp_info2("send [IPV6CP TermAck id=%i]\n", hdr.id);
	
	ppp_unit_send(ipv6cp->ppp, &hdr, 6);
}

static void ipv6cp_recv(struct ppp_handler_t*h)
{
	struct ipv6cp_hdr_t *hdr;
	struct ppp_ipv6cp_t *ipv6cp = container_of(h, typeof(*ipv6cp), hnd);
	int r;
	int delay_ack = ipv6cp->delay_ack;

	if (!ipv6cp->starting || ipv6cp->fsm.fsm_state == FSM_Closed || ipv6cp->ppp->terminating || conf_ipv6 == IPV6_DENY) {
		if (conf_ppp_verbose)
			log_ppp_warn("IPV6CP: discarding packet\n");
		if (ipv6cp->ppp->terminating)
			return;
		if (ipv6cp->fsm.fsm_state == FSM_Closed || conf_ipv6 == IPV6_DENY)
			lcp_send_proto_rej(ipv6cp->ppp, PPP_IPV6CP);
		return;
	}

	if (ipv6cp->ppp->buf_size < PPP_HEADERLEN + 2) {
		log_ppp_warn("IPV6CP: short packet received\n");
		return;
	}

	hdr = (struct ipv6cp_hdr_t *)ipv6cp->ppp->buf;
	if (ntohs(hdr->len) < PPP_HEADERLEN) {
		log_ppp_warn("IPV6CP: short packet received\n");
		return;
	}
	
	if ((hdr->code == CONFACK || hdr->code == CONFNAK || hdr->code == CONFREJ) && hdr->id != ipv6cp->fsm.id)
		return;

	ipv6cp->fsm.recv_id = hdr->id;
	
	switch(hdr->code) {
		case CONFREQ:
			r = ipv6cp_recv_conf_req(ipv6cp,(uint8_t*)(hdr + 1), ntohs(hdr->len) - PPP_HDRLEN);
			if (ipv6cp->ppp->stop_time) {
				ipv6cp_free_conf_req(ipv6cp);
				return;
			}
			if (r && ipv6cp->ld.passive) {
				ipv6cp->ld.passive = 0;
				ppp_fsm_lower_up(&ipv6cp->fsm);
				ppp_fsm_open(&ipv6cp->fsm);
				triton_timer_del(&ipv6cp->timeout);
			}
			if (delay_ack && !ipv6cp->delay_ack)
				__ipv6cp_layer_up(ipv6cp);
			if (ipv6cp->started || delay_ack) {
				if (r == IPV6CP_OPT_ACK)
					send_conf_ack(&ipv6cp->fsm);
				else
					r = IPV6CP_OPT_FAIL;
			} else {
				switch(r) {
					case IPV6CP_OPT_ACK:
						ppp_fsm_recv_conf_req_ack(&ipv6cp->fsm);
						break;
					case IPV6CP_OPT_NAK:
						ppp_fsm_recv_conf_req_nak(&ipv6cp->fsm);
						break;
					case IPV6CP_OPT_REJ:
						ppp_fsm_recv_conf_req_rej(&ipv6cp->fsm);
						break;
				}
			}
			ipv6cp_free_conf_req(ipv6cp);
			if (r == IPV6CP_OPT_FAIL)
				ppp_terminate(ipv6cp->ppp, TERM_USER_ERROR, 0);
			break;
		case CONFACK:
			if (ipv6cp_recv_conf_ack(ipv6cp,(uint8_t*)(hdr + 1), ntohs(hdr->len) - PPP_HDRLEN))
				ppp_terminate(ipv6cp->ppp, TERM_USER_ERROR, 0);
			else
				ppp_fsm_recv_conf_ack(&ipv6cp->fsm);
			break;
		case CONFNAK:
			ipv6cp_recv_conf_nak(ipv6cp,(uint8_t*)(hdr + 1), ntohs(hdr->len) - PPP_HDRLEN);
			ppp_fsm_recv_conf_rej(&ipv6cp->fsm);
			break;
		case CONFREJ:
			if (ipv6cp_recv_conf_rej(ipv6cp, (uint8_t*)(hdr + 1), ntohs(hdr->len) - PPP_HDRLEN))
				ppp_terminate(ipv6cp->ppp, TERM_USER_ERROR, 0);
			else
				ppp_fsm_recv_conf_rej(&ipv6cp->fsm);
			break;
		case TERMREQ:
			if (conf_ppp_verbose)
				log_ppp_info2("recv [IPV6CP TermReq id=%x]\n", hdr->id);
			ppp_fsm_recv_term_req(&ipv6cp->fsm);
			ppp_terminate(ipv6cp->ppp, TERM_USER_REQUEST, 0);
			break;
		case TERMACK:
			if (conf_ppp_verbose)
				log_ppp_info2("recv [IPV6CP TermAck id=%x]\n", hdr->id);
			//ppp_fsm_recv_term_ack(&ipv6cp->fsm);
			//ppp_terminate(ipv6cp->ppp, 0);
			break;
		case CODEREJ:
			if (conf_ppp_verbose)
				log_ppp_info2("recv [IPV6CP CodeRej id=%x]\n", hdr->id);
			ppp_fsm_recv_code_rej_bad(&ipv6cp->fsm);
			break;
		default:
			ppp_fsm_recv_unk(&ipv6cp->fsm);
			break;
	}
}

static void ipv6cp_recv_proto_rej(struct ppp_handler_t*h)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(h, typeof(*ipv6cp), hnd);

	if (ipv6cp->fsm.fsm_state == FSM_Initial || ipv6cp->fsm.fsm_state == FSM_Closed)
		return;
	
	ppp_fsm_lower_down(&ipv6cp->fsm);
	ppp_fsm_close(&ipv6cp->fsm);
}

int ipv6cp_option_register(struct ipv6cp_option_handler_t *h)
{
	/*struct ipv6cp_option_drv_t *p;

	list_for_each_entry(p,option_drv_list,entry)
		if (p->id==h->id) 
			return -1;*/
	
	list_add_tail(&h->entry, &option_handlers);

	return 0;
}

struct ipv6cp_option_t *ipv6cp_find_option(struct ppp_t *ppp, struct ipv6cp_option_handler_t *h)
{
	struct ppp_ipv6cp_t *ipv6cp = container_of(ppp_find_layer_data(ppp, &ipv6cp_layer), typeof(*ipv6cp), ld);
	struct ipv6cp_option_t *opt;
	
	list_for_each_entry(opt, &ipv6cp->options, entry)
		if (opt->h == h)
			return opt;
	
	log_emerg("ipv6cp: BUG: option not found\n");
	abort();
}

static struct ppp_layer_t ipv6cp_layer =
{
	.init   = ipv6cp_layer_init,
	.start  = ipv6cp_layer_start,
	.finish = ipv6cp_layer_finish,
	.free   = ipv6cp_layer_free,
};

static void load_config(void)
{
	const char *opt;

	opt = conf_get_opt("ppp", "ipv6");
	if (opt) {
		if (!strcmp(opt, "deny"))
			conf_ipv6 = IPV6_DENY;
		else if (!strcmp(opt, "allow"))
			conf_ipv6 = IPV6_ALLOW;
		else if (!strcmp(opt, "prefere"))
			conf_ipv6 = IPV6_PREFERE;
		else if (!strcmp(opt, "require"))
			conf_ipv6 = IPV6_REQUIRE;
		else
			conf_ipv6 = atoi(opt);
	}
}

static void ipv6cp_init(void)
{
	load_config();

	triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);

	ppp_register_layer("ipv6cp", &ipv6cp_layer);
}

DEFINE_INIT(5, ipv6cp_init);