+ note the meta-queue in NEWS
[jleu-quagga.git] / lib / if_rmap.c
blobe6f753c246bf6d91dbbcc02656c8a8517c643a9f
1 /* route-map for interface.
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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.
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.
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.
22 #include <zebra.h>
24 #include "hash.h"
25 #include "command.h"
26 #include "memory.h"
27 #include "if.h"
28 #include "if_rmap.h"
30 struct hash *ifrmaphash;
32 /* Hook functions. */
33 static void (*if_rmap_add_hook) (struct if_rmap *) = NULL;
34 static void (*if_rmap_delete_hook) (struct if_rmap *) = NULL;
36 static struct if_rmap *
37 if_rmap_new (void)
39 struct if_rmap *new;
41 new = XCALLOC (MTYPE_IF_RMAP, sizeof (struct if_rmap));
43 return new;
46 static void
47 if_rmap_free (struct if_rmap *if_rmap)
49 if (if_rmap->ifname)
50 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->ifname);
52 if (if_rmap->routemap[IF_RMAP_IN])
53 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
54 if (if_rmap->routemap[IF_RMAP_OUT])
55 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
57 XFREE (MTYPE_IF_RMAP, if_rmap);
60 struct if_rmap *
61 if_rmap_lookup (const char *ifname)
63 struct if_rmap key;
64 struct if_rmap *if_rmap;
66 /* temporary copy */
67 key.ifname = (char *)ifname;
69 if_rmap = hash_lookup (ifrmaphash, &key);
71 return if_rmap;
74 void
75 if_rmap_hook_add (void (*func) (struct if_rmap *))
77 if_rmap_add_hook = func;
80 void
81 if_rmap_hook_delete (void (*func) (struct if_rmap *))
83 if_rmap_delete_hook = func;
86 static void *
87 if_rmap_hash_alloc (void *arg)
89 struct if_rmap *ifarg = arg;
90 struct if_rmap *if_rmap;
92 if_rmap = if_rmap_new ();
93 if_rmap->ifname = XSTRDUP (MTYPE_IF_RMAP_NAME, ifarg->ifname);
95 return if_rmap;
98 static struct if_rmap *
99 if_rmap_get (const char *ifname)
101 struct if_rmap key;
103 /* temporary copy */
104 key.ifname = (char *)ifname;
106 return (struct if_rmap *) hash_get (ifrmaphash, &key, if_rmap_hash_alloc);
109 static unsigned int
110 if_rmap_hash_make (void *data)
112 struct if_rmap *if_rmap = data;
113 unsigned int i, key;
115 key = 0;
116 for (i = 0; i < strlen (if_rmap->ifname); i++)
117 key += if_rmap->ifname[i];
119 return key;
122 static int
123 if_rmap_hash_cmp (void *arg1, void* arg2)
125 struct if_rmap *if_rmap1 = arg1;
126 struct if_rmap *if_rmap2 = arg2;
127 if (strcmp (if_rmap1->ifname, if_rmap2->ifname) == 0)
128 return 1;
129 return 0;
132 static struct if_rmap *
133 if_rmap_set (const char *ifname, enum if_rmap_type type,
134 const char *routemap_name)
136 struct if_rmap *if_rmap;
138 if_rmap = if_rmap_get (ifname);
140 if (type == IF_RMAP_IN)
142 if (if_rmap->routemap[IF_RMAP_IN])
143 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
144 if_rmap->routemap[IF_RMAP_IN]
145 = XSTRDUP (MTYPE_IF_RMAP_NAME, routemap_name);
147 if (type == IF_RMAP_OUT)
149 if (if_rmap->routemap[IF_RMAP_OUT])
150 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
151 if_rmap->routemap[IF_RMAP_OUT]
152 = XSTRDUP (MTYPE_IF_RMAP_NAME, routemap_name);
155 if (if_rmap_add_hook)
156 (*if_rmap_add_hook) (if_rmap);
158 return if_rmap;
161 static int
162 if_rmap_unset (const char *ifname, enum if_rmap_type type,
163 const char *routemap_name)
165 struct if_rmap *if_rmap;
167 if_rmap = if_rmap_lookup (ifname);
168 if (!if_rmap)
169 return 0;
171 if (type == IF_RMAP_IN)
173 if (!if_rmap->routemap[IF_RMAP_IN])
174 return 0;
175 if (strcmp (if_rmap->routemap[IF_RMAP_IN], routemap_name) != 0)
176 return 0;
178 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
179 if_rmap->routemap[IF_RMAP_IN] = NULL;
182 if (type == IF_RMAP_OUT)
184 if (!if_rmap->routemap[IF_RMAP_OUT])
185 return 0;
186 if (strcmp (if_rmap->routemap[IF_RMAP_OUT], routemap_name) != 0)
187 return 0;
189 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
190 if_rmap->routemap[IF_RMAP_OUT] = NULL;
193 if (if_rmap_delete_hook)
194 (*if_rmap_delete_hook) (if_rmap);
196 if (if_rmap->routemap[IF_RMAP_IN] == NULL &&
197 if_rmap->routemap[IF_RMAP_OUT] == NULL)
199 hash_release (ifrmaphash, if_rmap);
200 if_rmap_free (if_rmap);
203 return 1;
206 DEFUN (if_rmap,
207 if_rmap_cmd,
208 "route-map RMAP_NAME (in|out) IFNAME",
209 "Route map set\n"
210 "Route map name\n"
211 "Route map set for input filtering\n"
212 "Route map set for output filtering\n"
213 "Route map interface name\n")
215 enum if_rmap_type type;
216 struct if_rmap *if_rmap;
218 if (strncmp (argv[1], "i", 1) == 0)
219 type = IF_RMAP_IN;
220 else if (strncmp (argv[1], "o", 1) == 0)
221 type = IF_RMAP_OUT;
222 else
224 vty_out (vty, "route-map direction must be [in|out]%s", VTY_NEWLINE);
225 return CMD_WARNING;
228 if_rmap = if_rmap_set (argv[2], type, argv[0]);
230 return CMD_SUCCESS;
233 ALIAS (if_rmap,
234 if_ipv6_rmap_cmd,
235 "route-map RMAP_NAME (in|out) IFNAME",
236 "Route map set\n"
237 "Route map name\n"
238 "Route map set for input filtering\n"
239 "Route map set for output filtering\n"
240 "Route map interface name\n")
242 DEFUN (no_if_rmap,
243 no_if_rmap_cmd,
244 "no route-map ROUTEMAP_NAME (in|out) IFNAME",
245 NO_STR
246 "Route map unset\n"
247 "Route map name\n"
248 "Route map for input filtering\n"
249 "Route map for output filtering\n"
250 "Route map interface name\n")
252 int ret;
253 enum if_rmap_type type;
255 if (strncmp (argv[1], "i", 1) == 0)
256 type = IF_RMAP_IN;
257 else if (strncmp (argv[1], "o", 1) == 0)
258 type = IF_RMAP_OUT;
259 else
261 vty_out (vty, "route-map direction must be [in|out]%s", VTY_NEWLINE);
262 return CMD_WARNING;
265 ret = if_rmap_unset (argv[2], type, argv[0]);
266 if (! ret)
268 vty_out (vty, "route-map doesn't exist%s", VTY_NEWLINE);
269 return CMD_WARNING;
271 return CMD_SUCCESS;
274 ALIAS (no_if_rmap,
275 no_if_ipv6_rmap_cmd,
276 "no route-map ROUTEMAP_NAME (in|out) IFNAME",
277 NO_STR
278 "Route map unset\n"
279 "Route map name\n"
280 "Route map for input filtering\n"
281 "Route map for output filtering\n"
282 "Route map interface name\n")
284 /* Configuration write function. */
286 config_write_if_rmap (struct vty *vty)
288 unsigned int i;
289 struct hash_backet *mp;
290 int write = 0;
292 for (i = 0; i < ifrmaphash->size; i++)
293 for (mp = ifrmaphash->index[i]; mp; mp = mp->next)
295 struct if_rmap *if_rmap;
297 if_rmap = mp->data;
299 if (if_rmap->routemap[IF_RMAP_IN])
301 vty_out (vty, " route-map %s in %s%s",
302 if_rmap->routemap[IF_RMAP_IN],
303 if_rmap->ifname,
304 VTY_NEWLINE);
305 write++;
308 if (if_rmap->routemap[IF_RMAP_OUT])
310 vty_out (vty, " route-map %s out %s%s",
311 if_rmap->routemap[IF_RMAP_OUT],
312 if_rmap->ifname,
313 VTY_NEWLINE);
314 write++;
317 return write;
320 void
321 if_rmap_reset ()
323 hash_clean (ifrmaphash, (void (*) (void *)) if_rmap_free);
326 void
327 if_rmap_init (int node)
329 ifrmaphash = hash_create (if_rmap_hash_make, if_rmap_hash_cmp);
330 if (node == RIPNG_NODE) {
331 install_element (RIPNG_NODE, &if_ipv6_rmap_cmd);
332 install_element (RIPNG_NODE, &no_if_ipv6_rmap_cmd);
333 } else if (node == RIP_NODE) {
334 install_element (RIP_NODE, &if_rmap_cmd);
335 install_element (RIP_NODE, &no_if_rmap_cmd);