interface_physical.c 5.8 KB

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