1 /* route-map for interface.
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
29 DEFINE_MTYPE_STATIC(LIB
, IF_RMAP_CTX
, "Interface route map container");
30 DEFINE_MTYPE_STATIC(LIB
, IF_RMAP_CTX_NAME
,
31 "Interface route map container name");
32 DEFINE_MTYPE_STATIC(LIB
, IF_RMAP
, "Interface route map");
33 DEFINE_MTYPE_STATIC(LIB
, IF_RMAP_NAME
, "I.f. route map name");
35 static struct list
*if_rmap_ctx_list
;
37 static struct if_rmap
*if_rmap_new(void)
41 new = XCALLOC(MTYPE_IF_RMAP
, sizeof(struct if_rmap
));
46 static void if_rmap_free(struct if_rmap
*if_rmap
)
48 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->ifname
);
50 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
51 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
53 XFREE(MTYPE_IF_RMAP
, if_rmap
);
56 struct if_rmap
*if_rmap_lookup(struct if_rmap_ctx
*ctx
, const char *ifname
)
59 struct if_rmap
*if_rmap
;
62 key
.ifname
= (ifname
) ? XSTRDUP(MTYPE_IF_RMAP_NAME
, ifname
) : NULL
;
64 if_rmap
= hash_lookup(ctx
->ifrmaphash
, &key
);
66 XFREE(MTYPE_IF_RMAP_NAME
, key
.ifname
);
71 void if_rmap_hook_add(struct if_rmap_ctx
*ctx
,
72 void (*func
)(struct if_rmap_ctx
*ctx
,
75 ctx
->if_rmap_add_hook
= func
;
78 void if_rmap_hook_delete(struct if_rmap_ctx
*ctx
,
79 void (*func
)(struct if_rmap_ctx
*ctx
,
82 ctx
->if_rmap_delete_hook
= func
;
85 static void *if_rmap_hash_alloc(void *arg
)
87 struct if_rmap
*ifarg
= (struct if_rmap
*)arg
;
88 struct if_rmap
*if_rmap
;
90 if_rmap
= if_rmap_new();
91 if_rmap
->ifname
= XSTRDUP(MTYPE_IF_RMAP_NAME
, ifarg
->ifname
);
96 static struct if_rmap
*if_rmap_get(struct if_rmap_ctx
*ctx
, const char *ifname
)
102 key
.ifname
= (ifname
) ? XSTRDUP(MTYPE_IF_RMAP_NAME
, ifname
) : NULL
;
104 ret
= hash_get(ctx
->ifrmaphash
, &key
, if_rmap_hash_alloc
);
106 XFREE(MTYPE_IF_RMAP_NAME
, key
.ifname
);
111 static unsigned int if_rmap_hash_make(const void *data
)
113 const struct if_rmap
*if_rmap
= data
;
115 return string_hash_make(if_rmap
->ifname
);
118 static bool if_rmap_hash_cmp(const void *arg1
, const void *arg2
)
120 const struct if_rmap
*if_rmap1
= arg1
;
121 const struct if_rmap
*if_rmap2
= arg2
;
123 return strcmp(if_rmap1
->ifname
, if_rmap2
->ifname
) == 0;
126 static struct if_rmap
*if_rmap_set(struct if_rmap_ctx
*ctx
,
127 const char *ifname
, enum if_rmap_type type
,
128 const char *routemap_name
)
130 struct if_rmap
*if_rmap
;
132 if_rmap
= if_rmap_get(ctx
, ifname
);
134 if (type
== IF_RMAP_IN
) {
135 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
136 if_rmap
->routemap
[IF_RMAP_IN
] =
137 XSTRDUP(MTYPE_IF_RMAP_NAME
, routemap_name
);
139 if (type
== IF_RMAP_OUT
) {
140 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
141 if_rmap
->routemap
[IF_RMAP_OUT
] =
142 XSTRDUP(MTYPE_IF_RMAP_NAME
, routemap_name
);
145 if (ctx
->if_rmap_add_hook
)
146 (ctx
->if_rmap_add_hook
)(ctx
, if_rmap
);
151 static int if_rmap_unset(struct if_rmap_ctx
*ctx
,
152 const char *ifname
, enum if_rmap_type type
,
153 const char *routemap_name
)
155 struct if_rmap
*if_rmap
;
157 if_rmap
= if_rmap_lookup(ctx
, ifname
);
161 if (type
== IF_RMAP_IN
) {
162 if (!if_rmap
->routemap
[IF_RMAP_IN
])
164 if (strcmp(if_rmap
->routemap
[IF_RMAP_IN
], routemap_name
) != 0)
167 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
170 if (type
== IF_RMAP_OUT
) {
171 if (!if_rmap
->routemap
[IF_RMAP_OUT
])
173 if (strcmp(if_rmap
->routemap
[IF_RMAP_OUT
], routemap_name
) != 0)
176 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
179 if (ctx
->if_rmap_delete_hook
)
180 ctx
->if_rmap_delete_hook(ctx
, if_rmap
);
182 if (if_rmap
->routemap
[IF_RMAP_IN
] == NULL
183 && if_rmap
->routemap
[IF_RMAP_OUT
] == NULL
) {
184 hash_release(ctx
->ifrmaphash
, if_rmap
);
185 if_rmap_free(if_rmap
);
193 "route-map RMAP_NAME <in|out> IFNAME",
196 "Route map set for input filtering\n"
197 "Route map set for output filtering\n"
198 "Route map interface name\n")
200 int idx_rmap_name
= 1;
203 enum if_rmap_type type
;
204 struct if_rmap_ctx
*ctx
=
205 (struct if_rmap_ctx
*)listnode_head(if_rmap_ctx_list
);
207 if (strncmp(argv
[idx_in_out
]->text
, "in", 1) == 0)
209 else if (strncmp(argv
[idx_in_out
]->text
, "out", 1) == 0)
212 vty_out(vty
, "route-map direction must be [in|out]\n");
213 return CMD_WARNING_CONFIG_FAILED
;
216 if_rmap_set(ctx
, argv
[idx_ifname
]->arg
,
217 type
, argv
[idx_rmap_name
]->arg
);
224 "no route-map ROUTEMAP_NAME <in|out> IFNAME",
228 "Route map for input filtering\n"
229 "Route map for output filtering\n"
230 "Route map interface name\n")
232 int idx_routemap_name
= 2;
236 enum if_rmap_type type
;
237 struct if_rmap_ctx
*ctx
=
238 (struct if_rmap_ctx
*)listnode_head(if_rmap_ctx_list
);
240 if (strncmp(argv
[idx_in_out
]->arg
, "i", 1) == 0)
242 else if (strncmp(argv
[idx_in_out
]->arg
, "o", 1) == 0)
245 vty_out(vty
, "route-map direction must be [in|out]\n");
246 return CMD_WARNING_CONFIG_FAILED
;
249 ret
= if_rmap_unset(ctx
, argv
[idx_ifname
]->arg
, type
,
250 argv
[idx_routemap_name
]->arg
);
252 vty_out(vty
, "route-map doesn't exist\n");
253 return CMD_WARNING_CONFIG_FAILED
;
259 /* Configuration write function. */
260 int config_write_if_rmap(struct vty
*vty
,
261 struct if_rmap_ctx
*ctx
)
264 struct hash_bucket
*mp
;
266 struct hash
*ifrmaphash
= ctx
->ifrmaphash
;
268 for (i
= 0; i
< ifrmaphash
->size
; i
++)
269 for (mp
= ifrmaphash
->index
[i
]; mp
; mp
= mp
->next
) {
270 struct if_rmap
*if_rmap
;
274 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
275 vty_out(vty
, " route-map %s in %s\n",
276 if_rmap
->routemap
[IF_RMAP_IN
],
281 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
282 vty_out(vty
, " route-map %s out %s\n",
283 if_rmap
->routemap
[IF_RMAP_OUT
],
291 void if_rmap_ctx_delete(struct if_rmap_ctx
*ctx
)
293 listnode_delete(if_rmap_ctx_list
, ctx
);
294 hash_clean(ctx
->ifrmaphash
, (void (*)(void *))if_rmap_free
);
296 XFREE(MTYPE_IF_RMAP_CTX_NAME
, ctx
);
297 XFREE(MTYPE_IF_RMAP_CTX
, ctx
);
300 /* name is optional: either vrf name, or other */
301 struct if_rmap_ctx
*if_rmap_ctx_create(const char *name
)
303 struct if_rmap_ctx
*ctx
;
305 ctx
= XCALLOC(MTYPE_IF_RMAP_CTX
, sizeof(struct if_rmap_ctx
));
308 ctx
->name
= XSTRDUP(MTYPE_IF_RMAP_CTX_NAME
, name
);
309 ctx
->ifrmaphash
= hash_create_size(4, if_rmap_hash_make
, if_rmap_hash_cmp
,
310 "Interface Route-Map Hash");
311 if (!if_rmap_ctx_list
)
312 if_rmap_ctx_list
= list_new();
313 listnode_add(if_rmap_ctx_list
, ctx
);
317 void if_rmap_init(int node
)
319 if (node
== RIPNG_NODE
) {
320 } else if (node
== RIP_NODE
) {
321 install_element(RIP_NODE
, &if_rmap_cmd
);
322 install_element(RIP_NODE
, &no_if_rmap_cmd
);
324 if_rmap_ctx_list
= list_new();
327 void if_rmap_terminate(void)
329 if (!if_rmap_ctx_list
)
331 list_delete(&if_rmap_ctx_list
);