]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_label.c
Merge pull request #12795 from pguibert6WIND/vpnv6_nexthop_encoding
[mirror_frr.git] / bgpd / bgp_label.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* BGP carrying label information
3 * Copyright (C) 2013 Cumulus Networks, Inc.
4 */
5
6 #include <zebra.h>
7
8 #include "command.h"
9 #include "thread.h"
10 #include "prefix.h"
11 #include "zclient.h"
12 #include "stream.h"
13 #include "network.h"
14 #include "log.h"
15 #include "memory.h"
16 #include "nexthop.h"
17 #include "mpls.h"
18
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"
27
28 extern struct zclient *zclient;
29
30 int bgp_parse_fec_update(void)
31 {
32 struct stream *s;
33 struct bgp_dest *dest;
34 struct bgp *bgp;
35 struct bgp_table *table;
36 struct prefix p;
37 uint32_t label;
38 afi_t afi;
39 safi_t safi;
40
41 s = zclient->ibuf;
42
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);
48
49 /* hack for the bgp instance & SAFI = have to send/receive it */
50 afi = family2afi(p.family);
51 safi = SAFI_UNICAST;
52 bgp = bgp_get_default();
53 if (!bgp) {
54 zlog_debug("no default bgp instance");
55 return -1;
56 }
57
58 table = bgp->rib[afi][safi];
59 if (!table) {
60 zlog_debug("no %u unicast table", p.family);
61 return -1;
62 }
63 dest = bgp_node_lookup(table, &p);
64 if (!dest) {
65 zlog_debug("no node for the prefix");
66 return -1;
67 }
68
69 /* treat it as implicit withdraw - the label is invalid */
70 if (label == MPLS_INVALID_LABEL)
71 bgp_unset_valid_label(&dest->local_label);
72 else {
73 dest->local_label = mpls_lse_encode(label, 0, 0, 1);
74 bgp_set_valid_label(&dest->local_label);
75 }
76 SET_FLAG(dest->flags, BGP_NODE_LABEL_CHANGED);
77 bgp_process(bgp, dest, afi, safi);
78 bgp_dest_unlock_node(dest);
79 return 1;
80 }
81
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)
84 {
85 struct peer *from;
86 mpls_label_t remote_label;
87 int reflect;
88
89 if (!dest || !pi || !to)
90 return MPLS_INVALID_LABEL;
91
92 remote_label = pi->extra ? pi->extra->label[0] : MPLS_INVALID_LABEL;
93 from = pi->peer;
94 reflect =
95 ((from->sort == BGP_PEER_IBGP) && (to->sort == BGP_PEER_IBGP));
96
97 if (reflect
98 && !CHECK_FLAG(to->af_flags[afi][safi],
99 PEER_FLAG_FORCE_NEXTHOP_SELF))
100 return remote_label;
101
102 if (CHECK_FLAG(to->af_flags[afi][safi], PEER_FLAG_NEXTHOP_UNCHANGED))
103 return remote_label;
104
105 return dest->local_label;
106 }
107
108 static void bgp_send_fec_register_label_msg(struct bgp_dest *dest, bool reg,
109 uint32_t label_index)
110 {
111 struct stream *s;
112 int command;
113 const struct prefix *p;
114 uint16_t flags = 0;
115 size_t flags_pos = 0;
116 mpls_label_t *local_label = &(dest->local_label);
117 uint32_t ttl = 0;
118 uint32_t bos = 0;
119 uint32_t exp = 0;
120 mpls_label_t label = MPLS_INVALID_LABEL;
121 bool have_label_to_reg;
122
123 mpls_lse_decode(*local_label, &label, &ttl, &exp, &bos);
124
125 have_label_to_reg = bgp_is_valid_label(local_label) &&
126 label != MPLS_LABEL_IMPLICIT_NULL;
127
128 p = bgp_dest_get_prefix(dest);
129
130 /* Check socket. */
131 if (!zclient || zclient->sock < 0)
132 return;
133
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),
137 label_index, label);
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. */
141 s = zclient->obuf;
142 stream_reset(s);
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);
149 if (reg) {
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);
157 }
158 SET_FLAG(dest->flags, BGP_NODE_REGISTERED_FOR_LABEL);
159 } else
160 UNSET_FLAG(dest->flags, BGP_NODE_REGISTERED_FOR_LABEL);
161
162 /* Set length and flags */
163 stream_putw_at(s, 0, stream_get_endp(s));
164
165 /*
166 * We only need to write new flags if this is a register
167 */
168 if (reg)
169 stream_putw_at(s, flags_pos, flags);
170
171 zclient_send_message(zclient);
172 }
173
174 /**
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
177 * label pool.
178 * Note that it will reject the allocated label if a label index is found,
179 * because the label index supposes predictable labels
180 */
181 int bgp_reg_for_label_callback(mpls_label_t new_label, void *labelid,
182 bool allocated)
183 {
184 struct bgp_dest *dest;
185
186 dest = labelid;
187
188 /*
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
192 */
193 if (!CHECK_FLAG(dest->flags, BGP_NODE_LABEL_REQUESTED)) {
194 bgp_dest_unlock_node(dest);
195 return -1;
196 }
197
198 bgp_dest_unlock_node(dest);
199
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);
203
204 if (!allocated) {
205 /*
206 * previously-allocated label is now invalid, set to implicit
207 * null until new label arrives
208 */
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);
214 }
215 }
216
217 dest->local_label = mpls_lse_encode(new_label, 0, 0, 1);
218 bgp_set_valid_label(&dest->local_label);
219
220 /*
221 * Get back to registering the FEC
222 */
223 bgp_send_fec_register_label_msg(dest, true, 0);
224
225 return 0;
226 }
227
228 void bgp_reg_dereg_for_label(struct bgp_dest *dest, struct bgp_path_info *pi,
229 bool reg)
230 {
231 bool with_label_index = false;
232 const struct prefix *p;
233 bool have_label_to_reg;
234 uint32_t ttl = 0;
235 uint32_t bos = 0;
236 uint32_t exp = 0;
237 mpls_label_t label = MPLS_INVALID_LABEL;
238
239 mpls_lse_decode(dest->local_label, &label, &ttl, &exp, &bos);
240
241 have_label_to_reg = bgp_is_valid_label(&dest->local_label) &&
242 label != MPLS_LABEL_IMPLICIT_NULL;
243
244 p = bgp_dest_get_prefix(dest);
245
246 if (BGP_DEBUG(labelpool, LABELPOOL))
247 zlog_debug("%s: %pFX: %s ", __func__, p,
248 (reg ? "reg" : "dereg"));
249
250 if (reg) {
251 assert(pi);
252 /*
253 * Determine if we will let zebra should derive label from
254 * label index instead of bgpd requesting from label pool
255 */
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);
261 } else {
262 /*
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.
266 */
267 if (!have_label_to_reg) {
268 SET_FLAG(dest->flags, BGP_NODE_LABEL_REQUESTED);
269 if (BGP_DEBUG(labelpool, LABELPOOL))
270 zlog_debug(
271 "%s: Requesting label from LP for %pFX",
272 __func__, p);
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.
277 */
278 bgp_lp_get(LP_TYPE_BGP_LU, dest,
279 bgp_reg_for_label_callback);
280 return;
281 }
282 }
283 } else {
284 UNSET_FLAG(dest->flags, BGP_NODE_LABEL_REQUESTED);
285 bgp_lp_release(LP_TYPE_BGP_LU, dest, label);
286 }
287
288 bgp_send_fec_register_label_msg(
289 dest, reg, with_label_index ? pi->attr->label_index : 0);
290 }
291
292 static int bgp_nlri_get_labels(struct peer *peer, uint8_t *pnt, uint8_t plen,
293 mpls_label_t *label)
294 {
295 uint8_t *data = pnt;
296 uint8_t *lim = pnt + plen;
297 uint8_t llen = 0;
298 uint8_t label_depth = 0;
299
300 for (; data < lim; data += BGP_LABEL_BYTES) {
301 memcpy(label, data, BGP_LABEL_BYTES);
302 llen += BGP_LABEL_BYTES;
303
304 bgp_set_valid_label(label);
305 label_depth += 1;
306
307 if (bgp_is_withdraw_label(label) || label_bos(label))
308 break;
309 }
310
311 /* If we RX multiple labels we will end up keeping only the last
312 * one. We do not yet support a label stack greater than 1. */
313 if (label_depth > 1)
314 zlog_info("%pBP rcvd UPDATE with label stack %d deep", peer,
315 label_depth);
316
317 if (!(bgp_is_withdraw_label(label) || label_bos(label)))
318 flog_warn(
319 EC_BGP_INVALID_LABEL_STACK,
320 "%pBP rcvd UPDATE with invalid label stack - no bottom of stack",
321 peer);
322
323 return llen;
324 }
325
326 int bgp_nlri_parse_label(struct peer *peer, struct attr *attr,
327 struct bgp_nlri *packet)
328 {
329 uint8_t *pnt;
330 uint8_t *lim;
331 struct prefix p;
332 int psize = 0;
333 int prefixlen;
334 afi_t afi;
335 safi_t safi;
336 bool addpath_capable;
337 uint32_t addpath_id;
338 mpls_label_t label = MPLS_INVALID_LABEL;
339 uint8_t llen;
340
341 pnt = packet->nlri;
342 lim = pnt + packet->length;
343 afi = packet->afi;
344 safi = packet->safi;
345 addpath_id = 0;
346
347 addpath_capable = bgp_addpath_encode_rx(peer, afi, safi);
348
349 for (; pnt < lim; pnt += psize) {
350 /* Clear prefix structure. */
351 memset(&p, 0, sizeof(p));
352
353 if (addpath_capable) {
354
355 /* When packet overflow occurs return immediately. */
356 if (pnt + BGP_ADDPATH_ID_LEN > lim)
357 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
358
359 memcpy(&addpath_id, pnt, BGP_ADDPATH_ID_LEN);
360 addpath_id = ntohl(addpath_id);
361 pnt += BGP_ADDPATH_ID_LEN;
362 }
363
364 /* Fetch prefix length. */
365 prefixlen = *pnt++;
366 p.family = afi2family(packet->afi);
367 psize = PSIZE(prefixlen);
368
369 /* sanity check against packet data */
370 if ((pnt + psize) > lim) {
371 flog_err(
372 EC_BGP_UPDATE_RCV,
373 "%s [Error] Update packet error / L-U (prefix length %d exceeds packet size %u)",
374 peer->host, prefixlen, (uint)(lim - pnt));
375 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
376 }
377
378 /* Fill in the labels */
379 llen = bgp_nlri_get_labels(peer, pnt, psize, &label);
380 p.prefixlen = prefixlen - BSIZE(llen);
381
382 /* There needs to be at least one label */
383 if (prefixlen < 24) {
384 flog_err(EC_BGP_UPDATE_RCV,
385 "%s [Error] Update packet error (wrong label length %d)",
386 peer->host, prefixlen);
387 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
388 BGP_NOTIFY_UPDATE_INVAL_NETWORK);
389 return BGP_NLRI_PARSE_ERROR_LABEL_LENGTH;
390 }
391
392 if ((afi == AFI_IP && p.prefixlen > IPV4_MAX_BITLEN)
393 || (afi == AFI_IP6 && p.prefixlen > IPV6_MAX_BITLEN))
394 return BGP_NLRI_PARSE_ERROR_PREFIX_LENGTH;
395
396 /* Fetch prefix from NLRI packet */
397 memcpy(&p.u.prefix, pnt + llen, psize - llen);
398
399 /* Check address. */
400 if (afi == AFI_IP && safi == SAFI_LABELED_UNICAST) {
401 if (IN_CLASSD(ntohl(p.u.prefix4.s_addr))) {
402 /* From RFC4271 Section 6.3:
403 *
404 * If a prefix in the NLRI field is semantically
405 * incorrect
406 * (e.g., an unexpected multicast IP address),
407 * an error SHOULD
408 * be logged locally, and the prefix SHOULD be
409 * ignored.
410 */
411 flog_err(
412 EC_BGP_UPDATE_RCV,
413 "%s: IPv4 labeled-unicast NLRI is multicast address %pI4, ignoring",
414 peer->host, &p.u.prefix4);
415 continue;
416 }
417 }
418
419 /* Check address. */
420 if (afi == AFI_IP6 && safi == SAFI_LABELED_UNICAST) {
421 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6)) {
422 flog_err(
423 EC_BGP_UPDATE_RCV,
424 "%s: IPv6 labeled-unicast NLRI is link-local address %pI6, ignoring",
425 peer->host, &p.u.prefix6);
426
427 continue;
428 }
429
430 if (IN6_IS_ADDR_MULTICAST(&p.u.prefix6)) {
431 flog_err(
432 EC_BGP_UPDATE_RCV,
433 "%s: IPv6 unicast NLRI is multicast address %pI6, ignoring",
434 peer->host, &p.u.prefix6);
435
436 continue;
437 }
438 }
439
440 if (attr) {
441 bgp_update(peer, &p, addpath_id, attr, packet->afi,
442 safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
443 NULL, &label, 1, 0, NULL);
444 } else {
445 bgp_withdraw(peer, &p, addpath_id, attr, packet->afi,
446 SAFI_UNICAST, ZEBRA_ROUTE_BGP,
447 BGP_ROUTE_NORMAL, NULL, &label, 1, NULL);
448 }
449 }
450
451 /* Packet length consistency check. */
452 if (pnt != lim) {
453 flog_err(
454 EC_BGP_UPDATE_RCV,
455 "%s [Error] Update packet error / L-U (%td data remaining after parsing)",
456 peer->host, lim - pnt);
457 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
458 }
459
460 return BGP_NLRI_PARSE_OK;
461 }