Path: blob/master/drivers/infiniband/core/netlink.c
37212 views
/*1* Copyright (c) 2010 Voltaire Inc. All rights reserved.2*3* This software is available to you under a choice of one of two4* licenses. You may choose to be licensed under the terms of the GNU5* General Public License (GPL) Version 2, available from the file6* COPYING in the main directory of this source tree, or the7* OpenIB.org BSD license below:8*9* Redistribution and use in source and binary forms, with or10* without modification, are permitted provided that the following11* conditions are met:12*13* - Redistributions of source code must retain the above14* copyright notice, this list of conditions and the following15* disclaimer.16*17* - Redistributions in binary form must reproduce the above18* copyright notice, this list of conditions and the following19* disclaimer in the documentation and/or other materials20* provided with the distribution.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND25* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS26* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN27* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN28* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE29* SOFTWARE.30*/3132#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__3334#include <net/netlink.h>35#include <net/net_namespace.h>36#include <net/sock.h>37#include <rdma/rdma_netlink.h>3839struct ibnl_client {40struct list_head list;41int index;42int nops;43const struct ibnl_client_cbs *cb_table;44};4546static DEFINE_MUTEX(ibnl_mutex);47static struct sock *nls;48static LIST_HEAD(client_list);4950int ibnl_add_client(int index, int nops,51const struct ibnl_client_cbs cb_table[])52{53struct ibnl_client *cur;54struct ibnl_client *nl_client;5556nl_client = kmalloc(sizeof *nl_client, GFP_KERNEL);57if (!nl_client)58return -ENOMEM;5960nl_client->index = index;61nl_client->nops = nops;62nl_client->cb_table = cb_table;6364mutex_lock(&ibnl_mutex);6566list_for_each_entry(cur, &client_list, list) {67if (cur->index == index) {68pr_warn("Client for %d already exists\n", index);69mutex_unlock(&ibnl_mutex);70kfree(nl_client);71return -EINVAL;72}73}7475list_add_tail(&nl_client->list, &client_list);7677mutex_unlock(&ibnl_mutex);7879return 0;80}81EXPORT_SYMBOL(ibnl_add_client);8283int ibnl_remove_client(int index)84{85struct ibnl_client *cur, *next;8687mutex_lock(&ibnl_mutex);88list_for_each_entry_safe(cur, next, &client_list, list) {89if (cur->index == index) {90list_del(&(cur->list));91mutex_unlock(&ibnl_mutex);92kfree(cur);93return 0;94}95}96pr_warn("Can't remove callback for client idx %d. Not found\n", index);97mutex_unlock(&ibnl_mutex);9899return -EINVAL;100}101EXPORT_SYMBOL(ibnl_remove_client);102103void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,104int len, int client, int op)105{106unsigned char *prev_tail;107108prev_tail = skb_tail_pointer(skb);109*nlh = NLMSG_NEW(skb, 0, seq, RDMA_NL_GET_TYPE(client, op),110len, NLM_F_MULTI);111(*nlh)->nlmsg_len = skb_tail_pointer(skb) - prev_tail;112return NLMSG_DATA(*nlh);113114nlmsg_failure:115nlmsg_trim(skb, prev_tail);116return NULL;117}118EXPORT_SYMBOL(ibnl_put_msg);119120int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,121int len, void *data, int type)122{123unsigned char *prev_tail;124125prev_tail = skb_tail_pointer(skb);126NLA_PUT(skb, type, len, data);127nlh->nlmsg_len += skb_tail_pointer(skb) - prev_tail;128return 0;129130nla_put_failure:131nlmsg_trim(skb, prev_tail - nlh->nlmsg_len);132return -EMSGSIZE;133}134EXPORT_SYMBOL(ibnl_put_attr);135136static int ibnl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)137{138struct ibnl_client *client;139int type = nlh->nlmsg_type;140int index = RDMA_NL_GET_CLIENT(type);141int op = RDMA_NL_GET_OP(type);142143list_for_each_entry(client, &client_list, list) {144if (client->index == index) {145if (op < 0 || op >= client->nops ||146!client->cb_table[RDMA_NL_GET_OP(op)].dump)147return -EINVAL;148return netlink_dump_start(nls, skb, nlh,149client->cb_table[op].dump,150NULL);151}152}153154pr_info("Index %d wasn't found in client list\n", index);155return -EINVAL;156}157158static void ibnl_rcv(struct sk_buff *skb)159{160mutex_lock(&ibnl_mutex);161netlink_rcv_skb(skb, &ibnl_rcv_msg);162mutex_unlock(&ibnl_mutex);163}164165int __init ibnl_init(void)166{167nls = netlink_kernel_create(&init_net, NETLINK_RDMA, 0, ibnl_rcv,168NULL, THIS_MODULE);169if (!nls) {170pr_warn("Failed to create netlink socket\n");171return -ENOMEM;172}173174return 0;175}176177void ibnl_cleanup(void)178{179struct ibnl_client *cur, *next;180181mutex_lock(&ibnl_mutex);182list_for_each_entry_safe(cur, next, &client_list, list) {183list_del(&(cur->list));184kfree(cur);185}186mutex_unlock(&ibnl_mutex);187188netlink_kernel_release(nls);189}190191192