]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_label.c
Merge pull request #5717 from pguibert6WIND/flowspec_issue_redistribute
[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 char addr[PREFIX_STRLEN];
136
137 pi = labelid;
138 /* Is this path still valid? */
139 if (!bgp_path_info_unlock(pi)) {
140 if (BGP_DEBUG(labelpool, LABELPOOL))
141 zlog_debug(
142 "%s: bgp_path_info is no longer valid, ignoring",
143 __func__);
144 return -1;
145 }
146
147 rn = pi->net;
148 prefix2str(&rn->p, addr, PREFIX_STRLEN);
149
150 if (BGP_DEBUG(labelpool, LABELPOOL))
151 zlog_debug("%s: FEC %s label=%u, allocated=%d", __func__, addr,
152 new_label, allocated);
153
154 if (!allocated) {
155 /*
156 * previously-allocated label is now invalid
157 */
158 if (pi->attr->label_index == MPLS_INVALID_LABEL_INDEX
159 && pi->attr->label != MPLS_LABEL_NONE
160 && CHECK_FLAG(rn->flags, BGP_NODE_REGISTERED_FOR_LABEL)) {
161 bgp_unregister_for_label(rn);
162 label_ntop(MPLS_LABEL_IMPLICIT_NULL, 1,
163 &rn->local_label);
164 bgp_set_valid_label(&rn->local_label);
165 }
166 return 0;
167 }
168
169 /*
170 * label index is assigned, this should be handled by SR-related code,
171 * so retry FEC registration and then reject label allocation for
172 * it to be released to label pool
173 */
174 if (pi->attr->label_index != MPLS_INVALID_LABEL_INDEX) {
175 flog_err(
176 EC_BGP_LABEL,
177 "%s: FEC %s Rejecting allocated label %u as Label Index is %u",
178 __func__, addr, new_label, pi->attr->label_index);
179
180 bgp_register_for_label(pi->net, pi);
181
182 return -1;
183 }
184
185 if (pi->attr->label != MPLS_INVALID_LABEL) {
186 if (new_label == pi->attr->label) {
187 /* already have same label, accept but do nothing */
188 return 0;
189 }
190 /* Shouldn't happen: different label allocation */
191 flog_err(EC_BGP_LABEL,
192 "%s: %s had label %u but got new assignment %u",
193 __func__, addr, pi->attr->label, new_label);
194 /* continue means use new one */
195 }
196
197 label_ntop(new_label, 1, &rn->local_label);
198 bgp_set_valid_label(&rn->local_label);
199
200 /*
201 * Get back to registering the FEC
202 */
203 bgp_register_for_label(pi->net, pi);
204
205 return 0;
206 }
207
208 void bgp_reg_dereg_for_label(struct bgp_node *rn, struct bgp_path_info *pi,
209 bool reg)
210 {
211 bool with_label_index = false;
212 struct stream *s;
213 struct prefix *p;
214 mpls_label_t *local_label;
215 int command;
216 uint16_t flags = 0;
217 size_t flags_pos = 0;
218 char addr[PREFIX_STRLEN];
219
220 p = &(rn->p);
221 local_label = &(rn->local_label);
222 /* this prevents the loop when we're called by
223 * bgp_reg_for_label_callback()
224 */
225 bool have_label_to_reg = bgp_is_valid_label(local_label)
226 && label_pton(local_label) != MPLS_LABEL_IMPLICIT_NULL;
227
228 if (reg) {
229 assert(pi);
230 /*
231 * Determine if we will let zebra should derive label from
232 * label index instead of bgpd requesting from label pool
233 */
234 if (CHECK_FLAG(pi->attr->flag,
235 ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID))
236 && pi->attr->label_index != BGP_INVALID_LABEL_INDEX) {
237 with_label_index = true;
238 } else {
239 /*
240 * If no label index was provided -- assume any label
241 * from label pool will do. This means that label index
242 * always takes precedence over auto-assigned labels.
243 */
244 if (!have_label_to_reg) {
245 if (BGP_DEBUG(labelpool, LABELPOOL)) {
246 prefix2str(p, addr, PREFIX_STRLEN);
247 zlog_debug("%s: Requesting label from LP for %s",
248 __func__, addr);
249 }
250 /* bgp_reg_for_label_callback() will call back
251 * __func__ when it gets a label from the pool.
252 * This means we'll never register FECs without
253 * valid labels.
254 */
255 bgp_lp_get(LP_TYPE_BGP_LU, pi,
256 bgp_reg_for_label_callback);
257 return;
258 }
259 }
260 }
261
262 /* Check socket. */
263 if (!zclient || zclient->sock < 0)
264 return;
265
266 /* If the route node has a local_label assigned or the
267 * path node has an MPLS SR label index allowing zebra to
268 * derive the label, proceed with registration. */
269 s = zclient->obuf;
270 stream_reset(s);
271 command = (reg) ? ZEBRA_FEC_REGISTER : ZEBRA_FEC_UNREGISTER;
272 zclient_create_header(s, command, VRF_DEFAULT);
273 flags_pos = stream_get_endp(s); /* save position of 'flags' */
274 stream_putw(s, flags); /* initial flags */
275 stream_putw(s, PREFIX_FAMILY(p));
276 stream_put_prefix(s, p);
277 if (reg) {
278 if (have_label_to_reg) {
279 flags |= ZEBRA_FEC_REGISTER_LABEL;
280 stream_putl(s, label_pton(local_label));
281 } else if (with_label_index) {
282 flags |= ZEBRA_FEC_REGISTER_LABEL_INDEX;
283 stream_putl(s, pi->attr->label_index);
284 }
285 SET_FLAG(rn->flags, BGP_NODE_REGISTERED_FOR_LABEL);
286 } else
287 UNSET_FLAG(rn->flags, BGP_NODE_REGISTERED_FOR_LABEL);
288
289 /* Set length and flags */
290 stream_putw_at(s, 0, stream_get_endp(s));
291
292 /*
293 * We only need to write new flags if this is a register
294 */
295 if (reg)
296 stream_putw_at(s, flags_pos, flags);
297
298 zclient_send_message(zclient);
299 }
300
301 static int bgp_nlri_get_labels(struct peer *peer, uint8_t *pnt, uint8_t plen,
302 mpls_label_t *label)
303 {
304 uint8_t *data = pnt;
305 uint8_t *lim = pnt + plen;
306 uint8_t llen = 0;
307 uint8_t label_depth = 0;
308
309 for (; data < lim; data += BGP_LABEL_BYTES) {
310 memcpy(label, data, BGP_LABEL_BYTES);
311 llen += BGP_LABEL_BYTES;
312
313 bgp_set_valid_label(label);
314 label_depth += 1;
315
316 if (bgp_is_withdraw_label(label) || label_bos(label))
317 break;
318 }
319
320 /* If we RX multiple labels we will end up keeping only the last
321 * one. We do not yet support a label stack greater than 1. */
322 if (label_depth > 1)
323 zlog_info("%s rcvd UPDATE with label stack %d deep", peer->host,
324 label_depth);
325
326 if (!(bgp_is_withdraw_label(label) || label_bos(label)))
327 flog_warn(
328 EC_BGP_INVALID_LABEL_STACK,
329 "%s rcvd UPDATE with invalid label stack - no bottom of stack",
330 peer->host);
331
332 return llen;
333 }
334
335 int bgp_nlri_parse_label(struct peer *peer, struct attr *attr,
336 struct bgp_nlri *packet)
337 {
338 uint8_t *pnt;
339 uint8_t *lim;
340 struct prefix p;
341 int psize = 0;
342 int prefixlen;
343 afi_t afi;
344 safi_t safi;
345 int addpath_encoded;
346 uint32_t addpath_id;
347 mpls_label_t label = MPLS_INVALID_LABEL;
348 uint8_t llen;
349
350 pnt = packet->nlri;
351 lim = pnt + packet->length;
352 afi = packet->afi;
353 safi = packet->safi;
354 addpath_id = 0;
355
356 addpath_encoded =
357 (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
358 && CHECK_FLAG(peer->af_cap[afi][safi],
359 PEER_CAP_ADDPATH_AF_TX_RCV));
360
361 for (; pnt < lim; pnt += psize) {
362 /* Clear prefix structure. */
363 memset(&p, 0, sizeof(struct prefix));
364
365 if (addpath_encoded) {
366
367 /* When packet overflow occurs return immediately. */
368 if (pnt + BGP_ADDPATH_ID_LEN > lim)
369 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
370
371 memcpy(&addpath_id, pnt, BGP_ADDPATH_ID_LEN);
372 addpath_id = ntohl(addpath_id);
373 pnt += BGP_ADDPATH_ID_LEN;
374 }
375
376 /* Fetch prefix length. */
377 prefixlen = *pnt++;
378 p.family = afi2family(packet->afi);
379 psize = PSIZE(prefixlen);
380
381 /* sanity check against packet data */
382 if ((pnt + psize) > lim) {
383 flog_err(
384 EC_BGP_UPDATE_RCV,
385 "%s [Error] Update packet error / L-U (prefix length %d exceeds packet size %u)",
386 peer->host, prefixlen, (uint)(lim - pnt));
387 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
388 }
389
390 /* Fill in the labels */
391 llen = bgp_nlri_get_labels(peer, pnt, psize, &label);
392 p.prefixlen = prefixlen - BSIZE(llen);
393
394 /* There needs to be at least one label */
395 if (prefixlen < 24) {
396 flog_err(EC_BGP_UPDATE_RCV,
397 "%s [Error] Update packet error"
398 " (wrong label length %d)",
399 peer->host, prefixlen);
400 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
401 BGP_NOTIFY_UPDATE_INVAL_NETWORK);
402 return BGP_NLRI_PARSE_ERROR_LABEL_LENGTH;
403 }
404
405 if ((afi == AFI_IP && p.prefixlen > 32)
406 || (afi == AFI_IP6 && p.prefixlen > 128))
407 return BGP_NLRI_PARSE_ERROR_PREFIX_LENGTH;
408
409 /* Fetch prefix from NLRI packet */
410 memcpy(&p.u.prefix, pnt + llen, psize - llen);
411
412 /* Check address. */
413 if (afi == AFI_IP && safi == SAFI_LABELED_UNICAST) {
414 if (IN_CLASSD(ntohl(p.u.prefix4.s_addr))) {
415 /* From RFC4271 Section 6.3:
416 *
417 * If a prefix in the NLRI field is semantically
418 * incorrect
419 * (e.g., an unexpected multicast IP address),
420 * an error SHOULD
421 * be logged locally, and the prefix SHOULD be
422 * ignored.
423 */
424 flog_err(
425 EC_BGP_UPDATE_RCV,
426 "%s: IPv4 labeled-unicast NLRI is multicast address %s, ignoring",
427 peer->host, inet_ntoa(p.u.prefix4));
428 continue;
429 }
430 }
431
432 /* Check address. */
433 if (afi == AFI_IP6 && safi == SAFI_LABELED_UNICAST) {
434 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6)) {
435 char buf[BUFSIZ];
436
437 flog_err(
438 EC_BGP_UPDATE_RCV,
439 "%s: IPv6 labeled-unicast NLRI is link-local address %s, ignoring",
440 peer->host,
441 inet_ntop(AF_INET6, &p.u.prefix6, buf,
442 BUFSIZ));
443
444 continue;
445 }
446
447 if (IN6_IS_ADDR_MULTICAST(&p.u.prefix6)) {
448 char buf[BUFSIZ];
449
450 flog_err(
451 EC_BGP_UPDATE_RCV,
452 "%s: IPv6 unicast NLRI is multicast address %s, ignoring",
453 peer->host,
454 inet_ntop(AF_INET6, &p.u.prefix6, buf,
455 BUFSIZ));
456
457 continue;
458 }
459 }
460
461 if (attr) {
462 bgp_update(peer, &p, addpath_id, attr, packet->afi,
463 SAFI_UNICAST, ZEBRA_ROUTE_BGP,
464 BGP_ROUTE_NORMAL, NULL, &label, 1, 0, NULL);
465 } else {
466 bgp_withdraw(peer, &p, addpath_id, attr, packet->afi,
467 SAFI_UNICAST, ZEBRA_ROUTE_BGP,
468 BGP_ROUTE_NORMAL, NULL, &label, 1, NULL);
469 }
470 }
471
472 /* Packet length consistency check. */
473 if (pnt != lim) {
474 flog_err(
475 EC_BGP_UPDATE_RCV,
476 "%s [Error] Update packet error / L-U (%zu data remaining after parsing)",
477 peer->host, lim - pnt);
478 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
479 }
480
481 return BGP_NLRI_PARSE_OK;
482 }