xt_tcpudp.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <linux/types.h>
  3. #include <linux/module.h>
  4. #include <net/ip.h>
  5. #include <linux/ipv6.h>
  6. #include <net/ipv6.h>
  7. #include <net/tcp.h>
  8. #include <net/udp.h>
  9. #include <linux/netfilter/x_tables.h>
  10. #include <linux/netfilter/xt_tcpudp.h>
  11. #include <linux/netfilter_ipv4/ip_tables.h>
  12. #include <linux/netfilter_ipv6/ip6_tables.h>
  13. MODULE_DESCRIPTION("Xtables: TCP, UDP and UDP-Lite match");
  14. MODULE_LICENSE("GPL");
  15. MODULE_ALIAS("xt_tcp");
  16. MODULE_ALIAS("xt_udp");
  17. MODULE_ALIAS("ipt_udp");
  18. MODULE_ALIAS("ipt_tcp");
  19. MODULE_ALIAS("ip6t_udp");
  20. MODULE_ALIAS("ip6t_tcp");
  21. /* Returns 1 if the port is matched by the range, 0 otherwise */
  22. static inline bool
  23. port_match(u_int16_t min, u_int16_t max, u_int16_t port, bool invert)
  24. {
  25. return (port >= min && port <= max) ^ invert;
  26. }
  27. static bool
  28. tcp_find_option(u_int8_t option,
  29. const struct sk_buff *skb,
  30. unsigned int protoff,
  31. unsigned int optlen,
  32. bool invert,
  33. bool *hotdrop)
  34. {
  35. /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
  36. const u_int8_t *op;
  37. u_int8_t _opt[60 - sizeof(struct tcphdr)];
  38. unsigned int i;
  39. pr_debug("finding option\n");
  40. if (!optlen)
  41. return invert;
  42. /* If we don't have the whole header, drop packet. */
  43. op = skb_header_pointer(skb, protoff + sizeof(struct tcphdr),
  44. optlen, _opt);
  45. if (op == NULL) {
  46. *hotdrop = true;
  47. return false;
  48. }
  49. for (i = 0; i < optlen; ) {
  50. if (op[i] == option) return !invert;
  51. if (op[i] < 2) i++;
  52. else i += op[i+1]?:1;
  53. }
  54. return invert;
  55. }
  56. static bool tcp_mt(const struct sk_buff *skb, struct xt_action_param *par)
  57. {
  58. const struct tcphdr *th;
  59. struct tcphdr _tcph;
  60. const struct xt_tcp *tcpinfo = par->matchinfo;
  61. if (par->fragoff != 0) {
  62. /* To quote Alan:
  63. Don't allow a fragment of TCP 8 bytes in. Nobody normal
  64. causes this. Its a cracker trying to break in by doing a
  65. flag overwrite to pass the direction checks.
  66. */
  67. if (par->fragoff == 1) {
  68. pr_debug("Dropping evil TCP offset=1 frag.\n");
  69. par->hotdrop = true;
  70. }
  71. /* Must not be a fragment. */
  72. return false;
  73. }
  74. th = skb_header_pointer(skb, par->thoff, sizeof(_tcph), &_tcph);
  75. if (th == NULL) {
  76. /* We've been asked to examine this packet, and we
  77. can't. Hence, no choice but to drop. */
  78. pr_debug("Dropping evil TCP offset=0 tinygram.\n");
  79. par->hotdrop = true;
  80. return false;
  81. }
  82. if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],
  83. ntohs(th->source),
  84. !!(tcpinfo->invflags & XT_TCP_INV_SRCPT)))
  85. return false;
  86. if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],
  87. ntohs(th->dest),
  88. !!(tcpinfo->invflags & XT_TCP_INV_DSTPT)))
  89. return false;
  90. if (!NF_INVF(tcpinfo, XT_TCP_INV_FLAGS,
  91. (((unsigned char *)th)[13] & tcpinfo->flg_mask) == tcpinfo->flg_cmp))
  92. return false;
  93. if (tcpinfo->option) {
  94. if (th->doff * 4 < sizeof(_tcph)) {
  95. par->hotdrop = true;
  96. return false;
  97. }
  98. if (!tcp_find_option(tcpinfo->option, skb, par->thoff,
  99. th->doff*4 - sizeof(_tcph),
  100. tcpinfo->invflags & XT_TCP_INV_OPTION,
  101. &par->hotdrop))
  102. return false;
  103. }
  104. return true;
  105. }
  106. static int tcp_mt_check(const struct xt_mtchk_param *par)
  107. {
  108. const struct xt_tcp *tcpinfo = par->matchinfo;
  109. /* Must specify no unknown invflags */
  110. return (tcpinfo->invflags & ~XT_TCP_INV_MASK) ? -EINVAL : 0;
  111. }
  112. static bool udp_mt(const struct sk_buff *skb, struct xt_action_param *par)
  113. {
  114. const struct udphdr *uh;
  115. struct udphdr _udph;
  116. const struct xt_udp *udpinfo = par->matchinfo;
  117. /* Must not be a fragment. */
  118. if (par->fragoff != 0)
  119. return false;
  120. uh = skb_header_pointer(skb, par->thoff, sizeof(_udph), &_udph);
  121. if (uh == NULL) {
  122. /* We've been asked to examine this packet, and we
  123. can't. Hence, no choice but to drop. */
  124. pr_debug("Dropping evil UDP tinygram.\n");
  125. par->hotdrop = true;
  126. return false;
  127. }
  128. return port_match(udpinfo->spts[0], udpinfo->spts[1],
  129. ntohs(uh->source),
  130. !!(udpinfo->invflags & XT_UDP_INV_SRCPT))
  131. && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
  132. ntohs(uh->dest),
  133. !!(udpinfo->invflags & XT_UDP_INV_DSTPT));
  134. }
  135. static int udp_mt_check(const struct xt_mtchk_param *par)
  136. {
  137. const struct xt_udp *udpinfo = par->matchinfo;
  138. /* Must specify no unknown invflags */
  139. return (udpinfo->invflags & ~XT_UDP_INV_MASK) ? -EINVAL : 0;
  140. }
  141. static struct xt_match tcpudp_mt_reg[] __read_mostly = {
  142. {
  143. .name = "tcp",
  144. .family = NFPROTO_IPV4,
  145. .checkentry = tcp_mt_check,
  146. .match = tcp_mt,
  147. .matchsize = sizeof(struct xt_tcp),
  148. .proto = IPPROTO_TCP,
  149. .me = THIS_MODULE,
  150. },
  151. {
  152. .name = "tcp",
  153. .family = NFPROTO_IPV6,
  154. .checkentry = tcp_mt_check,
  155. .match = tcp_mt,
  156. .matchsize = sizeof(struct xt_tcp),
  157. .proto = IPPROTO_TCP,
  158. .me = THIS_MODULE,
  159. },
  160. {
  161. .name = "udp",
  162. .family = NFPROTO_IPV4,
  163. .checkentry = udp_mt_check,
  164. .match = udp_mt,
  165. .matchsize = sizeof(struct xt_udp),
  166. .proto = IPPROTO_UDP,
  167. .me = THIS_MODULE,
  168. },
  169. {
  170. .name = "udp",
  171. .family = NFPROTO_IPV6,
  172. .checkentry = udp_mt_check,
  173. .match = udp_mt,
  174. .matchsize = sizeof(struct xt_udp),
  175. .proto = IPPROTO_UDP,
  176. .me = THIS_MODULE,
  177. },
  178. {
  179. .name = "udplite",
  180. .family = NFPROTO_IPV4,
  181. .checkentry = udp_mt_check,
  182. .match = udp_mt,
  183. .matchsize = sizeof(struct xt_udp),
  184. .proto = IPPROTO_UDPLITE,
  185. .me = THIS_MODULE,
  186. },
  187. {
  188. .name = "udplite",
  189. .family = NFPROTO_IPV6,
  190. .checkentry = udp_mt_check,
  191. .match = udp_mt,
  192. .matchsize = sizeof(struct xt_udp),
  193. .proto = IPPROTO_UDPLITE,
  194. .me = THIS_MODULE,
  195. },
  196. };
  197. static int __init tcpudp_mt_init(void)
  198. {
  199. return xt_register_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
  200. }
  201. static void __exit tcpudp_mt_exit(void)
  202. {
  203. xt_unregister_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
  204. }
  205. module_init(tcpudp_mt_init);
  206. module_exit(tcpudp_mt_exit);