]>
git.proxmox.com Git - mirror_frr.git/blob - lib/if_rmap.c
1ce2a43c59a72148baf8d52a1656e4e359db2597
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
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
30 DEFINE_MTYPE_STATIC(LIB
, IF_RMAP
, "Interface route map")
31 DEFINE_MTYPE_STATIC(LIB
, IF_RMAP_NAME
, "I.f. route map name")
33 struct hash
*ifrmaphash
;
36 static void (*if_rmap_add_hook
)(struct if_rmap
*) = NULL
;
37 static void (*if_rmap_delete_hook
)(struct if_rmap
*) = NULL
;
39 static struct if_rmap
*if_rmap_new(void)
43 new = XCALLOC(MTYPE_IF_RMAP
, sizeof(struct if_rmap
));
48 static void if_rmap_free(struct if_rmap
*if_rmap
)
51 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->ifname
);
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
]);
58 XFREE(MTYPE_IF_RMAP
, if_rmap
);
61 struct if_rmap
*if_rmap_lookup(const char *ifname
)
64 struct if_rmap
*if_rmap
;
67 key
.ifname
= (ifname
) ? XSTRDUP(MTYPE_IF_RMAP_NAME
, ifname
) : NULL
;
69 if_rmap
= hash_lookup(ifrmaphash
, &key
);
72 XFREE(MTYPE_IF_RMAP_NAME
, key
.ifname
);
77 void if_rmap_hook_add(void (*func
)(struct if_rmap
*))
79 if_rmap_add_hook
= func
;
82 void if_rmap_hook_delete(void (*func
)(struct if_rmap
*))
84 if_rmap_delete_hook
= func
;
87 static void *if_rmap_hash_alloc(void *arg
)
89 struct if_rmap
*ifarg
= (struct if_rmap
*)arg
;
90 struct if_rmap
*if_rmap
;
92 if_rmap
= if_rmap_new();
93 if_rmap
->ifname
= XSTRDUP(MTYPE_IF_RMAP_NAME
, ifarg
->ifname
);
98 static struct if_rmap
*if_rmap_get(const char *ifname
)
104 key
.ifname
= (ifname
) ? XSTRDUP(MTYPE_IF_RMAP_NAME
, ifname
) : NULL
;
106 ret
= hash_get(ifrmaphash
, &key
, if_rmap_hash_alloc
);
109 XFREE(MTYPE_IF_RMAP_NAME
, key
.ifname
);
114 static unsigned int if_rmap_hash_make(void *data
)
116 const struct if_rmap
*if_rmap
= data
;
118 return string_hash_make(if_rmap
->ifname
);
121 static int if_rmap_hash_cmp(const void *arg1
, const void *arg2
)
123 const struct if_rmap
*if_rmap1
= arg1
;
124 const struct if_rmap
*if_rmap2
= arg2
;
126 return strcmp(if_rmap1
->ifname
, if_rmap2
->ifname
) == 0;
129 static struct if_rmap
*if_rmap_set(const char *ifname
, enum if_rmap_type type
,
130 const char *routemap_name
)
132 struct if_rmap
*if_rmap
;
134 if_rmap
= if_rmap_get(ifname
);
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
);
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
);
151 if (if_rmap_add_hook
)
152 (*if_rmap_add_hook
)(if_rmap
);
157 static int if_rmap_unset(const char *ifname
, enum if_rmap_type type
,
158 const char *routemap_name
)
160 struct if_rmap
*if_rmap
;
162 if_rmap
= if_rmap_lookup(ifname
);
166 if (type
== IF_RMAP_IN
) {
167 if (!if_rmap
->routemap
[IF_RMAP_IN
])
169 if (strcmp(if_rmap
->routemap
[IF_RMAP_IN
], routemap_name
) != 0)
172 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
173 if_rmap
->routemap
[IF_RMAP_IN
] = NULL
;
176 if (type
== IF_RMAP_OUT
) {
177 if (!if_rmap
->routemap
[IF_RMAP_OUT
])
179 if (strcmp(if_rmap
->routemap
[IF_RMAP_OUT
], routemap_name
) != 0)
182 XFREE(MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
183 if_rmap
->routemap
[IF_RMAP_OUT
] = NULL
;
186 if (if_rmap_delete_hook
)
187 (*if_rmap_delete_hook
)(if_rmap
);
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
);
200 "route-map RMAP_NAME <in|out> IFNAME",
203 "Route map set for input filtering\n"
204 "Route map set for output filtering\n"
205 "Route map interface name\n")
207 int idx_rmap_name
= 1;
210 enum if_rmap_type type
;
212 if (strncmp(argv
[idx_in_out
]->text
, "in", 1) == 0)
214 else if (strncmp(argv
[idx_in_out
]->text
, "out", 1) == 0)
217 vty_out(vty
, "route-map direction must be [in|out]%s",
222 if_rmap_set(argv
[idx_ifname
]->arg
, type
, argv
[idx_rmap_name
]->arg
);
229 "no route-map ROUTEMAP_NAME <in|out> IFNAME",
233 "Route map for input filtering\n"
234 "Route map for output filtering\n"
235 "Route map interface name\n")
237 int idx_routemap_name
= 2;
241 enum if_rmap_type type
;
243 if (strncmp(argv
[idx_in_out
]->arg
, "i", 1) == 0)
245 else if (strncmp(argv
[idx_in_out
]->arg
, "o", 1) == 0)
248 vty_out(vty
, "route-map direction must be [in|out]%s",
253 ret
= if_rmap_unset(argv
[idx_ifname
]->arg
, type
,
254 argv
[idx_routemap_name
]->arg
);
256 vty_out(vty
, "route-map doesn't exist%s", VTY_NEWLINE
);
263 /* Configuration write function. */
264 int config_write_if_rmap(struct vty
*vty
)
267 struct hash_backet
*mp
;
270 for (i
= 0; i
< ifrmaphash
->size
; i
++)
271 for (mp
= ifrmaphash
->index
[i
]; mp
; mp
= mp
->next
) {
272 struct if_rmap
*if_rmap
;
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
);
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
);
295 hash_clean(ifrmaphash
, (void (*)(void *))if_rmap_free
);
298 void if_rmap_init(int node
)
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
);