aboutsummaryrefslogtreecommitdiff
path: root/net/core/sockev_nlmcast.c
blob: f238edbfb823e98d1546515ddcd153df83c4c5e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (c) 2014-2015, 2018 The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *
 * Default SOCKEV client implementation
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/export.h>
#include <linux/netlink.h>
#include <linux/sockev.h>
#include <net/sock.h>

static int registration_status;
static struct sock *socknlmsgsk;

static void sockev_skmsg_recv(struct sk_buff *skb)
{
	pr_debug("%s(): Got unsolicited request\n", __func__);
}

static struct netlink_kernel_cfg nlcfg = {
	.input = sockev_skmsg_recv
};

static void _sockev_event(unsigned long event, __u8 *evstr, int buflen)
{
	memset(evstr, 0, buflen);

	switch (event) {
	case SOCKEV_SOCKET:
		strlcpy(evstr, "SOCKEV_SOCKET", buflen);
		break;
	case SOCKEV_BIND:
		strlcpy(evstr, "SOCKEV_BIND", buflen);
		break;
	case SOCKEV_LISTEN:
		strlcpy(evstr, "SOCKEV_LISTEN", buflen);
		break;
	case SOCKEV_ACCEPT:
		strlcpy(evstr, "SOCKEV_ACCEPT", buflen);
		break;
	case SOCKEV_CONNECT:
		strlcpy(evstr, "SOCKEV_CONNECT", buflen);
		break;
	case SOCKEV_SHUTDOWN:
		strlcpy(evstr, "SOCKEV_SHUTDOWN", buflen);
		break;
	default:
		strlcpy(evstr, "UNKNOWN", buflen);
	}
}

static int sockev_client_cb(struct notifier_block *nb,
			    unsigned long event, void *data)
{
	struct sk_buff *skb;
	struct nlmsghdr *nlh;
	struct sknlsockevmsg *smsg;
	struct socket *sock;
	struct sock *sk;

	sock = (struct socket *)data;
	if (!socknlmsgsk || !sock)
		goto done;

	sk = sock->sk;
	if (!sk)
		goto done;

	if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
		goto done;

	if (event != SOCKEV_BIND && event != SOCKEV_LISTEN)
		goto done;

	skb = nlmsg_new(sizeof(struct sknlsockevmsg), GFP_KERNEL);
	if (!skb)
		goto done;

	nlh = nlmsg_put(skb, 0, 0, event, sizeof(struct sknlsockevmsg), 0);
	if (!nlh) {
		kfree_skb(skb);
		goto done;
	}

	NETLINK_CB(skb).dst_group = SKNLGRP_SOCKEV;

	smsg = nlmsg_data(nlh);
	smsg->pid = current->pid;
	_sockev_event(event, smsg->event, sizeof(smsg->event));
	smsg->skfamily = sk->sk_family;
	smsg->skstate = sk->sk_state;
	smsg->skprotocol = sk->sk_protocol;
	smsg->sktype = sk->sk_type;
	smsg->skflags = sk->sk_flags;
	nlmsg_notify(socknlmsgsk, skb, 0, SKNLGRP_SOCKEV, 0, GFP_KERNEL);
done:
	return 0;
}

static struct notifier_block sockev_notifier_client = {
	.notifier_call = sockev_client_cb,
	.next = 0,
	.priority = 0
};

/* ***************** Startup/Shutdown *************************************** */

static int __init sockev_client_init(void)
{
	int rc;

	registration_status = 1;
	rc = sockev_register_notify(&sockev_notifier_client);
	if (rc != 0) {
		registration_status = 0;
		pr_err("%s(): Failed to register cb (%d)\n", __func__, rc);
	}
	socknlmsgsk = netlink_kernel_create(&init_net, NETLINK_SOCKEV, &nlcfg);
	if (!socknlmsgsk) {
		pr_err("%s(): Failed to initialize netlink socket\n", __func__);
		if (registration_status)
			sockev_unregister_notify(&sockev_notifier_client);
		registration_status = 0;
	}

	return rc;
}

static void __exit sockev_client_exit(void)
{
	if (registration_status)
		sockev_unregister_notify(&sockev_notifier_client);
}

module_init(sockev_client_init)
module_exit(sockev_client_exit)
MODULE_LICENSE("GPL v2");