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