]>
Commit | Line | Data |
---|---|---|
cd1964ff DS |
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 | * | |
896014f4 DL |
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 | |
cd1964ff DS |
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 | ||
42 | extern struct zclient *zclient; | |
43 | ||
44 | int | |
45 | 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 | u_int32_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.prefix, 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_LABELED_UNICAST; | |
67 | bgp = bgp_get_default(); | |
68 | if (!bgp) | |
69 | { | |
70 | zlog_debug("no default bgp instance"); | |
71 | return -1; | |
72 | } | |
73 | ||
74 | table = bgp->rib[afi][safi]; | |
75 | if (!table) | |
76 | { | |
77 | zlog_debug("no %u labeled-unicast table", p.family); | |
78 | return -1; | |
79 | } | |
80 | rn = bgp_node_lookup(table, &p); | |
81 | if (!rn) | |
82 | { | |
83 | zlog_debug("no node for the prefix"); | |
84 | return -1; | |
85 | } | |
86 | ||
87 | /* treat it as implicit withdraw - the label is invalid */ | |
88 | if (label == MPLS_INVALID_LABEL) | |
89 | bgp_unset_valid_label(rn->local_label); | |
90 | else | |
91 | { | |
92 | label_ntop(label, 1, rn->local_label); | |
93 | bgp_set_valid_label(rn->local_label); | |
94 | } | |
95 | SET_FLAG(rn->flags, BGP_NODE_LABEL_CHANGED); | |
96 | bgp_unlock_node (rn); | |
97 | bgp_process (bgp, rn, afi, safi); | |
98 | return 1; | |
99 | } | |
100 | ||
101 | u_char * | |
102 | bgp_adv_label (struct bgp_node *rn, struct bgp_info *ri, struct peer *to, | |
103 | afi_t afi, safi_t safi) | |
104 | { | |
105 | struct peer *from; | |
106 | u_char *remote_label; | |
107 | int reflect; | |
108 | ||
109 | if (!rn || !ri || !to) | |
110 | return NULL; | |
111 | ||
112 | remote_label = ri->extra ? ri->extra->tag : NULL; | |
113 | from = ri->peer; | |
114 | reflect = ((from->sort == BGP_PEER_IBGP) && (to->sort == BGP_PEER_IBGP)); | |
115 | ||
116 | if (reflect && !CHECK_FLAG(to->af_flags[afi][safi], | |
117 | PEER_FLAG_FORCE_NEXTHOP_SELF)) | |
118 | return remote_label; | |
119 | ||
120 | if (CHECK_FLAG(to->af_flags[afi][safi], PEER_FLAG_NEXTHOP_UNCHANGED)) | |
121 | return remote_label; | |
122 | ||
123 | return rn->local_label; | |
124 | } | |
125 | ||
126 | void | |
28d58fd7 VV |
127 | bgp_reg_dereg_for_label (struct bgp_node *rn, struct bgp_info *ri, |
128 | int reg) | |
cd1964ff DS |
129 | { |
130 | struct stream *s; | |
131 | struct prefix *p; | |
132 | int command; | |
28d58fd7 VV |
133 | u_int16_t flags = 0; |
134 | size_t flags_pos = 0; | |
cd1964ff DS |
135 | |
136 | /* Check socket. */ | |
137 | if (!zclient || zclient->sock < 0) | |
138 | return; | |
139 | ||
140 | p = &(rn->p); | |
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); | |
28d58fd7 VV |
145 | flags_pos = stream_get_endp (s); /* save position of 'flags' */ |
146 | stream_putw(s, flags); /* initial flags */ | |
cd1964ff DS |
147 | stream_putw(s, PREFIX_FAMILY(p)); |
148 | stream_put_prefix(s, p); | |
6cf48acc | 149 | if (reg) |
28d58fd7 VV |
150 | { |
151 | assert (ri); | |
c5a543b4 | 152 | if (ri->attr->flag & ATTR_FLAG_BIT (BGP_ATTR_PREFIX_SID)) |
28d58fd7 VV |
153 | { |
154 | assert (ri->attr->extra); | |
c5a543b4 DW |
155 | |
156 | if (ri->attr->extra->label_index != BGP_INVALID_LABEL_INDEX) | |
157 | { | |
158 | flags |= ZEBRA_FEC_REGISTER_LABEL_INDEX; | |
159 | stream_putl (s, ri->attr->extra->label_index); | |
160 | } | |
28d58fd7 VV |
161 | } |
162 | SET_FLAG (rn->flags, BGP_NODE_REGISTERED_FOR_LABEL); | |
163 | } | |
6cf48acc VV |
164 | else |
165 | UNSET_FLAG (rn->flags, BGP_NODE_REGISTERED_FOR_LABEL); | |
cd1964ff | 166 | |
28d58fd7 VV |
167 | /* Set length and flags */ |
168 | stream_putw_at (s, 0, stream_get_endp (s)); | |
169 | stream_putw_at (s, flags_pos, flags); | |
170 | ||
171 | zclient_send_message(zclient); | |
cd1964ff DS |
172 | } |
173 | ||
174 | static int | |
175 | bgp_nlri_get_labels (struct peer *peer, u_char *pnt, u_char plen, | |
176 | u_char label[]) | |
177 | { | |
178 | u_char *data = pnt; | |
179 | u_char *lim = pnt + plen; | |
180 | u_char llen = 0; | |
181 | ||
182 | for (; data < lim; data += BGP_LABEL_BYTES) | |
183 | { | |
184 | memcpy(label, data, BGP_LABEL_BYTES); | |
185 | llen += 3; | |
186 | if (bgp_is_withdraw_label(label) || label_bos(label)) | |
187 | break; | |
188 | } | |
189 | if (!(bgp_is_withdraw_label(label) || label_bos(label))) | |
190 | zlog_warn("%s: [Update:RCVD] invalid label - no bottom of stack", | |
191 | peer->host); | |
192 | ||
193 | return llen; | |
194 | } | |
195 | ||
196 | int | |
197 | bgp_nlri_parse_label (struct peer *peer, struct attr *attr, | |
198 | struct bgp_nlri *packet) | |
199 | { | |
200 | u_char *pnt; | |
201 | u_char *lim; | |
202 | struct prefix p; | |
203 | int psize = 0; | |
204 | int prefixlen; | |
205 | afi_t afi; | |
206 | safi_t safi; | |
207 | int addpath_encoded; | |
208 | u_int32_t addpath_id; | |
209 | u_char label[3]; | |
210 | u_char llen; | |
211 | ||
212 | /* Check peer status. */ | |
213 | if (peer->status != Established) | |
214 | return 0; | |
215 | ||
216 | pnt = packet->nlri; | |
217 | lim = pnt + packet->length; | |
218 | afi = packet->afi; | |
219 | safi = packet->safi; | |
220 | addpath_id = 0; | |
221 | ||
222 | addpath_encoded = (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV) && | |
223 | CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_TX_RCV)); | |
224 | ||
225 | for (; pnt < lim; pnt += psize) | |
226 | { | |
227 | /* Clear prefix structure. */ | |
228 | memset (&p, 0, sizeof (struct prefix)); | |
229 | llen = 0; | |
230 | ||
231 | if (addpath_encoded) | |
232 | { | |
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 | { | |
250 | zlog_err ("%s [Error] Update packet error / L-U (prefix length %d exceeds packet size %u)", | |
251 | peer->host, | |
252 | 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 | // zlog_debug("rcvd label [%x/%x/%x], llen=%d\n", label[0], label[1], label[2], llen); | |
aab1814c | 259 | p.prefixlen = prefixlen - BSIZE(llen); |
cd1964ff DS |
260 | |
261 | /* There needs to be at least one label */ | |
262 | if (prefixlen < 24) | |
263 | { | |
264 | zlog_err ("%s [Error] Update packet error" | |
265 | " (wrong label length %d)", | |
266 | peer->host, prefixlen); | |
267 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, | |
268 | BGP_NOTIFY_UPDATE_INVAL_NETWORK); | |
269 | return -1; | |
270 | } | |
271 | ||
272 | if ((afi == AFI_IP && p.prefixlen > 32) | |
273 | || (afi == AFI_IP6 && p.prefixlen > 128)) | |
274 | return -1; | |
275 | ||
276 | /* Fetch prefix from NLRI packet */ | |
277 | memcpy (&p.u.prefix, pnt + llen, psize - llen); | |
278 | ||
279 | /* Check address. */ | |
280 | if (afi == AFI_IP && safi == SAFI_LABELED_UNICAST) | |
281 | { | |
282 | if (IN_CLASSD (ntohl (p.u.prefix4.s_addr))) | |
283 | { | |
284 | /* From RFC4271 Section 6.3: | |
285 | * | |
286 | * If a prefix in the NLRI field is semantically incorrect | |
287 | * (e.g., an unexpected multicast IP address), an error SHOULD | |
288 | * be logged locally, and the prefix SHOULD be ignored. | |
289 | */ | |
290 | zlog_err ("%s: IPv4 labeled-unicast NLRI is multicast address %s, ignoring", | |
291 | peer->host, inet_ntoa (p.u.prefix4)); | |
292 | continue; | |
293 | } | |
294 | } | |
295 | ||
296 | /* Check address. */ | |
297 | if (afi == AFI_IP6 && safi == SAFI_LABELED_UNICAST) | |
298 | { | |
299 | if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6)) | |
300 | { | |
301 | char buf[BUFSIZ]; | |
302 | ||
303 | zlog_err ("%s: IPv6 labeled-unicast NLRI is link-local address %s, ignoring", | |
304 | peer->host, inet_ntop (AF_INET6, &p.u.prefix6, buf, BUFSIZ)); | |
305 | ||
306 | continue; | |
307 | } | |
308 | ||
309 | if (IN6_IS_ADDR_MULTICAST (&p.u.prefix6)) | |
310 | { | |
311 | char buf[BUFSIZ]; | |
312 | ||
313 | zlog_err ("%s: IPv6 unicast NLRI is multicast address %s, ignoring", | |
314 | peer->host, inet_ntop (AF_INET6, &p.u.prefix6, buf, BUFSIZ)); | |
315 | ||
316 | continue; | |
317 | } | |
318 | } | |
319 | ||
320 | if (attr) | |
321 | { | |
322 | bgp_update (peer, &p, addpath_id, attr, packet->afi, SAFI_LABELED_UNICAST, | |
323 | ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, NULL, label, 0, NULL); | |
324 | } | |
325 | else | |
326 | { | |
327 | bgp_withdraw (peer, &p, addpath_id, attr, packet->afi, SAFI_LABELED_UNICAST, | |
328 | ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, NULL, label, NULL); | |
329 | } | |
330 | } | |
331 | ||
332 | /* Packet length consistency check. */ | |
333 | if (pnt != lim) | |
334 | { | |
335 | zlog_err ("%s [Error] Update packet error / L-U (%zu data remaining after parsing)", | |
336 | peer->host, lim - pnt); | |
337 | return -1; | |
338 | } | |
339 | ||
340 | return 0; | |
341 | } |