Skip to content

Commit f5715ae

Browse files
kkovaacsdavem330
authored andcommitted
ipv4: Implement IP_TRANSPARENT socket option
This patch introduces the IP_TRANSPARENT socket option: enabling that will make the IPv4 routing omit the non-local source address check on output. Setting IP_TRANSPARENT requires NET_ADMIN capability. Signed-off-by: KOVACS Krisztian <hidden@sch.bme.hu> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a210d01 commit f5715ae

5 files changed

Lines changed: 20 additions & 3 deletions

File tree

‎include/linux/in.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct in_addr {
7575
#define IP_IPSEC_POLICY 16
7676
#define IP_XFRM_POLICY 17
7777
#define IP_PASSSEC 18
78+
#define IP_TRANSPARENT 19
7879

7980
/* BSD compatibility */
8081
#define IP_RECVRETOPTS IP_RETOPTS

‎include/net/inet_sock.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ struct inet_sock {
129129
is_icsk:1,
130130
freebind:1,
131131
hdrincl:1,
132-
mc_loop:1;
132+
mc_loop:1,
133+
transparent:1;
133134
int mc_index;
134135
__be32 mc_addr;
135136
struct ip_mc_socklist *mc_list;

‎include/net/inet_timewait_sock.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ struct inet_timewait_sock {
128128
__be16 tw_dport;
129129
__u16 tw_num;
130130
/* And these are ours. */
131-
__u8 tw_ipv6only:1;
131+
__u8 tw_ipv6only:1,
132+
tw_transparent:1;
132133
/* 15 bits hole, try to pack */
133134
__u16 tw_ipv6_offset;
134135
unsigned long tw_ttd;

‎net/ipv4/inet_timewait_sock.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int stat
126126
tw->tw_reuse = sk->sk_reuse;
127127
tw->tw_hash = sk->sk_hash;
128128
tw->tw_ipv6only = 0;
129+
tw->tw_transparent = inet->transparent;
129130
tw->tw_prot = sk->sk_prot_creator;
130131
twsk_net_set(tw, hold_net(sock_net(sk)));
131132
atomic_set(&tw->tw_refcnt, 1);

‎net/ipv4/ip_sockglue.c‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ static int do_ip_setsockopt(struct sock *sk, int level,
419419
(1<<IP_TTL) | (1<<IP_HDRINCL) |
420420
(1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
421421
(1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
422-
(1<<IP_PASSSEC))) ||
422+
(1<<IP_PASSSEC) | (1<<IP_TRANSPARENT))) ||
423423
optname == IP_MULTICAST_TTL ||
424424
optname == IP_MULTICAST_LOOP) {
425425
if (optlen >= sizeof(int)) {
@@ -878,6 +878,16 @@ static int do_ip_setsockopt(struct sock *sk, int level,
878878
err = xfrm_user_policy(sk, optname, optval, optlen);
879879
break;
880880

881+
case IP_TRANSPARENT:
882+
if (!capable(CAP_NET_ADMIN)) {
883+
err = -EPERM;
884+
break;
885+
}
886+
if (optlen < 1)
887+
goto e_inval;
888+
inet->transparent = !!val;
889+
break;
890+
881891
default:
882892
err = -ENOPROTOOPT;
883893
break;
@@ -1130,6 +1140,9 @@ static int do_ip_getsockopt(struct sock *sk, int level, int optname,
11301140
case IP_FREEBIND:
11311141
val = inet->freebind;
11321142
break;
1143+
case IP_TRANSPARENT:
1144+
val = inet->transparent;
1145+
break;
11331146
default:
11341147
release_sock(sk);
11351148
return -ENOPROTOOPT;

0 commit comments

Comments
 (0)