xt_osf.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright (c) 2003+ Evgeniy Polyakov <zbr@ioremap.net>
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/if.h>
  22. #include <linux/inetdevice.h>
  23. #include <linux/ip.h>
  24. #include <linux/list.h>
  25. #include <linux/rculist.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/slab.h>
  28. #include <linux/tcp.h>
  29. #include <net/ip.h>
  30. #include <net/tcp.h>
  31. #include <linux/netfilter/nfnetlink.h>
  32. #include <linux/netfilter/x_tables.h>
  33. #include <net/netfilter/nf_log.h>
  34. #include <linux/netfilter/xt_osf.h>
  35. struct xt_osf_finger {
  36. struct rcu_head rcu_head;
  37. struct list_head finger_entry;
  38. struct xt_osf_user_finger finger;
  39. };
  40. enum osf_fmatch_states {
  41. /* Packet does not match the fingerprint */
  42. FMATCH_WRONG = 0,
  43. /* Packet matches the fingerprint */
  44. FMATCH_OK,
  45. /* Options do not match the fingerprint, but header does */
  46. FMATCH_OPT_WRONG,
  47. };
  48. /*
  49. * Indexed by dont-fragment bit.
  50. * It is the only constant value in the fingerprint.
  51. */
  52. static struct list_head xt_osf_fingers[2];
  53. static const struct nla_policy xt_osf_policy[OSF_ATTR_MAX + 1] = {
  54. [OSF_ATTR_FINGER] = { .len = sizeof(struct xt_osf_user_finger) },
  55. };
  56. static int xt_osf_add_callback(struct sock *ctnl, struct sk_buff *skb,
  57. const struct nlmsghdr *nlh,
  58. const struct nlattr * const osf_attrs[])
  59. {
  60. struct xt_osf_user_finger *f;
  61. struct xt_osf_finger *kf = NULL, *sf;
  62. int err = 0;
  63. if (!osf_attrs[OSF_ATTR_FINGER])
  64. return -EINVAL;
  65. if (!(nlh->nlmsg_flags & NLM_F_CREATE))
  66. return -EINVAL;
  67. f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
  68. kf = kmalloc(sizeof(struct xt_osf_finger), GFP_KERNEL);
  69. if (!kf)
  70. return -ENOMEM;
  71. memcpy(&kf->finger, f, sizeof(struct xt_osf_user_finger));
  72. list_for_each_entry(sf, &xt_osf_fingers[!!f->df], finger_entry) {
  73. if (memcmp(&sf->finger, f, sizeof(struct xt_osf_user_finger)))
  74. continue;
  75. kfree(kf);
  76. kf = NULL;
  77. if (nlh->nlmsg_flags & NLM_F_EXCL)
  78. err = -EEXIST;
  79. break;
  80. }
  81. /*
  82. * We are protected by nfnl mutex.
  83. */
  84. if (kf)
  85. list_add_tail_rcu(&kf->finger_entry, &xt_osf_fingers[!!f->df]);
  86. return err;
  87. }
  88. static int xt_osf_remove_callback(struct sock *ctnl, struct sk_buff *skb,
  89. const struct nlmsghdr *nlh,
  90. const struct nlattr * const osf_attrs[])
  91. {
  92. struct xt_osf_user_finger *f;
  93. struct xt_osf_finger *sf;
  94. int err = -ENOENT;
  95. if (!osf_attrs[OSF_ATTR_FINGER])
  96. return -EINVAL;
  97. f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
  98. list_for_each_entry(sf, &xt_osf_fingers[!!f->df], finger_entry) {
  99. if (memcmp(&sf->finger, f, sizeof(struct xt_osf_user_finger)))
  100. continue;
  101. /*
  102. * We are protected by nfnl mutex.
  103. */
  104. list_del_rcu(&sf->finger_entry);
  105. kfree_rcu(sf, rcu_head);
  106. err = 0;
  107. break;
  108. }
  109. return err;
  110. }
  111. static const struct nfnl_callback xt_osf_nfnetlink_callbacks[OSF_MSG_MAX] = {
  112. [OSF_MSG_ADD] = {
  113. .call = xt_osf_add_callback,
  114. .attr_count = OSF_ATTR_MAX,
  115. .policy = xt_osf_policy,
  116. },
  117. [OSF_MSG_REMOVE] = {
  118. .call = xt_osf_remove_callback,
  119. .attr_count = OSF_ATTR_MAX,
  120. .policy = xt_osf_policy,
  121. },
  122. };
  123. static const struct nfnetlink_subsystem xt_osf_nfnetlink = {
  124. .name = "osf",
  125. .subsys_id = NFNL_SUBSYS_OSF,
  126. .cb_count = OSF_MSG_MAX,
  127. .cb = xt_osf_nfnetlink_callbacks,
  128. };
  129. static inline int xt_osf_ttl(const struct sk_buff *skb, const struct xt_osf_info *info,
  130. unsigned char f_ttl)
  131. {
  132. const struct iphdr *ip = ip_hdr(skb);
  133. if (info->flags & XT_OSF_TTL) {
  134. if (info->ttl == XT_OSF_TTL_TRUE)
  135. return ip->ttl == f_ttl;
  136. if (info->ttl == XT_OSF_TTL_NOCHECK)
  137. return 1;
  138. else if (ip->ttl <= f_ttl)
  139. return 1;
  140. else {
  141. struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
  142. int ret = 0;
  143. for_ifa(in_dev) {
  144. if (inet_ifa_match(ip->saddr, ifa)) {
  145. ret = (ip->ttl == f_ttl);
  146. break;
  147. }
  148. }
  149. endfor_ifa(in_dev);
  150. return ret;
  151. }
  152. }
  153. return ip->ttl == f_ttl;
  154. }
  155. static bool
  156. xt_osf_match_packet(const struct sk_buff *skb, struct xt_action_param *p)
  157. {
  158. const struct xt_osf_info *info = p->matchinfo;
  159. const struct iphdr *ip = ip_hdr(skb);
  160. const struct tcphdr *tcp;
  161. struct tcphdr _tcph;
  162. int fmatch = FMATCH_WRONG, fcount = 0;
  163. unsigned int optsize = 0, check_WSS = 0;
  164. u16 window, totlen, mss = 0;
  165. bool df;
  166. const unsigned char *optp = NULL, *_optp = NULL;
  167. unsigned char opts[MAX_IPOPTLEN];
  168. const struct xt_osf_finger *kf;
  169. const struct xt_osf_user_finger *f;
  170. struct net *net = dev_net(p->in ? p->in : p->out);
  171. if (!info)
  172. return false;
  173. tcp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(struct tcphdr), &_tcph);
  174. if (!tcp)
  175. return false;
  176. if (!tcp->syn)
  177. return false;
  178. totlen = ntohs(ip->tot_len);
  179. df = ntohs(ip->frag_off) & IP_DF;
  180. window = ntohs(tcp->window);
  181. if (tcp->doff * 4 > sizeof(struct tcphdr)) {
  182. optsize = tcp->doff * 4 - sizeof(struct tcphdr);
  183. _optp = optp = skb_header_pointer(skb, ip_hdrlen(skb) +
  184. sizeof(struct tcphdr), optsize, opts);
  185. }
  186. rcu_read_lock();
  187. list_for_each_entry_rcu(kf, &xt_osf_fingers[df], finger_entry) {
  188. f = &kf->finger;
  189. if (!(info->flags & XT_OSF_LOG) && strcmp(info->genre, f->genre))
  190. continue;
  191. optp = _optp;
  192. fmatch = FMATCH_WRONG;
  193. if (totlen == f->ss && xt_osf_ttl(skb, info, f->ttl)) {
  194. int foptsize, optnum;
  195. /*
  196. * Should not happen if userspace parser was written correctly.
  197. */
  198. if (f->wss.wc >= OSF_WSS_MAX)
  199. continue;
  200. /* Check options */
  201. foptsize = 0;
  202. for (optnum = 0; optnum < f->opt_num; ++optnum)
  203. foptsize += f->opt[optnum].length;
  204. if (foptsize > MAX_IPOPTLEN ||
  205. optsize > MAX_IPOPTLEN ||
  206. optsize != foptsize)
  207. continue;
  208. check_WSS = f->wss.wc;
  209. for (optnum = 0; optnum < f->opt_num; ++optnum) {
  210. if (f->opt[optnum].kind == (*optp)) {
  211. __u32 len = f->opt[optnum].length;
  212. const __u8 *optend = optp + len;
  213. int loop_cont = 0;
  214. fmatch = FMATCH_OK;
  215. switch (*optp) {
  216. case OSFOPT_MSS:
  217. mss = optp[3];
  218. mss <<= 8;
  219. mss |= optp[2];
  220. mss = ntohs((__force __be16)mss);
  221. break;
  222. case OSFOPT_TS:
  223. loop_cont = 1;
  224. break;
  225. }
  226. optp = optend;
  227. } else
  228. fmatch = FMATCH_OPT_WRONG;
  229. if (fmatch != FMATCH_OK)
  230. break;
  231. }
  232. if (fmatch != FMATCH_OPT_WRONG) {
  233. fmatch = FMATCH_WRONG;
  234. switch (check_WSS) {
  235. case OSF_WSS_PLAIN:
  236. if (f->wss.val == 0 || window == f->wss.val)
  237. fmatch = FMATCH_OK;
  238. break;
  239. case OSF_WSS_MSS:
  240. /*
  241. * Some smart modems decrease mangle MSS to
  242. * SMART_MSS_2, so we check standard, decreased
  243. * and the one provided in the fingerprint MSS
  244. * values.
  245. */
  246. #define SMART_MSS_1 1460
  247. #define SMART_MSS_2 1448
  248. if (window == f->wss.val * mss ||
  249. window == f->wss.val * SMART_MSS_1 ||
  250. window == f->wss.val * SMART_MSS_2)
  251. fmatch = FMATCH_OK;
  252. break;
  253. case OSF_WSS_MTU:
  254. if (window == f->wss.val * (mss + 40) ||
  255. window == f->wss.val * (SMART_MSS_1 + 40) ||
  256. window == f->wss.val * (SMART_MSS_2 + 40))
  257. fmatch = FMATCH_OK;
  258. break;
  259. case OSF_WSS_MODULO:
  260. if ((window % f->wss.val) == 0)
  261. fmatch = FMATCH_OK;
  262. break;
  263. }
  264. }
  265. if (fmatch != FMATCH_OK)
  266. continue;
  267. fcount++;
  268. if (info->flags & XT_OSF_LOG)
  269. nf_log_packet(net, p->family, p->hooknum, skb,
  270. p->in, p->out, NULL,
  271. "%s [%s:%s] : %pI4:%d -> %pI4:%d hops=%d\n",
  272. f->genre, f->version, f->subtype,
  273. &ip->saddr, ntohs(tcp->source),
  274. &ip->daddr, ntohs(tcp->dest),
  275. f->ttl - ip->ttl);
  276. if ((info->flags & XT_OSF_LOG) &&
  277. info->loglevel == XT_OSF_LOGLEVEL_FIRST)
  278. break;
  279. }
  280. }
  281. rcu_read_unlock();
  282. if (!fcount && (info->flags & XT_OSF_LOG))
  283. nf_log_packet(net, p->family, p->hooknum, skb, p->in,
  284. p->out, NULL,
  285. "Remote OS is not known: %pI4:%u -> %pI4:%u\n",
  286. &ip->saddr, ntohs(tcp->source),
  287. &ip->daddr, ntohs(tcp->dest));
  288. if (fcount)
  289. fmatch = FMATCH_OK;
  290. return fmatch == FMATCH_OK;
  291. }
  292. static struct xt_match xt_osf_match = {
  293. .name = "osf",
  294. .revision = 0,
  295. .family = NFPROTO_IPV4,
  296. .proto = IPPROTO_TCP,
  297. .hooks = (1 << NF_INET_LOCAL_IN) |
  298. (1 << NF_INET_PRE_ROUTING) |
  299. (1 << NF_INET_FORWARD),
  300. .match = xt_osf_match_packet,
  301. .matchsize = sizeof(struct xt_osf_info),
  302. .me = THIS_MODULE,
  303. };
  304. static int __init xt_osf_init(void)
  305. {
  306. int err = -EINVAL;
  307. int i;
  308. for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i)
  309. INIT_LIST_HEAD(&xt_osf_fingers[i]);
  310. err = nfnetlink_subsys_register(&xt_osf_nfnetlink);
  311. if (err < 0) {
  312. pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err);
  313. goto err_out_exit;
  314. }
  315. err = xt_register_match(&xt_osf_match);
  316. if (err) {
  317. pr_err("Failed to register OS fingerprint "
  318. "matching module (%d)\n", err);
  319. goto err_out_remove;
  320. }
  321. return 0;
  322. err_out_remove:
  323. nfnetlink_subsys_unregister(&xt_osf_nfnetlink);
  324. err_out_exit:
  325. return err;
  326. }
  327. static void __exit xt_osf_fini(void)
  328. {
  329. struct xt_osf_finger *f;
  330. int i;
  331. nfnetlink_subsys_unregister(&xt_osf_nfnetlink);
  332. xt_unregister_match(&xt_osf_match);
  333. rcu_read_lock();
  334. for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i) {
  335. list_for_each_entry_rcu(f, &xt_osf_fingers[i], finger_entry) {
  336. list_del_rcu(&f->finger_entry);
  337. kfree_rcu(f, rcu_head);
  338. }
  339. }
  340. rcu_read_unlock();
  341. rcu_barrier();
  342. }
  343. module_init(xt_osf_init);
  344. module_exit(xt_osf_fini);
  345. MODULE_LICENSE("GPL");
  346. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  347. MODULE_DESCRIPTION("Passive OS fingerprint matching.");
  348. MODULE_ALIAS("ipt_osf");
  349. MODULE_ALIAS("ip6t_osf");
  350. MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_OSF);