• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
12 /* This function exists only for tap drivers that must support broken
13  * clients requesting UFO without specifying an IPv6 fragment ID.
14  *
15  * This is similar to ipv6_select_ident() but we use an independent hash
16  * seed to limit information leakage.
17  *
18  * The network header must be set before calling this.
19  */
ipv6_proxy_select_ident(struct sk_buff * skb)20 void ipv6_proxy_select_ident(struct sk_buff *skb)
21 {
22 	static u32 ip6_proxy_idents_hashrnd __read_mostly;
23 	struct in6_addr buf[2];
24 	struct in6_addr *addrs;
25 	u32 hash, id;
26 
27 	addrs = skb_header_pointer(skb,
28 				   skb_network_offset(skb) +
29 				   offsetof(struct ipv6hdr, saddr),
30 				   sizeof(buf), buf);
31 	if (!addrs)
32 		return;
33 
34 	net_get_random_once(&ip6_proxy_idents_hashrnd,
35 			    sizeof(ip6_proxy_idents_hashrnd));
36 
37 	hash = __ipv6_addr_jhash(&addrs[1], ip6_proxy_idents_hashrnd);
38 	hash = __ipv6_addr_jhash(&addrs[0], hash);
39 
40 	id = ip_idents_reserve(hash, 1);
41 	skb_shinfo(skb)->ip6_frag_id = htonl(id);
42 }
43 EXPORT_SYMBOL_GPL(ipv6_proxy_select_ident);
44 
ip6_find_1stfragopt(struct sk_buff * skb,u8 ** nexthdr)45 int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
46 {
47 	unsigned int offset = sizeof(struct ipv6hdr);
48 	unsigned int packet_len = skb_tail_pointer(skb) -
49 		skb_network_header(skb);
50 	int found_rhdr = 0;
51 	*nexthdr = &ipv6_hdr(skb)->nexthdr;
52 
53 	while (offset <= packet_len) {
54 		struct ipv6_opt_hdr *exthdr;
55 
56 		switch (**nexthdr) {
57 
58 		case NEXTHDR_HOP:
59 			break;
60 		case NEXTHDR_ROUTING:
61 			found_rhdr = 1;
62 			break;
63 		case NEXTHDR_DEST:
64 #if IS_ENABLED(CONFIG_IPV6_MIP6)
65 			if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
66 				break;
67 #endif
68 			if (found_rhdr)
69 				return offset;
70 			break;
71 		default:
72 			return offset;
73 		}
74 
75 		if (offset + sizeof(struct ipv6_opt_hdr) > packet_len)
76 			return -EINVAL;
77 
78 		exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
79 						 offset);
80 		offset += ipv6_optlen(exthdr);
81 		if (offset > IPV6_MAXPLEN)
82 			return -EINVAL;
83 		*nexthdr = &exthdr->nexthdr;
84 	}
85 
86 	return -EINVAL;
87 }
88 EXPORT_SYMBOL(ip6_find_1stfragopt);
89 
90 #if IS_ENABLED(CONFIG_IPV6)
ip6_dst_hoplimit(struct dst_entry * dst)91 int ip6_dst_hoplimit(struct dst_entry *dst)
92 {
93 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
94 	if (hoplimit == 0) {
95 		struct net_device *dev = dst->dev;
96 		struct inet6_dev *idev;
97 
98 		rcu_read_lock();
99 		idev = __in6_dev_get(dev);
100 		if (idev)
101 			hoplimit = idev->cnf.hop_limit;
102 		else
103 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
104 		rcu_read_unlock();
105 	}
106 	return hoplimit;
107 }
108 EXPORT_SYMBOL(ip6_dst_hoplimit);
109 #endif
110 
__ip6_local_out(struct sk_buff * skb)111 int __ip6_local_out(struct sk_buff *skb)
112 {
113 	int len;
114 
115 	len = skb->len - sizeof(struct ipv6hdr);
116 	if (len > IPV6_MAXPLEN)
117 		len = 0;
118 	ipv6_hdr(skb)->payload_len = htons(len);
119 	IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
120 
121 	return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
122 		       skb_dst(skb)->dev, dst_output);
123 }
124 EXPORT_SYMBOL_GPL(__ip6_local_out);
125 
ip6_local_out(struct sk_buff * skb)126 int ip6_local_out(struct sk_buff *skb)
127 {
128 	int err;
129 
130 	err = __ip6_local_out(skb);
131 	if (likely(err == 1))
132 		err = dst_output(skb);
133 
134 	return err;
135 }
136 EXPORT_SYMBOL_GPL(ip6_local_out);
137