exthdrs_core.c 7.5 KB

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