fixed some thread safety bugs
[anytun.git] / src / anytun.cpp
blob64da1309bf6da25a51ef5e7e01b439639ee07bce
1 /*
2 * anytun
4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <iostream>
32 #include <fstream>
33 #include <poll.h>
34 #include <fcntl.h>
35 #include <pwd.h>
36 #include <grp.h>
37 #include <sys/wait.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
41 #include <pthread.h>
42 #include <gcrypt.h>
43 #include <cerrno> // for ENOMEM
45 #include "datatypes.h"
47 #include "log.h"
48 #include "buffer.h"
49 #include "plainPacket.h"
50 #include "encryptedPacket.h"
51 #include "cipher.h"
52 #include "keyDerivation.h"
53 #include "authAlgo.h"
54 #include "cipherFactory.h"
55 #include "authAlgoFactory.h"
56 #include "keyDerivationFactory.h"
57 #include "signalController.h"
58 #include "packetSource.h"
59 #include "tunDevice.h"
60 #include "options.h"
61 #include "seqWindow.h"
62 #include "connectionList.h"
63 #include "routingTable.h"
64 #include "networkAddress.h"
66 #include "syncQueue.h"
67 #include "syncCommand.h"
69 #ifndef ANYTUN_NOSYNC
70 #include "syncSocketHandler.h"
71 #include "syncListenSocket.h"
73 #include "syncSocket.h"
74 #include "syncClientSocket.h"
75 #endif
77 #include "threadParam.h"
79 #define MAX_PACKET_LENGTH 1600
81 #define SESSION_KEYLEN_AUTH 20 // TODO: hardcoded size
82 #define SESSION_KEYLEN_ENCR 16 // TODO: hardcoded size
83 #define SESSION_KEYLEN_SALT 14 // TODO: hardcoded size
85 void createConnection(const std::string & remote_host, u_int16_t remote_port, ConnectionList & cl, u_int16_t seqSize, SyncQueue & queue, mux_t mux)
87 SeqWindow * seq= new SeqWindow(seqSize);
88 seq_nr_t seq_nr_=0;
89 KeyDerivation * kd = KeyDerivationFactory::create(gOpt.getKdPrf());
90 kd->init(gOpt.getKey(), gOpt.getSalt());
91 cLog.msg(Log::PRIO_NOTICE) << "added connection remote host " << remote_host << ":" << remote_port;
92 ConnectionParam connparam ( (*kd), (*seq), seq_nr_, remote_host, remote_port);
93 cl.addConnection(connparam,mux);
94 NetworkAddress addr(ipv4,gOpt.getIfconfigParamRemoteNetmask().c_str());
95 NetworkPrefix prefix(addr,32);
96 gRoutingTable.addRoute(prefix,mux);
97 SyncCommand sc (cl,mux);
98 queue.push(sc);
99 SyncCommand sc2 (prefix);
100 queue.push(sc2);
103 bool checkPacketSeqNr(EncryptedPacket& pack,ConnectionParam& conn)
105 // compare sender_id and seq with window
106 if(conn.seq_window_.hasSeqNr(pack.getSenderId(), pack.getSeqNr()))
108 cLog.msg(Log::PRIO_NOTICE) << "Replay attack from " << conn.remote_host_<<":"<< conn.remote_port_
109 << " seq:"<<pack.getSeqNr() << " sid: "<<pack.getSenderId();
110 return false;
113 conn.seq_window_.addSeqNr(pack.getSenderId(), pack.getSeqNr());
114 return true;
117 void* sender(void* p)
119 try
121 ThreadParam* param = reinterpret_cast<ThreadParam*>(p);
123 std::auto_ptr<Cipher> c(CipherFactory::create(gOpt.getCipher()));
124 std::auto_ptr<AuthAlgo> a(AuthAlgoFactory::create(gOpt.getAuthAlgo()) );
126 PlainPacket plain_packet(MAX_PACKET_LENGTH);
127 EncryptedPacket encrypted_packet(MAX_PACKET_LENGTH);
129 Buffer session_key(u_int32_t(SESSION_KEYLEN_ENCR)); // TODO: hardcoded size
130 Buffer session_salt(u_int32_t(SESSION_KEYLEN_SALT)); // TODO: hardcoded size
131 Buffer session_auth_key(u_int32_t(SESSION_KEYLEN_AUTH)); // TODO: hardcoded size
133 //TODO replace mux
134 u_int16_t mux = gOpt.getMux();
135 while(1)
137 plain_packet.setLength(MAX_PACKET_LENGTH);
138 encrypted_packet.withAuthTag(false);
139 encrypted_packet.setLength(MAX_PACKET_LENGTH);
141 // read packet from device
142 u_int32_t len = param->dev.read(plain_packet.getPayload(), plain_packet.getPayloadLength());
143 plain_packet.setPayloadLength(len);
144 // set payload type
145 if(param->dev.getType() == TYPE_TUN)
146 plain_packet.setPayloadType(PAYLOAD_TYPE_TUN);
147 else if(param->dev.getType() == TYPE_TAP)
148 plain_packet.setPayloadType(PAYLOAD_TYPE_TAP);
149 else
150 plain_packet.setPayloadType(0);
152 if(param->cl.empty())
153 continue;
154 //std::cout << "got Packet for plain "<<plain_packet.getDstAddr().toString();
155 mux = gRoutingTable.getRoute(plain_packet.getDstAddr());
156 //std::cout << " -> "<<mux << std::endl;
157 ConnectionMap::iterator cit = param->cl.getConnection(mux);
158 if(cit==param->cl.getEnd())
159 continue;
160 ConnectionParam & conn = cit->second;
162 if(conn.remote_host_==""||!conn.remote_port_)
163 continue;
164 // generate packet-key TODO: do this only when needed
165 conn.kd_.generate(LABEL_SATP_ENCRYPTION, conn.seq_nr_, session_key);
166 conn.kd_.generate(LABEL_SATP_SALT, conn.seq_nr_, session_salt);
168 c->setKey(session_key);
169 c->setSalt(session_salt);
171 // encrypt packet
172 c->encrypt(plain_packet, encrypted_packet, conn.seq_nr_, gOpt.getSenderId(), mux);
174 encrypted_packet.setHeader(conn.seq_nr_, gOpt.getSenderId(), mux);
175 conn.seq_nr_++;
177 // add authentication tag
178 if(a->getMaxLength()) {
179 encrypted_packet.addAuthTag();
180 conn.kd_.generate(LABEL_SATP_MSG_AUTH, encrypted_packet.getSeqNr(), session_auth_key);
181 a->setKey(session_auth_key);
182 a->generate(encrypted_packet);
186 param->src.send(encrypted_packet.getBuf(), encrypted_packet.getLength(), conn.remote_host_, conn.remote_port_);
188 catch (std::exception e)
190 // ignoring icmp port unreachable :) and other socket errors :(
194 catch(std::exception e)
196 cLog.msg(Log::PRIO_ERR) << "sender thread died due to an uncaught exception: " << e.what();
198 pthread_exit(NULL);
201 #ifndef ANYTUN_NOSYNC
202 void* syncConnector(void* p )
204 ThreadParam* param = reinterpret_cast<ThreadParam*>(p);
206 SocketHandler h;
207 SyncClientSocket sock(h,param->cl);
208 // sock.EnableSSL();
209 sock.Open( param->connto.host, param->connto.port);
210 h.Add(&sock);
211 while (h.GetCount())
213 h.Select();
215 pthread_exit(NULL);
218 void* syncListener(void* p )
220 ThreadParam* param = reinterpret_cast<ThreadParam*>(p);
222 SyncSocketHandler h(param->queue);
223 SyncListenSocket<SyncSocket,ConnectionList> l(h,param->cl);
225 if (l.Bind(gOpt.getLocalSyncPort()))
226 pthread_exit(NULL);
228 Utility::ResolveLocal(); // resolve local hostname
229 h.Add(&l);
230 h.Select(1,0);
231 while (1) {
232 h.Select(1,0);
235 #endif
237 void* receiver(void* p)
241 ThreadParam* param = reinterpret_cast<ThreadParam*>(p);
243 std::auto_ptr<Cipher> c( CipherFactory::create(gOpt.getCipher()) );
244 std::auto_ptr<AuthAlgo> a( AuthAlgoFactory::create(gOpt.getAuthAlgo()) );
246 EncryptedPacket encrypted_packet(MAX_PACKET_LENGTH);
247 PlainPacket plain_packet(MAX_PACKET_LENGTH);
249 Buffer session_key(u_int32_t(SESSION_KEYLEN_ENCR)); // TODO: hardcoded size
250 Buffer session_salt(u_int32_t(SESSION_KEYLEN_SALT)); // TODO: hardcoded size
251 Buffer session_auth_key(u_int32_t(SESSION_KEYLEN_AUTH)); // TODO: hardcoded size
253 while(1)
255 string remote_host;
256 u_int16_t remote_port;
258 plain_packet.setLength(MAX_PACKET_LENGTH);
259 encrypted_packet.withAuthTag(false);
260 encrypted_packet.setLength(MAX_PACKET_LENGTH);
262 // read packet from socket
263 u_int32_t len = param->src.recv(encrypted_packet.getBuf(), encrypted_packet.getLength(), remote_host, remote_port);
264 encrypted_packet.setLength(len);
266 mux_t mux = encrypted_packet.getMux();
267 // autodetect peer
268 if(gOpt.getRemoteAddr() == "" && param->cl.empty())
270 cLog.msg(Log::PRIO_NOTICE) << "autodetected remote host " << remote_host << ":" << remote_port;
271 createConnection(remote_host, remote_port, param->cl, gOpt.getSeqWindowSize(),param->queue,mux);
274 ConnectionMap::iterator cit = param->cl.getConnection(mux);
275 if (cit == param->cl.getEnd())
276 continue;
277 ConnectionParam & conn = cit->second;
279 // check whether auth tag is ok or not
280 if(a->getMaxLength()) {
281 encrypted_packet.withAuthTag(true);
282 conn.kd_.generate(LABEL_SATP_MSG_AUTH, encrypted_packet.getSeqNr(), session_auth_key);
283 a->setKey(session_auth_key);
284 if(!a->checkTag(encrypted_packet)) {
285 cLog.msg(Log::PRIO_NOTICE) << "wrong Authentication Tag!" << std::endl;
286 continue;
288 encrypted_packet.removeAuthTag();
291 //Allow dynamic IP changes
292 //TODO: add command line option to turn this off
293 if (remote_host != conn.remote_host_ || remote_port != conn.remote_port_)
295 cLog.msg(Log::PRIO_NOTICE) << "connection "<< mux << " autodetected remote host ip changed "
296 << remote_host << ":" << remote_port;
297 conn.remote_host_=remote_host;
298 conn.remote_port_=remote_port;
299 SyncCommand sc (param->cl,mux);
300 param->queue.push(sc);
303 // Replay Protection
304 if (!checkPacketSeqNr(encrypted_packet, conn))
305 continue;
307 // generate packet-key
308 conn.kd_.generate(LABEL_SATP_ENCRYPTION, encrypted_packet.getSeqNr(), session_key);
309 conn.kd_.generate(LABEL_SATP_SALT, encrypted_packet.getSeqNr(), session_salt);
310 c->setKey(session_key);
311 c->setSalt(session_salt);
313 // decrypt packet
314 c->decrypt(encrypted_packet, plain_packet);
316 // check payload_type
317 if((param->dev.getType() == TYPE_TUN && plain_packet.getPayloadType() != PAYLOAD_TYPE_TUN4 &&
318 plain_packet.getPayloadType() != PAYLOAD_TYPE_TUN6) ||
319 (param->dev.getType() == TYPE_TAP && plain_packet.getPayloadType() != PAYLOAD_TYPE_TAP))
320 continue;
322 // write it on the device
323 param->dev.write(plain_packet.getPayload(), plain_packet.getLength());
326 catch(std::exception e)
328 cLog.msg(Log::PRIO_ERR) << "receiver thread died due to an uncaught exception: " << e.what();
330 pthread_exit(NULL);
333 #define MIN_GCRYPT_VERSION "1.2.0"
334 #if defined(__GNUC__) && !defined(__OpenBSD__) // TODO: thread-safety on OpenBSD
335 // make libgcrypt thread safe
336 extern "C" {
337 GCRY_THREAD_OPTION_PTHREAD_IMPL;
339 #endif
341 bool initLibGCrypt()
343 // make libgcrypt thread safe
344 // this must be called before any other libgcrypt call
345 #if defined(__GNUC__) && !defined(__OpenBSD__) // TODO: thread-safety on OpenBSD
346 gcry_control( GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread );
347 #endif
349 // this must be called right after the GCRYCTL_SET_THREAD_CBS command
350 // no other function must be called till now
351 if( !gcry_check_version( MIN_GCRYPT_VERSION ) ) {
352 std::cout << "initLibGCrypt: Invalid Version of libgcrypt, should be >= " << MIN_GCRYPT_VERSION << std::endl;
353 return false;
356 gcry_error_t err = gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
357 if( err ) {
358 char buf[NL_TEXTMAX];
359 std::cout << "initLibGCrypt: Failed to disable secure memory: " << gpg_strerror_r(err, buf, NL_TEXTMAX) << std::endl;
360 return false;
363 // Tell Libgcrypt that initialization has completed.
364 err = gcry_control(GCRYCTL_INITIALIZATION_FINISHED);
365 if( err ) {
366 char buf[NL_TEXTMAX];
367 std::cout << "initLibGCrypt: Failed to finish initialization: " << gpg_strerror_r(err, buf, NL_TEXTMAX) << std::endl;
368 return false;
371 cLog.msg(Log::PRIO_NOTICE) << "initLibGCrypt: libgcrypt init finished";
372 return true;
375 void chrootAndDrop(std::string const& chrootdir, std::string const& username)
377 if (getuid() != 0)
379 std::cerr << "this programm has to be run as root in order to run in a chroot" << std::endl;
380 exit(-1);
383 struct passwd *pw = getpwnam(username.c_str());
384 if(pw) {
385 if(chroot(chrootdir.c_str()))
387 std::cerr << "can't chroot to " << chrootdir << std::endl;
388 exit(-1);
390 cLog.msg(Log::PRIO_NOTICE) << "we are in chroot jail (" << chrootdir << ") now" << std::endl;
391 chdir("/");
392 if (initgroups(pw->pw_name, pw->pw_gid) || setgid(pw->pw_gid) || setuid(pw->pw_uid))
394 std::cerr << "can't drop to user " << username << " " << pw->pw_uid << ":" << pw->pw_gid << std::endl;
395 exit(-1);
397 cLog.msg(Log::PRIO_NOTICE) << "dropped user to " << username << " " << pw->pw_uid << ":" << pw->pw_gid << std::endl;
399 else
401 std::cerr << "unknown user " << username << std::endl;
402 exit(-1);
406 void daemonize()
408 pid_t pid;
410 pid = fork();
411 if(pid) exit(0);
412 setsid();
413 pid = fork();
414 if(pid) exit(0);
416 // std::cout << "running in background now..." << std::endl;
418 int fd;
419 // for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors
420 for (fd=0;fd<=2;fd++) // close all file descriptors
421 close(fd);
422 fd=open("/dev/null",O_RDWR); // stdin
423 dup(fd); // stdout
424 dup(fd); // stderr
425 umask(027);
428 int execScript(string const& script, string const& ifname)
430 pid_t pid;
431 pid = fork();
432 if(!pid) {
433 int fd;
434 for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors
435 close(fd);
436 fd=open("/dev/null",O_RDWR); // stdin
437 dup(fd); // stdout
438 dup(fd); // stderr
439 return execl("/bin/sh", "/bin/sh", script.c_str(), ifname.c_str(), NULL);
441 int status = 0;
442 waitpid(pid, &status, 0);
443 return status;
446 int main(int argc, char* argv[])
448 bool daemonized=false;
449 try
452 // std::cout << "anytun - secure anycast tunneling protocol" << std::endl;
453 if(!gOpt.parse(argc, argv)) {
454 gOpt.printUsage();
455 exit(-1);
458 cLog.msg(Log::PRIO_NOTICE) << "anytun started...";
460 std::ofstream pidFile;
461 if(gOpt.getPidFile() != "") {
462 pidFile.open(gOpt.getPidFile().c_str());
463 if(!pidFile.is_open()) {
464 std::cout << "can't open pid file" << std::endl;
468 TunDevice dev(gOpt.getDevName() =="" ? NULL : gOpt.getDevName().c_str(),
469 gOpt.getDevType() =="" ? NULL : gOpt.getDevType().c_str(),
470 gOpt.getIfconfigParamLocal() =="" ? NULL : gOpt.getIfconfigParamLocal().c_str(),
471 gOpt.getIfconfigParamRemoteNetmask() =="" ? NULL : gOpt.getIfconfigParamRemoteNetmask().c_str());
472 cLog.msg(Log::PRIO_NOTICE) << "dev created (opened)";
473 cLog.msg(Log::PRIO_NOTICE) << "dev opened - actual name is '" << dev.getActualName() << "'";
474 cLog.msg(Log::PRIO_NOTICE) << "dev type is '" << dev.getTypeString() << "'";
475 if(gOpt.getPostUpScript() != "") {
476 int postup_ret = execScript(gOpt.getPostUpScript(), dev.getActualName());
477 cLog.msg(Log::PRIO_NOTICE) << "post up script '" << gOpt.getPostUpScript() << "' returned " << postup_ret;
481 // Buffer buff(u_int32_t(1600));
482 // int len;
483 // while(1)
484 // {
485 // len = dev.read(buff.getBuf(), buff.getLength());
486 // std::cout << "read " << len << " bytes from interface " << dev.getActualName() << std::endl;
487 // dev.write(buff.getBuf(), len);
488 // }
490 // return 0;
494 if(gOpt.getChroot())
495 chrootAndDrop(gOpt.getChrootDir(), gOpt.getUsername());
496 if(gOpt.getDaemonize())
497 daemonize();
498 daemonized = true;
500 if(pidFile.is_open()) {
501 pid_t pid = getpid();
502 pidFile << pid;
503 pidFile.close();
506 SignalController sig;
507 sig.init();
509 PacketSource* src;
510 if(gOpt.getLocalAddr() == "")
511 src = new UDPPacketSource(gOpt.getLocalPort());
512 else
513 src = new UDPPacketSource(gOpt.getLocalAddr(), gOpt.getLocalPort());
515 ConnectionList cl;
516 ConnectToList connect_to = gOpt.getConnectTo();
517 SyncQueue queue;
519 if(gOpt.getRemoteAddr() != "")
520 createConnection(gOpt.getRemoteAddr(),gOpt.getRemotePort(),cl,gOpt.getSeqWindowSize(), queue, gOpt.getMux());
522 ThreadParam p(dev, *src, cl, queue,*(new OptionConnectTo()));
524 // this must be called before any other libgcrypt call
525 if(!initLibGCrypt())
526 return -1;
528 pthread_t senderThread;
529 pthread_create(&senderThread, NULL, sender, &p);
530 pthread_t receiverThread;
531 pthread_create(&receiverThread, NULL, receiver, &p);
532 #ifndef ANYTUN_NOSYNC
533 pthread_t syncListenerThread;
534 if ( gOpt.getLocalSyncPort())
535 pthread_create(&syncListenerThread, NULL, syncListener, &p);
537 std::list<pthread_t> connectThreads;
538 for(ConnectToList::iterator it = connect_to.begin() ;it != connect_to.end(); ++it) {
539 connectThreads.push_back(pthread_t());
540 ThreadParam * point = new ThreadParam(dev, *src, cl, queue,*it);
541 pthread_create(& connectThreads.back(), NULL, syncConnector, point);
543 #endif
545 int ret = sig.run();
547 pthread_cancel(senderThread);
548 pthread_cancel(receiverThread);
549 #ifndef ANYTUN_NOSYNC
550 if ( gOpt.getLocalSyncPort())
551 pthread_cancel(syncListenerThread);
552 for( std::list<pthread_t>::iterator it = connectThreads.begin() ;it != connectThreads.end(); ++it)
553 pthread_cancel(*it);
554 #endif
556 pthread_join(senderThread, NULL);
557 pthread_join(receiverThread, NULL);
558 #ifndef ANYTUN_NOSYNC
559 if ( gOpt.getLocalSyncPort())
560 pthread_join(syncListenerThread, NULL);
562 for( std::list<pthread_t>::iterator it = connectThreads.begin() ;it != connectThreads.end(); ++it)
563 pthread_join(*it, NULL);
564 #endif
565 delete src;
566 delete &p.connto;
568 return ret;
570 catch(std::exception e)
572 if(daemonized)
573 cLog.msg(Log::PRIO_ERR) << "uncaught exception, exiting: " << e.what();
574 else
575 std::cout << "uncaught exception, exiting: " << e.what() << std::endl;