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> |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 46 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 47 | #include <net/xfrm.h> |
| 48 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | #include <asm/uaccess.h> |
Vlad Yasevich | 2207afc | 2012-11-15 08:49:19 +0000 | [diff] [blame^] | 51 | #include "ip6_offload.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 53 | int ipv6_find_tlv(struct sk_buff *skb, int offset, int type) |
| 54 | { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 55 | const unsigned char *nh = skb_network_header(skb); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 56 | int packet_len = skb->tail - skb->network_header; |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 57 | struct ipv6_opt_hdr *hdr; |
| 58 | int len; |
| 59 | |
| 60 | if (offset + 2 > packet_len) |
| 61 | goto bad; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 62 | hdr = (struct ipv6_opt_hdr *)(nh + offset); |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 63 | len = ((hdr->hdrlen + 1) << 3); |
| 64 | |
| 65 | if (offset + len > packet_len) |
| 66 | goto bad; |
| 67 | |
| 68 | offset += 2; |
| 69 | len -= 2; |
| 70 | |
| 71 | while (len > 0) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 72 | int opttype = nh[offset]; |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 73 | int optlen; |
| 74 | |
| 75 | if (opttype == type) |
| 76 | return offset; |
| 77 | |
| 78 | switch (opttype) { |
Eldad Zack | 1de5a71 | 2012-05-17 06:00:25 +0000 | [diff] [blame] | 79 | case IPV6_TLV_PAD1: |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 80 | optlen = 1; |
| 81 | break; |
| 82 | default: |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 83 | optlen = nh[offset + 1] + 2; |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 84 | if (optlen > len) |
| 85 | goto bad; |
| 86 | break; |
| 87 | } |
| 88 | offset += optlen; |
| 89 | len -= optlen; |
| 90 | } |
| 91 | /* not_found */ |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 92 | bad: |
| 93 | return -1; |
| 94 | } |
Masahide NAKAMURA | 59fbb3a6 | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 95 | EXPORT_SYMBOL_GPL(ipv6_find_tlv); |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* |
| 98 | * Parsing tlv encoded headers. |
| 99 | * |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 100 | * Parsing function "func" returns true, if parsing succeed |
| 101 | * and false, if it failed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | * It MUST NOT touch skb->h. |
| 103 | */ |
| 104 | |
| 105 | struct tlvtype_proc { |
| 106 | int type; |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 107 | bool (*func)(struct sk_buff *skb, int offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /********************* |
| 111 | Generic functions |
| 112 | *********************/ |
| 113 | |
| 114 | /* An unknown option is detected, decide what to do */ |
| 115 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 116 | static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 118 | switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | case 0: /* ignore */ |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 120 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | case 1: /* drop packet */ |
| 123 | break; |
| 124 | |
| 125 | case 3: /* Send ICMP if not a multicast address and drop packet */ |
| 126 | /* Actually, it is redundant check. icmp_send |
| 127 | will recheck in any case. |
| 128 | */ |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 129 | if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | break; |
| 131 | case 2: /* send ICMP PARM PROB regardless and drop packet */ |
| 132 | icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 133 | return false; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 137 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* Parse tlv encoded option header (hop-by-hop or destination) */ |
| 141 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 142 | 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] | 143 | { |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 144 | const struct tlvtype_proc *curr; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 145 | const unsigned char *nh = skb_network_header(skb); |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 146 | int off = skb_network_header_len(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 147 | int len = (skb_transport_header(skb)[1] + 1) << 3; |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 148 | int padlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 150 | if (skb_transport_offset(skb) + len > skb_headlen(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | goto bad; |
| 152 | |
| 153 | off += 2; |
| 154 | len -= 2; |
| 155 | |
| 156 | while (len > 0) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 157 | int optlen = nh[off + 1] + 2; |
Eldad Zack | c1412fc | 2012-04-12 17:36:17 -0400 | [diff] [blame] | 158 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 160 | switch (nh[off]) { |
Eldad Zack | 1de5a71 | 2012-05-17 06:00:25 +0000 | [diff] [blame] | 161 | case IPV6_TLV_PAD1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | optlen = 1; |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 163 | padlen++; |
| 164 | if (padlen > 7) |
| 165 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | break; |
| 167 | |
| 168 | case IPV6_TLV_PADN: |
Eldad Zack | c1412fc | 2012-04-12 17:36:17 -0400 | [diff] [blame] | 169 | /* RFC 2460 states that the purpose of PadN is |
| 170 | * to align the containing header to multiples |
| 171 | * of 8. 7 is therefore the highest valid value. |
| 172 | * See also RFC 4942, Section 2.1.9.5. |
| 173 | */ |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 174 | padlen += optlen; |
| 175 | if (padlen > 7) |
Eldad Zack | c1412fc | 2012-04-12 17:36:17 -0400 | [diff] [blame] | 176 | goto bad; |
| 177 | /* RFC 4942 recommends receiving hosts to |
| 178 | * actively check PadN payload to contain |
| 179 | * only zeroes. |
| 180 | */ |
| 181 | for (i = 2; i < optlen; i++) { |
| 182 | if (nh[off + i] != 0) |
| 183 | goto bad; |
| 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | break; |
| 186 | |
| 187 | default: /* Other TLV code so scan list */ |
| 188 | if (optlen > len) |
| 189 | goto bad; |
| 190 | for (curr=procs; curr->type >= 0; curr++) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 191 | if (curr->type == nh[off]) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 192 | /* type specific length/alignment |
| 193 | checks will be performed in the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | func(). */ |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 195 | if (curr->func(skb, off) == false) |
| 196 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | } |
| 200 | if (curr->type < 0) { |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 201 | if (ip6_tlvopt_unknown(skb, off) == 0) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 202 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 204 | padlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | break; |
| 206 | } |
| 207 | off += optlen; |
| 208 | len -= optlen; |
| 209 | } |
Eldad Zack | 9b905fe | 2012-05-20 01:59:33 +0000 | [diff] [blame] | 210 | /* This case will not be caught by above check since its padding |
| 211 | * length is smaller than 7: |
| 212 | * 1 byte NH + 1 byte Length + 6 bytes Padding |
| 213 | */ |
| 214 | if ((padlen == 6) && ((off - skb_network_header_len(skb)) == 8)) |
| 215 | goto bad; |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (len == 0) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 218 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | bad: |
| 220 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 221 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /***************************** |
| 225 | Destination options header. |
| 226 | *****************************/ |
| 227 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 228 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 229 | static bool ipv6_dest_hao(struct sk_buff *skb, int optoff) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 230 | { |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 231 | struct ipv6_destopt_hao *hao; |
| 232 | struct inet6_skb_parm *opt = IP6CB(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 233 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 234 | struct in6_addr tmp_addr; |
| 235 | int ret; |
| 236 | |
| 237 | if (opt->dsthao) { |
| 238 | LIMIT_NETDEBUG(KERN_DEBUG "hao duplicated\n"); |
| 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) { |
| 247 | LIMIT_NETDEBUG( |
| 248 | KERN_DEBUG "hao invalid option length = %d\n", hao->length); |
| 249 | goto discard; |
| 250 | } |
| 251 | |
| 252 | if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) { |
| 253 | LIMIT_NETDEBUG( |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 254 | KERN_DEBUG "hao is not an unicast addr: %pI6\n", &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 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 276 | tmp_addr = ipv6h->saddr; |
| 277 | ipv6h->saddr = hao->addr; |
| 278 | hao->addr = tmp_addr; |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 279 | |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 280 | if (skb->tstamp.tv64 == 0) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 281 | __net_timestamp(skb); |
| 282 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 283 | return true; |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 284 | |
| 285 | discard: |
| 286 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 287 | return false; |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 288 | } |
| 289 | #endif |
| 290 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 291 | static const struct tlvtype_proc tlvprocdestopt_lst[] = { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 292 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 293 | { |
| 294 | .type = IPV6_TLV_HAO, |
| 295 | .func = ipv6_dest_hao, |
| 296 | }, |
| 297 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | {-1, NULL} |
| 299 | }; |
| 300 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 301 | static int ipv6_destopt_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | struct inet6_skb_parm *opt = IP6CB(skb); |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 304 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 305 | __u16 dstbuf; |
| 306 | #endif |
Eric Dumazet | 897dc80 | 2011-07-28 04:00:35 +0000 | [diff] [blame] | 307 | struct dst_entry *dst = skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 309 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) || |
| 310 | !pskb_may_pull(skb, (skb_transport_offset(skb) + |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 311 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
Eric Dumazet | 897dc80 | 2011-07-28 04:00:35 +0000 | [diff] [blame] | 312 | IP6_INC_STATS_BH(dev_net(dst->dev), ip6_dst_idev(dst), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 313 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | kfree_skb(skb); |
| 315 | return -1; |
| 316 | } |
| 317 | |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 318 | opt->lastopt = opt->dst1 = skb_network_header_len(skb); |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 319 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 320 | dstbuf = opt->dst1; |
| 321 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 323 | if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) { |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 324 | skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3; |
Masahide NAKAMURA | dc435e6 | 2006-08-31 15:18:49 -0700 | [diff] [blame] | 325 | opt = IP6CB(skb); |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 326 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 327 | opt->nhoff = dstbuf; |
| 328 | #else |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 329 | opt->nhoff = opt->dst1; |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 330 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return 1; |
| 332 | } |
| 333 | |
Denis V. Lunev | 483a47d | 2008-10-08 11:09:27 -0700 | [diff] [blame] | 334 | IP6_INC_STATS_BH(dev_net(dst->dev), |
| 335 | ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | return -1; |
| 337 | } |
| 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | /******************************** |
| 340 | Routing header. |
| 341 | ********************************/ |
| 342 | |
Eric Dumazet | f6bc7d9 | 2010-06-14 04:39:27 +0000 | [diff] [blame] | 343 | /* called with rcu_read_lock() */ |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 344 | static int ipv6_rthdr_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | struct inet6_skb_parm *opt = IP6CB(skb); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 347 | struct in6_addr *addr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | struct in6_addr daddr; |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 349 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | int n, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | struct ipv6_rt_hdr *hdr; |
| 352 | struct rt0_hdr *rthdr; |
Denis V. Lunev | 483a47d | 2008-10-08 11:09:27 -0700 | [diff] [blame] | 353 | struct net *net = dev_net(skb->dev); |
| 354 | int accept_source_route = net->ipv6.devconf_all->accept_source_route; |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 355 | |
Eric Dumazet | f6bc7d9 | 2010-06-14 04:39:27 +0000 | [diff] [blame] | 356 | idev = __in6_dev_get(skb->dev); |
| 357 | if (idev && accept_source_route > idev->cnf.accept_source_route) |
| 358 | accept_source_route = idev->cnf.accept_source_route; |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 359 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 360 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) || |
| 361 | !pskb_may_pull(skb, (skb_transport_offset(skb) + |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 362 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 363 | IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 364 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | kfree_skb(skb); |
| 366 | return -1; |
| 367 | } |
| 368 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 369 | hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 371 | if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | skb->pkt_type != PACKET_HOST) { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 373 | IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 374 | IPSTATS_MIB_INADDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | kfree_skb(skb); |
| 376 | return -1; |
| 377 | } |
| 378 | |
| 379 | looped_back: |
| 380 | if (hdr->segments_left == 0) { |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 381 | switch (hdr->type) { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 382 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 383 | case IPV6_SRCRT_TYPE_2: |
| 384 | /* Silently discard type 2 header unless it was |
| 385 | * processed by own |
| 386 | */ |
| 387 | if (!addr) { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 388 | IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 389 | IPSTATS_MIB_INADDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 390 | kfree_skb(skb); |
| 391 | return -1; |
| 392 | } |
| 393 | break; |
| 394 | #endif |
| 395 | default: |
| 396 | break; |
| 397 | } |
| 398 | |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 399 | opt->lastopt = opt->srcrt = skb_network_header_len(skb); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 400 | skb->transport_header += (hdr->hdrlen + 1) << 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | opt->dst0 = opt->dst1; |
| 402 | opt->dst1 = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 403 | opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return 1; |
| 405 | } |
| 406 | |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 407 | switch (hdr->type) { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 408 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 409 | case IPV6_SRCRT_TYPE_2: |
YOSHIFUJI Hideaki | c382bb9 | 2007-07-10 22:47:58 -0700 | [diff] [blame] | 410 | if (accept_source_route < 0) |
| 411 | goto unknown_rh; |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 412 | /* Silently discard invalid RTH type 2 */ |
| 413 | if (hdr->hdrlen != 2 || hdr->segments_left != 1) { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 414 | IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 415 | IPSTATS_MIB_INHDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 416 | kfree_skb(skb); |
| 417 | return -1; |
| 418 | } |
| 419 | break; |
| 420 | #endif |
YOSHIFUJI Hideaki | c382bb9 | 2007-07-10 22:47:58 -0700 | [diff] [blame] | 421 | default: |
| 422 | goto unknown_rh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * This is the routing header forwarding algorithm from |
| 427 | * RFC 2460, page 16. |
| 428 | */ |
| 429 | |
| 430 | n = hdr->hdrlen >> 1; |
| 431 | |
| 432 | if (hdr->segments_left > n) { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 433 | IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 434 | IPSTATS_MIB_INHDRERRORS); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 435 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 436 | ((&hdr->segments_left) - |
| 437 | skb_network_header(skb))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | return -1; |
| 439 | } |
| 440 | |
| 441 | /* We are about to mangle packet header. Be careful! |
| 442 | Do not damage packets queued somewhere. |
| 443 | */ |
| 444 | if (skb_cloned(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | /* the copy is a forwarded packet */ |
Herbert Xu | 65c8846 | 2007-10-15 01:29:10 -0700 | [diff] [blame] | 446 | if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 447 | IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 448 | IPSTATS_MIB_OUTDISCARDS); |
| 449 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | return -1; |
| 451 | } |
Herbert Xu | 65c8846 | 2007-10-15 01:29:10 -0700 | [diff] [blame] | 452 | hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 455 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | skb->ip_summed = CHECKSUM_NONE; |
| 457 | |
| 458 | i = n - --hdr->segments_left; |
| 459 | |
| 460 | rthdr = (struct rt0_hdr *) hdr; |
| 461 | addr = rthdr->addr; |
| 462 | addr += i - 1; |
| 463 | |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 464 | switch (hdr->type) { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 465 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 466 | case IPV6_SRCRT_TYPE_2: |
| 467 | if (xfrm6_input_addr(skb, (xfrm_address_t *)addr, |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 468 | (xfrm_address_t *)&ipv6_hdr(skb)->saddr, |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 469 | IPPROTO_ROUTING) < 0) { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 470 | IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 471 | IPSTATS_MIB_INADDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 472 | kfree_skb(skb); |
| 473 | return -1; |
| 474 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 475 | if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) { |
| 476 | IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 477 | IPSTATS_MIB_INADDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 478 | kfree_skb(skb); |
| 479 | return -1; |
| 480 | } |
| 481 | break; |
| 482 | #endif |
| 483 | default: |
| 484 | break; |
| 485 | } |
| 486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | if (ipv6_addr_is_multicast(addr)) { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 488 | IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 489 | IPSTATS_MIB_INADDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | kfree_skb(skb); |
| 491 | return -1; |
| 492 | } |
| 493 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 494 | daddr = *addr; |
| 495 | *addr = ipv6_hdr(skb)->daddr; |
| 496 | ipv6_hdr(skb)->daddr = daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 498 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | ip6_route_input(skb); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 500 | if (skb_dst(skb)->error) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 501 | skb_push(skb, skb->data - skb_network_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | dst_input(skb); |
| 503 | return -1; |
| 504 | } |
| 505 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 506 | if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 507 | if (ipv6_hdr(skb)->hop_limit <= 1) { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 508 | IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 509 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 511 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | kfree_skb(skb); |
| 513 | return -1; |
| 514 | } |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 515 | ipv6_hdr(skb)->hop_limit--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | goto looped_back; |
| 517 | } |
| 518 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 519 | skb_push(skb, skb->data - skb_network_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | dst_input(skb); |
| 521 | return -1; |
YOSHIFUJI Hideaki | c382bb9 | 2007-07-10 22:47:58 -0700 | [diff] [blame] | 522 | |
| 523 | unknown_rh: |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 524 | IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS); |
YOSHIFUJI Hideaki | c382bb9 | 2007-07-10 22:47:58 -0700 | [diff] [blame] | 525 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 526 | (&hdr->type) - skb_network_header(skb)); |
| 527 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 530 | static const struct inet6_protocol rthdr_protocol = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | .handler = ipv6_rthdr_rcv, |
Vlad Yasevich | 2207afc | 2012-11-15 08:49:19 +0000 | [diff] [blame^] | 532 | .flags = INET6_PROTO_NOPOLICY, |
Vlad Yasevich | 8ca896c | 2012-11-15 08:49:13 +0000 | [diff] [blame] | 533 | }; |
| 534 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 535 | static const struct inet6_protocol destopt_protocol = { |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 536 | .handler = ipv6_destopt_rcv, |
Vlad Yasevich | 2207afc | 2012-11-15 08:49:19 +0000 | [diff] [blame^] | 537 | .flags = INET6_PROTO_NOPOLICY, |
Vlad Yasevich | 8ca896c | 2012-11-15 08:49:13 +0000 | [diff] [blame] | 538 | }; |
| 539 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 540 | static const struct inet6_protocol nodata_protocol = { |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 541 | .handler = dst_discard, |
| 542 | .flags = INET6_PROTO_NOPOLICY, |
| 543 | }; |
| 544 | |
| 545 | int __init ipv6_exthdrs_init(void) |
| 546 | { |
| 547 | int ret; |
| 548 | |
Vlad Yasevich | 3336288 | 2012-11-15 08:49:15 +0000 | [diff] [blame] | 549 | ret = ipv6_exthdrs_offload_init(); |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 550 | if (ret) |
| 551 | goto out; |
| 552 | |
Vlad Yasevich | 3336288 | 2012-11-15 08:49:15 +0000 | [diff] [blame] | 553 | ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING); |
| 554 | if (ret) |
| 555 | goto out_offload; |
| 556 | |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 557 | ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS); |
| 558 | if (ret) |
| 559 | goto out_rthdr; |
| 560 | |
| 561 | ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE); |
| 562 | if (ret) |
| 563 | goto out_destopt; |
| 564 | |
| 565 | out: |
| 566 | return ret; |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 567 | out_destopt: |
| 568 | inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS); |
Vlad Yasevich | 3336288 | 2012-11-15 08:49:15 +0000 | [diff] [blame] | 569 | out_rthdr: |
| 570 | inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING); |
| 571 | out_offload: |
| 572 | ipv6_exthdrs_offload_exit(); |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 573 | goto out; |
| 574 | }; |
| 575 | |
| 576 | void ipv6_exthdrs_exit(void) |
| 577 | { |
Vlad Yasevich | 2207afc | 2012-11-15 08:49:19 +0000 | [diff] [blame^] | 578 | ipv6_exthdrs_offload_exit(); |
Daniel Lezcano | 248b238 | 2007-12-11 02:23:54 -0800 | [diff] [blame] | 579 | inet6_del_protocol(&nodata_protocol, IPPROTO_NONE); |
| 580 | inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS); |
| 581 | inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING); |
| 582 | } |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | /********************************** |
| 585 | Hop-by-hop options. |
| 586 | **********************************/ |
| 587 | |
YOSHIFUJI Hideaki | e76b2b2 | 2007-05-09 14:01:59 -0700 | [diff] [blame] | 588 | /* |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 589 | * 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] | 590 | */ |
| 591 | static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb) |
| 592 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 593 | 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] | 594 | } |
| 595 | |
David S. Miller | 2570a4f | 2010-01-13 17:27:37 -0800 | [diff] [blame] | 596 | static inline struct net *ipv6_skb_net(struct sk_buff *skb) |
| 597 | { |
| 598 | return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev); |
| 599 | } |
| 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | /* Router Alert as of RFC 2711 */ |
| 602 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 603 | static bool ipv6_hop_ra(struct sk_buff *skb, int optoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 605 | const unsigned char *nh = skb_network_header(skb); |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 606 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 607 | if (nh[optoff + 1] == 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | IP6CB(skb)->ra = optoff; |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 609 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | } |
Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 611 | LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_ra: wrong RA length %d\n", |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 612 | nh[optoff + 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 614 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /* Jumbo payload */ |
| 618 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 619 | static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 621 | const unsigned char *nh = skb_network_header(skb); |
David S. Miller | 2570a4f | 2010-01-13 17:27:37 -0800 | [diff] [blame] | 622 | struct net *net = ipv6_skb_net(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | u32 pkt_len; |
| 624 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 625 | if (nh[optoff + 1] != 4 || (optoff & 3) != 2) { |
Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 626 | LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n", |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 627 | nh[optoff+1]); |
Denis V. Lunev | 483a47d | 2008-10-08 11:09:27 -0700 | [diff] [blame] | 628 | IP6_INC_STATS_BH(net, ipv6_skb_idev(skb), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 629 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | goto drop; |
| 631 | } |
| 632 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 633 | pkt_len = ntohl(*(__be32 *)(nh + optoff + 2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | if (pkt_len <= IPV6_MAXPLEN) { |
Denis V. Lunev | 483a47d | 2008-10-08 11:09:27 -0700 | [diff] [blame] | 635 | IP6_INC_STATS_BH(net, ipv6_skb_idev(skb), |
| 636 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 638 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 640 | if (ipv6_hdr(skb)->payload_len) { |
Denis V. Lunev | 483a47d | 2008-10-08 11:09:27 -0700 | [diff] [blame] | 641 | IP6_INC_STATS_BH(net, ipv6_skb_idev(skb), |
| 642 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 644 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | if (pkt_len > skb->len - sizeof(struct ipv6hdr)) { |
Denis V. Lunev | 483a47d | 2008-10-08 11:09:27 -0700 | [diff] [blame] | 648 | IP6_INC_STATS_BH(net, ipv6_skb_idev(skb), |
| 649 | IPSTATS_MIB_INTRUNCATEDPKTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | goto drop; |
| 651 | } |
Stephen Hemminger | 42ca89c | 2005-09-08 12:57:43 -0700 | [diff] [blame] | 652 | |
| 653 | if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) |
| 654 | goto drop; |
| 655 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 656 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
| 658 | drop: |
| 659 | kfree_skb(skb); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 660 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 663 | static const struct tlvtype_proc tlvprochopopt_lst[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
| 665 | .type = IPV6_TLV_ROUTERALERT, |
| 666 | .func = ipv6_hop_ra, |
| 667 | }, |
| 668 | { |
| 669 | .type = IPV6_TLV_JUMBO, |
| 670 | .func = ipv6_hop_jumbo, |
| 671 | }, |
| 672 | { -1, } |
| 673 | }; |
| 674 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 675 | int ipv6_parse_hopopts(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 677 | struct inet6_skb_parm *opt = IP6CB(skb); |
| 678 | |
YOSHIFUJI Hideaki | ec67009 | 2006-04-18 14:46:26 -0700 | [diff] [blame] | 679 | /* |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 680 | * skb_network_header(skb) is equal to skb->data, and |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 681 | * skb_network_header_len(skb) is always equal to |
YOSHIFUJI Hideaki | ec67009 | 2006-04-18 14:46:26 -0700 | [diff] [blame] | 682 | * sizeof(struct ipv6hdr) by definition of |
| 683 | * hop-by-hop options. |
| 684 | */ |
| 685 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) || |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 686 | !pskb_may_pull(skb, (sizeof(struct ipv6hdr) + |
| 687 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
YOSHIFUJI Hideaki | ec67009 | 2006-04-18 14:46:26 -0700 | [diff] [blame] | 688 | kfree_skb(skb); |
| 689 | return -1; |
| 690 | } |
| 691 | |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 692 | opt->hop = sizeof(struct ipv6hdr); |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 693 | if (ip6_parse_tlv(tlvprochopopt_lst, skb)) { |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 694 | skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3; |
Masahide NAKAMURA | dc435e6 | 2006-08-31 15:18:49 -0700 | [diff] [blame] | 695 | opt = IP6CB(skb); |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 696 | opt->nhoff = sizeof(struct ipv6hdr); |
YOSHIFUJI Hideaki | b809739 | 2006-04-18 14:48:45 -0700 | [diff] [blame] | 697 | return 1; |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 698 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | return -1; |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Creating outbound headers. |
| 704 | * |
| 705 | * "build" functions work when skb is filled from head to tail (datagram) |
| 706 | * "push" functions work when headers are added from tail to head (tcp) |
| 707 | * |
| 708 | * In both cases we assume, that caller reserved enough room |
| 709 | * for headers. |
| 710 | */ |
| 711 | |
| 712 | static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto, |
| 713 | struct ipv6_rt_hdr *opt, |
| 714 | struct in6_addr **addr_p) |
| 715 | { |
| 716 | struct rt0_hdr *phdr, *ihdr; |
| 717 | int hops; |
| 718 | |
| 719 | ihdr = (struct rt0_hdr *) opt; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3); |
| 722 | memcpy(phdr, ihdr, sizeof(struct rt0_hdr)); |
| 723 | |
| 724 | hops = ihdr->rt_hdr.hdrlen >> 1; |
| 725 | |
| 726 | if (hops > 1) |
| 727 | memcpy(phdr->addr, ihdr->addr + 1, |
| 728 | (hops - 1) * sizeof(struct in6_addr)); |
| 729 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 730 | phdr->addr[hops - 1] = **addr_p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | *addr_p = ihdr->addr; |
| 732 | |
| 733 | phdr->rt_hdr.nexthdr = *proto; |
| 734 | *proto = NEXTHDR_ROUTING; |
| 735 | } |
| 736 | |
| 737 | static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt) |
| 738 | { |
| 739 | struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt)); |
| 740 | |
| 741 | memcpy(h, opt, ipv6_optlen(opt)); |
| 742 | h->nexthdr = *proto; |
| 743 | *proto = type; |
| 744 | } |
| 745 | |
| 746 | void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, |
| 747 | u8 *proto, |
| 748 | struct in6_addr **daddr) |
| 749 | { |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 750 | if (opt->srcrt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | ipv6_push_rthdr(skb, proto, opt->srcrt, daddr); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 752 | /* |
| 753 | * IPV6_RTHDRDSTOPTS is ignored |
| 754 | * unless IPV6_RTHDR is set (RFC3542). |
| 755 | */ |
| 756 | if (opt->dst0opt) |
| 757 | ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt); |
| 758 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | if (opt->hopopt) |
| 760 | ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt); |
| 761 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 762 | EXPORT_SYMBOL(ipv6_push_nfrag_opts); |
| 763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto) |
| 765 | { |
| 766 | if (opt->dst1opt) |
| 767 | ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt); |
| 768 | } |
| 769 | |
| 770 | struct ipv6_txoptions * |
| 771 | ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt) |
| 772 | { |
| 773 | struct ipv6_txoptions *opt2; |
| 774 | |
| 775 | opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC); |
| 776 | if (opt2) { |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 777 | long dif = (char *)opt2 - (char *)opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | memcpy(opt2, opt, opt->tot_len); |
| 779 | if (opt2->hopopt) |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 780 | *((char **)&opt2->hopopt) += dif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | if (opt2->dst0opt) |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 782 | *((char **)&opt2->dst0opt) += dif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | if (opt2->dst1opt) |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 784 | *((char **)&opt2->dst1opt) += dif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | if (opt2->srcrt) |
Eldad Zack | ac3c817 | 2012-04-01 07:49:04 +0000 | [diff] [blame] | 786 | *((char **)&opt2->srcrt) += dif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | } |
| 788 | return opt2; |
| 789 | } |
Arnaldo Carvalho de Melo | 3cf3dc6 | 2005-12-13 23:23:20 -0800 | [diff] [blame] | 790 | EXPORT_SYMBOL_GPL(ipv6_dup_options); |
| 791 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 792 | static int ipv6_renew_option(void *ohdr, |
| 793 | struct ipv6_opt_hdr __user *newopt, int newoptlen, |
| 794 | int inherit, |
| 795 | struct ipv6_opt_hdr **hdr, |
| 796 | char **p) |
| 797 | { |
| 798 | if (inherit) { |
| 799 | if (ohdr) { |
| 800 | memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr)); |
| 801 | *hdr = (struct ipv6_opt_hdr *)*p; |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 802 | *p += CMSG_ALIGN(ipv6_optlen(*hdr)); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 803 | } |
| 804 | } else { |
| 805 | if (newopt) { |
| 806 | if (copy_from_user(*p, newopt, newoptlen)) |
| 807 | return -EFAULT; |
| 808 | *hdr = (struct ipv6_opt_hdr *)*p; |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 809 | if (ipv6_optlen(*hdr) > newoptlen) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 810 | return -EINVAL; |
| 811 | *p += CMSG_ALIGN(newoptlen); |
| 812 | } |
| 813 | } |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | struct ipv6_txoptions * |
| 818 | ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt, |
| 819 | int newtype, |
| 820 | struct ipv6_opt_hdr __user *newopt, int newoptlen) |
| 821 | { |
| 822 | int tot_len = 0; |
| 823 | char *p; |
| 824 | struct ipv6_txoptions *opt2; |
| 825 | int err; |
| 826 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 827 | if (opt) { |
| 828 | if (newtype != IPV6_HOPOPTS && opt->hopopt) |
| 829 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt)); |
| 830 | if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt) |
| 831 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt)); |
| 832 | if (newtype != IPV6_RTHDR && opt->srcrt) |
| 833 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt)); |
| 834 | if (newtype != IPV6_DSTOPTS && opt->dst1opt) |
| 835 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt)); |
| 836 | } |
| 837 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 838 | if (newopt && newoptlen) |
| 839 | tot_len += CMSG_ALIGN(newoptlen); |
| 840 | |
| 841 | if (!tot_len) |
| 842 | return NULL; |
| 843 | |
YOSHIFUJI Hideaki | 8b8aa4b | 2005-11-20 12:18:17 +0900 | [diff] [blame] | 844 | tot_len += sizeof(*opt2); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 845 | opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC); |
| 846 | if (!opt2) |
| 847 | return ERR_PTR(-ENOBUFS); |
| 848 | |
| 849 | memset(opt2, 0, tot_len); |
| 850 | |
| 851 | opt2->tot_len = tot_len; |
| 852 | p = (char *)(opt2 + 1); |
| 853 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 854 | err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen, |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 855 | newtype != IPV6_HOPOPTS, |
| 856 | &opt2->hopopt, &p); |
| 857 | if (err) |
| 858 | goto out; |
| 859 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 860 | err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen, |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 861 | newtype != IPV6_RTHDRDSTOPTS, |
| 862 | &opt2->dst0opt, &p); |
| 863 | if (err) |
| 864 | goto out; |
| 865 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 866 | err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen, |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 867 | newtype != IPV6_RTHDR, |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 868 | (struct ipv6_opt_hdr **)&opt2->srcrt, &p); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 869 | if (err) |
| 870 | goto out; |
| 871 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 872 | err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen, |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 873 | newtype != IPV6_DSTOPTS, |
| 874 | &opt2->dst1opt, &p); |
| 875 | if (err) |
| 876 | goto out; |
| 877 | |
| 878 | opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) + |
| 879 | (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) + |
| 880 | (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0); |
| 881 | opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0); |
| 882 | |
| 883 | return opt2; |
| 884 | out: |
YOSHIFUJI Hideaki | 8b8aa4b | 2005-11-20 12:18:17 +0900 | [diff] [blame] | 885 | sock_kfree_s(sk, opt2, opt2->tot_len); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 886 | return ERR_PTR(err); |
| 887 | } |
| 888 | |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 889 | struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space, |
| 890 | struct ipv6_txoptions *opt) |
| 891 | { |
| 892 | /* |
| 893 | * ignore the dest before srcrt unless srcrt is being included. |
| 894 | * --yoshfuji |
| 895 | */ |
| 896 | if (opt && opt->dst0opt && !opt->srcrt) { |
| 897 | if (opt_space != opt) { |
| 898 | memcpy(opt_space, opt, sizeof(*opt_space)); |
| 899 | opt = opt_space; |
| 900 | } |
| 901 | opt->opt_nflen -= ipv6_optlen(opt->dst0opt); |
| 902 | opt->dst0opt = NULL; |
| 903 | } |
| 904 | |
| 905 | return opt; |
| 906 | } |
Chris Elston | a495f83 | 2012-04-29 21:48:53 +0000 | [diff] [blame] | 907 | EXPORT_SYMBOL_GPL(ipv6_fixup_options); |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 908 | |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 909 | /** |
| 910 | * fl6_update_dst - update flowi destination address with info given |
| 911 | * by srcrt option, if any. |
| 912 | * |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 913 | * @fl6: flowi6 for which daddr is to be updated |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 914 | * @opt: struct ipv6_txoptions in which to look for srcrt opt |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 915 | * @orig: copy of original daddr address if modified |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 916 | * |
| 917 | * Returns NULL if no txoptions or no srcrt, otherwise returns orig |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 918 | * and initial value of fl6->daddr set in orig |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 919 | */ |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 920 | struct in6_addr *fl6_update_dst(struct flowi6 *fl6, |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 921 | const struct ipv6_txoptions *opt, |
| 922 | struct in6_addr *orig) |
| 923 | { |
| 924 | if (!opt || !opt->srcrt) |
| 925 | return NULL; |
| 926 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 927 | *orig = fl6->daddr; |
| 928 | fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr; |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 929 | return orig; |
| 930 | } |
Arnaud Ebalard | 20c59de | 2010-06-01 21:35:01 +0000 | [diff] [blame] | 931 | EXPORT_SYMBOL_GPL(fl6_update_dst); |