1 /* 2 * IPv6 library code, needed by static components when full IPv6 support is 3 * not configured or static. These functions are needed by GSO/GRO implementation. 4 */ 5 #include <linux/export.h> 6 #include <net/ip.h> 7 #include <net/ipv6.h> 8 #include <net/ip6_fib.h> 9 #include <net/addrconf.h> 10 #include <net/secure_seq.h> 11 #include <linux/netfilter.h> 12 13 static u32 __ipv6_select_ident(struct net *net, u32 hashrnd, 14 const struct in6_addr *dst, 15 const struct in6_addr *src) 16 { 17 u32 hash, id; 18 19 hash = __ipv6_addr_jhash(dst, hashrnd); 20 hash = __ipv6_addr_jhash(src, hash); 21 hash ^= net_hash_mix(net); 22 23 /* Treat id of 0 as unset and if we get 0 back from ip_idents_reserve, 24 * set the hight order instead thus minimizing possible future 25 * collisions. 26 */ 27 id = ip_idents_reserve(hash, 1); 28 if (unlikely(!id)) 29 id = 1 << 31; 30 31 return id; 32 } 33 34 __be32 ipv6_select_ident(struct net *net, 35 const struct in6_addr *daddr, 36 const struct in6_addr *saddr) 37 { 38 static u32 ip6_idents_hashrnd __read_mostly; 39 u32 id; 40 41 net_get_random_once(&ip6_idents_hashrnd, sizeof(ip6_idents_hashrnd)); 42 43 id = __ipv6_select_ident(net, ip6_idents_hashrnd, daddr, saddr); 44 return htonl(id); 45 } 46 EXPORT_SYMBOL(ipv6_select_ident); 47 48 int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr) 49 { 50 unsigned int offset = sizeof(struct ipv6hdr); 51 unsigned int packet_len = skb_tail_pointer(skb) - 52 skb_network_header(skb); 53 int found_rhdr = 0; 54 *nexthdr = &ipv6_hdr(skb)->nexthdr; 55 56 while (offset <= packet_len) { 57 struct ipv6_opt_hdr *exthdr; 58 59 switch (**nexthdr) { 60 61 case NEXTHDR_HOP: 62 break; 63 case NEXTHDR_ROUTING: 64 found_rhdr = 1; 65 break; 66 case NEXTHDR_DEST: 67 #if IS_ENABLED(CONFIG_IPV6_MIP6) 68 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) 69 break; 70 #endif 71 if (found_rhdr) 72 return offset; 73 break; 74 default: 75 return offset; 76 } 77 78 if (offset + sizeof(struct ipv6_opt_hdr) > packet_len) 79 return -EINVAL; 80 81 exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) + 82 offset); 83 offset += ipv6_optlen(exthdr); 84 if (offset > IPV6_MAXPLEN) 85 return -EINVAL; 86 *nexthdr = &exthdr->nexthdr; 87 } 88 89 return -EINVAL; 90 } 91 EXPORT_SYMBOL(ip6_find_1stfragopt); 92 93 #if IS_ENABLED(CONFIG_IPV6) 94 int ip6_dst_hoplimit(struct dst_entry *dst) 95 { 96 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 97 if (hoplimit == 0) { 98 struct net_device *dev = dst->dev; 99 struct inet6_dev *idev; 100 101 rcu_read_lock(); 102 idev = __in6_dev_get(dev); 103 if (idev) 104 hoplimit = idev->cnf.hop_limit; 105 else 106 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; 107 rcu_read_unlock(); 108 } 109 return hoplimit; 110 } 111 EXPORT_SYMBOL(ip6_dst_hoplimit); 112 #endif 113 114 int __ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) 115 { 116 int len; 117 118 len = skb->len - sizeof(struct ipv6hdr); 119 if (len > IPV6_MAXPLEN) 120 len = 0; 121 ipv6_hdr(skb)->payload_len = htons(len); 122 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); 123 124 /* if egress device is enslaved to an L3 master device pass the 125 * skb to its handler for processing 126 */ 127 skb = l3mdev_ip6_out(sk, skb); 128 if (unlikely(!skb)) 129 return 0; 130 131 skb->protocol = htons(ETH_P_IPV6); 132 133 return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, 134 net, sk, skb, NULL, skb_dst(skb)->dev, 135 dst_output); 136 } 137 EXPORT_SYMBOL_GPL(__ip6_local_out); 138 139 int ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) 140 { 141 int err; 142 143 err = __ip6_local_out(net, sk, skb); 144 if (likely(err == 1)) 145 err = dst_output(net, sk, skb); 146 147 return err; 148 } 149 EXPORT_SYMBOL_GPL(ip6_local_out); 150