drm_auth.c 5.8 KB

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