blob: a786a20ad823985299591736ad79f8dbd6d9a78f [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
53/*
54 * Parsing tlv encoded headers.
55 *
Eric Dumazeta50feda2012-05-18 18:57:34 +000056 * Parsing function "func" returns true, if parsing succeed
57 * and false, if it failed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * It MUST NOT touch skb->h.
59 */
60
61struct tlvtype_proc {
62 int type;
Eric Dumazeta50feda2012-05-18 18:57:34 +000063 bool (*func)(struct sk_buff *skb, int offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66/*********************
67 Generic functions
68 *********************/
69
70/* An unknown option is detected, decide what to do */
71
Eric Dumazeta50feda2012-05-18 18:57:34 +000072static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070074 switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 case 0: /* ignore */
Eric Dumazeta50feda2012-05-18 18:57:34 +000076 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 case 1: /* drop packet */
79 break;
80
81 case 3: /* Send ICMP if not a multicast address and drop packet */
82 /* Actually, it is redundant check. icmp_send
83 will recheck in any case.
84 */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070085 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 break;
87 case 2: /* send ICMP PARM PROB regardless and drop packet */
88 icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff);
Eric Dumazeta50feda2012-05-18 18:57:34 +000089 return false;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +000093 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96/* Parse tlv encoded option header (hop-by-hop or destination) */
97
Eric Dumazeta50feda2012-05-18 18:57:34 +000098static bool ip6_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Eric Dumazeta50feda2012-05-18 18:57:34 +0000100 const struct tlvtype_proc *curr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700101 const unsigned char *nh = skb_network_header(skb);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300102 int off = skb_network_header_len(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700103 int len = (skb_transport_header(skb)[1] + 1) << 3;
Eldad Zack9b905fe2012-05-20 01:59:33 +0000104 int padlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700106 if (skb_transport_offset(skb) + len > skb_headlen(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 goto bad;
108
109 off += 2;
110 len -= 2;
111
112 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700113 int optlen = nh[off + 1] + 2;
Eldad Zackc1412fc2012-04-12 17:36:17 -0400114 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700116 switch (nh[off]) {
Eldad Zack1de5a712012-05-17 06:00:25 +0000117 case IPV6_TLV_PAD1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 optlen = 1;
Eldad Zack9b905fe2012-05-20 01:59:33 +0000119 padlen++;
120 if (padlen > 7)
121 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 break;
123
124 case IPV6_TLV_PADN:
Eldad Zackc1412fc2012-04-12 17:36:17 -0400125 /* RFC 2460 states that the purpose of PadN is
126 * to align the containing header to multiples
127 * of 8. 7 is therefore the highest valid value.
128 * See also RFC 4942, Section 2.1.9.5.
129 */
Eldad Zack9b905fe2012-05-20 01:59:33 +0000130 padlen += optlen;
131 if (padlen > 7)
Eldad Zackc1412fc2012-04-12 17:36:17 -0400132 goto bad;
133 /* RFC 4942 recommends receiving hosts to
134 * actively check PadN payload to contain
135 * only zeroes.
136 */
137 for (i = 2; i < optlen; i++) {
138 if (nh[off + i] != 0)
139 goto bad;
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
142
143 default: /* Other TLV code so scan list */
144 if (optlen > len)
145 goto bad;
146 for (curr=procs; curr->type >= 0; curr++) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700147 if (curr->type == nh[off]) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900148 /* type specific length/alignment
149 checks will be performed in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 func(). */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000151 if (curr->func(skb, off) == false)
152 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 break;
154 }
155 }
156 if (curr->type < 0) {
Herbert Xue5bbef22007-10-15 12:50:28 -0700157 if (ip6_tlvopt_unknown(skb, off) == 0)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000158 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Eldad Zack9b905fe2012-05-20 01:59:33 +0000160 padlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 break;
162 }
163 off += optlen;
164 len -= optlen;
165 }
Eldad Zack9b905fe2012-05-20 01:59:33 +0000166 /* This case will not be caught by above check since its padding
167 * length is smaller than 7:
168 * 1 byte NH + 1 byte Length + 6 bytes Padding
169 */
170 if ((padlen == 6) && ((off - skb_network_header_len(skb)) == 8))
171 goto bad;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (len == 0)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000174 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175bad:
176 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000177 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/*****************************
181 Destination options header.
182 *****************************/
183
Amerigo Wang07a93622012-10-29 16:23:10 +0000184#if IS_ENABLED(CONFIG_IPV6_MIP6)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000185static bool ipv6_dest_hao(struct sk_buff *skb, int optoff)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700186{
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700187 struct ipv6_destopt_hao *hao;
188 struct inet6_skb_parm *opt = IP6CB(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700189 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700190 struct in6_addr tmp_addr;
191 int ret;
192
193 if (opt->dsthao) {
194 LIMIT_NETDEBUG(KERN_DEBUG "hao duplicated\n");
195 goto discard;
196 }
197 opt->dsthao = opt->dst1;
198 opt->dst1 = 0;
199
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700200 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700201
202 if (hao->length != 16) {
203 LIMIT_NETDEBUG(
204 KERN_DEBUG "hao invalid option length = %d\n", hao->length);
205 goto discard;
206 }
207
208 if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
209 LIMIT_NETDEBUG(
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700210 KERN_DEBUG "hao is not an unicast addr: %pI6\n", &hao->addr);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700211 goto discard;
212 }
213
214 ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
215 (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
216 if (unlikely(ret < 0))
217 goto discard;
218
219 if (skb_cloned(skb)) {
Herbert Xu65c88462007-10-15 01:29:10 -0700220 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700221 goto discard;
222
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700223 /* update all variable using below by copied skbuff */
Herbert Xu65c88462007-10-15 01:29:10 -0700224 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700225 optoff);
Herbert Xu65c88462007-10-15 01:29:10 -0700226 ipv6h = ipv6_hdr(skb);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700227 }
228
229 if (skb->ip_summed == CHECKSUM_COMPLETE)
230 skb->ip_summed = CHECKSUM_NONE;
231
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000232 tmp_addr = ipv6h->saddr;
233 ipv6h->saddr = hao->addr;
234 hao->addr = tmp_addr;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700235
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700236 if (skb->tstamp.tv64 == 0)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700237 __net_timestamp(skb);
238
Eric Dumazeta50feda2012-05-18 18:57:34 +0000239 return true;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700240
241 discard:
242 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000243 return false;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700244}
245#endif
246
Eric Dumazeta50feda2012-05-18 18:57:34 +0000247static const struct tlvtype_proc tlvprocdestopt_lst[] = {
Amerigo Wang07a93622012-10-29 16:23:10 +0000248#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700249 {
250 .type = IPV6_TLV_HAO,
251 .func = ipv6_dest_hao,
252 },
253#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 {-1, NULL}
255};
256
Herbert Xue5bbef22007-10-15 12:50:28 -0700257static int ipv6_destopt_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct inet6_skb_parm *opt = IP6CB(skb);
Amerigo Wang07a93622012-10-29 16:23:10 +0000260#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700261 __u16 dstbuf;
262#endif
Eric Dumazet897dc802011-07-28 04:00:35 +0000263 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700265 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
266 !pskb_may_pull(skb, (skb_transport_offset(skb) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700267 ((skb_transport_header(skb)[1] + 1) << 3)))) {
Eric Dumazet897dc802011-07-28 04:00:35 +0000268 IP6_INC_STATS_BH(dev_net(dst->dev), ip6_dst_idev(dst),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900269 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 kfree_skb(skb);
271 return -1;
272 }
273
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300274 opt->lastopt = opt->dst1 = skb_network_header_len(skb);
Amerigo Wang07a93622012-10-29 16:23:10 +0000275#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700276 dstbuf = opt->dst1;
277#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Herbert Xue5bbef22007-10-15 12:50:28 -0700279 if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700280 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
Masahide NAKAMURAdc435e62006-08-31 15:18:49 -0700281 opt = IP6CB(skb);
Amerigo Wang07a93622012-10-29 16:23:10 +0000282#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700283 opt->nhoff = dstbuf;
284#else
Patrick McHardy951dbc82006-01-06 23:02:34 -0800285 opt->nhoff = opt->dst1;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700286#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 1;
288 }
289
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700290 IP6_INC_STATS_BH(dev_net(dst->dev),
291 ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return -1;
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/********************************
296 Routing header.
297 ********************************/
298
Eric Dumazetf6bc7d92010-06-14 04:39:27 +0000299/* called with rcu_read_lock() */
Herbert Xue5bbef22007-10-15 12:50:28 -0700300static int ipv6_rthdr_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct inet6_skb_parm *opt = IP6CB(skb);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700303 struct in6_addr *addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct in6_addr daddr;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700305 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int n, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct ipv6_rt_hdr *hdr;
308 struct rt0_hdr *rthdr;
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700309 struct net *net = dev_net(skb->dev);
310 int accept_source_route = net->ipv6.devconf_all->accept_source_route;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700311
Eric Dumazetf6bc7d92010-06-14 04:39:27 +0000312 idev = __in6_dev_get(skb->dev);
313 if (idev && accept_source_route > idev->cnf.accept_source_route)
314 accept_source_route = idev->cnf.accept_source_route;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700315
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700316 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
317 !pskb_may_pull(skb, (skb_transport_offset(skb) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700318 ((skb_transport_header(skb)[1] + 1) << 3)))) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000319 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900320 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 kfree_skb(skb);
322 return -1;
323 }
324
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700325 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700327 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 skb->pkt_type != PACKET_HOST) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000329 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900330 IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 kfree_skb(skb);
332 return -1;
333 }
334
335looped_back:
336 if (hdr->segments_left == 0) {
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700337 switch (hdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000338#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700339 case IPV6_SRCRT_TYPE_2:
340 /* Silently discard type 2 header unless it was
341 * processed by own
342 */
343 if (!addr) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000344 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900345 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700346 kfree_skb(skb);
347 return -1;
348 }
349 break;
350#endif
351 default:
352 break;
353 }
354
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300355 opt->lastopt = opt->srcrt = skb_network_header_len(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700356 skb->transport_header += (hdr->hdrlen + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 opt->dst0 = opt->dst1;
358 opt->dst1 = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700359 opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 1;
361 }
362
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700363 switch (hdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000364#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700365 case IPV6_SRCRT_TYPE_2:
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700366 if (accept_source_route < 0)
367 goto unknown_rh;
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700368 /* Silently discard invalid RTH type 2 */
369 if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000370 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900371 IPSTATS_MIB_INHDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700372 kfree_skb(skb);
373 return -1;
374 }
375 break;
376#endif
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700377 default:
378 goto unknown_rh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /*
382 * This is the routing header forwarding algorithm from
383 * RFC 2460, page 16.
384 */
385
386 n = hdr->hdrlen >> 1;
387
388 if (hdr->segments_left > n) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000389 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900390 IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700391 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
392 ((&hdr->segments_left) -
393 skb_network_header(skb)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -1;
395 }
396
397 /* We are about to mangle packet header. Be careful!
398 Do not damage packets queued somewhere.
399 */
400 if (skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* the copy is a forwarded packet */
Herbert Xu65c88462007-10-15 01:29:10 -0700402 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
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_OUTDISCARDS);
405 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -1;
407 }
Herbert Xu65c88462007-10-15 01:29:10 -0700408 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
Patrick McHardy84fa7932006-08-29 16:44:56 -0700411 if (skb->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 skb->ip_summed = CHECKSUM_NONE;
413
414 i = n - --hdr->segments_left;
415
416 rthdr = (struct rt0_hdr *) hdr;
417 addr = rthdr->addr;
418 addr += i - 1;
419
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700420 switch (hdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000421#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700422 case IPV6_SRCRT_TYPE_2:
423 if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700424 (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700425 IPPROTO_ROUTING) < 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000426 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900427 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700428 kfree_skb(skb);
429 return -1;
430 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000431 if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
432 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900433 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700434 kfree_skb(skb);
435 return -1;
436 }
437 break;
438#endif
439 default:
440 break;
441 }
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (ipv6_addr_is_multicast(addr)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000444 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900445 IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 kfree_skb(skb);
447 return -1;
448 }
449
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000450 daddr = *addr;
451 *addr = ipv6_hdr(skb)->daddr;
452 ipv6_hdr(skb)->daddr = daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Eric Dumazetadf30902009-06-02 05:19:30 +0000454 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 ip6_route_input(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000456 if (skb_dst(skb)->error) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700457 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 dst_input(skb);
459 return -1;
460 }
461
Eric Dumazetadf30902009-06-02 05:19:30 +0000462 if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700463 if (ipv6_hdr(skb)->hop_limit <= 1) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000464 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900465 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000467 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 kfree_skb(skb);
469 return -1;
470 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700471 ipv6_hdr(skb)->hop_limit--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 goto looped_back;
473 }
474
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700475 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 dst_input(skb);
477 return -1;
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700478
479unknown_rh:
Eric Dumazetadf30902009-06-02 05:19:30 +0000480 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700481 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
482 (&hdr->type) - skb_network_header(skb));
483 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000486static const struct inet6_protocol rthdr_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 .handler = ipv6_rthdr_rcv,
Vlad Yasevich2207afc2012-11-15 08:49:19 +0000488 .flags = INET6_PROTO_NOPOLICY,
Vlad Yasevich8ca896c2012-11-15 08:49:13 +0000489};
490
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000491static const struct inet6_protocol destopt_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800492 .handler = ipv6_destopt_rcv,
Vlad Yasevich2207afc2012-11-15 08:49:19 +0000493 .flags = INET6_PROTO_NOPOLICY,
Vlad Yasevich8ca896c2012-11-15 08:49:13 +0000494};
495
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000496static const struct inet6_protocol nodata_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800497 .handler = dst_discard,
498 .flags = INET6_PROTO_NOPOLICY,
499};
500
501int __init ipv6_exthdrs_init(void)
502{
503 int ret;
504
Vlad Yasevich33362882012-11-15 08:49:15 +0000505 ret = ipv6_exthdrs_offload_init();
Daniel Lezcano248b2382007-12-11 02:23:54 -0800506 if (ret)
507 goto out;
508
Vlad Yasevich33362882012-11-15 08:49:15 +0000509 ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
510 if (ret)
511 goto out_offload;
512
Daniel Lezcano248b2382007-12-11 02:23:54 -0800513 ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
514 if (ret)
515 goto out_rthdr;
516
517 ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
518 if (ret)
519 goto out_destopt;
520
521out:
522 return ret;
Daniel Lezcano248b2382007-12-11 02:23:54 -0800523out_destopt:
524 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
Vlad Yasevich33362882012-11-15 08:49:15 +0000525out_rthdr:
526 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
527out_offload:
528 ipv6_exthdrs_offload_exit();
Daniel Lezcano248b2382007-12-11 02:23:54 -0800529 goto out;
530};
531
532void ipv6_exthdrs_exit(void)
533{
Vlad Yasevich2207afc2012-11-15 08:49:19 +0000534 ipv6_exthdrs_offload_exit();
Daniel Lezcano248b2382007-12-11 02:23:54 -0800535 inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
536 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
537 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/**********************************
541 Hop-by-hop options.
542 **********************************/
543
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700544/*
Eric Dumazetadf30902009-06-02 05:19:30 +0000545 * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700546 */
547static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
548{
Eric Dumazetadf30902009-06-02 05:19:30 +0000549 return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700550}
551
David S. Miller2570a4f2010-01-13 17:27:37 -0800552static inline struct net *ipv6_skb_net(struct sk_buff *skb)
553{
554 return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/* Router Alert as of RFC 2711 */
558
Eric Dumazeta50feda2012-05-18 18:57:34 +0000559static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700561 const unsigned char *nh = skb_network_header(skb);
Masahide NAKAMURAa80ff032006-08-23 19:19:50 -0700562
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700563 if (nh[optoff + 1] == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 IP6CB(skb)->ra = optoff;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000565 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
Patrick McHardy64ce2072005-08-09 20:50:53 -0700567 LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_ra: wrong RA length %d\n",
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700568 nh[optoff + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000570 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573/* Jumbo payload */
574
Eric Dumazeta50feda2012-05-18 18:57:34 +0000575static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700577 const unsigned char *nh = skb_network_header(skb);
David S. Miller2570a4f2010-01-13 17:27:37 -0800578 struct net *net = ipv6_skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 u32 pkt_len;
580
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700581 if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
Patrick McHardy64ce2072005-08-09 20:50:53 -0700582 LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700583 nh[optoff+1]);
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700584 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900585 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto drop;
587 }
588
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700589 pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (pkt_len <= IPV6_MAXPLEN) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700591 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
592 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000594 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700596 if (ipv6_hdr(skb)->payload_len) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700597 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
598 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000600 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602
603 if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700604 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
605 IPSTATS_MIB_INTRUNCATEDPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 goto drop;
607 }
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700608
609 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
610 goto drop;
611
Eric Dumazeta50feda2012-05-18 18:57:34 +0000612 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614drop:
615 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000616 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Eric Dumazeta50feda2012-05-18 18:57:34 +0000619static const struct tlvtype_proc tlvprochopopt_lst[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 {
621 .type = IPV6_TLV_ROUTERALERT,
622 .func = ipv6_hop_ra,
623 },
624 {
625 .type = IPV6_TLV_JUMBO,
626 .func = ipv6_hop_jumbo,
627 },
628 { -1, }
629};
630
Herbert Xue5bbef22007-10-15 12:50:28 -0700631int ipv6_parse_hopopts(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Patrick McHardy951dbc82006-01-06 23:02:34 -0800633 struct inet6_skb_parm *opt = IP6CB(skb);
634
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700635 /*
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700636 * skb_network_header(skb) is equal to skb->data, and
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300637 * skb_network_header_len(skb) is always equal to
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700638 * sizeof(struct ipv6hdr) by definition of
639 * hop-by-hop options.
640 */
641 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700642 !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
643 ((skb_transport_header(skb)[1] + 1) << 3)))) {
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700644 kfree_skb(skb);
645 return -1;
646 }
647
Patrick McHardy951dbc82006-01-06 23:02:34 -0800648 opt->hop = sizeof(struct ipv6hdr);
Herbert Xue5bbef22007-10-15 12:50:28 -0700649 if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700650 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
Masahide NAKAMURAdc435e62006-08-31 15:18:49 -0700651 opt = IP6CB(skb);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800652 opt->nhoff = sizeof(struct ipv6hdr);
YOSHIFUJI Hideakib8097392006-04-18 14:48:45 -0700653 return 1;
Patrick McHardy951dbc82006-01-06 23:02:34 -0800654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -1;
656}
657
658/*
659 * Creating outbound headers.
660 *
661 * "build" functions work when skb is filled from head to tail (datagram)
662 * "push" functions work when headers are added from tail to head (tcp)
663 *
664 * In both cases we assume, that caller reserved enough room
665 * for headers.
666 */
667
668static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
669 struct ipv6_rt_hdr *opt,
670 struct in6_addr **addr_p)
671{
672 struct rt0_hdr *phdr, *ihdr;
673 int hops;
674
675 ihdr = (struct rt0_hdr *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
678 memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
679
680 hops = ihdr->rt_hdr.hdrlen >> 1;
681
682 if (hops > 1)
683 memcpy(phdr->addr, ihdr->addr + 1,
684 (hops - 1) * sizeof(struct in6_addr));
685
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000686 phdr->addr[hops - 1] = **addr_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 *addr_p = ihdr->addr;
688
689 phdr->rt_hdr.nexthdr = *proto;
690 *proto = NEXTHDR_ROUTING;
691}
692
693static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
694{
695 struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt));
696
697 memcpy(h, opt, ipv6_optlen(opt));
698 h->nexthdr = *proto;
699 *proto = type;
700}
701
702void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
703 u8 *proto,
704 struct in6_addr **daddr)
705{
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900706 if (opt->srcrt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 ipv6_push_rthdr(skb, proto, opt->srcrt, daddr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900708 /*
709 * IPV6_RTHDRDSTOPTS is ignored
710 * unless IPV6_RTHDR is set (RFC3542).
711 */
712 if (opt->dst0opt)
713 ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (opt->hopopt)
716 ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
717}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900718EXPORT_SYMBOL(ipv6_push_nfrag_opts);
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
721{
722 if (opt->dst1opt)
723 ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
724}
725
726struct ipv6_txoptions *
727ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
728{
729 struct ipv6_txoptions *opt2;
730
731 opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
732 if (opt2) {
Eldad Zackac3c8172012-04-01 07:49:04 +0000733 long dif = (char *)opt2 - (char *)opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 memcpy(opt2, opt, opt->tot_len);
735 if (opt2->hopopt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000736 *((char **)&opt2->hopopt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (opt2->dst0opt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000738 *((char **)&opt2->dst0opt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (opt2->dst1opt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000740 *((char **)&opt2->dst1opt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (opt2->srcrt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000742 *((char **)&opt2->srcrt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 return opt2;
745}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800746EXPORT_SYMBOL_GPL(ipv6_dup_options);
747
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900748static int ipv6_renew_option(void *ohdr,
749 struct ipv6_opt_hdr __user *newopt, int newoptlen,
750 int inherit,
751 struct ipv6_opt_hdr **hdr,
752 char **p)
753{
754 if (inherit) {
755 if (ohdr) {
756 memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
757 *hdr = (struct ipv6_opt_hdr *)*p;
Joe Perchese3192692012-06-03 17:41:40 +0000758 *p += CMSG_ALIGN(ipv6_optlen(*hdr));
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900759 }
760 } else {
761 if (newopt) {
762 if (copy_from_user(*p, newopt, newoptlen))
763 return -EFAULT;
764 *hdr = (struct ipv6_opt_hdr *)*p;
Joe Perchese3192692012-06-03 17:41:40 +0000765 if (ipv6_optlen(*hdr) > newoptlen)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900766 return -EINVAL;
767 *p += CMSG_ALIGN(newoptlen);
768 }
769 }
770 return 0;
771}
772
773struct ipv6_txoptions *
774ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
775 int newtype,
776 struct ipv6_opt_hdr __user *newopt, int newoptlen)
777{
778 int tot_len = 0;
779 char *p;
780 struct ipv6_txoptions *opt2;
781 int err;
782
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700783 if (opt) {
784 if (newtype != IPV6_HOPOPTS && opt->hopopt)
785 tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
786 if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
787 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
788 if (newtype != IPV6_RTHDR && opt->srcrt)
789 tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
790 if (newtype != IPV6_DSTOPTS && opt->dst1opt)
791 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
792 }
793
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900794 if (newopt && newoptlen)
795 tot_len += CMSG_ALIGN(newoptlen);
796
797 if (!tot_len)
798 return NULL;
799
YOSHIFUJI Hideaki8b8aa4b2005-11-20 12:18:17 +0900800 tot_len += sizeof(*opt2);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900801 opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
802 if (!opt2)
803 return ERR_PTR(-ENOBUFS);
804
805 memset(opt2, 0, tot_len);
806
807 opt2->tot_len = tot_len;
808 p = (char *)(opt2 + 1);
809
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700810 err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900811 newtype != IPV6_HOPOPTS,
812 &opt2->hopopt, &p);
813 if (err)
814 goto out;
815
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700816 err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900817 newtype != IPV6_RTHDRDSTOPTS,
818 &opt2->dst0opt, &p);
819 if (err)
820 goto out;
821
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700822 err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900823 newtype != IPV6_RTHDR,
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700824 (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900825 if (err)
826 goto out;
827
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -0700828 err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900829 newtype != IPV6_DSTOPTS,
830 &opt2->dst1opt, &p);
831 if (err)
832 goto out;
833
834 opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
835 (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
836 (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
837 opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
838
839 return opt2;
840out:
YOSHIFUJI Hideaki8b8aa4b2005-11-20 12:18:17 +0900841 sock_kfree_s(sk, opt2, opt2->tot_len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900842 return ERR_PTR(err);
843}
844
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900845struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
846 struct ipv6_txoptions *opt)
847{
848 /*
849 * ignore the dest before srcrt unless srcrt is being included.
850 * --yoshfuji
851 */
852 if (opt && opt->dst0opt && !opt->srcrt) {
853 if (opt_space != opt) {
854 memcpy(opt_space, opt, sizeof(*opt_space));
855 opt = opt_space;
856 }
857 opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
858 opt->dst0opt = NULL;
859 }
860
861 return opt;
862}
Chris Elstona495f832012-04-29 21:48:53 +0000863EXPORT_SYMBOL_GPL(ipv6_fixup_options);
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900864
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000865/**
866 * fl6_update_dst - update flowi destination address with info given
867 * by srcrt option, if any.
868 *
David S. Miller4c9483b2011-03-12 16:22:43 -0500869 * @fl6: flowi6 for which daddr is to be updated
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000870 * @opt: struct ipv6_txoptions in which to look for srcrt opt
David S. Miller4c9483b2011-03-12 16:22:43 -0500871 * @orig: copy of original daddr address if modified
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000872 *
873 * Returns NULL if no txoptions or no srcrt, otherwise returns orig
David S. Miller4c9483b2011-03-12 16:22:43 -0500874 * and initial value of fl6->daddr set in orig
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000875 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500876struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000877 const struct ipv6_txoptions *opt,
878 struct in6_addr *orig)
879{
880 if (!opt || !opt->srcrt)
881 return NULL;
882
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000883 *orig = fl6->daddr;
884 fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000885 return orig;
886}
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000887EXPORT_SYMBOL_GPL(fl6_update_dst);