blob: 3d641b6e9b09256cb43f1cca0e9cabcc1526e30f [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700157 switch (nh[off]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 case IPV6_TLV_PAD0:
159 optlen = 1;
160 break;
161
162 case IPV6_TLV_PADN:
163 break;
164
165 default: /* Other TLV code so scan list */
166 if (optlen > len)
167 goto bad;
168 for (curr=procs; curr->type >= 0; curr++) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700169 if (curr->type == nh[off]) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900170 /* type specific length/alignment
171 checks will be performed in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 func(). */
Herbert Xue5bbef22007-10-15 12:50:28 -0700173 if (curr->func(skb, off) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
175 break;
176 }
177 }
178 if (curr->type < 0) {
Herbert Xue5bbef22007-10-15 12:50:28 -0700179 if (ip6_tlvopt_unknown(skb, off) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
181 }
182 break;
183 }
184 off += optlen;
185 len -= optlen;
186 }
187 if (len == 0)
188 return 1;
189bad:
190 kfree_skb(skb);
191 return 0;
192}
193
194/*****************************
195 Destination options header.
196 *****************************/
197
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700198#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Herbert Xue5bbef22007-10-15 12:50:28 -0700199static int ipv6_dest_hao(struct sk_buff *skb, int optoff)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700200{
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700201 struct ipv6_destopt_hao *hao;
202 struct inet6_skb_parm *opt = IP6CB(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700203 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700204 struct in6_addr tmp_addr;
205 int ret;
206
207 if (opt->dsthao) {
208 LIMIT_NETDEBUG(KERN_DEBUG "hao duplicated\n");
209 goto discard;
210 }
211 opt->dsthao = opt->dst1;
212 opt->dst1 = 0;
213
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700214 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700215
216 if (hao->length != 16) {
217 LIMIT_NETDEBUG(
218 KERN_DEBUG "hao invalid option length = %d\n", hao->length);
219 goto discard;
220 }
221
222 if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
223 LIMIT_NETDEBUG(
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700224 KERN_DEBUG "hao is not an unicast addr: %pI6\n", &hao->addr);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700225 goto discard;
226 }
227
228 ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
229 (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
230 if (unlikely(ret < 0))
231 goto discard;
232
233 if (skb_cloned(skb)) {
Herbert Xu65c88462007-10-15 01:29:10 -0700234 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700235 goto discard;
236
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700237 /* update all variable using below by copied skbuff */
Herbert Xu65c88462007-10-15 01:29:10 -0700238 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700239 optoff);
Herbert Xu65c88462007-10-15 01:29:10 -0700240 ipv6h = ipv6_hdr(skb);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700241 }
242
243 if (skb->ip_summed == CHECKSUM_COMPLETE)
244 skb->ip_summed = CHECKSUM_NONE;
245
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000246 tmp_addr = ipv6h->saddr;
247 ipv6h->saddr = hao->addr;
248 hao->addr = tmp_addr;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700249
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700250 if (skb->tstamp.tv64 == 0)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700251 __net_timestamp(skb);
252
253 return 1;
254
255 discard:
256 kfree_skb(skb);
257 return 0;
258}
259#endif
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static struct tlvtype_proc tlvprocdestopt_lst[] = {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700262#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700263 {
264 .type = IPV6_TLV_HAO,
265 .func = ipv6_dest_hao,
266 },
267#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 {-1, NULL}
269};
270
Herbert Xue5bbef22007-10-15 12:50:28 -0700271static int ipv6_destopt_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct inet6_skb_parm *opt = IP6CB(skb);
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700274#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700275 __u16 dstbuf;
276#endif
Eric Dumazet897dc802011-07-28 04:00:35 +0000277 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700279 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
280 !pskb_may_pull(skb, (skb_transport_offset(skb) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700281 ((skb_transport_header(skb)[1] + 1) << 3)))) {
Eric Dumazet897dc802011-07-28 04:00:35 +0000282 IP6_INC_STATS_BH(dev_net(dst->dev), ip6_dst_idev(dst),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900283 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 kfree_skb(skb);
285 return -1;
286 }
287
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300288 opt->lastopt = opt->dst1 = skb_network_header_len(skb);
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700289#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700290 dstbuf = opt->dst1;
291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Herbert Xue5bbef22007-10-15 12:50:28 -0700293 if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700294 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
Masahide NAKAMURAdc435e62006-08-31 15:18:49 -0700295 opt = IP6CB(skb);
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700296#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700297 opt->nhoff = dstbuf;
298#else
Patrick McHardy951dbc82006-01-06 23:02:34 -0800299 opt->nhoff = opt->dst1;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 1;
302 }
303
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700304 IP6_INC_STATS_BH(dev_net(dst->dev),
305 ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return -1;
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/********************************
310 Routing header.
311 ********************************/
312
Eric Dumazetf6bc7d92010-06-14 04:39:27 +0000313/* called with rcu_read_lock() */
Herbert Xue5bbef22007-10-15 12:50:28 -0700314static int ipv6_rthdr_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct inet6_skb_parm *opt = IP6CB(skb);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700317 struct in6_addr *addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct in6_addr daddr;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700319 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int n, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct ipv6_rt_hdr *hdr;
322 struct rt0_hdr *rthdr;
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700323 struct net *net = dev_net(skb->dev);
324 int accept_source_route = net->ipv6.devconf_all->accept_source_route;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700325
Eric Dumazetf6bc7d92010-06-14 04:39:27 +0000326 idev = __in6_dev_get(skb->dev);
327 if (idev && accept_source_route > idev->cnf.accept_source_route)
328 accept_source_route = idev->cnf.accept_source_route;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700329
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700330 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
331 !pskb_may_pull(skb, (skb_transport_offset(skb) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700332 ((skb_transport_header(skb)[1] + 1) << 3)))) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000333 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900334 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 kfree_skb(skb);
336 return -1;
337 }
338
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700339 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700341 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 skb->pkt_type != PACKET_HOST) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000343 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900344 IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 kfree_skb(skb);
346 return -1;
347 }
348
349looped_back:
350 if (hdr->segments_left == 0) {
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700351 switch (hdr->type) {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700352#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700353 case IPV6_SRCRT_TYPE_2:
354 /* Silently discard type 2 header unless it was
355 * processed by own
356 */
357 if (!addr) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000358 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900359 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700360 kfree_skb(skb);
361 return -1;
362 }
363 break;
364#endif
365 default:
366 break;
367 }
368
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300369 opt->lastopt = opt->srcrt = skb_network_header_len(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700370 skb->transport_header += (hdr->hdrlen + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 opt->dst0 = opt->dst1;
372 opt->dst1 = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700373 opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 1;
375 }
376
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700377 switch (hdr->type) {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700378#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700379 case IPV6_SRCRT_TYPE_2:
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700380 if (accept_source_route < 0)
381 goto unknown_rh;
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700382 /* Silently discard invalid RTH type 2 */
383 if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000384 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900385 IPSTATS_MIB_INHDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700386 kfree_skb(skb);
387 return -1;
388 }
389 break;
390#endif
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700391 default:
392 goto unknown_rh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /*
396 * This is the routing header forwarding algorithm from
397 * RFC 2460, page 16.
398 */
399
400 n = hdr->hdrlen >> 1;
401
402 if (hdr->segments_left > n) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000403 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900404 IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700405 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
406 ((&hdr->segments_left) -
407 skb_network_header(skb)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -1;
409 }
410
411 /* We are about to mangle packet header. Be careful!
412 Do not damage packets queued somewhere.
413 */
414 if (skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* the copy is a forwarded packet */
Herbert Xu65c88462007-10-15 01:29:10 -0700416 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000417 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900418 IPSTATS_MIB_OUTDISCARDS);
419 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return -1;
421 }
Herbert Xu65c88462007-10-15 01:29:10 -0700422 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
Patrick McHardy84fa7932006-08-29 16:44:56 -0700425 if (skb->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 skb->ip_summed = CHECKSUM_NONE;
427
428 i = n - --hdr->segments_left;
429
430 rthdr = (struct rt0_hdr *) hdr;
431 addr = rthdr->addr;
432 addr += i - 1;
433
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700434 switch (hdr->type) {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700435#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700436 case IPV6_SRCRT_TYPE_2:
437 if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700438 (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700439 IPPROTO_ROUTING) < 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000440 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900441 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700442 kfree_skb(skb);
443 return -1;
444 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000445 if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
446 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900447 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700448 kfree_skb(skb);
449 return -1;
450 }
451 break;
452#endif
453 default:
454 break;
455 }
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (ipv6_addr_is_multicast(addr)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000458 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900459 IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 kfree_skb(skb);
461 return -1;
462 }
463
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000464 daddr = *addr;
465 *addr = ipv6_hdr(skb)->daddr;
466 ipv6_hdr(skb)->daddr = daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Eric Dumazetadf30902009-06-02 05:19:30 +0000468 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ip6_route_input(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000470 if (skb_dst(skb)->error) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700471 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 dst_input(skb);
473 return -1;
474 }
475
Eric Dumazetadf30902009-06-02 05:19:30 +0000476 if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700477 if (ipv6_hdr(skb)->hop_limit <= 1) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000478 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900479 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000481 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 kfree_skb(skb);
483 return -1;
484 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700485 ipv6_hdr(skb)->hop_limit--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 goto looped_back;
487 }
488
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700489 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 dst_input(skb);
491 return -1;
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700492
493unknown_rh:
Eric Dumazetadf30902009-06-02 05:19:30 +0000494 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700495 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
496 (&hdr->type) - skb_network_header(skb));
497 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000500static const struct inet6_protocol rthdr_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 .handler = ipv6_rthdr_rcv,
Herbert Xuadcfc7d2006-06-30 13:36:15 -0700502 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503};
504
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000505static const struct inet6_protocol destopt_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800506 .handler = ipv6_destopt_rcv,
507 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508};
509
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000510static const struct inet6_protocol nodata_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800511 .handler = dst_discard,
512 .flags = INET6_PROTO_NOPOLICY,
513};
514
515int __init ipv6_exthdrs_init(void)
516{
517 int ret;
518
519 ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
520 if (ret)
521 goto out;
522
523 ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
524 if (ret)
525 goto out_rthdr;
526
527 ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
528 if (ret)
529 goto out_destopt;
530
531out:
532 return ret;
533out_rthdr:
534 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
535out_destopt:
536 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
537 goto out;
538};
539
540void ipv6_exthdrs_exit(void)
541{
542 inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
543 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
544 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
545}
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/**********************************
548 Hop-by-hop options.
549 **********************************/
550
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700551/*
Eric Dumazetadf30902009-06-02 05:19:30 +0000552 * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700553 */
554static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
555{
Eric Dumazetadf30902009-06-02 05:19:30 +0000556 return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700557}
558
David S. Miller2570a4f2010-01-13 17:27:37 -0800559static inline struct net *ipv6_skb_net(struct sk_buff *skb)
560{
561 return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/* Router Alert as of RFC 2711 */
565
Herbert Xue5bbef22007-10-15 12:50:28 -0700566static int ipv6_hop_ra(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700568 const unsigned char *nh = skb_network_header(skb);
Masahide NAKAMURAa80ff032006-08-23 19:19:50 -0700569
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700570 if (nh[optoff + 1] == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 IP6CB(skb)->ra = optoff;
572 return 1;
573 }
Patrick McHardy64ce2072005-08-09 20:50:53 -0700574 LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_ra: wrong RA length %d\n",
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700575 nh[optoff + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 kfree_skb(skb);
577 return 0;
578}
579
580/* Jumbo payload */
581
Herbert Xue5bbef22007-10-15 12:50:28 -0700582static int ipv6_hop_jumbo(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);
David S. Miller2570a4f2010-01-13 17:27:37 -0800585 struct net *net = ipv6_skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 u32 pkt_len;
587
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700588 if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
Patrick McHardy64ce2072005-08-09 20:50:53 -0700589 LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700590 nh[optoff+1]);
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700591 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900592 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 goto drop;
594 }
595
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700596 pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (pkt_len <= IPV6_MAXPLEN) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700598 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
599 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
601 return 0;
602 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700603 if (ipv6_hdr(skb)->payload_len) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700604 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
605 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
607 return 0;
608 }
609
610 if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700611 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
612 IPSTATS_MIB_INTRUNCATEDPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 goto drop;
614 }
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700615
616 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
617 goto drop;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return 1;
620
621drop:
622 kfree_skb(skb);
623 return 0;
624}
625
626static struct tlvtype_proc tlvprochopopt_lst[] = {
627 {
628 .type = IPV6_TLV_ROUTERALERT,
629 .func = ipv6_hop_ra,
630 },
631 {
632 .type = IPV6_TLV_JUMBO,
633 .func = ipv6_hop_jumbo,
634 },
635 { -1, }
636};
637
Herbert Xue5bbef22007-10-15 12:50:28 -0700638int ipv6_parse_hopopts(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Patrick McHardy951dbc82006-01-06 23:02:34 -0800640 struct inet6_skb_parm *opt = IP6CB(skb);
641
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700642 /*
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700643 * skb_network_header(skb) is equal to skb->data, and
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300644 * skb_network_header_len(skb) is always equal to
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700645 * sizeof(struct ipv6hdr) by definition of
646 * hop-by-hop options.
647 */
648 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700649 !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
650 ((skb_transport_header(skb)[1] + 1) << 3)))) {
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700651 kfree_skb(skb);
652 return -1;
653 }
654
Patrick McHardy951dbc82006-01-06 23:02:34 -0800655 opt->hop = sizeof(struct ipv6hdr);
Herbert Xue5bbef22007-10-15 12:50:28 -0700656 if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700657 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
Masahide NAKAMURAdc435e62006-08-31 15:18:49 -0700658 opt = IP6CB(skb);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800659 opt->nhoff = sizeof(struct ipv6hdr);
YOSHIFUJI Hideakib8097392006-04-18 14:48:45 -0700660 return 1;
Patrick McHardy951dbc82006-01-06 23:02:34 -0800661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return -1;
663}
664
665/*
666 * Creating outbound headers.
667 *
668 * "build" functions work when skb is filled from head to tail (datagram)
669 * "push" functions work when headers are added from tail to head (tcp)
670 *
671 * In both cases we assume, that caller reserved enough room
672 * for headers.
673 */
674
675static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
676 struct ipv6_rt_hdr *opt,
677 struct in6_addr **addr_p)
678{
679 struct rt0_hdr *phdr, *ihdr;
680 int hops;
681
682 ihdr = (struct rt0_hdr *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
685 memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
686
687 hops = ihdr->rt_hdr.hdrlen >> 1;
688
689 if (hops > 1)
690 memcpy(phdr->addr, ihdr->addr + 1,
691 (hops - 1) * sizeof(struct in6_addr));
692
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000693 phdr->addr[hops - 1] = **addr_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 *addr_p = ihdr->addr;
695
696 phdr->rt_hdr.nexthdr = *proto;
697 *proto = NEXTHDR_ROUTING;
698}
699
700static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
701{
702 struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt));
703
704 memcpy(h, opt, ipv6_optlen(opt));
705 h->nexthdr = *proto;
706 *proto = type;
707}
708
709void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
710 u8 *proto,
711 struct in6_addr **daddr)
712{
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900713 if (opt->srcrt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ipv6_push_rthdr(skb, proto, opt->srcrt, daddr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900715 /*
716 * IPV6_RTHDRDSTOPTS is ignored
717 * unless IPV6_RTHDR is set (RFC3542).
718 */
719 if (opt->dst0opt)
720 ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (opt->hopopt)
723 ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
724}
725
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900726EXPORT_SYMBOL(ipv6_push_nfrag_opts);
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
729{
730 if (opt->dst1opt)
731 ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
732}
733
734struct ipv6_txoptions *
735ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
736{
737 struct ipv6_txoptions *opt2;
738
739 opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
740 if (opt2) {
741 long dif = (char*)opt2 - (char*)opt;
742 memcpy(opt2, opt, opt->tot_len);
743 if (opt2->hopopt)
744 *((char**)&opt2->hopopt) += dif;
745 if (opt2->dst0opt)
746 *((char**)&opt2->dst0opt) += dif;
747 if (opt2->dst1opt)
748 *((char**)&opt2->dst1opt) += dif;
749 if (opt2->srcrt)
750 *((char**)&opt2->srcrt) += dif;
751 }
752 return opt2;
753}
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900754
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800755EXPORT_SYMBOL_GPL(ipv6_dup_options);
756
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900757static int ipv6_renew_option(void *ohdr,
758 struct ipv6_opt_hdr __user *newopt, int newoptlen,
759 int inherit,
760 struct ipv6_opt_hdr **hdr,
761 char **p)
762{
763 if (inherit) {
764 if (ohdr) {
765 memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
766 *hdr = (struct ipv6_opt_hdr *)*p;
767 *p += CMSG_ALIGN(ipv6_optlen(*(struct ipv6_opt_hdr **)hdr));
768 }
769 } else {
770 if (newopt) {
771 if (copy_from_user(*p, newopt, newoptlen))
772 return -EFAULT;
773 *hdr = (struct ipv6_opt_hdr *)*p;
774 if (ipv6_optlen(*(struct ipv6_opt_hdr **)hdr) > newoptlen)
775 return -EINVAL;
776 *p += CMSG_ALIGN(newoptlen);
777 }
778 }
779 return 0;
780}
781
782struct ipv6_txoptions *
783ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
784 int newtype,
785 struct ipv6_opt_hdr __user *newopt, int newoptlen)
786{
787 int tot_len = 0;
788 char *p;
789 struct ipv6_txoptions *opt2;
790 int err;
791
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700792 if (opt) {
793 if (newtype != IPV6_HOPOPTS && opt->hopopt)
794 tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
795 if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
796 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
797 if (newtype != IPV6_RTHDR && opt->srcrt)
798 tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
799 if (newtype != IPV6_DSTOPTS && opt->dst1opt)
800 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
801 }
802
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900803 if (newopt && newoptlen)
804 tot_len += CMSG_ALIGN(newoptlen);
805
806 if (!tot_len)
807 return NULL;
808
YOSHIFUJI Hideaki8b8aa4b2005-11-20 12:18:17 +0900809 tot_len += sizeof(*opt2);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900810 opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
811 if (!opt2)
812 return ERR_PTR(-ENOBUFS);
813
814 memset(opt2, 0, tot_len);
815
816 opt2->tot_len = tot_len;
817 p = (char *)(opt2 + 1);
818
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700819 err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900820 newtype != IPV6_HOPOPTS,
821 &opt2->hopopt, &p);
822 if (err)
823 goto out;
824
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700825 err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900826 newtype != IPV6_RTHDRDSTOPTS,
827 &opt2->dst0opt, &p);
828 if (err)
829 goto out;
830
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700831 err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900832 newtype != IPV6_RTHDR,
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700833 (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900834 if (err)
835 goto out;
836
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700837 err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900838 newtype != IPV6_DSTOPTS,
839 &opt2->dst1opt, &p);
840 if (err)
841 goto out;
842
843 opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
844 (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
845 (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
846 opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
847
848 return opt2;
849out:
YOSHIFUJI Hideaki8b8aa4b2005-11-20 12:18:17 +0900850 sock_kfree_s(sk, opt2, opt2->tot_len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900851 return ERR_PTR(err);
852}
853
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900854struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
855 struct ipv6_txoptions *opt)
856{
857 /*
858 * ignore the dest before srcrt unless srcrt is being included.
859 * --yoshfuji
860 */
861 if (opt && opt->dst0opt && !opt->srcrt) {
862 if (opt_space != opt) {
863 memcpy(opt_space, opt, sizeof(*opt_space));
864 opt = opt_space;
865 }
866 opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
867 opt->dst0opt = NULL;
868 }
869
870 return opt;
871}
872
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000873/**
874 * fl6_update_dst - update flowi destination address with info given
875 * by srcrt option, if any.
876 *
David S. Miller4c9483b2011-03-12 16:22:43 -0500877 * @fl6: flowi6 for which daddr is to be updated
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000878 * @opt: struct ipv6_txoptions in which to look for srcrt opt
David S. Miller4c9483b2011-03-12 16:22:43 -0500879 * @orig: copy of original daddr address if modified
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000880 *
881 * Returns NULL if no txoptions or no srcrt, otherwise returns orig
David S. Miller4c9483b2011-03-12 16:22:43 -0500882 * and initial value of fl6->daddr set in orig
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000883 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500884struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000885 const struct ipv6_txoptions *opt,
886 struct in6_addr *orig)
887{
888 if (!opt || !opt->srcrt)
889 return NULL;
890
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000891 *orig = fl6->daddr;
892 fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000893 return orig;
894}
895
896EXPORT_SYMBOL_GPL(fl6_update_dst);