xref: /netbsd-src/tests/sbin/ifconfig/t_capabilities.sh (revision fa6a6a38f152634be2d5ec9b93c67f19dcc6e2fd)
1# $NetBSD: t_capabilities.sh,v 1.3 2023/10/18 08:52:46 rin Exp $
2#
3# Copyright (c) 2020 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Jukka Ruohonen.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30setcap() {
31
32	echo "Request: $1 $2 ($3)"
33	ifconfig $1 $2
34
35	if [ $4 -eq 1 ]; then
36
37		if [ ! $? -eq 0 ]; then
38			atf_fail "Failed to enable $3 for $1"
39		fi
40
41		if [ -z "$(ifconfig $1 | grep "enabled" | grep $3)" ]; then
42			atf_fail "Failed to enable $3 for $1"
43		fi
44
45		echo "Request: $1 -$2 (-$3)"
46		ifconfig $1 -$2
47
48		if [ ! $? -eq 0 ]; then
49			atf_fail "Failed to disable $3 for $1"
50		fi
51
52		if [ ! -z "$(ifconfig $1 | grep "enabled" | grep $3)" ]; then
53			atf_fail "Failed to disable $3 for $1"
54		fi
55	fi
56}
57
58parsecap() {
59
60	i=$1
61	checkrv=$3
62	x=$(echo $2 | sed "s/.*<//" | sed "s/>//")
63
64	export IFS=","
65
66	for y in $x; do
67
68		z=""
69
70		if [ $y = "TSO4" ]; then
71			z="tso4"
72		elif [ $y = "IP4CSUM_Rx" ]; then
73			z="ip4csum-rx"
74		elif [ $y = "IP4CSUM_Tx" ]; then
75			z="ip4csum-tx"
76		elif [ $y = "TCP4CSUM_Rx" ]; then
77			z="tcp4csum-rx"
78		elif [ $y = "TCP4CSUM_Tx" ]; then
79			z="tcp4csum-tx"
80		elif [ $y = "UDP4CSUM_Rx" ]; then
81			z="udp4csum-rx"
82		elif [ $y = "UDP4CSUM_Tx" ]; then
83			z="udp4csum-tx"
84		elif [ $y = "TCP6CSUM_Rx" ]; then
85			z="tcp6csum-rx"
86		elif [ $y = "TCP6CSUM_Tx" ]; then
87			z="tcp6csum-tx"
88		elif [ $y = "UDP6CSUM_Rx" ]; then
89			z="udp6csum-rx"
90		elif [ $y = "UDP6CSUM_Tx" ]; then
91			z="udp6csum-tx"
92		elif [ $y = "TSO6" ]; then
93			z="tso6"
94		fi
95
96		if [ -z $z ]; then
97			echo "Skipping unrecognized $y for $i"
98		else
99			setcap $i $z $y $checkrv
100		fi
101	done
102
103	unset IFS
104}
105
106atf_test_case basic
107basic_head() {
108	atf_set "require.user" "root"
109	atf_set "descr" "Test setting interface capabilities"
110}
111
112basic_body() {
113
114	if ! [ $(atf_config_get "run_unsafe" "no") = "yes" ]; then
115		atf_skip "modify if_capenable for real interfaces"
116	fi
117
118	for i in $(ifconfig -l); do
119
120		c=$(ifconfig $i | grep "capabilities")
121
122		if [ -z "$c" ]; then
123			echo "Skipping $i, no capabilities"
124			continue
125		fi
126
127		if [ ! -z "$(echo $i | grep ixg)" ]; then
128			echo "Skipping $i (PR kern/50933)"
129			continue
130		fi
131
132		enabled=$(ifconfig $i | grep "enabled")
133
134		for l in $c; do
135
136			if [ ! -z "$(echo $l | grep "ec_capabilities")" ]; then
137				continue
138			fi
139
140			parsecap $i $l 1
141		done
142
143		if [ ! -z "$enabled" ]; then
144
145			echo "Restoring capabilities for $i"
146
147			for l in $enabled; do
148
149				ec=$(echo $l | grep "ec_enabled")
150
151				if [ ! -z "$ec" ]; then
152					continue
153				fi
154
155				parsecap $i $l 0
156			done
157	       fi
158	done
159}
160
161atf_init_test_cases() {
162	atf_add_test_case basic
163}
164