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