xfrm6_input.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
  4. *
  5. * Authors:
  6. * Mitsuru KANDA @USAGI
  7. * Kazunori MIYAZAWA @USAGI
  8. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  9. * YOSHIFUJI Hideaki @USAGI
  10. * IPv6 support
  11. */
  12. #include <linux/module.h>
  13. #include <linux/string.h>
  14. #include <linux/netfilter.h>
  15. #include <linux/netfilter_ipv6.h>
  16. #include <net/ipv6.h>
  17. #include <net/xfrm.h>
  18. #include <net/protocol.h>
  19. #include <net/gro.h>
  20. int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
  21. struct ip6_tnl *t)
  22. {
  23. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
  24. XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
  25. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
  26. return xfrm_input(skb, nexthdr, spi, 0);
  27. }
  28. EXPORT_SYMBOL(xfrm6_rcv_spi);
  29. static int xfrm6_transport_finish2(struct net *net, struct sock *sk,
  30. struct sk_buff *skb)
  31. {
  32. if (xfrm_trans_queue(skb, ip6_rcv_finish)) {
  33. kfree_skb(skb);
  34. return NET_RX_DROP;
  35. }
  36. return 0;
  37. }
  38. int xfrm6_transport_finish(struct sk_buff *skb, int async)
  39. {
  40. struct xfrm_offload *xo = xfrm_offload(skb);
  41. struct net_device *dev = skb->dev;
  42. int nhlen = -skb_network_offset(skb);
  43. skb_network_header(skb)[IP6CB(skb)->nhoff] =
  44. XFRM_MODE_SKB_CB(skb)->protocol;
  45. #ifndef CONFIG_NETFILTER
  46. if (!async)
  47. return 1;
  48. #endif
  49. __skb_push(skb, nhlen);
  50. ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  51. skb_postpush_rcsum(skb, skb_network_header(skb), nhlen);
  52. if (xo && (xo->flags & XFRM_GRO)) {
  53. /* The full l2 header needs to be preserved so that re-injecting the packet at l2
  54. * works correctly in the presence of vlan tags.
  55. */
  56. skb_mac_header_rebuild_full(skb, xo->orig_mac_len);
  57. skb_reset_network_header(skb);
  58. skb_reset_transport_header(skb);
  59. return 0;
  60. }
  61. NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
  62. dev_net(dev), NULL, skb, dev, NULL,
  63. xfrm6_transport_finish2);
  64. if (async)
  65. dev_put(dev);
  66. return 0;
  67. }
  68. static int __xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb, bool pull)
  69. {
  70. struct udp_sock *up = udp_sk(sk);
  71. struct udphdr *uh;
  72. struct ipv6hdr *ip6h;
  73. int len;
  74. int ip6hlen = sizeof(struct ipv6hdr);
  75. __u8 *udpdata;
  76. __be32 *udpdata32;
  77. u16 encap_type;
  78. encap_type = READ_ONCE(up->encap_type);
  79. /* if this is not encapsulated socket, then just return now */
  80. if (!encap_type)
  81. return 1;
  82. /* If this is a paged skb, make sure we pull up
  83. * whatever data we need to look at. */
  84. len = skb->len - sizeof(struct udphdr);
  85. if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
  86. return 1;
  87. /* Now we can get the pointers */
  88. uh = udp_hdr(skb);
  89. udpdata = (__u8 *)uh + sizeof(struct udphdr);
  90. udpdata32 = (__be32 *)udpdata;
  91. switch (encap_type) {
  92. default:
  93. case UDP_ENCAP_ESPINUDP:
  94. /* Check if this is a keepalive packet. If so, eat it. */
  95. if (len == 1 && udpdata[0] == 0xff) {
  96. return -EINVAL;
  97. } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
  98. /* ESP Packet without Non-ESP header */
  99. len = sizeof(struct udphdr);
  100. } else
  101. /* Must be an IKE packet.. pass it through */
  102. return 1;
  103. break;
  104. }
  105. /* At this point we are sure that this is an ESPinUDP packet,
  106. * so we need to remove 'len' bytes from the packet (the UDP
  107. * header and optional ESP marker bytes) and then modify the
  108. * protocol to ESP, and then call into the transform receiver.
  109. */
  110. if (skb_unclone(skb, GFP_ATOMIC))
  111. return -EINVAL;
  112. /* Now we can update and verify the packet length... */
  113. ip6h = ipv6_hdr(skb);
  114. ip6h->payload_len = htons(ntohs(ip6h->payload_len) - len);
  115. if (skb->len < ip6hlen + len) {
  116. /* packet is too small!?! */
  117. return -EINVAL;
  118. }
  119. /* pull the data buffer up to the ESP header and set the
  120. * transport header to point to ESP. Keep UDP on the stack
  121. * for later.
  122. */
  123. if (pull) {
  124. __skb_pull(skb, len);
  125. skb_reset_transport_header(skb);
  126. } else {
  127. skb_set_transport_header(skb, len);
  128. }
  129. /* process ESP */
  130. return 0;
  131. }
  132. /* If it's a keepalive packet, then just eat it.
  133. * If it's an encapsulated packet, then pass it to the
  134. * IPsec xfrm input.
  135. * Returns 0 if skb passed to xfrm or was dropped.
  136. * Returns >0 if skb should be passed to UDP.
  137. * Returns <0 if skb should be resubmitted (-ret is protocol)
  138. */
  139. int xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
  140. {
  141. int ret;
  142. if (skb->protocol == htons(ETH_P_IP))
  143. return xfrm4_udp_encap_rcv(sk, skb);
  144. ret = __xfrm6_udp_encap_rcv(sk, skb, true);
  145. if (!ret)
  146. return xfrm6_rcv_encap(skb, IPPROTO_ESP, 0,
  147. udp_sk(sk)->encap_type);
  148. if (ret < 0) {
  149. kfree_skb(skb);
  150. return 0;
  151. }
  152. return ret;
  153. }
  154. struct sk_buff *xfrm6_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
  155. struct sk_buff *skb)
  156. {
  157. int offset = skb_gro_offset(skb);
  158. const struct net_offload *ops;
  159. struct sk_buff *pp = NULL;
  160. int len, dlen;
  161. __u8 *udpdata;
  162. __be32 *udpdata32;
  163. if (skb->protocol == htons(ETH_P_IP))
  164. return xfrm4_gro_udp_encap_rcv(sk, head, skb);
  165. len = skb->len - offset;
  166. dlen = offset + min(len, 8);
  167. udpdata = skb_gro_header(skb, dlen, offset);
  168. udpdata32 = (__be32 *)udpdata;
  169. if (unlikely(!udpdata))
  170. return NULL;
  171. rcu_read_lock();
  172. ops = rcu_dereference(inet6_offloads[IPPROTO_ESP]);
  173. if (!ops || !ops->callbacks.gro_receive)
  174. goto out;
  175. /* check if it is a keepalive or IKE packet */
  176. if (len <= sizeof(struct ip_esp_hdr) || udpdata32[0] == 0)
  177. goto out;
  178. /* set the transport header to ESP */
  179. skb_set_transport_header(skb, offset);
  180. NAPI_GRO_CB(skb)->proto = IPPROTO_UDP;
  181. pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
  182. rcu_read_unlock();
  183. return pp;
  184. out:
  185. rcu_read_unlock();
  186. NAPI_GRO_CB(skb)->same_flow = 0;
  187. NAPI_GRO_CB(skb)->flush = 1;
  188. return NULL;
  189. }
  190. int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t)
  191. {
  192. return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
  193. 0, t);
  194. }
  195. EXPORT_SYMBOL(xfrm6_rcv_tnl);
  196. int xfrm6_rcv(struct sk_buff *skb)
  197. {
  198. return xfrm6_rcv_tnl(skb, NULL);
  199. }
  200. EXPORT_SYMBOL(xfrm6_rcv);
  201. int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
  202. xfrm_address_t *saddr, u8 proto)
  203. {
  204. struct net *net = dev_net(skb->dev);
  205. struct xfrm_state *x = NULL;
  206. struct sec_path *sp;
  207. int i = 0;
  208. sp = secpath_set(skb);
  209. if (!sp) {
  210. XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
  211. goto drop;
  212. }
  213. if (1 + sp->len == XFRM_MAX_DEPTH) {
  214. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  215. goto drop;
  216. }
  217. for (i = 0; i < 3; i++) {
  218. xfrm_address_t *dst, *src;
  219. switch (i) {
  220. case 0:
  221. dst = daddr;
  222. src = saddr;
  223. break;
  224. case 1:
  225. /* lookup state with wild-card source address */
  226. dst = daddr;
  227. src = (xfrm_address_t *)&in6addr_any;
  228. break;
  229. default:
  230. /* lookup state with wild-card addresses */
  231. dst = (xfrm_address_t *)&in6addr_any;
  232. src = (xfrm_address_t *)&in6addr_any;
  233. break;
  234. }
  235. x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6);
  236. if (!x)
  237. continue;
  238. if (unlikely(x->dir && x->dir != XFRM_SA_DIR_IN)) {
  239. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEDIRERROR);
  240. xfrm_state_put(x);
  241. x = NULL;
  242. continue;
  243. }
  244. spin_lock(&x->lock);
  245. if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
  246. likely(x->km.state == XFRM_STATE_VALID) &&
  247. !xfrm_state_check_expire(x)) {
  248. spin_unlock(&x->lock);
  249. if (x->type->input(x, skb) > 0) {
  250. /* found a valid state */
  251. break;
  252. }
  253. } else
  254. spin_unlock(&x->lock);
  255. xfrm_state_put(x);
  256. x = NULL;
  257. }
  258. if (!x) {
  259. XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
  260. xfrm_audit_state_notfound_simple(skb, AF_INET6);
  261. goto drop;
  262. }
  263. sp->xvec[sp->len++] = x;
  264. spin_lock(&x->lock);
  265. x->curlft.bytes += skb->len;
  266. x->curlft.packets++;
  267. spin_unlock(&x->lock);
  268. return 1;
  269. drop:
  270. return -1;
  271. }
  272. EXPORT_SYMBOL(xfrm6_input_addr);