| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** | 
| Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 2 | * \file drm_auth.c | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
|  | 38 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 
| Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 45 | * the one with matching magic number, while holding the drm_device::struct_mutex | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * lock. | 
|  | 47 | */ | 
| Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 48 | static struct drm_file *drm_find_file(struct drm_device * dev, drm_magic_t magic) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { | 
| Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 50 | struct drm_file *retval = NULL; | 
| Dave Airlie | 8fc2fdf | 2007-07-11 16:21:47 +1000 | [diff] [blame] | 51 | struct drm_magic_entry *pt; | 
| Dave Airlie | e0be428 | 2007-07-12 10:26:44 +1000 | [diff] [blame] | 52 | struct drm_hash_item *hash; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 |  | 
| Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 54 | mutex_lock(&dev->struct_mutex); | 
| Thomas Hellstrom | 8669cbc | 2006-08-07 22:22:10 +1000 | [diff] [blame] | 55 | if (!drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) { | 
| Dave Airlie | 8fc2fdf | 2007-07-11 16:21:47 +1000 | [diff] [blame] | 56 | pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item); | 
| Thomas Hellstrom | 8669cbc | 2006-08-07 22:22:10 +1000 | [diff] [blame] | 57 | retval = pt->priv; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } | 
| Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 59 | mutex_unlock(&dev->struct_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | return retval; | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | /** | 
|  | 64 | * Adds a magic number. | 
| Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 65 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 
| Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 72 | * the drm_device::struct_mutex lock. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | */ | 
| Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 74 | static int drm_add_magic(struct drm_device * dev, struct drm_file * priv, | 
| Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 75 | drm_magic_t magic) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { | 
| Dave Airlie | 8fc2fdf | 2007-07-11 16:21:47 +1000 | [diff] [blame] | 77 | struct drm_magic_entry *entry; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | DRM_DEBUG("%d\n", magic); | 
|  | 80 |  | 
| Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 81 | entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC); | 
|  | 82 | if (!entry) | 
|  | 83 | return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | memset(entry, 0, sizeof(*entry)); | 
| Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 85 | entry->priv = priv; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |  | 
| Thomas Hellstrom | 8669cbc | 2006-08-07 22:22:10 +1000 | [diff] [blame] | 87 | entry->hash_item.key = (unsigned long)magic; | 
| Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 88 | mutex_lock(&dev->struct_mutex); | 
| Thomas Hellstrom | 8669cbc | 2006-08-07 22:22:10 +1000 | [diff] [blame] | 89 | drm_ht_insert_item(&dev->magiclist, &entry->hash_item); | 
|  | 90 | list_add_tail(&entry->head, &dev->magicfree); | 
| Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 91 | mutex_unlock(&dev->struct_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 |  | 
|  | 93 | return 0; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | /** | 
|  | 97 | * Remove a magic number. | 
| Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 98 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | * \param dev DRM device. | 
|  | 100 | * \param magic magic number. | 
|  | 101 | * | 
|  | 102 | * Searches and unlinks the entry in drm_device::magiclist with the magic | 
| Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 103 | * number hash key, while holding the drm_device::struct_mutex lock. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | */ | 
| Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 105 | static int drm_remove_magic(struct drm_device * dev, drm_magic_t magic) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { | 
| Dave Airlie | 8fc2fdf | 2007-07-11 16:21:47 +1000 | [diff] [blame] | 107 | struct drm_magic_entry *pt; | 
| Dave Airlie | e0be428 | 2007-07-12 10:26:44 +1000 | [diff] [blame] | 108 | struct drm_hash_item *hash; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 |  | 
|  | 110 | DRM_DEBUG("%d\n", magic); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 |  | 
| Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 112 | mutex_lock(&dev->struct_mutex); | 
| Thomas Hellstrom | 8669cbc | 2006-08-07 22:22:10 +1000 | [diff] [blame] | 113 | if (drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) { | 
|  | 114 | mutex_unlock(&dev->struct_mutex); | 
|  | 115 | return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } | 
| Dave Airlie | 8fc2fdf | 2007-07-11 16:21:47 +1000 | [diff] [blame] | 117 | pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item); | 
| Thomas Hellstrom | 8669cbc | 2006-08-07 22:22:10 +1000 | [diff] [blame] | 118 | drm_ht_remove_item(&dev->magiclist, hash); | 
|  | 119 | list_del(&pt->head); | 
| Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 120 | mutex_unlock(&dev->struct_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 |  | 
|  | 122 | drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC); | 
|  | 123 |  | 
| Dave Airlie | 572225b | 2006-08-08 22:17:02 +1000 | [diff] [blame] | 124 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } | 
|  | 126 |  | 
|  | 127 | /** | 
|  | 128 | * Get a unique magic number (ioctl). | 
|  | 129 | * | 
|  | 130 | * \param inode device inode. | 
| Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 131 | * \param file_priv DRM file private. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 
| Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 138 | * file_priv. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | */ | 
| Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 140 | int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { | 
|  | 142 | static drm_magic_t sequence = 0; | 
|  | 143 | static DEFINE_SPINLOCK(lock); | 
| Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 144 | struct drm_auth *auth = data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 |  | 
| Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 146 | /* Find unique magic */ | 
| Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 147 | if (file_priv->magic) { | 
| Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 148 | auth->magic = file_priv->magic; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } else { | 
|  | 150 | do { | 
|  | 151 | spin_lock(&lock); | 
| Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 152 | if (!sequence) | 
|  | 153 | ++sequence;	/* reserve 0 */ | 
| Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 154 | auth->magic = sequence++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | spin_unlock(&lock); | 
| Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 156 | } while (drm_find_file(dev, auth->magic)); | 
|  | 157 | file_priv->magic = auth->magic; | 
|  | 158 | drm_add_magic(dev, file_priv, auth->magic); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
| Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 161 | DRM_DEBUG("%u\n", auth->magic); | 
|  | 162 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return 0; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | /** | 
|  | 167 | * Authenticate with a magic. | 
|  | 168 | * | 
|  | 169 | * \param inode device inode. | 
| Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 170 | * \param file_priv DRM file private. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | * \param cmd command. | 
|  | 172 | * \param arg pointer to a drm_auth structure. | 
|  | 173 | * \return zero if authentication successed, or a negative number otherwise. | 
|  | 174 | * | 
| Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 175 | * Checks if \p file_priv is associated with the magic number passed in \arg. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | */ | 
| Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 177 | int drm_authmagic(struct drm_device *dev, void *data, | 
|  | 178 | struct drm_file *file_priv) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { | 
| Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 180 | struct drm_auth *auth = data; | 
| Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 181 | struct drm_file *file; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 |  | 
| Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 183 | DRM_DEBUG("%u\n", auth->magic); | 
|  | 184 | if ((file = drm_find_file(dev, auth->magic))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | file->authenticated = 1; | 
| Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 186 | drm_remove_magic(dev, auth->magic); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return 0; | 
|  | 188 | } | 
|  | 189 | return -EINVAL; | 
|  | 190 | } |