xref: /linux/tools/testing/selftests/net/netns-sysctl.sh (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1*5b5233fbSMatteo Croce#!/bin/bash -e
2*5b5233fbSMatteo Croce# SPDX-License-Identifier: GPL-2.0
3*5b5233fbSMatteo Croce#
4*5b5233fbSMatteo Croce# This test checks that the network buffer sysctls are present
5*5b5233fbSMatteo Croce# in a network namespaces, and that they are readonly.
6*5b5233fbSMatteo Croce
7*5b5233fbSMatteo Crocesource lib.sh
8*5b5233fbSMatteo Croce
9*5b5233fbSMatteo Crocecleanup() {
10*5b5233fbSMatteo Croce    cleanup_ns $test_ns
11*5b5233fbSMatteo Croce}
12*5b5233fbSMatteo Croce
13*5b5233fbSMatteo Crocetrap cleanup EXIT
14*5b5233fbSMatteo Croce
15*5b5233fbSMatteo Crocefail() {
16*5b5233fbSMatteo Croce	echo "ERROR: $*" >&2
17*5b5233fbSMatteo Croce	exit 1
18*5b5233fbSMatteo Croce}
19*5b5233fbSMatteo Croce
20*5b5233fbSMatteo Crocesetup_ns test_ns
21*5b5233fbSMatteo Croce
22*5b5233fbSMatteo Crocefor sc in {r,w}mem_{default,max}; do
23*5b5233fbSMatteo Croce	# check that this is writable in a netns
24*5b5233fbSMatteo Croce	[ -w "/proc/sys/net/core/$sc" ] ||
25*5b5233fbSMatteo Croce		fail "$sc isn't writable in the init netns!"
26*5b5233fbSMatteo Croce
27*5b5233fbSMatteo Croce	# change the value in the host netns
28*5b5233fbSMatteo Croce	sysctl -qw "net.core.$sc=300000" ||
29*5b5233fbSMatteo Croce		fail "Can't write $sc in init netns!"
30*5b5233fbSMatteo Croce
31*5b5233fbSMatteo Croce	# check that the value is read from the init netns
32*5b5233fbSMatteo Croce	[ "$(ip netns exec $test_ns sysctl -n "net.core.$sc")" -eq 300000 ] ||
33*5b5233fbSMatteo Croce		fail "Value for $sc mismatch!"
34*5b5233fbSMatteo Croce
35*5b5233fbSMatteo Croce	# check that this isn't writable in a netns
36*5b5233fbSMatteo Croce	ip netns exec $test_ns [ -w "/proc/sys/net/core/$sc" ] &&
37*5b5233fbSMatteo Croce		fail "$sc is writable in a netns!"
38*5b5233fbSMatteo Crocedone
39*5b5233fbSMatteo Croce
40*5b5233fbSMatteo Croceecho 'Test passed OK'
41