1 // SPDX-License-Identifier: GPL-2.0-only
4 * Mappping of generic netlink family IDs to net devices
6 * Copyright (C) 2005-2006 Intel Corporation <linux-wimax@intel.com>
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 * We assign a single generic netlink family ID to each device (to
12 * We need a way to map family ID to a wimax_dev pointer.
14 * The idea is to use a very simple lookup. Using a netlink attribute
15 * with (for example) the interface name implies a heavier search over
16 * all the network devices; seemed kind of a waste given that we know
17 * we are looking for a WiMAX device and that most systems will have
18 * just a single WiMAX adapter.
20 * We put all the WiMAX devices in the system in a linked list and
21 * match the generic link family ID against the list.
23 * By using a linked list, the case of a single adapter in the system
24 * becomes (almost) no overhead, while still working for many more. If
25 * it ever goes beyond two, I'll be surprised.
27 #include <linux/device.h>
28 #include <net/genetlink.h>
29 #include <linux/netdevice.h>
30 #include <linux/list.h>
31 #include <linux/wimax.h>
32 #include "wimax-internal.h"
35 #define D_SUBMODULE id_table
36 #include "debug-levels.h"
39 static DEFINE_SPINLOCK(wimax_id_table_lock
);
40 static struct list_head wimax_id_table
= LIST_HEAD_INIT(wimax_id_table
);
44 * wimax_id_table_add - add a gennetlink familiy ID / wimax_dev mapping
46 * @wimax_dev: WiMAX device descriptor to associate to the Generic
49 * Look for an empty spot in the ID table; if none found, double the
50 * table's size and get the first spot.
52 void wimax_id_table_add(struct wimax_dev
*wimax_dev
)
54 d_fnstart(3, NULL
, "(wimax_dev %p)\n", wimax_dev
);
55 spin_lock(&wimax_id_table_lock
);
56 list_add(&wimax_dev
->id_table_node
, &wimax_id_table
);
57 spin_unlock(&wimax_id_table_lock
);
58 d_fnend(3, NULL
, "(wimax_dev %p)\n", wimax_dev
);
63 * wimax_get_netdev_by_info - lookup a wimax_dev from the gennetlink info
65 * The generic netlink family ID has been filled out in the
66 * nlmsghdr->nlmsg_type field, so we pull it from there, look it up in
67 * the mapping table and reference the wimax_dev.
69 * When done, the reference should be dropped with
70 * 'dev_put(wimax_dev->net_dev)'.
72 struct wimax_dev
*wimax_dev_get_by_genl_info(
73 struct genl_info
*info
, int ifindex
)
75 struct wimax_dev
*wimax_dev
= NULL
;
77 d_fnstart(3, NULL
, "(info %p ifindex %d)\n", info
, ifindex
);
78 spin_lock(&wimax_id_table_lock
);
79 list_for_each_entry(wimax_dev
, &wimax_id_table
, id_table_node
) {
80 if (wimax_dev
->net_dev
->ifindex
== ifindex
) {
81 dev_hold(wimax_dev
->net_dev
);
86 d_printf(1, NULL
, "wimax: no devices found with ifindex %d\n",
89 spin_unlock(&wimax_id_table_lock
);
90 d_fnend(3, NULL
, "(info %p ifindex %d) = %p\n",
91 info
, ifindex
, wimax_dev
);
97 * wimax_id_table_rm - Remove a gennetlink familiy ID / wimax_dev mapping
99 * @id: family ID to remove from the table
101 void wimax_id_table_rm(struct wimax_dev
*wimax_dev
)
103 spin_lock(&wimax_id_table_lock
);
104 list_del_init(&wimax_dev
->id_table_node
);
105 spin_unlock(&wimax_id_table_lock
);
110 * Release the gennetlink family id / mapping table
112 * On debug, verify that the table is empty upon removal. We want the
113 * code always compiled, to ensure it doesn't bit rot. It will be
114 * compiled out if CONFIG_BUG is disabled.
116 void wimax_id_table_release(void)
118 struct wimax_dev
*wimax_dev
;
123 spin_lock(&wimax_id_table_lock
);
124 list_for_each_entry(wimax_dev
, &wimax_id_table
, id_table_node
) {
125 pr_err("BUG: %s wimax_dev %p ifindex %d not cleared\n",
126 __func__
, wimax_dev
, wimax_dev
->net_dev
->ifindex
);
129 spin_unlock(&wimax_id_table_lock
);