blob: 70fbf6bc5a877c44fdbcedd5c76f65635685c0dd [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>
Vlad Yasevich2207afc2012-11-15 08:49:19 +000051#include "ip6_offload.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070053int ipv6_find_tlv(struct sk_buff *skb, int offset, int type)
54{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070055 const unsigned char *nh = skb_network_header(skb);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070056 int packet_len = skb->tail - skb->network_header;
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070057 struct ipv6_opt_hdr *hdr;
58 int len;
59
60 if (offset + 2 > packet_len)
61 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070062 hdr = (struct ipv6_opt_hdr *)(nh + offset);
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070063 len = ((hdr->hdrlen + 1) << 3);
64
65 if (offset + len > packet_len)
66 goto bad;
67
68 offset += 2;
69 len -= 2;
70
71 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070072 int opttype = nh[offset];
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070073 int optlen;
74
75 if (opttype == type)
76 return offset;
77
78 switch (opttype) {
Eldad Zack1de5a712012-05-17 06:00:25 +000079 case IPV6_TLV_PAD1:
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070080 optlen = 1;
81 break;
82 default:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070083 optlen = nh[offset + 1] + 2;
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070084 if (optlen > len)
85 goto bad;
86 break;
87 }
88 offset += optlen;
89 len -= optlen;
90 }
91 /* not_found */
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070092 bad:
93 return -1;
94}
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -070095EXPORT_SYMBOL_GPL(ipv6_find_tlv);
Masahide NAKAMURAc61a4042006-08-23 19:18:35 -070096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
98 * Parsing tlv encoded headers.
99 *
Eric Dumazeta50feda2012-05-18 18:57:34 +0000100 * Parsing function "func" returns true, if parsing succeed
101 * and false, if it failed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * It MUST NOT touch skb->h.
103 */
104
105struct tlvtype_proc {
106 int type;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000107 bool (*func)(struct sk_buff *skb, int offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110/*********************
111 Generic functions
112 *********************/
113
114/* An unknown option is detected, decide what to do */
115
Eric Dumazeta50feda2012-05-18 18:57:34 +0000116static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700118 switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 case 0: /* ignore */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000120 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 case 1: /* drop packet */
123 break;
124
125 case 3: /* Send ICMP if not a multicast address and drop packet */
126 /* Actually, it is redundant check. icmp_send
127 will recheck in any case.
128 */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700129 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 break;
131 case 2: /* send ICMP PARM PROB regardless and drop packet */
132 icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000133 return false;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000137 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140/* Parse tlv encoded option header (hop-by-hop or destination) */
141
Eric Dumazeta50feda2012-05-18 18:57:34 +0000142static bool ip6_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Eric Dumazeta50feda2012-05-18 18:57:34 +0000144 const struct tlvtype_proc *curr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700145 const unsigned char *nh = skb_network_header(skb);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300146 int off = skb_network_header_len(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700147 int len = (skb_transport_header(skb)[1] + 1) << 3;
Eldad Zack9b905fe2012-05-20 01:59:33 +0000148 int padlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700150 if (skb_transport_offset(skb) + len > skb_headlen(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 goto bad;
152
153 off += 2;
154 len -= 2;
155
156 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700157 int optlen = nh[off + 1] + 2;
Eldad Zackc1412fc2012-04-12 17:36:17 -0400158 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700160 switch (nh[off]) {
Eldad Zack1de5a712012-05-17 06:00:25 +0000161 case IPV6_TLV_PAD1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 optlen = 1;
Eldad Zack9b905fe2012-05-20 01:59:33 +0000163 padlen++;
164 if (padlen > 7)
165 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 break;
167
168 case IPV6_TLV_PADN:
Eldad Zackc1412fc2012-04-12 17:36:17 -0400169 /* RFC 2460 states that the purpose of PadN is
170 * to align the containing header to multiples
171 * of 8. 7 is therefore the highest valid value.
172 * See also RFC 4942, Section 2.1.9.5.
173 */
Eldad Zack9b905fe2012-05-20 01:59:33 +0000174 padlen += optlen;
175 if (padlen > 7)
Eldad Zackc1412fc2012-04-12 17:36:17 -0400176 goto bad;
177 /* RFC 4942 recommends receiving hosts to
178 * actively check PadN payload to contain
179 * only zeroes.
180 */
181 for (i = 2; i < optlen; i++) {
182 if (nh[off + i] != 0)
183 goto bad;
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 break;
186
187 default: /* Other TLV code so scan list */
188 if (optlen > len)
189 goto bad;
190 for (curr=procs; curr->type >= 0; curr++) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700191 if (curr->type == nh[off]) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900192 /* type specific length/alignment
193 checks will be performed in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 func(). */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000195 if (curr->func(skb, off) == false)
196 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 break;
198 }
199 }
200 if (curr->type < 0) {
Herbert Xue5bbef22007-10-15 12:50:28 -0700201 if (ip6_tlvopt_unknown(skb, off) == 0)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000202 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
Eldad Zack9b905fe2012-05-20 01:59:33 +0000204 padlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 break;
206 }
207 off += optlen;
208 len -= optlen;
209 }
Eldad Zack9b905fe2012-05-20 01:59:33 +0000210 /* This case will not be caught by above check since its padding
211 * length is smaller than 7:
212 * 1 byte NH + 1 byte Length + 6 bytes Padding
213 */
214 if ((padlen == 6) && ((off - skb_network_header_len(skb)) == 8))
215 goto bad;
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (len == 0)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000218 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219bad:
220 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000221 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/*****************************
225 Destination options header.
226 *****************************/
227
Amerigo Wang07a93622012-10-29 16:23:10 +0000228#if IS_ENABLED(CONFIG_IPV6_MIP6)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000229static bool ipv6_dest_hao(struct sk_buff *skb, int optoff)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700230{
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700231 struct ipv6_destopt_hao *hao;
232 struct inet6_skb_parm *opt = IP6CB(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700233 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700234 struct in6_addr tmp_addr;
235 int ret;
236
237 if (opt->dsthao) {
238 LIMIT_NETDEBUG(KERN_DEBUG "hao duplicated\n");
239 goto discard;
240 }
241 opt->dsthao = opt->dst1;
242 opt->dst1 = 0;
243
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700244 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700245
246 if (hao->length != 16) {
247 LIMIT_NETDEBUG(
248 KERN_DEBUG "hao invalid option length = %d\n", hao->length);
249 goto discard;
250 }
251
252 if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
253 LIMIT_NETDEBUG(
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700254 KERN_DEBUG "hao is not an unicast addr: %pI6\n", &hao->addr);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700255 goto discard;
256 }
257
258 ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
259 (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
260 if (unlikely(ret < 0))
261 goto discard;
262
263 if (skb_cloned(skb)) {
Herbert Xu65c88462007-10-15 01:29:10 -0700264 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700265 goto discard;
266
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700267 /* update all variable using below by copied skbuff */
Herbert Xu65c88462007-10-15 01:29:10 -0700268 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700269 optoff);
Herbert Xu65c88462007-10-15 01:29:10 -0700270 ipv6h = ipv6_hdr(skb);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700271 }
272
273 if (skb->ip_summed == CHECKSUM_COMPLETE)
274 skb->ip_summed = CHECKSUM_NONE;
275
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000276 tmp_addr = ipv6h->saddr;
277 ipv6h->saddr = hao->addr;
278 hao->addr = tmp_addr;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700279
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700280 if (skb->tstamp.tv64 == 0)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700281 __net_timestamp(skb);
282
Eric Dumazeta50feda2012-05-18 18:57:34 +0000283 return true;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700284
285 discard:
286 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000287 return false;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700288}
289#endif
290
Eric Dumazeta50feda2012-05-18 18:57:34 +0000291static const struct tlvtype_proc tlvprocdestopt_lst[] = {
Amerigo Wang07a93622012-10-29 16:23:10 +0000292#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700293 {
294 .type = IPV6_TLV_HAO,
295 .func = ipv6_dest_hao,
296 },
297#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 {-1, NULL}
299};
300
Herbert Xue5bbef22007-10-15 12:50:28 -0700301static int ipv6_destopt_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct inet6_skb_parm *opt = IP6CB(skb);
Amerigo Wang07a93622012-10-29 16:23:10 +0000304#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700305 __u16 dstbuf;
306#endif
Eric Dumazet897dc802011-07-28 04:00:35 +0000307 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700309 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
310 !pskb_may_pull(skb, (skb_transport_offset(skb) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700311 ((skb_transport_header(skb)[1] + 1) << 3)))) {
Eric Dumazet897dc802011-07-28 04:00:35 +0000312 IP6_INC_STATS_BH(dev_net(dst->dev), ip6_dst_idev(dst),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900313 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 kfree_skb(skb);
315 return -1;
316 }
317
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300318 opt->lastopt = opt->dst1 = skb_network_header_len(skb);
Amerigo Wang07a93622012-10-29 16:23:10 +0000319#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700320 dstbuf = opt->dst1;
321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Herbert Xue5bbef22007-10-15 12:50:28 -0700323 if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700324 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
Masahide NAKAMURAdc435e62006-08-31 15:18:49 -0700325 opt = IP6CB(skb);
Amerigo Wang07a93622012-10-29 16:23:10 +0000326#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700327 opt->nhoff = dstbuf;
328#else
Patrick McHardy951dbc82006-01-06 23:02:34 -0800329 opt->nhoff = opt->dst1;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700330#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 1;
332 }
333
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700334 IP6_INC_STATS_BH(dev_net(dst->dev),
335 ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return -1;
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/********************************
340 Routing header.
341 ********************************/
342
Eric Dumazetf6bc7d92010-06-14 04:39:27 +0000343/* called with rcu_read_lock() */
Herbert Xue5bbef22007-10-15 12:50:28 -0700344static int ipv6_rthdr_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct inet6_skb_parm *opt = IP6CB(skb);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700347 struct in6_addr *addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct in6_addr daddr;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700349 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int n, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct ipv6_rt_hdr *hdr;
352 struct rt0_hdr *rthdr;
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700353 struct net *net = dev_net(skb->dev);
354 int accept_source_route = net->ipv6.devconf_all->accept_source_route;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700355
Eric Dumazetf6bc7d92010-06-14 04:39:27 +0000356 idev = __in6_dev_get(skb->dev);
357 if (idev && accept_source_route > idev->cnf.accept_source_route)
358 accept_source_route = idev->cnf.accept_source_route;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700359
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700360 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
361 !pskb_may_pull(skb, (skb_transport_offset(skb) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700362 ((skb_transport_header(skb)[1] + 1) << 3)))) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000363 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900364 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 kfree_skb(skb);
366 return -1;
367 }
368
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700369 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700371 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 skb->pkt_type != PACKET_HOST) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000373 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900374 IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 kfree_skb(skb);
376 return -1;
377 }
378
379looped_back:
380 if (hdr->segments_left == 0) {
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700381 switch (hdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000382#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700383 case IPV6_SRCRT_TYPE_2:
384 /* Silently discard type 2 header unless it was
385 * processed by own
386 */
387 if (!addr) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000388 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900389 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700390 kfree_skb(skb);
391 return -1;
392 }
393 break;
394#endif
395 default:
396 break;
397 }
398
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300399 opt->lastopt = opt->srcrt = skb_network_header_len(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700400 skb->transport_header += (hdr->hdrlen + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 opt->dst0 = opt->dst1;
402 opt->dst1 = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700403 opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 1;
405 }
406
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700407 switch (hdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000408#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700409 case IPV6_SRCRT_TYPE_2:
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700410 if (accept_source_route < 0)
411 goto unknown_rh;
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700412 /* Silently discard invalid RTH type 2 */
413 if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000414 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900415 IPSTATS_MIB_INHDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700416 kfree_skb(skb);
417 return -1;
418 }
419 break;
420#endif
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700421 default:
422 goto unknown_rh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /*
426 * This is the routing header forwarding algorithm from
427 * RFC 2460, page 16.
428 */
429
430 n = hdr->hdrlen >> 1;
431
432 if (hdr->segments_left > n) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000433 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900434 IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700435 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
436 ((&hdr->segments_left) -
437 skb_network_header(skb)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return -1;
439 }
440
441 /* We are about to mangle packet header. Be careful!
442 Do not damage packets queued somewhere.
443 */
444 if (skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /* the copy is a forwarded packet */
Herbert Xu65c88462007-10-15 01:29:10 -0700446 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000447 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900448 IPSTATS_MIB_OUTDISCARDS);
449 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return -1;
451 }
Herbert Xu65c88462007-10-15 01:29:10 -0700452 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
Patrick McHardy84fa7932006-08-29 16:44:56 -0700455 if (skb->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 skb->ip_summed = CHECKSUM_NONE;
457
458 i = n - --hdr->segments_left;
459
460 rthdr = (struct rt0_hdr *) hdr;
461 addr = rthdr->addr;
462 addr += i - 1;
463
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700464 switch (hdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000465#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700466 case IPV6_SRCRT_TYPE_2:
467 if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700468 (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700469 IPPROTO_ROUTING) < 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000470 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900471 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700472 kfree_skb(skb);
473 return -1;
474 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000475 if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
476 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900477 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700478 kfree_skb(skb);
479 return -1;
480 }
481 break;
482#endif
483 default:
484 break;
485 }
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (ipv6_addr_is_multicast(addr)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000488 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900489 IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 kfree_skb(skb);
491 return -1;
492 }
493
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000494 daddr = *addr;
495 *addr = ipv6_hdr(skb)->daddr;
496 ipv6_hdr(skb)->daddr = daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Eric Dumazetadf30902009-06-02 05:19:30 +0000498 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 ip6_route_input(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000500 if (skb_dst(skb)->error) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700501 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 dst_input(skb);
503 return -1;
504 }
505
Eric Dumazetadf30902009-06-02 05:19:30 +0000506 if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700507 if (ipv6_hdr(skb)->hop_limit <= 1) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000508 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900509 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000511 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 kfree_skb(skb);
513 return -1;
514 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700515 ipv6_hdr(skb)->hop_limit--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto looped_back;
517 }
518
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700519 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 dst_input(skb);
521 return -1;
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700522
523unknown_rh:
Eric Dumazetadf30902009-06-02 05:19:30 +0000524 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700525 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
526 (&hdr->type) - skb_network_header(skb));
527 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000530static const struct inet6_protocol rthdr_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 .handler = ipv6_rthdr_rcv,
Vlad Yasevich2207afc2012-11-15 08:49:19 +0000532 .flags = INET6_PROTO_NOPOLICY,
Vlad Yasevich8ca896c2012-11-15 08:49:13 +0000533};
534
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000535static const struct inet6_protocol destopt_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800536 .handler = ipv6_destopt_rcv,
Vlad Yasevich2207afc2012-11-15 08:49:19 +0000537 .flags = INET6_PROTO_NOPOLICY,
Vlad Yasevich8ca896c2012-11-15 08:49:13 +0000538};
539
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000540static const struct inet6_protocol nodata_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800541 .handler = dst_discard,
542 .flags = INET6_PROTO_NOPOLICY,
543};
544
545int __init ipv6_exthdrs_init(void)
546{
547 int ret;
548
Vlad Yasevich33362882012-11-15 08:49:15 +0000549 ret = ipv6_exthdrs_offload_init();
Daniel Lezcano248b2382007-12-11 02:23:54 -0800550 if (ret)
551 goto out;
552
Vlad Yasevich33362882012-11-15 08:49:15 +0000553 ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
554 if (ret)
555 goto out_offload;
556
Daniel Lezcano248b2382007-12-11 02:23:54 -0800557 ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
558 if (ret)
559 goto out_rthdr;
560
561 ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
562 if (ret)
563 goto out_destopt;
564
565out:
566 return ret;
Daniel Lezcano248b2382007-12-11 02:23:54 -0800567out_destopt:
568 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
Vlad Yasevich33362882012-11-15 08:49:15 +0000569out_rthdr:
570 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
571out_offload:
572 ipv6_exthdrs_offload_exit();
Daniel Lezcano248b2382007-12-11 02:23:54 -0800573 goto out;
574};
575
576void ipv6_exthdrs_exit(void)
577{
Vlad Yasevich2207afc2012-11-15 08:49:19 +0000578 ipv6_exthdrs_offload_exit();
Daniel Lezcano248b2382007-12-11 02:23:54 -0800579 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);