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