blob: aa0a51e64682ac4a9e8db3aa9e57f1c02f227f66 [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>
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -070046#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
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) {
78 case IPV6_TLV_PAD0:
79 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 *
99 * Parsing function "func" returns 1, if parsing succeed
100 * and 0, if it failed.
101 * It MUST NOT touch skb->h.
102 */
103
104struct tlvtype_proc {
105 int type;
Herbert Xue5bbef22007-10-15 12:50:28 -0700106 int (*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
Herbert Xue5bbef22007-10-15 12:50:28 -0700115static int 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 */
119 return 1;
120
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);
132 return 0;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 kfree_skb(skb);
136 return 0;
137}
138
139/* Parse tlv encoded option header (hop-by-hop or destination) */
140
Herbert Xue5bbef22007-10-15 12:50:28 -0700141static int ip6_parse_tlv(struct tlvtype_proc *procs, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700148 if (skb_transport_offset(skb) + len > skb_headlen(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto bad;
150
151 off += 2;
152 len -= 2;
153
154 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700155 int optlen = nh[off + 1] + 2;
Eldad Zackc1412fc2012-04-12 17:36:17 -0400156 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700158 switch (nh[off]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 case IPV6_TLV_PAD0:
160 optlen = 1;
161 break;
162
163 case IPV6_TLV_PADN:
Eldad Zackc1412fc2012-04-12 17:36:17 -0400164 /* RFC 2460 states that the purpose of PadN is
165 * to align the containing header to multiples
166 * of 8. 7 is therefore the highest valid value.
167 * See also RFC 4942, Section 2.1.9.5.
168 */
169 if (optlen > 7)
170 goto bad;
171 /* RFC 4942 recommends receiving hosts to
172 * actively check PadN payload to contain
173 * only zeroes.
174 */
175 for (i = 2; i < optlen; i++) {
176 if (nh[off + i] != 0)
177 goto bad;
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 break;
180
181 default: /* Other TLV code so scan list */
182 if (optlen > len)
183 goto bad;
184 for (curr=procs; curr->type >= 0; curr++) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700185 if (curr->type == nh[off]) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900186 /* type specific length/alignment
187 checks will be performed in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 func(). */
Herbert Xue5bbef22007-10-15 12:50:28 -0700189 if (curr->func(skb, off) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
191 break;
192 }
193 }
194 if (curr->type < 0) {
Herbert Xue5bbef22007-10-15 12:50:28 -0700195 if (ip6_tlvopt_unknown(skb, off) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return 0;
197 }
198 break;
199 }
200 off += optlen;
201 len -= optlen;
202 }
203 if (len == 0)
204 return 1;
205bad:
206 kfree_skb(skb);
207 return 0;
208}
209
210/*****************************
211 Destination options header.
212 *****************************/
213
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700214#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Herbert Xue5bbef22007-10-15 12:50:28 -0700215static int ipv6_dest_hao(struct sk_buff *skb, int optoff)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700216{
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700217 struct ipv6_destopt_hao *hao;
218 struct inet6_skb_parm *opt = IP6CB(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700219 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700220 struct in6_addr tmp_addr;
221 int ret;
222
223 if (opt->dsthao) {
224 LIMIT_NETDEBUG(KERN_DEBUG "hao duplicated\n");
225 goto discard;
226 }
227 opt->dsthao = opt->dst1;
228 opt->dst1 = 0;
229
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700230 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700231
232 if (hao->length != 16) {
233 LIMIT_NETDEBUG(
234 KERN_DEBUG "hao invalid option length = %d\n", hao->length);
235 goto discard;
236 }
237
238 if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
239 LIMIT_NETDEBUG(
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700240 KERN_DEBUG "hao is not an unicast addr: %pI6\n", &hao->addr);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700241 goto discard;
242 }
243
244 ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
245 (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
246 if (unlikely(ret < 0))
247 goto discard;
248
249 if (skb_cloned(skb)) {
Herbert Xu65c88462007-10-15 01:29:10 -0700250 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700251 goto discard;
252
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700253 /* update all variable using below by copied skbuff */
Herbert Xu65c88462007-10-15 01:29:10 -0700254 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700255 optoff);
Herbert Xu65c88462007-10-15 01:29:10 -0700256 ipv6h = ipv6_hdr(skb);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700257 }
258
259 if (skb->ip_summed == CHECKSUM_COMPLETE)
260 skb->ip_summed = CHECKSUM_NONE;
261
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000262 tmp_addr = ipv6h->saddr;
263 ipv6h->saddr = hao->addr;
264 hao->addr = tmp_addr;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700265
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700266 if (skb->tstamp.tv64 == 0)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700267 __net_timestamp(skb);
268
269 return 1;
270
271 discard:
272 kfree_skb(skb);
273 return 0;
274}
275#endif
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277static struct tlvtype_proc tlvprocdestopt_lst[] = {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700278#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700279 {
280 .type = IPV6_TLV_HAO,
281 .func = ipv6_dest_hao,
282 },
283#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 {-1, NULL}
285};
286
Herbert Xue5bbef22007-10-15 12:50:28 -0700287static int ipv6_destopt_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct inet6_skb_parm *opt = IP6CB(skb);
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700290#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700291 __u16 dstbuf;
292#endif
Eric Dumazet897dc802011-07-28 04:00:35 +0000293 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700295 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
296 !pskb_may_pull(skb, (skb_transport_offset(skb) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700297 ((skb_transport_header(skb)[1] + 1) << 3)))) {
Eric Dumazet897dc802011-07-28 04:00:35 +0000298 IP6_INC_STATS_BH(dev_net(dst->dev), ip6_dst_idev(dst),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900299 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 kfree_skb(skb);
301 return -1;
302 }
303
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300304 opt->lastopt = opt->dst1 = skb_network_header_len(skb);
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700305#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700306 dstbuf = opt->dst1;
307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Herbert Xue5bbef22007-10-15 12:50:28 -0700309 if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700310 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
Masahide NAKAMURAdc435e62006-08-31 15:18:49 -0700311 opt = IP6CB(skb);
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700312#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700313 opt->nhoff = dstbuf;
314#else
Patrick McHardy951dbc82006-01-06 23:02:34 -0800315 opt->nhoff = opt->dst1;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700316#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 1;
318 }
319
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700320 IP6_INC_STATS_BH(dev_net(dst->dev),
321 ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return -1;
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/********************************
326 Routing header.
327 ********************************/
328
Eric Dumazetf6bc7d92010-06-14 04:39:27 +0000329/* called with rcu_read_lock() */
Herbert Xue5bbef22007-10-15 12:50:28 -0700330static int ipv6_rthdr_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct inet6_skb_parm *opt = IP6CB(skb);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700333 struct in6_addr *addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct in6_addr daddr;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700335 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int n, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct ipv6_rt_hdr *hdr;
338 struct rt0_hdr *rthdr;
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700339 struct net *net = dev_net(skb->dev);
340 int accept_source_route = net->ipv6.devconf_all->accept_source_route;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700341
Eric Dumazetf6bc7d92010-06-14 04:39:27 +0000342 idev = __in6_dev_get(skb->dev);
343 if (idev && accept_source_route > idev->cnf.accept_source_route)
344 accept_source_route = idev->cnf.accept_source_route;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700345
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700346 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
347 !pskb_may_pull(skb, (skb_transport_offset(skb) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700348 ((skb_transport_header(skb)[1] + 1) << 3)))) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000349 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900350 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 kfree_skb(skb);
352 return -1;
353 }
354
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700355 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700357 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 skb->pkt_type != PACKET_HOST) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000359 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900360 IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 kfree_skb(skb);
362 return -1;
363 }
364
365looped_back:
366 if (hdr->segments_left == 0) {
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700367 switch (hdr->type) {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700368#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700369 case IPV6_SRCRT_TYPE_2:
370 /* Silently discard type 2 header unless it was
371 * processed by own
372 */
373 if (!addr) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000374 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900375 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700376 kfree_skb(skb);
377 return -1;
378 }
379 break;
380#endif
381 default:
382 break;
383 }
384
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300385 opt->lastopt = opt->srcrt = skb_network_header_len(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700386 skb->transport_header += (hdr->hdrlen + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 opt->dst0 = opt->dst1;
388 opt->dst1 = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700389 opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 1;
391 }
392
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700393 switch (hdr->type) {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700394#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700395 case IPV6_SRCRT_TYPE_2:
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700396 if (accept_source_route < 0)
397 goto unknown_rh;
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700398 /* Silently discard invalid RTH type 2 */
399 if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000400 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900401 IPSTATS_MIB_INHDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700402 kfree_skb(skb);
403 return -1;
404 }
405 break;
406#endif
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700407 default:
408 goto unknown_rh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /*
412 * This is the routing header forwarding algorithm from
413 * RFC 2460, page 16.
414 */
415
416 n = hdr->hdrlen >> 1;
417
418 if (hdr->segments_left > n) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000419 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900420 IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700421 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
422 ((&hdr->segments_left) -
423 skb_network_header(skb)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -1;
425 }
426
427 /* We are about to mangle packet header. Be careful!
428 Do not damage packets queued somewhere.
429 */
430 if (skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* the copy is a forwarded packet */
Herbert Xu65c88462007-10-15 01:29:10 -0700432 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
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_OUTDISCARDS);
435 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -1;
437 }
Herbert Xu65c88462007-10-15 01:29:10 -0700438 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
Patrick McHardy84fa7932006-08-29 16:44:56 -0700441 if (skb->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 skb->ip_summed = CHECKSUM_NONE;
443
444 i = n - --hdr->segments_left;
445
446 rthdr = (struct rt0_hdr *) hdr;
447 addr = rthdr->addr;
448 addr += i - 1;
449
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700450 switch (hdr->type) {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700451#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700452 case IPV6_SRCRT_TYPE_2:
453 if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700454 (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700455 IPPROTO_ROUTING) < 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000456 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900457 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700458 kfree_skb(skb);
459 return -1;
460 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000461 if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
462 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900463 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700464 kfree_skb(skb);
465 return -1;
466 }
467 break;
468#endif
469 default:
470 break;
471 }
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (ipv6_addr_is_multicast(addr)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000474 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900475 IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 kfree_skb(skb);
477 return -1;
478 }
479
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000480 daddr = *addr;
481 *addr = ipv6_hdr(skb)->daddr;
482 ipv6_hdr(skb)->daddr = daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Eric Dumazetadf30902009-06-02 05:19:30 +0000484 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 ip6_route_input(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000486 if (skb_dst(skb)->error) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700487 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 dst_input(skb);
489 return -1;
490 }
491
Eric Dumazetadf30902009-06-02 05:19:30 +0000492 if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700493 if (ipv6_hdr(skb)->hop_limit <= 1) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000494 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900495 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000497 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 kfree_skb(skb);
499 return -1;
500 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700501 ipv6_hdr(skb)->hop_limit--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto looped_back;
503 }
504
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700505 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 dst_input(skb);
507 return -1;
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700508
509unknown_rh:
Eric Dumazetadf30902009-06-02 05:19:30 +0000510 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700511 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
512 (&hdr->type) - skb_network_header(skb));
513 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000516static const struct inet6_protocol rthdr_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 .handler = ipv6_rthdr_rcv,
Herbert Xuadcfc7d2006-06-30 13:36:15 -0700518 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519};
520
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000521static const struct inet6_protocol destopt_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800522 .handler = ipv6_destopt_rcv,
523 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524};
525
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000526static const struct inet6_protocol nodata_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800527 .handler = dst_discard,
528 .flags = INET6_PROTO_NOPOLICY,
529};
530
531int __init ipv6_exthdrs_init(void)
532{
533 int ret;
534
535 ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
536 if (ret)
537 goto out;
538
539 ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
540 if (ret)
541 goto out_rthdr;
542
543 ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
544 if (ret)
545 goto out_destopt;
546
547out:
548 return ret;
549out_rthdr:
550 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
551out_destopt:
552 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
553 goto out;
554};
555
556void ipv6_exthdrs_exit(void)
557{
558 inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
559 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
560 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/**********************************
564 Hop-by-hop options.
565 **********************************/
566
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700567/*
Eric Dumazetadf30902009-06-02 05:19:30 +0000568 * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700569 */
570static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
571{
Eric Dumazetadf30902009-06-02 05:19:30 +0000572 return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700573}
574
David S. Miller2570a4f2010-01-13 17:27:37 -0800575static inline struct net *ipv6_skb_net(struct sk_buff *skb)
576{
577 return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/* Router Alert as of RFC 2711 */
581
Herbert Xue5bbef22007-10-15 12:50:28 -0700582static int ipv6_hop_ra(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700584 const unsigned char *nh = skb_network_header(skb);
Masahide NAKAMURAa80ff032006-08-23 19:19:50 -0700585
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700586 if (nh[optoff + 1] == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 IP6CB(skb)->ra = optoff;
588 return 1;
589 }
Patrick McHardy64ce2072005-08-09 20:50:53 -0700590 LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_ra: wrong RA length %d\n",
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700591 nh[optoff + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 kfree_skb(skb);
593 return 0;
594}
595
596/* Jumbo payload */
597
Herbert Xue5bbef22007-10-15 12:50:28 -0700598static int ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700600 const unsigned char *nh = skb_network_header(skb);
David S. Miller2570a4f2010-01-13 17:27:37 -0800601 struct net *net = ipv6_skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 u32 pkt_len;
603
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700604 if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
Patrick McHardy64ce2072005-08-09 20:50:53 -0700605 LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700606 nh[optoff+1]);
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700607 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900608 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 goto drop;
610 }
611
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700612 pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (pkt_len <= IPV6_MAXPLEN) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700614 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
615 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
617 return 0;
618 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700619 if (ipv6_hdr(skb)->payload_len) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700620 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
621 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
623 return 0;
624 }
625
626 if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700627 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
628 IPSTATS_MIB_INTRUNCATEDPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 goto drop;
630 }
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700631
632 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
633 goto drop;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 1;
636
637drop:
638 kfree_skb(skb);
639 return 0;
640}
641
642static struct tlvtype_proc tlvprochopopt_lst[] = {
643 {
644 .type = IPV6_TLV_ROUTERALERT,
645 .func = ipv6_hop_ra,
646 },
647 {
648 .type = IPV6_TLV_JUMBO,
649 .func = ipv6_hop_jumbo,
650 },
651 { -1, }
652};
653
Herbert Xue5bbef22007-10-15 12:50:28 -0700654int ipv6_parse_hopopts(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Patrick McHardy951dbc82006-01-06 23:02:34 -0800656 struct inet6_skb_parm *opt = IP6CB(skb);
657
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700658 /*
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700659 * skb_network_header(skb) is equal to skb->data, and
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300660 * skb_network_header_len(skb) is always equal to
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700661 * sizeof(struct ipv6hdr) by definition of
662 * hop-by-hop options.
663 */
664 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700665 !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
666 ((skb_transport_header(skb)[1] + 1) << 3)))) {
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700667 kfree_skb(skb);
668 return -1;
669 }
670
Patrick McHardy951dbc82006-01-06 23:02:34 -0800671 opt->hop = sizeof(struct ipv6hdr);
Herbert Xue5bbef22007-10-15 12:50:28 -0700672 if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700673 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
Masahide NAKAMURAdc435e62006-08-31 15:18:49 -0700674 opt = IP6CB(skb);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800675 opt->nhoff = sizeof(struct ipv6hdr);
YOSHIFUJI Hideakib8097392006-04-18 14:48:45 -0700676 return 1;
Patrick McHardy951dbc82006-01-06 23:02:34 -0800677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -1;
679}
680
681/*
682 * Creating outbound headers.
683 *
684 * "build" functions work when skb is filled from head to tail (datagram)
685 * "push" functions work when headers are added from tail to head (tcp)
686 *
687 * In both cases we assume, that caller reserved enough room
688 * for headers.
689 */
690
691static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
692 struct ipv6_rt_hdr *opt,
693 struct in6_addr **addr_p)
694{
695 struct rt0_hdr *phdr, *ihdr;
696 int hops;
697
698 ihdr = (struct rt0_hdr *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
701 memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
702
703 hops = ihdr->rt_hdr.hdrlen >> 1;
704
705 if (hops > 1)
706 memcpy(phdr->addr, ihdr->addr + 1,
707 (hops - 1) * sizeof(struct in6_addr));
708
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000709 phdr->addr[hops - 1] = **addr_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 *addr_p = ihdr->addr;
711
712 phdr->rt_hdr.nexthdr = *proto;
713 *proto = NEXTHDR_ROUTING;
714}
715
716static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
717{
718 struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt));
719
720 memcpy(h, opt, ipv6_optlen(opt));
721 h->nexthdr = *proto;
722 *proto = type;
723}
724
725void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
726 u8 *proto,
727 struct in6_addr **daddr)
728{
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900729 if (opt->srcrt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ipv6_push_rthdr(skb, proto, opt->srcrt, daddr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900731 /*
732 * IPV6_RTHDRDSTOPTS is ignored
733 * unless IPV6_RTHDR is set (RFC3542).
734 */
735 if (opt->dst0opt)
736 ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (opt->hopopt)
739 ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
740}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900741EXPORT_SYMBOL(ipv6_push_nfrag_opts);
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
744{
745 if (opt->dst1opt)
746 ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
747}
748
749struct ipv6_txoptions *
750ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
751{
752 struct ipv6_txoptions *opt2;
753
754 opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
755 if (opt2) {
Eldad Zackac3c8172012-04-01 07:49:04 +0000756 long dif = (char *)opt2 - (char *)opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 memcpy(opt2, opt, opt->tot_len);
758 if (opt2->hopopt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000759 *((char **)&opt2->hopopt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (opt2->dst0opt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000761 *((char **)&opt2->dst0opt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (opt2->dst1opt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000763 *((char **)&opt2->dst1opt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (opt2->srcrt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000765 *((char **)&opt2->srcrt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 return opt2;
768}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800769EXPORT_SYMBOL_GPL(ipv6_dup_options);
770
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900771static int ipv6_renew_option(void *ohdr,
772 struct ipv6_opt_hdr __user *newopt, int newoptlen,
773 int inherit,
774 struct ipv6_opt_hdr **hdr,
775 char **p)
776{
777 if (inherit) {
778 if (ohdr) {
779 memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
780 *hdr = (struct ipv6_opt_hdr *)*p;
781 *p += CMSG_ALIGN(ipv6_optlen(*(struct ipv6_opt_hdr **)hdr));
782 }
783 } else {
784 if (newopt) {
785 if (copy_from_user(*p, newopt, newoptlen))
786 return -EFAULT;
787 *hdr = (struct ipv6_opt_hdr *)*p;
788 if (ipv6_optlen(*(struct ipv6_opt_hdr **)hdr) > newoptlen)
789 return -EINVAL;
790 *p += CMSG_ALIGN(newoptlen);
791 }
792 }
793 return 0;
794}
795
796struct ipv6_txoptions *
797ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
798 int newtype,
799 struct ipv6_opt_hdr __user *newopt, int newoptlen)
800{
801 int tot_len = 0;
802 char *p;
803 struct ipv6_txoptions *opt2;
804 int err;
805
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700806 if (opt) {
807 if (newtype != IPV6_HOPOPTS && opt->hopopt)
808 tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
809 if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
810 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
811 if (newtype != IPV6_RTHDR && opt->srcrt)
812 tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
813 if (newtype != IPV6_DSTOPTS && opt->dst1opt)
814 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
815 }
816
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900817 if (newopt && newoptlen)
818 tot_len += CMSG_ALIGN(newoptlen);
819
820 if (!tot_len)
821 return NULL;
822
YOSHIFUJI Hideaki8b8aa4b2005-11-20 12:18:17 +0900823 tot_len += sizeof(*opt2);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900824 opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
825 if (!opt2)
826 return ERR_PTR(-ENOBUFS);
827
828 memset(opt2, 0, tot_len);
829
830 opt2->tot_len = tot_len;
831 p = (char *)(opt2 + 1);
832
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700833 err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900834 newtype != IPV6_HOPOPTS,
835 &opt2->hopopt, &p);
836 if (err)
837 goto out;
838
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700839 err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900840 newtype != IPV6_RTHDRDSTOPTS,
841 &opt2->dst0opt, &p);
842 if (err)
843 goto out;
844
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700845 err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900846 newtype != IPV6_RTHDR,
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700847 (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900848 if (err)
849 goto out;
850
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700851 err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900852 newtype != IPV6_DSTOPTS,
853 &opt2->dst1opt, &p);
854 if (err)
855 goto out;
856
857 opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
858 (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
859 (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
860 opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
861
862 return opt2;
863out:
YOSHIFUJI Hideaki8b8aa4b2005-11-20 12:18:17 +0900864 sock_kfree_s(sk, opt2, opt2->tot_len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900865 return ERR_PTR(err);
866}
867
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900868struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
869 struct ipv6_txoptions *opt)
870{
871 /*
872 * ignore the dest before srcrt unless srcrt is being included.
873 * --yoshfuji
874 */
875 if (opt && opt->dst0opt && !opt->srcrt) {
876 if (opt_space != opt) {
877 memcpy(opt_space, opt, sizeof(*opt_space));
878 opt = opt_space;
879 }
880 opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
881 opt->dst0opt = NULL;
882 }
883
884 return opt;
885}
886
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000887/**
888 * fl6_update_dst - update flowi destination address with info given
889 * by srcrt option, if any.
890 *
David S. Miller4c9483b2011-03-12 16:22:43 -0500891 * @fl6: flowi6 for which daddr is to be updated
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000892 * @opt: struct ipv6_txoptions in which to look for srcrt opt
David S. Miller4c9483b2011-03-12 16:22:43 -0500893 * @orig: copy of original daddr address if modified
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000894 *
895 * Returns NULL if no txoptions or no srcrt, otherwise returns orig
David S. Miller4c9483b2011-03-12 16:22:43 -0500896 * and initial value of fl6->daddr set in orig
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000897 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500898struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000899 const struct ipv6_txoptions *opt,
900 struct in6_addr *orig)
901{
902 if (!opt || !opt->srcrt)
903 return NULL;
904
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000905 *orig = fl6->daddr;
906 fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000907 return orig;
908}
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000909EXPORT_SYMBOL_GPL(fl6_update_dst);