blob: 9f918a770f87795467c9f9ea279acb0de52364e7 [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>
Huw Davies2e532b72016-06-27 15:06:17 -040046#include <net/calipso.h>
Amerigo Wang07a93622012-10-29 16:23:10 +000047#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -070048#include <net/xfrm.h>
49#endif
David Lebrun1ababeb2016-11-08 14:57:39 +010050#include <linux/seg6.h>
51#include <net/seg6.h>
David Lebrun9baee832016-11-08 14:59:19 +010052#ifdef CONFIG_IPV6_SEG6_HMAC
53#include <net/seg6_hmac.h>
54#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Fabian Frederickce256982014-10-27 19:12:58 +010056#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*
59 * Parsing tlv encoded headers.
60 *
Eric Dumazeta50feda2012-05-18 18:57:34 +000061 * Parsing function "func" returns true, if parsing succeed
62 * and false, if it failed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * It MUST NOT touch skb->h.
64 */
65
66struct tlvtype_proc {
67 int type;
Eric Dumazeta50feda2012-05-18 18:57:34 +000068 bool (*func)(struct sk_buff *skb, int offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71/*********************
72 Generic functions
73 *********************/
74
75/* An unknown option is detected, decide what to do */
76
Eric Dumazeta50feda2012-05-18 18:57:34 +000077static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070079 switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 case 0: /* ignore */
Eric Dumazeta50feda2012-05-18 18:57:34 +000081 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 case 1: /* drop packet */
84 break;
85
86 case 3: /* Send ICMP if not a multicast address and drop packet */
87 /* Actually, it is redundant check. icmp_send
88 will recheck in any case.
89 */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070090 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 break;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -050092 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 case 2: /* send ICMP PARM PROB regardless and drop packet */
94 icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff);
Eric Dumazeta50feda2012-05-18 18:57:34 +000095 return false;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +000099 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102/* Parse tlv encoded option header (hop-by-hop or destination) */
103
Eric Dumazeta50feda2012-05-18 18:57:34 +0000104static bool ip6_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Eric Dumazeta50feda2012-05-18 18:57:34 +0000106 const struct tlvtype_proc *curr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700107 const unsigned char *nh = skb_network_header(skb);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300108 int off = skb_network_header_len(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700109 int len = (skb_transport_header(skb)[1] + 1) << 3;
Eldad Zack9b905fe2012-05-20 01:59:33 +0000110 int padlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700112 if (skb_transport_offset(skb) + len > skb_headlen(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 goto bad;
114
115 off += 2;
116 len -= 2;
117
118 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700119 int optlen = nh[off + 1] + 2;
Eldad Zackc1412fc2012-04-12 17:36:17 -0400120 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700122 switch (nh[off]) {
Eldad Zack1de5a712012-05-17 06:00:25 +0000123 case IPV6_TLV_PAD1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 optlen = 1;
Eldad Zack9b905fe2012-05-20 01:59:33 +0000125 padlen++;
126 if (padlen > 7)
127 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 break;
129
130 case IPV6_TLV_PADN:
Eldad Zackc1412fc2012-04-12 17:36:17 -0400131 /* RFC 2460 states that the purpose of PadN is
132 * to align the containing header to multiples
133 * of 8. 7 is therefore the highest valid value.
134 * See also RFC 4942, Section 2.1.9.5.
135 */
Eldad Zack9b905fe2012-05-20 01:59:33 +0000136 padlen += optlen;
137 if (padlen > 7)
Eldad Zackc1412fc2012-04-12 17:36:17 -0400138 goto bad;
139 /* RFC 4942 recommends receiving hosts to
140 * actively check PadN payload to contain
141 * only zeroes.
142 */
143 for (i = 2; i < optlen; i++) {
144 if (nh[off + i] != 0)
145 goto bad;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 break;
148
149 default: /* Other TLV code so scan list */
150 if (optlen > len)
151 goto bad;
Ian Morris67ba4152014-08-24 21:53:10 +0100152 for (curr = procs; curr->type >= 0; curr++) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700153 if (curr->type == nh[off]) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900154 /* type specific length/alignment
155 checks will be performed in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 func(). */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000157 if (curr->func(skb, off) == false)
158 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 break;
160 }
161 }
162 if (curr->type < 0) {
Herbert Xue5bbef22007-10-15 12:50:28 -0700163 if (ip6_tlvopt_unknown(skb, off) == 0)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000164 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Eldad Zack9b905fe2012-05-20 01:59:33 +0000166 padlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168 }
169 off += optlen;
170 len -= optlen;
171 }
Eldad Zack9b905fe2012-05-20 01:59:33 +0000172
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 int ret;
191
192 if (opt->dsthao) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800193 net_dbg_ratelimited("hao duplicated\n");
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700194 goto discard;
195 }
196 opt->dsthao = opt->dst1;
197 opt->dst1 = 0;
198
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700199 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700200
201 if (hao->length != 16) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800202 net_dbg_ratelimited("hao invalid option length = %d\n",
203 hao->length);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700204 goto discard;
205 }
206
207 if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800208 net_dbg_ratelimited("hao is not an unicast addr: %pI6\n",
209 &hao->addr);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700210 goto discard;
211 }
212
213 ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
214 (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
215 if (unlikely(ret < 0))
216 goto discard;
217
218 if (skb_cloned(skb)) {
Herbert Xu65c88462007-10-15 01:29:10 -0700219 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700220 goto discard;
221
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700222 /* update all variable using below by copied skbuff */
Herbert Xu65c88462007-10-15 01:29:10 -0700223 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700224 optoff);
Herbert Xu65c88462007-10-15 01:29:10 -0700225 ipv6h = ipv6_hdr(skb);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700226 }
227
228 if (skb->ip_summed == CHECKSUM_COMPLETE)
229 skb->ip_summed = CHECKSUM_NONE;
230
Gustavo A. R. Silva74b65512017-10-26 23:10:35 -0500231 swap(ipv6h->saddr, hao->addr);
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700232
Thomas Gleixner2456e852016-12-25 11:38:40 +0100233 if (skb->tstamp == 0)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700234 __net_timestamp(skb);
235
Eric Dumazeta50feda2012-05-18 18:57:34 +0000236 return true;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700237
238 discard:
239 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000240 return false;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700241}
242#endif
243
Eric Dumazeta50feda2012-05-18 18:57:34 +0000244static const struct tlvtype_proc tlvprocdestopt_lst[] = {
Amerigo Wang07a93622012-10-29 16:23:10 +0000245#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700246 {
247 .type = IPV6_TLV_HAO,
248 .func = ipv6_dest_hao,
249 },
250#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 {-1, NULL}
252};
253
Herbert Xue5bbef22007-10-15 12:50:28 -0700254static int ipv6_destopt_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct inet6_skb_parm *opt = IP6CB(skb);
Amerigo Wang07a93622012-10-29 16:23:10 +0000257#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700258 __u16 dstbuf;
259#endif
Eric Dumazet897dc802011-07-28 04:00:35 +0000260 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700262 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
263 !pskb_may_pull(skb, (skb_transport_offset(skb) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700264 ((skb_transport_header(skb)[1] + 1) << 3)))) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700265 __IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
266 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 kfree_skb(skb);
268 return -1;
269 }
270
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300271 opt->lastopt = opt->dst1 = skb_network_header_len(skb);
Amerigo Wang07a93622012-10-29 16:23:10 +0000272#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700273 dstbuf = opt->dst1;
274#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Herbert Xue5bbef22007-10-15 12:50:28 -0700276 if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700277 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
Masahide NAKAMURAdc435e62006-08-31 15:18:49 -0700278 opt = IP6CB(skb);
Amerigo Wang07a93622012-10-29 16:23:10 +0000279#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700280 opt->nhoff = dstbuf;
281#else
Patrick McHardy951dbc82006-01-06 23:02:34 -0800282 opt->nhoff = opt->dst1;
Masahide NAKAMURAa831f5b2006-08-23 19:24:48 -0700283#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 1;
285 }
286
Eric Dumazet1d015502016-04-27 16:44:40 -0700287 __IP6_INC_STATS(dev_net(dst->dev),
288 ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -1;
290}
291
David Lebrun1ababeb2016-11-08 14:57:39 +0100292static void seg6_update_csum(struct sk_buff *skb)
293{
294 struct ipv6_sr_hdr *hdr;
295 struct in6_addr *addr;
296 __be32 from, to;
297
298 /* srh is at transport offset and seg_left is already decremented
299 * but daddr is not yet updated with next segment
300 */
301
302 hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
303 addr = hdr->segments + hdr->segments_left;
304
305 hdr->segments_left++;
306 from = *(__be32 *)hdr;
307
308 hdr->segments_left--;
309 to = *(__be32 *)hdr;
310
311 /* update skb csum with diff resulting from seg_left decrement */
312
313 update_csum_diff4(skb, from, to);
314
315 /* compute csum diff between current and next segment and update */
316
317 update_csum_diff16(skb, (__be32 *)(&ipv6_hdr(skb)->daddr),
318 (__be32 *)addr);
319}
320
321static int ipv6_srh_rcv(struct sk_buff *skb)
322{
323 struct inet6_skb_parm *opt = IP6CB(skb);
324 struct net *net = dev_net(skb->dev);
325 struct ipv6_sr_hdr *hdr;
326 struct inet6_dev *idev;
327 struct in6_addr *addr;
David Lebrun1ababeb2016-11-08 14:57:39 +0100328 int accept_seg6;
329
330 hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
331
332 idev = __in6_dev_get(skb->dev);
333
334 accept_seg6 = net->ipv6.devconf_all->seg6_enabled;
335 if (accept_seg6 > idev->cnf.seg6_enabled)
336 accept_seg6 = idev->cnf.seg6_enabled;
337
338 if (!accept_seg6) {
339 kfree_skb(skb);
340 return -1;
341 }
342
David Lebrun9baee832016-11-08 14:59:19 +0100343#ifdef CONFIG_IPV6_SEG6_HMAC
344 if (!seg6_hmac_validate_skb(skb)) {
345 kfree_skb(skb);
346 return -1;
347 }
348#endif
349
David Lebrun1ababeb2016-11-08 14:57:39 +0100350looped_back:
David Lebrun013e8162017-02-02 11:29:38 +0100351 if (hdr->segments_left == 0) {
David Lebrun1ababeb2016-11-08 14:57:39 +0100352 if (hdr->nexthdr == NEXTHDR_IPV6) {
353 int offset = (hdr->hdrlen + 1) << 3;
354
355 skb_postpull_rcsum(skb, skb_network_header(skb),
356 skb_network_header_len(skb));
357
358 if (!pskb_pull(skb, offset)) {
359 kfree_skb(skb);
360 return -1;
361 }
362 skb_postpull_rcsum(skb, skb_transport_header(skb),
363 offset);
364
365 skb_reset_network_header(skb);
366 skb_reset_transport_header(skb);
367 skb->encapsulation = 0;
368
369 __skb_tunnel_rx(skb, skb->dev, net);
370
371 netif_rx(skb);
372 return -1;
373 }
374
375 opt->srcrt = skb_network_header_len(skb);
376 opt->lastopt = opt->srcrt;
377 skb->transport_header += (hdr->hdrlen + 1) << 3;
378 opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
379
380 return 1;
381 }
382
383 if (hdr->segments_left >= (hdr->hdrlen >> 1)) {
384 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
385 IPSTATS_MIB_INHDRERRORS);
386 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
387 ((&hdr->segments_left) -
388 skb_network_header(skb)));
David Lebrun1ababeb2016-11-08 14:57:39 +0100389 return -1;
390 }
391
392 if (skb_cloned(skb)) {
393 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
394 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
395 IPSTATS_MIB_OUTDISCARDS);
396 kfree_skb(skb);
397 return -1;
398 }
399 }
400
401 hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
402
403 hdr->segments_left--;
404 addr = hdr->segments + hdr->segments_left;
405
406 skb_push(skb, sizeof(struct ipv6hdr));
407
408 if (skb->ip_summed == CHECKSUM_COMPLETE)
409 seg6_update_csum(skb);
410
411 ipv6_hdr(skb)->daddr = *addr;
412
David Lebrun1ababeb2016-11-08 14:57:39 +0100413 skb_dst_drop(skb);
414
415 ip6_route_input(skb);
416
417 if (skb_dst(skb)->error) {
418 dst_input(skb);
419 return -1;
420 }
421
422 if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) {
423 if (ipv6_hdr(skb)->hop_limit <= 1) {
424 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
425 IPSTATS_MIB_INHDRERRORS);
426 icmpv6_send(skb, ICMPV6_TIME_EXCEED,
427 ICMPV6_EXC_HOPLIMIT, 0);
428 kfree_skb(skb);
429 return -1;
430 }
431 ipv6_hdr(skb)->hop_limit--;
432
David Lebrun013e8162017-02-02 11:29:38 +0100433 skb_pull(skb, sizeof(struct ipv6hdr));
434 goto looped_back;
David Lebrun1ababeb2016-11-08 14:57:39 +0100435 }
436
437 dst_input(skb);
438
439 return -1;
440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/********************************
443 Routing header.
444 ********************************/
445
Eric Dumazetf6bc7d92010-06-14 04:39:27 +0000446/* called with rcu_read_lock() */
Herbert Xue5bbef22007-10-15 12:50:28 -0700447static int ipv6_rthdr_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct inet6_skb_parm *opt = IP6CB(skb);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700450 struct in6_addr *addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct in6_addr daddr;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700452 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int n, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct ipv6_rt_hdr *hdr;
455 struct rt0_hdr *rthdr;
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700456 struct net *net = dev_net(skb->dev);
457 int accept_source_route = net->ipv6.devconf_all->accept_source_route;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700458
Eric Dumazetf6bc7d92010-06-14 04:39:27 +0000459 idev = __in6_dev_get(skb->dev);
460 if (idev && accept_source_route > idev->cnf.accept_source_route)
461 accept_source_route = idev->cnf.accept_source_route;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700462
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700463 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
464 !pskb_may_pull(skb, (skb_transport_offset(skb) +
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700465 ((skb_transport_header(skb)[1] + 1) << 3)))) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700466 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
467 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 kfree_skb(skb);
469 return -1;
470 }
471
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700472 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700474 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 skb->pkt_type != PACKET_HOST) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700476 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
477 IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 kfree_skb(skb);
479 return -1;
480 }
481
David Lebrun1ababeb2016-11-08 14:57:39 +0100482 /* segment routing */
483 if (hdr->type == IPV6_SRCRT_TYPE_4)
484 return ipv6_srh_rcv(skb);
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486looped_back:
487 if (hdr->segments_left == 0) {
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700488 switch (hdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000489#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700490 case IPV6_SRCRT_TYPE_2:
491 /* Silently discard type 2 header unless it was
492 * processed by own
493 */
494 if (!addr) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700495 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
496 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700497 kfree_skb(skb);
498 return -1;
499 }
500 break;
501#endif
502 default:
503 break;
504 }
505
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300506 opt->lastopt = opt->srcrt = skb_network_header_len(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700507 skb->transport_header += (hdr->hdrlen + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 opt->dst0 = opt->dst1;
509 opt->dst1 = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700510 opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return 1;
512 }
513
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700514 switch (hdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000515#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700516 case IPV6_SRCRT_TYPE_2:
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700517 if (accept_source_route < 0)
518 goto unknown_rh;
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700519 /* Silently discard invalid RTH type 2 */
520 if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700521 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
522 IPSTATS_MIB_INHDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700523 kfree_skb(skb);
524 return -1;
525 }
526 break;
527#endif
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700528 default:
529 goto unknown_rh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /*
533 * This is the routing header forwarding algorithm from
534 * RFC 2460, page 16.
535 */
536
537 n = hdr->hdrlen >> 1;
538
539 if (hdr->segments_left > n) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700540 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
541 IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700542 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
543 ((&hdr->segments_left) -
544 skb_network_header(skb)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return -1;
546 }
547
548 /* We are about to mangle packet header. Be careful!
549 Do not damage packets queued somewhere.
550 */
551 if (skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* the copy is a forwarded packet */
Herbert Xu65c88462007-10-15 01:29:10 -0700553 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700554 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
555 IPSTATS_MIB_OUTDISCARDS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900556 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -1;
558 }
Herbert Xu65c88462007-10-15 01:29:10 -0700559 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
Patrick McHardy84fa7932006-08-29 16:44:56 -0700562 if (skb->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 skb->ip_summed = CHECKSUM_NONE;
564
565 i = n - --hdr->segments_left;
566
567 rthdr = (struct rt0_hdr *) hdr;
568 addr = rthdr->addr;
569 addr += i - 1;
570
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700571 switch (hdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000572#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700573 case IPV6_SRCRT_TYPE_2:
574 if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700575 (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700576 IPPROTO_ROUTING) < 0) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700577 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
578 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700579 kfree_skb(skb);
580 return -1;
581 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000582 if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700583 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
584 IPSTATS_MIB_INADDRERRORS);
Masahide NAKAMURA65d4ed92006-08-23 19:16:22 -0700585 kfree_skb(skb);
586 return -1;
587 }
588 break;
589#endif
590 default:
591 break;
592 }
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (ipv6_addr_is_multicast(addr)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700595 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
596 IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 kfree_skb(skb);
598 return -1;
599 }
600
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000601 daddr = *addr;
602 *addr = ipv6_hdr(skb)->daddr;
603 ipv6_hdr(skb)->daddr = daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Eric Dumazetadf30902009-06-02 05:19:30 +0000605 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 ip6_route_input(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000607 if (skb_dst(skb)->error) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700608 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 dst_input(skb);
610 return -1;
611 }
612
Eric Dumazetadf30902009-06-02 05:19:30 +0000613 if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700614 if (ipv6_hdr(skb)->hop_limit <= 1) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700615 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
616 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000618 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 kfree_skb(skb);
620 return -1;
621 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700622 ipv6_hdr(skb)->hop_limit--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto looped_back;
624 }
625
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700626 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 dst_input(skb);
628 return -1;
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700629
630unknown_rh:
Eric Dumazet1d015502016-04-27 16:44:40 -0700631 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideakic382bb92007-07-10 22:47:58 -0700632 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
633 (&hdr->type) - skb_network_header(skb));
634 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000637static const struct inet6_protocol rthdr_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 .handler = ipv6_rthdr_rcv,
Vlad Yasevich2207afc2012-11-15 08:49:19 +0000639 .flags = INET6_PROTO_NOPOLICY,
Vlad Yasevich8ca896c2012-11-15 08:49:13 +0000640};
641
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000642static const struct inet6_protocol destopt_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800643 .handler = ipv6_destopt_rcv,
Vlad Yasevich2207afc2012-11-15 08:49:19 +0000644 .flags = INET6_PROTO_NOPOLICY,
Vlad Yasevich8ca896c2012-11-15 08:49:13 +0000645};
646
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000647static const struct inet6_protocol nodata_protocol = {
Daniel Lezcano248b2382007-12-11 02:23:54 -0800648 .handler = dst_discard,
649 .flags = INET6_PROTO_NOPOLICY,
650};
651
652int __init ipv6_exthdrs_init(void)
653{
654 int ret;
655
Vlad Yasevich33362882012-11-15 08:49:15 +0000656 ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
657 if (ret)
Vlad Yasevichc6b641a2012-11-15 08:49:22 +0000658 goto out;
Vlad Yasevich33362882012-11-15 08:49:15 +0000659
Daniel Lezcano248b2382007-12-11 02:23:54 -0800660 ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
661 if (ret)
662 goto out_rthdr;
663
664 ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
665 if (ret)
666 goto out_destopt;
667
668out:
669 return ret;
Daniel Lezcano248b2382007-12-11 02:23:54 -0800670out_destopt:
671 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
Vlad Yasevich33362882012-11-15 08:49:15 +0000672out_rthdr:
673 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
Daniel Lezcano248b2382007-12-11 02:23:54 -0800674 goto out;
675};
676
677void ipv6_exthdrs_exit(void)
678{
679 inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
680 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
681 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/**********************************
685 Hop-by-hop options.
686 **********************************/
687
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700688/*
Eric Dumazetadf30902009-06-02 05:19:30 +0000689 * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700690 */
691static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
692{
Eric Dumazetadf30902009-06-02 05:19:30 +0000693 return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
YOSHIFUJI Hideakie76b2b22007-05-09 14:01:59 -0700694}
695
David S. Miller2570a4f2010-01-13 17:27:37 -0800696static inline struct net *ipv6_skb_net(struct sk_buff *skb)
697{
698 return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/* Router Alert as of RFC 2711 */
702
Eric Dumazeta50feda2012-05-18 18:57:34 +0000703static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700705 const unsigned char *nh = skb_network_header(skb);
Masahide NAKAMURAa80ff032006-08-23 19:19:50 -0700706
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700707 if (nh[optoff + 1] == 2) {
YOSHIFUJI Hideaki / 吉藤英明dd3332b2013-01-13 05:02:45 +0000708 IP6CB(skb)->flags |= IP6SKB_ROUTERALERT;
709 memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra));
Eric Dumazeta50feda2012-05-18 18:57:34 +0000710 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Joe Perchesba7a46f2014-11-11 10:59:17 -0800712 net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n",
713 nh[optoff + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000715 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718/* Jumbo payload */
719
Eric Dumazeta50feda2012-05-18 18:57:34 +0000720static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700722 const unsigned char *nh = skb_network_header(skb);
David S. Miller2570a4f2010-01-13 17:27:37 -0800723 struct net *net = ipv6_skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 u32 pkt_len;
725
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700726 if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800727 net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
728 nh[optoff+1]);
Eric Dumazet1d015502016-04-27 16:44:40 -0700729 __IP6_INC_STATS(net, ipv6_skb_idev(skb),
730 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto drop;
732 }
733
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700734 pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (pkt_len <= IPV6_MAXPLEN) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700736 __IP6_INC_STATS(net, ipv6_skb_idev(skb),
737 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000739 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700741 if (ipv6_hdr(skb)->payload_len) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700742 __IP6_INC_STATS(net, ipv6_skb_idev(skb),
743 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000745 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
748 if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700749 __IP6_INC_STATS(net, ipv6_skb_idev(skb),
750 IPSTATS_MIB_INTRUNCATEDPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 goto drop;
752 }
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700753
754 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
755 goto drop;
756
Paolo Abenicb891fa2017-07-31 16:52:36 +0200757 IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000758 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760drop:
761 kfree_skb(skb);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000762 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Huw Davies2e532b72016-06-27 15:06:17 -0400765/* CALIPSO RFC 5570 */
766
767static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff)
768{
769 const unsigned char *nh = skb_network_header(skb);
770
771 if (nh[optoff + 1] < 8)
772 goto drop;
773
774 if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1])
775 goto drop;
776
777 if (!calipso_validate(skb, nh + optoff))
778 goto drop;
779
780 return true;
781
782drop:
783 kfree_skb(skb);
784 return false;
785}
786
Eric Dumazeta50feda2012-05-18 18:57:34 +0000787static const struct tlvtype_proc tlvprochopopt_lst[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 {
789 .type = IPV6_TLV_ROUTERALERT,
790 .func = ipv6_hop_ra,
791 },
792 {
793 .type = IPV6_TLV_JUMBO,
794 .func = ipv6_hop_jumbo,
795 },
Huw Davies2e532b72016-06-27 15:06:17 -0400796 {
797 .type = IPV6_TLV_CALIPSO,
798 .func = ipv6_hop_calipso,
799 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 { -1, }
801};
802
Herbert Xue5bbef22007-10-15 12:50:28 -0700803int ipv6_parse_hopopts(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Patrick McHardy951dbc82006-01-06 23:02:34 -0800805 struct inet6_skb_parm *opt = IP6CB(skb);
806
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700807 /*
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700808 * skb_network_header(skb) is equal to skb->data, and
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300809 * skb_network_header_len(skb) is always equal to
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700810 * sizeof(struct ipv6hdr) by definition of
811 * hop-by-hop options.
812 */
813 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700814 !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
815 ((skb_transport_header(skb)[1] + 1) << 3)))) {
YOSHIFUJI Hideakiec670092006-04-18 14:46:26 -0700816 kfree_skb(skb);
817 return -1;
818 }
819
Florian Westphal8b58a392015-07-08 23:32:12 +0200820 opt->flags |= IP6SKB_HOPBYHOP;
Herbert Xue5bbef22007-10-15 12:50:28 -0700821 if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700822 skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
Masahide NAKAMURAdc435e62006-08-31 15:18:49 -0700823 opt = IP6CB(skb);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800824 opt->nhoff = sizeof(struct ipv6hdr);
YOSHIFUJI Hideakib8097392006-04-18 14:48:45 -0700825 return 1;
Patrick McHardy951dbc82006-01-06 23:02:34 -0800826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return -1;
828}
829
830/*
831 * Creating outbound headers.
832 *
833 * "build" functions work when skb is filled from head to tail (datagram)
834 * "push" functions work when headers are added from tail to head (tcp)
835 *
836 * In both cases we assume, that caller reserved enough room
837 * for headers.
838 */
839
David Lebruna149e7c2016-11-08 14:59:21 +0100840static void ipv6_push_rthdr0(struct sk_buff *skb, u8 *proto,
841 struct ipv6_rt_hdr *opt,
842 struct in6_addr **addr_p, struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct rt0_hdr *phdr, *ihdr;
845 int hops;
846
847 ihdr = (struct rt0_hdr *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900848
Johannes Bergd58ff352017-06-16 14:29:23 +0200849 phdr = skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
851
852 hops = ihdr->rt_hdr.hdrlen >> 1;
853
854 if (hops > 1)
855 memcpy(phdr->addr, ihdr->addr + 1,
856 (hops - 1) * sizeof(struct in6_addr));
857
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000858 phdr->addr[hops - 1] = **addr_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 *addr_p = ihdr->addr;
860
861 phdr->rt_hdr.nexthdr = *proto;
862 *proto = NEXTHDR_ROUTING;
863}
864
David Lebruna149e7c2016-11-08 14:59:21 +0100865static void ipv6_push_rthdr4(struct sk_buff *skb, u8 *proto,
866 struct ipv6_rt_hdr *opt,
867 struct in6_addr **addr_p, struct in6_addr *saddr)
868{
869 struct ipv6_sr_hdr *sr_phdr, *sr_ihdr;
870 int plen, hops;
871
872 sr_ihdr = (struct ipv6_sr_hdr *)opt;
873 plen = (sr_ihdr->hdrlen + 1) << 3;
874
Johannes Bergd58ff352017-06-16 14:29:23 +0200875 sr_phdr = skb_push(skb, plen);
David Lebruna149e7c2016-11-08 14:59:21 +0100876 memcpy(sr_phdr, sr_ihdr, sizeof(struct ipv6_sr_hdr));
877
878 hops = sr_ihdr->first_segment + 1;
879 memcpy(sr_phdr->segments + 1, sr_ihdr->segments + 1,
880 (hops - 1) * sizeof(struct in6_addr));
881
882 sr_phdr->segments[0] = **addr_p;
David Lebrun925615c2017-08-05 12:38:24 +0200883 *addr_p = &sr_ihdr->segments[sr_ihdr->segments_left];
David Lebruna149e7c2016-11-08 14:59:21 +0100884
885#ifdef CONFIG_IPV6_SEG6_HMAC
886 if (sr_has_hmac(sr_phdr)) {
887 struct net *net = NULL;
888
889 if (skb->dev)
890 net = dev_net(skb->dev);
891 else if (skb->sk)
892 net = sock_net(skb->sk);
893
894 WARN_ON(!net);
895
896 if (net)
897 seg6_push_hmac(net, saddr, sr_phdr);
898 }
899#endif
900
901 sr_phdr->nexthdr = *proto;
902 *proto = NEXTHDR_ROUTING;
903}
904
905static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
906 struct ipv6_rt_hdr *opt,
907 struct in6_addr **addr_p, struct in6_addr *saddr)
908{
909 switch (opt->type) {
910 case IPV6_SRCRT_TYPE_0:
Sabrina Dubrocaec9c4212017-04-25 15:56:50 +0200911 case IPV6_SRCRT_STRICT:
912 case IPV6_SRCRT_TYPE_2:
David Lebruna149e7c2016-11-08 14:59:21 +0100913 ipv6_push_rthdr0(skb, proto, opt, addr_p, saddr);
914 break;
915 case IPV6_SRCRT_TYPE_4:
916 ipv6_push_rthdr4(skb, proto, opt, addr_p, saddr);
917 break;
918 default:
919 break;
920 }
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
924{
Johannes Bergd58ff352017-06-16 14:29:23 +0200925 struct ipv6_opt_hdr *h = skb_push(skb, ipv6_optlen(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 memcpy(h, opt, ipv6_optlen(opt));
928 h->nexthdr = *proto;
929 *proto = type;
930}
931
932void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
933 u8 *proto,
David Lebrun613fa3c2016-11-08 14:59:20 +0100934 struct in6_addr **daddr, struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900936 if (opt->srcrt) {
David Lebrun613fa3c2016-11-08 14:59:20 +0100937 ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900938 /*
939 * IPV6_RTHDRDSTOPTS is ignored
940 * unless IPV6_RTHDR is set (RFC3542).
941 */
942 if (opt->dst0opt)
943 ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (opt->hopopt)
946 ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
947}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
950{
951 if (opt->dst1opt)
952 ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
953}
David S. Miller5b8481f2017-05-01 15:10:20 -0400954EXPORT_SYMBOL(ipv6_push_frag_opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956struct ipv6_txoptions *
957ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
958{
959 struct ipv6_txoptions *opt2;
960
961 opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
962 if (opt2) {
Eldad Zackac3c8172012-04-01 07:49:04 +0000963 long dif = (char *)opt2 - (char *)opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 memcpy(opt2, opt, opt->tot_len);
965 if (opt2->hopopt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000966 *((char **)&opt2->hopopt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (opt2->dst0opt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000968 *((char **)&opt2->dst0opt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (opt2->dst1opt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000970 *((char **)&opt2->dst1opt) += dif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (opt2->srcrt)
Eldad Zackac3c8172012-04-01 07:49:04 +0000972 *((char **)&opt2->srcrt) += dif;
Reshetova, Elena0aeea212017-07-04 09:34:54 +0300973 refcount_set(&opt2->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975 return opt2;
976}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800977EXPORT_SYMBOL_GPL(ipv6_dup_options);
978
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900979static int ipv6_renew_option(void *ohdr,
980 struct ipv6_opt_hdr __user *newopt, int newoptlen,
981 int inherit,
982 struct ipv6_opt_hdr **hdr,
983 char **p)
984{
985 if (inherit) {
986 if (ohdr) {
987 memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
988 *hdr = (struct ipv6_opt_hdr *)*p;
Joe Perchese3192692012-06-03 17:41:40 +0000989 *p += CMSG_ALIGN(ipv6_optlen(*hdr));
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900990 }
991 } else {
992 if (newopt) {
993 if (copy_from_user(*p, newopt, newoptlen))
994 return -EFAULT;
995 *hdr = (struct ipv6_opt_hdr *)*p;
Joe Perchese3192692012-06-03 17:41:40 +0000996 if (ipv6_optlen(*hdr) > newoptlen)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900997 return -EINVAL;
998 *p += CMSG_ALIGN(newoptlen);
999 }
1000 }
1001 return 0;
1002}
1003
Huw Daviese67ae212016-06-27 15:02:50 -04001004/**
1005 * ipv6_renew_options - replace a specific ext hdr with a new one.
1006 *
1007 * @sk: sock from which to allocate memory
1008 * @opt: original options
1009 * @newtype: option type to replace in @opt
1010 * @newopt: new option of type @newtype to replace (user-mem)
1011 * @newoptlen: length of @newopt
1012 *
1013 * Returns a new set of options which is a copy of @opt with the
1014 * option type @newtype replaced with @newopt.
1015 *
1016 * @opt may be NULL, in which case a new set of options is returned
1017 * containing just @newopt.
1018 *
1019 * @newopt may be NULL, in which case the specified option type is
1020 * not copied into the new set of options.
1021 *
1022 * The new set of options is allocated from the socket option memory
1023 * buffer of @sk.
1024 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001025struct ipv6_txoptions *
1026ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
1027 int newtype,
1028 struct ipv6_opt_hdr __user *newopt, int newoptlen)
1029{
1030 int tot_len = 0;
1031 char *p;
1032 struct ipv6_txoptions *opt2;
1033 int err;
1034
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -07001035 if (opt) {
1036 if (newtype != IPV6_HOPOPTS && opt->hopopt)
1037 tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
1038 if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
1039 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
1040 if (newtype != IPV6_RTHDR && opt->srcrt)
1041 tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
1042 if (newtype != IPV6_DSTOPTS && opt->dst1opt)
1043 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
1044 }
1045
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001046 if (newopt && newoptlen)
1047 tot_len += CMSG_ALIGN(newoptlen);
1048
1049 if (!tot_len)
1050 return NULL;
1051
YOSHIFUJI Hideaki8b8aa4b2005-11-20 12:18:17 +09001052 tot_len += sizeof(*opt2);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001053 opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
1054 if (!opt2)
1055 return ERR_PTR(-ENOBUFS);
1056
1057 memset(opt2, 0, tot_len);
Reshetova, Elena0aeea212017-07-04 09:34:54 +03001058 refcount_set(&opt2->refcnt, 1);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001059 opt2->tot_len = tot_len;
1060 p = (char *)(opt2 + 1);
1061
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -07001062 err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001063 newtype != IPV6_HOPOPTS,
1064 &opt2->hopopt, &p);
1065 if (err)
1066 goto out;
1067
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -07001068 err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001069 newtype != IPV6_RTHDRDSTOPTS,
1070 &opt2->dst0opt, &p);
1071 if (err)
1072 goto out;
1073
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -07001074 err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001075 newtype != IPV6_RTHDR,
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -07001076 (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001077 if (err)
1078 goto out;
1079
YOSHIFUJI Hideaki99c7bc02006-08-31 14:52:17 -07001080 err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001081 newtype != IPV6_DSTOPTS,
1082 &opt2->dst1opt, &p);
1083 if (err)
1084 goto out;
1085
1086 opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
1087 (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
1088 (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
1089 opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
1090
1091 return opt2;
1092out:
YOSHIFUJI Hideaki8b8aa4b2005-11-20 12:18:17 +09001093 sock_kfree_s(sk, opt2, opt2->tot_len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001094 return ERR_PTR(err);
1095}
1096
Huw Daviese67ae212016-06-27 15:02:50 -04001097/**
1098 * ipv6_renew_options_kern - replace a specific ext hdr with a new one.
1099 *
1100 * @sk: sock from which to allocate memory
1101 * @opt: original options
1102 * @newtype: option type to replace in @opt
1103 * @newopt: new option of type @newtype to replace (kernel-mem)
1104 * @newoptlen: length of @newopt
1105 *
1106 * See ipv6_renew_options(). The difference is that @newopt is
1107 * kernel memory, rather than user memory.
1108 */
1109struct ipv6_txoptions *
1110ipv6_renew_options_kern(struct sock *sk, struct ipv6_txoptions *opt,
1111 int newtype, struct ipv6_opt_hdr *newopt,
1112 int newoptlen)
1113{
1114 struct ipv6_txoptions *ret_val;
1115 const mm_segment_t old_fs = get_fs();
1116
1117 set_fs(KERNEL_DS);
1118 ret_val = ipv6_renew_options(sk, opt, newtype,
1119 (struct ipv6_opt_hdr __user *)newopt,
1120 newoptlen);
1121 set_fs(old_fs);
1122 return ret_val;
1123}
1124
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +09001125struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
1126 struct ipv6_txoptions *opt)
1127{
1128 /*
1129 * ignore the dest before srcrt unless srcrt is being included.
1130 * --yoshfuji
1131 */
1132 if (opt && opt->dst0opt && !opt->srcrt) {
1133 if (opt_space != opt) {
1134 memcpy(opt_space, opt, sizeof(*opt_space));
1135 opt = opt_space;
1136 }
1137 opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
1138 opt->dst0opt = NULL;
1139 }
1140
1141 return opt;
1142}
Chris Elstona495f832012-04-29 21:48:53 +00001143EXPORT_SYMBOL_GPL(ipv6_fixup_options);
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +09001144
Arnaud Ebalard20c59de2010-06-01 21:35:01 +00001145/**
1146 * fl6_update_dst - update flowi destination address with info given
1147 * by srcrt option, if any.
1148 *
David S. Miller4c9483b2011-03-12 16:22:43 -05001149 * @fl6: flowi6 for which daddr is to be updated
Arnaud Ebalard20c59de2010-06-01 21:35:01 +00001150 * @opt: struct ipv6_txoptions in which to look for srcrt opt
David S. Miller4c9483b2011-03-12 16:22:43 -05001151 * @orig: copy of original daddr address if modified
Arnaud Ebalard20c59de2010-06-01 21:35:01 +00001152 *
1153 * Returns NULL if no txoptions or no srcrt, otherwise returns orig
David S. Miller4c9483b2011-03-12 16:22:43 -05001154 * and initial value of fl6->daddr set in orig
Arnaud Ebalard20c59de2010-06-01 21:35:01 +00001155 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001156struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
Arnaud Ebalard20c59de2010-06-01 21:35:01 +00001157 const struct ipv6_txoptions *opt,
1158 struct in6_addr *orig)
1159{
1160 if (!opt || !opt->srcrt)
1161 return NULL;
1162
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001163 *orig = fl6->daddr;
David Lebruna149e7c2016-11-08 14:59:21 +01001164
1165 switch (opt->srcrt->type) {
1166 case IPV6_SRCRT_TYPE_0:
Sabrina Dubrocaec9c4212017-04-25 15:56:50 +02001167 case IPV6_SRCRT_STRICT:
1168 case IPV6_SRCRT_TYPE_2:
David Lebruna149e7c2016-11-08 14:59:21 +01001169 fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
1170 break;
1171 case IPV6_SRCRT_TYPE_4:
1172 {
1173 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)opt->srcrt;
1174
David Lebrun925615c2017-08-05 12:38:24 +02001175 fl6->daddr = srh->segments[srh->segments_left];
David Lebruna149e7c2016-11-08 14:59:21 +01001176 break;
1177 }
1178 default:
1179 return NULL;
1180 }
1181
Arnaud Ebalard20c59de2010-06-01 21:35:01 +00001182 return orig;
1183}
Arnaud Ebalard20c59de2010-06-01 21:35:01 +00001184EXPORT_SYMBOL_GPL(fl6_update_dst);