You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.8 KiB

  1. /*
  2. * Copyright (C) 2017 jianhui zhao <jianhuizhao329@gmail.com>
  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. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/version.h>
  11. #include <linux/ip.h>
  12. #include <linux/tcp.h>
  13. #include <linux/udp.h>
  14. #include <net/netfilter/nf_nat.h>
  15. #include <net/netfilter/nf_nat_l3proto.h>
  16. #include "utils.h"
  17. #include "config.h"
  18. #define IPS_HIJACKED (1 << 31)
  19. #define IPS_ALLOWED (1 << 30)
  20. static u32 wd_nf_nat_setup_info(void *priv, struct sk_buff *skb,
  21. const struct nf_hook_state *state, struct nf_conn *ct)
  22. {
  23. struct config *conf = get_config();
  24. struct tcphdr *tcph = tcp_hdr(skb);
  25. union nf_conntrack_man_proto proto;
  26. struct nf_nat_range newrange;
  27. static uint16_t PORT_80 = htons(80);
  28. proto.tcp.port = (tcph->dest == PORT_80) ? htons(conf->port) : htons(conf->ssl_port);
  29. newrange.flags = NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED;
  30. newrange.min_addr.ip = newrange.max_addr.ip = conf->interface_ipaddr;
  31. newrange.min_proto = newrange.max_proto = proto;
  32. ct->status |= IPS_HIJACKED;
  33. return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_DST);
  34. }
  35. static u32 wifidog_hook(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
  36. {
  37. struct config *conf = get_config();
  38. struct iphdr *iph = ip_hdr(skb);
  39. struct nf_conn *ct;
  40. struct tcphdr *tcph;
  41. struct udphdr *udph;
  42. enum ip_conntrack_info ctinfo;
  43. static uint16_t PORT_80 = htons(80); /* http */
  44. static uint16_t PORT_443 = htons(443); /* https */
  45. static uint16_t PORT_67 = htons(67); /* dhcp */
  46. static uint16_t PORT_53 = htons(53); /* dns */
  47. if (unlikely(!conf->enabled))
  48. return NF_ACCEPT;
  49. if (state->in->ifindex != conf->interface_ifindex)
  50. return NF_ACCEPT;
  51. /* Accept broadcast */
  52. if (skb->pkt_type == PACKET_BROADCAST || skb->pkt_type == PACKET_MULTICAST)
  53. return NF_ACCEPT;
  54. /* Accept all to local area networks */
  55. if ((iph->daddr | ~conf->interface_mask) == conf->interface_broadcast)
  56. return NF_ACCEPT;
  57. ct = nf_ct_get(skb, &ctinfo);
  58. if (!ct || (ct->status & IPS_ALLOWED))
  59. return NF_ACCEPT;
  60. if (ct->status & IPS_HIJACKED) {
  61. if (is_allowed_mac(skb, state)) {
  62. /* Avoid duplication of authentication */
  63. nf_reset(skb);
  64. nf_ct_kill(ct);
  65. }
  66. return NF_ACCEPT;
  67. } else if (ctinfo == IP_CT_NEW && (is_allowed_dest_ip(skb, state) || is_allowed_mac(skb, state))) {
  68. ct->status |= IPS_ALLOWED;
  69. return NF_ACCEPT;
  70. }
  71. switch (iph->protocol) {
  72. case IPPROTO_TCP:
  73. tcph = tcp_hdr(skb);
  74. if(tcph->dest == PORT_53 || tcph->dest == PORT_67) {
  75. ct->status |= IPS_ALLOWED;
  76. return NF_ACCEPT;
  77. }
  78. if (tcph->dest == PORT_80 || tcph->dest == PORT_443)
  79. goto redirect;
  80. else
  81. return NF_DROP;
  82. case IPPROTO_UDP:
  83. udph = udp_hdr(skb);
  84. if(udph->dest == PORT_53 || udph->dest == PORT_67) {
  85. ct->status |= IPS_ALLOWED;
  86. return NF_ACCEPT;
  87. }
  88. return NF_DROP;
  89. default:
  90. ct->status |= IPS_ALLOWED;
  91. return NF_ACCEPT;
  92. }
  93. redirect:
  94. /* all packets from unknown client are dropped */
  95. if (ctinfo != IP_CT_NEW || (ct->status & IPS_DST_NAT_DONE)) {
  96. pr_debug("dropping packets of suspect stream, src:%pI4, dst:%pI4\n", &iph->saddr, &iph->daddr);
  97. return NF_DROP;
  98. }
  99. return nf_nat_ipv4_in(priv, skb, state, wd_nf_nat_setup_info);
  100. }
  101. static struct nf_hook_ops wifidog_ops[] __read_mostly = {
  102. {
  103. .hook = wifidog_hook,
  104. .pf = PF_INET,
  105. .hooknum = NF_INET_PRE_ROUTING,
  106. .priority = NF_IP_PRI_CONNTRACK + 1 /* after conntrack */
  107. }
  108. };
  109. static int __init wifidog_init(void)
  110. {
  111. int ret;
  112. ret = init_config();
  113. if (ret)
  114. return ret;
  115. #if LINUX_VERSION_CODE > KERNEL_VERSION(4, 12, 14)
  116. ret = nf_register_net_hooks(&init_net, wifidog_ops, ARRAY_SIZE(wifidog_ops));
  117. #else
  118. ret = nf_register_hooks(wifidog_ops, ARRAY_SIZE(wifidog_ops));
  119. #endif
  120. if (ret < 0) {
  121. pr_err("can't register hook\n");
  122. goto remove_config;
  123. }
  124. pr_info("kmod of wifidog is started\n");
  125. return 0;
  126. remove_config:
  127. deinit_config();
  128. return ret;
  129. }
  130. static void __exit wifidog_exit(void)
  131. {
  132. deinit_config();
  133. #if LINUX_VERSION_CODE > KERNEL_VERSION(4, 12, 14)
  134. nf_unregister_net_hooks(&init_net, wifidog_ops, ARRAY_SIZE(wifidog_ops));
  135. #else
  136. nf_unregister_hooks(wifidog_ops, ARRAY_SIZE(wifidog_ops));
  137. #endif
  138. pr_info("kmod of wifidog-ng is stop\n");
  139. }
  140. module_init(wifidog_init);
  141. module_exit(wifidog_exit);
  142. MODULE_AUTHOR("jianhui zhao <jianhuizhao329@gmail.com>");
  143. MODULE_LICENSE("GPL");