etherdevice: fix comments
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv6 / exthdrs.c
CommitLineData
1da177e4
LT
1/*
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 *
1da177e4
LT
10 * 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:
1ab1457c 17 * yoshfuji : ensure not to overrun while parsing
1da177e4
LT
18 * 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>
1da177e4
LT
28#include <linux/net.h>
29#include <linux/netdevice.h>
30#include <linux/in6.h>
31#include <linux/icmpv6.h>
5a0e3ad6 32#include <linux/slab.h>
bc3b2d7f 33#include <linux/export.h>
1da177e4 34
352e512c 35#include <net/dst.h>
1da177e4
LT
36#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>
59fbb3a6 46#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
65d4ed92
MN
47#include <net/xfrm.h>
48#endif
1da177e4
LT
49
50#include <asm/uaccess.h>
51
c61a4043
MN
52int ipv6_find_tlv(struct sk_buff *skb, int offset, int type)
53{
d56f90a7 54 const unsigned char *nh = skb_network_header(skb);
27a884dc 55 int packet_len = skb->tail - skb->network_header;
c61a4043
MN
56 struct ipv6_opt_hdr *hdr;
57 int len;
58
59 if (offset + 2 > packet_len)
60 goto bad;
d56f90a7 61 hdr = (struct ipv6_opt_hdr *)(nh + offset);
c61a4043
MN
62 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) {
d56f90a7 71 int opttype = nh[offset];
c61a4043
MN
72 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:
d56f90a7 82 optlen = nh[offset + 1] + 2;
c61a4043
MN
83 if (optlen > len)
84 goto bad;
85 break;
86 }
87 offset += optlen;
88 len -= optlen;
89 }
90 /* not_found */
c61a4043
MN
91 bad:
92 return -1;
93}
59fbb3a6 94EXPORT_SYMBOL_GPL(ipv6_find_tlv);
c61a4043 95
1da177e4
LT
96/*
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;
e5bbef20 106 int (*func)(struct sk_buff *skb, int offset);
1da177e4
LT
107};
108
109/*********************
110 Generic functions
111 *********************/
112
113/* An unknown option is detected, decide what to do */
114
e5bbef20 115static int ip6_tlvopt_unknown(struct sk_buff *skb, int optoff)
1da177e4 116{
d56f90a7 117 switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) {
1da177e4
LT
118 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 */
0660e03f 128 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr))
1da177e4
LT
129 break;
130 case 2: /* send ICMP PARM PROB regardless and drop packet */
131 icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff);
132 return 0;
3ff50b79 133 }
1da177e4
LT
134
135 kfree_skb(skb);
136 return 0;
137}
138
139/* Parse tlv encoded option header (hop-by-hop or destination) */
140
e5bbef20 141static int ip6_parse_tlv(struct tlvtype_proc *procs, struct sk_buff *skb)
1da177e4
LT
142{
143 struct tlvtype_proc *curr;
d56f90a7 144 const unsigned char *nh = skb_network_header(skb);
cfe1fc77 145 int off = skb_network_header_len(skb);
9c70220b 146 int len = (skb_transport_header(skb)[1] + 1) << 3;
1da177e4 147
ea2ae17d 148 if (skb_transport_offset(skb) + len > skb_headlen(skb))
1da177e4
LT
149 goto bad;
150
151 off += 2;
152 len -= 2;
153
154 while (len > 0) {
d56f90a7 155 int optlen = nh[off + 1] + 2;
c1412fce 156 int i;
1da177e4 157
d56f90a7 158 switch (nh[off]) {
1da177e4
LT
159 case IPV6_TLV_PAD0:
160 optlen = 1;
161 break;
162
163 case IPV6_TLV_PADN:
c1412fce
EZ
164 /* 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 }
1da177e4
LT
179 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++) {
d56f90a7 185 if (curr->type == nh[off]) {
1ab1457c
YH
186 /* type specific length/alignment
187 checks will be performed in the
1da177e4 188 func(). */
e5bbef20 189 if (curr->func(skb, off) == 0)
1da177e4
LT
190 return 0;
191 break;
192 }
193 }
194 if (curr->type < 0) {
e5bbef20 195 if (ip6_tlvopt_unknown(skb, off) == 0)
1da177e4
LT
196 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
59fbb3a6 214#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
e5bbef20 215static int ipv6_dest_hao(struct sk_buff *skb, int optoff)
a831f5bb 216{
a831f5bb
MN
217 struct ipv6_destopt_hao *hao;
218 struct inet6_skb_parm *opt = IP6CB(skb);
0660e03f 219 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
a831f5bb
MN
220 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
d56f90a7 230 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
a831f5bb
MN
231
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(
5b095d98 240 KERN_DEBUG "hao is not an unicast addr: %pI6\n", &hao->addr);
a831f5bb
MN
241 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)) {
65c88466 250 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
a831f5bb
MN
251 goto discard;
252
a831f5bb 253 /* update all variable using below by copied skbuff */
65c88466 254 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
d56f90a7 255 optoff);
65c88466 256 ipv6h = ipv6_hdr(skb);
a831f5bb
MN
257 }
258
259 if (skb->ip_summed == CHECKSUM_COMPLETE)
260 skb->ip_summed = CHECKSUM_NONE;
261
4e3fd7a0
AD
262 tmp_addr = ipv6h->saddr;
263 ipv6h->saddr = hao->addr;
264 hao->addr = tmp_addr;
a831f5bb 265
b7aa0bf7 266 if (skb->tstamp.tv64 == 0)
a831f5bb
MN
267 __net_timestamp(skb);
268
269 return 1;
270
271 discard:
272 kfree_skb(skb);
273 return 0;
274}
275#endif
276
1da177e4 277static struct tlvtype_proc tlvprocdestopt_lst[] = {
59fbb3a6 278#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
a831f5bb
MN
279 {
280 .type = IPV6_TLV_HAO,
281 .func = ipv6_dest_hao,
282 },
283#endif
1da177e4
LT
284 {-1, NULL}
285};
286
e5bbef20 287static int ipv6_destopt_rcv(struct sk_buff *skb)
1da177e4 288{
1da177e4 289 struct inet6_skb_parm *opt = IP6CB(skb);
59fbb3a6 290#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
a831f5bb
MN
291 __u16 dstbuf;
292#endif
897dc80b 293 struct dst_entry *dst = skb_dst(skb);
1da177e4 294
ea2ae17d
ACM
295 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
296 !pskb_may_pull(skb, (skb_transport_offset(skb) +
9c70220b 297 ((skb_transport_header(skb)[1] + 1) << 3)))) {
897dc80b 298 IP6_INC_STATS_BH(dev_net(dst->dev), ip6_dst_idev(dst),
a11d206d 299 IPSTATS_MIB_INHDRERRORS);
1da177e4
LT
300 kfree_skb(skb);
301 return -1;
302 }
303
cfe1fc77 304 opt->lastopt = opt->dst1 = skb_network_header_len(skb);
59fbb3a6 305#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
a831f5bb
MN
306 dstbuf = opt->dst1;
307#endif
1da177e4 308
e5bbef20 309 if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
b0e380b1 310 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
dc435e6d 311 opt = IP6CB(skb);
59fbb3a6 312#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
a831f5bb
MN
313 opt->nhoff = dstbuf;
314#else
951dbc8a 315 opt->nhoff = opt->dst1;
a831f5bb 316#endif
1da177e4
LT
317 return 1;
318 }
319
483a47d2
DL
320 IP6_INC_STATS_BH(dev_net(dst->dev),
321 ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
1da177e4
LT
322 return -1;
323}
324
1da177e4
LT
325/********************************
326 Routing header.
327 ********************************/
328
f6bc7d9e 329/* called with rcu_read_lock() */
e5bbef20 330static int ipv6_rthdr_rcv(struct sk_buff *skb)
1da177e4 331{
1da177e4 332 struct inet6_skb_parm *opt = IP6CB(skb);
65d4ed92 333 struct in6_addr *addr = NULL;
1da177e4 334 struct in6_addr daddr;
0bcbc926 335 struct inet6_dev *idev;
1da177e4 336 int n, i;
1da177e4
LT
337 struct ipv6_rt_hdr *hdr;
338 struct rt0_hdr *rthdr;
483a47d2
DL
339 struct net *net = dev_net(skb->dev);
340 int accept_source_route = net->ipv6.devconf_all->accept_source_route;
0bcbc926 341
f6bc7d9e
ED
342 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;
0bcbc926 345
ea2ae17d
ACM
346 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
347 !pskb_may_pull(skb, (skb_transport_offset(skb) +
9c70220b 348 ((skb_transport_header(skb)[1] + 1) << 3)))) {
adf30907 349 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
a11d206d 350 IPSTATS_MIB_INHDRERRORS);
1da177e4
LT
351 kfree_skb(skb);
352 return -1;
353 }
354
9c70220b 355 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
1da177e4 356
0660e03f 357 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
1da177e4 358 skb->pkt_type != PACKET_HOST) {
adf30907 359 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
a11d206d 360 IPSTATS_MIB_INADDRERRORS);
1da177e4
LT
361 kfree_skb(skb);
362 return -1;
363 }
364
365looped_back:
366 if (hdr->segments_left == 0) {
65d4ed92 367 switch (hdr->type) {
59fbb3a6 368#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
65d4ed92
MN
369 case IPV6_SRCRT_TYPE_2:
370 /* Silently discard type 2 header unless it was
371 * processed by own
372 */
373 if (!addr) {
adf30907 374 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
a11d206d 375 IPSTATS_MIB_INADDRERRORS);
65d4ed92
MN
376 kfree_skb(skb);
377 return -1;
378 }
379 break;
380#endif
381 default:
382 break;
383 }
384
cfe1fc77 385 opt->lastopt = opt->srcrt = skb_network_header_len(skb);
b0e380b1 386 skb->transport_header += (hdr->hdrlen + 1) << 3;
1da177e4
LT
387 opt->dst0 = opt->dst1;
388 opt->dst1 = 0;
d56f90a7 389 opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
1da177e4
LT
390 return 1;
391 }
392
65d4ed92 393 switch (hdr->type) {
59fbb3a6 394#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
65d4ed92 395 case IPV6_SRCRT_TYPE_2:
c382bb9d
YH
396 if (accept_source_route < 0)
397 goto unknown_rh;
65d4ed92
MN
398 /* Silently discard invalid RTH type 2 */
399 if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
adf30907 400 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
a11d206d 401 IPSTATS_MIB_INHDRERRORS);
65d4ed92
MN
402 kfree_skb(skb);
403 return -1;
404 }
405 break;
406#endif
c382bb9d
YH
407 default:
408 goto unknown_rh;
1da177e4 409 }
1da177e4
LT
410
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) {
adf30907 419 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
a11d206d 420 IPSTATS_MIB_INHDRERRORS);
d56f90a7
ACM
421 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
422 ((&hdr->segments_left) -
423 skb_network_header(skb)));
1da177e4
LT
424 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)) {
1da177e4 431 /* the copy is a forwarded packet */
65c88466 432 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
adf30907 433 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
a11d206d
YH
434 IPSTATS_MIB_OUTDISCARDS);
435 kfree_skb(skb);
1da177e4
LT
436 return -1;
437 }
65c88466 438 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
1da177e4
LT
439 }
440
84fa7933 441 if (skb->ip_summed == CHECKSUM_COMPLETE)
1da177e4
LT
442 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
65d4ed92 450 switch (hdr->type) {
59fbb3a6 451#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
65d4ed92
MN
452 case IPV6_SRCRT_TYPE_2:
453 if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
0660e03f 454 (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
65d4ed92 455 IPPROTO_ROUTING) < 0) {
adf30907 456 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
a11d206d 457 IPSTATS_MIB_INADDRERRORS);
65d4ed92
MN
458 kfree_skb(skb);
459 return -1;
460 }
adf30907
ED
461 if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
462 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
a11d206d 463 IPSTATS_MIB_INADDRERRORS);
65d4ed92
MN
464 kfree_skb(skb);
465 return -1;
466 }
467 break;
468#endif
469 default:
470 break;
471 }
472
1da177e4 473 if (ipv6_addr_is_multicast(addr)) {
adf30907 474 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
a11d206d 475 IPSTATS_MIB_INADDRERRORS);
1da177e4
LT
476 kfree_skb(skb);
477 return -1;
478 }
479
4e3fd7a0
AD
480 daddr = *addr;
481 *addr = ipv6_hdr(skb)->daddr;
482 ipv6_hdr(skb)->daddr = daddr;
1da177e4 483
adf30907 484 skb_dst_drop(skb);
1da177e4 485 ip6_route_input(skb);
adf30907 486 if (skb_dst(skb)->error) {
d56f90a7 487 skb_push(skb, skb->data - skb_network_header(skb));
1da177e4
LT
488 dst_input(skb);
489 return -1;
490 }
491
adf30907 492 if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
0660e03f 493 if (ipv6_hdr(skb)->hop_limit <= 1) {
adf30907 494 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
a11d206d 495 IPSTATS_MIB_INHDRERRORS);
1da177e4 496 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
3ffe533c 497 0);
1da177e4
LT
498 kfree_skb(skb);
499 return -1;
500 }
0660e03f 501 ipv6_hdr(skb)->hop_limit--;
1da177e4
LT
502 goto looped_back;
503 }
504
d56f90a7 505 skb_push(skb, skb->data - skb_network_header(skb));
1da177e4
LT
506 dst_input(skb);
507 return -1;
c382bb9d
YH
508
509unknown_rh:
adf30907 510 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
c382bb9d
YH
511 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
512 (&hdr->type) - skb_network_header(skb));
513 return -1;
1da177e4
LT
514}
515
41135cc8 516static const struct inet6_protocol rthdr_protocol = {
1da177e4 517 .handler = ipv6_rthdr_rcv,
adcfc7d0 518 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
1da177e4
LT
519};
520
41135cc8 521static const struct inet6_protocol destopt_protocol = {
248b238d
DL
522 .handler = ipv6_destopt_rcv,
523 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
524};
525
41135cc8 526static const struct inet6_protocol nodata_protocol = {
248b238d
DL
527 .handler = dst_discard,
528 .flags = INET6_PROTO_NOPOLICY,
529};
530
531int __init ipv6_exthdrs_init(void)
1da177e4 532{
248b238d
DL
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;
1da177e4
LT
554};
555
248b238d
DL
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
1da177e4
LT
563/**********************************
564 Hop-by-hop options.
565 **********************************/
566
e76b2b25 567/*
adf30907 568 * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
e76b2b25
YH
569 */
570static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
571{
adf30907 572 return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
e76b2b25
YH
573}
574
2570a4f5
DM
575static 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
1da177e4
LT
580/* Router Alert as of RFC 2711 */
581
e5bbef20 582static int ipv6_hop_ra(struct sk_buff *skb, int optoff)
1da177e4 583{
d56f90a7 584 const unsigned char *nh = skb_network_header(skb);
a80ff03e 585
d56f90a7 586 if (nh[optoff + 1] == 2) {
1da177e4
LT
587 IP6CB(skb)->ra = optoff;
588 return 1;
589 }
64ce2073 590 LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_ra: wrong RA length %d\n",
d56f90a7 591 nh[optoff + 1]);
1da177e4
LT
592 kfree_skb(skb);
593 return 0;
594}
595
596/* Jumbo payload */
597
e5bbef20 598static int ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
1da177e4 599{
d56f90a7 600 const unsigned char *nh = skb_network_header(skb);
2570a4f5 601 struct net *net = ipv6_skb_net(skb);
1da177e4
LT
602 u32 pkt_len;
603
d56f90a7 604 if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
64ce2073 605 LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
d56f90a7 606 nh[optoff+1]);
483a47d2 607 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
a11d206d 608 IPSTATS_MIB_INHDRERRORS);
1da177e4
LT
609 goto drop;
610 }
611
d56f90a7 612 pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
1da177e4 613 if (pkt_len <= IPV6_MAXPLEN) {
483a47d2
DL
614 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
615 IPSTATS_MIB_INHDRERRORS);
1da177e4
LT
616 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
617 return 0;
618 }
0660e03f 619 if (ipv6_hdr(skb)->payload_len) {
483a47d2
DL
620 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
621 IPSTATS_MIB_INHDRERRORS);
1da177e4
LT
622 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
623 return 0;
624 }
625
626 if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
483a47d2
DL
627 IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
628 IPSTATS_MIB_INTRUNCATEDPKTS);
1da177e4
LT
629 goto drop;
630 }
42ca89c1
SH
631
632 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
633 goto drop;
634
1da177e4
LT
635 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
e5bbef20 654int ipv6_parse_hopopts(struct sk_buff *skb)
1da177e4 655{
951dbc8a
PM
656 struct inet6_skb_parm *opt = IP6CB(skb);
657
ec670095 658 /*
d56f90a7 659 * skb_network_header(skb) is equal to skb->data, and
cfe1fc77 660 * skb_network_header_len(skb) is always equal to
ec670095
YH
661 * sizeof(struct ipv6hdr) by definition of
662 * hop-by-hop options.
663 */
664 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
9c70220b
ACM
665 !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
666 ((skb_transport_header(skb)[1] + 1) << 3)))) {
ec670095
YH
667 kfree_skb(skb);
668 return -1;
669 }
670
951dbc8a 671 opt->hop = sizeof(struct ipv6hdr);
e5bbef20 672 if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
b0e380b1 673 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
dc435e6d 674 opt = IP6CB(skb);
951dbc8a 675 opt->nhoff = sizeof(struct ipv6hdr);
b809739a 676 return 1;
951dbc8a 677 }
1da177e4
LT
678 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;
1ab1457c 699
1da177e4
LT
700 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
4e3fd7a0 709 phdr->addr[hops - 1] = **addr_p;
1da177e4
LT
710 *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{
333fad53 729 if (opt->srcrt) {
1da177e4 730 ipv6_push_rthdr(skb, proto, opt->srcrt, daddr);
333fad53
YH
731 /*
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 }
1da177e4
LT
738 if (opt->hopopt)
739 ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
740}
7159039a
YH
741EXPORT_SYMBOL(ipv6_push_nfrag_opts);
742
1da177e4
LT
743void 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) {
ac3c8172 756 long dif = (char *)opt2 - (char *)opt;
1da177e4
LT
757 memcpy(opt2, opt, opt->tot_len);
758 if (opt2->hopopt)
ac3c8172 759 *((char **)&opt2->hopopt) += dif;
1da177e4 760 if (opt2->dst0opt)
ac3c8172 761 *((char **)&opt2->dst0opt) += dif;
1da177e4 762 if (opt2->dst1opt)
ac3c8172 763 *((char **)&opt2->dst1opt) += dif;
1da177e4 764 if (opt2->srcrt)
ac3c8172 765 *((char **)&opt2->srcrt) += dif;
1da177e4
LT
766 }
767 return opt2;
768}
3cf3dc6c
ACM
769EXPORT_SYMBOL_GPL(ipv6_dup_options);
770
333fad53
YH
771static 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
99c7bc01
YH
806 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
333fad53
YH
817 if (newopt && newoptlen)
818 tot_len += CMSG_ALIGN(newoptlen);
819
820 if (!tot_len)
821 return NULL;
822
8b8aa4b5 823 tot_len += sizeof(*opt2);
333fad53
YH
824 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
99c7bc01 833 err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
333fad53
YH
834 newtype != IPV6_HOPOPTS,
835 &opt2->hopopt, &p);
836 if (err)
837 goto out;
838
99c7bc01 839 err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
333fad53
YH
840 newtype != IPV6_RTHDRDSTOPTS,
841 &opt2->dst0opt, &p);
842 if (err)
843 goto out;
844
99c7bc01 845 err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
333fad53 846 newtype != IPV6_RTHDR,
99c7bc01 847 (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
333fad53
YH
848 if (err)
849 goto out;
850
99c7bc01 851 err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
333fad53
YH
852 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:
8b8aa4b5 864 sock_kfree_s(sk, opt2, opt2->tot_len);
333fad53
YH
865 return ERR_PTR(err);
866}
867
df9890c3
YH
868struct 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}
a495f836 886EXPORT_SYMBOL_GPL(ipv6_fixup_options);
df9890c3 887
20c59de2
AE
888/**
889 * fl6_update_dst - update flowi destination address with info given
890 * by srcrt option, if any.
891 *
4c9483b2 892 * @fl6: flowi6 for which daddr is to be updated
20c59de2 893 * @opt: struct ipv6_txoptions in which to look for srcrt opt
4c9483b2 894 * @orig: copy of original daddr address if modified
20c59de2
AE
895 *
896 * Returns NULL if no txoptions or no srcrt, otherwise returns orig
4c9483b2 897 * and initial value of fl6->daddr set in orig
20c59de2 898 */
4c9483b2 899struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
20c59de2
AE
900 const struct ipv6_txoptions *opt,
901 struct in6_addr *orig)
902{
903 if (!opt || !opt->srcrt)
904 return NULL;
905
4e3fd7a0
AD
906 *orig = fl6->daddr;
907 fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
20c59de2
AE
908 return orig;
909}
20c59de2 910EXPORT_SYMBOL_GPL(fl6_update_dst);