]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /** |
b5e89ed5 | 2 | * \file drm_auth.c |
1da177e4 LT |
3 | * IOCTLs for authentication |
4 | * | |
5 | * \author Rickard E. (Rik) Faith <faith@valinux.com> | |
6 | * \author Gareth Hughes <gareth@valinux.com> | |
7 | */ | |
8 | ||
9 | /* | |
10 | * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com | |
11 | * | |
12 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. | |
13 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. | |
14 | * All Rights Reserved. | |
15 | * | |
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: | |
22 | * | |
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 | |
25 | * Software. | |
26 | * | |
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. | |
34 | */ | |
35 | ||
36 | #include "drmP.h" | |
37 | ||
1da177e4 LT |
38 | /** |
39 | * Find the file with the given magic number. | |
40 | * | |
41 | * \param dev DRM device. | |
42 | * \param magic magic number. | |
43 | * | |
44 | * Searches in drm_device::magiclist within all files with the same hash key | |
30e2fb18 | 45 | * the one with matching magic number, while holding the drm_device::struct_mutex |
1da177e4 LT |
46 | * lock. |
47 | */ | |
7c1c2871 | 48 | static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic) |
1da177e4 | 49 | { |
84b1fd10 | 50 | struct drm_file *retval = NULL; |
8fc2fdf4 | 51 | struct drm_magic_entry *pt; |
e0be428e | 52 | struct drm_hash_item *hash; |
7c1c2871 | 53 | struct drm_device *dev = master->minor->dev; |
1da177e4 | 54 | |
30e2fb18 | 55 | mutex_lock(&dev->struct_mutex); |
7c1c2871 | 56 | if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) { |
8fc2fdf4 | 57 | pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item); |
8669cbc5 | 58 | retval = pt->priv; |
1da177e4 | 59 | } |
30e2fb18 | 60 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
61 | return retval; |
62 | } | |
63 | ||
64 | /** | |
65 | * Adds a magic number. | |
b5e89ed5 | 66 | * |
1da177e4 LT |
67 | * \param dev DRM device. |
68 | * \param priv file private data. | |
69 | * \param magic magic number. | |
70 | * | |
71 | * Creates a drm_magic_entry structure and appends to the linked list | |
72 | * associated the magic number hash key in drm_device::magiclist, while holding | |
30e2fb18 | 73 | * the drm_device::struct_mutex lock. |
1da177e4 | 74 | */ |
7c1c2871 | 75 | static int drm_add_magic(struct drm_master *master, struct drm_file *priv, |
b5e89ed5 | 76 | drm_magic_t magic) |
1da177e4 | 77 | { |
8fc2fdf4 | 78 | struct drm_magic_entry *entry; |
7c1c2871 | 79 | struct drm_device *dev = master->minor->dev; |
1da177e4 LT |
80 | DRM_DEBUG("%d\n", magic); |
81 | ||
9a298b2a | 82 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
b5e89ed5 DA |
83 | if (!entry) |
84 | return -ENOMEM; | |
1da177e4 | 85 | memset(entry, 0, sizeof(*entry)); |
b5e89ed5 | 86 | entry->priv = priv; |
8669cbc5 | 87 | entry->hash_item.key = (unsigned long)magic; |
30e2fb18 | 88 | mutex_lock(&dev->struct_mutex); |
7c1c2871 DA |
89 | drm_ht_insert_item(&master->magiclist, &entry->hash_item); |
90 | list_add_tail(&entry->head, &master->magicfree); | |
30e2fb18 | 91 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
92 | |
93 | return 0; | |
94 | } | |
95 | ||
96 | /** | |
97 | * Remove a magic number. | |
b5e89ed5 | 98 | * |
1da177e4 LT |
99 | * \param dev DRM device. |
100 | * \param magic magic number. | |
101 | * | |
102 | * Searches and unlinks the entry in drm_device::magiclist with the magic | |
30e2fb18 | 103 | * number hash key, while holding the drm_device::struct_mutex lock. |
1da177e4 | 104 | */ |
7c1c2871 | 105 | static int drm_remove_magic(struct drm_master *master, drm_magic_t magic) |
1da177e4 | 106 | { |
8fc2fdf4 | 107 | struct drm_magic_entry *pt; |
e0be428e | 108 | struct drm_hash_item *hash; |
7c1c2871 | 109 | struct drm_device *dev = master->minor->dev; |
1da177e4 LT |
110 | |
111 | DRM_DEBUG("%d\n", magic); | |
1da177e4 | 112 | |
30e2fb18 | 113 | mutex_lock(&dev->struct_mutex); |
7c1c2871 | 114 | if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) { |
8669cbc5 TH |
115 | mutex_unlock(&dev->struct_mutex); |
116 | return -EINVAL; | |
1da177e4 | 117 | } |
8fc2fdf4 | 118 | pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item); |
7c1c2871 | 119 | drm_ht_remove_item(&master->magiclist, hash); |
8669cbc5 | 120 | list_del(&pt->head); |
30e2fb18 | 121 | mutex_unlock(&dev->struct_mutex); |
1da177e4 | 122 | |
9a298b2a | 123 | kfree(pt); |
1da177e4 | 124 | |
572225be | 125 | return 0; |
1da177e4 LT |
126 | } |
127 | ||
128 | /** | |
129 | * Get a unique magic number (ioctl). | |
130 | * | |
131 | * \param inode device inode. | |
6c340eac | 132 | * \param file_priv DRM file private. |
1da177e4 LT |
133 | * \param cmd command. |
134 | * \param arg pointer to a resulting drm_auth structure. | |
135 | * \return zero on success, or a negative number on failure. | |
136 | * | |
137 | * If there is a magic number in drm_file::magic then use it, otherwise | |
138 | * searches an unique non-zero magic number and add it associating it with \p | |
6c340eac | 139 | * file_priv. |
1da177e4 | 140 | */ |
c153f45f | 141 | int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv) |
1da177e4 LT |
142 | { |
143 | static drm_magic_t sequence = 0; | |
144 | static DEFINE_SPINLOCK(lock); | |
c153f45f | 145 | struct drm_auth *auth = data; |
1da177e4 | 146 | |
b5e89ed5 | 147 | /* Find unique magic */ |
6c340eac | 148 | if (file_priv->magic) { |
c153f45f | 149 | auth->magic = file_priv->magic; |
1da177e4 LT |
150 | } else { |
151 | do { | |
152 | spin_lock(&lock); | |
b5e89ed5 DA |
153 | if (!sequence) |
154 | ++sequence; /* reserve 0 */ | |
c153f45f | 155 | auth->magic = sequence++; |
1da177e4 | 156 | spin_unlock(&lock); |
7c1c2871 | 157 | } while (drm_find_file(file_priv->master, auth->magic)); |
c153f45f | 158 | file_priv->magic = auth->magic; |
7c1c2871 | 159 | drm_add_magic(file_priv->master, file_priv, auth->magic); |
1da177e4 LT |
160 | } |
161 | ||
c153f45f EA |
162 | DRM_DEBUG("%u\n", auth->magic); |
163 | ||
1da177e4 LT |
164 | return 0; |
165 | } | |
166 | ||
167 | /** | |
168 | * Authenticate with a magic. | |
169 | * | |
170 | * \param inode device inode. | |
6c340eac | 171 | * \param file_priv DRM file private. |
1da177e4 LT |
172 | * \param cmd command. |
173 | * \param arg pointer to a drm_auth structure. | |
174 | * \return zero if authentication successed, or a negative number otherwise. | |
175 | * | |
6c340eac | 176 | * Checks if \p file_priv is associated with the magic number passed in \arg. |
1da177e4 | 177 | */ |
c153f45f EA |
178 | int drm_authmagic(struct drm_device *dev, void *data, |
179 | struct drm_file *file_priv) | |
1da177e4 | 180 | { |
c153f45f | 181 | struct drm_auth *auth = data; |
84b1fd10 | 182 | struct drm_file *file; |
1da177e4 | 183 | |
c153f45f | 184 | DRM_DEBUG("%u\n", auth->magic); |
7c1c2871 | 185 | if ((file = drm_find_file(file_priv->master, auth->magic))) { |
1da177e4 | 186 | file->authenticated = 1; |
7c1c2871 | 187 | drm_remove_magic(file_priv->master, auth->magic); |
1da177e4 LT |
188 | return 0; |
189 | } | |
190 | return -EINVAL; | |
191 | } |