libxt_sctp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /* Shared library add-on to iptables for SCTP matching
  2. *
  3. * (C) 2003 by Harald Welte <laforge@gnumonks.org>
  4. *
  5. * This program is distributed under the terms of GNU GPL v2, 1991
  6. *
  7. * libipt_ecn.c borrowed heavily from libipt_dscp.c
  8. *
  9. */
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <getopt.h>
  15. #include <netdb.h>
  16. #include <ctype.h>
  17. #include <netinet/in.h>
  18. #include <xtables.h>
  19. #include <linux/netfilter/xt_sctp.h>
  20. #if 0
  21. #define DEBUGP(format, first...) printf(format, ##first)
  22. #define static
  23. #else
  24. #define DEBUGP(format, fist...)
  25. #endif
  26. static void
  27. print_chunk(uint32_t chunknum, int numeric);
  28. static void sctp_init(struct xt_entry_match *m)
  29. {
  30. int i;
  31. struct xt_sctp_info *einfo = (struct xt_sctp_info *)m->data;
  32. for (i = 0; i < XT_NUM_SCTP_FLAGS; i++) {
  33. einfo->flag_info[i].chunktype = -1;
  34. }
  35. }
  36. static void sctp_help(void)
  37. {
  38. printf(
  39. "sctp match options\n"
  40. "[!] --source-port port[:port] match source port(s)\n"
  41. " --sport ...\n"
  42. "[!] --destination-port port[:port] match destination port(s)\n"
  43. " --dport ...\n"
  44. "[!] --chunk-types (all|any|none) (chunktype[:flags])+ match if all, any or none of\n"
  45. " chunktypes are present\n"
  46. "chunktypes - DATA INIT INIT_ACK SACK HEARTBEAT HEARTBEAT_ACK ABORT SHUTDOWN SHUTDOWN_ACK ERROR COOKIE_ECHO COOKIE_ACK ECN_ECNE ECN_CWR SHUTDOWN_COMPLETE ASCONF ASCONF_ACK FORWARD_TSN ALL NONE\n");
  47. }
  48. static const struct option sctp_opts[] = {
  49. {.name = "source-port", .has_arg = true, .val = '1'},
  50. {.name = "sport", .has_arg = true, .val = '1'},
  51. {.name = "destination-port", .has_arg = true, .val = '2'},
  52. {.name = "dport", .has_arg = true, .val = '2'},
  53. {.name = "chunk-types", .has_arg = true, .val = '3'},
  54. XT_GETOPT_TABLEEND,
  55. };
  56. static void
  57. parse_sctp_ports(const char *portstring,
  58. uint16_t *ports)
  59. {
  60. char *buffer;
  61. char *cp;
  62. buffer = strdup(portstring);
  63. DEBUGP("%s\n", portstring);
  64. if ((cp = strchr(buffer, ':')) == NULL) {
  65. ports[0] = ports[1] = xtables_parse_port(buffer, "sctp");
  66. }
  67. else {
  68. *cp = '\0';
  69. cp++;
  70. ports[0] = buffer[0] ? xtables_parse_port(buffer, "sctp") : 0;
  71. ports[1] = cp[0] ? xtables_parse_port(cp, "sctp") : 0xFFFF;
  72. if (ports[0] > ports[1])
  73. xtables_error(PARAMETER_PROBLEM,
  74. "invalid portrange (min > max)");
  75. }
  76. free(buffer);
  77. }
  78. struct sctp_chunk_names {
  79. const char *name;
  80. unsigned int chunk_type;
  81. const char *valid_flags;
  82. };
  83. /*'ALL' and 'NONE' will be treated specially. */
  84. static const struct sctp_chunk_names sctp_chunk_names[]
  85. = { { .name = "DATA", .chunk_type = 0, .valid_flags = "----IUBE"},
  86. { .name = "INIT", .chunk_type = 1, .valid_flags = "--------"},
  87. { .name = "INIT_ACK", .chunk_type = 2, .valid_flags = "--------"},
  88. { .name = "SACK", .chunk_type = 3, .valid_flags = "--------"},
  89. { .name = "HEARTBEAT", .chunk_type = 4, .valid_flags = "--------"},
  90. { .name = "HEARTBEAT_ACK", .chunk_type = 5, .valid_flags = "--------"},
  91. { .name = "ABORT", .chunk_type = 6, .valid_flags = "-------T"},
  92. { .name = "SHUTDOWN", .chunk_type = 7, .valid_flags = "--------"},
  93. { .name = "SHUTDOWN_ACK", .chunk_type = 8, .valid_flags = "--------"},
  94. { .name = "ERROR", .chunk_type = 9, .valid_flags = "--------"},
  95. { .name = "COOKIE_ECHO", .chunk_type = 10, .valid_flags = "--------"},
  96. { .name = "COOKIE_ACK", .chunk_type = 11, .valid_flags = "--------"},
  97. { .name = "ECN_ECNE", .chunk_type = 12, .valid_flags = "--------"},
  98. { .name = "ECN_CWR", .chunk_type = 13, .valid_flags = "--------"},
  99. { .name = "SHUTDOWN_COMPLETE", .chunk_type = 14, .valid_flags = "-------T"},
  100. { .name = "ASCONF", .chunk_type = 193, .valid_flags = "--------"},
  101. { .name = "ASCONF_ACK", .chunk_type = 128, .valid_flags = "--------"},
  102. { .name = "FORWARD_TSN", .chunk_type = 192, .valid_flags = "--------"},
  103. };
  104. static void
  105. save_chunk_flag_info(struct xt_sctp_flag_info *flag_info,
  106. int *flag_count,
  107. int chunktype,
  108. int bit,
  109. int set)
  110. {
  111. int i;
  112. for (i = 0; i < *flag_count; i++) {
  113. if (flag_info[i].chunktype == chunktype) {
  114. DEBUGP("Previous match found\n");
  115. flag_info[i].chunktype = chunktype;
  116. flag_info[i].flag_mask |= (1 << bit);
  117. if (set) {
  118. flag_info[i].flag |= (1 << bit);
  119. }
  120. return;
  121. }
  122. }
  123. if (*flag_count == XT_NUM_SCTP_FLAGS) {
  124. xtables_error (PARAMETER_PROBLEM,
  125. "Number of chunk types with flags exceeds currently allowed limit."
  126. "Increasing this limit involves changing IPT_NUM_SCTP_FLAGS and"
  127. "recompiling both the kernel space and user space modules\n");
  128. }
  129. flag_info[*flag_count].chunktype = chunktype;
  130. flag_info[*flag_count].flag_mask |= (1 << bit);
  131. if (set) {
  132. flag_info[*flag_count].flag |= (1 << bit);
  133. }
  134. (*flag_count)++;
  135. }
  136. static void
  137. parse_sctp_chunk(struct xt_sctp_info *einfo,
  138. const char *chunks)
  139. {
  140. char *ptr;
  141. char *buffer;
  142. unsigned int i, j;
  143. int found = 0;
  144. char *chunk_flags;
  145. buffer = strdup(chunks);
  146. DEBUGP("Buffer: %s\n", buffer);
  147. SCTP_CHUNKMAP_RESET(einfo->chunkmap);
  148. if (!strcasecmp(buffer, "ALL")) {
  149. SCTP_CHUNKMAP_SET_ALL(einfo->chunkmap);
  150. goto out;
  151. }
  152. if (!strcasecmp(buffer, "NONE")) {
  153. SCTP_CHUNKMAP_RESET(einfo->chunkmap);
  154. goto out;
  155. }
  156. for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
  157. found = 0;
  158. DEBUGP("Next Chunk type %s\n", ptr);
  159. if ((chunk_flags = strchr(ptr, ':')) != NULL) {
  160. *chunk_flags++ = 0;
  161. }
  162. for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); ++i)
  163. if (strcasecmp(sctp_chunk_names[i].name, ptr) == 0) {
  164. DEBUGP("Chunk num %d\n", sctp_chunk_names[i].chunk_type);
  165. SCTP_CHUNKMAP_SET(einfo->chunkmap,
  166. sctp_chunk_names[i].chunk_type);
  167. found = 1;
  168. break;
  169. }
  170. if (!found)
  171. xtables_error(PARAMETER_PROBLEM,
  172. "Unknown sctp chunk `%s'", ptr);
  173. if (chunk_flags) {
  174. DEBUGP("Chunk flags %s\n", chunk_flags);
  175. for (j = 0; j < strlen(chunk_flags); j++) {
  176. char *p;
  177. int bit;
  178. if ((p = strchr(sctp_chunk_names[i].valid_flags,
  179. toupper(chunk_flags[j]))) != NULL) {
  180. bit = p - sctp_chunk_names[i].valid_flags;
  181. bit = 7 - bit;
  182. save_chunk_flag_info(einfo->flag_info,
  183. &(einfo->flag_count), i, bit,
  184. isupper(chunk_flags[j]));
  185. } else {
  186. xtables_error(PARAMETER_PROBLEM,
  187. "Invalid flags for chunk type %d\n", i);
  188. }
  189. }
  190. }
  191. }
  192. out:
  193. free(buffer);
  194. }
  195. static void
  196. parse_sctp_chunks(struct xt_sctp_info *einfo,
  197. const char *match_type,
  198. const char *chunks)
  199. {
  200. DEBUGP("Match type: %s Chunks: %s\n", match_type, chunks);
  201. if (!strcasecmp(match_type, "ANY")) {
  202. einfo->chunk_match_type = SCTP_CHUNK_MATCH_ANY;
  203. } else if (!strcasecmp(match_type, "ALL")) {
  204. einfo->chunk_match_type = SCTP_CHUNK_MATCH_ALL;
  205. } else if (!strcasecmp(match_type, "ONLY")) {
  206. einfo->chunk_match_type = SCTP_CHUNK_MATCH_ONLY;
  207. } else {
  208. xtables_error (PARAMETER_PROBLEM,
  209. "Match type has to be one of \"ALL\", \"ANY\" or \"ONLY\"");
  210. }
  211. SCTP_CHUNKMAP_RESET(einfo->chunkmap);
  212. parse_sctp_chunk(einfo, chunks);
  213. }
  214. static int
  215. sctp_parse(int c, char **argv, int invert, unsigned int *flags,
  216. const void *entry, struct xt_entry_match **match)
  217. {
  218. struct xt_sctp_info *einfo
  219. = (struct xt_sctp_info *)(*match)->data;
  220. switch (c) {
  221. case '1':
  222. if (*flags & XT_SCTP_SRC_PORTS)
  223. xtables_error(PARAMETER_PROBLEM,
  224. "Only one `--source-port' allowed");
  225. einfo->flags |= XT_SCTP_SRC_PORTS;
  226. parse_sctp_ports(optarg, einfo->spts);
  227. if (invert)
  228. einfo->invflags |= XT_SCTP_SRC_PORTS;
  229. *flags |= XT_SCTP_SRC_PORTS;
  230. break;
  231. case '2':
  232. if (*flags & XT_SCTP_DEST_PORTS)
  233. xtables_error(PARAMETER_PROBLEM,
  234. "Only one `--destination-port' allowed");
  235. einfo->flags |= XT_SCTP_DEST_PORTS;
  236. parse_sctp_ports(optarg, einfo->dpts);
  237. if (invert)
  238. einfo->invflags |= XT_SCTP_DEST_PORTS;
  239. *flags |= XT_SCTP_DEST_PORTS;
  240. break;
  241. case '3':
  242. if (*flags & XT_SCTP_CHUNK_TYPES)
  243. xtables_error(PARAMETER_PROBLEM,
  244. "Only one `--chunk-types' allowed");
  245. if (!argv[optind]
  246. || argv[optind][0] == '-' || argv[optind][0] == '!')
  247. xtables_error(PARAMETER_PROBLEM,
  248. "--chunk-types requires two args");
  249. einfo->flags |= XT_SCTP_CHUNK_TYPES;
  250. parse_sctp_chunks(einfo, optarg, argv[optind]);
  251. if (invert)
  252. einfo->invflags |= XT_SCTP_CHUNK_TYPES;
  253. optind++;
  254. *flags |= XT_SCTP_CHUNK_TYPES;
  255. break;
  256. }
  257. return 1;
  258. }
  259. static const char *
  260. port_to_service(int port)
  261. {
  262. const struct servent *service;
  263. if ((service = getservbyport(htons(port), "sctp")))
  264. return service->s_name;
  265. return NULL;
  266. }
  267. static void
  268. print_port(uint16_t port, int numeric)
  269. {
  270. const char *service;
  271. if (numeric || (service = port_to_service(port)) == NULL)
  272. printf("%u", port);
  273. else
  274. printf("%s", service);
  275. }
  276. static void
  277. print_ports(const char *name, uint16_t min, uint16_t max,
  278. int invert, int numeric)
  279. {
  280. const char *inv = invert ? "!" : "";
  281. if (min != 0 || max != 0xFFFF || invert) {
  282. printf(" %s", name);
  283. if (min == max) {
  284. printf(":%s", inv);
  285. print_port(min, numeric);
  286. } else {
  287. printf("s:%s", inv);
  288. print_port(min, numeric);
  289. printf(":");
  290. print_port(max, numeric);
  291. }
  292. }
  293. }
  294. static void
  295. print_chunk_flags(uint32_t chunknum, uint8_t chunk_flags, uint8_t chunk_flags_mask)
  296. {
  297. int i;
  298. DEBUGP("type: %d\tflags: %x\tflag mask: %x\n", chunknum, chunk_flags,
  299. chunk_flags_mask);
  300. if (chunk_flags_mask) {
  301. printf(":");
  302. }
  303. for (i = 7; i >= 0; i--) {
  304. if (chunk_flags_mask & (1 << i)) {
  305. if (chunk_flags & (1 << i)) {
  306. printf("%c", sctp_chunk_names[chunknum].valid_flags[7-i]);
  307. } else {
  308. printf("%c", tolower(sctp_chunk_names[chunknum].valid_flags[7-i]));
  309. }
  310. }
  311. }
  312. }
  313. static void
  314. print_chunk(uint32_t chunknum, int numeric)
  315. {
  316. if (numeric) {
  317. printf("0x%04X", chunknum);
  318. }
  319. else {
  320. int i;
  321. for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); ++i)
  322. if (sctp_chunk_names[i].chunk_type == chunknum)
  323. printf("%s", sctp_chunk_names[chunknum].name);
  324. }
  325. }
  326. static void
  327. print_chunks(const struct xt_sctp_info *einfo, int numeric)
  328. {
  329. uint32_t chunk_match_type = einfo->chunk_match_type;
  330. const struct xt_sctp_flag_info *flag_info = einfo->flag_info;
  331. int flag_count = einfo->flag_count;
  332. int i, j;
  333. int flag;
  334. switch (chunk_match_type) {
  335. case SCTP_CHUNK_MATCH_ANY: printf(" any"); break;
  336. case SCTP_CHUNK_MATCH_ALL: printf(" all"); break;
  337. case SCTP_CHUNK_MATCH_ONLY: printf(" only"); break;
  338. default: printf("Never reach here\n"); break;
  339. }
  340. if (SCTP_CHUNKMAP_IS_CLEAR(einfo->chunkmap)) {
  341. printf(" NONE");
  342. goto out;
  343. }
  344. if (SCTP_CHUNKMAP_IS_ALL_SET(einfo->chunkmap)) {
  345. printf(" ALL");
  346. goto out;
  347. }
  348. flag = 0;
  349. for (i = 0; i < 256; i++) {
  350. if (SCTP_CHUNKMAP_IS_SET(einfo->chunkmap, i)) {
  351. if (flag)
  352. printf(",");
  353. else
  354. putchar(' ');
  355. flag = 1;
  356. print_chunk(i, numeric);
  357. for (j = 0; j < flag_count; j++) {
  358. if (flag_info[j].chunktype == i) {
  359. print_chunk_flags(i, flag_info[j].flag,
  360. flag_info[j].flag_mask);
  361. }
  362. }
  363. }
  364. }
  365. out:
  366. return;
  367. }
  368. static void
  369. sctp_print(const void *ip, const struct xt_entry_match *match, int numeric)
  370. {
  371. const struct xt_sctp_info *einfo =
  372. (const struct xt_sctp_info *)match->data;
  373. printf(" sctp");
  374. if (einfo->flags & XT_SCTP_SRC_PORTS) {
  375. print_ports("spt", einfo->spts[0], einfo->spts[1],
  376. einfo->invflags & XT_SCTP_SRC_PORTS,
  377. numeric);
  378. }
  379. if (einfo->flags & XT_SCTP_DEST_PORTS) {
  380. print_ports("dpt", einfo->dpts[0], einfo->dpts[1],
  381. einfo->invflags & XT_SCTP_DEST_PORTS,
  382. numeric);
  383. }
  384. if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
  385. /* FIXME: print_chunks() is used in save() where the printing of '!'
  386. s taken care of, so we need to do that here as well */
  387. if (einfo->invflags & XT_SCTP_CHUNK_TYPES) {
  388. printf(" !");
  389. }
  390. print_chunks(einfo, numeric);
  391. }
  392. }
  393. static void sctp_save(const void *ip, const struct xt_entry_match *match)
  394. {
  395. const struct xt_sctp_info *einfo =
  396. (const struct xt_sctp_info *)match->data;
  397. if (einfo->flags & XT_SCTP_SRC_PORTS) {
  398. if (einfo->invflags & XT_SCTP_SRC_PORTS)
  399. printf(" !");
  400. if (einfo->spts[0] != einfo->spts[1])
  401. printf(" --sport %u:%u",
  402. einfo->spts[0], einfo->spts[1]);
  403. else
  404. printf(" --sport %u", einfo->spts[0]);
  405. }
  406. if (einfo->flags & XT_SCTP_DEST_PORTS) {
  407. if (einfo->invflags & XT_SCTP_DEST_PORTS)
  408. printf(" !");
  409. if (einfo->dpts[0] != einfo->dpts[1])
  410. printf(" --dport %u:%u",
  411. einfo->dpts[0], einfo->dpts[1]);
  412. else
  413. printf(" --dport %u", einfo->dpts[0]);
  414. }
  415. if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
  416. if (einfo->invflags & XT_SCTP_CHUNK_TYPES)
  417. printf(" !");
  418. printf(" --chunk-types");
  419. print_chunks(einfo, 0);
  420. }
  421. }
  422. static struct xtables_match sctp_match = {
  423. .name = "sctp",
  424. .family = NFPROTO_UNSPEC,
  425. .version = XTABLES_VERSION,
  426. .size = XT_ALIGN(sizeof(struct xt_sctp_info)),
  427. .userspacesize = XT_ALIGN(sizeof(struct xt_sctp_info)),
  428. .help = sctp_help,
  429. .init = sctp_init,
  430. .parse = sctp_parse,
  431. .print = sctp_print,
  432. .save = sctp_save,
  433. .extra_opts = sctp_opts,
  434. };
  435. void _init(void)
  436. {
  437. xtables_register_match(&sctp_match);
  438. }