]> git.proxmox.com Git - mirror_frr.git/blob - lib/if_rmap.c
Merge branch 'frr/pull/550'
[mirror_frr.git] / lib / if_rmap.c
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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "hash.h"
24 #include "command.h"
25 #include "memory.h"
26 #include "if.h"
27 #include "if_rmap.h"
28
29 DEFINE_MTYPE_STATIC(LIB, IF_RMAP, "Interface route map")
30 DEFINE_MTYPE_STATIC(LIB, IF_RMAP_NAME, "I.f. route map name")
31
32 struct hash *ifrmaphash;
33
34 /* Hook functions. */
35 static void (*if_rmap_add_hook) (struct if_rmap *) = NULL;
36 static void (*if_rmap_delete_hook) (struct if_rmap *) = NULL;
37
38 static struct if_rmap *
39 if_rmap_new (void)
40 {
41 struct if_rmap *new;
42
43 new = XCALLOC (MTYPE_IF_RMAP, sizeof (struct if_rmap));
44
45 return new;
46 }
47
48 static void
49 if_rmap_free (struct if_rmap *if_rmap)
50 {
51 if (if_rmap->ifname)
52 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->ifname);
53
54 if (if_rmap->routemap[IF_RMAP_IN])
55 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
56 if (if_rmap->routemap[IF_RMAP_OUT])
57 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
58
59 XFREE (MTYPE_IF_RMAP, if_rmap);
60 }
61
62 struct if_rmap *
63 if_rmap_lookup (const char *ifname)
64 {
65 struct if_rmap key;
66 struct if_rmap *if_rmap;
67
68 /* temporary copy */
69 key.ifname = (ifname) ? XSTRDUP (MTYPE_IF_RMAP_NAME, ifname) : NULL;
70
71 if_rmap = hash_lookup (ifrmaphash, &key);
72
73 if (key.ifname)
74 XFREE(MTYPE_IF_RMAP_NAME, key.ifname);
75
76 return if_rmap;
77 }
78
79 void
80 if_rmap_hook_add (void (*func) (struct if_rmap *))
81 {
82 if_rmap_add_hook = func;
83 }
84
85 void
86 if_rmap_hook_delete (void (*func) (struct if_rmap *))
87 {
88 if_rmap_delete_hook = func;
89 }
90
91 static void *
92 if_rmap_hash_alloc (void *arg)
93 {
94 struct if_rmap *ifarg = (struct if_rmap *)arg;
95 struct if_rmap *if_rmap;
96
97 if_rmap = if_rmap_new ();
98 if_rmap->ifname = XSTRDUP (MTYPE_IF_RMAP_NAME, ifarg->ifname);
99
100 return if_rmap;
101 }
102
103 static struct if_rmap *
104 if_rmap_get (const char *ifname)
105 {
106 struct if_rmap key;
107 struct if_rmap *ret;
108
109 /* temporary copy */
110 key.ifname = (ifname) ? XSTRDUP (MTYPE_IF_RMAP_NAME, ifname) : NULL;
111
112 ret = hash_get (ifrmaphash, &key, if_rmap_hash_alloc);
113
114 if (key.ifname)
115 XFREE(MTYPE_IF_RMAP_NAME, key.ifname);
116
117 return ret;
118 }
119
120 static unsigned int
121 if_rmap_hash_make (void *data)
122 {
123 const struct if_rmap *if_rmap = data;
124
125 return string_hash_make (if_rmap->ifname);
126 }
127
128 static int
129 if_rmap_hash_cmp (const void *arg1, const void* arg2)
130 {
131 const struct if_rmap *if_rmap1 = arg1;
132 const struct if_rmap *if_rmap2 = arg2;
133
134 return strcmp (if_rmap1->ifname, if_rmap2->ifname) == 0;
135 }
136
137 static struct if_rmap *
138 if_rmap_set (const char *ifname, enum if_rmap_type type,
139 const char *routemap_name)
140 {
141 struct if_rmap *if_rmap;
142
143 if_rmap = if_rmap_get (ifname);
144
145 if (type == IF_RMAP_IN)
146 {
147 if (if_rmap->routemap[IF_RMAP_IN])
148 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
149 if_rmap->routemap[IF_RMAP_IN]
150 = XSTRDUP (MTYPE_IF_RMAP_NAME, routemap_name);
151 }
152 if (type == IF_RMAP_OUT)
153 {
154 if (if_rmap->routemap[IF_RMAP_OUT])
155 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
156 if_rmap->routemap[IF_RMAP_OUT]
157 = XSTRDUP (MTYPE_IF_RMAP_NAME, routemap_name);
158 }
159
160 if (if_rmap_add_hook)
161 (*if_rmap_add_hook) (if_rmap);
162
163 return if_rmap;
164 }
165
166 static int
167 if_rmap_unset (const char *ifname, enum if_rmap_type type,
168 const char *routemap_name)
169 {
170 struct if_rmap *if_rmap;
171
172 if_rmap = if_rmap_lookup (ifname);
173 if (!if_rmap)
174 return 0;
175
176 if (type == IF_RMAP_IN)
177 {
178 if (!if_rmap->routemap[IF_RMAP_IN])
179 return 0;
180 if (strcmp (if_rmap->routemap[IF_RMAP_IN], routemap_name) != 0)
181 return 0;
182
183 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
184 if_rmap->routemap[IF_RMAP_IN] = NULL;
185 }
186
187 if (type == IF_RMAP_OUT)
188 {
189 if (!if_rmap->routemap[IF_RMAP_OUT])
190 return 0;
191 if (strcmp (if_rmap->routemap[IF_RMAP_OUT], routemap_name) != 0)
192 return 0;
193
194 XFREE (MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
195 if_rmap->routemap[IF_RMAP_OUT] = NULL;
196 }
197
198 if (if_rmap_delete_hook)
199 (*if_rmap_delete_hook) (if_rmap);
200
201 if (if_rmap->routemap[IF_RMAP_IN] == NULL &&
202 if_rmap->routemap[IF_RMAP_OUT] == NULL)
203 {
204 hash_release (ifrmaphash, if_rmap);
205 if_rmap_free (if_rmap);
206 }
207
208 return 1;
209 }
210
211 DEFUN (if_rmap,
212 if_rmap_cmd,
213 "route-map RMAP_NAME <in|out> IFNAME",
214 "Route map set\n"
215 "Route map name\n"
216 "Route map set for input filtering\n"
217 "Route map set for output filtering\n"
218 "Route map interface name\n")
219 {
220 int idx_rmap_name = 1;
221 int idx_in_out = 2;
222 int idx_ifname = 3;
223 enum if_rmap_type type;
224
225 if (strncmp (argv[idx_in_out]->text, "in", 1) == 0)
226 type = IF_RMAP_IN;
227 else if (strncmp (argv[idx_in_out]->text, "out", 1) == 0)
228 type = IF_RMAP_OUT;
229 else
230 {
231 vty_out (vty, "route-map direction must be [in|out]%s", VTY_NEWLINE);
232 return CMD_WARNING;
233 }
234
235 if_rmap_set (argv[idx_ifname]->arg, type, argv[idx_rmap_name]->arg);
236
237 return CMD_SUCCESS;
238 }
239
240 DEFUN (no_if_rmap,
241 no_if_rmap_cmd,
242 "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 idx_routemap_name = 2;
251 int idx_in_out = 3;
252 int idx_ifname = 4;
253 int ret;
254 enum if_rmap_type type;
255
256 if (strncmp (argv[idx_in_out]->arg, "i", 1) == 0)
257 type = IF_RMAP_IN;
258 else if (strncmp (argv[idx_in_out]->arg, "o", 1) == 0)
259 type = IF_RMAP_OUT;
260 else
261 {
262 vty_out (vty, "route-map direction must be [in|out]%s", VTY_NEWLINE);
263 return CMD_WARNING;
264 }
265
266 ret = if_rmap_unset (argv[idx_ifname]->arg, type, argv[idx_routemap_name]->arg);
267 if (! ret)
268 {
269 vty_out (vty, "route-map doesn't exist%s", VTY_NEWLINE);
270 return CMD_WARNING;
271 }
272 return CMD_SUCCESS;
273 }
274
275
276 /* Configuration write function. */
277 int
278 config_write_if_rmap (struct vty *vty)
279 {
280 unsigned int i;
281 struct hash_backet *mp;
282 int write = 0;
283
284 for (i = 0; i < ifrmaphash->size; i++)
285 for (mp = ifrmaphash->index[i]; mp; mp = mp->next)
286 {
287 struct if_rmap *if_rmap;
288
289 if_rmap = mp->data;
290
291 if (if_rmap->routemap[IF_RMAP_IN])
292 {
293 vty_out (vty, " route-map %s in %s%s",
294 if_rmap->routemap[IF_RMAP_IN],
295 if_rmap->ifname,
296 VTY_NEWLINE);
297 write++;
298 }
299
300 if (if_rmap->routemap[IF_RMAP_OUT])
301 {
302 vty_out (vty, " route-map %s out %s%s",
303 if_rmap->routemap[IF_RMAP_OUT],
304 if_rmap->ifname,
305 VTY_NEWLINE);
306 write++;
307 }
308 }
309 return write;
310 }
311
312 void
313 if_rmap_reset ()
314 {
315 hash_clean (ifrmaphash, (void (*) (void *)) if_rmap_free);
316 }
317
318 void
319 if_rmap_init (int node)
320 {
321 ifrmaphash = hash_create (if_rmap_hash_make, if_rmap_hash_cmp);
322 if (node == RIPNG_NODE) {
323 } else if (node == RIP_NODE) {
324 install_element (RIP_NODE, &if_rmap_cmd);
325 install_element (RIP_NODE, &no_if_rmap_cmd);
326 }
327 }