Pavel Emelyanov | 52b7c59 | 2011-12-09 06:23:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * udp_diag.c Module for monitoring UDP transport protocols sockets. |
| 3 | * |
| 4 | * Authors: Pavel Emelyanov, <xemul@parallels.com> |
| 5 | * |
| 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. |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/inet_diag.h> |
| 15 | #include <linux/udp.h> |
| 16 | #include <net/udp.h> |
| 17 | #include <net/udplite.h> |
| 18 | #include <linux/inet_diag.h> |
| 19 | #include <linux/sock_diag.h> |
| 20 | |
| 21 | static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb, |
| 22 | const struct nlmsghdr *nlh, struct inet_diag_req *req) |
| 23 | { |
Pavel Emelyanov | a925aa0 | 2011-12-09 06:24:06 +0000 | [diff] [blame^] | 24 | int err = -EINVAL; |
| 25 | struct sock *sk; |
| 26 | struct sk_buff *rep; |
| 27 | |
| 28 | if (req->sdiag_family == AF_INET) |
| 29 | sk = __udp4_lib_lookup(&init_net, |
| 30 | req->id.idiag_src[0], req->id.idiag_sport, |
| 31 | req->id.idiag_dst[0], req->id.idiag_dport, |
| 32 | req->id.idiag_if, tbl); |
| 33 | else if (req->sdiag_family == AF_INET6) |
| 34 | sk = __udp6_lib_lookup(&init_net, |
| 35 | (struct in6_addr *)req->id.idiag_src, |
| 36 | req->id.idiag_sport, |
| 37 | (struct in6_addr *)req->id.idiag_dst, |
| 38 | req->id.idiag_dport, |
| 39 | req->id.idiag_if, tbl); |
| 40 | else |
| 41 | goto out_nosk; |
| 42 | |
| 43 | err = -ENOENT; |
| 44 | if (sk == NULL) |
| 45 | goto out_nosk; |
| 46 | |
| 47 | err = inet_diag_check_cookie(sk, req); |
| 48 | if (err) |
| 49 | goto out; |
| 50 | |
| 51 | err = -ENOMEM; |
| 52 | rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) + |
| 53 | sizeof(struct inet_diag_meminfo) + |
| 54 | 64)), GFP_KERNEL); |
| 55 | if (!rep) |
| 56 | goto out; |
| 57 | |
| 58 | err = inet_sk_diag_fill(sk, NULL, rep, req, |
| 59 | NETLINK_CB(in_skb).pid, |
| 60 | nlh->nlmsg_seq, 0, nlh); |
| 61 | if (err < 0) { |
| 62 | WARN_ON(err == -EMSGSIZE); |
| 63 | kfree_skb(rep); |
| 64 | goto out; |
| 65 | } |
| 66 | err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid, |
| 67 | MSG_DONTWAIT); |
| 68 | if (err > 0) |
| 69 | err = 0; |
| 70 | out: |
| 71 | if (sk) |
| 72 | sock_put(sk); |
| 73 | out_nosk: |
| 74 | return err; |
Pavel Emelyanov | 52b7c59 | 2011-12-09 06:23:51 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void udp_dump(struct udp_table *table, struct sk_buff *skb, struct netlink_callback *cb, |
| 78 | struct inet_diag_req *r, struct nlattr *bc) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 83 | struct inet_diag_req *r, struct nlattr *bc) |
| 84 | { |
| 85 | udp_dump(&udp_table, skb, cb, r, bc); |
| 86 | } |
| 87 | |
| 88 | static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh, |
| 89 | struct inet_diag_req *req) |
| 90 | { |
| 91 | return udp_dump_one(&udp_table, in_skb, nlh, req); |
| 92 | } |
| 93 | |
| 94 | static const struct inet_diag_handler udp_diag_handler = { |
| 95 | .dump = udp_diag_dump, |
| 96 | .dump_one = udp_diag_dump_one, |
| 97 | .idiag_type = IPPROTO_UDP, |
| 98 | }; |
| 99 | |
| 100 | static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 101 | struct inet_diag_req *r, struct nlattr *bc) |
| 102 | { |
| 103 | udp_dump(&udplite_table, skb, cb, r, bc); |
| 104 | } |
| 105 | |
| 106 | static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh, |
| 107 | struct inet_diag_req *req) |
| 108 | { |
| 109 | return udp_dump_one(&udplite_table, in_skb, nlh, req); |
| 110 | } |
| 111 | |
| 112 | static const struct inet_diag_handler udplite_diag_handler = { |
| 113 | .dump = udplite_diag_dump, |
| 114 | .dump_one = udplite_diag_dump_one, |
| 115 | .idiag_type = IPPROTO_UDPLITE, |
| 116 | }; |
| 117 | |
| 118 | static int __init udp_diag_init(void) |
| 119 | { |
| 120 | int err; |
| 121 | |
| 122 | err = inet_diag_register(&udp_diag_handler); |
| 123 | if (err) |
| 124 | goto out; |
| 125 | err = inet_diag_register(&udplite_diag_handler); |
| 126 | if (err) |
| 127 | goto out_lite; |
| 128 | out: |
| 129 | return err; |
| 130 | out_lite: |
| 131 | inet_diag_unregister(&udp_diag_handler); |
| 132 | goto out; |
| 133 | } |
| 134 | |
| 135 | static void __exit udp_diag_exit(void) |
| 136 | { |
| 137 | inet_diag_unregister(&udplite_diag_handler); |
| 138 | inet_diag_unregister(&udp_diag_handler); |
| 139 | } |
| 140 | |
| 141 | module_init(udp_diag_init); |
| 142 | module_exit(udp_diag_exit); |
| 143 | MODULE_LICENSE("GPL"); |
| 144 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 17); |
| 145 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 136); |