bgpdump_lib.c 56 KB

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