nf_reject_ipv6.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* (C) 1999-2001 Paul `Rusty' Russell
  2. * (C) 2002-2004 Netfilter Core Team <coreteam@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/module.h>
  9. #include <net/ipv6.h>
  10. #include <net/ip6_route.h>
  11. #include <net/ip6_fib.h>
  12. #include <net/ip6_checksum.h>
  13. #include <net/netfilter/ipv6/nf_reject.h>
  14. #include <linux/netfilter_ipv6.h>
  15. #include <linux/netfilter_bridge.h>
  16. #include <net/netfilter/ipv6/nf_reject.h>
  17. const struct tcphdr *nf_reject_ip6_tcphdr_get(struct sk_buff *oldskb,
  18. struct tcphdr *otcph,
  19. unsigned int *otcplen, int hook)
  20. {
  21. const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
  22. u8 proto;
  23. __be16 frag_off;
  24. int tcphoff;
  25. proto = oip6h->nexthdr;
  26. tcphoff = ipv6_skip_exthdr(oldskb, ((u8 *)(oip6h + 1) - oldskb->data),
  27. &proto, &frag_off);
  28. if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
  29. pr_debug("Cannot get TCP header.\n");
  30. return NULL;
  31. }
  32. *otcplen = oldskb->len - tcphoff;
  33. /* IP header checks: fragment, too short. */
  34. if (proto != IPPROTO_TCP || *otcplen < sizeof(struct tcphdr)) {
  35. pr_debug("proto(%d) != IPPROTO_TCP or too short (len = %d)\n",
  36. proto, *otcplen);
  37. return NULL;
  38. }
  39. otcph = skb_header_pointer(oldskb, tcphoff, sizeof(struct tcphdr),
  40. otcph);
  41. if (otcph == NULL)
  42. return NULL;
  43. /* No RST for RST. */
  44. if (otcph->rst) {
  45. pr_debug("RST is set\n");
  46. return NULL;
  47. }
  48. /* Check checksum. */
  49. if (nf_ip6_checksum(oldskb, hook, tcphoff, IPPROTO_TCP)) {
  50. pr_debug("TCP checksum is invalid\n");
  51. return NULL;
  52. }
  53. return otcph;
  54. }
  55. EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_get);
  56. struct ipv6hdr *nf_reject_ip6hdr_put(struct sk_buff *nskb,
  57. const struct sk_buff *oldskb,
  58. __u8 protocol, int hoplimit)
  59. {
  60. struct ipv6hdr *ip6h;
  61. const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
  62. #define DEFAULT_TOS_VALUE 0x0U
  63. const __u8 tclass = DEFAULT_TOS_VALUE;
  64. skb_put(nskb, sizeof(struct ipv6hdr));
  65. skb_reset_network_header(nskb);
  66. ip6h = ipv6_hdr(nskb);
  67. ip6_flow_hdr(ip6h, tclass, 0);
  68. ip6h->hop_limit = hoplimit;
  69. ip6h->nexthdr = protocol;
  70. ip6h->saddr = oip6h->daddr;
  71. ip6h->daddr = oip6h->saddr;
  72. nskb->protocol = htons(ETH_P_IPV6);
  73. return ip6h;
  74. }
  75. EXPORT_SYMBOL_GPL(nf_reject_ip6hdr_put);
  76. void nf_reject_ip6_tcphdr_put(struct sk_buff *nskb,
  77. const struct sk_buff *oldskb,
  78. const struct tcphdr *oth, unsigned int otcplen)
  79. {
  80. struct tcphdr *tcph;
  81. int needs_ack;
  82. skb_reset_transport_header(nskb);
  83. tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
  84. /* Truncate to length (no data) */
  85. tcph->doff = sizeof(struct tcphdr)/4;
  86. tcph->source = oth->dest;
  87. tcph->dest = oth->source;
  88. if (oth->ack) {
  89. needs_ack = 0;
  90. tcph->seq = oth->ack_seq;
  91. tcph->ack_seq = 0;
  92. } else {
  93. needs_ack = 1;
  94. tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
  95. otcplen - (oth->doff<<2));
  96. tcph->seq = 0;
  97. }
  98. /* Reset flags */
  99. ((u_int8_t *)tcph)[13] = 0;
  100. tcph->rst = 1;
  101. tcph->ack = needs_ack;
  102. tcph->window = 0;
  103. tcph->urg_ptr = 0;
  104. tcph->check = 0;
  105. /* Adjust TCP checksum */
  106. tcph->check = csum_ipv6_magic(&ipv6_hdr(nskb)->saddr,
  107. &ipv6_hdr(nskb)->daddr,
  108. sizeof(struct tcphdr), IPPROTO_TCP,
  109. csum_partial(tcph,
  110. sizeof(struct tcphdr), 0));
  111. }
  112. EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_put);
  113. void nf_send_reset6(struct net *net, struct sk_buff *oldskb, int hook)
  114. {
  115. struct sk_buff *nskb;
  116. struct tcphdr _otcph;
  117. const struct tcphdr *otcph;
  118. unsigned int otcplen, hh_len;
  119. const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
  120. struct ipv6hdr *ip6h;
  121. struct dst_entry *dst = NULL;
  122. struct flowi6 fl6;
  123. if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
  124. (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
  125. pr_debug("addr is not unicast.\n");
  126. return;
  127. }
  128. otcph = nf_reject_ip6_tcphdr_get(oldskb, &_otcph, &otcplen, hook);
  129. if (!otcph)
  130. return;
  131. memset(&fl6, 0, sizeof(fl6));
  132. fl6.flowi6_proto = IPPROTO_TCP;
  133. fl6.saddr = oip6h->daddr;
  134. fl6.daddr = oip6h->saddr;
  135. fl6.fl6_sport = otcph->dest;
  136. fl6.fl6_dport = otcph->source;
  137. fl6.flowi6_mark = IP6_REPLY_MARK(net, oldskb->mark);
  138. security_skb_classify_flow(oldskb, flowi6_to_flowi(&fl6));
  139. dst = ip6_route_output(net, NULL, &fl6);
  140. if (dst == NULL || dst->error) {
  141. dst_release(dst);
  142. return;
  143. }
  144. dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
  145. if (IS_ERR(dst))
  146. return;
  147. hh_len = (dst->dev->hard_header_len + 15)&~15;
  148. nskb = alloc_skb(hh_len + 15 + dst->header_len + sizeof(struct ipv6hdr)
  149. + sizeof(struct tcphdr) + dst->trailer_len,
  150. GFP_ATOMIC);
  151. if (!nskb) {
  152. net_dbg_ratelimited("cannot alloc skb\n");
  153. dst_release(dst);
  154. return;
  155. }
  156. skb_dst_set(nskb, dst);
  157. nskb->mark = fl6.flowi6_mark;
  158. skb_reserve(nskb, hh_len + dst->header_len);
  159. ip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
  160. ip6_dst_hoplimit(dst));
  161. nf_reject_ip6_tcphdr_put(nskb, oldskb, otcph, otcplen);
  162. nf_ct_attach(nskb, oldskb);
  163. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  164. /* If we use ip6_local_out for bridged traffic, the MAC source on
  165. * the RST will be ours, instead of the destination's. This confuses
  166. * some routers/firewalls, and they drop the packet. So we need to
  167. * build the eth header using the original destination's MAC as the
  168. * source, and send the RST packet directly.
  169. */
  170. if (oldskb->nf_bridge) {
  171. struct ethhdr *oeth = eth_hdr(oldskb);
  172. nskb->dev = nf_bridge_get_physindev(oldskb);
  173. nskb->protocol = htons(ETH_P_IPV6);
  174. ip6h->payload_len = htons(sizeof(struct tcphdr));
  175. if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
  176. oeth->h_source, oeth->h_dest, nskb->len) < 0)
  177. return;
  178. dev_queue_xmit(nskb);
  179. } else
  180. #endif
  181. ip6_local_out(net, nskb->sk, nskb);
  182. }
  183. EXPORT_SYMBOL_GPL(nf_send_reset6);
  184. static bool reject6_csum_ok(struct sk_buff *skb, int hook)
  185. {
  186. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  187. int thoff;
  188. __be16 fo;
  189. u8 proto;
  190. if (skb->csum_bad)
  191. return false;
  192. if (skb_csum_unnecessary(skb))
  193. return true;
  194. proto = ip6h->nexthdr;
  195. thoff = ipv6_skip_exthdr(skb, ((u8 *)(ip6h + 1) - skb->data), &proto, &fo);
  196. if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
  197. return false;
  198. return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
  199. }
  200. void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
  201. unsigned char code, unsigned int hooknum)
  202. {
  203. if (!reject6_csum_ok(skb_in, hooknum))
  204. return;
  205. if (hooknum == NF_INET_LOCAL_OUT && skb_in->dev == NULL)
  206. skb_in->dev = net->loopback_dev;
  207. icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0);
  208. }
  209. EXPORT_SYMBOL_GPL(nf_send_unreach6);
  210. MODULE_LICENSE("GPL");