]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_offset.c
Merge pull request #3113 from donaldsharp/uninited_value
[mirror_frr.git] / ripd / rip_offset.c
1 /* RIP offset-list
2 * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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
19 */
20
21 #include <zebra.h>
22
23 #include "if.h"
24 #include "prefix.h"
25 #include "filter.h"
26 #include "command.h"
27 #include "linklist.h"
28 #include "memory.h"
29
30 #include "ripd/ripd.h"
31
32 #define RIP_OFFSET_LIST_IN 0
33 #define RIP_OFFSET_LIST_OUT 1
34 #define RIP_OFFSET_LIST_MAX 2
35
36 struct rip_offset_list {
37 char *ifname;
38
39 struct {
40 char *alist_name;
41 /* struct access_list *alist; */
42 int metric;
43 } direct[RIP_OFFSET_LIST_MAX];
44 };
45
46 static struct list *rip_offset_list_master;
47
48 static int strcmp_safe(const char *s1, const char *s2)
49 {
50 if (s1 == NULL && s2 == NULL)
51 return 0;
52 if (s1 == NULL)
53 return -1;
54 if (s2 == NULL)
55 return 1;
56 return strcmp(s1, s2);
57 }
58
59 static struct rip_offset_list *rip_offset_list_new(void)
60 {
61 return XCALLOC(MTYPE_RIP_OFFSET_LIST, sizeof(struct rip_offset_list));
62 }
63
64 static void rip_offset_list_free(struct rip_offset_list *offset)
65 {
66 XFREE(MTYPE_RIP_OFFSET_LIST, offset);
67 }
68
69 static struct rip_offset_list *rip_offset_list_lookup(const char *ifname)
70 {
71 struct rip_offset_list *offset;
72 struct listnode *node, *nnode;
73
74 for (ALL_LIST_ELEMENTS(rip_offset_list_master, node, nnode, offset)) {
75 if (strcmp_safe(offset->ifname, ifname) == 0)
76 return offset;
77 }
78 return NULL;
79 }
80
81 static struct rip_offset_list *rip_offset_list_get(const char *ifname)
82 {
83 struct rip_offset_list *offset;
84
85 offset = rip_offset_list_lookup(ifname);
86 if (offset)
87 return offset;
88
89 offset = rip_offset_list_new();
90 if (ifname)
91 offset->ifname = strdup(ifname);
92 listnode_add_sort(rip_offset_list_master, offset);
93
94 return offset;
95 }
96
97 static int rip_offset_list_set(struct vty *vty, const char *alist,
98 const char *direct_str, const char *metric_str,
99 const char *ifname)
100 {
101 int direct;
102 int metric;
103 struct rip_offset_list *offset;
104
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;
110 else {
111 vty_out(vty, "Invalid direction: %s\n", direct_str);
112 return CMD_WARNING_CONFIG_FAILED;
113 }
114
115 /* Check metric. */
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;
120 }
121
122 /* Get offset-list structure with interface name. */
123 offset = rip_offset_list_get(ifname);
124
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;
129
130 return CMD_SUCCESS;
131 }
132
133 static int rip_offset_list_unset(struct vty *vty, const char *alist,
134 const char *direct_str, const char *metric_str,
135 const char *ifname)
136 {
137 int direct;
138 int metric;
139 struct rip_offset_list *offset;
140
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;
146 else {
147 vty_out(vty, "Invalid direction: %s\n", direct_str);
148 return CMD_WARNING_CONFIG_FAILED;
149 }
150
151 /* Check metric. */
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;
156 }
157
158 /* Get offset-list structure with interface name. */
159 offset = rip_offset_list_lookup(ifname);
160
161 if (offset) {
162 if (offset->direct[direct].alist_name)
163 free(offset->direct[direct].alist_name);
164 offset->direct[direct].alist_name = NULL;
165
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);
169 if (offset->ifname)
170 free(offset->ifname);
171 rip_offset_list_free(offset);
172 }
173 } else {
174 vty_out(vty, "Can't find offset-list\n");
175 return CMD_WARNING_CONFIG_FAILED;
176 }
177 return CMD_SUCCESS;
178 }
179
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)
182
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)
185
186 /* If metric is modifed return 1. */
187 int rip_offset_list_apply_in(struct prefix_ipv4 *p, struct interface *ifp,
188 uint32_t *metric)
189 {
190 struct rip_offset_list *offset;
191 struct access_list *alist;
192
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));
197
198 if (alist
199 && access_list_apply(alist, (struct prefix *)p)
200 == FILTER_PERMIT) {
201 *metric += OFFSET_LIST_IN_METRIC(offset);
202 return 1;
203 }
204 return 0;
205 }
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));
210
211 if (alist
212 && access_list_apply(alist, (struct prefix *)p)
213 == FILTER_PERMIT) {
214 *metric += OFFSET_LIST_IN_METRIC(offset);
215 return 1;
216 }
217 return 0;
218 }
219 return 0;
220 }
221
222 /* If metric is modifed return 1. */
223 int rip_offset_list_apply_out(struct prefix_ipv4 *p, struct interface *ifp,
224 uint32_t *metric)
225 {
226 struct rip_offset_list *offset;
227 struct access_list *alist;
228
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));
234
235 if (alist
236 && access_list_apply(alist, (struct prefix *)p)
237 == FILTER_PERMIT) {
238 *metric += OFFSET_LIST_OUT_METRIC(offset);
239 return 1;
240 }
241 return 0;
242 }
243
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));
249
250 if (alist
251 && access_list_apply(alist, (struct prefix *)p)
252 == FILTER_PERMIT) {
253 *metric += OFFSET_LIST_OUT_METRIC(offset);
254 return 1;
255 }
256 return 0;
257 }
258 return 0;
259 }
260
261 DEFUN (rip_offset_list,
262 rip_offset_list_cmd,
263 "offset-list WORD <in|out> (0-16)",
264 "Modify RIP metric\n"
265 "Access-list name\n"
266 "For incoming updates\n"
267 "For outgoing updates\n"
268 "Metric value\n")
269 {
270 int idx_word = 1;
271 int idx_in_out = 2;
272 int idx_number = 3;
273 return rip_offset_list_set(vty, argv[idx_word]->arg,
274 argv[idx_in_out]->arg, argv[idx_number]->arg,
275 NULL);
276 }
277
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"
282 "Access-list name\n"
283 "For incoming updates\n"
284 "For outgoing updates\n"
285 "Metric value\n"
286 "Interface to match\n")
287 {
288 int idx_word = 1;
289 int idx_in_out = 2;
290 int idx_number = 3;
291 int idx_ifname = 4;
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);
295 }
296
297 DEFUN (no_rip_offset_list,
298 no_rip_offset_list_cmd,
299 "no offset-list WORD <in|out> (0-16)",
300 NO_STR
301 "Modify RIP metric\n"
302 "Access-list name\n"
303 "For incoming updates\n"
304 "For outgoing updates\n"
305 "Metric value\n")
306 {
307 int idx_word = 2;
308 int idx_in_out = 3;
309 int idx_number = 4;
310 return rip_offset_list_unset(vty, argv[idx_word]->arg,
311 argv[idx_in_out]->arg,
312 argv[idx_number]->arg, NULL);
313 }
314
315 DEFUN (no_rip_offset_list_ifname,
316 no_rip_offset_list_ifname_cmd,
317 "no offset-list WORD <in|out> (0-16) IFNAME",
318 NO_STR
319 "Modify RIP metric\n"
320 "Access-list name\n"
321 "For incoming updates\n"
322 "For outgoing updates\n"
323 "Metric value\n"
324 "Interface to match\n")
325 {
326 int idx_word = 2;
327 int idx_in_out = 3;
328 int idx_number = 4;
329 int idx_ifname = 5;
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);
333 }
334
335 static int offset_list_cmp(struct rip_offset_list *o1,
336 struct rip_offset_list *o2)
337 {
338 return strcmp_safe(o1->ifname, o2->ifname);
339 }
340
341 static void offset_list_del(struct rip_offset_list *offset)
342 {
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));
347 if (offset->ifname)
348 free(offset->ifname);
349 rip_offset_list_free(offset);
350 }
351
352 void rip_offset_init()
353 {
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;
357
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);
362 }
363
364 void rip_offset_clean()
365 {
366 list_delete(&rip_offset_list_master);
367
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;
371 }
372
373 int config_write_rip_offset_list(struct vty *vty)
374 {
375 struct listnode *node, *nnode;
376 struct rip_offset_list *offset;
377
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]
383 .alist_name,
384 offset->direct[RIP_OFFSET_LIST_IN]
385 .metric);
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]
389 .alist_name,
390 offset->direct[RIP_OFFSET_LIST_OUT]
391 .metric);
392 } else {
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]
396 .alist_name,
397 offset->direct[RIP_OFFSET_LIST_IN]
398 .metric,
399 offset->ifname);
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]
403 .alist_name,
404 offset->direct[RIP_OFFSET_LIST_OUT]
405 .metric,
406 offset->ifname);
407 }
408 }
409
410 return 0;
411 }