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
#define RIP_OFFSET_LIST_IN 0
32
#define RIP_OFFSET_LIST_OUT 1
33
#define RIP_OFFSET_LIST_MAX 2
35
struct rip_offset_list
42
/* struct access_list *alist; */
44
} direct[RIP_OFFSET_LIST_MAX];
47
static struct list *rip_offset_list_master;
50
strcmp_safe (const char *s1, const char *s2)
52
if (s1 == NULL && s2 == NULL)
58
return strcmp (s1, s2);
61
struct rip_offset_list *
62
rip_offset_list_new ()
64
struct rip_offset_list *new;
66
new = XMALLOC (MTYPE_RIP_OFFSET_LIST, sizeof (struct rip_offset_list));
67
memset (new, 0, sizeof (struct rip_offset_list));
72
rip_offset_list_free (struct rip_offset_list *offset)
74
XFREE (MTYPE_RIP_OFFSET_LIST, offset);
77
struct rip_offset_list *
78
rip_offset_list_lookup (const char *ifname)
80
struct rip_offset_list *offset;
81
struct listnode *node, *nnode;
83
for (ALL_LIST_ELEMENTS (rip_offset_list_master, node, nnode, offset))
85
if (strcmp_safe (offset->ifname, ifname) == 0)
91
struct rip_offset_list *
92
rip_offset_list_get (const char *ifname)
94
struct rip_offset_list *offset;
96
offset = rip_offset_list_lookup (ifname);
100
offset = rip_offset_list_new ();
102
offset->ifname = strdup (ifname);
103
listnode_add_sort (rip_offset_list_master, offset);
109
rip_offset_list_set (struct vty *vty, const char *alist, const char *direct_str,
110
const char *metric_str, const char *ifname)
114
struct rip_offset_list *offset;
116
/* Check direction. */
117
if (strncmp (direct_str, "i", 1) == 0)
118
direct = RIP_OFFSET_LIST_IN;
119
else if (strncmp (direct_str, "o", 1) == 0)
120
direct = RIP_OFFSET_LIST_OUT;
123
vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
128
metric = atoi (metric_str);
129
if (metric < 0 || metric > 16)
131
vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
135
/* Get offset-list structure with interface name. */
136
offset = rip_offset_list_get (ifname);
138
if (offset->direct[direct].alist_name)
139
free (offset->direct[direct].alist_name);
140
offset->direct[direct].alist_name = strdup (alist);
141
offset->direct[direct].metric = metric;
147
rip_offset_list_unset (struct vty *vty, const char *alist,
148
const char *direct_str, const char *metric_str,
153
struct rip_offset_list *offset;
155
/* Check direction. */
156
if (strncmp (direct_str, "i", 1) == 0)
157
direct = RIP_OFFSET_LIST_IN;
158
else if (strncmp (direct_str, "o", 1) == 0)
159
direct = RIP_OFFSET_LIST_OUT;
162
vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
167
metric = atoi (metric_str);
168
if (metric < 0 || metric > 16)
170
vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
174
/* Get offset-list structure with interface name. */
175
offset = rip_offset_list_lookup (ifname);
179
if (offset->direct[direct].alist_name)
180
free (offset->direct[direct].alist_name);
181
offset->direct[direct].alist_name = NULL;
183
if (offset->direct[RIP_OFFSET_LIST_IN].alist_name == NULL &&
184
offset->direct[RIP_OFFSET_LIST_OUT].alist_name == NULL)
186
listnode_delete (rip_offset_list_master, offset);
188
free (offset->ifname);
189
rip_offset_list_free (offset);
194
vty_out (vty, "Can't find offset-list%s", VTY_NEWLINE);
200
#define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIP_OFFSET_LIST_IN].alist_name)
201
#define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_IN].metric)
203
#define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIP_OFFSET_LIST_OUT].alist_name)
204
#define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_OUT].metric)
206
/* If metric is modifed return 1. */
208
rip_offset_list_apply_in (struct prefix_ipv4 *p, struct interface *ifp,
211
struct rip_offset_list *offset;
212
struct access_list *alist;
214
/* Look up offset-list with interface name. */
215
offset = rip_offset_list_lookup (ifp->name);
216
if (offset && OFFSET_LIST_IN_NAME (offset))
218
alist = access_list_lookup (AFI_IP, OFFSET_LIST_IN_NAME (offset));
221
&& access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
223
*metric += OFFSET_LIST_IN_METRIC (offset);
228
/* Look up offset-list without interface name. */
229
offset = rip_offset_list_lookup (NULL);
230
if (offset && OFFSET_LIST_IN_NAME (offset))
232
alist = access_list_lookup (AFI_IP, OFFSET_LIST_IN_NAME (offset));
235
&& access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
237
*metric += OFFSET_LIST_IN_METRIC (offset);
245
/* If metric is modifed return 1. */
247
rip_offset_list_apply_out (struct prefix_ipv4 *p, struct interface *ifp,
250
struct rip_offset_list *offset;
251
struct access_list *alist;
253
/* Look up offset-list with interface name. */
254
offset = rip_offset_list_lookup (ifp->name);
255
if (offset && OFFSET_LIST_OUT_NAME (offset))
257
alist = access_list_lookup (AFI_IP, OFFSET_LIST_OUT_NAME (offset));
260
&& access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
262
*metric += OFFSET_LIST_OUT_METRIC (offset);
268
/* Look up offset-list without interface name. */
269
offset = rip_offset_list_lookup (NULL);
270
if (offset && OFFSET_LIST_OUT_NAME (offset))
272
alist = access_list_lookup (AFI_IP, OFFSET_LIST_OUT_NAME (offset));
275
&& access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
277
*metric += OFFSET_LIST_OUT_METRIC (offset);
285
DEFUN (rip_offset_list,
287
"offset-list WORD (in|out) <0-16>",
288
"Modify RIP metric\n"
290
"For incoming updates\n"
291
"For outgoing updates\n"
294
return rip_offset_list_set (vty, argv[0], argv[1], argv[2], NULL);
297
DEFUN (rip_offset_list_ifname,
298
rip_offset_list_ifname_cmd,
299
"offset-list WORD (in|out) <0-16> IFNAME",
300
"Modify RIP metric\n"
302
"For incoming updates\n"
303
"For outgoing updates\n"
305
"Interface to match\n")
307
return rip_offset_list_set (vty, argv[0], argv[1], argv[2], argv[3]);
310
DEFUN (no_rip_offset_list,
311
no_rip_offset_list_cmd,
312
"no offset-list WORD (in|out) <0-16>",
314
"Modify RIP metric\n"
316
"For incoming updates\n"
317
"For outgoing updates\n"
320
return rip_offset_list_unset (vty, argv[0], argv[1], argv[2], NULL);
323
DEFUN (no_rip_offset_list_ifname,
324
no_rip_offset_list_ifname_cmd,
325
"no offset-list WORD (in|out) <0-16> IFNAME",
327
"Modify RIP metric\n"
329
"For incoming updates\n"
330
"For outgoing updates\n"
332
"Interface to match\n")
334
return rip_offset_list_unset (vty, argv[0], argv[1], argv[2], argv[3]);
338
offset_list_cmp (struct rip_offset_list *o1, struct rip_offset_list *o2)
340
return strcmp_safe (o1->ifname, o2->ifname);
344
offset_list_del (struct rip_offset_list *offset)
346
if (OFFSET_LIST_IN_NAME (offset))
347
free (OFFSET_LIST_IN_NAME (offset));
348
if (OFFSET_LIST_OUT_NAME (offset))
349
free (OFFSET_LIST_OUT_NAME (offset));
351
free (offset->ifname);
352
rip_offset_list_free (offset);
358
rip_offset_list_master = list_new ();
359
rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
360
rip_offset_list_master->del = (void (*)(void *)) offset_list_del;
362
install_element (RIP_NODE, &rip_offset_list_cmd);
363
install_element (RIP_NODE, &rip_offset_list_ifname_cmd);
364
install_element (RIP_NODE, &no_rip_offset_list_cmd);
365
install_element (RIP_NODE, &no_rip_offset_list_ifname_cmd);
371
list_delete (rip_offset_list_master);
373
rip_offset_list_master = list_new ();
374
rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
375
rip_offset_list_master->del = (void (*)(void *)) offset_list_del;
379
config_write_rip_offset_list (struct vty *vty)
381
struct listnode *node, *nnode;
382
struct rip_offset_list *offset;
384
for (ALL_LIST_ELEMENTS (rip_offset_list_master, node, nnode, offset))
386
if (! offset->ifname)
388
if (offset->direct[RIP_OFFSET_LIST_IN].alist_name)
389
vty_out (vty, " offset-list %s in %d%s",
390
offset->direct[RIP_OFFSET_LIST_IN].alist_name,
391
offset->direct[RIP_OFFSET_LIST_IN].metric,
393
if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name)
394
vty_out (vty, " offset-list %s out %d%s",
395
offset->direct[RIP_OFFSET_LIST_OUT].alist_name,
396
offset->direct[RIP_OFFSET_LIST_OUT].metric,
401
if (offset->direct[RIP_OFFSET_LIST_IN].alist_name)
402
vty_out (vty, " offset-list %s in %d %s%s",
403
offset->direct[RIP_OFFSET_LIST_IN].alist_name,
404
offset->direct[RIP_OFFSET_LIST_IN].metric,
405
offset->ifname, VTY_NEWLINE);
406
if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name)
407
vty_out (vty, " offset-list %s out %d %s%s",
408
offset->direct[RIP_OFFSET_LIST_OUT].alist_name,
409
offset->direct[RIP_OFFSET_LIST_OUT].metric,
410
offset->ifname, VTY_NEWLINE);