]> git.proxmox.com Git - mirror_frr.git/blame - ripd/rip_offset.c
zebra: Convert socket interface to use `union sockunion`
[mirror_frr.git] / ripd / rip_offset.c
CommitLineData
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 *
896014f4
DL
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
718e3744 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
dc63bfd4 30#include "ripd/ripd.h"
31
718e3744 32static struct list *rip_offset_list_master;
33
8c942f65
RW
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)
718e3744 36
8c942f65
RW
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)
718e3744 39
8c942f65 40struct rip_offset_list *rip_offset_list_new(const char *ifname)
718e3744 41{
d62a17ae 42 struct rip_offset_list *offset;
43
8c942f65
RW
44 offset = XCALLOC(MTYPE_RIP_OFFSET_LIST, sizeof(struct rip_offset_list));
45 offset->ifname = strdup(ifname);
d62a17ae 46 listnode_add_sort(rip_offset_list_master, offset);
47
48 return offset;
718e3744 49}
50
8c942f65 51void offset_list_del(struct rip_offset_list *offset)
718e3744 52{
8c942f65
RW
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);
718e3744 60}
61
8c942f65 62struct rip_offset_list *rip_offset_list_lookup(const char *ifname)
718e3744 63{
d62a17ae 64 struct rip_offset_list *offset;
8c942f65 65 struct listnode *node, *nnode;
d62a17ae 66
8c942f65
RW
67 for (ALL_LIST_ELEMENTS(rip_offset_list_master, node, nnode, offset)) {
68 if (strcmp(offset->ifname, ifname) == 0)
69 return offset;
d62a17ae 70 }
8c942f65 71 return NULL;
718e3744 72}
73
718e3744 74/* If metric is modifed return 1. */
d62a17ae 75int rip_offset_list_apply_in(struct prefix_ipv4 *p, struct interface *ifp,
d7c0a89a 76 uint32_t *metric)
718e3744 77{
d62a17ae 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;
718e3744 93 }
d62a17ae 94 /* Look up offset-list without interface name. */
8c942f65 95 offset = rip_offset_list_lookup("*");
d62a17ae 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;
718e3744 106 }
d62a17ae 107 return 0;
718e3744 108}
109
110/* If metric is modifed return 1. */
d62a17ae 111int rip_offset_list_apply_out(struct prefix_ipv4 *p, struct interface *ifp,
d7c0a89a 112 uint32_t *metric)
718e3744 113{
d62a17ae 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;
718e3744 130 }
d62a17ae 131
132 /* Look up offset-list without interface name. */
8c942f65 133 offset = rip_offset_list_lookup("*");
d62a17ae 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;
718e3744 145 }
d62a17ae 146 return 0;
718e3744 147}
148
d62a17ae 149static int offset_list_cmp(struct rip_offset_list *o1,
150 struct rip_offset_list *o2)
718e3744 151{
8c942f65 152 return strcmp(o1->ifname, o2->ifname);
718e3744 153}
154
d62a17ae 155void rip_offset_init()
718e3744 156{
d62a17ae 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;
718e3744 160}
161
d62a17ae 162void rip_offset_clean()
718e3744 163{
6a154c88 164 list_delete(&rip_offset_list_master);
718e3744 165
d62a17ae 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;
718e3744 169}