ip6t_hbh.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Kernel module to match Hop-by-Hop and Destination parameters. */
  2. /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/ipv6.h>
  12. #include <linux/types.h>
  13. #include <net/checksum.h>
  14. #include <net/ipv6.h>
  15. #include <asm/byteorder.h>
  16. #include <linux/netfilter/x_tables.h>
  17. #include <linux/netfilter_ipv6/ip6_tables.h>
  18. #include <linux/netfilter_ipv6/ip6t_opts.h>
  19. MODULE_LICENSE("GPL");
  20. MODULE_DESCRIPTION("Xtables: IPv6 Hop-By-Hop and Destination Header match");
  21. MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
  22. MODULE_ALIAS("ip6t_dst");
  23. /*
  24. * (Type & 0xC0) >> 6
  25. * 0 -> ignorable
  26. * 1 -> must drop the packet
  27. * 2 -> send ICMP PARM PROB regardless and drop packet
  28. * 3 -> Send ICMP if not a multicast address and drop packet
  29. * (Type & 0x20) >> 5
  30. * 0 -> invariant
  31. * 1 -> can change the routing
  32. * (Type & 0x1F) Type
  33. * 0 -> Pad1 (only 1 byte!)
  34. * 1 -> PadN LENGTH info (total length = length + 2)
  35. * C0 | 2 -> JUMBO 4 x x x x ( xxxx > 64k )
  36. * 5 -> RTALERT 2 x x
  37. */
  38. static struct xt_match hbh_mt6_reg[] __read_mostly;
  39. static bool
  40. hbh_mt6(const struct sk_buff *skb, struct xt_action_param *par)
  41. {
  42. struct ipv6_opt_hdr _optsh;
  43. const struct ipv6_opt_hdr *oh;
  44. const struct ip6t_opts *optinfo = par->matchinfo;
  45. unsigned int temp;
  46. unsigned int ptr = 0;
  47. unsigned int hdrlen = 0;
  48. bool ret = false;
  49. u8 _opttype;
  50. u8 _optlen;
  51. const u_int8_t *tp = NULL;
  52. const u_int8_t *lp = NULL;
  53. unsigned int optlen;
  54. int err;
  55. err = ipv6_find_hdr(skb, &ptr,
  56. (par->match == &hbh_mt6_reg[0]) ?
  57. NEXTHDR_HOP : NEXTHDR_DEST, NULL, NULL);
  58. if (err < 0) {
  59. if (err != -ENOENT)
  60. par->hotdrop = true;
  61. return false;
  62. }
  63. oh = skb_header_pointer(skb, ptr, sizeof(_optsh), &_optsh);
  64. if (oh == NULL) {
  65. par->hotdrop = true;
  66. return false;
  67. }
  68. hdrlen = ipv6_optlen(oh);
  69. if (skb->len - ptr < hdrlen) {
  70. /* Packet smaller than it's length field */
  71. return false;
  72. }
  73. pr_debug("IPv6 OPTS LEN %u %u ", hdrlen, oh->hdrlen);
  74. pr_debug("len %02X %04X %02X ",
  75. optinfo->hdrlen, hdrlen,
  76. (!(optinfo->flags & IP6T_OPTS_LEN) ||
  77. ((optinfo->hdrlen == hdrlen) ^
  78. !!(optinfo->invflags & IP6T_OPTS_INV_LEN))));
  79. ret = (oh != NULL) &&
  80. (!(optinfo->flags & IP6T_OPTS_LEN) ||
  81. ((optinfo->hdrlen == hdrlen) ^
  82. !!(optinfo->invflags & IP6T_OPTS_INV_LEN)));
  83. ptr += 2;
  84. hdrlen -= 2;
  85. if (!(optinfo->flags & IP6T_OPTS_OPTS)) {
  86. return ret;
  87. } else {
  88. pr_debug("Strict ");
  89. pr_debug("#%d ", optinfo->optsnr);
  90. for (temp = 0; temp < optinfo->optsnr; temp++) {
  91. /* type field exists ? */
  92. if (hdrlen < 1)
  93. break;
  94. tp = skb_header_pointer(skb, ptr, sizeof(_opttype),
  95. &_opttype);
  96. if (tp == NULL)
  97. break;
  98. /* Type check */
  99. if (*tp != (optinfo->opts[temp] & 0xFF00) >> 8) {
  100. pr_debug("Tbad %02X %02X\n", *tp,
  101. (optinfo->opts[temp] & 0xFF00) >> 8);
  102. return false;
  103. } else {
  104. pr_debug("Tok ");
  105. }
  106. /* Length check */
  107. if (*tp) {
  108. u16 spec_len;
  109. /* length field exists ? */
  110. if (hdrlen < 2)
  111. break;
  112. lp = skb_header_pointer(skb, ptr + 1,
  113. sizeof(_optlen),
  114. &_optlen);
  115. if (lp == NULL)
  116. break;
  117. spec_len = optinfo->opts[temp] & 0x00FF;
  118. if (spec_len != 0x00FF && spec_len != *lp) {
  119. pr_debug("Lbad %02X %04X\n", *lp,
  120. spec_len);
  121. return false;
  122. }
  123. pr_debug("Lok ");
  124. optlen = *lp + 2;
  125. } else {
  126. pr_debug("Pad1\n");
  127. optlen = 1;
  128. }
  129. /* Step to the next */
  130. pr_debug("len%04X\n", optlen);
  131. if ((ptr > skb->len - optlen || hdrlen < optlen) &&
  132. temp < optinfo->optsnr - 1) {
  133. pr_debug("new pointer is too large!\n");
  134. break;
  135. }
  136. ptr += optlen;
  137. hdrlen -= optlen;
  138. }
  139. if (temp == optinfo->optsnr)
  140. return ret;
  141. else
  142. return false;
  143. }
  144. return false;
  145. }
  146. static int hbh_mt6_check(const struct xt_mtchk_param *par)
  147. {
  148. const struct ip6t_opts *optsinfo = par->matchinfo;
  149. if (optsinfo->invflags & ~IP6T_OPTS_INV_MASK) {
  150. pr_debug("unknown flags %X\n", optsinfo->invflags);
  151. return -EINVAL;
  152. }
  153. if (optsinfo->flags & IP6T_OPTS_NSTRICT) {
  154. pr_debug("Not strict - not implemented");
  155. return -EINVAL;
  156. }
  157. return 0;
  158. }
  159. static struct xt_match hbh_mt6_reg[] __read_mostly = {
  160. {
  161. /* Note, hbh_mt6 relies on the order of hbh_mt6_reg */
  162. .name = "hbh",
  163. .family = NFPROTO_IPV6,
  164. .match = hbh_mt6,
  165. .matchsize = sizeof(struct ip6t_opts),
  166. .checkentry = hbh_mt6_check,
  167. .me = THIS_MODULE,
  168. },
  169. {
  170. .name = "dst",
  171. .family = NFPROTO_IPV6,
  172. .match = hbh_mt6,
  173. .matchsize = sizeof(struct ip6t_opts),
  174. .checkentry = hbh_mt6_check,
  175. .me = THIS_MODULE,
  176. },
  177. };
  178. static int __init hbh_mt6_init(void)
  179. {
  180. return xt_register_matches(hbh_mt6_reg, ARRAY_SIZE(hbh_mt6_reg));
  181. }
  182. static void __exit hbh_mt6_exit(void)
  183. {
  184. xt_unregister_matches(hbh_mt6_reg, ARRAY_SIZE(hbh_mt6_reg));
  185. }
  186. module_init(hbh_mt6_init);
  187. module_exit(hbh_mt6_exit);