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 struct rip_offset_list
*new;
68 new = XMALLOC (MTYPE_RIP_OFFSET_LIST
, sizeof (struct rip_offset_list
));
69 memset (new, 0, sizeof (struct rip_offset_list
));
74 rip_offset_list_free (struct rip_offset_list
*offset
)
76 XFREE (MTYPE_RIP_OFFSET_LIST
, offset
);
79 static struct rip_offset_list
*
80 rip_offset_list_lookup (const char *ifname
)
82 struct rip_offset_list
*offset
;
83 struct listnode
*node
, *nnode
;
85 for (ALL_LIST_ELEMENTS (rip_offset_list_master
, node
, nnode
, offset
))
87 if (strcmp_safe (offset
->ifname
, ifname
) == 0)
93 static struct rip_offset_list
*
94 rip_offset_list_get (const char *ifname
)
96 struct rip_offset_list
*offset
;
98 offset
= rip_offset_list_lookup (ifname
);
102 offset
= rip_offset_list_new ();
104 offset
->ifname
= strdup (ifname
);
105 listnode_add_sort (rip_offset_list_master
, offset
);
111 rip_offset_list_set (struct vty
*vty
, const char *alist
, const char *direct_str
,
112 const char *metric_str
, const char *ifname
)
116 struct rip_offset_list
*offset
;
118 /* Check direction. */
119 if (strncmp (direct_str
, "i", 1) == 0)
120 direct
= RIP_OFFSET_LIST_IN
;
121 else if (strncmp (direct_str
, "o", 1) == 0)
122 direct
= RIP_OFFSET_LIST_OUT
;
125 vty_out (vty
, "Invalid direction: %s%s", direct_str
, VTY_NEWLINE
);
130 metric
= atoi (metric_str
);
131 if (metric
< 0 || metric
> 16)
133 vty_out (vty
, "Invalid metric: %s%s", metric_str
, VTY_NEWLINE
);
137 /* Get offset-list structure with interface name. */
138 offset
= rip_offset_list_get (ifname
);
140 if (offset
->direct
[direct
].alist_name
)
141 free (offset
->direct
[direct
].alist_name
);
142 offset
->direct
[direct
].alist_name
= strdup (alist
);
143 offset
->direct
[direct
].metric
= metric
;
149 rip_offset_list_unset (struct vty
*vty
, const char *alist
,
150 const char *direct_str
, const char *metric_str
,
155 struct rip_offset_list
*offset
;
157 /* Check direction. */
158 if (strncmp (direct_str
, "i", 1) == 0)
159 direct
= RIP_OFFSET_LIST_IN
;
160 else if (strncmp (direct_str
, "o", 1) == 0)
161 direct
= RIP_OFFSET_LIST_OUT
;
164 vty_out (vty
, "Invalid direction: %s%s", direct_str
, VTY_NEWLINE
);
169 metric
= atoi (metric_str
);
170 if (metric
< 0 || metric
> 16)
172 vty_out (vty
, "Invalid metric: %s%s", metric_str
, VTY_NEWLINE
);
176 /* Get offset-list structure with interface name. */
177 offset
= rip_offset_list_lookup (ifname
);
181 if (offset
->direct
[direct
].alist_name
)
182 free (offset
->direct
[direct
].alist_name
);
183 offset
->direct
[direct
].alist_name
= NULL
;
185 if (offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
== NULL
&&
186 offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
== NULL
)
188 listnode_delete (rip_offset_list_master
, offset
);
190 free (offset
->ifname
);
191 rip_offset_list_free (offset
);
196 vty_out (vty
, "Can't find offset-list%s", VTY_NEWLINE
);
202 #define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIP_OFFSET_LIST_IN].alist_name)
203 #define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_IN].metric)
205 #define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIP_OFFSET_LIST_OUT].alist_name)
206 #define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_OUT].metric)
208 /* If metric is modifed return 1. */
210 rip_offset_list_apply_in (struct prefix_ipv4
*p
, struct interface
*ifp
,
213 struct rip_offset_list
*offset
;
214 struct access_list
*alist
;
216 /* Look up offset-list with interface name. */
217 offset
= rip_offset_list_lookup (ifp
->name
);
218 if (offset
&& OFFSET_LIST_IN_NAME (offset
))
220 alist
= access_list_lookup (AFI_IP
, OFFSET_LIST_IN_NAME (offset
));
223 && access_list_apply (alist
, (struct prefix
*)p
) == FILTER_PERMIT
)
225 *metric
+= OFFSET_LIST_IN_METRIC (offset
);
230 /* Look up offset-list without interface name. */
231 offset
= rip_offset_list_lookup (NULL
);
232 if (offset
&& OFFSET_LIST_IN_NAME (offset
))
234 alist
= access_list_lookup (AFI_IP
, OFFSET_LIST_IN_NAME (offset
));
237 && access_list_apply (alist
, (struct prefix
*)p
) == FILTER_PERMIT
)
239 *metric
+= OFFSET_LIST_IN_METRIC (offset
);
247 /* If metric is modifed return 1. */
249 rip_offset_list_apply_out (struct prefix_ipv4
*p
, struct interface
*ifp
,
252 struct rip_offset_list
*offset
;
253 struct access_list
*alist
;
255 /* Look up offset-list with interface name. */
256 offset
= rip_offset_list_lookup (ifp
->name
);
257 if (offset
&& OFFSET_LIST_OUT_NAME (offset
))
259 alist
= access_list_lookup (AFI_IP
, OFFSET_LIST_OUT_NAME (offset
));
262 && access_list_apply (alist
, (struct prefix
*)p
) == FILTER_PERMIT
)
264 *metric
+= OFFSET_LIST_OUT_METRIC (offset
);
270 /* Look up offset-list without interface name. */
271 offset
= rip_offset_list_lookup (NULL
);
272 if (offset
&& OFFSET_LIST_OUT_NAME (offset
))
274 alist
= access_list_lookup (AFI_IP
, OFFSET_LIST_OUT_NAME (offset
));
277 && access_list_apply (alist
, (struct prefix
*)p
) == FILTER_PERMIT
)
279 *metric
+= OFFSET_LIST_OUT_METRIC (offset
);
287 DEFUN (rip_offset_list
,
289 "offset-list WORD (in|out) <0-16>",
290 "Modify RIP metric\n"
292 "For incoming updates\n"
293 "For outgoing updates\n"
296 return rip_offset_list_set (vty
, argv
[0], argv
[1], argv
[2], NULL
);
299 DEFUN (rip_offset_list_ifname
,
300 rip_offset_list_ifname_cmd
,
301 "offset-list WORD (in|out) <0-16> IFNAME",
302 "Modify RIP metric\n"
304 "For incoming updates\n"
305 "For outgoing updates\n"
307 "Interface to match\n")
309 return rip_offset_list_set (vty
, argv
[0], argv
[1], argv
[2], argv
[3]);
312 DEFUN (no_rip_offset_list
,
313 no_rip_offset_list_cmd
,
314 "no offset-list WORD (in|out) <0-16>",
316 "Modify RIP metric\n"
318 "For incoming updates\n"
319 "For outgoing updates\n"
322 return rip_offset_list_unset (vty
, argv
[0], argv
[1], argv
[2], NULL
);
325 DEFUN (no_rip_offset_list_ifname
,
326 no_rip_offset_list_ifname_cmd
,
327 "no offset-list WORD (in|out) <0-16> IFNAME",
329 "Modify RIP metric\n"
331 "For incoming updates\n"
332 "For outgoing updates\n"
334 "Interface to match\n")
336 return rip_offset_list_unset (vty
, argv
[0], argv
[1], argv
[2], argv
[3]);
340 offset_list_cmp (struct rip_offset_list
*o1
, struct rip_offset_list
*o2
)
342 return strcmp_safe (o1
->ifname
, o2
->ifname
);
346 offset_list_del (struct rip_offset_list
*offset
)
348 if (OFFSET_LIST_IN_NAME (offset
))
349 free (OFFSET_LIST_IN_NAME (offset
));
350 if (OFFSET_LIST_OUT_NAME (offset
))
351 free (OFFSET_LIST_OUT_NAME (offset
));
353 free (offset
->ifname
);
354 rip_offset_list_free (offset
);
360 rip_offset_list_master
= list_new ();
361 rip_offset_list_master
->cmp
= (int (*)(void *, void *)) offset_list_cmp
;
362 rip_offset_list_master
->del
= (void (*)(void *)) offset_list_del
;
364 install_element (RIP_NODE
, &rip_offset_list_cmd
);
365 install_element (RIP_NODE
, &rip_offset_list_ifname_cmd
);
366 install_element (RIP_NODE
, &no_rip_offset_list_cmd
);
367 install_element (RIP_NODE
, &no_rip_offset_list_ifname_cmd
);
373 list_delete (rip_offset_list_master
);
375 rip_offset_list_master
= list_new ();
376 rip_offset_list_master
->cmp
= (int (*)(void *, void *)) offset_list_cmp
;
377 rip_offset_list_master
->del
= (void (*)(void *)) offset_list_del
;
381 config_write_rip_offset_list (struct vty
*vty
)
383 struct listnode
*node
, *nnode
;
384 struct rip_offset_list
*offset
;
386 for (ALL_LIST_ELEMENTS (rip_offset_list_master
, node
, nnode
, offset
))
388 if (! offset
->ifname
)
390 if (offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
)
391 vty_out (vty
, " offset-list %s in %d%s",
392 offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
,
393 offset
->direct
[RIP_OFFSET_LIST_IN
].metric
,
395 if (offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
)
396 vty_out (vty
, " offset-list %s out %d%s",
397 offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
,
398 offset
->direct
[RIP_OFFSET_LIST_OUT
].metric
,
403 if (offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
)
404 vty_out (vty
, " offset-list %s in %d %s%s",
405 offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
,
406 offset
->direct
[RIP_OFFSET_LIST_IN
].metric
,
407 offset
->ifname
, VTY_NEWLINE
);
408 if (offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
)
409 vty_out (vty
, " offset-list %s out %d %s%s",
410 offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
,
411 offset
->direct
[RIP_OFFSET_LIST_OUT
].metric
,
412 offset
->ifname
, VTY_NEWLINE
);