| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- /**
- * @file net_backend_badvpn.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * 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.
- *
- * @section DESCRIPTION
- *
- * BadVPN interface module.
- *
- * Synopsis: net.backend.badvpn(string ifname, string user, string exec, list(string) args)
- */
- #include <stdlib.h>
- #include <string.h>
- #include <misc/cmdline.h>
- #include <ncd/NCDModule.h>
- #include <ncd/NCDIfConfig.h>
- #include <generated/blog_channel_ncd_net_backend_badvpn.h>
- #define RETRY_TIME 5000
- #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
- struct instance {
- NCDModuleInst *i;
- char *ifname;
- char *user;
- char *exec;
- NCDValue *args;
- int dying;
- int started;
- BTimer timer;
- BProcess process;
- };
- static void try_process (struct instance *o);
- static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status);
- static void timer_handler (struct instance *o);
- static void instance_free (struct instance *o);
- void try_process (struct instance *o)
- {
- CmdLine c;
- if (!CmdLine_Init(&c)) {
- goto fail0;
- }
-
- // append exec
- if (!CmdLine_Append(&c, o->exec)) {
- goto fail1;
- }
-
- // append tapdev
- if (!CmdLine_Append(&c, "--tapdev") || !CmdLine_Append(&c, o->ifname)) {
- goto fail1;
- }
-
- // append arguments
- NCDValue *arg = NCDValue_ListFirst(o->args);
- while (arg) {
- // append argument
- if (!CmdLine_Append(&c, NCDValue_StringValue(arg))) {
- goto fail1;
- }
- arg = NCDValue_ListNext(o->args, arg);
- }
-
- // terminate cmdline
- if (!CmdLine_Finish(&c)) {
- goto fail1;
- }
-
- // start process
- if (!BProcess_Init(&o->process, o->i->params->manager, (BProcess_handler)process_handler, o, ((char **)c.arr.v)[0], (char **)c.arr.v, o->user)) {
- ModuleLog(o->i, BLOG_ERROR, "BProcess_Init failed");
- goto fail1;
- }
-
- CmdLine_Free(&c);
-
- // set started
- o->started = 1;
-
- return;
-
- fail1:
- CmdLine_Free(&c);
- fail0:
- // retry
- o->started = 0;
- BReactor_SetTimer(o->i->params->reactor, &o->timer);
- }
- void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
- {
- ASSERT(o->started)
-
- ModuleLog(o->i, BLOG_INFO, "process terminated");
-
- // free process
- BProcess_Free(&o->process);
-
- // set not started
- o->started = 0;
-
- if (o->dying) {
- instance_free(o);
- return;
- }
-
- // set timer
- BReactor_SetTimer(o->i->params->reactor, &o->timer);
- }
- void timer_handler (struct instance *o)
- {
- ASSERT(!o->started)
-
- ModuleLog(o->i, BLOG_INFO, "retrying");
-
- // try starting process again
- try_process(o);
- }
- static void func_new (NCDModuleInst *i)
- {
- // allocate instance
- struct instance *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
- goto fail0;
- }
- NCDModuleInst_Backend_SetUser(i, o);
-
- // init arguments
- o->i = i;
-
- // read arguments
- NCDValue *ifname_arg;
- NCDValue *user_arg;
- NCDValue *exec_arg;
- NCDValue *args_arg;
- if (!NCDValue_ListRead(o->i->args, 4, &ifname_arg, &user_arg, &exec_arg, &args_arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong arity");
- goto fail1;
- }
- if (NCDValue_Type(ifname_arg) != NCDVALUE_STRING || NCDValue_Type(user_arg) != NCDVALUE_STRING ||
- NCDValue_Type(exec_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
- ModuleLog(o->i, BLOG_ERROR, "wrong type");
- goto fail1;
- }
- o->ifname = NCDValue_StringValue(ifname_arg);
- o->user = NCDValue_StringValue(user_arg);
- o->exec = NCDValue_StringValue(exec_arg);
- o->args = args_arg;
-
- // check arguments
- NCDValue *arg = NCDValue_ListFirst(o->args);
- while (arg) {
- if (NCDValue_Type(arg) != NCDVALUE_STRING) {
- ModuleLog(o->i, BLOG_ERROR, "wrong type");
- goto fail1;
- }
- arg = NCDValue_ListNext(o->args, arg);
- }
-
- // create TAP device
- if (!NCDIfConfig_make_tuntap(o->ifname, o->user, 0)) {
- ModuleLog(o->i, BLOG_ERROR, "failed to create TAP device");
- goto fail1;
- }
-
- // set device up
- if (!NCDIfConfig_set_up(o->ifname)) {
- ModuleLog(o->i, BLOG_ERROR, "failed to set device up");
- goto fail2;
- }
-
- // set not dying
- o->dying = 0;
-
- // init timer
- BTimer_Init(&o->timer, RETRY_TIME, (BTimer_handler)timer_handler, o);
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
-
- // try starting process
- try_process(o);
-
- return;
-
- fail2:
- if (!NCDIfConfig_remove_tuntap(o->ifname, 0)) {
- ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
- }
- fail1:
- free(o);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- void instance_free (struct instance *o)
- {
- ASSERT(!o->started)
- NCDModuleInst *i = o->i;
-
- // free timer
- BReactor_RemoveTimer(o->i->params->reactor, &o->timer);
-
- // set device down
- if (!NCDIfConfig_set_down(o->ifname)) {
- ModuleLog(o->i, BLOG_ERROR, "failed to set device down");
- }
-
- // free TAP device
- if (!NCDIfConfig_remove_tuntap(o->ifname, 0)) {
- ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
- }
-
- // free instance
- free(o);
-
- NCDModuleInst_Backend_Dead(i);
- }
- static void func_die (void *vo)
- {
- struct instance *o = vo;
- ASSERT(!o->dying)
-
- if (!o->started) {
- instance_free(o);
- return;
- }
-
- // request termination
- BProcess_Terminate(&o->process);
-
- // remember dying
- o->dying = 1;
- }
- static const struct NCDModule modules[] = {
- {
- .type = "net.backend.badvpn",
- .func_new = func_new,
- .func_die = func_die
- }, {
- .type = NULL
- }
- };
- const struct NCDModuleGroup ncdmodule_net_backend_badvpn = {
- .modules = modules
- };
|