3 * $Id: peer.c,v 1.8 2003/05/16 12:09:03 mdw Exp $
5 * Communication with the peer
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.8 2003/05/16 12:09:03 mdw
33 * Allow binding to a chosen address.
35 * Revision 1.7 2003/04/15 14:12:05 mdw
36 * Insert a newline to improve readability.
38 * Revision 1.6 2001/06/19 22:07:59 mdw
39 * Use magic number for packet size.
41 * Revision 1.5 2001/03/03 11:15:19 mdw
42 * Set the socket send and receive buffers to maximum. At least this way,
43 * we won't drop large packets on the floor. If the administrator wants to
44 * prevent fragmentation of TrIPE messages, he can lower the MTU on the
45 * tunnel interface. Getting path-MTU stuff out of the kernel is too much
46 * system-specific hard work for this program.
48 * Revision 1.4 2001/02/16 21:40:24 mdw
49 * Change key exchange message interface. Maintain statistics.
51 * Revision 1.3 2001/02/04 17:10:58 mdw
52 * Make file descriptors be nonblocking and close-on-exec.
54 * Revision 1.2 2001/02/03 22:40:29 mdw
55 * Put timer information into the entropy pool when packets are received
56 * and on similar events. Reseed the generator on the interval timer.
58 * Revision 1.1 2001/02/03 20:26:37 mdw
63 /*----- Header files ------------------------------------------------------*/
67 /*----- Static variables --------------------------------------------------*/
69 static peer
*peers
= 0;
72 /*----- Main code ---------------------------------------------------------*/
76 * Arguments: @int fd@ = file descriptor to read from
77 * @unsigned mode@ = what happened
78 * @void *v@ = an uninteresting pointer
82 * Use: Reads a packet from somewhere.
85 static void p_read(int fd
, unsigned mode
, void *v
)
94 /* --- Read the data --- */
98 n
= recvfrom(fd
, buf_i
, sizeof(buf_i
), 0, &a
.sa
, &sz
);
100 a_warn("error reading socket: %s", strerror(errno
));
104 /* --- Find the appropriate peer --- */
106 assert(a
.sa
.sa_family
== AF_INET
);
107 for (p
= peers
; p
; p
= p
->next
) {
108 if (p
->peer
.sin
.sin_addr
.s_addr
== a
.sin
.sin_addr
.s_addr
&&
109 p
->peer
.sin
.sin_port
== a
.sin
.sin_port
)
112 a_warn("packet from unexpected peer: %s:%u",
113 inet_ntoa(a
.sin
.sin_addr
), (unsigned)ntohs(a
.sin
.sin_port
));
117 T( trace(T_PEER
, "peer: packet received from `%s'", p
->name
);
118 trace_block(T_PACKET
, "peer: packet contents", buf_i
, n
); )
120 /* --- Pick the packet apart --- */
122 p
->st
.t_last
= time(0);
125 buf_init(&b
, buf_i
, n
);
126 if ((ch
= buf_getbyte(&b
)) < 0) {
127 a_warn("bad packet from `%s': no type byte", p
->name
);
130 switch (ch
& MSG_CATMASK
) {
132 if (ch
& MSG_TYPEMASK
) {
133 a_warn("unknown packet type from `%s'", p
->name
);
137 buf_init(&bb
, buf_o
, sizeof(buf_o
));
138 if (ksl_decrypt(&p
->ks
, &b
, &bb
)) {
140 a_warn("couldn't decrypt inbound packet from `%s'", p
->name
);
145 p
->st
.sz_ipin
+= BSZ(&b
);
146 tun_inject(&p
->t
, &bb
);
149 a_warn("packet build failed");
153 kx_message(&p
->kx
, ch
& MSG_TYPEMASK
, &b
);
157 a_warn("bad packet from `%s': unknown packet type", p
->name
);
162 /* --- @p_txstart@ --- *
164 * Arguments: @peer *p@ = pointer to peer block
165 * @unsigned msg@ = message type code
167 * Returns: A pointer to a buffer to write to.
169 * Use: Starts sending to a peer. Only one send can happen at a
173 buf
*p_txstart(peer
*p
, unsigned msg
)
175 buf_init(&p
->b
, buf_o
, sizeof(buf_o
));
176 buf_putbyte(&p
->b
, msg
);
180 /* --- @p_txend@ --- *
182 * Arguments: @peer *p@ = pointer to peer block
186 * Use: Sends a packet to the peer.
189 void p_txend(peer
*p
)
192 a_warn("packet build failed");
195 IF_TRACING(T_PEER
, trace_block(T_PACKET
, "peer: sending packet",
196 BBASE(&p
->b
), BLEN(&p
->b
)); )
197 if (sendto(sock
.fd
, BBASE(&p
->b
), BLEN(&p
->b
),
198 0, &p
->peer
.sa
, p
->sasz
) < 0)
199 a_warn("packet send to `%s' failed: %s", p
->name
, strerror(errno
));
202 p
->st
.sz_out
+= BLEN(&p
->b
);
208 * Arguments: @peer *p@ = pointer to peer block
209 * @buf *b@ = buffer containing incoming packet
213 * Use: Handles a packet which needs to be sent to a peer.
216 void p_tun(peer
*p
, buf
*b
)
218 buf
*bb
= p_txstart(p
, MSG_PACKET
);
221 if (ksl_encrypt(&p
->ks
, b
, bb
))
223 if (BOK(bb
) && BLEN(bb
)) {
225 p
->st
.sz_ipout
+= BLEN(bb
);
230 /* --- @p_interval@ --- *
236 * Use: Called periodically to do tidying.
239 void p_interval(void)
244 reload
= km_interval();
245 for (p
= peers
; p
; p
= pp
) {
253 /* --- @p_stats@ --- *
255 * Arguments: @peer *p@ = pointer to a peer block
257 * Returns: A pointer to the peer's statistics.
260 stats
*p_stats(peer
*p
) { return (&p
->st
); }
262 /* --- @p_ifname@ --- *
264 * Arguments: @peer *p@ = pointer to a peer block
266 * Returns: A pointer to the peer's interface name.
269 const char *p_ifname(peer
*p
) { return (tun_ifname(&p
->t
)); }
271 /* --- @p_addr@ --- *
273 * Arguments: @peer *p@ = pointer to a peer block
275 * Returns: A pointer to the peer's address.
278 const addr
*p_addr(peer
*p
) { return (&p
->peer
); }
280 /* --- @p_init@ --- *
282 * Arguments: @struct in_addr addr@ = address to bind to
283 * @unsigned port@ = port number to listen to
287 * Use: Initializes the peer system; creates the socket.
290 void p_init(struct in_addr addr
, unsigned port
)
293 struct sockaddr_in sin
;
296 /* --- Note on socket buffer sizes --- *
298 * For some bizarre reason, Linux 2.2 (at least) doubles the socket buffer
299 * sizes I pass to @setsockopt@. I'm not putting special-case code here
300 * for Linux: BSD (at least TCPv2) does what I tell it rather than second-
304 if ((fd
= socket(PF_INET
, SOCK_DGRAM
, 0)) < 0)
305 die(EXIT_FAILURE
, "socket creation failed: %s", strerror(errno
));
307 sin
.sin_family
= AF_INET
;
309 sin
.sin_port
= htons(port
);
310 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
311 die(EXIT_FAILURE
, "bind failed: %s", strerror(errno
));
312 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &len
, sizeof(len
)) ||
313 setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &len
, sizeof(len
))) {
314 die(EXIT_FAILURE
, "failed to set socket buffer sizes: %s",
317 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
318 sel_initfile(&sel
, &sock
, fd
, SEL_READ
, p_read
, 0);
320 T( trace(T_PEER
, "peer: created socket"); )
323 /* --- @p_port@ --- *
327 * Returns: Port number used for socket.
330 unsigned p_port(void)
333 size_t sz
= sizeof(addr
);
335 if (getsockname(sock
.fd
, &a
.sa
, &sz
))
336 die(EXIT_FAILURE
, "couldn't read port number: %s", strerror(errno
));
337 assert(a
.sa
.sa_family
== AF_INET
);
338 return (ntohs(a
.sin
.sin_port
));
341 /* --- @p_create@ --- *
343 * Arguments: @const char *name@ = name for this peer
344 * @struct sockaddr *sa@ = socket address of peer
345 * @size_t sz@ = size of socket address
347 * Returns: Pointer to the peer block, or null if it failed.
349 * Use: Creates a new named peer block. No peer is actually attached
353 peer
*p_create(const char *name
, struct sockaddr
*sa
, size_t sz
)
355 peer
*p
= CREATE(peer
);
356 T( trace(T_PEER
, "peer: creating new peer `%s'", name
); )
357 p
->name
= xstrdup(name
);
360 memcpy(&p
->peer
.sa
, sa
, sz
);
362 memset(&p
->st
, 0, sizeof(stats
));
363 p
->st
.t_start
= time(0);
364 if (kx_init(&p
->kx
, p
, &p
->ks
))
366 if (tun_create(&p
->t
, p
))
382 /* --- @p_name@ --- *
384 * Arguments: @peer *p@ = pointer to a peer block
386 * Returns: A pointer to the peer's name.
389 const char *p_name(peer
*p
) { return (p
->name
); }
391 /* --- @p_find@ --- *
393 * Arguments: @const char *name@ = name to look up
395 * Returns: Pointer to the peer block, or null if not found.
397 * Use: Finds a peer by name.
400 peer
*p_find(const char *name
)
403 for (p
= peers
; p
; p
= p
->next
) {
404 if (strcmp(name
, p
->name
) == 0)
410 /* --- @p_destroy@ --- *
412 * Arguments: @peer *p@ = pointer to a peer
416 * Use: Destroys a peer.
419 void p_destroy(peer
*p
)
421 T( trace(T_PEER
, "peer: destroying peer `%s'", p
->name
); )
427 p
->next
->prev
= p
->prev
;
429 p
->prev
->next
= p
->next
;
435 /* --- @p_first@, @p_next@ --- *
437 * Arguments: @peer *p@ = a peer block
439 * Returns: @peer_first@ returns the first peer in some ordering;
440 * @peer_next@ returns the peer following a given one in the
441 * same ordering. Null is returned for the end of the list.
444 peer
*p_first(void) { return (peers
); }
445 peer
*p_next(peer
*p
) { return (p
->next
); }
447 /*----- That's all, folks -------------------------------------------------*/