1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* BGP carrying label information
3 * Copyright (C) 2013 Cumulus Networks, Inc.
19 #include "bgpd/bgpd.h"
20 #include "bgpd/bgp_table.h"
21 #include "bgpd/bgp_route.h"
22 #include "bgpd/bgp_attr.h"
23 #include "bgpd/bgp_label.h"
24 #include "bgpd/bgp_packet.h"
25 #include "bgpd/bgp_debug.h"
26 #include "bgpd/bgp_errors.h"
28 extern struct zclient
*zclient
;
30 int bgp_parse_fec_update(void)
33 struct bgp_dest
*dest
;
35 struct bgp_table
*table
;
43 memset(&p
, 0, sizeof(p
));
44 p
.family
= stream_getw(s
);
45 p
.prefixlen
= stream_getc(s
);
46 stream_get(p
.u
.val
, s
, PSIZE(p
.prefixlen
));
47 label
= stream_getl(s
);
49 /* hack for the bgp instance & SAFI = have to send/receive it */
50 afi
= family2afi(p
.family
);
52 bgp
= bgp_get_default();
54 zlog_debug("no default bgp instance");
58 table
= bgp
->rib
[afi
][safi
];
60 zlog_debug("no %u unicast table", p
.family
);
63 dest
= bgp_node_lookup(table
, &p
);
65 zlog_debug("no node for the prefix");
69 /* treat it as implicit withdraw - the label is invalid */
70 if (label
== MPLS_INVALID_LABEL
)
71 bgp_unset_valid_label(&dest
->local_label
);
73 dest
->local_label
= mpls_lse_encode(label
, 0, 0, 1);
74 bgp_set_valid_label(&dest
->local_label
);
76 SET_FLAG(dest
->flags
, BGP_NODE_LABEL_CHANGED
);
77 bgp_process(bgp
, dest
, afi
, safi
);
78 bgp_dest_unlock_node(dest
);
82 mpls_label_t
bgp_adv_label(struct bgp_dest
*dest
, struct bgp_path_info
*pi
,
83 struct peer
*to
, afi_t afi
, safi_t safi
)
86 mpls_label_t remote_label
;
89 if (!dest
|| !pi
|| !to
)
90 return MPLS_INVALID_LABEL
;
92 remote_label
= pi
->extra
? pi
->extra
->label
[0] : MPLS_INVALID_LABEL
;
95 ((from
->sort
== BGP_PEER_IBGP
) && (to
->sort
== BGP_PEER_IBGP
));
98 && !CHECK_FLAG(to
->af_flags
[afi
][safi
],
99 PEER_FLAG_FORCE_NEXTHOP_SELF
))
102 if (CHECK_FLAG(to
->af_flags
[afi
][safi
], PEER_FLAG_NEXTHOP_UNCHANGED
))
105 return dest
->local_label
;
108 static void bgp_send_fec_register_label_msg(struct bgp_dest
*dest
, bool reg
,
109 uint32_t label_index
)
113 const struct prefix
*p
;
115 size_t flags_pos
= 0;
116 mpls_label_t
*local_label
= &(dest
->local_label
);
120 mpls_label_t label
= MPLS_INVALID_LABEL
;
121 bool have_label_to_reg
;
123 mpls_lse_decode(*local_label
, &label
, &ttl
, &exp
, &bos
);
125 have_label_to_reg
= bgp_is_valid_label(local_label
) &&
126 label
!= MPLS_LABEL_IMPLICIT_NULL
;
128 p
= bgp_dest_get_prefix(dest
);
131 if (!zclient
|| zclient
->sock
< 0)
134 if (BGP_DEBUG(labelpool
, LABELPOOL
))
135 zlog_debug("%s: FEC %sregister %pRN label_index=%u label=%u",
136 __func__
, reg
? "" : "un", bgp_dest_to_rnode(dest
),
138 /* If the route node has a local_label assigned or the
139 * path node has an MPLS SR label index allowing zebra to
140 * derive the label, proceed with registration. */
143 command
= (reg
) ? ZEBRA_FEC_REGISTER
: ZEBRA_FEC_UNREGISTER
;
144 zclient_create_header(s
, command
, VRF_DEFAULT
);
145 flags_pos
= stream_get_endp(s
); /* save position of 'flags' */
146 stream_putw(s
, flags
); /* initial flags */
147 stream_putw(s
, PREFIX_FAMILY(p
));
148 stream_put_prefix(s
, p
);
150 /* label index takes precedence over auto-assigned label. */
151 if (label_index
!= 0) {
152 flags
|= ZEBRA_FEC_REGISTER_LABEL_INDEX
;
153 stream_putl(s
, label_index
);
154 } else if (have_label_to_reg
) {
155 flags
|= ZEBRA_FEC_REGISTER_LABEL
;
156 stream_putl(s
, label
);
158 SET_FLAG(dest
->flags
, BGP_NODE_REGISTERED_FOR_LABEL
);
160 UNSET_FLAG(dest
->flags
, BGP_NODE_REGISTERED_FOR_LABEL
);
162 /* Set length and flags */
163 stream_putw_at(s
, 0, stream_get_endp(s
));
166 * We only need to write new flags if this is a register
169 stream_putw_at(s
, flags_pos
, flags
);
171 zclient_send_message(zclient
);
175 * This is passed as the callback function to bgp_labelpool.c:bgp_lp_get()
176 * by bgp_reg_dereg_for_label() when a label needs to be obtained from
178 * Note that it will reject the allocated label if a label index is found,
179 * because the label index supposes predictable labels
181 int bgp_reg_for_label_callback(mpls_label_t new_label
, void *labelid
,
184 struct bgp_dest
*dest
;
189 * if the route had been removed or the request has gone then reject
190 * the allocated label. The requesting code will have done what is
191 * required to allocate the correct label
193 if (!CHECK_FLAG(dest
->flags
, BGP_NODE_LABEL_REQUESTED
)) {
194 bgp_dest_unlock_node(dest
);
198 bgp_dest_unlock_node(dest
);
200 if (BGP_DEBUG(labelpool
, LABELPOOL
))
201 zlog_debug("%s: FEC %pRN label=%u, allocated=%d", __func__
,
202 bgp_dest_to_rnode(dest
), new_label
, allocated
);
206 * previously-allocated label is now invalid, set to implicit
207 * null until new label arrives
209 if (CHECK_FLAG(dest
->flags
, BGP_NODE_REGISTERED_FOR_LABEL
)) {
210 UNSET_FLAG(dest
->flags
, BGP_NODE_LABEL_REQUESTED
);
211 dest
->local_label
= mpls_lse_encode(
212 MPLS_LABEL_IMPLICIT_NULL
, 0, 0, 1);
213 bgp_set_valid_label(&dest
->local_label
);
217 dest
->local_label
= mpls_lse_encode(new_label
, 0, 0, 1);
218 bgp_set_valid_label(&dest
->local_label
);
221 * Get back to registering the FEC
223 bgp_send_fec_register_label_msg(dest
, true, 0);
228 void bgp_reg_dereg_for_label(struct bgp_dest
*dest
, struct bgp_path_info
*pi
,
231 bool with_label_index
= false;
232 const struct prefix
*p
;
233 bool have_label_to_reg
;
237 mpls_label_t label
= MPLS_INVALID_LABEL
;
239 mpls_lse_decode(dest
->local_label
, &label
, &ttl
, &exp
, &bos
);
241 have_label_to_reg
= bgp_is_valid_label(&dest
->local_label
) &&
242 label
!= MPLS_LABEL_IMPLICIT_NULL
;
244 p
= bgp_dest_get_prefix(dest
);
246 if (BGP_DEBUG(labelpool
, LABELPOOL
))
247 zlog_debug("%s: %pFX: %s ", __func__
, p
,
248 (reg
? "reg" : "dereg"));
253 * Determine if we will let zebra should derive label from
254 * label index instead of bgpd requesting from label pool
256 if (CHECK_FLAG(pi
->attr
->flag
,
257 ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
))
258 && pi
->attr
->label_index
!= BGP_INVALID_LABEL_INDEX
) {
259 with_label_index
= true;
260 UNSET_FLAG(dest
->flags
, BGP_NODE_LABEL_REQUESTED
);
263 * If no label has been registered -- assume any label
264 * from label pool will do. This means that label index
265 * always takes precedence over auto-assigned labels.
267 if (!have_label_to_reg
) {
268 SET_FLAG(dest
->flags
, BGP_NODE_LABEL_REQUESTED
);
269 if (BGP_DEBUG(labelpool
, LABELPOOL
))
271 "%s: Requesting label from LP for %pFX",
273 /* bgp_reg_for_label_callback() will deal with
274 * fec registration when it gets a label from
275 * the pool. This means we'll never register
276 * FECs withoutvalid labels.
278 bgp_lp_get(LP_TYPE_BGP_LU
, dest
,
279 bgp_reg_for_label_callback
);
284 UNSET_FLAG(dest
->flags
, BGP_NODE_LABEL_REQUESTED
);
285 bgp_lp_release(LP_TYPE_BGP_LU
, dest
, label
);
288 bgp_send_fec_register_label_msg(
289 dest
, reg
, with_label_index
? pi
->attr
->label_index
: 0);
292 static int bgp_nlri_get_labels(struct peer
*peer
, uint8_t *pnt
, uint8_t plen
,
296 uint8_t *lim
= pnt
+ plen
;
298 uint8_t label_depth
= 0;
300 if (plen
< BGP_LABEL_BYTES
)
303 for (; data
< lim
; data
+= BGP_LABEL_BYTES
) {
304 memcpy(label
, data
, BGP_LABEL_BYTES
);
305 llen
+= BGP_LABEL_BYTES
;
307 bgp_set_valid_label(label
);
310 if (bgp_is_withdraw_label(label
) || label_bos(label
))
314 /* If we RX multiple labels we will end up keeping only the last
315 * one. We do not yet support a label stack greater than 1. */
317 zlog_info("%pBP rcvd UPDATE with label stack %d deep", peer
,
320 if (!(bgp_is_withdraw_label(label
) || label_bos(label
)))
322 EC_BGP_INVALID_LABEL_STACK
,
323 "%pBP rcvd UPDATE with invalid label stack - no bottom of stack",
329 int bgp_nlri_parse_label(struct peer
*peer
, struct attr
*attr
,
330 struct bgp_nlri
*packet
)
339 bool addpath_capable
;
341 mpls_label_t label
= MPLS_INVALID_LABEL
;
345 lim
= pnt
+ packet
->length
;
350 addpath_capable
= bgp_addpath_encode_rx(peer
, afi
, safi
);
352 for (; pnt
< lim
; pnt
+= psize
) {
353 /* Clear prefix structure. */
354 memset(&p
, 0, sizeof(p
));
356 if (addpath_capable
) {
358 /* When packet overflow occurs return immediately. */
359 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
360 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
362 memcpy(&addpath_id
, pnt
, BGP_ADDPATH_ID_LEN
);
363 addpath_id
= ntohl(addpath_id
);
364 pnt
+= BGP_ADDPATH_ID_LEN
;
367 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
370 /* Fetch prefix length. */
372 p
.family
= afi2family(packet
->afi
);
373 psize
= PSIZE(prefixlen
);
375 /* sanity check against packet data */
376 if ((pnt
+ psize
) > lim
) {
379 "%s [Error] Update packet error / L-U (prefix length %d exceeds packet size %u)",
380 peer
->host
, prefixlen
, (uint
)(lim
- pnt
));
381 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
384 /* Fill in the labels */
385 llen
= bgp_nlri_get_labels(peer
, pnt
, psize
, &label
);
389 "%s [Error] Update packet error (wrong label length 0)",
391 return BGP_NLRI_PARSE_ERROR_LABEL_LENGTH
;
393 p
.prefixlen
= prefixlen
- BSIZE(llen
);
395 /* There needs to be at least one label */
396 if (prefixlen
< 24) {
397 flog_err(EC_BGP_UPDATE_RCV
,
398 "%s [Error] Update packet error (wrong label length %d)",
399 peer
->host
, prefixlen
);
400 return BGP_NLRI_PARSE_ERROR_LABEL_LENGTH
;
403 if ((afi
== AFI_IP
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
404 || (afi
== AFI_IP6
&& p
.prefixlen
> IPV6_MAX_BITLEN
))
405 return BGP_NLRI_PARSE_ERROR_PREFIX_LENGTH
;
407 /* Fetch prefix from NLRI packet */
408 memcpy(&p
.u
.prefix
, pnt
+ llen
, psize
- llen
);
411 if (afi
== AFI_IP
&& safi
== SAFI_LABELED_UNICAST
) {
412 if (IN_CLASSD(ntohl(p
.u
.prefix4
.s_addr
))) {
413 /* From RFC4271 Section 6.3:
415 * If a prefix in the NLRI field is semantically
417 * (e.g., an unexpected multicast IP address),
419 * be logged locally, and the prefix SHOULD be
424 "%s: IPv4 labeled-unicast NLRI is multicast address %pI4, ignoring",
425 peer
->host
, &p
.u
.prefix4
);
431 if (afi
== AFI_IP6
&& safi
== SAFI_LABELED_UNICAST
) {
432 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
)) {
435 "%s: IPv6 labeled-unicast NLRI is link-local address %pI6, ignoring",
436 peer
->host
, &p
.u
.prefix6
);
441 if (IN6_IS_ADDR_MULTICAST(&p
.u
.prefix6
)) {
444 "%s: IPv6 unicast NLRI is multicast address %pI6, ignoring",
445 peer
->host
, &p
.u
.prefix6
);
452 bgp_update(peer
, &p
, addpath_id
, attr
, packet
->afi
,
453 safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
454 NULL
, &label
, 1, 0, NULL
);
456 bgp_withdraw(peer
, &p
, addpath_id
, packet
->afi
,
457 SAFI_UNICAST
, ZEBRA_ROUTE_BGP
,
458 BGP_ROUTE_NORMAL
, NULL
, &label
, 1, NULL
);
462 /* Packet length consistency check. */
466 "%s [Error] Update packet error / L-U (%td data remaining after parsing)",
467 peer
->host
, lim
- pnt
);
468 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
471 return BGP_NLRI_PARSE_OK
;