]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_offset.c
Merge remote-tracking branch 'frr/master' into rip-vrf
[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 OFFSET_LIST_IN_NAME(O) ((O)->direct[RIP_OFFSET_LIST_IN].alist_name)
33 #define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_IN].metric)
34
35 #define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIP_OFFSET_LIST_OUT].alist_name)
36 #define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_OUT].metric)
37
38 struct rip_offset_list *rip_offset_list_new(struct rip *rip, const char *ifname)
39 {
40 struct rip_offset_list *offset;
41
42 offset = XCALLOC(MTYPE_RIP_OFFSET_LIST, sizeof(struct rip_offset_list));
43 offset->rip = rip;
44 offset->ifname = strdup(ifname);
45 listnode_add_sort(rip->offset_list_master, offset);
46
47 return offset;
48 }
49
50 void offset_list_del(struct rip_offset_list *offset)
51 {
52 listnode_delete(offset->rip->offset_list_master, offset);
53 if (OFFSET_LIST_IN_NAME(offset))
54 free(OFFSET_LIST_IN_NAME(offset));
55 if (OFFSET_LIST_OUT_NAME(offset))
56 free(OFFSET_LIST_OUT_NAME(offset));
57 free(offset->ifname);
58 XFREE(MTYPE_RIP_OFFSET_LIST, offset);
59 }
60
61 struct rip_offset_list *rip_offset_list_lookup(struct rip *rip,
62 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_interface *ri = ifp->info;
79 struct rip_offset_list *offset;
80 struct access_list *alist;
81
82 /* Look up offset-list with interface name. */
83 offset = rip_offset_list_lookup(ri->rip, ifp->name);
84 if (offset && OFFSET_LIST_IN_NAME(offset)) {
85 alist = access_list_lookup(AFI_IP, OFFSET_LIST_IN_NAME(offset));
86
87 if (alist
88 && access_list_apply(alist, (struct prefix *)p)
89 == FILTER_PERMIT) {
90 *metric += OFFSET_LIST_IN_METRIC(offset);
91 return 1;
92 }
93 return 0;
94 }
95 /* Look up offset-list without interface name. */
96 offset = rip_offset_list_lookup(ri->rip, "*");
97 if (offset && OFFSET_LIST_IN_NAME(offset)) {
98 alist = access_list_lookup(AFI_IP, OFFSET_LIST_IN_NAME(offset));
99
100 if (alist
101 && access_list_apply(alist, (struct prefix *)p)
102 == FILTER_PERMIT) {
103 *metric += OFFSET_LIST_IN_METRIC(offset);
104 return 1;
105 }
106 return 0;
107 }
108 return 0;
109 }
110
111 /* If metric is modifed return 1. */
112 int rip_offset_list_apply_out(struct prefix_ipv4 *p, struct interface *ifp,
113 uint32_t *metric)
114 {
115 struct rip_interface *ri = ifp->info;
116 struct rip_offset_list *offset;
117 struct access_list *alist;
118
119 /* Look up offset-list with interface name. */
120 offset = rip_offset_list_lookup(ri->rip, ifp->name);
121 if (offset && OFFSET_LIST_OUT_NAME(offset)) {
122 alist = access_list_lookup(AFI_IP,
123 OFFSET_LIST_OUT_NAME(offset));
124
125 if (alist
126 && access_list_apply(alist, (struct prefix *)p)
127 == FILTER_PERMIT) {
128 *metric += OFFSET_LIST_OUT_METRIC(offset);
129 return 1;
130 }
131 return 0;
132 }
133
134 /* Look up offset-list without interface name. */
135 offset = rip_offset_list_lookup(ri->rip, "*");
136 if (offset && OFFSET_LIST_OUT_NAME(offset)) {
137 alist = access_list_lookup(AFI_IP,
138 OFFSET_LIST_OUT_NAME(offset));
139
140 if (alist
141 && access_list_apply(alist, (struct prefix *)p)
142 == FILTER_PERMIT) {
143 *metric += OFFSET_LIST_OUT_METRIC(offset);
144 return 1;
145 }
146 return 0;
147 }
148 return 0;
149 }
150
151 int offset_list_cmp(struct rip_offset_list *o1, struct rip_offset_list *o2)
152 {
153 return strcmp(o1->ifname, o2->ifname);
154 }