drm_auth.c revision 1.2.8.2 1 1.2.8.2 tls /**
2 1.2.8.2 tls * \file drm_auth.c
3 1.2.8.2 tls * IOCTLs for authentication
4 1.2.8.2 tls *
5 1.2.8.2 tls * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
6 1.2.8.2 tls * \author Gareth Hughes <gareth (at) valinux.com>
7 1.2.8.2 tls */
8 1.2.8.2 tls
9 1.2.8.2 tls /*
10 1.2.8.2 tls * Created: Tue Feb 2 08:37:54 1999 by faith (at) valinux.com
11 1.2.8.2 tls *
12 1.2.8.2 tls * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 1.2.8.2 tls * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 1.2.8.2 tls * All Rights Reserved.
15 1.2.8.2 tls *
16 1.2.8.2 tls * Permission is hereby granted, free of charge, to any person obtaining a
17 1.2.8.2 tls * copy of this software and associated documentation files (the "Software"),
18 1.2.8.2 tls * to deal in the Software without restriction, including without limitation
19 1.2.8.2 tls * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 1.2.8.2 tls * and/or sell copies of the Software, and to permit persons to whom the
21 1.2.8.2 tls * Software is furnished to do so, subject to the following conditions:
22 1.2.8.2 tls *
23 1.2.8.2 tls * The above copyright notice and this permission notice (including the next
24 1.2.8.2 tls * paragraph) shall be included in all copies or substantial portions of the
25 1.2.8.2 tls * Software.
26 1.2.8.2 tls *
27 1.2.8.2 tls * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 1.2.8.2 tls * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 1.2.8.2 tls * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 1.2.8.2 tls * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 1.2.8.2 tls * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 1.2.8.2 tls * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 1.2.8.2 tls * OTHER DEALINGS IN THE SOFTWARE.
34 1.2.8.2 tls */
35 1.2.8.2 tls
36 1.2.8.2 tls #include <drm/drmP.h>
37 1.2.8.2 tls
38 1.2.8.2 tls /**
39 1.2.8.2 tls * Find the file with the given magic number.
40 1.2.8.2 tls *
41 1.2.8.2 tls * \param dev DRM device.
42 1.2.8.2 tls * \param magic magic number.
43 1.2.8.2 tls *
44 1.2.8.2 tls * Searches in drm_device::magiclist within all files with the same hash key
45 1.2.8.2 tls * the one with matching magic number, while holding the drm_device::struct_mutex
46 1.2.8.2 tls * lock.
47 1.2.8.2 tls */
48 1.2.8.2 tls static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
49 1.2.8.2 tls {
50 1.2.8.2 tls struct drm_file *retval = NULL;
51 1.2.8.2 tls struct drm_magic_entry *pt;
52 1.2.8.2 tls struct drm_hash_item *hash;
53 1.2.8.2 tls struct drm_device *dev = master->minor->dev;
54 1.2.8.2 tls
55 1.2.8.2 tls mutex_lock(&dev->struct_mutex);
56 1.2.8.2 tls if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
57 1.2.8.2 tls pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
58 1.2.8.2 tls retval = pt->priv;
59 1.2.8.2 tls }
60 1.2.8.2 tls mutex_unlock(&dev->struct_mutex);
61 1.2.8.2 tls return retval;
62 1.2.8.2 tls }
63 1.2.8.2 tls
64 1.2.8.2 tls /**
65 1.2.8.2 tls * Adds a magic number.
66 1.2.8.2 tls *
67 1.2.8.2 tls * \param dev DRM device.
68 1.2.8.2 tls * \param priv file private data.
69 1.2.8.2 tls * \param magic magic number.
70 1.2.8.2 tls *
71 1.2.8.2 tls * Creates a drm_magic_entry structure and appends to the linked list
72 1.2.8.2 tls * associated the magic number hash key in drm_device::magiclist, while holding
73 1.2.8.2 tls * the drm_device::struct_mutex lock.
74 1.2.8.2 tls */
75 1.2.8.2 tls static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
76 1.2.8.2 tls drm_magic_t magic)
77 1.2.8.2 tls {
78 1.2.8.2 tls struct drm_magic_entry *entry;
79 1.2.8.2 tls struct drm_device *dev = master->minor->dev;
80 1.2.8.2 tls DRM_DEBUG("%d\n", magic);
81 1.2.8.2 tls
82 1.2.8.2 tls entry = kzalloc(sizeof(*entry), GFP_KERNEL);
83 1.2.8.2 tls if (!entry)
84 1.2.8.2 tls return -ENOMEM;
85 1.2.8.2 tls entry->priv = priv;
86 1.2.8.2 tls entry->hash_item.key = (unsigned long)magic;
87 1.2.8.2 tls mutex_lock(&dev->struct_mutex);
88 1.2.8.2 tls drm_ht_insert_item(&master->magiclist, &entry->hash_item);
89 1.2.8.2 tls list_add_tail(&entry->head, &master->magicfree);
90 1.2.8.2 tls mutex_unlock(&dev->struct_mutex);
91 1.2.8.2 tls
92 1.2.8.2 tls return 0;
93 1.2.8.2 tls }
94 1.2.8.2 tls
95 1.2.8.2 tls /**
96 1.2.8.2 tls * Remove a magic number.
97 1.2.8.2 tls *
98 1.2.8.2 tls * \param dev DRM device.
99 1.2.8.2 tls * \param magic magic number.
100 1.2.8.2 tls *
101 1.2.8.2 tls * Searches and unlinks the entry in drm_device::magiclist with the magic
102 1.2.8.2 tls * number hash key, while holding the drm_device::struct_mutex lock.
103 1.2.8.2 tls */
104 1.2.8.2 tls int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
105 1.2.8.2 tls {
106 1.2.8.2 tls struct drm_magic_entry *pt;
107 1.2.8.2 tls struct drm_hash_item *hash;
108 1.2.8.2 tls struct drm_device *dev = master->minor->dev;
109 1.2.8.2 tls
110 1.2.8.2 tls DRM_DEBUG("%d\n", magic);
111 1.2.8.2 tls
112 1.2.8.2 tls mutex_lock(&dev->struct_mutex);
113 1.2.8.2 tls if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
114 1.2.8.2 tls mutex_unlock(&dev->struct_mutex);
115 1.2.8.2 tls return -EINVAL;
116 1.2.8.2 tls }
117 1.2.8.2 tls pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
118 1.2.8.2 tls drm_ht_remove_item(&master->magiclist, hash);
119 1.2.8.2 tls list_del(&pt->head);
120 1.2.8.2 tls mutex_unlock(&dev->struct_mutex);
121 1.2.8.2 tls
122 1.2.8.2 tls kfree(pt);
123 1.2.8.2 tls
124 1.2.8.2 tls return 0;
125 1.2.8.2 tls }
126 1.2.8.2 tls
127 1.2.8.2 tls /**
128 1.2.8.2 tls * Get a unique magic number (ioctl).
129 1.2.8.2 tls *
130 1.2.8.2 tls * \param inode device inode.
131 1.2.8.2 tls * \param file_priv DRM file private.
132 1.2.8.2 tls * \param cmd command.
133 1.2.8.2 tls * \param arg pointer to a resulting drm_auth structure.
134 1.2.8.2 tls * \return zero on success, or a negative number on failure.
135 1.2.8.2 tls *
136 1.2.8.2 tls * If there is a magic number in drm_file::magic then use it, otherwise
137 1.2.8.2 tls * searches an unique non-zero magic number and add it associating it with \p
138 1.2.8.2 tls * file_priv.
139 1.2.8.2 tls * This ioctl needs protection by the drm_global_mutex, which protects
140 1.2.8.2 tls * struct drm_file::magic and struct drm_magic_entry::priv.
141 1.2.8.2 tls */
142 1.2.8.2 tls int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
143 1.2.8.2 tls {
144 1.2.8.2 tls static atomic_t sequence = ATOMIC_INIT(0);
145 1.2.8.2 tls struct drm_auth *auth = data;
146 1.2.8.2 tls
147 1.2.8.2 tls /* Find unique magic */
148 1.2.8.2 tls if (file_priv->magic) {
149 1.2.8.2 tls auth->magic = file_priv->magic;
150 1.2.8.2 tls } else {
151 1.2.8.2 tls do {
152 1.2.8.2 tls auth->magic = atomic_inc_return(&sequence);
153 1.2.8.2 tls } while (drm_find_file(file_priv->master, auth->magic));
154 1.2.8.2 tls file_priv->magic = auth->magic;
155 1.2.8.2 tls drm_add_magic(file_priv->master, file_priv, auth->magic);
156 1.2.8.2 tls }
157 1.2.8.2 tls
158 1.2.8.2 tls DRM_DEBUG("%u\n", auth->magic);
159 1.2.8.2 tls
160 1.2.8.2 tls return 0;
161 1.2.8.2 tls }
162 1.2.8.2 tls
163 1.2.8.2 tls /**
164 1.2.8.2 tls * Authenticate with a magic.
165 1.2.8.2 tls *
166 1.2.8.2 tls * \param inode device inode.
167 1.2.8.2 tls * \param file_priv DRM file private.
168 1.2.8.2 tls * \param cmd command.
169 1.2.8.2 tls * \param arg pointer to a drm_auth structure.
170 1.2.8.2 tls * \return zero if authentication successed, or a negative number otherwise.
171 1.2.8.2 tls *
172 1.2.8.2 tls * Checks if \p file_priv is associated with the magic number passed in \arg.
173 1.2.8.2 tls * This ioctl needs protection by the drm_global_mutex, which protects
174 1.2.8.2 tls * struct drm_file::magic and struct drm_magic_entry::priv.
175 1.2.8.2 tls */
176 1.2.8.2 tls int drm_authmagic(struct drm_device *dev, void *data,
177 1.2.8.2 tls struct drm_file *file_priv)
178 1.2.8.2 tls {
179 1.2.8.2 tls struct drm_auth *auth = data;
180 1.2.8.2 tls struct drm_file *file;
181 1.2.8.2 tls
182 1.2.8.2 tls DRM_DEBUG("%u\n", auth->magic);
183 1.2.8.2 tls if ((file = drm_find_file(file_priv->master, auth->magic))) {
184 1.2.8.2 tls file->authenticated = 1;
185 1.2.8.2 tls drm_remove_magic(file_priv->master, auth->magic);
186 1.2.8.2 tls return 0;
187 1.2.8.2 tls }
188 1.2.8.2 tls return -EINVAL;
189 1.2.8.2 tls }
190