1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2014 Tom Gundersen <teg@jklm.no>
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.
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.
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/>.
26 #include <linux/veth.h>
31 #include "event-util.h"
34 #include "process-util.h"
36 static void pppoe_handler(sd_pppoe
*ppp
, int event
, void *userdata
) {
37 static int pppoe_state
= -1;
38 sd_event
*e
= userdata
;
44 case PPPOE_EVENT_RUNNING
:
45 assert_se(pppoe_state
== -1);
48 case PPPOE_EVENT_STOPPED
:
49 assert_se(pppoe_state
== PPPOE_EVENT_RUNNING
);
51 assert_se(sd_event_exit(e
, 0) >= 0);
54 assert_not_reached("invalid pppoe event");
60 static int client_run(const char *client_name
, sd_event
*e
) {
64 client_ifindex
= (int) if_nametoindex(client_name
);
65 assert_se(client_ifindex
> 0);
67 assert_se(sd_pppoe_new(&pppoe
) >= 0);
68 assert_se(sd_pppoe_attach_event(pppoe
, e
, 0) >= 0);
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);
74 log_info("starting PPPoE client, it will exit when the server times out and sends PADT");
76 assert_se(sd_pppoe_start(pppoe
) >= 0);
78 assert_se(sd_event_loop(e
) >= 0);
80 assert_se(!sd_pppoe_unref(pppoe
));
85 static int test_pppoe_server(sd_event
*e
) {
89 int r
, client_ifindex
, server_ifindex
;
91 r
= unshare(CLONE_NEWNET
);
92 if (r
< 0 && errno
== EPERM
)
93 return EXIT_TEST_SKIP
;
97 assert_se(sd_rtnl_open(&rtnl
, 0) >= 0);
98 assert_se(sd_rtnl_attach_event(rtnl
, e
, 0) >= 0);
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);
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);
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);
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);
129 /* let the client send some discover messages before the server is started */
132 /* TODO: manage pppoe-server-options */
133 execlp("pppoe-server", "pppoe-server", "-F",
134 "-I", "pppoe-server",
136 "-S", "Service-Default",
137 "-S", "Service-First-Auxiliary",
138 "-S", "Service-Second-Auxiliary",
140 assert_not_reached("failed to execute pppoe-server. not installed?");
143 client_run("pppoe-client", e
);
145 assert_se(kill(pid
, SIGTERM
) >= 0);
146 assert_se(wait_for_terminate(pid
, NULL
) >= 0);
148 assert_se(!sd_rtnl_message_unref(m
));
149 assert_se(!sd_rtnl_unref(rtnl
));
154 int main(int argc
, char *argv
[]) {
155 _cleanup_event_unref_ sd_event
*e
= NULL
;
157 log_set_max_level(LOG_DEBUG
);
158 log_parse_environment();
161 assert_se(sd_event_new(&e
) >= 0);
164 log_info("running PPPoE client against local server");
166 return test_pppoe_server(e
);
167 } else if (argc
== 2) {
168 log_info("running PPPoE client over '%s'", argv
[1]);
170 return client_run(argv
[1], e
);
172 log_error("This program takes one or no arguments.\n"
173 "\t %s [<ifname>]", program_invocation_short_name
);