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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/*
* netlink.c - stp relayfs-related transport functions
*
* Copyright (C) IBM Corporation, 2005
* Copyright (C) Redhat Inc, 2005
*
* This file is released under the GPL.
*/
/** @file netlink.c
* @brief Systemtap netlink-related transport functions
*/
/** @addtogroup transport Transport Functions
* @{
*/
#include "netlink.h"
/* the control socket */
extern struct sock *stp_control;
/* queued packets logged from irq context */
static struct sk_buff_head delayed_pkts;
/* for netlink sequence numbers */
static int seq;
/**
* _stp_msg_rcv_skb - dispatch netlink control channel requests
*/
static void _stp_msg_rcv_skb(struct sk_buff *skb,
int (*cmd_handler) (int pid, int cmd, void *data))
{
struct nlmsghdr *nlh = NULL;
int pid, flags;
int nlmsglen, skblen;
void *data;
skblen = skb->len;
if (skblen < sizeof (*nlh))
return;
nlh = (struct nlmsghdr *)skb->data;
nlmsglen = nlh->nlmsg_len;
if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
return;
pid = nlh->nlmsg_pid;
flags = nlh->nlmsg_flags;
if (pid <= 0 || !(flags & NLM_F_REQUEST)) {
netlink_ack(skb, nlh, -EINVAL);
return;
}
if (flags & MSG_TRUNC) {
netlink_ack(skb, nlh, -ECOMM);
return;
}
data = NLMSG_DATA(nlh);
if (cmd_handler(pid, nlh->nlmsg_type, data))
netlink_ack(skb, nlh, -EINVAL);
if (flags & NLM_F_ACK)
netlink_ack(skb, nlh, 0);
}
/**
* _stp_msg_rcv - handle netlink control channel requests
*/
static void _stp_msg_rcv(struct sock *sk, int len)
{
struct sk_buff *skb;
while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
_stp_msg_rcv_skb(skb, sk->sk_user_data);
kfree_skb(skb);
}
}
/**
* _stp_send_delayed_packets - send delayed netlink packets
*/
static void _stp_send_delayed_pkts(unsigned long ignored)
{
struct sk_buff *skb;
while ((skb = skb_dequeue(&delayed_pkts)) != NULL) {
struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
int pid = nlh->nlmsg_pid;
netlink_unicast(stp_control, skb, pid, MSG_DONTWAIT);
}
}
static DECLARE_TASKLET(delayed_pkts_tasklet, _stp_send_delayed_pkts, 0);
/**
* _stp_netlink_send - send data over netlink channel
* @type: message type
* @data: data to send
* @len: length of data
* @pid: pid to send data to
*/
int _stp_netlink_send(int type, void *data, int len, int pid)
{
struct sk_buff *skb;
struct nlmsghdr *nlh;
void *d;
int size;
int err = 0;
size = NLMSG_SPACE(len);
skb = alloc_skb(size, GFP_ATOMIC);
if (!skb)
return -ENOMEM;
nlh = NLMSG_PUT(skb, pid, seq++, type, size - sizeof(*nlh));
nlh->nlmsg_flags = 0;
d = NLMSG_DATA(nlh);
memcpy(d, data, len);
if (in_irq()) {
skb_queue_tail(&delayed_pkts, skb);
tasklet_schedule(&delayed_pkts_tasklet);
} else
err = netlink_unicast(stp_control, skb, pid, MSG_DONTWAIT);
return err;
nlmsg_failure:
if (skb)
kfree_skb(skb);
return -1;
}
/**
* _stp_netlink_open - create netlink socket
* @unit: the netlink 'unit' to create
* @handler: handler function for stp 'commands'
*/
struct sock *_stp_netlink_open(int unit,
int (*handler) (int pid, int cmd, void *data))
{
struct sock *nl = netlink_kernel_create(unit, _stp_msg_rcv);
if (!nl) {
printk("stp-control: couldn't create netlink transport\n");
return NULL;
}
nl->sk_user_data = handler;
return nl;
}
/**
* _stp_netlink_close - close netlink socket
*/
void _stp_netlink_close (struct sock *nl)
{
BUG_ON(!nl);
sock_release(nl->sk_socket);
}
|