1 /** 2 * \file drm_auth.c 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 <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <dev/drm2/drmP.h> 40 41 static struct mtx drm_magic_lock; 42 43 /** 44 * Find the file with the given magic number. 45 * 46 * \param dev DRM device. 47 * \param magic magic number. 48 * 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 51 * lock. 52 */ 53 static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic) 54 { 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; 59 60 DRM_LOCK(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); 63 retval = pt->priv; 64 } 65 DRM_UNLOCK(dev); 66 return retval; 67 } 68 69 /** 70 * Adds a magic number. 71 * 72 * \param dev DRM device. 73 * \param priv file private data. 74 * \param magic magic number. 75 * 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. 79 */ 80 static int drm_add_magic(struct drm_master *master, struct drm_file *priv, 81 drm_magic_t magic) 82 { 83 struct drm_magic_entry *entry; 84 struct drm_device *dev = master->minor->dev; 85 DRM_DEBUG("%d\n", magic); 86 87 entry = malloc(sizeof(*entry), DRM_MEM_MAGIC, M_ZERO | M_NOWAIT); 88 if (!entry) 89 return -ENOMEM; 90 entry->priv = priv; 91 entry->hash_item.key = (unsigned long)magic; 92 DRM_LOCK(dev); 93 drm_ht_insert_item(&master->magiclist, &entry->hash_item); 94 list_add_tail(&entry->head, &master->magicfree); 95 DRM_UNLOCK(dev); 96 97 return 0; 98 } 99 100 /** 101 * Remove a magic number. 102 * 103 * \param dev DRM device. 104 * \param magic magic number. 105 * 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. 108 */ 109 int drm_remove_magic(struct drm_master *master, drm_magic_t magic) 110 { 111 struct drm_magic_entry *pt; 112 struct drm_hash_item *hash; 113 struct drm_device *dev = master->minor->dev; 114 115 DRM_DEBUG("%d\n", magic); 116 117 DRM_LOCK(dev); 118 if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) { 119 DRM_UNLOCK(dev); 120 return -EINVAL; 121 } 122 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item); 123 drm_ht_remove_item(&master->magiclist, hash); 124 list_del(&pt->head); 125 DRM_UNLOCK(dev); 126 127 free(pt, DRM_MEM_MAGIC); 128 129 return 0; 130 } 131 132 /** 133 * Get a unique magic number (ioctl). 134 * 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. 140 * 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 143 * file_priv. 144 * This ioctl needs protection by the drm_global_mutex, which protects 145 * struct drm_file::magic and struct drm_magic_entry::priv. 146 */ 147 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv) 148 { 149 static drm_magic_t sequence = 0; 150 struct drm_auth *auth = data; 151 152 /* Find unique magic */ 153 if (file_priv->magic) { 154 auth->magic = file_priv->magic; 155 } else { 156 do { 157 mtx_lock(&drm_magic_lock); 158 if (!sequence) 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); 165 } 166 167 DRM_DEBUG("%u\n", auth->magic); 168 169 return 0; 170 } 171 172 /** 173 * Authenticate with a magic. 174 * 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. 180 * 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. 184 */ 185 int drm_authmagic(struct drm_device *dev, void *data, 186 struct drm_file *file_priv) 187 { 188 struct drm_auth *auth = data; 189 struct drm_file *file; 190 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); 195 return 0; 196 } 197 return -EINVAL; 198 } 199 200 static int 201 drm_magic_init(void *arg) 202 { 203 204 mtx_init(&drm_magic_lock, "drm_getmagic__lock", NULL, MTX_DEF); 205 return (0); 206 } 207 208 static void 209 drm_magic_fini(void *arg) 210 { 211 212 mtx_destroy(&drm_magic_lock); 213 } 214 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); 217