PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/net/ipv6/netfilter/ip6t_frag.c

https://gitlab.com/culot/kernel_lge_madai
C | 136 lines | 112 code | 16 blank | 8 comment | 10 complexity | 6b02b8d2591901c49f7e173a2ac757e3 MD5 | raw file
  1. /* Kernel module to match FRAG 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 <linux/netfilter/x_tables.h>
  16. #include <linux/netfilter_ipv6/ip6_tables.h>
  17. #include <linux/netfilter_ipv6/ip6t_frag.h>
  18. MODULE_LICENSE("GPL");
  19. MODULE_DESCRIPTION("Xtables: IPv6 fragment match");
  20. MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
  21. /* Returns 1 if the id is matched by the range, 0 otherwise */
  22. static inline bool
  23. id_match(u_int32_t min, u_int32_t max, u_int32_t id, bool invert)
  24. {
  25. bool r;
  26. pr_debug("id_match:%c 0x%x <= 0x%x <= 0x%x\n", invert ? '!' : ' ',
  27. min, id, max);
  28. r = (id >= min && id <= max) ^ invert;
  29. pr_debug(" result %s\n", r ? "PASS" : "FAILED");
  30. return r;
  31. }
  32. static bool
  33. frag_mt6(const struct sk_buff *skb, struct xt_action_param *par)
  34. {
  35. struct frag_hdr _frag;
  36. const struct frag_hdr *fh;
  37. const struct ip6t_frag *fraginfo = par->matchinfo;
  38. unsigned int ptr;
  39. int err;
  40. err = ipv6_find_hdr(skb, &ptr, NEXTHDR_FRAGMENT, NULL);
  41. if (err < 0) {
  42. if (err != -ENOENT)
  43. par->hotdrop = true;
  44. return false;
  45. }
  46. fh = skb_header_pointer(skb, ptr, sizeof(_frag), &_frag);
  47. if (fh == NULL) {
  48. par->hotdrop = true;
  49. return false;
  50. }
  51. pr_debug("INFO %04X ", fh->frag_off);
  52. pr_debug("OFFSET %04X ", ntohs(fh->frag_off) & ~0x7);
  53. pr_debug("RES %02X %04X", fh->reserved, ntohs(fh->frag_off) & 0x6);
  54. pr_debug("MF %04X ", fh->frag_off & htons(IP6_MF));
  55. pr_debug("ID %u %08X\n", ntohl(fh->identification),
  56. ntohl(fh->identification));
  57. pr_debug("IPv6 FRAG id %02X ",
  58. id_match(fraginfo->ids[0], fraginfo->ids[1],
  59. ntohl(fh->identification),
  60. !!(fraginfo->invflags & IP6T_FRAG_INV_IDS)));
  61. pr_debug("res %02X %02X%04X %02X ",
  62. fraginfo->flags & IP6T_FRAG_RES, fh->reserved,
  63. ntohs(fh->frag_off) & 0x6,
  64. !((fraginfo->flags & IP6T_FRAG_RES) &&
  65. (fh->reserved || (ntohs(fh->frag_off) & 0x06))));
  66. pr_debug("first %02X %02X %02X ",
  67. fraginfo->flags & IP6T_FRAG_FST,
  68. ntohs(fh->frag_off) & ~0x7,
  69. !((fraginfo->flags & IP6T_FRAG_FST) &&
  70. (ntohs(fh->frag_off) & ~0x7)));
  71. pr_debug("mf %02X %02X %02X ",
  72. fraginfo->flags & IP6T_FRAG_MF,
  73. ntohs(fh->frag_off) & IP6_MF,
  74. !((fraginfo->flags & IP6T_FRAG_MF) &&
  75. !((ntohs(fh->frag_off) & IP6_MF))));
  76. pr_debug("last %02X %02X %02X\n",
  77. fraginfo->flags & IP6T_FRAG_NMF,
  78. ntohs(fh->frag_off) & IP6_MF,
  79. !((fraginfo->flags & IP6T_FRAG_NMF) &&
  80. (ntohs(fh->frag_off) & IP6_MF)));
  81. return (fh != NULL) &&
  82. id_match(fraginfo->ids[0], fraginfo->ids[1],
  83. ntohl(fh->identification),
  84. !!(fraginfo->invflags & IP6T_FRAG_INV_IDS)) &&
  85. !((fraginfo->flags & IP6T_FRAG_RES) &&
  86. (fh->reserved || (ntohs(fh->frag_off) & 0x6))) &&
  87. !((fraginfo->flags & IP6T_FRAG_FST) &&
  88. (ntohs(fh->frag_off) & ~0x7)) &&
  89. !((fraginfo->flags & IP6T_FRAG_MF) &&
  90. !(ntohs(fh->frag_off) & IP6_MF)) &&
  91. !((fraginfo->flags & IP6T_FRAG_NMF) &&
  92. (ntohs(fh->frag_off) & IP6_MF));
  93. }
  94. static int frag_mt6_check(const struct xt_mtchk_param *par)
  95. {
  96. const struct ip6t_frag *fraginfo = par->matchinfo;
  97. if (fraginfo->invflags & ~IP6T_FRAG_INV_MASK) {
  98. pr_debug("unknown flags %X\n", fraginfo->invflags);
  99. return -EINVAL;
  100. }
  101. return 0;
  102. }
  103. static struct xt_match frag_mt6_reg __read_mostly = {
  104. .name = "frag",
  105. .family = NFPROTO_IPV6,
  106. .match = frag_mt6,
  107. .matchsize = sizeof(struct ip6t_frag),
  108. .checkentry = frag_mt6_check,
  109. .me = THIS_MODULE,
  110. };
  111. static int __init frag_mt6_init(void)
  112. {
  113. return xt_register_match(&frag_mt6_reg);
  114. }
  115. static void __exit frag_mt6_exit(void)
  116. {
  117. xt_unregister_match(&frag_mt6_reg);
  118. }
  119. module_init(frag_mt6_init);
  120. module_exit(frag_mt6_exit);