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 | |
| 12 | int ipv6_ext_hdr(u8 nexthdr) |
| 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 | } |
| 24 | |
| 25 | /* |
| 26 | * Skip any extension headers. This is used by the ICMP module. |
| 27 | * |
| 28 | * Note that strictly speaking this conflicts with RFC 2460 4.0: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 29 | * ...The contents and semantics of each extension header determine whether |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * or not to proceed to the next header. Therefore, extension headers must |
| 31 | * be processed strictly in the order they appear in the packet; a |
| 32 | * receiver must not, for example, scan through a packet looking for a |
| 33 | * particular kind of extension header and process that header prior to |
| 34 | * processing all preceding ones. |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 35 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | * 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] | 37 | * 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] | 38 | * ICMP error message or not (errors should never be send in reply to |
| 39 | * ICMP error messages). |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 40 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | * But I see no other way to do this. This might need to be reexamined |
| 42 | * when Linux implements ESP (and maybe AUTH) headers. |
| 43 | * --AK |
| 44 | * |
Herbert Xu | 0d3d077 | 2005-04-24 20:16:19 -0700 | [diff] [blame] | 45 | * This function parses (probably truncated) exthdr set "hdr". |
| 46 | * "nexthdrp" initially points to some place, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | * where type of the first header can be found. |
| 48 | * |
| 49 | * It skips all well-known exthdrs, and returns pointer to the start |
| 50 | * of unparsable area i.e. the first header with unknown type. |
| 51 | * If it is not NULL *nexthdr is updated by type/protocol of this header. |
| 52 | * |
| 53 | * NOTES: - if packet terminated with NEXTHDR_NONE it returns NULL. |
| 54 | * - it may return pointer pointing beyond end of packet, |
| 55 | * if the last recognized header is truncated in the middle. |
| 56 | * - if packet is truncated, so that all parsed headers are skipped, |
| 57 | * it returns NULL. |
| 58 | * - First fragment header is skipped, not-first ones |
| 59 | * are considered as unparsable. |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 60 | * - Reports the offset field of the final fragment header so it is |
| 61 | * possible to tell whether this is a first fragment, later fragment, |
| 62 | * or not fragmented. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * - ESP is unparsable for now and considered like |
| 64 | * normal payload protocol. |
| 65 | * - Note also special handling of AUTH header. Thanks to IPsec wizards. |
| 66 | * |
| 67 | * --ANK (980726) |
| 68 | */ |
| 69 | |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 70 | int ipv6_skip_exthdr(const struct sk_buff *skb, int start, u8 *nexthdrp, |
| 71 | __be16 *frag_offp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
| 73 | u8 nexthdr = *nexthdrp; |
| 74 | |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 75 | *frag_offp = 0; |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | while (ipv6_ext_hdr(nexthdr)) { |
| 78 | struct ipv6_opt_hdr _hdr, *hp; |
| 79 | int hdrlen; |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | if (nexthdr == NEXTHDR_NONE) |
| 82 | return -1; |
| 83 | hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr); |
| 84 | if (hp == NULL) |
Herbert Xu | 0d3d077 | 2005-04-24 20:16:19 -0700 | [diff] [blame] | 85 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | if (nexthdr == NEXTHDR_FRAGMENT) { |
Al Viro | e69a4adc | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 87 | __be16 _frag_off, *fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | fp = skb_header_pointer(skb, |
| 89 | start+offsetof(struct frag_hdr, |
| 90 | frag_off), |
| 91 | sizeof(_frag_off), |
| 92 | &_frag_off); |
| 93 | if (fp == NULL) |
| 94 | return -1; |
| 95 | |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 96 | *frag_offp = *fp; |
| 97 | if (ntohs(*frag_offp) & ~0x7) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | break; |
| 99 | hdrlen = 8; |
| 100 | } else if (nexthdr == NEXTHDR_AUTH) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 101 | hdrlen = (hp->hdrlen+2)<<2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | else |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 103 | hdrlen = ipv6_optlen(hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | nexthdr = hp->nexthdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | start += hdrlen; |
| 107 | } |
| 108 | |
| 109 | *nexthdrp = nexthdr; |
| 110 | return start; |
| 111 | } |
| 112 | |
| 113 | EXPORT_SYMBOL(ipv6_ext_hdr); |
| 114 | EXPORT_SYMBOL(ipv6_skip_exthdr); |