]>
Commit | Line | Data |
---|---|---|
718e3744 | 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 | |
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 | |
19 | * 02111-1307, USA. | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "if.h" | |
25 | #include "prefix.h" | |
26 | #include "filter.h" | |
27 | #include "command.h" | |
28 | #include "linklist.h" | |
29 | #include "memory.h" | |
30 | ||
dc63bfd4 | 31 | #include "ripd/ripd.h" |
32 | ||
718e3744 | 33 | #define RIP_OFFSET_LIST_IN 0 |
34 | #define RIP_OFFSET_LIST_OUT 1 | |
35 | #define RIP_OFFSET_LIST_MAX 2 | |
36 | ||
37 | struct rip_offset_list | |
38 | { | |
39 | char *ifname; | |
40 | ||
41 | struct | |
42 | { | |
43 | char *alist_name; | |
44 | /* struct access_list *alist; */ | |
45 | int metric; | |
46 | } direct[RIP_OFFSET_LIST_MAX]; | |
47 | }; | |
48 | ||
49 | static struct list *rip_offset_list_master; | |
50 | ||
dc63bfd4 | 51 | static int |
98b718a9 | 52 | strcmp_safe (const char *s1, const char *s2) |
718e3744 | 53 | { |
54 | if (s1 == NULL && s2 == NULL) | |
55 | return 0; | |
56 | if (s1 == NULL) | |
57 | return -1; | |
58 | if (s2 == NULL) | |
59 | return 1; | |
60 | return strcmp (s1, s2); | |
61 | } | |
62 | ||
dc63bfd4 | 63 | static struct rip_offset_list * |
64 | rip_offset_list_new (void) | |
718e3744 | 65 | { |
393deb9b | 66 | return XCALLOC (MTYPE_RIP_OFFSET_LIST, sizeof (struct rip_offset_list)); |
718e3744 | 67 | } |
68 | ||
dc63bfd4 | 69 | static void |
718e3744 | 70 | rip_offset_list_free (struct rip_offset_list *offset) |
71 | { | |
72 | XFREE (MTYPE_RIP_OFFSET_LIST, offset); | |
73 | } | |
74 | ||
dc63bfd4 | 75 | static struct rip_offset_list * |
98b718a9 | 76 | rip_offset_list_lookup (const char *ifname) |
718e3744 | 77 | { |
78 | struct rip_offset_list *offset; | |
1eb8ef25 | 79 | struct listnode *node, *nnode; |
718e3744 | 80 | |
1eb8ef25 | 81 | for (ALL_LIST_ELEMENTS (rip_offset_list_master, node, nnode, offset)) |
718e3744 | 82 | { |
83 | if (strcmp_safe (offset->ifname, ifname) == 0) | |
84 | return offset; | |
85 | } | |
86 | return NULL; | |
87 | } | |
88 | ||
dc63bfd4 | 89 | static struct rip_offset_list * |
98b718a9 | 90 | rip_offset_list_get (const char *ifname) |
718e3744 | 91 | { |
92 | struct rip_offset_list *offset; | |
93 | ||
94 | offset = rip_offset_list_lookup (ifname); | |
95 | if (offset) | |
96 | return offset; | |
97 | ||
98 | offset = rip_offset_list_new (); | |
99 | if (ifname) | |
100 | offset->ifname = strdup (ifname); | |
101 | listnode_add_sort (rip_offset_list_master, offset); | |
102 | ||
103 | return offset; | |
104 | } | |
105 | ||
dc63bfd4 | 106 | static int |
98b718a9 | 107 | rip_offset_list_set (struct vty *vty, const char *alist, const char *direct_str, |
108 | const char *metric_str, const char *ifname) | |
718e3744 | 109 | { |
110 | int direct; | |
111 | int metric; | |
112 | struct rip_offset_list *offset; | |
113 | ||
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; | |
119 | else | |
120 | { | |
121 | vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE); | |
122 | return CMD_WARNING; | |
123 | } | |
124 | ||
125 | /* Check metric. */ | |
126 | metric = atoi (metric_str); | |
127 | if (metric < 0 || metric > 16) | |
128 | { | |
129 | vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE); | |
130 | return CMD_WARNING; | |
131 | } | |
132 | ||
133 | /* Get offset-list structure with interface name. */ | |
134 | offset = rip_offset_list_get (ifname); | |
135 | ||
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; | |
140 | ||
141 | return CMD_SUCCESS; | |
142 | } | |
143 | ||
dc63bfd4 | 144 | static int |
98b718a9 | 145 | rip_offset_list_unset (struct vty *vty, const char *alist, |
146 | const char *direct_str, const char *metric_str, | |
147 | const char *ifname) | |
718e3744 | 148 | { |
149 | int direct; | |
150 | int metric; | |
151 | struct rip_offset_list *offset; | |
152 | ||
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; | |
158 | else | |
159 | { | |
160 | vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE); | |
161 | return CMD_WARNING; | |
162 | } | |
163 | ||
164 | /* Check metric. */ | |
165 | metric = atoi (metric_str); | |
166 | if (metric < 0 || metric > 16) | |
167 | { | |
168 | vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE); | |
169 | return CMD_WARNING; | |
170 | } | |
171 | ||
172 | /* Get offset-list structure with interface name. */ | |
173 | offset = rip_offset_list_lookup (ifname); | |
174 | ||
175 | if (offset) | |
176 | { | |
177 | if (offset->direct[direct].alist_name) | |
178 | free (offset->direct[direct].alist_name); | |
179 | offset->direct[direct].alist_name = NULL; | |
180 | ||
181 | if (offset->direct[RIP_OFFSET_LIST_IN].alist_name == NULL && | |
182 | offset->direct[RIP_OFFSET_LIST_OUT].alist_name == NULL) | |
183 | { | |
184 | listnode_delete (rip_offset_list_master, offset); | |
185 | if (offset->ifname) | |
186 | free (offset->ifname); | |
187 | rip_offset_list_free (offset); | |
188 | } | |
189 | } | |
190 | else | |
191 | { | |
192 | vty_out (vty, "Can't find offset-list%s", VTY_NEWLINE); | |
193 | return CMD_WARNING; | |
194 | } | |
195 | return CMD_SUCCESS; | |
196 | } | |
197 | ||
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) | |
200 | ||
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) | |
203 | ||
204 | /* If metric is modifed return 1. */ | |
205 | int | |
206 | rip_offset_list_apply_in (struct prefix_ipv4 *p, struct interface *ifp, | |
207 | u_int32_t *metric) | |
208 | { | |
209 | struct rip_offset_list *offset; | |
210 | struct access_list *alist; | |
211 | ||
212 | /* Look up offset-list with interface name. */ | |
213 | offset = rip_offset_list_lookup (ifp->name); | |
214 | if (offset && OFFSET_LIST_IN_NAME (offset)) | |
215 | { | |
216 | alist = access_list_lookup (AFI_IP, OFFSET_LIST_IN_NAME (offset)); | |
217 | ||
218 | if (alist | |
219 | && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT) | |
220 | { | |
221 | *metric += OFFSET_LIST_IN_METRIC (offset); | |
222 | return 1; | |
223 | } | |
224 | return 0; | |
225 | } | |
226 | /* Look up offset-list without interface name. */ | |
227 | offset = rip_offset_list_lookup (NULL); | |
228 | if (offset && OFFSET_LIST_IN_NAME (offset)) | |
229 | { | |
230 | alist = access_list_lookup (AFI_IP, OFFSET_LIST_IN_NAME (offset)); | |
231 | ||
232 | if (alist | |
233 | && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT) | |
234 | { | |
235 | *metric += OFFSET_LIST_IN_METRIC (offset); | |
236 | return 1; | |
237 | } | |
238 | return 0; | |
239 | } | |
240 | return 0; | |
241 | } | |
242 | ||
243 | /* If metric is modifed return 1. */ | |
244 | int | |
245 | rip_offset_list_apply_out (struct prefix_ipv4 *p, struct interface *ifp, | |
246 | u_int32_t *metric) | |
247 | { | |
248 | struct rip_offset_list *offset; | |
249 | struct access_list *alist; | |
250 | ||
251 | /* Look up offset-list with interface name. */ | |
252 | offset = rip_offset_list_lookup (ifp->name); | |
253 | if (offset && OFFSET_LIST_OUT_NAME (offset)) | |
254 | { | |
255 | alist = access_list_lookup (AFI_IP, OFFSET_LIST_OUT_NAME (offset)); | |
256 | ||
257 | if (alist | |
258 | && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT) | |
259 | { | |
260 | *metric += OFFSET_LIST_OUT_METRIC (offset); | |
261 | return 1; | |
262 | } | |
263 | return 0; | |
264 | } | |
265 | ||
266 | /* Look up offset-list without interface name. */ | |
267 | offset = rip_offset_list_lookup (NULL); | |
268 | if (offset && OFFSET_LIST_OUT_NAME (offset)) | |
269 | { | |
270 | alist = access_list_lookup (AFI_IP, OFFSET_LIST_OUT_NAME (offset)); | |
271 | ||
272 | if (alist | |
273 | && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT) | |
274 | { | |
275 | *metric += OFFSET_LIST_OUT_METRIC (offset); | |
276 | return 1; | |
277 | } | |
278 | return 0; | |
279 | } | |
280 | return 0; | |
281 | } | |
282 | ||
283 | DEFUN (rip_offset_list, | |
284 | rip_offset_list_cmd, | |
6147e2c6 | 285 | "offset-list WORD <in|out> (0-16)", |
718e3744 | 286 | "Modify RIP metric\n" |
287 | "Access-list name\n" | |
288 | "For incoming updates\n" | |
289 | "For outgoing updates\n" | |
290 | "Metric value\n") | |
291 | { | |
80fa0c69 DW |
292 | int idx_word = 1; |
293 | int idx_in_out = 2; | |
294 | int idx_number = 3; | |
295 | return rip_offset_list_set (vty, argv[idx_word]->arg, argv[idx_in_out]->arg, argv[idx_number]->arg, NULL); | |
718e3744 | 296 | } |
297 | ||
298 | DEFUN (rip_offset_list_ifname, | |
299 | rip_offset_list_ifname_cmd, | |
6147e2c6 | 300 | "offset-list WORD <in|out> (0-16) IFNAME", |
718e3744 | 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 | "Interface to match\n") | |
307 | { | |
80fa0c69 DW |
308 | int idx_word = 1; |
309 | int idx_in_out = 2; | |
310 | int idx_number = 3; | |
311 | int idx_ifname = 4; | |
312 | return rip_offset_list_set (vty, argv[idx_word]->arg, argv[idx_in_out]->arg, argv[idx_number]->arg, argv[idx_ifname]->arg); | |
718e3744 | 313 | } |
314 | ||
315 | DEFUN (no_rip_offset_list, | |
316 | no_rip_offset_list_cmd, | |
6147e2c6 | 317 | "no offset-list WORD <in|out> (0-16)", |
718e3744 | 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 | { | |
80fa0c69 DW |
325 | int idx_word = 2; |
326 | int idx_in_out = 3; | |
327 | int idx_number = 4; | |
328 | return rip_offset_list_unset (vty, argv[idx_word]->arg, argv[idx_in_out]->arg, argv[idx_number]->arg, NULL); | |
718e3744 | 329 | } |
330 | ||
331 | DEFUN (no_rip_offset_list_ifname, | |
332 | no_rip_offset_list_ifname_cmd, | |
6147e2c6 | 333 | "no offset-list WORD <in|out> (0-16) IFNAME", |
718e3744 | 334 | NO_STR |
335 | "Modify RIP metric\n" | |
336 | "Access-list name\n" | |
337 | "For incoming updates\n" | |
338 | "For outgoing updates\n" | |
339 | "Metric value\n" | |
340 | "Interface to match\n") | |
341 | { | |
80fa0c69 DW |
342 | int idx_word = 2; |
343 | int idx_in_out = 3; | |
344 | int idx_number = 4; | |
345 | int idx_ifname = 5; | |
346 | return rip_offset_list_unset (vty, argv[idx_word]->arg, argv[idx_in_out]->arg, argv[idx_number]->arg, argv[idx_ifname]->arg); | |
718e3744 | 347 | } |
348 | ||
dc63bfd4 | 349 | static int |
718e3744 | 350 | offset_list_cmp (struct rip_offset_list *o1, struct rip_offset_list *o2) |
351 | { | |
352 | return strcmp_safe (o1->ifname, o2->ifname); | |
353 | } | |
354 | ||
dc63bfd4 | 355 | static void |
718e3744 | 356 | offset_list_del (struct rip_offset_list *offset) |
357 | { | |
358 | if (OFFSET_LIST_IN_NAME (offset)) | |
359 | free (OFFSET_LIST_IN_NAME (offset)); | |
360 | if (OFFSET_LIST_OUT_NAME (offset)) | |
361 | free (OFFSET_LIST_OUT_NAME (offset)); | |
362 | if (offset->ifname) | |
363 | free (offset->ifname); | |
364 | rip_offset_list_free (offset); | |
365 | } | |
366 | ||
367 | void | |
368 | rip_offset_init () | |
369 | { | |
370 | rip_offset_list_master = list_new (); | |
371 | rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp; | |
372 | rip_offset_list_master->del = (void (*)(void *)) offset_list_del; | |
373 | ||
374 | install_element (RIP_NODE, &rip_offset_list_cmd); | |
375 | install_element (RIP_NODE, &rip_offset_list_ifname_cmd); | |
376 | install_element (RIP_NODE, &no_rip_offset_list_cmd); | |
377 | install_element (RIP_NODE, &no_rip_offset_list_ifname_cmd); | |
378 | } | |
379 | ||
380 | void | |
381 | rip_offset_clean () | |
382 | { | |
383 | list_delete (rip_offset_list_master); | |
384 | ||
385 | rip_offset_list_master = list_new (); | |
386 | rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp; | |
387 | rip_offset_list_master->del = (void (*)(void *)) offset_list_del; | |
388 | } | |
389 | ||
390 | int | |
391 | config_write_rip_offset_list (struct vty *vty) | |
392 | { | |
1eb8ef25 | 393 | struct listnode *node, *nnode; |
718e3744 | 394 | struct rip_offset_list *offset; |
395 | ||
1eb8ef25 | 396 | for (ALL_LIST_ELEMENTS (rip_offset_list_master, node, nnode, offset)) |
718e3744 | 397 | { |
398 | if (! offset->ifname) | |
399 | { | |
400 | if (offset->direct[RIP_OFFSET_LIST_IN].alist_name) | |
401 | vty_out (vty, " offset-list %s in %d%s", | |
402 | offset->direct[RIP_OFFSET_LIST_IN].alist_name, | |
403 | offset->direct[RIP_OFFSET_LIST_IN].metric, | |
404 | VTY_NEWLINE); | |
405 | if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name) | |
406 | vty_out (vty, " offset-list %s out %d%s", | |
407 | offset->direct[RIP_OFFSET_LIST_OUT].alist_name, | |
408 | offset->direct[RIP_OFFSET_LIST_OUT].metric, | |
409 | VTY_NEWLINE); | |
410 | } | |
411 | else | |
412 | { | |
413 | if (offset->direct[RIP_OFFSET_LIST_IN].alist_name) | |
414 | vty_out (vty, " offset-list %s in %d %s%s", | |
415 | offset->direct[RIP_OFFSET_LIST_IN].alist_name, | |
416 | offset->direct[RIP_OFFSET_LIST_IN].metric, | |
417 | offset->ifname, VTY_NEWLINE); | |
418 | if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name) | |
419 | vty_out (vty, " offset-list %s out %d %s%s", | |
420 | offset->direct[RIP_OFFSET_LIST_OUT].alist_name, | |
421 | offset->direct[RIP_OFFSET_LIST_OUT].metric, | |
422 | offset->ifname, VTY_NEWLINE); | |
423 | } | |
424 | } | |
425 | ||
426 | return 0; | |
427 | } |