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 * $DragonFly: src/sys/dev/drm/drm_auth.c,v 1.2 2008/06/10 06:35:59 hasso 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 "drmP.h" 38 39 static int drm_hash_magic(drm_magic_t magic) 40 { 41 return magic & (DRM_HASH_SIZE-1); 42 } 43 44 /** 45 * Returns the file private associated with the given magic number. 46 */ 47 static drm_file_t *drm_find_file(drm_device_t *dev, drm_magic_t magic) 48 { 49 drm_magic_entry_t *pt; 50 int hash = drm_hash_magic(magic); 51 52 DRM_SPINLOCK_ASSERT(&dev->dev_lock); 53 54 for (pt = dev->magiclist[hash].head; pt; pt = pt->next) { 55 if (pt->magic == magic) { 56 return pt->priv; 57 } 58 } 59 60 return NULL; 61 } 62 63 /** 64 * Inserts the given magic number into the hash table of used magic number 65 * lists. 66 */ 67 static int drm_add_magic(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic) 68 { 69 int hash; 70 drm_magic_entry_t *entry; 71 72 DRM_DEBUG("%d\n", magic); 73 74 DRM_SPINLOCK_ASSERT(&dev->dev_lock); 75 76 hash = drm_hash_magic(magic); 77 entry = malloc(sizeof(*entry), M_DRM, M_ZERO | M_NOWAIT); 78 if (!entry) return ENOMEM; 79 entry->magic = magic; 80 entry->priv = priv; 81 entry->next = NULL; 82 83 if (dev->magiclist[hash].tail) { 84 dev->magiclist[hash].tail->next = entry; 85 dev->magiclist[hash].tail = entry; 86 } else { 87 dev->magiclist[hash].head = entry; 88 dev->magiclist[hash].tail = entry; 89 } 90 91 return 0; 92 } 93 94 /** 95 * Removes the given magic number from the hash table of used magic number 96 * lists. 97 */ 98 static int drm_remove_magic(drm_device_t *dev, drm_magic_t magic) 99 { 100 drm_magic_entry_t *prev = NULL; 101 drm_magic_entry_t *pt; 102 int hash; 103 104 DRM_SPINLOCK_ASSERT(&dev->dev_lock); 105 106 DRM_DEBUG("%d\n", magic); 107 hash = drm_hash_magic(magic); 108 109 for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) { 110 if (pt->magic == magic) { 111 if (dev->magiclist[hash].head == pt) { 112 dev->magiclist[hash].head = pt->next; 113 } 114 if (dev->magiclist[hash].tail == pt) { 115 dev->magiclist[hash].tail = prev; 116 } 117 if (prev) { 118 prev->next = pt->next; 119 } 120 return 0; 121 } 122 } 123 124 free(pt, M_DRM); 125 return EINVAL; 126 } 127 128 /** 129 * Called by the client, this returns a unique magic number to be authorized 130 * by the master. 131 * 132 * The master may use its own knowledge of the client (such as the X 133 * connection that the magic is passed over) to determine if the magic number 134 * should be authenticated. 135 */ 136 int drm_getmagic(drm_device_t *dev, void *data, struct drm_file *file_priv) 137 { 138 static drm_magic_t sequence = 0; 139 drm_auth_t *auth = data; 140 141 /* Find unique magic */ 142 if (file_priv->magic) { 143 auth->magic = file_priv->magic; 144 } else { 145 DRM_LOCK(); 146 do { 147 int old = sequence; 148 149 auth->magic = old+1; 150 151 if (!atomic_cmpset_int(&sequence, old, auth->magic)) 152 continue; 153 } while (drm_find_file(dev, auth->magic)); 154 file_priv->magic = auth->magic; 155 drm_add_magic(dev, file_priv, auth->magic); 156 DRM_UNLOCK(); 157 } 158 159 DRM_DEBUG("%u\n", auth->magic); 160 161 return 0; 162 } 163 164 /** 165 * Marks the client associated with the given magic number as authenticated. 166 */ 167 int drm_authmagic(drm_device_t *dev, void *data, struct drm_file *file_priv) 168 { 169 drm_auth_t *auth = data; 170 drm_file_t *priv; 171 172 DRM_DEBUG("%u\n", auth->magic); 173 174 DRM_LOCK(); 175 priv = drm_find_file(dev, auth->magic); 176 if (priv != NULL) { 177 priv->authenticated = 1; 178 drm_remove_magic(dev, auth->magic); 179 DRM_UNLOCK(); 180 return 0; 181 } else { 182 DRM_UNLOCK(); 183 return EINVAL; 184 } 185 } 186