bgp_attr.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* BGP attributes.
  2. Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #ifndef _QUAGGA_BGP_ATTR_H
  17. #define _QUAGGA_BGP_ATTR_H
  18. /* Simple bit mapping. */
  19. #define BITMAP_NBBY 8
  20. #define SET_BITMAP(MAP, NUM) \
  21. SET_FLAG (MAP[(NUM) / BITMAP_NBBY], 1 << ((NUM) % BITMAP_NBBY))
  22. #define CHECK_BITMAP(MAP, NUM) \
  23. CHECK_FLAG (MAP[(NUM) / BITMAP_NBBY], 1 << ((NUM) % BITMAP_NBBY))
  24. #define BGP_MED_MAX UINT32_MAX
  25. /* BGP Attribute type range. */
  26. #define BGP_ATTR_TYPE_RANGE 256
  27. #define BGP_ATTR_BITMAP_SIZE (BGP_ATTR_TYPE_RANGE / BITMAP_NBBY)
  28. /* BGP Attribute flags. */
  29. #define BGP_ATTR_FLAG_OPTIONAL 0x80 /* Attribute is optional. */
  30. #define BGP_ATTR_FLAG_TRANS 0x40 /* Attribute is transitive. */
  31. #define BGP_ATTR_FLAG_PARTIAL 0x20 /* Attribute is partial. */
  32. #define BGP_ATTR_FLAG_EXTLEN 0x10 /* Extended length flag. */
  33. /* BGP attribute header must bigger than 2. */
  34. #define BGP_ATTR_MIN_LEN 3 /* Attribute flag, type length. */
  35. #define BGP_ATTR_DEFAULT_WEIGHT 32768
  36. struct bgp_attr_encap_subtlv {
  37. struct bgp_attr_encap_subtlv *next; /* for chaining */
  38. uint16_t type;
  39. uint16_t length;
  40. uint8_t value[1]; /* will be extended */
  41. };
  42. /* Additional/uncommon BGP attributes.
  43. * lazily allocated as and when a struct attr
  44. * requires it.
  45. */
  46. struct attr_extra
  47. {
  48. /* Multi-Protocol Nexthop, AFI IPv6 */
  49. struct in6_addr mp_nexthop_global;
  50. struct in6_addr mp_nexthop_local;
  51. /* Extended Communities attribute. */
  52. struct ecommunity *ecommunity;
  53. /* Large Communities attribute. */
  54. struct lcommunity *lcommunity;
  55. /* Route-Reflector Cluster attribute */
  56. struct cluster_list *cluster;
  57. /* Unknown transitive attribute. */
  58. struct transit *transit;
  59. struct in_addr mp_nexthop_global_in;
  60. /* Aggregator Router ID attribute */
  61. struct in_addr aggregator_addr;
  62. /* Route Reflector Originator attribute */
  63. struct in_addr originator_id;
  64. /* Local weight, not actually an attribute */
  65. u_int32_t weight;
  66. /* Aggregator ASN */
  67. as_t aggregator_as;
  68. /* MP Nexthop length */
  69. u_char mp_nexthop_len;
  70. uint16_t encap_tunneltype; /* grr */
  71. struct bgp_attr_encap_subtlv *encap_subtlvs; /* rfc5512 */
  72. /* route tag */
  73. route_tag_t tag;
  74. };
  75. /* BGP core attribute structure. */
  76. struct attr
  77. {
  78. /* AS Path structure */
  79. struct aspath *aspath;
  80. /* Community structure */
  81. struct community *community;
  82. /* Lazily allocated pointer to extra attributes */
  83. struct attr_extra *extra;
  84. /* Reference count of this attribute. */
  85. unsigned long refcnt;
  86. /* Flag of attribute is set or not. */
  87. u_int32_t flag;
  88. /* Apart from in6_addr, the remaining static attributes */
  89. struct in_addr nexthop;
  90. u_int32_t med;
  91. u_int32_t local_pref;
  92. /* Path origin attribute */
  93. u_char origin;
  94. };
  95. /* Router Reflector related structure. */
  96. struct cluster_list
  97. {
  98. unsigned long refcnt;
  99. int length;
  100. struct in_addr *list;
  101. };
  102. /* Unknown transit attribute. */
  103. struct transit
  104. {
  105. unsigned long refcnt;
  106. int length;
  107. u_char *val;
  108. };
  109. #define ATTR_FLAG_BIT(X) (1 << ((X) - 1))
  110. typedef enum {
  111. BGP_ATTR_PARSE_PROCEED = 0,
  112. BGP_ATTR_PARSE_ERROR = -1,
  113. BGP_ATTR_PARSE_WITHDRAW = -2,
  114. /* only used internally, send notify + convert to BGP_ATTR_PARSE_ERROR */
  115. BGP_ATTR_PARSE_ERROR_NOTIFYPLS = -3,
  116. } bgp_attr_parse_ret_t;
  117. /* Prototypes. */
  118. extern void bgp_attr_init (void);
  119. extern void bgp_attr_finish (void);
  120. extern bgp_attr_parse_ret_t bgp_attr_parse (struct peer *, struct attr *,
  121. bgp_size_t, struct bgp_nlri *,
  122. struct bgp_nlri *);
  123. extern struct attr_extra *bgp_attr_extra_get (struct attr *);
  124. extern void bgp_attr_extra_free (struct attr *);
  125. extern void bgp_attr_dup (struct attr *, struct attr *);
  126. extern struct attr *bgp_attr_intern (struct attr *attr);
  127. extern void bgp_attr_unintern_sub (struct attr *);
  128. extern void bgp_attr_unintern (struct attr **);
  129. extern void bgp_attr_flush (struct attr *);
  130. extern struct attr *bgp_attr_default_set (struct attr *attr, u_char);
  131. extern struct attr *bgp_attr_default_intern (u_char);
  132. extern struct attr *bgp_attr_aggregate_intern (struct bgp *, u_char,
  133. struct aspath *,
  134. struct community *, int as_set, u_char);
  135. extern bgp_size_t bgp_packet_attribute (struct bgp *bgp, struct peer *,
  136. struct stream *, struct attr *,
  137. struct prefix *, afi_t, safi_t,
  138. struct peer *, struct prefix_rd *,
  139. u_char *);
  140. extern void bgp_dump_routes_attr (struct stream *, struct attr *,
  141. struct prefix *);
  142. extern int attrhash_cmp (const void *, const void *);
  143. extern unsigned int attrhash_key_make (void *);
  144. extern void attr_show_all (struct vty *);
  145. extern unsigned long int attr_count (void);
  146. extern unsigned long int attr_unknown_count (void);
  147. /* Cluster list prototypes. */
  148. extern int cluster_loop_check (struct cluster_list *, struct in_addr);
  149. extern void cluster_unintern (struct cluster_list **);
  150. /* Transit attribute prototypes. */
  151. void transit_unintern (struct transit **);
  152. /* Below exported for unit-test purposes only */
  153. struct bgp_attr_parser_args {
  154. struct peer *peer;
  155. bgp_size_t length; /* attribute data length; */
  156. bgp_size_t total; /* total length, inc header */
  157. struct attr *attr;
  158. u_int8_t type;
  159. u_int8_t flags;
  160. u_char *startp;
  161. };
  162. extern int bgp_mp_reach_parse (struct bgp_attr_parser_args *args,
  163. struct bgp_nlri *);
  164. extern int bgp_mp_unreach_parse (struct bgp_attr_parser_args *args,
  165. struct bgp_nlri *);
  166. extern struct bgp_attr_encap_subtlv *
  167. encap_tlv_dup(struct bgp_attr_encap_subtlv *orig);
  168. extern void
  169. bgp_attr_flush_encap(struct attr *attr);
  170. /**
  171. * Set of functions to encode MP_REACH_NLRI and MP_UNREACH_NLRI attributes.
  172. * Typical call sequence is to call _start(), followed by multiple _prefix(),
  173. * one for each NLRI that needs to be encoded into the UPDATE message, and
  174. * finally the _end() function.
  175. */
  176. extern size_t bgp_packet_mpattr_start(struct stream *s, afi_t afi, safi_t safi,
  177. struct attr *attr);
  178. extern void bgp_packet_mpattr_prefix(struct stream *s, afi_t afi, safi_t safi,
  179. struct prefix *p, struct prefix_rd *prd,
  180. u_char *tag);
  181. extern size_t bgp_packet_mpattr_prefix_size(afi_t afi, safi_t safi,
  182. struct prefix *p);
  183. extern void bgp_packet_mpattr_end(struct stream *s, size_t sizep);
  184. extern size_t bgp_packet_mpunreach_start (struct stream *s, afi_t afi,
  185. safi_t safi);
  186. extern void bgp_packet_mpunreach_prefix (struct stream *s, struct prefix *p,
  187. afi_t afi, safi_t safi, struct prefix_rd *prd,
  188. u_char *tag);
  189. extern void bgp_packet_mpunreach_end (struct stream *s, size_t attrlen_pnt);
  190. #endif /* _QUAGGA_BGP_ATTR_H */