paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* BGP dump to ascii converter |
| 2 | Copyright (C) 1999 Kunihiro Ishiguro |
| 3 | |
| 4 | This file is part of GNU Zebra. |
| 5 | |
| 6 | GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 2, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "zebra.h" |
| 24 | #include "stream.h" |
| 25 | #include "log.h" |
| 26 | #include "prefix.h" |
| 27 | #include "command.h" |
Donald Sharp | bf99b42 | 2015-10-21 10:00:47 -0400 | [diff] [blame^] | 28 | #include "memory.h" |
| 29 | #include "privs.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 30 | |
| 31 | #include "bgpd/bgpd.h" |
| 32 | #include "bgpd/bgp_dump.h" |
| 33 | #include "bgpd/bgp_attr.h" |
| 34 | #include "bgpd/bgp_aspath.h" |
| 35 | |
Donald Sharp | bf99b42 | 2015-10-21 10:00:47 -0400 | [diff] [blame^] | 36 | /* privileges */ |
| 37 | static zebra_capabilities_t _caps_p [] = |
| 38 | { |
| 39 | ZCAP_BIND, |
| 40 | ZCAP_NET_RAW, |
| 41 | ZCAP_NET_ADMIN, |
| 42 | }; |
| 43 | |
| 44 | struct zebra_privs_t bgpd_privs = |
| 45 | { |
| 46 | #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP) |
| 47 | .user = QUAGGA_USER, |
| 48 | .group = QUAGGA_GROUP, |
| 49 | #endif |
| 50 | #ifdef VTY_GROUP |
| 51 | .vty_group = VTY_GROUP, |
| 52 | #endif |
| 53 | .caps_p = _caps_p, |
| 54 | .cap_num_p = array_size(_caps_p), |
| 55 | .cap_num_i = 0, |
| 56 | }; |
| 57 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 58 | enum MRT_MSG_TYPES { |
| 59 | MSG_NULL, |
| 60 | MSG_START, /* sender is starting up */ |
| 61 | MSG_DIE, /* receiver should shut down */ |
| 62 | MSG_I_AM_DEAD, /* sender is shutting down */ |
| 63 | MSG_PEER_DOWN, /* sender's peer is down */ |
| 64 | MSG_PROTOCOL_BGP, /* msg is a BGP packet */ |
| 65 | MSG_PROTOCOL_RIP, /* msg is a RIP packet */ |
| 66 | MSG_PROTOCOL_IDRP, /* msg is an IDRP packet */ |
| 67 | MSG_PROTOCOL_RIPNG, /* msg is a RIPNG packet */ |
| 68 | MSG_PROTOCOL_BGP4PLUS, /* msg is a BGP4+ packet */ |
| 69 | MSG_PROTOCOL_BGP4PLUS_01, /* msg is a BGP4+ (draft 01) packet */ |
| 70 | MSG_PROTOCOL_OSPF, /* msg is an OSPF packet */ |
| 71 | MSG_TABLE_DUMP /* routing table dump */ |
| 72 | }; |
| 73 | |
Donald Sharp | bf99b42 | 2015-10-21 10:00:47 -0400 | [diff] [blame^] | 74 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 75 | attr_parse (struct stream *s, u_int16_t len) |
| 76 | { |
| 77 | u_int flag; |
| 78 | u_int type; |
| 79 | u_int16_t length; |
| 80 | u_int16_t lim; |
| 81 | |
| 82 | lim = s->getp + len; |
| 83 | |
Donald Sharp | bf99b42 | 2015-10-21 10:00:47 -0400 | [diff] [blame^] | 84 | printf ("attr_parse s->getp %zd, len %d, lim %d\n", s->getp, len, lim); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 85 | |
| 86 | while (s->getp < lim) |
| 87 | { |
| 88 | flag = stream_getc (s); |
| 89 | type = stream_getc (s); |
| 90 | |
Donald Sharp | bf99b42 | 2015-10-21 10:00:47 -0400 | [diff] [blame^] | 91 | if (flag & BGP_ATTR_FLAG_EXTLEN) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 92 | length = stream_getw (s); |
| 93 | else |
| 94 | length = stream_getc (s); |
| 95 | |
| 96 | printf ("FLAG: %d\n", flag); |
| 97 | printf ("TYPE: %d\n", type); |
| 98 | printf ("Len: %d\n", length); |
| 99 | |
| 100 | switch (type) |
| 101 | { |
| 102 | case BGP_ATTR_ORIGIN: |
| 103 | { |
| 104 | u_char origin; |
| 105 | origin = stream_getc (s); |
| 106 | printf ("ORIGIN: %d\n", origin); |
| 107 | } |
| 108 | break; |
| 109 | case BGP_ATTR_AS_PATH: |
| 110 | { |
Donald Sharp | bf99b42 | 2015-10-21 10:00:47 -0400 | [diff] [blame^] | 111 | struct aspath *aspath; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 112 | |
Donald Sharp | bf99b42 | 2015-10-21 10:00:47 -0400 | [diff] [blame^] | 113 | aspath = aspath_parse (s, length, 1); |
| 114 | printf ("ASPATH: %s\n", aspath->str); |
| 115 | aspath_free(aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 116 | } |
| 117 | break; |
Donald Sharp | bf99b42 | 2015-10-21 10:00:47 -0400 | [diff] [blame^] | 118 | case BGP_ATTR_NEXT_HOP: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | { |
| 120 | struct in_addr nexthop; |
| 121 | nexthop.s_addr = stream_get_ipv4 (s); |
| 122 | printf ("NEXTHOP: %s\n", inet_ntoa (nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 123 | } |
| 124 | break; |
| 125 | default: |
Donald Sharp | bf99b42 | 2015-10-21 10:00:47 -0400 | [diff] [blame^] | 126 | stream_getw_from (s, length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | int |
| 135 | main (int argc, char **argv) |
| 136 | { |
| 137 | int ret; |
| 138 | FILE *fp; |
| 139 | struct stream *s; |
| 140 | time_t now; |
| 141 | int type; |
| 142 | int subtype; |
Donald Sharp | bf99b42 | 2015-10-21 10:00:47 -0400 | [diff] [blame^] | 143 | size_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 144 | int source_as; |
| 145 | int dest_as; |
| 146 | int ifindex; |
| 147 | int family; |
| 148 | struct in_addr sip; |
| 149 | struct in_addr dip; |
| 150 | u_int16_t viewno, seq_num; |
| 151 | struct prefix_ipv4 p; |
| 152 | |
| 153 | s = stream_new (10000); |
| 154 | |
| 155 | if (argc != 2) |
| 156 | { |
| 157 | fprintf (stderr, "Usage: %s FILENAME\n", argv[0]); |
| 158 | exit (1); |
| 159 | } |
| 160 | fp = fopen (argv[1], "r"); |
| 161 | if (!fp) |
| 162 | { |
| 163 | perror ("fopen"); |
| 164 | exit (1); |
| 165 | } |
| 166 | |
| 167 | while (1) |
| 168 | { |
| 169 | stream_reset (s); |
| 170 | |
| 171 | ret = fread (s->data, 12, 1, fp); |
Donald Sharp | bf99b42 | 2015-10-21 10:00:47 -0400 | [diff] [blame^] | 172 | if (!ret || feof (fp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 173 | { |
| 174 | printf ("END OF FILE\n"); |
| 175 | break; |
| 176 | } |
| 177 | if (ferror (fp)) |
| 178 | { |
| 179 | printf ("ERROR OF FREAD\n"); |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | /* Extract header. */ |
| 184 | now = stream_getl (s); |
| 185 | type = stream_getw (s); |
| 186 | subtype = stream_getw (s); |
| 187 | len = stream_getl (s); |
| 188 | |
| 189 | printf ("TIME: %s", ctime (&now)); |
| 190 | |
| 191 | /* printf ("TYPE: %d/%d\n", type, subtype); */ |
| 192 | |
| 193 | if (type == MSG_PROTOCOL_BGP4MP) |
| 194 | printf ("TYPE: BGP4MP"); |
| 195 | else if (type == MSG_TABLE_DUMP) |
| 196 | printf ("TYPE: MSG_TABLE_DUMP"); |
| 197 | else |
| 198 | printf ("TYPE: Unknown %d", type); |
| 199 | |
| 200 | if (type == MSG_TABLE_DUMP) |
| 201 | switch (subtype) |
| 202 | { |
| 203 | case AFI_IP: |
| 204 | printf ("/AFI_IP\n"); |
| 205 | break; |
| 206 | case AFI_IP6: |
| 207 | printf ("/AFI_IP6\n"); |
| 208 | break; |
| 209 | default: |
| 210 | printf ("/UNKNOWN %d", subtype); |
| 211 | break; |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | switch (subtype) |
| 216 | { |
| 217 | case BGP4MP_STATE_CHANGE: |
| 218 | printf ("/CHANGE\n"); |
| 219 | break; |
| 220 | case BGP4MP_MESSAGE: |
| 221 | printf ("/MESSAGE\n"); |
| 222 | break; |
| 223 | case BGP4MP_ENTRY: |
| 224 | printf ("/ENTRY\n"); |
| 225 | break; |
| 226 | case BGP4MP_SNAPSHOT: |
| 227 | printf ("/SNAPSHOT\n"); |
| 228 | break; |
| 229 | default: |
| 230 | printf ("/UNKNOWN %d", subtype); |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | |
Donald Sharp | bf99b42 | 2015-10-21 10:00:47 -0400 | [diff] [blame^] | 235 | printf ("len: %zd\n", len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 236 | |
| 237 | ret = fread (s->data + 12, len, 1, fp); |
| 238 | if (feof (fp)) |
| 239 | { |
| 240 | printf ("ENDOF FILE 2\n"); |
| 241 | break; |
| 242 | } |
| 243 | if (ferror (fp)) |
| 244 | { |
| 245 | printf ("ERROR OF FREAD 2\n"); |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | /* printf ("now read %d\n", len); */ |
| 250 | |
| 251 | if (type == MSG_TABLE_DUMP) |
| 252 | { |
| 253 | u_char status; |
| 254 | time_t originated; |
| 255 | struct in_addr peer; |
| 256 | u_int16_t attrlen; |
| 257 | |
| 258 | viewno = stream_getw (s); |
| 259 | seq_num = stream_getw (s); |
| 260 | printf ("VIEW: %d\n", viewno); |
| 261 | printf ("SEQUENCE: %d\n", seq_num); |
| 262 | |
| 263 | /* start */ |
| 264 | while (s->getp < len - 16) |
| 265 | { |
| 266 | p.prefix.s_addr = stream_get_ipv4 (s); |
| 267 | p.prefixlen = stream_getc (s); |
| 268 | printf ("PREFIX: %s/%d\n", inet_ntoa (p.prefix), p.prefixlen); |
| 269 | |
| 270 | status = stream_getc (s); |
| 271 | originated = stream_getl (s); |
| 272 | peer.s_addr = stream_get_ipv4 (s); |
| 273 | source_as = stream_getw(s); |
| 274 | |
| 275 | printf ("FROM: %s AS%d\n", inet_ntoa (peer), source_as); |
| 276 | printf ("ORIGINATED: %s", ctime (&originated)); |
| 277 | |
| 278 | attrlen = stream_getw (s); |
| 279 | printf ("ATTRLEN: %d\n", attrlen); |
| 280 | |
| 281 | attr_parse (s, attrlen); |
| 282 | |
| 283 | printf ("STATUS: 0x%x\n", status); |
| 284 | } |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | source_as = stream_getw (s); |
| 289 | dest_as = stream_getw (s); |
| 290 | printf ("source_as: %d\n", source_as); |
| 291 | printf ("dest_as: %d\n", dest_as); |
| 292 | |
| 293 | ifindex = stream_getw (s); |
| 294 | family = stream_getw (s); |
| 295 | |
| 296 | printf ("ifindex: %d\n", ifindex); |
| 297 | printf ("family: %d\n", family); |
| 298 | |
| 299 | sip.s_addr = stream_get_ipv4 (s); |
| 300 | dip.s_addr = stream_get_ipv4 (s); |
| 301 | |
| 302 | printf ("saddr: %s\n", inet_ntoa (sip)); |
| 303 | printf ("daddr: %s\n", inet_ntoa (dip)); |
| 304 | |
| 305 | printf ("\n"); |
| 306 | } |
| 307 | } |
| 308 | fclose (fp); |
| 309 | return 0; |
| 310 | } |