Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 3 | * operating system. INET is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * "Ping" sockets |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * Based on ipv4/udp.c code. |
| 14 | * |
| 15 | * Authors: Vasiliy Kulikov / Openwall (for Linux 2.6), |
| 16 | * Pavel Kankovsky (for Linux 2.4.32) |
| 17 | * |
| 18 | * Pavel gave all rights to bugs to Vasiliy, |
| 19 | * none of the bugs are Pavel's now. |
| 20 | * |
| 21 | */ |
| 22 | |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 24 | #include <linux/types.h> |
| 25 | #include <linux/fcntl.h> |
| 26 | #include <linux/socket.h> |
| 27 | #include <linux/sockios.h> |
| 28 | #include <linux/in.h> |
| 29 | #include <linux/errno.h> |
| 30 | #include <linux/timer.h> |
| 31 | #include <linux/mm.h> |
| 32 | #include <linux/inet.h> |
| 33 | #include <linux/netdevice.h> |
| 34 | #include <net/snmp.h> |
| 35 | #include <net/ip.h> |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 36 | #include <net/icmp.h> |
| 37 | #include <net/protocol.h> |
| 38 | #include <linux/skbuff.h> |
| 39 | #include <linux/proc_fs.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 40 | #include <linux/export.h> |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 41 | #include <net/sock.h> |
| 42 | #include <net/ping.h> |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 43 | #include <net/udp.h> |
| 44 | #include <net/route.h> |
| 45 | #include <net/inet_common.h> |
| 46 | #include <net/checksum.h> |
| 47 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 48 | #if IS_ENABLED(CONFIG_IPV6) |
| 49 | #include <linux/in6.h> |
| 50 | #include <linux/icmpv6.h> |
| 51 | #include <net/addrconf.h> |
| 52 | #include <net/ipv6.h> |
| 53 | #include <net/transp_v6.h> |
| 54 | #endif |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 55 | |
Stephen Hemminger | ea074b3 | 2013-12-28 11:11:42 -0800 | [diff] [blame] | 56 | struct ping_table { |
| 57 | struct hlist_nulls_head hash[PING_HTABLE_SIZE]; |
| 58 | rwlock_t lock; |
| 59 | }; |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 60 | |
Stephen Hemminger | ea074b3 | 2013-12-28 11:11:42 -0800 | [diff] [blame] | 61 | static struct ping_table ping_table; |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 62 | struct pingv6_ops pingv6_ops; |
| 63 | EXPORT_SYMBOL_GPL(pingv6_ops); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 64 | |
Eric Dumazet | 1b1cb1f | 2011-05-13 22:59:19 +0000 | [diff] [blame] | 65 | static u16 ping_port_rover; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 66 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 67 | static inline int ping_hashfn(struct net *net, unsigned int num, unsigned int mask) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 68 | { |
| 69 | int res = (num + net_hash_mix(net)) & mask; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 70 | |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 71 | pr_debug("hash(%d) = %d\n", num, res); |
| 72 | return res; |
| 73 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 74 | EXPORT_SYMBOL_GPL(ping_hash); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 75 | |
| 76 | static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 77 | struct net *net, unsigned int num) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 78 | { |
| 79 | return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)]; |
| 80 | } |
| 81 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 82 | int ping_get_port(struct sock *sk, unsigned short ident) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 83 | { |
| 84 | struct hlist_nulls_node *node; |
| 85 | struct hlist_nulls_head *hlist; |
| 86 | struct inet_sock *isk, *isk2; |
| 87 | struct sock *sk2 = NULL; |
| 88 | |
| 89 | isk = inet_sk(sk); |
| 90 | write_lock_bh(&ping_table.lock); |
| 91 | if (ident == 0) { |
| 92 | u32 i; |
| 93 | u16 result = ping_port_rover + 1; |
| 94 | |
| 95 | for (i = 0; i < (1L << 16); i++, result++) { |
| 96 | if (!result) |
| 97 | result++; /* avoid zero */ |
| 98 | hlist = ping_hashslot(&ping_table, sock_net(sk), |
| 99 | result); |
| 100 | ping_portaddr_for_each_entry(sk2, node, hlist) { |
| 101 | isk2 = inet_sk(sk2); |
| 102 | |
| 103 | if (isk2->inet_num == result) |
| 104 | goto next_port; |
| 105 | } |
| 106 | |
| 107 | /* found */ |
| 108 | ping_port_rover = ident = result; |
| 109 | break; |
| 110 | next_port: |
| 111 | ; |
| 112 | } |
| 113 | if (i >= (1L << 16)) |
| 114 | goto fail; |
| 115 | } else { |
| 116 | hlist = ping_hashslot(&ping_table, sock_net(sk), ident); |
| 117 | ping_portaddr_for_each_entry(sk2, node, hlist) { |
| 118 | isk2 = inet_sk(sk2); |
| 119 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 120 | /* BUG? Why is this reuse and not reuseaddr? ping.c |
| 121 | * doesn't turn off SO_REUSEADDR, and it doesn't expect |
| 122 | * that other ping processes can steal its packets. |
| 123 | */ |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 124 | if ((isk2->inet_num == ident) && |
| 125 | (sk2 != sk) && |
| 126 | (!sk2->sk_reuse || !sk->sk_reuse)) |
| 127 | goto fail; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | pr_debug("found port/ident = %d\n", ident); |
| 132 | isk->inet_num = ident; |
| 133 | if (sk_unhashed(sk)) { |
| 134 | pr_debug("was not hashed\n"); |
| 135 | sock_hold(sk); |
| 136 | hlist_nulls_add_head(&sk->sk_nulls_node, hlist); |
| 137 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
| 138 | } |
| 139 | write_unlock_bh(&ping_table.lock); |
| 140 | return 0; |
| 141 | |
| 142 | fail: |
| 143 | write_unlock_bh(&ping_table.lock); |
| 144 | return 1; |
| 145 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 146 | EXPORT_SYMBOL_GPL(ping_get_port); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 147 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 148 | void ping_hash(struct sock *sk) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 149 | { |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 150 | pr_debug("ping_hash(sk->port=%u)\n", inet_sk(sk)->inet_num); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 151 | BUG(); /* "Please do not press this button again." */ |
| 152 | } |
| 153 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 154 | void ping_unhash(struct sock *sk) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 155 | { |
| 156 | struct inet_sock *isk = inet_sk(sk); |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 157 | pr_debug("ping_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 158 | if (sk_hashed(sk)) { |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 159 | write_lock_bh(&ping_table.lock); |
| 160 | hlist_nulls_del(&sk->sk_nulls_node); |
| 161 | sock_put(sk); |
Eric Dumazet | 747465e | 2012-01-16 19:27:39 +0000 | [diff] [blame] | 162 | isk->inet_num = 0; |
| 163 | isk->inet_sport = 0; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 164 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
| 165 | write_unlock_bh(&ping_table.lock); |
| 166 | } |
| 167 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 168 | EXPORT_SYMBOL_GPL(ping_unhash); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 169 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 170 | static struct sock *ping_lookup(struct net *net, struct sk_buff *skb, u16 ident) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 171 | { |
| 172 | struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident); |
| 173 | struct sock *sk = NULL; |
| 174 | struct inet_sock *isk; |
| 175 | struct hlist_nulls_node *hnode; |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 176 | int dif = skb->dev->ifindex; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 177 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 178 | if (skb->protocol == htons(ETH_P_IP)) { |
| 179 | pr_debug("try to find: num = %d, daddr = %pI4, dif = %d\n", |
| 180 | (int)ident, &ip_hdr(skb)->daddr, dif); |
| 181 | #if IS_ENABLED(CONFIG_IPV6) |
| 182 | } else if (skb->protocol == htons(ETH_P_IPV6)) { |
| 183 | pr_debug("try to find: num = %d, daddr = %pI6c, dif = %d\n", |
| 184 | (int)ident, &ipv6_hdr(skb)->daddr, dif); |
| 185 | #endif |
| 186 | } |
| 187 | |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 188 | read_lock_bh(&ping_table.lock); |
| 189 | |
| 190 | ping_portaddr_for_each_entry(sk, hnode, hslot) { |
| 191 | isk = inet_sk(sk); |
| 192 | |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 193 | pr_debug("iterate\n"); |
| 194 | if (isk->inet_num != ident) |
| 195 | continue; |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 196 | |
| 197 | if (skb->protocol == htons(ETH_P_IP) && |
| 198 | sk->sk_family == AF_INET) { |
| 199 | pr_debug("found: %p: num=%d, daddr=%pI4, dif=%d\n", sk, |
| 200 | (int) isk->inet_num, &isk->inet_rcv_saddr, |
| 201 | sk->sk_bound_dev_if); |
| 202 | |
| 203 | if (isk->inet_rcv_saddr && |
| 204 | isk->inet_rcv_saddr != ip_hdr(skb)->daddr) |
| 205 | continue; |
| 206 | #if IS_ENABLED(CONFIG_IPV6) |
| 207 | } else if (skb->protocol == htons(ETH_P_IPV6) && |
| 208 | sk->sk_family == AF_INET6) { |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 209 | |
| 210 | pr_debug("found: %p: num=%d, daddr=%pI6c, dif=%d\n", sk, |
| 211 | (int) isk->inet_num, |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 212 | &sk->sk_v6_rcv_saddr, |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 213 | sk->sk_bound_dev_if); |
| 214 | |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 215 | if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) && |
| 216 | !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 217 | &ipv6_hdr(skb)->daddr)) |
| 218 | continue; |
| 219 | #endif |
| 220 | } |
| 221 | |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 222 | if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) |
| 223 | continue; |
| 224 | |
| 225 | sock_hold(sk); |
| 226 | goto exit; |
| 227 | } |
| 228 | |
| 229 | sk = NULL; |
| 230 | exit: |
| 231 | read_unlock_bh(&ping_table.lock); |
| 232 | |
| 233 | return sk; |
| 234 | } |
| 235 | |
Eric W. Biederman | 7064d16 | 2012-05-24 10:34:21 -0600 | [diff] [blame] | 236 | static void inet_get_ping_group_range_net(struct net *net, kgid_t *low, |
| 237 | kgid_t *high) |
Vasiliy Kulikov | f56e03e | 2011-05-17 00:16:56 +0000 | [diff] [blame] | 238 | { |
Eric W. Biederman | 7064d16 | 2012-05-24 10:34:21 -0600 | [diff] [blame] | 239 | kgid_t *data = net->ipv4.sysctl_ping_group_range; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 240 | unsigned int seq; |
| 241 | |
Vasiliy Kulikov | f56e03e | 2011-05-17 00:16:56 +0000 | [diff] [blame] | 242 | do { |
Eric W. Biederman | 0bbf87d | 2013-09-28 14:10:59 -0700 | [diff] [blame] | 243 | seq = read_seqbegin(&net->ipv4.sysctl_local_ports.lock); |
Vasiliy Kulikov | f56e03e | 2011-05-17 00:16:56 +0000 | [diff] [blame] | 244 | |
| 245 | *low = data[0]; |
| 246 | *high = data[1]; |
Eric W. Biederman | 0bbf87d | 2013-09-28 14:10:59 -0700 | [diff] [blame] | 247 | } while (read_seqretry(&net->ipv4.sysctl_local_ports.lock, seq)); |
Vasiliy Kulikov | f56e03e | 2011-05-17 00:16:56 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 251 | int ping_init_sock(struct sock *sk) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 252 | { |
| 253 | struct net *net = sock_net(sk); |
Eric W. Biederman | 7064d16 | 2012-05-24 10:34:21 -0600 | [diff] [blame] | 254 | kgid_t group = current_egid(); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 255 | struct group_info *group_info = get_current_groups(); |
| 256 | int i, j, count = group_info->ngroups; |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 257 | kgid_t low, high; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 258 | |
Eric W. Biederman | 7064d16 | 2012-05-24 10:34:21 -0600 | [diff] [blame] | 259 | inet_get_ping_group_range_net(net, &low, &high); |
| 260 | if (gid_lte(low, group) && gid_lte(group, high)) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 261 | return 0; |
| 262 | |
| 263 | for (i = 0; i < group_info->nblocks; i++) { |
| 264 | int cp_count = min_t(int, NGROUPS_PER_BLOCK, count); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 265 | for (j = 0; j < cp_count; j++) { |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 266 | kgid_t gid = group_info->blocks[i][j]; |
| 267 | if (gid_lte(low, gid) && gid_lte(gid, high)) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | count -= cp_count; |
| 272 | } |
| 273 | |
| 274 | return -EACCES; |
| 275 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 276 | EXPORT_SYMBOL_GPL(ping_init_sock); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 277 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 278 | void ping_close(struct sock *sk, long timeout) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 279 | { |
| 280 | pr_debug("ping_close(sk=%p,sk->num=%u)\n", |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 281 | inet_sk(sk), inet_sk(sk)->inet_num); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 282 | pr_debug("isk->refcnt = %d\n", sk->sk_refcnt.counter); |
| 283 | |
| 284 | sk_common_release(sk); |
| 285 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 286 | EXPORT_SYMBOL_GPL(ping_close); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 287 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 288 | /* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */ |
Wu Fengguang | a06a2d3 | 2013-06-12 21:04:16 +0800 | [diff] [blame] | 289 | static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk, |
| 290 | struct sockaddr *uaddr, int addr_len) { |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 291 | struct net *net = sock_net(sk); |
| 292 | if (sk->sk_family == AF_INET) { |
| 293 | struct sockaddr_in *addr = (struct sockaddr_in *) uaddr; |
| 294 | int chk_addr_ret; |
| 295 | |
| 296 | if (addr_len < sizeof(*addr)) |
| 297 | return -EINVAL; |
| 298 | |
| 299 | pr_debug("ping_check_bind_addr(sk=%p,addr=%pI4,port=%d)\n", |
| 300 | sk, &addr->sin_addr.s_addr, ntohs(addr->sin_port)); |
| 301 | |
| 302 | chk_addr_ret = inet_addr_type(net, addr->sin_addr.s_addr); |
| 303 | |
| 304 | if (addr->sin_addr.s_addr == htonl(INADDR_ANY)) |
| 305 | chk_addr_ret = RTN_LOCAL; |
| 306 | |
| 307 | if ((sysctl_ip_nonlocal_bind == 0 && |
| 308 | isk->freebind == 0 && isk->transparent == 0 && |
| 309 | chk_addr_ret != RTN_LOCAL) || |
| 310 | chk_addr_ret == RTN_MULTICAST || |
| 311 | chk_addr_ret == RTN_BROADCAST) |
| 312 | return -EADDRNOTAVAIL; |
| 313 | |
| 314 | #if IS_ENABLED(CONFIG_IPV6) |
| 315 | } else if (sk->sk_family == AF_INET6) { |
| 316 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr; |
| 317 | int addr_type, scoped, has_addr; |
| 318 | struct net_device *dev = NULL; |
| 319 | |
| 320 | if (addr_len < sizeof(*addr)) |
| 321 | return -EINVAL; |
| 322 | |
Hannes Frederic Sowa | 82b276c | 2014-01-20 05:16:39 +0100 | [diff] [blame] | 323 | if (addr->sin6_family != AF_INET6) |
| 324 | return -EINVAL; |
| 325 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 326 | pr_debug("ping_check_bind_addr(sk=%p,addr=%pI6c,port=%d)\n", |
| 327 | sk, addr->sin6_addr.s6_addr, ntohs(addr->sin6_port)); |
| 328 | |
| 329 | addr_type = ipv6_addr_type(&addr->sin6_addr); |
| 330 | scoped = __ipv6_addr_needs_scope_id(addr_type); |
| 331 | if ((addr_type != IPV6_ADDR_ANY && |
| 332 | !(addr_type & IPV6_ADDR_UNICAST)) || |
| 333 | (scoped && !addr->sin6_scope_id)) |
| 334 | return -EINVAL; |
| 335 | |
| 336 | rcu_read_lock(); |
| 337 | if (addr->sin6_scope_id) { |
| 338 | dev = dev_get_by_index_rcu(net, addr->sin6_scope_id); |
| 339 | if (!dev) { |
| 340 | rcu_read_unlock(); |
| 341 | return -ENODEV; |
| 342 | } |
| 343 | } |
| 344 | has_addr = pingv6_ops.ipv6_chk_addr(net, &addr->sin6_addr, dev, |
| 345 | scoped); |
| 346 | rcu_read_unlock(); |
| 347 | |
| 348 | if (!(isk->freebind || isk->transparent || has_addr || |
| 349 | addr_type == IPV6_ADDR_ANY)) |
| 350 | return -EADDRNOTAVAIL; |
| 351 | |
| 352 | if (scoped) |
| 353 | sk->sk_bound_dev_if = addr->sin6_scope_id; |
| 354 | #endif |
| 355 | } else { |
| 356 | return -EAFNOSUPPORT; |
| 357 | } |
| 358 | return 0; |
| 359 | } |
| 360 | |
Wu Fengguang | a06a2d3 | 2013-06-12 21:04:16 +0800 | [diff] [blame] | 361 | static void ping_set_saddr(struct sock *sk, struct sockaddr *saddr) |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 362 | { |
| 363 | if (saddr->sa_family == AF_INET) { |
| 364 | struct inet_sock *isk = inet_sk(sk); |
| 365 | struct sockaddr_in *addr = (struct sockaddr_in *) saddr; |
| 366 | isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr; |
| 367 | #if IS_ENABLED(CONFIG_IPV6) |
| 368 | } else if (saddr->sa_family == AF_INET6) { |
| 369 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr; |
| 370 | struct ipv6_pinfo *np = inet6_sk(sk); |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 371 | sk->sk_v6_rcv_saddr = np->saddr = addr->sin6_addr; |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 372 | #endif |
| 373 | } |
| 374 | } |
| 375 | |
Wu Fengguang | a06a2d3 | 2013-06-12 21:04:16 +0800 | [diff] [blame] | 376 | static void ping_clear_saddr(struct sock *sk, int dif) |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 377 | { |
| 378 | sk->sk_bound_dev_if = dif; |
| 379 | if (sk->sk_family == AF_INET) { |
| 380 | struct inet_sock *isk = inet_sk(sk); |
| 381 | isk->inet_rcv_saddr = isk->inet_saddr = 0; |
| 382 | #if IS_ENABLED(CONFIG_IPV6) |
| 383 | } else if (sk->sk_family == AF_INET6) { |
| 384 | struct ipv6_pinfo *np = inet6_sk(sk); |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 385 | memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr)); |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 386 | memset(&np->saddr, 0, sizeof(np->saddr)); |
| 387 | #endif |
| 388 | } |
| 389 | } |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 390 | /* |
| 391 | * We need our own bind because there are no privileged id's == local ports. |
| 392 | * Moreover, we don't allow binding to multi- and broadcast addresses. |
| 393 | */ |
| 394 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 395 | int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 396 | { |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 397 | struct inet_sock *isk = inet_sk(sk); |
| 398 | unsigned short snum; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 399 | int err; |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 400 | int dif = sk->sk_bound_dev_if; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 401 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 402 | err = ping_check_bind_addr(sk, isk, uaddr, addr_len); |
| 403 | if (err) |
| 404 | return err; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 405 | |
| 406 | lock_sock(sk); |
| 407 | |
| 408 | err = -EINVAL; |
| 409 | if (isk->inet_num != 0) |
| 410 | goto out; |
| 411 | |
| 412 | err = -EADDRINUSE; |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 413 | ping_set_saddr(sk, uaddr); |
| 414 | snum = ntohs(((struct sockaddr_in *)uaddr)->sin_port); |
| 415 | if (ping_get_port(sk, snum) != 0) { |
| 416 | ping_clear_saddr(sk, dif); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 417 | goto out; |
| 418 | } |
| 419 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 420 | pr_debug("after bind(): num = %d, dif = %d\n", |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 421 | (int)isk->inet_num, |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 422 | (int)sk->sk_bound_dev_if); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 423 | |
| 424 | err = 0; |
Eric Dumazet | c2bb06d | 2013-10-09 03:05:48 -0700 | [diff] [blame] | 425 | if (sk->sk_family == AF_INET && isk->inet_rcv_saddr) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 426 | sk->sk_userlocks |= SOCK_BINDADDR_LOCK; |
Eric Dumazet | c2bb06d | 2013-10-09 03:05:48 -0700 | [diff] [blame] | 427 | #if IS_ENABLED(CONFIG_IPV6) |
| 428 | if (sk->sk_family == AF_INET6 && !ipv6_addr_any(&sk->sk_v6_rcv_saddr)) |
| 429 | sk->sk_userlocks |= SOCK_BINDADDR_LOCK; |
| 430 | #endif |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 431 | |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 432 | if (snum) |
| 433 | sk->sk_userlocks |= SOCK_BINDPORT_LOCK; |
| 434 | isk->inet_sport = htons(isk->inet_num); |
| 435 | isk->inet_daddr = 0; |
| 436 | isk->inet_dport = 0; |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 437 | |
| 438 | #if IS_ENABLED(CONFIG_IPV6) |
| 439 | if (sk->sk_family == AF_INET6) |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 440 | memset(&sk->sk_v6_daddr, 0, sizeof(sk->sk_v6_daddr)); |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 441 | #endif |
| 442 | |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 443 | sk_dst_reset(sk); |
| 444 | out: |
| 445 | release_sock(sk); |
| 446 | pr_debug("ping_v4_bind -> %d\n", err); |
| 447 | return err; |
| 448 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 449 | EXPORT_SYMBOL_GPL(ping_bind); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 450 | |
| 451 | /* |
| 452 | * Is this a supported type of ICMP message? |
| 453 | */ |
| 454 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 455 | static inline int ping_supported(int family, int type, int code) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 456 | { |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 457 | return (family == AF_INET && type == ICMP_ECHO && code == 0) || |
| 458 | (family == AF_INET6 && type == ICMPV6_ECHO_REQUEST && code == 0); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* |
| 462 | * This routine is called by the ICMP module when it gets some |
| 463 | * sort of error condition. |
| 464 | */ |
| 465 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 466 | void ping_err(struct sk_buff *skb, int offset, u32 info) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 467 | { |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 468 | int family; |
| 469 | struct icmphdr *icmph; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 470 | struct inet_sock *inet_sock; |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 471 | int type; |
| 472 | int code; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 473 | struct net *net = dev_net(skb->dev); |
| 474 | struct sock *sk; |
| 475 | int harderr; |
| 476 | int err; |
| 477 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 478 | if (skb->protocol == htons(ETH_P_IP)) { |
| 479 | family = AF_INET; |
| 480 | type = icmp_hdr(skb)->type; |
| 481 | code = icmp_hdr(skb)->code; |
| 482 | icmph = (struct icmphdr *)(skb->data + offset); |
| 483 | } else if (skb->protocol == htons(ETH_P_IPV6)) { |
| 484 | family = AF_INET6; |
| 485 | type = icmp6_hdr(skb)->icmp6_type; |
| 486 | code = icmp6_hdr(skb)->icmp6_code; |
| 487 | icmph = (struct icmphdr *) (skb->data + offset); |
| 488 | } else { |
| 489 | BUG(); |
| 490 | } |
| 491 | |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 492 | /* We assume the packet has already been checked by icmp_unreach */ |
| 493 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 494 | if (!ping_supported(family, icmph->type, icmph->code)) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 495 | return; |
| 496 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 497 | pr_debug("ping_err(proto=0x%x,type=%d,code=%d,id=%04x,seq=%04x)\n", |
| 498 | skb->protocol, type, code, ntohs(icmph->un.echo.id), |
| 499 | ntohs(icmph->un.echo.sequence)); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 500 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 501 | sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id)); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 502 | if (sk == NULL) { |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 503 | pr_debug("no socket, dropping\n"); |
| 504 | return; /* No socket for error */ |
| 505 | } |
| 506 | pr_debug("err on socket %p\n", sk); |
| 507 | |
| 508 | err = 0; |
| 509 | harderr = 0; |
| 510 | inet_sock = inet_sk(sk); |
| 511 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 512 | if (skb->protocol == htons(ETH_P_IP)) { |
| 513 | switch (type) { |
| 514 | default: |
| 515 | case ICMP_TIME_EXCEEDED: |
| 516 | err = EHOSTUNREACH; |
| 517 | break; |
| 518 | case ICMP_SOURCE_QUENCH: |
| 519 | /* This is not a real error but ping wants to see it. |
| 520 | * Report it with some fake errno. |
| 521 | */ |
| 522 | err = EREMOTEIO; |
| 523 | break; |
| 524 | case ICMP_PARAMETERPROB: |
| 525 | err = EPROTO; |
| 526 | harderr = 1; |
| 527 | break; |
| 528 | case ICMP_DEST_UNREACH: |
| 529 | if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ |
| 530 | ipv4_sk_update_pmtu(skb, sk, info); |
| 531 | if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) { |
| 532 | err = EMSGSIZE; |
| 533 | harderr = 1; |
| 534 | break; |
| 535 | } |
| 536 | goto out; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 537 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 538 | err = EHOSTUNREACH; |
| 539 | if (code <= NR_ICMP_UNREACH) { |
| 540 | harderr = icmp_err_convert[code].fatal; |
| 541 | err = icmp_err_convert[code].errno; |
| 542 | } |
| 543 | break; |
| 544 | case ICMP_REDIRECT: |
| 545 | /* See ICMP_SOURCE_QUENCH */ |
| 546 | ipv4_sk_redirect(skb, sk); |
| 547 | err = EREMOTEIO; |
| 548 | break; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 549 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 550 | #if IS_ENABLED(CONFIG_IPV6) |
| 551 | } else if (skb->protocol == htons(ETH_P_IPV6)) { |
| 552 | harderr = pingv6_ops.icmpv6_err_convert(type, code, &err); |
| 553 | #endif |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* |
| 557 | * RFC1122: OK. Passes ICMP errors back to application, as per |
| 558 | * 4.1.3.3. |
| 559 | */ |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 560 | if ((family == AF_INET && !inet_sock->recverr) || |
| 561 | (family == AF_INET6 && !inet6_sk(sk)->recverr)) { |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 562 | if (!harderr || sk->sk_state != TCP_ESTABLISHED) |
| 563 | goto out; |
| 564 | } else { |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 565 | if (family == AF_INET) { |
| 566 | ip_icmp_error(sk, skb, err, 0 /* no remote port */, |
| 567 | info, (u8 *)icmph); |
| 568 | #if IS_ENABLED(CONFIG_IPV6) |
| 569 | } else if (family == AF_INET6) { |
| 570 | pingv6_ops.ipv6_icmp_error(sk, skb, err, 0, |
| 571 | info, (u8 *)icmph); |
| 572 | #endif |
| 573 | } |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 574 | } |
| 575 | sk->sk_err = err; |
| 576 | sk->sk_error_report(sk); |
| 577 | out: |
| 578 | sock_put(sk); |
| 579 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 580 | EXPORT_SYMBOL_GPL(ping_err); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 581 | |
| 582 | /* |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 583 | * Copy and checksum an ICMP Echo packet from user space into a buffer |
| 584 | * starting from the payload. |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 585 | */ |
| 586 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 587 | int ping_getfrag(void *from, char *to, |
| 588 | int offset, int fraglen, int odd, struct sk_buff *skb) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 589 | { |
| 590 | struct pingfakehdr *pfh = (struct pingfakehdr *)from; |
| 591 | |
| 592 | if (offset == 0) { |
| 593 | if (fraglen < sizeof(struct icmphdr)) |
| 594 | BUG(); |
| 595 | if (csum_partial_copy_fromiovecend(to + sizeof(struct icmphdr), |
| 596 | pfh->iov, 0, fraglen - sizeof(struct icmphdr), |
| 597 | &pfh->wcheck)) |
| 598 | return -EFAULT; |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 599 | } else if (offset < sizeof(struct icmphdr)) { |
| 600 | BUG(); |
| 601 | } else { |
| 602 | if (csum_partial_copy_fromiovecend |
| 603 | (to, pfh->iov, offset - sizeof(struct icmphdr), |
| 604 | fraglen, &pfh->wcheck)) |
| 605 | return -EFAULT; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 606 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 607 | |
| 608 | #if IS_ENABLED(CONFIG_IPV6) |
| 609 | /* For IPv6, checksum each skb as we go along, as expected by |
| 610 | * icmpv6_push_pending_frames. For IPv4, accumulate the checksum in |
| 611 | * wcheck, it will be finalized in ping_v4_push_pending_frames. |
| 612 | */ |
| 613 | if (pfh->family == AF_INET6) { |
| 614 | skb->csum = pfh->wcheck; |
| 615 | skb->ip_summed = CHECKSUM_NONE; |
| 616 | pfh->wcheck = 0; |
| 617 | } |
| 618 | #endif |
| 619 | |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 620 | return 0; |
| 621 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 622 | EXPORT_SYMBOL_GPL(ping_getfrag); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 623 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 624 | static int ping_v4_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh, |
| 625 | struct flowi4 *fl4) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 626 | { |
| 627 | struct sk_buff *skb = skb_peek(&sk->sk_write_queue); |
| 628 | |
| 629 | pfh->wcheck = csum_partial((char *)&pfh->icmph, |
| 630 | sizeof(struct icmphdr), pfh->wcheck); |
| 631 | pfh->icmph.checksum = csum_fold(pfh->wcheck); |
| 632 | memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr)); |
| 633 | skb->ip_summed = CHECKSUM_NONE; |
| 634 | return ip_push_pending_frames(sk, fl4); |
| 635 | } |
| 636 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 637 | int ping_common_sendmsg(int family, struct msghdr *msg, size_t len, |
| 638 | void *user_icmph, size_t icmph_len) { |
| 639 | u8 type, code; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 640 | |
| 641 | if (len > 0xFFFF) |
| 642 | return -EMSGSIZE; |
| 643 | |
| 644 | /* |
| 645 | * Check the flags. |
| 646 | */ |
| 647 | |
| 648 | /* Mirror BSD error message compatibility */ |
| 649 | if (msg->msg_flags & MSG_OOB) |
| 650 | return -EOPNOTSUPP; |
| 651 | |
| 652 | /* |
| 653 | * Fetch the ICMP header provided by the userland. |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 654 | * iovec is modified! The ICMP header is consumed. |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 655 | */ |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 656 | if (memcpy_fromiovec(user_icmph, msg->msg_iov, icmph_len)) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 657 | return -EFAULT; |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 658 | |
| 659 | if (family == AF_INET) { |
| 660 | type = ((struct icmphdr *) user_icmph)->type; |
| 661 | code = ((struct icmphdr *) user_icmph)->code; |
| 662 | #if IS_ENABLED(CONFIG_IPV6) |
| 663 | } else if (family == AF_INET6) { |
| 664 | type = ((struct icmp6hdr *) user_icmph)->icmp6_type; |
| 665 | code = ((struct icmp6hdr *) user_icmph)->icmp6_code; |
| 666 | #endif |
| 667 | } else { |
| 668 | BUG(); |
| 669 | } |
| 670 | |
| 671 | if (!ping_supported(family, type, code)) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 672 | return -EINVAL; |
| 673 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 674 | return 0; |
| 675 | } |
| 676 | EXPORT_SYMBOL_GPL(ping_common_sendmsg); |
| 677 | |
Stephen Hemminger | ea074b3 | 2013-12-28 11:11:42 -0800 | [diff] [blame] | 678 | static int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, |
| 679 | size_t len) |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 680 | { |
| 681 | struct net *net = sock_net(sk); |
| 682 | struct flowi4 fl4; |
| 683 | struct inet_sock *inet = inet_sk(sk); |
| 684 | struct ipcm_cookie ipc; |
| 685 | struct icmphdr user_icmph; |
| 686 | struct pingfakehdr pfh; |
| 687 | struct rtable *rt = NULL; |
| 688 | struct ip_options_data opt_copy; |
| 689 | int free = 0; |
| 690 | __be32 saddr, daddr, faddr; |
| 691 | u8 tos; |
| 692 | int err; |
| 693 | |
| 694 | pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num); |
| 695 | |
| 696 | err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph, |
| 697 | sizeof(user_icmph)); |
| 698 | if (err) |
| 699 | return err; |
| 700 | |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 701 | /* |
| 702 | * Get and verify the address. |
| 703 | */ |
| 704 | |
| 705 | if (msg->msg_name) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 706 | DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 707 | if (msg->msg_namelen < sizeof(*usin)) |
| 708 | return -EINVAL; |
| 709 | if (usin->sin_family != AF_INET) |
| 710 | return -EINVAL; |
| 711 | daddr = usin->sin_addr.s_addr; |
| 712 | /* no remote port */ |
| 713 | } else { |
| 714 | if (sk->sk_state != TCP_ESTABLISHED) |
| 715 | return -EDESTADDRREQ; |
| 716 | daddr = inet->inet_daddr; |
| 717 | /* no remote port */ |
| 718 | } |
| 719 | |
| 720 | ipc.addr = inet->inet_saddr; |
| 721 | ipc.opt = NULL; |
| 722 | ipc.oif = sk->sk_bound_dev_if; |
| 723 | ipc.tx_flags = 0; |
Francesco Fusco | aa66158 | 2013-09-24 15:43:09 +0200 | [diff] [blame] | 724 | ipc.ttl = 0; |
| 725 | ipc.tos = -1; |
Daniel Borkmann | bf84a01 | 2013-04-14 08:08:13 +0000 | [diff] [blame] | 726 | |
| 727 | sock_tx_timestamp(sk, &ipc.tx_flags); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 728 | |
| 729 | if (msg->msg_controllen) { |
| 730 | err = ip_cmsg_send(sock_net(sk), msg, &ipc); |
| 731 | if (err) |
| 732 | return err; |
| 733 | if (ipc.opt) |
| 734 | free = 1; |
| 735 | } |
| 736 | if (!ipc.opt) { |
| 737 | struct ip_options_rcu *inet_opt; |
| 738 | |
| 739 | rcu_read_lock(); |
| 740 | inet_opt = rcu_dereference(inet->inet_opt); |
| 741 | if (inet_opt) { |
| 742 | memcpy(&opt_copy, inet_opt, |
| 743 | sizeof(*inet_opt) + inet_opt->opt.optlen); |
| 744 | ipc.opt = &opt_copy.opt; |
| 745 | } |
| 746 | rcu_read_unlock(); |
| 747 | } |
| 748 | |
| 749 | saddr = ipc.addr; |
| 750 | ipc.addr = faddr = daddr; |
| 751 | |
| 752 | if (ipc.opt && ipc.opt->opt.srr) { |
| 753 | if (!daddr) |
| 754 | return -EINVAL; |
| 755 | faddr = ipc.opt->opt.faddr; |
| 756 | } |
Francesco Fusco | aa66158 | 2013-09-24 15:43:09 +0200 | [diff] [blame] | 757 | tos = get_rttos(&ipc, inet); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 758 | if (sock_flag(sk, SOCK_LOCALROUTE) || |
| 759 | (msg->msg_flags & MSG_DONTROUTE) || |
| 760 | (ipc.opt && ipc.opt->opt.is_strictroute)) { |
| 761 | tos |= RTO_ONLINK; |
| 762 | } |
| 763 | |
| 764 | if (ipv4_is_multicast(daddr)) { |
| 765 | if (!ipc.oif) |
| 766 | ipc.oif = inet->mc_index; |
| 767 | if (!saddr) |
| 768 | saddr = inet->mc_addr; |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 769 | } else if (!ipc.oif) |
| 770 | ipc.oif = inet->uc_index; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 771 | |
| 772 | flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos, |
| 773 | RT_SCOPE_UNIVERSE, sk->sk_protocol, |
| 774 | inet_sk_flowi_flags(sk), faddr, saddr, 0, 0); |
| 775 | |
| 776 | security_sk_classify_flow(sk, flowi4_to_flowi(&fl4)); |
| 777 | rt = ip_route_output_flow(net, &fl4, sk); |
| 778 | if (IS_ERR(rt)) { |
| 779 | err = PTR_ERR(rt); |
| 780 | rt = NULL; |
| 781 | if (err == -ENETUNREACH) |
Eric Dumazet | f1d8cba | 2013-11-28 09:51:22 -0800 | [diff] [blame] | 782 | IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 783 | goto out; |
| 784 | } |
| 785 | |
| 786 | err = -EACCES; |
| 787 | if ((rt->rt_flags & RTCF_BROADCAST) && |
| 788 | !sock_flag(sk, SOCK_BROADCAST)) |
| 789 | goto out; |
| 790 | |
| 791 | if (msg->msg_flags & MSG_CONFIRM) |
| 792 | goto do_confirm; |
| 793 | back_from_confirm: |
| 794 | |
| 795 | if (!ipc.addr) |
| 796 | ipc.addr = fl4.daddr; |
| 797 | |
| 798 | lock_sock(sk); |
| 799 | |
| 800 | pfh.icmph.type = user_icmph.type; /* already checked */ |
| 801 | pfh.icmph.code = user_icmph.code; /* ditto */ |
| 802 | pfh.icmph.checksum = 0; |
| 803 | pfh.icmph.un.echo.id = inet->inet_sport; |
| 804 | pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence; |
| 805 | pfh.iov = msg->msg_iov; |
| 806 | pfh.wcheck = 0; |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 807 | pfh.family = AF_INET; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 808 | |
| 809 | err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len, |
| 810 | 0, &ipc, &rt, msg->msg_flags); |
| 811 | if (err) |
| 812 | ip_flush_pending_frames(sk); |
| 813 | else |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 814 | err = ping_v4_push_pending_frames(sk, &pfh, &fl4); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 815 | release_sock(sk); |
| 816 | |
| 817 | out: |
| 818 | ip_rt_put(rt); |
| 819 | if (free) |
| 820 | kfree(ipc.opt); |
| 821 | if (!err) { |
| 822 | icmp_out_count(sock_net(sk), user_icmph.type); |
| 823 | return len; |
| 824 | } |
| 825 | return err; |
| 826 | |
| 827 | do_confirm: |
| 828 | dst_confirm(&rt->dst); |
| 829 | if (!(msg->msg_flags & MSG_PROBE) || len) |
| 830 | goto back_from_confirm; |
| 831 | err = 0; |
| 832 | goto out; |
| 833 | } |
| 834 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 835 | int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, |
| 836 | size_t len, int noblock, int flags, int *addr_len) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 837 | { |
| 838 | struct inet_sock *isk = inet_sk(sk); |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 839 | int family = sk->sk_family; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 840 | struct sk_buff *skb; |
| 841 | int copied, err; |
| 842 | |
| 843 | pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num); |
| 844 | |
David S. Miller | a5e7424 | 2012-02-21 17:59:19 -0500 | [diff] [blame] | 845 | err = -EOPNOTSUPP; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 846 | if (flags & MSG_OOB) |
| 847 | goto out; |
| 848 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 849 | if (flags & MSG_ERRQUEUE) { |
| 850 | if (family == AF_INET) { |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 851 | return ip_recv_error(sk, msg, len, addr_len); |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 852 | #if IS_ENABLED(CONFIG_IPV6) |
| 853 | } else if (family == AF_INET6) { |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 854 | return pingv6_ops.ipv6_recv_error(sk, msg, len, |
| 855 | addr_len); |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 856 | #endif |
| 857 | } |
| 858 | } |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 859 | |
| 860 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 861 | if (!skb) |
| 862 | goto out; |
| 863 | |
| 864 | copied = skb->len; |
| 865 | if (copied > len) { |
| 866 | msg->msg_flags |= MSG_TRUNC; |
| 867 | copied = len; |
| 868 | } |
| 869 | |
| 870 | /* Don't bother checking the checksum */ |
| 871 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 872 | if (err) |
| 873 | goto done; |
| 874 | |
| 875 | sock_recv_timestamp(msg, sk, skb); |
| 876 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 877 | /* Copy the address and add cmsg data. */ |
| 878 | if (family == AF_INET) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 879 | DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); |
Hannes Frederic Sowa | bceaa90 | 2013-11-18 04:20:45 +0100 | [diff] [blame] | 880 | |
Hannes Frederic Sowa | cf970c0 | 2013-11-18 07:07:45 +0100 | [diff] [blame] | 881 | if (sin) { |
| 882 | sin->sin_family = AF_INET; |
| 883 | sin->sin_port = 0 /* skb->h.uh->source */; |
| 884 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
| 885 | memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 886 | *addr_len = sizeof(*sin); |
| 887 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 888 | |
| 889 | if (isk->cmsg_flags) |
| 890 | ip_cmsg_recv(msg, skb); |
| 891 | |
| 892 | #if IS_ENABLED(CONFIG_IPV6) |
| 893 | } else if (family == AF_INET6) { |
| 894 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 895 | struct ipv6hdr *ip6 = ipv6_hdr(skb); |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 896 | DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 897 | |
Hannes Frederic Sowa | cf970c0 | 2013-11-18 07:07:45 +0100 | [diff] [blame] | 898 | if (sin6) { |
| 899 | sin6->sin6_family = AF_INET6; |
| 900 | sin6->sin6_port = 0; |
| 901 | sin6->sin6_addr = ip6->saddr; |
| 902 | sin6->sin6_flowinfo = 0; |
| 903 | if (np->sndflow) |
| 904 | sin6->sin6_flowinfo = ip6_flowinfo(ip6); |
| 905 | sin6->sin6_scope_id = |
| 906 | ipv6_iface_scope_id(&sin6->sin6_addr, |
| 907 | IP6CB(skb)->iif); |
| 908 | *addr_len = sizeof(*sin6); |
| 909 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 910 | |
| 911 | if (inet6_sk(sk)->rxopt.all) |
Hannes Frederic Sowa | 4b261c7 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 912 | pingv6_ops.ip6_datagram_recv_common_ctl(sk, msg, skb); |
| 913 | if (skb->protocol == htons(ETH_P_IPV6) && |
| 914 | inet6_sk(sk)->rxopt.all) |
| 915 | pingv6_ops.ip6_datagram_recv_specific_ctl(sk, msg, skb); |
| 916 | else if (skb->protocol == htons(ETH_P_IP) && isk->cmsg_flags) |
| 917 | ip_cmsg_recv(msg, skb); |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 918 | #endif |
| 919 | } else { |
| 920 | BUG(); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 921 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 922 | |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 923 | err = copied; |
| 924 | |
| 925 | done: |
| 926 | skb_free_datagram(sk, skb); |
| 927 | out: |
| 928 | pr_debug("ping_recvmsg -> %d\n", err); |
| 929 | return err; |
| 930 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 931 | EXPORT_SYMBOL_GPL(ping_recvmsg); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 932 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 933 | int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 934 | { |
| 935 | pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n", |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 936 | inet_sk(sk), inet_sk(sk)->inet_num, skb); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 937 | if (sock_queue_rcv_skb(sk, skb) < 0) { |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 938 | kfree_skb(skb); |
| 939 | pr_debug("ping_queue_rcv_skb -> failed\n"); |
| 940 | return -1; |
| 941 | } |
| 942 | return 0; |
| 943 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 944 | EXPORT_SYMBOL_GPL(ping_queue_rcv_skb); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 945 | |
| 946 | |
| 947 | /* |
| 948 | * All we need to do is get the socket. |
| 949 | */ |
| 950 | |
| 951 | void ping_rcv(struct sk_buff *skb) |
| 952 | { |
| 953 | struct sock *sk; |
| 954 | struct net *net = dev_net(skb->dev); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 955 | struct icmphdr *icmph = icmp_hdr(skb); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 956 | |
| 957 | /* We assume the packet has already been checked by icmp_rcv */ |
| 958 | |
| 959 | pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n", |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 960 | skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence)); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 961 | |
| 962 | /* Push ICMP header back */ |
| 963 | skb_push(skb, skb->data - (u8 *)icmph); |
| 964 | |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 965 | sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id)); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 966 | if (sk != NULL) { |
| 967 | pr_debug("rcv on socket %p\n", sk); |
| 968 | ping_queue_rcv_skb(sk, skb_get(skb)); |
| 969 | sock_put(sk); |
| 970 | return; |
| 971 | } |
| 972 | pr_debug("no socket, dropping\n"); |
| 973 | |
| 974 | /* We're called from icmp_rcv(). kfree_skb() is done there. */ |
| 975 | } |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 976 | EXPORT_SYMBOL_GPL(ping_rcv); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 977 | |
| 978 | struct proto ping_prot = { |
| 979 | .name = "PING", |
| 980 | .owner = THIS_MODULE, |
| 981 | .init = ping_init_sock, |
| 982 | .close = ping_close, |
| 983 | .connect = ip4_datagram_connect, |
| 984 | .disconnect = udp_disconnect, |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 985 | .setsockopt = ip_setsockopt, |
| 986 | .getsockopt = ip_getsockopt, |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 987 | .sendmsg = ping_v4_sendmsg, |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 988 | .recvmsg = ping_recvmsg, |
| 989 | .bind = ping_bind, |
| 990 | .backlog_rcv = ping_queue_rcv_skb, |
Steffen Klassert | 8141ed9 | 2013-01-21 02:00:03 +0000 | [diff] [blame] | 991 | .release_cb = ip4_datagram_release_cb, |
Lorenzo Colitti | 6d0bfe2 | 2013-05-22 20:17:31 +0000 | [diff] [blame] | 992 | .hash = ping_hash, |
| 993 | .unhash = ping_unhash, |
| 994 | .get_port = ping_get_port, |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 995 | .obj_size = sizeof(struct inet_sock), |
| 996 | }; |
| 997 | EXPORT_SYMBOL(ping_prot); |
| 998 | |
| 999 | #ifdef CONFIG_PROC_FS |
| 1000 | |
| 1001 | static struct sock *ping_get_first(struct seq_file *seq, int start) |
| 1002 | { |
| 1003 | struct sock *sk; |
| 1004 | struct ping_iter_state *state = seq->private; |
| 1005 | struct net *net = seq_file_net(seq); |
| 1006 | |
| 1007 | for (state->bucket = start; state->bucket < PING_HTABLE_SIZE; |
| 1008 | ++state->bucket) { |
| 1009 | struct hlist_nulls_node *node; |
Changli Gao | 75e308c | 2011-05-18 21:16:01 +0000 | [diff] [blame] | 1010 | struct hlist_nulls_head *hslot; |
| 1011 | |
| 1012 | hslot = &ping_table.hash[state->bucket]; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1013 | |
| 1014 | if (hlist_nulls_empty(hslot)) |
| 1015 | continue; |
| 1016 | |
| 1017 | sk_nulls_for_each(sk, node, hslot) { |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1018 | if (net_eq(sock_net(sk), net) && |
| 1019 | sk->sk_family == state->family) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1020 | goto found; |
| 1021 | } |
| 1022 | } |
| 1023 | sk = NULL; |
| 1024 | found: |
| 1025 | return sk; |
| 1026 | } |
| 1027 | |
| 1028 | static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk) |
| 1029 | { |
| 1030 | struct ping_iter_state *state = seq->private; |
| 1031 | struct net *net = seq_file_net(seq); |
| 1032 | |
| 1033 | do { |
| 1034 | sk = sk_nulls_next(sk); |
| 1035 | } while (sk && (!net_eq(sock_net(sk), net))); |
| 1036 | |
| 1037 | if (!sk) |
| 1038 | return ping_get_first(seq, state->bucket + 1); |
| 1039 | return sk; |
| 1040 | } |
| 1041 | |
| 1042 | static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos) |
| 1043 | { |
| 1044 | struct sock *sk = ping_get_first(seq, 0); |
| 1045 | |
| 1046 | if (sk) |
| 1047 | while (pos && (sk = ping_get_next(seq, sk)) != NULL) |
| 1048 | --pos; |
| 1049 | return pos ? NULL : sk; |
| 1050 | } |
| 1051 | |
Lorenzo Colitti | d862e54 | 2013-05-31 15:05:50 +0000 | [diff] [blame] | 1052 | void *ping_seq_start(struct seq_file *seq, loff_t *pos, sa_family_t family) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1053 | { |
| 1054 | struct ping_iter_state *state = seq->private; |
| 1055 | state->bucket = 0; |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1056 | state->family = family; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1057 | |
| 1058 | read_lock_bh(&ping_table.lock); |
| 1059 | |
| 1060 | return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN; |
| 1061 | } |
Lorenzo Colitti | d862e54 | 2013-05-31 15:05:50 +0000 | [diff] [blame] | 1062 | EXPORT_SYMBOL_GPL(ping_seq_start); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1063 | |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1064 | static void *ping_v4_seq_start(struct seq_file *seq, loff_t *pos) |
| 1065 | { |
| 1066 | return ping_seq_start(seq, pos, AF_INET); |
| 1067 | } |
| 1068 | |
Lorenzo Colitti | d862e54 | 2013-05-31 15:05:50 +0000 | [diff] [blame] | 1069 | void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1070 | { |
| 1071 | struct sock *sk; |
| 1072 | |
| 1073 | if (v == SEQ_START_TOKEN) |
| 1074 | sk = ping_get_idx(seq, 0); |
| 1075 | else |
| 1076 | sk = ping_get_next(seq, v); |
| 1077 | |
| 1078 | ++*pos; |
| 1079 | return sk; |
| 1080 | } |
Lorenzo Colitti | d862e54 | 2013-05-31 15:05:50 +0000 | [diff] [blame] | 1081 | EXPORT_SYMBOL_GPL(ping_seq_next); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1082 | |
Lorenzo Colitti | d862e54 | 2013-05-31 15:05:50 +0000 | [diff] [blame] | 1083 | void ping_seq_stop(struct seq_file *seq, void *v) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1084 | { |
| 1085 | read_unlock_bh(&ping_table.lock); |
| 1086 | } |
Lorenzo Colitti | d862e54 | 2013-05-31 15:05:50 +0000 | [diff] [blame] | 1087 | EXPORT_SYMBOL_GPL(ping_seq_stop); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1088 | |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1089 | static void ping_v4_format_sock(struct sock *sp, struct seq_file *f, |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 1090 | int bucket) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1091 | { |
| 1092 | struct inet_sock *inet = inet_sk(sp); |
| 1093 | __be32 dest = inet->inet_daddr; |
| 1094 | __be32 src = inet->inet_rcv_saddr; |
| 1095 | __u16 destp = ntohs(inet->inet_dport); |
| 1096 | __u16 srcp = ntohs(inet->inet_sport); |
| 1097 | |
| 1098 | seq_printf(f, "%5d: %08X:%04X %08X:%04X" |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 1099 | " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d", |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1100 | bucket, src, srcp, dest, destp, sp->sk_state, |
| 1101 | sk_wmem_alloc_get(sp), |
| 1102 | sk_rmem_alloc_get(sp), |
Eric W. Biederman | a7cb5a4 | 2012-05-24 01:10:10 -0600 | [diff] [blame] | 1103 | 0, 0L, 0, |
| 1104 | from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), |
| 1105 | 0, sock_i_ino(sp), |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1106 | atomic_read(&sp->sk_refcnt), sp, |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 1107 | atomic_read(&sp->sk_drops)); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1110 | static int ping_v4_seq_show(struct seq_file *seq, void *v) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1111 | { |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 1112 | seq_setwidth(seq, 127); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1113 | if (v == SEQ_START_TOKEN) |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 1114 | seq_puts(seq, " sl local_address rem_address st tx_queue " |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1115 | "rx_queue tr tm->when retrnsmt uid timeout " |
| 1116 | "inode ref pointer drops"); |
| 1117 | else { |
| 1118 | struct ping_iter_state *state = seq->private; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1119 | |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 1120 | ping_v4_format_sock(v, seq, state->bucket); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1121 | } |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 1122 | seq_pad(seq, '\n'); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1123 | return 0; |
| 1124 | } |
| 1125 | |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1126 | static const struct seq_operations ping_v4_seq_ops = { |
| 1127 | .show = ping_v4_seq_show, |
| 1128 | .start = ping_v4_seq_start, |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1129 | .next = ping_seq_next, |
| 1130 | .stop = ping_seq_stop, |
| 1131 | }; |
| 1132 | |
| 1133 | static int ping_seq_open(struct inode *inode, struct file *file) |
| 1134 | { |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1135 | struct ping_seq_afinfo *afinfo = PDE_DATA(inode); |
| 1136 | return seq_open_net(inode, file, &afinfo->seq_ops, |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1137 | sizeof(struct ping_iter_state)); |
| 1138 | } |
| 1139 | |
Lorenzo Colitti | d862e54 | 2013-05-31 15:05:50 +0000 | [diff] [blame] | 1140 | const struct file_operations ping_seq_fops = { |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1141 | .open = ping_seq_open, |
| 1142 | .read = seq_read, |
| 1143 | .llseek = seq_lseek, |
| 1144 | .release = seq_release_net, |
| 1145 | }; |
Lorenzo Colitti | d862e54 | 2013-05-31 15:05:50 +0000 | [diff] [blame] | 1146 | EXPORT_SYMBOL_GPL(ping_seq_fops); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1147 | |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1148 | static struct ping_seq_afinfo ping_v4_seq_afinfo = { |
| 1149 | .name = "icmp", |
| 1150 | .family = AF_INET, |
| 1151 | .seq_fops = &ping_seq_fops, |
| 1152 | .seq_ops = { |
| 1153 | .start = ping_v4_seq_start, |
| 1154 | .show = ping_v4_seq_show, |
| 1155 | .next = ping_seq_next, |
| 1156 | .stop = ping_seq_stop, |
| 1157 | }, |
| 1158 | }; |
| 1159 | |
Lorenzo Colitti | d862e54 | 2013-05-31 15:05:50 +0000 | [diff] [blame] | 1160 | int ping_proc_register(struct net *net, struct ping_seq_afinfo *afinfo) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1161 | { |
| 1162 | struct proc_dir_entry *p; |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1163 | p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net, |
| 1164 | afinfo->seq_fops, afinfo); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1165 | if (!p) |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1166 | return -ENOMEM; |
| 1167 | return 0; |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1168 | } |
Lorenzo Colitti | d862e54 | 2013-05-31 15:05:50 +0000 | [diff] [blame] | 1169 | EXPORT_SYMBOL_GPL(ping_proc_register); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1170 | |
Lorenzo Colitti | d862e54 | 2013-05-31 15:05:50 +0000 | [diff] [blame] | 1171 | void ping_proc_unregister(struct net *net, struct ping_seq_afinfo *afinfo) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1172 | { |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1173 | remove_proc_entry(afinfo->name, net->proc_net); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1174 | } |
Lorenzo Colitti | d862e54 | 2013-05-31 15:05:50 +0000 | [diff] [blame] | 1175 | EXPORT_SYMBOL_GPL(ping_proc_unregister); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1176 | |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1177 | static int __net_init ping_v4_proc_init_net(struct net *net) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1178 | { |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1179 | return ping_proc_register(net, &ping_v4_seq_afinfo); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1182 | static void __net_exit ping_v4_proc_exit_net(struct net *net) |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1183 | { |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1184 | ping_proc_unregister(net, &ping_v4_seq_afinfo); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1187 | static struct pernet_operations ping_v4_net_ops = { |
| 1188 | .init = ping_v4_proc_init_net, |
| 1189 | .exit = ping_v4_proc_exit_net, |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1190 | }; |
| 1191 | |
| 1192 | int __init ping_proc_init(void) |
| 1193 | { |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1194 | return register_pernet_subsys(&ping_v4_net_ops); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | void ping_proc_exit(void) |
| 1198 | { |
Lorenzo Colitti | 8cc785f | 2013-05-31 15:05:49 +0000 | [diff] [blame] | 1199 | unregister_pernet_subsys(&ping_v4_net_ops); |
Vasiliy Kulikov | c319b4d | 2011-05-13 10:01:00 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | #endif |
| 1203 | |
| 1204 | void __init ping_init(void) |
| 1205 | { |
| 1206 | int i; |
| 1207 | |
| 1208 | for (i = 0; i < PING_HTABLE_SIZE; i++) |
| 1209 | INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i); |
| 1210 | rwlock_init(&ping_table.lock); |
| 1211 | } |