net_ipv4_arp_probe.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @file net_ipv4_arp_probe.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * ARP probing module.
  32. *
  33. * Synopsis:
  34. * net.ipv4.arp_probe(string ifname, string addr)
  35. *
  36. * Description:
  37. * Monitors local presence of an IPv4 host on a network interface.
  38. * On initialization, may take some time to determine whether
  39. * the host is present or not, then goes to UP state. When it
  40. * determines that presence has changed, toggles itself DOWN then
  41. * UP to expose the new determination.
  42. *
  43. * Variables:
  44. * exists - "true" if the host exists, "false" if not
  45. */
  46. #include <stdlib.h>
  47. #include <misc/ipaddr.h>
  48. #include <arpprobe/BArpProbe.h>
  49. #include <ncd/NCDModule.h>
  50. #include <generated/blog_channel_ncd_net_ipv4_arp_probe.h>
  51. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  52. #define STATE_UNKNOWN 1
  53. #define STATE_EXIST 2
  54. #define STATE_NOEXIST 3
  55. struct instance {
  56. NCDModuleInst *i;
  57. BArpProbe arpprobe;
  58. int state;
  59. };
  60. static void instance_free (struct instance *o);
  61. static void arpprobe_handler (struct instance *o, int event)
  62. {
  63. switch (event) {
  64. case BARPPROBE_EVENT_EXIST: {
  65. ASSERT(o->state == STATE_UNKNOWN || o->state == STATE_NOEXIST)
  66. ModuleLog(o->i, BLOG_INFO, "exist");
  67. if (o->state == STATE_NOEXIST) {
  68. // signal down
  69. NCDModuleInst_Backend_Down(o->i);
  70. }
  71. // signal up
  72. NCDModuleInst_Backend_Up(o->i);
  73. // set state exist
  74. o->state = STATE_EXIST;
  75. } break;
  76. case BARPPROBE_EVENT_NOEXIST: {
  77. ASSERT(o->state == STATE_UNKNOWN || o->state == STATE_EXIST)
  78. ModuleLog(o->i, BLOG_INFO, "noexist");
  79. if (o->state == STATE_EXIST) {
  80. // signal down
  81. NCDModuleInst_Backend_Down(o->i);
  82. }
  83. // signal up
  84. NCDModuleInst_Backend_Up(o->i);
  85. // set state noexist
  86. o->state = STATE_NOEXIST;
  87. } break;
  88. case BARPPROBE_EVENT_ERROR: {
  89. ModuleLog(o->i, BLOG_ERROR, "error");
  90. // set error
  91. NCDModuleInst_Backend_SetError(o->i);
  92. // die
  93. instance_free(o);
  94. return;
  95. } break;
  96. default: ASSERT(0);
  97. }
  98. }
  99. static void func_new (NCDModuleInst *i)
  100. {
  101. // allocate instance
  102. struct instance *o = malloc(sizeof(*o));
  103. if (!o) {
  104. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  105. goto fail0;
  106. }
  107. NCDModuleInst_Backend_SetUser(i, o);
  108. // init arguments
  109. o->i = i;
  110. // read arguments
  111. NCDValue *arg_ifname;
  112. NCDValue *arg_addr;
  113. if (!NCDValue_ListRead(i->args, 2, &arg_ifname, &arg_addr)) {
  114. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  115. goto fail1;
  116. }
  117. if (!NCDValue_IsStringNoNulls(arg_ifname) || !NCDValue_IsStringNoNulls(arg_addr)) {
  118. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  119. goto fail1;
  120. }
  121. char *ifname = NCDValue_StringValue(arg_ifname);
  122. char *addr_str = NCDValue_StringValue(arg_addr);
  123. // parse address
  124. uint32_t addr;
  125. if (!ipaddr_parse_ipv4_addr(addr_str, &addr)) {
  126. ModuleLog(o->i, BLOG_ERROR, "wrong address");
  127. goto fail1;
  128. }
  129. // init arpprobe
  130. if (!BArpProbe_Init(&o->arpprobe, ifname, addr, i->params->reactor, o, (BArpProbe_handler)arpprobe_handler)) {
  131. ModuleLog(o->i, BLOG_ERROR, "BArpProbe_Init failed");
  132. goto fail1;
  133. }
  134. // set state unknown
  135. o->state = STATE_UNKNOWN;
  136. return;
  137. fail1:
  138. free(o);
  139. fail0:
  140. NCDModuleInst_Backend_SetError(i);
  141. NCDModuleInst_Backend_Dead(i);
  142. }
  143. static void instance_free (struct instance *o)
  144. {
  145. NCDModuleInst *i = o->i;
  146. // free arpprobe
  147. BArpProbe_Free(&o->arpprobe);
  148. // free instance
  149. free(o);
  150. NCDModuleInst_Backend_Dead(i);
  151. }
  152. static void func_die (void *vo)
  153. {
  154. struct instance *o = vo;
  155. instance_free(o);
  156. }
  157. static int func_getvar (void *vo, const char *name, NCDValue *out)
  158. {
  159. struct instance *o = vo;
  160. ASSERT(o->state == STATE_EXIST || o->state == STATE_NOEXIST)
  161. if (!strcmp(name, "exists")) {
  162. const char *str = (o->state == STATE_EXIST ? "true" : "false");
  163. if (!NCDValue_InitString(out, str)) {
  164. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  165. return 0;
  166. }
  167. return 1;
  168. }
  169. return 0;
  170. }
  171. static const struct NCDModule modules[] = {
  172. {
  173. .type = "net.ipv4.arp_probe",
  174. .func_new = func_new,
  175. .func_die = func_die,
  176. .func_getvar = func_getvar
  177. }, {
  178. .type = NULL
  179. }
  180. };
  181. const struct NCDModuleGroup ncdmodule_net_ipv4_arp_probe = {
  182. .modules = modules
  183. };