ip6t_ipv6header.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* ipv6header match - matches IPv6 packets based
  2. on whether they contain certain headers */
  3. /* Original idea: Brad Chapman
  4. * Rewritten by: Andras Kis-Szabo <kisza@sch.bme.hu> */
  5. /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/ipv6.h>
  14. #include <linux/types.h>
  15. #include <net/checksum.h>
  16. #include <net/ipv6.h>
  17. #include <linux/netfilter/x_tables.h>
  18. #include <linux/netfilter_ipv6/ip6_tables.h>
  19. #include <linux/netfilter_ipv6/ip6t_ipv6header.h>
  20. MODULE_LICENSE("GPL");
  21. MODULE_DESCRIPTION("Xtables: IPv6 header types match");
  22. MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
  23. static bool
  24. ipv6header_mt6(const struct sk_buff *skb, struct xt_action_param *par)
  25. {
  26. const struct ip6t_ipv6header_info *info = par->matchinfo;
  27. unsigned int temp;
  28. int len;
  29. u8 nexthdr;
  30. unsigned int ptr;
  31. /* Make sure this isn't an evil packet */
  32. /* type of the 1st exthdr */
  33. nexthdr = ipv6_hdr(skb)->nexthdr;
  34. /* pointer to the 1st exthdr */
  35. ptr = sizeof(struct ipv6hdr);
  36. /* available length */
  37. len = skb->len - ptr;
  38. temp = 0;
  39. while (ip6t_ext_hdr(nexthdr)) {
  40. const struct ipv6_opt_hdr *hp;
  41. struct ipv6_opt_hdr _hdr;
  42. int hdrlen;
  43. /* No more exthdr -> evaluate */
  44. if (nexthdr == NEXTHDR_NONE) {
  45. temp |= MASK_NONE;
  46. break;
  47. }
  48. /* Is there enough space for the next ext header? */
  49. if (len < (int)sizeof(struct ipv6_opt_hdr))
  50. return false;
  51. /* ESP -> evaluate */
  52. if (nexthdr == NEXTHDR_ESP) {
  53. temp |= MASK_ESP;
  54. break;
  55. }
  56. hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
  57. BUG_ON(hp == NULL);
  58. /* Calculate the header length */
  59. if (nexthdr == NEXTHDR_FRAGMENT)
  60. hdrlen = 8;
  61. else if (nexthdr == NEXTHDR_AUTH)
  62. hdrlen = (hp->hdrlen + 2) << 2;
  63. else
  64. hdrlen = ipv6_optlen(hp);
  65. /* set the flag */
  66. switch (nexthdr) {
  67. case NEXTHDR_HOP:
  68. temp |= MASK_HOPOPTS;
  69. break;
  70. case NEXTHDR_ROUTING:
  71. temp |= MASK_ROUTING;
  72. break;
  73. case NEXTHDR_FRAGMENT:
  74. temp |= MASK_FRAGMENT;
  75. break;
  76. case NEXTHDR_AUTH:
  77. temp |= MASK_AH;
  78. break;
  79. case NEXTHDR_DEST:
  80. temp |= MASK_DSTOPTS;
  81. break;
  82. default:
  83. return false;
  84. }
  85. nexthdr = hp->nexthdr;
  86. len -= hdrlen;
  87. ptr += hdrlen;
  88. if (ptr > skb->len)
  89. break;
  90. }
  91. if (nexthdr != NEXTHDR_NONE && nexthdr != NEXTHDR_ESP)
  92. temp |= MASK_PROTO;
  93. if (info->modeflag)
  94. return !((temp ^ info->matchflags ^ info->invflags)
  95. & info->matchflags);
  96. else {
  97. if (info->invflags)
  98. return temp != info->matchflags;
  99. else
  100. return temp == info->matchflags;
  101. }
  102. }
  103. static int ipv6header_mt6_check(const struct xt_mtchk_param *par)
  104. {
  105. const struct ip6t_ipv6header_info *info = par->matchinfo;
  106. /* invflags is 0 or 0xff in hard mode */
  107. if ((!info->modeflag) && info->invflags != 0x00 &&
  108. info->invflags != 0xFF)
  109. return -EINVAL;
  110. return 0;
  111. }
  112. static struct xt_match ipv6header_mt6_reg __read_mostly = {
  113. .name = "ipv6header",
  114. .family = NFPROTO_IPV6,
  115. .match = ipv6header_mt6,
  116. .matchsize = sizeof(struct ip6t_ipv6header_info),
  117. .checkentry = ipv6header_mt6_check,
  118. .destroy = NULL,
  119. .me = THIS_MODULE,
  120. };
  121. static int __init ipv6header_mt6_init(void)
  122. {
  123. return xt_register_match(&ipv6header_mt6_reg);
  124. }
  125. static void __exit ipv6header_mt6_exit(void)
  126. {
  127. xt_unregister_match(&ipv6header_mt6_reg);
  128. }
  129. module_init(ipv6header_mt6_init);
  130. module_exit(ipv6header_mt6_exit);