]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_label.c
Merge pull request #6577 from rtrlib/2020-06-12-master-fixes
[mirror_frr.git] / bgpd / bgp_label.c
1 /* BGP carrying label information
2 * Copyright (C) 2013 Cumulus Networks, Inc.
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "command.h"
24 #include "thread.h"
25 #include "prefix.h"
26 #include "zclient.h"
27 #include "stream.h"
28 #include "network.h"
29 #include "log.h"
30 #include "memory.h"
31 #include "nexthop.h"
32 #include "mpls.h"
33
34 #include "bgpd/bgpd.h"
35 #include "bgpd/bgp_table.h"
36 #include "bgpd/bgp_route.h"
37 #include "bgpd/bgp_attr.h"
38 #include "bgpd/bgp_label.h"
39 #include "bgpd/bgp_packet.h"
40 #include "bgpd/bgp_debug.h"
41 #include "bgpd/bgp_errors.h"
42
43 extern struct zclient *zclient;
44
45 int bgp_parse_fec_update(void)
46 {
47 struct stream *s;
48 struct bgp_node *rn;
49 struct bgp *bgp;
50 struct bgp_table *table;
51 struct prefix p;
52 uint32_t label;
53 afi_t afi;
54 safi_t safi;
55
56 s = zclient->ibuf;
57
58 memset(&p, 0, sizeof(struct prefix));
59 p.family = stream_getw(s);
60 p.prefixlen = stream_getc(s);
61 stream_get(p.u.val, s, PSIZE(p.prefixlen));
62 label = stream_getl(s);
63
64 /* hack for the bgp instance & SAFI = have to send/receive it */
65 afi = family2afi(p.family);
66 safi = SAFI_UNICAST;
67 bgp = bgp_get_default();
68 if (!bgp) {
69 zlog_debug("no default bgp instance");
70 return -1;
71 }
72
73 table = bgp->rib[afi][safi];
74 if (!table) {
75 zlog_debug("no %u unicast table", p.family);
76 return -1;
77 }
78 rn = bgp_node_lookup(table, &p);
79 if (!rn) {
80 zlog_debug("no node for the prefix");
81 return -1;
82 }
83
84 /* treat it as implicit withdraw - the label is invalid */
85 if (label == MPLS_INVALID_LABEL)
86 bgp_unset_valid_label(&rn->local_label);
87 else {
88 label_ntop(label, 1, &rn->local_label);
89 bgp_set_valid_label(&rn->local_label);
90 }
91 SET_FLAG(rn->flags, BGP_NODE_LABEL_CHANGED);
92 bgp_unlock_node(rn);
93 bgp_process(bgp, rn, afi, safi);
94 return 1;
95 }
96
97 mpls_label_t bgp_adv_label(struct bgp_node *rn, struct bgp_path_info *pi,
98 struct peer *to, afi_t afi, safi_t safi)
99 {
100 struct peer *from;
101 mpls_label_t remote_label;
102 int reflect;
103
104 if (!rn || !pi || !to)
105 return MPLS_INVALID_LABEL;
106
107 remote_label = pi->extra ? pi->extra->label[0] : MPLS_INVALID_LABEL;
108 from = pi->peer;
109 reflect =
110 ((from->sort == BGP_PEER_IBGP) && (to->sort == BGP_PEER_IBGP));
111
112 if (reflect
113 && !CHECK_FLAG(to->af_flags[afi][safi],
114 PEER_FLAG_FORCE_NEXTHOP_SELF))
115 return remote_label;
116
117 if (CHECK_FLAG(to->af_flags[afi][safi], PEER_FLAG_NEXTHOP_UNCHANGED))
118 return remote_label;
119
120 return rn->local_label;
121 }
122
123 /**
124 * This is passed as the callback function to bgp_labelpool.c:bgp_lp_get()
125 * by bgp_reg_dereg_for_label() when a label needs to be obtained from
126 * label pool.
127 * Note that it will reject the allocated label if a label index is found,
128 * because the label index supposes predictable labels
129 */
130 int bgp_reg_for_label_callback(mpls_label_t new_label, void *labelid,
131 bool allocated)
132 {
133 struct bgp_path_info *pi;
134 struct bgp_node *rn;
135
136 pi = labelid;
137 /* Is this path still valid? */
138 if (!bgp_path_info_unlock(pi)) {
139 if (BGP_DEBUG(labelpool, LABELPOOL))
140 zlog_debug(
141 "%s: bgp_path_info is no longer valid, ignoring",
142 __func__);
143 return -1;
144 }
145
146 rn = pi->net;
147
148 if (BGP_DEBUG(labelpool, LABELPOOL))
149 zlog_debug("%s: FEC %pRN label=%u, allocated=%d", __func__, rn,
150 new_label, allocated);
151
152 if (!allocated) {
153 /*
154 * previously-allocated label is now invalid
155 */
156 if (pi->attr->label_index == MPLS_INVALID_LABEL_INDEX
157 && pi->attr->label != MPLS_LABEL_NONE
158 && CHECK_FLAG(rn->flags, BGP_NODE_REGISTERED_FOR_LABEL)) {
159 bgp_unregister_for_label(rn);
160 label_ntop(MPLS_LABEL_IMPLICIT_NULL, 1,
161 &rn->local_label);
162 bgp_set_valid_label(&rn->local_label);
163 }
164 return 0;
165 }
166
167 /*
168 * label index is assigned, this should be handled by SR-related code,
169 * so retry FEC registration and then reject label allocation for
170 * it to be released to label pool
171 */
172 if (pi->attr->label_index != MPLS_INVALID_LABEL_INDEX) {
173 flog_err(
174 EC_BGP_LABEL,
175 "%s: FEC %pRN Rejecting allocated label %u as Label Index is %u",
176 __func__, rn, new_label, pi->attr->label_index);
177
178 bgp_register_for_label(pi->net, pi);
179
180 return -1;
181 }
182
183 if (pi->attr->label != MPLS_INVALID_LABEL) {
184 if (new_label == pi->attr->label) {
185 /* already have same label, accept but do nothing */
186 return 0;
187 }
188 /* Shouldn't happen: different label allocation */
189 flog_err(EC_BGP_LABEL,
190 "%s: %pRN had label %u but got new assignment %u",
191 __func__, rn, pi->attr->label, new_label);
192 /* continue means use new one */
193 }
194
195 label_ntop(new_label, 1, &rn->local_label);
196 bgp_set_valid_label(&rn->local_label);
197
198 /*
199 * Get back to registering the FEC
200 */
201 bgp_register_for_label(pi->net, pi);
202
203 return 0;
204 }
205
206 void bgp_reg_dereg_for_label(struct bgp_node *rn, struct bgp_path_info *pi,
207 bool reg)
208 {
209 bool with_label_index = false;
210 struct stream *s;
211 const struct prefix *p;
212 mpls_label_t *local_label;
213 int command;
214 uint16_t flags = 0;
215 size_t flags_pos = 0;
216 char addr[PREFIX_STRLEN];
217
218 p = bgp_node_get_prefix(rn);
219 local_label = &(rn->local_label);
220 /* this prevents the loop when we're called by
221 * bgp_reg_for_label_callback()
222 */
223 bool have_label_to_reg = bgp_is_valid_label(local_label)
224 && label_pton(local_label) != MPLS_LABEL_IMPLICIT_NULL;
225
226 if (reg) {
227 assert(pi);
228 /*
229 * Determine if we will let zebra should derive label from
230 * label index instead of bgpd requesting from label pool
231 */
232 if (CHECK_FLAG(pi->attr->flag,
233 ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID))
234 && pi->attr->label_index != BGP_INVALID_LABEL_INDEX) {
235 with_label_index = true;
236 } else {
237 /*
238 * If no label index was provided -- assume any label
239 * from label pool will do. This means that label index
240 * always takes precedence over auto-assigned labels.
241 */
242 if (!have_label_to_reg) {
243 if (BGP_DEBUG(labelpool, LABELPOOL)) {
244 prefix2str(p, addr, PREFIX_STRLEN);
245 zlog_debug("%s: Requesting label from LP for %s",
246 __func__, addr);
247 }
248 /* bgp_reg_for_label_callback() will call back
249 * __func__ when it gets a label from the pool.
250 * This means we'll never register FECs without
251 * valid labels.
252 */
253 bgp_lp_get(LP_TYPE_BGP_LU, pi,
254 bgp_reg_for_label_callback);
255 return;
256 }
257 }
258 }
259
260 /* Check socket. */
261 if (!zclient || zclient->sock < 0)
262 return;
263
264 /* If the route node has a local_label assigned or the
265 * path node has an MPLS SR label index allowing zebra to
266 * derive the label, proceed with registration. */
267 s = zclient->obuf;
268 stream_reset(s);
269 command = (reg) ? ZEBRA_FEC_REGISTER : ZEBRA_FEC_UNREGISTER;
270 zclient_create_header(s, command, VRF_DEFAULT);
271 flags_pos = stream_get_endp(s); /* save position of 'flags' */
272 stream_putw(s, flags); /* initial flags */
273 stream_putw(s, PREFIX_FAMILY(p));
274 stream_put_prefix(s, p);
275 if (reg) {
276 if (have_label_to_reg) {
277 flags |= ZEBRA_FEC_REGISTER_LABEL;
278 stream_putl(s, label_pton(local_label));
279 } else if (with_label_index) {
280 flags |= ZEBRA_FEC_REGISTER_LABEL_INDEX;
281 stream_putl(s, pi->attr->label_index);
282 }
283 SET_FLAG(rn->flags, BGP_NODE_REGISTERED_FOR_LABEL);
284 } else
285 UNSET_FLAG(rn->flags, BGP_NODE_REGISTERED_FOR_LABEL);
286
287 /* Set length and flags */
288 stream_putw_at(s, 0, stream_get_endp(s));
289
290 /*
291 * We only need to write new flags if this is a register
292 */
293 if (reg)
294 stream_putw_at(s, flags_pos, flags);
295
296 zclient_send_message(zclient);
297 }
298
299 static int bgp_nlri_get_labels(struct peer *peer, uint8_t *pnt, uint8_t plen,
300 mpls_label_t *label)
301 {
302 uint8_t *data = pnt;
303 uint8_t *lim = pnt + plen;
304 uint8_t llen = 0;
305 uint8_t label_depth = 0;
306
307 for (; data < lim; data += BGP_LABEL_BYTES) {
308 memcpy(label, data, BGP_LABEL_BYTES);
309 llen += BGP_LABEL_BYTES;
310
311 bgp_set_valid_label(label);
312 label_depth += 1;
313
314 if (bgp_is_withdraw_label(label) || label_bos(label))
315 break;
316 }
317
318 /* If we RX multiple labels we will end up keeping only the last
319 * one. We do not yet support a label stack greater than 1. */
320 if (label_depth > 1)
321 zlog_info("%s rcvd UPDATE with label stack %d deep", peer->host,
322 label_depth);
323
324 if (!(bgp_is_withdraw_label(label) || label_bos(label)))
325 flog_warn(
326 EC_BGP_INVALID_LABEL_STACK,
327 "%s rcvd UPDATE with invalid label stack - no bottom of stack",
328 peer->host);
329
330 return llen;
331 }
332
333 int bgp_nlri_parse_label(struct peer *peer, struct attr *attr,
334 struct bgp_nlri *packet)
335 {
336 uint8_t *pnt;
337 uint8_t *lim;
338 struct prefix p;
339 int psize = 0;
340 int prefixlen;
341 afi_t afi;
342 safi_t safi;
343 int addpath_encoded;
344 uint32_t addpath_id;
345 mpls_label_t label = MPLS_INVALID_LABEL;
346 uint8_t llen;
347
348 pnt = packet->nlri;
349 lim = pnt + packet->length;
350 afi = packet->afi;
351 safi = packet->safi;
352 addpath_id = 0;
353
354 addpath_encoded =
355 (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
356 && CHECK_FLAG(peer->af_cap[afi][safi],
357 PEER_CAP_ADDPATH_AF_TX_RCV));
358
359 for (; pnt < lim; pnt += psize) {
360 /* Clear prefix structure. */
361 memset(&p, 0, sizeof(struct prefix));
362
363 if (addpath_encoded) {
364
365 /* When packet overflow occurs return immediately. */
366 if (pnt + BGP_ADDPATH_ID_LEN > lim)
367 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
368
369 memcpy(&addpath_id, pnt, BGP_ADDPATH_ID_LEN);
370 addpath_id = ntohl(addpath_id);
371 pnt += BGP_ADDPATH_ID_LEN;
372 }
373
374 /* Fetch prefix length. */
375 prefixlen = *pnt++;
376 p.family = afi2family(packet->afi);
377 psize = PSIZE(prefixlen);
378
379 /* sanity check against packet data */
380 if ((pnt + psize) > lim) {
381 flog_err(
382 EC_BGP_UPDATE_RCV,
383 "%s [Error] Update packet error / L-U (prefix length %d exceeds packet size %u)",
384 peer->host, prefixlen, (uint)(lim - pnt));
385 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
386 }
387
388 /* Fill in the labels */
389 llen = bgp_nlri_get_labels(peer, pnt, psize, &label);
390 p.prefixlen = prefixlen - BSIZE(llen);
391
392 /* There needs to be at least one label */
393 if (prefixlen < 24) {
394 flog_err(EC_BGP_UPDATE_RCV,
395 "%s [Error] Update packet error"
396 " (wrong label length %d)",
397 peer->host, prefixlen);
398 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
399 BGP_NOTIFY_UPDATE_INVAL_NETWORK);
400 return BGP_NLRI_PARSE_ERROR_LABEL_LENGTH;
401 }
402
403 if ((afi == AFI_IP && p.prefixlen > 32)
404 || (afi == AFI_IP6 && p.prefixlen > 128))
405 return BGP_NLRI_PARSE_ERROR_PREFIX_LENGTH;
406
407 /* Fetch prefix from NLRI packet */
408 memcpy(&p.u.prefix, pnt + llen, psize - llen);
409
410 /* Check address. */
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:
414 *
415 * If a prefix in the NLRI field is semantically
416 * incorrect
417 * (e.g., an unexpected multicast IP address),
418 * an error SHOULD
419 * be logged locally, and the prefix SHOULD be
420 * ignored.
421 */
422 flog_err(
423 EC_BGP_UPDATE_RCV,
424 "%s: IPv4 labeled-unicast NLRI is multicast address %s, ignoring",
425 peer->host, inet_ntoa(p.u.prefix4));
426 continue;
427 }
428 }
429
430 /* Check address. */
431 if (afi == AFI_IP6 && safi == SAFI_LABELED_UNICAST) {
432 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6)) {
433 char buf[BUFSIZ];
434
435 flog_err(
436 EC_BGP_UPDATE_RCV,
437 "%s: IPv6 labeled-unicast NLRI is link-local address %s, ignoring",
438 peer->host,
439 inet_ntop(AF_INET6, &p.u.prefix6, buf,
440 BUFSIZ));
441
442 continue;
443 }
444
445 if (IN6_IS_ADDR_MULTICAST(&p.u.prefix6)) {
446 char buf[BUFSIZ];
447
448 flog_err(
449 EC_BGP_UPDATE_RCV,
450 "%s: IPv6 unicast NLRI is multicast address %s, ignoring",
451 peer->host,
452 inet_ntop(AF_INET6, &p.u.prefix6, buf,
453 BUFSIZ));
454
455 continue;
456 }
457 }
458
459 if (attr) {
460 bgp_update(peer, &p, addpath_id, attr, packet->afi,
461 SAFI_UNICAST, ZEBRA_ROUTE_BGP,
462 BGP_ROUTE_NORMAL, NULL, &label, 1, 0, NULL);
463 } else {
464 bgp_withdraw(peer, &p, addpath_id, attr, packet->afi,
465 SAFI_UNICAST, ZEBRA_ROUTE_BGP,
466 BGP_ROUTE_NORMAL, NULL, &label, 1, NULL);
467 }
468 }
469
470 /* Packet length consistency check. */
471 if (pnt != lim) {
472 flog_err(
473 EC_BGP_UPDATE_RCV,
474 "%s [Error] Update packet error / L-U (%td data remaining after parsing)",
475 peer->host, lim - pnt);
476 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
477 }
478
479 return BGP_NLRI_PARSE_OK;
480 }