2 * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
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
31 #include "ripd/ripd.h"
33 #define RIP_OFFSET_LIST_IN 0
34 #define RIP_OFFSET_LIST_OUT 1
35 #define RIP_OFFSET_LIST_MAX 2
37 struct rip_offset_list
44 /* struct access_list *alist; */
46 } direct
[RIP_OFFSET_LIST_MAX
];
49 static struct list
*rip_offset_list_master
;
52 strcmp_safe (const char *s1
, const char *s2
)
54 if (s1
== NULL
&& s2
== NULL
)
60 return strcmp (s1
, s2
);
63 static struct rip_offset_list
*
64 rip_offset_list_new (void)
66 return XCALLOC (MTYPE_RIP_OFFSET_LIST
, sizeof (struct rip_offset_list
));
70 rip_offset_list_free (struct rip_offset_list
*offset
)
72 XFREE (MTYPE_RIP_OFFSET_LIST
, offset
);
75 static struct rip_offset_list
*
76 rip_offset_list_lookup (const char *ifname
)
78 struct rip_offset_list
*offset
;
79 struct listnode
*node
, *nnode
;
81 for (ALL_LIST_ELEMENTS (rip_offset_list_master
, node
, nnode
, offset
))
83 if (strcmp_safe (offset
->ifname
, ifname
) == 0)
89 static struct rip_offset_list
*
90 rip_offset_list_get (const char *ifname
)
92 struct rip_offset_list
*offset
;
94 offset
= rip_offset_list_lookup (ifname
);
98 offset
= rip_offset_list_new ();
100 offset
->ifname
= strdup (ifname
);
101 listnode_add_sort (rip_offset_list_master
, offset
);
107 rip_offset_list_set (struct vty
*vty
, const char *alist
, const char *direct_str
,
108 const char *metric_str
, const char *ifname
)
112 struct rip_offset_list
*offset
;
114 /* Check direction. */
115 if (strncmp (direct_str
, "i", 1) == 0)
116 direct
= RIP_OFFSET_LIST_IN
;
117 else if (strncmp (direct_str
, "o", 1) == 0)
118 direct
= RIP_OFFSET_LIST_OUT
;
121 vty_out (vty
, "Invalid direction: %s%s", direct_str
, VTY_NEWLINE
);
126 metric
= atoi (metric_str
);
127 if (metric
< 0 || metric
> 16)
129 vty_out (vty
, "Invalid metric: %s%s", metric_str
, VTY_NEWLINE
);
133 /* Get offset-list structure with interface name. */
134 offset
= rip_offset_list_get (ifname
);
136 if (offset
->direct
[direct
].alist_name
)
137 free (offset
->direct
[direct
].alist_name
);
138 offset
->direct
[direct
].alist_name
= strdup (alist
);
139 offset
->direct
[direct
].metric
= metric
;
145 rip_offset_list_unset (struct vty
*vty
, const char *alist
,
146 const char *direct_str
, const char *metric_str
,
151 struct rip_offset_list
*offset
;
153 /* Check direction. */
154 if (strncmp (direct_str
, "i", 1) == 0)
155 direct
= RIP_OFFSET_LIST_IN
;
156 else if (strncmp (direct_str
, "o", 1) == 0)
157 direct
= RIP_OFFSET_LIST_OUT
;
160 vty_out (vty
, "Invalid direction: %s%s", direct_str
, VTY_NEWLINE
);
165 metric
= atoi (metric_str
);
166 if (metric
< 0 || metric
> 16)
168 vty_out (vty
, "Invalid metric: %s%s", metric_str
, VTY_NEWLINE
);
172 /* Get offset-list structure with interface name. */
173 offset
= rip_offset_list_lookup (ifname
);
177 if (offset
->direct
[direct
].alist_name
)
178 free (offset
->direct
[direct
].alist_name
);
179 offset
->direct
[direct
].alist_name
= NULL
;
181 if (offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
== NULL
&&
182 offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
== NULL
)
184 listnode_delete (rip_offset_list_master
, offset
);
186 free (offset
->ifname
);
187 rip_offset_list_free (offset
);
192 vty_out (vty
, "Can't find offset-list%s", VTY_NEWLINE
);
198 #define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIP_OFFSET_LIST_IN].alist_name)
199 #define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_IN].metric)
201 #define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIP_OFFSET_LIST_OUT].alist_name)
202 #define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_OUT].metric)
204 /* If metric is modifed return 1. */
206 rip_offset_list_apply_in (struct prefix_ipv4
*p
, struct interface
*ifp
,
209 struct rip_offset_list
*offset
;
210 struct access_list
*alist
;
212 /* Look up offset-list with interface name. */
213 offset
= rip_offset_list_lookup (ifp
->name
);
214 if (offset
&& OFFSET_LIST_IN_NAME (offset
))
216 alist
= access_list_lookup (AFI_IP
, OFFSET_LIST_IN_NAME (offset
));
219 && access_list_apply (alist
, (struct prefix
*)p
) == FILTER_PERMIT
)
221 *metric
+= OFFSET_LIST_IN_METRIC (offset
);
226 /* Look up offset-list without interface name. */
227 offset
= rip_offset_list_lookup (NULL
);
228 if (offset
&& OFFSET_LIST_IN_NAME (offset
))
230 alist
= access_list_lookup (AFI_IP
, OFFSET_LIST_IN_NAME (offset
));
233 && access_list_apply (alist
, (struct prefix
*)p
) == FILTER_PERMIT
)
235 *metric
+= OFFSET_LIST_IN_METRIC (offset
);
243 /* If metric is modifed return 1. */
245 rip_offset_list_apply_out (struct prefix_ipv4
*p
, struct interface
*ifp
,
248 struct rip_offset_list
*offset
;
249 struct access_list
*alist
;
251 /* Look up offset-list with interface name. */
252 offset
= rip_offset_list_lookup (ifp
->name
);
253 if (offset
&& OFFSET_LIST_OUT_NAME (offset
))
255 alist
= access_list_lookup (AFI_IP
, OFFSET_LIST_OUT_NAME (offset
));
258 && access_list_apply (alist
, (struct prefix
*)p
) == FILTER_PERMIT
)
260 *metric
+= OFFSET_LIST_OUT_METRIC (offset
);
266 /* Look up offset-list without interface name. */
267 offset
= rip_offset_list_lookup (NULL
);
268 if (offset
&& OFFSET_LIST_OUT_NAME (offset
))
270 alist
= access_list_lookup (AFI_IP
, OFFSET_LIST_OUT_NAME (offset
));
273 && access_list_apply (alist
, (struct prefix
*)p
) == FILTER_PERMIT
)
275 *metric
+= OFFSET_LIST_OUT_METRIC (offset
);
283 DEFUN (rip_offset_list
,
285 "offset-list WORD (in|out) <0-16>",
286 "Modify RIP metric\n"
288 "For incoming updates\n"
289 "For outgoing updates\n"
292 return rip_offset_list_set (vty
, argv
[0], argv
[1], argv
[2], NULL
);
295 DEFUN (rip_offset_list_ifname
,
296 rip_offset_list_ifname_cmd
,
297 "offset-list WORD (in|out) <0-16> IFNAME",
298 "Modify RIP metric\n"
300 "For incoming updates\n"
301 "For outgoing updates\n"
303 "Interface to match\n")
305 return rip_offset_list_set (vty
, argv
[0], argv
[1], argv
[2], argv
[3]);
308 DEFUN (no_rip_offset_list
,
309 no_rip_offset_list_cmd
,
310 "no offset-list WORD (in|out) <0-16>",
312 "Modify RIP metric\n"
314 "For incoming updates\n"
315 "For outgoing updates\n"
318 return rip_offset_list_unset (vty
, argv
[0], argv
[1], argv
[2], NULL
);
321 DEFUN (no_rip_offset_list_ifname
,
322 no_rip_offset_list_ifname_cmd
,
323 "no offset-list WORD (in|out) <0-16> IFNAME",
325 "Modify RIP metric\n"
327 "For incoming updates\n"
328 "For outgoing updates\n"
330 "Interface to match\n")
332 return rip_offset_list_unset (vty
, argv
[0], argv
[1], argv
[2], argv
[3]);
336 offset_list_cmp (struct rip_offset_list
*o1
, struct rip_offset_list
*o2
)
338 return strcmp_safe (o1
->ifname
, o2
->ifname
);
342 offset_list_del (struct rip_offset_list
*offset
)
344 if (OFFSET_LIST_IN_NAME (offset
))
345 free (OFFSET_LIST_IN_NAME (offset
));
346 if (OFFSET_LIST_OUT_NAME (offset
))
347 free (OFFSET_LIST_OUT_NAME (offset
));
349 free (offset
->ifname
);
350 rip_offset_list_free (offset
);
356 rip_offset_list_master
= list_new ();
357 rip_offset_list_master
->cmp
= (int (*)(void *, void *)) offset_list_cmp
;
358 rip_offset_list_master
->del
= (void (*)(void *)) offset_list_del
;
360 install_element (RIP_NODE
, &rip_offset_list_cmd
);
361 install_element (RIP_NODE
, &rip_offset_list_ifname_cmd
);
362 install_element (RIP_NODE
, &no_rip_offset_list_cmd
);
363 install_element (RIP_NODE
, &no_rip_offset_list_ifname_cmd
);
369 list_delete (rip_offset_list_master
);
371 rip_offset_list_master
= list_new ();
372 rip_offset_list_master
->cmp
= (int (*)(void *, void *)) offset_list_cmp
;
373 rip_offset_list_master
->del
= (void (*)(void *)) offset_list_del
;
377 config_write_rip_offset_list (struct vty
*vty
)
379 struct listnode
*node
, *nnode
;
380 struct rip_offset_list
*offset
;
382 for (ALL_LIST_ELEMENTS (rip_offset_list_master
, node
, nnode
, offset
))
384 if (! offset
->ifname
)
386 if (offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
)
387 vty_out (vty
, " offset-list %s in %d%s",
388 offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
,
389 offset
->direct
[RIP_OFFSET_LIST_IN
].metric
,
391 if (offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
)
392 vty_out (vty
, " offset-list %s out %d%s",
393 offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
,
394 offset
->direct
[RIP_OFFSET_LIST_OUT
].metric
,
399 if (offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
)
400 vty_out (vty
, " offset-list %s in %d %s%s",
401 offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
,
402 offset
->direct
[RIP_OFFSET_LIST_IN
].metric
,
403 offset
->ifname
, VTY_NEWLINE
);
404 if (offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
)
405 vty_out (vty
, " offset-list %s out %d %s%s",
406 offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
,
407 offset
->direct
[RIP_OFFSET_LIST_OUT
].metric
,
408 offset
->ifname
, VTY_NEWLINE
);