xt_tcpudp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #define FWINVTCP(bool, invflg) ((bool) ^ !!(tcpinfo->invflags & (invflg)))
  75. th = skb_header_pointer(skb, par->thoff, sizeof(_tcph), &_tcph);
  76. if (th == NULL) {
  77. /* We've been asked to examine this packet, and we
  78. can't. Hence, no choice but to drop. */
  79. pr_debug("Dropping evil TCP offset=0 tinygram.\n");
  80. par->hotdrop = true;
  81. return false;
  82. }
  83. if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],
  84. ntohs(th->source),
  85. !!(tcpinfo->invflags & XT_TCP_INV_SRCPT)))
  86. return false;
  87. if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],
  88. ntohs(th->dest),
  89. !!(tcpinfo->invflags & XT_TCP_INV_DSTPT)))
  90. return false;
  91. if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask)
  92. == tcpinfo->flg_cmp,
  93. XT_TCP_INV_FLAGS))
  94. return false;
  95. if (tcpinfo->option) {
  96. if (th->doff * 4 < sizeof(_tcph)) {
  97. par->hotdrop = true;
  98. return false;
  99. }
  100. if (!tcp_find_option(tcpinfo->option, skb, par->thoff,
  101. th->doff*4 - sizeof(_tcph),
  102. tcpinfo->invflags & XT_TCP_INV_OPTION,
  103. &par->hotdrop))
  104. return false;
  105. }
  106. return true;
  107. }
  108. static int tcp_mt_check(const struct xt_mtchk_param *par)
  109. {
  110. const struct xt_tcp *tcpinfo = par->matchinfo;
  111. /* Must specify no unknown invflags */
  112. return (tcpinfo->invflags & ~XT_TCP_INV_MASK) ? -EINVAL : 0;
  113. }
  114. static bool udp_mt(const struct sk_buff *skb, struct xt_action_param *par)
  115. {
  116. const struct udphdr *uh;
  117. struct udphdr _udph;
  118. const struct xt_udp *udpinfo = par->matchinfo;
  119. /* Must not be a fragment. */
  120. if (par->fragoff != 0)
  121. return false;
  122. uh = skb_header_pointer(skb, par->thoff, sizeof(_udph), &_udph);
  123. if (uh == NULL) {
  124. /* We've been asked to examine this packet, and we
  125. can't. Hence, no choice but to drop. */
  126. pr_debug("Dropping evil UDP tinygram.\n");
  127. par->hotdrop = true;
  128. return false;
  129. }
  130. return port_match(udpinfo->spts[0], udpinfo->spts[1],
  131. ntohs(uh->source),
  132. !!(udpinfo->invflags & XT_UDP_INV_SRCPT))
  133. && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
  134. ntohs(uh->dest),
  135. !!(udpinfo->invflags & XT_UDP_INV_DSTPT));
  136. }
  137. static int udp_mt_check(const struct xt_mtchk_param *par)
  138. {
  139. const struct xt_udp *udpinfo = par->matchinfo;
  140. /* Must specify no unknown invflags */
  141. return (udpinfo->invflags & ~XT_UDP_INV_MASK) ? -EINVAL : 0;
  142. }
  143. static struct xt_match tcpudp_mt_reg[] __read_mostly = {
  144. {
  145. .name = "tcp",
  146. .family = NFPROTO_IPV4,
  147. .checkentry = tcp_mt_check,
  148. .match = tcp_mt,
  149. .matchsize = sizeof(struct xt_tcp),
  150. .proto = IPPROTO_TCP,
  151. .me = THIS_MODULE,
  152. },
  153. {
  154. .name = "tcp",
  155. .family = NFPROTO_IPV6,
  156. .checkentry = tcp_mt_check,
  157. .match = tcp_mt,
  158. .matchsize = sizeof(struct xt_tcp),
  159. .proto = IPPROTO_TCP,
  160. .me = THIS_MODULE,
  161. },
  162. {
  163. .name = "udp",
  164. .family = NFPROTO_IPV4,
  165. .checkentry = udp_mt_check,
  166. .match = udp_mt,
  167. .matchsize = sizeof(struct xt_udp),
  168. .proto = IPPROTO_UDP,
  169. .me = THIS_MODULE,
  170. },
  171. {
  172. .name = "udp",
  173. .family = NFPROTO_IPV6,
  174. .checkentry = udp_mt_check,
  175. .match = udp_mt,
  176. .matchsize = sizeof(struct xt_udp),
  177. .proto = IPPROTO_UDP,
  178. .me = THIS_MODULE,
  179. },
  180. {
  181. .name = "udplite",
  182. .family = NFPROTO_IPV4,
  183. .checkentry = udp_mt_check,
  184. .match = udp_mt,
  185. .matchsize = sizeof(struct xt_udp),
  186. .proto = IPPROTO_UDPLITE,
  187. .me = THIS_MODULE,
  188. },
  189. {
  190. .name = "udplite",
  191. .family = NFPROTO_IPV6,
  192. .checkentry = udp_mt_check,
  193. .match = udp_mt,
  194. .matchsize = sizeof(struct xt_udp),
  195. .proto = IPPROTO_UDPLITE,
  196. .me = THIS_MODULE,
  197. },
  198. };
  199. static int __init tcpudp_mt_init(void)
  200. {
  201. return xt_register_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
  202. }
  203. static void __exit tcpudp_mt_exit(void)
  204. {
  205. xt_unregister_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
  206. }
  207. module_init(tcpudp_mt_init);
  208. module_exit(tcpudp_mt_exit);