bgpdump.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  1. /*
  2. Copyright (c) 2007 - 2010 RIPE NCC - All Rights Reserved
  3. Permission to use, copy, modify, and distribute this software and its
  4. documentation for any purpose and without fee is hereby granted, provided
  5. that the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation, and that the name of the author not be used in advertising or
  8. publicity pertaining to distribution of the software without specific,
  9. written prior permission.
  10. THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  11. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  12. AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  13. DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  14. AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. Parts of this code have been engineered after analiyzing GNU Zebra's
  17. source code and therefore might contain declarations/code from GNU
  18. Zebra, Copyright (C) 1999 Kunihiro Ishiguro. Zebra is a free routing
  19. software, distributed under the GNU General Public License. A copy of
  20. this license is included with libbgpdump.
  21. Original Author: Shufu Mao(msf98@mails.tsinghua.edu.cn)
  22. */
  23. #include "bgpdump-config.h"
  24. #include "bgpdump_lib.h"
  25. #include "util.h"
  26. #include <time.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <stdlib.h>
  30. #include <netinet/in.h>
  31. #include <sys/socket.h>
  32. #include <arpa/inet.h>
  33. #include <stdbool.h>
  34. static void process(BGPDUMP_ENTRY *entry);
  35. static void process_bgpdump_mrtd_bgp(BGPDUMP_ENTRY *entry);
  36. static void show_ipv4_address(struct in_addr ip);
  37. static void show_attr(attributes_t *attr);
  38. static void show_prefixes(int count,struct prefix *prefix, int addpath);
  39. static void table_line_announce_1(struct mp_nlri *prefix,int count,BGPDUMP_ENTRY *entry,char *time_str);
  40. static void table_line_announce(struct prefix *prefix,int count,BGPDUMP_ENTRY *entry,char *time_str);
  41. static void mrtd_table_line_withdraw(struct prefix *prefix, int count, BGPDUMP_ENTRY *entry, char *time_str);
  42. static void mrtd_table_line_announce(struct prefix *prefix, int count, BGPDUMP_ENTRY *entry, char *time_str);
  43. static void table_line_withdraw(struct prefix *prefix,int count,BGPDUMP_ENTRY *entry,char *time_str);
  44. static void table_line_mrtd_route(BGPDUMP_MRTD_TABLE_DUMP *route,BGPDUMP_ENTRY *entry);
  45. static void table_line_dump_v2_prefix(BGPDUMP_TABLE_DUMP_V2_PREFIX *e,BGPDUMP_ENTRY *entry);
  46. static char *describe_origin(int origin);
  47. static int bgp4mp_message_direction_receive(BGPDUMP_ENTRY *entry);
  48. #ifdef BGPDUMP_HAVE_IPV6
  49. void show_prefixes6(int count,struct prefix *prefix, int addpath);
  50. static void table_line_withdraw6(struct prefix *prefix,int count,BGPDUMP_ENTRY *entry,char *time_str);
  51. static void table_line_announce6(struct mp_nlri *prefix,int count,BGPDUMP_ENTRY *entry,char *time_str);
  52. #endif
  53. static void show_line_prefix(const char* format, const char* time_str, const char* type);
  54. /* If no aspath was present as a string in the packet, return an empty string
  55. * so everything stays machine parsable */
  56. static char *attr_aspath(attributes_t *a) {
  57. if(a->flag & ATTR_FLAG_BIT(BGP_ATTR_AS_PATH) && a->aspath && a->aspath->str) {
  58. return a->aspath->str;
  59. }
  60. return "";
  61. }
  62. /* Helper function to return the direction of a BGP4MP_MESSAGE
  63. * returns true if it is a received message, false if it is a sent message */
  64. static int bgp4mp_message_direction_receive(BGPDUMP_ENTRY *entry) {
  65. switch(entry->subtype) {
  66. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_LOCAL:
  67. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4_LOCAL:
  68. return 0;
  69. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE:
  70. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4:
  71. default:
  72. return 1;
  73. }
  74. }
  75. /* Helper function that returns the format of a BGP4MP_MESSAGE as a string:
  76. - BGP4MP (Standard)
  77. - BGP4MP_ET (Extended Header)
  78. - BGP4MP_LOCAL (Local)
  79. - BGP4MP_ET_LOCAL (Local and Extended Header)
  80. - BGP4MP_AP (Standard, Additional Paths)
  81. - BGP4MP_ET_AP (Extended Header, Additional Paths)
  82. - BGP4MP_LOCAL_AP (Local, Additional Paths)
  83. - BGP4MP_ET_LOCAL_AP (Local, Extended Header, Additional Paths)
  84. */
  85. static const char* get_bgp4mp_format(BGPDUMP_ENTRY *entry) {
  86. if (entry->type == BGPDUMP_TYPE_ZEBRA_BGP_ET && !bgp4mp_message_direction_receive(entry)) {
  87. if (is_addpath(entry)) {
  88. return "BGP4MP_ET_LOCAL_AP";
  89. } else {
  90. return "BGP4MP_ET_LOCAL";
  91. }
  92. } else if (entry->type == BGPDUMP_TYPE_ZEBRA_BGP_ET) {
  93. if (is_addpath(entry)) {
  94. return "BGP4MP_ET_AP";
  95. } else {
  96. return "BGP4MP_ET";
  97. }
  98. } else if (!bgp4mp_message_direction_receive(entry)) {
  99. if (is_addpath(entry)) {
  100. return "BGP4MP_LOCAL_AP";
  101. } else {
  102. return "BGP4MP_LOCAL";
  103. }
  104. } else if (is_addpath(entry)) {
  105. return "BGP4MP_AP";
  106. } else {
  107. return "BGP4MP";
  108. }
  109. }
  110. static const char* get_bgp_state_name(u_int16_t state) {
  111. const char *bgp_state_name[] = {
  112. "Unknown",
  113. "Idle",
  114. "Connect",
  115. "Active",
  116. "Opensent",
  117. "Openconfirm",
  118. "Established",
  119. /* not defined in RFC 6396 but quagga puts them into update files */
  120. "Clearing",
  121. "Deleted",
  122. NULL
  123. };
  124. if (state && state >= (sizeof(bgp_state_name)) / (sizeof(*bgp_state_name))) {
  125. return "Unknown";
  126. } else {
  127. return bgp_state_name[state];
  128. }
  129. }
  130. static int mode=0;
  131. static int timetype=0;
  132. static int show_packet_index = 0;
  133. static int packet_index = 0;
  134. static const char USAGE[] = "\
  135. bgpdump version " PACKAGE_VERSION "\n\
  136. Usage: bgpdump [-m|-M] [-t dump|-t change] [-O <output-file>] <input-file>\n\
  137. bgpdump translates binary MRT files (possibly compressed) into readable output\n\
  138. Output mode:\n\
  139. -H multi-line, human-readable (the default)\n\
  140. -m one-line per entry with unix timestamps\n\
  141. -M one-line per entry with human readable timestamps\n\
  142. (there are other differences between -m and -M)\n\
  143. \n\
  144. Common options:\n\
  145. -O <file> output to <file> instead of STDOUT\n\
  146. -s log to syslog (the default)\n\
  147. -v log to STDERR\n\
  148. \n\
  149. Options for -m and -M modes:\n\
  150. -t dump timestamps for RIB dumps reflect the time of the dump (the default)\n\
  151. -t change timestamps for RIB dumps reflect the last route modification\n\
  152. -p show packet index at second position\n\
  153. \n\
  154. Special options:\n\
  155. -T run unit tests and exit\n\
  156. \n";
  157. extern char *optarg;
  158. extern int optind;
  159. int main(int argc, char *argv[]) {
  160. int c;
  161. int fd;
  162. bool usage_error = false;
  163. bool use_syslog = true;
  164. log_to_stderr();
  165. while ((c=getopt(argc,argv,"if:o:t:mMHO:svTp"))!=-1)
  166. switch(c)
  167. {
  168. case 'H':
  169. mode=0;
  170. break;
  171. case 'm':
  172. mode=1;
  173. break;
  174. case 'M':
  175. mode=2;
  176. break;
  177. case 't':
  178. if(strcmp(optarg,"dump")==0){
  179. timetype=0;
  180. } else if(strcmp(optarg,"change")==0){
  181. timetype=1;
  182. } else {
  183. printf("unknown -t option\n");
  184. exit(1);
  185. }
  186. break;
  187. case 'O':
  188. fprintf(stderr, "redirecting output to '%s'\n", optarg);
  189. fd = open(optarg, O_WRONLY|O_CREAT, 0666);
  190. if(fd < 0 || 0 > dup2(fd, STDOUT_FILENO)) {
  191. perror("can't open output file");
  192. exit(1);
  193. }
  194. break;
  195. case 's':
  196. use_syslog = true;
  197. break;
  198. case 'v':
  199. use_syslog = false;
  200. break;
  201. case 'i':
  202. case 'f':
  203. case 'o':
  204. warn("ignoring option '-%c'", c);
  205. break;
  206. case 'T':
  207. test_fmt_ip();
  208. test_utils();
  209. exit(0);
  210. case 'p':
  211. show_packet_index = 1;
  212. break;
  213. case '?':
  214. default:
  215. usage_error = true;
  216. }
  217. argc -= optind;
  218. argv += optind;
  219. if(use_syslog) {
  220. debug("logging to syslog");
  221. log_to_syslog();
  222. }
  223. if(usage_error || argc != 1) {
  224. if(argc != 1)
  225. err("you must supply exactly one file to process");
  226. fprintf(stderr, "%s", USAGE);
  227. exit(1);
  228. }
  229. // more efficient then line buffering
  230. // static char buffer[16000];
  231. // setbuffer(stdout, buffer, sizeof buffer);
  232. BGPDUMP *my_dump = bgpdump_open_dump(argv[0]);
  233. if(! my_dump)
  234. return 1;
  235. do {
  236. BGPDUMP_ENTRY *my_entry = bgpdump_read_next(my_dump);
  237. if(my_entry) {
  238. process(my_entry);
  239. bgpdump_free_mem(my_entry);
  240. packet_index++;
  241. }
  242. } while(my_dump->eof==0);
  243. bgpdump_close_dump(my_dump);
  244. return 0;
  245. }
  246. void process(BGPDUMP_ENTRY *entry) {
  247. struct tm *date;
  248. char time_str[128];
  249. char time_str2[128];
  250. char time_str_fixed[128];
  251. char prefix[BGPDUMP_ADDRSTRLEN];
  252. char *bgp4mp_format;
  253. char *bgp4mp_subtype_format;
  254. int len;
  255. date=gmtime(&entry->time);
  256. time2str(date,time_str_fixed);
  257. if (mode == 1) {
  258. // Timestamp mode
  259. len = sprintf(time_str, "%lld", (long long)entry->time);
  260. } else {
  261. len = time2str(date,time_str);
  262. }
  263. // Appending microseconds to time_str if needed
  264. if (entry->type == BGPDUMP_TYPE_ZEBRA_BGP_ET) {
  265. sprintf(time_str + len, ".%06ld", entry->ms);
  266. }
  267. if (mode==0)
  268. {
  269. printf("TIME: %s\n", time_str);
  270. }
  271. //printf("TIME: %s",asctime(gmtime(&entry->time)));
  272. //printf("LENGTH : %u\n", entry->length);
  273. switch(entry->type) {
  274. case BGPDUMP_TYPE_MRTD_TABLE_DUMP:
  275. if(mode==0){
  276. const char *prefix_str = NULL;
  277. switch(entry->subtype){
  278. #ifdef BGPDUMP_HAVE_IPV6
  279. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6:
  280. printf("TYPE: TABLE_DUMP/INET6\n");
  281. prefix_str = fmt_ipv6(entry->body.mrtd_table_dump.prefix,prefix);
  282. break;
  283. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6_32BIT_AS:
  284. printf("TYPE: TABLE_DUMP/INET6_32BIT_AS\n");
  285. prefix_str = fmt_ipv6(entry->body.mrtd_table_dump.prefix,prefix);
  286. break;
  287. #endif
  288. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP:
  289. printf("TYPE: TABLE_DUMP/INET\n");
  290. prefix_str = inet_ntoa(entry->body.mrtd_table_dump.prefix.v4_addr);
  291. break;
  292. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP_32BIT_AS:
  293. printf("TYPE: TABLE_DUMP/INET_32BIT_AS\n");
  294. prefix_str = inet_ntoa(entry->body.mrtd_table_dump.prefix.v4_addr);
  295. break;
  296. default:
  297. printf("Error: unknown table type %d\n", entry->subtype);
  298. return;
  299. }
  300. printf("VIEW: %d\n",entry->body.mrtd_table_dump.view);
  301. printf("SEQUENCE: %d\n",entry->body.mrtd_table_dump.sequence);
  302. printf("PREFIX: %s/%d\n",prefix_str,entry->body.mrtd_table_dump.mask);
  303. printf("FROM:");
  304. switch(entry->subtype)
  305. {
  306. #ifdef BGPDUMP_HAVE_IPV6
  307. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6:
  308. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6_32BIT_AS:
  309. fmt_ipv6(entry->body.mrtd_table_dump.peer_ip,prefix);
  310. printf("%s ",prefix);
  311. break;
  312. #endif
  313. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP:
  314. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP_32BIT_AS:
  315. if (entry->body.mrtd_table_dump.peer_ip.v4_addr.s_addr != 0x00000000L)
  316. printf("%s ",inet_ntoa(entry->body.mrtd_table_dump.peer_ip.v4_addr));
  317. else
  318. printf("N/A ");
  319. }
  320. printf("AS%u\n",entry->body.mrtd_table_dump.peer_as);
  321. //printf("FROM: %s AS%d\n",inet_ntoa(entry->body.mrtd_table_dump.peer_ip.v4_addr),entry->body.mrtd_table_dump.peer_as);
  322. //time2str(localtime(&entry->body.mrtd_table_dump.uptime),time_str2);
  323. time2str(gmtime(&entry->body.mrtd_table_dump.uptime),time_str2);
  324. printf("ORIGINATED: %s\n",time_str2);
  325. if (entry->attr && entry->attr->len)
  326. show_attr(entry->attr);
  327. printf("STATUS: 0x%x\n",entry->body.mrtd_table_dump.status);
  328. //printf(" UPTIME : %s",asctime(gmtime(&entry->body.mrtd_table_dump.uptime)));
  329. //printf(" PEER IP : %s\n",inet_ntoa(entry->body.mrtd_table_dump.peer_ip));
  330. //printf(" PEER IP : %s\n",inet_ntoa(entry->body.mrtd_table_dump.peer_ip.v4_addr));
  331. //printf(" PEER AS : %d\n",entry->body.mrtd_table_dump.peer_as);
  332. }
  333. else if (mode ==1 || mode ==2) // -m -M
  334. {
  335. table_line_mrtd_route(&entry->body.mrtd_table_dump,entry);
  336. }
  337. break;
  338. case BGPDUMP_TYPE_TABLE_DUMP_V2:
  339. if(mode == 0){
  340. char peer_ip[BGPDUMP_ADDRSTRLEN];
  341. //char time_str[30];
  342. int i;
  343. int addpath = is_addpath(entry);
  344. BGPDUMP_TABLE_DUMP_V2_PREFIX *e;
  345. e = &entry->body.mrtd_table_dump_v2_prefix;
  346. if(e->afi == AFI_IP){
  347. strncpy(prefix, inet_ntoa(e->prefix.v4_addr), BGPDUMP_ADDRSTRLEN);
  348. #ifdef BGPDUMP_HAVE_IPV6
  349. } else if(e->afi == AFI_IP6){
  350. fmt_ipv6(e->prefix, prefix);
  351. #endif
  352. }
  353. for(i = 0; i < e->entry_count; i++){
  354. // This is slightly nasty - as we want to print multiple entries
  355. // for multiple peers, we may need to print another TIME ourselves
  356. if(i) printf("\nTIME: %s\n",time_str_fixed);
  357. if(e->afi == AFI_IP){
  358. if (addpath)
  359. printf("TYPE: TABLE_DUMP_V2/IPV4_UNICAST_ADDPATH\n");
  360. else
  361. printf("TYPE: TABLE_DUMP_V2/IPV4_UNICAST\n");
  362. #ifdef BGPDUMP_HAVE_IPV6
  363. } else if(e->afi == AFI_IP6){
  364. if (addpath)
  365. printf("TYPE: TABLE_DUMP_V2/IPV6_UNICAST_ADDPATH\n");
  366. else
  367. printf("TYPE: TABLE_DUMP_V2/IPV6_UNICAST\n");
  368. #endif
  369. }
  370. printf("PREFIX: %s/%d",prefix, e->prefix_length);
  371. if (addpath)
  372. printf(" PATH_ID: %u", e->entries[i].path_id);
  373. printf("\n");
  374. printf("SEQUENCE: %d\n",e->seq);
  375. if(e->entries[i].peer->afi == AFI_IP){
  376. fmt_ipv4(e->entries[i].peer->peer_ip, peer_ip);
  377. #ifdef BGPDUMP_HAVE_IPV6
  378. } else if (e->entries[i].peer->afi == AFI_IP6){
  379. fmt_ipv6(e->entries[i].peer->peer_ip, peer_ip);
  380. #endif
  381. } else {
  382. sprintf(peer_ip, "[N/A, unsupported AF]");
  383. }
  384. printf("FROM: %s AS%u\n", peer_ip, e->entries[i].peer->peer_as);
  385. time_t time_temp = (time_t)((e->entries[i]).originated_time);
  386. time2str(gmtime(&time_temp),time_str);
  387. printf("ORIGINATED: %s\n",time_str);
  388. if (e->entries[i].attr && e->entries[i].attr->len)
  389. show_attr(e->entries[i].attr);
  390. }
  391. } else if (mode==1 || mode==2) { // -m -M
  392. table_line_dump_v2_prefix(&entry->body.mrtd_table_dump_v2_prefix,entry);
  393. }
  394. break;
  395. case BGPDUMP_TYPE_MRTD_BGP:
  396. process_bgpdump_mrtd_bgp(entry);
  397. break;
  398. case BGPDUMP_TYPE_ZEBRA_BGP:
  399. case BGPDUMP_TYPE_ZEBRA_BGP_ET:
  400. if (entry->type == BGPDUMP_TYPE_ZEBRA_BGP) {
  401. bgp4mp_format = "BGP4MP";
  402. } else {
  403. bgp4mp_format = "BGP4MP_ET";
  404. }
  405. int addpath = is_addpath(entry);
  406. switch(entry->subtype)
  407. {
  408. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE:
  409. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4:
  410. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_LOCAL:
  411. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4_LOCAL:
  412. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_ADDPATH:
  413. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4_ADDPATH:
  414. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_LOCAL_ADDPATH:
  415. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4_LOCAL_ADDPATH:
  416. if (addpath) {
  417. if (bgp4mp_message_direction_receive(entry))
  418. bgp4mp_subtype_format = "MESSAGE_ADDPATH";
  419. else
  420. bgp4mp_subtype_format = "MESSAGE_LOCAL_ADDPATH";
  421. } else {
  422. if (bgp4mp_message_direction_receive(entry))
  423. bgp4mp_subtype_format = "MESSAGE";
  424. else
  425. bgp4mp_subtype_format = "MESSAGE_LOCAL";
  426. }
  427. switch(entry->body.zebra_message.type)
  428. {
  429. case BGP_MSG_UPDATE:
  430. if (mode ==0)
  431. {
  432. printf("TYPE: %s/%s/Update\n", bgp4mp_format, bgp4mp_subtype_format);
  433. if (entry->body.zebra_message.source_as)
  434. {
  435. printf("FROM:");
  436. switch(entry->body.zebra_message.address_family)
  437. {
  438. #ifdef BGPDUMP_HAVE_IPV6
  439. case AFI_IP6:
  440. fmt_ipv6(entry->body.zebra_message.source_ip,prefix);
  441. printf(" %s ",prefix);
  442. break;
  443. #endif
  444. case AFI_IP:
  445. default:
  446. if (entry->body.zebra_message.source_ip.v4_addr.s_addr != 0x00000000L)
  447. printf(" %s ",inet_ntoa(entry->body.zebra_message.source_ip.v4_addr));
  448. else
  449. printf(" N/A ");
  450. }
  451. printf("AS%u\n",entry->body.zebra_message.source_as);
  452. }
  453. if (entry->body.zebra_message.destination_as)
  454. {
  455. printf("TO:");
  456. switch(entry->body.zebra_message.address_family)
  457. {
  458. #ifdef BGPDUMP_HAVE_IPV6
  459. case AFI_IP6:
  460. fmt_ipv6(entry->body.zebra_message.destination_ip,prefix);
  461. printf(" %s ",prefix);
  462. break;
  463. #endif
  464. case AFI_IP:
  465. default:
  466. if (entry->body.zebra_message.destination_ip.v4_addr.s_addr != 0x00000000L)
  467. printf(" %s ",inet_ntoa(entry->body.zebra_message.destination_ip.v4_addr));
  468. else
  469. printf(" N/A ");
  470. }
  471. printf("AS%u\n",entry->body.zebra_message.destination_as);
  472. }
  473. if (entry->attr && entry->attr->len)
  474. show_attr(entry->attr);
  475. if (entry->body.zebra_message.cut_bytes)
  476. {
  477. u_int16_t cutted,idx;
  478. u_int8_t buf[128];
  479. printf(" INCOMPLETE PACKET: %d bytes cutted\n",entry->body.zebra_message.cut_bytes);
  480. printf(" INCOMPLETE PART: ");
  481. if (entry->body.zebra_message.incomplete.orig_len)
  482. {
  483. cutted=entry->body.zebra_message.incomplete.prefix.len/8+1;
  484. buf[0]=entry->body.zebra_message.incomplete.orig_len;
  485. memcpy(buf+1,&entry->body.zebra_message.incomplete.prefix.address,cutted-1);
  486. for (idx=0;idx<cutted;idx++)
  487. {
  488. if (buf[idx]<0x10)
  489. printf("0%x ",buf[idx]);
  490. else
  491. printf("%x ",buf[idx]);
  492. }
  493. }
  494. printf("\n");
  495. }
  496. if(! entry->attr)
  497. return;
  498. if ((entry->body.zebra_message.withdraw_count) || (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI)))
  499. {
  500. #ifdef BGPDUMP_HAVE_IPV6
  501. if ((entry->body.zebra_message.withdraw_count)||(entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST] && entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST]->prefix_count) || (entry->attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST]->prefix_count) || (entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST]->prefix_count) ||(entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST] && entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST]->prefix_count) || (entry->attr->mp_info->withdraw[AFI_IP6][SAFI_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP6][SAFI_MULTICAST]->prefix_count) || (entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST_MULTICAST]->prefix_count) )
  502. #else
  503. if ((entry->body.zebra_message.withdraw_count)||(entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST] && entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST]->prefix_count) || (entry->attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST]->prefix_count) || (entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST]->prefix_count))
  504. #endif
  505. printf("WITHDRAW\n");
  506. if (entry->body.zebra_message.withdraw_count)
  507. // old style
  508. show_prefixes(entry->body.zebra_message.withdraw_count,entry->body.zebra_message.withdraw, addpath);
  509. // MP
  510. if (entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST] && entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST]->prefix_count)
  511. show_prefixes(entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST]->prefix_count,entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST]->nlri, addpath);
  512. if (entry->attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST]->prefix_count)
  513. show_prefixes(entry->attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST]->prefix_count,entry->attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST]->nlri, addpath);
  514. if (entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST]->prefix_count)
  515. show_prefixes(entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST]->prefix_count,entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST]->nlri, addpath);
  516. #ifdef BGPDUMP_HAVE_IPV6
  517. if (entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST] && entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST]->prefix_count)
  518. show_prefixes6(entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST]->prefix_count,entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST]->nlri, addpath);
  519. if (entry->attr->mp_info->withdraw[AFI_IP6][SAFI_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP6][SAFI_MULTICAST]->prefix_count)
  520. show_prefixes6(entry->attr->mp_info->withdraw[AFI_IP6][SAFI_MULTICAST]->prefix_count,entry->attr->mp_info->withdraw[AFI_IP6][SAFI_MULTICAST]->nlri, addpath);
  521. if (entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST_MULTICAST]->prefix_count)
  522. show_prefixes6(entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST_MULTICAST]->prefix_count,entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST_MULTICAST]->nlri, addpath);
  523. #endif
  524. }
  525. if ( (entry->body.zebra_message.announce_count) || (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI)))
  526. {
  527. printf("ANNOUNCE\n");
  528. if (entry->body.zebra_message.announce_count)
  529. // old style
  530. show_prefixes(entry->body.zebra_message.announce_count,entry->body.zebra_message.announce, addpath);
  531. // MP
  532. if (entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST] && entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->prefix_count)
  533. show_prefixes(entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->prefix_count,entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nlri, addpath);
  534. if (entry->attr->mp_info->announce[AFI_IP][SAFI_MULTICAST] && entry->attr->mp_info->announce[AFI_IP][SAFI_MULTICAST]->prefix_count)
  535. show_prefixes(entry->attr->mp_info->announce[AFI_IP][SAFI_MULTICAST]->prefix_count,entry->attr->mp_info->announce[AFI_IP][SAFI_MULTICAST]->nlri, addpath);
  536. if (entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST] && entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST]->prefix_count)
  537. show_prefixes(entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST]->prefix_count,entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST]->nlri, addpath);
  538. #ifdef BGPDUMP_HAVE_IPV6
  539. if (entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST] && entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->prefix_count)
  540. show_prefixes6(entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->prefix_count,entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nlri, addpath);
  541. if (entry->attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST] && entry->attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST]->prefix_count)
  542. show_prefixes6(entry->attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST]->prefix_count,entry->attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST]->nlri, addpath);
  543. if (entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST] && entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST]->prefix_count)
  544. show_prefixes6(entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST]->prefix_count,entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST]->nlri, addpath);
  545. #endif
  546. }
  547. }
  548. else if (mode == 1 || mode == 2) //-m -M
  549. {
  550. if ((entry->body.zebra_message.withdraw_count) || (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI)))
  551. {
  552. // old style
  553. table_line_withdraw(entry->body.zebra_message.withdraw,entry->body.zebra_message.withdraw_count,entry,time_str);
  554. // MP
  555. if (entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST] && entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST]->prefix_count)
  556. table_line_withdraw(entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST]->nlri,entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST]->prefix_count,entry,time_str);
  557. if (entry->attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST]->prefix_count)
  558. table_line_withdraw(entry->attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST]->nlri,entry->attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST]->prefix_count,entry,time_str);
  559. if (entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST]->prefix_count)
  560. table_line_withdraw(entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST]->nlri,entry->attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST]->prefix_count,entry,time_str);
  561. #ifdef BGPDUMP_HAVE_IPV6
  562. if (entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST] && entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST]->prefix_count)
  563. table_line_withdraw6(entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST]->nlri,entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST]->prefix_count,entry,time_str);
  564. if (entry->attr->mp_info->withdraw[AFI_IP6][SAFI_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP6][SAFI_MULTICAST]->prefix_count)
  565. table_line_withdraw6(entry->attr->mp_info->withdraw[AFI_IP6][SAFI_MULTICAST]->nlri,entry->attr->mp_info->withdraw[AFI_IP6][SAFI_MULTICAST]->prefix_count,entry,time_str);
  566. if (entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST_MULTICAST] && entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST_MULTICAST]->prefix_count)
  567. table_line_withdraw6(entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST_MULTICAST]->nlri,entry->attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST_MULTICAST]->prefix_count,entry,time_str);
  568. #endif
  569. }
  570. if ( (entry->body.zebra_message.announce_count) || (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI)))
  571. {
  572. // old style
  573. table_line_announce(entry->body.zebra_message.announce,entry->body.zebra_message.announce_count,entry,time_str);
  574. // MP
  575. if (entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST] && entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->prefix_count)
  576. table_line_announce_1(entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST],entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->prefix_count,entry,time_str);
  577. if (entry->attr->mp_info->announce[AFI_IP][SAFI_MULTICAST] && entry->attr->mp_info->announce[AFI_IP][SAFI_MULTICAST]->prefix_count)
  578. table_line_announce_1(entry->attr->mp_info->announce[AFI_IP][SAFI_MULTICAST],entry->attr->mp_info->announce[AFI_IP][SAFI_MULTICAST]->prefix_count,entry,time_str);
  579. if (entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST] && entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST]->prefix_count)
  580. table_line_announce_1(entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST],entry->attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST]->prefix_count,entry,time_str);
  581. #ifdef BGPDUMP_HAVE_IPV6
  582. if (entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST] && entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->prefix_count)
  583. table_line_announce6(entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST],entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->prefix_count,entry,time_str);
  584. if (entry->attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST] && entry->attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST]->prefix_count)
  585. table_line_announce6(entry->attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST],entry->attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST]->prefix_count,entry,time_str);
  586. if (entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST] && entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST]->prefix_count)
  587. table_line_announce6(entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST],entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST]->prefix_count,entry,time_str);
  588. #endif
  589. }
  590. }
  591. break;
  592. case BGP_MSG_OPEN:
  593. if (mode != 0)
  594. break;
  595. printf("TYPE: %s/%s/Open\n", bgp4mp_format, bgp4mp_subtype_format);
  596. if (entry->body.zebra_message.source_as)
  597. {
  598. printf("FROM:");
  599. switch(entry->body.zebra_message.address_family)
  600. {
  601. #ifdef BGPDUMP_HAVE_IPV6
  602. case AFI_IP6:
  603. fmt_ipv6(entry->body.zebra_message.source_ip,prefix);
  604. printf(" %s ",prefix);
  605. break;
  606. #endif
  607. case AFI_IP:
  608. default:
  609. if (entry->body.zebra_message.source_ip.v4_addr.s_addr != 0x00000000L)
  610. printf(" %s ",inet_ntoa(entry->body.zebra_message.source_ip.v4_addr));
  611. else
  612. printf(" N/A ");
  613. }
  614. printf("AS%u\n",entry->body.zebra_message.source_as);
  615. }
  616. if (entry->body.zebra_message.destination_as)
  617. {
  618. printf("TO:");
  619. switch(entry->body.zebra_message.address_family)
  620. {
  621. #ifdef BGPDUMP_HAVE_IPV6
  622. case AFI_IP6:
  623. fmt_ipv6(entry->body.zebra_message.destination_ip,prefix);
  624. printf(" %s ",prefix);
  625. break;
  626. #endif
  627. case AFI_IP:
  628. default:
  629. if (entry->body.zebra_message.destination_ip.v4_addr.s_addr != 0x00000000L)
  630. printf(" %s ",inet_ntoa(entry->body.zebra_message.destination_ip.v4_addr));
  631. else
  632. printf(" N/A ");
  633. }
  634. printf("AS%u\n",entry->body.zebra_message.destination_as);
  635. }
  636. printf("VERSION: %d\n",entry->body.zebra_message.version);
  637. printf("AS: %u\n",entry->body.zebra_message.my_as);
  638. printf("HOLD_TIME: %d\n",entry->body.zebra_message.hold_time);
  639. printf("ID: %s\n",inet_ntoa(entry->body.zebra_message.bgp_id));
  640. printf("OPT_PARM_LEN: %d\n",entry->body.zebra_message.opt_len);
  641. break;
  642. case BGP_MSG_NOTIFY:
  643. if (mode != 0)
  644. break;
  645. printf("TYPE: %s/%s/Notify\n", bgp4mp_format, bgp4mp_subtype_format);
  646. if (entry->body.zebra_message.source_as)
  647. {
  648. printf("FROM:");
  649. switch(entry->body.zebra_message.address_family)
  650. {
  651. #ifdef BGPDUMP_HAVE_IPV6
  652. case AFI_IP6:
  653. fmt_ipv6(entry->body.zebra_message.source_ip,prefix);
  654. printf(" %s ",prefix);
  655. break;
  656. #endif
  657. case AFI_IP:
  658. default:
  659. if (entry->body.zebra_message.source_ip.v4_addr.s_addr != 0x00000000L)
  660. printf(" %s ",inet_ntoa(entry->body.zebra_message.source_ip.v4_addr));
  661. else
  662. printf(" N/A ");
  663. }
  664. printf("AS%u\n",entry->body.zebra_message.source_as);
  665. }
  666. if (entry->body.zebra_message.destination_as)
  667. {
  668. printf("TO:");
  669. switch(entry->body.zebra_message.address_family)
  670. {
  671. #ifdef BGPDUMP_HAVE_IPV6
  672. case AFI_IP6:
  673. fmt_ipv6(entry->body.zebra_message.destination_ip,prefix);
  674. printf(" %s ",prefix);
  675. break;
  676. #endif
  677. case AFI_IP:
  678. default:
  679. if (entry->body.zebra_message.destination_ip.v4_addr.s_addr != 0x00000000L)
  680. printf(" %s ",inet_ntoa(entry->body.zebra_message.destination_ip.v4_addr));
  681. else
  682. printf(" N/A ");
  683. }
  684. printf("AS%u\n",entry->body.zebra_message.destination_as);
  685. }
  686. switch (entry->body.zebra_message.error_code)
  687. {
  688. case 1:
  689. printf(" ERROR CODE : 1 (Message Header Error)\n");
  690. switch(entry->body.zebra_message.sub_error_code)
  691. {
  692. case 1:
  693. printf(" SUB ERROR : 1 (Connection Not Synchronized)\n");
  694. break;
  695. case 2:
  696. printf(" SUB ERROR : 2 (Bad Message Length)\n");
  697. break;
  698. case 3:
  699. printf(" SUB ERROR : 3 (Bad Message Type)\n");
  700. break;
  701. default:
  702. printf(" SUB ERROR : %d\n",entry->body.zebra_message.sub_error_code);
  703. break;
  704. }
  705. break;
  706. case 2:
  707. printf(" ERROR CODE : 2 (OPEN Message Error)\n");
  708. switch(entry->body.zebra_message.sub_error_code)
  709. {
  710. case 1:
  711. printf(" SUB ERROR : 1 (Unsupported Version Number)\n");
  712. break;
  713. case 2:
  714. printf(" SUB ERROR : 2 (Bad Peer AS)\n");
  715. break;
  716. case 3:
  717. printf(" SUB ERROR : 3 (Bad BGP Identifier)\n");
  718. break;
  719. case 4:
  720. printf(" SUB ERROR : 4 (Unsupported Optional Parameter)\n");
  721. break;
  722. case 5:
  723. printf(" SUB ERROR : 5 (Authentication Failure)\n");
  724. break;
  725. case 6:
  726. printf(" SUB ERROR : 6 (Unacceptable Hold Time)\n");
  727. break;
  728. default:
  729. printf(" SUB ERROR : %d\n",entry->body.zebra_message.sub_error_code);
  730. break;
  731. }
  732. break;
  733. case 3:
  734. printf(" ERROR CODE : 3 (UPDATE Message Error)\n");
  735. switch(entry->body.zebra_message.sub_error_code)
  736. {
  737. case 1:
  738. printf(" SUB ERROR : 1 (Malformed Attribute List)\n");
  739. break;
  740. case 2:
  741. printf(" SUB ERROR : 2 (Unrecognized Well-known Attribute)\n");
  742. break;
  743. case 3:
  744. printf(" SUB ERROR : 3 (Missing Well-known Attribute)\n");
  745. break;
  746. case 4:
  747. printf(" SUB ERROR : 4 (Attribute Flags Error)\n");
  748. break;
  749. case 5:
  750. printf(" SUB ERROR : 5 (Attribute Length Error)\n");
  751. break;
  752. case 6:
  753. printf(" SUB ERROR : 6 (Invalid ORIGIN Attribute)\n");
  754. break;
  755. case 7:
  756. printf(" SUB ERROR : 7 (AS Routing Loop)\n");
  757. break;
  758. case 8:
  759. printf(" SUB ERROR : 8 (Invalid NEXT-HOP Attribute)\n");
  760. break;
  761. case 9:
  762. printf(" SUB ERROR : 9 (Optional Attribute Error)\n");
  763. break;
  764. case 10:
  765. printf(" SUB ERROR : 10 (Invalid Network Field)\n");
  766. break;
  767. case 11:
  768. printf(" SUB ERROR : 11 (Malformed AS-PATH)\n");
  769. break;
  770. default:
  771. printf(" SUB ERROR : %d\n",entry->body.zebra_message.sub_error_code);
  772. break;
  773. }
  774. break;
  775. case 4:
  776. printf(" ERROR CODE : 4 (Hold Timer Expired)\n");
  777. break;
  778. case 5:
  779. printf(" ERROR CODE : 5 (Finite State Machine Error)\n");
  780. break;
  781. case 6:
  782. printf(" ERROR CODE : 6 (Cease)\n");
  783. break;
  784. default:
  785. printf(" ERROR CODE : %d\n",entry->body.zebra_message.error_code);
  786. break;
  787. }
  788. break;
  789. case BGP_MSG_KEEPALIVE:
  790. if ( mode != 0)
  791. break;
  792. printf("TYPE: %s/%s/Keepalive\n", bgp4mp_format, bgp4mp_subtype_format);
  793. if (entry->body.zebra_message.source_as)
  794. {
  795. printf("FROM:");
  796. switch(entry->body.zebra_message.address_family)
  797. {
  798. #ifdef BGPDUMP_HAVE_IPV6
  799. case AFI_IP6:
  800. fmt_ipv6(entry->body.zebra_message.source_ip,prefix);
  801. printf(" %s ",prefix);
  802. break;
  803. #endif
  804. case AFI_IP:
  805. default:
  806. if (entry->body.zebra_message.source_ip.v4_addr.s_addr != 0x00000000L)
  807. printf(" %s ",inet_ntoa(entry->body.zebra_message.source_ip.v4_addr));
  808. else
  809. printf(" N/A ");
  810. }
  811. printf("AS%u\n",entry->body.zebra_message.source_as);
  812. }
  813. if (entry->body.zebra_message.destination_as)
  814. {
  815. printf("TO:");
  816. switch(entry->body.zebra_message.address_family)
  817. {
  818. #ifdef BGPDUMP_HAVE_IPV6
  819. case AFI_IP6:
  820. fmt_ipv6(entry->body.zebra_message.destination_ip,prefix);
  821. printf(" %s ",prefix);
  822. break;
  823. #endif
  824. case AFI_IP:
  825. default:
  826. if (entry->body.zebra_message.destination_ip.v4_addr.s_addr != 0x00000000L)
  827. printf(" %s ",inet_ntoa(entry->body.zebra_message.destination_ip.v4_addr));
  828. else
  829. printf(" N/A ");
  830. }
  831. printf("AS%u\n",entry->body.zebra_message.destination_as);
  832. }
  833. break;
  834. }
  835. break;
  836. case BGPDUMP_SUBTYPE_ZEBRA_BGP_STATE_CHANGE:
  837. case BGPDUMP_SUBTYPE_ZEBRA_BGP_STATE_CHANGE_AS4:
  838. if (mode==0)
  839. {
  840. printf("TYPE: %s/STATE_CHANGE\n", bgp4mp_format);
  841. printf("PEER:");
  842. switch(entry->body.zebra_state_change.address_family)
  843. {
  844. #ifdef BGPDUMP_HAVE_IPV6
  845. case AFI_IP6:
  846. fmt_ipv6(entry->body.zebra_state_change.source_ip,prefix);
  847. printf(" %s ",prefix);
  848. break;
  849. #endif
  850. case AFI_IP:
  851. default:
  852. if (entry->body.zebra_state_change.source_ip.v4_addr.s_addr != 0x00000000L)
  853. printf(" %s ",inet_ntoa(entry->body.zebra_message.source_ip.v4_addr));
  854. else
  855. printf(" N/A ");
  856. }
  857. //if (entry->body.zebra_message.source_ip.s_addr != 0x00000000L)
  858. // printf(" %s ",inet_ntoa(entry->body.zebra_message.source_ip));
  859. //else
  860. // printf(" N/A ");
  861. printf("AS%u\n",entry->body.zebra_state_change.source_as);
  862. printf("STATE: %s/%s\n",get_bgp_state_name(entry->body.zebra_state_change.old_state),get_bgp_state_name(entry->body.zebra_state_change.new_state));
  863. }
  864. else if (mode==1 || mode==2 ) //-m -M
  865. {
  866. switch(entry->body.zebra_state_change.address_family)
  867. {
  868. #ifdef BGPDUMP_HAVE_IPV6
  869. case AFI_IP6:
  870. fmt_ipv6(entry->body.zebra_state_change.source_ip, prefix);
  871. break;
  872. #endif
  873. case AFI_IP:
  874. default:
  875. sprintf(prefix, "%s", inet_ntoa(entry->body.zebra_state_change.source_ip.v4_addr));
  876. break;
  877. }
  878. show_line_prefix(bgp4mp_format, time_str, "STATE");
  879. printf("%s|%u|%d|%d\n", prefix,
  880. entry->body.zebra_state_change.source_as,
  881. entry->body.zebra_state_change.old_state,
  882. entry->body.zebra_state_change.new_state);
  883. }
  884. break;
  885. }
  886. break;
  887. }
  888. if (mode==0)
  889. printf("\n");
  890. }
  891. // print line prefix with respect of mode and show_packet_index flags
  892. // output will have format:
  893. // format|[packet_index|]time|type|
  894. void show_line_prefix(const char* format, const char* time_str, const char* type)
  895. {
  896. printf("%s|", format);
  897. if (show_packet_index)
  898. printf("%d|", packet_index);
  899. printf("%s|%s|", time_str, type);
  900. }
  901. void process_bgpdump_mrtd_bgp(BGPDUMP_ENTRY *entry) {
  902. struct tm *date;
  903. char time_str[128];
  904. date=gmtime(&entry->time);
  905. if (mode == 1) {
  906. sprintf(time_str, "%lld", (long long)entry->time);
  907. } else {
  908. time2str(date, time_str);
  909. }
  910. switch (entry->subtype) {
  911. case BGPDUMP_SUBTYPE_MRTD_BGP_UPDATE:
  912. case BGPDUMP_SUBTYPE_MRTD_BGP_KEEPALIVE:
  913. if (mode == 0) {
  914. if (entry->subtype == BGPDUMP_SUBTYPE_MRTD_BGP_UPDATE)
  915. printf("TYPE: BGP/MESSAGE/Update\n");
  916. else
  917. printf("TYPE: BGP/MESSAGE/Keepalive\n");
  918. if (entry->body.mrtd_message.source_as) {
  919. printf("FROM:");
  920. show_ipv4_address(entry->body.mrtd_message.source_ip);
  921. printf("AS%u\n", entry->body.mrtd_message.source_as);
  922. }
  923. if (entry->body.mrtd_message.destination_as) {
  924. printf("TO:");
  925. show_ipv4_address(entry->body.mrtd_message.destination_ip);
  926. printf("AS%u\n", entry->body.mrtd_message.destination_as);
  927. }
  928. if (entry->attr && entry->attr->len)
  929. show_attr(entry->attr);
  930. if (entry->body.mrtd_message.withdraw_count) {
  931. printf("WITHDRAW\n");
  932. show_prefixes(entry->body.mrtd_message.withdraw_count, entry->body.mrtd_message.withdraw, 0);
  933. }
  934. if (entry->body.mrtd_message.announce_count) {
  935. printf("ANNOUNCE\n");
  936. show_prefixes(entry->body.mrtd_message.announce_count, entry->body.mrtd_message.announce, 0);
  937. }
  938. }
  939. else {
  940. if (entry->subtype != BGPDUMP_SUBTYPE_MRTD_BGP_UPDATE)
  941. break;
  942. if (entry->body.mrtd_message.withdraw_count)
  943. mrtd_table_line_withdraw(entry->body.mrtd_message.withdraw, entry->body.mrtd_message.withdraw_count,
  944. entry, time_str);
  945. if (entry->body.mrtd_message.announce_count)
  946. mrtd_table_line_announce(entry->body.mrtd_message.announce, entry->body.mrtd_message.announce_count,
  947. entry, time_str);
  948. }
  949. break;
  950. case BGPDUMP_SUBTYPE_MRTD_BGP_STATE_CHANGE:
  951. if (mode == 0) {
  952. printf("TYPE: BGP/STATE_CHANGE\n");
  953. printf("PEER:");
  954. show_ipv4_address(entry->body.mrtd_state_change.destination_ip);
  955. printf("AS%u\n", entry->body.mrtd_state_change.destination_as);
  956. printf("STATE: %s/%s\n", get_bgp_state_name(entry->body.mrtd_state_change.old_state),
  957. get_bgp_state_name(entry->body.mrtd_state_change.new_state));
  958. }
  959. else if (mode == 1 || mode == 2) {
  960. show_line_prefix("BGP", time_str, "STATE");
  961. printf("%s|%u|%d|%d\n",
  962. inet_ntoa(entry->body.mrtd_state_change.destination_ip),
  963. entry->body.mrtd_state_change.destination_as,
  964. entry->body.mrtd_state_change.old_state,
  965. entry->body.mrtd_state_change.new_state);
  966. }
  967. break;
  968. default:
  969. if (mode == 0) {
  970. printf("TYPE: Subtype %d not supported yet\n", entry->subtype);
  971. }
  972. }
  973. }
  974. void mrtd_table_line_withdraw(struct prefix *prefix, int count, BGPDUMP_ENTRY *entry, char *time_str) {
  975. int i;
  976. for (i = 0; i < count; i++) {
  977. show_line_prefix("BGP", time_str, "W");
  978. printf("%s|%u|%s/%d\n", inet_ntoa(entry->body.mrtd_message.source_ip),
  979. entry->body.mrtd_message.source_as,
  980. inet_ntoa(prefix[i].address.v4_addr), prefix[i].len);
  981. }
  982. }
  983. void mrtd_table_line_announce(struct prefix *prefix, int count, BGPDUMP_ENTRY *entry, char *time_str) {
  984. int i, npref, nmed;
  985. char *aggregate = entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE) ? "AG" : "NAG";
  986. for (i = 0; i < count; i++) {
  987. show_line_prefix("BGP", time_str, "A");
  988. printf("%s|%u", inet_ntoa(entry->body.mrtd_message.source_ip),
  989. entry->body.mrtd_message.source_as);
  990. printf("|%s/%d|%s|%s", inet_ntoa(prefix[i].address.v4_addr), prefix[i].len,
  991. attr_aspath(entry->attr), describe_origin(entry->attr->origin));
  992. if (mode == 1) {
  993. npref = ((entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)) == 0) ? 0 : entry->attr->local_pref;
  994. nmed = ((entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)) == 0) ? 0 : entry->attr->med;
  995. printf("|%s|%u|%u", inet_ntoa(entry->attr->nexthop), npref, nmed);
  996. if ((entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES)) != 0)
  997. printf("%s|%s|", entry->attr->community->str+1, aggregate);
  998. else
  999. printf("|%s|", aggregate);
  1000. if (entry->attr->aggregator_addr.s_addr != -1)
  1001. printf("%u %s|\n", entry->attr->aggregator_as, inet_ntoa(entry->attr->aggregator_addr));
  1002. else
  1003. printf("|\n");
  1004. }
  1005. else
  1006. printf("\n");
  1007. }
  1008. }
  1009. void show_ipv4_address(struct in_addr ip) {
  1010. if (ip.s_addr != 0x00000000L)
  1011. printf(" %s ", inet_ntoa(ip));
  1012. else
  1013. printf(" N/A ");
  1014. }
  1015. void show_attr(attributes_t *attr) {
  1016. if(attr != NULL) {
  1017. if( (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ORIGIN) ) !=0 )
  1018. {
  1019. switch (attr->origin)
  1020. {
  1021. case 0:
  1022. printf("ORIGIN: IGP\n");
  1023. break;
  1024. case 1:
  1025. printf("ORIGIN: EGP\n");
  1026. break;
  1027. case 2:
  1028. printf("ORIGIN: INCOMPLETE\n");
  1029. }
  1030. }
  1031. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_AS_PATH) ) !=0)
  1032. printf("ASPATH: %s\n",attr->aspath->str);
  1033. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP) ) !=0)
  1034. printf("NEXT_HOP: %s\n",inet_ntoa(attr->nexthop));
  1035. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC) ) !=0)
  1036. printf("MULTI_EXIT_DISC: %u\n",attr->med);
  1037. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF) ) !=0)
  1038. printf("LOCAL_PREF: %u\n",attr->local_pref);
  1039. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE) ) !=0)
  1040. printf("ATOMIC_AGGREGATE\n");
  1041. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR) ) !=0)
  1042. printf("AGGREGATOR: AS%u %s\n",attr->aggregator_as,inet_ntoa(attr->aggregator_addr));
  1043. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID) ) !=0)
  1044. printf("ORIGINATOR_ID: %s\n",inet_ntoa(attr->originator_id));
  1045. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST) ) !=0)
  1046. {
  1047. int cluster_index;
  1048. printf("CLUSTER_LIST: ");
  1049. for (cluster_index = 0;cluster_index<attr->cluster->length;cluster_index++)
  1050. printf("%s ",inet_ntoa(attr->cluster->list[cluster_index]));
  1051. printf("\n");
  1052. }
  1053. int idx;
  1054. for (idx=0;idx<attr->unknown_num;idx++)
  1055. {
  1056. struct unknown_attr *unknown = attr->unknown + idx;
  1057. printf(" UNKNOWN_ATTR(%i, %i, %i):", unknown->flag, unknown->type, unknown->len);
  1058. int b;
  1059. for(b = 0; b < unknown->len; ++b)
  1060. printf(" %02x", unknown->raw[b]);
  1061. printf("\n");
  1062. }
  1063. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI) )!=0)
  1064. {
  1065. printf("MP_REACH_NLRI");
  1066. #ifdef BGPDUMP_HAVE_IPV6
  1067. if (attr->mp_info->announce[AFI_IP6][SAFI_UNICAST] || attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST] || attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST])
  1068. {
  1069. char buf[128];
  1070. if (attr->mp_info->announce[AFI_IP6][SAFI_UNICAST])
  1071. {
  1072. printf("(IPv6 Unicast)\n");
  1073. if (attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop_len==4)
  1074. printf("NEXT_HOP: %s\n",inet_ntoa(attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop.v4_addr));
  1075. if (attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop_len==16)
  1076. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop,buf));
  1077. if (attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop_len==32) {
  1078. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop,buf));
  1079. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop_local,buf));
  1080. }
  1081. }
  1082. else if (attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST])
  1083. {
  1084. printf("(IPv6 Multicast)\n");
  1085. if (attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST]->nexthop_len==4)
  1086. printf("NEXT_HOP: %s\n",inet_ntoa(attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST]->nexthop.v4_addr));
  1087. if (attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST]->nexthop_len==16)
  1088. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST]->nexthop,buf));
  1089. if (attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST]->nexthop_len==32) {
  1090. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST]->nexthop,buf));
  1091. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP6][SAFI_MULTICAST]->nexthop_local,buf));
  1092. }
  1093. }
  1094. else
  1095. {
  1096. printf("(IPv6 Both unicast and multicast)\n");
  1097. if (attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST]->nexthop_len==4)
  1098. printf("NEXT_HOP: %s\n",inet_ntoa(attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST]->nexthop.v4_addr));
  1099. if (attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST]->nexthop_len==16)
  1100. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST]->nexthop,buf));
  1101. if (attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST]->nexthop_len==32) {
  1102. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST]->nexthop,buf));
  1103. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP6][SAFI_UNICAST_MULTICAST]->nexthop_local,buf));
  1104. }
  1105. }
  1106. }
  1107. else
  1108. #endif
  1109. {
  1110. char buf[128];
  1111. if (attr->mp_info->announce[AFI_IP][SAFI_UNICAST])
  1112. {
  1113. printf("(IPv4 Unicast)\n");
  1114. if (attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop_len==4)
  1115. printf("NEXT_HOP: %s\n",inet_ntoa(attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop.v4_addr));
  1116. if (attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop_len==16)
  1117. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop,buf));
  1118. if (attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop_len==32) {
  1119. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop,buf));
  1120. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop_local,buf));
  1121. }
  1122. }
  1123. else if (attr->mp_info->announce[AFI_IP][SAFI_MULTICAST])
  1124. {
  1125. printf("(IPv4 Multicast)\n");
  1126. if (attr->mp_info->announce[AFI_IP][SAFI_MULTICAST]->nexthop_len==4)
  1127. printf("NEXT_HOP: %s\n",inet_ntoa(attr->mp_info->announce[AFI_IP][SAFI_MULTICAST]->nexthop.v4_addr));
  1128. if (attr->mp_info->announce[AFI_IP][SAFI_MULTICAST]->nexthop_len==16)
  1129. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP][SAFI_MULTICAST]->nexthop,buf));
  1130. if (attr->mp_info->announce[AFI_IP][SAFI_MULTICAST]->nexthop_len==32) {
  1131. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP][SAFI_MULTICAST]->nexthop,buf));
  1132. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP][SAFI_MULTICAST]->nexthop_local,buf));
  1133. }
  1134. }
  1135. else if (attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST])
  1136. {
  1137. printf("(IPv4 Both unicast and multicast)\n");
  1138. if (attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST]->nexthop_len==4)
  1139. printf("NEXT_HOP: %s\n",inet_ntoa(attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST]->nexthop.v4_addr));
  1140. if (attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST]->nexthop_len==16)
  1141. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST]->nexthop,buf));
  1142. if (attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST]->nexthop_len==32) {
  1143. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST]->nexthop,buf));
  1144. printf("NEXT_HOP: %s\n",fmt_ipv6(attr->mp_info->announce[AFI_IP][SAFI_UNICAST_MULTICAST]->nexthop_local,buf));
  1145. }
  1146. }
  1147. }
  1148. }
  1149. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI) )!=0)
  1150. {
  1151. printf("MP_UNREACH_NLRI");
  1152. #ifdef BGPDUMP_HAVE_IPV6
  1153. if (attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST] || attr->mp_info->withdraw[AFI_IP6][SAFI_MULTICAST] || attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST_MULTICAST])
  1154. {
  1155. if (attr->mp_info->withdraw[AFI_IP6][SAFI_UNICAST])
  1156. {
  1157. printf("(IPv6 Unicast)\n");
  1158. }
  1159. else if (attr->mp_info->withdraw[AFI_IP6][SAFI_MULTICAST])
  1160. {
  1161. printf("(IPv6 Multicast)\n");
  1162. }
  1163. else
  1164. {
  1165. printf("(IPv6 Both unicast and multicast)\n");
  1166. }
  1167. }
  1168. else
  1169. #endif
  1170. {
  1171. if (attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST])
  1172. {
  1173. printf("(IPv4 Unicast)\n");
  1174. }
  1175. else if (attr->mp_info->withdraw[AFI_IP][SAFI_MULTICAST])
  1176. {
  1177. printf("(IPv4 Multicast)\n");
  1178. }
  1179. else if (attr->mp_info->withdraw[AFI_IP][SAFI_UNICAST_MULTICAST])
  1180. {
  1181. printf("(IPv4 Both unicast and multicast)\n");
  1182. }
  1183. }
  1184. }
  1185. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES) ) !=0)
  1186. printf("COMMUNITY:%s\n",attr->community->str);
  1187. }
  1188. }
  1189. void show_prefixes(int count,struct prefix *prefix, int addpath) {
  1190. int i;
  1191. for(i=0;i<count;i++)
  1192. if (addpath)
  1193. printf(" %s/%d PATH_ID: %u\n",inet_ntoa(prefix[i].address.v4_addr),prefix[i].len, prefix[i].path_id);
  1194. else
  1195. printf(" %s/%d\n",inet_ntoa(prefix[i].address.v4_addr),prefix[i].len);
  1196. }
  1197. #ifdef BGPDUMP_HAVE_IPV6
  1198. void show_prefixes6(int count,struct prefix *prefix, int addpath)
  1199. {
  1200. int i;
  1201. char buf[128];
  1202. for (i=0;i<count;i++)
  1203. if (addpath)
  1204. printf(" %s/%d PATH_ID: %u\n",fmt_ipv6(prefix[i].address,buf),prefix[i].len, prefix[i].path_id);
  1205. else
  1206. printf(" %s/%d\n",fmt_ipv6(prefix[i].address,buf),prefix[i].len);
  1207. }
  1208. #endif
  1209. static void table_line_withdraw(struct prefix *prefix,int count,BGPDUMP_ENTRY *entry,char *time_str)
  1210. {
  1211. int idx;
  1212. char buf[128];
  1213. for (idx=0;idx<count;idx++)
  1214. {
  1215. show_line_prefix(get_bgp4mp_format(entry), time_str, "W");
  1216. switch(entry->body.zebra_message.address_family) {
  1217. #ifdef BGPDUMP_HAVE_IPV6
  1218. case AFI_IP6:
  1219. bgp4mp_message_direction_receive(entry)
  1220. ? printf("%s|%u|",
  1221. fmt_ipv6(entry->body.zebra_message.source_ip,buf),
  1222. entry->body.zebra_message.source_as)
  1223. : printf("%s|%u|",
  1224. fmt_ipv6(entry->body.zebra_message.destination_ip,buf),
  1225. entry->body.zebra_message.destination_as);
  1226. break;
  1227. #endif
  1228. case AFI_IP:
  1229. default:
  1230. bgp4mp_message_direction_receive(entry)
  1231. ? printf("%s|%u|",
  1232. inet_ntoa(entry->body.zebra_message.source_ip.v4_addr),
  1233. entry->body.zebra_message.source_as)
  1234. : printf("%s|%u|",
  1235. inet_ntoa(entry->body.zebra_message.destination_ip.v4_addr),
  1236. entry->body.zebra_message.destination_as);
  1237. break;
  1238. }
  1239. is_addpath(entry)
  1240. ? printf("%s/%d|%u\n",inet_ntoa(prefix[idx].address.v4_addr),prefix[idx].len,prefix[idx].path_id)
  1241. : printf("%s/%d\n",inet_ntoa(prefix[idx].address.v4_addr),prefix[idx].len);
  1242. }
  1243. }
  1244. #ifdef BGPDUMP_HAVE_IPV6
  1245. static void table_line_withdraw6(struct prefix *prefix,int count,BGPDUMP_ENTRY *entry,char *time_str)
  1246. {
  1247. int idx;
  1248. char buf[128];
  1249. char buf1[128];
  1250. for (idx=0;idx<count;idx++)
  1251. {
  1252. show_line_prefix(get_bgp4mp_format(entry), time_str, "W");
  1253. switch(entry->body.zebra_message.address_family) {
  1254. case AFI_IP6:
  1255. bgp4mp_message_direction_receive(entry)
  1256. ? printf("%s|%u|%s/%d",
  1257. fmt_ipv6(entry->body.zebra_message.source_ip,buf1),
  1258. entry->body.zebra_message.source_as,
  1259. fmt_ipv6(prefix[idx].address,buf),prefix[idx].len)
  1260. : printf("%s|%u|%s/%d",
  1261. fmt_ipv6(entry->body.zebra_message.destination_ip,buf1),
  1262. entry->body.zebra_message.destination_as,
  1263. fmt_ipv6(prefix[idx].address,buf),prefix[idx].len);
  1264. break;
  1265. case AFI_IP:
  1266. default:
  1267. bgp4mp_message_direction_receive(entry)
  1268. ? printf("%s|%u|%s/%d",
  1269. fmt_ipv4(entry->body.zebra_message.source_ip,buf1),
  1270. entry->body.zebra_message.source_as,
  1271. fmt_ipv6(prefix[idx].address,buf),prefix[idx].len)
  1272. : printf("%s|%u|%s/%d",
  1273. fmt_ipv6(entry->body.zebra_message.destination_ip,buf1),
  1274. entry->body.zebra_message.destination_as,
  1275. fmt_ipv6(prefix[idx].address,buf),prefix[idx].len);
  1276. break;
  1277. }
  1278. is_addpath(entry)
  1279. ? printf("|%u\n", prefix[idx].path_id)
  1280. : printf("\n");
  1281. }
  1282. }
  1283. #endif
  1284. static void table_line_announce(struct prefix *prefix,int count,BGPDUMP_ENTRY *entry,char *time_str)
  1285. {
  1286. int idx ;
  1287. char buf[128];
  1288. char tmp2[20];
  1289. unsigned int npref;
  1290. unsigned int nmed;
  1291. if (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE))
  1292. sprintf(tmp2,"AG");
  1293. else
  1294. sprintf(tmp2,"NAG");
  1295. for (idx=0;idx<count;idx++)
  1296. {
  1297. show_line_prefix(get_bgp4mp_format(entry), time_str, "A");
  1298. if (mode == 1)
  1299. {
  1300. switch(entry->body.zebra_message.address_family)
  1301. {
  1302. #ifdef BGPDUMP_HAVE_IPV6
  1303. case AFI_IP6:
  1304. bgp4mp_message_direction_receive(entry)
  1305. ? printf("%s|%u|",fmt_ipv6(entry->body.zebra_message.source_ip,buf),entry->body.zebra_message.source_as)
  1306. : printf("%s|%u|",fmt_ipv6(entry->body.zebra_message.destination_ip,buf),entry->body.zebra_message.destination_as);
  1307. break;
  1308. #endif
  1309. case AFI_IP:
  1310. default:
  1311. bgp4mp_message_direction_receive(entry)
  1312. ? printf("%s|%u|",inet_ntoa(entry->body.zebra_message.source_ip.v4_addr),entry->body.zebra_message.source_as)
  1313. : printf("%s|%u|",inet_ntoa(entry->body.zebra_message.destination_ip.v4_addr),entry->body.zebra_message.destination_as);
  1314. break;
  1315. }
  1316. is_addpath(entry)
  1317. ? printf("%s/%d|%u|%s|%s|",inet_ntoa(prefix[idx].address.v4_addr),prefix[idx].len,prefix[idx].path_id,attr_aspath(entry->attr),describe_origin(entry->attr->origin))
  1318. : printf("%s/%d|%s|%s|",inet_ntoa(prefix[idx].address.v4_addr),prefix[idx].len,attr_aspath(entry->attr),describe_origin(entry->attr->origin));
  1319. npref=entry->attr->local_pref;
  1320. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF) ) ==0)
  1321. npref=0;
  1322. nmed=entry->attr->med;
  1323. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC) ) ==0)
  1324. nmed=0;
  1325. printf("%s|%u|%u|",inet_ntoa(entry->attr->nexthop),npref,nmed);
  1326. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES) ) !=0)
  1327. printf("%s|%s|",entry->attr->community->str+1,tmp2);
  1328. else
  1329. printf("|%s|",tmp2);
  1330. if (entry->attr->aggregator_addr.s_addr != -1)
  1331. printf("%u %s|\n",entry->attr->aggregator_as,inet_ntoa(entry->attr->aggregator_addr));
  1332. else
  1333. printf("|\n");
  1334. }
  1335. else
  1336. {
  1337. switch(entry->body.zebra_message.address_family)
  1338. {
  1339. #ifdef BGPDUMP_HAVE_IPV6
  1340. case AFI_IP6:
  1341. bgp4mp_message_direction_receive(entry)
  1342. ? printf("%s|%u|",fmt_ipv6(entry->body.zebra_message.source_ip,buf),entry->body.zebra_message.source_as)
  1343. : printf("%s|%u|",fmt_ipv6(entry->body.zebra_message.destination_ip,buf),entry->body.zebra_message.destination_as);
  1344. break;
  1345. #endif
  1346. case AFI_IP:
  1347. default:
  1348. bgp4mp_message_direction_receive(entry)
  1349. ? printf("%s|%u|",inet_ntoa(entry->body.zebra_message.source_ip.v4_addr),entry->body.zebra_message.source_as)
  1350. : printf("%s|%u|",inet_ntoa(entry->body.zebra_message.destination_ip.v4_addr),entry->body.zebra_message.destination_as);
  1351. break;
  1352. }
  1353. is_addpath(entry)
  1354. ? printf("%s/%d|%u|%s|%s\n",inet_ntoa(prefix[idx].address.v4_addr),prefix[idx].len,prefix[idx].path_id,attr_aspath(entry->attr),describe_origin(entry->attr->origin))
  1355. : printf("%s/%d|%s|%s\n",inet_ntoa(prefix[idx].address.v4_addr),prefix[idx].len,attr_aspath(entry->attr),describe_origin(entry->attr->origin));
  1356. }
  1357. }
  1358. }
  1359. static void table_line_announce_1(struct mp_nlri *prefix,int count,BGPDUMP_ENTRY *entry,char *time_str)
  1360. {
  1361. int idx ;
  1362. char buf[128];
  1363. char tmp2[20];
  1364. unsigned int npref;
  1365. unsigned int nmed;
  1366. if (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE))
  1367. sprintf(tmp2,"AG");
  1368. else
  1369. sprintf(tmp2,"NAG");
  1370. for (idx=0;idx<count;idx++)
  1371. {
  1372. show_line_prefix(get_bgp4mp_format(entry), time_str, "A");
  1373. if (mode == 1)
  1374. {
  1375. if (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI))
  1376. {
  1377. switch(entry->body.zebra_message.address_family)
  1378. {
  1379. #ifdef BGPDUMP_HAVE_IPV6
  1380. case AFI_IP6:
  1381. bgp4mp_message_direction_receive(entry)
  1382. ? printf("%s|%u|",fmt_ipv6(entry->body.zebra_message.source_ip,buf),entry->body.zebra_message.source_as)
  1383. : printf("%s|%u|",fmt_ipv6(entry->body.zebra_message.destination_ip,buf),entry->body.zebra_message.destination_as);
  1384. break;
  1385. #endif
  1386. case AFI_IP:
  1387. default:
  1388. bgp4mp_message_direction_receive(entry)
  1389. ? printf("%s|%u|",inet_ntoa(entry->body.zebra_message.source_ip.v4_addr),entry->body.zebra_message.source_as)
  1390. : printf("%s|%u|",inet_ntoa(entry->body.zebra_message.destination_ip.v4_addr),entry->body.zebra_message.destination_as);
  1391. break;
  1392. }
  1393. is_addpath(entry)
  1394. ? printf("%s/%d|%u|%s|%s|",inet_ntoa(prefix->nlri[idx].address.v4_addr),prefix->nlri[idx].len,prefix->nlri[idx].path_id,attr_aspath(entry->attr),describe_origin(entry->attr->origin))
  1395. : printf("%s/%d|%s|%s|",inet_ntoa(prefix->nlri[idx].address.v4_addr),prefix->nlri[idx].len,attr_aspath(entry->attr),describe_origin(entry->attr->origin));
  1396. npref=entry->attr->local_pref;
  1397. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF) ) ==0)
  1398. npref=0;
  1399. nmed=entry->attr->med;
  1400. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC) ) ==0)
  1401. nmed=0;
  1402. if (prefix->nexthop_len == 4)
  1403. printf("%s|%d|%d|",inet_ntoa(prefix->nexthop.v4_addr),npref,nmed);
  1404. else if ((prefix->nexthop_len == 16) || (prefix->nexthop_len == 32))
  1405. printf("%s|%d|%d|",fmt_ipv6(prefix->nexthop,buf),npref,nmed);
  1406. //printf("%s|%d|%d|",inet_ntoa(prefix->nexthop.v4_addr),entry->attr->local_pref,entry->attr->med);
  1407. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES) ) !=0)
  1408. printf("%s|%s|",entry->attr->community->str+1,tmp2);
  1409. else
  1410. printf("|%s|",tmp2);
  1411. }
  1412. else
  1413. {
  1414. // I'm not convinced this code block is actually ever executed
  1415. // it appears to handle old-style (non-MP) yet this function isn't
  1416. // called for that type.....
  1417. // cpetrie@ 2016-08-12
  1418. switch(entry->body.zebra_message.address_family)
  1419. {
  1420. #ifdef BGPDUMP_HAVE_IPV6
  1421. case AFI_IP6:
  1422. bgp4mp_message_direction_receive(entry)
  1423. ? printf("%s|%u|",fmt_ipv6(entry->body.zebra_message.source_ip,buf),entry->body.zebra_message.source_as)
  1424. : printf("%s|%u|",fmt_ipv6(entry->body.zebra_message.destination_ip,buf),entry->body.zebra_message.destination_as);
  1425. break;
  1426. #endif
  1427. case AFI_IP:
  1428. default:
  1429. bgp4mp_message_direction_receive(entry)
  1430. ? printf("%s|%u|",inet_ntoa(entry->body.zebra_message.source_ip.v4_addr),entry->body.zebra_message.source_as)
  1431. : printf("%s|%u|",inet_ntoa(entry->body.zebra_message.destination_ip.v4_addr),entry->body.zebra_message.destination_as);
  1432. break;
  1433. }
  1434. is_addpath(entry)
  1435. ? printf("%s/%d|%u|%s|%s|",inet_ntoa(prefix->nlri[idx].address.v4_addr),prefix->nlri[idx].len,prefix->nlri[idx].path_id,attr_aspath(entry->attr),describe_origin(entry->attr->origin))
  1436. : printf("%s/%d|%s|%s|",inet_ntoa(prefix->nlri[idx].address.v4_addr),prefix->nlri[idx].len,attr_aspath(entry->attr),describe_origin(entry->attr->origin));
  1437. npref=entry->attr->local_pref;
  1438. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF) ) ==0)
  1439. npref=0;
  1440. nmed=entry->attr->med;
  1441. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC) ) ==0)
  1442. nmed=0;
  1443. printf("%s|%d|%d|",inet_ntoa(entry->attr->nexthop),npref,nmed);
  1444. //printf("%s|%d|%d|",inet_ntoa(entry->attr->nexthop),entry->attr->local_pref,entry->attr->med);
  1445. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES) ) !=0)
  1446. printf("%s|%s|",entry->attr->community->str+1,tmp2);
  1447. else
  1448. printf("|%s|",tmp2);
  1449. }
  1450. if (entry->attr->aggregator_addr.s_addr != -1)
  1451. printf("%u %s|\n",entry->attr->aggregator_as,inet_ntoa(entry->attr->aggregator_addr));
  1452. else
  1453. printf("|\n");
  1454. }
  1455. else
  1456. {
  1457. switch(entry->body.zebra_message.address_family)
  1458. {
  1459. #ifdef BGPDUMP_HAVE_IPV6
  1460. case AFI_IP6:
  1461. bgp4mp_message_direction_receive(entry)
  1462. ? printf("%s|%u|",fmt_ipv6(entry->body.zebra_message.source_ip,buf),entry->body.zebra_message.source_as)
  1463. : printf("%s|%u|",fmt_ipv6(entry->body.zebra_message.destination_ip,buf),entry->body.zebra_message.destination_as);
  1464. break;
  1465. #endif
  1466. case AFI_IP:
  1467. default:
  1468. bgp4mp_message_direction_receive(entry)
  1469. ? printf("%s|%u|",inet_ntoa(entry->body.zebra_message.source_ip.v4_addr),entry->body.zebra_message.source_as)
  1470. : printf("%s|%u|",inet_ntoa(entry->body.zebra_message.destination_ip.v4_addr),entry->body.zebra_message.destination_as);
  1471. break;
  1472. }
  1473. is_addpath(entry)
  1474. ? printf("%s/%d|%u|%s|%s\n",inet_ntoa(prefix->nlri[idx].address.v4_addr),prefix->nlri[idx].len,prefix->nlri[idx].path_id,attr_aspath(entry->attr),describe_origin(entry->attr->origin))
  1475. : printf("%s/%d|%s|%s\n",inet_ntoa(prefix->nlri[idx].address.v4_addr),prefix->nlri[idx].len,attr_aspath(entry->attr),describe_origin(entry->attr->origin));
  1476. }
  1477. }
  1478. }
  1479. #ifdef BGPDUMP_HAVE_IPV6
  1480. static void table_line_announce6(struct mp_nlri *prefix,int count,BGPDUMP_ENTRY *entry,char *time_str)
  1481. {
  1482. int idx ;
  1483. char buf[128];
  1484. char buf1[128];
  1485. char buf2[128];
  1486. char tmp2[20];
  1487. unsigned int npref;
  1488. unsigned int nmed;
  1489. if (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE))
  1490. sprintf(tmp2,"AG");
  1491. else
  1492. sprintf(tmp2,"NAG");
  1493. for (idx=0;idx<count;idx++)
  1494. {
  1495. show_line_prefix(get_bgp4mp_format(entry), time_str, "A");
  1496. if (mode == 1)
  1497. {
  1498. switch(entry->body.zebra_message.address_family)
  1499. {
  1500. case AFI_IP6:
  1501. npref=entry->attr->local_pref;
  1502. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF) ) ==0)
  1503. npref=0;
  1504. nmed=entry->attr->med;
  1505. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC) ) ==0)
  1506. nmed=0;
  1507. bgp4mp_message_direction_receive(entry)
  1508. ? printf("%s|%u|%s/%d|",fmt_ipv6(entry->body.zebra_message.source_ip,buf1),entry->body.zebra_message.source_as,fmt_ipv6(prefix->nlri[idx].address,buf2),prefix->nlri[idx].len)
  1509. : printf("%s|%u|%s/%d|",fmt_ipv6(entry->body.zebra_message.destination_ip,buf1),entry->body.zebra_message.destination_as,fmt_ipv6(prefix->nlri[idx].address,buf2),prefix->nlri[idx].len);
  1510. break;
  1511. case AFI_IP:
  1512. default:
  1513. npref=entry->attr->local_pref;
  1514. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF) ) ==0)
  1515. npref=0;
  1516. nmed=entry->attr->med;
  1517. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC) ) ==0)
  1518. nmed=0;
  1519. //printf("%s|%d|%d|",inet_ntoa(entry->attr->nexthop),nprof,nmed);
  1520. bgp4mp_message_direction_receive(entry)
  1521. ? printf("%s|%u|%s/%d|",fmt_ipv4(entry->body.zebra_message.source_ip,buf1),entry->body.zebra_message.source_as,fmt_ipv6(prefix->nlri[idx].address,buf2),prefix->nlri[idx].len)
  1522. : printf("%s|%u|%s/%d|",fmt_ipv4(entry->body.zebra_message.destination_ip,buf1),entry->body.zebra_message.destination_as,fmt_ipv6(prefix->nlri[idx].address,buf2),prefix->nlri[idx].len);
  1523. break;
  1524. }
  1525. if (is_addpath(entry))
  1526. printf("%u|", prefix->nlri[idx].path_id);
  1527. if (prefix->nexthop_len == 4)
  1528. printf("%s|%s|%s|%u|%u|", attr_aspath(entry->attr),describe_origin(entry->attr->origin),inet_ntoa(prefix->nexthop.v4_addr),npref,nmed);
  1529. else if ((prefix->nexthop_len == 16) || (prefix->nexthop_len == 32))
  1530. printf("%s|%s|%s|%u|%u|", attr_aspath(entry->attr),describe_origin(entry->attr->origin),fmt_ipv6(prefix->nexthop,buf),npref,nmed);
  1531. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES) ) !=0)
  1532. printf("%s|%s|",entry->attr->community->str+1,tmp2);
  1533. else
  1534. printf("|%s|",tmp2);
  1535. if (entry->attr->aggregator_addr.s_addr != -1)
  1536. printf("%u %s|\n",entry->attr->aggregator_as,inet_ntoa(entry->attr->aggregator_addr));
  1537. else
  1538. printf("|\n");
  1539. }
  1540. else
  1541. {
  1542. switch(entry->body.zebra_message.address_family)
  1543. {
  1544. case AFI_IP6:
  1545. bgp4mp_message_direction_receive(entry)
  1546. ? printf("%s|%u|%s/%d|",fmt_ipv6(entry->body.zebra_message.source_ip,buf1),entry->body.zebra_message.source_as,fmt_ipv6(prefix->nlri[idx].address,buf),prefix->nlri[idx].len)
  1547. : printf("%s|%u|%s/%d|",fmt_ipv6(entry->body.zebra_message.destination_ip,buf1),entry->body.zebra_message.destination_as,fmt_ipv6(prefix->nlri[idx].address,buf),prefix->nlri[idx].len);
  1548. break;
  1549. case AFI_IP:
  1550. default:
  1551. bgp4mp_message_direction_receive(entry)
  1552. ? printf("%s|%u|%s/%d|",fmt_ipv4(entry->body.zebra_message.source_ip,buf1),entry->body.zebra_message.source_as,fmt_ipv6(prefix->nlri[idx].address,buf),prefix->nlri[idx].len)
  1553. : printf("%s|%u|%s/%d|",fmt_ipv4(entry->body.zebra_message.destination_ip,buf1),entry->body.zebra_message.destination_as,fmt_ipv6(prefix->nlri[idx].address,buf),prefix->nlri[idx].len);
  1554. break;
  1555. }
  1556. if (is_addpath(entry))
  1557. printf("%u|", prefix->nlri[idx].path_id);
  1558. printf("%s|%s\n",attr_aspath(entry->attr),describe_origin(entry->attr->origin));
  1559. }
  1560. }
  1561. }
  1562. #endif
  1563. static void table_line_mrtd_route(BGPDUMP_MRTD_TABLE_DUMP *route,BGPDUMP_ENTRY *entry)
  1564. {
  1565. struct tm *date = NULL;
  1566. char tmp2[20];
  1567. unsigned int npref;
  1568. unsigned int nmed;
  1569. char time_str[20];
  1570. char peer[BGPDUMP_ADDRSTRLEN], prefix[BGPDUMP_ADDRSTRLEN], nexthop[BGPDUMP_ADDRSTRLEN];
  1571. if (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE))
  1572. sprintf(tmp2,"AG");
  1573. else
  1574. sprintf(tmp2,"NAG");
  1575. time_t *t;
  1576. if (timetype==0) {
  1577. t = &entry->time;
  1578. } else {
  1579. t = &route->uptime;
  1580. }
  1581. if (mode == 1) {
  1582. sprintf(time_str, "%lld", (long long)*t);
  1583. } else {
  1584. date=gmtime(t);
  1585. time2str(date, time_str);
  1586. }
  1587. show_line_prefix("TABLE_DUMP", time_str, "B");
  1588. #ifdef BGPDUMP_HAVE_IPV6
  1589. if (entry->subtype == AFI_IP6)
  1590. {
  1591. fmt_ipv6(route->peer_ip,peer);
  1592. fmt_ipv6(route->prefix,prefix);
  1593. }
  1594. else
  1595. #endif
  1596. {
  1597. strncpy(peer, inet_ntoa(route->peer_ip.v4_addr), BGPDUMP_ADDRSTRLEN);
  1598. strncpy(prefix, inet_ntoa(route->prefix.v4_addr), BGPDUMP_ADDRSTRLEN);
  1599. }
  1600. if (mode == 1)
  1601. {
  1602. printf("%s|%u|",peer,route->peer_as);
  1603. printf("%s/%d|%s|%s|",prefix,route->mask,
  1604. attr_aspath(entry->attr),describe_origin(entry->attr->origin));
  1605. npref=entry->attr->local_pref;
  1606. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF) ) ==0)
  1607. npref=0;
  1608. nmed=entry->attr->med;
  1609. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC) ) ==0)
  1610. nmed=0;
  1611. #ifdef BGPDUMP_HAVE_IPV6
  1612. if ((entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI)) && entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST])
  1613. {
  1614. fmt_ipv6(entry->attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop,nexthop);
  1615. }
  1616. else
  1617. #endif
  1618. {
  1619. strncpy(nexthop, inet_ntoa(entry->attr->nexthop), BGPDUMP_ADDRSTRLEN);
  1620. }
  1621. printf("%s|%u|%u|",nexthop,npref,nmed);
  1622. if( (entry->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES) ) !=0)
  1623. printf("%s|%s|",entry->attr->community->str+1,tmp2);
  1624. else
  1625. printf("|%s|",tmp2);
  1626. if (entry->attr->aggregator_addr.s_addr != -1)
  1627. printf("%u %s|\n",entry->attr->aggregator_as,inet_ntoa(entry->attr->aggregator_addr));
  1628. else
  1629. printf("|\n");
  1630. }
  1631. else
  1632. {
  1633. printf("%s|%u|",peer,route->peer_as);
  1634. printf("%s/%d|%s|%s\n",prefix,route->mask,attr_aspath(entry->attr),describe_origin(entry->attr->origin));
  1635. }
  1636. }
  1637. static char *describe_origin(int origin) {
  1638. if(origin == 0) return "IGP";
  1639. if(origin == 1) return "EGP";
  1640. return "INCOMPLETE";
  1641. }
  1642. static void table_line_dump_v2_prefix(BGPDUMP_TABLE_DUMP_V2_PREFIX *e,BGPDUMP_ENTRY *entry)
  1643. {
  1644. struct tm *date = NULL;
  1645. unsigned int npref;
  1646. unsigned int nmed;
  1647. char time_str[20];
  1648. char peer[BGPDUMP_ADDRSTRLEN], prefix[BGPDUMP_ADDRSTRLEN], nexthop[BGPDUMP_ADDRSTRLEN];
  1649. int i;
  1650. int addpath = is_addpath(entry);
  1651. for(i = 0; i < e->entry_count; i++) {
  1652. attributes_t *attr = e->entries[i].attr;
  1653. if(! attr)
  1654. continue;
  1655. char *origin = describe_origin(attr->origin);
  1656. char *aspath_str = (attr->aspath) ? attr->aspath->str: "";
  1657. char *aggregate = attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE) ? "AG" : "NAG";
  1658. if(e->entries[i].peer->afi == AFI_IP){
  1659. fmt_ipv4(e->entries[i].peer->peer_ip, peer);
  1660. #ifdef BGPDUMP_HAVE_IPV6
  1661. } else if(e->entries[i].peer->afi == AFI_IP6){
  1662. fmt_ipv6(e->entries[i].peer->peer_ip, peer);
  1663. #endif
  1664. }
  1665. if(e->afi == AFI_IP) {
  1666. fmt_ipv4(e->prefix, prefix);
  1667. #ifdef BGPDUMP_HAVE_IPV6
  1668. } else if(e->afi == AFI_IP6) {
  1669. fmt_ipv6(e->prefix, prefix);
  1670. #endif
  1671. }
  1672. time_t *t;
  1673. if (timetype==0) {
  1674. t = &entry->time;
  1675. } else {
  1676. time_t tmp = (time_t)((e->entries[i]).originated_time);
  1677. t = &tmp;
  1678. }
  1679. if (mode == 1) {
  1680. sprintf(time_str, "%lld", (long long)*t);
  1681. } else {
  1682. date=gmtime(t);
  1683. time2str(date, time_str);
  1684. }
  1685. (addpath)
  1686. ? show_line_prefix("TABLE_DUMP2_AP", time_str, "B")
  1687. : show_line_prefix("TABLE_DUMP2", time_str, "B");
  1688. if (mode == 1)
  1689. {
  1690. printf("%s|%u|",peer,e->entries[i].peer->peer_as);
  1691. (addpath)
  1692. ? printf("%s/%d|%u|%s|%s|",prefix,e->prefix_length,e->entries[i].path_id,aspath_str,origin)
  1693. : printf("%s/%d|%s|%s|",prefix,e->prefix_length,aspath_str,origin);
  1694. npref=attr->local_pref;
  1695. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF) ) ==0)
  1696. npref=0;
  1697. nmed=attr->med;
  1698. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC) ) ==0)
  1699. nmed=0;
  1700. if ((attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI)) && attr->mp_info->announce[AFI_IP6][SAFI_UNICAST])
  1701. {
  1702. if (attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop_len == 4)
  1703. strncpy(nexthop, inet_ntoa(attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop.v4_addr), BGPDUMP_ADDRSTRLEN);
  1704. else if (attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop_len == 16)
  1705. fmt_ipv6(attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop,nexthop);
  1706. else if (attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop_len == 32)
  1707. fmt_ipv6(attr->mp_info->announce[AFI_IP6][SAFI_UNICAST]->nexthop,nexthop);
  1708. }
  1709. else if ((attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI)) && attr->mp_info->announce[AFI_IP][SAFI_UNICAST])
  1710. {
  1711. if (attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop_len == 4)
  1712. strncpy(nexthop, inet_ntoa(attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop.v4_addr), BGPDUMP_ADDRSTRLEN);
  1713. else if (attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop_len == 16)
  1714. fmt_ipv6(attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop,nexthop);
  1715. else if (attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop_len == 32)
  1716. fmt_ipv6(attr->mp_info->announce[AFI_IP][SAFI_UNICAST]->nexthop,nexthop);
  1717. }
  1718. else
  1719. {
  1720. strncpy(nexthop, inet_ntoa(attr->nexthop), BGPDUMP_ADDRSTRLEN);
  1721. }
  1722. printf("%s|%u|%u|",nexthop,npref,nmed);
  1723. if( (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES) ) !=0)
  1724. printf("%s|%s|",attr->community->str+1,aggregate);
  1725. else
  1726. printf("|%s|",aggregate);
  1727. if (attr->aggregator_addr.s_addr != -1)
  1728. printf("%u %s|\n",attr->aggregator_as,inet_ntoa(attr->aggregator_addr));
  1729. else
  1730. printf("|\n");
  1731. }
  1732. else
  1733. {
  1734. printf("%s|%u|",peer,e->entries[i].peer->peer_as);
  1735. (addpath)
  1736. ? printf("%s/%d|%u|%s|%s\n",prefix,e->prefix_length,e->entries[i].path_id,aspath_str,origin)
  1737. : printf("%s/%d|%s|%s\n",prefix,e->prefix_length,aspath_str,origin);
  1738. }
  1739. }
  1740. }