net_ipv4_arp_probe.c 6.3 KB

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