1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 #include <linux/netdevice.h>
4 #include <net/netdev_lock.h>
5
6 #include "dev.h"
7
8 /**
9 * dev_change_name() - change name of a device
10 * @dev: device
11 * @newname: name (or format string) must be at least IFNAMSIZ
12 *
13 * Change name of a device, can pass format strings "eth%d".
14 * for wildcarding.
15 *
16 * Return: 0 on success, -errno on failure.
17 */
dev_change_name(struct net_device * dev,const char * newname)18 int dev_change_name(struct net_device *dev, const char *newname)
19 {
20 int ret;
21
22 netdev_lock_ops(dev);
23 ret = netif_change_name(dev, newname);
24 netdev_unlock_ops(dev);
25
26 return ret;
27 }
28
29 /**
30 * dev_set_alias() - change ifalias of a device
31 * @dev: device
32 * @alias: name up to IFALIASZ
33 * @len: limit of bytes to copy from info
34 *
35 * Set ifalias for a device.
36 *
37 * Return: 0 on success, -errno on failure.
38 */
dev_set_alias(struct net_device * dev,const char * alias,size_t len)39 int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
40 {
41 int ret;
42
43 netdev_lock_ops(dev);
44 ret = netif_set_alias(dev, alias, len);
45 netdev_unlock_ops(dev);
46
47 return ret;
48 }
49 EXPORT_SYMBOL(dev_set_alias);
50
51 /**
52 * dev_change_flags() - change device settings
53 * @dev: device
54 * @flags: device state flags
55 * @extack: netlink extended ack
56 *
57 * Change settings on device based state flags. The flags are
58 * in the userspace exported format.
59 *
60 * Return: 0 on success, -errno on failure.
61 */
dev_change_flags(struct net_device * dev,unsigned int flags,struct netlink_ext_ack * extack)62 int dev_change_flags(struct net_device *dev, unsigned int flags,
63 struct netlink_ext_ack *extack)
64 {
65 int ret;
66
67 netdev_lock_ops(dev);
68 ret = netif_change_flags(dev, flags, extack);
69 netdev_unlock_ops(dev);
70
71 return ret;
72 }
73 EXPORT_SYMBOL(dev_change_flags);
74
75 /**
76 * dev_set_group() - change group this device belongs to
77 * @dev: device
78 * @new_group: group this device should belong to
79 */
dev_set_group(struct net_device * dev,int new_group)80 void dev_set_group(struct net_device *dev, int new_group)
81 {
82 netdev_lock_ops(dev);
83 netif_set_group(dev, new_group);
84 netdev_unlock_ops(dev);
85 }
86
dev_set_mac_address_user(struct net_device * dev,struct sockaddr * sa,struct netlink_ext_ack * extack)87 int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa,
88 struct netlink_ext_ack *extack)
89 {
90 int ret;
91
92 down_write(&dev_addr_sem);
93 netdev_lock_ops(dev);
94 ret = netif_set_mac_address(dev, sa, extack);
95 netdev_unlock_ops(dev);
96 up_write(&dev_addr_sem);
97
98 return ret;
99 }
100 EXPORT_SYMBOL(dev_set_mac_address_user);
101
102 /**
103 * dev_change_net_namespace() - move device to different nethost namespace
104 * @dev: device
105 * @net: network namespace
106 * @pat: If not NULL name pattern to try if the current device name
107 * is already taken in the destination network namespace.
108 *
109 * This function shuts down a device interface and moves it
110 * to a new network namespace. On success 0 is returned, on
111 * a failure a netagive errno code is returned.
112 *
113 * Callers must hold the rtnl semaphore.
114 *
115 * Return: 0 on success, -errno on failure.
116 */
dev_change_net_namespace(struct net_device * dev,struct net * net,const char * pat)117 int dev_change_net_namespace(struct net_device *dev, struct net *net,
118 const char *pat)
119 {
120 int ret;
121
122 netdev_lock_ops(dev);
123 ret = netif_change_net_namespace(dev, net, pat, 0, NULL);
124 netdev_unlock_ops(dev);
125
126 return ret;
127 }
128 EXPORT_SYMBOL_GPL(dev_change_net_namespace);
129
130 /**
131 * dev_change_carrier() - change device carrier
132 * @dev: device
133 * @new_carrier: new value
134 *
135 * Change device carrier
136 *
137 * Return: 0 on success, -errno on failure.
138 */
dev_change_carrier(struct net_device * dev,bool new_carrier)139 int dev_change_carrier(struct net_device *dev, bool new_carrier)
140 {
141 int ret;
142
143 netdev_lock_ops(dev);
144 ret = netif_change_carrier(dev, new_carrier);
145 netdev_unlock_ops(dev);
146
147 return ret;
148 }
149
150 /**
151 * dev_change_tx_queue_len() - change TX queue length of a netdevice
152 * @dev: device
153 * @new_len: new tx queue length
154 *
155 * Return: 0 on success, -errno on failure.
156 */
dev_change_tx_queue_len(struct net_device * dev,unsigned long new_len)157 int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
158 {
159 int ret;
160
161 netdev_lock_ops(dev);
162 ret = netif_change_tx_queue_len(dev, new_len);
163 netdev_unlock_ops(dev);
164
165 return ret;
166 }
167
168 /**
169 * dev_change_proto_down() - set carrier according to proto_down
170 * @dev: device
171 * @proto_down: new value
172 *
173 * Return: 0 on success, -errno on failure.
174 */
dev_change_proto_down(struct net_device * dev,bool proto_down)175 int dev_change_proto_down(struct net_device *dev, bool proto_down)
176 {
177 int ret;
178
179 netdev_lock_ops(dev);
180 ret = netif_change_proto_down(dev, proto_down);
181 netdev_unlock_ops(dev);
182
183 return ret;
184 }
185
186 /**
187 * dev_open() - prepare an interface for use
188 * @dev: device to open
189 * @extack: netlink extended ack
190 *
191 * Takes a device from down to up state. The device's private open
192 * function is invoked and then the multicast lists are loaded. Finally
193 * the device is moved into the up state and a %NETDEV_UP message is
194 * sent to the netdev notifier chain.
195 *
196 * Calling this function on an active interface is a nop. On a failure
197 * a negative errno code is returned.
198 *
199 * Return: 0 on success, -errno on failure.
200 */
dev_open(struct net_device * dev,struct netlink_ext_ack * extack)201 int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
202 {
203 int ret;
204
205 netdev_lock_ops(dev);
206 ret = netif_open(dev, extack);
207 netdev_unlock_ops(dev);
208
209 return ret;
210 }
211 EXPORT_SYMBOL(dev_open);
212
213 /**
214 * dev_close() - shutdown an interface
215 * @dev: device to shutdown
216 *
217 * This function moves an active device into down state. A
218 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
219 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
220 * chain.
221 */
dev_close(struct net_device * dev)222 void dev_close(struct net_device *dev)
223 {
224 netdev_lock_ops(dev);
225 netif_close(dev);
226 netdev_unlock_ops(dev);
227 }
228 EXPORT_SYMBOL(dev_close);
229
dev_eth_ioctl(struct net_device * dev,struct ifreq * ifr,unsigned int cmd)230 int dev_eth_ioctl(struct net_device *dev,
231 struct ifreq *ifr, unsigned int cmd)
232 {
233 const struct net_device_ops *ops = dev->netdev_ops;
234 int ret = -ENODEV;
235
236 if (!ops->ndo_eth_ioctl)
237 return -EOPNOTSUPP;
238
239 netdev_lock_ops(dev);
240 if (netif_device_present(dev))
241 ret = ops->ndo_eth_ioctl(dev, ifr, cmd);
242 netdev_unlock_ops(dev);
243
244 return ret;
245 }
246 EXPORT_SYMBOL(dev_eth_ioctl);
247
dev_set_mtu(struct net_device * dev,int new_mtu)248 int dev_set_mtu(struct net_device *dev, int new_mtu)
249 {
250 int ret;
251
252 netdev_lock_ops(dev);
253 ret = netif_set_mtu(dev, new_mtu);
254 netdev_unlock_ops(dev);
255
256 return ret;
257 }
258 EXPORT_SYMBOL(dev_set_mtu);
259
260 /**
261 * dev_disable_lro() - disable Large Receive Offload on a device
262 * @dev: device
263 *
264 * Disable Large Receive Offload (LRO) on a net device. Must be
265 * called under RTNL. This is needed if received packets may be
266 * forwarded to another interface.
267 */
dev_disable_lro(struct net_device * dev)268 void dev_disable_lro(struct net_device *dev)
269 {
270 netdev_lock_ops(dev);
271 netif_disable_lro(dev);
272 netdev_unlock_ops(dev);
273 }
274 EXPORT_SYMBOL(dev_disable_lro);
275
276 /**
277 * dev_set_allmulti() - update allmulti count on a device
278 * @dev: device
279 * @inc: modifier
280 *
281 * Add or remove reception of all multicast frames to a device. While the
282 * count in the device remains above zero the interface remains listening
283 * to all interfaces. Once it hits zero the device reverts back to normal
284 * filtering operation. A negative @inc value is used to drop the counter
285 * when releasing a resource needing all multicasts.
286 *
287 * Return: 0 on success, -errno on failure.
288 */
289
dev_set_allmulti(struct net_device * dev,int inc)290 int dev_set_allmulti(struct net_device *dev, int inc)
291 {
292 int ret;
293
294 netdev_lock_ops(dev);
295 ret = netif_set_allmulti(dev, inc, true);
296 netdev_unlock_ops(dev);
297
298 return ret;
299 }
300 EXPORT_SYMBOL(dev_set_allmulti);
301
302 /**
303 * dev_set_mac_address() - change Media Access Control Address
304 * @dev: device
305 * @sa: new address
306 * @extack: netlink extended ack
307 *
308 * Change the hardware (MAC) address of the device
309 *
310 * Return: 0 on success, -errno on failure.
311 */
dev_set_mac_address(struct net_device * dev,struct sockaddr * sa,struct netlink_ext_ack * extack)312 int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
313 struct netlink_ext_ack *extack)
314 {
315 int ret;
316
317 netdev_lock_ops(dev);
318 ret = netif_set_mac_address(dev, sa, extack);
319 netdev_unlock_ops(dev);
320
321 return ret;
322 }
323 EXPORT_SYMBOL(dev_set_mac_address);
324
dev_xdp_propagate(struct net_device * dev,struct netdev_bpf * bpf)325 int dev_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
326 {
327 int ret;
328
329 netdev_lock_ops(dev);
330 ret = netif_xdp_propagate(dev, bpf);
331 netdev_unlock_ops(dev);
332
333 return ret;
334 }
335 EXPORT_SYMBOL_GPL(dev_xdp_propagate);
336