xref: /dragonfly/sys/dev/drm/drm_auth.c (revision 6c2b3e4e)
1 /*-
2  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Rickard E. (Rik) Faith <faith@valinux.com>
27  *    Gareth Hughes <gareth@valinux.com>
28  *
29  * $FreeBSD: src/sys/dev/drm2/drm_auth.c,v 1.1 2012/05/22 11:07:44 kib Exp $
30  */
31 
32 /** @file drm_auth.c
33  * Implementation of the get/authmagic ioctls implementing the authentication
34  * scheme between the master and clients.
35  */
36 
37 #include <drm/drmP.h>
38 
39 static int drm_hash_magic(drm_magic_t magic)
40 {
41 	return magic & (DRM_HASH_SIZE-1);
42 }
43 
44 /**
45  * Returns the file private associated with the given magic number.
46  */
47 static struct drm_file *drm_find_file(struct drm_device *dev, drm_magic_t magic)
48 {
49 	drm_magic_entry_t *pt;
50 	int hash = drm_hash_magic(magic);
51 
52 	DRM_LOCK_ASSERT(dev);
53 
54 	for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
55 		if (pt->magic == magic) {
56 			return pt->priv;
57 		}
58 	}
59 
60 	return NULL;
61 }
62 
63 /**
64  * Inserts the given magic number into the hash table of used magic number
65  * lists.
66  */
67 static int drm_add_magic(struct drm_device *dev, struct drm_file *priv,
68 			 drm_magic_t magic)
69 {
70 	int		  hash;
71 	drm_magic_entry_t *entry;
72 
73 	DRM_DEBUG("%d\n", magic);
74 
75 	DRM_LOCK_ASSERT(dev);
76 
77 	hash = drm_hash_magic(magic);
78 	entry = kmalloc(sizeof(*entry), DRM_MEM_MAGIC, M_ZERO | M_NOWAIT);
79 	if (!entry)
80 		return ENOMEM;
81 	entry->magic = magic;
82 	entry->priv  = priv;
83 	entry->next  = NULL;
84 
85 	if (dev->magiclist[hash].tail) {
86 		dev->magiclist[hash].tail->next = entry;
87 		dev->magiclist[hash].tail	= entry;
88 	} else {
89 		dev->magiclist[hash].head	= entry;
90 		dev->magiclist[hash].tail	= entry;
91 	}
92 
93 	return 0;
94 }
95 
96 /**
97  * Removes the given magic number from the hash table of used magic number
98  * lists.
99  */
100 static int drm_remove_magic(struct drm_device *dev, drm_magic_t magic)
101 {
102 	drm_magic_entry_t *prev = NULL;
103 	drm_magic_entry_t *pt;
104 	int		  hash;
105 
106 	DRM_LOCK_ASSERT(dev);
107 
108 	DRM_DEBUG("%d\n", magic);
109 	hash = drm_hash_magic(magic);
110 
111 	for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
112 		if (pt->magic == magic) {
113 			if (dev->magiclist[hash].head == pt) {
114 				dev->magiclist[hash].head = pt->next;
115 			}
116 			if (dev->magiclist[hash].tail == pt) {
117 				dev->magiclist[hash].tail = prev;
118 			}
119 			if (prev) {
120 				prev->next = pt->next;
121 			}
122 			drm_free(pt, DRM_MEM_MAGIC);
123 			return 0;
124 		}
125 	}
126 
127 	return EINVAL;
128 }
129 
130 /**
131  * Called by the client, this returns a unique magic number to be authorized
132  * by the master.
133  *
134  * The master may use its own knowledge of the client (such as the X
135  * connection that the magic is passed over) to determine if the magic number
136  * should be authenticated.
137  */
138 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
139 {
140 	static drm_magic_t sequence = 0;
141 	struct drm_auth *auth = data;
142 
143 	/* Find unique magic */
144 	if (file_priv->magic) {
145 		auth->magic = file_priv->magic;
146 	} else {
147 		DRM_LOCK(dev);
148 		do {
149 			int old = sequence;
150 
151 			auth->magic = old+1;
152 
153 			if (!atomic_cmpset_int(&sequence, old, auth->magic))
154 				continue;
155 		} while (drm_find_file(dev, auth->magic));
156 		file_priv->magic = auth->magic;
157 		drm_add_magic(dev, file_priv, auth->magic);
158 		DRM_UNLOCK(dev);
159 	}
160 
161 	DRM_DEBUG("%u\n", auth->magic);
162 
163 	return 0;
164 }
165 
166 /**
167  * Marks the client associated with the given magic number as authenticated.
168  */
169 int drm_authmagic(struct drm_device *dev, void *data,
170 		  struct drm_file *file_priv)
171 {
172 	struct drm_auth *auth = data;
173 	struct drm_file *priv;
174 
175 	DRM_DEBUG("%u\n", auth->magic);
176 
177 	DRM_LOCK(dev);
178 	priv = drm_find_file(dev, auth->magic);
179 	if (priv != NULL) {
180 		priv->authenticated = 1;
181 		drm_remove_magic(dev, auth->magic);
182 		DRM_UNLOCK(dev);
183 		return 0;
184 	} else {
185 		DRM_UNLOCK(dev);
186 		return EINVAL;
187 	}
188 }
189