// SPDX-License-Identifier: GPL-2.0-or-later
/* AFS cell and server record management
*
* Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*/
#include <linux/slab.h>
#include <linux/key.h>
#include <linux/ctype.h>
#include <linux/dns_resolver.h>
#include <linux/sched.h>
#include <linux/inet.h>
#include <linux/namei.h>
#include <keys/rxrpc-type.h>
#include "internal.h"
static unsigned __read_mostly afs_cell_gc_delay = 10;
static unsigned __read_mostly afs_cell_min_ttl = 10 * 60;
static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60;
static void afs_manage_cell(struct work_struct *);
static void afs_dec_cells_outstanding(struct afs_net *net)
{
if (atomic_dec_and_test(&net->cells_outstanding))
wake_up_var(&net->cells_outstanding);
}
/*
* Set the cell timer to fire after a given delay, assuming it's not already
* set for an earlier time.
*/
static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
{
if (net->live) {
atomic_inc(&net->cells_outstanding);
if (timer_reduce(&net->cells_timer, jiffies + delay * HZ))
afs_dec_cells_outstanding(net);
}
}
/*
* Look up and get an activation reference on a cell record under RCU
* conditions. The caller must hold the RCU read lock.
*/
struct afs_cell *afs_lookup_cell_rcu(struct afs_net *net,
const char *name, unsigned int namesz)
{
struct afs_cell *cell = NULL;
struct rb_node *p;
int n, seq = 0, ret = 0;
_enter("%*.*s", namesz, namesz, name);
if (name && namesz == 0)
return ERR_PTR(-EINVAL);
if (namesz > AFS_MAXCELLNAME)
return ERR_PTR(-ENAMETOOLONG);
do {
/* Unfortunately, rbtree walking doesn't give reliable results
* under just the RCU read lock, so we have to check for
* changes.
*/
if (cell)
afs_put_cell(net, cell);
cell = NULL;
ret = -ENOENT;
read_seqbegin_or_lock(&net->cells_lock, &seq);
if (!name) {
cell = rcu_dereference_raw(net->ws_cell);
if (cell) {
afs_get_cell(cell);
ret = 0;
break;
}
ret = -EDESTADDRREQ;
continue;
}
p = rcu_dereference_raw(net->cells.rb_node);
while (p) {
cell = rb_entry(p, struct afs_cell, net_node);
n = strncasecmp(cell->name, name,
min_t(size_t, cell->name_len, namesz));
if (n == 0)
n = cell->name_len - namesz;
if (n < 0) {
p = rcu_dereference_raw(p->rb_left);
} else if (n > 0) {
p = rcu_dereference_raw(p->rb_right);
} else {
if (atomic_inc_not_zero(&cell->usage)) {
ret = 0;
break;
}
/* We want to repeat the search, this time with
* the lock properly locked.
*/
}
cell = NULL;
}
} while (need_seqretry(&net->cells_lock, seq));
done_seqretry(&net->cells_lock, seq);
if (ret != 0 && cell)
afs_put_cell(net, cell);
return ret == 0 ? cell : ERR_PTR(ret);
}
/*
* Set up a cell record and fill in its name, VL server address list and
* allocate an anonymous key
*/
static struct afs_cell *afs_alloc_cell(struct afs_net *net,
const char *name, unsigned int namelen,
const char *addresses)
{
struct afs_vlserver_list *vllist;
struct afs_cell *cell;
int i, ret;
ASSERT(name);
if (namelen == 0)
return ERR_PTR(-EINVAL);
if (namelen > AFS_MAXCELLNAME) {
_leave(" = -ENAMETOOLONG");
return ERR_PTR(-ENAMETOOLONG);
}
/* Prohibit cell names that contain unprintable chars, '/' and '@' or
* that begin with a dot. This also precludes "@cell".
*/
if (name[0] == '.')
return ERR_PTR(-EINVAL);
for (i = 0; i < namelen; i++) {
char ch = name[i];
if (!isprint(ch) || ch == '/' || ch == '@')
return ERR_PTR(-EINVAL);
}
_enter("%*.*s,%s", namelen, namelen, name, addresses);
cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
if (!cell) {
_leave(" = -ENOMEM");
return ERR_PTR(-ENOMEM);
}
cell->name = kmalloc(namelen + 1, GFP_KERNEL);
if (!cell->name) {
kfree(cell);
return ERR_PTR(-ENOMEM);
}
cell->net =