summaryrefslogtreecommitdiff
path: root/accel-pptpd/ppp.c
blob: b032b06401a2fa399c54246db912d5088651a104 (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

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <linux/ppp_defs.h>
#include <linux/if_ppp.h>

#include "triton/triton.h"

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

static void ppp_read(struct triton_md_handler_t*);
static void ppp_write(struct triton_md_handler_t*);
static void ppp_timeout(struct triton_md_handler_t*);

struct ppp_t *alloc_ppp(void)
{
	struct ppp_t *ppp=malloc(sizeof(*ppp));
	memset(ppp,0,sizeof(*ppp));
	ppp->out_buf=malloc(PPP_MTU+PPP_HDRLEN);
	ppp->in_buf=malloc(PPP_MRU+PPP_HDRLEN);
	ppp->mtu=PPP_MTU;
	ppp->mru=PPP_MRU;
	return ppp;
}

int establish_ppp(struct ppp_t *ppp)
{
	/* Open an instance of /dev/ppp and connect the channel to it */
	if (ioctl(ppp->fd, PPPIOCGCHAN, &ppp->chan_idx)==-1)
	{
	    log_error("Couldn't get channel number\n");
	    return -1;
	}

	ppp->chan_fd=open("/dev/ppp", O_RDWR);
	if (ppp->chan_fd<0)
	{
	    log_error("Couldn't reopen /dev/ppp\n");
	    return -1;
	}

	if (ioctl(ppp->chan_fd, PPPIOCATTCHAN, &ppp->chan_idx)<0)
	{
	    log_error("Couldn't attach to channel %d\n", ppp->chan_idx);
	    goto exit_close_chan;
	}

	ppp->unit_fd=open("/dev/ppp", O_RDWR);
	if (ppp->unit_fd<0)
	{
	    log_error("Couldn't reopen /dev/ppp\n");
	    goto exit_close_chan;
	}

	ppp->unit_idx=-1;
	if (ioctl(ppp->unit_fd, PPPIOCNEWUNIT, &ppp->unit_idx)<0)
	{
		log_error("Couldn't create new ppp unit\n");
		goto exit_clodse_unit;
	}

  if (ioctl(ppp->chan_fd, PPPIOCCONNECT, &ppp->unit_idx)<0)
  {
		log_error("Couldn't attach to PPP unit %d\n", ppp->unit_idx);
		goto exit_clodse_unit;
	}

	log_info("connect: ppp%i <--> pptp(%s)\n",ppp->unit_idx,ppp->chan_name);

	ppp->h=malloc(sizeof(*ppp->h));
	memset(ppp->h,0,sizeof(*ppp->h));
	ppp->h->pd=ppp;
	ppp->h->fd=ppp->chan_fd;
	ppp->h->read=ppp_read;
	ppp->h->write=ppp_write;
	ppp->h->timeout=ppp_timeout;
	ppp->h->twait=-1;
	triton_md_register_handler(ppp->h);
	triton_md_enable_handler(ppp->h,MD_MODE_READ);
	INIT_LIST_HEAD(&ppp->handlers);

	ppp->cur_layer=PPP_LAYER_LCP;
	
	lcp_start(ppp);

	return 0;

exit_clodse_unit:
	close(ppp->unit_fd);
exit_close_chan:
	close(ppp->chan_fd);
	return -1;
}

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

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

	if (ppp->out_buf_size) return -1;
	if (size>PPP_MTU+PPP_HDRLEN) return -1;

	printf("ppp: send: ");
	print_buf((uint8_t*)data,size);
	
	n=write(ppp->unit_fd,data,size);
	/*if (n>=0)
	{
		if (n!=ppp->out_buf_size-ppp->out_buf_pos)
		{
			ppp->out_buf_pos+=n;
			triton_md_enable_handler(ppp->h,MD_MODE_WRITE);
		}
	}*/
	return n;
}

static void ppp_read(struct triton_md_handler_t*h)
{
	struct ppp_t *ppp=(struct ppp_t *)h->pd;
	struct ppp_handler_t *ppp_h=NULL;
	uint16_t proto;

	ppp->in_buf_size=read(h->fd,ppp->in_buf,PPP_MRU+PPP_HDRLEN);

	printf("ppp: recv: ");
	print_buf(ppp->in_buf,ppp->in_buf_size);

	proto=ntohs(*(uint16_t*)ppp->in_buf);
	list_for_each_entry(ppp_h,&ppp->handlers,entry)
	{
		if (ppp_h->proto==proto)
		{
			ppp_h->recv(ppp_h);
			return;
		}
	}

	log_warn("discarding unknown packet %x\n",proto);
}
static void ppp_write(struct triton_md_handler_t*h)
{
	struct ppp_t *ppp=(struct ppp_t *)h->pd;

	int n=write(ppp->unit_fd,ppp->out_buf+ppp->out_buf_pos,ppp->out_buf_size-ppp->out_buf_pos);
	if (n>=0)
	{
		ppp->out_buf_pos+=n;
		if (ppp->out_buf_pos==ppp->out_buf_size)
		{
			triton_md_disable_handler(ppp->h,MD_MODE_WRITE);
			ppp->out_buf_pos=0;
			ppp->out_buf_size=0;
		}
	}
}
static void ppp_timeout(struct triton_md_handler_t*h)
{

}

void ppp_layer_started(struct ppp_t *ppp)
{
	switch(ppp->cur_layer)
	{
		case PPP_LAYER_LCP:
			ppp->cur_layer++;
			if (auth_start(ppp))
				break;
		case PPP_LAYER_AUTH:
			ppp->cur_layer++;
			if (ccp_start(ppp))
				break;
		case PPP_LAYER_CCP:
			ppp->cur_layer++;
			if (ipcp_start(ppp))
				break;
		case PPP_LAYER_IPCP:
				break;
	}
}
void ppp_terminate(struct ppp_t *ppp)
{
	switch(ppp->cur_layer)
	{
		case PPP_LAYER_IPCP:
			ppp->cur_layer--;
			ipcp_finish(ppp);
		case PPP_LAYER_CCP:
			ppp->cur_layer--;
			ccp_finish(ppp);
		case PPP_LAYER_AUTH:
			ppp->cur_layer--;
			auth_finish(ppp);
		case PPP_LAYER_LCP:
			ppp->cur_layer--;
			lcp_finish(ppp);
	}
}


void ppp_register_handler(struct ppp_t *ppp,struct ppp_handler_t *h)
{
	list_add_tail(&h->entry,&ppp->handlers);
}
void ppp_unregister_handler(struct ppp_t *ppp,struct ppp_handler_t *h)
{
	list_del(&h->entry);
}