nft_reject_bridge.c 11 KB

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