bgpdump_lib.c 56 KB

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