net_ipv6_route.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <ncd/NCDModule.h>
  50. #include <ncd/NCDIfConfig.h>
  51. #include <generated/blog_channel_ncd_net_ipv6_route.h>
  52. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  53. #define TYPE_NORMAL 1
  54. #define TYPE_IFONLY 2
  55. #define TYPE_BLACKHOLE 3
  56. struct instance {
  57. NCDModuleInst *i;
  58. struct ipv6_ifaddr dest;
  59. int type;
  60. struct ipv6_addr gateway;
  61. int metric;
  62. const char *ifname;
  63. };
  64. static void func_new (void *vo, NCDModuleInst *i)
  65. {
  66. struct instance *o = vo;
  67. o->i = i;
  68. // read arguments
  69. NCDValRef dest_arg;
  70. NCDValRef dest_prefix_arg = NCDVal_NewInvalid();
  71. NCDValRef gateway_arg;
  72. NCDValRef metric_arg;
  73. NCDValRef ifname_arg;
  74. if (!NCDVal_ListRead(i->args, 4, &dest_arg, &gateway_arg, &metric_arg, &ifname_arg) &&
  75. !NCDVal_ListRead(i->args, 5, &dest_arg, &dest_prefix_arg, &gateway_arg, &metric_arg, &ifname_arg)
  76. ) {
  77. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  78. goto fail0;
  79. }
  80. if (!NCDVal_IsStringNoNulls(dest_arg) || !NCDVal_IsStringNoNulls(gateway_arg) ||
  81. !NCDVal_IsStringNoNulls(metric_arg) || !NCDVal_IsStringNoNulls(ifname_arg) ||
  82. (!NCDVal_IsInvalid(dest_prefix_arg) && !NCDVal_IsStringNoNulls(dest_prefix_arg))
  83. ) {
  84. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  85. goto fail0;
  86. }
  87. // read dest
  88. if (NCDVal_IsInvalid(dest_prefix_arg)) {
  89. if (!ipaddr6_parse_ipv6_ifaddr(NCDVal_StringValue(dest_arg), &o->dest)) {
  90. ModuleLog(o->i, BLOG_ERROR, "wrong CIDR notation dest");
  91. goto fail0;
  92. }
  93. } else {
  94. if (!ipaddr6_parse_ipv6_addr(NCDVal_StringValue(dest_arg), &o->dest.addr)) {
  95. ModuleLog(o->i, BLOG_ERROR, "wrong dest addr");
  96. goto fail0;
  97. }
  98. if (!ipaddr6_parse_ipv6_prefix(NCDVal_StringValue(dest_prefix_arg), &o->dest.prefix)) {
  99. ModuleLog(o->i, BLOG_ERROR, "wrong dest prefix");
  100. goto fail0;
  101. }
  102. }
  103. // read gateway and choose type
  104. const char *gateway_str = NCDVal_StringValue(gateway_arg);
  105. if (!strcmp(gateway_str, "none")) {
  106. o->type = TYPE_IFONLY;
  107. }
  108. else if (!strcmp(gateway_str, "blackhole")) {
  109. o->type = TYPE_BLACKHOLE;
  110. } else {
  111. if (!ipaddr6_parse_ipv6_addr(gateway_str, &o->gateway)) {
  112. ModuleLog(o->i, BLOG_ERROR, "wrong gateway");
  113. goto fail0;
  114. }
  115. o->type = TYPE_NORMAL;
  116. }
  117. // read metric
  118. o->metric = atoi(NCDVal_StringValue(metric_arg));
  119. // read ifname
  120. o->ifname = NCDVal_StringValue(ifname_arg);
  121. // add route
  122. int res = 0; // to remove warning
  123. switch (o->type) {
  124. case TYPE_NORMAL:
  125. res = NCDIfConfig_add_ipv6_route(o->dest, &o->gateway, o->metric, o->ifname);
  126. break;
  127. case TYPE_IFONLY:
  128. res = NCDIfConfig_add_ipv6_route(o->dest, NULL, o->metric, o->ifname);
  129. break;
  130. case TYPE_BLACKHOLE:
  131. res = NCDIfConfig_add_ipv6_blackhole_route(o->dest, o->metric);
  132. break;
  133. default: ASSERT(0);
  134. }
  135. if (!res) {
  136. ModuleLog(o->i, BLOG_ERROR, "failed to add route");
  137. goto fail0;
  138. }
  139. // signal up
  140. NCDModuleInst_Backend_Up(o->i);
  141. return;
  142. fail0:
  143. NCDModuleInst_Backend_SetError(i);
  144. NCDModuleInst_Backend_Dead(i);
  145. }
  146. static void func_die (void *vo)
  147. {
  148. struct instance *o = vo;
  149. // remove route
  150. int res = 0; // to remove warning
  151. switch (o->type) {
  152. case TYPE_NORMAL:
  153. res = NCDIfConfig_remove_ipv6_route(o->dest, &o->gateway, o->metric, o->ifname);
  154. break;
  155. case TYPE_IFONLY:
  156. res = NCDIfConfig_remove_ipv6_route(o->dest, NULL, o->metric, o->ifname);
  157. break;
  158. case TYPE_BLACKHOLE:
  159. res = NCDIfConfig_remove_ipv6_blackhole_route(o->dest, o->metric);
  160. break;
  161. default: ASSERT(0);
  162. }
  163. if (!res) {
  164. ModuleLog(o->i, BLOG_ERROR, "failed to remove route");
  165. }
  166. NCDModuleInst_Backend_Dead(o->i);
  167. }
  168. static const struct NCDModule modules[] = {
  169. {
  170. .type = "net.ipv6.route",
  171. .func_new2 = func_new,
  172. .func_die = func_die,
  173. .alloc_size = sizeof(struct instance)
  174. }, {
  175. .type = NULL
  176. }
  177. };
  178. const struct NCDModuleGroup ncdmodule_net_ipv6_route = {
  179. .modules = modules
  180. };