blob: 98571dab5cbed20268d606d4275d9d7c85744515 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* BGP attributes management routines.
2 Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "linklist.h"
24#include "prefix.h"
25#include "memory.h"
26#include "vector.h"
27#include "vty.h"
28#include "stream.h"
29#include "log.h"
30#include "hash.h"
Stephen Hemmingerc8e7b892010-08-27 14:12:54 -070031#include "jhash.h"
Donald Sharp04907292016-01-07 10:03:01 -050032#include "filter.h"
paul718e3742002-12-13 20:15:29 +000033
34#include "bgpd/bgpd.h"
35#include "bgpd/bgp_attr.h"
36#include "bgpd/bgp_route.h"
37#include "bgpd/bgp_aspath.h"
38#include "bgpd/bgp_community.h"
39#include "bgpd/bgp_debug.h"
40#include "bgpd/bgp_packet.h"
41#include "bgpd/bgp_ecommunity.h"
Lou Bergerc3741782016-01-12 13:42:01 -050042#include "table.h"
43#include "bgp_encap_types.h"
David Lamparter6b0655a2014-06-04 06:53:35 +020044
paul718e3742002-12-13 20:15:29 +000045/* Attribute strings for logging. */
Stephen Hemminger9bddac42009-05-15 09:59:51 -070046static const struct message attr_str [] =
paul718e3742002-12-13 20:15:29 +000047{
48 { BGP_ATTR_ORIGIN, "ORIGIN" },
49 { BGP_ATTR_AS_PATH, "AS_PATH" },
50 { BGP_ATTR_NEXT_HOP, "NEXT_HOP" },
51 { BGP_ATTR_MULTI_EXIT_DISC, "MULTI_EXIT_DISC" },
52 { BGP_ATTR_LOCAL_PREF, "LOCAL_PREF" },
53 { BGP_ATTR_ATOMIC_AGGREGATE, "ATOMIC_AGGREGATE" },
54 { BGP_ATTR_AGGREGATOR, "AGGREGATOR" },
55 { BGP_ATTR_COMMUNITIES, "COMMUNITY" },
56 { BGP_ATTR_ORIGINATOR_ID, "ORIGINATOR_ID" },
Denis Ovsienkof8627ff2011-10-10 16:52:20 +040057 { BGP_ATTR_CLUSTER_LIST, "CLUSTER_LIST" },
paul718e3742002-12-13 20:15:29 +000058 { BGP_ATTR_DPA, "DPA" },
59 { BGP_ATTR_ADVERTISER, "ADVERTISER"} ,
60 { BGP_ATTR_RCID_PATH, "RCID_PATH" },
61 { BGP_ATTR_MP_REACH_NLRI, "MP_REACH_NLRI" },
62 { BGP_ATTR_MP_UNREACH_NLRI, "MP_UNREACH_NLRI" },
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000063 { BGP_ATTR_EXT_COMMUNITIES, "EXT_COMMUNITIES" },
64 { BGP_ATTR_AS4_PATH, "AS4_PATH" },
65 { BGP_ATTR_AS4_AGGREGATOR, "AS4_AGGREGATOR" },
66 { BGP_ATTR_AS_PATHLIMIT, "AS_PATHLIMIT" },
Lou Bergerc3741782016-01-12 13:42:01 -050067 { BGP_ATTR_ENCAP, "ENCAP" },
paul718e3742002-12-13 20:15:29 +000068};
Balaji.G837d16c2012-09-26 14:09:10 +053069static const int attr_str_max = array_size(attr_str);
Denis Ovsienkoafcb7672011-10-23 22:32:44 +040070
71static const struct message attr_flag_str[] =
72{
73 { BGP_ATTR_FLAG_OPTIONAL, "Optional" },
74 { BGP_ATTR_FLAG_TRANS, "Transitive" },
75 { BGP_ATTR_FLAG_PARTIAL, "Partial" },
76 /* bgp_attr_flags_diagnose() relies on this bit being last in this list */
77 { BGP_ATTR_FLAG_EXTLEN, "Extended Length" },
78};
David Lamparter6b0655a2014-06-04 06:53:35 +020079
Stephen Hemminger9bddac42009-05-15 09:59:51 -070080static struct hash *cluster_hash;
paul718e3742002-12-13 20:15:29 +000081
paul94f2b392005-06-28 12:44:16 +000082static void *
Paul Jakma923de652007-04-29 18:25:17 +000083cluster_hash_alloc (void *p)
paul718e3742002-12-13 20:15:29 +000084{
Paul Jakma923de652007-04-29 18:25:17 +000085 struct cluster_list * val = (struct cluster_list *) p;
paul718e3742002-12-13 20:15:29 +000086 struct cluster_list *cluster;
87
88 cluster = XMALLOC (MTYPE_CLUSTER, sizeof (struct cluster_list));
89 cluster->length = val->length;
90
91 if (cluster->length)
92 {
93 cluster->list = XMALLOC (MTYPE_CLUSTER_VAL, val->length);
94 memcpy (cluster->list, val->list, val->length);
95 }
96 else
97 cluster->list = NULL;
98
99 cluster->refcnt = 0;
100
101 return cluster;
102}
103
104/* Cluster list related functions. */
paul94f2b392005-06-28 12:44:16 +0000105static struct cluster_list *
paul5228ad22004-06-04 17:58:18 +0000106cluster_parse (struct in_addr * pnt, int length)
paul718e3742002-12-13 20:15:29 +0000107{
108 struct cluster_list tmp;
109 struct cluster_list *cluster;
110
111 tmp.length = length;
paul5228ad22004-06-04 17:58:18 +0000112 tmp.list = pnt;
paul718e3742002-12-13 20:15:29 +0000113
114 cluster = hash_get (cluster_hash, &tmp, cluster_hash_alloc);
115 cluster->refcnt++;
116 return cluster;
117}
118
119int
120cluster_loop_check (struct cluster_list *cluster, struct in_addr originator)
121{
122 int i;
123
124 for (i = 0; i < cluster->length / 4; i++)
125 if (cluster->list[i].s_addr == originator.s_addr)
126 return 1;
127 return 0;
128}
129
paul94f2b392005-06-28 12:44:16 +0000130static unsigned int
Paul Jakma923de652007-04-29 18:25:17 +0000131cluster_hash_key_make (void *p)
paul718e3742002-12-13 20:15:29 +0000132{
Stephen Hemmingerc8e7b892010-08-27 14:12:54 -0700133 const struct cluster_list *cluster = p;
paul718e3742002-12-13 20:15:29 +0000134
Stephen Hemmingerc8e7b892010-08-27 14:12:54 -0700135 return jhash(cluster->list, cluster->length, 0);
paul718e3742002-12-13 20:15:29 +0000136}
137
paul94f2b392005-06-28 12:44:16 +0000138static int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +0100139cluster_hash_cmp (const void *p1, const void *p2)
paul718e3742002-12-13 20:15:29 +0000140{
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +0100141 const struct cluster_list * cluster1 = p1;
142 const struct cluster_list * cluster2 = p2;
Paul Jakma923de652007-04-29 18:25:17 +0000143
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +0100144 return (cluster1->length == cluster2->length &&
145 memcmp (cluster1->list, cluster2->list, cluster1->length) == 0);
paul718e3742002-12-13 20:15:29 +0000146}
147
paul94f2b392005-06-28 12:44:16 +0000148static void
paul718e3742002-12-13 20:15:29 +0000149cluster_free (struct cluster_list *cluster)
150{
151 if (cluster->list)
152 XFREE (MTYPE_CLUSTER_VAL, cluster->list);
153 XFREE (MTYPE_CLUSTER, cluster);
154}
155
Chris Caputo228da422009-07-18 05:44:03 +0000156#if 0
paul94f2b392005-06-28 12:44:16 +0000157static struct cluster_list *
paul718e3742002-12-13 20:15:29 +0000158cluster_dup (struct cluster_list *cluster)
159{
160 struct cluster_list *new;
161
Stephen Hemminger393deb92008-08-18 14:13:29 -0700162 new = XCALLOC (MTYPE_CLUSTER, sizeof (struct cluster_list));
paul718e3742002-12-13 20:15:29 +0000163 new->length = cluster->length;
164
165 if (cluster->length)
166 {
167 new->list = XMALLOC (MTYPE_CLUSTER_VAL, cluster->length);
168 memcpy (new->list, cluster->list, cluster->length);
169 }
170 else
171 new->list = NULL;
172
173 return new;
174}
Chris Caputo228da422009-07-18 05:44:03 +0000175#endif
paul718e3742002-12-13 20:15:29 +0000176
paul94f2b392005-06-28 12:44:16 +0000177static struct cluster_list *
paul718e3742002-12-13 20:15:29 +0000178cluster_intern (struct cluster_list *cluster)
179{
180 struct cluster_list *find;
181
182 find = hash_get (cluster_hash, cluster, cluster_hash_alloc);
183 find->refcnt++;
184
185 return find;
186}
187
188void
189cluster_unintern (struct cluster_list *cluster)
190{
paul718e3742002-12-13 20:15:29 +0000191 if (cluster->refcnt)
192 cluster->refcnt--;
193
194 if (cluster->refcnt == 0)
195 {
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400196 hash_release (cluster_hash, cluster);
paul718e3742002-12-13 20:15:29 +0000197 cluster_free (cluster);
198 }
199}
200
paul94f2b392005-06-28 12:44:16 +0000201static void
202cluster_init (void)
paul718e3742002-12-13 20:15:29 +0000203{
204 cluster_hash = hash_create (cluster_hash_key_make, cluster_hash_cmp);
205}
Chris Caputo228da422009-07-18 05:44:03 +0000206
207static void
208cluster_finish (void)
209{
Lou Berger056f3762013-04-10 12:30:04 -0700210 hash_clean (cluster_hash, (void (*)(void *))cluster_free);
Chris Caputo228da422009-07-18 05:44:03 +0000211 hash_free (cluster_hash);
212 cluster_hash = NULL;
213}
David Lamparter6b0655a2014-06-04 06:53:35 +0200214
Lou Bergerc3741782016-01-12 13:42:01 -0500215struct bgp_attr_encap_subtlv *
216encap_tlv_dup(struct bgp_attr_encap_subtlv *orig)
217{
218 struct bgp_attr_encap_subtlv *new;
219 struct bgp_attr_encap_subtlv *tail;
220 struct bgp_attr_encap_subtlv *p;
221
222 for (p = orig, tail = new = NULL; p; p = p->next) {
223 int size = sizeof(struct bgp_attr_encap_subtlv) - 1 + p->length;
224 if (tail) {
225 tail->next = XCALLOC(MTYPE_ENCAP_TLV, size);
226 tail = tail->next;
227 } else {
228 tail = new = XCALLOC(MTYPE_ENCAP_TLV, size);
229 }
230 assert(tail);
231 memcpy(tail, p, size);
232 tail->next = NULL;
233 }
234
235 return new;
236}
237
238static void
239encap_free(struct bgp_attr_encap_subtlv *p)
240{
241 struct bgp_attr_encap_subtlv *next;
242 while (p) {
243 next = p->next;
244 p->next = NULL;
245 XFREE(MTYPE_ENCAP_TLV, p);
246 p = next;
247 }
248}
249
250void
251bgp_attr_flush_encap(struct attr *attr)
252{
253 if (!attr || !attr->extra)
254 return;
255
256 if (attr->extra->encap_subtlvs) {
257 encap_free(attr->extra->encap_subtlvs);
258 attr->extra->encap_subtlvs = NULL;
259 }
260}
261
262/*
263 * Compare encap sub-tlv chains
264 *
265 * 1 = equivalent
266 * 0 = not equivalent
267 *
268 * This algorithm could be made faster if needed
269 */
270static int
271encap_same(struct bgp_attr_encap_subtlv *h1, struct bgp_attr_encap_subtlv *h2)
272{
273 struct bgp_attr_encap_subtlv *p;
274 struct bgp_attr_encap_subtlv *q;
275
276 if (!h1 && !h2)
277 return 1;
278 if (h1 && !h2)
279 return 0;
280 if (!h1 && h2)
281 return 0;
282 if (h1 == h2)
283 return 1;
284
285 for (p = h1; p; p = p->next) {
286 for (q = h2; q; q = q->next) {
287 if ((p->type == q->type) &&
288 (p->length == q->length) &&
289 !memcmp(p->value, q->value, p->length)) {
290
291 break;
292 }
293 }
294 if (!q)
295 return 0;
296 }
297
298 for (p = h2; p; p = p->next) {
299 for (q = h1; q; q = q->next) {
300 if ((p->type == q->type) &&
301 (p->length == q->length) &&
302 !memcmp(p->value, q->value, p->length)) {
303
304 break;
305 }
306 }
307 if (!q)
308 return 0;
309 }
310
311 return 1;
312}
313
paul718e3742002-12-13 20:15:29 +0000314/* Unknown transit attribute. */
Stephen Hemminger9bddac42009-05-15 09:59:51 -0700315static struct hash *transit_hash;
paul718e3742002-12-13 20:15:29 +0000316
paul94f2b392005-06-28 12:44:16 +0000317static void
paul718e3742002-12-13 20:15:29 +0000318transit_free (struct transit *transit)
319{
320 if (transit->val)
321 XFREE (MTYPE_TRANSIT_VAL, transit->val);
322 XFREE (MTYPE_TRANSIT, transit);
323}
324
Paul Jakma923de652007-04-29 18:25:17 +0000325
paul94f2b392005-06-28 12:44:16 +0000326static void *
Paul Jakma923de652007-04-29 18:25:17 +0000327transit_hash_alloc (void *p)
paul718e3742002-12-13 20:15:29 +0000328{
329 /* Transit structure is already allocated. */
Paul Jakma923de652007-04-29 18:25:17 +0000330 return p;
paul718e3742002-12-13 20:15:29 +0000331}
332
paul94f2b392005-06-28 12:44:16 +0000333static struct transit *
paul718e3742002-12-13 20:15:29 +0000334transit_intern (struct transit *transit)
335{
336 struct transit *find;
337
338 find = hash_get (transit_hash, transit, transit_hash_alloc);
339 if (find != transit)
340 transit_free (transit);
341 find->refcnt++;
342
343 return find;
344}
345
346void
347transit_unintern (struct transit *transit)
348{
paul718e3742002-12-13 20:15:29 +0000349 if (transit->refcnt)
350 transit->refcnt--;
351
352 if (transit->refcnt == 0)
353 {
Stephen Hemminger9206f9e2011-12-18 19:43:40 +0400354 hash_release (transit_hash, transit);
paul718e3742002-12-13 20:15:29 +0000355 transit_free (transit);
356 }
357}
358
paul94f2b392005-06-28 12:44:16 +0000359static unsigned int
Paul Jakma923de652007-04-29 18:25:17 +0000360transit_hash_key_make (void *p)
paul718e3742002-12-13 20:15:29 +0000361{
Stephen Hemmingerc8e7b892010-08-27 14:12:54 -0700362 const struct transit * transit = p;
paul718e3742002-12-13 20:15:29 +0000363
Stephen Hemmingerc8e7b892010-08-27 14:12:54 -0700364 return jhash(transit->val, transit->length, 0);
paul718e3742002-12-13 20:15:29 +0000365}
366
paul94f2b392005-06-28 12:44:16 +0000367static int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +0100368transit_hash_cmp (const void *p1, const void *p2)
paul718e3742002-12-13 20:15:29 +0000369{
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +0100370 const struct transit * transit1 = p1;
371 const struct transit * transit2 = p2;
Paul Jakma923de652007-04-29 18:25:17 +0000372
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +0100373 return (transit1->length == transit2->length &&
374 memcmp (transit1->val, transit2->val, transit1->length) == 0);
paul718e3742002-12-13 20:15:29 +0000375}
376
paul94f2b392005-06-28 12:44:16 +0000377static void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800378transit_init (void)
paul718e3742002-12-13 20:15:29 +0000379{
380 transit_hash = hash_create (transit_hash_key_make, transit_hash_cmp);
381}
Chris Caputo228da422009-07-18 05:44:03 +0000382
383static void
384transit_finish (void)
385{
Lou Berger056f3762013-04-10 12:30:04 -0700386 hash_clean (transit_hash, (void (*)(void *))transit_free);
Chris Caputo228da422009-07-18 05:44:03 +0000387 hash_free (transit_hash);
388 transit_hash = NULL;
389}
David Lamparter6b0655a2014-06-04 06:53:35 +0200390
paul718e3742002-12-13 20:15:29 +0000391/* Attribute hash routines. */
Stephen Hemminger9bddac42009-05-15 09:59:51 -0700392static struct hash *attrhash;
paul718e3742002-12-13 20:15:29 +0000393
Paul Jakmafb982c22007-05-04 20:15:47 +0000394static struct attr_extra *
395bgp_attr_extra_new (void)
396{
397 return XCALLOC (MTYPE_ATTR_EXTRA, sizeof (struct attr_extra));
398}
399
400void
401bgp_attr_extra_free (struct attr *attr)
402{
403 if (attr->extra)
404 {
Lou Bergerc3741782016-01-12 13:42:01 -0500405 if (attr->extra->encap_subtlvs) {
406 encap_free(attr->extra->encap_subtlvs);
407 attr->extra->encap_subtlvs = NULL;
408 }
Paul Jakmafb982c22007-05-04 20:15:47 +0000409 XFREE (MTYPE_ATTR_EXTRA, attr->extra);
410 attr->extra = NULL;
411 }
412}
413
414struct attr_extra *
415bgp_attr_extra_get (struct attr *attr)
416{
417 if (!attr->extra)
418 attr->extra = bgp_attr_extra_new();
419 return attr->extra;
420}
421
422/* Shallow copy of an attribute
423 * Though, not so shallow that it doesn't copy the contents
424 * of the attr_extra pointed to by 'extra'
425 */
426void
427bgp_attr_dup (struct attr *new, struct attr *orig)
428{
Jorge Boncompte [DTI2]558d1fe2012-05-07 16:53:05 +0000429 struct attr_extra *extra = new->extra;
430
Paul Jakmafb982c22007-05-04 20:15:47 +0000431 *new = *orig;
Christian Frankea0de1d12012-12-07 16:35:00 +0000432 /* if caller provided attr_extra space, use it in any case.
433 *
434 * This is neccesary even if orig->extra equals NULL, because otherwise
435 * memory may be later allocated on the heap by bgp_attr_extra_get.
436 *
437 * That memory would eventually be leaked, because the caller must not
438 * call bgp_attr_extra_free if he provided attr_extra on the stack.
439 */
440 if (extra)
Paul Jakmafb982c22007-05-04 20:15:47 +0000441 {
Christian Frankea0de1d12012-12-07 16:35:00 +0000442 new->extra = extra;
443 memset(new->extra, 0, sizeof(struct attr_extra));
Lou Bergerc3741782016-01-12 13:42:01 -0500444 if (orig->extra) {
Christian Frankea0de1d12012-12-07 16:35:00 +0000445 *new->extra = *orig->extra;
Lou Bergerc3741782016-01-12 13:42:01 -0500446 if (orig->extra->encap_subtlvs) {
447 new->extra->encap_subtlvs = encap_tlv_dup(orig->extra->encap_subtlvs);
448 }
449 }
Christian Frankea0de1d12012-12-07 16:35:00 +0000450 }
451 else if (orig->extra)
452 {
453 new->extra = bgp_attr_extra_new();
Paul Jakmafb982c22007-05-04 20:15:47 +0000454 *new->extra = *orig->extra;
Lou Bergerc3741782016-01-12 13:42:01 -0500455 if (orig->extra->encap_subtlvs) {
456 new->extra->encap_subtlvs = encap_tlv_dup(orig->extra->encap_subtlvs);
457 }
Paul Jakmafb982c22007-05-04 20:15:47 +0000458 }
459}
460
Paul Jakmacbdfbaa2006-03-30 13:20:48 +0000461unsigned long int
462attr_count (void)
463{
464 return attrhash->count;
465}
466
467unsigned long int
468attr_unknown_count (void)
469{
470 return transit_hash->count;
471}
472
paul718e3742002-12-13 20:15:29 +0000473unsigned int
Paul Jakma923de652007-04-29 18:25:17 +0000474attrhash_key_make (void *p)
paul718e3742002-12-13 20:15:29 +0000475{
Jorge Boncompte [DTI2]7fb0cd82012-05-07 16:52:58 +0000476 const struct attr *attr = (struct attr *) p;
477 const struct attr_extra *extra = attr->extra;
Stephen Hemmingerc8e7b892010-08-27 14:12:54 -0700478 uint32_t key = 0;
479#define MIX(val) key = jhash_1word(val, key)
paul718e3742002-12-13 20:15:29 +0000480
Stephen Hemmingerc8e7b892010-08-27 14:12:54 -0700481 MIX(attr->origin);
482 MIX(attr->nexthop.s_addr);
483 MIX(attr->med);
484 MIX(attr->local_pref);
485
Paul Jakmac8f3fe32010-12-05 20:28:02 +0000486 key += attr->origin;
487 key += attr->nexthop.s_addr;
488 key += attr->med;
489 key += attr->local_pref;
Paul Jakmafb982c22007-05-04 20:15:47 +0000490
Jorge Boncompte [DTI2]7fb0cd82012-05-07 16:52:58 +0000491 if (extra)
Paul Jakmafb982c22007-05-04 20:15:47 +0000492 {
Jorge Boncompte [DTI2]7fb0cd82012-05-07 16:52:58 +0000493 MIX(extra->aggregator_as);
494 MIX(extra->aggregator_addr.s_addr);
495 MIX(extra->weight);
496 MIX(extra->mp_nexthop_global_in.s_addr);
Pradosh Mohapatra689bb662013-09-07 07:13:37 +0000497 MIX(extra->originator_id.s_addr);
Paul Jakmafb982c22007-05-04 20:15:47 +0000498 }
499
paul718e3742002-12-13 20:15:29 +0000500 if (attr->aspath)
Stephen Hemmingerc8e7b892010-08-27 14:12:54 -0700501 MIX(aspath_key_make (attr->aspath));
paul718e3742002-12-13 20:15:29 +0000502 if (attr->community)
Stephen Hemmingerc8e7b892010-08-27 14:12:54 -0700503 MIX(community_hash_make (attr->community));
Paul Jakmafb982c22007-05-04 20:15:47 +0000504
Jorge Boncompte [DTI2]7fb0cd82012-05-07 16:52:58 +0000505 if (extra)
Paul Jakmafb982c22007-05-04 20:15:47 +0000506 {
Jorge Boncompte [DTI2]7fb0cd82012-05-07 16:52:58 +0000507 if (extra->ecommunity)
508 MIX(ecommunity_hash_make (extra->ecommunity));
509 if (extra->cluster)
510 MIX(cluster_hash_key_make (extra->cluster));
511 if (extra->transit)
512 MIX(transit_hash_key_make (extra->transit));
paul718e3742002-12-13 20:15:29 +0000513
Jorge Boncompte [DTI2]7fb0cd82012-05-07 16:52:58 +0000514 MIX(extra->mp_nexthop_len);
515 key = jhash(extra->mp_nexthop_global.s6_addr, 16, key);
516 key = jhash(extra->mp_nexthop_local.s6_addr, 16, key);
Paul Jakmafb982c22007-05-04 20:15:47 +0000517 }
paul718e3742002-12-13 20:15:29 +0000518
519 return key;
520}
521
522int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +0100523attrhash_cmp (const void *p1, const void *p2)
paul718e3742002-12-13 20:15:29 +0000524{
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +0100525 const struct attr * attr1 = p1;
526 const struct attr * attr2 = p2;
Paul Jakma923de652007-04-29 18:25:17 +0000527
paul718e3742002-12-13 20:15:29 +0000528 if (attr1->flag == attr2->flag
529 && attr1->origin == attr2->origin
530 && attr1->nexthop.s_addr == attr2->nexthop.s_addr
paul718e3742002-12-13 20:15:29 +0000531 && attr1->aspath == attr2->aspath
532 && attr1->community == attr2->community
Paul Jakmafb982c22007-05-04 20:15:47 +0000533 && attr1->med == attr2->med
Paul Jakmac8f3fe32010-12-05 20:28:02 +0000534 && attr1->local_pref == attr2->local_pref)
Paul Jakmafb982c22007-05-04 20:15:47 +0000535 {
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +0100536 const struct attr_extra *ae1 = attr1->extra;
537 const struct attr_extra *ae2 = attr2->extra;
Paul Jakmafb982c22007-05-04 20:15:47 +0000538
539 if (ae1 && ae2
540 && ae1->aggregator_as == ae2->aggregator_as
541 && ae1->aggregator_addr.s_addr == ae2->aggregator_addr.s_addr
542 && ae1->weight == ae2->weight
Paul Jakmafb982c22007-05-04 20:15:47 +0000543 && ae1->mp_nexthop_len == ae2->mp_nexthop_len
544 && IPV6_ADDR_SAME (&ae1->mp_nexthop_global, &ae2->mp_nexthop_global)
545 && IPV6_ADDR_SAME (&ae1->mp_nexthop_local, &ae2->mp_nexthop_local)
Paul Jakmafb982c22007-05-04 20:15:47 +0000546 && IPV4_ADDR_SAME (&ae1->mp_nexthop_global_in, &ae2->mp_nexthop_global_in)
547 && ae1->ecommunity == ae2->ecommunity
548 && ae1->cluster == ae2->cluster
Pradosh Mohapatra689bb662013-09-07 07:13:37 +0000549 && ae1->transit == ae2->transit
Lou Bergerc3741782016-01-12 13:42:01 -0500550 && (ae1->encap_tunneltype == ae2->encap_tunneltype)
551 && encap_same(ae1->encap_subtlvs, ae2->encap_subtlvs)
Pradosh Mohapatra689bb662013-09-07 07:13:37 +0000552 && IPV4_ADDR_SAME (&ae1->originator_id, &ae2->originator_id))
Paul Jakmafb982c22007-05-04 20:15:47 +0000553 return 1;
554 else if (ae1 || ae2)
555 return 0;
556 /* neither attribute has extra attributes, so they're same */
557 return 1;
558 }
paul718e3742002-12-13 20:15:29 +0000559 else
560 return 0;
561}
562
paul94f2b392005-06-28 12:44:16 +0000563static void
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +0100564attrhash_init (void)
paul718e3742002-12-13 20:15:29 +0000565{
566 attrhash = hash_create (attrhash_key_make, attrhash_cmp);
567}
568
Lou Berger056f3762013-04-10 12:30:04 -0700569/*
570 * special for hash_clean below
571 */
572static void
573attr_vfree (void *attr)
574{
575 bgp_attr_extra_free ((struct attr *)attr);
576 XFREE (MTYPE_ATTR, attr);
577}
578
paul94f2b392005-06-28 12:44:16 +0000579static void
Chris Caputo228da422009-07-18 05:44:03 +0000580attrhash_finish (void)
581{
Lou Berger056f3762013-04-10 12:30:04 -0700582 hash_clean(attrhash, attr_vfree);
Chris Caputo228da422009-07-18 05:44:03 +0000583 hash_free (attrhash);
584 attrhash = NULL;
585}
586
587static void
paul718e3742002-12-13 20:15:29 +0000588attr_show_all_iterator (struct hash_backet *backet, struct vty *vty)
589{
590 struct attr *attr = backet->data;
591
592 vty_out (vty, "attr[%ld] nexthop %s%s", attr->refcnt,
593 inet_ntoa (attr->nexthop), VTY_NEWLINE);
594}
595
596void
597attr_show_all (struct vty *vty)
598{
599 hash_iterate (attrhash,
600 (void (*)(struct hash_backet *, void *))
601 attr_show_all_iterator,
602 vty);
603}
604
paul94f2b392005-06-28 12:44:16 +0000605static void *
Paul Jakma923de652007-04-29 18:25:17 +0000606bgp_attr_hash_alloc (void *p)
paul718e3742002-12-13 20:15:29 +0000607{
Paul Jakma923de652007-04-29 18:25:17 +0000608 struct attr * val = (struct attr *) p;
paul718e3742002-12-13 20:15:29 +0000609 struct attr *attr;
610
611 attr = XMALLOC (MTYPE_ATTR, sizeof (struct attr));
612 *attr = *val;
Paul Jakmafb982c22007-05-04 20:15:47 +0000613 if (val->extra)
614 {
615 attr->extra = bgp_attr_extra_new ();
616 *attr->extra = *val->extra;
Lou Bergerc3741782016-01-12 13:42:01 -0500617
618 if (attr->extra->encap_subtlvs) {
619 attr->extra->encap_subtlvs = encap_tlv_dup(attr->extra->encap_subtlvs);
620 }
Paul Jakmafb982c22007-05-04 20:15:47 +0000621 }
paul718e3742002-12-13 20:15:29 +0000622 attr->refcnt = 0;
623 return attr;
624}
625
626/* Internet argument attribute. */
627struct attr *
628bgp_attr_intern (struct attr *attr)
629{
630 struct attr *find;
631
632 /* Intern referenced strucutre. */
633 if (attr->aspath)
634 {
635 if (! attr->aspath->refcnt)
636 attr->aspath = aspath_intern (attr->aspath);
637 else
638 attr->aspath->refcnt++;
639 }
640 if (attr->community)
641 {
642 if (! attr->community->refcnt)
643 attr->community = community_intern (attr->community);
644 else
645 attr->community->refcnt++;
646 }
Paul Jakmafb982c22007-05-04 20:15:47 +0000647 if (attr->extra)
paul718e3742002-12-13 20:15:29 +0000648 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000649 struct attr_extra *attre = attr->extra;
650
651 if (attre->ecommunity)
652 {
653 if (! attre->ecommunity->refcnt)
654 attre->ecommunity = ecommunity_intern (attre->ecommunity);
655 else
656 attre->ecommunity->refcnt++;
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000657
Paul Jakmafb982c22007-05-04 20:15:47 +0000658 }
659 if (attre->cluster)
660 {
661 if (! attre->cluster->refcnt)
662 attre->cluster = cluster_intern (attre->cluster);
663 else
664 attre->cluster->refcnt++;
665 }
666 if (attre->transit)
667 {
668 if (! attre->transit->refcnt)
669 attre->transit = transit_intern (attre->transit);
670 else
671 attre->transit->refcnt++;
672 }
paul718e3742002-12-13 20:15:29 +0000673 }
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000674
paul718e3742002-12-13 20:15:29 +0000675 find = (struct attr *) hash_get (attrhash, attr, bgp_attr_hash_alloc);
676 find->refcnt++;
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000677
paul718e3742002-12-13 20:15:29 +0000678 return find;
679}
680
Paul Jakma03e214c2007-04-29 18:31:07 +0000681
paul718e3742002-12-13 20:15:29 +0000682/* Make network statement's attribute. */
683struct attr *
684bgp_attr_default_set (struct attr *attr, u_char origin)
685{
686 memset (attr, 0, sizeof (struct attr));
Paul Jakmafb982c22007-05-04 20:15:47 +0000687 bgp_attr_extra_get (attr);
688
paul718e3742002-12-13 20:15:29 +0000689 attr->origin = origin;
690 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
691 attr->aspath = aspath_empty ();
692 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
Paul Jakmafb982c22007-05-04 20:15:47 +0000693 attr->extra->weight = BGP_ATTR_DEFAULT_WEIGHT;
paul718e3742002-12-13 20:15:29 +0000694 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
Paul Jakmafb982c22007-05-04 20:15:47 +0000695 attr->extra->mp_nexthop_len = IPV6_MAX_BYTELEN;
Paul Jakma03e214c2007-04-29 18:31:07 +0000696
paul718e3742002-12-13 20:15:29 +0000697 return attr;
698}
699
Paul Jakma03e214c2007-04-29 18:31:07 +0000700
paul718e3742002-12-13 20:15:29 +0000701/* Make network statement's attribute. */
702struct attr *
703bgp_attr_default_intern (u_char origin)
704{
705 struct attr attr;
706 struct attr *new;
Jorge Boncompte [DTI2]e16a4132012-05-07 16:52:57 +0000707
Lou Bergerc3741782016-01-12 13:42:01 -0500708 memset (&attr, 0, sizeof (struct attr));
709 bgp_attr_extra_get (&attr);
710
Paul Jakma03e214c2007-04-29 18:31:07 +0000711 bgp_attr_default_set(&attr, origin);
paul718e3742002-12-13 20:15:29 +0000712
713 new = bgp_attr_intern (&attr);
Paul Jakmafb982c22007-05-04 20:15:47 +0000714 bgp_attr_extra_free (&attr);
715
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000716 aspath_unintern (&new->aspath);
paul718e3742002-12-13 20:15:29 +0000717 return new;
718}
719
720struct attr *
721bgp_attr_aggregate_intern (struct bgp *bgp, u_char origin,
722 struct aspath *aspath,
723 struct community *community, int as_set)
724{
725 struct attr attr;
726 struct attr *new;
Jorge Boncompte [DTI2]938ef3a2012-05-07 16:52:59 +0000727 struct attr_extra attre;
paul718e3742002-12-13 20:15:29 +0000728
729 memset (&attr, 0, sizeof (struct attr));
Jorge Boncompte [DTI2]938ef3a2012-05-07 16:52:59 +0000730 memset (&attre, 0, sizeof (struct attr_extra));
731 attr.extra = &attre;
732
paul718e3742002-12-13 20:15:29 +0000733 /* Origin attribute. */
734 attr.origin = origin;
735 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
736
737 /* AS path attribute. */
738 if (aspath)
739 attr.aspath = aspath_intern (aspath);
740 else
741 attr.aspath = aspath_empty ();
742 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
743
744 /* Next hop attribute. */
745 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
746
747 if (community)
748 {
749 attr.community = community;
750 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES);
751 }
752
Jorge Boncompte [DTI2]938ef3a2012-05-07 16:52:59 +0000753 attre.weight = BGP_ATTR_DEFAULT_WEIGHT;
Jorge Boncompte [DTI2]938ef3a2012-05-07 16:52:59 +0000754 attre.mp_nexthop_len = IPV6_MAX_BYTELEN;
Lou Berger205e6742016-01-12 13:42:11 -0500755
paul718e3742002-12-13 20:15:29 +0000756 if (! as_set)
757 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
758 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR);
759 if (CHECK_FLAG (bgp->config, BGP_CONFIG_CONFEDERATION))
Jorge Boncompte [DTI2]938ef3a2012-05-07 16:52:59 +0000760 attre.aggregator_as = bgp->confed_id;
paul718e3742002-12-13 20:15:29 +0000761 else
Jorge Boncompte [DTI2]938ef3a2012-05-07 16:52:59 +0000762 attre.aggregator_as = bgp->as;
763 attre.aggregator_addr = bgp->router_id;
paul718e3742002-12-13 20:15:29 +0000764
765 new = bgp_attr_intern (&attr);
Jorge Boncompte [DTI2]938ef3a2012-05-07 16:52:59 +0000766
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000767 aspath_unintern (&new->aspath);
paul718e3742002-12-13 20:15:29 +0000768 return new;
769}
770
Paul Jakmab881c702010-11-23 16:35:42 +0000771/* Unintern just the sub-components of the attr, but not the attr */
772void
773bgp_attr_unintern_sub (struct attr *attr)
774{
775 /* aspath refcount shoud be decrement. */
776 if (attr->aspath)
777 aspath_unintern (&attr->aspath);
Jorge Boncompte [DTI2]d92a2f32013-07-31 16:36:08 +0000778 UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH));
Paul Jakmab881c702010-11-23 16:35:42 +0000779
780 if (attr->community)
781 community_unintern (&attr->community);
Jorge Boncompte [DTI2]d92a2f32013-07-31 16:36:08 +0000782 UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES));
Paul Jakmab881c702010-11-23 16:35:42 +0000783
784 if (attr->extra)
785 {
786 if (attr->extra->ecommunity)
787 ecommunity_unintern (&attr->extra->ecommunity);
Jorge Boncompte [DTI2]d92a2f32013-07-31 16:36:08 +0000788 UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES));
Paul Jakmab881c702010-11-23 16:35:42 +0000789
790 if (attr->extra->cluster)
791 cluster_unintern (attr->extra->cluster);
Jorge Boncompte [DTI2]d92a2f32013-07-31 16:36:08 +0000792 UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST));
Paul Jakmab881c702010-11-23 16:35:42 +0000793
794 if (attr->extra->transit)
795 transit_unintern (attr->extra->transit);
796 }
797}
798
paul718e3742002-12-13 20:15:29 +0000799/* Free bgp attribute and aspath. */
800void
Jorge Boncompte [DTI2]1a2fd702012-05-07 16:53:00 +0000801bgp_attr_unintern (struct attr **pattr)
paul718e3742002-12-13 20:15:29 +0000802{
Jorge Boncompte [DTI2]1a2fd702012-05-07 16:53:00 +0000803 struct attr *attr = *pattr;
paul718e3742002-12-13 20:15:29 +0000804 struct attr *ret;
Paul Jakmab881c702010-11-23 16:35:42 +0000805 struct attr tmp;
Jorge Boncompte [DTI2]b9f1dca2012-05-07 16:53:01 +0000806 struct attr_extra tmp_extra;
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000807
paul718e3742002-12-13 20:15:29 +0000808 /* Decrement attribute reference. */
Jorge Boncompte [DTI2]1a2fd702012-05-07 16:53:00 +0000809 attr->refcnt--;
Paul Jakmab881c702010-11-23 16:35:42 +0000810
Jorge Boncompte [DTI2]1a2fd702012-05-07 16:53:00 +0000811 tmp = *attr;
Paul Jakmab881c702010-11-23 16:35:42 +0000812
Jorge Boncompte [DTI2]1a2fd702012-05-07 16:53:00 +0000813 if (attr->extra)
Paul Jakmafb982c22007-05-04 20:15:47 +0000814 {
Jorge Boncompte [DTI2]b9f1dca2012-05-07 16:53:01 +0000815 tmp.extra = &tmp_extra;
Jorge Boncompte [DTI2]1a2fd702012-05-07 16:53:00 +0000816 memcpy (tmp.extra, attr->extra, sizeof (struct attr_extra));
Paul Jakmafb982c22007-05-04 20:15:47 +0000817 }
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000818
paul718e3742002-12-13 20:15:29 +0000819 /* If reference becomes zero then free attribute object. */
Jorge Boncompte [DTI2]1a2fd702012-05-07 16:53:00 +0000820 if (attr->refcnt == 0)
821 {
822 ret = hash_release (attrhash, attr);
paul718e3742002-12-13 20:15:29 +0000823 assert (ret != NULL);
Jorge Boncompte [DTI2]1a2fd702012-05-07 16:53:00 +0000824 bgp_attr_extra_free (attr);
825 XFREE (MTYPE_ATTR, attr);
826 *pattr = NULL;
paul718e3742002-12-13 20:15:29 +0000827 }
828
Paul Jakmab881c702010-11-23 16:35:42 +0000829 bgp_attr_unintern_sub (&tmp);
paul718e3742002-12-13 20:15:29 +0000830}
831
832void
833bgp_attr_flush (struct attr *attr)
834{
835 if (attr->aspath && ! attr->aspath->refcnt)
836 aspath_free (attr->aspath);
837 if (attr->community && ! attr->community->refcnt)
838 community_free (attr->community);
Paul Jakmafb982c22007-05-04 20:15:47 +0000839 if (attr->extra)
840 {
841 struct attr_extra *attre = attr->extra;
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000842
Paul Jakmafb982c22007-05-04 20:15:47 +0000843 if (attre->ecommunity && ! attre->ecommunity->refcnt)
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000844 ecommunity_free (&attre->ecommunity);
Paul Jakmafb982c22007-05-04 20:15:47 +0000845 if (attre->cluster && ! attre->cluster->refcnt)
846 cluster_free (attre->cluster);
847 if (attre->transit && ! attre->transit->refcnt)
848 transit_free (attre->transit);
Lou Bergerc3741782016-01-12 13:42:01 -0500849 encap_free(attre->encap_subtlvs);
850 attre->encap_subtlvs = NULL;
Paul Jakmafb982c22007-05-04 20:15:47 +0000851 }
paul718e3742002-12-13 20:15:29 +0000852}
853
Paul Jakmab881c702010-11-23 16:35:42 +0000854/* Implement draft-scudder-idr-optional-transitive behaviour and
855 * avoid resetting sessions for malformed attributes which are
856 * are partial/optional and hence where the error likely was not
857 * introduced by the sending neighbour.
858 */
859static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +0000860bgp_attr_malformed (struct bgp_attr_parser_args *args, u_char subcode,
861 bgp_size_t length)
Paul Jakmab881c702010-11-23 16:35:42 +0000862{
Paul Jakma835315b2012-01-18 12:28:30 +0000863 struct peer *const peer = args->peer;
864 const u_int8_t flags = args->flags;
865 /* startp and length must be special-cased, as whether or not to
866 * send the attribute data with the NOTIFY depends on the error,
867 * the caller therefore signals this with the seperate length argument
868 */
Paul Jakmabd471fe2012-03-15 11:30:00 +0000869 u_char *notify_datap = (length > 0 ? args->startp : NULL);
Paul Jakma835315b2012-01-18 12:28:30 +0000870
Paul Jakmab881c702010-11-23 16:35:42 +0000871 /* Only relax error handling for eBGP peers */
Jorge Boncompte [DTI2]6d85b152012-05-07 16:52:54 +0000872 if (peer->sort != BGP_PEER_EBGP)
Paul Jakmab881c702010-11-23 16:35:42 +0000873 {
874 bgp_notify_send_with_data (peer, BGP_NOTIFY_UPDATE_ERR, subcode,
Paul Jakmabd471fe2012-03-15 11:30:00 +0000875 notify_datap, length);
Paul Jakmab881c702010-11-23 16:35:42 +0000876 return BGP_ATTR_PARSE_ERROR;
877
878 }
879
Paul Jakmabd471fe2012-03-15 11:30:00 +0000880 /* Adjust the stream getp to the end of the attribute, in case we can
881 * still proceed but the caller hasn't read all the attribute.
882 */
883 stream_set_getp (BGP_INPUT (peer),
884 (args->startp - STREAM_DATA (BGP_INPUT (peer)))
885 + args->total);
886
Paul Jakma835315b2012-01-18 12:28:30 +0000887 switch (args->type) {
Paul Jakmafa61e162012-03-25 21:31:47 +0100888 /* where an attribute is relatively inconsequential, e.g. it does not
889 * affect route selection, and can be safely ignored, then any such
890 * attributes which are malformed should just be ignored and the route
891 * processed as normal.
Paul Jakmab881c702010-11-23 16:35:42 +0000892 */
893 case BGP_ATTR_AS4_AGGREGATOR:
894 case BGP_ATTR_AGGREGATOR:
895 case BGP_ATTR_ATOMIC_AGGREGATE:
896 return BGP_ATTR_PARSE_PROCEED;
897
898 /* Core attributes, particularly ones which may influence route
Paul Jakmafa61e162012-03-25 21:31:47 +0100899 * selection, should always cause session resets
Paul Jakmab881c702010-11-23 16:35:42 +0000900 */
901 case BGP_ATTR_ORIGIN:
902 case BGP_ATTR_AS_PATH:
903 case BGP_ATTR_NEXT_HOP:
904 case BGP_ATTR_MULTI_EXIT_DISC:
905 case BGP_ATTR_LOCAL_PREF:
906 case BGP_ATTR_COMMUNITIES:
907 case BGP_ATTR_ORIGINATOR_ID:
908 case BGP_ATTR_CLUSTER_LIST:
909 case BGP_ATTR_MP_REACH_NLRI:
910 case BGP_ATTR_MP_UNREACH_NLRI:
911 case BGP_ATTR_EXT_COMMUNITIES:
912 bgp_notify_send_with_data (peer, BGP_NOTIFY_UPDATE_ERR, subcode,
Paul Jakmabd471fe2012-03-15 11:30:00 +0000913 notify_datap, length);
Paul Jakmab881c702010-11-23 16:35:42 +0000914 return BGP_ATTR_PARSE_ERROR;
915 }
916
917 /* Partial optional attributes that are malformed should not cause
918 * the whole session to be reset. Instead treat it as a withdrawal
919 * of the routes, if possible.
920 */
Paul Jakma835315b2012-01-18 12:28:30 +0000921 if (CHECK_FLAG (flags, BGP_ATTR_FLAG_TRANS)
922 && CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL)
923 && CHECK_FLAG (flags, BGP_ATTR_FLAG_PARTIAL))
Paul Jakmab881c702010-11-23 16:35:42 +0000924 return BGP_ATTR_PARSE_WITHDRAW;
925
926 /* default to reset */
David Lamparterf57000c2014-06-04 01:01:10 +0200927 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
Paul Jakmab881c702010-11-23 16:35:42 +0000928}
929
Denis Ovsienkoafcb7672011-10-23 22:32:44 +0400930/* Find out what is wrong with the path attribute flag bits and log the error.
931 "Flag bits" here stand for Optional, Transitive and Partial, but not for
932 Extended Length. Checking O/T/P bits at once implies, that the attribute
933 being diagnosed is defined by RFC as either a "well-known" or an "optional,
934 non-transitive" attribute. */
935static void
Paul Jakma835315b2012-01-18 12:28:30 +0000936bgp_attr_flags_diagnose (struct bgp_attr_parser_args *args,
937 u_int8_t desired_flags /* how RFC says it must be */
Denis Ovsienkoafcb7672011-10-23 22:32:44 +0400938)
939{
940 u_char seen = 0, i;
Paul Jakma835315b2012-01-18 12:28:30 +0000941 u_char real_flags = args->flags;
942 const u_int8_t attr_code = args->type;
943
Denis Ovsienkoafcb7672011-10-23 22:32:44 +0400944 desired_flags &= ~BGP_ATTR_FLAG_EXTLEN;
945 real_flags &= ~BGP_ATTR_FLAG_EXTLEN;
946 for (i = 0; i <= 2; i++) /* O,T,P, but not E */
947 if
948 (
949 CHECK_FLAG (desired_flags, attr_flag_str[i].key) !=
950 CHECK_FLAG (real_flags, attr_flag_str[i].key)
951 )
952 {
Paul Jakma835315b2012-01-18 12:28:30 +0000953 zlog (args->peer->log, LOG_ERR, "%s attribute must%s be flagged as \"%s\"",
Denis Ovsienkoafcb7672011-10-23 22:32:44 +0400954 LOOKUP (attr_str, attr_code),
955 CHECK_FLAG (desired_flags, attr_flag_str[i].key) ? "" : " not",
956 attr_flag_str[i].str);
957 seen = 1;
958 }
Paul Jakmafa5831e2012-03-27 11:54:04 +0100959 if (!seen)
960 {
961 zlog (args->peer->log, LOG_DEBUG,
962 "Strange, %s called for attr %s, but no problem found with flags"
963 " (real flags 0x%x, desired 0x%x)",
964 __func__, LOOKUP (attr_str, attr_code),
965 real_flags, desired_flags);
966 }
Denis Ovsienkoafcb7672011-10-23 22:32:44 +0400967}
968
Paul Jakma3ecab4c2012-01-17 13:31:33 +0000969/* Required flags for attributes. EXTLEN will be masked off when testing,
970 * as will PARTIAL for optional+transitive attributes.
971 */
972const u_int8_t attr_flags_values [] = {
973 [BGP_ATTR_ORIGIN] = BGP_ATTR_FLAG_TRANS,
974 [BGP_ATTR_AS_PATH] = BGP_ATTR_FLAG_TRANS,
975 [BGP_ATTR_NEXT_HOP] = BGP_ATTR_FLAG_TRANS,
976 [BGP_ATTR_MULTI_EXIT_DISC] = BGP_ATTR_FLAG_OPTIONAL,
977 [BGP_ATTR_LOCAL_PREF] = BGP_ATTR_FLAG_TRANS,
978 [BGP_ATTR_ATOMIC_AGGREGATE] = BGP_ATTR_FLAG_TRANS,
979 [BGP_ATTR_AGGREGATOR] = BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL,
980 [BGP_ATTR_COMMUNITIES] = BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL,
981 [BGP_ATTR_ORIGINATOR_ID] = BGP_ATTR_FLAG_OPTIONAL,
982 [BGP_ATTR_CLUSTER_LIST] = BGP_ATTR_FLAG_OPTIONAL,
983 [BGP_ATTR_MP_REACH_NLRI] = BGP_ATTR_FLAG_OPTIONAL,
984 [BGP_ATTR_MP_UNREACH_NLRI] = BGP_ATTR_FLAG_OPTIONAL,
985 [BGP_ATTR_EXT_COMMUNITIES] = BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
986 [BGP_ATTR_AS4_PATH] = BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
987 [BGP_ATTR_AS4_AGGREGATOR] = BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
988};
989static const size_t attr_flags_values_max =
990 sizeof (attr_flags_values) / sizeof (attr_flags_values[0]);
991
992static int
Paul Jakma835315b2012-01-18 12:28:30 +0000993bgp_attr_flag_invalid (struct bgp_attr_parser_args *args)
Paul Jakma3ecab4c2012-01-17 13:31:33 +0000994{
995 u_int8_t mask = BGP_ATTR_FLAG_EXTLEN;
Paul Jakma835315b2012-01-18 12:28:30 +0000996 const u_int8_t flags = args->flags;
997 const u_int8_t attr_code = args->type;
998 struct peer *const peer = args->peer;
Paul Jakma3ecab4c2012-01-17 13:31:33 +0000999
1000 /* there may be attributes we don't know about */
1001 if (attr_code > attr_flags_values_max)
1002 return 0;
1003 if (attr_flags_values[attr_code] == 0)
1004 return 0;
1005
1006 /* RFC4271, "For well-known attributes, the Transitive bit MUST be set to
1007 * 1."
1008 */
1009 if (!CHECK_FLAG (BGP_ATTR_FLAG_OPTIONAL, flags)
1010 && !CHECK_FLAG (BGP_ATTR_FLAG_TRANS, flags))
1011 {
1012 zlog (peer->log, LOG_ERR,
1013 "%s well-known attributes must have transitive flag set (%x)",
1014 LOOKUP (attr_str, attr_code), flags);
1015 return 1;
1016 }
1017
1018 /* "For well-known attributes and for optional non-transitive attributes,
1019 * the Partial bit MUST be set to 0."
1020 */
1021 if (CHECK_FLAG (flags, BGP_ATTR_FLAG_PARTIAL))
1022 {
1023 if (!CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL))
1024 {
1025 zlog (peer->log, LOG_ERR,
1026 "%s well-known attribute "
1027 "must NOT have the partial flag set (%x)",
1028 LOOKUP (attr_str, attr_code), flags);
1029 return 1;
1030 }
1031 if (CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL)
1032 && !CHECK_FLAG (flags, BGP_ATTR_FLAG_TRANS))
1033 {
1034 zlog (peer->log, LOG_ERR,
1035 "%s optional + transitive attribute "
1036 "must NOT have the partial flag set (%x)",
1037 LOOKUP (attr_str, attr_code), flags);
1038 return 1;
1039 }
1040 }
1041
1042 /* Optional transitive attributes may go through speakers that don't
1043 * reocgnise them and set the Partial bit.
1044 */
1045 if (CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL)
1046 && CHECK_FLAG (flags, BGP_ATTR_FLAG_TRANS))
1047 SET_FLAG (mask, BGP_ATTR_FLAG_PARTIAL);
1048
Paul Jakma683f2b82012-03-23 14:58:45 +00001049 if ((flags & ~mask)
Paul Jakma3ecab4c2012-01-17 13:31:33 +00001050 == attr_flags_values[attr_code])
1051 return 0;
1052
Paul Jakma835315b2012-01-18 12:28:30 +00001053 bgp_attr_flags_diagnose (args, attr_flags_values[attr_code]);
Paul Jakma3ecab4c2012-01-17 13:31:33 +00001054 return 1;
1055}
1056
paul718e3742002-12-13 20:15:29 +00001057/* Get origin attribute of the update message. */
Paul Jakmab881c702010-11-23 16:35:42 +00001058static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +00001059bgp_attr_origin (struct bgp_attr_parser_args *args)
paul718e3742002-12-13 20:15:29 +00001060{
Paul Jakma835315b2012-01-18 12:28:30 +00001061 struct peer *const peer = args->peer;
1062 struct attr *const attr = args->attr;
1063 const bgp_size_t length = args->length;
1064
paul718e3742002-12-13 20:15:29 +00001065 /* If any recognized attribute has Attribute Length that conflicts
1066 with the expected length (based on the attribute type code), then
1067 the Error Subcode is set to Attribute Length Error. The Data
1068 field contains the erroneous attribute (type, length and
1069 value). */
1070 if (length != 1)
1071 {
1072 zlog (peer->log, LOG_ERR, "Origin attribute length is not one %d",
1073 length);
Paul Jakma835315b2012-01-18 12:28:30 +00001074 return bgp_attr_malformed (args,
Paul Jakmab881c702010-11-23 16:35:42 +00001075 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
Paul Jakma835315b2012-01-18 12:28:30 +00001076 args->total);
paul718e3742002-12-13 20:15:29 +00001077 }
1078
1079 /* Fetch origin attribute. */
1080 attr->origin = stream_getc (BGP_INPUT (peer));
1081
1082 /* If the ORIGIN attribute has an undefined value, then the Error
1083 Subcode is set to Invalid Origin Attribute. The Data field
1084 contains the unrecognized attribute (type, length and value). */
1085 if ((attr->origin != BGP_ORIGIN_IGP)
1086 && (attr->origin != BGP_ORIGIN_EGP)
1087 && (attr->origin != BGP_ORIGIN_INCOMPLETE))
1088 {
1089 zlog (peer->log, LOG_ERR, "Origin attribute value is invalid %d",
1090 attr->origin);
Paul Jakma835315b2012-01-18 12:28:30 +00001091 return bgp_attr_malformed (args,
Paul Jakmab881c702010-11-23 16:35:42 +00001092 BGP_NOTIFY_UPDATE_INVAL_ORIGIN,
Paul Jakma835315b2012-01-18 12:28:30 +00001093 args->total);
paul718e3742002-12-13 20:15:29 +00001094 }
1095
1096 /* Set oring attribute flag. */
1097 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
1098
1099 return 0;
1100}
Paul Jakmaab005292010-11-27 22:48:34 +00001101
1102/* Parse AS path information. This function is wrapper of
1103 aspath_parse. */
1104static int
Paul Jakma835315b2012-01-18 12:28:30 +00001105bgp_attr_aspath (struct bgp_attr_parser_args *args)
paul718e3742002-12-13 20:15:29 +00001106{
Paul Jakma835315b2012-01-18 12:28:30 +00001107 struct attr *const attr = args->attr;
1108 struct peer *const peer = args->peer;
1109 const bgp_size_t length = args->length;
Paul Jakma83a9a222012-01-08 14:15:03 +00001110
Paul Jakmaab005292010-11-27 22:48:34 +00001111 /*
1112 * peer with AS4 => will get 4Byte ASnums
1113 * otherwise, will get 16 Bit
1114 */
1115 attr->aspath = aspath_parse (peer->ibuf, length,
1116 CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV));
1117
1118 /* In case of IBGP, length will be zero. */
1119 if (! attr->aspath)
paul718e3742002-12-13 20:15:29 +00001120 {
Paul Jakmab881c702010-11-23 16:35:42 +00001121 zlog (peer->log, LOG_ERR,
1122 "Malformed AS path from %s, length is %d",
1123 peer->host, length);
Paul Jakma835315b2012-01-18 12:28:30 +00001124 return bgp_attr_malformed (args, BGP_NOTIFY_UPDATE_MAL_AS_PATH, 0);
Paul Jakmaab005292010-11-27 22:48:34 +00001125 }
Chris Hallcddb8112010-08-09 22:31:37 +04001126
Paul Jakmaab005292010-11-27 22:48:34 +00001127 /* Set aspath attribute flag. */
1128 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
paul718e3742002-12-13 20:15:29 +00001129
Paul Jakmab881c702010-11-23 16:35:42 +00001130 return BGP_ATTR_PARSE_PROCEED;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001131}
1132
Paul Jakmab881c702010-11-23 16:35:42 +00001133static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +00001134bgp_attr_aspath_check (struct peer *const peer, struct attr *const attr)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001135{
1136 /* These checks were part of bgp_attr_aspath, but with
1137 * as4 we should to check aspath things when
1138 * aspath synthesizing with as4_path has already taken place.
1139 * Otherwise we check ASPATH and use the synthesized thing, and that is
1140 * not right.
1141 * So do the checks later, i.e. here
1142 */
1143 struct bgp *bgp = peer->bgp;
1144 struct aspath *aspath;
1145
Vasilis Tsiligiannisca87e1d2009-07-20 01:28:35 +03001146 /* Confederation sanity check. */
Jorge Boncompte [DTI2]6d85b152012-05-07 16:52:54 +00001147 if ((peer->sort == BGP_PEER_CONFED && ! aspath_left_confed_check (attr->aspath)) ||
1148 (peer->sort == BGP_PEER_EBGP && aspath_confed_check (attr->aspath)))
Vasilis Tsiligiannisca87e1d2009-07-20 01:28:35 +03001149 {
1150 zlog (peer->log, LOG_ERR, "Malformed AS path from %s", peer->host);
Paul Jakma835315b2012-01-18 12:28:30 +00001151 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1152 BGP_NOTIFY_UPDATE_MAL_AS_PATH);
1153 return BGP_ATTR_PARSE_ERROR;
Vasilis Tsiligiannisca87e1d2009-07-20 01:28:35 +03001154 }
1155
paul718e3742002-12-13 20:15:29 +00001156 /* First AS check for EBGP. */
1157 if (bgp != NULL && bgp_flag_check (bgp, BGP_FLAG_ENFORCE_FIRST_AS))
1158 {
Jorge Boncompte [DTI2]6d85b152012-05-07 16:52:54 +00001159 if (peer->sort == BGP_PEER_EBGP
paul718e3742002-12-13 20:15:29 +00001160 && ! aspath_firstas_check (attr->aspath, peer->as))
1161 {
1162 zlog (peer->log, LOG_ERR,
Denis Ovsienkoaea339f2009-04-30 17:16:22 +04001163 "%s incorrect first AS (must be %u)", peer->host, peer->as);
Paul Jakma835315b2012-01-18 12:28:30 +00001164 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1165 BGP_NOTIFY_UPDATE_MAL_AS_PATH);
1166 return BGP_ATTR_PARSE_ERROR;
paul718e3742002-12-13 20:15:29 +00001167 }
1168 }
1169
1170 /* local-as prepend */
1171 if (peer->change_local_as &&
1172 ! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND))
1173 {
1174 aspath = aspath_dup (attr->aspath);
1175 aspath = aspath_add_seq (aspath, peer->change_local_as);
Paul Jakmaf6f434b2010-11-23 21:28:03 +00001176 aspath_unintern (&attr->aspath);
paul718e3742002-12-13 20:15:29 +00001177 attr->aspath = aspath_intern (aspath);
1178 }
1179
Paul Jakmab881c702010-11-23 16:35:42 +00001180 return BGP_ATTR_PARSE_PROCEED;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001181}
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001182
Paul Jakmaab005292010-11-27 22:48:34 +00001183/* Parse AS4 path information. This function is another wrapper of
1184 aspath_parse. */
1185static int
Paul Jakma835315b2012-01-18 12:28:30 +00001186bgp_attr_as4_path (struct bgp_attr_parser_args *args, struct aspath **as4_path)
Paul Jakmaab005292010-11-27 22:48:34 +00001187{
Paul Jakma835315b2012-01-18 12:28:30 +00001188 struct peer *const peer = args->peer;
1189 struct attr *const attr = args->attr;
1190 const bgp_size_t length = args->length;
Paul Jakma3ecab4c2012-01-17 13:31:33 +00001191
Paul Jakmaab005292010-11-27 22:48:34 +00001192 *as4_path = aspath_parse (peer->ibuf, length, 1);
1193
Paul Jakmab881c702010-11-23 16:35:42 +00001194 /* In case of IBGP, length will be zero. */
1195 if (!*as4_path)
1196 {
1197 zlog (peer->log, LOG_ERR,
1198 "Malformed AS4 path from %s, length is %d",
1199 peer->host, length);
Paul Jakma835315b2012-01-18 12:28:30 +00001200 return bgp_attr_malformed (args,
Paul Jakmab881c702010-11-23 16:35:42 +00001201 BGP_NOTIFY_UPDATE_MAL_AS_PATH,
Paul Jakma835315b2012-01-18 12:28:30 +00001202 0);
Paul Jakmab881c702010-11-23 16:35:42 +00001203 }
1204
Paul Jakmaab005292010-11-27 22:48:34 +00001205 /* Set aspath attribute flag. */
1206 if (as4_path)
1207 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_PATH);
1208
Paul Jakmab881c702010-11-23 16:35:42 +00001209 return BGP_ATTR_PARSE_PROCEED;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001210}
1211
paul718e3742002-12-13 20:15:29 +00001212/* Nexthop attribute. */
Paul Jakmab881c702010-11-23 16:35:42 +00001213static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +00001214bgp_attr_nexthop (struct bgp_attr_parser_args *args)
paul718e3742002-12-13 20:15:29 +00001215{
Paul Jakma835315b2012-01-18 12:28:30 +00001216 struct peer *const peer = args->peer;
1217 struct attr *const attr = args->attr;
1218 const bgp_size_t length = args->length;
1219
Denis Ovsienkobc3443e2011-09-22 12:48:14 +04001220 in_addr_t nexthop_h, nexthop_n;
paul718e3742002-12-13 20:15:29 +00001221
paul718e3742002-12-13 20:15:29 +00001222 /* Check nexthop attribute length. */
1223 if (length != 4)
1224 {
1225 zlog (peer->log, LOG_ERR, "Nexthop attribute length isn't four [%d]",
1226 length);
1227
Paul Jakma835315b2012-01-18 12:28:30 +00001228 return bgp_attr_malformed (args,
Paul Jakmab881c702010-11-23 16:35:42 +00001229 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
Paul Jakma835315b2012-01-18 12:28:30 +00001230 args->total);
paul718e3742002-12-13 20:15:29 +00001231 }
1232
Denis Ovsienkobc3443e2011-09-22 12:48:14 +04001233 /* According to section 6.3 of RFC4271, syntactically incorrect NEXT_HOP
1234 attribute must result in a NOTIFICATION message (this is implemented below).
1235 At the same time, semantically incorrect NEXT_HOP is more likely to be just
1236 logged locally (this is implemented somewhere else). The UPDATE message
1237 gets ignored in any of these cases. */
1238 nexthop_n = stream_get_ipv4 (peer->ibuf);
1239 nexthop_h = ntohl (nexthop_n);
1240 if (IPV4_NET0 (nexthop_h) || IPV4_NET127 (nexthop_h) || IPV4_CLASS_DE (nexthop_h))
1241 {
1242 char buf[INET_ADDRSTRLEN];
David Lamparterbb02b822014-06-04 01:01:00 +02001243 inet_ntop (AF_INET, &nexthop_n, buf, INET_ADDRSTRLEN);
Denis Ovsienkobc3443e2011-09-22 12:48:14 +04001244 zlog (peer->log, LOG_ERR, "Martian nexthop %s", buf);
Paul Jakma835315b2012-01-18 12:28:30 +00001245 return bgp_attr_malformed (args,
Denis Ovsienkobc3443e2011-09-22 12:48:14 +04001246 BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP,
Paul Jakma835315b2012-01-18 12:28:30 +00001247 args->total);
Denis Ovsienkobc3443e2011-09-22 12:48:14 +04001248 }
1249
1250 attr->nexthop.s_addr = nexthop_n;
paul718e3742002-12-13 20:15:29 +00001251 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
1252
Paul Jakmab881c702010-11-23 16:35:42 +00001253 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00001254}
1255
1256/* MED atrribute. */
Paul Jakmab881c702010-11-23 16:35:42 +00001257static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +00001258bgp_attr_med (struct bgp_attr_parser_args *args)
paul718e3742002-12-13 20:15:29 +00001259{
Paul Jakma835315b2012-01-18 12:28:30 +00001260 struct peer *const peer = args->peer;
1261 struct attr *const attr = args->attr;
1262 const bgp_size_t length = args->length;
1263
paul718e3742002-12-13 20:15:29 +00001264 /* Length check. */
1265 if (length != 4)
1266 {
1267 zlog (peer->log, LOG_ERR,
1268 "MED attribute length isn't four [%d]", length);
Paul Jakmab881c702010-11-23 16:35:42 +00001269
Paul Jakma835315b2012-01-18 12:28:30 +00001270 return bgp_attr_malformed (args,
Paul Jakmab881c702010-11-23 16:35:42 +00001271 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
Paul Jakma835315b2012-01-18 12:28:30 +00001272 args->total);
paul718e3742002-12-13 20:15:29 +00001273 }
1274
1275 attr->med = stream_getl (peer->ibuf);
1276
1277 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
1278
Paul Jakmab881c702010-11-23 16:35:42 +00001279 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00001280}
1281
1282/* Local preference attribute. */
Paul Jakmab881c702010-11-23 16:35:42 +00001283static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +00001284bgp_attr_local_pref (struct bgp_attr_parser_args *args)
paul718e3742002-12-13 20:15:29 +00001285{
Paul Jakma835315b2012-01-18 12:28:30 +00001286 struct peer *const peer = args->peer;
1287 struct attr *const attr = args->attr;
1288 const bgp_size_t length = args->length;
Paul Jakma3ecab4c2012-01-17 13:31:33 +00001289
Denis Ovsienkoa624cae2011-10-08 13:54:48 +04001290 /* Length check. */
1291 if (length != 4)
1292 {
Paul Jakma835315b2012-01-18 12:28:30 +00001293 zlog (peer->log, LOG_ERR, "LOCAL_PREF attribute length isn't 4 [%u]",
1294 length);
1295 return bgp_attr_malformed (args,
Denis Ovsienkoa624cae2011-10-08 13:54:48 +04001296 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
Paul Jakma835315b2012-01-18 12:28:30 +00001297 args->total);
Denis Ovsienkoa624cae2011-10-08 13:54:48 +04001298 }
Denis Ovsienko0ea968d2011-09-19 16:30:47 +04001299
paul718e3742002-12-13 20:15:29 +00001300 /* If it is contained in an UPDATE message that is received from an
1301 external peer, then this attribute MUST be ignored by the
1302 receiving speaker. */
Jorge Boncompte [DTI2]6d85b152012-05-07 16:52:54 +00001303 if (peer->sort == BGP_PEER_EBGP)
paul718e3742002-12-13 20:15:29 +00001304 {
paul9985f832005-02-09 15:51:56 +00001305 stream_forward_getp (peer->ibuf, length);
Paul Jakmab881c702010-11-23 16:35:42 +00001306 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00001307 }
1308
Denis Ovsienkoa624cae2011-10-08 13:54:48 +04001309 attr->local_pref = stream_getl (peer->ibuf);
paul718e3742002-12-13 20:15:29 +00001310
1311 /* Set atomic aggregate flag. */
1312 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF);
1313
Paul Jakmab881c702010-11-23 16:35:42 +00001314 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00001315}
1316
1317/* Atomic aggregate. */
paul94f2b392005-06-28 12:44:16 +00001318static int
Paul Jakma835315b2012-01-18 12:28:30 +00001319bgp_attr_atomic (struct bgp_attr_parser_args *args)
paul718e3742002-12-13 20:15:29 +00001320{
Paul Jakma835315b2012-01-18 12:28:30 +00001321 struct peer *const peer = args->peer;
1322 struct attr *const attr = args->attr;
1323 const bgp_size_t length = args->length;
Paul Jakma3ecab4c2012-01-17 13:31:33 +00001324
Denis Ovsienko9eba2ad2011-09-20 14:43:50 +04001325 /* Length check. */
paul718e3742002-12-13 20:15:29 +00001326 if (length != 0)
1327 {
Paul Jakma835315b2012-01-18 12:28:30 +00001328 zlog (peer->log, LOG_ERR, "ATOMIC_AGGREGATE attribute length isn't 0 [%u]",
1329 length);
1330 return bgp_attr_malformed (args,
Paul Jakmab881c702010-11-23 16:35:42 +00001331 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
Paul Jakma835315b2012-01-18 12:28:30 +00001332 args->total);
paul718e3742002-12-13 20:15:29 +00001333 }
1334
1335 /* Set atomic aggregate flag. */
1336 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
1337
Paul Jakmab881c702010-11-23 16:35:42 +00001338 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00001339}
1340
1341/* Aggregator attribute */
paul94f2b392005-06-28 12:44:16 +00001342static int
Paul Jakma835315b2012-01-18 12:28:30 +00001343bgp_attr_aggregator (struct bgp_attr_parser_args *args)
paul718e3742002-12-13 20:15:29 +00001344{
Paul Jakma835315b2012-01-18 12:28:30 +00001345 struct peer *const peer = args->peer;
1346 struct attr *const attr = args->attr;
1347 const bgp_size_t length = args->length;
1348
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001349 int wantedlen = 6;
Paul Jakmafb982c22007-05-04 20:15:47 +00001350 struct attr_extra *attre = bgp_attr_extra_get (attr);
Paul Jakma3ecab4c2012-01-17 13:31:33 +00001351
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001352 /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
Paul Jakmab881c702010-11-23 16:35:42 +00001353 if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001354 wantedlen = 8;
1355
1356 if (length != wantedlen)
paul718e3742002-12-13 20:15:29 +00001357 {
Paul Jakma835315b2012-01-18 12:28:30 +00001358 zlog (peer->log, LOG_ERR, "AGGREGATOR attribute length isn't %u [%u]",
1359 wantedlen, length);
1360 return bgp_attr_malformed (args,
Paul Jakmab881c702010-11-23 16:35:42 +00001361 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
Paul Jakma835315b2012-01-18 12:28:30 +00001362 args->total);
paul718e3742002-12-13 20:15:29 +00001363 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001364
1365 if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) )
1366 attre->aggregator_as = stream_getl (peer->ibuf);
1367 else
1368 attre->aggregator_as = stream_getw (peer->ibuf);
Paul Jakmafb982c22007-05-04 20:15:47 +00001369 attre->aggregator_addr.s_addr = stream_get_ipv4 (peer->ibuf);
paul718e3742002-12-13 20:15:29 +00001370
1371 /* Set atomic aggregate flag. */
1372 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR);
1373
Paul Jakmab881c702010-11-23 16:35:42 +00001374 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00001375}
1376
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001377/* New Aggregator attribute */
Paul Jakmab881c702010-11-23 16:35:42 +00001378static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +00001379bgp_attr_as4_aggregator (struct bgp_attr_parser_args *args,
1380 as_t *as4_aggregator_as,
1381 struct in_addr *as4_aggregator_addr)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001382{
Paul Jakma835315b2012-01-18 12:28:30 +00001383 struct peer *const peer = args->peer;
1384 struct attr *const attr = args->attr;
1385 const bgp_size_t length = args->length;
1386
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001387 if (length != 8)
1388 {
Paul Jakma835315b2012-01-18 12:28:30 +00001389 zlog (peer->log, LOG_ERR, "New Aggregator length is not 8 [%d]",
1390 length);
1391 return bgp_attr_malformed (args,
Paul Jakmab881c702010-11-23 16:35:42 +00001392 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
Paul Jakma835315b2012-01-18 12:28:30 +00001393 0);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001394 }
Paul Jakma3ecab4c2012-01-17 13:31:33 +00001395
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001396 *as4_aggregator_as = stream_getl (peer->ibuf);
1397 as4_aggregator_addr->s_addr = stream_get_ipv4 (peer->ibuf);
1398
1399 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR);
1400
Paul Jakmab881c702010-11-23 16:35:42 +00001401 return BGP_ATTR_PARSE_PROCEED;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001402}
1403
1404/* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
1405 */
Paul Jakmab881c702010-11-23 16:35:42 +00001406static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +00001407bgp_attr_munge_as4_attrs (struct peer *const peer,
1408 struct attr *const attr,
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001409 struct aspath *as4_path, as_t as4_aggregator,
1410 struct in_addr *as4_aggregator_addr)
1411{
1412 int ignore_as4_path = 0;
1413 struct aspath *newpath;
1414 struct attr_extra *attre = attr->extra;
Paul Jakma055086f2014-09-23 15:23:01 +01001415
1416 if (!attr->aspath)
1417 {
1418 /* NULL aspath shouldn't be possible as bgp_attr_parse should have
1419 * checked that all well-known, mandatory attributes were present.
1420 *
1421 * Can only be a problem with peer itself - hard error
1422 */
1423 return BGP_ATTR_PARSE_ERROR;
1424 }
1425
Paul Jakmab881c702010-11-23 16:35:42 +00001426 if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001427 {
1428 /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
1429 * if given.
1430 * It is worth a warning though, because the peer really
1431 * should not send them
1432 */
1433 if (BGP_DEBUG(as4, AS4))
1434 {
1435 if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH)))
1436 zlog_debug ("[AS4] %s %s AS4_PATH",
1437 peer->host, "AS4 capable peer, yet it sent");
1438
1439 if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR)))
1440 zlog_debug ("[AS4] %s %s AS4_AGGREGATOR",
1441 peer->host, "AS4 capable peer, yet it sent");
1442 }
1443
Paul Jakmab881c702010-11-23 16:35:42 +00001444 return BGP_ATTR_PARSE_PROCEED;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001445 }
1446
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001447 /* We have a asn16 peer. First, look for AS4_AGGREGATOR
1448 * because that may override AS4_PATH
1449 */
1450 if (attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR) ) )
1451 {
Paul Jakmab881c702010-11-23 16:35:42 +00001452 if (attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR) ) )
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001453 {
Paul Jakma370b64a2007-12-22 16:49:52 +00001454 assert (attre);
1455
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001456 /* received both.
1457 * if the as_number in aggregator is not AS_TRANS,
1458 * then AS4_AGGREGATOR and AS4_PATH shall be ignored
1459 * and the Aggregator shall be taken as
1460 * info on the aggregating node, and the AS_PATH
1461 * shall be taken as the AS_PATH
1462 * otherwise
1463 * the Aggregator shall be ignored and the
1464 * AS4_AGGREGATOR shall be taken as the
1465 * Aggregating node and the AS_PATH is to be
1466 * constructed "as in all other cases"
1467 */
Paul Jakmab881c702010-11-23 16:35:42 +00001468 if (attre->aggregator_as != BGP_AS_TRANS)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001469 {
1470 /* ignore */
1471 if ( BGP_DEBUG(as4, AS4))
1472 zlog_debug ("[AS4] %s BGP not AS4 capable peer"
1473 " send AGGREGATOR != AS_TRANS and"
1474 " AS4_AGGREGATOR, so ignore"
1475 " AS4_AGGREGATOR and AS4_PATH", peer->host);
1476 ignore_as4_path = 1;
1477 }
1478 else
1479 {
1480 /* "New_aggregator shall be taken as aggregator" */
1481 attre->aggregator_as = as4_aggregator;
1482 attre->aggregator_addr.s_addr = as4_aggregator_addr->s_addr;
1483 }
1484 }
1485 else
1486 {
1487 /* We received a AS4_AGGREGATOR but no AGGREGATOR.
1488 * That is bogus - but reading the conditions
1489 * we have to handle AS4_AGGREGATOR as if it were
1490 * AGGREGATOR in that case
1491 */
1492 if ( BGP_DEBUG(as4, AS4))
1493 zlog_debug ("[AS4] %s BGP not AS4 capable peer send"
1494 " AS4_AGGREGATOR but no AGGREGATOR, will take"
1495 " it as if AGGREGATOR with AS_TRANS had been there", peer->host);
Paul Jakma370b64a2007-12-22 16:49:52 +00001496 (attre = bgp_attr_extra_get (attr))->aggregator_as = as4_aggregator;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001497 /* sweep it under the carpet and simulate a "good" AGGREGATOR */
1498 attr->flag |= (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR));
1499 }
1500 }
1501
1502 /* need to reconcile NEW_AS_PATH and AS_PATH */
Paul Jakmab881c702010-11-23 16:35:42 +00001503 if (!ignore_as4_path && (attr->flag & (ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH))))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001504 {
Paul Jakma055086f2014-09-23 15:23:01 +01001505 newpath = aspath_reconcile_as4 (attr->aspath, as4_path);
1506 aspath_unintern (&attr->aspath);
1507 attr->aspath = aspath_intern (newpath);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001508 }
Paul Jakmab881c702010-11-23 16:35:42 +00001509 return BGP_ATTR_PARSE_PROCEED;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001510}
1511
paul718e3742002-12-13 20:15:29 +00001512/* Community attribute. */
Paul Jakmab881c702010-11-23 16:35:42 +00001513static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +00001514bgp_attr_community (struct bgp_attr_parser_args *args)
paul718e3742002-12-13 20:15:29 +00001515{
Paul Jakma835315b2012-01-18 12:28:30 +00001516 struct peer *const peer = args->peer;
1517 struct attr *const attr = args->attr;
1518 const bgp_size_t length = args->length;
Paul Jakmab881c702010-11-23 16:35:42 +00001519
paul718e3742002-12-13 20:15:29 +00001520 if (length == 0)
Paul Jakmab2ceea12007-09-07 14:24:55 +00001521 {
1522 attr->community = NULL;
Paul Jakmab881c702010-11-23 16:35:42 +00001523 return BGP_ATTR_PARSE_PROCEED;
Paul Jakmab2ceea12007-09-07 14:24:55 +00001524 }
Paul Jakma0c466382010-12-05 17:17:26 +00001525
1526 attr->community =
1527 community_parse ((u_int32_t *)stream_pnt (peer->ibuf), length);
1528
1529 /* XXX: fix community_parse to use stream API and remove this */
1530 stream_forward_getp (peer->ibuf, length);
paul718e3742002-12-13 20:15:29 +00001531
Paul Jakma0c466382010-12-05 17:17:26 +00001532 if (!attr->community)
Paul Jakma835315b2012-01-18 12:28:30 +00001533 return bgp_attr_malformed (args,
Paul Jakmab881c702010-11-23 16:35:42 +00001534 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
Paul Jakma835315b2012-01-18 12:28:30 +00001535 args->total);
Paul Jakma0c466382010-12-05 17:17:26 +00001536
paul718e3742002-12-13 20:15:29 +00001537 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES);
1538
Paul Jakmab881c702010-11-23 16:35:42 +00001539 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00001540}
1541
1542/* Originator ID attribute. */
Paul Jakmab881c702010-11-23 16:35:42 +00001543static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +00001544bgp_attr_originator_id (struct bgp_attr_parser_args *args)
paul718e3742002-12-13 20:15:29 +00001545{
Paul Jakma835315b2012-01-18 12:28:30 +00001546 struct peer *const peer = args->peer;
1547 struct attr *const attr = args->attr;
1548 const bgp_size_t length = args->length;
1549
Denis Ovsienkod595b562011-09-30 15:08:54 +04001550 /* Length check. */
paul718e3742002-12-13 20:15:29 +00001551 if (length != 4)
1552 {
1553 zlog (peer->log, LOG_ERR, "Bad originator ID length %d", length);
1554
Paul Jakma835315b2012-01-18 12:28:30 +00001555 return bgp_attr_malformed (args,
Paul Jakmab881c702010-11-23 16:35:42 +00001556 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
Paul Jakma835315b2012-01-18 12:28:30 +00001557 args->total);
paul718e3742002-12-13 20:15:29 +00001558 }
1559
Paul Jakmafb982c22007-05-04 20:15:47 +00001560 (bgp_attr_extra_get (attr))->originator_id.s_addr
1561 = stream_get_ipv4 (peer->ibuf);
paul718e3742002-12-13 20:15:29 +00001562
1563 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID);
1564
Paul Jakmab881c702010-11-23 16:35:42 +00001565 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00001566}
1567
1568/* Cluster list attribute. */
Paul Jakmab881c702010-11-23 16:35:42 +00001569static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +00001570bgp_attr_cluster_list (struct bgp_attr_parser_args *args)
paul718e3742002-12-13 20:15:29 +00001571{
Paul Jakma835315b2012-01-18 12:28:30 +00001572 struct peer *const peer = args->peer;
1573 struct attr *const attr = args->attr;
1574 const bgp_size_t length = args->length;
1575
paul718e3742002-12-13 20:15:29 +00001576 /* Check length. */
1577 if (length % 4)
1578 {
1579 zlog (peer->log, LOG_ERR, "Bad cluster list length %d", length);
1580
Paul Jakma835315b2012-01-18 12:28:30 +00001581 return bgp_attr_malformed (args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
1582 args->total);
paul718e3742002-12-13 20:15:29 +00001583 }
1584
Paul Jakmafb982c22007-05-04 20:15:47 +00001585 (bgp_attr_extra_get (attr))->cluster
1586 = cluster_parse ((struct in_addr *)stream_pnt (peer->ibuf), length);
Paul Jakmab881c702010-11-23 16:35:42 +00001587
1588 /* XXX: Fix cluster_parse to use stream API and then remove this */
1589 stream_forward_getp (peer->ibuf, length);
paul718e3742002-12-13 20:15:29 +00001590
1591 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST);
1592
Paul Jakmab881c702010-11-23 16:35:42 +00001593 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00001594}
1595
1596/* Multiprotocol reachability information parse. */
Paul Jakma03292802008-06-07 20:37:10 +00001597int
Paul Jakma835315b2012-01-18 12:28:30 +00001598bgp_mp_reach_parse (struct bgp_attr_parser_args *args,
1599 struct bgp_nlri *mp_update)
paul718e3742002-12-13 20:15:29 +00001600{
Michael Lambert4c9641b2010-07-22 13:20:55 -04001601 afi_t afi;
1602 safi_t safi;
paul718e3742002-12-13 20:15:29 +00001603 bgp_size_t nlri_len;
Paul Jakma6e4ab122007-04-10 19:36:48 +00001604 size_t start;
paul718e3742002-12-13 20:15:29 +00001605 int ret;
1606 struct stream *s;
Paul Jakma835315b2012-01-18 12:28:30 +00001607 struct peer *const peer = args->peer;
1608 struct attr *const attr = args->attr;
1609 const bgp_size_t length = args->length;
Paul Jakmafb982c22007-05-04 20:15:47 +00001610 struct attr_extra *attre = bgp_attr_extra_get(attr);
Paul Jakma835315b2012-01-18 12:28:30 +00001611
paul718e3742002-12-13 20:15:29 +00001612 /* Set end of packet. */
Paul Jakma6e4ab122007-04-10 19:36:48 +00001613 s = BGP_INPUT(peer);
1614 start = stream_get_getp(s);
1615
1616 /* safe to read statically sized header? */
1617#define BGP_MP_REACH_MIN_SIZE 5
Paul Jakma03292802008-06-07 20:37:10 +00001618#define LEN_LEFT (length - (stream_get_getp(s) - start))
Paul Jakma6e4ab122007-04-10 19:36:48 +00001619 if ((length > STREAM_READABLE(s)) || (length < BGP_MP_REACH_MIN_SIZE))
Paul Jakma03292802008-06-07 20:37:10 +00001620 {
1621 zlog_info ("%s: %s sent invalid length, %lu",
1622 __func__, peer->host, (unsigned long)length);
David Lamparterf57000c2014-06-04 01:01:10 +02001623 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
Paul Jakma03292802008-06-07 20:37:10 +00001624 }
Paul Jakma6e4ab122007-04-10 19:36:48 +00001625
paul718e3742002-12-13 20:15:29 +00001626 /* Load AFI, SAFI. */
1627 afi = stream_getw (s);
1628 safi = stream_getc (s);
1629
1630 /* Get nexthop length. */
Paul Jakmafb982c22007-05-04 20:15:47 +00001631 attre->mp_nexthop_len = stream_getc (s);
Paul Jakma6e4ab122007-04-10 19:36:48 +00001632
Paul Jakma03292802008-06-07 20:37:10 +00001633 if (LEN_LEFT < attre->mp_nexthop_len)
1634 {
1635 zlog_info ("%s: %s, MP nexthop length, %u, goes past end of attribute",
1636 __func__, peer->host, attre->mp_nexthop_len);
David Lamparterf57000c2014-06-04 01:01:10 +02001637 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
Paul Jakma03292802008-06-07 20:37:10 +00001638 }
Paul Jakma6e4ab122007-04-10 19:36:48 +00001639
paul718e3742002-12-13 20:15:29 +00001640 /* Nexthop length check. */
Paul Jakmafb982c22007-05-04 20:15:47 +00001641 switch (attre->mp_nexthop_len)
paul718e3742002-12-13 20:15:29 +00001642 {
1643 case 4:
Paul Jakmafb982c22007-05-04 20:15:47 +00001644 stream_get (&attre->mp_nexthop_global_in, s, 4);
Michael Lambert66bed4f2009-07-28 11:26:14 -04001645 /* Probably needed for RFC 2283 */
1646 if (attr->nexthop.s_addr == 0)
1647 memcpy(&attr->nexthop.s_addr, &attre->mp_nexthop_global_in, 4);
paul718e3742002-12-13 20:15:29 +00001648 break;
1649 case 12:
Stephen Hemminger9206f9e2011-12-18 19:43:40 +04001650 stream_getl (s); /* RD high */
1651 stream_getl (s); /* RD low */
1652 stream_get (&attre->mp_nexthop_global_in, s, 4);
paul718e3742002-12-13 20:15:29 +00001653 break;
Lou Berger9da04bc2016-01-12 13:41:55 -05001654 case 24:
1655 {
1656 u_int32_t rd_high __attribute__((unused));
1657 u_int32_t rd_low __attribute__((unused));
1658
1659 rd_high = stream_getl (s);
1660 rd_low = stream_getl (s);
1661 }
1662 /* fall through */
paul718e3742002-12-13 20:15:29 +00001663 case 16:
Paul Jakmafb982c22007-05-04 20:15:47 +00001664 stream_get (&attre->mp_nexthop_global, s, 16);
paul718e3742002-12-13 20:15:29 +00001665 break;
1666 case 32:
Lou Berger9da04bc2016-01-12 13:41:55 -05001667 case 48:
1668 if (attre->mp_nexthop_len == 48) {
1669 u_int32_t rd_high __attribute__((unused));
1670 u_int32_t rd_low __attribute__((unused));
1671
1672 rd_high = stream_getl (s);
1673 rd_low = stream_getl (s);
1674 }
Paul Jakmafb982c22007-05-04 20:15:47 +00001675 stream_get (&attre->mp_nexthop_global, s, 16);
Lou Berger9da04bc2016-01-12 13:41:55 -05001676
1677 if (attre->mp_nexthop_len == 48) {
1678 u_int32_t rd_high __attribute__((unused));
1679 u_int32_t rd_low __attribute__((unused));
1680
1681 rd_high = stream_getl (s);
1682 rd_low = stream_getl (s);
1683 }
Paul Jakmafb982c22007-05-04 20:15:47 +00001684 stream_get (&attre->mp_nexthop_local, s, 16);
1685 if (! IN6_IS_ADDR_LINKLOCAL (&attre->mp_nexthop_local))
paul718e3742002-12-13 20:15:29 +00001686 {
1687 char buf1[INET6_ADDRSTRLEN];
1688 char buf2[INET6_ADDRSTRLEN];
1689
1690 if (BGP_DEBUG (update, UPDATE_IN))
ajs557865c2004-12-08 19:59:11 +00001691 zlog_debug ("%s got two nexthop %s %s but second one is not a link-local nexthop", peer->host,
Paul Jakmafb982c22007-05-04 20:15:47 +00001692 inet_ntop (AF_INET6, &attre->mp_nexthop_global,
paul718e3742002-12-13 20:15:29 +00001693 buf1, INET6_ADDRSTRLEN),
Paul Jakmafb982c22007-05-04 20:15:47 +00001694 inet_ntop (AF_INET6, &attre->mp_nexthop_local,
paul718e3742002-12-13 20:15:29 +00001695 buf2, INET6_ADDRSTRLEN));
1696
Paul Jakmafb982c22007-05-04 20:15:47 +00001697 attre->mp_nexthop_len = 16;
paul718e3742002-12-13 20:15:29 +00001698 }
1699 break;
paul718e3742002-12-13 20:15:29 +00001700 default:
Paul Jakma03292802008-06-07 20:37:10 +00001701 zlog_info ("%s: (%s) Wrong multiprotocol next hop length: %d",
1702 __func__, peer->host, attre->mp_nexthop_len);
David Lamparterf57000c2014-06-04 01:01:10 +02001703 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
paul718e3742002-12-13 20:15:29 +00001704 }
1705
Paul Jakma03292802008-06-07 20:37:10 +00001706 if (!LEN_LEFT)
1707 {
1708 zlog_info ("%s: (%s) Failed to read SNPA and NLRI(s)",
1709 __func__, peer->host);
David Lamparterf57000c2014-06-04 01:01:10 +02001710 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
Paul Jakma03292802008-06-07 20:37:10 +00001711 }
paul718e3742002-12-13 20:15:29 +00001712
Paul Jakma6e4ab122007-04-10 19:36:48 +00001713 {
1714 u_char val;
1715 if ((val = stream_getc (s)))
1716 zlog_warn ("%s sent non-zero value, %u, for defunct SNPA-length field",
1717 peer->host, val);
1718 }
1719
1720 /* must have nrli_len, what is left of the attribute */
Paul Jakma03292802008-06-07 20:37:10 +00001721 nlri_len = LEN_LEFT;
Paul Jakma6e4ab122007-04-10 19:36:48 +00001722 if ((!nlri_len) || (nlri_len > STREAM_READABLE(s)))
Paul Jakma03292802008-06-07 20:37:10 +00001723 {
1724 zlog_info ("%s: (%s) Failed to read NLRI",
1725 __func__, peer->host);
David Lamparterf57000c2014-06-04 01:01:10 +02001726 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
Paul Jakma03292802008-06-07 20:37:10 +00001727 }
Paul Jakma18ab08b2016-01-27 16:37:33 +00001728
paul718e3742002-12-13 20:15:29 +00001729 mp_update->afi = afi;
1730 mp_update->safi = safi;
1731 mp_update->nlri = stream_pnt (s);
1732 mp_update->length = nlri_len;
1733
Paul Jakma18ab08b2016-01-27 16:37:33 +00001734 ret = bgp_nlri_sanity_check (peer, mp_update);
1735 if (ret < 0)
1736 {
1737 zlog_info ("%s: (%s) NLRI doesn't pass sanity check",
1738 __func__, peer->host);
1739 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
1740 }
1741
paul9985f832005-02-09 15:51:56 +00001742 stream_forward_getp (s, nlri_len);
paul718e3742002-12-13 20:15:29 +00001743
David Lamparterdaefeb82014-12-08 17:42:12 +01001744 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_MP_REACH_NLRI);
1745
Paul Jakmab881c702010-11-23 16:35:42 +00001746 return BGP_ATTR_PARSE_PROCEED;
Paul Jakma03292802008-06-07 20:37:10 +00001747#undef LEN_LEFT
paul718e3742002-12-13 20:15:29 +00001748}
1749
1750/* Multiprotocol unreachable parse */
Paul Jakma03292802008-06-07 20:37:10 +00001751int
Paul Jakma835315b2012-01-18 12:28:30 +00001752bgp_mp_unreach_parse (struct bgp_attr_parser_args *args,
paul718e3742002-12-13 20:15:29 +00001753 struct bgp_nlri *mp_withdraw)
1754{
1755 struct stream *s;
Michael Lambert4c9641b2010-07-22 13:20:55 -04001756 afi_t afi;
1757 safi_t safi;
paul718e3742002-12-13 20:15:29 +00001758 u_int16_t withdraw_len;
Paul Jakma835315b2012-01-18 12:28:30 +00001759 struct peer *const peer = args->peer;
David Lamparterdaefeb82014-12-08 17:42:12 +01001760 struct attr *const attr = args->attr;
Paul Jakma835315b2012-01-18 12:28:30 +00001761 const bgp_size_t length = args->length;
paul718e3742002-12-13 20:15:29 +00001762
1763 s = peer->ibuf;
Paul Jakma6e4ab122007-04-10 19:36:48 +00001764
1765#define BGP_MP_UNREACH_MIN_SIZE 3
1766 if ((length > STREAM_READABLE(s)) || (length < BGP_MP_UNREACH_MIN_SIZE))
David Lamparterf57000c2014-06-04 01:01:10 +02001767 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
Paul Jakma6e4ab122007-04-10 19:36:48 +00001768
paul718e3742002-12-13 20:15:29 +00001769 afi = stream_getw (s);
1770 safi = stream_getc (s);
Paul Jakma6e4ab122007-04-10 19:36:48 +00001771
1772 withdraw_len = length - BGP_MP_UNREACH_MIN_SIZE;
paul718e3742002-12-13 20:15:29 +00001773
paul718e3742002-12-13 20:15:29 +00001774 mp_withdraw->afi = afi;
1775 mp_withdraw->safi = safi;
1776 mp_withdraw->nlri = stream_pnt (s);
1777 mp_withdraw->length = withdraw_len;
1778
Paul Jakma18ab08b2016-01-27 16:37:33 +00001779 if (bgp_nlri_sanity_check (peer, mp_withdraw) < 0)
1780 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
1781
paul9985f832005-02-09 15:51:56 +00001782 stream_forward_getp (s, withdraw_len);
paul718e3742002-12-13 20:15:29 +00001783
David Lamparterdaefeb82014-12-08 17:42:12 +01001784 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_MP_UNREACH_NLRI);
1785
Paul Jakmab881c702010-11-23 16:35:42 +00001786 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00001787}
1788
1789/* Extended Community attribute. */
Paul Jakmab881c702010-11-23 16:35:42 +00001790static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +00001791bgp_attr_ext_communities (struct bgp_attr_parser_args *args)
paul718e3742002-12-13 20:15:29 +00001792{
Paul Jakma835315b2012-01-18 12:28:30 +00001793 struct peer *const peer = args->peer;
1794 struct attr *const attr = args->attr;
1795 const bgp_size_t length = args->length;
Paul Jakmab881c702010-11-23 16:35:42 +00001796
paul718e3742002-12-13 20:15:29 +00001797 if (length == 0)
Paul Jakmafb982c22007-05-04 20:15:47 +00001798 {
1799 if (attr->extra)
1800 attr->extra->ecommunity = NULL;
Paul Jakma0c466382010-12-05 17:17:26 +00001801 /* Empty extcomm doesn't seem to be invalid per se */
Paul Jakmab881c702010-11-23 16:35:42 +00001802 return BGP_ATTR_PARSE_PROCEED;
Paul Jakmafb982c22007-05-04 20:15:47 +00001803 }
Paul Jakma0c466382010-12-05 17:17:26 +00001804
1805 (bgp_attr_extra_get (attr))->ecommunity =
1806 ecommunity_parse ((u_int8_t *)stream_pnt (peer->ibuf), length);
1807 /* XXX: fix ecommunity_parse to use stream API */
1808 stream_forward_getp (peer->ibuf, length);
1809
1810 if (!attr->extra->ecommunity)
Paul Jakma835315b2012-01-18 12:28:30 +00001811 return bgp_attr_malformed (args,
1812 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
1813 args->total);
Paul Jakma0c466382010-12-05 17:17:26 +00001814
paul718e3742002-12-13 20:15:29 +00001815 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES);
1816
Paul Jakmab881c702010-11-23 16:35:42 +00001817 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00001818}
1819
Lou Bergerc3741782016-01-12 13:42:01 -05001820/* Parse Tunnel Encap attribute in an UPDATE */
1821static int
1822bgp_attr_encap(
1823 uint8_t type,
1824 struct peer *peer, /* IN */
1825 bgp_size_t length, /* IN: attr's length field */
1826 struct attr *attr, /* IN: caller already allocated */
1827 u_char flag, /* IN: attr's flags field */
1828 u_char *startp)
1829{
1830 bgp_size_t total;
1831 struct attr_extra *attre = NULL;
1832 struct bgp_attr_encap_subtlv *stlv_last = NULL;
1833 uint16_t tunneltype;
1834
1835 total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
1836
1837 if (!CHECK_FLAG(flag, BGP_ATTR_FLAG_TRANS)
1838 || !CHECK_FLAG(flag, BGP_ATTR_FLAG_OPTIONAL))
1839 {
1840 zlog (peer->log, LOG_ERR,
1841 "Tunnel Encap attribute flag isn't optional and transitive %d", flag);
1842 bgp_notify_send_with_data (peer,
1843 BGP_NOTIFY_UPDATE_ERR,
1844 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
1845 startp, total);
1846 return -1;
1847 }
1848
1849 if (BGP_ATTR_ENCAP == type) {
1850 /* read outer TLV type and length */
1851 uint16_t tlv_length;
1852
1853 if (length < 4) {
1854 zlog (peer->log, LOG_ERR,
1855 "Tunnel Encap attribute not long enough to contain outer T,L");
1856 bgp_notify_send_with_data(peer,
1857 BGP_NOTIFY_UPDATE_ERR,
1858 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
1859 startp, total);
1860 return -1;
1861 }
1862 tunneltype = stream_getw (BGP_INPUT (peer));
1863 tlv_length = stream_getw (BGP_INPUT (peer));
1864 length -= 4;
1865
1866 if (tlv_length != length) {
1867 zlog (peer->log, LOG_ERR, "%s: tlv_length(%d) != length(%d)",
1868 __func__, tlv_length, length);
1869 }
1870 }
1871
1872 while (length >= 4) {
1873 uint16_t subtype;
1874 uint16_t sublength;
1875 struct bgp_attr_encap_subtlv *tlv;
1876
Lou Berger298cc2f2016-01-12 13:42:02 -05001877 if (BGP_ATTR_ENCAP == type) {
1878 subtype = stream_getc (BGP_INPUT (peer));
1879 sublength = stream_getc (BGP_INPUT (peer));
1880 length -= 2;
1881 }
Lou Bergerc3741782016-01-12 13:42:01 -05001882
1883 if (sublength > length) {
1884 zlog (peer->log, LOG_ERR,
1885 "Tunnel Encap attribute sub-tlv length %d exceeds remaining length %d",
1886 sublength, length);
1887 bgp_notify_send_with_data (peer,
1888 BGP_NOTIFY_UPDATE_ERR,
1889 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
1890 startp, total);
1891 return -1;
1892 }
1893
1894 /* alloc and copy sub-tlv */
1895 /* TBD make sure these are freed when attributes are released */
1896 tlv = XCALLOC (MTYPE_ENCAP_TLV, sizeof(struct bgp_attr_encap_subtlv)-1+sublength);
1897 tlv->type = subtype;
1898 tlv->length = sublength;
1899 stream_get(tlv->value, peer->ibuf, sublength);
1900 length -= sublength;
1901
1902 /* attach tlv to encap chain */
1903 if (!attre) {
1904 attre = bgp_attr_extra_get(attr);
1905 if (BGP_ATTR_ENCAP == type) {
1906 for (stlv_last = attre->encap_subtlvs; stlv_last && stlv_last->next;
1907 stlv_last = stlv_last->next);
1908 if (stlv_last) {
1909 stlv_last->next = tlv;
1910 } else {
1911 attre->encap_subtlvs = tlv;
1912 }
1913 }
1914 } else {
1915 stlv_last->next = tlv;
1916 }
1917 stlv_last = tlv;
1918 }
1919
1920 if (attre && (BGP_ATTR_ENCAP == type)) {
1921 attre->encap_tunneltype = tunneltype;
1922 }
1923
1924 if (length) {
1925 /* spurious leftover data */
1926 zlog (peer->log, LOG_ERR,
1927 "Tunnel Encap attribute length is bad: %d leftover octets", length);
1928 bgp_notify_send_with_data (peer,
1929 BGP_NOTIFY_UPDATE_ERR,
1930 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
1931 startp, total);
1932 return -1;
1933 }
1934
1935 return 0;
1936}
1937
paul718e3742002-12-13 20:15:29 +00001938/* BGP unknown attribute treatment. */
Paul Jakmab881c702010-11-23 16:35:42 +00001939static bgp_attr_parse_ret_t
Paul Jakma835315b2012-01-18 12:28:30 +00001940bgp_attr_unknown (struct bgp_attr_parser_args *args)
paul718e3742002-12-13 20:15:29 +00001941{
Paul Jakma8794e8d2012-02-13 13:53:07 +00001942 bgp_size_t total = args->total;
paul718e3742002-12-13 20:15:29 +00001943 struct transit *transit;
Paul Jakmafb982c22007-05-04 20:15:47 +00001944 struct attr_extra *attre;
Paul Jakma835315b2012-01-18 12:28:30 +00001945 struct peer *const peer = args->peer;
1946 struct attr *const attr = args->attr;
1947 u_char *const startp = args->startp;
1948 const u_char type = args->type;
1949 const u_char flag = args->flags;
1950 const bgp_size_t length = args->length;
1951
paul718e3742002-12-13 20:15:29 +00001952
hassof4184462005-02-01 20:13:16 +00001953 if (BGP_DEBUG (normal, NORMAL))
1954 zlog_debug ("%s Unknown attribute is received (type %d, length %d)",
1955 peer->host, type, length);
1956
paul718e3742002-12-13 20:15:29 +00001957 if (BGP_DEBUG (events, EVENTS))
ajs557865c2004-12-08 19:59:11 +00001958 zlog (peer->log, LOG_DEBUG,
paul718e3742002-12-13 20:15:29 +00001959 "Unknown attribute type %d length %d is received", type, length);
1960
1961 /* Forward read pointer of input stream. */
paul9985f832005-02-09 15:51:56 +00001962 stream_forward_getp (peer->ibuf, length);
paul718e3742002-12-13 20:15:29 +00001963
paul718e3742002-12-13 20:15:29 +00001964 /* If any of the mandatory well-known attributes are not recognized,
1965 then the Error Subcode is set to Unrecognized Well-known
1966 Attribute. The Data field contains the unrecognized attribute
1967 (type, length and value). */
Paul Jakmab881c702010-11-23 16:35:42 +00001968 if (!CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL))
paul718e3742002-12-13 20:15:29 +00001969 {
Paul Jakma835315b2012-01-18 12:28:30 +00001970 return bgp_attr_malformed (args,
Paul Jakmab881c702010-11-23 16:35:42 +00001971 BGP_NOTIFY_UPDATE_UNREC_ATTR,
Paul Jakma835315b2012-01-18 12:28:30 +00001972 args->total);
paul718e3742002-12-13 20:15:29 +00001973 }
1974
1975 /* Unrecognized non-transitive optional attributes must be quietly
1976 ignored and not passed along to other BGP peers. */
1977 if (! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS))
Paul Jakmab881c702010-11-23 16:35:42 +00001978 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00001979
1980 /* If a path with recognized transitive optional attribute is
1981 accepted and passed along to other BGP peers and the Partial bit
1982 in the Attribute Flags octet is set to 1 by some previous AS, it
1983 is not set back to 0 by the current AS. */
1984 SET_FLAG (*startp, BGP_ATTR_FLAG_PARTIAL);
1985
1986 /* Store transitive attribute to the end of attr->transit. */
Paul Jakmafb982c22007-05-04 20:15:47 +00001987 if (! ((attre = bgp_attr_extra_get(attr))->transit) )
Stephen Hemminger393deb92008-08-18 14:13:29 -07001988 attre->transit = XCALLOC (MTYPE_TRANSIT, sizeof (struct transit));
paul718e3742002-12-13 20:15:29 +00001989
Paul Jakmafb982c22007-05-04 20:15:47 +00001990 transit = attre->transit;
paul718e3742002-12-13 20:15:29 +00001991
1992 if (transit->val)
1993 transit->val = XREALLOC (MTYPE_TRANSIT_VAL, transit->val,
1994 transit->length + total);
1995 else
1996 transit->val = XMALLOC (MTYPE_TRANSIT_VAL, total);
1997
1998 memcpy (transit->val + transit->length, startp, total);
1999 transit->length += total;
2000
Paul Jakmab881c702010-11-23 16:35:42 +00002001 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00002002}
2003
Paul Jakma055086f2014-09-23 15:23:01 +01002004/* Well-known attribute check. */
2005static int
2006bgp_attr_check (struct peer *peer, struct attr *attr)
2007{
2008 u_char type = 0;
2009
Paul Jakmaaed1b552014-10-21 16:59:01 +01002010 /* BGP Graceful-Restart End-of-RIB for IPv4 unicast is signaled as an
2011 * empty UPDATE. */
2012 if (CHECK_FLAG (peer->cap, PEER_CAP_RESTART_RCV) && !attr->flag)
2013 return BGP_ATTR_PARSE_PROCEED;
2014
2015 /* "An UPDATE message that contains the MP_UNREACH_NLRI is not required
2016 to carry any other path attributes.", though if MP_REACH_NLRI or NLRI
2017 are present, it should. Check for any other attribute being present
2018 instead.
2019 */
2020 if (attr->flag == ATTR_FLAG_BIT (BGP_ATTR_MP_UNREACH_NLRI))
2021 return BGP_ATTR_PARSE_PROCEED;
2022
Paul Jakma055086f2014-09-23 15:23:01 +01002023 if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_ORIGIN)))
2024 type = BGP_ATTR_ORIGIN;
2025
2026 if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH)))
2027 type = BGP_ATTR_AS_PATH;
Paul Jakmaaed1b552014-10-21 16:59:01 +01002028
2029 /* RFC 2858 makes Next-Hop optional/ignored, if MP_REACH_NLRI is present and
2030 * NLRI is empty. We can't easily check NLRI empty here though.
2031 */
Paul Jakma1a211cb2014-11-01 17:21:47 +00002032 if (!CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP))
2033 && !CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_MP_REACH_NLRI)))
Paul Jakma055086f2014-09-23 15:23:01 +01002034 type = BGP_ATTR_NEXT_HOP;
Paul Jakmaaed1b552014-10-21 16:59:01 +01002035
Paul Jakma055086f2014-09-23 15:23:01 +01002036 if (peer->sort == BGP_PEER_IBGP
2037 && ! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF)))
2038 type = BGP_ATTR_LOCAL_PREF;
2039
2040 if (type)
2041 {
2042 zlog (peer->log, LOG_WARNING,
Paul Jakmaaed1b552014-10-21 16:59:01 +01002043 "%s Missing well-known attribute %d / %s",
2044 peer->host, type, LOOKUP (attr_str, type));
Paul Jakma055086f2014-09-23 15:23:01 +01002045 bgp_notify_send_with_data (peer,
2046 BGP_NOTIFY_UPDATE_ERR,
2047 BGP_NOTIFY_UPDATE_MISS_ATTR,
2048 &type, 1);
2049 return BGP_ATTR_PARSE_ERROR;
2050 }
2051 return BGP_ATTR_PARSE_PROCEED;
2052}
2053
paul718e3742002-12-13 20:15:29 +00002054/* Read attribute of update packet. This function is called from
Andrew Certain8b366b92012-11-07 23:50:08 +00002055 bgp_update_receive() in bgp_packet.c. */
Paul Jakmab881c702010-11-23 16:35:42 +00002056bgp_attr_parse_ret_t
paul718e3742002-12-13 20:15:29 +00002057bgp_attr_parse (struct peer *peer, struct attr *attr, bgp_size_t size,
2058 struct bgp_nlri *mp_update, struct bgp_nlri *mp_withdraw)
2059{
2060 int ret;
Paul Jakmab881c702010-11-23 16:35:42 +00002061 u_char flag = 0;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002062 u_char type = 0;
paul718e3742002-12-13 20:15:29 +00002063 bgp_size_t length;
2064 u_char *startp, *endp;
2065 u_char *attr_endp;
2066 u_char seen[BGP_ATTR_BITMAP_SIZE];
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002067 /* we need the as4_path only until we have synthesized the as_path with it */
2068 /* same goes for as4_aggregator */
2069 struct aspath *as4_path = NULL;
2070 as_t as4_aggregator = 0;
David Lamparter5181a022015-09-15 03:00:09 -07002071 struct in_addr as4_aggregator_addr = { .s_addr = 0 };
paul718e3742002-12-13 20:15:29 +00002072
2073 /* Initialize bitmap. */
2074 memset (seen, 0, BGP_ATTR_BITMAP_SIZE);
2075
2076 /* End pointer of BGP attribute. */
2077 endp = BGP_INPUT_PNT (peer) + size;
Paul Jakmab881c702010-11-23 16:35:42 +00002078
paul718e3742002-12-13 20:15:29 +00002079 /* Get attributes to the end of attribute length. */
2080 while (BGP_INPUT_PNT (peer) < endp)
2081 {
2082 /* Check remaining length check.*/
2083 if (endp - BGP_INPUT_PNT (peer) < BGP_ATTR_MIN_LEN)
2084 {
gdtc29fdba2004-12-09 14:46:46 +00002085 /* XXX warning: long int format, int arg (arg 5) */
paul718e3742002-12-13 20:15:29 +00002086 zlog (peer->log, LOG_WARNING,
heasleyd68ab102011-07-12 20:09:18 +04002087 "%s: error BGP attribute length %lu is smaller than min len",
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002088 peer->host,
2089 (unsigned long) (endp - STREAM_PNT (BGP_INPUT (peer))));
paul718e3742002-12-13 20:15:29 +00002090
2091 bgp_notify_send (peer,
2092 BGP_NOTIFY_UPDATE_ERR,
2093 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
Paul Jakmab881c702010-11-23 16:35:42 +00002094 return BGP_ATTR_PARSE_ERROR;
paul718e3742002-12-13 20:15:29 +00002095 }
2096
2097 /* Fetch attribute flag and type. */
2098 startp = BGP_INPUT_PNT (peer);
Denis Ovsienko2d42e682011-09-27 15:35:39 +04002099 /* "The lower-order four bits of the Attribute Flags octet are
2100 unused. They MUST be zero when sent and MUST be ignored when
2101 received." */
2102 flag = 0xF0 & stream_getc (BGP_INPUT (peer));
paul718e3742002-12-13 20:15:29 +00002103 type = stream_getc (BGP_INPUT (peer));
2104
Paul Jakma370b64a2007-12-22 16:49:52 +00002105 /* Check whether Extended-Length applies and is in bounds */
2106 if (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN)
2107 && ((endp - startp) < (BGP_ATTR_MIN_LEN + 1)))
2108 {
2109 zlog (peer->log, LOG_WARNING,
heasleyd68ab102011-07-12 20:09:18 +04002110 "%s: Extended length set, but just %lu bytes of attr header",
Paul Jakma370b64a2007-12-22 16:49:52 +00002111 peer->host,
2112 (unsigned long) (endp - STREAM_PNT (BGP_INPUT (peer))));
2113
2114 bgp_notify_send (peer,
2115 BGP_NOTIFY_UPDATE_ERR,
2116 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
Paul Jakmab881c702010-11-23 16:35:42 +00002117 return BGP_ATTR_PARSE_ERROR;
Paul Jakma370b64a2007-12-22 16:49:52 +00002118 }
Paul Jakma835315b2012-01-18 12:28:30 +00002119
paul718e3742002-12-13 20:15:29 +00002120 /* Check extended attribue length bit. */
2121 if (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN))
2122 length = stream_getw (BGP_INPUT (peer));
2123 else
2124 length = stream_getc (BGP_INPUT (peer));
2125
2126 /* If any attribute appears more than once in the UPDATE
2127 message, then the Error Subcode is set to Malformed Attribute
2128 List. */
2129
2130 if (CHECK_BITMAP (seen, type))
2131 {
2132 zlog (peer->log, LOG_WARNING,
heasleyd68ab102011-07-12 20:09:18 +04002133 "%s: error BGP attribute type %d appears twice in a message",
paul718e3742002-12-13 20:15:29 +00002134 peer->host, type);
2135
2136 bgp_notify_send (peer,
2137 BGP_NOTIFY_UPDATE_ERR,
2138 BGP_NOTIFY_UPDATE_MAL_ATTR);
Paul Jakmab881c702010-11-23 16:35:42 +00002139 return BGP_ATTR_PARSE_ERROR;
paul718e3742002-12-13 20:15:29 +00002140 }
2141
2142 /* Set type to bitmap to check duplicate attribute. `type' is
2143 unsigned char so it never overflow bitmap range. */
2144
2145 SET_BITMAP (seen, type);
2146
2147 /* Overflow check. */
2148 attr_endp = BGP_INPUT_PNT (peer) + length;
2149
2150 if (attr_endp > endp)
2151 {
2152 zlog (peer->log, LOG_WARNING,
heasleyd68ab102011-07-12 20:09:18 +04002153 "%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p", peer->host, type, length, size, attr_endp, endp);
paul718e3742002-12-13 20:15:29 +00002154 bgp_notify_send (peer,
2155 BGP_NOTIFY_UPDATE_ERR,
2156 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
Paul Jakmab881c702010-11-23 16:35:42 +00002157 return BGP_ATTR_PARSE_ERROR;
paul718e3742002-12-13 20:15:29 +00002158 }
Paul Jakma835315b2012-01-18 12:28:30 +00002159
2160 struct bgp_attr_parser_args attr_args = {
2161 .peer = peer,
2162 .length = length,
2163 .attr = attr,
2164 .type = type,
2165 .flags = flag,
2166 .startp = startp,
2167 .total = attr_endp - startp,
2168 };
2169
2170
2171 /* If any recognized attribute has Attribute Flags that conflict
2172 with the Attribute Type Code, then the Error Subcode is set to
2173 Attribute Flags Error. The Data field contains the erroneous
2174 attribute (type, length and value). */
2175 if (bgp_attr_flag_invalid (&attr_args))
Paul Jakmafa61e162012-03-25 21:31:47 +01002176 {
2177 bgp_attr_parse_ret_t ret;
2178 ret = bgp_attr_malformed (&attr_args,
2179 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
2180 attr_args.total);
2181 if (ret == BGP_ATTR_PARSE_PROCEED)
2182 continue;
2183 return ret;
2184 }
paul718e3742002-12-13 20:15:29 +00002185
2186 /* OK check attribute and store it's value. */
2187 switch (type)
2188 {
2189 case BGP_ATTR_ORIGIN:
Paul Jakma835315b2012-01-18 12:28:30 +00002190 ret = bgp_attr_origin (&attr_args);
paul718e3742002-12-13 20:15:29 +00002191 break;
2192 case BGP_ATTR_AS_PATH:
Paul Jakma835315b2012-01-18 12:28:30 +00002193 ret = bgp_attr_aspath (&attr_args);
paul718e3742002-12-13 20:15:29 +00002194 break;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002195 case BGP_ATTR_AS4_PATH:
Paul Jakma835315b2012-01-18 12:28:30 +00002196 ret = bgp_attr_as4_path (&attr_args, &as4_path);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002197 break;
paul718e3742002-12-13 20:15:29 +00002198 case BGP_ATTR_NEXT_HOP:
Paul Jakma835315b2012-01-18 12:28:30 +00002199 ret = bgp_attr_nexthop (&attr_args);
paul718e3742002-12-13 20:15:29 +00002200 break;
2201 case BGP_ATTR_MULTI_EXIT_DISC:
Paul Jakma835315b2012-01-18 12:28:30 +00002202 ret = bgp_attr_med (&attr_args);
paul718e3742002-12-13 20:15:29 +00002203 break;
2204 case BGP_ATTR_LOCAL_PREF:
Paul Jakma835315b2012-01-18 12:28:30 +00002205 ret = bgp_attr_local_pref (&attr_args);
paul718e3742002-12-13 20:15:29 +00002206 break;
2207 case BGP_ATTR_ATOMIC_AGGREGATE:
Paul Jakma835315b2012-01-18 12:28:30 +00002208 ret = bgp_attr_atomic (&attr_args);
paul718e3742002-12-13 20:15:29 +00002209 break;
2210 case BGP_ATTR_AGGREGATOR:
Paul Jakma835315b2012-01-18 12:28:30 +00002211 ret = bgp_attr_aggregator (&attr_args);
paul718e3742002-12-13 20:15:29 +00002212 break;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002213 case BGP_ATTR_AS4_AGGREGATOR:
Paul Jakma835315b2012-01-18 12:28:30 +00002214 ret = bgp_attr_as4_aggregator (&attr_args,
2215 &as4_aggregator,
2216 &as4_aggregator_addr);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002217 break;
paul718e3742002-12-13 20:15:29 +00002218 case BGP_ATTR_COMMUNITIES:
Paul Jakma835315b2012-01-18 12:28:30 +00002219 ret = bgp_attr_community (&attr_args);
paul718e3742002-12-13 20:15:29 +00002220 break;
2221 case BGP_ATTR_ORIGINATOR_ID:
Paul Jakma835315b2012-01-18 12:28:30 +00002222 ret = bgp_attr_originator_id (&attr_args);
paul718e3742002-12-13 20:15:29 +00002223 break;
2224 case BGP_ATTR_CLUSTER_LIST:
Paul Jakma835315b2012-01-18 12:28:30 +00002225 ret = bgp_attr_cluster_list (&attr_args);
paul718e3742002-12-13 20:15:29 +00002226 break;
2227 case BGP_ATTR_MP_REACH_NLRI:
Paul Jakma835315b2012-01-18 12:28:30 +00002228 ret = bgp_mp_reach_parse (&attr_args, mp_update);
paul718e3742002-12-13 20:15:29 +00002229 break;
2230 case BGP_ATTR_MP_UNREACH_NLRI:
Paul Jakma835315b2012-01-18 12:28:30 +00002231 ret = bgp_mp_unreach_parse (&attr_args, mp_withdraw);
paul718e3742002-12-13 20:15:29 +00002232 break;
2233 case BGP_ATTR_EXT_COMMUNITIES:
Paul Jakma835315b2012-01-18 12:28:30 +00002234 ret = bgp_attr_ext_communities (&attr_args);
paul718e3742002-12-13 20:15:29 +00002235 break;
Lou Bergerc3741782016-01-12 13:42:01 -05002236 case BGP_ATTR_ENCAP:
2237 ret = bgp_attr_encap (type, peer, length, attr, flag, startp);
2238 break;
paul718e3742002-12-13 20:15:29 +00002239 default:
Paul Jakma835315b2012-01-18 12:28:30 +00002240 ret = bgp_attr_unknown (&attr_args);
paul718e3742002-12-13 20:15:29 +00002241 break;
2242 }
Paul Jakmab881c702010-11-23 16:35:42 +00002243
David Lamparterf57000c2014-06-04 01:01:10 +02002244 if (ret == BGP_ATTR_PARSE_ERROR_NOTIFYPLS)
2245 {
2246 bgp_notify_send (peer,
2247 BGP_NOTIFY_UPDATE_ERR,
2248 BGP_NOTIFY_UPDATE_MAL_ATTR);
2249 ret = BGP_ATTR_PARSE_ERROR;
2250 }
2251
Paul Jakmab881c702010-11-23 16:35:42 +00002252 /* If hard error occured immediately return to the caller. */
2253 if (ret == BGP_ATTR_PARSE_ERROR)
Paul Jakma6e4ab122007-04-10 19:36:48 +00002254 {
2255 zlog (peer->log, LOG_WARNING,
2256 "%s: Attribute %s, parse error",
2257 peer->host,
2258 LOOKUP (attr_str, type));
Paul Jakmab881c702010-11-23 16:35:42 +00002259 if (as4_path)
2260 aspath_unintern (&as4_path);
2261 return ret;
Paul Jakma6e4ab122007-04-10 19:36:48 +00002262 }
Paul Jakmab881c702010-11-23 16:35:42 +00002263 if (ret == BGP_ATTR_PARSE_WITHDRAW)
2264 {
2265
2266 zlog (peer->log, LOG_WARNING,
2267 "%s: Attribute %s, parse error - treating as withdrawal",
2268 peer->host,
2269 LOOKUP (attr_str, type));
2270 if (as4_path)
2271 aspath_unintern (&as4_path);
2272 return ret;
2273 }
2274
paul718e3742002-12-13 20:15:29 +00002275 /* Check the fetched length. */
2276 if (BGP_INPUT_PNT (peer) != attr_endp)
2277 {
2278 zlog (peer->log, LOG_WARNING,
Paul Jakma6e4ab122007-04-10 19:36:48 +00002279 "%s: BGP attribute %s, fetch error",
2280 peer->host, LOOKUP (attr_str, type));
paul718e3742002-12-13 20:15:29 +00002281 bgp_notify_send (peer,
2282 BGP_NOTIFY_UPDATE_ERR,
2283 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
Paul Jakmab881c702010-11-23 16:35:42 +00002284 if (as4_path)
2285 aspath_unintern (&as4_path);
2286 return BGP_ATTR_PARSE_ERROR;
paul718e3742002-12-13 20:15:29 +00002287 }
2288 }
paul718e3742002-12-13 20:15:29 +00002289 /* Check final read pointer is same as end pointer. */
2290 if (BGP_INPUT_PNT (peer) != endp)
2291 {
2292 zlog (peer->log, LOG_WARNING,
heasleyd68ab102011-07-12 20:09:18 +04002293 "%s: BGP attribute %s, length mismatch",
Paul Jakma6e4ab122007-04-10 19:36:48 +00002294 peer->host, LOOKUP (attr_str, type));
paul718e3742002-12-13 20:15:29 +00002295 bgp_notify_send (peer,
2296 BGP_NOTIFY_UPDATE_ERR,
2297 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
Paul Jakmab881c702010-11-23 16:35:42 +00002298 if (as4_path)
2299 aspath_unintern (&as4_path);
2300 return BGP_ATTR_PARSE_ERROR;
paul718e3742002-12-13 20:15:29 +00002301 }
Paul Jakma055086f2014-09-23 15:23:01 +01002302
2303 /* Check all mandatory well-known attributes are present */
2304 {
2305 bgp_attr_parse_ret_t ret;
2306 if ((ret = bgp_attr_check (peer, attr)) < 0)
2307 {
2308 if (as4_path)
2309 aspath_unintern (&as4_path);
2310 return ret;
2311 }
2312 }
2313
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002314 /*
2315 * At this place we can see whether we got AS4_PATH and/or
2316 * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
2317 * We can not do this before we've read all attributes because
2318 * the as4 handling does not say whether AS4_PATH has to be sent
2319 * after AS_PATH or not - and when AS4_AGGREGATOR will be send
2320 * in relationship to AGGREGATOR.
2321 * So, to be defensive, we are not relying on any order and read
2322 * all attributes first, including these 32bit ones, and now,
2323 * afterwards, we look what and if something is to be done for as4.
Paul Jakmaaed1b552014-10-21 16:59:01 +01002324 *
2325 * It is possible to not have AS_PATH, e.g. GR EoR and sole
2326 * MP_UNREACH_NLRI.
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002327 */
David Lamparterf57000c2014-06-04 01:01:10 +02002328 /* actually... this doesn't ever return failure currently, but
2329 * better safe than sorry */
Paul Jakmaaed1b552014-10-21 16:59:01 +01002330 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH))
2331 && bgp_attr_munge_as4_attrs (peer, attr, as4_path,
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002332 as4_aggregator, &as4_aggregator_addr))
Paul Jakmab881c702010-11-23 16:35:42 +00002333 {
David Lamparterf57000c2014-06-04 01:01:10 +02002334 bgp_notify_send (peer,
2335 BGP_NOTIFY_UPDATE_ERR,
2336 BGP_NOTIFY_UPDATE_MAL_ATTR);
Paul Jakmab881c702010-11-23 16:35:42 +00002337 if (as4_path)
2338 aspath_unintern (&as4_path);
2339 return BGP_ATTR_PARSE_ERROR;
2340 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002341
2342 /* At this stage, we have done all fiddling with as4, and the
2343 * resulting info is in attr->aggregator resp. attr->aspath
2344 * so we can chuck as4_aggregator and as4_path alltogether in
2345 * order to save memory
2346 */
Paul Jakmab881c702010-11-23 16:35:42 +00002347 if (as4_path)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002348 {
Paul Jakmaf6f434b2010-11-23 21:28:03 +00002349 aspath_unintern (&as4_path); /* unintern - it is in the hash */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002350 /* The flag that we got this is still there, but that does not
2351 * do any trouble
2352 */
2353 }
2354 /*
2355 * The "rest" of the code does nothing with as4_aggregator.
2356 * there is no memory attached specifically which is not part
2357 * of the attr.
2358 * so ignoring just means do nothing.
2359 */
2360 /*
2361 * Finally do the checks on the aspath we did not do yet
2362 * because we waited for a potentially synthesized aspath.
2363 */
Paul Jakmab881c702010-11-23 16:35:42 +00002364 if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH)))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002365 {
Paul Jakma835315b2012-01-18 12:28:30 +00002366 ret = bgp_attr_aspath_check (peer, attr);
Paul Jakmab881c702010-11-23 16:35:42 +00002367 if (ret != BGP_ATTR_PARSE_PROCEED)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002368 return ret;
2369 }
2370
paul718e3742002-12-13 20:15:29 +00002371 /* Finally intern unknown attribute. */
Paul Jakmafb982c22007-05-04 20:15:47 +00002372 if (attr->extra && attr->extra->transit)
2373 attr->extra->transit = transit_intern (attr->extra->transit);
paul718e3742002-12-13 20:15:29 +00002374
Paul Jakmab881c702010-11-23 16:35:42 +00002375 return BGP_ATTR_PARSE_PROCEED;
paul718e3742002-12-13 20:15:29 +00002376}
2377
paul718e3742002-12-13 20:15:29 +00002378int stream_put_prefix (struct stream *, struct prefix *);
2379
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002380size_t
2381bgp_packet_mpattr_start (struct stream *s, afi_t afi, safi_t safi,
2382 struct attr *attr)
2383{
2384 size_t sizep;
2385
2386 /* Set extended bit always to encode the attribute length as 2 bytes */
2387 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN);
2388 stream_putc (s, BGP_ATTR_MP_REACH_NLRI);
2389 sizep = stream_get_endp (s);
2390 stream_putw (s, 0); /* Marker: Attribute length. */
Lou Berger050defe2016-01-12 13:41:59 -05002391
2392 stream_putw (s, afi);
2393 stream_putc (s, (safi == SAFI_MPLS_VPN) ? SAFI_MPLS_LABELED_VPN : safi);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002394
2395 /* Nexthop */
2396 switch (afi)
2397 {
2398 case AFI_IP:
2399 switch (safi)
2400 {
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002401 case SAFI_MULTICAST:
2402 stream_putc (s, 4);
2403 stream_put_ipv4 (s, attr->nexthop.s_addr);
2404 break;
2405 case SAFI_MPLS_VPN:
2406 stream_putc (s, 12);
Lou Berger050defe2016-01-12 13:41:59 -05002407 stream_putl (s, 0); /* RD = 0, per RFC */
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002408 stream_putl (s, 0);
2409 stream_put (s, &attr->extra->mp_nexthop_global_in, 4);
2410 break;
Lou Berger298cc2f2016-01-12 13:42:02 -05002411 case SAFI_ENCAP:
2412 stream_putc (s, 4);
2413 stream_put (s, &attr->extra->mp_nexthop_global_in, 4);
2414 break;
Lou Berger050defe2016-01-12 13:41:59 -05002415 case SAFI_UNICAST: /* invalid for IPv4 */
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002416 default:
2417 break;
2418 }
2419 break;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002420 case AFI_IP6:
2421 switch (safi)
2422 {
2423 case SAFI_UNICAST:
2424 case SAFI_MULTICAST:
2425 {
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002426 struct attr_extra *attre = attr->extra;
2427
2428 assert (attr->extra);
2429 stream_putc (s, attre->mp_nexthop_len);
2430 stream_put (s, &attre->mp_nexthop_global, 16);
2431 if (attre->mp_nexthop_len == 32)
2432 stream_put (s, &attre->mp_nexthop_local, 16);
2433 }
Lou Berger050defe2016-01-12 13:41:59 -05002434 break;
2435 case SAFI_MPLS_VPN:
2436 {
2437 struct attr_extra *attre = attr->extra;
2438
2439 assert (attr->extra);
2440 if (attre->mp_nexthop_len == 16) {
2441 stream_putc (s, 24);
2442 stream_putl (s, 0); /* RD = 0, per RFC */
2443 stream_putl (s, 0);
2444 stream_put (s, &attre->mp_nexthop_global, 16);
2445 } else if (attre->mp_nexthop_len == 32) {
2446 stream_putc (s, 48);
2447 stream_putl (s, 0); /* RD = 0, per RFC */
2448 stream_putl (s, 0);
2449 stream_put (s, &attre->mp_nexthop_global, 16);
2450 stream_putl (s, 0); /* RD = 0, per RFC */
2451 stream_putl (s, 0);
2452 stream_put (s, &attre->mp_nexthop_local, 16);
2453 }
2454 }
2455 break;
Lou Berger298cc2f2016-01-12 13:42:02 -05002456 case SAFI_ENCAP:
2457 assert (attr->extra);
2458 stream_putc (s, 16);
2459 stream_put (s, &attr->extra->mp_nexthop_global, 16);
2460 break;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002461 default:
2462 break;
2463 }
2464 break;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002465 default:
2466 break;
2467 }
2468
2469 /* SNPA */
2470 stream_putc (s, 0);
2471 return sizep;
2472}
2473
2474void
2475bgp_packet_mpattr_prefix (struct stream *s, afi_t afi, safi_t safi,
2476 struct prefix *p, struct prefix_rd *prd,
2477 u_char *tag)
2478{
Lou Berger050defe2016-01-12 13:41:59 -05002479 if (safi == SAFI_MPLS_VPN)
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002480 {
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002481 /* Tag, RD, Prefix write. */
2482 stream_putc (s, p->prefixlen + 88);
2483 stream_put (s, tag, 3);
2484 stream_put (s, prd->val, 8);
2485 stream_put (s, &p->u.prefix, PSIZE (p->prefixlen));
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002486 }
Lou Berger050defe2016-01-12 13:41:59 -05002487 else
2488 stream_put_prefix (s, p);
2489}
2490
2491size_t
2492bgp_packet_mpattr_prefix_size (afi_t afi, safi_t safi, struct prefix *p)
2493{
2494 int size = PSIZE (p->prefixlen);
2495 if (safi == SAFI_MPLS_VPN)
2496 size += 88;
2497 return size;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002498}
2499
Lou Bergerc3741782016-01-12 13:42:01 -05002500/*
2501 * Encodes the tunnel encapsulation attribute
2502 */
2503static void
2504bgp_packet_mpattr_tea(
2505 struct bgp *bgp,
2506 struct peer *peer,
2507 struct stream *s,
2508 struct attr *attr,
2509 uint8_t attrtype)
2510{
2511 unsigned int attrlenfield = 0;
Lou Berger298cc2f2016-01-12 13:42:02 -05002512 unsigned int attrhdrlen = 0;
Lou Bergerc3741782016-01-12 13:42:01 -05002513 struct bgp_attr_encap_subtlv *subtlvs;
2514 struct bgp_attr_encap_subtlv *st;
2515 const char *attrname;
2516
2517 if (!attr || !attr->extra)
2518 return;
2519
2520 switch (attrtype) {
2521 case BGP_ATTR_ENCAP:
2522 attrname = "Tunnel Encap";
2523 subtlvs = attr->extra->encap_subtlvs;
2524
2525 /*
2526 * The tunnel encap attr has an "outer" tlv.
2527 * T = tunneltype,
2528 * L = total length of subtlvs,
2529 * V = concatenated subtlvs.
2530 */
2531 attrlenfield = 2 + 2; /* T + L */
Lou Berger298cc2f2016-01-12 13:42:02 -05002532 attrhdrlen = 1 + 1; /* subTLV T + L */
Lou Bergerc3741782016-01-12 13:42:01 -05002533 break;
2534
2535 default:
2536 assert(0);
2537 }
2538
2539
Lou Berger298cc2f2016-01-12 13:42:02 -05002540 /* if no tlvs, don't make attr */
2541 if (subtlvs == NULL)
2542 return;
2543
Lou Bergerc3741782016-01-12 13:42:01 -05002544 /* compute attr length */
2545 for (st = subtlvs; st; st = st->next) {
Lou Berger298cc2f2016-01-12 13:42:02 -05002546 attrlenfield += (attrhdrlen + st->length);
Lou Bergerc3741782016-01-12 13:42:01 -05002547 }
2548
Lou Bergerc3741782016-01-12 13:42:01 -05002549 if (attrlenfield > 0xffff) {
2550 zlog (peer->log, LOG_ERR,
2551 "%s attribute is too long (length=%d), can't send it",
2552 attrname,
2553 attrlenfield);
2554 return;
2555 }
2556
2557 if (attrlenfield > 0xff) {
2558 /* 2-octet length field */
2559 stream_putc (s,
2560 BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN);
2561 stream_putc (s, attrtype);
2562 stream_putw (s, attrlenfield & 0xffff);
2563 } else {
2564 /* 1-octet length field */
2565 stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL);
2566 stream_putc (s, attrtype);
2567 stream_putc (s, attrlenfield & 0xff);
2568 }
2569
2570 if (attrtype == BGP_ATTR_ENCAP) {
2571 /* write outer T+L */
2572 stream_putw(s, attr->extra->encap_tunneltype);
2573 stream_putw(s, attrlenfield - 4);
2574 }
2575
2576 /* write each sub-tlv */
2577 for (st = subtlvs; st; st = st->next) {
Lou Berger298cc2f2016-01-12 13:42:02 -05002578 if (attrtype == BGP_ATTR_ENCAP) {
2579 stream_putc (s, st->type);
2580 stream_putc (s, st->length);
2581 }
Lou Bergerc3741782016-01-12 13:42:01 -05002582 stream_put (s, st->value, st->length);
2583 }
2584}
2585
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002586void
2587bgp_packet_mpattr_end (struct stream *s, size_t sizep)
2588{
2589 /* Set MP attribute length. Don't count the (2) bytes used to encode
2590 the attr length */
2591 stream_putw_at (s, sizep, (stream_get_endp (s) - sizep) - 2);
2592}
2593
paul718e3742002-12-13 20:15:29 +00002594/* Make attribute packet. */
2595bgp_size_t
2596bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002597 struct stream *s, struct attr *attr,
2598 struct prefix *p, afi_t afi, safi_t safi,
2599 struct peer *from, struct prefix_rd *prd, u_char *tag)
paul718e3742002-12-13 20:15:29 +00002600{
paulfe69a502005-09-10 16:55:02 +00002601 size_t cp;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002602 size_t aspath_sizep;
paul718e3742002-12-13 20:15:29 +00002603 struct aspath *aspath;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002604 int send_as4_path = 0;
2605 int send_as4_aggregator = 0;
2606 int use32bit = (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)) ? 1 : 0;
paul718e3742002-12-13 20:15:29 +00002607
2608 if (! bgp)
2609 bgp = bgp_get_default ();
2610
2611 /* Remember current pointer. */
paul9985f832005-02-09 15:51:56 +00002612 cp = stream_get_endp (s);
paul718e3742002-12-13 20:15:29 +00002613
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002614 if (p && !(afi == AFI_IP && safi == SAFI_UNICAST))
2615 {
Lou Berger050defe2016-01-12 13:41:59 -05002616 size_t mpattrlen_pos = 0;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002617 mpattrlen_pos = bgp_packet_mpattr_start(s, afi, safi, attr);
2618 bgp_packet_mpattr_prefix(s, afi, safi, p, prd, tag);
2619 bgp_packet_mpattr_end(s, mpattrlen_pos);
2620 }
2621
paul718e3742002-12-13 20:15:29 +00002622 /* Origin attribute. */
2623 stream_putc (s, BGP_ATTR_FLAG_TRANS);
2624 stream_putc (s, BGP_ATTR_ORIGIN);
2625 stream_putc (s, 1);
2626 stream_putc (s, attr->origin);
2627
2628 /* AS path attribute. */
2629
2630 /* If remote-peer is EBGP */
Jorge Boncompte [DTI2]6d85b152012-05-07 16:52:54 +00002631 if (peer->sort == BGP_PEER_EBGP
paul718e3742002-12-13 20:15:29 +00002632 && (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_AS_PATH_UNCHANGED)
paulfe69a502005-09-10 16:55:02 +00002633 || attr->aspath->segments == NULL)
paulfee0f4c2004-09-13 05:12:46 +00002634 && (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)))
paul718e3742002-12-13 20:15:29 +00002635 {
2636 aspath = aspath_dup (attr->aspath);
2637
2638 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION))
2639 {
2640 /* Strip the confed info, and then stuff our path CONFED_ID
2641 on the front */
2642 aspath = aspath_delete_confed_seq (aspath);
2643 aspath = aspath_add_seq (aspath, bgp->confed_id);
2644 }
2645 else
2646 {
Andrew Certain9d3f9702012-11-07 23:50:07 +00002647 if (peer->change_local_as) {
2648 /* If replace-as is specified, we only use the change_local_as when
2649 advertising routes. */
2650 if( ! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS) ) {
2651 aspath = aspath_add_seq (aspath, peer->local_as);
2652 }
paul718e3742002-12-13 20:15:29 +00002653 aspath = aspath_add_seq (aspath, peer->change_local_as);
Andrew Certain9d3f9702012-11-07 23:50:07 +00002654 } else {
2655 aspath = aspath_add_seq (aspath, peer->local_as);
2656 }
paul718e3742002-12-13 20:15:29 +00002657 }
2658 }
Jorge Boncompte [DTI2]6d85b152012-05-07 16:52:54 +00002659 else if (peer->sort == BGP_PEER_CONFED)
paul718e3742002-12-13 20:15:29 +00002660 {
2661 /* A confed member, so we need to do the AS_CONFED_SEQUENCE thing */
2662 aspath = aspath_dup (attr->aspath);
2663 aspath = aspath_add_confed_seq (aspath, peer->local_as);
2664 }
2665 else
2666 aspath = attr->aspath;
2667
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002668 /* If peer is not AS4 capable, then:
2669 * - send the created AS_PATH out as AS4_PATH (optional, transitive),
2670 * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path segment
2671 * types are in it (i.e. exclude them if they are there)
2672 * AND do this only if there is at least one asnum > 65535 in the path!
2673 * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and change
2674 * all ASnums > 65535 to BGP_AS_TRANS
2675 */
paul718e3742002-12-13 20:15:29 +00002676
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002677 stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
2678 stream_putc (s, BGP_ATTR_AS_PATH);
2679 aspath_sizep = stream_get_endp (s);
2680 stream_putw (s, 0);
2681 stream_putw_at (s, aspath_sizep, aspath_put (s, aspath, use32bit));
2682
2683 /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
2684 * in the path
2685 */
2686 if (!use32bit && aspath_has_as4 (aspath))
2687 send_as4_path = 1; /* we'll do this later, at the correct place */
2688
paul718e3742002-12-13 20:15:29 +00002689 /* Nexthop attribute. */
Lou Berger050defe2016-01-12 13:41:59 -05002690 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP) && afi == AFI_IP &&
2691 safi == SAFI_UNICAST) /* only write NH attr for unicast safi */
paul718e3742002-12-13 20:15:29 +00002692 {
2693 stream_putc (s, BGP_ATTR_FLAG_TRANS);
2694 stream_putc (s, BGP_ATTR_NEXT_HOP);
2695 stream_putc (s, 4);
2696 if (safi == SAFI_MPLS_VPN)
2697 {
2698 if (attr->nexthop.s_addr == 0)
2699 stream_put_ipv4 (s, peer->nexthop.v4.s_addr);
2700 else
2701 stream_put_ipv4 (s, attr->nexthop.s_addr);
2702 }
2703 else
2704 stream_put_ipv4 (s, attr->nexthop.s_addr);
2705 }
2706
2707 /* MED attribute. */
2708 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
2709 {
2710 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
2711 stream_putc (s, BGP_ATTR_MULTI_EXIT_DISC);
2712 stream_putc (s, 4);
2713 stream_putl (s, attr->med);
2714 }
2715
2716 /* Local preference. */
Jorge Boncompte [DTI2]6d85b152012-05-07 16:52:54 +00002717 if (peer->sort == BGP_PEER_IBGP ||
2718 peer->sort == BGP_PEER_CONFED)
paul718e3742002-12-13 20:15:29 +00002719 {
2720 stream_putc (s, BGP_ATTR_FLAG_TRANS);
2721 stream_putc (s, BGP_ATTR_LOCAL_PREF);
2722 stream_putc (s, 4);
2723 stream_putl (s, attr->local_pref);
2724 }
2725
2726 /* Atomic aggregate. */
2727 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE))
2728 {
2729 stream_putc (s, BGP_ATTR_FLAG_TRANS);
2730 stream_putc (s, BGP_ATTR_ATOMIC_AGGREGATE);
2731 stream_putc (s, 0);
2732 }
2733
2734 /* Aggregator. */
2735 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR))
2736 {
Paul Jakmafb982c22007-05-04 20:15:47 +00002737 assert (attr->extra);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002738
2739 /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
paul718e3742002-12-13 20:15:29 +00002740 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2741 stream_putc (s, BGP_ATTR_AGGREGATOR);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002742
2743 if (use32bit)
2744 {
2745 /* AS4 capable peer */
2746 stream_putc (s, 8);
2747 stream_putl (s, attr->extra->aggregator_as);
2748 }
2749 else
2750 {
2751 /* 2-byte AS peer */
2752 stream_putc (s, 6);
2753
2754 /* Is ASN representable in 2-bytes? Or must AS_TRANS be used? */
2755 if ( attr->extra->aggregator_as > 65535 )
2756 {
2757 stream_putw (s, BGP_AS_TRANS);
2758
2759 /* we have to send AS4_AGGREGATOR, too.
2760 * we'll do that later in order to send attributes in ascending
2761 * order.
2762 */
2763 send_as4_aggregator = 1;
2764 }
2765 else
2766 stream_putw (s, (u_int16_t) attr->extra->aggregator_as);
2767 }
Paul Jakmafb982c22007-05-04 20:15:47 +00002768 stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
paul718e3742002-12-13 20:15:29 +00002769 }
2770
2771 /* Community attribute. */
2772 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY)
2773 && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES)))
2774 {
2775 if (attr->community->size * 4 > 255)
2776 {
2777 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
2778 stream_putc (s, BGP_ATTR_COMMUNITIES);
2779 stream_putw (s, attr->community->size * 4);
2780 }
2781 else
2782 {
2783 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2784 stream_putc (s, BGP_ATTR_COMMUNITIES);
2785 stream_putc (s, attr->community->size * 4);
2786 }
2787 stream_put (s, attr->community->val, attr->community->size * 4);
2788 }
2789
2790 /* Route Reflector. */
Jorge Boncompte [DTI2]6d85b152012-05-07 16:52:54 +00002791 if (peer->sort == BGP_PEER_IBGP
paul718e3742002-12-13 20:15:29 +00002792 && from
Jorge Boncompte [DTI2]6d85b152012-05-07 16:52:54 +00002793 && from->sort == BGP_PEER_IBGP)
paul718e3742002-12-13 20:15:29 +00002794 {
2795 /* Originator ID. */
2796 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
2797 stream_putc (s, BGP_ATTR_ORIGINATOR_ID);
2798 stream_putc (s, 4);
2799
2800 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
Paul Jakmafb982c22007-05-04 20:15:47 +00002801 stream_put_in_addr (s, &attr->extra->originator_id);
Paul Jakma34c3f812006-05-12 23:25:37 +00002802 else
2803 stream_put_in_addr (s, &from->remote_id);
paul718e3742002-12-13 20:15:29 +00002804
2805 /* Cluster list. */
2806 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
2807 stream_putc (s, BGP_ATTR_CLUSTER_LIST);
2808
Paul Jakma9eda90c2007-08-30 13:36:17 +00002809 if (attr->extra && attr->extra->cluster)
paul718e3742002-12-13 20:15:29 +00002810 {
Paul Jakmafb982c22007-05-04 20:15:47 +00002811 stream_putc (s, attr->extra->cluster->length + 4);
paul718e3742002-12-13 20:15:29 +00002812 /* If this peer configuration's parent BGP has cluster_id. */
2813 if (bgp->config & BGP_CONFIG_CLUSTER_ID)
2814 stream_put_in_addr (s, &bgp->cluster_id);
2815 else
2816 stream_put_in_addr (s, &bgp->router_id);
Paul Jakmafb982c22007-05-04 20:15:47 +00002817 stream_put (s, attr->extra->cluster->list,
2818 attr->extra->cluster->length);
paul718e3742002-12-13 20:15:29 +00002819 }
2820 else
2821 {
2822 stream_putc (s, 4);
2823 /* If this peer configuration's parent BGP has cluster_id. */
2824 if (bgp->config & BGP_CONFIG_CLUSTER_ID)
2825 stream_put_in_addr (s, &bgp->cluster_id);
2826 else
2827 stream_put_in_addr (s, &bgp->router_id);
2828 }
2829 }
2830
paul718e3742002-12-13 20:15:29 +00002831 /* Extended Communities attribute. */
2832 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY)
2833 && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES)))
2834 {
Paul Jakmafb982c22007-05-04 20:15:47 +00002835 struct attr_extra *attre = attr->extra;
2836
2837 assert (attre);
2838
Jorge Boncompte [DTI2]6d85b152012-05-07 16:52:54 +00002839 if (peer->sort == BGP_PEER_IBGP
2840 || peer->sort == BGP_PEER_CONFED)
paul718e3742002-12-13 20:15:29 +00002841 {
Paul Jakmafb982c22007-05-04 20:15:47 +00002842 if (attre->ecommunity->size * 8 > 255)
hasso4372df72004-05-20 10:20:02 +00002843 {
2844 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
2845 stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
Paul Jakmafb982c22007-05-04 20:15:47 +00002846 stream_putw (s, attre->ecommunity->size * 8);
hasso4372df72004-05-20 10:20:02 +00002847 }
2848 else
2849 {
2850 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2851 stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
Paul Jakmafb982c22007-05-04 20:15:47 +00002852 stream_putc (s, attre->ecommunity->size * 8);
hasso4372df72004-05-20 10:20:02 +00002853 }
Paul Jakmafb982c22007-05-04 20:15:47 +00002854 stream_put (s, attre->ecommunity->val, attre->ecommunity->size * 8);
paul718e3742002-12-13 20:15:29 +00002855 }
2856 else
2857 {
paul5228ad22004-06-04 17:58:18 +00002858 u_int8_t *pnt;
hasso4372df72004-05-20 10:20:02 +00002859 int tbit;
2860 int ecom_tr_size = 0;
2861 int i;
2862
Paul Jakmafb982c22007-05-04 20:15:47 +00002863 for (i = 0; i < attre->ecommunity->size; i++)
hasso4372df72004-05-20 10:20:02 +00002864 {
Paul Jakmafb982c22007-05-04 20:15:47 +00002865 pnt = attre->ecommunity->val + (i * 8);
hasso4372df72004-05-20 10:20:02 +00002866 tbit = *pnt;
2867
2868 if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE))
2869 continue;
2870
2871 ecom_tr_size++;
2872 }
2873
2874 if (ecom_tr_size)
2875 {
2876 if (ecom_tr_size * 8 > 255)
2877 {
2878 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
2879 stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
2880 stream_putw (s, ecom_tr_size * 8);
2881 }
2882 else
2883 {
2884 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2885 stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
2886 stream_putc (s, ecom_tr_size * 8);
2887 }
2888
Paul Jakmafb982c22007-05-04 20:15:47 +00002889 for (i = 0; i < attre->ecommunity->size; i++)
hasso4372df72004-05-20 10:20:02 +00002890 {
Paul Jakmafb982c22007-05-04 20:15:47 +00002891 pnt = attre->ecommunity->val + (i * 8);
hasso4372df72004-05-20 10:20:02 +00002892 tbit = *pnt;
2893
2894 if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE))
2895 continue;
2896
2897 stream_put (s, pnt, 8);
2898 }
2899 }
paul718e3742002-12-13 20:15:29 +00002900 }
paul718e3742002-12-13 20:15:29 +00002901 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002902
2903 if ( send_as4_path )
2904 {
2905 /* If the peer is NOT As4 capable, AND */
2906 /* there are ASnums > 65535 in path THEN
2907 * give out AS4_PATH */
2908
2909 /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
2910 * path segments!
2911 * Hm, I wonder... confederation things *should* only be at
2912 * the beginning of an aspath, right? Then we should use
2913 * aspath_delete_confed_seq for this, because it is already
2914 * there! (JK)
2915 * Folks, talk to me: what is reasonable here!?
2916 */
2917 aspath = aspath_delete_confed_seq (aspath);
2918
2919 stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN);
2920 stream_putc (s, BGP_ATTR_AS4_PATH);
2921 aspath_sizep = stream_get_endp (s);
2922 stream_putw (s, 0);
2923 stream_putw_at (s, aspath_sizep, aspath_put (s, aspath, 1));
2924 }
2925
2926 if (aspath != attr->aspath)
2927 aspath_free (aspath);
2928
2929 if ( send_as4_aggregator )
2930 {
2931 assert (attr->extra);
2932
2933 /* send AS4_AGGREGATOR, at this place */
2934 /* this section of code moved here in order to ensure the correct
2935 * *ascending* order of attributes
2936 */
2937 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2938 stream_putc (s, BGP_ATTR_AS4_AGGREGATOR);
2939 stream_putc (s, 8);
2940 stream_putl (s, attr->extra->aggregator_as);
2941 stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
2942 }
Lou Berger298cc2f2016-01-12 13:42:02 -05002943
2944 if ((afi == AFI_IP || afi == AFI_IP6) &&
2945 (safi == SAFI_ENCAP || safi == SAFI_MPLS_VPN))
2946 {
2947 /* Tunnel Encap attribute */
2948 bgp_packet_mpattr_tea(bgp, peer, s, attr, BGP_ATTR_ENCAP);
2949 }
2950
paul718e3742002-12-13 20:15:29 +00002951 /* Unknown transit attribute. */
Paul Jakmafb982c22007-05-04 20:15:47 +00002952 if (attr->extra && attr->extra->transit)
2953 stream_put (s, attr->extra->transit->val, attr->extra->transit->length);
paul718e3742002-12-13 20:15:29 +00002954
2955 /* Return total size of attribute. */
paul9985f832005-02-09 15:51:56 +00002956 return stream_get_endp (s) - cp;
paul718e3742002-12-13 20:15:29 +00002957}
2958
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002959size_t
2960bgp_packet_mpunreach_start (struct stream *s, afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +00002961{
paul718e3742002-12-13 20:15:29 +00002962 unsigned long attrlen_pnt;
paul718e3742002-12-13 20:15:29 +00002963
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002964 /* Set extended bit always to encode the attribute length as 2 bytes */
2965 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN);
paul718e3742002-12-13 20:15:29 +00002966 stream_putc (s, BGP_ATTR_MP_UNREACH_NLRI);
2967
paul9985f832005-02-09 15:51:56 +00002968 attrlen_pnt = stream_get_endp (s);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002969 stream_putw (s, 0); /* Length of this attribute. */
paul718e3742002-12-13 20:15:29 +00002970
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002971 stream_putw (s, afi);
Lou Berger050defe2016-01-12 13:41:59 -05002972 stream_putc (s, (safi == SAFI_MPLS_VPN) ? SAFI_MPLS_LABELED_VPN : safi);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002973 return attrlen_pnt;
2974}
paul718e3742002-12-13 20:15:29 +00002975
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002976void
2977bgp_packet_mpunreach_prefix (struct stream *s, struct prefix *p,
2978 afi_t afi, safi_t safi, struct prefix_rd *prd,
2979 u_char *tag)
2980{
Lou Berger050defe2016-01-12 13:41:59 -05002981 bgp_packet_mpattr_prefix (s, afi, safi, p, prd, tag);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002982}
paul718e3742002-12-13 20:15:29 +00002983
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +00002984void
2985bgp_packet_mpunreach_end (struct stream *s, size_t attrlen_pnt)
2986{
Lou Berger050defe2016-01-12 13:41:59 -05002987 bgp_packet_mpattr_end (s, attrlen_pnt);
paul718e3742002-12-13 20:15:29 +00002988}
2989
2990/* Initialization of attribute. */
2991void
paulfe69a502005-09-10 16:55:02 +00002992bgp_attr_init (void)
paul718e3742002-12-13 20:15:29 +00002993{
paul718e3742002-12-13 20:15:29 +00002994 aspath_init ();
2995 attrhash_init ();
2996 community_init ();
2997 ecommunity_init ();
2998 cluster_init ();
2999 transit_init ();
3000}
3001
Chris Caputo228da422009-07-18 05:44:03 +00003002void
3003bgp_attr_finish (void)
3004{
3005 aspath_finish ();
3006 attrhash_finish ();
3007 community_finish ();
3008 ecommunity_finish ();
3009 cluster_finish ();
3010 transit_finish ();
3011}
3012
paul718e3742002-12-13 20:15:29 +00003013/* Make attribute packet. */
3014void
paula3845922003-10-18 01:30:50 +00003015bgp_dump_routes_attr (struct stream *s, struct attr *attr,
3016 struct prefix *prefix)
paul718e3742002-12-13 20:15:29 +00003017{
3018 unsigned long cp;
3019 unsigned long len;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00003020 size_t aspath_lenp;
paul718e3742002-12-13 20:15:29 +00003021 struct aspath *aspath;
3022
3023 /* Remember current pointer. */
paul9985f832005-02-09 15:51:56 +00003024 cp = stream_get_endp (s);
paul718e3742002-12-13 20:15:29 +00003025
3026 /* Place holder of length. */
3027 stream_putw (s, 0);
3028
3029 /* Origin attribute. */
3030 stream_putc (s, BGP_ATTR_FLAG_TRANS);
3031 stream_putc (s, BGP_ATTR_ORIGIN);
3032 stream_putc (s, 1);
3033 stream_putc (s, attr->origin);
3034
3035 aspath = attr->aspath;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00003036
3037 stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
3038 stream_putc (s, BGP_ATTR_AS_PATH);
3039 aspath_lenp = stream_get_endp (s);
3040 stream_putw (s, 0);
3041
3042 stream_putw_at (s, aspath_lenp, aspath_put (s, aspath, 1));
paul718e3742002-12-13 20:15:29 +00003043
3044 /* Nexthop attribute. */
paula3845922003-10-18 01:30:50 +00003045 /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
3046 if(prefix != NULL
paula3845922003-10-18 01:30:50 +00003047 && prefix->family != AF_INET6
paula3845922003-10-18 01:30:50 +00003048 )
3049 {
3050 stream_putc (s, BGP_ATTR_FLAG_TRANS);
3051 stream_putc (s, BGP_ATTR_NEXT_HOP);
3052 stream_putc (s, 4);
3053 stream_put_ipv4 (s, attr->nexthop.s_addr);
3054 }
paul718e3742002-12-13 20:15:29 +00003055
3056 /* MED attribute. */
3057 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
3058 {
3059 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
3060 stream_putc (s, BGP_ATTR_MULTI_EXIT_DISC);
3061 stream_putc (s, 4);
3062 stream_putl (s, attr->med);
3063 }
3064
3065 /* Local preference. */
3066 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))
3067 {
3068 stream_putc (s, BGP_ATTR_FLAG_TRANS);
3069 stream_putc (s, BGP_ATTR_LOCAL_PREF);
3070 stream_putc (s, 4);
3071 stream_putl (s, attr->local_pref);
3072 }
3073
3074 /* Atomic aggregate. */
3075 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE))
3076 {
3077 stream_putc (s, BGP_ATTR_FLAG_TRANS);
3078 stream_putc (s, BGP_ATTR_ATOMIC_AGGREGATE);
3079 stream_putc (s, 0);
3080 }
3081
3082 /* Aggregator. */
3083 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR))
3084 {
Paul Jakmafb982c22007-05-04 20:15:47 +00003085 assert (attr->extra);
paul718e3742002-12-13 20:15:29 +00003086 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
3087 stream_putc (s, BGP_ATTR_AGGREGATOR);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00003088 stream_putc (s, 8);
3089 stream_putl (s, attr->extra->aggregator_as);
Paul Jakmafb982c22007-05-04 20:15:47 +00003090 stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
paul718e3742002-12-13 20:15:29 +00003091 }
3092
3093 /* Community attribute. */
3094 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES))
3095 {
3096 if (attr->community->size * 4 > 255)
3097 {
3098 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
3099 stream_putc (s, BGP_ATTR_COMMUNITIES);
3100 stream_putw (s, attr->community->size * 4);
3101 }
3102 else
3103 {
3104 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
3105 stream_putc (s, BGP_ATTR_COMMUNITIES);
3106 stream_putc (s, attr->community->size * 4);
3107 }
3108 stream_put (s, attr->community->val, attr->community->size * 4);
3109 }
3110
paula3845922003-10-18 01:30:50 +00003111 /* Add a MP_NLRI attribute to dump the IPv6 next hop */
Paul Jakmafb982c22007-05-04 20:15:47 +00003112 if (prefix != NULL && prefix->family == AF_INET6 && attr->extra &&
3113 (attr->extra->mp_nexthop_len == 16 || attr->extra->mp_nexthop_len == 32) )
paula3845922003-10-18 01:30:50 +00003114 {
3115 int sizep;
Paul Jakmafb982c22007-05-04 20:15:47 +00003116 struct attr_extra *attre = attr->extra;
3117
paula3845922003-10-18 01:30:50 +00003118 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
3119 stream_putc(s, BGP_ATTR_MP_REACH_NLRI);
paul9985f832005-02-09 15:51:56 +00003120 sizep = stream_get_endp (s);
paula3845922003-10-18 01:30:50 +00003121
3122 /* MP header */
Paul Jakmafb982c22007-05-04 20:15:47 +00003123 stream_putc (s, 0); /* Marker: Attribute length. */
paula3845922003-10-18 01:30:50 +00003124 stream_putw(s, AFI_IP6); /* AFI */
3125 stream_putc(s, SAFI_UNICAST); /* SAFI */
3126
3127 /* Next hop */
Paul Jakmafb982c22007-05-04 20:15:47 +00003128 stream_putc(s, attre->mp_nexthop_len);
3129 stream_put(s, &attre->mp_nexthop_global, 16);
3130 if (attre->mp_nexthop_len == 32)
3131 stream_put(s, &attre->mp_nexthop_local, 16);
paula3845922003-10-18 01:30:50 +00003132
3133 /* SNPA */
3134 stream_putc(s, 0);
3135
3136 /* Prefix */
3137 stream_put_prefix(s, prefix);
3138
3139 /* Set MP attribute length. */
paul9985f832005-02-09 15:51:56 +00003140 stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1);
paula3845922003-10-18 01:30:50 +00003141 }
paula3845922003-10-18 01:30:50 +00003142
paul718e3742002-12-13 20:15:29 +00003143 /* Return total size of attribute. */
paul9985f832005-02-09 15:51:56 +00003144 len = stream_get_endp (s) - cp - 2;
paul718e3742002-12-13 20:15:29 +00003145 stream_putw_at (s, cp, len);
3146}