xref: /openbsd-src/sys/dev/pci/drm/drm_auth.c (revision 850e275390052b330d93020bf619a739a3c277ac)
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 /** @file drm_auth.c
32  * Implementation of the get/authmagic ioctls implementing the authentication
33  * scheme between the master and clients.
34  */
35 
36 #include "drmP.h"
37 
38 /**
39  * Called by the client, this returns a unique magic number to be authorized
40  * by the master.
41  *
42  * The master may use its own knowledge of the client (such as the X
43  * connection that the magic is passed over) to determine if the magic number
44  * should be authenticated.
45  */
46 int
47 drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
48 {
49 	struct drm_auth		*auth = data;
50 	struct drm_magic_entry	*entry;
51 
52 	/* Find unique magic */
53 	if (file_priv->magic) {
54 		auth->magic = file_priv->magic;
55 	} else {
56 		entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
57 		if (entry == NULL)
58 			return (ENOMEM);
59 		DRM_LOCK();
60 		entry->magic = file_priv->magic = auth->magic = dev->magicid++;
61 		entry->priv = file_priv;
62 		SPLAY_INSERT(drm_magic_tree, &dev->magiclist, entry);
63 
64 		DRM_UNLOCK();
65 		DRM_DEBUG("%d\n", auth->magic);
66 	}
67 
68 	DRM_DEBUG("%u\n", auth->magic);
69 
70 	return (0);
71 }
72 
73 /**
74  * Marks the client associated with the given magic number as authenticated.
75  */
76 int
77 drm_authmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
78 {
79 	struct drm_auth		*auth = data;
80 	struct drm_magic_entry	*pt, key;
81 
82 	DRM_DEBUG("%u\n", auth->magic);
83 
84 	key.magic = auth->magic;
85 	DRM_LOCK();
86 	if ((pt = SPLAY_FIND(drm_magic_tree, &dev->magiclist, &key)) == NULL ||
87 	    pt->priv == NULL) {
88 		DRM_UNLOCK();
89 		return (EINVAL);
90 	}
91 
92 	pt->priv->authenticated = 1;
93 	SPLAY_REMOVE(drm_magic_tree, &dev->magiclist, pt);
94 	DRM_UNLOCK();
95 
96 	drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
97 
98 	return (0);
99 }
100 
101 int
102 drm_magic_cmp(struct drm_magic_entry *dme1, struct drm_magic_entry *dme2)
103 {
104 	return (dme1->magic - dme2->magic);
105 }
106 
107 SPLAY_GENERATE(drm_magic_tree, drm_magic_entry, node, drm_magic_cmp);
108