Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Extension Header handling for IPv6 |
| 3 | * Linux INET6 implementation |
| 4 | * |
| 5 | * Authors: |
| 6 | * Pedro Roque <roque@di.fc.ul.pt> |
| 7 | * Andi Kleen <ak@muc.de> |
| 8 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
| 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * as published by the Free Software Foundation; either version |
| 13 | * 2 of the License, or (at your option) any later version. |
| 14 | */ |
| 15 | |
| 16 | /* Changes: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 17 | * yoshfuji : ensure not to overrun while parsing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * tlv options. |
| 19 | * Mitsuru KANDA @USAGI and: Remove ipv6_parse_exthdrs(). |
| 20 | * YOSHIFUJI Hideaki @USAGI Register inbound extension header |
| 21 | * handlers as inet6_protocol{}. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/socket.h> |
| 27 | #include <linux/sockios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/net.h> |
| 29 | #include <linux/netdevice.h> |
| 30 | #include <linux/in6.h> |
| 31 | #include <linux/icmpv6.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 33 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Herbert Xu | 352e512 | 2007-11-13 21:34:06 -0800 | [diff] [blame] | 35 | #include <net/dst.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <net/sock.h> |
| 37 | #include <net/snmp.h> |
| 38 | |
| 39 | #include <net/ipv6.h> |
| 40 | #include <net/protocol.h> |
| 41 | #include <net/transp_v6.h> |
| 42 | #include <net/rawv6.h> |
| 43 | #include <net/ndisc.h> |
| 44 | #include <net/ip6_route.h> |
| 45 | #include <net/addrconf.h> |
Huw Davies | 2e532b7 | 2016-06-27 15:06:17 -0400 | [diff] [blame] | 46 | #include <net/calipso.h> |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 47 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 48 | #include <net/xfrm.h> |
| 49 | #endif |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 50 | #include <linux/seg6.h> |
| 51 | #include <net/seg6.h> |
David Lebrun | 9baee83 | 2016-11-08 14:59:19 +0100 | [diff] [blame] | 52 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 53 | #include <net/seg6_hmac.h> |
| 54 | #endif |
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 | |
| 58 | /* |
| 59 | * Parsing tlv encoded headers. |
| 60 | * |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 61 | * Parsing function "func" returns true, if parsing succeed |
| 62 | * and false, if it failed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * It MUST NOT touch skb->h. |
| 64 | */ |
| 65 | |
| 66 | struct tlvtype_proc { |
| 67 | int type; |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 68 | bool (*func)(struct sk_buff *skb, int offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /********************* |
| 72 | Generic functions |
| 73 | *********************/ |
| 74 | |
| 75 | /* An unknown option is detected, decide what to do */ |
| 76 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 77 | static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 79 | switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | case 0: /* ignore */ |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 81 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 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 Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 90 | if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | break; |
| 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 | |
| 97 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 98 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* Parse tlv encoded option header (hop-by-hop or destination) */ |
| 102 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 103 | static bool ip6_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 105 | const struct tlvtype_proc *curr; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 106 | const unsigned char *nh = skb_network_header(skb); |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 107 | int off = skb_network_header_len(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 108 | int len = (skb_transport_header(skb)[1] + 1) << 3; |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 109 | int padlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 111 | if (skb_transport_offset(skb) + len > skb_headlen(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | goto bad; |
| 113 | |
| 114 | off += 2; |
| 115 | len -= 2; |
| 116 | |
| 117 | while (len > 0) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 118 | int optlen = nh[off + 1] + 2; |
Eldad Zack | c1412fc | 2012-04-12 17:36:17 -0400 | [diff] [blame] | 119 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 121 | switch (nh[off]) { |
Eldad Zack | 1de5a71 | 2012-05-17 06:00:25 +0000 | [diff] [blame] | 122 | case IPV6_TLV_PAD1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | optlen = 1; |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 124 | padlen++; |
| 125 | if (padlen > 7) |
| 126 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | break; |
| 128 | |
| 129 | case IPV6_TLV_PADN: |
Eldad Zack | c1412fc | 2012-04-12 17:36:17 -0400 | [diff] [blame] | 130 | /* RFC 2460 states that the purpose of PadN is |
| 131 | * to align the containing header to multiples |
| 132 | * of 8. 7 is therefore the highest valid value. |
| 133 | * See also RFC 4942, Section 2.1.9.5. |
| 134 | */ |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 135 | padlen += optlen; |
| 136 | if (padlen > 7) |
Eldad Zack | c1412fc | 2012-04-12 17:36:17 -0400 | [diff] [blame] | 137 | goto bad; |
| 138 | /* RFC 4942 recommends receiving hosts to |
| 139 | * actively check PadN payload to contain |
| 140 | * only zeroes. |
| 141 | */ |
| 142 | for (i = 2; i < optlen; i++) { |
| 143 | if (nh[off + i] != 0) |
| 144 | goto bad; |
| 145 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | break; |
| 147 | |
| 148 | default: /* Other TLV code so scan list */ |
| 149 | if (optlen > len) |
| 150 | goto bad; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 151 | for (curr = procs; curr->type >= 0; curr++) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 152 | if (curr->type == nh[off]) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 153 | /* type specific length/alignment |
| 154 | checks will be performed in the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | func(). */ |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 156 | if (curr->func(skb, off) == false) |
| 157 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | break; |
| 159 | } |
| 160 | } |
| 161 | if (curr->type < 0) { |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 162 | if (ip6_tlvopt_unknown(skb, off) == 0) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 163 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 165 | padlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | off += optlen; |
| 169 | len -= optlen; |
| 170 | } |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | if (len == 0) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 173 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | bad: |
| 175 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 176 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /***************************** |
| 180 | Destination options header. |
| 181 | *****************************/ |
| 182 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 183 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 184 | static bool ipv6_dest_hao(struct sk_buff *skb, int optoff) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 185 | { |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 186 | struct ipv6_destopt_hao *hao; |
| 187 | struct inet6_skb_parm *opt = IP6CB(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 188 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 189 | struct in6_addr tmp_addr; |
| 190 | int ret; |
| 191 | |
| 192 | if (opt->dsthao) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 193 | net_dbg_ratelimited("hao duplicated\n"); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 194 | goto discard; |
| 195 | } |
| 196 | opt->dsthao = opt->dst1; |
| 197 | opt->dst1 = 0; |
| 198 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 199 | hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 200 | |
| 201 | if (hao->length != 16) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 202 | net_dbg_ratelimited("hao invalid option length = %d\n", |
| 203 | hao->length); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 204 | goto discard; |
| 205 | } |
| 206 | |
| 207 | if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 208 | net_dbg_ratelimited("hao is not an unicast addr: %pI6\n", |
| 209 | &hao->addr); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 210 | 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 Xu | 65c8846 | 2007-10-15 01:29:10 -0700 | [diff] [blame] | 219 | if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 220 | goto discard; |
| 221 | |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 222 | /* update all variable using below by copied skbuff */ |
Herbert Xu | 65c8846 | 2007-10-15 01:29:10 -0700 | [diff] [blame] | 223 | hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 224 | optoff); |
Herbert Xu | 65c8846 | 2007-10-15 01:29:10 -0700 | [diff] [blame] | 225 | ipv6h = ipv6_hdr(skb); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 229 | skb->ip_summed = CHECKSUM_NONE; |
| 230 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 231 | tmp_addr = ipv6h->saddr; |
| 232 | ipv6h->saddr = hao->addr; |
| 233 | hao->addr = tmp_addr; |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 234 | |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 235 | if (skb->tstamp.tv64 == 0) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 236 | __net_timestamp(skb); |
| 237 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 238 | return true; |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 239 | |
| 240 | discard: |
| 241 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 242 | return false; |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 243 | } |
| 244 | #endif |
| 245 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 246 | static const struct tlvtype_proc tlvprocdestopt_lst[] = { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 247 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 248 | { |
| 249 | .type = IPV6_TLV_HAO, |
| 250 | .func = ipv6_dest_hao, |
| 251 | }, |
| 252 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | {-1, NULL} |
| 254 | }; |
| 255 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 256 | static int ipv6_destopt_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | struct inet6_skb_parm *opt = IP6CB(skb); |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 259 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 260 | __u16 dstbuf; |
| 261 | #endif |
Eric Dumazet | 897dc80 | 2011-07-28 04:00:35 +0000 | [diff] [blame] | 262 | struct dst_entry *dst = skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 264 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) || |
| 265 | !pskb_may_pull(skb, (skb_transport_offset(skb) + |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 266 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 267 | __IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), |
| 268 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | kfree_skb(skb); |
| 270 | return -1; |
| 271 | } |
| 272 | |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 273 | opt->lastopt = opt->dst1 = skb_network_header_len(skb); |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 274 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 275 | dstbuf = opt->dst1; |
| 276 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 278 | if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) { |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 279 | skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3; |
Masahide NAKAMURA | dc435e6 | 2006-08-31 15:18:49 -0700 | [diff] [blame] | 280 | opt = IP6CB(skb); |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 281 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 282 | opt->nhoff = dstbuf; |
| 283 | #else |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 284 | opt->nhoff = opt->dst1; |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 285 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | return 1; |
| 287 | } |
| 288 | |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 289 | __IP6_INC_STATS(dev_net(dst->dev), |
| 290 | ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | return -1; |
| 292 | } |
| 293 | |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 294 | static void seg6_update_csum(struct sk_buff *skb) |
| 295 | { |
| 296 | struct ipv6_sr_hdr *hdr; |
| 297 | struct in6_addr *addr; |
| 298 | __be32 from, to; |
| 299 | |
| 300 | /* srh is at transport offset and seg_left is already decremented |
| 301 | * but daddr is not yet updated with next segment |
| 302 | */ |
| 303 | |
| 304 | hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb); |
| 305 | addr = hdr->segments + hdr->segments_left; |
| 306 | |
| 307 | hdr->segments_left++; |
| 308 | from = *(__be32 *)hdr; |
| 309 | |
| 310 | hdr->segments_left--; |
| 311 | to = *(__be32 *)hdr; |
| 312 | |
| 313 | /* update skb csum with diff resulting from seg_left decrement */ |
| 314 | |
| 315 | update_csum_diff4(skb, from, to); |
| 316 | |
| 317 | /* compute csum diff between current and next segment and update */ |
| 318 | |
| 319 | update_csum_diff16(skb, (__be32 *)(&ipv6_hdr(skb)->daddr), |
| 320 | (__be32 *)addr); |
| 321 | } |
| 322 | |
| 323 | static int ipv6_srh_rcv(struct sk_buff *skb) |
| 324 | { |
| 325 | struct inet6_skb_parm *opt = IP6CB(skb); |
| 326 | struct net *net = dev_net(skb->dev); |
| 327 | struct ipv6_sr_hdr *hdr; |
| 328 | struct inet6_dev *idev; |
| 329 | struct in6_addr *addr; |
| 330 | bool cleanup = false; |
| 331 | int accept_seg6; |
| 332 | |
| 333 | hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb); |
| 334 | |
| 335 | idev = __in6_dev_get(skb->dev); |
| 336 | |
| 337 | accept_seg6 = net->ipv6.devconf_all->seg6_enabled; |
| 338 | if (accept_seg6 > idev->cnf.seg6_enabled) |
| 339 | accept_seg6 = idev->cnf.seg6_enabled; |
| 340 | |
| 341 | if (!accept_seg6) { |
| 342 | kfree_skb(skb); |
| 343 | return -1; |
| 344 | } |
| 345 | |
David Lebrun | 9baee83 | 2016-11-08 14:59:19 +0100 | [diff] [blame] | 346 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 347 | if (!seg6_hmac_validate_skb(skb)) { |
| 348 | kfree_skb(skb); |
| 349 | return -1; |
| 350 | } |
| 351 | #endif |
| 352 | |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 353 | looped_back: |
| 354 | if (hdr->segments_left > 0) { |
| 355 | if (hdr->nexthdr != NEXTHDR_IPV6 && hdr->segments_left == 1 && |
| 356 | sr_has_cleanup(hdr)) |
| 357 | cleanup = true; |
| 358 | } else { |
| 359 | if (hdr->nexthdr == NEXTHDR_IPV6) { |
| 360 | int offset = (hdr->hdrlen + 1) << 3; |
| 361 | |
| 362 | skb_postpull_rcsum(skb, skb_network_header(skb), |
| 363 | skb_network_header_len(skb)); |
| 364 | |
| 365 | if (!pskb_pull(skb, offset)) { |
| 366 | kfree_skb(skb); |
| 367 | return -1; |
| 368 | } |
| 369 | skb_postpull_rcsum(skb, skb_transport_header(skb), |
| 370 | offset); |
| 371 | |
| 372 | skb_reset_network_header(skb); |
| 373 | skb_reset_transport_header(skb); |
| 374 | skb->encapsulation = 0; |
| 375 | |
| 376 | __skb_tunnel_rx(skb, skb->dev, net); |
| 377 | |
| 378 | netif_rx(skb); |
| 379 | return -1; |
| 380 | } |
| 381 | |
| 382 | opt->srcrt = skb_network_header_len(skb); |
| 383 | opt->lastopt = opt->srcrt; |
| 384 | skb->transport_header += (hdr->hdrlen + 1) << 3; |
| 385 | opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb); |
| 386 | |
| 387 | return 1; |
| 388 | } |
| 389 | |
| 390 | if (hdr->segments_left >= (hdr->hdrlen >> 1)) { |
| 391 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 392 | IPSTATS_MIB_INHDRERRORS); |
| 393 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 394 | ((&hdr->segments_left) - |
| 395 | skb_network_header(skb))); |
| 396 | kfree_skb(skb); |
| 397 | return -1; |
| 398 | } |
| 399 | |
| 400 | if (skb_cloned(skb)) { |
| 401 | if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) { |
| 402 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 403 | IPSTATS_MIB_OUTDISCARDS); |
| 404 | kfree_skb(skb); |
| 405 | return -1; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb); |
| 410 | |
| 411 | hdr->segments_left--; |
| 412 | addr = hdr->segments + hdr->segments_left; |
| 413 | |
| 414 | skb_push(skb, sizeof(struct ipv6hdr)); |
| 415 | |
| 416 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 417 | seg6_update_csum(skb); |
| 418 | |
| 419 | ipv6_hdr(skb)->daddr = *addr; |
| 420 | |
| 421 | if (cleanup) { |
| 422 | int srhlen = (hdr->hdrlen + 1) << 3; |
| 423 | int nh = hdr->nexthdr; |
| 424 | |
| 425 | skb_pull_rcsum(skb, sizeof(struct ipv6hdr) + srhlen); |
| 426 | memmove(skb_network_header(skb) + srhlen, |
| 427 | skb_network_header(skb), |
| 428 | (unsigned char *)hdr - skb_network_header(skb)); |
| 429 | skb->network_header += srhlen; |
| 430 | ipv6_hdr(skb)->nexthdr = nh; |
| 431 | ipv6_hdr(skb)->payload_len = htons(skb->len - |
| 432 | sizeof(struct ipv6hdr)); |
| 433 | skb_push_rcsum(skb, sizeof(struct ipv6hdr)); |
| 434 | } |
| 435 | |
| 436 | skb_dst_drop(skb); |
| 437 | |
| 438 | ip6_route_input(skb); |
| 439 | |
| 440 | if (skb_dst(skb)->error) { |
| 441 | dst_input(skb); |
| 442 | return -1; |
| 443 | } |
| 444 | |
| 445 | if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) { |
| 446 | if (ipv6_hdr(skb)->hop_limit <= 1) { |
| 447 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 448 | IPSTATS_MIB_INHDRERRORS); |
| 449 | icmpv6_send(skb, ICMPV6_TIME_EXCEED, |
| 450 | ICMPV6_EXC_HOPLIMIT, 0); |
| 451 | kfree_skb(skb); |
| 452 | return -1; |
| 453 | } |
| 454 | ipv6_hdr(skb)->hop_limit--; |
| 455 | |
| 456 | /* be sure that srh is still present before reinjecting */ |
| 457 | if (!cleanup) { |
| 458 | skb_pull(skb, sizeof(struct ipv6hdr)); |
| 459 | goto looped_back; |
| 460 | } |
| 461 | skb_set_transport_header(skb, sizeof(struct ipv6hdr)); |
| 462 | IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); |
| 463 | } |
| 464 | |
| 465 | dst_input(skb); |
| 466 | |
| 467 | return -1; |
| 468 | } |
| 469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | /******************************** |
| 471 | Routing header. |
| 472 | ********************************/ |
| 473 | |
Eric Dumazet | f6bc7d9 | 2010-06-14 04:39:27 +0000 | [diff] [blame] | 474 | /* called with rcu_read_lock() */ |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 475 | static int ipv6_rthdr_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | struct inet6_skb_parm *opt = IP6CB(skb); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 478 | struct in6_addr *addr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | struct in6_addr daddr; |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 480 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | int n, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | struct ipv6_rt_hdr *hdr; |
| 483 | struct rt0_hdr *rthdr; |
Denis V. Lunev | 483a47d | 2008-10-08 11:09:27 -0700 | [diff] [blame] | 484 | struct net *net = dev_net(skb->dev); |
| 485 | int accept_source_route = net->ipv6.devconf_all->accept_source_route; |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 486 | |
Eric Dumazet | f6bc7d9 | 2010-06-14 04:39:27 +0000 | [diff] [blame] | 487 | idev = __in6_dev_get(skb->dev); |
| 488 | if (idev && accept_source_route > idev->cnf.accept_source_route) |
| 489 | accept_source_route = idev->cnf.accept_source_route; |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 490 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 491 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) || |
| 492 | !pskb_may_pull(skb, (skb_transport_offset(skb) + |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 493 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 494 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 495 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | kfree_skb(skb); |
| 497 | return -1; |
| 498 | } |
| 499 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 500 | hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 502 | if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | skb->pkt_type != PACKET_HOST) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 504 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 505 | IPSTATS_MIB_INADDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | kfree_skb(skb); |
| 507 | return -1; |
| 508 | } |
| 509 | |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 510 | /* segment routing */ |
| 511 | if (hdr->type == IPV6_SRCRT_TYPE_4) |
| 512 | return ipv6_srh_rcv(skb); |
| 513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | looped_back: |
| 515 | if (hdr->segments_left == 0) { |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 516 | switch (hdr->type) { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 517 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 518 | case IPV6_SRCRT_TYPE_2: |
| 519 | /* Silently discard type 2 header unless it was |
| 520 | * processed by own |
| 521 | */ |
| 522 | if (!addr) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 523 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 524 | IPSTATS_MIB_INADDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 525 | kfree_skb(skb); |
| 526 | return -1; |
| 527 | } |
| 528 | break; |
| 529 | #endif |
| 530 | default: |
| 531 | break; |
| 532 | } |
| 533 | |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 534 | opt->lastopt = opt->srcrt = skb_network_header_len(skb); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 535 | skb->transport_header += (hdr->hdrlen + 1) << 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | opt->dst0 = opt->dst1; |
| 537 | opt->dst1 = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 538 | opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | return 1; |
| 540 | } |
| 541 | |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 542 | switch (hdr->type) { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 543 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 544 | case IPV6_SRCRT_TYPE_2: |
YOSHIFUJI Hideaki | c382bb9 | 2007-07-10 22:47:58 -0700 | [diff] [blame] | 545 | if (accept_source_route < 0) |
| 546 | goto unknown_rh; |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 547 | /* Silently discard invalid RTH type 2 */ |
| 548 | if (hdr->hdrlen != 2 || hdr->segments_left != 1) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 549 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 550 | IPSTATS_MIB_INHDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 551 | kfree_skb(skb); |
| 552 | return -1; |
| 553 | } |
| 554 | break; |
| 555 | #endif |
YOSHIFUJI Hideaki | c382bb9 | 2007-07-10 22:47:58 -0700 | [diff] [blame] | 556 | default: |
| 557 | goto unknown_rh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | /* |
| 561 | * This is the routing header forwarding algorithm from |
| 562 | * RFC 2460, page 16. |
| 563 | */ |
| 564 | |
| 565 | n = hdr->hdrlen >> 1; |
| 566 | |
| 567 | if (hdr->segments_left > n) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 568 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 569 | IPSTATS_MIB_INHDRERRORS); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 570 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 571 | ((&hdr->segments_left) - |
| 572 | skb_network_header(skb))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | return -1; |
| 574 | } |
| 575 | |
| 576 | /* We are about to mangle packet header. Be careful! |
| 577 | Do not damage packets queued somewhere. |
| 578 | */ |
| 579 | if (skb_cloned(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | /* the copy is a forwarded packet */ |
Herbert Xu | 65c8846 | 2007-10-15 01:29:10 -0700 | [diff] [blame] | 581 | if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 582 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 583 | IPSTATS_MIB_OUTDISCARDS); |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 584 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | return -1; |
| 586 | } |
Herbert Xu | 65c8846 | 2007-10-15 01:29:10 -0700 | [diff] [blame] | 587 | hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 590 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | skb->ip_summed = CHECKSUM_NONE; |
| 592 | |
| 593 | i = n - --hdr->segments_left; |
| 594 | |
| 595 | rthdr = (struct rt0_hdr *) hdr; |
| 596 | addr = rthdr->addr; |
| 597 | addr += i - 1; |
| 598 | |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 599 | switch (hdr->type) { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 600 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 601 | case IPV6_SRCRT_TYPE_2: |
| 602 | if (xfrm6_input_addr(skb, (xfrm_address_t *)addr, |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 603 | (xfrm_address_t *)&ipv6_hdr(skb)->saddr, |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 604 | IPPROTO_ROUTING) < 0) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 605 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 606 | IPSTATS_MIB_INADDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 607 | kfree_skb(skb); |
| 608 | return -1; |
| 609 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 610 | if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 611 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 612 | IPSTATS_MIB_INADDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 613 | kfree_skb(skb); |
| 614 | return -1; |
| 615 | } |
| 616 | break; |
| 617 | #endif |
| 618 | default: |
| 619 | break; |
| 620 | } |
| 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | if (ipv6_addr_is_multicast(addr)) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 623 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 624 | IPSTATS_MIB_INADDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | kfree_skb(skb); |
| 626 | return -1; |
| 627 | } |
| 628 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 629 | daddr = *addr; |
| 630 | *addr = ipv6_hdr(skb)->daddr; |
| 631 | ipv6_hdr(skb)->daddr = daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 633 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | ip6_route_input(skb); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 635 | if (skb_dst(skb)->error) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 636 | skb_push(skb, skb->data - skb_network_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | dst_input(skb); |
| 638 | return -1; |
| 639 | } |
| 640 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 641 | if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 642 | if (ipv6_hdr(skb)->hop_limit <= 1) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 643 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 644 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 646 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | kfree_skb(skb); |
| 648 | return -1; |
| 649 | } |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 650 | ipv6_hdr(skb)->hop_limit--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | goto looped_back; |
| 652 | } |
| 653 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 654 | skb_push(skb, skb->data - skb_network_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | dst_input(skb); |
| 656 | return -1; |
YOSHIFUJI Hideaki | c382bb9 | 2007-07-10 22:47:58 -0700 | [diff] [blame] | 657 | |
| 658 | unknown_rh: |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 659 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS); |
YOSHIFUJI Hideaki | c382bb9 | 2007-07-10 22:47:58 -0700 | [diff] [blame] | 660 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 661 | (&hdr->type) - skb_network_header(skb)); |
| 662 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 665 | static const struct inet6_protocol rthdr_protocol = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | .handler = ipv6_rthdr_rcv, |
Vlad Yasevich | 2207afc | 2012-11-15 08:49:19 +0000 | [diff] [blame] | 667 | .flags = INET6_PROTO_NOPOLICY, |
Vlad Yasevich | 8ca896c | 2012-11-15 08:49:13 +0000 | [diff] [blame] | 668 | }; |
| 669 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 670 | static const struct inet6_protocol destopt_protocol = { |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 671 | .handler = ipv6_destopt_rcv, |
Vlad Yasevich | 2207afc | 2012-11-15 08:49:19 +0000 | [diff] [blame] | 672 | .flags = INET6_PROTO_NOPOLICY, |
Vlad Yasevich | 8ca896c | 2012-11-15 08:49:13 +0000 | [diff] [blame] | 673 | }; |
| 674 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 675 | static const struct inet6_protocol nodata_protocol = { |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 676 | .handler = dst_discard, |
| 677 | .flags = INET6_PROTO_NOPOLICY, |
| 678 | }; |
| 679 | |
| 680 | int __init ipv6_exthdrs_init(void) |
| 681 | { |
| 682 | int ret; |
| 683 | |
Vlad Yasevich | 3336288 | 2012-11-15 08:49:15 +0000 | [diff] [blame] | 684 | ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING); |
| 685 | if (ret) |
Vlad Yasevich | c6b641a | 2012-11-15 08:49:22 +0000 | [diff] [blame] | 686 | goto out; |
Vlad Yasevich | 3336288 | 2012-11-15 08:49:15 +0000 | [diff] [blame] | 687 | |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 688 | ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS); |
| 689 | if (ret) |
| 690 | goto out_rthdr; |
| 691 | |
| 692 | ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE); |
| 693 | if (ret) |
| 694 | goto out_destopt; |
| 695 | |
| 696 | out: |
| 697 | return ret; |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 698 | out_destopt: |
| 699 | inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS); |
Vlad Yasevich | 3336288 | 2012-11-15 08:49:15 +0000 | [diff] [blame] | 700 | out_rthdr: |
| 701 | inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING); |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 702 | goto out; |
| 703 | }; |
| 704 | |
| 705 | void ipv6_exthdrs_exit(void) |
| 706 | { |
| 707 | inet6_del_protocol(&nodata_protocol, IPPROTO_NONE); |
| 708 | inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS); |
| 709 | inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING); |
| 710 | } |
| 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | /********************************** |
| 713 | Hop-by-hop options. |
| 714 | **********************************/ |
| 715 | |
YOSHIFUJI Hideaki | e76b2b2 | 2007-05-09 14:01:59 -0700 | [diff] [blame] | 716 | /* |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 717 | * 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] | 718 | */ |
| 719 | static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb) |
| 720 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 721 | return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev); |
YOSHIFUJI Hideaki | e76b2b2 | 2007-05-09 14:01:59 -0700 | [diff] [blame] | 722 | } |
| 723 | |
David S. Miller | 2570a4f | 2010-01-13 17:27:37 -0800 | [diff] [blame] | 724 | static inline struct net *ipv6_skb_net(struct sk_buff *skb) |
| 725 | { |
| 726 | return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev); |
| 727 | } |
| 728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | /* Router Alert as of RFC 2711 */ |
| 730 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 731 | static bool ipv6_hop_ra(struct sk_buff *skb, int optoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 733 | const unsigned char *nh = skb_network_header(skb); |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 734 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 735 | if (nh[optoff + 1] == 2) { |
YOSHIFUJI Hideaki / 吉藤英明 | dd3332b | 2013-01-13 05:02:45 +0000 | [diff] [blame] | 736 | IP6CB(skb)->flags |= IP6SKB_ROUTERALERT; |
| 737 | memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra)); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 738 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | } |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 740 | net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n", |
| 741 | nh[optoff + 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 743 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* Jumbo payload */ |
| 747 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 748 | static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 750 | const unsigned char *nh = skb_network_header(skb); |
David S. Miller | 2570a4f | 2010-01-13 17:27:37 -0800 | [diff] [blame] | 751 | struct net *net = ipv6_skb_net(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | u32 pkt_len; |
| 753 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 754 | if (nh[optoff + 1] != 4 || (optoff & 3) != 2) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 755 | net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n", |
| 756 | nh[optoff+1]); |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 757 | __IP6_INC_STATS(net, ipv6_skb_idev(skb), |
| 758 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | goto drop; |
| 760 | } |
| 761 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 762 | pkt_len = ntohl(*(__be32 *)(nh + optoff + 2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | if (pkt_len <= IPV6_MAXPLEN) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 764 | __IP6_INC_STATS(net, ipv6_skb_idev(skb), |
| 765 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 767 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 769 | if (ipv6_hdr(skb)->payload_len) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 770 | __IP6_INC_STATS(net, ipv6_skb_idev(skb), |
| 771 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 773 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | if (pkt_len > skb->len - sizeof(struct ipv6hdr)) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 777 | __IP6_INC_STATS(net, ipv6_skb_idev(skb), |
| 778 | IPSTATS_MIB_INTRUNCATEDPKTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | goto drop; |
| 780 | } |
Stephen Hemminger | 42ca89c | 2005-09-08 12:57:43 -0700 | [diff] [blame] | 781 | |
| 782 | if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) |
| 783 | goto drop; |
| 784 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 785 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
| 787 | drop: |
| 788 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 789 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Huw Davies | 2e532b7 | 2016-06-27 15:06:17 -0400 | [diff] [blame] | 792 | /* CALIPSO RFC 5570 */ |
| 793 | |
| 794 | static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff) |
| 795 | { |
| 796 | const unsigned char *nh = skb_network_header(skb); |
| 797 | |
| 798 | if (nh[optoff + 1] < 8) |
| 799 | goto drop; |
| 800 | |
| 801 | if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1]) |
| 802 | goto drop; |
| 803 | |
| 804 | if (!calipso_validate(skb, nh + optoff)) |
| 805 | goto drop; |
| 806 | |
| 807 | return true; |
| 808 | |
| 809 | drop: |
| 810 | kfree_skb(skb); |
| 811 | return false; |
| 812 | } |
| 813 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 814 | static const struct tlvtype_proc tlvprochopopt_lst[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
| 816 | .type = IPV6_TLV_ROUTERALERT, |
| 817 | .func = ipv6_hop_ra, |
| 818 | }, |
| 819 | { |
| 820 | .type = IPV6_TLV_JUMBO, |
| 821 | .func = ipv6_hop_jumbo, |
| 822 | }, |
Huw Davies | 2e532b7 | 2016-06-27 15:06:17 -0400 | [diff] [blame] | 823 | { |
| 824 | .type = IPV6_TLV_CALIPSO, |
| 825 | .func = ipv6_hop_calipso, |
| 826 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | { -1, } |
| 828 | }; |
| 829 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 830 | int ipv6_parse_hopopts(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | { |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 832 | struct inet6_skb_parm *opt = IP6CB(skb); |
| 833 | |
YOSHIFUJI Hideaki | ec67009 | 2006-04-18 14:46:26 -0700 | [diff] [blame] | 834 | /* |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 835 | * skb_network_header(skb) is equal to skb->data, and |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 836 | * skb_network_header_len(skb) is always equal to |
YOSHIFUJI Hideaki | ec67009 | 2006-04-18 14:46:26 -0700 | [diff] [blame] | 837 | * sizeof(struct ipv6hdr) by definition of |
| 838 | * hop-by-hop options. |
| 839 | */ |
| 840 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) || |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 841 | !pskb_may_pull(skb, (sizeof(struct ipv6hdr) + |
| 842 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
YOSHIFUJI Hideaki | ec67009 | 2006-04-18 14:46:26 -0700 | [diff] [blame] | 843 | kfree_skb(skb); |
| 844 | return -1; |
| 845 | } |
| 846 | |
Florian Westphal | 8b58a39 | 2015-07-08 23:32:12 +0200 | [diff] [blame] | 847 | opt->flags |= IP6SKB_HOPBYHOP; |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 848 | if (ip6_parse_tlv(tlvprochopopt_lst, skb)) { |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 849 | skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3; |
Masahide NAKAMURA | dc435e6 | 2006-08-31 15:18:49 -0700 | [diff] [blame] | 850 | opt = IP6CB(skb); |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 851 | opt->nhoff = sizeof(struct ipv6hdr); |
YOSHIFUJI Hideaki | b809739 | 2006-04-18 14:48:45 -0700 | [diff] [blame] | 852 | return 1; |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 853 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | return -1; |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * Creating outbound headers. |
| 859 | * |
| 860 | * "build" functions work when skb is filled from head to tail (datagram) |
| 861 | * "push" functions work when headers are added from tail to head (tcp) |
| 862 | * |
| 863 | * In both cases we assume, that caller reserved enough room |
| 864 | * for headers. |
| 865 | */ |
| 866 | |
| 867 | static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto, |
| 868 | struct ipv6_rt_hdr *opt, |
David Lebrun | 613fa3c | 2016-11-08 14:59:20 +0100 | [diff] [blame^] | 869 | struct in6_addr **addr_p, struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | { |
| 871 | struct rt0_hdr *phdr, *ihdr; |
| 872 | int hops; |
| 873 | |
| 874 | ihdr = (struct rt0_hdr *) opt; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3); |
| 877 | memcpy(phdr, ihdr, sizeof(struct rt0_hdr)); |
| 878 | |
| 879 | hops = ihdr->rt_hdr.hdrlen >> 1; |
| 880 | |
| 881 | if (hops > 1) |
| 882 | memcpy(phdr->addr, ihdr->addr + 1, |
| 883 | (hops - 1) * sizeof(struct in6_addr)); |
| 884 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 885 | phdr->addr[hops - 1] = **addr_p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | *addr_p = ihdr->addr; |
| 887 | |
| 888 | phdr->rt_hdr.nexthdr = *proto; |
| 889 | *proto = NEXTHDR_ROUTING; |
| 890 | } |
| 891 | |
| 892 | static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt) |
| 893 | { |
| 894 | struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt)); |
| 895 | |
| 896 | memcpy(h, opt, ipv6_optlen(opt)); |
| 897 | h->nexthdr = *proto; |
| 898 | *proto = type; |
| 899 | } |
| 900 | |
| 901 | void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, |
| 902 | u8 *proto, |
David Lebrun | 613fa3c | 2016-11-08 14:59:20 +0100 | [diff] [blame^] | 903 | struct in6_addr **daddr, struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | { |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 905 | if (opt->srcrt) { |
David Lebrun | 613fa3c | 2016-11-08 14:59:20 +0100 | [diff] [blame^] | 906 | ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 907 | /* |
| 908 | * IPV6_RTHDRDSTOPTS is ignored |
| 909 | * unless IPV6_RTHDR is set (RFC3542). |
| 910 | */ |
| 911 | if (opt->dst0opt) |
| 912 | ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt); |
| 913 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | if (opt->hopopt) |
| 915 | ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt); |
| 916 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 917 | EXPORT_SYMBOL(ipv6_push_nfrag_opts); |
| 918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto) |
| 920 | { |
| 921 | if (opt->dst1opt) |
| 922 | ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt); |
| 923 | } |
| 924 | |
| 925 | struct ipv6_txoptions * |
| 926 | ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt) |
| 927 | { |
| 928 | struct ipv6_txoptions *opt2; |
| 929 | |
| 930 | opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC); |
| 931 | if (opt2) { |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 932 | long dif = (char *)opt2 - (char *)opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | memcpy(opt2, opt, opt->tot_len); |
| 934 | if (opt2->hopopt) |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 935 | *((char **)&opt2->hopopt) += dif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | if (opt2->dst0opt) |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 937 | *((char **)&opt2->dst0opt) += dif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | if (opt2->dst1opt) |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 939 | *((char **)&opt2->dst1opt) += dif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | if (opt2->srcrt) |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 941 | *((char **)&opt2->srcrt) += dif; |
Eric Dumazet | 45f6fad | 2015-11-29 19:37:57 -0800 | [diff] [blame] | 942 | atomic_set(&opt2->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | } |
| 944 | return opt2; |
| 945 | } |
Arnaldo Carvalho de Melo | 3cf3dc6 | 2005-12-13 23:23:20 -0800 | [diff] [blame] | 946 | EXPORT_SYMBOL_GPL(ipv6_dup_options); |
| 947 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 948 | static int ipv6_renew_option(void *ohdr, |
| 949 | struct ipv6_opt_hdr __user *newopt, int newoptlen, |
| 950 | int inherit, |
| 951 | struct ipv6_opt_hdr **hdr, |
| 952 | char **p) |
| 953 | { |
| 954 | if (inherit) { |
| 955 | if (ohdr) { |
| 956 | memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr)); |
| 957 | *hdr = (struct ipv6_opt_hdr *)*p; |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 958 | *p += CMSG_ALIGN(ipv6_optlen(*hdr)); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 959 | } |
| 960 | } else { |
| 961 | if (newopt) { |
| 962 | if (copy_from_user(*p, newopt, newoptlen)) |
| 963 | return -EFAULT; |
| 964 | *hdr = (struct ipv6_opt_hdr *)*p; |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 965 | if (ipv6_optlen(*hdr) > newoptlen) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 966 | return -EINVAL; |
| 967 | *p += CMSG_ALIGN(newoptlen); |
| 968 | } |
| 969 | } |
| 970 | return 0; |
| 971 | } |
| 972 | |
Huw Davies | e67ae21 | 2016-06-27 15:02:50 -0400 | [diff] [blame] | 973 | /** |
| 974 | * ipv6_renew_options - replace a specific ext hdr with a new one. |
| 975 | * |
| 976 | * @sk: sock from which to allocate memory |
| 977 | * @opt: original options |
| 978 | * @newtype: option type to replace in @opt |
| 979 | * @newopt: new option of type @newtype to replace (user-mem) |
| 980 | * @newoptlen: length of @newopt |
| 981 | * |
| 982 | * Returns a new set of options which is a copy of @opt with the |
| 983 | * option type @newtype replaced with @newopt. |
| 984 | * |
| 985 | * @opt may be NULL, in which case a new set of options is returned |
| 986 | * containing just @newopt. |
| 987 | * |
| 988 | * @newopt may be NULL, in which case the specified option type is |
| 989 | * not copied into the new set of options. |
| 990 | * |
| 991 | * The new set of options is allocated from the socket option memory |
| 992 | * buffer of @sk. |
| 993 | */ |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 994 | struct ipv6_txoptions * |
| 995 | ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt, |
| 996 | int newtype, |
| 997 | struct ipv6_opt_hdr __user *newopt, int newoptlen) |
| 998 | { |
| 999 | int tot_len = 0; |
| 1000 | char *p; |
| 1001 | struct ipv6_txoptions *opt2; |
| 1002 | int err; |
| 1003 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 1004 | if (opt) { |
| 1005 | if (newtype != IPV6_HOPOPTS && opt->hopopt) |
| 1006 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt)); |
| 1007 | if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt) |
| 1008 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt)); |
| 1009 | if (newtype != IPV6_RTHDR && opt->srcrt) |
| 1010 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt)); |
| 1011 | if (newtype != IPV6_DSTOPTS && opt->dst1opt) |
| 1012 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt)); |
| 1013 | } |
| 1014 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1015 | if (newopt && newoptlen) |
| 1016 | tot_len += CMSG_ALIGN(newoptlen); |
| 1017 | |
| 1018 | if (!tot_len) |
| 1019 | return NULL; |
| 1020 | |
YOSHIFUJI Hideaki | 8b8aa4b | 2005-11-20 12:18:17 +0900 | [diff] [blame] | 1021 | tot_len += sizeof(*opt2); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1022 | opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC); |
| 1023 | if (!opt2) |
| 1024 | return ERR_PTR(-ENOBUFS); |
| 1025 | |
| 1026 | memset(opt2, 0, tot_len); |
Eric Dumazet | 45f6fad | 2015-11-29 19:37:57 -0800 | [diff] [blame] | 1027 | atomic_set(&opt2->refcnt, 1); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1028 | opt2->tot_len = tot_len; |
| 1029 | p = (char *)(opt2 + 1); |
| 1030 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 1031 | err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen, |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1032 | newtype != IPV6_HOPOPTS, |
| 1033 | &opt2->hopopt, &p); |
| 1034 | if (err) |
| 1035 | goto out; |
| 1036 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 1037 | err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen, |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1038 | newtype != IPV6_RTHDRDSTOPTS, |
| 1039 | &opt2->dst0opt, &p); |
| 1040 | if (err) |
| 1041 | goto out; |
| 1042 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 1043 | err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen, |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1044 | newtype != IPV6_RTHDR, |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 1045 | (struct ipv6_opt_hdr **)&opt2->srcrt, &p); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1046 | if (err) |
| 1047 | goto out; |
| 1048 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 1049 | err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen, |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1050 | newtype != IPV6_DSTOPTS, |
| 1051 | &opt2->dst1opt, &p); |
| 1052 | if (err) |
| 1053 | goto out; |
| 1054 | |
| 1055 | opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) + |
| 1056 | (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) + |
| 1057 | (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0); |
| 1058 | opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0); |
| 1059 | |
| 1060 | return opt2; |
| 1061 | out: |
YOSHIFUJI Hideaki | 8b8aa4b | 2005-11-20 12:18:17 +0900 | [diff] [blame] | 1062 | sock_kfree_s(sk, opt2, opt2->tot_len); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1063 | return ERR_PTR(err); |
| 1064 | } |
| 1065 | |
Huw Davies | e67ae21 | 2016-06-27 15:02:50 -0400 | [diff] [blame] | 1066 | /** |
| 1067 | * ipv6_renew_options_kern - replace a specific ext hdr with a new one. |
| 1068 | * |
| 1069 | * @sk: sock from which to allocate memory |
| 1070 | * @opt: original options |
| 1071 | * @newtype: option type to replace in @opt |
| 1072 | * @newopt: new option of type @newtype to replace (kernel-mem) |
| 1073 | * @newoptlen: length of @newopt |
| 1074 | * |
| 1075 | * See ipv6_renew_options(). The difference is that @newopt is |
| 1076 | * kernel memory, rather than user memory. |
| 1077 | */ |
| 1078 | struct ipv6_txoptions * |
| 1079 | ipv6_renew_options_kern(struct sock *sk, struct ipv6_txoptions *opt, |
| 1080 | int newtype, struct ipv6_opt_hdr *newopt, |
| 1081 | int newoptlen) |
| 1082 | { |
| 1083 | struct ipv6_txoptions *ret_val; |
| 1084 | const mm_segment_t old_fs = get_fs(); |
| 1085 | |
| 1086 | set_fs(KERNEL_DS); |
| 1087 | ret_val = ipv6_renew_options(sk, opt, newtype, |
| 1088 | (struct ipv6_opt_hdr __user *)newopt, |
| 1089 | newoptlen); |
| 1090 | set_fs(old_fs); |
| 1091 | return ret_val; |
| 1092 | } |
| 1093 | |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 1094 | struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space, |
| 1095 | struct ipv6_txoptions *opt) |
| 1096 | { |
| 1097 | /* |
| 1098 | * ignore the dest before srcrt unless srcrt is being included. |
| 1099 | * --yoshfuji |
| 1100 | */ |
| 1101 | if (opt && opt->dst0opt && !opt->srcrt) { |
| 1102 | if (opt_space != opt) { |
| 1103 | memcpy(opt_space, opt, sizeof(*opt_space)); |
| 1104 | opt = opt_space; |
| 1105 | } |
| 1106 | opt->opt_nflen -= ipv6_optlen(opt->dst0opt); |
| 1107 | opt->dst0opt = NULL; |
| 1108 | } |
| 1109 | |
| 1110 | return opt; |
| 1111 | } |
Chris Elston | a495f83 | 2012-04-29 21:48:53 +0000 | [diff] [blame] | 1112 | EXPORT_SYMBOL_GPL(ipv6_fixup_options); |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 1113 | |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1114 | /** |
| 1115 | * fl6_update_dst - update flowi destination address with info given |
| 1116 | * by srcrt option, if any. |
| 1117 | * |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1118 | * @fl6: flowi6 for which daddr is to be updated |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1119 | * @opt: struct ipv6_txoptions in which to look for srcrt opt |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1120 | * @orig: copy of original daddr address if modified |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1121 | * |
| 1122 | * Returns NULL if no txoptions or no srcrt, otherwise returns orig |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1123 | * and initial value of fl6->daddr set in orig |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1124 | */ |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1125 | struct in6_addr *fl6_update_dst(struct flowi6 *fl6, |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1126 | const struct ipv6_txoptions *opt, |
| 1127 | struct in6_addr *orig) |
| 1128 | { |
| 1129 | if (!opt || !opt->srcrt) |
| 1130 | return NULL; |
| 1131 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1132 | *orig = fl6->daddr; |
| 1133 | fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr; |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1134 | return orig; |
| 1135 | } |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 1136 | EXPORT_SYMBOL_GPL(fl6_update_dst); |