net: ipv6: Add IPv6 support to the ping socket.

This adds the ability to send ICMPv6 echo requests without a
raw socket. The equivalent ability for ICMPv4 was added in
2011.

Instead of having separate code paths for IPv4 and IPv6, make
most of the code in net/ipv4/ping.c dual-stack and only add a
few IPv6-specific bits (like the protocol definition) to a new
net/ipv6/ping.c. Hopefully this will reduce divergence and/or
duplication of bugs in the future.

Caveats:

- Setting options via ancillary data (e.g., using IPV6_PKTINFO
  to specify the outgoing interface) is not yet supported.
- There are no separate security settings for IPv4 and IPv6;
  everything is controlled by /proc/net/ipv4/ping_group_range.
- The proc interface does not yet display IPv6 ping sockets
  properly.

Tested with a patched copy of ping6 and using raw socket calls.
Compiles and works with all of CONFIG_IPV6={n,m,y}.

Signed-off-by: Lorenzo Colitti <lorenzo@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Lorenzo Colitti 2013-05-22 20:17:31 +00:00 коммит произвёл David S. Miller
Родитель 6e2842f4bb
Коммит 6d0bfe2261
9 изменённых файлов: 699 добавлений и 172 удалений

Просмотреть файл

@ -260,6 +260,12 @@ static inline void fl6_sock_release(struct ip6_flowlabel *fl)
extern void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info); extern void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info);
int icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
struct icmp6hdr *thdr, int len);
struct dst_entry *icmpv6_route_lookup(struct net *net, struct sk_buff *skb,
struct sock *sk, struct flowi6 *fl6);
extern int ip6_ra_control(struct sock *sk, int sel); extern int ip6_ra_control(struct sock *sk, int sel);
extern int ipv6_parse_hopopts(struct sk_buff *skb); extern int ipv6_parse_hopopts(struct sk_buff *skb);

Просмотреть файл

@ -13,6 +13,7 @@
#ifndef _PING_H #ifndef _PING_H
#define _PING_H #define _PING_H
#include <net/icmp.h>
#include <net/netns/hash.h> #include <net/netns/hash.h>
/* PING_HTABLE_SIZE must be power of 2 */ /* PING_HTABLE_SIZE must be power of 2 */
@ -28,6 +29,18 @@
*/ */
#define GID_T_MAX (((gid_t)~0U) >> 1) #define GID_T_MAX (((gid_t)~0U) >> 1)
/* Compatibility glue so we can support IPv6 when it's compiled as a module */
struct pingv6_ops {
int (*ipv6_recv_error)(struct sock *sk, struct msghdr *msg, int len);
int (*ip6_datagram_recv_ctl)(struct sock *sk, struct msghdr *msg,
struct sk_buff *skb);
int (*icmpv6_err_convert)(u8 type, u8 code, int *err);
void (*ipv6_icmp_error)(struct sock *sk, struct sk_buff *skb, int err,
__be16 port, u32 info, u8 *payload);
int (*ipv6_chk_addr)(struct net *net, const struct in6_addr *addr,
struct net_device *dev, int strict);
};
struct ping_table { struct ping_table {
struct hlist_nulls_head hash[PING_HTABLE_SIZE]; struct hlist_nulls_head hash[PING_HTABLE_SIZE];
rwlock_t lock; rwlock_t lock;
@ -39,10 +52,39 @@ struct ping_iter_state {
}; };
extern struct proto ping_prot; extern struct proto ping_prot;
extern struct ping_table ping_table;
#if IS_ENABLED(CONFIG_IPV6)
extern struct pingv6_ops pingv6_ops;
#endif
struct pingfakehdr {
struct icmphdr icmph;
struct iovec *iov;
sa_family_t family;
__wsum wcheck;
};
extern void ping_rcv(struct sk_buff *); int ping_get_port(struct sock *sk, unsigned short ident);
extern void ping_err(struct sk_buff *, u32 info); void ping_hash(struct sock *sk);
void ping_unhash(struct sock *sk);
int ping_init_sock(struct sock *sk);
void ping_close(struct sock *sk, long timeout);
int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len);
void ping_err(struct sk_buff *skb, int offset, u32 info);
int ping_getfrag(void *from, char *to, int offset, int fraglen, int odd,
struct sk_buff *);
int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
size_t len, int noblock, int flags, int *addr_len);
int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
void *user_icmph, size_t icmph_len);
int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
size_t len);
int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
size_t len);
int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
void ping_rcv(struct sk_buff *skb);
#ifdef CONFIG_PROC_FS #ifdef CONFIG_PROC_FS
extern int __init ping_proc_init(void); extern int __init ping_proc_init(void);
@ -50,6 +92,7 @@ extern void ping_proc_exit(void);
#endif #endif
void __init ping_init(void); void __init ping_init(void);
int __init pingv6_init(void);
void pingv6_exit(void);
#endif /* _PING_H */ #endif /* _PING_H */

Просмотреть файл

@ -11,6 +11,7 @@ extern struct proto rawv6_prot;
extern struct proto udpv6_prot; extern struct proto udpv6_prot;
extern struct proto udplitev6_prot; extern struct proto udplitev6_prot;
extern struct proto tcpv6_prot; extern struct proto tcpv6_prot;
extern struct proto pingv6_prot;
struct flowi6; struct flowi6;
@ -21,6 +22,8 @@ extern int ipv6_frag_init(void);
extern void ipv6_frag_exit(void); extern void ipv6_frag_exit(void);
/* transport protocols */ /* transport protocols */
extern int pingv6_init(void);
extern void pingv6_exit(void);
extern int rawv6_init(void); extern int rawv6_init(void);
extern void rawv6_exit(void); extern void rawv6_exit(void);
extern int udpv6_init(void); extern int udpv6_init(void);

Просмотреть файл

