1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (c) 2019 Richard Palethorpe <rpalethorpe@suse.com>
4 *
5 * The user or file requires CAP_NET_RAW for this test to work.
6 * e.g use "$ setcap cap_net_raw=pei tst_capability"
7 */
8
9#include <unistd.h>
10#include <sys/types.h>
11
12#include "tst_test.h"
13#include "tst_capability.h"
14#include "tst_safe_net.h"
15
16#include "lapi/socket.h"
17
18static void run(void)
19{
20	TEST(socket(AF_INET, SOCK_RAW, 1));
21	if (TST_RET > -1) {
22		tst_res(TFAIL, "Created raw socket");
23		SAFE_CLOSE(TST_RET);
24	} else if (TST_ERR != EPERM) {
25		tst_res(TFAIL | TTERRNO,
26			"Failed to create socket for wrong reason");
27	} else {
28		tst_res(TPASS | TTERRNO, "Didn't create raw socket");
29	}
30}
31
32static void setup(void)
33{
34	if (geteuid() == 0)
35		tst_res(TWARN, "CAP_NET_RAW may be ignored when euid == 0");
36
37	TEST(socket(AF_INET, SOCK_RAW, 1));
38	if (TST_RET < 0)
39		tst_brk(TFAIL | TTERRNO, "Can't create raw socket in setup");
40
41	SAFE_CLOSE(TST_RET);
42}
43
44static struct tst_test test = {
45	.setup = setup,
46	.test_all = run,
47	.caps = (struct tst_cap []) {
48		TST_CAP(TST_CAP_REQ, CAP_NET_RAW),
49		TST_CAP(TST_CAP_DROP, CAP_NET_RAW),
50		{}
51	},
52};
53