1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// SPDX-License-Identifier: GPL-2.0-or-later
/* RIP peer support
* Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
*/
#include <zebra.h>
#include "if.h"
#include "prefix.h"
#include "command.h"
#include "linklist.h"
#include "frrevent.h"
#include "memory.h"
#include "table.h"
#include "ripd/ripd.h"
#include "ripd/rip_bfd.h"
DEFINE_MTYPE_STATIC(RIPD, RIP_PEER, "RIP peer");
static struct rip_peer *rip_peer_new(void)
{
return XCALLOC(MTYPE_RIP_PEER, sizeof(struct rip_peer));
}
void rip_peer_free(struct rip_peer *peer)
{
bfd_sess_free(&peer->bfd_session);
EVENT_OFF(peer->t_timeout);
XFREE(MTYPE_RIP_PEER, peer);
}
struct rip_peer *rip_peer_lookup(struct rip *rip, struct in_addr *addr)
{
struct rip_peer *peer;
struct listnode *node, *nnode;
for (ALL_LIST_ELEMENTS(rip->peer_list, node, nnode, peer)) {
if (IPV4_ADDR_SAME(&peer->addr, addr))
return peer;
}
return NULL;
}
struct rip_peer *rip_peer_lookup_next(struct rip *rip, struct in_addr *addr)
{
struct rip_peer *peer;
struct listnode *node, *nnode;
for (ALL_LIST_ELEMENTS(rip->peer_list, node, nnode, peer)) {
if (htonl(peer->addr.s_addr) > htonl(addr->s_addr))
return peer;
}
return NULL;
}
/* RIP peer is timeout. */
static void rip_peer_timeout(struct event *t)
{
struct rip_peer *peer;
peer = EVENT_ARG(t);
listnode_delete(peer->rip->peer_list, peer);
rip_peer_free(peer);
}
/* Get RIP peer. At the same time update timeout thread. */
static struct rip_peer *rip_peer_get(struct rip *rip, struct rip_interface *ri,
struct in_addr *addr)
{
struct rip_peer *peer;
peer = rip_peer_lookup(rip, addr);
if (peer) {
EVENT_OFF(peer->t_timeout);
} else {
peer = rip_peer_new();
peer->rip = rip;
peer->ri = ri;
peer->addr = *addr;
rip_bfd_session_update(peer);
listnode_add_sort(rip->peer_list, peer);
}
/* Update timeout thread. */
event_add_timer(master, rip_peer_timeout, peer, RIP_PEER_TIMER_DEFAULT,
&peer->t_timeout);
/* Last update time set. */
time(&peer->uptime);
return peer;
}
void rip_peer_update(struct rip *rip, struct rip_interface *ri,
struct sockaddr_in *from, uint8_t version)
{
struct rip_peer *peer;
peer = rip_peer_get(rip, ri, &from->sin_addr);
peer->version = version;
}
void rip_peer_bad_route(struct rip *rip, struct rip_interface *ri,
struct sockaddr_in *from)
{
struct rip_peer *peer;
peer = rip_peer_get(rip, ri, &from->sin_addr);
peer->recv_badroutes++;
}
void rip_peer_bad_packet(struct rip *rip, struct rip_interface *ri,
struct sockaddr_in *from)
{
struct rip_peer *peer;
peer = rip_peer_get(rip, ri, &from->sin_addr);
peer->recv_badpackets++;
}
/* Display peer uptime. */
static char *rip_peer_uptime(struct rip_peer *peer, char *buf, size_t len)
{
time_t uptime;
/* If there is no connection has been done before print `never'. */
if (peer->uptime == 0) {
snprintf(buf, len, "never ");
return buf;
}
/* Get current time. */
uptime = time(NULL);
uptime -= peer->uptime;
frrtime_to_interval(uptime, buf, len);
return buf;
}
void rip_peer_display(struct vty *vty, struct rip *rip)
{
struct rip_peer *peer;
struct listnode *node, *nnode;
#define RIP_UPTIME_LEN 25
char timebuf[RIP_UPTIME_LEN];
for (ALL_LIST_ELEMENTS(rip->peer_list, node, nnode, peer)) {
vty_out(vty, " %-17pI4 %9d %9d %9d %11s\n",
&peer->addr, peer->recv_badpackets,
peer->recv_badroutes, ZEBRA_RIP_DISTANCE_DEFAULT,
rip_peer_uptime(peer, timebuf, RIP_UPTIME_LEN));
}
}
int rip_peer_list_cmp(struct rip_peer *p1, struct rip_peer *p2)
{
if (p2->addr.s_addr == p1->addr.s_addr)
return 0;
return (htonl(p1->addr.s_addr) < htonl(p2->addr.s_addr)) ? -1 : 1;
}
void rip_peer_list_del(void *arg)
{
rip_peer_free(arg);
}
void rip_peer_delete_routes(const struct rip_peer *peer)
{
struct route_node *route_node;
for (route_node = route_top(peer->rip->table); route_node;
route_node = route_next(route_node)) {
struct rip_info *route_entry;
struct listnode *listnode;
struct listnode *listnode_next;
struct list *list;
list = route_node->info;
if (list == NULL)
continue;
for (ALL_LIST_ELEMENTS(list, listnode, listnode_next,
route_entry)) {
if (!rip_route_rte(route_entry))
continue;
if (route_entry->from.s_addr != peer->addr.s_addr)
continue;
if (listcount(list) == 1) {
EVENT_OFF(route_entry->t_timeout);
EVENT_OFF(route_entry->t_garbage_collect);
listnode_delete(list, route_entry);
if (list_isempty(list)) {
list_delete((struct list **)&route_node
->info);
route_unlock_node(route_node);
}
rip_info_free(route_entry);
/* Signal the output process to trigger an
* update (see section 2.5). */
rip_event(peer->rip, RIP_TRIGGERED_UPDATE, 0);
} else
rip_ecmp_delete(peer->rip, route_entry);
break;
}
}
}
|