18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * IPv6 library code, needed by static components when full IPv6 support is 48c2ecf20Sopenharmony_ci * not configured or static. These functions are needed by GSO/GRO implementation. 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci#include <linux/export.h> 78c2ecf20Sopenharmony_ci#include <net/ip.h> 88c2ecf20Sopenharmony_ci#include <net/ipv6.h> 98c2ecf20Sopenharmony_ci#include <net/ip6_fib.h> 108c2ecf20Sopenharmony_ci#include <net/addrconf.h> 118c2ecf20Sopenharmony_ci#include <net/secure_seq.h> 128c2ecf20Sopenharmony_ci#include <linux/netfilter.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_cistatic u32 __ipv6_select_ident(struct net *net, 158c2ecf20Sopenharmony_ci const struct in6_addr *dst, 168c2ecf20Sopenharmony_ci const struct in6_addr *src) 178c2ecf20Sopenharmony_ci{ 188c2ecf20Sopenharmony_ci u32 id; 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci do { 218c2ecf20Sopenharmony_ci id = prandom_u32(); 228c2ecf20Sopenharmony_ci } while (!id); 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci return id; 258c2ecf20Sopenharmony_ci} 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci/* This function exists only for tap drivers that must support broken 288c2ecf20Sopenharmony_ci * clients requesting UFO without specifying an IPv6 fragment ID. 298c2ecf20Sopenharmony_ci * 308c2ecf20Sopenharmony_ci * This is similar to ipv6_select_ident() but we use an independent hash 318c2ecf20Sopenharmony_ci * seed to limit information leakage. 328c2ecf20Sopenharmony_ci * 338c2ecf20Sopenharmony_ci * The network header must be set before calling this. 348c2ecf20Sopenharmony_ci */ 358c2ecf20Sopenharmony_ci__be32 ipv6_proxy_select_ident(struct net *net, struct sk_buff *skb) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci struct in6_addr buf[2]; 388c2ecf20Sopenharmony_ci struct in6_addr *addrs; 398c2ecf20Sopenharmony_ci u32 id; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci addrs = skb_header_pointer(skb, 428c2ecf20Sopenharmony_ci skb_network_offset(skb) + 438c2ecf20Sopenharmony_ci offsetof(struct ipv6hdr, saddr), 448c2ecf20Sopenharmony_ci sizeof(buf), buf); 458c2ecf20Sopenharmony_ci if (!addrs) 468c2ecf20Sopenharmony_ci return 0; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci id = __ipv6_select_ident(net, &addrs[1], &addrs[0]); 498c2ecf20Sopenharmony_ci return htonl(id); 508c2ecf20Sopenharmony_ci} 518c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ipv6_proxy_select_ident); 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci__be32 ipv6_select_ident(struct net *net, 548c2ecf20Sopenharmony_ci const struct in6_addr *daddr, 558c2ecf20Sopenharmony_ci const struct in6_addr *saddr) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci u32 id; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci id = __ipv6_select_ident(net, daddr, saddr); 608c2ecf20Sopenharmony_ci return htonl(id); 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ipv6_select_ident); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ciint ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci unsigned int offset = sizeof(struct ipv6hdr); 678c2ecf20Sopenharmony_ci unsigned int packet_len = skb_tail_pointer(skb) - 688c2ecf20Sopenharmony_ci skb_network_header(skb); 698c2ecf20Sopenharmony_ci int found_rhdr = 0; 708c2ecf20Sopenharmony_ci *nexthdr = &ipv6_hdr(skb)->nexthdr; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci while (offset <= packet_len) { 738c2ecf20Sopenharmony_ci struct ipv6_opt_hdr *exthdr; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci switch (**nexthdr) { 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci case NEXTHDR_HOP: 788c2ecf20Sopenharmony_ci break; 798c2ecf20Sopenharmony_ci case NEXTHDR_ROUTING: 808c2ecf20Sopenharmony_ci found_rhdr = 1; 818c2ecf20Sopenharmony_ci break; 828c2ecf20Sopenharmony_ci case NEXTHDR_DEST: 838c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6_MIP6) 848c2ecf20Sopenharmony_ci if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) 858c2ecf20Sopenharmony_ci break; 868c2ecf20Sopenharmony_ci#endif 878c2ecf20Sopenharmony_ci if (found_rhdr) 888c2ecf20Sopenharmony_ci return offset; 898c2ecf20Sopenharmony_ci break; 908c2ecf20Sopenharmony_ci default: 918c2ecf20Sopenharmony_ci return offset; 928c2ecf20Sopenharmony_ci } 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci if (offset + sizeof(struct ipv6_opt_hdr) > packet_len) 958c2ecf20Sopenharmony_ci return -EINVAL; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) + 988c2ecf20Sopenharmony_ci offset); 998c2ecf20Sopenharmony_ci offset += ipv6_optlen(exthdr); 1008c2ecf20Sopenharmony_ci if (offset > IPV6_MAXPLEN) 1018c2ecf20Sopenharmony_ci return -EINVAL; 1028c2ecf20Sopenharmony_ci *nexthdr = &exthdr->nexthdr; 1038c2ecf20Sopenharmony_ci } 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci return -EINVAL; 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ip6_find_1stfragopt); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) 1108c2ecf20Sopenharmony_ciint ip6_dst_hoplimit(struct dst_entry *dst) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 1138c2ecf20Sopenharmony_ci if (hoplimit == 0) { 1148c2ecf20Sopenharmony_ci struct net_device *dev = dst->dev; 1158c2ecf20Sopenharmony_ci struct inet6_dev *idev; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci rcu_read_lock(); 1188c2ecf20Sopenharmony_ci idev = __in6_dev_get(dev); 1198c2ecf20Sopenharmony_ci if (idev) 1208c2ecf20Sopenharmony_ci hoplimit = idev->cnf.hop_limit; 1218c2ecf20Sopenharmony_ci else 1228c2ecf20Sopenharmony_ci hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; 1238c2ecf20Sopenharmony_ci rcu_read_unlock(); 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci return hoplimit; 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ip6_dst_hoplimit); 1288c2ecf20Sopenharmony_ci#endif 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ciint __ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci int len; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci len = skb->len - sizeof(struct ipv6hdr); 1358c2ecf20Sopenharmony_ci if (len > IPV6_MAXPLEN) 1368c2ecf20Sopenharmony_ci len = 0; 1378c2ecf20Sopenharmony_ci ipv6_hdr(skb)->payload_len = htons(len); 1388c2ecf20Sopenharmony_ci IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci /* if egress device is enslaved to an L3 master device pass the 1418c2ecf20Sopenharmony_ci * skb to its handler for processing 1428c2ecf20Sopenharmony_ci */ 1438c2ecf20Sopenharmony_ci skb = l3mdev_ip6_out(sk, skb); 1448c2ecf20Sopenharmony_ci if (unlikely(!skb)) 1458c2ecf20Sopenharmony_ci return 0; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci skb->protocol = htons(ETH_P_IPV6); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, 1508c2ecf20Sopenharmony_ci net, sk, skb, NULL, skb_dst(skb)->dev, 1518c2ecf20Sopenharmony_ci dst_output); 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__ip6_local_out); 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ciint ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) 1568c2ecf20Sopenharmony_ci{ 1578c2ecf20Sopenharmony_ci int err; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci err = __ip6_local_out(net, sk, skb); 1608c2ecf20Sopenharmony_ci if (likely(err == 1)) 1618c2ecf20Sopenharmony_ci err = dst_output(net, sk, skb); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci return err; 1648c2ecf20Sopenharmony_ci} 1658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ip6_local_out); 166