]> git.proxmox.com Git - mirror_frr.git/blame - nhrpd/nhrp_event.c
sharpd: Allow sharpd to accept nexthop group as part of route install
[mirror_frr.git] / nhrpd / nhrp_event.c
CommitLineData
2fb975da
TT
1/* NHRP event manager
2 * Copyright (c) 2014-2015 Timo Teräs
3 *
4 * This file is free software: you may copy, redistribute and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
b45ac5f5
DL
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
2fb975da
TT
14#include <string.h>
15#include <sys/socket.h>
16#include <sys/un.h>
17
18#include "thread.h"
19#include "zbuf.h"
20#include "log.h"
21#include "nhrpd.h"
22
23const char *nhrp_event_socket_path;
24struct nhrp_reqid_pool nhrp_event_reqid;
25
26struct event_manager {
27 struct thread *t_reconnect, *t_read, *t_write;
28 struct zbuf ibuf;
29 struct zbuf_queue obuf;
30 int fd;
996c9314 31 uint8_t ibuf_data[4 * 1024];
2fb975da
TT
32};
33
34static int evmgr_reconnect(struct thread *t);
35
36static void evmgr_connection_error(struct event_manager *evmgr)
37{
38 THREAD_OFF(evmgr->t_read);
39 THREAD_OFF(evmgr->t_write);
40 zbuf_reset(&evmgr->ibuf);
41 zbufq_reset(&evmgr->obuf);
42
43 if (evmgr->fd >= 0)
44 close(evmgr->fd);
45 evmgr->fd = -1;
46 if (nhrp_event_socket_path)
ffa2c898
QY
47 thread_add_timer_msec(master, evmgr_reconnect, evmgr, 10,
48 &evmgr->t_reconnect);
2fb975da
TT
49}
50
51static void evmgr_recv_message(struct event_manager *evmgr, struct zbuf *zb)
52{
53 struct zbuf zl;
54 uint32_t eventid = 0;
55 size_t len;
56 char buf[256], result[64] = "";
57
58 while (zbuf_may_pull_until(zb, "\n", &zl)) {
59 len = zbuf_used(&zl) - 1;
996c9314 60 if (len >= sizeof(buf) - 1)
2fb975da
TT
61 continue;
62 memcpy(buf, zbuf_pulln(&zl, len), len);
63 buf[len] = 0;
64
65 debugf(NHRP_DEBUG_EVENT, "evmgr: msg: %s", buf);
0651460e 66 if (sscanf(buf, "eventid=%" SCNu32, &eventid) != 1)
6c8ca260
JB
67 continue;
68 if (sscanf(buf, "result=%63s", result) != 1)
69 continue;
2fb975da 70 }
996c9314
LB
71 debugf(NHRP_DEBUG_EVENT, "evmgr: received: eventid=%d result=%s",
72 eventid, result);
2fb975da 73 if (eventid && result[0]) {
996c9314
LB
74 struct nhrp_reqid *r =
75 nhrp_reqid_lookup(&nhrp_event_reqid, eventid);
76 if (r)
77 r->cb(r, result);
2fb975da
TT
78 }
79}
80
81static int evmgr_read(struct thread *t)
82{
83 struct event_manager *evmgr = THREAD_ARG(t);
84 struct zbuf *ibuf = &evmgr->ibuf;
85 struct zbuf msg;
86
87 evmgr->t_read = NULL;
996c9314 88 if (zbuf_read(ibuf, evmgr->fd, (size_t)-1) < 0) {
2fb975da
TT
89 evmgr_connection_error(evmgr);
90 return 0;
91 }
92
93 /* Process all messages in buffer */
94 while (zbuf_may_pull_until(ibuf, "\n\n", &msg))
95 evmgr_recv_message(evmgr, &msg);
96
ffa2c898 97 thread_add_read(master, evmgr_read, evmgr, evmgr->fd, &evmgr->t_read);
2fb975da
TT
98 return 0;
99}
100
101static int evmgr_write(struct thread *t)
102{
103 struct event_manager *evmgr = THREAD_ARG(t);
104 int r;
105
106 evmgr->t_write = NULL;
107 r = zbufq_write(&evmgr->obuf, evmgr->fd);
108 if (r > 0) {
ffa2c898
QY
109 thread_add_write(master, evmgr_write, evmgr, evmgr->fd,
110 &evmgr->t_write);
2fb975da
TT
111 } else if (r < 0) {
112 evmgr_connection_error(evmgr);
113 }
114
115 return 0;
116}
117
118static void evmgr_hexdump(struct zbuf *zb, const uint8_t *val, size_t vallen)
119{
120 static const char xd[] = "0123456789abcdef";
121 size_t i;
122 char *ptr;
123
996c9314
LB
124 ptr = zbuf_pushn(zb, 2 * vallen);
125 if (!ptr)
126 return;
2fb975da
TT
127
128 for (i = 0; i < vallen; i++) {
129 uint8_t b = val[i];
130 *(ptr++) = xd[b >> 4];
131 *(ptr++) = xd[b & 0xf];
132 }
133}
134
135static void evmgr_put(struct zbuf *zb, const char *fmt, ...)
136{
137 const char *pos, *nxt, *str;
138 const uint8_t *bin;
139 const union sockunion *su;
140 int len;
141 va_list va;
142
143 va_start(va, fmt);
144 for (pos = fmt; (nxt = strchr(pos, '%')) != NULL; pos = nxt + 2) {
996c9314 145 zbuf_put(zb, pos, nxt - pos);
2fb975da
TT
146 switch (nxt[1]) {
147 case '%':
148 zbuf_put8(zb, '%');
149 break;
150 case 'u':
996c9314
LB
151 zb->tail +=
152 snprintf((char *)zb->tail, zbuf_tailroom(zb),
153 "%u", va_arg(va, uint32_t));
2fb975da
TT
154 break;
155 case 's':
156 str = va_arg(va, const char *);
157 zbuf_put(zb, str, strlen(str));
158 break;
159 case 'U':
160 su = va_arg(va, const union sockunion *);
996c9314
LB
161 if (sockunion2str(su, (char *)zb->tail,
162 zbuf_tailroom(zb)))
163 zb->tail += strlen((char *)zb->tail);
2fb975da
TT
164 else
165 zbuf_set_werror(zb);
166 break;
167 case 'H':
168 bin = va_arg(va, const uint8_t *);
169 len = va_arg(va, int);
170 evmgr_hexdump(zb, bin, len);
171 break;
172 }
173 }
174 va_end(va);
175 zbuf_put(zb, pos, strlen(pos));
176}
177
178static void evmgr_submit(struct event_manager *evmgr, struct zbuf *obuf)
179{
180 if (obuf->error) {
181 zbuf_free(obuf);
182 return;
183 }
184 zbuf_put(obuf, "\n", 1);
185 zbufq_queue(&evmgr->obuf, obuf);
186 if (evmgr->fd >= 0)
ffa2c898
QY
187 thread_add_write(master, evmgr_write, evmgr, evmgr->fd,
188 &evmgr->t_write);
2fb975da
TT
189}
190
191static int evmgr_reconnect(struct thread *t)
192{
193 struct event_manager *evmgr = THREAD_ARG(t);
194 int fd;
195
196 evmgr->t_reconnect = NULL;
996c9314
LB
197 if (evmgr->fd >= 0 || !nhrp_event_socket_path)
198 return 0;
2fb975da
TT
199
200 fd = sock_open_unix(nhrp_event_socket_path);
201 if (fd < 0) {
202 zlog_warn("%s: failure connecting nhrp-event socket: %s",
996c9314 203 __PRETTY_FUNCTION__, strerror(errno));
2fb975da 204 zbufq_reset(&evmgr->obuf);
ffa2c898
QY
205 thread_add_timer(master, evmgr_reconnect, evmgr, 10,
206 &evmgr->t_reconnect);
2fb975da
TT
207 return 0;
208 }
209
210 zlog_info("Connected to Event Manager");
211 evmgr->fd = fd;
ffa2c898 212 thread_add_read(master, evmgr_read, evmgr, evmgr->fd, &evmgr->t_read);
2fb975da
TT
213
214 return 0;
215}
216
217static struct event_manager evmgr_connection;
218
219void evmgr_init(void)
220{
221 struct event_manager *evmgr = &evmgr_connection;
222
223 evmgr->fd = -1;
224 zbuf_init(&evmgr->ibuf, evmgr->ibuf_data, sizeof(evmgr->ibuf_data), 0);
225 zbufq_init(&evmgr->obuf);
ffa2c898
QY
226 thread_add_timer_msec(master, evmgr_reconnect, evmgr, 10,
227 &evmgr->t_reconnect);
2fb975da
TT
228}
229
230void evmgr_set_socket(const char *socket)
231{
d258c885 232 if (nhrp_event_socket_path) {
996c9314 233 free((char *)nhrp_event_socket_path);
d258c885
DS
234 nhrp_event_socket_path = NULL;
235 }
236 if (socket)
237 nhrp_event_socket_path = strdup(socket);
2fb975da
TT
238 evmgr_connection_error(&evmgr_connection);
239}
240
241void evmgr_terminate(void)
242{
243}
244
996c9314
LB
245void evmgr_notify(const char *name, struct nhrp_cache *c,
246 void (*cb)(struct nhrp_reqid *, void *))
2fb975da
TT
247{
248 struct event_manager *evmgr = &evmgr_connection;
249 struct nhrp_vc *vc;
250 struct nhrp_interface *nifp = c->ifp->info;
251 struct zbuf *zb;
252 afi_t afi = family2afi(sockunion_family(&c->remote_addr));
253
254 if (!nhrp_event_socket_path) {
996c9314 255 cb(&c->eventid, (void *)"accept");
2fb975da
TT
256 return;
257 }
258
259 debugf(NHRP_DEBUG_EVENT, "evmgr: sending event %s", name);
260
261 vc = c->new.peer ? c->new.peer->vc : NULL;
996c9314
LB
262 zb = zbuf_alloc(
263 1024 + (vc ? (vc->local.certlen + vc->remote.certlen) * 2 : 0));
2fb975da
TT
264
265 if (cb) {
266 nhrp_reqid_free(&nhrp_event_reqid, &c->eventid);
996c9314
LB
267 evmgr_put(zb, "eventid=%u\n",
268 nhrp_reqid_alloc(&nhrp_event_reqid, &c->eventid, cb));
2fb975da
TT
269 }
270
271 evmgr_put(zb,
996c9314
LB
272 "event=%s\n"
273 "type=%s\n"
274 "old_type=%s\n"
275 "num_nhs=%u\n"
276 "interface=%s\n"
277 "local_addr=%U\n",
278 name, nhrp_cache_type_str[c->new.type],
279 nhrp_cache_type_str[c->cur.type],
280 (unsigned int)nhrp_cache_counts[NHRP_CACHE_NHS], c->ifp->name,
281 &nifp->afi[afi].addr);
2fb975da
TT
282
283 if (vc) {
284 evmgr_put(zb,
996c9314
LB
285 "vc_initiated=%s\n"
286 "local_nbma=%U\n"
287 "local_cert=%H\n"
288 "remote_addr=%U\n"
289 "remote_nbma=%U\n"
290 "remote_cert=%H\n",
291 c->new.peer->requested ? "yes" : "no",
292 &vc->local.nbma, vc->local.cert, vc->local.certlen,
293 &c->remote_addr, &vc->remote.nbma, vc->remote.cert,
294 vc->remote.certlen);
2fb975da
TT
295 }
296
297 evmgr_submit(evmgr, zb);
298}