bgpdump.c 81 KB

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