blob: 6730e94cb8fc7c580fd70e63a368c9609cd040f1 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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"
hasso0750d212003-05-24 21:41:49 +000028#include "if_rmap.h"
paul718e3742002-12-13 20:15:29 +000029
30struct hash *ifrmaphash;
31
32/* Hook functions. */
paul8cc41982005-05-06 21:25:49 +000033static void (*if_rmap_add_hook) (struct if_rmap *) = NULL;
34static void (*if_rmap_delete_hook) (struct if_rmap *) = NULL;
paul718e3742002-12-13 20:15:29 +000035
paul8cc41982005-05-06 21:25:49 +000036static struct if_rmap *
37if_rmap_new (void)
paul718e3742002-12-13 20:15:29 +000038{
39 struct if_rmap *new;
40
41 new = XCALLOC (MTYPE_IF_RMAP, sizeof (struct if_rmap));
42
43 return new;
44}
45
paul8cc41982005-05-06 21:25:49 +000046static void
paul718e3742002-12-13 20:15:29 +000047if_rmap_free (struct if_rmap *if_rmap)
48{
49 if (if_rmap->ifname)
50 free (if_rmap->ifname);
51
52 if (if_rmap->routemap[IF_RMAP_IN])
53 free (if_rmap->routemap[IF_RMAP_IN]);
54 if (if_rmap->routemap[IF_RMAP_OUT])
55 free (if_rmap->routemap[IF_RMAP_OUT]);
56
57 XFREE (MTYPE_IF_RMAP, if_rmap);
58}
59
60struct if_rmap *
paul9035efa2004-10-10 11:56:56 +000061if_rmap_lookup (const char *ifname)
paul718e3742002-12-13 20:15:29 +000062{
63 struct if_rmap key;
64 struct if_rmap *if_rmap;
65
paul9035efa2004-10-10 11:56:56 +000066 /* temporary copy */
67 key.ifname = (char *)ifname;
paul718e3742002-12-13 20:15:29 +000068
69 if_rmap = hash_lookup (ifrmaphash, &key);
70
71 return if_rmap;
72}
73
74void
75if_rmap_hook_add (void (*func) (struct if_rmap *))
76{
77 if_rmap_add_hook = func;
78}
79
80void
81if_rmap_hook_delete (void (*func) (struct if_rmap *))
82{
83 if_rmap_delete_hook = func;
84}
85
paul8cc41982005-05-06 21:25:49 +000086static void *
87if_rmap_hash_alloc (void *arg)
paul718e3742002-12-13 20:15:29 +000088{
paul8cc41982005-05-06 21:25:49 +000089 struct if_rmap *ifarg = arg;
paul718e3742002-12-13 20:15:29 +000090 struct if_rmap *if_rmap;
91
92 if_rmap = if_rmap_new ();
paul8cc41982005-05-06 21:25:49 +000093 if_rmap->ifname = strdup (ifarg->ifname);
paul718e3742002-12-13 20:15:29 +000094
95 return if_rmap;
96}
97
paul8cc41982005-05-06 21:25:49 +000098static struct if_rmap *
paul9035efa2004-10-10 11:56:56 +000099if_rmap_get (const char *ifname)
paul718e3742002-12-13 20:15:29 +0000100{
101 struct if_rmap key;
102
paul9035efa2004-10-10 11:56:56 +0000103 /* temporary copy */
104 key.ifname = (char *)ifname;
paul718e3742002-12-13 20:15:29 +0000105
106 return (struct if_rmap *) hash_get (ifrmaphash, &key, if_rmap_hash_alloc);
107}
108
paul8cc41982005-05-06 21:25:49 +0000109static unsigned int
110if_rmap_hash_make (void *data)
paul718e3742002-12-13 20:15:29 +0000111{
paul8cc41982005-05-06 21:25:49 +0000112 struct if_rmap *if_rmap = data;
hasso8c328f12004-10-05 21:01:23 +0000113 unsigned int i, key;
paul718e3742002-12-13 20:15:29 +0000114
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
paul8cc41982005-05-06 21:25:49 +0000122static int
123if_rmap_hash_cmp (void *arg1, void* arg2)
paul718e3742002-12-13 20:15:29 +0000124{
paul8cc41982005-05-06 21:25:49 +0000125 struct if_rmap *if_rmap1 = arg1;
126 struct if_rmap *if_rmap2 = arg2;
paul718e3742002-12-13 20:15:29 +0000127 if (strcmp (if_rmap1->ifname, if_rmap2->ifname) == 0)
128 return 1;
129 return 0;
130}
131
paul8cc41982005-05-06 21:25:49 +0000132static struct if_rmap *
paul9035efa2004-10-10 11:56:56 +0000133if_rmap_set (const char *ifname, enum if_rmap_type type,
134 const char *routemap_name)
paul718e3742002-12-13 20:15:29 +0000135{
136 struct if_rmap *if_rmap;
137
138 if_rmap = if_rmap_get (ifname);
139
140 if (type == IF_RMAP_IN)
141 {
142 if (if_rmap->routemap[IF_RMAP_IN])
143 free (if_rmap->routemap[IF_RMAP_IN]);
144 if_rmap->routemap[IF_RMAP_IN] = strdup (routemap_name);
145 }
146 if (type == IF_RMAP_OUT)
147 {
148 if (if_rmap->routemap[IF_RMAP_OUT])
149 free (if_rmap->routemap[IF_RMAP_OUT]);
150 if_rmap->routemap[IF_RMAP_OUT] = strdup (routemap_name);
151 }
152
153 if (if_rmap_add_hook)
154 (*if_rmap_add_hook) (if_rmap);
155
156 return if_rmap;
157}
158
paul8cc41982005-05-06 21:25:49 +0000159static int
paul9035efa2004-10-10 11:56:56 +0000160if_rmap_unset (const char *ifname, enum if_rmap_type type,
161 const char *routemap_name)
paul718e3742002-12-13 20:15:29 +0000162{
163 struct if_rmap *if_rmap;
164
165 if_rmap = if_rmap_lookup (ifname);
166 if (!if_rmap)
167 return 0;
168
169 if (type == IF_RMAP_IN)
170 {
171 if (!if_rmap->routemap[IF_RMAP_IN])
172 return 0;
173 if (strcmp (if_rmap->routemap[IF_RMAP_IN], routemap_name) != 0)
174 return 0;
175
176 free (if_rmap->routemap[IF_RMAP_IN]);
177 if_rmap->routemap[IF_RMAP_IN] = NULL;
178 }
179
180 if (type == IF_RMAP_OUT)
181 {
182 if (!if_rmap->routemap[IF_RMAP_OUT])
183 return 0;
184 if (strcmp (if_rmap->routemap[IF_RMAP_OUT], routemap_name) != 0)
185 return 0;
186
187 free (if_rmap->routemap[IF_RMAP_OUT]);
188 if_rmap->routemap[IF_RMAP_OUT] = NULL;
189 }
190
191 if (if_rmap_delete_hook)
192 (*if_rmap_delete_hook) (if_rmap);
193
194 if (if_rmap->routemap[IF_RMAP_IN] == NULL &&
195 if_rmap->routemap[IF_RMAP_OUT] == NULL)
196 {
197 hash_release (ifrmaphash, if_rmap);
198 if_rmap_free (if_rmap);
199 }
200
201 return 1;
202}
203
hasso0750d212003-05-24 21:41:49 +0000204DEFUN (if_rmap,
205 if_rmap_cmd,
paul718e3742002-12-13 20:15:29 +0000206 "route-map RMAP_NAME (in|out) IFNAME",
207 "Route map set\n"
208 "Route map name\n"
209 "Route map set for input filtering\n"
210 "Route map set for output filtering\n"
211 "Route map interface name\n")
212{
213 enum if_rmap_type type;
214 struct if_rmap *if_rmap;
215
216 if (strncmp (argv[1], "i", 1) == 0)
217 type = IF_RMAP_IN;
218 else if (strncmp (argv[1], "o", 1) == 0)
219 type = IF_RMAP_OUT;
220 else
221 {
222 vty_out (vty, "route-map direction must be [in|out]%s", VTY_NEWLINE);
223 return CMD_WARNING;
224 }
225
226 if_rmap = if_rmap_set (argv[2], type, argv[0]);
227
228 return CMD_SUCCESS;
hasso4f849472003-05-25 15:13:49 +0000229}
230
231ALIAS (if_rmap,
232 if_ipv6_rmap_cmd,
233 "route-map RMAP_NAME (in|out) IFNAME",
234 "Route map set\n"
235 "Route map name\n"
236 "Route map set for input filtering\n"
237 "Route map set for output filtering\n"
238 "Route map interface name\n")
paul718e3742002-12-13 20:15:29 +0000239
hasso0750d212003-05-24 21:41:49 +0000240DEFUN (no_if_rmap,
241 no_if_rmap_cmd,
paul718e3742002-12-13 20:15:29 +0000242 "no route-map ROUTEMAP_NAME (in|out) IFNAME",
243 NO_STR
244 "Route map unset\n"
245 "Route map name\n"
246 "Route map for input filtering\n"
247 "Route map for output filtering\n"
248 "Route map interface name\n")
249{
250 int ret;
251 enum if_rmap_type type;
252
253 if (strncmp (argv[1], "i", 1) == 0)
254 type = IF_RMAP_IN;
255 else if (strncmp (argv[1], "o", 1) == 0)
256 type = IF_RMAP_OUT;
257 else
258 {
259 vty_out (vty, "route-map direction must be [in|out]%s", VTY_NEWLINE);
260 return CMD_WARNING;
261 }
262
263 ret = if_rmap_unset (argv[2], type, argv[0]);
264 if (! ret)
265 {
266 vty_out (vty, "route-map doesn't exist%s", VTY_NEWLINE);
267 return CMD_WARNING;
268 }
269 return CMD_SUCCESS;
hasso4f849472003-05-25 15:13:49 +0000270}
271
272ALIAS (no_if_rmap,
273 no_if_ipv6_rmap_cmd,
274 "no route-map ROUTEMAP_NAME (in|out) IFNAME",
275 NO_STR
276 "Route map unset\n"
277 "Route map name\n"
278 "Route map for input filtering\n"
279 "Route map for output filtering\n"
280 "Route map interface name\n")
paul718e3742002-12-13 20:15:29 +0000281
282/* Configuration write function. */
283int
284config_write_if_rmap (struct vty *vty)
285{
hasso8c328f12004-10-05 21:01:23 +0000286 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000287 struct hash_backet *mp;
288 int write = 0;
289
290 for (i = 0; i < ifrmaphash->size; i++)
291 for (mp = ifrmaphash->index[i]; mp; mp = mp->next)
292 {
293 struct if_rmap *if_rmap;
294
295 if_rmap = mp->data;
296
297 if (if_rmap->routemap[IF_RMAP_IN])
298 {
299 vty_out (vty, " route-map %s in %s%s",
300 if_rmap->routemap[IF_RMAP_IN],
301 if_rmap->ifname,
302 VTY_NEWLINE);
303 write++;
304 }
305
306 if (if_rmap->routemap[IF_RMAP_OUT])
307 {
308 vty_out (vty, " route-map %s out %s%s",
309 if_rmap->routemap[IF_RMAP_OUT],
310 if_rmap->ifname,
311 VTY_NEWLINE);
312 write++;
313 }
314 }
315 return write;
316}
317
318void
319if_rmap_reset ()
320{
321 hash_clean (ifrmaphash, (void (*) (void *)) if_rmap_free);
322}
323
324void
hasso0750d212003-05-24 21:41:49 +0000325if_rmap_init (int node)
paul718e3742002-12-13 20:15:29 +0000326{
327 ifrmaphash = hash_create (if_rmap_hash_make, if_rmap_hash_cmp);
hasso0750d212003-05-24 21:41:49 +0000328 if (node == RIPNG_NODE) {
hasso4f849472003-05-25 15:13:49 +0000329 install_element (RIPNG_NODE, &if_ipv6_rmap_cmd);
330 install_element (RIPNG_NODE, &no_if_ipv6_rmap_cmd);
331 } else if (node == RIP_NODE) {
332 install_element (RIP_NODE, &if_rmap_cmd);
333 install_element (RIP_NODE, &no_if_rmap_cmd);
hasso0750d212003-05-24 21:41:49 +0000334 }
paul718e3742002-12-13 20:15:29 +0000335}