drm_auth.c 5.6 KB

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