Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 1 | /* zebra routemap. |
| 2 | * Copyright (C) 2006 IBM Corporation |
| 3 | * |
| 4 | * This file is part of GNU Zebra. |
| 5 | * |
| 6 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2, or (at your option) any |
| 9 | * later version. |
| 10 | * |
| 11 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | * 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include "memory.h" |
| 25 | #include "prefix.h" |
| 26 | #include "rib.h" |
| 27 | #include "routemap.h" |
| 28 | #include "command.h" |
| 29 | #include "filter.h" |
| 30 | #include "plist.h" |
| 31 | |
| 32 | #include "zebra/zserv.h" |
| 33 | |
| 34 | /* Add zebra route map rule */ |
| 35 | static int |
| 36 | zebra_route_match_add(struct vty *vty, struct route_map_index *index, |
| 37 | const char *command, const char *arg) |
| 38 | { |
| 39 | int ret; |
| 40 | |
| 41 | ret = route_map_add_match (index, command, arg); |
| 42 | if (ret) |
| 43 | { |
| 44 | switch (ret) |
| 45 | { |
| 46 | case RMAP_RULE_MISSING: |
| 47 | vty_out (vty, "%% Can't find rule.%s", VTY_NEWLINE); |
| 48 | return CMD_WARNING; |
| 49 | case RMAP_COMPILE_ERROR: |
| 50 | vty_out (vty, "%% Argument is malformed.%s", VTY_NEWLINE); |
| 51 | return CMD_WARNING; |
| 52 | } |
| 53 | } |
| 54 | return CMD_SUCCESS; |
| 55 | } |
| 56 | |
| 57 | /* Delete zebra route map rule. */ |
| 58 | static int |
| 59 | zebra_route_match_delete (struct vty *vty, struct route_map_index *index, |
| 60 | const char *command, const char *arg) |
| 61 | { |
| 62 | int ret; |
| 63 | |
| 64 | ret = route_map_delete_match (index, command, arg); |
| 65 | if (ret) |
| 66 | { |
| 67 | switch (ret) |
| 68 | { |
| 69 | case RMAP_RULE_MISSING: |
| 70 | vty_out (vty, "%% Can't find rule.%s", VTY_NEWLINE); |
| 71 | return CMD_WARNING; |
| 72 | case RMAP_COMPILE_ERROR: |
| 73 | vty_out (vty, "%% Argument is malformed.%s", VTY_NEWLINE); |
| 74 | return CMD_WARNING; |
| 75 | } |
| 76 | } |
| 77 | return CMD_SUCCESS; |
| 78 | } |
| 79 | |
| 80 | /* Add zebra route map rule. */ |
| 81 | static int |
| 82 | zebra_route_set_add (struct vty *vty, struct route_map_index *index, |
| 83 | const char *command, const char *arg) |
| 84 | { |
| 85 | int ret; |
| 86 | |
| 87 | ret = route_map_add_set (index, command, arg); |
| 88 | if (ret) |
| 89 | { |
| 90 | switch (ret) |
| 91 | { |
| 92 | case RMAP_RULE_MISSING: |
| 93 | vty_out (vty, "%% Can't find rule.%s", VTY_NEWLINE); |
| 94 | return CMD_WARNING; |
| 95 | case RMAP_COMPILE_ERROR: |
| 96 | vty_out (vty, "%% Argument is malformed.%s", VTY_NEWLINE); |
| 97 | return CMD_WARNING; |
| 98 | } |
| 99 | } |
| 100 | return CMD_SUCCESS; |
| 101 | } |
| 102 | |
| 103 | /* Delete zebra route map rule. */ |
| 104 | static int |
| 105 | zebra_route_set_delete (struct vty *vty, struct route_map_index *index, |
| 106 | const char *command, const char *arg) |
| 107 | { |
| 108 | int ret; |
| 109 | |
| 110 | ret = route_map_delete_set (index, command, arg); |
| 111 | if (ret) |
| 112 | { |
| 113 | switch (ret) |
| 114 | { |
| 115 | case RMAP_RULE_MISSING: |
| 116 | vty_out (vty, "%% Can't find rule.%s", VTY_NEWLINE); |
| 117 | return CMD_WARNING; |
| 118 | case RMAP_COMPILE_ERROR: |
| 119 | vty_out (vty, "%% Argument is malformed.%s", VTY_NEWLINE); |
| 120 | return CMD_WARNING; |
| 121 | } |
| 122 | } |
| 123 | return CMD_SUCCESS; |
| 124 | } |
| 125 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 126 | |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 127 | /* `match interface IFNAME' */ |
| 128 | /* Match function return 1 if match is success else return zero. */ |
| 129 | static route_map_result_t |
| 130 | route_match_interface (void *rule, struct prefix *prefix, |
| 131 | route_map_object_t type, void *object) |
| 132 | { |
Feng Lu | 1885d0a | 2015-05-22 11:40:04 +0200 | [diff] [blame^] | 133 | struct nexthop_vrfid *nh_vrf; |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 134 | struct nexthop *nexthop; |
| 135 | char *ifname = rule; |
| 136 | unsigned int ifindex; |
| 137 | |
| 138 | if (type == RMAP_ZEBRA) |
| 139 | { |
| 140 | if (strcasecmp(ifname, "any") == 0) |
| 141 | return RMAP_MATCH; |
Feng Lu | 1885d0a | 2015-05-22 11:40:04 +0200 | [diff] [blame^] | 142 | nh_vrf = object; |
| 143 | if (!nh_vrf) |
| 144 | return RMAP_NOMATCH; |
| 145 | ifindex = ifname2ifindex_vrf (ifname, nh_vrf->vrf_id); |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 146 | if (ifindex == 0) |
| 147 | return RMAP_NOMATCH; |
Feng Lu | 1885d0a | 2015-05-22 11:40:04 +0200 | [diff] [blame^] | 148 | nexthop = nh_vrf->nexthop; |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 149 | if (!nexthop) |
| 150 | return RMAP_NOMATCH; |
| 151 | if (nexthop->ifindex == ifindex) |
| 152 | return RMAP_MATCH; |
| 153 | } |
| 154 | return RMAP_NOMATCH; |
| 155 | } |
| 156 | |
| 157 | /* Route map `match interface' match statement. `arg' is IFNAME value */ |
| 158 | static void * |
| 159 | route_match_interface_compile (const char *arg) |
| 160 | { |
| 161 | return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg); |
| 162 | } |
| 163 | |
| 164 | /* Free route map's compiled `match interface' value. */ |
| 165 | static void |
| 166 | route_match_interface_free (void *rule) |
| 167 | { |
| 168 | XFREE (MTYPE_ROUTE_MAP_COMPILED, rule); |
| 169 | } |
| 170 | |
| 171 | /* Route map commands for interface matching */ |
| 172 | struct route_map_rule_cmd route_match_interface_cmd = |
| 173 | { |
| 174 | "interface", |
| 175 | route_match_interface, |
| 176 | route_match_interface_compile, |
| 177 | route_match_interface_free |
| 178 | }; |
| 179 | |
| 180 | DEFUN (match_interface, |
| 181 | match_interface_cmd, |
| 182 | "match interface WORD", |
| 183 | MATCH_STR |
| 184 | "match first hop interface of route\n" |
| 185 | "Interface name\n") |
| 186 | { |
| 187 | return zebra_route_match_add (vty, vty->index, "interface", argv[0]); |
| 188 | } |
| 189 | |
| 190 | DEFUN (no_match_interface, |
| 191 | no_match_interface_cmd, |
| 192 | "no match interface", |
| 193 | NO_STR |
| 194 | MATCH_STR |
| 195 | "Match first hop interface of route\n") |
| 196 | { |
| 197 | if (argc == 0) |
| 198 | return zebra_route_match_delete (vty, vty->index, "interface", NULL); |
| 199 | |
| 200 | return zebra_route_match_delete (vty, vty->index, "interface", argv[0]); |
| 201 | } |
| 202 | |
| 203 | ALIAS (no_match_interface, |
| 204 | no_match_interface_val_cmd, |
| 205 | "no match interface WORD", |
| 206 | NO_STR |
| 207 | MATCH_STR |
| 208 | "Match first hop interface of route\n" |
| 209 | "Interface name\n") |
| 210 | |
| 211 | DEFUN (match_ip_next_hop, |
| 212 | match_ip_next_hop_cmd, |
| 213 | "match ip next-hop (<1-199>|<1300-2699>|WORD)", |
| 214 | MATCH_STR |
| 215 | IP_STR |
| 216 | "Match next-hop address of route\n" |
| 217 | "IP access-list number\n" |
| 218 | "IP access-list number (expanded range)\n" |
| 219 | "IP Access-list name\n") |
| 220 | { |
| 221 | return zebra_route_match_add (vty, vty->index, "ip next-hop", argv[0]); |
| 222 | } |
| 223 | |
| 224 | DEFUN (no_match_ip_next_hop, |
| 225 | no_match_ip_next_hop_cmd, |
| 226 | "no match ip next-hop", |
| 227 | NO_STR |
| 228 | MATCH_STR |
| 229 | IP_STR |
| 230 | "Match next-hop address of route\n") |
| 231 | { |
| 232 | if (argc == 0) |
| 233 | return zebra_route_match_delete (vty, vty->index, "ip next-hop", NULL); |
| 234 | |
| 235 | return zebra_route_match_delete (vty, vty->index, "ip next-hop", argv[0]); |
| 236 | } |
| 237 | |
| 238 | ALIAS (no_match_ip_next_hop, |
| 239 | no_match_ip_next_hop_val_cmd, |
| 240 | "no match ip next-hop (<1-199>|<1300-2699>|WORD)", |
| 241 | NO_STR |
| 242 | MATCH_STR |
| 243 | IP_STR |
| 244 | "Match next-hop address of route\n" |
| 245 | "IP access-list number\n" |
| 246 | "IP access-list number (expanded range)\n" |
| 247 | "IP Access-list name\n") |
| 248 | |
| 249 | DEFUN (match_ip_next_hop_prefix_list, |
| 250 | match_ip_next_hop_prefix_list_cmd, |
| 251 | "match ip next-hop prefix-list WORD", |
| 252 | MATCH_STR |
| 253 | IP_STR |
| 254 | "Match next-hop address of route\n" |
| 255 | "Match entries of prefix-lists\n" |
| 256 | "IP prefix-list name\n") |
| 257 | { |
| 258 | return zebra_route_match_add (vty, vty->index, "ip next-hop prefix-list", argv[0]); |
| 259 | } |
| 260 | |
| 261 | DEFUN (no_match_ip_next_hop_prefix_list, |
| 262 | no_match_ip_next_hop_prefix_list_cmd, |
| 263 | "no match ip next-hop prefix-list", |
| 264 | NO_STR |
| 265 | MATCH_STR |
| 266 | IP_STR |
| 267 | "Match next-hop address of route\n" |
| 268 | "Match entries of prefix-lists\n") |
| 269 | { |
| 270 | if (argc == 0) |
| 271 | return zebra_route_match_delete (vty, vty->index, "ip next-hop prefix-list", NULL); |
| 272 | |
| 273 | return zebra_route_match_delete (vty, vty->index, "ip next-hop prefix-list", argv[0]); |
| 274 | } |
| 275 | |
| 276 | ALIAS (no_match_ip_next_hop_prefix_list, |
| 277 | no_match_ip_next_hop_prefix_list_val_cmd, |
| 278 | "no match ip next-hop prefix-list WORD", |
| 279 | NO_STR |
| 280 | MATCH_STR |
| 281 | IP_STR |
| 282 | "Match next-hop address of route\n" |
| 283 | "Match entries of prefix-lists\n" |
| 284 | "IP prefix-list name\n") |
| 285 | |
| 286 | DEFUN (match_ip_address, |
| 287 | match_ip_address_cmd, |
| 288 | "match ip address (<1-199>|<1300-2699>|WORD)", |
| 289 | MATCH_STR |
| 290 | IP_STR |
| 291 | "Match address of route\n" |
| 292 | "IP access-list number\n" |
| 293 | "IP access-list number (expanded range)\n" |
| 294 | "IP Access-list name\n") |
| 295 | |
| 296 | { |
| 297 | return zebra_route_match_add (vty, vty->index, "ip address", argv[0]); |
| 298 | } |
| 299 | |
| 300 | DEFUN (no_match_ip_address, |
| 301 | no_match_ip_address_cmd, |
| 302 | "no match ip address", |
| 303 | NO_STR |
| 304 | MATCH_STR |
| 305 | IP_STR |
| 306 | "Match address of route\n") |
| 307 | { |
| 308 | if (argc == 0) |
| 309 | return zebra_route_match_delete (vty, vty->index, "ip address", NULL); |
| 310 | |
| 311 | return zebra_route_match_delete (vty, vty->index, "ip address", argv[0]); |
| 312 | } |
| 313 | |
| 314 | ALIAS (no_match_ip_address, |
| 315 | no_match_ip_address_val_cmd, |
| 316 | "no match ip address (<1-199>|<1300-2699>|WORD)", |
| 317 | NO_STR |
| 318 | MATCH_STR |
| 319 | IP_STR |
| 320 | "Match address of route\n" |
| 321 | "IP access-list number\n" |
| 322 | "IP access-list number (expanded range)\n" |
| 323 | "IP Access-list name\n") |
| 324 | |
| 325 | DEFUN (match_ip_address_prefix_list, |
| 326 | match_ip_address_prefix_list_cmd, |
| 327 | "match ip address prefix-list WORD", |
| 328 | MATCH_STR |
| 329 | IP_STR |
| 330 | "Match address of route\n" |
| 331 | "Match entries of prefix-lists\n" |
| 332 | "IP prefix-list name\n") |
| 333 | { |
| 334 | return zebra_route_match_add (vty, vty->index, "ip address prefix-list", argv[0]); |
| 335 | } |
| 336 | |
| 337 | DEFUN (no_match_ip_address_prefix_list, |
| 338 | no_match_ip_address_prefix_list_cmd, |
| 339 | "no match ip address prefix-list", |
| 340 | NO_STR |
| 341 | MATCH_STR |
| 342 | IP_STR |
| 343 | "Match address of route\n" |
| 344 | "Match entries of prefix-lists\n") |
| 345 | { |
| 346 | if (argc == 0) |
| 347 | return zebra_route_match_delete (vty, vty->index, "ip address prefix-list", NULL); |
| 348 | |
| 349 | return zebra_route_match_delete (vty, vty->index, "ip address prefix-list", argv[0]); |
| 350 | } |
| 351 | |
| 352 | ALIAS (no_match_ip_address_prefix_list, |
| 353 | no_match_ip_address_prefix_list_val_cmd, |
| 354 | "no match ip address prefix-list WORD", |
| 355 | NO_STR |
| 356 | MATCH_STR |
| 357 | IP_STR |
| 358 | "Match address of route\n" |
| 359 | "Match entries of prefix-lists\n" |
| 360 | "IP prefix-list name\n") |
| 361 | |
| 362 | /* set functions */ |
| 363 | |
| 364 | DEFUN (set_src, |
| 365 | set_src_cmd, |
| 366 | "set src A.B.C.D", |
| 367 | SET_STR |
| 368 | "src address for route\n" |
| 369 | "src address\n") |
| 370 | { |
| 371 | struct in_addr src; |
| 372 | struct interface *pif; |
| 373 | |
| 374 | if (inet_pton(AF_INET, argv[0], &src) <= 0) |
| 375 | { |
| 376 | vty_out (vty, "%% not a local address%s", VTY_NEWLINE); |
| 377 | return CMD_WARNING; |
| 378 | } |
| 379 | |
| 380 | pif = if_lookup_exact_address (src); |
| 381 | if (!pif) |
| 382 | { |
| 383 | vty_out (vty, "%% not a local address%s", VTY_NEWLINE); |
| 384 | return CMD_WARNING; |
| 385 | } |
| 386 | return zebra_route_set_add (vty, vty->index, "src", argv[0]); |
| 387 | } |
| 388 | |
| 389 | DEFUN (no_set_src, |
| 390 | no_set_src_cmd, |
| 391 | "no set src", |
| 392 | NO_STR |
| 393 | SET_STR |
| 394 | "Source address for route\n") |
| 395 | { |
| 396 | if (argc == 0) |
| 397 | return zebra_route_set_delete (vty, vty->index, "src", NULL); |
| 398 | |
| 399 | return zebra_route_set_delete (vty, vty->index, "src", argv[0]); |
| 400 | } |
| 401 | |
| 402 | ALIAS (no_set_src, |
| 403 | no_set_src_val_cmd, |
| 404 | "no set src (A.B.C.D)", |
| 405 | NO_STR |
| 406 | SET_STR |
| 407 | "src address for route\n" |
| 408 | "src address\n") |
| 409 | |
| 410 | /*XXXXXXXXXXXXXXXXXXXXXXXXXXXX*/ |
| 411 | |
| 412 | /* `match ip next-hop IP_ACCESS_LIST' */ |
| 413 | |
| 414 | /* Match function return 1 if match is success else return zero. */ |
| 415 | static route_map_result_t |
| 416 | route_match_ip_next_hop (void *rule, struct prefix *prefix, |
| 417 | route_map_object_t type, void *object) |
| 418 | { |
| 419 | struct access_list *alist; |
| 420 | struct nexthop *nexthop; |
| 421 | struct prefix_ipv4 p; |
| 422 | |
| 423 | if (type == RMAP_ZEBRA) |
| 424 | { |
| 425 | nexthop = object; |
| 426 | switch (nexthop->type) { |
| 427 | case NEXTHOP_TYPE_IFINDEX: |
| 428 | case NEXTHOP_TYPE_IFNAME: |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 429 | /* Interface routes can't match ip next-hop */ |
| 430 | return RMAP_NOMATCH; |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 431 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
| 432 | case NEXTHOP_TYPE_IPV4_IFNAME: |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 433 | case NEXTHOP_TYPE_IPV4: |
| 434 | p.family = AF_INET; |
| 435 | p.prefix = nexthop->gate.ipv4; |
| 436 | p.prefixlen = IPV4_MAX_BITLEN; |
| 437 | break; |
| 438 | default: |
| 439 | return RMAP_NOMATCH; |
| 440 | } |
| 441 | alist = access_list_lookup (AFI_IP, (char *) rule); |
| 442 | if (alist == NULL) |
| 443 | return RMAP_NOMATCH; |
| 444 | |
| 445 | return (access_list_apply (alist, &p) == FILTER_DENY ? |
| 446 | RMAP_NOMATCH : RMAP_MATCH); |
| 447 | } |
| 448 | return RMAP_NOMATCH; |
| 449 | } |
| 450 | |
| 451 | /* Route map `ip next-hop' match statement. `arg' should be |
| 452 | access-list name. */ |
| 453 | static void * |
| 454 | route_match_ip_next_hop_compile (const char *arg) |
| 455 | { |
| 456 | return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg); |
| 457 | } |
| 458 | |
| 459 | /* Free route map's compiled `. */ |
| 460 | static void |
| 461 | route_match_ip_next_hop_free (void *rule) |
| 462 | { |
| 463 | XFREE (MTYPE_ROUTE_MAP_COMPILED, rule); |
| 464 | } |
| 465 | |
| 466 | /* Route map commands for ip next-hop matching. */ |
| 467 | static struct route_map_rule_cmd route_match_ip_next_hop_cmd = |
| 468 | { |
| 469 | "ip next-hop", |
| 470 | route_match_ip_next_hop, |
| 471 | route_match_ip_next_hop_compile, |
| 472 | route_match_ip_next_hop_free |
| 473 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 474 | |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 475 | /* `match ip next-hop prefix-list PREFIX_LIST' */ |
| 476 | |
| 477 | static route_map_result_t |
| 478 | route_match_ip_next_hop_prefix_list (void *rule, struct prefix *prefix, |
| 479 | route_map_object_t type, void *object) |
| 480 | { |
| 481 | struct prefix_list *plist; |
| 482 | struct nexthop *nexthop; |
| 483 | struct prefix_ipv4 p; |
| 484 | |
| 485 | if (type == RMAP_ZEBRA) |
| 486 | { |
| 487 | nexthop = object; |
| 488 | switch (nexthop->type) { |
| 489 | case NEXTHOP_TYPE_IFINDEX: |
| 490 | case NEXTHOP_TYPE_IFNAME: |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 491 | /* Interface routes can't match ip next-hop */ |
| 492 | return RMAP_NOMATCH; |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 493 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
| 494 | case NEXTHOP_TYPE_IPV4_IFNAME: |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 495 | case NEXTHOP_TYPE_IPV4: |
| 496 | p.family = AF_INET; |
| 497 | p.prefix = nexthop->gate.ipv4; |
| 498 | p.prefixlen = IPV4_MAX_BITLEN; |
| 499 | break; |
| 500 | default: |
| 501 | return RMAP_NOMATCH; |
| 502 | } |
| 503 | plist = prefix_list_lookup (AFI_IP, (char *) rule); |
| 504 | if (plist == NULL) |
| 505 | return RMAP_NOMATCH; |
| 506 | |
| 507 | return (prefix_list_apply (plist, &p) == PREFIX_DENY ? |
| 508 | RMAP_NOMATCH : RMAP_MATCH); |
| 509 | } |
| 510 | return RMAP_NOMATCH; |
| 511 | } |
| 512 | |
| 513 | static void * |
| 514 | route_match_ip_next_hop_prefix_list_compile (const char *arg) |
| 515 | { |
| 516 | return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg); |
| 517 | } |
| 518 | |
| 519 | static void |
| 520 | route_match_ip_next_hop_prefix_list_free (void *rule) |
| 521 | { |
| 522 | XFREE (MTYPE_ROUTE_MAP_COMPILED, rule); |
| 523 | } |
| 524 | |
| 525 | static struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd = |
| 526 | { |
| 527 | "ip next-hop prefix-list", |
| 528 | route_match_ip_next_hop_prefix_list, |
| 529 | route_match_ip_next_hop_prefix_list_compile, |
| 530 | route_match_ip_next_hop_prefix_list_free |
| 531 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 532 | |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 533 | /* `match ip address IP_ACCESS_LIST' */ |
| 534 | |
| 535 | /* Match function should return 1 if match is success else return |
| 536 | zero. */ |
| 537 | static route_map_result_t |
| 538 | route_match_ip_address (void *rule, struct prefix *prefix, |
| 539 | route_map_object_t type, void *object) |
| 540 | { |
| 541 | struct access_list *alist; |
| 542 | |
| 543 | if (type == RMAP_ZEBRA) |
| 544 | { |
| 545 | alist = access_list_lookup (AFI_IP, (char *) rule); |
| 546 | if (alist == NULL) |
| 547 | return RMAP_NOMATCH; |
| 548 | |
| 549 | return (access_list_apply (alist, prefix) == FILTER_DENY ? |
| 550 | RMAP_NOMATCH : RMAP_MATCH); |
| 551 | } |
| 552 | return RMAP_NOMATCH; |
| 553 | } |
| 554 | |
| 555 | /* Route map `ip address' match statement. `arg' should be |
| 556 | access-list name. */ |
| 557 | static void * |
| 558 | route_match_ip_address_compile (const char *arg) |
| 559 | { |
| 560 | return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg); |
| 561 | } |
| 562 | |
| 563 | /* Free route map's compiled `ip address' value. */ |
| 564 | static void |
| 565 | route_match_ip_address_free (void *rule) |
| 566 | { |
| 567 | XFREE (MTYPE_ROUTE_MAP_COMPILED, rule); |
| 568 | } |
| 569 | |
| 570 | /* Route map commands for ip address matching. */ |
| 571 | static struct route_map_rule_cmd route_match_ip_address_cmd = |
| 572 | { |
| 573 | "ip address", |
| 574 | route_match_ip_address, |
| 575 | route_match_ip_address_compile, |
| 576 | route_match_ip_address_free |
| 577 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 578 | |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 579 | /* `match ip address prefix-list PREFIX_LIST' */ |
| 580 | |
| 581 | static route_map_result_t |
| 582 | route_match_ip_address_prefix_list (void *rule, struct prefix *prefix, |
| 583 | route_map_object_t type, void *object) |
| 584 | { |
| 585 | struct prefix_list *plist; |
| 586 | |
| 587 | if (type == RMAP_ZEBRA) |
| 588 | { |
| 589 | plist = prefix_list_lookup (AFI_IP, (char *) rule); |
| 590 | if (plist == NULL) |
| 591 | return RMAP_NOMATCH; |
| 592 | |
| 593 | return (prefix_list_apply (plist, prefix) == PREFIX_DENY ? |
| 594 | RMAP_NOMATCH : RMAP_MATCH); |
| 595 | } |
| 596 | return RMAP_NOMATCH; |
| 597 | } |
| 598 | |
| 599 | static void * |
| 600 | route_match_ip_address_prefix_list_compile (const char *arg) |
| 601 | { |
| 602 | return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg); |
| 603 | } |
| 604 | |
| 605 | static void |
| 606 | route_match_ip_address_prefix_list_free (void *rule) |
| 607 | { |
| 608 | XFREE (MTYPE_ROUTE_MAP_COMPILED, rule); |
| 609 | } |
| 610 | |
| 611 | static struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd = |
| 612 | { |
| 613 | "ip address prefix-list", |
| 614 | route_match_ip_address_prefix_list, |
| 615 | route_match_ip_address_prefix_list_compile, |
| 616 | route_match_ip_address_prefix_list_free |
| 617 | }; |
| 618 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 619 | |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 620 | /* `set src A.B.C.D' */ |
| 621 | |
| 622 | /* Set src. */ |
| 623 | static route_map_result_t |
| 624 | route_set_src (void *rule, struct prefix *prefix, |
| 625 | route_map_object_t type, void *object) |
| 626 | { |
| 627 | if (type == RMAP_ZEBRA) |
| 628 | { |
| 629 | struct nexthop *nexthop; |
| 630 | |
| 631 | nexthop = object; |
| 632 | nexthop->src = *(union g_addr *)rule; |
| 633 | } |
| 634 | return RMAP_OKAY; |
| 635 | } |
| 636 | |
| 637 | /* set src compilation. */ |
| 638 | static void * |
| 639 | route_set_src_compile (const char *arg) |
| 640 | { |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 641 | union g_addr src, *psrc; |
| 642 | |
Paul Jakma | 8dd1a8d | 2011-04-11 16:33:20 +0100 | [diff] [blame] | 643 | if (inet_pton(AF_INET, arg, &src.ipv4) != 1 |
Andrew J. Schorr | 0930331 | 2007-05-30 20:10:34 +0000 | [diff] [blame] | 644 | #ifdef HAVE_IPV6 |
Paul Jakma | 8dd1a8d | 2011-04-11 16:33:20 +0100 | [diff] [blame] | 645 | && inet_pton(AF_INET6, arg, &src.ipv6) != 1 |
Andrew J. Schorr | 0930331 | 2007-05-30 20:10:34 +0000 | [diff] [blame] | 646 | #endif /* HAVE_IPV6 */ |
Paul Jakma | 8dd1a8d | 2011-04-11 16:33:20 +0100 | [diff] [blame] | 647 | ) |
| 648 | return NULL; |
Paul Jakma | 5921ef9 | 2007-05-04 20:13:20 +0000 | [diff] [blame] | 649 | |
| 650 | psrc = XMALLOC (MTYPE_ROUTE_MAP_COMPILED, sizeof (union g_addr)); |
| 651 | *psrc = src; |
| 652 | |
| 653 | return psrc; |
| 654 | } |
| 655 | |
| 656 | /* Free route map's compiled `set src' value. */ |
| 657 | static void |
| 658 | route_set_src_free (void *rule) |
| 659 | { |
| 660 | XFREE (MTYPE_ROUTE_MAP_COMPILED, rule); |
| 661 | } |
| 662 | |
| 663 | /* Set src rule structure. */ |
| 664 | static struct route_map_rule_cmd route_set_src_cmd = |
| 665 | { |
| 666 | "src", |
| 667 | route_set_src, |
| 668 | route_set_src_compile, |
| 669 | route_set_src_free, |
| 670 | }; |
| 671 | |
| 672 | void |
| 673 | zebra_route_map_init () |
| 674 | { |
| 675 | route_map_init (); |
| 676 | route_map_init_vty (); |
| 677 | |
| 678 | route_map_install_match (&route_match_interface_cmd); |
| 679 | route_map_install_match (&route_match_ip_next_hop_cmd); |
| 680 | route_map_install_match (&route_match_ip_next_hop_prefix_list_cmd); |
| 681 | route_map_install_match (&route_match_ip_address_cmd); |
| 682 | route_map_install_match (&route_match_ip_address_prefix_list_cmd); |
| 683 | /* */ |
| 684 | route_map_install_set (&route_set_src_cmd); |
| 685 | /* */ |
| 686 | install_element (RMAP_NODE, &match_interface_cmd); |
| 687 | install_element (RMAP_NODE, &no_match_interface_cmd); |
| 688 | install_element (RMAP_NODE, &no_match_interface_val_cmd); |
| 689 | install_element (RMAP_NODE, &match_ip_next_hop_cmd); |
| 690 | install_element (RMAP_NODE, &no_match_ip_next_hop_cmd); |
| 691 | install_element (RMAP_NODE, &no_match_ip_next_hop_val_cmd); |
| 692 | install_element (RMAP_NODE, &match_ip_next_hop_prefix_list_cmd); |
| 693 | install_element (RMAP_NODE, &no_match_ip_next_hop_prefix_list_cmd); |
| 694 | install_element (RMAP_NODE, &no_match_ip_next_hop_prefix_list_val_cmd); |
| 695 | install_element (RMAP_NODE, &match_ip_address_cmd); |
| 696 | install_element (RMAP_NODE, &no_match_ip_address_cmd); |
| 697 | install_element (RMAP_NODE, &no_match_ip_address_val_cmd); |
| 698 | install_element (RMAP_NODE, &match_ip_address_prefix_list_cmd); |
| 699 | install_element (RMAP_NODE, &no_match_ip_address_prefix_list_cmd); |
| 700 | install_element (RMAP_NODE, &no_match_ip_address_prefix_list_val_cmd); |
| 701 | /* */ |
| 702 | install_element (RMAP_NODE, &set_src_cmd); |
| 703 | install_element (RMAP_NODE, &no_set_src_cmd); |
| 704 | } |