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 | |
| 189 | static int |
| 190 | babel_redistribute_unset (int type) |
| 191 | { |
| 192 | if (! zclient->redist[type]) |
| 193 | return CMD_SUCCESS; |
| 194 | |
| 195 | zclient->redist[type] = 0; |
| 196 | |
| 197 | if (zclient->sock > 0) |
| 198 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type); |
| 199 | |
| 200 | /* perhaps should we remove xroutes having the same type... */ |
| 201 | |
| 202 | return CMD_SUCCESS; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /* [Babel Command] */ |
| 207 | DEFUN (babel_redistribute_type, |
| 208 | babel_redistribute_type_cmd, |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 209 | "redistribute " QUAGGA_REDIST_STR_BABELD, |
| 210 | "Redistribute\n" |
| 211 | QUAGGA_REDIST_HELP_STR_BABELD) |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 212 | { |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 213 | int type; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 214 | |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 215 | type = proto_redistnum(AFI_IP6, argv[0]); |
| 216 | |
| 217 | if (type < 0) |
| 218 | type = proto_redistnum(AFI_IP, argv[0]); |
| 219 | |
| 220 | if (type < 0) { |
| 221 | vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE); |
| 222 | return CMD_WARNING; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 225 | zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type); |
| 226 | return CMD_SUCCESS; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* [Babel Command] */ |
| 230 | DEFUN (no_babel_redistribute_type, |
| 231 | no_babel_redistribute_type_cmd, |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 232 | "no redistribute " QUAGGA_REDIST_STR_BABELD, |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 233 | NO_STR |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 234 | "Redistribute\n" |
| 235 | QUAGGA_REDIST_HELP_STR_BABELD) |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 236 | { |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 237 | int type; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 238 | |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 239 | type = proto_redistnum(AFI_IP6, argv[0]); |
| 240 | |
| 241 | if (type < 0) |
| 242 | type = proto_redistnum(AFI_IP, argv[0]); |
| 243 | |
| 244 | if (type < 0) { |
| 245 | vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE); |
| 246 | return CMD_WARNING; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Matthieu Boutier | 05c943a | 2012-01-26 22:15:34 +0100 | [diff] [blame] | 249 | return babel_redistribute_unset (type); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Matthieu Boutier | f1305cb | 2012-01-20 00:19:35 +0100 | [diff] [blame] | 252 | #ifndef NO_DEBUG |
| 253 | /* [Babel Command] */ |
| 254 | DEFUN (babel_debug, |
| 255 | babel_debug_cmd, |
| 256 | "debug (common|kernel|filter|timeout|interface|route|all)", |
| 257 | "Enable debug messages for specific or all part.\n" |
| 258 | "Common messages (default)\n" |
| 259 | "Kernel messages\n" |
| 260 | "Filter messages\n" |
| 261 | "Timeout messages\n" |
| 262 | "Interface messages\n" |
| 263 | "Route messages\n" |
| 264 | "All messages\n") |
| 265 | { |
| 266 | int i; |
| 267 | |
| 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; |
| 272 | return CMD_SUCCESS; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE); |
| 277 | |
| 278 | return CMD_WARNING; |
| 279 | } |
| 280 | |
| 281 | /* [Babel Command] */ |
| 282 | DEFUN (no_babel_debug, |
| 283 | no_babel_debug_cmd, |
| 284 | "no debug (common|kernel|filter|timeout|interface|route|all)", |
| 285 | NO_STR |
| 286 | "Disable debug messages for specific or all part.\n" |
| 287 | "Common messages (default)\n" |
| 288 | "Kernel messages\n" |
| 289 | "Filter messages\n" |
| 290 | "Timeout messages\n" |
| 291 | "Interface messages\n" |
| 292 | "Route messages\n" |
| 293 | "All messages\n") |
| 294 | { |
| 295 | int i; |
| 296 | |
| 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; |
Matthieu Boutier | a0edef1 | 2012-01-27 22:54:11 +0100 | [diff] [blame] | 301 | return CMD_SUCCESS; |
Matthieu Boutier | f1305cb | 2012-01-20 00:19:35 +0100 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE); |
| 306 | |
| 307 | return CMD_WARNING; |
| 308 | } |
| 309 | #endif /* NO_DEBUG */ |
| 310 | |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 311 | |
| 312 | void babelz_zebra_init(void) |
| 313 | { |
| 314 | zclient = zclient_new(); |
| 315 | zclient_init(zclient, ZEBRA_ROUTE_BABEL); |
| 316 | |
| 317 | zclient->interface_add = babel_interface_add; |
| 318 | zclient->interface_delete = babel_interface_delete; |
| 319 | zclient->interface_up = babel_interface_up; |
| 320 | zclient->interface_down = babel_interface_down; |
| 321 | zclient->interface_address_add = babel_interface_address_add; |
| 322 | zclient->interface_address_delete = babel_interface_address_delete; |
| 323 | zclient->ipv4_route_add = babel_zebra_read_ipv4; |
| 324 | zclient->ipv4_route_delete = babel_zebra_read_ipv4; |
| 325 | zclient->ipv6_route_add = babel_zebra_read_ipv6; |
| 326 | zclient->ipv6_route_delete = babel_zebra_read_ipv6; |
| 327 | |
| 328 | install_node (&zebra_node, zebra_config_write); |
| 329 | install_element(BABEL_NODE, &babel_redistribute_type_cmd); |
| 330 | install_element(BABEL_NODE, &no_babel_redistribute_type_cmd); |
Matthieu Boutier | f1305cb | 2012-01-20 00:19:35 +0100 | [diff] [blame] | 331 | install_element(BABEL_NODE, &babel_debug_cmd); |
| 332 | install_element(BABEL_NODE, &no_babel_debug_cmd); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static int |
| 336 | zebra_config_write (struct vty *vty) |
| 337 | { |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 338 | if (! zclient->enable) |
| 339 | { |
| 340 | vty_out (vty, "no router zebra%s", VTY_NEWLINE); |
| 341 | return 1; |
| 342 | } |
| 343 | else if (! zclient->redist[ZEBRA_ROUTE_BABEL]) |
| 344 | { |
| 345 | vty_out (vty, "router zebra%s", VTY_NEWLINE); |
| 346 | vty_out (vty, " no redistribute babel%s", VTY_NEWLINE); |
| 347 | return 1; |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | void |
| 353 | babel_zebra_close_connexion(void) |
| 354 | { |
| 355 | zclient_stop(zclient); |
| 356 | } |