1 /*
2  * Linux WiMAX
3  * Mappping of generic netlink family IDs to net devices
4  *
5  *
6  * Copyright (C) 2005-2006 Intel Corporation <linux-wimax@intel.com>
7  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version
11  * 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301, USA.
22  *
23  *
24  * We assign a single generic netlink family ID to each device (to
25  * simplify lookup).
26  *
27  * We need a way to map family ID to a wimax_dev pointer.
28  *
29  * The idea is to use a very simple lookup. Using a netlink attribute
30  * with (for example) the interface name implies a heavier search over
31  * all the network devices; seemed kind of a waste given that we know
32  * we are looking for a WiMAX device and that most systems will have
33  * just a single WiMAX adapter.
34  *
35  * We put all the WiMAX devices in the system in a linked list and
36  * match the generic link family ID against the list.
37  *
38  * By using a linked list, the case of a single adapter in the system
39  * becomes (almost) no overhead, while still working for many more. If
40  * it ever goes beyond two, I'll be surprised.
41  */
42 #include <linux/device.h>
43 #include <net/genetlink.h>
44 #include <linux/netdevice.h>
45 #include <linux/list.h>
46 #include <linux/wimax.h>
47 #include "wimax-internal.h"
48 
49 
50 #define D_SUBMODULE id_table
51 #include "debug-levels.h"
52 
53 
54 static DEFINE_SPINLOCK(wimax_id_table_lock);
55 static struct list_head wimax_id_table = LIST_HEAD_INIT(wimax_id_table);
56 
57 
58 /*
59  * wimax_id_table_add - add a gennetlink familiy ID / wimax_dev mapping
60  *
61  * @wimax_dev: WiMAX device descriptor to associate to the Generic
62  *     Netlink family ID.
63  *
64  * Look for an empty spot in the ID table; if none found, double the
65  * table's size and get the first spot.
66  */
wimax_id_table_add(struct wimax_dev * wimax_dev)67 void wimax_id_table_add(struct wimax_dev *wimax_dev)
68 {
69 	d_fnstart(3, NULL, "(wimax_dev %p)\n", wimax_dev);
70 	spin_lock(&wimax_id_table_lock);
71 	list_add(&wimax_dev->id_table_node, &wimax_id_table);
72 	spin_unlock(&wimax_id_table_lock);
73 	d_fnend(3, NULL, "(wimax_dev %p)\n", wimax_dev);
74 }
75 
76 
77 /*
78  * wimax_get_netdev_by_info - lookup a wimax_dev from the gennetlink info
79  *
80  * The generic netlink family ID has been filled out in the
81  * nlmsghdr->nlmsg_type field, so we pull it from there, look it up in
82  * the mapping table and reference the wimax_dev.
83  *
84  * When done, the reference should be dropped with
85  * 'dev_put(wimax_dev->net_dev)'.
86  */
wimax_dev_get_by_genl_info(struct genl_info * info,int ifindex)87 struct wimax_dev *wimax_dev_get_by_genl_info(
88 	struct genl_info *info, int ifindex)
89 {
90 	struct wimax_dev *wimax_dev = NULL;
91 
92 	d_fnstart(3, NULL, "(info %p ifindex %d)\n", info, ifindex);
93 	spin_lock(&wimax_id_table_lock);
94 	list_for_each_entry(wimax_dev, &wimax_id_table, id_table_node) {
95 		if (wimax_dev->net_dev->ifindex == ifindex) {
96 			dev_hold(wimax_dev->net_dev);
97 			goto found;
98 		}
99 	}
100 	wimax_dev = NULL;
101 	d_printf(1, NULL, "wimax: no devices found with ifindex %d\n",
102 		 ifindex);
103 found:
104 	spin_unlock(&wimax_id_table_lock);
105 	d_fnend(3, NULL, "(info %p ifindex %d) = %p\n",
106 		info, ifindex, wimax_dev);
107 	return wimax_dev;
108 }
109 
110 
111 /*
112  * wimax_id_table_rm - Remove a gennetlink familiy ID / wimax_dev mapping
113  *
114  * @id: family ID to remove from the table
115  */
wimax_id_table_rm(struct wimax_dev * wimax_dev)116 void wimax_id_table_rm(struct wimax_dev *wimax_dev)
117 {
118 	spin_lock(&wimax_id_table_lock);
119 	list_del_init(&wimax_dev->id_table_node);
120 	spin_unlock(&wimax_id_table_lock);
121 }
122 
123 
124 /*
125  * Release the gennetlink family id / mapping table
126  *
127  * On debug, verify that the table is empty upon removal. We want the
128  * code always compiled, to ensure it doesn't bit rot. It will be
129  * compiled out if CONFIG_BUG is disabled.
130  */
wimax_id_table_release(void)131 void wimax_id_table_release(void)
132 {
133 	struct wimax_dev *wimax_dev;
134 
135 #ifndef CONFIG_BUG
136 	return;
137 #endif
138 	spin_lock(&wimax_id_table_lock);
139 	list_for_each_entry(wimax_dev, &wimax_id_table, id_table_node) {
140 		pr_err("BUG: %s wimax_dev %p ifindex %d not cleared\n",
141 		       __func__, wimax_dev, wimax_dev->net_dev->ifindex);
142 		WARN_ON(1);
143 	}
144 	spin_unlock(&wimax_id_table_lock);
145 }
146