]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_btoa.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / bgpd / bgp_btoa.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* BGP dump to ascii converter
3 * Copyright (C) 1999 Kunihiro Ishiguro
4 */
5
6 #include <zebra.h>
7
8 #include "zebra.h"
9 #include "stream.h"
10 #include "log.h"
11 #include "prefix.h"
12 #include "command.h"
13 #include "memory.h"
14 #include "privs.h"
15 #include "filter.h"
16
17 #include "bgpd/bgpd.h"
18 #include "bgpd/bgp_dump.h"
19 #include "bgpd/bgp_attr.h"
20 #include "bgpd/bgp_aspath.h"
21
22 /* privileges */
23 static zebra_capabilities_t _caps_p[] = {
24 ZCAP_BIND, ZCAP_NET_RAW, ZCAP_NET_ADMIN,
25 };
26
27 struct zebra_privs_t bgpd_privs = {
28 #if defined(FRR_USER) && defined(FRR_GROUP)
29 .user = FRR_USER,
30 .group = FRR_GROUP,
31 #endif
32 #ifdef VTY_GROUP
33 .vty_group = VTY_GROUP,
34 #endif
35 .caps_p = _caps_p,
36 .cap_num_p = array_size(_caps_p),
37 .cap_num_i = 0,
38 };
39
40 enum MRT_MSG_TYPES {
41 MSG_NULL,
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 */
54 };
55
56 static void attr_parse(struct stream *s, uint16_t len)
57 {
58 unsigned int flag;
59 unsigned int type;
60 uint16_t length;
61 uint16_t lim;
62
63 lim = s->getp + len;
64
65 printf("%s s->getp %zd, len %d, lim %d\n", __func__, s->getp, len, lim);
66
67 while (s->getp < lim) {
68 flag = stream_getc(s);
69 type = stream_getc(s);
70
71 if (flag & BGP_ATTR_FLAG_EXTLEN)
72 length = stream_getw(s);
73 else
74 length = stream_getc(s);
75
76 printf("FLAG: %d\n", flag);
77 printf("TYPE: %d\n", type);
78 printf("Len: %d\n", length);
79
80 switch (type) {
81 case BGP_ATTR_ORIGIN: {
82 uint8_t origin;
83 origin = stream_getc(s);
84 printf("ORIGIN: %d\n", origin);
85 } break;
86 case BGP_ATTR_AS_PATH: {
87 struct aspath *aspath;
88
89 aspath = aspath_parse(s, length, 1,
90 bgp_get_asnotation(NULL));
91 printf("ASPATH: %s\n", aspath->str);
92 aspath_free(aspath);
93 } break;
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);
98 } break;
99 default:
100 stream_getw_from(s, length);
101 break;
102 }
103 }
104 }
105
106 int main(int argc, char **argv)
107 {
108 int ret;
109 int fd;
110 struct stream *s;
111 time_t now;
112 int type;
113 int subtype;
114 size_t len;
115 int source_as;
116 int dest_as;
117 ifindex_t ifindex;
118 int family;
119 struct in_addr sip;
120 struct in_addr dip;
121 uint16_t viewno, seq_num;
122 struct prefix_ipv4 p;
123
124 s = stream_new(10000);
125
126 if (argc != 2) {
127 fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
128 exit(1);
129 }
130 fd = open(argv[1], O_RDONLY);
131 if (fd < 0) {
132 fprintf(stdout,
133 "%% Can't open configuration file %s due to '%s'.\n",
134 argv[1], safe_strerror(errno));
135 exit(1);
136 }
137
138 while (1) {
139 stream_reset(s);
140
141 ret = stream_read(s, fd, 12);
142 if (ret != 12) {
143 if (!ret)
144 printf("END OF FILE\n");
145 else if (ret < 0)
146 printf("ERROR OF READ\n");
147 else
148 printf("UNDERFLOW\n");
149 break;
150 }
151
152 /* Extract header. */
153 now = stream_getl(s);
154 type = stream_getw(s);
155 subtype = stream_getw(s);
156 len = stream_getl(s);
157
158 printf("TIME: %s", ctime(&now));
159
160 /* printf ("TYPE: %d/%d\n", type, subtype); */
161
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");
168 else
169 printf("TYPE: Unknown %d", type);
170
171 if (type == MSG_TABLE_DUMP)
172 switch (subtype) {
173 case AFI_IP:
174 printf("/AFI_IP\n");
175 break;
176 case AFI_IP6:
177 printf("/AFI_IP6\n");
178 break;
179 default:
180 printf("/UNKNOWN %d", subtype);
181 break;
182 }
183 else {
184 switch (subtype) {
185 case BGP4MP_STATE_CHANGE:
186 printf("/CHANGE\n");
187 break;
188 case BGP4MP_MESSAGE:
189 printf("/MESSAGE\n");
190 break;
191 case BGP4MP_ENTRY:
192 printf("/ENTRY\n");
193 break;
194 case BGP4MP_SNAPSHOT:
195 printf("/SNAPSHOT\n");
196 break;
197 default:
198 printf("/UNKNOWN %d", subtype);
199 break;
200 }
201 }
202
203 printf("len: %zd\n", len);
204
205 ret = stream_read(s, fd, len);
206 if (ret != (int)len) {
207 if (!ret)
208 printf("END OF FILE 2\n");
209 else if (ret < 0)
210 printf("ERROR OF READ 2\n");
211 else
212 printf("UNDERFLOW 2\n");
213 break;
214 }
215
216 /* printf ("now read %d\n", len); */
217
218 if (type == MSG_TABLE_DUMP) {
219 uint8_t status;
220 time_t originated;
221 struct in_addr peer;
222 uint16_t attrlen;
223
224 viewno = stream_getw(s);
225 seq_num = stream_getw(s);
226 printf("VIEW: %d\n", viewno);
227 printf("SEQUENCE: %d\n", seq_num);
228
229 /* start */
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,
234 p.prefixlen);
235
236 status = stream_getc(s);
237 originated = stream_getl(s);
238 peer.s_addr = stream_get_ipv4(s);
239 source_as = stream_getw(s);
240
241 printf("FROM: %pI4 AS%d\n", &peer, source_as);
242 printf("ORIGINATED: %s", ctime(&originated));
243
244 attrlen = stream_getw(s);
245 printf("ATTRLEN: %d\n", attrlen);
246
247 attr_parse(s, attrlen);
248
249 printf("STATUS: 0x%x\n", status);
250 }
251 } else {
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);
256
257 ifindex = stream_getw(s);
258 family = stream_getw(s);
259
260 printf("ifindex: %d\n", ifindex);
261 printf("family: %d\n", family);
262
263 sip.s_addr = stream_get_ipv4(s);
264 dip.s_addr = stream_get_ipv4(s);
265
266 printf("saddr: %pI4\n", &sip);
267 printf("daddr: %pI4\n", &dip);
268
269 printf("\n");
270 }
271 }
272 close(fd);
273 return 0;
274 }