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
*
43 new = XCALLOC (MTYPE_IF_RMAP
, sizeof (struct if_rmap
));
49 if_rmap_free (struct if_rmap
*if_rmap
)
52 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->ifname
);
54 if (if_rmap
->routemap
[IF_RMAP_IN
])
55 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
56 if (if_rmap
->routemap
[IF_RMAP_OUT
])
57 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
59 XFREE (MTYPE_IF_RMAP
, if_rmap
);
63 if_rmap_lookup (const char *ifname
)
66 struct if_rmap
*if_rmap
;
69 key
.ifname
= (ifname
) ? XSTRDUP (MTYPE_IF_RMAP_NAME
, ifname
) : NULL
;
71 if_rmap
= hash_lookup (ifrmaphash
, &key
);
74 XFREE(MTYPE_IF_RMAP_NAME
, key
.ifname
);
80 if_rmap_hook_add (void (*func
) (struct if_rmap
*))
82 if_rmap_add_hook
= func
;
86 if_rmap_hook_delete (void (*func
) (struct if_rmap
*))
88 if_rmap_delete_hook
= func
;
92 if_rmap_hash_alloc (void *arg
)
94 struct if_rmap
*ifarg
= (struct if_rmap
*)arg
;
95 struct if_rmap
*if_rmap
;
97 if_rmap
= if_rmap_new ();
98 if_rmap
->ifname
= XSTRDUP (MTYPE_IF_RMAP_NAME
, ifarg
->ifname
);
103 static struct if_rmap
*
104 if_rmap_get (const char *ifname
)
110 key
.ifname
= (ifname
) ? XSTRDUP (MTYPE_IF_RMAP_NAME
, ifname
) : NULL
;
112 ret
= hash_get (ifrmaphash
, &key
, if_rmap_hash_alloc
);
115 XFREE(MTYPE_IF_RMAP_NAME
, key
.ifname
);
121 if_rmap_hash_make (void *data
)
123 const struct if_rmap
*if_rmap
= data
;
125 return string_hash_make (if_rmap
->ifname
);
129 if_rmap_hash_cmp (const void *arg1
, const void* arg2
)
131 const struct if_rmap
*if_rmap1
= arg1
;
132 const struct if_rmap
*if_rmap2
= arg2
;
134 return strcmp (if_rmap1
->ifname
, if_rmap2
->ifname
) == 0;
137 static struct if_rmap
*
138 if_rmap_set (const char *ifname
, enum if_rmap_type type
,
139 const char *routemap_name
)
141 struct if_rmap
*if_rmap
;
143 if_rmap
= if_rmap_get (ifname
);
145 if (type
== IF_RMAP_IN
)
147 if (if_rmap
->routemap
[IF_RMAP_IN
])
148 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
149 if_rmap
->routemap
[IF_RMAP_IN
]
150 = XSTRDUP (MTYPE_IF_RMAP_NAME
, routemap_name
);
152 if (type
== IF_RMAP_OUT
)
154 if (if_rmap
->routemap
[IF_RMAP_OUT
])
155 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
156 if_rmap
->routemap
[IF_RMAP_OUT
]
157 = XSTRDUP (MTYPE_IF_RMAP_NAME
, routemap_name
);
160 if (if_rmap_add_hook
)
161 (*if_rmap_add_hook
) (if_rmap
);
167 if_rmap_unset (const char *ifname
, enum if_rmap_type type
,
168 const char *routemap_name
)
170 struct if_rmap
*if_rmap
;
172 if_rmap
= if_rmap_lookup (ifname
);
176 if (type
== IF_RMAP_IN
)
178 if (!if_rmap
->routemap
[IF_RMAP_IN
])
180 if (strcmp (if_rmap
->routemap
[IF_RMAP_IN
], routemap_name
) != 0)
183 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_IN
]);
184 if_rmap
->routemap
[IF_RMAP_IN
] = NULL
;
187 if (type
== IF_RMAP_OUT
)
189 if (!if_rmap
->routemap
[IF_RMAP_OUT
])
191 if (strcmp (if_rmap
->routemap
[IF_RMAP_OUT
], routemap_name
) != 0)
194 XFREE (MTYPE_IF_RMAP_NAME
, if_rmap
->routemap
[IF_RMAP_OUT
]);
195 if_rmap
->routemap
[IF_RMAP_OUT
] = NULL
;
198 if (if_rmap_delete_hook
)
199 (*if_rmap_delete_hook
) (if_rmap
);
201 if (if_rmap
->routemap
[IF_RMAP_IN
] == NULL
&&
202 if_rmap
->routemap
[IF_RMAP_OUT
] == NULL
)
204 hash_release (ifrmaphash
, if_rmap
);
205 if_rmap_free (if_rmap
);
213 "route-map RMAP_NAME <in|out> IFNAME",
216 "Route map set for input filtering\n"
217 "Route map set for output filtering\n"
218 "Route map interface name\n")
220 int idx_rmap_name
= 1;
223 enum if_rmap_type type
;
225 if (strncmp (argv
[idx_in_out
]->text
, "in", 1) == 0)
227 else if (strncmp (argv
[idx_in_out
]->text
, "out", 1) == 0)
231 vty_outln (vty
, "route-map direction must be [in|out]");
232 return CMD_WARNING_CONFIG_FAILED
;
235 if_rmap_set (argv
[idx_ifname
]->arg
, type
, argv
[idx_rmap_name
]->arg
);
242 "no route-map ROUTEMAP_NAME <in|out> IFNAME",
246 "Route map for input filtering\n"
247 "Route map for output filtering\n"
248 "Route map interface name\n")
250 int idx_routemap_name
= 2;
254 enum if_rmap_type type
;
256 if (strncmp (argv
[idx_in_out
]->arg
, "i", 1) == 0)
258 else if (strncmp (argv
[idx_in_out
]->arg
, "o", 1) == 0)
262 vty_outln (vty
, "route-map direction must be [in|out]");
263 return CMD_WARNING_CONFIG_FAILED
;
266 ret
= if_rmap_unset (argv
[idx_ifname
]->arg
, type
, argv
[idx_routemap_name
]->arg
);
269 vty_outln (vty
, "route-map doesn't exist");
270 return CMD_WARNING_CONFIG_FAILED
;
276 /* Configuration write function. */
278 config_write_if_rmap (struct vty
*vty
)
281 struct hash_backet
*mp
;
284 for (i
= 0; i
< ifrmaphash
->size
; i
++)
285 for (mp
= ifrmaphash
->index
[i
]; mp
; mp
= mp
->next
)
287 struct if_rmap
*if_rmap
;
291 if (if_rmap
->routemap
[IF_RMAP_IN
])
293 vty_outln (vty
, " route-map %s in %s",
294 if_rmap
->routemap
[IF_RMAP_IN
],
299 if (if_rmap
->routemap
[IF_RMAP_OUT
])
301 vty_outln (vty
, " route-map %s out %s",
302 if_rmap
->routemap
[IF_RMAP_OUT
],
313 hash_clean (ifrmaphash
, (void (*) (void *)) if_rmap_free
);
317 if_rmap_init (int node
)
319 ifrmaphash
= hash_create (if_rmap_hash_make
, if_rmap_hash_cmp
, NULL
);
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
);