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