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 */ 30 31 /** @file drm_auth.c 32 * Implementation of the get/authmagic ioctls implementing the authentication 33 * scheme between the master and clients. 34 */ 35 36 #include "drmP.h" 37 38 drm_file_t *drm_find_file(drm_device_t *, drm_magic_t); 39 int drm_add_magic(drm_device_t *, drm_file_t *, drm_magic_t); 40 int drm_remove_magic(drm_device_t *, drm_magic_t); 41 42 /** 43 * Returns the file private associated with the given magic number. 44 */ 45 drm_file_t * 46 drm_find_file(drm_device_t *dev, drm_magic_t magic) 47 { 48 struct drm_magic_entry *pt; 49 struct drm_magic_entry key; 50 51 DRM_SPINLOCK_ASSERT(&dev->dev_lock); 52 53 key.magic = magic; 54 if ((pt = SPLAY_FIND(drm_magic_tree, &dev->magiclist, &key)) != NULL) 55 return (pt->priv); 56 return (NULL); 57 } 58 59 /** 60 * Inserts the given magic number into the hash table of used magic number 61 * lists. 62 */ 63 int 64 drm_add_magic(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic) 65 { 66 struct drm_magic_entry *entry; 67 68 DRM_DEBUG("%d\n", magic); 69 70 DRM_SPINLOCK_ASSERT(&dev->dev_lock); 71 72 if ((entry = malloc(sizeof(*entry), M_DRM, M_ZERO | M_NOWAIT)) == NULL) 73 return (ENOMEM); 74 entry->magic = magic; 75 entry->priv = priv; 76 SPLAY_INSERT(drm_magic_tree, &dev->magiclist, entry); 77 78 return (0); 79 } 80 81 /** 82 * Removes the given magic number from the hash table of used magic number 83 * lists. 84 */ 85 int 86 drm_remove_magic(drm_device_t *dev, drm_magic_t magic) 87 { 88 struct drm_magic_entry *pt; 89 struct drm_magic_entry key; 90 91 DRM_SPINLOCK_ASSERT(&dev->dev_lock); 92 93 DRM_DEBUG("%d\n", magic); 94 95 key.magic = magic; 96 if ((pt = SPLAY_FIND(drm_magic_tree, &dev->magiclist, &key)) == NULL) 97 return (EINVAL); 98 SPLAY_REMOVE(drm_magic_tree, &dev->magiclist, pt); 99 free(pt, M_DRM); 100 return (0); 101 } 102 103 /** 104 * Called by the client, this returns a unique magic number to be authorized 105 * by the master. 106 * 107 * The master may use its own knowledge of the client (such as the X 108 * connection that the magic is passed over) to determine if the magic number 109 * should be authenticated. 110 */ 111 int 112 drm_getmagic(drm_device_t *dev, void *data, struct drm_file *file_priv) 113 { 114 static drm_magic_t sequence = 0; 115 drm_auth_t *auth = data; 116 117 /* Find unique magic */ 118 if (file_priv->magic) { 119 auth->magic = file_priv->magic; 120 } else { 121 DRM_LOCK(); 122 do { 123 int old = sequence; 124 125 auth->magic = ++old; 126 127 if (!atomic_cmpset_int(&sequence, old, auth->magic)) 128 continue; 129 } while (drm_find_file(dev, auth->magic)); 130 131 file_priv->magic = auth->magic; 132 drm_add_magic(dev, file_priv, auth->magic); 133 DRM_UNLOCK(); 134 } 135 136 DRM_DEBUG("%u\n", auth->magic); 137 138 return (0); 139 } 140 141 /** 142 * Marks the client associated with the given magic number as authenticated. 143 */ 144 int 145 drm_authmagic(drm_device_t *dev, void *data, struct drm_file *file_priv) 146 { 147 drm_auth_t *auth = data; 148 drm_file_t *priv; 149 150 DRM_DEBUG("%u\n", auth->magic); 151 152 DRM_LOCK(); 153 priv = drm_find_file(dev, auth->magic); 154 if (priv != NULL) { 155 priv->authenticated = 1; 156 drm_remove_magic(dev, auth->magic); 157 DRM_UNLOCK(); 158 return (0); 159 } else { 160 DRM_UNLOCK(); 161 return (EINVAL); 162 } 163 } 164 165 int 166 drm_magic_cmp(struct drm_magic_entry *dme1, struct drm_magic_entry *dme2) 167 { 168 return (dme1->magic - dme2->magic); 169 } 170 171 SPLAY_GENERATE(drm_magic_tree, drm_magic_entry, node, drm_magic_cmp); 172