Skip to content

Commit 3632679

Browse files
NicolasDichtelPaolo Abeni
authored andcommitted
ipv{4,6}/raw: fix output xfrm lookup wrt protocol
With a raw socket bound to IPPROTO_RAW (ie with hdrincl enabled), the protocol field of the flow structure, build by raw_sendmsg() / rawv6_sendmsg()), is set to IPPROTO_RAW. This breaks the ipsec policy lookup when some policies are defined with a protocol in the selector. For ipv6, the sin6_port field from 'struct sockaddr_in6' could be used to specify the protocol. Just accept all values for IPPROTO_RAW socket. For ipv4, the sin_port field of 'struct sockaddr_in' could not be used without breaking backward compatibility (the value of this field was never checked). Let's add a new kind of control message, so that the userland could specify which protocol is used. Fixes: 1da177e ("Linux-2.6.12-rc2") CC: [email protected] Signed-off-by: Nicolas Dichtel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent 6007612 commit 3632679

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

include/net/ip.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct ipcm_cookie {
7676
__be32 addr;
7777
int oif;
7878
struct ip_options_rcu *opt;
79+
__u8 protocol;
7980
__u8 ttl;
8081
__s16 tos;
8182
char priority;
@@ -96,6 +97,7 @@ static inline void ipcm_init_sk(struct ipcm_cookie *ipcm,
9697
ipcm->sockc.tsflags = inet->sk.sk_tsflags;
9798
ipcm->oif = READ_ONCE(inet->sk.sk_bound_dev_if);
9899
ipcm->addr = inet->inet_saddr;
100+
ipcm->protocol = inet->inet_num;
99101
}
100102

101103
#define IPCB(skb) ((struct inet_skb_parm*)((skb)->cb))

include/uapi/linux/in.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ struct in_addr {
163163
#define IP_MULTICAST_ALL 49
164164
#define IP_UNICAST_IF 50
165165
#define IP_LOCAL_PORT_RANGE 51
166+
#define IP_PROTOCOL 52
166167

167168
#define MCAST_EXCLUDE 0
168169
#define MCAST_INCLUDE 1

net/ipv4/ip_sockglue.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,14 @@ int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
317317
ipc->tos = val;
318318
ipc->priority = rt_tos2priority(ipc->tos);
319319
break;
320-
320+
case IP_PROTOCOL:
321+
if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
322+
return -EINVAL;
323+
val = *(int *)CMSG_DATA(cmsg);
324+
if (val < 1 || val > 255)
325+
return -EINVAL;
326+
ipc->protocol = val;
327+
break;
321328
default:
322329
return -EINVAL;
323330
}
@@ -1761,6 +1768,9 @@ int do_ip_getsockopt(struct sock *sk, int level, int optname,
17611768
case IP_LOCAL_PORT_RANGE:
17621769
val = inet->local_port_range.hi << 16 | inet->local_port_range.lo;
17631770
break;
1771+
case IP_PROTOCOL:
1772+
val = inet_sk(sk)->inet_num;
1773+
break;
17641774
default:
17651775
sockopt_release_sock(sk);
17661776
return -ENOPROTOOPT;

net/ipv4/raw.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,9 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
532532
}
533533

534534
ipcm_init_sk(&ipc, inet);
535+
/* Keep backward compat */
536+
if (hdrincl)
537+
ipc.protocol = IPPROTO_RAW;
535538

536539
if (msg->msg_controllen) {
537540
err = ip_cmsg_send(sk, msg, &ipc, false);
@@ -599,7 +602,7 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
599602

600603
flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos,
601604
RT_SCOPE_UNIVERSE,
602-
hdrincl ? IPPROTO_RAW : sk->sk_protocol,
605+
hdrincl ? ipc.protocol : sk->sk_protocol,
603606
inet_sk_flowi_flags(sk) |
604607
(hdrincl ? FLOWI_FLAG_KNOWN_NH : 0),
605608
daddr, saddr, 0, 0, sk->sk_uid);

net/ipv6/raw.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,8 @@ static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
793793

794794
if (!proto)
795795
proto = inet->inet_num;
796-
else if (proto != inet->inet_num)
796+
else if (proto != inet->inet_num &&
797+
inet->inet_num != IPPROTO_RAW)
797798
return -EINVAL;
798799

799800
if (proto > 255)

0 commit comments

Comments
 (0)