1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* BGP dump to ascii converter
3 * Copyright (C) 1999 Kunihiro Ishiguro
17 #include "bgpd/bgpd.h"
18 #include "bgpd/bgp_dump.h"
19 #include "bgpd/bgp_attr.h"
20 #include "bgpd/bgp_aspath.h"
23 static zebra_capabilities_t _caps_p
[] = {
24 ZCAP_BIND
, ZCAP_NET_RAW
, ZCAP_NET_ADMIN
,
27 struct zebra_privs_t bgpd_privs
= {
28 #if defined(FRR_USER) && defined(FRR_GROUP)
33 .vty_group
= VTY_GROUP
,
36 .cap_num_p
= array_size(_caps_p
),
42 MSG_START
, /* sender is starting up */
43 MSG_DIE
, /* receiver should shut down */
44 MSG_I_AM_DEAD
, /* sender is shutting down */
45 MSG_PEER_DOWN
, /* sender's peer is down */
46 MSG_PROTOCOL_BGP
, /* msg is a BGP packet */
47 MSG_PROTOCOL_RIP
, /* msg is a RIP packet */
48 MSG_PROTOCOL_IDRP
, /* msg is an IDRP packet */
49 MSG_PROTOCOL_RIPNG
, /* msg is a RIPNG packet */
50 MSG_PROTOCOL_BGP4PLUS
, /* msg is a BGP4+ packet */
51 MSG_PROTOCOL_BGP4PLUS_01
, /* msg is a BGP4+ (draft 01) packet */
52 MSG_PROTOCOL_OSPF
, /* msg is an OSPF packet */
53 MSG_TABLE_DUMP
/* routing table dump */
56 static void attr_parse(struct stream
*s
, uint16_t len
)
65 printf("%s s->getp %zd, len %d, lim %d\n", __func__
, s
->getp
, len
, lim
);
67 while (s
->getp
< lim
) {
68 flag
= stream_getc(s
);
69 type
= stream_getc(s
);
71 if (flag
& BGP_ATTR_FLAG_EXTLEN
)
72 length
= stream_getw(s
);
74 length
= stream_getc(s
);
76 printf("FLAG: %d\n", flag
);
77 printf("TYPE: %d\n", type
);
78 printf("Len: %d\n", length
);
81 case BGP_ATTR_ORIGIN
: {
83 origin
= stream_getc(s
);
84 printf("ORIGIN: %d\n", origin
);
86 case BGP_ATTR_AS_PATH
: {
87 struct aspath
*aspath
;
89 aspath
= aspath_parse(s
, length
, 1,
90 bgp_get_asnotation(NULL
));
91 printf("ASPATH: %s\n", aspath
->str
);
94 case BGP_ATTR_NEXT_HOP
: {
95 struct in_addr nexthop
;
96 nexthop
.s_addr
= stream_get_ipv4(s
);
97 printf("NEXTHOP: %pI4\n", &nexthop
);
100 stream_getw_from(s
, length
);
106 int main(int argc
, char **argv
)
121 uint16_t viewno
, seq_num
;
122 struct prefix_ipv4 p
;
124 s
= stream_new(10000);
127 fprintf(stderr
, "Usage: %s FILENAME\n", argv
[0]);
130 fd
= open(argv
[1], O_RDONLY
);
133 "%% Can't open configuration file %s due to '%s'.\n",
134 argv
[1], safe_strerror(errno
));
141 ret
= stream_read(s
, fd
, 12);
144 printf("END OF FILE\n");
146 printf("ERROR OF READ\n");
148 printf("UNDERFLOW\n");
152 /* Extract header. */
153 now
= stream_getl(s
);
154 type
= stream_getw(s
);
155 subtype
= stream_getw(s
);
156 len
= stream_getl(s
);
158 printf("TIME: %s", ctime(&now
));
160 /* printf ("TYPE: %d/%d\n", type, subtype); */
162 if (type
== MSG_PROTOCOL_BGP4MP
)
163 printf("TYPE: BGP4MP");
164 else if (type
== MSG_PROTOCOL_BGP4MP_ET
)
165 printf("TYPE: BGP4MP_ET");
166 else if (type
== MSG_TABLE_DUMP
)
167 printf("TYPE: MSG_TABLE_DUMP");
169 printf("TYPE: Unknown %d", type
);
171 if (type
== MSG_TABLE_DUMP
)
177 printf("/AFI_IP6\n");
180 printf("/UNKNOWN %d", subtype
);
185 case BGP4MP_STATE_CHANGE
:
189 printf("/MESSAGE\n");
194 case BGP4MP_SNAPSHOT
:
195 printf("/SNAPSHOT\n");
198 printf("/UNKNOWN %d", subtype
);
203 printf("len: %zd\n", len
);
205 ret
= stream_read(s
, fd
, len
);
206 if (ret
!= (int)len
) {
208 printf("END OF FILE 2\n");
210 printf("ERROR OF READ 2\n");
212 printf("UNDERFLOW 2\n");
216 /* printf ("now read %d\n", len); */
218 if (type
== MSG_TABLE_DUMP
) {
224 viewno
= stream_getw(s
);
225 seq_num
= stream_getw(s
);
226 printf("VIEW: %d\n", viewno
);
227 printf("SEQUENCE: %d\n", seq_num
);
230 while (s
->getp
< len
- 16) {
231 p
.prefix
.s_addr
= stream_get_ipv4(s
);
232 p
.prefixlen
= stream_getc(s
);
233 printf("PREFIX: %pI4/%d\n", &p
.prefix
,
236 status
= stream_getc(s
);
237 originated
= stream_getl(s
);
238 peer
.s_addr
= stream_get_ipv4(s
);
239 source_as
= stream_getw(s
);
241 printf("FROM: %pI4 AS%d\n", &peer
, source_as
);
242 printf("ORIGINATED: %s", ctime(&originated
));
244 attrlen
= stream_getw(s
);
245 printf("ATTRLEN: %d\n", attrlen
);
247 attr_parse(s
, attrlen
);
249 printf("STATUS: 0x%x\n", status
);
252 source_as
= stream_getw(s
);
253 dest_as
= stream_getw(s
);
254 printf("source_as: %d\n", source_as
);
255 printf("dest_as: %d\n", dest_as
);
257 ifindex
= stream_getw(s
);
258 family
= stream_getw(s
);
260 printf("ifindex: %d\n", ifindex
);
261 printf("family: %d\n", family
);
263 sip
.s_addr
= stream_get_ipv4(s
);
264 dip
.s_addr
= stream_get_ipv4(s
);
266 printf("saddr: %pI4\n", &sip
);
267 printf("daddr: %pI4\n", &dip
);