summaryrefslogtreecommitdiff
path: root/accel-pptpd/ppp_lcp.c
blob: dbc819a19a3bf1ce5866f2d6e536cd63bc6f9c61 (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
#include <stdlib.h>
#include <string.h>
#include <linux/ppp_defs.h>
#include <linux/if_ppp.h>
#include <arpa/inet.h>

#include "triton/triton.h"

#include "log.h"

#include "ppp.h"
#include "ppp_lcp.h"

struct recv_opt_t
{
	struct list_head entry;
	struct lcp_opt_hdr_t *hdr;
	int len;
	int state;
	struct lcp_option_t *lopt;
};

static LIST_HEAD(option_handlers);

static void lcp_layer_up(struct ppp_fsm_t*);
static void lcp_layer_down(struct ppp_fsm_t*);
static void 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 lcp_recv(struct ppp_handler_t*);

static void lcp_options_init(struct ppp_lcp_t *lcp)
{
	struct lcp_option_t *lopt;
	struct lcp_option_handler_t *h;

	INIT_LIST_HEAD(&lcp->options);

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

static void lcp_options_free(struct ppp_lcp_t *lcp)
{
	struct lcp_option_t *lopt;

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

static struct ppp_layer_data_t *lcp_layer_init(struct ppp_t *ppp)
{
	struct ppp_lcp_t *lcp=malloc(sizeof(*lcp));
	memset(lcp,0,sizeof(*lcp));
	
	log_debug("lcp_layer_init\n");

	lcp->ppp=ppp;
	lcp->fsm.ppp=ppp;
	
	lcp->hnd.proto=PPP_LCP;
	lcp->hnd.recv=lcp_recv;
	
	ppp_register_chan_handler(ppp,&lcp->hnd);
	
	ppp_fsm_init(&lcp->fsm);

	lcp->fsm.layer_up=lcp_layer_up;
	lcp->fsm.layer_finished=lcp_layer_down;
	lcp->fsm.send_conf_req=send_conf_req;
	lcp->fsm.send_conf_ack=send_conf_ack;
	lcp->fsm.send_conf_nak=send_conf_nak;
	lcp->fsm.send_conf_rej=send_conf_rej;

	INIT_LIST_HEAD(&lcp->ropt_list);

	return &lcp->ld;
}

void lcp_layer_start(struct ppp_layer_data_t *ld)
{
	struct ppp_lcp_t *lcp=container_of(ld,typeof(*lcp),ld);
	
	log_debug("lcp_layer_start\n");

	lcp_options_init(lcp);
	ppp_fsm_lower_up(&lcp->fsm);
	ppp_fsm_open(&lcp->fsm);
}

void lcp_layer_finish(struct ppp_layer_data_t *ld)
{
	struct ppp_lcp_t *lcp=container_of(ld,typeof(*lcp),ld);
	
	log_debug("lcp_layer_finish\n");
	ppp_fsm_close(&lcp->fsm);
}

void lcp_layer_free(struct ppp_layer_data_t *ld)
{
	struct ppp_lcp_t *lcp=container_of(ld,typeof(*lcp),ld);
	
	log_debug("lcp_layer_free\n");
	
	ppp_unregister_handler(lcp->ppp,&lcp->hnd);
	lcp_options_free(lcp);
	
	free(lcp);
}

static void lcp_layer_up(struct ppp_fsm_t *fsm)
{
	struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm);
	log_debug("lcp_layer_started\n");
	ppp_layer_started(lcp->ppp,&lcp->ld);
}

static void lcp_layer_down(struct ppp_fsm_t *fsm)
{
	struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm);
	log_debug("lcp_layer_finished\n");
	ppp_layer_finished(lcp->ppp,&lcp->ld);
}

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

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

static void send_conf_req(struct ppp_fsm_t *fsm)
{
	struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm);
	uint8_t *buf=malloc(lcp->conf_req_len), *ptr=buf;
	struct lcp_hdr_t *lcp_hdr=(struct lcp_hdr_t*)ptr;
	struct lcp_option_t *lopt;
	int n;

	log_debug("send [LCP ConfReq");
	lcp_hdr->proto=htons(PPP_LCP);
	lcp_hdr->code=CONFREQ;
	lcp_hdr->id=++lcp->fsm.id;
	lcp_hdr->len=0;
	log_debug(" id=%x",lcp_hdr->id);
	
	ptr+=sizeof(*lcp_hdr);

	list_for_each_entry(lopt,&lcp->options,entry)
	{
		n=lopt->h->send_conf_req(lcp,lopt,ptr);
		if (n)
		{
			log_debug(" ");
			lopt->h->print(log_debug,lopt,NULL);
			ptr+=n;
		}
	}
	
	log_debug("]\n");

	lcp_hdr->len=htons((ptr-buf)-2);
	ppp_chan_send(lcp->ppp,lcp_hdr,ptr-buf);
}

