summaryrefslogtreecommitdiff
path: root/sys/src/cmd/ip/ppp/thw.c
blob: e8af7c0a17e657076edafdc80e70c6de1c05939d (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
#include <u.h>
#include <libc.h>
#include <ip.h>
#include <auth.h>
#include "ppp.h"
#include "thwack.h"

typedef struct Cstate Cstate;
struct Cstate
{
	ulong		seq;
	Thwack		th;
	ulong		stats[ThwStats];
};

typedef struct Uncstate Uncstate;
struct Uncstate
{
	QLock		ackl;			/* lock for acks sent back to compressor */
	int		doack;			/* send an ack? */
	int		badpacks;		/* bad packets seen in a row */
	ulong		ackseq;			/* packets to ack */
	int		ackmask;

	int		active;			/* 0 => waiting for resetack */
	int		resetid;		/* id of most recent reset */
	Unthwack	ut;
};

enum
{
	ThwAcked	= 1UL << 23,
	ThwCompMask	= 3UL << 21,
	ThwCompressed	= 0UL << 21,
	ThwUncomp	= 1UL << 21,
	ThwUncompAdd	= 2UL << 21,		/* uncompressed, but add to decompression buffer */
	ThwSeqMask	= 0x0fffff,
	ThwSmallPack	= 96,
};

static	void		*compinit(PPP*);
static	Block*		comp(PPP*, ushort, Block*, int*);
static	Block		*compresetreq(void*, Block*);
static	void		compcompack(void*, Block*);
static	void		compfini(void*);

static	void		*uncinit(PPP*);
static	Block*		uncomp(PPP*, Block*, int *protop, Block**);
static	void		uncfini(void*);
static	void		uncresetack(void*, Block*);

Comptype cthwack = {
	compinit,
	comp,
	compresetreq,
	compfini
};

Uncomptype uncthwack = {
	uncinit,
	uncomp,
	uncresetack,
	uncfini
};

static void *
compinit(PPP *)
{
	Cstate *cs;

	cs = mallocz(sizeof(Cstate), 1);
	thwackinit(&cs->th);
	return cs;
}

static void
compfini(void *as)
{
	Cstate *cs;

	cs = as;
	thwackcleanup(&cs->th);
	free(cs);
}


static Block *
compresetreq(void *as, Block *b)
{
	Cstate *cs;
	Lcpmsg *m;
	int id;

	cs = as;
	m = (Lcpmsg*)b->rptr;
	id = m->id;

	thwackinit(&cs->th);

	freeb(b);

	netlog("thwack resetreq id=%d \n", id);

	b = alloclcp(Lresetack, id, 4, &m);
	hnputs(m->len, 4);

	return b;
}

static Block*
comp(PPP *ppp, ushort proto, Block *b, int *protop)
{
	Uncstate *uncs;
	Cstate *cs;
	Block *bb;
	ulong seq, acked;
	int n, nn, mustadd;

	cs = ppp->cstate;
	*protop = 0;

	/* put ack and protocol into b */
	n = BLEN(b);
	if(b->rptr - (2+4) < b->base)
		sysfatal("thwack: not enough header in block");
	acked = 0;
	if(ppp->unctype == &uncthwack){
		uncs = ppp->uncstate;
		qlock(&uncs->ackl);
		if(uncs->doack){
			uncs->doack = 0;
			b->rptr -= 4;
			b->rptr[0] = uncs->ackseq >> 16;
			b->rptr[1] = uncs->ackseq >> 8;
			b->rptr[2] = uncs->ackseq;
			b->rptr[3] = uncs->ackmask;
			acked = ThwAcked;
		}
		qunlock(&uncs->ackl);
	}
	if(proto > 0xff){
		b->rptr -= 2;
		b->rptr[0] = proto >> 8;
		b->rptr[1] = proto;
	}else{
		b->rptr--;
		b->rptr[0] = proto;
	}

	bb = allocb(BLEN(b) + 3);

	seq = cs->seq;
	if(n <= 3){
		mustadd = 0;
		nn = -1;
	}else{
		mustadd = n < ThwSmallPack;
		nn = thwack(&cs->th, mustadd, bb->wptr + 3, n - 3, b, seq, cs->stats);
	}
	if(nn < 0 && !mustadd){
		if(!acked || BLEN(b) + 1 > ppp->mtu){
			freeb(bb);
			if(acked)
				b->rptr += 4;
			if(proto > 0xff)
				b->rptr += 2;
			else
				b->rptr++;
			*protop = proto;
			return b;
		}
		bb->wptr[0] = (ThwUncomp | ThwAcked) >> 16;

		memmove(bb->wptr + 1, b->rptr, BLEN(b));

		bb->wptr += BLEN(b) + 1;
		freeb(b);
	}else{
		cs->seq = (seq + 1) & ThwSeqMask;
		if(nn < 0){
			nn = BLEN(b);
			memmove(bb->wptr + 3, b->rptr, nn);
			seq |= ThwUncompAdd;
		}else
			seq |= ThwCompressed;
		seq |= acked;
		bb->wptr[0] = seq>>16;
		bb->wptr[1] = seq>>8;
		bb->wptr[2] = seq;

		bb->wptr += nn + 3;
	}

	*protop = Pcdata;
	return bb;
}

static	void *
uncinit(PPP *)
{
	Uncstate *s;

	s = mallocz(sizeof(Uncstate), 1);

	s->active = 1;

	unthwackinit(&s->ut);

	return s;
}

static	void
uncfini(void *as)
{
	free(as);
}

static	void
uncresetack(void *as, Block *b)
{
	Uncstate *s;
	Lcpmsg *m;

	s = as;
	m = (Lcpmsg*)b->rptr;

	/*
	 * rfc 1962 says we must reset every message
	 * we don't since we may have acked some messages
	 * which the compressor will use in the future.
	 */
	netlog("unthwack resetack id=%d resetid=%d active=%d\n", m->id, s->resetid, s->active);
	if(m->id == (uchar)s->resetid && !s->active){
		s->active = 1;
		unthwackinit(&s->ut);
	}
}

static	Block*
uncomp(PPP *ppp, Block *bb, int *protop, Block **reply)
{
	Lcpmsg *m;
	Cstate *cs;
	Uncstate *uncs;
	Block *b, *r;
	ulong seq, mseq;
	ushort proto;
	uchar mask;
	int n;

	*reply = nil;
	*protop = 0;
	uncs = ppp->uncstate;

	if(BLEN(bb) < 4){
		syslog(0, "ppp", ": thwack: short packet\n");
		freeb(bb);
		return nil;
	}

	if(!uncs->active){
		netlog("unthwack: inactive, killing packet\n");
		freeb(bb);
		r = alloclcp(Lresetreq, uncs->resetid, 4, &m);
		hnputs(m->len, 4);
		*reply = r;
		return nil;
	}

	seq = bb->rptr[0] << 16;
	if((seq & ThwCompMask) == ThwUncomp){
		bb->rptr++;
		b = bb;
	}else{
		seq |= (bb->rptr[1]<<8) | bb->rptr[2];
		bb->rptr += 3;
		if((seq & ThwCompMask) == ThwCompressed){
			b = allocb(ThwMaxBlock);
			n = unthwack(&uncs->ut, b->wptr, ThwMaxBlock, bb->rptr, BLEN(bb), seq & ThwSeqMask);
			freeb(bb);
			if(n < 2){
				syslog(0, "ppp", ": unthwack: short or corrupted packet %d seq=%ld\n", n, seq);
				netlog("unthwack: short or corrupted packet n=%d seq=%ld: %s\n", n, seq, uncs->ut.err);
				freeb(b);

				r = alloclcp(Lresetreq, ++uncs->resetid, 4, &m);
				hnputs(m->len, 4);
				*reply = r;
				uncs->active = 0;
				return nil;
			}
			b->wptr += n;
		}else{
			unthwackadd(&uncs->ut, bb->rptr, BLEN(bb), seq & ThwSeqMask);
			b = bb;
		}

		/*
		 * update ack state
		 */
		mseq = unthwackstate(&uncs->ut, &mask);
		qlock(&uncs->ackl);
		uncs->ackseq = mseq;
		uncs->ackmask = mask;
		uncs->doack = 1;
		qunlock(&uncs->ackl);
	}

	/*
	 * grab the compressed protocol field
	 */
	proto = *b->rptr++;
	if((proto & 1) == 0)
		proto = (proto << 8) | *b->rptr++;
	*protop = proto;

	/*
	 * decode the ack, and forward to compressor
	 */
	if(seq & ThwAcked){
		if(ppp->ctype == &cthwack){
			cs = ppp->cstate;
			mseq = (b->rptr[0]<<16) | (b->rptr[1]<<8) | b->rptr[2];
			mask = b->rptr[3];
			thwackack(&cs->th, mseq, mask);
		}
		b->rptr += 4;
	}
	return b;
}