nft_compat.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/netlink.h>
  14. #include <linux/netfilter.h>
  15. #include <linux/netfilter/nfnetlink.h>
  16. #include <linux/netfilter/nf_tables.h>
  17. #include <linux/netfilter/nf_tables_compat.h>
  18. #include <linux/netfilter/x_tables.h>
  19. #include <linux/netfilter_ipv4/ip_tables.h>
  20. #include <linux/netfilter_ipv6/ip6_tables.h>
  21. #include <linux/netfilter_bridge/ebtables.h>
  22. #include <linux/netfilter_arp/arp_tables.h>
  23. #include <net/netfilter/nf_tables.h>
  24. struct nft_xt {
  25. struct list_head head;
  26. struct nft_expr_ops ops;
  27. unsigned int refcnt;
  28. };
  29. static void nft_xt_put(struct nft_xt *xt)
  30. {
  31. if (--xt->refcnt == 0) {
  32. list_del(&xt->head);
  33. kfree(xt);
  34. }
  35. }
  36. static int nft_compat_chain_validate_dependency(const char *tablename,
  37. const struct nft_chain *chain)
  38. {
  39. const struct nft_base_chain *basechain;
  40. if (!tablename || !(chain->flags & NFT_BASE_CHAIN))
  41. return 0;
  42. basechain = nft_base_chain(chain);
  43. if (strcmp(tablename, "nat") == 0 &&
  44. basechain->type->type != NFT_CHAIN_T_NAT)
  45. return -EINVAL;
  46. return 0;
  47. }
  48. union nft_entry {
  49. struct ipt_entry e4;
  50. struct ip6t_entry e6;
  51. struct ebt_entry ebt;
  52. struct arpt_entry arp;
  53. };
  54. static inline void
  55. nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info)
  56. {
  57. par->target = xt;
  58. par->targinfo = xt_info;
  59. par->hotdrop = false;
  60. }
  61. static void nft_target_eval_xt(const struct nft_expr *expr,
  62. struct nft_regs *regs,
  63. const struct nft_pktinfo *pkt)
  64. {
  65. void *info = nft_expr_priv(expr);
  66. struct xt_target *target = expr->ops->data;
  67. struct sk_buff *skb = pkt->skb;
  68. int ret;
  69. nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
  70. ret = target->target(skb, &pkt->xt);
  71. if (pkt->xt.hotdrop)
  72. ret = NF_DROP;
  73. switch (ret) {
  74. case XT_CONTINUE:
  75. regs->verdict.code = NFT_CONTINUE;
  76. break;
  77. default:
  78. regs->verdict.code = ret;
  79. break;
  80. }
  81. }
  82. static void nft_target_eval_bridge(const struct nft_expr *expr,
  83. struct nft_regs *regs,
  84. const struct nft_pktinfo *pkt)
  85. {
  86. void *info = nft_expr_priv(expr);
  87. struct xt_target *target = expr->ops->data;
  88. struct sk_buff *skb = pkt->skb;
  89. int ret;
  90. nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
  91. ret = target->target(skb, &pkt->xt);
  92. if (pkt->xt.hotdrop)
  93. ret = NF_DROP;
  94. switch (ret) {
  95. case EBT_ACCEPT:
  96. regs->verdict.code = NF_ACCEPT;
  97. break;
  98. case EBT_DROP:
  99. regs->verdict.code = NF_DROP;
  100. break;
  101. case EBT_CONTINUE:
  102. regs->verdict.code = NFT_CONTINUE;
  103. break;
  104. case EBT_RETURN:
  105. regs->verdict.code = NFT_RETURN;
  106. break;
  107. default:
  108. regs->verdict.code = ret;
  109. break;
  110. }
  111. }
  112. static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = {
  113. [NFTA_TARGET_NAME] = { .type = NLA_NUL_STRING },
  114. [NFTA_TARGET_REV] = { .type = NLA_U32 },
  115. [NFTA_TARGET_INFO] = { .type = NLA_BINARY },
  116. };
  117. static void
  118. nft_target_set_tgchk_param(struct xt_tgchk_param *par,
  119. const struct nft_ctx *ctx,
  120. struct xt_target *target, void *info,
  121. union nft_entry *entry, u16 proto, bool inv)
  122. {
  123. par->net = ctx->net;
  124. par->table = ctx->table->name;
  125. switch (ctx->afi->family) {
  126. case AF_INET:
  127. entry->e4.ip.proto = proto;
  128. entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
  129. break;
  130. case AF_INET6:
  131. if (proto)
  132. entry->e6.ipv6.flags |= IP6T_F_PROTO;
  133. entry->e6.ipv6.proto = proto;
  134. entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
  135. break;
  136. case NFPROTO_BRIDGE:
  137. entry->ebt.ethproto = (__force __be16)proto;
  138. entry->ebt.invflags = inv ? EBT_IPROTO : 0;
  139. break;
  140. case NFPROTO_ARP:
  141. break;
  142. }
  143. par->entryinfo = entry;
  144. par->target = target;
  145. par->targinfo = info;
  146. if (ctx->chain->flags & NFT_BASE_CHAIN) {
  147. const struct nft_base_chain *basechain =
  148. nft_base_chain(ctx->chain);
  149. const struct nf_hook_ops *ops = &basechain->ops[0];
  150. par->hook_mask = 1 << ops->hooknum;
  151. } else {
  152. par->hook_mask = 0;
  153. }
  154. par->family = ctx->afi->family;
  155. par->nft_compat = true;
  156. }
  157. static void target_compat_from_user(struct xt_target *t, void *in, void *out)
  158. {
  159. int pad;
  160. memcpy(out, in, t->targetsize);
  161. pad = XT_ALIGN(t->targetsize) - t->targetsize;
  162. if (pad > 0)
  163. memset(out + t->targetsize, 0, pad);
  164. }
  165. static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = {
  166. [NFTA_RULE_COMPAT_PROTO] = { .type = NLA_U32 },
  167. [NFTA_RULE_COMPAT_FLAGS] = { .type = NLA_U32 },
  168. };
  169. static int nft_parse_compat(const struct nlattr *attr, u16 *proto, bool *inv)
  170. {
  171. struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1];
  172. u32 flags;
  173. int err;
  174. err = nla_parse_nested(tb, NFTA_RULE_COMPAT_MAX, attr,
  175. nft_rule_compat_policy);
  176. if (err < 0)
  177. return err;
  178. if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
  179. return -EINVAL;
  180. flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
  181. if (flags & ~NFT_RULE_COMPAT_F_MASK)
  182. return -EINVAL;
  183. if (flags & NFT_RULE_COMPAT_F_INV)
  184. *inv = true;
  185. *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
  186. return 0;
  187. }
  188. static int
  189. nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
  190. const struct nlattr * const tb[])
  191. {
  192. void *info = nft_expr_priv(expr);
  193. struct xt_target *target = expr->ops->data;
  194. struct xt_tgchk_param par;
  195. size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
  196. u16 proto = 0;
  197. bool inv = false;
  198. union nft_entry e = {};
  199. int ret;
  200. ret = nft_compat_chain_validate_dependency(target->table, ctx->chain);
  201. if (ret < 0)
  202. goto err;
  203. target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
  204. if (ctx->nla[NFTA_RULE_COMPAT]) {
  205. ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
  206. if (ret < 0)
  207. goto err;
  208. }
  209. nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
  210. ret = xt_check_target(&par, size, proto, inv);
  211. if (ret < 0)
  212. goto err;
  213. /* The standard target cannot be used */
  214. if (target->target == NULL) {
  215. ret = -EINVAL;
  216. goto err;
  217. }
  218. return 0;
  219. err:
  220. module_put(target->me);
  221. return ret;
  222. }
  223. static void
  224. nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
  225. {
  226. struct xt_target *target = expr->ops->data;
  227. void *info = nft_expr_priv(expr);
  228. struct xt_tgdtor_param par;
  229. par.net = ctx->net;
  230. par.target = target;
  231. par.targinfo = info;
  232. par.family = ctx->afi->family;
  233. if (par.target->destroy != NULL)
  234. par.target->destroy(&par);
  235. nft_xt_put(container_of(expr->ops, struct nft_xt, ops));
  236. module_put(target->me);
  237. }
  238. static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
  239. {
  240. const struct xt_target *target = expr->ops->data;
  241. void *info = nft_expr_priv(expr);
  242. if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
  243. nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
  244. nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(target->targetsize), info))
  245. goto nla_put_failure;
  246. return 0;
  247. nla_put_failure:
  248. return -1;
  249. }
  250. static int nft_target_validate(const struct nft_ctx *ctx,
  251. const struct nft_expr *expr,
  252. const struct nft_data **data)
  253. {
  254. struct xt_target *target = expr->ops->data;
  255. unsigned int hook_mask = 0;
  256. int ret;
  257. if (ctx->chain->flags & NFT_BASE_CHAIN) {
  258. const struct nft_base_chain *basechain =
  259. nft_base_chain(ctx->chain);
  260. const struct nf_hook_ops *ops = &basechain->ops[0];
  261. hook_mask = 1 << ops->hooknum;
  262. if (!(hook_mask & target->hooks))
  263. return -EINVAL;
  264. ret = nft_compat_chain_validate_dependency(target->table,
  265. ctx->chain);
  266. if (ret < 0)
  267. return ret;
  268. }
  269. return 0;
  270. }
  271. static void nft_match_eval(const struct nft_expr *expr,
  272. struct nft_regs *regs,
  273. const struct nft_pktinfo *pkt)
  274. {
  275. void *info = nft_expr_priv(expr);
  276. struct xt_match *match = expr->ops->data;
  277. struct sk_buff *skb = pkt->skb;
  278. bool ret;
  279. nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
  280. ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
  281. if (pkt->xt.hotdrop) {
  282. regs->verdict.code = NF_DROP;
  283. return;
  284. }
  285. switch (ret ? 1 : 0) {
  286. case 1:
  287. regs->verdict.code = NFT_CONTINUE;
  288. break;
  289. case 0:
  290. regs->verdict.code = NFT_BREAK;
  291. break;
  292. }
  293. }
  294. static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
  295. [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING },
  296. [NFTA_MATCH_REV] = { .type = NLA_U32 },
  297. [NFTA_MATCH_INFO] = { .type = NLA_BINARY },
  298. };
  299. /* struct xt_mtchk_param and xt_tgchk_param look very similar */
  300. static void
  301. nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
  302. struct xt_match *match, void *info,
  303. union nft_entry *entry, u16 proto, bool inv)
  304. {
  305. par->net = ctx->net;
  306. par->table = ctx->table->name;
  307. switch (ctx->afi->family) {
  308. case AF_INET:
  309. entry->e4.ip.proto = proto;
  310. entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
  311. break;
  312. case AF_INET6:
  313. if (proto)
  314. entry->e6.ipv6.flags |= IP6T_F_PROTO;
  315. entry->e6.ipv6.proto = proto;
  316. entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
  317. break;
  318. case NFPROTO_BRIDGE:
  319. entry->ebt.ethproto = (__force __be16)proto;
  320. entry->ebt.invflags = inv ? EBT_IPROTO : 0;
  321. break;
  322. case NFPROTO_ARP:
  323. break;
  324. }
  325. par->entryinfo = entry;
  326. par->match = match;
  327. par->matchinfo = info;
  328. if (ctx->chain->flags & NFT_BASE_CHAIN) {
  329. const struct nft_base_chain *basechain =
  330. nft_base_chain(ctx->chain);
  331. const struct nf_hook_ops *ops = &basechain->ops[0];
  332. par->hook_mask = 1 << ops->hooknum;
  333. } else {
  334. par->hook_mask = 0;
  335. }
  336. par->family = ctx->afi->family;
  337. par->nft_compat = true;
  338. }
  339. static void match_compat_from_user(struct xt_match *m, void *in, void *out)
  340. {
  341. int pad;
  342. memcpy(out, in, m->matchsize);
  343. pad = XT_ALIGN(m->matchsize) - m->matchsize;
  344. if (pad > 0)
  345. memset(out + m->matchsize, 0, pad);
  346. }
  347. static int
  348. nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
  349. const struct nlattr * const tb[])
  350. {
  351. void *info = nft_expr_priv(expr);
  352. struct xt_match *match = expr->ops->data;
  353. struct xt_mtchk_param par;
  354. size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
  355. u16 proto = 0;
  356. bool inv = false;
  357. union nft_entry e = {};
  358. int ret;
  359. ret = nft_compat_chain_validate_dependency(match->table, ctx->chain);
  360. if (ret < 0)
  361. goto err;
  362. match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
  363. if (ctx->nla[NFTA_RULE_COMPAT]) {
  364. ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
  365. if (ret < 0)
  366. goto err;
  367. }
  368. nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
  369. ret = xt_check_match(&par, size, proto, inv);
  370. if (ret < 0)
  371. goto err;
  372. return 0;
  373. err:
  374. module_put(match->me);
  375. return ret;
  376. }
  377. static void
  378. nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
  379. {
  380. struct xt_match *match = expr->ops->data;
  381. void *info = nft_expr_priv(expr);
  382. struct xt_mtdtor_param par;
  383. par.net = ctx->net;
  384. par.match = match;
  385. par.matchinfo = info;
  386. par.family = ctx->afi->family;
  387. if (par.match->destroy != NULL)
  388. par.match->destroy(&par);
  389. nft_xt_put(container_of(expr->ops, struct nft_xt, ops));
  390. module_put(match->me);
  391. }
  392. static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
  393. {
  394. void *info = nft_expr_priv(expr);
  395. struct xt_match *match = expr->ops->data;
  396. if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
  397. nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
  398. nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(match->matchsize), info))
  399. goto nla_put_failure;
  400. return 0;
  401. nla_put_failure:
  402. return -1;
  403. }
  404. static int nft_match_validate(const struct nft_ctx *ctx,
  405. const struct nft_expr *expr,
  406. const struct nft_data **data)
  407. {
  408. struct xt_match *match = expr->ops->data;
  409. unsigned int hook_mask = 0;
  410. int ret;
  411. if (ctx->chain->flags & NFT_BASE_CHAIN) {
  412. const struct nft_base_chain *basechain =
  413. nft_base_chain(ctx->chain);
  414. const struct nf_hook_ops *ops = &basechain->ops[0];
  415. hook_mask = 1 << ops->hooknum;
  416. if (!(hook_mask & match->hooks))
  417. return -EINVAL;
  418. ret = nft_compat_chain_validate_dependency(match->table,
  419. ctx->chain);
  420. if (ret < 0)
  421. return ret;
  422. }
  423. return 0;
  424. }
  425. static int
  426. nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
  427. int event, u16 family, const char *name,
  428. int rev, int target)
  429. {
  430. struct nlmsghdr *nlh;
  431. struct nfgenmsg *nfmsg;
  432. unsigned int flags = portid ? NLM_F_MULTI : 0;
  433. event |= NFNL_SUBSYS_NFT_COMPAT << 8;
  434. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
  435. if (nlh == NULL)
  436. goto nlmsg_failure;
  437. nfmsg = nlmsg_data(nlh);
  438. nfmsg->nfgen_family = family;
  439. nfmsg->version = NFNETLINK_V0;
  440. nfmsg->res_id = 0;
  441. if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
  442. nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
  443. nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
  444. goto nla_put_failure;
  445. nlmsg_end(skb, nlh);
  446. return skb->len;
  447. nlmsg_failure:
  448. nla_put_failure:
  449. nlmsg_cancel(skb, nlh);
  450. return -1;
  451. }
  452. static int nfnl_compat_get(struct net *net, struct sock *nfnl,
  453. struct sk_buff *skb, const struct nlmsghdr *nlh,
  454. const struct nlattr * const tb[])
  455. {
  456. int ret = 0, target;
  457. struct nfgenmsg *nfmsg;
  458. const char *fmt;
  459. const char *name;
  460. u32 rev;
  461. struct sk_buff *skb2;
  462. if (tb[NFTA_COMPAT_NAME] == NULL ||
  463. tb[NFTA_COMPAT_REV] == NULL ||
  464. tb[NFTA_COMPAT_TYPE] == NULL)
  465. return -EINVAL;
  466. name = nla_data(tb[NFTA_COMPAT_NAME]);
  467. rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
  468. target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
  469. nfmsg = nlmsg_data(nlh);
  470. switch(nfmsg->nfgen_family) {
  471. case AF_INET:
  472. fmt = "ipt_%s";
  473. break;
  474. case AF_INET6:
  475. fmt = "ip6t_%s";
  476. break;
  477. case NFPROTO_BRIDGE:
  478. fmt = "ebt_%s";
  479. break;
  480. case NFPROTO_ARP:
  481. fmt = "arpt_%s";
  482. break;
  483. default:
  484. pr_err("nft_compat: unsupported protocol %d\n",
  485. nfmsg->nfgen_family);
  486. return -EINVAL;
  487. }
  488. try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
  489. rev, target, &ret),
  490. fmt, name);
  491. if (ret < 0)
  492. return ret;
  493. skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  494. if (skb2 == NULL)
  495. return -ENOMEM;
  496. /* include the best revision for this extension in the message */
  497. if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
  498. nlh->nlmsg_seq,
  499. NFNL_MSG_TYPE(nlh->nlmsg_type),
  500. NFNL_MSG_COMPAT_GET,
  501. nfmsg->nfgen_family,
  502. name, ret, target) <= 0) {
  503. kfree_skb(skb2);
  504. return -ENOSPC;
  505. }
  506. ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
  507. MSG_DONTWAIT);
  508. if (ret > 0)
  509. ret = 0;
  510. return ret == -EAGAIN ? -ENOBUFS : ret;
  511. }
  512. static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
  513. [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
  514. .len = NFT_COMPAT_NAME_MAX-1 },
  515. [NFTA_COMPAT_REV] = { .type = NLA_U32 },
  516. [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
  517. };
  518. static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
  519. [NFNL_MSG_COMPAT_GET] = { .call = nfnl_compat_get,
  520. .attr_count = NFTA_COMPAT_MAX,
  521. .policy = nfnl_compat_policy_get },
  522. };
  523. static const struct nfnetlink_subsystem nfnl_compat_subsys = {
  524. .name = "nft-compat",
  525. .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
  526. .cb_count = NFNL_MSG_COMPAT_MAX,
  527. .cb = nfnl_nft_compat_cb,
  528. };
  529. static LIST_HEAD(nft_match_list);
  530. static struct nft_expr_type nft_match_type;
  531. static bool nft_match_cmp(const struct xt_match *match,
  532. const char *name, u32 rev, u32 family)
  533. {
  534. return strcmp(match->name, name) == 0 && match->revision == rev &&
  535. (match->family == NFPROTO_UNSPEC || match->family == family);
  536. }
  537. static const struct nft_expr_ops *
  538. nft_match_select_ops(const struct nft_ctx *ctx,
  539. const struct nlattr * const tb[])
  540. {
  541. struct nft_xt *nft_match;
  542. struct xt_match *match;
  543. char *mt_name;
  544. u32 rev, family;
  545. int err;
  546. if (tb[NFTA_MATCH_NAME] == NULL ||
  547. tb[NFTA_MATCH_REV] == NULL ||
  548. tb[NFTA_MATCH_INFO] == NULL)
  549. return ERR_PTR(-EINVAL);
  550. mt_name = nla_data(tb[NFTA_MATCH_NAME]);
  551. rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
  552. family = ctx->afi->family;
  553. /* Re-use the existing match if it's already loaded. */
  554. list_for_each_entry(nft_match, &nft_match_list, head) {
  555. struct xt_match *match = nft_match->ops.data;
  556. if (nft_match_cmp(match, mt_name, rev, family)) {
  557. if (!try_module_get(match->me))
  558. return ERR_PTR(-ENOENT);
  559. nft_match->refcnt++;
  560. return &nft_match->ops;
  561. }
  562. }
  563. match = xt_request_find_match(family, mt_name, rev);
  564. if (IS_ERR(match))
  565. return ERR_PTR(-ENOENT);
  566. if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
  567. err = -EINVAL;
  568. goto err;
  569. }
  570. /* This is the first time we use this match, allocate operations */
  571. nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
  572. if (nft_match == NULL) {
  573. err = -ENOMEM;
  574. goto err;
  575. }
  576. nft_match->refcnt = 1;
  577. nft_match->ops.type = &nft_match_type;
  578. nft_match->ops.size = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
  579. nft_match->ops.eval = nft_match_eval;
  580. nft_match->ops.init = nft_match_init;
  581. nft_match->ops.destroy = nft_match_destroy;
  582. nft_match->ops.dump = nft_match_dump;
  583. nft_match->ops.validate = nft_match_validate;
  584. nft_match->ops.data = match;
  585. list_add(&nft_match->head, &nft_match_list);
  586. return &nft_match->ops;
  587. err:
  588. module_put(match->me);
  589. return ERR_PTR(err);
  590. }
  591. static struct nft_expr_type nft_match_type __read_mostly = {
  592. .name = "match",
  593. .select_ops = nft_match_select_ops,
  594. .policy = nft_match_policy,
  595. .maxattr = NFTA_MATCH_MAX,
  596. .owner = THIS_MODULE,
  597. };
  598. static LIST_HEAD(nft_target_list);
  599. static struct nft_expr_type nft_target_type;
  600. static bool nft_target_cmp(const struct xt_target *tg,
  601. const char *name, u32 rev, u32 family)
  602. {
  603. return strcmp(tg->name, name) == 0 && tg->revision == rev &&
  604. (tg->family == NFPROTO_UNSPEC || tg->family == family);
  605. }
  606. static const struct nft_expr_ops *
  607. nft_target_select_ops(const struct nft_ctx *ctx,
  608. const struct nlattr * const tb[])
  609. {
  610. struct nft_xt *nft_target;
  611. struct xt_target *target;
  612. char *tg_name;
  613. u32 rev, family;
  614. int err;
  615. if (tb[NFTA_TARGET_NAME] == NULL ||
  616. tb[NFTA_TARGET_REV] == NULL ||
  617. tb[NFTA_TARGET_INFO] == NULL)
  618. return ERR_PTR(-EINVAL);
  619. tg_name = nla_data(tb[NFTA_TARGET_NAME]);
  620. rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
  621. family = ctx->afi->family;
  622. /* Re-use the existing target if it's already loaded. */
  623. list_for_each_entry(nft_target, &nft_target_list, head) {
  624. struct xt_target *target = nft_target->ops.data;
  625. if (nft_target_cmp(target, tg_name, rev, family)) {
  626. if (!try_module_get(target->me))
  627. return ERR_PTR(-ENOENT);
  628. nft_target->refcnt++;
  629. return &nft_target->ops;
  630. }
  631. }
  632. target = xt_request_find_target(family, tg_name, rev);
  633. if (IS_ERR(target))
  634. return ERR_PTR(-ENOENT);
  635. if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
  636. err = -EINVAL;
  637. goto err;
  638. }
  639. /* This is the first time we use this target, allocate operations */
  640. nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
  641. if (nft_target == NULL) {
  642. err = -ENOMEM;
  643. goto err;
  644. }
  645. nft_target->refcnt = 1;
  646. nft_target->ops.type = &nft_target_type;
  647. nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
  648. nft_target->ops.init = nft_target_init;
  649. nft_target->ops.destroy = nft_target_destroy;
  650. nft_target->ops.dump = nft_target_dump;
  651. nft_target->ops.validate = nft_target_validate;
  652. nft_target->ops.data = target;
  653. if (family == NFPROTO_BRIDGE)
  654. nft_target->ops.eval = nft_target_eval_bridge;
  655. else
  656. nft_target->ops.eval = nft_target_eval_xt;
  657. list_add(&nft_target->head, &nft_target_list);
  658. return &nft_target->ops;
  659. err:
  660. module_put(target->me);
  661. return ERR_PTR(err);
  662. }
  663. static struct nft_expr_type nft_target_type __read_mostly = {
  664. .name = "target",
  665. .select_ops = nft_target_select_ops,
  666. .policy = nft_target_policy,
  667. .maxattr = NFTA_TARGET_MAX,
  668. .owner = THIS_MODULE,
  669. };
  670. static int __init nft_compat_module_init(void)
  671. {
  672. int ret;
  673. ret = nft_register_expr(&nft_match_type);
  674. if (ret < 0)
  675. return ret;
  676. ret = nft_register_expr(&nft_target_type);
  677. if (ret < 0)
  678. goto err_match;
  679. ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
  680. if (ret < 0) {
  681. pr_err("nft_compat: cannot register with nfnetlink.\n");
  682. goto err_target;
  683. }
  684. pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n");
  685. return ret;
  686. err_target:
  687. nft_unregister_expr(&nft_target_type);
  688. err_match:
  689. nft_unregister_expr(&nft_match_type);
  690. return ret;
  691. }
  692. static void __exit nft_compat_module_exit(void)
  693. {
  694. nfnetlink_subsys_unregister(&nfnl_compat_subsys);
  695. nft_unregister_expr(&nft_target_type);
  696. nft_unregister_expr(&nft_match_type);
  697. }
  698. MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
  699. module_init(nft_compat_module_init);
  700. module_exit(nft_compat_module_exit);
  701. MODULE_LICENSE("GPL");
  702. MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
  703. MODULE_ALIAS_NFT_EXPR("match");
  704. MODULE_ALIAS_NFT_EXPR("target");