blob: 932b5aa96a67ffd6731f87e537827a64ec9916b2 [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
36#include "drmP.h"
37
38/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * Find the file with the given magic number.
40 *
41 * \param dev DRM device.
42 * \param magic magic number.
43 *
44 * Searches in drm_device::magiclist within all files with the same hash key
Dave Airlie30e2fb12006-02-02 19:37:46 +110045 * the one with matching magic number, while holding the drm_device::struct_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * lock.
47 */
Dave Airlie7c1c2872008-11-28 14:22:24 +100048static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Dave Airlie84b1fd12007-07-11 15:53:27 +100050 struct drm_file *retval = NULL;
Dave Airlie8fc2fdf2007-07-11 16:21:47 +100051 struct drm_magic_entry *pt;
Dave Airliee0be4282007-07-12 10:26:44 +100052 struct drm_hash_item *hash;
Dave Airlie7c1c2872008-11-28 14:22:24 +100053 struct drm_device *dev = master->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Dave Airlie30e2fb12006-02-02 19:37:46 +110055 mutex_lock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +100056 if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
Dave Airlie8fc2fdf2007-07-11 16:21:47 +100057 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100058 retval = pt->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
Dave Airlie30e2fb12006-02-02 19:37:46 +110060 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return retval;
62}
63
64/**
65 * Adds a magic number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100066 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * \param dev DRM device.
68 * \param priv file private data.
69 * \param magic magic number.
70 *
71 * Creates a drm_magic_entry structure and appends to the linked list
72 * associated the magic number hash key in drm_device::magiclist, while holding
Dave Airlie30e2fb12006-02-02 19:37:46 +110073 * the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
Dave Airlie7c1c2872008-11-28 14:22:24 +100075static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100076 drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Dave Airlie8fc2fdf2007-07-11 16:21:47 +100078 struct drm_magic_entry *entry;
Dave Airlie7c1c2872008-11-28 14:22:24 +100079 struct drm_device *dev = master->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 DRM_DEBUG("%d\n", magic);
81
Eric Anholt9a298b22009-03-24 12:23:04 -070082 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100083 if (!entry)
84 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 memset(entry, 0, sizeof(*entry));
Dave Airlieb5e89ed2005-09-25 14:28:13 +100086 entry->priv = priv;
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100087 entry->hash_item.key = (unsigned long)magic;
Dave Airlie30e2fb12006-02-02 19:37:46 +110088 mutex_lock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +100089 drm_ht_insert_item(&master->magiclist, &entry->hash_item);
90 list_add_tail(&entry->head, &master->magicfree);
Dave Airlie30e2fb12006-02-02 19:37:46 +110091 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 return 0;
94}
95
96/**
97 * Remove a magic number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * \param dev DRM device.
100 * \param magic magic number.
101 *
102 * Searches and unlinks the entry in drm_device::magiclist with the magic
Dave Airlie30e2fb12006-02-02 19:37:46 +1100103 * number hash key, while holding the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000105static int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000107 struct drm_magic_entry *pt;
Dave Airliee0be4282007-07-12 10:26:44 +1000108 struct drm_hash_item *hash;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000109 struct drm_device *dev = master->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 DRM_DEBUG("%d\n", magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Dave Airlie30e2fb12006-02-02 19:37:46 +1100113 mutex_lock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000114 if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +1000115 mutex_unlock(&dev->struct_mutex);
116 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000118 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000119 drm_ht_remove_item(&master->magiclist, hash);
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +1000120 list_del(&pt->head);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100121 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Eric Anholt9a298b22009-03-24 12:23:04 -0700123 kfree(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Dave Airlie572225b2006-08-08 22:17:02 +1000125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128/**
129 * Get a unique magic number (ioctl).
130 *
131 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000132 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * \param cmd command.
134 * \param arg pointer to a resulting drm_auth structure.
135 * \return zero on success, or a negative number on failure.
136 *
137 * If there is a magic number in drm_file::magic then use it, otherwise
138 * searches an unique non-zero magic number and add it associating it with \p
Eric Anholt6c340ea2007-08-25 20:23:09 +1000139 * file_priv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
Eric Anholtc153f452007-09-03 12:06:45 +1000141int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 static drm_magic_t sequence = 0;
144 static DEFINE_SPINLOCK(lock);
Eric Anholtc153f452007-09-03 12:06:45 +1000145 struct drm_auth *auth = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000147 /* Find unique magic */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000148 if (file_priv->magic) {
Eric Anholtc153f452007-09-03 12:06:45 +1000149 auth->magic = file_priv->magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 } else {
151 do {
152 spin_lock(&lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000153 if (!sequence)
154 ++sequence; /* reserve 0 */
Eric Anholtc153f452007-09-03 12:06:45 +1000155 auth->magic = sequence++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 spin_unlock(&lock);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000157 } while (drm_find_file(file_priv->master, auth->magic));
Eric Anholtc153f452007-09-03 12:06:45 +1000158 file_priv->magic = auth->magic;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000159 drm_add_magic(file_priv->master, file_priv, auth->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
Eric Anholtc153f452007-09-03 12:06:45 +1000162 DRM_DEBUG("%u\n", auth->magic);
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 0;
165}
166
167/**
168 * Authenticate with a magic.
169 *
170 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000171 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * \param cmd command.
173 * \param arg pointer to a drm_auth structure.
174 * \return zero if authentication successed, or a negative number otherwise.
175 *
Eric Anholt6c340ea2007-08-25 20:23:09 +1000176 * Checks if \p file_priv is associated with the magic number passed in \arg.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
Eric Anholtc153f452007-09-03 12:06:45 +1000178int drm_authmagic(struct drm_device *dev, void *data,
179 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Eric Anholtc153f452007-09-03 12:06:45 +1000181 struct drm_auth *auth = data;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000182 struct drm_file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Eric Anholtc153f452007-09-03 12:06:45 +1000184 DRM_DEBUG("%u\n", auth->magic);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000185 if ((file = drm_find_file(file_priv->master, auth->magic))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 file->authenticated = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000187 drm_remove_magic(file_priv->master, auth->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return 0;
189 }
190 return -EINVAL;
191}