blob: 708a2044c6313bfba4183c33495167a8a04859ab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_auth.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * IOCTLs for authentication
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
11 *
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drmP.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
David Herrmann71d39482014-08-29 12:12:30 +020038struct drm_magic_entry {
39 struct list_head head;
40 struct drm_hash_item hash_item;
41 struct drm_file *priv;
42};
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * Find the file with the given magic number.
46 *
47 * \param dev DRM device.
48 * \param magic magic number.
49 *
50 * Searches in drm_device::magiclist within all files with the same hash key
Dave Airlie30e2fb12006-02-02 19:37:46 +110051 * the one with matching magic number, while holding the drm_device::struct_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * lock.
53 */
Dave Airlie7c1c2872008-11-28 14:22:24 +100054static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Dave Airlie84b1fd12007-07-11 15:53:27 +100056 struct drm_file *retval = NULL;
Dave Airlie8fc2fdf2007-07-11 16:21:47 +100057 struct drm_magic_entry *pt;
Dave Airliee0be4282007-07-12 10:26:44 +100058 struct drm_hash_item *hash;
Dave Airlie7c1c2872008-11-28 14:22:24 +100059 struct drm_device *dev = master->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Dave Airlie30e2fb12006-02-02 19:37:46 +110061 mutex_lock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +100062 if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
Dave Airlie8fc2fdf2007-07-11 16:21:47 +100063 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100064 retval = pt->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
Dave Airlie30e2fb12006-02-02 19:37:46 +110066 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return retval;
68}
69
70/**
71 * Adds a magic number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100072 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * \param dev DRM device.
74 * \param priv file private data.
75 * \param magic magic number.
76 *
77 * Creates a drm_magic_entry structure and appends to the linked list
78 * associated the magic number hash key in drm_device::magiclist, while holding
Dave Airlie30e2fb12006-02-02 19:37:46 +110079 * the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
Dave Airlie7c1c2872008-11-28 14:22:24 +100081static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100082 drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Dave Airlie8fc2fdf2007-07-11 16:21:47 +100084 struct drm_magic_entry *entry;
Dave Airlie7c1c2872008-11-28 14:22:24 +100085 struct drm_device *dev = master->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 DRM_DEBUG("%d\n", magic);
87
Julia Lawall6ebc22e2010-05-13 21:58:56 +020088 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100089 if (!entry)
90 return -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100091 entry->priv = priv;
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100092 entry->hash_item.key = (unsigned long)magic;
Dave Airlie30e2fb12006-02-02 19:37:46 +110093 mutex_lock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +100094 drm_ht_insert_item(&master->magiclist, &entry->hash_item);
95 list_add_tail(&entry->head, &master->magicfree);
Dave Airlie30e2fb12006-02-02 19:37:46 +110096 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 return 0;
99}
100
101/**
102 * Remove a magic number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000103 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * \param dev DRM device.
105 * \param magic magic number.
106 *
107 * Searches and unlinks the entry in drm_device::magiclist with the magic
Dave Airlie30e2fb12006-02-02 19:37:46 +1100108 * number hash key, while holding the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
Thomas Hellstrom598781d2012-01-24 18:54:21 +0100110int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000112 struct drm_magic_entry *pt;
Dave Airliee0be4282007-07-12 10:26:44 +1000113 struct drm_hash_item *hash;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000114 struct drm_device *dev = master->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 DRM_DEBUG("%d\n", magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Dave Airlie30e2fb12006-02-02 19:37:46 +1100118 mutex_lock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000119 if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +1000120 mutex_unlock(&dev->struct_mutex);
121 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000123 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000124 drm_ht_remove_item(&master->magiclist, hash);
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +1000125 list_del(&pt->head);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100126 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Eric Anholt9a298b22009-03-24 12:23:04 -0700128 kfree(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Dave Airlie572225b2006-08-08 22:17:02 +1000130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133/**
134 * Get a unique magic number (ioctl).
135 *
136 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000137 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * \param cmd command.
139 * \param arg pointer to a resulting drm_auth structure.
140 * \return zero on success, or a negative number on failure.
141 *
142 * If there is a magic number in drm_file::magic then use it, otherwise
143 * searches an unique non-zero magic number and add it associating it with \p
Eric Anholt6c340ea2007-08-25 20:23:09 +1000144 * file_priv.
Thomas Hellstrom598781d2012-01-24 18:54:21 +0100145 * This ioctl needs protection by the drm_global_mutex, which protects
146 * struct drm_file::magic and struct drm_magic_entry::priv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 */
Eric Anholtc153f452007-09-03 12:06:45 +1000148int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 static drm_magic_t sequence = 0;
151 static DEFINE_SPINLOCK(lock);
Eric Anholtc153f452007-09-03 12:06:45 +1000152 struct drm_auth *auth = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000154 /* Find unique magic */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000155 if (file_priv->magic) {
Eric Anholtc153f452007-09-03 12:06:45 +1000156 auth->magic = file_priv->magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 } else {
158 do {
159 spin_lock(&lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000160 if (!sequence)
161 ++sequence; /* reserve 0 */
Eric Anholtc153f452007-09-03 12:06:45 +1000162 auth->magic = sequence++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 spin_unlock(&lock);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000164 } while (drm_find_file(file_priv->master, auth->magic));
Eric Anholtc153f452007-09-03 12:06:45 +1000165 file_priv->magic = auth->magic;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000166 drm_add_magic(file_priv->master, file_priv, auth->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
Eric Anholtc153f452007-09-03 12:06:45 +1000169 DRM_DEBUG("%u\n", auth->magic);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
172}
173
174/**
175 * Authenticate with a magic.
176 *
177 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000178 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * \param cmd command.
180 * \param arg pointer to a drm_auth structure.
181 * \return zero if authentication successed, or a negative number otherwise.
182 *
Eric Anholt6c340ea2007-08-25 20:23:09 +1000183 * Checks if \p file_priv is associated with the magic number passed in \arg.
Thomas Hellstrom598781d2012-01-24 18:54:21 +0100184 * This ioctl needs protection by the drm_global_mutex, which protects
185 * struct drm_file::magic and struct drm_magic_entry::priv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
Eric Anholtc153f452007-09-03 12:06:45 +1000187int drm_authmagic(struct drm_device *dev, void *data,
188 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Eric Anholtc153f452007-09-03 12:06:45 +1000190 struct drm_auth *auth = data;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000191 struct drm_file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Eric Anholtc153f452007-09-03 12:06:45 +1000193 DRM_DEBUG("%u\n", auth->magic);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000194 if ((file = drm_find_file(file_priv->master, auth->magic))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 file->authenticated = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000196 drm_remove_magic(file_priv->master, auth->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 0;
198 }
199 return -EINVAL;
200}