blob: cb6f0828ca44a32019c729cb0d6a4befaf060475 [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
Vlad Yasevich8ca896c2012-11-15 08:49:13 +0000534static const struct net_offload rthdr_offload = {
535 .flags = INET6_PROTO_GSO_EXTHDR,
536};
537
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000538static const struct inet6_protocol destopt_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800539 .handler = ipv6_destopt_rcv,
540 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541};
542
Vlad Yasevich8ca896c2012-11-15 08:49:13 +0000543static const struct net_offload dstopt_offload = {
544 .flags = INET6_PROTO_GSO_EXTHDR,
545};
546
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000547static const struct inet6_protocol nodata_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800548 .handler = dst_discard,
549 .flags = INET6_PROTO_NOPOLICY,
550};
551
552int __init ipv6_exthdrs_init(void)
553{
554 int ret;
555
556 ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
557 if (ret)
558 goto out;
559
560 ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
561 if (ret)
562 goto out_rthdr;
563
564 ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
565 if (ret)
566 goto out_destopt;
567
568out:
569 return ret;
570out_rthdr:
571 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
572out_destopt:
573 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
574 goto out;
575};
576
577void ipv6_exthdrs_exit(void)
578{
579 inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
580 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
581 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/**********************************
585 Hop-by-hop options.
586 **********************************/
587
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700588/*
Eric Dumazetadf30902009-06-02 05:19:30 +0000589 * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700590 */
591static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
592{
Eric Dumazetadf30902009-06-02 05:19:30 +0000593 return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700594}
595
David S. Miller2570a4f2010-01-13 17:27:37 -0800596static inline struct net *ipv6_skb_net(struct sk_buff *skb)
597{
598 return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/* Router Alert as of RFC 2711 */
602
Eric Dumazeta50feda2012-05-18 18:57:34 +0000603static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700605 const unsigned char *nh = skb_network_header(skb);
Masahide NAKAMURAa80ff032006-08-23 19:19:50 -0700606
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700607 if (nh[optoff + 1] == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 IP6CB(skb)->ra = optoff;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000609 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Patrick McHardy64ce2072005-08-09 20:50:53 -0700611 LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_ra: wrong RA length %d\n",
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700612 nh[optoff + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000614 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617/* Jumbo payload */
618
Eric Dumazeta50feda2012-05-18 18:57:34 +0000619static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700621 const unsigned char *nh = skb_network_header(skb);
David S. Miller2570a4f2010-01-13 17:27:37 -0800622 struct net *net = ipv6_skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 u32 pkt_len;
624
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700625 if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
Patrick McHardy64ce2072005-08-09 20:50:53 -0700626 LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700627 nh[optoff+1]);
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700628 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900629 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 goto drop;
631 }
632
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700633 pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (pkt_len <= IPV6_MAXPLEN) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700635 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
636 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000638 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700640 if (ipv6_hdr(skb)->payload_len) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700641 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
642 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000644 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
647 if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700648 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
649 IPSTATS_MIB_INTRUNCATEDPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 goto drop;
651 }
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700652
653 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
654 goto drop;
655
Eric Dumazeta50feda2012-05-18 18:57:34 +0000656 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658drop:
659 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000660 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Eric Dumazeta50feda2012-05-18 18:57:34 +0000663static const struct tlvtype_proc tlvprochopopt_lst[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 {
665 .type = IPV6_TLV_ROUTERALERT,
666 .func = ipv6_hop_ra,
667 },
668 {
669 .type = IPV6_TLV_JUMBO,
670 .func = ipv6_hop_jumbo,
671 },
672 { -1, }
673};
674
Herbert Xue5bbef22007-10-15 12:50:28 -0700675int ipv6_parse_hopopts(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Patrick McHardy951dbc82006-01-06 23:02:34 -0800677 struct inet6_skb_parm *opt = IP6CB(skb);
678
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700679 /*
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700680 * skb_network_header(skb) is equal to skb->data, and
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300681 * skb_network_header_len(skb) is always equal to
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700682 * sizeof(struct ipv6hdr) by definition of
683 * hop-by-hop options.
684 */
685 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700686 !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
687 ((skb_transport_header(skb)[1] + 1) << 3)))) {
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700688 kfree_skb(skb);
689 return -1;
690 }
691
Patrick McHardy951dbc82006-01-06 23:02:34 -0800692 opt->hop = sizeof(struct ipv6hdr);
Herbert Xue5bbef22007-10-15 12:50:28 -0700693 if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700694 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
Masahide NAKAMURAdc435e62006-08-31 15:18:49 -0700695 opt = IP6CB(skb);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800696 opt->nhoff = sizeof(struct ipv6hdr);
YOSHIFUJI Hideakib8097392006-04-18 14:48:45 -0700697 return 1;
Patrick McHardy951dbc82006-01-06 23:02:34 -0800698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return -1;
700}
701
702/*
703 * Creating outbound headers.
704 *
705 * "build" functions work when skb is filled from head to tail (datagram)
706 * "push" functions work when headers are added from tail to head (tcp)
707 *
708 * In both cases we assume, that caller reserved enough room
709 * for headers.
710 */
711
712static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
713 struct ipv6_rt_hdr *opt,
714 struct in6_addr **addr_p)
715{
716 struct rt0_hdr *phdr, *ihdr;
717 int hops;
718
719 ihdr = (struct rt0_hdr *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
722 memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
723
724 hops = ihdr->rt_hdr.hdrlen >> 1;
725
726 if (hops > 1)
727 memcpy(phdr->addr, ihdr->addr + 1,
728 (hops - 1) * sizeof(struct in6_addr));
729
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000730 phdr->addr[hops - 1] = **addr_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 *addr_p = ihdr->addr;
732
733 phdr->rt_hdr.nexthdr = *proto;
734 *proto = NEXTHDR_ROUTING;
735}
736
737static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
738{
739 struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt));
740
741 memcpy(h, opt, ipv6_optlen(opt));
742 h->nexthdr = *proto;
743 *proto = type;
744}
745
746void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
747 u8 *proto,
748 struct in6_addr **daddr)
749{
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900750 if (opt->srcrt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 ipv6_push_rthdr(skb, proto, opt->srcrt, daddr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900752 /*
753 * IPV6_RTHDRDSTOPTS is ignored
754 * unless IPV6_RTHDR is set (RFC3542).
755 */
756 if (opt->dst0opt)
757 ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (opt->hopopt)
760 ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
761}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900762EXPORT_SYMBOL(ipv6_push_nfrag_opts);
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
765{
766 if (opt->dst1opt)
767 ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
768}
769
770struct ipv6_txoptions *
771ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
772{
773 struct ipv6_txoptions *opt2;
774
775 opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
776 if (opt2) {
Eldad Zackac3c8172012-04-01 07:49:04 +0000777 long dif = (char *)opt2 - (char *)opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 memcpy(opt2, opt, opt->tot_len);
779 if (opt2->hopopt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000780 *((char **)&opt2->hopopt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (opt2->dst0opt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000782 *((char **)&opt2->dst0opt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (opt2->dst1opt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000784 *((char **)&opt2->dst1opt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (opt2->srcrt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000786 *((char **)&opt2->srcrt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788 return opt2;
789}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800790EXPORT_SYMBOL_GPL(ipv6_dup_options);
791
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900792static int ipv6_renew_option(void *ohdr,
793 struct ipv6_opt_hdr __user *newopt, int newoptlen,
794 int inherit,
795 struct ipv6_opt_hdr **hdr,
796 char **p)
797{
798 if (inherit) {
799 if (ohdr) {
800 memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
801 *hdr = (struct ipv6_opt_hdr *)*p;
Joe Perchese3192692012-06-03 17:41:40 +0000802 *p += CMSG_ALIGN(ipv6_optlen(*hdr));
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900803 }
804 } else {
805 if (newopt) {
806 if (copy_from_user(*p, newopt, newoptlen))
807 return -EFAULT;
808 *hdr = (struct ipv6_opt_hdr *)*p;
Joe Perchese3192692012-06-03 17:41:40 +0000809 if (ipv6_optlen(*hdr) > newoptlen)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900810 return -EINVAL;
811 *p += CMSG_ALIGN(newoptlen);
812 }
813 }
814 return 0;
815}
816
817struct ipv6_txoptions *
818ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
819 int newtype,
820 struct ipv6_opt_hdr __user *newopt, int newoptlen)
821{
822 int tot_len = 0;
823 char *p;
824 struct ipv6_txoptions *opt2;
825 int err;
826
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700827 if (opt) {
828 if (newtype != IPV6_HOPOPTS && opt->hopopt)
829 tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
830 if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
831 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
832 if (newtype != IPV6_RTHDR && opt->srcrt)
833 tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
834 if (newtype != IPV6_DSTOPTS && opt->dst1opt)
835 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
836 }
837
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900838 if (newopt && newoptlen)
839 tot_len += CMSG_ALIGN(newoptlen);
840
841 if (!tot_len)
842 return NULL;
843
YOSHIFUJI Hideaki8b8aa4b2005-11-20 12:18:17 +0900844 tot_len += sizeof(*opt2);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900845 opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
846 if (!opt2)
847 return ERR_PTR(-ENOBUFS);
848
849 memset(opt2, 0, tot_len);
850
851 opt2->tot_len = tot_len;
852 p = (char *)(opt2 + 1);
853
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700854 err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900855 newtype != IPV6_HOPOPTS,
856 &opt2->hopopt, &p);
857 if (err)
858 goto out;
859
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700860 err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900861 newtype != IPV6_RTHDRDSTOPTS,
862 &opt2->dst0opt, &p);
863 if (err)
864 goto out;
865
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700866 err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900867 newtype != IPV6_RTHDR,
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700868 (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900869 if (err)
870 goto out;
871
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700872 err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900873 newtype != IPV6_DSTOPTS,
874 &opt2->dst1opt, &p);
875 if (err)
876 goto out;
877
878 opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
879 (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
880 (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
881 opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
882
883 return opt2;
884out:
YOSHIFUJI Hideaki8b8aa4b2005-11-20 12:18:17 +0900885 sock_kfree_s(sk, opt2, opt2->tot_len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900886 return ERR_PTR(err);
887}
888
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900889struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
890 struct ipv6_txoptions *opt)
891{
892 /*
893 * ignore the dest before srcrt unless srcrt is being included.
894 * --yoshfuji
895 */
896 if (opt && opt->dst0opt && !opt->srcrt) {
897 if (opt_space != opt) {
898 memcpy(opt_space, opt, sizeof(*opt_space));
899 opt = opt_space;
900 }
901 opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
902 opt->dst0opt = NULL;
903 }
904
905 return opt;
906}
Chris Elstona495f832012-04-29 21:48:53 +0000907EXPORT_SYMBOL_GPL(ipv6_fixup_options);
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900908
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000909/**
910 * fl6_update_dst - update flowi destination address with info given
911 * by srcrt option, if any.
912 *
David S. Miller4c9483b2011-03-12 16:22:43 -0500913 * @fl6: flowi6 for which daddr is to be updated
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000914 * @opt: struct ipv6_txoptions in which to look for srcrt opt
David S. Miller4c9483b2011-03-12 16:22:43 -0500915 * @orig: copy of original daddr address if modified
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000916 *
917 * Returns NULL if no txoptions or no srcrt, otherwise returns orig
David S. Miller4c9483b2011-03-12 16:22:43 -0500918 * and initial value of fl6->daddr set in orig
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000919 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500920struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000921 const struct ipv6_txoptions *opt,
922 struct in6_addr *orig)
923{
924 if (!opt || !opt->srcrt)
925 return NULL;
926
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000927 *orig = fl6->daddr;
928 fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000929 return orig;
930}
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000931EXPORT_SYMBOL_GPL(fl6_update_dst);