xt_osf.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/capability.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 (!capable(CAP_NET_ADMIN))
  65. return -EPERM;
  66. if (!osf_attrs[OSF_ATTR_FINGER])
  67. return -EINVAL;
  68. if (!(nlh->nlmsg_flags & NLM_F_CREATE))
  69. return -EINVAL;
  70. f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
  71. kf = kmalloc(sizeof(struct xt_osf_finger), GFP_KERNEL);
  72. if (!kf)
  73. return -ENOMEM;
  74. memcpy(&kf->finger, f, sizeof(struct xt_osf_user_finger));
  75. list_for_each_entry(sf, &xt_osf_fingers[!!f->df], finger_entry) {
  76. if (memcmp(&sf->finger, f, sizeof(struct xt_osf_user_finger)))
  77. continue;
  78. kfree(kf);
  79. kf = NULL;
  80. if (nlh->nlmsg_flags & NLM_F_EXCL)
  81. err = -EEXIST;
  82. break;
  83. }
  84. /*
  85. * We are protected by nfnl mutex.
  86. */
  87. if (kf)
  88. list_add_tail_rcu(&kf->finger_entry, &xt_osf_fingers[!!f->df]);
  89. return err;
  90. }
  91. static int xt_osf_remove_callback(struct sock *ctnl, struct sk_buff *skb,
  92. const struct nlmsghdr *nlh,
  93. const struct nlattr * const osf_attrs[])
  94. {
  95. struct xt_osf_user_finger *f;
  96. struct xt_osf_finger *sf;
  97. int err = -ENOENT;
  98. if (!capable(CAP_NET_ADMIN))
  99. return -EPERM;
  100. if (!osf_attrs[OSF_ATTR_FINGER])
  101. return -EINVAL;
  102. f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
  103. list_for_each_entry(sf, &xt_osf_fingers[!!f->df], finger_entry) {
  104. if (memcmp(&sf->finger, f, sizeof(struct xt_osf_user_finger)))
  105. continue;
  106. /*
  107. * We are protected by nfnl mutex.
  108. */
  109. list_del_rcu(&sf->finger_entry);
  110. kfree_rcu(sf, rcu_head);
  111. err = 0;
  112. break;
  113. }
  114. return err;
  115. }
  116. static const struct nfnl_callback xt_osf_nfnetlink_callbacks[OSF_MSG_MAX] = {
  117. [OSF_MSG_ADD] = {
  118. .call = xt_osf_add_callback,
  119. .attr_count = OSF_ATTR_MAX,
  120. .policy = xt_osf_policy,
  121. },
  122. [OSF_MSG_REMOVE] = {
  123. .call = xt_osf_remove_callback,
  124. .attr_count = OSF_ATTR_MAX,
  125. .policy = xt_osf_policy,
  126. },
  127. };
  128. static const struct nfnetlink_subsystem xt_osf_nfnetlink = {
  129. .name = "osf",
  130. .subsys_id = NFNL_SUBSYS_OSF,
  131. .cb_count = OSF_MSG_MAX,
  132. .cb = xt_osf_nfnetlink_callbacks,
  133. };
  134. static inline int xt_osf_ttl(const struct sk_buff *skb, const struct xt_osf_info *info,
  135. unsigned char f_ttl)
  136. {
  137. const struct iphdr *ip = ip_hdr(skb);
  138. if (info->flags & XT_OSF_TTL) {
  139. if (info->ttl == XT_OSF_TTL_TRUE)
  140. return ip->ttl == f_ttl;
  141. if (info->ttl == XT_OSF_TTL_NOCHECK)
  142. return 1;
  143. else if (ip->ttl <= f_ttl)
  144. return 1;
  145. else {
  146. struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
  147. int ret = 0;
  148. for_ifa(in_dev) {
  149. if (inet_ifa_match(ip->saddr, ifa)) {
  150. ret = (ip->ttl == f_ttl);
  151. break;
  152. }
  153. }
  154. endfor_ifa(in_dev);
  155. return ret;
  156. }
  157. }
  158. return ip->ttl == f_ttl;
  159. }
  160. static bool
  161. xt_osf_match_packet(const struct sk_buff *skb, struct xt_action_param *p)
  162. {
  163. const struct xt_osf_info *info = p->matchinfo;
  164. const struct iphdr *ip = ip_hdr(skb);
  165. const struct tcphdr *tcp;
  166. struct tcphdr _tcph;
  167. int fmatch = FMATCH_WRONG, fcount = 0;
  168. unsigned int optsize = 0, check_WSS = 0;
  169. u16 window, totlen, mss = 0;
  170. bool df;
  171. const unsigned char *optp = NULL, *_optp = NULL;
  172. unsigned char opts[MAX_IPOPTLEN];
  173. const struct xt_osf_finger *kf;
  174. const struct xt_osf_user_finger *f;
  175. struct net *net = p->net;
  176. if (!info)
  177. return false;
  178. tcp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(struct tcphdr), &_tcph);
  179. if (!tcp)
  180. return false;
  181. if (!tcp->syn)
  182. return false;
  183. totlen = ntohs(ip->tot_len);
  184. df = ntohs(ip->frag_off) & IP_DF;
  185. window = ntohs(tcp->window);
  186. if (tcp->doff * 4 > sizeof(struct tcphdr)) {
  187. optsize = tcp->doff * 4 - sizeof(struct tcphdr);
  188. _optp = optp = skb_header_pointer(skb, ip_hdrlen(skb) +
  189. sizeof(struct tcphdr), optsize, opts);
  190. }
  191. rcu_read_lock();
  192. list_for_each_entry_rcu(kf, &xt_osf_fingers[df], finger_entry) {
  193. int foptsize, optnum;
  194. f = &kf->finger;
  195. if (!(info->flags & XT_OSF_LOG) && strcmp(info->genre, f->genre))
  196. continue;
  197. optp = _optp;
  198. fmatch = FMATCH_WRONG;
  199. if (totlen != f->ss || !xt_osf_ttl(skb, info, f->ttl))
  200. continue;
  201. /*
  202. * Should not happen if userspace parser was written correctly.
  203. */
  204. if (f->wss.wc >= OSF_WSS_MAX)
  205. continue;
  206. /* Check options */
  207. foptsize = 0;
  208. for (optnum = 0; optnum < f->opt_num; ++optnum)
  209. foptsize += f->opt[optnum].length;
  210. if (foptsize > MAX_IPOPTLEN ||
  211. optsize > MAX_IPOPTLEN ||
  212. optsize != foptsize)
  213. continue;
  214. check_WSS = f->wss.wc;
  215. for (optnum = 0; optnum < f->opt_num; ++optnum) {
  216. if (f->opt[optnum].kind == (*optp)) {
  217. __u32 len = f->opt[optnum].length;
  218. const __u8 *optend = optp + len;
  219. int loop_cont = 0;
  220. fmatch = FMATCH_OK;
  221. switch (*optp) {
  222. case OSFOPT_MSS:
  223. mss = optp[3];
  224. mss <<= 8;
  225. mss |= optp[2];
  226. mss = ntohs((__force __be16)mss);
  227. break;
  228. case OSFOPT_TS:
  229. loop_cont = 1;
  230. break;
  231. }
  232. optp = optend;
  233. } else
  234. fmatch = FMATCH_OPT_WRONG;
  235. if (fmatch != FMATCH_OK)
  236. break;
  237. }
  238. if (fmatch != FMATCH_OPT_WRONG) {
  239. fmatch = FMATCH_WRONG;
  240. switch (check_WSS) {
  241. case OSF_WSS_PLAIN:
  242. if (f->wss.val == 0 || window == f->wss.val)
  243. fmatch = FMATCH_OK;
  244. break;
  245. case OSF_WSS_MSS:
  246. /*
  247. * Some smart modems decrease mangle MSS to
  248. * SMART_MSS_2, so we check standard, decreased
  249. * and the one provided in the fingerprint MSS
  250. * values.
  251. */
  252. #define SMART_MSS_1 1460
  253. #define SMART_MSS_2 1448
  254. if (window == f->wss.val * mss ||
  255. window == f->wss.val * SMART_MSS_1 ||
  256. window == f->wss.val * SMART_MSS_2)
  257. fmatch = FMATCH_OK;
  258. break;
  259. case OSF_WSS_MTU:
  260. if (window == f->wss.val * (mss + 40) ||
  261. window == f->wss.val * (SMART_MSS_1 + 40) ||
  262. window == f->wss.val * (SMART_MSS_2 + 40))
  263. fmatch = FMATCH_OK;
  264. break;
  265. case OSF_WSS_MODULO:
  266. if ((window % f->wss.val) == 0)
  267. fmatch = FMATCH_OK;
  268. break;
  269. }
  270. }
  271. if (fmatch != FMATCH_OK)
  272. continue;
  273. fcount++;
  274. if (info->flags & XT_OSF_LOG)
  275. nf_log_packet(net, p->family, p->hooknum, skb,
  276. p->in, p->out, NULL,
  277. "%s [%s:%s] : %pI4:%d -> %pI4:%d hops=%d\n",
  278. f->genre, f->version, f->subtype,
  279. &ip->saddr, ntohs(tcp->source),
  280. &ip->daddr, ntohs(tcp->dest),
  281. f->ttl - ip->ttl);
  282. if ((info->flags & XT_OSF_LOG) &&
  283. info->loglevel == XT_OSF_LOGLEVEL_FIRST)
  284. break;
  285. }
  286. rcu_read_unlock();
  287. if (!fcount && (info->flags & XT_OSF_LOG))
  288. nf_log_packet(net, p->family, p->hooknum, skb, p->in,
  289. p->out, NULL,
  290. "Remote OS is not known: %pI4:%u -> %pI4:%u\n",
  291. &ip->saddr, ntohs(tcp->source),
  292. &ip->daddr, ntohs(tcp->dest));
  293. if (fcount)
  294. fmatch = FMATCH_OK;
  295. return fmatch == FMATCH_OK;
  296. }
  297. static struct xt_match xt_osf_match = {
  298. .name = "osf",
  299. .revision = 0,
  300. .family = NFPROTO_IPV4,
  301. .proto = IPPROTO_TCP,
  302. .hooks = (1 << NF_INET_LOCAL_IN) |
  303. (1 << NF_INET_PRE_ROUTING) |
  304. (1 << NF_INET_FORWARD),
  305. .match = xt_osf_match_packet,
  306. .matchsize = sizeof(struct xt_osf_info),
  307. .me = THIS_MODULE,
  308. };
  309. static int __init xt_osf_init(void)
  310. {
  311. int err = -EINVAL;
  312. int i;
  313. for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i)
  314. INIT_LIST_HEAD(&xt_osf_fingers[i]);
  315. err = nfnetlink_subsys_register(&xt_osf_nfnetlink);
  316. if (err < 0) {
  317. pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err);
  318. goto err_out_exit;
  319. }
  320. err = xt_register_match(&xt_osf_match);
  321. if (err) {
  322. pr_err("Failed to register OS fingerprint "
  323. "matching module (%d)\n", err);
  324. goto err_out_remove;
  325. }
  326. return 0;
  327. err_out_remove:
  328. nfnetlink_subsys_unregister(&xt_osf_nfnetlink);
  329. err_out_exit:
  330. return err;
  331. }
  332. static void __exit xt_osf_fini(void)
  333. {
  334. struct xt_osf_finger *f;
  335. int i;
  336. nfnetlink_subsys_unregister(&xt_osf_nfnetlink);
  337. xt_unregister_match(&xt_osf_match);
  338. rcu_read_lock();
  339. for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i) {
  340. list_for_each_entry_rcu(f, &xt_osf_fingers[i], finger_entry) {
  341. list_del_rcu(&f->finger_entry);
  342. kfree_rcu(f, rcu_head);
  343. }
  344. }
  345. rcu_read_unlock();
  346. rcu_barrier();
  347. }
  348. module_init(xt_osf_init);
  349. module_exit(xt_osf_fini);
  350. MODULE_LICENSE("GPL");
  351. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  352. MODULE_DESCRIPTION("Passive OS fingerprint matching.");
  353. MODULE_ALIAS("ipt_osf");
  354. MODULE_ALIAS("ip6t_osf");
  355. MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_OSF);