]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_offset.c
Merge pull request #3287 from donaldsharp/v6_access_list
[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 static struct list *rip_offset_list_master;
33
34 #define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIP_OFFSET_LIST_IN].alist_name)
35 #define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_IN].metric)
36
37 #define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIP_OFFSET_LIST_OUT].alist_name)
38 #define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_OUT].metric)
39
40 struct rip_offset_list *rip_offset_list_new(const char *ifname)
41 {
42 struct rip_offset_list *offset;
43
44 offset = XCALLOC(MTYPE_RIP_OFFSET_LIST, sizeof(struct rip_offset_list));
45 offset->ifname = strdup(ifname);
46 listnode_add_sort(rip_offset_list_master, offset);
47
48 return offset;
49 }
50
51 void offset_list_del(struct rip_offset_list *offset)
52 {
53 listnode_delete(rip_offset_list_master, offset);
54 if (OFFSET_LIST_IN_NAME(offset))
55 free(OFFSET_LIST_IN_NAME(offset));
56 if (OFFSET_LIST_OUT_NAME(offset))
57 free(OFFSET_LIST_OUT_NAME(offset));
58 free(offset->ifname);
59 XFREE(MTYPE_RIP_OFFSET_LIST, offset);
60 }
61
62 struct rip_offset_list *rip_offset_list_lookup(const char *ifname)
63 {
64 struct rip_offset_list *offset;
65 struct listnode *node, *nnode;
66
67 for (ALL_LIST_ELEMENTS(rip_offset_list_master, node, nnode, offset)) {
68 if (strcmp(offset->ifname, ifname) == 0)
69 return offset;
70 }
71 return NULL;
72 }
73
74 /* If metric is modifed return 1. */
75 int rip_offset_list_apply_in(struct prefix_ipv4 *p, struct interface *ifp,
76 uint32_t *metric)
77 {
78 struct rip_offset_list *offset;
79 struct access_list *alist;
80
81 /* Look up offset-list with interface name. */
82 offset = rip_offset_list_lookup(ifp->name);
83 if (offset && OFFSET_LIST_IN_NAME(offset)) {
84 alist = access_list_lookup(AFI_IP, OFFSET_LIST_IN_NAME(offset));
85
86 if (alist
87 && access_list_apply(alist, (struct prefix *)p)
88 == FILTER_PERMIT) {
89 *metric += OFFSET_LIST_IN_METRIC(offset);
90 return 1;
91 }
92 return 0;
93 }
94 /* Look up offset-list without interface name. */
95 offset = rip_offset_list_lookup("*");
96 if (offset && OFFSET_LIST_IN_NAME(offset)) {
97 alist = access_list_lookup(AFI_IP, OFFSET_LIST_IN_NAME(offset));
98
99 if (alist
100 && access_list_apply(alist, (struct prefix *)p)
101 == FILTER_PERMIT) {
102 *metric += OFFSET_LIST_IN_METRIC(offset);
103 return 1;
104 }
105 return 0;
106 }
107 return 0;
108 }
109
110 /* If metric is modifed return 1. */
111 int rip_offset_list_apply_out(struct prefix_ipv4 *p, struct interface *ifp,
112 uint32_t *metric)
113 {
114 struct rip_offset_list *offset;
115 struct access_list *alist;
116
117 /* Look up offset-list with interface name. */
118 offset = rip_offset_list_lookup(ifp->name);
119 if (offset && OFFSET_LIST_OUT_NAME(offset)) {
120 alist = access_list_lookup(AFI_IP,
121 OFFSET_LIST_OUT_NAME(offset));
122
123 if (alist
124 && access_list_apply(alist, (struct prefix *)p)
125 == FILTER_PERMIT) {
126 *metric += OFFSET_LIST_OUT_METRIC(offset);
127 return 1;
128 }
129 return 0;
130 }
131
132 /* Look up offset-list without interface name. */
133 offset = rip_offset_list_lookup("*");
134 if (offset && OFFSET_LIST_OUT_NAME(offset)) {
135 alist = access_list_lookup(AFI_IP,
136 OFFSET_LIST_OUT_NAME(offset));
137
138 if (alist
139 && access_list_apply(alist, (struct prefix *)p)
140 == FILTER_PERMIT) {
141 *metric += OFFSET_LIST_OUT_METRIC(offset);
142 return 1;
143 }
144 return 0;
145 }
146 return 0;
147 }
148
149 static int offset_list_cmp(struct rip_offset_list *o1,
150 struct rip_offset_list *o2)
151 {
152 return strcmp(o1->ifname, o2->ifname);
153 }
154
155 void rip_offset_init()
156 {
157 rip_offset_list_master = list_new();
158 rip_offset_list_master->cmp = (int (*)(void *, void *))offset_list_cmp;
159 rip_offset_list_master->del = (void (*)(void *))offset_list_del;
160 }
161
162 void rip_offset_clean()
163 {
164 list_delete(&rip_offset_list_master);
165
166 rip_offset_list_master = list_new();
167 rip_offset_list_master->cmp = (int (*)(void *, void *))offset_list_cmp;
168 rip_offset_list_master->del = (void (*)(void *))offset_list_del;
169 }