blob: 0f3fa9c703d45aa4adc72118a09a460cfef5fb26 [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. */
33void (*if_rmap_add_hook) (struct if_rmap *) = NULL;
34void (*if_rmap_delete_hook) (struct if_rmap *) = NULL;
35
36struct if_rmap *
37if_rmap_new ()
38{
39 struct if_rmap *new;
40
41 new = XCALLOC (MTYPE_IF_RMAP, sizeof (struct if_rmap));
42
43 return new;
44}
45
46void
47if_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 *
61if_rmap_lookup (char *ifname)
62{
63 struct if_rmap key;
64 struct if_rmap *if_rmap;
65
66 key.ifname = ifname;
67
68 if_rmap = hash_lookup (ifrmaphash, &key);
69
70 return if_rmap;
71}
72
73void
74if_rmap_hook_add (void (*func) (struct if_rmap *))
75{
76 if_rmap_add_hook = func;
77}
78
79void
80if_rmap_hook_delete (void (*func) (struct if_rmap *))
81{
82 if_rmap_delete_hook = func;
83}
84
85void *
86if_rmap_hash_alloc (struct if_rmap *arg)
87{
88 struct if_rmap *if_rmap;
89
90 if_rmap = if_rmap_new ();
91 if_rmap->ifname = strdup (arg->ifname);
92
93 return if_rmap;
94}
95
96struct if_rmap *
97if_rmap_get (char *ifname)
98{
99 struct if_rmap key;
100
101 key.ifname = ifname;
102
103 return (struct if_rmap *) hash_get (ifrmaphash, &key, if_rmap_hash_alloc);
104}
105
106unsigned int
107if_rmap_hash_make (struct if_rmap *if_rmap)
108{
hasso8c328f12004-10-05 21:01:23 +0000109 unsigned int i, key;
paul718e3742002-12-13 20:15:29 +0000110
111 key = 0;
112 for (i = 0; i < strlen (if_rmap->ifname); i++)
113 key += if_rmap->ifname[i];
114
115 return key;
116}
117
118int
119if_rmap_hash_cmp (struct if_rmap *if_rmap1, struct if_rmap *if_rmap2)
120{
121 if (strcmp (if_rmap1->ifname, if_rmap2->ifname) == 0)
122 return 1;
123 return 0;
124}
125
126struct if_rmap *
127if_rmap_set (char *ifname, enum if_rmap_type type, char *routemap_name)
128{
129 struct if_rmap *if_rmap;
130
131 if_rmap = if_rmap_get (ifname);
132
133 if (type == IF_RMAP_IN)
134 {
135 if (if_rmap->routemap[IF_RMAP_IN])
136 free (if_rmap->routemap[IF_RMAP_IN]);
137 if_rmap->routemap[IF_RMAP_IN] = strdup (routemap_name);
138 }
139 if (type == IF_RMAP_OUT)
140 {
141 if (if_rmap->routemap[IF_RMAP_OUT])
142 free (if_rmap->routemap[IF_RMAP_OUT]);
143 if_rmap->routemap[IF_RMAP_OUT] = strdup (routemap_name);
144 }
145
146 if (if_rmap_add_hook)
147 (*if_rmap_add_hook) (if_rmap);
148
149 return if_rmap;
150}
151
152int
153if_rmap_unset (char *ifname, enum if_rmap_type type, char *routemap_name)
154{
155 struct if_rmap *if_rmap;
156
157 if_rmap = if_rmap_lookup (ifname);
158 if (!if_rmap)
159 return 0;
160
161 if (type == IF_RMAP_IN)
162 {
163 if (!if_rmap->routemap[IF_RMAP_IN])
164 return 0;
165 if (strcmp (if_rmap->routemap[IF_RMAP_IN], routemap_name) != 0)
166 return 0;
167
168 free (if_rmap->routemap[IF_RMAP_IN]);
169 if_rmap->routemap[IF_RMAP_IN] = NULL;
170 }
171
172 if (type == IF_RMAP_OUT)
173 {
174 if (!if_rmap->routemap[IF_RMAP_OUT])
175 return 0;
176 if (strcmp (if_rmap->routemap[IF_RMAP_OUT], routemap_name) != 0)
177 return 0;
178
179 free (if_rmap->routemap[IF_RMAP_OUT]);
180 if_rmap->routemap[IF_RMAP_OUT] = NULL;
181 }
182
183 if (if_rmap_delete_hook)
184 (*if_rmap_delete_hook) (if_rmap);
185
186 if (if_rmap->routemap[IF_RMAP_IN] == NULL &&
187 if_rmap->routemap[IF_RMAP_OUT] == NULL)
188 {
189 hash_release (ifrmaphash, if_rmap);
190 if_rmap_free (if_rmap);
191 }
192
193 return 1;
194}
195
hasso0750d212003-05-24 21:41:49 +0000196DEFUN (if_rmap,
197 if_rmap_cmd,
paul718e3742002-12-13 20:15:29 +0000198 "route-map RMAP_NAME (in|out) IFNAME",
199 "Route map set\n"
200 "Route map name\n"
201 "Route map set for input filtering\n"
202 "Route map set for output filtering\n"
203 "Route map interface name\n")
204{
205 enum if_rmap_type type;
206 struct if_rmap *if_rmap;
207
208 if (strncmp (argv[1], "i", 1) == 0)
209 type = IF_RMAP_IN;
210 else if (strncmp (argv[1], "o", 1) == 0)
211 type = IF_RMAP_OUT;
212 else
213 {
214 vty_out (vty, "route-map direction must be [in|out]%s", VTY_NEWLINE);
215 return CMD_WARNING;
216 }
217
218 if_rmap = if_rmap_set (argv[2], type, argv[0]);
219
220 return CMD_SUCCESS;
hasso4f849472003-05-25 15:13:49 +0000221}
222
223ALIAS (if_rmap,
224 if_ipv6_rmap_cmd,
225 "route-map RMAP_NAME (in|out) IFNAME",
226 "Route map set\n"
227 "Route map name\n"
228 "Route map set for input filtering\n"
229 "Route map set for output filtering\n"
230 "Route map interface name\n")
paul718e3742002-12-13 20:15:29 +0000231
hasso0750d212003-05-24 21:41:49 +0000232DEFUN (no_if_rmap,
233 no_if_rmap_cmd,
paul718e3742002-12-13 20:15:29 +0000234 "no route-map ROUTEMAP_NAME (in|out) IFNAME",
235 NO_STR
236 "Route map unset\n"
237 "Route map name\n"
238 "Route map for input filtering\n"
239 "Route map for output filtering\n"
240 "Route map interface name\n")
241{
242 int ret;
243 enum if_rmap_type type;
244
245 if (strncmp (argv[1], "i", 1) == 0)
246 type = IF_RMAP_IN;
247 else if (strncmp (argv[1], "o", 1) == 0)
248 type = IF_RMAP_OUT;
249 else
250 {
251 vty_out (vty, "route-map direction must be [in|out]%s", VTY_NEWLINE);
252 return CMD_WARNING;
253 }
254
255 ret = if_rmap_unset (argv[2], type, argv[0]);
256 if (! ret)
257 {
258 vty_out (vty, "route-map doesn't exist%s", VTY_NEWLINE);
259 return CMD_WARNING;
260 }
261 return CMD_SUCCESS;
hasso4f849472003-05-25 15:13:49 +0000262}
263
264ALIAS (no_if_rmap,
265 no_if_ipv6_rmap_cmd,
266 "no route-map ROUTEMAP_NAME (in|out) IFNAME",
267 NO_STR
268 "Route map unset\n"
269 "Route map name\n"
270 "Route map for input filtering\n"
271 "Route map for output filtering\n"
272 "Route map interface name\n")
paul718e3742002-12-13 20:15:29 +0000273
274/* Configuration write function. */
275int
276config_write_if_rmap (struct vty *vty)
277{
hasso8c328f12004-10-05 21:01:23 +0000278 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000279 struct hash_backet *mp;
280 int write = 0;
281
282 for (i = 0; i < ifrmaphash->size; i++)
283 for (mp = ifrmaphash->index[i]; mp; mp = mp->next)
284 {
285 struct if_rmap *if_rmap;
286
287 if_rmap = mp->data;
288
289 if (if_rmap->routemap[IF_RMAP_IN])
290 {
291 vty_out (vty, " route-map %s in %s%s",
292 if_rmap->routemap[IF_RMAP_IN],
293 if_rmap->ifname,
294 VTY_NEWLINE);
295 write++;
296 }
297
298 if (if_rmap->routemap[IF_RMAP_OUT])
299 {
300 vty_out (vty, " route-map %s out %s%s",
301 if_rmap->routemap[IF_RMAP_OUT],
302 if_rmap->ifname,
303 VTY_NEWLINE);
304 write++;
305 }
306 }
307 return write;
308}
309
310void
311if_rmap_reset ()
312{
313 hash_clean (ifrmaphash, (void (*) (void *)) if_rmap_free);
314}
315
316void
hasso0750d212003-05-24 21:41:49 +0000317if_rmap_init (int node)
paul718e3742002-12-13 20:15:29 +0000318{
319 ifrmaphash = hash_create (if_rmap_hash_make, if_rmap_hash_cmp);
hasso0750d212003-05-24 21:41:49 +0000320 if (node == RIPNG_NODE) {
hasso4f849472003-05-25 15:13:49 +0000321 install_element (RIPNG_NODE, &if_ipv6_rmap_cmd);
322 install_element (RIPNG_NODE, &no_if_ipv6_rmap_cmd);
323 } else if (node == RIP_NODE) {
324 install_element (RIP_NODE, &if_rmap_cmd);
325 install_element (RIP_NODE, &no_if_rmap_cmd);
hasso0750d212003-05-24 21:41:49 +0000326 }
paul718e3742002-12-13 20:15:29 +0000327}