net_ipv4_arp_probe.c 6.1 KB

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