~kris/9p

9hist

ref: 9f49b60d534e3ba63fa64a9e6c3565aeaa189f21 9hist/port/stnoether.c -rw-r--r-- 5.7 KiB
9f49b60d — David du Colombier Plan 9 from Bell Labs 1991-11-09 34 years ago
                                                                                
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
/*
 *  ethernet specific multiplexor for nonet
 *
 *  this line discipline gets pushed onto an ethernet channel
 *  to demultiplex/multiplex nonet conversations.
 */
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"io.h"
#include	"errno.h"
#include	"../port/nonet.h"

#define DPRINT if(pnonet)print
extern int pnonet;

static void	etherparse(uchar*, char*);
static void	noetherclose(Queue*);
static void	noetheriput(Queue*, Block*);
static void	noetheropen(Queue*, Stream*);
static void	noetheroput(Queue*, Block*);

/*
 *  ethernet header of a packet
 */
#define EHDRSIZE 	(ETHERHDRSIZE + NO_HDRSIZE)
#define EMAXBODY	(ETHERMAXTU - EHDRSIZE)	/* maximum ethernet packet body */
#define ETHER_TYPE	0x900	/* most significant byte last */

/*
 *  the ethernet multiplexor stream module definition
 */
Qinfo noetherinfo =
{
	noetheriput,
	noetheroput,
	noetheropen,
	noetherclose,
	"noether"
};

/*
 *  perform the ether specific part of nonetconnect.  just stick
 *  the address into the prototype header.
 */
void
noetherconnect(Noconv *cp, char *ea)
{
	Etherpkt *eh;

	eh = (Etherpkt*)cp->media->rptr;
	etherparse(eh->d, ea);
	eh->type[0] = ETHER_TYPE>>8;
	eh->type[1] = ETHER_TYPE & 0xff;
}

/*
 *  set up an ether interface
 */
static void
noetheropen(Queue *q, Stream *s)
{
	streamenter(s);
	nonetnewifc(q, s, ETHERMAXTU, ETHERMINTU, ETHERHDRSIZE, noetherconnect);
}

/*
 *  tear down an ether interface
 */
static void
noetherclose(Queue *q)
{
	Noifc *ifc;

	ifc = (Noifc*)q->ptr;
	nonetfreeifc(ifc);
}

/*
 *  configure the system
 */
static void
noetheroput(Queue *q, Block *bp)
{
	Noifc *ifc;

	ifc = (Noifc*)q->ptr;
	if(bp->type != M_DATA){
		if(streamparse("config", bp)){
			if(*bp->rptr == 0)
				strcpy(ifc->name, "nonet");
			else
				strncpy(ifc->name, (char *)bp->rptr, sizeof(ifc->name));
		} else
			PUTNEXT(q, bp);
		return;
	}

	PUTNEXT(q, bp);
}

/*
 *  respond to a misaddressed message with a close
 */
void
noetherbad(Noifc *ifc, Block *bp, int circuit)
{
	Etherpkt *eh, *neh;
	Nohdr *nh, *nnh;
	Block *nbp;
	int r;
	Noconv *cp, *ep;

	/*
	 *  crack the packet header
	 */
	eh = (Etherpkt*)bp->rptr;
	nh = (Nohdr*)eh->data;
/*	print("bad %.2ux%.2ux%.2ux%.2ux%.2ux%.2ux c %d m %d f %d\n",
		eh->s[0], eh->s[1], eh->s[2], eh->s[3], eh->s[4],
		eh->s[5], circuit, nh->mid, nh->flag); /**/
	if(nh->flag & NO_RESET)
		goto out;

	/*
	 *  only one reset per message
	 */
	r = (nh->remain[1]<<8) | nh->remain[0];
	if(r<0)
		goto out;

	/*
	 *  craft an error reply
	 */
/*	print("sending reset\n"); /**/
	nbp = allocb(60);
	nbp->flags |= S_DELIM;
	nbp->wptr = nbp->rptr + 60;
	memset(bp->rptr, 0, 60);
	neh = (Etherpkt *)nbp->rptr;
	nnh = (Nohdr*)neh->data;
	memmove(neh, eh, EHDRSIZE);
	nnh->circuit[0] ^= 1;
	nnh->remain[0] = nnh->remain[1] = 0;
	nnh->flag = NO_HANGUP | NO_RESET;
	nnh->ack = nh->mid;
	nnh->mid = nh->ack;
	memmove(neh->s, eh->d, sizeof(neh->s));
	memmove(neh->d, eh->s, sizeof(neh->d));
	nonetcksum(nbp, ETHERHDRSIZE);
	PUTNEXT(ifc->wq, nbp);
out:
	freeb(bp);
}

