summaryrefslogtreecommitdiff
path: root/net/test/net_test.sh
blob: 7185fd510def9df6eb9e2ee36e70a68d483c63b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
if [[ -n "${verbose}" ]]; then
  echo 'Current working directory:'
  echo " - according to builtin:  [$(pwd)]"
  echo " - according to /bin/pwd: [$(/bin/pwd)]"
  echo

  echo 'Shell environment:'
  env
  echo

  echo -n "net_test.sh (pid $$, parent ${PPID}, tty $(tty)) running [$0] with args:"
  for arg in "$@"; do
    echo -n " [${arg}]"
  done
  echo
  echo
fi

if [[ "$(tty)" == 'not a tty' ]]; then
  echo 'not a tty? perhaps not quite real kernel default /dev/console - trying to fix.'
  if [[ -c /dev/console ]]; then
    [[ "$(readlink /proc/$$/fd/0)" != '/dev/console' ]] || exec < /dev/console
    [[ "$(readlink /proc/$$/fd/1)" != '/dev/console' ]] || exec > /dev/console
    [[ "$(readlink /proc/$$/fd/2)" != '/dev/console' ]] || exec 2> /dev/console
  fi
fi

if [[ "$(tty)" == '/dev/console' ]]; then
  ARCH="$(uname -m)"
  # Underscore is illegal in hostname, replace with hyphen
  ARCH="${ARCH//_/-}"

  # setsid + /dev/tty{,AMA,S}0 allows bash's job control to work, ie. Ctrl+C/Z
  if [[ -e '/proc/exitcode' ]]; then
    # exists only in UML
    CON='/dev/tty0'
    hostname "uml-${ARCH}"
  elif [[ -c '/dev/ttyAMA0' ]]; then
    # Qemu for arm (note: /dev/ttyS0 also exists for exitcode)
    CON='/dev/ttyAMA0'
    hostname "qemu-${ARCH}"
  elif [[ -c '/dev/ttyS0' ]]; then
    # Qemu for x86 (note: /dev/ttyS1 also exists for exitcode)
    CON='/dev/ttyS0'
    hostname "qemu-${ARCH}"
  else
    # Can't figure it out, job control won't work, tough luck
    echo 'Unable to figure out proper console - job control will not work.' >&2
    CON=''
    hostname "local-${ARCH}"
  fi

  unset ARCH

  echo -n "$(hostname): Currently tty[/dev/console], but it should be [${CON}]..."

  if [[ -n "${CON}" ]]; then
    # Redirect std{in,out,err} to the console equivalent tty
    # which actually supports all standard tty ioctls
    exec <"${CON}" >&"${CON}"

    # Bash wants to be session leader, hence need for setsid
    echo " re-executing..."
    exec /usr/bin/setsid "$0" "$@"
    # If the above exec fails, we just fall through...
    # (this implies failure to *find* setsid, not error return from bash,
    #  in practice due to image construction this cannot happen)
  else
    echo
  fi

  # In case we fall through, clean up
  unset CON
fi

if [[ -n "${verbose}" ]]; then
  echo 'TTY settings:'
  stty
  echo

  echo 'TTY settings (verbose):'
  stty -a
  echo

  echo 'Restoring TTY sanity...'
fi

stty sane
stty 115200
[[ -z "${console_cols}" ]] || stty columns "${console_cols}"
[[ -z "${console_rows}" ]] || stty rows    "${console_rows}"

if [[ -n "${verbose}" ]]; then
  echo

  echo 'TTY settings:'
  stty
  echo

  echo 'TTY settings (verbose):'
  stty -a
  echo
fi

# By the time we get here we should have a sane console:
#  - 115200 baud rate
#  - appropriate (and known) width and height (note: this assumes
#    that the terminal doesn't get further resized)
#  - it is no longer /dev/console, so job control should function
#    (this means working ctrl+c [abort] and ctrl+z [suspend])


# This defaults to 60 which is needlessly long during boot
# (we will reset it back to the default later)
echo 0 > /proc/sys/kernel/random/urandom_min_reseed_secs

if [[ -n "${entropy}" ]]; then
  echo "adding entropy from hex string [${entropy}]" >&2

  # In kernel/include/uapi/linux/random.h RNDADDENTROPY is defined as
  # _IOW('R', 0x03, int[2]) =(R is 0x52)= 0x40085203 = 1074287107
  /usr/bin/python3 3>/dev/random <<EOF
import base64, fcntl, struct
rnd = base64.b64decode('${entropy}')
fcntl.ioctl(3, 0x40085203, struct.pack('ii', len(rnd) * 8, len(rnd)) + rnd)
EOF

fi

# Make sure the urandom pool has a chance to initialize before we reset
# the reseed timer back to 60 seconds.  One timer tick should be enough.
sleep 1.1

# By this point either 'random: crng init done' (newer kernels)
# or 'random: nonblocking pool is initialized' (older kernels)
# should have been printed out to dmesg/console.

# Reset it back to boot time default
echo 60 > /proc/sys/kernel/random/urandom_min_reseed_secs

# Make sure /sys is mounted
[[ -d /sys/fs ]] || mount -t sysfs sysfs -o nosuid,nodev,noexec /sys

if ! [[ "$(uname -r)" =~ ^([0-3]|4[.][0-8])[.] ]]; then
  # Mount the bpf filesystem on Linux version 4.9+
  mount -t bpf bpf -o nosuid,nodev,noexec /sys/fs/bpf
fi

if ! [[ "$(uname -r)" =~ ^([0-3]|4[.][0-9]|4[.]1[0-3])[.] ]]; then
  # Mount the Cgroup v2 filesystem on Linux version 4.14+
  mount -t cgroup2 cgroup2 -o nosuid,nodev,noexec /sys/fs/cgroup
fi

# In case IPv6 is compiled as a module.
[ -f /proc/net/if_inet6 ] || insmod $DIR/kernel/net-next/net/ipv6/ipv6.ko

# Minimal network setup.
ip link set lo up
ip link set lo mtu 16436
if [[ -d /sys/class/net/eth0 ]]; then
  ip link set eth0 up
fi

# Allow people to run ping.
echo '0 2147483647' > /proc/sys/net/ipv4/ping_group_range

# Allow unprivileged use of eBPF (matches Android OS)
if [[ "$(< /proc/sys/kernel/unprivileged_bpf_disabled)" != '0' ]]; then
  echo 0 > /proc/sys/kernel/unprivileged_bpf_disabled
fi

# Read environment variables passed to the kernel to determine if script is
# running on builder and to find which test to run.

if [ "$net_test_mode" != "builder" ]; then
  # Fall out to a shell once the test completes or if there's an error.
  trap "exec /bin/bash" ERR EXIT
fi

echo -e "Running $net_test $net_test_args\n"
$net_test $net_test_args
rv="$?"

# Write exit code of net_test to a file so that the builder can use it
# to signal failure if any tests fail.
echo "${rv}" > "${exitcode}"

# Additionally on UML make it the exit code of UML kernel binary itself.
if [[ -e '/proc/exitcode' ]]; then
  echo "${rv}" > /proc/exitcode
fi