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_storage * ss,struct netlink_ext_ack * extack)87 int dev_set_mac_address_user(struct net_device *dev,
88 struct sockaddr_storage *ss,
89 struct netlink_ext_ack *extack)
90 {
91 int ret;
92
93 down_write(&dev_addr_sem);
94 netdev_lock_ops(dev);
95 ret = netif_set_mac_address(dev, ss, extack);
96 netdev_unlock_ops(dev);
97 up_write(&dev_addr_sem);
98
99 return ret;
100 }
101 EXPORT_SYMBOL(dev_set_mac_address_user);
102
103 /**
104 * dev_change_net_namespace() - move device to different nethost namespace
105 * @dev: device
106 * @net: network namespace
107 * @pat: If not NULL name pattern to try if the current device name
108 * is already taken in the destination network namespace.
109 *
110 * This function shuts down a device interface and moves it
111 * to a new network namespace. On success 0 is returned, on
112 * a failure a netagive errno code is returned.
113 *
114 * Callers must hold the rtnl semaphore.
115 *
116 * Return: 0 on success, -errno on failure.
117 */
dev_change_net_namespace(struct net_device * dev,struct net * net,const char * pat)118 int dev_change_net_namespace(struct net_device *dev, struct net *net,
119 const char *pat)
120 {
121 return __dev_change_net_namespace(dev, net, pat, 0, NULL);
122 }
123 EXPORT_SYMBOL_GPL(dev_change_net_namespace);
124
125 /**
126 * dev_change_carrier() - change device carrier
127 * @dev: device
128 * @new_carrier: new value
129 *
130 * Change device carrier
131 *
132 * Return: 0 on success, -errno on failure.
133 */
dev_change_carrier(struct net_device * dev,bool new_carrier)134 int dev_change_carrier(struct net_device *dev, bool new_carrier)
135 {
136 int ret;
137
138 netdev_lock_ops(dev);
139 ret = netif_change_carrier(dev, new_carrier);
140 netdev_unlock_ops(dev);
141
142 return ret;
143 }
144
145 /**
146 * dev_change_tx_queue_len() - change TX queue length of a netdevice
147 * @dev: device
148 * @new_len: new tx queue length
149 *
150 * Return: 0 on success, -errno on failure.
151 */
dev_change_tx_queue_len(struct net_device * dev,unsigned long new_len)152 int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
153 {
154 int ret;
155
156 netdev_lock_ops(dev);
157 ret = netif_change_tx_queue_len(dev, new_len);
158 netdev_unlock_ops(dev);
159
160 return ret;
161 }
162
163 /**
164 * dev_change_proto_down() - set carrier according to proto_down
165 * @dev: device
166 * @proto_down: new value
167 *
168 * Return: 0 on success, -errno on failure.
169 */
dev_change_proto_down(struct net_device * dev,bool proto_down)170 int dev_change_proto_down(struct net_device *dev, bool proto_down)
171 {
172 int ret;
173
174 netdev_lock_ops(dev);
175 ret = netif_change_proto_down(dev, proto_down);
176 netdev_unlock_ops(dev);
177
178 return ret;
179 }
180
181 /**
182 * dev_open() - prepare an interface for use
183 * @dev: device to open
184 * @extack: netlink extended ack
185 *
186 * Takes a device from down to up state. The device's private open
187 * function is invoked and then the multicast lists are loaded. Finally
188 * the device is moved into the up state and a %NETDEV_UP message is
189 * sent to the netdev notifier chain.
190 *
191 * Calling this function on an active interface is a nop. On a failure
192 * a negative errno code is returned.
193 *
194 * Return: 0 on success, -errno on failure.
195 */
dev_open(struct net_device * dev,struct netlink_ext_ack * extack)196 int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
197 {
198 int ret;
199
200 netdev_lock_ops(dev);
201 ret = netif_open(dev, extack);
202 netdev_unlock_ops(dev);
203
204 return ret;
205 }
206 EXPORT_SYMBOL(dev_open);
207
208 /**
209 * dev_close() - shutdown an interface
210 * @dev: device to shutdown
211 *
212 * This function moves an active device into down state. A
213 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
214 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
215 * chain.
216 */
dev_close(struct net_device * dev)217 void dev_close(struct net_device *dev)
218 {
219 netdev_lock_ops(dev);
220 netif_close(dev);
221 netdev_unlock_ops(dev);
222 }
223 EXPORT_SYMBOL(dev_close);
224
dev_eth_ioctl(struct net_device * dev,struct ifreq * ifr,unsigned int cmd)225 int dev_eth_ioctl(struct net_device *dev,
226 struct ifreq *ifr, unsigned int cmd)
227 {
228 const struct net_device_ops *ops = dev->netdev_ops;
229 int ret = -ENODEV;
230
231 if (!ops->ndo_eth_ioctl)
232 return -EOPNOTSUPP;
233
234 netdev_lock_ops(dev);
235 if (netif_device_present(dev))
236 ret = ops->ndo_eth_ioctl(dev, ifr, cmd);
237 netdev_unlock_ops(dev);
238
239 return ret;
240 }
241 EXPORT_SYMBOL(dev_eth_ioctl);
242
dev_set_mtu(struct net_device * dev,int new_mtu)243 int dev_set_mtu(struct net_device *dev, int new_mtu)
244 {
245 int ret;
246
247 netdev_lock_ops(dev);
248 ret = netif_set_mtu(dev, new_mtu);
249 netdev_unlock_ops(dev);
250
251 return ret;
252 }
253 EXPORT_SYMBOL(dev_set_mtu);
254
255 /**
256 * dev_disable_lro() - disable Large Receive Offload on a device
257 * @dev: device
258 *
259 * Disable Large Receive Offload (LRO) on a net device. Must be
260 * called under RTNL. This is needed if received packets may be
261 * forwarded to another interface.
262 */
dev_disable_lro(struct net_device * dev)263 void dev_disable_lro(struct net_device *dev)
264 {
265 netdev_lock_ops(dev);
266 netif_disable_lro(dev);
267 netdev_unlock_ops(dev);
268 }
269 EXPORT_SYMBOL(dev_disable_lro);
270
271 /**
272 * dev_set_promiscuity() - update promiscuity count on a device
273 * @dev: device
274 * @inc: modifier
275 *
276 * Add or remove promiscuity from a device. While the count in the device
277 * remains above zero the interface remains promiscuous. Once it hits zero
278 * the device reverts back to normal filtering operation. A negative inc
279 * value is used to drop promiscuity on the device.
280 * Return 0 if successful or a negative errno code on error.
281 */
dev_set_promiscuity(struct net_device * dev,int inc)282 int dev_set_promiscuity(struct net_device *dev, int inc)
283 {
284 int ret;
285
286 netdev_lock_ops(dev);
287 ret = netif_set_promiscuity(dev, inc);
288 netdev_unlock_ops(dev);
289
290 return ret;
291 }
292 EXPORT_SYMBOL(dev_set_promiscuity);
293
294 /**
295 * dev_set_allmulti() - update allmulti count on a device
296 * @dev: device
297 * @inc: modifier
298 *
299 * Add or remove reception of all multicast frames to a device. While the
300 * count in the device remains above zero the interface remains listening
301 * to all interfaces. Once it hits zero the device reverts back to normal
302 * filtering operation. A negative @inc value is used to drop the counter
303 * when releasing a resource needing all multicasts.
304 *
305 * Return: 0 on success, -errno on failure.
306 */
307
dev_set_allmulti(struct net_device * dev,int inc)308 int dev_set_allmulti(struct net_device *dev, int inc)
309 {
310 int ret;
311
312 netdev_lock_ops(dev);
313 ret = netif_set_allmulti(dev, inc, true);
314 netdev_unlock_ops(dev);
315
316 return ret;
317 }
318 EXPORT_SYMBOL(dev_set_allmulti);
319
320 /**
321 * dev_set_mac_address() - change Media Access Control Address
322 * @dev: device
323 * @ss: new address
324 * @extack: netlink extended ack
325 *
326 * Change the hardware (MAC) address of the device
327 *
328 * Return: 0 on success, -errno on failure.
329 */
dev_set_mac_address(struct net_device * dev,struct sockaddr_storage * ss,struct netlink_ext_ack * extack)330 int dev_set_mac_address(struct net_device *dev, struct sockaddr_storage *ss,
331 struct netlink_ext_ack *extack)
332 {
333 int ret;
334
335 netdev_lock_ops(dev);
336 ret = netif_set_mac_address(dev, ss, extack);
337 netdev_unlock_ops(dev);
338
339 return ret;
340 }
341 EXPORT_SYMBOL(dev_set_mac_address);
342
dev_xdp_propagate(struct net_device * dev,struct netdev_bpf * bpf)343 int dev_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
344 {
345 int ret;
346
347 netdev_lock_ops(dev);
348 ret = netif_xdp_propagate(dev, bpf);
349 netdev_unlock_ops(dev);
350
351 return ret;
352 }
353 EXPORT_SYMBOL_GPL(dev_xdp_propagate);
354
355 /**
356 * netdev_state_change() - device changes state
357 * @dev: device to cause notification
358 *
359 * Called to indicate a device has changed state. This function calls
360 * the notifier chains for netdev_chain and sends a NEWLINK message
361 * to the routing socket.
362 */
netdev_state_change(struct net_device * dev)363 void netdev_state_change(struct net_device *dev)
364 {
365 netdev_lock_ops(dev);
366 netif_state_change(dev);
367 netdev_unlock_ops(dev);
368 }
369 EXPORT_SYMBOL(netdev_state_change);
370