2 * udp_diag.c Module for monitoring UDP transport protocols sockets.
4 * Authors: Pavel Emelyanov, <xemul@parallels.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/inet_diag.h>
15 #include <linux/udp.h>
17 #include <net/udplite.h>
18 #include <linux/sock_diag.h>
20 static int sk_diag_dump(struct sock
*sk
, struct sk_buff
*skb
,
21 struct netlink_callback
*cb
, struct inet_diag_req_v2
*req
,
24 if (!inet_diag_bc_sk(bc
, sk
))
27 return inet_sk_diag_fill(sk
, NULL
, skb
, req
,
28 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
29 NETLINK_CB(cb
->skb
).portid
,
30 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
33 static int udp_dump_one(struct udp_table
*tbl
, struct sk_buff
*in_skb
,
34 const struct nlmsghdr
*nlh
, struct inet_diag_req_v2
*req
)
39 struct net
*net
= sock_net(in_skb
->sk
);
41 if (req
->sdiag_family
== AF_INET
)
42 sk
= __udp4_lib_lookup(net
,
43 req
->id
.idiag_src
[0], req
->id
.idiag_sport
,
44 req
->id
.idiag_dst
[0], req
->id
.idiag_dport
,
45 req
->id
.idiag_if
, tbl
);
46 #if IS_ENABLED(CONFIG_IPV6)
47 else if (req
->sdiag_family
== AF_INET6
)
48 sk
= __udp6_lib_lookup(net
,
49 (struct in6_addr
*)req
->id
.idiag_src
,
51 (struct in6_addr
*)req
->id
.idiag_dst
,
53 req
->id
.idiag_if
, tbl
);
62 err
= sock_diag_check_cookie(sk
, req
->id
.idiag_cookie
);
67 rep
= nlmsg_new(sizeof(struct inet_diag_msg
) +
68 sizeof(struct inet_diag_meminfo
) + 64,
73 err
= inet_sk_diag_fill(sk
, NULL
, rep
, req
,
74 sk_user_ns(NETLINK_CB(in_skb
).sk
),
75 NETLINK_CB(in_skb
).portid
,
76 nlh
->nlmsg_seq
, 0, nlh
);
78 WARN_ON(err
== -EMSGSIZE
);
82 err
= netlink_unicast(net
->diag_nlsk
, rep
, NETLINK_CB(in_skb
).portid
,
93 static void udp_dump(struct udp_table
*table
, struct sk_buff
*skb
, struct netlink_callback
*cb
,
94 struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
96 int num
, s_num
, slot
, s_slot
;
97 struct net
*net
= sock_net(skb
->sk
);
100 num
= s_num
= cb
->args
[1];
102 for (slot
= s_slot
; slot
<= table
->mask
; num
= s_num
= 0, slot
++) {
104 struct hlist_nulls_node
*node
;
105 struct udp_hslot
*hslot
= &table
->hash
[slot
];
107 if (hlist_nulls_empty(&hslot
->head
))
110 spin_lock_bh(&hslot
->lock
);
111 sk_nulls_for_each(sk
, node
, &hslot
->head
) {
112 struct inet_sock
*inet
= inet_sk(sk
);
114 if (!net_eq(sock_net(sk
), net
))
118 if (!(r
->idiag_states
& (1 << sk
->sk_state
)))
120 if (r
->sdiag_family
!= AF_UNSPEC
&&
121 sk
->sk_family
!= r
->sdiag_family
)
123 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
126 if (r
->id
.idiag_dport
!= inet
->inet_dport
&&
130 if (sk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
131 spin_unlock_bh(&hslot
->lock
);
137 spin_unlock_bh(&hslot
->lock
);
144 static void udp_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
145 struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
147 udp_dump(&udp_table
, skb
, cb
, r
, bc
);
150 static int udp_diag_dump_one(struct sk_buff
*in_skb
, const struct nlmsghdr
*nlh
,
151 struct inet_diag_req_v2
*req
)
153 return udp_dump_one(&udp_table
, in_skb
, nlh
, req
);
156 static void udp_diag_get_info(struct sock
*sk
, struct inet_diag_msg
*r
,
159 r
->idiag_rqueue
= sk_rmem_alloc_get(sk
);
160 r
->idiag_wqueue
= sk_wmem_alloc_get(sk
);
163 static const struct inet_diag_handler udp_diag_handler
= {
164 .dump
= udp_diag_dump
,
165 .dump_one
= udp_diag_dump_one
,
166 .idiag_get_info
= udp_diag_get_info
,
167 .idiag_type
= IPPROTO_UDP
,
170 static void udplite_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
171 struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
173 udp_dump(&udplite_table
, skb
, cb
, r
, bc
);
176 static int udplite_diag_dump_one(struct sk_buff
*in_skb
, const struct nlmsghdr
*nlh
,
177 struct inet_diag_req_v2
*req
)
179 return udp_dump_one(&udplite_table
, in_skb
, nlh
, req
);
182 static const struct inet_diag_handler udplite_diag_handler
= {
183 .dump
= udplite_diag_dump
,
184 .dump_one
= udplite_diag_dump_one
,
185 .idiag_get_info
= udp_diag_get_info
,
186 .idiag_type
= IPPROTO_UDPLITE
,
189 static int __init
udp_diag_init(void)
193 err
= inet_diag_register(&udp_diag_handler
);
196 err
= inet_diag_register(&udplite_diag_handler
);
202 inet_diag_unregister(&udp_diag_handler
);
206 static void __exit
udp_diag_exit(void)
208 inet_diag_unregister(&udplite_diag_handler
);
209 inet_diag_unregister(&udp_diag_handler
);
212 module_init(udp_diag_init
);
213 module_exit(udp_diag_exit
);
214 MODULE_LICENSE("GPL");
215 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2-17 /* AF_INET - IPPROTO_UDP */);
216 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2-136 /* AF_INET - IPPROTO_UDPLITE */);