@ -939,7 +939,8 @@ error:
void icmp_err(struct sk_buff *skb, u32 info) void icmp_err(struct sk_buff *skb, u32 info)
{ {
struct iphdr *iph = (struct iphdr *)skb->data; struct iphdr *iph = (struct iphdr *)skb->data;
struct icmphdr *icmph = (struct icmphdr *)(skb->data+(iph->ihl<<2)); int offset = iph->ihl<<2;
struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
int type = icmp_hdr(skb)->type; int type = icmp_hdr(skb)->type;
int code = icmp_hdr(skb)->code; int code = icmp_hdr(skb)->code;
struct net *net = dev_net(skb->dev); struct net *net = dev_net(skb->dev);
@ -949,7 +950,7 @@ void icmp_err(struct sk_buff *skb, u32 info)
* triggered by ICMP_ECHOREPLY which sent from kernel. * triggered by ICMP_ECHOREPLY which sent from kernel.
*/ */
if (icmph->type != ICMP_ECHOREPLY) { if (icmph->type != ICMP_ECHOREPLY) {
ping_err(skb, info); ping_err(skb, offset, info);
return; return;
} }

Просмотреть файл

@ -33,7 +33,6 @@
#include <linux/netdevice.h> #include <linux/netdevice.h>
#include <net/snmp.h> #include <net/snmp.h>
#include <net/ip.h> #include <net/ip.h>
#include <net/ipv6.h>
#include <net/icmp.h> #include <net/icmp.h>
#include <net/protocol.h> #include <net/protocol.h>
#include <linux/skbuff.h> #include <linux/skbuff.h>
@ -46,8 +45,18 @@
#include <net/inet_common.h> #include <net/inet_common.h>
#include <net/checksum.h> #include <net/checksum.h>
#if IS_ENABLED(CONFIG_IPV6)
#include <linux/in6.h>
#include <linux/icmpv6.h>
#include <net/addrconf.h>
#include <net/ipv6.h>
#include <net/transp_v6.h>
#endif
static struct ping_table ping_table;
struct ping_table ping_table;
struct pingv6_ops pingv6_ops;
EXPORT_SYMBOL_GPL(pingv6_ops);
static u16 ping_port_rover; static u16 ping_port_rover;
@ -58,6 +67,7 @@ static inline int ping_hashfn(struct net *net, unsigned int num, unsigned int ma
pr_debug("hash(%d) = %d\n", num, res); pr_debug("hash(%d) = %d\n", num, res);
return res; return res;
} }
EXPORT_SYMBOL_GPL(ping_hash);
static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table, static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table,
struct net *net, unsigned int num) struct net *net, unsigned int num)
@ -65,7 +75,7 @@ static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table,
return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)]; return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)];
} }
static int ping_v4_get_port(struct sock *sk, unsigned short ident) int ping_get_port(struct sock *sk, unsigned short ident)
{ {
struct hlist_nulls_node *node; struct hlist_nulls_node *node;
struct hlist_nulls_head *hlist; struct hlist_nulls_head *hlist;
@ -103,6 +113,10 @@ next_port:
ping_portaddr_for_each_entry(sk2, node, hlist) { ping_portaddr_for_each_entry(sk2, node, hlist) {
isk2 = inet_sk(sk2); isk2 = inet_sk(sk2);
/* BUG? Why is this reuse and not reuseaddr? ping.c
* doesn't turn off SO_REUSEADDR, and it doesn't expect
* that other ping processes can steal its packets.
*/
if ((isk2->inet_num == ident) && if ((isk2->inet_num == ident) &&
(sk2 != sk) && (sk2 != sk) &&
(!sk2->sk_reuse || !sk->sk_reuse)) (!sk2->sk_reuse || !sk->sk_reuse))
@ -125,17 +139,18 @@ fail:
write_unlock_bh(&ping_table.lock); write_unlock_bh(&ping_table.lock);
return 1; return 1;
} }
EXPORT_SYMBOL_GPL(ping_get_port);
static void ping_v4_hash(struct sock *sk) void ping_hash(struct sock *sk)
{ {
pr_debug("ping_v4_hash(sk->port=%u)\n", inet_sk(sk)->inet_num); pr_debug("ping_hash(sk->port=%u)\n", inet_sk(sk)->inet_num);
BUG(); /* "Please do not press this button again." */ BUG(); /* "Please do not press this button again." */
} }
static void ping_v4_unhash(struct sock *sk) void ping_unhash(struct sock *sk)
{ {
struct inet_sock *isk = inet_sk(sk); struct inet_sock *isk = inet_sk(sk);
pr_debug("ping_v4_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num); pr_debug("ping_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num);
if (sk_hashed(sk)) { if (sk_hashed(sk)) {
write_lock_bh(&ping_table.lock); write_lock_bh(&ping_table.lock);
hlist_nulls_del(&sk->sk_nulls_node); hlist_nulls_del(&sk->sk_nulls_node);
@ -146,31 +161,61 @@ static void ping_v4_unhash(struct sock *sk)
write_unlock_bh(&ping_table.lock); write_unlock_bh(&ping_table.lock);
} }
} }
EXPORT_SYMBOL_GPL(ping_unhash);
static struct sock *ping_v4_lookup(struct net *net, __be32 saddr, __be32 daddr, static struct sock *ping_lookup(struct net *net, struct sk_buff *skb, u16 ident)
u16 ident, int dif)
{ {
struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident); struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident);
struct sock *sk = NULL; struct sock *sk = NULL;
struct inet_sock *isk; struct inet_sock *isk;
struct hlist_nulls_node *hnode; struct hlist_nulls_node *hnode;
int dif = skb->dev->ifindex;
if (skb->protocol == htons(ETH_P_IP)) {
pr_debug("try to find: num = %d, daddr = %pI4, dif = %d\n", pr_debug("try to find: num = %d, daddr = %pI4, dif = %d\n",
(int)ident, &daddr, dif); (int)ident, &ip_hdr(skb)->daddr, dif);
#if IS_ENABLED(CONFIG_IPV6)
} else if (skb->protocol == htons(ETH_P_IPV6)) {
pr_debug("try to find: num = %d, daddr = %pI6c, dif = %d\n",
(int)ident, &ipv6_hdr(skb)->daddr, dif);
#endif
}
read_lock_bh(&ping_table.lock); read_lock_bh(&ping_table.lock);
ping_portaddr_for_each_entry(sk, hnode, hslot) { ping_portaddr_for_each_entry(sk, hnode, hslot) {
isk = inet_sk(sk); isk = inet_sk(sk);
pr_debug("iterate\n");
if (isk->inet_num != ident)
continue;
if (skb->protocol == htons(ETH_P_IP) &&
sk->sk_family == AF_INET) {
pr_debug("found: %p: num=%d, daddr=%pI4, dif=%d\n", sk, pr_debug("found: %p: num=%d, daddr=%pI4, dif=%d\n", sk,
(int) isk->inet_num, &isk->inet_rcv_saddr, (int) isk->inet_num, &isk->inet_rcv_saddr,
sk->sk_bound_dev_if); sk->sk_bound_dev_if);
pr_debug("iterate\n"); if (isk->inet_rcv_saddr &&
if (isk->inet_num != ident) isk->inet_rcv_saddr != ip_hdr(skb)->daddr)
continue; continue;
if (isk->inet_rcv_saddr && isk->inet_rcv_saddr != daddr) #if IS_ENABLED(CONFIG_IPV6)
} else if (skb->protocol == htons(ETH_P_IPV6) &&
sk->sk_family == AF_INET6) {
struct ipv6_pinfo *np = inet6_sk(sk);
pr_debug("found: %p: num=%d, daddr=%pI6c, dif=%d\n", sk,
(int) isk->inet_num,
&inet6_sk(sk)->rcv_saddr,
sk->sk_bound_dev_if);
if (!ipv6_addr_any(&np->rcv_saddr) &&
!ipv6_addr_equal(&np->rcv_saddr,
&ipv6_hdr(skb)->daddr))
continue; continue;
#endif
}
if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
continue; continue;
@ -200,7 +245,7 @@ static void inet_get_ping_group_range_net(struct net *net, kgid_t *low,
} }
static int ping_init_sock(struct sock *sk) int ping_init_sock(struct sock *sk)
{ {
struct net *net = sock_net(sk); struct net *net = sock_net(sk);
kgid_t group = current_egid(); kgid_t group = current_egid();
@ -225,8 +270,9 @@ static int ping_init_sock(struct sock *sk)
return -EACCES; return -EACCES;
} }
EXPORT_SYMBOL_GPL(ping_init_sock);
static void ping_close(struct sock *sk, long timeout) void ping_close(struct sock *sk, long timeout)
{ {
pr_debug("ping_close(sk=%p,sk->num=%u)\n", pr_debug("ping_close(sk=%p,sk->num=%u)\n",
inet_sk(sk), inet_sk(sk)->inet_num); inet_sk(sk), inet_sk(sk)->inet_num);
@ -234,27 +280,24 @@ static void ping_close(struct sock *sk, long timeout)
sk_common_release(sk); sk_common_release(sk);
} }
EXPORT_SYMBOL_GPL(ping_close);
/* /* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */
* We need our own bind because there are no privileged id's == local ports. int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
* Moreover, we don't allow binding to multi- and broadcast addresses. struct sockaddr *uaddr, int addr_len) {
*/ struct net *net = sock_net(sk);
if (sk->sk_family == AF_INET) {
static int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
{
struct sockaddr_in *addr = (struct sockaddr_in *) uaddr; struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
struct inet_sock *isk = inet_sk(sk);
unsigned short snum;
int chk_addr_ret; int chk_addr_ret;
int err;
if (addr_len < sizeof(struct sockaddr_in)) if (addr_len < sizeof(*addr))
return -EINVAL; return -EINVAL;
pr_debug("ping_v4_bind(sk=%p,sa_addr=%08x,sa_port=%d)\n", pr_debug("ping_check_bind_addr(sk=%p,addr=%pI4,port=%d)\n",
sk, addr->sin_addr.s_addr, ntohs(addr->sin_port)); sk, &addr->sin_addr.s_addr, ntohs(addr->sin_port));
chk_addr_ret = inet_addr_type(net, addr->sin_addr.s_addr);
chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
if (addr->sin_addr.s_addr == htonl(INADDR_ANY)) if (addr->sin_addr.s_addr == htonl(INADDR_ANY))
chk_addr_ret = RTN_LOCAL; chk_addr_ret = RTN_LOCAL;
@ -265,6 +308,95 @@ static int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
chk_addr_ret == RTN_BROADCAST) chk_addr_ret == RTN_BROADCAST)
return -EADDRNOTAVAIL; return -EADDRNOTAVAIL;
#if IS_ENABLED(CONFIG_IPV6)
} else if (sk->sk_family == AF_INET6) {
struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
int addr_type, scoped, has_addr;
struct net_device *dev = NULL;
if (addr_len < sizeof(*addr))
return -EINVAL;
pr_debug("ping_check_bind_addr(sk=%p,addr=%pI6c,port=%d)\n",
sk, addr->sin6_addr.s6_addr, ntohs(addr->sin6_port));
addr_type = ipv6_addr_type(&addr->sin6_addr);
scoped = __ipv6_addr_needs_scope_id(addr_type);
if ((addr_type != IPV6_ADDR_ANY &&
!(addr_type & IPV6_ADDR_UNICAST)) ||
(scoped && !addr->sin6_scope_id))
return -EINVAL;
rcu_read_lock();
if (addr->sin6_scope_id) {
dev = dev_get_by_index_rcu(net, addr->sin6_scope_id);
if (!dev) {
rcu_read_unlock();
return -ENODEV;
}
}
has_addr = pingv6_ops.ipv6_chk_addr(net, &addr->sin6_addr, dev,
scoped);
rcu_read_unlock();
if (!(isk->freebind || isk->transparent || has_addr ||
addr_type == IPV6_ADDR_ANY))
return -EADDRNOTAVAIL;
if (scoped)
sk->sk_bound_dev_if = addr->sin6_scope_id;
#endif
} else {
return -EAFNOSUPPORT;
}
return 0;
}
void ping_set_saddr(struct sock *sk, struct sockaddr *saddr)
{
if (saddr->sa_family == AF_INET) {
struct inet_sock *isk = inet_sk(sk);
struct sockaddr_in *addr = (struct sockaddr_in *) saddr;
isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
#if IS_ENABLED(CONFIG_IPV6)
} else if (saddr->sa_family == AF_INET6) {
struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr;
struct ipv6_pinfo *np = inet6_sk(sk);
np->rcv_saddr = np->saddr = addr->sin6_addr;
#endif
}
}
void ping_clear_saddr(struct sock *sk, int dif)
{
sk->sk_bound_dev_if = dif;
if (sk->sk_family == AF_INET) {
struct inet_sock *isk = inet_sk(sk);
isk->inet_rcv_saddr = isk->inet_saddr = 0;
#if IS_ENABLED(CONFIG_IPV6)
} else if (sk->sk_family == AF_INET6) {
struct ipv6_pinfo *np = inet6_sk(sk);
memset(&np->rcv_saddr, 0, sizeof(np->rcv_saddr));
memset(&np->saddr, 0, sizeof(np->saddr));
#endif
}
}
/*
* We need our own bind because there are no privileged id's == local ports.
* Moreover, we don't allow binding to multi- and broadcast addresses.
*/
int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
{
struct inet_sock *isk = inet_sk(sk);
unsigned short snum;
int err;
int dif = sk->sk_bound_dev_if;
err = ping_check_bind_addr(sk, isk, uaddr, addr_len);
if (err)
return err;
lock_sock(sk); lock_sock(sk);
err = -EINVAL; err = -EINVAL;
@ -272,42 +404,50 @@ static int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
goto out; goto out;
err = -EADDRINUSE; err = -EADDRINUSE;
isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr; ping_set_saddr(sk, uaddr);
snum = ntohs(addr->sin_port); snum = ntohs(((struct sockaddr_in *)uaddr)->sin_port);
if (ping_v4_get_port(sk, snum) != 0) { if (ping_get_port(sk, snum) != 0) {
isk->inet_saddr = isk->inet_rcv_saddr = 0; ping_clear_saddr(sk, dif);
goto out; goto out;
} }
pr_debug("after bind(): num = %d, daddr = %pI4, dif = %d\n", pr_debug("after bind(): num = %d, dif = %d\n",
(int)isk->inet_num, (int)isk->inet_num,
&isk->inet_rcv_saddr,
(int)sk->sk_bound_dev_if); (int)sk->sk_bound_dev_if);
err = 0; err = 0;
if (isk->inet_rcv_saddr) if ((sk->sk_family == AF_INET && isk->inet_rcv_saddr) ||
(sk->sk_family == AF_INET6 &&
!ipv6_addr_any(&inet6_sk(sk)->rcv_saddr)))
sk->sk_userlocks |= SOCK_BINDADDR_LOCK; sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
if (snum) if (snum)
sk->sk_userlocks |= SOCK_BINDPORT_LOCK; sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
isk->inet_sport = htons(isk->inet_num); isk->inet_sport = htons(isk->inet_num);
isk->inet_daddr = 0; isk->inet_daddr = 0;
isk->inet_dport = 0; isk->inet_dport = 0;
#if IS_ENABLED(CONFIG_IPV6)
if (sk->sk_family == AF_INET6)
memset(&inet6_sk(sk)->daddr, 0, sizeof(inet6_sk(sk)->daddr));
#endif
sk_dst_reset(sk); sk_dst_reset(sk);
out: out:
release_sock(sk); release_sock(sk);
pr_debug("ping_v4_bind -> %d\n", err); pr_debug("ping_v4_bind -> %d\n", err);
return err; return err;
} }
EXPORT_SYMBOL_GPL(ping_bind);
/* /*
* Is this a supported type of ICMP message? * Is this a supported type of ICMP message?
*/ */
static inline int ping_supported(int type, int code) static inline int ping_supported(int family, int type, int code)
{ {
if (type == ICMP_ECHO && code == 0) return (family == AF_INET && type == ICMP_ECHO && code == 0) ||
return 1; (family == AF_INET6 && type == ICMPV6_ECHO_REQUEST && code == 0);
return 0;
} }
/* /*
@ -315,30 +455,42 @@ static inline int ping_supported(int type, int code)
* sort of error condition. * sort of error condition.
*/ */
static int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb); void ping_err(struct sk_buff *skb, int offset, u32 info)
void ping_err(struct sk_buff *skb, u32 info)
{ {
struct iphdr *iph = (struct iphdr *)skb->data; int family;
struct icmphdr *icmph = (struct icmphdr *)(skb->data+(iph->ihl<<2)); struct icmphdr *icmph;
struct inet_sock *inet_sock; struct inet_sock *inet_sock;
int type = icmp_hdr(skb)->type; int type;
int code = icmp_hdr(skb)->code; int code;
struct net *net = dev_net(skb->dev); struct net *net = dev_net(skb->dev);
struct sock *sk; struct sock *sk;
int harderr; int harderr;
int err; int err;
if (skb->protocol == htons(ETH_P_IP)) {
family = AF_INET;
type = icmp_hdr(skb)->type;
code = icmp_hdr(skb)->code;
icmph = (struct icmphdr *)(skb->data + offset);
} else if (skb->protocol == htons(ETH_P_IPV6)) {
family = AF_INET6;
type = icmp6_hdr(skb)->icmp6_type;
code = icmp6_hdr(skb)->icmp6_code;
icmph = (struct icmphdr *) (skb->data + offset);
} else {
BUG();
}
/* We assume the packet has already been checked by icmp_unreach */ /* We assume the packet has already been checked by icmp_unreach */
if (!ping_supported(icmph->type, icmph->code)) if (!ping_supported(family, icmph->type, icmph->code))
return; return;
pr_debug("ping_err(type=%04x,code=%04x,id=%04x,seq=%04x)\n", type, pr_debug("ping_err(proto=0x%x,type=%d,code=%d,id=%04x,seq=%04x)\n",
code, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence)); skb->protocol, type, code, ntohs(icmph->un.echo.id),
ntohs(icmph->un.echo.sequence));
sk = ping_v4_lookup(net, iph->daddr, iph->saddr, sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
ntohs(icmph->un.echo.id), skb->dev->ifindex);
if (sk == NULL) { if (sk == NULL) {
pr_debug("no socket, dropping\n"); pr_debug("no socket, dropping\n");
return; /* No socket for error */ return; /* No socket for error */
@ -349,6 +501,7 @@ void ping_err(struct sk_buff *skb, u32 info)
harderr = 0; harderr = 0;
inet_sock = inet_sk(sk); inet_sock = inet_sk(sk);
if (skb->protocol == htons(ETH_P_IP)) {
switch (type) { switch (type) {
default: default:
case ICMP_TIME_EXCEEDED: case ICMP_TIME_EXCEEDED:
@ -356,7 +509,8 @@ void ping_err(struct sk_buff *skb, u32 info)
break; break;
case ICMP_SOURCE_QUENCH: case ICMP_SOURCE_QUENCH:
/* This is not a real error but ping wants to see it. /* This is not a real error but ping wants to see it.
* Report it with some fake errno. */ * Report it with some fake errno.
*/
err = EREMOTEIO; err = EREMOTEIO;
break; break;
case ICMP_PARAMETERPROB: case ICMP_PARAMETERPROB:
@ -385,35 +539,44 @@ void ping_err(struct sk_buff *skb, u32 info)
err = EREMOTEIO; err = EREMOTEIO;
break; break;
} }
#if IS_ENABLED(CONFIG_IPV6)
} else if (skb->protocol == htons(ETH_P_IPV6)) {
harderr = pingv6_ops.icmpv6_err_convert(type, code, &err);
#endif
}
/* /*
* RFC1122: OK. Passes ICMP errors back to application, as per * RFC1122: OK. Passes ICMP errors back to application, as per
* 4.1.3.3. * 4.1.3.3.
*/ */
if (!inet_sock->recverr) { if ((family == AF_INET && !inet_sock->recverr) ||
(family == AF_INET6 && !inet6_sk(sk)->recverr)) {
if (!harderr || sk->sk_state != TCP_ESTABLISHED) if (!harderr || sk->sk_state != TCP_ESTABLISHED)
goto out; goto out;
} else { } else {
if (family == AF_INET) {
ip_icmp_error(sk, skb, err, 0 /* no remote port */, ip_icmp_error(sk, skb, err, 0 /* no remote port */,
info, (u8 *)icmph); info, (u8 *)icmph);
#if IS_ENABLED(CONFIG_IPV6)
} else if (family == AF_INET6) {
pingv6_ops.ipv6_icmp_error(sk, skb, err, 0,
info, (u8 *)icmph);
#endif
}
} }
sk->sk_err = err; sk->sk_err = err;
sk->sk_error_report(sk); sk->sk_error_report(sk);
out: out:
sock_put(sk); sock_put(sk);
} }
EXPORT_SYMBOL_GPL(ping_err);
/* /*
* Copy and checksum an ICMP Echo packet from user space into a buffer. * Copy and checksum an ICMP Echo packet from user space into a buffer
* starting from the payload.
*/ */
struct pingfakehdr { int ping_getfrag(void *from, char *to,
struct icmphdr icmph;
struct iovec *iov;
__wsum wcheck;
};
static int ping_getfrag(void *from, char *to,
int offset, int fraglen, int odd, struct sk_buff *skb) int offset, int fraglen, int odd, struct sk_buff *skb)
{ {
struct pingfakehdr *pfh = (struct pingfakehdr *)from; struct pingfakehdr *pfh = (struct pingfakehdr *)from;
@ -425,19 +588,32 @@ static int ping_getfrag(void *from, char *to,
pfh->iov, 0, fraglen - sizeof(struct icmphdr), pfh->iov, 0, fraglen - sizeof(struct icmphdr),
&pfh->wcheck)) &pfh->wcheck))
return -EFAULT; return -EFAULT;
} else if (offset < sizeof(struct icmphdr)) {
return 0;
}
if (offset < sizeof(struct icmphdr))
BUG(); BUG();
} else {
if (csum_partial_copy_fromiovecend if (csum_partial_copy_fromiovecend
(to, pfh->iov, offset - sizeof(struct icmphdr), (to, pfh->iov, offset - sizeof(struct icmphdr),
fraglen, &pfh->wcheck)) fraglen, &pfh->wcheck))
return -EFAULT; return -EFAULT;
return 0;
} }
static int ping_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh, #if IS_ENABLED(CONFIG_IPV6)
/* For IPv6, checksum each skb as we go along, as expected by
* icmpv6_push_pending_frames. For IPv4, accumulate the checksum in
* wcheck, it will be finalized in ping_v4_push_pending_frames.
*/
if (pfh->family == AF_INET6) {
skb->csum = pfh->wcheck;
skb->ip_summed = CHECKSUM_NONE;
pfh->wcheck = 0;
}
#endif
return 0;
}
EXPORT_SYMBOL_GPL(ping_getfrag);
static int ping_v4_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh,
struct flowi4 *fl4) struct flowi4 *fl4)
{ {
struct sk_buff *skb = skb_peek(&sk->sk_write_queue); struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
@ -450,7 +626,48 @@ static int ping_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh,
return ip_push_pending_frames(sk, fl4); return ip_push_pending_frames(sk, fl4);
} }
static int ping_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
void *user_icmph, size_t icmph_len) {
u8 type, code;
if (len > 0xFFFF)
return -EMSGSIZE;
/*
* Check the flags.
*/
/* Mirror BSD error message compatibility */
if (msg->msg_flags & MSG_OOB)
return -EOPNOTSUPP;
/*
* Fetch the ICMP header provided by the userland.
* iovec is modified! The ICMP header is consumed.
*/
if (memcpy_fromiovec(user_icmph, msg->msg_iov, icmph_len))
return -EFAULT;
if (family == AF_INET) {
type = ((struct icmphdr *) user_icmph)->type;
code = ((struct icmphdr *) user_icmph)->code;
#if IS_ENABLED(CONFIG_IPV6)
} else if (family == AF_INET6) {
type = ((struct icmp6hdr *) user_icmph)->icmp6_type;
code = ((struct icmp6hdr *) user_icmph)->icmp6_code;
#endif
} else {
BUG();
}
if (!ping_supported(family, type, code))
return -EINVAL;
return 0;
}
EXPORT_SYMBOL_GPL(ping_common_sendmsg);
int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
size_t len) size_t len)
{ {
struct net *net = sock_net(sk); struct net *net = sock_net(sk);
@ -466,30 +683,12 @@ static int ping_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
u8 tos; u8 tos;
int err; int err;
pr_debug("ping_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num); pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph,
if (len > 0xFFFF) sizeof(user_icmph));
return -EMSGSIZE; if (err)
return err;
/*
* Check the flags.
*/
/* Mirror BSD error message compatibility */
if (msg->msg_flags & MSG_OOB)
return -EOPNOTSUPP;
/*
* Fetch the ICMP header provided by the userland.
* iovec is modified!
*/
if (memcpy_fromiovec((u8 *)&user_icmph, msg->msg_iov,
sizeof(struct icmphdr)))
return -EFAULT;
if (!ping_supported(user_icmph.type, user_icmph.code))
return -EINVAL;
/* /*
* Get and verify the address. * Get and verify the address.
@ -595,13 +794,14 @@ back_from_confirm:
pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence; pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
pfh.iov = msg->msg_iov; pfh.iov = msg->msg_iov;
pfh.wcheck = 0; pfh.wcheck = 0;
pfh.family = AF_INET;
err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len, err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
0, &ipc, &rt, msg->msg_flags); 0, &ipc, &rt, msg->msg_flags);
if (err) if (err)
ip_flush_pending_frames(sk); ip_flush_pending_frames(sk);
else else
err = ping_push_pending_frames(sk, &pfh, &fl4); err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
release_sock(sk); release_sock(sk);
out: out:
@ -622,11 +822,13 @@ do_confirm:
goto out; goto out;
} }
static int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
size_t len, int noblock, int flags, int *addr_len) size_t len, int noblock, int flags, int *addr_len)
{ {
struct inet_sock *isk = inet_sk(sk); struct inet_sock *isk = inet_sk(sk);
struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; int family = sk->sk_family;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
struct sk_buff *skb; struct sk_buff *skb;
int copied, err; int copied, err;
@ -636,11 +838,22 @@ static int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
if (flags & MSG_OOB) if (flags & MSG_OOB)
goto out; goto out;
if (addr_len) if (addr_len) {
if (family == AF_INET)
*addr_len = sizeof(*sin); *addr_len = sizeof(*sin);
else if (family == AF_INET6 && addr_len)
*addr_len = sizeof(*sin6);
}
if (flags & MSG_ERRQUEUE) if (flags & MSG_ERRQUEUE) {
if (family == AF_INET) {
return ip_recv_error(sk, msg, len); return ip_recv_error(sk, msg, len);
#if IS_ENABLED(CONFIG_IPV6)
} else if (family == AF_INET6) {
return pingv6_ops.ipv6_recv_error(sk, msg, len);
#endif
}
}
skb = skb_recv_datagram(sk, flags, noblock, &err); skb = skb_recv_datagram(sk, flags, noblock, &err);
if (!skb) if (!skb)
@ -659,15 +872,40 @@ static int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
sock_recv_timestamp(msg, sk, skb); sock_recv_timestamp(msg, sk, skb);
/* Copy the address. */ /* Copy the address and add cmsg data. */
if (sin) { if (family == AF_INET) {
sin = (struct sockaddr_in *) msg->msg_name;
sin->sin_family = AF_INET; sin->sin_family = AF_INET;
sin->sin_port = 0 /* skb->h.uh->source */; sin->sin_port = 0 /* skb->h.uh->source */;
sin->sin_addr.s_addr = ip_hdr(skb)->saddr; sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
}
if (isk->cmsg_flags) if (isk->cmsg_flags)
ip_cmsg_recv(msg, skb); ip_cmsg_recv(msg, skb);
#if IS_ENABLED(CONFIG_IPV6)
} else if (family == AF_INET6) {
struct ipv6_pinfo *np = inet6_sk(sk);
struct ipv6hdr *ip6 = ipv6_hdr(skb);
sin6 = (struct sockaddr_in6 *) msg->msg_name;
sin6->sin6_family = AF_INET6;
sin6->sin6_port = 0;
sin6->sin6_addr = ip6->saddr;
if (np->sndflow)
sin6->sin6_flowinfo = ip6_flowinfo(ip6);
if (__ipv6_addr_needs_scope_id(
ipv6_addr_type(&sin6->sin6_addr)))
sin6->sin6_scope_id = IP6CB(skb)->iif;
if (inet6_sk(sk)->rxopt.all)
pingv6_ops.ip6_datagram_recv_ctl(sk, msg, skb);
#endif
} else {
BUG();
}
err = copied; err = copied;
done: done:
@ -676,8 +914,9 @@ out:
pr_debug("ping_recvmsg -> %d\n", err); pr_debug("ping_recvmsg -> %d\n", err);
return err; return err;
} }
EXPORT_SYMBOL_GPL(ping_recvmsg);
static int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
{ {
pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n", pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
inet_sk(sk), inet_sk(sk)->inet_num, skb); inet_sk(sk), inet_sk(sk)->inet_num, skb);
@ -688,6 +927,7 @@ static int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
} }
return 0; return 0;
} }
EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
/* /*
@ -698,10 +938,7 @@ void ping_rcv(struct sk_buff *skb)
{ {
struct sock *sk; struct sock *sk;
struct net *net = dev_net(skb->dev); struct net *net = dev_net(skb->dev);
struct iphdr *iph = ip_hdr(skb);
struct icmphdr *icmph = icmp_hdr(skb); struct icmphdr *icmph = icmp_hdr(skb);
__be32 saddr = iph->saddr;
__be32 daddr = iph->daddr;
/* We assume the packet has already been checked by icmp_rcv */ /* We assume the packet has already been checked by icmp_rcv */
@ -711,8 +948,7 @@ void ping_rcv(struct sk_buff *skb)
/* Push ICMP header back */ /* Push ICMP header back */
skb_push(skb, skb->data - (u8 *)icmph); skb_push(skb, skb->data - (u8 *)icmph);
sk = ping_v4_lookup(net, saddr, daddr, ntohs(icmph->un.echo.id), sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
skb->dev->ifindex);
if (sk != NULL) { if (sk != NULL) {
pr_debug("rcv on socket %p\n", sk); pr_debug("rcv on socket %p\n", sk);
ping_queue_rcv_skb(sk, skb_get(skb)); ping_queue_rcv_skb(sk, skb_get(skb));
@ -723,6 +959,7 @@ void ping_rcv(struct sk_buff *skb)
/* We're called from icmp_rcv(). kfree_skb() is done there. */ /* We're called from icmp_rcv(). kfree_skb() is done there. */
} }
EXPORT_SYMBOL_GPL(ping_rcv);
struct proto ping_prot = { struct proto ping_prot = {
.name = "PING", .name = "PING",
@ -733,14 +970,14 @@ struct proto ping_prot = {
.disconnect = udp_disconnect, .disconnect = udp_disconnect,
.setsockopt = ip_setsockopt, .setsockopt = ip_setsockopt,
.getsockopt = ip_getsockopt, .getsockopt = ip_getsockopt,
.sendmsg = ping_sendmsg, .sendmsg = ping_v4_sendmsg,
.recvmsg = ping_recvmsg, .recvmsg = ping_recvmsg,
.bind = ping_bind, .bind = ping_bind,
.backlog_rcv = ping_queue_rcv_skb, .backlog_rcv = ping_queue_rcv_skb,
.release_cb = ip4_datagram_release_cb, .release_cb = ip4_datagram_release_cb,
.hash = ping_v4_hash, .hash = ping_hash,
.unhash = ping_v4_unhash, .unhash = ping_unhash,
.get_port = ping_v4_get_port, .get_port = ping_get_port,
.obj_size = sizeof(struct inet_sock), .obj_size = sizeof(struct inet_sock),
}; };
EXPORT_SYMBOL(ping_prot); EXPORT_SYMBOL(ping_prot);

Просмотреть файл

@ -7,7 +7,7 @@ obj-$(CONFIG_IPV6) += ipv6.o
ipv6-objs := af_inet6.o anycast.o ip6_output.o ip6_input.o addrconf.o \ ipv6-objs := af_inet6.o anycast.o ip6_output.o ip6_input.o addrconf.o \
addrlabel.o \ addrlabel.o \
route.o ip6_fib.o ipv6_sockglue.o ndisc.o udp.o udplite.o \ route.o ip6_fib.o ipv6_sockglue.o ndisc.o udp.o udplite.o \
raw.o icmp.o mcast.o reassembly.o tcp_ipv6.o \ raw.o icmp.o mcast.o reassembly.o tcp_ipv6.o ping.o \
exthdrs.o datagram.o ip6_flowlabel.o inet6_connection_sock.o exthdrs.o datagram.o ip6_flowlabel.o inet6_connection_sock.o
ipv6-offload := ip6_offload.o tcpv6_offload.o udp_offload.o exthdrs_offload.o ipv6-offload := ip6_offload.o tcpv6_offload.o udp_offload.o exthdrs_offload.o

Просмотреть файл

@ -49,6 +49,7 @@
#include <net/udp.h> #include <net/udp.h>
#include <net/udplite.h> #include <net/udplite.h>
#include <net/tcp.h> #include <net/tcp.h>
#include <net/ping.h>
#include <net/protocol.h> #include <net/protocol.h>
#include <net/inet_common.h> #include <net/inet_common.h>
#include <net/route.h> #include <net/route.h>
@ -840,6 +841,9 @@ static int __init inet6_init(void)
if (err) if (err)
goto out_unregister_udplite_proto; goto out_unregister_udplite_proto;
err = proto_register(&pingv6_prot, 1);
if (err)
goto out_unregister_ping_proto;
/* We MUST register RAW sockets before we create the ICMP6, /* We MUST register RAW sockets before we create the ICMP6,
* IGMP6, or NDISC control sockets. * IGMP6, or NDISC control sockets.
@ -930,6 +934,10 @@ static int __init inet6_init(void)
if (err) if (err)
goto ipv6_packet_fail; goto ipv6_packet_fail;
err = pingv6_init();
if (err)
goto pingv6_fail;
#ifdef CONFIG_SYSCTL #ifdef CONFIG_SYSCTL
err = ipv6_sysctl_register(); err = ipv6_sysctl_register();
if (err) if (err)
@ -942,6 +950,8 @@ out:
sysctl_fail: sysctl_fail:
ipv6_packet_cleanup(); ipv6_packet_cleanup();
#endif #endif
pingv6_fail:
pingv6_exit();
ipv6_packet_fail: ipv6_packet_fail:
tcpv6_exit(); tcpv6_exit();
tcpv6_fail: tcpv6_fail:
@ -985,6 +995,8 @@ register_pernet_fail:
rtnl_unregister_all(PF_INET6); rtnl_unregister_all(PF_INET6);
out_sock_register_fail: out_sock_register_fail:
rawv6_exit(); rawv6_exit();
out_unregister_ping_proto:
proto_unregister(&pingv6_prot);
out_unregister_raw_proto: out_unregister_raw_proto:
proto_unregister(&rawv6_prot); proto_unregister(&rawv6_prot);
out_unregister_udplite_proto: out_unregister_udplite_proto:

Просмотреть файл

@ -57,6 +57,7 @@
#include <net/ipv6.h> #include <net/ipv6.h>
#include <net/ip6_checksum.h> #include <net/ip6_checksum.h>
#include <net/ping.h>
#include <net/protocol.h> #include <net/protocol.h>
#include <net/raw.h> #include <net/raw.h>
#include <net/rawv6.h> #include <net/rawv6.h>
@ -84,12 +85,18 @@ static inline struct sock *icmpv6_sk(struct net *net)
static void icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, static void icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
u8 type, u8 code, int offset, __be32 info) u8 type, u8 code, int offset, __be32 info)
{ {
/* icmpv6_notify checks 8 bytes can be pulled, icmp6hdr is 8 bytes */
struct icmp6hdr *icmp6 = (struct icmp6hdr *) (skb->data + offset);
struct net *net = dev_net(skb->dev); struct net *net = dev_net(skb->dev);
if (type == ICMPV6_PKT_TOOBIG) if (type == ICMPV6_PKT_TOOBIG)
ip6_update_pmtu(skb, net, info, 0, 0); ip6_update_pmtu(skb, net, info, 0, 0);
else if (type == NDISC_REDIRECT) else if (type == NDISC_REDIRECT)
ip6_redirect(skb, net, 0, 0); ip6_redirect(skb, net, 0, 0);
if (!(type & ICMPV6_INFOMSG_MASK))
if (icmp6->icmp6_type == ICMPV6_ECHO_REQUEST)
ping_err(skb, offset, info);
} }
static int icmpv6_rcv(struct sk_buff *skb); static int icmpv6_rcv(struct sk_buff *skb);
@ -224,7 +231,8 @@ static bool opt_unrec(struct sk_buff *skb, __u32 offset)
return (*op & 0xC0) == 0x80; return (*op & 0xC0) == 0x80;
} }
static int icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6, struct icmp6hdr *thdr, int len) int icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
struct icmp6hdr *thdr, int len)
{ {
struct sk_buff *skb; struct sk_buff *skb;
struct icmp6hdr *icmp6h; struct icmp6hdr *icmp6h;
@ -307,7 +315,7 @@ static void mip6_addr_swap(struct sk_buff *skb)
static inline void mip6_addr_swap(struct sk_buff *skb) {} static inline void mip6_addr_swap(struct sk_buff *skb) {}
#endif #endif
static struct dst_entry *icmpv6_route_lookup(struct net *net, struct sk_buff *skb, struct dst_entry *icmpv6_route_lookup(struct net *net, struct sk_buff *skb,
struct sock *sk, struct flowi6 *fl6) struct sock *sk, struct flowi6 *fl6)
{ {
struct dst_entry *dst, *dst2; struct dst_entry *dst, *dst2;
@ -697,7 +705,8 @@ static int icmpv6_rcv(struct sk_buff *skb)
skb->csum = ~csum_unfold(csum_ipv6_magic(saddr, daddr, skb->len, skb->csum = ~csum_unfold(csum_ipv6_magic(saddr, daddr, skb->len,
IPPROTO_ICMPV6, 0)); IPPROTO_ICMPV6, 0));
if (__skb_checksum_complete(skb)) { if (__skb_checksum_complete(skb)) {
LIMIT_NETDEBUG(KERN_DEBUG "ICMPv6 checksum failed [%pI6 > %pI6]\n", LIMIT_NETDEBUG(KERN_DEBUG
"ICMPv6 checksum failed [%pI6c > %pI6c]\n",
saddr, daddr); saddr, daddr);
goto csum_error; goto csum_error;
} }
@ -718,7 +727,7 @@ static int icmpv6_rcv(struct sk_buff *skb)
break; break;
case ICMPV6_ECHO_REPLY: case ICMPV6_ECHO_REPLY:
/* we couldn't care less */ ping_rcv(skb);
break; break;
case ICMPV6_PKT_TOOBIG: case ICMPV6_PKT_TOOBIG:

216
net/ipv6/ping.c Normal file
Просмотреть файл

@ -0,0 +1,216 @@
/*
* INET An implementation of the TCP/IP protocol suite for the LINUX
* operating system. INET is implemented using the BSD Socket
* interface as the means of communication with the user level.
*
* "Ping" sockets
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Based on ipv4/ping.c code.
*
* Authors: Lorenzo Colitti (IPv6 support)
* Vasiliy Kulikov / Openwall (IPv4 implementation, for Linux 2.6),
* Pavel Kankovsky (IPv4 implementation, for Linux 2.4.32)
*
*/
#include <net/addrconf.h>
#include <net/ipv6.h>
#include <net/ip6_route.h>
#include <net/protocol.h>
#include <net/udp.h>
#include <net/transp_v6.h>
#include <net/ping.h>
struct proto pingv6_prot = {
.name = "PINGv6",
.owner = THIS_MODULE,
.init = ping_init_sock,
.close = ping_close,
.connect = ip6_datagram_connect,
.disconnect = udp_disconnect,
.setsockopt = ipv6_setsockopt,
.getsockopt = ipv6_getsockopt,
.sendmsg = ping_v6_sendmsg,
.recvmsg = ping_recvmsg,
.bind = ping_bind,
.backlog_rcv = ping_queue_rcv_skb,
.hash = ping_hash,
.unhash = ping_unhash,
.get_port = ping_get_port,
.obj_size = sizeof(struct raw6_sock),
};
EXPORT_SYMBOL_GPL(pingv6_prot);
static struct inet_protosw pingv6_protosw = {
.type = SOCK_DGRAM,
.protocol = IPPROTO_ICMPV6,
.prot = &pingv6_prot,
.ops = &inet6_dgram_ops,
.no_check = UDP_CSUM_DEFAULT,
.flags = INET_PROTOSW_REUSE,
};
/* Compatibility glue so we can support IPv6 when it's compiled as a module */
int dummy_ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
{
return -EAFNOSUPPORT;
}
int dummy_ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
struct sk_buff *skb)
{
return -EAFNOSUPPORT;
}
int dummy_icmpv6_err_convert(u8 type, u8 code, int *err)
{
return -EAFNOSUPPORT;
}
void dummy_ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
__be16 port, u32 info, u8 *payload) {}
int dummy_ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
struct net_device *dev, int strict)
{
return 0;
}
int __init pingv6_init(void)
{
pingv6_ops.ipv6_recv_error = ipv6_recv_error;
pingv6_ops.ip6_datagram_recv_ctl = ip6_datagram_recv_ctl;
pingv6_ops.icmpv6_err_convert = icmpv6_err_convert;
pingv6_ops.ipv6_icmp_error = ipv6_icmp_error;
pingv6_ops.ipv6_chk_addr = ipv6_chk_addr;
return inet6_register_protosw(&pingv6_protosw);
}
/* This never gets called because it's not possible to unload the ipv6 module,
* but just in case.
*/
void pingv6_exit(void)
{
pingv6_ops.ipv6_recv_error = dummy_ipv6_recv_error;
pingv6_ops.ip6_datagram_recv_ctl = dummy_ip6_datagram_recv_ctl;
pingv6_ops.icmpv6_err_convert = dummy_icmpv6_err_convert;
pingv6_ops.ipv6_icmp_error = dummy_ipv6_icmp_error;
pingv6_ops.ipv6_chk_addr = dummy_ipv6_chk_addr;
inet6_unregister_protosw(&pingv6_protosw);
}
int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
size_t len)
{
struct inet_sock *inet = inet_sk(sk);
struct ipv6_pinfo *np = inet6_sk(sk);
struct icmp6hdr user_icmph;
int addr_type;
struct in6_addr *daddr;
int iif = 0;
struct flowi6 fl6;
int err;
int hlimit;
struct dst_entry *dst;
struct rt6_info *rt;
struct pingfakehdr pfh;
pr_debug("ping_v6_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
err = ping_common_sendmsg(AF_INET6, msg, len, &user_icmph,
sizeof(user_icmph));
if (err)
return err;
if (msg->msg_name) {
struct sockaddr_in6 *u = (struct sockaddr_in6 *) msg->msg_name;
if (msg->msg_namelen < sizeof(struct sockaddr_in6) ||
u->sin6_family != AF_INET6) {
return -EINVAL;
}
if (sk->sk_bound_dev_if &&
sk->sk_bound_dev_if != u->sin6_scope_id) {
return -EINVAL;
}
daddr = &(u->sin6_addr);
iif = u->sin6_scope_id;
} else {
if (sk->sk_state != TCP_ESTABLISHED)
return -EDESTADDRREQ;
daddr = &np->daddr;
}
if (!iif)
iif = sk->sk_bound_dev_if;
addr_type = ipv6_addr_type(daddr);
if (__ipv6_addr_needs_scope_id(addr_type) && !iif)
return -EINVAL;
if (addr_type & IPV6_ADDR_MAPPED)
return -EINVAL;
/* TODO: use ip6_datagram_send_ctl to get options from cmsg */
memset(&fl6, 0, sizeof(fl6));
fl6.flowi6_proto = IPPROTO_ICMPV6;
fl6.saddr = np->saddr;
fl6.daddr = *daddr;
fl6.fl6_icmp_type = user_icmph.icmp6_type;
fl6.fl6_icmp_code = user_icmph.icmp6_code;
security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
fl6.flowi6_oif = np->mcast_oif;
else if (!fl6.flowi6_oif)
fl6.flowi6_oif = np->ucast_oif;
dst = ip6_sk_dst_lookup_flow(sk, &fl6, daddr, 1);
if (IS_ERR(dst))
return PTR_ERR(dst);
rt = (struct rt6_info *) dst;
np = inet6_sk(sk);
if (!np)
return -EBADF;
if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
fl6.flowi6_oif = np->mcast_oif;
else if (!fl6.flowi6_oif)
fl6.flowi6_oif = np->ucast_oif;
pfh.icmph.type = user_icmph.icmp6_type;
pfh.icmph.code = user_icmph.icmp6_code;
pfh.icmph.checksum = 0;
pfh.icmph.un.echo.id = inet->inet_sport;
pfh.icmph.un.echo.sequence = user_icmph.icmp6_sequence;
pfh.iov = msg->msg_iov;
pfh.wcheck = 0;
pfh.family = AF_INET6;
if (ipv6_addr_is_multicast(&fl6.daddr))
hlimit = np->mcast_hops;
else
hlimit = np->hop_limit;
if (hlimit < 0)
hlimit = ip6_dst_hoplimit(dst);
err = ip6_append_data(sk, ping_getfrag, &pfh, len,
0, hlimit,
np->tclass, NULL, &fl6, rt,
MSG_DONTWAIT, np->dontfrag);
if (err) {
ICMP6_INC_STATS_BH(sock_net(sk), rt->rt6i_idev,
ICMP6_MIB_OUTERRORS);
ip6_flush_pending_frames(sk);
} else {
err = icmpv6_push_pending_frames(sk, &fl6,
(struct icmp6hdr *) &pfh.icmph,
len);
}
return err;
}