net_ipv4_arp_probe.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/module_common.h>
  50. #include <generated/blog_channel_ncd_net_ipv4_arp_probe.h>
  51. #define STATE_UNKNOWN 1
  52. #define STATE_EXIST 2
  53. #define STATE_NOEXIST 3
  54. struct instance {
  55. NCDModuleInst *i;
  56. BArpProbe arpprobe;
  57. int state;
  58. };
  59. static void instance_free (struct instance *o, int is_error);
  60. static void arpprobe_handler (struct instance *o, int event)
  61. {
  62. switch (event) {
  63. case BARPPROBE_EVENT_EXIST: {
  64. ASSERT(o->state == STATE_UNKNOWN || o->state == STATE_NOEXIST)
  65. ModuleLog(o->i, BLOG_INFO, "exist");
  66. if (o->state == STATE_NOEXIST) {
  67. // signal down
  68. NCDModuleInst_Backend_Down(o->i);
  69. }
  70. // signal up
  71. NCDModuleInst_Backend_Up(o->i);
  72. // set state exist
  73. o->state = STATE_EXIST;
  74. } break;
  75. case BARPPROBE_EVENT_NOEXIST: {
  76. ASSERT(o->state == STATE_UNKNOWN || o->state == STATE_EXIST)
  77. ModuleLog(o->i, BLOG_INFO, "noexist");
  78. if (o->state == STATE_EXIST) {
  79. // signal down
  80. NCDModuleInst_Backend_Down(o->i);
  81. }
  82. // signal up
  83. NCDModuleInst_Backend_Up(o->i);
  84. // set state noexist
  85. o->state = STATE_NOEXIST;
  86. } break;
  87. case BARPPROBE_EVENT_ERROR: {
  88. ModuleLog(o->i, BLOG_ERROR, "error");
  89. // die
  90. instance_free(o, 1);
  91. return;
  92. } break;
  93. default: ASSERT(0);
  94. }
  95. }
  96. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  97. {
  98. struct instance *o = vo;
  99. o->i = i;
  100. // read arguments
  101. NCDValRef arg_ifname;
  102. NCDValRef arg_addr;
  103. if (!NCDVal_ListRead(params->args, 2, &arg_ifname, &arg_addr)) {
  104. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  105. goto fail0;
  106. }
  107. if (!NCDVal_IsStringNoNulls(arg_ifname) || !NCDVal_IsString(arg_addr)) {
  108. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  109. goto fail0;
  110. }
  111. // parse address
  112. uint32_t addr;
  113. if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(arg_addr), &addr)) {
  114. ModuleLog(o->i, BLOG_ERROR, "wrong address");
  115. goto fail0;
  116. }
  117. // null terminate ifname
  118. NCDValNullTermString ifname_nts;
  119. if (!NCDVal_StringNullTerminate(arg_ifname, &ifname_nts)) {
  120. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  121. goto fail0;
  122. }
  123. // init arpprobe
  124. int res = BArpProbe_Init(&o->arpprobe, ifname_nts.data, addr, i->params->iparams->reactor, o, (BArpProbe_handler)arpprobe_handler);
  125. NCDValNullTermString_Free(&ifname_nts);
  126. if (!res) {
  127. ModuleLog(o->i, BLOG_ERROR, "BArpProbe_Init failed");
  128. goto fail0;
  129. }
  130. // set state unknown
  131. o->state = STATE_UNKNOWN;
  132. return;
  133. fail0:
  134. NCDModuleInst_Backend_DeadError(i);
  135. }
  136. static void instance_free (struct instance *o, int is_error)
  137. {
  138. // free arpprobe
  139. BArpProbe_Free(&o->arpprobe);
  140. if (is_error) {
  141. NCDModuleInst_Backend_DeadError(o->i);
  142. } else {
  143. NCDModuleInst_Backend_Dead(o->i);
  144. }
  145. }
  146. static void func_die (void *vo)
  147. {
  148. struct instance *o = vo;
  149. instance_free(o, 0);
  150. }
  151. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  152. {
  153. struct instance *o = vo;
  154. ASSERT(o->state == STATE_EXIST || o->state == STATE_NOEXIST)
  155. if (!strcmp(name, "exists")) {
  156. *out = ncd_make_boolean(mem, o->state == STATE_EXIST, o->i->params->iparams->string_index);
  157. return 1;
  158. }
  159. return 0;
  160. }
  161. static struct NCDModule modules[] = {
  162. {
  163. .type = "net.ipv4.arp_probe",
  164. .func_new2 = func_new,
  165. .func_die = func_die,
  166. .func_getvar = func_getvar,
  167. .alloc_size = sizeof(struct instance)
  168. }, {
  169. .type = NULL
  170. }
  171. };
  172. const struct NCDModuleGroup ncdmodule_net_ipv4_arp_probe = {
  173. .modules = modules
  174. };