]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_label.c
Merge remote-tracking branch 'frr/master' into warnings
[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_info *ri,
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 || !ri || !to)
105 return MPLS_INVALID_LABEL;
106
107 remote_label = ri->extra ? ri->extra->label[0] : MPLS_INVALID_LABEL;
108 from = ri->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 void bgp_reg_dereg_for_label(struct bgp_node *rn, struct bgp_info *ri, int reg)
124 {
125 struct stream *s;
126 struct prefix *p;
127 int command;
128 uint16_t flags = 0;
129 size_t flags_pos = 0;
130
131 /* Check socket. */
132 if (!zclient || zclient->sock < 0)
133 return;
134
135 p = &(rn->p);
136 s = zclient->obuf;
137 stream_reset(s);
138 command = (reg) ? ZEBRA_FEC_REGISTER : ZEBRA_FEC_UNREGISTER;
139 zclient_create_header(s, command, VRF_DEFAULT);
140 flags_pos = stream_get_endp(s); /* save position of 'flags' */
141 stream_putw(s, flags); /* initial flags */
142 stream_putw(s, PREFIX_FAMILY(p));
143 stream_put_prefix(s, p);
144 if (reg) {
145 assert(ri);
146 if (ri->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID)) {
147 if (ri->attr->label_index != BGP_INVALID_LABEL_INDEX) {
148 flags |= ZEBRA_FEC_REGISTER_LABEL_INDEX;
149 stream_putl(s, ri->attr->label_index);
150 }
151 }
152 SET_FLAG(rn->flags, BGP_NODE_REGISTERED_FOR_LABEL);
153 } else
154 UNSET_FLAG(rn->flags, BGP_NODE_REGISTERED_FOR_LABEL);
155
156 /* Set length and flags */
157 stream_putw_at(s, 0, stream_get_endp(s));
158
159 /*
160 * We only need to write new flags if this is a register
161 */
162 if (reg)
163 stream_putw_at(s, flags_pos, flags);
164
165 zclient_send_message(zclient);
166 }
167
168 static int bgp_nlri_get_labels(struct peer *peer, uint8_t *pnt, uint8_t plen,
169 mpls_label_t *label)
170 {
171 uint8_t *data = pnt;
172 uint8_t *lim = pnt + plen;
173 uint8_t llen = 0;
174 uint8_t label_depth = 0;
175
176 for (; data < lim; data += BGP_LABEL_BYTES) {
177 memcpy(label, data, BGP_LABEL_BYTES);
178 llen += BGP_LABEL_BYTES;
179
180 bgp_set_valid_label(label);
181 label_depth += 1;
182
183 if (bgp_is_withdraw_label(label) || label_bos(label))
184 break;
185 }
186
187 /* If we RX multiple labels we will end up keeping only the last
188 * one. We do not yet support a label stack greater than 1. */
189 if (label_depth > 1)
190 zlog_info("%s rcvd UPDATE with label stack %d deep", peer->host,
191 label_depth);
192
193 if (!(bgp_is_withdraw_label(label) || label_bos(label)))
194 flog_warn(
195 BGP_WARN_INVALID_LABEL_STACK,
196 "%s rcvd UPDATE with invalid label stack - no bottom of stack",
197 peer->host);
198
199 return llen;
200 }
201
202 int bgp_nlri_parse_label(struct peer *peer, struct attr *attr,
203 struct bgp_nlri *packet)
204 {
205 uint8_t *pnt;
206 uint8_t *lim;
207 struct prefix p;
208 int psize = 0;
209 int prefixlen;
210 afi_t afi;
211 safi_t safi;
212 int addpath_encoded;
213 uint32_t addpath_id;
214 mpls_label_t label = MPLS_INVALID_LABEL;
215 uint8_t llen;
216
217 pnt = packet->nlri;
218 lim = pnt + packet->length;
219 afi = packet->afi;
220 safi = packet->safi;
221 addpath_id = 0;
222
223 addpath_encoded =
224 (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
225 && CHECK_FLAG(peer->af_cap[afi][safi],
226 PEER_CAP_ADDPATH_AF_TX_RCV));
227
228 for (; pnt < lim; pnt += psize) {
229 /* Clear prefix structure. */
230 memset(&p, 0, sizeof(struct prefix));
231
232 if (addpath_encoded) {
233
234 /* When packet overflow occurs return immediately. */
235 if (pnt + BGP_ADDPATH_ID_LEN > lim)
236 return -1;
237
238 addpath_id = ntohl(*((uint32_t *)pnt));
239 pnt += BGP_ADDPATH_ID_LEN;
240 }
241
242 /* Fetch prefix length. */
243 prefixlen = *pnt++;
244 p.family = afi2family(packet->afi);
245 psize = PSIZE(prefixlen);
246
247 /* sanity check against packet data */
248 if ((pnt + psize) > lim) {
249 flog_err(
250 BGP_ERR_UPDATE_RCV,
251 "%s [Error] Update packet error / L-U (prefix length %d exceeds packet size %u)",
252 peer->host, prefixlen, (uint)(lim - pnt));
253 return -1;
254 }
255
256 /* Fill in the labels */
257 llen = bgp_nlri_get_labels(peer, pnt, psize, &label);
258 p.prefixlen = prefixlen - BSIZE(llen);
259
260 /* There needs to be at least one label */
261 if (prefixlen < 24) {
262 flog_err(BGP_ERR_UPDATE_RCV,
263 "%s [Error] Update packet error"
264 " (wrong label length %d)",
265 peer->host, prefixlen);
266 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
267 BGP_NOTIFY_UPDATE_INVAL_NETWORK);
268 return -1;
269 }
270
271 if ((afi == AFI_IP && p.prefixlen > 32)
272 || (afi == AFI_IP6 && p.prefixlen > 128))
273 return -1;
274
275 /* Fetch prefix from NLRI packet */
276 memcpy(&p.u.prefix, pnt + llen, psize - llen);
277
278 /* Check address. */
279 if (afi == AFI_IP && safi == SAFI_LABELED_UNICAST) {
280 if (IN_CLASSD(ntohl(p.u.prefix4.s_addr))) {
281 /* From RFC4271 Section 6.3:
282 *
283 * If a prefix in the NLRI field is semantically
284 * incorrect
285 * (e.g., an unexpected multicast IP address),
286 * an error SHOULD
287 * be logged locally, and the prefix SHOULD be
288 * ignored.
289 */
290 flog_err(
291 BGP_ERR_UPDATE_RCV,
292 "%s: IPv4 labeled-unicast NLRI is multicast address %s, ignoring",
293 peer->host, inet_ntoa(p.u.prefix4));
294 continue;
295 }
296 }
297
298 /* Check address. */
299 if (afi == AFI_IP6 && safi == SAFI_LABELED_UNICAST) {
300 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6)) {
301 char buf[BUFSIZ];
302
303 flog_err(
304 BGP_ERR_UPDATE_RCV,
305 "%s: IPv6 labeled-unicast NLRI is link-local address %s, ignoring",
306 peer->host,
307 inet_ntop(AF_INET6, &p.u.prefix6, buf,
308 BUFSIZ));
309
310 continue;
311 }
312
313 if (IN6_IS_ADDR_MULTICAST(&p.u.prefix6)) {
314 char buf[BUFSIZ];
315
316 flog_err(
317 BGP_ERR_UPDATE_RCV,
318 "%s: IPv6 unicast NLRI is multicast address %s, ignoring",
319 peer->host,
320 inet_ntop(AF_INET6, &p.u.prefix6, buf,
321 BUFSIZ));
322
323 continue;
324 }
325 }
326
327 if (attr) {
328 bgp_update(peer, &p, addpath_id, attr, packet->afi,
329 SAFI_UNICAST, ZEBRA_ROUTE_BGP,
330 BGP_ROUTE_NORMAL, NULL, &label, 1, 0, NULL);
331 } else {
332 bgp_withdraw(peer, &p, addpath_id, attr, packet->afi,
333 SAFI_UNICAST, ZEBRA_ROUTE_BGP,
334 BGP_ROUTE_NORMAL, NULL, &label, 1, NULL);
335 }
336 }
337
338 /* Packet length consistency check. */
339 if (pnt != lim) {
340 flog_err(
341 BGP_ERR_UPDATE_RCV,
342 "%s [Error] Update packet error / L-U (%zu data remaining after parsing)",
343 peer->host, lim - pnt);
344 return -1;
345 }
346
347 return 0;
348 }