net_ipv4_addr.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @file net_ipv4_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. * IPv4 address module.
  32. *
  33. * Synopsis:
  34. * net.ipv4.addr(string ifname, string addr, string prefix)
  35. * net.ipv4.addr(string ifname, string cidr_addr)
  36. *
  37. * Description:
  38. * Adds the given address to the given network interface on initialization,
  39. * and removes it on deinitialization. The second form takes the address and
  40. * prefix in CIDR notation (a.b.c.d/n).
  41. */
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <ncd/NCDModule.h>
  45. #include <ncd/NCDIfConfig.h>
  46. #include <generated/blog_channel_ncd_net_ipv4_addr.h>
  47. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  48. struct instance {
  49. NCDModuleInst *i;
  50. NCDValNullTermString ifname_nts;
  51. struct ipv4_ifaddr ifaddr;
  52. };
  53. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  54. {
  55. struct instance *o = vo;
  56. o->i = i;
  57. // read arguments
  58. NCDValRef ifname_arg;
  59. NCDValRef addr_arg;
  60. NCDValRef prefix_arg = NCDVal_NewInvalid();
  61. if (!NCDVal_ListRead(params->args, 2, &ifname_arg, &addr_arg) &&
  62. !NCDVal_ListRead(params->args, 3, &ifname_arg, &addr_arg, &prefix_arg)
  63. ) {
  64. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  65. goto fail0;
  66. }
  67. if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsString(addr_arg) ||
  68. (!NCDVal_IsInvalid(prefix_arg) && !NCDVal_IsString(prefix_arg))
  69. ) {
  70. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  71. goto fail0;
  72. }
  73. // null terminate ifname
  74. if (!NCDVal_StringNullTerminate(ifname_arg, &o->ifname_nts)) {
  75. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  76. goto fail0;
  77. }
  78. if (NCDVal_IsInvalid(prefix_arg)) {
  79. if (!ipaddr_parse_ipv4_ifaddr_bin(NCDVal_StringData(addr_arg), NCDVal_StringLength(addr_arg), &o->ifaddr)) {
  80. ModuleLog(o->i, BLOG_ERROR, "wrong CIDR notation address");
  81. goto fail1;
  82. }
  83. } else {
  84. if (!ipaddr_parse_ipv4_addr_bin(NCDVal_StringData(addr_arg), NCDVal_StringLength(addr_arg), &o->ifaddr.addr)) {
  85. ModuleLog(o->i, BLOG_ERROR, "wrong address");
  86. goto fail1;
  87. }
  88. if (!ipaddr_parse_ipv4_prefix_bin(NCDVal_StringData(prefix_arg), NCDVal_StringLength(prefix_arg), &o->ifaddr.prefix)) {
  89. ModuleLog(o->i, BLOG_ERROR, "wrong prefix");
  90. goto fail1;
  91. }
  92. }
  93. // add address
  94. if (!NCDIfConfig_add_ipv4_addr(o->ifname_nts.data, o->ifaddr)) {
  95. ModuleLog(o->i, BLOG_ERROR, "failed to add IP address");
  96. goto fail1;
  97. }
  98. // signal up
  99. NCDModuleInst_Backend_Up(o->i);
  100. return;
  101. fail1:
  102. NCDValNullTermString_Free(&o->ifname_nts);
  103. fail0:
  104. NCDModuleInst_Backend_DeadError(i);
  105. }
  106. static void func_die (void *vo)
  107. {
  108. struct instance *o = vo;
  109. // remove address
  110. if (!NCDIfConfig_remove_ipv4_addr(o->ifname_nts.data, o->ifaddr)) {
  111. ModuleLog(o->i, BLOG_ERROR, "failed to remove IP address");
  112. }
  113. // free ifname nts
  114. NCDValNullTermString_Free(&o->ifname_nts);
  115. NCDModuleInst_Backend_Dead(o->i);
  116. }
  117. static struct NCDModule modules[] = {
  118. {
  119. .type = "net.ipv4.addr",
  120. .func_new2 = func_new,
  121. .func_die = func_die,
  122. .alloc_size = sizeof(struct instance)
  123. }, {
  124. .type = NULL
  125. }
  126. };
  127. const struct NCDModuleGroup ncdmodule_net_ipv4_addr = {
  128. .modules = modules
  129. };