interface_physical.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @file interface_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. #include <stdlib.h>
  23. #include <string.h>
  24. #include <misc/dead.h>
  25. #include <system/DebugObject.h>
  26. #include <ncd/NCDInterfaceModule.h>
  27. #include <ncd/NCDIfConfig.h>
  28. #include <ncd/NCDInterfaceMonitor.h>
  29. #define STATE_WAITDEVICE 1
  30. #define STATE_WAITLINK 2
  31. #define STATE_FINISHED 3
  32. struct instance {
  33. struct NCDInterfaceModuleNCD params;
  34. NCDInterfaceMonitor monitor;
  35. int state;
  36. DebugObject d_obj;
  37. #ifndef NDEBUG
  38. dead_t d_dead;
  39. #endif
  40. };
  41. static void report_error (struct instance *o)
  42. {
  43. #ifndef NDEBUG
  44. DEAD_ENTER(o->d_dead)
  45. #endif
  46. NCDInterfaceModuleNCD_Event(&o->params, NCDINTERFACEMODULE_EVENT_ERROR);
  47. #ifndef NDEBUG
  48. ASSERT(DEAD_KILLED)
  49. DEAD_LEAVE(o->d_dead);
  50. #endif
  51. }
  52. static int try_start (struct instance *o)
  53. {
  54. // query interface state
  55. int flags = NCDIfConfig_query(o->params.conf->name);
  56. if (!(flags&NCDIFCONFIG_FLAG_EXISTS)) {
  57. NCDInterfaceModuleNCD_Log(&o->params, BLOG_INFO, "device doesn't exist");
  58. // waiting for device
  59. o->state = STATE_WAITDEVICE;
  60. } else {
  61. if ((flags&NCDIFCONFIG_FLAG_UP)) {
  62. NCDInterfaceModuleNCD_Log(&o->params, BLOG_ERROR, "device already up - NOT configuring");
  63. return 0;
  64. }
  65. // set interface up
  66. if (!NCDIfConfig_set_up(o->params.conf->name)) {
  67. NCDInterfaceModuleNCD_Log(&o->params, BLOG_ERROR, "failed to set device up");
  68. return 0;
  69. }
  70. NCDInterfaceModuleNCD_Log(&o->params, BLOG_INFO, "waiting for link");
  71. // waiting for link
  72. o->state = STATE_WAITLINK;
  73. }
  74. return 1;
  75. }
  76. static void monitor_handler (struct instance *o, const char *ifname, int if_flags)
  77. {
  78. DebugObject_Access(&o->d_obj);
  79. if (strcmp(ifname, o->params.conf->name)) {
  80. return;
  81. }
  82. if (!(if_flags&NCDIFCONFIG_FLAG_EXISTS)) {
  83. if (o->state > STATE_WAITDEVICE) {
  84. int prev_state = o->state;
  85. NCDInterfaceModuleNCD_Log(&o->params, BLOG_INFO, "device down");
  86. // set state
  87. o->state = STATE_WAITDEVICE;
  88. // report
  89. if (prev_state == STATE_FINISHED) {
  90. NCDInterfaceModuleNCD_Event(&o->params, NCDINTERFACEMODULE_EVENT_DOWN);
  91. return;
  92. }
  93. }
  94. } else {
  95. if (o->state == STATE_WAITDEVICE) {
  96. NCDInterfaceModuleNCD_Log(&o->params, BLOG_INFO, "device up");
  97. if (!try_start(o)) {
  98. report_error(o);
  99. return;
  100. }
  101. return;
  102. }
  103. if ((if_flags&NCDIFCONFIG_FLAG_RUNNING)) {
  104. if (o->state == STATE_WAITLINK) {
  105. NCDInterfaceModuleNCD_Log(&o->params, BLOG_INFO, "link up");
  106. // set state
  107. o->state = STATE_FINISHED;
  108. // report
  109. NCDInterfaceModuleNCD_Event(&o->params, NCDINTERFACEMODULE_EVENT_UP);
  110. return;
  111. }
  112. } else {
  113. if (o->state == STATE_FINISHED) {
  114. NCDInterfaceModuleNCD_Log(&o->params, BLOG_INFO, "link down");
  115. // set state
  116. o->state = STATE_WAITLINK;
  117. // report
  118. NCDInterfaceModuleNCD_Event(&o->params, NCDINTERFACEMODULE_EVENT_DOWN);
  119. return;
  120. }
  121. }
  122. }
  123. }
  124. static void * func_new (struct NCDInterfaceModuleNCD params, int *initial_up_state)
  125. {
  126. // allocate instance
  127. struct instance *o = malloc(sizeof(*o));
  128. if (!o) {
  129. NCDInterfaceModuleNCD_Log(&params, BLOG_ERROR, "failed to allocate instance");
  130. goto fail0;
  131. }
  132. // init arguments
  133. o->params = params;
  134. // init monitor
  135. if (!NCDInterfaceMonitor_Init(&o->monitor, o->params.reactor, (NCDInterfaceMonitor_handler)monitor_handler, o)) {
  136. NCDInterfaceModuleNCD_Log(&o->params, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  137. goto fail1;
  138. }
  139. if (!try_start(o)) {
  140. goto fail2;
  141. }
  142. DebugObject_Init(&o->d_obj);
  143. #ifndef NDEBUG
  144. DEAD_INIT(o->d_dead);
  145. #endif
  146. *initial_up_state = 0;
  147. return o;
  148. fail2:
  149. NCDInterfaceMonitor_Free(&o->monitor);
  150. fail1:
  151. free(o);
  152. fail0:
  153. return NULL;
  154. }
  155. static void func_free (void *vo)
  156. {
  157. struct instance *o = vo;
  158. DebugObject_Free(&o->d_obj);
  159. #ifndef NDEBUG
  160. DEAD_KILL(o->d_dead);
  161. #endif
  162. // set interface down
  163. if (o->state > STATE_WAITDEVICE) {
  164. NCDIfConfig_set_down(o->params.conf->name);
  165. }
  166. // free monitor
  167. NCDInterfaceMonitor_Free(&o->monitor);
  168. // free instance
  169. free(o);
  170. }
  171. const struct NCDInterfaceModule ncd_interface_physical = {
  172. .type = "physical",
  173. .func_new = func_new,
  174. .func_free = func_free
  175. };