/net/ipv6/netfilter/ip6t_dst.c

https://bitbucket.org/abioy/linux · C · 278 lines · 213 code · 34 blank · 31 comment · 53 complexity · 3b8dc9e4e1b7f5a0df1a8a3ad9e37c50 MD5 · raw file

  1. /* Kernel module to match Hop-by-Hop and Destination parameters. */
  2. #include <linux/module.h>
  3. #include <linux/skbuff.h>
  4. #include <linux/ipv6.h>
  5. #include <linux/types.h>
  6. #include <net/checksum.h>
  7. #include <net/ipv6.h>
  8. #include <asm/byteorder.h>
  9. #include <linux/netfilter_ipv6/ip6_tables.h>
  10. #include <linux/netfilter_ipv6/ip6t_opts.h>
  11. #define LOW(n) (n & 0x00FF)
  12. #define HOPBYHOP 0
  13. MODULE_LICENSE("GPL");
  14. #if HOPBYHOP
  15. MODULE_DESCRIPTION("IPv6 HbH match");
  16. #else
  17. MODULE_DESCRIPTION("IPv6 DST match");
  18. #endif
  19. MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
  20. #if 0
  21. #define DEBUGP printk
  22. #else
  23. #define DEBUGP(format, args...)
  24. #endif
  25. /*
  26. * (Type & 0xC0) >> 6
  27. * 0 -> ignorable
  28. * 1 -> must drop the packet
  29. * 2 -> send ICMP PARM PROB regardless and drop packet
  30. * 3 -> Send ICMP if not a multicast address and drop packet
  31. * (Type & 0x20) >> 5
  32. * 0 -> invariant
  33. * 1 -> can change the routing
  34. * (Type & 0x1F) Type
  35. * 0 -> PAD0 (only 1 byte!)
  36. * 1 -> PAD1 LENGTH info (total length = length + 2)
  37. * C0 | 2 -> JUMBO 4 x x x x ( xxxx > 64k )
  38. * 5 -> RTALERT 2 x x
  39. */
  40. static int
  41. match(const struct sk_buff *skb,
  42. const struct net_device *in,
  43. const struct net_device *out,
  44. const void *matchinfo,
  45. int offset,
  46. const void *protohdr,
  47. u_int16_t datalen,
  48. int *hotdrop)
  49. {
  50. struct ipv6_opt_hdr *optsh = NULL;
  51. const struct ip6t_opts *optinfo = matchinfo;
  52. unsigned int temp;
  53. unsigned int len;
  54. u8 nexthdr;
  55. unsigned int ptr;
  56. unsigned int hdrlen = 0;
  57. unsigned int ret = 0;
  58. u_int16_t *optdesc = NULL;
  59. /* type of the 1st exthdr */
  60. nexthdr = skb->nh.ipv6h->nexthdr;
  61. /* pointer to the 1st exthdr */
  62. ptr = sizeof(struct ipv6hdr);
  63. /* available length */
  64. len = skb->len - ptr;
  65. temp = 0;
  66. while (ip6t_ext_hdr(nexthdr)) {
  67. struct ipv6_opt_hdr *hdr;
  68. DEBUGP("ipv6_opts header iteration \n");
  69. /* Is there enough space for the next ext header? */
  70. if (len < (int)sizeof(struct ipv6_opt_hdr))
  71. return 0;
  72. /* No more exthdr -> evaluate */
  73. if (nexthdr == NEXTHDR_NONE) {
  74. break;
  75. }
  76. /* ESP -> evaluate */
  77. if (nexthdr == NEXTHDR_ESP) {
  78. break;
  79. }
  80. hdr=(void *)(skb->data)+ptr;
  81. /* Calculate the header length */
  82. if (nexthdr == NEXTHDR_FRAGMENT) {
  83. hdrlen = 8;
  84. } else if (nexthdr == NEXTHDR_AUTH)
  85. hdrlen = (hdr->hdrlen+2)<<2;
  86. else
  87. hdrlen = ipv6_optlen(hdr);
  88. /* OPTS -> evaluate */
  89. #if HOPBYHOP
  90. if (nexthdr == NEXTHDR_HOP) {
  91. temp |= MASK_HOPOPTS;
  92. #else
  93. if (nexthdr == NEXTHDR_DEST) {
  94. temp |= MASK_DSTOPTS;
  95. #endif
  96. break;
  97. }
  98. /* set the flag */
  99. switch (nexthdr){
  100. case NEXTHDR_HOP:
  101. case NEXTHDR_ROUTING:
  102. case NEXTHDR_FRAGMENT:
  103. case NEXTHDR_AUTH:
  104. case NEXTHDR_DEST:
  105. break;
  106. default:
  107. DEBUGP("ipv6_opts match: unknown nextheader %u\n",nexthdr);
  108. return 0;
  109. break;
  110. }
  111. nexthdr = hdr->nexthdr;
  112. len -= hdrlen;
  113. ptr += hdrlen;
  114. if ( ptr > skb->len ) {
  115. DEBUGP("ipv6_opts: new pointer is too large! \n");
  116. break;
  117. }
  118. }
  119. /* OPTIONS header not found */
  120. #if HOPBYHOP
  121. if ( temp != MASK_HOPOPTS ) return 0;
  122. #else
  123. if ( temp != MASK_DSTOPTS ) return 0;
  124. #endif
  125. if (len < (int)sizeof(struct ipv6_opt_hdr)){
  126. *hotdrop = 1;
  127. return 0;
  128. }
  129. if (len < hdrlen){
  130. /* Packet smaller than it's length field */
  131. return 0;
  132. }
  133. optsh=(void *)(skb->data)+ptr;
  134. DEBUGP("IPv6 OPTS LEN %u %u ", hdrlen, optsh->hdrlen);
  135. DEBUGP("len %02X %04X %02X ",
  136. optinfo->hdrlen, hdrlen,
  137. (!(optinfo->flags & IP6T_OPTS_LEN) ||
  138. ((optinfo->hdrlen == hdrlen) ^
  139. !!(optinfo->invflags & IP6T_OPTS_INV_LEN))));
  140. ret = (optsh != NULL)
  141. &&
  142. (!(optinfo->flags & IP6T_OPTS_LEN) ||
  143. ((optinfo->hdrlen == hdrlen) ^
  144. !!(optinfo->invflags & IP6T_OPTS_INV_LEN)));
  145. temp = len = 0;
  146. ptr += 2;
  147. hdrlen -= 2;
  148. if ( !(optinfo->flags & IP6T_OPTS_OPTS) ){
  149. return ret;
  150. } else if (optinfo->flags & IP6T_OPTS_NSTRICT) {
  151. DEBUGP("Not strict - not implemented");
  152. } else {
  153. DEBUGP("Strict ");
  154. DEBUGP("#%d ",optinfo->optsnr);
  155. for(temp=0; temp<optinfo->optsnr; temp++){
  156. optdesc = (void *)(skb->data)+ptr;
  157. /* Type check */
  158. if ( (unsigned char)*optdesc !=
  159. (optinfo->opts[temp] & 0xFF00)>>8 ){
  160. DEBUGP("Tbad %02X %02X\n",
  161. (unsigned char)*optdesc,
  162. (optinfo->opts[temp] &
  163. 0xFF00)>>8);
  164. return 0;
  165. } else {
  166. DEBUGP("Tok ");
  167. }
  168. /* Length check */
  169. if (((optinfo->opts[temp] & 0x00FF) != 0xFF) &&
  170. (unsigned char)*optdesc != 0){
  171. if ( ntohs((u16)*optdesc) !=
  172. optinfo->opts[temp] ){
  173. DEBUGP("Lbad %02X %04X %04X\n",
  174. (unsigned char)*optdesc,
  175. ntohs((u16)*optdesc),
  176. optinfo->opts[temp]);
  177. return 0;
  178. } else {
  179. DEBUGP("Lok ");
  180. }
  181. }
  182. /* Step to the next */
  183. if ((unsigned char)*optdesc == 0){
  184. DEBUGP("PAD0 \n");
  185. ptr++;
  186. hdrlen--;
  187. } else {
  188. ptr += LOW(ntohs(*optdesc));
  189. hdrlen -= LOW(ntohs(*optdesc));
  190. DEBUGP("len%04X \n",
  191. LOW(ntohs(*optdesc)));
  192. }
  193. if (ptr > skb->len || ( !hdrlen &&
  194. (temp != optinfo->optsnr - 1))) {
  195. DEBUGP("new pointer is too large! \n");
  196. break;
  197. }
  198. }
  199. if (temp == optinfo->optsnr)
  200. return ret;
  201. else return 0;
  202. }
  203. return 0;
  204. }
  205. /* Called when user tries to insert an entry of this type. */
  206. static int
  207. checkentry(const char *tablename,
  208. const struct ip6t_ip6 *ip,
  209. void *matchinfo,
  210. unsigned int matchinfosize,
  211. unsigned int hook_mask)
  212. {
  213. const struct ip6t_opts *optsinfo = matchinfo;
  214. if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_opts))) {
  215. DEBUGP("ip6t_opts: matchsize %u != %u\n",
  216. matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_opts)));
  217. return 0;
  218. }
  219. if (optsinfo->invflags & ~IP6T_OPTS_INV_MASK) {
  220. DEBUGP("ip6t_opts: unknown flags %X\n",
  221. optsinfo->invflags);
  222. return 0;
  223. }
  224. return 1;
  225. }
  226. static struct ip6t_match opts_match = {
  227. #if HOPBYHOP
  228. .name = "hbh",
  229. #else
  230. .name = "dst",
  231. #endif
  232. .match = &match,
  233. .checkentry = &checkentry,
  234. };
  235. static int __init init(void)
  236. {
  237. return ip6t_register_match(&opts_match);
  238. }
  239. static void __exit cleanup(void)
  240. {
  241. ip6t_unregister_match(&opts_match);
  242. }
  243. module_init(init);
  244. module_exit(cleanup);