drm_auth.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * Find the file with the given magic number.
  37. *
  38. * \param dev DRM device.
  39. * \param magic magic number.
  40. *
  41. * Searches in drm_device::magiclist within all files with the same hash key
  42. * the one with matching magic number, while holding the drm_device::struct_mutex
  43. * lock.
  44. */
  45. static struct drm_file *drm_find_file(struct drm_device * dev, drm_magic_t magic)
  46. {
  47. struct drm_file *retval = NULL;
  48. struct drm_magic_entry *pt;
  49. struct drm_hash_item *hash;
  50. mutex_lock(&dev->struct_mutex);
  51. if (!drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
  52. pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
  53. retval = pt->priv;
  54. }
  55. mutex_unlock(&dev->struct_mutex);
  56. return retval;
  57. }
  58. /**
  59. * Adds a magic number.
  60. *
  61. * \param dev DRM device.
  62. * \param priv file private data.
  63. * \param magic magic number.
  64. *
  65. * Creates a drm_magic_entry structure and appends to the linked list
  66. * associated the magic number hash key in drm_device::magiclist, while holding
  67. * the drm_device::struct_mutex lock.
  68. */
  69. static int drm_add_magic(struct drm_device * dev, struct drm_file * priv,
  70. drm_magic_t magic)
  71. {
  72. struct drm_magic_entry *entry;
  73. DRM_DEBUG("%d\n", magic);
  74. entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
  75. if (!entry)
  76. return -ENOMEM;
  77. memset(entry, 0, sizeof(*entry));
  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. return 0;
  85. }
  86. /**
  87. * Remove a magic number.
  88. *
  89. * \param dev DRM device.
  90. * \param magic magic number.
  91. *
  92. * Searches and unlinks the entry in drm_device::magiclist with the magic
  93. * number hash key, while holding the drm_device::struct_mutex lock.
  94. */
  95. static int drm_remove_magic(struct drm_device * dev, drm_magic_t magic)
  96. {
  97. struct drm_magic_entry *pt;
  98. struct drm_hash_item *hash;
  99. DRM_DEBUG("%d\n", magic);
  100. mutex_lock(&dev->struct_mutex);
  101. if (drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
  102. mutex_unlock(&dev->struct_mutex);
  103. return -EINVAL;
  104. }
  105. pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
  106. drm_ht_remove_item(&dev->magiclist, hash);
  107. list_del(&pt->head);
  108. mutex_unlock(&dev->struct_mutex);
  109. drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
  110. return 0;
  111. }
  112. /**
  113. * Get a unique magic number (ioctl).
  114. *
  115. * \param inode device inode.
  116. * \param file_priv DRM file private.
  117. * \param cmd command.
  118. * \param arg pointer to a resulting drm_auth structure.
  119. * \return zero on success, or a negative number on failure.
  120. *
  121. * If there is a magic number in drm_file::magic then use it, otherwise
  122. * searches an unique non-zero magic number and add it associating it with \p
  123. * file_priv.
  124. */
  125. int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
  126. {
  127. static drm_magic_t sequence = 0;
  128. static DEFINE_SPINLOCK(lock);
  129. struct drm_auth *auth = data;
  130. /* Find unique magic */
  131. if (file_priv->magic) {
  132. auth->magic = file_priv->magic;
  133. } else {
  134. do {
  135. spin_lock(&lock);
  136. if (!sequence)
  137. ++sequence; /* reserve 0 */
  138. auth->magic = sequence++;
  139. spin_unlock(&lock);
  140. } while (drm_find_file(dev, auth->magic));
  141. file_priv->magic = auth->magic;
  142. drm_add_magic(dev, file_priv, auth->magic);
  143. }
  144. DRM_DEBUG("%u\n", auth->magic);
  145. return 0;
  146. }
  147. /**
  148. * Authenticate with a magic.
  149. *
  150. * \param inode device inode.
  151. * \param file_priv DRM file private.
  152. * \param cmd command.
  153. * \param arg pointer to a drm_auth structure.
  154. * \return zero if authentication successed, or a negative number otherwise.
  155. *
  156. * Checks if \p file_priv is associated with the magic number passed in \arg.
  157. */
  158. int drm_authmagic(struct drm_device *dev, void *data,
  159. struct drm_file *file_priv)
  160. {
  161. struct drm_auth *auth = data;
  162. struct drm_file *file;
  163. DRM_DEBUG("%u\n", auth->magic);
  164. if ((file = drm_find_file(dev, auth->magic))) {
  165. file->authenticated = 1;
  166. drm_remove_magic(dev, auth->magic);
  167. return 0;
  168. }
  169. return -EINVAL;
  170. }