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