Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPv6 library code, needed by static components when full IPv6 support is |
| 3 | * not configured or static. |
| 4 | */ |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 5 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <net/ipv6.h> |
| 7 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 8 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * find out if nexthdr is a well-known extension header or a protocol |
| 10 | */ |
| 11 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 12 | bool ipv6_ext_hdr(u8 nexthdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 14 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * find out if nexthdr is an extension header or a protocol |
| 16 | */ |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 17 | return (nexthdr == NEXTHDR_HOP) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | (nexthdr == NEXTHDR_ROUTING) || |
| 19 | (nexthdr == NEXTHDR_FRAGMENT) || |
| 20 | (nexthdr == NEXTHDR_AUTH) || |
| 21 | (nexthdr == NEXTHDR_NONE) || |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 22 | (nexthdr == NEXTHDR_DEST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | } |
Eldad Zack | 81bd60b | 2012-04-01 07:49:05 +0000 | [diff] [blame] | 24 | EXPORT_SYMBOL(ipv6_ext_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * Skip any extension headers. This is used by the ICMP module. |
| 28 | * |
| 29 | * Note that strictly speaking this conflicts with RFC 2460 4.0: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 30 | * ...The contents and semantics of each extension header determine whether |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | * or not to proceed to the next header. Therefore, extension headers must |
| 32 | * be processed strictly in the order they appear in the packet; a |
| 33 | * receiver must not, for example, scan through a packet looking for a |
| 34 | * particular kind of extension header and process that header prior to |
| 35 | * processing all preceding ones. |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 36 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * We do exactly this. This is a protocol bug. We can't decide after a |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 38 | * seeing an unknown discard-with-error flavour TLV option if it's a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | * ICMP error message or not (errors should never be send in reply to |
| 40 | * ICMP error messages). |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 41 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * But I see no other way to do this. This might need to be reexamined |
| 43 | * when Linux implements ESP (and maybe AUTH) headers. |
| 44 | * --AK |
| 45 | * |
Herbert Xu | 0d3d077 | 2005-04-24 20:16:19 -0700 | [diff] [blame] | 46 | * This function parses (probably truncated) exthdr set "hdr". |
| 47 | * "nexthdrp" initially points to some place, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | * where type of the first header can be found. |
| 49 | * |
| 50 | * It skips all well-known exthdrs, and returns pointer to the start |
| 51 | * of unparsable area i.e. the first header with unknown type. |
| 52 | * If it is not NULL *nexthdr is updated by type/protocol of this header. |
| 53 | * |
| 54 | * NOTES: - if packet terminated with NEXTHDR_NONE it returns NULL. |
| 55 | * - it may return pointer pointing beyond end of packet, |
| 56 | * if the last recognized header is truncated in the middle. |
| 57 | * - if packet is truncated, so that all parsed headers are skipped, |
| 58 | * it returns NULL. |
| 59 | * - First fragment header is skipped, not-first ones |
| 60 | * are considered as unparsable. |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 61 | * - Reports the offset field of the final fragment header so it is |
| 62 | * possible to tell whether this is a first fragment, later fragment, |
| 63 | * or not fragmented. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | * - ESP is unparsable for now and considered like |
| 65 | * normal payload protocol. |
| 66 | * - Note also special handling of AUTH header. Thanks to IPsec wizards. |
| 67 | * |
| 68 | * --ANK (980726) |
| 69 | */ |
| 70 | |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 71 | int ipv6_skip_exthdr(const struct sk_buff *skb, int start, u8 *nexthdrp, |
| 72 | __be16 *frag_offp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
| 74 | u8 nexthdr = *nexthdrp; |
| 75 | |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 76 | *frag_offp = 0; |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | while (ipv6_ext_hdr(nexthdr)) { |
| 79 | struct ipv6_opt_hdr _hdr, *hp; |
| 80 | int hdrlen; |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | if (nexthdr == NEXTHDR_NONE) |
| 83 | return -1; |
| 84 | hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 85 | if (!hp) |
Herbert Xu | 0d3d077 | 2005-04-24 20:16:19 -0700 | [diff] [blame] | 86 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | if (nexthdr == NEXTHDR_FRAGMENT) { |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 88 | __be16 _frag_off, *fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | fp = skb_header_pointer(skb, |
| 90 | start+offsetof(struct frag_hdr, |
| 91 | frag_off), |
| 92 | sizeof(_frag_off), |
| 93 | &_frag_off); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 94 | if (!fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | return -1; |
| 96 | |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 97 | *frag_offp = *fp; |
| 98 | if (ntohs(*frag_offp) & ~0x7) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | break; |
| 100 | hdrlen = 8; |
| 101 | } else if (nexthdr == NEXTHDR_AUTH) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 102 | hdrlen = (hp->hdrlen+2)<<2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | else |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 104 | hdrlen = ipv6_optlen(hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | nexthdr = hp->nexthdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | start += hdrlen; |
| 108 | } |
| 109 | |
| 110 | *nexthdrp = nexthdr; |
| 111 | return start; |
| 112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | EXPORT_SYMBOL(ipv6_skip_exthdr); |
Vlad Yasevich | 3c73a03 | 2012-11-15 08:49:20 +0000 | [diff] [blame] | 114 | |
| 115 | int ipv6_find_tlv(struct sk_buff *skb, int offset, int type) |
| 116 | { |
| 117 | const unsigned char *nh = skb_network_header(skb); |
Simon Horman | 29a3cad | 2013-05-28 20:34:26 +0000 | [diff] [blame] | 118 | int packet_len = skb_tail_pointer(skb) - skb_network_header(skb); |
Vlad Yasevich | 3c73a03 | 2012-11-15 08:49:20 +0000 | [diff] [blame] | 119 | struct ipv6_opt_hdr *hdr; |
| 120 | int len; |
| 121 | |
| 122 | if (offset + 2 > packet_len) |
| 123 | goto bad; |
| 124 | hdr = (struct ipv6_opt_hdr *)(nh + offset); |
| 125 | len = ((hdr->hdrlen + 1) << 3); |
| 126 | |
| 127 | if (offset + len > packet_len) |
| 128 | goto bad; |
| 129 | |
| 130 | offset += 2; |
| 131 | len -= 2; |
| 132 | |
| 133 | while (len > 0) { |
| 134 | int opttype = nh[offset]; |
| 135 | int optlen; |
| 136 | |
| 137 | if (opttype == type) |
| 138 | return offset; |
| 139 | |
| 140 | switch (opttype) { |
| 141 | case IPV6_TLV_PAD1: |
| 142 | optlen = 1; |
| 143 | break; |
| 144 | default: |
| 145 | optlen = nh[offset + 1] + 2; |
| 146 | if (optlen > len) |
| 147 | goto bad; |
| 148 | break; |
| 149 | } |
| 150 | offset += optlen; |
| 151 | len -= optlen; |
| 152 | } |
| 153 | /* not_found */ |
| 154 | bad: |
| 155 | return -1; |
| 156 | } |
| 157 | EXPORT_SYMBOL_GPL(ipv6_find_tlv); |
David S. Miller | e716503 | 2012-11-30 12:01:30 -0500 | [diff] [blame] | 158 | |
Jesse Gross | f8f6267 | 2012-11-09 17:05:07 -0800 | [diff] [blame] | 159 | /* |
| 160 | * find the offset to specified header or the protocol number of last header |
| 161 | * if target < 0. "last header" is transport protocol header, ESP, or |
| 162 | * "No next header". |
| 163 | * |
| 164 | * Note that *offset is used as input/output parameter. an if it is not zero, |
| 165 | * then it must be a valid offset to an inner IPv6 header. This can be used |
| 166 | * to explore inner IPv6 header, eg. ICMPv6 error messages. |
| 167 | * |
| 168 | * If target header is found, its offset is set in *offset and return protocol |
| 169 | * number. Otherwise, return -1. |
| 170 | * |
| 171 | * If the first fragment doesn't contain the final protocol header or |
| 172 | * NEXTHDR_NONE it is considered invalid. |
| 173 | * |
| 174 | * Note that non-1st fragment is special case that "the protocol number |
| 175 | * of last header" is "next header" field in Fragment header. In this case, |
| 176 | * *offset is meaningless and fragment offset is stored in *fragoff if fragoff |
| 177 | * isn't NULL. |
| 178 | * |
Ansis Atteka | 9195bb8 | 2012-11-09 17:11:31 -0800 | [diff] [blame] | 179 | * if flags is not NULL and it's a fragment, then the frag flag |
| 180 | * IP6_FH_F_FRAG will be set. If it's an AH header, the |
| 181 | * IP6_FH_F_AUTH flag is set and target < 0, then this function will |
| 182 | * stop at the AH header. If IP6_FH_F_SKIP_RH flag was passed, then this |
| 183 | * function will skip all those routing headers, where segements_left was 0. |
Jesse Gross | f8f6267 | 2012-11-09 17:05:07 -0800 | [diff] [blame] | 184 | */ |
| 185 | int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset, |
| 186 | int target, unsigned short *fragoff, int *flags) |
| 187 | { |
| 188 | unsigned int start = skb_network_offset(skb) + sizeof(struct ipv6hdr); |
| 189 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; |
| 190 | unsigned int len; |
Ansis Atteka | 9195bb8 | 2012-11-09 17:11:31 -0800 | [diff] [blame] | 191 | bool found; |
Jesse Gross | f8f6267 | 2012-11-09 17:05:07 -0800 | [diff] [blame] | 192 | |
| 193 | if (fragoff) |
| 194 | *fragoff = 0; |
| 195 | |
| 196 | if (*offset) { |
| 197 | struct ipv6hdr _ip6, *ip6; |
| 198 | |
| 199 | ip6 = skb_header_pointer(skb, *offset, sizeof(_ip6), &_ip6); |
| 200 | if (!ip6 || (ip6->version != 6)) { |
| 201 | printk(KERN_ERR "IPv6 header not found\n"); |
| 202 | return -EBADMSG; |
| 203 | } |
| 204 | start = *offset + sizeof(struct ipv6hdr); |
| 205 | nexthdr = ip6->nexthdr; |
| 206 | } |
| 207 | len = skb->len - start; |
| 208 | |
Ansis Atteka | 9195bb8 | 2012-11-09 17:11:31 -0800 | [diff] [blame] | 209 | do { |
Jesse Gross | f8f6267 | 2012-11-09 17:05:07 -0800 | [diff] [blame] | 210 | struct ipv6_opt_hdr _hdr, *hp; |
| 211 | unsigned int hdrlen; |
Ansis Atteka | 9195bb8 | 2012-11-09 17:11:31 -0800 | [diff] [blame] | 212 | found = (nexthdr == target); |
Jesse Gross | f8f6267 | 2012-11-09 17:05:07 -0800 | [diff] [blame] | 213 | |
| 214 | if ((!ipv6_ext_hdr(nexthdr)) || nexthdr == NEXTHDR_NONE) { |
Hans Schillstrom | accfe0e | 2014-02-27 12:57:58 +0100 | [diff] [blame] | 215 | if (target < 0 || found) |
Jesse Gross | f8f6267 | 2012-11-09 17:05:07 -0800 | [diff] [blame] | 216 | break; |
| 217 | return -ENOENT; |
| 218 | } |
| 219 | |
| 220 | hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 221 | if (!hp) |
Jesse Gross | f8f6267 | 2012-11-09 17:05:07 -0800 | [diff] [blame] | 222 | return -EBADMSG; |
Ansis Atteka | 9195bb8 | 2012-11-09 17:11:31 -0800 | [diff] [blame] | 223 | |
| 224 | if (nexthdr == NEXTHDR_ROUTING) { |
| 225 | struct ipv6_rt_hdr _rh, *rh; |
| 226 | |
| 227 | rh = skb_header_pointer(skb, start, sizeof(_rh), |
| 228 | &_rh); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 229 | if (!rh) |
Ansis Atteka | 9195bb8 | 2012-11-09 17:11:31 -0800 | [diff] [blame] | 230 | return -EBADMSG; |
| 231 | |
| 232 | if (flags && (*flags & IP6_FH_F_SKIP_RH) && |
| 233 | rh->segments_left == 0) |
| 234 | found = false; |
| 235 | } |
| 236 | |
Jesse Gross | f8f6267 | 2012-11-09 17:05:07 -0800 | [diff] [blame] | 237 | if (nexthdr == NEXTHDR_FRAGMENT) { |
| 238 | unsigned short _frag_off; |
| 239 | __be16 *fp; |
| 240 | |
| 241 | if (flags) /* Indicate that this is a fragment */ |
| 242 | *flags |= IP6_FH_F_FRAG; |
| 243 | fp = skb_header_pointer(skb, |
| 244 | start+offsetof(struct frag_hdr, |
| 245 | frag_off), |
| 246 | sizeof(_frag_off), |
| 247 | &_frag_off); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 248 | if (!fp) |
Jesse Gross | f8f6267 | 2012-11-09 17:05:07 -0800 | [diff] [blame] | 249 | return -EBADMSG; |
| 250 | |
| 251 | _frag_off = ntohs(*fp) & ~0x7; |
| 252 | if (_frag_off) { |
| 253 | if (target < 0 && |
| 254 | ((!ipv6_ext_hdr(hp->nexthdr)) || |
| 255 | hp->nexthdr == NEXTHDR_NONE)) { |
| 256 | if (fragoff) |
| 257 | *fragoff = _frag_off; |
| 258 | return hp->nexthdr; |
| 259 | } |
Florian Westphal | 5d150a98 | 2016-03-01 16:15:16 +0100 | [diff] [blame^] | 260 | if (!found) |
| 261 | return -ENOENT; |
| 262 | if (fragoff) |
| 263 | *fragoff = _frag_off; |
| 264 | break; |
Jesse Gross | f8f6267 | 2012-11-09 17:05:07 -0800 | [diff] [blame] | 265 | } |
| 266 | hdrlen = 8; |
| 267 | } else if (nexthdr == NEXTHDR_AUTH) { |
| 268 | if (flags && (*flags & IP6_FH_F_AUTH) && (target < 0)) |
| 269 | break; |
| 270 | hdrlen = (hp->hdrlen + 2) << 2; |
| 271 | } else |
| 272 | hdrlen = ipv6_optlen(hp); |
| 273 | |
Ansis Atteka | 9195bb8 | 2012-11-09 17:11:31 -0800 | [diff] [blame] | 274 | if (!found) { |
| 275 | nexthdr = hp->nexthdr; |
| 276 | len -= hdrlen; |
| 277 | start += hdrlen; |
| 278 | } |
| 279 | } while (!found); |
Jesse Gross | f8f6267 | 2012-11-09 17:05:07 -0800 | [diff] [blame] | 280 | |
| 281 | *offset = start; |
| 282 | return nexthdr; |
| 283 | } |
| 284 | EXPORT_SYMBOL(ipv6_find_hdr); |
David S. Miller | e716503 | 2012-11-30 12:01:30 -0500 | [diff] [blame] | 285 | |