]>
Commit | Line | Data |
---|---|---|
eac6e3f0 RW |
1 | /* |
2 | * Copyright (C) 2016 by Open Source Routing. | |
3 | * | |
180fc2cd RW |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
eac6e3f0 | 8 | * |
180fc2cd | 9 | * This program is distributed in the hope that it will be useful, but |
eac6e3f0 RW |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * General Public License for more details. | |
13 | * | |
180fc2cd RW |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; see the file COPYING; if not, write to the | |
16 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
17 | * MA 02110-1301 USA | |
eac6e3f0 RW |
18 | */ |
19 | ||
20 | #include <zebra.h> | |
21 | ||
22 | #include "prefix.h" | |
23 | #include "stream.h" | |
24 | #include "memory.h" | |
25 | #include "zclient.h" | |
26 | #include "command.h" | |
27 | #include "network.h" | |
28 | #include "linklist.h" | |
ce549947 | 29 | #include "mpls.h" |
eac6e3f0 RW |
30 | |
31 | #include "ldpd.h" | |
32 | #include "ldpe.h" | |
33 | #include "lde.h" | |
34 | #include "log.h" | |
35 | #include "ldp_debug.h" | |
36 | ||
37 | static void ifp2kif(struct interface *, struct kif *); | |
38 | static void ifc2kaddr(struct interface *, struct connected *, | |
39 | struct kaddr *); | |
ce549947 | 40 | static int zebra_send_mpls_labels(int, struct kroute *); |
eac6e3f0 RW |
41 | static int ldp_router_id_update(int, struct zclient *, zebra_size_t, |
42 | vrf_id_t); | |
43 | static int ldp_interface_add(int, struct zclient *, zebra_size_t, | |
44 | vrf_id_t); | |
45 | static int ldp_interface_delete(int, struct zclient *, zebra_size_t, | |
46 | vrf_id_t); | |
47 | static int ldp_interface_status_change(int command, struct zclient *, | |
48 | zebra_size_t, vrf_id_t); | |
49 | static int ldp_interface_address_add(int, struct zclient *, zebra_size_t, | |
50 | vrf_id_t); | |
51 | static int ldp_interface_address_delete(int, struct zclient *, | |
52 | zebra_size_t, vrf_id_t); | |
53 | static int ldp_zebra_read_route(int, struct zclient *, zebra_size_t, | |
54 | vrf_id_t); | |
55 | static void ldp_zebra_connected(struct zclient *); | |
56 | ||
57 | static struct zclient *zclient; | |
58 | ||
59 | static void | |
60 | ifp2kif(struct interface *ifp, struct kif *kif) | |
61 | { | |
62 | memset(kif, 0, sizeof(*kif)); | |
63 | strlcpy(kif->ifname, ifp->name, sizeof(kif->ifname)); | |
64 | kif->ifindex = ifp->ifindex; | |
988ded8d | 65 | kif->operative = if_is_operative(ifp); |
26519d8c RW |
66 | if (ifp->ll_type == ZEBRA_LLT_ETHER) |
67 | memcpy(kif->mac, ifp->hw_addr, ETHER_ADDR_LEN); | |
eac6e3f0 RW |
68 | } |
69 | ||
70 | static void | |
71 | ifc2kaddr(struct interface *ifp, struct connected *ifc, struct kaddr *ka) | |
72 | { | |
73 | memset(ka, 0, sizeof(*ka)); | |
9cf67225 | 74 | strlcpy(ka->ifname, ifp->name, sizeof(ka->ifname)); |
eac6e3f0 RW |
75 | ka->ifindex = ifp->ifindex; |
76 | ka->af = ifc->address->family; | |
77 | ka->prefixlen = ifc->address->prefixlen; | |
78 | ||
79 | switch (ka->af) { | |
80 | case AF_INET: | |
81 | ka->addr.v4 = ifc->address->u.prefix4; | |
82 | if (ifc->destination) | |
83 | ka->dstbrd.v4 = ifc->destination->u.prefix4; | |
84 | break; | |
85 | case AF_INET6: | |
86 | ka->addr.v6 = ifc->address->u.prefix6; | |
87 | if (ifc->destination) | |
88 | ka->dstbrd.v6 = ifc->destination->u.prefix6; | |
89 | break; | |
90 | default: | |
91 | break; | |
92 | } | |
93 | } | |
94 | ||
ce549947 RW |
95 | static int |
96 | zebra_send_mpls_labels(int cmd, struct kroute *kr) | |
97 | { | |
98 | struct stream *s; | |
99 | ||
100 | if (kr->local_label < MPLS_LABEL_RESERVED_MAX || | |
101 | kr->remote_label == NO_LABEL) | |
102 | return (0); | |
103 | ||
88d88a9c | 104 | debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)", |
ce549947 | 105 | log_addr(kr->af, &kr->prefix), kr->prefixlen, |
88d88a9c RW |
106 | log_addr(kr->af, &kr->nexthop), kr->ifindex, |
107 | log_label(kr->local_label), log_label(kr->remote_label), | |
ce549947 RW |
108 | (cmd == ZEBRA_MPLS_LABELS_ADD) ? "add" : "delete"); |
109 | ||
110 | /* Reset stream. */ | |
111 | s = zclient->obuf; | |
112 | stream_reset(s); | |
113 | ||
114 | zclient_create_header(s, cmd, VRF_DEFAULT); | |
115 | stream_putc(s, ZEBRA_LSP_LDP); | |
116 | stream_putl(s, kr->af); | |
117 | switch (kr->af) { | |
118 | case AF_INET: | |
119 | stream_put_in_addr(s, &kr->prefix.v4); | |
120 | stream_putc(s, kr->prefixlen); | |
121 | stream_put_in_addr(s, &kr->nexthop.v4); | |
122 | break; | |
123 | case AF_INET6: | |
124 | stream_write(s, (u_char *)&kr->prefix.v6, 16); | |
125 | stream_putc(s, kr->prefixlen); | |
126 | stream_write(s, (u_char *)&kr->nexthop.v6, 16); | |
127 | break; | |
128 | default: | |
129 | fatalx("kr_change: unknown af"); | |
130 | } | |
88d88a9c | 131 | stream_putl(s, kr->ifindex); |
ce549947 RW |
132 | stream_putc(s, kr->priority); |
133 | stream_putl(s, kr->local_label); | |
134 | stream_putl(s, kr->remote_label); | |
135 | ||
136 | /* Put length at the first point of the stream. */ | |
137 | stream_putw_at(s, 0, stream_get_endp(s)); | |
138 | ||
139 | return (zclient_send_message(zclient)); | |
140 | } | |
141 | ||
eac6e3f0 RW |
142 | int |
143 | kr_change(struct kroute *kr) | |
144 | { | |
ce549947 | 145 | return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD, kr)); |
eac6e3f0 RW |
146 | } |
147 | ||
148 | int | |
149 | kr_delete(struct kroute *kr) | |
150 | { | |
ce549947 | 151 | return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE, kr)); |
eac6e3f0 RW |
152 | } |
153 | ||
154 | int | |
155 | kmpw_set(struct kpw *kpw) | |
156 | { | |
157 | /* TODO */ | |
158 | return (0); | |
159 | } | |
160 | ||
161 | int | |
162 | kmpw_unset(struct kpw *kpw) | |
163 | { | |
164 | /* TODO */ | |
165 | return (0); | |
166 | } | |
167 | ||
168 | void | |
169 | kif_redistribute(const char *ifname) | |
170 | { | |
171 | struct listnode *node, *cnode; | |
172 | struct interface *ifp; | |
173 | struct connected *ifc; | |
174 | struct kif kif; | |
175 | struct kaddr ka; | |
176 | ||
177 | for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT), node, ifp)) { | |
178 | if (ifname && strcmp(ifname, ifp->name) != 0) | |
179 | continue; | |
180 | ||
181 | ifp2kif(ifp, &kif); | |
52b530fc | 182 | main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif)); |
eac6e3f0 RW |
183 | |
184 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, ifc)) { | |
185 | ifc2kaddr(ifp, ifc, &ka); | |
186 | main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka, | |
187 | sizeof(ka)); | |
188 | } | |
189 | } | |
190 | } | |
191 | ||
192 | static int | |
193 | ldp_router_id_update(int command, struct zclient *zclient, zebra_size_t length, | |
194 | vrf_id_t vrf_id) | |
195 | { | |
196 | struct prefix router_id; | |
197 | ||
198 | zebra_router_id_update_read(zclient->ibuf, &router_id); | |
199 | ||
200 | if (bad_addr_v4(router_id.u.prefix4)) | |
201 | return (0); | |
202 | ||
203 | debug_zebra_in("router-id update %s", inet_ntoa(router_id.u.prefix4)); | |
204 | ||
205 | global.rtr_id.s_addr = router_id.u.prefix4.s_addr; | |
206 | main_imsg_compose_ldpe(IMSG_RTRID_UPDATE, 0, &global.rtr_id, | |
207 | sizeof(global.rtr_id)); | |
208 | ||
209 | return (0); | |
210 | } | |
211 | ||
212 | static int | |
213 | ldp_interface_add(int command, struct zclient *zclient, zebra_size_t length, | |
214 | vrf_id_t vrf_id) | |
215 | { | |
216 | struct interface *ifp; | |
217 | struct kif kif; | |
218 | ||
219 | ifp = zebra_interface_add_read(zclient->ibuf, vrf_id); | |
220 | debug_zebra_in("interface add %s index %d mtu %d", ifp->name, | |
221 | ifp->ifindex, ifp->mtu); | |
222 | ||
223 | ifp2kif(ifp, &kif); | |
52b530fc | 224 | main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif)); |
eac6e3f0 RW |
225 | |
226 | return (0); | |
227 | } | |
228 | ||
229 | static int | |
230 | ldp_interface_delete(int command, struct zclient *zclient, zebra_size_t length, | |
231 | vrf_id_t vrf_id) | |
232 | { | |
233 | struct interface *ifp; | |
9cf67225 | 234 | struct kif kif; |
eac6e3f0 RW |
235 | |
236 | /* zebra_interface_state_read() updates interface structure in iflist */ | |
237 | ifp = zebra_interface_state_read(zclient->ibuf, vrf_id); | |
238 | if (ifp == NULL) | |
239 | return (0); | |
240 | ||
241 | debug_zebra_in("interface delete %s index %d mtu %d", ifp->name, | |
242 | ifp->ifindex, ifp->mtu); | |
243 | ||
244 | /* To support pseudo interface do not free interface structure. */ | |
245 | /* if_delete(ifp); */ | |
9cf67225 RW |
246 | ifp->ifindex = IFINDEX_DELETED; |
247 | ||
248 | ifp2kif(ifp, &kif); | |
249 | main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif)); | |
eac6e3f0 RW |
250 | |
251 | return (0); | |
252 | } | |
253 | ||
254 | static int | |
255 | ldp_interface_status_change(int command, struct zclient *zclient, | |
256 | zebra_size_t length, vrf_id_t vrf_id) | |
257 | { | |
258 | struct interface *ifp; | |
259 | struct listnode *node; | |
260 | struct connected *ifc; | |
261 | struct kif kif; | |
262 | struct kaddr ka; | |
eac6e3f0 RW |
263 | |
264 | /* | |
265 | * zebra_interface_state_read() updates interface structure in | |
266 | * iflist. | |
267 | */ | |
268 | ifp = zebra_interface_state_read(zclient->ibuf, vrf_id); | |
269 | if (ifp == NULL) | |
270 | return (0); | |
271 | ||
272 | debug_zebra_in("interface %s state update", ifp->name); | |
273 | ||
274 | ifp2kif(ifp, &kif); | |
52b530fc | 275 | main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif)); |
eac6e3f0 | 276 | |
988ded8d | 277 | if (if_is_operative(ifp)) { |
eac6e3f0 RW |
278 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) { |
279 | ifc2kaddr(ifp, ifc, &ka); | |
280 | main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka, | |
281 | sizeof(ka)); | |
282 | } | |
283 | } else { | |
284 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) { | |
285 | ifc2kaddr(ifp, ifc, &ka); | |
286 | main_imsg_compose_ldpe(IMSG_DELADDR, 0, &ka, | |
287 | sizeof(ka)); | |
288 | } | |
289 | } | |
290 | ||
291 | return (0); | |
292 | } | |
293 | ||
294 | static int | |
295 | ldp_interface_address_add(int command, struct zclient *zclient, | |
296 | zebra_size_t length, vrf_id_t vrf_id) | |
297 | { | |
298 | struct connected *ifc; | |
299 | struct interface *ifp; | |
300 | struct kaddr ka; | |
301 | ||
302 | ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id); | |
303 | if (ifc == NULL) | |
304 | return (0); | |
305 | ||
306 | ifp = ifc->ifp; | |
307 | ifc2kaddr(ifp, ifc, &ka); | |
308 | ||
309 | /* Filter invalid addresses. */ | |
310 | if (bad_addr(ka.af, &ka.addr)) | |
311 | return (0); | |
312 | ||
9cf67225 RW |
313 | debug_zebra_in("address add %s/%u interface %s", |
314 | log_addr(ka.af, &ka.addr), ka.prefixlen, ifp->name); | |
eac6e3f0 RW |
315 | |
316 | /* notify ldpe about new address */ | |
317 | main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka, sizeof(ka)); | |
318 | ||
319 | return (0); | |
320 | } | |
321 | ||
322 | static int | |
323 | ldp_interface_address_delete(int command, struct zclient *zclient, | |
324 | zebra_size_t length, vrf_id_t vrf_id) | |
325 | { | |
326 | struct connected *ifc; | |
327 | struct interface *ifp; | |
328 | struct kaddr ka; | |
329 | ||
330 | ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id); | |
331 | if (ifc == NULL) | |
332 | return (0); | |
333 | ||
334 | ifp = ifc->ifp; | |
335 | ifc2kaddr(ifp, ifc, &ka); | |
336 | connected_free(ifc); | |
337 | ||
338 | /* Filter invalid addresses. */ | |
339 | if (bad_addr(ka.af, &ka.addr)) | |
340 | return (0); | |
341 | ||
9cf67225 RW |
342 | debug_zebra_in("address delete %s/%u interface %s", |
343 | log_addr(ka.af, &ka.addr), ka.prefixlen, ifp->name); | |
eac6e3f0 RW |
344 | |
345 | /* notify ldpe about removed address */ | |
346 | main_imsg_compose_ldpe(IMSG_DELADDR, 0, &ka, sizeof(ka)); | |
347 | ||
348 | return (0); | |
349 | } | |
350 | ||
351 | static int | |
352 | ldp_zebra_read_route(int command, struct zclient *zclient, zebra_size_t length, | |
353 | vrf_id_t vrf_id) | |
354 | { | |
355 | struct stream *s; | |
356 | u_char type; | |
357 | u_char message_flags; | |
358 | struct kroute kr; | |
8cb1fc45 | 359 | int nhnum = 0, nhlen; |
eac6e3f0 | 360 | size_t nhmark; |
1f2ff5b5 | 361 | int add = 0; |
eac6e3f0 RW |
362 | |
363 | memset(&kr, 0, sizeof(kr)); | |
364 | s = zclient->ibuf; | |
365 | ||
366 | type = stream_getc(s); | |
a695cc7b RW |
367 | switch (type) { |
368 | case ZEBRA_ROUTE_CONNECT: | |
eac6e3f0 | 369 | kr.flags |= F_CONNECTED; |
a695cc7b RW |
370 | break; |
371 | case ZEBRA_ROUTE_BGP: | |
372 | /* LDP should follow the IGP and ignore BGP routes */ | |
373 | return (0); | |
374 | default: | |
375 | break; | |
376 | } | |
377 | ||
3f67fb9c | 378 | stream_getl(s); /* flags, unused */ |
eac6e3f0 RW |
379 | stream_getw(s); /* instance, unused */ |
380 | message_flags = stream_getc(s); | |
eac6e3f0 RW |
381 | |
382 | switch (command) { | |
eac6e3f0 | 383 | case ZEBRA_REDISTRIBUTE_IPV4_ADD: |
eac6e3f0 RW |
384 | case ZEBRA_REDISTRIBUTE_IPV4_DEL: |
385 | kr.af = AF_INET; | |
386 | nhlen = sizeof(struct in_addr); | |
387 | break; | |
eac6e3f0 | 388 | case ZEBRA_REDISTRIBUTE_IPV6_ADD: |
eac6e3f0 RW |
389 | case ZEBRA_REDISTRIBUTE_IPV6_DEL: |
390 | kr.af = AF_INET6; | |
391 | nhlen = sizeof(struct in6_addr); | |
392 | break; | |
393 | default: | |
394 | fatalx("ldp_zebra_read_route: unknown command"); | |
395 | } | |
396 | kr.prefixlen = stream_getc(s); | |
397 | stream_get(&kr.prefix, s, PSIZE(kr.prefixlen)); | |
398 | ||
399 | if (bad_addr(kr.af, &kr.prefix) || | |
400 | (kr.af == AF_INET6 && IN6_IS_SCOPE_EMBED(&kr.prefix.v6))) | |
401 | return (0); | |
402 | ||
81a164e2 CF |
403 | if (kr.af == AF_INET6 && |
404 | CHECK_FLAG(message_flags, ZAPI_MESSAGE_SRCPFX)) { | |
405 | uint8_t src_prefixlen; | |
406 | ||
407 | src_prefixlen = stream_getc(s); | |
408 | ||
409 | /* we completely ignore srcdest routes for now. */ | |
410 | if (src_prefixlen) | |
411 | return (0); | |
412 | } | |
413 | ||
8cb1fc45 RW |
414 | if (CHECK_FLAG(message_flags, ZAPI_MESSAGE_NEXTHOP)) { |
415 | nhnum = stream_getc(s); | |
416 | nhmark = stream_get_getp(s); | |
417 | stream_set_getp(s, nhmark + nhnum * (nhlen + 5)); | |
418 | } | |
eac6e3f0 RW |
419 | |
420 | if (CHECK_FLAG(message_flags, ZAPI_MESSAGE_DISTANCE)) | |
421 | kr.priority = stream_getc(s); | |
422 | if (CHECK_FLAG(message_flags, ZAPI_MESSAGE_METRIC)) | |
423 | stream_getl(s); /* metric, not used */ | |
424 | ||
8cb1fc45 RW |
425 | if (CHECK_FLAG(message_flags, ZAPI_MESSAGE_NEXTHOP)) |
426 | stream_set_getp(s, nhmark); | |
427 | ||
1f2ff5b5 RW |
428 | if (command == ZEBRA_REDISTRIBUTE_IPV4_ADD || |
429 | command == ZEBRA_REDISTRIBUTE_IPV6_ADD) | |
430 | add = 1; | |
431 | ||
432 | if (nhnum == 0) | |
433 | debug_zebra_in("route %s %s/%d (%s)", (add) ? "add" : "delete", | |
434 | log_addr(kr.af, &kr.prefix), kr.prefixlen, | |
435 | zebra_route_string(type)); | |
eac6e3f0 RW |
436 | |
437 | /* loop through all the nexthops */ | |
438 | for (; nhnum > 0; nhnum--) { | |
439 | switch (kr.af) { | |
440 | case AF_INET: | |
441 | kr.nexthop.v4.s_addr = stream_get_ipv4(s); | |
442 | break; | |
443 | case AF_INET6: | |
444 | stream_get(&kr.nexthop.v6, s, sizeof(kr.nexthop.v6)); | |
445 | break; | |
446 | default: | |
447 | break; | |
448 | } | |
449 | stream_getc(s); /* ifindex_num, unused. */ | |
450 | kr.ifindex = stream_getl(s); | |
451 | ||
1f2ff5b5 RW |
452 | debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)", |
453 | (add) ? "add" : "delete", log_addr(kr.af, &kr.prefix), | |
454 | kr.prefixlen, log_addr(kr.af, &kr.nexthop), kr.ifindex, | |
455 | zebra_route_string(type)); | |
456 | ||
457 | if (add) | |
eac6e3f0 RW |
458 | main_imsg_compose_lde(IMSG_NETWORK_ADD, 0, &kr, |
459 | sizeof(kr)); | |
eac6e3f0 RW |
460 | } |
461 | ||
8cb1fc45 | 462 | main_imsg_compose_lde(IMSG_NETWORK_UPDATE, 0, &kr, sizeof(kr)); |
134970a2 | 463 | |
eac6e3f0 RW |
464 | return (0); |
465 | } | |
466 | ||
467 | static void | |
468 | ldp_zebra_connected(struct zclient *zclient) | |
469 | { | |
eac6e3f0 | 470 | zclient_send_reg_requests(zclient, VRF_DEFAULT); |
a695cc7b RW |
471 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, |
472 | ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); | |
473 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6, | |
474 | ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); | |
eac6e3f0 RW |
475 | } |
476 | ||
477 | void | |
478 | ldp_zebra_init(struct thread_master *master) | |
479 | { | |
480 | /* Set default values. */ | |
481 | zclient = zclient_new(master); | |
482 | zclient_init(zclient, ZEBRA_ROUTE_LDP, 0); | |
483 | ||
484 | /* set callbacks */ | |
485 | zclient->zebra_connected = ldp_zebra_connected; | |
486 | zclient->router_id_update = ldp_router_id_update; | |
487 | zclient->interface_add = ldp_interface_add; | |
488 | zclient->interface_delete = ldp_interface_delete; | |
489 | zclient->interface_up = ldp_interface_status_change; | |
490 | zclient->interface_down = ldp_interface_status_change; | |
491 | zclient->interface_address_add = ldp_interface_address_add; | |
492 | zclient->interface_address_delete = ldp_interface_address_delete; | |
eac6e3f0 RW |
493 | zclient->redistribute_route_ipv4_add = ldp_zebra_read_route; |
494 | zclient->redistribute_route_ipv4_del = ldp_zebra_read_route; | |
eac6e3f0 RW |
495 | zclient->redistribute_route_ipv6_add = ldp_zebra_read_route; |
496 | zclient->redistribute_route_ipv6_del = ldp_zebra_read_route; | |
497 | } | |
64dffe25 RW |
498 | |
499 | void | |
500 | ldp_zebra_destroy(void) | |
501 | { | |
502 | zclient_stop(zclient); | |
503 | zclient_free(zclient); | |
504 | zclient = NULL; | |
505 | } |