net_backend_physical.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @file net_backend_physical.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Physical network interface module.
  25. *
  26. * Synopsis: net.backend.physical(string ifname)
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ncd/NCDModule.h>
  31. #include <ncd/NCDIfConfig.h>
  32. #include <ncd/NCDInterfaceMonitor.h>
  33. #include <generated/blog_channel_ncd_net_backend_physical.h>
  34. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  35. #define STATE_WAITDEVICE 1
  36. #define STATE_WAITLINK 2
  37. #define STATE_FINISHED 3
  38. struct instance {
  39. NCDModuleInst *i;
  40. const char *ifname;
  41. NCDInterfaceMonitor monitor;
  42. int state;
  43. };
  44. static int try_start (struct instance *o)
  45. {
  46. // query interface state
  47. int flags = NCDIfConfig_query(o->ifname);
  48. if (!(flags&NCDIFCONFIG_FLAG_EXISTS)) {
  49. ModuleLog(o->i, BLOG_INFO, "device doesn't exist");
  50. // waiting for device
  51. o->state = STATE_WAITDEVICE;
  52. } else {
  53. if ((flags&NCDIFCONFIG_FLAG_UP)) {
  54. ModuleLog(o->i, BLOG_ERROR, "device already up - NOT configuring");
  55. return 0;
  56. }
  57. // set interface up
  58. if (!NCDIfConfig_set_up(o->ifname)) {
  59. ModuleLog(o->i, BLOG_ERROR, "failed to set device up");
  60. return 0;
  61. }
  62. ModuleLog(o->i, BLOG_INFO, "waiting for link");
  63. // waiting for link
  64. o->state = STATE_WAITLINK;
  65. }
  66. return 1;
  67. }
  68. static void monitor_handler (struct instance *o, const char *ifname, int if_flags)
  69. {
  70. if (strcmp(ifname, o->ifname)) {
  71. return;
  72. }
  73. if (!(if_flags&NCDIFCONFIG_FLAG_EXISTS)) {
  74. if (o->state > STATE_WAITDEVICE) {
  75. int prev_state = o->state;
  76. ModuleLog(o->i, BLOG_INFO, "device down");
  77. // set state
  78. o->state = STATE_WAITDEVICE;
  79. // report
  80. if (prev_state == STATE_FINISHED) {
  81. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  82. }
  83. }
  84. } else {
  85. if (o->state == STATE_WAITDEVICE) {
  86. ModuleLog(o->i, BLOG_INFO, "device up");
  87. if (!try_start(o)) {
  88. NCDModuleInst_Backend_Died(o->i, 1);
  89. return;
  90. }
  91. return;
  92. }
  93. if ((if_flags&NCDIFCONFIG_FLAG_RUNNING)) {
  94. if (o->state == STATE_WAITLINK) {
  95. ModuleLog(o->i, BLOG_INFO, "link up");
  96. // set state
  97. o->state = STATE_FINISHED;
  98. // report
  99. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  100. }
  101. } else {
  102. if (o->state == STATE_FINISHED) {
  103. ModuleLog(o->i, BLOG_INFO, "link down");
  104. // set state
  105. o->state = STATE_WAITLINK;
  106. // report
  107. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  108. }
  109. }
  110. }
  111. }
  112. static void * func_new (NCDModuleInst *i)
  113. {
  114. // allocate instance
  115. struct instance *o = malloc(sizeof(*o));
  116. if (!o) {
  117. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  118. goto fail0;
  119. }
  120. // init arguments
  121. o->i = i;
  122. // check arguments
  123. NCDValue *arg;
  124. if (!NCDValue_ListRead(i->args, 1, &arg)) {
  125. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  126. goto fail1;
  127. }
  128. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  129. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  130. goto fail1;
  131. }
  132. o->ifname = NCDValue_StringValue(arg);
  133. // init monitor
  134. if (!NCDInterfaceMonitor_Init(&o->monitor, o->i->reactor, (NCDInterfaceMonitor_handler)monitor_handler, o)) {
  135. ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  136. goto fail1;
  137. }
  138. if (!try_start(o)) {
  139. goto fail2;
  140. }
  141. return o;
  142. fail2:
  143. NCDInterfaceMonitor_Free(&o->monitor);
  144. fail1:
  145. free(o);
  146. fail0:
  147. return NULL;
  148. }
  149. static void func_free (void *vo)
  150. {
  151. struct instance *o = vo;
  152. // set interface down
  153. if (o->state > STATE_WAITDEVICE) {
  154. NCDIfConfig_set_down(o->ifname);
  155. }
  156. // free monitor
  157. NCDInterfaceMonitor_Free(&o->monitor);
  158. // free instance
  159. free(o);
  160. }
  161. static const struct NCDModule modules[] = {
  162. {
  163. .type = "net.backend.physical",
  164. .func_new = func_new,
  165. .func_free = func_free
  166. }, {
  167. .type = NULL
  168. }
  169. };
  170. const struct NCDModuleGroup ncdmodule_net_backend_physical = {
  171. .modules = modules
  172. };