nft_reject_bridge.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (c) 2014 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. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/netlink.h>
  12. #include <linux/netfilter.h>
  13. #include <linux/netfilter/nf_tables.h>
  14. #include <net/netfilter/nf_tables.h>
  15. #include <net/netfilter/nft_reject.h>
  16. #include <net/netfilter/nf_tables_bridge.h>
  17. #include <net/netfilter/ipv4/nf_reject.h>
  18. #include <net/netfilter/ipv6/nf_reject.h>
  19. #include <linux/ip.h>
  20. #include <net/ip.h>
  21. #include <net/ip6_checksum.h>
  22. #include <linux/netfilter_bridge.h>
  23. #include <linux/netfilter_ipv6.h>
  24. #include "../br_private.h"
  25. static void nft_reject_br_push_etherhdr(struct sk_buff *oldskb,
  26. struct sk_buff *nskb)
  27. {
  28. struct ethhdr *eth;
  29. eth = (struct ethhdr *)skb_push(nskb, ETH_HLEN);
  30. skb_reset_mac_header(nskb);
  31. ether_addr_copy(eth->h_source, eth_hdr(oldskb)->h_dest);
  32. ether_addr_copy(eth->h_dest, eth_hdr(oldskb)->h_source);
  33. eth->h_proto = eth_hdr(oldskb)->h_proto;
  34. skb_pull(nskb, ETH_HLEN);
  35. }
  36. /* We cannot use oldskb->dev, it can be either bridge device (NF_BRIDGE INPUT)
  37. * or the bridge port (NF_BRIDGE PREROUTING).
  38. */
  39. static void nft_reject_br_send_v4_tcp_reset(struct sk_buff *oldskb,
  40. const struct net_device *dev,
  41. int hook)
  42. {
  43. struct sk_buff *nskb;
  44. struct iphdr *niph;
  45. const struct tcphdr *oth;
  46. struct tcphdr _oth;
  47. if (!nft_bridge_iphdr_validate(oldskb))
  48. return;
  49. oth = nf_reject_ip_tcphdr_get(oldskb, &_oth, hook);
  50. if (!oth)
  51. return;
  52. nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
  53. LL_MAX_HEADER, GFP_ATOMIC);
  54. if (!nskb)
  55. return;
  56. skb_reserve(nskb, LL_MAX_HEADER);
  57. niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_TCP,
  58. sysctl_ip_default_ttl);
  59. nf_reject_ip_tcphdr_put(nskb, oldskb, oth);
  60. niph->ttl = sysctl_ip_default_ttl;
  61. niph->tot_len = htons(nskb->len);
  62. ip_send_check(niph);
  63. nft_reject_br_push_etherhdr(oldskb, nskb);
  64. br_deliver(br_port_get_rcu(dev), nskb);
  65. }
  66. static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb,
  67. const struct net_device *dev,
  68. int hook, u8 code)
  69. {
  70. struct sk_buff *nskb;
  71. struct iphdr *niph;
  72. struct icmphdr *icmph;
  73. unsigned int len;
  74. void *payload;
  75. __wsum csum;
  76. u8 proto;
  77. if (oldskb->csum_bad || !nft_bridge_iphdr_validate(oldskb))
  78. return;
  79. /* IP header checks: fragment. */
  80. if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
  81. return;
  82. /* RFC says return as much as we can without exceeding 576 bytes. */
  83. len = min_t(unsigned int, 536, oldskb->len);
  84. if (!pskb_may_pull(oldskb, len))
  85. return;
  86. if (pskb_trim_rcsum(oldskb, ntohs(ip_hdr(oldskb)->tot_len)))
  87. return;
  88. if (ip_hdr(oldskb)->protocol == IPPROTO_TCP ||
  89. ip_hdr(oldskb)->protocol == IPPROTO_UDP)
  90. proto = ip_hdr(oldskb)->protocol;
  91. else
  92. proto = 0;
  93. if (!skb_csum_unnecessary(oldskb) &&
  94. nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), proto))
  95. return;
  96. nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmphdr) +
  97. LL_MAX_HEADER + len, GFP_ATOMIC);
  98. if (!nskb)
  99. return;
  100. skb_reserve(nskb, LL_MAX_HEADER);
  101. niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_ICMP,
  102. sysctl_ip_default_ttl);
  103. skb_reset_transport_header(nskb);
  104. icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
  105. memset(icmph, 0, sizeof(*icmph));
  106. icmph->type = ICMP_DEST_UNREACH;
  107. icmph->code = code;
  108. payload = skb_put(nskb, len);
  109. memcpy(payload, skb_network_header(oldskb), len);
  110. csum = csum_partial((void *)icmph, len + sizeof(struct icmphdr), 0);
  111. icmph->checksum = csum_fold(csum);
  112. niph->tot_len = htons(nskb->len);
  113. ip_send_check(niph);
  114. nft_reject_br_push_etherhdr(oldskb, nskb);
  115. br_deliver(br_port_get_rcu(dev), nskb);
  116. }
  117. static void nft_reject_br_send_v6_tcp_reset(struct net *net,
  118. struct sk_buff *oldskb,
  119. const struct net_device *dev,
  120. int hook)
  121. {
  122. struct sk_buff *nskb;
  123. const struct tcphdr *oth;
  124. struct tcphdr _oth;
  125. unsigned int otcplen;
  126. struct ipv6hdr *nip6h;
  127. if (!nft_bridge_ip6hdr_validate(oldskb))
  128. return;
  129. oth = nf_reject_ip6_tcphdr_get(oldskb, &_oth, &otcplen, hook);
  130. if (!oth)
  131. return;
  132. nskb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(struct tcphdr) +
  133. LL_MAX_HEADER, GFP_ATOMIC);
  134. if (!nskb)
  135. return;
  136. skb_reserve(nskb, LL_MAX_HEADER);
  137. nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
  138. net->ipv6.devconf_all->hop_limit);
  139. nf_reject_ip6_tcphdr_put(nskb, oldskb, oth, otcplen);
  140. nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
  141. nft_reject_br_push_etherhdr(oldskb, nskb);
  142. br_deliver(br_port_get_rcu(dev), nskb);
  143. }
  144. static bool reject6_br_csum_ok(struct sk_buff *skb, int hook)
  145. {
  146. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  147. int thoff;
  148. __be16 fo;
  149. u8 proto = ip6h->nexthdr;
  150. if (skb->csum_bad)
  151. return false;
  152. if (skb_csum_unnecessary(skb))
  153. return true;
  154. if (ip6h->payload_len &&
  155. pskb_trim_rcsum(skb, ntohs(ip6h->payload_len) + sizeof(*ip6h)))
  156. return false;
  157. ip6h = ipv6_hdr(skb);
  158. thoff = ipv6_skip_exthdr(skb, ((u8*)(ip6h+1) - skb->data), &proto, &fo);
  159. if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
  160. return false;
  161. return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
  162. }
  163. static void nft_reject_br_send_v6_unreach(struct net *net,
  164. struct sk_buff *oldskb,
  165. const struct net_device *dev,
  166. int hook, u8 code)
  167. {
  168. struct sk_buff *nskb;
  169. struct ipv6hdr *nip6h;
  170. struct icmp6hdr *icmp6h;
  171. unsigned int len;
  172. void *payload;
  173. if (!nft_bridge_ip6hdr_validate(oldskb))
  174. return;
  175. /* Include "As much of invoking packet as possible without the ICMPv6
  176. * packet exceeding the minimum IPv6 MTU" in the ICMP payload.
  177. */
  178. len = min_t(unsigned int, 1220, oldskb->len);
  179. if (!pskb_may_pull(oldskb, len))
  180. return;
  181. if (!reject6_br_csum_ok(oldskb, hook))
  182. return;
  183. nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmp6hdr) +
  184. LL_MAX_HEADER + len, GFP_ATOMIC);
  185. if (!nskb)
  186. return;
  187. skb_reserve(nskb, LL_MAX_HEADER);
  188. nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_ICMPV6,
  189. net->ipv6.devconf_all->hop_limit);
  190. skb_reset_transport_header(nskb);
  191. icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
  192. memset(icmp6h, 0, sizeof(*icmp6h));
  193. icmp6h->icmp6_type = ICMPV6_DEST_UNREACH;
  194. icmp6h->icmp6_code = code;
  195. payload = skb_put(nskb, len);
  196. memcpy(payload, skb_network_header(oldskb), len);
  197. nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
  198. icmp6h->icmp6_cksum =
  199. csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr,
  200. nskb->len - sizeof(struct ipv6hdr),
  201. IPPROTO_ICMPV6,
  202. csum_partial(icmp6h,
  203. nskb->len - sizeof(struct ipv6hdr),
  204. 0));
  205. nft_reject_br_push_etherhdr(oldskb, nskb);
  206. br_deliver(br_port_get_rcu(dev), nskb);
  207. }
  208. static void nft_reject_bridge_eval(const struct nft_expr *expr,
  209. struct nft_regs *regs,
  210. const struct nft_pktinfo *pkt)
  211. {
  212. struct nft_reject *priv = nft_expr_priv(expr);
  213. const unsigned char *dest = eth_hdr(pkt->skb)->h_dest;
  214. if (is_broadcast_ether_addr(dest) ||
  215. is_multicast_ether_addr(dest))
  216. goto out;
  217. switch (eth_hdr(pkt->skb)->h_proto) {
  218. case htons(ETH_P_IP):
  219. switch (priv->type) {
  220. case NFT_REJECT_ICMP_UNREACH:
  221. nft_reject_br_send_v4_unreach(pkt->skb, pkt->in,
  222. pkt->hook,
  223. priv->icmp_code);
  224. break;
  225. case NFT_REJECT_TCP_RST:
  226. nft_reject_br_send_v4_tcp_reset(pkt->skb, pkt->in,
  227. pkt->hook);
  228. break;
  229. case NFT_REJECT_ICMPX_UNREACH:
  230. nft_reject_br_send_v4_unreach(pkt->skb, pkt->in,
  231. pkt->hook,
  232. nft_reject_icmp_code(priv->icmp_code));
  233. break;
  234. }
  235. break;
  236. case htons(ETH_P_IPV6):
  237. switch (priv->type) {
  238. case NFT_REJECT_ICMP_UNREACH:
  239. nft_reject_br_send_v6_unreach(pkt->net, pkt->skb,
  240. pkt->in, pkt->hook,
  241. priv->icmp_code);
  242. break;
  243. case NFT_REJECT_TCP_RST:
  244. nft_reject_br_send_v6_tcp_reset(pkt->net, pkt->skb,
  245. pkt->in, pkt->hook);
  246. break;
  247. case NFT_REJECT_ICMPX_UNREACH:
  248. nft_reject_br_send_v6_unreach(pkt->net, pkt->skb,
  249. pkt->in, pkt->hook,
  250. nft_reject_icmpv6_code(priv->icmp_code));
  251. break;
  252. }
  253. break;
  254. default:
  255. /* No explicit way to reject this protocol, drop it. */
  256. break;
  257. }
  258. out:
  259. regs->verdict.code = NF_DROP;
  260. }
  261. static int nft_reject_bridge_validate(const struct nft_ctx *ctx,
  262. const struct nft_expr *expr,
  263. const struct nft_data **data)
  264. {
  265. return nft_chain_validate_hooks(ctx->chain, (1 << NF_BR_PRE_ROUTING) |
  266. (1 << NF_BR_LOCAL_IN));
  267. }
  268. static int nft_reject_bridge_init(const struct nft_ctx *ctx,
  269. const struct nft_expr *expr,
  270. const struct nlattr * const tb[])
  271. {
  272. struct nft_reject *priv = nft_expr_priv(expr);
  273. int icmp_code, err;
  274. err = nft_reject_bridge_validate(ctx, expr, NULL);
  275. if (err < 0)
  276. return err;
  277. if (tb[NFTA_REJECT_TYPE] == NULL)
  278. return -EINVAL;
  279. priv->type = ntohl(nla_get_be32(tb[NFTA_REJECT_TYPE]));
  280. switch (priv->type) {
  281. case NFT_REJECT_ICMP_UNREACH:
  282. case NFT_REJECT_ICMPX_UNREACH:
  283. if (tb[NFTA_REJECT_ICMP_CODE] == NULL)
  284. return -EINVAL;
  285. icmp_code = nla_get_u8(tb[NFTA_REJECT_ICMP_CODE]);
  286. if (priv->type == NFT_REJECT_ICMPX_UNREACH &&
  287. icmp_code > NFT_REJECT_ICMPX_MAX)
  288. return -EINVAL;
  289. priv->icmp_code = icmp_code;
  290. break;
  291. case NFT_REJECT_TCP_RST:
  292. break;
  293. default:
  294. return -EINVAL;
  295. }
  296. return 0;
  297. }
  298. static int nft_reject_bridge_dump(struct sk_buff *skb,
  299. const struct nft_expr *expr)
  300. {
  301. const struct nft_reject *priv = nft_expr_priv(expr);
  302. if (nla_put_be32(skb, NFTA_REJECT_TYPE, htonl(priv->type)))
  303. goto nla_put_failure;
  304. switch (priv->type) {
  305. case NFT_REJECT_ICMP_UNREACH:
  306. case NFT_REJECT_ICMPX_UNREACH:
  307. if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code))
  308. goto nla_put_failure;
  309. break;
  310. default:
  311. break;
  312. }
  313. return 0;
  314. nla_put_failure:
  315. return -1;
  316. }
  317. static struct nft_expr_type nft_reject_bridge_type;
  318. static const struct nft_expr_ops nft_reject_bridge_ops = {
  319. .type = &nft_reject_bridge_type,
  320. .size = NFT_EXPR_SIZE(sizeof(struct nft_reject)),
  321. .eval = nft_reject_bridge_eval,
  322. .init = nft_reject_bridge_init,
  323. .dump = nft_reject_bridge_dump,
  324. .validate = nft_reject_bridge_validate,
  325. };
  326. static struct nft_expr_type nft_reject_bridge_type __read_mostly = {
  327. .family = NFPROTO_BRIDGE,
  328. .name = "reject",
  329. .ops = &nft_reject_bridge_ops,
  330. .policy = nft_reject_policy,
  331. .maxattr = NFTA_REJECT_MAX,
  332. .owner = THIS_MODULE,
  333. };
  334. static int __init nft_reject_bridge_module_init(void)
  335. {
  336. return nft_register_expr(&nft_reject_bridge_type);
  337. }
  338. static void __exit nft_reject_bridge_module_exit(void)
  339. {
  340. nft_unregister_expr(&nft_reject_bridge_type);
  341. }
  342. module_init(nft_reject_bridge_module_init);
  343. module_exit(nft_reject_bridge_module_exit);
  344. MODULE_LICENSE("GPL");
  345. MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
  346. MODULE_ALIAS_NFT_AF_EXPR(AF_BRIDGE, "reject");