libxt_tcp.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* Shared library add-on to iptables to add TCP support. */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <netdb.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <getopt.h>
  8. #include <netinet/in.h>
  9. #include <xtables.h>
  10. #include <linux/netfilter/xt_tcpudp.h>
  11. static void tcp_help(void)
  12. {
  13. printf(
  14. "tcp match options:\n"
  15. "[!] --tcp-flags mask comp match when TCP flags & mask == comp\n"
  16. " (Flags: SYN ACK FIN RST URG PSH ALL NONE)\n"
  17. "[!] --syn match when only SYN flag set\n"
  18. " (equivalent to --tcp-flags SYN,RST,ACK,FIN SYN)\n"
  19. "[!] --source-port port[:port]\n"
  20. " --sport ...\n"
  21. " match source port(s)\n"
  22. "[!] --destination-port port[:port]\n"
  23. " --dport ...\n"
  24. " match destination port(s)\n"
  25. "[!] --tcp-option number match if TCP option set\n");
  26. }
  27. static const struct option tcp_opts[] = {
  28. {.name = "source-port", .has_arg = true, .val = '1'},
  29. {.name = "sport", .has_arg = true, .val = '1'}, /* synonym */
  30. {.name = "destination-port", .has_arg = true, .val = '2'},
  31. {.name = "dport", .has_arg = true, .val = '2'}, /* synonym */
  32. {.name = "syn", .has_arg = false, .val = '3'},
  33. {.name = "tcp-flags", .has_arg = true, .val = '4'},
  34. {.name = "tcp-option", .has_arg = true, .val = '5'},
  35. XT_GETOPT_TABLEEND,
  36. };
  37. static void
  38. parse_tcp_ports(const char *portstring, uint16_t *ports)
  39. {
  40. char *buffer;
  41. char *cp;
  42. buffer = strdup(portstring);
  43. if ((cp = strchr(buffer, ':')) == NULL)
  44. ports[0] = ports[1] = xtables_parse_port(buffer, "tcp");
  45. else {
  46. *cp = '\0';
  47. cp++;
  48. ports[0] = buffer[0] ? xtables_parse_port(buffer, "tcp") : 0;
  49. ports[1] = cp[0] ? xtables_parse_port(cp, "tcp") : 0xFFFF;
  50. if (ports[0] > ports[1])
  51. xtables_error(PARAMETER_PROBLEM,
  52. "invalid portrange (min > max)");
  53. }
  54. free(buffer);
  55. }
  56. struct tcp_flag_names {
  57. const char *name;
  58. unsigned int flag;
  59. };
  60. static const struct tcp_flag_names tcp_flag_names[]
  61. = { { "FIN", 0x01 },
  62. { "SYN", 0x02 },
  63. { "RST", 0x04 },
  64. { "PSH", 0x08 },
  65. { "ACK", 0x10 },
  66. { "URG", 0x20 },
  67. { "ALL", 0x3F },
  68. { "NONE", 0 },
  69. };
  70. static unsigned int
  71. parse_tcp_flag(const char *flags)
  72. {
  73. unsigned int ret = 0;
  74. char *ptr;
  75. char *buffer;
  76. buffer = strdup(flags);
  77. for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
  78. unsigned int i;
  79. for (i = 0; i < ARRAY_SIZE(tcp_flag_names); ++i)
  80. if (strcasecmp(tcp_flag_names[i].name, ptr) == 0) {
  81. ret |= tcp_flag_names[i].flag;
  82. break;
  83. }
  84. if (i == ARRAY_SIZE(tcp_flag_names))
  85. xtables_error(PARAMETER_PROBLEM,
  86. "Unknown TCP flag `%s'", ptr);
  87. }
  88. free(buffer);
  89. return ret;
  90. }
  91. static void
  92. parse_tcp_flags(struct xt_tcp *tcpinfo,
  93. const char *mask,
  94. const char *cmp,
  95. int invert)
  96. {
  97. tcpinfo->flg_mask = parse_tcp_flag(mask);
  98. tcpinfo->flg_cmp = parse_tcp_flag(cmp);
  99. if (invert)
  100. tcpinfo->invflags |= XT_TCP_INV_FLAGS;
  101. }
  102. static void
  103. parse_tcp_option(const char *option, uint8_t *result)
  104. {
  105. unsigned int ret;
  106. if (!xtables_strtoui(option, NULL, &ret, 1, UINT8_MAX))
  107. xtables_error(PARAMETER_PROBLEM, "Bad TCP option \"%s\"", option);
  108. *result = ret;
  109. }
  110. static void tcp_init(struct xt_entry_match *m)
  111. {
  112. struct xt_tcp *tcpinfo = (struct xt_tcp *)m->data;
  113. tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF;
  114. }
  115. #define TCP_SRC_PORTS 0x01
  116. #define TCP_DST_PORTS 0x02
  117. #define TCP_FLAGS 0x04
  118. #define TCP_OPTION 0x08
  119. static int
  120. tcp_parse(int c, char **argv, int invert, unsigned int *flags,
  121. const void *entry, struct xt_entry_match **match)
  122. {
  123. struct xt_tcp *tcpinfo = (struct xt_tcp *)(*match)->data;
  124. switch (c) {
  125. case '1':
  126. if (*flags & TCP_SRC_PORTS)
  127. xtables_error(PARAMETER_PROBLEM,
  128. "Only one `--source-port' allowed");
  129. parse_tcp_ports(optarg, tcpinfo->spts);
  130. if (invert)
  131. tcpinfo->invflags |= XT_TCP_INV_SRCPT;
  132. *flags |= TCP_SRC_PORTS;
  133. break;
  134. case '2':
  135. if (*flags & TCP_DST_PORTS)
  136. xtables_error(PARAMETER_PROBLEM,
  137. "Only one `--destination-port' allowed");
  138. parse_tcp_ports(optarg, tcpinfo->dpts);
  139. if (invert)
  140. tcpinfo->invflags |= XT_TCP_INV_DSTPT;
  141. *flags |= TCP_DST_PORTS;
  142. break;
  143. case '3':
  144. if (*flags & TCP_FLAGS)
  145. xtables_error(PARAMETER_PROBLEM,
  146. "Only one of `--syn' or `--tcp-flags' "
  147. " allowed");
  148. parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert);
  149. *flags |= TCP_FLAGS;
  150. break;
  151. case '4':
  152. if (*flags & TCP_FLAGS)
  153. xtables_error(PARAMETER_PROBLEM,
  154. "Only one of `--syn' or `--tcp-flags' "
  155. " allowed");
  156. if (!argv[optind]
  157. || argv[optind][0] == '-' || argv[optind][0] == '!')
  158. xtables_error(PARAMETER_PROBLEM,
  159. "--tcp-flags requires two args.");
  160. parse_tcp_flags(tcpinfo, optarg, argv[optind],
  161. invert);
  162. optind++;
  163. *flags |= TCP_FLAGS;
  164. break;
  165. case '5':
  166. if (*flags & TCP_OPTION)
  167. xtables_error(PARAMETER_PROBLEM,
  168. "Only one `--tcp-option' allowed");
  169. parse_tcp_option(optarg, &tcpinfo->option);
  170. if (invert)
  171. tcpinfo->invflags |= XT_TCP_INV_OPTION;
  172. *flags |= TCP_OPTION;
  173. break;
  174. }
  175. return 1;
  176. }
  177. static const char *
  178. port_to_service(int port)
  179. {
  180. const struct servent *service;
  181. if ((service = getservbyport(htons(port), "tcp")))
  182. return service->s_name;
  183. return NULL;
  184. }
  185. static void
  186. print_port(uint16_t port, int numeric)
  187. {
  188. const char *service;
  189. if (numeric || (service = port_to_service(port)) == NULL)
  190. printf("%u", port);
  191. else
  192. printf("%s", service);
  193. }
  194. static void
  195. print_ports(const char *name, uint16_t min, uint16_t max,
  196. int invert, int numeric)
  197. {
  198. const char *inv = invert ? "!" : "";
  199. if (min != 0 || max != 0xFFFF || invert) {
  200. printf(" %s", name);
  201. if (min == max) {
  202. printf(":%s", inv);
  203. print_port(min, numeric);
  204. } else {
  205. printf("s:%s", inv);
  206. print_port(min, numeric);
  207. printf(":");
  208. print_port(max, numeric);
  209. }
  210. }
  211. }
  212. static void
  213. print_option(uint8_t option, int invert, int numeric)
  214. {
  215. if (option || invert)
  216. printf(" option=%s%u", invert ? "!" : "", option);
  217. }
  218. static void
  219. print_tcpf(uint8_t flags)
  220. {
  221. int have_flag = 0;
  222. while (flags) {
  223. unsigned int i;
  224. for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++);
  225. if (have_flag)
  226. printf(",");
  227. printf("%s", tcp_flag_names[i].name);
  228. have_flag = 1;
  229. flags &= ~tcp_flag_names[i].flag;
  230. }
  231. if (!have_flag)
  232. printf("NONE");
  233. }
  234. static void
  235. print_flags(uint8_t mask, uint8_t cmp, int invert, int numeric)
  236. {
  237. if (mask || invert) {
  238. printf(" flags:%s", invert ? "!" : "");
  239. if (numeric)
  240. printf("0x%02X/0x%02X", mask, cmp);
  241. else {
  242. print_tcpf(mask);
  243. printf("/");
  244. print_tcpf(cmp);
  245. }
  246. }
  247. }
  248. static void
  249. tcp_print(const void *ip, const struct xt_entry_match *match, int numeric)
  250. {
  251. const struct xt_tcp *tcp = (struct xt_tcp *)match->data;
  252. printf(" tcp");
  253. print_ports("spt", tcp->spts[0], tcp->spts[1],
  254. tcp->invflags & XT_TCP_INV_SRCPT,
  255. numeric);
  256. print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
  257. tcp->invflags & XT_TCP_INV_DSTPT,
  258. numeric);
  259. print_option(tcp->option,
  260. tcp->invflags & XT_TCP_INV_OPTION,
  261. numeric);
  262. print_flags(tcp->flg_mask, tcp->flg_cmp,
  263. tcp->invflags & XT_TCP_INV_FLAGS,
  264. numeric);
  265. if (tcp->invflags & ~XT_TCP_INV_MASK)
  266. printf(" Unknown invflags: 0x%X",
  267. tcp->invflags & ~XT_TCP_INV_MASK);
  268. }
  269. static void tcp_save(const void *ip, const struct xt_entry_match *match)
  270. {
  271. const struct xt_tcp *tcpinfo = (struct xt_tcp *)match->data;
  272. if (tcpinfo->spts[0] != 0
  273. || tcpinfo->spts[1] != 0xFFFF) {
  274. if (tcpinfo->invflags & XT_TCP_INV_SRCPT)
  275. printf(" !");
  276. if (tcpinfo->spts[0]
  277. != tcpinfo->spts[1])
  278. printf(" --sport %u:%u",
  279. tcpinfo->spts[0],
  280. tcpinfo->spts[1]);
  281. else
  282. printf(" --sport %u",
  283. tcpinfo->spts[0]);
  284. }
  285. if (tcpinfo->dpts[0] != 0
  286. || tcpinfo->dpts[1] != 0xFFFF) {
  287. if (tcpinfo->invflags & XT_TCP_INV_DSTPT)
  288. printf(" !");
  289. if (tcpinfo->dpts[0]
  290. != tcpinfo->dpts[1])
  291. printf(" --dport %u:%u",
  292. tcpinfo->dpts[0],
  293. tcpinfo->dpts[1]);
  294. else
  295. printf(" --dport %u",
  296. tcpinfo->dpts[0]);
  297. }
  298. if (tcpinfo->option
  299. || (tcpinfo->invflags & XT_TCP_INV_OPTION)) {
  300. if (tcpinfo->invflags & XT_TCP_INV_OPTION)
  301. printf(" !");
  302. printf(" --tcp-option %u", tcpinfo->option);
  303. }
  304. if (tcpinfo->flg_mask
  305. || (tcpinfo->invflags & XT_TCP_INV_FLAGS)) {
  306. if (tcpinfo->invflags & XT_TCP_INV_FLAGS)
  307. printf(" !");
  308. printf(" --tcp-flags ");
  309. print_tcpf(tcpinfo->flg_mask);
  310. printf(" ");
  311. print_tcpf(tcpinfo->flg_cmp);
  312. }
  313. }
  314. static struct xtables_match tcp_match = {
  315. .family = NFPROTO_UNSPEC,
  316. .name = "tcp",
  317. .version = XTABLES_VERSION,
  318. .size = XT_ALIGN(sizeof(struct xt_tcp)),
  319. .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
  320. .help = tcp_help,
  321. .init = tcp_init,
  322. .parse = tcp_parse,
  323. .print = tcp_print,
  324. .save = tcp_save,
  325. .extra_opts = tcp_opts,
  326. };
  327. void
  328. _init(void)
  329. {
  330. xtables_register_match(&tcp_match);
  331. }