PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/external/iptables/extensions/libxt_TEE.c

http://droidwall.googlecode.com/
C | 202 lines | 165 code | 27 blank | 10 comment | 24 complexity | e4b28d64398b99f49b6665e5babcbacc MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * "TEE" target extension for iptables
  3. * Copyright © Sebastian Claßen <sebastian.classen [at] freenet.ag>, 2007
  4. * Jan Engelhardt <jengelh [at] medozas de>, 2007 - 2010
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License; either
  8. * version 2 of the License, or any later version, as published by the
  9. * Free Software Foundation.
  10. */
  11. #include <sys/socket.h>
  12. #include <getopt.h>
  13. #include <stdbool.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <arpa/inet.h>
  18. #include <net/if.h>
  19. #include <netinet/in.h>
  20. #include <xtables.h>
  21. #include <linux/netfilter.h>
  22. #include <linux/netfilter/x_tables.h>
  23. #include <linux/netfilter/xt_TEE.h>
  24. enum {
  25. FLAG_GATEWAY = 1 << 0,
  26. FLAG_OIF = 1 << 1,
  27. };
  28. static const struct option tee_tg_opts[] = {
  29. {.name = "gateway", .has_arg = true, .val = 'g'},
  30. {.name = "oif", .has_arg = true, .val = 'o'},
  31. {NULL},
  32. };
  33. static void tee_tg_help(void)
  34. {
  35. printf(
  36. "TEE target options:\n"
  37. " --gateway IPADDR Route packet via the gateway given by address\n"
  38. " --oif NAME Include oif in route calculation\n"
  39. "\n");
  40. }
  41. static int tee_tg_parse(int c, char **argv, int invert, unsigned int *flags,
  42. const void *entry, struct xt_entry_target **target)
  43. {
  44. struct xt_tee_tginfo *info = (void *)(*target)->data;
  45. const struct in_addr *ia;
  46. switch (c) {
  47. case 'g':
  48. if (*flags & FLAG_GATEWAY)
  49. xtables_error(PARAMETER_PROBLEM,
  50. "Cannot specify --gateway more than once");
  51. ia = xtables_numeric_to_ipaddr(optarg);
  52. if (ia == NULL)
  53. xtables_error(PARAMETER_PROBLEM,
  54. "Invalid IP address %s", optarg);
  55. memcpy(&info->gw, ia, sizeof(*ia));
  56. *flags |= FLAG_GATEWAY;
  57. return true;
  58. case 'o':
  59. if (*flags & FLAG_OIF)
  60. xtables_error(PARAMETER_PROBLEM,
  61. "Cannot specify --oif more than once");
  62. if (strlen(optarg) >= sizeof(info->oif))
  63. xtables_error(PARAMETER_PROBLEM,
  64. "oif name too long");
  65. strcpy(info->oif, optarg);
  66. *flags |= FLAG_OIF;
  67. return true;
  68. }
  69. return false;
  70. }
  71. static int tee_tg6_parse(int c, char **argv, int invert, unsigned int *flags,
  72. const void *entry, struct xt_entry_target **target)
  73. {
  74. struct xt_tee_tginfo *info = (void *)(*target)->data;
  75. const struct in6_addr *ia;
  76. switch (c) {
  77. case 'g':
  78. if (*flags & FLAG_GATEWAY)
  79. xtables_error(PARAMETER_PROBLEM,
  80. "Cannot specify --gateway more than once");
  81. ia = xtables_numeric_to_ip6addr(optarg);
  82. if (ia == NULL)
  83. xtables_error(PARAMETER_PROBLEM,
  84. "Invalid IP address %s", optarg);
  85. memcpy(&info->gw, ia, sizeof(*ia));
  86. *flags |= FLAG_GATEWAY;
  87. return true;
  88. case 'o':
  89. if (*flags & FLAG_OIF)
  90. xtables_error(PARAMETER_PROBLEM,
  91. "Cannot specify --oif more than once");
  92. if (strlen(optarg) >= sizeof(info->oif))
  93. xtables_error(PARAMETER_PROBLEM,
  94. "oif name too long");
  95. strcpy(info->oif, optarg);
  96. *flags |= FLAG_OIF;
  97. return true;
  98. }
  99. return false;
  100. }
  101. static void tee_tg_check(unsigned int flags)
  102. {
  103. if (flags == 0)
  104. xtables_error(PARAMETER_PROBLEM, "TEE target: "
  105. "--gateway parameter required");
  106. }
  107. static void tee_tg_print(const void *ip, const struct xt_entry_target *target,
  108. int numeric)
  109. {
  110. const struct xt_tee_tginfo *info = (const void *)target->data;
  111. if (numeric)
  112. printf("TEE gw:%s ", xtables_ipaddr_to_numeric(&info->gw.in));
  113. else
  114. printf("TEE gw:%s ", xtables_ipaddr_to_anyname(&info->gw.in));
  115. if (*info->oif != '\0')
  116. printf("oif=%s ", info->oif);
  117. }
  118. static void tee_tg6_print(const void *ip, const struct xt_entry_target *target,
  119. int numeric)
  120. {
  121. const struct xt_tee_tginfo *info = (const void *)target->data;
  122. if (numeric)
  123. printf("TEE gw:%s ", xtables_ip6addr_to_numeric(&info->gw.in6));
  124. else
  125. printf("TEE gw:%s ", xtables_ip6addr_to_anyname(&info->gw.in6));
  126. if (*info->oif != '\0')
  127. printf("oif=%s ", info->oif);
  128. }
  129. static void tee_tg_save(const void *ip, const struct xt_entry_target *target)
  130. {
  131. const struct xt_tee_tginfo *info = (const void *)target->data;
  132. printf("--gateway %s ", xtables_ipaddr_to_numeric(&info->gw.in));
  133. if (*info->oif != '\0')
  134. printf("--oif %s ", info->oif);
  135. }
  136. static void tee_tg6_save(const void *ip, const struct xt_entry_target *target)
  137. {
  138. const struct xt_tee_tginfo *info = (const void *)target->data;
  139. printf("--gateway %s ", xtables_ip6addr_to_numeric(&info->gw.in6));
  140. if (*info->oif != '\0')
  141. printf("--oif %s ", info->oif);
  142. }
  143. static struct xtables_target tee_tg_reg = {
  144. .name = "TEE",
  145. .version = XTABLES_VERSION,
  146. .revision = 1,
  147. .family = NFPROTO_IPV4,
  148. .size = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
  149. .userspacesize = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
  150. .help = tee_tg_help,
  151. .parse = tee_tg_parse,
  152. .final_check = tee_tg_check,
  153. .print = tee_tg_print,
  154. .save = tee_tg_save,
  155. .extra_opts = tee_tg_opts,
  156. };
  157. static struct xtables_target tee_tg6_reg = {
  158. .name = "TEE",
  159. .version = XTABLES_VERSION,
  160. .revision = 1,
  161. .family = NFPROTO_IPV6,
  162. .size = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
  163. .userspacesize = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
  164. .help = tee_tg_help,
  165. .parse = tee_tg6_parse,
  166. .final_check = tee_tg_check,
  167. .print = tee_tg6_print,
  168. .save = tee_tg6_save,
  169. .extra_opts = tee_tg_opts,
  170. };
  171. void libxt_TEE_init(void)
  172. {
  173. xtables_register_target(&tee_tg_reg);
  174. xtables_register_target(&tee_tg6_reg);
  175. }