blob: 8a37524d0867de8bdc2bf864919ef45a5f12156e [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>
Daniel Vetter67d0ec42014-09-10 12:43:53 +020037#include "drm_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
David Herrmann71d39482014-08-29 12:12:30 +020039struct drm_magic_entry {
David Herrmann71d39482014-08-29 12:12:30 +020040 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);
Dave Airlie30e2fb12006-02-02 19:37:46 +110095 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 return 0;
98}
99
100/**
101 * Remove a magic number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000102 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * \param dev DRM device.
104 * \param magic magic number.
105 *
106 * Searches and unlinks the entry in drm_device::magiclist with the magic
Dave Airlie30e2fb12006-02-02 19:37:46 +1100107 * number hash key, while holding the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
Thomas Hellstrom598781d2012-01-24 18:54:21 +0100109int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000111 struct drm_magic_entry *pt;
Dave Airliee0be4282007-07-12 10:26:44 +1000112 struct drm_hash_item *hash;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000113 struct drm_device *dev = master->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 DRM_DEBUG("%d\n", magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Dave Airlie30e2fb12006-02-02 19:37:46 +1100117 mutex_lock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000118 if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +1000119 mutex_unlock(&dev->struct_mutex);
120 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000122 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000123 drm_ht_remove_item(&master->magiclist, hash);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100124 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Eric Anholt9a298b22009-03-24 12:23:04 -0700126 kfree(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Dave Airlie572225b2006-08-08 22:17:02 +1000128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131/**
132 * Get a unique magic number (ioctl).
133 *
134 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000135 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * \param cmd command.
137 * \param arg pointer to a resulting drm_auth structure.
138 * \return zero on success, or a negative number on failure.
139 *
140 * If there is a magic number in drm_file::magic then use it, otherwise
141 * searches an unique non-zero magic number and add it associating it with \p
Eric Anholt6c340ea2007-08-25 20:23:09 +1000142 * file_priv.
Thomas Hellstrom598781d2012-01-24 18:54:21 +0100143 * This ioctl needs protection by the drm_global_mutex, which protects
144 * struct drm_file::magic and struct drm_magic_entry::priv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
Eric Anholtc153f452007-09-03 12:06:45 +1000146int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 static drm_magic_t sequence = 0;
149 static DEFINE_SPINLOCK(lock);
Eric Anholtc153f452007-09-03 12:06:45 +1000150 struct drm_auth *auth = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000152 /* Find unique magic */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000153 if (file_priv->magic) {
Eric Anholtc153f452007-09-03 12:06:45 +1000154 auth->magic = file_priv->magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 } else {
156 do {
157 spin_lock(&lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000158 if (!sequence)
159 ++sequence; /* reserve 0 */
Eric Anholtc153f452007-09-03 12:06:45 +1000160 auth->magic = sequence++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 spin_unlock(&lock);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000162 } while (drm_find_file(file_priv->master, auth->magic));
Eric Anholtc153f452007-09-03 12:06:45 +1000163 file_priv->magic = auth->magic;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000164 drm_add_magic(file_priv->master, file_priv, auth->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
Eric Anholtc153f452007-09-03 12:06:45 +1000167 DRM_DEBUG("%u\n", auth->magic);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return 0;
170}
171
172/**
173 * Authenticate with a magic.
174 *
175 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000176 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * \param cmd command.
178 * \param arg pointer to a drm_auth structure.
179 * \return zero if authentication successed, or a negative number otherwise.
180 *
Eric Anholt6c340ea2007-08-25 20:23:09 +1000181 * Checks if \p file_priv is associated with the magic number passed in \arg.
Thomas Hellstrom598781d2012-01-24 18:54:21 +0100182 * This ioctl needs protection by the drm_global_mutex, which protects
183 * struct drm_file::magic and struct drm_magic_entry::priv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
Eric Anholtc153f452007-09-03 12:06:45 +1000185int drm_authmagic(struct drm_device *dev, void *data,
186 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Eric Anholtc153f452007-09-03 12:06:45 +1000188 struct drm_auth *auth = data;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000189 struct drm_file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Eric Anholtc153f452007-09-03 12:06:45 +1000191 DRM_DEBUG("%u\n", auth->magic);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000192 if ((file = drm_find_file(file_priv->master, auth->magic))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 file->authenticated = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000194 drm_remove_magic(file_priv->master, auth->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return 0;
196 }
197 return -EINVAL;
198}