1 /*- 2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 * OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * Authors: 26 * Rickard E. (Rik) Faith <faith@valinux.com> 27 * Gareth Hughes <gareth@valinux.com> 28 * 29 * $FreeBSD: src/sys/dev/drm2/drm_auth.c,v 1.1 2012/05/22 11:07:44 kib Exp $ 30 */ 31 32 /** @file drm_auth.c 33 * Implementation of the get/authmagic ioctls implementing the authentication 34 * scheme between the master and clients. 35 */ 36 37 #include <drm/drmP.h> 38 39 /** 40 * Find the file with the given magic number. 41 * 42 * \param dev DRM device. 43 * \param magic magic number. 44 * 45 * Searches in drm_device::magiclist within all files with the same hash key 46 * the one with matching magic number, while holding the drm_device::struct_mutex 47 * lock. 48 */ 49 static struct drm_file *drm_find_file(struct drm_device *dev, drm_magic_t magic) 50 { 51 struct drm_file *retval = NULL; 52 drm_magic_entry_t *pt; 53 struct drm_hash_item *hash; 54 55 mutex_lock(&dev->struct_mutex); 56 if (!drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) { 57 pt = drm_hash_entry(hash, drm_magic_entry_t, hash_item); 58 retval = pt->priv; 59 } 60 mutex_unlock(&dev->struct_mutex); 61 return retval; 62 } 63 64 /** 65 * Inserts the given magic number into the hash table of used magic number 66 * lists. 67 */ 68 static int drm_add_magic(struct drm_device *dev, struct drm_file *priv, 69 drm_magic_t magic) 70 { 71 drm_magic_entry_t *entry; 72 73 DRM_DEBUG("%d\n", magic); 74 75 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 76 if (!entry) 77 return -ENOMEM; 78 entry->priv = priv; 79 entry->hash_item.key = (unsigned long)magic; 80 mutex_lock(&dev->struct_mutex); 81 drm_ht_insert_item(&dev->magiclist, &entry->hash_item); 82 list_add_tail(&entry->head, &dev->magicfree); 83 mutex_unlock(&dev->struct_mutex); 84 85 return 0; 86 } 87 88 /** 89 * Removes the given magic number from the hash table of used magic number 90 * lists. 91 */ 92 static int drm_remove_magic(struct drm_device *dev, drm_magic_t magic) 93 { 94 drm_magic_entry_t *pt; 95 struct drm_hash_item *hash; 96 97 DRM_DEBUG("%d\n", magic); 98 99 mutex_lock(&dev->struct_mutex); 100 if (drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) { 101 mutex_unlock(&dev->struct_mutex); 102 return -EINVAL; 103 } 104 pt = drm_hash_entry(hash, drm_magic_entry_t, hash_item); 105 drm_ht_remove_item(&dev->magiclist, hash); 106 list_del(&pt->head); 107 mutex_unlock(&dev->struct_mutex); 108 109 kfree(pt); 110 111 return 0; 112 } 113 114 /** 115 * Called by the client, this returns a unique magic number to be authorized 116 * by the master. 117 * 118 * The master may use its own knowledge of the client (such as the X 119 * connection that the magic is passed over) to determine if the magic number 120 * should be authenticated. 121 */ 122 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv) 123 { 124 static drm_magic_t sequence = 0; 125 struct drm_auth *auth = data; 126 127 /* Find unique magic */ 128 if (file_priv->magic) { 129 auth->magic = file_priv->magic; 130 } else { 131 DRM_LOCK(dev); 132 do { 133 int old = sequence; 134 135 auth->magic = old+1; 136 137 if (!atomic_cmpset_int(&sequence, old, auth->magic)) 138 continue; 139 } while (drm_find_file(dev, auth->magic)); 140 file_priv->magic = auth->magic; 141 drm_add_magic(dev, file_priv, auth->magic); 142 DRM_UNLOCK(dev); 143 } 144 145 DRM_DEBUG("%u\n", auth->magic); 146 147 return 0; 148 } 149 150 /** 151 * Marks the client associated with the given magic number as authenticated. 152 */ 153 int drm_authmagic(struct drm_device *dev, void *data, 154 struct drm_file *file_priv) 155 { 156 struct drm_auth *auth = data; 157 struct drm_file *priv; 158 159 DRM_DEBUG("%u\n", auth->magic); 160 161 DRM_LOCK(dev); 162 priv = drm_find_file(dev, auth->magic); 163 if (priv != NULL) { 164 priv->authenticated = 1; 165 drm_remove_magic(dev, auth->magic); 166 DRM_UNLOCK(dev); 167 return 0; 168 } else { 169 DRM_UNLOCK(dev); 170 return EINVAL; 171 } 172 } 173