exthdrs_core.c 7.5 KB

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