net_ipv6_wait_dynamic_addr.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * @file net_ipv6_wait_dynamic_addr.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. * Synopsis:
  32. * net.ipv6.wait_dynamic_addr(string ifname)
  33. *
  34. * Description:
  35. * Waits for a dynamic IPv6 address to be obtained on the interface,
  36. * and goes up when it is obtained.
  37. * If the address is subsequently lost, goes back down and again waits
  38. * for an address.
  39. *
  40. * Variables:
  41. * string addr - dynamic address obtained on the interface
  42. * string prefix - prefix length
  43. * string cidr_addr - address and prefix (addr/prefix)
  44. */
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <stdio.h>
  48. #include <misc/get_iface_info.h>
  49. #include <misc/ipaddr6.h>
  50. #include <ncd/NCDModule.h>
  51. #include <ncd/extra/NCDInterfaceMonitor.h>
  52. #include <generated/blog_channel_ncd_net_ipv6_wait_dynamic_addr.h>
  53. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  54. struct instance {
  55. NCDModuleInst *i;
  56. NCDInterfaceMonitor monitor;
  57. struct ipv6_ifaddr ifaddr;
  58. int up;
  59. };
  60. static void instance_free (struct instance *o, int is_error);
  61. static void monitor_handler (struct instance *o, struct NCDInterfaceMonitor_event event)
  62. {
  63. if (!o->up && event.event == NCDIFMONITOR_EVENT_IPV6_ADDR_ADDED && (event.u.ipv6_addr.addr_flags & NCDIFMONITOR_ADDR_FLAG_DYNAMIC)) {
  64. // rememeber address, set up
  65. o->ifaddr = event.u.ipv6_addr.addr;
  66. o->up = 1;
  67. // signal up
  68. NCDModuleInst_Backend_Up(o->i);
  69. }
  70. else if (o->up && event.event == NCDIFMONITOR_EVENT_IPV6_ADDR_REMOVED && !memcmp(event.u.ipv6_addr.addr.addr.bytes, o->ifaddr.addr.bytes, 16) && event.u.ipv6_addr.addr.prefix == o->ifaddr.prefix) {
  71. // set not up
  72. o->up = 0;
  73. // signal down
  74. NCDModuleInst_Backend_Down(o->i);
  75. }
  76. }
  77. static void monitor_handler_error (struct instance *o)
  78. {
  79. ModuleLog(o->i, BLOG_ERROR, "monitor error");
  80. instance_free(o, 1);
  81. }
  82. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  83. {
  84. struct instance *o = vo;
  85. o->i = i;
  86. // read arguments
  87. NCDValRef ifname_arg;
  88. if (!NCDVal_ListRead(params->args, 1, &ifname_arg)) {
  89. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  90. goto fail0;
  91. }
  92. if (!NCDVal_IsStringNoNulls(ifname_arg)) {
  93. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  94. goto fail0;
  95. }
  96. // null terminate ifname
  97. NCDValNullTermString ifname_nts;
  98. if (!NCDVal_StringNullTerminate(ifname_arg, &ifname_nts)) {
  99. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  100. goto fail0;
  101. }
  102. // get interface index
  103. int ifindex;
  104. int res = get_iface_info(ifname_nts.data, NULL, NULL, &ifindex);
  105. NCDValNullTermString_Free(&ifname_nts);
  106. if (!res) {
  107. ModuleLog(o->i, BLOG_ERROR, "failed to get interface index");
  108. goto fail0;
  109. }
  110. // init monitor
  111. if (!NCDInterfaceMonitor_Init(&o->monitor, ifindex, NCDIFMONITOR_WATCH_IPV6_ADDR, i->params->iparams->reactor, o, (NCDInterfaceMonitor_handler)monitor_handler, (NCDInterfaceMonitor_handler_error)monitor_handler_error)) {
  112. ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  113. goto fail0;
  114. }
  115. // set not up
  116. o->up = 0;
  117. return;
  118. fail0:
  119. NCDModuleInst_Backend_DeadError(i);
  120. }
  121. static void instance_free (struct instance *o, int is_error)
  122. {
  123. // free monitor
  124. NCDInterfaceMonitor_Free(&o->monitor);
  125. if (is_error) {
  126. NCDModuleInst_Backend_DeadError(o->i);
  127. } else {
  128. NCDModuleInst_Backend_Dead(o->i);
  129. }
  130. }
  131. static void func_die (void *vo)
  132. {
  133. struct instance *o = vo;
  134. instance_free(o, 0);
  135. }
  136. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  137. {
  138. struct instance *o = vo;
  139. ASSERT(o->up)
  140. if (!strcmp(name, "addr")) {
  141. char str[IPADDR6_PRINT_MAX];
  142. ipaddr6_print_addr(o->ifaddr.addr, str);
  143. *out = NCDVal_NewString(mem, str);
  144. return 1;
  145. }
  146. if (!strcmp(name, "prefix")) {
  147. char str[10];
  148. sprintf(str, "%d", o->ifaddr.prefix);
  149. *out = NCDVal_NewString(mem, str);
  150. return 1;
  151. }
  152. if (!strcmp(name, "cidr_addr")) {
  153. char str[IPADDR6_PRINT_MAX];
  154. ipaddr6_print_ifaddr(o->ifaddr, str);
  155. *out = NCDVal_NewString(mem, str);
  156. return 1;
  157. }
  158. return 0;
  159. }
  160. static struct NCDModule modules[] = {
  161. {
  162. .type = "net.ipv6.wait_dynamic_addr",
  163. .func_new2 = func_new,
  164. .func_die = func_die,
  165. .func_getvar = func_getvar,
  166. .alloc_size = sizeof(struct instance)
  167. }, {
  168. .type = NULL
  169. }
  170. };
  171. const struct NCDModuleGroup ncdmodule_net_ipv6_wait_dynamic_addr = {
  172. .modules = modules
  173. };