nft_compat.c 20 KB

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