87b60d6617ef1d55703f01cbf7a7a849ef515645
[linux-2.6-block.git] / net / netfilter / nft_osf.c
1 #include <net/ip.h>
2 #include <net/tcp.h>
3
4 #include <net/netfilter/nf_tables.h>
5 #include <linux/netfilter/nfnetlink_osf.h>
6
7 struct nft_osf {
8         enum nft_registers      dreg:8;
9         u8                      ttl;
10         u32                     flags;
11 };
12
13 static const struct nla_policy nft_osf_policy[NFTA_OSF_MAX + 1] = {
14         [NFTA_OSF_DREG]         = { .type = NLA_U32 },
15         [NFTA_OSF_TTL]          = { .type = NLA_U8 },
16         [NFTA_OSF_FLAGS]        = { .type = NLA_U32 },
17 };
18
19 static void nft_osf_eval(const struct nft_expr *expr, struct nft_regs *regs,
20                          const struct nft_pktinfo *pkt)
21 {
22         struct nft_osf *priv = nft_expr_priv(expr);
23         u32 *dest = &regs->data[priv->dreg];
24         struct sk_buff *skb = pkt->skb;
25         char os_match[NFT_OSF_MAXGENRELEN + 1];
26         const struct tcphdr *tcp;
27         struct nf_osf_data data;
28         struct tcphdr _tcph;
29
30         tcp = skb_header_pointer(skb, ip_hdrlen(skb),
31                                  sizeof(struct tcphdr), &_tcph);
32         if (!tcp) {
33                 regs->verdict.code = NFT_BREAK;
34                 return;
35         }
36         if (!tcp->syn) {
37                 regs->verdict.code = NFT_BREAK;
38                 return;
39         }
40
41         if (!nf_osf_find(skb, nf_osf_fingers, priv->ttl, &data)) {
42                 strncpy((char *)dest, "unknown", NFT_OSF_MAXGENRELEN);
43         } else {
44                 if (priv->flags & NFT_OSF_F_VERSION)
45                         snprintf(os_match, NFT_OSF_MAXGENRELEN, "%s:%s",
46                                  data.genre, data.version);
47                 else
48                         strlcpy(os_match, data.genre, NFT_OSF_MAXGENRELEN);
49
50                 strncpy((char *)dest, os_match, NFT_OSF_MAXGENRELEN);
51         }
52 }
53
54 static int nft_osf_init(const struct nft_ctx *ctx,
55                         const struct nft_expr *expr,
56                         const struct nlattr * const tb[])
57 {
58         struct nft_osf *priv = nft_expr_priv(expr);
59         u32 flags;
60         int err;
61         u8 ttl;
62
63         if (tb[NFTA_OSF_TTL]) {
64                 ttl = nla_get_u8(tb[NFTA_OSF_TTL]);
65                 if (ttl > 2)
66                         return -EINVAL;
67                 priv->ttl = ttl;
68         }
69
70         if (tb[NFTA_OSF_FLAGS]) {
71                 flags = ntohl(nla_get_be32(tb[NFTA_OSF_FLAGS]));
72                 if (flags != NFT_OSF_F_VERSION)
73                         return -EINVAL;
74                 priv->flags = flags;
75         }
76
77         priv->dreg = nft_parse_register(tb[NFTA_OSF_DREG]);
78         err = nft_validate_register_store(ctx, priv->dreg, NULL,
79                                           NFT_DATA_VALUE, NFT_OSF_MAXGENRELEN);
80         if (err < 0)
81                 return err;
82
83         return 0;
84 }
85
86 static int nft_osf_dump(struct sk_buff *skb, const struct nft_expr *expr)
87 {
88         const struct nft_osf *priv = nft_expr_priv(expr);
89
90         if (nla_put_u8(skb, NFTA_OSF_TTL, priv->ttl))
91                 goto nla_put_failure;
92
93         if (nla_put_be32(skb, NFTA_OSF_FLAGS, ntohl(priv->flags)))
94                 goto nla_put_failure;
95
96         if (nft_dump_register(skb, NFTA_OSF_DREG, priv->dreg))
97                 goto nla_put_failure;
98
99         return 0;
100
101 nla_put_failure:
102         return -1;
103 }
104
105 static int nft_osf_validate(const struct nft_ctx *ctx,
106                             const struct nft_expr *expr,
107                             const struct nft_data **data)
108 {
109         return nft_chain_validate_hooks(ctx->chain, (1 << NF_INET_LOCAL_IN) |
110                                                     (1 << NF_INET_PRE_ROUTING) |
111                                                     (1 << NF_INET_FORWARD));
112 }
113
114 static struct nft_expr_type nft_osf_type;
115 static const struct nft_expr_ops nft_osf_op = {
116         .eval           = nft_osf_eval,
117         .size           = NFT_EXPR_SIZE(sizeof(struct nft_osf)),
118         .init           = nft_osf_init,
119         .dump           = nft_osf_dump,
120         .type           = &nft_osf_type,
121         .validate       = nft_osf_validate,
122 };
123
124 static struct nft_expr_type nft_osf_type __read_mostly = {
125         .ops            = &nft_osf_op,
126         .name           = "osf",
127         .owner          = THIS_MODULE,
128         .policy         = nft_osf_policy,
129         .maxattr        = NFTA_OSF_MAX,
130 };
131
132 static int __init nft_osf_module_init(void)
133 {
134         return nft_register_expr(&nft_osf_type);
135 }
136
137 static void __exit nft_osf_module_exit(void)
138 {
139         return nft_unregister_expr(&nft_osf_type);
140 }
141
142 module_init(nft_osf_module_init);
143 module_exit(nft_osf_module_exit);
144
145 MODULE_LICENSE("GPL");
146 MODULE_AUTHOR("Fernando Fernandez <ffmancera@riseup.net>");
147 MODULE_ALIAS_NFT_EXPR("osf");