paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* BGP attributes management routines. |
| 2 | Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro |
| 3 | |
| 4 | This file is part of GNU Zebra. |
| 5 | |
| 6 | GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 2, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "linklist.h" |
| 24 | #include "prefix.h" |
| 25 | #include "memory.h" |
| 26 | #include "vector.h" |
| 27 | #include "vty.h" |
| 28 | #include "stream.h" |
| 29 | #include "log.h" |
| 30 | #include "hash.h" |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 31 | #include "jhash.h" |
Donald Sharp | 0490729 | 2016-01-07 10:03:01 -0500 | [diff] [blame] | 32 | #include "filter.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | |
| 34 | #include "bgpd/bgpd.h" |
| 35 | #include "bgpd/bgp_attr.h" |
| 36 | #include "bgpd/bgp_route.h" |
| 37 | #include "bgpd/bgp_aspath.h" |
| 38 | #include "bgpd/bgp_community.h" |
| 39 | #include "bgpd/bgp_debug.h" |
| 40 | #include "bgpd/bgp_packet.h" |
| 41 | #include "bgpd/bgp_ecommunity.h" |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 42 | #include "table.h" |
| 43 | #include "bgp_encap_types.h" |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 44 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 45 | /* Attribute strings for logging. */ |
Stephen Hemminger | 9bddac4 | 2009-05-15 09:59:51 -0700 | [diff] [blame] | 46 | static const struct message attr_str [] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | { |
| 48 | { BGP_ATTR_ORIGIN, "ORIGIN" }, |
| 49 | { BGP_ATTR_AS_PATH, "AS_PATH" }, |
| 50 | { BGP_ATTR_NEXT_HOP, "NEXT_HOP" }, |
| 51 | { BGP_ATTR_MULTI_EXIT_DISC, "MULTI_EXIT_DISC" }, |
| 52 | { BGP_ATTR_LOCAL_PREF, "LOCAL_PREF" }, |
| 53 | { BGP_ATTR_ATOMIC_AGGREGATE, "ATOMIC_AGGREGATE" }, |
| 54 | { BGP_ATTR_AGGREGATOR, "AGGREGATOR" }, |
| 55 | { BGP_ATTR_COMMUNITIES, "COMMUNITY" }, |
| 56 | { BGP_ATTR_ORIGINATOR_ID, "ORIGINATOR_ID" }, |
Denis Ovsienko | f8627ff | 2011-10-10 16:52:20 +0400 | [diff] [blame] | 57 | { BGP_ATTR_CLUSTER_LIST, "CLUSTER_LIST" }, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 58 | { BGP_ATTR_DPA, "DPA" }, |
| 59 | { BGP_ATTR_ADVERTISER, "ADVERTISER"} , |
| 60 | { BGP_ATTR_RCID_PATH, "RCID_PATH" }, |
| 61 | { BGP_ATTR_MP_REACH_NLRI, "MP_REACH_NLRI" }, |
| 62 | { BGP_ATTR_MP_UNREACH_NLRI, "MP_UNREACH_NLRI" }, |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 63 | { BGP_ATTR_EXT_COMMUNITIES, "EXT_COMMUNITIES" }, |
| 64 | { BGP_ATTR_AS4_PATH, "AS4_PATH" }, |
| 65 | { BGP_ATTR_AS4_AGGREGATOR, "AS4_AGGREGATOR" }, |
| 66 | { BGP_ATTR_AS_PATHLIMIT, "AS_PATHLIMIT" }, |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 67 | { BGP_ATTR_ENCAP, "ENCAP" }, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | }; |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 69 | static const int attr_str_max = array_size(attr_str); |
Denis Ovsienko | afcb767 | 2011-10-23 22:32:44 +0400 | [diff] [blame] | 70 | |
| 71 | static const struct message attr_flag_str[] = |
| 72 | { |
| 73 | { BGP_ATTR_FLAG_OPTIONAL, "Optional" }, |
| 74 | { BGP_ATTR_FLAG_TRANS, "Transitive" }, |
| 75 | { BGP_ATTR_FLAG_PARTIAL, "Partial" }, |
| 76 | /* bgp_attr_flags_diagnose() relies on this bit being last in this list */ |
| 77 | { BGP_ATTR_FLAG_EXTLEN, "Extended Length" }, |
| 78 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 79 | |
Stephen Hemminger | 9bddac4 | 2009-05-15 09:59:51 -0700 | [diff] [blame] | 80 | static struct hash *cluster_hash; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 81 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 82 | static void * |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 83 | cluster_hash_alloc (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | { |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 85 | struct cluster_list * val = (struct cluster_list *) p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 86 | struct cluster_list *cluster; |
| 87 | |
| 88 | cluster = XMALLOC (MTYPE_CLUSTER, sizeof (struct cluster_list)); |
| 89 | cluster->length = val->length; |
| 90 | |
| 91 | if (cluster->length) |
| 92 | { |
| 93 | cluster->list = XMALLOC (MTYPE_CLUSTER_VAL, val->length); |
| 94 | memcpy (cluster->list, val->list, val->length); |
| 95 | } |
| 96 | else |
| 97 | cluster->list = NULL; |
| 98 | |
| 99 | cluster->refcnt = 0; |
| 100 | |
| 101 | return cluster; |
| 102 | } |
| 103 | |
| 104 | /* Cluster list related functions. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 105 | static struct cluster_list * |
paul | 5228ad2 | 2004-06-04 17:58:18 +0000 | [diff] [blame] | 106 | cluster_parse (struct in_addr * pnt, int length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 107 | { |
| 108 | struct cluster_list tmp; |
| 109 | struct cluster_list *cluster; |
| 110 | |
| 111 | tmp.length = length; |
paul | 5228ad2 | 2004-06-04 17:58:18 +0000 | [diff] [blame] | 112 | tmp.list = pnt; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 113 | |
| 114 | cluster = hash_get (cluster_hash, &tmp, cluster_hash_alloc); |
| 115 | cluster->refcnt++; |
| 116 | return cluster; |
| 117 | } |
| 118 | |
| 119 | int |
| 120 | cluster_loop_check (struct cluster_list *cluster, struct in_addr originator) |
| 121 | { |
| 122 | int i; |
| 123 | |
| 124 | for (i = 0; i < cluster->length / 4; i++) |
| 125 | if (cluster->list[i].s_addr == originator.s_addr) |
| 126 | return 1; |
| 127 | return 0; |
| 128 | } |
| 129 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 130 | static unsigned int |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 131 | cluster_hash_key_make (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 132 | { |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 133 | const struct cluster_list *cluster = p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 134 | |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 135 | return jhash(cluster->list, cluster->length, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | } |
| 137 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 138 | static int |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 139 | cluster_hash_cmp (const void *p1, const void *p2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 140 | { |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 141 | const struct cluster_list * cluster1 = p1; |
| 142 | const struct cluster_list * cluster2 = p2; |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 143 | |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 144 | return (cluster1->length == cluster2->length && |
| 145 | memcmp (cluster1->list, cluster2->list, cluster1->length) == 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 146 | } |
| 147 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 148 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 149 | cluster_free (struct cluster_list *cluster) |
| 150 | { |
| 151 | if (cluster->list) |
| 152 | XFREE (MTYPE_CLUSTER_VAL, cluster->list); |
| 153 | XFREE (MTYPE_CLUSTER, cluster); |
| 154 | } |
| 155 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 156 | #if 0 |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 157 | static struct cluster_list * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | cluster_dup (struct cluster_list *cluster) |
| 159 | { |
| 160 | struct cluster_list *new; |
| 161 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 162 | new = XCALLOC (MTYPE_CLUSTER, sizeof (struct cluster_list)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | new->length = cluster->length; |
| 164 | |
| 165 | if (cluster->length) |
| 166 | { |
| 167 | new->list = XMALLOC (MTYPE_CLUSTER_VAL, cluster->length); |
| 168 | memcpy (new->list, cluster->list, cluster->length); |
| 169 | } |
| 170 | else |
| 171 | new->list = NULL; |
| 172 | |
| 173 | return new; |
| 174 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 175 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 176 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 177 | static struct cluster_list * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 178 | cluster_intern (struct cluster_list *cluster) |
| 179 | { |
| 180 | struct cluster_list *find; |
| 181 | |
| 182 | find = hash_get (cluster_hash, cluster, cluster_hash_alloc); |
| 183 | find->refcnt++; |
| 184 | |
| 185 | return find; |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | cluster_unintern (struct cluster_list *cluster) |
| 190 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 191 | if (cluster->refcnt) |
| 192 | cluster->refcnt--; |
| 193 | |
| 194 | if (cluster->refcnt == 0) |
| 195 | { |
Stephen Hemminger | 9206f9e | 2011-12-18 19:43:40 +0400 | [diff] [blame] | 196 | hash_release (cluster_hash, cluster); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 197 | cluster_free (cluster); |
| 198 | } |
| 199 | } |
| 200 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 201 | static void |
| 202 | cluster_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 203 | { |
| 204 | cluster_hash = hash_create (cluster_hash_key_make, cluster_hash_cmp); |
| 205 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 206 | |
| 207 | static void |
| 208 | cluster_finish (void) |
| 209 | { |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 210 | hash_clean (cluster_hash, (void (*)(void *))cluster_free); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 211 | hash_free (cluster_hash); |
| 212 | cluster_hash = NULL; |
| 213 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 214 | |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 215 | struct bgp_attr_encap_subtlv * |
| 216 | encap_tlv_dup(struct bgp_attr_encap_subtlv *orig) |
| 217 | { |
| 218 | struct bgp_attr_encap_subtlv *new; |
| 219 | struct bgp_attr_encap_subtlv *tail; |
| 220 | struct bgp_attr_encap_subtlv *p; |
| 221 | |
| 222 | for (p = orig, tail = new = NULL; p; p = p->next) { |
| 223 | int size = sizeof(struct bgp_attr_encap_subtlv) - 1 + p->length; |
| 224 | if (tail) { |
| 225 | tail->next = XCALLOC(MTYPE_ENCAP_TLV, size); |
| 226 | tail = tail->next; |
| 227 | } else { |
| 228 | tail = new = XCALLOC(MTYPE_ENCAP_TLV, size); |
| 229 | } |
| 230 | assert(tail); |
| 231 | memcpy(tail, p, size); |
| 232 | tail->next = NULL; |
| 233 | } |
| 234 | |
| 235 | return new; |
| 236 | } |
| 237 | |
| 238 | static void |
| 239 | encap_free(struct bgp_attr_encap_subtlv *p) |
| 240 | { |
| 241 | struct bgp_attr_encap_subtlv *next; |
| 242 | while (p) { |
| 243 | next = p->next; |
| 244 | p->next = NULL; |
| 245 | XFREE(MTYPE_ENCAP_TLV, p); |
| 246 | p = next; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void |
| 251 | bgp_attr_flush_encap(struct attr *attr) |
| 252 | { |
| 253 | if (!attr || !attr->extra) |
| 254 | return; |
| 255 | |
| 256 | if (attr->extra->encap_subtlvs) { |
| 257 | encap_free(attr->extra->encap_subtlvs); |
| 258 | attr->extra->encap_subtlvs = NULL; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Compare encap sub-tlv chains |
| 264 | * |
| 265 | * 1 = equivalent |
| 266 | * 0 = not equivalent |
| 267 | * |
| 268 | * This algorithm could be made faster if needed |
| 269 | */ |
| 270 | static int |
| 271 | encap_same(struct bgp_attr_encap_subtlv *h1, struct bgp_attr_encap_subtlv *h2) |
| 272 | { |
| 273 | struct bgp_attr_encap_subtlv *p; |
| 274 | struct bgp_attr_encap_subtlv *q; |
| 275 | |
| 276 | if (!h1 && !h2) |
| 277 | return 1; |
| 278 | if (h1 && !h2) |
| 279 | return 0; |
| 280 | if (!h1 && h2) |
| 281 | return 0; |
| 282 | if (h1 == h2) |
| 283 | return 1; |
| 284 | |
| 285 | for (p = h1; p; p = p->next) { |
| 286 | for (q = h2; q; q = q->next) { |
| 287 | if ((p->type == q->type) && |
| 288 | (p->length == q->length) && |
| 289 | !memcmp(p->value, q->value, p->length)) { |
| 290 | |
| 291 | break; |
| 292 | } |
| 293 | } |
| 294 | if (!q) |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | for (p = h2; p; p = p->next) { |
| 299 | for (q = h1; q; q = q->next) { |
| 300 | if ((p->type == q->type) && |
| 301 | (p->length == q->length) && |
| 302 | !memcmp(p->value, q->value, p->length)) { |
| 303 | |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | if (!q) |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | return 1; |
| 312 | } |
| 313 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 314 | /* Unknown transit attribute. */ |
Stephen Hemminger | 9bddac4 | 2009-05-15 09:59:51 -0700 | [diff] [blame] | 315 | static struct hash *transit_hash; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 316 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 317 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 318 | transit_free (struct transit *transit) |
| 319 | { |
| 320 | if (transit->val) |
| 321 | XFREE (MTYPE_TRANSIT_VAL, transit->val); |
| 322 | XFREE (MTYPE_TRANSIT, transit); |
| 323 | } |
| 324 | |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 325 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 326 | static void * |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 327 | transit_hash_alloc (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 328 | { |
| 329 | /* Transit structure is already allocated. */ |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 330 | return p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 331 | } |
| 332 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 333 | static struct transit * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 334 | transit_intern (struct transit *transit) |
| 335 | { |
| 336 | struct transit *find; |
| 337 | |
| 338 | find = hash_get (transit_hash, transit, transit_hash_alloc); |
| 339 | if (find != transit) |
| 340 | transit_free (transit); |
| 341 | find->refcnt++; |
| 342 | |
| 343 | return find; |
| 344 | } |
| 345 | |
| 346 | void |
| 347 | transit_unintern (struct transit *transit) |
| 348 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 349 | if (transit->refcnt) |
| 350 | transit->refcnt--; |
| 351 | |
| 352 | if (transit->refcnt == 0) |
| 353 | { |
Stephen Hemminger | 9206f9e | 2011-12-18 19:43:40 +0400 | [diff] [blame] | 354 | hash_release (transit_hash, transit); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 355 | transit_free (transit); |
| 356 | } |
| 357 | } |
| 358 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 359 | static unsigned int |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 360 | transit_hash_key_make (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 361 | { |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 362 | const struct transit * transit = p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 363 | |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 364 | return jhash(transit->val, transit->length, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 365 | } |
| 366 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 367 | static int |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 368 | transit_hash_cmp (const void *p1, const void *p2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 369 | { |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 370 | const struct transit * transit1 = p1; |
| 371 | const struct transit * transit2 = p2; |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 372 | |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 373 | return (transit1->length == transit2->length && |
| 374 | memcmp (transit1->val, transit2->val, transit1->length) == 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 375 | } |
| 376 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 377 | static void |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 378 | transit_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 379 | { |
| 380 | transit_hash = hash_create (transit_hash_key_make, transit_hash_cmp); |
| 381 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 382 | |
| 383 | static void |
| 384 | transit_finish (void) |
| 385 | { |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 386 | hash_clean (transit_hash, (void (*)(void *))transit_free); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 387 | hash_free (transit_hash); |
| 388 | transit_hash = NULL; |
| 389 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 390 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 391 | /* Attribute hash routines. */ |
Stephen Hemminger | 9bddac4 | 2009-05-15 09:59:51 -0700 | [diff] [blame] | 392 | static struct hash *attrhash; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 393 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 394 | static struct attr_extra * |
| 395 | bgp_attr_extra_new (void) |
| 396 | { |
| 397 | return XCALLOC (MTYPE_ATTR_EXTRA, sizeof (struct attr_extra)); |
| 398 | } |
| 399 | |
| 400 | void |
| 401 | bgp_attr_extra_free (struct attr *attr) |
| 402 | { |
| 403 | if (attr->extra) |
| 404 | { |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 405 | if (attr->extra->encap_subtlvs) { |
| 406 | encap_free(attr->extra->encap_subtlvs); |
| 407 | attr->extra->encap_subtlvs = NULL; |
| 408 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 409 | XFREE (MTYPE_ATTR_EXTRA, attr->extra); |
| 410 | attr->extra = NULL; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | struct attr_extra * |
| 415 | bgp_attr_extra_get (struct attr *attr) |
| 416 | { |
| 417 | if (!attr->extra) |
| 418 | attr->extra = bgp_attr_extra_new(); |
| 419 | return attr->extra; |
| 420 | } |
| 421 | |
| 422 | /* Shallow copy of an attribute |
| 423 | * Though, not so shallow that it doesn't copy the contents |
| 424 | * of the attr_extra pointed to by 'extra' |
| 425 | */ |
| 426 | void |
| 427 | bgp_attr_dup (struct attr *new, struct attr *orig) |
| 428 | { |
Jorge Boncompte [DTI2] | 558d1fe | 2012-05-07 16:53:05 +0000 | [diff] [blame] | 429 | struct attr_extra *extra = new->extra; |
| 430 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 431 | *new = *orig; |
Christian Franke | a0de1d1 | 2012-12-07 16:35:00 +0000 | [diff] [blame] | 432 | /* if caller provided attr_extra space, use it in any case. |
| 433 | * |
| 434 | * This is neccesary even if orig->extra equals NULL, because otherwise |
| 435 | * memory may be later allocated on the heap by bgp_attr_extra_get. |
| 436 | * |
| 437 | * That memory would eventually be leaked, because the caller must not |
| 438 | * call bgp_attr_extra_free if he provided attr_extra on the stack. |
| 439 | */ |
| 440 | if (extra) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 441 | { |
Christian Franke | a0de1d1 | 2012-12-07 16:35:00 +0000 | [diff] [blame] | 442 | new->extra = extra; |
| 443 | memset(new->extra, 0, sizeof(struct attr_extra)); |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 444 | if (orig->extra) { |
Christian Franke | a0de1d1 | 2012-12-07 16:35:00 +0000 | [diff] [blame] | 445 | *new->extra = *orig->extra; |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 446 | if (orig->extra->encap_subtlvs) { |
| 447 | new->extra->encap_subtlvs = encap_tlv_dup(orig->extra->encap_subtlvs); |
| 448 | } |
| 449 | } |
Christian Franke | a0de1d1 | 2012-12-07 16:35:00 +0000 | [diff] [blame] | 450 | } |
| 451 | else if (orig->extra) |
| 452 | { |
| 453 | new->extra = bgp_attr_extra_new(); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 454 | *new->extra = *orig->extra; |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 455 | if (orig->extra->encap_subtlvs) { |
| 456 | new->extra->encap_subtlvs = encap_tlv_dup(orig->extra->encap_subtlvs); |
| 457 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Paul Jakma | cbdfbaa | 2006-03-30 13:20:48 +0000 | [diff] [blame] | 461 | unsigned long int |
| 462 | attr_count (void) |
| 463 | { |
| 464 | return attrhash->count; |
| 465 | } |
| 466 | |
| 467 | unsigned long int |
| 468 | attr_unknown_count (void) |
| 469 | { |
| 470 | return transit_hash->count; |
| 471 | } |
| 472 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 473 | unsigned int |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 474 | attrhash_key_make (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 475 | { |
Jorge Boncompte [DTI2] | 7fb0cd8 | 2012-05-07 16:52:58 +0000 | [diff] [blame] | 476 | const struct attr *attr = (struct attr *) p; |
| 477 | const struct attr_extra *extra = attr->extra; |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 478 | uint32_t key = 0; |
| 479 | #define MIX(val) key = jhash_1word(val, key) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 480 | |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 481 | MIX(attr->origin); |
| 482 | MIX(attr->nexthop.s_addr); |
| 483 | MIX(attr->med); |
| 484 | MIX(attr->local_pref); |
| 485 | |
Paul Jakma | c8f3fe3 | 2010-12-05 20:28:02 +0000 | [diff] [blame] | 486 | key += attr->origin; |
| 487 | key += attr->nexthop.s_addr; |
| 488 | key += attr->med; |
| 489 | key += attr->local_pref; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 490 | |
Jorge Boncompte [DTI2] | 7fb0cd8 | 2012-05-07 16:52:58 +0000 | [diff] [blame] | 491 | if (extra) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 492 | { |
Jorge Boncompte [DTI2] | 7fb0cd8 | 2012-05-07 16:52:58 +0000 | [diff] [blame] | 493 | MIX(extra->aggregator_as); |
| 494 | MIX(extra->aggregator_addr.s_addr); |
| 495 | MIX(extra->weight); |
| 496 | MIX(extra->mp_nexthop_global_in.s_addr); |
Pradosh Mohapatra | 689bb66 | 2013-09-07 07:13:37 +0000 | [diff] [blame] | 497 | MIX(extra->originator_id.s_addr); |
Piotr Chytła | 605aa33 | 2015-12-01 10:03:54 -0500 | [diff] [blame] | 498 | MIX(extra->tag); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 499 | } |
| 500 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 501 | if (attr->aspath) |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 502 | MIX(aspath_key_make (attr->aspath)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 503 | if (attr->community) |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 504 | MIX(community_hash_make (attr->community)); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 505 | |
Jorge Boncompte [DTI2] | 7fb0cd8 | 2012-05-07 16:52:58 +0000 | [diff] [blame] | 506 | if (extra) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 507 | { |
Jorge Boncompte [DTI2] | 7fb0cd8 | 2012-05-07 16:52:58 +0000 | [diff] [blame] | 508 | if (extra->ecommunity) |
| 509 | MIX(ecommunity_hash_make (extra->ecommunity)); |
| 510 | if (extra->cluster) |
| 511 | MIX(cluster_hash_key_make (extra->cluster)); |
| 512 | if (extra->transit) |
| 513 | MIX(transit_hash_key_make (extra->transit)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 514 | |
Jorge Boncompte [DTI2] | 7fb0cd8 | 2012-05-07 16:52:58 +0000 | [diff] [blame] | 515 | MIX(extra->mp_nexthop_len); |
| 516 | key = jhash(extra->mp_nexthop_global.s6_addr, 16, key); |
| 517 | key = jhash(extra->mp_nexthop_local.s6_addr, 16, key); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 518 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 519 | |
| 520 | return key; |
| 521 | } |
| 522 | |
| 523 | int |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 524 | attrhash_cmp (const void *p1, const void *p2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 525 | { |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 526 | const struct attr * attr1 = p1; |
| 527 | const struct attr * attr2 = p2; |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 528 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 529 | if (attr1->flag == attr2->flag |
| 530 | && attr1->origin == attr2->origin |
| 531 | && attr1->nexthop.s_addr == attr2->nexthop.s_addr |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 532 | && attr1->aspath == attr2->aspath |
| 533 | && attr1->community == attr2->community |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 534 | && attr1->med == attr2->med |
Paul Jakma | c8f3fe3 | 2010-12-05 20:28:02 +0000 | [diff] [blame] | 535 | && attr1->local_pref == attr2->local_pref) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 536 | { |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 537 | const struct attr_extra *ae1 = attr1->extra; |
| 538 | const struct attr_extra *ae2 = attr2->extra; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 539 | |
| 540 | if (ae1 && ae2 |
| 541 | && ae1->aggregator_as == ae2->aggregator_as |
| 542 | && ae1->aggregator_addr.s_addr == ae2->aggregator_addr.s_addr |
| 543 | && ae1->weight == ae2->weight |
Piotr Chytła | 605aa33 | 2015-12-01 10:03:54 -0500 | [diff] [blame] | 544 | && ae1->tag == ae2->tag |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 545 | && ae1->mp_nexthop_len == ae2->mp_nexthop_len |
| 546 | && IPV6_ADDR_SAME (&ae1->mp_nexthop_global, &ae2->mp_nexthop_global) |
| 547 | && IPV6_ADDR_SAME (&ae1->mp_nexthop_local, &ae2->mp_nexthop_local) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 548 | && IPV4_ADDR_SAME (&ae1->mp_nexthop_global_in, &ae2->mp_nexthop_global_in) |
| 549 | && ae1->ecommunity == ae2->ecommunity |
| 550 | && ae1->cluster == ae2->cluster |
Pradosh Mohapatra | 689bb66 | 2013-09-07 07:13:37 +0000 | [diff] [blame] | 551 | && ae1->transit == ae2->transit |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 552 | && (ae1->encap_tunneltype == ae2->encap_tunneltype) |
| 553 | && encap_same(ae1->encap_subtlvs, ae2->encap_subtlvs) |
Pradosh Mohapatra | 689bb66 | 2013-09-07 07:13:37 +0000 | [diff] [blame] | 554 | && IPV4_ADDR_SAME (&ae1->originator_id, &ae2->originator_id)) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 555 | return 1; |
| 556 | else if (ae1 || ae2) |
| 557 | return 0; |
| 558 | /* neither attribute has extra attributes, so they're same */ |
| 559 | return 1; |
| 560 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 561 | else |
| 562 | return 0; |
| 563 | } |
| 564 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 565 | static void |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 566 | attrhash_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 567 | { |
| 568 | attrhash = hash_create (attrhash_key_make, attrhash_cmp); |
| 569 | } |
| 570 | |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 571 | /* |
| 572 | * special for hash_clean below |
| 573 | */ |
| 574 | static void |
| 575 | attr_vfree (void *attr) |
| 576 | { |
| 577 | bgp_attr_extra_free ((struct attr *)attr); |
| 578 | XFREE (MTYPE_ATTR, attr); |
| 579 | } |
| 580 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 581 | static void |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 582 | attrhash_finish (void) |
| 583 | { |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 584 | hash_clean(attrhash, attr_vfree); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 585 | hash_free (attrhash); |
| 586 | attrhash = NULL; |
| 587 | } |
| 588 | |
| 589 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 590 | attr_show_all_iterator (struct hash_backet *backet, struct vty *vty) |
| 591 | { |
| 592 | struct attr *attr = backet->data; |
| 593 | |
| 594 | vty_out (vty, "attr[%ld] nexthop %s%s", attr->refcnt, |
| 595 | inet_ntoa (attr->nexthop), VTY_NEWLINE); |
| 596 | } |
| 597 | |
| 598 | void |
| 599 | attr_show_all (struct vty *vty) |
| 600 | { |
| 601 | hash_iterate (attrhash, |
| 602 | (void (*)(struct hash_backet *, void *)) |
| 603 | attr_show_all_iterator, |
| 604 | vty); |
| 605 | } |
| 606 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 607 | static void * |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 608 | bgp_attr_hash_alloc (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 609 | { |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 610 | struct attr * val = (struct attr *) p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 611 | struct attr *attr; |
| 612 | |
| 613 | attr = XMALLOC (MTYPE_ATTR, sizeof (struct attr)); |
| 614 | *attr = *val; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 615 | if (val->extra) |
| 616 | { |
| 617 | attr->extra = bgp_attr_extra_new (); |
| 618 | *attr->extra = *val->extra; |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 619 | |
| 620 | if (attr->extra->encap_subtlvs) { |
| 621 | attr->extra->encap_subtlvs = encap_tlv_dup(attr->extra->encap_subtlvs); |
| 622 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 623 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 624 | attr->refcnt = 0; |
| 625 | return attr; |
| 626 | } |
| 627 | |
| 628 | /* Internet argument attribute. */ |
| 629 | struct attr * |
| 630 | bgp_attr_intern (struct attr *attr) |
| 631 | { |
| 632 | struct attr *find; |
| 633 | |
| 634 | /* Intern referenced strucutre. */ |
| 635 | if (attr->aspath) |
| 636 | { |
| 637 | if (! attr->aspath->refcnt) |
| 638 | attr->aspath = aspath_intern (attr->aspath); |
| 639 | else |
| 640 | attr->aspath->refcnt++; |
| 641 | } |
| 642 | if (attr->community) |
| 643 | { |
| 644 | if (! attr->community->refcnt) |
| 645 | attr->community = community_intern (attr->community); |
| 646 | else |
| 647 | attr->community->refcnt++; |
| 648 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 649 | if (attr->extra) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 650 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 651 | struct attr_extra *attre = attr->extra; |
| 652 | |
| 653 | if (attre->ecommunity) |
| 654 | { |
| 655 | if (! attre->ecommunity->refcnt) |
| 656 | attre->ecommunity = ecommunity_intern (attre->ecommunity); |
| 657 | else |
| 658 | attre->ecommunity->refcnt++; |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 659 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 660 | } |
| 661 | if (attre->cluster) |
| 662 | { |
| 663 | if (! attre->cluster->refcnt) |
| 664 | attre->cluster = cluster_intern (attre->cluster); |
| 665 | else |
| 666 | attre->cluster->refcnt++; |
| 667 | } |
| 668 | if (attre->transit) |
| 669 | { |
| 670 | if (! attre->transit->refcnt) |
| 671 | attre->transit = transit_intern (attre->transit); |
| 672 | else |
| 673 | attre->transit->refcnt++; |
| 674 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 675 | } |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 676 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 677 | find = (struct attr *) hash_get (attrhash, attr, bgp_attr_hash_alloc); |
| 678 | find->refcnt++; |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 679 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 680 | return find; |
| 681 | } |
| 682 | |
Paul Jakma | 03e214c | 2007-04-29 18:31:07 +0000 | [diff] [blame] | 683 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 684 | /* Make network statement's attribute. */ |
| 685 | struct attr * |
| 686 | bgp_attr_default_set (struct attr *attr, u_char origin) |
| 687 | { |
| 688 | memset (attr, 0, sizeof (struct attr)); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 689 | bgp_attr_extra_get (attr); |
| 690 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 691 | attr->origin = origin; |
| 692 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN); |
| 693 | attr->aspath = aspath_empty (); |
| 694 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 695 | attr->extra->weight = BGP_ATTR_DEFAULT_WEIGHT; |
Piotr Chytła | 605aa33 | 2015-12-01 10:03:54 -0500 | [diff] [blame] | 696 | attr->extra->tag = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 697 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 698 | attr->extra->mp_nexthop_len = IPV6_MAX_BYTELEN; |
Paul Jakma | 03e214c | 2007-04-29 18:31:07 +0000 | [diff] [blame] | 699 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 700 | return attr; |
| 701 | } |
| 702 | |
Paul Jakma | 03e214c | 2007-04-29 18:31:07 +0000 | [diff] [blame] | 703 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 704 | /* Make network statement's attribute. */ |
| 705 | struct attr * |
| 706 | bgp_attr_default_intern (u_char origin) |
| 707 | { |
| 708 | struct attr attr; |
| 709 | struct attr *new; |
Jorge Boncompte [DTI2] | e16a413 | 2012-05-07 16:52:57 +0000 | [diff] [blame] | 710 | |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 711 | memset (&attr, 0, sizeof (struct attr)); |
| 712 | bgp_attr_extra_get (&attr); |
| 713 | |
Paul Jakma | 03e214c | 2007-04-29 18:31:07 +0000 | [diff] [blame] | 714 | bgp_attr_default_set(&attr, origin); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 715 | |
| 716 | new = bgp_attr_intern (&attr); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 717 | bgp_attr_extra_free (&attr); |
| 718 | |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 719 | aspath_unintern (&new->aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 720 | return new; |
| 721 | } |
| 722 | |
Daniel Walton | 76a7280 | 2015-05-19 17:47:24 -0700 | [diff] [blame] | 723 | /* Create the attributes for an aggregate */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 724 | struct attr * |
| 725 | bgp_attr_aggregate_intern (struct bgp *bgp, u_char origin, |
| 726 | struct aspath *aspath, |
Daniel Walton | f2eb9ca | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 727 | struct community *community, int as_set, |
| 728 | u_char atomic_aggregate) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 729 | { |
| 730 | struct attr attr; |
| 731 | struct attr *new; |
Jorge Boncompte [DTI2] | 938ef3a | 2012-05-07 16:52:59 +0000 | [diff] [blame] | 732 | struct attr_extra attre; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 733 | |
| 734 | memset (&attr, 0, sizeof (struct attr)); |
Jorge Boncompte [DTI2] | 938ef3a | 2012-05-07 16:52:59 +0000 | [diff] [blame] | 735 | memset (&attre, 0, sizeof (struct attr_extra)); |
| 736 | attr.extra = &attre; |
| 737 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 738 | /* Origin attribute. */ |
| 739 | attr.origin = origin; |
| 740 | attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN); |
| 741 | |
| 742 | /* AS path attribute. */ |
| 743 | if (aspath) |
| 744 | attr.aspath = aspath_intern (aspath); |
| 745 | else |
| 746 | attr.aspath = aspath_empty (); |
| 747 | attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH); |
| 748 | |
| 749 | /* Next hop attribute. */ |
| 750 | attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP); |
| 751 | |
| 752 | if (community) |
| 753 | { |
| 754 | attr.community = community; |
| 755 | attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES); |
| 756 | } |
| 757 | |
Jorge Boncompte [DTI2] | 938ef3a | 2012-05-07 16:52:59 +0000 | [diff] [blame] | 758 | attre.weight = BGP_ATTR_DEFAULT_WEIGHT; |
Jorge Boncompte [DTI2] | 938ef3a | 2012-05-07 16:52:59 +0000 | [diff] [blame] | 759 | attre.mp_nexthop_len = IPV6_MAX_BYTELEN; |
Lou Berger | 205e674 | 2016-01-12 13:42:11 -0500 | [diff] [blame] | 760 | |
Daniel Walton | f2eb9ca | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 761 | if (! as_set || atomic_aggregate) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 762 | attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE); |
| 763 | attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR); |
| 764 | if (CHECK_FLAG (bgp->config, BGP_CONFIG_CONFEDERATION)) |
Jorge Boncompte [DTI2] | 938ef3a | 2012-05-07 16:52:59 +0000 | [diff] [blame] | 765 | attre.aggregator_as = bgp->confed_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 766 | else |
Jorge Boncompte [DTI2] | 938ef3a | 2012-05-07 16:52:59 +0000 | [diff] [blame] | 767 | attre.aggregator_as = bgp->as; |
| 768 | attre.aggregator_addr = bgp->router_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 769 | |
| 770 | new = bgp_attr_intern (&attr); |
Jorge Boncompte [DTI2] | 938ef3a | 2012-05-07 16:52:59 +0000 | [diff] [blame] | 771 | |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 772 | aspath_unintern (&new->aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 773 | return new; |
| 774 | } |
| 775 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 776 | /* Unintern just the sub-components of the attr, but not the attr */ |
| 777 | void |
| 778 | bgp_attr_unintern_sub (struct attr *attr) |
| 779 | { |
| 780 | /* aspath refcount shoud be decrement. */ |
| 781 | if (attr->aspath) |
| 782 | aspath_unintern (&attr->aspath); |
Jorge Boncompte [DTI2] | d92a2f3 | 2013-07-31 16:36:08 +0000 | [diff] [blame] | 783 | UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH)); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 784 | |
| 785 | if (attr->community) |
| 786 | community_unintern (&attr->community); |
Jorge Boncompte [DTI2] | d92a2f3 | 2013-07-31 16:36:08 +0000 | [diff] [blame] | 787 | UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES)); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 788 | |
| 789 | if (attr->extra) |
| 790 | { |
| 791 | if (attr->extra->ecommunity) |
| 792 | ecommunity_unintern (&attr->extra->ecommunity); |
Jorge Boncompte [DTI2] | d92a2f3 | 2013-07-31 16:36:08 +0000 | [diff] [blame] | 793 | UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES)); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 794 | |
| 795 | if (attr->extra->cluster) |
| 796 | cluster_unintern (attr->extra->cluster); |
Jorge Boncompte [DTI2] | d92a2f3 | 2013-07-31 16:36:08 +0000 | [diff] [blame] | 797 | UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST)); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 798 | |
| 799 | if (attr->extra->transit) |
| 800 | transit_unintern (attr->extra->transit); |
| 801 | } |
| 802 | } |
| 803 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 804 | /* Free bgp attribute and aspath. */ |
| 805 | void |
Jorge Boncompte [DTI2] | 1a2fd70 | 2012-05-07 16:53:00 +0000 | [diff] [blame] | 806 | bgp_attr_unintern (struct attr **pattr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 807 | { |
Jorge Boncompte [DTI2] | 1a2fd70 | 2012-05-07 16:53:00 +0000 | [diff] [blame] | 808 | struct attr *attr = *pattr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 809 | struct attr *ret; |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 810 | struct attr tmp; |
Jorge Boncompte [DTI2] | b9f1dca | 2012-05-07 16:53:01 +0000 | [diff] [blame] | 811 | struct attr_extra tmp_extra; |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 812 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 813 | /* Decrement attribute reference. */ |
Jorge Boncompte [DTI2] | 1a2fd70 | 2012-05-07 16:53:00 +0000 | [diff] [blame] | 814 | attr->refcnt--; |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 815 | |
Jorge Boncompte [DTI2] | 1a2fd70 | 2012-05-07 16:53:00 +0000 | [diff] [blame] | 816 | tmp = *attr; |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 817 | |
Jorge Boncompte [DTI2] | 1a2fd70 | 2012-05-07 16:53:00 +0000 | [diff] [blame] | 818 | if (attr->extra) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 819 | { |
Jorge Boncompte [DTI2] | b9f1dca | 2012-05-07 16:53:01 +0000 | [diff] [blame] | 820 | tmp.extra = &tmp_extra; |
Jorge Boncompte [DTI2] | 1a2fd70 | 2012-05-07 16:53:00 +0000 | [diff] [blame] | 821 | memcpy (tmp.extra, attr->extra, sizeof (struct attr_extra)); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 822 | } |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 823 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 824 | /* If reference becomes zero then free attribute object. */ |
Jorge Boncompte [DTI2] | 1a2fd70 | 2012-05-07 16:53:00 +0000 | [diff] [blame] | 825 | if (attr->refcnt == 0) |
| 826 | { |
| 827 | ret = hash_release (attrhash, attr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 828 | assert (ret != NULL); |
Jorge Boncompte [DTI2] | 1a2fd70 | 2012-05-07 16:53:00 +0000 | [diff] [blame] | 829 | bgp_attr_extra_free (attr); |
| 830 | XFREE (MTYPE_ATTR, attr); |
| 831 | *pattr = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 834 | bgp_attr_unintern_sub (&tmp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | void |
| 838 | bgp_attr_flush (struct attr *attr) |
| 839 | { |
| 840 | if (attr->aspath && ! attr->aspath->refcnt) |
Lou Berger | 370b7e5 | 2016-02-04 21:29:49 -0500 | [diff] [blame] | 841 | { |
| 842 | aspath_free (attr->aspath); |
| 843 | attr->aspath = NULL; |
| 844 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 845 | if (attr->community && ! attr->community->refcnt) |
Lou Berger | 370b7e5 | 2016-02-04 21:29:49 -0500 | [diff] [blame] | 846 | { |
| 847 | community_free (attr->community); |
| 848 | attr->community = NULL; |
| 849 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 850 | if (attr->extra) |
| 851 | { |
| 852 | struct attr_extra *attre = attr->extra; |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 853 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 854 | if (attre->ecommunity && ! attre->ecommunity->refcnt) |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 855 | ecommunity_free (&attre->ecommunity); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 856 | if (attre->cluster && ! attre->cluster->refcnt) |
Lou Berger | 370b7e5 | 2016-02-04 21:29:49 -0500 | [diff] [blame] | 857 | { |
| 858 | cluster_free (attre->cluster); |
| 859 | attre->cluster = NULL; |
| 860 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 861 | if (attre->transit && ! attre->transit->refcnt) |
Lou Berger | 370b7e5 | 2016-02-04 21:29:49 -0500 | [diff] [blame] | 862 | { |
| 863 | transit_free (attre->transit); |
| 864 | attre->transit = NULL; |
| 865 | } |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 866 | encap_free(attre->encap_subtlvs); |
| 867 | attre->encap_subtlvs = NULL; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 868 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 871 | /* Implement draft-scudder-idr-optional-transitive behaviour and |
| 872 | * avoid resetting sessions for malformed attributes which are |
| 873 | * are partial/optional and hence where the error likely was not |
| 874 | * introduced by the sending neighbour. |
| 875 | */ |
| 876 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 877 | bgp_attr_malformed (struct bgp_attr_parser_args *args, u_char subcode, |
| 878 | bgp_size_t length) |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 879 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 880 | struct peer *const peer = args->peer; |
| 881 | const u_int8_t flags = args->flags; |
| 882 | /* startp and length must be special-cased, as whether or not to |
| 883 | * send the attribute data with the NOTIFY depends on the error, |
| 884 | * the caller therefore signals this with the seperate length argument |
| 885 | */ |
Paul Jakma | bd471fe | 2012-03-15 11:30:00 +0000 | [diff] [blame] | 886 | u_char *notify_datap = (length > 0 ? args->startp : NULL); |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 887 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 888 | /* Only relax error handling for eBGP peers */ |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 889 | if (peer->sort != BGP_PEER_EBGP) |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 890 | { |
| 891 | bgp_notify_send_with_data (peer, BGP_NOTIFY_UPDATE_ERR, subcode, |
Paul Jakma | bd471fe | 2012-03-15 11:30:00 +0000 | [diff] [blame] | 892 | notify_datap, length); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 893 | return BGP_ATTR_PARSE_ERROR; |
| 894 | |
| 895 | } |
| 896 | |
Paul Jakma | bd471fe | 2012-03-15 11:30:00 +0000 | [diff] [blame] | 897 | /* Adjust the stream getp to the end of the attribute, in case we can |
| 898 | * still proceed but the caller hasn't read all the attribute. |
| 899 | */ |
| 900 | stream_set_getp (BGP_INPUT (peer), |
| 901 | (args->startp - STREAM_DATA (BGP_INPUT (peer))) |
| 902 | + args->total); |
| 903 | |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 904 | switch (args->type) { |
Paul Jakma | fa61e16 | 2012-03-25 21:31:47 +0100 | [diff] [blame] | 905 | /* where an attribute is relatively inconsequential, e.g. it does not |
| 906 | * affect route selection, and can be safely ignored, then any such |
| 907 | * attributes which are malformed should just be ignored and the route |
| 908 | * processed as normal. |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 909 | */ |
| 910 | case BGP_ATTR_AS4_AGGREGATOR: |
| 911 | case BGP_ATTR_AGGREGATOR: |
| 912 | case BGP_ATTR_ATOMIC_AGGREGATE: |
| 913 | return BGP_ATTR_PARSE_PROCEED; |
| 914 | |
| 915 | /* Core attributes, particularly ones which may influence route |
Paul Jakma | fa61e16 | 2012-03-25 21:31:47 +0100 | [diff] [blame] | 916 | * selection, should always cause session resets |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 917 | */ |
| 918 | case BGP_ATTR_ORIGIN: |
| 919 | case BGP_ATTR_AS_PATH: |
| 920 | case BGP_ATTR_NEXT_HOP: |
| 921 | case BGP_ATTR_MULTI_EXIT_DISC: |
| 922 | case BGP_ATTR_LOCAL_PREF: |
| 923 | case BGP_ATTR_COMMUNITIES: |
| 924 | case BGP_ATTR_ORIGINATOR_ID: |
| 925 | case BGP_ATTR_CLUSTER_LIST: |
| 926 | case BGP_ATTR_MP_REACH_NLRI: |
| 927 | case BGP_ATTR_MP_UNREACH_NLRI: |
| 928 | case BGP_ATTR_EXT_COMMUNITIES: |
| 929 | bgp_notify_send_with_data (peer, BGP_NOTIFY_UPDATE_ERR, subcode, |
Paul Jakma | bd471fe | 2012-03-15 11:30:00 +0000 | [diff] [blame] | 930 | notify_datap, length); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 931 | return BGP_ATTR_PARSE_ERROR; |
| 932 | } |
| 933 | |
| 934 | /* Partial optional attributes that are malformed should not cause |
| 935 | * the whole session to be reset. Instead treat it as a withdrawal |
| 936 | * of the routes, if possible. |
| 937 | */ |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 938 | if (CHECK_FLAG (flags, BGP_ATTR_FLAG_TRANS) |
| 939 | && CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL) |
| 940 | && CHECK_FLAG (flags, BGP_ATTR_FLAG_PARTIAL)) |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 941 | return BGP_ATTR_PARSE_WITHDRAW; |
| 942 | |
| 943 | /* default to reset */ |
David Lamparter | f57000c | 2014-06-04 01:01:10 +0200 | [diff] [blame] | 944 | return BGP_ATTR_PARSE_ERROR_NOTIFYPLS; |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Denis Ovsienko | afcb767 | 2011-10-23 22:32:44 +0400 | [diff] [blame] | 947 | /* Find out what is wrong with the path attribute flag bits and log the error. |
| 948 | "Flag bits" here stand for Optional, Transitive and Partial, but not for |
| 949 | Extended Length. Checking O/T/P bits at once implies, that the attribute |
| 950 | being diagnosed is defined by RFC as either a "well-known" or an "optional, |
| 951 | non-transitive" attribute. */ |
| 952 | static void |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 953 | bgp_attr_flags_diagnose (struct bgp_attr_parser_args *args, |
| 954 | u_int8_t desired_flags /* how RFC says it must be */ |
Denis Ovsienko | afcb767 | 2011-10-23 22:32:44 +0400 | [diff] [blame] | 955 | ) |
| 956 | { |
| 957 | u_char seen = 0, i; |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 958 | u_char real_flags = args->flags; |
| 959 | const u_int8_t attr_code = args->type; |
| 960 | |
Denis Ovsienko | afcb767 | 2011-10-23 22:32:44 +0400 | [diff] [blame] | 961 | desired_flags &= ~BGP_ATTR_FLAG_EXTLEN; |
| 962 | real_flags &= ~BGP_ATTR_FLAG_EXTLEN; |
| 963 | for (i = 0; i <= 2; i++) /* O,T,P, but not E */ |
| 964 | if |
| 965 | ( |
| 966 | CHECK_FLAG (desired_flags, attr_flag_str[i].key) != |
| 967 | CHECK_FLAG (real_flags, attr_flag_str[i].key) |
| 968 | ) |
| 969 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 970 | zlog (args->peer->log, LOG_ERR, "%s attribute must%s be flagged as \"%s\"", |
Denis Ovsienko | afcb767 | 2011-10-23 22:32:44 +0400 | [diff] [blame] | 971 | LOOKUP (attr_str, attr_code), |
| 972 | CHECK_FLAG (desired_flags, attr_flag_str[i].key) ? "" : " not", |
| 973 | attr_flag_str[i].str); |
| 974 | seen = 1; |
| 975 | } |
Paul Jakma | fa5831e | 2012-03-27 11:54:04 +0100 | [diff] [blame] | 976 | if (!seen) |
| 977 | { |
| 978 | zlog (args->peer->log, LOG_DEBUG, |
| 979 | "Strange, %s called for attr %s, but no problem found with flags" |
| 980 | " (real flags 0x%x, desired 0x%x)", |
| 981 | __func__, LOOKUP (attr_str, attr_code), |
| 982 | real_flags, desired_flags); |
| 983 | } |
Denis Ovsienko | afcb767 | 2011-10-23 22:32:44 +0400 | [diff] [blame] | 984 | } |
| 985 | |
Paul Jakma | 3ecab4c | 2012-01-17 13:31:33 +0000 | [diff] [blame] | 986 | /* Required flags for attributes. EXTLEN will be masked off when testing, |
| 987 | * as will PARTIAL for optional+transitive attributes. |
| 988 | */ |
| 989 | const u_int8_t attr_flags_values [] = { |
| 990 | [BGP_ATTR_ORIGIN] = BGP_ATTR_FLAG_TRANS, |
| 991 | [BGP_ATTR_AS_PATH] = BGP_ATTR_FLAG_TRANS, |
| 992 | [BGP_ATTR_NEXT_HOP] = BGP_ATTR_FLAG_TRANS, |
| 993 | [BGP_ATTR_MULTI_EXIT_DISC] = BGP_ATTR_FLAG_OPTIONAL, |
| 994 | [BGP_ATTR_LOCAL_PREF] = BGP_ATTR_FLAG_TRANS, |
| 995 | [BGP_ATTR_ATOMIC_AGGREGATE] = BGP_ATTR_FLAG_TRANS, |
| 996 | [BGP_ATTR_AGGREGATOR] = BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL, |
| 997 | [BGP_ATTR_COMMUNITIES] = BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL, |
| 998 | [BGP_ATTR_ORIGINATOR_ID] = BGP_ATTR_FLAG_OPTIONAL, |
| 999 | [BGP_ATTR_CLUSTER_LIST] = BGP_ATTR_FLAG_OPTIONAL, |
| 1000 | [BGP_ATTR_MP_REACH_NLRI] = BGP_ATTR_FLAG_OPTIONAL, |
| 1001 | [BGP_ATTR_MP_UNREACH_NLRI] = BGP_ATTR_FLAG_OPTIONAL, |
| 1002 | [BGP_ATTR_EXT_COMMUNITIES] = BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS, |
| 1003 | [BGP_ATTR_AS4_PATH] = BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS, |
| 1004 | [BGP_ATTR_AS4_AGGREGATOR] = BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS, |
| 1005 | }; |
| 1006 | static const size_t attr_flags_values_max = |
| 1007 | sizeof (attr_flags_values) / sizeof (attr_flags_values[0]); |
| 1008 | |
| 1009 | static int |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1010 | bgp_attr_flag_invalid (struct bgp_attr_parser_args *args) |
Paul Jakma | 3ecab4c | 2012-01-17 13:31:33 +0000 | [diff] [blame] | 1011 | { |
| 1012 | u_int8_t mask = BGP_ATTR_FLAG_EXTLEN; |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1013 | const u_int8_t flags = args->flags; |
| 1014 | const u_int8_t attr_code = args->type; |
| 1015 | struct peer *const peer = args->peer; |
Paul Jakma | 3ecab4c | 2012-01-17 13:31:33 +0000 | [diff] [blame] | 1016 | |
| 1017 | /* there may be attributes we don't know about */ |
| 1018 | if (attr_code > attr_flags_values_max) |
| 1019 | return 0; |
| 1020 | if (attr_flags_values[attr_code] == 0) |
| 1021 | return 0; |
| 1022 | |
| 1023 | /* RFC4271, "For well-known attributes, the Transitive bit MUST be set to |
| 1024 | * 1." |
| 1025 | */ |
| 1026 | if (!CHECK_FLAG (BGP_ATTR_FLAG_OPTIONAL, flags) |
| 1027 | && !CHECK_FLAG (BGP_ATTR_FLAG_TRANS, flags)) |
| 1028 | { |
| 1029 | zlog (peer->log, LOG_ERR, |
| 1030 | "%s well-known attributes must have transitive flag set (%x)", |
| 1031 | LOOKUP (attr_str, attr_code), flags); |
| 1032 | return 1; |
| 1033 | } |
| 1034 | |
| 1035 | /* "For well-known attributes and for optional non-transitive attributes, |
| 1036 | * the Partial bit MUST be set to 0." |
| 1037 | */ |
| 1038 | if (CHECK_FLAG (flags, BGP_ATTR_FLAG_PARTIAL)) |
| 1039 | { |
| 1040 | if (!CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL)) |
| 1041 | { |
| 1042 | zlog (peer->log, LOG_ERR, |
| 1043 | "%s well-known attribute " |
| 1044 | "must NOT have the partial flag set (%x)", |
| 1045 | LOOKUP (attr_str, attr_code), flags); |
| 1046 | return 1; |
| 1047 | } |
| 1048 | if (CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL) |
| 1049 | && !CHECK_FLAG (flags, BGP_ATTR_FLAG_TRANS)) |
| 1050 | { |
| 1051 | zlog (peer->log, LOG_ERR, |
| 1052 | "%s optional + transitive attribute " |
| 1053 | "must NOT have the partial flag set (%x)", |
| 1054 | LOOKUP (attr_str, attr_code), flags); |
| 1055 | return 1; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | /* Optional transitive attributes may go through speakers that don't |
| 1060 | * reocgnise them and set the Partial bit. |
| 1061 | */ |
| 1062 | if (CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL) |
| 1063 | && CHECK_FLAG (flags, BGP_ATTR_FLAG_TRANS)) |
| 1064 | SET_FLAG (mask, BGP_ATTR_FLAG_PARTIAL); |
| 1065 | |
Paul Jakma | 683f2b8 | 2012-03-23 14:58:45 +0000 | [diff] [blame] | 1066 | if ((flags & ~mask) |
Paul Jakma | 3ecab4c | 2012-01-17 13:31:33 +0000 | [diff] [blame] | 1067 | == attr_flags_values[attr_code]) |
| 1068 | return 0; |
| 1069 | |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1070 | bgp_attr_flags_diagnose (args, attr_flags_values[attr_code]); |
Paul Jakma | 3ecab4c | 2012-01-17 13:31:33 +0000 | [diff] [blame] | 1071 | return 1; |
| 1072 | } |
| 1073 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1074 | /* Get origin attribute of the update message. */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1075 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1076 | bgp_attr_origin (struct bgp_attr_parser_args *args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1077 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1078 | struct peer *const peer = args->peer; |
| 1079 | struct attr *const attr = args->attr; |
| 1080 | const bgp_size_t length = args->length; |
| 1081 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1082 | /* If any recognized attribute has Attribute Length that conflicts |
| 1083 | with the expected length (based on the attribute type code), then |
| 1084 | the Error Subcode is set to Attribute Length Error. The Data |
| 1085 | field contains the erroneous attribute (type, length and |
| 1086 | value). */ |
| 1087 | if (length != 1) |
| 1088 | { |
| 1089 | zlog (peer->log, LOG_ERR, "Origin attribute length is not one %d", |
| 1090 | length); |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1091 | return bgp_attr_malformed (args, |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1092 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1093 | args->total); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | /* Fetch origin attribute. */ |
| 1097 | attr->origin = stream_getc (BGP_INPUT (peer)); |
| 1098 | |
| 1099 | /* If the ORIGIN attribute has an undefined value, then the Error |
| 1100 | Subcode is set to Invalid Origin Attribute. The Data field |
| 1101 | contains the unrecognized attribute (type, length and value). */ |
| 1102 | if ((attr->origin != BGP_ORIGIN_IGP) |
| 1103 | && (attr->origin != BGP_ORIGIN_EGP) |
| 1104 | && (attr->origin != BGP_ORIGIN_INCOMPLETE)) |
| 1105 | { |
| 1106 | zlog (peer->log, LOG_ERR, "Origin attribute value is invalid %d", |
| 1107 | attr->origin); |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1108 | return bgp_attr_malformed (args, |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1109 | BGP_NOTIFY_UPDATE_INVAL_ORIGIN, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1110 | args->total); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | /* Set oring attribute flag. */ |
| 1114 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN); |
| 1115 | |
| 1116 | return 0; |
| 1117 | } |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 1118 | |
| 1119 | /* Parse AS path information. This function is wrapper of |
| 1120 | aspath_parse. */ |
| 1121 | static int |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1122 | bgp_attr_aspath (struct bgp_attr_parser_args *args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1123 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1124 | struct attr *const attr = args->attr; |
| 1125 | struct peer *const peer = args->peer; |
| 1126 | const bgp_size_t length = args->length; |
Paul Jakma | 83a9a22 | 2012-01-08 14:15:03 +0000 | [diff] [blame] | 1127 | |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 1128 | /* |
| 1129 | * peer with AS4 => will get 4Byte ASnums |
| 1130 | * otherwise, will get 16 Bit |
| 1131 | */ |
| 1132 | attr->aspath = aspath_parse (peer->ibuf, length, |
| 1133 | CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)); |
| 1134 | |
| 1135 | /* In case of IBGP, length will be zero. */ |
| 1136 | if (! attr->aspath) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1137 | { |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1138 | zlog (peer->log, LOG_ERR, |
| 1139 | "Malformed AS path from %s, length is %d", |
| 1140 | peer->host, length); |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1141 | return bgp_attr_malformed (args, BGP_NOTIFY_UPDATE_MAL_AS_PATH, 0); |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 1142 | } |
Chris Hall | cddb811 | 2010-08-09 22:31:37 +0400 | [diff] [blame] | 1143 | |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 1144 | /* Set aspath attribute flag. */ |
| 1145 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1146 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1147 | return BGP_ATTR_PARSE_PROCEED; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1150 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1151 | bgp_attr_aspath_check (struct peer *const peer, struct attr *const attr) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1152 | { |
| 1153 | /* These checks were part of bgp_attr_aspath, but with |
| 1154 | * as4 we should to check aspath things when |
| 1155 | * aspath synthesizing with as4_path has already taken place. |
| 1156 | * Otherwise we check ASPATH and use the synthesized thing, and that is |
| 1157 | * not right. |
| 1158 | * So do the checks later, i.e. here |
| 1159 | */ |
| 1160 | struct bgp *bgp = peer->bgp; |
| 1161 | struct aspath *aspath; |
| 1162 | |
Vasilis Tsiligiannis | ca87e1d | 2009-07-20 01:28:35 +0300 | [diff] [blame] | 1163 | /* Confederation sanity check. */ |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 1164 | if ((peer->sort == BGP_PEER_CONFED && ! aspath_left_confed_check (attr->aspath)) || |
| 1165 | (peer->sort == BGP_PEER_EBGP && aspath_confed_check (attr->aspath))) |
Vasilis Tsiligiannis | ca87e1d | 2009-07-20 01:28:35 +0300 | [diff] [blame] | 1166 | { |
| 1167 | zlog (peer->log, LOG_ERR, "Malformed AS path from %s", peer->host); |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1168 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, |
| 1169 | BGP_NOTIFY_UPDATE_MAL_AS_PATH); |
| 1170 | return BGP_ATTR_PARSE_ERROR; |
Vasilis Tsiligiannis | ca87e1d | 2009-07-20 01:28:35 +0300 | [diff] [blame] | 1171 | } |
| 1172 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1173 | /* First AS check for EBGP. */ |
| 1174 | if (bgp != NULL && bgp_flag_check (bgp, BGP_FLAG_ENFORCE_FIRST_AS)) |
| 1175 | { |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 1176 | if (peer->sort == BGP_PEER_EBGP |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1177 | && ! aspath_firstas_check (attr->aspath, peer->as)) |
| 1178 | { |
| 1179 | zlog (peer->log, LOG_ERR, |
Denis Ovsienko | aea339f | 2009-04-30 17:16:22 +0400 | [diff] [blame] | 1180 | "%s incorrect first AS (must be %u)", peer->host, peer->as); |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1181 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, |
| 1182 | BGP_NOTIFY_UPDATE_MAL_AS_PATH); |
| 1183 | return BGP_ATTR_PARSE_ERROR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | /* local-as prepend */ |
| 1188 | if (peer->change_local_as && |
| 1189 | ! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND)) |
| 1190 | { |
| 1191 | aspath = aspath_dup (attr->aspath); |
| 1192 | aspath = aspath_add_seq (aspath, peer->change_local_as); |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 1193 | aspath_unintern (&attr->aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1194 | attr->aspath = aspath_intern (aspath); |
| 1195 | } |
| 1196 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1197 | return BGP_ATTR_PARSE_PROCEED; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1198 | } |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1199 | |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 1200 | /* Parse AS4 path information. This function is another wrapper of |
| 1201 | aspath_parse. */ |
| 1202 | static int |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1203 | bgp_attr_as4_path (struct bgp_attr_parser_args *args, struct aspath **as4_path) |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 1204 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1205 | struct peer *const peer = args->peer; |
| 1206 | struct attr *const attr = args->attr; |
| 1207 | const bgp_size_t length = args->length; |
Paul Jakma | 3ecab4c | 2012-01-17 13:31:33 +0000 | [diff] [blame] | 1208 | |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 1209 | *as4_path = aspath_parse (peer->ibuf, length, 1); |
| 1210 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1211 | /* In case of IBGP, length will be zero. */ |
| 1212 | if (!*as4_path) |
| 1213 | { |
| 1214 | zlog (peer->log, LOG_ERR, |
| 1215 | "Malformed AS4 path from %s, length is %d", |
| 1216 | peer->host, length); |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1217 | return bgp_attr_malformed (args, |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1218 | BGP_NOTIFY_UPDATE_MAL_AS_PATH, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1219 | 0); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 1222 | /* Set aspath attribute flag. */ |
| 1223 | if (as4_path) |
| 1224 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_PATH); |
| 1225 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1226 | return BGP_ATTR_PARSE_PROCEED; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1229 | /* Nexthop attribute. */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1230 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1231 | bgp_attr_nexthop (struct bgp_attr_parser_args *args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1232 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1233 | struct peer *const peer = args->peer; |
| 1234 | struct attr *const attr = args->attr; |
| 1235 | const bgp_size_t length = args->length; |
| 1236 | |
Denis Ovsienko | bc3443e | 2011-09-22 12:48:14 +0400 | [diff] [blame] | 1237 | in_addr_t nexthop_h, nexthop_n; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1238 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1239 | /* Check nexthop attribute length. */ |
| 1240 | if (length != 4) |
| 1241 | { |
| 1242 | zlog (peer->log, LOG_ERR, "Nexthop attribute length isn't four [%d]", |
| 1243 | length); |
| 1244 | |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1245 | return bgp_attr_malformed (args, |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1246 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1247 | args->total); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Denis Ovsienko | bc3443e | 2011-09-22 12:48:14 +0400 | [diff] [blame] | 1250 | /* According to section 6.3 of RFC4271, syntactically incorrect NEXT_HOP |
| 1251 | attribute must result in a NOTIFICATION message (this is implemented below). |
| 1252 | At the same time, semantically incorrect NEXT_HOP is more likely to be just |
| 1253 | logged locally (this is implemented somewhere else). The UPDATE message |
| 1254 | gets ignored in any of these cases. */ |
| 1255 | nexthop_n = stream_get_ipv4 (peer->ibuf); |
| 1256 | nexthop_h = ntohl (nexthop_n); |
Lou Berger | f53585d | 2016-05-17 07:10:36 -0400 | [diff] [blame] | 1257 | if ((IPV4_NET0 (nexthop_h) || IPV4_NET127 (nexthop_h) || IPV4_CLASS_DE (nexthop_h)) |
| 1258 | && !BGP_DEBUG (allow_martians, ALLOW_MARTIANS)) /* loopbacks may be used in testing */ |
Denis Ovsienko | bc3443e | 2011-09-22 12:48:14 +0400 | [diff] [blame] | 1259 | { |
| 1260 | char buf[INET_ADDRSTRLEN]; |
David Lamparter | bb02b82 | 2014-06-04 01:01:00 +0200 | [diff] [blame] | 1261 | inet_ntop (AF_INET, &nexthop_n, buf, INET_ADDRSTRLEN); |
Denis Ovsienko | bc3443e | 2011-09-22 12:48:14 +0400 | [diff] [blame] | 1262 | zlog (peer->log, LOG_ERR, "Martian nexthop %s", buf); |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1263 | return bgp_attr_malformed (args, |
Denis Ovsienko | bc3443e | 2011-09-22 12:48:14 +0400 | [diff] [blame] | 1264 | BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1265 | args->total); |
Denis Ovsienko | bc3443e | 2011-09-22 12:48:14 +0400 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | attr->nexthop.s_addr = nexthop_n; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1269 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP); |
| 1270 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1271 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | /* MED atrribute. */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1275 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1276 | bgp_attr_med (struct bgp_attr_parser_args *args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1277 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1278 | struct peer *const peer = args->peer; |
| 1279 | struct attr *const attr = args->attr; |
| 1280 | const bgp_size_t length = args->length; |
| 1281 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1282 | /* Length check. */ |
| 1283 | if (length != 4) |
| 1284 | { |
| 1285 | zlog (peer->log, LOG_ERR, |
| 1286 | "MED attribute length isn't four [%d]", length); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1287 | |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1288 | return bgp_attr_malformed (args, |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1289 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1290 | args->total); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | attr->med = stream_getl (peer->ibuf); |
| 1294 | |
| 1295 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC); |
| 1296 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1297 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | /* Local preference attribute. */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1301 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1302 | bgp_attr_local_pref (struct bgp_attr_parser_args *args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1303 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1304 | struct peer *const peer = args->peer; |
| 1305 | struct attr *const attr = args->attr; |
| 1306 | const bgp_size_t length = args->length; |
Paul Jakma | 3ecab4c | 2012-01-17 13:31:33 +0000 | [diff] [blame] | 1307 | |
Denis Ovsienko | a624cae | 2011-10-08 13:54:48 +0400 | [diff] [blame] | 1308 | /* Length check. */ |
| 1309 | if (length != 4) |
| 1310 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1311 | zlog (peer->log, LOG_ERR, "LOCAL_PREF attribute length isn't 4 [%u]", |
| 1312 | length); |
| 1313 | return bgp_attr_malformed (args, |
Denis Ovsienko | a624cae | 2011-10-08 13:54:48 +0400 | [diff] [blame] | 1314 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1315 | args->total); |
Denis Ovsienko | a624cae | 2011-10-08 13:54:48 +0400 | [diff] [blame] | 1316 | } |
Denis Ovsienko | 0ea968d | 2011-09-19 16:30:47 +0400 | [diff] [blame] | 1317 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1318 | /* If it is contained in an UPDATE message that is received from an |
| 1319 | external peer, then this attribute MUST be ignored by the |
| 1320 | receiving speaker. */ |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 1321 | if (peer->sort == BGP_PEER_EBGP) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1322 | { |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1323 | stream_forward_getp (peer->ibuf, length); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1324 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Denis Ovsienko | a624cae | 2011-10-08 13:54:48 +0400 | [diff] [blame] | 1327 | attr->local_pref = stream_getl (peer->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1328 | |
| 1329 | /* Set atomic aggregate flag. */ |
| 1330 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF); |
| 1331 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1332 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /* Atomic aggregate. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1336 | static int |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1337 | bgp_attr_atomic (struct bgp_attr_parser_args *args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1338 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1339 | struct peer *const peer = args->peer; |
| 1340 | struct attr *const attr = args->attr; |
| 1341 | const bgp_size_t length = args->length; |
Paul Jakma | 3ecab4c | 2012-01-17 13:31:33 +0000 | [diff] [blame] | 1342 | |
Denis Ovsienko | 9eba2ad | 2011-09-20 14:43:50 +0400 | [diff] [blame] | 1343 | /* Length check. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1344 | if (length != 0) |
| 1345 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1346 | zlog (peer->log, LOG_ERR, "ATOMIC_AGGREGATE attribute length isn't 0 [%u]", |
| 1347 | length); |
| 1348 | return bgp_attr_malformed (args, |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1349 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1350 | args->total); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | /* Set atomic aggregate flag. */ |
| 1354 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE); |
| 1355 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1356 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | /* Aggregator attribute */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1360 | static int |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1361 | bgp_attr_aggregator (struct bgp_attr_parser_args *args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1362 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1363 | struct peer *const peer = args->peer; |
| 1364 | struct attr *const attr = args->attr; |
| 1365 | const bgp_size_t length = args->length; |
| 1366 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1367 | int wantedlen = 6; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1368 | struct attr_extra *attre = bgp_attr_extra_get (attr); |
Paul Jakma | 3ecab4c | 2012-01-17 13:31:33 +0000 | [diff] [blame] | 1369 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1370 | /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1371 | if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1372 | wantedlen = 8; |
| 1373 | |
| 1374 | if (length != wantedlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1375 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1376 | zlog (peer->log, LOG_ERR, "AGGREGATOR attribute length isn't %u [%u]", |
| 1377 | wantedlen, length); |
| 1378 | return bgp_attr_malformed (args, |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1379 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1380 | args->total); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1381 | } |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1382 | |
| 1383 | if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) ) |
| 1384 | attre->aggregator_as = stream_getl (peer->ibuf); |
| 1385 | else |
| 1386 | attre->aggregator_as = stream_getw (peer->ibuf); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1387 | attre->aggregator_addr.s_addr = stream_get_ipv4 (peer->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1388 | |
| 1389 | /* Set atomic aggregate flag. */ |
| 1390 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR); |
| 1391 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1392 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1395 | /* New Aggregator attribute */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1396 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1397 | bgp_attr_as4_aggregator (struct bgp_attr_parser_args *args, |
| 1398 | as_t *as4_aggregator_as, |
| 1399 | struct in_addr *as4_aggregator_addr) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1400 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1401 | struct peer *const peer = args->peer; |
| 1402 | struct attr *const attr = args->attr; |
| 1403 | const bgp_size_t length = args->length; |
| 1404 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1405 | if (length != 8) |
| 1406 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1407 | zlog (peer->log, LOG_ERR, "New Aggregator length is not 8 [%d]", |
| 1408 | length); |
| 1409 | return bgp_attr_malformed (args, |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1410 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1411 | 0); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1412 | } |
Paul Jakma | 3ecab4c | 2012-01-17 13:31:33 +0000 | [diff] [blame] | 1413 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1414 | *as4_aggregator_as = stream_getl (peer->ibuf); |
| 1415 | as4_aggregator_addr->s_addr = stream_get_ipv4 (peer->ibuf); |
| 1416 | |
| 1417 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR); |
| 1418 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1419 | return BGP_ATTR_PARSE_PROCEED; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH. |
| 1423 | */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1424 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1425 | bgp_attr_munge_as4_attrs (struct peer *const peer, |
| 1426 | struct attr *const attr, |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1427 | struct aspath *as4_path, as_t as4_aggregator, |
| 1428 | struct in_addr *as4_aggregator_addr) |
| 1429 | { |
| 1430 | int ignore_as4_path = 0; |
| 1431 | struct aspath *newpath; |
| 1432 | struct attr_extra *attre = attr->extra; |
Paul Jakma | 055086f | 2014-09-23 15:23:01 +0100 | [diff] [blame] | 1433 | |
| 1434 | if (!attr->aspath) |
| 1435 | { |
| 1436 | /* NULL aspath shouldn't be possible as bgp_attr_parse should have |
| 1437 | * checked that all well-known, mandatory attributes were present. |
| 1438 | * |
| 1439 | * Can only be a problem with peer itself - hard error |
| 1440 | */ |
| 1441 | return BGP_ATTR_PARSE_ERROR; |
| 1442 | } |
| 1443 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1444 | if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1445 | { |
| 1446 | /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR |
| 1447 | * if given. |
| 1448 | * It is worth a warning though, because the peer really |
| 1449 | * should not send them |
| 1450 | */ |
| 1451 | if (BGP_DEBUG(as4, AS4)) |
| 1452 | { |
| 1453 | if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH))) |
| 1454 | zlog_debug ("[AS4] %s %s AS4_PATH", |
| 1455 | peer->host, "AS4 capable peer, yet it sent"); |
| 1456 | |
| 1457 | if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR))) |
| 1458 | zlog_debug ("[AS4] %s %s AS4_AGGREGATOR", |
| 1459 | peer->host, "AS4 capable peer, yet it sent"); |
| 1460 | } |
| 1461 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1462 | return BGP_ATTR_PARSE_PROCEED; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1465 | /* We have a asn16 peer. First, look for AS4_AGGREGATOR |
| 1466 | * because that may override AS4_PATH |
| 1467 | */ |
| 1468 | if (attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR) ) ) |
| 1469 | { |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1470 | if (attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR) ) ) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1471 | { |
Paul Jakma | 370b64a | 2007-12-22 16:49:52 +0000 | [diff] [blame] | 1472 | assert (attre); |
| 1473 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1474 | /* received both. |
| 1475 | * if the as_number in aggregator is not AS_TRANS, |
| 1476 | * then AS4_AGGREGATOR and AS4_PATH shall be ignored |
| 1477 | * and the Aggregator shall be taken as |
| 1478 | * info on the aggregating node, and the AS_PATH |
| 1479 | * shall be taken as the AS_PATH |
| 1480 | * otherwise |
| 1481 | * the Aggregator shall be ignored and the |
| 1482 | * AS4_AGGREGATOR shall be taken as the |
| 1483 | * Aggregating node and the AS_PATH is to be |
| 1484 | * constructed "as in all other cases" |
| 1485 | */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1486 | if (attre->aggregator_as != BGP_AS_TRANS) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1487 | { |
| 1488 | /* ignore */ |
| 1489 | if ( BGP_DEBUG(as4, AS4)) |
| 1490 | zlog_debug ("[AS4] %s BGP not AS4 capable peer" |
| 1491 | " send AGGREGATOR != AS_TRANS and" |
| 1492 | " AS4_AGGREGATOR, so ignore" |
| 1493 | " AS4_AGGREGATOR and AS4_PATH", peer->host); |
| 1494 | ignore_as4_path = 1; |
| 1495 | } |
| 1496 | else |
| 1497 | { |
| 1498 | /* "New_aggregator shall be taken as aggregator" */ |
| 1499 | attre->aggregator_as = as4_aggregator; |
| 1500 | attre->aggregator_addr.s_addr = as4_aggregator_addr->s_addr; |
| 1501 | } |
| 1502 | } |
| 1503 | else |
| 1504 | { |
| 1505 | /* We received a AS4_AGGREGATOR but no AGGREGATOR. |
| 1506 | * That is bogus - but reading the conditions |
| 1507 | * we have to handle AS4_AGGREGATOR as if it were |
| 1508 | * AGGREGATOR in that case |
| 1509 | */ |
| 1510 | if ( BGP_DEBUG(as4, AS4)) |
| 1511 | zlog_debug ("[AS4] %s BGP not AS4 capable peer send" |
| 1512 | " AS4_AGGREGATOR but no AGGREGATOR, will take" |
| 1513 | " it as if AGGREGATOR with AS_TRANS had been there", peer->host); |
Paul Jakma | 370b64a | 2007-12-22 16:49:52 +0000 | [diff] [blame] | 1514 | (attre = bgp_attr_extra_get (attr))->aggregator_as = as4_aggregator; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1515 | /* sweep it under the carpet and simulate a "good" AGGREGATOR */ |
| 1516 | attr->flag |= (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR)); |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | /* need to reconcile NEW_AS_PATH and AS_PATH */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1521 | if (!ignore_as4_path && (attr->flag & (ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH)))) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1522 | { |
Paul Jakma | 055086f | 2014-09-23 15:23:01 +0100 | [diff] [blame] | 1523 | newpath = aspath_reconcile_as4 (attr->aspath, as4_path); |
| 1524 | aspath_unintern (&attr->aspath); |
| 1525 | attr->aspath = aspath_intern (newpath); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1526 | } |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1527 | return BGP_ATTR_PARSE_PROCEED; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1530 | /* Community attribute. */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1531 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1532 | bgp_attr_community (struct bgp_attr_parser_args *args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1533 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1534 | struct peer *const peer = args->peer; |
| 1535 | struct attr *const attr = args->attr; |
| 1536 | const bgp_size_t length = args->length; |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1537 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1538 | if (length == 0) |
Paul Jakma | b2ceea1 | 2007-09-07 14:24:55 +0000 | [diff] [blame] | 1539 | { |
| 1540 | attr->community = NULL; |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1541 | return BGP_ATTR_PARSE_PROCEED; |
Paul Jakma | b2ceea1 | 2007-09-07 14:24:55 +0000 | [diff] [blame] | 1542 | } |
Paul Jakma | 0c46638 | 2010-12-05 17:17:26 +0000 | [diff] [blame] | 1543 | |
| 1544 | attr->community = |
| 1545 | community_parse ((u_int32_t *)stream_pnt (peer->ibuf), length); |
| 1546 | |
| 1547 | /* XXX: fix community_parse to use stream API and remove this */ |
| 1548 | stream_forward_getp (peer->ibuf, length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1549 | |
Paul Jakma | 0c46638 | 2010-12-05 17:17:26 +0000 | [diff] [blame] | 1550 | if (!attr->community) |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1551 | return bgp_attr_malformed (args, |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1552 | BGP_NOTIFY_UPDATE_OPT_ATTR_ERR, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1553 | args->total); |
Paul Jakma | 0c46638 | 2010-12-05 17:17:26 +0000 | [diff] [blame] | 1554 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1555 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES); |
| 1556 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1557 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | /* Originator ID attribute. */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1561 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1562 | bgp_attr_originator_id (struct bgp_attr_parser_args *args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1563 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1564 | struct peer *const peer = args->peer; |
| 1565 | struct attr *const attr = args->attr; |
| 1566 | const bgp_size_t length = args->length; |
| 1567 | |
Denis Ovsienko | d595b56 | 2011-09-30 15:08:54 +0400 | [diff] [blame] | 1568 | /* Length check. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1569 | if (length != 4) |
| 1570 | { |
| 1571 | zlog (peer->log, LOG_ERR, "Bad originator ID length %d", length); |
| 1572 | |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1573 | return bgp_attr_malformed (args, |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1574 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1575 | args->total); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1578 | (bgp_attr_extra_get (attr))->originator_id.s_addr |
| 1579 | = stream_get_ipv4 (peer->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1580 | |
| 1581 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID); |
| 1582 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1583 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | /* Cluster list attribute. */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1587 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1588 | bgp_attr_cluster_list (struct bgp_attr_parser_args *args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1589 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1590 | struct peer *const peer = args->peer; |
| 1591 | struct attr *const attr = args->attr; |
| 1592 | const bgp_size_t length = args->length; |
| 1593 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1594 | /* Check length. */ |
| 1595 | if (length % 4) |
| 1596 | { |
| 1597 | zlog (peer->log, LOG_ERR, "Bad cluster list length %d", length); |
| 1598 | |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1599 | return bgp_attr_malformed (args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
| 1600 | args->total); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1603 | (bgp_attr_extra_get (attr))->cluster |
| 1604 | = cluster_parse ((struct in_addr *)stream_pnt (peer->ibuf), length); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1605 | |
| 1606 | /* XXX: Fix cluster_parse to use stream API and then remove this */ |
| 1607 | stream_forward_getp (peer->ibuf, length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1608 | |
| 1609 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST); |
| 1610 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1611 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | /* Multiprotocol reachability information parse. */ |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1615 | int |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1616 | bgp_mp_reach_parse (struct bgp_attr_parser_args *args, |
| 1617 | struct bgp_nlri *mp_update) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1618 | { |
Michael Lambert | 4c9641b | 2010-07-22 13:20:55 -0400 | [diff] [blame] | 1619 | afi_t afi; |
| 1620 | safi_t safi; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1621 | bgp_size_t nlri_len; |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1622 | size_t start; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1623 | struct stream *s; |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1624 | struct peer *const peer = args->peer; |
| 1625 | struct attr *const attr = args->attr; |
| 1626 | const bgp_size_t length = args->length; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1627 | struct attr_extra *attre = bgp_attr_extra_get(attr); |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1628 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1629 | /* Set end of packet. */ |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1630 | s = BGP_INPUT(peer); |
| 1631 | start = stream_get_getp(s); |
| 1632 | |
| 1633 | /* safe to read statically sized header? */ |
| 1634 | #define BGP_MP_REACH_MIN_SIZE 5 |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1635 | #define LEN_LEFT (length - (stream_get_getp(s) - start)) |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1636 | if ((length > STREAM_READABLE(s)) || (length < BGP_MP_REACH_MIN_SIZE)) |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1637 | { |
| 1638 | zlog_info ("%s: %s sent invalid length, %lu", |
| 1639 | __func__, peer->host, (unsigned long)length); |
David Lamparter | f57000c | 2014-06-04 01:01:10 +0200 | [diff] [blame] | 1640 | return BGP_ATTR_PARSE_ERROR_NOTIFYPLS; |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1641 | } |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1642 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1643 | /* Load AFI, SAFI. */ |
| 1644 | afi = stream_getw (s); |
| 1645 | safi = stream_getc (s); |
| 1646 | |
| 1647 | /* Get nexthop length. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1648 | attre->mp_nexthop_len = stream_getc (s); |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1649 | |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1650 | if (LEN_LEFT < attre->mp_nexthop_len) |
| 1651 | { |
| 1652 | zlog_info ("%s: %s, MP nexthop length, %u, goes past end of attribute", |
| 1653 | __func__, peer->host, attre->mp_nexthop_len); |
David Lamparter | f57000c | 2014-06-04 01:01:10 +0200 | [diff] [blame] | 1654 | return BGP_ATTR_PARSE_ERROR_NOTIFYPLS; |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1655 | } |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1656 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1657 | /* Nexthop length check. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1658 | switch (attre->mp_nexthop_len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1659 | { |
| 1660 | case 4: |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1661 | stream_get (&attre->mp_nexthop_global_in, s, 4); |
Michael Lambert | 66bed4f | 2009-07-28 11:26:14 -0400 | [diff] [blame] | 1662 | /* Probably needed for RFC 2283 */ |
| 1663 | if (attr->nexthop.s_addr == 0) |
| 1664 | memcpy(&attr->nexthop.s_addr, &attre->mp_nexthop_global_in, 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1665 | break; |
| 1666 | case 12: |
Stephen Hemminger | 9206f9e | 2011-12-18 19:43:40 +0400 | [diff] [blame] | 1667 | stream_getl (s); /* RD high */ |
| 1668 | stream_getl (s); /* RD low */ |
| 1669 | stream_get (&attre->mp_nexthop_global_in, s, 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1670 | break; |
Lou Berger | 9da04bc | 2016-01-12 13:41:55 -0500 | [diff] [blame] | 1671 | case 24: |
| 1672 | { |
| 1673 | u_int32_t rd_high __attribute__((unused)); |
| 1674 | u_int32_t rd_low __attribute__((unused)); |
| 1675 | |
| 1676 | rd_high = stream_getl (s); |
| 1677 | rd_low = stream_getl (s); |
| 1678 | } |
| 1679 | /* fall through */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1680 | case 16: |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1681 | stream_get (&attre->mp_nexthop_global, s, 16); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1682 | break; |
| 1683 | case 32: |
Lou Berger | 9da04bc | 2016-01-12 13:41:55 -0500 | [diff] [blame] | 1684 | case 48: |
| 1685 | if (attre->mp_nexthop_len == 48) { |
| 1686 | u_int32_t rd_high __attribute__((unused)); |
| 1687 | u_int32_t rd_low __attribute__((unused)); |
| 1688 | |
| 1689 | rd_high = stream_getl (s); |
| 1690 | rd_low = stream_getl (s); |
| 1691 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1692 | stream_get (&attre->mp_nexthop_global, s, 16); |
Lou Berger | 9da04bc | 2016-01-12 13:41:55 -0500 | [diff] [blame] | 1693 | |
| 1694 | if (attre->mp_nexthop_len == 48) { |
| 1695 | u_int32_t rd_high __attribute__((unused)); |
| 1696 | u_int32_t rd_low __attribute__((unused)); |
| 1697 | |
| 1698 | rd_high = stream_getl (s); |
| 1699 | rd_low = stream_getl (s); |
| 1700 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1701 | stream_get (&attre->mp_nexthop_local, s, 16); |
| 1702 | if (! IN6_IS_ADDR_LINKLOCAL (&attre->mp_nexthop_local)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1703 | { |
| 1704 | char buf1[INET6_ADDRSTRLEN]; |
| 1705 | char buf2[INET6_ADDRSTRLEN]; |
| 1706 | |
| 1707 | if (BGP_DEBUG (update, UPDATE_IN)) |
ajs | 557865c | 2004-12-08 19:59:11 +0000 | [diff] [blame] | 1708 | zlog_debug ("%s got two nexthop %s %s but second one is not a link-local nexthop", peer->host, |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1709 | inet_ntop (AF_INET6, &attre->mp_nexthop_global, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1710 | buf1, INET6_ADDRSTRLEN), |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1711 | inet_ntop (AF_INET6, &attre->mp_nexthop_local, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1712 | buf2, INET6_ADDRSTRLEN)); |
| 1713 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1714 | attre->mp_nexthop_len = 16; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1715 | } |
| 1716 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1717 | default: |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1718 | zlog_info ("%s: (%s) Wrong multiprotocol next hop length: %d", |
| 1719 | __func__, peer->host, attre->mp_nexthop_len); |
David Lamparter | f57000c | 2014-06-04 01:01:10 +0200 | [diff] [blame] | 1720 | return BGP_ATTR_PARSE_ERROR_NOTIFYPLS; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1723 | if (!LEN_LEFT) |
| 1724 | { |
| 1725 | zlog_info ("%s: (%s) Failed to read SNPA and NLRI(s)", |
| 1726 | __func__, peer->host); |
David Lamparter | f57000c | 2014-06-04 01:01:10 +0200 | [diff] [blame] | 1727 | return BGP_ATTR_PARSE_ERROR_NOTIFYPLS; |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1728 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1729 | |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1730 | { |
| 1731 | u_char val; |
| 1732 | if ((val = stream_getc (s))) |
| 1733 | zlog_warn ("%s sent non-zero value, %u, for defunct SNPA-length field", |
| 1734 | peer->host, val); |
| 1735 | } |
| 1736 | |
| 1737 | /* must have nrli_len, what is left of the attribute */ |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1738 | nlri_len = LEN_LEFT; |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1739 | if ((!nlri_len) || (nlri_len > STREAM_READABLE(s))) |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1740 | { |
| 1741 | zlog_info ("%s: (%s) Failed to read NLRI", |
| 1742 | __func__, peer->host); |
David Lamparter | f57000c | 2014-06-04 01:01:10 +0200 | [diff] [blame] | 1743 | return BGP_ATTR_PARSE_ERROR_NOTIFYPLS; |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1744 | } |
Paul Jakma | 18ab08b | 2016-01-27 16:37:33 +0000 | [diff] [blame] | 1745 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1746 | mp_update->afi = afi; |
| 1747 | mp_update->safi = safi; |
| 1748 | mp_update->nlri = stream_pnt (s); |
| 1749 | mp_update->length = nlri_len; |
| 1750 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1751 | stream_forward_getp (s, nlri_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1752 | |
David Lamparter | daefeb8 | 2014-12-08 17:42:12 +0100 | [diff] [blame] | 1753 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_MP_REACH_NLRI); |
| 1754 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1755 | return BGP_ATTR_PARSE_PROCEED; |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1756 | #undef LEN_LEFT |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | /* Multiprotocol unreachable parse */ |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1760 | int |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1761 | bgp_mp_unreach_parse (struct bgp_attr_parser_args *args, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1762 | struct bgp_nlri *mp_withdraw) |
| 1763 | { |
| 1764 | struct stream *s; |
Michael Lambert | 4c9641b | 2010-07-22 13:20:55 -0400 | [diff] [blame] | 1765 | afi_t afi; |
| 1766 | safi_t safi; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1767 | u_int16_t withdraw_len; |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1768 | struct peer *const peer = args->peer; |
David Lamparter | daefeb8 | 2014-12-08 17:42:12 +0100 | [diff] [blame] | 1769 | struct attr *const attr = args->attr; |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1770 | const bgp_size_t length = args->length; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1771 | |
| 1772 | s = peer->ibuf; |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1773 | |
| 1774 | #define BGP_MP_UNREACH_MIN_SIZE 3 |
| 1775 | if ((length > STREAM_READABLE(s)) || (length < BGP_MP_UNREACH_MIN_SIZE)) |
David Lamparter | f57000c | 2014-06-04 01:01:10 +0200 | [diff] [blame] | 1776 | return BGP_ATTR_PARSE_ERROR_NOTIFYPLS; |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1777 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1778 | afi = stream_getw (s); |
| 1779 | safi = stream_getc (s); |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1780 | |
| 1781 | withdraw_len = length - BGP_MP_UNREACH_MIN_SIZE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1782 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1783 | mp_withdraw->afi = afi; |
| 1784 | mp_withdraw->safi = safi; |
| 1785 | mp_withdraw->nlri = stream_pnt (s); |
| 1786 | mp_withdraw->length = withdraw_len; |
| 1787 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1788 | stream_forward_getp (s, withdraw_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1789 | |
David Lamparter | daefeb8 | 2014-12-08 17:42:12 +0100 | [diff] [blame] | 1790 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_MP_UNREACH_NLRI); |
| 1791 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1792 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | /* Extended Community attribute. */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1796 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1797 | bgp_attr_ext_communities (struct bgp_attr_parser_args *args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1798 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1799 | struct peer *const peer = args->peer; |
| 1800 | struct attr *const attr = args->attr; |
| 1801 | const bgp_size_t length = args->length; |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1802 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1803 | if (length == 0) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1804 | { |
| 1805 | if (attr->extra) |
| 1806 | attr->extra->ecommunity = NULL; |
Paul Jakma | 0c46638 | 2010-12-05 17:17:26 +0000 | [diff] [blame] | 1807 | /* Empty extcomm doesn't seem to be invalid per se */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1808 | return BGP_ATTR_PARSE_PROCEED; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1809 | } |
Paul Jakma | 0c46638 | 2010-12-05 17:17:26 +0000 | [diff] [blame] | 1810 | |
| 1811 | (bgp_attr_extra_get (attr))->ecommunity = |
| 1812 | ecommunity_parse ((u_int8_t *)stream_pnt (peer->ibuf), length); |
| 1813 | /* XXX: fix ecommunity_parse to use stream API */ |
| 1814 | stream_forward_getp (peer->ibuf, length); |
| 1815 | |
| 1816 | if (!attr->extra->ecommunity) |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1817 | return bgp_attr_malformed (args, |
| 1818 | BGP_NOTIFY_UPDATE_OPT_ATTR_ERR, |
| 1819 | args->total); |
Paul Jakma | 0c46638 | 2010-12-05 17:17:26 +0000 | [diff] [blame] | 1820 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1821 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES); |
| 1822 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1823 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 1826 | /* Parse Tunnel Encap attribute in an UPDATE */ |
| 1827 | static int |
| 1828 | bgp_attr_encap( |
| 1829 | uint8_t type, |
| 1830 | struct peer *peer, /* IN */ |
| 1831 | bgp_size_t length, /* IN: attr's length field */ |
| 1832 | struct attr *attr, /* IN: caller already allocated */ |
| 1833 | u_char flag, /* IN: attr's flags field */ |
| 1834 | u_char *startp) |
| 1835 | { |
| 1836 | bgp_size_t total; |
| 1837 | struct attr_extra *attre = NULL; |
| 1838 | struct bgp_attr_encap_subtlv *stlv_last = NULL; |
| 1839 | uint16_t tunneltype; |
| 1840 | |
| 1841 | total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3); |
| 1842 | |
| 1843 | if (!CHECK_FLAG(flag, BGP_ATTR_FLAG_TRANS) |
| 1844 | || !CHECK_FLAG(flag, BGP_ATTR_FLAG_OPTIONAL)) |
| 1845 | { |
| 1846 | zlog (peer->log, LOG_ERR, |
| 1847 | "Tunnel Encap attribute flag isn't optional and transitive %d", flag); |
| 1848 | bgp_notify_send_with_data (peer, |
| 1849 | BGP_NOTIFY_UPDATE_ERR, |
| 1850 | BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR, |
| 1851 | startp, total); |
| 1852 | return -1; |
| 1853 | } |
| 1854 | |
| 1855 | if (BGP_ATTR_ENCAP == type) { |
| 1856 | /* read outer TLV type and length */ |
| 1857 | uint16_t tlv_length; |
| 1858 | |
| 1859 | if (length < 4) { |
| 1860 | zlog (peer->log, LOG_ERR, |
| 1861 | "Tunnel Encap attribute not long enough to contain outer T,L"); |
| 1862 | bgp_notify_send_with_data(peer, |
| 1863 | BGP_NOTIFY_UPDATE_ERR, |
| 1864 | BGP_NOTIFY_UPDATE_OPT_ATTR_ERR, |
| 1865 | startp, total); |
| 1866 | return -1; |
| 1867 | } |
| 1868 | tunneltype = stream_getw (BGP_INPUT (peer)); |
| 1869 | tlv_length = stream_getw (BGP_INPUT (peer)); |
| 1870 | length -= 4; |
| 1871 | |
| 1872 | if (tlv_length != length) { |
| 1873 | zlog (peer->log, LOG_ERR, "%s: tlv_length(%d) != length(%d)", |
| 1874 | __func__, tlv_length, length); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | while (length >= 4) { |
Donald Sharp | 985c355 | 2016-03-10 20:16:47 -0500 | [diff] [blame] | 1879 | uint16_t subtype = 0; |
| 1880 | uint16_t sublength = 0; |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 1881 | struct bgp_attr_encap_subtlv *tlv; |
| 1882 | |
Lou Berger | 298cc2f | 2016-01-12 13:42:02 -0500 | [diff] [blame] | 1883 | if (BGP_ATTR_ENCAP == type) { |
| 1884 | subtype = stream_getc (BGP_INPUT (peer)); |
| 1885 | sublength = stream_getc (BGP_INPUT (peer)); |
| 1886 | length -= 2; |
| 1887 | } |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 1888 | |
| 1889 | if (sublength > length) { |
| 1890 | zlog (peer->log, LOG_ERR, |
| 1891 | "Tunnel Encap attribute sub-tlv length %d exceeds remaining length %d", |
| 1892 | sublength, length); |
| 1893 | bgp_notify_send_with_data (peer, |
| 1894 | BGP_NOTIFY_UPDATE_ERR, |
| 1895 | BGP_NOTIFY_UPDATE_OPT_ATTR_ERR, |
| 1896 | startp, total); |
| 1897 | return -1; |
| 1898 | } |
| 1899 | |
| 1900 | /* alloc and copy sub-tlv */ |
| 1901 | /* TBD make sure these are freed when attributes are released */ |
| 1902 | tlv = XCALLOC (MTYPE_ENCAP_TLV, sizeof(struct bgp_attr_encap_subtlv)-1+sublength); |
| 1903 | tlv->type = subtype; |
| 1904 | tlv->length = sublength; |
| 1905 | stream_get(tlv->value, peer->ibuf, sublength); |
| 1906 | length -= sublength; |
| 1907 | |
| 1908 | /* attach tlv to encap chain */ |
| 1909 | if (!attre) { |
| 1910 | attre = bgp_attr_extra_get(attr); |
| 1911 | if (BGP_ATTR_ENCAP == type) { |
| 1912 | for (stlv_last = attre->encap_subtlvs; stlv_last && stlv_last->next; |
| 1913 | stlv_last = stlv_last->next); |
| 1914 | if (stlv_last) { |
| 1915 | stlv_last->next = tlv; |
| 1916 | } else { |
| 1917 | attre->encap_subtlvs = tlv; |
| 1918 | } |
| 1919 | } |
| 1920 | } else { |
| 1921 | stlv_last->next = tlv; |
| 1922 | } |
| 1923 | stlv_last = tlv; |
| 1924 | } |
| 1925 | |
| 1926 | if (attre && (BGP_ATTR_ENCAP == type)) { |
| 1927 | attre->encap_tunneltype = tunneltype; |
| 1928 | } |
| 1929 | |
| 1930 | if (length) { |
| 1931 | /* spurious leftover data */ |
| 1932 | zlog (peer->log, LOG_ERR, |
| 1933 | "Tunnel Encap attribute length is bad: %d leftover octets", length); |
| 1934 | bgp_notify_send_with_data (peer, |
| 1935 | BGP_NOTIFY_UPDATE_ERR, |
| 1936 | BGP_NOTIFY_UPDATE_OPT_ATTR_ERR, |
| 1937 | startp, total); |
| 1938 | return -1; |
| 1939 | } |
| 1940 | |
| 1941 | return 0; |
| 1942 | } |
| 1943 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1944 | /* BGP unknown attribute treatment. */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1945 | static bgp_attr_parse_ret_t |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1946 | bgp_attr_unknown (struct bgp_attr_parser_args *args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1947 | { |
Paul Jakma | 8794e8d | 2012-02-13 13:53:07 +0000 | [diff] [blame] | 1948 | bgp_size_t total = args->total; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1949 | struct transit *transit; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1950 | struct attr_extra *attre; |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1951 | struct peer *const peer = args->peer; |
| 1952 | struct attr *const attr = args->attr; |
| 1953 | u_char *const startp = args->startp; |
| 1954 | const u_char type = args->type; |
| 1955 | const u_char flag = args->flags; |
| 1956 | const bgp_size_t length = args->length; |
| 1957 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1958 | |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame] | 1959 | if (BGP_DEBUG (normal, NORMAL)) |
| 1960 | zlog_debug ("%s Unknown attribute is received (type %d, length %d)", |
| 1961 | peer->host, type, length); |
| 1962 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1963 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 557865c | 2004-12-08 19:59:11 +0000 | [diff] [blame] | 1964 | zlog (peer->log, LOG_DEBUG, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1965 | "Unknown attribute type %d length %d is received", type, length); |
| 1966 | |
| 1967 | /* Forward read pointer of input stream. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1968 | stream_forward_getp (peer->ibuf, length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1969 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1970 | /* If any of the mandatory well-known attributes are not recognized, |
| 1971 | then the Error Subcode is set to Unrecognized Well-known |
| 1972 | Attribute. The Data field contains the unrecognized attribute |
| 1973 | (type, length and value). */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1974 | if (!CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1975 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1976 | return bgp_attr_malformed (args, |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1977 | BGP_NOTIFY_UPDATE_UNREC_ATTR, |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 1978 | args->total); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | /* Unrecognized non-transitive optional attributes must be quietly |
| 1982 | ignored and not passed along to other BGP peers. */ |
| 1983 | if (! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS)) |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 1984 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1985 | |
| 1986 | /* If a path with recognized transitive optional attribute is |
| 1987 | accepted and passed along to other BGP peers and the Partial bit |
| 1988 | in the Attribute Flags octet is set to 1 by some previous AS, it |
| 1989 | is not set back to 0 by the current AS. */ |
| 1990 | SET_FLAG (*startp, BGP_ATTR_FLAG_PARTIAL); |
| 1991 | |
| 1992 | /* Store transitive attribute to the end of attr->transit. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1993 | if (! ((attre = bgp_attr_extra_get(attr))->transit) ) |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 1994 | attre->transit = XCALLOC (MTYPE_TRANSIT, sizeof (struct transit)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1995 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1996 | transit = attre->transit; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1997 | |
| 1998 | if (transit->val) |
| 1999 | transit->val = XREALLOC (MTYPE_TRANSIT_VAL, transit->val, |
| 2000 | transit->length + total); |
| 2001 | else |
| 2002 | transit->val = XMALLOC (MTYPE_TRANSIT_VAL, total); |
| 2003 | |
| 2004 | memcpy (transit->val + transit->length, startp, total); |
| 2005 | transit->length += total; |
| 2006 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2007 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
Paul Jakma | 055086f | 2014-09-23 15:23:01 +0100 | [diff] [blame] | 2010 | /* Well-known attribute check. */ |
| 2011 | static int |
| 2012 | bgp_attr_check (struct peer *peer, struct attr *attr) |
| 2013 | { |
| 2014 | u_char type = 0; |
| 2015 | |
Paul Jakma | aed1b55 | 2014-10-21 16:59:01 +0100 | [diff] [blame] | 2016 | /* BGP Graceful-Restart End-of-RIB for IPv4 unicast is signaled as an |
| 2017 | * empty UPDATE. */ |
| 2018 | if (CHECK_FLAG (peer->cap, PEER_CAP_RESTART_RCV) && !attr->flag) |
| 2019 | return BGP_ATTR_PARSE_PROCEED; |
| 2020 | |
| 2021 | /* "An UPDATE message that contains the MP_UNREACH_NLRI is not required |
| 2022 | to carry any other path attributes.", though if MP_REACH_NLRI or NLRI |
| 2023 | are present, it should. Check for any other attribute being present |
| 2024 | instead. |
| 2025 | */ |
| 2026 | if (attr->flag == ATTR_FLAG_BIT (BGP_ATTR_MP_UNREACH_NLRI)) |
| 2027 | return BGP_ATTR_PARSE_PROCEED; |
| 2028 | |
Paul Jakma | 055086f | 2014-09-23 15:23:01 +0100 | [diff] [blame] | 2029 | if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_ORIGIN))) |
| 2030 | type = BGP_ATTR_ORIGIN; |
| 2031 | |
| 2032 | if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH))) |
| 2033 | type = BGP_ATTR_AS_PATH; |
Paul Jakma | aed1b55 | 2014-10-21 16:59:01 +0100 | [diff] [blame] | 2034 | |
| 2035 | /* RFC 2858 makes Next-Hop optional/ignored, if MP_REACH_NLRI is present and |
| 2036 | * NLRI is empty. We can't easily check NLRI empty here though. |
| 2037 | */ |
Paul Jakma | 1a211cb | 2014-11-01 17:21:47 +0000 | [diff] [blame] | 2038 | if (!CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP)) |
| 2039 | && !CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_MP_REACH_NLRI))) |
Paul Jakma | 055086f | 2014-09-23 15:23:01 +0100 | [diff] [blame] | 2040 | type = BGP_ATTR_NEXT_HOP; |
Paul Jakma | aed1b55 | 2014-10-21 16:59:01 +0100 | [diff] [blame] | 2041 | |
Paul Jakma | 055086f | 2014-09-23 15:23:01 +0100 | [diff] [blame] | 2042 | if (peer->sort == BGP_PEER_IBGP |
| 2043 | && ! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))) |
| 2044 | type = BGP_ATTR_LOCAL_PREF; |
| 2045 | |
| 2046 | if (type) |
| 2047 | { |
| 2048 | zlog (peer->log, LOG_WARNING, |
Paul Jakma | aed1b55 | 2014-10-21 16:59:01 +0100 | [diff] [blame] | 2049 | "%s Missing well-known attribute %d / %s", |
| 2050 | peer->host, type, LOOKUP (attr_str, type)); |
Paul Jakma | 055086f | 2014-09-23 15:23:01 +0100 | [diff] [blame] | 2051 | bgp_notify_send_with_data (peer, |
| 2052 | BGP_NOTIFY_UPDATE_ERR, |
| 2053 | BGP_NOTIFY_UPDATE_MISS_ATTR, |
| 2054 | &type, 1); |
| 2055 | return BGP_ATTR_PARSE_ERROR; |
| 2056 | } |
| 2057 | return BGP_ATTR_PARSE_PROCEED; |
| 2058 | } |
| 2059 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2060 | /* Read attribute of update packet. This function is called from |
Andrew Certain | 8b366b9 | 2012-11-07 23:50:08 +0000 | [diff] [blame] | 2061 | bgp_update_receive() in bgp_packet.c. */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2062 | bgp_attr_parse_ret_t |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2063 | bgp_attr_parse (struct peer *peer, struct attr *attr, bgp_size_t size, |
| 2064 | struct bgp_nlri *mp_update, struct bgp_nlri *mp_withdraw) |
| 2065 | { |
| 2066 | int ret; |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2067 | u_char flag = 0; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2068 | u_char type = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2069 | bgp_size_t length; |
| 2070 | u_char *startp, *endp; |
| 2071 | u_char *attr_endp; |
| 2072 | u_char seen[BGP_ATTR_BITMAP_SIZE]; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2073 | /* we need the as4_path only until we have synthesized the as_path with it */ |
| 2074 | /* same goes for as4_aggregator */ |
| 2075 | struct aspath *as4_path = NULL; |
| 2076 | as_t as4_aggregator = 0; |
David Lamparter | 5181a02 | 2015-09-15 03:00:09 -0700 | [diff] [blame] | 2077 | struct in_addr as4_aggregator_addr = { .s_addr = 0 }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2078 | |
| 2079 | /* Initialize bitmap. */ |
| 2080 | memset (seen, 0, BGP_ATTR_BITMAP_SIZE); |
| 2081 | |
| 2082 | /* End pointer of BGP attribute. */ |
| 2083 | endp = BGP_INPUT_PNT (peer) + size; |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2084 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2085 | /* Get attributes to the end of attribute length. */ |
| 2086 | while (BGP_INPUT_PNT (peer) < endp) |
| 2087 | { |
| 2088 | /* Check remaining length check.*/ |
| 2089 | if (endp - BGP_INPUT_PNT (peer) < BGP_ATTR_MIN_LEN) |
| 2090 | { |
gdt | c29fdba | 2004-12-09 14:46:46 +0000 | [diff] [blame] | 2091 | /* XXX warning: long int format, int arg (arg 5) */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2092 | zlog (peer->log, LOG_WARNING, |
heasley | d68ab10 | 2011-07-12 20:09:18 +0400 | [diff] [blame] | 2093 | "%s: error BGP attribute length %lu is smaller than min len", |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2094 | peer->host, |
| 2095 | (unsigned long) (endp - STREAM_PNT (BGP_INPUT (peer)))); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2096 | |
| 2097 | bgp_notify_send (peer, |
| 2098 | BGP_NOTIFY_UPDATE_ERR, |
| 2099 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2100 | return BGP_ATTR_PARSE_ERROR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | /* Fetch attribute flag and type. */ |
| 2104 | startp = BGP_INPUT_PNT (peer); |
Denis Ovsienko | 2d42e68 | 2011-09-27 15:35:39 +0400 | [diff] [blame] | 2105 | /* "The lower-order four bits of the Attribute Flags octet are |
| 2106 | unused. They MUST be zero when sent and MUST be ignored when |
| 2107 | received." */ |
| 2108 | flag = 0xF0 & stream_getc (BGP_INPUT (peer)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2109 | type = stream_getc (BGP_INPUT (peer)); |
| 2110 | |
Paul Jakma | 370b64a | 2007-12-22 16:49:52 +0000 | [diff] [blame] | 2111 | /* Check whether Extended-Length applies and is in bounds */ |
| 2112 | if (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) |
| 2113 | && ((endp - startp) < (BGP_ATTR_MIN_LEN + 1))) |
| 2114 | { |
| 2115 | zlog (peer->log, LOG_WARNING, |
heasley | d68ab10 | 2011-07-12 20:09:18 +0400 | [diff] [blame] | 2116 | "%s: Extended length set, but just %lu bytes of attr header", |
Paul Jakma | 370b64a | 2007-12-22 16:49:52 +0000 | [diff] [blame] | 2117 | peer->host, |
| 2118 | (unsigned long) (endp - STREAM_PNT (BGP_INPUT (peer)))); |
| 2119 | |
| 2120 | bgp_notify_send (peer, |
| 2121 | BGP_NOTIFY_UPDATE_ERR, |
| 2122 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2123 | return BGP_ATTR_PARSE_ERROR; |
Paul Jakma | 370b64a | 2007-12-22 16:49:52 +0000 | [diff] [blame] | 2124 | } |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2125 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2126 | /* Check extended attribue length bit. */ |
| 2127 | if (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN)) |
| 2128 | length = stream_getw (BGP_INPUT (peer)); |
| 2129 | else |
| 2130 | length = stream_getc (BGP_INPUT (peer)); |
| 2131 | |
| 2132 | /* If any attribute appears more than once in the UPDATE |
| 2133 | message, then the Error Subcode is set to Malformed Attribute |
| 2134 | List. */ |
| 2135 | |
| 2136 | if (CHECK_BITMAP (seen, type)) |
| 2137 | { |
| 2138 | zlog (peer->log, LOG_WARNING, |
heasley | d68ab10 | 2011-07-12 20:09:18 +0400 | [diff] [blame] | 2139 | "%s: error BGP attribute type %d appears twice in a message", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2140 | peer->host, type); |
| 2141 | |
| 2142 | bgp_notify_send (peer, |
| 2143 | BGP_NOTIFY_UPDATE_ERR, |
| 2144 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2145 | return BGP_ATTR_PARSE_ERROR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | /* Set type to bitmap to check duplicate attribute. `type' is |
| 2149 | unsigned char so it never overflow bitmap range. */ |
| 2150 | |
| 2151 | SET_BITMAP (seen, type); |
| 2152 | |
| 2153 | /* Overflow check. */ |
| 2154 | attr_endp = BGP_INPUT_PNT (peer) + length; |
| 2155 | |
| 2156 | if (attr_endp > endp) |
| 2157 | { |
| 2158 | zlog (peer->log, LOG_WARNING, |
heasley | d68ab10 | 2011-07-12 20:09:18 +0400 | [diff] [blame] | 2159 | "%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p", peer->host, type, length, size, attr_endp, endp); |
Daniel Walton | c696987 | 2015-05-19 18:03:43 -0700 | [diff] [blame] | 2160 | zlog_warn ("%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p", peer->host, type, length, size, attr_endp, endp); |
| 2161 | bgp_notify_send_with_data (peer, |
| 2162 | BGP_NOTIFY_UPDATE_ERR, |
| 2163 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
| 2164 | startp, attr_endp - startp); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2165 | return BGP_ATTR_PARSE_ERROR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2166 | } |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2167 | |
| 2168 | struct bgp_attr_parser_args attr_args = { |
| 2169 | .peer = peer, |
| 2170 | .length = length, |
| 2171 | .attr = attr, |
| 2172 | .type = type, |
| 2173 | .flags = flag, |
| 2174 | .startp = startp, |
| 2175 | .total = attr_endp - startp, |
| 2176 | }; |
| 2177 | |
| 2178 | |
| 2179 | /* If any recognized attribute has Attribute Flags that conflict |
| 2180 | with the Attribute Type Code, then the Error Subcode is set to |
| 2181 | Attribute Flags Error. The Data field contains the erroneous |
| 2182 | attribute (type, length and value). */ |
| 2183 | if (bgp_attr_flag_invalid (&attr_args)) |
Paul Jakma | fa61e16 | 2012-03-25 21:31:47 +0100 | [diff] [blame] | 2184 | { |
| 2185 | bgp_attr_parse_ret_t ret; |
| 2186 | ret = bgp_attr_malformed (&attr_args, |
| 2187 | BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR, |
| 2188 | attr_args.total); |
| 2189 | if (ret == BGP_ATTR_PARSE_PROCEED) |
| 2190 | continue; |
| 2191 | return ret; |
| 2192 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2193 | |
| 2194 | /* OK check attribute and store it's value. */ |
| 2195 | switch (type) |
| 2196 | { |
| 2197 | case BGP_ATTR_ORIGIN: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2198 | ret = bgp_attr_origin (&attr_args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2199 | break; |
| 2200 | case BGP_ATTR_AS_PATH: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2201 | ret = bgp_attr_aspath (&attr_args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2202 | break; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2203 | case BGP_ATTR_AS4_PATH: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2204 | ret = bgp_attr_as4_path (&attr_args, &as4_path); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2205 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2206 | case BGP_ATTR_NEXT_HOP: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2207 | ret = bgp_attr_nexthop (&attr_args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2208 | break; |
| 2209 | case BGP_ATTR_MULTI_EXIT_DISC: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2210 | ret = bgp_attr_med (&attr_args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2211 | break; |
| 2212 | case BGP_ATTR_LOCAL_PREF: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2213 | ret = bgp_attr_local_pref (&attr_args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2214 | break; |
| 2215 | case BGP_ATTR_ATOMIC_AGGREGATE: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2216 | ret = bgp_attr_atomic (&attr_args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2217 | break; |
| 2218 | case BGP_ATTR_AGGREGATOR: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2219 | ret = bgp_attr_aggregator (&attr_args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2220 | break; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2221 | case BGP_ATTR_AS4_AGGREGATOR: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2222 | ret = bgp_attr_as4_aggregator (&attr_args, |
| 2223 | &as4_aggregator, |
| 2224 | &as4_aggregator_addr); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2225 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2226 | case BGP_ATTR_COMMUNITIES: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2227 | ret = bgp_attr_community (&attr_args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2228 | break; |
| 2229 | case BGP_ATTR_ORIGINATOR_ID: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2230 | ret = bgp_attr_originator_id (&attr_args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2231 | break; |
| 2232 | case BGP_ATTR_CLUSTER_LIST: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2233 | ret = bgp_attr_cluster_list (&attr_args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2234 | break; |
| 2235 | case BGP_ATTR_MP_REACH_NLRI: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2236 | ret = bgp_mp_reach_parse (&attr_args, mp_update); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2237 | break; |
| 2238 | case BGP_ATTR_MP_UNREACH_NLRI: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2239 | ret = bgp_mp_unreach_parse (&attr_args, mp_withdraw); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2240 | break; |
| 2241 | case BGP_ATTR_EXT_COMMUNITIES: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2242 | ret = bgp_attr_ext_communities (&attr_args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2243 | break; |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 2244 | case BGP_ATTR_ENCAP: |
| 2245 | ret = bgp_attr_encap (type, peer, length, attr, flag, startp); |
| 2246 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2247 | default: |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2248 | ret = bgp_attr_unknown (&attr_args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2249 | break; |
| 2250 | } |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2251 | |
David Lamparter | f57000c | 2014-06-04 01:01:10 +0200 | [diff] [blame] | 2252 | if (ret == BGP_ATTR_PARSE_ERROR_NOTIFYPLS) |
| 2253 | { |
| 2254 | bgp_notify_send (peer, |
| 2255 | BGP_NOTIFY_UPDATE_ERR, |
| 2256 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
| 2257 | ret = BGP_ATTR_PARSE_ERROR; |
| 2258 | } |
| 2259 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2260 | /* If hard error occured immediately return to the caller. */ |
| 2261 | if (ret == BGP_ATTR_PARSE_ERROR) |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 2262 | { |
| 2263 | zlog (peer->log, LOG_WARNING, |
| 2264 | "%s: Attribute %s, parse error", |
| 2265 | peer->host, |
| 2266 | LOOKUP (attr_str, type)); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2267 | if (as4_path) |
| 2268 | aspath_unintern (&as4_path); |
| 2269 | return ret; |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 2270 | } |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2271 | if (ret == BGP_ATTR_PARSE_WITHDRAW) |
| 2272 | { |
| 2273 | |
| 2274 | zlog (peer->log, LOG_WARNING, |
| 2275 | "%s: Attribute %s, parse error - treating as withdrawal", |
| 2276 | peer->host, |
| 2277 | LOOKUP (attr_str, type)); |
| 2278 | if (as4_path) |
| 2279 | aspath_unintern (&as4_path); |
| 2280 | return ret; |
| 2281 | } |
| 2282 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2283 | /* Check the fetched length. */ |
| 2284 | if (BGP_INPUT_PNT (peer) != attr_endp) |
| 2285 | { |
| 2286 | zlog (peer->log, LOG_WARNING, |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 2287 | "%s: BGP attribute %s, fetch error", |
| 2288 | peer->host, LOOKUP (attr_str, type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2289 | bgp_notify_send (peer, |
| 2290 | BGP_NOTIFY_UPDATE_ERR, |
| 2291 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2292 | if (as4_path) |
| 2293 | aspath_unintern (&as4_path); |
| 2294 | return BGP_ATTR_PARSE_ERROR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2295 | } |
| 2296 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2297 | /* Check final read pointer is same as end pointer. */ |
| 2298 | if (BGP_INPUT_PNT (peer) != endp) |
| 2299 | { |
| 2300 | zlog (peer->log, LOG_WARNING, |
heasley | d68ab10 | 2011-07-12 20:09:18 +0400 | [diff] [blame] | 2301 | "%s: BGP attribute %s, length mismatch", |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 2302 | peer->host, LOOKUP (attr_str, type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2303 | bgp_notify_send (peer, |
| 2304 | BGP_NOTIFY_UPDATE_ERR, |
| 2305 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2306 | if (as4_path) |
| 2307 | aspath_unintern (&as4_path); |
| 2308 | return BGP_ATTR_PARSE_ERROR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2309 | } |
Paul Jakma | 055086f | 2014-09-23 15:23:01 +0100 | [diff] [blame] | 2310 | |
| 2311 | /* Check all mandatory well-known attributes are present */ |
| 2312 | { |
| 2313 | bgp_attr_parse_ret_t ret; |
| 2314 | if ((ret = bgp_attr_check (peer, attr)) < 0) |
| 2315 | { |
| 2316 | if (as4_path) |
| 2317 | aspath_unintern (&as4_path); |
| 2318 | return ret; |
| 2319 | } |
| 2320 | } |
| 2321 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2322 | /* |
| 2323 | * At this place we can see whether we got AS4_PATH and/or |
| 2324 | * AS4_AGGREGATOR from a 16Bit peer and act accordingly. |
| 2325 | * We can not do this before we've read all attributes because |
| 2326 | * the as4 handling does not say whether AS4_PATH has to be sent |
| 2327 | * after AS_PATH or not - and when AS4_AGGREGATOR will be send |
| 2328 | * in relationship to AGGREGATOR. |
| 2329 | * So, to be defensive, we are not relying on any order and read |
| 2330 | * all attributes first, including these 32bit ones, and now, |
| 2331 | * afterwards, we look what and if something is to be done for as4. |
Paul Jakma | aed1b55 | 2014-10-21 16:59:01 +0100 | [diff] [blame] | 2332 | * |
| 2333 | * It is possible to not have AS_PATH, e.g. GR EoR and sole |
| 2334 | * MP_UNREACH_NLRI. |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2335 | */ |
David Lamparter | f57000c | 2014-06-04 01:01:10 +0200 | [diff] [blame] | 2336 | /* actually... this doesn't ever return failure currently, but |
| 2337 | * better safe than sorry */ |
Paul Jakma | aed1b55 | 2014-10-21 16:59:01 +0100 | [diff] [blame] | 2338 | if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH)) |
| 2339 | && bgp_attr_munge_as4_attrs (peer, attr, as4_path, |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2340 | as4_aggregator, &as4_aggregator_addr)) |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2341 | { |
David Lamparter | f57000c | 2014-06-04 01:01:10 +0200 | [diff] [blame] | 2342 | bgp_notify_send (peer, |
| 2343 | BGP_NOTIFY_UPDATE_ERR, |
| 2344 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2345 | if (as4_path) |
| 2346 | aspath_unintern (&as4_path); |
| 2347 | return BGP_ATTR_PARSE_ERROR; |
| 2348 | } |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2349 | |
| 2350 | /* At this stage, we have done all fiddling with as4, and the |
| 2351 | * resulting info is in attr->aggregator resp. attr->aspath |
| 2352 | * so we can chuck as4_aggregator and as4_path alltogether in |
| 2353 | * order to save memory |
| 2354 | */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2355 | if (as4_path) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2356 | { |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 2357 | aspath_unintern (&as4_path); /* unintern - it is in the hash */ |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2358 | /* The flag that we got this is still there, but that does not |
| 2359 | * do any trouble |
| 2360 | */ |
| 2361 | } |
| 2362 | /* |
| 2363 | * The "rest" of the code does nothing with as4_aggregator. |
| 2364 | * there is no memory attached specifically which is not part |
| 2365 | * of the attr. |
| 2366 | * so ignoring just means do nothing. |
| 2367 | */ |
| 2368 | /* |
| 2369 | * Finally do the checks on the aspath we did not do yet |
| 2370 | * because we waited for a potentially synthesized aspath. |
| 2371 | */ |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2372 | if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH))) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2373 | { |
Paul Jakma | 835315b | 2012-01-18 12:28:30 +0000 | [diff] [blame] | 2374 | ret = bgp_attr_aspath_check (peer, attr); |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2375 | if (ret != BGP_ATTR_PARSE_PROCEED) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2376 | return ret; |
| 2377 | } |
| 2378 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2379 | /* Finally intern unknown attribute. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2380 | if (attr->extra && attr->extra->transit) |
| 2381 | attr->extra->transit = transit_intern (attr->extra->transit); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2382 | |
Paul Jakma | b881c70 | 2010-11-23 16:35:42 +0000 | [diff] [blame] | 2383 | return BGP_ATTR_PARSE_PROCEED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2386 | int stream_put_prefix (struct stream *, struct prefix *); |
| 2387 | |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2388 | size_t |
| 2389 | bgp_packet_mpattr_start (struct stream *s, afi_t afi, safi_t safi, |
| 2390 | struct attr *attr) |
| 2391 | { |
| 2392 | size_t sizep; |
| 2393 | |
| 2394 | /* Set extended bit always to encode the attribute length as 2 bytes */ |
| 2395 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN); |
| 2396 | stream_putc (s, BGP_ATTR_MP_REACH_NLRI); |
| 2397 | sizep = stream_get_endp (s); |
| 2398 | stream_putw (s, 0); /* Marker: Attribute length. */ |
Lou Berger | 050defe | 2016-01-12 13:41:59 -0500 | [diff] [blame] | 2399 | |
| 2400 | stream_putw (s, afi); |
| 2401 | stream_putc (s, (safi == SAFI_MPLS_VPN) ? SAFI_MPLS_LABELED_VPN : safi); |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2402 | |
| 2403 | /* Nexthop */ |
| 2404 | switch (afi) |
| 2405 | { |
| 2406 | case AFI_IP: |
| 2407 | switch (safi) |
| 2408 | { |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2409 | case SAFI_MULTICAST: |
| 2410 | stream_putc (s, 4); |
| 2411 | stream_put_ipv4 (s, attr->nexthop.s_addr); |
| 2412 | break; |
| 2413 | case SAFI_MPLS_VPN: |
| 2414 | stream_putc (s, 12); |
Lou Berger | 050defe | 2016-01-12 13:41:59 -0500 | [diff] [blame] | 2415 | stream_putl (s, 0); /* RD = 0, per RFC */ |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2416 | stream_putl (s, 0); |
| 2417 | stream_put (s, &attr->extra->mp_nexthop_global_in, 4); |
| 2418 | break; |
Lou Berger | 298cc2f | 2016-01-12 13:42:02 -0500 | [diff] [blame] | 2419 | case SAFI_ENCAP: |
| 2420 | stream_putc (s, 4); |
| 2421 | stream_put (s, &attr->extra->mp_nexthop_global_in, 4); |
| 2422 | break; |
Lou Berger | 050defe | 2016-01-12 13:41:59 -0500 | [diff] [blame] | 2423 | case SAFI_UNICAST: /* invalid for IPv4 */ |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2424 | default: |
| 2425 | break; |
| 2426 | } |
| 2427 | break; |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2428 | case AFI_IP6: |
| 2429 | switch (safi) |
| 2430 | { |
| 2431 | case SAFI_UNICAST: |
| 2432 | case SAFI_MULTICAST: |
| 2433 | { |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2434 | struct attr_extra *attre = attr->extra; |
| 2435 | |
| 2436 | assert (attr->extra); |
| 2437 | stream_putc (s, attre->mp_nexthop_len); |
| 2438 | stream_put (s, &attre->mp_nexthop_global, 16); |
| 2439 | if (attre->mp_nexthop_len == 32) |
| 2440 | stream_put (s, &attre->mp_nexthop_local, 16); |
| 2441 | } |
Lou Berger | 050defe | 2016-01-12 13:41:59 -0500 | [diff] [blame] | 2442 | break; |
| 2443 | case SAFI_MPLS_VPN: |
| 2444 | { |
| 2445 | struct attr_extra *attre = attr->extra; |
| 2446 | |
| 2447 | assert (attr->extra); |
| 2448 | if (attre->mp_nexthop_len == 16) { |
| 2449 | stream_putc (s, 24); |
| 2450 | stream_putl (s, 0); /* RD = 0, per RFC */ |
| 2451 | stream_putl (s, 0); |
| 2452 | stream_put (s, &attre->mp_nexthop_global, 16); |
| 2453 | } else if (attre->mp_nexthop_len == 32) { |
| 2454 | stream_putc (s, 48); |
| 2455 | stream_putl (s, 0); /* RD = 0, per RFC */ |
| 2456 | stream_putl (s, 0); |
| 2457 | stream_put (s, &attre->mp_nexthop_global, 16); |
| 2458 | stream_putl (s, 0); /* RD = 0, per RFC */ |
| 2459 | stream_putl (s, 0); |
| 2460 | stream_put (s, &attre->mp_nexthop_local, 16); |
| 2461 | } |
| 2462 | } |
| 2463 | break; |
Lou Berger | 298cc2f | 2016-01-12 13:42:02 -0500 | [diff] [blame] | 2464 | case SAFI_ENCAP: |
| 2465 | assert (attr->extra); |
| 2466 | stream_putc (s, 16); |
| 2467 | stream_put (s, &attr->extra->mp_nexthop_global, 16); |
| 2468 | break; |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2469 | default: |
| 2470 | break; |
| 2471 | } |
| 2472 | break; |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2473 | default: |
| 2474 | break; |
| 2475 | } |
| 2476 | |
| 2477 | /* SNPA */ |
| 2478 | stream_putc (s, 0); |
| 2479 | return sizep; |
| 2480 | } |
| 2481 | |
| 2482 | void |
| 2483 | bgp_packet_mpattr_prefix (struct stream *s, afi_t afi, safi_t safi, |
| 2484 | struct prefix *p, struct prefix_rd *prd, |
| 2485 | u_char *tag) |
| 2486 | { |
Lou Berger | 050defe | 2016-01-12 13:41:59 -0500 | [diff] [blame] | 2487 | if (safi == SAFI_MPLS_VPN) |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2488 | { |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2489 | /* Tag, RD, Prefix write. */ |
| 2490 | stream_putc (s, p->prefixlen + 88); |
| 2491 | stream_put (s, tag, 3); |
| 2492 | stream_put (s, prd->val, 8); |
| 2493 | stream_put (s, &p->u.prefix, PSIZE (p->prefixlen)); |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2494 | } |
Lou Berger | 050defe | 2016-01-12 13:41:59 -0500 | [diff] [blame] | 2495 | else |
| 2496 | stream_put_prefix (s, p); |
| 2497 | } |
| 2498 | |
| 2499 | size_t |
| 2500 | bgp_packet_mpattr_prefix_size (afi_t afi, safi_t safi, struct prefix *p) |
| 2501 | { |
| 2502 | int size = PSIZE (p->prefixlen); |
| 2503 | if (safi == SAFI_MPLS_VPN) |
| 2504 | size += 88; |
| 2505 | return size; |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 2508 | /* |
| 2509 | * Encodes the tunnel encapsulation attribute |
| 2510 | */ |
| 2511 | static void |
| 2512 | bgp_packet_mpattr_tea( |
| 2513 | struct bgp *bgp, |
| 2514 | struct peer *peer, |
| 2515 | struct stream *s, |
| 2516 | struct attr *attr, |
| 2517 | uint8_t attrtype) |
| 2518 | { |
| 2519 | unsigned int attrlenfield = 0; |
Lou Berger | 298cc2f | 2016-01-12 13:42:02 -0500 | [diff] [blame] | 2520 | unsigned int attrhdrlen = 0; |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 2521 | struct bgp_attr_encap_subtlv *subtlvs; |
| 2522 | struct bgp_attr_encap_subtlv *st; |
| 2523 | const char *attrname; |
| 2524 | |
| 2525 | if (!attr || !attr->extra) |
| 2526 | return; |
| 2527 | |
| 2528 | switch (attrtype) { |
| 2529 | case BGP_ATTR_ENCAP: |
| 2530 | attrname = "Tunnel Encap"; |
| 2531 | subtlvs = attr->extra->encap_subtlvs; |
| 2532 | |
| 2533 | /* |
| 2534 | * The tunnel encap attr has an "outer" tlv. |
| 2535 | * T = tunneltype, |
| 2536 | * L = total length of subtlvs, |
| 2537 | * V = concatenated subtlvs. |
| 2538 | */ |
| 2539 | attrlenfield = 2 + 2; /* T + L */ |
Lou Berger | 298cc2f | 2016-01-12 13:42:02 -0500 | [diff] [blame] | 2540 | attrhdrlen = 1 + 1; /* subTLV T + L */ |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 2541 | break; |
| 2542 | |
| 2543 | default: |
| 2544 | assert(0); |
| 2545 | } |
| 2546 | |
| 2547 | |
Lou Berger | 298cc2f | 2016-01-12 13:42:02 -0500 | [diff] [blame] | 2548 | /* if no tlvs, don't make attr */ |
| 2549 | if (subtlvs == NULL) |
| 2550 | return; |
| 2551 | |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 2552 | /* compute attr length */ |
| 2553 | for (st = subtlvs; st; st = st->next) { |
Lou Berger | 298cc2f | 2016-01-12 13:42:02 -0500 | [diff] [blame] | 2554 | attrlenfield += (attrhdrlen + st->length); |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 2555 | } |
| 2556 | |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 2557 | if (attrlenfield > 0xffff) { |
| 2558 | zlog (peer->log, LOG_ERR, |
| 2559 | "%s attribute is too long (length=%d), can't send it", |
| 2560 | attrname, |
| 2561 | attrlenfield); |
| 2562 | return; |
| 2563 | } |
| 2564 | |
| 2565 | if (attrlenfield > 0xff) { |
| 2566 | /* 2-octet length field */ |
| 2567 | stream_putc (s, |
| 2568 | BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN); |
| 2569 | stream_putc (s, attrtype); |
| 2570 | stream_putw (s, attrlenfield & 0xffff); |
| 2571 | } else { |
| 2572 | /* 1-octet length field */ |
| 2573 | stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL); |
| 2574 | stream_putc (s, attrtype); |
| 2575 | stream_putc (s, attrlenfield & 0xff); |
| 2576 | } |
| 2577 | |
| 2578 | if (attrtype == BGP_ATTR_ENCAP) { |
| 2579 | /* write outer T+L */ |
| 2580 | stream_putw(s, attr->extra->encap_tunneltype); |
| 2581 | stream_putw(s, attrlenfield - 4); |
| 2582 | } |
| 2583 | |
| 2584 | /* write each sub-tlv */ |
| 2585 | for (st = subtlvs; st; st = st->next) { |
Lou Berger | 298cc2f | 2016-01-12 13:42:02 -0500 | [diff] [blame] | 2586 | if (attrtype == BGP_ATTR_ENCAP) { |
| 2587 | stream_putc (s, st->type); |
| 2588 | stream_putc (s, st->length); |
| 2589 | } |
Lou Berger | c374178 | 2016-01-12 13:42:01 -0500 | [diff] [blame] | 2590 | stream_put (s, st->value, st->length); |
| 2591 | } |
| 2592 | } |
| 2593 | |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2594 | void |
| 2595 | bgp_packet_mpattr_end (struct stream *s, size_t sizep) |
| 2596 | { |
| 2597 | /* Set MP attribute length. Don't count the (2) bytes used to encode |
| 2598 | the attr length */ |
| 2599 | stream_putw_at (s, sizep, (stream_get_endp (s) - sizep) - 2); |
| 2600 | } |
| 2601 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2602 | /* Make attribute packet. */ |
| 2603 | bgp_size_t |
| 2604 | bgp_packet_attribute (struct bgp *bgp, struct peer *peer, |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2605 | struct stream *s, struct attr *attr, |
| 2606 | struct prefix *p, afi_t afi, safi_t safi, |
| 2607 | struct peer *from, struct prefix_rd *prd, u_char *tag) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2608 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 2609 | size_t cp; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2610 | size_t aspath_sizep; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2611 | struct aspath *aspath; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2612 | int send_as4_path = 0; |
| 2613 | int send_as4_aggregator = 0; |
| 2614 | int use32bit = (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)) ? 1 : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2615 | |
| 2616 | if (! bgp) |
| 2617 | bgp = bgp_get_default (); |
| 2618 | |
| 2619 | /* Remember current pointer. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2620 | cp = stream_get_endp (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2621 | |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2622 | if (p && !(afi == AFI_IP && safi == SAFI_UNICAST)) |
| 2623 | { |
Lou Berger | 050defe | 2016-01-12 13:41:59 -0500 | [diff] [blame] | 2624 | size_t mpattrlen_pos = 0; |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2625 | mpattrlen_pos = bgp_packet_mpattr_start(s, afi, safi, attr); |
| 2626 | bgp_packet_mpattr_prefix(s, afi, safi, p, prd, tag); |
| 2627 | bgp_packet_mpattr_end(s, mpattrlen_pos); |
| 2628 | } |
| 2629 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2630 | /* Origin attribute. */ |
| 2631 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 2632 | stream_putc (s, BGP_ATTR_ORIGIN); |
| 2633 | stream_putc (s, 1); |
| 2634 | stream_putc (s, attr->origin); |
| 2635 | |
| 2636 | /* AS path attribute. */ |
| 2637 | |
| 2638 | /* If remote-peer is EBGP */ |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 2639 | if (peer->sort == BGP_PEER_EBGP |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2640 | && (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_AS_PATH_UNCHANGED) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 2641 | || attr->aspath->segments == NULL) |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 2642 | && (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2643 | { |
| 2644 | aspath = aspath_dup (attr->aspath); |
| 2645 | |
| 2646 | if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) |
| 2647 | { |
| 2648 | /* Strip the confed info, and then stuff our path CONFED_ID |
| 2649 | on the front */ |
| 2650 | aspath = aspath_delete_confed_seq (aspath); |
| 2651 | aspath = aspath_add_seq (aspath, bgp->confed_id); |
| 2652 | } |
| 2653 | else |
| 2654 | { |
Andrew Certain | 9d3f970 | 2012-11-07 23:50:07 +0000 | [diff] [blame] | 2655 | if (peer->change_local_as) { |
| 2656 | /* If replace-as is specified, we only use the change_local_as when |
| 2657 | advertising routes. */ |
| 2658 | if( ! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS) ) { |
| 2659 | aspath = aspath_add_seq (aspath, peer->local_as); |
| 2660 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2661 | aspath = aspath_add_seq (aspath, peer->change_local_as); |
Andrew Certain | 9d3f970 | 2012-11-07 23:50:07 +0000 | [diff] [blame] | 2662 | } else { |
| 2663 | aspath = aspath_add_seq (aspath, peer->local_as); |
| 2664 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2665 | } |
| 2666 | } |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 2667 | else if (peer->sort == BGP_PEER_CONFED) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2668 | { |
| 2669 | /* A confed member, so we need to do the AS_CONFED_SEQUENCE thing */ |
| 2670 | aspath = aspath_dup (attr->aspath); |
| 2671 | aspath = aspath_add_confed_seq (aspath, peer->local_as); |
| 2672 | } |
| 2673 | else |
| 2674 | aspath = attr->aspath; |
| 2675 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2676 | /* If peer is not AS4 capable, then: |
| 2677 | * - send the created AS_PATH out as AS4_PATH (optional, transitive), |
| 2678 | * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path segment |
| 2679 | * types are in it (i.e. exclude them if they are there) |
| 2680 | * AND do this only if there is at least one asnum > 65535 in the path! |
| 2681 | * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and change |
| 2682 | * all ASnums > 65535 to BGP_AS_TRANS |
| 2683 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2684 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2685 | stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN); |
| 2686 | stream_putc (s, BGP_ATTR_AS_PATH); |
| 2687 | aspath_sizep = stream_get_endp (s); |
| 2688 | stream_putw (s, 0); |
| 2689 | stream_putw_at (s, aspath_sizep, aspath_put (s, aspath, use32bit)); |
| 2690 | |
| 2691 | /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs |
| 2692 | * in the path |
| 2693 | */ |
| 2694 | if (!use32bit && aspath_has_as4 (aspath)) |
| 2695 | send_as4_path = 1; /* we'll do this later, at the correct place */ |
| 2696 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2697 | /* Nexthop attribute. */ |
Lou Berger | 050defe | 2016-01-12 13:41:59 -0500 | [diff] [blame] | 2698 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP) && afi == AFI_IP && |
| 2699 | safi == SAFI_UNICAST) /* only write NH attr for unicast safi */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2700 | { |
| 2701 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 2702 | stream_putc (s, BGP_ATTR_NEXT_HOP); |
| 2703 | stream_putc (s, 4); |
| 2704 | if (safi == SAFI_MPLS_VPN) |
| 2705 | { |
| 2706 | if (attr->nexthop.s_addr == 0) |
| 2707 | stream_put_ipv4 (s, peer->nexthop.v4.s_addr); |
| 2708 | else |
| 2709 | stream_put_ipv4 (s, attr->nexthop.s_addr); |
| 2710 | } |
| 2711 | else |
| 2712 | stream_put_ipv4 (s, attr->nexthop.s_addr); |
| 2713 | } |
| 2714 | |
| 2715 | /* MED attribute. */ |
| 2716 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC)) |
| 2717 | { |
| 2718 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); |
| 2719 | stream_putc (s, BGP_ATTR_MULTI_EXIT_DISC); |
| 2720 | stream_putc (s, 4); |
| 2721 | stream_putl (s, attr->med); |
| 2722 | } |
| 2723 | |
| 2724 | /* Local preference. */ |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 2725 | if (peer->sort == BGP_PEER_IBGP || |
| 2726 | peer->sort == BGP_PEER_CONFED) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2727 | { |
| 2728 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 2729 | stream_putc (s, BGP_ATTR_LOCAL_PREF); |
| 2730 | stream_putc (s, 4); |
| 2731 | stream_putl (s, attr->local_pref); |
| 2732 | } |
| 2733 | |
| 2734 | /* Atomic aggregate. */ |
| 2735 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE)) |
| 2736 | { |
| 2737 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 2738 | stream_putc (s, BGP_ATTR_ATOMIC_AGGREGATE); |
| 2739 | stream_putc (s, 0); |
| 2740 | } |
| 2741 | |
| 2742 | /* Aggregator. */ |
| 2743 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR)) |
| 2744 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2745 | assert (attr->extra); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2746 | |
| 2747 | /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2748 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 2749 | stream_putc (s, BGP_ATTR_AGGREGATOR); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2750 | |
| 2751 | if (use32bit) |
| 2752 | { |
| 2753 | /* AS4 capable peer */ |
| 2754 | stream_putc (s, 8); |
| 2755 | stream_putl (s, attr->extra->aggregator_as); |
| 2756 | } |
| 2757 | else |
| 2758 | { |
| 2759 | /* 2-byte AS peer */ |
| 2760 | stream_putc (s, 6); |
| 2761 | |
| 2762 | /* Is ASN representable in 2-bytes? Or must AS_TRANS be used? */ |
| 2763 | if ( attr->extra->aggregator_as > 65535 ) |
| 2764 | { |
| 2765 | stream_putw (s, BGP_AS_TRANS); |
| 2766 | |
| 2767 | /* we have to send AS4_AGGREGATOR, too. |
| 2768 | * we'll do that later in order to send attributes in ascending |
| 2769 | * order. |
| 2770 | */ |
| 2771 | send_as4_aggregator = 1; |
| 2772 | } |
| 2773 | else |
| 2774 | stream_putw (s, (u_int16_t) attr->extra->aggregator_as); |
| 2775 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2776 | stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | /* Community attribute. */ |
| 2780 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY) |
| 2781 | && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES))) |
| 2782 | { |
| 2783 | if (attr->community->size * 4 > 255) |
| 2784 | { |
| 2785 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN); |
| 2786 | stream_putc (s, BGP_ATTR_COMMUNITIES); |
| 2787 | stream_putw (s, attr->community->size * 4); |
| 2788 | } |
| 2789 | else |
| 2790 | { |
| 2791 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 2792 | stream_putc (s, BGP_ATTR_COMMUNITIES); |
| 2793 | stream_putc (s, attr->community->size * 4); |
| 2794 | } |
| 2795 | stream_put (s, attr->community->val, attr->community->size * 4); |
| 2796 | } |
| 2797 | |
| 2798 | /* Route Reflector. */ |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 2799 | if (peer->sort == BGP_PEER_IBGP |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2800 | && from |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 2801 | && from->sort == BGP_PEER_IBGP) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2802 | { |
| 2803 | /* Originator ID. */ |
| 2804 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); |
| 2805 | stream_putc (s, BGP_ATTR_ORIGINATOR_ID); |
| 2806 | stream_putc (s, 4); |
| 2807 | |
| 2808 | if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2809 | stream_put_in_addr (s, &attr->extra->originator_id); |
Paul Jakma | 34c3f81 | 2006-05-12 23:25:37 +0000 | [diff] [blame] | 2810 | else |
| 2811 | stream_put_in_addr (s, &from->remote_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2812 | |
| 2813 | /* Cluster list. */ |
| 2814 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); |
| 2815 | stream_putc (s, BGP_ATTR_CLUSTER_LIST); |
| 2816 | |
Paul Jakma | 9eda90c | 2007-08-30 13:36:17 +0000 | [diff] [blame] | 2817 | if (attr->extra && attr->extra->cluster) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2818 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2819 | stream_putc (s, attr->extra->cluster->length + 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2820 | /* If this peer configuration's parent BGP has cluster_id. */ |
| 2821 | if (bgp->config & BGP_CONFIG_CLUSTER_ID) |
| 2822 | stream_put_in_addr (s, &bgp->cluster_id); |
| 2823 | else |
| 2824 | stream_put_in_addr (s, &bgp->router_id); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2825 | stream_put (s, attr->extra->cluster->list, |
| 2826 | attr->extra->cluster->length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2827 | } |
| 2828 | else |
| 2829 | { |
| 2830 | stream_putc (s, 4); |
| 2831 | /* If this peer configuration's parent BGP has cluster_id. */ |
| 2832 | if (bgp->config & BGP_CONFIG_CLUSTER_ID) |
| 2833 | stream_put_in_addr (s, &bgp->cluster_id); |
| 2834 | else |
| 2835 | stream_put_in_addr (s, &bgp->router_id); |
| 2836 | } |
| 2837 | } |
| 2838 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2839 | /* Extended Communities attribute. */ |
| 2840 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY) |
| 2841 | && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES))) |
| 2842 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2843 | struct attr_extra *attre = attr->extra; |
| 2844 | |
| 2845 | assert (attre); |
| 2846 | |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 2847 | if (peer->sort == BGP_PEER_IBGP |
| 2848 | || peer->sort == BGP_PEER_CONFED) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2849 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2850 | if (attre->ecommunity->size * 8 > 255) |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2851 | { |
| 2852 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN); |
| 2853 | stream_putc (s, BGP_ATTR_EXT_COMMUNITIES); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2854 | stream_putw (s, attre->ecommunity->size * 8); |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2855 | } |
| 2856 | else |
| 2857 | { |
| 2858 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 2859 | stream_putc (s, BGP_ATTR_EXT_COMMUNITIES); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2860 | stream_putc (s, attre->ecommunity->size * 8); |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2861 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2862 | stream_put (s, attre->ecommunity->val, attre->ecommunity->size * 8); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2863 | } |
| 2864 | else |
| 2865 | { |
paul | 5228ad2 | 2004-06-04 17:58:18 +0000 | [diff] [blame] | 2866 | u_int8_t *pnt; |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2867 | int tbit; |
| 2868 | int ecom_tr_size = 0; |
| 2869 | int i; |
| 2870 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2871 | for (i = 0; i < attre->ecommunity->size; i++) |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2872 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2873 | pnt = attre->ecommunity->val + (i * 8); |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2874 | tbit = *pnt; |
| 2875 | |
| 2876 | if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE)) |
| 2877 | continue; |
| 2878 | |
| 2879 | ecom_tr_size++; |
| 2880 | } |
| 2881 | |
| 2882 | if (ecom_tr_size) |
| 2883 | { |
| 2884 | if (ecom_tr_size * 8 > 255) |
| 2885 | { |
| 2886 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN); |
| 2887 | stream_putc (s, BGP_ATTR_EXT_COMMUNITIES); |
| 2888 | stream_putw (s, ecom_tr_size * 8); |
| 2889 | } |
| 2890 | else |
| 2891 | { |
| 2892 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 2893 | stream_putc (s, BGP_ATTR_EXT_COMMUNITIES); |
| 2894 | stream_putc (s, ecom_tr_size * 8); |
| 2895 | } |
| 2896 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2897 | for (i = 0; i < attre->ecommunity->size; i++) |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2898 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2899 | pnt = attre->ecommunity->val + (i * 8); |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2900 | tbit = *pnt; |
| 2901 | |
| 2902 | if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE)) |
| 2903 | continue; |
| 2904 | |
| 2905 | stream_put (s, pnt, 8); |
| 2906 | } |
| 2907 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2908 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2909 | } |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2910 | |
| 2911 | if ( send_as4_path ) |
| 2912 | { |
| 2913 | /* If the peer is NOT As4 capable, AND */ |
| 2914 | /* there are ASnums > 65535 in path THEN |
| 2915 | * give out AS4_PATH */ |
| 2916 | |
| 2917 | /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET |
| 2918 | * path segments! |
| 2919 | * Hm, I wonder... confederation things *should* only be at |
| 2920 | * the beginning of an aspath, right? Then we should use |
| 2921 | * aspath_delete_confed_seq for this, because it is already |
| 2922 | * there! (JK) |
| 2923 | * Folks, talk to me: what is reasonable here!? |
| 2924 | */ |
| 2925 | aspath = aspath_delete_confed_seq (aspath); |
| 2926 | |
| 2927 | stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN); |
| 2928 | stream_putc (s, BGP_ATTR_AS4_PATH); |
| 2929 | aspath_sizep = stream_get_endp (s); |
| 2930 | stream_putw (s, 0); |
| 2931 | stream_putw_at (s, aspath_sizep, aspath_put (s, aspath, 1)); |
| 2932 | } |
| 2933 | |
| 2934 | if (aspath != attr->aspath) |
| 2935 | aspath_free (aspath); |
| 2936 | |
| 2937 | if ( send_as4_aggregator ) |
| 2938 | { |
| 2939 | assert (attr->extra); |
| 2940 | |
| 2941 | /* send AS4_AGGREGATOR, at this place */ |
| 2942 | /* this section of code moved here in order to ensure the correct |
| 2943 | * *ascending* order of attributes |
| 2944 | */ |
| 2945 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 2946 | stream_putc (s, BGP_ATTR_AS4_AGGREGATOR); |
| 2947 | stream_putc (s, 8); |
| 2948 | stream_putl (s, attr->extra->aggregator_as); |
| 2949 | stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr); |
| 2950 | } |
Lou Berger | 298cc2f | 2016-01-12 13:42:02 -0500 | [diff] [blame] | 2951 | |
| 2952 | if ((afi == AFI_IP || afi == AFI_IP6) && |
| 2953 | (safi == SAFI_ENCAP || safi == SAFI_MPLS_VPN)) |
| 2954 | { |
| 2955 | /* Tunnel Encap attribute */ |
| 2956 | bgp_packet_mpattr_tea(bgp, peer, s, attr, BGP_ATTR_ENCAP); |
| 2957 | } |
| 2958 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2959 | /* Unknown transit attribute. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2960 | if (attr->extra && attr->extra->transit) |
| 2961 | stream_put (s, attr->extra->transit->val, attr->extra->transit->length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2962 | |
| 2963 | /* Return total size of attribute. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2964 | return stream_get_endp (s) - cp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2967 | size_t |
| 2968 | bgp_packet_mpunreach_start (struct stream *s, afi_t afi, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2969 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2970 | unsigned long attrlen_pnt; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2971 | |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2972 | /* Set extended bit always to encode the attribute length as 2 bytes */ |
| 2973 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2974 | stream_putc (s, BGP_ATTR_MP_UNREACH_NLRI); |
| 2975 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2976 | attrlen_pnt = stream_get_endp (s); |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2977 | stream_putw (s, 0); /* Length of this attribute. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2978 | |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2979 | stream_putw (s, afi); |
Lou Berger | 050defe | 2016-01-12 13:41:59 -0500 | [diff] [blame] | 2980 | stream_putc (s, (safi == SAFI_MPLS_VPN) ? SAFI_MPLS_LABELED_VPN : safi); |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2981 | return attrlen_pnt; |
| 2982 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2983 | |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2984 | void |
| 2985 | bgp_packet_mpunreach_prefix (struct stream *s, struct prefix *p, |
| 2986 | afi_t afi, safi_t safi, struct prefix_rd *prd, |
| 2987 | u_char *tag) |
| 2988 | { |
Lou Berger | 050defe | 2016-01-12 13:41:59 -0500 | [diff] [blame] | 2989 | bgp_packet_mpattr_prefix (s, afi, safi, p, prd, tag); |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2990 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2991 | |
Pradosh Mohapatra | 8c71e48 | 2014-01-15 06:57:57 +0000 | [diff] [blame] | 2992 | void |
| 2993 | bgp_packet_mpunreach_end (struct stream *s, size_t attrlen_pnt) |
| 2994 | { |
Lou Berger | 050defe | 2016-01-12 13:41:59 -0500 | [diff] [blame] | 2995 | bgp_packet_mpattr_end (s, attrlen_pnt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
| 2998 | /* Initialization of attribute. */ |
| 2999 | void |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 3000 | bgp_attr_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3001 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3002 | aspath_init (); |
| 3003 | attrhash_init (); |
| 3004 | community_init (); |
| 3005 | ecommunity_init (); |
| 3006 | cluster_init (); |
| 3007 | transit_init (); |
| 3008 | } |
| 3009 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 3010 | void |
| 3011 | bgp_attr_finish (void) |
| 3012 | { |
| 3013 | aspath_finish (); |
| 3014 | attrhash_finish (); |
| 3015 | community_finish (); |
| 3016 | ecommunity_finish (); |
| 3017 | cluster_finish (); |
| 3018 | transit_finish (); |
| 3019 | } |
| 3020 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3021 | /* Make attribute packet. */ |
| 3022 | void |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 3023 | bgp_dump_routes_attr (struct stream *s, struct attr *attr, |
| 3024 | struct prefix *prefix) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3025 | { |
| 3026 | unsigned long cp; |
| 3027 | unsigned long len; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 3028 | size_t aspath_lenp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3029 | struct aspath *aspath; |
| 3030 | |
| 3031 | /* Remember current pointer. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 3032 | cp = stream_get_endp (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3033 | |
| 3034 | /* Place holder of length. */ |
| 3035 | stream_putw (s, 0); |
| 3036 | |
| 3037 | /* Origin attribute. */ |
| 3038 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 3039 | stream_putc (s, BGP_ATTR_ORIGIN); |
| 3040 | stream_putc (s, 1); |
| 3041 | stream_putc (s, attr->origin); |
| 3042 | |
| 3043 | aspath = attr->aspath; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 3044 | |
| 3045 | stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN); |
| 3046 | stream_putc (s, BGP_ATTR_AS_PATH); |
| 3047 | aspath_lenp = stream_get_endp (s); |
| 3048 | stream_putw (s, 0); |
| 3049 | |
| 3050 | stream_putw_at (s, aspath_lenp, aspath_put (s, aspath, 1)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3051 | |
| 3052 | /* Nexthop attribute. */ |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 3053 | /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */ |
| 3054 | if(prefix != NULL |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 3055 | && prefix->family != AF_INET6 |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 3056 | ) |
| 3057 | { |
| 3058 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 3059 | stream_putc (s, BGP_ATTR_NEXT_HOP); |
| 3060 | stream_putc (s, 4); |
| 3061 | stream_put_ipv4 (s, attr->nexthop.s_addr); |
| 3062 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3063 | |
| 3064 | /* MED attribute. */ |
| 3065 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC)) |
| 3066 | { |
| 3067 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); |
| 3068 | stream_putc (s, BGP_ATTR_MULTI_EXIT_DISC); |
| 3069 | stream_putc (s, 4); |
| 3070 | stream_putl (s, attr->med); |
| 3071 | } |
| 3072 | |
| 3073 | /* Local preference. */ |
| 3074 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF)) |
| 3075 | { |
| 3076 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 3077 | stream_putc (s, BGP_ATTR_LOCAL_PREF); |
| 3078 | stream_putc (s, 4); |
| 3079 | stream_putl (s, attr->local_pref); |
| 3080 | } |
| 3081 | |
| 3082 | /* Atomic aggregate. */ |
| 3083 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE)) |
| 3084 | { |
| 3085 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 3086 | stream_putc (s, BGP_ATTR_ATOMIC_AGGREGATE); |
| 3087 | stream_putc (s, 0); |
| 3088 | } |
| 3089 | |
| 3090 | /* Aggregator. */ |
| 3091 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR)) |
| 3092 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 3093 | assert (attr->extra); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3094 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 3095 | stream_putc (s, BGP_ATTR_AGGREGATOR); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 3096 | stream_putc (s, 8); |
| 3097 | stream_putl (s, attr->extra->aggregator_as); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 3098 | stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | /* Community attribute. */ |
| 3102 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES)) |
| 3103 | { |
| 3104 | if (attr->community->size * 4 > 255) |
| 3105 | { |
| 3106 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN); |
| 3107 | stream_putc (s, BGP_ATTR_COMMUNITIES); |
| 3108 | stream_putw (s, attr->community->size * 4); |
| 3109 | } |
| 3110 | else |
| 3111 | { |
| 3112 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 3113 | stream_putc (s, BGP_ATTR_COMMUNITIES); |
| 3114 | stream_putc (s, attr->community->size * 4); |
| 3115 | } |
| 3116 | stream_put (s, attr->community->val, attr->community->size * 4); |
| 3117 | } |
| 3118 | |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 3119 | /* Add a MP_NLRI attribute to dump the IPv6 next hop */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 3120 | if (prefix != NULL && prefix->family == AF_INET6 && attr->extra && |
| 3121 | (attr->extra->mp_nexthop_len == 16 || attr->extra->mp_nexthop_len == 32) ) |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 3122 | { |
| 3123 | int sizep; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 3124 | struct attr_extra *attre = attr->extra; |
| 3125 | |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 3126 | stream_putc(s, BGP_ATTR_FLAG_OPTIONAL); |
| 3127 | stream_putc(s, BGP_ATTR_MP_REACH_NLRI); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 3128 | sizep = stream_get_endp (s); |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 3129 | |
| 3130 | /* MP header */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 3131 | stream_putc (s, 0); /* Marker: Attribute length. */ |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 3132 | stream_putw(s, AFI_IP6); /* AFI */ |
| 3133 | stream_putc(s, SAFI_UNICAST); /* SAFI */ |
| 3134 | |
| 3135 | /* Next hop */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 3136 | stream_putc(s, attre->mp_nexthop_len); |
| 3137 | stream_put(s, &attre->mp_nexthop_global, 16); |
| 3138 | if (attre->mp_nexthop_len == 32) |
| 3139 | stream_put(s, &attre->mp_nexthop_local, 16); |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 3140 | |
| 3141 | /* SNPA */ |
| 3142 | stream_putc(s, 0); |
| 3143 | |
| 3144 | /* Prefix */ |
| 3145 | stream_put_prefix(s, prefix); |
| 3146 | |
| 3147 | /* Set MP attribute length. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 3148 | stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1); |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 3149 | } |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 3150 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3151 | /* Return total size of attribute. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 3152 | len = stream_get_endp (s) - cp - 2; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3153 | stream_putw_at (s, cp, len); |
| 3154 | } |