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" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 32 | |
| 33 | #include "bgpd/bgpd.h" |
| 34 | #include "bgpd/bgp_attr.h" |
| 35 | #include "bgpd/bgp_route.h" |
| 36 | #include "bgpd/bgp_aspath.h" |
| 37 | #include "bgpd/bgp_community.h" |
| 38 | #include "bgpd/bgp_debug.h" |
| 39 | #include "bgpd/bgp_packet.h" |
| 40 | #include "bgpd/bgp_ecommunity.h" |
| 41 | |
| 42 | /* Attribute strings for logging. */ |
Stephen Hemminger | 9bddac4 | 2009-05-15 09:59:51 -0700 | [diff] [blame] | 43 | static const struct message attr_str [] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 44 | { |
| 45 | { BGP_ATTR_ORIGIN, "ORIGIN" }, |
| 46 | { BGP_ATTR_AS_PATH, "AS_PATH" }, |
| 47 | { BGP_ATTR_NEXT_HOP, "NEXT_HOP" }, |
| 48 | { BGP_ATTR_MULTI_EXIT_DISC, "MULTI_EXIT_DISC" }, |
| 49 | { BGP_ATTR_LOCAL_PREF, "LOCAL_PREF" }, |
| 50 | { BGP_ATTR_ATOMIC_AGGREGATE, "ATOMIC_AGGREGATE" }, |
| 51 | { BGP_ATTR_AGGREGATOR, "AGGREGATOR" }, |
| 52 | { BGP_ATTR_COMMUNITIES, "COMMUNITY" }, |
| 53 | { BGP_ATTR_ORIGINATOR_ID, "ORIGINATOR_ID" }, |
| 54 | { BGP_ATTR_CLUSTER_LIST, "CLUSTERLIST" }, |
| 55 | { BGP_ATTR_DPA, "DPA" }, |
| 56 | { BGP_ATTR_ADVERTISER, "ADVERTISER"} , |
| 57 | { BGP_ATTR_RCID_PATH, "RCID_PATH" }, |
| 58 | { BGP_ATTR_MP_REACH_NLRI, "MP_REACH_NLRI" }, |
| 59 | { BGP_ATTR_MP_UNREACH_NLRI, "MP_UNREACH_NLRI" }, |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 60 | { BGP_ATTR_EXT_COMMUNITIES, "EXT_COMMUNITIES" }, |
| 61 | { BGP_ATTR_AS4_PATH, "AS4_PATH" }, |
| 62 | { BGP_ATTR_AS4_AGGREGATOR, "AS4_AGGREGATOR" }, |
| 63 | { BGP_ATTR_AS_PATHLIMIT, "AS_PATHLIMIT" }, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 64 | }; |
Stephen Hemminger | 9bddac4 | 2009-05-15 09:59:51 -0700 | [diff] [blame] | 65 | static const int attr_str_max = sizeof(attr_str)/sizeof(attr_str[0]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 66 | |
Stephen Hemminger | 9bddac4 | 2009-05-15 09:59:51 -0700 | [diff] [blame] | 67 | static struct hash *cluster_hash; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 69 | static void * |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 70 | cluster_hash_alloc (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 71 | { |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 72 | struct cluster_list * val = (struct cluster_list *) p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 73 | struct cluster_list *cluster; |
| 74 | |
| 75 | cluster = XMALLOC (MTYPE_CLUSTER, sizeof (struct cluster_list)); |
| 76 | cluster->length = val->length; |
| 77 | |
| 78 | if (cluster->length) |
| 79 | { |
| 80 | cluster->list = XMALLOC (MTYPE_CLUSTER_VAL, val->length); |
| 81 | memcpy (cluster->list, val->list, val->length); |
| 82 | } |
| 83 | else |
| 84 | cluster->list = NULL; |
| 85 | |
| 86 | cluster->refcnt = 0; |
| 87 | |
| 88 | return cluster; |
| 89 | } |
| 90 | |
| 91 | /* Cluster list related functions. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 92 | static struct cluster_list * |
paul | 5228ad2 | 2004-06-04 17:58:18 +0000 | [diff] [blame] | 93 | cluster_parse (struct in_addr * pnt, int length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 94 | { |
| 95 | struct cluster_list tmp; |
| 96 | struct cluster_list *cluster; |
| 97 | |
| 98 | tmp.length = length; |
paul | 5228ad2 | 2004-06-04 17:58:18 +0000 | [diff] [blame] | 99 | tmp.list = pnt; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 100 | |
| 101 | cluster = hash_get (cluster_hash, &tmp, cluster_hash_alloc); |
| 102 | cluster->refcnt++; |
| 103 | return cluster; |
| 104 | } |
| 105 | |
| 106 | int |
| 107 | cluster_loop_check (struct cluster_list *cluster, struct in_addr originator) |
| 108 | { |
| 109 | int i; |
| 110 | |
| 111 | for (i = 0; i < cluster->length / 4; i++) |
| 112 | if (cluster->list[i].s_addr == originator.s_addr) |
| 113 | return 1; |
| 114 | return 0; |
| 115 | } |
| 116 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 117 | static unsigned int |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 118 | cluster_hash_key_make (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | { |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 120 | const struct cluster_list *cluster = p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 121 | |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 122 | return jhash(cluster->list, cluster->length, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 123 | } |
| 124 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 125 | static int |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 126 | cluster_hash_cmp (const void *p1, const void *p2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | { |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 128 | const struct cluster_list * cluster1 = p1; |
| 129 | const struct cluster_list * cluster2 = p2; |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 130 | |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 131 | return (cluster1->length == cluster2->length && |
| 132 | memcmp (cluster1->list, cluster2->list, cluster1->length) == 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 133 | } |
| 134 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 135 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | cluster_free (struct cluster_list *cluster) |
| 137 | { |
| 138 | if (cluster->list) |
| 139 | XFREE (MTYPE_CLUSTER_VAL, cluster->list); |
| 140 | XFREE (MTYPE_CLUSTER, cluster); |
| 141 | } |
| 142 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 143 | #if 0 |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 144 | static struct cluster_list * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 145 | cluster_dup (struct cluster_list *cluster) |
| 146 | { |
| 147 | struct cluster_list *new; |
| 148 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 149 | new = XCALLOC (MTYPE_CLUSTER, sizeof (struct cluster_list)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 150 | new->length = cluster->length; |
| 151 | |
| 152 | if (cluster->length) |
| 153 | { |
| 154 | new->list = XMALLOC (MTYPE_CLUSTER_VAL, cluster->length); |
| 155 | memcpy (new->list, cluster->list, cluster->length); |
| 156 | } |
| 157 | else |
| 158 | new->list = NULL; |
| 159 | |
| 160 | return new; |
| 161 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 162 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 164 | static struct cluster_list * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 165 | cluster_intern (struct cluster_list *cluster) |
| 166 | { |
| 167 | struct cluster_list *find; |
| 168 | |
| 169 | find = hash_get (cluster_hash, cluster, cluster_hash_alloc); |
| 170 | find->refcnt++; |
| 171 | |
| 172 | return find; |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | cluster_unintern (struct cluster_list *cluster) |
| 177 | { |
| 178 | struct cluster_list *ret; |
| 179 | |
| 180 | if (cluster->refcnt) |
| 181 | cluster->refcnt--; |
| 182 | |
| 183 | if (cluster->refcnt == 0) |
| 184 | { |
| 185 | ret = hash_release (cluster_hash, cluster); |
| 186 | cluster_free (cluster); |
| 187 | } |
| 188 | } |
| 189 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 190 | static void |
| 191 | cluster_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 192 | { |
| 193 | cluster_hash = hash_create (cluster_hash_key_make, cluster_hash_cmp); |
| 194 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 195 | |
| 196 | static void |
| 197 | cluster_finish (void) |
| 198 | { |
| 199 | hash_free (cluster_hash); |
| 200 | cluster_hash = NULL; |
| 201 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 202 | |
| 203 | /* Unknown transit attribute. */ |
Stephen Hemminger | 9bddac4 | 2009-05-15 09:59:51 -0700 | [diff] [blame] | 204 | static struct hash *transit_hash; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 205 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 206 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 207 | transit_free (struct transit *transit) |
| 208 | { |
| 209 | if (transit->val) |
| 210 | XFREE (MTYPE_TRANSIT_VAL, transit->val); |
| 211 | XFREE (MTYPE_TRANSIT, transit); |
| 212 | } |
| 213 | |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 214 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 215 | static void * |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 216 | transit_hash_alloc (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 217 | { |
| 218 | /* Transit structure is already allocated. */ |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 219 | return p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 220 | } |
| 221 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 222 | static struct transit * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 223 | transit_intern (struct transit *transit) |
| 224 | { |
| 225 | struct transit *find; |
| 226 | |
| 227 | find = hash_get (transit_hash, transit, transit_hash_alloc); |
| 228 | if (find != transit) |
| 229 | transit_free (transit); |
| 230 | find->refcnt++; |
| 231 | |
| 232 | return find; |
| 233 | } |
| 234 | |
| 235 | void |
| 236 | transit_unintern (struct transit *transit) |
| 237 | { |
| 238 | struct transit *ret; |
| 239 | |
| 240 | if (transit->refcnt) |
| 241 | transit->refcnt--; |
| 242 | |
| 243 | if (transit->refcnt == 0) |
| 244 | { |
| 245 | ret = hash_release (transit_hash, transit); |
| 246 | transit_free (transit); |
| 247 | } |
| 248 | } |
| 249 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 250 | static unsigned int |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 251 | transit_hash_key_make (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 252 | { |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 253 | const struct transit * transit = p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 254 | |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 255 | return jhash(transit->val, transit->length, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 256 | } |
| 257 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 258 | static int |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 259 | transit_hash_cmp (const void *p1, const void *p2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 260 | { |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 261 | const struct transit * transit1 = p1; |
| 262 | const struct transit * transit2 = p2; |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 263 | |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 264 | return (transit1->length == transit2->length && |
| 265 | memcmp (transit1->val, transit2->val, transit1->length) == 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 266 | } |
| 267 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 268 | static void |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 269 | transit_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 270 | { |
| 271 | transit_hash = hash_create (transit_hash_key_make, transit_hash_cmp); |
| 272 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 273 | |
| 274 | static void |
| 275 | transit_finish (void) |
| 276 | { |
| 277 | hash_free (transit_hash); |
| 278 | transit_hash = NULL; |
| 279 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 280 | |
| 281 | /* Attribute hash routines. */ |
Stephen Hemminger | 9bddac4 | 2009-05-15 09:59:51 -0700 | [diff] [blame] | 282 | static struct hash *attrhash; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 283 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 284 | static struct attr_extra * |
| 285 | bgp_attr_extra_new (void) |
| 286 | { |
| 287 | return XCALLOC (MTYPE_ATTR_EXTRA, sizeof (struct attr_extra)); |
| 288 | } |
| 289 | |
| 290 | void |
| 291 | bgp_attr_extra_free (struct attr *attr) |
| 292 | { |
| 293 | if (attr->extra) |
| 294 | { |
| 295 | XFREE (MTYPE_ATTR_EXTRA, attr->extra); |
| 296 | attr->extra = NULL; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | struct attr_extra * |
| 301 | bgp_attr_extra_get (struct attr *attr) |
| 302 | { |
| 303 | if (!attr->extra) |
| 304 | attr->extra = bgp_attr_extra_new(); |
| 305 | return attr->extra; |
| 306 | } |
| 307 | |
| 308 | /* Shallow copy of an attribute |
| 309 | * Though, not so shallow that it doesn't copy the contents |
| 310 | * of the attr_extra pointed to by 'extra' |
| 311 | */ |
| 312 | void |
| 313 | bgp_attr_dup (struct attr *new, struct attr *orig) |
| 314 | { |
| 315 | *new = *orig; |
| 316 | if (orig->extra) |
| 317 | { |
| 318 | new->extra = bgp_attr_extra_new(); |
| 319 | *new->extra = *orig->extra; |
| 320 | } |
| 321 | } |
| 322 | |
Paul Jakma | cbdfbaa | 2006-03-30 13:20:48 +0000 | [diff] [blame] | 323 | unsigned long int |
| 324 | attr_count (void) |
| 325 | { |
| 326 | return attrhash->count; |
| 327 | } |
| 328 | |
| 329 | unsigned long int |
| 330 | attr_unknown_count (void) |
| 331 | { |
| 332 | return transit_hash->count; |
| 333 | } |
| 334 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 335 | unsigned int |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 336 | attrhash_key_make (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 337 | { |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 338 | const struct attr * attr = (struct attr *) p; |
| 339 | uint32_t key = 0; |
| 340 | #define MIX(val) key = jhash_1word(val, key) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 341 | |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 342 | MIX(attr->origin); |
| 343 | MIX(attr->nexthop.s_addr); |
| 344 | MIX(attr->med); |
| 345 | MIX(attr->local_pref); |
| 346 | |
Paul Jakma | c8f3fe3 | 2010-12-05 20:28:02 +0000 | [diff] [blame] | 347 | key += attr->origin; |
| 348 | key += attr->nexthop.s_addr; |
| 349 | key += attr->med; |
| 350 | key += attr->local_pref; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 351 | |
| 352 | if (attr->extra) |
| 353 | { |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 354 | MIX(attr->extra->aggregator_as); |
| 355 | MIX(attr->extra->aggregator_addr.s_addr); |
| 356 | MIX(attr->extra->weight); |
| 357 | MIX(attr->extra->mp_nexthop_global_in.s_addr); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 358 | } |
| 359 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 360 | if (attr->aspath) |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 361 | MIX(aspath_key_make (attr->aspath)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 362 | if (attr->community) |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 363 | MIX(community_hash_make (attr->community)); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 364 | |
| 365 | if (attr->extra) |
| 366 | { |
| 367 | if (attr->extra->ecommunity) |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 368 | MIX(ecommunity_hash_make (attr->extra->ecommunity)); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 369 | if (attr->extra->cluster) |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 370 | MIX(cluster_hash_key_make (attr->extra->cluster)); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 371 | if (attr->extra->transit) |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 372 | MIX(transit_hash_key_make (attr->extra->transit)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 373 | |
| 374 | #ifdef HAVE_IPV6 |
Stephen Hemminger | c8e7b89 | 2010-08-27 14:12:54 -0700 | [diff] [blame] | 375 | MIX(attr->extra->mp_nexthop_len); |
| 376 | key = jhash2(attr->extra->mp_nexthop_global.s6_addr32, 4, key); |
| 377 | key = jhash2(attr->extra->mp_nexthop_local.s6_addr32, 4, key); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | #endif /* HAVE_IPV6 */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 379 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 380 | |
| 381 | return key; |
| 382 | } |
| 383 | |
| 384 | int |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 385 | attrhash_cmp (const void *p1, const void *p2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 386 | { |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 387 | const struct attr * attr1 = p1; |
| 388 | const struct attr * attr2 = p2; |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 389 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 390 | if (attr1->flag == attr2->flag |
| 391 | && attr1->origin == attr2->origin |
| 392 | && attr1->nexthop.s_addr == attr2->nexthop.s_addr |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 393 | && attr1->aspath == attr2->aspath |
| 394 | && attr1->community == attr2->community |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 395 | && attr1->med == attr2->med |
Paul Jakma | c8f3fe3 | 2010-12-05 20:28:02 +0000 | [diff] [blame] | 396 | && attr1->local_pref == attr2->local_pref) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 397 | { |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 398 | const struct attr_extra *ae1 = attr1->extra; |
| 399 | const struct attr_extra *ae2 = attr2->extra; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 400 | |
| 401 | if (ae1 && ae2 |
| 402 | && ae1->aggregator_as == ae2->aggregator_as |
| 403 | && ae1->aggregator_addr.s_addr == ae2->aggregator_addr.s_addr |
| 404 | && ae1->weight == ae2->weight |
| 405 | #ifdef HAVE_IPV6 |
| 406 | && ae1->mp_nexthop_len == ae2->mp_nexthop_len |
| 407 | && IPV6_ADDR_SAME (&ae1->mp_nexthop_global, &ae2->mp_nexthop_global) |
| 408 | && IPV6_ADDR_SAME (&ae1->mp_nexthop_local, &ae2->mp_nexthop_local) |
| 409 | #endif /* HAVE_IPV6 */ |
| 410 | && IPV4_ADDR_SAME (&ae1->mp_nexthop_global_in, &ae2->mp_nexthop_global_in) |
| 411 | && ae1->ecommunity == ae2->ecommunity |
| 412 | && ae1->cluster == ae2->cluster |
| 413 | && ae1->transit == ae2->transit) |
| 414 | return 1; |
| 415 | else if (ae1 || ae2) |
| 416 | return 0; |
| 417 | /* neither attribute has extra attributes, so they're same */ |
| 418 | return 1; |
| 419 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 420 | else |
| 421 | return 0; |
| 422 | } |
| 423 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 424 | static void |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 425 | attrhash_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 426 | { |
| 427 | attrhash = hash_create (attrhash_key_make, attrhash_cmp); |
| 428 | } |
| 429 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 430 | static void |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 431 | attrhash_finish (void) |
| 432 | { |
| 433 | hash_free (attrhash); |
| 434 | attrhash = NULL; |
| 435 | } |
| 436 | |
| 437 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 438 | attr_show_all_iterator (struct hash_backet *backet, struct vty *vty) |
| 439 | { |
| 440 | struct attr *attr = backet->data; |
| 441 | |
| 442 | vty_out (vty, "attr[%ld] nexthop %s%s", attr->refcnt, |
| 443 | inet_ntoa (attr->nexthop), VTY_NEWLINE); |
| 444 | } |
| 445 | |
| 446 | void |
| 447 | attr_show_all (struct vty *vty) |
| 448 | { |
| 449 | hash_iterate (attrhash, |
| 450 | (void (*)(struct hash_backet *, void *)) |
| 451 | attr_show_all_iterator, |
| 452 | vty); |
| 453 | } |
| 454 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 455 | static void * |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 456 | bgp_attr_hash_alloc (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 457 | { |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 458 | struct attr * val = (struct attr *) p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 459 | struct attr *attr; |
| 460 | |
| 461 | attr = XMALLOC (MTYPE_ATTR, sizeof (struct attr)); |
| 462 | *attr = *val; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 463 | if (val->extra) |
| 464 | { |
| 465 | attr->extra = bgp_attr_extra_new (); |
| 466 | *attr->extra = *val->extra; |
| 467 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 468 | attr->refcnt = 0; |
| 469 | return attr; |
| 470 | } |
| 471 | |
| 472 | /* Internet argument attribute. */ |
| 473 | struct attr * |
| 474 | bgp_attr_intern (struct attr *attr) |
| 475 | { |
| 476 | struct attr *find; |
| 477 | |
| 478 | /* Intern referenced strucutre. */ |
| 479 | if (attr->aspath) |
| 480 | { |
| 481 | if (! attr->aspath->refcnt) |
| 482 | attr->aspath = aspath_intern (attr->aspath); |
| 483 | else |
| 484 | attr->aspath->refcnt++; |
| 485 | } |
| 486 | if (attr->community) |
| 487 | { |
| 488 | if (! attr->community->refcnt) |
| 489 | attr->community = community_intern (attr->community); |
| 490 | else |
| 491 | attr->community->refcnt++; |
| 492 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 493 | if (attr->extra) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 494 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 495 | struct attr_extra *attre = attr->extra; |
| 496 | |
| 497 | if (attre->ecommunity) |
| 498 | { |
| 499 | if (! attre->ecommunity->refcnt) |
| 500 | attre->ecommunity = ecommunity_intern (attre->ecommunity); |
| 501 | else |
| 502 | attre->ecommunity->refcnt++; |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 503 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 504 | } |
| 505 | if (attre->cluster) |
| 506 | { |
| 507 | if (! attre->cluster->refcnt) |
| 508 | attre->cluster = cluster_intern (attre->cluster); |
| 509 | else |
| 510 | attre->cluster->refcnt++; |
| 511 | } |
| 512 | if (attre->transit) |
| 513 | { |
| 514 | if (! attre->transit->refcnt) |
| 515 | attre->transit = transit_intern (attre->transit); |
| 516 | else |
| 517 | attre->transit->refcnt++; |
| 518 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 519 | } |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 520 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 521 | find = (struct attr *) hash_get (attrhash, attr, bgp_attr_hash_alloc); |
| 522 | find->refcnt++; |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 523 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 524 | return find; |
| 525 | } |
| 526 | |
Paul Jakma | 03e214c | 2007-04-29 18:31:07 +0000 | [diff] [blame] | 527 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 528 | /* Make network statement's attribute. */ |
| 529 | struct attr * |
| 530 | bgp_attr_default_set (struct attr *attr, u_char origin) |
| 531 | { |
| 532 | memset (attr, 0, sizeof (struct attr)); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 533 | bgp_attr_extra_get (attr); |
| 534 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 535 | attr->origin = origin; |
| 536 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN); |
| 537 | attr->aspath = aspath_empty (); |
| 538 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 539 | attr->extra->weight = BGP_ATTR_DEFAULT_WEIGHT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 540 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP); |
| 541 | #ifdef HAVE_IPV6 |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 542 | attr->extra->mp_nexthop_len = IPV6_MAX_BYTELEN; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 543 | #endif |
Paul Jakma | 03e214c | 2007-04-29 18:31:07 +0000 | [diff] [blame] | 544 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 545 | return attr; |
| 546 | } |
| 547 | |
Paul Jakma | 03e214c | 2007-04-29 18:31:07 +0000 | [diff] [blame] | 548 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 549 | /* Make network statement's attribute. */ |
| 550 | struct attr * |
| 551 | bgp_attr_default_intern (u_char origin) |
| 552 | { |
| 553 | struct attr attr; |
| 554 | struct attr *new; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 555 | struct attr_extra *attre; |
| 556 | |
| 557 | memset (&attr, 0, sizeof (struct attr)); |
| 558 | attre = bgp_attr_extra_get (&attr); |
| 559 | |
Paul Jakma | 03e214c | 2007-04-29 18:31:07 +0000 | [diff] [blame] | 560 | bgp_attr_default_set(&attr, origin); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 561 | |
| 562 | new = bgp_attr_intern (&attr); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 563 | bgp_attr_extra_free (&attr); |
| 564 | |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 565 | aspath_unintern (&new->aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 566 | return new; |
| 567 | } |
| 568 | |
| 569 | struct attr * |
| 570 | bgp_attr_aggregate_intern (struct bgp *bgp, u_char origin, |
| 571 | struct aspath *aspath, |
| 572 | struct community *community, int as_set) |
| 573 | { |
| 574 | struct attr attr; |
| 575 | struct attr *new; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 576 | struct attr_extra *attre; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 577 | |
| 578 | memset (&attr, 0, sizeof (struct attr)); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 579 | attre = bgp_attr_extra_get (&attr); |
| 580 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 581 | /* Origin attribute. */ |
| 582 | attr.origin = origin; |
| 583 | attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN); |
| 584 | |
| 585 | /* AS path attribute. */ |
| 586 | if (aspath) |
| 587 | attr.aspath = aspath_intern (aspath); |
| 588 | else |
| 589 | attr.aspath = aspath_empty (); |
| 590 | attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH); |
| 591 | |
| 592 | /* Next hop attribute. */ |
| 593 | attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP); |
| 594 | |
| 595 | if (community) |
| 596 | { |
| 597 | attr.community = community; |
| 598 | attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES); |
| 599 | } |
| 600 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 601 | attre->weight = BGP_ATTR_DEFAULT_WEIGHT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 602 | #ifdef HAVE_IPV6 |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 603 | attre->mp_nexthop_len = IPV6_MAX_BYTELEN; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 604 | #endif |
| 605 | if (! as_set) |
| 606 | attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE); |
| 607 | attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR); |
| 608 | if (CHECK_FLAG (bgp->config, BGP_CONFIG_CONFEDERATION)) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 609 | attre->aggregator_as = bgp->confed_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 610 | else |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 611 | attre->aggregator_as = bgp->as; |
| 612 | attre->aggregator_addr = bgp->router_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 613 | |
| 614 | new = bgp_attr_intern (&attr); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 615 | bgp_attr_extra_free (&attr); |
| 616 | |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 617 | aspath_unintern (&new->aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 618 | return new; |
| 619 | } |
| 620 | |
| 621 | /* Free bgp attribute and aspath. */ |
| 622 | void |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 623 | bgp_attr_unintern (struct attr **attr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 624 | { |
| 625 | struct attr *ret; |
| 626 | struct aspath *aspath; |
| 627 | struct community *community; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 628 | struct ecommunity *ecommunity = NULL; |
| 629 | struct cluster_list *cluster = NULL; |
| 630 | struct transit *transit = NULL; |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 631 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 632 | /* Decrement attribute reference. */ |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 633 | (*attr)->refcnt--; |
| 634 | aspath = (*attr)->aspath; |
| 635 | community = (*attr)->community; |
| 636 | if ((*attr)->extra) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 637 | { |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 638 | ecommunity = (*attr)->extra->ecommunity; |
| 639 | cluster = (*attr)->extra->cluster; |
| 640 | transit = (*attr)->extra->transit; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 641 | } |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 642 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 643 | /* If reference becomes zero then free attribute object. */ |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 644 | if ((*attr)->refcnt == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 645 | { |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 646 | ret = hash_release (attrhash, *attr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 647 | assert (ret != NULL); |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 648 | bgp_attr_extra_free (*attr); |
| 649 | XFREE (MTYPE_ATTR, *attr); |
| 650 | *attr = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /* aspath refcount shoud be decrement. */ |
| 654 | if (aspath) |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 655 | aspath_unintern (&aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 656 | if (community) |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 657 | community_unintern (&community); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 658 | if (ecommunity) |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 659 | ecommunity_unintern (&ecommunity); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 660 | if (cluster) |
| 661 | cluster_unintern (cluster); |
| 662 | if (transit) |
| 663 | transit_unintern (transit); |
| 664 | } |
| 665 | |
| 666 | void |
| 667 | bgp_attr_flush (struct attr *attr) |
| 668 | { |
| 669 | if (attr->aspath && ! attr->aspath->refcnt) |
| 670 | aspath_free (attr->aspath); |
| 671 | if (attr->community && ! attr->community->refcnt) |
| 672 | community_free (attr->community); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 673 | if (attr->extra) |
| 674 | { |
| 675 | struct attr_extra *attre = attr->extra; |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 676 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 677 | if (attre->ecommunity && ! attre->ecommunity->refcnt) |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 678 | ecommunity_free (&attre->ecommunity); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 679 | if (attre->cluster && ! attre->cluster->refcnt) |
| 680 | cluster_free (attre->cluster); |
| 681 | if (attre->transit && ! attre->transit->refcnt) |
| 682 | transit_free (attre->transit); |
| 683 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /* Get origin attribute of the update message. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 687 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 688 | bgp_attr_origin (struct peer *peer, bgp_size_t length, |
| 689 | struct attr *attr, u_char flag, u_char *startp) |
| 690 | { |
| 691 | bgp_size_t total; |
| 692 | |
| 693 | /* total is entire attribute length include Attribute Flags (1), |
| 694 | Attribute Type code (1) and Attribute length (1 or 2). */ |
| 695 | total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3); |
| 696 | |
| 697 | /* If any recognized attribute has Attribute Flags that conflict |
| 698 | with the Attribute Type Code, then the Error Subcode is set to |
| 699 | Attribute Flags Error. The Data field contains the erroneous |
| 700 | attribute (type, length and value). */ |
| 701 | if (flag != BGP_ATTR_FLAG_TRANS) |
| 702 | { |
| 703 | zlog (peer->log, LOG_ERR, |
| 704 | "Origin attribute flag isn't transitive %d", flag); |
| 705 | bgp_notify_send_with_data (peer, |
| 706 | BGP_NOTIFY_UPDATE_ERR, |
| 707 | BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR, |
| 708 | startp, total); |
| 709 | return -1; |
| 710 | } |
| 711 | |
| 712 | /* If any recognized attribute has Attribute Length that conflicts |
| 713 | with the expected length (based on the attribute type code), then |
| 714 | the Error Subcode is set to Attribute Length Error. The Data |
| 715 | field contains the erroneous attribute (type, length and |
| 716 | value). */ |
| 717 | if (length != 1) |
| 718 | { |
| 719 | zlog (peer->log, LOG_ERR, "Origin attribute length is not one %d", |
| 720 | length); |
| 721 | bgp_notify_send_with_data (peer, BGP_NOTIFY_UPDATE_ERR, |
| 722 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
| 723 | startp, total); |
| 724 | return -1; |
| 725 | } |
| 726 | |
| 727 | /* Fetch origin attribute. */ |
| 728 | attr->origin = stream_getc (BGP_INPUT (peer)); |
| 729 | |
| 730 | /* If the ORIGIN attribute has an undefined value, then the Error |
| 731 | Subcode is set to Invalid Origin Attribute. The Data field |
| 732 | contains the unrecognized attribute (type, length and value). */ |
| 733 | if ((attr->origin != BGP_ORIGIN_IGP) |
| 734 | && (attr->origin != BGP_ORIGIN_EGP) |
| 735 | && (attr->origin != BGP_ORIGIN_INCOMPLETE)) |
| 736 | { |
| 737 | zlog (peer->log, LOG_ERR, "Origin attribute value is invalid %d", |
| 738 | attr->origin); |
| 739 | |
| 740 | bgp_notify_send_with_data (peer, |
| 741 | BGP_NOTIFY_UPDATE_ERR, |
| 742 | BGP_NOTIFY_UPDATE_INVAL_ORIGIN, |
| 743 | startp, total); |
| 744 | return -1; |
| 745 | } |
| 746 | |
| 747 | /* Set oring attribute flag. */ |
| 748 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN); |
| 749 | |
| 750 | return 0; |
| 751 | } |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 752 | |
| 753 | /* Parse AS path information. This function is wrapper of |
| 754 | aspath_parse. */ |
| 755 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 756 | bgp_attr_aspath (struct peer *peer, bgp_size_t length, |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 757 | struct attr *attr, u_char flag, u_char *startp) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 758 | { |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 759 | bgp_size_t total; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 760 | |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 761 | total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 762 | |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 763 | /* Flag check. */ |
| 764 | if (CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL) |
| 765 | || ! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 766 | { |
| 767 | zlog (peer->log, LOG_ERR, |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 768 | "As-Path attribute flag isn't transitive %d", flag); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 769 | bgp_notify_send_with_data (peer, |
| 770 | BGP_NOTIFY_UPDATE_ERR, |
| 771 | BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR, |
| 772 | startp, total); |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 773 | return -1; |
Chris Hall | cddb811 | 2010-08-09 22:31:37 +0400 | [diff] [blame] | 774 | } |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 775 | |
| 776 | /* |
| 777 | * peer with AS4 => will get 4Byte ASnums |
| 778 | * otherwise, will get 16 Bit |
| 779 | */ |
| 780 | attr->aspath = aspath_parse (peer->ibuf, length, |
| 781 | CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)); |
| 782 | |
| 783 | /* In case of IBGP, length will be zero. */ |
| 784 | if (! attr->aspath) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 785 | { |
| 786 | zlog (peer->log, LOG_ERR, "Malformed AS path length is %d", length); |
| 787 | bgp_notify_send (peer, |
| 788 | BGP_NOTIFY_UPDATE_ERR, |
| 789 | BGP_NOTIFY_UPDATE_MAL_AS_PATH); |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 790 | return -1; |
| 791 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 792 | |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 793 | /* Set aspath attribute flag. */ |
| 794 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH); |
| 795 | |
| 796 | return 0; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | static int bgp_attr_aspath_check( struct peer *peer, |
| 800 | struct attr *attr) |
| 801 | { |
| 802 | /* These checks were part of bgp_attr_aspath, but with |
| 803 | * as4 we should to check aspath things when |
| 804 | * aspath synthesizing with as4_path has already taken place. |
| 805 | * Otherwise we check ASPATH and use the synthesized thing, and that is |
| 806 | * not right. |
| 807 | * So do the checks later, i.e. here |
| 808 | */ |
| 809 | struct bgp *bgp = peer->bgp; |
| 810 | struct aspath *aspath; |
| 811 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 812 | bgp = peer->bgp; |
| 813 | |
Vasilis Tsiligiannis | ca87e1d | 2009-07-20 01:28:35 +0300 | [diff] [blame] | 814 | /* Confederation sanity check. */ |
| 815 | if ((peer_sort (peer) == BGP_PEER_CONFED && ! aspath_left_confed_check (attr->aspath)) || |
| 816 | (peer_sort (peer) == BGP_PEER_EBGP && aspath_confed_check (attr->aspath))) |
| 817 | { |
| 818 | zlog (peer->log, LOG_ERR, "Malformed AS path from %s", peer->host); |
| 819 | bgp_notify_send (peer, |
| 820 | BGP_NOTIFY_UPDATE_ERR, |
| 821 | BGP_NOTIFY_UPDATE_MAL_AS_PATH); |
| 822 | return -1; |
| 823 | } |
| 824 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 825 | /* First AS check for EBGP. */ |
| 826 | if (bgp != NULL && bgp_flag_check (bgp, BGP_FLAG_ENFORCE_FIRST_AS)) |
| 827 | { |
| 828 | if (peer_sort (peer) == BGP_PEER_EBGP |
| 829 | && ! aspath_firstas_check (attr->aspath, peer->as)) |
| 830 | { |
| 831 | zlog (peer->log, LOG_ERR, |
Denis Ovsienko | aea339f | 2009-04-30 17:16:22 +0400 | [diff] [blame] | 832 | "%s incorrect first AS (must be %u)", peer->host, peer->as); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 833 | bgp_notify_send (peer, |
| 834 | BGP_NOTIFY_UPDATE_ERR, |
| 835 | BGP_NOTIFY_UPDATE_MAL_AS_PATH); |
| 836 | return -1; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /* local-as prepend */ |
| 841 | if (peer->change_local_as && |
| 842 | ! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND)) |
| 843 | { |
| 844 | aspath = aspath_dup (attr->aspath); |
| 845 | aspath = aspath_add_seq (aspath, peer->change_local_as); |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 846 | aspath_unintern (&attr->aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 847 | attr->aspath = aspath_intern (aspath); |
| 848 | } |
| 849 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 850 | return 0; |
| 851 | |
| 852 | } |
| 853 | |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 854 | /* Parse AS4 path information. This function is another wrapper of |
| 855 | aspath_parse. */ |
| 856 | static int |
| 857 | bgp_attr_as4_path (struct peer *peer, bgp_size_t length, |
| 858 | struct attr *attr, struct aspath **as4_path) |
| 859 | { |
| 860 | *as4_path = aspath_parse (peer->ibuf, length, 1); |
| 861 | |
| 862 | /* Set aspath attribute flag. */ |
| 863 | if (as4_path) |
| 864 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_PATH); |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 869 | /* Nexthop attribute. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 870 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 871 | bgp_attr_nexthop (struct peer *peer, bgp_size_t length, |
| 872 | struct attr *attr, u_char flag, u_char *startp) |
| 873 | { |
| 874 | bgp_size_t total; |
| 875 | |
| 876 | total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3); |
| 877 | |
| 878 | /* Flag check. */ |
| 879 | if (CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL) |
| 880 | || ! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS)) |
| 881 | { |
| 882 | zlog (peer->log, LOG_ERR, |
| 883 | "Origin attribute flag isn't transitive %d", flag); |
| 884 | bgp_notify_send_with_data (peer, |
| 885 | BGP_NOTIFY_UPDATE_ERR, |
| 886 | BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR, |
| 887 | startp, total); |
| 888 | return -1; |
| 889 | } |
| 890 | |
| 891 | /* Check nexthop attribute length. */ |
| 892 | if (length != 4) |
| 893 | { |
| 894 | zlog (peer->log, LOG_ERR, "Nexthop attribute length isn't four [%d]", |
| 895 | length); |
| 896 | |
| 897 | bgp_notify_send_with_data (peer, |
| 898 | BGP_NOTIFY_UPDATE_ERR, |
| 899 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
| 900 | startp, total); |
| 901 | return -1; |
| 902 | } |
| 903 | |
| 904 | attr->nexthop.s_addr = stream_get_ipv4 (peer->ibuf); |
| 905 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP); |
| 906 | |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | /* MED atrribute. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 911 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 912 | bgp_attr_med (struct peer *peer, bgp_size_t length, |
| 913 | struct attr *attr, u_char flag, u_char *startp) |
| 914 | { |
| 915 | bgp_size_t total; |
| 916 | |
| 917 | total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3); |
| 918 | |
| 919 | /* Length check. */ |
| 920 | if (length != 4) |
| 921 | { |
| 922 | zlog (peer->log, LOG_ERR, |
| 923 | "MED attribute length isn't four [%d]", length); |
| 924 | |
| 925 | bgp_notify_send_with_data (peer, |
| 926 | BGP_NOTIFY_UPDATE_ERR, |
| 927 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, |
| 928 | startp, total); |
| 929 | return -1; |
| 930 | } |
| 931 | |
| 932 | attr->med = stream_getl (peer->ibuf); |
| 933 | |
| 934 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC); |
| 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | /* Local preference attribute. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 940 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 941 | bgp_attr_local_pref (struct peer *peer, bgp_size_t length, |
| 942 | struct attr *attr, u_char flag) |
| 943 | { |
| 944 | /* If it is contained in an UPDATE message that is received from an |
| 945 | external peer, then this attribute MUST be ignored by the |
| 946 | receiving speaker. */ |
| 947 | if (peer_sort (peer) == BGP_PEER_EBGP) |
| 948 | { |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 949 | stream_forward_getp (peer->ibuf, length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | if (length == 4) |
| 954 | attr->local_pref = stream_getl (peer->ibuf); |
| 955 | else |
| 956 | attr->local_pref = 0; |
| 957 | |
| 958 | /* Set atomic aggregate flag. */ |
| 959 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF); |
| 960 | |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | /* Atomic aggregate. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 965 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 966 | bgp_attr_atomic (struct peer *peer, bgp_size_t length, |
| 967 | struct attr *attr, u_char flag) |
| 968 | { |
| 969 | if (length != 0) |
| 970 | { |
| 971 | zlog (peer->log, LOG_ERR, "Bad atomic aggregate length %d", length); |
| 972 | |
| 973 | bgp_notify_send (peer, |
| 974 | BGP_NOTIFY_UPDATE_ERR, |
| 975 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
| 976 | return -1; |
| 977 | } |
| 978 | |
| 979 | /* Set atomic aggregate flag. */ |
| 980 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE); |
| 981 | |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | /* Aggregator attribute */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 986 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 987 | bgp_attr_aggregator (struct peer *peer, bgp_size_t length, |
| 988 | struct attr *attr, u_char flag) |
| 989 | { |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 990 | int wantedlen = 6; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 991 | struct attr_extra *attre = bgp_attr_extra_get (attr); |
| 992 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 993 | /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */ |
| 994 | if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) ) |
| 995 | wantedlen = 8; |
| 996 | |
| 997 | if (length != wantedlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 998 | { |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 999 | zlog (peer->log, LOG_ERR, "Aggregator length is not %d [%d]", wantedlen, length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1000 | |
| 1001 | bgp_notify_send (peer, |
| 1002 | BGP_NOTIFY_UPDATE_ERR, |
| 1003 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
| 1004 | return -1; |
| 1005 | } |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1006 | |
| 1007 | if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) ) |
| 1008 | attre->aggregator_as = stream_getl (peer->ibuf); |
| 1009 | else |
| 1010 | attre->aggregator_as = stream_getw (peer->ibuf); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1011 | attre->aggregator_addr.s_addr = stream_get_ipv4 (peer->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1012 | |
| 1013 | /* Set atomic aggregate flag. */ |
| 1014 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR); |
| 1015 | |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1019 | /* New Aggregator attribute */ |
| 1020 | static int |
| 1021 | bgp_attr_as4_aggregator (struct peer *peer, bgp_size_t length, |
| 1022 | struct attr *attr, as_t *as4_aggregator_as, |
| 1023 | struct in_addr *as4_aggregator_addr) |
| 1024 | { |
| 1025 | if (length != 8) |
| 1026 | { |
| 1027 | zlog (peer->log, LOG_ERR, "New Aggregator length is not 8 [%d]", length); |
| 1028 | |
| 1029 | bgp_notify_send (peer, |
| 1030 | BGP_NOTIFY_UPDATE_ERR, |
| 1031 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
| 1032 | return -1; |
| 1033 | } |
| 1034 | *as4_aggregator_as = stream_getl (peer->ibuf); |
| 1035 | as4_aggregator_addr->s_addr = stream_get_ipv4 (peer->ibuf); |
| 1036 | |
| 1037 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR); |
| 1038 | |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH. |
| 1043 | */ |
| 1044 | static int |
| 1045 | bgp_attr_munge_as4_attrs (struct peer *peer, struct attr *attr, |
| 1046 | struct aspath *as4_path, as_t as4_aggregator, |
| 1047 | struct in_addr *as4_aggregator_addr) |
| 1048 | { |
| 1049 | int ignore_as4_path = 0; |
| 1050 | struct aspath *newpath; |
| 1051 | struct attr_extra *attre = attr->extra; |
| 1052 | |
| 1053 | if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) ) |
| 1054 | { |
| 1055 | /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR |
| 1056 | * if given. |
| 1057 | * It is worth a warning though, because the peer really |
| 1058 | * should not send them |
| 1059 | */ |
| 1060 | if (BGP_DEBUG(as4, AS4)) |
| 1061 | { |
| 1062 | if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH))) |
| 1063 | zlog_debug ("[AS4] %s %s AS4_PATH", |
| 1064 | peer->host, "AS4 capable peer, yet it sent"); |
| 1065 | |
| 1066 | if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR))) |
| 1067 | zlog_debug ("[AS4] %s %s AS4_AGGREGATOR", |
| 1068 | peer->host, "AS4 capable peer, yet it sent"); |
| 1069 | } |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | if (attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH)) |
| 1075 | && !(attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS_PATH)))) |
| 1076 | { |
| 1077 | /* Hu? This is not supposed to happen at all! |
| 1078 | * got as4_path and no aspath, |
| 1079 | * This should already |
| 1080 | * have been handled by 'well known attributes missing' |
| 1081 | * But... yeah, paranoia |
| 1082 | * Take this as a "malformed attribute" |
| 1083 | */ |
| 1084 | zlog (peer->log, LOG_ERR, |
| 1085 | "%s BGP not AS4 capable peer sent AS4_PATH but" |
| 1086 | " no AS_PATH, cant do anything here", peer->host); |
| 1087 | bgp_notify_send (peer, |
| 1088 | BGP_NOTIFY_UPDATE_ERR, |
| 1089 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
| 1090 | return -1; |
| 1091 | } |
| 1092 | |
| 1093 | /* We have a asn16 peer. First, look for AS4_AGGREGATOR |
| 1094 | * because that may override AS4_PATH |
| 1095 | */ |
| 1096 | if (attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR) ) ) |
| 1097 | { |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1098 | if ( attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR) ) ) |
| 1099 | { |
Paul Jakma | 370b64a | 2007-12-22 16:49:52 +0000 | [diff] [blame] | 1100 | assert (attre); |
| 1101 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1102 | /* received both. |
| 1103 | * if the as_number in aggregator is not AS_TRANS, |
| 1104 | * then AS4_AGGREGATOR and AS4_PATH shall be ignored |
| 1105 | * and the Aggregator shall be taken as |
| 1106 | * info on the aggregating node, and the AS_PATH |
| 1107 | * shall be taken as the AS_PATH |
| 1108 | * otherwise |
| 1109 | * the Aggregator shall be ignored and the |
| 1110 | * AS4_AGGREGATOR shall be taken as the |
| 1111 | * Aggregating node and the AS_PATH is to be |
| 1112 | * constructed "as in all other cases" |
| 1113 | */ |
| 1114 | if ( attre->aggregator_as != BGP_AS_TRANS ) |
| 1115 | { |
| 1116 | /* ignore */ |
| 1117 | if ( BGP_DEBUG(as4, AS4)) |
| 1118 | zlog_debug ("[AS4] %s BGP not AS4 capable peer" |
| 1119 | " send AGGREGATOR != AS_TRANS and" |
| 1120 | " AS4_AGGREGATOR, so ignore" |
| 1121 | " AS4_AGGREGATOR and AS4_PATH", peer->host); |
| 1122 | ignore_as4_path = 1; |
| 1123 | } |
| 1124 | else |
| 1125 | { |
| 1126 | /* "New_aggregator shall be taken as aggregator" */ |
| 1127 | attre->aggregator_as = as4_aggregator; |
| 1128 | attre->aggregator_addr.s_addr = as4_aggregator_addr->s_addr; |
| 1129 | } |
| 1130 | } |
| 1131 | else |
| 1132 | { |
| 1133 | /* We received a AS4_AGGREGATOR but no AGGREGATOR. |
| 1134 | * That is bogus - but reading the conditions |
| 1135 | * we have to handle AS4_AGGREGATOR as if it were |
| 1136 | * AGGREGATOR in that case |
| 1137 | */ |
| 1138 | if ( BGP_DEBUG(as4, AS4)) |
| 1139 | zlog_debug ("[AS4] %s BGP not AS4 capable peer send" |
| 1140 | " AS4_AGGREGATOR but no AGGREGATOR, will take" |
| 1141 | " it as if AGGREGATOR with AS_TRANS had been there", peer->host); |
Paul Jakma | 370b64a | 2007-12-22 16:49:52 +0000 | [diff] [blame] | 1142 | (attre = bgp_attr_extra_get (attr))->aggregator_as = as4_aggregator; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1143 | /* sweep it under the carpet and simulate a "good" AGGREGATOR */ |
| 1144 | attr->flag |= (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR)); |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | /* need to reconcile NEW_AS_PATH and AS_PATH */ |
| 1149 | if ( !ignore_as4_path && (attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH))) ) |
| 1150 | { |
| 1151 | newpath = aspath_reconcile_as4 (attr->aspath, as4_path); |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 1152 | aspath_unintern (&attr->aspath); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1153 | attr->aspath = aspath_intern (newpath); |
| 1154 | } |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1158 | /* Community attribute. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1159 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1160 | bgp_attr_community (struct peer *peer, bgp_size_t length, |
| 1161 | struct attr *attr, u_char flag) |
| 1162 | { |
| 1163 | if (length == 0) |
Paul Jakma | b2ceea1 | 2007-09-07 14:24:55 +0000 | [diff] [blame] | 1164 | { |
| 1165 | attr->community = NULL; |
| 1166 | return 0; |
| 1167 | } |
Paul Jakma | 0c46638 | 2010-12-05 17:17:26 +0000 | [diff] [blame] | 1168 | |
| 1169 | attr->community = |
| 1170 | community_parse ((u_int32_t *)stream_pnt (peer->ibuf), length); |
| 1171 | |
| 1172 | /* XXX: fix community_parse to use stream API and remove this */ |
| 1173 | stream_forward_getp (peer->ibuf, length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1174 | |
Paul Jakma | 0c46638 | 2010-12-05 17:17:26 +0000 | [diff] [blame] | 1175 | if (!attr->community) |
| 1176 | return -1; |
| 1177 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1178 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES); |
| 1179 | |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | /* Originator ID attribute. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1184 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1185 | bgp_attr_originator_id (struct peer *peer, bgp_size_t length, |
| 1186 | struct attr *attr, u_char flag) |
| 1187 | { |
| 1188 | if (length != 4) |
| 1189 | { |
| 1190 | zlog (peer->log, LOG_ERR, "Bad originator ID length %d", length); |
| 1191 | |
| 1192 | bgp_notify_send (peer, |
| 1193 | BGP_NOTIFY_UPDATE_ERR, |
| 1194 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
| 1195 | return -1; |
| 1196 | } |
| 1197 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1198 | (bgp_attr_extra_get (attr))->originator_id.s_addr |
| 1199 | = stream_get_ipv4 (peer->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1200 | |
| 1201 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID); |
| 1202 | |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | /* Cluster list attribute. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1207 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1208 | bgp_attr_cluster_list (struct peer *peer, bgp_size_t length, |
| 1209 | struct attr *attr, u_char flag) |
| 1210 | { |
| 1211 | /* Check length. */ |
| 1212 | if (length % 4) |
| 1213 | { |
| 1214 | zlog (peer->log, LOG_ERR, "Bad cluster list length %d", length); |
| 1215 | |
| 1216 | bgp_notify_send (peer, |
| 1217 | BGP_NOTIFY_UPDATE_ERR, |
| 1218 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
| 1219 | return -1; |
| 1220 | } |
| 1221 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1222 | (bgp_attr_extra_get (attr))->cluster |
| 1223 | = cluster_parse ((struct in_addr *)stream_pnt (peer->ibuf), length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1224 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1225 | stream_forward_getp (peer->ibuf, length);; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1226 | |
| 1227 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST); |
| 1228 | |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | /* Multiprotocol reachability information parse. */ |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1233 | int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1234 | bgp_mp_reach_parse (struct peer *peer, bgp_size_t length, struct attr *attr, |
| 1235 | struct bgp_nlri *mp_update) |
| 1236 | { |
Michael Lambert | 4c9641b | 2010-07-22 13:20:55 -0400 | [diff] [blame] | 1237 | afi_t afi; |
| 1238 | safi_t safi; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1239 | bgp_size_t nlri_len; |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1240 | size_t start; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1241 | int ret; |
| 1242 | struct stream *s; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1243 | struct attr_extra *attre = bgp_attr_extra_get(attr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1244 | |
| 1245 | /* Set end of packet. */ |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1246 | s = BGP_INPUT(peer); |
| 1247 | start = stream_get_getp(s); |
| 1248 | |
| 1249 | /* safe to read statically sized header? */ |
| 1250 | #define BGP_MP_REACH_MIN_SIZE 5 |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1251 | #define LEN_LEFT (length - (stream_get_getp(s) - start)) |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1252 | if ((length > STREAM_READABLE(s)) || (length < BGP_MP_REACH_MIN_SIZE)) |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1253 | { |
| 1254 | zlog_info ("%s: %s sent invalid length, %lu", |
| 1255 | __func__, peer->host, (unsigned long)length); |
| 1256 | return -1; |
| 1257 | } |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1258 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1259 | /* Load AFI, SAFI. */ |
| 1260 | afi = stream_getw (s); |
| 1261 | safi = stream_getc (s); |
| 1262 | |
| 1263 | /* Get nexthop length. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1264 | attre->mp_nexthop_len = stream_getc (s); |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1265 | |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1266 | if (LEN_LEFT < attre->mp_nexthop_len) |
| 1267 | { |
| 1268 | zlog_info ("%s: %s, MP nexthop length, %u, goes past end of attribute", |
| 1269 | __func__, peer->host, attre->mp_nexthop_len); |
| 1270 | return -1; |
| 1271 | } |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1272 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1273 | /* Nexthop length check. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1274 | switch (attre->mp_nexthop_len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1275 | { |
| 1276 | case 4: |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1277 | stream_get (&attre->mp_nexthop_global_in, s, 4); |
Michael Lambert | 66bed4f | 2009-07-28 11:26:14 -0400 | [diff] [blame] | 1278 | /* Probably needed for RFC 2283 */ |
| 1279 | if (attr->nexthop.s_addr == 0) |
| 1280 | memcpy(&attr->nexthop.s_addr, &attre->mp_nexthop_global_in, 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | case 12: |
| 1283 | { |
| 1284 | u_int32_t rd_high; |
| 1285 | u_int32_t rd_low; |
| 1286 | |
| 1287 | rd_high = stream_getl (s); |
| 1288 | rd_low = stream_getl (s); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1289 | stream_get (&attre->mp_nexthop_global_in, s, 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1290 | } |
| 1291 | break; |
| 1292 | #ifdef HAVE_IPV6 |
| 1293 | case 16: |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1294 | stream_get (&attre->mp_nexthop_global, s, 16); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1295 | break; |
| 1296 | case 32: |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1297 | stream_get (&attre->mp_nexthop_global, s, 16); |
| 1298 | stream_get (&attre->mp_nexthop_local, s, 16); |
| 1299 | if (! IN6_IS_ADDR_LINKLOCAL (&attre->mp_nexthop_local)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1300 | { |
| 1301 | char buf1[INET6_ADDRSTRLEN]; |
| 1302 | char buf2[INET6_ADDRSTRLEN]; |
| 1303 | |
| 1304 | if (BGP_DEBUG (update, UPDATE_IN)) |
ajs | 557865c | 2004-12-08 19:59:11 +0000 | [diff] [blame] | 1305 | 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] | 1306 | inet_ntop (AF_INET6, &attre->mp_nexthop_global, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1307 | buf1, INET6_ADDRSTRLEN), |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1308 | inet_ntop (AF_INET6, &attre->mp_nexthop_local, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1309 | buf2, INET6_ADDRSTRLEN)); |
| 1310 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1311 | attre->mp_nexthop_len = 16; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1312 | } |
| 1313 | break; |
| 1314 | #endif /* HAVE_IPV6 */ |
| 1315 | default: |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1316 | zlog_info ("%s: (%s) Wrong multiprotocol next hop length: %d", |
| 1317 | __func__, peer->host, attre->mp_nexthop_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1318 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1321 | if (!LEN_LEFT) |
| 1322 | { |
| 1323 | zlog_info ("%s: (%s) Failed to read SNPA and NLRI(s)", |
| 1324 | __func__, peer->host); |
| 1325 | return -1; |
| 1326 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1327 | |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1328 | { |
| 1329 | u_char val; |
| 1330 | if ((val = stream_getc (s))) |
| 1331 | zlog_warn ("%s sent non-zero value, %u, for defunct SNPA-length field", |
| 1332 | peer->host, val); |
| 1333 | } |
| 1334 | |
| 1335 | /* must have nrli_len, what is left of the attribute */ |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1336 | nlri_len = LEN_LEFT; |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1337 | if ((!nlri_len) || (nlri_len > STREAM_READABLE(s))) |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1338 | { |
| 1339 | zlog_info ("%s: (%s) Failed to read NLRI", |
| 1340 | __func__, peer->host); |
| 1341 | return -1; |
| 1342 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1343 | |
| 1344 | if (safi != BGP_SAFI_VPNV4) |
| 1345 | { |
| 1346 | ret = bgp_nlri_sanity_check (peer, afi, stream_pnt (s), nlri_len); |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1347 | if (ret < 0) |
| 1348 | { |
| 1349 | zlog_info ("%s: (%s) NLRI doesn't pass sanity check", |
| 1350 | __func__, peer->host); |
| 1351 | return -1; |
| 1352 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | mp_update->afi = afi; |
| 1356 | mp_update->safi = safi; |
| 1357 | mp_update->nlri = stream_pnt (s); |
| 1358 | mp_update->length = nlri_len; |
| 1359 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1360 | stream_forward_getp (s, nlri_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1361 | |
| 1362 | return 0; |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1363 | #undef LEN_LEFT |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | /* Multiprotocol unreachable parse */ |
Paul Jakma | 0329280 | 2008-06-07 20:37:10 +0000 | [diff] [blame] | 1367 | int |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1368 | bgp_mp_unreach_parse (struct peer *peer, bgp_size_t length, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1369 | struct bgp_nlri *mp_withdraw) |
| 1370 | { |
| 1371 | struct stream *s; |
Michael Lambert | 4c9641b | 2010-07-22 13:20:55 -0400 | [diff] [blame] | 1372 | afi_t afi; |
| 1373 | safi_t safi; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1374 | u_int16_t withdraw_len; |
| 1375 | int ret; |
| 1376 | |
| 1377 | s = peer->ibuf; |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1378 | |
| 1379 | #define BGP_MP_UNREACH_MIN_SIZE 3 |
| 1380 | if ((length > STREAM_READABLE(s)) || (length < BGP_MP_UNREACH_MIN_SIZE)) |
| 1381 | return -1; |
| 1382 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1383 | afi = stream_getw (s); |
| 1384 | safi = stream_getc (s); |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1385 | |
| 1386 | withdraw_len = length - BGP_MP_UNREACH_MIN_SIZE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1387 | |
| 1388 | if (safi != BGP_SAFI_VPNV4) |
| 1389 | { |
| 1390 | ret = bgp_nlri_sanity_check (peer, afi, stream_pnt (s), withdraw_len); |
| 1391 | if (ret < 0) |
| 1392 | return -1; |
| 1393 | } |
| 1394 | |
| 1395 | mp_withdraw->afi = afi; |
| 1396 | mp_withdraw->safi = safi; |
| 1397 | mp_withdraw->nlri = stream_pnt (s); |
| 1398 | mp_withdraw->length = withdraw_len; |
| 1399 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1400 | stream_forward_getp (s, withdraw_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1401 | |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | /* Extended Community attribute. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1406 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1407 | bgp_attr_ext_communities (struct peer *peer, bgp_size_t length, |
| 1408 | struct attr *attr, u_char flag) |
| 1409 | { |
| 1410 | if (length == 0) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1411 | { |
| 1412 | if (attr->extra) |
| 1413 | attr->extra->ecommunity = NULL; |
Paul Jakma | 0c46638 | 2010-12-05 17:17:26 +0000 | [diff] [blame] | 1414 | /* Empty extcomm doesn't seem to be invalid per se */ |
| 1415 | return 0; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1416 | } |
Paul Jakma | 0c46638 | 2010-12-05 17:17:26 +0000 | [diff] [blame] | 1417 | |
| 1418 | (bgp_attr_extra_get (attr))->ecommunity = |
| 1419 | ecommunity_parse ((u_int8_t *)stream_pnt (peer->ibuf), length); |
| 1420 | /* XXX: fix ecommunity_parse to use stream API */ |
| 1421 | stream_forward_getp (peer->ibuf, length); |
| 1422 | |
| 1423 | if (!attr->extra->ecommunity) |
| 1424 | return -1; |
| 1425 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1426 | attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES); |
| 1427 | |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
| 1431 | /* BGP unknown attribute treatment. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1432 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1433 | bgp_attr_unknown (struct peer *peer, struct attr *attr, u_char flag, |
| 1434 | u_char type, bgp_size_t length, u_char *startp) |
| 1435 | { |
| 1436 | bgp_size_t total; |
| 1437 | struct transit *transit; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1438 | struct attr_extra *attre; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1439 | |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame] | 1440 | if (BGP_DEBUG (normal, NORMAL)) |
| 1441 | zlog_debug ("%s Unknown attribute is received (type %d, length %d)", |
| 1442 | peer->host, type, length); |
| 1443 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1444 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 557865c | 2004-12-08 19:59:11 +0000 | [diff] [blame] | 1445 | zlog (peer->log, LOG_DEBUG, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1446 | "Unknown attribute type %d length %d is received", type, length); |
| 1447 | |
| 1448 | /* Forward read pointer of input stream. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1449 | stream_forward_getp (peer->ibuf, length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1450 | |
| 1451 | /* Adjest total length to include type and length. */ |
| 1452 | total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3); |
| 1453 | |
| 1454 | /* If any of the mandatory well-known attributes are not recognized, |
| 1455 | then the Error Subcode is set to Unrecognized Well-known |
| 1456 | Attribute. The Data field contains the unrecognized attribute |
| 1457 | (type, length and value). */ |
| 1458 | if (! CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL)) |
| 1459 | { |
| 1460 | /* Adjust startp to do not include flag value. */ |
| 1461 | bgp_notify_send_with_data (peer, |
| 1462 | BGP_NOTIFY_UPDATE_ERR, |
| 1463 | BGP_NOTIFY_UPDATE_UNREC_ATTR, |
| 1464 | startp, total); |
| 1465 | return -1; |
| 1466 | } |
| 1467 | |
| 1468 | /* Unrecognized non-transitive optional attributes must be quietly |
| 1469 | ignored and not passed along to other BGP peers. */ |
| 1470 | if (! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS)) |
| 1471 | return 0; |
| 1472 | |
| 1473 | /* If a path with recognized transitive optional attribute is |
| 1474 | accepted and passed along to other BGP peers and the Partial bit |
| 1475 | in the Attribute Flags octet is set to 1 by some previous AS, it |
| 1476 | is not set back to 0 by the current AS. */ |
| 1477 | SET_FLAG (*startp, BGP_ATTR_FLAG_PARTIAL); |
| 1478 | |
| 1479 | /* Store transitive attribute to the end of attr->transit. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1480 | if (! ((attre = bgp_attr_extra_get(attr))->transit) ) |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 1481 | attre->transit = XCALLOC (MTYPE_TRANSIT, sizeof (struct transit)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1482 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1483 | transit = attre->transit; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1484 | |
| 1485 | if (transit->val) |
| 1486 | transit->val = XREALLOC (MTYPE_TRANSIT_VAL, transit->val, |
| 1487 | transit->length + total); |
| 1488 | else |
| 1489 | transit->val = XMALLOC (MTYPE_TRANSIT_VAL, total); |
| 1490 | |
| 1491 | memcpy (transit->val + transit->length, startp, total); |
| 1492 | transit->length += total; |
| 1493 | |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | /* Read attribute of update packet. This function is called from |
| 1498 | bgp_update() in bgpd.c. */ |
| 1499 | int |
| 1500 | bgp_attr_parse (struct peer *peer, struct attr *attr, bgp_size_t size, |
| 1501 | struct bgp_nlri *mp_update, struct bgp_nlri *mp_withdraw) |
| 1502 | { |
| 1503 | int ret; |
| 1504 | u_char flag; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1505 | u_char type = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1506 | bgp_size_t length; |
| 1507 | u_char *startp, *endp; |
| 1508 | u_char *attr_endp; |
| 1509 | u_char seen[BGP_ATTR_BITMAP_SIZE]; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1510 | /* we need the as4_path only until we have synthesized the as_path with it */ |
| 1511 | /* same goes for as4_aggregator */ |
| 1512 | struct aspath *as4_path = NULL; |
| 1513 | as_t as4_aggregator = 0; |
| 1514 | struct in_addr as4_aggregator_addr = { 0 }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1515 | |
| 1516 | /* Initialize bitmap. */ |
| 1517 | memset (seen, 0, BGP_ATTR_BITMAP_SIZE); |
| 1518 | |
| 1519 | /* End pointer of BGP attribute. */ |
| 1520 | endp = BGP_INPUT_PNT (peer) + size; |
| 1521 | |
| 1522 | /* Get attributes to the end of attribute length. */ |
| 1523 | while (BGP_INPUT_PNT (peer) < endp) |
| 1524 | { |
| 1525 | /* Check remaining length check.*/ |
| 1526 | if (endp - BGP_INPUT_PNT (peer) < BGP_ATTR_MIN_LEN) |
| 1527 | { |
gdt | c29fdba | 2004-12-09 14:46:46 +0000 | [diff] [blame] | 1528 | /* XXX warning: long int format, int arg (arg 5) */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1529 | zlog (peer->log, LOG_WARNING, |
Paul Jakma | 4136717 | 2007-08-06 15:24:51 +0000 | [diff] [blame] | 1530 | "%s error BGP attribute length %lu is smaller than min len", |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1531 | peer->host, |
| 1532 | (unsigned long) (endp - STREAM_PNT (BGP_INPUT (peer)))); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1533 | |
| 1534 | bgp_notify_send (peer, |
| 1535 | BGP_NOTIFY_UPDATE_ERR, |
| 1536 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
| 1537 | return -1; |
| 1538 | } |
| 1539 | |
| 1540 | /* Fetch attribute flag and type. */ |
| 1541 | startp = BGP_INPUT_PNT (peer); |
| 1542 | flag = stream_getc (BGP_INPUT (peer)); |
| 1543 | type = stream_getc (BGP_INPUT (peer)); |
| 1544 | |
Paul Jakma | 370b64a | 2007-12-22 16:49:52 +0000 | [diff] [blame] | 1545 | /* Check whether Extended-Length applies and is in bounds */ |
| 1546 | if (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) |
| 1547 | && ((endp - startp) < (BGP_ATTR_MIN_LEN + 1))) |
| 1548 | { |
| 1549 | zlog (peer->log, LOG_WARNING, |
Paul Jakma | 851a1a5 | 2008-07-22 19:56:56 +0000 | [diff] [blame] | 1550 | "%s Extended length set, but just %lu bytes of attr header", |
Paul Jakma | 370b64a | 2007-12-22 16:49:52 +0000 | [diff] [blame] | 1551 | peer->host, |
| 1552 | (unsigned long) (endp - STREAM_PNT (BGP_INPUT (peer)))); |
| 1553 | |
| 1554 | bgp_notify_send (peer, |
| 1555 | BGP_NOTIFY_UPDATE_ERR, |
| 1556 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
| 1557 | return -1; |
| 1558 | } |
| 1559 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1560 | /* Check extended attribue length bit. */ |
| 1561 | if (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN)) |
| 1562 | length = stream_getw (BGP_INPUT (peer)); |
| 1563 | else |
| 1564 | length = stream_getc (BGP_INPUT (peer)); |
| 1565 | |
| 1566 | /* If any attribute appears more than once in the UPDATE |
| 1567 | message, then the Error Subcode is set to Malformed Attribute |
| 1568 | List. */ |
| 1569 | |
| 1570 | if (CHECK_BITMAP (seen, type)) |
| 1571 | { |
| 1572 | zlog (peer->log, LOG_WARNING, |
| 1573 | "%s error BGP attribute type %d appears twice in a message", |
| 1574 | peer->host, type); |
| 1575 | |
| 1576 | bgp_notify_send (peer, |
| 1577 | BGP_NOTIFY_UPDATE_ERR, |
| 1578 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
| 1579 | return -1; |
| 1580 | } |
| 1581 | |
| 1582 | /* Set type to bitmap to check duplicate attribute. `type' is |
| 1583 | unsigned char so it never overflow bitmap range. */ |
| 1584 | |
| 1585 | SET_BITMAP (seen, type); |
| 1586 | |
| 1587 | /* Overflow check. */ |
| 1588 | attr_endp = BGP_INPUT_PNT (peer) + length; |
| 1589 | |
| 1590 | if (attr_endp > endp) |
| 1591 | { |
| 1592 | zlog (peer->log, LOG_WARNING, |
| 1593 | "%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); |
| 1594 | bgp_notify_send (peer, |
| 1595 | BGP_NOTIFY_UPDATE_ERR, |
| 1596 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
| 1597 | return -1; |
| 1598 | } |
| 1599 | |
| 1600 | /* OK check attribute and store it's value. */ |
| 1601 | switch (type) |
| 1602 | { |
| 1603 | case BGP_ATTR_ORIGIN: |
| 1604 | ret = bgp_attr_origin (peer, length, attr, flag, startp); |
| 1605 | break; |
| 1606 | case BGP_ATTR_AS_PATH: |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 1607 | ret = bgp_attr_aspath (peer, length, attr, flag, startp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1608 | break; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1609 | case BGP_ATTR_AS4_PATH: |
Paul Jakma | ab00529 | 2010-11-27 22:48:34 +0000 | [diff] [blame] | 1610 | ret = bgp_attr_as4_path (peer, length, attr, &as4_path ); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1611 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1612 | case BGP_ATTR_NEXT_HOP: |
| 1613 | ret = bgp_attr_nexthop (peer, length, attr, flag, startp); |
| 1614 | break; |
| 1615 | case BGP_ATTR_MULTI_EXIT_DISC: |
| 1616 | ret = bgp_attr_med (peer, length, attr, flag, startp); |
| 1617 | break; |
| 1618 | case BGP_ATTR_LOCAL_PREF: |
| 1619 | ret = bgp_attr_local_pref (peer, length, attr, flag); |
| 1620 | break; |
| 1621 | case BGP_ATTR_ATOMIC_AGGREGATE: |
| 1622 | ret = bgp_attr_atomic (peer, length, attr, flag); |
| 1623 | break; |
| 1624 | case BGP_ATTR_AGGREGATOR: |
| 1625 | ret = bgp_attr_aggregator (peer, length, attr, flag); |
| 1626 | break; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1627 | case BGP_ATTR_AS4_AGGREGATOR: |
| 1628 | ret = bgp_attr_as4_aggregator (peer, length, attr, &as4_aggregator, &as4_aggregator_addr); |
| 1629 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1630 | case BGP_ATTR_COMMUNITIES: |
| 1631 | ret = bgp_attr_community (peer, length, attr, flag); |
| 1632 | break; |
| 1633 | case BGP_ATTR_ORIGINATOR_ID: |
| 1634 | ret = bgp_attr_originator_id (peer, length, attr, flag); |
| 1635 | break; |
| 1636 | case BGP_ATTR_CLUSTER_LIST: |
| 1637 | ret = bgp_attr_cluster_list (peer, length, attr, flag); |
| 1638 | break; |
| 1639 | case BGP_ATTR_MP_REACH_NLRI: |
| 1640 | ret = bgp_mp_reach_parse (peer, length, attr, mp_update); |
| 1641 | break; |
| 1642 | case BGP_ATTR_MP_UNREACH_NLRI: |
| 1643 | ret = bgp_mp_unreach_parse (peer, length, mp_withdraw); |
| 1644 | break; |
| 1645 | case BGP_ATTR_EXT_COMMUNITIES: |
| 1646 | ret = bgp_attr_ext_communities (peer, length, attr, flag); |
| 1647 | break; |
| 1648 | default: |
| 1649 | ret = bgp_attr_unknown (peer, attr, flag, type, length, startp); |
| 1650 | break; |
| 1651 | } |
| 1652 | |
| 1653 | /* If error occured immediately return to the caller. */ |
| 1654 | if (ret < 0) |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1655 | { |
| 1656 | zlog (peer->log, LOG_WARNING, |
| 1657 | "%s: Attribute %s, parse error", |
| 1658 | peer->host, |
| 1659 | LOOKUP (attr_str, type)); |
| 1660 | bgp_notify_send (peer, |
| 1661 | BGP_NOTIFY_UPDATE_ERR, |
| 1662 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
| 1663 | return ret; |
| 1664 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1665 | |
| 1666 | /* Check the fetched length. */ |
| 1667 | if (BGP_INPUT_PNT (peer) != attr_endp) |
| 1668 | { |
| 1669 | zlog (peer->log, LOG_WARNING, |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1670 | "%s: BGP attribute %s, fetch error", |
| 1671 | peer->host, LOOKUP (attr_str, type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1672 | bgp_notify_send (peer, |
| 1673 | BGP_NOTIFY_UPDATE_ERR, |
| 1674 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
| 1675 | return -1; |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | /* Check final read pointer is same as end pointer. */ |
| 1680 | if (BGP_INPUT_PNT (peer) != endp) |
| 1681 | { |
| 1682 | zlog (peer->log, LOG_WARNING, |
Paul Jakma | 6e4ab12 | 2007-04-10 19:36:48 +0000 | [diff] [blame] | 1683 | "%s BGP attribute %s, length mismatch", |
| 1684 | peer->host, LOOKUP (attr_str, type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1685 | bgp_notify_send (peer, |
| 1686 | BGP_NOTIFY_UPDATE_ERR, |
| 1687 | BGP_NOTIFY_UPDATE_ATTR_LENG_ERR); |
| 1688 | return -1; |
| 1689 | } |
| 1690 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1691 | /* |
| 1692 | * At this place we can see whether we got AS4_PATH and/or |
| 1693 | * AS4_AGGREGATOR from a 16Bit peer and act accordingly. |
| 1694 | * We can not do this before we've read all attributes because |
| 1695 | * the as4 handling does not say whether AS4_PATH has to be sent |
| 1696 | * after AS_PATH or not - and when AS4_AGGREGATOR will be send |
| 1697 | * in relationship to AGGREGATOR. |
| 1698 | * So, to be defensive, we are not relying on any order and read |
| 1699 | * all attributes first, including these 32bit ones, and now, |
| 1700 | * afterwards, we look what and if something is to be done for as4. |
| 1701 | */ |
| 1702 | if (bgp_attr_munge_as4_attrs (peer, attr, as4_path, |
| 1703 | as4_aggregator, &as4_aggregator_addr)) |
| 1704 | return -1; |
| 1705 | |
| 1706 | /* At this stage, we have done all fiddling with as4, and the |
| 1707 | * resulting info is in attr->aggregator resp. attr->aspath |
| 1708 | * so we can chuck as4_aggregator and as4_path alltogether in |
| 1709 | * order to save memory |
| 1710 | */ |
| 1711 | if ( as4_path ) |
| 1712 | { |
Paul Jakma | f6f434b | 2010-11-23 21:28:03 +0000 | [diff] [blame] | 1713 | aspath_unintern (&as4_path); /* unintern - it is in the hash */ |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1714 | /* The flag that we got this is still there, but that does not |
| 1715 | * do any trouble |
| 1716 | */ |
| 1717 | } |
| 1718 | /* |
| 1719 | * The "rest" of the code does nothing with as4_aggregator. |
| 1720 | * there is no memory attached specifically which is not part |
| 1721 | * of the attr. |
| 1722 | * so ignoring just means do nothing. |
| 1723 | */ |
| 1724 | /* |
| 1725 | * Finally do the checks on the aspath we did not do yet |
| 1726 | * because we waited for a potentially synthesized aspath. |
| 1727 | */ |
| 1728 | if ( attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS_PATH))) |
| 1729 | { |
| 1730 | ret = bgp_attr_aspath_check( peer, attr ); |
| 1731 | if ( ret < 0 ) |
| 1732 | return ret; |
| 1733 | } |
| 1734 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1735 | /* Finally intern unknown attribute. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1736 | if (attr->extra && attr->extra->transit) |
| 1737 | attr->extra->transit = transit_intern (attr->extra->transit); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1738 | |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | /* Well-known attribute check. */ |
| 1743 | int |
| 1744 | bgp_attr_check (struct peer *peer, struct attr *attr) |
| 1745 | { |
| 1746 | u_char type = 0; |
| 1747 | |
| 1748 | if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_ORIGIN))) |
| 1749 | type = BGP_ATTR_ORIGIN; |
| 1750 | |
| 1751 | if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH))) |
| 1752 | type = BGP_ATTR_AS_PATH; |
| 1753 | |
| 1754 | if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP))) |
| 1755 | type = BGP_ATTR_NEXT_HOP; |
| 1756 | |
| 1757 | if (peer_sort (peer) == BGP_PEER_IBGP |
| 1758 | && ! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))) |
| 1759 | type = BGP_ATTR_LOCAL_PREF; |
| 1760 | |
| 1761 | if (type) |
| 1762 | { |
| 1763 | zlog (peer->log, LOG_WARNING, |
| 1764 | "%s Missing well-known attribute %d.", |
| 1765 | peer->host, type); |
| 1766 | bgp_notify_send_with_data (peer, |
| 1767 | BGP_NOTIFY_UPDATE_ERR, |
| 1768 | BGP_NOTIFY_UPDATE_MISS_ATTR, |
| 1769 | &type, 1); |
| 1770 | return -1; |
| 1771 | } |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
| 1775 | int stream_put_prefix (struct stream *, struct prefix *); |
| 1776 | |
| 1777 | /* Make attribute packet. */ |
| 1778 | bgp_size_t |
| 1779 | bgp_packet_attribute (struct bgp *bgp, struct peer *peer, |
| 1780 | struct stream *s, struct attr *attr, struct prefix *p, |
| 1781 | afi_t afi, safi_t safi, struct peer *from, |
Paul Jakma | a3b6ea5 | 2006-05-04 07:52:12 +0000 | [diff] [blame] | 1782 | struct prefix_rd *prd, u_char *tag) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1783 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1784 | size_t cp; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1785 | size_t aspath_sizep; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1786 | struct aspath *aspath; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1787 | int send_as4_path = 0; |
| 1788 | int send_as4_aggregator = 0; |
| 1789 | int use32bit = (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)) ? 1 : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1790 | |
| 1791 | if (! bgp) |
| 1792 | bgp = bgp_get_default (); |
| 1793 | |
| 1794 | /* Remember current pointer. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1795 | cp = stream_get_endp (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1796 | |
| 1797 | /* Origin attribute. */ |
| 1798 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 1799 | stream_putc (s, BGP_ATTR_ORIGIN); |
| 1800 | stream_putc (s, 1); |
| 1801 | stream_putc (s, attr->origin); |
| 1802 | |
| 1803 | /* AS path attribute. */ |
| 1804 | |
| 1805 | /* If remote-peer is EBGP */ |
| 1806 | if (peer_sort (peer) == BGP_PEER_EBGP |
| 1807 | && (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_AS_PATH_UNCHANGED) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1808 | || attr->aspath->segments == NULL) |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1809 | && (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1810 | { |
| 1811 | aspath = aspath_dup (attr->aspath); |
| 1812 | |
| 1813 | if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) |
| 1814 | { |
| 1815 | /* Strip the confed info, and then stuff our path CONFED_ID |
| 1816 | on the front */ |
| 1817 | aspath = aspath_delete_confed_seq (aspath); |
| 1818 | aspath = aspath_add_seq (aspath, bgp->confed_id); |
| 1819 | } |
| 1820 | else |
| 1821 | { |
| 1822 | aspath = aspath_add_seq (aspath, peer->local_as); |
| 1823 | if (peer->change_local_as) |
| 1824 | aspath = aspath_add_seq (aspath, peer->change_local_as); |
| 1825 | } |
| 1826 | } |
| 1827 | else if (peer_sort (peer) == BGP_PEER_CONFED) |
| 1828 | { |
| 1829 | /* A confed member, so we need to do the AS_CONFED_SEQUENCE thing */ |
| 1830 | aspath = aspath_dup (attr->aspath); |
| 1831 | aspath = aspath_add_confed_seq (aspath, peer->local_as); |
| 1832 | } |
| 1833 | else |
| 1834 | aspath = attr->aspath; |
| 1835 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1836 | /* If peer is not AS4 capable, then: |
| 1837 | * - send the created AS_PATH out as AS4_PATH (optional, transitive), |
| 1838 | * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path segment |
| 1839 | * types are in it (i.e. exclude them if they are there) |
| 1840 | * AND do this only if there is at least one asnum > 65535 in the path! |
| 1841 | * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and change |
| 1842 | * all ASnums > 65535 to BGP_AS_TRANS |
| 1843 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1844 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1845 | stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN); |
| 1846 | stream_putc (s, BGP_ATTR_AS_PATH); |
| 1847 | aspath_sizep = stream_get_endp (s); |
| 1848 | stream_putw (s, 0); |
| 1849 | stream_putw_at (s, aspath_sizep, aspath_put (s, aspath, use32bit)); |
| 1850 | |
| 1851 | /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs |
| 1852 | * in the path |
| 1853 | */ |
| 1854 | if (!use32bit && aspath_has_as4 (aspath)) |
| 1855 | send_as4_path = 1; /* we'll do this later, at the correct place */ |
| 1856 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1857 | /* Nexthop attribute. */ |
| 1858 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP) && afi == AFI_IP) |
| 1859 | { |
| 1860 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 1861 | stream_putc (s, BGP_ATTR_NEXT_HOP); |
| 1862 | stream_putc (s, 4); |
| 1863 | if (safi == SAFI_MPLS_VPN) |
| 1864 | { |
| 1865 | if (attr->nexthop.s_addr == 0) |
| 1866 | stream_put_ipv4 (s, peer->nexthop.v4.s_addr); |
| 1867 | else |
| 1868 | stream_put_ipv4 (s, attr->nexthop.s_addr); |
| 1869 | } |
| 1870 | else |
| 1871 | stream_put_ipv4 (s, attr->nexthop.s_addr); |
| 1872 | } |
| 1873 | |
| 1874 | /* MED attribute. */ |
| 1875 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC)) |
| 1876 | { |
| 1877 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); |
| 1878 | stream_putc (s, BGP_ATTR_MULTI_EXIT_DISC); |
| 1879 | stream_putc (s, 4); |
| 1880 | stream_putl (s, attr->med); |
| 1881 | } |
| 1882 | |
| 1883 | /* Local preference. */ |
| 1884 | if (peer_sort (peer) == BGP_PEER_IBGP || |
| 1885 | peer_sort (peer) == BGP_PEER_CONFED) |
| 1886 | { |
| 1887 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 1888 | stream_putc (s, BGP_ATTR_LOCAL_PREF); |
| 1889 | stream_putc (s, 4); |
| 1890 | stream_putl (s, attr->local_pref); |
| 1891 | } |
| 1892 | |
| 1893 | /* Atomic aggregate. */ |
| 1894 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE)) |
| 1895 | { |
| 1896 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 1897 | stream_putc (s, BGP_ATTR_ATOMIC_AGGREGATE); |
| 1898 | stream_putc (s, 0); |
| 1899 | } |
| 1900 | |
| 1901 | /* Aggregator. */ |
| 1902 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR)) |
| 1903 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1904 | assert (attr->extra); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1905 | |
| 1906 | /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1907 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 1908 | stream_putc (s, BGP_ATTR_AGGREGATOR); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1909 | |
| 1910 | if (use32bit) |
| 1911 | { |
| 1912 | /* AS4 capable peer */ |
| 1913 | stream_putc (s, 8); |
| 1914 | stream_putl (s, attr->extra->aggregator_as); |
| 1915 | } |
| 1916 | else |
| 1917 | { |
| 1918 | /* 2-byte AS peer */ |
| 1919 | stream_putc (s, 6); |
| 1920 | |
| 1921 | /* Is ASN representable in 2-bytes? Or must AS_TRANS be used? */ |
| 1922 | if ( attr->extra->aggregator_as > 65535 ) |
| 1923 | { |
| 1924 | stream_putw (s, BGP_AS_TRANS); |
| 1925 | |
| 1926 | /* we have to send AS4_AGGREGATOR, too. |
| 1927 | * we'll do that later in order to send attributes in ascending |
| 1928 | * order. |
| 1929 | */ |
| 1930 | send_as4_aggregator = 1; |
| 1931 | } |
| 1932 | else |
| 1933 | stream_putw (s, (u_int16_t) attr->extra->aggregator_as); |
| 1934 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1935 | stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | /* Community attribute. */ |
| 1939 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY) |
| 1940 | && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES))) |
| 1941 | { |
| 1942 | if (attr->community->size * 4 > 255) |
| 1943 | { |
| 1944 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN); |
| 1945 | stream_putc (s, BGP_ATTR_COMMUNITIES); |
| 1946 | stream_putw (s, attr->community->size * 4); |
| 1947 | } |
| 1948 | else |
| 1949 | { |
| 1950 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 1951 | stream_putc (s, BGP_ATTR_COMMUNITIES); |
| 1952 | stream_putc (s, attr->community->size * 4); |
| 1953 | } |
| 1954 | stream_put (s, attr->community->val, attr->community->size * 4); |
| 1955 | } |
| 1956 | |
| 1957 | /* Route Reflector. */ |
| 1958 | if (peer_sort (peer) == BGP_PEER_IBGP |
| 1959 | && from |
| 1960 | && peer_sort (from) == BGP_PEER_IBGP) |
| 1961 | { |
| 1962 | /* Originator ID. */ |
| 1963 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); |
| 1964 | stream_putc (s, BGP_ATTR_ORIGINATOR_ID); |
| 1965 | stream_putc (s, 4); |
| 1966 | |
| 1967 | if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1968 | stream_put_in_addr (s, &attr->extra->originator_id); |
Paul Jakma | 34c3f81 | 2006-05-12 23:25:37 +0000 | [diff] [blame] | 1969 | else |
| 1970 | stream_put_in_addr (s, &from->remote_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1971 | |
| 1972 | /* Cluster list. */ |
| 1973 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); |
| 1974 | stream_putc (s, BGP_ATTR_CLUSTER_LIST); |
| 1975 | |
Paul Jakma | 9eda90c | 2007-08-30 13:36:17 +0000 | [diff] [blame] | 1976 | if (attr->extra && attr->extra->cluster) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1977 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1978 | stream_putc (s, attr->extra->cluster->length + 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1979 | /* If this peer configuration's parent BGP has cluster_id. */ |
| 1980 | if (bgp->config & BGP_CONFIG_CLUSTER_ID) |
| 1981 | stream_put_in_addr (s, &bgp->cluster_id); |
| 1982 | else |
| 1983 | stream_put_in_addr (s, &bgp->router_id); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 1984 | stream_put (s, attr->extra->cluster->list, |
| 1985 | attr->extra->cluster->length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1986 | } |
| 1987 | else |
| 1988 | { |
| 1989 | stream_putc (s, 4); |
| 1990 | /* If this peer configuration's parent BGP has cluster_id. */ |
| 1991 | if (bgp->config & BGP_CONFIG_CLUSTER_ID) |
| 1992 | stream_put_in_addr (s, &bgp->cluster_id); |
| 1993 | else |
| 1994 | stream_put_in_addr (s, &bgp->router_id); |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | #ifdef HAVE_IPV6 |
| 1999 | /* If p is IPv6 address put it into attribute. */ |
| 2000 | if (p->family == AF_INET6) |
| 2001 | { |
| 2002 | unsigned long sizep; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2003 | struct attr_extra *attre = attr->extra; |
| 2004 | |
| 2005 | assert (attr->extra); |
| 2006 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2007 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); |
| 2008 | stream_putc (s, BGP_ATTR_MP_REACH_NLRI); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2009 | sizep = stream_get_endp (s); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2010 | stream_putc (s, 0); /* Marker: Attribute length. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2011 | stream_putw (s, AFI_IP6); /* AFI */ |
| 2012 | stream_putc (s, safi); /* SAFI */ |
| 2013 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2014 | stream_putc (s, attre->mp_nexthop_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2015 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2016 | if (attre->mp_nexthop_len == 16) |
| 2017 | stream_put (s, &attre->mp_nexthop_global, 16); |
| 2018 | else if (attre->mp_nexthop_len == 32) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2019 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2020 | stream_put (s, &attre->mp_nexthop_global, 16); |
| 2021 | stream_put (s, &attre->mp_nexthop_local, 16); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | /* SNPA */ |
| 2025 | stream_putc (s, 0); |
| 2026 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2027 | /* Prefix write. */ |
| 2028 | stream_put_prefix (s, p); |
| 2029 | |
| 2030 | /* Set MP attribute length. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2031 | stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2032 | } |
| 2033 | #endif /* HAVE_IPV6 */ |
| 2034 | |
| 2035 | if (p->family == AF_INET && safi == SAFI_MULTICAST) |
| 2036 | { |
| 2037 | unsigned long sizep; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2038 | |
| 2039 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); |
| 2040 | stream_putc (s, BGP_ATTR_MP_REACH_NLRI); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2041 | sizep = stream_get_endp (s); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2042 | stream_putc (s, 0); /* Marker: Attribute Length. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2043 | stream_putw (s, AFI_IP); /* AFI */ |
| 2044 | stream_putc (s, SAFI_MULTICAST); /* SAFI */ |
| 2045 | |
| 2046 | stream_putc (s, 4); |
| 2047 | stream_put_ipv4 (s, attr->nexthop.s_addr); |
| 2048 | |
| 2049 | /* SNPA */ |
| 2050 | stream_putc (s, 0); |
| 2051 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2052 | /* Prefix write. */ |
| 2053 | stream_put_prefix (s, p); |
| 2054 | |
| 2055 | /* Set MP attribute length. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2056 | stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | if (p->family == AF_INET && safi == SAFI_MPLS_VPN) |
| 2060 | { |
| 2061 | unsigned long sizep; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2062 | |
| 2063 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); |
| 2064 | stream_putc (s, BGP_ATTR_MP_REACH_NLRI); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2065 | sizep = stream_get_endp (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2066 | stream_putc (s, 0); /* Length of this attribute. */ |
| 2067 | stream_putw (s, AFI_IP); /* AFI */ |
| 2068 | stream_putc (s, BGP_SAFI_VPNV4); /* SAFI */ |
| 2069 | |
| 2070 | stream_putc (s, 12); |
| 2071 | stream_putl (s, 0); |
| 2072 | stream_putl (s, 0); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2073 | stream_put (s, &attr->extra->mp_nexthop_global_in, 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2074 | |
| 2075 | /* SNPA */ |
| 2076 | stream_putc (s, 0); |
| 2077 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2078 | /* Tag, RD, Prefix write. */ |
| 2079 | stream_putc (s, p->prefixlen + 88); |
| 2080 | stream_put (s, tag, 3); |
| 2081 | stream_put (s, prd->val, 8); |
| 2082 | stream_put (s, &p->u.prefix, PSIZE (p->prefixlen)); |
| 2083 | |
| 2084 | /* Set MP attribute length. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2085 | stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | /* Extended Communities attribute. */ |
| 2089 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY) |
| 2090 | && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES))) |
| 2091 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2092 | struct attr_extra *attre = attr->extra; |
| 2093 | |
| 2094 | assert (attre); |
| 2095 | |
| 2096 | if (peer_sort (peer) == BGP_PEER_IBGP |
| 2097 | || peer_sort (peer) == BGP_PEER_CONFED) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2098 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2099 | if (attre->ecommunity->size * 8 > 255) |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2100 | { |
| 2101 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN); |
| 2102 | stream_putc (s, BGP_ATTR_EXT_COMMUNITIES); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2103 | stream_putw (s, attre->ecommunity->size * 8); |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2104 | } |
| 2105 | else |
| 2106 | { |
| 2107 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 2108 | stream_putc (s, BGP_ATTR_EXT_COMMUNITIES); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2109 | stream_putc (s, attre->ecommunity->size * 8); |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2110 | } |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2111 | stream_put (s, attre->ecommunity->val, attre->ecommunity->size * 8); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2112 | } |
| 2113 | else |
| 2114 | { |
paul | 5228ad2 | 2004-06-04 17:58:18 +0000 | [diff] [blame] | 2115 | u_int8_t *pnt; |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2116 | int tbit; |
| 2117 | int ecom_tr_size = 0; |
| 2118 | int i; |
| 2119 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2120 | for (i = 0; i < attre->ecommunity->size; i++) |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2121 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2122 | pnt = attre->ecommunity->val + (i * 8); |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2123 | tbit = *pnt; |
| 2124 | |
| 2125 | if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE)) |
| 2126 | continue; |
| 2127 | |
| 2128 | ecom_tr_size++; |
| 2129 | } |
| 2130 | |
| 2131 | if (ecom_tr_size) |
| 2132 | { |
| 2133 | if (ecom_tr_size * 8 > 255) |
| 2134 | { |
| 2135 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN); |
| 2136 | stream_putc (s, BGP_ATTR_EXT_COMMUNITIES); |
| 2137 | stream_putw (s, ecom_tr_size * 8); |
| 2138 | } |
| 2139 | else |
| 2140 | { |
| 2141 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 2142 | stream_putc (s, BGP_ATTR_EXT_COMMUNITIES); |
| 2143 | stream_putc (s, ecom_tr_size * 8); |
| 2144 | } |
| 2145 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2146 | for (i = 0; i < attre->ecommunity->size; i++) |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2147 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2148 | pnt = attre->ecommunity->val + (i * 8); |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 2149 | tbit = *pnt; |
| 2150 | |
| 2151 | if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE)) |
| 2152 | continue; |
| 2153 | |
| 2154 | stream_put (s, pnt, 8); |
| 2155 | } |
| 2156 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2157 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2158 | } |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2159 | |
| 2160 | if ( send_as4_path ) |
| 2161 | { |
| 2162 | /* If the peer is NOT As4 capable, AND */ |
| 2163 | /* there are ASnums > 65535 in path THEN |
| 2164 | * give out AS4_PATH */ |
| 2165 | |
| 2166 | /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET |
| 2167 | * path segments! |
| 2168 | * Hm, I wonder... confederation things *should* only be at |
| 2169 | * the beginning of an aspath, right? Then we should use |
| 2170 | * aspath_delete_confed_seq for this, because it is already |
| 2171 | * there! (JK) |
| 2172 | * Folks, talk to me: what is reasonable here!? |
| 2173 | */ |
| 2174 | aspath = aspath_delete_confed_seq (aspath); |
| 2175 | |
| 2176 | stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN); |
| 2177 | stream_putc (s, BGP_ATTR_AS4_PATH); |
| 2178 | aspath_sizep = stream_get_endp (s); |
| 2179 | stream_putw (s, 0); |
| 2180 | stream_putw_at (s, aspath_sizep, aspath_put (s, aspath, 1)); |
| 2181 | } |
| 2182 | |
| 2183 | if (aspath != attr->aspath) |
| 2184 | aspath_free (aspath); |
| 2185 | |
| 2186 | if ( send_as4_aggregator ) |
| 2187 | { |
| 2188 | assert (attr->extra); |
| 2189 | |
| 2190 | /* send AS4_AGGREGATOR, at this place */ |
| 2191 | /* this section of code moved here in order to ensure the correct |
| 2192 | * *ascending* order of attributes |
| 2193 | */ |
| 2194 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 2195 | stream_putc (s, BGP_ATTR_AS4_AGGREGATOR); |
| 2196 | stream_putc (s, 8); |
| 2197 | stream_putl (s, attr->extra->aggregator_as); |
| 2198 | stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr); |
| 2199 | } |
Paul Jakma | 4136717 | 2007-08-06 15:24:51 +0000 | [diff] [blame] | 2200 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2201 | /* Unknown transit attribute. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2202 | if (attr->extra && attr->extra->transit) |
| 2203 | stream_put (s, attr->extra->transit->val, attr->extra->transit->length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2204 | |
| 2205 | /* Return total size of attribute. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2206 | return stream_get_endp (s) - cp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | bgp_size_t |
| 2210 | bgp_packet_withdraw (struct peer *peer, struct stream *s, struct prefix *p, |
| 2211 | afi_t afi, safi_t safi, struct prefix_rd *prd, |
Paul Jakma | a3b6ea5 | 2006-05-04 07:52:12 +0000 | [diff] [blame] | 2212 | u_char *tag) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2213 | { |
| 2214 | unsigned long cp; |
| 2215 | unsigned long attrlen_pnt; |
| 2216 | bgp_size_t size; |
| 2217 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2218 | cp = stream_get_endp (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2219 | |
| 2220 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); |
| 2221 | stream_putc (s, BGP_ATTR_MP_UNREACH_NLRI); |
| 2222 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2223 | attrlen_pnt = stream_get_endp (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2224 | stream_putc (s, 0); /* Length of this attribute. */ |
| 2225 | |
| 2226 | stream_putw (s, family2afi (p->family)); |
| 2227 | |
| 2228 | if (safi == SAFI_MPLS_VPN) |
| 2229 | { |
| 2230 | /* SAFI */ |
| 2231 | stream_putc (s, BGP_SAFI_VPNV4); |
| 2232 | |
| 2233 | /* prefix. */ |
| 2234 | stream_putc (s, p->prefixlen + 88); |
| 2235 | stream_put (s, tag, 3); |
| 2236 | stream_put (s, prd->val, 8); |
| 2237 | stream_put (s, &p->u.prefix, PSIZE (p->prefixlen)); |
| 2238 | } |
| 2239 | else |
| 2240 | { |
| 2241 | /* SAFI */ |
| 2242 | stream_putc (s, safi); |
| 2243 | |
| 2244 | /* prefix */ |
| 2245 | stream_put_prefix (s, p); |
| 2246 | } |
| 2247 | |
| 2248 | /* Set MP attribute length. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2249 | size = stream_get_endp (s) - attrlen_pnt - 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2250 | stream_putc_at (s, attrlen_pnt, size); |
| 2251 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2252 | return stream_get_endp (s) - cp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | /* Initialization of attribute. */ |
| 2256 | void |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 2257 | bgp_attr_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2258 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2259 | aspath_init (); |
| 2260 | attrhash_init (); |
| 2261 | community_init (); |
| 2262 | ecommunity_init (); |
| 2263 | cluster_init (); |
| 2264 | transit_init (); |
| 2265 | } |
| 2266 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 2267 | void |
| 2268 | bgp_attr_finish (void) |
| 2269 | { |
| 2270 | aspath_finish (); |
| 2271 | attrhash_finish (); |
| 2272 | community_finish (); |
| 2273 | ecommunity_finish (); |
| 2274 | cluster_finish (); |
| 2275 | transit_finish (); |
| 2276 | } |
| 2277 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2278 | /* Make attribute packet. */ |
| 2279 | void |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 2280 | bgp_dump_routes_attr (struct stream *s, struct attr *attr, |
| 2281 | struct prefix *prefix) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2282 | { |
| 2283 | unsigned long cp; |
| 2284 | unsigned long len; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2285 | size_t aspath_lenp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2286 | struct aspath *aspath; |
| 2287 | |
| 2288 | /* Remember current pointer. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2289 | cp = stream_get_endp (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2290 | |
| 2291 | /* Place holder of length. */ |
| 2292 | stream_putw (s, 0); |
| 2293 | |
| 2294 | /* Origin attribute. */ |
| 2295 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 2296 | stream_putc (s, BGP_ATTR_ORIGIN); |
| 2297 | stream_putc (s, 1); |
| 2298 | stream_putc (s, attr->origin); |
| 2299 | |
| 2300 | aspath = attr->aspath; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2301 | |
| 2302 | stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN); |
| 2303 | stream_putc (s, BGP_ATTR_AS_PATH); |
| 2304 | aspath_lenp = stream_get_endp (s); |
| 2305 | stream_putw (s, 0); |
| 2306 | |
| 2307 | stream_putw_at (s, aspath_lenp, aspath_put (s, aspath, 1)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2308 | |
| 2309 | /* Nexthop attribute. */ |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 2310 | /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */ |
| 2311 | if(prefix != NULL |
| 2312 | #ifdef HAVE_IPV6 |
| 2313 | && prefix->family != AF_INET6 |
| 2314 | #endif /* HAVE_IPV6 */ |
| 2315 | ) |
| 2316 | { |
| 2317 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 2318 | stream_putc (s, BGP_ATTR_NEXT_HOP); |
| 2319 | stream_putc (s, 4); |
| 2320 | stream_put_ipv4 (s, attr->nexthop.s_addr); |
| 2321 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2322 | |
| 2323 | /* MED attribute. */ |
| 2324 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC)) |
| 2325 | { |
| 2326 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); |
| 2327 | stream_putc (s, BGP_ATTR_MULTI_EXIT_DISC); |
| 2328 | stream_putc (s, 4); |
| 2329 | stream_putl (s, attr->med); |
| 2330 | } |
| 2331 | |
| 2332 | /* Local preference. */ |
| 2333 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF)) |
| 2334 | { |
| 2335 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 2336 | stream_putc (s, BGP_ATTR_LOCAL_PREF); |
| 2337 | stream_putc (s, 4); |
| 2338 | stream_putl (s, attr->local_pref); |
| 2339 | } |
| 2340 | |
| 2341 | /* Atomic aggregate. */ |
| 2342 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE)) |
| 2343 | { |
| 2344 | stream_putc (s, BGP_ATTR_FLAG_TRANS); |
| 2345 | stream_putc (s, BGP_ATTR_ATOMIC_AGGREGATE); |
| 2346 | stream_putc (s, 0); |
| 2347 | } |
| 2348 | |
| 2349 | /* Aggregator. */ |
| 2350 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR)) |
| 2351 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2352 | assert (attr->extra); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2353 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 2354 | stream_putc (s, BGP_ATTR_AGGREGATOR); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 2355 | stream_putc (s, 8); |
| 2356 | stream_putl (s, attr->extra->aggregator_as); |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2357 | stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | /* Community attribute. */ |
| 2361 | if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES)) |
| 2362 | { |
| 2363 | if (attr->community->size * 4 > 255) |
| 2364 | { |
| 2365 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN); |
| 2366 | stream_putc (s, BGP_ATTR_COMMUNITIES); |
| 2367 | stream_putw (s, attr->community->size * 4); |
| 2368 | } |
| 2369 | else |
| 2370 | { |
| 2371 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS); |
| 2372 | stream_putc (s, BGP_ATTR_COMMUNITIES); |
| 2373 | stream_putc (s, attr->community->size * 4); |
| 2374 | } |
| 2375 | stream_put (s, attr->community->val, attr->community->size * 4); |
| 2376 | } |
| 2377 | |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 2378 | #ifdef HAVE_IPV6 |
| 2379 | /* Add a MP_NLRI attribute to dump the IPv6 next hop */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2380 | if (prefix != NULL && prefix->family == AF_INET6 && attr->extra && |
| 2381 | (attr->extra->mp_nexthop_len == 16 || attr->extra->mp_nexthop_len == 32) ) |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 2382 | { |
| 2383 | int sizep; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2384 | struct attr_extra *attre = attr->extra; |
| 2385 | |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 2386 | stream_putc(s, BGP_ATTR_FLAG_OPTIONAL); |
| 2387 | stream_putc(s, BGP_ATTR_MP_REACH_NLRI); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2388 | sizep = stream_get_endp (s); |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 2389 | |
| 2390 | /* MP header */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2391 | stream_putc (s, 0); /* Marker: Attribute length. */ |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 2392 | stream_putw(s, AFI_IP6); /* AFI */ |
| 2393 | stream_putc(s, SAFI_UNICAST); /* SAFI */ |
| 2394 | |
| 2395 | /* Next hop */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 2396 | stream_putc(s, attre->mp_nexthop_len); |
| 2397 | stream_put(s, &attre->mp_nexthop_global, 16); |
| 2398 | if (attre->mp_nexthop_len == 32) |
| 2399 | stream_put(s, &attre->mp_nexthop_local, 16); |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 2400 | |
| 2401 | /* SNPA */ |
| 2402 | stream_putc(s, 0); |
| 2403 | |
| 2404 | /* Prefix */ |
| 2405 | stream_put_prefix(s, prefix); |
| 2406 | |
| 2407 | /* Set MP attribute length. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2408 | stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1); |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 2409 | } |
| 2410 | #endif /* HAVE_IPV6 */ |
| 2411 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2412 | /* Return total size of attribute. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2413 | len = stream_get_endp (s) - cp - 2; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2414 | stream_putw_at (s, cp, len); |
| 2415 | } |