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 | */ | |
84b1fd10 | 48 | static struct drm_file *drm_find_file(struct drm_device * dev, 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; |
1da177e4 | 53 | |
30e2fb18 | 54 | mutex_lock(&dev->struct_mutex); |
8669cbc5 | 55 | if (!drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) { |
8fc2fdf4 | 56 | pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item); |
8669cbc5 | 57 | retval = pt->priv; |
1da177e4 | 58 | } |
30e2fb18 | 59 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
60 | return retval; |
61 | } | |
62 | ||
63 | /** | |
64 | * Adds a magic number. | |
b5e89ed5 | 65 | * |
1da177e4 LT |
66 | * \param dev DRM device. |
67 | * \param priv file private data. | |
68 | * \param magic magic number. | |
69 | * | |
70 | * Creates a drm_magic_entry structure and appends to the linked list | |
71 | * associated the magic number hash key in drm_device::magiclist, while holding | |
30e2fb18 | 72 | * the drm_device::struct_mutex lock. |
1da177e4 | 73 | */ |
84b1fd10 | 74 | static int drm_add_magic(struct drm_device * dev, struct drm_file * priv, |
b5e89ed5 | 75 | drm_magic_t magic) |
1da177e4 | 76 | { |
8fc2fdf4 | 77 | struct drm_magic_entry *entry; |
1da177e4 LT |
78 | |
79 | DRM_DEBUG("%d\n", magic); | |
80 | ||
b5e89ed5 DA |
81 | entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC); |
82 | if (!entry) | |
83 | return -ENOMEM; | |
1da177e4 | 84 | memset(entry, 0, sizeof(*entry)); |
b5e89ed5 | 85 | entry->priv = priv; |
1da177e4 | 86 | |
8669cbc5 | 87 | entry->hash_item.key = (unsigned long)magic; |
30e2fb18 | 88 | mutex_lock(&dev->struct_mutex); |
8669cbc5 TH |
89 | drm_ht_insert_item(&dev->magiclist, &entry->hash_item); |
90 | list_add_tail(&entry->head, &dev->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 | */ |
84b1fd10 | 105 | static int drm_remove_magic(struct drm_device * dev, drm_magic_t magic) |
1da177e4 | 106 | { |
8fc2fdf4 | 107 | struct drm_magic_entry *pt; |
e0be428e | 108 | struct drm_hash_item *hash; |
1da177e4 LT |
109 | |
110 | DRM_DEBUG("%d\n", magic); | |
1da177e4 | 111 | |
30e2fb18 | 112 | mutex_lock(&dev->struct_mutex); |
8669cbc5 TH |
113 | if (drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) { |
114 | mutex_unlock(&dev->struct_mutex); | |
115 | return -EINVAL; | |
1da177e4 | 116 | } |
8fc2fdf4 | 117 | pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item); |
8669cbc5 TH |
118 | drm_ht_remove_item(&dev->magiclist, hash); |
119 | list_del(&pt->head); | |
30e2fb18 | 120 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
121 | |
122 | drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC); | |
123 | ||
572225be | 124 | return 0; |
1da177e4 LT |
125 | } |
126 | ||
127 | /** | |
128 | * Get a unique magic number (ioctl). | |
129 | * | |
130 | * \param inode device inode. | |
6c340eac | 131 | * \param file_priv DRM file private. |
1da177e4 LT |
132 | * \param cmd command. |
133 | * \param arg pointer to a resulting drm_auth structure. | |
134 | * \return zero on success, or a negative number on failure. | |
135 | * | |
136 | * If there is a magic number in drm_file::magic then use it, otherwise | |
137 | * searches an unique non-zero magic number and add it associating it with \p | |
6c340eac | 138 | * file_priv. |
1da177e4 | 139 | */ |
6c340eac | 140 | int drm_getmagic(struct inode *inode, struct drm_file *file_priv, |
b5e89ed5 | 141 | unsigned int cmd, unsigned long arg) |
1da177e4 LT |
142 | { |
143 | static drm_magic_t sequence = 0; | |
144 | static DEFINE_SPINLOCK(lock); | |
6c340eac | 145 | struct drm_device *dev = file_priv->head->dev; |
c60ce623 | 146 | struct drm_auth auth; |
1da177e4 | 147 | |
b5e89ed5 | 148 | /* Find unique magic */ |
6c340eac EA |
149 | if (file_priv->magic) { |
150 | auth.magic = file_priv->magic; | |
1da177e4 LT |
151 | } else { |
152 | do { | |
153 | spin_lock(&lock); | |
b5e89ed5 DA |
154 | if (!sequence) |
155 | ++sequence; /* reserve 0 */ | |
1da177e4 LT |
156 | auth.magic = sequence++; |
157 | spin_unlock(&lock); | |
158 | } while (drm_find_file(dev, auth.magic)); | |
6c340eac EA |
159 | file_priv->magic = auth.magic; |
160 | drm_add_magic(dev, file_priv, auth.magic); | |
1da177e4 LT |
161 | } |
162 | ||
163 | DRM_DEBUG("%u\n", auth.magic); | |
c60ce623 | 164 | if (copy_to_user((struct drm_auth __user *) arg, &auth, sizeof(auth))) |
1da177e4 LT |
165 | return -EFAULT; |
166 | return 0; | |
167 | } | |
168 | ||
169 | /** | |
170 | * Authenticate with a magic. | |
171 | * | |
172 | * \param inode device inode. | |
6c340eac | 173 | * \param file_priv DRM file private. |
1da177e4 LT |
174 | * \param cmd command. |
175 | * \param arg pointer to a drm_auth structure. | |
176 | * \return zero if authentication successed, or a negative number otherwise. | |
177 | * | |
6c340eac | 178 | * Checks if \p file_priv is associated with the magic number passed in \arg. |
1da177e4 | 179 | */ |
6c340eac | 180 | int drm_authmagic(struct inode *inode, struct drm_file *file_priv, |
b5e89ed5 | 181 | unsigned int cmd, unsigned long arg) |
1da177e4 | 182 | { |
6c340eac | 183 | struct drm_device *dev = file_priv->head->dev; |
c60ce623 | 184 | struct drm_auth auth; |
84b1fd10 | 185 | struct drm_file *file; |
1da177e4 | 186 | |
c60ce623 | 187 | if (copy_from_user(&auth, (struct drm_auth __user *) arg, sizeof(auth))) |
1da177e4 LT |
188 | return -EFAULT; |
189 | DRM_DEBUG("%u\n", auth.magic); | |
190 | if ((file = drm_find_file(dev, auth.magic))) { | |
191 | file->authenticated = 1; | |
192 | drm_remove_magic(dev, auth.magic); | |
193 | return 0; | |
194 | } | |
195 | return -EINVAL; | |
196 | } |