3 * IOCTLs for authentication
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <dev/drm2/drmP.h>
41 static struct mtx drm_magic_lock;
44 * Find the file with the given magic number.
46 * \param dev DRM device.
47 * \param magic magic number.
49 * Searches in drm_device::magiclist within all files with the same hash key
50 * the one with matching magic number, while holding the drm_device::struct_mutex
53 static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
55 struct drm_file *retval = NULL;
56 struct drm_magic_entry *pt;
57 struct drm_hash_item *hash;
58 struct drm_device *dev = master->minor->dev;
61 if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
62 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
70 * Adds a magic number.
72 * \param dev DRM device.
73 * \param priv file private data.
74 * \param magic magic number.
76 * Creates a drm_magic_entry structure and appends to the linked list
77 * associated the magic number hash key in drm_device::magiclist, while holding
78 * the drm_device::struct_mutex lock.
80 static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
83 struct drm_magic_entry *entry;
84 struct drm_device *dev = master->minor->dev;
85 DRM_DEBUG("%d\n", magic);
87 entry = malloc(sizeof(*entry), DRM_MEM_MAGIC, M_ZERO | M_NOWAIT);
91 entry->hash_item.key = (unsigned long)magic;
93 drm_ht_insert_item(&master->magiclist, &entry->hash_item);
94 list_add_tail(&entry->head, &master->magicfree);
101 * Remove a magic number.
103 * \param dev DRM device.
104 * \param magic magic number.
106 * Searches and unlinks the entry in drm_device::magiclist with the magic
107 * number hash key, while holding the drm_device::struct_mutex lock.
109 int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
111 struct drm_magic_entry *pt;
112 struct drm_hash_item *hash;
113 struct drm_device *dev = master->minor->dev;
115 DRM_DEBUG("%d\n", magic);
118 if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
122 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
123 drm_ht_remove_item(&master->magiclist, hash);
127 free(pt, DRM_MEM_MAGIC);
133 * Get a unique magic number (ioctl).
135 * \param inode device inode.
136 * \param file_priv DRM file private.
137 * \param cmd command.
138 * \param arg pointer to a resulting drm_auth structure.
139 * \return zero on success, or a negative number on failure.
141 * If there is a magic number in drm_file::magic then use it, otherwise
142 * searches an unique non-zero magic number and add it associating it with \p
144 * This ioctl needs protection by the drm_global_mutex, which protects
145 * struct drm_file::magic and struct drm_magic_entry::priv.
147 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
149 static drm_magic_t sequence = 0;
150 struct drm_auth *auth = data;
152 /* Find unique magic */
153 if (file_priv->magic) {
154 auth->magic = file_priv->magic;
157 mtx_lock(&drm_magic_lock);
159 ++sequence; /* reserve 0 */
160 auth->magic = sequence++;
161 mtx_unlock(&drm_magic_lock);
162 } while (drm_find_file(file_priv->master, auth->magic));
163 file_priv->magic = auth->magic;
164 drm_add_magic(file_priv->master, file_priv, auth->magic);
167 DRM_DEBUG("%u\n", auth->magic);
173 * Authenticate with a magic.
175 * \param inode device inode.
176 * \param file_priv DRM file private.
177 * \param cmd command.
178 * \param arg pointer to a drm_auth structure.
179 * \return zero if authentication successed, or a negative number otherwise.
181 * Checks if \p file_priv is associated with the magic number passed in \arg.
182 * This ioctl needs protection by the drm_global_mutex, which protects
183 * struct drm_file::magic and struct drm_magic_entry::priv.
185 int drm_authmagic(struct drm_device *dev, void *data,
186 struct drm_file *file_priv)
188 struct drm_auth *auth = data;
189 struct drm_file *file;
191 DRM_DEBUG("%u\n", auth->magic);
192 if ((file = drm_find_file(file_priv->master, auth->magic))) {
193 file->authenticated = 1;
194 drm_remove_magic(file_priv->master, auth->magic);
201 drm_magic_init(void *arg)
204 mtx_init(&drm_magic_lock, "drm_getmagic__lock", NULL, MTX_DEF);
209 drm_magic_fini(void *arg)
212 mtx_destroy(&drm_magic_lock);
215 SYSINIT(drm_magic_init, SI_SUB_KLD, SI_ORDER_MIDDLE, drm_magic_init, NULL);
216 SYSUNINIT(drm_magic_fini, SI_SUB_KLD, SI_ORDER_MIDDLE, drm_magic_fini, NULL);