]>
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 | ||
37 | ||
38 | /* Linked list of RIPng peer. */ | |
39 | struct list *peer_list; | |
6b0655a2 | 40 | |
d62a17ae | 41 | static struct ripng_peer *ripng_peer_new(void) |
a94434b6 | 42 | { |
d62a17ae | 43 | return XCALLOC(MTYPE_RIPNG_PEER, sizeof(struct ripng_peer)); |
a94434b6 | 44 | } |
45 | ||
d62a17ae | 46 | static void ripng_peer_free(struct ripng_peer *peer) |
a94434b6 | 47 | { |
d62a17ae | 48 | XFREE(MTYPE_RIPNG_PEER, peer); |
a94434b6 | 49 | } |
50 | ||
d62a17ae | 51 | struct ripng_peer *ripng_peer_lookup(struct in6_addr *addr) |
a94434b6 | 52 | { |
d62a17ae | 53 | struct ripng_peer *peer; |
54 | struct listnode *node, *nnode; | |
55 | ||
56 | for (ALL_LIST_ELEMENTS(peer_list, node, nnode, peer)) { | |
57 | if (IPV6_ADDR_SAME(&peer->addr, addr)) | |
58 | return peer; | |
59 | } | |
60 | return NULL; | |
a94434b6 | 61 | } |
62 | ||
d62a17ae | 63 | struct ripng_peer *ripng_peer_lookup_next(struct in6_addr *addr) |
a94434b6 | 64 | { |
d62a17ae | 65 | struct ripng_peer *peer; |
66 | struct listnode *node, *nnode; | |
67 | ||
68 | for (ALL_LIST_ELEMENTS(peer_list, node, nnode, peer)) { | |
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); |
83 | listnode_delete(peer_list, peer); | |
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. */ | |
d62a17ae | 90 | static struct ripng_peer *ripng_peer_get(struct in6_addr *addr) |
a94434b6 | 91 | { |
d62a17ae | 92 | struct ripng_peer *peer; |
93 | ||
94 | peer = ripng_peer_lookup(addr); | |
95 | ||
96 | if (peer) { | |
97 | if (peer->t_timeout) | |
98 | thread_cancel(peer->t_timeout); | |
99 | } else { | |
100 | peer = ripng_peer_new(); | |
101 | peer->addr = *addr; /* XXX */ | |
102 | listnode_add_sort(peer_list, peer); | |
103 | } | |
104 | ||
105 | /* Update timeout thread. */ | |
106 | peer->t_timeout = NULL; | |
107 | thread_add_timer(master, ripng_peer_timeout, peer, | |
108 | RIPNG_PEER_TIMER_DEFAULT, &peer->t_timeout); | |
109 | ||
110 | /* Last update time set. */ | |
111 | time(&peer->uptime); | |
112 | ||
113 | return peer; | |
a94434b6 | 114 | } |
115 | ||
d7c0a89a | 116 | void ripng_peer_update(struct sockaddr_in6 *from, uint8_t version) |
a94434b6 | 117 | { |
d62a17ae | 118 | struct ripng_peer *peer; |
119 | peer = ripng_peer_get(&from->sin6_addr); | |
120 | peer->version = version; | |
a94434b6 | 121 | } |
122 | ||
d62a17ae | 123 | void ripng_peer_bad_route(struct sockaddr_in6 *from) |
a94434b6 | 124 | { |
d62a17ae | 125 | struct ripng_peer *peer; |
126 | peer = ripng_peer_get(&from->sin6_addr); | |
127 | peer->recv_badroutes++; | |
a94434b6 | 128 | } |
129 | ||
d62a17ae | 130 | void ripng_peer_bad_packet(struct sockaddr_in6 *from) |
a94434b6 | 131 | { |
d62a17ae | 132 | struct ripng_peer *peer; |
133 | peer = ripng_peer_get(&from->sin6_addr); | |
134 | peer->recv_badpackets++; | |
a94434b6 | 135 | } |
136 | ||
137 | /* Display peer uptime. */ | |
d62a17ae | 138 | static char *ripng_peer_uptime(struct ripng_peer *peer, char *buf, size_t len) |
a94434b6 | 139 | { |
d62a17ae | 140 | time_t uptime; |
141 | struct tm *tm; | |
142 | ||
143 | /* If there is no connection has been done before print `never'. */ | |
144 | if (peer->uptime == 0) { | |
145 | snprintf(buf, len, "never "); | |
146 | return buf; | |
147 | } | |
148 | ||
149 | /* Get current time. */ | |
150 | uptime = time(NULL); | |
151 | uptime -= peer->uptime; | |
152 | tm = gmtime(&uptime); | |
153 | ||
d62a17ae | 154 | if (uptime < ONE_DAY_SECOND) |
155 | snprintf(buf, len, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, | |
156 | tm->tm_sec); | |
157 | else if (uptime < ONE_WEEK_SECOND) | |
158 | snprintf(buf, len, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour, | |
159 | tm->tm_min); | |
160 | else | |
161 | snprintf(buf, len, "%02dw%dd%02dh", tm->tm_yday / 7, | |
162 | tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour); | |
163 | return buf; | |
a94434b6 | 164 | } |
165 | ||
d62a17ae | 166 | void ripng_peer_display(struct vty *vty) |
a94434b6 | 167 | { |
d62a17ae | 168 | struct ripng_peer *peer; |
169 | struct listnode *node, *nnode; | |
a94434b6 | 170 | #define RIPNG_UPTIME_LEN 25 |
d62a17ae | 171 | char timebuf[RIPNG_UPTIME_LEN]; |
172 | ||
173 | for (ALL_LIST_ELEMENTS(peer_list, node, nnode, peer)) { | |
174 | vty_out(vty, " %s \n%14s %10d %10d %10d %s\n", | |
175 | inet6_ntoa(peer->addr), " ", peer->recv_badpackets, | |
176 | peer->recv_badroutes, ZEBRA_RIPNG_DISTANCE_DEFAULT, | |
177 | ripng_peer_uptime(peer, timebuf, RIPNG_UPTIME_LEN)); | |
178 | } | |
a94434b6 | 179 | } |
180 | ||
d62a17ae | 181 | static int ripng_peer_list_cmp(struct ripng_peer *p1, struct ripng_peer *p2) |
a94434b6 | 182 | { |
27fa3398 | 183 | return memcmp(&p1->addr, &p2->addr, sizeof(struct in6_addr)); |
a94434b6 | 184 | } |
185 | ||
4d762f26 | 186 | void ripng_peer_init(void) |
a94434b6 | 187 | { |
d62a17ae | 188 | peer_list = list_new(); |
189 | peer_list->cmp = (int (*)(void *, void *))ripng_peer_list_cmp; | |
a94434b6 | 190 | } |