print-bgp.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861
  1. /*
  2. * Copyright (C) 1999 WIDE Project.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the project nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * Extensively modified by Hannes Gredler (hannes@gredler.at) for more
  30. * complete BGP support.
  31. */
  32. /* \summary: Border Gateway Protocol (BGP) printer */
  33. #ifdef HAVE_CONFIG_H
  34. #include "config.h"
  35. #endif
  36. #include <netdissect-stdinc.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include "netdissect.h"
  40. #include "addrtoname.h"
  41. #include "extract.h"
  42. #include "af.h"
  43. #include "l2vpn.h"
  44. struct bgp {
  45. uint8_t bgp_marker[16];
  46. uint16_t bgp_len;
  47. uint8_t bgp_type;
  48. };
  49. #define BGP_SIZE 19 /* unaligned */
  50. #define BGP_OPEN 1
  51. #define BGP_UPDATE 2
  52. #define BGP_NOTIFICATION 3
  53. #define BGP_KEEPALIVE 4
  54. #define BGP_ROUTE_REFRESH 5
  55. static const struct tok bgp_msg_values[] = {
  56. { BGP_OPEN, "Open"},
  57. { BGP_UPDATE, "Update"},
  58. { BGP_NOTIFICATION, "Notification"},
  59. { BGP_KEEPALIVE, "Keepalive"},
  60. { BGP_ROUTE_REFRESH, "Route Refresh"},
  61. { 0, NULL}
  62. };
  63. struct bgp_open {
  64. uint8_t bgpo_marker[16];
  65. uint16_t bgpo_len;
  66. uint8_t bgpo_type;
  67. uint8_t bgpo_version;
  68. uint16_t bgpo_myas;
  69. uint16_t bgpo_holdtime;
  70. uint32_t bgpo_id;
  71. uint8_t bgpo_optlen;
  72. /* options should follow */
  73. };
  74. #define BGP_OPEN_SIZE 29 /* unaligned */
  75. struct bgp_opt {
  76. uint8_t bgpopt_type;
  77. uint8_t bgpopt_len;
  78. /* variable length */
  79. };
  80. #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
  81. #define BGP_CAP_HEADER_SIZE 2 /* some compilers may pad to 4 bytes */
  82. struct bgp_notification {
  83. uint8_t bgpn_marker[16];
  84. uint16_t bgpn_len;
  85. uint8_t bgpn_type;
  86. uint8_t bgpn_major;
  87. uint8_t bgpn_minor;
  88. };
  89. #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
  90. struct bgp_route_refresh {
  91. uint8_t bgp_marker[16];
  92. uint16_t len;
  93. uint8_t type;
  94. uint8_t afi[2]; /* the compiler messes this structure up */
  95. uint8_t res; /* when doing misaligned sequences of int8 and int16 */
  96. uint8_t safi; /* afi should be int16 - so we have to access it using */
  97. }; /* EXTRACT_16BITS(&bgp_route_refresh->afi) (sigh) */
  98. #define BGP_ROUTE_REFRESH_SIZE 23
  99. #define bgp_attr_lenlen(flags, p) \
  100. (((flags) & 0x10) ? 2 : 1)
  101. #define bgp_attr_len(flags, p) \
  102. (((flags) & 0x10) ? EXTRACT_16BITS(p) : *(p))
  103. #define BGPTYPE_ORIGIN 1
  104. #define BGPTYPE_AS_PATH 2
  105. #define BGPTYPE_NEXT_HOP 3
  106. #define BGPTYPE_MULTI_EXIT_DISC 4
  107. #define BGPTYPE_LOCAL_PREF 5
  108. #define BGPTYPE_ATOMIC_AGGREGATE 6
  109. #define BGPTYPE_AGGREGATOR 7
  110. #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
  111. #define BGPTYPE_ORIGINATOR_ID 9 /* RFC4456 */
  112. #define BGPTYPE_CLUSTER_LIST 10 /* RFC4456 */
  113. #define BGPTYPE_DPA 11 /* deprecated, draft-ietf-idr-bgp-dpa */
  114. #define BGPTYPE_ADVERTISERS 12 /* deprecated RFC1863 */
  115. #define BGPTYPE_RCID_PATH 13 /* deprecated RFC1863 */
  116. #define BGPTYPE_MP_REACH_NLRI 14 /* RFC4760 */
  117. #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC4760 */
  118. #define BGPTYPE_EXTD_COMMUNITIES 16 /* RFC4360 */
  119. #define BGPTYPE_AS4_PATH 17 /* RFC6793 */
  120. #define BGPTYPE_AGGREGATOR4 18 /* RFC6793 */
  121. #define BGPTYPE_PMSI_TUNNEL 22 /* RFC6514 */
  122. #define BGPTYPE_TUNNEL_ENCAP 23 /* RFC5512 */
  123. #define BGPTYPE_TRAFFIC_ENG 24 /* RFC5543 */
  124. #define BGPTYPE_IPV6_EXTD_COMMUNITIES 25 /* RFC5701 */
  125. #define BGPTYPE_AIGP 26 /* RFC7311 */
  126. #define BGPTYPE_PE_DISTINGUISHER_LABEL 27 /* RFC6514 */
  127. #define BGPTYPE_ENTROPY_LABEL 28 /* RFC6790 */
  128. #define BGPTYPE_LARGE_COMMUNITY 32 /* draft-ietf-idr-large-community-05 */
  129. #define BGPTYPE_ATTR_SET 128 /* RFC6368 */
  130. #define BGP_MP_NLRI_MINSIZE 3 /* End of RIB Marker detection */
  131. static const struct tok bgp_attr_values[] = {
  132. { BGPTYPE_ORIGIN, "Origin"},
  133. { BGPTYPE_AS_PATH, "AS Path"},
  134. { BGPTYPE_AS4_PATH, "AS4 Path"},
  135. { BGPTYPE_NEXT_HOP, "Next Hop"},
  136. { BGPTYPE_MULTI_EXIT_DISC, "Multi Exit Discriminator"},
  137. { BGPTYPE_LOCAL_PREF, "Local Preference"},
  138. { BGPTYPE_ATOMIC_AGGREGATE, "Atomic Aggregate"},
  139. { BGPTYPE_AGGREGATOR, "Aggregator"},
  140. { BGPTYPE_AGGREGATOR4, "Aggregator4"},
  141. { BGPTYPE_COMMUNITIES, "Community"},
  142. { BGPTYPE_ORIGINATOR_ID, "Originator ID"},
  143. { BGPTYPE_CLUSTER_LIST, "Cluster List"},
  144. { BGPTYPE_DPA, "DPA"},
  145. { BGPTYPE_ADVERTISERS, "Advertisers"},
  146. { BGPTYPE_RCID_PATH, "RCID Path / Cluster ID"},
  147. { BGPTYPE_MP_REACH_NLRI, "Multi-Protocol Reach NLRI"},
  148. { BGPTYPE_MP_UNREACH_NLRI, "Multi-Protocol Unreach NLRI"},
  149. { BGPTYPE_EXTD_COMMUNITIES, "Extended Community"},
  150. { BGPTYPE_PMSI_TUNNEL, "PMSI Tunnel"},
  151. { BGPTYPE_TUNNEL_ENCAP, "Tunnel Encapsulation"},
  152. { BGPTYPE_TRAFFIC_ENG, "Traffic Engineering"},
  153. { BGPTYPE_IPV6_EXTD_COMMUNITIES, "IPv6 Extended Community"},
  154. { BGPTYPE_AIGP, "Accumulated IGP Metric"},
  155. { BGPTYPE_PE_DISTINGUISHER_LABEL, "PE Distinguisher Label"},
  156. { BGPTYPE_ENTROPY_LABEL, "Entropy Label"},
  157. { BGPTYPE_LARGE_COMMUNITY, "Large Community"},
  158. { BGPTYPE_ATTR_SET, "Attribute Set"},
  159. { 255, "Reserved for development"},
  160. { 0, NULL}
  161. };
  162. #define BGP_AS_SET 1
  163. #define BGP_AS_SEQUENCE 2
  164. #define BGP_CONFED_AS_SEQUENCE 3 /* draft-ietf-idr-rfc3065bis-01 */
  165. #define BGP_CONFED_AS_SET 4 /* draft-ietf-idr-rfc3065bis-01 */
  166. #define BGP_AS_SEG_TYPE_MIN BGP_AS_SET
  167. #define BGP_AS_SEG_TYPE_MAX BGP_CONFED_AS_SET
  168. static const struct tok bgp_as_path_segment_open_values[] = {
  169. { BGP_AS_SEQUENCE, ""},
  170. { BGP_AS_SET, "{ "},
  171. { BGP_CONFED_AS_SEQUENCE, "( "},
  172. { BGP_CONFED_AS_SET, "({ "},
  173. { 0, NULL}
  174. };
  175. static const struct tok bgp_as_path_segment_close_values[] = {
  176. { BGP_AS_SEQUENCE, ""},
  177. { BGP_AS_SET, "}"},
  178. { BGP_CONFED_AS_SEQUENCE, ")"},
  179. { BGP_CONFED_AS_SET, "})"},
  180. { 0, NULL}
  181. };
  182. #define BGP_OPT_AUTH 1
  183. #define BGP_OPT_CAP 2
  184. static const struct tok bgp_opt_values[] = {
  185. { BGP_OPT_AUTH, "Authentication Information"},
  186. { BGP_OPT_CAP, "Capabilities Advertisement"},
  187. { 0, NULL}
  188. };
  189. #define BGP_CAPCODE_MP 1 /* RFC2858 */
  190. #define BGP_CAPCODE_RR 2 /* RFC2918 */
  191. #define BGP_CAPCODE_ORF 3 /* RFC5291 */
  192. #define BGP_CAPCODE_MR 4 /* RFC3107 */
  193. #define BGP_CAPCODE_EXT_NH 5 /* RFC5549 */
  194. #define BGP_CAPCODE_RESTART 64 /* RFC4724 */
  195. #define BGP_CAPCODE_AS_NEW 65 /* RFC6793 */
  196. #define BGP_CAPCODE_DYN_CAP 67 /* draft-ietf-idr-dynamic-cap */
  197. #define BGP_CAPCODE_MULTISESS 68 /* draft-ietf-idr-bgp-multisession */
  198. #define BGP_CAPCODE_ADD_PATH 69 /* RFC7911 */
  199. #define BGP_CAPCODE_ENH_RR 70 /* draft-keyur-bgp-enhanced-route-refresh */
  200. #define BGP_CAPCODE_RR_CISCO 128
  201. static const struct tok bgp_capcode_values[] = {
  202. { BGP_CAPCODE_MP, "Multiprotocol Extensions"},
  203. { BGP_CAPCODE_RR, "Route Refresh"},
  204. { BGP_CAPCODE_ORF, "Cooperative Route Filtering"},
  205. { BGP_CAPCODE_MR, "Multiple Routes to a Destination"},
  206. { BGP_CAPCODE_EXT_NH, "Extended Next Hop Encoding"},
  207. { BGP_CAPCODE_RESTART, "Graceful Restart"},
  208. { BGP_CAPCODE_AS_NEW, "32-Bit AS Number"},
  209. { BGP_CAPCODE_DYN_CAP, "Dynamic Capability"},
  210. { BGP_CAPCODE_MULTISESS, "Multisession BGP"},
  211. { BGP_CAPCODE_ADD_PATH, "Multiple Paths"},
  212. { BGP_CAPCODE_ENH_RR, "Enhanced Route Refresh"},
  213. { BGP_CAPCODE_RR_CISCO, "Route Refresh (Cisco)"},
  214. { 0, NULL}
  215. };
  216. #define BGP_NOTIFY_MAJOR_MSG 1
  217. #define BGP_NOTIFY_MAJOR_OPEN 2
  218. #define BGP_NOTIFY_MAJOR_UPDATE 3
  219. #define BGP_NOTIFY_MAJOR_HOLDTIME 4
  220. #define BGP_NOTIFY_MAJOR_FSM 5
  221. #define BGP_NOTIFY_MAJOR_CEASE 6
  222. #define BGP_NOTIFY_MAJOR_CAP 7
  223. static const struct tok bgp_notify_major_values[] = {
  224. { BGP_NOTIFY_MAJOR_MSG, "Message Header Error"},
  225. { BGP_NOTIFY_MAJOR_OPEN, "OPEN Message Error"},
  226. { BGP_NOTIFY_MAJOR_UPDATE, "UPDATE Message Error"},
  227. { BGP_NOTIFY_MAJOR_HOLDTIME,"Hold Timer Expired"},
  228. { BGP_NOTIFY_MAJOR_FSM, "Finite State Machine Error"},
  229. { BGP_NOTIFY_MAJOR_CEASE, "Cease"},
  230. { BGP_NOTIFY_MAJOR_CAP, "Capability Message Error"},
  231. { 0, NULL}
  232. };
  233. /* draft-ietf-idr-cease-subcode-02 */
  234. #define BGP_NOTIFY_MINOR_CEASE_MAXPRFX 1
  235. static const struct tok bgp_notify_minor_cease_values[] = {
  236. { BGP_NOTIFY_MINOR_CEASE_MAXPRFX, "Maximum Number of Prefixes Reached"},
  237. { 2, "Administratively Shutdown"},
  238. { 3, "Peer Unconfigured"},
  239. { 4, "Administratively Reset"},
  240. { 5, "Connection Rejected"},
  241. { 6, "Other Configuration Change"},
  242. { 7, "Connection Collision Resolution"},
  243. { 0, NULL}
  244. };
  245. static const struct tok bgp_notify_minor_msg_values[] = {
  246. { 1, "Connection Not Synchronized"},
  247. { 2, "Bad Message Length"},
  248. { 3, "Bad Message Type"},
  249. { 0, NULL}
  250. };
  251. static const struct tok bgp_notify_minor_open_values[] = {
  252. { 1, "Unsupported Version Number"},
  253. { 2, "Bad Peer AS"},
  254. { 3, "Bad BGP Identifier"},
  255. { 4, "Unsupported Optional Parameter"},
  256. { 5, "Authentication Failure"},
  257. { 6, "Unacceptable Hold Time"},
  258. { 7, "Capability Message Error"},
  259. { 0, NULL}
  260. };
  261. static const struct tok bgp_notify_minor_update_values[] = {
  262. { 1, "Malformed Attribute List"},
  263. { 2, "Unrecognized Well-known Attribute"},
  264. { 3, "Missing Well-known Attribute"},
  265. { 4, "Attribute Flags Error"},
  266. { 5, "Attribute Length Error"},
  267. { 6, "Invalid ORIGIN Attribute"},
  268. { 7, "AS Routing Loop"},
  269. { 8, "Invalid NEXT_HOP Attribute"},
  270. { 9, "Optional Attribute Error"},
  271. { 10, "Invalid Network Field"},
  272. { 11, "Malformed AS_PATH"},
  273. { 0, NULL}
  274. };
  275. static const struct tok bgp_notify_minor_fsm_values[] = {
  276. { 1, "In OpenSent State"},
  277. { 2, "In OpenConfirm State"},
  278. { 3, "In Established State"},
  279. { 0, NULL }
  280. };
  281. static const struct tok bgp_notify_minor_cap_values[] = {
  282. { 1, "Invalid Action Value" },
  283. { 2, "Invalid Capability Length" },
  284. { 3, "Malformed Capability Value" },
  285. { 4, "Unsupported Capability Code" },
  286. { 0, NULL }
  287. };
  288. static const struct tok bgp_origin_values[] = {
  289. { 0, "IGP"},
  290. { 1, "EGP"},
  291. { 2, "Incomplete"},
  292. { 0, NULL}
  293. };
  294. #define BGP_PMSI_TUNNEL_RSVP_P2MP 1
  295. #define BGP_PMSI_TUNNEL_LDP_P2MP 2
  296. #define BGP_PMSI_TUNNEL_PIM_SSM 3
  297. #define BGP_PMSI_TUNNEL_PIM_SM 4
  298. #define BGP_PMSI_TUNNEL_PIM_BIDIR 5
  299. #define BGP_PMSI_TUNNEL_INGRESS 6
  300. #define BGP_PMSI_TUNNEL_LDP_MP2MP 7
  301. static const struct tok bgp_pmsi_tunnel_values[] = {
  302. { BGP_PMSI_TUNNEL_RSVP_P2MP, "RSVP-TE P2MP LSP"},
  303. { BGP_PMSI_TUNNEL_LDP_P2MP, "LDP P2MP LSP"},
  304. { BGP_PMSI_TUNNEL_PIM_SSM, "PIM-SSM Tree"},
  305. { BGP_PMSI_TUNNEL_PIM_SM, "PIM-SM Tree"},
  306. { BGP_PMSI_TUNNEL_PIM_BIDIR, "PIM-Bidir Tree"},
  307. { BGP_PMSI_TUNNEL_INGRESS, "Ingress Replication"},
  308. { BGP_PMSI_TUNNEL_LDP_MP2MP, "LDP MP2MP LSP"},
  309. { 0, NULL}
  310. };
  311. static const struct tok bgp_pmsi_flag_values[] = {
  312. { 0x01, "Leaf Information required"},
  313. { 0, NULL}
  314. };
  315. #define BGP_AIGP_TLV 1
  316. static const struct tok bgp_aigp_values[] = {
  317. { BGP_AIGP_TLV, "AIGP"},
  318. { 0, NULL}
  319. };
  320. /* Subsequent address family identifier, RFC2283 section 7 */
  321. #define SAFNUM_RES 0
  322. #define SAFNUM_UNICAST 1
  323. #define SAFNUM_MULTICAST 2
  324. #define SAFNUM_UNIMULTICAST 3 /* deprecated now */
  325. /* labeled BGP RFC3107 */
  326. #define SAFNUM_LABUNICAST 4
  327. /* RFC6514 */
  328. #define SAFNUM_MULTICAST_VPN 5
  329. /* draft-nalawade-kapoor-tunnel-safi */
  330. #define SAFNUM_TUNNEL 64
  331. /* RFC4761 */
  332. #define SAFNUM_VPLS 65
  333. /* RFC6037 */
  334. #define SAFNUM_MDT 66
  335. /* RFC4364 */
  336. #define SAFNUM_VPNUNICAST 128
  337. /* RFC6513 */
  338. #define SAFNUM_VPNMULTICAST 129
  339. #define SAFNUM_VPNUNIMULTICAST 130 /* deprecated now */
  340. /* RFC4684 */
  341. #define SAFNUM_RT_ROUTING_INFO 132
  342. #define BGP_VPN_RD_LEN 8
  343. static const struct tok bgp_safi_values[] = {
  344. { SAFNUM_RES, "Reserved"},
  345. { SAFNUM_UNICAST, "Unicast"},
  346. { SAFNUM_MULTICAST, "Multicast"},
  347. { SAFNUM_UNIMULTICAST, "Unicast+Multicast"},
  348. { SAFNUM_LABUNICAST, "labeled Unicast"},
  349. { SAFNUM_TUNNEL, "Tunnel"},
  350. { SAFNUM_VPLS, "VPLS"},
  351. { SAFNUM_MDT, "MDT"},
  352. { SAFNUM_VPNUNICAST, "labeled VPN Unicast"},
  353. { SAFNUM_VPNMULTICAST, "labeled VPN Multicast"},
  354. { SAFNUM_VPNUNIMULTICAST, "labeled VPN Unicast+Multicast"},
  355. { SAFNUM_RT_ROUTING_INFO, "Route Target Routing Information"},
  356. { SAFNUM_MULTICAST_VPN, "Multicast VPN"},
  357. { 0, NULL }
  358. };
  359. /* well-known community */
  360. #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
  361. #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
  362. #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
  363. /* Extended community type - draft-ietf-idr-bgp-ext-communities-05 */
  364. #define BGP_EXT_COM_RT_0 0x0002 /* Route Target,Format AS(2bytes):AN(4bytes) */
  365. #define BGP_EXT_COM_RT_1 0x0102 /* Route Target,Format IP address:AN(2bytes) */
  366. #define BGP_EXT_COM_RT_2 0x0202 /* Route Target,Format AN(4bytes):local(2bytes) */
  367. #define BGP_EXT_COM_RO_0 0x0003 /* Route Origin,Format AS(2bytes):AN(4bytes) */
  368. #define BGP_EXT_COM_RO_1 0x0103 /* Route Origin,Format IP address:AN(2bytes) */
  369. #define BGP_EXT_COM_RO_2 0x0203 /* Route Origin,Format AN(4bytes):local(2bytes) */
  370. #define BGP_EXT_COM_LINKBAND 0x4004 /* Link Bandwidth,Format AS(2B):Bandwidth(4B) */
  371. /* rfc2547 bgp-mpls-vpns */
  372. #define BGP_EXT_COM_VPN_ORIGIN 0x0005 /* OSPF Domain ID / VPN of Origin - draft-rosen-vpns-ospf-bgp-mpls */
  373. #define BGP_EXT_COM_VPN_ORIGIN2 0x0105 /* duplicate - keep for backwards compatability */
  374. #define BGP_EXT_COM_VPN_ORIGIN3 0x0205 /* duplicate - keep for backwards compatability */
  375. #define BGP_EXT_COM_VPN_ORIGIN4 0x8005 /* duplicate - keep for backwards compatability */
  376. #define BGP_EXT_COM_OSPF_RTYPE 0x0306 /* OSPF Route Type,Format Area(4B):RouteType(1B):Options(1B) */
  377. #define BGP_EXT_COM_OSPF_RTYPE2 0x8000 /* duplicate - keep for backwards compatability */
  378. #define BGP_EXT_COM_OSPF_RID 0x0107 /* OSPF Router ID,Format RouterID(4B):Unused(2B) */
  379. #define BGP_EXT_COM_OSPF_RID2 0x8001 /* duplicate - keep for backwards compatability */
  380. #define BGP_EXT_COM_L2INFO 0x800a /* draft-kompella-ppvpn-l2vpn */
  381. #define BGP_EXT_COM_SOURCE_AS 0x0009 /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
  382. #define BGP_EXT_COM_VRF_RT_IMP 0x010b /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
  383. #define BGP_EXT_COM_L2VPN_RT_0 0x000a /* L2VPN Identifier,Format AS(2bytes):AN(4bytes) */
  384. #define BGP_EXT_COM_L2VPN_RT_1 0xF10a /* L2VPN Identifier,Format IP address:AN(2bytes) */
  385. /* http://www.cisco.com/en/US/tech/tk436/tk428/technologies_tech_note09186a00801eb09a.shtml */
  386. #define BGP_EXT_COM_EIGRP_GEN 0x8800
  387. #define BGP_EXT_COM_EIGRP_METRIC_AS_DELAY 0x8801
  388. #define BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW 0x8802
  389. #define BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU 0x8803
  390. #define BGP_EXT_COM_EIGRP_EXT_REMAS_REMID 0x8804
  391. #define BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC 0x8805
  392. static const struct tok bgp_extd_comm_flag_values[] = {
  393. { 0x8000, "vendor-specific"},
  394. { 0x4000, "non-transitive"},
  395. { 0, NULL},
  396. };
  397. static const struct tok bgp_extd_comm_subtype_values[] = {
  398. { BGP_EXT_COM_RT_0, "target"},
  399. { BGP_EXT_COM_RT_1, "target"},
  400. { BGP_EXT_COM_RT_2, "target"},
  401. { BGP_EXT_COM_RO_0, "origin"},
  402. { BGP_EXT_COM_RO_1, "origin"},
  403. { BGP_EXT_COM_RO_2, "origin"},
  404. { BGP_EXT_COM_LINKBAND, "link-BW"},
  405. { BGP_EXT_COM_VPN_ORIGIN, "ospf-domain"},
  406. { BGP_EXT_COM_VPN_ORIGIN2, "ospf-domain"},
  407. { BGP_EXT_COM_VPN_ORIGIN3, "ospf-domain"},
  408. { BGP_EXT_COM_VPN_ORIGIN4, "ospf-domain"},
  409. { BGP_EXT_COM_OSPF_RTYPE, "ospf-route-type"},
  410. { BGP_EXT_COM_OSPF_RTYPE2, "ospf-route-type"},
  411. { BGP_EXT_COM_OSPF_RID, "ospf-router-id"},
  412. { BGP_EXT_COM_OSPF_RID2, "ospf-router-id"},
  413. { BGP_EXT_COM_L2INFO, "layer2-info"},
  414. { BGP_EXT_COM_EIGRP_GEN , "eigrp-general-route (flag, tag)" },
  415. { BGP_EXT_COM_EIGRP_METRIC_AS_DELAY , "eigrp-route-metric (AS, delay)" },
  416. { BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW , "eigrp-route-metric (reliability, nexthop, bandwidth)" },
  417. { BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU , "eigrp-route-metric (load, MTU)" },
  418. { BGP_EXT_COM_EIGRP_EXT_REMAS_REMID , "eigrp-external-route (remote-AS, remote-ID)" },
  419. { BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC , "eigrp-external-route (remote-proto, remote-metric)" },
  420. { BGP_EXT_COM_SOURCE_AS, "source-AS" },
  421. { BGP_EXT_COM_VRF_RT_IMP, "vrf-route-import"},
  422. { BGP_EXT_COM_L2VPN_RT_0, "l2vpn-id"},
  423. { BGP_EXT_COM_L2VPN_RT_1, "l2vpn-id"},
  424. { 0, NULL},
  425. };
  426. /* OSPF codes for BGP_EXT_COM_OSPF_RTYPE draft-rosen-vpns-ospf-bgp-mpls */
  427. #define BGP_OSPF_RTYPE_RTR 1 /* OSPF Router LSA */
  428. #define BGP_OSPF_RTYPE_NET 2 /* OSPF Network LSA */
  429. #define BGP_OSPF_RTYPE_SUM 3 /* OSPF Summary LSA */
  430. #define BGP_OSPF_RTYPE_EXT 5 /* OSPF External LSA, note that ASBR doesn't apply to MPLS-VPN */
  431. #define BGP_OSPF_RTYPE_NSSA 7 /* OSPF NSSA External*/
  432. #define BGP_OSPF_RTYPE_SHAM 129 /* OSPF-MPLS-VPN Sham link */
  433. #define BGP_OSPF_RTYPE_METRIC_TYPE 0x1 /* LSB of RTYPE Options Field */
  434. static const struct tok bgp_extd_comm_ospf_rtype_values[] = {
  435. { BGP_OSPF_RTYPE_RTR, "Router" },
  436. { BGP_OSPF_RTYPE_NET, "Network" },
  437. { BGP_OSPF_RTYPE_SUM, "Summary" },
  438. { BGP_OSPF_RTYPE_EXT, "External" },
  439. { BGP_OSPF_RTYPE_NSSA,"NSSA External" },
  440. { BGP_OSPF_RTYPE_SHAM,"MPLS-VPN Sham" },
  441. { 0, NULL },
  442. };
  443. /* ADD-PATH Send/Receive field values */
  444. static const struct tok bgp_add_path_recvsend[] = {
  445. { 1, "Receive" },
  446. { 2, "Send" },
  447. { 3, "Both" },
  448. { 0, NULL },
  449. };
  450. static char astostr[20];
  451. /*
  452. * as_printf
  453. *
  454. * Convert an AS number into a string and return string pointer.
  455. *
  456. * Depending on bflag is set or not, AS number is converted into ASDOT notation
  457. * or plain number notation.
  458. *
  459. */
  460. static char *
  461. as_printf(netdissect_options *ndo,
  462. char *str, int size, u_int asnum)
  463. {
  464. if (!ndo->ndo_bflag || asnum <= 0xFFFF) {
  465. snprintf(str, size, "%u", asnum);
  466. } else {
  467. snprintf(str, size, "%u.%u", asnum >> 16, asnum & 0xFFFF);
  468. }
  469. return str;
  470. }
  471. #define ITEMCHECK(minlen) if (itemlen < minlen) goto badtlv;
  472. int
  473. decode_prefix4(netdissect_options *ndo,
  474. const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
  475. {
  476. struct in_addr addr;
  477. u_int plen, plenbytes;
  478. ND_TCHECK(pptr[0]);
  479. ITEMCHECK(1);
  480. plen = pptr[0];
  481. if (32 < plen)
  482. return -1;
  483. itemlen -= 1;
  484. memset(&addr, 0, sizeof(addr));
  485. plenbytes = (plen + 7) / 8;
  486. ND_TCHECK2(pptr[1], plenbytes);
  487. ITEMCHECK(plenbytes);
  488. memcpy(&addr, &pptr[1], plenbytes);
  489. if (plen % 8) {
  490. ((u_char *)&addr)[plenbytes - 1] &=
  491. ((0xff00 >> (plen % 8)) & 0xff);
  492. }
  493. snprintf(buf, buflen, "%s/%d", ipaddr_string(ndo, &addr), plen);
  494. return 1 + plenbytes;
  495. trunc:
  496. return -2;
  497. badtlv:
  498. return -3;
  499. }
  500. static int
  501. decode_labeled_prefix4(netdissect_options *ndo,
  502. const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
  503. {
  504. struct in_addr addr;
  505. u_int plen, plenbytes;
  506. /* prefix length and label = 4 bytes */
  507. ND_TCHECK2(pptr[0], 4);
  508. ITEMCHECK(4);
  509. plen = pptr[0]; /* get prefix length */
  510. /* this is one of the weirdnesses of rfc3107
  511. the label length (actually the label + COS bits)
  512. is added to the prefix length;
  513. we also do only read out just one label -
  514. there is no real application for advertisement of
  515. stacked labels in a single BGP message
  516. */
  517. if (24 > plen)
  518. return -1;
  519. plen-=24; /* adjust prefixlen - labellength */
  520. if (32 < plen)
  521. return -1;
  522. itemlen -= 4;
  523. memset(&addr, 0, sizeof(addr));
  524. plenbytes = (plen + 7) / 8;
  525. ND_TCHECK2(pptr[4], plenbytes);
  526. ITEMCHECK(plenbytes);
  527. memcpy(&addr, &pptr[4], plenbytes);
  528. if (plen % 8) {
  529. ((u_char *)&addr)[plenbytes - 1] &=
  530. ((0xff00 >> (plen % 8)) & 0xff);
  531. }
  532. /* the label may get offsetted by 4 bits so lets shift it right */
  533. snprintf(buf, buflen, "%s/%d, label:%u %s",
  534. ipaddr_string(ndo, &addr),
  535. plen,
  536. EXTRACT_24BITS(pptr+1)>>4,
  537. ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
  538. return 4 + plenbytes;
  539. trunc:
  540. return -2;
  541. badtlv:
  542. return -3;
  543. }
  544. /*
  545. * bgp_vpn_ip_print
  546. *
  547. * print an ipv4 or ipv6 address into a buffer dependend on address length.
  548. */
  549. static char *
  550. bgp_vpn_ip_print(netdissect_options *ndo,
  551. const u_char *pptr, u_int addr_length)
  552. {
  553. /* worst case string is s fully formatted v6 address */
  554. static char addr[sizeof("1234:5678:89ab:cdef:1234:5678:89ab:cdef")];
  555. char *pos = addr;
  556. switch(addr_length) {
  557. case (sizeof(struct in_addr) << 3): /* 32 */
  558. ND_TCHECK2(pptr[0], sizeof(struct in_addr));
  559. snprintf(pos, sizeof(addr), "%s", ipaddr_string(ndo, pptr));
  560. break;
  561. case (sizeof(struct in6_addr) << 3): /* 128 */
  562. ND_TCHECK2(pptr[0], sizeof(struct in6_addr));
  563. snprintf(pos, sizeof(addr), "%s", ip6addr_string(ndo, pptr));
  564. break;
  565. default:
  566. snprintf(pos, sizeof(addr), "bogus address length %u", addr_length);
  567. break;
  568. }
  569. pos += strlen(pos);
  570. trunc:
  571. *(pos) = '\0';
  572. return (addr);
  573. }
  574. /*
  575. * bgp_vpn_sg_print
  576. *
  577. * print an multicast s,g entry into a buffer.
  578. * the s,g entry is encoded like this.
  579. *
  580. * +-----------------------------------+
  581. * | Multicast Source Length (1 octet) |
  582. * +-----------------------------------+
  583. * | Multicast Source (Variable) |
  584. * +-----------------------------------+
  585. * | Multicast Group Length (1 octet) |
  586. * +-----------------------------------+
  587. * | Multicast Group (Variable) |
  588. * +-----------------------------------+
  589. *
  590. * return the number of bytes read from the wire.
  591. */
  592. static int
  593. bgp_vpn_sg_print(netdissect_options *ndo,
  594. const u_char *pptr, char *buf, u_int buflen)
  595. {
  596. uint8_t addr_length;
  597. u_int total_length, offset;
  598. total_length = 0;
  599. /* Source address length, encoded in bits */
  600. ND_TCHECK2(pptr[0], 1);
  601. addr_length = *pptr++;
  602. /* Source address */
  603. ND_TCHECK2(pptr[0], (addr_length >> 3));
  604. total_length += (addr_length >> 3) + 1;
  605. offset = strlen(buf);
  606. if (addr_length) {
  607. snprintf(buf + offset, buflen - offset, ", Source %s",
  608. bgp_vpn_ip_print(ndo, pptr, addr_length));
  609. pptr += (addr_length >> 3);
  610. }
  611. /* Group address length, encoded in bits */
  612. ND_TCHECK2(pptr[0], 1);
  613. addr_length = *pptr++;
  614. /* Group address */
  615. ND_TCHECK2(pptr[0], (addr_length >> 3));
  616. total_length += (addr_length >> 3) + 1;
  617. offset = strlen(buf);
  618. if (addr_length) {
  619. snprintf(buf + offset, buflen - offset, ", Group %s",
  620. bgp_vpn_ip_print(ndo, pptr, addr_length));
  621. pptr += (addr_length >> 3);
  622. }
  623. trunc:
  624. return (total_length);
  625. }
  626. /* RDs and RTs share the same semantics
  627. * we use bgp_vpn_rd_print for
  628. * printing route targets inside a NLRI */
  629. char *
  630. bgp_vpn_rd_print(netdissect_options *ndo,
  631. const u_char *pptr)
  632. {
  633. /* allocate space for the largest possible string */
  634. static char rd[sizeof("xxxxxxxxxx:xxxxx (xxx.xxx.xxx.xxx:xxxxx)")];
  635. char *pos = rd;
  636. /* ok lets load the RD format */
  637. switch (EXTRACT_16BITS(pptr)) {
  638. /* 2-byte-AS:number fmt*/
  639. case 0:
  640. snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u (= %u.%u.%u.%u)",
  641. EXTRACT_16BITS(pptr+2),
  642. EXTRACT_32BITS(pptr+4),
  643. *(pptr+4), *(pptr+5), *(pptr+6), *(pptr+7));
  644. break;
  645. /* IP-address:AS fmt*/
  646. case 1:
  647. snprintf(pos, sizeof(rd) - (pos - rd), "%u.%u.%u.%u:%u",
  648. *(pptr+2), *(pptr+3), *(pptr+4), *(pptr+5), EXTRACT_16BITS(pptr+6));
  649. break;
  650. /* 4-byte-AS:number fmt*/
  651. case 2:
  652. snprintf(pos, sizeof(rd) - (pos - rd), "%s:%u (%u.%u.%u.%u:%u)",
  653. as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(pptr+2)),
  654. EXTRACT_16BITS(pptr+6), *(pptr+2), *(pptr+3), *(pptr+4),
  655. *(pptr+5), EXTRACT_16BITS(pptr+6));
  656. break;
  657. default:
  658. snprintf(pos, sizeof(rd) - (pos - rd), "unknown RD format");
  659. break;
  660. }
  661. pos += strlen(pos);
  662. *(pos) = '\0';
  663. return (rd);
  664. }
  665. static int
  666. decode_rt_routing_info(netdissect_options *ndo,
  667. const u_char *pptr, char *buf, u_int buflen)
  668. {
  669. uint8_t route_target[8];
  670. u_int plen;
  671. char asbuf[sizeof(astostr)]; /* bgp_vpn_rd_print() overwrites astostr */
  672. /* NLRI "prefix length" from RFC 2858 Section 4. */
  673. ND_TCHECK(pptr[0]);
  674. plen = pptr[0]; /* get prefix length */
  675. /* NLRI "prefix" (ibid), valid lengths are { 0, 32, 33, ..., 96 } bits.
  676. * RFC 4684 Section 4 defines the layout of "origin AS" and "route
  677. * target" fields inside the "prefix" depending on its length.
  678. */
  679. if (0 == plen) {
  680. /* Without "origin AS", without "route target". */
  681. snprintf(buf, buflen, "default route target");
  682. return 1;
  683. }
  684. if (32 > plen)
  685. return -1;
  686. /* With at least "origin AS", possibly with "route target". */
  687. ND_TCHECK_32BITS(pptr + 1);
  688. as_printf(ndo, asbuf, sizeof(asbuf), EXTRACT_32BITS(pptr + 1));
  689. plen-=32; /* adjust prefix length */
  690. if (64 < plen)
  691. return -1;
  692. /* From now on (plen + 7) / 8 evaluates to { 0, 1, 2, ..., 8 }
  693. * and gives the number of octets in the variable-length "route
  694. * target" field inside this NLRI "prefix". Look for it.
  695. */
  696. memset(&route_target, 0, sizeof(route_target));
  697. ND_TCHECK2(pptr[5], (plen + 7) / 8);
  698. memcpy(&route_target, &pptr[5], (plen + 7) / 8);
  699. /* Which specification says to do this? */
  700. if (plen % 8) {
  701. ((u_char *)&route_target)[(plen + 7) / 8 - 1] &=
  702. ((0xff00 >> (plen % 8)) & 0xff);
  703. }
  704. snprintf(buf, buflen, "origin AS: %s, route target %s",
  705. asbuf,
  706. bgp_vpn_rd_print(ndo, (u_char *)&route_target));
  707. return 5 + (plen + 7) / 8;
  708. trunc:
  709. return -2;
  710. }
  711. static int
  712. decode_labeled_vpn_prefix4(netdissect_options *ndo,
  713. const u_char *pptr, char *buf, u_int buflen)
  714. {
  715. struct in_addr addr;
  716. u_int plen;
  717. ND_TCHECK(pptr[0]);
  718. plen = pptr[0]; /* get prefix length */
  719. if ((24+64) > plen)
  720. return -1;
  721. plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
  722. if (32 < plen)
  723. return -1;
  724. memset(&addr, 0, sizeof(addr));
  725. ND_TCHECK2(pptr[12], (plen + 7) / 8);
  726. memcpy(&addr, &pptr[12], (plen + 7) / 8);
  727. if (plen % 8) {
  728. ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
  729. ((0xff00 >> (plen % 8)) & 0xff);
  730. }
  731. /* the label may get offsetted by 4 bits so lets shift it right */
  732. snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
  733. bgp_vpn_rd_print(ndo, pptr+4),
  734. ipaddr_string(ndo, &addr),
  735. plen,
  736. EXTRACT_24BITS(pptr+1)>>4,
  737. ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
  738. return 12 + (plen + 7) / 8;
  739. trunc:
  740. return -2;
  741. }
  742. /*
  743. * +-------------------------------+
  744. * | |
  745. * | RD:IPv4-address (12 octets) |
  746. * | |
  747. * +-------------------------------+
  748. * | MDT Group-address (4 octets) |
  749. * +-------------------------------+
  750. */
  751. #define MDT_VPN_NLRI_LEN 16
  752. static int
  753. decode_mdt_vpn_nlri(netdissect_options *ndo,
  754. const u_char *pptr, char *buf, u_int buflen)
  755. {
  756. const u_char *rd;
  757. const u_char *vpn_ip;
  758. ND_TCHECK(pptr[0]);
  759. /* if the NLRI is not predefined length, quit.*/
  760. if (*pptr != MDT_VPN_NLRI_LEN * 8)
  761. return -1;
  762. pptr++;
  763. /* RD */
  764. ND_TCHECK2(pptr[0], 8);
  765. rd = pptr;
  766. pptr+=8;
  767. /* IPv4 address */
  768. ND_TCHECK2(pptr[0], sizeof(struct in_addr));
  769. vpn_ip = pptr;
  770. pptr+=sizeof(struct in_addr);
  771. /* MDT Group Address */
  772. ND_TCHECK2(pptr[0], sizeof(struct in_addr));
  773. snprintf(buf, buflen, "RD: %s, VPN IP Address: %s, MC Group Address: %s",
  774. bgp_vpn_rd_print(ndo, rd), ipaddr_string(ndo, vpn_ip), ipaddr_string(ndo, pptr));
  775. return MDT_VPN_NLRI_LEN + 1;
  776. trunc:
  777. return -2;
  778. }
  779. #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI 1
  780. #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI 2
  781. #define BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI 3
  782. #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF 4
  783. #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE 5
  784. #define BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN 6
  785. #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN 7
  786. static const struct tok bgp_multicast_vpn_route_type_values[] = {
  787. { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI, "Intra-AS I-PMSI"},
  788. { BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI, "Inter-AS I-PMSI"},
  789. { BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI, "S-PMSI"},
  790. { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF, "Intra-AS Segment-Leaf"},
  791. { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE, "Source-Active"},
  792. { BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN, "Shared Tree Join"},
  793. { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN, "Source Tree Join"},
  794. { 0, NULL}
  795. };
  796. static int
  797. decode_multicast_vpn(netdissect_options *ndo,
  798. const u_char *pptr, char *buf, u_int buflen)
  799. {
  800. uint8_t route_type, route_length, addr_length, sg_length;
  801. u_int offset;
  802. ND_TCHECK2(pptr[0], 2);
  803. route_type = *pptr++;
  804. route_length = *pptr++;
  805. snprintf(buf, buflen, "Route-Type: %s (%u), length: %u",
  806. tok2str(bgp_multicast_vpn_route_type_values,
  807. "Unknown", route_type),
  808. route_type, route_length);
  809. switch(route_type) {
  810. case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI:
  811. ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
  812. offset = strlen(buf);
  813. snprintf(buf + offset, buflen - offset, ", RD: %s, Originator %s",
  814. bgp_vpn_rd_print(ndo, pptr),
  815. bgp_vpn_ip_print(ndo, pptr + BGP_VPN_RD_LEN,
  816. (route_length - BGP_VPN_RD_LEN) << 3));
  817. break;
  818. case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI:
  819. ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN + 4);
  820. offset = strlen(buf);
  821. snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
  822. bgp_vpn_rd_print(ndo, pptr),
  823. as_printf(ndo, astostr, sizeof(astostr),
  824. EXTRACT_32BITS(pptr + BGP_VPN_RD_LEN)));
  825. break;
  826. case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI:
  827. ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
  828. offset = strlen(buf);
  829. snprintf(buf + offset, buflen - offset, ", RD: %s",
  830. bgp_vpn_rd_print(ndo, pptr));
  831. pptr += BGP_VPN_RD_LEN;
  832. sg_length = bgp_vpn_sg_print(ndo, pptr, buf, buflen);
  833. addr_length = route_length - sg_length;
  834. ND_TCHECK2(pptr[0], addr_length);
  835. offset = strlen(buf);
  836. snprintf(buf + offset, buflen - offset, ", Originator %s",
  837. bgp_vpn_ip_print(ndo, pptr, addr_length << 3));
  838. break;
  839. case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE:
  840. ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
  841. offset = strlen(buf);
  842. snprintf(buf + offset, buflen - offset, ", RD: %s",
  843. bgp_vpn_rd_print(ndo, pptr));
  844. pptr += BGP_VPN_RD_LEN;
  845. bgp_vpn_sg_print(ndo, pptr, buf, buflen);
  846. break;
  847. case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN: /* fall through */
  848. case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN:
  849. ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN + 4);
  850. offset = strlen(buf);
  851. snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
  852. bgp_vpn_rd_print(ndo, pptr),
  853. as_printf(ndo, astostr, sizeof(astostr),
  854. EXTRACT_32BITS(pptr + BGP_VPN_RD_LEN)));
  855. pptr += BGP_VPN_RD_LEN + 4;
  856. bgp_vpn_sg_print(ndo, pptr, buf, buflen);
  857. break;
  858. /*
  859. * no per route-type printing yet.
  860. */
  861. case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF:
  862. default:
  863. break;
  864. }
  865. return route_length + 2;
  866. trunc:
  867. return -2;
  868. }
  869. /*
  870. * As I remember, some versions of systems have an snprintf() that
  871. * returns -1 if the buffer would have overflowed. If the return
  872. * value is negative, set buflen to 0, to indicate that we've filled
  873. * the buffer up.
  874. *
  875. * If the return value is greater than buflen, that means that
  876. * the buffer would have overflowed; again, set buflen to 0 in
  877. * that case.
  878. */
  879. #define UPDATE_BUF_BUFLEN(buf, buflen, stringlen) \
  880. if (stringlen<0) \
  881. buflen=0; \
  882. else if ((u_int)stringlen>buflen) \
  883. buflen=0; \
  884. else { \
  885. buflen-=stringlen; \
  886. buf+=stringlen; \
  887. }
  888. static int
  889. decode_labeled_vpn_l2(netdissect_options *ndo,
  890. const u_char *pptr, char *buf, u_int buflen)
  891. {
  892. int plen,tlen,stringlen,tlv_type,tlv_len,ttlv_len;
  893. ND_TCHECK2(pptr[0], 2);
  894. plen=EXTRACT_16BITS(pptr);
  895. tlen=plen;
  896. pptr+=2;
  897. /* Old and new L2VPN NLRI share AFI/SAFI
  898. * -> Assume a 12 Byte-length NLRI is auto-discovery-only
  899. * and > 17 as old format. Complain for the middle case
  900. */
  901. if (plen==12) {
  902. /* assume AD-only with RD, BGPNH */
  903. ND_TCHECK2(pptr[0],12);
  904. buf[0]='\0';
  905. stringlen=snprintf(buf, buflen, "RD: %s, BGPNH: %s",
  906. bgp_vpn_rd_print(ndo, pptr),
  907. ipaddr_string(ndo, pptr+8)
  908. );
  909. UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
  910. pptr+=12;
  911. tlen-=12;
  912. return plen;
  913. } else if (plen>17) {
  914. /* assume old format */
  915. /* RD, ID, LBLKOFF, LBLBASE */
  916. ND_TCHECK2(pptr[0],15);
  917. buf[0]='\0';
  918. stringlen=snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
  919. bgp_vpn_rd_print(ndo, pptr),
  920. EXTRACT_16BITS(pptr+8),
  921. EXTRACT_16BITS(pptr+10),
  922. EXTRACT_24BITS(pptr+12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
  923. UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
  924. pptr+=15;
  925. tlen-=15;
  926. /* ok now the variable part - lets read out TLVs*/
  927. while (tlen>0) {
  928. if (tlen < 3)
  929. return -1;
  930. ND_TCHECK2(pptr[0], 3);
  931. tlv_type=*pptr++;
  932. tlv_len=EXTRACT_16BITS(pptr);
  933. ttlv_len=tlv_len;
  934. pptr+=2;
  935. switch(tlv_type) {
  936. case 1:
  937. if (buflen!=0) {
  938. stringlen=snprintf(buf,buflen, "\n\t\tcircuit status vector (%u) length: %u: 0x",
  939. tlv_type,
  940. tlv_len);
  941. UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
  942. }
  943. ttlv_len=ttlv_len/8+1; /* how many bytes do we need to read ? */
  944. while (ttlv_len>0) {
  945. ND_TCHECK(pptr[0]);
  946. if (buflen!=0) {
  947. stringlen=snprintf(buf,buflen, "%02x",*pptr++);
  948. UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
  949. }
  950. ttlv_len--;
  951. }
  952. break;
  953. default:
  954. if (buflen!=0) {
  955. stringlen=snprintf(buf,buflen, "\n\t\tunknown TLV #%u, length: %u",
  956. tlv_type,
  957. tlv_len);
  958. UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
  959. }
  960. break;
  961. }
  962. tlen-=(tlv_len<<3); /* the tlv-length is expressed in bits so lets shift it right */
  963. }
  964. return plen+2;
  965. } else {
  966. /* complain bitterly ? */
  967. /* fall through */
  968. goto trunc;
  969. }
  970. trunc:
  971. return -2;
  972. }
  973. int
  974. decode_prefix6(netdissect_options *ndo,
  975. const u_char *pd, u_int itemlen, char *buf, u_int buflen)
  976. {
  977. struct in6_addr addr;
  978. u_int plen, plenbytes;
  979. ND_TCHECK(pd[0]);
  980. ITEMCHECK(1);
  981. plen = pd[0];
  982. if (128 < plen)
  983. return -1;
  984. itemlen -= 1;
  985. memset(&addr, 0, sizeof(addr));
  986. plenbytes = (plen + 7) / 8;
  987. ND_TCHECK2(pd[1], plenbytes);
  988. ITEMCHECK(plenbytes);
  989. memcpy(&addr, &pd[1], plenbytes);
  990. if (plen % 8) {
  991. addr.s6_addr[plenbytes - 1] &=
  992. ((0xff00 >> (plen % 8)) & 0xff);
  993. }
  994. snprintf(buf, buflen, "%s/%d", ip6addr_string(ndo, &addr), plen);
  995. return 1 + plenbytes;
  996. trunc:
  997. return -2;
  998. badtlv:
  999. return -3;
  1000. }
  1001. static int
  1002. decode_labeled_prefix6(netdissect_options *ndo,
  1003. const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
  1004. {
  1005. struct in6_addr addr;
  1006. u_int plen, plenbytes;
  1007. /* prefix length and label = 4 bytes */
  1008. ND_TCHECK2(pptr[0], 4);
  1009. ITEMCHECK(4);
  1010. plen = pptr[0]; /* get prefix length */
  1011. if (24 > plen)
  1012. return -1;
  1013. plen-=24; /* adjust prefixlen - labellength */
  1014. if (128 < plen)
  1015. return -1;
  1016. itemlen -= 4;
  1017. memset(&addr, 0, sizeof(addr));
  1018. plenbytes = (plen + 7) / 8;
  1019. ND_TCHECK2(pptr[4], plenbytes);
  1020. memcpy(&addr, &pptr[4], plenbytes);
  1021. if (plen % 8) {
  1022. addr.s6_addr[plenbytes - 1] &=
  1023. ((0xff00 >> (plen % 8)) & 0xff);
  1024. }
  1025. /* the label may get offsetted by 4 bits so lets shift it right */
  1026. snprintf(buf, buflen, "%s/%d, label:%u %s",
  1027. ip6addr_string(ndo, &addr),
  1028. plen,
  1029. EXTRACT_24BITS(pptr+1)>>4,
  1030. ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
  1031. return 4 + plenbytes;
  1032. trunc:
  1033. return -2;
  1034. badtlv:
  1035. return -3;
  1036. }
  1037. static int
  1038. decode_labeled_vpn_prefix6(netdissect_options *ndo,
  1039. const u_char *pptr, char *buf, u_int buflen)
  1040. {
  1041. struct in6_addr addr;
  1042. u_int plen;
  1043. ND_TCHECK(pptr[0]);
  1044. plen = pptr[0]; /* get prefix length */
  1045. if ((24+64) > plen)
  1046. return -1;
  1047. plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
  1048. if (128 < plen)
  1049. return -1;
  1050. memset(&addr, 0, sizeof(addr));
  1051. ND_TCHECK2(pptr[12], (plen + 7) / 8);
  1052. memcpy(&addr, &pptr[12], (plen + 7) / 8);
  1053. if (plen % 8) {
  1054. addr.s6_addr[(plen + 7) / 8 - 1] &=
  1055. ((0xff00 >> (plen % 8)) & 0xff);
  1056. }
  1057. /* the label may get offsetted by 4 bits so lets shift it right */
  1058. snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
  1059. bgp_vpn_rd_print(ndo, pptr+4),
  1060. ip6addr_string(ndo, &addr),
  1061. plen,
  1062. EXTRACT_24BITS(pptr+1)>>4,
  1063. ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
  1064. return 12 + (plen + 7) / 8;
  1065. trunc:
  1066. return -2;
  1067. }
  1068. static int
  1069. decode_clnp_prefix(netdissect_options *ndo,
  1070. const u_char *pptr, char *buf, u_int buflen)
  1071. {
  1072. uint8_t addr[19];
  1073. u_int plen;
  1074. ND_TCHECK(pptr[0]);
  1075. plen = pptr[0]; /* get prefix length */
  1076. if (152 < plen)
  1077. return -1;
  1078. memset(&addr, 0, sizeof(addr));
  1079. ND_TCHECK2(pptr[4], (plen + 7) / 8);
  1080. memcpy(&addr, &pptr[4], (plen + 7) / 8);
  1081. if (plen % 8) {
  1082. addr[(plen + 7) / 8 - 1] &=
  1083. ((0xff00 >> (plen % 8)) & 0xff);
  1084. }
  1085. snprintf(buf, buflen, "%s/%d",
  1086. isonsap_string(ndo, addr,(plen + 7) / 8),
  1087. plen);
  1088. return 1 + (plen + 7) / 8;
  1089. trunc:
  1090. return -2;
  1091. }
  1092. static int
  1093. decode_labeled_vpn_clnp_prefix(netdissect_options *ndo,
  1094. const u_char *pptr, char *buf, u_int buflen)
  1095. {
  1096. uint8_t addr[19];
  1097. u_int plen;
  1098. ND_TCHECK(pptr[0]);
  1099. plen = pptr[0]; /* get prefix length */
  1100. if ((24+64) > plen)
  1101. return -1;
  1102. plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
  1103. if (152 < plen)
  1104. return -1;
  1105. memset(&addr, 0, sizeof(addr));
  1106. ND_TCHECK2(pptr[12], (plen + 7) / 8);
  1107. memcpy(&addr, &pptr[12], (plen + 7) / 8);
  1108. if (plen % 8) {
  1109. addr[(plen + 7) / 8 - 1] &=
  1110. ((0xff00 >> (plen % 8)) & 0xff);
  1111. }
  1112. /* the label may get offsetted by 4 bits so lets shift it right */
  1113. snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
  1114. bgp_vpn_rd_print(ndo, pptr+4),
  1115. isonsap_string(ndo, addr,(plen + 7) / 8),
  1116. plen,
  1117. EXTRACT_24BITS(pptr+1)>>4,
  1118. ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
  1119. return 12 + (plen + 7) / 8;
  1120. trunc:
  1121. return -2;
  1122. }
  1123. /*
  1124. * bgp_attr_get_as_size
  1125. *
  1126. * Try to find the size of the ASs encoded in an as-path. It is not obvious, as
  1127. * both Old speakers that do not support 4 byte AS, and the new speakers that do
  1128. * support, exchange AS-Path with the same path-attribute type value 0x02.
  1129. */
  1130. static int
  1131. bgp_attr_get_as_size(netdissect_options *ndo,
  1132. uint8_t bgpa_type, const u_char *pptr, int len)
  1133. {
  1134. const u_char *tptr = pptr;
  1135. /*
  1136. * If the path attribute is the optional AS4 path type, then we already
  1137. * know, that ASs must be encoded in 4 byte format.
  1138. */
  1139. if (bgpa_type == BGPTYPE_AS4_PATH) {
  1140. return 4;
  1141. }
  1142. /*
  1143. * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path
  1144. * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes
  1145. * each.
  1146. */
  1147. while (tptr < pptr + len) {
  1148. ND_TCHECK(tptr[0]);
  1149. /*
  1150. * If we do not find a valid segment type, our guess might be wrong.
  1151. */
  1152. if (tptr[0] < BGP_AS_SEG_TYPE_MIN || tptr[0] > BGP_AS_SEG_TYPE_MAX) {
  1153. goto trunc;
  1154. }
  1155. ND_TCHECK(tptr[1]);
  1156. tptr += 2 + tptr[1] * 2;
  1157. }
  1158. /*
  1159. * If we correctly reached end of the AS path attribute data content,
  1160. * then most likely ASs were indeed encoded as 2 bytes.
  1161. */
  1162. if (tptr == pptr + len) {
  1163. return 2;
  1164. }
  1165. trunc:
  1166. /*
  1167. * We can come here, either we did not have enough data, or if we
  1168. * try to decode 4 byte ASs in 2 byte format. Either way, return 4,
  1169. * so that calller can try to decode each AS as of 4 bytes. If indeed
  1170. * there was not enough data, it will crib and end the parse anyways.
  1171. */
  1172. return 4;
  1173. }
  1174. static int
  1175. bgp_attr_print(netdissect_options *ndo,
  1176. u_int atype, const u_char *pptr, u_int len)
  1177. {
  1178. int i;
  1179. uint16_t af;
  1180. uint8_t safi, snpa, nhlen;
  1181. union { /* copy buffer for bandwidth values */
  1182. float f;
  1183. uint32_t i;
  1184. } bw;
  1185. int advance;
  1186. u_int tlen;
  1187. const u_char *tptr;
  1188. char buf[MAXHOSTNAMELEN + 100];
  1189. int as_size;
  1190. tptr = pptr;
  1191. tlen=len;
  1192. switch (atype) {
  1193. case BGPTYPE_ORIGIN:
  1194. if (len != 1)
  1195. ND_PRINT((ndo, "invalid len"));
  1196. else {
  1197. ND_TCHECK(*tptr);
  1198. ND_PRINT((ndo, "%s", tok2str(bgp_origin_values,
  1199. "Unknown Origin Typecode",
  1200. tptr[0])));
  1201. }
  1202. break;
  1203. /*
  1204. * Process AS4 byte path and AS2 byte path attributes here.
  1205. */
  1206. case BGPTYPE_AS4_PATH:
  1207. case BGPTYPE_AS_PATH:
  1208. if (len % 2) {
  1209. ND_PRINT((ndo, "invalid len"));
  1210. break;
  1211. }
  1212. if (!len) {
  1213. ND_PRINT((ndo, "empty"));
  1214. break;
  1215. }
  1216. /*
  1217. * BGP updates exchanged between New speakers that support 4
  1218. * byte AS, ASs are always encoded in 4 bytes. There is no
  1219. * definitive way to find this, just by the packet's
  1220. * contents. So, check for packet's TLV's sanity assuming
  1221. * 2 bytes first, and it does not pass, assume that ASs are
  1222. * encoded in 4 bytes format and move on.
  1223. */
  1224. as_size = bgp_attr_get_as_size(ndo, atype, pptr, len);
  1225. while (tptr < pptr + len) {
  1226. ND_TCHECK(tptr[0]);
  1227. ND_PRINT((ndo, "%s", tok2str(bgp_as_path_segment_open_values,
  1228. "?", tptr[0])));
  1229. ND_TCHECK(tptr[1]);
  1230. for (i = 0; i < tptr[1] * as_size; i += as_size) {
  1231. ND_TCHECK2(tptr[2 + i], as_size);
  1232. ND_PRINT((ndo, "%s ",
  1233. as_printf(ndo, astostr, sizeof(astostr),
  1234. as_size == 2 ?
  1235. EXTRACT_16BITS(&tptr[2 + i]) :
  1236. EXTRACT_32BITS(&tptr[2 + i]))));
  1237. }
  1238. ND_TCHECK(tptr[0]);
  1239. ND_PRINT((ndo, "%s", tok2str(bgp_as_path_segment_close_values,
  1240. "?", tptr[0])));
  1241. ND_TCHECK(tptr[1]);
  1242. tptr += 2 + tptr[1] * as_size;
  1243. }
  1244. break;
  1245. case BGPTYPE_NEXT_HOP:
  1246. if (len != 4)
  1247. ND_PRINT((ndo, "invalid len"));
  1248. else {
  1249. ND_TCHECK2(tptr[0], 4);
  1250. ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr)));
  1251. }
  1252. break;
  1253. case BGPTYPE_MULTI_EXIT_DISC:
  1254. case BGPTYPE_LOCAL_PREF:
  1255. if (len != 4)
  1256. ND_PRINT((ndo, "invalid len"));
  1257. else {
  1258. ND_TCHECK2(tptr[0], 4);
  1259. ND_PRINT((ndo, "%u", EXTRACT_32BITS(tptr)));
  1260. }
  1261. break;
  1262. case BGPTYPE_ATOMIC_AGGREGATE:
  1263. if (len != 0)
  1264. ND_PRINT((ndo, "invalid len"));
  1265. break;
  1266. case BGPTYPE_AGGREGATOR:
  1267. /*
  1268. * Depending on the AS encoded is of 2 bytes or of 4 bytes,
  1269. * the length of this PA can be either 6 bytes or 8 bytes.
  1270. */
  1271. if (len != 6 && len != 8) {
  1272. ND_PRINT((ndo, "invalid len"));
  1273. break;
  1274. }
  1275. ND_TCHECK2(tptr[0], len);
  1276. if (len == 6) {
  1277. ND_PRINT((ndo, " AS #%s, origin %s",
  1278. as_printf(ndo, astostr, sizeof(astostr), EXTRACT_16BITS(tptr)),
  1279. ipaddr_string(ndo, tptr + 2)));
  1280. } else {
  1281. ND_PRINT((ndo, " AS #%s, origin %s",
  1282. as_printf(ndo, astostr, sizeof(astostr),
  1283. EXTRACT_32BITS(tptr)), ipaddr_string(ndo, tptr + 4)));
  1284. }
  1285. break;
  1286. case BGPTYPE_AGGREGATOR4:
  1287. if (len != 8) {
  1288. ND_PRINT((ndo, "invalid len"));
  1289. break;
  1290. }
  1291. ND_TCHECK2(tptr[0], 8);
  1292. ND_PRINT((ndo, " AS #%s, origin %s",
  1293. as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(tptr)),
  1294. ipaddr_string(ndo, tptr + 4)));
  1295. break;
  1296. case BGPTYPE_COMMUNITIES:
  1297. if (len % 4) {
  1298. ND_PRINT((ndo, "invalid len"));
  1299. break;
  1300. }
  1301. while (tlen>0) {
  1302. uint32_t comm;
  1303. ND_TCHECK2(tptr[0], 4);
  1304. comm = EXTRACT_32BITS(tptr);
  1305. switch (comm) {
  1306. case BGP_COMMUNITY_NO_EXPORT:
  1307. ND_PRINT((ndo, " NO_EXPORT"));
  1308. break;
  1309. case BGP_COMMUNITY_NO_ADVERT:
  1310. ND_PRINT((ndo, " NO_ADVERTISE"));
  1311. break;
  1312. case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
  1313. ND_PRINT((ndo, " NO_EXPORT_SUBCONFED"));
  1314. break;
  1315. default:
  1316. ND_PRINT((ndo, "%u:%u%s",
  1317. (comm >> 16) & 0xffff,
  1318. comm & 0xffff,
  1319. (tlen>4) ? ", " : ""));
  1320. break;
  1321. }
  1322. tlen -=4;
  1323. tptr +=4;
  1324. }
  1325. break;
  1326. case BGPTYPE_ORIGINATOR_ID:
  1327. if (len != 4) {
  1328. ND_PRINT((ndo, "invalid len"));
  1329. break;
  1330. }
  1331. ND_TCHECK2(tptr[0], 4);
  1332. ND_PRINT((ndo, "%s",ipaddr_string(ndo, tptr)));
  1333. break;
  1334. case BGPTYPE_CLUSTER_LIST:
  1335. if (len % 4) {
  1336. ND_PRINT((ndo, "invalid len"));
  1337. break;
  1338. }
  1339. while (tlen>0) {
  1340. ND_TCHECK2(tptr[0], 4);
  1341. ND_PRINT((ndo, "%s%s",
  1342. ipaddr_string(ndo, tptr),
  1343. (tlen>4) ? ", " : ""));
  1344. tlen -=4;
  1345. tptr +=4;
  1346. }
  1347. break;
  1348. case BGPTYPE_MP_REACH_NLRI:
  1349. ND_TCHECK2(tptr[0], 3);
  1350. af = EXTRACT_16BITS(tptr);
  1351. safi = tptr[2];
  1352. ND_PRINT((ndo, "\n\t AFI: %s (%u), %sSAFI: %s (%u)",
  1353. tok2str(af_values, "Unknown AFI", af),
  1354. af,
  1355. (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
  1356. tok2str(bgp_safi_values, "Unknown SAFI", safi),
  1357. safi));
  1358. switch(af<<8 | safi) {
  1359. case (AFNUM_INET<<8 | SAFNUM_UNICAST):
  1360. case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
  1361. case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
  1362. case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
  1363. case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
  1364. case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
  1365. case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
  1366. case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
  1367. case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
  1368. case (AFNUM_INET<<8 | SAFNUM_MDT):
  1369. case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
  1370. case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
  1371. case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
  1372. case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
  1373. case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
  1374. case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
  1375. case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
  1376. case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
  1377. case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
  1378. case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
  1379. case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
  1380. case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
  1381. case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
  1382. case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
  1383. case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
  1384. case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
  1385. case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
  1386. break;
  1387. default:
  1388. ND_TCHECK2(tptr[0], tlen);
  1389. ND_PRINT((ndo, "\n\t no AFI %u / SAFI %u decoder", af, safi));
  1390. if (ndo->ndo_vflag <= 1)
  1391. print_unknown_data(ndo, tptr, "\n\t ", tlen);
  1392. goto done;
  1393. break;
  1394. }
  1395. tptr +=3;
  1396. ND_TCHECK(tptr[0]);
  1397. nhlen = tptr[0];
  1398. tlen = nhlen;
  1399. tptr++;
  1400. if (tlen) {
  1401. int nnh = 0;
  1402. ND_PRINT((ndo, "\n\t nexthop: "));
  1403. while (tlen > 0) {
  1404. if ( nnh++ > 0 ) {
  1405. ND_PRINT((ndo, ", " ));
  1406. }
  1407. switch(af<<8 | safi) {
  1408. case (AFNUM_INET<<8 | SAFNUM_UNICAST):
  1409. case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
  1410. case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
  1411. case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
  1412. case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
  1413. case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
  1414. case (AFNUM_INET<<8 | SAFNUM_MDT):
  1415. if (tlen < (int)sizeof(struct in_addr)) {
  1416. ND_PRINT((ndo, "invalid len"));
  1417. tlen = 0;
  1418. } else {
  1419. ND_TCHECK2(tptr[0], sizeof(struct in_addr));
  1420. ND_PRINT((ndo, "%s",ipaddr_string(ndo, tptr)));
  1421. tlen -= sizeof(struct in_addr);
  1422. tptr += sizeof(struct in_addr);
  1423. }
  1424. break;
  1425. case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
  1426. case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
  1427. case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
  1428. if (tlen < (int)(sizeof(struct in_addr)+BGP_VPN_RD_LEN)) {
  1429. ND_PRINT((ndo, "invalid len"));
  1430. tlen = 0;
  1431. } else {
  1432. ND_TCHECK2(tptr[0], sizeof(struct in_addr)+BGP_VPN_RD_LEN);
  1433. ND_PRINT((ndo, "RD: %s, %s",
  1434. bgp_vpn_rd_print(ndo, tptr),
  1435. ipaddr_string(ndo, tptr+BGP_VPN_RD_LEN)));
  1436. tlen -= (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
  1437. tptr += (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
  1438. }
  1439. break;
  1440. case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
  1441. case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
  1442. case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
  1443. case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
  1444. if (tlen < (int)sizeof(struct in6_addr)) {
  1445. ND_PRINT((ndo, "invalid len"));
  1446. tlen = 0;
  1447. } else {
  1448. ND_TCHECK2(tptr[0], sizeof(struct in6_addr));
  1449. ND_PRINT((ndo, "%s", ip6addr_string(ndo, tptr)));
  1450. tlen -= sizeof(struct in6_addr);
  1451. tptr += sizeof(struct in6_addr);
  1452. }
  1453. break;
  1454. case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
  1455. case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
  1456. case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
  1457. if (tlen < (int)(sizeof(struct in6_addr)+BGP_VPN_RD_LEN)) {
  1458. ND_PRINT((ndo, "invalid len"));
  1459. tlen = 0;
  1460. } else {
  1461. ND_TCHECK2(tptr[0], sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
  1462. ND_PRINT((ndo, "RD: %s, %s",
  1463. bgp_vpn_rd_print(ndo, tptr),
  1464. ip6addr_string(ndo, tptr+BGP_VPN_RD_LEN)));
  1465. tlen -= (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
  1466. tptr += (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
  1467. }
  1468. break;
  1469. case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
  1470. case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
  1471. case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
  1472. case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
  1473. if (tlen < (int)sizeof(struct in_addr)) {
  1474. ND_PRINT((ndo, "invalid len"));
  1475. tlen = 0;
  1476. } else {
  1477. ND_TCHECK2(tptr[0], sizeof(struct in_addr));
  1478. ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr)));
  1479. tlen -= (sizeof(struct in_addr));
  1480. tptr += (sizeof(struct in_addr));
  1481. }
  1482. break;
  1483. case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
  1484. case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
  1485. case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
  1486. ND_TCHECK2(tptr[0], tlen);
  1487. ND_PRINT((ndo, "%s", isonsap_string(ndo, tptr, tlen)));
  1488. tptr += tlen;
  1489. tlen = 0;
  1490. break;
  1491. case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
  1492. case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
  1493. case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
  1494. if (tlen < BGP_VPN_RD_LEN+1) {
  1495. ND_PRINT((ndo, "invalid len"));
  1496. tlen = 0;
  1497. } else {
  1498. ND_TCHECK2(tptr[0], tlen);
  1499. ND_PRINT((ndo, "RD: %s, %s",
  1500. bgp_vpn_rd_print(ndo, tptr),
  1501. isonsap_string(ndo, tptr+BGP_VPN_RD_LEN,tlen-BGP_VPN_RD_LEN)));
  1502. /* rfc986 mapped IPv4 address ? */
  1503. if (EXTRACT_32BITS(tptr+BGP_VPN_RD_LEN) == 0x47000601)
  1504. ND_PRINT((ndo, " = %s", ipaddr_string(ndo, tptr+BGP_VPN_RD_LEN+4)));
  1505. /* rfc1888 mapped IPv6 address ? */
  1506. else if (EXTRACT_24BITS(tptr+BGP_VPN_RD_LEN) == 0x350000)
  1507. ND_PRINT((ndo, " = %s", ip6addr_string(ndo, tptr+BGP_VPN_RD_LEN+3)));
  1508. tptr += tlen;
  1509. tlen = 0;
  1510. }
  1511. break;
  1512. default:
  1513. ND_TCHECK2(tptr[0], tlen);
  1514. ND_PRINT((ndo, "no AFI %u/SAFI %u decoder", af, safi));
  1515. if (ndo->ndo_vflag <= 1)
  1516. print_unknown_data(ndo, tptr, "\n\t ", tlen);
  1517. tptr += tlen;
  1518. tlen = 0;
  1519. goto done;
  1520. break;
  1521. }
  1522. }
  1523. }
  1524. ND_PRINT((ndo, ", nh-length: %u", nhlen));
  1525. tptr += tlen;
  1526. ND_TCHECK(tptr[0]);
  1527. snpa = tptr[0];
  1528. tptr++;
  1529. if (snpa) {
  1530. ND_PRINT((ndo, "\n\t %u SNPA", snpa));
  1531. for (/*nothing*/; snpa > 0; snpa--) {
  1532. ND_TCHECK(tptr[0]);
  1533. ND_PRINT((ndo, "\n\t %d bytes", tptr[0]));
  1534. tptr += tptr[0] + 1;
  1535. }
  1536. } else {
  1537. ND_PRINT((ndo, ", no SNPA"));
  1538. }
  1539. while (tptr < pptr + len) {
  1540. switch (af<<8 | safi) {
  1541. case (AFNUM_INET<<8 | SAFNUM_UNICAST):
  1542. case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
  1543. case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
  1544. advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
  1545. if (advance == -1)
  1546. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1547. else if (advance == -2)
  1548. goto trunc;
  1549. else if (advance == -3)
  1550. break; /* bytes left, but not enough */
  1551. else
  1552. ND_PRINT((ndo, "\n\t %s", buf));
  1553. break;
  1554. case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
  1555. advance = decode_labeled_prefix4(ndo, tptr, len, buf, sizeof(buf));
  1556. if (advance == -1)
  1557. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1558. else if (advance == -2)
  1559. goto trunc;
  1560. else if (advance == -3)
  1561. break; /* bytes left, but not enough */
  1562. else
  1563. ND_PRINT((ndo, "\n\t %s", buf));
  1564. break;
  1565. case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
  1566. case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
  1567. case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
  1568. advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, sizeof(buf));
  1569. if (advance == -1)
  1570. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1571. else if (advance == -2)
  1572. goto trunc;
  1573. else
  1574. ND_PRINT((ndo, "\n\t %s", buf));
  1575. break;
  1576. case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
  1577. advance = decode_rt_routing_info(ndo, tptr, buf, sizeof(buf));
  1578. if (advance == -1)
  1579. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1580. else if (advance == -2)
  1581. goto trunc;
  1582. else
  1583. ND_PRINT((ndo, "\n\t %s", buf));
  1584. break;
  1585. case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
  1586. case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
  1587. advance = decode_multicast_vpn(ndo, tptr, buf, sizeof(buf));
  1588. if (advance == -1)
  1589. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1590. else if (advance == -2)
  1591. goto trunc;
  1592. else
  1593. ND_PRINT((ndo, "\n\t %s", buf));
  1594. break;
  1595. case (AFNUM_INET<<8 | SAFNUM_MDT):
  1596. advance = decode_mdt_vpn_nlri(ndo, tptr, buf, sizeof(buf));
  1597. if (advance == -1)
  1598. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1599. else if (advance == -2)
  1600. goto trunc;
  1601. else
  1602. ND_PRINT((ndo, "\n\t %s", buf));
  1603. break;
  1604. case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
  1605. case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
  1606. case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
  1607. advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
  1608. if (advance == -1)
  1609. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1610. else if (advance == -2)
  1611. goto trunc;
  1612. else if (advance == -3)
  1613. break; /* bytes left, but not enough */
  1614. else
  1615. ND_PRINT((ndo, "\n\t %s", buf));
  1616. break;
  1617. case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
  1618. advance = decode_labeled_prefix6(ndo, tptr, len, buf, sizeof(buf));
  1619. if (advance == -1)
  1620. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1621. else if (advance == -2)
  1622. goto trunc;
  1623. else if (advance == -3)
  1624. break; /* bytes left, but not enough */
  1625. else
  1626. ND_PRINT((ndo, "\n\t %s", buf));
  1627. break;
  1628. case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
  1629. case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
  1630. case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
  1631. advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, sizeof(buf));
  1632. if (advance == -1)
  1633. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1634. else if (advance == -2)
  1635. goto trunc;
  1636. else
  1637. ND_PRINT((ndo, "\n\t %s", buf));
  1638. break;
  1639. case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
  1640. case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
  1641. case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
  1642. case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
  1643. advance = decode_labeled_vpn_l2(ndo, tptr, buf, sizeof(buf));
  1644. if (advance == -1)
  1645. ND_PRINT((ndo, "\n\t (illegal length)"));
  1646. else if (advance == -2)
  1647. goto trunc;
  1648. else
  1649. ND_PRINT((ndo, "\n\t %s", buf));
  1650. break;
  1651. case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
  1652. case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
  1653. case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
  1654. advance = decode_clnp_prefix(ndo, tptr, buf, sizeof(buf));
  1655. if (advance == -1)
  1656. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1657. else if (advance == -2)
  1658. goto trunc;
  1659. else
  1660. ND_PRINT((ndo, "\n\t %s", buf));
  1661. break;
  1662. case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
  1663. case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
  1664. case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
  1665. advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, sizeof(buf));
  1666. if (advance == -1)
  1667. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1668. else if (advance == -2)
  1669. goto trunc;
  1670. else
  1671. ND_PRINT((ndo, "\n\t %s", buf));
  1672. break;
  1673. default:
  1674. ND_TCHECK2(*tptr,tlen);
  1675. ND_PRINT((ndo, "\n\t no AFI %u / SAFI %u decoder", af, safi));
  1676. if (ndo->ndo_vflag <= 1)
  1677. print_unknown_data(ndo, tptr, "\n\t ", tlen);
  1678. advance = 0;
  1679. tptr = pptr + len;
  1680. break;
  1681. }
  1682. if (advance < 0)
  1683. break;
  1684. tptr += advance;
  1685. }
  1686. done:
  1687. break;
  1688. case BGPTYPE_MP_UNREACH_NLRI:
  1689. ND_TCHECK2(tptr[0], BGP_MP_NLRI_MINSIZE);
  1690. af = EXTRACT_16BITS(tptr);
  1691. safi = tptr[2];
  1692. ND_PRINT((ndo, "\n\t AFI: %s (%u), %sSAFI: %s (%u)",
  1693. tok2str(af_values, "Unknown AFI", af),
  1694. af,
  1695. (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
  1696. tok2str(bgp_safi_values, "Unknown SAFI", safi),
  1697. safi));
  1698. if (len == BGP_MP_NLRI_MINSIZE)
  1699. ND_PRINT((ndo, "\n\t End-of-Rib Marker (empty NLRI)"));
  1700. tptr += 3;
  1701. while (tptr < pptr + len) {
  1702. switch (af<<8 | safi) {
  1703. case (AFNUM_INET<<8 | SAFNUM_UNICAST):
  1704. case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
  1705. case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
  1706. advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
  1707. if (advance == -1)
  1708. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1709. else if (advance == -2)
  1710. goto trunc;
  1711. else if (advance == -3)
  1712. break; /* bytes left, but not enough */
  1713. else
  1714. ND_PRINT((ndo, "\n\t %s", buf));
  1715. break;
  1716. case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
  1717. advance = decode_labeled_prefix4(ndo, tptr, len, buf, sizeof(buf));
  1718. if (advance == -1)
  1719. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1720. else if (advance == -2)
  1721. goto trunc;
  1722. else if (advance == -3)
  1723. break; /* bytes left, but not enough */
  1724. else
  1725. ND_PRINT((ndo, "\n\t %s", buf));
  1726. break;
  1727. case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
  1728. case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
  1729. case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
  1730. advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, sizeof(buf));
  1731. if (advance == -1)
  1732. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1733. else if (advance == -2)
  1734. goto trunc;
  1735. else
  1736. ND_PRINT((ndo, "\n\t %s", buf));
  1737. break;
  1738. case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
  1739. case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
  1740. case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
  1741. advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
  1742. if (advance == -1)
  1743. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1744. else if (advance == -2)
  1745. goto trunc;
  1746. else if (advance == -3)
  1747. break; /* bytes left, but not enough */
  1748. else
  1749. ND_PRINT((ndo, "\n\t %s", buf));
  1750. break;
  1751. case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
  1752. advance = decode_labeled_prefix6(ndo, tptr, len, buf, sizeof(buf));
  1753. if (advance == -1)
  1754. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1755. else if (advance == -2)
  1756. goto trunc;
  1757. else if (advance == -3)
  1758. break; /* bytes left, but not enough */
  1759. else
  1760. ND_PRINT((ndo, "\n\t %s", buf));
  1761. break;
  1762. case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
  1763. case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
  1764. case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
  1765. advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, sizeof(buf));
  1766. if (advance == -1)
  1767. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1768. else if (advance == -2)
  1769. goto trunc;
  1770. else
  1771. ND_PRINT((ndo, "\n\t %s", buf));
  1772. break;
  1773. case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
  1774. case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
  1775. case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
  1776. case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
  1777. advance = decode_labeled_vpn_l2(ndo, tptr, buf, sizeof(buf));
  1778. if (advance == -1)
  1779. ND_PRINT((ndo, "\n\t (illegal length)"));
  1780. else if (advance == -2)
  1781. goto trunc;
  1782. else
  1783. ND_PRINT((ndo, "\n\t %s", buf));
  1784. break;
  1785. case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
  1786. case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
  1787. case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
  1788. advance = decode_clnp_prefix(ndo, tptr, buf, sizeof(buf));
  1789. if (advance == -1)
  1790. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1791. else if (advance == -2)
  1792. goto trunc;
  1793. else
  1794. ND_PRINT((ndo, "\n\t %s", buf));
  1795. break;
  1796. case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
  1797. case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
  1798. case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
  1799. advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, sizeof(buf));
  1800. if (advance == -1)
  1801. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1802. else if (advance == -2)
  1803. goto trunc;
  1804. else
  1805. ND_PRINT((ndo, "\n\t %s", buf));
  1806. break;
  1807. case (AFNUM_INET<<8 | SAFNUM_MDT):
  1808. advance = decode_mdt_vpn_nlri(ndo, tptr, buf, sizeof(buf));
  1809. if (advance == -1)
  1810. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1811. else if (advance == -2)
  1812. goto trunc;
  1813. else
  1814. ND_PRINT((ndo, "\n\t %s", buf));
  1815. break;
  1816. case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
  1817. case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
  1818. advance = decode_multicast_vpn(ndo, tptr, buf, sizeof(buf));
  1819. if (advance == -1)
  1820. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  1821. else if (advance == -2)
  1822. goto trunc;
  1823. else
  1824. ND_PRINT((ndo, "\n\t %s", buf));
  1825. break;
  1826. default:
  1827. ND_TCHECK2(*(tptr-3),tlen);
  1828. ND_PRINT((ndo, "no AFI %u / SAFI %u decoder", af, safi));
  1829. if (ndo->ndo_vflag <= 1)
  1830. print_unknown_data(ndo, tptr-3, "\n\t ", tlen);
  1831. advance = 0;
  1832. tptr = pptr + len;
  1833. break;
  1834. }
  1835. if (advance < 0)
  1836. break;
  1837. tptr += advance;
  1838. }
  1839. break;
  1840. case BGPTYPE_EXTD_COMMUNITIES:
  1841. if (len % 8) {
  1842. ND_PRINT((ndo, "invalid len"));
  1843. break;
  1844. }
  1845. while (tlen>0) {
  1846. uint16_t extd_comm;
  1847. ND_TCHECK2(tptr[0], 2);
  1848. extd_comm=EXTRACT_16BITS(tptr);
  1849. ND_PRINT((ndo, "\n\t %s (0x%04x), Flags [%s]",
  1850. tok2str(bgp_extd_comm_subtype_values,
  1851. "unknown extd community typecode",
  1852. extd_comm),
  1853. extd_comm,
  1854. bittok2str(bgp_extd_comm_flag_values, "none", extd_comm)));
  1855. ND_TCHECK2(*(tptr+2), 6);
  1856. switch(extd_comm) {
  1857. case BGP_EXT_COM_RT_0:
  1858. case BGP_EXT_COM_RO_0:
  1859. case BGP_EXT_COM_L2VPN_RT_0:
  1860. ND_PRINT((ndo, ": %u:%u (= %s)",
  1861. EXTRACT_16BITS(tptr+2),
  1862. EXTRACT_32BITS(tptr+4),
  1863. ipaddr_string(ndo, tptr+4)));
  1864. break;
  1865. case BGP_EXT_COM_RT_1:
  1866. case BGP_EXT_COM_RO_1:
  1867. case BGP_EXT_COM_L2VPN_RT_1:
  1868. case BGP_EXT_COM_VRF_RT_IMP:
  1869. ND_PRINT((ndo, ": %s:%u",
  1870. ipaddr_string(ndo, tptr+2),
  1871. EXTRACT_16BITS(tptr+6)));
  1872. break;
  1873. case BGP_EXT_COM_RT_2:
  1874. case BGP_EXT_COM_RO_2:
  1875. ND_PRINT((ndo, ": %s:%u",
  1876. as_printf(ndo, astostr, sizeof(astostr),
  1877. EXTRACT_32BITS(tptr+2)), EXTRACT_16BITS(tptr+6)));
  1878. break;
  1879. case BGP_EXT_COM_LINKBAND:
  1880. bw.i = EXTRACT_32BITS(tptr+2);
  1881. ND_PRINT((ndo, ": bandwidth: %.3f Mbps",
  1882. bw.f*8/1000000));
  1883. break;
  1884. case BGP_EXT_COM_VPN_ORIGIN:
  1885. case BGP_EXT_COM_VPN_ORIGIN2:
  1886. case BGP_EXT_COM_VPN_ORIGIN3:
  1887. case BGP_EXT_COM_VPN_ORIGIN4:
  1888. case BGP_EXT_COM_OSPF_RID:
  1889. case BGP_EXT_COM_OSPF_RID2:
  1890. ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr+2)));
  1891. break;
  1892. case BGP_EXT_COM_OSPF_RTYPE:
  1893. case BGP_EXT_COM_OSPF_RTYPE2:
  1894. ND_PRINT((ndo, ": area:%s, router-type:%s, metric-type:%s%s",
  1895. ipaddr_string(ndo, tptr+2),
  1896. tok2str(bgp_extd_comm_ospf_rtype_values,
  1897. "unknown (0x%02x)",
  1898. *(tptr+6)),
  1899. (*(tptr+7) & BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "",
  1900. ((*(tptr+6) == BGP_OSPF_RTYPE_EXT) || (*(tptr+6) == BGP_OSPF_RTYPE_NSSA)) ? "E1" : ""));
  1901. break;
  1902. case BGP_EXT_COM_L2INFO:
  1903. ND_PRINT((ndo, ": %s Control Flags [0x%02x]:MTU %u",
  1904. tok2str(l2vpn_encaps_values,
  1905. "unknown encaps",
  1906. *(tptr+2)),
  1907. *(tptr+3),
  1908. EXTRACT_16BITS(tptr+4)));
  1909. break;
  1910. case BGP_EXT_COM_SOURCE_AS:
  1911. ND_PRINT((ndo, ": AS %u", EXTRACT_16BITS(tptr+2)));
  1912. break;
  1913. default:
  1914. ND_TCHECK2(*tptr,8);
  1915. print_unknown_data(ndo, tptr, "\n\t ", 8);
  1916. break;
  1917. }
  1918. tlen -=8;
  1919. tptr +=8;
  1920. }
  1921. break;
  1922. case BGPTYPE_PMSI_TUNNEL:
  1923. {
  1924. uint8_t tunnel_type, flags;
  1925. ND_TCHECK2(tptr[0], 5);
  1926. tunnel_type = *(tptr+1);
  1927. flags = *tptr;
  1928. tlen = len;
  1929. ND_PRINT((ndo, "\n\t Tunnel-type %s (%u), Flags [%s], MPLS Label %u",
  1930. tok2str(bgp_pmsi_tunnel_values, "Unknown", tunnel_type),
  1931. tunnel_type,
  1932. bittok2str(bgp_pmsi_flag_values, "none", flags),
  1933. EXTRACT_24BITS(tptr+2)>>4));
  1934. tptr +=5;
  1935. tlen -= 5;
  1936. switch (tunnel_type) {
  1937. case BGP_PMSI_TUNNEL_PIM_SM: /* fall through */
  1938. case BGP_PMSI_TUNNEL_PIM_BIDIR:
  1939. ND_TCHECK2(tptr[0], 8);
  1940. ND_PRINT((ndo, "\n\t Sender %s, P-Group %s",
  1941. ipaddr_string(ndo, tptr),
  1942. ipaddr_string(ndo, tptr+4)));
  1943. break;
  1944. case BGP_PMSI_TUNNEL_PIM_SSM:
  1945. ND_TCHECK2(tptr[0], 8);
  1946. ND_PRINT((ndo, "\n\t Root-Node %s, P-Group %s",
  1947. ipaddr_string(ndo, tptr),
  1948. ipaddr_string(ndo, tptr+4)));
  1949. break;
  1950. case BGP_PMSI_TUNNEL_INGRESS:
  1951. ND_TCHECK2(tptr[0], 4);
  1952. ND_PRINT((ndo, "\n\t Tunnel-Endpoint %s",
  1953. ipaddr_string(ndo, tptr)));
  1954. break;
  1955. case BGP_PMSI_TUNNEL_LDP_P2MP: /* fall through */
  1956. case BGP_PMSI_TUNNEL_LDP_MP2MP:
  1957. ND_TCHECK2(tptr[0], 8);
  1958. ND_PRINT((ndo, "\n\t Root-Node %s, LSP-ID 0x%08x",
  1959. ipaddr_string(ndo, tptr),
  1960. EXTRACT_32BITS(tptr+4)));
  1961. break;
  1962. case BGP_PMSI_TUNNEL_RSVP_P2MP:
  1963. ND_TCHECK2(tptr[0], 8);
  1964. ND_PRINT((ndo, "\n\t Extended-Tunnel-ID %s, P2MP-ID 0x%08x",
  1965. ipaddr_string(ndo, tptr),
  1966. EXTRACT_32BITS(tptr+4)));
  1967. break;
  1968. default:
  1969. if (ndo->ndo_vflag <= 1) {
  1970. print_unknown_data(ndo, tptr, "\n\t ", tlen);
  1971. }
  1972. }
  1973. break;
  1974. }
  1975. case BGPTYPE_AIGP:
  1976. {
  1977. uint8_t type;
  1978. uint16_t length;
  1979. tlen = len;
  1980. while (tlen >= 3) {
  1981. ND_TCHECK2(tptr[0], 3);
  1982. type = *tptr;
  1983. length = EXTRACT_16BITS(tptr+1);
  1984. tptr += 3;
  1985. tlen -= 3;
  1986. ND_PRINT((ndo, "\n\t %s TLV (%u), length %u",
  1987. tok2str(bgp_aigp_values, "Unknown", type),
  1988. type, length));
  1989. if (length < 3)
  1990. goto trunc;
  1991. length -= 3;
  1992. /*
  1993. * Check if we can read the TLV data.
  1994. */
  1995. ND_TCHECK2(tptr[3], length);
  1996. switch (type) {
  1997. case BGP_AIGP_TLV:
  1998. if (length < 8)
  1999. goto trunc;
  2000. ND_PRINT((ndo, ", metric %" PRIu64,
  2001. EXTRACT_64BITS(tptr)));
  2002. break;
  2003. default:
  2004. if (ndo->ndo_vflag <= 1) {
  2005. print_unknown_data(ndo, tptr,"\n\t ", length);
  2006. }
  2007. }
  2008. tptr += length;
  2009. tlen -= length;
  2010. }
  2011. break;
  2012. }
  2013. case BGPTYPE_ATTR_SET:
  2014. ND_TCHECK2(tptr[0], 4);
  2015. if (len < 4)
  2016. goto trunc;
  2017. ND_PRINT((ndo, "\n\t Origin AS: %s",
  2018. as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(tptr))));
  2019. tptr+=4;
  2020. len -=4;
  2021. while (len) {
  2022. u_int aflags, alenlen, alen;
  2023. ND_TCHECK2(tptr[0], 2);
  2024. if (len < 2)
  2025. goto trunc;
  2026. aflags = *tptr;
  2027. atype = *(tptr + 1);
  2028. tptr += 2;
  2029. len -= 2;
  2030. alenlen = bgp_attr_lenlen(aflags, tptr);
  2031. ND_TCHECK2(tptr[0], alenlen);
  2032. if (len < alenlen)
  2033. goto trunc;
  2034. alen = bgp_attr_len(aflags, tptr);
  2035. tptr += alenlen;
  2036. len -= alenlen;
  2037. ND_PRINT((ndo, "\n\t %s (%u), length: %u",
  2038. tok2str(bgp_attr_values,
  2039. "Unknown Attribute", atype),
  2040. atype,
  2041. alen));
  2042. if (aflags) {
  2043. ND_PRINT((ndo, ", Flags [%s%s%s%s",
  2044. aflags & 0x80 ? "O" : "",
  2045. aflags & 0x40 ? "T" : "",
  2046. aflags & 0x20 ? "P" : "",
  2047. aflags & 0x10 ? "E" : ""));
  2048. if (aflags & 0xf)
  2049. ND_PRINT((ndo, "+%x", aflags & 0xf));
  2050. ND_PRINT((ndo, "]: "));
  2051. }
  2052. /* FIXME check for recursion */
  2053. if (!bgp_attr_print(ndo, atype, tptr, alen))
  2054. return 0;
  2055. tptr += alen;
  2056. len -= alen;
  2057. }
  2058. break;
  2059. case BGPTYPE_LARGE_COMMUNITY:
  2060. if (len == 0 || len % 12) {
  2061. ND_PRINT((ndo, "invalid len"));
  2062. break;
  2063. }
  2064. ND_PRINT((ndo, "\n\t "));
  2065. while (len > 0) {
  2066. ND_TCHECK2(*tptr, 12);
  2067. ND_PRINT((ndo, "%u:%u:%u%s",
  2068. EXTRACT_32BITS(tptr),
  2069. EXTRACT_32BITS(tptr + 4),
  2070. EXTRACT_32BITS(tptr + 8),
  2071. (len > 12) ? ", " : ""));
  2072. tptr += 12;
  2073. len -= 12;
  2074. }
  2075. break;
  2076. default:
  2077. ND_TCHECK2(*pptr,len);
  2078. ND_PRINT((ndo, "\n\t no Attribute %u decoder", atype)); /* we have no decoder for the attribute */
  2079. if (ndo->ndo_vflag <= 1)
  2080. print_unknown_data(ndo, pptr, "\n\t ", len);
  2081. break;
  2082. }
  2083. if (ndo->ndo_vflag > 1 && len) { /* omit zero length attributes*/
  2084. ND_TCHECK2(*pptr,len);
  2085. print_unknown_data(ndo, pptr, "\n\t ", len);
  2086. }
  2087. return 1;
  2088. trunc:
  2089. return 0;
  2090. }
  2091. static void
  2092. bgp_capabilities_print(netdissect_options *ndo,
  2093. const u_char *opt, int caps_len)
  2094. {
  2095. int cap_type, cap_len, tcap_len, cap_offset;
  2096. int i = 0;
  2097. while (i < caps_len) {
  2098. ND_TCHECK2(opt[i], BGP_CAP_HEADER_SIZE);
  2099. cap_type=opt[i];
  2100. cap_len=opt[i+1];
  2101. tcap_len=cap_len;
  2102. ND_PRINT((ndo, "\n\t %s (%u), length: %u",
  2103. tok2str(bgp_capcode_values, "Unknown",
  2104. cap_type),
  2105. cap_type,
  2106. cap_len));
  2107. ND_TCHECK2(opt[i+2], cap_len);
  2108. switch (cap_type) {
  2109. case BGP_CAPCODE_MP:
  2110. ND_PRINT((ndo, "\n\t\tAFI %s (%u), SAFI %s (%u)",
  2111. tok2str(af_values, "Unknown",
  2112. EXTRACT_16BITS(opt+i+2)),
  2113. EXTRACT_16BITS(opt+i+2),
  2114. tok2str(bgp_safi_values, "Unknown",
  2115. opt[i+5]),
  2116. opt[i+5]));
  2117. break;
  2118. case BGP_CAPCODE_RESTART:
  2119. ND_PRINT((ndo, "\n\t\tRestart Flags: [%s], Restart Time %us",
  2120. ((opt[i+2])&0x80) ? "R" : "none",
  2121. EXTRACT_16BITS(opt+i+2)&0xfff));
  2122. tcap_len-=2;
  2123. cap_offset=4;
  2124. while(tcap_len>=4) {
  2125. ND_PRINT((ndo, "\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
  2126. tok2str(af_values,"Unknown",
  2127. EXTRACT_16BITS(opt+i+cap_offset)),
  2128. EXTRACT_16BITS(opt+i+cap_offset),
  2129. tok2str(bgp_safi_values,"Unknown",
  2130. opt[i+cap_offset+2]),
  2131. opt[i+cap_offset+2],
  2132. ((opt[i+cap_offset+3])&0x80) ? "yes" : "no" ));
  2133. tcap_len-=4;
  2134. cap_offset+=4;
  2135. }
  2136. break;
  2137. case BGP_CAPCODE_RR:
  2138. case BGP_CAPCODE_RR_CISCO:
  2139. break;
  2140. case BGP_CAPCODE_AS_NEW:
  2141. /*
  2142. * Extract the 4 byte AS number encoded.
  2143. */
  2144. if (cap_len == 4) {
  2145. ND_PRINT((ndo, "\n\t\t 4 Byte AS %s",
  2146. as_printf(ndo, astostr, sizeof(astostr),
  2147. EXTRACT_32BITS(opt + i + 2))));
  2148. }
  2149. break;
  2150. case BGP_CAPCODE_ADD_PATH:
  2151. cap_offset=2;
  2152. if (tcap_len == 0) {
  2153. ND_PRINT((ndo, " (bogus)")); /* length */
  2154. break;
  2155. }
  2156. while (tcap_len > 0) {
  2157. if (tcap_len < 4) {
  2158. ND_PRINT((ndo, "\n\t\t(invalid)"));
  2159. break;
  2160. }
  2161. ND_PRINT((ndo, "\n\t\tAFI %s (%u), SAFI %s (%u), Send/Receive: %s",
  2162. tok2str(af_values,"Unknown",EXTRACT_16BITS(opt+i+cap_offset)),
  2163. EXTRACT_16BITS(opt+i+cap_offset),
  2164. tok2str(bgp_safi_values,"Unknown",opt[i+cap_offset+2]),
  2165. opt[i+cap_offset+2],
  2166. tok2str(bgp_add_path_recvsend,"Bogus (0x%02x)",opt[i+cap_offset+3])
  2167. ));
  2168. tcap_len-=4;
  2169. cap_offset+=4;
  2170. }
  2171. break;
  2172. default:
  2173. ND_PRINT((ndo, "\n\t\tno decoder for Capability %u",
  2174. cap_type));
  2175. if (ndo->ndo_vflag <= 1)
  2176. print_unknown_data(ndo, &opt[i+2], "\n\t\t", cap_len);
  2177. break;
  2178. }
  2179. if (ndo->ndo_vflag > 1 && cap_len > 0) {
  2180. print_unknown_data(ndo, &opt[i+2], "\n\t\t", cap_len);
  2181. }
  2182. i += BGP_CAP_HEADER_SIZE + cap_len;
  2183. }
  2184. return;
  2185. trunc:
  2186. ND_PRINT((ndo, "[|BGP]"));
  2187. }
  2188. static void
  2189. bgp_open_print(netdissect_options *ndo,
  2190. const u_char *dat, int length)
  2191. {
  2192. struct bgp_open bgpo;
  2193. struct bgp_opt bgpopt;
  2194. const u_char *opt;
  2195. int i;
  2196. ND_TCHECK2(dat[0], BGP_OPEN_SIZE);
  2197. memcpy(&bgpo, dat, BGP_OPEN_SIZE);
  2198. ND_PRINT((ndo, "\n\t Version %d, ", bgpo.bgpo_version));
  2199. ND_PRINT((ndo, "my AS %s, ",
  2200. as_printf(ndo, astostr, sizeof(astostr), ntohs(bgpo.bgpo_myas))));
  2201. ND_PRINT((ndo, "Holdtime %us, ", ntohs(bgpo.bgpo_holdtime)));
  2202. ND_PRINT((ndo, "ID %s", ipaddr_string(ndo, &bgpo.bgpo_id)));
  2203. ND_PRINT((ndo, "\n\t Optional parameters, length: %u", bgpo.bgpo_optlen));
  2204. /* some little sanity checking */
  2205. if (length < bgpo.bgpo_optlen+BGP_OPEN_SIZE)
  2206. return;
  2207. /* ugly! */
  2208. opt = &((const struct bgp_open *)dat)->bgpo_optlen;
  2209. opt++;
  2210. i = 0;
  2211. while (i < bgpo.bgpo_optlen) {
  2212. ND_TCHECK2(opt[i], BGP_OPT_SIZE);
  2213. memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE);
  2214. if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) {
  2215. ND_PRINT((ndo, "\n\t Option %d, length: %u", bgpopt.bgpopt_type, bgpopt.bgpopt_len));
  2216. break;
  2217. }
  2218. ND_PRINT((ndo, "\n\t Option %s (%u), length: %u",
  2219. tok2str(bgp_opt_values,"Unknown",
  2220. bgpopt.bgpopt_type),
  2221. bgpopt.bgpopt_type,
  2222. bgpopt.bgpopt_len));
  2223. /* now let's decode the options we know*/
  2224. switch(bgpopt.bgpopt_type) {
  2225. case BGP_OPT_CAP:
  2226. bgp_capabilities_print(ndo, &opt[i+BGP_OPT_SIZE],
  2227. bgpopt.bgpopt_len);
  2228. break;
  2229. case BGP_OPT_AUTH:
  2230. default:
  2231. ND_PRINT((ndo, "\n\t no decoder for option %u",
  2232. bgpopt.bgpopt_type));
  2233. break;
  2234. }
  2235. i += BGP_OPT_SIZE + bgpopt.bgpopt_len;
  2236. }
  2237. return;
  2238. trunc:
  2239. ND_PRINT((ndo, "[|BGP]"));
  2240. }
  2241. static void
  2242. bgp_update_print(netdissect_options *ndo,
  2243. const u_char *dat, int length)
  2244. {
  2245. struct bgp bgp;
  2246. const u_char *p;
  2247. int withdrawn_routes_len;
  2248. int len;
  2249. int i;
  2250. ND_TCHECK2(dat[0], BGP_SIZE);
  2251. if (length < BGP_SIZE)
  2252. goto trunc;
  2253. memcpy(&bgp, dat, BGP_SIZE);
  2254. p = dat + BGP_SIZE; /*XXX*/
  2255. length -= BGP_SIZE;
  2256. /* Unfeasible routes */
  2257. ND_TCHECK2(p[0], 2);
  2258. if (length < 2)
  2259. goto trunc;
  2260. withdrawn_routes_len = EXTRACT_16BITS(p);
  2261. p += 2;
  2262. length -= 2;
  2263. if (withdrawn_routes_len) {
  2264. /*
  2265. * Without keeping state from the original NLRI message,
  2266. * it's not possible to tell if this a v4 or v6 route,
  2267. * so only try to decode it if we're not v6 enabled.
  2268. */
  2269. ND_TCHECK2(p[0], withdrawn_routes_len);
  2270. if (length < withdrawn_routes_len)
  2271. goto trunc;
  2272. ND_PRINT((ndo, "\n\t Withdrawn routes: %d bytes", withdrawn_routes_len));
  2273. p += withdrawn_routes_len;
  2274. length -= withdrawn_routes_len;
  2275. }
  2276. ND_TCHECK2(p[0], 2);
  2277. if (length < 2)
  2278. goto trunc;
  2279. len = EXTRACT_16BITS(p);
  2280. p += 2;
  2281. length -= 2;
  2282. if (withdrawn_routes_len == 0 && len == 0 && length == 0) {
  2283. /* No withdrawn routes, no path attributes, no NLRI */
  2284. ND_PRINT((ndo, "\n\t End-of-Rib Marker (empty NLRI)"));
  2285. return;
  2286. }
  2287. if (len) {
  2288. /* do something more useful!*/
  2289. while (len) {
  2290. int aflags, atype, alenlen, alen;
  2291. ND_TCHECK2(p[0], 2);
  2292. if (len < 2)
  2293. goto trunc;
  2294. if (length < 2)
  2295. goto trunc;
  2296. aflags = *p;
  2297. atype = *(p + 1);
  2298. p += 2;
  2299. len -= 2;
  2300. length -= 2;
  2301. alenlen = bgp_attr_lenlen(aflags, p);
  2302. ND_TCHECK2(p[0], alenlen);
  2303. if (len < alenlen)
  2304. goto trunc;
  2305. if (length < alenlen)
  2306. goto trunc;
  2307. alen = bgp_attr_len(aflags, p);
  2308. p += alenlen;
  2309. len -= alenlen;
  2310. length -= alenlen;
  2311. ND_PRINT((ndo, "\n\t %s (%u), length: %u",
  2312. tok2str(bgp_attr_values, "Unknown Attribute",
  2313. atype),
  2314. atype,
  2315. alen));
  2316. if (aflags) {
  2317. ND_PRINT((ndo, ", Flags [%s%s%s%s",
  2318. aflags & 0x80 ? "O" : "",
  2319. aflags & 0x40 ? "T" : "",
  2320. aflags & 0x20 ? "P" : "",
  2321. aflags & 0x10 ? "E" : ""));
  2322. if (aflags & 0xf)
  2323. ND_PRINT((ndo, "+%x", aflags & 0xf));
  2324. ND_PRINT((ndo, "]: "));
  2325. }
  2326. if (len < alen)
  2327. goto trunc;
  2328. if (length < alen)
  2329. goto trunc;
  2330. if (!bgp_attr_print(ndo, atype, p, alen))
  2331. goto trunc;
  2332. p += alen;
  2333. len -= alen;
  2334. length -= alen;
  2335. }
  2336. }
  2337. if (length) {
  2338. /*
  2339. * XXX - what if they're using the "Advertisement of
  2340. * Multiple Paths in BGP" feature:
  2341. *
  2342. * https://datatracker.ietf.org/doc/draft-ietf-idr-add-paths/
  2343. *
  2344. * http://tools.ietf.org/html/draft-ietf-idr-add-paths-06
  2345. */
  2346. ND_PRINT((ndo, "\n\t Updated routes:"));
  2347. while (length) {
  2348. char buf[MAXHOSTNAMELEN + 100];
  2349. i = decode_prefix4(ndo, p, length, buf, sizeof(buf));
  2350. if (i == -1) {
  2351. ND_PRINT((ndo, "\n\t (illegal prefix length)"));
  2352. break;
  2353. } else if (i == -2)
  2354. goto trunc;
  2355. else if (i == -3)
  2356. goto trunc; /* bytes left, but not enough */
  2357. else {
  2358. ND_PRINT((ndo, "\n\t %s", buf));
  2359. p += i;
  2360. length -= i;
  2361. }
  2362. }
  2363. }
  2364. return;
  2365. trunc:
  2366. ND_PRINT((ndo, "[|BGP]"));
  2367. }
  2368. static void
  2369. bgp_notification_print(netdissect_options *ndo,
  2370. const u_char *dat, int length)
  2371. {
  2372. struct bgp_notification bgpn;
  2373. const u_char *tptr;
  2374. ND_TCHECK2(dat[0], BGP_NOTIFICATION_SIZE);
  2375. memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE);
  2376. /* some little sanity checking */
  2377. if (length<BGP_NOTIFICATION_SIZE)
  2378. return;
  2379. ND_PRINT((ndo, ", %s (%u)",
  2380. tok2str(bgp_notify_major_values, "Unknown Error",
  2381. bgpn.bgpn_major),
  2382. bgpn.bgpn_major));
  2383. switch (bgpn.bgpn_major) {
  2384. case BGP_NOTIFY_MAJOR_MSG:
  2385. ND_PRINT((ndo, ", subcode %s (%u)",
  2386. tok2str(bgp_notify_minor_msg_values, "Unknown",
  2387. bgpn.bgpn_minor),
  2388. bgpn.bgpn_minor));
  2389. break;
  2390. case BGP_NOTIFY_MAJOR_OPEN:
  2391. ND_PRINT((ndo, ", subcode %s (%u)",
  2392. tok2str(bgp_notify_minor_open_values, "Unknown",
  2393. bgpn.bgpn_minor),
  2394. bgpn.bgpn_minor));
  2395. break;
  2396. case BGP_NOTIFY_MAJOR_UPDATE:
  2397. ND_PRINT((ndo, ", subcode %s (%u)",
  2398. tok2str(bgp_notify_minor_update_values, "Unknown",
  2399. bgpn.bgpn_minor),
  2400. bgpn.bgpn_minor));
  2401. break;
  2402. case BGP_NOTIFY_MAJOR_FSM:
  2403. ND_PRINT((ndo, " subcode %s (%u)",
  2404. tok2str(bgp_notify_minor_fsm_values, "Unknown",
  2405. bgpn.bgpn_minor),
  2406. bgpn.bgpn_minor));
  2407. break;
  2408. case BGP_NOTIFY_MAJOR_CAP:
  2409. ND_PRINT((ndo, " subcode %s (%u)",
  2410. tok2str(bgp_notify_minor_cap_values, "Unknown",
  2411. bgpn.bgpn_minor),
  2412. bgpn.bgpn_minor));
  2413. break;
  2414. case BGP_NOTIFY_MAJOR_CEASE:
  2415. ND_PRINT((ndo, ", subcode %s (%u)",
  2416. tok2str(bgp_notify_minor_cease_values, "Unknown",
  2417. bgpn.bgpn_minor),
  2418. bgpn.bgpn_minor));
  2419. /* draft-ietf-idr-cease-subcode-02 mentions optionally 7 bytes
  2420. * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES
  2421. */
  2422. if(bgpn.bgpn_minor == BGP_NOTIFY_MINOR_CEASE_MAXPRFX && length >= BGP_NOTIFICATION_SIZE + 7) {
  2423. tptr = dat + BGP_NOTIFICATION_SIZE;
  2424. ND_TCHECK2(*tptr, 7);
  2425. ND_PRINT((ndo, ", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u",
  2426. tok2str(af_values, "Unknown",
  2427. EXTRACT_16BITS(tptr)),
  2428. EXTRACT_16BITS(tptr),
  2429. tok2str(bgp_safi_values, "Unknown", *(tptr+2)),
  2430. *(tptr+2),
  2431. EXTRACT_32BITS(tptr+3)));
  2432. }
  2433. break;
  2434. default:
  2435. break;
  2436. }
  2437. return;
  2438. trunc:
  2439. ND_PRINT((ndo, "[|BGP]"));
  2440. }
  2441. static void
  2442. bgp_route_refresh_print(netdissect_options *ndo,
  2443. const u_char *pptr, int len)
  2444. {
  2445. const struct bgp_route_refresh *bgp_route_refresh_header;
  2446. ND_TCHECK2(pptr[0], BGP_ROUTE_REFRESH_SIZE);
  2447. /* some little sanity checking */
  2448. if (len<BGP_ROUTE_REFRESH_SIZE)
  2449. return;
  2450. bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr;
  2451. ND_PRINT((ndo, "\n\t AFI %s (%u), SAFI %s (%u)",
  2452. tok2str(af_values,"Unknown",
  2453. /* this stinks but the compiler pads the structure
  2454. * weird */
  2455. EXTRACT_16BITS(&bgp_route_refresh_header->afi)),
  2456. EXTRACT_16BITS(&bgp_route_refresh_header->afi),
  2457. tok2str(bgp_safi_values,"Unknown",
  2458. bgp_route_refresh_header->safi),
  2459. bgp_route_refresh_header->safi));
  2460. if (ndo->ndo_vflag > 1) {
  2461. ND_TCHECK2(*pptr, len);
  2462. print_unknown_data(ndo, pptr, "\n\t ", len);
  2463. }
  2464. return;
  2465. trunc:
  2466. ND_PRINT((ndo, "[|BGP]"));
  2467. }
  2468. static int
  2469. bgp_header_print(netdissect_options *ndo,
  2470. const u_char *dat, int length)
  2471. {
  2472. struct bgp bgp;
  2473. ND_TCHECK2(dat[0], BGP_SIZE);
  2474. memcpy(&bgp, dat, BGP_SIZE);
  2475. ND_PRINT((ndo, "\n\t%s Message (%u), length: %u",
  2476. tok2str(bgp_msg_values, "Unknown", bgp.bgp_type),
  2477. bgp.bgp_type,
  2478. length));
  2479. switch (bgp.bgp_type) {
  2480. case BGP_OPEN:
  2481. bgp_open_print(ndo, dat, length);
  2482. break;
  2483. case BGP_UPDATE:
  2484. bgp_update_print(ndo, dat, length);
  2485. break;
  2486. case BGP_NOTIFICATION:
  2487. bgp_notification_print(ndo, dat, length);
  2488. break;
  2489. case BGP_KEEPALIVE:
  2490. break;
  2491. case BGP_ROUTE_REFRESH:
  2492. bgp_route_refresh_print(ndo, dat, length);
  2493. break;
  2494. default:
  2495. /* we have no decoder for the BGP message */
  2496. ND_TCHECK2(*dat, length);
  2497. ND_PRINT((ndo, "\n\t no Message %u decoder", bgp.bgp_type));
  2498. print_unknown_data(ndo, dat, "\n\t ", length);
  2499. break;
  2500. }
  2501. return 1;
  2502. trunc:
  2503. ND_PRINT((ndo, "[|BGP]"));
  2504. return 0;
  2505. }
  2506. void
  2507. bgp_print(netdissect_options *ndo,
  2508. const u_char *dat, int length)
  2509. {
  2510. const u_char *p;
  2511. const u_char *ep;
  2512. const u_char *start;
  2513. const u_char marker[] = {
  2514. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  2515. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  2516. };
  2517. struct bgp bgp;
  2518. uint16_t hlen;
  2519. ep = dat + length;
  2520. if (ndo->ndo_snapend < dat + length)
  2521. ep = ndo->ndo_snapend;
  2522. ND_PRINT((ndo, ": BGP"));
  2523. if (ndo->ndo_vflag < 1) /* lets be less chatty */
  2524. return;
  2525. p = dat;
  2526. start = p;
  2527. while (p < ep) {
  2528. if (!ND_TTEST2(p[0], 1))
  2529. break;
  2530. if (p[0] != 0xff) {
  2531. p++;
  2532. continue;
  2533. }
  2534. if (!ND_TTEST2(p[0], sizeof(marker)))
  2535. break;
  2536. if (memcmp(p, marker, sizeof(marker)) != 0) {
  2537. p++;
  2538. continue;
  2539. }
  2540. /* found BGP header */
  2541. ND_TCHECK2(p[0], BGP_SIZE); /*XXX*/
  2542. memcpy(&bgp, p, BGP_SIZE);
  2543. if (start != p)
  2544. ND_PRINT((ndo, " [|BGP]"));
  2545. hlen = ntohs(bgp.bgp_len);
  2546. if (hlen < BGP_SIZE) {
  2547. ND_PRINT((ndo, "\n[|BGP Bogus header length %u < %u]", hlen,
  2548. BGP_SIZE));
  2549. break;
  2550. }
  2551. if (ND_TTEST2(p[0], hlen)) {
  2552. if (!bgp_header_print(ndo, p, hlen))
  2553. return;
  2554. p += hlen;
  2555. start = p;
  2556. } else {
  2557. ND_PRINT((ndo, "\n[|BGP %s]",
  2558. tok2str(bgp_msg_values,
  2559. "Unknown Message Type",
  2560. bgp.bgp_type)));
  2561. break;
  2562. }
  2563. }
  2564. return;
  2565. trunc:
  2566. ND_PRINT((ndo, " [|BGP]"));
  2567. }
  2568. /*
  2569. * Local Variables:
  2570. * c-style: whitesmith
  2571. * c-basic-offset: 4
  2572. * End:
  2573. */