xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_ospf.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <ctype.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include <fcntl.h>
33*0Sstevel@tonic-gate #include <string.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/time.h>
36*0Sstevel@tonic-gate #include <sys/socket.h>
37*0Sstevel@tonic-gate #include <sys/sockio.h>
38*0Sstevel@tonic-gate #include <net/if.h>
39*0Sstevel@tonic-gate #include <netinet/in_systm.h>
40*0Sstevel@tonic-gate #include <netinet/in.h>
41*0Sstevel@tonic-gate #include <netinet/ip.h>
42*0Sstevel@tonic-gate #include <netinet/if_ether.h>
43*0Sstevel@tonic-gate #include <arpa/inet.h>
44*0Sstevel@tonic-gate #include "snoop.h"
45*0Sstevel@tonic-gate #include "snoop_ospf.h"
46*0Sstevel@tonic-gate #include "snoop_ospf6.h"
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate extern char *dlc_header;
49*0Sstevel@tonic-gate static char *sum_line;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate char *ospf_types[] = {
52*0Sstevel@tonic-gate 	"umd", 		/* 0 */
53*0Sstevel@tonic-gate 	"Hello",	/* 1 */
54*0Sstevel@tonic-gate 	"DD",		/* 2 */
55*0Sstevel@tonic-gate 	"LSReq",	/* 3 */
56*0Sstevel@tonic-gate 	"LSUpd",	/* 4 */
57*0Sstevel@tonic-gate 	"LSAck",	/* 5 */
58*0Sstevel@tonic-gate };
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate static char *ospf_authtypes[] = {
61*0Sstevel@tonic-gate 	"None", 	/* 0 */
62*0Sstevel@tonic-gate 	"simple",	/* 1 */
63*0Sstevel@tonic-gate 	"md5",		/* 2 */
64*0Sstevel@tonic-gate };
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate const struct bits ospf_rla_flag_bits[] = {
67*0Sstevel@tonic-gate 	{ RLA_FLAG_B,		"B" },
68*0Sstevel@tonic-gate 	{ RLA_FLAG_E,		"E" },
69*0Sstevel@tonic-gate 	{ RLA_FLAG_V,		"V" },
70*0Sstevel@tonic-gate 	{ RLA_FLAG_W,		"W" },
71*0Sstevel@tonic-gate 	{ 0, 			NULL }
72*0Sstevel@tonic-gate };
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate const struct bits ospf_db_flags_bits[] = {
75*0Sstevel@tonic-gate 	{ OSPF_DB_INIT,		"I" },
76*0Sstevel@tonic-gate 	{ OSPF_DB_MORE,		"M" },
77*0Sstevel@tonic-gate 	{ OSPF_DB_MASTER,	"MS" },
78*0Sstevel@tonic-gate 	{ 0, 			NULL }
79*0Sstevel@tonic-gate };
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate const struct bits ospf_option_bits[] = {
82*0Sstevel@tonic-gate 	{ OSPF_OPTION_T,	"T" },
83*0Sstevel@tonic-gate 	{ OSPF_OPTION_E,	"E" },
84*0Sstevel@tonic-gate 	{ OSPF_OPTION_MC,	"MC" },
85*0Sstevel@tonic-gate 	{ 0,			NULL }
86*0Sstevel@tonic-gate };
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate static int interpret_ospf_hello(int, struct ospfhdr *, int);
89*0Sstevel@tonic-gate static void ospf_print_ls_type(int, uint32_t, struct in_addr, struct in_addr);
90*0Sstevel@tonic-gate static void interpret_ospf_lsa_hdr(int, struct lsa_hdr *);
91*0Sstevel@tonic-gate static int interpret_ospf_lsa(int flags, struct lsa *lsa, uchar_t *);
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate char *
ospf_print_bits(const struct bits * bp,uchar_t options)94*0Sstevel@tonic-gate ospf_print_bits(const struct bits *bp, uchar_t options)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	static char bitstring[32];
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	bitstring[0] = '\0';
99*0Sstevel@tonic-gate 	do {
100*0Sstevel@tonic-gate 		if (options & bp->bit) {
101*0Sstevel@tonic-gate 			strcat(bitstring, bp->str);
102*0Sstevel@tonic-gate 			strcat(bitstring, "/");
103*0Sstevel@tonic-gate 		}
104*0Sstevel@tonic-gate 	} while ((++bp)->bit);
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	/* wipe out the trailing "/" */
107*0Sstevel@tonic-gate 	bitstring[strlen(bitstring) - 1] = '\0';
108*0Sstevel@tonic-gate 	return (bitstring);
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate char *
ospf_print_lsa_age(long age)112*0Sstevel@tonic-gate ospf_print_lsa_age(long age)
113*0Sstevel@tonic-gate {
114*0Sstevel@tonic-gate 	long sec, mins, hour;
115*0Sstevel@tonic-gate 	static char lsa_age[16];
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	sec = age % 60;
118*0Sstevel@tonic-gate 	mins = (age / 60) % 60;
119*0Sstevel@tonic-gate 	hour = age / 3600;
120*0Sstevel@tonic-gate 	if (hour != 0)
121*0Sstevel@tonic-gate 		snprintf(lsa_age, sizeof (lsa_age), "%u:%02u:%02u",
122*0Sstevel@tonic-gate 		    hour, mins, sec);
123*0Sstevel@tonic-gate 	else if (mins != 0)
124*0Sstevel@tonic-gate 		snprintf(lsa_age, sizeof (lsa_age), "%u:%02u", mins, sec);
125*0Sstevel@tonic-gate 	else
126*0Sstevel@tonic-gate 		snprintf(lsa_age, sizeof (lsa_age), "%u", sec);
127*0Sstevel@tonic-gate 	return (lsa_age);
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate static int
interpret_ospf_hello(int flags,struct ospfhdr * op,int fraglen)131*0Sstevel@tonic-gate interpret_ospf_hello(int flags, struct ospfhdr *op, int fraglen)
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate 	struct in_addr *nbr;
134*0Sstevel@tonic-gate 	int j;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if (fraglen < OSPF_MIN_HEADER_SIZE + OSPF_MIN_HELLO_HEADER_SIZE)
137*0Sstevel@tonic-gate 		return (-1); /* truncated packet */
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	if (flags & F_SUM) {
140*0Sstevel@tonic-gate 		if (op->ospf_hello.hello_dr.s_addr != 0) {
141*0Sstevel@tonic-gate 			(void) sprintf(sum_line, "DR=%s ",
142*0Sstevel@tonic-gate 			    inet_ntoa(op->ospf_hello.hello_dr));
143*0Sstevel@tonic-gate 		}
144*0Sstevel@tonic-gate 		sum_line += strlen(sum_line);
145*0Sstevel@tonic-gate 		if (op->ospf_hello.hello_bdr.s_addr != 0) {
146*0Sstevel@tonic-gate 			(void) sprintf(sum_line, "BDR=%s ",
147*0Sstevel@tonic-gate 			    inet_ntoa(op->ospf_hello.hello_bdr));
148*0Sstevel@tonic-gate 		}
149*0Sstevel@tonic-gate 		sum_line += strlen(sum_line);
150*0Sstevel@tonic-gate 		nbr = op->ospf_hello.hello_neighbor;
151*0Sstevel@tonic-gate 		j = 0;
152*0Sstevel@tonic-gate 		while ((uchar_t *)nbr < ((uchar_t *)op + fraglen)) {
153*0Sstevel@tonic-gate 			if ((uchar_t *)nbr + sizeof (struct in_addr) >
154*0Sstevel@tonic-gate 			    ((uchar_t *)op + fraglen))
155*0Sstevel@tonic-gate 				return (-1); /* truncated */
156*0Sstevel@tonic-gate 			j++;
157*0Sstevel@tonic-gate 			++nbr;
158*0Sstevel@tonic-gate 		}
159*0Sstevel@tonic-gate 		(void) sprintf(sum_line, "%d nbrs", j);
160*0Sstevel@tonic-gate 		sum_line += strlen(sum_line);
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	}
163*0Sstevel@tonic-gate 	if (flags & F_DTAIL) {
164*0Sstevel@tonic-gate 		show_header("OSPF HELLO:  ", "Hello Packet",
165*0Sstevel@tonic-gate 		    ntohs(op->ospf_len));
166*0Sstevel@tonic-gate 		show_space();
167*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
168*0Sstevel@tonic-gate 		    "Options = %s", ospf_print_bits(ospf_option_bits,
169*0Sstevel@tonic-gate 		    op->ospf_hello.hello_options));
170*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(), "Mask = %s",
171*0Sstevel@tonic-gate 		    inet_ntoa(op->ospf_hello.hello_mask));
172*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
173*0Sstevel@tonic-gate 		    "Hello interval = %d",
174*0Sstevel@tonic-gate 		    ntohs(op->ospf_hello.hello_helloint));
175*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
176*0Sstevel@tonic-gate 		    "Priority = %d", op->ospf_hello.hello_priority);
177*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
178*0Sstevel@tonic-gate 		    "Dead interval = %u", ntohl(op->ospf_hello.hello_deadint));
179*0Sstevel@tonic-gate 		if (op->ospf_hello.hello_dr.s_addr != 0) {
180*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
181*0Sstevel@tonic-gate 			    "Designated Router = %s",
182*0Sstevel@tonic-gate 			    inet_ntoa(op->ospf_hello.hello_dr));
183*0Sstevel@tonic-gate 		}
184*0Sstevel@tonic-gate 		if (op->ospf_hello.hello_bdr.s_addr != 0) {
185*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
186*0Sstevel@tonic-gate 			    "Backup Designated Router = %s",
187*0Sstevel@tonic-gate 			    inet_ntoa(op->ospf_hello.hello_bdr));
188*0Sstevel@tonic-gate 		}
189*0Sstevel@tonic-gate 		nbr = op->ospf_hello.hello_neighbor;
190*0Sstevel@tonic-gate 		while ((uchar_t *)nbr < ((uchar_t *)op + fraglen)) {
191*0Sstevel@tonic-gate 			if ((uchar_t *)nbr + sizeof (struct in_addr) >
192*0Sstevel@tonic-gate 			    ((uchar_t *)op + fraglen))
193*0Sstevel@tonic-gate 				return (-1); /* truncated */
194*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
195*0Sstevel@tonic-gate 			    "Neighbor: %s", inet_ntoa(*nbr));
196*0Sstevel@tonic-gate 			++nbr;
197*0Sstevel@tonic-gate 		}
198*0Sstevel@tonic-gate 	}
199*0Sstevel@tonic-gate 	return (fraglen);
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate static void
ospf_print_ls_type(int flags,uint32_t ls_type,struct in_addr ls_stateid,struct in_addr ls_router)203*0Sstevel@tonic-gate ospf_print_ls_type(int flags, uint32_t ls_type, struct in_addr ls_stateid,
204*0Sstevel@tonic-gate     struct in_addr ls_router)
205*0Sstevel@tonic-gate {
206*0Sstevel@tonic-gate 	switch (ls_type) {
207*0Sstevel@tonic-gate 	case LS_TYPE_ROUTER:
208*0Sstevel@tonic-gate 		if (flags & F_SUM) {
209*0Sstevel@tonic-gate 			sprintf(sum_line, " rtr %s ", inet_ntoa(ls_router));
210*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
211*0Sstevel@tonic-gate 		}
212*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
213*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
214*0Sstevel@tonic-gate 			    "Router LSA; Router = %s ", inet_ntoa(ls_router));
215*0Sstevel@tonic-gate 		}
216*0Sstevel@tonic-gate 		break;
217*0Sstevel@tonic-gate 	case LS_TYPE_NETWORK:
218*0Sstevel@tonic-gate 		if (flags & F_SUM) {
219*0Sstevel@tonic-gate 			sprintf(sum_line, " net dr %s ", inet_ntoa(ls_router));
220*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
221*0Sstevel@tonic-gate 			sprintf(sum_line, "if %s ", inet_ntoa(ls_stateid));
222*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
223*0Sstevel@tonic-gate 		}
224*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
225*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
226*0Sstevel@tonic-gate 			    "Network LSA Router = %s ", inet_ntoa(ls_router));
227*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
228*0Sstevel@tonic-gate 			    "            Interface = %s ",
229*0Sstevel@tonic-gate 			    inet_ntoa(ls_stateid));
230*0Sstevel@tonic-gate 		}
231*0Sstevel@tonic-gate 		break;
232*0Sstevel@tonic-gate 	case LS_TYPE_SUM_IP:
233*0Sstevel@tonic-gate 		if (flags & F_SUM) {
234*0Sstevel@tonic-gate 			sprintf(sum_line, " sum %s ", inet_ntoa(ls_stateid));
235*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
236*0Sstevel@tonic-gate 			sprintf(sum_line, "abr %s ", inet_ntoa(ls_router));
237*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
238*0Sstevel@tonic-gate 		}
239*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
240*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
241*0Sstevel@tonic-gate 			    "Summary LSA IP = %s ", inet_ntoa(ls_stateid));
242*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
243*0Sstevel@tonic-gate 			    "            Area Border Router = %s ",
244*0Sstevel@tonic-gate 			    inet_ntoa(ls_router));
245*0Sstevel@tonic-gate 		}
246*0Sstevel@tonic-gate 		break;
247*0Sstevel@tonic-gate 	case LS_TYPE_SUM_ABR:
248*0Sstevel@tonic-gate 		if (flags & F_SUM) {
249*0Sstevel@tonic-gate 			sprintf(sum_line, "abr %s ", inet_ntoa(ls_stateid));
250*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
251*0Sstevel@tonic-gate 			sprintf(sum_line, "asbr %s ", inet_ntoa(ls_router));
252*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
253*0Sstevel@tonic-gate 		}
254*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
255*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
256*0Sstevel@tonic-gate 			    "ASBR Summary abr = %s ", inet_ntoa(ls_stateid));
257*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
258*0Sstevel@tonic-gate 			    "             asbr = %s ", inet_ntoa(ls_router));
259*0Sstevel@tonic-gate 		}
260*0Sstevel@tonic-gate 		break;
261*0Sstevel@tonic-gate 	case LS_TYPE_ASE:
262*0Sstevel@tonic-gate 		if (flags & F_SUM) {
263*0Sstevel@tonic-gate 			sprintf(sum_line, " ase %s", inet_ntoa(ls_stateid));
264*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
265*0Sstevel@tonic-gate 			sprintf(sum_line, " asbr %s", inet_ntoa(ls_router));
266*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
267*0Sstevel@tonic-gate 		}
268*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
269*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
270*0Sstevel@tonic-gate 			    "AS External LSA ase = %s ", inet_ntoa(ls_stateid));
271*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
272*0Sstevel@tonic-gate 			    "                asbr = %s ", inet_ntoa(ls_router));
273*0Sstevel@tonic-gate 		}
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 		break;
276*0Sstevel@tonic-gate 	case LS_TYPE_GROUP:
277*0Sstevel@tonic-gate 		if (flags & F_SUM) {
278*0Sstevel@tonic-gate 			sprintf(sum_line, " group %s", inet_ntoa(ls_stateid));
279*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
280*0Sstevel@tonic-gate 			sprintf(sum_line, " rtr %s", inet_ntoa(ls_router));
281*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
282*0Sstevel@tonic-gate 		}
283*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
284*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
285*0Sstevel@tonic-gate 			    "Group LSA %s ", inet_ntoa(ls_stateid));
286*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
287*0Sstevel@tonic-gate 			    "          rtr = %s ", inet_ntoa(ls_router));
288*0Sstevel@tonic-gate 		}
289*0Sstevel@tonic-gate 		break;
290*0Sstevel@tonic-gate 	default:
291*0Sstevel@tonic-gate 		if (flags & F_SUM) {
292*0Sstevel@tonic-gate 			sprintf(sum_line, " unknown LSA type %d", ls_type);
293*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
294*0Sstevel@tonic-gate 		}
295*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
296*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
297*0Sstevel@tonic-gate 			    "Unknown LSA type %d", ls_type);
298*0Sstevel@tonic-gate 		}
299*0Sstevel@tonic-gate 		break;
300*0Sstevel@tonic-gate 	}
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate static void
interpret_ospf_lsa_hdr(int flags,struct lsa_hdr * lsah)304*0Sstevel@tonic-gate interpret_ospf_lsa_hdr(int flags, struct lsa_hdr *lsah)
305*0Sstevel@tonic-gate {
306*0Sstevel@tonic-gate 	if (flags & F_SUM)
307*0Sstevel@tonic-gate 		return;
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	if (flags & F_DTAIL) {
310*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
311*0Sstevel@tonic-gate 		    "Options = %s",
312*0Sstevel@tonic-gate 		    ospf_print_bits(ospf_option_bits, lsah->ls_options));
313*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
314*0Sstevel@tonic-gate 		    "Sequence = %X ", ntohl(lsah->ls_seq));
315*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
316*0Sstevel@tonic-gate 		    "Age = %X ", ospf_print_lsa_age(ntohs(lsah->ls_age)));
317*0Sstevel@tonic-gate 	}
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	ospf_print_ls_type(flags, lsah->ls_type, lsah->ls_stateid,
320*0Sstevel@tonic-gate 	    lsah->ls_router);
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate }
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate #define	TRUNC(addr)	((uchar_t *)(addr) > fragend)
325*0Sstevel@tonic-gate static int
interpret_ospf_lsa(int flags,struct lsa * lsa,uchar_t * fragend)326*0Sstevel@tonic-gate interpret_ospf_lsa(int flags, struct lsa *lsa, uchar_t *fragend)
327*0Sstevel@tonic-gate {
328*0Sstevel@tonic-gate 	uchar_t *ls_end;
329*0Sstevel@tonic-gate 	int rla_count, k;
330*0Sstevel@tonic-gate 	struct rlalink *rl;
331*0Sstevel@tonic-gate 	struct tos_metric *tosp;
332*0Sstevel@tonic-gate 	struct in_addr *addr;
333*0Sstevel@tonic-gate 	uint32_t *tosmetric;
334*0Sstevel@tonic-gate 	struct aslametric *am;
335*0Sstevel@tonic-gate 	uint32_t tm;
336*0Sstevel@tonic-gate 	int tos, metric;
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	interpret_ospf_lsa_hdr(flags, &lsa->ls_hdr);
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	ls_end = (uchar_t *)lsa + ntohs(lsa->ls_hdr.ls_length);
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	if (TRUNC(ls_end))
343*0Sstevel@tonic-gate 		return (-1);
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	switch (lsa->ls_hdr.ls_type) {
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	case LS_TYPE_ROUTER:
348*0Sstevel@tonic-gate 		if (TRUNC(&lsa->lsa_un.un_rla.rla_flags))
349*0Sstevel@tonic-gate 			return (-1);
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
352*0Sstevel@tonic-gate 			(void) ospf_print_bits(ospf_rla_flag_bits,
353*0Sstevel@tonic-gate 			    lsa->lsa_un.un_rla.rla_flags);
354*0Sstevel@tonic-gate 		}
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 		if (TRUNC(&lsa->lsa_un.un_rla.rla_count))
357*0Sstevel@tonic-gate 			return (-1);
358*0Sstevel@tonic-gate 		rla_count = ntohs(lsa->lsa_un.un_rla.rla_count);
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate 		rl = lsa->lsa_un.un_rla.rla_link;
361*0Sstevel@tonic-gate 		if (TRUNC(rl))
362*0Sstevel@tonic-gate 			return (-1);
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 		while (rla_count-- != 0) {
365*0Sstevel@tonic-gate 			if (TRUNC((uchar_t *)rl + sizeof (*rl)))
366*0Sstevel@tonic-gate 				return (-1);
367*0Sstevel@tonic-gate 			switch (rl->link_type) {
368*0Sstevel@tonic-gate 			case RLA_TYPE_VIRTUAL:
369*0Sstevel@tonic-gate 				if (flags & F_DTAIL) {
370*0Sstevel@tonic-gate 					(void) snprintf(get_line(0, 0),
371*0Sstevel@tonic-gate 					    get_line_remain(), "Virtual Link");
372*0Sstevel@tonic-gate 				}
373*0Sstevel@tonic-gate 				/* fall through */
374*0Sstevel@tonic-gate 			case RLA_TYPE_ROUTER:
375*0Sstevel@tonic-gate 				if (flags & F_DTAIL) {
376*0Sstevel@tonic-gate 					(void) snprintf(get_line(0, 0),
377*0Sstevel@tonic-gate 					    get_line_remain(), "Neighbor = %s",
378*0Sstevel@tonic-gate 					    inet_ntoa(rl->link_id));
379*0Sstevel@tonic-gate 					(void) snprintf(get_line(0, 0),
380*0Sstevel@tonic-gate 					    get_line_remain(), "Interface = %s",
381*0Sstevel@tonic-gate 					    inet_ntoa(rl->link_data));
382*0Sstevel@tonic-gate 				}
383*0Sstevel@tonic-gate 				break;
384*0Sstevel@tonic-gate 			case RLA_TYPE_TRANSIT:
385*0Sstevel@tonic-gate 				if (flags & F_DTAIL) {
386*0Sstevel@tonic-gate 					(void) snprintf(get_line(0, 0),
387*0Sstevel@tonic-gate 					    get_line_remain(),
388*0Sstevel@tonic-gate 					    "Designated Router = %s",
389*0Sstevel@tonic-gate 					    inet_ntoa(rl->link_id));
390*0Sstevel@tonic-gate 					(void) snprintf(get_line(0, 0),
391*0Sstevel@tonic-gate 					    get_line_remain(), "Interface = %s",
392*0Sstevel@tonic-gate 					    inet_ntoa(rl->link_data));
393*0Sstevel@tonic-gate 				}
394*0Sstevel@tonic-gate 				break;
395*0Sstevel@tonic-gate 			case RLA_TYPE_STUB:
396*0Sstevel@tonic-gate 				if (flags & F_DTAIL) {
397*0Sstevel@tonic-gate 					(void) snprintf(get_line(0, 0),
398*0Sstevel@tonic-gate 					    get_line_remain(), "Network = %s",
399*0Sstevel@tonic-gate 					    inet_ntoa(rl->link_id));
400*0Sstevel@tonic-gate 					(void) snprintf(get_line(0, 0),
401*0Sstevel@tonic-gate 					    get_line_remain(), "Mask = %s",
402*0Sstevel@tonic-gate 					    inet_ntoa(rl->link_data));
403*0Sstevel@tonic-gate 				}
404*0Sstevel@tonic-gate 				break;
405*0Sstevel@tonic-gate 			default:
406*0Sstevel@tonic-gate 				if (flags & F_DTAIL) {
407*0Sstevel@tonic-gate 					(void) snprintf(get_line(0, 0),
408*0Sstevel@tonic-gate 					    get_line_remain(),
409*0Sstevel@tonic-gate 					    "Unknown link type %d",
410*0Sstevel@tonic-gate 					    rl->link_type);
411*0Sstevel@tonic-gate 				}
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 			}
414*0Sstevel@tonic-gate 			if (flags & F_DTAIL) {
415*0Sstevel@tonic-gate 				(void) snprintf(get_line(0, 0),
416*0Sstevel@tonic-gate 				    get_line_remain(), "TOS 0 metric = %d",
417*0Sstevel@tonic-gate 				    ntohs(rl->link_tos0metric));
418*0Sstevel@tonic-gate 			}
419*0Sstevel@tonic-gate 			tosp = (struct tos_metric *)(
420*0Sstevel@tonic-gate 				(uchar_t *)rl + sizeof (rl->link_tos0metric));
421*0Sstevel@tonic-gate 			for (k = 0; k > (int)rl->link_toscount; ++k, ++tosp) {
422*0Sstevel@tonic-gate 				if (TRUNC(tosp))
423*0Sstevel@tonic-gate 					return (-1);
424*0Sstevel@tonic-gate 				if (flags & F_DTAIL) {
425*0Sstevel@tonic-gate 					(void) snprintf(get_line(0, 0),
426*0Sstevel@tonic-gate 					    get_line_remain(),
427*0Sstevel@tonic-gate 					    "TOS %d metric = %d",
428*0Sstevel@tonic-gate 					    tosp->tos_type,
429*0Sstevel@tonic-gate 					    ntohs(tosp->tos_metric));
430*0Sstevel@tonic-gate 				}
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 			}
433*0Sstevel@tonic-gate 			rl = (struct rlalink *)((uchar_t *)(rl + 1) +
434*0Sstevel@tonic-gate 			    ((rl->link_toscount) * sizeof (*tosp)));
435*0Sstevel@tonic-gate 			if (TRUNC(rl))
436*0Sstevel@tonic-gate 				return (-1); /* truncated */
437*0Sstevel@tonic-gate 		}
438*0Sstevel@tonic-gate 		break;
439*0Sstevel@tonic-gate 	case LS_TYPE_NETWORK:
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 		if (TRUNC(&lsa->lsa_un.un_nla.nla_mask))
442*0Sstevel@tonic-gate 			return (-1);
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
445*0Sstevel@tonic-gate 			snprintf(get_line(0, 0), get_line_remain(),
446*0Sstevel@tonic-gate 			    "Mask = %s",
447*0Sstevel@tonic-gate 			    inet_ntoa(lsa->lsa_un.un_nla.nla_mask));
448*0Sstevel@tonic-gate 			snprintf(get_line(0, 0), get_line_remain(),
449*0Sstevel@tonic-gate 			    "Routers:");
450*0Sstevel@tonic-gate 		}
451*0Sstevel@tonic-gate 		addr = lsa->lsa_un.un_nla.nla_router;
452*0Sstevel@tonic-gate 		while ((uchar_t *)addr < ls_end) {
453*0Sstevel@tonic-gate 			if ((uchar_t *)addr + sizeof (struct in_addr) > ls_end)
454*0Sstevel@tonic-gate 				return (-1); /* truncated */
455*0Sstevel@tonic-gate 			if (flags & F_DTAIL) {
456*0Sstevel@tonic-gate 				snprintf(get_line(0, 0), get_line_remain(),
457*0Sstevel@tonic-gate 				    "\t%s", inet_ntoa(*addr));
458*0Sstevel@tonic-gate 			}
459*0Sstevel@tonic-gate 			++addr;
460*0Sstevel@tonic-gate 		}
461*0Sstevel@tonic-gate 		break;
462*0Sstevel@tonic-gate 	case LS_TYPE_SUM_IP:
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 		if (TRUNC((uchar_t *)&lsa->lsa_un.un_sla.sla_mask +
465*0Sstevel@tonic-gate 		    sizeof (struct in_addr)))
466*0Sstevel@tonic-gate 			return (-1);
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
469*0Sstevel@tonic-gate 			snprintf(get_line(0, 0), get_line_remain(), "Mask = %s",
470*0Sstevel@tonic-gate 			    inet_ntoa(lsa->lsa_un.un_sla.sla_mask));
471*0Sstevel@tonic-gate 		}
472*0Sstevel@tonic-gate 		/* FALLTHROUGH */
473*0Sstevel@tonic-gate 	case LS_TYPE_SUM_ABR:
474*0Sstevel@tonic-gate 		if (TRUNC(&lsa->lsa_un.un_sla.sla_tosmetric))
475*0Sstevel@tonic-gate 			return (-1);
476*0Sstevel@tonic-gate 		tosmetric = lsa->lsa_un.un_sla.sla_tosmetric;
477*0Sstevel@tonic-gate 		while ((uchar_t *)tosmetric < ls_end) {
478*0Sstevel@tonic-gate 			if ((uchar_t *)tosmetric + sizeof (tm) > fragend)
479*0Sstevel@tonic-gate 				return (-1); /* truncated */
480*0Sstevel@tonic-gate 			tm = ntohl(*tosmetric);
481*0Sstevel@tonic-gate 			tos = (tm & SLA_MASK_TOS) >> SLA_SHIFT_TOS;
482*0Sstevel@tonic-gate 			metric = tm & SLA_MASK_METRIC;
483*0Sstevel@tonic-gate 			if (flags & F_DTAIL) {
484*0Sstevel@tonic-gate 				snprintf(get_line(0, 0), get_line_remain(),
485*0Sstevel@tonic-gate 				    " tos %d metric %d", tos, metric);
486*0Sstevel@tonic-gate 			}
487*0Sstevel@tonic-gate 			++tosmetric;
488*0Sstevel@tonic-gate 		}
489*0Sstevel@tonic-gate 		break;
490*0Sstevel@tonic-gate 	case LS_TYPE_ASE:
491*0Sstevel@tonic-gate 		if (TRUNC(&lsa->lsa_un.un_asla.asla_mask))
492*0Sstevel@tonic-gate 			return (-1);
493*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
494*0Sstevel@tonic-gate 			snprintf(get_line(0, 0), get_line_remain(), "Mask = %s",
495*0Sstevel@tonic-gate 			    inet_ntoa(lsa->lsa_un.un_asla.asla_mask));
496*0Sstevel@tonic-gate 		}
497*0Sstevel@tonic-gate 		am = lsa->lsa_un.un_asla.asla_metric;
498*0Sstevel@tonic-gate 		while ((uchar_t *)am < ls_end) {
499*0Sstevel@tonic-gate 			if ((uchar_t *)am + sizeof (tm) > fragend)
500*0Sstevel@tonic-gate 				return (-1); /* truncated */
501*0Sstevel@tonic-gate 			tm = ntohl(am->asla_tosmetric);
502*0Sstevel@tonic-gate 			tos = (tm & ASLA_MASK_TOS) >> ASLA_SHIFT_TOS;
503*0Sstevel@tonic-gate 			metric = tm & ASLA_MASK_METRIC;
504*0Sstevel@tonic-gate 			if (flags & F_DTAIL) {
505*0Sstevel@tonic-gate 				snprintf(get_line(0, 0), get_line_remain(),
506*0Sstevel@tonic-gate 				    " type %d tos %d metric %d",
507*0Sstevel@tonic-gate 				    (tm & ASLA_FLAG_EXTERNAL) ? 2 : 1,
508*0Sstevel@tonic-gate 				    tos, metric);
509*0Sstevel@tonic-gate 			}
510*0Sstevel@tonic-gate 			if (am->asla_forward.s_addr != 0) {
511*0Sstevel@tonic-gate 				if (flags & F_DTAIL)  {
512*0Sstevel@tonic-gate 					snprintf(get_line(0, 0),
513*0Sstevel@tonic-gate 					    get_line_remain(), " Forward %s",
514*0Sstevel@tonic-gate 					    inet_ntoa(am->asla_forward));
515*0Sstevel@tonic-gate 				}
516*0Sstevel@tonic-gate 			}
517*0Sstevel@tonic-gate 			if (am->asla_tag.s_addr != 0) {
518*0Sstevel@tonic-gate 				if (flags & F_DTAIL)  {
519*0Sstevel@tonic-gate 					snprintf(get_line(0, 0),
520*0Sstevel@tonic-gate 					    get_line_remain(), " Tag %s",
521*0Sstevel@tonic-gate 					    inet_ntoa(am->asla_tag));
522*0Sstevel@tonic-gate 				}
523*0Sstevel@tonic-gate 			}
524*0Sstevel@tonic-gate 			++am;
525*0Sstevel@tonic-gate 		}
526*0Sstevel@tonic-gate 		break;
527*0Sstevel@tonic-gate 	default:
528*0Sstevel@tonic-gate 		if (flags & F_DTAIL)  {
529*0Sstevel@tonic-gate 			snprintf(get_line(0, 0), get_line_remain(),
530*0Sstevel@tonic-gate 			    " Unknown LSA type %d", lsa->ls_hdr.ls_type);
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 		}
533*0Sstevel@tonic-gate 		break;
534*0Sstevel@tonic-gate 	}
535*0Sstevel@tonic-gate 	return (0);
536*0Sstevel@tonic-gate }
537*0Sstevel@tonic-gate #undef TRUNC
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate int
interpret_ospf(int flags,struct ospfhdr * ospf,int iplen,int fraglen)540*0Sstevel@tonic-gate interpret_ospf(int flags, struct ospfhdr *ospf, int iplen, int fraglen)
541*0Sstevel@tonic-gate {
542*0Sstevel@tonic-gate 	int nlsa, nlsah = 0;
543*0Sstevel@tonic-gate 	struct lsa_hdr *lsah;
544*0Sstevel@tonic-gate 	struct lsr *lsr;
545*0Sstevel@tonic-gate 	struct lsa *lsa;
546*0Sstevel@tonic-gate 	boolean_t trunc = B_FALSE;
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 	if ((fraglen < OSPF_MIN_HEADER_SIZE) ||
549*0Sstevel@tonic-gate 	    (fraglen < ntohs(ospf->ospf_len)))
550*0Sstevel@tonic-gate 		return (fraglen);	/* incomplete header */
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 	if (fraglen > ntohs(ospf->ospf_len))
553*0Sstevel@tonic-gate 		fraglen = ntohs(ospf->ospf_len);
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	if (ospf->ospf_type > OSPF_TYPE_MAX) {
557*0Sstevel@tonic-gate 		if (flags & F_SUM) {
558*0Sstevel@tonic-gate 			(void) sprintf(sum_line, "Unknown OSPF TYPE %d \n",
559*0Sstevel@tonic-gate 			    ospf->ospf_type);
560*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
561*0Sstevel@tonic-gate 		}
562*0Sstevel@tonic-gate 		if (flags & F_SUM) {
563*0Sstevel@tonic-gate 			show_header("OSPF:  ", "OSPF Header", fraglen);
564*0Sstevel@tonic-gate 			show_space();
565*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
566*0Sstevel@tonic-gate 			    "Unknown OSPF Type = %d", ospf->ospf_type);
567*0Sstevel@tonic-gate 		}
568*0Sstevel@tonic-gate 		return (fraglen);
569*0Sstevel@tonic-gate 	}
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate 	if (flags & F_SUM) {
572*0Sstevel@tonic-gate 		sum_line = (char *)get_sum_line();
573*0Sstevel@tonic-gate 		(void) sprintf(sum_line, "OSPF %s RTRID=%s ",
574*0Sstevel@tonic-gate 		    ospf_types[ospf->ospf_type],
575*0Sstevel@tonic-gate 		    inet_ntoa(ospf->ospf_routerid));
576*0Sstevel@tonic-gate 		sum_line += strlen(sum_line);
577*0Sstevel@tonic-gate 		(void) sprintf(sum_line, "AREA=%s LEN=%d ",
578*0Sstevel@tonic-gate 		    inet_ntoa(ospf->ospf_areaid),
579*0Sstevel@tonic-gate 		    ntohs((ushort_t)ospf->ospf_len));
580*0Sstevel@tonic-gate 		sum_line += strlen(sum_line);
581*0Sstevel@tonic-gate 	}
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate 	if (flags & F_DTAIL) {
584*0Sstevel@tonic-gate 		show_header("OSPF:  ", "OSPF Header", fraglen);
585*0Sstevel@tonic-gate 		show_space();
586*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
587*0Sstevel@tonic-gate 		    "Version = %d", ospf->ospf_version);
588*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
589*0Sstevel@tonic-gate 		    "Type = %s", ospf_types[ospf->ospf_type]);
590*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
591*0Sstevel@tonic-gate 		    "Router ID = %s", inet_ntoa(ospf->ospf_routerid));
592*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
593*0Sstevel@tonic-gate 		    "Area ID = %s", inet_ntoa(ospf->ospf_areaid));
594*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
595*0Sstevel@tonic-gate 		    "Checksum = 0x%x", ospf->ospf_chksum);
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 		if (ospf->ospf_authtype > OSPF_AUTH_TYPE_MAX) {
598*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
599*0Sstevel@tonic-gate 			    "Auth = %d (unknown auth type)",
600*0Sstevel@tonic-gate 			    ospf->ospf_authtype);
601*0Sstevel@tonic-gate 		} else {
602*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
603*0Sstevel@tonic-gate 			    "Auth = %s", ospf_authtypes[ospf->ospf_authtype]);
604*0Sstevel@tonic-gate 		}
605*0Sstevel@tonic-gate 	}
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 	if (ospf->ospf_version != 2) {
608*0Sstevel@tonic-gate 		if (ospf->ospf_version == 3) {
609*0Sstevel@tonic-gate 			if (flags & F_DTAIL)
610*0Sstevel@tonic-gate 				snprintf(get_line(0, 0), get_line_remain(),
611*0Sstevel@tonic-gate 				    "ospfv3 packet in ipv4 header");
612*0Sstevel@tonic-gate 			return (interpret_ospf6(flags, ospf, iplen, fraglen));
613*0Sstevel@tonic-gate 		} else  {
614*0Sstevel@tonic-gate 			return (fraglen);
615*0Sstevel@tonic-gate 		}
616*0Sstevel@tonic-gate 	}
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	switch (ospf->ospf_type) {
619*0Sstevel@tonic-gate 	case OSPF_TYPE_HELLO:
620*0Sstevel@tonic-gate 		if (interpret_ospf_hello(flags, ospf, fraglen) < 0)
621*0Sstevel@tonic-gate 			trunc = B_TRUE;
622*0Sstevel@tonic-gate 		break;
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 	case OSPF_TYPE_DB:
625*0Sstevel@tonic-gate 		if (fraglen < OSPF_MIN_HEADER_SIZE + OSPF_MIN_DB_HEADER_SIZE) {
626*0Sstevel@tonic-gate 			trunc = B_TRUE;
627*0Sstevel@tonic-gate 			break;
628*0Sstevel@tonic-gate 		}
629*0Sstevel@tonic-gate 		if (flags & F_SUM) {
630*0Sstevel@tonic-gate 			sprintf(sum_line, " %s %s S %X", ospf_print_bits(
631*0Sstevel@tonic-gate 			    ospf_option_bits, ospf->ospf_db.db_options),
632*0Sstevel@tonic-gate 			    ospf_print_bits(ospf_db_flags_bits,
633*0Sstevel@tonic-gate 			    ospf->ospf_db.db_flags),
634*0Sstevel@tonic-gate 			    ntohl(ospf->ospf_db.db_seq));
635*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
636*0Sstevel@tonic-gate 		}
637*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
638*0Sstevel@tonic-gate 			show_header("OSPF DB:  ", "Database Description Packet",
639*0Sstevel@tonic-gate 			    fraglen);
640*0Sstevel@tonic-gate 			show_space();
641*0Sstevel@tonic-gate 			snprintf(get_line(0, 0), get_line_remain(),
642*0Sstevel@tonic-gate 			    "Options = %s", ospf_print_bits(
643*0Sstevel@tonic-gate 			    ospf_option_bits, ospf->ospf_db.db_options));
644*0Sstevel@tonic-gate 			snprintf(get_line(0, 0), get_line_remain(),
645*0Sstevel@tonic-gate 			    "Flags = %s", ospf_print_bits(
646*0Sstevel@tonic-gate 			    ospf_db_flags_bits, ospf->ospf_db.db_flags));
647*0Sstevel@tonic-gate 			snprintf(get_line(0, 0), get_line_remain(),
648*0Sstevel@tonic-gate 			    "Sequence = 0x%X", ntohl(ospf->ospf_db.db_seq));
649*0Sstevel@tonic-gate 			/*  Print all the LS advs */
650*0Sstevel@tonic-gate 			lsah = ospf->ospf_db.db_lshdr;
651*0Sstevel@tonic-gate 			while ((uchar_t *)lsah < ((uchar_t *)ospf + fraglen)) {
652*0Sstevel@tonic-gate 				if ((uchar_t *)lsah + sizeof (struct lsa_hdr) >
653*0Sstevel@tonic-gate 				    ((uchar_t *)ospf + fraglen)) {
654*0Sstevel@tonic-gate 					trunc = B_TRUE;
655*0Sstevel@tonic-gate 					break;
656*0Sstevel@tonic-gate 				}
657*0Sstevel@tonic-gate 				interpret_ospf_lsa_hdr(flags, lsah);
658*0Sstevel@tonic-gate 				++lsah;
659*0Sstevel@tonic-gate 			}
660*0Sstevel@tonic-gate 		}
661*0Sstevel@tonic-gate 		break;
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	case OSPF_TYPE_LSR:
664*0Sstevel@tonic-gate 		if (fraglen < OSPF_MIN_HEADER_SIZE + OSPF_MIN_LSR_HEADER_SIZE) {
665*0Sstevel@tonic-gate 			trunc = B_TRUE;
666*0Sstevel@tonic-gate 			break;
667*0Sstevel@tonic-gate 		}
668*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
669*0Sstevel@tonic-gate 			snprintf(get_line(0, 0), get_line_remain(),
670*0Sstevel@tonic-gate 			    "Link State Request Packet");
671*0Sstevel@tonic-gate 		}
672*0Sstevel@tonic-gate 		lsr = ospf->ospf_lsr;
673*0Sstevel@tonic-gate 		while ((uchar_t *)lsr < ((uchar_t *)ospf + fraglen)) {
674*0Sstevel@tonic-gate 			if ((uchar_t *)lsr + sizeof (struct lsr) >
675*0Sstevel@tonic-gate 			    ((uchar_t *)ospf + fraglen)) {
676*0Sstevel@tonic-gate 				trunc = B_TRUE;
677*0Sstevel@tonic-gate 				break;
678*0Sstevel@tonic-gate 			}
679*0Sstevel@tonic-gate 			if (flags & F_SUM) {
680*0Sstevel@tonic-gate 				nlsah++;
681*0Sstevel@tonic-gate 			}
682*0Sstevel@tonic-gate 			if (flags & F_DTAIL) {
683*0Sstevel@tonic-gate 				ospf_print_ls_type(flags, ntohl(lsr->ls_type),
684*0Sstevel@tonic-gate 				    lsr->ls_stateid, lsr->ls_router);
685*0Sstevel@tonic-gate 			}
686*0Sstevel@tonic-gate 			++lsr;
687*0Sstevel@tonic-gate 		}
688*0Sstevel@tonic-gate 		if (flags & F_SUM) {
689*0Sstevel@tonic-gate 			sprintf(sum_line, " %d LSAs", nlsah);
690*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
691*0Sstevel@tonic-gate 		}
692*0Sstevel@tonic-gate 		break;
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate 	case OSPF_TYPE_LSU:
695*0Sstevel@tonic-gate 		if (fraglen < OSPF_MIN_HEADER_SIZE + OSPF_MIN_LSU_HEADER_SIZE) {
696*0Sstevel@tonic-gate 			trunc = B_TRUE;
697*0Sstevel@tonic-gate 			break;
698*0Sstevel@tonic-gate 		}
699*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
700*0Sstevel@tonic-gate 			show_header("OSPF LSU:  ", "Link State Update Packet",
701*0Sstevel@tonic-gate 			    fraglen);
702*0Sstevel@tonic-gate 			show_space();
703*0Sstevel@tonic-gate 		}
704*0Sstevel@tonic-gate 		lsa = ospf->ospf_lsu.lsu_lsa;
705*0Sstevel@tonic-gate 		nlsa = ntohl(ospf->ospf_lsu.lsu_count);
706*0Sstevel@tonic-gate 		if (flags & F_SUM) {
707*0Sstevel@tonic-gate 			sprintf(sum_line, "%d LSAs", nlsa);
708*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
709*0Sstevel@tonic-gate 			break;
710*0Sstevel@tonic-gate 		}
711*0Sstevel@tonic-gate 		while (nlsa-- != 0) {
712*0Sstevel@tonic-gate 			uchar_t *fragend = (uchar_t *)ospf + fraglen;
713*0Sstevel@tonic-gate 			if (((uchar_t *)lsa >= fragend) ||
714*0Sstevel@tonic-gate 			    ((uchar_t *)lsa + sizeof (struct lsa_hdr) >
715*0Sstevel@tonic-gate 			    fragend) ||
716*0Sstevel@tonic-gate 			    ((uchar_t *)lsa + ntohs(lsa->ls_hdr.ls_length) >
717*0Sstevel@tonic-gate 			    fragend)) {
718*0Sstevel@tonic-gate 				trunc = B_TRUE;
719*0Sstevel@tonic-gate 				break;
720*0Sstevel@tonic-gate 			}
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 			if (interpret_ospf_lsa(flags, lsa, fragend) < 0) {
723*0Sstevel@tonic-gate 				trunc = B_TRUE;
724*0Sstevel@tonic-gate 				break;
725*0Sstevel@tonic-gate 			}
726*0Sstevel@tonic-gate 			lsa = (struct lsa *)((uchar_t *)lsa +
727*0Sstevel@tonic-gate 			    ntohs(lsa->ls_hdr.ls_length));
728*0Sstevel@tonic-gate 		}
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 		break;
731*0Sstevel@tonic-gate 
732*0Sstevel@tonic-gate 	case OSPF_TYPE_LSA:
733*0Sstevel@tonic-gate 		if (flags & F_DTAIL) {
734*0Sstevel@tonic-gate 			show_header("OSPF LSA:  ", "Link State Ack Packet",
735*0Sstevel@tonic-gate 			    fraglen);
736*0Sstevel@tonic-gate 			show_space();
737*0Sstevel@tonic-gate 		}
738*0Sstevel@tonic-gate 		lsah = ospf->ospf_lsa.lsa_lshdr;
739*0Sstevel@tonic-gate 		nlsah = 0;
740*0Sstevel@tonic-gate 		while ((uchar_t *)lsah < ((uchar_t *)ospf + fraglen)) {
741*0Sstevel@tonic-gate 			if ((uchar_t *)lsah + sizeof (struct lsa_hdr) >
742*0Sstevel@tonic-gate 			    ((uchar_t *)ospf + fraglen)) {
743*0Sstevel@tonic-gate 				trunc = B_TRUE;
744*0Sstevel@tonic-gate 				break;
745*0Sstevel@tonic-gate 			}
746*0Sstevel@tonic-gate 			nlsah++;
747*0Sstevel@tonic-gate 			if (flags & F_DTAIL)
748*0Sstevel@tonic-gate 				interpret_ospf_lsa_hdr(flags, lsah);
749*0Sstevel@tonic-gate 			++lsah;
750*0Sstevel@tonic-gate 		}
751*0Sstevel@tonic-gate 		if (flags & F_SUM) {
752*0Sstevel@tonic-gate 			sprintf(sum_line, "%d LSAs", nlsah);
753*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
754*0Sstevel@tonic-gate 		}
755*0Sstevel@tonic-gate 		break;
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 	default:
758*0Sstevel@tonic-gate 		/* NOTREACHED */
759*0Sstevel@tonic-gate 		break;
760*0Sstevel@tonic-gate 	}
761*0Sstevel@tonic-gate 	if (trunc) {
762*0Sstevel@tonic-gate 		if (flags & F_SUM) {
763*0Sstevel@tonic-gate 			sprintf(sum_line, "--truncated");
764*0Sstevel@tonic-gate 			sum_line += strlen(sum_line);
765*0Sstevel@tonic-gate 		}
766*0Sstevel@tonic-gate 		if (flags & F_DTAIL)
767*0Sstevel@tonic-gate 			snprintf(get_line(0, 0), get_line_remain(),
768*0Sstevel@tonic-gate 			    "--truncated");
769*0Sstevel@tonic-gate 	}
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate 	return (fraglen);
772*0Sstevel@tonic-gate }
773