| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950 |
- /*
- * Copyright (C) Ambroz Bizjak <ambrop7@gmail.com>
- * Contributions:
- * Transparent DNS: Copyright (C) Kerem Hadimli <kerem.hadimli@gmail.com>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <stdint.h>
- #include <stdio.h>
- #include <stddef.h>
- #include <string.h>
- #include <limits.h>
- #include <misc/version.h>
- #include <misc/loggers_string.h>
- #include <misc/loglevel.h>
- #include <misc/minmax.h>
- #include <misc/offset.h>
- #include <misc/dead.h>
- #include <misc/ipv4_proto.h>
- #include <misc/ipv6_proto.h>
- #include <misc/udp_proto.h>
- #include <misc/byteorder.h>
- #include <misc/balloc.h>
- #include <misc/open_standard_streams.h>
- #include <misc/read_file.h>
- #include <misc/ipaddr6.h>
- #include <misc/concat_strings.h>
- #include <structure/LinkedList1.h>
- #include <base/BLog.h>
- #include <system/BReactor.h>
- #include <system/BSignal.h>
- #include <system/BAddr.h>
- #include <system/BNetwork.h>
- #include <flow/SinglePacketBuffer.h>
- #include <socksclient/BSocksClient.h>
- #include <tuntap/BTap.h>
- #include <lwip/init.h>
- #include <lwip/ip_addr.h>
- #include <lwip/priv/tcp_priv.h>
- #include <lwip/netif.h>
- #include <lwip/tcp.h>
- #include <lwip/ip4_frag.h>
- #include <lwip/nd6.h>
- #include <lwip/ip6_frag.h>
- #include <tun2socks/SocksUdpGwClient.h>
- #include <socks_udp_client/SocksUdpClient.h>
- #ifndef BADVPN_USE_WINAPI
- #include <base/BLog_syslog.h>
- #endif
- #include <tun2socks/tun2socks.h>
- #include <generated/blog_channel_tun2socks.h>
- #define LOGGER_STDOUT 1
- #define LOGGER_SYSLOG 2
- #define SYNC_DECL \
- BPending sync_mark; \
- #define SYNC_FROMHERE \
- BPending_Init(&sync_mark, BReactor_PendingGroup(&ss), NULL, NULL); \
- BPending_Set(&sync_mark);
- #define SYNC_BREAK \
- BPending_Free(&sync_mark);
- #define SYNC_COMMIT \
- BReactor_Synchronize(&ss, &sync_mark.base); \
- BPending_Free(&sync_mark);
- // command-line options
- struct {
- int help;
- int version;
- int logger;
- #ifndef BADVPN_USE_WINAPI
- char *logger_syslog_facility;
- char *logger_syslog_ident;
- #endif
- int loglevel;
- int loglevels[BLOG_NUM_CHANNELS];
- char *tundev;
- char *netif_ipaddr;
- char *netif_netmask;
- char *netif_ip6addr;
- char *socks_server_addr;
- char *username;
- char *password;
- char *password_file;
- int append_source_to_username;
- char *udpgw_remote_server_addr;
- int udpgw_max_connections;
- int udpgw_connection_buffer_size;
- int udpgw_transparent_dns;
- int socks5_udp;
- } options;
- // TCP client
- struct tcp_client {
- int aborted;
- dead_t dead_aborted;
- LinkedList1Node list_node;
- BAddr local_addr;
- BAddr remote_addr;
- struct tcp_pcb *pcb;
- int client_closed;
- uint8_t buf[TCP_WND];
- int buf_used;
- char *socks_username;
- BSocksClient socks_client;
- int socks_up;
- int socks_closed;
- StreamPassInterface *socks_send_if;
- StreamRecvInterface *socks_recv_if;
- uint8_t socks_recv_buf[CLIENT_SOCKS_RECV_BUF_SIZE];
- int socks_recv_buf_used;
- int socks_recv_buf_sent;
- int socks_recv_waiting;
- int socks_recv_tcp_pending;
- };
- // IP address of netif
- BIPAddr netif_ipaddr;
- // netmask of netif
- BIPAddr netif_netmask;
- // IP6 address of netif
- struct ipv6_addr netif_ip6addr;
- // SOCKS server address
- BAddr socks_server_addr;
- // allocated password file contents
- uint8_t *password_file_contents;
- // SOCKS authentication information
- struct BSocksClient_auth_info socks_auth_info[2];
- size_t socks_num_auth_info;
- // remote udpgw server addr, if provided
- BAddr udpgw_remote_server_addr;
- // reactor
- BReactor ss;
- // set to 1 by terminate
- int quitting;
- // TUN device
- BTap device;
- // device write buffer
- uint8_t *device_write_buf;
- // device reading
- SinglePacketBuffer device_read_buffer;
- PacketPassInterface device_read_interface;
- // UDP support mode
- enum UdpMode {UdpModeNone, UdpModeUdpgw, UdpModeSocks};
- enum UdpMode udp_mode;
- // udpgw client
- SocksUdpGwClient udpgw_client;
- int udp_mtu;
- // SOCKS5-UDP client
- SocksUdpClient socks_udp_client;
- // TCP timer
- BTimer tcp_timer;
- int tcp_timer_mod4;
- // job for initializing lwip
- BPending lwip_init_job;
- // lwip netif
- int have_netif;
- struct netif the_netif;
- // lwip TCP listener
- struct tcp_pcb *listener;
- // lwip TCP/IPv6 listener
- struct tcp_pcb *listener_ip6;
- // TCP clients
- LinkedList1 tcp_clients;
- // number of clients
- int num_clients;
- static void terminate (void);
- static void print_help (const char *name);
- static void print_version (void);
- static int parse_arguments (int argc, char *argv[]);
- static int process_arguments (void);
- static void signal_handler (void *unused);
- static BAddr baddr_from_lwip (const ip_addr_t *ip_addr, uint16_t port_hostorder);
- static void lwip_init_job_hadler (void *unused);
- static void tcp_timer_handler (void *unused);
- static void device_error_handler (void *unused);
- static void device_read_handler_send (void *unused, uint8_t *data, int data_len);
- static int process_device_udp_packet (uint8_t *data, int data_len);
- static err_t netif_init_func (struct netif *netif);
- static err_t netif_output_func (struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr);
- static err_t netif_output_ip6_func (struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr);
- static err_t common_netif_output (struct netif *netif, struct pbuf *p);
- static err_t netif_input_func (struct pbuf *p, struct netif *inp);
- static void client_logfunc (struct tcp_client *client);
- static void client_log (struct tcp_client *client, int level, const char *fmt, ...);
- static err_t listener_accept_func (void *arg, struct tcp_pcb *newpcb, err_t err);
- static void client_handle_freed_client (struct tcp_client *client);
- static void client_free_client (struct tcp_client *client);
- static void client_abort_client (struct tcp_client *client);
- static void client_abort_pcb (struct tcp_client *client);
- static void client_free_socks (struct tcp_client *client);
- static void client_murder (struct tcp_client *client);
- static void client_dealloc (struct tcp_client *client);
- static void client_err_func (void *arg, err_t err);
- static err_t client_recv_func (void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
- static void client_socks_handler (struct tcp_client *client, int event);
- static void client_send_to_socks (struct tcp_client *client);
- static void client_socks_send_handler_done (struct tcp_client *client, int data_len);
- static void client_socks_recv_initiate (struct tcp_client *client);
- static void client_socks_recv_handler_done (struct tcp_client *client, int data_len);
- static int client_socks_recv_send_out (struct tcp_client *client);
- static err_t client_sent_func (void *arg, struct tcp_pcb *tpcb, u16_t len);
- static void udp_send_packet_to_device (void *unused, BAddr local_addr, BAddr remote_addr, const uint8_t *data, int data_len);
- int main (int argc, char **argv)
- {
- if (argc <= 0) {
- return 1;
- }
-
- // open standard streams
- open_standard_streams();
-
- // parse command-line arguments
- if (!parse_arguments(argc, argv)) {
- fprintf(stderr, "Failed to parse arguments\n");
- print_help(argv[0]);
- goto fail0;
- }
-
- // handle --help and --version
- if (options.help) {
- print_version();
- print_help(argv[0]);
- return 0;
- }
- if (options.version) {
- print_version();
- return 0;
- }
-
- // initialize logger
- switch (options.logger) {
- case LOGGER_STDOUT:
- BLog_InitStdout();
- break;
- #ifndef BADVPN_USE_WINAPI
- case LOGGER_SYSLOG:
- if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
- fprintf(stderr, "Failed to initialize syslog logger\n");
- goto fail0;
- }
- break;
- #endif
- default:
- ASSERT(0);
- }
-
- // configure logger channels
- for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
- if (options.loglevels[i] >= 0) {
- BLog_SetChannelLoglevel(i, options.loglevels[i]);
- }
- else if (options.loglevel >= 0) {
- BLog_SetChannelLoglevel(i, options.loglevel);
- }
- }
-
- BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
-
- // clear password contents pointer
- password_file_contents = NULL;
-
- // initialize network
- if (!BNetwork_GlobalInit()) {
- BLog(BLOG_ERROR, "BNetwork_GlobalInit failed");
- goto fail1;
- }
-
- // process arguments
- if (!process_arguments()) {
- BLog(BLOG_ERROR, "Failed to process arguments");
- goto fail1;
- }
-
- // init time
- BTime_Init();
-
- // init reactor
- if (!BReactor_Init(&ss)) {
- BLog(BLOG_ERROR, "BReactor_Init failed");
- goto fail1;
- }
-
- // set not quitting
- quitting = 0;
-
- // setup signal handler
- if (!BSignal_Init(&ss, signal_handler, NULL)) {
- BLog(BLOG_ERROR, "BSignal_Init failed");
- goto fail2;
- }
-
- // init TUN device
- if (!BTap_Init(&device, &ss, options.tundev, device_error_handler, NULL, 1)) {
- BLog(BLOG_ERROR, "BTap_Init failed");
- goto fail3;
- }
-
- // NOTE: the order of the following is important:
- // first device writing must evaluate,
- // then lwip (so it can send packets to the device),
- // then device reading (so it can pass received packets to lwip).
-
- // init device reading
- PacketPassInterface_Init(&device_read_interface, BTap_GetMTU(&device), device_read_handler_send, NULL, BReactor_PendingGroup(&ss));
- if (!SinglePacketBuffer_Init(&device_read_buffer, BTap_GetOutput(&device), &device_read_interface, BReactor_PendingGroup(&ss))) {
- BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed");
- goto fail4;
- }
-
- // Compute the largest possible UDP payload that we can receive from or send to the
- // TUN device.
- udp_mtu = BTap_GetMTU(&device) - (int)(sizeof(struct ipv4_header) + sizeof(struct udp_header));
- if (options.netif_ip6addr) {
- int udp_ip6_mtu = BTap_GetMTU(&device) - (int)(sizeof(struct ipv6_header) + sizeof(struct udp_header));
- if (udp_mtu < udp_ip6_mtu) {
- udp_mtu = udp_ip6_mtu;
- }
- }
- if (udp_mtu < 0) {
- udp_mtu = 0;
- }
- if (options.udpgw_remote_server_addr) {
- udp_mode = UdpModeUdpgw;
- // make sure our UDP payloads aren't too large for udpgw
- int udpgw_mtu = udpgw_compute_mtu(udp_mtu);
- if (udpgw_mtu < 0 || udpgw_mtu > PACKETPROTO_MAXPAYLOAD) {
- BLog(BLOG_ERROR, "device MTU is too large for UDP");
- goto fail4a;
- }
-
- // init udpgw client
- if (!SocksUdpGwClient_Init(&udpgw_client, udp_mtu, DEFAULT_UDPGW_MAX_CONNECTIONS,
- options.udpgw_connection_buffer_size, UDPGW_KEEPALIVE_TIME, socks_server_addr,
- socks_auth_info, socks_num_auth_info, udpgw_remote_server_addr,
- UDPGW_RECONNECT_TIME, &ss, NULL, udp_send_packet_to_device))
- {
- BLog(BLOG_ERROR, "SocksUdpGwClient_Init failed");
- goto fail4a;
- }
- } else if (options.socks5_udp) {
- udp_mode = UdpModeSocks;
- // init SOCKS UDP client
- SocksUdpClient_Init(&socks_udp_client, udp_mtu, DEFAULT_UDPGW_MAX_CONNECTIONS,
- SOCKS_UDP_SEND_BUFFER_PACKETS, UDPGW_KEEPALIVE_TIME, socks_server_addr,
- socks_auth_info, socks_num_auth_info, &ss, NULL, udp_send_packet_to_device);
- } else {
- udp_mode = UdpModeNone;
- }
-
- // init lwip init job
- BPending_Init(&lwip_init_job, BReactor_PendingGroup(&ss), lwip_init_job_hadler, NULL);
- BPending_Set(&lwip_init_job);
-
- // init device write buffer
- if (!(device_write_buf = (uint8_t *)BAlloc(BTap_GetMTU(&device)))) {
- BLog(BLOG_ERROR, "BAlloc failed");
- goto fail5;
- }
-
- // init TCP timer
- // it won't trigger before lwip is initialized, becuase the lwip init is a job
- BTimer_Init(&tcp_timer, TCP_TMR_INTERVAL, tcp_timer_handler, NULL);
- BReactor_SetTimer(&ss, &tcp_timer);
- tcp_timer_mod4 = 0;
-
- // set no netif
- have_netif = 0;
-
- // set no listener
- listener = NULL;
- listener_ip6 = NULL;
-
- // init clients list
- LinkedList1_Init(&tcp_clients);
-
- // init number of clients
- num_clients = 0;
-
- // enter event loop
- BLog(BLOG_NOTICE, "entering event loop");
- BReactor_Exec(&ss);
-
- // free clients
- LinkedList1Node *node;
- while (node = LinkedList1_GetFirst(&tcp_clients)) {
- struct tcp_client *client = UPPER_OBJECT(node, struct tcp_client, list_node);
- client_murder(client);
- }
-
- // free listener
- if (listener_ip6) {
- tcp_close(listener_ip6);
- }
- if (listener) {
- tcp_close(listener);
- }
-
- // free netif
- if (have_netif) {
- netif_remove(&the_netif);
- }
-
- BReactor_RemoveTimer(&ss, &tcp_timer);
- BFree(device_write_buf);
- fail5:
- BPending_Free(&lwip_init_job);
- if (udp_mode == UdpModeUdpgw) {
- SocksUdpGwClient_Free(&udpgw_client);
- } else if (udp_mode == UdpModeSocks) {
- SocksUdpClient_Free(&socks_udp_client);
- }
- fail4a:
- SinglePacketBuffer_Free(&device_read_buffer);
- fail4:
- PacketPassInterface_Free(&device_read_interface);
- BTap_Free(&device);
- fail3:
- BSignal_Finish();
- fail2:
- BReactor_Free(&ss);
- fail1:
- BFree(password_file_contents);
- BLog(BLOG_NOTICE, "exiting");
- BLog_Free();
- fail0:
- DebugObjectGlobal_Finish();
-
- return 1;
- }
- void terminate (void)
- {
- ASSERT(!quitting)
-
- BLog(BLOG_NOTICE, "tearing down");
-
- // set quitting
- quitting = 1;
-
- // exit event loop
- BReactor_Quit(&ss, 1);
- }
- void print_help (const char *name)
- {
- printf(
- "Usage:\n"
- " %s\n"
- " [--help]\n"
- " [--version]\n"
- " [--logger <"LOGGERS_STRING">]\n"
- #ifndef BADVPN_USE_WINAPI
- " (logger=syslog?\n"
- " [--syslog-facility <string>]\n"
- " [--syslog-ident <string>]\n"
- " )\n"
- #endif
- " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
- " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
- " [--tundev <name>]\n"
- " --netif-ipaddr <ipaddr>\n"
- " --netif-netmask <ipnetmask>\n"
- " --socks-server-addr <addr>\n"
- " [--netif-ip6addr <addr>]\n"
- " [--username <username>]\n"
- " [--password <password>]\n"
- " [--password-file <file>]\n"
- " [--append-source-to-username]\n"
- " [--udpgw-remote-server-addr <addr>]\n"
- " [--udpgw-max-connections <number>]\n"
- " [--udpgw-connection-buffer-size <number>]\n"
- " [--udpgw-transparent-dns]\n"
- " [--socks5-udp]\n"
- "Address format is a.b.c.d:port (IPv4) or [addr]:port (IPv6).\n",
- name
- );
- }
- void print_version (void)
- {
- printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
- }
- int parse_arguments (int argc, char *argv[])
- {
- if (argc <= 0) {
- return 0;
- }
-
- options.help = 0;
- options.version = 0;
- options.logger = LOGGER_STDOUT;
- #ifndef BADVPN_USE_WINAPI
- options.logger_syslog_facility = "daemon";
- options.logger_syslog_ident = argv[0];
- #endif
- options.loglevel = -1;
- for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
- options.loglevels[i] = -1;
- }
- options.tundev = NULL;
- options.netif_ipaddr = NULL;
- options.netif_netmask = NULL;
- options.netif_ip6addr = NULL;
- options.socks_server_addr = NULL;
- options.username = NULL;
- options.password = NULL;
- options.password_file = NULL;
- options.append_source_to_username = 0;
- options.udpgw_remote_server_addr = NULL;
- options.udpgw_max_connections = DEFAULT_UDPGW_MAX_CONNECTIONS;
- options.udpgw_connection_buffer_size = DEFAULT_UDPGW_CONNECTION_BUFFER_SIZE;
- options.udpgw_transparent_dns = 0;
- options.socks5_udp = 0;
-
- int i;
- for (i = 1; i < argc; i++) {
- char *arg = argv[i];
- if (!strcmp(arg, "--help")) {
- options.help = 1;
- }
- else if (!strcmp(arg, "--version")) {
- options.version = 1;
- }
- else if (!strcmp(arg, "--logger")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- char *arg2 = argv[i + 1];
- if (!strcmp(arg2, "stdout")) {
- options.logger = LOGGER_STDOUT;
- }
- #ifndef BADVPN_USE_WINAPI
- else if (!strcmp(arg2, "syslog")) {
- options.logger = LOGGER_SYSLOG;
- }
- #endif
- else {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- #ifndef BADVPN_USE_WINAPI
- else if (!strcmp(arg, "--syslog-facility")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.logger_syslog_facility = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--syslog-ident")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.logger_syslog_ident = argv[i + 1];
- i++;
- }
- #endif
- else if (!strcmp(arg, "--loglevel")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--channel-loglevel")) {
- if (2 >= argc - i) {
- fprintf(stderr, "%s: requires two arguments\n", arg);
- return 0;
- }
- int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
- if (channel < 0) {
- fprintf(stderr, "%s: wrong channel argument\n", arg);
- return 0;
- }
- int loglevel = parse_loglevel(argv[i + 2]);
- if (loglevel < 0) {
- fprintf(stderr, "%s: wrong loglevel argument\n", arg);
- return 0;
- }
- options.loglevels[channel] = loglevel;
- i += 2;
- }
- else if (!strcmp(arg, "--tundev")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.tundev = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--netif-ipaddr")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.netif_ipaddr = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--netif-netmask")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.netif_netmask = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--netif-ip6addr")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.netif_ip6addr = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--socks-server-addr")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.socks_server_addr = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--username")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.username = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--password")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.password = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--password-file")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.password_file = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--append-source-to-username")) {
- options.append_source_to_username = 1;
- }
- else if (!strcmp(arg, "--udpgw-remote-server-addr")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.udpgw_remote_server_addr = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--udpgw-max-connections")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.udpgw_max_connections = atoi(argv[i + 1])) <= 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--udpgw-connection-buffer-size")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.udpgw_connection_buffer_size = atoi(argv[i + 1])) <= 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--udpgw-transparent-dns")) {
- options.udpgw_transparent_dns = 1;
- }
- else if (!strcmp(arg, "--socks5-udp")) {
- options.socks5_udp = 1;
- }
- else {
- fprintf(stderr, "unknown option: %s\n", arg);
- return 0;
- }
- }
-
- if (options.help || options.version) {
- return 1;
- }
-
- if (!options.netif_ipaddr) {
- fprintf(stderr, "--netif-ipaddr is required\n");
- return 0;
- }
-
- if (!options.netif_netmask) {
- fprintf(stderr, "--netif-netmask is required\n");
- return 0;
- }
-
- if (!options.socks_server_addr) {
- fprintf(stderr, "--socks-server-addr is required\n");
- return 0;
- }
-
- if (options.username) {
- if (!options.password && !options.password_file) {
- fprintf(stderr, "username given but password not given\n");
- return 0;
- }
-
- if (options.password && options.password_file) {
- fprintf(stderr, "--password and --password-file cannot both be given\n");
- return 0;
- }
- }
-
- return 1;
- }
- int process_arguments (void)
- {
- ASSERT(!password_file_contents)
-
- // resolve netif ipaddr
- if (!BIPAddr_Resolve(&netif_ipaddr, options.netif_ipaddr, 0)) {
- BLog(BLOG_ERROR, "netif ipaddr: BIPAddr_Resolve failed");
- return 0;
- }
- if (netif_ipaddr.type != BADDR_TYPE_IPV4) {
- BLog(BLOG_ERROR, "netif ipaddr: must be an IPv4 address");
- return 0;
- }
-
- // resolve netif netmask
- if (!BIPAddr_Resolve(&netif_netmask, options.netif_netmask, 0)) {
- BLog(BLOG_ERROR, "netif netmask: BIPAddr_Resolve failed");
- return 0;
- }
- if (netif_netmask.type != BADDR_TYPE_IPV4) {
- BLog(BLOG_ERROR, "netif netmask: must be an IPv4 address");
- return 0;
- }
-
- // parse IP6 address
- if (options.netif_ip6addr) {
- if (!ipaddr6_parse_ipv6_addr(MemRef_MakeCstr(options.netif_ip6addr), &netif_ip6addr)) {
- BLog(BLOG_ERROR, "netif ip6addr: incorrect");
- return 0;
- }
- }
-
- // resolve SOCKS server address
- if (!BAddr_Parse2(&socks_server_addr, options.socks_server_addr, NULL, 0, 0)) {
- BLog(BLOG_ERROR, "socks server addr: BAddr_Parse2 failed");
- return 0;
- }
-
- // add none socks authentication method
- socks_auth_info[0] = BSocksClient_auth_none();
- socks_num_auth_info = 1;
-
- // add password socks authentication method
- if (options.username) {
- const char *password;
- size_t password_len;
- if (options.password) {
- password = options.password;
- password_len = strlen(options.password);
- } else {
- if (!read_file(options.password_file, &password_file_contents, &password_len)) {
- BLog(BLOG_ERROR, "failed to read password file");
- return 0;
- }
- password = (char *)password_file_contents;
- }
-
- socks_auth_info[socks_num_auth_info++] = BSocksClient_auth_password(
- options.username, strlen(options.username),
- password, password_len
- );
- }
-
- // resolve remote udpgw server address
- if (options.udpgw_remote_server_addr) {
- if (!BAddr_Parse2(&udpgw_remote_server_addr, options.udpgw_remote_server_addr, NULL, 0, 0)) {
- BLog(BLOG_ERROR, "remote udpgw server addr: BAddr_Parse2 failed");
- return 0;
- }
- }
-
- return 1;
- }
- void signal_handler (void *unused)
- {
- ASSERT(!quitting)
-
- BLog(BLOG_NOTICE, "termination requested");
-
- terminate();
- }
- BAddr baddr_from_lwip (const ip_addr_t *ip_addr, uint16_t port_hostorder)
- {
- BAddr addr;
- if (IP_IS_V6(ip_addr)) {
- BAddr_InitIPv6(&addr, (uint8_t *)ip_addr->u_addr.ip6.addr, hton16(port_hostorder));
- } else {
- BAddr_InitIPv4(&addr, ip_addr->u_addr.ip4.addr, hton16(port_hostorder));
- }
- return addr;
- }
- void lwip_init_job_hadler (void *unused)
- {
- ASSERT(!quitting)
- ASSERT(netif_ipaddr.type == BADDR_TYPE_IPV4)
- ASSERT(netif_netmask.type == BADDR_TYPE_IPV4)
- ASSERT(!have_netif)
- ASSERT(!listener)
- ASSERT(!listener_ip6)
-
- BLog(BLOG_DEBUG, "lwip init");
-
- // NOTE: the device may fail during this, but there's no harm in not checking
- // for that at every step
-
- // init lwip
- lwip_init();
-
- // make addresses for netif
- ip4_addr_t addr;
- addr.addr = netif_ipaddr.ipv4;
- ip4_addr_t netmask;
- netmask.addr = netif_netmask.ipv4;
- ip4_addr_t gw;
- ip4_addr_set_any(&gw);
-
- // init netif
- if (!netif_add(&the_netif, &addr, &netmask, &gw, NULL, netif_init_func, netif_input_func)) {
- BLog(BLOG_ERROR, "netif_add failed");
- goto fail;
- }
- have_netif = 1;
-
- // set netif up
- netif_set_up(&the_netif);
-
- // set netif link up, otherwise ip route will refuse to route
- netif_set_link_up(&the_netif);
-
- // set netif pretend TCP
- netif_set_pretend_tcp(&the_netif, 1);
-
- // set netif default
- netif_set_default(&the_netif);
-
- if (options.netif_ip6addr) {
- // add IPv6 address
- ip6_addr_t ip6addr;
- memset(&ip6addr, 0, sizeof(ip6addr)); // clears any "zone"
- memcpy(ip6addr.addr, netif_ip6addr.bytes, sizeof(netif_ip6addr.bytes));
- netif_ip6_addr_set(&the_netif, 0, &ip6addr);
- netif_ip6_addr_set_state(&the_netif, 0, IP6_ADDR_VALID);
- }
-
- // init listener
- struct tcp_pcb *l = tcp_new_ip_type(IPADDR_TYPE_V4);
- if (!l) {
- BLog(BLOG_ERROR, "tcp_new_ip_type failed");
- goto fail;
- }
-
- // bind listener
- if (tcp_bind_to_netif(l, "ho0") != ERR_OK) {
- BLog(BLOG_ERROR, "tcp_bind_to_netif failed");
- tcp_close(l);
- goto fail;
- }
-
- // ensure the listener only accepts connections from this netif
- tcp_bind_netif(l, &the_netif);
-
- // listen listener
- if (!(listener = tcp_listen(l))) {
- BLog(BLOG_ERROR, "tcp_listen failed");
- tcp_close(l);
- goto fail;
- }
-
- // setup listener accept handler
- tcp_accept(listener, listener_accept_func);
-
- if (options.netif_ip6addr) {
- struct tcp_pcb *l_ip6 = tcp_new_ip_type(IPADDR_TYPE_V6);
- if (!l_ip6) {
- BLog(BLOG_ERROR, "tcp_new_ip_type failed");
- goto fail;
- }
-
- if (tcp_bind_to_netif(l_ip6, "ho0") != ERR_OK) {
- BLog(BLOG_ERROR, "tcp_bind_to_netif failed");
- tcp_close(l_ip6);
- goto fail;
- }
-
- tcp_bind_netif(l_ip6, &the_netif);
-
- if (!(listener_ip6 = tcp_listen(l_ip6))) {
- BLog(BLOG_ERROR, "tcp_listen failed");
- tcp_close(l_ip6);
- goto fail;
- }
-
- tcp_accept(listener_ip6, listener_accept_func);
- }
-
- return;
-
- fail:
- if (!quitting) {
- terminate();
- }
- }
- void tcp_timer_handler (void *unused)
- {
- ASSERT(!quitting)
-
- BLog(BLOG_DEBUG, "TCP timer");
-
- // schedule next timer
- BReactor_SetTimer(&ss, &tcp_timer);
-
- // call the TCP timer function (every 1/4 second)
- tcp_tmr();
-
- // increment tcp_timer_mod4
- tcp_timer_mod4 = (tcp_timer_mod4 + 1) % 4;
-
- // every second, call other timer functions
- if (tcp_timer_mod4 == 0) {
- #if IP_REASSEMBLY
- ASSERT(IP_TMR_INTERVAL == 4 * TCP_TMR_INTERVAL)
- ip_reass_tmr();
- #endif
-
- #if LWIP_IPV6
- ASSERT(ND6_TMR_INTERVAL == 4 * TCP_TMR_INTERVAL)
- nd6_tmr();
- #endif
-
- #if LWIP_IPV6 && LWIP_IPV6_REASS
- ASSERT(IP6_REASS_TMR_INTERVAL == 4 * TCP_TMR_INTERVAL)
- ip6_reass_tmr();
- #endif
- }
- }
- void device_error_handler (void *unused)
- {
- ASSERT(!quitting)
-
- BLog(BLOG_ERROR, "device error");
-
- terminate();
- return;
- }
- void device_read_handler_send (void *unused, uint8_t *data, int data_len)
- {
- ASSERT(!quitting)
- ASSERT(data_len >= 0)
-
- BLog(BLOG_DEBUG, "device: received packet");
-
- // accept packet
- PacketPassInterface_Done(&device_read_interface);
-
- // process UDP directly
- if (process_device_udp_packet(data, data_len)) {
- return;
- }
-
- // obtain pbuf
- if (data_len > UINT16_MAX) {
- BLog(BLOG_WARNING, "device read: packet too large");
- return;
- }
- struct pbuf *p = pbuf_alloc(PBUF_RAW, data_len, PBUF_POOL);
- if (!p) {
- BLog(BLOG_WARNING, "device read: pbuf_alloc failed");
- return;
- }
-
- // write packet to pbuf
- ASSERT_FORCE(pbuf_take(p, data, data_len) == ERR_OK)
-
- // pass pbuf to input
- if (the_netif.input(p, &the_netif) != ERR_OK) {
- BLog(BLOG_WARNING, "device read: input failed");
- pbuf_free(p);
- }
- }
- int process_device_udp_packet (uint8_t *data, int data_len)
- {
- ASSERT(data_len >= 0)
-
- // do nothing if we don't use udpgw or SOCKS UDP
- if (udp_mode == UdpModeNone) {
- goto fail;
- }
-
- BAddr local_addr;
- BAddr remote_addr;
- int is_dns;
-
- uint8_t ip_version = 0;
- if (data_len > 0) {
- ip_version = (data[0] >> 4);
- }
-
- switch (ip_version) {
- case 4: {
- // ignore non-UDP packets
- if (data_len < sizeof(struct ipv4_header) || data[offsetof(struct ipv4_header, protocol)] != IPV4_PROTOCOL_UDP) {
- goto fail;
- }
-
- // parse IPv4 header
- struct ipv4_header ipv4_header;
- if (!ipv4_check(data, data_len, &ipv4_header, &data, &data_len)) {
- goto fail;
- }
-
- // parse UDP
- struct udp_header udp_header;
- if (!udp_check(data, data_len, &udp_header, &data, &data_len)) {
- goto fail;
- }
-
- // verify UDP checksum
- uint16_t checksum_in_packet = udp_header.checksum;
- udp_header.checksum = 0;
- uint16_t checksum_computed = udp_checksum(&udp_header, data, data_len, ipv4_header.source_address, ipv4_header.destination_address);
- if (checksum_in_packet != checksum_computed) {
- goto fail;
- }
-
- BLog(BLOG_INFO, "UDP: from device %d bytes", data_len);
-
- // construct addresses
- BAddr_InitIPv4(&local_addr, ipv4_header.source_address, udp_header.source_port);
- BAddr_InitIPv4(&remote_addr, ipv4_header.destination_address, udp_header.dest_port);
-
- // if transparent DNS is enabled, any packet arriving at out netif
- // address to port 53 is considered a DNS packet
- is_dns = (options.udpgw_transparent_dns &&
- ipv4_header.destination_address == netif_ipaddr.ipv4 &&
- udp_header.dest_port == hton16(53));
- } break;
-
- case 6: {
- // ignore if IPv6 support is disabled
- if (!options.netif_ip6addr) {
- goto fail;
- }
-
- // ignore non-UDP packets
- if (data_len < sizeof(struct ipv6_header) || data[offsetof(struct ipv6_header, next_header)] != IPV6_NEXT_UDP) {
- goto fail;
- }
-
- // parse IPv6 header
- struct ipv6_header ipv6_header;
- if (!ipv6_check(data, data_len, &ipv6_header, &data, &data_len)) {
- goto fail;
- }
-
- // parse UDP
- struct udp_header udp_header;
- if (!udp_check(data, data_len, &udp_header, &data, &data_len)) {
- goto fail;
- }
-
- // verify UDP checksum
- uint16_t checksum_in_packet = udp_header.checksum;
- udp_header.checksum = 0;
- uint16_t checksum_computed = udp_ip6_checksum(&udp_header, data, data_len, ipv6_header.source_address, ipv6_header.destination_address);
- if (checksum_in_packet != checksum_computed) {
- goto fail;
- }
-
- BLog(BLOG_INFO, "UDP/IPv6: from device %d bytes", data_len);
-
- // construct addresses
- BAddr_InitIPv6(&local_addr, ipv6_header.source_address, udp_header.source_port);
- BAddr_InitIPv6(&remote_addr, ipv6_header.destination_address, udp_header.dest_port);
-
- // TODO dns
- is_dns = 0;
- } break;
-
- default: {
- goto fail;
- } break;
- }
-
- // check payload length
- if (data_len > udp_mtu) {
- BLog(BLOG_ERROR, "packet is too large, cannot send to udpgw");
- goto fail;
- }
-
- // submit packet to udpgw or SOCKS UDP
- if (udp_mode == UdpModeUdpgw) {
- SocksUdpGwClient_SubmitPacket(&udpgw_client, local_addr, remote_addr,
- is_dns, data, data_len);
- } else if (udp_mode == UdpModeSocks) {
- SocksUdpClient_SubmitPacket(&socks_udp_client, local_addr, remote_addr, data, data_len);
- }
-
- return 1;
-
- fail:
- return 0;
- }
- err_t netif_init_func (struct netif *netif)
- {
- BLog(BLOG_DEBUG, "netif func init");
-
- netif->name[0] = 'h';
- netif->name[1] = 'o';
- netif->output = netif_output_func;
- netif->output_ip6 = netif_output_ip6_func;
-
- return ERR_OK;
- }
- err_t netif_output_func (struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
- {
- return common_netif_output(netif, p);
- }
- err_t netif_output_ip6_func (struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
- {
- return common_netif_output(netif, p);
- }
- err_t common_netif_output (struct netif *netif, struct pbuf *p)
- {
- SYNC_DECL
-
- BLog(BLOG_DEBUG, "device write: send packet");
-
- if (quitting) {
- return ERR_OK;
- }
-
- // if there is just one chunk, send it directly, else via buffer
- if (!p->next) {
- if (p->len > BTap_GetMTU(&device)) {
- BLog(BLOG_WARNING, "netif func output: no space left");
- goto out;
- }
-
- SYNC_FROMHERE
- BTap_Send(&device, (uint8_t *)p->payload, p->len);
- SYNC_COMMIT
- } else {
- int len = 0;
- do {
- if (p->len > BTap_GetMTU(&device) - len) {
- BLog(BLOG_WARNING, "netif func output: no space left");
- goto out;
- }
- memcpy(device_write_buf + len, p->payload, p->len);
- len += p->len;
- } while (p = p->next);
-
- SYNC_FROMHERE
- BTap_Send(&device, device_write_buf, len);
- SYNC_COMMIT
- }
-
- out:
- return ERR_OK;
- }
- err_t netif_input_func (struct pbuf *p, struct netif *inp)
- {
- uint8_t ip_version = 0;
- if (p->len > 0) {
- ip_version = (((uint8_t *)p->payload)[0] >> 4);
- }
-
- switch (ip_version) {
- case 4: {
- return ip_input(p, inp);
- } break;
- case 6: {
- if (options.netif_ip6addr) {
- return ip6_input(p, inp);
- }
- } break;
- }
-
- pbuf_free(p);
- return ERR_OK;
- }
- void client_logfunc (struct tcp_client *client)
- {
- char local_addr_s[BADDR_MAX_PRINT_LEN];
- BAddr_Print(&client->local_addr, local_addr_s);
- char remote_addr_s[BADDR_MAX_PRINT_LEN];
- BAddr_Print(&client->remote_addr, remote_addr_s);
-
- BLog_Append("%05d (%s %s): ", num_clients, local_addr_s, remote_addr_s);
- }
- void client_log (struct tcp_client *client, int level, const char *fmt, ...)
- {
- va_list vl;
- va_start(vl, fmt);
- BLog_LogViaFuncVarArg((BLog_logfunc)client_logfunc, client, BLOG_CURRENT_CHANNEL, level, fmt, vl);
- va_end(vl);
- }
- err_t listener_accept_func (void *arg, struct tcp_pcb *newpcb, err_t err)
- {
- ASSERT(err == ERR_OK)
-
- // allocate client structure
- struct tcp_client *client = (struct tcp_client *)malloc(sizeof(*client));
- if (!client) {
- BLog(BLOG_ERROR, "listener accept: malloc failed");
- goto fail0;
- }
- client->socks_username = NULL;
-
- SYNC_DECL
- SYNC_FROMHERE
-
- // read addresses
- client->local_addr = baddr_from_lwip(&newpcb->local_ip, newpcb->local_port);
- client->remote_addr = baddr_from_lwip(&newpcb->remote_ip, newpcb->remote_port);
-
- // get destination address
- BAddr addr = client->local_addr;
- #ifdef OVERRIDE_DEST_ADDR
- ASSERT_FORCE(BAddr_Parse2(&addr, OVERRIDE_DEST_ADDR, NULL, 0, 1))
- #endif
-
- // add source address to username if requested
- if (options.username && options.append_source_to_username) {
- char addr_str[BADDR_MAX_PRINT_LEN];
- BAddr_Print(&client->remote_addr, addr_str);
- client->socks_username = concat_strings(3, options.username, "@", addr_str);
- if (!client->socks_username) {
- goto fail1;
- }
- socks_auth_info[1].password.username = client->socks_username;
- socks_auth_info[1].password.username_len = strlen(client->socks_username);
- }
-
- // init SOCKS
- if (!BSocksClient_Init(&client->socks_client,
- socks_server_addr, socks_auth_info, socks_num_auth_info, addr, /*udp=*/false,
- (BSocksClient_handler)client_socks_handler, client, &ss))
- {
- BLog(BLOG_ERROR, "listener accept: BSocksClient_Init failed");
- goto fail1;
- }
-
- // init aborted and dead_aborted
- client->aborted = 0;
- DEAD_INIT(client->dead_aborted);
-
- // add to linked list
- LinkedList1_Append(&tcp_clients, &client->list_node);
-
- // increment counter
- ASSERT(num_clients >= 0)
- num_clients++;
-
- // set pcb
- client->pcb = newpcb;
-
- // set client not closed
- client->client_closed = 0;
-
- // setup handler argument
- tcp_arg(client->pcb, client);
-
- // setup handlers
- tcp_err(client->pcb, client_err_func);
- tcp_recv(client->pcb, client_recv_func);
-
- // setup buffer
- client->buf_used = 0;
-
- // set SOCKS not up, not closed
- client->socks_up = 0;
- client->socks_closed = 0;
-
- client_log(client, BLOG_INFO, "accepted");
-
- DEAD_ENTER(client->dead_aborted)
- SYNC_COMMIT
- DEAD_LEAVE2(client->dead_aborted)
-
- // Return ERR_ABRT if and only if tcp_abort was called from this callback.
- return (DEAD_KILLED > 0) ? ERR_ABRT : ERR_OK;
-
- fail1:
- SYNC_BREAK
- free(client->socks_username);
- free(client);
- fail0:
- return ERR_MEM;
- }
- void client_handle_freed_client (struct tcp_client *client)
- {
- ASSERT(!client->client_closed)
-
- // pcb was taken care of by the caller
-
- // set client closed
- client->client_closed = 1;
-
- // if we have data to be sent to SOCKS and can send it, keep sending
- if (client->buf_used > 0 && !client->socks_closed) {
- client_log(client, BLOG_INFO, "waiting untill buffered data is sent to SOCKS");
- } else {
- if (!client->socks_closed) {
- client_free_socks(client);
- } else {
- client_dealloc(client);
- }
- }
- }
- void client_free_client (struct tcp_client *client)
- {
- ASSERT(!client->client_closed)
-
- // remove callbacks
- tcp_err(client->pcb, NULL);
- tcp_recv(client->pcb, NULL);
- tcp_sent(client->pcb, NULL);
-
- // free pcb
- err_t err = tcp_close(client->pcb);
- if (err != ERR_OK) {
- client_log(client, BLOG_ERROR, "tcp_close failed (%d)", err);
- client_abort_pcb(client);
- }
-
- client_handle_freed_client(client);
- }
- void client_abort_client (struct tcp_client *client)
- {
- ASSERT(!client->client_closed)
-
- // remove callbacks
- tcp_err(client->pcb, NULL);
- tcp_recv(client->pcb, NULL);
- tcp_sent(client->pcb, NULL);
-
- // abort
- client_abort_pcb(client);
-
- client_handle_freed_client(client);
- }
- void client_abort_pcb (struct tcp_client *client)
- {
- ASSERT(!client->aborted)
-
- // abort the PCB
- tcp_abort(client->pcb);
-
- // mark aborted
- client->aborted = 1;
-
- // kill dead_aborted with value 1 signaling that tcp_abort was done;
- // this is contrasted to killing with value -1 from client_dealloc
- // signaling that the client was freed without tcp_abort
- DEAD_KILL_WITH(client->dead_aborted, 1);
- }
- void client_free_socks (struct tcp_client *client)
- {
- ASSERT(!client->socks_closed)
-
- // stop sending to SOCKS
- if (client->socks_up) {
- // stop receiving from client
- if (!client->client_closed) {
- tcp_recv(client->pcb, NULL);
- }
- }
-
- // free SOCKS
- BSocksClient_Free(&client->socks_client);
-
- // set SOCKS closed
- client->socks_closed = 1;
-
- // if we have data to be sent to the client and we can send it, keep sending
- if (client->socks_up && (client->socks_recv_buf_used >= 0 || client->socks_recv_tcp_pending > 0) && !client->client_closed) {
- client_log(client, BLOG_INFO, "waiting until buffered data is sent to client");
- } else {
- if (!client->client_closed) {
- client_free_client(client);
- } else {
- client_dealloc(client);
- }
- }
- }
- void client_murder (struct tcp_client *client)
- {
- // free client
- if (!client->client_closed) {
- // remove callbacks
- tcp_err(client->pcb, NULL);
- tcp_recv(client->pcb, NULL);
- tcp_sent(client->pcb, NULL);
-
- // abort
- client_abort_pcb(client);
-
- // set client closed
- client->client_closed = 1;
- }
-
- // free SOCKS
- if (!client->socks_closed) {
- // free SOCKS
- BSocksClient_Free(&client->socks_client);
-
- // set SOCKS closed
- client->socks_closed = 1;
- }
-
- // dealloc entry
- client_dealloc(client);
- }
- void client_dealloc (struct tcp_client *client)
- {
- ASSERT(client->client_closed)
- ASSERT(client->socks_closed)
-
- // decrement counter
- ASSERT(num_clients > 0)
- num_clients--;
-
- // remove client entry
- LinkedList1_Remove(&tcp_clients, &client->list_node);
-
- // kill dead_aborted with value -1 unless already aborted
- if (!client->aborted) {
- DEAD_KILL_WITH(client->dead_aborted, -1);
- }
-
- // free memory
- free(client->socks_username);
- free(client);
- }
- void client_err_func (void *arg, err_t err)
- {
- struct tcp_client *client = (struct tcp_client *)arg;
- ASSERT(!client->client_closed)
-
- client_log(client, BLOG_INFO, "client error (%d)", (int)err);
-
- // the pcb was taken care of by the caller
- client_handle_freed_client(client);
- }
- err_t client_recv_func (void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
- {
- struct tcp_client *client = (struct tcp_client *)arg;
- ASSERT(!client->client_closed)
- ASSERT(err == ERR_OK) // checked in lwIP source. Otherwise, I've no idea what should
- // be done with the pbuf in case of an error.
-
- DEAD_ENTER(client->dead_aborted)
-
- if (!p) {
- client_log(client, BLOG_INFO, "client closed");
- client_free_client(client);
- } else {
- ASSERT(p->tot_len > 0)
-
- // check if we have enough buffer
- if (p->tot_len > sizeof(client->buf) - client->buf_used) {
- client_log(client, BLOG_ERROR, "no buffer for data !?!");
- DEAD_LEAVE2(client->dead_aborted)
- return ERR_MEM;
- }
-
- // copy data to buffer
- ASSERT_EXECUTE(pbuf_copy_partial(p, client->buf + client->buf_used, p->tot_len, 0) == p->tot_len)
- client->buf_used += p->tot_len;
-
- // free pbuff
- int p_tot_len = p->tot_len;
- pbuf_free(p);
-
- // if there was nothing in the buffer before, and SOCKS is up, start send data
- if (client->buf_used == p_tot_len && client->socks_up) {
- ASSERT(!client->socks_closed) // this callback is removed when SOCKS is closed
-
- SYNC_DECL
- SYNC_FROMHERE
- client_send_to_socks(client);
- SYNC_COMMIT
- }
- }
-
- DEAD_LEAVE2(client->dead_aborted)
-
- // Return ERR_ABRT if and only if tcp_abort was called from this callback.
- return (DEAD_KILLED > 0) ? ERR_ABRT : ERR_OK;
- }
- void client_socks_handler (struct tcp_client *client, int event)
- {
- ASSERT(!client->socks_closed)
-
- switch (event) {
- case BSOCKSCLIENT_EVENT_ERROR: {
- client_log(client, BLOG_INFO, "SOCKS error");
-
- client_free_socks(client);
- } break;
-
- case BSOCKSCLIENT_EVENT_UP: {
- ASSERT(!client->socks_up)
-
- client_log(client, BLOG_INFO, "SOCKS up");
-
- // init sending
- client->socks_send_if = BSocksClient_GetSendInterface(&client->socks_client);
- StreamPassInterface_Sender_Init(client->socks_send_if, (StreamPassInterface_handler_done)client_socks_send_handler_done, client);
-
- // init receiving
- client->socks_recv_if = BSocksClient_GetRecvInterface(&client->socks_client);
- StreamRecvInterface_Receiver_Init(client->socks_recv_if, (StreamRecvInterface_handler_done)client_socks_recv_handler_done, client);
- client->socks_recv_buf_used = -1;
- client->socks_recv_tcp_pending = 0;
- if (!client->client_closed) {
- tcp_sent(client->pcb, client_sent_func);
- }
-
- // set up
- client->socks_up = 1;
-
- // start sending data if there is any
- if (client->buf_used > 0) {
- client_send_to_socks(client);
- }
-
- // start receiving data if client is still up
- if (!client->client_closed) {
- client_socks_recv_initiate(client);
- }
- } break;
-
- case BSOCKSCLIENT_EVENT_ERROR_CLOSED: {
- ASSERT(client->socks_up)
-
- client_log(client, BLOG_INFO, "SOCKS closed");
-
- client_free_socks(client);
- } break;
-
- default:
- ASSERT(0);
- }
- }
- void client_send_to_socks (struct tcp_client *client)
- {
- ASSERT(!client->socks_closed)
- ASSERT(client->socks_up)
- ASSERT(client->buf_used > 0)
-
- // schedule sending
- StreamPassInterface_Sender_Send(client->socks_send_if, client->buf, client->buf_used);
- }
- void client_socks_send_handler_done (struct tcp_client *client, int data_len)
- {
- ASSERT(!client->socks_closed)
- ASSERT(client->socks_up)
- ASSERT(client->buf_used > 0)
- ASSERT(data_len > 0)
- ASSERT(data_len <= client->buf_used)
-
- // remove sent data from buffer
- memmove(client->buf, client->buf + data_len, client->buf_used - data_len);
- client->buf_used -= data_len;
-
- if (!client->client_closed) {
- // confirm sent data
- tcp_recved(client->pcb, data_len);
- }
-
- if (client->buf_used > 0) {
- // send any further data
- StreamPassInterface_Sender_Send(client->socks_send_if, client->buf, client->buf_used);
- }
- else if (client->client_closed) {
- // client was closed we've sent everything we had buffered; we're done with it
- client_log(client, BLOG_INFO, "removing after client went down");
-
- client_free_socks(client);
- }
- }
- void client_socks_recv_initiate (struct tcp_client *client)
- {
- ASSERT(!client->client_closed)
- ASSERT(!client->socks_closed)
- ASSERT(client->socks_up)
- ASSERT(client->socks_recv_buf_used == -1)
-
- StreamRecvInterface_Receiver_Recv(client->socks_recv_if, client->socks_recv_buf, sizeof(client->socks_recv_buf));
- }
- void client_socks_recv_handler_done (struct tcp_client *client, int data_len)
- {
- ASSERT(data_len > 0)
- ASSERT(data_len <= sizeof(client->socks_recv_buf))
- ASSERT(!client->socks_closed)
- ASSERT(client->socks_up)
- ASSERT(client->socks_recv_buf_used == -1)
-
- // if client was closed, stop receiving
- if (client->client_closed) {
- return;
- }
-
- // set amount of data in buffer
- client->socks_recv_buf_used = data_len;
- client->socks_recv_buf_sent = 0;
- client->socks_recv_waiting = 0;
-
- // send to client
- if (client_socks_recv_send_out(client) < 0) {
- return;
- }
-
- // continue receiving if needed
- if (client->socks_recv_buf_used == -1) {
- client_socks_recv_initiate(client);
- }
- }
- int client_socks_recv_send_out (struct tcp_client *client)
- {
- ASSERT(!client->client_closed)
- ASSERT(client->socks_up)
- ASSERT(client->socks_recv_buf_used > 0)
- ASSERT(client->socks_recv_buf_sent < client->socks_recv_buf_used)
- ASSERT(!client->socks_recv_waiting)
-
- // return value -1 means tcp_abort() was done,
- // 0 means it wasn't and the client (pcb) is still up
-
- do {
- int to_write = bmin_int(client->socks_recv_buf_used - client->socks_recv_buf_sent, tcp_sndbuf(client->pcb));
- if (to_write == 0) {
- break;
- }
-
- err_t err = tcp_write(client->pcb, client->socks_recv_buf + client->socks_recv_buf_sent, to_write, TCP_WRITE_FLAG_COPY);
- if (err != ERR_OK) {
- if (err == ERR_MEM) {
- break;
- }
-
- client_log(client, BLOG_INFO, "tcp_write failed (%d)", (int)err);
-
- client_abort_client(client);
- return -1;
- }
-
- client->socks_recv_buf_sent += to_write;
- client->socks_recv_tcp_pending += to_write;
- } while (client->socks_recv_buf_sent < client->socks_recv_buf_used);
-
- // start sending now
- err_t err = tcp_output(client->pcb);
- if (err != ERR_OK) {
- client_log(client, BLOG_INFO, "tcp_output failed (%d)", (int)err);
-
- client_abort_client(client);
- return -1;
- }
-
- // more data to queue?
- if (client->socks_recv_buf_sent < client->socks_recv_buf_used) {
- if (client->socks_recv_tcp_pending == 0) {
- client_log(client, BLOG_ERROR, "can't queue data, but all data was confirmed !?!");
-
- client_abort_client(client);
- return -1;
- }
-
- // set waiting, continue in client_sent_func
- client->socks_recv_waiting = 1;
- return 0;
- }
-
- // everything was queued
- client->socks_recv_buf_used = -1;
-
- return 0;
- }
- err_t client_sent_func (void *arg, struct tcp_pcb *tpcb, u16_t len)
- {
- struct tcp_client *client = (struct tcp_client *)arg;
-
- ASSERT(!client->client_closed)
- ASSERT(client->socks_up)
- ASSERT(len > 0)
- ASSERT(len <= client->socks_recv_tcp_pending)
-
- DEAD_ENTER(client->dead_aborted)
-
- // decrement pending
- client->socks_recv_tcp_pending -= len;
-
- // continue queuing
- if (client->socks_recv_buf_used > 0) {
- ASSERT(client->socks_recv_waiting)
- ASSERT(client->socks_recv_buf_sent < client->socks_recv_buf_used)
-
- // set not waiting
- client->socks_recv_waiting = 0;
-
- // possibly send more data
- if (client_socks_recv_send_out(client) < 0) {
- goto out;
- }
-
- // we just queued some data, so it can't have been confirmed yet
- ASSERT(client->socks_recv_tcp_pending > 0)
-
- // continue receiving if needed
- if (client->socks_recv_buf_used == -1 && !client->socks_closed) {
- SYNC_DECL
- SYNC_FROMHERE
- client_socks_recv_initiate(client);
- SYNC_COMMIT
- }
- } else {
- // have we sent everything after SOCKS was closed?
- if (client->socks_closed && client->socks_recv_tcp_pending == 0) {
- client_log(client, BLOG_INFO, "removing after SOCKS went down");
- client_free_client(client);
- }
- }
-
- out:
- DEAD_LEAVE2(client->dead_aborted)
-
- // Return ERR_ABRT if and only if tcp_abort was called from this callback.
- return (DEAD_KILLED > 0) ? ERR_ABRT : ERR_OK;
- }
- void udp_send_packet_to_device (void *unused, BAddr local_addr, BAddr remote_addr, const uint8_t *data, int data_len)
- {
- ASSERT(udp_mode != UdpModeNone)
- ASSERT(local_addr.type == BADDR_TYPE_IPV4 || local_addr.type == BADDR_TYPE_IPV6)
- ASSERT(local_addr.type == remote_addr.type)
- ASSERT(data_len >= 0)
- char const *source_name = (udp_mode == UdpModeUdpgw) ? "udpgw" : "SOCKS UDP";
-
- int packet_length = 0;
-
- switch (local_addr.type) {
- case BADDR_TYPE_IPV4: {
- BLog(BLOG_INFO, "UDP: from %s %d bytes", source_name, data_len);
-
- if (data_len > UINT16_MAX - (sizeof(struct ipv4_header) + sizeof(struct udp_header)) ||
- data_len > BTap_GetMTU(&device) - (int)(sizeof(struct ipv4_header) + sizeof(struct udp_header))
- ) {
- BLog(BLOG_ERROR, "UDP: packet is too large");
- return;
- }
-
- // build IP header
- struct ipv4_header iph;
- iph.version4_ihl4 = IPV4_MAKE_VERSION_IHL(sizeof(iph));
- iph.ds = hton8(0);
- iph.total_length = hton16(sizeof(iph) + sizeof(struct udp_header) + data_len);
- iph.identification = hton16(0);
- iph.flags3_fragmentoffset13 = hton16(0);
- iph.ttl = hton8(64);
- iph.protocol = hton8(IPV4_PROTOCOL_UDP);
- iph.checksum = hton16(0);
- iph.source_address = remote_addr.ipv4.ip;
- iph.destination_address = local_addr.ipv4.ip;
- iph.checksum = ipv4_checksum(&iph, NULL, 0);
-
- // build UDP header
- struct udp_header udph;
- udph.source_port = remote_addr.ipv4.port;
- udph.dest_port = local_addr.ipv4.port;
- udph.length = hton16(sizeof(udph) + data_len);
- udph.checksum = hton16(0);
- udph.checksum = udp_checksum(&udph, data, data_len, iph.source_address, iph.destination_address);
-
- // write packet
- memcpy(device_write_buf, &iph, sizeof(iph));
- memcpy(device_write_buf + sizeof(iph), &udph, sizeof(udph));
- memcpy(device_write_buf + sizeof(iph) + sizeof(udph), data, data_len);
- packet_length = sizeof(iph) + sizeof(udph) + data_len;
- } break;
-
- case BADDR_TYPE_IPV6: {
- BLog(BLOG_INFO, "UDP/IPv6: from %s %d bytes", source_name, data_len);
-
- if (!options.netif_ip6addr) {
- BLog(BLOG_ERROR, "got IPv6 packet from %s but IPv6 is disabled", source_name);
- return;
- }
-
- if (data_len > UINT16_MAX - sizeof(struct udp_header) ||
- data_len > BTap_GetMTU(&device) - (int)(sizeof(struct ipv6_header) + sizeof(struct udp_header))
- ) {
- BLog(BLOG_ERROR, "UDP/IPv6: packet is too large");
- return;
- }
-
- // build IPv6 header
- struct ipv6_header iph;
- iph.version4_tc4 = hton8((6 << 4));
- iph.tc4_fl4 = hton8(0);
- iph.fl = hton16(0);
- iph.payload_length = hton16(sizeof(struct udp_header) + data_len);
- iph.next_header = hton8(IPV6_NEXT_UDP);
- iph.hop_limit = hton8(64);
- memcpy(iph.source_address, remote_addr.ipv6.ip, 16);
- memcpy(iph.destination_address, local_addr.ipv6.ip, 16);
-
- // build UDP header
- struct udp_header udph;
- udph.source_port = remote_addr.ipv6.port;
- udph.dest_port = local_addr.ipv6.port;
- udph.length = hton16(sizeof(udph) + data_len);
- udph.checksum = hton16(0);
- udph.checksum = udp_ip6_checksum(&udph, data, data_len, iph.source_address, iph.destination_address);
-
- // write packet
- memcpy(device_write_buf, &iph, sizeof(iph));
- memcpy(device_write_buf + sizeof(iph), &udph, sizeof(udph));
- memcpy(device_write_buf + sizeof(iph) + sizeof(udph), data, data_len);
- packet_length = sizeof(iph) + sizeof(udph) + data_len;
- } break;
- }
-
- // submit packet
- BTap_Send(&device, device_write_buf, packet_length);
- }
|