blob: 6313abd53c9d059717113ab1d4763791a6a72312 [file] [log] [blame]
Vlad Yasevich3c73a032012-11-15 08:49:20 +00001/*
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/ipv6.h>
7#include <net/ip6_fib.h>
Cong Wang3ce9b352013-08-31 13:44:28 +08008#include <net/addrconf.h>
Hannes Frederic Sowa6dfac5c2014-03-30 18:28:03 +02009#include <net/secure_seq.h>
Vlad Yasevich3c73a032012-11-15 08:49:20 +000010
11void ipv6_select_ident(struct frag_hdr *fhdr, struct rt6_info *rt)
12{
13 static atomic_t ipv6_fragmentation_id;
Hannes Frederic Sowa6dfac5c2014-03-30 18:28:03 +020014 struct in6_addr addr;
Vlad Yasevich3c73a032012-11-15 08:49:20 +000015 int old, new;
16
17#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowa6dfac5c2014-03-30 18:28:03 +020018 struct inet_peer *peer;
19 struct net *net;
Vlad Yasevich3c73a032012-11-15 08:49:20 +000020
Hannes Frederic Sowa6dfac5c2014-03-30 18:28:03 +020021 net = dev_net(rt->dst.dev);
22 peer = inet_getpeer_v6(net->ipv6.peers, &rt->rt6i_dst.addr, 1);
23 if (peer) {
24 fhdr->identification = htonl(inet_getid(peer, 0));
25 inet_putpeer(peer);
26 return;
Vlad Yasevich3c73a032012-11-15 08:49:20 +000027 }
28#endif
29 do {
30 old = atomic_read(&ipv6_fragmentation_id);
31 new = old + 1;
32 if (!new)
33 new = 1;
34 } while (atomic_cmpxchg(&ipv6_fragmentation_id, old, new) != old);
Hannes Frederic Sowa6dfac5c2014-03-30 18:28:03 +020035
36 addr = rt->rt6i_dst.addr;
37 addr.s6_addr32[0] ^= (__force __be32)new;
38 fhdr->identification = htonl(secure_ipv6_id(addr.s6_addr32));
Vlad Yasevich3c73a032012-11-15 08:49:20 +000039}
40EXPORT_SYMBOL(ipv6_select_ident);
41
42int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
43{
44 u16 offset = sizeof(struct ipv6hdr);
45 struct ipv6_opt_hdr *exthdr =
46 (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
Simon Horman29a3cad2013-05-28 20:34:26 +000047 unsigned int packet_len = skb_tail_pointer(skb) -
48 skb_network_header(skb);
Vlad Yasevich3c73a032012-11-15 08:49:20 +000049 int found_rhdr = 0;
50 *nexthdr = &ipv6_hdr(skb)->nexthdr;
51
52 while (offset + 1 <= packet_len) {
53
54 switch (**nexthdr) {
55
56 case NEXTHDR_HOP:
57 break;
58 case NEXTHDR_ROUTING:
59 found_rhdr = 1;
60 break;
61 case NEXTHDR_DEST:
62#if IS_ENABLED(CONFIG_IPV6_MIP6)
63 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
64 break;
65#endif
66 if (found_rhdr)
67 return offset;
68 break;
69 default :
70 return offset;
71 }
72
73 offset += ipv6_optlen(exthdr);
74 *nexthdr = &exthdr->nexthdr;
75 exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
76 offset);
77 }
78
79 return offset;
80}
81EXPORT_SYMBOL(ip6_find_1stfragopt);
Cong Wang3ce9b352013-08-31 13:44:28 +080082
83#if IS_ENABLED(CONFIG_IPV6)
84int ip6_dst_hoplimit(struct dst_entry *dst)
85{
86 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
87 if (hoplimit == 0) {
88 struct net_device *dev = dst->dev;
89 struct inet6_dev *idev;
90
91 rcu_read_lock();
92 idev = __in6_dev_get(dev);
93 if (idev)
94 hoplimit = idev->cnf.hop_limit;
95 else
96 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
97 rcu_read_unlock();
98 }
99 return hoplimit;
100}
101EXPORT_SYMBOL(ip6_dst_hoplimit);
102#endif
Cong Wang788787b2013-08-31 13:44:29 +0800103
104int __ip6_local_out(struct sk_buff *skb)
105{
106 int len;
107
108 len = skb->len - sizeof(struct ipv6hdr);
109 if (len > IPV6_MAXPLEN)
110 len = 0;
111 ipv6_hdr(skb)->payload_len = htons(len);
112
113 return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
114 skb_dst(skb)->dev, dst_output);
115}
116EXPORT_SYMBOL_GPL(__ip6_local_out);
117
118int ip6_local_out(struct sk_buff *skb)
119{
120 int err;
121
122 err = __ip6_local_out(skb);
123 if (likely(err == 1))
124 err = dst_output(skb);
125
126 return err;
127}
128EXPORT_SYMBOL_GPL(ip6_local_out);