net_ipv6_route.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * @file net_ipv6_route.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. * IPv6 route module.
  32. *
  33. * Synopsis:
  34. * net.ipv6.route(string dest, string dest_prefix, string gateway, string metric, string ifname)
  35. * net.ipv6.route(string cidr_dest, string gateway, string metric, string ifname)
  36. *
  37. * Description:
  38. * Adds an IPv6 route to the system's routing table on initiailzation, and
  39. * removes it on deinitialization. The second form takes the destination in
  40. * CIDR notation (address/prefix).
  41. * If 'gateway' is "none", the route will only be associated with an interface.
  42. * If 'gateway' is "blackhole", the route will be a blackhole route (and 'ifname' is unused).
  43. * NOTE: blackhole routes for IPv6 are not yet implemented in Linux;
  44. * adding them via this interface will only work once they
  45. * have been.
  46. */
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <misc/debug.h>
  50. #include <ncd/NCDModule.h>
  51. #include <ncd/NCDIfConfig.h>
  52. #include <ncd/extra/value_utils.h>
  53. #include <generated/blog_channel_ncd_net_ipv6_route.h>
  54. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  55. #define TYPE_NORMAL 1
  56. #define TYPE_IFONLY 2
  57. #define TYPE_BLACKHOLE 3
  58. struct instance {
  59. NCDModuleInst *i;
  60. struct ipv6_ifaddr dest;
  61. int type;
  62. struct ipv6_addr gateway;
  63. int metric;
  64. NCDValNullTermString ifname_nts;
  65. };
  66. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  67. {
  68. struct instance *o = vo;
  69. o->i = i;
  70. // read arguments
  71. NCDValRef dest_arg;
  72. NCDValRef dest_prefix_arg = NCDVal_NewInvalid();
  73. NCDValRef gateway_arg;
  74. NCDValRef metric_arg;
  75. NCDValRef ifname_arg;
  76. if (!NCDVal_ListRead(params->args, 4, &dest_arg, &gateway_arg, &metric_arg, &ifname_arg) &&
  77. !NCDVal_ListRead(params->args, 5, &dest_arg, &dest_prefix_arg, &gateway_arg, &metric_arg, &ifname_arg)
  78. ) {
  79. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  80. goto fail0;
  81. }
  82. if (!NCDVal_IsString(dest_arg) || !NCDVal_IsString(gateway_arg) ||
  83. !NCDVal_IsString(metric_arg) || !NCDVal_IsStringNoNulls(ifname_arg) ||
  84. (!NCDVal_IsInvalid(dest_prefix_arg) && !NCDVal_IsString(dest_prefix_arg))
  85. ) {
  86. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  87. goto fail0;
  88. }
  89. // read dest
  90. if (NCDVal_IsInvalid(dest_prefix_arg)) {
  91. if (!ipaddr6_parse_ipv6_ifaddr_bin(NCDVal_StringData(dest_arg), NCDVal_StringLength(dest_arg), &o->dest)) {
  92. ModuleLog(o->i, BLOG_ERROR, "wrong CIDR notation dest");
  93. goto fail0;
  94. }
  95. } else {
  96. if (!ipaddr6_parse_ipv6_addr_bin(NCDVal_StringData(dest_arg), NCDVal_StringLength(dest_arg), &o->dest.addr)) {
  97. ModuleLog(o->i, BLOG_ERROR, "wrong dest addr");
  98. goto fail0;
  99. }
  100. if (!ipaddr6_parse_ipv6_prefix_bin(NCDVal_StringData(dest_prefix_arg), NCDVal_StringLength(dest_prefix_arg), &o->dest.prefix)) {
  101. ModuleLog(o->i, BLOG_ERROR, "wrong dest prefix");
  102. goto fail0;
  103. }
  104. }
  105. // read gateway and choose type
  106. if (NCDVal_StringEquals(gateway_arg, "none")) {
  107. o->type = TYPE_IFONLY;
  108. }
  109. else if (NCDVal_StringEquals(gateway_arg, "blackhole")) {
  110. o->type = TYPE_BLACKHOLE;
  111. } else {
  112. if (!ipaddr6_parse_ipv6_addr_bin(NCDVal_StringData(gateway_arg), NCDVal_StringLength(gateway_arg), &o->gateway)) {
  113. ModuleLog(o->i, BLOG_ERROR, "wrong gateway");
  114. goto fail0;
  115. }
  116. o->type = TYPE_NORMAL;
  117. }
  118. // read metric
  119. uintmax_t metric;
  120. if (!ncd_read_uintmax(metric_arg, &metric) || metric > INT_MAX) {
  121. ModuleLog(i, BLOG_ERROR, "bad metric");
  122. goto fail0;
  123. }
  124. o->metric = metric;
  125. // null terminate ifname
  126. if (!NCDVal_StringNullTerminate(ifname_arg, &o->ifname_nts)) {
  127. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  128. goto fail0;
  129. }
  130. // add route
  131. int res = 0; // to remove warning
  132. switch (o->type) {
  133. case TYPE_NORMAL:
  134. res = NCDIfConfig_add_ipv6_route(o->dest, &o->gateway, o->metric, o->ifname_nts.data);
  135. break;
  136. case TYPE_IFONLY:
  137. res = NCDIfConfig_add_ipv6_route(o->dest, NULL, o->metric, o->ifname_nts.data);
  138. break;
  139. case TYPE_BLACKHOLE:
  140. res = NCDIfConfig_add_ipv6_blackhole_route(o->dest, o->metric);
  141. break;
  142. default: ASSERT(0);
  143. }
  144. if (!res) {
  145. ModuleLog(o->i, BLOG_ERROR, "failed to add route");
  146. goto fail1;
  147. }
  148. // signal up
  149. NCDModuleInst_Backend_Up(o->i);
  150. return;
  151. fail1:
  152. NCDValNullTermString_Free(&o->ifname_nts);
  153. fail0:
  154. NCDModuleInst_Backend_DeadError(i);
  155. }
  156. static void func_die (void *vo)
  157. {
  158. struct instance *o = vo;
  159. // remove route
  160. int res = 0; // to remove warning
  161. switch (o->type) {
  162. case TYPE_NORMAL:
  163. res = NCDIfConfig_remove_ipv6_route(o->dest, &o->gateway, o->metric, o->ifname_nts.data);
  164. break;
  165. case TYPE_IFONLY:
  166. res = NCDIfConfig_remove_ipv6_route(o->dest, NULL, o->metric, o->ifname_nts.data);
  167. break;
  168. case TYPE_BLACKHOLE:
  169. res = NCDIfConfig_remove_ipv6_blackhole_route(o->dest, o->metric);
  170. break;
  171. default: ASSERT(0);
  172. }
  173. if (!res) {
  174. ModuleLog(o->i, BLOG_ERROR, "failed to remove route");
  175. }
  176. // free ifname nts
  177. NCDValNullTermString_Free(&o->ifname_nts);
  178. NCDModuleInst_Backend_Dead(o->i);
  179. }
  180. static struct NCDModule modules[] = {
  181. {
  182. .type = "net.ipv6.route",
  183. .func_new2 = func_new,
  184. .func_die = func_die,
  185. .alloc_size = sizeof(struct instance)
  186. }, {
  187. .type = NULL
  188. }
  189. };
  190. const struct NCDModuleGroup ncdmodule_net_ipv6_route = {
  191. .modules = modules
  192. };