bgpdump_lib.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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. */
  22. #include "bgpdump-config.h"
  23. #include "cfile_tools.h"
  24. #include "bgpdump_lib.h"
  25. #include "bgpdump_mstream.h"
  26. #include "util.h"
  27. #include <sys/stat.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <stdbool.h>
  31. #include <netinet/in.h>
  32. #include <sys/socket.h>
  33. #include <arpa/inet.h>
  34. #include <zlib.h>
  35. #include <assert.h>
  36. void bgpdump_free_attr(attributes_t *attr);
  37. static int process_mrtd_table_dump(struct mstream *s,BGPDUMP_ENTRY *entry);
  38. static int process_mrtd_table_dump_v2(struct mstream *s,BGPDUMP_ENTRY *entry);
  39. static int process_mrtd_table_dump_v2_peer_index_table(struct mstream *s,BGPDUMP_ENTRY *entry);
  40. static int process_mrtd_table_dump_v2_ipv4_unicast(struct mstream *s,BGPDUMP_ENTRY *entry);
  41. static int process_mrtd_table_dump_v2_ipv6_unicast(struct mstream *s,BGPDUMP_ENTRY *entry);
  42. static int process_zebra_bgp(struct mstream *s,BGPDUMP_ENTRY *entry);
  43. static int process_zebra_bgp_state_change(struct mstream *s,BGPDUMP_ENTRY *entry, u_int8_t asn_len);
  44. static int process_zebra_bgp_message(struct mstream *s,BGPDUMP_ENTRY *entry, u_int8_t asn_len);
  45. static int process_zebra_bgp_message_update(struct mstream *s,BGPDUMP_ENTRY *entry, u_int8_t asn_len);
  46. static int process_zebra_bgp_message_open(struct mstream *s,BGPDUMP_ENTRY *entry, u_int8_t asn_len);
  47. static int process_zebra_bgp_message_notify(struct mstream *s,BGPDUMP_ENTRY *entry);
  48. static int process_zebra_bgp_entry(struct mstream *s,BGPDUMP_ENTRY *entry);
  49. static int process_zebra_bgp_snapshot(struct mstream *s,BGPDUMP_ENTRY *entry);
  50. static attributes_t *process_attributes(struct mstream *s, u_int8_t asn_len, struct zebra_incomplete *incomplete);
  51. static void process_attr_aspath_string(struct aspath *as);
  52. static char aspath_delimiter_char (u_char type, u_char which);
  53. static void process_attr_community_string(struct community *com);
  54. static void process_mp_announce(struct mstream *s, struct mp_info *info, struct zebra_incomplete *incomplete);
  55. static void process_mp_withdraw(struct mstream *s, struct mp_info *info, struct zebra_incomplete *incomplete);
  56. static int read_prefix_list(struct mstream *s, u_int16_t af, struct prefix *prefixes, struct zebra_incomplete *incomplete);
  57. static as_t read_asn(struct mstream *s, as_t *asn, u_int8_t len);
  58. static struct aspath *create_aspath(u_int16_t len, u_int8_t asn_len);
  59. static void aspath_error(struct aspath *as);
  60. static int check_new_aspath(struct aspath *aspath);
  61. static void process_asn32_trans(attributes_t *attr, u_int8_t asn_len);
  62. static struct aspath *asn32_merge_paths(struct aspath *path, struct aspath *newpath);
  63. static void asn32_expand_16_to_32(char *dst, char *src, int len);
  64. BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE *table_dump_v2_peer_index_table = NULL;
  65. #if defined(linux)
  66. static size_t strlcat(char *dst, const char *src, size_t size);
  67. #endif
  68. char *bgpdump_version(void) {
  69. return PACKAGE_VERSION;
  70. }
  71. BGPDUMP *bgpdump_open_dump(const char *filename) {
  72. CFRFILE *f = cfr_open(filename);
  73. if(! f) {
  74. perror("can't open dumpfile");
  75. return NULL;
  76. }
  77. BGPDUMP *this_dump = malloc(sizeof(BGPDUMP));
  78. strcpy(this_dump->filename, "[STDIN]");
  79. if(filename && strcmp(filename, "-")) {
  80. strcpy(this_dump->filename, filename);
  81. }
  82. this_dump->f = f;
  83. this_dump->eof=0;
  84. this_dump->parsed = 0;
  85. this_dump->parsed_ok = 0;
  86. return this_dump;
  87. }
  88. void bgpdump_close_dump(BGPDUMP *dump) {
  89. if(dump!=NULL) {
  90. if(table_dump_v2_peer_index_table){
  91. if(table_dump_v2_peer_index_table->entries) {
  92. free(table_dump_v2_peer_index_table->entries);
  93. table_dump_v2_peer_index_table->entries = NULL;
  94. }
  95. free(table_dump_v2_peer_index_table);
  96. table_dump_v2_peer_index_table = NULL;
  97. }
  98. cfr_close(dump->f);
  99. free(dump);
  100. }
  101. }
  102. BGPDUMP_ENTRY* bgpdump_read_next(BGPDUMP *dump) {
  103. assert(dump);
  104. BGPDUMP_ENTRY *this_entry=NULL;
  105. struct mstream s;
  106. u_char *buffer;
  107. int ok=0;
  108. u_int32_t bytes_read;
  109. this_entry = malloc(sizeof(BGPDUMP_ENTRY));
  110. bytes_read = cfr_read_n(dump->f, &(this_entry->time), 4);
  111. bytes_read += cfr_read_n(dump->f, &(this_entry->type), 2);
  112. bytes_read += cfr_read_n(dump->f, &(this_entry->subtype), 2);
  113. bytes_read += cfr_read_n(dump->f, &(this_entry->length), 4);
  114. if (bytes_read == 12) {
  115. /* Intel byte ordering stuff ... */
  116. this_entry->type = ntohs(this_entry->type);
  117. this_entry->subtype = ntohs(this_entry->subtype);
  118. this_entry->time = ntohl(this_entry->time);
  119. this_entry->length = ntohl(this_entry->length);
  120. /* If Extended Header format, then reading the miscroseconds attribute */
  121. if (this_entry->type == BGPDUMP_TYPE_ZEBRA_BGP_ET) {
  122. bytes_read += cfr_read_n(dump->f, &(this_entry->ms), 4);
  123. if (bytes_read == 16) {
  124. this_entry->ms = ntohl(this_entry->ms);
  125. /* "The Microsecond Timestamp is included in the computation of
  126. * the Length field value." (RFC6396 2011) */
  127. this_entry->length -= 4;
  128. ok = 1;
  129. }
  130. } else {
  131. this_entry->ms = 0;
  132. ok = 1;
  133. }
  134. }
  135. if (!ok) {
  136. if(bytes_read > 0) {
  137. /* Malformed record */
  138. err("bgpdump_read_next: incomplete MRT header (%d bytes read, expecting 12 or 16)",
  139. bytes_read);
  140. }
  141. /* Nothing more to read, quit */
  142. free(this_entry);
  143. dump->eof = 1;
  144. return(NULL);
  145. }
  146. dump->parsed++;
  147. this_entry->attr=NULL;
  148. buffer = malloc(this_entry->length);
  149. bytes_read = cfr_read_n(dump->f, buffer, this_entry->length);
  150. if(bytes_read != this_entry->length) {
  151. err("bgpdump_read_next: incomplete dump record (%d bytes read, expecting %d)",
  152. bytes_read, this_entry->length);
  153. free(this_entry);
  154. free(buffer);
  155. dump->eof=1;
  156. return(NULL);
  157. }
  158. ok=0;
  159. mstream_init(&s,buffer,this_entry->length);
  160. switch(this_entry->type) {
  161. case BGPDUMP_TYPE_MRTD_BGP:
  162. ok = process_mrtd_bgp(&s,this_entry);
  163. break;
  164. case BGPDUMP_TYPE_MRTD_TABLE_DUMP:
  165. ok = process_mrtd_table_dump(&s,this_entry);
  166. break;
  167. case BGPDUMP_TYPE_ZEBRA_BGP:
  168. case BGPDUMP_TYPE_ZEBRA_BGP_ET:
  169. ok = process_zebra_bgp(&s,this_entry);
  170. break;
  171. case BGPDUMP_TYPE_TABLE_DUMP_V2:
  172. ok = process_mrtd_table_dump_v2(&s,this_entry);
  173. break;
  174. }
  175. free(buffer);
  176. if(ok) {
  177. dump->parsed_ok++;
  178. } else {
  179. bgpdump_free_mem(this_entry);
  180. return NULL;
  181. }
  182. return this_entry;
  183. }
  184. static void bgpdump_free_mp_info(struct mp_info *info) {
  185. u_int16_t afi;
  186. u_int8_t safi;
  187. for(afi = 1; afi <= BGPDUMP_MAX_AFI; afi++) {
  188. for(safi = 1; safi < BGPDUMP_MAX_SAFI; safi++) {
  189. if(info->announce[afi][safi])
  190. free(info->announce[afi][safi]);
  191. info->announce[afi][safi] = NULL;
  192. if(info->withdraw[afi][safi]) {
  193. free(info->withdraw[afi][safi]);
  194. info->withdraw[afi][safi] = NULL;
  195. }
  196. }
  197. }
  198. free(info);
  199. }
  200. void bgpdump_free_mem(BGPDUMP_ENTRY *entry) {
  201. if(entry!=NULL) {
  202. bgpdump_free_attr(entry->attr);
  203. switch(entry->type) {
  204. case BGPDUMP_TYPE_ZEBRA_BGP:
  205. switch(entry->subtype) {
  206. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE:
  207. switch(entry->body.zebra_message.type) {
  208. case BGP_MSG_NOTIFY:
  209. if(entry->body.zebra_message.notify_data)
  210. free(entry->body.zebra_message.notify_data);
  211. break;
  212. case BGP_MSG_OPEN:
  213. if(entry->body.zebra_message.opt_data)
  214. free(entry->body.zebra_message.opt_data);
  215. break;
  216. }
  217. break;
  218. }
  219. break;
  220. case BGPDUMP_TYPE_TABLE_DUMP_V2:
  221. if(entry->subtype == BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV4_UNICAST ||
  222. entry->subtype == BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV6_UNICAST ){
  223. BGPDUMP_TABLE_DUMP_V2_PREFIX *e;
  224. e = &entry->body.mrtd_table_dump_v2_prefix;
  225. int i;
  226. for(i = 0; i < e->entry_count; i++){
  227. bgpdump_free_attr(e->entries[i].attr);
  228. }
  229. free(e->entries);
  230. }
  231. break;
  232. }
  233. free(entry);
  234. }
  235. }
  236. void bgpdump_free_attr(attributes_t *attr){
  237. if(attr != NULL) {
  238. u_int16_t i;
  239. struct aspath *path, *pathstofree[3] = { attr->aspath, attr->old_aspath, attr->new_aspath };
  240. for(i = 0; i < sizeof(pathstofree) / sizeof(pathstofree[0]); i++) {
  241. path = pathstofree[i];
  242. if(path) {
  243. if(path->data)
  244. free(path->data);
  245. if(path->str)
  246. free(path->str);
  247. free(path);
  248. }
  249. }
  250. if(attr->community != NULL) {
  251. if(attr->community->val != NULL)
  252. free(attr->community->val);
  253. if(attr->community->str != NULL)
  254. free(attr->community->str);
  255. free(attr->community);
  256. }
  257. if(attr->data != NULL)
  258. free(attr->data);
  259. if(attr->mp_info != NULL)
  260. bgpdump_free_mp_info(attr->mp_info);
  261. if(attr->cluster != NULL) {
  262. free(attr->cluster->list);
  263. free(attr->cluster);
  264. }
  265. if (attr->unknown_num) {
  266. for (i = 0; i < attr->unknown_num; i++)
  267. free(attr->unknown[i].raw);
  268. free(attr->unknown);
  269. }
  270. free(attr);
  271. }
  272. }
  273. int process_mrtd_bgp(struct mstream *s, BGPDUMP_ENTRY *entry) {
  274. switch(entry->subtype) {
  275. case BGPDUMP_SUBTYPE_MRTD_BGP_UPDATE:
  276. case BGPDUMP_SUBTYPE_MRTD_BGP_KEEPALIVE:
  277. read_asn(s, &entry->body.mrtd_message.source_as, ASN16_LEN);
  278. entry->body.mrtd_message.source_ip = mstream_get_ipv4(s);
  279. read_asn(s, &entry->body.mrtd_message.destination_as, ASN16_LEN);
  280. entry->body.mrtd_message.destination_ip = mstream_get_ipv4(s);
  281. mstream_t withdraw_stream = mstream_copy(s, mstream_getw(s, NULL));
  282. entry->body.mrtd_message.withdraw_count = read_prefix_list(&withdraw_stream, AFI_IP,
  283. entry->body.mrtd_message.withdraw,
  284. &entry->body.mrtd_message.incomplete);
  285. entry->attr = process_attributes(s, ASN16_LEN, &entry->body.mrtd_message.incomplete);
  286. entry->body.mrtd_message.announce_count = read_prefix_list(s, AFI_IP,
  287. entry->body.mrtd_message.announce,
  288. &entry->body.mrtd_message.incomplete);
  289. break;
  290. case BGPDUMP_SUBTYPE_MRTD_BGP_STATE_CHANGE:
  291. read_asn(s, &entry->body.mrtd_state_change.destination_as, ASN16_LEN);
  292. entry->body.mrtd_state_change.destination_ip = mstream_get_ipv4(s);
  293. entry->body.mrtd_state_change.old_state = mstream_getw(s, NULL);
  294. entry->body.mrtd_state_change.new_state = mstream_getw(s, NULL);
  295. break;
  296. }
  297. return 1;
  298. }
  299. int process_mrtd_table_dump(struct mstream *s,BGPDUMP_ENTRY *entry) {
  300. int afi = entry->subtype;
  301. u_int8_t asn_len;
  302. u_int32_t temp_time = 0;
  303. mstream_getw(s,&entry->body.mrtd_table_dump.view);
  304. mstream_getw(s,&entry->body.mrtd_table_dump.sequence);
  305. switch(afi) {
  306. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP:
  307. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP_32BIT_AS:
  308. entry->body.mrtd_table_dump.prefix.v4_addr = mstream_get_ipv4(s);
  309. break;
  310. #ifdef BGPDUMP_HAVE_IPV6
  311. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6:
  312. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6_32BIT_AS:
  313. mstream_get(s, &entry->body.mrtd_table_dump.prefix.v6_addr.s6_addr, 16);
  314. break;
  315. #endif
  316. default:
  317. warn("process_mrtd_table_dump: unknown AFI %d", afi);
  318. mstream_get(s, NULL, mstream_can_read(s));
  319. return 0;
  320. }
  321. mstream_getc(s,&entry->body.mrtd_table_dump.mask);
  322. mstream_getc(s,&entry->body.mrtd_table_dump.status);
  323. mstream_getl(s,&temp_time);
  324. (entry->body).mrtd_table_dump.uptime = temp_time;
  325. switch(afi) {
  326. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP:
  327. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP_32BIT_AS:
  328. entry->body.mrtd_table_dump.peer_ip.v4_addr = mstream_get_ipv4(s);
  329. break;
  330. #ifdef BGPDUMP_HAVE_IPV6
  331. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6:
  332. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6_32BIT_AS:
  333. mstream_get(s, &entry->body.mrtd_table_dump.peer_ip.v6_addr.s6_addr, 16);
  334. break;
  335. #endif
  336. }
  337. switch(afi) {
  338. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP:
  339. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6:
  340. asn_len = ASN16_LEN;
  341. break;
  342. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP_32BIT_AS:
  343. case BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6_32BIT_AS:
  344. asn_len = ASN32_LEN;
  345. break;
  346. default:
  347. assert(0); // unreachable
  348. }
  349. read_asn(s,&entry->body.mrtd_table_dump.peer_as, asn_len);
  350. entry->attr = process_attributes(s, asn_len, NULL);
  351. return 1;
  352. }
  353. int process_mrtd_table_dump_v2(struct mstream *s,BGPDUMP_ENTRY *entry) {
  354. switch(entry->subtype){
  355. case BGPDUMP_SUBTYPE_TABLE_DUMP_V2_PEER_INDEX_TABLE:
  356. return process_mrtd_table_dump_v2_peer_index_table(s, entry);
  357. break;
  358. case BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV4_UNICAST:
  359. return process_mrtd_table_dump_v2_ipv4_unicast(s, entry);
  360. break;
  361. case BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV6_UNICAST:
  362. return process_mrtd_table_dump_v2_ipv6_unicast(s, entry);
  363. break;
  364. case BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_GENERIC:
  365. //return process_mrtd_table_dump_v2_generic(s, entry);
  366. break;
  367. }
  368. return 0;
  369. }
  370. int process_mrtd_table_dump_v2_peer_index_table(struct mstream *s,BGPDUMP_ENTRY *entry) {
  371. BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE *t;
  372. uint16_t i;
  373. uint8_t peertype;
  374. uint16_t view_name_len;
  375. if(table_dump_v2_peer_index_table){
  376. if(table_dump_v2_peer_index_table->entries)
  377. free(table_dump_v2_peer_index_table->entries);
  378. free(table_dump_v2_peer_index_table);
  379. }
  380. table_dump_v2_peer_index_table = malloc(sizeof(BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE));
  381. t = table_dump_v2_peer_index_table;
  382. t->entries = NULL;
  383. t->local_bgp_id = mstream_get_ipv4(s);
  384. mstream_getw(s,&view_name_len);
  385. strcpy(t->view_name, "");
  386. // view_name_len is without trailing \0
  387. if(view_name_len+1 > BGPDUMP_TYPE_TABLE_DUMP_V2_MAX_VIEWNAME_LEN) {
  388. warn("process_mrtd_table_dump_v2_peer_index_table: view name length more than maximum length (%d), ignoring view name", BGPDUMP_TYPE_TABLE_DUMP_V2_MAX_VIEWNAME_LEN);
  389. } else {
  390. mstream_get(s, t->view_name, view_name_len);
  391. t->view_name[view_name_len] = 0;
  392. }
  393. mstream_getw(s,&t->peer_count);
  394. t->entries = malloc(sizeof(BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE_ENTRY) * t->peer_count);
  395. if(t->entries == NULL){
  396. err("process_mrtd_table_dump_v2_peer_index_table: failed to allocate memory for index table");
  397. return 0;
  398. }
  399. for(i=0; i < t->peer_count; i++) {
  400. mstream_getc(s,&peertype);
  401. t->entries[i].afi = AFI_IP;
  402. #ifdef BGPDUMP_HAVE_IPV6
  403. if(peertype & BGPDUMP_PEERTYPE_TABLE_DUMP_V2_AFI_IP6)
  404. t->entries[i].afi = AFI_IP6;
  405. #endif
  406. t->entries[i].peer_bgp_id = mstream_get_ipv4(s);
  407. if(t->entries[i].afi == AFI_IP)
  408. t->entries[i].peer_ip.v4_addr = mstream_get_ipv4(s);
  409. #ifdef BGPDUMP_HAVE_IPV6
  410. else
  411. mstream_get(s, &t->entries[i].peer_ip.v6_addr.s6_addr, 16);
  412. #endif
  413. if(peertype & BGPDUMP_PEERTYPE_TABLE_DUMP_V2_AS4)
  414. read_asn(s, &t->entries[i].peer_as, 4);
  415. else
  416. read_asn(s, &t->entries[i].peer_as, 2);
  417. }
  418. return 0;
  419. }
  420. int process_mrtd_table_dump_v2_ipv4_unicast(struct mstream *s, BGPDUMP_ENTRY *entry){
  421. BGPDUMP_TABLE_DUMP_V2_PREFIX *prefixdata;
  422. prefixdata = &entry->body.mrtd_table_dump_v2_prefix;
  423. uint16_t i;
  424. prefixdata->afi = AFI_IP;
  425. prefixdata->safi = SAFI_UNICAST;
  426. mstream_getl(s, &prefixdata->seq);
  427. mstream_getc(s, &prefixdata->prefix_length);
  428. bzero(&prefixdata->prefix.v4_addr.s_addr, 4);
  429. mstream_get(s, &prefixdata->prefix.v4_addr.s_addr, (prefixdata->prefix_length+7)/8);
  430. mstream_getw(s, &prefixdata->entry_count);
  431. prefixdata->entries = malloc(sizeof(BGPDUMP_TABLE_DUMP_V2_ROUTE_ENTRY) * prefixdata->entry_count);
  432. if(prefixdata->entries == NULL){
  433. err("process_mrtd_table_dump_v2_ipv4_unicast: failed to allocate memory for entry table");
  434. return 0;
  435. }
  436. for(i=0; i < prefixdata->entry_count; i++){
  437. BGPDUMP_TABLE_DUMP_V2_ROUTE_ENTRY *e;
  438. e = &prefixdata->entries[i];
  439. mstream_getw(s, &e->peer_index);
  440. e->peer = &table_dump_v2_peer_index_table->entries[e->peer_index];
  441. mstream_getl(s, &e->originated_time);
  442. e->attr = process_attributes(s, 4, NULL);
  443. }
  444. return 1;
  445. }
  446. int process_mrtd_table_dump_v2_ipv6_unicast(struct mstream *s, BGPDUMP_ENTRY *entry){
  447. #ifdef BGPDUMP_HAVE_IPV6
  448. BGPDUMP_TABLE_DUMP_V2_PREFIX *prefixdata;
  449. prefixdata = &entry->body.mrtd_table_dump_v2_prefix;
  450. uint16_t i;
  451. prefixdata->afi = AFI_IP6;
  452. prefixdata->safi = SAFI_UNICAST;
  453. mstream_getl(s, &prefixdata->seq);
  454. mstream_getc(s, &prefixdata->prefix_length);
  455. bzero(&prefixdata->prefix.v6_addr.s6_addr, 16);
  456. mstream_get(s, &prefixdata->prefix.v6_addr.s6_addr, (prefixdata->prefix_length+7)/8);
  457. mstream_getw(s, &prefixdata->entry_count);
  458. prefixdata->entries = malloc(sizeof(BGPDUMP_TABLE_DUMP_V2_ROUTE_ENTRY) * prefixdata->entry_count);
  459. if(prefixdata->entries == NULL){
  460. err("process_mrtd_table_dump_v2_ipv6_unicast: failed to allocate memory for entry table");
  461. return 0;
  462. }
  463. for(i=0; i < prefixdata->entry_count; i++){
  464. BGPDUMP_TABLE_DUMP_V2_ROUTE_ENTRY *e;
  465. e = &prefixdata->entries[i];
  466. mstream_getw(s, &e->peer_index);
  467. e->peer = &table_dump_v2_peer_index_table->entries[e->peer_index];
  468. mstream_getl(s, &e->originated_time);
  469. e->attr = process_attributes(s, 4, NULL);
  470. }
  471. #endif
  472. return 1;
  473. }
  474. int process_zebra_bgp(struct mstream *s,BGPDUMP_ENTRY *entry) {
  475. switch(entry->subtype) {
  476. case BGPDUMP_SUBTYPE_ZEBRA_BGP_STATE_CHANGE:
  477. return process_zebra_bgp_state_change(s, entry, ASN16_LEN);
  478. case BGPDUMP_SUBTYPE_ZEBRA_BGP_STATE_CHANGE_AS4:
  479. return process_zebra_bgp_state_change(s, entry, ASN32_LEN);
  480. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE:
  481. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_LOCAL:
  482. return process_zebra_bgp_message(s, entry, ASN16_LEN);
  483. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4:
  484. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4_LOCAL:
  485. return process_zebra_bgp_message(s, entry, ASN32_LEN);
  486. case BGPDUMP_SUBTYPE_ZEBRA_BGP_ENTRY:
  487. return process_zebra_bgp_entry(s,entry);
  488. case BGPDUMP_SUBTYPE_ZEBRA_BGP_SNAPSHOT:
  489. return process_zebra_bgp_snapshot(s, entry);
  490. default:
  491. warn("process_zebra_bgp: unknown subtype %d", entry->subtype);
  492. return 0;
  493. }
  494. }
  495. int process_zebra_bgp_state_change(struct mstream *s,BGPDUMP_ENTRY *entry, u_int8_t asn_len) {
  496. read_asn(s, &entry->body.zebra_state_change.source_as, asn_len);
  497. read_asn(s, &entry->body.zebra_state_change.destination_as, asn_len);
  498. /* Work around Zebra dump corruption.
  499. * N.B. I don't see this in quagga 0.96.4 any more. Is it fixed? */
  500. if (entry->length == 8) {
  501. warn("process_zebra_bgp_state_change: 8-byte state change (zebra bug?)");
  502. mstream_getw(s,&entry->body.zebra_state_change.old_state);
  503. mstream_getw(s,&entry->body.zebra_state_change.new_state);
  504. /* Fill in with dummy values */
  505. entry->body.zebra_state_change.interface_index = 0;
  506. entry->body.zebra_state_change.address_family = AFI_IP;
  507. entry->body.zebra_state_change.source_ip.v4_addr.s_addr = 0;
  508. entry->body.zebra_state_change.destination_ip.v4_addr.s_addr = 0;
  509. return 1;
  510. }
  511. mstream_getw(s,&entry->body.zebra_state_change.interface_index);
  512. mstream_getw(s,&entry->body.zebra_state_change.address_family);
  513. switch(entry->body.zebra_state_change.address_family) {
  514. case AFI_IP:
  515. // length could be 20 or 24 (asn16 vs asn32)
  516. if(entry->length != 20 && entry->length != 24) {
  517. warn("process_zebra_bgp_state_change: bad length %d",
  518. entry->length);
  519. return 0;
  520. }
  521. entry->body.zebra_state_change.source_ip.v4_addr = mstream_get_ipv4(s);
  522. entry->body.zebra_state_change.destination_ip.v4_addr = mstream_get_ipv4(s);
  523. break;
  524. #ifdef BGPDUMP_HAVE_IPV6
  525. case AFI_IP6:
  526. // length could be 44 or 48 (asn16 vs asn32)
  527. if(entry->length != 44 && entry->length != 48) {
  528. warn("process_zebra_bgp_state_change: bad length %d",
  529. entry->length);
  530. return 0;
  531. }
  532. mstream_get(s, &entry->body.zebra_state_change.source_ip.v6_addr.s6_addr, 16);
  533. mstream_get(s, &entry->body.zebra_state_change.destination_ip.v6_addr.s6_addr, 16);
  534. break;
  535. #endif
  536. default:
  537. warn("process_zebra_bgp_state_change: unknown AFI %d",
  538. entry->body.zebra_state_change.address_family);
  539. return 0;
  540. }
  541. mstream_getw(s,&entry->body.zebra_state_change.old_state);
  542. mstream_getw(s,&entry->body.zebra_state_change.new_state);
  543. return 1;
  544. }
  545. int process_zebra_bgp_message(struct mstream *s,BGPDUMP_ENTRY *entry, u_int8_t asn_len) {
  546. u_char marker[16]; /* BGP marker */
  547. switch(entry->subtype) {
  548. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_LOCAL:
  549. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4_LOCAL:
  550. read_asn(s, &entry->body.zebra_message.destination_as, asn_len);
  551. read_asn(s, &entry->body.zebra_message.source_as, asn_len);
  552. break;
  553. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE:
  554. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4:
  555. default:
  556. read_asn(s, &entry->body.zebra_message.source_as, asn_len);
  557. read_asn(s, &entry->body.zebra_message.destination_as, asn_len);
  558. break;
  559. }
  560. mstream_getw(s,&entry->body.zebra_message.interface_index);
  561. mstream_getw(s,&entry->body.zebra_message.address_family);
  562. entry->body.zebra_message.opt_len = 0;
  563. entry->body.zebra_message.opt_data = NULL;
  564. entry->body.zebra_message.notify_len = 0;
  565. entry->body.zebra_message.notify_data = NULL;
  566. switch(entry->body.zebra_message.address_family) {
  567. case AFI_IP:
  568. switch(entry->subtype) {
  569. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_LOCAL:
  570. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4_LOCAL:
  571. entry->body.zebra_message.destination_ip.v4_addr = mstream_get_ipv4(s);
  572. entry->body.zebra_message.source_ip.v4_addr = mstream_get_ipv4(s);
  573. break;
  574. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE:
  575. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4:
  576. default:
  577. entry->body.zebra_message.source_ip.v4_addr = mstream_get_ipv4(s);
  578. entry->body.zebra_message.destination_ip.v4_addr = mstream_get_ipv4(s);
  579. break;
  580. }
  581. mstream_get (s, marker, 16);
  582. break;
  583. #ifdef BGPDUMP_HAVE_IPV6
  584. case AFI_IP6:
  585. switch(entry->subtype) {
  586. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_LOCAL:
  587. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4_LOCAL:
  588. mstream_get(s,&entry->body.zebra_message.destination_ip.v6_addr.s6_addr, 16);
  589. mstream_get(s,&entry->body.zebra_message.source_ip.v6_addr.s6_addr, 16);
  590. break;
  591. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE:
  592. case BGPDUMP_SUBTYPE_ZEBRA_BGP_MESSAGE_AS4:
  593. default:
  594. mstream_get(s,&entry->body.zebra_message.source_ip.v6_addr.s6_addr, 16);
  595. mstream_get(s,&entry->body.zebra_message.destination_ip.v6_addr.s6_addr, 16);
  596. break;
  597. }
  598. mstream_get (s, marker, 16);
  599. break;
  600. #endif
  601. case 0xFFFF:
  602. /* Zebra doesn't dump ifindex or src/dest IPs in OPEN
  603. * messages. Work around it. */
  604. if (entry->body.zebra_message.interface_index == 0xFFFF) {
  605. memset(marker, 0xFF, 4);
  606. mstream_get (s, marker + 4, 12);
  607. entry->body.zebra_message.interface_index = 0;
  608. entry->body.zebra_message.address_family = AFI_IP;
  609. entry->body.zebra_message.source_ip.v4_addr.s_addr = 0;
  610. entry->body.zebra_message.destination_ip.v4_addr.s_addr = 0;
  611. break;
  612. }
  613. /* Note fall through! If we don't recognize this type of data corruption, we say
  614. * the address family is unsupported (since FFFF is not a valid address family) */
  615. default:
  616. /* unsupported address family */
  617. warn("process_zebra_bgp_message: unsupported AFI %d",
  618. entry->body.zebra_message.address_family);
  619. return 0;
  620. }
  621. if(memcmp(marker, "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377", 16) != 0) {
  622. /* bad marker... ignore packet */
  623. warn(
  624. "bgp_message: bad marker: %02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x",
  625. marker[0],marker[1],marker[2],marker[3],marker[4],marker[5],marker[6],marker[7],
  626. marker[8],marker[9],marker[10],marker[11],marker[12],marker[13],marker[14],marker[15]);
  627. return 0;
  628. }
  629. mstream_getw(s,&entry->body.zebra_message.size);
  630. int expected = entry->body.zebra_message.size - sizeof(marker) - sizeof(u_int16_t);
  631. mstream_t copy = mstream_copy(s, expected);
  632. entry->body.zebra_message.cut_bytes = expected - mstream_can_read(&copy);
  633. switch(mstream_getc (&copy, &entry->body.zebra_message.type)) {
  634. case BGP_MSG_OPEN:
  635. return process_zebra_bgp_message_open(&copy, entry, asn_len);
  636. case BGP_MSG_UPDATE:
  637. return process_zebra_bgp_message_update(&copy, entry, asn_len);
  638. case BGP_MSG_NOTIFY:
  639. return process_zebra_bgp_message_notify(&copy, entry);
  640. case BGP_MSG_KEEPALIVE:
  641. /* Nothing to do */
  642. return 1;
  643. case BGP_MSG_ROUTE_REFRESH_01:
  644. /* Not implemented yet */
  645. warn("bgp_message: MSG_ROUTE_REFRESH_01 not implemented yet");
  646. return 0;
  647. case BGP_MSG_ROUTE_REFRESH:
  648. /* Not implemented yet */
  649. warn("bgp_message: MSG_ROUTE_REFRESH not implemented yet");
  650. return 0;
  651. default:
  652. warn("bgp_message: unknown BGP message type %d",
  653. entry->body.zebra_message.type);
  654. return 0;
  655. }
  656. }
  657. int process_zebra_bgp_message_notify(struct mstream *s, BGPDUMP_ENTRY *entry) {
  658. mstream_getc(s, &entry->body.zebra_message.error_code);
  659. mstream_getc(s, &entry->body.zebra_message.sub_error_code);
  660. entry->body.zebra_message.notify_len = entry->body.zebra_message.size - 21;
  661. if(entry->body.zebra_message.notify_len > 0) {
  662. entry->body.zebra_message.notify_data = malloc(entry->body.zebra_message.notify_len);
  663. mstream_get(s, entry->body.zebra_message.notify_data, entry->body.zebra_message.notify_len);
  664. }
  665. return 1;
  666. }
  667. int process_zebra_bgp_message_open(struct mstream *s, BGPDUMP_ENTRY *entry, u_int8_t asn_len) {
  668. mstream_getc(s, &entry->body.zebra_message.version);
  669. read_asn(s, &entry->body.zebra_message.my_as, asn_len);
  670. mstream_getw(s, &entry->body.zebra_message.hold_time);
  671. entry->body.zebra_message.bgp_id = mstream_get_ipv4(s);
  672. mstream_getc(s, &entry->body.zebra_message.opt_len);
  673. if(entry->body.zebra_message.opt_len) {
  674. entry->body.zebra_message.opt_data = malloc(entry->body.zebra_message.opt_len);
  675. mstream_get(s, entry->body.zebra_message.opt_data, entry->body.zebra_message.opt_len);
  676. }
  677. return 1;
  678. }
  679. int process_zebra_bgp_message_update(struct mstream *s, BGPDUMP_ENTRY *entry, u_int8_t asn_len) {
  680. entry->body.zebra_message.incomplete.orig_len = 0;
  681. mstream_t withdraw_stream = mstream_copy(s, mstream_getw(s, NULL));
  682. entry->body.zebra_message.withdraw_count = read_prefix_list(&withdraw_stream, AFI_IP,
  683. entry->body.zebra_message.withdraw,
  684. &entry->body.zebra_message.incomplete);
  685. entry->attr = process_attributes(s, asn_len, &entry->body.zebra_message.incomplete);
  686. entry->body.zebra_message.announce_count = read_prefix_list(s, AFI_IP,
  687. entry->body.zebra_message.announce,
  688. &entry->body.zebra_message.incomplete);
  689. return 1;
  690. }
  691. int process_zebra_bgp_entry(struct mstream *s, BGPDUMP_ENTRY *entry) {
  692. warn("process_zebra_bgp_entry: record type not implemented yet");
  693. return 0;
  694. }
  695. int process_zebra_bgp_snapshot(struct mstream *s, BGPDUMP_ENTRY *entry) {
  696. warn("process_zebra_bgp_snapshot: record type not implemented yet");
  697. return 0;
  698. }
  699. static attributes_t *attr_init(struct mstream *s, int len) {
  700. attributes_t *attr = malloc(sizeof(struct attr));
  701. attr->data=malloc(len);
  702. memcpy(attr->data, &s->start[s->position], len);
  703. attr->len = len;
  704. attr->flag = 0;
  705. attr->origin = -1;
  706. attr->nexthop.s_addr = INADDR_NONE;
  707. attr->med = -1;
  708. attr->local_pref = -1;
  709. attr->aggregator_as = -1;
  710. attr->aggregator_addr.s_addr = INADDR_NONE;
  711. attr->weight = -1;
  712. attr->originator_id.s_addr = -1;
  713. attr->cluster = NULL;
  714. attr->aspath = NULL;
  715. attr->community = NULL;
  716. attr->transit = NULL;
  717. attr->mp_info = calloc(1, sizeof(struct mp_info));;
  718. attr->unknown_num = 0;
  719. attr->unknown = NULL;
  720. attr->new_aspath = NULL;
  721. attr->old_aspath = NULL;
  722. attr->new_aggregator_as = -1;
  723. attr->new_aggregator_addr.s_addr = INADDR_NONE;
  724. return attr;
  725. }
  726. static void process_unknown_attr(struct mstream *s, attributes_t *attr, int flag, int type, int len) {
  727. /* Unknown attribute. Save as is */
  728. attr->unknown_num++;
  729. attr->unknown = realloc(attr->unknown, attr->unknown_num * sizeof(struct unknown_attr));
  730. /* Pointer to the unknown attribute we want to fill in */
  731. struct unknown_attr unknown = {
  732. .flag = flag,
  733. .type = type,
  734. .len = len,
  735. .raw = malloc(len)
  736. };
  737. attr->unknown[attr->unknown_num - 1] = unknown;
  738. mstream_get(s, unknown.raw, len);
  739. }
  740. static void process_one_attr(struct mstream *outer_stream, attributes_t *attr, u_int8_t asn_len, struct zebra_incomplete *incomplete) {
  741. int flag = mstream_getc(outer_stream, NULL);
  742. int type = mstream_getc(outer_stream, NULL);
  743. int len;
  744. if(flag & BGP_ATTR_FLAG_EXTLEN)
  745. len = mstream_getw(outer_stream,NULL);
  746. else
  747. len = mstream_getc(outer_stream,NULL);
  748. //info("flag:%-2i type:%-2i length:%i", flag, type, len);
  749. mstream_t ms = mstream_copy(outer_stream, len), *s = &ms;
  750. if(mstream_can_read(s) != len) {
  751. warn("ERROR attribute is truncated: expected=%u remaining=%u\n", len, mstream_can_read(s));
  752. return;
  753. }
  754. /* Take note of all attributes, including unknown ones */
  755. if(type <= sizeof(attr->flag) * 8)
  756. attr->flag |= ATTR_FLAG_BIT(type);
  757. switch(type) {
  758. case BGP_ATTR_MP_REACH_NLRI:
  759. process_mp_announce(s, attr->mp_info, incomplete);
  760. break;
  761. case BGP_ATTR_MP_UNREACH_NLRI:
  762. process_mp_withdraw(s, attr->mp_info, incomplete);
  763. break;
  764. case BGP_ATTR_ORIGIN:
  765. assert(attr->origin == -1);
  766. attr->origin = mstream_getc(s, NULL);
  767. break;
  768. case BGP_ATTR_AS_PATH:
  769. assert(! attr->aspath);
  770. attr->aspath = create_aspath(len, asn_len);
  771. mstream_get(s, attr->aspath->data, len);
  772. process_attr_aspath_string(attr->aspath);
  773. break;
  774. case BGP_ATTR_NEXT_HOP:
  775. assert(INADDR_NONE == attr->nexthop.s_addr);
  776. attr->nexthop = mstream_get_ipv4(s);
  777. break;
  778. case BGP_ATTR_MULTI_EXIT_DISC:
  779. assert(-1 == attr->med);
  780. mstream_getl(s,&attr->med);
  781. break;
  782. case BGP_ATTR_LOCAL_PREF:
  783. assert(-1 == attr->local_pref);
  784. mstream_getl(s,&attr->local_pref);
  785. break;
  786. case BGP_ATTR_ATOMIC_AGGREGATE:
  787. break;
  788. case BGP_ATTR_AGGREGATOR:
  789. assert(-1 == attr->new_aggregator_as);
  790. read_asn(s, &attr->aggregator_as, asn_len);
  791. attr->aggregator_addr = mstream_get_ipv4(s);
  792. break;
  793. case BGP_ATTR_COMMUNITIES:
  794. assert(! attr->community);
  795. attr->community = malloc(sizeof(struct community));
  796. attr->community->size = len / 4;
  797. attr->community->val = malloc(len);
  798. mstream_get(s,attr->community->val,len);
  799. attr->community->str = NULL;
  800. process_attr_community_string(attr->community);
  801. break;
  802. case BGP_ATTR_NEW_AS_PATH:
  803. assert(! attr->new_aspath);
  804. attr->new_aspath = create_aspath(len, ASN32_LEN);
  805. mstream_get(s,attr->new_aspath->data, len);
  806. process_attr_aspath_string(attr->new_aspath);
  807. /* AS_CONFED_SEQUENCE and AS_CONFED_SET segments invalid in NEW_AS_PATH */
  808. check_new_aspath(attr->new_aspath);
  809. break;
  810. case BGP_ATTR_NEW_AGGREGATOR:
  811. assert(-1 == attr->new_aggregator_as);
  812. read_asn(s, &attr->new_aggregator_as, ASN32_LEN);
  813. attr->new_aggregator_addr = mstream_get_ipv4(s);
  814. break;
  815. case BGP_ATTR_ORIGINATOR_ID:
  816. assert(INADDR_NONE == attr->originator_id.s_addr);
  817. attr->originator_id = mstream_get_ipv4(s);
  818. break;
  819. case BGP_ATTR_CLUSTER_LIST:
  820. assert(! attr->cluster);
  821. attr->cluster = malloc(sizeof(struct cluster_list));
  822. attr->cluster->length = len/4;
  823. attr->cluster->list = malloc((attr->cluster->length) * sizeof(struct in_addr));
  824. int i; // cluster index
  825. for (i = 0; i < attr->cluster->length; i++)
  826. attr->cluster->list[i] = mstream_get_ipv4(s);
  827. break;
  828. default:
  829. process_unknown_attr(s, attr, flag, type, len);
  830. }
  831. }
  832. attributes_t *process_attributes(struct mstream *s, u_int8_t asn_len, struct zebra_incomplete *incomplete) {
  833. int total = mstream_getw(s, NULL);
  834. attributes_t *attr = attr_init(s, total);
  835. mstream_t copy = mstream_copy(s, total);
  836. if(mstream_can_read(&copy) != total)
  837. warn("entry is truncated: expected=%u remaining=%u", total, mstream_can_read(&copy));
  838. while(mstream_can_read(&copy))
  839. process_one_attr(&copy, attr, asn_len, incomplete);
  840. // Once all attributes have been read, take care of ASN32 transition
  841. process_asn32_trans(attr, asn_len);
  842. return attr;
  843. }
  844. struct aspath *create_aspath(u_int16_t len, u_int8_t asn_len) {
  845. struct aspath *aspath = malloc(sizeof(struct aspath));
  846. if(aspath) {
  847. aspath->asn_len = asn_len;
  848. aspath->length = len;
  849. aspath->count = 0;
  850. aspath->str = NULL;
  851. if(len > 0)
  852. aspath->data = malloc(len);
  853. else
  854. aspath->data = NULL;
  855. }
  856. return aspath;
  857. }
  858. void aspath_error(struct aspath *as) {
  859. as->count = 0;
  860. if(as->str) {
  861. free(as->str);
  862. as->str = NULL;
  863. }
  864. as->str = malloc(strlen(ASPATH_STR_ERROR) + 1);
  865. strcpy(as->str, ASPATH_STR_ERROR);
  866. }
  867. void process_attr_aspath_string(struct aspath *as) {
  868. const int MAX_ASPATH_LEN = 8000;
  869. as->str = malloc(MAX_ASPATH_LEN);
  870. /* Set default values */
  871. int space = 0;
  872. u_char type = AS_SEQUENCE;
  873. int pos = 0;
  874. /* Set initial pointer. */
  875. caddr_t pnt = as->data;
  876. caddr_t end = pnt + as->length;
  877. struct assegment *segment = NULL;
  878. while (pnt < end) {
  879. int i;
  880. /* For fetch value. */
  881. segment = (struct assegment *) pnt;
  882. /* Check AS type validity. */
  883. if ((segment->type != AS_SET) &&
  884. (segment->type != AS_SEQUENCE) &&
  885. (segment->type != AS_CONFED_SET) &&
  886. (segment->type != AS_CONFED_SEQUENCE))
  887. {
  888. aspath_error(as);
  889. return;
  890. }
  891. /* Check AS length. */
  892. if ((pnt + (segment->length * as->asn_len) + AS_HEADER_SIZE) > end)
  893. {
  894. aspath_error(as);
  895. return;
  896. }
  897. /* If segment type is changed, print previous type's end
  898. character. */
  899. if (type != AS_SEQUENCE)
  900. as->str[pos++] = aspath_delimiter_char (type, AS_SEG_END);
  901. if (space)
  902. as->str[pos++] = ' ';
  903. if (segment->type != AS_SEQUENCE)
  904. as->str[pos++] = aspath_delimiter_char (segment->type, AS_SEG_START);
  905. space = 0;
  906. /* Increment as->count - NOT ignoring CONFED_SETS/SEQUENCES any more.
  907. I doubt anybody was relying on this behaviour anyway. */
  908. switch(segment->type) {
  909. case AS_SEQUENCE:
  910. case AS_CONFED_SEQUENCE:
  911. as->count += segment->length;
  912. break;
  913. case AS_SET:
  914. case AS_CONFED_SET:
  915. as->count += 1;
  916. break;
  917. }
  918. for (i = 0; i < segment->length; i++)
  919. {
  920. as_t asn;
  921. if (space)
  922. {
  923. if (segment->type == AS_SET
  924. || segment->type == AS_CONFED_SET)
  925. as->str[pos++] = ',';
  926. else
  927. as->str[pos++] = ' ';
  928. }
  929. else
  930. space = 1;
  931. int asn_pos = i * as->asn_len;
  932. switch(as->asn_len) {
  933. case ASN16_LEN:
  934. asn = ntohs (*(u_int16_t *) (segment->data + asn_pos));
  935. break;
  936. case ASN32_LEN:
  937. asn = ntohl (*(u_int32_t *) (segment->data + asn_pos));
  938. break;
  939. default:
  940. assert("invalid asn_len" && false);
  941. }
  942. pos += int2str(asn, as->str + pos);
  943. if(pos > MAX_ASPATH_LEN - 100) {
  944. strcpy(as->str + pos, "...");
  945. return;
  946. };
  947. }
  948. type = segment->type;
  949. pnt += (segment->length * as->asn_len) + AS_HEADER_SIZE;
  950. }
  951. if (segment && segment->type != AS_SEQUENCE)
  952. as->str[pos++] = aspath_delimiter_char (segment->type, AS_SEG_END);
  953. as->str[pos] = '\0';
  954. }
  955. char aspath_delimiter_char (u_char type, u_char which) {
  956. int i;
  957. struct
  958. {
  959. int type;
  960. char start;
  961. char end;
  962. } aspath_delim_char [] =
  963. {
  964. { AS_SET, '{', '}' },
  965. { AS_SEQUENCE, ' ', ' ' },
  966. { AS_CONFED_SET, '[', ']' },
  967. { AS_CONFED_SEQUENCE, '(', ')' },
  968. { 0, '\0', '\0' }
  969. };
  970. for (i = 0; aspath_delim_char[i].type != 0; i++)
  971. {
  972. if (aspath_delim_char[i].type == type)
  973. {
  974. if (which == AS_SEG_START)
  975. return aspath_delim_char[i].start;
  976. else if (which == AS_SEG_END)
  977. return aspath_delim_char[i].end;
  978. }
  979. }
  980. return ' ';
  981. }
  982. void process_attr_community_string(struct community *com) {
  983. char buf[BUFSIZ];
  984. int i;
  985. u_int32_t comval;
  986. u_int16_t as;
  987. u_int16_t val;
  988. memset (buf, 0, BUFSIZ);
  989. for (i = 0; i < com->size; i++)
  990. {
  991. memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
  992. comval = ntohl (comval);
  993. switch (comval)
  994. {
  995. case COMMUNITY_NO_EXPORT:
  996. strlcat (buf, " no-export", BUFSIZ);
  997. break;
  998. case COMMUNITY_NO_ADVERTISE:
  999. strlcat (buf, " no-advertise", BUFSIZ);
  1000. break;
  1001. case COMMUNITY_LOCAL_AS:
  1002. strlcat (buf, " local-AS", BUFSIZ);
  1003. break;
  1004. default:
  1005. as = (comval >> 16) & 0xFFFF;
  1006. val = comval & 0xFFFF;
  1007. snprintf (buf + strlen (buf), BUFSIZ - strlen (buf),
  1008. " %d:%d", as, val);
  1009. break;
  1010. }
  1011. }
  1012. com->str = malloc(strlen(buf)+1);
  1013. strcpy(com->str, buf);
  1014. }
  1015. static struct mp_nlri *get_nexthop(struct mstream *s, u_int16_t afi) {
  1016. struct mp_nlri *nlri = calloc(1, sizeof(struct mp_nlri));
  1017. nlri->nexthop_len = mstream_getc(s, NULL);
  1018. // sometimes nexthop_len is 0 - not sure what this means (see IS-626)
  1019. // if(mp_nlri->nexthop_len == 0)
  1020. // return len;
  1021. if(afi == AFI_IP) {
  1022. assert(nlri->nexthop_len == 4);
  1023. nlri->nexthop.v4_addr = mstream_get_ipv4(s);
  1024. return nlri;
  1025. }
  1026. #ifdef BGPDUMP_HAVE_IPV6
  1027. assert(afi == AFI_IP6);
  1028. mstream_get(s, &nlri->nexthop.v6_addr, 16);
  1029. if(nlri->nexthop_len == 32) {
  1030. /* Is there also a link-local address? */
  1031. mstream_get(s, &nlri->nexthop_local.v6_addr.s6_addr, 16);
  1032. } else if(nlri->nexthop_len != 16) {
  1033. warn("process_mp_announce: unknown MP nexthop length %d", nlri->nexthop_len);
  1034. }
  1035. #endif
  1036. return nlri;
  1037. }
  1038. void process_mp_announce(struct mstream *s, struct mp_info *info, struct zebra_incomplete *incomplete) {
  1039. u_int16_t afi;
  1040. u_int8_t safi;
  1041. // look for MRT abbreviated MP_NLRI packets
  1042. if(s->start[s->position] != 0) {
  1043. assert(info->announce[AFI_IP6][SAFI_UNICAST] == NULL);
  1044. info->announce[AFI_IP6][SAFI_UNICAST] = get_nexthop(s, AFI_IP6);
  1045. return;
  1046. }
  1047. mstream_getw(s, &afi);
  1048. mstream_getc(s, &safi);
  1049. if(afi > BGPDUMP_MAX_AFI || safi > BGPDUMP_MAX_SAFI) {
  1050. warn("process_mp_announce: unknown protocol(AFI=%d, SAFI=%d)!", afi, safi);
  1051. return;
  1052. }
  1053. if(info->announce[afi][safi] != NULL) {
  1054. warn("process_mp_announce: two MP_NLRI for the same protocol(%d, %d)!", afi, safi);
  1055. return;
  1056. }
  1057. info->announce[afi][safi] = get_nexthop(s, afi);
  1058. // SNPA is defunct and num_snpa should always be 0
  1059. u_int8_t num_snpa;
  1060. if(mstream_getc(s, &num_snpa))
  1061. warn("process_mp_announce: MP_NLRI contains SNPAs, skipping");
  1062. for(; num_snpa > 0; --num_snpa) {
  1063. mstream_get(s, NULL, mstream_getc(s, NULL));
  1064. }
  1065. info->announce[afi][safi]->prefix_count = read_prefix_list(s, afi, info->announce[afi][safi]->nlri, incomplete);
  1066. }
  1067. void process_mp_withdraw(struct mstream *s, struct mp_info *info, struct zebra_incomplete *incomplete) {
  1068. u_int16_t afi;
  1069. u_int8_t safi;
  1070. struct mp_nlri *mp_nlri;
  1071. mstream_getw(s, &afi);
  1072. mstream_getc(s, &safi);
  1073. /* Do we know about this address family? */
  1074. if(afi > BGPDUMP_MAX_AFI || safi > BGPDUMP_MAX_SAFI) {
  1075. warn("process_mp_withdraw: unknown AFI,SAFI %d,%d!", afi, safi);
  1076. return;
  1077. }
  1078. /* If there are 2 NLRI's for the same protocol, fail but don't burn and die */
  1079. if(info->withdraw[afi][safi] != NULL) {
  1080. warn("process_mp_withdraw: update contains more than one MP_NLRI with AFI,SAFI %d,%d!", afi, safi);
  1081. return;
  1082. }
  1083. /* Allocate structure */
  1084. mp_nlri = malloc(sizeof(struct mp_nlri));
  1085. memset(mp_nlri, 0, sizeof(struct mp_nlri));
  1086. info->withdraw[afi][safi] = mp_nlri;
  1087. mp_nlri->prefix_count = read_prefix_list(s, afi, mp_nlri->nlri, incomplete);
  1088. }
  1089. static int read_prefix_list(struct mstream *s, u_int16_t afi, struct prefix *prefixes, struct zebra_incomplete *incomplete) {
  1090. int count = 0;
  1091. while(mstream_can_read(s)) {
  1092. u_int8_t p_len = mstream_getc(s,NULL); // length in bits
  1093. u_int8_t p_bytes = (p_len + 7) / 8;
  1094. /* Truncated prefix list? */
  1095. if(mstream_can_read(s) < p_bytes) {
  1096. if(! incomplete)
  1097. break;
  1098. /* Put prefix in incomplete structure */
  1099. incomplete->afi = afi;
  1100. incomplete->orig_len = p_len;
  1101. incomplete->prefix = (struct prefix) {
  1102. .len = mstream_can_read(s) * 8
  1103. };
  1104. mstream_get(s, &incomplete->prefix.address, p_bytes);
  1105. break;
  1106. }
  1107. struct prefix *prefix = prefixes + count;
  1108. if(count++ > MAX_PREFIXES)
  1109. continue;
  1110. *prefix = (struct prefix) { .len = p_len };
  1111. mstream_get(s, &prefix->address, p_bytes);
  1112. }
  1113. if(count > MAX_PREFIXES) {
  1114. err("too many prefixes (%i > %i)", count, MAX_PREFIXES);
  1115. return MAX_PREFIXES;
  1116. }
  1117. return count;
  1118. }
  1119. static as_t read_asn(struct mstream *s, as_t *asn, u_int8_t len) {
  1120. u_int16_t asn16;
  1121. assert(len == sizeof(u_int32_t) || len == sizeof(u_int16_t));
  1122. switch(len) {
  1123. case sizeof(u_int32_t):
  1124. return mstream_getl(s, asn);
  1125. case sizeof(u_int16_t):
  1126. mstream_getw(s, &asn16);
  1127. if(asn)
  1128. *asn = asn16;
  1129. return asn16;
  1130. default:
  1131. /* Not reached. Avoid compiler warning */
  1132. return 0;
  1133. }
  1134. }
  1135. int check_new_aspath(struct aspath *aspath) {
  1136. struct assegment *segment;
  1137. for(segment = (struct assegment *) aspath->data;
  1138. segment < (struct assegment *) (aspath->data + aspath->length);
  1139. segment = (struct assegment *) ((char *) segment + sizeof(*segment) + segment->length * ASN32_LEN)) {
  1140. if(segment->type == AS_CONFED_SEQUENCE || segment->type == AS_CONFED_SET) {
  1141. warn("check_new_aspath: invalid segment of type AS_CONFED_%s in NEW_AS_PATH",
  1142. segment->type == AS_CONFED_SET ? "SET" : "SEQUENCE");
  1143. return 0;
  1144. }
  1145. }
  1146. return 1;
  1147. }
  1148. void process_asn32_trans(attributes_t *attr, u_int8_t asn_len) {
  1149. if(asn_len == ASN32_LEN) {
  1150. /* These attributes "SHOULD NOT" be used with ASN32. */
  1151. if(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEW_AS_PATH))
  1152. warn("process_asn32_trans: ASN32 message contains NEW_AS_PATH attribute");
  1153. if(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEW_AGGREGATOR))
  1154. warn("process_asn32_trans: ASN32 message contains NEW_AGGREGATOR attribute");
  1155. /* Don't compute anything, just leave AS_PATH and AGGREGATOR as they are */
  1156. return;
  1157. }
  1158. /* Process NEW_AGGREGATOR */
  1159. if(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR) &&
  1160. attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEW_AGGREGATOR)) {
  1161. /* Both AGGREGATOR and NEW_AGGREGATOR present, merge */
  1162. if(attr->aggregator_as != AS_TRAN) {
  1163. /* Don't do anything */
  1164. return;
  1165. } else {
  1166. attr->old_aggregator_as = attr->aggregator_as;
  1167. attr->old_aggregator_addr = attr->aggregator_addr;
  1168. attr->aggregator_as = attr->new_aggregator_as;
  1169. attr->aggregator_addr = attr->new_aggregator_addr;
  1170. }
  1171. }
  1172. /* Process NEW_AS_PATH */
  1173. if(! (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEW_AS_PATH)))
  1174. return;
  1175. // attr->aspath may be NULL, at least in case of MP_UNREACH_NLRI
  1176. if(attr->aspath == NULL) return;
  1177. if(attr->aspath->count < attr->new_aspath->count) {
  1178. return;
  1179. }
  1180. /* Merge paths */
  1181. attr->old_aspath = attr->aspath;
  1182. attr->aspath = asn32_merge_paths(attr->old_aspath, attr->new_aspath);
  1183. if(attr->aspath) {
  1184. process_attr_aspath_string(attr->aspath);
  1185. }
  1186. }
  1187. struct aspath *asn32_merge_paths(struct aspath *path, struct aspath *newpath) {
  1188. struct aspath *mergedpath = create_aspath(0, ASN32_LEN);
  1189. struct assegment *segment, *mergedsegment;
  1190. int newlen;
  1191. /* Keep copying segments from AS_PATH until our path is as long as AS_PATH - NEW_AS_PATH. */
  1192. segment = (struct assegment *) (path->data);
  1193. while(mergedpath->count < path->count - newpath->count) {
  1194. /* Make room */
  1195. newlen = mergedpath->length + sizeof(struct assegment) + segment->length * ASN32_LEN;
  1196. mergedpath->data = realloc(mergedpath->data, newlen);
  1197. /* Create a new AS-path segment */
  1198. mergedsegment = (struct assegment *) (mergedpath->data + mergedpath->length);
  1199. /* Copy segment over. AS_PATH contains 16-bit ASes, so expand */
  1200. mergedsegment->type = segment->type;
  1201. mergedsegment->length = segment->length;
  1202. asn32_expand_16_to_32(mergedsegment->data, segment->data, segment->length);
  1203. /* Update length */
  1204. mergedpath->length = newlen;
  1205. if(segment->type == AS_SET || segment->type == AS_CONFED_SET) {
  1206. mergedpath->count += 1;
  1207. } else {
  1208. mergedpath->count += segment->length;
  1209. /* Did we copy too many ASes over? */
  1210. if(mergedpath->count > path->count - newpath->count) {
  1211. mergedsegment->length -= mergedpath->count - (path->count - newpath->count);
  1212. mergedpath->length -= (mergedpath->count - (path->count - newpath->count)) * ASN32_LEN;
  1213. }
  1214. }
  1215. }
  1216. /* Append NEW_AS_PATH to merged path */
  1217. mergedpath->data = realloc(mergedpath->data, mergedpath->length + newpath->length);
  1218. memcpy(mergedpath->data + mergedpath->length, newpath->data, newpath->length);
  1219. mergedpath->length += newpath->length;
  1220. return mergedpath;
  1221. }
  1222. void asn32_expand_16_to_32(char *dst, char *src, int len) {
  1223. u_int32_t *dstarray = (u_int32_t *) dst;
  1224. u_int16_t *srcarray = (u_int16_t *) src;
  1225. int i;
  1226. for(i = 0; i < len; i++) {
  1227. dstarray[i] = htonl(ntohs(srcarray[i]));
  1228. }
  1229. }
  1230. #if defined(linux)
  1231. size_t strlcat(char *dst, const char *src, size_t size) {
  1232. if (strlen (dst) + strlen (src) >= size)
  1233. return -1;
  1234. strcat (dst, src);
  1235. return (strlen(dst));
  1236. }
  1237. #endif