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
34 #include "datatypes.h"
41 #include "signalController.h"
42 #include "packetSource.h"
43 #include "tunDevice.h"
45 #include "seqWindow.h"
47 #define PAYLOAD_TYPE_TAP 0x6558
48 #define PAYLOAD_TYPE_TUN 0x0800
62 Param
* param
= reinterpret_cast<Param
*>(p
);
67 Packet
pack(1600); // fix me... mtu size
69 // read packet from device
70 int len
= param
->dev
.read(pack
);
73 if(param
->opt
.getRemoteAddr() == "")
77 if(param
->dev
.getType() == TunDevice::TYPE_TUN
)
78 pack
.addPayloadType(PAYLOAD_TYPE_TUN
);
79 else if(param
->dev
.getType() == TunDevice::TYPE_TAP
)
80 pack
.addPayloadType(PAYLOAD_TYPE_TAP
);
82 pack
.addPayloadType(0);
85 param
->c
.cypher(pack
);
87 // add header to packet
88 pack
.addHeader(param
->opt
.getSenderId(), seq
);
90 // calc auth_tag and add it to the packet
91 auth_tag_t at
= param
->a
.calc(pack
);
94 // send it out to remote host
95 param
->src
.send(pack
, param
->opt
.getRemoteAddr(), param
->opt
.getRemotePort());
100 void* receiver(void* p
)
102 Param
* param
= reinterpret_cast<Param
*>(p
);
107 u_int16_t remote_port
;
108 Packet
pack(1600); // fix me... mtu size
110 // read packet from socket
111 u_int32_t len
= param
->src
.recv(pack
, remote_host
, remote_port
);
112 pack
.resizeBack(len
);
113 pack
.withPayloadType(true).withHeader(true).withAuthTag(true);
115 // check auth_tag and remove it
116 auth_tag_t at
= pack
.getAuthTag();
117 pack
.removeAuthTag();
118 if(at
!= param
->a
.calc(pack
))
122 if(param
->opt
.getRemoteAddr() == "")
124 param
->opt
.setRemoteAddrPort(remote_host
, remote_port
);
125 cLog
.msg(Log::PRIO_NOTICE
) << "autodetected remote host " << remote_host
<< ":" << remote_port
;
127 // compare sender_id and seq with window
128 if(param
->seq
.hasSeqNr(pack
.getSenderId(), pack
.getSeqNr()))
130 param
->seq
.addSeqNr(pack
.getSenderId(), pack
.getSeqNr());
133 // decypher the packet
134 param
->c
.cypher(pack
);
136 // check payload_type and remove it
137 if((param
->dev
.getType() == TunDevice::TYPE_TUN
&& pack
.getPayloadType() != PAYLOAD_TYPE_TUN
) ||
138 (param
->dev
.getType() == TunDevice::TYPE_TAP
&& pack
.getPayloadType() != PAYLOAD_TYPE_TAP
))
140 pack
.removePayloadType();
142 // write it on the device
143 param
->dev
.write(pack
);
148 int main(int argc
, char* argv
[])
150 std::cout
<< "anytun - secure anycast tunneling protocol" << std::endl
;
152 if(!opt
.parse(argc
, argv
))
157 cLog
.msg(Log::PRIO_NOTICE
) << "anytun started...";
159 SignalController sig
;
162 TunDevice
dev(opt
.getDevName().c_str(), opt
.getIfconfigParamLocal().c_str(), opt
.getIfconfigParamRemoteNetmask().c_str());
163 SeqWindow
seq(opt
.getSeqWindowSize());
167 if(opt
.getLocalAddr() == "")
168 src
= new UDPPacketSource(opt
.getLocalPort());
170 src
= new UDPPacketSource(opt
.getLocalAddr(), opt
.getLocalPort());
172 struct Param p
= {opt
, dev
, c
, a
, *src
, seq
};
174 std::cout
<< "dev created (opened)" << std::endl
;
175 std::cout
<< "dev opened - actual name is '" << p
.dev
.getActualName() << "'" << std::endl
;
176 std::cout
<< "dev type is '" << p
.dev
.getTypeString() << "'" << std::endl
;
178 pthread_t senderThread
;
179 pthread_create(&senderThread
, NULL
, sender
, &p
);
180 pthread_t receiverThread
;
181 pthread_create(&receiverThread
, NULL
, receiver
, &p
);
185 pthread_cancel(senderThread
);
186 pthread_cancel(receiverThread
);
187 pthread_join(senderThread
, NULL
);
188 pthread_join(receiverThread
, NULL
);