12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/fcntl.h>
16 #include <linux/random.h>
17 #include <linux/slab.h>
20 #include <linux/time.h>
33 #include <linux/stddef.h>
35 #include <linux/inet_diag.h>
36 #include <linux/sock_diag.h>
47 #if IS_ENABLED(CONFIG_IPV6)
57 if (!inet_diag_table[proto])
58 request_module(
"net-pf-%d-proto-%d-type-%d-%d",
PF_NETLINK,
62 if (!inet_diag_table[proto])
65 return inet_diag_table[
proto];
68 static inline void inet_diag_unlock_handler(
80 const struct inet_sock *inet = inet_sk(sk);
91 nlh = nlmsg_put(skb, portid, seq, unlh->
nlmsg_type,
sizeof(*r),
104 r->
id.idiag_if = sk->sk_bound_dev_if;
109 r->
id.idiag_src[0] = inet->inet_rcv_saddr;
110 r->
id.idiag_dst[0] = inet->inet_daddr;
119 #if IS_ENABLED(CONFIG_IPV6)
140 .idiag_tmem = sk_wmem_alloc_get(sk),
156 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
166 }
else if (timer_pending(&sk->
sk_timer)) {
182 info = nla_data(attr);
197 return nlmsg_end(skb, nlh);
200 nlmsg_cancel(skb, nlh);
205 static int inet_csk_diag_fill(
struct sock *
sk,
212 skb, req, user_ns, portid, seq, nlmsg_flags, unlh);
224 nlh = nlmsg_put(skb, portid, seq, unlh->
nlmsg_type,
sizeof(*r),
238 r->
id.idiag_if = tw->tw_bound_dev_if;
242 r->
id.idiag_src[0] = tw->tw_rcv_saddr;
243 r->
id.idiag_dst[0] = tw->tw_daddr;
251 #if IS_ENABLED(CONFIG_IPV6)
254 inet6_twsk((
struct sock *)tw);
261 return nlmsg_end(skb, nlh);
264 static int sk_diag_fill(
struct sock *sk,
struct sk_buff *skb,
272 skb, r, portid, seq, nlmsg_flags,
274 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, nlmsg_flags, unlh);
283 struct net *
net = sock_net(in_skb->
sk);
287 sk = inet_lookup(net, hashinfo, req->
id.idiag_dst[0],
288 req->
id.idiag_dport, req->
id.idiag_src[0],
289 req->
id.idiag_sport, req->
id.idiag_if);
291 #if IS_ENABLED(CONFIG_IPV6)
321 err = sk_diag_fill(sk, rep, req,
347 static int inet_diag_get_exact(
struct sk_buff *in_skb,
356 err = PTR_ERR(handler);
358 err = handler->
dump_one(in_skb, nlh, req);
359 inet_diag_unlock_handler(handler);
366 int words = bits >> 5;
371 if (
memcmp(a1, a2, words << 2))
381 mask =
htonl((0xffffffff) << (32 - bits));
383 if ((w1 ^ w2) & mask)
391 static int inet_diag_bc_run(
const struct nlattr *_bc,
394 const void *
bc = nla_data(_bc);
395 int len = nla_len(_bc);
408 yes = entry->
sport >= op[1].
no;
411 yes = entry->
sport <= op[1].
no;
414 yes = entry->
dport >= op[1].
no;
417 yes = entry->
dport <= op[1].
no;
428 if (cond->
port != -1 &&
444 if (addr[0] == 0 && addr[1] == 0 &&
445 addr[2] ==
htonl(0xffff) &&
446 bitstring_match(addr + 3,
457 if (bitstring_match(addr, cond->
addr,
484 entry.
family = sk->sk_family;
485 #if IS_ENABLED(CONFIG_IPV6)
494 entry.
saddr = &inet->inet_rcv_saddr;
495 entry.
daddr = &inet->inet_daddr;
501 return inet_diag_bc_run(bc, &entry);
505 static int valid_cc(
const void *bc,
int len,
int cc)
514 if (op->
yes < 4 || op->
yes & 3)
541 addr_len =
sizeof(
struct in_addr);
549 *min_len += addr_len;
561 static inline bool valid_port_comparison(
const struct inet_diag_bc_op *op,
562 int len,
int *min_len)
571 static int inet_diag_bc_audit(
const void *bytecode,
int bytecode_len)
573 const void *bc = bytecode;
574 int len = bytecode_len;
584 if (!valid_hostcond(bc, len, &min_len))
591 if (!valid_port_comparison(bc, len, &min_len))
603 if (op->
no < min_len || op->
no > len + 4 || op->
no & 3)
606 !valid_cc(bytecode, bytecode_len, len - op->
no))
610 if (op->
yes < min_len || op->
yes > len + 4 || op->
yes & 3)
615 return len == 0 ? 0 : -
EINVAL;
618 static int inet_csk_diag_dump(
struct sock *sk,
627 return inet_csk_diag_fill(sk, skb, r,
642 entry.
family = tw->tw_family;
643 #if IS_ENABLED(CONFIG_IPV6)
646 inet6_twsk((
struct sock *)tw);
652 entry.
saddr = &tw->tw_rcv_saddr;
653 entry.
daddr = &tw->tw_daddr;
659 if (!inet_diag_bc_run(bc, &entry))
663 return inet_twsk_diag_fill(tw, skb, r,
671 static inline void inet_diag_req_addrs(
const struct sock *sk,
677 #if IS_ENABLED(CONFIG_IPV6)
680 entry->
saddr = inet6_rsk(req)->loc_addr.s6_addr32;
681 entry->
daddr = inet6_rsk(req)->rmt_addr.s6_addr32;
683 ipv6_addr_set_v4mapped(ireq->
loc_addr,
684 &entry->saddr_storage);
685 ipv6_addr_set_v4mapped(ireq->
rmt_addr,
686 &entry->daddr_storage);
687 entry->
saddr = entry->saddr_storage.s6_addr32;
688 entry->
daddr = entry->daddr_storage.s6_addr32;
698 static int inet_diag_fill_req(
struct sk_buff *skb,
struct sock *sk,
710 nlh = nlmsg_put(skb, portid, seq, unlh->
nlmsg_type,
sizeof(*r),
721 r->
id.idiag_if = sk->sk_bound_dev_if;
737 #if IS_ENABLED(CONFIG_IPV6)
740 inet_diag_req_addrs(sk, req, &entry);
746 return nlmsg_end(skb, nlh);
749 static int inet_diag_dump_reqs(
struct sk_buff *skb,
struct sock *sk,
759 int reqnum, s_reqnum;
763 s_reqnum = cb->
args[4];
768 entry.
family = sk->sk_family;
773 if (!lopt || !lopt->
qlen)
785 for (req = head;
req; reqnum++, req = req->
dl_next) {
788 if (reqnum < s_reqnum)
795 inet_diag_req_addrs(sk, req, &entry);
798 if (!inet_diag_bc_run(bc, &entry))
802 err = inet_diag_fill_req(skb, sk, req,
805 cb->
nlh->nlmsg_seq, cb->
nlh);
808 cb->
args[4] = reqnum;
827 struct net *
net = sock_net(skb->
sk);
830 s_num = num = cb->
args[2];
832 if (cb->
args[0] == 0) {
842 ilb = &hashinfo->listening_hash[
i];
843 spin_lock_bh(&ilb->
lock);
847 if (!net_eq(sock_net(sk), net))
868 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
869 spin_unlock_bh(&ilb->
lock);
877 if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
878 spin_unlock_bh(&ilb->
lock);
887 spin_unlock_bh(&ilb->
lock);
895 s_i = num = s_num = 0;
901 for (i = s_i; i <= hashinfo->
ehash_mask; i++) {
909 if (hlist_nulls_empty(&head->
chain) &&
910 hlist_nulls_empty(&head->
twchain))
920 if (!net_eq(sock_net(sk), net))
935 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
936 spin_unlock_bh(lock);
948 if (!net_eq(twsk_net(tw), net))
962 if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) {
963 spin_unlock_bh(lock);
970 spin_unlock_bh(lock);
988 if (!IS_ERR(handler))
989 handler->
dump(skb, cb, r, bc);
991 err = PTR_ERR(handler);
992 inet_diag_unlock_handler(handler);
994 return err ? : skb->
len;
1002 if (nlmsg_attrlen(cb->
nlh, hdrlen))
1005 return __inet_diag_dump(skb, cb, nlmsg_data(cb->
nlh), bc);
1008 static inline int inet_diag_type2proto(
int type)
1028 req.sdiag_protocol = inet_diag_type2proto(cb->
nlh->nlmsg_type);
1033 if (nlmsg_attrlen(cb->
nlh, hdrlen))
1036 return __inet_diag_dump(skb, cb, &req, bc);
1039 static int inet_diag_get_exact_compat(
struct sk_buff *in_skb,
1046 req.sdiag_protocol = inet_diag_type2proto(nlh->
nlmsg_type);
1051 return inet_diag_get_exact(in_skb, nlh, &req);
1054 static int inet_diag_rcv_msg_compat(
struct sk_buff *skb,
struct nlmsghdr *nlh)
1057 struct net *
net = sock_net(skb->
sk);
1060 nlmsg_len(nlh) < hdrlen)
1064 if (nlmsg_attrlen(nlh, hdrlen)) {
1067 attr = nlmsg_find_attr(nlh, hdrlen,
1071 inet_diag_bc_audit(nla_data(attr),
nla_len(attr)))
1076 .dump = inet_diag_dump_compat,
1078 return netlink_dump_start(net->
diag_nlsk, skb, nlh, &c);
1082 return inet_diag_get_exact_compat(skb, nlh);
1088 struct net *net = sock_net(skb->
sk);
1090 if (nlmsg_len(h) < hdrlen)
1094 if (nlmsg_attrlen(h, hdrlen)) {
1096 attr = nlmsg_find_attr(h, hdrlen,
1100 inet_diag_bc_audit(nla_data(attr),
nla_len(attr)))
1105 .dump = inet_diag_dump,
1107 return netlink_dump_start(net->
diag_nlsk, skb, h, &c);
1111 return inet_diag_get_exact(skb, h, nlmsg_data(h));
1116 .dump = inet_diag_handler_dump,
1121 .dump = inet_diag_handler_dump,
1134 if (inet_diag_table[type] ==
NULL) {
1135 inet_diag_table[
type] =
h;
1157 static int __init inet_diag_init(
void)
1160 sizeof(
struct inet_diag_handler *));
1163 inet_diag_table = kzalloc(inet_diag_table_size,
GFP_KERNEL);
1164 if (!inet_diag_table)
1182 kfree(inet_diag_table);
1186 static void __exit inet_diag_exit(
void)
1191 kfree(inet_diag_table);