drm_auth.c revision 1.2.4.2 1 1.2.4.2 yamt /**
2 1.2.4.2 yamt * \file drm_auth.c
3 1.2.4.2 yamt * IOCTLs for authentication
4 1.2.4.2 yamt *
5 1.2.4.2 yamt * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
6 1.2.4.2 yamt * \author Gareth Hughes <gareth (at) valinux.com>
7 1.2.4.2 yamt */
8 1.2.4.2 yamt
9 1.2.4.2 yamt /*
10 1.2.4.2 yamt * Created: Tue Feb 2 08:37:54 1999 by faith (at) valinux.com
11 1.2.4.2 yamt *
12 1.2.4.2 yamt * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 1.2.4.2 yamt * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 1.2.4.2 yamt * All Rights Reserved.
15 1.2.4.2 yamt *
16 1.2.4.2 yamt * Permission is hereby granted, free of charge, to any person obtaining a
17 1.2.4.2 yamt * copy of this software and associated documentation files (the "Software"),
18 1.2.4.2 yamt * to deal in the Software without restriction, including without limitation
19 1.2.4.2 yamt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 1.2.4.2 yamt * and/or sell copies of the Software, and to permit persons to whom the
21 1.2.4.2 yamt * Software is furnished to do so, subject to the following conditions:
22 1.2.4.2 yamt *
23 1.2.4.2 yamt * The above copyright notice and this permission notice (including the next
24 1.2.4.2 yamt * paragraph) shall be included in all copies or substantial portions of the
25 1.2.4.2 yamt * Software.
26 1.2.4.2 yamt *
27 1.2.4.2 yamt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 1.2.4.2 yamt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 1.2.4.2 yamt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 1.2.4.2 yamt * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 1.2.4.2 yamt * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 1.2.4.2 yamt * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 1.2.4.2 yamt * OTHER DEALINGS IN THE SOFTWARE.
34 1.2.4.2 yamt */
35 1.2.4.2 yamt
36 1.2.4.2 yamt #include <drm/drmP.h>
37 1.2.4.2 yamt
38 1.2.4.2 yamt /**
39 1.2.4.2 yamt * Find the file with the given magic number.
40 1.2.4.2 yamt *
41 1.2.4.2 yamt * \param dev DRM device.
42 1.2.4.2 yamt * \param magic magic number.
43 1.2.4.2 yamt *
44 1.2.4.2 yamt * Searches in drm_device::magiclist within all files with the same hash key
45 1.2.4.2 yamt * the one with matching magic number, while holding the drm_device::struct_mutex
46 1.2.4.2 yamt * lock.
47 1.2.4.2 yamt */
48 1.2.4.2 yamt static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
49 1.2.4.2 yamt {
50 1.2.4.2 yamt struct drm_file *retval = NULL;
51 1.2.4.2 yamt struct drm_magic_entry *pt;
52 1.2.4.2 yamt struct drm_hash_item *hash;
53 1.2.4.2 yamt struct drm_device *dev = master->minor->dev;
54 1.2.4.2 yamt
55 1.2.4.2 yamt mutex_lock(&dev->struct_mutex);
56 1.2.4.2 yamt if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
57 1.2.4.2 yamt pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
58 1.2.4.2 yamt retval = pt->priv;
59 1.2.4.2 yamt }
60 1.2.4.2 yamt mutex_unlock(&dev->struct_mutex);
61 1.2.4.2 yamt return retval;
62 1.2.4.2 yamt }
63 1.2.4.2 yamt
64 1.2.4.2 yamt /**
65 1.2.4.2 yamt * Adds a magic number.
66 1.2.4.2 yamt *
67 1.2.4.2 yamt * \param dev DRM device.
68 1.2.4.2 yamt * \param priv file private data.
69 1.2.4.2 yamt * \param magic magic number.
70 1.2.4.2 yamt *
71 1.2.4.2 yamt * Creates a drm_magic_entry structure and appends to the linked list
72 1.2.4.2 yamt * associated the magic number hash key in drm_device::magiclist, while holding
73 1.2.4.2 yamt * the drm_device::struct_mutex lock.
74 1.2.4.2 yamt */
75 1.2.4.2 yamt static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
76 1.2.4.2 yamt drm_magic_t magic)
77 1.2.4.2 yamt {
78 1.2.4.2 yamt struct drm_magic_entry *entry;
79 1.2.4.2 yamt struct drm_device *dev = master->minor->dev;
80 1.2.4.2 yamt DRM_DEBUG("%d\n", magic);
81 1.2.4.2 yamt
82 1.2.4.2 yamt entry = kzalloc(sizeof(*entry), GFP_KERNEL);
83 1.2.4.2 yamt if (!entry)
84 1.2.4.2 yamt return -ENOMEM;
85 1.2.4.2 yamt entry->priv = priv;
86 1.2.4.2 yamt entry->hash_item.key = (unsigned long)magic;
87 1.2.4.2 yamt mutex_lock(&dev->struct_mutex);
88 1.2.4.2 yamt drm_ht_insert_item(&master->magiclist, &entry->hash_item);
89 1.2.4.2 yamt list_add_tail(&entry->head, &master->magicfree);
90 1.2.4.2 yamt mutex_unlock(&dev->struct_mutex);
91 1.2.4.2 yamt
92 1.2.4.2 yamt return 0;
93 1.2.4.2 yamt }
94 1.2.4.2 yamt
95 1.2.4.2 yamt /**
96 1.2.4.2 yamt * Remove a magic number.
97 1.2.4.2 yamt *
98 1.2.4.2 yamt * \param dev DRM device.
99 1.2.4.2 yamt * \param magic magic number.
100 1.2.4.2 yamt *
101 1.2.4.2 yamt * Searches and unlinks the entry in drm_device::magiclist with the magic
102 1.2.4.2 yamt * number hash key, while holding the drm_device::struct_mutex lock.
103 1.2.4.2 yamt */
104 1.2.4.2 yamt int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
105 1.2.4.2 yamt {
106 1.2.4.2 yamt struct drm_magic_entry *pt;
107 1.2.4.2 yamt struct drm_hash_item *hash;
108 1.2.4.2 yamt struct drm_device *dev = master->minor->dev;
109 1.2.4.2 yamt
110 1.2.4.2 yamt DRM_DEBUG("%d\n", magic);
111 1.2.4.2 yamt
112 1.2.4.2 yamt mutex_lock(&dev->struct_mutex);
113 1.2.4.2 yamt if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
114 1.2.4.2 yamt mutex_unlock(&dev->struct_mutex);
115 1.2.4.2 yamt return -EINVAL;
116 1.2.4.2 yamt }
117 1.2.4.2 yamt pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
118 1.2.4.2 yamt drm_ht_remove_item(&master->magiclist, hash);
119 1.2.4.2 yamt list_del(&pt->head);
120 1.2.4.2 yamt mutex_unlock(&dev->struct_mutex);
121 1.2.4.2 yamt
122 1.2.4.2 yamt kfree(pt);
123 1.2.4.2 yamt
124 1.2.4.2 yamt return 0;
125 1.2.4.2 yamt }
126 1.2.4.2 yamt
127 1.2.4.2 yamt /**
128 1.2.4.2 yamt * Get a unique magic number (ioctl).
129 1.2.4.2 yamt *
130 1.2.4.2 yamt * \param inode device inode.
131 1.2.4.2 yamt * \param file_priv DRM file private.
132 1.2.4.2 yamt * \param cmd command.
133 1.2.4.2 yamt * \param arg pointer to a resulting drm_auth structure.
134 1.2.4.2 yamt * \return zero on success, or a negative number on failure.
135 1.2.4.2 yamt *
136 1.2.4.2 yamt * If there is a magic number in drm_file::magic then use it, otherwise
137 1.2.4.2 yamt * searches an unique non-zero magic number and add it associating it with \p
138 1.2.4.2 yamt * file_priv.
139 1.2.4.2 yamt * This ioctl needs protection by the drm_global_mutex, which protects
140 1.2.4.2 yamt * struct drm_file::magic and struct drm_magic_entry::priv.
141 1.2.4.2 yamt */
142 1.2.4.2 yamt int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
143 1.2.4.2 yamt {
144 1.2.4.2 yamt static atomic_t sequence = ATOMIC_INIT(0);
145 1.2.4.2 yamt struct drm_auth *auth = data;
146 1.2.4.2 yamt
147 1.2.4.2 yamt /* Find unique magic */
148 1.2.4.2 yamt if (file_priv->magic) {
149 1.2.4.2 yamt auth->magic = file_priv->magic;
150 1.2.4.2 yamt } else {
151 1.2.4.2 yamt do {
152 1.2.4.2 yamt auth->magic = atomic_inc_return(&sequence);
153 1.2.4.2 yamt } while (drm_find_file(file_priv->master, auth->magic));
154 1.2.4.2 yamt file_priv->magic = auth->magic;
155 1.2.4.2 yamt drm_add_magic(file_priv->master, file_priv, auth->magic);
156 1.2.4.2 yamt }
157 1.2.4.2 yamt
158 1.2.4.2 yamt DRM_DEBUG("%u\n", auth->magic);
159 1.2.4.2 yamt
160 1.2.4.2 yamt return 0;
161 1.2.4.2 yamt }
162 1.2.4.2 yamt
163 1.2.4.2 yamt /**
164 1.2.4.2 yamt * Authenticate with a magic.
165 1.2.4.2 yamt *
166 1.2.4.2 yamt * \param inode device inode.
167 1.2.4.2 yamt * \param file_priv DRM file private.
168 1.2.4.2 yamt * \param cmd command.
169 1.2.4.2 yamt * \param arg pointer to a drm_auth structure.
170 1.2.4.2 yamt * \return zero if authentication successed, or a negative number otherwise.
171 1.2.4.2 yamt *
172 1.2.4.2 yamt * Checks if \p file_priv is associated with the magic number passed in \arg.
173 1.2.4.2 yamt * This ioctl needs protection by the drm_global_mutex, which protects
174 1.2.4.2 yamt * struct drm_file::magic and struct drm_magic_entry::priv.
175 1.2.4.2 yamt */
176 1.2.4.2 yamt int drm_authmagic(struct drm_device *dev, void *data,
177 1.2.4.2 yamt struct drm_file *file_priv)
178 1.2.4.2 yamt {
179 1.2.4.2 yamt struct drm_auth *auth = data;
180 1.2.4.2 yamt struct drm_file *file;
181 1.2.4.2 yamt
182 1.2.4.2 yamt DRM_DEBUG("%u\n", auth->magic);
183 1.2.4.2 yamt if ((file = drm_find_file(file_priv->master, auth->magic))) {
184 1.2.4.2 yamt file->authenticated = 1;
185 1.2.4.2 yamt drm_remove_magic(file_priv->master, auth->magic);
186 1.2.4.2 yamt return 0;
187 1.2.4.2 yamt }
188 1.2.4.2 yamt return -EINVAL;
189 1.2.4.2 yamt }
190