drm_auth.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * \file drm_auth.h
  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_sem
  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. down(&dev->struct_sem);
  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. up(&dev->struct_sem);
  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_sem lock.
  83. */
  84. static int drm_add_magic(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
  85. {
  86. int hash;
  87. drm_magic_entry_t *entry;
  88. DRM_DEBUG("%d\n", magic);
  89. hash = drm_hash_magic(magic);
  90. entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
  91. if (!entry) return -ENOMEM;
  92. memset(entry, 0, sizeof(*entry));
  93. entry->magic = magic;
  94. entry->priv = priv;
  95. entry->next = NULL;
  96. down(&dev->struct_sem);
  97. if (dev->magiclist[hash].tail) {
  98. dev->magiclist[hash].tail->next = entry;
  99. dev->magiclist[hash].tail = entry;
  100. } else {
  101. dev->magiclist[hash].head = entry;
  102. dev->magiclist[hash].tail = entry;
  103. }
  104. up(&dev->struct_sem);
  105. return 0;
  106. }
  107. /**
  108. * Remove a magic number.
  109. *
  110. * \param dev DRM device.
  111. * \param magic magic number.
  112. *
  113. * Searches and unlinks the entry in drm_device::magiclist with the magic
  114. * number hash key, while holding the drm_device::struct_sem lock.
  115. */
  116. static int drm_remove_magic(drm_device_t *dev, drm_magic_t magic)
  117. {
  118. drm_magic_entry_t *prev = NULL;
  119. drm_magic_entry_t *pt;
  120. int hash;
  121. DRM_DEBUG("%d\n", magic);
  122. hash = drm_hash_magic(magic);
  123. down(&dev->struct_sem);
  124. for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
  125. if (pt->magic == magic) {
  126. if (dev->magiclist[hash].head == pt) {
  127. dev->magiclist[hash].head = pt->next;
  128. }
  129. if (dev->magiclist[hash].tail == pt) {
  130. dev->magiclist[hash].tail = prev;
  131. }
  132. if (prev) {
  133. prev->next = pt->next;
  134. }
  135. up(&dev->struct_sem);
  136. return 0;
  137. }
  138. }
  139. up(&dev->struct_sem);
  140. drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
  141. return -EINVAL;
  142. }
  143. /**
  144. * Get a unique magic number (ioctl).
  145. *
  146. * \param inode device inode.
  147. * \param filp file pointer.
  148. * \param cmd command.
  149. * \param arg pointer to a resulting drm_auth structure.
  150. * \return zero on success, or a negative number on failure.
  151. *
  152. * If there is a magic number in drm_file::magic then use it, otherwise
  153. * searches an unique non-zero magic number and add it associating it with \p
  154. * filp.
  155. */
  156. int drm_getmagic(struct inode *inode, struct file *filp,
  157. unsigned int cmd, unsigned long arg)
  158. {
  159. static drm_magic_t sequence = 0;
  160. static DEFINE_SPINLOCK(lock);
  161. drm_file_t *priv = filp->private_data;
  162. drm_device_t *dev = priv->head->dev;
  163. drm_auth_t auth;
  164. /* Find unique magic */
  165. if (priv->magic) {
  166. auth.magic = priv->magic;
  167. } else {
  168. do {
  169. spin_lock(&lock);
  170. if (!sequence) ++sequence; /* reserve 0 */
  171. auth.magic = sequence++;
  172. spin_unlock(&lock);
  173. } while (drm_find_file(dev, auth.magic));
  174. priv->magic = auth.magic;
  175. drm_add_magic(dev, priv, auth.magic);
  176. }
  177. DRM_DEBUG("%u\n", auth.magic);
  178. if (copy_to_user((drm_auth_t __user *)arg, &auth, sizeof(auth)))
  179. return -EFAULT;
  180. return 0;
  181. }
  182. /**
  183. * Authenticate with a magic.
  184. *
  185. * \param inode device inode.
  186. * \param filp file pointer.
  187. * \param cmd command.
  188. * \param arg pointer to a drm_auth structure.
  189. * \return zero if authentication successed, or a negative number otherwise.
  190. *
  191. * Checks if \p filp is associated with the magic number passed in \arg.
  192. */
  193. int drm_authmagic(struct inode *inode, struct file *filp,
  194. unsigned int cmd, unsigned long arg)
  195. {
  196. drm_file_t *priv = filp->private_data;
  197. drm_device_t *dev = priv->head->dev;
  198. drm_auth_t auth;
  199. drm_file_t *file;
  200. if (copy_from_user(&auth, (drm_auth_t __user *)arg, sizeof(auth)))
  201. return -EFAULT;
  202. DRM_DEBUG("%u\n", auth.magic);
  203. if ((file = drm_find_file(dev, auth.magic))) {
  204. file->authenticated = 1;
  205. drm_remove_magic(dev, auth.magic);
  206. return 0;
  207. }
  208. return -EINVAL;
  209. }