18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * xfrm6_input.c: based on net/ipv4/xfrm4_input.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Authors: 68c2ecf20Sopenharmony_ci * Mitsuru KANDA @USAGI 78c2ecf20Sopenharmony_ci * Kazunori MIYAZAWA @USAGI 88c2ecf20Sopenharmony_ci * Kunihiro Ishiguro <kunihiro@ipinfusion.com> 98c2ecf20Sopenharmony_ci * YOSHIFUJI Hideaki @USAGI 108c2ecf20Sopenharmony_ci * IPv6 support 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/module.h> 148c2ecf20Sopenharmony_ci#include <linux/string.h> 158c2ecf20Sopenharmony_ci#include <linux/netfilter.h> 168c2ecf20Sopenharmony_ci#include <linux/netfilter_ipv6.h> 178c2ecf20Sopenharmony_ci#include <net/ipv6.h> 188c2ecf20Sopenharmony_ci#include <net/xfrm.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ciint xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi, 218c2ecf20Sopenharmony_ci struct ip6_tnl *t) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t; 248c2ecf20Sopenharmony_ci XFRM_SPI_SKB_CB(skb)->family = AF_INET6; 258c2ecf20Sopenharmony_ci XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr); 268c2ecf20Sopenharmony_ci return xfrm_input(skb, nexthdr, spi, 0); 278c2ecf20Sopenharmony_ci} 288c2ecf20Sopenharmony_ciEXPORT_SYMBOL(xfrm6_rcv_spi); 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic int xfrm6_transport_finish2(struct net *net, struct sock *sk, 318c2ecf20Sopenharmony_ci struct sk_buff *skb) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci if (xfrm_trans_queue(skb, ip6_rcv_finish)) { 348c2ecf20Sopenharmony_ci kfree_skb(skb); 358c2ecf20Sopenharmony_ci return NET_RX_DROP; 368c2ecf20Sopenharmony_ci } 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci return 0; 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ciint xfrm6_transport_finish(struct sk_buff *skb, int async) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci struct xfrm_offload *xo = xfrm_offload(skb); 448c2ecf20Sopenharmony_ci int nhlen = skb->data - skb_network_header(skb); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci skb_network_header(skb)[IP6CB(skb)->nhoff] = 478c2ecf20Sopenharmony_ci XFRM_MODE_SKB_CB(skb)->protocol; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci#ifndef CONFIG_NETFILTER 508c2ecf20Sopenharmony_ci if (!async) 518c2ecf20Sopenharmony_ci return 1; 528c2ecf20Sopenharmony_ci#endif 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci __skb_push(skb, nhlen); 558c2ecf20Sopenharmony_ci ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); 568c2ecf20Sopenharmony_ci skb_postpush_rcsum(skb, skb_network_header(skb), nhlen); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci if (xo && (xo->flags & XFRM_GRO)) { 598c2ecf20Sopenharmony_ci skb_mac_header_rebuild(skb); 608c2ecf20Sopenharmony_ci skb_reset_transport_header(skb); 618c2ecf20Sopenharmony_ci return 0; 628c2ecf20Sopenharmony_ci } 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, 658c2ecf20Sopenharmony_ci dev_net(skb->dev), NULL, skb, skb->dev, NULL, 668c2ecf20Sopenharmony_ci xfrm6_transport_finish2); 678c2ecf20Sopenharmony_ci return 0; 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci/* If it's a keepalive packet, then just eat it. 718c2ecf20Sopenharmony_ci * If it's an encapsulated packet, then pass it to the 728c2ecf20Sopenharmony_ci * IPsec xfrm input. 738c2ecf20Sopenharmony_ci * Returns 0 if skb passed to xfrm or was dropped. 748c2ecf20Sopenharmony_ci * Returns >0 if skb should be passed to UDP. 758c2ecf20Sopenharmony_ci * Returns <0 if skb should be resubmitted (-ret is protocol) 768c2ecf20Sopenharmony_ci */ 778c2ecf20Sopenharmony_ciint xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci struct udp_sock *up = udp_sk(sk); 808c2ecf20Sopenharmony_ci struct udphdr *uh; 818c2ecf20Sopenharmony_ci struct ipv6hdr *ip6h; 828c2ecf20Sopenharmony_ci int len; 838c2ecf20Sopenharmony_ci int ip6hlen = sizeof(struct ipv6hdr); 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci __u8 *udpdata; 868c2ecf20Sopenharmony_ci __be32 *udpdata32; 878c2ecf20Sopenharmony_ci __u16 encap_type = up->encap_type; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci if (skb->protocol == htons(ETH_P_IP)) 908c2ecf20Sopenharmony_ci return xfrm4_udp_encap_rcv(sk, skb); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci /* if this is not encapsulated socket, then just return now */ 938c2ecf20Sopenharmony_ci if (!encap_type) 948c2ecf20Sopenharmony_ci return 1; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci /* If this is a paged skb, make sure we pull up 978c2ecf20Sopenharmony_ci * whatever data we need to look at. */ 988c2ecf20Sopenharmony_ci len = skb->len - sizeof(struct udphdr); 998c2ecf20Sopenharmony_ci if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8))) 1008c2ecf20Sopenharmony_ci return 1; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci /* Now we can get the pointers */ 1038c2ecf20Sopenharmony_ci uh = udp_hdr(skb); 1048c2ecf20Sopenharmony_ci udpdata = (__u8 *)uh + sizeof(struct udphdr); 1058c2ecf20Sopenharmony_ci udpdata32 = (__be32 *)udpdata; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci switch (encap_type) { 1088c2ecf20Sopenharmony_ci default: 1098c2ecf20Sopenharmony_ci case UDP_ENCAP_ESPINUDP: 1108c2ecf20Sopenharmony_ci /* Check if this is a keepalive packet. If so, eat it. */ 1118c2ecf20Sopenharmony_ci if (len == 1 && udpdata[0] == 0xff) { 1128c2ecf20Sopenharmony_ci goto drop; 1138c2ecf20Sopenharmony_ci } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) { 1148c2ecf20Sopenharmony_ci /* ESP Packet without Non-ESP header */ 1158c2ecf20Sopenharmony_ci len = sizeof(struct udphdr); 1168c2ecf20Sopenharmony_ci } else 1178c2ecf20Sopenharmony_ci /* Must be an IKE packet.. pass it through */ 1188c2ecf20Sopenharmony_ci return 1; 1198c2ecf20Sopenharmony_ci break; 1208c2ecf20Sopenharmony_ci case UDP_ENCAP_ESPINUDP_NON_IKE: 1218c2ecf20Sopenharmony_ci /* Check if this is a keepalive packet. If so, eat it. */ 1228c2ecf20Sopenharmony_ci if (len == 1 && udpdata[0] == 0xff) { 1238c2ecf20Sopenharmony_ci goto drop; 1248c2ecf20Sopenharmony_ci } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) && 1258c2ecf20Sopenharmony_ci udpdata32[0] == 0 && udpdata32[1] == 0) { 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci /* ESP Packet with Non-IKE marker */ 1288c2ecf20Sopenharmony_ci len = sizeof(struct udphdr) + 2 * sizeof(u32); 1298c2ecf20Sopenharmony_ci } else 1308c2ecf20Sopenharmony_ci /* Must be an IKE packet.. pass it through */ 1318c2ecf20Sopenharmony_ci return 1; 1328c2ecf20Sopenharmony_ci break; 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci /* At this point we are sure that this is an ESPinUDP packet, 1368c2ecf20Sopenharmony_ci * so we need to remove 'len' bytes from the packet (the UDP 1378c2ecf20Sopenharmony_ci * header and optional ESP marker bytes) and then modify the 1388c2ecf20Sopenharmony_ci * protocol to ESP, and then call into the transform receiver. 1398c2ecf20Sopenharmony_ci */ 1408c2ecf20Sopenharmony_ci if (skb_unclone(skb, GFP_ATOMIC)) 1418c2ecf20Sopenharmony_ci goto drop; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci /* Now we can update and verify the packet length... */ 1448c2ecf20Sopenharmony_ci ip6h = ipv6_hdr(skb); 1458c2ecf20Sopenharmony_ci ip6h->payload_len = htons(ntohs(ip6h->payload_len) - len); 1468c2ecf20Sopenharmony_ci if (skb->len < ip6hlen + len) { 1478c2ecf20Sopenharmony_ci /* packet is too small!?! */ 1488c2ecf20Sopenharmony_ci goto drop; 1498c2ecf20Sopenharmony_ci } 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci /* pull the data buffer up to the ESP header and set the 1528c2ecf20Sopenharmony_ci * transport header to point to ESP. Keep UDP on the stack 1538c2ecf20Sopenharmony_ci * for later. 1548c2ecf20Sopenharmony_ci */ 1558c2ecf20Sopenharmony_ci __skb_pull(skb, len); 1568c2ecf20Sopenharmony_ci skb_reset_transport_header(skb); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci /* process ESP */ 1598c2ecf20Sopenharmony_ci return xfrm6_rcv_encap(skb, IPPROTO_ESP, 0, encap_type); 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cidrop: 1628c2ecf20Sopenharmony_ci kfree_skb(skb); 1638c2ecf20Sopenharmony_ci return 0; 1648c2ecf20Sopenharmony_ci} 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ciint xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t) 1678c2ecf20Sopenharmony_ci{ 1688c2ecf20Sopenharmony_ci return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff], 1698c2ecf20Sopenharmony_ci 0, t); 1708c2ecf20Sopenharmony_ci} 1718c2ecf20Sopenharmony_ciEXPORT_SYMBOL(xfrm6_rcv_tnl); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ciint xfrm6_rcv(struct sk_buff *skb) 1748c2ecf20Sopenharmony_ci{ 1758c2ecf20Sopenharmony_ci return xfrm6_rcv_tnl(skb, NULL); 1768c2ecf20Sopenharmony_ci} 1778c2ecf20Sopenharmony_ciEXPORT_SYMBOL(xfrm6_rcv); 1788c2ecf20Sopenharmony_ciint xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr, 1798c2ecf20Sopenharmony_ci xfrm_address_t *saddr, u8 proto) 1808c2ecf20Sopenharmony_ci{ 1818c2ecf20Sopenharmony_ci struct net *net = dev_net(skb->dev); 1828c2ecf20Sopenharmony_ci struct xfrm_state *x = NULL; 1838c2ecf20Sopenharmony_ci struct sec_path *sp; 1848c2ecf20Sopenharmony_ci int i = 0; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci sp = secpath_set(skb); 1878c2ecf20Sopenharmony_ci if (!sp) { 1888c2ecf20Sopenharmony_ci XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR); 1898c2ecf20Sopenharmony_ci goto drop; 1908c2ecf20Sopenharmony_ci } 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci if (1 + sp->len == XFRM_MAX_DEPTH) { 1938c2ecf20Sopenharmony_ci XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR); 1948c2ecf20Sopenharmony_ci goto drop; 1958c2ecf20Sopenharmony_ci } 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci for (i = 0; i < 3; i++) { 1988c2ecf20Sopenharmony_ci xfrm_address_t *dst, *src; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci switch (i) { 2018c2ecf20Sopenharmony_ci case 0: 2028c2ecf20Sopenharmony_ci dst = daddr; 2038c2ecf20Sopenharmony_ci src = saddr; 2048c2ecf20Sopenharmony_ci break; 2058c2ecf20Sopenharmony_ci case 1: 2068c2ecf20Sopenharmony_ci /* lookup state with wild-card source address */ 2078c2ecf20Sopenharmony_ci dst = daddr; 2088c2ecf20Sopenharmony_ci src = (xfrm_address_t *)&in6addr_any; 2098c2ecf20Sopenharmony_ci break; 2108c2ecf20Sopenharmony_ci default: 2118c2ecf20Sopenharmony_ci /* lookup state with wild-card addresses */ 2128c2ecf20Sopenharmony_ci dst = (xfrm_address_t *)&in6addr_any; 2138c2ecf20Sopenharmony_ci src = (xfrm_address_t *)&in6addr_any; 2148c2ecf20Sopenharmony_ci break; 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6); 2188c2ecf20Sopenharmony_ci if (!x) 2198c2ecf20Sopenharmony_ci continue; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci spin_lock(&x->lock); 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) && 2248c2ecf20Sopenharmony_ci likely(x->km.state == XFRM_STATE_VALID) && 2258c2ecf20Sopenharmony_ci !xfrm_state_check_expire(x)) { 2268c2ecf20Sopenharmony_ci spin_unlock(&x->lock); 2278c2ecf20Sopenharmony_ci if (x->type->input(x, skb) > 0) { 2288c2ecf20Sopenharmony_ci /* found a valid state */ 2298c2ecf20Sopenharmony_ci break; 2308c2ecf20Sopenharmony_ci } 2318c2ecf20Sopenharmony_ci } else 2328c2ecf20Sopenharmony_ci spin_unlock(&x->lock); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci xfrm_state_put(x); 2358c2ecf20Sopenharmony_ci x = NULL; 2368c2ecf20Sopenharmony_ci } 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci if (!x) { 2398c2ecf20Sopenharmony_ci XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES); 2408c2ecf20Sopenharmony_ci xfrm_audit_state_notfound_simple(skb, AF_INET6); 2418c2ecf20Sopenharmony_ci goto drop; 2428c2ecf20Sopenharmony_ci } 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci sp->xvec[sp->len++] = x; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci spin_lock(&x->lock); 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci x->curlft.bytes += skb->len; 2498c2ecf20Sopenharmony_ci x->curlft.packets++; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci spin_unlock(&x->lock); 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci return 1; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_cidrop: 2568c2ecf20Sopenharmony_ci return -1; 2578c2ecf20Sopenharmony_ci} 2588c2ecf20Sopenharmony_ciEXPORT_SYMBOL(xfrm6_input_addr); 259