1
0

bgpdump.c 82 KB

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