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