Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 1 | /* |
| 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. |
| 6 | * |
| 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. |
| 11 | * |
| 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/>. |
| 14 | * |
| 15 | * This file incorporates work covered by the following copyright and |
| 16 | * permission notice: |
| 17 | * |
| 18 | Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek |
| 19 | |
| 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: |
| 26 | |
| 27 | The above copyright notice and this permission notice shall be included in |
| 28 | all copies or substantial portions of the Software. |
| 29 | |
| 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 |
| 36 | THE SOFTWARE. |
| 37 | */ |
| 38 | |
| 39 | /* quagga's includes */ |
| 40 | #include <zebra.h> |
| 41 | #include "command.h" |
| 42 | #include "zclient.h" |
| 43 | #include "stream.h" |
| 44 | |
| 45 | /* babel's includes*/ |
| 46 | #include "babel_zebra.h" |
| 47 | #include "babel_interface.h" |
| 48 | #include "xroute.h" |
Matthieu Boutier | f1305cb | 2012-01-20 00:19:35 +0100 | [diff] [blame] | 49 | #include "util.h" |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 50 | |
| 51 | void babelz_zebra_init(void); |
| 52 | |
| 53 | |
| 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); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 57 | |
Matthieu Boutier | f1305cb | 2012-01-20 00:19:35 +0100 | [diff] [blame] | 58 | /* Debug types */ |
| 59 | static struct { |
| 60 | int type; |
| 61 | int str_min_len; |
| 62 | const char *str; |
| 63 | } debug_type[] = { |
| 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"}, |
| 71 | {0, 0, NULL} |
| 72 | }; |
| 73 | |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 74 | /* Zebra node structure. */ |
| 75 | struct cmd_node zebra_node = |
| 76 | { |
| 77 | ZEBRA_NODE, |
| 78 | "%s(config-router)# ", |
| 79 | 1 /* vtysh? yes */ |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | /* Zebra route add and delete treatment (ipv6). */ |
| 84 | static int |
| 85 | babel_zebra_read_ipv6 (int command, struct zclient *zclient, |
| 86 | zebra_size_t length) |
| 87 | { |
| 88 | struct stream *s; |
| 89 | struct zapi_ipv6 api; |
| 90 | unsigned long ifindex = -1; |
| 91 | struct in6_addr nexthop; |
| 92 | struct prefix_ipv6 prefix; |
| 93 | |
| 94 | s = zclient->ibuf; |
| 95 | ifindex = 0; |
| 96 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 97 | memset (&api, 0, sizeof(struct zapi_ipv6)); |
| 98 | memset (&prefix, 0, sizeof (struct prefix_ipv6)); |
| 99 | |
| 100 | /* Type, flags, message. */ |
| 101 | api.type = stream_getc (s); |
| 102 | api.flags = stream_getc (s); |
| 103 | api.message = stream_getc (s); |
| 104 | |
| 105 | /* IPv6 prefix. */ |
| 106 | prefix.family = AF_INET6; |
| 107 | prefix.prefixlen = stream_getc (s); |
| 108 | stream_get (&prefix.prefix, s, PSIZE (prefix.prefixlen)); |
| 109 | |
| 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)); |
| 114 | } |
| 115 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) { |
| 116 | api.ifindex_num = stream_getc (s); |
| 117 | ifindex = stream_getl (s); |
| 118 | } |
| 119 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 120 | api.distance = stream_getc (s); |
| 121 | else |
| 122 | api.distance = 0; |
| 123 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 124 | api.metric = stream_getl (s); |
| 125 | else |
| 126 | api.metric = 0; |
| 127 | |
| 128 | if (command == ZEBRA_IPV6_ROUTE_ADD) |
| 129 | babel_ipv6_route_add(&api, &prefix, ifindex, &nexthop); |
| 130 | else |
| 131 | babel_ipv6_route_delete(&api, &prefix, ifindex); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int |
| 137 | babel_zebra_read_ipv4 (int command, struct zclient *zclient, |
| 138 | zebra_size_t length) |
| 139 | { |
| 140 | struct stream *s; |
| 141 | struct zapi_ipv4 api; |
| 142 | unsigned long ifindex = -1; |
| 143 | struct in_addr nexthop; |
| 144 | struct prefix_ipv4 prefix; |
| 145 | |
| 146 | s = zclient->ibuf; |
| 147 | ifindex = 0; |
| 148 | memset (&nexthop, 0, sizeof (struct in_addr)); |
| 149 | memset (&api, 0, sizeof(struct zapi_ipv4)); |
| 150 | memset (&prefix, 0, sizeof (struct prefix_ipv4)); |
| 151 | |
| 152 | /* Type, flags, message. */ |
| 153 | api.type = stream_getc (s); |
| 154 | api.flags = stream_getc (s); |
| 155 | api.message = stream_getc (s); |
| 156 | |
| 157 | /* IPv6 prefix. */ |
| 158 | prefix.family = AF_INET; |
| 159 | prefix.prefixlen = stream_getc (s); |
| 160 | stream_get (&prefix.prefix, s, PSIZE (prefix.prefixlen)); |
| 161 | |
| 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)); |
| 166 | } |
| 167 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) { |
| 168 | api.ifindex_num = stream_getc (s); |
| 169 | ifindex = stream_getl (s); |
| 170 | } |
| 171 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 172 | api.distance = stream_getc (s); |
| 173 | else |
| 174 | api.distance = 0; |
| 175 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 176 | api.metric = stream_getl (s); |
| 177 | else |
| 178 | api.metric = 0; |
| 179 | |
| 180 | if (command == ZEBRA_IPV6_ROUTE_ADD) { |
| 181 | babel_ipv4_route_add(&api, &prefix, ifindex, &nexthop); |
| 182 | } else { |
| 183 | babel_ipv4_route_delete(&api, &prefix, ifindex); |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 189 | /* [Babel Command] */ |
| 190 | DEFUN (babel_redistribute_type, |
| 191 | babel_redistribute_type_cmd, |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 192 | "redistribute " QUAGGA_REDIST_STR_BABELD, |
| 193 | "Redistribute\n" |
| 194 | QUAGGA_REDIST_HELP_STR_BABELD) |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 195 | { |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 196 | int type; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 197 | |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 198 | type = proto_redistnum(AFI_IP6, argv[0]); |
| 199 | |
| 200 | if (type < 0) |
| 201 | type = proto_redistnum(AFI_IP, argv[0]); |
| 202 | |
| 203 | if (type < 0) { |
| 204 | vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE); |
| 205 | return CMD_WARNING; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 208 | zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type); |
| 209 | return CMD_SUCCESS; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* [Babel Command] */ |
| 213 | DEFUN (no_babel_redistribute_type, |
| 214 | no_babel_redistribute_type_cmd, |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 215 | "no redistribute " QUAGGA_REDIST_STR_BABELD, |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 216 | NO_STR |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 217 | "Redistribute\n" |
| 218 | QUAGGA_REDIST_HELP_STR_BABELD) |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 219 | { |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 220 | int type; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 221 | |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 222 | type = proto_redistnum(AFI_IP6, argv[0]); |
| 223 | |
| 224 | if (type < 0) |
| 225 | type = proto_redistnum(AFI_IP, argv[0]); |
| 226 | |
| 227 | if (type < 0) { |
| 228 | vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE); |
| 229 | return CMD_WARNING; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Denis Ovsienko | 359be3d | 2012-02-11 15:25:01 +0400 | [diff] [blame] | 232 | zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type); |
| 233 | /* perhaps should we remove xroutes having the same type... */ |
| 234 | return CMD_SUCCESS; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Matthieu Boutier | f1305cb | 2012-01-20 00:19:35 +0100 | [diff] [blame] | 237 | #ifndef NO_DEBUG |
| 238 | /* [Babel Command] */ |
| 239 | DEFUN (babel_debug, |
| 240 | babel_debug_cmd, |
| 241 | "debug (common|kernel|filter|timeout|interface|route|all)", |
| 242 | "Enable debug messages for specific or all part.\n" |
| 243 | "Common messages (default)\n" |
| 244 | "Kernel messages\n" |
| 245 | "Filter messages\n" |
| 246 | "Timeout messages\n" |
| 247 | "Interface messages\n" |
| 248 | "Route messages\n" |
| 249 | "All messages\n") |
| 250 | { |
| 251 | int i; |
| 252 | |
| 253 | for(i = 0; debug_type[i].str != NULL; i++) { |
| 254 | if (strncmp (debug_type[i].str, argv[0], |
| 255 | debug_type[i].str_min_len) == 0) { |
| 256 | debug |= debug_type[i].type; |
| 257 | return CMD_SUCCESS; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE); |
| 262 | |
| 263 | return CMD_WARNING; |
| 264 | } |
| 265 | |
| 266 | /* [Babel Command] */ |
| 267 | DEFUN (no_babel_debug, |
| 268 | no_babel_debug_cmd, |
| 269 | "no debug (common|kernel|filter|timeout|interface|route|all)", |
| 270 | NO_STR |
| 271 | "Disable debug messages for specific or all part.\n" |
| 272 | "Common messages (default)\n" |
| 273 | "Kernel messages\n" |
| 274 | "Filter messages\n" |
| 275 | "Timeout messages\n" |
| 276 | "Interface messages\n" |
| 277 | "Route messages\n" |
| 278 | "All messages\n") |
| 279 | { |
| 280 | int i; |
| 281 | |
| 282 | for (i = 0; debug_type[i].str; i++) { |
| 283 | if (strncmp(debug_type[i].str, argv[0], |
| 284 | debug_type[i].str_min_len) == 0) { |
| 285 | debug &= ~debug_type[i].type; |
Matthieu Boutier | a0edef1 | 2012-01-27 22:54:11 +0100 | [diff] [blame] | 286 | return CMD_SUCCESS; |
Matthieu Boutier | f1305cb | 2012-01-20 00:19:35 +0100 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE); |
| 291 | |
| 292 | return CMD_WARNING; |
| 293 | } |
| 294 | #endif /* NO_DEBUG */ |
| 295 | |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 296 | |
| 297 | void babelz_zebra_init(void) |
| 298 | { |
| 299 | zclient = zclient_new(); |
| 300 | zclient_init(zclient, ZEBRA_ROUTE_BABEL); |
| 301 | |
| 302 | zclient->interface_add = babel_interface_add; |
| 303 | zclient->interface_delete = babel_interface_delete; |
| 304 | zclient->interface_up = babel_interface_up; |
| 305 | zclient->interface_down = babel_interface_down; |
| 306 | zclient->interface_address_add = babel_interface_address_add; |
| 307 | zclient->interface_address_delete = babel_interface_address_delete; |
| 308 | zclient->ipv4_route_add = babel_zebra_read_ipv4; |
| 309 | zclient->ipv4_route_delete = babel_zebra_read_ipv4; |
| 310 | zclient->ipv6_route_add = babel_zebra_read_ipv6; |
| 311 | zclient->ipv6_route_delete = babel_zebra_read_ipv6; |
| 312 | |
| 313 | install_node (&zebra_node, zebra_config_write); |
| 314 | install_element(BABEL_NODE, &babel_redistribute_type_cmd); |
| 315 | install_element(BABEL_NODE, &no_babel_redistribute_type_cmd); |
Matthieu Boutier | f1305cb | 2012-01-20 00:19:35 +0100 | [diff] [blame] | 316 | install_element(BABEL_NODE, &babel_debug_cmd); |
| 317 | install_element(BABEL_NODE, &no_babel_debug_cmd); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static int |
| 321 | zebra_config_write (struct vty *vty) |
| 322 | { |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 323 | if (! zclient->enable) |
| 324 | { |
| 325 | vty_out (vty, "no router zebra%s", VTY_NEWLINE); |
| 326 | return 1; |
| 327 | } |
| 328 | else if (! zclient->redist[ZEBRA_ROUTE_BABEL]) |
| 329 | { |
| 330 | vty_out (vty, "router zebra%s", VTY_NEWLINE); |
| 331 | vty_out (vty, " no redistribute babel%s", VTY_NEWLINE); |
| 332 | return 1; |
| 333 | } |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | void |
| 338 | babel_zebra_close_connexion(void) |
| 339 | { |
| 340 | zclient_stop(zclient); |
| 341 | } |