1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2 #include <linux/module.h>
3 #include <linux/kernel.h>
5 #include <linux/capability.h>
7 #include <linux/inetdevice.h>
9 #include <linux/list.h>
10 #include <linux/rculist.h>
11 #include <linux/skbuff.h>
12 #include <linux/slab.h>
13 #include <linux/tcp.h>
18 #include <linux/netfilter/nfnetlink.h>
19 #include <linux/netfilter/x_tables.h>
20 #include <net/netfilter/nf_log.h>
21 #include <linux/netfilter/nfnetlink_osf.h>
24 * Indexed by dont-fragment bit.
25 * It is the only constant value in the fingerprint.
27 struct list_head nf_osf_fingers[2];
28 EXPORT_SYMBOL_GPL(nf_osf_fingers);
30 static inline int nf_osf_ttl(const struct sk_buff *skb,
31 int ttl_check, unsigned char f_ttl)
33 const struct iphdr *ip = ip_hdr(skb);
35 if (ttl_check != -1) {
36 if (ttl_check == NF_OSF_TTL_TRUE)
37 return ip->ttl == f_ttl;
38 if (ttl_check == NF_OSF_TTL_NOCHECK)
40 else if (ip->ttl <= f_ttl)
43 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
47 if (inet_ifa_match(ip->saddr, ifa)) {
48 ret = (ip->ttl == f_ttl);
58 return ip->ttl == f_ttl;
61 struct nf_osf_hdr_ctx {
65 const unsigned char *optp;
69 static bool nf_osf_match_one(const struct sk_buff *skb,
70 const struct nf_osf_user_finger *f,
72 struct nf_osf_hdr_ctx *ctx)
74 const __u8 *optpinit = ctx->optp;
75 unsigned int check_WSS = 0;
76 int fmatch = FMATCH_WRONG;
80 if (ctx->totlen != f->ss || !nf_osf_ttl(skb, ttl_check, f->ttl))
84 * Should not happen if userspace parser was written correctly.
86 if (f->wss.wc >= OSF_WSS_MAX)
92 for (optnum = 0; optnum < f->opt_num; ++optnum)
93 foptsize += f->opt[optnum].length;
95 if (foptsize > MAX_IPOPTLEN ||
96 ctx->optsize > MAX_IPOPTLEN ||
97 ctx->optsize != foptsize)
100 check_WSS = f->wss.wc;
102 for (optnum = 0; optnum < f->opt_num; ++optnum) {
103 if (f->opt[optnum].kind == *ctx->optp) {
104 __u32 len = f->opt[optnum].length;
105 const __u8 *optend = ctx->optp + len;
109 switch (*ctx->optp) {
115 mss = ntohs((__force __be16)mss);
123 fmatch = FMATCH_OPT_WRONG;
125 if (fmatch != FMATCH_OK)
129 if (fmatch != FMATCH_OPT_WRONG) {
130 fmatch = FMATCH_WRONG;
134 if (f->wss.val == 0 || ctx->window == f->wss.val)
139 * Some smart modems decrease mangle MSS to
140 * SMART_MSS_2, so we check standard, decreased
141 * and the one provided in the fingerprint MSS
144 #define SMART_MSS_1 1460
145 #define SMART_MSS_2 1448
146 if (ctx->window == f->wss.val * mss ||
147 ctx->window == f->wss.val * SMART_MSS_1 ||
148 ctx->window == f->wss.val * SMART_MSS_2)
152 if (ctx->window == f->wss.val * (mss + 40) ||
153 ctx->window == f->wss.val * (SMART_MSS_1 + 40) ||
154 ctx->window == f->wss.val * (SMART_MSS_2 + 40))
158 if ((ctx->window % f->wss.val) == 0)
164 if (fmatch != FMATCH_OK)
165 ctx->optp = optpinit;
167 return fmatch == FMATCH_OK;
170 static const struct tcphdr *nf_osf_hdr_ctx_init(struct nf_osf_hdr_ctx *ctx,
171 const struct sk_buff *skb,
172 const struct iphdr *ip,
174 struct tcphdr *_tcph)
176 const struct tcphdr *tcp;
178 tcp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(struct tcphdr), _tcph);
185 ctx->totlen = ntohs(ip->tot_len);
186 ctx->df = ntohs(ip->frag_off) & IP_DF;
187 ctx->window = ntohs(tcp->window);
189 if (tcp->doff * 4 > sizeof(struct tcphdr)) {
190 ctx->optsize = tcp->doff * 4 - sizeof(struct tcphdr);
192 ctx->optp = skb_header_pointer(skb, ip_hdrlen(skb) +
193 sizeof(struct tcphdr), ctx->optsize, opts);
202 nf_osf_match(const struct sk_buff *skb, u_int8_t family,
203 int hooknum, struct net_device *in, struct net_device *out,
204 const struct nf_osf_info *info, struct net *net,
205 const struct list_head *nf_osf_fingers)
207 const struct iphdr *ip = ip_hdr(skb);
208 const struct nf_osf_user_finger *f;
209 unsigned char opts[MAX_IPOPTLEN];
210 const struct nf_osf_finger *kf;
211 int fcount = 0, ttl_check;
212 int fmatch = FMATCH_WRONG;
213 struct nf_osf_hdr_ctx ctx;
214 const struct tcphdr *tcp;
217 memset(&ctx, 0, sizeof(ctx));
219 tcp = nf_osf_hdr_ctx_init(&ctx, skb, ip, opts, &_tcph);
223 ttl_check = (info->flags & NF_OSF_TTL) ? info->ttl : -1;
225 list_for_each_entry_rcu(kf, &nf_osf_fingers[ctx.df], finger_entry) {
229 if (!(info->flags & NF_OSF_LOG) && strcmp(info->genre, f->genre))
232 if (!nf_osf_match_one(skb, f, ttl_check, &ctx))
239 if (info->flags & NF_OSF_LOG)
240 nf_log_packet(net, family, hooknum, skb,
242 "%s [%s:%s] : %pI4:%d -> %pI4:%d hops=%d\n",
243 f->genre, f->version, f->subtype,
244 &ip->saddr, ntohs(tcp->source),
245 &ip->daddr, ntohs(tcp->dest),
248 if ((info->flags & NF_OSF_LOG) &&
249 info->loglevel == NF_OSF_LOGLEVEL_FIRST)
253 if (!fcount && (info->flags & NF_OSF_LOG))
254 nf_log_packet(net, family, hooknum, skb, in, out, NULL,
255 "Remote OS is not known: %pI4:%u -> %pI4:%u\n",
256 &ip->saddr, ntohs(tcp->source),
257 &ip->daddr, ntohs(tcp->dest));
262 return fmatch == FMATCH_OK;
264 EXPORT_SYMBOL_GPL(nf_osf_match);
266 const char *nf_osf_find(const struct sk_buff *skb,
267 const struct list_head *nf_osf_fingers)
269 const struct iphdr *ip = ip_hdr(skb);
270 const struct nf_osf_user_finger *f;
271 unsigned char opts[MAX_IPOPTLEN];
272 const struct nf_osf_finger *kf;
273 struct nf_osf_hdr_ctx ctx;
274 const struct tcphdr *tcp;
275 const char *genre = NULL;
278 memset(&ctx, 0, sizeof(ctx));
280 tcp = nf_osf_hdr_ctx_init(&ctx, skb, ip, opts, &_tcph);
284 list_for_each_entry_rcu(kf, &nf_osf_fingers[ctx.df], finger_entry) {
286 if (!nf_osf_match_one(skb, f, -1, &ctx))
295 EXPORT_SYMBOL_GPL(nf_osf_find);
297 static const struct nla_policy nfnl_osf_policy[OSF_ATTR_MAX + 1] = {
298 [OSF_ATTR_FINGER] = { .len = sizeof(struct nf_osf_user_finger) },
301 static int nfnl_osf_add_callback(struct net *net, struct sock *ctnl,
302 struct sk_buff *skb, const struct nlmsghdr *nlh,
303 const struct nlattr * const osf_attrs[],
304 struct netlink_ext_ack *extack)
306 struct nf_osf_user_finger *f;
307 struct nf_osf_finger *kf = NULL, *sf;
310 if (!capable(CAP_NET_ADMIN))
313 if (!osf_attrs[OSF_ATTR_FINGER])
316 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
319 f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
321 kf = kmalloc(sizeof(struct nf_osf_finger), GFP_KERNEL);
325 memcpy(&kf->finger, f, sizeof(struct nf_osf_user_finger));
327 list_for_each_entry(sf, &nf_osf_fingers[!!f->df], finger_entry) {
328 if (memcmp(&sf->finger, f, sizeof(struct nf_osf_user_finger)))
334 if (nlh->nlmsg_flags & NLM_F_EXCL)
340 * We are protected by nfnl mutex.
343 list_add_tail_rcu(&kf->finger_entry, &nf_osf_fingers[!!f->df]);
348 static int nfnl_osf_remove_callback(struct net *net, struct sock *ctnl,
350 const struct nlmsghdr *nlh,
351 const struct nlattr * const osf_attrs[],
352 struct netlink_ext_ack *extack)
354 struct nf_osf_user_finger *f;
355 struct nf_osf_finger *sf;
358 if (!capable(CAP_NET_ADMIN))
361 if (!osf_attrs[OSF_ATTR_FINGER])
364 f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
366 list_for_each_entry(sf, &nf_osf_fingers[!!f->df], finger_entry) {
367 if (memcmp(&sf->finger, f, sizeof(struct nf_osf_user_finger)))
371 * We are protected by nfnl mutex.
373 list_del_rcu(&sf->finger_entry);
374 kfree_rcu(sf, rcu_head);
383 static const struct nfnl_callback nfnl_osf_callbacks[OSF_MSG_MAX] = {
385 .call = nfnl_osf_add_callback,
386 .attr_count = OSF_ATTR_MAX,
387 .policy = nfnl_osf_policy,
390 .call = nfnl_osf_remove_callback,
391 .attr_count = OSF_ATTR_MAX,
392 .policy = nfnl_osf_policy,
396 static const struct nfnetlink_subsystem nfnl_osf_subsys = {
398 .subsys_id = NFNL_SUBSYS_OSF,
399 .cb_count = OSF_MSG_MAX,
400 .cb = nfnl_osf_callbacks,
403 static int __init nfnl_osf_init(void)
408 for (i = 0; i < ARRAY_SIZE(nf_osf_fingers); ++i)
409 INIT_LIST_HEAD(&nf_osf_fingers[i]);
411 err = nfnetlink_subsys_register(&nfnl_osf_subsys);
413 pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err);
422 static void __exit nfnl_osf_fini(void)
424 struct nf_osf_finger *f;
427 nfnetlink_subsys_unregister(&nfnl_osf_subsys);
430 for (i = 0; i < ARRAY_SIZE(nf_osf_fingers); ++i) {
431 list_for_each_entry_rcu(f, &nf_osf_fingers[i], finger_entry) {
432 list_del_rcu(&f->finger_entry);
433 kfree_rcu(f, rcu_head);
441 module_init(nfnl_osf_init);
442 module_exit(nfnl_osf_fini);
444 MODULE_LICENSE("GPL");