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 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
30 #include "ripd/ripd.h"
32 #define RIP_OFFSET_LIST_IN 0
33 #define RIP_OFFSET_LIST_OUT 1
34 #define RIP_OFFSET_LIST_MAX 2
36 struct rip_offset_list
{
41 /* struct access_list *alist; */
43 } direct
[RIP_OFFSET_LIST_MAX
];
46 static struct list
*rip_offset_list_master
;
48 static int strcmp_safe(const char *s1
, const char *s2
)
50 if (s1
== NULL
&& s2
== NULL
)
56 return strcmp(s1
, s2
);
59 static struct rip_offset_list
*rip_offset_list_new(void)
61 return XCALLOC(MTYPE_RIP_OFFSET_LIST
, sizeof(struct rip_offset_list
));
64 static void rip_offset_list_free(struct rip_offset_list
*offset
)
66 XFREE(MTYPE_RIP_OFFSET_LIST
, offset
);
69 static struct rip_offset_list
*rip_offset_list_lookup(const char *ifname
)
71 struct rip_offset_list
*offset
;
72 struct listnode
*node
, *nnode
;
74 for (ALL_LIST_ELEMENTS(rip_offset_list_master
, node
, nnode
, offset
)) {
75 if (strcmp_safe(offset
->ifname
, ifname
) == 0)
81 static struct rip_offset_list
*rip_offset_list_get(const char *ifname
)
83 struct rip_offset_list
*offset
;
85 offset
= rip_offset_list_lookup(ifname
);
89 offset
= rip_offset_list_new();
91 offset
->ifname
= strdup(ifname
);
92 listnode_add_sort(rip_offset_list_master
, offset
);
97 static int rip_offset_list_set(struct vty
*vty
, const char *alist
,
98 const char *direct_str
, const char *metric_str
,
103 struct rip_offset_list
*offset
;
105 /* Check direction. */
106 if (strncmp(direct_str
, "i", 1) == 0)
107 direct
= RIP_OFFSET_LIST_IN
;
108 else if (strncmp(direct_str
, "o", 1) == 0)
109 direct
= RIP_OFFSET_LIST_OUT
;
111 vty_out(vty
, "Invalid direction: %s\n", direct_str
);
112 return CMD_WARNING_CONFIG_FAILED
;
116 metric
= atoi(metric_str
);
117 if (metric
< 0 || metric
> 16) {
118 vty_out(vty
, "Invalid metric: %s\n", metric_str
);
119 return CMD_WARNING_CONFIG_FAILED
;
122 /* Get offset-list structure with interface name. */
123 offset
= rip_offset_list_get(ifname
);
125 if (offset
->direct
[direct
].alist_name
)
126 free(offset
->direct
[direct
].alist_name
);
127 offset
->direct
[direct
].alist_name
= strdup(alist
);
128 offset
->direct
[direct
].metric
= metric
;
133 static int rip_offset_list_unset(struct vty
*vty
, const char *alist
,
134 const char *direct_str
, const char *metric_str
,
139 struct rip_offset_list
*offset
;
141 /* Check direction. */
142 if (strncmp(direct_str
, "i", 1) == 0)
143 direct
= RIP_OFFSET_LIST_IN
;
144 else if (strncmp(direct_str
, "o", 1) == 0)
145 direct
= RIP_OFFSET_LIST_OUT
;
147 vty_out(vty
, "Invalid direction: %s\n", direct_str
);
148 return CMD_WARNING_CONFIG_FAILED
;
152 metric
= atoi(metric_str
);
153 if (metric
< 0 || metric
> 16) {
154 vty_out(vty
, "Invalid metric: %s\n", metric_str
);
155 return CMD_WARNING_CONFIG_FAILED
;
158 /* Get offset-list structure with interface name. */
159 offset
= rip_offset_list_lookup(ifname
);
162 if (offset
->direct
[direct
].alist_name
)
163 free(offset
->direct
[direct
].alist_name
);
164 offset
->direct
[direct
].alist_name
= NULL
;
166 if (offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
== NULL
167 && offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
== NULL
) {
168 listnode_delete(rip_offset_list_master
, offset
);
170 free(offset
->ifname
);
171 rip_offset_list_free(offset
);
174 vty_out(vty
, "Can't find offset-list\n");
175 return CMD_WARNING_CONFIG_FAILED
;
180 #define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIP_OFFSET_LIST_IN].alist_name)
181 #define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_IN].metric)
183 #define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIP_OFFSET_LIST_OUT].alist_name)
184 #define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_OUT].metric)
186 /* If metric is modifed return 1. */
187 int rip_offset_list_apply_in(struct prefix_ipv4
*p
, struct interface
*ifp
,
190 struct rip_offset_list
*offset
;
191 struct access_list
*alist
;
193 /* Look up offset-list with interface name. */
194 offset
= rip_offset_list_lookup(ifp
->name
);
195 if (offset
&& OFFSET_LIST_IN_NAME(offset
)) {
196 alist
= access_list_lookup(AFI_IP
, OFFSET_LIST_IN_NAME(offset
));
199 && access_list_apply(alist
, (struct prefix
*)p
)
201 *metric
+= OFFSET_LIST_IN_METRIC(offset
);
206 /* Look up offset-list without interface name. */
207 offset
= rip_offset_list_lookup(NULL
);
208 if (offset
&& OFFSET_LIST_IN_NAME(offset
)) {
209 alist
= access_list_lookup(AFI_IP
, OFFSET_LIST_IN_NAME(offset
));
212 && access_list_apply(alist
, (struct prefix
*)p
)
214 *metric
+= OFFSET_LIST_IN_METRIC(offset
);
222 /* If metric is modifed return 1. */
223 int rip_offset_list_apply_out(struct prefix_ipv4
*p
, struct interface
*ifp
,
226 struct rip_offset_list
*offset
;
227 struct access_list
*alist
;
229 /* Look up offset-list with interface name. */
230 offset
= rip_offset_list_lookup(ifp
->name
);
231 if (offset
&& OFFSET_LIST_OUT_NAME(offset
)) {
232 alist
= access_list_lookup(AFI_IP
,
233 OFFSET_LIST_OUT_NAME(offset
));
236 && access_list_apply(alist
, (struct prefix
*)p
)
238 *metric
+= OFFSET_LIST_OUT_METRIC(offset
);
244 /* Look up offset-list without interface name. */
245 offset
= rip_offset_list_lookup(NULL
);
246 if (offset
&& OFFSET_LIST_OUT_NAME(offset
)) {
247 alist
= access_list_lookup(AFI_IP
,
248 OFFSET_LIST_OUT_NAME(offset
));
251 && access_list_apply(alist
, (struct prefix
*)p
)
253 *metric
+= OFFSET_LIST_OUT_METRIC(offset
);
261 DEFUN (rip_offset_list
,
263 "offset-list WORD <in|out> (0-16)",
264 "Modify RIP metric\n"
266 "For incoming updates\n"
267 "For outgoing updates\n"
273 return rip_offset_list_set(vty
, argv
[idx_word
]->arg
,
274 argv
[idx_in_out
]->arg
, argv
[idx_number
]->arg
,
278 DEFUN (rip_offset_list_ifname
,
279 rip_offset_list_ifname_cmd
,
280 "offset-list WORD <in|out> (0-16) IFNAME",
281 "Modify RIP metric\n"
283 "For incoming updates\n"
284 "For outgoing updates\n"
286 "Interface to match\n")
292 return rip_offset_list_set(vty
, argv
[idx_word
]->arg
,
293 argv
[idx_in_out
]->arg
, argv
[idx_number
]->arg
,
294 argv
[idx_ifname
]->arg
);
297 DEFUN (no_rip_offset_list
,
298 no_rip_offset_list_cmd
,
299 "no offset-list WORD <in|out> (0-16)",
301 "Modify RIP metric\n"
303 "For incoming updates\n"
304 "For outgoing updates\n"
310 return rip_offset_list_unset(vty
, argv
[idx_word
]->arg
,
311 argv
[idx_in_out
]->arg
,
312 argv
[idx_number
]->arg
, NULL
);
315 DEFUN (no_rip_offset_list_ifname
,
316 no_rip_offset_list_ifname_cmd
,
317 "no offset-list WORD <in|out> (0-16) IFNAME",
319 "Modify RIP metric\n"
321 "For incoming updates\n"
322 "For outgoing updates\n"
324 "Interface to match\n")
330 return rip_offset_list_unset(
331 vty
, argv
[idx_word
]->arg
, argv
[idx_in_out
]->arg
,
332 argv
[idx_number
]->arg
, argv
[idx_ifname
]->arg
);
335 static int offset_list_cmp(struct rip_offset_list
*o1
,
336 struct rip_offset_list
*o2
)
338 return strcmp_safe(o1
->ifname
, o2
->ifname
);
341 static void offset_list_del(struct rip_offset_list
*offset
)
343 if (OFFSET_LIST_IN_NAME(offset
))
344 free(OFFSET_LIST_IN_NAME(offset
));
345 if (OFFSET_LIST_OUT_NAME(offset
))
346 free(OFFSET_LIST_OUT_NAME(offset
));
348 free(offset
->ifname
);
349 rip_offset_list_free(offset
);
352 void rip_offset_init()
354 rip_offset_list_master
= list_new();
355 rip_offset_list_master
->cmp
= (int (*)(void *, void *))offset_list_cmp
;
356 rip_offset_list_master
->del
= (void (*)(void *))offset_list_del
;
358 install_element(RIP_NODE
, &rip_offset_list_cmd
);
359 install_element(RIP_NODE
, &rip_offset_list_ifname_cmd
);
360 install_element(RIP_NODE
, &no_rip_offset_list_cmd
);
361 install_element(RIP_NODE
, &no_rip_offset_list_ifname_cmd
);
364 void rip_offset_clean()
366 list_delete_and_null(&rip_offset_list_master
);
368 rip_offset_list_master
= list_new();
369 rip_offset_list_master
->cmp
= (int (*)(void *, void *))offset_list_cmp
;
370 rip_offset_list_master
->del
= (void (*)(void *))offset_list_del
;
373 int config_write_rip_offset_list(struct vty
*vty
)
375 struct listnode
*node
, *nnode
;
376 struct rip_offset_list
*offset
;
378 for (ALL_LIST_ELEMENTS(rip_offset_list_master
, node
, nnode
, offset
)) {
379 if (!offset
->ifname
) {
380 if (offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
)
381 vty_out(vty
, " offset-list %s in %d\n",
382 offset
->direct
[RIP_OFFSET_LIST_IN
]
384 offset
->direct
[RIP_OFFSET_LIST_IN
]
386 if (offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
)
387 vty_out(vty
, " offset-list %s out %d\n",
388 offset
->direct
[RIP_OFFSET_LIST_OUT
]
390 offset
->direct
[RIP_OFFSET_LIST_OUT
]
393 if (offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
)
394 vty_out(vty
, " offset-list %s in %d %s\n",
395 offset
->direct
[RIP_OFFSET_LIST_IN
]
397 offset
->direct
[RIP_OFFSET_LIST_IN
]
400 if (offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
)
401 vty_out(vty
, " offset-list %s out %d %s\n",
402 offset
->direct
[RIP_OFFSET_LIST_OUT
]
404 offset
->direct
[RIP_OFFSET_LIST_OUT
]