/*
 *  Input a packet and use the ether address to select the correct
 *  nonet device to pass it to.
 *
 *  Simplifying assumption:  one put == one packet && the complete header
 *	is in the first block.  If this isn't true, demultiplexing will not work.
 */
static void
noetheriput(Queue *q, Block *bp)
{
	Noifc *ifc;
	int circuit;
	Noconv *cp, *ep;
	Etherpkt *eh, *peh;
	Nohdr *nh;
	ulong s;
	Block *nbp;
	int next;
	Nocall *clp;

	if(bp->type != M_DATA){
		PUTNEXT(q, bp);
		return;
	}

	ifc = (Noifc*)q->ptr;
	eh = (Etherpkt*)bp->rptr;
	nh = (Nohdr*)eh->data;
	circuit = (nh->circuit[2]<<16) | (nh->circuit[1]<<8) | nh->circuit[0];
	s = (nh->sum[1]<<8) | nh->sum[0];
	if(s && s!=nonetcksum(bp, ETHERHDRSIZE)){
/*		print("checksum error %ux %ux\n", s, (nh->sum[1]<<8) | nh->sum[0]); /**/
		freeb(bp);
		return;
	}

	/*
	 *  look for an existing circuit.
	 */
	ep = &ifc->conv[conf.nnoconv];
	for(cp = &ifc->conv[0]; cp < ep; cp++){
		nbp = cp->media;
		if(nbp == 0)
			continue;
		peh = (Etherpkt*)nbp->rptr;
		if(circuit==cp->rcvcircuit && memcmp(peh->d, eh->s, sizeof(eh->s))==0){
			if(!canqlock(cp)){
				freeb(bp);
				return;
			}
			peh = (Etherpkt*)nbp->rptr;
			if(circuit==cp->rcvcircuit
			&& memcmp(peh->d, eh->s, sizeof(eh->s))==0){
				bp->rptr += ifc->hsize;
				nonetrcvmsg(cp, bp);
				qunlock(cp);
				return;
			}
			qunlock(cp);
		}
	}

	/*
	 *  if not a new call, then its misaddressed
	 */
	if((nh->flag & NO_NEWCALL) == 0){
		noetherbad(ifc, bp, circuit);
		return;
	}

	/*
	 *  Queue call in a circular queue and wakeup a listener.
	 */
	DPRINT("call in\n");
	lock(&ifc->lock);
	next = (ifc->wptr + 1) % Nnocalls;
	if(next == ifc->rptr){
		/* no room in the queue */
		unlock(&ifc->lock);
		freeb(bp);
		return;
	}
	clp = &ifc->call[ifc->wptr];
	sprint(clp->raddr, "%.2ux%.2ux%.2ux%.2ux%.2ux%.2ux",
		eh->s[0], eh->s[1], eh->s[2], eh->s[3], eh->s[4], eh->s[5]);
	clp->circuit = circuit^1;
	bp->rptr += ifc->hsize;
	clp->msg = bp;
	ifc->wptr = next;
	unlock(&ifc->lock);
	wakeup(&ifc->listenr);
}

/*
 *  parse an ethernet address (assumed to be 12 ascii hex digits)
 */
static void
etherparse(uchar *to, char *from)
{
	ulong ip;
	uchar nip[4];
	int tdig;
	int fdig;
	int i;

#ifdef MAGNUM
	/* Dot means ip address */
	if(strchr(from, '.')) {
		ip = ipparse(from);
		if(ip == 0)
			error(Ebadnet);

		hnputl(nip, ip);			
		if(arp_lookup(nip, to))
			return;
	}
#endif

	if(strlen(from) != 12)
		error(Ebadnet);

	for(i = 0; i < 6; i++){
		fdig = (*from++)&0x7f;
		tdig = fdig >= 'a' ? ((fdig - 'a') + 10)
				: (fdig >= 'A' ? ((fdig - 'A') + 10) : (fdig - '0'));
		fdig = (*from++)&0x7f;
		tdig <<= 4;
		tdig |= fdig >= 'a' ? ((fdig - 'a') + 10)
				: (fdig >= 'A' ? ((fdig - 'A') + 10) : (fdig - '0'));
		*to++ = tdig;
	}
}