exthdrs_core.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. int 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. /*
  23. * Skip any extension headers. This is used by the ICMP module.
  24. *
  25. * Note that strictly speaking this conflicts with RFC 2460 4.0:
  26. * ...The contents and semantics of each extension header determine whether
  27. * or not to proceed to the next header. Therefore, extension headers must
  28. * be processed strictly in the order they appear in the packet; a
  29. * receiver must not, for example, scan through a packet looking for a
  30. * particular kind of extension header and process that header prior to
  31. * processing all preceding ones.
  32. *
  33. * We do exactly this. This is a protocol bug. We can't decide after a
  34. * seeing an unknown discard-with-error flavour TLV option if it's a
  35. * ICMP error message or not (errors should never be send in reply to
  36. * ICMP error messages).
  37. *
  38. * But I see no other way to do this. This might need to be reexamined
  39. * when Linux implements ESP (and maybe AUTH) headers.
  40. * --AK
  41. *
  42. * This function parses (probably truncated) exthdr set "hdr".
  43. * "nexthdrp" initially points to some place,
  44. * where type of the first header can be found.
  45. *
  46. * It skips all well-known exthdrs, and returns pointer to the start
  47. * of unparsable area i.e. the first header with unknown type.
  48. * If it is not NULL *nexthdr is updated by type/protocol of this header.
  49. *
  50. * NOTES: - if packet terminated with NEXTHDR_NONE it returns NULL.
  51. * - it may return pointer pointing beyond end of packet,
  52. * if the last recognized header is truncated in the middle.
  53. * - if packet is truncated, so that all parsed headers are skipped,
  54. * it returns NULL.
  55. * - First fragment header is skipped, not-first ones
  56. * are considered as unparsable.
  57. * - ESP is unparsable for now and considered like
  58. * normal payload protocol.
  59. * - Note also special handling of AUTH header. Thanks to IPsec wizards.
  60. *
  61. * --ANK (980726)
  62. */
  63. int ipv6_skip_exthdr(const struct sk_buff *skb, int start, u8 *nexthdrp)
  64. {
  65. u8 nexthdr = *nexthdrp;
  66. while (ipv6_ext_hdr(nexthdr)) {
  67. struct ipv6_opt_hdr _hdr, *hp;
  68. int hdrlen;
  69. if (nexthdr == NEXTHDR_NONE)
  70. return -1;
  71. hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
  72. if (hp == NULL)
  73. return -1;
  74. if (nexthdr == NEXTHDR_FRAGMENT) {
  75. __be16 _frag_off, *fp;
  76. fp = skb_header_pointer(skb,
  77. start+offsetof(struct frag_hdr,
  78. frag_off),
  79. sizeof(_frag_off),
  80. &_frag_off);
  81. if (fp == NULL)
  82. return -1;
  83. if (ntohs(*fp) & ~0x7)
  84. break;
  85. hdrlen = 8;
  86. } else if (nexthdr == NEXTHDR_AUTH)
  87. hdrlen = (hp->hdrlen+2)<<2;
  88. else
  89. hdrlen = ipv6_optlen(hp);
  90. nexthdr = hp->nexthdr;
  91. start += hdrlen;
  92. }
  93. *nexthdrp = nexthdr;
  94. return start;
  95. }
  96. EXPORT_SYMBOL(ipv6_ext_hdr);
  97. EXPORT_SYMBOL(ipv6_skip_exthdr);