010-add-set-dscpmark-support.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. From 74267bacce0c43e5038b0377cb7c08f1ad9d50a3 Mon Sep 17 00:00:00 2001
  2. From: Kevin Darbyshire-Bryant <[email protected]>
  3. Date: Sat, 23 Mar 2019 10:21:03 +0000
  4. Subject: [PATCH] iptables: connmark - add set-dscpmark option for openwrt
  5. Naive user space front end to xt_connmark 'setdscp' option.
  6. iptables -A QOS_MARK_eth0 -t mangle -j CONNMARK --set-dscpmark 0xfc000000/0x01000000
  7. This version has a hack to support a backport to 4.14
  8. Signed-off-by: Kevin Darbyshire-Bryant <[email protected]>
  9. ---
  10. extensions/libxt_CONNMARK.c | 315 +++++++++++++++++++++++++-
  11. include/linux/netfilter/xt_connmark.h | 10 +
  12. 2 files changed, 324 insertions(+), 1 deletion(-)
  13. --- a/extensions/libxt_CONNMARK.c
  14. +++ b/extensions/libxt_CONNMARK.c
  15. @@ -22,6 +22,7 @@
  16. #include <stdbool.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. +#include <strings.h>
  20. #include <xtables.h>
  21. #include <linux/netfilter/xt_CONNMARK.h>
  22. @@ -49,6 +50,7 @@ enum {
  23. O_CTMASK,
  24. O_NFMASK,
  25. O_MASK,
  26. + O_DSCP_MARK,
  27. F_SET_MARK = 1 << O_SET_MARK,
  28. F_SAVE_MARK = 1 << O_SAVE_MARK,
  29. F_RESTORE_MARK = 1 << O_RESTORE_MARK,
  30. @@ -61,8 +63,10 @@ enum {
  31. F_CTMASK = 1 << O_CTMASK,
  32. F_NFMASK = 1 << O_NFMASK,
  33. F_MASK = 1 << O_MASK,
  34. + F_DSCP_MARK = 1 << O_DSCP_MARK,
  35. F_OP_ANY = F_SET_MARK | F_SAVE_MARK | F_RESTORE_MARK |
  36. - F_AND_MARK | F_OR_MARK | F_XOR_MARK | F_SET_XMARK,
  37. + F_AND_MARK | F_OR_MARK | F_XOR_MARK | F_SET_XMARK |
  38. + F_DSCP_MARK,
  39. };
  40. static const char *const xt_connmark_shift_ops[] = {
  41. @@ -114,6 +118,8 @@ static const struct xt_option_entry conn
  42. .excl = F_MASK, .flags = XTOPT_PUT, XTOPT_POINTER(s, nfmask)},
  43. {.name = "mask", .id = O_MASK, .type = XTTYPE_UINT32,
  44. .excl = F_CTMASK | F_NFMASK},
  45. + {.name = "set-dscpmark", .id = O_DSCP_MARK, .type = XTTYPE_MARKMASK32,
  46. + .excl = F_OP_ANY},
  47. XTOPT_TABLEEND,
  48. };
  49. #undef s
  50. @@ -148,6 +154,38 @@ static const struct xt_option_entry conn
  51. };
  52. #undef s
  53. +#define s struct xt_connmark_tginfo3
  54. +static const struct xt_option_entry connmark_tg_opts_v3[] = {
  55. + {.name = "set-xmark", .id = O_SET_XMARK, .type = XTTYPE_MARKMASK32,
  56. + .excl = F_OP_ANY},
  57. + {.name = "set-mark", .id = O_SET_MARK, .type = XTTYPE_MARKMASK32,
  58. + .excl = F_OP_ANY},
  59. + {.name = "and-mark", .id = O_AND_MARK, .type = XTTYPE_UINT32,
  60. + .excl = F_OP_ANY},
  61. + {.name = "or-mark", .id = O_OR_MARK, .type = XTTYPE_UINT32,
  62. + .excl = F_OP_ANY},
  63. + {.name = "xor-mark", .id = O_XOR_MARK, .type = XTTYPE_UINT32,
  64. + .excl = F_OP_ANY},
  65. + {.name = "save-mark", .id = O_SAVE_MARK, .type = XTTYPE_NONE,
  66. + .excl = F_OP_ANY},
  67. + {.name = "restore-mark", .id = O_RESTORE_MARK, .type = XTTYPE_NONE,
  68. + .excl = F_OP_ANY},
  69. + {.name = "left-shift-mark", .id = O_LEFT_SHIFT_MARK, .type = XTTYPE_UINT8,
  70. + .min = 0, .max = 32},
  71. + {.name = "right-shift-mark", .id = O_RIGHT_SHIFT_MARK, .type = XTTYPE_UINT8,
  72. + .min = 0, .max = 32},
  73. + {.name = "ctmask", .id = O_CTMASK, .type = XTTYPE_UINT32,
  74. + .excl = F_MASK, .flags = XTOPT_PUT, XTOPT_POINTER(s, ctmask)},
  75. + {.name = "nfmask", .id = O_NFMASK, .type = XTTYPE_UINT32,
  76. + .excl = F_MASK, .flags = XTOPT_PUT, XTOPT_POINTER(s, nfmask)},
  77. + {.name = "mask", .id = O_MASK, .type = XTTYPE_UINT32,
  78. + .excl = F_CTMASK | F_NFMASK},
  79. + {.name = "set-dscpmark", .id = O_DSCP_MARK, .type = XTTYPE_MARKMASK32,
  80. + .excl = F_OP_ANY},
  81. + XTOPT_TABLEEND,
  82. +};
  83. +#undef s
  84. +
  85. static void connmark_tg_help(void)
  86. {
  87. printf(
  88. @@ -175,6 +213,15 @@ static void connmark_tg_help_v2(void)
  89. );
  90. }
  91. +static void connmark_tg_help_v3(void)
  92. +{
  93. + connmark_tg_help_v2();
  94. + printf(
  95. +" --set-dscpmark value/mask Save DSCP to conntrack mark value\n"
  96. +);
  97. +}
  98. +
  99. +
  100. static void connmark_tg_init(struct xt_entry_target *target)
  101. {
  102. struct xt_connmark_tginfo1 *info = (void *)target->data;
  103. @@ -199,6 +246,16 @@ static void connmark_tg_init_v2(struct x
  104. info->shift_bits = 0;
  105. }
  106. +static void connmark_tg_init_v3(struct xt_entry_target *target)
  107. +{
  108. + struct xt_connmark_tginfo3 *info;
  109. +
  110. + connmark_tg_init_v2(target);
  111. + info = (void *)target->data;
  112. +
  113. + info->func = 0;
  114. +}
  115. +
  116. static void CONNMARK_parse(struct xt_option_call *cb)
  117. {
  118. struct xt_connmark_target_info *markinfo = cb->data;
  119. @@ -253,6 +310,23 @@ static void connmark_tg_parse(struct xt_
  120. info->ctmark = cb->val.u32;
  121. info->ctmask = 0;
  122. break;
  123. + case O_DSCP_MARK:
  124. +/* we sneaky sneaky this. nfmask isn't used by the set mark functionality
  125. + * and by default is set to uint32max. We can use the top bit as a flag
  126. + * that we're in DSCP_MARK submode of SET_MARK, if set then it's normal
  127. + * if unset then we're in DSCP_MARK
  128. + */
  129. + info->mode = XT_CONNMARK_SET;
  130. + info->ctmark = cb->val.mark;
  131. + info->ctmask = cb->val.mask;
  132. + info->nfmask = info->ctmark ? ffs(info->ctmark) - 1 : 0;
  133. + /* need 6 contiguous bits */
  134. + if ((~0 & (info->ctmark >> info->nfmask)) != 0x3f)
  135. + xtables_error(PARAMETER_PROBLEM,
  136. + "CONNMARK set-dscpmark: need 6 contiguous dscpmask bits");
  137. + if (info->ctmark & info->ctmask)
  138. + xtables_error(PARAMETER_PROBLEM,
  139. + "CONNMARK set-dscpmark: dscpmask/statemask bits overlap");
  140. case O_SAVE_MARK:
  141. info->mode = XT_CONNMARK_SAVE;
  142. break;
  143. @@ -320,6 +394,78 @@ static void connmark_tg_parse_v2(struct
  144. }
  145. }
  146. +static void connmark_tg_parse_v3(struct xt_option_call *cb)
  147. +{
  148. + struct xt_connmark_tginfo3 *info = cb->data;
  149. +
  150. + xtables_option_parse(cb);
  151. + switch (cb->entry->id) {
  152. + case O_SET_XMARK:
  153. + info->mode = XT_CONNMARK_SET;
  154. + info->func = XT_CONNMARK_VALUE;
  155. + info->ctmark = cb->val.mark;
  156. + info->ctmask = cb->val.mask;
  157. + break;
  158. + case O_SET_MARK:
  159. + info->mode = XT_CONNMARK_SET;
  160. + info->func = XT_CONNMARK_VALUE;
  161. + info->ctmark = cb->val.mark;
  162. + info->ctmask = cb->val.mark | cb->val.mask;
  163. + break;
  164. + case O_AND_MARK:
  165. + info->mode = XT_CONNMARK_SET;
  166. + info->func = XT_CONNMARK_VALUE;
  167. + info->ctmark = 0;
  168. + info->ctmask = ~cb->val.u32;
  169. + break;
  170. + case O_OR_MARK:
  171. + info->mode = XT_CONNMARK_SET;
  172. + info->func = XT_CONNMARK_VALUE;
  173. + info->ctmark = cb->val.u32;
  174. + info->ctmask = cb->val.u32;
  175. + break;
  176. + case O_XOR_MARK:
  177. + info->mode = XT_CONNMARK_SET;
  178. + info->func = XT_CONNMARK_VALUE;
  179. + info->ctmark = cb->val.u32;
  180. + info->ctmask = 0;
  181. + break;
  182. + case O_DSCP_MARK:
  183. + info->mode = XT_CONNMARK_SET;
  184. + info->func = XT_CONNMARK_DSCP;
  185. + info->ctmark = cb->val.mark;
  186. + info->ctmask = cb->val.mask;
  187. + info->shift_bits = info->ctmark ? ffs(info->ctmark) - 1 : 0;
  188. + /* need 6 contiguous bits */
  189. + if ((~0 & (info->ctmark >> info->shift_bits)) != 0x3f)
  190. + xtables_error(PARAMETER_PROBLEM,
  191. + "CONNMARK set-dscpmark: need 6 contiguous dscpmask bits");
  192. + if (info->ctmark & info->ctmask)
  193. + xtables_error(PARAMETER_PROBLEM,
  194. + "CONNMARK set-dscpmark: dscpmask/statemask bits overlap");
  195. + break;
  196. + case O_SAVE_MARK:
  197. + info->mode = XT_CONNMARK_SAVE;
  198. + break;
  199. + case O_RESTORE_MARK:
  200. + info->mode = XT_CONNMARK_RESTORE;
  201. + break;
  202. + case O_MASK:
  203. + info->nfmask = info->ctmask = cb->val.u32;
  204. + break;
  205. + case O_LEFT_SHIFT_MARK:
  206. + info->shift_dir = D_SHIFT_LEFT;
  207. + info->shift_bits = cb->val.u8;
  208. + break;
  209. + case O_RIGHT_SHIFT_MARK:
  210. + info->shift_dir = D_SHIFT_RIGHT;
  211. + info->shift_bits = cb->val.u8;
  212. + break;
  213. + default:
  214. + break;
  215. + }
  216. +}
  217. +
  218. static void connmark_tg_check(struct xt_fcheck_call *cb)
  219. {
  220. if (!(cb->xflags & F_OP_ANY))
  221. @@ -463,6 +609,65 @@ connmark_tg_print_v2(const void *ip, con
  222. }
  223. }
  224. +static void
  225. +connmark_tg_print_v3(const void *ip, const struct xt_entry_target *target,
  226. + int numeric)
  227. +{
  228. + const struct xt_connmark_tginfo3 *info = (const void *)target->data;
  229. + const char *shift_op = xt_connmark_shift_ops[info->shift_dir];
  230. +
  231. + switch (info->mode) {
  232. + case XT_CONNMARK_SET:
  233. + if (info->func & XT_CONNMARK_DSCP) {
  234. + printf(" CONNMARK DSCP 0x%x/0x%x",
  235. + info->ctmark, info->ctmask);
  236. + }
  237. + if (info->func & XT_CONNMARK_VALUE) {
  238. + if (info->ctmark == 0)
  239. + printf(" CONNMARK and 0x%x",
  240. + (unsigned int)(uint32_t)~info->ctmask);
  241. + else if (info->ctmark == info->ctmask)
  242. + printf(" CONNMARK or 0x%x", info->ctmark);
  243. + else if (info->ctmask == 0)
  244. + printf(" CONNMARK xor 0x%x", info->ctmark);
  245. + else if (info->ctmask == 0xFFFFFFFFU)
  246. + printf(" CONNMARK set 0x%x", info->ctmark);
  247. + else
  248. + printf(" CONNMARK xset 0x%x/0x%x",
  249. + info->ctmark, info->ctmask);
  250. + }
  251. + break;
  252. + case XT_CONNMARK_SAVE:
  253. + if (info->nfmask == UINT32_MAX && info->ctmask == UINT32_MAX)
  254. + printf(" CONNMARK save");
  255. + else if (info->nfmask == info->ctmask)
  256. + printf(" CONNMARK save mask 0x%x", info->nfmask);
  257. + else
  258. + printf(" CONNMARK save nfmask 0x%x ctmask ~0x%x",
  259. + info->nfmask, info->ctmask);
  260. + break;
  261. + case XT_CONNMARK_RESTORE:
  262. + if (info->ctmask == UINT32_MAX && info->nfmask == UINT32_MAX)
  263. + printf(" CONNMARK restore");
  264. + else if (info->ctmask == info->nfmask)
  265. + printf(" CONNMARK restore mask 0x%x", info->ctmask);
  266. + else
  267. + printf(" CONNMARK restore ctmask 0x%x nfmask ~0x%x",
  268. + info->ctmask, info->nfmask);
  269. + break;
  270. +
  271. + default:
  272. + printf(" ERROR: UNKNOWN CONNMARK MODE");
  273. + break;
  274. + }
  275. +
  276. + if (info->mode <= XT_CONNMARK_RESTORE &&
  277. + !(info->mode == XT_CONNMARK_SET && info->func == XT_CONNMARK_DSCP) &&
  278. + info->shift_bits != 0) {
  279. + printf(" %s %u", shift_op, info->shift_bits);
  280. + }
  281. +}
  282. +
  283. static void CONNMARK_save(const void *ip, const struct xt_entry_target *target)
  284. {
  285. const struct xt_connmark_target_info *markinfo =
  286. @@ -548,6 +753,38 @@ connmark_tg_save_v2(const void *ip, cons
  287. }
  288. }
  289. +static void
  290. +connmark_tg_save_v3(const void *ip, const struct xt_entry_target *target)
  291. +{
  292. + const struct xt_connmark_tginfo3 *info = (const void *)target->data;
  293. + const char *shift_op = xt_connmark_shift_ops[info->shift_dir];
  294. +
  295. + switch (info->mode) {
  296. + case XT_CONNMARK_SET:
  297. + if (info->func & XT_CONNMARK_VALUE)
  298. + printf(" --set-xmark 0x%x/0x%x", info->ctmark, info->ctmask);
  299. + if (info->func & XT_CONNMARK_DSCP)
  300. + printf(" --set-dscpmark 0x%x/0x%x", info->ctmark, info->ctmask);
  301. + break;
  302. + case XT_CONNMARK_SAVE:
  303. + printf(" --save-mark --nfmask 0x%x --ctmask 0x%x",
  304. + info->nfmask, info->ctmask);
  305. + break;
  306. + case XT_CONNMARK_RESTORE:
  307. + printf(" --restore-mark --nfmask 0x%x --ctmask 0x%x",
  308. + info->nfmask, info->ctmask);
  309. + break;
  310. + default:
  311. + printf(" ERROR: UNKNOWN CONNMARK MODE");
  312. + break;
  313. + }
  314. + if (info->mode <= XT_CONNMARK_RESTORE &&
  315. + !(info->mode == XT_CONNMARK_SET && info->func == XT_CONNMARK_DSCP) &&
  316. + info->shift_bits != 0) {
  317. + printf(" --%s %u", shift_op, info->shift_bits);
  318. + }
  319. +}
  320. +
  321. static int connmark_tg_xlate(struct xt_xlate *xl,
  322. const struct xt_xlate_tg_params *params)
  323. {
  324. @@ -644,6 +881,66 @@ static int connmark_tg_xlate_v2(struct x
  325. return 1;
  326. }
  327. +
  328. +static int connmark_tg_xlate_v3(struct xt_xlate *xl,
  329. + const struct xt_xlate_tg_params *params)
  330. +{
  331. + const struct xt_connmark_tginfo3 *info =
  332. + (const void *)params->target->data;
  333. + const char *shift_op = xt_connmark_shift_ops[info->shift_dir];
  334. +
  335. + switch (info->mode) {
  336. + case XT_CONNMARK_SET:
  337. + xt_xlate_add(xl, "ct mark set ");
  338. + if (info->func & XT_CONNMARK_VALUE) {
  339. + if (info->ctmask == 0xFFFFFFFFU)
  340. + xt_xlate_add(xl, "0x%x ", info->ctmark);
  341. + else if (info->ctmark == 0)
  342. + xt_xlate_add(xl, "ct mark and 0x%x", ~info->ctmask);
  343. + else if (info->ctmark == info->ctmask)
  344. + xt_xlate_add(xl, "ct mark or 0x%x",
  345. + info->ctmark);
  346. + else if (info->ctmask == 0)
  347. + xt_xlate_add(xl, "ct mark xor 0x%x",
  348. + info->ctmark);
  349. + else
  350. + xt_xlate_add(xl, "ct mark xor 0x%x and 0x%x",
  351. + info->ctmark, ~info->ctmask);
  352. + }
  353. + if (info->func & XT_CONNMARK_DSCP) {
  354. +/* FIXME the nftables syntax would go here if only we knew what it was */
  355. + xt_xlate_add(xl, "ct mark set typeof(ct mark) ip dscp "
  356. + "<< %u or 0x%x", info->shift_bits,
  357. + info->ctmask);
  358. + }
  359. + break;
  360. + case XT_CONNMARK_SAVE:
  361. + xt_xlate_add(xl, "ct mark set mark");
  362. + if (!(info->nfmask == UINT32_MAX &&
  363. + info->ctmask == UINT32_MAX)) {
  364. + if (info->nfmask == info->ctmask)
  365. + xt_xlate_add(xl, " and 0x%x", info->nfmask);
  366. + }
  367. + break;
  368. + case XT_CONNMARK_RESTORE:
  369. + xt_xlate_add(xl, "meta mark set ct mark");
  370. + if (!(info->nfmask == UINT32_MAX &&
  371. + info->ctmask == UINT32_MAX)) {
  372. + if (info->nfmask == info->ctmask)
  373. + xt_xlate_add(xl, " and 0x%x", info->nfmask);
  374. + }
  375. + break;
  376. + }
  377. +
  378. + if (info->mode <= XT_CONNMARK_RESTORE &&
  379. + !(info->mode == XT_CONNMARK_SET && info->func == XT_CONNMARK_DSCP) &&
  380. + info->shift_bits != 0) {
  381. + xt_xlate_add(xl, " %s %u", shift_op, info->shift_bits);
  382. + }
  383. +
  384. + return 1;
  385. +}
  386. +
  387. static struct xtables_target connmark_tg_reg[] = {
  388. {
  389. .family = NFPROTO_UNSPEC,
  390. @@ -692,6 +989,22 @@ static struct xtables_target connmark_tg
  391. .x6_options = connmark_tg_opts_v2,
  392. .xlate = connmark_tg_xlate_v2,
  393. },
  394. + {
  395. + .version = XTABLES_VERSION,
  396. + .name = "CONNMARK",
  397. + .revision = 3,
  398. + .family = NFPROTO_UNSPEC,
  399. + .size = XT_ALIGN(sizeof(struct xt_connmark_tginfo3)),
  400. + .userspacesize = XT_ALIGN(sizeof(struct xt_connmark_tginfo3)),
  401. + .help = connmark_tg_help_v3,
  402. + .init = connmark_tg_init_v3,
  403. + .print = connmark_tg_print_v3,
  404. + .save = connmark_tg_save_v3,
  405. + .x6_parse = connmark_tg_parse_v3,
  406. + .x6_fcheck = connmark_tg_check,
  407. + .x6_options = connmark_tg_opts_v3,
  408. + .xlate = connmark_tg_xlate_v3,
  409. + },
  410. };
  411. void _init(void)
  412. --- a/include/linux/netfilter/xt_connmark.h
  413. +++ b/include/linux/netfilter/xt_connmark.h
  414. @@ -18,6 +18,11 @@ enum {
  415. XT_CONNMARK_RESTORE
  416. };
  417. +enum {
  418. + XT_CONNMARK_VALUE = (1 << 0),
  419. + XT_CONNMARK_DSCP = (1 << 1)
  420. +};
  421. +
  422. struct xt_connmark_tginfo1 {
  423. __u32 ctmark, ctmask, nfmask;
  424. __u8 mode;
  425. @@ -28,6 +33,11 @@ struct xt_connmark_tginfo2 {
  426. __u8 shift_dir, shift_bits, mode;
  427. };
  428. +struct xt_connmark_tginfo3 {
  429. + __u32 ctmark, ctmask, nfmask;
  430. + __u8 shift_dir, shift_bits, mode, func;
  431. +};
  432. +
  433. struct xt_connmark_mtinfo1 {
  434. __u32 mark, mask;
  435. __u8 invert;