xt_osf.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/if.h>
  23. #include <linux/inetdevice.h>
  24. #include <linux/ip.h>
  25. #include <linux/list.h>
  26. #include <linux/rculist.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/slab.h>
  29. #include <linux/tcp.h>
  30. #include <net/ip.h>
  31. #include <net/tcp.h>
  32. #include <linux/netfilter/nfnetlink.h>
  33. #include <linux/netfilter/x_tables.h>
  34. #include <net/netfilter/nf_log.h>
  35. #include <linux/netfilter/xt_osf.h>
  36. struct xt_osf_finger {
  37. struct rcu_head rcu_head;
  38. struct list_head finger_entry;
  39. struct xt_osf_user_finger finger;
  40. };
  41. enum osf_fmatch_states {
  42. /* Packet does not match the fingerprint */
  43. FMATCH_WRONG = 0,
  44. /* Packet matches the fingerprint */
  45. FMATCH_OK,
  46. /* Options do not match the fingerprint, but header does */
  47. FMATCH_OPT_WRONG,
  48. };
  49. /*
  50. * Indexed by dont-fragment bit.
  51. * It is the only constant value in the fingerprint.
  52. */
  53. static struct list_head xt_osf_fingers[2];
  54. static const struct nla_policy xt_osf_policy[OSF_ATTR_MAX + 1] = {
  55. [OSF_ATTR_FINGER] = { .len = sizeof(struct xt_osf_user_finger) },
  56. };
  57. static int xt_osf_add_callback(struct sock *ctnl, struct sk_buff *skb,
  58. const struct nlmsghdr *nlh,
  59. const struct nlattr * const osf_attrs[])
  60. {
  61. struct xt_osf_user_finger *f;
  62. struct xt_osf_finger *kf = NULL, *sf;
  63. int err = 0;
  64. if (!osf_attrs[OSF_ATTR_FINGER])
  65. return -EINVAL;
  66. if (!(nlh->nlmsg_flags & NLM_F_CREATE))
  67. return -EINVAL;
  68. f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
  69. kf = kmalloc(sizeof(struct xt_osf_finger), GFP_KERNEL);
  70. if (!kf)
  71. return -ENOMEM;
  72. memcpy(&kf->finger, f, sizeof(struct xt_osf_user_finger));
  73. list_for_each_entry(sf, &xt_osf_fingers[!!f->df], finger_entry) {
  74. if (memcmp(&sf->finger, f, sizeof(struct xt_osf_user_finger)))
  75. continue;
  76. kfree(kf);
  77. kf = NULL;
  78. if (nlh->nlmsg_flags & NLM_F_EXCL)
  79. err = -EEXIST;
  80. break;
  81. }
  82. /*
  83. * We are protected by nfnl mutex.
  84. */
  85. if (kf)
  86. list_add_tail_rcu(&kf->finger_entry, &xt_osf_fingers[!!f->df]);
  87. return err;
  88. }
  89. static int xt_osf_remove_callback(struct sock *ctnl, struct sk_buff *skb,
  90. const struct nlmsghdr *nlh,
  91. const struct nlattr * const osf_attrs[])
  92. {
  93. struct xt_osf_user_finger *f;
  94. struct xt_osf_finger *sf;
  95. int err = -ENOENT;
  96. if (!osf_attrs[OSF_ATTR_FINGER])
  97. return -EINVAL;
  98. f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
  99. list_for_each_entry(sf, &xt_osf_fingers[!!f->df], finger_entry) {
  100. if (memcmp(&sf->finger, f, sizeof(struct xt_osf_user_finger)))
  101. continue;
  102. /*
  103. * We are protected by nfnl mutex.
  104. */
  105. list_del_rcu(&sf->finger_entry);
  106. kfree_rcu(sf, rcu_head);
  107. err = 0;
  108. break;
  109. }
  110. return err;
  111. }
  112. static const struct nfnl_callback xt_osf_nfnetlink_callbacks[OSF_MSG_MAX] = {
  113. [OSF_MSG_ADD] = {
  114. .call = xt_osf_add_callback,
  115. .attr_count = OSF_ATTR_MAX,
  116. .policy = xt_osf_policy,
  117. },
  118. [OSF_MSG_REMOVE] = {
  119. .call = xt_osf_remove_callback,
  120. .attr_count = OSF_ATTR_MAX,
  121. .policy = xt_osf_policy,
  122. },
  123. };
  124. static const struct nfnetlink_subsystem xt_osf_nfnetlink = {
  125. .name = "osf",
  126. .subsys_id = NFNL_SUBSYS_OSF,
  127. .cb_count = OSF_MSG_MAX,
  128. .cb = xt_osf_nfnetlink_callbacks,
  129. };
  130. static inline int xt_osf_ttl(const struct sk_buff *skb, const struct xt_osf_info *info,
  131. unsigned char f_ttl)
  132. {
  133. const struct iphdr *ip = ip_hdr(skb);
  134. if (info->flags & XT_OSF_TTL) {
  135. if (info->ttl == XT_OSF_TTL_TRUE)
  136. return ip->ttl == f_ttl;
  137. if (info->ttl == XT_OSF_TTL_NOCHECK)
  138. return 1;
  139. else if (ip->ttl <= f_ttl)
  140. return 1;
  141. else {
  142. struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
  143. int ret = 0;
  144. for_ifa(in_dev) {
  145. if (inet_ifa_match(ip->saddr, ifa)) {
  146. ret = (ip->ttl == f_ttl);
  147. break;
  148. }
  149. }
  150. endfor_ifa(in_dev);
  151. return ret;
  152. }
  153. }
  154. return ip->ttl == f_ttl;
  155. }
  156. static bool
  157. xt_osf_match_packet(const struct sk_buff *skb, struct xt_action_param *p)
  158. {
  159. const struct xt_osf_info *info = p->matchinfo;
  160. const struct iphdr *ip = ip_hdr(skb);
  161. const struct tcphdr *tcp;
  162. struct tcphdr _tcph;
  163. int fmatch = FMATCH_WRONG, fcount = 0;
  164. unsigned int optsize = 0, check_WSS = 0;
  165. u16 window, totlen, mss = 0;
  166. bool df;
  167. const unsigned char *optp = NULL, *_optp = NULL;
  168. unsigned char opts[MAX_IPOPTLEN];
  169. const struct xt_osf_finger *kf;
  170. const struct xt_osf_user_finger *f;
  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(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(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(p->family, p->hooknum, skb, p->in, p->out, NULL,
  284. "Remote OS is not known: %pI4:%u -> %pI4:%u\n",
  285. &ip->saddr, ntohs(tcp->source),
  286. &ip->daddr, ntohs(tcp->dest));
  287. if (fcount)
  288. fmatch = FMATCH_OK;
  289. return fmatch == FMATCH_OK;
  290. }
  291. static struct xt_match xt_osf_match = {
  292. .name = "osf",
  293. .revision = 0,
  294. .family = NFPROTO_IPV4,
  295. .proto = IPPROTO_TCP,
  296. .hooks = (1 << NF_INET_LOCAL_IN) |
  297. (1 << NF_INET_PRE_ROUTING) |
  298. (1 << NF_INET_FORWARD),
  299. .match = xt_osf_match_packet,
  300. .matchsize = sizeof(struct xt_osf_info),
  301. .me = THIS_MODULE,
  302. };
  303. static int __init xt_osf_init(void)
  304. {
  305. int err = -EINVAL;
  306. int i;
  307. for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i)
  308. INIT_LIST_HEAD(&xt_osf_fingers[i]);
  309. err = nfnetlink_subsys_register(&xt_osf_nfnetlink);
  310. if (err < 0) {
  311. pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err);
  312. goto err_out_exit;
  313. }
  314. err = xt_register_match(&xt_osf_match);
  315. if (err) {
  316. pr_err("Failed to register OS fingerprint "
  317. "matching module (%d)\n", err);
  318. goto err_out_remove;
  319. }
  320. return 0;
  321. err_out_remove:
  322. nfnetlink_subsys_unregister(&xt_osf_nfnetlink);
  323. err_out_exit:
  324. return err;
  325. }
  326. static void __exit xt_osf_fini(void)
  327. {
  328. struct xt_osf_finger *f;
  329. int i;
  330. nfnetlink_subsys_unregister(&xt_osf_nfnetlink);
  331. xt_unregister_match(&xt_osf_match);
  332. rcu_read_lock();
  333. for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i) {
  334. list_for_each_entry_rcu(f, &xt_osf_fingers[i], finger_entry) {
  335. list_del_rcu(&f->finger_entry);
  336. kfree_rcu(f, rcu_head);
  337. }
  338. }
  339. rcu_read_unlock();
  340. rcu_barrier();
  341. }
  342. module_init(xt_osf_init);
  343. module_exit(xt_osf_fini);
  344. MODULE_LICENSE("GPL");
  345. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  346. MODULE_DESCRIPTION("Passive OS fingerprint matching.");
  347. MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_OSF);