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