]>
Commit | Line | Data |
---|---|---|
a94434b6 | 1 | /* RIPng peer support |
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 | |
a94434b6 | 19 | */ |
20 | ||
21 | /* RIPng support added by Vincent Jardin <vincent.jardin@6wind.com> | |
22 | * Copyright (C) 2002 6WIND | |
23 | */ | |
24 | ||
25 | #include <zebra.h> | |
26 | ||
27 | #include "if.h" | |
28 | #include "prefix.h" | |
29 | #include "command.h" | |
30 | #include "linklist.h" | |
31 | #include "thread.h" | |
32 | #include "memory.h" | |
33 | ||
34 | #include "ripngd/ripngd.h" | |
35 | #include "ripngd/ripng_nexthop.h" | |
36 | ||
b3a7e30d DL |
37 | DEFINE_MTYPE_STATIC(RIPNGD, RIPNG_PEER, "RIPng peer") |
38 | ||
d62a17ae | 39 | static struct ripng_peer *ripng_peer_new(void) |
a94434b6 | 40 | { |
d62a17ae | 41 | return XCALLOC(MTYPE_RIPNG_PEER, sizeof(struct ripng_peer)); |
a94434b6 | 42 | } |
43 | ||
d62a17ae | 44 | static void ripng_peer_free(struct ripng_peer *peer) |
a94434b6 | 45 | { |
56bf1cb2 | 46 | RIPNG_TIMER_OFF(peer->t_timeout); |
d62a17ae | 47 | XFREE(MTYPE_RIPNG_PEER, peer); |
a94434b6 | 48 | } |
49 | ||
5c84b9a5 | 50 | struct ripng_peer *ripng_peer_lookup(struct ripng *ripng, struct in6_addr *addr) |
a94434b6 | 51 | { |
d62a17ae | 52 | struct ripng_peer *peer; |
53 | struct listnode *node, *nnode; | |
54 | ||
ecece94c | 55 | for (ALL_LIST_ELEMENTS(ripng->peer_list, node, nnode, peer)) { |
d62a17ae | 56 | if (IPV6_ADDR_SAME(&peer->addr, addr)) |
57 | return peer; | |
58 | } | |
59 | return NULL; | |
a94434b6 | 60 | } |
61 | ||
5c84b9a5 RW |
62 | struct ripng_peer *ripng_peer_lookup_next(struct ripng *ripng, |
63 | struct in6_addr *addr) | |
a94434b6 | 64 | { |
d62a17ae | 65 | struct ripng_peer *peer; |
66 | struct listnode *node, *nnode; | |
67 | ||
ecece94c | 68 | for (ALL_LIST_ELEMENTS(ripng->peer_list, node, nnode, peer)) { |
d62a17ae | 69 | if (addr6_cmp(&peer->addr, addr) > 0) |
70 | return peer; | |
71 | } | |
72 | return NULL; | |
a94434b6 | 73 | } |
74 | ||
75 | /* RIPng peer is timeout. | |
76 | * Garbage collector. | |
77 | **/ | |
d62a17ae | 78 | static int ripng_peer_timeout(struct thread *t) |
a94434b6 | 79 | { |
d62a17ae | 80 | struct ripng_peer *peer; |
a94434b6 | 81 | |
d62a17ae | 82 | peer = THREAD_ARG(t); |
5c84b9a5 | 83 | listnode_delete(peer->ripng->peer_list, peer); |
d62a17ae | 84 | ripng_peer_free(peer); |
a94434b6 | 85 | |
d62a17ae | 86 | return 0; |
a94434b6 | 87 | } |
88 | ||
89 | /* Get RIPng peer. At the same time update timeout thread. */ | |
5c84b9a5 RW |
90 | static struct ripng_peer *ripng_peer_get(struct ripng *ripng, |
91 | struct in6_addr *addr) | |
a94434b6 | 92 | { |
d62a17ae | 93 | struct ripng_peer *peer; |
94 | ||
5c84b9a5 | 95 | peer = ripng_peer_lookup(ripng, addr); |
d62a17ae | 96 | |
97 | if (peer) { | |
98 | if (peer->t_timeout) | |
99 | thread_cancel(peer->t_timeout); | |
100 | } else { | |
101 | peer = ripng_peer_new(); | |
5c84b9a5 RW |
102 | peer->ripng = ripng; |
103 | peer->addr = *addr; | |
ecece94c | 104 | listnode_add_sort(ripng->peer_list, peer); |
d62a17ae | 105 | } |
106 | ||
107 | /* Update timeout thread. */ | |
108 | peer->t_timeout = NULL; | |
109 | thread_add_timer(master, ripng_peer_timeout, peer, | |
110 | RIPNG_PEER_TIMER_DEFAULT, &peer->t_timeout); | |
111 | ||
112 | /* Last update time set. */ | |
113 | time(&peer->uptime); | |
114 | ||
115 | return peer; | |
a94434b6 | 116 | } |
117 | ||
5c84b9a5 RW |
118 | void ripng_peer_update(struct ripng *ripng, struct sockaddr_in6 *from, |
119 | uint8_t version) | |
a94434b6 | 120 | { |
d62a17ae | 121 | struct ripng_peer *peer; |
5c84b9a5 | 122 | peer = ripng_peer_get(ripng, &from->sin6_addr); |
d62a17ae | 123 | peer->version = version; |
a94434b6 | 124 | } |
125 | ||
5c84b9a5 | 126 | void ripng_peer_bad_route(struct ripng *ripng, struct sockaddr_in6 *from) |
a94434b6 | 127 | { |
d62a17ae | 128 | struct ripng_peer *peer; |
5c84b9a5 | 129 | peer = ripng_peer_get(ripng, &from->sin6_addr); |
d62a17ae | 130 | peer->recv_badroutes++; |
a94434b6 | 131 | } |
132 | ||
5c84b9a5 | 133 | void ripng_peer_bad_packet(struct ripng *ripng, struct sockaddr_in6 *from) |
a94434b6 | 134 | { |
d62a17ae | 135 | struct ripng_peer *peer; |
5c84b9a5 | 136 | peer = ripng_peer_get(ripng, &from->sin6_addr); |
d62a17ae | 137 | peer->recv_badpackets++; |
a94434b6 | 138 | } |
139 | ||
140 | /* Display peer uptime. */ | |
d62a17ae | 141 | static char *ripng_peer_uptime(struct ripng_peer *peer, char *buf, size_t len) |
a94434b6 | 142 | { |
d62a17ae | 143 | time_t uptime; |
d62a17ae | 144 | |
145 | /* If there is no connection has been done before print `never'. */ | |
146 | if (peer->uptime == 0) { | |
147 | snprintf(buf, len, "never "); | |
148 | return buf; | |
149 | } | |
150 | ||
151 | /* Get current time. */ | |
152 | uptime = time(NULL); | |
153 | uptime -= peer->uptime; | |
d0636ead MS |
154 | |
155 | frrtime_to_interval(uptime, buf, len); | |
156 | ||
d62a17ae | 157 | return buf; |
a94434b6 | 158 | } |
159 | ||
5c84b9a5 | 160 | void ripng_peer_display(struct vty *vty, struct ripng *ripng) |
a94434b6 | 161 | { |
d62a17ae | 162 | struct ripng_peer *peer; |
163 | struct listnode *node, *nnode; | |
a94434b6 | 164 | #define RIPNG_UPTIME_LEN 25 |
d62a17ae | 165 | char timebuf[RIPNG_UPTIME_LEN]; |
166 | ||
ecece94c | 167 | for (ALL_LIST_ELEMENTS(ripng->peer_list, node, nnode, peer)) { |
d62a17ae | 168 | vty_out(vty, " %s \n%14s %10d %10d %10d %s\n", |
169 | inet6_ntoa(peer->addr), " ", peer->recv_badpackets, | |
170 | peer->recv_badroutes, ZEBRA_RIPNG_DISTANCE_DEFAULT, | |
171 | ripng_peer_uptime(peer, timebuf, RIPNG_UPTIME_LEN)); | |
172 | } | |
a94434b6 | 173 | } |
174 | ||
ecece94c | 175 | int ripng_peer_list_cmp(struct ripng_peer *p1, struct ripng_peer *p2) |
a94434b6 | 176 | { |
27fa3398 | 177 | return memcmp(&p1->addr, &p2->addr, sizeof(struct in6_addr)); |
a94434b6 | 178 | } |
56bf1cb2 RW |
179 | |
180 | void ripng_peer_list_del(void *arg) | |
181 | { | |
182 | ripng_peer_free(arg); | |
183 | } |