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
, "Interface route map")
30 DEFINE_MTYPE_STATIC(LIB
, IF_RMAP_NAME
, "I.f. route map name")
32 struct hash
*ifrmaphash
;
35 static void (*if_rmap_add_hook
)(struct if_rmap
*) = NULL
;
36 static void (*if_rmap_delete_hook
)(struct if_rmap
*) = NULL
;
38 static struct if_rmap
*if_rmap_new(void)
42 new = XCALLOC(MTYPE_IF_RMAP
, sizeof(struct if_rmap
));
47 static void if_rmap_free(struct if_rmap
*if_rmap
)
50 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->ifname
);
52 if (if_rmap
->routemap
[IF_RMAP_IN
])
53 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
54 if (if_rmap
->routemap
[IF_RMAP_OUT
])
55 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
57 XFREE(MTYPE_IF_RMAP
, if_rmap
);
60 struct if_rmap
*if_rmap_lookup(const char *ifname
)
63 struct if_rmap
*if_rmap
;
66 key
.ifname
= (ifname
) ? XSTRDUP(MTYPE_IF_RMAP_NAME
, ifname
) : NULL
;
68 if_rmap
= hash_lookup(ifrmaphash
, &key
);
71 XFREE(MTYPE_IF_RMAP_NAME
, key
.ifname
);
76 void if_rmap_hook_add(void (*func
)(struct if_rmap
*))
78 if_rmap_add_hook
= func
;
81 void if_rmap_hook_delete(void (*func
)(struct if_rmap
*))
83 if_rmap_delete_hook
= func
;
86 static void *if_rmap_hash_alloc(void *arg
)
88 struct if_rmap
*ifarg
= (struct if_rmap
*)arg
;
89 struct if_rmap
*if_rmap
;
91 if_rmap
= if_rmap_new();
92 if_rmap
->ifname
= XSTRDUP(MTYPE_IF_RMAP_NAME
, ifarg
->ifname
);
97 static struct if_rmap
*if_rmap_get(const char *ifname
)
103 key
.ifname
= (ifname
) ? XSTRDUP(MTYPE_IF_RMAP_NAME
, ifname
) : NULL
;
105 ret
= hash_get(ifrmaphash
, &key
, if_rmap_hash_alloc
);
108 XFREE(MTYPE_IF_RMAP_NAME
, key
.ifname
);
113 static unsigned int if_rmap_hash_make(void *data
)
115 const struct if_rmap
*if_rmap
= data
;
117 return string_hash_make(if_rmap
->ifname
);
120 static int if_rmap_hash_cmp(const void *arg1
, const void *arg2
)
122 const struct if_rmap
*if_rmap1
= arg1
;
123 const struct if_rmap
*if_rmap2
= arg2
;
125 return strcmp(if_rmap1
->ifname
, if_rmap2
->ifname
) == 0;
128 static struct if_rmap
*if_rmap_set(const char *ifname
, enum if_rmap_type type
,
129 const char *routemap_name
)
131 struct if_rmap
*if_rmap
;
133 if_rmap
= if_rmap_get(ifname
);
135 if (type
== IF_RMAP_IN
) {
136 if (if_rmap
->routemap
[IF_RMAP_IN
])
137 XFREE(MTYPE_IF_RMAP_NAME
,
138 if_rmap
->routemap
[IF_RMAP_IN
]);
139 if_rmap
->routemap
[IF_RMAP_IN
] =
140 XSTRDUP(MTYPE_IF_RMAP_NAME
, routemap_name
);
142 if (type
== IF_RMAP_OUT
) {
143 if (if_rmap
->routemap
[IF_RMAP_OUT
])
144 XFREE(MTYPE_IF_RMAP_NAME
,
145 if_rmap
->routemap
[IF_RMAP_OUT
]);
146 if_rmap
->routemap
[IF_RMAP_OUT
] =
147 XSTRDUP(MTYPE_IF_RMAP_NAME
, routemap_name
);
150 if (if_rmap_add_hook
)
151 (*if_rmap_add_hook
)(if_rmap
);
156 static int if_rmap_unset(const char *ifname
, enum if_rmap_type type
,
157 const char *routemap_name
)
159 struct if_rmap
*if_rmap
;
161 if_rmap
= if_rmap_lookup(ifname
);
165 if (type
== IF_RMAP_IN
) {
166 if (!if_rmap
->routemap
[IF_RMAP_IN
])
168 if (strcmp(if_rmap
->routemap
[IF_RMAP_IN
], routemap_name
) != 0)
171 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
172 if_rmap
->routemap
[IF_RMAP_IN
] = NULL
;
175 if (type
== IF_RMAP_OUT
) {
176 if (!if_rmap
->routemap
[IF_RMAP_OUT
])
178 if (strcmp(if_rmap
->routemap
[IF_RMAP_OUT
], routemap_name
) != 0)
181 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
182 if_rmap
->routemap
[IF_RMAP_OUT
] = NULL
;
185 if (if_rmap_delete_hook
)
186 (*if_rmap_delete_hook
)(if_rmap
);
188 if (if_rmap
->routemap
[IF_RMAP_IN
] == NULL
189 && if_rmap
->routemap
[IF_RMAP_OUT
] == NULL
) {
190 hash_release(ifrmaphash
, if_rmap
);
191 if_rmap_free(if_rmap
);
199 "route-map RMAP_NAME <in|out> IFNAME",
202 "Route map set for input filtering\n"
203 "Route map set for output filtering\n"
204 "Route map interface name\n")
206 int idx_rmap_name
= 1;
209 enum if_rmap_type type
;
211 if (strncmp(argv
[idx_in_out
]->text
, "in", 1) == 0)
213 else if (strncmp(argv
[idx_in_out
]->text
, "out", 1) == 0)
216 vty_out(vty
, "route-map direction must be [in|out]\n");
217 return CMD_WARNING_CONFIG_FAILED
;
220 if_rmap_set(argv
[idx_ifname
]->arg
, type
, argv
[idx_rmap_name
]->arg
);
227 "no route-map ROUTEMAP_NAME <in|out> IFNAME",
231 "Route map for input filtering\n"
232 "Route map for output filtering\n"
233 "Route map interface name\n")
235 int idx_routemap_name
= 2;
239 enum if_rmap_type type
;
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(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
)
264 struct hash_backet
*mp
;
267 for (i
= 0; i
< ifrmaphash
->size
; i
++)
268 for (mp
= ifrmaphash
->index
[i
]; mp
; mp
= mp
->next
) {
269 struct if_rmap
*if_rmap
;
273 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
274 vty_out(vty
, " route-map %s in %s\n",
275 if_rmap
->routemap
[IF_RMAP_IN
],
280 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
281 vty_out(vty
, " route-map %s out %s\n",
282 if_rmap
->routemap
[IF_RMAP_OUT
],
292 hash_clean(ifrmaphash
, (void (*)(void *))if_rmap_free
);
295 void if_rmap_init(int node
)
297 ifrmaphash
= hash_create_size(4,
300 "Interface Route-Map Hash");
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
);