libxt_conntrack.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /*
  2. * libxt_conntrack
  3. * Shared library add-on to iptables for conntrack matching support.
  4. *
  5. * GPL (C) 2001 Marc Boucher (marc@mbsi.ca).
  6. * Copyright © CC Computer Consultants GmbH, 2007 - 2008
  7. * Jan Engelhardt <jengelh@computergmbh.de>
  8. */
  9. #include <stdbool.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <xtables.h>
  15. #include <linux/netfilter/xt_conntrack.h>
  16. #include <linux/netfilter/xt_state.h>
  17. #include <linux/netfilter/nf_conntrack_common.h>
  18. #ifndef XT_STATE_UNTRACKED
  19. #define XT_STATE_UNTRACKED (1 << (IP_CT_NUMBER + 1))
  20. #endif
  21. struct ip_conntrack_old_tuple {
  22. struct {
  23. __be32 ip;
  24. union {
  25. __u16 all;
  26. } u;
  27. } src;
  28. struct {
  29. __be32 ip;
  30. union {
  31. __u16 all;
  32. } u;
  33. /* The protocol. */
  34. __u16 protonum;
  35. } dst;
  36. };
  37. struct xt_conntrack_info {
  38. unsigned int statemask, statusmask;
  39. struct ip_conntrack_old_tuple tuple[IP_CT_DIR_MAX];
  40. struct in_addr sipmsk[IP_CT_DIR_MAX], dipmsk[IP_CT_DIR_MAX];
  41. unsigned long expires_min, expires_max;
  42. /* Flags word */
  43. uint8_t flags;
  44. /* Inverse flags */
  45. uint8_t invflags;
  46. };
  47. enum {
  48. O_CTSTATE = 0,
  49. O_CTPROTO,
  50. O_CTORIGSRC,
  51. O_CTORIGDST,
  52. O_CTREPLSRC,
  53. O_CTREPLDST,
  54. O_CTORIGSRCPORT,
  55. O_CTORIGDSTPORT,
  56. O_CTREPLSRCPORT,
  57. O_CTREPLDSTPORT,
  58. O_CTSTATUS,
  59. O_CTEXPIRE,
  60. O_CTDIR,
  61. };
  62. static void conntrack_mt_help(void)
  63. {
  64. printf(
  65. "conntrack match options:\n"
  66. "[!] --ctstate {INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED|SNAT|DNAT}[,...]\n"
  67. " State(s) to match\n"
  68. "[!] --ctproto proto Protocol to match; by number or name, e.g. \"tcp\"\n"
  69. "[!] --ctorigsrc address[/mask]\n"
  70. "[!] --ctorigdst address[/mask]\n"
  71. "[!] --ctreplsrc address[/mask]\n"
  72. "[!] --ctrepldst address[/mask]\n"
  73. " Original/Reply source/destination address\n"
  74. "[!] --ctorigsrcport port\n"
  75. "[!] --ctorigdstport port\n"
  76. "[!] --ctreplsrcport port\n"
  77. "[!] --ctrepldstport port\n"
  78. " TCP/UDP/SCTP orig./reply source/destination port\n"
  79. "[!] --ctstatus {NONE|EXPECTED|SEEN_REPLY|ASSURED|CONFIRMED}[,...]\n"
  80. " Status(es) to match\n"
  81. "[!] --ctexpire time[:time] Match remaining lifetime in seconds against\n"
  82. " value or range of values (inclusive)\n"
  83. " --ctdir {ORIGINAL|REPLY} Flow direction of packet\n");
  84. }
  85. #define s struct xt_conntrack_info /* for v0 */
  86. static const struct xt_option_entry conntrack_mt_opts_v0[] = {
  87. {.name = "ctstate", .id = O_CTSTATE, .type = XTTYPE_STRING,
  88. .flags = XTOPT_INVERT},
  89. {.name = "ctproto", .id = O_CTPROTO, .type = XTTYPE_PROTOCOL,
  90. .flags = XTOPT_INVERT},
  91. {.name = "ctorigsrc", .id = O_CTORIGSRC, .type = XTTYPE_HOST,
  92. .flags = XTOPT_INVERT},
  93. {.name = "ctorigdst", .id = O_CTORIGDST, .type = XTTYPE_HOST,
  94. .flags = XTOPT_INVERT},
  95. {.name = "ctreplsrc", .id = O_CTREPLSRC, .type = XTTYPE_HOST,
  96. .flags = XTOPT_INVERT},
  97. {.name = "ctrepldst", .id = O_CTREPLDST, .type = XTTYPE_HOST,
  98. .flags = XTOPT_INVERT},
  99. {.name = "ctstatus", .id = O_CTSTATUS, .type = XTTYPE_STRING,
  100. .flags = XTOPT_INVERT},
  101. {.name = "ctexpire", .id = O_CTEXPIRE, .type = XTTYPE_UINT32RC,
  102. .flags = XTOPT_INVERT},
  103. XTOPT_TABLEEND,
  104. };
  105. #undef s
  106. #define s struct xt_conntrack_mtinfo2
  107. /* We exploit the fact that v1-v2 share the same xt_o_e layout */
  108. static const struct xt_option_entry conntrack2_mt_opts[] = {
  109. {.name = "ctstate", .id = O_CTSTATE, .type = XTTYPE_STRING,
  110. .flags = XTOPT_INVERT},
  111. {.name = "ctproto", .id = O_CTPROTO, .type = XTTYPE_PROTOCOL,
  112. .flags = XTOPT_INVERT},
  113. {.name = "ctorigsrc", .id = O_CTORIGSRC, .type = XTTYPE_HOSTMASK,
  114. .flags = XTOPT_INVERT},
  115. {.name = "ctorigdst", .id = O_CTORIGDST, .type = XTTYPE_HOSTMASK,
  116. .flags = XTOPT_INVERT},
  117. {.name = "ctreplsrc", .id = O_CTREPLSRC, .type = XTTYPE_HOSTMASK,
  118. .flags = XTOPT_INVERT},
  119. {.name = "ctrepldst", .id = O_CTREPLDST, .type = XTTYPE_HOSTMASK,
  120. .flags = XTOPT_INVERT},
  121. {.name = "ctstatus", .id = O_CTSTATUS, .type = XTTYPE_STRING,
  122. .flags = XTOPT_INVERT},
  123. {.name = "ctexpire", .id = O_CTEXPIRE, .type = XTTYPE_UINT32RC,
  124. .flags = XTOPT_INVERT},
  125. /*
  126. * Rev 1 and 2 only store one port, and we would normally use
  127. * %XTTYPE_PORT (rather than %XTTYPE_PORTRC) for that. The resulting
  128. * error message - in case a user passed a range nevertheless -
  129. * "port 22:23 resolved to nothing" is not quite as useful as using
  130. * %XTTYPE_PORTC and libxt_conntrack's own range test.
  131. */
  132. {.name = "ctorigsrcport", .id = O_CTORIGSRCPORT, .type = XTTYPE_PORTRC,
  133. .flags = XTOPT_INVERT | XTOPT_NBO},
  134. {.name = "ctorigdstport", .id = O_CTORIGDSTPORT, .type = XTTYPE_PORTRC,
  135. .flags = XTOPT_INVERT | XTOPT_NBO},
  136. {.name = "ctreplsrcport", .id = O_CTREPLSRCPORT, .type = XTTYPE_PORTRC,
  137. .flags = XTOPT_INVERT | XTOPT_NBO},
  138. {.name = "ctrepldstport", .id = O_CTREPLDSTPORT, .type = XTTYPE_PORTRC,
  139. .flags = XTOPT_INVERT | XTOPT_NBO},
  140. {.name = "ctdir", .id = O_CTDIR, .type = XTTYPE_STRING},
  141. XTOPT_TABLEEND,
  142. };
  143. #undef s
  144. #define s struct xt_conntrack_mtinfo3
  145. /* Difference from v2 is the non-NBO form. */
  146. static const struct xt_option_entry conntrack3_mt_opts[] = {
  147. {.name = "ctstate", .id = O_CTSTATE, .type = XTTYPE_STRING,
  148. .flags = XTOPT_INVERT},
  149. {.name = "ctproto", .id = O_CTPROTO, .type = XTTYPE_PROTOCOL,
  150. .flags = XTOPT_INVERT},
  151. {.name = "ctorigsrc", .id = O_CTORIGSRC, .type = XTTYPE_HOSTMASK,
  152. .flags = XTOPT_INVERT},
  153. {.name = "ctorigdst", .id = O_CTORIGDST, .type = XTTYPE_HOSTMASK,
  154. .flags = XTOPT_INVERT},
  155. {.name = "ctreplsrc", .id = O_CTREPLSRC, .type = XTTYPE_HOSTMASK,
  156. .flags = XTOPT_INVERT},
  157. {.name = "ctrepldst", .id = O_CTREPLDST, .type = XTTYPE_HOSTMASK,
  158. .flags = XTOPT_INVERT},
  159. {.name = "ctstatus", .id = O_CTSTATUS, .type = XTTYPE_STRING,
  160. .flags = XTOPT_INVERT},
  161. {.name = "ctexpire", .id = O_CTEXPIRE, .type = XTTYPE_UINT32RC,
  162. .flags = XTOPT_INVERT},
  163. {.name = "ctorigsrcport", .id = O_CTORIGSRCPORT, .type = XTTYPE_PORTRC,
  164. .flags = XTOPT_INVERT},
  165. {.name = "ctorigdstport", .id = O_CTORIGDSTPORT, .type = XTTYPE_PORTRC,
  166. .flags = XTOPT_INVERT},
  167. {.name = "ctreplsrcport", .id = O_CTREPLSRCPORT, .type = XTTYPE_PORTRC,
  168. .flags = XTOPT_INVERT},
  169. {.name = "ctrepldstport", .id = O_CTREPLDSTPORT, .type = XTTYPE_PORTRC,
  170. .flags = XTOPT_INVERT},
  171. {.name = "ctdir", .id = O_CTDIR, .type = XTTYPE_STRING},
  172. XTOPT_TABLEEND,
  173. };
  174. #undef s
  175. static int
  176. parse_state(const char *state, size_t len, struct xt_conntrack_info *sinfo)
  177. {
  178. if (strncasecmp(state, "INVALID", len) == 0)
  179. sinfo->statemask |= XT_CONNTRACK_STATE_INVALID;
  180. else if (strncasecmp(state, "NEW", len) == 0)
  181. sinfo->statemask |= XT_CONNTRACK_STATE_BIT(IP_CT_NEW);
  182. else if (strncasecmp(state, "ESTABLISHED", len) == 0)
  183. sinfo->statemask |= XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED);
  184. else if (strncasecmp(state, "RELATED", len) == 0)
  185. sinfo->statemask |= XT_CONNTRACK_STATE_BIT(IP_CT_RELATED);
  186. else if (strncasecmp(state, "UNTRACKED", len) == 0)
  187. sinfo->statemask |= XT_CONNTRACK_STATE_UNTRACKED;
  188. else if (strncasecmp(state, "SNAT", len) == 0)
  189. sinfo->statemask |= XT_CONNTRACK_STATE_SNAT;
  190. else if (strncasecmp(state, "DNAT", len) == 0)
  191. sinfo->statemask |= XT_CONNTRACK_STATE_DNAT;
  192. else
  193. return 0;
  194. return 1;
  195. }
  196. static void
  197. parse_states(const char *arg, struct xt_conntrack_info *sinfo)
  198. {
  199. const char *comma;
  200. while ((comma = strchr(arg, ',')) != NULL) {
  201. if (comma == arg || !parse_state(arg, comma-arg, sinfo))
  202. xtables_error(PARAMETER_PROBLEM, "Bad ctstate \"%s\"", arg);
  203. arg = comma+1;
  204. }
  205. if (!*arg)
  206. xtables_error(PARAMETER_PROBLEM, "\"--ctstate\" requires a list of "
  207. "states with no spaces, e.g. "
  208. "ESTABLISHED,RELATED");
  209. if (strlen(arg) == 0 || !parse_state(arg, strlen(arg), sinfo))
  210. xtables_error(PARAMETER_PROBLEM, "Bad ctstate \"%s\"", arg);
  211. }
  212. static bool
  213. conntrack_ps_state(struct xt_conntrack_mtinfo3 *info, const char *state,
  214. size_t z)
  215. {
  216. if (strncasecmp(state, "INVALID", z) == 0)
  217. info->state_mask |= XT_CONNTRACK_STATE_INVALID;
  218. else if (strncasecmp(state, "NEW", z) == 0)
  219. info->state_mask |= XT_CONNTRACK_STATE_BIT(IP_CT_NEW);
  220. else if (strncasecmp(state, "ESTABLISHED", z) == 0)
  221. info->state_mask |= XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED);
  222. else if (strncasecmp(state, "RELATED", z) == 0)
  223. info->state_mask |= XT_CONNTRACK_STATE_BIT(IP_CT_RELATED);
  224. else if (strncasecmp(state, "UNTRACKED", z) == 0)
  225. info->state_mask |= XT_CONNTRACK_STATE_UNTRACKED;
  226. else if (strncasecmp(state, "SNAT", z) == 0)
  227. info->state_mask |= XT_CONNTRACK_STATE_SNAT;
  228. else if (strncasecmp(state, "DNAT", z) == 0)
  229. info->state_mask |= XT_CONNTRACK_STATE_DNAT;
  230. else
  231. return false;
  232. return true;
  233. }
  234. static void
  235. conntrack_ps_states(struct xt_conntrack_mtinfo3 *info, const char *arg)
  236. {
  237. const char *comma;
  238. while ((comma = strchr(arg, ',')) != NULL) {
  239. if (comma == arg || !conntrack_ps_state(info, arg, comma - arg))
  240. xtables_error(PARAMETER_PROBLEM,
  241. "Bad ctstate \"%s\"", arg);
  242. arg = comma + 1;
  243. }
  244. if (strlen(arg) == 0 || !conntrack_ps_state(info, arg, strlen(arg)))
  245. xtables_error(PARAMETER_PROBLEM, "Bad ctstate \"%s\"", arg);
  246. }
  247. static int
  248. parse_status(const char *status, size_t len, struct xt_conntrack_info *sinfo)
  249. {
  250. if (strncasecmp(status, "NONE", len) == 0)
  251. sinfo->statusmask |= 0;
  252. else if (strncasecmp(status, "EXPECTED", len) == 0)
  253. sinfo->statusmask |= IPS_EXPECTED;
  254. else if (strncasecmp(status, "SEEN_REPLY", len) == 0)
  255. sinfo->statusmask |= IPS_SEEN_REPLY;
  256. else if (strncasecmp(status, "ASSURED", len) == 0)
  257. sinfo->statusmask |= IPS_ASSURED;
  258. #ifdef IPS_CONFIRMED
  259. else if (strncasecmp(status, "CONFIRMED", len) == 0)
  260. sinfo->statusmask |= IPS_CONFIRMED;
  261. #endif
  262. else
  263. return 0;
  264. return 1;
  265. }
  266. static void
  267. parse_statuses(const char *arg, struct xt_conntrack_info *sinfo)
  268. {
  269. const char *comma;
  270. while ((comma = strchr(arg, ',')) != NULL) {
  271. if (comma == arg || !parse_status(arg, comma-arg, sinfo))
  272. xtables_error(PARAMETER_PROBLEM, "Bad ctstatus \"%s\"", arg);
  273. arg = comma+1;
  274. }
  275. if (strlen(arg) == 0 || !parse_status(arg, strlen(arg), sinfo))
  276. xtables_error(PARAMETER_PROBLEM, "Bad ctstatus \"%s\"", arg);
  277. }
  278. static bool
  279. conntrack_ps_status(struct xt_conntrack_mtinfo3 *info, const char *status,
  280. size_t z)
  281. {
  282. if (strncasecmp(status, "NONE", z) == 0)
  283. info->status_mask |= 0;
  284. else if (strncasecmp(status, "EXPECTED", z) == 0)
  285. info->status_mask |= IPS_EXPECTED;
  286. else if (strncasecmp(status, "SEEN_REPLY", z) == 0)
  287. info->status_mask |= IPS_SEEN_REPLY;
  288. else if (strncasecmp(status, "ASSURED", z) == 0)
  289. info->status_mask |= IPS_ASSURED;
  290. else if (strncasecmp(status, "CONFIRMED", z) == 0)
  291. info->status_mask |= IPS_CONFIRMED;
  292. else
  293. return false;
  294. return true;
  295. }
  296. static void
  297. conntrack_ps_statuses(struct xt_conntrack_mtinfo3 *info, const char *arg)
  298. {
  299. const char *comma;
  300. while ((comma = strchr(arg, ',')) != NULL) {
  301. if (comma == arg || !conntrack_ps_status(info, arg, comma - arg))
  302. xtables_error(PARAMETER_PROBLEM,
  303. "Bad ctstatus \"%s\"", arg);
  304. arg = comma + 1;
  305. }
  306. if (strlen(arg) == 0 || !conntrack_ps_status(info, arg, strlen(arg)))
  307. xtables_error(PARAMETER_PROBLEM, "Bad ctstatus \"%s\"", arg);
  308. }
  309. static void conntrack_parse(struct xt_option_call *cb)
  310. {
  311. struct xt_conntrack_info *sinfo = cb->data;
  312. xtables_option_parse(cb);
  313. switch (cb->entry->id) {
  314. case O_CTSTATE:
  315. parse_states(cb->arg, sinfo);
  316. if (cb->invert)
  317. sinfo->invflags |= XT_CONNTRACK_STATE;
  318. break;
  319. case O_CTPROTO:
  320. sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum = cb->val.protocol;
  321. if (cb->invert)
  322. sinfo->invflags |= XT_CONNTRACK_PROTO;
  323. if (sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum == 0
  324. && (sinfo->invflags & XT_INV_PROTO))
  325. xtables_error(PARAMETER_PROBLEM,
  326. "rule would never match protocol");
  327. sinfo->flags |= XT_CONNTRACK_PROTO;
  328. break;
  329. case O_CTORIGSRC:
  330. if (cb->invert)
  331. sinfo->invflags |= XT_CONNTRACK_ORIGSRC;
  332. sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip = cb->val.haddr.ip;
  333. sinfo->flags |= XT_CONNTRACK_ORIGSRC;
  334. break;
  335. case O_CTORIGDST:
  336. if (cb->invert)
  337. sinfo->invflags |= XT_CONNTRACK_ORIGDST;
  338. sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip = cb->val.haddr.ip;
  339. sinfo->flags |= XT_CONNTRACK_ORIGDST;
  340. break;
  341. case O_CTREPLSRC:
  342. if (cb->invert)
  343. sinfo->invflags |= XT_CONNTRACK_REPLSRC;
  344. sinfo->tuple[IP_CT_DIR_REPLY].src.ip = cb->val.haddr.ip;
  345. sinfo->flags |= XT_CONNTRACK_REPLSRC;
  346. break;
  347. case O_CTREPLDST:
  348. if (cb->invert)
  349. sinfo->invflags |= XT_CONNTRACK_REPLDST;
  350. sinfo->tuple[IP_CT_DIR_REPLY].dst.ip = cb->val.haddr.ip;
  351. sinfo->flags |= XT_CONNTRACK_REPLDST;
  352. break;
  353. case O_CTSTATUS:
  354. parse_statuses(cb->arg, sinfo);
  355. if (cb->invert)
  356. sinfo->invflags |= XT_CONNTRACK_STATUS;
  357. sinfo->flags |= XT_CONNTRACK_STATUS;
  358. break;
  359. case O_CTEXPIRE:
  360. sinfo->expires_min = cb->val.u32_range[0];
  361. sinfo->expires_max = cb->val.u32_range[0];
  362. if (cb->nvals >= 2)
  363. sinfo->expires_max = cb->val.u32_range[1];
  364. if (cb->invert)
  365. sinfo->invflags |= XT_CONNTRACK_EXPIRES;
  366. sinfo->flags |= XT_CONNTRACK_EXPIRES;
  367. break;
  368. }
  369. }
  370. static void conntrack_mt_parse(struct xt_option_call *cb, uint8_t rev)
  371. {
  372. struct xt_conntrack_mtinfo3 *info = cb->data;
  373. xtables_option_parse(cb);
  374. switch (cb->entry->id) {
  375. case O_CTSTATE:
  376. conntrack_ps_states(info, cb->arg);
  377. info->match_flags |= XT_CONNTRACK_STATE;
  378. if (cb->invert)
  379. info->invert_flags |= XT_CONNTRACK_STATE;
  380. break;
  381. case O_CTPROTO:
  382. info->l4proto = cb->val.protocol;
  383. if (info->l4proto == 0 && (info->invert_flags & XT_INV_PROTO))
  384. xtables_error(PARAMETER_PROBLEM, "conntrack: rule would "
  385. "never match protocol");
  386. info->match_flags |= XT_CONNTRACK_PROTO;
  387. if (cb->invert)
  388. info->invert_flags |= XT_CONNTRACK_PROTO;
  389. break;
  390. case O_CTORIGSRC:
  391. info->origsrc_addr = cb->val.haddr;
  392. info->origsrc_mask = cb->val.hmask;
  393. info->match_flags |= XT_CONNTRACK_ORIGSRC;
  394. if (cb->invert)
  395. info->invert_flags |= XT_CONNTRACK_ORIGSRC;
  396. break;
  397. case O_CTORIGDST:
  398. info->origdst_addr = cb->val.haddr;
  399. info->origdst_mask = cb->val.hmask;
  400. info->match_flags |= XT_CONNTRACK_ORIGDST;
  401. if (cb->invert)
  402. info->invert_flags |= XT_CONNTRACK_ORIGDST;
  403. break;
  404. case O_CTREPLSRC:
  405. info->replsrc_addr = cb->val.haddr;
  406. info->replsrc_mask = cb->val.hmask;
  407. info->match_flags |= XT_CONNTRACK_REPLSRC;
  408. if (cb->invert)
  409. info->invert_flags |= XT_CONNTRACK_REPLSRC;
  410. break;
  411. case O_CTREPLDST:
  412. info->repldst_addr = cb->val.haddr;
  413. info->repldst_mask = cb->val.hmask;
  414. info->match_flags |= XT_CONNTRACK_REPLDST;
  415. if (cb->invert)
  416. info->invert_flags |= XT_CONNTRACK_REPLDST;
  417. break;
  418. case O_CTSTATUS:
  419. conntrack_ps_statuses(info, cb->arg);
  420. info->match_flags |= XT_CONNTRACK_STATUS;
  421. if (cb->invert)
  422. info->invert_flags |= XT_CONNTRACK_STATUS;
  423. break;
  424. case O_CTEXPIRE:
  425. info->expires_min = cb->val.u32_range[0];
  426. info->expires_max = cb->val.u32_range[0];
  427. if (cb->nvals >= 2)
  428. info->expires_max = cb->val.u32_range[1];
  429. info->match_flags |= XT_CONNTRACK_EXPIRES;
  430. if (cb->invert)
  431. info->invert_flags |= XT_CONNTRACK_EXPIRES;
  432. break;
  433. case O_CTORIGSRCPORT:
  434. info->origsrc_port = cb->val.port_range[0];
  435. info->origsrc_port_high = cb->val.port_range[cb->nvals >= 2];
  436. info->match_flags |= XT_CONNTRACK_ORIGSRC_PORT;
  437. if (cb->invert)
  438. info->invert_flags |= XT_CONNTRACK_ORIGSRC_PORT;
  439. break;
  440. case O_CTORIGDSTPORT:
  441. info->origdst_port = cb->val.port_range[0];
  442. info->origdst_port_high = cb->val.port_range[cb->nvals >= 2];
  443. info->match_flags |= XT_CONNTRACK_ORIGDST_PORT;
  444. if (cb->invert)
  445. info->invert_flags |= XT_CONNTRACK_ORIGDST_PORT;
  446. break;
  447. case O_CTREPLSRCPORT:
  448. info->replsrc_port = cb->val.port_range[0];
  449. info->replsrc_port_high = cb->val.port_range[cb->nvals >= 2];
  450. info->match_flags |= XT_CONNTRACK_REPLSRC_PORT;
  451. if (cb->invert)
  452. info->invert_flags |= XT_CONNTRACK_REPLSRC_PORT;
  453. break;
  454. case O_CTREPLDSTPORT:
  455. info->repldst_port = cb->val.port_range[0];
  456. info->repldst_port_high = cb->val.port_range[cb->nvals >= 2];
  457. info->match_flags |= XT_CONNTRACK_REPLDST_PORT;
  458. if (cb->invert)
  459. info->invert_flags |= XT_CONNTRACK_REPLDST_PORT;
  460. break;
  461. case O_CTDIR:
  462. if (strcasecmp(cb->arg, "ORIGINAL") == 0) {
  463. info->match_flags |= XT_CONNTRACK_DIRECTION;
  464. info->invert_flags &= ~XT_CONNTRACK_DIRECTION;
  465. } else if (strcasecmp(cb->arg, "REPLY") == 0) {
  466. info->match_flags |= XT_CONNTRACK_DIRECTION;
  467. info->invert_flags |= XT_CONNTRACK_DIRECTION;
  468. } else {
  469. xtables_param_act(XTF_BAD_VALUE, "conntrack", "--ctdir", cb->arg);
  470. }
  471. break;
  472. }
  473. }
  474. #define cinfo_transform(r, l) \
  475. do { \
  476. memcpy((r), (l), offsetof(typeof(*(l)), state_mask)); \
  477. (r)->state_mask = (l)->state_mask; \
  478. (r)->status_mask = (l)->status_mask; \
  479. } while (false);
  480. static void conntrack1_mt_parse(struct xt_option_call *cb)
  481. {
  482. struct xt_conntrack_mtinfo1 *info = cb->data;
  483. struct xt_conntrack_mtinfo3 up;
  484. memset(&up, 0, sizeof(up));
  485. cinfo_transform(&up, info);
  486. up.origsrc_port_high = up.origsrc_port;
  487. up.origdst_port_high = up.origdst_port;
  488. up.replsrc_port_high = up.replsrc_port;
  489. up.repldst_port_high = up.repldst_port;
  490. cb->data = &up;
  491. conntrack_mt_parse(cb, 3);
  492. if (up.origsrc_port != up.origsrc_port_high ||
  493. up.origdst_port != up.origdst_port_high ||
  494. up.replsrc_port != up.replsrc_port_high ||
  495. up.repldst_port != up.repldst_port_high)
  496. xtables_error(PARAMETER_PROBLEM,
  497. "conntrack rev 1 does not support port ranges");
  498. cinfo_transform(info, &up);
  499. cb->data = info;
  500. }
  501. static void conntrack2_mt_parse(struct xt_option_call *cb)
  502. {
  503. #define cinfo2_transform(r, l) \
  504. memcpy((r), (l), offsetof(typeof(*(l)), sizeof(*info));
  505. struct xt_conntrack_mtinfo2 *info = cb->data;
  506. struct xt_conntrack_mtinfo3 up;
  507. memset(&up, 0, sizeof(up));
  508. memcpy(&up, info, sizeof(*info));
  509. up.origsrc_port_high = up.origsrc_port;
  510. up.origdst_port_high = up.origdst_port;
  511. up.replsrc_port_high = up.replsrc_port;
  512. up.repldst_port_high = up.repldst_port;
  513. cb->data = &up;
  514. conntrack_mt_parse(cb, 3);
  515. if (up.origsrc_port != up.origsrc_port_high ||
  516. up.origdst_port != up.origdst_port_high ||
  517. up.replsrc_port != up.replsrc_port_high ||
  518. up.repldst_port != up.repldst_port_high)
  519. xtables_error(PARAMETER_PROBLEM,
  520. "conntrack rev 2 does not support port ranges");
  521. memcpy(info, &up, sizeof(*info));
  522. cb->data = info;
  523. #undef cinfo2_transform
  524. }
  525. static void conntrack3_mt_parse(struct xt_option_call *cb)
  526. {
  527. conntrack_mt_parse(cb, 3);
  528. }
  529. static void conntrack_mt_check(struct xt_fcheck_call *cb)
  530. {
  531. if (cb->xflags == 0)
  532. xtables_error(PARAMETER_PROBLEM, "conntrack: At least one option "
  533. "is required");
  534. }
  535. static void
  536. print_state(unsigned int statemask)
  537. {
  538. const char *sep = " ";
  539. if (statemask & XT_CONNTRACK_STATE_INVALID) {
  540. printf("%sINVALID", sep);
  541. sep = ",";
  542. }
  543. if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_NEW)) {
  544. printf("%sNEW", sep);
  545. sep = ",";
  546. }
  547. if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_RELATED)) {
  548. printf("%sRELATED", sep);
  549. sep = ",";
  550. }
  551. if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED)) {
  552. printf("%sESTABLISHED", sep);
  553. sep = ",";
  554. }
  555. if (statemask & XT_CONNTRACK_STATE_UNTRACKED) {
  556. printf("%sUNTRACKED", sep);
  557. sep = ",";
  558. }
  559. if (statemask & XT_CONNTRACK_STATE_SNAT) {
  560. printf("%sSNAT", sep);
  561. sep = ",";
  562. }
  563. if (statemask & XT_CONNTRACK_STATE_DNAT) {
  564. printf("%sDNAT", sep);
  565. sep = ",";
  566. }
  567. }
  568. static void
  569. print_status(unsigned int statusmask)
  570. {
  571. const char *sep = " ";
  572. if (statusmask & IPS_EXPECTED) {
  573. printf("%sEXPECTED", sep);
  574. sep = ",";
  575. }
  576. if (statusmask & IPS_SEEN_REPLY) {
  577. printf("%sSEEN_REPLY", sep);
  578. sep = ",";
  579. }
  580. if (statusmask & IPS_ASSURED) {
  581. printf("%sASSURED", sep);
  582. sep = ",";
  583. }
  584. if (statusmask & IPS_CONFIRMED) {
  585. printf("%sCONFIRMED", sep);
  586. sep = ",";
  587. }
  588. if (statusmask == 0)
  589. printf("%sNONE", sep);
  590. }
  591. static void
  592. conntrack_dump_addr(const union nf_inet_addr *addr,
  593. const union nf_inet_addr *mask,
  594. unsigned int family, bool numeric)
  595. {
  596. if (family == NFPROTO_IPV4) {
  597. if (!numeric && addr->ip == 0) {
  598. printf(" anywhere");
  599. return;
  600. }
  601. if (numeric)
  602. printf(" %s%s",
  603. xtables_ipaddr_to_numeric(&addr->in),
  604. xtables_ipmask_to_numeric(&mask->in));
  605. else
  606. printf(" %s%s",
  607. xtables_ipaddr_to_anyname(&addr->in),
  608. xtables_ipmask_to_numeric(&mask->in));
  609. } else if (family == NFPROTO_IPV6) {
  610. if (!numeric && addr->ip6[0] == 0 && addr->ip6[1] == 0 &&
  611. addr->ip6[2] == 0 && addr->ip6[3] == 0) {
  612. printf(" anywhere");
  613. return;
  614. }
  615. if (numeric)
  616. printf(" %s%s",
  617. xtables_ip6addr_to_numeric(&addr->in6),
  618. xtables_ip6mask_to_numeric(&mask->in6));
  619. else
  620. printf(" %s%s",
  621. xtables_ip6addr_to_anyname(&addr->in6),
  622. xtables_ip6mask_to_numeric(&mask->in6));
  623. }
  624. }
  625. static void
  626. print_addr(const struct in_addr *addr, const struct in_addr *mask,
  627. int inv, int numeric)
  628. {
  629. char buf[BUFSIZ];
  630. if (inv)
  631. printf(" !");
  632. if (mask->s_addr == 0L && !numeric)
  633. printf(" %s", "anywhere");
  634. else {
  635. if (numeric)
  636. strcpy(buf, xtables_ipaddr_to_numeric(addr));
  637. else
  638. strcpy(buf, xtables_ipaddr_to_anyname(addr));
  639. strcat(buf, xtables_ipmask_to_numeric(mask));
  640. printf(" %s", buf);
  641. }
  642. }
  643. static void
  644. matchinfo_print(const void *ip, const struct xt_entry_match *match, int numeric, const char *optpfx)
  645. {
  646. const struct xt_conntrack_info *sinfo = (const void *)match->data;
  647. if(sinfo->flags & XT_CONNTRACK_STATE) {
  648. if (sinfo->invflags & XT_CONNTRACK_STATE)
  649. printf(" !");
  650. printf(" %sctstate", optpfx);
  651. print_state(sinfo->statemask);
  652. }
  653. if(sinfo->flags & XT_CONNTRACK_PROTO) {
  654. if (sinfo->invflags & XT_CONNTRACK_PROTO)
  655. printf(" !");
  656. printf(" %sctproto", optpfx);
  657. printf(" %u", sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum);
  658. }
  659. if(sinfo->flags & XT_CONNTRACK_ORIGSRC) {
  660. if (sinfo->invflags & XT_CONNTRACK_ORIGSRC)
  661. printf(" !");
  662. printf(" %sctorigsrc", optpfx);
  663. print_addr(
  664. (struct in_addr *)&sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip,
  665. &sinfo->sipmsk[IP_CT_DIR_ORIGINAL],
  666. false,
  667. numeric);
  668. }
  669. if(sinfo->flags & XT_CONNTRACK_ORIGDST) {
  670. if (sinfo->invflags & XT_CONNTRACK_ORIGDST)
  671. printf(" !");
  672. printf(" %sctorigdst", optpfx);
  673. print_addr(
  674. (struct in_addr *)&sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip,
  675. &sinfo->dipmsk[IP_CT_DIR_ORIGINAL],
  676. false,
  677. numeric);
  678. }
  679. if(sinfo->flags & XT_CONNTRACK_REPLSRC) {
  680. if (sinfo->invflags & XT_CONNTRACK_REPLSRC)
  681. printf(" !");
  682. printf(" %sctreplsrc", optpfx);
  683. print_addr(
  684. (struct in_addr *)&sinfo->tuple[IP_CT_DIR_REPLY].src.ip,
  685. &sinfo->sipmsk[IP_CT_DIR_REPLY],
  686. false,
  687. numeric);
  688. }
  689. if(sinfo->flags & XT_CONNTRACK_REPLDST) {
  690. if (sinfo->invflags & XT_CONNTRACK_REPLDST)
  691. printf(" !");
  692. printf(" %sctrepldst", optpfx);
  693. print_addr(
  694. (struct in_addr *)&sinfo->tuple[IP_CT_DIR_REPLY].dst.ip,
  695. &sinfo->dipmsk[IP_CT_DIR_REPLY],
  696. false,
  697. numeric);
  698. }
  699. if(sinfo->flags & XT_CONNTRACK_STATUS) {
  700. if (sinfo->invflags & XT_CONNTRACK_STATUS)
  701. printf(" !");
  702. printf(" %sctstatus", optpfx);
  703. print_status(sinfo->statusmask);
  704. }
  705. if(sinfo->flags & XT_CONNTRACK_EXPIRES) {
  706. if (sinfo->invflags & XT_CONNTRACK_EXPIRES)
  707. printf(" !");
  708. printf(" %sctexpire ", optpfx);
  709. if (sinfo->expires_max == sinfo->expires_min)
  710. printf("%lu", sinfo->expires_min);
  711. else
  712. printf("%lu:%lu", sinfo->expires_min, sinfo->expires_max);
  713. }
  714. if (sinfo->flags & XT_CONNTRACK_DIRECTION) {
  715. if (sinfo->invflags & XT_CONNTRACK_DIRECTION)
  716. printf(" %sctdir REPLY", optpfx);
  717. else
  718. printf(" %sctdir ORIGINAL", optpfx);
  719. }
  720. }
  721. static void
  722. conntrack_dump_ports(const char *prefix, const char *opt,
  723. u_int16_t port_low, u_int16_t port_high)
  724. {
  725. if (port_high == 0 || port_low == port_high)
  726. printf(" %s%s %u", prefix, opt, port_low);
  727. else
  728. printf(" %s%s %u:%u", prefix, opt, port_low, port_high);
  729. }
  730. static void
  731. conntrack_dump(const struct xt_conntrack_mtinfo3 *info, const char *prefix,
  732. unsigned int family, bool numeric, bool v3)
  733. {
  734. if (info->match_flags & XT_CONNTRACK_STATE) {
  735. if (info->invert_flags & XT_CONNTRACK_STATE)
  736. printf(" !");
  737. printf(" %s%s", prefix,
  738. info->match_flags & XT_CONNTRACK_STATE_ALIAS
  739. ? "state" : "ctstate");
  740. print_state(info->state_mask);
  741. }
  742. if (info->match_flags & XT_CONNTRACK_PROTO) {
  743. if (info->invert_flags & XT_CONNTRACK_PROTO)
  744. printf(" !");
  745. printf(" %sctproto %u", prefix, info->l4proto);
  746. }
  747. if (info->match_flags & XT_CONNTRACK_ORIGSRC) {
  748. if (info->invert_flags & XT_CONNTRACK_ORIGSRC)
  749. printf(" !");
  750. printf(" %sctorigsrc", prefix);
  751. conntrack_dump_addr(&info->origsrc_addr, &info->origsrc_mask,
  752. family, numeric);
  753. }
  754. if (info->match_flags & XT_CONNTRACK_ORIGDST) {
  755. if (info->invert_flags & XT_CONNTRACK_ORIGDST)
  756. printf(" !");
  757. printf(" %sctorigdst", prefix);
  758. conntrack_dump_addr(&info->origdst_addr, &info->origdst_mask,
  759. family, numeric);
  760. }
  761. if (info->match_flags & XT_CONNTRACK_REPLSRC) {
  762. if (info->invert_flags & XT_CONNTRACK_REPLSRC)
  763. printf(" !");
  764. printf(" %sctreplsrc", prefix);
  765. conntrack_dump_addr(&info->replsrc_addr, &info->replsrc_mask,
  766. family, numeric);
  767. }
  768. if (info->match_flags & XT_CONNTRACK_REPLDST) {
  769. if (info->invert_flags & XT_CONNTRACK_REPLDST)
  770. printf(" !");
  771. printf(" %sctrepldst", prefix);
  772. conntrack_dump_addr(&info->repldst_addr, &info->repldst_mask,
  773. family, numeric);
  774. }
  775. if (info->match_flags & XT_CONNTRACK_ORIGSRC_PORT) {
  776. if (info->invert_flags & XT_CONNTRACK_ORIGSRC_PORT)
  777. printf(" !");
  778. conntrack_dump_ports(prefix, "ctorigsrcport",
  779. v3 ? info->origsrc_port : ntohs(info->origsrc_port),
  780. v3 ? info->origsrc_port_high : 0);
  781. }
  782. if (info->match_flags & XT_CONNTRACK_ORIGDST_PORT) {
  783. if (info->invert_flags & XT_CONNTRACK_ORIGDST_PORT)
  784. printf(" !");
  785. conntrack_dump_ports(prefix, "ctorigdstport",
  786. v3 ? info->origdst_port : ntohs(info->origdst_port),
  787. v3 ? info->origdst_port_high : 0);
  788. }
  789. if (info->match_flags & XT_CONNTRACK_REPLSRC_PORT) {
  790. if (info->invert_flags & XT_CONNTRACK_REPLSRC_PORT)
  791. printf(" !");
  792. conntrack_dump_ports(prefix, "ctreplsrcport",
  793. v3 ? info->replsrc_port : ntohs(info->replsrc_port),
  794. v3 ? info->replsrc_port_high : 0);
  795. }
  796. if (info->match_flags & XT_CONNTRACK_REPLDST_PORT) {
  797. if (info->invert_flags & XT_CONNTRACK_REPLDST_PORT)
  798. printf(" !");
  799. conntrack_dump_ports(prefix, "ctrepldstport",
  800. v3 ? info->repldst_port : ntohs(info->repldst_port),
  801. v3 ? info->repldst_port_high : 0);
  802. }
  803. if (info->match_flags & XT_CONNTRACK_STATUS) {
  804. if (info->invert_flags & XT_CONNTRACK_STATUS)
  805. printf(" !");
  806. printf(" %sctstatus", prefix);
  807. print_status(info->status_mask);
  808. }
  809. if (info->match_flags & XT_CONNTRACK_EXPIRES) {
  810. if (info->invert_flags & XT_CONNTRACK_EXPIRES)
  811. printf(" !");
  812. printf(" %sctexpire ", prefix);
  813. if (info->expires_max == info->expires_min)
  814. printf("%u", (unsigned int)info->expires_min);
  815. else
  816. printf("%u:%u", (unsigned int)info->expires_min,
  817. (unsigned int)info->expires_max);
  818. }
  819. if (info->match_flags & XT_CONNTRACK_DIRECTION) {
  820. if (info->invert_flags & XT_CONNTRACK_DIRECTION)
  821. printf(" %sctdir REPLY", prefix);
  822. else
  823. printf(" %sctdir ORIGINAL", prefix);
  824. }
  825. }
  826. static const char *
  827. conntrack_print_name_alias(const struct xt_entry_match *match)
  828. {
  829. struct xt_conntrack_mtinfo1 *info = (void *)match->data;
  830. return info->match_flags & XT_CONNTRACK_STATE_ALIAS
  831. ? "state" : "conntrack";
  832. }
  833. static void conntrack_print(const void *ip, const struct xt_entry_match *match,
  834. int numeric)
  835. {
  836. matchinfo_print(ip, match, numeric, "");
  837. }
  838. static void
  839. conntrack1_mt4_print(const void *ip, const struct xt_entry_match *match,
  840. int numeric)
  841. {
  842. const struct xt_conntrack_mtinfo1 *info = (void *)match->data;
  843. struct xt_conntrack_mtinfo3 up;
  844. cinfo_transform(&up, info);
  845. conntrack_dump(&up, "", NFPROTO_IPV4, numeric, false);
  846. }
  847. static void
  848. conntrack1_mt6_print(const void *ip, const struct xt_entry_match *match,
  849. int numeric)
  850. {
  851. const struct xt_conntrack_mtinfo1 *info = (void *)match->data;
  852. struct xt_conntrack_mtinfo3 up;
  853. cinfo_transform(&up, info);
  854. conntrack_dump(&up, "", NFPROTO_IPV6, numeric, false);
  855. }
  856. static void
  857. conntrack2_mt_print(const void *ip, const struct xt_entry_match *match,
  858. int numeric)
  859. {
  860. conntrack_dump((const void *)match->data, "", NFPROTO_IPV4, numeric, false);
  861. }
  862. static void
  863. conntrack2_mt6_print(const void *ip, const struct xt_entry_match *match,
  864. int numeric)
  865. {
  866. conntrack_dump((const void *)match->data, "", NFPROTO_IPV6, numeric, false);
  867. }
  868. static void
  869. conntrack3_mt_print(const void *ip, const struct xt_entry_match *match,
  870. int numeric)
  871. {
  872. conntrack_dump((const void *)match->data, "", NFPROTO_IPV4, numeric, true);
  873. }
  874. static void
  875. conntrack3_mt6_print(const void *ip, const struct xt_entry_match *match,
  876. int numeric)
  877. {
  878. conntrack_dump((const void *)match->data, "", NFPROTO_IPV6, numeric, true);
  879. }
  880. static void conntrack_save(const void *ip, const struct xt_entry_match *match)
  881. {
  882. matchinfo_print(ip, match, 1, "--");
  883. }
  884. static void conntrack3_mt_save(const void *ip,
  885. const struct xt_entry_match *match)
  886. {
  887. conntrack_dump((const void *)match->data, "--", NFPROTO_IPV4, true, true);
  888. }
  889. static void conntrack3_mt6_save(const void *ip,
  890. const struct xt_entry_match *match)
  891. {
  892. conntrack_dump((const void *)match->data, "--", NFPROTO_IPV6, true, true);
  893. }
  894. static void conntrack2_mt_save(const void *ip,
  895. const struct xt_entry_match *match)
  896. {
  897. conntrack_dump((const void *)match->data, "--", NFPROTO_IPV4, true, false);
  898. }
  899. static void conntrack2_mt6_save(const void *ip,
  900. const struct xt_entry_match *match)
  901. {
  902. conntrack_dump((const void *)match->data, "--", NFPROTO_IPV6, true, false);
  903. }
  904. static void
  905. conntrack1_mt4_save(const void *ip, const struct xt_entry_match *match)
  906. {
  907. const struct xt_conntrack_mtinfo1 *info = (void *)match->data;
  908. struct xt_conntrack_mtinfo3 up;
  909. cinfo_transform(&up, info);
  910. conntrack_dump(&up, "--", NFPROTO_IPV4, true, false);
  911. }
  912. static void
  913. conntrack1_mt6_save(const void *ip, const struct xt_entry_match *match)
  914. {
  915. const struct xt_conntrack_mtinfo1 *info = (void *)match->data;
  916. struct xt_conntrack_mtinfo3 up;
  917. cinfo_transform(&up, info);
  918. conntrack_dump(&up, "--", NFPROTO_IPV6, true, false);
  919. }
  920. static void
  921. state_help(void)
  922. {
  923. printf(
  924. "state match options:\n"
  925. " [!] --state [INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED][,...]\n"
  926. " State(s) to match\n");
  927. }
  928. static const struct xt_option_entry state_opts[] = {
  929. {.name = "state", .id = O_CTSTATE, .type = XTTYPE_STRING,
  930. .flags = XTOPT_MAND | XTOPT_INVERT},
  931. XTOPT_TABLEEND,
  932. };
  933. static unsigned int
  934. state_parse_state(const char *state, size_t len)
  935. {
  936. if (strncasecmp(state, "INVALID", len) == 0)
  937. return XT_STATE_INVALID;
  938. else if (strncasecmp(state, "NEW", len) == 0)
  939. return XT_STATE_BIT(IP_CT_NEW);
  940. else if (strncasecmp(state, "ESTABLISHED", len) == 0)
  941. return XT_STATE_BIT(IP_CT_ESTABLISHED);
  942. else if (strncasecmp(state, "RELATED", len) == 0)
  943. return XT_STATE_BIT(IP_CT_RELATED);
  944. else if (strncasecmp(state, "UNTRACKED", len) == 0)
  945. return XT_STATE_UNTRACKED;
  946. return 0;
  947. }
  948. static unsigned int
  949. state_parse_states(const char *arg)
  950. {
  951. const char *comma;
  952. unsigned int mask = 0, flag;
  953. while ((comma = strchr(arg, ',')) != NULL) {
  954. if (comma == arg)
  955. goto badstate;
  956. flag = state_parse_state(arg, comma-arg);
  957. if (flag == 0)
  958. goto badstate;
  959. mask |= flag;
  960. arg = comma+1;
  961. }
  962. if (!*arg)
  963. xtables_error(PARAMETER_PROBLEM, "\"--state\" requires a list of "
  964. "states with no spaces, e.g. "
  965. "ESTABLISHED,RELATED");
  966. if (strlen(arg) == 0)
  967. goto badstate;
  968. flag = state_parse_state(arg, strlen(arg));
  969. if (flag == 0)
  970. goto badstate;
  971. mask |= flag;
  972. return mask;
  973. badstate:
  974. xtables_error(PARAMETER_PROBLEM, "Bad state \"%s\"", arg);
  975. }
  976. static void state_parse(struct xt_option_call *cb)
  977. {
  978. struct xt_state_info *sinfo = cb->data;
  979. xtables_option_parse(cb);
  980. sinfo->statemask = state_parse_states(cb->arg);
  981. if (cb->invert)
  982. sinfo->statemask = ~sinfo->statemask;
  983. }
  984. static void state_ct1_parse(struct xt_option_call *cb)
  985. {
  986. struct xt_conntrack_mtinfo1 *sinfo = cb->data;
  987. xtables_option_parse(cb);
  988. sinfo->match_flags = XT_CONNTRACK_STATE | XT_CONNTRACK_STATE_ALIAS;
  989. sinfo->state_mask = state_parse_states(cb->arg);
  990. if (cb->invert)
  991. sinfo->invert_flags |= XT_CONNTRACK_STATE;
  992. }
  993. static void state_ct23_parse(struct xt_option_call *cb)
  994. {
  995. struct xt_conntrack_mtinfo3 *sinfo = cb->data;
  996. xtables_option_parse(cb);
  997. sinfo->match_flags = XT_CONNTRACK_STATE | XT_CONNTRACK_STATE_ALIAS;
  998. sinfo->state_mask = state_parse_states(cb->arg);
  999. if (cb->invert)
  1000. sinfo->invert_flags |= XT_CONNTRACK_STATE;
  1001. }
  1002. static void state_print_state(unsigned int statemask)
  1003. {
  1004. const char *sep = "";
  1005. if (statemask & XT_STATE_INVALID) {
  1006. printf("%sINVALID", sep);
  1007. sep = ",";
  1008. }
  1009. if (statemask & XT_STATE_BIT(IP_CT_NEW)) {
  1010. printf("%sNEW", sep);
  1011. sep = ",";
  1012. }
  1013. if (statemask & XT_STATE_BIT(IP_CT_RELATED)) {
  1014. printf("%sRELATED", sep);
  1015. sep = ",";
  1016. }
  1017. if (statemask & XT_STATE_BIT(IP_CT_ESTABLISHED)) {
  1018. printf("%sESTABLISHED", sep);
  1019. sep = ",";
  1020. }
  1021. if (statemask & XT_STATE_UNTRACKED) {
  1022. printf("%sUNTRACKED", sep);
  1023. sep = ",";
  1024. }
  1025. }
  1026. static void
  1027. state_print(const void *ip,
  1028. const struct xt_entry_match *match,
  1029. int numeric)
  1030. {
  1031. const struct xt_state_info *sinfo = (const void *)match->data;
  1032. printf(" state ");
  1033. state_print_state(sinfo->statemask);
  1034. }
  1035. static void state_save(const void *ip, const struct xt_entry_match *match)
  1036. {
  1037. const struct xt_state_info *sinfo = (const void *)match->data;
  1038. printf(" --state ");
  1039. state_print_state(sinfo->statemask);
  1040. }
  1041. static struct xtables_match conntrack_mt_reg[] = {
  1042. {
  1043. .version = XTABLES_VERSION,
  1044. .name = "conntrack",
  1045. .revision = 0,
  1046. .family = NFPROTO_IPV4,
  1047. .size = XT_ALIGN(sizeof(struct xt_conntrack_info)),
  1048. .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_info)),
  1049. .help = conntrack_mt_help,
  1050. .x6_parse = conntrack_parse,
  1051. .x6_fcheck = conntrack_mt_check,
  1052. .print = conntrack_print,
  1053. .save = conntrack_save,
  1054. .alias = conntrack_print_name_alias,
  1055. .x6_options = conntrack_mt_opts_v0,
  1056. },
  1057. {
  1058. .version = XTABLES_VERSION,
  1059. .name = "conntrack",
  1060. .revision = 1,
  1061. .family = NFPROTO_IPV4,
  1062. .size = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
  1063. .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
  1064. .help = conntrack_mt_help,
  1065. .x6_parse = conntrack1_mt_parse,
  1066. .x6_fcheck = conntrack_mt_check,
  1067. .print = conntrack1_mt4_print,
  1068. .save = conntrack1_mt4_save,
  1069. .alias = conntrack_print_name_alias,
  1070. .x6_options = conntrack2_mt_opts,
  1071. },
  1072. {
  1073. .version = XTABLES_VERSION,
  1074. .name = "conntrack",
  1075. .revision = 1,
  1076. .family = NFPROTO_IPV6,
  1077. .size = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
  1078. .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
  1079. .help = conntrack_mt_help,
  1080. .x6_parse = conntrack1_mt_parse,
  1081. .x6_fcheck = conntrack_mt_check,
  1082. .print = conntrack1_mt6_print,
  1083. .save = conntrack1_mt6_save,
  1084. .alias = conntrack_print_name_alias,
  1085. .x6_options = conntrack2_mt_opts,
  1086. },
  1087. {
  1088. .version = XTABLES_VERSION,
  1089. .name = "conntrack",
  1090. .revision = 2,
  1091. .family = NFPROTO_IPV4,
  1092. .size = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
  1093. .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
  1094. .help = conntrack_mt_help,
  1095. .x6_parse = conntrack2_mt_parse,
  1096. .x6_fcheck = conntrack_mt_check,
  1097. .print = conntrack2_mt_print,
  1098. .save = conntrack2_mt_save,
  1099. .alias = conntrack_print_name_alias,
  1100. .x6_options = conntrack2_mt_opts,
  1101. },
  1102. {
  1103. .version = XTABLES_VERSION,
  1104. .name = "conntrack",
  1105. .revision = 2,
  1106. .family = NFPROTO_IPV6,
  1107. .size = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
  1108. .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
  1109. .help = conntrack_mt_help,
  1110. .x6_parse = conntrack2_mt_parse,
  1111. .x6_fcheck = conntrack_mt_check,
  1112. .print = conntrack2_mt6_print,
  1113. .save = conntrack2_mt6_save,
  1114. .alias = conntrack_print_name_alias,
  1115. .x6_options = conntrack2_mt_opts,
  1116. },
  1117. {
  1118. .version = XTABLES_VERSION,
  1119. .name = "conntrack",
  1120. .revision = 3,
  1121. .family = NFPROTO_IPV4,
  1122. .size = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
  1123. .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
  1124. .help = conntrack_mt_help,
  1125. .x6_parse = conntrack3_mt_parse,
  1126. .x6_fcheck = conntrack_mt_check,
  1127. .print = conntrack3_mt_print,
  1128. .save = conntrack3_mt_save,
  1129. .alias = conntrack_print_name_alias,
  1130. .x6_options = conntrack3_mt_opts,
  1131. },
  1132. {
  1133. .version = XTABLES_VERSION,
  1134. .name = "conntrack",
  1135. .revision = 3,
  1136. .family = NFPROTO_IPV6,
  1137. .size = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
  1138. .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
  1139. .help = conntrack_mt_help,
  1140. .x6_parse = conntrack3_mt_parse,
  1141. .x6_fcheck = conntrack_mt_check,
  1142. .print = conntrack3_mt6_print,
  1143. .save = conntrack3_mt6_save,
  1144. .alias = conntrack_print_name_alias,
  1145. .x6_options = conntrack3_mt_opts,
  1146. },
  1147. {
  1148. .family = NFPROTO_UNSPEC,
  1149. .name = "state",
  1150. .real_name = "conntrack",
  1151. .revision = 1,
  1152. .ext_flags = XTABLES_EXT_ALIAS,
  1153. .version = XTABLES_VERSION,
  1154. .size = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
  1155. .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
  1156. .help = state_help,
  1157. .x6_parse = state_ct1_parse,
  1158. .x6_options = state_opts,
  1159. },
  1160. {
  1161. .family = NFPROTO_UNSPEC,
  1162. .name = "state",
  1163. .real_name = "conntrack",
  1164. .revision = 2,
  1165. .ext_flags = XTABLES_EXT_ALIAS,
  1166. .version = XTABLES_VERSION,
  1167. .size = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
  1168. .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
  1169. .help = state_help,
  1170. .x6_parse = state_ct23_parse,
  1171. .x6_options = state_opts,
  1172. },
  1173. {
  1174. .family = NFPROTO_UNSPEC,
  1175. .name = "state",
  1176. .real_name = "conntrack",
  1177. .revision = 3,
  1178. .ext_flags = XTABLES_EXT_ALIAS,
  1179. .version = XTABLES_VERSION,
  1180. .size = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
  1181. .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
  1182. .help = state_help,
  1183. .x6_parse = state_ct23_parse,
  1184. .x6_options = state_opts,
  1185. },
  1186. {
  1187. .family = NFPROTO_UNSPEC,
  1188. .name = "state",
  1189. .revision = 0,
  1190. .version = XTABLES_VERSION,
  1191. .size = XT_ALIGN(sizeof(struct xt_state_info)),
  1192. .userspacesize = XT_ALIGN(sizeof(struct xt_state_info)),
  1193. .help = state_help,
  1194. .print = state_print,
  1195. .save = state_save,
  1196. .x6_parse = state_parse,
  1197. .x6_options = state_opts,
  1198. },
  1199. };
  1200. void _init(void)
  1201. {
  1202. xtables_register_matches(conntrack_mt_reg, ARRAY_SIZE(conntrack_mt_reg));
  1203. }