2 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 #include "babel_zebra.h"
31 #include "babel_interface.h"
35 void babelz_zebra_init(void);
38 /* we must use a pointer because of zclient.c's functions (new, free). */
39 struct zclient
*zclient
;
47 {BABEL_DEBUG_COMMON
, 1, "common"},
48 {BABEL_DEBUG_KERNEL
, 1, "kernel"},
49 {BABEL_DEBUG_FILTER
, 1, "filter"},
50 {BABEL_DEBUG_TIMEOUT
, 1, "timeout"},
51 {BABEL_DEBUG_IF
, 1, "interface"},
52 {BABEL_DEBUG_ROUTE
, 1, "route"},
53 {BABEL_DEBUG_ALL
, 1, "all"},
57 /* Zebra route add and delete treatment (ipv6). */
59 babel_zebra_read_ipv6 (int command
, struct zclient
*zclient
,
60 zebra_size_t length
, vrf_id_t vrf
)
64 unsigned long ifindex
= -1;
65 struct in6_addr nexthop
;
66 struct prefix_ipv6 prefix
;
70 memset (&nexthop
, 0, sizeof (struct in6_addr
));
71 memset (&api
, 0, sizeof(struct zapi_ipv6
));
72 memset (&prefix
, 0, sizeof (struct prefix_ipv6
));
74 /* Type, flags, message. */
75 api
.type
= stream_getc (s
);
76 api
.instance
= stream_getw (s
);
77 api
.flags
= stream_getl (s
);
78 api
.message
= stream_getc (s
);
81 prefix
.family
= AF_INET6
;
82 prefix
.prefixlen
= stream_getc (s
);
83 stream_get (&prefix
.prefix
, s
, PSIZE (prefix
.prefixlen
));
85 /* Nexthop, ifindex, distance, metric. */
86 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
87 api
.nexthop_num
= stream_getc (s
);
88 stream_get (&nexthop
, s
, sizeof(nexthop
));
90 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
)) {
91 api
.ifindex_num
= stream_getc (s
);
92 ifindex
= stream_getl (s
);
94 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
95 api
.distance
= stream_getc (s
);
98 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
99 api
.metric
= stream_getl (s
);
103 if (command
== ZEBRA_REDISTRIBUTE_IPV6_ADD
)
104 babel_ipv6_route_add(&api
, &prefix
, ifindex
, &nexthop
);
106 babel_ipv6_route_delete(&api
, &prefix
, ifindex
);
112 babel_zebra_read_ipv4 (int command
, struct zclient
*zclient
,
113 zebra_size_t length
, vrf_id_t vrf
)
116 struct zapi_ipv4 api
;
117 unsigned long ifindex
= -1;
118 struct in_addr nexthop
;
119 struct prefix_ipv4 prefix
;
123 memset (&nexthop
, 0, sizeof (struct in_addr
));
124 memset (&api
, 0, sizeof(struct zapi_ipv4
));
125 memset (&prefix
, 0, sizeof (struct prefix_ipv4
));
127 /* Type, flags, message. */
128 api
.type
= stream_getc (s
);
129 api
.instance
= stream_getw (s
);
130 api
.flags
= stream_getl (s
);
131 api
.message
= stream_getc (s
);
134 prefix
.family
= AF_INET
;
135 prefix
.prefixlen
= stream_getc (s
);
136 stream_get (&prefix
.prefix
, s
, PSIZE (prefix
.prefixlen
));
138 /* Nexthop, ifindex, distance, metric. */
139 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
140 api
.nexthop_num
= stream_getc (s
);
141 stream_get (&nexthop
, s
, sizeof(nexthop
));
143 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
)) {
144 api
.ifindex_num
= stream_getc (s
);
145 ifindex
= stream_getl (s
);
147 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
148 api
.distance
= stream_getc (s
);
151 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
152 api
.metric
= stream_getl (s
);
156 if (command
== ZEBRA_REDISTRIBUTE_IPV4_ADD
) {
157 babel_ipv4_route_add(&api
, &prefix
, ifindex
, &nexthop
);
159 babel_ipv4_route_delete(&api
, &prefix
, ifindex
);
165 /* [Babel Command] */
166 DEFUN (babel_redistribute_type
,
167 babel_redistribute_type_cmd
,
168 "[no] redistribute <ipv4 " FRR_IP_REDIST_STR_BABELD
"|ipv6 " FRR_IP6_REDIST_STR_BABELD
">",
171 "Redistribute IPv4 routes\n"
172 FRR_IP_REDIST_HELP_STR_BABELD
173 "Redistribute IPv6 routes\n"
174 FRR_IP6_REDIST_HELP_STR_BABELD
)
182 if (argv_find(argv
, argc
, "no", &idx
))
184 argv_find(argv
, argc
, "redistribute", &idx
);
185 family
= str2family(argv
[idx
+ 1]->text
);
187 return CMD_WARNING_CONFIG_FAILED
;
189 afi
= family2afi(family
);
191 return CMD_WARNING_CONFIG_FAILED
;
193 type
= proto_redistnum(afi
, argv
[idx
+ 2]->text
);
195 vty_out (vty
, "Invalid type %s\n", argv
[idx
+ 2]->arg
);
196 return CMD_WARNING_CONFIG_FAILED
;
200 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
, 0, VRF_DEFAULT
);
202 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
, 0, VRF_DEFAULT
);
203 /* perhaps should we remove xroutes having the same type... */
209 /* [Babel Command] */
212 "debug babel <common|kernel|filter|timeout|interface|route|all>",
213 "Enable debug messages for specific or all part.\n"
214 "Babel information\n"
215 "Common messages (default)\n"
219 "Interface messages\n"
225 for(i
= 0; debug_type
[i
].str
!= NULL
; i
++) {
226 if (strncmp (debug_type
[i
].str
, argv
[2]->arg
,
227 debug_type
[i
].str_min_len
) == 0) {
228 debug
|= debug_type
[i
].type
;
233 vty_out (vty
, "Invalid type %s\n", argv
[2]->arg
);
235 return CMD_WARNING_CONFIG_FAILED
;
238 /* [Babel Command] */
239 DEFUN (no_debug_babel
,
241 "no debug babel <common|kernel|filter|timeout|interface|route|all>",
243 "Disable debug messages for specific or all part.\n"
244 "Babel information\n"
245 "Common messages (default)\n"
249 "Interface messages\n"
255 for (i
= 0; debug_type
[i
].str
; i
++) {
256 if (strncmp(debug_type
[i
].str
, argv
[3]->arg
,
257 debug_type
[i
].str_min_len
) == 0) {
258 debug
&= ~debug_type
[i
].type
;
263 vty_out (vty
, "Invalid type %s\n", argv
[3]->arg
);
265 return CMD_WARNING_CONFIG_FAILED
;
267 #endif /* NO_DEBUG */
269 /* Output "debug" statement lines, if necessary. */
271 debug_babel_config_write (struct vty
* vty
)
278 if (debug
== BABEL_DEBUG_ALL
)
280 vty_out (vty
, "debug babel all\n");
284 for (i
= 0; debug_type
[i
].str
!= NULL
; i
++)
287 debug_type
[i
].type
!= BABEL_DEBUG_ALL
288 && CHECK_FLAG (debug
, debug_type
[i
].type
)
291 vty_out (vty
, "debug babel %s\n", debug_type
[i
].str
);
296 vty_out (vty
, "!\n");
300 #endif /* NO_DEBUG */
303 DEFUN_NOSH (show_debugging_babel
,
304 show_debugging_babel_cmd
,
305 "show debugging [babel]",
310 vty_out(vty
, "BABEL debugging status\n");
312 debug_babel_config_write(vty
);
318 babel_zebra_connected (struct zclient
*zclient
)
320 zclient_send_reg_requests (zclient
, VRF_DEFAULT
);
323 void babelz_zebra_init(void)
325 zclient
= zclient_new(master
);
326 zclient_init(zclient
, ZEBRA_ROUTE_BABEL
, 0);
328 zclient
->zebra_connected
= babel_zebra_connected
;
329 zclient
->interface_add
= babel_interface_add
;
330 zclient
->interface_delete
= babel_interface_delete
;
331 zclient
->interface_up
= babel_interface_up
;
332 zclient
->interface_down
= babel_interface_down
;
333 zclient
->interface_address_add
= babel_interface_address_add
;
334 zclient
->interface_address_delete
= babel_interface_address_delete
;
335 zclient
->redistribute_route_ipv4_add
= babel_zebra_read_ipv4
;
336 zclient
->redistribute_route_ipv4_del
= babel_zebra_read_ipv4
;
337 zclient
->redistribute_route_ipv6_add
= babel_zebra_read_ipv6
;
338 zclient
->redistribute_route_ipv6_del
= babel_zebra_read_ipv6
;
340 install_element(BABEL_NODE
, &babel_redistribute_type_cmd
);
341 install_element(ENABLE_NODE
, &debug_babel_cmd
);
342 install_element(ENABLE_NODE
, &no_debug_babel_cmd
);
343 install_element(CONFIG_NODE
, &debug_babel_cmd
);
344 install_element(CONFIG_NODE
, &no_debug_babel_cmd
);
346 install_element(VIEW_NODE
, &show_debugging_babel_cmd
);
350 babel_zebra_close_connexion(void)
352 zclient_stop(zclient
);
353 zclient_free(zclient
);