bgp_encap_tlv.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /*
  2. * Copyright 2015, LabN Consulting, L.L.C.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. */
  19. #include <zebra.h>
  20. #include "memory.h"
  21. #include "prefix.h"
  22. #include "vty.h"
  23. #include "filter.h"
  24. #include "bgpd.h"
  25. #include "bgp_attr.h"
  26. #include "bgp_encap_types.h"
  27. #include "bgp_encap_tlv.h"
  28. /***********************************************************************
  29. * SUBTLV ENCODE
  30. ***********************************************************************/
  31. /* rfc5512 4.1 */
  32. static struct bgp_attr_encap_subtlv *
  33. subtlv_encode_encap_l2tpv3_over_ip(
  34. struct bgp_tea_subtlv_encap_l2tpv3_over_ip *st)
  35. {
  36. struct bgp_attr_encap_subtlv *new;
  37. uint8_t *p;
  38. int total = 4 + st->cookie_length;
  39. /* sanity check */
  40. assert(st->cookie_length <= sizeof(st->cookie));
  41. assert(total <= 0xff);
  42. new = XCALLOC(MTYPE_ENCAP_TLV, sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
  43. assert(new);
  44. new->type = BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION;
  45. new->length = total;
  46. p = new->value;
  47. *p++ = (st->sessionid & 0xff000000) >> 24;
  48. *p++ = (st->sessionid & 0xff0000) >> 16;
  49. *p++ = (st->sessionid & 0xff00) >> 8;
  50. *p++ = (st->sessionid & 0xff);
  51. memcpy(p, st->cookie, st->cookie_length);
  52. return new;
  53. }
  54. /* rfc5512 4.1 */
  55. static struct bgp_attr_encap_subtlv *
  56. subtlv_encode_encap_gre(
  57. struct bgp_tea_subtlv_encap_gre_key *st)
  58. {
  59. struct bgp_attr_encap_subtlv *new;
  60. uint8_t *p;
  61. int total = 4;
  62. assert(total <= 0xff);
  63. new = XCALLOC(MTYPE_ENCAP_TLV, sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
  64. assert(new);
  65. new->type = BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION;
  66. new->length = total;
  67. p = new->value;
  68. *p++ = (st->gre_key & 0xff000000) >> 24;
  69. *p++ = (st->gre_key & 0xff0000) >> 16;
  70. *p++ = (st->gre_key & 0xff00) >> 8;
  71. *p++ = (st->gre_key & 0xff);
  72. return new;
  73. }
  74. static struct bgp_attr_encap_subtlv *
  75. subtlv_encode_encap_pbb(
  76. struct bgp_tea_subtlv_encap_pbb *st)
  77. {
  78. struct bgp_attr_encap_subtlv *new;
  79. uint8_t *p;
  80. int total = 1 + 3 + 6 + 2; /* flags + isid + madaddr + vid */
  81. assert(total <= 0xff);
  82. new = XCALLOC(MTYPE_ENCAP_TLV, sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
  83. assert(new);
  84. new->type = BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION;
  85. new->length = total;
  86. p = new->value;
  87. *p++ = (st->flag_isid? 0x80: 0) |
  88. (st->flag_vid? 0x40: 0) |
  89. 0;
  90. if (st->flag_isid) {
  91. *p = (st->isid & 0xff0000) >> 16;
  92. *(p+1) = (st->isid & 0xff00) >> 8;
  93. *(p+2) = (st->isid & 0xff);
  94. }
  95. p += 3;
  96. memcpy(p, st->macaddr, 6);
  97. p += 6;
  98. if (st->flag_vid) {
  99. *p++ = (st->vid & 0xf00) >> 8;
  100. *p++ = st->vid & 0xff;
  101. }
  102. return new;
  103. }
  104. /* rfc5512 4.2 */
  105. static struct bgp_attr_encap_subtlv *
  106. subtlv_encode_proto_type(
  107. struct bgp_tea_subtlv_proto_type *st)
  108. {
  109. struct bgp_attr_encap_subtlv *new;
  110. uint8_t *p;
  111. int total = 2;
  112. assert(total <= 0xff);
  113. new = XCALLOC(MTYPE_ENCAP_TLV, sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
  114. assert(new);
  115. new->type = BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE;
  116. new->length = total;
  117. p = new->value;
  118. *p++ = (st->proto & 0xff00) >> 8;
  119. *p++ = (st->proto & 0xff);
  120. return new;
  121. }
  122. /* rfc5512 4.3 */
  123. static struct bgp_attr_encap_subtlv *
  124. subtlv_encode_color(
  125. struct bgp_tea_subtlv_color *st)
  126. {
  127. struct bgp_attr_encap_subtlv *new;
  128. uint8_t *p;
  129. int total = 8;
  130. assert(total <= 0xff);
  131. new = XCALLOC(MTYPE_ENCAP_TLV, sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
  132. assert(new);
  133. new->type = BGP_ENCAP_SUBTLV_TYPE_COLOR;
  134. new->length = total;
  135. p = new->value;
  136. *p++ = 0x03; /* transitive*/
  137. *p++ = 0x0b;
  138. *p++ = 0; /* reserved */
  139. *p++ = 0; /* reserved */
  140. *p++ = (st->color & 0xff000000) >> 24;
  141. *p++ = (st->color & 0xff0000) >> 16;
  142. *p++ = (st->color & 0xff00) >> 8;
  143. *p++ = (st->color & 0xff);
  144. return new;
  145. }
  146. /* rfc 5566 4. */
  147. static struct bgp_attr_encap_subtlv *
  148. subtlv_encode_ipsec_ta(
  149. struct bgp_tea_subtlv_ipsec_ta *st)
  150. {
  151. struct bgp_attr_encap_subtlv *new;
  152. uint8_t *p;
  153. int total = 2 + st->authenticator_length;
  154. /* sanity check */
  155. assert(st->authenticator_length <= sizeof(st->value));
  156. assert(total <= 0xff);
  157. new = XCALLOC(MTYPE_ENCAP_TLV, sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
  158. assert(new);
  159. new->type = BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA;
  160. new->length = total;
  161. p = new->value;
  162. *p++ = (st->authenticator_type & 0xff00) >> 8;
  163. *p++ = st->authenticator_type & 0xff;
  164. memcpy(p, st->value, st->authenticator_length);
  165. return new;
  166. }
  167. /* draft-rosen-idr-tunnel-encaps 2.1 */
  168. static struct bgp_attr_encap_subtlv *
  169. subtlv_encode_remote_endpoint(
  170. struct bgp_tea_subtlv_remote_endpoint *st)
  171. {
  172. struct bgp_attr_encap_subtlv *new;
  173. uint8_t *p;
  174. int total = (st->family==AF_INET?8:20);
  175. assert(total <= 0xff);
  176. new = XCALLOC(MTYPE_ENCAP_TLV, sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
  177. assert(new);
  178. new->type = BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT;
  179. new->length = total;
  180. p = new->value;
  181. if (st->family == AF_INET) {
  182. memcpy (p, &(st->ip_address.v4.s_addr), 4);
  183. p+=4;
  184. } else {
  185. assert (st->family == AF_INET6);
  186. memcpy (p, &(st->ip_address.v6.s6_addr), 16);
  187. p+=16;
  188. }
  189. memcpy (p, &(st->as4), 4);
  190. return new;
  191. }
  192. /***********************************************************************
  193. * TUNNEL TYPE-SPECIFIC TLV ENCODE
  194. ***********************************************************************/
  195. /*
  196. * requires "extra" and "last" to be defined in caller
  197. */
  198. #define ENC_SUBTLV(flag, function, field) do {\
  199. struct bgp_attr_encap_subtlv *new;\
  200. if (CHECK_FLAG(bet->valid_subtlvs, (flag))) {\
  201. new = function(&bet->field);\
  202. if (last) {\
  203. last->next = new;\
  204. } else {\
  205. extra->encap_subtlvs = new;\
  206. }\
  207. last = new;\
  208. }\
  209. } while (0)
  210. void
  211. bgp_encap_type_l2tpv3overip_to_tlv(
  212. struct bgp_encap_type_l2tpv3_over_ip *bet, /* input structure */
  213. struct attr *attr)
  214. {
  215. struct attr_extra *extra = bgp_attr_extra_get(attr);
  216. struct bgp_attr_encap_subtlv *last;
  217. /* advance to last subtlv */
  218. for (last = extra->encap_subtlvs; last && last->next; last = last->next);
  219. extra->encap_tunneltype = BGP_ENCAP_TYPE_L2TPV3_OVER_IP;
  220. assert(CHECK_FLAG(bet->valid_subtlvs, BGP_TEA_SUBTLV_ENCAP));
  221. ENC_SUBTLV(BGP_TEA_SUBTLV_ENCAP, subtlv_encode_encap_l2tpv3_over_ip, st_encap);
  222. ENC_SUBTLV(BGP_TEA_SUBTLV_PROTO_TYPE, subtlv_encode_proto_type, st_proto);
  223. ENC_SUBTLV(BGP_TEA_SUBTLV_COLOR, subtlv_encode_color, st_color);
  224. ENC_SUBTLV(BGP_TEA_SUBTLV_REMOTE_ENDPOINT, subtlv_encode_remote_endpoint, st_endpoint);
  225. }
  226. void
  227. bgp_encap_type_gre_to_tlv(
  228. struct bgp_encap_type_gre *bet, /* input structure */
  229. struct attr *attr)
  230. {
  231. struct attr_extra *extra = bgp_attr_extra_get(attr);
  232. struct bgp_attr_encap_subtlv *last;
  233. /* advance to last subtlv */
  234. for (last = extra->encap_subtlvs; last && last->next; last = last->next);
  235. extra->encap_tunneltype = BGP_ENCAP_TYPE_GRE;
  236. ENC_SUBTLV(BGP_TEA_SUBTLV_ENCAP, subtlv_encode_encap_gre, st_encap);
  237. ENC_SUBTLV(BGP_TEA_SUBTLV_PROTO_TYPE, subtlv_encode_proto_type, st_proto);
  238. ENC_SUBTLV(BGP_TEA_SUBTLV_COLOR, subtlv_encode_color, st_color);
  239. ENC_SUBTLV(BGP_TEA_SUBTLV_REMOTE_ENDPOINT, subtlv_encode_remote_endpoint, st_endpoint);
  240. }
  241. void
  242. bgp_encap_type_ip_in_ip_to_tlv(
  243. struct bgp_encap_type_ip_in_ip *bet, /* input structure */
  244. struct attr *attr)
  245. {
  246. struct attr_extra *extra = bgp_attr_extra_get(attr);
  247. struct bgp_attr_encap_subtlv *last;
  248. /* advance to last subtlv */
  249. for (last = extra->encap_subtlvs; last && last->next; last = last->next);
  250. extra->encap_tunneltype = BGP_ENCAP_TYPE_IP_IN_IP;
  251. ENC_SUBTLV(BGP_TEA_SUBTLV_PROTO_TYPE, subtlv_encode_proto_type, st_proto);
  252. ENC_SUBTLV(BGP_TEA_SUBTLV_COLOR, subtlv_encode_color, st_color);
  253. ENC_SUBTLV(BGP_TEA_SUBTLV_REMOTE_ENDPOINT, subtlv_encode_remote_endpoint, st_endpoint);
  254. }
  255. void
  256. bgp_encap_type_transmit_tunnel_endpoint(
  257. struct bgp_encap_type_transmit_tunnel_endpoint *bet, /* input structure */
  258. struct attr *attr)
  259. {
  260. struct attr_extra *extra = bgp_attr_extra_get(attr);
  261. struct bgp_attr_encap_subtlv *last;
  262. /* advance to last subtlv */
  263. for (last = extra->encap_subtlvs; last && last->next; last = last->next);
  264. extra->encap_tunneltype = BGP_ENCAP_TYPE_TRANSMIT_TUNNEL_ENDPOINT;
  265. /* no subtlvs for this type */
  266. }
  267. void
  268. bgp_encap_type_ipsec_in_tunnel_mode_to_tlv(
  269. struct bgp_encap_type_ipsec_in_tunnel_mode *bet, /* input structure */
  270. struct attr *attr)
  271. {
  272. struct attr_extra *extra = bgp_attr_extra_get(attr);
  273. struct bgp_attr_encap_subtlv *last;
  274. /* advance to last subtlv */
  275. for (last = extra->encap_subtlvs; last && last->next; last = last->next);
  276. extra->encap_tunneltype = BGP_ENCAP_TYPE_IPSEC_IN_TUNNEL_MODE;
  277. ENC_SUBTLV(BGP_TEA_SUBTLV_IPSEC_TA, subtlv_encode_ipsec_ta, st_ipsec_ta);
  278. }
  279. void
  280. bgp_encap_type_ip_in_ip_tunnel_with_ipsec_transport_mode_to_tlv(
  281. struct bgp_encap_type_ip_in_ip_tunnel_with_ipsec_transport_mode *bet, /* input structure */
  282. struct attr *attr)
  283. {
  284. struct attr_extra *extra = bgp_attr_extra_get(attr);
  285. struct bgp_attr_encap_subtlv *last;
  286. /* advance to last subtlv */
  287. for (last = extra->encap_subtlvs; last && last->next; last = last->next);
  288. extra->encap_tunneltype = BGP_ENCAP_TYPE_IP_IN_IP_TUNNEL_WITH_IPSEC_TRANSPORT_MODE;
  289. ENC_SUBTLV(BGP_TEA_SUBTLV_IPSEC_TA, subtlv_encode_ipsec_ta, st_ipsec_ta);
  290. }
  291. void
  292. bgp_encap_type_mpls_in_ip_tunnel_with_ipsec_transport_mode_to_tlv(
  293. struct bgp_encap_type_mpls_in_ip_tunnel_with_ipsec_transport_mode *bet, /* input structure */
  294. struct attr *attr)
  295. {
  296. struct attr_extra *extra = bgp_attr_extra_get(attr);
  297. struct bgp_attr_encap_subtlv *last;
  298. /* advance to last subtlv */
  299. for (last = extra->encap_subtlvs; last && last->next; last = last->next);
  300. extra->encap_tunneltype = BGP_ENCAP_TYPE_MPLS_IN_IP_TUNNEL_WITH_IPSEC_TRANSPORT_MODE;
  301. ENC_SUBTLV(BGP_TEA_SUBTLV_IPSEC_TA, subtlv_encode_ipsec_ta, st_ipsec_ta);
  302. }
  303. void
  304. bgp_encap_type_pbb_to_tlv(
  305. struct bgp_encap_type_pbb *bet, /* input structure */
  306. struct attr *attr)
  307. {
  308. struct attr_extra *extra = bgp_attr_extra_get(attr);
  309. struct bgp_attr_encap_subtlv *last;
  310. /* advance to last subtlv */
  311. for (last = extra->encap_subtlvs; last && last->next; last = last->next);
  312. extra->encap_tunneltype = BGP_ENCAP_TYPE_PBB;
  313. assert(CHECK_FLAG(bet->valid_subtlvs, BGP_TEA_SUBTLV_ENCAP));
  314. ENC_SUBTLV(BGP_TEA_SUBTLV_ENCAP, subtlv_encode_encap_pbb, st_encap);
  315. }
  316. void
  317. bgp_encap_type_vxlan_to_tlv(
  318. struct bgp_encap_type_vxlan *bet, /* input structure */
  319. struct attr *attr)
  320. {
  321. struct attr_extra *extra = bgp_attr_extra_get(attr);
  322. extra->encap_tunneltype = BGP_ENCAP_TYPE_VXLAN;
  323. }
  324. void
  325. bgp_encap_type_nvgre_to_tlv(
  326. struct bgp_encap_type_nvgre *bet, /* input structure */
  327. struct attr *attr)
  328. {
  329. struct attr_extra *extra = bgp_attr_extra_get(attr);
  330. extra->encap_tunneltype = BGP_ENCAP_TYPE_NVGRE;
  331. }
  332. void
  333. bgp_encap_type_mpls_to_tlv(
  334. struct bgp_encap_type_mpls *bet, /* input structure */
  335. struct attr *attr)
  336. {
  337. struct attr_extra *extra = bgp_attr_extra_get(attr);
  338. extra->encap_tunneltype = BGP_ENCAP_TYPE_MPLS;
  339. }
  340. void
  341. bgp_encap_type_mpls_in_gre_to_tlv(
  342. struct bgp_encap_type_mpls_in_gre *bet, /* input structure */
  343. struct attr *attr)
  344. {
  345. struct attr_extra *extra = bgp_attr_extra_get(attr);
  346. extra->encap_tunneltype = BGP_ENCAP_TYPE_MPLS_IN_GRE;
  347. }
  348. void
  349. bgp_encap_type_vxlan_gpe_to_tlv(
  350. struct bgp_encap_type_vxlan_gpe *bet, /* input structure */
  351. struct attr *attr)
  352. {
  353. struct attr_extra *extra = bgp_attr_extra_get(attr);
  354. extra->encap_tunneltype = BGP_ENCAP_TYPE_VXLAN_GPE;
  355. }
  356. void
  357. bgp_encap_type_mpls_in_udp_to_tlv(
  358. struct bgp_encap_type_mpls_in_udp *bet, /* input structure */
  359. struct attr *attr)
  360. {
  361. struct attr_extra *extra = bgp_attr_extra_get(attr);
  362. extra->encap_tunneltype = BGP_ENCAP_TYPE_MPLS_IN_UDP;
  363. }
  364. /***********************************************************************
  365. * SUBTLV DECODE
  366. ***********************************************************************/
  367. /* rfc5512 4.1 */
  368. static int
  369. subtlv_decode_encap_l2tpv3_over_ip(
  370. struct bgp_attr_encap_subtlv *subtlv,
  371. struct bgp_tea_subtlv_encap_l2tpv3_over_ip *st)
  372. {
  373. if (subtlv->length < 4) {
  374. zlog_debug("%s, subtlv length %d is less than 4",
  375. __func__, subtlv->length);
  376. return -1;
  377. }
  378. st->sessionid = (subtlv->value[0] << 24) |
  379. (subtlv->value[1] << 16) |
  380. (subtlv->value[2] << 8) |
  381. subtlv->value[3];
  382. st->cookie_length = subtlv->length - 4;
  383. if (st->cookie_length > sizeof(st->cookie)) {
  384. zlog_debug("%s, subtlv length %d is greater than %d",
  385. __func__, st->cookie_length, (int)sizeof(st->cookie));
  386. return -1;
  387. }
  388. memcpy(st->cookie, subtlv->value + 4, st->cookie_length);
  389. return 0;
  390. }
  391. /* rfc5512 4.1 */
  392. static int
  393. subtlv_decode_encap_gre(
  394. struct bgp_attr_encap_subtlv *subtlv,
  395. struct bgp_tea_subtlv_encap_gre_key *st)
  396. {
  397. if (subtlv->length != 4) {
  398. zlog_debug("%s, subtlv length %d does not equal 4",
  399. __func__, subtlv->length);
  400. return -1;
  401. }
  402. st->gre_key = (subtlv->value[0] << 24) |
  403. (subtlv->value[1] << 16) |
  404. (subtlv->value[2] << 8) |
  405. subtlv->value[3];
  406. return 0;
  407. }
  408. static int
  409. subtlv_decode_encap_pbb(
  410. struct bgp_attr_encap_subtlv *subtlv,
  411. struct bgp_tea_subtlv_encap_pbb *st)
  412. {
  413. if (subtlv->length != 1 + 3 + 6 + 2) {
  414. zlog_debug("%s, subtlv length %d does not equal %d",
  415. __func__, subtlv->length, 1 + 3 + 6 + 2);
  416. return -1;
  417. }
  418. if (subtlv->value[0] & 0x80) {
  419. st->flag_isid = 1;
  420. st->isid = (subtlv->value[1] << 16) |
  421. (subtlv->value[2] << 8) |
  422. subtlv->value[3];
  423. }
  424. if (subtlv->value[0] & 0x40) {
  425. st->flag_vid = 1;
  426. st->vid = ((subtlv->value[10] & 0x0f) << 8) | subtlv->value[11];
  427. }
  428. memcpy(st->macaddr, subtlv->value + 4, 6);
  429. return 0;
  430. }
  431. /* rfc5512 4.2 */
  432. static int
  433. subtlv_decode_proto_type(
  434. struct bgp_attr_encap_subtlv *subtlv,
  435. struct bgp_tea_subtlv_proto_type *st)
  436. {
  437. if (subtlv->length != 2) {
  438. zlog_debug("%s, subtlv length %d does not equal 2",
  439. __func__, subtlv->length);
  440. return -1;
  441. }
  442. st->proto = (subtlv->value[0] << 8) | subtlv->value[1];
  443. return 0;
  444. }
  445. /* rfc5512 4.3 */
  446. static int
  447. subtlv_decode_color(
  448. struct bgp_attr_encap_subtlv *subtlv,
  449. struct bgp_tea_subtlv_color *st)
  450. {
  451. if (subtlv->length != 8) {
  452. zlog_debug("%s, subtlv length %d does not equal 8",
  453. __func__, subtlv->length);
  454. return -1;
  455. }
  456. if ((subtlv->value[0] != 0x03) ||
  457. (subtlv->value[1] != 0x0b) ||
  458. (subtlv->value[2] != 0) ||
  459. (subtlv->value[3] != 0)) {
  460. zlog_debug("%s, subtlv value 1st 4 bytes are not 0x030b0000", __func__);
  461. return -1;
  462. }
  463. st->color = (subtlv->value[4] << 24) |
  464. (subtlv->value[5] << 16) |
  465. (subtlv->value[6] << 8) |
  466. subtlv->value[7];
  467. return 0;
  468. }
  469. /* rfc 5566 4. */
  470. static int
  471. subtlv_decode_ipsec_ta(
  472. struct bgp_attr_encap_subtlv *subtlv,
  473. struct bgp_tea_subtlv_ipsec_ta *st)
  474. {
  475. st->authenticator_length = subtlv->length - 2;
  476. if (st->authenticator_length > sizeof(st->value)) {
  477. zlog_debug("%s, authenticator length %d exceeds storage maximum %d",
  478. __func__, st->authenticator_length, (int)sizeof(st->value));
  479. return -1;
  480. }
  481. st->authenticator_type = (subtlv->value[0] << 8) | subtlv->value[1];
  482. memcpy(st->value, subtlv->value + 2, st->authenticator_length);
  483. return 0;
  484. }
  485. /* draft-rosen-idr-tunnel-encaps 2.1 */
  486. static int
  487. subtlv_decode_remote_endpoint(
  488. struct bgp_attr_encap_subtlv *subtlv,
  489. struct bgp_tea_subtlv_remote_endpoint *st)
  490. {
  491. int i;
  492. if (subtlv->length != 8 && subtlv->length != 20 ) {
  493. zlog_debug("%s, subtlv length %d does not equal 8 or 20",
  494. __func__, subtlv->length);
  495. return -1;
  496. }
  497. if (subtlv->length == 8) {
  498. st->family = AF_INET;
  499. st->ip_address.v4.s_addr = ((subtlv->value[0] << 24) |
  500. (subtlv->value[1] << 16) |
  501. (subtlv->value[2] << 8) |
  502. subtlv->value[3]);
  503. } else {
  504. st->family = AF_INET6;
  505. memcpy (&(st->ip_address.v6.s6_addr), subtlv->value, 16);
  506. }
  507. i = subtlv->length - 4;
  508. st->as4 = ((subtlv->value[i] << 24) |
  509. (subtlv->value[i+1] << 16) |
  510. (subtlv->value[i+2] << 8) |
  511. subtlv->value[i+3]);
  512. return 0;
  513. }
  514. /***********************************************************************
  515. * TUNNEL TYPE-SPECIFIC TLV DECODE
  516. ***********************************************************************/
  517. int
  518. tlv_to_bgp_encap_type_l2tpv3overip(
  519. struct bgp_attr_encap_subtlv *stlv, /* subtlv chain */
  520. struct bgp_encap_type_l2tpv3_over_ip *bet) /* caller-allocated */
  521. {
  522. struct bgp_attr_encap_subtlv *st;
  523. int rc = 0;
  524. for (st = stlv; st; st = st->next) {
  525. switch (st->type) {
  526. case BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION:
  527. rc |= subtlv_decode_encap_l2tpv3_over_ip(st, &bet->st_encap);
  528. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_ENCAP);
  529. break;
  530. case BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE:
  531. rc |= subtlv_decode_proto_type(st, &bet->st_proto);
  532. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_PROTO_TYPE);
  533. break;
  534. case BGP_ENCAP_SUBTLV_TYPE_COLOR:
  535. rc |= subtlv_decode_color(st, &bet->st_color);
  536. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_COLOR);
  537. break;
  538. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  539. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  540. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  541. break;
  542. default:
  543. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  544. rc |= -1;
  545. break;
  546. }
  547. }
  548. return rc;
  549. }
  550. int
  551. tlv_to_bgp_encap_type_gre(
  552. struct bgp_attr_encap_subtlv *stlv, /* subtlv chain */
  553. struct bgp_encap_type_gre *bet) /* caller-allocated */
  554. {
  555. struct bgp_attr_encap_subtlv *st;
  556. int rc = 0;
  557. for (st = stlv; st; st = st->next) {
  558. switch (st->type) {
  559. case BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION:
  560. rc |= subtlv_decode_encap_gre(st, &bet->st_encap);
  561. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_ENCAP);
  562. break;
  563. case BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE:
  564. rc |= subtlv_decode_proto_type(st, &bet->st_proto);
  565. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_PROTO_TYPE);
  566. break;
  567. case BGP_ENCAP_SUBTLV_TYPE_COLOR:
  568. rc |= subtlv_decode_color(st, &bet->st_color);
  569. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_COLOR);
  570. break;
  571. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  572. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  573. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  574. break;
  575. default:
  576. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  577. rc |= -1;
  578. break;
  579. }
  580. }
  581. return rc;
  582. }
  583. int
  584. tlv_to_bgp_encap_type_ip_in_ip(
  585. struct bgp_attr_encap_subtlv *stlv, /* subtlv chain */
  586. struct bgp_encap_type_ip_in_ip *bet) /* caller-allocated */
  587. {
  588. struct bgp_attr_encap_subtlv *st;
  589. int rc = 0;
  590. for (st = stlv; st; st = st->next) {
  591. switch (st->type) {
  592. case BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE:
  593. rc |= subtlv_decode_proto_type(st, &bet->st_proto);
  594. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_PROTO_TYPE);
  595. break;
  596. case BGP_ENCAP_SUBTLV_TYPE_COLOR:
  597. rc |= subtlv_decode_color(st, &bet->st_color);
  598. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_COLOR);
  599. break;
  600. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  601. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  602. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  603. break;
  604. default:
  605. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  606. rc |= -1;
  607. break;
  608. }
  609. }
  610. return rc;
  611. }
  612. int
  613. tlv_to_bgp_encap_type_transmit_tunnel_endpoint(
  614. struct bgp_attr_encap_subtlv *stlv,
  615. struct bgp_encap_type_transmit_tunnel_endpoint *bet)
  616. {
  617. struct bgp_attr_encap_subtlv *st;
  618. int rc = 0;
  619. for (st = stlv; st; st = st->next) {
  620. switch (st->type) {
  621. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  622. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  623. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  624. break;
  625. default:
  626. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  627. rc |= -1;
  628. break;
  629. }
  630. }
  631. return rc;
  632. }
  633. int
  634. tlv_to_bgp_encap_type_ipsec_in_tunnel_mode(
  635. struct bgp_attr_encap_subtlv *stlv, /* subtlv chain */
  636. struct bgp_encap_type_ipsec_in_tunnel_mode *bet) /* caller-allocated */
  637. {
  638. struct bgp_attr_encap_subtlv *st;
  639. int rc = 0;
  640. for (st = stlv; st; st = st->next) {
  641. switch (st->type) {
  642. case BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA:
  643. rc |= subtlv_decode_ipsec_ta(st, &bet->st_ipsec_ta);
  644. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_IPSEC_TA);
  645. break;
  646. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  647. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  648. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  649. break;
  650. default:
  651. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  652. rc |= -1;
  653. break;
  654. }
  655. }
  656. return rc;
  657. }
  658. int
  659. tlv_to_bgp_encap_type_ip_in_ip_tunnel_with_ipsec_transport_mode(
  660. struct bgp_attr_encap_subtlv *stlv,
  661. struct bgp_encap_type_ip_in_ip_tunnel_with_ipsec_transport_mode *bet)
  662. {
  663. struct bgp_attr_encap_subtlv *st;
  664. int rc = 0;
  665. for (st = stlv; st; st = st->next) {
  666. switch (st->type) {
  667. case BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA:
  668. rc |= subtlv_decode_ipsec_ta(st, &bet->st_ipsec_ta);
  669. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_IPSEC_TA);
  670. break;
  671. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  672. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  673. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  674. break;
  675. default:
  676. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  677. rc |= -1;
  678. break;
  679. }
  680. }
  681. return rc;
  682. }
  683. int
  684. tlv_to_bgp_encap_type_mpls_in_ip_tunnel_with_ipsec_transport_mode(
  685. struct bgp_attr_encap_subtlv *stlv,
  686. struct bgp_encap_type_mpls_in_ip_tunnel_with_ipsec_transport_mode *bet)
  687. {
  688. struct bgp_attr_encap_subtlv *st;
  689. int rc = 0;
  690. for (st = stlv; st; st = st->next) {
  691. switch (st->type) {
  692. case BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA:
  693. rc |= subtlv_decode_ipsec_ta(st, &bet->st_ipsec_ta);
  694. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_IPSEC_TA);
  695. break;
  696. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  697. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  698. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  699. break;
  700. default:
  701. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  702. rc |= -1;
  703. break;
  704. }
  705. }
  706. return rc;
  707. }
  708. int
  709. tlv_to_bgp_encap_type_vxlan(
  710. struct bgp_attr_encap_subtlv *stlv,
  711. struct bgp_encap_type_vxlan *bet)
  712. {
  713. struct bgp_attr_encap_subtlv *st;
  714. int rc = 0;
  715. for (st = stlv; st; st = st->next) {
  716. switch (st->type) {
  717. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  718. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  719. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  720. break;
  721. default:
  722. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  723. rc |= -1;
  724. break;
  725. }
  726. }
  727. return rc;
  728. }
  729. int
  730. tlv_to_bgp_encap_type_nvgre(
  731. struct bgp_attr_encap_subtlv *stlv,
  732. struct bgp_encap_type_nvgre *bet)
  733. {
  734. struct bgp_attr_encap_subtlv *st;
  735. int rc = 0;
  736. for (st = stlv; st; st = st->next) {
  737. switch (st->type) {
  738. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  739. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  740. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  741. break;
  742. default:
  743. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  744. rc |= -1;
  745. break;
  746. }
  747. }
  748. return rc;
  749. }
  750. int
  751. tlv_to_bgp_encap_type_mpls(
  752. struct bgp_attr_encap_subtlv *stlv,
  753. struct bgp_encap_type_mpls *bet)
  754. {
  755. struct bgp_attr_encap_subtlv *st;
  756. int rc = 0;
  757. for (st = stlv; st; st = st->next) {
  758. switch (st->type) {
  759. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  760. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  761. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  762. break;
  763. default:
  764. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  765. rc |= -1;
  766. break;
  767. }
  768. }
  769. return rc;
  770. }
  771. int
  772. tlv_to_bgp_encap_type_mpls_in_gre(
  773. struct bgp_attr_encap_subtlv *stlv,
  774. struct bgp_encap_type_mpls_in_gre *bet)
  775. {
  776. struct bgp_attr_encap_subtlv *st;
  777. int rc = 0;
  778. for (st = stlv; st; st = st->next) {
  779. switch (st->type) {
  780. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  781. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  782. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  783. break;
  784. default:
  785. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  786. rc |= -1;
  787. break;
  788. }
  789. }
  790. return rc;
  791. }
  792. int
  793. tlv_to_bgp_encap_type_vxlan_gpe(
  794. struct bgp_attr_encap_subtlv *stlv,
  795. struct bgp_encap_type_vxlan_gpe *bet)
  796. {
  797. struct bgp_attr_encap_subtlv *st;
  798. int rc = 0;
  799. for (st = stlv; st; st = st->next) {
  800. switch (st->type) {
  801. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  802. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  803. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  804. break;
  805. default:
  806. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  807. rc |= -1;
  808. break;
  809. }
  810. }
  811. return rc;
  812. }
  813. int
  814. tlv_to_bgp_encap_type_mpls_in_udp(
  815. struct bgp_attr_encap_subtlv *stlv,
  816. struct bgp_encap_type_mpls_in_udp *bet)
  817. {
  818. struct bgp_attr_encap_subtlv *st;
  819. int rc = 0;
  820. for (st = stlv; st; st = st->next) {
  821. switch (st->type) {
  822. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  823. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  824. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  825. break;
  826. default:
  827. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  828. rc |= -1;
  829. break;
  830. }
  831. }
  832. return rc;
  833. }
  834. int
  835. tlv_to_bgp_encap_type_pbb(
  836. struct bgp_attr_encap_subtlv *stlv, /* subtlv chain */
  837. struct bgp_encap_type_pbb *bet) /* caller-allocated */
  838. {
  839. struct bgp_attr_encap_subtlv *st;
  840. int rc = 0;
  841. for (st = stlv; st; st = st->next) {
  842. switch (st->type) {
  843. case BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION:
  844. rc |= subtlv_decode_encap_pbb(st, &bet->st_encap);
  845. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_ENCAP);
  846. break;
  847. case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
  848. rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
  849. SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
  850. break;
  851. default:
  852. zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
  853. rc |= -1;
  854. break;
  855. }
  856. }
  857. return rc;
  858. }