Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Extension Header handling for IPv6 |
| 4 | * Linux INET6 implementation |
| 5 | * |
| 6 | * Authors: |
| 7 | * Pedro Roque <roque@di.fc.ul.pt> |
| 8 | * Andi Kleen <ak@muc.de> |
| 9 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | /* Changes: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 13 | * yoshfuji : ensure not to overrun while parsing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * tlv options. |
| 15 | * Mitsuru KANDA @USAGI and: Remove ipv6_parse_exthdrs(). |
| 16 | * YOSHIFUJI Hideaki @USAGI Register inbound extension header |
| 17 | * handlers as inet6_protocol{}. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/socket.h> |
| 23 | #include <linux/sockios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/net.h> |
| 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/in6.h> |
| 27 | #include <linux/icmpv6.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 29 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Herbert Xu | 352e512 | 2007-11-13 21:34:06 -0800 | [diff] [blame] | 31 | #include <net/dst.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <net/sock.h> |
| 33 | #include <net/snmp.h> |
| 34 | |
| 35 | #include <net/ipv6.h> |
| 36 | #include <net/protocol.h> |
| 37 | #include <net/transp_v6.h> |
| 38 | #include <net/rawv6.h> |
| 39 | #include <net/ndisc.h> |
| 40 | #include <net/ip6_route.h> |
| 41 | #include <net/addrconf.h> |
Huw Davies | 2e532b7 | 2016-06-27 15:06:17 -0400 | [diff] [blame] | 42 | #include <net/calipso.h> |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 43 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 44 | #include <net/xfrm.h> |
| 45 | #endif |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 46 | #include <linux/seg6.h> |
| 47 | #include <net/seg6.h> |
David Lebrun | 9baee83 | 2016-11-08 14:59:19 +0100 | [diff] [blame] | 48 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 49 | #include <net/seg6_hmac.h> |
| 50 | #endif |
Alexander Aring | 8610c7c | 2020-03-27 18:00:20 -0400 | [diff] [blame] | 51 | #include <net/rpl.h> |
Justin Iurman | 9ee11f0 | 2021-07-20 21:42:57 +0200 | [diff] [blame] | 52 | #include <linux/ioam6.h> |
| 53 | #include <net/ioam6.h> |
| 54 | #include <net/dst_metadata.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Fabian Frederick | ce25698 | 2014-10-27 19:12:58 +0100 | [diff] [blame] | 56 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /********************* |
| 59 | Generic functions |
| 60 | *********************/ |
| 61 | |
| 62 | /* An unknown option is detected, decide what to do */ |
| 63 | |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 64 | static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff, |
| 65 | bool disallow_unknowns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 67 | if (disallow_unknowns) { |
| 68 | /* If unknown TLVs are disallowed by configuration |
| 69 | * then always silently drop packet. Note this also |
| 70 | * means no ICMP parameter problem is sent which |
| 71 | * could be a good property to mitigate a reflection DOS |
| 72 | * attack. |
| 73 | */ |
| 74 | |
| 75 | goto drop; |
| 76 | } |
| 77 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 78 | switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | case 0: /* ignore */ |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 80 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | case 1: /* drop packet */ |
| 83 | break; |
| 84 | |
| 85 | case 3: /* Send ICMP if not a multicast address and drop packet */ |
| 86 | /* Actually, it is redundant check. icmp_send |
| 87 | will recheck in any case. |
| 88 | */ |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 89 | if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | break; |
Joe Perches | a8eceea | 2020-03-12 15:50:22 -0700 | [diff] [blame] | 91 | fallthrough; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | case 2: /* send ICMP PARM PROB regardless and drop packet */ |
| 93 | icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 94 | return false; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 95 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 97 | drop: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 99 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Eric Dumazet | 51b8f81 | 2021-08-03 08:31:05 -0700 | [diff] [blame] | 102 | static bool ipv6_hop_ra(struct sk_buff *skb, int optoff); |
| 103 | static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff); |
| 104 | static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff); |
| 105 | static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff); |
| 106 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
| 107 | static bool ipv6_dest_hao(struct sk_buff *skb, int optoff); |
| 108 | #endif |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | /* Parse tlv encoded option header (hop-by-hop or destination) */ |
| 111 | |
Eric Dumazet | 51b8f81 | 2021-08-03 08:31:05 -0700 | [diff] [blame] | 112 | static bool ip6_parse_tlv(bool hopbyhop, |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 113 | struct sk_buff *skb, |
| 114 | int max_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 116 | int len = (skb_transport_header(skb)[1] + 1) << 3; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 117 | const unsigned char *nh = skb_network_header(skb); |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 118 | int off = skb_network_header_len(skb); |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 119 | bool disallow_unknowns = false; |
| 120 | int tlv_count = 0; |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 121 | int padlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 123 | if (unlikely(max_count < 0)) { |
| 124 | disallow_unknowns = true; |
| 125 | max_count = -max_count; |
| 126 | } |
| 127 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 128 | if (skb_transport_offset(skb) + len > skb_headlen(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | goto bad; |
| 130 | |
| 131 | off += 2; |
| 132 | len -= 2; |
| 133 | |
| 134 | while (len > 0) { |
Eric Dumazet | 624085a | 2021-06-24 03:07:20 -0700 | [diff] [blame] | 135 | int optlen, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Eric Dumazet | 624085a | 2021-06-24 03:07:20 -0700 | [diff] [blame] | 137 | if (nh[off] == IPV6_TLV_PAD1) { |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 138 | padlen++; |
| 139 | if (padlen > 7) |
| 140 | goto bad; |
Eric Dumazet | 624085a | 2021-06-24 03:07:20 -0700 | [diff] [blame] | 141 | off++; |
| 142 | len--; |
| 143 | continue; |
| 144 | } |
| 145 | if (len < 2) |
| 146 | goto bad; |
| 147 | optlen = nh[off + 1] + 2; |
| 148 | if (optlen > len) |
| 149 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Eric Dumazet | 624085a | 2021-06-24 03:07:20 -0700 | [diff] [blame] | 151 | if (nh[off] == IPV6_TLV_PADN) { |
Eldad Zack | c1412fc | 2012-04-12 17:36:17 -0400 | [diff] [blame] | 152 | /* RFC 2460 states that the purpose of PadN is |
| 153 | * to align the containing header to multiples |
| 154 | * of 8. 7 is therefore the highest valid value. |
| 155 | * See also RFC 4942, Section 2.1.9.5. |
| 156 | */ |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 157 | padlen += optlen; |
| 158 | if (padlen > 7) |
Eldad Zack | c1412fc | 2012-04-12 17:36:17 -0400 | [diff] [blame] | 159 | goto bad; |
| 160 | /* RFC 4942 recommends receiving hosts to |
| 161 | * actively check PadN payload to contain |
| 162 | * only zeroes. |
| 163 | */ |
| 164 | for (i = 2; i < optlen; i++) { |
| 165 | if (nh[off + i] != 0) |
| 166 | goto bad; |
| 167 | } |
Eric Dumazet | 624085a | 2021-06-24 03:07:20 -0700 | [diff] [blame] | 168 | } else { |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 169 | tlv_count++; |
| 170 | if (tlv_count > max_count) |
| 171 | goto bad; |
| 172 | |
Eric Dumazet | 51b8f81 | 2021-08-03 08:31:05 -0700 | [diff] [blame] | 173 | if (hopbyhop) { |
| 174 | switch (nh[off]) { |
| 175 | case IPV6_TLV_ROUTERALERT: |
| 176 | if (!ipv6_hop_ra(skb, off)) |
| 177 | return false; |
| 178 | break; |
| 179 | case IPV6_TLV_IOAM: |
| 180 | if (!ipv6_hop_ioam(skb, off)) |
| 181 | return false; |
| 182 | break; |
| 183 | case IPV6_TLV_JUMBO: |
| 184 | if (!ipv6_hop_jumbo(skb, off)) |
| 185 | return false; |
| 186 | break; |
| 187 | case IPV6_TLV_CALIPSO: |
| 188 | if (!ipv6_hop_calipso(skb, off)) |
| 189 | return false; |
| 190 | break; |
| 191 | default: |
| 192 | if (!ip6_tlvopt_unknown(skb, off, |
| 193 | disallow_unknowns)) |
| 194 | return false; |
| 195 | break; |
| 196 | } |
| 197 | } else { |
| 198 | switch (nh[off]) { |
| 199 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
| 200 | case IPV6_TLV_HAO: |
| 201 | if (!ipv6_dest_hao(skb, off)) |
| 202 | return false; |
| 203 | break; |
| 204 | #endif |
| 205 | default: |
| 206 | if (!ip6_tlvopt_unknown(skb, off, |
| 207 | disallow_unknowns)) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 208 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | break; |
| 210 | } |
| 211 | } |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 212 | padlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | off += optlen; |
| 215 | len -= optlen; |
| 216 | } |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (len == 0) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 219 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | bad: |
| 221 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 222 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /***************************** |
| 226 | Destination options header. |
| 227 | *****************************/ |
| 228 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 229 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 230 | static bool ipv6_dest_hao(struct sk_buff *skb, int optoff) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 231 | { |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 232 | struct ipv6_destopt_hao *hao; |
| 233 | struct inet6_skb_parm *opt = IP6CB(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 234 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 235 | int ret; |
| 236 | |
| 237 | if (opt->dsthao) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 238 | net_dbg_ratelimited("hao duplicated\n"); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 239 | goto discard; |
| 240 | } |
| 241 | opt->dsthao = opt->dst1; |
| 242 | opt->dst1 = 0; |
| 243 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 244 | hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 245 | |
| 246 | if (hao->length != 16) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 247 | net_dbg_ratelimited("hao invalid option length = %d\n", |
| 248 | hao->length); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 249 | goto discard; |
| 250 | } |
| 251 | |
| 252 | if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 253 | net_dbg_ratelimited("hao is not an unicast addr: %pI6\n", |
| 254 | &hao->addr); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 255 | goto discard; |
| 256 | } |
| 257 | |
| 258 | ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr, |
| 259 | (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS); |
| 260 | if (unlikely(ret < 0)) |
| 261 | goto discard; |
| 262 | |
| 263 | if (skb_cloned(skb)) { |
Herbert Xu | 65c8846 | 2007-10-15 01:29:10 -0700 | [diff] [blame] | 264 | if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 265 | goto discard; |
| 266 | |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 267 | /* update all variable using below by copied skbuff */ |
Herbert Xu | 65c8846 | 2007-10-15 01:29:10 -0700 | [diff] [blame] | 268 | hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 269 | optoff); |
Herbert Xu | 65c8846 | 2007-10-15 01:29:10 -0700 | [diff] [blame] | 270 | ipv6h = ipv6_hdr(skb); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 274 | skb->ip_summed = CHECKSUM_NONE; |
| 275 | |
Gustavo A. R. Silva | 74b6551 | 2017-10-26 23:10:35 -0500 | [diff] [blame] | 276 | swap(ipv6h->saddr, hao->addr); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 277 | |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 278 | if (skb->tstamp == 0) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 279 | __net_timestamp(skb); |
| 280 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 281 | return true; |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 282 | |
| 283 | discard: |
| 284 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 285 | return false; |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 286 | } |
| 287 | #endif |
| 288 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 289 | static int ipv6_destopt_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 291 | struct inet6_dev *idev = __in6_dev_get(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | struct inet6_skb_parm *opt = IP6CB(skb); |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 293 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 294 | __u16 dstbuf; |
| 295 | #endif |
Eric Dumazet | 897dc80 | 2011-07-28 04:00:35 +0000 | [diff] [blame] | 296 | struct dst_entry *dst = skb_dst(skb); |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 297 | struct net *net = dev_net(skb->dev); |
| 298 | int extlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 300 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) || |
| 301 | !pskb_may_pull(skb, (skb_transport_offset(skb) + |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 302 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 303 | __IP6_INC_STATS(dev_net(dst->dev), idev, |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 304 | IPSTATS_MIB_INHDRERRORS); |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 305 | fail_and_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | kfree_skb(skb); |
| 307 | return -1; |
| 308 | } |
| 309 | |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 310 | extlen = (skb_transport_header(skb)[1] + 1) << 3; |
| 311 | if (extlen > net->ipv6.sysctl.max_dst_opts_len) |
| 312 | goto fail_and_free; |
| 313 | |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 314 | opt->lastopt = opt->dst1 = skb_network_header_len(skb); |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 315 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 316 | dstbuf = opt->dst1; |
| 317 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Eric Dumazet | 51b8f81 | 2021-08-03 08:31:05 -0700 | [diff] [blame] | 319 | if (ip6_parse_tlv(false, skb, net->ipv6.sysctl.max_dst_opts_cnt)) { |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 320 | skb->transport_header += extlen; |
Masahide NAKAMURA | dc435e6 | 2006-08-31 15:18:49 -0700 | [diff] [blame] | 321 | opt = IP6CB(skb); |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 322 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 323 | opt->nhoff = dstbuf; |
| 324 | #else |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 325 | opt->nhoff = opt->dst1; |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 326 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | return 1; |
| 328 | } |
| 329 | |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 330 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return -1; |
| 332 | } |
| 333 | |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 334 | static void seg6_update_csum(struct sk_buff *skb) |
| 335 | { |
| 336 | struct ipv6_sr_hdr *hdr; |
| 337 | struct in6_addr *addr; |
| 338 | __be32 from, to; |
| 339 | |
| 340 | /* srh is at transport offset and seg_left is already decremented |
| 341 | * but daddr is not yet updated with next segment |
| 342 | */ |
| 343 | |
| 344 | hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb); |
| 345 | addr = hdr->segments + hdr->segments_left; |
| 346 | |
| 347 | hdr->segments_left++; |
| 348 | from = *(__be32 *)hdr; |
| 349 | |
| 350 | hdr->segments_left--; |
| 351 | to = *(__be32 *)hdr; |
| 352 | |
| 353 | /* update skb csum with diff resulting from seg_left decrement */ |
| 354 | |
| 355 | update_csum_diff4(skb, from, to); |
| 356 | |
| 357 | /* compute csum diff between current and next segment and update */ |
| 358 | |
| 359 | update_csum_diff16(skb, (__be32 *)(&ipv6_hdr(skb)->daddr), |
| 360 | (__be32 *)addr); |
| 361 | } |
| 362 | |
| 363 | static int ipv6_srh_rcv(struct sk_buff *skb) |
| 364 | { |
| 365 | struct inet6_skb_parm *opt = IP6CB(skb); |
| 366 | struct net *net = dev_net(skb->dev); |
| 367 | struct ipv6_sr_hdr *hdr; |
| 368 | struct inet6_dev *idev; |
| 369 | struct in6_addr *addr; |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 370 | int accept_seg6; |
| 371 | |
| 372 | hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb); |
| 373 | |
| 374 | idev = __in6_dev_get(skb->dev); |
| 375 | |
| 376 | accept_seg6 = net->ipv6.devconf_all->seg6_enabled; |
| 377 | if (accept_seg6 > idev->cnf.seg6_enabled) |
| 378 | accept_seg6 = idev->cnf.seg6_enabled; |
| 379 | |
| 380 | if (!accept_seg6) { |
| 381 | kfree_skb(skb); |
| 382 | return -1; |
| 383 | } |
| 384 | |
David Lebrun | 9baee83 | 2016-11-08 14:59:19 +0100 | [diff] [blame] | 385 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 386 | if (!seg6_hmac_validate_skb(skb)) { |
| 387 | kfree_skb(skb); |
| 388 | return -1; |
| 389 | } |
| 390 | #endif |
| 391 | |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 392 | looped_back: |
David Lebrun | 013e816 | 2017-02-02 11:29:38 +0100 | [diff] [blame] | 393 | if (hdr->segments_left == 0) { |
Julien Massonneau | ee90c6b | 2021-03-11 16:53:18 +0100 | [diff] [blame] | 394 | if (hdr->nexthdr == NEXTHDR_IPV6 || hdr->nexthdr == NEXTHDR_IPV4) { |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 395 | int offset = (hdr->hdrlen + 1) << 3; |
| 396 | |
| 397 | skb_postpull_rcsum(skb, skb_network_header(skb), |
| 398 | skb_network_header_len(skb)); |
| 399 | |
| 400 | if (!pskb_pull(skb, offset)) { |
| 401 | kfree_skb(skb); |
| 402 | return -1; |
| 403 | } |
| 404 | skb_postpull_rcsum(skb, skb_transport_header(skb), |
| 405 | offset); |
| 406 | |
| 407 | skb_reset_network_header(skb); |
| 408 | skb_reset_transport_header(skb); |
| 409 | skb->encapsulation = 0; |
Julien Massonneau | ee90c6b | 2021-03-11 16:53:18 +0100 | [diff] [blame] | 410 | if (hdr->nexthdr == NEXTHDR_IPV4) |
| 411 | skb->protocol = htons(ETH_P_IP); |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 412 | __skb_tunnel_rx(skb, skb->dev, net); |
| 413 | |
| 414 | netif_rx(skb); |
| 415 | return -1; |
| 416 | } |
| 417 | |
| 418 | opt->srcrt = skb_network_header_len(skb); |
| 419 | opt->lastopt = opt->srcrt; |
| 420 | skb->transport_header += (hdr->hdrlen + 1) << 3; |
| 421 | opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb); |
| 422 | |
| 423 | return 1; |
| 424 | } |
| 425 | |
| 426 | if (hdr->segments_left >= (hdr->hdrlen >> 1)) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 427 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 428 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 429 | ((&hdr->segments_left) - |
| 430 | skb_network_header(skb))); |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 431 | return -1; |
| 432 | } |
| 433 | |
| 434 | if (skb_cloned(skb)) { |
| 435 | if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) { |
| 436 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 437 | IPSTATS_MIB_OUTDISCARDS); |
| 438 | kfree_skb(skb); |
| 439 | return -1; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb); |
| 444 | |
| 445 | hdr->segments_left--; |
| 446 | addr = hdr->segments + hdr->segments_left; |
| 447 | |
| 448 | skb_push(skb, sizeof(struct ipv6hdr)); |
| 449 | |
| 450 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 451 | seg6_update_csum(skb); |
| 452 | |
| 453 | ipv6_hdr(skb)->daddr = *addr; |
| 454 | |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 455 | skb_dst_drop(skb); |
| 456 | |
| 457 | ip6_route_input(skb); |
| 458 | |
| 459 | if (skb_dst(skb)->error) { |
| 460 | dst_input(skb); |
| 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) { |
| 465 | if (ipv6_hdr(skb)->hop_limit <= 1) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 466 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 467 | icmpv6_send(skb, ICMPV6_TIME_EXCEED, |
| 468 | ICMPV6_EXC_HOPLIMIT, 0); |
| 469 | kfree_skb(skb); |
| 470 | return -1; |
| 471 | } |
| 472 | ipv6_hdr(skb)->hop_limit--; |
| 473 | |
David Lebrun | 013e816 | 2017-02-02 11:29:38 +0100 | [diff] [blame] | 474 | skb_pull(skb, sizeof(struct ipv6hdr)); |
| 475 | goto looped_back; |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | dst_input(skb); |
| 479 | |
| 480 | return -1; |
| 481 | } |
| 482 | |
Alexander Aring | 8610c7c | 2020-03-27 18:00:20 -0400 | [diff] [blame] | 483 | static int ipv6_rpl_srh_rcv(struct sk_buff *skb) |
| 484 | { |
| 485 | struct ipv6_rpl_sr_hdr *hdr, *ohdr, *chdr; |
| 486 | struct inet6_skb_parm *opt = IP6CB(skb); |
| 487 | struct net *net = dev_net(skb->dev); |
| 488 | struct inet6_dev *idev; |
| 489 | struct ipv6hdr *oldhdr; |
| 490 | struct in6_addr addr; |
| 491 | unsigned char *buf; |
| 492 | int accept_rpl_seg; |
| 493 | int i, err; |
| 494 | u64 n = 0; |
| 495 | u32 r; |
| 496 | |
| 497 | idev = __in6_dev_get(skb->dev); |
| 498 | |
| 499 | accept_rpl_seg = net->ipv6.devconf_all->rpl_seg_enabled; |
| 500 | if (accept_rpl_seg > idev->cnf.rpl_seg_enabled) |
| 501 | accept_rpl_seg = idev->cnf.rpl_seg_enabled; |
| 502 | |
| 503 | if (!accept_rpl_seg) { |
| 504 | kfree_skb(skb); |
| 505 | return -1; |
| 506 | } |
| 507 | |
| 508 | looped_back: |
| 509 | hdr = (struct ipv6_rpl_sr_hdr *)skb_transport_header(skb); |
| 510 | |
| 511 | if (hdr->segments_left == 0) { |
| 512 | if (hdr->nexthdr == NEXTHDR_IPV6) { |
| 513 | int offset = (hdr->hdrlen + 1) << 3; |
| 514 | |
| 515 | skb_postpull_rcsum(skb, skb_network_header(skb), |
| 516 | skb_network_header_len(skb)); |
| 517 | |
| 518 | if (!pskb_pull(skb, offset)) { |
| 519 | kfree_skb(skb); |
| 520 | return -1; |
| 521 | } |
| 522 | skb_postpull_rcsum(skb, skb_transport_header(skb), |
| 523 | offset); |
| 524 | |
| 525 | skb_reset_network_header(skb); |
| 526 | skb_reset_transport_header(skb); |
| 527 | skb->encapsulation = 0; |
| 528 | |
| 529 | __skb_tunnel_rx(skb, skb->dev, net); |
| 530 | |
| 531 | netif_rx(skb); |
| 532 | return -1; |
| 533 | } |
| 534 | |
| 535 | opt->srcrt = skb_network_header_len(skb); |
| 536 | opt->lastopt = opt->srcrt; |
| 537 | skb->transport_header += (hdr->hdrlen + 1) << 3; |
| 538 | opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb); |
| 539 | |
| 540 | return 1; |
| 541 | } |
| 542 | |
| 543 | if (!pskb_may_pull(skb, sizeof(*hdr))) { |
| 544 | kfree_skb(skb); |
| 545 | return -1; |
| 546 | } |
| 547 | |
| 548 | n = (hdr->hdrlen << 3) - hdr->pad - (16 - hdr->cmpre); |
| 549 | r = do_div(n, (16 - hdr->cmpri)); |
| 550 | /* checks if calculation was without remainder and n fits into |
| 551 | * unsigned char which is segments_left field. Should not be |
| 552 | * higher than that. |
| 553 | */ |
| 554 | if (r || (n + 1) > 255) { |
| 555 | kfree_skb(skb); |
| 556 | return -1; |
| 557 | } |
| 558 | |
| 559 | if (hdr->segments_left > n + 1) { |
| 560 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
| 561 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 562 | ((&hdr->segments_left) - |
| 563 | skb_network_header(skb))); |
| 564 | return -1; |
| 565 | } |
| 566 | |
| 567 | if (skb_cloned(skb)) { |
| 568 | if (pskb_expand_head(skb, IPV6_RPL_SRH_WORST_SWAP_SIZE, 0, |
| 569 | GFP_ATOMIC)) { |
| 570 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 571 | IPSTATS_MIB_OUTDISCARDS); |
| 572 | kfree_skb(skb); |
| 573 | return -1; |
| 574 | } |
| 575 | } else { |
| 576 | err = skb_cow_head(skb, IPV6_RPL_SRH_WORST_SWAP_SIZE); |
| 577 | if (unlikely(err)) { |
| 578 | kfree_skb(skb); |
| 579 | return -1; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | hdr = (struct ipv6_rpl_sr_hdr *)skb_transport_header(skb); |
| 584 | |
| 585 | if (!pskb_may_pull(skb, ipv6_rpl_srh_size(n, hdr->cmpri, |
| 586 | hdr->cmpre))) { |
| 587 | kfree_skb(skb); |
| 588 | return -1; |
| 589 | } |
| 590 | |
| 591 | hdr->segments_left--; |
| 592 | i = n - hdr->segments_left; |
| 593 | |
Gustavo A. R. Silva | 6f39345 | 2020-06-22 18:07:41 -0500 | [diff] [blame] | 594 | buf = kcalloc(struct_size(hdr, segments.addr, n + 2), 2, GFP_ATOMIC); |
Alexander Aring | 8610c7c | 2020-03-27 18:00:20 -0400 | [diff] [blame] | 595 | if (unlikely(!buf)) { |
| 596 | kfree_skb(skb); |
| 597 | return -1; |
| 598 | } |
| 599 | |
| 600 | ohdr = (struct ipv6_rpl_sr_hdr *)buf; |
| 601 | ipv6_rpl_srh_decompress(ohdr, hdr, &ipv6_hdr(skb)->daddr, n); |
| 602 | chdr = (struct ipv6_rpl_sr_hdr *)(buf + ((ohdr->hdrlen + 1) << 3)); |
| 603 | |
| 604 | if ((ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST) || |
| 605 | (ipv6_addr_type(&ohdr->rpl_segaddr[i]) & IPV6_ADDR_MULTICAST)) { |
| 606 | kfree_skb(skb); |
| 607 | kfree(buf); |
| 608 | return -1; |
| 609 | } |
| 610 | |
| 611 | err = ipv6_chk_rpl_srh_loop(net, ohdr->rpl_segaddr, n + 1); |
| 612 | if (err) { |
| 613 | icmpv6_send(skb, ICMPV6_PARAMPROB, 0, 0); |
| 614 | kfree_skb(skb); |
| 615 | kfree(buf); |
| 616 | return -1; |
| 617 | } |
| 618 | |
| 619 | addr = ipv6_hdr(skb)->daddr; |
| 620 | ipv6_hdr(skb)->daddr = ohdr->rpl_segaddr[i]; |
| 621 | ohdr->rpl_segaddr[i] = addr; |
| 622 | |
| 623 | ipv6_rpl_srh_compress(chdr, ohdr, &ipv6_hdr(skb)->daddr, n); |
| 624 | |
| 625 | oldhdr = ipv6_hdr(skb); |
| 626 | |
| 627 | skb_pull(skb, ((hdr->hdrlen + 1) << 3)); |
| 628 | skb_postpull_rcsum(skb, oldhdr, |
| 629 | sizeof(struct ipv6hdr) + ((hdr->hdrlen + 1) << 3)); |
| 630 | skb_push(skb, ((chdr->hdrlen + 1) << 3) + sizeof(struct ipv6hdr)); |
| 631 | skb_reset_network_header(skb); |
| 632 | skb_mac_header_rebuild(skb); |
| 633 | skb_set_transport_header(skb, sizeof(struct ipv6hdr)); |
| 634 | |
| 635 | memmove(ipv6_hdr(skb), oldhdr, sizeof(struct ipv6hdr)); |
| 636 | memcpy(skb_transport_header(skb), chdr, (chdr->hdrlen + 1) << 3); |
| 637 | |
| 638 | ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); |
| 639 | skb_postpush_rcsum(skb, ipv6_hdr(skb), |
| 640 | sizeof(struct ipv6hdr) + ((chdr->hdrlen + 1) << 3)); |
| 641 | |
| 642 | kfree(buf); |
| 643 | |
| 644 | skb_dst_drop(skb); |
| 645 | |
| 646 | ip6_route_input(skb); |
| 647 | |
| 648 | if (skb_dst(skb)->error) { |
| 649 | dst_input(skb); |
| 650 | return -1; |
| 651 | } |
| 652 | |
| 653 | if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) { |
| 654 | if (ipv6_hdr(skb)->hop_limit <= 1) { |
| 655 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
| 656 | icmpv6_send(skb, ICMPV6_TIME_EXCEED, |
| 657 | ICMPV6_EXC_HOPLIMIT, 0); |
| 658 | kfree_skb(skb); |
| 659 | return -1; |
| 660 | } |
| 661 | ipv6_hdr(skb)->hop_limit--; |
| 662 | |
| 663 | skb_pull(skb, sizeof(struct ipv6hdr)); |
| 664 | goto looped_back; |
| 665 | } |
| 666 | |
| 667 | dst_input(skb); |
| 668 | |
| 669 | return -1; |
| 670 | } |
| 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | /******************************** |
| 673 | Routing header. |
| 674 | ********************************/ |
| 675 | |
Eric Dumazet | f6bc7d9 | 2010-06-14 04:39:27 +0000 | [diff] [blame] | 676 | /* called with rcu_read_lock() */ |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 677 | static int ipv6_rthdr_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 679 | struct inet6_dev *idev = __in6_dev_get(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | struct inet6_skb_parm *opt = IP6CB(skb); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 681 | struct in6_addr *addr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | struct in6_addr daddr; |
| 683 | int n, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | struct ipv6_rt_hdr *hdr; |
| 685 | struct rt0_hdr *rthdr; |
Denis V. Lunev | 483a47d | 2008-10-08 11:09:27 -0700 | [diff] [blame] | 686 | struct net *net = dev_net(skb->dev); |
| 687 | int accept_source_route = net->ipv6.devconf_all->accept_source_route; |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 688 | |
Eric Dumazet | f6bc7d9 | 2010-06-14 04:39:27 +0000 | [diff] [blame] | 689 | if (idev && accept_source_route > idev->cnf.accept_source_route) |
| 690 | accept_source_route = idev->cnf.accept_source_route; |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 691 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 692 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) || |
| 693 | !pskb_may_pull(skb, (skb_transport_offset(skb) + |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 694 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 695 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | kfree_skb(skb); |
| 697 | return -1; |
| 698 | } |
| 699 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 700 | hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 702 | if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | skb->pkt_type != PACKET_HOST) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 704 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | kfree_skb(skb); |
| 706 | return -1; |
| 707 | } |
| 708 | |
Alexander Aring | 8610c7c | 2020-03-27 18:00:20 -0400 | [diff] [blame] | 709 | switch (hdr->type) { |
| 710 | case IPV6_SRCRT_TYPE_4: |
| 711 | /* segment routing */ |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 712 | return ipv6_srh_rcv(skb); |
Alexander Aring | 8610c7c | 2020-03-27 18:00:20 -0400 | [diff] [blame] | 713 | case IPV6_SRCRT_TYPE_3: |
| 714 | /* rpl segment routing */ |
| 715 | return ipv6_rpl_srh_rcv(skb); |
| 716 | default: |
| 717 | break; |
| 718 | } |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | looped_back: |
| 721 | if (hdr->segments_left == 0) { |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 722 | switch (hdr->type) { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 723 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 724 | case IPV6_SRCRT_TYPE_2: |
| 725 | /* Silently discard type 2 header unless it was |
| 726 | * processed by own |
| 727 | */ |
| 728 | if (!addr) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 729 | __IP6_INC_STATS(net, idev, |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 730 | IPSTATS_MIB_INADDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 731 | kfree_skb(skb); |
| 732 | return -1; |
| 733 | } |
| 734 | break; |
| 735 | #endif |
| 736 | default: |
| 737 | break; |
| 738 | } |
| 739 | |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 740 | opt->lastopt = opt->srcrt = skb_network_header_len(skb); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 741 | skb->transport_header += (hdr->hdrlen + 1) << 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | opt->dst0 = opt->dst1; |
| 743 | opt->dst1 = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 744 | opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | return 1; |
| 746 | } |
| 747 | |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 748 | switch (hdr->type) { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 749 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 750 | case IPV6_SRCRT_TYPE_2: |
YOSHIFUJI Hideaki | c382bb9 | 2007-07-10 22:47:58 -0700 | [diff] [blame] | 751 | if (accept_source_route < 0) |
| 752 | goto unknown_rh; |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 753 | /* Silently discard invalid RTH type 2 */ |
| 754 | if (hdr->hdrlen != 2 || hdr->segments_left != 1) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 755 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 756 | kfree_skb(skb); |
| 757 | return -1; |
| 758 | } |
| 759 | break; |
| 760 | #endif |
YOSHIFUJI Hideaki | c382bb9 | 2007-07-10 22:47:58 -0700 | [diff] [blame] | 761 | default: |
| 762 | goto unknown_rh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
| 765 | /* |
| 766 | * This is the routing header forwarding algorithm from |
| 767 | * RFC 2460, page 16. |
| 768 | */ |
| 769 | |
| 770 | n = hdr->hdrlen >> 1; |
| 771 | |
| 772 | if (hdr->segments_left > n) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 773 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 774 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 775 | ((&hdr->segments_left) - |
| 776 | skb_network_header(skb))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | return -1; |
| 778 | } |
| 779 | |
| 780 | /* We are about to mangle packet header. Be careful! |
| 781 | Do not damage packets queued somewhere. |
| 782 | */ |
| 783 | if (skb_cloned(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | /* the copy is a forwarded packet */ |
Herbert Xu | 65c8846 | 2007-10-15 01:29:10 -0700 | [diff] [blame] | 785 | if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 786 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 787 | IPSTATS_MIB_OUTDISCARDS); |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 788 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | return -1; |
| 790 | } |
Herbert Xu | 65c8846 | 2007-10-15 01:29:10 -0700 | [diff] [blame] | 791 | hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 794 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | skb->ip_summed = CHECKSUM_NONE; |
| 796 | |
| 797 | i = n - --hdr->segments_left; |
| 798 | |
| 799 | rthdr = (struct rt0_hdr *) hdr; |
| 800 | addr = rthdr->addr; |
| 801 | addr += i - 1; |
| 802 | |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 803 | switch (hdr->type) { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 804 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 805 | case IPV6_SRCRT_TYPE_2: |
| 806 | if (xfrm6_input_addr(skb, (xfrm_address_t *)addr, |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 807 | (xfrm_address_t *)&ipv6_hdr(skb)->saddr, |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 808 | IPPROTO_ROUTING) < 0) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 809 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 810 | kfree_skb(skb); |
| 811 | return -1; |
| 812 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 813 | if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 814 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 815 | kfree_skb(skb); |
| 816 | return -1; |
| 817 | } |
| 818 | break; |
| 819 | #endif |
| 820 | default: |
| 821 | break; |
| 822 | } |
| 823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | if (ipv6_addr_is_multicast(addr)) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 825 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | kfree_skb(skb); |
| 827 | return -1; |
| 828 | } |
| 829 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 830 | daddr = *addr; |
| 831 | *addr = ipv6_hdr(skb)->daddr; |
| 832 | ipv6_hdr(skb)->daddr = daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 834 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | ip6_route_input(skb); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 836 | if (skb_dst(skb)->error) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 837 | skb_push(skb, skb->data - skb_network_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | dst_input(skb); |
| 839 | return -1; |
| 840 | } |
| 841 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 842 | if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 843 | if (ipv6_hdr(skb)->hop_limit <= 1) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 844 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 846 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | kfree_skb(skb); |
| 848 | return -1; |
| 849 | } |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 850 | ipv6_hdr(skb)->hop_limit--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | goto looped_back; |
| 852 | } |
| 853 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 854 | skb_push(skb, skb->data - skb_network_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | dst_input(skb); |
| 856 | return -1; |
YOSHIFUJI Hideaki | c382bb9 | 2007-07-10 22:47:58 -0700 | [diff] [blame] | 857 | |
| 858 | unknown_rh: |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 859 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
YOSHIFUJI Hideaki | c382bb9 | 2007-07-10 22:47:58 -0700 | [diff] [blame] | 860 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 861 | (&hdr->type) - skb_network_header(skb)); |
| 862 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 865 | static const struct inet6_protocol rthdr_protocol = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | .handler = ipv6_rthdr_rcv, |
Vlad Yasevich | 2207afc | 2012-11-15 08:49:19 +0000 | [diff] [blame] | 867 | .flags = INET6_PROTO_NOPOLICY, |
Vlad Yasevich | 8ca896c | 2012-11-15 08:49:13 +0000 | [diff] [blame] | 868 | }; |
| 869 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 870 | static const struct inet6_protocol destopt_protocol = { |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 871 | .handler = ipv6_destopt_rcv, |
Vlad Yasevich | 2207afc | 2012-11-15 08:49:19 +0000 | [diff] [blame] | 872 | .flags = INET6_PROTO_NOPOLICY, |
Vlad Yasevich | 8ca896c | 2012-11-15 08:49:13 +0000 | [diff] [blame] | 873 | }; |
| 874 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 875 | static const struct inet6_protocol nodata_protocol = { |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 876 | .handler = dst_discard, |
| 877 | .flags = INET6_PROTO_NOPOLICY, |
| 878 | }; |
| 879 | |
| 880 | int __init ipv6_exthdrs_init(void) |
| 881 | { |
| 882 | int ret; |
| 883 | |
Vlad Yasevich | 3336288 | 2012-11-15 08:49:15 +0000 | [diff] [blame] | 884 | ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING); |
| 885 | if (ret) |
Vlad Yasevich | c6b641a | 2012-11-15 08:49:22 +0000 | [diff] [blame] | 886 | goto out; |
Vlad Yasevich | 3336288 | 2012-11-15 08:49:15 +0000 | [diff] [blame] | 887 | |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 888 | ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS); |
| 889 | if (ret) |
| 890 | goto out_rthdr; |
| 891 | |
| 892 | ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE); |
| 893 | if (ret) |
| 894 | goto out_destopt; |
| 895 | |
| 896 | out: |
| 897 | return ret; |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 898 | out_destopt: |
| 899 | inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS); |
Vlad Yasevich | 3336288 | 2012-11-15 08:49:15 +0000 | [diff] [blame] | 900 | out_rthdr: |
| 901 | inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING); |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 902 | goto out; |
| 903 | }; |
| 904 | |
| 905 | void ipv6_exthdrs_exit(void) |
| 906 | { |
| 907 | inet6_del_protocol(&nodata_protocol, IPPROTO_NONE); |
| 908 | inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS); |
| 909 | inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING); |
| 910 | } |
| 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | /********************************** |
| 913 | Hop-by-hop options. |
| 914 | **********************************/ |
| 915 | |
YOSHIFUJI Hideaki | e76b2b2 | 2007-05-09 14:01:59 -0700 | [diff] [blame] | 916 | /* |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 917 | * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input(). |
YOSHIFUJI Hideaki | e76b2b2 | 2007-05-09 14:01:59 -0700 | [diff] [blame] | 918 | */ |
David S. Miller | 2570a4f | 2010-01-13 17:27:37 -0800 | [diff] [blame] | 919 | static inline struct net *ipv6_skb_net(struct sk_buff *skb) |
| 920 | { |
| 921 | return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev); |
| 922 | } |
| 923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | /* Router Alert as of RFC 2711 */ |
| 925 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 926 | static bool ipv6_hop_ra(struct sk_buff *skb, int optoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 928 | const unsigned char *nh = skb_network_header(skb); |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 929 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 930 | if (nh[optoff + 1] == 2) { |
YOSHIFUJI Hideaki / 吉藤英明 | dd3332b | 2013-01-13 05:02:45 +0000 | [diff] [blame] | 931 | IP6CB(skb)->flags |= IP6SKB_ROUTERALERT; |
| 932 | memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra)); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 933 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | } |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 935 | net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n", |
| 936 | nh[optoff + 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 938 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Justin Iurman | 9ee11f0 | 2021-07-20 21:42:57 +0200 | [diff] [blame] | 941 | /* IOAM */ |
| 942 | |
| 943 | static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff) |
| 944 | { |
| 945 | struct ioam6_trace_hdr *trace; |
| 946 | struct ioam6_namespace *ns; |
| 947 | struct ioam6_hdr *hdr; |
| 948 | |
| 949 | /* Bad alignment (must be 4n-aligned) */ |
| 950 | if (optoff & 3) |
| 951 | goto drop; |
| 952 | |
| 953 | /* Ignore if IOAM is not enabled on ingress */ |
| 954 | if (!__in6_dev_get(skb->dev)->cnf.ioam6_enabled) |
| 955 | goto ignore; |
| 956 | |
| 957 | /* Truncated Option header */ |
| 958 | hdr = (struct ioam6_hdr *)(skb_network_header(skb) + optoff); |
| 959 | if (hdr->opt_len < 2) |
| 960 | goto drop; |
| 961 | |
| 962 | switch (hdr->type) { |
| 963 | case IOAM6_TYPE_PREALLOC: |
| 964 | /* Truncated Pre-allocated Trace header */ |
| 965 | if (hdr->opt_len < 2 + sizeof(*trace)) |
| 966 | goto drop; |
| 967 | |
| 968 | /* Malformed Pre-allocated Trace header */ |
| 969 | trace = (struct ioam6_trace_hdr *)((u8 *)hdr + sizeof(*hdr)); |
| 970 | if (hdr->opt_len < 2 + sizeof(*trace) + trace->remlen * 4) |
| 971 | goto drop; |
| 972 | |
| 973 | /* Ignore if the IOAM namespace is unknown */ |
| 974 | ns = ioam6_namespace(ipv6_skb_net(skb), trace->namespace_id); |
| 975 | if (!ns) |
| 976 | goto ignore; |
| 977 | |
| 978 | if (!skb_valid_dst(skb)) |
| 979 | ip6_route_input(skb); |
| 980 | |
Justin Iurman | 52d03786 | 2021-10-03 20:45:36 +0200 | [diff] [blame] | 981 | ioam6_fill_trace_data(skb, ns, trace, true); |
Justin Iurman | 9ee11f0 | 2021-07-20 21:42:57 +0200 | [diff] [blame] | 982 | break; |
| 983 | default: |
| 984 | break; |
| 985 | } |
| 986 | |
| 987 | ignore: |
| 988 | return true; |
| 989 | |
| 990 | drop: |
| 991 | kfree_skb(skb); |
| 992 | return false; |
| 993 | } |
| 994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | /* Jumbo payload */ |
| 996 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 997 | static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 999 | const unsigned char *nh = skb_network_header(skb); |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 1000 | struct inet6_dev *idev = __in6_dev_get_safely(skb->dev); |
David S. Miller | 2570a4f | 2010-01-13 17:27:37 -0800 | [diff] [blame] | 1001 | struct net *net = ipv6_skb_net(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | u32 pkt_len; |
| 1003 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 1004 | if (nh[optoff + 1] != 4 || (optoff & 3) != 2) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 1005 | net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n", |
| 1006 | nh[optoff+1]); |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 1007 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | goto drop; |
| 1009 | } |
| 1010 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 1011 | pkt_len = ntohl(*(__be32 *)(nh + optoff + 2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | if (pkt_len <= IPV6_MAXPLEN) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 1013 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1015 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | } |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1017 | if (ipv6_hdr(skb)->payload_len) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 1018 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1020 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | if (pkt_len > skb->len - sizeof(struct ipv6hdr)) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 1024 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INTRUNCATEDPKTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | goto drop; |
| 1026 | } |
Stephen Hemminger | 42ca89c | 2005-09-08 12:57:43 -0700 | [diff] [blame] | 1027 | |
| 1028 | if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) |
| 1029 | goto drop; |
| 1030 | |
Paolo Abeni | cb891fa | 2017-07-31 16:52:36 +0200 | [diff] [blame] | 1031 | IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM; |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1032 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
| 1034 | drop: |
| 1035 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1036 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Huw Davies | 2e532b7 | 2016-06-27 15:06:17 -0400 | [diff] [blame] | 1039 | /* CALIPSO RFC 5570 */ |
| 1040 | |
| 1041 | static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff) |
| 1042 | { |
| 1043 | const unsigned char *nh = skb_network_header(skb); |
| 1044 | |
| 1045 | if (nh[optoff + 1] < 8) |
| 1046 | goto drop; |
| 1047 | |
| 1048 | if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1]) |
| 1049 | goto drop; |
| 1050 | |
| 1051 | if (!calipso_validate(skb, nh + optoff)) |
| 1052 | goto drop; |
| 1053 | |
| 1054 | return true; |
| 1055 | |
| 1056 | drop: |
| 1057 | kfree_skb(skb); |
| 1058 | return false; |
| 1059 | } |
| 1060 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 1061 | int ipv6_parse_hopopts(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | { |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 1063 | struct inet6_skb_parm *opt = IP6CB(skb); |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 1064 | struct net *net = dev_net(skb->dev); |
| 1065 | int extlen; |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 1066 | |
YOSHIFUJI Hideaki | ec67009 | 2006-04-18 14:46:26 -0700 | [diff] [blame] | 1067 | /* |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 1068 | * skb_network_header(skb) is equal to skb->data, and |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 1069 | * skb_network_header_len(skb) is always equal to |
YOSHIFUJI Hideaki | ec67009 | 2006-04-18 14:46:26 -0700 | [diff] [blame] | 1070 | * sizeof(struct ipv6hdr) by definition of |
| 1071 | * hop-by-hop options. |
| 1072 | */ |
| 1073 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) || |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1074 | !pskb_may_pull(skb, (sizeof(struct ipv6hdr) + |
| 1075 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 1076 | fail_and_free: |
YOSHIFUJI Hideaki | ec67009 | 2006-04-18 14:46:26 -0700 | [diff] [blame] | 1077 | kfree_skb(skb); |
| 1078 | return -1; |
| 1079 | } |
| 1080 | |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 1081 | extlen = (skb_transport_header(skb)[1] + 1) << 3; |
| 1082 | if (extlen > net->ipv6.sysctl.max_hbh_opts_len) |
| 1083 | goto fail_and_free; |
| 1084 | |
Florian Westphal | 8b58a39 | 2015-07-08 23:32:12 +0200 | [diff] [blame] | 1085 | opt->flags |= IP6SKB_HOPBYHOP; |
Eric Dumazet | 51b8f81 | 2021-08-03 08:31:05 -0700 | [diff] [blame] | 1086 | if (ip6_parse_tlv(true, skb, net->ipv6.sysctl.max_hbh_opts_cnt)) { |
Tom Herbert | 47d3d7a | 2017-10-30 14:16:00 -0700 | [diff] [blame] | 1087 | skb->transport_header += extlen; |
Masahide NAKAMURA | dc435e6 | 2006-08-31 15:18:49 -0700 | [diff] [blame] | 1088 | opt = IP6CB(skb); |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 1089 | opt->nhoff = sizeof(struct ipv6hdr); |
YOSHIFUJI Hideaki | b809739 | 2006-04-18 14:48:45 -0700 | [diff] [blame] | 1090 | return 1; |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 1091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | return -1; |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * Creating outbound headers. |
| 1097 | * |
| 1098 | * "build" functions work when skb is filled from head to tail (datagram) |
| 1099 | * "push" functions work when headers are added from tail to head (tcp) |
| 1100 | * |
| 1101 | * In both cases we assume, that caller reserved enough room |
| 1102 | * for headers. |
| 1103 | */ |
| 1104 | |
David Lebrun | a149e7c | 2016-11-08 14:59:21 +0100 | [diff] [blame] | 1105 | static void ipv6_push_rthdr0(struct sk_buff *skb, u8 *proto, |
| 1106 | struct ipv6_rt_hdr *opt, |
| 1107 | struct in6_addr **addr_p, struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | { |
| 1109 | struct rt0_hdr *phdr, *ihdr; |
| 1110 | int hops; |
| 1111 | |
| 1112 | ihdr = (struct rt0_hdr *) opt; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1113 | |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 1114 | phdr = skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | memcpy(phdr, ihdr, sizeof(struct rt0_hdr)); |
| 1116 | |
| 1117 | hops = ihdr->rt_hdr.hdrlen >> 1; |
| 1118 | |
| 1119 | if (hops > 1) |
| 1120 | memcpy(phdr->addr, ihdr->addr + 1, |
| 1121 | (hops - 1) * sizeof(struct in6_addr)); |
| 1122 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1123 | phdr->addr[hops - 1] = **addr_p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | *addr_p = ihdr->addr; |
| 1125 | |
| 1126 | phdr->rt_hdr.nexthdr = *proto; |
| 1127 | *proto = NEXTHDR_ROUTING; |
| 1128 | } |
| 1129 | |
David Lebrun | a149e7c | 2016-11-08 14:59:21 +0100 | [diff] [blame] | 1130 | static void ipv6_push_rthdr4(struct sk_buff *skb, u8 *proto, |
| 1131 | struct ipv6_rt_hdr *opt, |
| 1132 | struct in6_addr **addr_p, struct in6_addr *saddr) |
| 1133 | { |
| 1134 | struct ipv6_sr_hdr *sr_phdr, *sr_ihdr; |
| 1135 | int plen, hops; |
| 1136 | |
| 1137 | sr_ihdr = (struct ipv6_sr_hdr *)opt; |
| 1138 | plen = (sr_ihdr->hdrlen + 1) << 3; |
| 1139 | |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 1140 | sr_phdr = skb_push(skb, plen); |
David Lebrun | a149e7c | 2016-11-08 14:59:21 +0100 | [diff] [blame] | 1141 | memcpy(sr_phdr, sr_ihdr, sizeof(struct ipv6_sr_hdr)); |
| 1142 | |
| 1143 | hops = sr_ihdr->first_segment + 1; |
| 1144 | memcpy(sr_phdr->segments + 1, sr_ihdr->segments + 1, |
| 1145 | (hops - 1) * sizeof(struct in6_addr)); |
| 1146 | |
| 1147 | sr_phdr->segments[0] = **addr_p; |
David Lebrun | 925615c | 2017-08-05 12:38:24 +0200 | [diff] [blame] | 1148 | *addr_p = &sr_ihdr->segments[sr_ihdr->segments_left]; |
David Lebrun | a149e7c | 2016-11-08 14:59:21 +0100 | [diff] [blame] | 1149 | |
Mathieu Xhonneux | ccc12b1 | 2018-01-10 13:35:49 +0000 | [diff] [blame] | 1150 | if (sr_ihdr->hdrlen > hops * 2) { |
| 1151 | int tlvs_offset, tlvs_length; |
| 1152 | |
| 1153 | tlvs_offset = (1 + hops * 2) << 3; |
| 1154 | tlvs_length = (sr_ihdr->hdrlen - hops * 2) << 3; |
| 1155 | memcpy((char *)sr_phdr + tlvs_offset, |
| 1156 | (char *)sr_ihdr + tlvs_offset, tlvs_length); |
| 1157 | } |
| 1158 | |
David Lebrun | a149e7c | 2016-11-08 14:59:21 +0100 | [diff] [blame] | 1159 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 1160 | if (sr_has_hmac(sr_phdr)) { |
| 1161 | struct net *net = NULL; |
| 1162 | |
| 1163 | if (skb->dev) |
| 1164 | net = dev_net(skb->dev); |
| 1165 | else if (skb->sk) |
| 1166 | net = sock_net(skb->sk); |
| 1167 | |
| 1168 | WARN_ON(!net); |
| 1169 | |
| 1170 | if (net) |
| 1171 | seg6_push_hmac(net, saddr, sr_phdr); |
| 1172 | } |
| 1173 | #endif |
| 1174 | |
| 1175 | sr_phdr->nexthdr = *proto; |
| 1176 | *proto = NEXTHDR_ROUTING; |
| 1177 | } |
| 1178 | |
| 1179 | static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto, |
| 1180 | struct ipv6_rt_hdr *opt, |
| 1181 | struct in6_addr **addr_p, struct in6_addr *saddr) |
| 1182 | { |
| 1183 | switch (opt->type) { |
| 1184 | case IPV6_SRCRT_TYPE_0: |
Sabrina Dubroca | ec9c421 | 2017-04-25 15:56:50 +0200 | [diff] [blame] | 1185 | case IPV6_SRCRT_STRICT: |
| 1186 | case IPV6_SRCRT_TYPE_2: |
David Lebrun | a149e7c | 2016-11-08 14:59:21 +0100 | [diff] [blame] | 1187 | ipv6_push_rthdr0(skb, proto, opt, addr_p, saddr); |
| 1188 | break; |
| 1189 | case IPV6_SRCRT_TYPE_4: |
| 1190 | ipv6_push_rthdr4(skb, proto, opt, addr_p, saddr); |
| 1191 | break; |
| 1192 | default: |
| 1193 | break; |
| 1194 | } |
| 1195 | } |
| 1196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt) |
| 1198 | { |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 1199 | struct ipv6_opt_hdr *h = skb_push(skb, ipv6_optlen(opt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | |
| 1201 | memcpy(h, opt, ipv6_optlen(opt)); |
| 1202 | h->nexthdr = *proto; |
| 1203 | *proto = type; |
| 1204 | } |
| 1205 | |
| 1206 | void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, |
| 1207 | u8 *proto, |
David Lebrun | 613fa3c | 2016-11-08 14:59:20 +0100 | [diff] [blame] | 1208 | struct in6_addr **daddr, struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | { |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1210 | if (opt->srcrt) { |
David Lebrun | 613fa3c | 2016-11-08 14:59:20 +0100 | [diff] [blame] | 1211 | ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1212 | /* |
| 1213 | * IPV6_RTHDRDSTOPTS is ignored |
| 1214 | * unless IPV6_RTHDR is set (RFC3542). |
| 1215 | */ |
| 1216 | if (opt->dst0opt) |
| 1217 | ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt); |
| 1218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | if (opt->hopopt) |
| 1220 | ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt); |
| 1221 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto) |
| 1224 | { |
| 1225 | if (opt->dst1opt) |
| 1226 | ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt); |
| 1227 | } |
David S. Miller | 5b8481f | 2017-05-01 15:10:20 -0400 | [diff] [blame] | 1228 | EXPORT_SYMBOL(ipv6_push_frag_opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | |
| 1230 | struct ipv6_txoptions * |
| 1231 | ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt) |
| 1232 | { |
| 1233 | struct ipv6_txoptions *opt2; |
| 1234 | |
| 1235 | opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC); |
| 1236 | if (opt2) { |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 1237 | long dif = (char *)opt2 - (char *)opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | memcpy(opt2, opt, opt->tot_len); |
| 1239 | if (opt2->hopopt) |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 1240 | *((char **)&opt2->hopopt) += dif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | if (opt2->dst0opt) |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 1242 | *((char **)&opt2->dst0opt) += dif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | if (opt2->dst1opt) |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 1244 | *((char **)&opt2->dst1opt) += dif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | if (opt2->srcrt) |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 1246 | *((char **)&opt2->srcrt) += dif; |
Reshetova, Elena | 0aeea21 | 2017-07-04 09:34:54 +0300 | [diff] [blame] | 1247 | refcount_set(&opt2->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | } |
| 1249 | return opt2; |
| 1250 | } |
Arnaldo Carvalho de Melo | 3cf3dc6 | 2005-12-13 23:23:20 -0800 | [diff] [blame] | 1251 | EXPORT_SYMBOL_GPL(ipv6_dup_options); |
| 1252 | |
Paul Moore | a9ba23d | 2018-07-04 09:58:05 -0400 | [diff] [blame] | 1253 | static void ipv6_renew_option(int renewtype, |
| 1254 | struct ipv6_opt_hdr **dest, |
| 1255 | struct ipv6_opt_hdr *old, |
| 1256 | struct ipv6_opt_hdr *new, |
| 1257 | int newtype, char **p) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1258 | { |
Paul Moore | a9ba23d | 2018-07-04 09:58:05 -0400 | [diff] [blame] | 1259 | struct ipv6_opt_hdr *src; |
| 1260 | |
| 1261 | src = (renewtype == newtype ? new : old); |
| 1262 | if (!src) |
| 1263 | return; |
| 1264 | |
| 1265 | memcpy(*p, src, ipv6_optlen(src)); |
| 1266 | *dest = (struct ipv6_opt_hdr *)*p; |
| 1267 | *p += CMSG_ALIGN(ipv6_optlen(*dest)); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1268 | } |
| 1269 | |
Huw Davies | e67ae21 | 2016-06-27 15:02:50 -0400 | [diff] [blame] | 1270 | /** |
| 1271 | * ipv6_renew_options - replace a specific ext hdr with a new one. |
| 1272 | * |
| 1273 | * @sk: sock from which to allocate memory |
| 1274 | * @opt: original options |
| 1275 | * @newtype: option type to replace in @opt |
| 1276 | * @newopt: new option of type @newtype to replace (user-mem) |
Huw Davies | e67ae21 | 2016-06-27 15:02:50 -0400 | [diff] [blame] | 1277 | * |
| 1278 | * Returns a new set of options which is a copy of @opt with the |
| 1279 | * option type @newtype replaced with @newopt. |
| 1280 | * |
| 1281 | * @opt may be NULL, in which case a new set of options is returned |
| 1282 | * containing just @newopt. |
| 1283 | * |
| 1284 | * @newopt may be NULL, in which case the specified option type is |
| 1285 | * not copied into the new set of options. |
| 1286 | * |
| 1287 | * The new set of options is allocated from the socket option memory |
| 1288 | * buffer of @sk. |
| 1289 | */ |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1290 | struct ipv6_txoptions * |
| 1291 | ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt, |
Paul Moore | a9ba23d | 2018-07-04 09:58:05 -0400 | [diff] [blame] | 1292 | int newtype, struct ipv6_opt_hdr *newopt) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1293 | { |
| 1294 | int tot_len = 0; |
| 1295 | char *p; |
| 1296 | struct ipv6_txoptions *opt2; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1297 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 1298 | if (opt) { |
| 1299 | if (newtype != IPV6_HOPOPTS && opt->hopopt) |
| 1300 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt)); |
| 1301 | if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt) |
| 1302 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt)); |
| 1303 | if (newtype != IPV6_RTHDR && opt->srcrt) |
| 1304 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt)); |
| 1305 | if (newtype != IPV6_DSTOPTS && opt->dst1opt) |
| 1306 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt)); |
| 1307 | } |
| 1308 | |
Paul Moore | a9ba23d | 2018-07-04 09:58:05 -0400 | [diff] [blame] | 1309 | if (newopt) |
| 1310 | tot_len += CMSG_ALIGN(ipv6_optlen(newopt)); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1311 | |
| 1312 | if (!tot_len) |
| 1313 | return NULL; |
| 1314 | |
YOSHIFUJI Hideaki | 8b8aa4b | 2005-11-20 12:18:17 +0900 | [diff] [blame] | 1315 | tot_len += sizeof(*opt2); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1316 | opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC); |
| 1317 | if (!opt2) |
| 1318 | return ERR_PTR(-ENOBUFS); |
| 1319 | |
| 1320 | memset(opt2, 0, tot_len); |
Reshetova, Elena | 0aeea21 | 2017-07-04 09:34:54 +0300 | [diff] [blame] | 1321 | refcount_set(&opt2->refcnt, 1); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1322 | opt2->tot_len = tot_len; |
| 1323 | p = (char *)(opt2 + 1); |
| 1324 | |
Paul Moore | a9ba23d | 2018-07-04 09:58:05 -0400 | [diff] [blame] | 1325 | ipv6_renew_option(IPV6_HOPOPTS, &opt2->hopopt, |
| 1326 | (opt ? opt->hopopt : NULL), |
| 1327 | newopt, newtype, &p); |
| 1328 | ipv6_renew_option(IPV6_RTHDRDSTOPTS, &opt2->dst0opt, |
| 1329 | (opt ? opt->dst0opt : NULL), |
| 1330 | newopt, newtype, &p); |
| 1331 | ipv6_renew_option(IPV6_RTHDR, |
| 1332 | (struct ipv6_opt_hdr **)&opt2->srcrt, |
| 1333 | (opt ? (struct ipv6_opt_hdr *)opt->srcrt : NULL), |
| 1334 | newopt, newtype, &p); |
| 1335 | ipv6_renew_option(IPV6_DSTOPTS, &opt2->dst1opt, |
| 1336 | (opt ? opt->dst1opt : NULL), |
| 1337 | newopt, newtype, &p); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1338 | |
| 1339 | opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) + |
| 1340 | (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) + |
| 1341 | (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0); |
| 1342 | opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0); |
| 1343 | |
| 1344 | return opt2; |
Huw Davies | e67ae21 | 2016-06-27 15:02:50 -0400 | [diff] [blame] | 1345 | } |
| 1346 | |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 1347 | struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space, |
| 1348 | struct ipv6_txoptions *opt) |
| 1349 | { |
| 1350 | /* |
| 1351 | * ignore the dest before srcrt unless srcrt is being included. |
| 1352 | * --yoshfuji |
| 1353 | */ |
| 1354 | if (opt && opt->dst0opt && !opt->srcrt) { |
| 1355 | if (opt_space != opt) { |
| 1356 | memcpy(opt_space, opt, sizeof(*opt_space)); |
| 1357 | opt = opt_space; |
| 1358 | } |
| 1359 | opt->opt_nflen -= ipv6_optlen(opt->dst0opt); |
| 1360 | opt->dst0opt = NULL; |
| 1361 | } |
| 1362 | |
| 1363 | return opt; |
| 1364 | } |
Chris Elston | a495f83 | 2012-04-29 21:48:53 +0000 | [diff] [blame] | 1365 | EXPORT_SYMBOL_GPL(ipv6_fixup_options); |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 1366 | |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1367 | /** |
| 1368 | * fl6_update_dst - update flowi destination address with info given |
| 1369 | * by srcrt option, if any. |
| 1370 | * |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1371 | * @fl6: flowi6 for which daddr is to be updated |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1372 | * @opt: struct ipv6_txoptions in which to look for srcrt opt |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1373 | * @orig: copy of original daddr address if modified |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1374 | * |
| 1375 | * Returns NULL if no txoptions or no srcrt, otherwise returns orig |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1376 | * and initial value of fl6->daddr set in orig |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1377 | */ |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1378 | struct in6_addr *fl6_update_dst(struct flowi6 *fl6, |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1379 | const struct ipv6_txoptions *opt, |
| 1380 | struct in6_addr *orig) |
| 1381 | { |
| 1382 | if (!opt || !opt->srcrt) |
| 1383 | return NULL; |
| 1384 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1385 | *orig = fl6->daddr; |
David Lebrun | a149e7c | 2016-11-08 14:59:21 +0100 | [diff] [blame] | 1386 | |
| 1387 | switch (opt->srcrt->type) { |
| 1388 | case IPV6_SRCRT_TYPE_0: |
Sabrina Dubroca | ec9c421 | 2017-04-25 15:56:50 +0200 | [diff] [blame] | 1389 | case IPV6_SRCRT_STRICT: |
| 1390 | case IPV6_SRCRT_TYPE_2: |
David Lebrun | a149e7c | 2016-11-08 14:59:21 +0100 | [diff] [blame] | 1391 | fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr; |
| 1392 | break; |
| 1393 | case IPV6_SRCRT_TYPE_4: |
| 1394 | { |
| 1395 | struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)opt->srcrt; |
| 1396 | |
David Lebrun | 925615c | 2017-08-05 12:38:24 +0200 | [diff] [blame] | 1397 | fl6->daddr = srh->segments[srh->segments_left]; |
David Lebrun | a149e7c | 2016-11-08 14:59:21 +0100 | [diff] [blame] | 1398 | break; |
| 1399 | } |
| 1400 | default: |
| 1401 | return NULL; |
| 1402 | } |
| 1403 | |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1404 | return orig; |
| 1405 | } |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1406 | EXPORT_SYMBOL_GPL(fl6_update_dst); |