1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2013 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/>.
22 #include <sys/ioctl.h>
24 #include <linux/ethtool.h>
25 #include <linux/sockios.h>
27 #include "ethtool-util.h"
33 int ethtool_connect(int *ret) {
36 assert_return(ret, -EINVAL);
38 fd = socket(PF_INET, SOCK_DGRAM, 0);
48 int ethtool_set_speed(int fd, const char *ifname, const unsigned int speed, const char *duplex)
51 struct ethtool_cmd ecmd;
55 if (speed == 0 && !duplex)
58 memset(&ecmd, 0x00, sizeof(struct ethtool_cmd));
59 ecmd.cmd = ETHTOOL_GSET;
60 memset(&ifr, 0x00, sizeof(struct ifreq));
61 strscpy(ifr.ifr_name, IFNAMSIZ, ifname);
62 ifr.ifr_data = (void *)&ecmd;
64 r = ioctl(fd, SIOCETHTOOL, &ifr);
68 if (ethtool_cmd_speed(&ecmd) != speed) {
69 ethtool_cmd_speed_set(&ecmd, speed);
74 if (streq(duplex, "half")) {
75 if (ecmd.duplex != DUPLEX_HALF) {
76 ecmd.duplex = DUPLEX_HALF;
79 } else if (streq(duplex, "full"))
80 if (ecmd.duplex != DUPLEX_FULL) {
81 ecmd.duplex = DUPLEX_FULL;
87 ecmd.cmd = ETHTOOL_SSET;
89 r = ioctl(fd, SIOCETHTOOL, &ifr);
97 int ethtool_set_wol(int fd, const char *ifname, const char *wol) {
99 struct ethtool_wolinfo ecmd;
106 memset(&ecmd, 0x00, sizeof(struct ethtool_wolinfo));
107 ecmd.cmd = ETHTOOL_GWOL;
108 memset(&ifr, 0x00, sizeof(struct ifreq));
109 strscpy(ifr.ifr_name, IFNAMSIZ, ifname);
110 ifr.ifr_data = (void *)&ecmd;
112 r = ioctl(fd, SIOCETHTOOL, &ifr);
116 if (streq(wol, "phy")) {
117 if (ecmd.wolopts != WAKE_PHY) {
118 ecmd.wolopts = WAKE_PHY;
121 } else if (streq(wol, "magic")) {
122 if (ecmd.wolopts != WAKE_MAGIC) {
123 ecmd.wolopts = WAKE_MAGIC;
126 } else if (streq(wol, "off")) {
127 if (ecmd.wolopts != 0) {
135 ecmd.cmd = ETHTOOL_SWOL;
137 r = ioctl(fd, SIOCETHTOOL, &ifr);