net_ipv4_arp_probe.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. o->i = i;
  108. NCDModuleInst_Backend_SetUser(i, o);
  109. // read arguments
  110. NCDValRef arg_ifname;
  111. NCDValRef arg_addr;
  112. if (!NCDVal_ListRead(i->args, 2, &arg_ifname, &arg_addr)) {
  113. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  114. goto fail1;
  115. }
  116. if (!NCDVal_IsStringNoNulls(arg_ifname) || !NCDVal_IsStringNoNulls(arg_addr)) {
  117. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  118. goto fail1;
  119. }
  120. const char *ifname = NCDVal_StringValue(arg_ifname);
  121. const char *addr_str = NCDVal_StringValue(arg_addr);
  122. // parse address
  123. uint32_t addr;
  124. if (!ipaddr_parse_ipv4_addr((char *)addr_str, &addr)) {
  125. ModuleLog(o->i, BLOG_ERROR, "wrong address");
  126. goto fail1;
  127. }
  128. // init arpprobe
  129. if (!BArpProbe_Init(&o->arpprobe, ifname, addr, i->iparams->reactor, o, (BArpProbe_handler)arpprobe_handler)) {
  130. ModuleLog(o->i, BLOG_ERROR, "BArpProbe_Init failed");
  131. goto fail1;
  132. }
  133. // set state unknown
  134. o->state = STATE_UNKNOWN;
  135. return;
  136. fail1:
  137. free(o);
  138. fail0:
  139. NCDModuleInst_Backend_SetError(i);
  140. NCDModuleInst_Backend_Dead(i);
  141. }
  142. static void instance_free (struct instance *o)
  143. {
  144. NCDModuleInst *i = o->i;
  145. // free arpprobe
  146. BArpProbe_Free(&o->arpprobe);
  147. // free instance
  148. free(o);
  149. NCDModuleInst_Backend_Dead(i);
  150. }
  151. static void func_die (void *vo)
  152. {
  153. struct instance *o = vo;
  154. instance_free(o);
  155. }
  156. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  157. {
  158. struct instance *o = vo;
  159. ASSERT(o->state == STATE_EXIST || o->state == STATE_NOEXIST)
  160. if (!strcmp(name, "exists")) {
  161. const char *str = (o->state == STATE_EXIST ? "true" : "false");
  162. *out = NCDVal_NewString(mem, str);
  163. if (NCDVal_IsInvalid(*out)) {
  164. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  165. }
  166. return 1;
  167. }
  168. return 0;
  169. }
  170. static const struct NCDModule modules[] = {
  171. {
  172. .type = "net.ipv4.arp_probe",
  173. .func_new = func_new,
  174. .func_die = func_die,
  175. .func_getvar = func_getvar
  176. }, {
  177. .type = NULL
  178. }
  179. };
  180. const struct NCDModuleGroup ncdmodule_net_ipv4_arp_probe = {
  181. .modules = modules
  182. };