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
, "Interface route map container name")
31 DEFINE_MTYPE_STATIC(LIB
, IF_RMAP
, "Interface route map")
32 DEFINE_MTYPE_STATIC(LIB
, IF_RMAP_NAME
, "I.f. route map name")
34 static struct list
*if_rmap_ctx_list
;
36 static struct if_rmap
*if_rmap_new(void)
40 new = XCALLOC(MTYPE_IF_RMAP
, sizeof(struct if_rmap
));
45 static void if_rmap_free(struct if_rmap
*if_rmap
)
47 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->ifname
);
49 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
50 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
52 XFREE(MTYPE_IF_RMAP
, if_rmap
);
55 struct if_rmap
*if_rmap_lookup(struct if_rmap_ctx
*ctx
, const char *ifname
)
58 struct if_rmap
*if_rmap
;
61 key
.ifname
= (ifname
) ? XSTRDUP(MTYPE_IF_RMAP_NAME
, ifname
) : NULL
;
63 if_rmap
= hash_lookup(ctx
->ifrmaphash
, &key
);
65 XFREE(MTYPE_IF_RMAP_NAME
, key
.ifname
);
70 void if_rmap_hook_add(struct if_rmap_ctx
*ctx
,
71 void (*func
)(struct if_rmap_ctx
*ctx
,
74 ctx
->if_rmap_add_hook
= func
;
77 void if_rmap_hook_delete(struct if_rmap_ctx
*ctx
,
78 void (*func
)(struct if_rmap_ctx
*ctx
,
81 ctx
->if_rmap_delete_hook
= func
;
84 static void *if_rmap_hash_alloc(void *arg
)
86 struct if_rmap
*ifarg
= (struct if_rmap
*)arg
;
87 struct if_rmap
*if_rmap
;
89 if_rmap
= if_rmap_new();
90 if_rmap
->ifname
= XSTRDUP(MTYPE_IF_RMAP_NAME
, ifarg
->ifname
);
95 static struct if_rmap
*if_rmap_get(struct if_rmap_ctx
*ctx
, const char *ifname
)
101 key
.ifname
= (ifname
) ? XSTRDUP(MTYPE_IF_RMAP_NAME
, ifname
) : NULL
;
103 ret
= hash_get(ctx
->ifrmaphash
, &key
, if_rmap_hash_alloc
);
105 XFREE(MTYPE_IF_RMAP_NAME
, key
.ifname
);
110 static unsigned int if_rmap_hash_make(const void *data
)
112 const struct if_rmap
*if_rmap
= data
;
114 return string_hash_make(if_rmap
->ifname
);
117 static bool if_rmap_hash_cmp(const void *arg1
, const void *arg2
)
119 const struct if_rmap
*if_rmap1
= arg1
;
120 const struct if_rmap
*if_rmap2
= arg2
;
122 return strcmp(if_rmap1
->ifname
, if_rmap2
->ifname
) == 0;
125 static struct if_rmap
*if_rmap_set(struct if_rmap_ctx
*ctx
,
126 const char *ifname
, enum if_rmap_type type
,
127 const char *routemap_name
)
129 struct if_rmap
*if_rmap
;
131 if_rmap
= if_rmap_get(ctx
, ifname
);
133 if (type
== IF_RMAP_IN
) {
134 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
135 if_rmap
->routemap
[IF_RMAP_IN
] =
136 XSTRDUP(MTYPE_IF_RMAP_NAME
, routemap_name
);
138 if (type
== IF_RMAP_OUT
) {
139 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
140 if_rmap
->routemap
[IF_RMAP_OUT
] =
141 XSTRDUP(MTYPE_IF_RMAP_NAME
, routemap_name
);
144 if (ctx
->if_rmap_add_hook
)
145 (ctx
->if_rmap_add_hook
)(ctx
, if_rmap
);
150 static int if_rmap_unset(struct if_rmap_ctx
*ctx
,
151 const char *ifname
, enum if_rmap_type type
,
152 const char *routemap_name
)
154 struct if_rmap
*if_rmap
;
156 if_rmap
= if_rmap_lookup(ctx
, ifname
);
160 if (type
== IF_RMAP_IN
) {
161 if (!if_rmap
->routemap
[IF_RMAP_IN
])
163 if (strcmp(if_rmap
->routemap
[IF_RMAP_IN
], routemap_name
) != 0)
166 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
167 if_rmap
->routemap
[IF_RMAP_IN
] = NULL
;
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
]);
177 if_rmap
->routemap
[IF_RMAP_OUT
] = NULL
;
180 if (ctx
->if_rmap_delete_hook
)
181 ctx
->if_rmap_delete_hook(ctx
, if_rmap
);
183 if (if_rmap
->routemap
[IF_RMAP_IN
] == NULL
184 && if_rmap
->routemap
[IF_RMAP_OUT
] == NULL
) {
185 hash_release(ctx
->ifrmaphash
, if_rmap
);
186 if_rmap_free(if_rmap
);
194 "route-map RMAP_NAME <in|out> IFNAME",
197 "Route map set for input filtering\n"
198 "Route map set for output filtering\n"
199 "Route map interface name\n")
201 int idx_rmap_name
= 1;
204 enum if_rmap_type type
;
205 struct if_rmap_ctx
*ctx
=
206 (struct if_rmap_ctx
*)listnode_head(if_rmap_ctx_list
);
208 if (strncmp(argv
[idx_in_out
]->text
, "in", 1) == 0)
210 else if (strncmp(argv
[idx_in_out
]->text
, "out", 1) == 0)
213 vty_out(vty
, "route-map direction must be [in|out]\n");
214 return CMD_WARNING_CONFIG_FAILED
;
217 if_rmap_set(ctx
, argv
[idx_ifname
]->arg
,
218 type
, argv
[idx_rmap_name
]->arg
);
225 "no route-map ROUTEMAP_NAME <in|out> IFNAME",
229 "Route map for input filtering\n"
230 "Route map for output filtering\n"
231 "Route map interface name\n")
233 int idx_routemap_name
= 2;
237 enum if_rmap_type type
;
238 struct if_rmap_ctx
*ctx
=
239 (struct if_rmap_ctx
*)listnode_head(if_rmap_ctx_list
);
241 if (strncmp(argv
[idx_in_out
]->arg
, "i", 1) == 0)
243 else if (strncmp(argv
[idx_in_out
]->arg
, "o", 1) == 0)
246 vty_out(vty
, "route-map direction must be [in|out]\n");
247 return CMD_WARNING_CONFIG_FAILED
;
250 ret
= if_rmap_unset(ctx
, argv
[idx_ifname
]->arg
, type
,
251 argv
[idx_routemap_name
]->arg
);
253 vty_out(vty
, "route-map doesn't exist\n");
254 return CMD_WARNING_CONFIG_FAILED
;
260 /* Configuration write function. */
261 int config_write_if_rmap(struct vty
*vty
,
262 struct if_rmap_ctx
*ctx
)
265 struct hash_bucket
*mp
;
267 struct hash
*ifrmaphash
= ctx
->ifrmaphash
;
269 for (i
= 0; i
< ifrmaphash
->size
; i
++)
270 for (mp
= ifrmaphash
->index
[i
]; mp
; mp
= mp
->next
) {
271 struct if_rmap
*if_rmap
;
275 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
276 vty_out(vty
, " route-map %s in %s\n",
277 if_rmap
->routemap
[IF_RMAP_IN
],
282 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
283 vty_out(vty
, " route-map %s out %s\n",
284 if_rmap
->routemap
[IF_RMAP_OUT
],
292 void if_rmap_ctx_delete(struct if_rmap_ctx
*ctx
)
294 listnode_delete(if_rmap_ctx_list
, ctx
);
295 hash_clean(ctx
->ifrmaphash
, (void (*)(void *))if_rmap_free
);
297 XFREE(MTYPE_IF_RMAP_CTX_NAME
, ctx
);
298 XFREE(MTYPE_IF_RMAP_CTX
, ctx
);
301 /* name is optional: either vrf name, or other */
302 struct if_rmap_ctx
*if_rmap_ctx_create(const char *name
)
304 struct if_rmap_ctx
*ctx
;
306 ctx
= XCALLOC(MTYPE_IF_RMAP_CTX
, sizeof(struct if_rmap_ctx
));
309 ctx
->name
= XSTRDUP(MTYPE_IF_RMAP_CTX_NAME
, name
);
310 ctx
->ifrmaphash
= hash_create_size(4, if_rmap_hash_make
, if_rmap_hash_cmp
,
311 "Interface Route-Map Hash");
312 if (!if_rmap_ctx_list
)
313 if_rmap_ctx_list
= list_new();
314 listnode_add(if_rmap_ctx_list
, ctx
);
318 void if_rmap_init(int node
)
320 if (node
== RIPNG_NODE
) {
321 } else if (node
== RIP_NODE
) {
322 install_element(RIP_NODE
, &if_rmap_cmd
);
323 install_element(RIP_NODE
, &no_if_rmap_cmd
);
325 if_rmap_ctx_list
= list_new();
328 void if_rmap_terminate(void)
330 if (!if_rmap_ctx_list
)
332 list_delete(&if_rmap_ctx_list
);