nft_meta.c 9.2 KB

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