2 * Implementation of the SID table type.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
8 #define SIDTAB_HASH(sid) \
9 (sid & SIDTAB_HASH_MASK)
11 #define INIT_SIDTAB_LOCK(s) spin_lock_init(&s->lock)
12 #define SIDTAB_LOCK(s) spin_lock_irq(&s->lock)
13 #define SIDTAB_UNLOCK(s) spin_unlock_irq(&s->lock)
15 int sidtab_init(struct sidtab *s)
19 s->htable = kmalloc(sizeof(*(s->htable)) * SIDTAB_SIZE, GFP_ATOMIC);
22 for (i = 0; i < SIDTAB_SIZE; i++)
31 int sidtab_insert(struct sidtab *s, u32 sid, struct context *context)
34 struct sidtab_node *prev, *cur, *newnode;
41 hvalue = SIDTAB_HASH(sid);
43 cur = s->htable[hvalue];
44 while (cur != NULL && sid > cur->sid) {
49 if (cur && sid == cur->sid) {
54 newnode = kmalloc(sizeof(*newnode), GFP_ATOMIC);
55 if (newnode == NULL) {
60 if (context_cpy(&newnode->context, context)) {
67 newnode->next = prev->next;
71 newnode->next = s->htable[hvalue];
73 s->htable[hvalue] = newnode;
77 if (sid >= s->next_sid)
78 s->next_sid = sid + 1;
83 int sidtab_remove(struct sidtab *s, u32 sid)
86 struct sidtab_node *cur, *last;
93 hvalue = SIDTAB_HASH(sid);
95 cur = s->htable[hvalue];
96 while (cur != NULL && sid > cur->sid) {
101 if (cur == NULL || sid != cur->sid) {
107 s->htable[hvalue] = cur->next;
109 last->next = cur->next;
111 context_destroy(&cur->context);
119 struct context *sidtab_search(struct sidtab *s, u32 sid)
122 struct sidtab_node *cur;
127 hvalue = SIDTAB_HASH(sid);
128 cur = s->htable[hvalue];
129 while (cur != NULL && sid > cur->sid)
132 if (cur == NULL || sid != cur->sid) {
133 /* Remap invalid SIDs to the unlabeled SID. */
134 sid = SECINITSID_UNLABELED;
135 hvalue = SIDTAB_HASH(sid);
136 cur = s->htable[hvalue];
137 while (cur != NULL && sid > cur->sid)
139 if (!cur || sid != cur->sid)
143 return &cur->context;
146 int sidtab_map(struct sidtab *s,
147 int (*apply) (u32 sid,
148 struct context *context,
153 struct sidtab_node *cur;
158 for (i = 0; i < SIDTAB_SIZE; i++) {
160 while (cur != NULL) {
161 rc = apply(cur->sid, &cur->context, args);
171 void sidtab_map_remove_on_error(struct sidtab *s,
172 int (*apply) (u32 sid,
173 struct context *context,
178 struct sidtab_node *last, *cur, *temp;
183 for (i = 0; i < SIDTAB_SIZE; i++) {
186 while (cur != NULL) {
187 ret = apply(cur->sid, &cur->context, args);
190 last->next = cur->next;
192 s->htable[i] = cur->next;
197 context_destroy(&temp->context);
210 static inline u32 sidtab_search_context(struct sidtab *s,
211 struct context *context)
214 struct sidtab_node *cur;
216 for (i = 0; i < SIDTAB_SIZE; i++) {
218 while (cur != NULL) {
219 if (context_cmp(&cur->context, context))
227 int sidtab_context_to_sid(struct sidtab *s,
228 struct context *context,
234 *out_sid = SECSID_NULL;
236 sid = sidtab_search_context(s, context);
239 /* Rescan now that we hold the lock. */
240 sid = sidtab_search_context(s, context);
243 /* No SID exists for the context. Allocate a new one. */
244 if (s->next_sid == UINT_MAX || s->shutdown) {
249 ret = sidtab_insert(s, sid, context);
263 void sidtab_hash_eval(struct sidtab *h, char *tag)
265 int i, chain_len, slots_used, max_chain_len;
266 struct sidtab_node *cur;
270 for (i = 0; i < SIDTAB_SIZE; i++) {
280 if (chain_len > max_chain_len)
281 max_chain_len = chain_len;
285 printk(KERN_INFO "%s: %d entries and %d/%d buckets used, longest "
286 "chain length %d\n", tag, h->nel, slots_used, SIDTAB_SIZE,
290 void sidtab_destroy(struct sidtab *s)
293 struct sidtab_node *cur, *temp;
298 for (i = 0; i < SIDTAB_SIZE; i++) {
300 while (cur != NULL) {
303 context_destroy(&temp->context);
314 void sidtab_set(struct sidtab *dst, struct sidtab *src)
317 dst->htable = src->htable;
319 dst->next_sid = src->next_sid;
324 void sidtab_shutdown(struct sidtab *s)