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