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 (char *s1
, 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 (char *ifname
)
80 struct rip_offset_list
*offset
;
83 LIST_LOOP (rip_offset_list_master
, offset
, nn
)
85 if (strcmp_safe (offset
->ifname
, ifname
) == 0)
91 struct rip_offset_list
*
92 rip_offset_list_get (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
, char *alist
, char *direct_str
,
110 char *metric_str
, 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
, char *alist
, char *direct_str
,
148 char *metric_str
, char *ifname
)
152 struct rip_offset_list
*offset
;
154 /* Check direction. */
155 if (strncmp (direct_str
, "i", 1) == 0)
156 direct
= RIP_OFFSET_LIST_IN
;
157 else if (strncmp (direct_str
, "o", 1) == 0)
158 direct
= RIP_OFFSET_LIST_OUT
;
161 vty_out (vty
, "Invalid direction: %s%s", direct_str
, VTY_NEWLINE
);
166 metric
= atoi (metric_str
);
167 if (metric
< 0 || metric
> 16)
169 vty_out (vty
, "Invalid metric: %s%s", metric_str
, VTY_NEWLINE
);
173 /* Get offset-list structure with interface name. */
174 offset
= rip_offset_list_lookup (ifname
);
178 if (offset
->direct
[direct
].alist_name
)
179 free (offset
->direct
[direct
].alist_name
);
180 offset
->direct
[direct
].alist_name
= NULL
;
182 if (offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
== NULL
&&
183 offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
== NULL
)
185 listnode_delete (rip_offset_list_master
, offset
);
187 free (offset
->ifname
);
188 rip_offset_list_free (offset
);
193 vty_out (vty
, "Can't find offset-list%s", VTY_NEWLINE
);
199 #define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIP_OFFSET_LIST_IN].alist_name)
200 #define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_IN].metric)
202 #define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIP_OFFSET_LIST_OUT].alist_name)
203 #define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_OUT].metric)
205 /* If metric is modifed return 1. */
207 rip_offset_list_apply_in (struct prefix_ipv4
*p
, struct interface
*ifp
,
210 struct rip_offset_list
*offset
;
211 struct access_list
*alist
;
213 /* Look up offset-list with interface name. */
214 offset
= rip_offset_list_lookup (ifp
->name
);
215 if (offset
&& OFFSET_LIST_IN_NAME (offset
))
217 alist
= access_list_lookup (AFI_IP
, OFFSET_LIST_IN_NAME (offset
));
220 && access_list_apply (alist
, (struct prefix
*)p
) == FILTER_PERMIT
)
222 *metric
+= OFFSET_LIST_IN_METRIC (offset
);
227 /* Look up offset-list without interface name. */
228 offset
= rip_offset_list_lookup (NULL
);
229 if (offset
&& OFFSET_LIST_IN_NAME (offset
))
231 alist
= access_list_lookup (AFI_IP
, OFFSET_LIST_IN_NAME (offset
));
234 && access_list_apply (alist
, (struct prefix
*)p
) == FILTER_PERMIT
)
236 *metric
+= OFFSET_LIST_IN_METRIC (offset
);
244 /* If metric is modifed return 1. */
246 rip_offset_list_apply_out (struct prefix_ipv4
*p
, struct interface
*ifp
,
249 struct rip_offset_list
*offset
;
250 struct access_list
*alist
;
252 /* Look up offset-list with interface name. */
253 offset
= rip_offset_list_lookup (ifp
->name
);
254 if (offset
&& OFFSET_LIST_OUT_NAME (offset
))
256 alist
= access_list_lookup (AFI_IP
, OFFSET_LIST_OUT_NAME (offset
));
259 && access_list_apply (alist
, (struct prefix
*)p
) == FILTER_PERMIT
)
261 *metric
+= OFFSET_LIST_OUT_METRIC (offset
);
267 /* Look up offset-list without interface name. */
268 offset
= rip_offset_list_lookup (NULL
);
269 if (offset
&& OFFSET_LIST_OUT_NAME (offset
))
271 alist
= access_list_lookup (AFI_IP
, OFFSET_LIST_OUT_NAME (offset
));
274 && access_list_apply (alist
, (struct prefix
*)p
) == FILTER_PERMIT
)
276 *metric
+= OFFSET_LIST_OUT_METRIC (offset
);
284 DEFUN (rip_offset_list
,
286 "offset-list WORD (in|out) <0-16>",
287 "Modify RIP metric\n"
289 "For incoming updates\n"
290 "For outgoing updates\n"
293 return rip_offset_list_set (vty
, argv
[0], argv
[1], argv
[2], NULL
);
296 DEFUN (rip_offset_list_ifname
,
297 rip_offset_list_ifname_cmd
,
298 "offset-list WORD (in|out) <0-16> IFNAME",
299 "Modify RIP metric\n"
301 "For incoming updates\n"
302 "For outgoing updates\n"
304 "Interface to match\n")
306 return rip_offset_list_set (vty
, argv
[0], argv
[1], argv
[2], argv
[3]);
309 DEFUN (no_rip_offset_list
,
310 no_rip_offset_list_cmd
,
311 "no offset-list WORD (in|out) <0-16>",
313 "Modify RIP metric\n"
315 "For incoming updates\n"
316 "For outgoing updates\n"
319 return rip_offset_list_unset (vty
, argv
[0], argv
[1], argv
[2], NULL
);
322 DEFUN (no_rip_offset_list_ifname
,
323 no_rip_offset_list_ifname_cmd
,
324 "no offset-list WORD (in|out) <0-16> IFNAME",
326 "Modify RIP metric\n"
328 "For incoming updates\n"
329 "For outgoing updates\n"
331 "Interface to match\n")
333 return rip_offset_list_unset (vty
, argv
[0], argv
[1], argv
[2], argv
[3]);
337 offset_list_cmp (struct rip_offset_list
*o1
, struct rip_offset_list
*o2
)
339 return strcmp_safe (o1
->ifname
, o2
->ifname
);
343 offset_list_del (struct rip_offset_list
*offset
)
345 if (OFFSET_LIST_IN_NAME (offset
))
346 free (OFFSET_LIST_IN_NAME (offset
));
347 if (OFFSET_LIST_OUT_NAME (offset
))
348 free (OFFSET_LIST_OUT_NAME (offset
));
350 free (offset
->ifname
);
351 rip_offset_list_free (offset
);
357 rip_offset_list_master
= list_new ();
358 rip_offset_list_master
->cmp
= (int (*)(void *, void *)) offset_list_cmp
;
359 rip_offset_list_master
->del
= (void (*)(void *)) offset_list_del
;
361 install_element (RIP_NODE
, &rip_offset_list_cmd
);
362 install_element (RIP_NODE
, &rip_offset_list_ifname_cmd
);
363 install_element (RIP_NODE
, &no_rip_offset_list_cmd
);
364 install_element (RIP_NODE
, &no_rip_offset_list_ifname_cmd
);
370 list_delete (rip_offset_list_master
);
372 rip_offset_list_master
= list_new ();
373 rip_offset_list_master
->cmp
= (int (*)(void *, void *)) offset_list_cmp
;
374 rip_offset_list_master
->del
= (void (*)(void *)) offset_list_del
;
378 config_write_rip_offset_list (struct vty
*vty
)
381 struct rip_offset_list
*offset
;
383 LIST_LOOP (rip_offset_list_master
, offset
, nn
)
385 if (! offset
->ifname
)
387 if (offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
)
388 vty_out (vty
, " offset-list %s in %d%s",
389 offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
,
390 offset
->direct
[RIP_OFFSET_LIST_IN
].metric
,
392 if (offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
)
393 vty_out (vty
, " offset-list %s out %d%s",
394 offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
,
395 offset
->direct
[RIP_OFFSET_LIST_OUT
].metric
,
400 if (offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
)
401 vty_out (vty
, " offset-list %s in %d %s%s",
402 offset
->direct
[RIP_OFFSET_LIST_IN
].alist_name
,
403 offset
->direct
[RIP_OFFSET_LIST_IN
].metric
,
404 offset
->ifname
, VTY_NEWLINE
);
405 if (offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
)
406 vty_out (vty
, " offset-list %s out %d %s%s",
407 offset
->direct
[RIP_OFFSET_LIST_OUT
].alist_name
,
408 offset
->direct
[RIP_OFFSET_LIST_OUT
].metric
,
409 offset
->ifname
, VTY_NEWLINE
);