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