3 * Communication with the peer
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
30 /*----- Global state ------------------------------------------------------*/
32 udpsocket udpsock[NADDRFAM];
34 /*----- Static variables --------------------------------------------------*/
36 static sym_table byname;
37 static addrmap byaddr;
38 static unsigned nmobile;
39 static struct tunnel_node {
40 struct tunnel_node *next;
41 const tunnel_ops *tops;
42 } *tunnels, **tunnels_tail = &tunnels;
43 const tunnel_ops *dflttun;
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @p_pingtype@ --- *
49 * Arguments: @unsigned msg@ = message type
51 * Returns: String to describe the message.
54 static const char *p_pingtype(unsigned msg)
56 switch (msg & MSG_TYPEMASK) {
59 return "transport-ping";
62 return "encrypted-ping";
68 /* --- @p_ponged@ --- *
70 * Arguments: @peer *p@ = peer packet arrived from
71 * @unsigned msg@ = message type
72 * @buf *b@ = buffer containing payload
76 * Use: Processes a ping response.
79 static void p_ponged(peer *p, unsigned msg, buf *b)
86 trace(T_PEER, "peer: received %s reply from %s",
87 p_pingtype(msg), p->spec.name);
88 trace_block(T_PACKET, "peer: ping contents", BBASE(b), BSZ(b));
91 if (buf_getu32(b, &id) ||
92 (magic = buf_get(b, sizeof(pg->magic))) == 0 ||
94 a_warn("PEER", "?PEER", p, "malformed-%s", p_pingtype(msg), A_END);
98 for (pg = p->pings; pg; pg = pg->next) {
104 "unexpected-%s", p_pingtype(msg),
105 "0x%08lx", (unsigned long)id,
110 if (memcmp(magic, pg->magic, sizeof(pg->magic)) != 0) {
111 a_warn("PEER", "?PEER", p, "corrupt-%s", p_pingtype(msg), A_END);
114 p_pingdone(pg, PING_OK);
117 /* --- @p_rxupdstats@ --- *
119 * Arguments: @peer *p@ = peer to update
120 * @size_t n@ = size of incoming packet
124 * Use: Updates the peer's incoming packet statistics.
127 static void p_rxupdstats(peer *p, size_t n)
129 p->st.t_last = time(0);
134 /* --- @p_encrypt@ --- *
136 * Arguments: @peer *p@ = peer to encrypt message to
137 * @int ty@ message type to send
138 * @buf *bin, *bout@ = input and output buffers
142 * Use: Convenience function for packet encryption. Forces
143 * renegotiation when necessary. Check for the output buffer
144 * being broken to find out whether the encryption was
148 static int p_encrypt(peer *p, int ty, buf *bin, buf *bout)
150 int err = ksl_encrypt(&p->ks, ty, bin, bout);
152 if (err == KSERR_REGEN) {
161 /* --- @p_updateaddr@ --- *
163 * Arguments: @peer *p@ = pointer to peer block
164 * @const addr *a@ = address to associate with this peer
166 * Returns: Zero if the address was changed; @+1@ if it was already
169 * Use: Updates our idea of @p@'s address.
172 int p_updateaddr(peer *p, const addr *a)
175 peer_byaddr *pa, *qa;
179 /* --- Figure out how to proceed --- *
181 * If this address already belongs to a different peer, then swap the
182 * addresses over. This doesn't leave the displaced peer in an especially
183 * good state, but it ought to get sorted out soon enough.
186 pa = am_find(&byaddr, a, sizeof(peer_byaddr), &f);
190 T( trace(T_PEER, "peer: updating address for `%s'", p_name(p)); )
191 am_remove(&byaddr, p->byaddr);
192 p->byaddr = pa; p->spec.sa = *a; pa->p = p;
193 p->afix = afix(p->spec.sa.sa.sa_family); assert(p->afix >= 0);
194 a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END);
197 q = pa->p; qa = p->byaddr;
198 T( trace(T_PEER, "peer: swapping addresses for `%s' and `%s'",
199 p_name(p), p_name(q)); )
200 q->byaddr = qa; qa->p = q; q->spec.sa = p->spec.sa;
201 p->byaddr = pa; pa->p = p; p->spec.sa = *a;
202 ix = p->afix; p->afix = q->afix; q->afix = ix;
203 a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END);
204 a_notify("NEWADDR", "?PEER", q, "?ADDR", &q->spec.sa, A_END);
209 /* --- @p_decrypt@ --- *
211 * Arguments: @peer **pp@ = pointer to peer to decrypt message from
212 * @addr *a@ = address the packet arrived on
213 * @size_t n@ = size of original incoming packet
214 * @int ty@ = message type to expect
215 * @buf *bin, *bout@ = input and output buffers
217 * Returns: Zero on success; nonzero on error.
219 * Use: Convenience function for packet decryption. Reports errors
220 * and updates statistics appropriately.
222 * If @*pp@ is null on entry and there are mobile peers then we
223 * see if any of them can decrypt the packet. If so, we record
224 * @*a@ as the peer's new address and send a notification.
227 static int p_decrypt(peer **pp, addr *a, size_t n,
228 int ty, buf *bin, buf *bout)
231 int err = KSERR_DECRYPT;
233 /* --- If we have a match on the source address then try that first --- */
237 T( trace(T_PEER, "peer: decrypting packet from known peer `%s'",
239 if ((err = ksl_decrypt(&q->ks, ty, bin, bout)) != KSERR_DECRYPT ||
240 !(q->spec.f & PSF_MOBILE) || nmobile == 1) {
244 T( trace(T_PEER, "peer: failed to decrypt: try other mobile peers..."); )
246 T( trace(T_PEER, "peer: unknown source: trying mobile peers...") );
252 /* --- See whether any mobile peer is interested --- */
256 if (qq == q || !(qq->spec.f & PSF_MOBILE)) continue;
257 if ((err = ksl_decrypt(&qq->ks, ty, bin, bout)) == KSERR_DECRYPT) {
258 T( trace(T_PEER, "peer: peer `%s' failed to decrypt",
265 trace(T_PEER, "peer: peer `%s' reports success", p_name(qq));
267 trace(T_PEER, "peer: peer `%s' reports decryption error %d",
275 /* --- We've searched the mobile peers --- */
280 a_warn("PEER", "-", "unexpected-source", "?ADDR", a, A_END);
282 a_warn("PEER", "?PEER", p, "decrypt-failed",
283 "error-code", "%d", err, A_END);
289 /* --- We found one that accepted, so update the peer's address --- */
299 if (p) p->st.n_reject++;
300 a_warn("PEER", "?PEER", p, "decrypt-failed",
301 "error-code", "%d", err, A_END);
309 /* --- @p_read@ --- *
311 * Arguments: @int fd@ = file descriptor to read from
312 * @unsigned mode@ = what happened
313 * @void *v@ = an uninteresting pointer
317 * Use: Reads a packet from somewhere.
320 static void p_read(int fd, unsigned mode, void *v)
330 char name[NI_MAXHOST], svc[NI_MAXSERV];
333 /* --- Read the data --- */
337 n = recvfrom(fd, buf_i, sizeof(buf_i), 0, &a.sa, &sz);
339 a_warn("PEER", "-", "socket-read-error", "?ERRNO", A_END);
343 ix = afix(a.sa.sa_family);
344 getnameinfo(&a.sa, sz, name, sizeof(name), svc, sizeof(svc),
345 NI_NUMERICHOST | NI_NUMERICSERV);
348 /* --- If the packet is a greeting, don't check peers --- */
350 if (n && buf_i[0] == (MSG_MISC | MISC_GREET)) {
352 trace(T_PEER, "peer: greeting received from %s %s %s",
353 aftab[ix].name, name, svc);
354 trace_block(T_PACKET, "peer: greeting contents", buf_i, n);
356 buf_init(&b, buf_i, n);
358 if (c_check(0, 0, &b) || BLEFT(&b)) {
359 a_warn("PEER", "-", "invalid-greeting", A_END);
363 "?B64", buf_i + 1, (size_t)(n - 1),
369 /* --- Find the appropriate peer --- *
371 * At this stage, don't worry too much about whether we actually found it.
374 p = p_findbyaddr(&a);
379 "peer: packet received from `%s' from address %s %s %s",
380 p_name(p), aftab[ix].name, name, svc);
382 trace(T_PEER, "peer: packet received from unknown address %s %s %s",
383 aftab[ix].name, name, svc);
385 trace_block(T_PACKET, "peer: packet contents", buf_i, n);
388 /* --- Pick the packet apart --- */
390 buf_init(&b, buf_i, n);
391 if ((ch = buf_getbyte(&b)) < 0) {
392 a_warn("PEER", "?PEER", p, "bad-packet", "no-type", A_END);
395 switch (ch & MSG_CATMASK) {
397 if (ch & MSG_TYPEMASK) {
401 "unknown-type", "0x%02x", ch,
403 if (p) p->st.n_reject++;
406 buf_init(&bb, buf_o, sizeof(buf_o));
407 if (p_decrypt(&p, &a, n, MSG_PACKET, &b, &bb))
411 p->st.sz_ipin += BSZ(&b);
412 p->t->ops->inject(p->t, &bb);
415 a_warn("PEER", "?PEER", p, "packet-build-failed", A_END);
419 if (p) p_rxupdstats(p, n);
420 if (kx_message(p ? &p->kx : 0, &a, ch & MSG_TYPEMASK, &b)) goto unexp;
423 switch (ch & MSG_TYPEMASK) {
427 T( trace(T_PEER, "peer: received NOP packet"); )
432 buf_put(p_txstart(p, MSG_MISC | MISC_PONG), BCUR(&b), BLEFT(&b));
438 p_ponged(p, MISC_PONG, &b);
441 buf_init(&bb, buf_t, sizeof(buf_t));
442 if (p_decrypt(&p, &a, n, ch, &b, &bb))
446 p_encrypt(p, MSG_MISC | MISC_EPONG, &bb,
447 p_txstart(p, MSG_MISC | MISC_EPONG));
452 buf_init(&bb, buf_t, sizeof(buf_t));
453 if (p_decrypt(&p, &a, n, ch, &b, &bb))
457 p_ponged(p, MISC_EPONG, &bb);
461 buf_init(&bb, buf_t, sizeof(buf_t));
462 if (p_decrypt(&p, &a, n, ch, &b, &bb)) return;
463 if (!(p->spec.f&PSF_EPHEM)) return;
466 if (BSZ(&bb)) return;
473 if (p) p->st.n_reject++;
477 "unknown-category", "0x%02x", ch,
481 a_warn("PEER", "-", "unexpected-source", "?ADDR", &a, A_END);
486 /* --- @p_txstart@ --- *
488 * Arguments: @peer *p@ = pointer to peer block
489 * @unsigned msg@ = message type code
491 * Returns: A pointer to a buffer to write to.
493 * Use: Starts sending to a peer. Only one send can happen at a
497 buf *p_txstart(peer *p, unsigned msg)
499 buf_init(&p->b, buf_o, sizeof(buf_o));
500 buf_putbyte(&p->b, msg);
504 /* --- @p_txaddr@ --- *
506 * Arguments: @const addr *a@ = recipient address
507 * @const void *p@ = pointer to packet to send
508 * @size_t sz@ = length of packet
510 * Returns: Zero if successful, nonzero on error.
512 * Use: Sends a packet to an address which (possibly) isn't a current
516 int p_txaddr(const addr *a, const void *p, size_t sz)
518 socklen_t sasz = addrsz(a);
521 if ((i = afix(a->sa.sa_family)) < 0) {
522 a_warn("PEER", "?ADDR", a, "disabled-address-family", A_END);
525 IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet", p, sz); )
526 if (sendto(udpsock[i].sf.fd, p, sz, 0, &a->sa, sasz) < 0) {
527 a_warn("PEER", "?ADDR", a, "socket-write-error", "?ERRNO", A_END);
533 /* --- @p_txend@ --- *
535 * Arguments: @peer *p@ = pointer to peer block
539 * Use: Sends a packet to the peer.
542 static void p_setkatimer(peer *);
544 static int p_dotxend(peer *p)
546 socklen_t sasz = addrsz(&p->spec.sa);
549 a_warn("PEER", "?PEER", p, "packet-build-failed", A_END);
552 IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet",
553 BBASE(&p->b), BLEN(&p->b)); )
554 if (sendto(udpsock[p->afix].sf.fd, BBASE(&p->b), BLEN(&p->b),
555 0, &p->spec.sa.sa, sasz) < 0) {
556 a_warn("PEER", "?PEER", p, "socket-write-error", "?ERRNO", A_END);
560 p->st.sz_out += BLEN(&p->b);
565 void p_txend(peer *p)
567 if (p_dotxend(p) && p->spec.t_ka) {
568 sel_rmtimer(&p->tka);
573 /* --- @p_pingwrite@ --- *
575 * Arguments: @ping *p@ = ping structure
576 * @buf *b@ = buffer to write in
580 * Use: Fills in a ping structure and writes the packet payload.
583 static void p_pingwrite(ping *p, buf *b)
585 static uint32 seq = 0;
588 GR_FILL(&rand_global, p->magic, sizeof(p->magic));
589 buf_putu32(b, p->id);
590 buf_put(b, p->magic, sizeof(p->magic));
593 /* --- @p_pingdone@ --- *
595 * Arguments: @ping *p@ = ping structure
596 * @int rc@ = return code to pass on
600 * Use: Disposes of a ping structure, maybe sending a notification.
603 void p_pingdone(ping *p, int rc)
605 if (p->prev) p->prev->next = p->next;
606 else p->p->pings = p->next;
607 if (p->next) p->next->prev = p->prev;
608 if (rc != PING_TIMEOUT) sel_rmtimer(&p->t);
609 T( trace(T_PEER, "peer: ping 0x%08lx done (rc = %d)",
610 (unsigned long)p->id, rc); )
611 if (rc >= 0) p->func(rc, p->arg);
614 /* --- @p_pingtimeout@ --- *
616 * Arguments: @struct timeval *now@ = the time now
617 * @void *pv@ = pointer to ping block
621 * Use: Called when a ping times out.
624 static void p_pingtimeout(struct timeval *now, void *pv)
628 T( trace(T_PEER, "peer: ping 0x%08lx timed out", (unsigned long)p->id); )
629 p_pingdone(p, PING_TIMEOUT);
632 /* --- @p_pingsend@ --- *
634 * Arguments: @peer *p@ = destination peer
635 * @ping *pg@ = structure to fill in
636 * @unsigned type@ = message type
637 * @unsigned long timeout@ = how long to wait before giving up
638 * @void (*func)(int, void *)@ = callback function
639 * @void *arg@ = argument for callback
641 * Returns: Zero if successful, nonzero if it failed.
643 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
644 * if we get an answer within the timeout, or zero if no answer.
647 int p_pingsend(peer *p, ping *pg, unsigned type,
648 unsigned long timeout,
649 void (*func)(int, void *), void *arg)
657 b = p_txstart(p, MSG_MISC | MISC_PING);
662 pg->msg = MISC_EPONG;
663 b = p_txstart(p, MSG_MISC | MISC_EPING);
664 buf_init(&bb, buf_t, sizeof(buf_t));
665 p_pingwrite(pg, &bb);
667 p_encrypt(p, MSG_MISC | MISC_EPING, &bb, b);
682 if (p->pings) p->pings->prev = pg;
684 gettimeofday(&tv, 0);
685 tv.tv_sec += timeout;
686 sel_addtimer(&sel, &pg->t, &tv, p_pingtimeout, pg);
687 T( trace(T_PEER, "peer: send %s 0x%08lx to %s",
688 p_pingtype(type), (unsigned long)pg->id, p->spec.name); )
692 /* --- @p_greet@ --- *
694 * Arguments: @peer *p@ = peer to send to
695 * @const void *c@ = pointer to challenge
696 * @size_t sz@ = size of challenge
700 * Use: Sends a greeting packet.
703 void p_greet(peer *p, const void *c, size_t sz)
705 buf *b = p_txstart(p, MSG_MISC | MISC_GREET);
712 * Arguments: @peer *p@ = pointer to peer block
713 * @buf *b@ = buffer containing incoming packet
717 * Use: Handles a packet which needs to be sent to a peer.
720 void p_tun(peer *p, buf *b)
722 buf *bb = p_txstart(p, MSG_PACKET);
725 p_encrypt(p, MSG_PACKET, b, bb);
726 if (BOK(bb) && BLEN(bb)) {
728 p->st.sz_ipout += BLEN(bb);
733 /* --- @p_keyreload@ --- *
739 * Use: Forces a check of the daemon's keyring files.
742 void p_keyreload(void)
745 FOREACH_PEER(p, { kx_newkeys(&p->kx); });
748 /* --- @p_interval@ --- *
754 * Use: Called periodically to do tidying.
757 void p_interval(void)
760 FOREACH_PEER(p, { ksl_prune(&p->ks); });
763 /* --- @p_stats@ --- *
765 * Arguments: @peer *p@ = pointer to a peer block
767 * Returns: A pointer to the peer's statistics.
770 stats *p_stats(peer *p) { return (&p->st); }
772 /* --- @p_ifname@ --- *
774 * Arguments: @peer *p@ = pointer to a peer block
776 * Returns: A pointer to the peer's interface name.
779 const char *p_ifname(peer *p) { return (p->ifname); }
781 /* --- @p_setifname@ --- *
783 * Arguments: @peer *p@ = pointer to a peer block
784 * @const char *name@ = pointer to the new name
788 * Use: Changes the name held for a peer's interface.
791 void p_setifname(peer *p, const char *name)
794 p->ifname = xstrdup(name);
795 if (p->spec.tops->setifname)
796 p->spec.tops->setifname(p->t, name);
799 /* --- @p_addr@ --- *
801 * Arguments: @peer *p@ = pointer to a peer block
803 * Returns: A pointer to the peer's address.
806 const addr *p_addr(peer *p) { return (&p->spec.sa); }
808 /* --- @p_bind@ --- *
810 * Arguments: @struct addrinfo *ailist@ = addresses to bind to
812 * Returns: Zero on success, @-1@ on failure.
814 * Use: Binds to the main UDP sockets.
817 int p_bind(struct addrinfo *ailist)
824 unsigned port, lastport = 0;
828 for (i = 0; i < NADDRFAM; i++) udpsock[i].sf.fd = -1;
830 for (ai = ailist; ai; ai = ai->ai_next) {
831 if ((i = afix(ai->ai_family)) < 0) continue;
832 if (udpsock[i].sf.fd != -1) continue;
834 /* --- Note on socket buffer sizes --- *
836 * For some bizarre reason, Linux 2.2 (at least) doubles the socket
837 * buffer sizes I pass to @setsockopt@. I'm not putting special-case
838 * code here for Linux: BSD (at least TCPv2) does what I tell it rather
839 * than second-guessing me.
842 if ((fd = socket(ai->ai_family, SOCK_DGRAM, 0)) < 0) {
843 a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
844 "create-failed", "?ERRNO", A_END);
847 if (i == AFIX_INET6 &&
848 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes))) {
849 a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
850 "set-v6only-failed", "?ERRNO", A_END);
853 assert(ai->ai_addrlen <= sizeof(a));
854 memcpy(&a, ai->ai_addr, ai->ai_addrlen);
855 if ((port = getport(&a)) == 0 && lastport) setport(&a, lastport);
856 if (bind(fd, &a.sa, addrsz(&a))) {
857 a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
858 "bind-failed", "?ERRNO", A_END);
861 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
862 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len))) {
863 a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
864 "set-buffers-failed", "?ERRNO", A_END);
867 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
869 udpsock[i].port = port;
872 if (getsockname(fd, &a.sa, &sz)) {
873 a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
874 "read-local-address-failed", "?ERRNO", A_END);
877 udpsock[i].port = lastport = getport(&a);
879 T( trace(T_PEER, "peer: created %s socket", aftab[i].name); )
880 sel_initfile(&sel, &udpsock[i].sf, fd, SEL_READ, p_read, 0);
881 sel_addfile(&udpsock[i].sf);
888 if (fd != -1) close(fd);
893 /* --- @p_unbind@ --- *
899 * Use: Unbinds the UDP sockets. There must not be any active peers,
900 * and none can be created until the sockets are rebound.
908 { peer_iter it; p_mkiter(&it); assert(!p_next(&it)); }
911 for (i = 0; i < NADDRFAM; i++) {
912 if (udpsock[i].sf.fd == -1) continue;
913 sel_rmfile(&udpsock[i].sf);
914 close(udpsock[i].sf.fd);
915 udpsock[i].sf.fd = -1;
919 /* --- @p_init@ --- *
925 * Use: Initializes the peer system.
934 /* --- @p_addtun@ --- *
936 * Arguments: @const tunnel_ops *tops@ = tunnel ops to add
938 * Returns: Zero on success, @-1@ on failure.
940 * Use: Adds a tunnel class to the list of known classes, if it
941 * initializes properly. If there is no current default tunnel,
942 * then this one is made the default.
944 * Does nothing if the tunnel class is already known. So adding
945 * a bunch of tunnels takes quadratic time, but there will be
946 * too few to care about.
949 int p_addtun(const tunnel_ops *tops)
951 struct tunnel_node *tn;
953 for (tn = tunnels; tn; tn = tn->next)
954 if (tn->tops == tops) return (0);
955 if (tops->init()) return (-1);
956 tn = CREATE(struct tunnel_node);
957 tn->next = 0; tn->tops = tops;
958 *tunnels_tail = tn; tunnels_tail = &tn->next;
959 if (!dflttun) dflttun = tops;
963 /* --- @p_setdflttun@ --- *
965 * Arguments: @const tunnel_ops *tops@ = tunnel ops to set
969 * Use: Sets the default tunnel. It must already be registered. The
970 * old default is forgotten.
973 void p_setdflttun(const tunnel_ops *tops)
976 /* --- @p_dflttun@ --- *
980 * Returns: A pointer to the current default tunnel operations, or null
981 * if no tunnels are defined.
984 const tunnel_ops *p_dflttun(void) { return (dflttun); }
986 /* --- @p_findtun@ --- *
988 * Arguments: @const char *name@ = tunnel name
990 * Returns: Pointer to the tunnel operations, or null.
992 * Use: Finds the operations for a named tunnel class.
995 const tunnel_ops *p_findtun(const char *name)
997 const struct tunnel_node *tn;
999 for (tn = tunnels; tn; tn = tn->next)
1000 if (mystrieq(tn->tops->name, name) == 0) return (tn->tops);
1004 /* --- @p_mktuniter@ --- *
1006 * Arguments: @tuniter *i@ = pointer to iterator to initialize
1010 * Use: Initializes a tunnel iterator.
1013 void p_mktuniter(tun_iter *i) { i->next = tunnels; }
1015 /* --- @p_nexttun@ --- *
1017 * Arguments: @tuniter *i@ = pointer to iterator
1019 * Returns: Pointer to the next tunnel's operations, or null.
1022 const tunnel_ops *p_nexttun(tun_iter *i)
1024 const struct tunnel_node *tn = i->next;
1026 if (!tn) return (0);
1027 else { i->next = tn->next; return (tn->tops); }
1030 /* --- @p_keepalive@ --- *
1032 * Arguments: @struct timeval *now@ = the current time
1033 * @void *pv@ = peer to wake up
1037 * Use: Sends a keepalive ping message to its peer.
1040 static void p_keepalive(struct timeval *now, void *pv)
1044 p_txstart(p, MSG_MISC | MISC_NOP); p_dotxend(p);
1045 T( trace(T_PEER, "peer: sent keepalive to %s", p->spec.name); )
1049 /* --- @p_setkatimer@ --- *
1051 * Arguments: @peer *p@ = peer to set
1055 * Use: Resets the keepalive timer thing.
1058 static void p_setkatimer(peer *p)
1064 gettimeofday(&tv, 0);
1065 tv.tv_sec += p->spec.t_ka;
1066 sel_addtimer(&sel, &p->tka, &tv, p_keepalive, p);
1069 /* --- @p_create@ --- *
1071 * Arguments: @peerspec *spec@ = information about this peer
1073 * Returns: Pointer to the peer block, or null if it failed.
1075 * Use: Creates a new named peer block. No peer is actually attached
1079 peer *p_create(peerspec *spec)
1081 peer *p = CREATE(peer);
1082 const tunnel_ops *tops = spec->tops;
1086 p->byname = sym_find(&byname, spec->name, -1, sizeof(peer_byname), &f);
1088 p->byaddr = am_find(&byaddr, &spec->sa, sizeof(peer_byaddr), &f);
1090 p->byname->p = p->byaddr->p = p;
1092 T( trace(T_PEER, "peer: creating new peer `%s'", spec->name); )
1094 p->spec.name = (/*unconst*/ char *)SYM_NAME(p->byname);
1095 if (spec->tag) p->spec.tag = xstrdup(spec->tag);
1096 if (spec->privtag) p->spec.privtag = xstrdup(spec->privtag);
1097 if (spec->knock) p->spec.knock = xstrdup(spec->knock);
1101 p->afix = afix(p->spec.sa.sa.sa_family); assert(p->afix >= 0);
1102 memset(&p->st, 0, sizeof(stats));
1103 p->st.t_start = time(0);
1104 if (!(tops->flags & TUNF_PRIVOPEN))
1106 else if ((fd = ps_tunfd(tops, &p->ifname)) < 0)
1108 if ((p->t = tops->create(p, fd, &p->ifname)) == 0)
1110 T( trace(T_TUNNEL, "peer: attached interface %s to peer `%s'",
1111 p->ifname, p_name(p)); )
1114 if (kx_setup(&p->kx, p, &p->ks, p->spec.f & PSF_KXMASK))
1119 "?ADDR", &p->spec.sa,
1121 if (!(p->spec.f & KXF_CORK)) {
1122 a_notify("KXSTART", "?PEER", p, A_END);
1123 /* Couldn't tell anyone before */
1125 if (p->spec.f & PSF_MOBILE) nmobile++;
1129 if (spec->t_ka) sel_rmtimer(&p->tka);
1131 p->t->ops->destroy(p->t);
1134 if (fd >= 0) close(fd);
1136 am_remove(&byaddr, p->byaddr);
1137 if (p->spec.tag) xfree(p->spec.tag);
1138 if (p->spec.privtag) xfree(p->spec.privtag);
1140 sym_remove(&byname, p->byname);
1146 /* --- @p_name@ --- *
1148 * Arguments: @peer *p@ = pointer to a peer block
1150 * Returns: A pointer to the peer's name.
1153 const char *p_name(peer *p)
1154 { if (p) return (p->spec.name); else return ("-"); }
1156 /* --- @p_tag@ --- *
1158 * Arguments: @peer *p@ = pointer to a peer block
1160 * Returns: A pointer to the peer's public key tag.
1163 const char *p_tag(peer *p)
1164 { return (p->spec.tag ? p->spec.tag : p->spec.name); }
1166 /* --- @p_privtag@ --- *
1168 * Arguments: @peer *p@ = pointer to a peer block
1170 * Returns: A pointer to the peer's private key tag.
1173 const char *p_privtag(peer *p)
1174 { return (p->spec.privtag ? p->spec.privtag : tag_priv); }
1176 /* --- @p_spec@ --- *
1178 * Arguments: @peer *p@ = pointer to a peer block
1180 * Returns: Pointer to the peer's specification
1183 const peerspec *p_spec(peer *p) { return (&p->spec); }
1185 /* --- @p_findbyaddr@ --- *
1187 * Arguments: @const addr *a@ = address to look up
1189 * Returns: Pointer to the peer block, or null if not found.
1191 * Use: Finds a peer by address.
1194 peer *p_findbyaddr(const addr *a)
1198 if ((pa = am_find(&byaddr, a, 0, 0)) != 0) {
1205 /* --- @p_find@ --- *
1207 * Arguments: @const char *name@ = name to look up
1209 * Returns: Pointer to the peer block, or null if not found.
1211 * Use: Finds a peer by name.
1214 peer *p_find(const char *name)
1218 if ((pn = sym_find(&byname, name, -1, 0, 0)) != 0)
1223 /* --- @p_destroy@ --- *
1225 * Arguments: @peer *p@ = pointer to a peer
1226 * @int bye@ = say goodbye to the peer?
1230 * Use: Destroys a peer.
1233 void p_destroy(peer *p, int bye)
1238 T( trace(T_PEER, "peer: destroying peer `%s'", p->spec.name); )
1241 b = p_txstart(p, MSG_MISC | MISC_BYE);
1242 buf_init(&bb, buf_t, sizeof(buf_t));
1243 assert(BOK(&bb)); buf_flip(&bb);
1244 p_encrypt(p, MSG_MISC | MISC_BYE, &bb, b);
1248 a_notify("KILL", "%s", p->spec.name, A_END);
1251 if (p->spec.f & PSF_MOBILE) nmobile--;
1252 if (p->ifname) xfree(p->ifname);
1253 if (p->spec.tag) xfree(p->spec.tag);
1254 if (p->spec.privtag) xfree(p->spec.privtag);
1255 if (p->spec.knock) xfree(p->spec.knock);
1256 p->t->ops->destroy(p->t);
1257 if (p->spec.t_ka) sel_rmtimer(&p->tka);
1258 for (pg = p->pings; pg; pg = ppg) {
1260 p_pingdone(pg, PING_PEERDIED);
1262 sym_remove(&byname, p->byname);
1263 am_remove(&byaddr, p->byaddr);
1268 /* --- @p_destroyall@ --- *
1274 * Use: Destroys all of the peers, saying goodbye.
1277 void p_destroyall(void) { FOREACH_PEER(p, { p_destroy(p, 1); }); }
1279 /* --- @p_mkiter@ --- *
1281 * Arguments: @peer_iter *i@ = pointer to an iterator
1285 * Use: Initializes the iterator.
1288 void p_mkiter(peer_iter *i) { sym_mkiter(&i->i, &byname); }
1290 /* --- @p_next@ --- *
1292 * Arguments: @peer_iter *i@ = pointer to an iterator
1294 * Returns: Next peer, or null if at the end.
1296 * Use: Returns the next peer.
1299 peer *p_next(peer_iter *i)
1303 if ((pn = sym_next(&i->i)) == 0)
1308 /*----- That's all, folks -------------------------------------------------*/