static void send_conf_ack(struct ppp_fsm_t *fsm)
{
	struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm);
	struct lcp_hdr_t *hdr=(struct lcp_hdr_t*)lcp->ppp->chan_buf;

	hdr->code=CONFACK;
	log_debug("send [LCP ConfAck id=%x ]\n",lcp->fsm.recv_id);

	ppp_chan_send(lcp->ppp,hdr,ntohs(hdr->len)+2);
}

static void send_conf_nak(struct ppp_fsm_t *fsm)
{
	struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm);
	uint8_t *buf=malloc(lcp->conf_req_len), *ptr=buf;
	struct lcp_hdr_t *lcp_hdr=(struct lcp_hdr_t*)ptr;
	struct recv_opt_t *ropt;

	log_debug("send [LCP ConfNak id=%x",lcp->fsm.recv_id);

	lcp_hdr->proto=htons(PPP_LCP);
	lcp_hdr->code=CONFNAK;
	lcp_hdr->id=lcp->fsm.recv_id;
	lcp_hdr->len=0;
	
	ptr+=sizeof(*lcp_hdr);

	list_for_each_entry(ropt,&lcp->ropt_list,entry)
	{
		if (ropt->state==LCP_OPT_NAK)
		{
			log_debug(" ");
			ropt->lopt->h->print(log_debug,ropt->lopt,NULL);
			ptr+=ropt->lopt->h->send_conf_nak(lcp,ropt->lopt,ptr);
		}
	}
	
	log_debug("]\n");

	lcp_hdr->len=htons((ptr-buf)-2);
	ppp_chan_send(lcp->ppp,lcp_hdr,ptr-buf);
}

static void send_conf_rej(struct ppp_fsm_t *fsm)
{
	struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm);
	uint8_t *buf=malloc(lcp->ropt_len), *ptr=buf;
	struct lcp_hdr_t *lcp_hdr=(struct lcp_hdr_t*)ptr;
	struct recv_opt_t *ropt;

	log_debug("send [LCP ConfRej id=%x ",lcp->fsm.recv_id);

	lcp_hdr->proto=htons(PPP_LCP);
	lcp_hdr->code=CONFREJ;
	lcp_hdr->id=lcp->fsm.recv_id;
	lcp_hdr->len=0;

	ptr+=sizeof(*lcp_hdr);

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

	log_debug("]\n");

	lcp_hdr->len=htons((ptr-buf)-2);
	ppp_chan_send(lcp->ppp,lcp_hdr,ptr-buf);
}

static int lcp_recv_conf_req(struct ppp_lcp_t *lcp,uint8_t *data,int size)
{
	struct lcp_opt_hdr_t *hdr;
	struct recv_opt_t *ropt;
	struct lcp_option_t *lopt;
	int r,ret=1;

	lcp->ropt_len=size;

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

		ropt=malloc(sizeof(*ropt));
		if (hdr->len>size) ropt->len=size;
		else ropt->len=hdr->len;
		ropt->hdr=hdr;
		ropt->state=LCP_OPT_NONE;
		list_add_tail(&ropt->entry,&lcp->ropt_list);

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

	log_debug("recv [LCP ConfReq id=%x",lcp->fsm.recv_id);
	list_for_each_entry(ropt,&lcp->ropt_list,entry)
	{
		list_for_each_entry(lopt,&lcp->options,entry)
		{
			if (lopt->id==ropt->hdr->id)
			{
				log_debug(" ");
				lopt->h->print(log_debug,lopt,(uint8_t*)ropt->hdr);
				r=lopt->h->recv_conf_req(lcp,lopt,(uint8_t*)ropt->hdr);
				lopt->state=r;
				ropt->state=r;
				ropt->lopt=lopt;
				if (r<ret) ret=r;
				break;
			}
		}
		if (!ropt->lopt)
		{
			log_debug(" ");
			print_ropt(ropt);
			ropt->state=LCP_OPT_REJ;
			ret=LCP_OPT_REJ;
		}
	}
	log_debug("]\n");

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

	return ret;
}

static void lcp_free_conf_req(struct ppp_lcp_t *lcp)
{
	struct recv_opt_t *ropt;

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

static int lcp_recv_conf_rej(struct ppp_lcp_t *lcp,uint8_t *data,int size)
{
	struct lcp_opt_hdr_t *hdr;
	struct lcp_option_t *lopt;
	int res=0;

	log_debug("recv [LCP ConfRej id=%x",lcp->fsm.recv_id);

	if (lcp->fsm.recv_id!=lcp->fsm.id)
	{
		log_debug(": id mismatch ]\n");
		return 0;
	}

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

		data+=hdr->len;
		size-=hdr->len;
	}
	log_debug("]\n");
	return res;
}

static int lcp_recv_conf_nak(struct ppp_lcp_t *lcp,uint8_t *data,int size)
{
	struct lcp_opt_hdr_t *hdr;
	struct lcp_option_t *lopt;
	int res=0;

	log_debug("recv [LCP ConfNak id=%x",lcp->fsm.recv_id);

	if (lcp->fsm.recv_id!=lcp->fsm.id)
	{
		log_debug(": id mismatch ]\n");
		return 0;
	}

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

		data+=hdr->len;
		size-=hdr->len;
	}
	log_debug("]\n");
	return res;
}

