exthdrs_core.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. if (len < 2)
  130. goto bad;
  131. optlen = nh[offset + 1] + 2;
  132. if (optlen > len)
  133. goto bad;
  134. break;
  135. }
  136. offset += optlen;
  137. len -= optlen;
  138. }
  139. /* not_found */
  140. bad:
  141. return -1;
  142. }
  143. EXPORT_SYMBOL_GPL(ipv6_find_tlv);
  144. /*
  145. * find the offset to specified header or the protocol number of last header
  146. * if target < 0. "last header" is transport protocol header, ESP, or
  147. * "No next header".
  148. *
  149. * Note that *offset is used as input/output parameter, and if it is not zero,
  150. * then it must be a valid offset to an inner IPv6 header. This can be used
  151. * to explore inner IPv6 header, eg. ICMPv6 error messages.
  152. *
  153. * If target header is found, its offset is set in *offset and return protocol
  154. * number. Otherwise, return -1.
  155. *
  156. * If the first fragment doesn't contain the final protocol header or
  157. * NEXTHDR_NONE it is considered invalid.
  158. *
  159. * Note that non-1st fragment is special case that "the protocol number
  160. * of last header" is "next header" field in Fragment header. In this case,
  161. * *offset is meaningless and fragment offset is stored in *fragoff if fragoff
  162. * isn't NULL.
  163. *
  164. * if flags is not NULL and it's a fragment, then the frag flag
  165. * IP6_FH_F_FRAG will be set. If it's an AH header, the
  166. * IP6_FH_F_AUTH flag is set and target < 0, then this function will
  167. * stop at the AH header. If IP6_FH_F_SKIP_RH flag was passed, then this
  168. * function will skip all those routing headers, where segements_left was 0.
  169. */
  170. int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
  171. int target, unsigned short *fragoff, int *flags)
  172. {
  173. unsigned int start = skb_network_offset(skb) + sizeof(struct ipv6hdr);
  174. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  175. bool found;
  176. if (fragoff)
  177. *fragoff = 0;
  178. if (*offset) {
  179. struct ipv6hdr _ip6, *ip6;
  180. ip6 = skb_header_pointer(skb, *offset, sizeof(_ip6), &_ip6);
  181. if (!ip6 || (ip6->version != 6))
  182. return -EBADMSG;
  183. start = *offset + sizeof(struct ipv6hdr);
  184. nexthdr = ip6->nexthdr;
  185. }
  186. do {
  187. struct ipv6_opt_hdr _hdr, *hp;
  188. unsigned int hdrlen;
  189. found = (nexthdr == target);
  190. if ((!ipv6_ext_hdr(nexthdr)) || nexthdr == NEXTHDR_NONE) {
  191. if (target < 0 || found)
  192. break;
  193. return -ENOENT;
  194. }
  195. hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
  196. if (!hp)
  197. return -EBADMSG;
  198. if (nexthdr == NEXTHDR_ROUTING) {
  199. struct ipv6_rt_hdr _rh, *rh;
  200. rh = skb_header_pointer(skb, start, sizeof(_rh),
  201. &_rh);
  202. if (!rh)
  203. return -EBADMSG;
  204. if (flags && (*flags & IP6_FH_F_SKIP_RH) &&
  205. rh->segments_left == 0)
  206. found = false;
  207. }
  208. if (nexthdr == NEXTHDR_FRAGMENT) {
  209. unsigned short _frag_off;
  210. __be16 *fp;
  211. if (flags) /* Indicate that this is a fragment */
  212. *flags |= IP6_FH_F_FRAG;
  213. fp = skb_header_pointer(skb,
  214. start+offsetof(struct frag_hdr,
  215. frag_off),
  216. sizeof(_frag_off),
  217. &_frag_off);
  218. if (!fp)
  219. return -EBADMSG;
  220. _frag_off = ntohs(*fp) & ~0x7;
  221. if (_frag_off) {
  222. if (target < 0 &&
  223. ((!ipv6_ext_hdr(hp->nexthdr)) ||
  224. hp->nexthdr == NEXTHDR_NONE)) {
  225. if (fragoff)
  226. *fragoff = _frag_off;
  227. return hp->nexthdr;
  228. }
  229. if (!found)
  230. return -ENOENT;
  231. if (fragoff)
  232. *fragoff = _frag_off;
  233. break;
  234. }
  235. hdrlen = 8;
  236. } else if (nexthdr == NEXTHDR_AUTH) {
  237. if (flags && (*flags & IP6_FH_F_AUTH) && (target < 0))
  238. break;
  239. hdrlen = ipv6_authlen(hp);
  240. } else
  241. hdrlen = ipv6_optlen(hp);
  242. if (!found) {
  243. nexthdr = hp->nexthdr;
  244. start += hdrlen;
  245. }
  246. } while (!found);
  247. *offset = start;
  248. return nexthdr;
  249. }
  250. EXPORT_SYMBOL(ipv6_find_hdr);