paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* route-map for interface. |
| 2 | * Copyright (C) 1999 Kunihiro Ishiguro |
| 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 "hash.h" |
| 25 | #include "command.h" |
| 26 | #include "memory.h" |
| 27 | #include "if.h" |
hasso | 0750d21 | 2003-05-24 21:41:49 +0000 | [diff] [blame] | 28 | #include "if_rmap.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 29 | |
| 30 | struct hash *ifrmaphash; |
| 31 | |
| 32 | /* Hook functions. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 33 | static void (*if_rmap_add_hook) (struct if_rmap *) = NULL; |
| 34 | static void (*if_rmap_delete_hook) (struct if_rmap *) = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 36 | static struct if_rmap * |
| 37 | if_rmap_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 38 | { |
| 39 | struct if_rmap *new; |
| 40 | |
| 41 | new = XCALLOC (MTYPE_IF_RMAP, sizeof (struct if_rmap)); |
| 42 | |
| 43 | return new; |
| 44 | } |
| 45 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 46 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | if_rmap_free (struct if_rmap *if_rmap) |
| 48 | { |
| 49 | if (if_rmap->ifname) |
paul | 0241684 | 2005-10-26 05:05:16 +0000 | [diff] [blame] | 50 | XFREE (MTYPE_IF_RMAP_NAME, if_rmap->ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 51 | |
| 52 | if (if_rmap->routemap[IF_RMAP_IN]) |
paul | 0241684 | 2005-10-26 05:05:16 +0000 | [diff] [blame] | 53 | XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | if (if_rmap->routemap[IF_RMAP_OUT]) |
paul | 0241684 | 2005-10-26 05:05:16 +0000 | [diff] [blame] | 55 | XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 56 | |
| 57 | XFREE (MTYPE_IF_RMAP, if_rmap); |
| 58 | } |
| 59 | |
| 60 | struct if_rmap * |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 61 | if_rmap_lookup (const char *ifname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 62 | { |
| 63 | struct if_rmap key; |
| 64 | struct if_rmap *if_rmap; |
| 65 | |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 66 | /* temporary copy */ |
| 67 | key.ifname = (char *)ifname; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | |
| 69 | if_rmap = hash_lookup (ifrmaphash, &key); |
| 70 | |
| 71 | return if_rmap; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | if_rmap_hook_add (void (*func) (struct if_rmap *)) |
| 76 | { |
| 77 | if_rmap_add_hook = func; |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | if_rmap_hook_delete (void (*func) (struct if_rmap *)) |
| 82 | { |
| 83 | if_rmap_delete_hook = func; |
| 84 | } |
| 85 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 86 | static void * |
| 87 | if_rmap_hash_alloc (void *arg) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 88 | { |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 89 | struct if_rmap *ifarg = arg; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 90 | struct if_rmap *if_rmap; |
| 91 | |
| 92 | if_rmap = if_rmap_new (); |
paul | 0241684 | 2005-10-26 05:05:16 +0000 | [diff] [blame] | 93 | if_rmap->ifname = XSTRDUP (MTYPE_IF_RMAP_NAME, ifarg->ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 94 | |
| 95 | return if_rmap; |
| 96 | } |
| 97 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 98 | static struct if_rmap * |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 99 | if_rmap_get (const char *ifname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 100 | { |
| 101 | struct if_rmap key; |
| 102 | |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 103 | /* temporary copy */ |
| 104 | key.ifname = (char *)ifname; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 105 | |
| 106 | return (struct if_rmap *) hash_get (ifrmaphash, &key, if_rmap_hash_alloc); |
| 107 | } |
| 108 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 109 | static unsigned int |
| 110 | if_rmap_hash_make (void *data) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 111 | { |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 112 | struct if_rmap *if_rmap = data; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 113 | unsigned int i, key; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 114 | |
| 115 | key = 0; |
| 116 | for (i = 0; i < strlen (if_rmap->ifname); i++) |
| 117 | key += if_rmap->ifname[i]; |
| 118 | |
| 119 | return key; |
| 120 | } |
| 121 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 122 | static int |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 123 | if_rmap_hash_cmp (const void *arg1, const void* arg2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | { |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 125 | const struct if_rmap *if_rmap1 = arg1; |
| 126 | const struct if_rmap *if_rmap2 = arg2; |
| 127 | |
| 128 | return strcmp (if_rmap1->ifname, if_rmap2->ifname) == 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 129 | } |
| 130 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 131 | static struct if_rmap * |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 132 | if_rmap_set (const char *ifname, enum if_rmap_type type, |
| 133 | const char *routemap_name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 134 | { |
| 135 | struct if_rmap *if_rmap; |
| 136 | |
| 137 | if_rmap = if_rmap_get (ifname); |
| 138 | |
| 139 | if (type == IF_RMAP_IN) |
| 140 | { |
| 141 | if (if_rmap->routemap[IF_RMAP_IN]) |
paul | 0241684 | 2005-10-26 05:05:16 +0000 | [diff] [blame] | 142 | XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]); |
| 143 | if_rmap->routemap[IF_RMAP_IN] |
| 144 | = XSTRDUP (MTYPE_IF_RMAP_NAME, routemap_name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 145 | } |
| 146 | if (type == IF_RMAP_OUT) |
| 147 | { |
| 148 | if (if_rmap->routemap[IF_RMAP_OUT]) |
paul | 0241684 | 2005-10-26 05:05:16 +0000 | [diff] [blame] | 149 | XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]); |
| 150 | if_rmap->routemap[IF_RMAP_OUT] |
| 151 | = XSTRDUP (MTYPE_IF_RMAP_NAME, routemap_name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | if (if_rmap_add_hook) |
| 155 | (*if_rmap_add_hook) (if_rmap); |
| 156 | |
| 157 | return if_rmap; |
| 158 | } |
| 159 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 160 | static int |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 161 | if_rmap_unset (const char *ifname, enum if_rmap_type type, |
| 162 | const char *routemap_name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | { |
| 164 | struct if_rmap *if_rmap; |
| 165 | |
| 166 | if_rmap = if_rmap_lookup (ifname); |
| 167 | if (!if_rmap) |
| 168 | return 0; |
| 169 | |
| 170 | if (type == IF_RMAP_IN) |
| 171 | { |
| 172 | if (!if_rmap->routemap[IF_RMAP_IN]) |
| 173 | return 0; |
| 174 | if (strcmp (if_rmap->routemap[IF_RMAP_IN], routemap_name) != 0) |
| 175 | return 0; |
| 176 | |
paul | 0241684 | 2005-10-26 05:05:16 +0000 | [diff] [blame] | 177 | XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 178 | if_rmap->routemap[IF_RMAP_IN] = NULL; |
| 179 | } |
| 180 | |
| 181 | if (type == IF_RMAP_OUT) |
| 182 | { |
| 183 | if (!if_rmap->routemap[IF_RMAP_OUT]) |
| 184 | return 0; |
| 185 | if (strcmp (if_rmap->routemap[IF_RMAP_OUT], routemap_name) != 0) |
| 186 | return 0; |
| 187 | |
paul | 0241684 | 2005-10-26 05:05:16 +0000 | [diff] [blame] | 188 | XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 189 | if_rmap->routemap[IF_RMAP_OUT] = NULL; |
| 190 | } |
| 191 | |
| 192 | if (if_rmap_delete_hook) |
| 193 | (*if_rmap_delete_hook) (if_rmap); |
| 194 | |
| 195 | if (if_rmap->routemap[IF_RMAP_IN] == NULL && |
| 196 | if_rmap->routemap[IF_RMAP_OUT] == NULL) |
| 197 | { |
| 198 | hash_release (ifrmaphash, if_rmap); |
| 199 | if_rmap_free (if_rmap); |
| 200 | } |
| 201 | |
| 202 | return 1; |
| 203 | } |
| 204 | |
hasso | 0750d21 | 2003-05-24 21:41:49 +0000 | [diff] [blame] | 205 | DEFUN (if_rmap, |
| 206 | if_rmap_cmd, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 207 | "route-map RMAP_NAME (in|out) IFNAME", |
| 208 | "Route map set\n" |
| 209 | "Route map name\n" |
| 210 | "Route map set for input filtering\n" |
| 211 | "Route map set for output filtering\n" |
| 212 | "Route map interface name\n") |
| 213 | { |
| 214 | enum if_rmap_type type; |
Denis Ovsienko | 94e7cd9 | 2011-12-07 01:35:11 +0400 | [diff] [blame] | 215 | struct if_rmap *if_rmap; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 216 | |
| 217 | if (strncmp (argv[1], "i", 1) == 0) |
| 218 | type = IF_RMAP_IN; |
| 219 | else if (strncmp (argv[1], "o", 1) == 0) |
| 220 | type = IF_RMAP_OUT; |
| 221 | else |
| 222 | { |
| 223 | vty_out (vty, "route-map direction must be [in|out]%s", VTY_NEWLINE); |
| 224 | return CMD_WARNING; |
| 225 | } |
| 226 | |
Denis Ovsienko | 94e7cd9 | 2011-12-07 01:35:11 +0400 | [diff] [blame] | 227 | if_rmap = if_rmap_set (argv[2], type, argv[0]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 228 | |
| 229 | return CMD_SUCCESS; |
hasso | 4f84947 | 2003-05-25 15:13:49 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | ALIAS (if_rmap, |
| 233 | if_ipv6_rmap_cmd, |
| 234 | "route-map RMAP_NAME (in|out) IFNAME", |
| 235 | "Route map set\n" |
| 236 | "Route map name\n" |
| 237 | "Route map set for input filtering\n" |
| 238 | "Route map set for output filtering\n" |
| 239 | "Route map interface name\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 240 | |
hasso | 0750d21 | 2003-05-24 21:41:49 +0000 | [diff] [blame] | 241 | DEFUN (no_if_rmap, |
| 242 | no_if_rmap_cmd, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 243 | "no route-map ROUTEMAP_NAME (in|out) IFNAME", |
| 244 | NO_STR |
| 245 | "Route map unset\n" |
| 246 | "Route map name\n" |
| 247 | "Route map for input filtering\n" |
| 248 | "Route map for output filtering\n" |
| 249 | "Route map interface name\n") |
| 250 | { |
| 251 | int ret; |
| 252 | enum if_rmap_type type; |
| 253 | |
| 254 | if (strncmp (argv[1], "i", 1) == 0) |
| 255 | type = IF_RMAP_IN; |
| 256 | else if (strncmp (argv[1], "o", 1) == 0) |
| 257 | type = IF_RMAP_OUT; |
| 258 | else |
| 259 | { |
| 260 | vty_out (vty, "route-map direction must be [in|out]%s", VTY_NEWLINE); |
| 261 | return CMD_WARNING; |
| 262 | } |
| 263 | |
| 264 | ret = if_rmap_unset (argv[2], type, argv[0]); |
| 265 | if (! ret) |
| 266 | { |
| 267 | vty_out (vty, "route-map doesn't exist%s", VTY_NEWLINE); |
| 268 | return CMD_WARNING; |
| 269 | } |
| 270 | return CMD_SUCCESS; |
hasso | 4f84947 | 2003-05-25 15:13:49 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | ALIAS (no_if_rmap, |
| 274 | no_if_ipv6_rmap_cmd, |
| 275 | "no route-map ROUTEMAP_NAME (in|out) IFNAME", |
| 276 | NO_STR |
| 277 | "Route map unset\n" |
| 278 | "Route map name\n" |
| 279 | "Route map for input filtering\n" |
| 280 | "Route map for output filtering\n" |
| 281 | "Route map interface name\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 282 | |
| 283 | /* Configuration write function. */ |
| 284 | int |
| 285 | config_write_if_rmap (struct vty *vty) |
| 286 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 287 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 288 | struct hash_backet *mp; |
| 289 | int write = 0; |
| 290 | |
| 291 | for (i = 0; i < ifrmaphash->size; i++) |
| 292 | for (mp = ifrmaphash->index[i]; mp; mp = mp->next) |
| 293 | { |
| 294 | struct if_rmap *if_rmap; |
| 295 | |
| 296 | if_rmap = mp->data; |
| 297 | |
| 298 | if (if_rmap->routemap[IF_RMAP_IN]) |
| 299 | { |
| 300 | vty_out (vty, " route-map %s in %s%s", |
| 301 | if_rmap->routemap[IF_RMAP_IN], |
| 302 | if_rmap->ifname, |
| 303 | VTY_NEWLINE); |
| 304 | write++; |
| 305 | } |
| 306 | |
| 307 | if (if_rmap->routemap[IF_RMAP_OUT]) |
| 308 | { |
| 309 | vty_out (vty, " route-map %s out %s%s", |
| 310 | if_rmap->routemap[IF_RMAP_OUT], |
| 311 | if_rmap->ifname, |
| 312 | VTY_NEWLINE); |
| 313 | write++; |
| 314 | } |
| 315 | } |
| 316 | return write; |
| 317 | } |
| 318 | |
| 319 | void |
| 320 | if_rmap_reset () |
| 321 | { |
| 322 | hash_clean (ifrmaphash, (void (*) (void *)) if_rmap_free); |
| 323 | } |
| 324 | |
| 325 | void |
hasso | 0750d21 | 2003-05-24 21:41:49 +0000 | [diff] [blame] | 326 | if_rmap_init (int node) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 327 | { |
| 328 | ifrmaphash = hash_create (if_rmap_hash_make, if_rmap_hash_cmp); |
hasso | 0750d21 | 2003-05-24 21:41:49 +0000 | [diff] [blame] | 329 | if (node == RIPNG_NODE) { |
hasso | 4f84947 | 2003-05-25 15:13:49 +0000 | [diff] [blame] | 330 | install_element (RIPNG_NODE, &if_ipv6_rmap_cmd); |
| 331 | install_element (RIPNG_NODE, &no_if_ipv6_rmap_cmd); |
| 332 | } else if (node == RIP_NODE) { |
| 333 | install_element (RIP_NODE, &if_rmap_cmd); |
| 334 | install_element (RIP_NODE, &no_if_rmap_cmd); |
hasso | 0750d21 | 2003-05-24 21:41:49 +0000 | [diff] [blame] | 335 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | } |