libxt_recent.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <xtables.h>
  5. #include <linux/netfilter/xt_recent.h>
  6. enum {
  7. O_SET = 0,
  8. O_RCHECK,
  9. O_UPDATE,
  10. O_REMOVE,
  11. O_SECONDS,
  12. O_REAP,
  13. O_HITCOUNT,
  14. O_RTTL,
  15. O_NAME,
  16. O_RSOURCE,
  17. O_RDEST,
  18. O_MASK,
  19. F_SET = 1 << O_SET,
  20. F_RCHECK = 1 << O_RCHECK,
  21. F_UPDATE = 1 << O_UPDATE,
  22. F_REMOVE = 1 << O_REMOVE,
  23. F_SECONDS = 1 << O_SECONDS,
  24. F_ANY_OP = F_SET | F_RCHECK | F_UPDATE | F_REMOVE,
  25. };
  26. #define s struct xt_recent_mtinfo
  27. static const struct xt_option_entry recent_opts_v0[] = {
  28. {.name = "set", .id = O_SET, .type = XTTYPE_NONE,
  29. .excl = F_ANY_OP, .flags = XTOPT_INVERT},
  30. {.name = "rcheck", .id = O_RCHECK, .type = XTTYPE_NONE,
  31. .excl = F_ANY_OP, .flags = XTOPT_INVERT},
  32. {.name = "update", .id = O_UPDATE, .type = XTTYPE_NONE,
  33. .excl = F_ANY_OP, .flags = XTOPT_INVERT},
  34. {.name = "remove", .id = O_REMOVE, .type = XTTYPE_NONE,
  35. .excl = F_ANY_OP, .flags = XTOPT_INVERT},
  36. {.name = "seconds", .id = O_SECONDS, .type = XTTYPE_UINT32,
  37. .flags = XTOPT_PUT, XTOPT_POINTER(s, seconds), .min = 1},
  38. {.name = "reap", .id = O_REAP, .type = XTTYPE_NONE,
  39. .also = F_SECONDS },
  40. {.name = "hitcount", .id = O_HITCOUNT, .type = XTTYPE_UINT32,
  41. .flags = XTOPT_PUT, XTOPT_POINTER(s, hit_count)},
  42. {.name = "rttl", .id = O_RTTL, .type = XTTYPE_NONE,
  43. .excl = F_SET | F_REMOVE},
  44. {.name = "name", .id = O_NAME, .type = XTTYPE_STRING,
  45. .flags = XTOPT_PUT, XTOPT_POINTER(s, name)},
  46. {.name = "rsource", .id = O_RSOURCE, .type = XTTYPE_NONE},
  47. {.name = "rdest", .id = O_RDEST, .type = XTTYPE_NONE},
  48. XTOPT_TABLEEND,
  49. };
  50. #undef s
  51. #define s struct xt_recent_mtinfo_v1
  52. static const struct xt_option_entry recent_opts_v1[] = {
  53. {.name = "set", .id = O_SET, .type = XTTYPE_NONE,
  54. .excl = F_ANY_OP, .flags = XTOPT_INVERT},
  55. {.name = "rcheck", .id = O_RCHECK, .type = XTTYPE_NONE,
  56. .excl = F_ANY_OP, .flags = XTOPT_INVERT},
  57. {.name = "update", .id = O_UPDATE, .type = XTTYPE_NONE,
  58. .excl = F_ANY_OP, .flags = XTOPT_INVERT},
  59. {.name = "remove", .id = O_REMOVE, .type = XTTYPE_NONE,
  60. .excl = F_ANY_OP, .flags = XTOPT_INVERT},
  61. {.name = "seconds", .id = O_SECONDS, .type = XTTYPE_UINT32,
  62. .flags = XTOPT_PUT, XTOPT_POINTER(s, seconds)},
  63. {.name = "hitcount", .id = O_HITCOUNT, .type = XTTYPE_UINT32,
  64. .flags = XTOPT_PUT, XTOPT_POINTER(s, hit_count)},
  65. {.name = "rttl", .id = O_RTTL, .type = XTTYPE_NONE,
  66. .excl = F_SET | F_REMOVE},
  67. {.name = "name", .id = O_NAME, .type = XTTYPE_STRING,
  68. .flags = XTOPT_PUT, XTOPT_POINTER(s, name)},
  69. {.name = "rsource", .id = O_RSOURCE, .type = XTTYPE_NONE},
  70. {.name = "rdest", .id = O_RDEST, .type = XTTYPE_NONE},
  71. {.name = "mask", .id = O_MASK, .type = XTTYPE_HOST,
  72. .flags = XTOPT_PUT, XTOPT_POINTER(s, mask)},
  73. XTOPT_TABLEEND,
  74. };
  75. #undef s
  76. static void recent_help(void)
  77. {
  78. printf(
  79. "recent match options:\n"
  80. "[!] --set Add source address to list, always matches.\n"
  81. "[!] --rcheck Match if source address in list.\n"
  82. "[!] --update Match if source address in list, also update last-seen time.\n"
  83. "[!] --remove Match if source address in list, also removes that address from list.\n"
  84. " --seconds seconds For check and update commands above.\n"
  85. " Specifies that the match will only occur if source address last seen within\n"
  86. " the last 'seconds' seconds.\n"
  87. " --reap Purge entries older then 'seconds'.\n"
  88. " Can only be used in conjunction with the seconds option.\n"
  89. " --hitcount hits For check and update commands above.\n"
  90. " Specifies that the match will only occur if source address seen hits times.\n"
  91. " May be used in conjunction with the seconds option.\n"
  92. " --rttl For check and update commands above.\n"
  93. " Specifies that the match will only occur if the source address and the TTL\n"
  94. " match between this packet and the one which was set.\n"
  95. " Useful if you have problems with people spoofing their source address in order\n"
  96. " to DoS you via this module.\n"
  97. " --name name Name of the recent list to be used. DEFAULT used if none given.\n"
  98. " --rsource Match/Save the source address of each packet in the recent list table (default).\n"
  99. " --rdest Match/Save the destination address of each packet in the recent list table.\n"
  100. " --mask netmask Netmask that will be applied to this recent list.\n"
  101. "xt_recent by: Stephen Frost <sfrost@snowman.net>. http://snowman.net/projects/ipt_recent/\n");
  102. }
  103. enum {
  104. XT_RECENT_REV_0 = 0,
  105. XT_RECENT_REV_1,
  106. };
  107. static void recent_init(struct xt_entry_match *match, unsigned int rev)
  108. {
  109. struct xt_recent_mtinfo *info = (struct xt_recent_mtinfo *)match->data;
  110. struct xt_recent_mtinfo_v1 *info_v1 =
  111. (struct xt_recent_mtinfo_v1 *)match->data;
  112. strncpy(info->name,"DEFAULT", XT_RECENT_NAME_LEN);
  113. /* even though XT_RECENT_NAME_LEN is currently defined as 200,
  114. * better be safe, than sorry */
  115. info->name[XT_RECENT_NAME_LEN-1] = '\0';
  116. info->side = XT_RECENT_SOURCE;
  117. if (rev == XT_RECENT_REV_1)
  118. memset(&info_v1->mask, 0xFF, sizeof(info_v1->mask));
  119. }
  120. static void recent_parse(struct xt_option_call *cb)
  121. {
  122. struct xt_recent_mtinfo *info = cb->data;
  123. xtables_option_parse(cb);
  124. switch (cb->entry->id) {
  125. case O_SET:
  126. info->check_set |= XT_RECENT_SET;
  127. if (cb->invert)
  128. info->invert = true;
  129. break;
  130. case O_RCHECK:
  131. info->check_set |= XT_RECENT_CHECK;
  132. if (cb->invert)
  133. info->invert = true;
  134. break;
  135. case O_UPDATE:
  136. info->check_set |= XT_RECENT_UPDATE;
  137. if (cb->invert)
  138. info->invert = true;
  139. break;
  140. case O_REMOVE:
  141. info->check_set |= XT_RECENT_REMOVE;
  142. if (cb->invert)
  143. info->invert = true;
  144. break;
  145. case O_RTTL:
  146. info->check_set |= XT_RECENT_TTL;
  147. break;
  148. case O_RSOURCE:
  149. info->side = XT_RECENT_SOURCE;
  150. break;
  151. case O_RDEST:
  152. info->side = XT_RECENT_DEST;
  153. break;
  154. case O_REAP:
  155. info->check_set |= XT_RECENT_REAP;
  156. break;
  157. }
  158. }
  159. static void recent_check(struct xt_fcheck_call *cb)
  160. {
  161. if (!(cb->xflags & F_ANY_OP))
  162. xtables_error(PARAMETER_PROBLEM,
  163. "recent: you must specify one of `--set', `--rcheck' "
  164. "`--update' or `--remove'");
  165. }
  166. static void recent_print(const void *ip, const struct xt_entry_match *match,
  167. unsigned int family)
  168. {
  169. const struct xt_recent_mtinfo_v1 *info = (const void *)match->data;
  170. if (info->invert)
  171. printf(" !");
  172. printf(" recent:");
  173. if (info->check_set & XT_RECENT_SET)
  174. printf(" SET");
  175. if (info->check_set & XT_RECENT_CHECK)
  176. printf(" CHECK");
  177. if (info->check_set & XT_RECENT_UPDATE)
  178. printf(" UPDATE");
  179. if (info->check_set & XT_RECENT_REMOVE)
  180. printf(" REMOVE");
  181. if(info->seconds) printf(" seconds: %d", info->seconds);
  182. if (info->check_set & XT_RECENT_REAP)
  183. printf(" reap");
  184. if(info->hit_count) printf(" hit_count: %d", info->hit_count);
  185. if (info->check_set & XT_RECENT_TTL)
  186. printf(" TTL-Match");
  187. if(info->name) printf(" name: %s", info->name);
  188. if (info->side == XT_RECENT_SOURCE)
  189. printf(" side: source");
  190. if (info->side == XT_RECENT_DEST)
  191. printf(" side: dest");
  192. switch(family) {
  193. case NFPROTO_IPV4:
  194. printf(" mask: %s",
  195. xtables_ipaddr_to_numeric(&info->mask.in));
  196. break;
  197. case NFPROTO_IPV6:
  198. printf(" mask: %s",
  199. xtables_ip6addr_to_numeric(&info->mask.in6));
  200. break;
  201. }
  202. }
  203. static void recent_save(const void *ip, const struct xt_entry_match *match,
  204. unsigned int family)
  205. {
  206. const struct xt_recent_mtinfo_v1 *info = (const void *)match->data;
  207. if (info->invert)
  208. printf(" !");
  209. if (info->check_set & XT_RECENT_SET)
  210. printf(" --set");
  211. if (info->check_set & XT_RECENT_CHECK)
  212. printf(" --rcheck");
  213. if (info->check_set & XT_RECENT_UPDATE)
  214. printf(" --update");
  215. if (info->check_set & XT_RECENT_REMOVE)
  216. printf(" --remove");
  217. if(info->seconds) printf(" --seconds %d", info->seconds);
  218. if (info->check_set & XT_RECENT_REAP)
  219. printf(" --reap");
  220. if(info->hit_count) printf(" --hitcount %d", info->hit_count);
  221. if (info->check_set & XT_RECENT_TTL)
  222. printf(" --rttl");
  223. if(info->name) printf(" --name %s",info->name);
  224. switch(family) {
  225. case NFPROTO_IPV4:
  226. printf(" --mask %s",
  227. xtables_ipaddr_to_numeric(&info->mask.in));
  228. break;
  229. case NFPROTO_IPV6:
  230. printf(" --mask %s",
  231. xtables_ip6addr_to_numeric(&info->mask.in6));
  232. break;
  233. }
  234. if (info->side == XT_RECENT_SOURCE)
  235. printf(" --rsource");
  236. if (info->side == XT_RECENT_DEST)
  237. printf(" --rdest");
  238. }
  239. static void recent_init_v0(struct xt_entry_match *match)
  240. {
  241. recent_init(match, XT_RECENT_REV_0);
  242. }
  243. static void recent_init_v1(struct xt_entry_match *match)
  244. {
  245. recent_init(match, XT_RECENT_REV_1);
  246. }
  247. static void recent_save_v0(const void *ip, const struct xt_entry_match *match)
  248. {
  249. recent_save(ip, match, NFPROTO_UNSPEC);
  250. }
  251. static void recent_save_v4(const void *ip, const struct xt_entry_match *match)
  252. {
  253. recent_save(ip, match, NFPROTO_IPV4);
  254. }
  255. static void recent_save_v6(const void *ip, const struct xt_entry_match *match)
  256. {
  257. recent_save(ip, match, NFPROTO_IPV6);
  258. }
  259. static void recent_print_v0(const void *ip, const struct xt_entry_match *match,
  260. int numeric)
  261. {
  262. recent_print(ip, match, NFPROTO_UNSPEC);
  263. }
  264. static void recent_print_v4(const void *ip, const struct xt_entry_match *match,
  265. int numeric)
  266. {
  267. recent_print(ip, match, NFPROTO_IPV4);
  268. }
  269. static void recent_print_v6(const void *ip, const struct xt_entry_match *match,
  270. int numeric)
  271. {
  272. recent_print(ip, match, NFPROTO_IPV6);
  273. }
  274. static struct xtables_match recent_mt_reg[] = {
  275. {
  276. .name = "recent",
  277. .version = XTABLES_VERSION,
  278. .revision = 0,
  279. .family = NFPROTO_UNSPEC,
  280. .size = XT_ALIGN(sizeof(struct xt_recent_mtinfo)),
  281. .userspacesize = XT_ALIGN(sizeof(struct xt_recent_mtinfo)),
  282. .help = recent_help,
  283. .init = recent_init_v0,
  284. .x6_parse = recent_parse,
  285. .x6_fcheck = recent_check,
  286. .print = recent_print_v0,
  287. .save = recent_save_v0,
  288. .x6_options = recent_opts_v0,
  289. },
  290. {
  291. .name = "recent",
  292. .version = XTABLES_VERSION,
  293. .revision = 1,
  294. .family = NFPROTO_IPV4,
  295. .size = XT_ALIGN(sizeof(struct xt_recent_mtinfo_v1)),
  296. .userspacesize = XT_ALIGN(sizeof(struct xt_recent_mtinfo_v1)),
  297. .help = recent_help,
  298. .init = recent_init_v1,
  299. .x6_parse = recent_parse,
  300. .x6_fcheck = recent_check,
  301. .print = recent_print_v4,
  302. .save = recent_save_v4,
  303. .x6_options = recent_opts_v1,
  304. },
  305. {
  306. .name = "recent",
  307. .version = XTABLES_VERSION,
  308. .revision = 1,
  309. .family = NFPROTO_IPV6,
  310. .size = XT_ALIGN(sizeof(struct xt_recent_mtinfo_v1)),
  311. .userspacesize = XT_ALIGN(sizeof(struct xt_recent_mtinfo_v1)),
  312. .help = recent_help,
  313. .init = recent_init_v1,
  314. .x6_parse = recent_parse,
  315. .x6_fcheck = recent_check,
  316. .print = recent_print_v6,
  317. .save = recent_save_v6,
  318. .x6_options = recent_opts_v1,
  319. },
  320. };
  321. void _init(void)
  322. {
  323. xtables_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
  324. }