blob: f005acc58b2aad4db9119e307b45d4029f2bc159 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Extension Header handling for IPv6
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Andi Kleen <ak@muc.de>
8 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16/* Changes:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090017 * yoshfuji : ensure not to overrun while parsing
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * tlv options.
19 * Mitsuru KANDA @USAGI and: Remove ipv6_parse_exthdrs().
20 * YOSHIFUJI Hideaki @USAGI Register inbound extension header
21 * handlers as inet6_protocol{}.
22 */
23
24#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/net.h>
29#include <linux/netdevice.h>
30#include <linux/in6.h>
31#include <linux/icmpv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040033#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Herbert Xu352e5122007-11-13 21:34:06 -080035#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <net/sock.h>
37#include <net/snmp.h>
38
39#include <net/ipv6.h>
40#include <net/protocol.h>
41#include <net/transp_v6.h>
42#include <net/rawv6.h>
43#include <net/ndisc.h>
44#include <net/ip6_route.h>
45#include <net/addrconf.h>
Amerigo Wang07a93622012-10-29 16:23:10 +000046#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -070047#include <net/xfrm.h>
48#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/uaccess.h>
51
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070052int ipv6_find_tlv(struct sk_buff *skb, int offset, int type)
53{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070054 const unsigned char *nh = skb_network_header(skb);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070055 int packet_len = skb->tail - skb->network_header;
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070056 struct ipv6_opt_hdr *hdr;
57 int len;
58
59 if (offset + 2 > packet_len)
60 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070061 hdr = (struct ipv6_opt_hdr *)(nh + offset);
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070062 len = ((hdr->hdrlen + 1) << 3);
63
64 if (offset + len > packet_len)
65 goto bad;
66
67 offset += 2;
68 len -= 2;
69
70 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070071 int opttype = nh[offset];
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070072 int optlen;
73
74 if (opttype == type)
75 return offset;
76
77 switch (opttype) {
Eldad Zack1de5a712012-05-17 06:00:25 +000078 case IPV6_TLV_PAD1:
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070079 optlen = 1;
80 break;
81 default:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070082 optlen = nh[offset + 1] + 2;
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070083 if (optlen > len)
84 goto bad;
85 break;
86 }
87 offset += optlen;
88 len -= optlen;
89 }
90 /* not_found */
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070091 bad:
92 return -1;
93}
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -070094EXPORT_SYMBOL_GPL(ipv6_find_tlv);
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
97 * Parsing tlv encoded headers.
98 *
Eric Dumazeta50feda2012-05-18 18:57:34 +000099 * Parsing function "func" returns true, if parsing succeed
100 * and false, if it failed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * It MUST NOT touch skb->h.
102 */
103
104struct tlvtype_proc {
105 int type;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000106 bool (*func)(struct sk_buff *skb, int offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109/*********************
110 Generic functions
111 *********************/
112
113/* An unknown option is detected, decide what to do */
114
Eric Dumazeta50feda2012-05-18 18:57:34 +0000115static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700117 switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 case 0: /* ignore */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000119 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 case 1: /* drop packet */
122 break;
123
124 case 3: /* Send ICMP if not a multicast address and drop packet */
125 /* Actually, it is redundant check. icmp_send
126 will recheck in any case.
127 */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700128 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 break;
130 case 2: /* send ICMP PARM PROB regardless and drop packet */
131 icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000132 return false;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000136 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139/* Parse tlv encoded option header (hop-by-hop or destination) */
140
Eric Dumazeta50feda2012-05-18 18:57:34 +0000141static bool ip6_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Eric Dumazeta50feda2012-05-18 18:57:34 +0000143 const struct tlvtype_proc *curr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700144 const unsigned char *nh = skb_network_header(skb);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300145 int off = skb_network_header_len(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700146 int len = (skb_transport_header(skb)[1] + 1) << 3;
Eldad Zack9b905fe2012-05-20 01:59:33 +0000147 int padlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700149 if (skb_transport_offset(skb) + len > skb_headlen(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 goto bad;
151
152 off += 2;
153 len -= 2;
154
155 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700156 int optlen = nh[off + 1] + 2;
Eldad Zackc1412fc2012-04-12 17:36:17 -0400157 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700159 switch (nh[off]) {
Eldad Zack1de5a712012-05-17 06:00:25 +0000160 case IPV6_TLV_PAD1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 optlen = 1;
Eldad Zack9b905fe2012-05-20 01:59:33 +0000162 padlen++;
163 if (padlen > 7)
164 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 break;
166
167 case IPV6_TLV_PADN:
Eldad Zackc1412fc2012-04-12 17:36:17 -0400168 /* RFC 2460 states that the purpose of PadN is
169 * to align the containing header to multiples
170 * of 8. 7 is therefore the highest valid value.
171 * See also RFC 4942, Section 2.1.9.5.
172 */
Eldad Zack9b905fe2012-05-20 01:59:33 +0000173 padlen += optlen;
174 if (padlen > 7)
Eldad Zackc1412fc2012-04-12 17:36:17 -0400175 goto bad;
176 /* RFC 4942 recommends receiving hosts to
177 * actively check PadN payload to contain
178 * only zeroes.
179 */
180 for (i = 2; i < optlen; i++) {
181 if (nh[off + i] != 0)
182 goto bad;
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185
186 default: /* Other TLV code so scan list */
187 if (optlen > len)
188 goto bad;
189 for (curr=procs; curr->type >= 0; curr++) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700190 if (curr->type == nh[off]) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900191 /* type specific length/alignment
192 checks will be performed in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 func(). */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000194 if (curr->func(skb, off) == false)
195 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
197 }
198 }
199 if (curr->type < 0) {
Herbert Xue5bbef22007-10-15 12:50:28 -0700200 if (ip6_tlvopt_unknown(skb, off) == 0)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000201 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Eldad Zack9b905fe2012-05-20 01:59:33 +0000203 padlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205 }
206 off += optlen;
207 len -= optlen;
208 }
Eldad Zack9b905fe2012-05-20 01:59:33 +0000209 /* This case will not be caught by above check since its padding
210 * length is smaller than 7:
211 * 1 byte NH + 1 byte Length + 6 bytes Padding
212 */
213 if ((padlen == 6) && ((off - skb_network_header_len(skb)) == 8))
214 goto bad;
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (len == 0)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000217 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218bad:
219 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000220 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223/*****************************
224 Destination options header.
225 *****************************/
226
Amerigo Wang07a93622012-10-29 16:23:10 +0000227#if IS_ENABLED(CONFIG_IPV6_MIP6)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000228static bool ipv6_dest_hao(struct sk_buff *skb, int optoff)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700229{
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700230 struct ipv6_destopt_hao *hao;
231 struct inet6_skb_parm *opt = IP6CB(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700232 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700233 struct in6_addr tmp_addr;
234 int ret;
235
236 if (opt->dsthao) {
237 LIMIT_NETDEBUG(KERN_DEBUG "hao duplicated\n");
238 goto discard;
239 }
240 opt->dsthao = opt->dst1;
241 opt->dst1 = 0;
242
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700243 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700244
245 if (hao->length != 16) {
246 LIMIT_NETDEBUG(
247 KERN_DEBUG "hao invalid option length = %d\n", hao->length);
248 goto discard;
249 }
250
251 if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
252 LIMIT_NETDEBUG(
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700253 KERN_DEBUG "hao is not an unicast addr: %pI6\n", &hao->addr);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700254 goto discard;
255 }
256
257 ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
258 (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
259 if (unlikely(ret < 0))
260 goto discard;
261
262 if (skb_cloned(skb)) {
Herbert Xu65c88462007-10-15 01:29:10 -0700263 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700264 goto discard;
265
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700266 /* update all variable using below by copied skbuff */
Herbert Xu65c88462007-10-15 01:29:10 -0700267 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700268 optoff);
Herbert Xu65c88462007-10-15 01:29:10 -0700269 ipv6h = ipv6_hdr(skb);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700270 }
271
272 if (skb->ip_summed == CHECKSUM_COMPLETE)
273 skb->ip_summed = CHECKSUM_NONE;
274
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000275 tmp_addr = ipv6h->saddr;
276 ipv6h->saddr = hao->addr;
277 hao->addr = tmp_addr;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700278
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700279 if (skb->tstamp.tv64 == 0)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700280 __net_timestamp(skb);
281
Eric Dumazeta50feda2012-05-18 18:57:34 +0000282 return true;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700283
284 discard:
285 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000286 return false;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700287}
288#endif
289
Eric Dumazeta50feda2012-05-18 18:57:34 +0000290static const struct tlvtype_proc tlvprocdestopt_lst[] = {
Amerigo Wang07a93622012-10-29 16:23:10 +0000291#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700292 {
293 .type = IPV6_TLV_HAO,
294 .func = ipv6_dest_hao,
295 },
296#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 {-1, NULL}
298};
299
Herbert Xue5bbef22007-10-15 12:50:28 -0700300static int ipv6_destopt_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct inet6_skb_parm *opt = IP6CB(skb);
Amerigo Wang07a93622012-10-29 16:23:10 +0000303#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700304 __u16 dstbuf;
305#endif
Eric Dumazet897dc802011-07-28 04:00:35 +0000306 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700308 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
309 !pskb_may_pull(skb, (skb_transport_offset(skb) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700310 ((skb_transport_header(skb)[1] + 1) << 3)))) {
Eric Dumazet897dc802011-07-28 04:00:35 +0000311 IP6_INC_STATS_BH(dev_net(dst->dev), ip6_dst_idev(dst),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900312 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 kfree_skb(skb);
314 return -1;
315 }
316
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300317 opt->lastopt = opt->dst1 = skb_network_header_len(skb);
Amerigo Wang07a93622012-10-29 16:23:10 +0000318#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700319 dstbuf = opt->dst1;
320#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Herbert Xue5bbef22007-10-15 12:50:28 -0700322 if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700323 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
Masahide NAKAMURAdc435e62006-08-31 15:18:49 -0700324 opt = IP6CB(skb);
Amerigo Wang07a93622012-10-29 16:23:10 +0000325#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700326 opt->nhoff = dstbuf;
327#else
Patrick McHardy951dbc82006-01-06 23:02:34 -0800328 opt->nhoff = opt->dst1;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700329#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 1;
331 }
332
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700333 IP6_INC_STATS_BH(dev_net(dst->dev),
334 ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return -1;
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/********************************
339 Routing header.
340 ********************************/
341
Eric Dumazetf6bc7d92010-06-14 04:39:27 +0000342/* called with rcu_read_lock() */
Herbert Xue5bbef22007-10-15 12:50:28 -0700343static int ipv6_rthdr_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct inet6_skb_parm *opt = IP6CB(skb);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700346 struct in6_addr *addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct in6_addr daddr;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700348 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int n, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct ipv6_rt_hdr *hdr;
351 struct rt0_hdr *rthdr;
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700352 struct net *net = dev_net(skb->dev);
353 int accept_source_route = net->ipv6.devconf_all->accept_source_route;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700354
Eric Dumazetf6bc7d92010-06-14 04:39:27 +0000355 idev = __in6_dev_get(skb->dev);
356 if (idev && accept_source_route > idev->cnf.accept_source_route)
357 accept_source_route = idev->cnf.accept_source_route;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700358
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700359 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
360 !pskb_may_pull(skb, (skb_transport_offset(skb) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700361 ((skb_transport_header(skb)[1] + 1) << 3)))) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000362 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900363 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 kfree_skb(skb);
365 return -1;
366 }
367
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700368 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700370 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 skb->pkt_type != PACKET_HOST) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000372 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900373 IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 kfree_skb(skb);
375 return -1;
376 }
377
378looped_back:
379 if (hdr->segments_left == 0) {
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700380 switch (hdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000381#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700382 case IPV6_SRCRT_TYPE_2:
383 /* Silently discard type 2 header unless it was
384 * processed by own
385 */
386 if (!addr) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000387 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900388 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700389 kfree_skb(skb);
390 return -1;
391 }
392 break;
393#endif
394 default:
395 break;
396 }
397
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300398 opt->lastopt = opt->srcrt = skb_network_header_len(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700399 skb->transport_header += (hdr->hdrlen + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 opt->dst0 = opt->dst1;
401 opt->dst1 = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700402 opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return 1;
404 }
405
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700406 switch (hdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000407#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700408 case IPV6_SRCRT_TYPE_2:
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700409 if (accept_source_route < 0)
410 goto unknown_rh;
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700411 /* Silently discard invalid RTH type 2 */
412 if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000413 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900414 IPSTATS_MIB_INHDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700415 kfree_skb(skb);
416 return -1;
417 }
418 break;
419#endif
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700420 default:
421 goto unknown_rh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /*
425 * This is the routing header forwarding algorithm from
426 * RFC 2460, page 16.
427 */
428
429 n = hdr->hdrlen >> 1;
430
431 if (hdr->segments_left > n) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000432 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900433 IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700434 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
435 ((&hdr->segments_left) -
436 skb_network_header(skb)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return -1;
438 }
439
440 /* We are about to mangle packet header. Be careful!
441 Do not damage packets queued somewhere.
442 */
443 if (skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* the copy is a forwarded packet */
Herbert Xu65c88462007-10-15 01:29:10 -0700445 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000446 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900447 IPSTATS_MIB_OUTDISCARDS);
448 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return -1;
450 }
Herbert Xu65c88462007-10-15 01:29:10 -0700451 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
Patrick McHardy84fa7932006-08-29 16:44:56 -0700454 if (skb->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 skb->ip_summed = CHECKSUM_NONE;
456
457 i = n - --hdr->segments_left;
458
459 rthdr = (struct rt0_hdr *) hdr;
460 addr = rthdr->addr;
461 addr += i - 1;
462
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700463 switch (hdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000464#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700465 case IPV6_SRCRT_TYPE_2:
466 if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700467 (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700468 IPPROTO_ROUTING) < 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000469 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900470 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700471 kfree_skb(skb);
472 return -1;
473 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000474 if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
475 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900476 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700477 kfree_skb(skb);
478 return -1;
479 }
480 break;
481#endif
482 default:
483 break;
484 }
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (ipv6_addr_is_multicast(addr)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000487 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900488 IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 kfree_skb(skb);
490 return -1;
491 }
492
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000493 daddr = *addr;
494 *addr = ipv6_hdr(skb)->daddr;
495 ipv6_hdr(skb)->daddr = daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Eric Dumazetadf30902009-06-02 05:19:30 +0000497 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 ip6_route_input(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000499 if (skb_dst(skb)->error) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700500 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 dst_input(skb);
502 return -1;
503 }
504
Eric Dumazetadf30902009-06-02 05:19:30 +0000505 if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700506 if (ipv6_hdr(skb)->hop_limit <= 1) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000507 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900508 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000510 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 kfree_skb(skb);
512 return -1;
513 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700514 ipv6_hdr(skb)->hop_limit--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 goto looped_back;
516 }
517
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700518 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 dst_input(skb);
520 return -1;
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700521
522unknown_rh:
Eric Dumazetadf30902009-06-02 05:19:30 +0000523 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700524 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
525 (&hdr->type) - skb_network_header(skb));
526 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000529static const struct inet6_protocol rthdr_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 .handler = ipv6_rthdr_rcv,
Herbert Xuadcfc7d2006-06-30 13:36:15 -0700531 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532};
533
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000534static const struct inet6_protocol destopt_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800535 .handler = ipv6_destopt_rcv,
536 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537};
538
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000539static const struct inet6_protocol nodata_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800540 .handler = dst_discard,
541 .flags = INET6_PROTO_NOPOLICY,
542};
543
544int __init ipv6_exthdrs_init(void)
545{
546 int ret;
547
548 ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
549 if (ret)
550 goto out;
551
552 ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
553 if (ret)
554 goto out_rthdr;
555
556 ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
557 if (ret)
558 goto out_destopt;
559
560out:
561 return ret;
562out_rthdr:
563 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
564out_destopt:
565 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
566 goto out;
567};
568
569void ipv6_exthdrs_exit(void)
570{
571 inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
572 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
573 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/**********************************
577 Hop-by-hop options.
578 **********************************/
579
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700580/*
Eric Dumazetadf30902009-06-02 05:19:30 +0000581 * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700582 */
583static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
584{
Eric Dumazetadf30902009-06-02 05:19:30 +0000585 return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700586}
587
David S. Miller2570a4f2010-01-13 17:27:37 -0800588static inline struct net *ipv6_skb_net(struct sk_buff *skb)
589{
590 return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/* Router Alert as of RFC 2711 */
594
Eric Dumazeta50feda2012-05-18 18:57:34 +0000595static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700597 const unsigned char *nh = skb_network_header(skb);
Masahide NAKAMURAa80ff032006-08-23 19:19:50 -0700598
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700599 if (nh[optoff + 1] == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 IP6CB(skb)->ra = optoff;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000601 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Patrick McHardy64ce2072005-08-09 20:50:53 -0700603 LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_ra: wrong RA length %d\n",
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700604 nh[optoff + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000606 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609/* Jumbo payload */
610
Eric Dumazeta50feda2012-05-18 18:57:34 +0000611static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700613 const unsigned char *nh = skb_network_header(skb);
David S. Miller2570a4f2010-01-13 17:27:37 -0800614 struct net *net = ipv6_skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 u32 pkt_len;
616
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700617 if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
Patrick McHardy64ce2072005-08-09 20:50:53 -0700618 LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700619 nh[optoff+1]);
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700620 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900621 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 goto drop;
623 }
624
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700625 pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (pkt_len <= IPV6_MAXPLEN) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700627 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
628 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000630 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700632 if (ipv6_hdr(skb)->payload_len) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700633 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
634 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000636 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638
639 if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700640 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
641 IPSTATS_MIB_INTRUNCATEDPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 goto drop;
643 }
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700644
645 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
646 goto drop;
647
Eric Dumazeta50feda2012-05-18 18:57:34 +0000648 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650drop:
651 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000652 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Eric Dumazeta50feda2012-05-18 18:57:34 +0000655static const struct tlvtype_proc tlvprochopopt_lst[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 {
657 .type = IPV6_TLV_ROUTERALERT,
658 .func = ipv6_hop_ra,
659 },
660 {
661 .type = IPV6_TLV_JUMBO,
662 .func = ipv6_hop_jumbo,
663 },
664 { -1, }
665};
666
Herbert Xue5bbef22007-10-15 12:50:28 -0700667int ipv6_parse_hopopts(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Patrick McHardy951dbc82006-01-06 23:02:34 -0800669 struct inet6_skb_parm *opt = IP6CB(skb);
670
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700671 /*
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700672 * skb_network_header(skb) is equal to skb->data, and
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300673 * skb_network_header_len(skb) is always equal to
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700674 * sizeof(struct ipv6hdr) by definition of
675 * hop-by-hop options.
676 */
677 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700678 !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
679 ((skb_transport_header(skb)[1] + 1) << 3)))) {
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700680 kfree_skb(skb);
681 return -1;
682 }
683
Patrick McHardy951dbc82006-01-06 23:02:34 -0800684 opt->hop = sizeof(struct ipv6hdr);
Herbert Xue5bbef22007-10-15 12:50:28 -0700685 if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700686 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
Masahide NAKAMURAdc435e62006-08-31 15:18:49 -0700687 opt = IP6CB(skb);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800688 opt->nhoff = sizeof(struct ipv6hdr);
YOSHIFUJI Hideakib8097392006-04-18 14:48:45 -0700689 return 1;
Patrick McHardy951dbc82006-01-06 23:02:34 -0800690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return -1;
692}
693
694/*
695 * Creating outbound headers.
696 *
697 * "build" functions work when skb is filled from head to tail (datagram)
698 * "push" functions work when headers are added from tail to head (tcp)
699 *
700 * In both cases we assume, that caller reserved enough room
701 * for headers.
702 */
703
704static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
705 struct ipv6_rt_hdr *opt,
706 struct in6_addr **addr_p)
707{
708 struct rt0_hdr *phdr, *ihdr;
709 int hops;
710
711 ihdr = (struct rt0_hdr *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
714 memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
715
716 hops = ihdr->rt_hdr.hdrlen >> 1;
717
718 if (hops > 1)
719 memcpy(phdr->addr, ihdr->addr + 1,
720 (hops - 1) * sizeof(struct in6_addr));
721
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000722 phdr->addr[hops - 1] = **addr_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 *addr_p = ihdr->addr;
724
725 phdr->rt_hdr.nexthdr = *proto;
726 *proto = NEXTHDR_ROUTING;
727}
728
729static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
730{
731 struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt));
732
733 memcpy(h, opt, ipv6_optlen(opt));
734 h->nexthdr = *proto;
735 *proto = type;
736}
737
738void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
739 u8 *proto,
740 struct in6_addr **daddr)
741{
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900742 if (opt->srcrt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 ipv6_push_rthdr(skb, proto, opt->srcrt, daddr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900744 /*
745 * IPV6_RTHDRDSTOPTS is ignored
746 * unless IPV6_RTHDR is set (RFC3542).
747 */
748 if (opt->dst0opt)
749 ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (opt->hopopt)
752 ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
753}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900754EXPORT_SYMBOL(ipv6_push_nfrag_opts);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
757{
758 if (opt->dst1opt)
759 ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
760}
761
762struct ipv6_txoptions *
763ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
764{
765 struct ipv6_txoptions *opt2;
766
767 opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
768 if (opt2) {
Eldad Zackac3c8172012-04-01 07:49:04 +0000769 long dif = (char *)opt2 - (char *)opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 memcpy(opt2, opt, opt->tot_len);
771 if (opt2->hopopt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000772 *((char **)&opt2->hopopt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (opt2->dst0opt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000774 *((char **)&opt2->dst0opt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (opt2->dst1opt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000776 *((char **)&opt2->dst1opt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (opt2->srcrt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000778 *((char **)&opt2->srcrt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 return opt2;
781}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800782EXPORT_SYMBOL_GPL(ipv6_dup_options);
783
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900784static int ipv6_renew_option(void *ohdr,
785 struct ipv6_opt_hdr __user *newopt, int newoptlen,
786 int inherit,
787 struct ipv6_opt_hdr **hdr,
788 char **p)
789{
790 if (inherit) {
791 if (ohdr) {
792 memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
793 *hdr = (struct ipv6_opt_hdr *)*p;
Joe Perchese3192692012-06-03 17:41:40 +0000794 *p += CMSG_ALIGN(ipv6_optlen(*hdr));
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900795 }
796 } else {
797 if (newopt) {
798 if (copy_from_user(*p, newopt, newoptlen))
799 return -EFAULT;
800 *hdr = (struct ipv6_opt_hdr *)*p;
Joe Perchese3192692012-06-03 17:41:40 +0000801 if (ipv6_optlen(*hdr) > newoptlen)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900802 return -EINVAL;
803 *p += CMSG_ALIGN(newoptlen);
804 }
805 }
806 return 0;
807}
808
809struct ipv6_txoptions *
810ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
811 int newtype,
812 struct ipv6_opt_hdr __user *newopt, int newoptlen)
813{
814 int tot_len = 0;
815 char *p;
816 struct ipv6_txoptions *opt2;
817 int err;
818
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700819 if (opt) {
820 if (newtype != IPV6_HOPOPTS && opt->hopopt)
821 tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
822 if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
823 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
824 if (newtype != IPV6_RTHDR && opt->srcrt)
825 tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
826 if (newtype != IPV6_DSTOPTS && opt->dst1opt)
827 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
828 }
829
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900830 if (newopt && newoptlen)
831 tot_len += CMSG_ALIGN(newoptlen);
832
833 if (!tot_len)
834 return NULL;
835
YOSHIFUJI Hideaki8b8aa4b2005-11-20 12:18:17 +0900836 tot_len += sizeof(*opt2);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900837 opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
838 if (!opt2)
839 return ERR_PTR(-ENOBUFS);
840
841 memset(opt2, 0, tot_len);
842
843 opt2->tot_len = tot_len;
844 p = (char *)(opt2 + 1);
845
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700846 err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900847 newtype != IPV6_HOPOPTS,
848 &opt2->hopopt, &p);
849 if (err)
850 goto out;
851
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700852 err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900853 newtype != IPV6_RTHDRDSTOPTS,
854 &opt2->dst0opt, &p);
855 if (err)
856 goto out;
857
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700858 err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900859 newtype != IPV6_RTHDR,
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700860 (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900861 if (err)
862 goto out;
863
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700864 err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900865 newtype != IPV6_DSTOPTS,
866 &opt2->dst1opt, &p);
867 if (err)
868 goto out;
869
870 opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
871 (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
872 (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
873 opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
874
875 return opt2;
876out:
YOSHIFUJI Hideaki8b8aa4b2005-11-20 12:18:17 +0900877 sock_kfree_s(sk, opt2, opt2->tot_len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900878 return ERR_PTR(err);
879}
880
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900881struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
882 struct ipv6_txoptions *opt)
883{
884 /*
885 * ignore the dest before srcrt unless srcrt is being included.
886 * --yoshfuji
887 */
888 if (opt && opt->dst0opt && !opt->srcrt) {
889 if (opt_space != opt) {
890 memcpy(opt_space, opt, sizeof(*opt_space));
891 opt = opt_space;
892 }
893 opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
894 opt->dst0opt = NULL;
895 }
896
897 return opt;
898}
Chris Elstona495f832012-04-29 21:48:53 +0000899EXPORT_SYMBOL_GPL(ipv6_fixup_options);
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900900
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000901/**
902 * fl6_update_dst - update flowi destination address with info given
903 * by srcrt option, if any.
904 *
David S. Miller4c9483b2011-03-12 16:22:43 -0500905 * @fl6: flowi6 for which daddr is to be updated
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000906 * @opt: struct ipv6_txoptions in which to look for srcrt opt
David S. Miller4c9483b2011-03-12 16:22:43 -0500907 * @orig: copy of original daddr address if modified
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000908 *
909 * Returns NULL if no txoptions or no srcrt, otherwise returns orig
David S. Miller4c9483b2011-03-12 16:22:43 -0500910 * and initial value of fl6->daddr set in orig
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000911 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500912struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000913 const struct ipv6_txoptions *opt,
914 struct in6_addr *orig)
915{
916 if (!opt || !opt->srcrt)
917 return NULL;
918
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000919 *orig = fl6->daddr;
920 fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000921 return orig;
922}
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000923EXPORT_SYMBOL_GPL(fl6_update_dst);