]>
git.proxmox.com Git - mirror_frr.git/blob - lib/if_rmap.c
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
*
44 new = XCALLOC (MTYPE_IF_RMAP
, sizeof (struct if_rmap
));
50 if_rmap_free (struct if_rmap
*if_rmap
)
53 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->ifname
);
55 if (if_rmap
->routemap
[IF_RMAP_IN
])
56 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
57 if (if_rmap
->routemap
[IF_RMAP_OUT
])
58 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
60 XFREE (MTYPE_IF_RMAP
, if_rmap
);
64 if_rmap_lookup (const char *ifname
)
67 struct if_rmap
*if_rmap
;
70 key
.ifname
= (ifname
) ? XSTRDUP (MTYPE_IF_RMAP_NAME
, ifname
) : NULL
;
72 if_rmap
= hash_lookup (ifrmaphash
, &key
);
75 XFREE(MTYPE_IF_RMAP_NAME
, key
.ifname
);
81 if_rmap_hook_add (void (*func
) (struct if_rmap
*))
83 if_rmap_add_hook
= func
;
87 if_rmap_hook_delete (void (*func
) (struct if_rmap
*))
89 if_rmap_delete_hook
= func
;
93 if_rmap_hash_alloc (void *arg
)
95 struct if_rmap
*ifarg
= (struct if_rmap
*)arg
;
96 struct if_rmap
*if_rmap
;
98 if_rmap
= if_rmap_new ();
99 if_rmap
->ifname
= XSTRDUP (MTYPE_IF_RMAP_NAME
, ifarg
->ifname
);
104 static struct if_rmap
*
105 if_rmap_get (const char *ifname
)
111 key
.ifname
= (ifname
) ? XSTRDUP (MTYPE_IF_RMAP_NAME
, ifname
) : NULL
;
113 ret
= hash_get (ifrmaphash
, &key
, if_rmap_hash_alloc
);
116 XFREE(MTYPE_IF_RMAP_NAME
, key
.ifname
);
122 if_rmap_hash_make (void *data
)
124 const struct if_rmap
*if_rmap
= data
;
126 return string_hash_make (if_rmap
->ifname
);
130 if_rmap_hash_cmp (const void *arg1
, const void* arg2
)
132 const struct if_rmap
*if_rmap1
= arg1
;
133 const struct if_rmap
*if_rmap2
= arg2
;
135 return strcmp (if_rmap1
->ifname
, if_rmap2
->ifname
) == 0;
138 static struct if_rmap
*
139 if_rmap_set (const char *ifname
, enum if_rmap_type type
,
140 const char *routemap_name
)
142 struct if_rmap
*if_rmap
;
144 if_rmap
= if_rmap_get (ifname
);
146 if (type
== IF_RMAP_IN
)
148 if (if_rmap
->routemap
[IF_RMAP_IN
])
149 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
150 if_rmap
->routemap
[IF_RMAP_IN
]
151 = XSTRDUP (MTYPE_IF_RMAP_NAME
, routemap_name
);
153 if (type
== IF_RMAP_OUT
)
155 if (if_rmap
->routemap
[IF_RMAP_OUT
])
156 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
157 if_rmap
->routemap
[IF_RMAP_OUT
]
158 = XSTRDUP (MTYPE_IF_RMAP_NAME
, routemap_name
);
161 if (if_rmap_add_hook
)
162 (*if_rmap_add_hook
) (if_rmap
);
168 if_rmap_unset (const char *ifname
, enum if_rmap_type type
,
169 const char *routemap_name
)
171 struct if_rmap
*if_rmap
;
173 if_rmap
= if_rmap_lookup (ifname
);
177 if (type
== IF_RMAP_IN
)
179 if (!if_rmap
->routemap
[IF_RMAP_IN
])
181 if (strcmp (if_rmap
->routemap
[IF_RMAP_IN
], routemap_name
) != 0)
184 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
185 if_rmap
->routemap
[IF_RMAP_IN
] = NULL
;
188 if (type
== IF_RMAP_OUT
)
190 if (!if_rmap
->routemap
[IF_RMAP_OUT
])
192 if (strcmp (if_rmap
->routemap
[IF_RMAP_OUT
], routemap_name
) != 0)
195 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
196 if_rmap
->routemap
[IF_RMAP_OUT
] = NULL
;
199 if (if_rmap_delete_hook
)
200 (*if_rmap_delete_hook
) (if_rmap
);
202 if (if_rmap
->routemap
[IF_RMAP_IN
] == NULL
&&
203 if_rmap
->routemap
[IF_RMAP_OUT
] == NULL
)
205 hash_release (ifrmaphash
, if_rmap
);
206 if_rmap_free (if_rmap
);
214 "route-map RMAP_NAME <in|out> IFNAME",
217 "Route map set for input filtering\n"
218 "Route map set for output filtering\n"
219 "Route map interface name\n")
221 int idx_rmap_name
= 1;
224 enum if_rmap_type type
;
226 if (strncmp (argv
[idx_in_out
]->text
, "in", 1) == 0)
228 else if (strncmp (argv
[idx_in_out
]->text
, "out", 1) == 0)
232 vty_out (vty
, "route-map direction must be [in|out]%s", VTY_NEWLINE
);
236 if_rmap_set (argv
[idx_ifname
]->arg
, type
, argv
[idx_rmap_name
]->arg
);
243 "no route-map ROUTEMAP_NAME <in|out> IFNAME",
247 "Route map for input filtering\n"
248 "Route map for output filtering\n"
249 "Route map interface name\n")
251 int idx_routemap_name
= 2;
255 enum if_rmap_type type
;
257 if (strncmp (argv
[idx_in_out
]->arg
, "i", 1) == 0)
259 else if (strncmp (argv
[idx_in_out
]->arg
, "o", 1) == 0)
263 vty_out (vty
, "route-map direction must be [in|out]%s", VTY_NEWLINE
);
267 ret
= if_rmap_unset (argv
[idx_ifname
]->arg
, type
, argv
[idx_routemap_name
]->arg
);
270 vty_out (vty
, "route-map doesn't exist%s", VTY_NEWLINE
);
277 /* Configuration write function. */
279 config_write_if_rmap (struct vty
*vty
)
282 struct hash_backet
*mp
;
285 for (i
= 0; i
< ifrmaphash
->size
; i
++)
286 for (mp
= ifrmaphash
->index
[i
]; mp
; mp
= mp
->next
)
288 struct if_rmap
*if_rmap
;
292 if (if_rmap
->routemap
[IF_RMAP_IN
])
294 vty_out (vty
, " route-map %s in %s%s",
295 if_rmap
->routemap
[IF_RMAP_IN
],
301 if (if_rmap
->routemap
[IF_RMAP_OUT
])
303 vty_out (vty
, " route-map %s out %s%s",
304 if_rmap
->routemap
[IF_RMAP_OUT
],
316 hash_clean (ifrmaphash
, (void (*) (void *)) if_rmap_free
);
320 if_rmap_init (int node
)
322 ifrmaphash
= hash_create (if_rmap_hash_make
, if_rmap_hash_cmp
);
323 if (node
== RIPNG_NODE
) {
324 } else if (node
== RIP_NODE
) {
325 install_element (RIP_NODE
, &if_rmap_cmd
);
326 install_element (RIP_NODE
, &no_if_rmap_cmd
);