Commit 4b261c75 authored by Hannes Frederic Sowa's avatar Hannes Frederic Sowa Committed by David S. Miller

ipv6: make IPV6_RECVPKTINFO work for ipv4 datagrams

We currently don't report IPV6_RECVPKTINFO in cmsg access ancillary data
for IPv4 datagrams on IPv6 sockets.

This patch splits the ip6_datagram_recv_ctl into two functions, one
which handles both protocol families, AF_INET and AF_INET6, while the
ip6_datagram_recv_specific_ctl only handles IPv6 cmsg data.

ip6_datagram_recv_*_ctl never reported back any errors, so we can make
them return void. Also provide a helper for protocols which don't offer dual
personality to further use ip6_datagram_recv_ctl, which is exported to
modules.

I needed to shuffle the code for ping around a bit to make it easier to
implement dual personality for ping ipv6 sockets in future.
Reported-by: default avatarGert Doering <gert@space.net>
Signed-off-by: default avatarHannes Frederic Sowa <hannes@stressinduktion.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent a6e2fe17
...@@ -284,6 +284,8 @@ static inline void inet_sk_copy_descendant(struct sock *sk_to, ...@@ -284,6 +284,8 @@ static inline void inet_sk_copy_descendant(struct sock *sk_to,
#define __ipv6_only_sock(sk) (inet6_sk(sk)->ipv6only) #define __ipv6_only_sock(sk) (inet6_sk(sk)->ipv6only)
#define ipv6_only_sock(sk) ((sk)->sk_family == PF_INET6 && __ipv6_only_sock(sk)) #define ipv6_only_sock(sk) ((sk)->sk_family == PF_INET6 && __ipv6_only_sock(sk))
#define ipv6_sk_rxinfo(sk) ((sk)->sk_family == PF_INET6 && \
inet6_sk(sk)->rxopt.bits.rxinfo)
static inline const struct in6_addr *inet6_rcv_saddr(const struct sock *sk) static inline const struct in6_addr *inet6_rcv_saddr(const struct sock *sk)
{ {
...@@ -300,6 +302,7 @@ static inline int inet_v6_ipv6only(const struct sock *sk) ...@@ -300,6 +302,7 @@ static inline int inet_v6_ipv6only(const struct sock *sk)
#else #else
#define __ipv6_only_sock(sk) 0 #define __ipv6_only_sock(sk) 0
#define ipv6_only_sock(sk) 0 #define ipv6_only_sock(sk) 0
#define ipv6_sk_rxinfo(sk) 0
static inline struct ipv6_pinfo * inet6_sk(const struct sock *__sk) static inline struct ipv6_pinfo * inet6_sk(const struct sock *__sk)
{ {
......
...@@ -63,6 +63,7 @@ struct ipcm_cookie { ...@@ -63,6 +63,7 @@ struct ipcm_cookie {
}; };
#define IPCB(skb) ((struct inet_skb_parm*)((skb)->cb)) #define IPCB(skb) ((struct inet_skb_parm*)((skb)->cb))
#define PKTINFO_SKB_CB(skb) ((struct in_pktinfo *)((skb)->cb))
struct ip_ra_chain { struct ip_ra_chain {
struct ip_ra_chain __rcu *next; struct ip_ra_chain __rcu *next;
......
...@@ -33,8 +33,12 @@ ...@@ -33,8 +33,12 @@
struct pingv6_ops { struct pingv6_ops {
int (*ipv6_recv_error)(struct sock *sk, struct msghdr *msg, int len, int (*ipv6_recv_error)(struct sock *sk, struct msghdr *msg, int len,
int *addr_len); int *addr_len);
int (*ip6_datagram_recv_ctl)(struct sock *sk, struct msghdr *msg, void (*ip6_datagram_recv_common_ctl)(struct sock *sk,
struct sk_buff *skb); struct msghdr *msg,
struct sk_buff *skb);
void (*ip6_datagram_recv_specific_ctl)(struct sock *sk,
struct msghdr *msg,
struct sk_buff *skb);
int (*icmpv6_err_convert)(u8 type, u8 code, int *err); int (*icmpv6_err_convert)(u8 type, u8 code, int *err);
void (*ipv6_icmp_error)(struct sock *sk, struct sk_buff *skb, int err, void (*ipv6_icmp_error)(struct sock *sk, struct sk_buff *skb, int err,
__be16 port, u32 info, u8 *payload); __be16 port, u32 info, u8 *payload);
......
...@@ -32,8 +32,13 @@ void tcpv6_exit(void); ...@@ -32,8 +32,13 @@ void tcpv6_exit(void);
int udpv6_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len); int udpv6_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
int ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg, /* this does all the common and the specific ctl work */
struct sk_buff *skb); void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
struct sk_buff *skb);
void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
struct sk_buff *skb);
void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
struct sk_buff *skb);
int ip6_datagram_send_ctl(struct net *net, struct sock *sk, struct msghdr *msg, int ip6_datagram_send_ctl(struct net *net, struct sock *sk, struct msghdr *msg,
struct flowi6 *fl6, struct ipv6_txoptions *opt, struct flowi6 *fl6, struct ipv6_txoptions *opt,
......
...@@ -56,7 +56,6 @@ ...@@ -56,7 +56,6 @@
/* /*
* SOL_IP control messages. * SOL_IP control messages.
*/ */
#define PKTINFO_SKB_CB(__skb) ((struct in_pktinfo *)((__skb)->cb))
static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
{ {
...@@ -1055,9 +1054,10 @@ static int do_ip_setsockopt(struct sock *sk, int level, ...@@ -1055,9 +1054,10 @@ static int do_ip_setsockopt(struct sock *sk, int level,
void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb) void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
{ {
struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb); struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
ipv6_sk_rxinfo(sk);
if ((inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) && if (prepare && skb_rtable(skb)) {
skb_rtable(skb)) {
pktinfo->ipi_ifindex = inet_iif(skb); pktinfo->ipi_ifindex = inet_iif(skb);
pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb); pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
} else { } else {
......
...@@ -906,7 +906,12 @@ int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, ...@@ -906,7 +906,12 @@ int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
} }
if (inet6_sk(sk)->rxopt.all) if (inet6_sk(sk)->rxopt.all)
pingv6_ops.ip6_datagram_recv_ctl(sk, msg, skb); pingv6_ops.ip6_datagram_recv_common_ctl(sk, msg, skb);
if (skb->protocol == htons(ETH_P_IPV6) &&
inet6_sk(sk)->rxopt.all)
pingv6_ops.ip6_datagram_recv_specific_ctl(sk, msg, skb);
else if (skb->protocol == htons(ETH_P_IP) && isk->cmsg_flags)
ip_cmsg_recv(msg, skb);
#endif #endif
} else { } else {
BUG(); BUG();
......
...@@ -377,10 +377,12 @@ int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len) ...@@ -377,10 +377,12 @@ int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
sin->sin6_family = AF_INET6; sin->sin6_family = AF_INET6;
sin->sin6_flowinfo = 0; sin->sin6_flowinfo = 0;
sin->sin6_port = 0; sin->sin6_port = 0;
if (np->rxopt.all)
ip6_datagram_recv_common_ctl(sk, msg, skb);
if (skb->protocol == htons(ETH_P_IPV6)) { if (skb->protocol == htons(ETH_P_IPV6)) {
sin->sin6_addr = ipv6_hdr(skb)->saddr; sin->sin6_addr = ipv6_hdr(skb)->saddr;
if (np->rxopt.all) if (np->rxopt.all)
ip6_datagram_recv_ctl(sk, msg, skb); ip6_datagram_recv_specific_ctl(sk, msg, skb);
sin->sin6_scope_id = sin->sin6_scope_id =
ipv6_iface_scope_id(&sin->sin6_addr, ipv6_iface_scope_id(&sin->sin6_addr,
IP6CB(skb)->iif); IP6CB(skb)->iif);
...@@ -471,20 +473,34 @@ int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len, ...@@ -471,20 +473,34 @@ int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
} }
int ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg, void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
struct sk_buff *skb) struct sk_buff *skb)
{ {
struct ipv6_pinfo *np = inet6_sk(sk); struct ipv6_pinfo *np = inet6_sk(sk);
struct inet6_skb_parm *opt = IP6CB(skb); bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
unsigned char *nh = skb_network_header(skb);
if (np->rxopt.bits.rxinfo) { if (np->rxopt.bits.rxinfo) {
struct in6_pktinfo src_info; struct in6_pktinfo src_info;
src_info.ipi6_ifindex = opt->iif; if (is_ipv6) {
src_info.ipi6_addr = ipv6_hdr(skb)->daddr; src_info.ipi6_ifindex = IP6CB(skb)->iif;
src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
} else {
src_info.ipi6_ifindex =
PKTINFO_SKB_CB(skb)->ipi_ifindex;
ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
&src_info.ipi6_addr);
}
put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
} }
}
void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
struct sk_buff *skb)
{
struct ipv6_pinfo *np = inet6_sk(sk);
struct inet6_skb_parm *opt = IP6CB(skb);
unsigned char *nh = skb_network_header(skb);
if (np->rxopt.bits.rxhlim) { if (np->rxopt.bits.rxhlim) {
int hlim = ipv6_hdr(skb)->hop_limit; int hlim = ipv6_hdr(skb)->hop_limit;
...@@ -602,7 +618,13 @@ int ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg, ...@@ -602,7 +618,13 @@ int ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6); put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
} }
} }
return 0; }
void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
struct sk_buff *skb)
{
ip6_datagram_recv_common_ctl(sk, msg, skb);
ip6_datagram_recv_specific_ctl(sk, msg, skb);
} }
EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl); EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
......
...@@ -1002,10 +1002,8 @@ static int do_ipv6_getsockopt(struct sock *sk, int level, int optname, ...@@ -1002,10 +1002,8 @@ static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
release_sock(sk); release_sock(sk);
if (skb) { if (skb) {
int err = ip6_datagram_recv_ctl(sk, &msg, skb); ip6_datagram_recv_ctl(sk, &msg, skb);
kfree_skb(skb); kfree_skb(skb);
if (err)
return err;
} else { } else {
if (np->rxopt.bits.rxinfo) { if (np->rxopt.bits.rxinfo) {
struct in6_pktinfo src_info; struct in6_pktinfo src_info;
......
...@@ -62,10 +62,9 @@ static int dummy_ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, ...@@ -62,10 +62,9 @@ static int dummy_ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len,
{ {
return -EAFNOSUPPORT; return -EAFNOSUPPORT;
} }
static int dummy_ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg, static void dummy_ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
struct sk_buff *skb) struct sk_buff *skb)
{ {
return -EAFNOSUPPORT;
} }
static int dummy_icmpv6_err_convert(u8 type, u8 code, int *err) static int dummy_icmpv6_err_convert(u8 type, u8 code, int *err)
{ {
...@@ -254,7 +253,9 @@ int __init pingv6_init(void) ...@@ -254,7 +253,9 @@ int __init pingv6_init(void)
return ret; return ret;
#endif #endif
pingv6_ops.ipv6_recv_error = ipv6_recv_error; pingv6_ops.ipv6_recv_error = ipv6_recv_error;
pingv6_ops.ip6_datagram_recv_ctl = ip6_datagram_recv_ctl; pingv6_ops.ip6_datagram_recv_common_ctl = ip6_datagram_recv_common_ctl;
pingv6_ops.ip6_datagram_recv_specific_ctl =
ip6_datagram_recv_specific_ctl;
pingv6_ops.icmpv6_err_convert = icmpv6_err_convert; pingv6_ops.icmpv6_err_convert = icmpv6_err_convert;
pingv6_ops.ipv6_icmp_error = ipv6_icmp_error; pingv6_ops.ipv6_icmp_error = ipv6_icmp_error;
pingv6_ops.ipv6_chk_addr = ipv6_chk_addr; pingv6_ops.ipv6_chk_addr = ipv6_chk_addr;
...@@ -267,7 +268,8 @@ int __init pingv6_init(void) ...@@ -267,7 +268,8 @@ int __init pingv6_init(void)
void pingv6_exit(void) void pingv6_exit(void)
{ {
pingv6_ops.ipv6_recv_error = dummy_ipv6_recv_error; pingv6_ops.ipv6_recv_error = dummy_ipv6_recv_error;
pingv6_ops.ip6_datagram_recv_ctl = dummy_ip6_datagram_recv_ctl; pingv6_ops.ip6_datagram_recv_common_ctl = dummy_ip6_datagram_recv_ctl;
pingv6_ops.ip6_datagram_recv_specific_ctl = dummy_ip6_datagram_recv_ctl;
pingv6_ops.icmpv6_err_convert = dummy_icmpv6_err_convert; pingv6_ops.icmpv6_err_convert = dummy_icmpv6_err_convert;
pingv6_ops.ipv6_icmp_error = dummy_ipv6_icmp_error; pingv6_ops.ipv6_icmp_error = dummy_ipv6_icmp_error;
pingv6_ops.ipv6_chk_addr = dummy_ipv6_chk_addr; pingv6_ops.ipv6_chk_addr = dummy_ipv6_chk_addr;
......
...@@ -477,12 +477,16 @@ int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, ...@@ -477,12 +477,16 @@ int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
} }
*addr_len = sizeof(*sin6); *addr_len = sizeof(*sin6);
} }
if (np->rxopt.all)
ip6_datagram_recv_common_ctl(sk, msg, skb);
if (is_udp4) { if (is_udp4) {
if (inet->cmsg_flags) if (inet->cmsg_flags)
ip_cmsg_recv(msg, skb); ip_cmsg_recv(msg, skb);
} else { } else {
if (np->rxopt.all) if (np->rxopt.all)
ip6_datagram_recv_ctl(sk, msg, skb); ip6_datagram_recv_specific_ctl(sk, msg, skb);
} }
err = copied; err = copied;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment