]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/test-pppoe.c
shared: add process-util.[ch]
[thirdparty/systemd.git] / src / libsystemd-network / test-pppoe.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright (C) 2014 Tom Gundersen <teg@jklm.no>
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <unistd.h>
25
26 #include <linux/veth.h>
27 #include <net/if.h>
28
29 #include "util.h"
30 #include "sd-event.h"
31 #include "event-util.h"
32 #include "sd-rtnl.h"
33 #include "sd-pppoe.h"
34 #include "process-util.h"
35
36 static void pppoe_handler(sd_pppoe *ppp, int event, void *userdata) {
37 static int pppoe_state = -1;
38 sd_event *e = userdata;
39
40 assert_se(ppp);
41 assert_se(e);
42
43 switch (event) {
44 case PPPOE_EVENT_RUNNING:
45 assert_se(pppoe_state == -1);
46 log_info("running");
47 break;
48 case PPPOE_EVENT_STOPPED:
49 assert_se(pppoe_state == PPPOE_EVENT_RUNNING);
50 log_info("stopped");
51 assert_se(sd_event_exit(e, 0) >= 0);
52 break;
53 default:
54 assert_not_reached("invalid pppoe event");
55 }
56
57 pppoe_state = event;
58 }
59
60 static int client_run(const char *client_name, sd_event *e) {
61 sd_pppoe *pppoe;
62 int client_ifindex;
63
64 client_ifindex = (int) if_nametoindex(client_name);
65 assert_se(client_ifindex > 0);
66
67 assert_se(sd_pppoe_new(&pppoe) >= 0);
68 assert_se(sd_pppoe_attach_event(pppoe, e, 0) >= 0);
69
70 assert_se(sd_pppoe_set_ifname(pppoe, "pppoe-client") >= 0);
71 assert_se(sd_pppoe_set_ifindex(pppoe, client_ifindex) >= 0);
72 assert_se(sd_pppoe_set_callback(pppoe, pppoe_handler, e) >= 0);
73
74 log_info("starting PPPoE client, it will exit when the server times out and sends PADT");
75
76 assert_se(sd_pppoe_start(pppoe) >= 0);
77
78 assert_se(sd_event_loop(e) >= 0);
79
80 assert_se(!sd_pppoe_unref(pppoe));
81
82 return EXIT_SUCCESS;
83 }
84
85 static int test_pppoe_server(sd_event *e) {
86 sd_rtnl *rtnl;
87 sd_rtnl_message *m;
88 pid_t pid;
89 int r, client_ifindex, server_ifindex;
90
91 r = unshare(CLONE_NEWNET);
92 if (r < 0 && errno == EPERM)
93 return EXIT_TEST_SKIP;
94
95 assert_se(r >= 0);
96
97 assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
98 assert_se(sd_rtnl_attach_event(rtnl, e, 0) >= 0);
99
100 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
101 assert_se(sd_rtnl_message_append_string(m, IFLA_IFNAME, "pppoe-server") >= 0);
102 assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
103 assert_se(sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA, "veth") >= 0);
104 assert_se(sd_rtnl_message_open_container(m, VETH_INFO_PEER) >= 0);
105 assert_se(sd_rtnl_message_append_string(m, IFLA_IFNAME, "pppoe-client") >= 0);
106 assert_se(sd_rtnl_message_close_container(m) >= 0);
107 assert_se(sd_rtnl_message_close_container(m) >= 0);
108 assert_se(sd_rtnl_message_close_container(m) >= 0);
109 assert_se(sd_rtnl_call(rtnl, m, 0, NULL) >= 0);
110
111 client_ifindex = (int) if_nametoindex("pppoe-client");
112 assert_se(client_ifindex > 0);
113 server_ifindex = (int) if_nametoindex("pppoe-server");
114 assert_se(server_ifindex > 0);
115
116 m = sd_rtnl_message_unref(m);
117 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, client_ifindex) >= 0);
118 assert_se(sd_rtnl_message_link_set_flags(m, IFF_UP, IFF_UP) >= 0);
119 assert_se(sd_rtnl_call(rtnl, m, 0, NULL) >= 0);
120
121 m = sd_rtnl_message_unref(m);
122 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, server_ifindex) >= 0);
123 assert_se(sd_rtnl_message_link_set_flags(m, IFF_UP, IFF_UP) >= 0);
124 assert_se(sd_rtnl_call(rtnl, m, 0, NULL) >= 0);
125
126 pid = fork();
127 assert_se(pid >= 0);
128 if (pid == 0) {
129 /* let the client send some discover messages before the server is started */
130 sleep(2);
131
132 /* TODO: manage pppoe-server-options */
133 execlp("pppoe-server", "pppoe-server", "-F",
134 "-I", "pppoe-server",
135 "-C", "Test-AC",
136 "-S", "Service-Default",
137 "-S", "Service-First-Auxiliary",
138 "-S", "Service-Second-Auxiliary",
139 NULL);
140 assert_not_reached("failed to execute pppoe-server. not installed?");
141 }
142
143 client_run("pppoe-client", e);
144
145 assert_se(kill(pid, SIGTERM) >= 0);
146 assert_se(wait_for_terminate(pid, NULL) >= 0);
147
148 assert_se(!sd_rtnl_message_unref(m));
149 assert_se(!sd_rtnl_unref(rtnl));
150
151 return EXIT_SUCCESS;
152 }
153
154 int main(int argc, char *argv[]) {
155 _cleanup_event_unref_ sd_event *e = NULL;
156
157 log_set_max_level(LOG_DEBUG);
158 log_parse_environment();
159 log_open();
160
161 assert_se(sd_event_new(&e) >= 0);
162
163 if (argc == 1) {
164 log_info("running PPPoE client against local server");
165
166 return test_pppoe_server(e);
167 } else if (argc == 2) {
168 log_info("running PPPoE client over '%s'", argv[1]);
169
170 return client_run(argv[1], e);
171 } else {
172 log_error("This program takes one or no arguments.\n"
173 "\t %s [<ifname>]", program_invocation_short_name);
174 return EXIT_FAILURE;
175 }
176 }