]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: ISC | |
2 | /* $OpenBSD$ */ | |
3 | ||
4 | /* | |
5 | * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org> | |
6 | * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org> | |
7 | * Copyright (c) 2004, 2005, 2008 Esben Norby <norby@openbsd.org> | |
8 | */ | |
9 | ||
10 | #include <zebra.h> | |
11 | ||
12 | #include "ldpd.h" | |
13 | #include "ldpe.h" | |
14 | #include "log.h" | |
15 | ||
16 | #include "sockopt.h" | |
17 | ||
18 | static struct iface *disc_find_iface(unsigned int, int, union ldpd_addr *); | |
19 | static void session_read(struct event *thread); | |
20 | static void session_write(struct event *thread); | |
21 | static ssize_t session_get_pdu(struct ibuf_read *, char **); | |
22 | static void tcp_close(struct tcp_conn *); | |
23 | static struct pending_conn *pending_conn_new(int, int, union ldpd_addr *); | |
24 | static void pending_conn_timeout(struct event *thread); | |
25 | ||
26 | int | |
27 | gen_ldp_hdr(struct ibuf *buf, uint16_t size) | |
28 | { | |
29 | struct ldp_hdr ldp_hdr; | |
30 | ||
31 | memset(&ldp_hdr, 0, sizeof(ldp_hdr)); | |
32 | ldp_hdr.version = htons(LDP_VERSION); | |
33 | /* exclude the 'Version' and 'PDU Length' fields from the total */ | |
34 | ldp_hdr.length = htons(size - LDP_HDR_DEAD_LEN); | |
35 | ldp_hdr.lsr_id = ldp_rtr_id_get(leconf); | |
36 | ldp_hdr.lspace_id = 0; | |
37 | ||
38 | return (ibuf_add(buf, &ldp_hdr, LDP_HDR_SIZE)); | |
39 | } | |
40 | ||
41 | int | |
42 | gen_msg_hdr(struct ibuf *buf, uint16_t type, uint16_t size) | |
43 | { | |
44 | static int msgcnt = 0; | |
45 | struct ldp_msg msg; | |
46 | ||
47 | memset(&msg, 0, sizeof(msg)); | |
48 | msg.type = htons(type); | |
49 | /* exclude the 'Type' and 'Length' fields from the total */ | |
50 | msg.length = htons(size - LDP_MSG_DEAD_LEN); | |
51 | msg.id = htonl(++msgcnt); | |
52 | ||
53 | return (ibuf_add(buf, &msg, sizeof(msg))); | |
54 | } | |
55 | ||
56 | /* send packets */ | |
57 | int | |
58 | send_packet(int fd, int af, union ldpd_addr *dst, struct iface_af *ia, | |
59 | void *pkt, size_t len) | |
60 | { | |
61 | union sockunion su; | |
62 | ||
63 | switch (af) { | |
64 | case AF_INET: | |
65 | if (ia && IN_MULTICAST(ntohl(dst->v4.s_addr))) { | |
66 | /* set outgoing interface for multicast traffic */ | |
67 | if (sock_set_ipv4_mcast(ia->iface) == -1) { | |
68 | log_debug("%s: error setting multicast interface, %s", __func__, ia->iface->name); | |
69 | return (-1); | |
70 | } | |
71 | } | |
72 | break; | |
73 | case AF_INET6: | |
74 | if (ia && IN6_IS_ADDR_MULTICAST(&dst->v6)) { | |
75 | /* set outgoing interface for multicast traffic */ | |
76 | if (sock_set_ipv6_mcast(ia->iface) == -1) { | |
77 | log_debug("%s: error setting multicast interface, %s", __func__, ia->iface->name); | |
78 | return (-1); | |
79 | } | |
80 | } | |
81 | break; | |
82 | default: | |
83 | fatalx("send_packet: unknown af"); | |
84 | } | |
85 | ||
86 | addr2sa(af, dst, LDP_PORT, &su); | |
87 | if (sendto(fd, pkt, len, 0, &su.sa, sockaddr_len(&su.sa)) == -1) { | |
88 | log_warn("%s: error sending packet to %s", __func__, | |
89 | log_sockaddr(&su.sa)); | |
90 | return (-1); | |
91 | } | |
92 | ||
93 | return (0); | |
94 | } | |
95 | ||
96 | /* Discovery functions */ | |
97 | void disc_recv_packet(struct event *thread) | |
98 | { | |
99 | int fd = EVENT_FD(thread); | |
100 | struct event **threadp = EVENT_ARG(thread); | |
101 | ||
102 | union { | |
103 | struct cmsghdr hdr; | |
104 | #ifdef HAVE_STRUCT_SOCKADDR_DL | |
105 | char buf[CMSG_SPACE(sizeof(struct sockaddr_dl))]; | |
106 | #else | |
107 | char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; | |
108 | #endif | |
109 | } cmsgbuf; | |
110 | struct msghdr m; | |
111 | struct sockaddr_storage from; | |
112 | struct iovec iov; | |
113 | char *buf; | |
114 | #ifndef MSG_MCAST | |
115 | struct cmsghdr *cmsg; | |
116 | #endif | |
117 | ssize_t r; | |
118 | int multicast; | |
119 | int af; | |
120 | union ldpd_addr src; | |
121 | unsigned int ifindex = 0; | |
122 | struct iface *iface = NULL; | |
123 | uint16_t len; | |
124 | struct ldp_hdr ldp_hdr; | |
125 | uint16_t pdu_len; | |
126 | struct ldp_msg msg; | |
127 | uint16_t msg_len; | |
128 | struct in_addr lsr_id; | |
129 | ||
130 | /* reschedule read */ | |
131 | event_add_read(master, disc_recv_packet, threadp, fd, threadp); | |
132 | ||
133 | /* setup buffer */ | |
134 | memset(&m, 0, sizeof(m)); | |
135 | iov.iov_base = buf = pkt_ptr; | |
136 | iov.iov_len = IBUF_READ_SIZE; | |
137 | m.msg_name = &from; | |
138 | m.msg_namelen = sizeof(from); | |
139 | m.msg_iov = &iov; | |
140 | m.msg_iovlen = 1; | |
141 | m.msg_control = &cmsgbuf.buf; | |
142 | m.msg_controllen = sizeof(cmsgbuf.buf); | |
143 | ||
144 | if ((r = recvmsg(fd, &m, 0)) == -1) { | |
145 | if (errno != EAGAIN && errno != EINTR) | |
146 | log_debug("%s: read error: %s", __func__, strerror(errno)); | |
147 | return; | |
148 | } | |
149 | ||
150 | sa2addr((struct sockaddr *)&from, &af, &src, NULL); | |
151 | #ifdef MSG_MCAST | |
152 | multicast = (m.msg_flags & MSG_MCAST) ? 1 : 0; | |
153 | #else | |
154 | multicast = 0; | |
155 | for (cmsg = CMSG_FIRSTHDR(&m); cmsg != NULL; cmsg = CMSG_NXTHDR(&m, cmsg)) { | |
156 | #if defined(HAVE_IP_PKTINFO) | |
157 | if (af == AF_INET && | |
158 | cmsg->cmsg_level == IPPROTO_IP && | |
159 | cmsg->cmsg_type == IP_PKTINFO) { | |
160 | struct in_pktinfo *pktinfo; | |
161 | ||
162 | pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg); | |
163 | if (IN_MULTICAST(ntohl(pktinfo->ipi_addr.s_addr))) | |
164 | multicast = 1; | |
165 | break; | |
166 | } | |
167 | #elif defined(HAVE_IP_RECVDSTADDR) | |
168 | if (af == AF_INET && | |
169 | cmsg->cmsg_level == IPPROTO_IP && | |
170 | cmsg->cmsg_type == IP_RECVDSTADDR) { | |
171 | struct in_addr *addr; | |
172 | ||
173 | addr = (struct in_addr *)CMSG_DATA(cmsg); | |
174 | if (IN_MULTICAST(ntohl(addr->s_addr))) | |
175 | multicast = 1; | |
176 | break; | |
177 | } | |
178 | #else | |
179 | #error "Unsupported socket API" | |
180 | #endif | |
181 | if (af == AF_INET6 && | |
182 | cmsg->cmsg_level == IPPROTO_IPV6 && | |
183 | cmsg->cmsg_type == IPV6_PKTINFO) { | |
184 | struct in6_pktinfo *pktinfo; | |
185 | ||
186 | pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg); | |
187 | if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr)) | |
188 | multicast = 1; | |
189 | break; | |
190 | } | |
191 | } | |
192 | #endif /* MSG_MCAST */ | |
193 | if (bad_addr(af, &src)) { | |
194 | log_debug("%s: invalid source address: %s", __func__, log_addr(af, &src)); | |
195 | return; | |
196 | } | |
197 | ifindex = getsockopt_ifindex(af, &m); | |
198 | ||
199 | /* find a matching interface */ | |
200 | if (multicast) { | |
201 | iface = disc_find_iface(ifindex, af, &src); | |
202 | if (iface == NULL) | |
203 | return; | |
204 | } | |
205 | ||
206 | /* check packet size */ | |
207 | len = (uint16_t)r; | |
208 | if (len < (LDP_HDR_SIZE + LDP_MSG_SIZE) || len > LDP_MAX_LEN) { | |
209 | log_debug("%s: bad packet size, source %s", __func__, log_addr(af, &src)); | |
210 | return; | |
211 | } | |
212 | ||
213 | /* LDP header sanity checks */ | |
214 | memcpy(&ldp_hdr, buf, sizeof(ldp_hdr)); | |
215 | if (ntohs(ldp_hdr.version) != LDP_VERSION) { | |
216 | log_debug("%s: invalid LDP version %d, source %s", __func__, | |
217 | ntohs(ldp_hdr.version), log_addr(af, &src)); | |
218 | return; | |
219 | } | |
220 | if (ntohs(ldp_hdr.lspace_id) != 0) { | |
221 | log_debug("%s: invalid label space %u, source %s", __func__, | |
222 | ntohs(ldp_hdr.lspace_id), log_addr(af, &src)); | |
223 | return; | |
224 | } | |
225 | /* check "PDU Length" field */ | |
226 | pdu_len = ntohs(ldp_hdr.length); | |
227 | if ((pdu_len < (LDP_HDR_PDU_LEN + LDP_MSG_SIZE)) || | |
228 | (pdu_len > (len - LDP_HDR_DEAD_LEN))) { | |
229 | log_debug("%s: invalid LDP packet length %u, source %s", | |
230 | __func__, ntohs(ldp_hdr.length), log_addr(af, &src)); | |
231 | return; | |
232 | } | |
233 | buf += LDP_HDR_SIZE; | |
234 | len -= LDP_HDR_SIZE; | |
235 | ||
236 | lsr_id.s_addr = ldp_hdr.lsr_id; | |
237 | ||
238 | /* | |
239 | * For UDP, we process only the first message of each packet. This does | |
240 | * not impose any restrictions since LDP uses UDP only for sending Hello | |
241 | * packets. | |
242 | */ | |
243 | memcpy(&msg, buf, sizeof(msg)); | |
244 | ||
245 | /* check "Message Length" field */ | |
246 | msg_len = ntohs(msg.length); | |
247 | if (msg_len < LDP_MSG_LEN || ((msg_len + LDP_MSG_DEAD_LEN) > pdu_len)) { | |
248 | log_debug("%s: invalid LDP message length %u, source %s", | |
249 | __func__, ntohs(msg.length), log_addr(af, &src)); | |
250 | return; | |
251 | } | |
252 | buf += LDP_MSG_SIZE; | |
253 | len -= LDP_MSG_SIZE; | |
254 | ||
255 | /* switch LDP packet type */ | |
256 | switch (ntohs(msg.type)) { | |
257 | case MSG_TYPE_HELLO: | |
258 | recv_hello(lsr_id, &msg, af, &src, iface, multicast, buf, len); | |
259 | break; | |
260 | default: | |
261 | log_debug("%s: unknown LDP packet type, source %s", __func__, | |
262 | log_addr(af, &src)); | |
263 | } | |
264 | } | |
265 | ||
266 | static struct iface * | |
267 | disc_find_iface(unsigned int ifindex, int af, union ldpd_addr *src) | |
268 | { | |
269 | struct iface *iface; | |
270 | struct iface_af *ia; | |
271 | ||
272 | iface = if_lookup(leconf, ifindex); | |
273 | if (iface == NULL) | |
274 | return (NULL); | |
275 | ||
276 | ia = iface_af_get(iface, af); | |
277 | if (!ia->enabled) | |
278 | return (NULL); | |
279 | ||
280 | /* | |
281 | * RFC 7552 - Section 5.1: | |
282 | * "Link-local IPv6 address MUST be used as the source IP address in | |
283 | * IPv6 LDP Link Hellos". | |
284 | */ | |
285 | if (af == AF_INET6 && !IN6_IS_ADDR_LINKLOCAL(&src->v6)) | |
286 | return (NULL); | |
287 | ||
288 | return (iface); | |
289 | } | |
290 | ||
291 | void session_accept(struct event *thread) | |
292 | { | |
293 | int fd = EVENT_FD(thread); | |
294 | struct sockaddr_storage src; | |
295 | socklen_t len = sizeof(src); | |
296 | int newfd; | |
297 | int af; | |
298 | union ldpd_addr addr; | |
299 | struct nbr *nbr; | |
300 | struct pending_conn *pconn; | |
301 | ||
302 | newfd = accept(fd, (struct sockaddr *)&src, &len); | |
303 | if (newfd == -1) { | |
304 | /* | |
305 | * Pause accept if we are out of file descriptors, or | |
306 | * libevent will haunt us here too. | |
307 | */ | |
308 | if (errno == ENFILE || errno == EMFILE) { | |
309 | accept_pause(); | |
310 | } else if (errno != EWOULDBLOCK && errno != EINTR && errno != ECONNABORTED) | |
311 | log_debug("%s: accept error: %s", __func__, strerror(errno)); | |
312 | return; | |
313 | } | |
314 | sock_set_nonblock(newfd); | |
315 | ||
316 | sa2addr((struct sockaddr *)&src, &af, &addr, NULL); | |
317 | ||
318 | /* | |
319 | * Since we don't support label spaces, we can identify this neighbor | |
320 | * just by its source address. This way we don't need to wait for its | |
321 | * Initialization message to know who we are talking to. | |
322 | */ | |
323 | nbr = nbr_find_addr(af, &addr); | |
324 | if (nbr == NULL) { | |
325 | /* | |
326 | * According to RFC 5036, we would need to send a No Hello | |
327 | * Error Notification message and close this TCP connection | |
328 | * right now. But doing so would trigger the backoff exponential | |
329 | * timer in the remote peer, which would considerably slow down | |
330 | * the session establishment process. The trick here is to wait | |
331 | * five seconds before sending the Notification Message. There's | |
332 | * a good chance that the remote peer will send us a Hello | |
333 | * message within this interval, so it's worth waiting before | |
334 | * taking a more drastic measure. | |
335 | */ | |
336 | pconn = pending_conn_find(af, &addr); | |
337 | if (pconn) | |
338 | close(newfd); | |
339 | else | |
340 | pending_conn_new(newfd, af, &addr); | |
341 | return; | |
342 | } | |
343 | /* protection against buggy implementations */ | |
344 | if (nbr_session_active_role(nbr)) { | |
345 | close(newfd); | |
346 | return; | |
347 | } | |
348 | if (nbr->state != NBR_STA_PRESENT) { | |
349 | log_debug("%s: lsr-id %pI4: rejecting additional transport connection", __func__, &nbr->id); | |
350 | close(newfd); | |
351 | return; | |
352 | } | |
353 | ||
354 | session_accept_nbr(nbr, newfd); | |
355 | } | |
356 | ||
357 | void | |
358 | session_accept_nbr(struct nbr *nbr, int fd) | |
359 | { | |
360 | #ifdef __OpenBSD__ | |
361 | struct nbr_params *nbrp; | |
362 | int opt; | |
363 | socklen_t len; | |
364 | ||
365 | nbrp = nbr_params_find(leconf, nbr->id); | |
366 | if (nbr_gtsm_check(fd, nbr, nbrp)) { | |
367 | close(fd); | |
368 | return; | |
369 | } | |
370 | ||
371 | if (nbrp && nbrp->auth.method == AUTH_MD5SIG) { | |
372 | if (sysdep.no_pfkey || sysdep.no_md5sig) { | |
373 | log_warnx("md5sig configured but not available"); | |
374 | close(fd); | |
375 | return; | |
376 | } | |
377 | ||
378 | len = sizeof(opt); | |
379 | if (getsockopt(fd, IPPROTO_TCP, TCP_MD5SIG, &opt, &len) == -1) | |
380 | fatal("getsockopt TCP_MD5SIG"); | |
381 | if (!opt) { /* non-md5'd connection! */ | |
382 | log_warnx("connection attempt without md5 signature"); | |
383 | close(fd); | |
384 | return; | |
385 | } | |
386 | } | |
387 | #endif | |
388 | ||
389 | nbr->tcp = tcp_new(fd, nbr); | |
390 | nbr_fsm(nbr, NBR_EVT_MATCH_ADJ); | |
391 | } | |
392 | ||
393 | static void session_read(struct event *thread) | |
394 | { | |
395 | int fd = EVENT_FD(thread); | |
396 | struct nbr *nbr = EVENT_ARG(thread); | |
397 | struct tcp_conn *tcp = nbr->tcp; | |
398 | struct ldp_hdr *ldp_hdr; | |
399 | struct ldp_msg *msg; | |
400 | char *buf = NULL, *pdu; | |
401 | ssize_t n, len; | |
402 | uint16_t pdu_len, msg_len, msg_size, max_pdu_len; | |
403 | int ret; | |
404 | ||
405 | event_add_read(master, session_read, nbr, fd, &tcp->rev); | |
406 | ||
407 | if ((n = read(fd, tcp->rbuf->buf + tcp->rbuf->wpos, | |
408 | sizeof(tcp->rbuf->buf) - tcp->rbuf->wpos)) == -1) { | |
409 | if (errno != EINTR && errno != EAGAIN) { | |
410 | log_warn("%s: read error", __func__); | |
411 | nbr_fsm(nbr, NBR_EVT_CLOSE_SESSION); | |
412 | return; | |
413 | } | |
414 | /* retry read */ | |
415 | return; | |
416 | } | |
417 | if (n == 0) { | |
418 | /* connection closed */ | |
419 | log_debug("%s: connection closed by remote end", __func__); | |
420 | nbr_fsm(nbr, NBR_EVT_CLOSE_SESSION); | |
421 | return; | |
422 | } | |
423 | tcp->rbuf->wpos += n; | |
424 | ||
425 | while ((len = session_get_pdu(tcp->rbuf, &buf)) > 0) { | |
426 | pdu = buf; | |
427 | ldp_hdr = (struct ldp_hdr *)pdu; | |
428 | if (ntohs(ldp_hdr->version) != LDP_VERSION) { | |
429 | session_shutdown(nbr, S_BAD_PROTO_VER, 0, 0); | |
430 | free(buf); | |
431 | return; | |
432 | } | |
433 | ||
434 | pdu_len = ntohs(ldp_hdr->length); | |
435 | /* | |
436 | * RFC 5036 - Section 3.5.3: | |
437 | * "Prior to completion of the negotiation, the maximum | |
438 | * allowable length is 4096 bytes". | |
439 | */ | |
440 | if (nbr->state == NBR_STA_OPER) | |
441 | max_pdu_len = nbr->max_pdu_len; | |
442 | else | |
443 | max_pdu_len = LDP_MAX_LEN; | |
444 | if (pdu_len < (LDP_HDR_PDU_LEN + LDP_MSG_SIZE) || pdu_len > max_pdu_len) { | |
445 | session_shutdown(nbr, S_BAD_PDU_LEN, 0, 0); | |
446 | free(buf); | |
447 | return; | |
448 | } | |
449 | pdu_len -= LDP_HDR_PDU_LEN; | |
450 | if (ldp_hdr->lsr_id != nbr->id.s_addr || ldp_hdr->lspace_id != 0) { | |
451 | session_shutdown(nbr, S_BAD_LDP_ID, 0, 0); | |
452 | free(buf); | |
453 | return; | |
454 | } | |
455 | pdu += LDP_HDR_SIZE; | |
456 | len -= LDP_HDR_SIZE; | |
457 | ||
458 | nbr_fsm(nbr, NBR_EVT_PDU_RCVD); | |
459 | ||
460 | while (len >= LDP_MSG_SIZE) { | |
461 | uint16_t type; | |
462 | ||
463 | msg = (struct ldp_msg *)pdu; | |
464 | type = ntohs(msg->type); | |
465 | msg_len = ntohs(msg->length); | |
466 | if (msg_len < LDP_MSG_LEN || (msg_len + LDP_MSG_DEAD_LEN) > pdu_len) { | |
467 | session_shutdown(nbr, S_BAD_MSG_LEN, msg->id, msg->type); | |
468 | free(buf); | |
469 | return; | |
470 | } | |
471 | msg_size = msg_len + LDP_MSG_DEAD_LEN; | |
472 | pdu_len -= msg_size; | |
473 | ||
474 | /* check for error conditions earlier */ | |
475 | switch (type) { | |
476 | case MSG_TYPE_INIT: | |
477 | if ((nbr->state != NBR_STA_INITIAL) && | |
478 | (nbr->state != NBR_STA_OPENSENT)) { | |
479 | session_shutdown(nbr, S_SHUTDOWN, msg->id, msg->type); | |
480 | free(buf); | |
481 | return; | |
482 | } | |
483 | break; | |
484 | case MSG_TYPE_KEEPALIVE: | |
485 | if ((nbr->state == NBR_STA_INITIAL) || | |
486 | (nbr->state == NBR_STA_OPENSENT)) { | |
487 | session_shutdown(nbr, S_SHUTDOWN, msg->id, msg->type); | |
488 | free(buf); | |
489 | return; | |
490 | } | |
491 | break; | |
492 | case MSG_TYPE_NOTIFICATION: | |
493 | break; | |
494 | default: | |
495 | if (nbr->state != NBR_STA_OPER) { | |
496 | session_shutdown(nbr, S_SHUTDOWN, msg->id, msg->type); | |
497 | free(buf); | |
498 | return; | |
499 | } | |
500 | break; | |
501 | } | |
502 | ||
503 | /* switch LDP packet type */ | |
504 | switch (type) { | |
505 | case MSG_TYPE_NOTIFICATION: | |
506 | ret = recv_notification(nbr, pdu, msg_size); | |
507 | break; | |
508 | case MSG_TYPE_INIT: | |
509 | ret = recv_init(nbr, pdu, msg_size); | |
510 | break; | |
511 | case MSG_TYPE_KEEPALIVE: | |
512 | ret = recv_keepalive(nbr, pdu, msg_size); | |
513 | break; | |
514 | case MSG_TYPE_CAPABILITY: | |
515 | ret = recv_capability(nbr, pdu, msg_size); | |
516 | break; | |
517 | case MSG_TYPE_ADDR: | |
518 | case MSG_TYPE_ADDRWITHDRAW: | |
519 | ret = recv_address(nbr, pdu, msg_size); | |
520 | break; | |
521 | case MSG_TYPE_LABELMAPPING: | |
522 | case MSG_TYPE_LABELREQUEST: | |
523 | case MSG_TYPE_LABELWITHDRAW: | |
524 | case MSG_TYPE_LABELRELEASE: | |
525 | case MSG_TYPE_LABELABORTREQ: | |
526 | ret = recv_labelmessage(nbr, pdu, msg_size, type); | |
527 | break; | |
528 | default: | |
529 | log_debug("%s: unknown LDP message from nbr %pI4", | |
530 | __func__, &nbr->id); | |
531 | if (!(ntohs(msg->type) & UNKNOWN_FLAG)) { | |
532 | nbr->stats.unknown_msg++; | |
533 | send_notification(nbr->tcp, S_UNKNOWN_MSG, msg->id, msg->type); | |
534 | } | |
535 | /* ignore the message */ | |
536 | ret = 0; | |
537 | break; | |
538 | } | |
539 | ||
540 | if (ret == -1) { | |
541 | /* parser failed, giving up */ | |
542 | free(buf); | |
543 | return; | |
544 | } | |
545 | ||
546 | /* no errors - update per neighbor message counters */ | |
547 | switch (type) { | |
548 | case MSG_TYPE_NOTIFICATION: | |
549 | nbr->stats.notif_rcvd++; | |
550 | break; | |
551 | case MSG_TYPE_KEEPALIVE: | |
552 | nbr->stats.kalive_rcvd++; | |
553 | break; | |
554 | case MSG_TYPE_CAPABILITY: | |
555 | nbr->stats.capability_rcvd++; | |
556 | break; | |
557 | case MSG_TYPE_ADDR: | |
558 | nbr->stats.addr_rcvd++; | |
559 | break; | |
560 | case MSG_TYPE_ADDRWITHDRAW: | |
561 | nbr->stats.addrwdraw_rcvd++; | |
562 | break; | |
563 | case MSG_TYPE_LABELMAPPING: | |
564 | nbr->stats.labelmap_rcvd++; | |
565 | break; | |
566 | case MSG_TYPE_LABELREQUEST: | |
567 | nbr->stats.labelreq_rcvd++; | |
568 | break; | |
569 | case MSG_TYPE_LABELWITHDRAW: | |
570 | nbr->stats.labelwdraw_rcvd++; | |
571 | break; | |
572 | case MSG_TYPE_LABELRELEASE: | |
573 | nbr->stats.labelrel_rcvd++; | |
574 | break; | |
575 | case MSG_TYPE_LABELABORTREQ: | |
576 | nbr->stats.labelabreq_rcvd++; | |
577 | break; | |
578 | default: | |
579 | break; | |
580 | } | |
581 | ||
582 | /* Analyse the next message */ | |
583 | pdu += msg_size; | |
584 | len -= msg_size; | |
585 | } | |
586 | free(buf); | |
587 | buf = NULL; | |
588 | if (len != 0) { | |
589 | session_shutdown(nbr, S_BAD_PDU_LEN, 0, 0); | |
590 | return; | |
591 | } | |
592 | } | |
593 | ||
594 | /* shouldn't happen, session_get_pdu should be > 0 if buf was | |
595 | * allocated - but let's get rid of the SA warning. | |
596 | */ | |
597 | free(buf); | |
598 | } | |
599 | ||
600 | static void session_write(struct event *thread) | |
601 | { | |
602 | struct tcp_conn *tcp = EVENT_ARG(thread); | |
603 | struct nbr *nbr = tcp->nbr; | |
604 | ||
605 | tcp->wbuf.ev = NULL; | |
606 | ||
607 | if (msgbuf_write(&tcp->wbuf.wbuf) <= 0) | |
608 | if (errno != EAGAIN && nbr) | |
609 | nbr_fsm(nbr, NBR_EVT_CLOSE_SESSION); | |
610 | ||
611 | if (nbr == NULL && !tcp->wbuf.wbuf.queued) { | |
612 | /* | |
613 | * We are done sending the notification message, now we can | |
614 | * close the socket. | |
615 | */ | |
616 | tcp_close(tcp); | |
617 | return; | |
618 | } | |
619 | ||
620 | evbuf_event_add(&tcp->wbuf); | |
621 | } | |
622 | ||
623 | void | |
624 | session_shutdown(struct nbr *nbr, uint32_t status, uint32_t msg_id, | |
625 | uint32_t msg_type) | |
626 | { | |
627 | switch (nbr->state) { | |
628 | case NBR_STA_PRESENT: | |
629 | if (nbr_pending_connect(nbr)) | |
630 | EVENT_OFF(nbr->ev_connect); | |
631 | break; | |
632 | case NBR_STA_INITIAL: | |
633 | case NBR_STA_OPENREC: | |
634 | case NBR_STA_OPENSENT: | |
635 | /* update SNMP session counters during initialization */ | |
636 | leconf->stats.session_attempts++; | |
637 | send_notification(nbr->tcp, status, msg_id, msg_type); | |
638 | ||
639 | nbr_fsm(nbr, NBR_EVT_CLOSE_SESSION); | |
640 | break; | |
641 | case NBR_STA_OPER: | |
642 | send_notification(nbr->tcp, status, msg_id, msg_type); | |
643 | ||
644 | nbr_fsm(nbr, NBR_EVT_CLOSE_SESSION); | |
645 | break; | |
646 | default: | |
647 | fatalx("session_shutdown: unknown neighbor state"); | |
648 | } | |
649 | } | |
650 | ||
651 | void | |
652 | session_close(struct nbr *nbr) | |
653 | { | |
654 | log_debug("%s: closing session with lsr-id %pI4", __func__, &nbr->id); | |
655 | ||
656 | ldp_sync_fsm_nbr_event(nbr, LDP_SYNC_EVT_SESSION_CLOSE); | |
657 | ||
658 | tcp_close(nbr->tcp); | |
659 | nbr_stop_ktimer(nbr); | |
660 | nbr_stop_ktimeout(nbr); | |
661 | nbr_stop_itimeout(nbr); | |
662 | } | |
663 | ||
664 | static ssize_t | |
665 | session_get_pdu(struct ibuf_read *r, char **b) | |
666 | { | |
667 | struct ldp_hdr l; | |
668 | size_t av, dlen, left; | |
669 | ||
670 | av = r->wpos; | |
671 | if (av < sizeof(l)) | |
672 | return (0); | |
673 | ||
674 | memcpy(&l, r->buf, sizeof(l)); | |
675 | dlen = ntohs(l.length) + LDP_HDR_DEAD_LEN; | |
676 | if (dlen > av) | |
677 | return (0); | |
678 | ||
679 | if ((*b = malloc(dlen)) == NULL) | |
680 | return (-1); | |
681 | ||
682 | memcpy(*b, r->buf, dlen); | |
683 | if (dlen < av) { | |
684 | left = av - dlen; | |
685 | memmove(r->buf, r->buf + dlen, left); | |
686 | r->wpos = left; | |
687 | } else | |
688 | r->wpos = 0; | |
689 | ||
690 | return (dlen); | |
691 | } | |
692 | ||
693 | struct tcp_conn * | |
694 | tcp_new(int fd, struct nbr *nbr) | |
695 | { | |
696 | struct tcp_conn *tcp; | |
697 | struct sockaddr_storage ss; | |
698 | socklen_t len = sizeof(ss); | |
699 | ||
700 | if ((tcp = calloc(1, sizeof(*tcp))) == NULL) | |
701 | fatal(__func__); | |
702 | ||
703 | tcp->fd = fd; | |
704 | evbuf_init(&tcp->wbuf, tcp->fd, session_write, tcp); | |
705 | ||
706 | if (nbr) { | |
707 | if ((tcp->rbuf = calloc(1, sizeof(struct ibuf_read))) == NULL) | |
708 | fatal(__func__); | |
709 | ||
710 | event_add_read(master, session_read, nbr, tcp->fd, &tcp->rev); | |
711 | tcp->nbr = nbr; | |
712 | } | |
713 | ||
714 | if (getsockname(fd, (struct sockaddr *)&ss, &len) != 0) | |
715 | log_warn("%s: getsockname", __func__); | |
716 | else | |
717 | sa2addr((struct sockaddr *)&ss, NULL, NULL, &tcp->lport); | |
718 | if (getpeername(fd, (struct sockaddr *)&ss, &len) != 0) | |
719 | log_warn("%s: getpeername", __func__); | |
720 | else | |
721 | sa2addr((struct sockaddr *)&ss, NULL, NULL, &tcp->rport); | |
722 | ||
723 | return (tcp); | |
724 | } | |
725 | ||
726 | static void | |
727 | tcp_close(struct tcp_conn *tcp) | |
728 | { | |
729 | /* try to flush write buffer */ | |
730 | msgbuf_write(&tcp->wbuf.wbuf); | |
731 | evbuf_clear(&tcp->wbuf); | |
732 | ||
733 | if (tcp->nbr) { | |
734 | EVENT_OFF(tcp->rev); | |
735 | free(tcp->rbuf); | |
736 | tcp->nbr->tcp = NULL; | |
737 | } | |
738 | ||
739 | close(tcp->fd); | |
740 | accept_unpause(); | |
741 | free(tcp); | |
742 | } | |
743 | ||
744 | static struct pending_conn * | |
745 | pending_conn_new(int fd, int af, union ldpd_addr *addr) | |
746 | { | |
747 | struct pending_conn *pconn; | |
748 | ||
749 | if ((pconn = calloc(1, sizeof(*pconn))) == NULL) | |
750 | fatal(__func__); | |
751 | ||
752 | pconn->fd = fd; | |
753 | pconn->af = af; | |
754 | pconn->addr = *addr; | |
755 | TAILQ_INSERT_TAIL(&global.pending_conns, pconn, entry); | |
756 | pconn->ev_timeout = NULL; | |
757 | event_add_timer(master, pending_conn_timeout, pconn, | |
758 | PENDING_CONN_TIMEOUT, &pconn->ev_timeout); | |
759 | ||
760 | return (pconn); | |
761 | } | |
762 | ||
763 | void | |
764 | pending_conn_del(struct pending_conn *pconn) | |
765 | { | |
766 | EVENT_OFF(pconn->ev_timeout); | |
767 | TAILQ_REMOVE(&global.pending_conns, pconn, entry); | |
768 | free(pconn); | |
769 | } | |
770 | ||
771 | struct pending_conn * | |
772 | pending_conn_find(int af, union ldpd_addr *addr) | |
773 | { | |
774 | struct pending_conn *pconn; | |
775 | ||
776 | TAILQ_FOREACH(pconn, &global.pending_conns, entry) | |
777 | if (af == pconn->af && ldp_addrcmp(af, addr, &pconn->addr) == 0) | |
778 | return (pconn); | |
779 | ||
780 | return (NULL); | |
781 | } | |
782 | ||
783 | static void pending_conn_timeout(struct event *thread) | |
784 | { | |
785 | struct pending_conn *pconn = EVENT_ARG(thread); | |
786 | struct tcp_conn *tcp; | |
787 | ||
788 | pconn->ev_timeout = NULL; | |
789 | ||
790 | log_debug("%s: no adjacency with remote end: %s", __func__, | |
791 | log_addr(pconn->af, &pconn->addr)); | |
792 | ||
793 | /* | |
794 | * Create a write buffer detached from any neighbor to send a | |
795 | * notification message reliably. | |
796 | */ | |
797 | tcp = tcp_new(pconn->fd, NULL); | |
798 | send_notification(tcp, S_NO_HELLO, 0, 0); | |
799 | msgbuf_write(&tcp->wbuf.wbuf); | |
800 | ||
801 | pending_conn_del(pconn); | |
802 | } |