| 
									
										
										
										
											2000-03-20 21:50:17 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *	BIRD -- BGP Attributes | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *	(c) 2000 Martin Mares <mj@ucw.cz> | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |  *	(c) 2008--2016 Ondrej Zajicek <santiago@crfreenet.org> | 
					
						
							|  |  |  |  *	(c) 2008--2016 CZ.NIC z.s.p.o. | 
					
						
							| 
									
										
										
										
											2000-03-20 21:50:17 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  *	Can be freely distributed and used under the terms of the GNU GPL. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-05-02 16:07:41 +00:00
										 |  |  | #undef LOCAL_DEBUG
 | 
					
						
							| 
									
										
										
										
											2000-03-31 23:21:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-17 11:25:15 +00:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-20 21:50:17 +00:00
										 |  |  | #include "nest/bird.h"
 | 
					
						
							|  |  |  | #include "nest/iface.h"
 | 
					
						
							|  |  |  | #include "nest/protocol.h"
 | 
					
						
							| 
									
										
										
										
											2022-03-31 19:09:38 +02:00
										 |  |  | #include "nest/rt.h"
 | 
					
						
							| 
									
										
										
										
											2022-03-31 19:22:07 +02:00
										 |  |  | #include "lib/attrs.h"
 | 
					
						
							| 
									
										
										
										
											2000-03-20 21:50:17 +00:00
										 |  |  | #include "conf/conf.h"
 | 
					
						
							| 
									
										
										
										
											2000-03-31 23:21:37 +00:00
										 |  |  | #include "lib/resource.h"
 | 
					
						
							|  |  |  | #include "lib/string.h"
 | 
					
						
							|  |  |  | #include "lib/unaligned.h"
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | #include "lib/macro.h"
 | 
					
						
							| 
									
										
										
										
											2000-03-20 21:50:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "bgp.h"
 | 
					
						
							| 
									
										
										
										
											2000-03-31 23:21:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-30 01:09:18 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *   UPDATE message error handling | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * All checks from RFC 4271 6.3 are done as specified with these exceptions: | 
					
						
							|  |  |  |  *  - The semantic check of an IP address from NEXT_HOP attribute is missing. | 
					
						
							|  |  |  |  *  - Checks of some optional attribute values are missing. | 
					
						
							|  |  |  |  *  - Syntactic and semantic checks of NLRIs (done in DECODE_PREFIX()) | 
					
						
							|  |  |  |  *    are probably inadequate. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Loop detection based on AS_PATH causes updates to be withdrawn. RFC | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  |  * 4271 does not explicitly specify the behavior in that case. | 
					
						
							| 
									
										
										
										
											2011-03-30 01:09:18 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Loop detection related to route reflection (based on ORIGINATOR_ID | 
					
						
							|  |  |  |  * and CLUSTER_LIST) causes updates to be withdrawn. RFC 4456 8 | 
					
						
							|  |  |  |  * specifies that such updates should be ignored, but that is generally | 
					
						
							|  |  |  |  * a bad idea. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |  * BGP attribute table has several hooks: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * export - Hook that validates and normalizes attribute during export phase. | 
					
						
							|  |  |  |  * Receives eattr, may modify it (e.g., sort community lists for canonical | 
					
						
							| 
									
										
										
										
											2022-01-28 05:35:22 +01:00
										 |  |  |  * representation), UNSET() it (e.g., skip empty lists), or REJECT() the route | 
					
						
							|  |  |  |  * if necessary. May assume that eattr has value valid w.r.t. its type, but may | 
					
						
							|  |  |  |  * be invalid w.r.t. BGP constraints. Optional. | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * encode - Hook that converts internal representation to external one during | 
					
						
							|  |  |  |  * packet writing. Receives eattr and puts it in the buffer (including attribute | 
					
						
							|  |  |  |  * header). Returns number of bytes, or -1 if not enough space. May assume that | 
					
						
							|  |  |  |  * eattr has value valid w.r.t. its type and validated by export hook. Mandatory | 
					
						
							|  |  |  |  * for all known attributes that exist internally after export phase (i.e., all | 
					
						
							|  |  |  |  * except pseudoattributes MP_(UN)REACH_NLRI). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * decode - Hook that converts external representation to internal one during | 
					
						
							|  |  |  |  * packet parsing. Receives attribute data in buffer, validates it and adds | 
					
						
							|  |  |  |  * attribute to ea_list. If data are invalid, steps DISCARD(), WITHDRAW() or | 
					
						
							|  |  |  |  * bgp_parse_error() may be used to escape. Mandatory for all known attributes. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * format - Optional hook that converts eattr to textual representation. | 
					
						
							| 
									
										
										
										
											2011-03-30 01:09:18 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | union bgp_attr_desc { | 
					
						
							|  |  |  |   struct ea_class class; | 
					
						
							|  |  |  |   struct { | 
					
						
							|  |  |  |     EA_CLASS_INSIDE; | 
					
						
							|  |  |  |     uint flags; | 
					
						
							|  |  |  |     void (*export)(struct bgp_export_state *s, eattr *a); | 
					
						
							|  |  |  |     int  (*encode)(struct bgp_write_state *s, eattr *a, byte *buf, uint size); | 
					
						
							|  |  |  |     void (*decode)(struct bgp_parse_state *s, uint code, uint flags, byte *data, uint len, ea_list **to); | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2000-04-10 12:39:51 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | static union bgp_attr_desc bgp_attr_table[]; | 
					
						
							|  |  |  | static inline const union bgp_attr_desc *bgp_find_attr_desc(eattr *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   const struct ea_class *class = ea_class_find(a->id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ((class < &bgp_attr_table[0].class) || (class >= &bgp_attr_table[BGP_ATTR_MAX].class)) | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (const union bgp_attr_desc *) class; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | #define BGP_EA_ID(code)	(bgp_attr_table[code].id)
 | 
					
						
							|  |  |  | #define EA_BGP_ID(code)	(((union bgp_attr_desc *) ea_class_find(code)) - bgp_attr_table)
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  | void bgp_set_attr_u32(ea_list **to, uint code, uint flags, u32 val) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   const union bgp_attr_desc *desc = &bgp_attr_table[code]; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   ea_set_attr(to, EA_LITERAL_EMBEDDED( | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | 	&desc->class, | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  | 	flags & ~BAF_EXT_LEN, | 
					
						
							|  |  |  | 	val | 
					
						
							|  |  |  | 	)); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  | void bgp_set_attr_ptr(ea_list **to, uint code, uint flags, const struct adata *ad) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   const union bgp_attr_desc *desc = &bgp_attr_table[code]; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 19:31:50 +02:00
										 |  |  |   ea_set_attr(to, EA_LITERAL_DIRECT_ADATA( | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | 	&desc->class, | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  | 	flags & ~BAF_EXT_LEN, | 
					
						
							| 
									
										
										
										
											2022-04-10 19:31:50 +02:00
										 |  |  | 	ad | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  | 	)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | bgp_set_attr_data(ea_list **to, uint code, uint flags, void *data, uint len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   const union bgp_attr_desc *desc = &bgp_attr_table[code]; | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 19:31:50 +02:00
										 |  |  |   ea_set_attr(to, EA_LITERAL_STORE_ADATA( | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | 	&desc->class, | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  | 	flags & ~BAF_EXT_LEN, | 
					
						
							|  |  |  | 	data, | 
					
						
							|  |  |  | 	len | 
					
						
							|  |  |  | 	)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | void | 
					
						
							|  |  |  | bgp_unset_attr(ea_list **to, uint code) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   const union bgp_attr_desc *desc = &bgp_attr_table[code]; | 
					
						
							|  |  |  |   ea_unset_attr(to, 0, &desc->class); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | #define REPORT(msg, args...) \
 | 
					
						
							|  |  |  |   ({ log(L_REMOTE "%s: " msg, s->proto->p.name, ## args); }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define DISCARD(msg, args...) \
 | 
					
						
							|  |  |  |   ({ REPORT(msg, ## args); return; }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define WITHDRAW(msg, args...) \
 | 
					
						
							|  |  |  |   ({ REPORT(msg, ## args); s->err_withdraw = 1; return; }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define UNSET(a) \
 | 
					
						
							| 
									
										
										
										
											2022-03-14 11:13:48 +01:00
										 |  |  |   ({ a->undef = 1; return; }) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-28 05:35:22 +01:00
										 |  |  | #define REJECT(msg, args...) \
 | 
					
						
							|  |  |  |   ({ log(L_ERR "%s: " msg, s->proto->p.name, ## args); s->err_reject = 1; return; }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | #define NEW_BGP		"Discarding %s attribute received from AS4-aware neighbor"
 | 
					
						
							|  |  |  | #define BAD_EBGP	"Discarding %s attribute received from EBGP neighbor"
 | 
					
						
							|  |  |  | #define BAD_LENGTH	"Malformed %s attribute - invalid length (%u)"
 | 
					
						
							|  |  |  | #define BAD_VALUE	"Malformed %s attribute - invalid value (%u)"
 | 
					
						
							|  |  |  | #define NO_MANDATORY	"Missing mandatory %s attribute"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | bgp_put_attr_hdr3(byte *buf, uint code, uint flags, uint len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-30 16:59:11 +02:00
										 |  |  |   *buf++ = flags & ~BAF_EXT_LEN; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   *buf++ = code; | 
					
						
							|  |  |  |   *buf++ = len; | 
					
						
							|  |  |  |   return 3; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | bgp_put_attr_hdr4(byte *buf, uint code, uint flags, uint len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   *buf++ = flags | BAF_EXT_LEN; | 
					
						
							|  |  |  |   *buf++ = code; | 
					
						
							|  |  |  |   put_u16(buf, len); | 
					
						
							|  |  |  |   return 4; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | bgp_put_attr_hdr(byte *buf, uint code, uint flags, uint len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (len < 256) | 
					
						
							|  |  |  |     return bgp_put_attr_hdr3(buf, code, flags, len); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     return bgp_put_attr_hdr4(buf, code, flags, len); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-03-30 01:09:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_encode_u8(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size) | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (size < (3+1)) | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   bgp_put_attr_hdr3(buf, EA_BGP_ID(a->id), a->flags, 1); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   buf[3] = a->u.data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 3+1; | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static int | 
					
						
							|  |  |  | bgp_encode_u32(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size) | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (size < (3+4)) | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   bgp_put_attr_hdr3(buf, EA_BGP_ID(a->id), a->flags, 4); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   put_u32(buf+3, a->u.data); | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   return 3+4; | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_encode_u32s(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size) | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   uint len = a->u.ptr->length; | 
					
						
							| 
									
										
										
										
											2009-06-23 10:50:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (size < (4+len)) | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2009-06-23 10:50:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   uint hdr = bgp_put_attr_hdr(buf, EA_BGP_ID(a->id), a->flags, len); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   put_u32s(buf + hdr, (u32 *) a->u.ptr->data, len / 4); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return hdr + len; | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-12-27 14:26:11 +01:00
										 |  |  | bgp_put_attr(byte *buf, uint size, uint code, uint flags, const byte *data, uint len) | 
					
						
							| 
									
										
										
										
											2009-06-23 10:50:57 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (size < (4+len)) | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2009-06-23 10:50:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   uint hdr = bgp_put_attr_hdr(buf, code, flags, len); | 
					
						
							|  |  |  |   memcpy(buf + hdr, data, len); | 
					
						
							| 
									
										
										
										
											2009-06-23 10:50:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   return hdr + len; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | bgp_encode_raw(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   return bgp_put_attr(buf, size, EA_BGP_ID(a->id), a->flags, a->u.ptr->data, a->u.ptr->length); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *	AIGP handling | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | bgp_aigp_valid(byte *data, uint len, char *err, uint elen) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   byte *pos = data; | 
					
						
							|  |  |  |   char *err_dsc = NULL; | 
					
						
							|  |  |  |   uint err_val = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define BAD(DSC,VAL) ({ err_dsc = DSC; err_val = VAL; goto bad; })
 | 
					
						
							|  |  |  |   while (len) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if (len < 3) | 
					
						
							|  |  |  |       BAD("TLV framing error", len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Process one TLV */ | 
					
						
							|  |  |  |     uint ptype = pos[0]; | 
					
						
							|  |  |  |     uint plen = get_u16(pos + 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (len < plen) | 
					
						
							|  |  |  |       BAD("TLV framing error", plen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (plen < 3) | 
					
						
							|  |  |  |       BAD("Bad TLV length", plen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ((ptype == BGP_AIGP_METRIC) && (plen != 11)) | 
					
						
							|  |  |  |       BAD("Bad AIGP TLV length", plen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ADVANCE(pos, len, plen); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | #undef BAD
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bad: | 
					
						
							|  |  |  |   if (err) | 
					
						
							|  |  |  |     if (bsnprintf(err, elen, "%s (%u) at %d", err_dsc, err_val, (int) (pos - data)) < 0) | 
					
						
							|  |  |  |       err[0] = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const byte * | 
					
						
							|  |  |  | bgp_aigp_get_tlv(const struct adata *ad, uint type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!ad) | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   uint len = ad->length; | 
					
						
							|  |  |  |   const byte *pos = ad->data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   while (len) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     uint ptype = pos[0]; | 
					
						
							|  |  |  |     uint plen = get_u16(pos + 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ptype == type) | 
					
						
							|  |  |  |       return pos; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ADVANCE(pos, len, plen); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct adata * | 
					
						
							|  |  |  | bgp_aigp_set_tlv(struct linpool *pool, const struct adata *ad, uint type, byte *data, uint dlen) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   uint len = ad ? ad->length : 0; | 
					
						
							|  |  |  |   const byte *pos = ad ? ad->data : NULL; | 
					
						
							|  |  |  |   struct adata *res = lp_alloc_adata(pool, len + 3 + dlen); | 
					
						
							|  |  |  |   byte *dst = res->data; | 
					
						
							|  |  |  |   byte *tlv = NULL; | 
					
						
							|  |  |  |   int del = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   while (len) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     uint ptype = pos[0]; | 
					
						
							|  |  |  |     uint plen = get_u16(pos + 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Find position for new TLV */ | 
					
						
							|  |  |  |     if ((ptype >= type) && !tlv) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       tlv = dst; | 
					
						
							|  |  |  |       dst += 3 + dlen; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Skip first matching TLV, copy others */ | 
					
						
							|  |  |  |     if ((ptype == type) && !del) | 
					
						
							|  |  |  |       del = 1; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       memcpy(dst, pos, plen); | 
					
						
							|  |  |  |       dst += plen; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ADVANCE(pos, len, plen); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!tlv) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     tlv = dst; | 
					
						
							|  |  |  |     dst += 3 + dlen; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Store the TLD */ | 
					
						
							|  |  |  |   put_u8(tlv + 0, type); | 
					
						
							|  |  |  |   put_u16(tlv + 1, 3 + dlen); | 
					
						
							|  |  |  |   memcpy(tlv + 3, data, dlen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Update length */ | 
					
						
							|  |  |  |   res->length = dst - res->data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static u64 UNUSED | 
					
						
							|  |  |  | bgp_aigp_get_metric(const struct adata *ad, u64 def) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   const byte *b = bgp_aigp_get_tlv(ad, BGP_AIGP_METRIC); | 
					
						
							|  |  |  |   return b ? get_u64(b + 3) : def; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct adata * | 
					
						
							|  |  |  | bgp_aigp_set_metric(struct linpool *pool, const struct adata *ad, u64 metric) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   byte data[8]; | 
					
						
							|  |  |  |   put_u64(data, metric); | 
					
						
							|  |  |  |   return bgp_aigp_set_tlv(pool, ad, BGP_AIGP_METRIC, data, 8); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | bgp_total_aigp_metric_(rte *e, u64 *metric, const struct adata **ad) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   eattr *a = ea_find(e->attrs->eattrs, BGP_EA_ID(BA_AIGP)); | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  |   if (!a) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const byte *b = bgp_aigp_get_tlv(a->u.ptr, BGP_AIGP_METRIC); | 
					
						
							|  |  |  |   if (!b) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   u64 aigp = get_u64(b + 3); | 
					
						
							| 
									
										
										
										
											2022-04-20 10:25:14 +02:00
										 |  |  |   u64 step = rt_get_igp_metric(e); | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (!rte_resolvable(e) || (step >= IGP_METRIC_UNKNOWN)) | 
					
						
							|  |  |  |     step = BGP_AIGP_MAX; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!step) | 
					
						
							|  |  |  |     step = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   *ad = a->u.ptr; | 
					
						
							|  |  |  |   *metric = aigp + step; | 
					
						
							|  |  |  |   if (*metric < aigp) | 
					
						
							|  |  |  |     *metric = BGP_AIGP_MAX; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | bgp_init_aigp_metric(rte *e, u64 *metric, const struct adata **ad) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (e->attrs->source == RTS_BGP) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   *metric = rt_get_igp_metric(e); | 
					
						
							|  |  |  |   *ad = NULL; | 
					
						
							|  |  |  |   return *metric < IGP_METRIC_UNKNOWN; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-20 23:18:34 +01:00
										 |  |  | u32 | 
					
						
							|  |  |  | bgp_rte_igp_metric(struct rte *rt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   u64 metric = bgp_total_aigp_metric(rt); | 
					
						
							|  |  |  |   return (u32) MIN(metric, (u64) IGP_METRIC_UNKNOWN); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *	Attribute hooks | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-06-23 10:50:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | bgp_export_origin(struct bgp_export_state *s, eattr *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (a->u.data > 2) | 
					
						
							| 
									
										
										
										
											2022-01-28 05:35:22 +01:00
										 |  |  |     REJECT(BAD_VALUE, "ORIGIN", a->u.data); | 
					
						
							| 
									
										
										
										
											2009-06-23 10:50:57 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | bgp_decode_origin(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (len != 1) | 
					
						
							|  |  |  |     WITHDRAW(BAD_LENGTH, "ORIGIN", len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (data[0] > 2) | 
					
						
							|  |  |  |     WITHDRAW(BAD_VALUE, "ORIGIN", data[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_u32(to, BA_ORIGIN, flags, data[0]); | 
					
						
							| 
									
										
										
										
											2009-06-23 10:50:57 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2019-08-17 13:36:36 +02:00
										 |  |  | bgp_format_origin(const eattr *a, byte *buf, uint size UNUSED) | 
					
						
							| 
									
										
										
										
											2009-06-23 10:50:57 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   static const char *bgp_origin_names[] = { "IGP", "EGP", "Incomplete" }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bsprintf(buf, (a->u.data <= 2) ? bgp_origin_names[a->u.data] : "?"); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-10 02:06:07 +01:00
										 |  |  | static inline int | 
					
						
							|  |  |  | bgp_as_path_first_as_equal(const byte *data, uint len, u32 asn) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return (len >= 6) && | 
					
						
							|  |  |  |     ((data[0] == AS_PATH_SEQUENCE) || (data[0] == AS_PATH_CONFED_SEQUENCE)) && | 
					
						
							|  |  |  |     (data[1] > 0) && | 
					
						
							|  |  |  |     (get_u32(data+2) == asn); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_encode_as_path(struct bgp_write_state *s, eattr *a, byte *buf, uint size) | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-27 14:26:11 +01:00
										 |  |  |   const byte *data = a->u.ptr->data; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   uint len = a->u.ptr->length; | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (!s->as4_session) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     /* Prepare 16-bit AS_PATH (from 32-bit one) in a temporary buffer */ | 
					
						
							| 
									
										
										
										
											2018-12-27 14:26:11 +01:00
										 |  |  |     byte *dst = alloca(len); | 
					
						
							|  |  |  |     len = as_path_32to16(dst, data, len); | 
					
						
							|  |  |  |     data = dst; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return bgp_put_attr(buf, size, BA_AS_PATH, a->flags, data, len); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_as_path(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  |   struct bgp_proto *p = s->proto; | 
					
						
							|  |  |  |   int as_length = s->as4_session ? 4 : 2; | 
					
						
							| 
									
										
										
										
											2019-11-04 22:07:03 +01:00
										 |  |  |   int as_sets = p->cf->allow_as_sets; | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  |   int as_confed = p->cf->confederation && p->is_interior; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   char err[128]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-04 22:07:03 +01:00
										 |  |  |   if (!as_path_valid(data, len, as_length, as_sets, as_confed, err, sizeof(err))) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     WITHDRAW("Malformed AS_PATH attribute - %s", err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!s->as4_session) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     /* Prepare 32-bit AS_PATH (from 16-bit one) in a temporary buffer */ | 
					
						
							|  |  |  |     byte *src = data; | 
					
						
							|  |  |  |     data = alloca(2*len); | 
					
						
							|  |  |  |     len = as_path_16to32(data, src, len); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-10 02:06:07 +01:00
										 |  |  |   /* In some circumstances check for initial AS_CONFED_SEQUENCE; RFC 5065 5.0 */ | 
					
						
							|  |  |  |   if (p->is_interior && !p->is_internal && | 
					
						
							|  |  |  |       ((len < 2) || (data[0] != AS_PATH_CONFED_SEQUENCE))) | 
					
						
							|  |  |  |     WITHDRAW("Malformed AS_PATH attribute - %s", "missing initial AS_CONFED_SEQUENCE"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Reject routes with first AS in AS_PATH not matching neighbor AS; RFC 4271 6.3 */ | 
					
						
							|  |  |  |   if (!p->is_internal && p->cf->enforce_first_as && | 
					
						
							|  |  |  |       !bgp_as_path_first_as_equal(data, len, p->remote_as)) | 
					
						
							|  |  |  |     WITHDRAW("Malformed AS_PATH attribute - %s", "First AS differs from neigbor AS"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_data(to, BA_AS_PATH, flags, data, len); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | bgp_encode_next_hop(struct bgp_write_state *s, eattr *a, byte *buf, uint size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /*
 | 
					
						
							|  |  |  |    * The NEXT_HOP attribute is used only in traditional (IPv4) BGP. In MP-BGP, | 
					
						
							|  |  |  |    * the next hop is encoded as a part of the MP_REACH_NLRI attribute, so we | 
					
						
							|  |  |  |    * store it and encode it later by AFI-specific hooks. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 17:38:19 +01:00
										 |  |  |   if (!s->mp_reach) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2018-11-20 17:38:19 +01:00
										 |  |  |     // ASSERT(a->u.ptr->length == sizeof(ip_addr));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* FIXME: skip IPv6 next hops for IPv4 routes during MRT dump */ | 
					
						
							|  |  |  |     ip_addr *addr = (void *) a->u.ptr->data; | 
					
						
							|  |  |  |     if ((a->u.ptr->length != sizeof(ip_addr)) || !ipa_is_ip4(*addr)) | 
					
						
							|  |  |  |       return 0; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (size < (3+4)) | 
					
						
							|  |  |  |       return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bgp_put_attr_hdr3(buf, BA_NEXT_HOP, a->flags, 4); | 
					
						
							| 
									
										
										
										
											2018-11-20 17:38:19 +01:00
										 |  |  |     put_ip4(buf+3, ipa_to_ip4(*addr)); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 3+4; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     s->mp_next_hop = a; | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_next_hop(struct bgp_parse_state *s, uint code UNUSED, uint flags UNUSED, byte *data, uint len, ea_list **to UNUSED) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (len != 4) | 
					
						
							|  |  |  |     WITHDRAW(BAD_LENGTH, "NEXT_HOP", len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Semantic checks are done later */ | 
					
						
							|  |  |  |   s->ip_next_hop_len = len; | 
					
						
							|  |  |  |   s->ip_next_hop_data = data; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* TODO: This function should use AF-specific hook */ | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2019-08-17 13:36:36 +02:00
										 |  |  | bgp_format_next_hop(const eattr *a, byte *buf, uint size UNUSED) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | { | 
					
						
							|  |  |  |   ip_addr *nh = (void *) a->u.ptr->data; | 
					
						
							|  |  |  |   uint len = a->u.ptr->length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ASSERT((len == 16) || (len == 32)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* in IPv6, we may have two addresses in NEXT HOP */ | 
					
						
							|  |  |  |   if ((len == 16) || ipa_zero(nh[1])) | 
					
						
							|  |  |  |     bsprintf(buf, "%I", nh[0]); | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     bsprintf(buf, "%I %I", nh[0], nh[1]); | 
					
						
							| 
									
										
										
										
											2000-05-04 09:03:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-17 14:17:23 +01:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_decode_med(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							| 
									
										
										
										
											2009-11-17 14:17:23 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (len != 4) | 
					
						
							|  |  |  |     WITHDRAW(BAD_LENGTH, "MULTI_EXIT_DISC", len); | 
					
						
							| 
									
										
										
										
											2009-11-17 14:17:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   u32 val = get_u32(data); | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_u32(to, BA_MULTI_EXIT_DISC, flags, val); | 
					
						
							| 
									
										
										
										
											2009-11-17 14:17:23 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_export_local_pref(struct bgp_export_state *s, eattr *a) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-04-28 11:19:12 +02:00
										 |  |  |   if (!s->proto->is_interior && !s->proto->cf->allow_local_pref) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     UNSET(a); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-21 17:47:56 +01:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_decode_local_pref(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							| 
									
										
										
										
											2009-02-21 17:47:56 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-04-28 11:19:12 +02:00
										 |  |  |   if (!s->proto->is_interior && !s->proto->cf->allow_local_pref) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     DISCARD(BAD_EBGP, "LOCAL_PREF"); | 
					
						
							| 
									
										
										
										
											2009-02-21 17:47:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (len != 4) | 
					
						
							|  |  |  |     WITHDRAW(BAD_LENGTH, "LOCAL_PREF", len); | 
					
						
							| 
									
										
										
										
											2009-02-21 17:47:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   u32 val = get_u32(data); | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_u32(to, BA_LOCAL_PREF, flags, val); | 
					
						
							| 
									
										
										
										
											2009-02-21 17:47:56 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_atomic_aggr(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data UNUSED, uint len, ea_list **to) | 
					
						
							| 
									
										
										
										
											2011-03-30 01:09:18 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (len != 0) | 
					
						
							|  |  |  |     DISCARD(BAD_LENGTH, "ATOMIC_AGGR", len); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_data(to, BA_ATOMIC_AGGR, flags, NULL, 0); | 
					
						
							| 
									
										
										
										
											2011-03-30 01:09:18 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_encode_aggregator(struct bgp_write_state *s, eattr *a, byte *buf, uint size) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-27 14:26:11 +01:00
										 |  |  |   const byte *data = a->u.ptr->data; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   uint len = a->u.ptr->length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!s->as4_session) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     /* Prepare 16-bit AGGREGATOR (from 32-bit one) in a temporary buffer */ | 
					
						
							| 
									
										
										
										
											2018-12-27 14:26:11 +01:00
										 |  |  |     byte *dst = alloca(6); | 
					
						
							|  |  |  |     len = aggregator_32to16(dst, data); | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:53 +02:00
										 |  |  |     data = dst; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return bgp_put_attr(buf, size, BA_AGGREGATOR, a->flags, data, len); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_aggregator(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (len != (s->as4_session ? 8 : 6)) | 
					
						
							|  |  |  |     DISCARD(BAD_LENGTH, "AGGREGATOR", len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!s->as4_session) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     /* Prepare 32-bit AGGREGATOR (from 16-bit one) in a temporary buffer */ | 
					
						
							|  |  |  |     byte *src = data; | 
					
						
							|  |  |  |     data = alloca(8); | 
					
						
							|  |  |  |     len = aggregator_16to32(data, src); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_data(to, BA_AGGREGATOR, flags, data, len); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2019-08-17 13:36:36 +02:00
										 |  |  | bgp_format_aggregator(const eattr *a, byte *buf, uint size UNUSED) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-27 14:26:11 +01:00
										 |  |  |   const byte *data = a->u.ptr->data; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   bsprintf(buf, "%I4 AS%u", get_ip4(data+4), get_u32(data+0)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_export_community(struct bgp_export_state *s, eattr *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (a->u.ptr->length == 0) | 
					
						
							|  |  |  |     UNSET(a); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   a->u.ptr = int_set_sort(s->pool, a->u.ptr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_community(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!len || (len % 4)) | 
					
						
							|  |  |  |     WITHDRAW(BAD_LENGTH, "COMMUNITY", len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   struct adata *ad = lp_alloc_adata(s->pool, len); | 
					
						
							|  |  |  |   get_u32s(data, (u32 *) ad->data, len / 4); | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_ptr(to, BA_COMMUNITY, flags, ad); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_export_originator_id(struct bgp_export_state *s, eattr *a) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (!s->proto->is_internal) | 
					
						
							|  |  |  |     UNSET(a); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-08 23:33:22 +01:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_decode_originator_id(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!s->proto->is_internal) | 
					
						
							|  |  |  |     DISCARD(BAD_EBGP, "ORIGINATOR_ID"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (len != 4) | 
					
						
							|  |  |  |     WITHDRAW(BAD_LENGTH, "ORIGINATOR_ID", len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   u32 val = get_u32(data); | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_u32(to, BA_ORIGINATOR_ID, flags, val); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_export_cluster_list(struct bgp_export_state *s UNUSED, eattr *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!s->proto->is_internal) | 
					
						
							|  |  |  |     UNSET(a); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (a->u.ptr->length == 0) | 
					
						
							|  |  |  |     UNSET(a); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_cluster_list(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!s->proto->is_internal) | 
					
						
							|  |  |  |     DISCARD(BAD_EBGP, "CLUSTER_LIST"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!len || (len % 4)) | 
					
						
							|  |  |  |     WITHDRAW(BAD_LENGTH, "CLUSTER_LIST", len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   struct adata *ad = lp_alloc_adata(s->pool, len); | 
					
						
							|  |  |  |   get_u32s(data, (u32 *) ad->data, len / 4); | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_ptr(to, BA_CLUSTER_LIST, flags, ad); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2019-08-17 13:36:36 +02:00
										 |  |  | bgp_format_cluster_list(const eattr *a, byte *buf, uint size) | 
					
						
							| 
									
										
										
										
											2008-11-08 23:33:22 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-07-03 19:43:30 +02:00
										 |  |  |   /* Truncates cluster lists larger than buflen, probably not a problem */ | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   int_set_format(a->u.ptr, 0, -1, buf, size); | 
					
						
							| 
									
										
										
										
											2008-11-08 23:33:22 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 15:37:01 +01:00
										 |  |  | int | 
					
						
							|  |  |  | bgp_encode_mp_reach_mrt(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /*
 | 
					
						
							|  |  |  |    *	Limited version of MP_REACH_NLRI used for MRT table dumps (IPv6 only): | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    *	3 B	MP_REACH_NLRI header | 
					
						
							|  |  |  |    *	1 B	MP_REACH_NLRI data - Length of Next Hop Network Address | 
					
						
							|  |  |  |    *	var	MP_REACH_NLRI data - Network Address of Next Hop | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ip_addr *nh = (void *) a->u.ptr->data; | 
					
						
							|  |  |  |   uint len = a->u.ptr->length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ASSERT((len == 16) || (len == 32)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (size < (3+1+len)) | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bgp_put_attr_hdr3(buf, BA_MP_REACH_NLRI, BAF_OPTIONAL, 1+len); | 
					
						
							|  |  |  |   buf[3] = len; | 
					
						
							|  |  |  |   buf += 4; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   put_ip6(buf, ipa_to_ip6(nh[0])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (len == 32) | 
					
						
							|  |  |  |     put_ip6(buf+16, ipa_to_ip6(nh[1])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 3+1+len; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static inline u32 | 
					
						
							|  |  |  | get_af3(byte *buf) | 
					
						
							| 
									
										
										
										
											2000-05-04 09:03:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   return (get_u16(buf) << 16) | buf[2]; | 
					
						
							| 
									
										
										
										
											2000-05-04 09:03:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | bgp_decode_mp_reach_nlri(struct bgp_parse_state *s, uint code UNUSED, uint flags UNUSED, byte *data, uint len, ea_list **to UNUSED) | 
					
						
							| 
									
										
										
										
											2000-05-04 09:03:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /*
 | 
					
						
							|  |  |  |    *	2 B	MP_REACH_NLRI data - Address Family Identifier | 
					
						
							|  |  |  |    *	1 B	MP_REACH_NLRI data - Subsequent Address Family Identifier | 
					
						
							|  |  |  |    *	1 B	MP_REACH_NLRI data - Length of Next Hop Network Address | 
					
						
							|  |  |  |    *	var	MP_REACH_NLRI data - Network Address of Next Hop | 
					
						
							|  |  |  |    *	1 B	MP_REACH_NLRI data - Reserved (zero) | 
					
						
							|  |  |  |    *	var	MP_REACH_NLRI data - Network Layer Reachability Information | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ((len < 5) || (len < (5 + (uint) data[3]))) | 
					
						
							|  |  |  |     bgp_parse_error(s, 9); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   s->mp_reach_af = get_af3(data); | 
					
						
							|  |  |  |   s->mp_next_hop_len = data[3]; | 
					
						
							|  |  |  |   s->mp_next_hop_data = data + 4; | 
					
						
							|  |  |  |   s->mp_reach_len = len - 5 - s->mp_next_hop_len; | 
					
						
							|  |  |  |   s->mp_reach_nlri = data + 5 + s->mp_next_hop_len; | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_mp_unreach_nlri(struct bgp_parse_state *s, uint code UNUSED, uint flags UNUSED, byte *data, uint len, ea_list **to UNUSED) | 
					
						
							| 
									
										
										
										
											2011-08-12 21:03:43 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /*
 | 
					
						
							|  |  |  |    *	2 B	MP_UNREACH_NLRI data - Address Family Identifier | 
					
						
							|  |  |  |    *	1 B	MP_UNREACH_NLRI data - Subsequent Address Family Identifier | 
					
						
							|  |  |  |    *	var	MP_UNREACH_NLRI data - Network Layer Reachability Information | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (len < 3) | 
					
						
							|  |  |  |     bgp_parse_error(s, 9); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   s->mp_unreach_af = get_af3(data); | 
					
						
							|  |  |  |   s->mp_unreach_len = len - 3; | 
					
						
							|  |  |  |   s->mp_unreach_nlri = data + 3; | 
					
						
							| 
									
										
										
										
											2011-08-12 21:03:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_export_ext_community(struct bgp_export_state *s, eattr *a) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-24 17:47:45 +02:00
										 |  |  |   if (!s->proto->is_interior) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     struct adata *ad = ec_set_del_nontrans(s->pool, a->u.ptr); | 
					
						
							| 
									
										
										
										
											2017-12-13 15:57:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-24 17:47:45 +02:00
										 |  |  |     if (ad->length == 0) | 
					
						
							|  |  |  |       UNSET(a); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-24 17:47:45 +02:00
										 |  |  |     ec_set_sort_x(ad); | 
					
						
							|  |  |  |     a->u.ptr = ad; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if (a->u.ptr->length == 0) | 
					
						
							|  |  |  |       UNSET(a); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     a->u.ptr = ec_set_sort(s->pool, a->u.ptr); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_ext_community(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!len || (len % 8)) | 
					
						
							|  |  |  |     WITHDRAW(BAD_LENGTH, "EXT_COMMUNITY", len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   struct adata *ad = lp_alloc_adata(s->pool, len); | 
					
						
							|  |  |  |   get_u32s(data, (u32 *) ad->data, len / 4); | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_ptr(to, BA_EXT_COMMUNITY, flags, ad); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_as4_aggregator(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (s->as4_session) | 
					
						
							|  |  |  |     DISCARD(NEW_BGP, "AS4_AGGREGATOR"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (len != 8) | 
					
						
							|  |  |  |     DISCARD(BAD_LENGTH, "AS4_AGGREGATOR", len); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_data(to, BA_AS4_AGGREGATOR, flags, data, len); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_as4_path(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-04 22:07:03 +01:00
										 |  |  |   struct bgp_proto *p = s->proto; | 
					
						
							|  |  |  |   int sets = p->cf->allow_as_sets; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   char err[128]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (s->as4_session) | 
					
						
							|  |  |  |     DISCARD(NEW_BGP, "AS4_PATH"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (len < 6) | 
					
						
							|  |  |  |     DISCARD(BAD_LENGTH, "AS4_PATH", len); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-04 22:07:03 +01:00
										 |  |  |   if (!as_path_valid(data, len, 4, sets, 1, err, sizeof(err))) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     DISCARD("Malformed AS4_PATH attribute - %s", err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  |   struct adata *a = lp_alloc_adata(s->pool, len); | 
					
						
							|  |  |  |   memcpy(a->data, data, len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* AS_CONFED* segments are invalid in AS4_PATH; RFC 6793 6 */ | 
					
						
							|  |  |  |   if (as_path_contains_confed(a)) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     REPORT("Discarding AS_CONFED* segment from AS4_PATH attribute"); | 
					
						
							|  |  |  |     a = as_path_strip_confed(s->pool, a); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_ptr(to, BA_AS4_PATH, flags, a); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_export_aigp(struct bgp_export_state *s, eattr *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!s->channel->cf->aigp) | 
					
						
							|  |  |  |     UNSET(a); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_aigp(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char err[128]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Acceptability test postponed to bgp_finish_attrs() */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ((flags ^ bgp_attr_table[BA_AIGP].flags) & (BAF_OPTIONAL | BAF_TRANSITIVE)) | 
					
						
							|  |  |  |     DISCARD("Malformed AIGP attribute - conflicting flags (%02x)", flags); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!bgp_aigp_valid(data, len, err, sizeof(err))) | 
					
						
							|  |  |  |     DISCARD("Malformed AIGP attribute - %s", err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_data(to, BA_AIGP, flags, data, len); | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2019-08-17 13:36:36 +02:00
										 |  |  | bgp_format_aigp(const eattr *a, byte *buf, uint size UNUSED) | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   const byte *b = bgp_aigp_get_tlv(a->u.ptr, BGP_AIGP_METRIC); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!b) | 
					
						
							|  |  |  |     bsprintf(buf, "?"); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     bsprintf(buf, "%lu", get_u64(b + 3)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | bgp_export_large_community(struct bgp_export_state *s, eattr *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (a->u.ptr->length == 0) | 
					
						
							|  |  |  |     UNSET(a); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   a->u.ptr = lc_set_sort(s->pool, a->u.ptr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_large_community(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!len || (len % 12)) | 
					
						
							|  |  |  |     WITHDRAW(BAD_LENGTH, "LARGE_COMMUNITY", len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   struct adata *ad = lp_alloc_adata(s->pool, len); | 
					
						
							|  |  |  |   get_u32s(data, (u32 *) ad->data, len / 4); | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_ptr(to, BA_LARGE_COMMUNITY, flags, ad); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-22 15:00:07 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | bgp_export_mpls_label_stack(struct bgp_export_state *s, eattr *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   net_addr *n = s->route->net->n.addr; | 
					
						
							|  |  |  |   u32 *labels = (u32 *) a->u.ptr->data; | 
					
						
							|  |  |  |   uint lnum = a->u.ptr->length / 4; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Perhaps we should just ignore it? */ | 
					
						
							|  |  |  |   if (!s->mpls) | 
					
						
							| 
									
										
										
										
											2022-01-28 05:35:22 +01:00
										 |  |  |     REJECT("Unexpected MPLS stack"); | 
					
						
							| 
									
										
										
										
											2017-03-22 15:00:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Empty MPLS stack is not allowed */ | 
					
						
							|  |  |  |   if (!lnum) | 
					
						
							| 
									
										
										
										
											2022-01-28 05:35:22 +01:00
										 |  |  |     REJECT("Malformed MPLS stack - empty"); | 
					
						
							| 
									
										
										
										
											2017-03-22 15:00:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* This is ugly, but we must ensure that labels fit into NLRI field */ | 
					
						
							|  |  |  |   if ((24*lnum + (net_is_vpn(n) ? 64 : 0) + net_pxlen(n)) > 255) | 
					
						
							| 
									
										
										
										
											2022-01-28 05:35:22 +01:00
										 |  |  |     REJECT("Malformed MPLS stack - too many labels (%u)", lnum); | 
					
						
							| 
									
										
										
										
											2017-03-22 15:00:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for (uint i = 0; i < lnum; i++) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if (labels[i] > 0xfffff) | 
					
						
							| 
									
										
										
										
											2022-01-28 05:35:22 +01:00
										 |  |  |       REJECT("Malformed MPLS stack - invalid label (%u)", labels[i]); | 
					
						
							| 
									
										
										
										
											2017-03-22 15:00:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* TODO: Check for special-purpose label values? */ | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | bgp_encode_mpls_label_stack(struct bgp_write_state *s, eattr *a, byte *buf UNUSED, uint size UNUSED) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /*
 | 
					
						
							|  |  |  |    * MPLS labels are encoded as a part of the NLRI in MP_REACH_NLRI attribute, | 
					
						
							|  |  |  |    * so we store MPLS_LABEL_STACK and encode it later by AFI-specific hooks. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   s->mpls_labels = a->u.ptr; | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | bgp_decode_mpls_label_stack(struct bgp_parse_state *s, uint code UNUSED, uint flags UNUSED, byte *data UNUSED, uint len UNUSED, ea_list **to UNUSED) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   DISCARD("Discarding received attribute #0"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2019-08-17 13:36:36 +02:00
										 |  |  | bgp_format_mpls_label_stack(const eattr *a, byte *buf, uint size) | 
					
						
							| 
									
										
										
										
											2017-03-22 15:00:07 +01:00
										 |  |  | { | 
					
						
							|  |  |  |   u32 *labels = (u32 *) a->u.ptr->data; | 
					
						
							|  |  |  |   uint lnum = a->u.ptr->length / 4; | 
					
						
							|  |  |  |   char *pos = buf; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (uint i = 0; i < lnum; i++) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if (size < 20) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       bsprintf(pos, "..."); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     uint l = bsprintf(pos, "%d/", labels[i]); | 
					
						
							|  |  |  |     ADVANCE(pos, size, l); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Clear last slash or terminate empty string */ | 
					
						
							|  |  |  |   pos[lnum ? -1 : 0] = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | static inline void | 
					
						
							|  |  |  | bgp_export_unknown(struct bgp_export_state *s UNUSED, eattr *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!(a->flags & BAF_TRANSITIVE)) | 
					
						
							|  |  |  |     UNSET(a); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   a->flags |= BAF_PARTIAL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static inline void | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  | bgp_decode_unknown(struct bgp_parse_state *s UNUSED, uint code, uint flags, byte *data, uint len, ea_list **to) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   if (!(flags & BAF_OPTIONAL)) | 
					
						
							|  |  |  |     WITHDRAW("Unknown attribute (code %u) - conflicting flags (%02x)", code, flags); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-02 16:57:45 +01:00
										 |  |  |   /* Cannot use bgp_set_attr_data() as it works on known attributes only */ | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   ea_set_attr_data(to, &bgp_attr_table[code].class, flags, data, len); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline void | 
					
						
							|  |  |  | bgp_format_unknown(const eattr *a, byte *buf, uint size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (a->flags & BAF_TRANSITIVE) | 
					
						
							|  |  |  |     bsnprintf(buf, size, "(transitive)"); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  *	Attribute table | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | static union bgp_attr_desc bgp_attr_table[BGP_ATTR_MAX] = { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   [BA_ORIGIN] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_origin", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_ENUM_BGP_ORIGIN, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_TRANSITIVE, | 
					
						
							|  |  |  |     .export = bgp_export_origin, | 
					
						
							|  |  |  |     .encode = bgp_encode_u8, | 
					
						
							|  |  |  |     .decode = bgp_decode_origin, | 
					
						
							|  |  |  |     .format = bgp_format_origin, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_AS_PATH] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_path", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_PATH, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_TRANSITIVE, | 
					
						
							|  |  |  |     .encode = bgp_encode_as_path, | 
					
						
							|  |  |  |     .decode = bgp_decode_as_path, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_NEXT_HOP] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_next_hop", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_IP, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_TRANSITIVE, | 
					
						
							|  |  |  |     .encode = bgp_encode_next_hop, | 
					
						
							|  |  |  |     .decode = bgp_decode_next_hop, | 
					
						
							|  |  |  |     .format = bgp_format_next_hop, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_MULTI_EXIT_DISC] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_med", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_INT, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_OPTIONAL, | 
					
						
							|  |  |  |     .encode = bgp_encode_u32, | 
					
						
							|  |  |  |     .decode = bgp_decode_med, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_LOCAL_PREF] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_local_pref", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_INT, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_TRANSITIVE, | 
					
						
							|  |  |  |     .export = bgp_export_local_pref, | 
					
						
							|  |  |  |     .encode = bgp_encode_u32, | 
					
						
							|  |  |  |     .decode = bgp_decode_local_pref, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_ATOMIC_AGGR] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_atomic_aggr", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_OPAQUE, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_TRANSITIVE, | 
					
						
							|  |  |  |     .encode = bgp_encode_raw, | 
					
						
							|  |  |  |     .decode = bgp_decode_atomic_aggr, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_AGGREGATOR] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_aggregator", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_OPAQUE, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_OPTIONAL | BAF_TRANSITIVE, | 
					
						
							|  |  |  |     .encode = bgp_encode_aggregator, | 
					
						
							|  |  |  |     .decode = bgp_decode_aggregator, | 
					
						
							|  |  |  |     .format = bgp_format_aggregator, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_COMMUNITY] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_community", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_CLIST, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_OPTIONAL | BAF_TRANSITIVE, | 
					
						
							|  |  |  |     .export = bgp_export_community, | 
					
						
							|  |  |  |     .encode = bgp_encode_u32s, | 
					
						
							|  |  |  |     .decode = bgp_decode_community, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_ORIGINATOR_ID] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_originator_id", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_QUAD, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_OPTIONAL, | 
					
						
							|  |  |  |     .export = bgp_export_originator_id, | 
					
						
							|  |  |  |     .encode = bgp_encode_u32, | 
					
						
							|  |  |  |     .decode = bgp_decode_originator_id, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_CLUSTER_LIST] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_cluster_list", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_CLIST, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_OPTIONAL, | 
					
						
							|  |  |  |     .export = bgp_export_cluster_list, | 
					
						
							|  |  |  |     .encode = bgp_encode_u32s, | 
					
						
							|  |  |  |     .decode = bgp_decode_cluster_list, | 
					
						
							|  |  |  |     .format = bgp_format_cluster_list, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_MP_REACH_NLRI] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_mp_reach_nlri", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_OPAQUE, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_OPTIONAL, | 
					
						
							|  |  |  |     .decode = bgp_decode_mp_reach_nlri, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_MP_UNREACH_NLRI] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_mp_unreach_nlri", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_OPAQUE, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_OPTIONAL, | 
					
						
							|  |  |  |     .decode = bgp_decode_mp_unreach_nlri, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_EXT_COMMUNITY] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_ext_community", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_ECLIST, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_OPTIONAL | BAF_TRANSITIVE, | 
					
						
							|  |  |  |     .export = bgp_export_ext_community, | 
					
						
							|  |  |  |     .encode = bgp_encode_u32s, | 
					
						
							|  |  |  |     .decode = bgp_decode_ext_community, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_AS4_PATH] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_as4_path", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_PATH, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_OPTIONAL | BAF_TRANSITIVE, | 
					
						
							|  |  |  |     .encode = bgp_encode_raw, | 
					
						
							|  |  |  |     .decode = bgp_decode_as4_path, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   [BA_AS4_AGGREGATOR] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_as4_aggregator", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_OPAQUE, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_OPTIONAL | BAF_TRANSITIVE, | 
					
						
							|  |  |  |     .encode = bgp_encode_raw, | 
					
						
							|  |  |  |     .decode = bgp_decode_as4_aggregator, | 
					
						
							|  |  |  |     .format = bgp_format_aggregator, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  |   [BA_AIGP] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_aigp", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_OPAQUE, | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  |     .flags = BAF_OPTIONAL | BAF_DECODE_FLAGS, | 
					
						
							|  |  |  |     .export = bgp_export_aigp, | 
					
						
							|  |  |  |     .encode = bgp_encode_raw, | 
					
						
							|  |  |  |     .decode = bgp_decode_aigp, | 
					
						
							|  |  |  |     .format = bgp_format_aigp, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   [BA_LARGE_COMMUNITY] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_large_community", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_LCLIST, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     .flags = BAF_OPTIONAL | BAF_TRANSITIVE, | 
					
						
							|  |  |  |     .export = bgp_export_large_community, | 
					
						
							|  |  |  |     .encode = bgp_encode_u32s, | 
					
						
							|  |  |  |     .decode = bgp_decode_large_community, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2017-03-22 15:00:07 +01:00
										 |  |  |   [BA_MPLS_LABEL_STACK] = { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .name = "bgp_mpls_label_stack", | 
					
						
							| 
									
										
										
										
											2022-03-26 11:56:02 +01:00
										 |  |  |     .type = T_CLIST, | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     .readonly = 1, | 
					
						
							| 
									
										
										
										
											2017-03-22 15:00:07 +01:00
										 |  |  |     .export = bgp_export_mpls_label_stack, | 
					
						
							|  |  |  |     .encode = bgp_encode_mpls_label_stack, | 
					
						
							|  |  |  |     .decode = bgp_decode_mpls_label_stack, | 
					
						
							|  |  |  |     .format = bgp_format_mpls_label_stack, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | eattr * | 
					
						
							|  |  |  | bgp_find_attr(ea_list *attrs, uint code) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   return ea_find(attrs, BGP_EA_ID(code)); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | void | 
					
						
							|  |  |  | bgp_register_attrs(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   for (uint i=0; i<ARRAY_SIZE(bgp_attr_table); i++) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if (!bgp_attr_table[i].name) | 
					
						
							|  |  |  |       bgp_attr_table[i] = (union bgp_attr_desc) { | 
					
						
							|  |  |  | 	.name = mb_sprintf(&root_pool, "bgp_unknown_0x%02x", i), | 
					
						
							|  |  |  | 	.type = T_OPAQUE, | 
					
						
							|  |  |  | 	.flags = BAF_OPTIONAL, | 
					
						
							|  |  |  | 	.readonly = 1, | 
					
						
							|  |  |  | 	.export = bgp_export_unknown, | 
					
						
							|  |  |  | 	.encode = bgp_encode_raw, | 
					
						
							|  |  |  | 	.decode = bgp_decode_unknown, | 
					
						
							|  |  |  | 	.format = bgp_format_unknown, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ea_register_init(&bgp_attr_table[i].class); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  *	Attribute export | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static inline void | 
					
						
							|  |  |  | bgp_export_attr(struct bgp_export_state *s, eattr *a, ea_list *to) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   const union bgp_attr_desc *desc = bgp_find_attr_desc(a); | 
					
						
							|  |  |  |   if (!desc) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   /* The flags might have been zero if the attr was added locally */ | 
					
						
							|  |  |  |   a->flags = (a->flags & BAF_PARTIAL) | desc->flags; | 
					
						
							| 
									
										
										
										
											2000-05-02 12:51:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   /* Set partial bit if new opt-trans attribute is attached to non-local route */ | 
					
						
							|  |  |  |   if ((s->src != NULL) && (a->originated) && | 
					
						
							|  |  |  |       (a->flags & BAF_OPTIONAL) && (a->flags & BAF_TRANSITIVE)) | 
					
						
							|  |  |  |     a->flags |= BAF_PARTIAL; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   /* Call specific hook */ | 
					
						
							|  |  |  |   CALL(desc->export, s, a); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   /* Attribute might become undefined in hook */ | 
					
						
							|  |  |  |   if (a->undef) | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Append updated attribute */ | 
					
						
							|  |  |  |   to->attrs[to->count++] = *a; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * bgp_export_attrs - export BGP attributes | 
					
						
							|  |  |  |  * @s: BGP export state | 
					
						
							|  |  |  |  * @attrs: a list of extended attributes | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The bgp_export_attrs() function takes a list of attributes and merges it to | 
					
						
							|  |  |  |  * one newly allocated and sorted segment. Attributes are validated and | 
					
						
							|  |  |  |  * normalized by type-specific export hooks and attribute flags are updated. | 
					
						
							|  |  |  |  * Some attributes may be eliminated (e.g. unknown non-tranitive attributes, or | 
					
						
							|  |  |  |  * empty community sets). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Result: one sorted attribute list segment, or NULL if attributes are unsuitable. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static inline ea_list * | 
					
						
							| 
									
										
										
										
											2022-04-10 14:11:46 +02:00
										 |  |  | bgp_export_attrs(struct bgp_export_state *s, const ea_list *a) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* Merge the attribute list */ | 
					
						
							| 
									
										
										
										
											2022-04-10 14:11:46 +02:00
										 |  |  |   ea_list *new = ea_normalize(a); | 
					
						
							|  |  |  |   ASSERT_DIE(new); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   uint i, count; | 
					
						
							|  |  |  |   count = new->count; | 
					
						
							|  |  |  |   new->count = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Export each attribute */ | 
					
						
							|  |  |  |   for (i = 0; i < count; i++) | 
					
						
							|  |  |  |     bgp_export_attr(s, &new->attrs[i], new); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-28 05:35:22 +01:00
										 |  |  |   if (s->err_reject) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return new; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  *	Attribute encoding | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | bgp_encode_attr(struct bgp_write_state *s, eattr *a, byte *buf, uint size) | 
					
						
							| 
									
										
										
										
											2000-05-04 20:02:56 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   const union bgp_attr_desc *desc = bgp_find_attr_desc(a); | 
					
						
							|  |  |  |   ASSERT_DIE(desc); | 
					
						
							|  |  |  |   return desc->encode(s, a, buf, size); | 
					
						
							| 
									
										
										
										
											2000-05-04 20:02:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * bgp_encode_attrs - encode BGP attributes | 
					
						
							|  |  |  |  * @s: BGP write state | 
					
						
							|  |  |  |  * @attrs: a list of extended attributes | 
					
						
							|  |  |  |  * @buf: buffer | 
					
						
							|  |  |  |  * @end: buffer end | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The bgp_encode_attrs() function takes a list of extended attributes | 
					
						
							|  |  |  |  * and converts it to its BGP representation (a part of an Update message). | 
					
						
							| 
									
										
										
										
											2018-11-20 17:38:19 +01:00
										 |  |  |  * BGP write state may be fake when called from MRT protocol. | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Result: Length of the attribute block generated or -1 if not enough space. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | bgp_encode_attrs(struct bgp_write_state *s, ea_list *attrs, byte *buf, byte *end) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   byte *pos = buf; | 
					
						
							|  |  |  |   int i, len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (i = 0; i < attrs->count; i++) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     len = bgp_encode_attr(s, &attrs->attrs[i], pos, end - pos); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (len < 0) | 
					
						
							|  |  |  |       return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pos += len; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return pos - buf; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  *	Attribute decoding | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void bgp_process_as4_attrs(ea_list **attrs, struct linpool *pool); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | bgp_as_path_loopy(struct bgp_proto *p, ea_list *attrs, u32 asn) | 
					
						
							| 
									
										
										
										
											2000-05-04 20:02:56 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   eattr *e = bgp_find_attr(attrs, BA_AS_PATH); | 
					
						
							|  |  |  |   int num = p->cf->allow_local_as + 1; | 
					
						
							|  |  |  |   return (e && (num > 0) && as_path_contains(e->u.ptr, asn, num)); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static inline int | 
					
						
							|  |  |  | bgp_originator_id_loopy(struct bgp_proto *p, ea_list *attrs) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   eattr *e = bgp_find_attr(attrs, BA_ORIGINATOR_ID); | 
					
						
							|  |  |  |   return (e && (e->u.data == p->local_id)); | 
					
						
							| 
									
										
										
										
											2000-05-04 20:02:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static inline int | 
					
						
							|  |  |  | bgp_cluster_list_loopy(struct bgp_proto *p, ea_list *attrs) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   eattr *e = bgp_find_attr(attrs, BA_CLUSTER_LIST); | 
					
						
							|  |  |  |   return (e && int_set_contains(e->u.ptr, p->rr_cluster_id)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static inline void | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  | bgp_decode_attr(struct bgp_parse_state *s, byte code, byte flags, byte *data, uint len, ea_list **to) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | { | 
					
						
							|  |  |  |   /* Handle duplicate attributes; RFC 7606 3 (g) */ | 
					
						
							|  |  |  |   if (BIT32_TEST(s->attrs_seen, code)) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if ((code == BA_MP_REACH_NLRI) || (code == BA_MP_UNREACH_NLRI)) | 
					
						
							|  |  |  |       bgp_parse_error(s, 1); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       DISCARD("Discarding duplicate attribute (code %u)", code); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   BIT32_SET(s->attrs_seen, code); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   ASSERT_DIE(bgp_attr_table[code].id); | 
					
						
							|  |  |  |   const union bgp_attr_desc *desc = &bgp_attr_table[code]; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   /* Handle conflicting flags; RFC 7606 3 (c) */ | 
					
						
							|  |  |  |   if (((flags ^ desc->flags) & (BAF_OPTIONAL | BAF_TRANSITIVE)) && | 
					
						
							|  |  |  |       !(desc->flags & BAF_DECODE_FLAGS)) | 
					
						
							|  |  |  |     WITHDRAW("Malformed %s attribute - conflicting flags (%02x, expected %02x)", desc->name, flags, desc->flags); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   desc->decode(s, code, flags, data, len, to); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * bgp_decode_attrs - check and decode BGP attributes | 
					
						
							|  |  |  |  * @s: BGP parse state | 
					
						
							|  |  |  |  * @data: start of attribute block | 
					
						
							|  |  |  |  * @len: length of attribute block | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This function takes a BGP attribute block (a part of an Update message), checks | 
					
						
							|  |  |  |  * its consistency and converts it to a list of BIRD route attributes represented | 
					
						
							|  |  |  |  * by an (uncached) &rta. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | ea_list * | 
					
						
							|  |  |  | bgp_decode_attrs(struct bgp_parse_state *s, byte *data, uint len) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   struct bgp_proto *p = s->proto; | 
					
						
							|  |  |  |   ea_list *attrs = NULL; | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   uint alen; | 
					
						
							|  |  |  |   byte code, flags; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   byte *pos = data; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* Parse the attributes */ | 
					
						
							|  |  |  |   while (len) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     alen = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Read attribute type */ | 
					
						
							|  |  |  |     if (len < 2) | 
					
						
							|  |  |  |       goto framing_error; | 
					
						
							|  |  |  |     flags = pos[0]; | 
					
						
							|  |  |  |     code = pos[1]; | 
					
						
							|  |  |  |     ADVANCE(pos, len, 2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Read attribute length */ | 
					
						
							|  |  |  |     if (flags & BAF_EXT_LEN) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |       if (len < 2) | 
					
						
							|  |  |  | 	goto framing_error; | 
					
						
							|  |  |  |       alen = get_u16(pos); | 
					
						
							|  |  |  |       ADVANCE(pos, len, 2); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (len < 1) | 
					
						
							|  |  |  | 	goto framing_error; | 
					
						
							|  |  |  |       alen = *pos; | 
					
						
							|  |  |  |       ADVANCE(pos, len, 1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (alen > len) | 
					
						
							|  |  |  |       goto framing_error; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     DBG("Attr %02x %02x %u\n", code, flags, alen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bgp_decode_attr(s, code, flags, pos, alen, &attrs); | 
					
						
							|  |  |  |     ADVANCE(pos, len, alen); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (s->err_withdraw) | 
					
						
							|  |  |  |     goto withdraw; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* If there is no reachability NLRI, we are finished */ | 
					
						
							|  |  |  |   if (!s->ip_reach_len && !s->mp_reach_len) | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Handle missing mandatory attributes; RFC 7606 3 (d) */ | 
					
						
							|  |  |  |   if (!BIT32_TEST(s->attrs_seen, BA_ORIGIN)) | 
					
						
							|  |  |  |   { REPORT(NO_MANDATORY, "ORIGIN"); goto withdraw; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!BIT32_TEST(s->attrs_seen, BA_AS_PATH)) | 
					
						
							|  |  |  |   { REPORT(NO_MANDATORY, "AS_PATH"); goto withdraw; } | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-13 14:47:37 +02:00
										 |  |  |   if (s->ip_reach_len && !BIT32_TEST(s->attrs_seen, BA_NEXT_HOP)) | 
					
						
							|  |  |  |   { REPORT(NO_MANDATORY, "NEXT_HOP"); goto withdraw; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* When receiving attributes from non-AS4-aware BGP speaker, we have to
 | 
					
						
							|  |  |  |      reconstruct AS_PATH and AGGREGATOR attributes; RFC 6793 4.2.3 */ | 
					
						
							|  |  |  |   if (!p->as4_session) | 
					
						
							|  |  |  |     bgp_process_as4_attrs(&attrs, s->pool); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* Reject routes with our ASN in AS_PATH attribute */ | 
					
						
							|  |  |  |   if (bgp_as_path_loopy(p, attrs, p->local_as)) | 
					
						
							| 
									
										
										
										
											2022-01-28 18:13:18 +01:00
										 |  |  |     goto loop; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  |   /* Reject routes with our Confederation ID in AS_PATH attribute; RFC 5065 4.0 */ | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if ((p->public_as != p->local_as) && bgp_as_path_loopy(p, attrs, p->public_as)) | 
					
						
							| 
									
										
										
										
											2022-01-28 18:13:18 +01:00
										 |  |  |     goto loop; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* Reject routes with our Router ID in ORIGINATOR_ID attribute; RFC 4456 8 */ | 
					
						
							|  |  |  |   if (p->is_internal && bgp_originator_id_loopy(p, attrs)) | 
					
						
							| 
									
										
										
										
											2022-01-28 18:13:18 +01:00
										 |  |  |     goto loop; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* Reject routes with our Cluster ID in CLUSTER_LIST attribute; RFC 4456 8 */ | 
					
						
							|  |  |  |   if (p->rr_client && bgp_cluster_list_loopy(p, attrs)) | 
					
						
							| 
									
										
										
										
											2022-01-28 18:13:18 +01:00
										 |  |  |     goto loop; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* If there is no local preference, define one */ | 
					
						
							|  |  |  |   if (!BIT32_TEST(s->attrs_seen, BA_LOCAL_PREF)) | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |     bgp_set_attr_u32(&attrs, BA_LOCAL_PREF, 0, p->cf->default_local_pref); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   return attrs; | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | framing_error: | 
					
						
							|  |  |  |   /* RFC 7606 4 - handle attribute framing errors */ | 
					
						
							|  |  |  |   REPORT("Malformed attribute list - framing error (%u/%u) at %d", | 
					
						
							|  |  |  | 	 alen, len, (int) (pos - s->attrs)); | 
					
						
							| 
									
										
										
										
											2000-04-10 12:39:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | withdraw: | 
					
						
							|  |  |  |   /* RFC 7606 5.2 - handle missing NLRI during errors */ | 
					
						
							|  |  |  |   if (!s->ip_reach_len && !s->mp_reach_len) | 
					
						
							|  |  |  |     bgp_parse_error(s, 1); | 
					
						
							| 
									
										
										
										
											2000-04-12 14:09:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   s->err_withdraw = 1; | 
					
						
							|  |  |  |   return NULL; | 
					
						
							| 
									
										
										
										
											2022-01-28 18:13:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | loop: | 
					
						
							|  |  |  |   /* Loops are handled as withdraws, but ignored silently. Do not set err_withdraw. */ | 
					
						
							|  |  |  |   return NULL; | 
					
						
							| 
									
										
										
										
											2000-04-17 11:25:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  | void | 
					
						
							|  |  |  | bgp_finish_attrs(struct bgp_parse_state *s, rta *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /* AIGP test here instead of in bgp_decode_aigp() - we need to know channel */ | 
					
						
							|  |  |  |   if (BIT32_TEST(s->attrs_seen, BA_AIGP) && !s->channel->cf->aigp) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     REPORT("Discarding AIGP attribute received on non-AIGP session"); | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |     bgp_unset_attr(&a->eattrs, BA_AIGP); | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-10 12:39:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *	Route bucket hash table | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-08-12 21:03:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | #define RBH_KEY(b)		b->eattrs, b->hash
 | 
					
						
							|  |  |  | #define RBH_NEXT(b)		b->next
 | 
					
						
							|  |  |  | #define RBH_EQ(a1,h1,a2,h2)	h1 == h2 && ea_same(a1, a2)
 | 
					
						
							|  |  |  | #define RBH_FN(a,h)		h
 | 
					
						
							| 
									
										
										
										
											2011-08-12 21:03:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | #define RBH_REHASH		bgp_rbh_rehash
 | 
					
						
							|  |  |  | #define RBH_PARAMS		/8, *2, 2, 2, 8, 20
 | 
					
						
							| 
									
										
										
										
											2011-08-12 21:03:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | HASH_DEFINE_REHASH_FN(RBH, struct bgp_bucket) | 
					
						
							| 
									
										
										
										
											2016-10-01 12:50:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | void | 
					
						
							|  |  |  | bgp_init_bucket_table(struct bgp_channel *c) | 
					
						
							| 
									
										
										
										
											2016-10-01 12:50:29 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   HASH_INIT(c->bucket_hash, c->pool, 8); | 
					
						
							| 
									
										
										
										
											2016-10-01 12:50:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   init_list(&c->bucket_queue); | 
					
						
							|  |  |  |   c->withdraw_bucket = NULL; | 
					
						
							| 
									
										
										
										
											2000-04-10 12:39:51 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 00:55:34 +01:00
										 |  |  | void | 
					
						
							|  |  |  | bgp_free_bucket_table(struct bgp_channel *c) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   HASH_FREE(c->bucket_hash); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   struct bgp_bucket *b; | 
					
						
							|  |  |  |   WALK_LIST_FIRST(b, c->bucket_queue) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     rem_node(&b->send_node); | 
					
						
							|  |  |  |     mb_free(b); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mb_free(c->withdraw_bucket); | 
					
						
							|  |  |  |   c->withdraw_bucket = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-10 12:39:51 +00:00
										 |  |  | static struct bgp_bucket * | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_get_bucket(struct bgp_channel *c, ea_list *new) | 
					
						
							| 
									
										
										
										
											2000-04-10 12:39:51 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-10 14:11:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* Hash and lookup */ | 
					
						
							|  |  |  |   u32 hash = ea_hash(new); | 
					
						
							|  |  |  |   struct bgp_bucket *b = HASH_FIND(c->bucket_hash, RBH, new, hash); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (b) | 
					
						
							|  |  |  |     return b; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 14:11:46 +02:00
										 |  |  |   /* Scan the list for total size */ | 
					
						
							|  |  |  |   uint ea_size = BIRD_CPU_ALIGN(ea_list_size(new)); | 
					
						
							|  |  |  |   uint size = sizeof(struct bgp_bucket) + ea_size; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 14:11:46 +02:00
										 |  |  |   /* Allocate the bucket */ | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   b = mb_alloc(c->pool, size); | 
					
						
							| 
									
										
										
										
											2020-11-25 15:44:00 +01:00
										 |  |  |   *b = (struct bgp_bucket) { }; | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  |   init_list(&b->prefixes); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   b->hash = hash; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 14:11:46 +02:00
										 |  |  |   /* Copy the ea_list */ | 
					
						
							|  |  |  |   ea_list_copy(b->eattrs, new, ea_size); | 
					
						
							| 
									
										
										
										
											2000-04-10 12:39:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* Insert the bucket to send queue and bucket hash */ | 
					
						
							|  |  |  |   add_tail(&c->bucket_queue, &b->send_node); | 
					
						
							|  |  |  |   HASH_INSERT2(c->bucket_hash, RBH, c->pool, b); | 
					
						
							| 
									
										
										
										
											2000-04-10 12:39:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return b; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct bgp_bucket * | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_get_withdraw_bucket(struct bgp_channel *c) | 
					
						
							| 
									
										
										
										
											2000-04-10 12:39:51 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (!c->withdraw_bucket) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     c->withdraw_bucket = mb_allocz(c->pool, sizeof(struct bgp_bucket)); | 
					
						
							|  |  |  |     init_list(&c->withdraw_bucket->prefixes); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2000-04-10 12:39:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   return c->withdraw_bucket; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2000-04-10 12:39:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | void | 
					
						
							|  |  |  | bgp_free_bucket(struct bgp_channel *c, struct bgp_bucket *b) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   rem_node(&b->send_node); | 
					
						
							|  |  |  |   HASH_REMOVE2(c->bucket_hash, RBH, c->pool, b); | 
					
						
							|  |  |  |   mb_free(b); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | void | 
					
						
							|  |  |  | bgp_defer_bucket(struct bgp_channel *c, struct bgp_bucket *b) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   rem_node(&b->send_node); | 
					
						
							|  |  |  |   add_tail(&c->bucket_queue, &b->send_node); | 
					
						
							| 
									
										
										
										
											2000-04-10 12:39:51 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_withdraw_bucket(struct bgp_channel *c, struct bgp_bucket *b) | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   struct bgp_proto *p = (void *) c->c.proto; | 
					
						
							|  |  |  |   struct bgp_bucket *wb = bgp_get_withdraw_bucket(c); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   log(L_ERR "%s: Attribute list too long", p->p.name); | 
					
						
							|  |  |  |   while (!EMPTY_LIST(b->prefixes)) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     struct bgp_prefix *px = HEAD(b->prefixes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     log(L_ERR "%s: - withdrawing %N", p->p.name, &px->net); | 
					
						
							|  |  |  |     rem_node(&px->buck_node); | 
					
						
							|  |  |  |     add_tail(&wb->prefixes, &px->buck_node); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2000-04-17 09:37:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *	Prefix hash table | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | #define PXH_KEY(px)		px->net, px->path_id, px->hash
 | 
					
						
							|  |  |  | #define PXH_NEXT(px)		px->next
 | 
					
						
							|  |  |  | #define PXH_EQ(n1,i1,h1,n2,i2,h2) h1 == h2 && i1 == i2 && net_equal(n1, n2)
 | 
					
						
							|  |  |  | #define PXH_FN(n,i,h)		h
 | 
					
						
							| 
									
										
										
										
											2013-11-26 22:37:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define PXH_REHASH		bgp_pxh_rehash
 | 
					
						
							| 
									
										
										
										
											2019-06-11 13:19:21 +00:00
										 |  |  | #define PXH_PARAMS		/8, *2, 2, 2, 8, 24
 | 
					
						
							| 
									
										
										
										
											2013-11-26 22:37:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-26 22:37:24 +01:00
										 |  |  | HASH_DEFINE_REHASH_FN(PXH, struct bgp_prefix) | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_init_prefix_table(struct bgp_channel *c) | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   HASH_INIT(c->prefix_hash, c->pool, 8); | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 18:28:07 +01:00
										 |  |  |   uint alen = net_addr_length[c->c.net_type]; | 
					
						
							|  |  |  |   c->prefix_slab = alen ? sl_new(c->pool, sizeof(struct bgp_prefix) + alen) : NULL; | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-25 11:51:38 +01:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2017-02-08 14:34:48 +01:00
										 |  |  | bgp_free_prefix_table(struct bgp_channel *c) | 
					
						
							| 
									
										
										
										
											2016-11-25 11:51:38 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-02-08 14:34:48 +01:00
										 |  |  |   HASH_FREE(c->prefix_hash); | 
					
						
							| 
									
										
										
										
											2016-11-25 11:51:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 14:34:48 +01:00
										 |  |  |   rfree(c->prefix_slab); | 
					
						
							|  |  |  |   c->prefix_slab = NULL; | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct bgp_prefix * | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_get_prefix(struct bgp_channel *c, net_addr *net, u32 path_id) | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   u32 hash = net_hash(net) ^ u32_hash(path_id); | 
					
						
							|  |  |  |   struct bgp_prefix *px = HASH_FIND(c->prefix_hash, PXH, net, path_id, hash); | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (px) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     rem_node(&px->buck_node); | 
					
						
							|  |  |  |     return px; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 18:28:07 +01:00
										 |  |  |   if (c->prefix_slab) | 
					
						
							|  |  |  |     px = sl_alloc(c->prefix_slab); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     px = mb_alloc(c->pool, sizeof(struct bgp_prefix) + net->length); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-25 15:44:00 +01:00
										 |  |  |   *px = (struct bgp_prefix) { }; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   px->hash = hash; | 
					
						
							|  |  |  |   px->path_id = path_id; | 
					
						
							|  |  |  |   net_copy(px->net, net); | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   HASH_INSERT2(c->prefix_hash, PXH, c->pool, px); | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   return px; | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_free_prefix(struct bgp_channel *c, struct bgp_prefix *px) | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   rem_node(&px->buck_node); | 
					
						
							|  |  |  |   HASH_REMOVE2(c->prefix_hash, PXH, c->pool, px); | 
					
						
							| 
									
										
										
										
											2016-12-07 18:28:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (c->prefix_slab) | 
					
						
							| 
									
										
										
										
											2022-04-04 20:31:14 +02:00
										 |  |  |     sl_free(px); | 
					
						
							| 
									
										
										
										
											2016-12-07 18:28:07 +01:00
										 |  |  |   else | 
					
						
							|  |  |  |     mb_free(px); | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *	BGP protocol glue | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2020-04-12 21:54:20 +02:00
										 |  |  | bgp_preexport(struct proto *P, rte *e) | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-10 17:08:29 +02:00
										 |  |  |   struct proto *SRC = e->src->proto; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   struct bgp_proto *p = (struct bgp_proto *) P; | 
					
						
							|  |  |  |   struct bgp_proto *src = (SRC->proto == &proto_bgp) ? (struct bgp_proto *) SRC : NULL; | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* Reject our routes */ | 
					
						
							|  |  |  |   if (src == p) | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* Accept non-BGP routes */ | 
					
						
							|  |  |  |   if (src == NULL) | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 20:25:35 +01:00
										 |  |  |   /* Reject flowspec that failed validation */ | 
					
						
							|  |  |  |   if ((e->attrs->dest == RTD_UNREACHABLE) && net_is_flow(e->net->n.addr)) | 
					
						
							|  |  |  |       return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* IBGP route reflection, RFC 4456 */ | 
					
						
							|  |  |  |   if (p->is_internal && src->is_internal && (p->local_as == src->local_as)) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     /* Rejected unless configured as route reflector */ | 
					
						
							|  |  |  |     if (!p->rr_client && !src->rr_client) | 
					
						
							|  |  |  |       return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Generally, this should be handled when path is received, but we check it
 | 
					
						
							|  |  |  |        also here as rr_cluster_id may be undefined or different in src. */ | 
					
						
							|  |  |  |     if (p->rr_cluster_id && bgp_cluster_list_loopy(p, e->attrs->eattrs)) | 
					
						
							|  |  |  |       return -1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Handle well-known communities, RFC 1997 */ | 
					
						
							|  |  |  |   struct eattr *c; | 
					
						
							|  |  |  |   if (p->cf->interpret_communities && | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |       (c = ea_find(e->attrs->eattrs, BGP_EA_ID(BA_COMMUNITY)))) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2018-12-27 14:26:11 +01:00
										 |  |  |     const struct adata *d = c->u.ptr; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Do not export anywhere */ | 
					
						
							|  |  |  |     if (int_set_contains(d, BGP_COMM_NO_ADVERTISE)) | 
					
						
							|  |  |  |       return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Do not export outside of AS (or member-AS) */ | 
					
						
							|  |  |  |     if (!p->is_internal && int_set_contains(d, BGP_COMM_NO_EXPORT_SUBCONFED)) | 
					
						
							|  |  |  |       return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Do not export outside of AS (or confederation) */ | 
					
						
							|  |  |  |     if (!p->is_interior && int_set_contains(d, BGP_COMM_NO_EXPORT)) | 
					
						
							|  |  |  |       return -1; | 
					
						
							| 
									
										
										
										
											2018-07-31 18:40:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Do not export LLGR_STALE routes to LLGR-ignorant peers */ | 
					
						
							|  |  |  |     if (!p->conn->remote_caps->llgr_aware && int_set_contains(d, BGP_COMM_LLGR_STALE)) | 
					
						
							|  |  |  |       return -1; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | static ea_list * | 
					
						
							| 
									
										
										
										
											2017-02-07 15:55:51 +01:00
										 |  |  | bgp_update_attrs(struct bgp_proto *p, struct bgp_channel *c, rte *e, ea_list *attrs0, struct linpool *pool) | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-10 17:08:29 +02:00
										 |  |  |   struct proto *SRC = e->src->proto; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   struct bgp_proto *src = (SRC->proto == &proto_bgp) ? (void *) SRC : NULL; | 
					
						
							| 
									
										
										
										
											2017-03-22 15:00:07 +01:00
										 |  |  |   struct bgp_export_state s = { .proto = p, .channel = c, .pool = pool, .src = src, .route = e, .mpls = c->desc->mpls }; | 
					
						
							| 
									
										
										
										
											2017-02-07 15:55:51 +01:00
										 |  |  |   ea_list *attrs = attrs0; | 
					
						
							| 
									
										
										
										
											2000-04-10 11:21:40 +00:00
										 |  |  |   eattr *a; | 
					
						
							| 
									
										
										
										
											2018-12-27 14:26:11 +01:00
										 |  |  |   const adata *ad; | 
					
						
							| 
									
										
										
										
											2000-04-10 11:21:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* ORIGIN attribute - mandatory, attach if missing */ | 
					
						
							| 
									
										
										
										
											2017-02-07 15:55:51 +01:00
										 |  |  |   if (! bgp_find_attr(attrs0, BA_ORIGIN)) | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |     bgp_set_attr_u32(&attrs, BA_ORIGIN, 0, src ? ORIGIN_INCOMPLETE : ORIGIN_IGP); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-07 15:55:51 +01:00
										 |  |  |   /* AS_PATH attribute - mandatory */ | 
					
						
							|  |  |  |   a = bgp_find_attr(attrs0, BA_AS_PATH); | 
					
						
							|  |  |  |   ad = a ? a->u.ptr : &null_adata; | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* AS_PATH attribute - strip AS_CONFED* segments outside confederation */ | 
					
						
							|  |  |  |   if ((!p->cf->confederation || !p->is_interior) && as_path_contains_confed(ad)) | 
					
						
							|  |  |  |     ad = as_path_strip_confed(pool, ad); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* AS_PATH attribute - keep or prepend ASN */ | 
					
						
							| 
									
										
										
											
												BGP: Do not prepend ASN in export from non-RS EBGP to RS EBGP
When route is exported to regular EBGP, local ASN should be prepended to
AS_PATH. When route is propagated by route server (between RS-marked
EBGP peers), it should not change AS_PATH. Question is what to do in
other cases (from non-RS EBGP, IBGP, or locally originated to RS EBGP).
In 1.6.x, we did not prepend ASN in non-RS EBGP or IBGP to RS EBGP, but
we prepended in local to RS EBGP.
In 2.0.x, we changed that so only RS-EBGP to RS-EBGP is not prepended.
We received some negative responses (thanks to heisenbug and Alexander
Zubkov), we decided to change it back. One reason is that it is simple
to modify the AS_PATH by filters, but not possible to un-modify
changes done by BGP itself. Also, as 1.6.x behavior was not really
consistent, the final behavior is that ASN is never prepended when
exported to RS EBGP, like to IBGP.
Note that i do not express an opinion about whether such configurations
are even reasonable.
											
										 
											2018-12-12 17:36:54 +01:00
										 |  |  |   if (p->is_internal || p->rs_client) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   { | 
					
						
							|  |  |  |     /* IBGP or route server -> just ensure there is one */ | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  |     if (!a) | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |       bgp_set_attr_ptr(&attrs, BA_AS_PATH, 0, &null_adata); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   else if (p->is_interior) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  |     /* Confederation -> prepend ASN as AS_CONFED_SEQUENCE */ | 
					
						
							|  |  |  |     ad = as_path_prepend2(pool, ad, AS_PATH_CONFED_SEQUENCE, p->public_as); | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |     bgp_set_attr_ptr(&attrs, BA_AS_PATH, 0, ad); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   else /* Regular EBGP (no RS, no confederation) */ | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  |     /* Regular EBGP -> prepend ASN as regular sequence */ | 
					
						
							|  |  |  |     ad = as_path_prepend2(pool, ad, AS_PATH_SEQUENCE, p->public_as); | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |     bgp_set_attr_ptr(&attrs, BA_AS_PATH, 0, ad); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* MULTI_EXIT_DESC attribute - accept only if set in export filter */ | 
					
						
							| 
									
										
										
										
											2017-02-07 15:55:51 +01:00
										 |  |  |     a = bgp_find_attr(attrs0, BA_MULTI_EXIT_DISC); | 
					
						
							| 
									
										
										
										
											2022-03-14 10:06:44 +01:00
										 |  |  |     if (a && !(a->fresh)) | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |       bgp_unset_attr(&attrs, BA_MULTI_EXIT_DISC); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* NEXT_HOP attribute - delegated to AF-specific hook */ | 
					
						
							| 
									
										
										
										
											2017-02-07 15:55:51 +01:00
										 |  |  |   a = bgp_find_attr(attrs0, BA_NEXT_HOP); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   bgp_update_next_hop(&s, a, &attrs); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* LOCAL_PREF attribute - required for IBGP, attach if missing */ | 
					
						
							| 
									
										
										
										
											2017-02-07 15:55:51 +01:00
										 |  |  |   if (p->is_interior && ! bgp_find_attr(attrs0, BA_LOCAL_PREF)) | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |     bgp_set_attr_u32(&attrs, BA_LOCAL_PREF, 0, p->cf->default_local_pref); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  |   /* AIGP attribute - accumulate local metric or originate new one */ | 
					
						
							|  |  |  |   u64 metric; | 
					
						
							|  |  |  |   if (s.local_next_hop && | 
					
						
							|  |  |  |       (bgp_total_aigp_metric_(e, &metric, &ad) || | 
					
						
							|  |  |  |        (c->cf->aigp_originate && bgp_init_aigp_metric(e, &metric, &ad)))) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     ad = bgp_aigp_set_metric(pool, ad, metric); | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |     bgp_set_attr_ptr(&attrs, BA_AIGP, 0, ad); | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* IBGP route reflection, RFC 4456 */ | 
					
						
							|  |  |  |   if (src && src->is_internal && p->is_internal && (src->local_as == p->local_as)) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     /* ORIGINATOR_ID attribute - attach if not already set */ | 
					
						
							| 
									
										
										
										
											2017-02-07 15:55:51 +01:00
										 |  |  |     if (! bgp_find_attr(attrs0, BA_ORIGINATOR_ID)) | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |       bgp_set_attr_u32(&attrs, BA_ORIGINATOR_ID, 0, src->remote_id); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* CLUSTER_LIST attribute - prepend cluster ID */ | 
					
						
							| 
									
										
										
										
											2017-02-07 15:55:51 +01:00
										 |  |  |     a = bgp_find_attr(attrs0, BA_CLUSTER_LIST); | 
					
						
							|  |  |  |     ad = a ? a->u.ptr : NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Prepend src cluster ID */ | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     if (src->rr_cluster_id) | 
					
						
							| 
									
										
										
										
											2017-02-08 14:34:48 +01:00
										 |  |  |       ad = int_set_prepend(pool, ad, src->rr_cluster_id); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-07 15:55:51 +01:00
										 |  |  |     /* Prepend dst cluster ID if src and dst clusters are different */ | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     if (p->rr_cluster_id && (src->rr_cluster_id != p->rr_cluster_id)) | 
					
						
							| 
									
										
										
										
											2017-02-08 14:34:48 +01:00
										 |  |  |       ad = int_set_prepend(pool, ad, p->rr_cluster_id); | 
					
						
							| 
									
										
										
										
											2017-02-07 15:55:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Should be at least one prepended cluster ID */ | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |     bgp_set_attr_ptr(&attrs, BA_CLUSTER_LIST, 0, ad); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* AS4_* transition attributes, RFC 6793 4.2.2 */ | 
					
						
							|  |  |  |   if (! p->as4_session) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     a = bgp_find_attr(attrs, BA_AS_PATH); | 
					
						
							|  |  |  |     if (a && as_path_contains_as4(a->u.ptr)) | 
					
						
							| 
									
										
										
										
											2000-04-10 11:21:40 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |       bgp_set_attr_ptr(&attrs, BA_AS_PATH, 0, as_path_to_old(pool, a->u.ptr)); | 
					
						
							|  |  |  |       bgp_set_attr_ptr(&attrs, BA_AS4_PATH, 0, as_path_strip_confed(pool, a->u.ptr)); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     a = bgp_find_attr(attrs, BA_AGGREGATOR); | 
					
						
							|  |  |  |     if (a && aggregator_contains_as4(a->u.ptr)) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |       bgp_set_attr_ptr(&attrs, BA_AGGREGATOR, 0, aggregator_to_old(pool, a->u.ptr)); | 
					
						
							|  |  |  |       bgp_set_attr_ptr(&attrs, BA_AS4_AGGREGATOR, 0, a->u.ptr); | 
					
						
							| 
									
										
										
										
											2000-04-10 11:21:40 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-07 15:55:51 +01:00
										 |  |  |   /*
 | 
					
						
							|  |  |  |    * Presence of mandatory attributes ORIGIN and AS_PATH is ensured by above | 
					
						
							|  |  |  |    * conditions. Presence and validity of quasi-mandatory NEXT_HOP attribute | 
					
						
							|  |  |  |    * should be checked in AF-specific hooks. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* Apply per-attribute export hooks for validatation and normalization */ | 
					
						
							|  |  |  |   return bgp_export_attrs(&s, attrs); | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2018-05-29 12:08:12 +02:00
										 |  |  | bgp_rt_notify(struct proto *P, struct channel *C, net *n, rte *new, rte *old) | 
					
						
							| 
									
										
										
										
											2010-01-27 17:22:57 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   struct bgp_proto *p = (void *) P; | 
					
						
							|  |  |  |   struct bgp_channel *c = (void *) C; | 
					
						
							|  |  |  |   struct bgp_bucket *buck; | 
					
						
							|  |  |  |   struct bgp_prefix *px; | 
					
						
							|  |  |  |   u32 path; | 
					
						
							| 
									
										
										
										
											2010-01-27 17:22:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (new) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2022-03-02 11:22:32 +01:00
										 |  |  |     struct ea_list *attrs = bgp_update_attrs(p, c, new, new->attrs->eattrs, tmp_linpool); | 
					
						
							| 
									
										
										
										
											2010-01-27 17:22:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 03:44:21 +01:00
										 |  |  |     /* Error during attribute processing */ | 
					
						
							|  |  |  |     if (!attrs) | 
					
						
							|  |  |  |       log(L_ERR "%s: Invalid route %N withdrawn", p->p.name, n->n.addr); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     /* If attributes are invalid, we fail back to withdraw */ | 
					
						
							|  |  |  |     buck = attrs ? bgp_get_bucket(c, attrs) : bgp_get_withdraw_bucket(c); | 
					
						
							| 
									
										
										
										
											2020-04-10 17:08:29 +02:00
										 |  |  |     path = new->src->global_id; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   { | 
					
						
							|  |  |  |     buck = bgp_get_withdraw_bucket(c); | 
					
						
							| 
									
										
										
										
											2020-04-10 17:08:29 +02:00
										 |  |  |     path = old->src->global_id; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   px = bgp_get_prefix(c, n->n.addr, c->add_path_tx ? path : 0); | 
					
						
							|  |  |  |   add_tail(&buck->prefixes, &px->buck_node); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bgp_schedule_packet(p->conn, c, PKT_UPDATE); | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-26 22:59:21 +01:00
										 |  |  | static inline u32 | 
					
						
							|  |  |  | bgp_get_neighbor(rte *r) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   eattr *e = ea_find(r->attrs->eattrs, BGP_EA_ID(BA_AS_PATH)); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:59:21 +01:00
										 |  |  |   u32 as; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  |   if (e && as_path_get_first_regular(e->u.ptr, &as)) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:59:21 +01:00
										 |  |  |     return as; | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* If AS_PATH is not defined, we treat rte as locally originated */ | 
					
						
							| 
									
										
										
										
											2020-04-10 17:08:29 +02:00
										 |  |  |   struct bgp_proto *p = (void *) r->src->proto; | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  |   return p->cf->confederation ?: p->local_as; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:59:21 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 18:40:38 +02:00
										 |  |  | static inline int | 
					
						
							|  |  |  | rte_stale(rte *r) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  |   if (r->pflags & BGP_REF_STALE) | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (r->pflags & BGP_REF_NOT_STALE) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* If staleness is unknown, compute and cache it */ | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   eattr *a = ea_find(r->attrs->eattrs, BGP_EA_ID(BA_COMMUNITY)); | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  |   if (a && int_set_contains(a->u.ptr, BGP_COMM_LLGR_STALE)) | 
					
						
							| 
									
										
										
										
											2018-07-31 18:40:38 +02:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  |     r->pflags |= BGP_REF_STALE; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     r->pflags |= BGP_REF_NOT_STALE; | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-07-31 18:40:38 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  | int | 
					
						
							|  |  |  | bgp_rte_better(rte *new, rte *old) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-10 17:08:29 +02:00
										 |  |  |   struct bgp_proto *new_bgp = (struct bgp_proto *) new->src->proto; | 
					
						
							|  |  |  |   struct bgp_proto *old_bgp = (struct bgp_proto *) old->src->proto; | 
					
						
							| 
									
										
										
										
											2000-04-17 12:46:07 +00:00
										 |  |  |   eattr *x, *y; | 
					
						
							|  |  |  |   u32 n, o; | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  |   /* Skip suppressed routes (see bgp_rte_recalculate()) */ | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  |   n = new->pflags & BGP_REF_SUPPRESSED; | 
					
						
							|  |  |  |   o = old->pflags & BGP_REF_SUPPRESSED; | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  |   if (n > o) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  |   if (n < o) | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-28 13:13:34 +02:00
										 |  |  |   /* RFC 4271 9.1.2.1. Route resolvability test */ | 
					
						
							| 
									
										
										
										
											2010-12-07 23:33:55 +01:00
										 |  |  |   n = rte_resolvable(new); | 
					
						
							|  |  |  |   o = rte_resolvable(old); | 
					
						
							| 
									
										
										
										
											2010-07-28 13:13:34 +02:00
										 |  |  |   if (n > o) | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   if (n < o) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 18:40:38 +02:00
										 |  |  |   /* LLGR draft - depreference stale routes */ | 
					
						
							|  |  |  |   n = rte_stale(new); | 
					
						
							|  |  |  |   o = rte_stale(old); | 
					
						
							|  |  |  |   if (n > o) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  |   if (n < o) | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  /* Start with local preferences */ | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   x = ea_find(new->attrs->eattrs, BGP_EA_ID(BA_LOCAL_PREF)); | 
					
						
							|  |  |  |   y = ea_find(old->attrs->eattrs, BGP_EA_ID(BA_LOCAL_PREF)); | 
					
						
							| 
									
										
										
										
											2000-04-17 12:46:07 +00:00
										 |  |  |   n = x ? x->u.data : new_bgp->cf->default_local_pref; | 
					
						
							|  |  |  |   o = y ? y->u.data : old_bgp->cf->default_local_pref; | 
					
						
							|  |  |  |   if (n > o) | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   if (n < o) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  |   /* RFC 7311 4.1 - Apply AIGP metric */ | 
					
						
							|  |  |  |   u64 n2 = bgp_total_aigp_metric(new); | 
					
						
							|  |  |  |   u64 o2 = bgp_total_aigp_metric(old); | 
					
						
							|  |  |  |   if (n2 < o2) | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   if (n2 > o2) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  |   /* RFC 4271 9.1.2.2. a)  Use AS path lengths */ | 
					
						
							| 
									
										
										
										
											2000-04-17 12:46:07 +00:00
										 |  |  |   if (new_bgp->cf->compare_path_lengths || old_bgp->cf->compare_path_lengths) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     x = ea_find(new->attrs->eattrs, BGP_EA_ID(BA_AS_PATH)); | 
					
						
							|  |  |  |     y = ea_find(old->attrs->eattrs, BGP_EA_ID(BA_AS_PATH)); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     n = x ? as_path_getlen(x->u.ptr) : AS_PATH_MAXLEN; | 
					
						
							|  |  |  |     o = y ? as_path_getlen(y->u.ptr) : AS_PATH_MAXLEN; | 
					
						
							|  |  |  |     if (n < o) | 
					
						
							|  |  |  |       return 1; | 
					
						
							|  |  |  |     if (n > o) | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  |   /* RFC 4271 9.1.2.2. b) Use origins */ | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   x = ea_find(new->attrs->eattrs, BGP_EA_ID(BA_ORIGIN)); | 
					
						
							|  |  |  |   y = ea_find(old->attrs->eattrs, BGP_EA_ID(BA_ORIGIN)); | 
					
						
							| 
									
										
										
										
											2000-04-28 15:13:29 +00:00
										 |  |  |   n = x ? x->u.data : ORIGIN_INCOMPLETE; | 
					
						
							|  |  |  |   o = y ? y->u.data : ORIGIN_INCOMPLETE; | 
					
						
							| 
									
										
										
										
											2000-04-17 12:46:07 +00:00
										 |  |  |   if (n < o) | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   if (n > o) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  |   /* RFC 4271 9.1.2.2. c) Compare MED's */ | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  |   /* Proper RFC 4271 path selection cannot be interpreted as finding
 | 
					
						
							|  |  |  |    * the best path in some ordering. It is implemented partially in | 
					
						
							|  |  |  |    * bgp_rte_recalculate() when deterministic_med option is | 
					
						
							|  |  |  |    * active. Without that option, the behavior is just an | 
					
						
							|  |  |  |    * approximation, which in specific situations may lead to | 
					
						
							|  |  |  |    * persistent routing loops, because it is nondeterministic - it | 
					
						
							|  |  |  |    * depends on the order in which routes appeared. But it is also the | 
					
						
							|  |  |  |    * same behavior as used by default in Cisco routers, so it is | 
					
						
							|  |  |  |    * probably not a big issue. | 
					
						
							| 
									
										
										
										
											2011-04-22 16:13:27 +02:00
										 |  |  |    */ | 
					
						
							|  |  |  |   if (new_bgp->cf->med_metric || old_bgp->cf->med_metric || | 
					
						
							|  |  |  |       (bgp_get_neighbor(new) == bgp_get_neighbor(old))) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     x = ea_find(new->attrs->eattrs, BGP_EA_ID(BA_MULTI_EXIT_DISC)); | 
					
						
							|  |  |  |     y = ea_find(old->attrs->eattrs, BGP_EA_ID(BA_MULTI_EXIT_DISC)); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     n = x ? x->u.data : new_bgp->cf->default_med; | 
					
						
							|  |  |  |     o = y ? y->u.data : old_bgp->cf->default_med; | 
					
						
							|  |  |  |     if (n < o) | 
					
						
							|  |  |  |       return 1; | 
					
						
							|  |  |  |     if (n > o) | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2000-04-17 12:46:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  |   /* RFC 4271 9.1.2.2. d) Prefer external peers */ | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (new_bgp->is_interior > old_bgp->is_interior) | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   if (new_bgp->is_interior < old_bgp->is_interior) | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-31 01:04:32 +02:00
										 |  |  |   /* RFC 4271 9.1.2.2. e) Compare IGP metrics */ | 
					
						
							| 
									
										
										
										
											2022-04-20 10:25:14 +02:00
										 |  |  |   n = new_bgp->cf->igp_metric ? rt_get_igp_metric(new) : 0; | 
					
						
							|  |  |  |   o = old_bgp->cf->igp_metric ? rt_get_igp_metric(old) : 0; | 
					
						
							| 
									
										
										
										
											2010-07-31 01:04:32 +02:00
										 |  |  |   if (n < o) | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   if (n > o) | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* RFC 4271 9.1.2.2. f) Compare BGP identifiers */ | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* RFC 4456 9. a) Use ORIGINATOR_ID instead of local neighbor ID */ | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   x = ea_find(new->attrs->eattrs, BGP_EA_ID(BA_ORIGINATOR_ID)); | 
					
						
							|  |  |  |   y = ea_find(old->attrs->eattrs, BGP_EA_ID(BA_ORIGINATOR_ID)); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  |   n = x ? x->u.data : new_bgp->remote_id; | 
					
						
							|  |  |  |   o = y ? y->u.data : old_bgp->remote_id; | 
					
						
							| 
									
										
										
										
											2009-11-17 11:41:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* RFC 5004 - prefer older routes */ | 
					
						
							|  |  |  |   /* (if both are external and from different peer) */ | 
					
						
							|  |  |  |   if ((new_bgp->cf->prefer_older || old_bgp->cf->prefer_older) && | 
					
						
							|  |  |  |       !new_bgp->is_internal && n != o) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* rest of RFC 4271 9.1.2.2. f) */ | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  |   if (n < o) | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   if (n > o) | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 14:09:24 +01:00
										 |  |  |   /* RFC 4456 9. b) Compare cluster list lengths */ | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   x = ea_find(new->attrs->eattrs, BGP_EA_ID(BA_CLUSTER_LIST)); | 
					
						
							|  |  |  |   y = ea_find(old->attrs->eattrs, BGP_EA_ID(BA_CLUSTER_LIST)); | 
					
						
							| 
									
										
										
										
											2010-02-26 14:09:24 +01:00
										 |  |  |   n = x ? int_set_get_size(x->u.ptr) : 0; | 
					
						
							|  |  |  |   o = y ? int_set_get_size(y->u.ptr) : 0; | 
					
						
							|  |  |  |   if (n < o) | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   if (n > o) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  |   /* RFC 4271 9.1.2.2. g) Compare peer IP adresses */ | 
					
						
							| 
									
										
										
										
											2019-04-02 17:22:31 +02:00
										 |  |  |   return ipa_compare(new_bgp->remote_ip, old_bgp->remote_ip) < 0; | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  | int | 
					
						
							|  |  |  | bgp_rte_mergable(rte *pri, rte *sec) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-10 17:08:29 +02:00
										 |  |  |   struct bgp_proto *pri_bgp = (struct bgp_proto *) pri->src->proto; | 
					
						
							|  |  |  |   struct bgp_proto *sec_bgp = (struct bgp_proto *) sec->src->proto; | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  |   eattr *x, *y; | 
					
						
							|  |  |  |   u32 p, s; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Skip suppressed routes (see bgp_rte_recalculate()) */ | 
					
						
							| 
									
										
										
										
											2022-03-22 14:39:32 +01:00
										 |  |  |   if ((pri->pflags ^ sec->pflags) & BGP_REF_SUPPRESSED) | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* RFC 4271 9.1.2.1. Route resolvability test */ | 
					
						
							| 
									
										
										
										
											2019-08-06 15:09:42 +02:00
										 |  |  |   if (rte_resolvable(pri) != rte_resolvable(sec)) | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-22 14:39:32 +01:00
										 |  |  |   /* LLGR draft - depreference stale routes */ | 
					
						
							|  |  |  |   if (rte_stale(pri) != rte_stale(sec)) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  |   /* Start with local preferences */ | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   x = ea_find(pri->attrs->eattrs, BGP_EA_ID(BA_LOCAL_PREF)); | 
					
						
							|  |  |  |   y = ea_find(sec->attrs->eattrs, BGP_EA_ID(BA_LOCAL_PREF)); | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  |   p = x ? x->u.data : pri_bgp->cf->default_local_pref; | 
					
						
							|  |  |  |   s = y ? y->u.data : sec_bgp->cf->default_local_pref; | 
					
						
							|  |  |  |   if (p != s) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* RFC 4271 9.1.2.2. a)  Use AS path lengths */ | 
					
						
							|  |  |  |   if (pri_bgp->cf->compare_path_lengths || sec_bgp->cf->compare_path_lengths) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     x = ea_find(pri->attrs->eattrs, BGP_EA_ID(BA_AS_PATH)); | 
					
						
							|  |  |  |     y = ea_find(sec->attrs->eattrs, BGP_EA_ID(BA_AS_PATH)); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     p = x ? as_path_getlen(x->u.ptr) : AS_PATH_MAXLEN; | 
					
						
							|  |  |  |     s = y ? as_path_getlen(y->u.ptr) : AS_PATH_MAXLEN; | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     if (p != s) | 
					
						
							|  |  |  |       return 0; | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | //    if (DELTA(p, s) > pri_bgp->cf->relax_multipath)
 | 
					
						
							|  |  |  | //      return 0;
 | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* RFC 4271 9.1.2.2. b) Use origins */ | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   x = ea_find(pri->attrs->eattrs, BGP_EA_ID(BA_ORIGIN)); | 
					
						
							|  |  |  |   y = ea_find(sec->attrs->eattrs, BGP_EA_ID(BA_ORIGIN)); | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  |   p = x ? x->u.data : ORIGIN_INCOMPLETE; | 
					
						
							|  |  |  |   s = y ? y->u.data : ORIGIN_INCOMPLETE; | 
					
						
							|  |  |  |   if (p != s) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* RFC 4271 9.1.2.2. c) Compare MED's */ | 
					
						
							|  |  |  |   if (pri_bgp->cf->med_metric || sec_bgp->cf->med_metric || | 
					
						
							|  |  |  |       (bgp_get_neighbor(pri) == bgp_get_neighbor(sec))) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |     x = ea_find(pri->attrs->eattrs, BGP_EA_ID(BA_MULTI_EXIT_DISC)); | 
					
						
							|  |  |  |     y = ea_find(sec->attrs->eattrs, BGP_EA_ID(BA_MULTI_EXIT_DISC)); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     p = x ? x->u.data : pri_bgp->cf->default_med; | 
					
						
							|  |  |  |     s = y ? y->u.data : sec_bgp->cf->default_med; | 
					
						
							|  |  |  |     if (p != s) | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* RFC 4271 9.1.2.2. d) Prefer external peers */ | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  |   if (pri_bgp->is_interior != sec_bgp->is_interior) | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* RFC 4271 9.1.2.2. e) Compare IGP metrics */ | 
					
						
							| 
									
										
										
										
											2022-04-20 10:25:14 +02:00
										 |  |  |   p = pri_bgp->cf->igp_metric ? rt_get_igp_metric(pri) : 0; | 
					
						
							|  |  |  |   s = sec_bgp->cf->igp_metric ? rt_get_igp_metric(sec) : 0; | 
					
						
							| 
									
										
										
										
											2015-06-08 02:20:43 +02:00
										 |  |  |   if (p != s) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Remaining criteria are ignored */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  | static inline int | 
					
						
							|  |  |  | same_group(rte *r, u32 lpref, u32 lasn) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-20 12:24:26 +02:00
										 |  |  |   return (rt_get_preference(r) == lpref) && (bgp_get_neighbor(r) == lasn); | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | use_deterministic_med(rte *r) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-10 17:08:29 +02:00
										 |  |  |   struct proto *P = r->src->proto; | 
					
						
							| 
									
										
										
										
											2012-07-04 21:31:03 +02:00
										 |  |  |   return (P->proto == &proto_bgp) && ((struct bgp_proto *) P)->cf->deterministic_med; | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | bgp_rte_recalculate(rtable *table, net *net, rte *new, rte *old, rte *old_best) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   rte *r, *s; | 
					
						
							|  |  |  |   rte *key = new ? new : old; | 
					
						
							| 
									
										
										
										
											2022-04-20 12:24:26 +02:00
										 |  |  |   u32 lpref = rt_get_preference(key); | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  |   u32 lasn = bgp_get_neighbor(key); | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  |   int old_suppressed = old ? !!(old->pflags & BGP_REF_SUPPRESSED) : 0; | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /*
 | 
					
						
							|  |  |  |    * Proper RFC 4271 path selection is a bit complicated, it cannot be | 
					
						
							|  |  |  |    * implemented just by rte_better(), because it is not a linear | 
					
						
							|  |  |  |    * ordering. But it can be splitted to two levels, where the lower | 
					
						
							|  |  |  |    * level chooses the best routes in each group of routes from the | 
					
						
							|  |  |  |    * same neighboring AS and higher level chooses the best route (with | 
					
						
							|  |  |  |    * a slightly different ordering) between the best-in-group routes. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * When deterministic_med is disabled, we just ignore this issue and | 
					
						
							|  |  |  |    * choose the best route by bgp_rte_better() alone. If enabled, the | 
					
						
							|  |  |  |    * lower level of the route selection is done here (for the group | 
					
						
							|  |  |  |    * to which the changed route belongs), all routes in group are | 
					
						
							|  |  |  |    * marked as suppressed, just chosen best-in-group is not. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * Global best route selection then implements higher level by | 
					
						
							|  |  |  |    * choosing between non-suppressed routes (as they are always | 
					
						
							|  |  |  |    * preferred over suppressed routes). Routes from BGP protocols | 
					
						
							|  |  |  |    * that do not set deterministic_med are just never suppressed. As | 
					
						
							|  |  |  |    * they do not participate in the lower level selection, it is OK | 
					
						
							|  |  |  |    * that this fn is not called for them. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * The idea is simple, the implementation is more problematic, | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |    * mostly because of optimizations in rte_recalculate() that | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  |    * avoids full recalculation in most cases. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * We can assume that at least one of new, old is non-NULL and both | 
					
						
							|  |  |  |    * are from the same protocol with enabled deterministic_med. We | 
					
						
							|  |  |  |    * group routes by both neighbor AS (lasn) and preference (lpref), | 
					
						
							|  |  |  |    * because bgp_rte_better() does not handle preference itself. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* If new and old are from different groups, we just process that
 | 
					
						
							|  |  |  |      as two independent events */ | 
					
						
							|  |  |  |   if (new && old && !same_group(old, lpref, lasn)) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   { | 
					
						
							|  |  |  |     int i1, i2; | 
					
						
							|  |  |  |     i1 = bgp_rte_recalculate(table, net, NULL, old, old_best); | 
					
						
							|  |  |  |     i2 = bgp_rte_recalculate(table, net, new, NULL, old_best); | 
					
						
							|  |  |  |     return i1 || i2; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /*
 | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  |    * We could find the best-in-group and then make some shortcuts like | 
					
						
							|  |  |  |    * in rte_recalculate, but as we would have to walk through all | 
					
						
							|  |  |  |    * net->routes just to find it, it is probably not worth. So we | 
					
						
							| 
									
										
										
										
											2019-08-06 15:09:42 +02:00
										 |  |  |    * just have one simple fast case that use just the old route. | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  |    * We also set suppressed flag to avoid using it in bgp_rte_better(). | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (new) | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  |     new->pflags |= BGP_REF_SUPPRESSED; | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (old) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  |     old->pflags |= BGP_REF_SUPPRESSED; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-06 15:09:42 +02:00
										 |  |  |     /* The fast case - replace not best with worse (or remove not best) */ | 
					
						
							|  |  |  |     if (old_suppressed && !(new && bgp_rte_better(new, old))) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |       return 0; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* The default case - find a new best-in-group route */ | 
					
						
							|  |  |  |   r = new; /* new may not be in the list */ | 
					
						
							| 
									
										
										
										
											2012-11-10 14:26:13 +01:00
										 |  |  |   for (s=net->routes; rte_is_valid(s); s=s->next) | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  |     if (use_deterministic_med(s) && same_group(s, lpref, lasn)) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  |       s->pflags |= BGP_REF_SUPPRESSED; | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |       if (!r || bgp_rte_better(s, r)) | 
					
						
							|  |  |  | 	r = s; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Simple case - the last route in group disappears */ | 
					
						
							|  |  |  |   if (!r) | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-06 15:09:42 +02:00
										 |  |  |   /* Found if new is mergable with best-in-group */ | 
					
						
							|  |  |  |   if (new && (new != r) && bgp_rte_mergable(r, new)) | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  |     new->pflags &= ~BGP_REF_SUPPRESSED; | 
					
						
							| 
									
										
										
										
											2019-08-06 15:09:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Found all existing routes mergable with best-in-group */ | 
					
						
							|  |  |  |   for (s=net->routes; rte_is_valid(s); s=s->next) | 
					
						
							|  |  |  |     if (use_deterministic_med(s) && same_group(s, lpref, lasn)) | 
					
						
							|  |  |  |       if ((s != r) && bgp_rte_mergable(r, s)) | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  | 	s->pflags &= ~BGP_REF_SUPPRESSED; | 
					
						
							| 
									
										
										
										
											2019-08-06 15:09:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  |   /* Found best-in-group */ | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  |   r->pflags &= ~BGP_REF_SUPPRESSED; | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /*
 | 
					
						
							|  |  |  |    * There are generally two reasons why we have to force | 
					
						
							|  |  |  |    * recalculation (return 1): First, the new route may be wrongfully | 
					
						
							|  |  |  |    * chosen to be the best in the first case check in | 
					
						
							|  |  |  |    * rte_recalculate(), this may happen only if old_best is from the | 
					
						
							|  |  |  |    * same group. Second, another (different than new route) | 
					
						
							|  |  |  |    * best-in-group is chosen and that may be the proper best (although | 
					
						
							|  |  |  |    * rte_recalculate() without ignore that possibility). | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * There are three possible cases according to whether the old route | 
					
						
							| 
									
										
										
										
											2019-08-06 15:09:42 +02:00
										 |  |  |    * was the best in group (OBG, i.e. !old_suppressed) and whether the | 
					
						
							|  |  |  |    * new route is the best in group (NBG, tested by r == new). These | 
					
						
							|  |  |  |    * cases work even if old or new is NULL. | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  |    * | 
					
						
							|  |  |  |    * NBG -> new is a possible candidate for the best route, so we just | 
					
						
							|  |  |  |    *        check for the first reason using same_group(). | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * !NBG && OBG -> Second reason applies, return 1 | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * !NBG && !OBG -> Best in group does not change, old != old_best, | 
					
						
							|  |  |  |    *                 rte_better(new, old_best) is false and therefore | 
					
						
							|  |  |  |    *                 the first reason does not apply, return 0 | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (r == new) | 
					
						
							|  |  |  |     return old_best && same_group(old_best, lpref, lasn); | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2019-08-06 15:09:42 +02:00
										 |  |  |     return !old_suppressed; | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 18:40:38 +02:00
										 |  |  | struct rte * | 
					
						
							|  |  |  | bgp_rte_modify_stale(struct rte *r, struct linpool *pool) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   eattr *a = ea_find(r->attrs->eattrs, BGP_EA_ID(BA_COMMUNITY)); | 
					
						
							| 
									
										
										
										
											2018-12-27 14:26:11 +01:00
										 |  |  |   const struct adata *ad = a ? a->u.ptr : NULL; | 
					
						
							| 
									
										
										
										
											2018-07-31 18:40:38 +02:00
										 |  |  |   uint flags = a ? a->flags : BAF_PARTIAL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (ad && int_set_contains(ad, BGP_COMM_NO_LLGR)) | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (ad && int_set_contains(ad, BGP_COMM_LLGR_STALE)) | 
					
						
							|  |  |  |     return r; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   r = rte_cow_rta(r, pool); | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   bgp_set_attr_ptr(&(r->attrs->eattrs), BA_COMMUNITY, flags, | 
					
						
							| 
									
										
										
										
											2018-07-31 18:40:38 +02:00
										 |  |  | 		   int_set_add(pool, ad, BGP_COMM_LLGR_STALE)); | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  |   r->pflags |= BGP_REF_STALE; | 
					
						
							| 
									
										
										
										
											2018-07-31 18:40:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return r; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Reconstruct AS_PATH and AGGREGATOR according to RFC 6793 4.2.3 | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  | bgp_process_as4_attrs(ea_list **attrs, struct linpool *pool) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   eattr *p2 = bgp_find_attr(*attrs, BA_AS_PATH); | 
					
						
							|  |  |  |   eattr *p4 = bgp_find_attr(*attrs, BA_AS4_PATH); | 
					
						
							|  |  |  |   eattr *a2 = bgp_find_attr(*attrs, BA_AGGREGATOR); | 
					
						
							|  |  |  |   eattr *a4 = bgp_find_attr(*attrs, BA_AS4_AGGREGATOR); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* First, unset AS4_* attributes */ | 
					
						
							| 
									
										
										
										
											2022-04-10 19:15:18 +02:00
										 |  |  |   if (p4) bgp_unset_attr(attrs, BA_AS4_PATH); | 
					
						
							|  |  |  |   if (a4) bgp_unset_attr(attrs, BA_AS4_AGGREGATOR); | 
					
						
							| 
									
										
										
										
											2000-03-31 23:21:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* Handle AGGREGATOR attribute */ | 
					
						
							|  |  |  |   if (a2 && a4) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     u32 a2_asn = get_u32(a2->u.ptr->data); | 
					
						
							| 
									
										
										
										
											2000-04-09 22:05:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     /* If routes were aggregated by an old router, then AS4_PATH and
 | 
					
						
							|  |  |  |        AS4_AGGREGATOR are invalid. In that case we give up. */ | 
					
						
							|  |  |  |     if (a2_asn != AS_TRANS) | 
					
						
							|  |  |  |       return; | 
					
						
							| 
									
										
										
										
											2009-04-23 14:44:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     /* Use AS4_AGGREGATOR instead of AGGREGATOR */ | 
					
						
							|  |  |  |     a2->u.ptr = a4->u.ptr; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2000-03-31 23:21:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   /* Handle AS_PATH attribute */ | 
					
						
							|  |  |  |   if (p2 && p4) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2017-01-22 16:32:42 +01:00
										 |  |  |     /* Both as_path_getlen() and as_path_cut() take AS_CONFED* as zero length */ | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     int p2_len = as_path_getlen(p2->u.ptr); | 
					
						
							|  |  |  |     int p4_len = as_path_getlen(p4->u.ptr); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:45:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     /* AS_PATH is too short, give up */ | 
					
						
							|  |  |  |     if (p2_len < p4_len) | 
					
						
							|  |  |  |       return; | 
					
						
							| 
									
										
										
										
											2000-03-31 23:21:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |     /* Merge AS_PATH and AS4_PATH */ | 
					
						
							| 
									
										
										
										
											2018-12-27 14:26:11 +01:00
										 |  |  |     struct adata *apc = as_path_cut(pool, p2->u.ptr, p2_len - p4_len); | 
					
						
							|  |  |  |     p2->u.ptr = as_path_merge(pool, apc, p4->u.ptr); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2000-03-31 23:21:37 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2000-04-01 10:21:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-05-19 11:01:41 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2018-05-29 12:08:12 +02:00
										 |  |  | bgp_get_route_info(rte *e, byte *buf) | 
					
						
							| 
									
										
										
										
											2000-05-19 11:01:41 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-19 16:23:42 +01:00
										 |  |  |   eattr *p = ea_find(e->attrs->eattrs, BGP_EA_ID(BA_AS_PATH)); | 
					
						
							|  |  |  |   eattr *o = ea_find(e->attrs->eattrs, BGP_EA_ID(BA_ORIGIN)); | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  |   u32 origas; | 
					
						
							| 
									
										
										
										
											2000-05-19 11:01:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-20 12:24:26 +02:00
										 |  |  |   buf += bsprintf(buf, " (%d", rt_get_preference(e)); | 
					
						
							| 
									
										
										
										
											2012-01-20 16:20:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 15:01:36 +01:00
										 |  |  |   if (e->pflags & BGP_REF_SUPPRESSED) | 
					
						
							| 
									
										
										
										
											2012-01-20 16:20:03 +01:00
										 |  |  |     buf += bsprintf(buf, "-"); | 
					
						
							| 
									
										
										
										
											2011-12-22 13:20:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 18:40:38 +02:00
										 |  |  |   if (rte_stale(e)) | 
					
						
							|  |  |  |     buf += bsprintf(buf, "s"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:17:20 +02:00
										 |  |  |   u64 metric = bgp_total_aigp_metric(e); | 
					
						
							|  |  |  |   if (metric < BGP_AIGP_MAX) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     buf += bsprintf(buf, "/%lu", metric); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-04-20 10:25:14 +02:00
										 |  |  |   else if (metric = rt_get_igp_metric(e)) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   { | 
					
						
							|  |  |  |     if (!rte_resolvable(e)) | 
					
						
							|  |  |  |       buf += bsprintf(buf, "/-"); | 
					
						
							| 
									
										
										
										
											2022-04-20 10:25:14 +02:00
										 |  |  |     else if (metric >= IGP_METRIC_UNKNOWN) | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |       buf += bsprintf(buf, "/?"); | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2022-04-20 10:25:14 +02:00
										 |  |  |       buf += bsprintf(buf, "/%d", metric); | 
					
						
							| 
									
										
										
										
											2016-12-07 14:11:28 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-07-31 01:04:32 +02:00
										 |  |  |   buf += bsprintf(buf, ") ["); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-12 20:44:58 +02:00
										 |  |  |   if (p && as_path_get_last(p->u.ptr, &origas)) | 
					
						
							| 
									
										
										
										
											2008-10-26 22:36:08 +01:00
										 |  |  |     buf += bsprintf(buf, "AS%u", origas); | 
					
						
							| 
									
										
										
										
											2000-05-19 11:01:41 +00:00
										 |  |  |   if (o) | 
					
						
							|  |  |  |     buf += bsprintf(buf, "%c", "ie?"[o->u.data]); | 
					
						
							|  |  |  |   strcpy(buf, "]"); | 
					
						
							|  |  |  | } |