Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 84f39b08 authored by Lorenzo Colitti's avatar Lorenzo Colitti Committed by David S. Miller
Browse files

net: support marking accepting TCP sockets



When using mark-based routing, sockets returned from accept()
may need to be marked differently depending on the incoming
connection request.

This is the case, for example, if different socket marks identify
different networks: a listening socket may want to accept
connections from all networks, but each connection should be
marked with the network that the request came in on, so that
subsequent packets are sent on the correct network.

This patch adds a sysctl to mark TCP sockets based on the fwmark
of the incoming SYN packet. If enabled, and an unmarked socket
receives a SYN, then the SYN packet's fwmark is written to the
connection's inet_request_sock, and later written back to the
accepted socket when the connection is established.  If the
socket already has a nonzero mark, then the behaviour is the same
as it is today, i.e., the listening socket's fwmark is used.

Black-box tested using user-mode linux:

- IPv4/IPv6 SYN+ACK, FIN, etc. packets are routed based on the
  mark of the incoming SYN packet.
- The socket returned by accept() is marked with the mark of the
  incoming SYN packet.
- Tested with syncookies=1 and syncookies=2.

Signed-off-by: default avatarLorenzo Colitti <lorenzo@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 1b3c61dc
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ struct inet_request_sock {
	kmemcheck_bitfield_end(flags);
	struct ip_options_rcu	*opt;
	struct sk_buff		*pktopts;
	u32                     ir_mark;
};

static inline struct inet_request_sock *inet_rsk(const struct request_sock *sk)
@@ -97,6 +98,15 @@ static inline struct inet_request_sock *inet_rsk(const struct request_sock *sk)
	return (struct inet_request_sock *)sk;
}

static inline u32 inet_request_mark(struct sock *sk, struct sk_buff *skb)
{
	if (!sk->sk_mark && sock_net(sk)->ipv4.sysctl_tcp_fwmark_accept) {
		return skb->mark;
	} else {
		return sk->sk_mark;
	}
}

struct inet_cork {
	unsigned int		flags;
	__be32			addr;
+1 −0
Original line number Diff line number Diff line
@@ -78,6 +78,7 @@ struct netns_ipv4 {
	int sysctl_ip_fwd_use_pmtu;

	int sysctl_fwmark_reflect;
	int sysctl_tcp_fwmark_accept;

	struct ping_group_range ping_group_range;

+4 −2
Original line number Diff line number Diff line
@@ -408,7 +408,7 @@ struct dst_entry *inet_csk_route_req(struct sock *sk,
	struct net *net = sock_net(sk);
	int flags = inet_sk_flowi_flags(sk);

	flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark,
	flowi4_init_output(fl4, sk->sk_bound_dev_if, ireq->ir_mark,
			   RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
			   sk->sk_protocol,
			   flags,
@@ -445,7 +445,7 @@ struct dst_entry *inet_csk_route_child_sock(struct sock *sk,

	rcu_read_lock();
	opt = rcu_dereference(newinet->inet_opt);
	flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark,
	flowi4_init_output(fl4, sk->sk_bound_dev_if, inet_rsk(req)->ir_mark,
			   RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
			   sk->sk_protocol, inet_sk_flowi_flags(sk),
			   (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr,
@@ -680,6 +680,8 @@ struct sock *inet_csk_clone_lock(const struct sock *sk,
		inet_sk(newsk)->inet_sport = htons(inet_rsk(req)->ir_num);
		newsk->sk_write_space = sk_stream_write_space;

		newsk->sk_mark = inet_rsk(req)->ir_mark;

		newicsk->icsk_retransmits = 0;
		newicsk->icsk_backoff	  = 0;
		newicsk->icsk_probes_out  = 0;
+2 −1
Original line number Diff line number Diff line
@@ -303,6 +303,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
	ireq->ir_rmt_port	= th->source;
	ireq->ir_loc_addr	= ip_hdr(skb)->daddr;
	ireq->ir_rmt_addr	= ip_hdr(skb)->saddr;
	ireq->ir_mark		= inet_request_mark(sk, skb);
	ireq->ecn_ok		= ecn_ok;
	ireq->snd_wscale	= tcp_opt.snd_wscale;
	ireq->sack_ok		= tcp_opt.sack_ok;
@@ -339,7 +340,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
	 * hasn't changed since we received the original syn, but I see
	 * no easy way to do this.
	 */
	flowi4_init_output(&fl4, sk->sk_bound_dev_if, sk->sk_mark,
	flowi4_init_output(&fl4, sk->sk_bound_dev_if, ireq->ir_mark,
			   RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, IPPROTO_TCP,
			   inet_sk_flowi_flags(sk),
			   (opt && opt->srr) ? opt->faddr : ireq->ir_rmt_addr,
+7 −0
Original line number Diff line number Diff line
@@ -845,6 +845,13 @@ static struct ctl_table ipv4_net_table[] = {
		.mode		= 0644,
		.proc_handler	= proc_dointvec,
	},
	{
		.procname	= "tcp_fwmark_accept",
		.data		= &init_net.ipv4.sysctl_tcp_fwmark_accept,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= proc_dointvec,
	},
	{ }
};

Loading