libxt_dccp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* Shared library add-on to iptables for DCCP matching
  2. *
  3. * (C) 2005 by Harald Welte <laforge@netfilter.org>
  4. *
  5. * This program is distributed under the terms of GNU GPL v2, 1991
  6. *
  7. */
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <netdb.h>
  13. #include <arpa/inet.h>
  14. #include <xtables.h>
  15. #include <linux/dccp.h>
  16. #include <linux/netfilter/x_tables.h>
  17. #include <linux/netfilter/xt_dccp.h>
  18. #if 0
  19. #define DEBUGP(format, first...) printf(format, ##first)
  20. #define static
  21. #else
  22. #define DEBUGP(format, fist...)
  23. #endif
  24. enum {
  25. O_SOURCE_PORT = 0,
  26. O_DEST_PORT,
  27. O_DCCP_TYPES,
  28. O_DCCP_OPTION,
  29. };
  30. static void dccp_help(void)
  31. {
  32. printf(
  33. "dccp match options\n"
  34. "[!] --source-port port[:port] match source port(s)\n"
  35. " --sport ...\n"
  36. "[!] --destination-port port[:port] match destination port(s)\n"
  37. " --dport ...\n"
  38. "[!] --dccp-types type[,...] match when packet is one of the given types\n"
  39. "[!] --dccp-option option match if option (by number!) is set\n"
  40. );
  41. }
  42. #define s struct xt_dccp_info
  43. static const struct xt_option_entry dccp_opts[] = {
  44. {.name = "source-port", .id = O_SOURCE_PORT, .type = XTTYPE_PORTRC,
  45. .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, spts)},
  46. {.name = "sport", .id = O_SOURCE_PORT, .type = XTTYPE_PORTRC,
  47. .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, spts)},
  48. {.name = "destination-port", .id = O_DEST_PORT, .type = XTTYPE_PORTRC,
  49. .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, dpts)},
  50. {.name = "dport", .id = O_DEST_PORT, .type = XTTYPE_PORTRC,
  51. .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, dpts)},
  52. {.name = "dccp-types", .id = O_DCCP_TYPES, .type = XTTYPE_STRING,
  53. .flags = XTOPT_INVERT},
  54. {.name = "dccp-option", .id = O_DCCP_OPTION, .type = XTTYPE_UINT8,
  55. .min = 1, .max = UINT8_MAX, .flags = XTOPT_INVERT | XTOPT_PUT,
  56. XTOPT_POINTER(s, option)},
  57. XTOPT_TABLEEND,
  58. };
  59. #undef s
  60. static const char *const dccp_pkt_types[] = {
  61. [DCCP_PKT_REQUEST] = "REQUEST",
  62. [DCCP_PKT_RESPONSE] = "RESPONSE",
  63. [DCCP_PKT_DATA] = "DATA",
  64. [DCCP_PKT_ACK] = "ACK",
  65. [DCCP_PKT_DATAACK] = "DATAACK",
  66. [DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
  67. [DCCP_PKT_CLOSE] = "CLOSE",
  68. [DCCP_PKT_RESET] = "RESET",
  69. [DCCP_PKT_SYNC] = "SYNC",
  70. [DCCP_PKT_SYNCACK] = "SYNCACK",
  71. [DCCP_PKT_INVALID] = "INVALID",
  72. };
  73. static uint16_t
  74. parse_dccp_types(const char *typestring)
  75. {
  76. uint16_t typemask = 0;
  77. char *ptr, *buffer;
  78. buffer = strdup(typestring);
  79. for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
  80. unsigned int i;
  81. for (i = 0; i < ARRAY_SIZE(dccp_pkt_types); ++i)
  82. if (!strcasecmp(dccp_pkt_types[i], ptr)) {
  83. typemask |= (1 << i);
  84. break;
  85. }
  86. if (i == ARRAY_SIZE(dccp_pkt_types))
  87. xtables_error(PARAMETER_PROBLEM,
  88. "Unknown DCCP type `%s'", ptr);
  89. }
  90. free(buffer);
  91. return typemask;
  92. }
  93. static void dccp_parse(struct xt_option_call *cb)
  94. {
  95. struct xt_dccp_info *einfo = cb->data;
  96. xtables_option_parse(cb);
  97. switch (cb->entry->id) {
  98. case O_SOURCE_PORT:
  99. einfo->flags |= XT_DCCP_SRC_PORTS;
  100. if (cb->invert)
  101. einfo->invflags |= XT_DCCP_SRC_PORTS;
  102. break;
  103. case O_DEST_PORT:
  104. einfo->flags |= XT_DCCP_DEST_PORTS;
  105. if (cb->invert)
  106. einfo->invflags |= XT_DCCP_DEST_PORTS;
  107. break;
  108. case O_DCCP_TYPES:
  109. einfo->flags |= XT_DCCP_TYPE;
  110. einfo->typemask = parse_dccp_types(cb->arg);
  111. if (cb->invert)
  112. einfo->invflags |= XT_DCCP_TYPE;
  113. break;
  114. case O_DCCP_OPTION:
  115. einfo->flags |= XT_DCCP_OPTION;
  116. if (cb->invert)
  117. einfo->invflags |= XT_DCCP_OPTION;
  118. break;
  119. }
  120. }
  121. static const char *
  122. port_to_service(int port)
  123. {
  124. const struct servent *service;
  125. if ((service = getservbyport(htons(port), "dccp")))
  126. return service->s_name;
  127. return NULL;
  128. }
  129. static void
  130. print_port(uint16_t port, int numeric)
  131. {
  132. const char *service;
  133. if (numeric || (service = port_to_service(port)) == NULL)
  134. printf("%u", port);
  135. else
  136. printf("%s", service);
  137. }
  138. static void
  139. print_ports(const char *name, uint16_t min, uint16_t max,
  140. int invert, int numeric)
  141. {
  142. const char *inv = invert ? "!" : "";
  143. if (min != 0 || max != 0xFFFF || invert) {
  144. printf(" %s", name);
  145. if (min == max) {
  146. printf(":%s", inv);
  147. print_port(min, numeric);
  148. } else {
  149. printf("s:%s", inv);
  150. print_port(min, numeric);
  151. printf(":");
  152. print_port(max, numeric);
  153. }
  154. }
  155. }
  156. static void
  157. print_types(uint16_t types, int inverted, int numeric)
  158. {
  159. int have_type = 0;
  160. if (inverted)
  161. printf(" !");
  162. printf(" ");
  163. while (types) {
  164. unsigned int i;
  165. for (i = 0; !(types & (1 << i)); i++);
  166. if (have_type)
  167. printf(",");
  168. else
  169. have_type = 1;
  170. if (numeric)
  171. printf("%u", i);
  172. else
  173. printf("%s", dccp_pkt_types[i]);
  174. types &= ~(1 << i);
  175. }
  176. }
  177. static void
  178. print_option(uint8_t option, int invert, int numeric)
  179. {
  180. if (option || invert)
  181. printf(" option=%s%u", invert ? "!" : "", option);
  182. }
  183. static void
  184. dccp_print(const void *ip, const struct xt_entry_match *match, int numeric)
  185. {
  186. const struct xt_dccp_info *einfo =
  187. (const struct xt_dccp_info *)match->data;
  188. printf(" dccp");
  189. if (einfo->flags & XT_DCCP_SRC_PORTS) {
  190. print_ports("spt", einfo->spts[0], einfo->spts[1],
  191. einfo->invflags & XT_DCCP_SRC_PORTS,
  192. numeric);
  193. }
  194. if (einfo->flags & XT_DCCP_DEST_PORTS) {
  195. print_ports("dpt", einfo->dpts[0], einfo->dpts[1],
  196. einfo->invflags & XT_DCCP_DEST_PORTS,
  197. numeric);
  198. }
  199. if (einfo->flags & XT_DCCP_TYPE) {
  200. print_types(einfo->typemask,
  201. einfo->invflags & XT_DCCP_TYPE,
  202. numeric);
  203. }
  204. if (einfo->flags & XT_DCCP_OPTION) {
  205. print_option(einfo->option,
  206. einfo->invflags & XT_DCCP_OPTION, numeric);
  207. }
  208. }
  209. static void dccp_save(const void *ip, const struct xt_entry_match *match)
  210. {
  211. const struct xt_dccp_info *einfo =
  212. (const struct xt_dccp_info *)match->data;
  213. if (einfo->flags & XT_DCCP_SRC_PORTS) {
  214. if (einfo->invflags & XT_DCCP_SRC_PORTS)
  215. printf(" !");
  216. if (einfo->spts[0] != einfo->spts[1])
  217. printf(" --sport %u:%u",
  218. einfo->spts[0], einfo->spts[1]);
  219. else
  220. printf(" --sport %u", einfo->spts[0]);
  221. }
  222. if (einfo->flags & XT_DCCP_DEST_PORTS) {
  223. if (einfo->invflags & XT_DCCP_DEST_PORTS)
  224. printf(" !");
  225. if (einfo->dpts[0] != einfo->dpts[1])
  226. printf(" --dport %u:%u",
  227. einfo->dpts[0], einfo->dpts[1]);
  228. else
  229. printf(" --dport %u", einfo->dpts[0]);
  230. }
  231. if (einfo->flags & XT_DCCP_TYPE) {
  232. printf("%s --dccp-types",
  233. einfo->invflags & XT_DCCP_TYPE ? " !" : "");
  234. print_types(einfo->typemask, false, 0);
  235. }
  236. if (einfo->flags & XT_DCCP_OPTION) {
  237. printf("%s --dccp-option %u",
  238. einfo->invflags & XT_DCCP_OPTION ? " !" : "",
  239. einfo->option);
  240. }
  241. }
  242. static struct xtables_match dccp_match = {
  243. .name = "dccp",
  244. .family = NFPROTO_UNSPEC,
  245. .version = XTABLES_VERSION,
  246. .size = XT_ALIGN(sizeof(struct xt_dccp_info)),
  247. .userspacesize = XT_ALIGN(sizeof(struct xt_dccp_info)),
  248. .help = dccp_help,
  249. .print = dccp_print,
  250. .save = dccp_save,
  251. .x6_parse = dccp_parse,
  252. .x6_options = dccp_opts,
  253. };
  254. void _init(void)
  255. {
  256. xtables_register_match(&dccp_match);
  257. }