server/: Eliminate the remaining address-family-specific knowledge.
[tripe] / server / peer.c
1 /* -*-c-*-
2 *
3 * Communication with the peer
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
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.
16 *
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
20 * for more details.
21 *
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/>.
24 */
25
26 /*----- Header files ------------------------------------------------------*/
27
28 #include "tripe.h"
29
30 /*----- Global state ------------------------------------------------------*/
31
32 sel_file udpsock[NADDRFAM];
33
34 /*----- Static variables --------------------------------------------------*/
35
36 static sym_table byname;
37 static addrmap byaddr;
38 static unsigned nmobile;
39
40 /*----- Tunnel table ------------------------------------------------------*/
41
42 const tunnel_ops *tunnels[] = {
43 #ifdef TUN_LINUX
44 &tun_linux,
45 #endif
46 #ifdef TUN_BSD
47 &tun_bsd,
48 #endif
49 #ifdef TUN_UNET
50 &tun_unet,
51 #endif
52 &tun_slip,
53 0
54 }, *tun_default;
55
56 /*----- Main code ---------------------------------------------------------*/
57
58 /* --- @p_pingtype@ --- *
59 *
60 * Arguments: @unsigned msg@ = message type
61 *
62 * Returns: String to describe the message.
63 */
64
65 static const char *p_pingtype(unsigned msg)
66 {
67 switch (msg & MSG_TYPEMASK) {
68 case MISC_PING:
69 case MISC_PONG:
70 return "transport-ping";
71 case MISC_EPING:
72 case MISC_EPONG:
73 return "encrypted-ping";
74 default:
75 abort();
76 }
77 }
78
79 /* --- @p_ponged@ --- *
80 *
81 * Arguments: @peer *p@ = peer packet arrived from
82 * @unsigned msg@ = message type
83 * @buf *b@ = buffer containing payload
84 *
85 * Returns: ---
86 *
87 * Use: Processes a ping response.
88 */
89
90 static void p_ponged(peer *p, unsigned msg, buf *b)
91 {
92 uint32 id;
93 const octet *magic;
94 ping *pg;
95
96 IF_TRACING(T_PEER, {
97 trace(T_PEER, "peer: received %s reply from %s",
98 p_pingtype(msg), p->spec.name);
99 trace_block(T_PACKET, "peer: ping contents", BBASE(b), BSZ(b));
100 })
101
102 if (buf_getu32(b, &id) ||
103 (magic = buf_get(b, sizeof(pg->magic))) == 0 ||
104 BLEFT(b)) {
105 a_warn("PEER", "?PEER", p, "malformed-%s", p_pingtype(msg), A_END);
106 return;
107 }
108
109 for (pg = p->pings; pg; pg = pg->next) {
110 if (pg->id == id)
111 goto found;
112 }
113 a_warn("PEER",
114 "?PEER", p,
115 "unexpected-%s", p_pingtype(msg),
116 "0x%08lx", (unsigned long)id,
117 A_END);
118 return;
119
120 found:
121 if (memcmp(magic, pg->magic, sizeof(pg->magic)) != 0) {
122 a_warn("PEER", "?PEER", p, "corrupt-%s", p_pingtype(msg), A_END);
123 return;
124 }
125 p_pingdone(pg, PING_OK);
126 }
127
128 /* --- @p_rxupdstats@ --- *
129 *
130 * Arguments: @peer *p@ = peer to update
131 * @size_t n@ = size of incoming packet
132 *
133 * Returns: ---
134 *
135 * Use: Updates the peer's incoming packet statistics.
136 */
137
138 static void p_rxupdstats(peer *p, size_t n)
139 {
140 p->st.t_last = time(0);
141 p->st.n_in++;
142 p->st.sz_in += n;
143 }
144
145 /* --- @p_encrypt@ --- *
146 *
147 * Arguments: @peer *p@ = peer to encrypt message to
148 * @int ty@ message type to send
149 * @buf *bin, *bout@ = input and output buffers
150 *
151 * Returns: ---
152 *
153 * Use: Convenience function for packet encryption. Forces
154 * renegotiation when necessary. Check for the output buffer
155 * being broken to find out whether the encryption was
156 * successful.
157 */
158
159 static int p_encrypt(peer *p, int ty, buf *bin, buf *bout)
160 {
161 int err = ksl_encrypt(&p->ks, ty, bin, bout);
162
163 if (err == KSERR_REGEN) {
164 kx_start(&p->kx, 1);
165 err = 0;
166 }
167 if (!BOK(bout))
168 err = -1;
169 return (err);
170 }
171
172 /* --- @p_updateaddr@ --- *
173 *
174 * Arguments: @peer *p@ = pointer to peer block
175 * @const addr *a@ = address to associate with this peer
176 *
177 * Returns: Zero if the address was changed; @+1@ if it was already
178 * right.
179 *
180 * Use: Updates our idea of @p@'s address.
181 */
182
183 int p_updateaddr(peer *p, const addr *a)
184 {
185 peer *q;
186 peer_byaddr *pa, *qa;
187 int ix;
188 unsigned f;
189
190 /* --- Figure out how to proceed --- *
191 *
192 * If this address already belongs to a different peer, then swap the
193 * addresses over. This doesn't leave the displaced peer in an especially
194 * good state, but it ought to get sorted out soon enough.
195 */
196
197 pa = am_find(&byaddr, a, sizeof(peer_byaddr), &f);
198 if (f && pa->p == p)
199 return (+1);
200 else if (!f) {
201 T( trace(T_PEER, "peer: updating address for `%s'", p_name(p)); )
202 am_remove(&byaddr, p->byaddr);
203 p->byaddr = pa; p->spec.sa = *a; pa->p = p;
204 p->afix = afix(p->spec.sa.sa.sa_family); assert(p->afix >= 0);
205 a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END);
206 return (0);
207 } else {
208 q = pa->p; qa = p->byaddr;
209 T( trace(T_PEER, "peer: swapping addresses for `%s' and `%s'",
210 p_name(p), p_name(q)); )
211 q->byaddr = qa; qa->p = q; q->spec.sa = p->spec.sa;
212 p->byaddr = pa; pa->p = p; p->spec.sa = *a;
213 ix = p->afix; p->afix = q->afix; q->afix = ix;
214 a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END);
215 a_notify("NEWADDR", "?PEER", q, "?ADDR", &q->spec.sa, A_END);
216 return (0);
217 }
218 }
219
220 /* --- @p_decrypt@ --- *
221 *
222 * Arguments: @peer **pp@ = pointer to peer to decrypt message from
223 * @addr *a@ = address the packet arrived on
224 * @size_t n@ = size of original incoming packet
225 * @int ty@ = message type to expect
226 * @buf *bin, *bout@ = input and output buffers
227 *
228 * Returns: Zero on success; nonzero on error.
229 *
230 * Use: Convenience function for packet decryption. Reports errors
231 * and updates statistics appropriately.
232 *
233 * If @*pp@ is null on entry and there are mobile peers then we
234 * see if any of them can decrypt the packet. If so, we record
235 * @*a@ as the peer's new address and send a notification.
236 */
237
238 static int p_decrypt(peer **pp, addr *a, size_t n,
239 int ty, buf *bin, buf *bout)
240 {
241 peer *p, *q;
242 int err = KSERR_DECRYPT;
243
244 /* --- If we have a match on the source address then try that first --- */
245
246 q = *pp;
247 if (q) {
248 T( trace(T_PEER, "peer: decrypting packet from known peer `%s'",
249 p_name(q)); )
250 if ((err = ksl_decrypt(&q->ks, ty, bin, bout)) != KSERR_DECRYPT ||
251 !(q->spec.f & PSF_MOBILE) || nmobile == 1) {
252 p = q;
253 goto match;
254 }
255 T( trace(T_PEER, "peer: failed to decrypt: try other mobile peers..."); )
256 } else if (nmobile)
257 T( trace(T_PEER, "peer: unknown source: trying mobile peers...") );
258 else {
259 p = 0;
260 goto searched;
261 }
262
263 /* --- See whether any mobile peer is interested --- */
264
265 p = 0;
266 FOREACH_PEER(qq, {
267 if (qq == q || !(qq->spec.f & PSF_MOBILE)) continue;
268 if ((err = ksl_decrypt(&qq->ks, ty, bin, bout)) == KSERR_DECRYPT) {
269 T( trace(T_PEER, "peer: peer `%s' failed to decrypt",
270 p_name(qq)); )
271 continue;
272 } else {
273 p = qq;
274 IF_TRACING(T_PEER, {
275 if (!err)
276 trace(T_PEER, "peer: peer `%s' reports success", p_name(qq));
277 else {
278 trace(T_PEER, "peer: peer `%s' reports decryption error %d",
279 p_name(qq), err);
280 }
281 })
282 break;
283 }
284 });
285
286 /* --- We've searched the mobile peers --- */
287
288 searched:
289 if (!p) {
290 if (!q)
291 a_warn("PEER", "-", "unexpected-source", "?ADDR", a, A_END);
292 else {
293 a_warn("PEER", "?PEER", p, "decrypt-failed",
294 "error-code", "%d", err, A_END);
295 p_rxupdstats(q, n);
296 }
297 return (-1);
298 }
299
300 /* --- We found one that accepted, so update the peer's address --- */
301
302 if (!err) {
303 *pp = p;
304 p_updateaddr(p, a);
305 }
306
307 match:
308 p_rxupdstats(p, n);
309 if (err) {
310 if (p) p->st.n_reject++;
311 a_warn("PEER", "?PEER", p, "decrypt-failed",
312 "error-code", "%d", err, A_END);
313 return (-1);
314 }
315 if (!BOK(bout))
316 return (-1);
317 return (0);
318 }
319
320 /* --- @p_read@ --- *
321 *
322 * Arguments: @int fd@ = file descriptor to read from
323 * @unsigned mode@ = what happened
324 * @void *v@ = an uninteresting pointer
325 *
326 * Returns: ---
327 *
328 * Use: Reads a packet from somewhere.
329 */
330
331 static void p_read(int fd, unsigned mode, void *v)
332 {
333 peer *p = 0;
334 addr a;
335 socklen_t sz;
336 ssize_t n;
337 int ch;
338 buf b, bb;
339 #ifndef NTRACE
340 int ix = -1;
341 char name[NI_MAXHOST], svc[NI_MAXSERV];
342 #endif
343
344 /* --- Read the data --- */
345
346 QUICKRAND;
347 sz = sizeof(addr);
348 n = recvfrom(fd, buf_i, sizeof(buf_i), 0, &a.sa, &sz);
349 if (n < 0) {
350 a_warn("PEER", "-", "socket-read-error", "?ERRNO", A_END);
351 return;
352 }
353 IF_TRACING(T_PEER, {
354 ix = afix(a.sa.sa_family);
355 getnameinfo(&a.sa, sz, name, sizeof(name), svc, sizeof(svc),
356 NI_NUMERICHOST | NI_NUMERICSERV);
357 })
358
359 /* --- If the packet is a greeting, don't check peers --- */
360
361 if (n && buf_i[0] == (MSG_MISC | MISC_GREET)) {
362 IF_TRACING(T_PEER, {
363 trace(T_PEER, "peer: greeting received from %s %s %s",
364 aftab[ix].name, name, svc);
365 trace_block(T_PACKET, "peer: greeting contents", buf_i, n);
366 })
367 buf_init(&b, buf_i, n);
368 buf_getbyte(&b);
369 if (c_check(&b) || BLEFT(&b)) {
370 a_warn("PEER", "-", "invalid-greeting", A_END);
371 return;
372 }
373 a_notify("GREET",
374 "?B64", buf_i + 1, (size_t)(n - 1),
375 "?ADDR", &a,
376 A_END);
377 return;
378 }
379
380 /* --- Find the appropriate peer --- *
381 *
382 * At this stage, don't worry too much about whether we actually found it.
383 */
384
385 p = p_findbyaddr(&a);
386
387 IF_TRACING(T_PEER, {
388 if (p) {
389 trace(T_PEER,
390 "peer: packet received from `%s' from address %s %s %s",
391 p_name(p), aftab[ix].name, name, svc);
392 } else {
393 trace(T_PEER, "peer: packet received from unknown address %s %s %s",
394 aftab[ix].name, name, svc);
395 }
396 trace_block(T_PACKET, "peer: packet contents", buf_i, n);
397 })
398
399 /* --- Pick the packet apart --- */
400
401 buf_init(&b, buf_i, n);
402 if ((ch = buf_getbyte(&b)) < 0) {
403 a_warn("PEER", "?PEER", p, "bad-packet", "no-type", A_END);
404 return;
405 }
406 switch (ch & MSG_CATMASK) {
407 case MSG_PACKET:
408 if (ch & MSG_TYPEMASK) {
409 a_warn("PEER",
410 "?PEER", p,
411 "bad-packet",
412 "unknown-type", "0x%02x", ch,
413 A_END);
414 if (p) p->st.n_reject++;
415 return;
416 }
417 buf_init(&bb, buf_o, sizeof(buf_o));
418 if (p_decrypt(&p, &a, n, MSG_PACKET, &b, &bb))
419 return;
420 if (BOK(&bb)) {
421 p->st.n_ipin++;
422 p->st.sz_ipin += BSZ(&b);
423 p->t->ops->inject(p->t, &bb);
424 } else {
425 p->st.n_reject++;
426 a_warn("PEER", "?PEER", p, "packet-build-failed", A_END);
427 }
428 break;
429 case MSG_KEYEXCH:
430 if (!p) goto unexp;
431 p_rxupdstats(p, n);
432 kx_message(&p->kx, ch & MSG_TYPEMASK, &b);
433 break;
434 case MSG_MISC:
435 switch (ch & MSG_TYPEMASK) {
436 case MISC_NOP:
437 if (!p) goto unexp;
438 p_rxupdstats(p, n);
439 T( trace(T_PEER, "peer: received NOP packet"); )
440 break;
441 case MISC_PING:
442 if (!p) goto unexp;
443 p_rxupdstats(p, n);
444 buf_put(p_txstart(p, MSG_MISC | MISC_PONG), BCUR(&b), BLEFT(&b));
445 p_txend(p);
446 break;
447 case MISC_PONG:
448 if (!p) goto unexp;
449 p_rxupdstats(p, n);
450 p_ponged(p, MISC_PONG, &b);
451 break;
452 case MISC_EPING:
453 buf_init(&bb, buf_t, sizeof(buf_t));
454 if (p_decrypt(&p, &a, n, ch, &b, &bb))
455 return;
456 if (BOK(&bb)) {
457 buf_flip(&bb);
458 p_encrypt(p, MSG_MISC | MISC_EPONG, &bb,
459 p_txstart(p, MSG_MISC | MISC_EPONG));
460 p_txend(p);
461 }
462 break;
463 case MISC_EPONG:
464 buf_init(&bb, buf_t, sizeof(buf_t));
465 if (p_decrypt(&p, &a, n, ch, &b, &bb))
466 return;
467 if (BOK(&bb)) {
468 buf_flip(&bb);
469 p_ponged(p, MISC_EPONG, &bb);
470 }
471 break;
472 }
473 break;
474 default:
475 if (p) p->st.n_reject++;
476 a_warn("PEER",
477 "?PEER", p,
478 "bad-packet",
479 "unknown-category", "0x%02x", ch,
480 A_END);
481 break;
482 unexp:
483 a_warn("PEER", "-", "unexpected-source", "?ADDR", &a, A_END);
484 break;
485 }
486 }
487
488 /* --- @p_txstart@ --- *
489 *
490 * Arguments: @peer *p@ = pointer to peer block
491 * @unsigned msg@ = message type code
492 *
493 * Returns: A pointer to a buffer to write to.
494 *
495 * Use: Starts sending to a peer. Only one send can happen at a
496 * time.
497 */
498
499 buf *p_txstart(peer *p, unsigned msg)
500 {
501 buf_init(&p->b, buf_o, sizeof(buf_o));
502 buf_putbyte(&p->b, msg);
503 return (&p->b);
504 }
505
506 /* --- @p_txend@ --- *
507 *
508 * Arguments: @peer *p@ = pointer to peer block
509 *
510 * Returns: ---
511 *
512 * Use: Sends a packet to the peer.
513 */
514
515 static void p_setkatimer(peer *);
516
517 static int p_dotxend(peer *p)
518 {
519 socklen_t sasz = addrsz(&p->spec.sa);
520
521 if (!BOK(&p->b)) {
522 a_warn("PEER", "?PEER", p, "packet-build-failed", A_END);
523 return (0);
524 }
525 IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet",
526 BBASE(&p->b), BLEN(&p->b)); )
527 if (sendto(udpsock[p->afix].fd, BBASE(&p->b), BLEN(&p->b),
528 0, &p->spec.sa.sa, sasz) < 0) {
529 a_warn("PEER", "?PEER", p, "socket-write-error", "?ERRNO", A_END);
530 return (0);
531 } else {
532 p->st.n_out++;
533 p->st.sz_out += BLEN(&p->b);
534 return (1);
535 }
536 }
537
538 void p_txend(peer *p)
539 {
540 if (p_dotxend(p) && p->spec.t_ka) {
541 sel_rmtimer(&p->tka);
542 p_setkatimer(p);
543 }
544 }
545
546 /* --- @p_pingwrite@ --- *
547 *
548 * Arguments: @ping *p@ = ping structure
549 * @buf *b@ = buffer to write in
550 *
551 * Returns: ---
552 *
553 * Use: Fills in a ping structure and writes the packet payload.
554 */
555
556 static void p_pingwrite(ping *p, buf *b)
557 {
558 static uint32 seq = 0;
559
560 p->id = U32(seq++);
561 GR_FILL(&rand_global, p->magic, sizeof(p->magic));
562 buf_putu32(b, p->id);
563 buf_put(b, p->magic, sizeof(p->magic));
564 }
565
566 /* --- @p_pingdone@ --- *
567 *
568 * Arguments: @ping *p@ = ping structure
569 * @int rc@ = return code to pass on
570 *
571 * Returns: ---
572 *
573 * Use: Disposes of a ping structure, maybe sending a notification.
574 */
575
576 void p_pingdone(ping *p, int rc)
577 {
578 if (p->prev) p->prev->next = p->next;
579 else p->p->pings = p->next;
580 if (p->next) p->next->prev = p->prev;
581 if (rc != PING_TIMEOUT) sel_rmtimer(&p->t);
582 T( trace(T_PEER, "peer: ping 0x%08lx done (rc = %d)",
583 (unsigned long)p->id, rc); )
584 if (rc >= 0) p->func(rc, p->arg);
585 }
586
587 /* --- @p_pingtimeout@ --- *
588 *
589 * Arguments: @struct timeval *now@ = the time now
590 * @void *pv@ = pointer to ping block
591 *
592 * Returns: ---
593 *
594 * Use: Called when a ping times out.
595 */
596
597 static void p_pingtimeout(struct timeval *now, void *pv)
598 {
599 ping *p = pv;
600
601 T( trace(T_PEER, "peer: ping 0x%08lx timed out", (unsigned long)p->id); )
602 p_pingdone(p, PING_TIMEOUT);
603 }
604
605 /* --- @p_pingsend@ --- *
606 *
607 * Arguments: @peer *p@ = destination peer
608 * @ping *pg@ = structure to fill in
609 * @unsigned type@ = message type
610 * @unsigned long timeout@ = how long to wait before giving up
611 * @void (*func)(int, void *)@ = callback function
612 * @void *arg@ = argument for callback
613 *
614 * Returns: Zero if successful, nonzero if it failed.
615 *
616 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
617 * if we get an answer within the timeout, or zero if no answer.
618 */
619
620 int p_pingsend(peer *p, ping *pg, unsigned type,
621 unsigned long timeout,
622 void (*func)(int, void *), void *arg)
623 {
624 buf *b, bb;
625 struct timeval tv;
626
627 switch (type) {
628 case MISC_PING:
629 pg->msg = MISC_PONG;
630 b = p_txstart(p, MSG_MISC | MISC_PING);
631 p_pingwrite(pg, b);
632 p_txend(p);
633 break;
634 case MISC_EPING:
635 pg->msg = MISC_EPONG;
636 b = p_txstart(p, MSG_MISC | MISC_EPING);
637 buf_init(&bb, buf_t, sizeof(buf_t));
638 p_pingwrite(pg, &bb);
639 buf_flip(&bb);
640 p_encrypt(p, MSG_MISC | MISC_EPING, &bb, b);
641 if (!BOK(b))
642 return (-1);
643 p_txend(p);
644 break;
645 default:
646 abort();
647 break;
648 }
649
650 pg->next = p->pings;
651 pg->prev = 0;
652 pg->p = p;
653 pg->func = func;
654 pg->arg = arg;
655 if (p->pings) p->pings->prev = pg;
656 p->pings = pg;
657 gettimeofday(&tv, 0);
658 tv.tv_sec += timeout;
659 sel_addtimer(&sel, &pg->t, &tv, p_pingtimeout, pg);
660 T( trace(T_PEER, "peer: send %s 0x%08lx to %s",
661 p_pingtype(type), (unsigned long)pg->id, p->spec.name); )
662 return (0);
663 }
664
665 /* --- @p_greet@ --- *
666 *
667 * Arguments: @peer *p@ = peer to send to
668 * @const void *c@ = pointer to challenge
669 * @size_t sz@ = size of challenge
670 *
671 * Returns: ---
672 *
673 * Use: Sends a greeting packet.
674 */
675
676 void p_greet(peer *p, const void *c, size_t sz)
677 {
678 buf *b = p_txstart(p, MSG_MISC | MISC_GREET);
679 buf_put(b, c, sz);
680 p_txend(p);
681 }
682
683 /* --- @p_tun@ --- *
684 *
685 * Arguments: @peer *p@ = pointer to peer block
686 * @buf *b@ = buffer containing incoming packet
687 *
688 * Returns: ---
689 *
690 * Use: Handles a packet which needs to be sent to a peer.
691 */
692
693 void p_tun(peer *p, buf *b)
694 {
695 buf *bb = p_txstart(p, MSG_PACKET);
696
697 QUICKRAND;
698 p_encrypt(p, MSG_PACKET, b, bb);
699 if (BOK(bb) && BLEN(bb)) {
700 p->st.n_ipout++;
701 p->st.sz_ipout += BLEN(bb);
702 p_txend(p);
703 }
704 }
705
706 /* --- @p_keyreload@ --- *
707 *
708 * Arguments: ---
709 *
710 * Returns: ---
711 *
712 * Use: Forces a check of the daemon's keyring files.
713 */
714
715 void p_keyreload(void)
716 {
717 if (km_reload())
718 FOREACH_PEER(p, { kx_newkeys(&p->kx); });
719 }
720
721 /* --- @p_interval@ --- *
722 *
723 * Arguments: ---
724 *
725 * Returns: ---
726 *
727 * Use: Called periodically to do tidying.
728 */
729
730 void p_interval(void)
731 {
732 p_keyreload();
733 FOREACH_PEER(p, { ksl_prune(&p->ks); });
734 }
735
736 /* --- @p_stats@ --- *
737 *
738 * Arguments: @peer *p@ = pointer to a peer block
739 *
740 * Returns: A pointer to the peer's statistics.
741 */
742
743 stats *p_stats(peer *p) { return (&p->st); }
744
745 /* --- @p_ifname@ --- *
746 *
747 * Arguments: @peer *p@ = pointer to a peer block
748 *
749 * Returns: A pointer to the peer's interface name.
750 */
751
752 const char *p_ifname(peer *p) { return (p->ifname); }
753
754 /* --- @p_setifname@ --- *
755 *
756 * Arguments: @peer *p@ = pointer to a peer block
757 * @const char *name@ = pointer to the new name
758 *
759 * Returns: ---
760 *
761 * Use: Changes the name held for a peer's interface.
762 */
763
764 void p_setifname(peer *p, const char *name)
765 {
766 xfree(p->ifname);
767 p->ifname = xstrdup(name);
768 if (p->spec.tops->setifname)
769 p->spec.tops->setifname(p->t, name);
770 }
771
772 /* --- @p_addr@ --- *
773 *
774 * Arguments: @peer *p@ = pointer to a peer block
775 *
776 * Returns: A pointer to the peer's address.
777 */
778
779 const addr *p_addr(peer *p) { return (&p->spec.sa); }
780
781 /* --- @p_init@ --- *
782 *
783 * Arguments: @struct addrinfo *ailist@ = addresses to bind to
784 *
785 * Returns: ---
786 *
787 * Use: Initializes the peer system; creates the socket.
788 */
789
790 void p_init(struct addrinfo *ailist)
791 {
792 int fd;
793 int len = PKBUFSZ;
794 int i;
795 struct addrinfo *ai;
796 unsigned port, lastport = 0;
797 addr a;
798 socklen_t sz;
799
800 for (i = 0; i < NADDRFAM; i++) udpsock[i].fd = -1;
801
802 for (ai = ailist; ai; ai = ai->ai_next) {
803 if ((i = afix(ai->ai_family)) < 0) continue;
804 if (udpsock[i].fd != -1) continue;
805
806 /* --- Note on socket buffer sizes --- *
807 *
808 * For some bizarre reason, Linux 2.2 (at least) doubles the socket
809 * buffer sizes I pass to @setsockopt@. I'm not putting special-case
810 * code here for Linux: BSD (at least TCPv2) does what I tell it rather
811 * than second-guessing me.
812 */
813
814 if ((fd = socket(ai->ai_family, SOCK_DGRAM, 0)) < 0)
815 die(EXIT_FAILURE, "socket creation failed: %s", strerror(errno));
816 assert(ai->ai_addrlen <= sizeof(a));
817 memcpy(&a, ai->ai_addr, ai->ai_addrlen);
818 if ((port = getport(&a)) == 0 && lastport) setport(&a, lastport);
819 if (bind(fd, &a.sa, addrsz(&a)))
820 die(EXIT_FAILURE, "bind failed: %s", strerror(errno));
821 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
822 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len))) {
823 die(EXIT_FAILURE, "failed to set socket buffer sizes: %s",
824 strerror(errno));
825 }
826 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
827 sel_initfile(&sel, &udpsock[i], fd, SEL_READ, p_read, 0);
828 sel_addfile(&udpsock[i]);
829 T( trace(T_PEER, "peer: created %s socket", aftab[i].name); )
830 if (!port) {
831 sz = sizeof(a);
832 if (getsockname(fd, &a.sa, &sz)) {
833 die(EXIT_FAILURE, "failed to read local socket address: %s",
834 strerror(errno));
835 }
836 lastport = getport(&a);
837 }
838 }
839
840 sym_create(&byname);
841 am_create(&byaddr);
842 }
843
844 /* --- @p_port@ --- *
845 *
846 * Arguments: @int i@ = address family index to retrieve
847 *
848 * Returns: Port number used for socket.
849 */
850
851 unsigned p_port(int i)
852 {
853 addr a;
854 socklen_t sz = sizeof(addr);
855
856 if (getsockname(udpsock[i].fd, &a.sa, &sz))
857 die(EXIT_FAILURE, "couldn't read port number: %s", strerror(errno));
858 return (getport(&a));
859 }
860
861 /* --- @p_keepalive@ --- *
862 *
863 * Arguments: @struct timeval *now@ = the current time
864 * @void *pv@ = peer to wake up
865 *
866 * Returns: ---
867 *
868 * Use: Sends a keepalive ping message to its peer.
869 */
870
871 static void p_keepalive(struct timeval *now, void *pv)
872 {
873 peer *p = pv;
874
875 p_txstart(p, MSG_MISC | MISC_NOP); p_dotxend(p);
876 T( trace(T_PEER, "peer: sent keepalive to %s", p->spec.name); )
877 p_setkatimer(p);
878 }
879
880 /* --- @p_setkatimer@ --- *
881 *
882 * Arguments: @peer *p@ = peer to set
883 *
884 * Returns: ---
885 *
886 * Use: Resets the keepalive timer thing.
887 */
888
889 static void p_setkatimer(peer *p)
890 {
891 struct timeval tv;
892
893 if (!p->spec.t_ka)
894 return;
895 gettimeofday(&tv, 0);
896 tv.tv_sec += p->spec.t_ka;
897 sel_addtimer(&sel, &p->tka, &tv, p_keepalive, p);
898 }
899
900 /* --- @p_create@ --- *
901 *
902 * Arguments: @peerspec *spec@ = information about this peer
903 *
904 * Returns: Pointer to the peer block, or null if it failed.
905 *
906 * Use: Creates a new named peer block. No peer is actually attached
907 * by this point.
908 */
909
910 peer *p_create(peerspec *spec)
911 {
912 peer *p = CREATE(peer);
913 const tunnel_ops *tops = spec->tops;
914 int fd;
915 unsigned f;
916
917 p->byname = sym_find(&byname, spec->name, -1, sizeof(peer_byname), &f);
918 if (f) goto tidy_0;
919 p->byaddr = am_find(&byaddr, &spec->sa, sizeof(peer_byaddr), &f);
920 if (f) goto tidy_1;
921 p->byname->p = p->byaddr->p = p;
922
923 T( trace(T_PEER, "peer: creating new peer `%s'", spec->name); )
924 p->spec = *spec;
925 p->spec.name = (/*unconst*/ char *)SYM_NAME(p->byname);
926 if (spec->tag) p->spec.tag = xstrdup(spec->tag);
927 if (spec->privtag) p->spec.privtag = xstrdup(spec->privtag);
928 p->ks = 0;
929 p->pings = 0;
930 p->ifname = 0;
931 p->afix = afix(p->spec.sa.sa.sa_family); assert(p->afix >= 0);
932 memset(&p->st, 0, sizeof(stats));
933 p->st.t_start = time(0);
934 if (!(tops->flags & TUNF_PRIVOPEN))
935 fd = -1;
936 else if ((fd = ps_tunfd(tops, &p->ifname)) < 0)
937 goto tidy_2;
938 if ((p->t = tops->create(p, fd, &p->ifname)) == 0)
939 goto tidy_3;
940 T( trace(T_TUNNEL, "peer: attached interface %s to peer `%s'",
941 p->ifname, p_name(p)); )
942 p_setkatimer(p);
943 if (kx_init(&p->kx, p, &p->ks, p->spec.f & PSF_KXMASK))
944 goto tidy_4;
945 a_notify("ADD",
946 "?PEER", p,
947 "%s", p->ifname,
948 "?ADDR", &p->spec.sa,
949 A_END);
950 if (!(p->spec.f & KXF_CORK)) {
951 a_notify("KXSTART", "?PEER", p, A_END);
952 /* Couldn't tell anyone before */
953 }
954 if (p->spec.f & PSF_MOBILE) nmobile++;
955 return (p);
956
957 tidy_4:
958 if (spec->t_ka) sel_rmtimer(&p->tka);
959 xfree(p->ifname);
960 p->t->ops->destroy(p->t);
961 tidy_3:
962 if (fd >= 0) close(fd);
963 tidy_2:
964 am_remove(&byaddr, p->byaddr);
965 if (p->spec.tag) xfree(p->spec.tag);
966 if (p->spec.privtag) xfree(p->spec.privtag);
967 tidy_1:
968 sym_remove(&byname, p->byname);
969 tidy_0:
970 DESTROY(p);
971 return (0);
972 }
973
974 /* --- @p_name@ --- *
975 *
976 * Arguments: @peer *p@ = pointer to a peer block
977 *
978 * Returns: A pointer to the peer's name.
979 */
980
981 const char *p_name(peer *p)
982 { if (p) return (p->spec.name); else return ("-"); }
983
984 /* --- @p_tag@ --- *
985 *
986 * Arguments: @peer *p@ = pointer to a peer block
987 *
988 * Returns: A pointer to the peer's public key tag.
989 */
990
991 const char *p_tag(peer *p)
992 { return (p->spec.tag ? p->spec.tag : p->spec.name); }
993
994 /* --- @p_privtag@ --- *
995 *
996 * Arguments: @peer *p@ = pointer to a peer block
997 *
998 * Returns: A pointer to the peer's private key tag.
999 */
1000
1001 const char *p_privtag(peer *p)
1002 { return (p->spec.privtag ? p->spec.privtag : tag_priv); }
1003
1004 /* --- @p_spec@ --- *
1005 *
1006 * Arguments: @peer *p@ = pointer to a peer block
1007 *
1008 * Returns: Pointer to the peer's specification
1009 */
1010
1011 const peerspec *p_spec(peer *p) { return (&p->spec); }
1012
1013 /* --- @p_findbyaddr@ --- *
1014 *
1015 * Arguments: @const addr *a@ = address to look up
1016 *
1017 * Returns: Pointer to the peer block, or null if not found.
1018 *
1019 * Use: Finds a peer by address.
1020 */
1021
1022 peer *p_findbyaddr(const addr *a)
1023 {
1024 peer_byaddr *pa;
1025
1026 if ((pa = am_find(&byaddr, a, 0, 0)) != 0) {
1027 assert(pa->p);
1028 return (pa->p);
1029 }
1030 return (0);
1031 }
1032
1033 /* --- @p_find@ --- *
1034 *
1035 * Arguments: @const char *name@ = name to look up
1036 *
1037 * Returns: Pointer to the peer block, or null if not found.
1038 *
1039 * Use: Finds a peer by name.
1040 */
1041
1042 peer *p_find(const char *name)
1043 {
1044 peer_byname *pn;
1045
1046 if ((pn = sym_find(&byname, name, -1, 0, 0)) != 0)
1047 return (pn->p);
1048 return (0);
1049 }
1050
1051 /* --- @p_destroy@ --- *
1052 *
1053 * Arguments: @peer *p@ = pointer to a peer
1054 *
1055 * Returns: ---
1056 *
1057 * Use: Destroys a peer.
1058 */
1059
1060 void p_destroy(peer *p)
1061 {
1062 ping *pg, *ppg;
1063
1064 T( trace(T_PEER, "peer: destroying peer `%s'", p->spec.name); )
1065 a_notify("KILL", "%s", p->spec.name, A_END);
1066 ksl_free(&p->ks);
1067 kx_free(&p->kx);
1068 if (p->spec.f & PSF_MOBILE) nmobile--;
1069 if (p->ifname) xfree(p->ifname);
1070 if (p->spec.tag) xfree(p->spec.tag);
1071 if (p->spec.privtag) xfree(p->spec.privtag);
1072 p->t->ops->destroy(p->t);
1073 if (p->spec.t_ka) sel_rmtimer(&p->tka);
1074 for (pg = p->pings; pg; pg = ppg) {
1075 ppg = pg->next;
1076 p_pingdone(pg, PING_PEERDIED);
1077 }
1078 sym_remove(&byname, p->byname);
1079 am_remove(&byaddr, p->byaddr);
1080 DESTROY(p);
1081 }
1082
1083 /* --- @p_mkiter@ --- *
1084 *
1085 * Arguments: @peer_iter *i@ = pointer to an iterator
1086 *
1087 * Returns: ---
1088 *
1089 * Use: Initializes the iterator.
1090 */
1091
1092 void p_mkiter(peer_iter *i) { sym_mkiter(&i->i, &byname); }
1093
1094 /* --- @p_next@ --- *
1095 *
1096 * Arguments: @peer_iter *i@ = pointer to an iterator
1097 *
1098 * Returns: Next peer, or null if at the end.
1099 *
1100 * Use: Returns the next peer.
1101 */
1102
1103 peer *p_next(peer_iter *i)
1104 {
1105 peer_byname *pn;
1106
1107 if ((pn = sym_next(&i->i)) == 0)
1108 return (0);
1109 return (pn->p);
1110 }
1111
1112 /*----- That's all, folks -------------------------------------------------*/