aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/ip/snoopy/ospf.c
blob: d5871dd9b0ab0ac96f2fe245314a0664ffcdf2a3 (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
#include <u.h>
#include <libc.h>
#include <ip.h>
#include <libsec.h>
#include "dat.h"
#include "protos.h"


/*
 *  OSPF packets
 */
typedef struct Ospfpkt	Ospfpkt;
struct Ospfpkt
{
	uchar	version;
	uchar	type;
	uchar	length[2];
	uchar	router[4];
	uchar	area[4];
	uchar	sum[2];
	uchar	autype[2];
	uchar	auth[8];
	uchar	data[1];
};
#define OSPF_HDRSIZE	24

enum
{
	OSPFhello=	1,
	OSPFdd=		2,
	OSPFlsrequest=	3,
	OSPFlsupdate=	4,
	OSPFlsack=	5
};


char *ospftype[] = {
	[OSPFhello]	"hello",
	[OSPFdd]	"data definition",
	[OSPFlsrequest]	"link state request",
	[OSPFlsupdate]	"link state update",
	[OSPFlsack]	"link state ack"
};

char*
ospfpkttype(int x)
{
	static char type[16];

	if(x > 0 && x <= OSPFlsack)
		return ospftype[x];
	sprint(type, "type %d", x);
	return type;
}

char*
ospfauth(Ospfpkt *ospf)
{
	static char auth[100];

	switch(ospf->type){
	case 0:
		return "no authentication";
	case 1:
		sprint(auth, "password(%8.8ux %8.8ux)", NetL(ospf->auth),
			NetL(ospf->auth+4));
		break;
	case 2:
		sprint(auth, "crypto(plen %d id %d dlen %d)", NetS(ospf->auth),
			ospf->auth[2], ospf->auth[3]);
		break;
	default:
		sprint(auth, "auth%d(%8.8ux %8.8ux)", NetS(ospf->autype), NetL(ospf->auth),
			NetL(ospf->auth+4));
	}
	return auth;
}

typedef struct Ospfhello	Ospfhello;
struct Ospfhello
{
	uchar	mask[4];
	uchar	interval[2];
	uchar	options;
	uchar	pri;
	uchar	deadint[4];
	uchar	designated[4];
	uchar	bdesignated[4];
	uchar	neighbor[1];
};

char*
seprintospfhello(char *p, char *e, void *a)
{
	Ospfhello *h = a;

	return seprint(p, e, "%s(mask %V interval %d opt %ux pri %ux deadt %d designated %V bdesignated %V)",
		ospftype[OSPFhello],
		h->mask, NetS(h->interval), h->options, h->pri,
		NetL(h->deadint), h->designated, h->bdesignated);
}

enum
{
	LSARouter=	1,
	LSANetwork=	2,
	LSASummN=	3,
	LSASummR=	4,
	LSAASext=	5
};


char *lsatype[] = {
	[LSARouter]	"Router LSA",
	[LSANetwork]	"Network LSA",
	[LSASummN]	"Summary LSA (Network)",
	[LSASummR]	"Summary LSA (Router)",
	[LSAASext]	"LSA AS external"
};

char*
lsapkttype(int x)
{
	static char type[16];

	if(x > 0 && x <= LSAASext)
		return lsatype[x];
	sprint(type, "type %d", x);
	return type;
}

/* OSPF Link State Advertisement Header */
/* rfc2178 section 12.1 */
/* data of Ospfpkt point to a 4-uchar value that is the # of LSAs */
struct OspfLSAhdr {
	uchar	lsage[2];
	uchar	options;	/* 0x2=stub area, 0x1=TOS routing capable */

	uchar	lstype;	/* 1=Router-LSAs
						 * 2=Network-LSAs
						 * 3=Summary-LSAs (to network)
						 * 4=Summary-LSAs (to AS boundary routers)
						 * 5=AS-External-LSAs
						 */
	uchar	lsid[4];
	uchar	advtrt[4];

	uchar	lsseqno[4];
	uchar	lscksum[2];
	uchar	lsalen[2];	/* includes the 20 byte lsa header */
};

struct Ospfrt {
	uchar	linkid[4];
	uchar	linkdata[4];
	uchar	typ;
	uchar	numtos;
	uchar	metric[2];

};

struct OspfrtLSA {
	struct OspfLSAhdr	hdr;
	uchar			netmask[4];
};

struct OspfntLSA {
	struct OspfLSAhdr	hdr;
	uchar			netmask[4];
	uchar			attrt[4];
};

/* Summary Link State Advertisement info */
struct Ospfsumm {
	uchar	flag;	/* always zero */
	uchar	metric[3];
};

struct OspfsummLSA {
	struct OspfLSAhdr	hdr;
	uchar			netmask[4];
	struct Ospfsumm		lsa;
};

/* AS external Link State Advertisement info */
struct OspfASext {
	uchar	flag;	/* external */
	uchar	metric[3];
	uchar	fwdaddr[4];
	uchar	exrttag[4];
};

struct OspfASextLSA {
	struct OspfLSAhdr	hdr;
	uchar			netmask[4];
	struct OspfASext	lsa;
};

/* OSPF Link State Update Packet */
struct OspfLSupdpkt {
	uchar	lsacnt[4];
	union {
		uchar			hdr[1];
		struct OspfrtLSA	rt[1];
		struct OspfntLSA	nt[1];
		struct OspfsummLSA	sum[1];
		struct OspfASextLSA	as[1];
	};
};

char*
seprintospflsaheader(char *p, char *e, struct OspfLSAhdr *h)
{
	return seprint(p, e, "age %d opt %ux type %ux lsid %V adv_rt %V seqno %ux c %4.4ux l %d",
		NetS(h->lsage), h->options&0xff, h->lstype,
		h->lsid, h->advtrt, NetL(h->lsseqno), NetS(h->lscksum),
		NetS(h->lsalen));
}

/* OSPF Database Description Packet */
struct OspfDDpkt {
	uchar	intMTU[2];
	uchar	options;
	uchar	bits;
	uchar	DDseqno[4];
	struct OspfLSAhdr	hdr[1];		/* LSA headers... */
};

char*
seprintospfdatadesc(char *p, char *e, void *a, int len)
{
	int nlsa, i;
	struct OspfDDpkt *g;

	g = (struct OspfDDpkt *)a;
	nlsa = len/sizeof(struct OspfLSAhdr);
	for (i=0; i<nlsa; i++) {
		p = seprint(p, e, "lsa%d(", i);
		p = seprintospflsaheader(p, e, &(g->hdr[i]));
		p = seprint(p, e, ")");
	}
	return seprint(p, e, ")");
}

char*
seprintospflsupdate(char *p, char *e, void *a, int len)
{
	int nlsa, i;
	struct OspfLSupdpkt *g;
	struct OspfLSAhdr *h;

	g = (struct OspfLSupdpkt *)a;
	nlsa = NetL(g->lsacnt);
	h = (struct OspfLSAhdr *)(g->hdr);
	p = seprint(p, e, "%d-%s(", nlsa, ospfpkttype(OSPFlsupdate));

	switch(h->lstype) {
	case LSARouter:
		{
/*			struct OspfrtLSA *h;
 */
		}
		break;
	case LSANetwork:
		{
			struct OspfntLSA *h;

			for (i=0; i<nlsa; i++) {
				h = &(g->nt[i]);
				p = seprint(p, e, "lsa%d(", i);
				p = seprintospflsaheader(p, e, &(h->hdr));
				p = seprint(p, e, " mask %V attrt %V)",
					h->netmask, h->attrt);
			}
		}
		break;
	case LSASummN:
	case LSASummR:
		{
			struct OspfsummLSA *h;

			for (i=0; i<nlsa; i++) {
				h = &(g->sum[i]);
				p = seprint(p, e, "lsa%d(", i);
				p = seprintospflsaheader(p, e, &(h->hdr));
				p = seprint(p, e, " mask %V met %d)",
					h->netmask, Net3(h->lsa.metric));
			}
		}
		break;
	case LSAASext:
		{
			struct OspfASextLSA *h;

			for (i=0; i<nlsa; i++) {
				h = &(g->as[i]);
				p = seprint(p, e, " lsa%d(", i);
				p = seprintospflsaheader(p, e, &(h->hdr));
				p = seprint(p, e, " mask %V extflg %1.1ux met %d fwdaddr %V extrtflg %ux)",
					h->netmask, h->lsa.flag, Net3(h->lsa.metric),
					h->lsa.fwdaddr, NetL(h->lsa.exrttag));
			}
		}
		break;
	default:
		p = seprint(p, e, "Not an LS update, lstype %d ", h->lstype);
		p = seprint(p, e, " %.*H", len>64?64:len, a);
		break;
	}
	return seprint(p, e, ")");
}

char*
seprintospflsack(char *p, char *e, void *a, int len)
{
	int nlsa, i;
	struct OspfLSAhdr *h;

	h = (struct OspfLSAhdr *)a;
	nlsa = len/sizeof(struct OspfLSAhdr);
	p = seprint(p, e, "%d-%s(", nlsa, ospfpkttype(OSPFlsack));
	for (i=0; i<nlsa; i++) {
		p = seprint(p, e, " lsa%d(", i);
		p = seprintospflsaheader(p, e, &(h[i]));
		p = seprint(p, e, ")");
	}
	return seprint(p, e, ")");
}

int
p_seprint(Msg *m)
{
	Ospfpkt *ospf;
	int len, x;
	char *p, *e;

	len = m->pe - m->ps;
	if(len < OSPF_HDRSIZE)
		return -1;
	p = m->p;
	e = m->e;

	/* adjust packet size */
	ospf = (Ospfpkt*)m->ps;
	x = NetS(ospf->length);
	if(x < len)
		return -1;
	x -= OSPF_HDRSIZE;

	p = seprint(p, e, "ver=%d type=%d len=%d r=%V a=%V c=%4.4ux %s ",
		ospf->version, ospf->type, x,
		ospf->router, ospf->area, NetS(ospf->sum),
		ospfauth(ospf));

	switch (ospf->type) {
	case OSPFhello:
		p = seprintospfhello(p, e, ospf->data);
		break;
	case OSPFdd:
		p = seprintospfdatadesc(p, e, ospf->data, x);
		break;
	case OSPFlsrequest:
		p = seprint(p, e, " %s->", ospfpkttype(ospf->type));
		goto Default;
	case OSPFlsupdate:
		p = seprintospflsupdate(p, e, ospf->data, x);
		break;
	case OSPFlsack:
		p = seprintospflsack(p, e, ospf->data, x);
		break;
	default:
Default:
		p = seprint(p, e, " data=%.*H", x>64?64:x, ospf->data);
	}
	m->p = p;
	m->pr = nil;
	return 0;
}

Proto ospf =
{
	"ospf",
	nil,
	nil,
	p_seprint,
	nil,
	nil,
	nil,
	defaultframer
};