chiark / gitweb /
udev: link-config: add ethtool support
[elogind.git] / src / udev / net / ethtool-util.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) 2013 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 <sys/ioctl.h>
23 #include <net/if.h>
24 #include <linux/ethtool.h>
25 #include <linux/sockios.h>
26
27 #include "ethtool-util.h"
28
29 #include "strxcpyx.h"
30 #include "util.h"
31 #include "log.h"
32
33 int ethtool_connect(int *ret) {
34         int fd;
35
36         assert_return(ret, -EINVAL);
37
38         fd = socket(PF_INET, SOCK_DGRAM, 0);
39         if (fd < 0) {
40                 return -errno;
41         }
42
43         *ret = fd;
44
45         return 0;
46 }
47
48 int ethtool_set_speed(int fd, const char *ifname, const unsigned int speed, const char *duplex)
49 {
50         struct ifreq ifr;
51         struct ethtool_cmd ecmd;
52         bool need_update;
53         int r;
54
55         if (speed == 0 && !duplex)
56                 return 0;
57
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;
63
64         r = ioctl(fd, SIOCETHTOOL, &ifr);
65         if (r < 0)
66                 return -errno;
67
68         if (ethtool_cmd_speed(&ecmd) != speed) {
69                 ethtool_cmd_speed_set(&ecmd, speed);
70                 need_update = true;
71         }
72
73         if (duplex) {
74                 if (streq(duplex, "half")) {
75                         if (ecmd.duplex != DUPLEX_HALF) {
76                                 ecmd.duplex = DUPLEX_HALF;
77                                 need_update = true;
78                         }
79                 } else if (streq(duplex, "full"))
80                         if (ecmd.duplex != DUPLEX_FULL) {
81                                 ecmd.duplex = DUPLEX_FULL;
82                                 need_update = true;
83                         }
84         }
85
86         if (need_update) {
87                 ecmd.cmd = ETHTOOL_SSET;
88
89                 r = ioctl(fd, SIOCETHTOOL, &ifr);
90                 if (r < 0)
91                         return -errno;
92         }
93
94         return 0;
95 }
96
97 int ethtool_set_wol(int fd, const char *ifname, const char *wol) {
98         struct ifreq ifr;
99         struct ethtool_wolinfo ecmd;
100         bool need_update;
101         int r;
102
103         if (!wol)
104                 return 0;
105
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;
111
112         r = ioctl(fd, SIOCETHTOOL, &ifr);
113         if (r < 0)
114                 return -errno;
115
116         if (streq(wol, "phy")) {
117                 if (ecmd.wolopts != WAKE_PHY) {
118                         ecmd.wolopts = WAKE_PHY;
119                         need_update = true;
120                 }
121         } else if (streq(wol, "magic")) {
122                 if (ecmd.wolopts != WAKE_MAGIC) {
123                         ecmd.wolopts = WAKE_MAGIC;
124                         need_update = true;
125                 }
126         } else if (streq(wol, "off")) {
127                 if (ecmd.wolopts != 0) {
128                         ecmd.wolopts = 0;
129                         need_update = true;
130                 }
131         } else
132                 return -EINVAL;
133
134         if (need_update) {
135                 ecmd.cmd = ETHTOOL_SWOL;
136
137                 r = ioctl(fd, SIOCETHTOOL, &ifr);
138                 if (r < 0)
139                         return -errno;
140         }
141
142         return 0;
143 }