nft_meta.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
  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. * Development of this code funded by Astaro AG (http://www.astaro.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/nf_tables.h>
  16. #include <linux/in.h>
  17. #include <linux/ip.h>
  18. #include <linux/ipv6.h>
  19. #include <linux/smp.h>
  20. #include <linux/static_key.h>
  21. #include <net/dst.h>
  22. #include <net/sock.h>
  23. #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
  24. #include <net/netfilter/nf_tables.h>
  25. #include <net/netfilter/nf_tables_core.h>
  26. #include <net/netfilter/nft_meta.h>
  27. #include <uapi/linux/netfilter_bridge.h> /* NF_BR_PRE_ROUTING */
  28. static DEFINE_PER_CPU(struct rnd_state, nft_prandom_state);
  29. void nft_meta_get_eval(const struct nft_expr *expr,
  30. struct nft_regs *regs,
  31. const struct nft_pktinfo *pkt)
  32. {
  33. const struct nft_meta *priv = nft_expr_priv(expr);
  34. const struct sk_buff *skb = pkt->skb;
  35. const struct net_device *in = pkt->in, *out = pkt->out;
  36. struct sock *sk;
  37. u32 *dest = &regs->data[priv->dreg];
  38. switch (priv->key) {
  39. case NFT_META_LEN:
  40. *dest = skb->len;
  41. break;
  42. case NFT_META_PROTOCOL:
  43. *dest = 0;
  44. *(__be16 *)dest = skb->protocol;
  45. break;
  46. case NFT_META_NFPROTO:
  47. *dest = pkt->pf;
  48. break;
  49. case NFT_META_L4PROTO:
  50. if (!pkt->tprot_set)
  51. goto err;
  52. *dest = pkt->tprot;
  53. break;
  54. case NFT_META_PRIORITY:
  55. *dest = skb->priority;
  56. break;
  57. case NFT_META_MARK:
  58. *dest = skb->mark;
  59. break;
  60. case NFT_META_IIF:
  61. if (in == NULL)
  62. goto err;
  63. *dest = in->ifindex;
  64. break;
  65. case NFT_META_OIF:
  66. if (out == NULL)
  67. goto err;
  68. *dest = out->ifindex;
  69. break;
  70. case NFT_META_IIFNAME:
  71. if (in == NULL)
  72. goto err;
  73. strncpy((char *)dest, in->name, IFNAMSIZ);
  74. break;
  75. case NFT_META_OIFNAME:
  76. if (out == NULL)
  77. goto err;
  78. strncpy((char *)dest, out->name, IFNAMSIZ);
  79. break;
  80. case NFT_META_IIFTYPE:
  81. if (in == NULL)
  82. goto err;
  83. *dest = 0;
  84. *(u16 *)dest = in->type;
  85. break;
  86. case NFT_META_OIFTYPE:
  87. if (out == NULL)
  88. goto err;
  89. *dest = 0;
  90. *(u16 *)dest = out->type;
  91. break;
  92. case NFT_META_SKUID:
  93. sk = skb_to_full_sk(skb);
  94. if (!sk || !sk_fullsock(sk))
  95. goto err;
  96. read_lock_bh(&sk->sk_callback_lock);
  97. if (sk->sk_socket == NULL ||
  98. sk->sk_socket->file == NULL) {
  99. read_unlock_bh(&sk->sk_callback_lock);
  100. goto err;
  101. }
  102. *dest = from_kuid_munged(&init_user_ns,
  103. sk->sk_socket->file->f_cred->fsuid);
  104. read_unlock_bh(&sk->sk_callback_lock);
  105. break;
  106. case NFT_META_SKGID:
  107. sk = skb_to_full_sk(skb);
  108. if (!sk || !sk_fullsock(sk))
  109. goto err;
  110. read_lock_bh(&sk->sk_callback_lock);
  111. if (sk->sk_socket == NULL ||
  112. sk->sk_socket->file == NULL) {
  113. read_unlock_bh(&sk->sk_callback_lock);
  114. goto err;
  115. }
  116. *dest = from_kgid_munged(&init_user_ns,
  117. sk->sk_socket->file->f_cred->fsgid);
  118. read_unlock_bh(&sk->sk_callback_lock);
  119. break;
  120. #ifdef CONFIG_IP_ROUTE_CLASSID
  121. case NFT_META_RTCLASSID: {
  122. const struct dst_entry *dst = skb_dst(skb);
  123. if (dst == NULL)
  124. goto err;
  125. *dest = dst->tclassid;
  126. break;
  127. }
  128. #endif
  129. #ifdef CONFIG_NETWORK_SECMARK
  130. case NFT_META_SECMARK:
  131. *dest = skb->secmark;
  132. break;
  133. #endif
  134. case NFT_META_PKTTYPE:
  135. if (skb->pkt_type != PACKET_LOOPBACK) {
  136. *dest = skb->pkt_type;
  137. break;
  138. }
  139. switch (pkt->pf) {
  140. case NFPROTO_IPV4:
  141. if (ipv4_is_multicast(ip_hdr(skb)->daddr))
  142. *dest = PACKET_MULTICAST;
  143. else
  144. *dest = PACKET_BROADCAST;
  145. break;
  146. case NFPROTO_IPV6:
  147. if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF)
  148. *dest = PACKET_MULTICAST;
  149. else
  150. *dest = PACKET_BROADCAST;
  151. break;
  152. case NFPROTO_NETDEV:
  153. switch (skb->protocol) {
  154. case htons(ETH_P_IP): {
  155. int noff = skb_network_offset(skb);
  156. struct iphdr *iph, _iph;
  157. iph = skb_header_pointer(skb, noff,
  158. sizeof(_iph), &_iph);
  159. if (!iph)
  160. goto err;
  161. if (ipv4_is_multicast(iph->daddr))
  162. *dest = PACKET_MULTICAST;
  163. else
  164. *dest = PACKET_BROADCAST;
  165. break;
  166. }
  167. case htons(ETH_P_IPV6):
  168. *dest = PACKET_MULTICAST;
  169. break;
  170. default:
  171. WARN_ON_ONCE(1);
  172. goto err;
  173. }
  174. break;
  175. default:
  176. WARN_ON_ONCE(1);
  177. goto err;
  178. }
  179. break;
  180. case NFT_META_CPU:
  181. *dest = raw_smp_processor_id();
  182. break;
  183. case NFT_META_IIFGROUP:
  184. if (in == NULL)
  185. goto err;
  186. *dest = in->group;
  187. break;
  188. case NFT_META_OIFGROUP:
  189. if (out == NULL)
  190. goto err;
  191. *dest = out->group;
  192. break;
  193. #ifdef CONFIG_CGROUP_NET_CLASSID
  194. case NFT_META_CGROUP:
  195. sk = skb_to_full_sk(skb);
  196. if (!sk || !sk_fullsock(sk))
  197. goto err;
  198. *dest = sock_cgroup_classid(&sk->sk_cgrp_data);
  199. break;
  200. #endif
  201. case NFT_META_PRANDOM: {
  202. struct rnd_state *state = this_cpu_ptr(&nft_prandom_state);
  203. *dest = prandom_u32_state(state);
  204. break;
  205. }
  206. default:
  207. WARN_ON(1);
  208. goto err;
  209. }
  210. return;
  211. err:
  212. regs->verdict.code = NFT_BREAK;
  213. }
  214. EXPORT_SYMBOL_GPL(nft_meta_get_eval);
  215. void nft_meta_set_eval(const struct nft_expr *expr,
  216. struct nft_regs *regs,
  217. const struct nft_pktinfo *pkt)
  218. {
  219. const struct nft_meta *meta = nft_expr_priv(expr);
  220. struct sk_buff *skb = pkt->skb;
  221. u32 value = regs->data[meta->sreg];
  222. switch (meta->key) {
  223. case NFT_META_MARK:
  224. skb->mark = value;
  225. break;
  226. case NFT_META_PRIORITY:
  227. skb->priority = value;
  228. break;
  229. case NFT_META_PKTTYPE:
  230. if (skb->pkt_type != value &&
  231. skb_pkt_type_ok(value) && skb_pkt_type_ok(skb->pkt_type))
  232. skb->pkt_type = value;
  233. break;
  234. case NFT_META_NFTRACE:
  235. skb->nf_trace = !!value;
  236. break;
  237. default:
  238. WARN_ON(1);
  239. }
  240. }
  241. EXPORT_SYMBOL_GPL(nft_meta_set_eval);
  242. const struct nla_policy nft_meta_policy[NFTA_META_MAX + 1] = {
  243. [NFTA_META_DREG] = { .type = NLA_U32 },
  244. [NFTA_META_KEY] = { .type = NLA_U32 },
  245. [NFTA_META_SREG] = { .type = NLA_U32 },
  246. };
  247. EXPORT_SYMBOL_GPL(nft_meta_policy);
  248. int nft_meta_get_init(const struct nft_ctx *ctx,
  249. const struct nft_expr *expr,
  250. const struct nlattr * const tb[])
  251. {
  252. struct nft_meta *priv = nft_expr_priv(expr);
  253. unsigned int len;
  254. priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
  255. switch (priv->key) {
  256. case NFT_META_PROTOCOL:
  257. case NFT_META_IIFTYPE:
  258. case NFT_META_OIFTYPE:
  259. len = sizeof(u16);
  260. break;
  261. case NFT_META_NFPROTO:
  262. case NFT_META_L4PROTO:
  263. case NFT_META_LEN:
  264. case NFT_META_PRIORITY:
  265. case NFT_META_MARK:
  266. case NFT_META_IIF:
  267. case NFT_META_OIF:
  268. case NFT_META_SKUID:
  269. case NFT_META_SKGID:
  270. #ifdef CONFIG_IP_ROUTE_CLASSID
  271. case NFT_META_RTCLASSID:
  272. #endif
  273. #ifdef CONFIG_NETWORK_SECMARK
  274. case NFT_META_SECMARK:
  275. #endif
  276. case NFT_META_PKTTYPE:
  277. case NFT_META_CPU:
  278. case NFT_META_IIFGROUP:
  279. case NFT_META_OIFGROUP:
  280. #ifdef CONFIG_CGROUP_NET_CLASSID
  281. case NFT_META_CGROUP:
  282. #endif
  283. len = sizeof(u32);
  284. break;
  285. case NFT_META_IIFNAME:
  286. case NFT_META_OIFNAME:
  287. len = IFNAMSIZ;
  288. break;
  289. case NFT_META_PRANDOM:
  290. prandom_init_once(&nft_prandom_state);
  291. len = sizeof(u32);
  292. break;
  293. default:
  294. return -EOPNOTSUPP;
  295. }
  296. priv->dreg = nft_parse_register(tb[NFTA_META_DREG]);
  297. return nft_validate_register_store(ctx, priv->dreg, NULL,
  298. NFT_DATA_VALUE, len);
  299. }
  300. EXPORT_SYMBOL_GPL(nft_meta_get_init);
  301. int nft_meta_set_validate(const struct nft_ctx *ctx,
  302. const struct nft_expr *expr,
  303. const struct nft_data **data)
  304. {
  305. struct nft_meta *priv = nft_expr_priv(expr);
  306. unsigned int hooks;
  307. if (priv->key != NFT_META_PKTTYPE)
  308. return 0;
  309. switch (ctx->afi->family) {
  310. case NFPROTO_BRIDGE:
  311. hooks = 1 << NF_BR_PRE_ROUTING;
  312. break;
  313. case NFPROTO_NETDEV:
  314. hooks = 1 << NF_NETDEV_INGRESS;
  315. break;
  316. default:
  317. return -EOPNOTSUPP;
  318. }
  319. return nft_chain_validate_hooks(ctx->chain, hooks);
  320. }
  321. EXPORT_SYMBOL_GPL(nft_meta_set_validate);
  322. int nft_meta_set_init(const struct nft_ctx *ctx,
  323. const struct nft_expr *expr,
  324. const struct nlattr * const tb[])
  325. {
  326. struct nft_meta *priv = nft_expr_priv(expr);
  327. unsigned int len;
  328. int err;
  329. priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
  330. switch (priv->key) {
  331. case NFT_META_MARK:
  332. case NFT_META_PRIORITY:
  333. len = sizeof(u32);
  334. break;
  335. case NFT_META_NFTRACE:
  336. len = sizeof(u8);
  337. break;
  338. case NFT_META_PKTTYPE:
  339. len = sizeof(u8);
  340. break;
  341. default:
  342. return -EOPNOTSUPP;
  343. }
  344. err = nft_meta_set_validate(ctx, expr, NULL);
  345. if (err < 0)
  346. return err;
  347. priv->sreg = nft_parse_register(tb[NFTA_META_SREG]);
  348. err = nft_validate_register_load(priv->sreg, len);
  349. if (err < 0)
  350. return err;
  351. if (priv->key == NFT_META_NFTRACE)
  352. static_branch_inc(&nft_trace_enabled);
  353. return 0;
  354. }
  355. EXPORT_SYMBOL_GPL(nft_meta_set_init);
  356. int nft_meta_get_dump(struct sk_buff *skb,
  357. const struct nft_expr *expr)
  358. {
  359. const struct nft_meta *priv = nft_expr_priv(expr);
  360. if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
  361. goto nla_put_failure;
  362. if (nft_dump_register(skb, NFTA_META_DREG, priv->dreg))
  363. goto nla_put_failure;
  364. return 0;
  365. nla_put_failure:
  366. return -1;
  367. }
  368. EXPORT_SYMBOL_GPL(nft_meta_get_dump);
  369. int nft_meta_set_dump(struct sk_buff *skb,
  370. const struct nft_expr *expr)
  371. {
  372. const struct nft_meta *priv = nft_expr_priv(expr);
  373. if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
  374. goto nla_put_failure;
  375. if (nft_dump_register(skb, NFTA_META_SREG, priv->sreg))
  376. goto nla_put_failure;
  377. return 0;
  378. nla_put_failure:
  379. return -1;
  380. }
  381. EXPORT_SYMBOL_GPL(nft_meta_set_dump);
  382. void nft_meta_set_destroy(const struct nft_ctx *ctx,
  383. const struct nft_expr *expr)
  384. {
  385. const struct nft_meta *priv = nft_expr_priv(expr);
  386. if (priv->key == NFT_META_NFTRACE)
  387. static_branch_dec(&nft_trace_enabled);
  388. }
  389. EXPORT_SYMBOL_GPL(nft_meta_set_destroy);
  390. static struct nft_expr_type nft_meta_type;
  391. static const struct nft_expr_ops nft_meta_get_ops = {
  392. .type = &nft_meta_type,
  393. .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
  394. .eval = nft_meta_get_eval,
  395. .init = nft_meta_get_init,
  396. .dump = nft_meta_get_dump,
  397. };
  398. static const struct nft_expr_ops nft_meta_set_ops = {
  399. .type = &nft_meta_type,
  400. .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
  401. .eval = nft_meta_set_eval,
  402. .init = nft_meta_set_init,
  403. .destroy = nft_meta_set_destroy,
  404. .dump = nft_meta_set_dump,
  405. .validate = nft_meta_set_validate,
  406. };
  407. static const struct nft_expr_ops *
  408. nft_meta_select_ops(const struct nft_ctx *ctx,
  409. const struct nlattr * const tb[])
  410. {
  411. if (tb[NFTA_META_KEY] == NULL)
  412. return ERR_PTR(-EINVAL);
  413. if (tb[NFTA_META_DREG] && tb[NFTA_META_SREG])
  414. return ERR_PTR(-EINVAL);
  415. if (tb[NFTA_META_DREG])
  416. return &nft_meta_get_ops;
  417. if (tb[NFTA_META_SREG])
  418. return &nft_meta_set_ops;
  419. return ERR_PTR(-EINVAL);
  420. }
  421. static struct nft_expr_type nft_meta_type __read_mostly = {
  422. .name = "meta",
  423. .select_ops = &nft_meta_select_ops,
  424. .policy = nft_meta_policy,
  425. .maxattr = NFTA_META_MAX,
  426. .owner = THIS_MODULE,
  427. };
  428. static int __init nft_meta_module_init(void)
  429. {
  430. return nft_register_expr(&nft_meta_type);
  431. }
  432. static void __exit nft_meta_module_exit(void)
  433. {
  434. nft_unregister_expr(&nft_meta_type);
  435. }
  436. module_init(nft_meta_module_init);
  437. module_exit(nft_meta_module_exit);
  438. MODULE_LICENSE("GPL");
  439. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  440. MODULE_ALIAS_NFT_EXPR("meta");