2 * This file is free software: you may copy, redistribute and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation, either version 2 of the License, or (at your
5 * option) any later version.
7 * This file is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 * This file incorporates work covered by the following copyright and
18 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
20 Permission is hereby granted, free of charge, to any person obtaining a copy
21 of this software and associated documentation files (the "Software"), to deal
22 in the Software without restriction, including without limitation the rights
23 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24 copies of the Software, and to permit persons to whom the Software is
25 furnished to do so, subject to the following conditions:
27 The above copyright notice and this permission notice shall be included in
28 all copies or substantial portions of the Software.
30 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 /* quagga's includes */
46 #include "babel_zebra.h"
47 #include "babel_interface.h"
51 void babelz_zebra_init(void);
54 /* we must use a pointer because of zclient.c's functions (new, free). */
55 struct zclient
*zclient
;
56 static int zebra_config_write (struct vty
*vty
);
64 {BABEL_DEBUG_COMMON
, 1, "common"},
65 {BABEL_DEBUG_KERNEL
, 1, "kernel"},
66 {BABEL_DEBUG_FILTER
, 1, "filter"},
67 {BABEL_DEBUG_TIMEOUT
, 1, "timeout"},
68 {BABEL_DEBUG_IF
, 1, "interface"},
69 {BABEL_DEBUG_ROUTE
, 1, "route"},
70 {BABEL_DEBUG_ALL
, 1, "all"},
74 /* Zebra node structure. */
75 struct cmd_node zebra_node
=
78 "%s(config-router)# ",
83 /* Zebra route add and delete treatment (ipv6). */
85 babel_zebra_read_ipv6 (int command
, struct zclient
*zclient
,
90 unsigned long ifindex
= -1;
91 struct in6_addr nexthop
;
92 struct prefix_ipv6 prefix
;
96 memset (&nexthop
, 0, sizeof (struct in6_addr
));
97 memset (&api
, 0, sizeof(struct zapi_ipv6
));
98 memset (&prefix
, 0, sizeof (struct prefix_ipv6
));
100 /* Type, flags, message. */
101 api
.type
= stream_getc (s
);
102 api
.flags
= stream_getc (s
);
103 api
.message
= stream_getc (s
);
106 prefix
.family
= AF_INET6
;
107 prefix
.prefixlen
= stream_getc (s
);
108 stream_get (&prefix
.prefix
, s
, PSIZE (prefix
.prefixlen
));
110 /* Nexthop, ifindex, distance, metric. */
111 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
112 api
.nexthop_num
= stream_getc (s
);
113 stream_get (&nexthop
, s
, sizeof(nexthop
));
115 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
)) {
116 api
.ifindex_num
= stream_getc (s
);
117 ifindex
= stream_getl (s
);
119 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
120 api
.distance
= stream_getc (s
);
123 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
124 api
.metric
= stream_getl (s
);
128 if (command
== ZEBRA_IPV6_ROUTE_ADD
)
129 babel_ipv6_route_add(&api
, &prefix
, ifindex
, &nexthop
);
131 babel_ipv6_route_delete(&api
, &prefix
, ifindex
);
137 babel_zebra_read_ipv4 (int command
, struct zclient
*zclient
,
141 struct zapi_ipv4 api
;
142 unsigned long ifindex
= -1;
143 struct in_addr nexthop
;
144 struct prefix_ipv4 prefix
;
148 memset (&nexthop
, 0, sizeof (struct in_addr
));
149 memset (&api
, 0, sizeof(struct zapi_ipv4
));
150 memset (&prefix
, 0, sizeof (struct prefix_ipv4
));
152 /* Type, flags, message. */
153 api
.type
= stream_getc (s
);
154 api
.flags
= stream_getc (s
);
155 api
.message
= stream_getc (s
);
158 prefix
.family
= AF_INET
;
159 prefix
.prefixlen
= stream_getc (s
);
160 stream_get (&prefix
.prefix
, s
, PSIZE (prefix
.prefixlen
));
162 /* Nexthop, ifindex, distance, metric. */
163 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
164 api
.nexthop_num
= stream_getc (s
);
165 stream_get (&nexthop
, s
, sizeof(nexthop
));
167 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
)) {
168 api
.ifindex_num
= stream_getc (s
);
169 ifindex
= stream_getl (s
);
171 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
172 api
.distance
= stream_getc (s
);
175 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
176 api
.metric
= stream_getl (s
);
180 if (command
== ZEBRA_IPV6_ROUTE_ADD
) {
181 babel_ipv4_route_add(&api
, &prefix
, ifindex
, &nexthop
);
183 babel_ipv4_route_delete(&api
, &prefix
, ifindex
);
190 babel_redistribute_unset (int type
)
192 if (! zclient
->redist
[type
])
195 zclient
->redist
[type
] = 0;
197 if (zclient
->sock
> 0)
198 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, type
);
200 /* perhaps should we remove xroutes having the same type... */
206 /* [Babel Command] */
207 DEFUN (babel_redistribute_type
,
208 babel_redistribute_type_cmd
,
209 "redistribute " QUAGGA_REDIST_STR_BABELD
,
211 QUAGGA_REDIST_HELP_STR_BABELD
)
215 type
= proto_redistnum(AFI_IP6
, argv
[0]);
218 type
= proto_redistnum(AFI_IP
, argv
[0]);
221 vty_out(vty
, "Invalid type %s%s", argv
[0], VTY_NEWLINE
);
225 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD
, zclient
, type
);
229 /* [Babel Command] */
230 DEFUN (no_babel_redistribute_type
,
231 no_babel_redistribute_type_cmd
,
232 "no redistribute " QUAGGA_REDIST_STR_BABELD
,
235 QUAGGA_REDIST_HELP_STR_BABELD
)
239 type
= proto_redistnum(AFI_IP6
, argv
[0]);
242 type
= proto_redistnum(AFI_IP
, argv
[0]);
245 vty_out(vty
, "Invalid type %s%s", argv
[0], VTY_NEWLINE
);
249 return babel_redistribute_unset (type
);
253 /* [Babel Command] */
256 "debug (common|kernel|filter|timeout|interface|route|all)",
257 "Enable debug messages for specific or all part.\n"
258 "Common messages (default)\n"
262 "Interface messages\n"
268 for(i
= 0; debug_type
[i
].str
!= NULL
; i
++) {
269 if (strncmp (debug_type
[i
].str
, argv
[0],
270 debug_type
[i
].str_min_len
) == 0) {
271 debug
|= debug_type
[i
].type
;
276 vty_out(vty
, "Invalid type %s%s", argv
[0], VTY_NEWLINE
);
281 /* [Babel Command] */
282 DEFUN (no_babel_debug
,
284 "no debug (common|kernel|filter|timeout|interface|route|all)",
286 "Disable debug messages for specific or all part.\n"
287 "Common messages (default)\n"
291 "Interface messages\n"
297 for (i
= 0; debug_type
[i
].str
; i
++) {
298 if (strncmp(debug_type
[i
].str
, argv
[0],
299 debug_type
[i
].str_min_len
) == 0) {
300 debug
&= ~debug_type
[i
].type
;
304 vty_out(vty
, "Invalid type %s%s", argv
[0], VTY_NEWLINE
);
308 #endif /* NO_DEBUG */
311 void babelz_zebra_init(void)
313 zclient
= zclient_new();
314 zclient_init(zclient
, ZEBRA_ROUTE_BABEL
);
316 zclient
->interface_add
= babel_interface_add
;
317 zclient
->interface_delete
= babel_interface_delete
;
318 zclient
->interface_up
= babel_interface_up
;
319 zclient
->interface_down
= babel_interface_down
;
320 zclient
->interface_address_add
= babel_interface_address_add
;
321 zclient
->interface_address_delete
= babel_interface_address_delete
;
322 zclient
->ipv4_route_add
= babel_zebra_read_ipv4
;
323 zclient
->ipv4_route_delete
= babel_zebra_read_ipv4
;
324 zclient
->ipv6_route_add
= babel_zebra_read_ipv6
;
325 zclient
->ipv6_route_delete
= babel_zebra_read_ipv6
;
327 install_node (&zebra_node
, zebra_config_write
);
328 install_element(BABEL_NODE
, &babel_redistribute_type_cmd
);
329 install_element(BABEL_NODE
, &no_babel_redistribute_type_cmd
);
330 install_element(BABEL_NODE
, &babel_debug_cmd
);
331 install_element(BABEL_NODE
, &no_babel_debug_cmd
);
335 zebra_config_write (struct vty
*vty
)
337 if (! zclient
->enable
)
339 vty_out (vty
, "no router zebra%s", VTY_NEWLINE
);
342 else if (! zclient
->redist
[ZEBRA_ROUTE_BABEL
])
344 vty_out (vty
, "router zebra%s", VTY_NEWLINE
);
345 vty_out (vty
, " no redistribute babel%s", VTY_NEWLINE
);
352 babel_zebra_close_connexion(void)
354 zclient_stop(zclient
);