static int lcp_recv_conf_ack(struct ppp_lcp_t *lcp,uint8_t *data,int size)
{
	struct lcp_opt_hdr_t *hdr;
	struct lcp_option_t *lopt;
	int res=0;

	log_debug("recv [LCP ConfAck id=%x",lcp->fsm.recv_id);

	if (lcp->fsm.recv_id!=lcp->fsm.id)
	{
		log_debug(": id mismatch ]\n");
		return 0;
	}

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

		data+=hdr->len;
		size-=hdr->len;
	}
	log_debug("]\n");
	return res;
}

static void lcp_recv_echo_repl(struct ppp_lcp_t *lcp,uint8_t *data,int size)
{

}

void send_echo_reply(struct ppp_lcp_t *lcp)
{
	struct lcp_echo_reply_t
	{
		struct lcp_hdr_t hdr;
		struct lcp_opt32_t magic;
	} __attribute__((packed)) msg = 
	{
		.hdr.proto=htons(PPP_LCP),
		.hdr.code=ECHOREP,
		.hdr.id=lcp->fsm.recv_id,
		.hdr.len=htons(8),
		.magic.val=0,
	};

	ppp_chan_send(lcp->ppp,&msg,ntohs(msg.hdr.len)+2);
}

static void lcp_recv(struct ppp_handler_t*h)
{
	struct lcp_hdr_t *hdr;
	struct ppp_lcp_t *lcp=container_of(h,typeof(*lcp),hnd);
	int r;
	char *term_msg;
	
	if (lcp->ppp->chan_buf_size<PPP_HEADERLEN+2)
	{
		log_warn("LCP: short packet received\n");
		return;
	}

	hdr=(struct lcp_hdr_t *)lcp->ppp->chan_buf;
	if (ntohs(hdr->len)<PPP_HEADERLEN)
	{
		log_warn("LCP: short packet received\n");
		return;
	}

	lcp->fsm.recv_id=hdr->id;
	switch(hdr->code)
	{
		case CONFREQ:
			r=lcp_recv_conf_req(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN);
			switch(r)
			{
				case LCP_OPT_ACK:
					ppp_fsm_recv_conf_req_ack(&lcp->fsm);
					break;
				case LCP_OPT_NAK:
					ppp_fsm_recv_conf_req_nak(&lcp->fsm);
					break;
				case LCP_OPT_REJ:
					ppp_fsm_recv_conf_req_rej(&lcp->fsm);
					break;
			}
			lcp_free_conf_req(lcp);
			if (r==LCP_OPT_FAIL)
				ppp_terminate(lcp->ppp);
			break;
		case CONFACK:
			if (lcp_recv_conf_ack(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN))
				ppp_terminate(lcp->ppp);
			else
				ppp_fsm_recv_conf_ack(&lcp->fsm);
			break;
		case CONFNAK:
			lcp_recv_conf_nak(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN);
			ppp_fsm_recv_conf_rej(&lcp->fsm);
			break;
		case CONFREJ:
			if (lcp_recv_conf_rej(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN))
				ppp_terminate(lcp->ppp);
			else
				ppp_fsm_recv_conf_rej(&lcp->fsm);
			break;
		case TERMREQ:
			term_msg=strndup((uint8_t*)(hdr+1),ntohs(hdr->len));
			log_debug("recv [LCP TermReq id=%x \"%s\"]\n",hdr->id,term_msg);
			free(term_msg);
			ppp_fsm_recv_term_req(&lcp->fsm);
			ppp_terminate(lcp->ppp);
			break;
		case TERMACK:
			term_msg=strndup((uint8_t*)(hdr+1),ntohs(hdr->len));
			log_debug("recv [LCP TermAck id=%x \"%s\"]\n",hdr->id,term_msg);
			free(term_msg);
			ppp_fsm_recv_term_ack(&lcp->fsm);
			break;
		case CODEREJ:
			log_debug("recv [LCP CodeRej id=%x]\n",hdr->id);
			ppp_fsm_recv_code_rej_bad(&lcp->fsm);
			break;
		case ECHOREQ:
			send_echo_reply(lcp);
			break;
		case ECHOREP:
			lcp_recv_echo_repl(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN);
			break;
		default:
			ppp_fsm_recv_unk(&lcp->fsm);
			break;
	}
}

int lcp_option_register(struct lcp_option_handler_t *h)
{
	/*struct lcp_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;
}

static struct ppp_layer_t lcp_layer=
{
	.init=lcp_layer_init,
	.start=lcp_layer_start,
	.finish=lcp_layer_finish,
	.free=lcp_layer_free,
};

static void __init lcp_init(void)
{
	ppp_register_layer("lcp",&lcp_layer);
}