]>
Commit | Line | Data |
---|---|---|
8429abe0 RW |
1 | /* $OpenBSD$ */ |
2 | ||
3 | /* | |
4 | * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org> | |
5 | * Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org> | |
6 | * Copyright (c) 2004 Esben Norby <norby@openbsd.org> | |
7 | * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> | |
8 | * | |
9 | * Permission to use, copy, modify, and distribute this software for any | |
10 | * purpose with or without fee is hereby granted, provided that the above | |
11 | * copyright notice and this permission notice appear in all copies. | |
12 | * | |
13 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
14 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
15 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
16 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
17 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
18 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
19 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
20 | */ | |
21 | ||
eac6e3f0 | 22 | #include <zebra.h> |
8429abe0 RW |
23 | |
24 | #include "ldp.h" | |
25 | #include "ldpd.h" | |
26 | #include "ldpe.h" | |
27 | #include "log.h" | |
28 | #include "lde.h" | |
eac6e3f0 | 29 | #include "ldp_debug.h" |
8429abe0 | 30 | |
eac6e3f0 RW |
31 | #include <lib/log.h> |
32 | #include "memory.h" | |
33 | #include "privs.h" | |
34 | #include "sigevent.h" | |
35 | #include "mpls.h" | |
fea12efb | 36 | #include <lib/linklist.h> |
37 | #include "zclient.h" | |
38 | #include "stream.h" | |
39 | #include "network.h" | |
689f5a8c | 40 | #include "libfrr.h" |
eac6e3f0 RW |
41 | |
42 | static void lde_shutdown(void); | |
43 | static int lde_dispatch_imsg(struct thread *); | |
44 | static int lde_dispatch_parent(struct thread *); | |
45926e58 RZ |
45 | static __inline int lde_nbr_compare(const struct lde_nbr *, |
46 | const struct lde_nbr *); | |
8429abe0 RW |
47 | static struct lde_nbr *lde_nbr_new(uint32_t, struct lde_nbr *); |
48 | static void lde_nbr_del(struct lde_nbr *); | |
49 | static struct lde_nbr *lde_nbr_find(uint32_t); | |
50 | static void lde_nbr_clear(void); | |
51 | static void lde_nbr_addr_update(struct lde_nbr *, | |
52 | struct lde_addr *, int); | |
45926e58 RZ |
53 | static __inline int lde_map_compare(const struct lde_map *, |
54 | const struct lde_map *); | |
8429abe0 RW |
55 | static void lde_map_free(void *); |
56 | static int lde_address_add(struct lde_nbr *, struct lde_addr *); | |
57 | static int lde_address_del(struct lde_nbr *, struct lde_addr *); | |
58 | static void lde_address_list_free(struct lde_nbr *); | |
d7c0a89a | 59 | static void zclient_sync_init(unsigned short instance); |
fea12efb | 60 | static void lde_label_list_init(void); |
5afba51d | 61 | static int lde_get_label_chunk(void); |
fea12efb | 62 | static void on_get_label_chunk_response(uint32_t start, uint32_t end); |
63 | static uint32_t lde_get_next_label(void); | |
8429abe0 RW |
64 | |
65 | RB_GENERATE(nbr_tree, lde_nbr, entry, lde_nbr_compare) | |
d3e1887a | 66 | RB_GENERATE(lde_map_head, lde_map, entry, lde_map_compare) |
8429abe0 RW |
67 | |
68 | struct ldpd_conf *ldeconf; | |
69 | struct nbr_tree lde_nbrs = RB_INITIALIZER(&lde_nbrs); | |
70 | ||
71 | static struct imsgev *iev_ldpe; | |
28e8294c | 72 | static struct imsgev *iev_main, *iev_main_sync; |
8429abe0 | 73 | |
eac6e3f0 RW |
74 | /* Master of threads. */ |
75 | struct thread_master *master; | |
76 | ||
77 | /* lde privileges */ | |
78 | static zebra_capabilities_t _caps_p [] = | |
8429abe0 | 79 | { |
342213ea | 80 | ZCAP_NET_ADMIN |
eac6e3f0 | 81 | }; |
8429abe0 | 82 | |
eac6e3f0 RW |
83 | static struct zebra_privs_t lde_privs = |
84 | { | |
eac6e3f0 RW |
85 | #if defined(VTY_GROUP) |
86 | .vty_group = VTY_GROUP, | |
87 | #endif | |
88 | .caps_p = _caps_p, | |
89 | .cap_num_p = array_size(_caps_p), | |
90 | .cap_num_i = 0 | |
91 | }; | |
92 | ||
fea12efb | 93 | /* List of chunks of labels externally assigned by Zebra */ |
5afba51d RW |
94 | static struct list *label_chunk_list; |
95 | static struct listnode *current_label_chunk; | |
96 | ||
97 | /* Synchronous zclient to request labels */ | |
98 | static struct zclient *zclient_sync; | |
fea12efb | 99 | |
eac6e3f0 RW |
100 | /* SIGINT / SIGTERM handler. */ |
101 | static void | |
102 | sigint(void) | |
103 | { | |
104 | lde_shutdown(); | |
8429abe0 RW |
105 | } |
106 | ||
eac6e3f0 RW |
107 | static struct quagga_signal_t lde_signals[] = |
108 | { | |
1e7e440f RW |
109 | { |
110 | .signal = SIGHUP, | |
111 | /* ignore */ | |
112 | }, | |
eac6e3f0 RW |
113 | { |
114 | .signal = SIGINT, | |
115 | .handler = &sigint, | |
116 | }, | |
117 | { | |
118 | .signal = SIGTERM, | |
119 | .handler = &sigint, | |
120 | }, | |
121 | }; | |
122 | ||
8429abe0 RW |
123 | /* label decision engine */ |
124 | void | |
274f5abf | 125 | lde(void) |
8429abe0 | 126 | { |
eac6e3f0 | 127 | struct thread thread; |
8429abe0 | 128 | |
eac6e3f0 | 129 | #ifdef HAVE_SETPROCTITLE |
8429abe0 | 130 | setproctitle("label decision engine"); |
eac6e3f0 | 131 | #endif |
8429abe0 | 132 | ldpd_process = PROC_LDE_ENGINE; |
fa68f9da | 133 | log_procname = log_procnames[PROC_LDE_ENGINE]; |
8429abe0 | 134 | |
972a411c | 135 | master = thread_master_create(NULL); |
8429abe0 RW |
136 | |
137 | /* setup signal handler */ | |
eac6e3f0 | 138 | signal_init(master, array_size(lde_signals), lde_signals); |
8429abe0 | 139 | |
28e8294c RW |
140 | /* setup pipes and event handlers to the parent process */ |
141 | if ((iev_main = calloc(1, sizeof(struct imsgev))) == NULL) | |
8429abe0 | 142 | fatal(NULL); |
28e8294c | 143 | imsg_init(&iev_main->ibuf, LDPD_FD_ASYNC); |
eac6e3f0 | 144 | iev_main->handler_read = lde_dispatch_parent; |
66e78ae6 QY |
145 | iev_main->ev_read = NULL; |
146 | thread_add_read(master, iev_main->handler_read, iev_main, iev_main->ibuf.fd, | |
147 | &iev_main->ev_read); | |
eac6e3f0 | 148 | iev_main->handler_write = ldp_write_handler; |
28e8294c RW |
149 | |
150 | if ((iev_main_sync = calloc(1, sizeof(struct imsgev))) == NULL) | |
151 | fatal(NULL); | |
152 | imsg_init(&iev_main_sync->ibuf, LDPD_FD_SYNC); | |
eac6e3f0 | 153 | |
274f5abf RW |
154 | /* create base configuration */ |
155 | ldeconf = config_new_empty(); | |
156 | ||
157 | /* Fetch next active thread. */ | |
158 | while (thread_fetch(master, &thread)) | |
159 | thread_call(&thread); | |
160 | } | |
161 | ||
162 | void | |
163 | lde_init(struct ldpd_init *init) | |
164 | { | |
165 | /* drop privileges */ | |
af7e63a3 RW |
166 | lde_privs.user = init->user; |
167 | lde_privs.group = init->group; | |
37a1f2fb | 168 | zprivs_preinit(&lde_privs); |
274f5abf RW |
169 | zprivs_init(&lde_privs); |
170 | ||
eac6e3f0 | 171 | /* start the LIB garbage collector */ |
8429abe0 RW |
172 | lde_gc_start_timer(); |
173 | ||
fea12efb | 174 | /* Init synchronous zclient and label list */ |
689f5a8c DL |
175 | frr_zclient_addr(&zclient_addr, &zclient_addr_len, |
176 | init->zclient_serv_path); | |
274f5abf | 177 | zclient_sync_init(init->instance); |
fea12efb | 178 | lde_label_list_init(); |
8429abe0 RW |
179 | } |
180 | ||
eac6e3f0 | 181 | static void |
8429abe0 RW |
182 | lde_shutdown(void) |
183 | { | |
184 | /* close pipes */ | |
835a7376 RW |
185 | if (iev_ldpe) { |
186 | msgbuf_clear(&iev_ldpe->ibuf.w); | |
187 | close(iev_ldpe->ibuf.fd); | |
50732983 | 188 | iev_ldpe->ibuf.fd = -1; |
835a7376 | 189 | } |
8429abe0 RW |
190 | msgbuf_clear(&iev_main->ibuf.w); |
191 | close(iev_main->ibuf.fd); | |
50732983 | 192 | iev_main->ibuf.fd = -1; |
28e8294c RW |
193 | msgbuf_clear(&iev_main_sync->ibuf.w); |
194 | close(iev_main_sync->ibuf.fd); | |
50732983 | 195 | iev_main_sync->ibuf.fd = -1; |
8429abe0 RW |
196 | |
197 | lde_gc_stop_timer(); | |
198 | lde_nbr_clear(); | |
199 | fec_tree_clear(); | |
200 | ||
201 | config_clear(ldeconf); | |
202 | ||
835a7376 RW |
203 | if (iev_ldpe) |
204 | free(iev_ldpe); | |
8429abe0 | 205 | free(iev_main); |
28e8294c | 206 | free(iev_main_sync); |
8429abe0 RW |
207 | |
208 | log_info("label decision engine exiting"); | |
209 | exit(0); | |
210 | } | |
211 | ||
212 | /* imesg */ | |
eac6e3f0 | 213 | int |
8429abe0 RW |
214 | lde_imsg_compose_parent(int type, pid_t pid, void *data, uint16_t datalen) |
215 | { | |
50732983 RW |
216 | if (iev_main->ibuf.fd == -1) |
217 | return (0); | |
8429abe0 RW |
218 | return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen)); |
219 | } | |
220 | ||
f2232fdf RW |
221 | void |
222 | lde_imsg_compose_parent_sync(int type, pid_t pid, void *data, uint16_t datalen) | |
223 | { | |
50732983 RW |
224 | if (iev_main_sync->ibuf.fd == -1) |
225 | return; | |
f2232fdf RW |
226 | imsg_compose_event(iev_main_sync, type, 0, pid, -1, data, datalen); |
227 | imsg_flush(&iev_main_sync->ibuf); | |
228 | } | |
229 | ||
8429abe0 RW |
230 | int |
231 | lde_imsg_compose_ldpe(int type, uint32_t peerid, pid_t pid, void *data, | |
232 | uint16_t datalen) | |
233 | { | |
50732983 RW |
234 | if (iev_ldpe->ibuf.fd == -1) |
235 | return (0); | |
8429abe0 RW |
236 | return (imsg_compose_event(iev_ldpe, type, peerid, pid, |
237 | -1, data, datalen)); | |
238 | } | |
239 | ||
240 | /* ARGSUSED */ | |
eac6e3f0 RW |
241 | static int |
242 | lde_dispatch_imsg(struct thread *thread) | |
8429abe0 | 243 | { |
eac6e3f0 | 244 | struct imsgev *iev = THREAD_ARG(thread); |
8429abe0 RW |
245 | struct imsgbuf *ibuf = &iev->ibuf; |
246 | struct imsg imsg; | |
247 | struct lde_nbr *ln; | |
236c6935 RW |
248 | struct map *map; |
249 | struct lde_addr *lde_addr; | |
250 | struct notify_msg *nm; | |
8429abe0 | 251 | ssize_t n; |
eac6e3f0 | 252 | int shut = 0; |
8429abe0 | 253 | |
eac6e3f0 RW |
254 | iev->ev_read = NULL; |
255 | ||
256 | if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) | |
257 | fatal("imsg_read error"); | |
258 | if (n == 0) /* connection closed */ | |
259 | shut = 1; | |
8429abe0 RW |
260 | |
261 | for (;;) { | |
262 | if ((n = imsg_get(ibuf, &imsg)) == -1) | |
263 | fatal("lde_dispatch_imsg: imsg_get error"); | |
264 | if (n == 0) | |
265 | break; | |
266 | ||
267 | switch (imsg.hdr.type) { | |
268 | case IMSG_LABEL_MAPPING_FULL: | |
269 | ln = lde_nbr_find(imsg.hdr.peerid); | |
270 | if (ln == NULL) { | |
271 | log_debug("%s: cannot find lde neighbor", | |
272 | __func__); | |
273 | break; | |
274 | } | |
275 | ||
276 | fec_snap(ln); | |
277 | break; | |
278 | case IMSG_LABEL_MAPPING: | |
279 | case IMSG_LABEL_REQUEST: | |
280 | case IMSG_LABEL_RELEASE: | |
281 | case IMSG_LABEL_WITHDRAW: | |
282 | case IMSG_LABEL_ABORT: | |
236c6935 RW |
283 | if (imsg.hdr.len - IMSG_HEADER_SIZE != |
284 | sizeof(struct map)) | |
8429abe0 | 285 | fatalx("lde_dispatch_imsg: wrong imsg len"); |
236c6935 | 286 | map = imsg.data; |
8429abe0 RW |
287 | |
288 | ln = lde_nbr_find(imsg.hdr.peerid); | |
289 | if (ln == NULL) { | |
290 | log_debug("%s: cannot find lde neighbor", | |
291 | __func__); | |
292 | break; | |
293 | } | |
294 | ||
295 | switch (imsg.hdr.type) { | |
296 | case IMSG_LABEL_MAPPING: | |
236c6935 | 297 | lde_check_mapping(map, ln); |
8429abe0 RW |
298 | break; |
299 | case IMSG_LABEL_REQUEST: | |
236c6935 | 300 | lde_check_request(map, ln); |
8429abe0 RW |
301 | break; |
302 | case IMSG_LABEL_RELEASE: | |
236c6935 | 303 | lde_check_release(map, ln); |
8429abe0 RW |
304 | break; |
305 | case IMSG_LABEL_WITHDRAW: | |
236c6935 | 306 | lde_check_withdraw(map, ln); |
8429abe0 RW |
307 | break; |
308 | case IMSG_LABEL_ABORT: | |
309 | /* not necessary */ | |
310 | break; | |
311 | } | |
312 | break; | |
313 | case IMSG_ADDRESS_ADD: | |
236c6935 RW |
314 | if (imsg.hdr.len - IMSG_HEADER_SIZE != |
315 | sizeof(struct lde_addr)) | |
8429abe0 | 316 | fatalx("lde_dispatch_imsg: wrong imsg len"); |
236c6935 | 317 | lde_addr = imsg.data; |
8429abe0 RW |
318 | |
319 | ln = lde_nbr_find(imsg.hdr.peerid); | |
320 | if (ln == NULL) { | |
321 | log_debug("%s: cannot find lde neighbor", | |
322 | __func__); | |
323 | break; | |
324 | } | |
236c6935 | 325 | if (lde_address_add(ln, lde_addr) < 0) { |
8429abe0 RW |
326 | log_debug("%s: cannot add address %s, it " |
327 | "already exists", __func__, | |
236c6935 | 328 | log_addr(lde_addr->af, &lde_addr->addr)); |
8429abe0 RW |
329 | } |
330 | break; | |
331 | case IMSG_ADDRESS_DEL: | |
236c6935 RW |
332 | if (imsg.hdr.len - IMSG_HEADER_SIZE != |
333 | sizeof(struct lde_addr)) | |
8429abe0 | 334 | fatalx("lde_dispatch_imsg: wrong imsg len"); |
236c6935 | 335 | lde_addr = imsg.data; |
8429abe0 RW |
336 | |
337 | ln = lde_nbr_find(imsg.hdr.peerid); | |
338 | if (ln == NULL) { | |
339 | log_debug("%s: cannot find lde neighbor", | |
340 | __func__); | |
341 | break; | |
342 | } | |
236c6935 | 343 | if (lde_address_del(ln, lde_addr) < 0) { |
8429abe0 RW |
344 | log_debug("%s: cannot delete address %s, it " |
345 | "does not exist", __func__, | |
236c6935 | 346 | log_addr(lde_addr->af, &lde_addr->addr)); |
8429abe0 RW |
347 | } |
348 | break; | |
349 | case IMSG_NOTIFICATION: | |
236c6935 RW |
350 | if (imsg.hdr.len - IMSG_HEADER_SIZE != |
351 | sizeof(struct notify_msg)) | |
8429abe0 | 352 | fatalx("lde_dispatch_imsg: wrong imsg len"); |
236c6935 | 353 | nm = imsg.data; |
8429abe0 RW |
354 | |
355 | ln = lde_nbr_find(imsg.hdr.peerid); | |
356 | if (ln == NULL) { | |
357 | log_debug("%s: cannot find lde neighbor", | |
358 | __func__); | |
359 | break; | |
360 | } | |
361 | ||
236c6935 | 362 | switch (nm->status_code) { |
8429abe0 | 363 | case S_PW_STATUS: |
236c6935 | 364 | l2vpn_recv_pw_status(ln, nm); |
8429abe0 | 365 | break; |
257799cd RW |
366 | case S_ENDOFLIB: |
367 | /* | |
368 | * Do nothing for now. Should be useful in | |
369 | * the future when we implement LDP-IGP | |
370 | * Synchronization (RFC 5443) and Graceful | |
371 | * Restart (RFC 3478). | |
372 | */ | |
8429abe0 RW |
373 | default: |
374 | break; | |
375 | } | |
376 | break; | |
377 | case IMSG_NEIGHBOR_UP: | |
378 | if (imsg.hdr.len - IMSG_HEADER_SIZE != | |
379 | sizeof(struct lde_nbr)) | |
380 | fatalx("lde_dispatch_imsg: wrong imsg len"); | |
381 | ||
382 | if (lde_nbr_find(imsg.hdr.peerid)) | |
383 | fatalx("lde_dispatch_imsg: " | |
384 | "neighbor already exists"); | |
385 | lde_nbr_new(imsg.hdr.peerid, imsg.data); | |
386 | break; | |
387 | case IMSG_NEIGHBOR_DOWN: | |
388 | lde_nbr_del(lde_nbr_find(imsg.hdr.peerid)); | |
389 | break; | |
390 | case IMSG_CTL_SHOW_LIB: | |
391 | rt_dump(imsg.hdr.pid); | |
392 | ||
393 | lde_imsg_compose_ldpe(IMSG_CTL_END, 0, | |
394 | imsg.hdr.pid, NULL, 0); | |
395 | break; | |
396 | case IMSG_CTL_SHOW_L2VPN_PW: | |
397 | l2vpn_pw_ctl(imsg.hdr.pid); | |
398 | ||
399 | lde_imsg_compose_ldpe(IMSG_CTL_END, 0, | |
400 | imsg.hdr.pid, NULL, 0); | |
401 | break; | |
402 | case IMSG_CTL_SHOW_L2VPN_BINDING: | |
403 | l2vpn_binding_ctl(imsg.hdr.pid); | |
404 | ||
405 | lde_imsg_compose_ldpe(IMSG_CTL_END, 0, | |
406 | imsg.hdr.pid, NULL, 0); | |
407 | break; | |
8429abe0 RW |
408 | default: |
409 | log_debug("%s: unexpected imsg %d", __func__, | |
410 | imsg.hdr.type); | |
411 | break; | |
412 | } | |
413 | imsg_free(&imsg); | |
414 | } | |
415 | if (!shut) | |
416 | imsg_event_add(iev); | |
417 | else { | |
eac6e3f0 RW |
418 | /* this pipe is dead, so remove the event handlers and exit */ |
419 | THREAD_READ_OFF(iev->ev_read); | |
420 | THREAD_WRITE_OFF(iev->ev_write); | |
421 | lde_shutdown(); | |
8429abe0 | 422 | } |
eac6e3f0 RW |
423 | |
424 | return (0); | |
8429abe0 RW |
425 | } |
426 | ||
427 | /* ARGSUSED */ | |
eac6e3f0 RW |
428 | static int |
429 | lde_dispatch_parent(struct thread *thread) | |
8429abe0 RW |
430 | { |
431 | static struct ldpd_conf *nconf; | |
52b530fc | 432 | struct iface *iface, *niface; |
8429abe0 RW |
433 | struct tnbr *ntnbr; |
434 | struct nbr_params *nnbrp; | |
52b530fc RW |
435 | static struct l2vpn *l2vpn, *nl2vpn; |
436 | struct l2vpn_if *lif, *nlif; | |
437 | struct l2vpn_pw *pw, *npw; | |
8429abe0 | 438 | struct imsg imsg; |
52b530fc | 439 | struct kif *kif; |
236c6935 | 440 | struct kroute *kr; |
11bf8e13 | 441 | int fd; |
eac6e3f0 | 442 | struct imsgev *iev = THREAD_ARG(thread); |
8429abe0 RW |
443 | struct imsgbuf *ibuf = &iev->ibuf; |
444 | ssize_t n; | |
445 | int shut = 0; | |
446 | struct fec fec; | |
447 | ||
eac6e3f0 RW |
448 | iev->ev_read = NULL; |
449 | ||
450 | if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) | |
451 | fatal("imsg_read error"); | |
452 | if (n == 0) /* connection closed */ | |
453 | shut = 1; | |
8429abe0 RW |
454 | |
455 | for (;;) { | |
456 | if ((n = imsg_get(ibuf, &imsg)) == -1) | |
457 | fatal("lde_dispatch_parent: imsg_get error"); | |
458 | if (n == 0) | |
459 | break; | |
460 | ||
461 | switch (imsg.hdr.type) { | |
52b530fc RW |
462 | case IMSG_IFSTATUS: |
463 | if (imsg.hdr.len != IMSG_HEADER_SIZE + | |
464 | sizeof(struct kif)) | |
465 | fatalx("IFSTATUS imsg with wrong len"); | |
466 | kif = imsg.data; | |
467 | ||
468 | iface = if_lookup_name(ldeconf, kif->ifname); | |
469 | if (iface) { | |
470 | if_update_info(iface, kif); | |
471 | break; | |
472 | } | |
473 | ||
474 | RB_FOREACH(l2vpn, l2vpn_head, &ldeconf->l2vpn_tree) { | |
475 | lif = l2vpn_if_find(l2vpn, kif->ifname); | |
476 | if (lif) { | |
477 | l2vpn_if_update_info(lif, kif); | |
478 | break; | |
479 | } | |
480 | pw = l2vpn_pw_find(l2vpn, kif->ifname); | |
481 | if (pw) { | |
482 | l2vpn_pw_update_info(pw, kif); | |
483 | break; | |
484 | } | |
485 | } | |
486 | break; | |
87b5f1b7 RW |
487 | case IMSG_PW_UPDATE: |
488 | if (imsg.hdr.len != IMSG_HEADER_SIZE + | |
489 | sizeof(struct zapi_pw_status)) | |
490 | fatalx("PW_UPDATE imsg with wrong len"); | |
491 | ||
492 | if (l2vpn_pw_status_update(imsg.data) != 0) | |
493 | log_warnx("%s: error updating PW status", | |
494 | __func__); | |
495 | break; | |
8429abe0 | 496 | case IMSG_NETWORK_ADD: |
8cb1fc45 | 497 | case IMSG_NETWORK_UPDATE: |
236c6935 RW |
498 | if (imsg.hdr.len != IMSG_HEADER_SIZE + |
499 | sizeof(struct kroute)) { | |
8429abe0 RW |
500 | log_warnx("%s: wrong imsg len", __func__); |
501 | break; | |
502 | } | |
236c6935 | 503 | kr = imsg.data; |
8429abe0 | 504 | |
236c6935 | 505 | switch (kr->af) { |
8429abe0 RW |
506 | case AF_INET: |
507 | fec.type = FEC_TYPE_IPV4; | |
236c6935 RW |
508 | fec.u.ipv4.prefix = kr->prefix.v4; |
509 | fec.u.ipv4.prefixlen = kr->prefixlen; | |
8429abe0 RW |
510 | break; |
511 | case AF_INET6: | |
512 | fec.type = FEC_TYPE_IPV6; | |
236c6935 RW |
513 | fec.u.ipv6.prefix = kr->prefix.v6; |
514 | fec.u.ipv6.prefixlen = kr->prefixlen; | |
8429abe0 RW |
515 | break; |
516 | default: | |
517 | fatalx("lde_dispatch_parent: unknown af"); | |
518 | } | |
519 | ||
520 | switch (imsg.hdr.type) { | |
521 | case IMSG_NETWORK_ADD: | |
236c6935 RW |
522 | lde_kernel_insert(&fec, kr->af, &kr->nexthop, |
523 | kr->ifindex, kr->priority, | |
524 | kr->flags & F_CONNECTED, NULL); | |
8429abe0 | 525 | break; |
8cb1fc45 RW |
526 | case IMSG_NETWORK_UPDATE: |
527 | lde_kernel_update(&fec); | |
8429abe0 RW |
528 | break; |
529 | } | |
530 | break; | |
531 | case IMSG_SOCKET_IPC: | |
532 | if (iev_ldpe) { | |
533 | log_warnx("%s: received unexpected imsg fd " | |
534 | "to ldpe", __func__); | |
535 | break; | |
536 | } | |
537 | if ((fd = imsg.fd) == -1) { | |
538 | log_warnx("%s: expected to receive imsg fd to " | |
539 | "ldpe but didn't receive any", __func__); | |
540 | break; | |
541 | } | |
542 | ||
543 | if ((iev_ldpe = malloc(sizeof(struct imsgev))) == NULL) | |
544 | fatal(NULL); | |
545 | imsg_init(&iev_ldpe->ibuf, fd); | |
eac6e3f0 | 546 | iev_ldpe->handler_read = lde_dispatch_imsg; |
66e78ae6 QY |
547 | iev_ldpe->ev_read = NULL; |
548 | thread_add_read(master, iev_ldpe->handler_read, iev_ldpe, iev_ldpe->ibuf.fd, | |
549 | &iev_ldpe->ev_read); | |
eac6e3f0 RW |
550 | iev_ldpe->handler_write = ldp_write_handler; |
551 | iev_ldpe->ev_write = NULL; | |
8429abe0 | 552 | break; |
274f5abf RW |
553 | case IMSG_INIT: |
554 | if (imsg.hdr.len != IMSG_HEADER_SIZE + | |
555 | sizeof(struct ldpd_init)) | |
556 | fatalx("INIT imsg with wrong len"); | |
557 | ||
558 | memcpy(&init, imsg.data, sizeof(init)); | |
559 | lde_init(&init); | |
560 | break; | |
8429abe0 RW |
561 | case IMSG_RECONF_CONF: |
562 | if ((nconf = malloc(sizeof(struct ldpd_conf))) == | |
563 | NULL) | |
564 | fatal(NULL); | |
565 | memcpy(nconf, imsg.data, sizeof(struct ldpd_conf)); | |
566 | ||
45926e58 RZ |
567 | RB_INIT(iface_head, &nconf->iface_tree); |
568 | RB_INIT(tnbr_head, &nconf->tnbr_tree); | |
569 | RB_INIT(nbrp_head, &nconf->nbrp_tree); | |
570 | RB_INIT(l2vpn_head, &nconf->l2vpn_tree); | |
8429abe0 RW |
571 | break; |
572 | case IMSG_RECONF_IFACE: | |
573 | if ((niface = malloc(sizeof(struct iface))) == NULL) | |
574 | fatal(NULL); | |
575 | memcpy(niface, imsg.data, sizeof(struct iface)); | |
576 | ||
7d3d7491 | 577 | RB_INSERT(iface_head, &nconf->iface_tree, niface); |
8429abe0 RW |
578 | break; |
579 | case IMSG_RECONF_TNBR: | |
580 | if ((ntnbr = malloc(sizeof(struct tnbr))) == NULL) | |
581 | fatal(NULL); | |
582 | memcpy(ntnbr, imsg.data, sizeof(struct tnbr)); | |
583 | ||
7989cdba | 584 | RB_INSERT(tnbr_head, &nconf->tnbr_tree, ntnbr); |
8429abe0 RW |
585 | break; |
586 | case IMSG_RECONF_NBRP: | |
587 | if ((nnbrp = malloc(sizeof(struct nbr_params))) == NULL) | |
588 | fatal(NULL); | |
589 | memcpy(nnbrp, imsg.data, sizeof(struct nbr_params)); | |
590 | ||
76c4abd1 | 591 | RB_INSERT(nbrp_head, &nconf->nbrp_tree, nnbrp); |
8429abe0 RW |
592 | break; |
593 | case IMSG_RECONF_L2VPN: | |
594 | if ((nl2vpn = malloc(sizeof(struct l2vpn))) == NULL) | |
595 | fatal(NULL); | |
596 | memcpy(nl2vpn, imsg.data, sizeof(struct l2vpn)); | |
597 | ||
45926e58 RZ |
598 | RB_INIT(l2vpn_if_head, &nl2vpn->if_tree); |
599 | RB_INIT(l2vpn_pw_head, &nl2vpn->pw_tree); | |
600 | RB_INIT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree); | |
8429abe0 | 601 | |
90d7e7bd | 602 | RB_INSERT(l2vpn_head, &nconf->l2vpn_tree, nl2vpn); |
8429abe0 RW |
603 | break; |
604 | case IMSG_RECONF_L2VPN_IF: | |
605 | if ((nlif = malloc(sizeof(struct l2vpn_if))) == NULL) | |
606 | fatal(NULL); | |
607 | memcpy(nlif, imsg.data, sizeof(struct l2vpn_if)); | |
608 | ||
029c1958 | 609 | RB_INSERT(l2vpn_if_head, &nl2vpn->if_tree, nlif); |
8429abe0 RW |
610 | break; |
611 | case IMSG_RECONF_L2VPN_PW: | |
612 | if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL) | |
613 | fatal(NULL); | |
614 | memcpy(npw, imsg.data, sizeof(struct l2vpn_pw)); | |
615 | ||
20bacaeb | 616 | RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_tree, npw); |
8429abe0 | 617 | break; |
eac6e3f0 RW |
618 | case IMSG_RECONF_L2VPN_IPW: |
619 | if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL) | |
620 | fatal(NULL); | |
621 | memcpy(npw, imsg.data, sizeof(struct l2vpn_pw)); | |
622 | ||
20bacaeb | 623 | RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree, npw); |
eac6e3f0 | 624 | break; |
8429abe0 RW |
625 | case IMSG_RECONF_END: |
626 | merge_config(ldeconf, nconf); | |
1d75a89d | 627 | ldp_clear_config(nconf); |
8429abe0 RW |
628 | nconf = NULL; |
629 | break; | |
eac6e3f0 RW |
630 | case IMSG_DEBUG_UPDATE: |
631 | if (imsg.hdr.len != IMSG_HEADER_SIZE + | |
632 | sizeof(ldp_debug)) { | |
633 | log_warnx("%s: wrong imsg len", __func__); | |
634 | break; | |
635 | } | |
636 | memcpy(&ldp_debug, imsg.data, sizeof(ldp_debug)); | |
637 | break; | |
8429abe0 RW |
638 | default: |
639 | log_debug("%s: unexpected imsg %d", __func__, | |
640 | imsg.hdr.type); | |
641 | break; | |
642 | } | |
643 | imsg_free(&imsg); | |
644 | } | |
645 | if (!shut) | |
646 | imsg_event_add(iev); | |
647 | else { | |
eac6e3f0 RW |
648 | /* this pipe is dead, so remove the event handlers and exit */ |
649 | THREAD_READ_OFF(iev->ev_read); | |
650 | THREAD_WRITE_OFF(iev->ev_write); | |
651 | lde_shutdown(); | |
8429abe0 | 652 | } |
eac6e3f0 RW |
653 | |
654 | return (0); | |
8429abe0 RW |
655 | } |
656 | ||
45a8eba9 RW |
657 | int |
658 | lde_acl_check(char *acl_name, int af, union ldpd_addr *addr, uint8_t prefixlen) | |
659 | { | |
660 | return ldp_acl_request(iev_main_sync, acl_name, af, addr, prefixlen); | |
661 | } | |
662 | ||
8429abe0 | 663 | uint32_t |
8cb1fc45 | 664 | lde_update_label(struct fec_node *fn) |
8429abe0 | 665 | { |
8cb1fc45 RW |
666 | struct fec_nh *fnh; |
667 | int connected = 0; | |
668 | ||
669 | LIST_FOREACH(fnh, &fn->nexthops, entry) { | |
670 | if (fnh->flags & F_FEC_NH_CONNECTED) { | |
671 | connected = 1; | |
672 | break; | |
673 | } | |
674 | } | |
8429abe0 | 675 | |
45a8eba9 | 676 | /* should we allocate a label for this fec? */ |
8cb1fc45 | 677 | switch (fn->fec.type) { |
45a8eba9 RW |
678 | case FEC_TYPE_IPV4: |
679 | if ((ldeconf->ipv4.flags & F_LDPD_AF_ALLOCHOSTONLY) && | |
8cb1fc45 | 680 | fn->fec.u.ipv4.prefixlen != 32) |
45a8eba9 RW |
681 | return (NO_LABEL); |
682 | if (lde_acl_check(ldeconf->ipv4.acl_label_allocate_for, | |
8cb1fc45 RW |
683 | AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix, |
684 | fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT) | |
45a8eba9 RW |
685 | return (NO_LABEL); |
686 | break; | |
687 | case FEC_TYPE_IPV6: | |
688 | if ((ldeconf->ipv6.flags & F_LDPD_AF_ALLOCHOSTONLY) && | |
8cb1fc45 | 689 | fn->fec.u.ipv6.prefixlen != 128) |
45a8eba9 RW |
690 | return (NO_LABEL); |
691 | if (lde_acl_check(ldeconf->ipv6.acl_label_allocate_for, | |
8cb1fc45 RW |
692 | AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix, |
693 | fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT) | |
45a8eba9 RW |
694 | return (NO_LABEL); |
695 | break; | |
696 | default: | |
45a8eba9 RW |
697 | break; |
698 | } | |
699 | ||
700 | if (connected) { | |
701 | /* choose implicit or explicit-null depending on configuration */ | |
8cb1fc45 | 702 | switch (fn->fec.type) { |
45a8eba9 RW |
703 | case FEC_TYPE_IPV4: |
704 | if (!(ldeconf->ipv4.flags & F_LDPD_AF_EXPNULL)) | |
70e98a7f | 705 | return (MPLS_LABEL_IMPLICIT_NULL); |
45a8eba9 | 706 | if (lde_acl_check(ldeconf->ipv4.acl_label_expnull_for, |
8cb1fc45 RW |
707 | AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix, |
708 | fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT) | |
70e98a7f DS |
709 | return (MPLS_LABEL_IMPLICIT_NULL); |
710 | return MPLS_LABEL_IPV4_EXPLICIT_NULL; | |
45a8eba9 RW |
711 | case FEC_TYPE_IPV6: |
712 | if (!(ldeconf->ipv6.flags & F_LDPD_AF_EXPNULL)) | |
70e98a7f | 713 | return (MPLS_LABEL_IMPLICIT_NULL); |
45a8eba9 | 714 | if (lde_acl_check(ldeconf->ipv6.acl_label_expnull_for, |
8cb1fc45 RW |
715 | AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix, |
716 | fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT) | |
70e98a7f DS |
717 | return (MPLS_LABEL_IMPLICIT_NULL); |
718 | return MPLS_LABEL_IPV6_EXPLICIT_NULL; | |
45a8eba9 | 719 | default: |
8cb1fc45 | 720 | fatalx("lde_update_label: unexpected fec type"); |
45a8eba9 RW |
721 | break; |
722 | } | |
723 | } | |
724 | ||
8cb1fc45 RW |
725 | /* preserve current label if there's no need to update it */ |
726 | if (fn->local_label != NO_LABEL && | |
727 | fn->local_label > MPLS_LABEL_RESERVED_MAX) | |
728 | return (fn->local_label); | |
729 | ||
5afba51d | 730 | return (lde_get_next_label()); |
8429abe0 RW |
731 | } |
732 | ||
733 | void | |
734 | lde_send_change_klabel(struct fec_node *fn, struct fec_nh *fnh) | |
735 | { | |
87b5f1b7 RW |
736 | struct kroute kr; |
737 | struct zapi_pw zpw; | |
8429abe0 RW |
738 | struct l2vpn_pw *pw; |
739 | ||
740 | switch (fn->fec.type) { | |
741 | case FEC_TYPE_IPV4: | |
742 | memset(&kr, 0, sizeof(kr)); | |
743 | kr.af = AF_INET; | |
744 | kr.prefix.v4 = fn->fec.u.ipv4.prefix; | |
745 | kr.prefixlen = fn->fec.u.ipv4.prefixlen; | |
746 | kr.nexthop.v4 = fnh->nexthop.v4; | |
88d88a9c | 747 | kr.ifindex = fnh->ifindex; |
8429abe0 RW |
748 | kr.local_label = fn->local_label; |
749 | kr.remote_label = fnh->remote_label; | |
750 | kr.priority = fnh->priority; | |
751 | ||
752 | lde_imsg_compose_parent(IMSG_KLABEL_CHANGE, 0, &kr, | |
753 | sizeof(kr)); | |
8429abe0 RW |
754 | break; |
755 | case FEC_TYPE_IPV6: | |
756 | memset(&kr, 0, sizeof(kr)); | |
757 | kr.af = AF_INET6; | |
758 | kr.prefix.v6 = fn->fec.u.ipv6.prefix; | |
759 | kr.prefixlen = fn->fec.u.ipv6.prefixlen; | |
760 | kr.nexthop.v6 = fnh->nexthop.v6; | |
88d88a9c | 761 | kr.ifindex = fnh->ifindex; |
8429abe0 RW |
762 | kr.local_label = fn->local_label; |
763 | kr.remote_label = fnh->remote_label; | |
764 | kr.priority = fnh->priority; | |
765 | ||
766 | lde_imsg_compose_parent(IMSG_KLABEL_CHANGE, 0, &kr, | |
767 | sizeof(kr)); | |
8429abe0 RW |
768 | break; |
769 | case FEC_TYPE_PWID: | |
3c5b5220 RW |
770 | pw = (struct l2vpn_pw *) fn->data; |
771 | if (!pw || fn->local_label == NO_LABEL || | |
8429abe0 RW |
772 | fnh->remote_label == NO_LABEL) |
773 | return; | |
774 | ||
3c5b5220 | 775 | pw->enabled = true; |
87b5f1b7 RW |
776 | pw2zpw(pw, &zpw); |
777 | zpw.local_label = fn->local_label; | |
778 | zpw.remote_label = fnh->remote_label; | |
779 | lde_imsg_compose_parent(IMSG_KPW_SET, 0, &zpw, sizeof(zpw)); | |
8429abe0 RW |
780 | break; |
781 | } | |
782 | } | |
783 | ||
784 | void | |
785 | lde_send_delete_klabel(struct fec_node *fn, struct fec_nh *fnh) | |
786 | { | |
787 | struct kroute kr; | |
87b5f1b7 | 788 | struct zapi_pw zpw; |
8429abe0 RW |
789 | struct l2vpn_pw *pw; |
790 | ||
791 | switch (fn->fec.type) { | |
792 | case FEC_TYPE_IPV4: | |
793 | memset(&kr, 0, sizeof(kr)); | |
794 | kr.af = AF_INET; | |
795 | kr.prefix.v4 = fn->fec.u.ipv4.prefix; | |
796 | kr.prefixlen = fn->fec.u.ipv4.prefixlen; | |
797 | kr.nexthop.v4 = fnh->nexthop.v4; | |
88d88a9c | 798 | kr.ifindex = fnh->ifindex; |
8429abe0 RW |
799 | kr.local_label = fn->local_label; |
800 | kr.remote_label = fnh->remote_label; | |
801 | kr.priority = fnh->priority; | |
802 | ||
803 | lde_imsg_compose_parent(IMSG_KLABEL_DELETE, 0, &kr, | |
804 | sizeof(kr)); | |
8429abe0 RW |
805 | break; |
806 | case FEC_TYPE_IPV6: | |
807 | memset(&kr, 0, sizeof(kr)); | |
808 | kr.af = AF_INET6; | |
809 | kr.prefix.v6 = fn->fec.u.ipv6.prefix; | |
810 | kr.prefixlen = fn->fec.u.ipv6.prefixlen; | |
811 | kr.nexthop.v6 = fnh->nexthop.v6; | |
88d88a9c | 812 | kr.ifindex = fnh->ifindex; |
8429abe0 RW |
813 | kr.local_label = fn->local_label; |
814 | kr.remote_label = fnh->remote_label; | |
815 | kr.priority = fnh->priority; | |
816 | ||
817 | lde_imsg_compose_parent(IMSG_KLABEL_DELETE, 0, &kr, | |
818 | sizeof(kr)); | |
8429abe0 RW |
819 | break; |
820 | case FEC_TYPE_PWID: | |
821 | pw = (struct l2vpn_pw *) fn->data; | |
3c5b5220 RW |
822 | if (!pw) |
823 | return; | |
824 | ||
825 | pw->enabled = false; | |
87b5f1b7 RW |
826 | pw2zpw(pw, &zpw); |
827 | zpw.local_label = fn->local_label; | |
828 | zpw.remote_label = fnh->remote_label; | |
829 | lde_imsg_compose_parent(IMSG_KPW_UNSET, 0, &zpw, sizeof(zpw)); | |
8429abe0 RW |
830 | break; |
831 | } | |
832 | } | |
833 | ||
834 | void | |
835 | lde_fec2map(struct fec *fec, struct map *map) | |
836 | { | |
837 | memset(map, 0, sizeof(*map)); | |
838 | ||
839 | switch (fec->type) { | |
840 | case FEC_TYPE_IPV4: | |
841 | map->type = MAP_TYPE_PREFIX; | |
842 | map->fec.prefix.af = AF_INET; | |
843 | map->fec.prefix.prefix.v4 = fec->u.ipv4.prefix; | |
844 | map->fec.prefix.prefixlen = fec->u.ipv4.prefixlen; | |
845 | break; | |
846 | case FEC_TYPE_IPV6: | |
847 | map->type = MAP_TYPE_PREFIX; | |
848 | map->fec.prefix.af = AF_INET6; | |
849 | map->fec.prefix.prefix.v6 = fec->u.ipv6.prefix; | |
850 | map->fec.prefix.prefixlen = fec->u.ipv6.prefixlen; | |
851 | break; | |
852 | case FEC_TYPE_PWID: | |
853 | map->type = MAP_TYPE_PWID; | |
854 | map->fec.pwid.type = fec->u.pwid.type; | |
855 | map->fec.pwid.group_id = 0; | |
856 | map->flags |= F_MAP_PW_ID; | |
857 | map->fec.pwid.pwid = fec->u.pwid.pwid; | |
858 | break; | |
859 | } | |
860 | } | |
861 | ||
862 | void | |
863 | lde_map2fec(struct map *map, struct in_addr lsr_id, struct fec *fec) | |
864 | { | |
865 | memset(fec, 0, sizeof(*fec)); | |
866 | ||
867 | switch (map->type) { | |
868 | case MAP_TYPE_PREFIX: | |
869 | switch (map->fec.prefix.af) { | |
870 | case AF_INET: | |
871 | fec->type = FEC_TYPE_IPV4; | |
872 | fec->u.ipv4.prefix = map->fec.prefix.prefix.v4; | |
873 | fec->u.ipv4.prefixlen = map->fec.prefix.prefixlen; | |
874 | break; | |
875 | case AF_INET6: | |
876 | fec->type = FEC_TYPE_IPV6; | |
877 | fec->u.ipv6.prefix = map->fec.prefix.prefix.v6; | |
878 | fec->u.ipv6.prefixlen = map->fec.prefix.prefixlen; | |
879 | break; | |
880 | default: | |
881 | fatalx("lde_map2fec: unknown af"); | |
882 | break; | |
883 | } | |
884 | break; | |
885 | case MAP_TYPE_PWID: | |
886 | fec->type = FEC_TYPE_PWID; | |
887 | fec->u.pwid.type = map->fec.pwid.type; | |
888 | fec->u.pwid.pwid = map->fec.pwid.pwid; | |
889 | fec->u.pwid.lsr_id = lsr_id; | |
890 | break; | |
891 | } | |
892 | } | |
893 | ||
894 | void | |
895 | lde_send_labelmapping(struct lde_nbr *ln, struct fec_node *fn, int single) | |
896 | { | |
7c2abbd7 RW |
897 | struct lde_wdraw *lw; |
898 | struct lde_map *me; | |
899 | struct lde_req *lre; | |
900 | struct map map; | |
901 | struct l2vpn_pw *pw; | |
902 | ||
903 | /* | |
904 | * We shouldn't send a new label mapping if we have a pending | |
905 | * label release to receive. In this case, schedule to send a | |
906 | * label mapping as soon as a label release is received. | |
907 | */ | |
908 | lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, &fn->fec); | |
909 | if (lw) { | |
b4fcca6b RW |
910 | if (!fec_find(&ln->sent_map_pending, &fn->fec)) { |
911 | debug_evt("%s: FEC %s: scheduling to send label " | |
912 | "mapping later (waiting for pending label release)", | |
913 | __func__, log_fec(&fn->fec)); | |
7c2abbd7 | 914 | lde_map_pending_add(ln, fn); |
b4fcca6b | 915 | } |
7c2abbd7 RW |
916 | return; |
917 | } | |
8429abe0 RW |
918 | |
919 | /* | |
920 | * This function skips SL.1 - 3 and SL.9 - 14 because the label | |
921 | * allocation is done way earlier (because of the merging nature of | |
922 | * ldpd). | |
923 | */ | |
924 | ||
925 | lde_fec2map(&fn->fec, &map); | |
926 | switch (fn->fec.type) { | |
927 | case FEC_TYPE_IPV4: | |
928 | if (!ln->v4_enabled) | |
929 | return; | |
45a8eba9 RW |
930 | if (lde_acl_check(ldeconf->ipv4.acl_label_advertise_to, |
931 | AF_INET, (union ldpd_addr *)&ln->id, 32) != FILTER_PERMIT) | |
932 | return; | |
933 | if (lde_acl_check(ldeconf->ipv4.acl_label_advertise_for, | |
934 | AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix, | |
935 | fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT) | |
936 | return; | |
8429abe0 RW |
937 | break; |
938 | case FEC_TYPE_IPV6: | |
939 | if (!ln->v6_enabled) | |
940 | return; | |
45a8eba9 RW |
941 | if (lde_acl_check(ldeconf->ipv6.acl_label_advertise_to, |
942 | AF_INET, (union ldpd_addr *)&ln->id, 32) != FILTER_PERMIT) | |
943 | return; | |
944 | if (lde_acl_check(ldeconf->ipv6.acl_label_advertise_for, | |
945 | AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix, | |
946 | fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT) | |
947 | return; | |
8429abe0 RW |
948 | break; |
949 | case FEC_TYPE_PWID: | |
950 | pw = (struct l2vpn_pw *) fn->data; | |
951 | if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr) | |
952 | /* not the remote end of the pseudowire */ | |
953 | return; | |
954 | ||
955 | map.flags |= F_MAP_PW_IFMTU; | |
956 | map.fec.pwid.ifmtu = pw->l2vpn->mtu; | |
957 | if (pw->flags & F_PW_CWORD) | |
958 | map.flags |= F_MAP_PW_CWORD; | |
959 | if (pw->flags & F_PW_STATUSTLV) { | |
960 | map.flags |= F_MAP_PW_STATUS; | |
87b5f1b7 | 961 | map.pw_status = pw->local_status; |
8429abe0 RW |
962 | } |
963 | break; | |
964 | } | |
965 | map.label = fn->local_label; | |
966 | ||
967 | /* SL.6: is there a pending request for this mapping? */ | |
968 | lre = (struct lde_req *)fec_find(&ln->recv_req, &fn->fec); | |
969 | if (lre) { | |
970 | /* set label request msg id in the mapping response. */ | |
971 | map.requestid = lre->msg_id; | |
972 | map.flags = F_MAP_REQ_ID; | |
973 | ||
974 | /* SL.7: delete record of pending request */ | |
975 | lde_req_del(ln, lre, 0); | |
976 | } | |
977 | ||
978 | /* SL.4: send label mapping */ | |
979 | lde_imsg_compose_ldpe(IMSG_MAPPING_ADD, ln->peerid, 0, | |
980 | &map, sizeof(map)); | |
981 | if (single) | |
982 | lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0, | |
983 | NULL, 0); | |
984 | ||
985 | /* SL.5: record sent label mapping */ | |
986 | me = (struct lde_map *)fec_find(&ln->sent_map, &fn->fec); | |
987 | if (me == NULL) | |
988 | me = lde_map_add(ln, fn, 1); | |
989 | me->map = map; | |
990 | } | |
991 | ||
992 | void | |
0bcc2916 RW |
993 | lde_send_labelwithdraw(struct lde_nbr *ln, struct fec_node *fn, |
994 | struct map *wcard, struct status_tlv *st) | |
8429abe0 RW |
995 | { |
996 | struct lde_wdraw *lw; | |
997 | struct map map; | |
998 | struct fec *f; | |
999 | struct l2vpn_pw *pw; | |
1000 | ||
1001 | if (fn) { | |
1002 | lde_fec2map(&fn->fec, &map); | |
1003 | switch (fn->fec.type) { | |
1004 | case FEC_TYPE_IPV4: | |
1005 | if (!ln->v4_enabled) | |
1006 | return; | |
1007 | break; | |
1008 | case FEC_TYPE_IPV6: | |
1009 | if (!ln->v6_enabled) | |
1010 | return; | |
1011 | break; | |
1012 | case FEC_TYPE_PWID: | |
1013 | pw = (struct l2vpn_pw *) fn->data; | |
1014 | if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr) | |
1015 | /* not the remote end of the pseudowire */ | |
1016 | return; | |
1017 | ||
1018 | if (pw->flags & F_PW_CWORD) | |
1019 | map.flags |= F_MAP_PW_CWORD; | |
1020 | break; | |
1021 | } | |
1022 | map.label = fn->local_label; | |
0bcc2916 RW |
1023 | } else |
1024 | memcpy(&map, wcard, sizeof(map)); | |
8429abe0 RW |
1025 | |
1026 | if (st) { | |
1027 | map.st.status_code = st->status_code; | |
1028 | map.st.msg_id = st->msg_id; | |
1029 | map.st.msg_type = st->msg_type; | |
1030 | map.flags |= F_MAP_STATUS; | |
1031 | } | |
1032 | ||
1033 | /* SWd.1: send label withdraw. */ | |
1034 | lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD, ln->peerid, 0, | |
1035 | &map, sizeof(map)); | |
1036 | lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD_END, ln->peerid, 0, NULL, 0); | |
1037 | ||
1038 | /* SWd.2: record label withdraw. */ | |
1039 | if (fn) { | |
1040 | lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, &fn->fec); | |
1041 | if (lw == NULL) | |
1042 | lw = lde_wdraw_add(ln, fn); | |
1043 | lw->label = map.label; | |
1044 | } else { | |
0bcc2916 RW |
1045 | struct lde_map *me; |
1046 | ||
8429abe0 RW |
1047 | RB_FOREACH(f, fec_tree, &ft) { |
1048 | fn = (struct fec_node *)f; | |
0bcc2916 RW |
1049 | me = (struct lde_map *)fec_find(&ln->sent_map, &fn->fec); |
1050 | if (lde_wildcard_apply(wcard, &fn->fec, me) == 0) | |
1051 | continue; | |
8429abe0 RW |
1052 | |
1053 | lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, | |
1054 | &fn->fec); | |
1055 | if (lw == NULL) | |
1056 | lw = lde_wdraw_add(ln, fn); | |
1057 | lw->label = map.label; | |
1058 | } | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | void | |
0bcc2916 | 1063 | lde_send_labelwithdraw_wcard(struct lde_nbr *ln, uint32_t label) |
8429abe0 | 1064 | { |
0bcc2916 | 1065 | struct map wcard; |
8429abe0 | 1066 | |
0bcc2916 RW |
1067 | memset(&wcard, 0, sizeof(wcard)); |
1068 | wcard.type = MAP_TYPE_WILDCARD; | |
1069 | wcard.label = label; | |
1070 | lde_send_labelwithdraw(ln, NULL, &wcard, NULL); | |
8429abe0 RW |
1071 | } |
1072 | ||
d4afb819 RW |
1073 | void |
1074 | lde_send_labelwithdraw_twcard_prefix(struct lde_nbr *ln, uint16_t af, | |
1075 | uint32_t label) | |
1076 | { | |
1077 | struct map wcard; | |
1078 | ||
1079 | memset(&wcard, 0, sizeof(wcard)); | |
1080 | wcard.type = MAP_TYPE_TYPED_WCARD; | |
1081 | wcard.fec.twcard.type = MAP_TYPE_PREFIX; | |
1082 | wcard.fec.twcard.u.prefix_af = af; | |
1083 | wcard.label = label; | |
1084 | lde_send_labelwithdraw(ln, NULL, &wcard, NULL); | |
1085 | } | |
1086 | ||
aba50a83 RW |
1087 | void |
1088 | lde_send_labelwithdraw_twcard_pwid(struct lde_nbr *ln, uint16_t pw_type, | |
1089 | uint32_t label) | |
1090 | { | |
1091 | struct map wcard; | |
1092 | ||
1093 | memset(&wcard, 0, sizeof(wcard)); | |
1094 | wcard.type = MAP_TYPE_TYPED_WCARD; | |
1095 | wcard.fec.twcard.type = MAP_TYPE_PWID; | |
1096 | wcard.fec.twcard.u.pw_type = pw_type; | |
1097 | wcard.label = label; | |
1098 | lde_send_labelwithdraw(ln, NULL, &wcard, NULL); | |
1099 | } | |
1100 | ||
8429abe0 | 1101 | void |
0bcc2916 RW |
1102 | lde_send_labelwithdraw_pwid_wcard(struct lde_nbr *ln, uint16_t pw_type, |
1103 | uint32_t group_id) | |
1104 | { | |
1105 | struct map wcard; | |
1106 | ||
1107 | memset(&wcard, 0, sizeof(wcard)); | |
1108 | wcard.type = MAP_TYPE_PWID; | |
1109 | wcard.fec.pwid.type = pw_type; | |
1110 | wcard.fec.pwid.group_id = group_id; | |
1111 | /* we can not append a Label TLV when using PWid group wildcards. */ | |
1112 | wcard.label = NO_LABEL; | |
1113 | lde_send_labelwithdraw(ln, NULL, &wcard, NULL); | |
1114 | } | |
1115 | ||
1116 | void | |
1117 | lde_send_labelrelease(struct lde_nbr *ln, struct fec_node *fn, | |
1118 | struct map *wcard, uint32_t label) | |
8429abe0 RW |
1119 | { |
1120 | struct map map; | |
1121 | struct l2vpn_pw *pw; | |
1122 | ||
1123 | if (fn) { | |
1124 | lde_fec2map(&fn->fec, &map); | |
1125 | switch (fn->fec.type) { | |
1126 | case FEC_TYPE_IPV4: | |
1127 | if (!ln->v4_enabled) | |
1128 | return; | |
1129 | break; | |
1130 | case FEC_TYPE_IPV6: | |
1131 | if (!ln->v6_enabled) | |
1132 | return; | |
1133 | break; | |
1134 | case FEC_TYPE_PWID: | |
1135 | pw = (struct l2vpn_pw *) fn->data; | |
1136 | if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr) | |
1137 | /* not the remote end of the pseudowire */ | |
1138 | return; | |
1139 | ||
1140 | if (pw->flags & F_PW_CWORD) | |
1141 | map.flags |= F_MAP_PW_CWORD; | |
1142 | break; | |
1143 | } | |
0bcc2916 RW |
1144 | } else |
1145 | memcpy(&map, wcard, sizeof(map)); | |
8429abe0 RW |
1146 | map.label = label; |
1147 | ||
1148 | lde_imsg_compose_ldpe(IMSG_RELEASE_ADD, ln->peerid, 0, | |
1149 | &map, sizeof(map)); | |
1150 | lde_imsg_compose_ldpe(IMSG_RELEASE_ADD_END, ln->peerid, 0, NULL, 0); | |
1151 | } | |
1152 | ||
1153 | void | |
05aac414 | 1154 | lde_send_notification(struct lde_nbr *ln, uint32_t status_code, uint32_t msg_id, |
8429abe0 RW |
1155 | uint16_t msg_type) |
1156 | { | |
1157 | struct notify_msg nm; | |
1158 | ||
1159 | memset(&nm, 0, sizeof(nm)); | |
1160 | nm.status_code = status_code; | |
1161 | /* 'msg_id' and 'msg_type' should be in network byte order */ | |
1162 | nm.msg_id = msg_id; | |
1163 | nm.msg_type = msg_type; | |
1164 | ||
05aac414 | 1165 | lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0, |
8429abe0 RW |
1166 | &nm, sizeof(nm)); |
1167 | } | |
1168 | ||
257799cd RW |
1169 | void |
1170 | lde_send_notification_eol_prefix(struct lde_nbr *ln, int af) | |
1171 | { | |
1172 | struct notify_msg nm; | |
1173 | ||
1174 | memset(&nm, 0, sizeof(nm)); | |
1175 | nm.status_code = S_ENDOFLIB; | |
1176 | nm.fec.type = MAP_TYPE_TYPED_WCARD; | |
1177 | nm.fec.fec.twcard.type = MAP_TYPE_PREFIX; | |
1178 | nm.fec.fec.twcard.u.prefix_af = af; | |
1179 | nm.flags |= F_NOTIF_FEC; | |
1180 | ||
1181 | lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0, | |
1182 | &nm, sizeof(nm)); | |
1183 | } | |
1184 | ||
1185 | void | |
1186 | lde_send_notification_eol_pwid(struct lde_nbr *ln, uint16_t pw_type) | |
1187 | { | |
1188 | struct notify_msg nm; | |
1189 | ||
1190 | memset(&nm, 0, sizeof(nm)); | |
1191 | nm.status_code = S_ENDOFLIB; | |
1192 | nm.fec.type = MAP_TYPE_TYPED_WCARD; | |
1193 | nm.fec.fec.twcard.type = MAP_TYPE_PWID; | |
1194 | nm.fec.fec.twcard.u.pw_type = pw_type; | |
1195 | nm.flags |= F_NOTIF_FEC; | |
1196 | ||
1197 | lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0, | |
1198 | &nm, sizeof(nm)); | |
1199 | } | |
1200 | ||
8429abe0 | 1201 | static __inline int |
45926e58 | 1202 | lde_nbr_compare(const struct lde_nbr *a, const struct lde_nbr *b) |
8429abe0 RW |
1203 | { |
1204 | return (a->peerid - b->peerid); | |
1205 | } | |
1206 | ||
1207 | static struct lde_nbr * | |
1208 | lde_nbr_new(uint32_t peerid, struct lde_nbr *new) | |
1209 | { | |
1210 | struct lde_nbr *ln; | |
1211 | ||
1212 | if ((ln = calloc(1, sizeof(*ln))) == NULL) | |
1213 | fatal(__func__); | |
1214 | ||
1215 | ln->id = new->id; | |
1216 | ln->v4_enabled = new->v4_enabled; | |
1217 | ln->v6_enabled = new->v6_enabled; | |
257799cd | 1218 | ln->flags = new->flags; |
8429abe0 RW |
1219 | ln->peerid = peerid; |
1220 | fec_init(&ln->recv_map); | |
1221 | fec_init(&ln->sent_map); | |
7c2abbd7 | 1222 | fec_init(&ln->sent_map_pending); |
8429abe0 RW |
1223 | fec_init(&ln->recv_req); |
1224 | fec_init(&ln->sent_req); | |
1225 | fec_init(&ln->sent_wdraw); | |
1226 | ||
1227 | TAILQ_INIT(&ln->addr_list); | |
1228 | ||
1229 | if (RB_INSERT(nbr_tree, &lde_nbrs, ln) != NULL) | |
1230 | fatalx("lde_nbr_new: RB_INSERT failed"); | |
1231 | ||
1232 | return (ln); | |
1233 | } | |
1234 | ||
1235 | static void | |
1236 | lde_nbr_del(struct lde_nbr *ln) | |
1237 | { | |
1238 | struct fec *f; | |
1239 | struct fec_node *fn; | |
1240 | struct fec_nh *fnh; | |
1241 | struct l2vpn_pw *pw; | |
1242 | ||
1243 | if (ln == NULL) | |
1244 | return; | |
1245 | ||
1246 | /* uninstall received mappings */ | |
1247 | RB_FOREACH(f, fec_tree, &ft) { | |
1248 | fn = (struct fec_node *)f; | |
1249 | ||
1250 | LIST_FOREACH(fnh, &fn->nexthops, entry) { | |
1251 | switch (f->type) { | |
1252 | case FEC_TYPE_IPV4: | |
1253 | case FEC_TYPE_IPV6: | |
1254 | if (!lde_address_find(ln, fnh->af, | |
1255 | &fnh->nexthop)) | |
1256 | continue; | |
1257 | break; | |
1258 | case FEC_TYPE_PWID: | |
1259 | if (f->u.pwid.lsr_id.s_addr != ln->id.s_addr) | |
1260 | continue; | |
1261 | pw = (struct l2vpn_pw *) fn->data; | |
1262 | if (pw) | |
1263 | l2vpn_pw_reset(pw); | |
1264 | break; | |
1265 | default: | |
1266 | break; | |
1267 | } | |
1268 | ||
1269 | lde_send_delete_klabel(fn, fnh); | |
1270 | fnh->remote_label = NO_LABEL; | |
1271 | } | |
1272 | } | |
1273 | ||
1274 | lde_address_list_free(ln); | |
1275 | ||
1276 | fec_clear(&ln->recv_map, lde_map_free); | |
1277 | fec_clear(&ln->sent_map, lde_map_free); | |
7c2abbd7 | 1278 | fec_clear(&ln->sent_map_pending, free); |
8429abe0 RW |
1279 | fec_clear(&ln->recv_req, free); |
1280 | fec_clear(&ln->sent_req, free); | |
1281 | fec_clear(&ln->sent_wdraw, free); | |
1282 | ||
1283 | RB_REMOVE(nbr_tree, &lde_nbrs, ln); | |
1284 | ||
1285 | free(ln); | |
1286 | } | |
1287 | ||
1288 | static struct lde_nbr * | |
1289 | lde_nbr_find(uint32_t peerid) | |
1290 | { | |
1291 | struct lde_nbr ln; | |
1292 | ||
1293 | ln.peerid = peerid; | |
1294 | ||
1295 | return (RB_FIND(nbr_tree, &lde_nbrs, &ln)); | |
1296 | } | |
1297 | ||
1298 | struct lde_nbr * | |
1299 | lde_nbr_find_by_lsrid(struct in_addr addr) | |
1300 | { | |
1301 | struct lde_nbr *ln; | |
1302 | ||
1303 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
1304 | if (ln->id.s_addr == addr.s_addr) | |
1305 | return (ln); | |
1306 | ||
1307 | return (NULL); | |
1308 | } | |
1309 | ||
1310 | struct lde_nbr * | |
1311 | lde_nbr_find_by_addr(int af, union ldpd_addr *addr) | |
1312 | { | |
1313 | struct lde_nbr *ln; | |
1314 | ||
1315 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
1316 | if (lde_address_find(ln, af, addr) != NULL) | |
1317 | return (ln); | |
1318 | ||
1319 | return (NULL); | |
1320 | } | |
1321 | ||
1322 | static void | |
1323 | lde_nbr_clear(void) | |
1324 | { | |
1325 | struct lde_nbr *ln; | |
1326 | ||
55cd0f61 DS |
1327 | while (!RB_EMPTY(nbr_tree, &lde_nbrs)) { |
1328 | ln = RB_ROOT(nbr_tree, &lde_nbrs); | |
1329 | ||
8429abe0 | 1330 | lde_nbr_del(ln); |
55cd0f61 | 1331 | } |
8429abe0 RW |
1332 | } |
1333 | ||
1334 | static void | |
1335 | lde_nbr_addr_update(struct lde_nbr *ln, struct lde_addr *lde_addr, int removed) | |
1336 | { | |
1337 | struct fec *fec; | |
1338 | struct fec_node *fn; | |
1339 | struct fec_nh *fnh; | |
1340 | struct lde_map *me; | |
1341 | ||
1342 | RB_FOREACH(fec, fec_tree, &ln->recv_map) { | |
8429abe0 RW |
1343 | switch (fec->type) { |
1344 | case FEC_TYPE_IPV4: | |
1345 | if (lde_addr->af != AF_INET) | |
1346 | continue; | |
1347 | break; | |
1348 | case FEC_TYPE_IPV6: | |
1349 | if (lde_addr->af != AF_INET6) | |
1350 | continue; | |
1351 | break; | |
1352 | default: | |
1353 | continue; | |
1354 | } | |
1355 | ||
1e4c8673 RW |
1356 | fn = (struct fec_node *)fec_find(&ft, fec); |
1357 | if (fn == NULL) | |
1358 | /* shouldn't happen */ | |
1359 | continue; | |
1360 | ||
8429abe0 RW |
1361 | LIST_FOREACH(fnh, &fn->nexthops, entry) { |
1362 | if (ldp_addrcmp(fnh->af, &fnh->nexthop, | |
1363 | &lde_addr->addr)) | |
1364 | continue; | |
1365 | ||
1366 | if (removed) { | |
1367 | lde_send_delete_klabel(fn, fnh); | |
1368 | fnh->remote_label = NO_LABEL; | |
1369 | } else { | |
1370 | me = (struct lde_map *)fec; | |
1371 | fnh->remote_label = me->map.label; | |
1372 | lde_send_change_klabel(fn, fnh); | |
1373 | } | |
1374 | break; | |
1375 | } | |
1376 | } | |
1377 | } | |
1378 | ||
d3e1887a | 1379 | static __inline int |
45926e58 | 1380 | lde_map_compare(const struct lde_map *a, const struct lde_map *b) |
d3e1887a RW |
1381 | { |
1382 | return (ldp_addrcmp(AF_INET, (union ldpd_addr *)&a->nexthop->id, | |
1383 | (union ldpd_addr *)&b->nexthop->id)); | |
1384 | } | |
1385 | ||
8429abe0 RW |
1386 | struct lde_map * |
1387 | lde_map_add(struct lde_nbr *ln, struct fec_node *fn, int sent) | |
1388 | { | |
1389 | struct lde_map *me; | |
1390 | ||
1391 | me = calloc(1, sizeof(*me)); | |
1392 | if (me == NULL) | |
1393 | fatal(__func__); | |
1394 | ||
1395 | me->fec = fn->fec; | |
1396 | me->nexthop = ln; | |
1397 | ||
1398 | if (sent) { | |
d3e1887a RW |
1399 | RB_INSERT(lde_map_head, &fn->upstream, me); |
1400 | me->head = &fn->upstream; | |
8429abe0 RW |
1401 | if (fec_insert(&ln->sent_map, &me->fec)) |
1402 | log_warnx("failed to add %s to sent map", | |
1403 | log_fec(&me->fec)); | |
1404 | /* XXX on failure more cleanup is needed */ | |
1405 | } else { | |
d3e1887a RW |
1406 | RB_INSERT(lde_map_head, &fn->downstream, me); |
1407 | me->head = &fn->downstream; | |
8429abe0 RW |
1408 | if (fec_insert(&ln->recv_map, &me->fec)) |
1409 | log_warnx("failed to add %s to recv map", | |
1410 | log_fec(&me->fec)); | |
1411 | } | |
1412 | ||
1413 | return (me); | |
1414 | } | |
1415 | ||
1416 | void | |
1417 | lde_map_del(struct lde_nbr *ln, struct lde_map *me, int sent) | |
1418 | { | |
1419 | if (sent) | |
1420 | fec_remove(&ln->sent_map, &me->fec); | |
1421 | else | |
1422 | fec_remove(&ln->recv_map, &me->fec); | |
1423 | ||
1424 | lde_map_free(me); | |
1425 | } | |
1426 | ||
1427 | static void | |
1428 | lde_map_free(void *ptr) | |
1429 | { | |
1430 | struct lde_map *map = ptr; | |
1431 | ||
d3e1887a | 1432 | RB_REMOVE(lde_map_head, map->head, map); |
8429abe0 RW |
1433 | free(map); |
1434 | } | |
1435 | ||
7c2abbd7 RW |
1436 | struct fec * |
1437 | lde_map_pending_add(struct lde_nbr *ln, struct fec_node *fn) | |
1438 | { | |
1439 | struct fec *map; | |
1440 | ||
1441 | map = calloc(1, sizeof(*map)); | |
1442 | if (map == NULL) | |
1443 | fatal(__func__); | |
1444 | ||
1445 | *map = fn->fec; | |
1446 | if (fec_insert(&ln->sent_map_pending, map)) | |
1447 | log_warnx("failed to add %s to sent map (pending)", | |
1448 | log_fec(map)); | |
1449 | ||
1450 | return (map); | |
1451 | } | |
1452 | ||
1453 | void | |
1454 | lde_map_pending_del(struct lde_nbr *ln, struct fec *map) | |
1455 | { | |
1456 | fec_remove(&ln->sent_map_pending, map); | |
1457 | free(map); | |
1458 | } | |
1459 | ||
8429abe0 RW |
1460 | struct lde_req * |
1461 | lde_req_add(struct lde_nbr *ln, struct fec *fec, int sent) | |
1462 | { | |
1463 | struct fec_tree *t; | |
1464 | struct lde_req *lre; | |
1465 | ||
1466 | t = sent ? &ln->sent_req : &ln->recv_req; | |
1467 | ||
1468 | lre = calloc(1, sizeof(*lre)); | |
1469 | if (lre != NULL) { | |
1470 | lre->fec = *fec; | |
1471 | ||
1472 | if (fec_insert(t, &lre->fec)) { | |
1473 | log_warnx("failed to add %s to %s req", | |
1474 | log_fec(&lre->fec), sent ? "sent" : "recv"); | |
1475 | free(lre); | |
1476 | return (NULL); | |
1477 | } | |
1478 | } | |
1479 | ||
1480 | return (lre); | |
1481 | } | |
1482 | ||
1483 | void | |
1484 | lde_req_del(struct lde_nbr *ln, struct lde_req *lre, int sent) | |
1485 | { | |
1486 | if (sent) | |
1487 | fec_remove(&ln->sent_req, &lre->fec); | |
1488 | else | |
1489 | fec_remove(&ln->recv_req, &lre->fec); | |
1490 | ||
1491 | free(lre); | |
1492 | } | |
1493 | ||
1494 | struct lde_wdraw * | |
1495 | lde_wdraw_add(struct lde_nbr *ln, struct fec_node *fn) | |
1496 | { | |
1497 | struct lde_wdraw *lw; | |
1498 | ||
1499 | lw = calloc(1, sizeof(*lw)); | |
1500 | if (lw == NULL) | |
1501 | fatal(__func__); | |
1502 | ||
1503 | lw->fec = fn->fec; | |
1504 | ||
1505 | if (fec_insert(&ln->sent_wdraw, &lw->fec)) | |
1506 | log_warnx("failed to add %s to sent wdraw", | |
1507 | log_fec(&lw->fec)); | |
1508 | ||
1509 | return (lw); | |
1510 | } | |
1511 | ||
1512 | void | |
1513 | lde_wdraw_del(struct lde_nbr *ln, struct lde_wdraw *lw) | |
1514 | { | |
1515 | fec_remove(&ln->sent_wdraw, &lw->fec); | |
1516 | free(lw); | |
1517 | } | |
1518 | ||
1519 | void | |
45a8eba9 | 1520 | lde_change_egress_label(int af) |
8429abe0 RW |
1521 | { |
1522 | struct lde_nbr *ln; | |
1523 | struct fec *f; | |
1524 | struct fec_node *fn; | |
1525 | ||
be54d744 | 1526 | /* explicitly withdraw all null labels */ |
8429abe0 | 1527 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) { |
70e98a7f | 1528 | lde_send_labelwithdraw_wcard(ln, MPLS_LABEL_IMPLICIT_NULL); |
45a8eba9 | 1529 | if (ln->v4_enabled) |
70e98a7f DS |
1530 | lde_send_labelwithdraw_wcard( |
1531 | ln, | |
1532 | MPLS_LABEL_IPV4_EXPLICIT_NULL); | |
45a8eba9 | 1533 | if (ln->v6_enabled) |
70e98a7f DS |
1534 | lde_send_labelwithdraw_wcard( |
1535 | ln, | |
1536 | MPLS_LABEL_IPV6_EXPLICIT_NULL); | |
be54d744 | 1537 | } |
8429abe0 | 1538 | |
be54d744 RW |
1539 | /* update label of connected routes */ |
1540 | RB_FOREACH(f, fec_tree, &ft) { | |
1541 | fn = (struct fec_node *)f; | |
1542 | if (fn->local_label > MPLS_LABEL_RESERVED_MAX) | |
1543 | continue; | |
8429abe0 | 1544 | |
be54d744 RW |
1545 | switch (af) { |
1546 | case AF_INET: | |
1547 | if (fn->fec.type != FEC_TYPE_IPV4) | |
1548 | continue; | |
1549 | break; | |
1550 | case AF_INET6: | |
1551 | if (fn->fec.type != FEC_TYPE_IPV6) | |
1552 | continue; | |
1553 | break; | |
1554 | default: | |
1555 | fatalx("lde_change_egress_label: unknown af"); | |
8429abe0 RW |
1556 | } |
1557 | ||
8cb1fc45 | 1558 | fn->local_label = lde_update_label(fn); |
45a8eba9 RW |
1559 | if (fn->local_label != NO_LABEL) |
1560 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
1561 | lde_send_labelmapping(ln, fn, 0); | |
be54d744 RW |
1562 | } |
1563 | RB_FOREACH(ln, nbr_tree, &lde_nbrs) | |
8429abe0 RW |
1564 | lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0, |
1565 | NULL, 0); | |
8429abe0 RW |
1566 | } |
1567 | ||
1568 | static int | |
1569 | lde_address_add(struct lde_nbr *ln, struct lde_addr *lde_addr) | |
1570 | { | |
1571 | struct lde_addr *new; | |
1572 | ||
1573 | if (lde_address_find(ln, lde_addr->af, &lde_addr->addr) != NULL) | |
1574 | return (-1); | |
1575 | ||
1576 | if ((new = calloc(1, sizeof(*new))) == NULL) | |
1577 | fatal(__func__); | |
1578 | ||
1579 | new->af = lde_addr->af; | |
1580 | new->addr = lde_addr->addr; | |
1581 | TAILQ_INSERT_TAIL(&ln->addr_list, new, entry); | |
1582 | ||
1583 | /* reevaluate the previously received mappings from this neighbor */ | |
1584 | lde_nbr_addr_update(ln, lde_addr, 0); | |
1585 | ||
1586 | return (0); | |
1587 | } | |
1588 | ||
1589 | static int | |
1590 | lde_address_del(struct lde_nbr *ln, struct lde_addr *lde_addr) | |
1591 | { | |
1592 | lde_addr = lde_address_find(ln, lde_addr->af, &lde_addr->addr); | |
1593 | if (lde_addr == NULL) | |
1594 | return (-1); | |
1595 | ||
1596 | /* reevaluate the previously received mappings from this neighbor */ | |
1597 | lde_nbr_addr_update(ln, lde_addr, 1); | |
1598 | ||
1599 | TAILQ_REMOVE(&ln->addr_list, lde_addr, entry); | |
1600 | free(lde_addr); | |
1601 | ||
1602 | return (0); | |
1603 | } | |
1604 | ||
1605 | struct lde_addr * | |
1606 | lde_address_find(struct lde_nbr *ln, int af, union ldpd_addr *addr) | |
1607 | { | |
1608 | struct lde_addr *lde_addr; | |
1609 | ||
1610 | TAILQ_FOREACH(lde_addr, &ln->addr_list, entry) | |
1611 | if (lde_addr->af == af && | |
1612 | ldp_addrcmp(af, &lde_addr->addr, addr) == 0) | |
1613 | return (lde_addr); | |
1614 | ||
1615 | return (NULL); | |
1616 | } | |
1617 | ||
1618 | static void | |
1619 | lde_address_list_free(struct lde_nbr *ln) | |
1620 | { | |
1621 | struct lde_addr *lde_addr; | |
1622 | ||
1623 | while ((lde_addr = TAILQ_FIRST(&ln->addr_list)) != NULL) { | |
1624 | TAILQ_REMOVE(&ln->addr_list, lde_addr, entry); | |
1625 | free(lde_addr); | |
1626 | } | |
1627 | } | |
fea12efb | 1628 | |
d7c0a89a | 1629 | static void zclient_sync_init(unsigned short instance) |
5afba51d RW |
1630 | { |
1631 | /* Initialize special zclient for synchronous message exchanges. */ | |
e1a1880d | 1632 | zclient_sync = zclient_new_notify(master, &zclient_options_default); |
5afba51d RW |
1633 | zclient_sync->sock = -1; |
1634 | zclient_sync->redist_default = ZEBRA_ROUTE_LDP; | |
1635 | zclient_sync->instance = instance; | |
342213ea DS |
1636 | zclient_sync->privs = &lde_privs; |
1637 | ||
5afba51d RW |
1638 | while (zclient_socket_connect(zclient_sync) < 0) { |
1639 | log_warnx("Error connecting synchronous zclient!"); | |
1640 | sleep(1); | |
1641 | } | |
fa84d193 DL |
1642 | /* make socket non-blocking */ |
1643 | sock_set_nonblock(zclient_sync->sock); | |
5afba51d RW |
1644 | |
1645 | /* Connect to label manager */ | |
1646 | while (lm_label_manager_connect(zclient_sync) != 0) { | |
1647 | log_warnx("Error connecting to label manager!"); | |
1648 | sleep(1); | |
1649 | } | |
1650 | } | |
1651 | ||
fea12efb | 1652 | static void |
1653 | lde_del_label_chunk(void *val) | |
1654 | { | |
1655 | free(val); | |
1656 | } | |
5afba51d | 1657 | |
fea12efb | 1658 | static int |
1659 | lde_get_label_chunk(void) | |
1660 | { | |
5afba51d RW |
1661 | int ret; |
1662 | uint32_t start, end; | |
fea12efb | 1663 | |
08e4b244 | 1664 | debug_labels("getting label chunk (size %u)", CHUNK_SIZE); |
fea12efb | 1665 | ret = lm_get_label_chunk(zclient_sync, 0, CHUNK_SIZE, &start, &end); |
5afba51d | 1666 | if (ret < 0) { |
fea12efb | 1667 | log_warnx("Error getting label chunk!"); |
fea12efb | 1668 | return -1; |
1669 | } | |
1670 | ||
1671 | on_get_label_chunk_response(start, end); | |
1672 | ||
5afba51d | 1673 | return (0); |
fea12efb | 1674 | } |
5afba51d | 1675 | |
fea12efb | 1676 | static void |
1677 | lde_label_list_init(void) | |
1678 | { | |
1679 | label_chunk_list = list_new(); | |
1680 | label_chunk_list->del = lde_del_label_chunk; | |
1681 | ||
1682 | /* get first chunk */ | |
73be9aeb | 1683 | while (lde_get_label_chunk () != 0) { |
f2232fdf | 1684 | log_warnx("Error getting first label chunk!"); |
d1fcf957 | 1685 | sleep(1); |
fea12efb | 1686 | } |
1687 | } | |
1688 | ||
1689 | static void | |
1690 | on_get_label_chunk_response(uint32_t start, uint32_t end) | |
1691 | { | |
1692 | struct label_chunk *new_label_chunk; | |
1693 | ||
08e4b244 | 1694 | debug_labels("label chunk assign: %u - %u", start, end); |
fea12efb | 1695 | |
1696 | new_label_chunk = calloc(1, sizeof(struct label_chunk)); | |
66749b59 | 1697 | if (!new_label_chunk) { |
1698 | log_warn("Error trying to allocate label chunk %u - %u", start, end); | |
1699 | return; | |
1700 | } | |
fea12efb | 1701 | |
1702 | new_label_chunk->start = start; | |
1703 | new_label_chunk->end = end; | |
1704 | new_label_chunk->used_mask = 0; | |
1705 | ||
1706 | listnode_add(label_chunk_list, (void *)new_label_chunk); | |
1707 | ||
1708 | /* let's update current if needed */ | |
1709 | if (!current_label_chunk) | |
1710 | current_label_chunk = listtail(label_chunk_list); | |
1711 | } | |
1712 | ||
1713 | static uint32_t | |
1714 | lde_get_next_label(void) | |
1715 | { | |
5afba51d | 1716 | struct label_chunk *label_chunk; |
2a178cdd RW |
1717 | uint32_t i, size; |
1718 | uint64_t pos; | |
5afba51d | 1719 | uint32_t label = NO_LABEL; |
fea12efb | 1720 | |
1721 | while (current_label_chunk) { | |
1722 | label_chunk = listgetdata(current_label_chunk); | |
1723 | if (!label_chunk) | |
1724 | goto end; | |
1725 | ||
1726 | /* try to get next free label in currently used label chunk */ | |
1727 | size = label_chunk->end - label_chunk->start + 1; | |
1728 | for (i = 0, pos = 1; i < size; i++, pos <<= 1) { | |
1729 | if (!(pos & label_chunk->used_mask)) { | |
1730 | label_chunk->used_mask |= pos; | |
1731 | label = label_chunk->start + i; | |
1732 | goto end; | |
1733 | } | |
1734 | } | |
1735 | current_label_chunk = listnextnode(current_label_chunk); | |
1736 | } | |
1737 | ||
1738 | end: | |
1739 | /* we moved till the last chunk, or were not able to find a label, | |
1740 | so let's ask for another one */ | |
5afba51d RW |
1741 | if (!current_label_chunk || |
1742 | current_label_chunk == listtail(label_chunk_list) || | |
1743 | label == NO_LABEL) { | |
fea12efb | 1744 | if (lde_get_label_chunk() != 0) |
1745 | log_warn("%s: Error getting label chunk!", __func__); | |
1746 | ||
1747 | } | |
1748 | ||
5afba51d | 1749 | return (label); |
fea12efb | 1750 | } |