bgp_attr.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /* Route-Reflector Cluster attribute */
  54. struct cluster_list *cluster;
  55. /* Unknown transitive attribute. */
  56. struct transit *transit;
  57. struct in_addr mp_nexthop_global_in;
  58. /* Aggregator Router ID attribute */
  59. struct in_addr aggregator_addr;
  60. /* Route Reflector Originator attribute */
  61. struct in_addr originator_id;
  62. /* Local weight, not actually an attribute */
  63. u_int32_t weight;
  64. /* Aggregator ASN */
  65. as_t aggregator_as;
  66. /* MP Nexthop length */
  67. u_char mp_nexthop_len;
  68. uint16_t encap_tunneltype; /* grr */
  69. struct bgp_attr_encap_subtlv *encap_subtlvs; /* rfc5512 */
  70. /* route tag */
  71. u_short tag;
  72. };
  73. /* BGP core attribute structure. */
  74. struct attr
  75. {
  76. /* AS Path structure */
  77. struct aspath *aspath;
  78. /* Community structure */
  79. struct community *community;
  80. /* Lazily allocated pointer to extra attributes */
  81. struct attr_extra *extra;
  82. /* Reference count of this attribute. */
  83. unsigned long refcnt;
  84. /* Flag of attribute is set or not. */
  85. u_int32_t flag;
  86. /* Apart from in6_addr, the remaining static attributes */
  87. struct in_addr nexthop;
  88. u_int32_t med;
  89. u_int32_t local_pref;
  90. /* Path origin attribute */
  91. u_char origin;
  92. };
  93. /* Router Reflector related structure. */
  94. struct cluster_list
  95. {
  96. unsigned long refcnt;
  97. int length;
  98. struct in_addr *list;
  99. };
  100. /* Unknown transit attribute. */
  101. struct transit
  102. {
  103. unsigned long refcnt;
  104. int length;
  105. u_char *val;
  106. };
  107. #define ATTR_FLAG_BIT(X) (1 << ((X) - 1))
  108. typedef enum {
  109. BGP_ATTR_PARSE_PROCEED = 0,
  110. BGP_ATTR_PARSE_ERROR = -1,
  111. BGP_ATTR_PARSE_WITHDRAW = -2,
  112. /* only used internally, send notify + convert to BGP_ATTR_PARSE_ERROR */
  113. BGP_ATTR_PARSE_ERROR_NOTIFYPLS = -3,
  114. } bgp_attr_parse_ret_t;
  115. /* Prototypes. */
  116. extern void bgp_attr_init (void);
  117. extern void bgp_attr_finish (void);
  118. extern bgp_attr_parse_ret_t bgp_attr_parse (struct peer *, struct attr *,
  119. bgp_size_t, struct bgp_nlri *,
  120. struct bgp_nlri *);
  121. extern struct attr_extra *bgp_attr_extra_get (struct attr *);
  122. extern void bgp_attr_extra_free (struct attr *);
  123. extern void bgp_attr_dup (struct attr *, struct attr *);
  124. extern struct attr *bgp_attr_intern (struct attr *attr);
  125. extern void bgp_attr_unintern_sub (struct attr *);
  126. extern void bgp_attr_unintern (struct attr **);
  127. extern void bgp_attr_flush (struct attr *);
  128. extern struct attr *bgp_attr_default_set (struct attr *attr, u_char);
  129. extern struct attr *bgp_attr_default_intern (u_char);
  130. extern struct attr *bgp_attr_aggregate_intern (struct bgp *, u_char,
  131. struct aspath *,
  132. struct community *, int as_set);
  133. extern bgp_size_t bgp_packet_attribute (struct bgp *bgp, struct peer *,
  134. struct stream *, struct attr *,
  135. struct prefix *, afi_t, safi_t,
  136. struct peer *, struct prefix_rd *,
  137. u_char *);
  138. extern void bgp_dump_routes_attr (struct stream *, struct attr *,
  139. struct prefix *);
  140. extern int attrhash_cmp (const void *, const void *);
  141. extern unsigned int attrhash_key_make (void *);
  142. extern void attr_show_all (struct vty *);
  143. extern unsigned long int attr_count (void);
  144. extern unsigned long int attr_unknown_count (void);
  145. /* Cluster list prototypes. */
  146. extern int cluster_loop_check (struct cluster_list *, struct in_addr);
  147. extern void cluster_unintern (struct cluster_list *);
  148. /* Transit attribute prototypes. */
  149. void transit_unintern (struct transit *);
  150. /* Below exported for unit-test purposes only */
  151. struct bgp_attr_parser_args {
  152. struct peer *peer;
  153. bgp_size_t length; /* attribute data length; */
  154. bgp_size_t total; /* total length, inc header */
  155. struct attr *attr;
  156. u_int8_t type;
  157. u_int8_t flags;
  158. u_char *startp;
  159. };
  160. extern int bgp_mp_reach_parse (struct bgp_attr_parser_args *args,
  161. struct bgp_nlri *);
  162. extern int bgp_mp_unreach_parse (struct bgp_attr_parser_args *args,
  163. struct bgp_nlri *);
  164. extern struct bgp_attr_encap_subtlv *
  165. encap_tlv_dup(struct bgp_attr_encap_subtlv *orig);
  166. extern void
  167. bgp_attr_flush_encap(struct attr *attr);
  168. /**
  169. * Set of functions to encode MP_REACH_NLRI and MP_UNREACH_NLRI attributes.
  170. * Typical call sequence is to call _start(), followed by multiple _prefix(),
  171. * one for each NLRI that needs to be encoded into the UPDATE message, and
  172. * finally the _end() function.
  173. */
  174. extern size_t bgp_packet_mpattr_start(struct stream *s, afi_t afi, safi_t safi,
  175. struct attr *attr);
  176. extern void bgp_packet_mpattr_prefix(struct stream *s, afi_t afi, safi_t safi,
  177. struct prefix *p, struct prefix_rd *prd,
  178. u_char *tag);
  179. extern size_t bgp_packet_mpattr_prefix_size(afi_t afi, safi_t safi,
  180. struct prefix *p);
  181. extern void bgp_packet_mpattr_end(struct stream *s, size_t sizep);
  182. extern size_t bgp_packet_mpunreach_start (struct stream *s, afi_t afi,
  183. safi_t safi);
  184. extern void bgp_packet_mpunreach_prefix (struct stream *s, struct prefix *p,
  185. afi_t afi, safi_t safi, struct prefix_rd *prd,
  186. u_char *tag);
  187. extern void bgp_packet_mpunreach_end (struct stream *s, size_t attrlen_pnt);
  188. #endif /* _QUAGGA_BGP_ATTR_H */