xt_multiport.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Kernel module to match one of a list of TCP/UDP(-Lite)/SCTP/DCCP ports:
  2. ports are in the same place so we can treat them as equal. */
  3. /* (C) 1999-2001 Paul `Rusty' Russell
  4. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/udp.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/in.h>
  16. #include <linux/netfilter/xt_multiport.h>
  17. #include <linux/netfilter/x_tables.h>
  18. #include <linux/netfilter_ipv4/ip_tables.h>
  19. #include <linux/netfilter_ipv6/ip6_tables.h>
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  22. MODULE_DESCRIPTION("Xtables: multiple port matching for TCP, UDP, UDP-Lite, SCTP and DCCP");
  23. MODULE_ALIAS("ipt_multiport");
  24. MODULE_ALIAS("ip6t_multiport");
  25. /* Returns 1 if the port is matched by the test, 0 otherwise. */
  26. static inline bool
  27. ports_match_v1(const struct xt_multiport_v1 *minfo,
  28. u_int16_t src, u_int16_t dst)
  29. {
  30. unsigned int i;
  31. u_int16_t s, e;
  32. for (i = 0; i < minfo->count; i++) {
  33. s = minfo->ports[i];
  34. if (minfo->pflags[i]) {
  35. /* range port matching */
  36. e = minfo->ports[++i];
  37. pr_debug("src or dst matches with %d-%d?\n", s, e);
  38. if (minfo->flags == XT_MULTIPORT_SOURCE
  39. && src >= s && src <= e)
  40. return true ^ minfo->invert;
  41. if (minfo->flags == XT_MULTIPORT_DESTINATION
  42. && dst >= s && dst <= e)
  43. return true ^ minfo->invert;
  44. if (minfo->flags == XT_MULTIPORT_EITHER
  45. && ((dst >= s && dst <= e)
  46. || (src >= s && src <= e)))
  47. return true ^ minfo->invert;
  48. } else {
  49. /* exact port matching */
  50. pr_debug("src or dst matches with %d?\n", s);
  51. if (minfo->flags == XT_MULTIPORT_SOURCE
  52. && src == s)
  53. return true ^ minfo->invert;
  54. if (minfo->flags == XT_MULTIPORT_DESTINATION
  55. && dst == s)
  56. return true ^ minfo->invert;
  57. if (minfo->flags == XT_MULTIPORT_EITHER
  58. && (src == s || dst == s))
  59. return true ^ minfo->invert;
  60. }
  61. }
  62. return minfo->invert;
  63. }
  64. static bool
  65. multiport_mt(const struct sk_buff *skb, struct xt_action_param *par)
  66. {
  67. const __be16 *pptr;
  68. __be16 _ports[2];
  69. const struct xt_multiport_v1 *multiinfo = par->matchinfo;
  70. if (par->fragoff != 0)
  71. return false;
  72. pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports);
  73. if (pptr == NULL) {
  74. /* We've been asked to examine this packet, and we
  75. * can't. Hence, no choice but to drop.
  76. */
  77. pr_debug("Dropping evil offset=0 tinygram.\n");
  78. par->hotdrop = true;
  79. return false;
  80. }
  81. return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1]));
  82. }
  83. static inline bool
  84. check(u_int16_t proto,
  85. u_int8_t ip_invflags,
  86. u_int8_t match_flags,
  87. u_int8_t count)
  88. {
  89. /* Must specify supported protocol, no unknown flags or bad count */
  90. return (proto == IPPROTO_TCP || proto == IPPROTO_UDP
  91. || proto == IPPROTO_UDPLITE
  92. || proto == IPPROTO_SCTP || proto == IPPROTO_DCCP)
  93. && !(ip_invflags & XT_INV_PROTO)
  94. && (match_flags == XT_MULTIPORT_SOURCE
  95. || match_flags == XT_MULTIPORT_DESTINATION
  96. || match_flags == XT_MULTIPORT_EITHER)
  97. && count <= XT_MULTI_PORTS;
  98. }
  99. static int multiport_mt_check(const struct xt_mtchk_param *par)
  100. {
  101. const struct ipt_ip *ip = par->entryinfo;
  102. const struct xt_multiport_v1 *multiinfo = par->matchinfo;
  103. return check(ip->proto, ip->invflags, multiinfo->flags,
  104. multiinfo->count) ? 0 : -EINVAL;
  105. }
  106. static int multiport_mt6_check(const struct xt_mtchk_param *par)
  107. {
  108. const struct ip6t_ip6 *ip = par->entryinfo;
  109. const struct xt_multiport_v1 *multiinfo = par->matchinfo;
  110. return check(ip->proto, ip->invflags, multiinfo->flags,
  111. multiinfo->count) ? 0 : -EINVAL;
  112. }
  113. static struct xt_match multiport_mt_reg[] __read_mostly = {
  114. {
  115. .name = "multiport",
  116. .family = NFPROTO_IPV4,
  117. .revision = 1,
  118. .checkentry = multiport_mt_check,
  119. .match = multiport_mt,
  120. .matchsize = sizeof(struct xt_multiport_v1),
  121. .me = THIS_MODULE,
  122. },
  123. {
  124. .name = "multiport",
  125. .family = NFPROTO_IPV6,
  126. .revision = 1,
  127. .checkentry = multiport_mt6_check,
  128. .match = multiport_mt,
  129. .matchsize = sizeof(struct xt_multiport_v1),
  130. .me = THIS_MODULE,
  131. },
  132. };
  133. static int __init multiport_mt_init(void)
  134. {
  135. return xt_register_matches(multiport_mt_reg,
  136. ARRAY_SIZE(multiport_mt_reg));
  137. }
  138. static void __exit multiport_mt_exit(void)
  139. {
  140. xt_unregister_matches(multiport_mt_reg, ARRAY_SIZE(multiport_mt_reg));
  141. }
  142. module_init(multiport_mt_init);
  143. module_exit(multiport_mt_exit);