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