blob: fc8e8aaa34fb37f44e338bb237867078d84c020f [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 {
40 struct list_head head;
41 struct drm_hash_item hash_item;
42 struct drm_file *priv;
43};
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * Find the file with the given magic number.
47 *
48 * \param dev DRM device.
49 * \param magic magic number.
50 *
51 * Searches in drm_device::magiclist within all files with the same hash key
Dave Airlie30e2fb12006-02-02 19:37:46 +110052 * the one with matching magic number, while holding the drm_device::struct_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * lock.
54 */
Dave Airlie7c1c2872008-11-28 14:22:24 +100055static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Dave Airlie84b1fd12007-07-11 15:53:27 +100057 struct drm_file *retval = NULL;
Dave Airlie8fc2fdf2007-07-11 16:21:47 +100058 struct drm_magic_entry *pt;
Dave Airliee0be4282007-07-12 10:26:44 +100059 struct drm_hash_item *hash;
Dave Airlie7c1c2872008-11-28 14:22:24 +100060 struct drm_device *dev = master->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Dave Airlie30e2fb12006-02-02 19:37:46 +110062 mutex_lock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +100063 if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
Dave Airlie8fc2fdf2007-07-11 16:21:47 +100064 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100065 retval = pt->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
Dave Airlie30e2fb12006-02-02 19:37:46 +110067 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return retval;
69}
70
71/**
72 * Adds a magic number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100073 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * \param dev DRM device.
75 * \param priv file private data.
76 * \param magic magic number.
77 *
78 * Creates a drm_magic_entry structure and appends to the linked list
79 * associated the magic number hash key in drm_device::magiclist, while holding
Dave Airlie30e2fb12006-02-02 19:37:46 +110080 * the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
Dave Airlie7c1c2872008-11-28 14:22:24 +100082static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100083 drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Dave Airlie8fc2fdf2007-07-11 16:21:47 +100085 struct drm_magic_entry *entry;
Dave Airlie7c1c2872008-11-28 14:22:24 +100086 struct drm_device *dev = master->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 DRM_DEBUG("%d\n", magic);
88
Julia Lawall6ebc22e2010-05-13 21:58:56 +020089 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100090 if (!entry)
91 return -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092 entry->priv = priv;
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100093 entry->hash_item.key = (unsigned long)magic;
Dave Airlie30e2fb12006-02-02 19:37:46 +110094 mutex_lock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +100095 drm_ht_insert_item(&master->magiclist, &entry->hash_item);
96 list_add_tail(&entry->head, &master->magicfree);
Dave Airlie30e2fb12006-02-02 19:37:46 +110097 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 return 0;
100}
101
102/**
103 * Remove a magic number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000104 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * \param dev DRM device.
106 * \param magic magic number.
107 *
108 * Searches and unlinks the entry in drm_device::magiclist with the magic
Dave Airlie30e2fb12006-02-02 19:37:46 +1100109 * number hash key, while holding the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
Thomas Hellstrom598781d2012-01-24 18:54:21 +0100111int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000113 struct drm_magic_entry *pt;
Dave Airliee0be4282007-07-12 10:26:44 +1000114 struct drm_hash_item *hash;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000115 struct drm_device *dev = master->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 DRM_DEBUG("%d\n", magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Dave Airlie30e2fb12006-02-02 19:37:46 +1100119 mutex_lock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000120 if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +1000121 mutex_unlock(&dev->struct_mutex);
122 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000124 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000125 drm_ht_remove_item(&master->magiclist, hash);
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +1000126 list_del(&pt->head);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100127 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Eric Anholt9a298b22009-03-24 12:23:04 -0700129 kfree(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Dave Airlie572225b2006-08-08 22:17:02 +1000131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134/**
135 * Get a unique magic number (ioctl).
136 *
137 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000138 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * \param cmd command.
140 * \param arg pointer to a resulting drm_auth structure.
141 * \return zero on success, or a negative number on failure.
142 *
143 * If there is a magic number in drm_file::magic then use it, otherwise
144 * searches an unique non-zero magic number and add it associating it with \p
Eric Anholt6c340ea2007-08-25 20:23:09 +1000145 * file_priv.
Thomas Hellstrom598781d2012-01-24 18:54:21 +0100146 * This ioctl needs protection by the drm_global_mutex, which protects
147 * struct drm_file::magic and struct drm_magic_entry::priv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 */
Eric Anholtc153f452007-09-03 12:06:45 +1000149int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 static drm_magic_t sequence = 0;
152 static DEFINE_SPINLOCK(lock);
Eric Anholtc153f452007-09-03 12:06:45 +1000153 struct drm_auth *auth = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000155 /* Find unique magic */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000156 if (file_priv->magic) {
Eric Anholtc153f452007-09-03 12:06:45 +1000157 auth->magic = file_priv->magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 } else {
159 do {
160 spin_lock(&lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000161 if (!sequence)
162 ++sequence; /* reserve 0 */
Eric Anholtc153f452007-09-03 12:06:45 +1000163 auth->magic = sequence++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 spin_unlock(&lock);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000165 } while (drm_find_file(file_priv->master, auth->magic));
Eric Anholtc153f452007-09-03 12:06:45 +1000166 file_priv->magic = auth->magic;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000167 drm_add_magic(file_priv->master, file_priv, auth->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169
Eric Anholtc153f452007-09-03 12:06:45 +1000170 DRM_DEBUG("%u\n", auth->magic);
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return 0;
173}
174
175/**
176 * Authenticate with a magic.
177 *
178 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000179 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * \param cmd command.
181 * \param arg pointer to a drm_auth structure.
182 * \return zero if authentication successed, or a negative number otherwise.
183 *
Eric Anholt6c340ea2007-08-25 20:23:09 +1000184 * Checks if \p file_priv is associated with the magic number passed in \arg.
Thomas Hellstrom598781d2012-01-24 18:54:21 +0100185 * This ioctl needs protection by the drm_global_mutex, which protects
186 * struct drm_file::magic and struct drm_magic_entry::priv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
Eric Anholtc153f452007-09-03 12:06:45 +1000188int drm_authmagic(struct drm_device *dev, void *data,
189 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Eric Anholtc153f452007-09-03 12:06:45 +1000191 struct drm_auth *auth = data;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000192 struct drm_file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Eric Anholtc153f452007-09-03 12:06:45 +1000194 DRM_DEBUG("%u\n", auth->magic);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000195 if ((file = drm_find_file(file_priv->master, auth->magic))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 file->authenticated = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000197 drm_remove_magic(file_priv->master, auth->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
199 }
200 return -EINVAL;
201}