Blame view
net/ipv4/raw_diag.c
6.28 KB
09c434b8a treewide: Add SPD... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
432490f9d net: ip, diag -- ... |
2 3 4 5 |
#include <linux/module.h> #include <linux/inet_diag.h> #include <linux/sock_diag.h> |
f8da97798 net: ip, diag: in... |
6 |
#include <net/inet_sock.h> |
432490f9d net: ip, diag -- ... |
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <net/raw.h> #include <net/rawv6.h> #ifdef pr_fmt # undef pr_fmt #endif #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt static struct raw_hashinfo * raw_get_hashinfo(const struct inet_diag_req_v2 *r) { if (r->sdiag_family == AF_INET) { return &raw_v4_hashinfo; #if IS_ENABLED(CONFIG_IPV6) } else if (r->sdiag_family == AF_INET6) { return &raw_v6_hashinfo; #endif } else { |
432490f9d net: ip, diag -- ... |
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
return ERR_PTR(-EINVAL); } } /* * Due to requirement of not breaking user API we can't simply * rename @pad field in inet_diag_req_v2 structure, instead * use helper to figure it out. */ static struct sock *raw_lookup(struct net *net, struct sock *from, const struct inet_diag_req_v2 *req) { struct inet_diag_req_raw *r = (void *)req; struct sock *sk = NULL; if (r->sdiag_family == AF_INET) sk = __raw_v4_lookup(net, from, r->sdiag_raw_protocol, r->id.idiag_dst[0], r->id.idiag_src[0], |
67359930e net: ipv4: add se... |
46 |
r->id.idiag_if, 0); |
432490f9d net: ip, diag -- ... |
47 48 49 50 51 |
#if IS_ENABLED(CONFIG_IPV6) else sk = __raw_v6_lookup(net, from, r->sdiag_raw_protocol, (const struct in6_addr *)r->id.idiag_src, (const struct in6_addr *)r->id.idiag_dst, |
5108ab4bf net: ipv6: add se... |
52 |
r->id.idiag_if, 0); |
432490f9d net: ip, diag -- ... |
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 |
#endif return sk; } static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2 *r) { struct raw_hashinfo *hashinfo = raw_get_hashinfo(r); struct sock *sk = NULL, *s; int slot; if (IS_ERR(hashinfo)) return ERR_CAST(hashinfo); read_lock(&hashinfo->lock); for (slot = 0; slot < RAW_HTABLE_SIZE; slot++) { sk_for_each(s, &hashinfo->ht[slot]) { sk = raw_lookup(net, s, r); if (sk) { /* * Grab it and keep until we fill * diag meaage to be reported, so * caller should call sock_put then. * We can do that because we're keeping * hashinfo->lock here. */ sock_hold(sk); |
9999370fa net: ip, raw_diag... |
79 |
goto out_unlock; |
432490f9d net: ip, diag -- ... |
80 81 82 |
} } } |
9999370fa net: ip, raw_diag... |
83 |
out_unlock: |
432490f9d net: ip, diag -- ... |
84 85 86 87 |
read_unlock(&hashinfo->lock); return sk ? sk : ERR_PTR(-ENOENT); } |
5682d393b inet_diag: Refact... |
88 |
static int raw_diag_dump_one(struct netlink_callback *cb, |
432490f9d net: ip, diag -- ... |
89 90 |
const struct inet_diag_req_v2 *r) { |
5682d393b inet_diag: Refact... |
91 |
struct sk_buff *in_skb = cb->skb; |
432490f9d net: ip, diag -- ... |
92 93 |
struct sk_buff *rep; struct sock *sk; |
5682d393b inet_diag: Refact... |
94 |
struct net *net; |
432490f9d net: ip, diag -- ... |
95 |
int err; |
5682d393b inet_diag: Refact... |
96 |
net = sock_net(in_skb->sk); |
432490f9d net: ip, diag -- ... |
97 98 99 |
sk = raw_sock_get(net, r); if (IS_ERR(sk)) return PTR_ERR(sk); |
83f73c5bb inet_diag: return... |
100 101 102 |
rep = nlmsg_new(nla_total_size(sizeof(struct inet_diag_msg)) + inet_diag_msg_attrs_size() + nla_total_size(sizeof(struct inet_diag_meminfo)) + 64, |
432490f9d net: ip, diag -- ... |
103 104 105 106 107 |
GFP_KERNEL); if (!rep) { sock_put(sk); return -ENOMEM; } |
5682d393b inet_diag: Refact... |
108 |
err = inet_sk_diag_fill(sk, NULL, rep, cb, r, 0, |
432490f9d net: ip, diag -- ... |
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
netlink_net_capable(in_skb, CAP_NET_ADMIN)); sock_put(sk); if (err < 0) { kfree_skb(rep); return err; } err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid, MSG_DONTWAIT); if (err > 0) err = 0; return err; } static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, struct netlink_callback *cb, const struct inet_diag_req_v2 *r, struct nlattr *bc, bool net_admin) { if (!inet_diag_bc_sk(bc, sk)) return 0; |
5682d393b inet_diag: Refact... |
132 |
return inet_sk_diag_fill(sk, NULL, skb, cb, r, NLM_F_MULTI, net_admin); |
432490f9d net: ip, diag -- ... |
133 134 135 |
} static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, |
0df6d3284 inet_diag: Move t... |
136 |
const struct inet_diag_req_v2 *r) |
432490f9d net: ip, diag -- ... |
137 138 139 140 |
{ bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); struct raw_hashinfo *hashinfo = raw_get_hashinfo(r); struct net *net = sock_net(skb->sk); |
0df6d3284 inet_diag: Move t... |
141 |
struct inet_diag_dump_data *cb_data; |
432490f9d net: ip, diag -- ... |
142 143 |
int num, s_num, slot, s_slot; struct sock *sk = NULL; |
0df6d3284 inet_diag: Move t... |
144 |
struct nlattr *bc; |
432490f9d net: ip, diag -- ... |
145 146 147 |
if (IS_ERR(hashinfo)) return; |
0df6d3284 inet_diag: Move t... |
148 149 |
cb_data = cb->data; bc = cb_data->inet_diag_nla_bc; |
432490f9d net: ip, diag -- ... |
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
s_slot = cb->args[0]; num = s_num = cb->args[1]; read_lock(&hashinfo->lock); for (slot = s_slot; slot < RAW_HTABLE_SIZE; s_num = 0, slot++) { num = 0; sk_for_each(sk, &hashinfo->ht[slot]) { struct inet_sock *inet = inet_sk(sk); if (!net_eq(sock_net(sk), net)) continue; if (num < s_num) goto next; if (sk->sk_family != r->sdiag_family) goto next; if (r->id.idiag_sport != inet->inet_sport && r->id.idiag_sport) goto next; if (r->id.idiag_dport != inet->inet_dport && r->id.idiag_dport) goto next; if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0) goto out_unlock; next: num++; } } out_unlock: read_unlock(&hashinfo->lock); cb->args[0] = slot; cb->args[1] = num; } static void raw_diag_get_info(struct sock *sk, struct inet_diag_msg *r, void *info) { r->idiag_rqueue = sk_rmem_alloc_get(sk); r->idiag_wqueue = sk_wmem_alloc_get(sk); } #ifdef CONFIG_INET_DIAG_DESTROY static int raw_diag_destroy(struct sk_buff *in_skb, const struct inet_diag_req_v2 *r) { struct net *net = sock_net(in_skb->sk); struct sock *sk; |
cd05a0eca net: ip, raw_diag... |
199 |
int err; |
432490f9d net: ip, diag -- ... |
200 201 202 203 |
sk = raw_sock_get(net, r); if (IS_ERR(sk)) return PTR_ERR(sk); |
cd05a0eca net: ip, raw_diag... |
204 205 206 |
err = sock_diag_destroy(sk, ECONNABORTED); sock_put(sk); return err; |
432490f9d net: ip, diag -- ... |
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
} #endif static const struct inet_diag_handler raw_diag_handler = { .dump = raw_diag_dump, .dump_one = raw_diag_dump_one, .idiag_get_info = raw_diag_get_info, .idiag_type = IPPROTO_RAW, .idiag_info_size = 0, #ifdef CONFIG_INET_DIAG_DESTROY .destroy = raw_diag_destroy, #endif }; static void __always_unused __check_inet_diag_req_raw(void) { /* * Make sure the two structures are identical, * except the @pad field. */ #define __offset_mismatch(m1, m2) \ (offsetof(struct inet_diag_req_v2, m1) != \ offsetof(struct inet_diag_req_raw, m2)) BUILD_BUG_ON(sizeof(struct inet_diag_req_v2) != sizeof(struct inet_diag_req_raw)); BUILD_BUG_ON(__offset_mismatch(sdiag_family, sdiag_family)); BUILD_BUG_ON(__offset_mismatch(sdiag_protocol, sdiag_protocol)); BUILD_BUG_ON(__offset_mismatch(idiag_ext, idiag_ext)); BUILD_BUG_ON(__offset_mismatch(pad, sdiag_raw_protocol)); BUILD_BUG_ON(__offset_mismatch(idiag_states, idiag_states)); BUILD_BUG_ON(__offset_mismatch(id, id)); #undef __offset_mismatch } static int __init raw_diag_init(void) { return inet_diag_register(&raw_diag_handler); } static void __exit raw_diag_exit(void) { inet_diag_unregister(&raw_diag_handler); } module_init(raw_diag_init); module_exit(raw_diag_exit); MODULE_LICENSE("GPL"); MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-255 /* AF_INET - IPPROTO_RAW */); MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10-255 /* AF_INET6 - IPPROTO_RAW */); |