1 #include <linux/mutex.h>
2 #include <linux/socket.h>
3 #include <linux/skbuff.h>
4 #include <net/netlink.h>
5 #include <net/net_namespace.h>
6 #include <linux/module.h>
8 #include <linux/inet_diag.h>
9 #include <linux/sock_diag.h>
11 static struct sock_diag_handler *sock_diag_handlers[AF_MAX];
12 static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
13 static DEFINE_MUTEX(sock_diag_table_mutex);
15 void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
17 mutex_lock(&sock_diag_table_mutex);
19 mutex_unlock(&sock_diag_table_mutex);
21 EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
23 void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
25 mutex_lock(&sock_diag_table_mutex);
26 inet_rcv_compat = NULL;
27 mutex_unlock(&sock_diag_table_mutex);
29 EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
31 int sock_diag_register(struct sock_diag_handler *hndl)
35 if (hndl->family >= AF_MAX)
38 mutex_lock(&sock_diag_table_mutex);
39 if (sock_diag_handlers[hndl->family])
42 sock_diag_handlers[hndl->family] = hndl;
43 mutex_unlock(&sock_diag_table_mutex);
47 EXPORT_SYMBOL_GPL(sock_diag_register);
49 void sock_diag_unregister(struct sock_diag_handler *hnld)
51 int family = hnld->family;
56 mutex_lock(&sock_diag_table_mutex);
57 BUG_ON(sock_diag_handlers[family] != hnld);
58 sock_diag_handlers[family] = NULL;
59 mutex_unlock(&sock_diag_table_mutex);
61 EXPORT_SYMBOL_GPL(sock_diag_unregister);
63 static inline struct sock_diag_handler *sock_diag_lock_handler(int family)
65 if (sock_diag_handlers[family] == NULL)
66 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
67 NETLINK_SOCK_DIAG, IPPROTO_IP);
69 mutex_lock(&sock_diag_table_mutex);
70 return sock_diag_handlers[family];
73 static inline void sock_diag_unlock_handler(struct sock_diag_handler *h)
75 mutex_unlock(&sock_diag_table_mutex);
78 static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
81 struct sock_diag_req *req = NLMSG_DATA(nlh);
82 struct sock_diag_handler *hndl;
84 if (nlmsg_len(nlh) < sizeof(*req))
87 hndl = sock_diag_lock_handler(req->sdiag_family);
91 err = hndl->dump(skb, nlh);
92 sock_diag_unlock_handler(hndl);
97 static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
101 switch (nlh->nlmsg_type) {
102 case TCPDIAG_GETSOCK:
103 case DCCPDIAG_GETSOCK:
104 if (inet_rcv_compat == NULL)
105 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
106 NETLINK_SOCK_DIAG, IPPROTO_IP);
108 mutex_lock(&sock_diag_table_mutex);
109 if (inet_rcv_compat != NULL)
110 ret = inet_rcv_compat(skb, nlh);
113 mutex_unlock(&sock_diag_table_mutex);
116 case SOCK_DIAG_BY_FAMILY:
117 return __sock_diag_rcv_msg(skb, nlh);
123 static DEFINE_MUTEX(sock_diag_mutex);
125 static void sock_diag_rcv(struct sk_buff *skb)
127 mutex_lock(&sock_diag_mutex);
128 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
129 mutex_unlock(&sock_diag_mutex);
132 struct sock *sock_diag_nlsk;
133 EXPORT_SYMBOL_GPL(sock_diag_nlsk);
135 static int __init sock_diag_init(void)
137 sock_diag_nlsk = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0,
138 sock_diag_rcv, NULL, THIS_MODULE);
139 return sock_diag_nlsk == NULL ? -ENOMEM : 0;
142 static void __exit sock_diag_exit(void)
144 netlink_kernel_release(sock_diag_nlsk);
147 module_init(sock_diag_init);
148 module_exit(sock_diag_exit);
149 MODULE_LICENSE("GPL");
150 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);