drm_auth.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
  10. *
  11. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * All Rights Reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a
  16. * copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice (including the next
  23. * paragraph) shall be included in all copies or substantial portions of the
  24. * Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  29. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #include "drmP.h"
  35. /**
  36. * Generate a hash key from a magic.
  37. *
  38. * \param magic magic.
  39. * \return hash key.
  40. *
  41. * The key is the modulus of the hash table size, #DRM_HASH_SIZE, which must be
  42. * a power of 2.
  43. */
  44. static int drm_hash_magic(drm_magic_t magic)
  45. {
  46. return magic & (DRM_HASH_SIZE - 1);
  47. }
  48. /**
  49. * Find the file with the given magic number.
  50. *
  51. * \param dev DRM device.
  52. * \param magic magic number.
  53. *
  54. * Searches in drm_device::magiclist within all files with the same hash key
  55. * the one with matching magic number, while holding the drm_device::struct_mutex
  56. * lock.
  57. */
  58. static drm_file_t *drm_find_file(drm_device_t * dev, drm_magic_t magic)
  59. {
  60. drm_file_t *retval = NULL;
  61. drm_magic_entry_t *pt;
  62. int hash = drm_hash_magic(magic);
  63. mutex_lock(&dev->struct_mutex);
  64. for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
  65. if (pt->magic == magic) {
  66. retval = pt->priv;
  67. break;
  68. }
  69. }
  70. mutex_unlock(&dev->struct_mutex);
  71. return retval;
  72. }
  73. /**
  74. * Adds a magic number.
  75. *
  76. * \param dev DRM device.
  77. * \param priv file private data.
  78. * \param magic magic number.
  79. *
  80. * Creates a drm_magic_entry structure and appends to the linked list
  81. * associated the magic number hash key in drm_device::magiclist, while holding
  82. * the drm_device::struct_mutex lock.
  83. */
  84. static int drm_add_magic(drm_device_t * dev, drm_file_t * priv,
  85. drm_magic_t magic)
  86. {
  87. int hash;
  88. drm_magic_entry_t *entry;
  89. DRM_DEBUG("%d\n", magic);
  90. hash = drm_hash_magic(magic);
  91. entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
  92. if (!entry)
  93. return -ENOMEM;
  94. memset(entry, 0, sizeof(*entry));
  95. entry->magic = magic;
  96. entry->priv = priv;
  97. entry->next = NULL;
  98. mutex_lock(&dev->struct_mutex);
  99. if (dev->magiclist[hash].tail) {
  100. dev->magiclist[hash].tail->next = entry;
  101. dev->magiclist[hash].tail = entry;
  102. } else {
  103. dev->magiclist[hash].head = entry;
  104. dev->magiclist[hash].tail = entry;
  105. }
  106. mutex_unlock(&dev->struct_mutex);
  107. return 0;
  108. }
  109. /**
  110. * Remove a magic number.
  111. *
  112. * \param dev DRM device.
  113. * \param magic magic number.
  114. *
  115. * Searches and unlinks the entry in drm_device::magiclist with the magic
  116. * number hash key, while holding the drm_device::struct_mutex lock.
  117. */
  118. static int drm_remove_magic(drm_device_t * dev, drm_magic_t magic)
  119. {
  120. drm_magic_entry_t *prev = NULL;
  121. drm_magic_entry_t *pt;
  122. int hash;
  123. DRM_DEBUG("%d\n", magic);
  124. hash = drm_hash_magic(magic);
  125. mutex_lock(&dev->struct_mutex);
  126. for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
  127. if (pt->magic == magic) {
  128. if (dev->magiclist[hash].head == pt) {
  129. dev->magiclist[hash].head = pt->next;
  130. }
  131. if (dev->magiclist[hash].tail == pt) {
  132. dev->magiclist[hash].tail = prev;
  133. }
  134. if (prev) {
  135. prev->next = pt->next;
  136. }
  137. mutex_unlock(&dev->struct_mutex);
  138. return 0;
  139. }
  140. }
  141. mutex_unlock(&dev->struct_mutex);
  142. drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
  143. return -EINVAL;
  144. }
  145. /**
  146. * Get a unique magic number (ioctl).
  147. *
  148. * \param inode device inode.
  149. * \param filp file pointer.
  150. * \param cmd command.
  151. * \param arg pointer to a resulting drm_auth structure.
  152. * \return zero on success, or a negative number on failure.
  153. *
  154. * If there is a magic number in drm_file::magic then use it, otherwise
  155. * searches an unique non-zero magic number and add it associating it with \p
  156. * filp.
  157. */
  158. int drm_getmagic(struct inode *inode, struct file *filp,
  159. unsigned int cmd, unsigned long arg)
  160. {
  161. static drm_magic_t sequence = 0;
  162. static DEFINE_SPINLOCK(lock);
  163. drm_file_t *priv = filp->private_data;
  164. drm_device_t *dev = priv->head->dev;
  165. drm_auth_t auth;
  166. /* Find unique magic */
  167. if (priv->magic) {
  168. auth.magic = priv->magic;
  169. } else {
  170. do {
  171. spin_lock(&lock);
  172. if (!sequence)
  173. ++sequence; /* reserve 0 */
  174. auth.magic = sequence++;
  175. spin_unlock(&lock);
  176. } while (drm_find_file(dev, auth.magic));
  177. priv->magic = auth.magic;
  178. drm_add_magic(dev, priv, auth.magic);
  179. }
  180. DRM_DEBUG("%u\n", auth.magic);
  181. if (copy_to_user((drm_auth_t __user *) arg, &auth, sizeof(auth)))
  182. return -EFAULT;
  183. return 0;
  184. }
  185. /**
  186. * Authenticate with a magic.
  187. *
  188. * \param inode device inode.
  189. * \param filp file pointer.
  190. * \param cmd command.
  191. * \param arg pointer to a drm_auth structure.
  192. * \return zero if authentication successed, or a negative number otherwise.
  193. *
  194. * Checks if \p filp is associated with the magic number passed in \arg.
  195. */
  196. int drm_authmagic(struct inode *inode, struct file *filp,
  197. unsigned int cmd, unsigned long arg)
  198. {
  199. drm_file_t *priv = filp->private_data;
  200. drm_device_t *dev = priv->head->dev;
  201. drm_auth_t auth;
  202. drm_file_t *file;
  203. if (copy_from_user(&auth, (drm_auth_t __user *) arg, sizeof(auth)))
  204. return -EFAULT;
  205. DRM_DEBUG("%u\n", auth.magic);
  206. if ((file = drm_find_file(dev, auth.magic))) {
  207. file->authenticated = 1;
  208. drm_remove_magic(dev, auth.magic);
  209. return 0;
  210. }
  211. return -EINVAL;
  212. }