libxt_policy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <netdb.h>
  6. #include <xtables.h>
  7. #include <linux/netfilter/xt_policy.h>
  8. enum {
  9. O_DIRECTION = 0,
  10. O_POLICY,
  11. O_STRICT,
  12. O_REQID,
  13. O_SPI,
  14. O_PROTO,
  15. O_MODE,
  16. O_TUNNELSRC,
  17. O_TUNNELDST,
  18. O_NEXT,
  19. F_STRICT = 1 << O_STRICT,
  20. };
  21. static void policy_help(void)
  22. {
  23. printf(
  24. "policy match options:\n"
  25. " --dir in|out match policy applied during decapsulation/\n"
  26. " policy to be applied during encapsulation\n"
  27. " --pol none|ipsec match policy\n"
  28. " --strict match entire policy instead of single element\n"
  29. " at any position\n"
  30. "These options may be used repeatedly, to describe policy elements:\n"
  31. "[!] --reqid reqid match reqid\n"
  32. "[!] --spi spi match SPI\n"
  33. "[!] --proto proto match protocol (ah/esp/ipcomp)\n"
  34. "[!] --mode mode match mode (transport/tunnel)\n"
  35. "[!] --tunnel-src addr/mask match tunnel source\n"
  36. "[!] --tunnel-dst addr/mask match tunnel destination\n"
  37. " --next begin next element in policy\n");
  38. }
  39. static const struct xt_option_entry policy_opts[] = {
  40. {.name = "dir", .id = O_DIRECTION, .type = XTTYPE_STRING},
  41. {.name = "pol", .id = O_POLICY, .type = XTTYPE_STRING},
  42. {.name = "strict", .id = O_STRICT, .type = XTTYPE_NONE},
  43. {.name = "reqid", .id = O_REQID, .type = XTTYPE_UINT32,
  44. .flags = XTOPT_MULTI | XTOPT_INVERT},
  45. {.name = "spi", .id = O_SPI, .type = XTTYPE_UINT32,
  46. .flags = XTOPT_MULTI | XTOPT_INVERT},
  47. {.name = "tunnel-src", .id = O_TUNNELSRC, .type = XTTYPE_HOSTMASK,
  48. .flags = XTOPT_MULTI | XTOPT_INVERT},
  49. {.name = "tunnel-dst", .id = O_TUNNELDST, .type = XTTYPE_HOSTMASK,
  50. .flags = XTOPT_MULTI | XTOPT_INVERT},
  51. {.name = "proto", .id = O_PROTO, .type = XTTYPE_PROTOCOL,
  52. .flags = XTOPT_MULTI | XTOPT_INVERT},
  53. {.name = "mode", .id = O_MODE, .type = XTTYPE_STRING,
  54. .flags = XTOPT_MULTI | XTOPT_INVERT},
  55. {.name = "next", .id = O_NEXT, .type = XTTYPE_NONE,
  56. .flags = XTOPT_MULTI, .also = F_STRICT},
  57. XTOPT_TABLEEND,
  58. };
  59. static int parse_direction(const char *s)
  60. {
  61. if (strcmp(s, "in") == 0)
  62. return XT_POLICY_MATCH_IN;
  63. if (strcmp(s, "out") == 0)
  64. return XT_POLICY_MATCH_OUT;
  65. xtables_error(PARAMETER_PROBLEM, "policy_match: invalid dir \"%s\"", s);
  66. }
  67. static int parse_policy(const char *s)
  68. {
  69. if (strcmp(s, "none") == 0)
  70. return XT_POLICY_MATCH_NONE;
  71. if (strcmp(s, "ipsec") == 0)
  72. return 0;
  73. xtables_error(PARAMETER_PROBLEM, "policy match: invalid policy \"%s\"", s);
  74. }
  75. static int parse_mode(const char *s)
  76. {
  77. if (strcmp(s, "transport") == 0)
  78. return XT_POLICY_MODE_TRANSPORT;
  79. if (strcmp(s, "tunnel") == 0)
  80. return XT_POLICY_MODE_TUNNEL;
  81. xtables_error(PARAMETER_PROBLEM, "policy match: invalid mode \"%s\"", s);
  82. }
  83. static void policy_parse(struct xt_option_call *cb)
  84. {
  85. struct xt_policy_info *info = cb->data;
  86. struct xt_policy_elem *e = &info->pol[info->len];
  87. xtables_option_parse(cb);
  88. switch (cb->entry->id) {
  89. case O_DIRECTION:
  90. info->flags |= parse_direction(cb->arg);
  91. break;
  92. case O_POLICY:
  93. info->flags |= parse_policy(cb->arg);
  94. break;
  95. case O_STRICT:
  96. info->flags |= XT_POLICY_MATCH_STRICT;
  97. break;
  98. case O_REQID:
  99. if (e->match.reqid)
  100. xtables_error(PARAMETER_PROBLEM,
  101. "policy match: double --reqid option");
  102. e->match.reqid = 1;
  103. e->invert.reqid = cb->invert;
  104. e->reqid = cb->val.u32;
  105. break;
  106. case O_SPI:
  107. if (e->match.spi)
  108. xtables_error(PARAMETER_PROBLEM,
  109. "policy match: double --spi option");
  110. e->match.spi = 1;
  111. e->invert.spi = cb->invert;
  112. e->spi = cb->val.u32;
  113. break;
  114. case O_TUNNELSRC:
  115. if (e->match.saddr)
  116. xtables_error(PARAMETER_PROBLEM,
  117. "policy match: double --tunnel-src option");
  118. e->match.saddr = 1;
  119. e->invert.saddr = cb->invert;
  120. memcpy(&e->saddr, &cb->val.haddr, sizeof(cb->val.haddr));
  121. memcpy(&e->smask, &cb->val.hmask, sizeof(cb->val.hmask));
  122. break;
  123. case O_TUNNELDST:
  124. if (e->match.daddr)
  125. xtables_error(PARAMETER_PROBLEM,
  126. "policy match: double --tunnel-dst option");
  127. e->match.daddr = 1;
  128. e->invert.daddr = cb->invert;
  129. memcpy(&e->daddr, &cb->val.haddr, sizeof(cb->val.haddr));
  130. memcpy(&e->dmask, &cb->val.hmask, sizeof(cb->val.hmask));
  131. break;
  132. case O_PROTO:
  133. if (e->match.proto)
  134. xtables_error(PARAMETER_PROBLEM,
  135. "policy match: double --proto option");
  136. e->proto = cb->val.protocol;
  137. if (e->proto != IPPROTO_AH && e->proto != IPPROTO_ESP &&
  138. e->proto != IPPROTO_COMP)
  139. xtables_error(PARAMETER_PROBLEM,
  140. "policy match: protocol must be ah/esp/ipcomp");
  141. e->match.proto = 1;
  142. e->invert.proto = cb->invert;
  143. break;
  144. case O_MODE:
  145. if (e->match.mode)
  146. xtables_error(PARAMETER_PROBLEM,
  147. "policy match: double --mode option");
  148. e->match.mode = 1;
  149. e->invert.mode = cb->invert;
  150. e->mode = parse_mode(cb->arg);
  151. break;
  152. case O_NEXT:
  153. if (++info->len == XT_POLICY_MAX_ELEM)
  154. xtables_error(PARAMETER_PROBLEM,
  155. "policy match: maximum policy depth reached");
  156. break;
  157. }
  158. }
  159. static void policy_check(struct xt_fcheck_call *cb)
  160. {
  161. struct xt_policy_info *info = cb->data;
  162. const struct xt_policy_elem *e;
  163. int i;
  164. /*
  165. * The old "no parameters given" check is carried out
  166. * by testing for --dir.
  167. */
  168. if (!(info->flags & (XT_POLICY_MATCH_IN | XT_POLICY_MATCH_OUT)))
  169. xtables_error(PARAMETER_PROBLEM,
  170. "policy match: neither --dir in nor --dir out specified");
  171. if (info->flags & XT_POLICY_MATCH_NONE) {
  172. if (info->flags & XT_POLICY_MATCH_STRICT)
  173. xtables_error(PARAMETER_PROBLEM,
  174. "policy match: policy none but --strict given");
  175. if (info->len != 0)
  176. xtables_error(PARAMETER_PROBLEM,
  177. "policy match: policy none but policy given");
  178. } else
  179. info->len++; /* increase len by 1, no --next after last element */
  180. /*
  181. * This is already represented with O_NEXT requiring F_STRICT in the
  182. * options table, but will keep this code as a comment for reference.
  183. *
  184. if (!(info->flags & XT_POLICY_MATCH_STRICT) && info->len > 1)
  185. xtables_error(PARAMETER_PROBLEM,
  186. "policy match: multiple elements but no --strict");
  187. */
  188. for (i = 0; i < info->len; i++) {
  189. e = &info->pol[i];
  190. if (info->flags & XT_POLICY_MATCH_STRICT &&
  191. !(e->match.reqid || e->match.spi || e->match.saddr ||
  192. e->match.daddr || e->match.proto || e->match.mode))
  193. xtables_error(PARAMETER_PROBLEM,
  194. "policy match: empty policy element %u. "
  195. "--strict is in effect, but at least one of "
  196. "reqid, spi, tunnel-src, tunnel-dst, proto or "
  197. "mode is required.", i);
  198. if ((e->match.saddr || e->match.daddr)
  199. && ((e->mode == XT_POLICY_MODE_TUNNEL && e->invert.mode) ||
  200. (e->mode == XT_POLICY_MODE_TRANSPORT && !e->invert.mode)))
  201. xtables_error(PARAMETER_PROBLEM,
  202. "policy match: --tunnel-src/--tunnel-dst "
  203. "is only valid in tunnel mode");
  204. }
  205. }
  206. static void print_mode(const char *prefix, uint8_t mode, int numeric)
  207. {
  208. printf(" %smode ", prefix);
  209. switch (mode) {
  210. case XT_POLICY_MODE_TRANSPORT:
  211. printf("transport");
  212. break;
  213. case XT_POLICY_MODE_TUNNEL:
  214. printf("tunnel");
  215. break;
  216. default:
  217. printf("???");
  218. break;
  219. }
  220. }
  221. static void print_proto(const char *prefix, uint8_t proto, int numeric)
  222. {
  223. const struct protoent *p = NULL;
  224. printf(" %sproto ", prefix);
  225. if (!numeric)
  226. p = getprotobynumber(proto);
  227. if (p != NULL)
  228. printf("%s", p->p_name);
  229. else
  230. printf("%u", proto);
  231. }
  232. #define PRINT_INVERT(x) \
  233. do { \
  234. if (x) \
  235. printf(" !"); \
  236. } while(0)
  237. static void print_entry(const char *prefix, const struct xt_policy_elem *e,
  238. bool numeric, uint8_t family)
  239. {
  240. if (e->match.reqid) {
  241. PRINT_INVERT(e->invert.reqid);
  242. printf(" %sreqid %u", prefix, e->reqid);
  243. }
  244. if (e->match.spi) {
  245. PRINT_INVERT(e->invert.spi);
  246. printf(" %sspi 0x%x", prefix, e->spi);
  247. }
  248. if (e->match.proto) {
  249. PRINT_INVERT(e->invert.proto);
  250. print_proto(prefix, e->proto, numeric);
  251. }
  252. if (e->match.mode) {
  253. PRINT_INVERT(e->invert.mode);
  254. print_mode(prefix, e->mode, numeric);
  255. }
  256. if (e->match.daddr) {
  257. PRINT_INVERT(e->invert.daddr);
  258. if (family == NFPROTO_IPV6)
  259. printf(" %stunnel-dst %s%s", prefix,
  260. xtables_ip6addr_to_numeric(&e->daddr.a6),
  261. xtables_ip6mask_to_numeric(&e->dmask.a6));
  262. else
  263. printf(" %stunnel-dst %s%s", prefix,
  264. xtables_ipaddr_to_numeric(&e->daddr.a4),
  265. xtables_ipmask_to_numeric(&e->dmask.a4));
  266. }
  267. if (e->match.saddr) {
  268. PRINT_INVERT(e->invert.saddr);
  269. if (family == NFPROTO_IPV6)
  270. printf(" %stunnel-src %s%s", prefix,
  271. xtables_ip6addr_to_numeric(&e->saddr.a6),
  272. xtables_ip6mask_to_numeric(&e->smask.a6));
  273. else
  274. printf(" %stunnel-src %s%s", prefix,
  275. xtables_ipaddr_to_numeric(&e->saddr.a4),
  276. xtables_ipmask_to_numeric(&e->smask.a4));
  277. }
  278. }
  279. static void print_flags(const char *prefix, const struct xt_policy_info *info)
  280. {
  281. if (info->flags & XT_POLICY_MATCH_IN)
  282. printf(" %sdir in", prefix);
  283. else
  284. printf(" %sdir out", prefix);
  285. if (info->flags & XT_POLICY_MATCH_NONE)
  286. printf(" %spol none", prefix);
  287. else
  288. printf(" %spol ipsec", prefix);
  289. if (info->flags & XT_POLICY_MATCH_STRICT)
  290. printf(" %sstrict", prefix);
  291. }
  292. static void policy4_print(const void *ip, const struct xt_entry_match *match,
  293. int numeric)
  294. {
  295. const struct xt_policy_info *info = (void *)match->data;
  296. unsigned int i;
  297. printf(" policy match");
  298. print_flags("", info);
  299. for (i = 0; i < info->len; i++) {
  300. if (info->len > 1)
  301. printf(" [%u]", i);
  302. print_entry("", &info->pol[i], numeric, NFPROTO_IPV4);
  303. }
  304. }
  305. static void policy6_print(const void *ip, const struct xt_entry_match *match,
  306. int numeric)
  307. {
  308. const struct xt_policy_info *info = (void *)match->data;
  309. unsigned int i;
  310. printf(" policy match");
  311. print_flags("", info);
  312. for (i = 0; i < info->len; i++) {
  313. if (info->len > 1)
  314. printf(" [%u]", i);
  315. print_entry("", &info->pol[i], numeric, NFPROTO_IPV6);
  316. }
  317. }
  318. static void policy4_save(const void *ip, const struct xt_entry_match *match)
  319. {
  320. const struct xt_policy_info *info = (void *)match->data;
  321. unsigned int i;
  322. print_flags("--", info);
  323. for (i = 0; i < info->len; i++) {
  324. print_entry("--", &info->pol[i], false, NFPROTO_IPV4);
  325. if (i + 1 < info->len)
  326. printf(" --next");
  327. }
  328. }
  329. static void policy6_save(const void *ip, const struct xt_entry_match *match)
  330. {
  331. const struct xt_policy_info *info = (void *)match->data;
  332. unsigned int i;
  333. print_flags("--", info);
  334. for (i = 0; i < info->len; i++) {
  335. print_entry("--", &info->pol[i], false, NFPROTO_IPV6);
  336. if (i + 1 < info->len)
  337. printf(" --next");
  338. }
  339. }
  340. static struct xtables_match policy_mt_reg[] = {
  341. {
  342. .name = "policy",
  343. .version = XTABLES_VERSION,
  344. .family = NFPROTO_IPV4,
  345. .size = XT_ALIGN(sizeof(struct xt_policy_info)),
  346. .userspacesize = XT_ALIGN(sizeof(struct xt_policy_info)),
  347. .help = policy_help,
  348. .x6_parse = policy_parse,
  349. .x6_fcheck = policy_check,
  350. .print = policy4_print,
  351. .save = policy4_save,
  352. .x6_options = policy_opts,
  353. },
  354. {
  355. .name = "policy",
  356. .version = XTABLES_VERSION,
  357. .family = NFPROTO_IPV6,
  358. .size = XT_ALIGN(sizeof(struct xt_policy_info)),
  359. .userspacesize = XT_ALIGN(sizeof(struct xt_policy_info)),
  360. .help = policy_help,
  361. .x6_parse = policy_parse,
  362. .x6_fcheck = policy_check,
  363. .print = policy6_print,
  364. .save = policy6_save,
  365. .x6_options = policy_opts,
  366. },
  367. };
  368. void _init(void)
  369. {
  370. xtables_register_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg));
  371. }