chiark / gitweb /
Remove buf, and add Ethereal analysis.
[tripe] / peer.c
CommitLineData
410c8acf 1/* -*-c-*-
2 *
dd9647cc 3 * $Id: peer.c,v 1.10 2003/10/15 09:29:38 mdw Exp $
410c8acf 4 *
5 * Communication with the peer
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Trivial IP Encryption (TrIPE).
13 *
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * TrIPE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: peer.c,v $
dd9647cc 32 * Revision 1.10 2003/10/15 09:29:38 mdw
33 * Cosmetic fix to changelog comment.
34 *
7ed14135 35 * Revision 1.9 2003/07/13 11:19:49 mdw
dd9647cc 36 * Incompatible protocol fix! Include message type code under MAC tag to
37 * prevent cut-and-paste from key-exchange messages to general packet
38 * transport.
7ed14135 39 *
767b36e2 40 * Revision 1.8 2003/05/16 12:09:03 mdw
41 * Allow binding to a chosen address.
42 *
7b052fe6 43 * Revision 1.7 2003/04/15 14:12:05 mdw
44 * Insert a newline to improve readability.
45 *
df9dfccf 46 * Revision 1.6 2001/06/19 22:07:59 mdw
47 * Use magic number for packet size.
48 *
0a9920e2 49 * Revision 1.5 2001/03/03 11:15:19 mdw
50 * Set the socket send and receive buffers to maximum. At least this way,
51 * we won't drop large packets on the floor. If the administrator wants to
52 * prevent fragmentation of TrIPE messages, he can lower the MTU on the
53 * tunnel interface. Getting path-MTU stuff out of the kernel is too much
54 * system-specific hard work for this program.
55 *
5bb41301 56 * Revision 1.4 2001/02/16 21:40:24 mdw
57 * Change key exchange message interface. Maintain statistics.
58 *
650a6624 59 * Revision 1.3 2001/02/04 17:10:58 mdw
60 * Make file descriptors be nonblocking and close-on-exec.
61 *
8d0c7a83 62 * Revision 1.2 2001/02/03 22:40:29 mdw
63 * Put timer information into the entropy pool when packets are received
64 * and on similar events. Reseed the generator on the interval timer.
65 *
410c8acf 66 * Revision 1.1 2001/02/03 20:26:37 mdw
67 * Initial checkin.
68 *
69 */
70
71/*----- Header files ------------------------------------------------------*/
72
73#include "tripe.h"
74
75/*----- Static variables --------------------------------------------------*/
76
77static peer *peers = 0;
78static sel_file sock;
79
80/*----- Main code ---------------------------------------------------------*/
81
82/* --- @p_read@ --- *
83 *
84 * Arguments: @int fd@ = file descriptor to read from
85 * @unsigned mode@ = what happened
86 * @void *v@ = an uninteresting pointer
87 *
88 * Returns: ---
89 *
90 * Use: Reads a packet from somewhere.
91 */
92
93static void p_read(int fd, unsigned mode, void *v)
94{
95 peer *p;
96 addr a;
97 size_t sz;
98 ssize_t n;
99 int ch;
100 buf b, bb;
101
102 /* --- Read the data --- */
103
8d0c7a83 104 TIMER;
410c8acf 105 sz = sizeof(addr);
106 n = recvfrom(fd, buf_i, sizeof(buf_i), 0, &a.sa, &sz);
107 if (n < 0) {
108 a_warn("error reading socket: %s", strerror(errno));
109 return;
110 }
111
112 /* --- Find the appropriate peer --- */
113
114 assert(a.sa.sa_family == AF_INET);
410c8acf 115 for (p = peers; p; p = p->next) {
410c8acf 116 if (p->peer.sin.sin_addr.s_addr == a.sin.sin_addr.s_addr &&
117 p->peer.sin.sin_port == a.sin.sin_port)
118 goto found;
119 }
120 a_warn("packet from unexpected peer: %s:%u",
121 inet_ntoa(a.sin.sin_addr), (unsigned)ntohs(a.sin.sin_port));
122 return;
123
124found:
125 T( trace(T_PEER, "peer: packet received from `%s'", p->name);
126 trace_block(T_PACKET, "peer: packet contents", buf_i, n); )
127
128 /* --- Pick the packet apart --- */
129
5bb41301 130 p->st.t_last = time(0);
131 p->st.n_in++;
132 p->st.sz_in += n;
410c8acf 133 buf_init(&b, buf_i, n);
134 if ((ch = buf_getbyte(&b)) < 0) {
135 a_warn("bad packet from `%s': no type byte", p->name);
136 return;
137 }
5bb41301 138 switch (ch & MSG_CATMASK) {
410c8acf 139 case MSG_PACKET:
5bb41301 140 if (ch & MSG_TYPEMASK) {
141 a_warn("unknown packet type from `%s'", p->name);
142 p->st.n_reject++;
143 return;
144 }
410c8acf 145 buf_init(&bb, buf_o, sizeof(buf_o));
7ed14135 146 if (ksl_decrypt(&p->ks, MSG_PACKET, &b, &bb)) {
5bb41301 147 p->st.n_reject++;
148 a_warn("couldn't decrypt inbound packet from `%s'", p->name);
410c8acf 149 return;
150 }
5bb41301 151 if (BOK(&bb)) {
152 p->st.n_ipin++;
153 p->st.sz_ipin += BSZ(&b);
410c8acf 154 tun_inject(&p->t, &bb);
5bb41301 155 } else {
156 p->st.n_reject++;
410c8acf 157 a_warn("packet build failed");
5bb41301 158 }
410c8acf 159 break;
5bb41301 160 case MSG_KEYEXCH:
161 kx_message(&p->kx, ch & MSG_TYPEMASK, &b);
410c8acf 162 break;
163 default:
5bb41301 164 p->st.n_reject++;
410c8acf 165 a_warn("bad packet from `%s': unknown packet type", p->name);
166 break;
167 }
168}
169
170/* --- @p_txstart@ --- *
171 *
172 * Arguments: @peer *p@ = pointer to peer block
173 * @unsigned msg@ = message type code
174 *
175 * Returns: A pointer to a buffer to write to.
176 *
177 * Use: Starts sending to a peer. Only one send can happen at a
178 * time.
179 */
180
181buf *p_txstart(peer *p, unsigned msg)
182{
183 buf_init(&p->b, buf_o, sizeof(buf_o));
184 buf_putbyte(&p->b, msg);
185 return (&p->b);
186}
187
188/* --- @p_txend@ --- *
189 *
190 * Arguments: @peer *p@ = pointer to peer block
191 *
192 * Returns: ---
193 *
194 * Use: Sends a packet to the peer.
195 */
196
197void p_txend(peer *p)
198{
199 if (!BOK(&p->b)) {
200 a_warn("packet build failed");
201 return;
202 }
203 IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet",
204 BBASE(&p->b), BLEN(&p->b)); )
205 if (sendto(sock.fd, BBASE(&p->b), BLEN(&p->b),
206 0, &p->peer.sa, p->sasz) < 0)
207 a_warn("packet send to `%s' failed: %s", p->name, strerror(errno));
5bb41301 208 else {
209 p->st.n_out++;
210 p->st.sz_out += BLEN(&p->b);
211 }
410c8acf 212}
213
214/* --- @p_tun@ --- *
215 *
216 * Arguments: @peer *p@ = pointer to peer block
217 * @buf *b@ = buffer containing incoming packet
218 *
219 * Returns: ---
220 *
221 * Use: Handles a packet which needs to be sent to a peer.
222 */
223
224void p_tun(peer *p, buf *b)
225{
226 buf *bb = p_txstart(p, MSG_PACKET);
7b052fe6 227
8d0c7a83 228 TIMER;
7ed14135 229 if (ksl_encrypt(&p->ks, MSG_PACKET, b, bb))
410c8acf 230 kx_start(&p->kx);
5bb41301 231 if (BOK(bb) && BLEN(bb)) {
232 p->st.n_ipout++;
233 p->st.sz_ipout += BLEN(bb);
410c8acf 234 p_txend(p);
5bb41301 235 }
410c8acf 236}
237
238/* --- @p_interval@ --- *
239 *
240 * Arguments: ---
241 *
242 * Returns: ---
243 *
244 * Use: Called periodically to do tidying.
245 */
246
247void p_interval(void)
248{
249 peer *p, *pp;
250 int reload;
251
252 reload = km_interval();
253 for (p = peers; p; p = pp) {
254 pp = p->next;
255 if (reload)
256 kx_newkeys(&p->kx);
5bb41301 257 ksl_prune(&p->ks);
410c8acf 258 }
259}
260
5bb41301 261/* --- @p_stats@ --- *
262 *
263 * Arguments: @peer *p@ = pointer to a peer block
264 *
265 * Returns: A pointer to the peer's statistics.
266 */
267
268stats *p_stats(peer *p) { return (&p->st); }
269
410c8acf 270/* --- @p_ifname@ --- *
271 *
272 * Arguments: @peer *p@ = pointer to a peer block
273 *
274 * Returns: A pointer to the peer's interface name.
275 */
276
277const char *p_ifname(peer *p) { return (tun_ifname(&p->t)); }
278
279/* --- @p_addr@ --- *
280 *
281 * Arguments: @peer *p@ = pointer to a peer block
282 *
283 * Returns: A pointer to the peer's address.
284 */
285
286const addr *p_addr(peer *p) { return (&p->peer); }
287
288/* --- @p_init@ --- *
289 *
767b36e2 290 * Arguments: @struct in_addr addr@ = address to bind to
291 * @unsigned port@ = port number to listen to
410c8acf 292 *
293 * Returns: ---
294 *
295 * Use: Initializes the peer system; creates the socket.
296 */
297
767b36e2 298void p_init(struct in_addr addr, unsigned port)
410c8acf 299{
300 int fd;
301 struct sockaddr_in sin;
df9dfccf 302 int len = PKBUFSZ;
0a9920e2 303
304 /* --- Note on socket buffer sizes --- *
305 *
306 * For some bizarre reason, Linux 2.2 (at least) doubles the socket buffer
307 * sizes I pass to @setsockopt@. I'm not putting special-case code here
308 * for Linux: BSD (at least TCPv2) does what I tell it rather than second-
309 * guessing me.
310 */
410c8acf 311
312 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
313 die(EXIT_FAILURE, "socket creation failed: %s", strerror(errno));
314 BURN(sin);
315 sin.sin_family = AF_INET;
767b36e2 316 sin.sin_addr = addr;
410c8acf 317 sin.sin_port = htons(port);
318 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
319 die(EXIT_FAILURE, "bind failed: %s", strerror(errno));
0a9920e2 320 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
321 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len))) {
322 die(EXIT_FAILURE, "failed to set socket buffer sizes: %s",
323 strerror(errno));
324 }
650a6624 325 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
410c8acf 326 sel_initfile(&sel, &sock, fd, SEL_READ, p_read, 0);
327 sel_addfile(&sock);
328 T( trace(T_PEER, "peer: created socket"); )
329}
330
331/* --- @p_port@ --- *
332 *
333 * Arguments: ---
334 *
335 * Returns: Port number used for socket.
336 */
337
338unsigned p_port(void)
339{
340 addr a;
341 size_t sz = sizeof(addr);
342
343 if (getsockname(sock.fd, &a.sa, &sz))
344 die(EXIT_FAILURE, "couldn't read port number: %s", strerror(errno));
345 assert(a.sa.sa_family == AF_INET);
346 return (ntohs(a.sin.sin_port));
347}
348
349/* --- @p_create@ --- *
350 *
351 * Arguments: @const char *name@ = name for this peer
352 * @struct sockaddr *sa@ = socket address of peer
353 * @size_t sz@ = size of socket address
354 *
355 * Returns: Pointer to the peer block, or null if it failed.
356 *
357 * Use: Creates a new named peer block. No peer is actually attached
358 * by this point.
359 */
360
361peer *p_create(const char *name, struct sockaddr *sa, size_t sz)
362{
363 peer *p = CREATE(peer);
364 T( trace(T_PEER, "peer: creating new peer `%s'", name); )
365 p->name = xstrdup(name);
366 p->ks = 0;
367 p->prev = 0;
368 memcpy(&p->peer.sa, sa, sz);
369 p->sasz = sz;
5bb41301 370 memset(&p->st, 0, sizeof(stats));
371 p->st.t_start = time(0);
410c8acf 372 if (kx_init(&p->kx, p, &p->ks))
373 goto tidy_0;
374 if (tun_create(&p->t, p))
375 goto tidy_1;
376 p->next = peers;
377 if (peers)
378 peers->prev = p;
379 peers = p;
380 return (p);
381
382tidy_1:
383 kx_free(&p->kx);
384tidy_0:
385 xfree(p->name);
386 DESTROY(p);
387 return (0);
388}
389
390/* --- @p_name@ --- *
391 *
392 * Arguments: @peer *p@ = pointer to a peer block
393 *
394 * Returns: A pointer to the peer's name.
395 */
396
397const char *p_name(peer *p) { return (p->name); }
398
399/* --- @p_find@ --- *
400 *
401 * Arguments: @const char *name@ = name to look up
402 *
403 * Returns: Pointer to the peer block, or null if not found.
404 *
405 * Use: Finds a peer by name.
406 */
407
408peer *p_find(const char *name)
409{
410 peer *p;
411 for (p = peers; p; p = p->next) {
412 if (strcmp(name, p->name) == 0)
413 return (p);
414 }
415 return (0);
416}
417
418/* --- @p_destroy@ --- *
419 *
420 * Arguments: @peer *p@ = pointer to a peer
421 *
422 * Returns: ---
423 *
424 * Use: Destroys a peer.
425 */
426
427void p_destroy(peer *p)
428{
429 T( trace(T_PEER, "peer: destroying peer `%s'", p->name); )
5bb41301 430 ksl_free(&p->ks);
410c8acf 431 kx_free(&p->kx);
432 tun_destroy(&p->t);
433 xfree(p->name);
434 if (p->next)
435 p->next->prev = p->prev;
436 if (p->prev)
437 p->prev->next = p->next;
438 else
439 peers = p->next;
440 DESTROY(p);
441}
442
443/* --- @p_first@, @p_next@ --- *
444 *
445 * Arguments: @peer *p@ = a peer block
446 *
447 * Returns: @peer_first@ returns the first peer in some ordering;
448 * @peer_next@ returns the peer following a given one in the
449 * same ordering. Null is returned for the end of the list.
450 */
451
452peer *p_first(void) { return (peers); }
453peer *p_next(peer *p) { return (p->next); }
454
455/*----- That's all, folks -------------------------------------------------*/