]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: ISC |
8429abe0 RW |
2 | /* $OpenBSD$ */ |
3 | ||
4 | /* | |
5 | * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org> | |
6 | * Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org> | |
7 | * Copyright (c) 2004 Esben Norby <norby@openbsd.org> | |
8 | * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> | |
8429abe0 RW |
9 | */ |
10 | ||
eac6e3f0 | 11 | #include <zebra.h> |
8429abe0 RW |
12 | |
13 | #include "ldp.h" | |
14 | #include "ldpd.h" | |
15 | #include "ldpe.h" | |
16 | #include "log.h" | |
17 | #include "lde.h" | |
eac6e3f0 | 18 | #include "ldp_debug.h" |
077d336a | 19 | #include "rlfa.h" |
8429abe0 | 20 | |
eac6e3f0 RW |
21 | #include <lib/log.h> |
22 | #include "memory.h" | |
23 | #include "privs.h" | |
24 | #include "sigevent.h" | |
25 | #include "mpls.h" | |
fea12efb | 26 | #include <lib/linklist.h> |
27 | #include "zclient.h" | |
28 | #include "stream.h" | |
29 | #include "network.h" | |
689f5a8c | 30 | #include "libfrr.h" |
eac6e3f0 RW |
31 | |
32 | static void lde_shutdown(void); | |
cc9f21da DS |
33 | static void lde_dispatch_imsg(struct thread *thread); |
34 | static void lde_dispatch_parent(struct thread *thread); | |
45926e58 RZ |
35 | static __inline int lde_nbr_compare(const struct lde_nbr *, |
36 | const struct lde_nbr *); | |
8429abe0 RW |
37 | static struct lde_nbr *lde_nbr_new(uint32_t, struct lde_nbr *); |
38 | static void lde_nbr_del(struct lde_nbr *); | |
39 | static struct lde_nbr *lde_nbr_find(uint32_t); | |
40 | static void lde_nbr_clear(void); | |
41 | static void lde_nbr_addr_update(struct lde_nbr *, | |
42 | struct lde_addr *, int); | |
45926e58 RZ |
43 | static __inline int lde_map_compare(const struct lde_map *, |
44 | const struct lde_map *); | |
8429abe0 RW |
45 | static void lde_map_free(void *); |
46 | static int lde_address_add(struct lde_nbr *, struct lde_addr *); | |
47 | static int lde_address_del(struct lde_nbr *, struct lde_addr *); | |
48 | static void lde_address_list_free(struct lde_nbr *); | |
9d694b0b | 49 | static void zclient_sync_init(void); |
fea12efb | 50 | static void lde_label_list_init(void); |
5afba51d | 51 | static int lde_get_label_chunk(void); |
fea12efb | 52 | static void on_get_label_chunk_response(uint32_t start, uint32_t end); |
53 | static uint32_t lde_get_next_label(void); | |
aff1743c KS |
54 | static bool lde_fec_connected(const struct fec_node *); |
55 | static bool lde_fec_outside_mpls_network(const struct fec_node *); | |
2d1aa1e8 | 56 | static void lde_check_filter_af(int, struct ldpd_af_conf *, |
57 | const char *); | |
8429abe0 RW |
58 | |
59 | RB_GENERATE(nbr_tree, lde_nbr, entry, lde_nbr_compare) | |
d3e1887a | 60 | RB_GENERATE(lde_map_head, lde_map, entry, lde_map_compare) |
8429abe0 RW |
61 | |
62 | struct ldpd_conf *ldeconf; | |
63 | struct nbr_tree lde_nbrs = RB_INITIALIZER(&lde_nbrs); | |
64 | ||
65 | static struct imsgev *iev_ldpe; | |
59c5b83b | 66 | static struct imsgev iev_main_sync_data; |
28e8294c | 67 | static struct imsgev *iev_main, *iev_main_sync; |
8429abe0 | 68 | |
eac6e3f0 RW |
69 | /* lde privileges */ |
70 | static zebra_capabilities_t _caps_p [] = | |
8429abe0 | 71 | { |
342213ea | 72 | ZCAP_NET_ADMIN |
eac6e3f0 | 73 | }; |
8429abe0 | 74 | |
eac6e3f0 RW |
75 | static struct zebra_privs_t lde_privs = |
76 | { | |
eac6e3f0 RW |
77 | #if defined(VTY_GROUP) |
78 | .vty_group = VTY_GROUP, | |
79 | #endif | |
80 | .caps_p = _caps_p, | |
81 | .cap_num_p = array_size(_caps_p), | |
82 | .cap_num_i = 0 | |
83 | }; | |
84 | ||
fea12efb | 85 | /* List of chunks of labels externally assigned by Zebra */ |
5afba51d RW |
86 | static struct list *label_chunk_list; |
87 | static struct listnode *current_label_chunk; | |
88 | ||
89 | /* Synchronous zclient to request labels */ | |
90 | static struct zclient *zclient_sync; | |
fea12efb | 91 | |
eac6e3f0 RW |
92 | /* SIGINT / SIGTERM handler. */ |
93 | static void | |
94 | sigint(void) | |
95 | { | |
96 | lde_shutdown(); | |
8429abe0 RW |
97 | } |
98 | ||
7cc91e67 | 99 | static struct frr_signal_t lde_signals[] = |
eac6e3f0 | 100 | { |
1e7e440f RW |
101 | { |
102 | .signal = SIGHUP, | |
103 | /* ignore */ | |
104 | }, | |
eac6e3f0 RW |
105 | { |
106 | .signal = SIGINT, | |
107 | .handler = &sigint, | |
108 | }, | |
109 | { | |
110 | .signal = SIGTERM, | |
111 | .handler = &sigint, | |
112 | }, | |
113 | }; | |
114 | ||
8429abe0 RW |
115 | /* label decision engine */ |
116 | void | |
274f5abf | 117 | lde(void) |
8429abe0 | 118 | { |
eac6e3f0 | 119 | #ifdef HAVE_SETPROCTITLE |
8429abe0 | 120 | setproctitle("label decision engine"); |
eac6e3f0 | 121 | #endif |
8429abe0 | 122 | ldpd_process = PROC_LDE_ENGINE; |
fa68f9da | 123 | log_procname = log_procnames[PROC_LDE_ENGINE]; |
8429abe0 | 124 | |
f9a4d683 | 125 | master = frr_init(); |
adf8924d DL |
126 | /* no frr_config_fork() here, allow frr_pthread to create threads */ |
127 | frr_is_after_fork = true; | |
8429abe0 RW |
128 | |
129 | /* setup signal handler */ | |
eac6e3f0 | 130 | signal_init(master, array_size(lde_signals), lde_signals); |
8429abe0 | 131 | |
28e8294c RW |
132 | /* setup pipes and event handlers to the parent process */ |
133 | if ((iev_main = calloc(1, sizeof(struct imsgev))) == NULL) | |
8429abe0 | 134 | fatal(NULL); |
28e8294c | 135 | imsg_init(&iev_main->ibuf, LDPD_FD_ASYNC); |
eac6e3f0 | 136 | iev_main->handler_read = lde_dispatch_parent; |
66e78ae6 QY |
137 | thread_add_read(master, iev_main->handler_read, iev_main, iev_main->ibuf.fd, |
138 | &iev_main->ev_read); | |
eac6e3f0 | 139 | iev_main->handler_write = ldp_write_handler; |
28e8294c | 140 | |
59c5b83b DS |
141 | memset(&iev_main_sync_data, 0, sizeof(iev_main_sync_data)); |
142 | iev_main_sync = &iev_main_sync_data; | |
28e8294c | 143 | imsg_init(&iev_main_sync->ibuf, LDPD_FD_SYNC); |
eac6e3f0 | 144 | |
274f5abf RW |
145 | /* create base configuration */ |
146 | ldeconf = config_new_empty(); | |
147 | ||
f9a4d683 | 148 | struct thread thread; |
274f5abf RW |
149 | while (thread_fetch(master, &thread)) |
150 | thread_call(&thread); | |
f9a4d683 KS |
151 | |
152 | /* NOTREACHED */ | |
153 | return; | |
274f5abf RW |
154 | } |
155 | ||
156 | void | |
157 | lde_init(struct ldpd_init *init) | |
158 | { | |
159 | /* drop privileges */ | |
af7e63a3 RW |
160 | lde_privs.user = init->user; |
161 | lde_privs.group = init->group; | |
37a1f2fb | 162 | zprivs_preinit(&lde_privs); |
274f5abf RW |
163 | zprivs_init(&lde_privs); |
164 | ||
eac6e3f0 | 165 | /* start the LIB garbage collector */ |
8429abe0 RW |
166 | lde_gc_start_timer(); |
167 | ||
fea12efb | 168 | /* Init synchronous zclient and label list */ |
689f5a8c DL |
169 | frr_zclient_addr(&zclient_addr, &zclient_addr_len, |
170 | init->zclient_serv_path); | |
9d694b0b | 171 | zclient_sync_init(); |
8429abe0 RW |
172 | } |
173 | ||
eac6e3f0 | 174 | static void |
8429abe0 RW |
175 | lde_shutdown(void) |
176 | { | |
177 | /* close pipes */ | |
835a7376 RW |
178 | if (iev_ldpe) { |
179 | msgbuf_clear(&iev_ldpe->ibuf.w); | |
180 | close(iev_ldpe->ibuf.fd); | |
50732983 | 181 | iev_ldpe->ibuf.fd = -1; |
835a7376 | 182 | } |
8429abe0 RW |
183 | msgbuf_clear(&iev_main->ibuf.w); |
184 | close(iev_main->ibuf.fd); | |
50732983 | 185 | iev_main->ibuf.fd = -1; |
28e8294c RW |
186 | msgbuf_clear(&iev_main_sync->ibuf.w); |
187 | close(iev_main_sync->ibuf.fd); | |
50732983 | 188 | iev_main_sync->ibuf.fd = -1; |
8429abe0 RW |
189 | |
190 | lde_gc_stop_timer(); | |
191 | lde_nbr_clear(); | |
192 | fec_tree_clear(); | |
193 | ||
194 | config_clear(ldeconf); | |
195 | ||
835a7376 RW |
196 | if (iev_ldpe) |
197 | free(iev_ldpe); | |
8429abe0 RW |
198 | free(iev_main); |
199 | ||
200 | log_info("label decision engine exiting"); | |
b12a63f1 MS |
201 | |
202 | zlog_fini(); | |
8429abe0 RW |
203 | exit(0); |
204 | } | |
205 | ||
206 | /* imesg */ | |
eac6e3f0 | 207 | int |
8429abe0 RW |
208 | lde_imsg_compose_parent(int type, pid_t pid, void *data, uint16_t datalen) |
209 | { | |
50732983 RW |
210 | if (iev_main->ibuf.fd == -1) |
211 | return (0); | |
8429abe0 RW |
212 | return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen)); |
213 | } | |
214 | ||
f2232fdf RW |
215 | void |
216 | lde_imsg_compose_parent_sync(int type, pid_t pid, void *data, uint16_t datalen) | |
217 | { | |
50732983 RW |
218 | if (iev_main_sync->ibuf.fd == -1) |
219 | return; | |
f2232fdf RW |
220 | imsg_compose_event(iev_main_sync, type, 0, pid, -1, data, datalen); |
221 | imsg_flush(&iev_main_sync->ibuf); | |
222 | } | |
223 | ||
8429abe0 RW |
224 | int |
225 | lde_imsg_compose_ldpe(int type, uint32_t peerid, pid_t pid, void *data, | |
226 | uint16_t datalen) | |
227 | { | |
50732983 RW |
228 | if (iev_ldpe->ibuf.fd == -1) |
229 | return (0); | |
8429abe0 RW |
230 | return (imsg_compose_event(iev_ldpe, type, peerid, pid, |
231 | -1, data, datalen)); | |
232 | } | |
233 | ||
234 | /* ARGSUSED */ | |
cc9f21da | 235 | static void lde_dispatch_imsg(struct thread *thread) |
8429abe0 | 236 | { |
eac6e3f0 | 237 | struct imsgev *iev = THREAD_ARG(thread); |
8429abe0 RW |
238 | struct imsgbuf *ibuf = &iev->ibuf; |
239 | struct imsg imsg; | |
240 | struct lde_nbr *ln; | |
236c6935 RW |
241 | struct map *map; |
242 | struct lde_addr *lde_addr; | |
243 | struct notify_msg *nm; | |
8429abe0 | 244 | ssize_t n; |
eac6e3f0 | 245 | int shut = 0; |
8429abe0 | 246 | |
eac6e3f0 RW |
247 | iev->ev_read = NULL; |
248 | ||
249 | if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) | |
250 | fatal("imsg_read error"); | |
251 | if (n == 0) /* connection closed */ | |
252 | shut = 1; | |
8429abe0 RW |
253 | |
254 | for (;;) { | |
255 | if ((n = imsg_get(ibuf, &imsg)) == -1) | |
256 | fatal("lde_dispatch_imsg: imsg_get error"); | |
257 | if (n == 0) | |
258 | break; | |
259 | ||
260 | switch (imsg.hdr.type) { | |
261 | case IMSG_LABEL_MAPPING_FULL: | |
262 | ln = lde_nbr_find(imsg.hdr.peerid); | |
263 | if (ln == NULL) { | |
264 | log_debug("%s: cannot find lde neighbor", | |
265 | __func__); | |
266 | break; | |
267 | } | |
268 | ||
269 | fec_snap(ln); | |
270 | break; | |
271 | case IMSG_LABEL_MAPPING: | |
272 | case IMSG_LABEL_REQUEST: | |
273 | case IMSG_LABEL_RELEASE: | |
274 | case IMSG_LABEL_WITHDRAW: | |
275 | case IMSG_LABEL_ABORT: | |
236c6935 RW |
276 | if (imsg.hdr.len - IMSG_HEADER_SIZE != |
277 | sizeof(struct map)) | |
8429abe0 | 278 | fatalx("lde_dispatch_imsg: wrong imsg len"); |
236c6935 | 279 | map = imsg.data; |
8429abe0 RW |
280 | |
281 | ln = lde_nbr_find(imsg.hdr.peerid); | |
282 | if (ln == NULL) { | |
283 | log_debug("%s: cannot find lde neighbor", | |
284 | __func__); | |
285 | break; | |
286 | } | |
287 | ||
288 | switch (imsg.hdr.type) { | |
289 | case IMSG_LABEL_MAPPING: | |
4272a064 | 290 | lde_check_mapping(map, ln, 1); |
8429abe0 RW |
291 | break; |
292 | case IMSG_LABEL_REQUEST: | |
236c6935 | 293 | lde_check_request(map, ln); |
8429abe0 RW |
294 | break; |
295 | case IMSG_LABEL_RELEASE: | |
236c6935 | 296 | lde_check_release(map, ln); |
8429abe0 RW |
297 | break; |
298 | case IMSG_LABEL_WITHDRAW: | |
236c6935 | 299 | lde_check_withdraw(map, ln); |
8429abe0 RW |
300 | break; |
301 | case IMSG_LABEL_ABORT: | |
302 | /* not necessary */ | |
303 | break; | |
304 | } | |
305 | break; | |
306 | case IMSG_ADDRESS_ADD: | |
236c6935 RW |
307 | if (imsg.hdr.len - IMSG_HEADER_SIZE != |
308 | sizeof(struct lde_addr)) | |
8429abe0 | 309 | fatalx("lde_dispatch_imsg: wrong imsg len"); |
236c6935 | 310 | lde_addr = imsg.data; |
8429abe0 RW |
311 | |
312 | ln = lde_nbr_find(imsg.hdr.peerid); | |
313 | if (ln == NULL) { | |
314 | log_debug("%s: cannot find lde neighbor", | |
315 | __func__); | |
316 | break; | |
317 | } | |
236c6935 | 318 | if (lde_address_add(ln, lde_addr) < 0) { |
3efd0893 | 319 | log_debug("%s: cannot add address %s, it already exists", __func__, |
236c6935 | 320 | log_addr(lde_addr->af, &lde_addr->addr)); |
8429abe0 RW |
321 | } |
322 | break; | |
323 | case IMSG_ADDRESS_DEL: | |
236c6935 RW |
324 | if (imsg.hdr.len - IMSG_HEADER_SIZE != |
325 | sizeof(struct lde_addr)) | |
8429abe0 | 326 | fatalx("lde_dispatch_imsg: wrong imsg len"); |
236c6935 | 327 | lde_addr = imsg.data; |
8429abe0 RW |
328 | |
329 | ln = lde_nbr_find(imsg.hdr.peerid); | |
330 | if (ln == NULL) { | |
331 | log_debug("%s: cannot find lde neighbor", | |
332 | __func__); | |
333 | break; | |
334 | } | |
236c6935 | 335 | if (lde_address_del(ln, lde_addr) < 0) { |
3efd0893 | 336 | log_debug("%s: cannot delete address %s, it does not exist", __func__, |
236c6935 | 337 | log_addr(lde_addr->af, &lde_addr->addr)); |
8429abe0 RW |
338 | } |
339 | break; | |
340 | case IMSG_NOTIFICATION: | |
236c6935 RW |
341 | if (imsg.hdr.len - IMSG_HEADER_SIZE != |
342 | sizeof(struct notify_msg)) | |
8429abe0 | 343 | fatalx("lde_dispatch_imsg: wrong imsg len"); |
236c6935 | 344 | nm = imsg.data; |
8429abe0 RW |
345 | |
346 | ln = lde_nbr_find(imsg.hdr.peerid); | |
347 | if (ln == NULL) { | |
348 | log_debug("%s: cannot find lde neighbor", | |
349 | __func__); | |
350 | break; | |
351 | } | |
352 | ||
236c6935 | 353 | switch (nm->status_code) { |
8429abe0 | 354 | case S_PW_STATUS: |
236c6935 | 355 | l2vpn_recv_pw_status(ln, nm); |
8429abe0 | 356 | break; |
257799cd RW |
357 | case S_ENDOFLIB: |
358 | /* | |
359 | * Do nothing for now. Should be useful in | |
360 | * the future when we implement LDP-IGP | |
361 | * Synchronization (RFC 5443) and Graceful | |
362 | * Restart (RFC 3478). | |
363 | */ | |
8429abe0 RW |
364 | default: |
365 | break; | |
366 | } | |
367 | break; | |
368 | case IMSG_NEIGHBOR_UP: | |
369 | if (imsg.hdr.len - IMSG_HEADER_SIZE != | |
370 | sizeof(struct lde_nbr)) | |
371 | fatalx("lde_dispatch_imsg: wrong imsg len"); | |
372 | ||
373 | if (lde_nbr_find(imsg.hdr.peerid)) | |
3efd0893 | 374 | fatalx("lde_dispatch_imsg: neighbor already exists"); |
8429abe0 RW |
375 | lde_nbr_new(imsg.hdr.peerid, imsg.data); |
376 | break; | |
377 | case IMSG_NEIGHBOR_DOWN: | |
378 | lde_nbr_del(lde_nbr_find(imsg.hdr.peerid)); | |
379 | break; | |
380 | case IMSG_CTL_SHOW_LIB: | |
381 | rt_dump(imsg.hdr.pid); | |
382 | ||
383 | lde_imsg_compose_ldpe(IMSG_CTL_END, 0, | |
384 | imsg.hdr.pid, NULL, 0); | |
385 | break; | |
386 | case IMSG_CTL_SHOW_L2VPN_PW: | |
387 | l2vpn_pw_ctl(imsg.hdr.pid); | |
388 | ||
389 | lde_imsg_compose_ldpe(IMSG_CTL_END, 0, | |
390 | imsg.hdr.pid, NULL, 0); | |
391 | break; | |
392 | case IMSG_CTL_SHOW_L2VPN_BINDING: | |
393 | l2vpn_binding_ctl(imsg.hdr.pid); | |
394 | ||
395 | lde_imsg_compose_ldpe(IMSG_CTL_END, 0, | |
396 | imsg.hdr.pid, NULL, 0); | |
397 | break; | |
8429abe0 RW |
398 | default: |
399 | log_debug("%s: unexpected imsg %d", __func__, | |
400 | imsg.hdr.type); | |
401 | break; | |
402 | } | |
403 | imsg_free(&imsg); | |
404 | } | |
405 | if (!shut) | |
406 | imsg_event_add(iev); | |
407 | else { | |
eac6e3f0 | 408 | /* this pipe is dead, so remove the event handlers and exit */ |
2783a692 DS |
409 | THREAD_OFF(iev->ev_read); |
410 | THREAD_OFF(iev->ev_write); | |
eac6e3f0 | 411 | lde_shutdown(); |
8429abe0 RW |
412 | } |
413 | } | |
414 | ||
415 | /* ARGSUSED */ | |
cc9f21da | 416 | static void lde_dispatch_parent(struct thread *thread) |
8429abe0 RW |
417 | { |
418 | static struct ldpd_conf *nconf; | |
52b530fc | 419 | struct iface *iface, *niface; |
8429abe0 RW |
420 | struct tnbr *ntnbr; |
421 | struct nbr_params *nnbrp; | |
52b530fc RW |
422 | static struct l2vpn *l2vpn, *nl2vpn; |
423 | struct l2vpn_if *lif, *nlif; | |
424 | struct l2vpn_pw *pw, *npw; | |
8429abe0 | 425 | struct imsg imsg; |
52b530fc | 426 | struct kif *kif; |
236c6935 | 427 | struct kroute *kr; |
11bf8e13 | 428 | int fd; |
eac6e3f0 | 429 | struct imsgev *iev = THREAD_ARG(thread); |
8429abe0 RW |
430 | struct imsgbuf *ibuf = &iev->ibuf; |
431 | ssize_t n; | |
432 | int shut = 0; | |
433 | struct fec fec; | |
2d1aa1e8 | 434 | struct ldp_access *laccess; |
077d336a RW |
435 | struct ldp_rlfa_node *rnode, *rntmp; |
436 | struct ldp_rlfa_client *rclient; | |
437 | struct zapi_rlfa_request *rlfa_req; | |
438 | struct zapi_rlfa_igp *rlfa_igp; | |
8429abe0 | 439 | |
eac6e3f0 RW |
440 | iev->ev_read = NULL; |
441 | ||
442 | if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) | |
443 | fatal("imsg_read error"); | |
444 | if (n == 0) /* connection closed */ | |
445 | shut = 1; | |
8429abe0 RW |
446 | |
447 | for (;;) { | |
448 | if ((n = imsg_get(ibuf, &imsg)) == -1) | |
449 | fatal("lde_dispatch_parent: imsg_get error"); | |
450 | if (n == 0) | |
451 | break; | |
452 | ||
453 | switch (imsg.hdr.type) { | |
52b530fc RW |
454 | case IMSG_IFSTATUS: |
455 | if (imsg.hdr.len != IMSG_HEADER_SIZE + | |
456 | sizeof(struct kif)) | |
457 | fatalx("IFSTATUS imsg with wrong len"); | |
458 | kif = imsg.data; | |
459 | ||
460 | iface = if_lookup_name(ldeconf, kif->ifname); | |
461 | if (iface) { | |
462 | if_update_info(iface, kif); | |
95535717 | 463 | |
464 | /* if up see if any labels need to be updated */ | |
465 | if (kif->operative) | |
466 | lde_route_update(iface, AF_UNSPEC); | |
52b530fc RW |
467 | break; |
468 | } | |
469 | ||
470 | RB_FOREACH(l2vpn, l2vpn_head, &ldeconf->l2vpn_tree) { | |
471 | lif = l2vpn_if_find(l2vpn, kif->ifname); | |
472 | if (lif) { | |
473 | l2vpn_if_update_info(lif, kif); | |
474 | break; | |
475 | } | |
476 | pw = l2vpn_pw_find(l2vpn, kif->ifname); | |
477 | if (pw) { | |
478 | l2vpn_pw_update_info(pw, kif); | |
479 | break; | |
480 | } | |
481 | } | |
482 | break; | |
87b5f1b7 RW |
483 | case IMSG_PW_UPDATE: |
484 | if (imsg.hdr.len != IMSG_HEADER_SIZE + | |
485 | sizeof(struct zapi_pw_status)) | |
486 | fatalx("PW_UPDATE imsg with wrong len"); | |
487 | ||
488 | if (l2vpn_pw_status_update(imsg.data) != 0) | |
489 | log_warnx("%s: error updating PW status", | |
490 | __func__); | |
491 | break; | |
8429abe0 | 492 | case IMSG_NETWORK_ADD: |
8cb1fc45 | 493 | case IMSG_NETWORK_UPDATE: |
236c6935 RW |
494 | if (imsg.hdr.len != IMSG_HEADER_SIZE + |
495 | sizeof(struct kroute)) { | |
8429abe0 RW |
496 | log_warnx("%s: wrong imsg len", __func__); |
497 | break; | |
498 | } | |
236c6935 | 499 | kr = imsg.data; |
8429abe0 | 500 | |
236c6935 | 501 | switch (kr->af) { |
8429abe0 RW |
502 | case AF_INET: |
503 | fec.type = FEC_TYPE_IPV4; | |
236c6935 RW |
504 | fec.u.ipv4.prefix = kr->prefix.v4; |
505 | fec.u.ipv4.prefixlen = kr->prefixlen; | |
8429abe0 RW |
506 | break; |
507 | case AF_INET6: | |
508 | fec.type = FEC_TYPE_IPV6; | |
236c6935 RW |
509 | fec.u.ipv6.prefix = kr->prefix.v6; |
510 | fec.u.ipv6.prefixlen = kr->prefixlen; | |
8429abe0 RW |
511 | break; |
512 | default: | |
513 | fatalx("lde_dispatch_parent: unknown af"); | |
514 | } | |
515 | ||
516 | switch (imsg.hdr.type) { | |
517 | case IMSG_NETWORK_ADD: | |
236c6935 | 518 | lde_kernel_insert(&fec, kr->af, &kr->nexthop, |
e132dea0 RW |
519 | kr->ifindex, kr->route_type, |
520 | kr->route_instance, | |
236c6935 | 521 | kr->flags & F_CONNECTED, NULL); |
8429abe0 | 522 | break; |
8cb1fc45 RW |
523 | case IMSG_NETWORK_UPDATE: |
524 | lde_kernel_update(&fec); | |
8429abe0 RW |
525 | break; |
526 | } | |
527 | break; | |
528 | case IMSG_SOCKET_IPC: | |
529 | if (iev_ldpe) { | |
3efd0893 | 530 | log_warnx("%s: received unexpected imsg fd to ldpe", __func__); |
8429abe0 RW |
531 | break; |
532 | } | |
533 | if ((fd = imsg.fd) == -1) { | |
3efd0893 | 534 | log_warnx("%s: expected to receive imsg fd to ldpe but didn't receive any", __func__); |
8429abe0 RW |
535 | break; |
536 | } | |
537 | ||
538 | if ((iev_ldpe = malloc(sizeof(struct imsgev))) == NULL) | |
539 | fatal(NULL); | |
540 | imsg_init(&iev_ldpe->ibuf, fd); | |
eac6e3f0 | 541 | iev_ldpe->handler_read = lde_dispatch_imsg; |
66e78ae6 QY |
542 | thread_add_read(master, iev_ldpe->handler_read, iev_ldpe, iev_ldpe->ibuf.fd, |
543 | &iev_ldpe->ev_read); | |
eac6e3f0 RW |
544 | iev_ldpe->handler_write = ldp_write_handler; |
545 | iev_ldpe->ev_write = NULL; | |
8429abe0 | 546 | break; |
274f5abf RW |
547 | case IMSG_INIT: |
548 | if (imsg.hdr.len != IMSG_HEADER_SIZE + | |
549 | sizeof(struct ldpd_init)) | |
550 | fatalx("INIT imsg with wrong len"); | |
551 | ||
552 | memcpy(&init, imsg.data, sizeof(init)); | |
553 | lde_init(&init); | |
554 | break; | |
f9a4d683 KS |
555 | case IMSG_AGENTX_ENABLED: |
556 | ldp_agentx_enabled(); | |
557 | break; | |
8429abe0 RW |
558 | case IMSG_RECONF_CONF: |
559 | if ((nconf = malloc(sizeof(struct ldpd_conf))) == | |
560 | NULL) | |
561 | fatal(NULL); | |
562 | memcpy(nconf, imsg.data, sizeof(struct ldpd_conf)); | |
563 | ||
45926e58 RZ |
564 | RB_INIT(iface_head, &nconf->iface_tree); |
565 | RB_INIT(tnbr_head, &nconf->tnbr_tree); | |
566 | RB_INIT(nbrp_head, &nconf->nbrp_tree); | |
567 | RB_INIT(l2vpn_head, &nconf->l2vpn_tree); | |
8429abe0 RW |
568 | break; |
569 | case IMSG_RECONF_IFACE: | |
570 | if ((niface = malloc(sizeof(struct iface))) == NULL) | |
571 | fatal(NULL); | |
572 | memcpy(niface, imsg.data, sizeof(struct iface)); | |
573 | ||
7d3d7491 | 574 | RB_INSERT(iface_head, &nconf->iface_tree, niface); |
8429abe0 RW |
575 | break; |
576 | case IMSG_RECONF_TNBR: | |
577 | if ((ntnbr = malloc(sizeof(struct tnbr))) == NULL) | |
578 | fatal(NULL); | |
579 | memcpy(ntnbr, imsg.data, sizeof(struct tnbr)); | |
580 | ||
7989cdba | 581 | RB_INSERT(tnbr_head, &nconf->tnbr_tree, ntnbr); |
8429abe0 RW |
582 | break; |
583 | case IMSG_RECONF_NBRP: | |
584 | if ((nnbrp = malloc(sizeof(struct nbr_params))) == NULL) | |
585 | fatal(NULL); | |
586 | memcpy(nnbrp, imsg.data, sizeof(struct nbr_params)); | |
587 | ||
76c4abd1 | 588 | RB_INSERT(nbrp_head, &nconf->nbrp_tree, nnbrp); |
8429abe0 RW |
589 | break; |
590 | case IMSG_RECONF_L2VPN: | |
591 | if ((nl2vpn = malloc(sizeof(struct l2vpn))) == NULL) | |
592 | fatal(NULL); | |
593 | memcpy(nl2vpn, imsg.data, sizeof(struct l2vpn)); | |
594 | ||
45926e58 RZ |
595 | RB_INIT(l2vpn_if_head, &nl2vpn->if_tree); |
596 | RB_INIT(l2vpn_pw_head, &nl2vpn->pw_tree); | |
597 | RB_INIT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree); | |
8429abe0 | 598 | |
90d7e7bd | 599 | RB_INSERT(l2vpn_head, &nconf->l2vpn_tree, nl2vpn); |
8429abe0 RW |
600 | break; |
601 | case IMSG_RECONF_L2VPN_IF: | |
602 | if ((nlif = malloc(sizeof(struct l2vpn_if))) == NULL) | |
603 | fatal(NULL); | |
604 | memcpy(nlif, imsg.data, sizeof(struct l2vpn_if)); | |
605 | ||
029c1958 | 606 | RB_INSERT(l2vpn_if_head, &nl2vpn->if_tree, nlif); |
8429abe0 RW |
607 | break; |
608 | case IMSG_RECONF_L2VPN_PW: | |
609 | if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL) | |
610 | fatal(NULL); | |
611 | memcpy(npw, imsg.data, sizeof(struct l2vpn_pw)); | |
612 | ||
20bacaeb | 613 | RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_tree, npw); |
8429abe0 | 614 | break; |
eac6e3f0 RW |
615 | case IMSG_RECONF_L2VPN_IPW: |
616 | if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL) | |
617 | fatal(NULL); | |
618 | memcpy(npw, imsg.data, sizeof(struct l2vpn_pw)); | |
619 | ||
20bacaeb | 620 | RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree, npw); |
eac6e3f0 | 621 | break; |
8429abe0 RW |
622 | case IMSG_RECONF_END: |
623 | merge_config(ldeconf, nconf); | |
1d75a89d | 624 | ldp_clear_config(nconf); |
8429abe0 RW |
625 | nconf = NULL; |
626 | break; | |
eac6e3f0 RW |
627 | case IMSG_DEBUG_UPDATE: |
628 | if (imsg.hdr.len != IMSG_HEADER_SIZE + | |
629 | sizeof(ldp_debug)) { | |
630 | log_warnx("%s: wrong imsg len", __func__); | |
631 | break; | |
632 | } | |
633 | memcpy(&ldp_debug, imsg.data, sizeof(ldp_debug)); | |
634 | break; | |
2d1aa1e8 | 635 | case IMSG_FILTER_UPDATE: |
636 | if (imsg.hdr.len != IMSG_HEADER_SIZE + | |
637 | sizeof(struct ldp_access)) { | |
638 | log_warnx("%s: wrong imsg len", __func__); | |
639 | break; | |
640 | } | |
641 | laccess = imsg.data; | |
642 | lde_check_filter_af(AF_INET, &ldeconf->ipv4, | |
643 | laccess->name); | |
644 | lde_check_filter_af(AF_INET6, &ldeconf->ipv6, | |
645 | laccess->name); | |
646 | break; | |
077d336a RW |
647 | case IMSG_RLFA_REG: |
648 | if (imsg.hdr.len != IMSG_HEADER_SIZE + | |
649 | sizeof(struct zapi_rlfa_request)) { | |
650 | log_warnx("%s: wrong imsg len", __func__); | |
651 | break; | |
652 | } | |
653 | rlfa_req = imsg.data; | |
654 | rnode = rlfa_node_find(&rlfa_req->destination, | |
655 | rlfa_req->pq_address); | |
656 | if (!rnode) | |
657 | rnode = rlfa_node_new(&rlfa_req->destination, | |
658 | rlfa_req->pq_address); | |
659 | rclient = rlfa_client_find(rnode, &rlfa_req->igp); | |
660 | if (rclient) | |
661 | /* RLFA already registered - do nothing */ | |
662 | break; | |
663 | rclient = rlfa_client_new(rnode, &rlfa_req->igp); | |
664 | lde_rlfa_check(rclient); | |
665 | break; | |
666 | case IMSG_RLFA_UNREG_ALL: | |
667 | if (imsg.hdr.len != IMSG_HEADER_SIZE + | |
668 | sizeof(struct zapi_rlfa_igp)) { | |
669 | log_warnx("%s: wrong imsg len", __func__); | |
670 | break; | |
671 | } | |
672 | rlfa_igp = imsg.data; | |
673 | ||
674 | RB_FOREACH_SAFE (rnode, ldp_rlfa_node_head, | |
675 | &rlfa_node_tree, rntmp) { | |
676 | rclient = rlfa_client_find(rnode, rlfa_igp); | |
677 | if (!rclient) | |
678 | continue; | |
679 | ||
680 | rlfa_client_del(rclient); | |
681 | } | |
682 | break; | |
8429abe0 RW |
683 | default: |
684 | log_debug("%s: unexpected imsg %d", __func__, | |
685 | imsg.hdr.type); | |
686 | break; | |
687 | } | |
688 | imsg_free(&imsg); | |
689 | } | |
690 | if (!shut) | |
691 | imsg_event_add(iev); | |
692 | else { | |
eac6e3f0 | 693 | /* this pipe is dead, so remove the event handlers and exit */ |
2783a692 DS |
694 | THREAD_OFF(iev->ev_read); |
695 | THREAD_OFF(iev->ev_write); | |
eac6e3f0 | 696 | lde_shutdown(); |
8429abe0 RW |
697 | } |
698 | } | |
699 | ||
45a8eba9 RW |
700 | int |
701 | lde_acl_check(char *acl_name, int af, union ldpd_addr *addr, uint8_t prefixlen) | |
702 | { | |
703 | return ldp_acl_request(iev_main_sync, acl_name, af, addr, prefixlen); | |
704 | } | |
705 | ||
aff1743c KS |
706 | static bool lde_fec_connected(const struct fec_node *fn) |
707 | { | |
708 | struct fec_nh *fnh; | |
709 | ||
710 | LIST_FOREACH(fnh, &fn->nexthops, entry) | |
711 | if (fnh->flags & F_FEC_NH_CONNECTED) | |
712 | return true; | |
713 | ||
714 | return false; | |
715 | } | |
716 | ||
717 | static bool lde_fec_outside_mpls_network(const struct fec_node *fn) | |
718 | { | |
719 | struct fec_nh *fnh; | |
720 | ||
721 | LIST_FOREACH(fnh, &fn->nexthops, entry) | |
722 | if (!(fnh->flags & F_FEC_NH_NO_LDP)) | |
723 | return false; | |
724 | ||
725 | return true; | |
726 | } | |
727 | ||
8429abe0 | 728 | uint32_t |
8cb1fc45 | 729 | lde_update_label(struct fec_node *fn) |
8429abe0 | 730 | { |
8429abe0 | 731 | |
45a8eba9 | 732 | /* should we allocate a label for this fec? */ |
8cb1fc45 | 733 | switch (fn->fec.type) { |
45a8eba9 | 734 | case FEC_TYPE_IPV4: |
12256b84 DA |
735 | if ((ldeconf->ipv4.flags & F_LDPD_AF_ALLOCHOSTONLY) |
736 | && fn->fec.u.ipv4.prefixlen != IPV4_MAX_BITLEN) | |
45a8eba9 RW |
737 | return (NO_LABEL); |
738 | if (lde_acl_check(ldeconf->ipv4.acl_label_allocate_for, | |
8cb1fc45 RW |
739 | AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix, |
740 | fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT) | |
45a8eba9 RW |
741 | return (NO_LABEL); |
742 | break; | |
743 | case FEC_TYPE_IPV6: | |
13ccce6e DA |
744 | if ((ldeconf->ipv6.flags & F_LDPD_AF_ALLOCHOSTONLY) |
745 | && fn->fec.u.ipv6.prefixlen != IPV6_MAX_BITLEN) | |
45a8eba9 RW |
746 | return (NO_LABEL); |
747 | if (lde_acl_check(ldeconf->ipv6.acl_label_allocate_for, | |
8cb1fc45 RW |
748 | AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix, |
749 | fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT) | |
45a8eba9 RW |
750 | return (NO_LABEL); |
751 | break; | |
4a055ac2 | 752 | case FEC_TYPE_PWID: |
45a8eba9 RW |
753 | break; |
754 | } | |
755 | ||
aff1743c KS |
756 | /* |
757 | * If connected interface act as egress for fec. | |
758 | * If LDP is not configured on an interface but there | |
759 | * are other NHs with interfaces configured with LDP | |
760 | * then don't act as an egress for the fec, otherwise | |
761 | * act as an egress for the fec | |
762 | */ | |
763 | if (lde_fec_connected(fn) || lde_fec_outside_mpls_network(fn)) { | |
45a8eba9 | 764 | /* choose implicit or explicit-null depending on configuration */ |
8cb1fc45 | 765 | switch (fn->fec.type) { |
45a8eba9 RW |
766 | case FEC_TYPE_IPV4: |
767 | if (!(ldeconf->ipv4.flags & F_LDPD_AF_EXPNULL)) | |
70e98a7f | 768 | return (MPLS_LABEL_IMPLICIT_NULL); |
45a8eba9 | 769 | if (lde_acl_check(ldeconf->ipv4.acl_label_expnull_for, |
8cb1fc45 RW |
770 | AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix, |
771 | fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT) | |
70e98a7f DS |
772 | return (MPLS_LABEL_IMPLICIT_NULL); |
773 | return MPLS_LABEL_IPV4_EXPLICIT_NULL; | |
45a8eba9 RW |
774 | case FEC_TYPE_IPV6: |
775 | if (!(ldeconf->ipv6.flags & F_LDPD_AF_EXPNULL)) | |
70e98a7f | 776 | return (MPLS_LABEL_IMPLICIT_NULL); |
45a8eba9 | 777 | if (lde_acl_check(ldeconf->ipv6.acl_label_expnull_for, |
8cb1fc45 RW |
778 | AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix, |
779 | fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT) | |
70e98a7f DS |
780 | return (MPLS_LABEL_IMPLICIT_NULL); |
781 | return MPLS_LABEL_IPV6_EXPLICIT_NULL; | |
4a055ac2 | 782 | case FEC_TYPE_PWID: |
45a8eba9 RW |
783 | break; |
784 | } | |
785 | } | |
786 | ||
8cb1fc45 RW |
787 | /* preserve current label if there's no need to update it */ |
788 | if (fn->local_label != NO_LABEL && | |
789 | fn->local_label > MPLS_LABEL_RESERVED_MAX) | |
790 | return (fn->local_label); | |
791 | ||
5afba51d | 792 | return (lde_get_next_label()); |
8429abe0 RW |
793 | } |
794 | ||
795 | void | |
796 | lde_send_change_klabel(struct fec_node *fn, struct fec_nh *fnh) | |
797 | { | |
87b5f1b7 RW |
798 | struct kroute kr; |
799 | struct zapi_pw zpw; | |
8429abe0 RW |
800 | struct l2vpn_pw *pw; |
801 | ||
aff1743c KS |
802 | /* |
803 | * Ordered Control: don't program label into HW until a | |
804 | * labelmap msg has been received from upstream router | |
805 | */ | |
806 | if (fnh->flags & F_FEC_NH_DEFER) | |
807 | return; | |
808 | ||
8429abe0 RW |
809 | switch (fn->fec.type) { |
810 | case FEC_TYPE_IPV4: | |
811 | memset(&kr, 0, sizeof(kr)); | |
812 | kr.af = AF_INET; | |
813 | kr.prefix.v4 = fn->fec.u.ipv4.prefix; | |
814 | kr.prefixlen = fn->fec.u.ipv4.prefixlen; | |
815 | kr.nexthop.v4 = fnh->nexthop.v4; | |
88d88a9c | 816 | kr.ifindex = fnh->ifindex; |
8429abe0 RW |
817 | kr.local_label = fn->local_label; |
818 | kr.remote_label = fnh->remote_label; | |
e132dea0 RW |
819 | kr.route_type = fnh->route_type; |
820 | kr.route_instance = fnh->route_instance; | |
8429abe0 RW |
821 | lde_imsg_compose_parent(IMSG_KLABEL_CHANGE, 0, &kr, |
822 | sizeof(kr)); | |
8429abe0 RW |
823 | break; |
824 | case FEC_TYPE_IPV6: | |
825 | memset(&kr, 0, sizeof(kr)); | |
826 | kr.af = AF_INET6; | |
827 | kr.prefix.v6 = fn->fec.u.ipv6.prefix; | |
828 | kr.prefixlen = fn->fec.u.ipv6.prefixlen; | |
829 | kr.nexthop.v6 = fnh->nexthop.v6; | |
88d88a9c | 830 | kr.ifindex = fnh->ifindex; |
8429abe0 RW |
831 | kr.local_label = fn->local_label; |
832 | kr.remote_label = fnh->remote_label; | |
e132dea0 RW |
833 | kr.route_type = fnh->route_type; |
834 | kr.route_instance = fnh->route_instance; | |
8429abe0 RW |
835 | |
836 | lde_imsg_compose_parent(IMSG_KLABEL_CHANGE, 0, &kr, | |
837 | sizeof(kr)); | |
8429abe0 RW |
838 | break; |
839 | case FEC_TYPE_PWID: | |
3c5b5220 RW |
840 | pw = (struct l2vpn_pw *) fn->data; |
841 | if (!pw || fn->local_label == NO_LABEL || | |
8429abe0 RW |
842 | fnh->remote_label == NO_LABEL) |
843 | return; | |
844 | ||
3c5b5220 | 845 | pw->enabled = true; |
87b5f1b7 RW |
846 | pw2zpw(pw, &zpw); |
847 | zpw.local_label = fn->local_label; | |
848 | zpw.remote_label = fnh->remote_label; | |
849 | lde_imsg_compose_parent(IMSG_KPW_SET, 0, &zpw, sizeof(zpw)); | |
8429abe0 RW |
850 | break; |
851 | } | |
852 | } | |
853 | ||
854 | void | |
855 | lde_send_delete_klabel(struct fec_node *fn, struct fec_nh *fnh) | |
856 | { | |
857 | struct kroute kr; | |
87b5f1b7 | 858 | struct zapi_pw zpw; |
8429abe0 RW |
859 | struct l2vpn_pw *pw; |
860 | ||
861 | switch (fn->fec.type) { | |
862 | case FEC_TYPE_IPV4: | |
863 | memset(&kr, 0, sizeof(kr)); | |
864 | kr.af = AF_INET; | |
865 | kr.prefix.v4 = fn->fec.u.ipv4.prefix; | |
866 | kr.prefixlen = fn->fec.u.ipv4.prefixlen; | |
867 | kr.nexthop.v4 = fnh->nexthop.v4; | |
88d88a9c | 868 | kr.ifindex = fnh->ifindex; |
8429abe0 RW |
869 | kr.local_label = fn->local_label; |
870 | kr.remote_label = fnh->remote_label; | |
e132dea0 RW |
871 | kr.route_type = fnh->route_type; |
872 | kr.route_instance = fnh->route_instance; | |
8429abe0 RW |
873 | |
874 | lde_imsg_compose_parent(IMSG_KLABEL_DELETE, 0, &kr, | |
875 | sizeof(kr)); | |
8429abe0 RW |
876 | break; |
877 | case FEC_TYPE_IPV6: | |
878 | memset(&kr, 0, sizeof(kr)); | |
879 | kr.af = AF_INET6; | |
880 | kr.prefix.v6 = fn->fec.u.ipv6.prefix; | |
881 | kr.prefixlen = fn->fec.u.ipv6.prefixlen; | |
882 | kr.nexthop.v6 = fnh->nexthop.v6; | |
88d88a9c | 883 | kr.ifindex = fnh->ifindex; |
8429abe0 RW |
884 | kr.local_label = fn->local_label; |
885 | kr.remote_label = fnh->remote_label; | |
e132dea0 RW |
886 | kr.route_type = fnh->route_type; |
887 | kr.route_instance = fnh->route_instance; | |
8429abe0 RW |
888 | |
889 | lde_imsg_compose_parent(IMSG_KLABEL_DELETE, 0, &kr, | |
890 | sizeof(kr)); | |
8429abe0 RW |
891 | break; |
892 | case FEC_TYPE_PWID: | |
893 | pw = (struct l2vpn_pw *) fn->data; | |
3c5b5220 RW |
894 | if (!pw) |
895 | return; | |
896 | ||
897 | pw->enabled = false; | |
87b5f1b7 RW |
898 | pw2zpw(pw, &zpw); |
899 | zpw.local_label = fn->local_label; | |
900 | zpw.remote_label = fnh->remote_label; | |
901 | lde_imsg_compose_parent(IMSG_KPW_UNSET, 0, &zpw, sizeof(zpw)); | |
8429abe0 RW |
902 | break; |
903 | } | |
904 | } | |
905 | ||
077d336a RW |
906 | void |
907 | lde_fec2prefix(const struct fec *fec, struct prefix *prefix) | |
908 | { | |
909 | memset(prefix, 0, sizeof(*prefix)); | |
910 | switch (fec->type) { | |
911 | case FEC_TYPE_IPV4: | |
912 | prefix->family = AF_INET; | |
913 | prefix->u.prefix4 = fec->u.ipv4.prefix; | |
914 | prefix->prefixlen = fec->u.ipv4.prefixlen; | |
915 | break; | |
916 | case FEC_TYPE_IPV6: | |
917 | prefix->family = AF_INET6; | |
918 | prefix->u.prefix6 = fec->u.ipv6.prefix; | |
919 | prefix->prefixlen = fec->u.ipv6.prefixlen; | |
920 | break; | |
4a055ac2 | 921 | case FEC_TYPE_PWID: |
077d336a RW |
922 | prefix->family = AF_UNSPEC; |
923 | break; | |
924 | } | |
925 | } | |
926 | ||
927 | void | |
928 | lde_prefix2fec(const struct prefix *prefix, struct fec *fec) | |
929 | { | |
930 | memset(fec, 0, sizeof(*fec)); | |
931 | switch (prefix->family) { | |
932 | case AF_INET: | |
933 | fec->type = FEC_TYPE_IPV4; | |
934 | fec->u.ipv4.prefix = prefix->u.prefix4; | |
935 | fec->u.ipv4.prefixlen = prefix->prefixlen; | |
936 | break; | |
937 | case AF_INET6: | |
938 | fec->type = FEC_TYPE_IPV6; | |
939 | fec->u.ipv6.prefix = prefix->u.prefix6; | |
940 | fec->u.ipv6.prefixlen = prefix->prefixlen; | |
941 | break; | |
942 | default: | |
943 | fatalx("lde_prefix2fec: unknown af"); | |
944 | break; | |
945 | } | |
946 | } | |
947 | ||
8429abe0 RW |
948 | void |
949 | lde_fec2map(struct fec *fec, struct map *map) | |
950 | { | |
951 | memset(map, 0, sizeof(*map)); | |
952 | ||
953 | switch (fec->type) { | |
954 | case FEC_TYPE_IPV4: | |
955 | map->type = MAP_TYPE_PREFIX; | |
956 | map->fec.prefix.af = AF_INET; | |
957 | map->fec.prefix.prefix.v4 = fec->u.ipv4.prefix; | |
958 | map->fec.prefix.prefixlen = fec->u.ipv4.prefixlen; | |
959 | break; | |
960 | case FEC_TYPE_IPV6: | |
961 | map->type = MAP_TYPE_PREFIX; | |
962 | map->fec.prefix.af = AF_INET6; | |
963 | map->fec.prefix.prefix.v6 = fec->u.ipv6.prefix; | |
964 | map->fec.prefix.prefixlen = fec->u.ipv6.prefixlen; | |
965 | break; | |
966 | case FEC_TYPE_PWID: | |
967 | map->type = MAP_TYPE_PWID; | |
968 | map->fec.pwid.type = fec->u.pwid.type; | |
969 | map->fec.pwid.group_id = 0; | |
970 | map->flags |= F_MAP_PW_ID; | |
971 | map->fec.pwid.pwid = fec->u.pwid.pwid; | |
972 | break; | |
973 | } | |
974 | } | |
975 | ||
976 | void | |
977 | lde_map2fec(struct map *map, struct in_addr lsr_id, struct fec *fec) | |
978 | { | |
979 | memset(fec, 0, sizeof(*fec)); | |
980 | ||
981 | switch (map->type) { | |
982 | case MAP_TYPE_PREFIX: | |
983 | switch (map->fec.prefix.af) { | |
984 | case AF_INET: | |
985 | fec->type = FEC_TYPE_IPV4; | |
986 | fec->u.ipv4.prefix = map->fec.prefix.prefix.v4; | |
987 | fec->u.ipv4.prefixlen = map->fec.prefix.prefixlen; | |
988 | break; | |
989 | case AF_INET6: | |
990 | fec->type = FEC_TYPE_IPV6; | |
991 | fec->u.ipv6.prefix = map->fec.prefix.prefix.v6; | |
992 | fec->u.ipv6.prefixlen = map->fec.prefix.prefixlen; | |
993 | break; | |
994 | default: | |
995 | fatalx("lde_map2fec: unknown af"); | |
996 | break; | |
997 | } | |
998 | break; | |
999 | case MAP_TYPE_PWID: | |
1000 | fec->type = FEC_TYPE_PWID; | |
1001 | fec->u.pwid.type = map->fec.pwid.type; | |
1002 | fec->u.pwid.pwid = map->fec.pwid.pwid; | |
1003 | fec->u.pwid.lsr_id = lsr_id; | |
1004 | break; | |
1005 | } | |
1006 | } | |
1007 | ||
1008 | void | |
1009 | lde_send_labelmapping(struct lde_nbr *ln, struct fec_node *fn, int single) | |
1010 | { | |
7c2abbd7 RW |
1011 | struct lde_wdraw *lw; |
1012 | struct lde_map *me; | |
1013 | struct lde_req *lre; | |
1014 | struct map map; | |
1015 | struct l2vpn_pw *pw; | |
aff1743c KS |
1016 | struct fec_nh *fnh; |
1017 | bool allow = false; | |
1018 | ||
1019 | /* | |
1020 | * Ordered Control: do not send a labelmap msg until | |
1021 | * a labelmap message is received from downstream router | |
1022 | * and don't send labelmap back to downstream router | |
1023 | */ | |
1024 | if (ldeconf->flags & F_LDPD_ORDERED_CONTROL) { | |
1025 | LIST_FOREACH(fnh, &fn->nexthops, entry) { | |
1026 | if (fnh->flags & F_FEC_NH_DEFER) | |
1027 | continue; | |
1028 | ||
1029 | if (lde_address_find(ln, fnh->af, &fnh->nexthop)) | |
1030 | return; | |
1031 | allow = true; | |
1032 | break; | |
1033 | } | |
1034 | if (!allow) | |
1035 | return; | |
1036 | } | |
7c2abbd7 RW |
1037 | |
1038 | /* | |
1039 | * We shouldn't send a new label mapping if we have a pending | |
1040 | * label release to receive. In this case, schedule to send a | |
1041 | * label mapping as soon as a label release is received. | |
1042 | */ | |
1043 | lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, &fn->fec); | |
1044 | if (lw) { | |
b4fcca6b | 1045 | if (!fec_find(&ln->sent_map_pending, &fn->fec)) { |
3efd0893 | 1046 | debug_evt("%s: FEC %s: scheduling to send label mapping later (waiting for pending label release)", |
b4fcca6b | 1047 | __func__, log_fec(&fn->fec)); |
7c2abbd7 | 1048 | lde_map_pending_add(ln, fn); |
b4fcca6b | 1049 | } |
7c2abbd7 RW |
1050 | return; |
1051 | } | |
8429abe0 RW |
1052 | |
1053 | /* | |
1054 | * This function skips SL.1 - 3 and SL.9 - 14 because the label | |
1055 | * allocation is done way earlier (because of the merging nature of | |
1056 | * ldpd). | |
1057 | */ | |
1058 | ||
1059 | lde_fec2map(&fn->fec, &map); | |
1060 | switch (fn->fec.type) { | |
1061 | case FEC_TYPE_IPV4: | |
1062 | if (!ln->v4_enabled) | |
1063 | return; | |
45a8eba9 RW |
1064 | if (lde_acl_check(ldeconf->ipv4.acl_label_advertise_to, |
1065 | AF_INET, (union ldpd_addr *)&ln->id, 32) != FILTER_PERMIT) | |
1066 | return; | |
1067 | if (lde_acl_check(ldeconf->ipv4.acl_label_advertise_for, | |
1068 | AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix, | |
1069 | fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT) | |
1070 | return; | |
8429abe0 RW |
1071 | break; |
1072 | case FEC_TYPE_IPV6: | |
1073 | if (!ln->v6_enabled) | |
1074 | return; | |
45a8eba9 RW |
1075 | if (lde_acl_check(ldeconf->ipv6.acl_label_advertise_to, |
1076 | AF_INET, (union ldpd_addr *)&ln->id, 32) != FILTER_PERMIT) | |
1077 | return; | |
1078 | if (lde_acl_check(ldeconf->ipv6.acl_label_advertise_for, | |
1079 | AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix, | |
1080 | fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT) | |
1081 | return; | |
8429abe0 RW |
1082 | break; |
1083 | case FEC_TYPE_PWID: | |
1084 | pw = (struct l2vpn_pw *) fn->data; | |
1085 | if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr) | |
1086 | /* not the remote end of the pseudowire */ | |
1087 | return; | |
1088 | ||
1089 | map.flags |= F_MAP_PW_IFMTU; | |
1090 | map.fec.pwid.ifmtu = pw->l2vpn->mtu; | |
1091 | if (pw->flags & F_PW_CWORD) | |
1092 | map.flags |= F_MAP_PW_CWORD; | |
1093 | if (pw->flags & F_PW_STATUSTLV) { | |
1094 | map.flags |= F_MAP_PW_STATUS; | |
87b5f1b7 | 1095 | map.pw_status = pw->local_status; |
8429abe0 RW |
1096 | } |
1097 | break; | |
1098 | } | |
1099 | map.label = fn->local_label; | |
1100 | ||
1101 | /* SL.6: is there a pending request for this mapping? */ | |
1102 | lre = (struct lde_req *)fec_find(&ln->recv_req, &fn->fec); | |
1103 | if (lre) { | |
1104 | /* set label request msg id in the mapping response. */ | |
1105 | map.requestid = lre->msg_id; | |
1106 | map.flags = F_MAP_REQ_ID; | |
1107 | ||
1108 | /* SL.7: delete record of pending request */ | |
1109 | lde_req_del(ln, lre, 0); | |
1110 | } | |
1111 | ||
1112 | /* SL.4: send label mapping */ | |
1113 | lde_imsg_compose_ldpe(IMSG_MAPPING_ADD, ln->peerid, 0, | |
1114 | &map, sizeof(map)); | |
1115 | if (single) | |
1116 | lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0, | |
1117 | NULL, 0); | |
1118 | ||
1119 | /* SL.5: record sent label mapping */ | |
1120 | me = (struct lde_map *)fec_find(&ln->sent_map, &fn->fec); | |
1121 | if (me == NULL) | |
1122 | me = lde_map_add(ln, fn, 1); | |
1123 | me->map = map; | |
1124 | } | |
1125 | ||
1126 | void | |
0bcc2916 RW |
1127 | lde_send_labelwithdraw(struct lde_nbr *ln, struct fec_node *fn, |
1128 | struct map *wcard, struct status_tlv *st) | |
8429abe0 RW |
1129 | { |
1130 | struct lde_wdraw *lw; | |
1131 | struct map map; | |
1132 | struct fec *f; | |
1133 | struct l2vpn_pw *pw; | |
1134 | ||
1135 | if (fn) { | |
1136 | lde_fec2map(&fn->fec, &map); | |
1137 | switch (fn->fec.type) { | |
1138 | case FEC_TYPE_IPV4: | |
1139 | if (!ln->v4_enabled) | |
1140 | return; | |
1141 | break; | |
1142 | case FEC_TYPE_IPV6: | |
1143 | if (!ln->v6_enabled) | |
1144 | return; | |
1145 | break; | |
1146 | case FEC_TYPE_PWID: | |
1147 | pw = (struct l2vpn_pw *) fn->data; | |
1148 | if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr) | |
1149 | /* not the remote end of the pseudowire */ | |
1150 | return; | |
1151 | ||
1152 | if (pw->flags & F_PW_CWORD) | |
1153 | map.flags |= F_MAP_PW_CWORD; | |
1154 | break; | |
1155 | } | |
1156 | map.label = fn->local_label; | |
0bcc2916 RW |
1157 | } else |
1158 | memcpy(&map, wcard, sizeof(map)); | |
8429abe0 RW |
1159 | |
1160 | if (st) { | |
1161 | map.st.status_code = st->status_code; | |
1162 | map.st.msg_id = st->msg_id; | |
1163 | map.st.msg_type = st->msg_type; | |
1164 | map.flags |= F_MAP_STATUS; | |
1165 | } | |
1166 | ||
1167 | /* SWd.1: send label withdraw. */ | |
1168 | lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD, ln->peerid, 0, | |
1169 | &map, sizeof(map)); | |
1170 | lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD_END, ln->peerid, 0, NULL, 0); | |
1171 | ||
1172 | /* SWd.2: record label withdraw. */ | |
1173 | if (fn) { | |
1174 | lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, &fn->fec); | |
1175 | if (lw == NULL) | |
1176 | lw = lde_wdraw_add(ln, fn); | |
1177 | lw->label = map.label; | |
1178 | } else { | |
0bcc2916 RW |
1179 | struct lde_map *me; |
1180 | ||
8429abe0 RW |
1181 | RB_FOREACH(f, fec_tree, &ft) { |
1182 | fn = (struct fec_node *)f; | |
0bcc2916 RW |
1183 | me = (struct lde_map *)fec_find(&ln->sent_map, &fn->fec); |
1184 | if (lde_wildcard_apply(wcard, &fn->fec, me) == 0) | |
1185 | continue; | |
8429abe0 RW |
1186 | |
1187 | lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, | |
1188 | &fn->fec); | |
1189 | if (lw == NULL) | |
1190 | lw = lde_wdraw_add(ln, fn); | |
1191 | lw->label = map.label; | |
1192 | } | |
1193 | } | |
1194 | } | |
1195 | ||
1196 | void | |
0bcc2916 | 1197 | lde_send_labelwithdraw_wcard(struct lde_nbr *ln, uint32_t label) |
8429abe0 | 1198 | { |
0bcc2916 | 1199 | struct map wcard; |
8429abe0 | 1200 | |
0bcc2916 RW |
1201 | memset(&wcard, 0, sizeof(wcard)); |
1202 | wcard.type = MAP_TYPE_WILDCARD; | |
1203 | wcard.label = label; | |
1204 | lde_send_labelwithdraw(ln, NULL, &wcard, NULL); | |
8429abe0 RW |
1205 | } |
1206 | ||
d4afb819 RW |
1207 | void |
1208 | lde_send_labelwithdraw_twcard_prefix(struct lde_nbr *ln, uint16_t af, | |
1209 | uint32_t label) | |
1210 | { | |
1211 | struct map wcard; | |
1212 | ||
1213 | memset(&wcard, 0, sizeof(wcard)); | |
1214 | wcard.type = MAP_TYPE_TYPED_WCARD; | |
1215 | wcard.fec.twcard.type = MAP_TYPE_PREFIX; | |
1216 | wcard.fec.twcard.u.prefix_af = af; | |
1217 | wcard.label = label; | |
1218 | lde_send_labelwithdraw(ln, NULL, &wcard, NULL); | |
1219 | } | |
1220 | ||
aba50a83 RW |
1221 | void |
1222 | lde_send_labelwithdraw_twcard_pwid(struct lde_nbr *ln, uint16_t pw_type, | |
1223 | uint32_t label) | |
1224 | { | |
1225 | struct map wcard; | |
1226 | ||
1227 | memset(&wcard, 0, sizeof(wcard)); | |
1228 | wcard.type = MAP_TYPE_TYPED_WCARD; | |
1229 | wcard.fec.twcard.type = MAP_TYPE_PWID; | |
1230 | wcard.fec.twcard.u.pw_type = pw_type; | |
1231 | wcard.label = label; | |
1232 | lde_send_labelwithdraw(ln, NULL, &wcard, NULL); | |
1233 | } | |
1234 | ||
8429abe0 | 1235 | void |
0bcc2916 RW |
1236 | lde_send_labelwithdraw_pwid_wcard(struct lde_nbr *ln, uint16_t pw_type, |
1237 | uint32_t group_id) | |
1238 | { | |
1239 | struct map wcard; | |
1240 | ||
1241 | memset(&wcard, 0, sizeof(wcard)); | |
1242 | wcard.type = MAP_TYPE_PWID; | |
1243 | wcard.fec.pwid.type = pw_type; | |
1244 | wcard.fec.pwid.group_id = group_id; | |
1245 | /* we can not append a Label TLV when using PWid group wildcards. */ | |
1246 | wcard.label = NO_LABEL; | |
1247 | lde_send_labelwithdraw(ln, NULL, &wcard, NULL); | |
1248 | } | |
1249 | ||
1250 | void | |
1251 | lde_send_labelrelease(struct lde_nbr *ln, struct fec_node *fn, | |
1252 | struct map *wcard, uint32_t label) | |
8429abe0 RW |
1253 | { |
1254 | struct map map; | |
1255 | struct l2vpn_pw *pw; | |
1256 | ||
1257 | if (fn) { | |
1258 | lde_fec2map(&fn->fec, &map); | |
1259 | switch (fn->fec.type) { | |
1260 | case FEC_TYPE_IPV4: | |
1261 | if (!ln->v4_enabled) | |
1262 | return; | |
1263 | break; | |
1264 | case FEC_TYPE_IPV6: | |
1265 | if (!ln->v6_enabled) | |
1266 | return; | |
1267 | break; | |
1268 | case FEC_TYPE_PWID: | |
1269 | pw = (struct l2vpn_pw *) fn->data; | |
1270 | if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr) | |
1271 | /* not the remote end of the pseudowire */ | |
1272 | return; | |
1273 | ||
1274 | if (pw->flags & F_PW_CWORD) | |
1275 | map.flags |= F_MAP_PW_CWORD; | |
1276 | break; | |
1277 | } | |
0bcc2916 RW |
1278 | } else |
1279 | memcpy(&map, wcard, sizeof(map)); | |
8429abe0 RW |
1280 | map.label = label; |
1281 | ||
1282 | lde_imsg_compose_ldpe(IMSG_RELEASE_ADD, ln->peerid, 0, | |
1283 | &map, sizeof(map)); | |
1284 | lde_imsg_compose_ldpe(IMSG_RELEASE_ADD_END, ln->peerid, 0, NULL, 0); | |
1285 | } | |
1286 | ||
2d1aa1e8 | 1287 | void |
1288 | lde_send_labelrequest(struct lde_nbr *ln, struct fec_node *fn, | |
1289 | struct map *wcard, int single) | |
1290 | { | |
1291 | struct map map; | |
1292 | struct fec *f; | |
1293 | struct lde_req *lre; | |
1294 | ||
1295 | if (fn) { | |
1296 | lde_fec2map(&fn->fec, &map); | |
1297 | switch (fn->fec.type) { | |
1298 | case FEC_TYPE_IPV4: | |
1299 | if (!ln->v4_enabled) | |
1300 | return; | |
1301 | break; | |
1302 | case FEC_TYPE_IPV6: | |
1303 | if (!ln->v6_enabled) | |
1304 | return; | |
1305 | break; | |
4a055ac2 | 1306 | case FEC_TYPE_PWID: |
2d1aa1e8 | 1307 | fatalx("lde_send_labelrequest: unknown af"); |
1308 | } | |
1309 | } else | |
1310 | memcpy(&map, wcard, sizeof(map)); | |
1311 | ||
1312 | map.label = NO_LABEL; | |
1313 | ||
1314 | if (fn) { | |
1315 | /* SLR1.1: has label request for FEC been previously sent | |
1316 | * and still outstanding just return, | |
1317 | */ | |
1318 | lre = (struct lde_req *)fec_find(&ln->sent_req, &fn->fec); | |
1319 | if (lre == NULL) { | |
1320 | /* SLRq.3: send label request */ | |
1321 | lde_imsg_compose_ldpe(IMSG_REQUEST_ADD, ln->peerid, 0, | |
1322 | &map, sizeof(map)); | |
1323 | if (single) | |
1324 | lde_imsg_compose_ldpe(IMSG_REQUEST_ADD_END, | |
1325 | ln->peerid, 0, NULL, 0); | |
1326 | ||
1327 | /* SLRq.4: record sent request */ | |
1328 | lde_req_add(ln, &fn->fec, 1); | |
1329 | } | |
1330 | } else { | |
1331 | /* if Wilcard just send label request */ | |
1332 | /* SLRq.3: send label request */ | |
1333 | lde_imsg_compose_ldpe(IMSG_REQUEST_ADD, | |
1334 | ln->peerid, 0, &map, sizeof(map)); | |
1335 | if (single) | |
1336 | lde_imsg_compose_ldpe(IMSG_REQUEST_ADD_END, | |
1337 | ln->peerid, 0, NULL, 0); | |
1338 | ||
1339 | /* SLRq.4: record sent request */ | |
1340 | RB_FOREACH(f, fec_tree, &ft) { | |
1341 | fn = (struct fec_node *)f; | |
1342 | lre = (struct lde_req *)fec_find(&ln->sent_req, &fn->fec); | |
1343 | if (lde_wildcard_apply(wcard, &fn->fec, NULL) == 0) | |
1344 | continue; | |
1345 | if (lre == NULL) | |
1346 | lde_req_add(ln, f, 1); | |
1347 | } | |
1348 | } | |
1349 | } | |
1350 | ||
1351 | void | |
1352 | lde_send_labelrequest_wcard(struct lde_nbr *ln, uint16_t af) | |
1353 | { | |
1354 | struct map wcard; | |
1355 | ||
1356 | memset(&wcard, 0, sizeof(wcard)); | |
1357 | wcard.type = MAP_TYPE_TYPED_WCARD; | |
1358 | wcard.fec.twcard.type = MAP_TYPE_PREFIX; | |
1359 | wcard.fec.twcard.u.prefix_af = af; | |
1360 | lde_send_labelrequest(ln, NULL, &wcard, 1); | |
1361 | } | |
1362 | ||
8429abe0 | 1363 | void |
05aac414 | 1364 | lde_send_notification(struct lde_nbr *ln, uint32_t status_code, uint32_t msg_id, |
8429abe0 RW |
1365 | uint16_t msg_type) |
1366 | { | |
1367 | struct notify_msg nm; | |
1368 | ||
1369 | memset(&nm, 0, sizeof(nm)); | |
1370 | nm.status_code = status_code; | |
1371 | /* 'msg_id' and 'msg_type' should be in network byte order */ | |
1372 | nm.msg_id = msg_id; | |
1373 | nm.msg_type = msg_type; | |
1374 | ||
05aac414 | 1375 | lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0, |
8429abe0 RW |
1376 | &nm, sizeof(nm)); |
1377 | } | |
1378 | ||
257799cd RW |
1379 | void |
1380 | lde_send_notification_eol_prefix(struct lde_nbr *ln, int af) | |
1381 | { | |
1382 | struct notify_msg nm; | |
1383 | ||
1384 | memset(&nm, 0, sizeof(nm)); | |
1385 | nm.status_code = S_ENDOFLIB; | |
1386 | nm.fec.type = MAP_TYPE_TYPED_WCARD; | |
1387 | nm.fec.fec.twcard.type = MAP_TYPE_PREFIX; | |
1388 | nm.fec.fec.twcard.u.prefix_af = af; | |
1389 | nm.flags |= F_NOTIF_FEC; | |
1390 | ||
1391 | lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0, | |
1392 | &nm, sizeof(nm)); | |
1393 | } | |
1394 | ||
1395 | void | |
1396 | lde_send_notification_eol_pwid(struct lde_nbr *ln, uint16_t pw_type) | |
1397 | { | |
1398 | struct notify_msg nm; | |
1399 | ||
1400 | memset(&nm, 0, sizeof(nm)); | |
1401 | nm.status_code = S_ENDOFLIB; | |
1402 | nm.fec.type = MAP_TYPE_TYPED_WCARD; | |
1403 | nm.fec.fec.twcard.type = MAP_TYPE_PWID; | |
1404 | nm.fec.fec.twcard.u.pw_type = pw_type; | |
1405 | nm.flags |= F_NOTIF_FEC; | |
1406 | ||
1407 | lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0, | |
1408 | &nm, sizeof(nm)); | |
1409 | } | |
1410 | ||
8429abe0 | 1411 | static __inline int |
45926e58 | 1412 | lde_nbr_compare(const struct lde_nbr *a, const struct lde_nbr *b) |
8429abe0 RW |
1413 | { |
1414 | return (a->peerid - b->peerid); | |
1415 | } | |
1416 | ||
1417 | static struct lde_nbr * | |
1418 | lde_nbr_new(uint32_t peerid, struct lde_nbr *new) | |
1419 | { | |
1420 | struct lde_nbr *ln; | |
1421 | ||
1422 | if ((ln = calloc(1, sizeof(*ln))) == NULL) | |
1423 | fatal(__func__); | |
1424 | ||
1425 | ln->id = new->id; | |
1426 | ln->v4_enabled = new->v4_enabled; | |
1427 | ln->v6_enabled = new->v6_enabled; | |
257799cd | 1428 | ln->flags = new->flags; |
8429abe0 RW |
1429 | ln->peerid = peerid; |
1430 | fec_init(&ln->recv_map); | |
1431 | fec_init(&ln->sent_map); | |
7c2abbd7 | 1432 | fec_init(&ln->sent_map_pending); |
8429abe0 RW |
1433 | fec_init(&ln->recv_req); |
1434 | fec_init(&ln->sent_req); | |
1435 | fec_init(&ln->sent_wdraw); | |
1436 | ||
1437 | TAILQ_INIT(&ln->addr_list); | |
1438 | ||
1439 | if (RB_INSERT(nbr_tree, &lde_nbrs, ln) != NULL) | |
1440 | fatalx("lde_nbr_new: RB_INSERT failed"); | |
1441 | ||
1442 | return (ln); | |
1443 | } | |
1444 | ||
1445 | static void | |
1446 | lde_nbr_del(struct lde_nbr *ln) | |
1447 | { | |
1448 | struct fec *f; | |
1449 | struct fec_node *fn; | |
1450 | struct fec_nh *fnh; | |
1451 | struct l2vpn_pw *pw; | |
aff1743c | 1452 | struct lde_nbr *lnbr; |
8429abe0 RW |
1453 | |
1454 | if (ln == NULL) | |
1455 | return; | |
1456 | ||
1457 | /* uninstall received mappings */ | |
1458 | RB_FOREACH(f, fec_tree, &ft) { | |
1459 | fn = (struct fec_node *)f; | |
1460 | ||
077d336a RW |
1461 | /* Update RLFA clients. */ |
1462 | lde_rlfa_update_clients(f, ln, MPLS_INVALID_LABEL); | |
1463 | ||
8429abe0 RW |
1464 | LIST_FOREACH(fnh, &fn->nexthops, entry) { |
1465 | switch (f->type) { | |
1466 | case FEC_TYPE_IPV4: | |
1467 | case FEC_TYPE_IPV6: | |
1468 | if (!lde_address_find(ln, fnh->af, | |
1469 | &fnh->nexthop)) | |
1470 | continue; | |
aff1743c KS |
1471 | |
1472 | /* | |
1473 | * Ordered Control: must mark any non-connected | |
1474 | * NH to wait until we receive a labelmap msg | |
1475 | * before installing in kernel and sending to | |
1476 | * peer, must do this as NHs are not removed | |
1477 | * when lsps go down. Also send label withdraw | |
1478 | * to other neighbors for all fecs from neighbor | |
1479 | * going down | |
1480 | */ | |
1481 | if (ldeconf->flags & F_LDPD_ORDERED_CONTROL) { | |
1482 | fnh->flags |= F_FEC_NH_DEFER; | |
1483 | ||
1484 | RB_FOREACH(lnbr, nbr_tree, &lde_nbrs) { | |
1485 | if (ln->peerid == lnbr->peerid) | |
1486 | continue; | |
1487 | lde_send_labelwithdraw(lnbr, fn, NULL, NULL); | |
1488 | } | |
1489 | } | |
8429abe0 RW |
1490 | break; |
1491 | case FEC_TYPE_PWID: | |
1492 | if (f->u.pwid.lsr_id.s_addr != ln->id.s_addr) | |
1493 | continue; | |
1494 | pw = (struct l2vpn_pw *) fn->data; | |
6bbdd9e9 | 1495 | if (pw) { |
1496 | pw->reason = F_PW_NO_REMOTE_LABEL; | |
8429abe0 | 1497 | l2vpn_pw_reset(pw); |
6bbdd9e9 | 1498 | } |
8429abe0 RW |
1499 | break; |
1500 | default: | |
1501 | break; | |
1502 | } | |
1503 | ||
1504 | lde_send_delete_klabel(fn, fnh); | |
1505 | fnh->remote_label = NO_LABEL; | |
1506 | } | |
1507 | } | |
1508 | ||
1509 | lde_address_list_free(ln); | |
1510 | ||
1511 | fec_clear(&ln->recv_map, lde_map_free); | |
1512 | fec_clear(&ln->sent_map, lde_map_free); | |
7c2abbd7 | 1513 | fec_clear(&ln->sent_map_pending, free); |
8429abe0 RW |
1514 | fec_clear(&ln->recv_req, free); |
1515 | fec_clear(&ln->sent_req, free); | |
1516 | fec_clear(&ln->sent_wdraw, free); | |
1517 | ||
1518 | RB_REMOVE(nbr_tree, &lde_nbrs, ln); | |
1519 | ||
1520 | free(ln); | |
1521 | } | |
1522 | ||
1523 | static struct lde_nbr * | |
1524 | lde_nbr_find(uint32_t peerid) | |
1525 | { | |
1526 | struct lde_nbr ln; | |
1527 | ||
1528 | ln.peerid = peerid; | |
1529 | ||
1530 | return (RB_FIND(nbr_tree, &lde_nbrs, &ln)); | |
1531 | } | |
1532 | ||
1533 | struct lde_nbr * | |
1534 | lde_nbr_find_by_lsrid(struct in_addr addr) | |
1535 | { | |
1536 | struct lde_nbr *ln; | |
1537 | ||
1538 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
1539 | if (ln->id.s_addr == addr.s_addr) | |
1540 | return (ln); | |
1541 | ||
1542 | return (NULL); | |
1543 | } | |
1544 | ||
1545 | struct lde_nbr * | |
1546 | lde_nbr_find_by_addr(int af, union ldpd_addr *addr) | |
1547 | { | |
1548 | struct lde_nbr *ln; | |
1549 | ||
1550 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
1551 | if (lde_address_find(ln, af, addr) != NULL) | |
1552 | return (ln); | |
1553 | ||
1554 | return (NULL); | |
1555 | } | |
1556 | ||
1557 | static void | |
1558 | lde_nbr_clear(void) | |
1559 | { | |
1560 | struct lde_nbr *ln; | |
1561 | ||
55cd0f61 DS |
1562 | while (!RB_EMPTY(nbr_tree, &lde_nbrs)) { |
1563 | ln = RB_ROOT(nbr_tree, &lde_nbrs); | |
1564 | ||
8429abe0 | 1565 | lde_nbr_del(ln); |
55cd0f61 | 1566 | } |
8429abe0 RW |
1567 | } |
1568 | ||
1569 | static void | |
1570 | lde_nbr_addr_update(struct lde_nbr *ln, struct lde_addr *lde_addr, int removed) | |
1571 | { | |
1572 | struct fec *fec; | |
1573 | struct fec_node *fn; | |
1574 | struct fec_nh *fnh; | |
1575 | struct lde_map *me; | |
1576 | ||
1577 | RB_FOREACH(fec, fec_tree, &ln->recv_map) { | |
8429abe0 RW |
1578 | switch (fec->type) { |
1579 | case FEC_TYPE_IPV4: | |
1580 | if (lde_addr->af != AF_INET) | |
1581 | continue; | |
1582 | break; | |
1583 | case FEC_TYPE_IPV6: | |
1584 | if (lde_addr->af != AF_INET6) | |
1585 | continue; | |
1586 | break; | |
4a055ac2 | 1587 | case FEC_TYPE_PWID: |
8429abe0 RW |
1588 | continue; |
1589 | } | |
1590 | ||
1e4c8673 RW |
1591 | fn = (struct fec_node *)fec_find(&ft, fec); |
1592 | if (fn == NULL) | |
1593 | /* shouldn't happen */ | |
1594 | continue; | |
1595 | ||
8429abe0 RW |
1596 | LIST_FOREACH(fnh, &fn->nexthops, entry) { |
1597 | if (ldp_addrcmp(fnh->af, &fnh->nexthop, | |
1598 | &lde_addr->addr)) | |
1599 | continue; | |
1600 | ||
1601 | if (removed) { | |
1602 | lde_send_delete_klabel(fn, fnh); | |
1603 | fnh->remote_label = NO_LABEL; | |
1604 | } else { | |
1605 | me = (struct lde_map *)fec; | |
1606 | fnh->remote_label = me->map.label; | |
1607 | lde_send_change_klabel(fn, fnh); | |
1608 | } | |
1609 | break; | |
1610 | } | |
1611 | } | |
1612 | } | |
1613 | ||
ff5279ba | 1614 | void |
1615 | lde_allow_broken_lsp_update(int new_config) | |
1616 | { | |
1617 | struct fec_node *fn; | |
1618 | struct fec_nh *fnh; | |
1619 | struct fec *f; | |
1620 | ||
1621 | RB_FOREACH(f, fec_tree, &ft) { | |
1622 | fn = (struct fec_node *)f; | |
1623 | ||
1624 | LIST_FOREACH(fnh, &fn->nexthops, entry) { | |
1625 | /* allow-broken-lsp config is changing so | |
1626 | * we need to reprogram labeled routes to | |
1627 | * have proper top-level label | |
1628 | */ | |
1629 | if (!(new_config & F_LDPD_ALLOW_BROKEN_LSP)) | |
1630 | lde_send_delete_klabel(fn, fnh); | |
1631 | ||
1632 | if (fn->local_label != NO_LABEL) | |
1633 | lde_send_change_klabel(fn, fnh); | |
1634 | } | |
1635 | } | |
1636 | } | |
1637 | ||
d3e1887a | 1638 | static __inline int |
45926e58 | 1639 | lde_map_compare(const struct lde_map *a, const struct lde_map *b) |
d3e1887a RW |
1640 | { |
1641 | return (ldp_addrcmp(AF_INET, (union ldpd_addr *)&a->nexthop->id, | |
1642 | (union ldpd_addr *)&b->nexthop->id)); | |
1643 | } | |
1644 | ||
8429abe0 RW |
1645 | struct lde_map * |
1646 | lde_map_add(struct lde_nbr *ln, struct fec_node *fn, int sent) | |
1647 | { | |
1648 | struct lde_map *me; | |
1649 | ||
1650 | me = calloc(1, sizeof(*me)); | |
1651 | if (me == NULL) | |
1652 | fatal(__func__); | |
1653 | ||
1654 | me->fec = fn->fec; | |
1655 | me->nexthop = ln; | |
1656 | ||
1657 | if (sent) { | |
d3e1887a RW |
1658 | RB_INSERT(lde_map_head, &fn->upstream, me); |
1659 | me->head = &fn->upstream; | |
8429abe0 RW |
1660 | if (fec_insert(&ln->sent_map, &me->fec)) |
1661 | log_warnx("failed to add %s to sent map", | |
1662 | log_fec(&me->fec)); | |
1663 | /* XXX on failure more cleanup is needed */ | |
1664 | } else { | |
d3e1887a RW |
1665 | RB_INSERT(lde_map_head, &fn->downstream, me); |
1666 | me->head = &fn->downstream; | |
8429abe0 RW |
1667 | if (fec_insert(&ln->recv_map, &me->fec)) |
1668 | log_warnx("failed to add %s to recv map", | |
1669 | log_fec(&me->fec)); | |
1670 | } | |
1671 | ||
1672 | return (me); | |
1673 | } | |
1674 | ||
1675 | void | |
1676 | lde_map_del(struct lde_nbr *ln, struct lde_map *me, int sent) | |
1677 | { | |
1678 | if (sent) | |
1679 | fec_remove(&ln->sent_map, &me->fec); | |
1680 | else | |
1681 | fec_remove(&ln->recv_map, &me->fec); | |
1682 | ||
1683 | lde_map_free(me); | |
1684 | } | |
1685 | ||
1686 | static void | |
1687 | lde_map_free(void *ptr) | |
1688 | { | |
1689 | struct lde_map *map = ptr; | |
1690 | ||
d3e1887a | 1691 | RB_REMOVE(lde_map_head, map->head, map); |
8429abe0 RW |
1692 | free(map); |
1693 | } | |
1694 | ||
7c2abbd7 RW |
1695 | struct fec * |
1696 | lde_map_pending_add(struct lde_nbr *ln, struct fec_node *fn) | |
1697 | { | |
1698 | struct fec *map; | |
1699 | ||
1700 | map = calloc(1, sizeof(*map)); | |
1701 | if (map == NULL) | |
1702 | fatal(__func__); | |
1703 | ||
1704 | *map = fn->fec; | |
1705 | if (fec_insert(&ln->sent_map_pending, map)) | |
1706 | log_warnx("failed to add %s to sent map (pending)", | |
1707 | log_fec(map)); | |
1708 | ||
1709 | return (map); | |
1710 | } | |
1711 | ||
1712 | void | |
1713 | lde_map_pending_del(struct lde_nbr *ln, struct fec *map) | |
1714 | { | |
1715 | fec_remove(&ln->sent_map_pending, map); | |
1716 | free(map); | |
1717 | } | |
1718 | ||
8429abe0 RW |
1719 | struct lde_req * |
1720 | lde_req_add(struct lde_nbr *ln, struct fec *fec, int sent) | |
1721 | { | |
1722 | struct fec_tree *t; | |
1723 | struct lde_req *lre; | |
1724 | ||
1725 | t = sent ? &ln->sent_req : &ln->recv_req; | |
1726 | ||
1727 | lre = calloc(1, sizeof(*lre)); | |
1728 | if (lre != NULL) { | |
1729 | lre->fec = *fec; | |
1730 | ||
1731 | if (fec_insert(t, &lre->fec)) { | |
1732 | log_warnx("failed to add %s to %s req", | |
1733 | log_fec(&lre->fec), sent ? "sent" : "recv"); | |
1734 | free(lre); | |
1735 | return (NULL); | |
1736 | } | |
1737 | } | |
1738 | ||
1739 | return (lre); | |
1740 | } | |
1741 | ||
1742 | void | |
1743 | lde_req_del(struct lde_nbr *ln, struct lde_req *lre, int sent) | |
1744 | { | |
1745 | if (sent) | |
1746 | fec_remove(&ln->sent_req, &lre->fec); | |
1747 | else | |
1748 | fec_remove(&ln->recv_req, &lre->fec); | |
1749 | ||
1750 | free(lre); | |
1751 | } | |
1752 | ||
1753 | struct lde_wdraw * | |
1754 | lde_wdraw_add(struct lde_nbr *ln, struct fec_node *fn) | |
1755 | { | |
1756 | struct lde_wdraw *lw; | |
1757 | ||
1758 | lw = calloc(1, sizeof(*lw)); | |
1759 | if (lw == NULL) | |
1760 | fatal(__func__); | |
1761 | ||
1762 | lw->fec = fn->fec; | |
1763 | ||
1764 | if (fec_insert(&ln->sent_wdraw, &lw->fec)) | |
1765 | log_warnx("failed to add %s to sent wdraw", | |
1766 | log_fec(&lw->fec)); | |
1767 | ||
1768 | return (lw); | |
1769 | } | |
1770 | ||
1771 | void | |
1772 | lde_wdraw_del(struct lde_nbr *ln, struct lde_wdraw *lw) | |
1773 | { | |
1774 | fec_remove(&ln->sent_wdraw, &lw->fec); | |
1775 | free(lw); | |
1776 | } | |
1777 | ||
1778 | void | |
45a8eba9 | 1779 | lde_change_egress_label(int af) |
8429abe0 RW |
1780 | { |
1781 | struct lde_nbr *ln; | |
1782 | struct fec *f; | |
1783 | struct fec_node *fn; | |
1784 | ||
be54d744 | 1785 | /* explicitly withdraw all null labels */ |
8429abe0 | 1786 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) { |
70e98a7f | 1787 | lde_send_labelwithdraw_wcard(ln, MPLS_LABEL_IMPLICIT_NULL); |
45a8eba9 | 1788 | if (ln->v4_enabled) |
70e98a7f DS |
1789 | lde_send_labelwithdraw_wcard( |
1790 | ln, | |
1791 | MPLS_LABEL_IPV4_EXPLICIT_NULL); | |
45a8eba9 | 1792 | if (ln->v6_enabled) |
70e98a7f DS |
1793 | lde_send_labelwithdraw_wcard( |
1794 | ln, | |
1795 | MPLS_LABEL_IPV6_EXPLICIT_NULL); | |
be54d744 | 1796 | } |
8429abe0 | 1797 | |
be54d744 RW |
1798 | /* update label of connected routes */ |
1799 | RB_FOREACH(f, fec_tree, &ft) { | |
1800 | fn = (struct fec_node *)f; | |
1801 | if (fn->local_label > MPLS_LABEL_RESERVED_MAX) | |
1802 | continue; | |
8429abe0 | 1803 | |
be54d744 RW |
1804 | switch (af) { |
1805 | case AF_INET: | |
1806 | if (fn->fec.type != FEC_TYPE_IPV4) | |
1807 | continue; | |
1808 | break; | |
1809 | case AF_INET6: | |
1810 | if (fn->fec.type != FEC_TYPE_IPV6) | |
1811 | continue; | |
1812 | break; | |
1813 | default: | |
1814 | fatalx("lde_change_egress_label: unknown af"); | |
8429abe0 RW |
1815 | } |
1816 | ||
8cb1fc45 | 1817 | fn->local_label = lde_update_label(fn); |
45a8eba9 RW |
1818 | if (fn->local_label != NO_LABEL) |
1819 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
1820 | lde_send_labelmapping(ln, fn, 0); | |
be54d744 RW |
1821 | } |
1822 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
8429abe0 RW |
1823 | lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0, |
1824 | NULL, 0); | |
8429abe0 RW |
1825 | } |
1826 | ||
86753560 | 1827 | void |
2d1aa1e8 | 1828 | lde_change_allocate_filter(int af) |
86753560 | 1829 | { |
1830 | struct lde_nbr *ln; | |
1831 | struct fec *f; | |
1832 | struct fec_node *fn; | |
1833 | uint32_t new_label; | |
1834 | ||
2d1aa1e8 | 1835 | /* reallocate labels for fecs that match this filter */ |
86753560 | 1836 | RB_FOREACH(f, fec_tree, &ft) { |
1837 | fn = (struct fec_node *)f; | |
1838 | ||
1839 | switch (af) { | |
1840 | case AF_INET: | |
1841 | if (fn->fec.type != FEC_TYPE_IPV4) | |
1842 | continue; | |
1843 | break; | |
1844 | case AF_INET6: | |
1845 | if (fn->fec.type != FEC_TYPE_IPV6) | |
1846 | continue; | |
1847 | break; | |
1848 | default: | |
2d1aa1e8 | 1849 | fatalx("lde_change_allocate_filter: unknown af"); |
86753560 | 1850 | } |
1851 | ||
1852 | /* | |
1853 | * If the local label has changed to NO_LABEL, send a label | |
1854 | * withdraw to all peers. | |
1855 | * If the local label has changed and it's different from | |
1856 | * NO_LABEL, send a label mapping to all peers advertising | |
1857 | * the new label. | |
1858 | * If the local label hasn't changed, do nothing | |
1859 | */ | |
1860 | new_label = lde_update_label(fn); | |
1861 | if (fn->local_label != new_label) { | |
1862 | if (new_label == NO_LABEL) | |
1863 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
1864 | lde_send_labelwithdraw(ln, fn, | |
1865 | NULL, NULL); | |
1866 | ||
1867 | fn->local_label = new_label; | |
1868 | if (fn->local_label != NO_LABEL) | |
1869 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
1870 | lde_send_labelmapping(ln, fn, 0); | |
1871 | } | |
1872 | } | |
1873 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
1874 | lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0, | |
1875 | NULL, 0); | |
1876 | } | |
1877 | ||
2d1aa1e8 | 1878 | void |
1879 | lde_change_advertise_filter(int af) | |
1880 | { | |
1881 | struct lde_nbr *ln; | |
1882 | struct fec *f; | |
1883 | struct fec_node *fn; | |
1884 | char *acl_to_filter; | |
1885 | char *acl_for_filter; | |
1886 | union ldpd_addr *prefix; | |
1887 | uint8_t plen; | |
1888 | struct lde_map *me; | |
1889 | ||
1890 | /* advertise label for fecs to neighbors if matches advertise filters */ | |
1891 | switch (af) { | |
1892 | case AF_INET: | |
1893 | acl_to_filter = ldeconf->ipv4.acl_label_advertise_to; | |
1894 | acl_for_filter = ldeconf->ipv4.acl_label_advertise_for; | |
1895 | break; | |
1896 | case AF_INET6: | |
1897 | acl_to_filter = ldeconf->ipv6.acl_label_advertise_to; | |
1898 | acl_for_filter = ldeconf->ipv6.acl_label_advertise_for; | |
1899 | break; | |
1900 | default: | |
1901 | fatalx("lde_change_advertise_filter: unknown af"); | |
1902 | } | |
1903 | ||
1904 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) { | |
1905 | if (lde_acl_check(acl_to_filter, af, (union ldpd_addr *)&ln->id, | |
1906 | IPV4_MAX_BITLEN) != FILTER_PERMIT) | |
1907 | lde_send_labelwithdraw_wcard(ln, NO_LABEL); | |
1908 | else { | |
1909 | /* This neighbor is allowed in to_filter, so | |
1910 | * send labels if fec also matches for_filter | |
1911 | */ | |
1912 | RB_FOREACH(f, fec_tree, &ft) { | |
1913 | fn = (struct fec_node *)f; | |
1914 | switch (af) { | |
1915 | case AF_INET: | |
1916 | if (fn->fec.type != FEC_TYPE_IPV4) | |
1917 | continue; | |
1918 | prefix = (union ldpd_addr *) | |
1919 | &fn->fec.u.ipv4.prefix; | |
1920 | plen = fn->fec.u.ipv4.prefixlen; | |
1921 | break; | |
1922 | case FEC_TYPE_IPV6: | |
1923 | if (fn->fec.type != FEC_TYPE_IPV6) | |
1924 | continue; | |
1925 | prefix = (union ldpd_addr *) | |
1926 | &fn->fec.u.ipv6.prefix; | |
1927 | plen = fn->fec.u.ipv6.prefixlen; | |
1928 | break; | |
1929 | default: | |
1930 | continue; | |
1931 | } | |
1932 | if (lde_acl_check(acl_for_filter, af, | |
1933 | prefix, plen) != FILTER_PERMIT) { | |
1934 | me = (struct lde_map *)fec_find( | |
1935 | &ln->sent_map, &fn->fec); | |
1936 | if (me) | |
1937 | /* fec filtered withdraw */ | |
1938 | lde_send_labelwithdraw(ln, fn, | |
1939 | NULL, NULL); | |
1940 | } else | |
1941 | /* fec allowed send map */ | |
1942 | lde_send_labelmapping(ln, fn, 0); | |
1943 | } | |
1944 | lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, | |
1945 | ln->peerid, 0, NULL, 0); | |
1946 | } | |
1947 | } | |
1948 | } | |
1949 | ||
1950 | ||
1951 | void | |
1952 | lde_change_accept_filter(int af) | |
1953 | { | |
1954 | struct lde_nbr *ln; | |
1955 | struct fec *f; | |
1956 | struct fec_node *fn; | |
1957 | char *acl_for_filter; | |
1958 | char *acl_from_filter; | |
1959 | union ldpd_addr *prefix; | |
1960 | uint8_t plen; | |
1961 | struct lde_map *me; | |
1962 | enum fec_type type; | |
1963 | ||
1964 | /* accept labels from neighbors specified in the from_filter and for | |
1965 | * fecs defined in the for_filter | |
1966 | */ | |
1967 | switch (af) { | |
1968 | case AF_INET: | |
1969 | acl_for_filter = ldeconf->ipv4.acl_label_accept_for; | |
1970 | acl_from_filter = ldeconf->ipv4.acl_label_accept_from; | |
1971 | type = FEC_TYPE_IPV4; | |
1972 | break; | |
1973 | case AF_INET6: | |
1974 | acl_for_filter = ldeconf->ipv6.acl_label_accept_for; | |
1975 | acl_from_filter = ldeconf->ipv6.acl_label_accept_from; | |
1976 | type = FEC_TYPE_IPV6; | |
1977 | break; | |
1978 | default: | |
1979 | fatalx("lde_change_accept_filter: unknown af"); | |
1980 | } | |
1981 | ||
1982 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) { | |
1983 | if (lde_acl_check(acl_from_filter, AF_INET, (union ldpd_addr *) | |
1984 | &ln->id, IPV4_MAX_BITLEN) != FILTER_PERMIT) { | |
1985 | /* This neighbor is now filtered so remove fecs from | |
1986 | * recv list | |
1987 | */ | |
1988 | RB_FOREACH(f, fec_tree, &ft) { | |
1989 | fn = (struct fec_node *)f; | |
1990 | if (fn->fec.type == type) { | |
1991 | me = (struct lde_map *)fec_find( | |
1992 | &ln->recv_map, &fn->fec); | |
1993 | if (me) | |
1994 | lde_map_del(ln, me, 0); | |
1995 | } | |
1996 | } | |
1997 | } else if (ln->flags & F_NBR_CAP_TWCARD) { | |
1998 | /* This neighbor is allowed and supports type | |
1999 | * wildcard so send a labelrequest | |
2000 | * to get any new labels from neighbor | |
2001 | * and make sure any fecs we currently have | |
2002 | * match for_filter. | |
2003 | */ | |
2004 | RB_FOREACH(f, fec_tree, &ft) { | |
2005 | fn = (struct fec_node *)f; | |
2006 | switch (af) { | |
2007 | case AF_INET: | |
2008 | if (fn->fec.type != FEC_TYPE_IPV4) | |
2009 | continue; | |
2010 | prefix = (union ldpd_addr *) | |
2011 | &fn->fec.u.ipv4.prefix; | |
2012 | plen = fn->fec.u.ipv4.prefixlen; | |
2013 | break; | |
2014 | case AF_INET6: | |
2015 | if (fn->fec.type != FEC_TYPE_IPV6) | |
2016 | continue; | |
2017 | prefix = (union ldpd_addr *) | |
2018 | &fn->fec.u.ipv6.prefix; | |
2019 | plen = fn->fec.u.ipv6.prefixlen; | |
2020 | break; | |
2021 | default: | |
2022 | continue; | |
2023 | } | |
2024 | if (lde_acl_check(acl_for_filter, af, | |
2025 | prefix, plen) != FILTER_PERMIT) { | |
2026 | me = (struct lde_map *)fec_find( | |
2027 | &ln->recv_map, &fn->fec); | |
2028 | if (me) | |
2029 | lde_map_del(ln, me, 0); | |
2030 | } | |
2031 | } | |
2032 | lde_send_labelrequest_wcard(ln, af); | |
2033 | } else | |
2034 | /* Type Wildcard is not supported so restart session */ | |
2035 | lde_imsg_compose_ldpe(IMSG_NBR_SHUTDOWN, ln->peerid, 0, | |
2036 | NULL, 0); | |
2037 | } | |
2038 | } | |
2039 | ||
2040 | void | |
2041 | lde_change_expnull_for_filter(int af) | |
2042 | { | |
2043 | struct lde_nbr *ln; | |
2044 | struct fec *f; | |
2045 | struct fec_node *fn; | |
2046 | char *acl_name; | |
2047 | uint32_t exp_label; | |
2048 | union ldpd_addr *prefix; | |
2049 | uint8_t plen; | |
2050 | ||
2051 | /* Configure explicit-null advertisement for all fecs in this filter */ | |
2052 | RB_FOREACH(f, fec_tree, &ft) { | |
2053 | fn = (struct fec_node *)f; | |
2054 | ||
2055 | switch (af) { | |
2056 | case AF_INET: | |
2057 | if (fn->fec.type != FEC_TYPE_IPV4) | |
2058 | continue; | |
2059 | acl_name = ldeconf->ipv4.acl_label_expnull_for; | |
2060 | prefix = (union ldpd_addr *)&fn->fec.u.ipv4.prefix; | |
2061 | plen = fn->fec.u.ipv4.prefixlen; | |
2062 | exp_label = MPLS_LABEL_IPV4_EXPLICIT_NULL; | |
2063 | break; | |
2064 | case AF_INET6: | |
2065 | if (fn->fec.type != FEC_TYPE_IPV6) | |
2066 | continue; | |
2067 | acl_name = ldeconf->ipv6.acl_label_expnull_for; | |
2068 | prefix = (union ldpd_addr *)&fn->fec.u.ipv6.prefix; | |
2069 | plen = fn->fec.u.ipv6.prefixlen; | |
2070 | exp_label = MPLS_LABEL_IPV6_EXPLICIT_NULL; | |
2071 | break; | |
2072 | default: | |
2073 | fatalx("lde_change_expnull_for_filter: unknown af"); | |
2074 | } | |
2075 | ||
2076 | if (lde_acl_check(acl_name, af, prefix, plen) == FILTER_PERMIT) { | |
2077 | /* for this fec change any imp-null to exp-null */ | |
2078 | if (fn->local_label == MPLS_LABEL_IMPLICIT_NULL) { | |
2079 | fn->local_label= lde_update_label(fn); | |
2080 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
2081 | lde_send_labelmapping(ln, fn, 0); | |
2082 | } | |
2083 | } else { | |
2084 | /* for this fec change any exp-null back to imp-null */ | |
2085 | if (fn->local_label == exp_label) { | |
2086 | fn->local_label = lde_update_label(fn); | |
2087 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
2088 | lde_send_labelmapping(ln, fn, 0); | |
2089 | } | |
2090 | } | |
2091 | } | |
2092 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
2093 | lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0, | |
2094 | NULL, 0); | |
2095 | } | |
2096 | ||
8429abe0 RW |
2097 | static int |
2098 | lde_address_add(struct lde_nbr *ln, struct lde_addr *lde_addr) | |
2099 | { | |
2100 | struct lde_addr *new; | |
2101 | ||
2102 | if (lde_address_find(ln, lde_addr->af, &lde_addr->addr) != NULL) | |
2103 | return (-1); | |
2104 | ||
2105 | if ((new = calloc(1, sizeof(*new))) == NULL) | |
2106 | fatal(__func__); | |
2107 | ||
2108 | new->af = lde_addr->af; | |
2109 | new->addr = lde_addr->addr; | |
2110 | TAILQ_INSERT_TAIL(&ln->addr_list, new, entry); | |
2111 | ||
2112 | /* reevaluate the previously received mappings from this neighbor */ | |
2113 | lde_nbr_addr_update(ln, lde_addr, 0); | |
2114 | ||
2115 | return (0); | |
2116 | } | |
2117 | ||
2118 | static int | |
2119 | lde_address_del(struct lde_nbr *ln, struct lde_addr *lde_addr) | |
2120 | { | |
2121 | lde_addr = lde_address_find(ln, lde_addr->af, &lde_addr->addr); | |
2122 | if (lde_addr == NULL) | |
2123 | return (-1); | |
2124 | ||
2125 | /* reevaluate the previously received mappings from this neighbor */ | |
2126 | lde_nbr_addr_update(ln, lde_addr, 1); | |
2127 | ||
2128 | TAILQ_REMOVE(&ln->addr_list, lde_addr, entry); | |
2129 | free(lde_addr); | |
2130 | ||
2131 | return (0); | |
2132 | } | |
2133 | ||
2134 | struct lde_addr * | |
2135 | lde_address_find(struct lde_nbr *ln, int af, union ldpd_addr *addr) | |
2136 | { | |
2137 | struct lde_addr *lde_addr; | |
2138 | ||
2139 | TAILQ_FOREACH(lde_addr, &ln->addr_list, entry) | |
2140 | if (lde_addr->af == af && | |
2141 | ldp_addrcmp(af, &lde_addr->addr, addr) == 0) | |
2142 | return (lde_addr); | |
2143 | ||
2144 | return (NULL); | |
2145 | } | |
2146 | ||
2147 | static void | |
2148 | lde_address_list_free(struct lde_nbr *ln) | |
2149 | { | |
2150 | struct lde_addr *lde_addr; | |
2151 | ||
a43ad4fe | 2152 | while ((lde_addr = TAILQ_POP_FIRST(&ln->addr_list, entry)) != NULL) |
8429abe0 | 2153 | free(lde_addr); |
8429abe0 | 2154 | } |
fea12efb | 2155 | |
9d694b0b MS |
2156 | /* |
2157 | * Event callback used to retry the label-manager sync zapi session. | |
2158 | */ | |
cc9f21da | 2159 | static void zclient_sync_retry(struct thread *thread) |
9d694b0b MS |
2160 | { |
2161 | zclient_sync_init(); | |
9d694b0b MS |
2162 | } |
2163 | ||
2164 | /* | |
2165 | * Initialize and open a synchronous zapi session. This is used by label chunk | |
2166 | * management code, which acquires and releases blocks of labels from the | |
2167 | * zebra label-manager module. | |
2168 | */ | |
2169 | static void zclient_sync_init(void) | |
5afba51d | 2170 | { |
17da84a4 | 2171 | struct zclient_options options = zclient_options_default; |
9d694b0b | 2172 | |
17da84a4 KS |
2173 | options.synchronous = true; |
2174 | ||
5afba51d | 2175 | /* Initialize special zclient for synchronous message exchanges. */ |
a243d1db | 2176 | zclient_sync = zclient_new(master, &options, NULL, 0); |
5afba51d RW |
2177 | zclient_sync->sock = -1; |
2178 | zclient_sync->redist_default = ZEBRA_ROUTE_LDP; | |
4cebdb9b | 2179 | zclient_sync->session_id = 1; /* Distinguish from main session */ |
342213ea DS |
2180 | zclient_sync->privs = &lde_privs; |
2181 | ||
9d694b0b | 2182 | if (zclient_socket_connect(zclient_sync) < 0) { |
5afba51d | 2183 | log_warnx("Error connecting synchronous zclient!"); |
9d694b0b | 2184 | goto retry; |
5afba51d | 2185 | } |
fa84d193 DL |
2186 | /* make socket non-blocking */ |
2187 | sock_set_nonblock(zclient_sync->sock); | |
5afba51d | 2188 | |
17da84a4 | 2189 | /* Send hello to notify zebra this is a synchronous client */ |
7cfdb485 | 2190 | if (zclient_send_hello(zclient_sync) == ZCLIENT_SEND_FAILURE) { |
17da84a4 | 2191 | log_warnx("Error sending hello for synchronous zclient!"); |
9d694b0b | 2192 | goto retry; |
17da84a4 KS |
2193 | } |
2194 | ||
5afba51d | 2195 | /* Connect to label manager */ |
9d694b0b | 2196 | if (lm_label_manager_connect(zclient_sync, 0) != 0) { |
5afba51d | 2197 | log_warnx("Error connecting to label manager!"); |
9d694b0b | 2198 | goto retry; |
5afba51d | 2199 | } |
9d694b0b MS |
2200 | |
2201 | /* Finish label-manager init once the LM session is running */ | |
2202 | lde_label_list_init(); | |
2203 | ||
2204 | return; | |
2205 | ||
2206 | retry: | |
2207 | ||
2208 | /* Discard failed zclient object */ | |
2209 | zclient_stop(zclient_sync); | |
2210 | zclient_free(zclient_sync); | |
2211 | zclient_sync = NULL; | |
2212 | ||
2213 | /* Retry using a timer */ | |
2214 | thread_add_timer(master, zclient_sync_retry, NULL, 1, NULL); | |
5afba51d RW |
2215 | } |
2216 | ||
fea12efb | 2217 | static void |
2218 | lde_del_label_chunk(void *val) | |
2219 | { | |
2220 | free(val); | |
2221 | } | |
5afba51d | 2222 | |
f4ec681c BA |
2223 | static int |
2224 | lde_release_label_chunk(uint32_t start, uint32_t end) | |
2225 | { | |
2226 | int ret; | |
2227 | ||
2228 | ret = lm_release_label_chunk(zclient_sync, start, end); | |
2229 | if (ret < 0) { | |
2230 | log_warnx("Error releasing label chunk!"); | |
2231 | return (-1); | |
2232 | } | |
2233 | return (0); | |
2234 | } | |
2235 | ||
fea12efb | 2236 | static int |
2237 | lde_get_label_chunk(void) | |
2238 | { | |
5afba51d RW |
2239 | int ret; |
2240 | uint32_t start, end; | |
fea12efb | 2241 | |
08e4b244 | 2242 | debug_labels("getting label chunk (size %u)", CHUNK_SIZE); |
0e3b6a92 EDP |
2243 | ret = lm_get_label_chunk(zclient_sync, 0, MPLS_LABEL_BASE_ANY, |
2244 | CHUNK_SIZE, &start, &end); | |
5afba51d | 2245 | if (ret < 0) { |
fea12efb | 2246 | log_warnx("Error getting label chunk!"); |
fea12efb | 2247 | return -1; |
2248 | } | |
2249 | ||
2250 | on_get_label_chunk_response(start, end); | |
2251 | ||
5afba51d | 2252 | return (0); |
fea12efb | 2253 | } |
5afba51d | 2254 | |
fea12efb | 2255 | static void |
2256 | lde_label_list_init(void) | |
2257 | { | |
2258 | label_chunk_list = list_new(); | |
2259 | label_chunk_list->del = lde_del_label_chunk; | |
2260 | ||
2261 | /* get first chunk */ | |
73be9aeb | 2262 | while (lde_get_label_chunk () != 0) { |
f2232fdf | 2263 | log_warnx("Error getting first label chunk!"); |
d1fcf957 | 2264 | sleep(1); |
fea12efb | 2265 | } |
2266 | } | |
2267 | ||
2268 | static void | |
2269 | on_get_label_chunk_response(uint32_t start, uint32_t end) | |
2270 | { | |
2271 | struct label_chunk *new_label_chunk; | |
2272 | ||
08e4b244 | 2273 | debug_labels("label chunk assign: %u - %u", start, end); |
fea12efb | 2274 | |
2275 | new_label_chunk = calloc(1, sizeof(struct label_chunk)); | |
66749b59 | 2276 | if (!new_label_chunk) { |
2277 | log_warn("Error trying to allocate label chunk %u - %u", start, end); | |
2278 | return; | |
2279 | } | |
fea12efb | 2280 | |
2281 | new_label_chunk->start = start; | |
2282 | new_label_chunk->end = end; | |
2283 | new_label_chunk->used_mask = 0; | |
2284 | ||
2285 | listnode_add(label_chunk_list, (void *)new_label_chunk); | |
2286 | ||
2287 | /* let's update current if needed */ | |
2288 | if (!current_label_chunk) | |
2289 | current_label_chunk = listtail(label_chunk_list); | |
2290 | } | |
2291 | ||
f4ec681c BA |
2292 | void |
2293 | lde_free_label(uint32_t label) | |
2294 | { | |
2295 | struct listnode *node; | |
2296 | struct label_chunk *label_chunk; | |
2297 | uint64_t pos; | |
2298 | ||
2299 | for (ALL_LIST_ELEMENTS_RO(label_chunk_list, node, label_chunk)) { | |
2300 | if (label <= label_chunk->end && label >= label_chunk->start) { | |
2301 | pos = 1ULL << (label - label_chunk->start); | |
2302 | label_chunk->used_mask &= ~pos; | |
2303 | /* if nobody is using this chunk and it's not current_label_chunk, then free it */ | |
2304 | if (!label_chunk->used_mask && (current_label_chunk != node)) { | |
2305 | if (lde_release_label_chunk(label_chunk->start, label_chunk->end) != 0) | |
2306 | log_warnx("%s: Error releasing label chunk!", __func__); | |
2307 | else { | |
2308 | listnode_delete(label_chunk_list, label_chunk); | |
2309 | lde_del_label_chunk(label_chunk); | |
2310 | } | |
2311 | } | |
2312 | break; | |
2313 | } | |
2314 | } | |
2315 | return; | |
2316 | } | |
2317 | ||
fea12efb | 2318 | static uint32_t |
2319 | lde_get_next_label(void) | |
2320 | { | |
5afba51d | 2321 | struct label_chunk *label_chunk; |
2a178cdd RW |
2322 | uint32_t i, size; |
2323 | uint64_t pos; | |
5afba51d | 2324 | uint32_t label = NO_LABEL; |
fea12efb | 2325 | |
2326 | while (current_label_chunk) { | |
2327 | label_chunk = listgetdata(current_label_chunk); | |
2328 | if (!label_chunk) | |
2329 | goto end; | |
2330 | ||
2331 | /* try to get next free label in currently used label chunk */ | |
2332 | size = label_chunk->end - label_chunk->start + 1; | |
2333 | for (i = 0, pos = 1; i < size; i++, pos <<= 1) { | |
2334 | if (!(pos & label_chunk->used_mask)) { | |
2335 | label_chunk->used_mask |= pos; | |
2336 | label = label_chunk->start + i; | |
2337 | goto end; | |
2338 | } | |
2339 | } | |
2340 | current_label_chunk = listnextnode(current_label_chunk); | |
2341 | } | |
2342 | ||
2343 | end: | |
2344 | /* we moved till the last chunk, or were not able to find a label, | |
2345 | so let's ask for another one */ | |
5afba51d RW |
2346 | if (!current_label_chunk || |
2347 | current_label_chunk == listtail(label_chunk_list) || | |
2348 | label == NO_LABEL) { | |
fea12efb | 2349 | if (lde_get_label_chunk() != 0) |
2350 | log_warn("%s: Error getting label chunk!", __func__); | |
2351 | ||
2352 | } | |
2353 | ||
5afba51d | 2354 | return (label); |
fea12efb | 2355 | } |
2d1aa1e8 | 2356 | |
2357 | static void | |
2358 | lde_check_filter_af(int af, struct ldpd_af_conf *af_conf, | |
2359 | const char *filter_name) | |
2360 | { | |
2361 | if (strcmp(af_conf->acl_label_allocate_for, filter_name) == 0) | |
2362 | lde_change_allocate_filter(af); | |
2363 | if ((strcmp(af_conf->acl_label_advertise_to, filter_name) == 0) | |
2364 | || (strcmp(af_conf->acl_label_advertise_for, filter_name) == 0)) | |
2365 | lde_change_advertise_filter(af); | |
2366 | if ((strcmp(af_conf->acl_label_accept_for, filter_name) == 0) | |
2367 | || (strcmp(af_conf->acl_label_accept_from, filter_name) == 0)) | |
2368 | lde_change_accept_filter(af); | |
2369 | if (strcmp(af_conf->acl_label_expnull_for, filter_name) == 0) | |
2370 | lde_change_expnull_for_filter(af); | |
2371 | } | |
95535717 | 2372 | |
2373 | void lde_route_update(struct iface *iface, int af) | |
2374 | { | |
2375 | struct fec *f; | |
2376 | struct fec_node *fn; | |
2377 | struct fec_nh *fnh; | |
2378 | struct lde_nbr *ln; | |
2379 | ||
2380 | /* update label of non-connected routes */ | |
2381 | log_debug("update labels for interface %s", iface->name); | |
2382 | RB_FOREACH(f, fec_tree, &ft) { | |
2383 | fn = (struct fec_node *)f; | |
2384 | if (IS_MPLS_UNRESERVED_LABEL(fn->local_label)) | |
2385 | continue; | |
2386 | ||
2387 | switch (af) { | |
2388 | case AF_INET: | |
2389 | if (fn->fec.type != FEC_TYPE_IPV4) | |
2390 | continue; | |
2391 | break; | |
2392 | case AF_INET6: | |
2393 | if (fn->fec.type != FEC_TYPE_IPV6) | |
2394 | continue; | |
2395 | break; | |
2396 | default: | |
2397 | /* unspecified so process both address families */ | |
2398 | break; | |
2399 | } | |
2400 | ||
2401 | LIST_FOREACH(fnh, &fn->nexthops, entry) { | |
2402 | /* | |
2403 | * If connected leave existing label. If LDP | |
2404 | * configured on interface or a static route | |
2405 | * may need new label. If no LDP configured | |
2406 | * treat fec as a connected route | |
2407 | */ | |
2408 | if (fnh->flags & F_FEC_NH_CONNECTED) | |
2409 | break; | |
2410 | ||
2411 | if (fnh->ifindex != iface->ifindex) | |
2412 | continue; | |
2413 | ||
2414 | fnh->flags &= ~F_FEC_NH_NO_LDP; | |
2415 | if (IS_MPLS_RESERVED_LABEL(fn->local_label)) { | |
2416 | fn->local_label = NO_LABEL; | |
2417 | fn->local_label = lde_update_label(fn); | |
2418 | if (fn->local_label != NO_LABEL) | |
2419 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
2420 | lde_send_labelmapping( | |
2421 | ln, fn, 0); | |
2422 | } | |
2423 | break; | |
2424 | } | |
2425 | } | |
2426 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
2427 | lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, | |
2428 | 0, NULL, 0); | |
2429 | } | |
2430 | ||
2431 | void lde_route_update_release(struct iface *iface, int af) | |
2432 | { | |
2433 | struct lde_nbr *ln; | |
2434 | struct fec *f; | |
2435 | struct fec_node *fn; | |
2436 | struct fec_nh *fnh; | |
2437 | ||
2438 | /* update label of interfaces no longer running LDP */ | |
2439 | log_debug("release all labels for interface %s af %s", iface->name, | |
2440 | af == AF_INET ? "ipv4" : "ipv6"); | |
2441 | RB_FOREACH(f, fec_tree, &ft) { | |
2442 | fn = (struct fec_node *)f; | |
2443 | ||
2444 | switch (af) { | |
2445 | case AF_INET: | |
2446 | if (fn->fec.type != FEC_TYPE_IPV4) | |
2447 | continue; | |
2448 | break; | |
2449 | case AF_INET6: | |
2450 | if (fn->fec.type != FEC_TYPE_IPV6) | |
2451 | continue; | |
2452 | break; | |
2453 | default: | |
2454 | fatalx("lde_route_update_release: unknown af"); | |
2455 | } | |
2456 | ||
2457 | if (fn->local_label == NO_LABEL) | |
2458 | continue; | |
2459 | ||
2460 | LIST_FOREACH(fnh, &fn->nexthops, entry) { | |
2461 | /* | |
2462 | * If connected leave existing label. If LDP | |
2463 | * removed from interface may need new label | |
2464 | * and would be treated as a connected route | |
2465 | */ | |
2466 | if (fnh->flags & F_FEC_NH_CONNECTED) | |
2467 | break; | |
2468 | ||
2469 | if (fnh->ifindex != iface->ifindex) | |
2470 | continue; | |
2471 | ||
2472 | fnh->flags |= F_FEC_NH_NO_LDP; | |
2473 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
2474 | lde_send_labelwithdraw(ln, fn, NULL, NULL); | |
2475 | lde_free_label(fn->local_label); | |
2476 | fn->local_label = NO_LABEL; | |
2477 | fn->local_label = lde_update_label(fn); | |
2478 | if (fn->local_label != NO_LABEL) | |
2479 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
2480 | lde_send_labelmapping(ln, fn, 0); | |
2481 | break; | |
2482 | } | |
2483 | } | |
2484 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
2485 | lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, | |
2486 | 0, NULL, 0); | |
2487 | } | |
2488 | ||
2489 | void lde_route_update_release_all(int af) | |
2490 | { | |
2491 | struct lde_nbr *ln; | |
2492 | struct fec *f; | |
2493 | struct fec_node *fn; | |
2494 | struct fec_nh *fnh; | |
2495 | ||
2496 | /* remove labels from all interfaces as LDP is no longer running for | |
2497 | * this address family | |
2498 | */ | |
2499 | log_debug("release all labels for address family %s", | |
2500 | af == AF_INET ? "ipv4" : "ipv6"); | |
2501 | RB_FOREACH(f, fec_tree, &ft) { | |
2502 | fn = (struct fec_node *)f; | |
2503 | switch (af) { | |
2504 | case AF_INET: | |
2505 | if (fn->fec.type != FEC_TYPE_IPV4) | |
2506 | continue; | |
2507 | break; | |
2508 | case AF_INET6: | |
2509 | if (fn->fec.type != FEC_TYPE_IPV6) | |
2510 | continue; | |
2511 | break; | |
2512 | default: | |
2513 | fatalx("lde_route_update_release: unknown af"); | |
2514 | } | |
2515 | ||
2516 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
2517 | lde_send_labelwithdraw(ln, fn, NULL, NULL); | |
2518 | ||
2519 | LIST_FOREACH(fnh, &fn->nexthops, entry) { | |
2520 | fnh->flags |= F_FEC_NH_NO_LDP; | |
2521 | lde_send_delete_klabel(fn, fnh); | |
2522 | } | |
2523 | } | |
2524 | } |