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 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34/** @file drm_auth.c
35 * Implementation of the get/authmagic ioctls implementing the authentication
36 * scheme between the master and clients.
37 */
38
39#include <dev/drm2/drmP.h>
40
41static int drm_hash_magic(drm_magic_t magic)
42{
43	return magic & (DRM_HASH_SIZE-1);
44}
45
46/**
47 * Returns the file private associated with the given magic number.
48 */
49static struct drm_file *drm_find_file(struct drm_device *dev, drm_magic_t magic)
50{
51	drm_magic_entry_t *pt;
52	int hash = drm_hash_magic(magic);
53
54	DRM_LOCK_ASSERT(dev);
55
56	for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
57		if (pt->magic == magic) {
58			return pt->priv;
59		}
60	}
61
62	return NULL;
63}
64
65/**
66 * Inserts the given magic number into the hash table of used magic number
67 * lists.
68 */
69static int drm_add_magic(struct drm_device *dev, struct drm_file *priv,
70			 drm_magic_t magic)
71{
72	int		  hash;
73	drm_magic_entry_t *entry;
74
75	DRM_DEBUG("%d\n", magic);
76
77	DRM_LOCK_ASSERT(dev);
78
79	hash = drm_hash_magic(magic);
80	entry = malloc(sizeof(*entry), DRM_MEM_MAGIC, M_ZERO | M_NOWAIT);
81	if (!entry)
82		return ENOMEM;
83	entry->magic = magic;
84	entry->priv  = priv;
85	entry->next  = NULL;
86
87	if (dev->magiclist[hash].tail) {
88		dev->magiclist[hash].tail->next = entry;
89		dev->magiclist[hash].tail	= entry;
90	} else {
91		dev->magiclist[hash].head	= entry;
92		dev->magiclist[hash].tail	= entry;
93	}
94
95	return 0;
96}
97
98/**
99 * Removes the given magic number from the hash table of used magic number
100 * lists.
101 */
102static int drm_remove_magic(struct drm_device *dev, drm_magic_t magic)
103{
104	drm_magic_entry_t *prev = NULL;
105	drm_magic_entry_t *pt;
106	int		  hash;
107
108	DRM_LOCK_ASSERT(dev);
109
110	DRM_DEBUG("%d\n", magic);
111	hash = drm_hash_magic(magic);
112
113	for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
114		if (pt->magic == magic) {
115			if (dev->magiclist[hash].head == pt) {
116				dev->magiclist[hash].head = pt->next;
117			}
118			if (dev->magiclist[hash].tail == pt) {
119				dev->magiclist[hash].tail = prev;
120			}
121			if (prev) {
122				prev->next = pt->next;
123			}
124			free(pt, DRM_MEM_MAGIC);
125			return 0;
126		}
127	}
128
129	return EINVAL;
130}
131
132/**
133 * Called by the client, this returns a unique magic number to be authorized
134 * by the master.
135 *
136 * The master may use its own knowledge of the client (such as the X
137 * connection that the magic is passed over) to determine if the magic number
138 * should be authenticated.
139 */
140int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
141{
142	static drm_magic_t sequence = 0;
143	struct drm_auth *auth = data;
144
145	/* Find unique magic */
146	if (file_priv->magic) {
147		auth->magic = file_priv->magic;
148	} else {
149		DRM_LOCK(dev);
150		do {
151			int old = sequence;
152
153			auth->magic = old+1;
154
155			if (!atomic_cmpset_int(&sequence, old, auth->magic))
156				continue;
157		} while (drm_find_file(dev, auth->magic));
158		file_priv->magic = auth->magic;
159		drm_add_magic(dev, file_priv, auth->magic);
160		DRM_UNLOCK(dev);
161	}
162
163	DRM_DEBUG("%u\n", auth->magic);
164
165	return 0;
166}
167
168/**
169 * Marks the client associated with the given magic number as authenticated.
170 */
171int drm_authmagic(struct drm_device *dev, void *data,
172		  struct drm_file *file_priv)
173{
174	struct drm_auth *auth = data;
175	struct drm_file *priv;
176
177	DRM_DEBUG("%u\n", auth->magic);
178
179	DRM_LOCK(dev);
180	priv = drm_find_file(dev, auth->magic);
181	if (priv != NULL) {
182		priv->authenticated = 1;
183		drm_remove_magic(dev, auth->magic);
184		DRM_UNLOCK(dev);
185		return 0;
186	} else {
187		DRM_UNLOCK(dev);
188		return EINVAL;
189	}
190}
191