libxt_rateest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stddef.h>
  6. #include <getopt.h>
  7. #include <xtables.h>
  8. #include <linux/netfilter/xt_rateest.h>
  9. static void rateest_help(void)
  10. {
  11. printf(
  12. "rateest match options:\n"
  13. " --rateest1 name Rate estimator name\n"
  14. " --rateest2 name Rate estimator name\n"
  15. " --rateest-delta Compare difference(s) to given rate(s)\n"
  16. " --rateest-bps1 [bps] Compare bps\n"
  17. " --rateest-pps1 [pps] Compare pps\n"
  18. " --rateest-bps2 [bps] Compare bps\n"
  19. " --rateest-pps2 [pps] Compare pps\n"
  20. " [!] --rateest-lt Match if rate is less than given rate/estimator\n"
  21. " [!] --rateest-gt Match if rate is greater than given rate/estimator\n"
  22. " [!] --rateest-eq Match if rate is equal to given rate/estimator\n");
  23. }
  24. enum rateest_options {
  25. OPT_RATEEST1,
  26. OPT_RATEEST2,
  27. OPT_RATEEST_BPS1,
  28. OPT_RATEEST_PPS1,
  29. OPT_RATEEST_BPS2,
  30. OPT_RATEEST_PPS2,
  31. OPT_RATEEST_DELTA,
  32. OPT_RATEEST_LT,
  33. OPT_RATEEST_GT,
  34. OPT_RATEEST_EQ,
  35. };
  36. static const struct option rateest_opts[] = {
  37. {.name = "rateest1", .has_arg = true, .val = OPT_RATEEST1},
  38. {.name = "rateest", .has_arg = true, .val = OPT_RATEEST1}, /* alias for absolute mode */
  39. {.name = "rateest2", .has_arg = true, .val = OPT_RATEEST2},
  40. {.name = "rateest-bps1", .has_arg = false, .val = OPT_RATEEST_BPS1},
  41. {.name = "rateest-pps1", .has_arg = false, .val = OPT_RATEEST_PPS1},
  42. {.name = "rateest-bps2", .has_arg = false, .val = OPT_RATEEST_BPS2},
  43. {.name = "rateest-pps2", .has_arg = false, .val = OPT_RATEEST_PPS2},
  44. {.name = "rateest-bps", .has_arg = false, .val = OPT_RATEEST_BPS2}, /* alias for absolute mode */
  45. {.name = "rateest-pps", .has_arg = false, .val = OPT_RATEEST_PPS2}, /* alias for absolute mode */
  46. {.name = "rateest-delta", .has_arg = false, .val = OPT_RATEEST_DELTA},
  47. {.name = "rateest-lt", .has_arg = false, .val = OPT_RATEEST_LT},
  48. {.name = "rateest-gt", .has_arg = false, .val = OPT_RATEEST_GT},
  49. {.name = "rateest-eq", .has_arg = false, .val = OPT_RATEEST_EQ},
  50. XT_GETOPT_TABLEEND,
  51. };
  52. /* Copied from iproute. See http://physics.nist.gov/cuu/Units/binary.html */
  53. static const struct rate_suffix {
  54. const char *name;
  55. double scale;
  56. } suffixes[] = {
  57. { "bit", 1. },
  58. { "Kibit", 1024. },
  59. { "kbit", 1000. },
  60. { "Mibit", 1024.*1024. },
  61. { "mbit", 1000000. },
  62. { "Gibit", 1024.*1024.*1024. },
  63. { "gbit", 1000000000. },
  64. { "Tibit", 1024.*1024.*1024.*1024. },
  65. { "tbit", 1000000000000. },
  66. { "Bps", 8. },
  67. { "KiBps", 8.*1024. },
  68. { "KBps", 8000. },
  69. { "MiBps", 8.*1024*1024. },
  70. { "MBps", 8000000. },
  71. { "GiBps", 8.*1024.*1024.*1024. },
  72. { "GBps", 8000000000. },
  73. { "TiBps", 8.*1024.*1024.*1024.*1024. },
  74. { "TBps", 8000000000000. },
  75. {NULL},
  76. };
  77. static int
  78. rateest_get_rate(uint32_t *rate, const char *str)
  79. {
  80. char *p;
  81. double bps = strtod(str, &p);
  82. const struct rate_suffix *s;
  83. if (p == str)
  84. return -1;
  85. if (*p == '\0') {
  86. *rate = bps / 8.; /* assume bytes/sec */
  87. return 0;
  88. }
  89. for (s = suffixes; s->name; ++s) {
  90. if (strcasecmp(s->name, p) == 0) {
  91. *rate = (bps * s->scale) / 8.;
  92. return 0;
  93. }
  94. }
  95. return -1;
  96. }
  97. static int
  98. rateest_parse(int c, char **argv, int invert, unsigned int *flags,
  99. const void *entry, struct xt_entry_match **match)
  100. {
  101. struct xt_rateest_match_info *info = (void *)(*match)->data;
  102. unsigned int val;
  103. switch (c) {
  104. case OPT_RATEEST1:
  105. if (invert)
  106. xtables_error(PARAMETER_PROBLEM,
  107. "rateest: rateest can't be inverted");
  108. if (*flags & (1 << c))
  109. xtables_error(PARAMETER_PROBLEM,
  110. "rateest: can't specify --rateest1 twice");
  111. *flags |= 1 << c;
  112. strncpy(info->name1, optarg, sizeof(info->name1) - 1);
  113. break;
  114. case OPT_RATEEST2:
  115. if (invert)
  116. xtables_error(PARAMETER_PROBLEM,
  117. "rateest: rateest can't be inverted");
  118. if (*flags & (1 << c))
  119. xtables_error(PARAMETER_PROBLEM,
  120. "rateest: can't specify --rateest2 twice");
  121. *flags |= 1 << c;
  122. strncpy(info->name2, optarg, sizeof(info->name2) - 1);
  123. info->flags |= XT_RATEEST_MATCH_REL;
  124. break;
  125. case OPT_RATEEST_BPS1:
  126. if (invert)
  127. xtables_error(PARAMETER_PROBLEM,
  128. "rateest: rateest-bps can't be inverted");
  129. if (*flags & (1 << c))
  130. xtables_error(PARAMETER_PROBLEM,
  131. "rateest: can't specify --rateest-bps1 twice");
  132. *flags |= 1 << c;
  133. info->flags |= XT_RATEEST_MATCH_BPS;
  134. /* The rate is optional and only required in absolute mode */
  135. if (!argv[optind] || *argv[optind] == '-' || *argv[optind] == '!')
  136. break;
  137. if (rateest_get_rate(&info->bps1, argv[optind]) < 0)
  138. xtables_error(PARAMETER_PROBLEM,
  139. "rateest: could not parse rate `%s'",
  140. argv[optind]);
  141. optind++;
  142. break;
  143. case OPT_RATEEST_PPS1:
  144. if (invert)
  145. xtables_error(PARAMETER_PROBLEM,
  146. "rateest: rateest-pps can't be inverted");
  147. if (*flags & (1 << c))
  148. xtables_error(PARAMETER_PROBLEM,
  149. "rateest: can't specify --rateest-pps1 twice");
  150. *flags |= 1 << c;
  151. info->flags |= XT_RATEEST_MATCH_PPS;
  152. /* The rate is optional and only required in absolute mode */
  153. if (!argv[optind] || *argv[optind] == '-' || *argv[optind] == '!')
  154. break;
  155. if (!xtables_strtoui(argv[optind], NULL, &val, 0, UINT32_MAX))
  156. xtables_error(PARAMETER_PROBLEM,
  157. "rateest: could not parse pps `%s'",
  158. argv[optind]);
  159. info->pps1 = val;
  160. optind++;
  161. break;
  162. case OPT_RATEEST_BPS2:
  163. if (invert)
  164. xtables_error(PARAMETER_PROBLEM,
  165. "rateest: rateest-bps can't be inverted");
  166. if (*flags & (1 << c))
  167. xtables_error(PARAMETER_PROBLEM,
  168. "rateest: can't specify --rateest-bps2 twice");
  169. *flags |= 1 << c;
  170. info->flags |= XT_RATEEST_MATCH_BPS;
  171. /* The rate is optional and only required in absolute mode */
  172. if (!argv[optind] || *argv[optind] == '-' || *argv[optind] == '!')
  173. break;
  174. if (rateest_get_rate(&info->bps2, argv[optind]) < 0)
  175. xtables_error(PARAMETER_PROBLEM,
  176. "rateest: could not parse rate `%s'",
  177. argv[optind]);
  178. optind++;
  179. break;
  180. case OPT_RATEEST_PPS2:
  181. if (invert)
  182. xtables_error(PARAMETER_PROBLEM,
  183. "rateest: rateest-pps can't be inverted");
  184. if (*flags & (1 << c))
  185. xtables_error(PARAMETER_PROBLEM,
  186. "rateest: can't specify --rateest-pps2 twice");
  187. *flags |= 1 << c;
  188. info->flags |= XT_RATEEST_MATCH_PPS;
  189. /* The rate is optional and only required in absolute mode */
  190. if (!argv[optind] || *argv[optind] == '-' || *argv[optind] == '!')
  191. break;
  192. if (!xtables_strtoui(argv[optind], NULL, &val, 0, UINT32_MAX))
  193. xtables_error(PARAMETER_PROBLEM,
  194. "rateest: could not parse pps `%s'",
  195. argv[optind]);
  196. info->pps2 = val;
  197. optind++;
  198. break;
  199. case OPT_RATEEST_DELTA:
  200. if (invert)
  201. xtables_error(PARAMETER_PROBLEM,
  202. "rateest: rateest-delta can't be inverted");
  203. if (*flags & (1 << c))
  204. xtables_error(PARAMETER_PROBLEM,
  205. "rateest: can't specify --rateest-delta twice");
  206. *flags |= 1 << c;
  207. info->flags |= XT_RATEEST_MATCH_DELTA;
  208. break;
  209. case OPT_RATEEST_EQ:
  210. if (*flags & (1 << c))
  211. xtables_error(PARAMETER_PROBLEM,
  212. "rateest: can't specify lt/gt/eq twice");
  213. *flags |= 1 << c;
  214. info->mode = XT_RATEEST_MATCH_EQ;
  215. if (invert)
  216. info->flags |= XT_RATEEST_MATCH_INVERT;
  217. break;
  218. case OPT_RATEEST_LT:
  219. if (*flags & (1 << c))
  220. xtables_error(PARAMETER_PROBLEM,
  221. "rateest: can't specify lt/gt/eq twice");
  222. *flags |= 1 << c;
  223. info->mode = XT_RATEEST_MATCH_LT;
  224. if (invert)
  225. info->flags |= XT_RATEEST_MATCH_INVERT;
  226. break;
  227. case OPT_RATEEST_GT:
  228. if (*flags & (1 << c))
  229. xtables_error(PARAMETER_PROBLEM,
  230. "rateest: can't specify lt/gt/eq twice");
  231. *flags |= 1 << c;
  232. info->mode = XT_RATEEST_MATCH_GT;
  233. if (invert)
  234. info->flags |= XT_RATEEST_MATCH_INVERT;
  235. break;
  236. }
  237. return 1;
  238. }
  239. static void rateest_final_check(struct xt_fcheck_call *cb)
  240. {
  241. struct xt_rateest_match_info *info = cb->data;
  242. if (info == NULL)
  243. xtables_error(PARAMETER_PROBLEM, "rateest match: "
  244. "you need to specify some flags");
  245. if (!(info->flags & XT_RATEEST_MATCH_REL))
  246. info->flags |= XT_RATEEST_MATCH_ABS;
  247. }
  248. static void
  249. rateest_print_rate(uint32_t rate, int numeric)
  250. {
  251. double tmp = (double)rate*8;
  252. if (numeric)
  253. printf(" %u", rate);
  254. else if (tmp >= 1000.0*1000000.0)
  255. printf(" %.0fMbit", tmp/1000000.0);
  256. else if (tmp >= 1000.0 * 1000.0)
  257. printf(" %.0fKbit", tmp/1000.0);
  258. else
  259. printf(" %.0fbit", tmp);
  260. }
  261. static void
  262. rateest_print_mode(const struct xt_rateest_match_info *info,
  263. const char *prefix)
  264. {
  265. if (info->flags & XT_RATEEST_MATCH_INVERT)
  266. printf(" !");
  267. switch (info->mode) {
  268. case XT_RATEEST_MATCH_EQ:
  269. printf(" %seq", prefix);
  270. break;
  271. case XT_RATEEST_MATCH_LT:
  272. printf(" %slt", prefix);
  273. break;
  274. case XT_RATEEST_MATCH_GT:
  275. printf(" %sgt", prefix);
  276. break;
  277. default:
  278. exit(1);
  279. }
  280. }
  281. static void
  282. rateest_print(const void *ip, const struct xt_entry_match *match, int numeric)
  283. {
  284. const struct xt_rateest_match_info *info = (const void *)match->data;
  285. printf(" rateest match ");
  286. printf("%s", info->name1);
  287. if (info->flags & XT_RATEEST_MATCH_DELTA)
  288. printf(" delta");
  289. if (info->flags & XT_RATEEST_MATCH_BPS) {
  290. printf(" bps");
  291. if (info->flags & XT_RATEEST_MATCH_DELTA)
  292. rateest_print_rate(info->bps1, numeric);
  293. if (info->flags & XT_RATEEST_MATCH_ABS) {
  294. rateest_print_rate(info->bps2, numeric);
  295. rateest_print_mode(info, "");
  296. }
  297. }
  298. if (info->flags & XT_RATEEST_MATCH_PPS) {
  299. printf(" pps");
  300. if (info->flags & XT_RATEEST_MATCH_DELTA)
  301. printf(" %u", info->pps1);
  302. if (info->flags & XT_RATEEST_MATCH_ABS) {
  303. rateest_print_mode(info, "");
  304. printf(" %u", info->pps2);
  305. }
  306. }
  307. if (info->flags & XT_RATEEST_MATCH_REL) {
  308. rateest_print_mode(info, "");
  309. printf(" %s", info->name2);
  310. if (info->flags & XT_RATEEST_MATCH_BPS) {
  311. printf(" bps");
  312. if (info->flags & XT_RATEEST_MATCH_DELTA)
  313. rateest_print_rate(info->bps2, numeric);
  314. }
  315. if (info->flags & XT_RATEEST_MATCH_PPS) {
  316. printf(" pps");
  317. if (info->flags & XT_RATEEST_MATCH_DELTA)
  318. printf(" %u", info->pps2);
  319. }
  320. }
  321. }
  322. static void __rateest_save_rate(const struct xt_rateest_match_info *info,
  323. const char *name, uint32_t r1, uint32_t r2,
  324. int numeric)
  325. {
  326. if (info->flags & XT_RATEEST_MATCH_DELTA) {
  327. printf(" --rateest-%s1", name);
  328. rateest_print_rate(r1, numeric);
  329. rateest_print_mode(info, "--rateest-");
  330. printf(" --rateest-%s2", name);
  331. } else {
  332. rateest_print_mode(info, "--rateest-");
  333. printf(" --rateest-%s", name);
  334. }
  335. if (info->flags & (XT_RATEEST_MATCH_ABS|XT_RATEEST_MATCH_DELTA))
  336. rateest_print_rate(r2, numeric);
  337. }
  338. static void rateest_save_rates(const struct xt_rateest_match_info *info)
  339. {
  340. if (info->flags & XT_RATEEST_MATCH_BPS)
  341. __rateest_save_rate(info, "bps", info->bps1, info->bps2, 0);
  342. if (info->flags & XT_RATEEST_MATCH_PPS)
  343. __rateest_save_rate(info, "pps", info->pps1, info->pps2, 1);
  344. }
  345. static void
  346. rateest_save(const void *ip, const struct xt_entry_match *match)
  347. {
  348. const struct xt_rateest_match_info *info = (const void *)match->data;
  349. if (info->flags & XT_RATEEST_MATCH_DELTA)
  350. printf(" --rateest-delta");
  351. if (info->flags & XT_RATEEST_MATCH_REL) {
  352. printf(" --rateest1 %s", info->name1);
  353. rateest_save_rates(info);
  354. printf(" --rateest2 %s", info->name2);
  355. } else { /* XT_RATEEST_MATCH_ABS */
  356. printf(" --rateest %s", info->name1);
  357. rateest_save_rates(info);
  358. }
  359. }
  360. static struct xtables_match rateest_mt_reg = {
  361. .family = NFPROTO_UNSPEC,
  362. .name = "rateest",
  363. .version = XTABLES_VERSION,
  364. .size = XT_ALIGN(sizeof(struct xt_rateest_match_info)),
  365. .userspacesize = XT_ALIGN(offsetof(struct xt_rateest_match_info, est1)),
  366. .help = rateest_help,
  367. .parse = rateest_parse,
  368. .x6_fcheck = rateest_final_check,
  369. .print = rateest_print,
  370. .save = rateest_save,
  371. .extra_opts = rateest_opts,
  372. };
  373. void _init(void)
  374. {
  375. xtables_register_match(&rateest_mt_reg);
  376. }