]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * INET An implementation of the TCP/IP protocol suite for the LINUX | |
3 | * operating system. INET is implemented using the BSD Socket | |
4 | * interface as the means of communication with the user level. | |
5 | * | |
6 | * Routing netlink socket interface: protocol independent part. | |
7 | * | |
8 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public License | |
12 | * as published by the Free Software Foundation; either version | |
13 | * 2 of the License, or (at your option) any later version. | |
14 | * | |
15 | * Fixes: | |
16 | * Vitaly E. Lavrov RTA_OK arithmetics was wrong. | |
17 | */ | |
18 | ||
ee5d032f | 19 | #include <linux/bitops.h> |
1da177e4 LT |
20 | #include <linux/errno.h> |
21 | #include <linux/module.h> | |
22 | #include <linux/types.h> | |
23 | #include <linux/socket.h> | |
24 | #include <linux/kernel.h> | |
1da177e4 LT |
25 | #include <linux/timer.h> |
26 | #include <linux/string.h> | |
27 | #include <linux/sockios.h> | |
28 | #include <linux/net.h> | |
29 | #include <linux/fcntl.h> | |
30 | #include <linux/mm.h> | |
31 | #include <linux/slab.h> | |
32 | #include <linux/interrupt.h> | |
33 | #include <linux/capability.h> | |
34 | #include <linux/skbuff.h> | |
35 | #include <linux/init.h> | |
36 | #include <linux/security.h> | |
6756ae4b | 37 | #include <linux/mutex.h> |
1823730f | 38 | #include <linux/if_addr.h> |
77162022 | 39 | #include <linux/if_bridge.h> |
f6f6424b | 40 | #include <linux/if_vlan.h> |
ebc08a6f | 41 | #include <linux/pci.h> |
77162022 | 42 | #include <linux/etherdevice.h> |
58038695 | 43 | #include <linux/bpf.h> |
1da177e4 | 44 | |
7c0f6ba6 | 45 | #include <linux/uaccess.h> |
1da177e4 LT |
46 | |
47 | #include <linux/inet.h> | |
48 | #include <linux/netdevice.h> | |
82f28412 | 49 | #include <net/switchdev.h> |
1da177e4 LT |
50 | #include <net/ip.h> |
51 | #include <net/protocol.h> | |
52 | #include <net/arp.h> | |
53 | #include <net/route.h> | |
54 | #include <net/udp.h> | |
ea697639 | 55 | #include <net/tcp.h> |
1da177e4 LT |
56 | #include <net/sock.h> |
57 | #include <net/pkt_sched.h> | |
14c0b97d | 58 | #include <net/fib_rules.h> |
e2849863 | 59 | #include <net/rtnetlink.h> |
30ffee84 | 60 | #include <net/net_namespace.h> |
1da177e4 | 61 | |
e0d087af | 62 | struct rtnl_link { |
e2849863 TG |
63 | rtnl_doit_func doit; |
64 | rtnl_dumpit_func dumpit; | |
62256f98 | 65 | unsigned int flags; |
e2849863 TG |
66 | }; |
67 | ||
6756ae4b | 68 | static DEFINE_MUTEX(rtnl_mutex); |
1da177e4 LT |
69 | |
70 | void rtnl_lock(void) | |
71 | { | |
6756ae4b | 72 | mutex_lock(&rtnl_mutex); |
1da177e4 | 73 | } |
e0d087af | 74 | EXPORT_SYMBOL(rtnl_lock); |
1da177e4 | 75 | |
1b5c5493 ED |
76 | static struct sk_buff *defer_kfree_skb_list; |
77 | void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) | |
78 | { | |
79 | if (head && tail) { | |
80 | tail->next = defer_kfree_skb_list; | |
81 | defer_kfree_skb_list = head; | |
82 | } | |
83 | } | |
84 | EXPORT_SYMBOL(rtnl_kfree_skbs); | |
85 | ||
6756ae4b | 86 | void __rtnl_unlock(void) |
1da177e4 | 87 | { |
1b5c5493 ED |
88 | struct sk_buff *head = defer_kfree_skb_list; |
89 | ||
90 | defer_kfree_skb_list = NULL; | |
91 | ||
6756ae4b | 92 | mutex_unlock(&rtnl_mutex); |
1b5c5493 ED |
93 | |
94 | while (head) { | |
95 | struct sk_buff *next = head->next; | |
96 | ||
97 | kfree_skb(head); | |
98 | cond_resched(); | |
99 | head = next; | |
100 | } | |
1da177e4 | 101 | } |
6756ae4b | 102 | |
1da177e4 LT |
103 | void rtnl_unlock(void) |
104 | { | |
58ec3b4d | 105 | /* This fellow will unlock it for us. */ |
1da177e4 LT |
106 | netdev_run_todo(); |
107 | } | |
e0d087af | 108 | EXPORT_SYMBOL(rtnl_unlock); |
1da177e4 | 109 | |
6756ae4b SH |
110 | int rtnl_trylock(void) |
111 | { | |
112 | return mutex_trylock(&rtnl_mutex); | |
113 | } | |
e0d087af | 114 | EXPORT_SYMBOL(rtnl_trylock); |
6756ae4b | 115 | |
c9c1014b PM |
116 | int rtnl_is_locked(void) |
117 | { | |
118 | return mutex_is_locked(&rtnl_mutex); | |
119 | } | |
e0d087af | 120 | EXPORT_SYMBOL(rtnl_is_locked); |
c9c1014b | 121 | |
a898def2 | 122 | #ifdef CONFIG_PROVE_LOCKING |
0cbf3343 | 123 | bool lockdep_rtnl_is_held(void) |
a898def2 PM |
124 | { |
125 | return lockdep_is_held(&rtnl_mutex); | |
126 | } | |
127 | EXPORT_SYMBOL(lockdep_rtnl_is_held); | |
128 | #endif /* #ifdef CONFIG_PROVE_LOCKING */ | |
129 | ||
6853dd48 | 130 | static struct rtnl_link __rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; |
019a3169 | 131 | static refcount_t rtnl_msg_handlers_ref[RTNL_FAMILY_MAX + 1]; |
e2849863 TG |
132 | |
133 | static inline int rtm_msgindex(int msgtype) | |
134 | { | |
135 | int msgindex = msgtype - RTM_BASE; | |
136 | ||
137 | /* | |
138 | * msgindex < 0 implies someone tried to register a netlink | |
139 | * control code. msgindex >= RTM_NR_MSGTYPES may indicate that | |
140 | * the message type has not been added to linux/rtnetlink.h | |
141 | */ | |
142 | BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); | |
143 | ||
144 | return msgindex; | |
145 | } | |
146 | ||
e2849863 TG |
147 | /** |
148 | * __rtnl_register - Register a rtnetlink message type | |
149 | * @protocol: Protocol family or PF_UNSPEC | |
150 | * @msgtype: rtnetlink message type | |
151 | * @doit: Function pointer called for each request message | |
152 | * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message | |
b97bac64 | 153 | * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions |
e2849863 TG |
154 | * |
155 | * Registers the specified function pointers (at least one of them has | |
156 | * to be non-NULL) to be called whenever a request message for the | |
157 | * specified protocol family and message type is received. | |
158 | * | |
159 | * The special protocol family PF_UNSPEC may be used to define fallback | |
160 | * function pointers for the case when no entry for the specific protocol | |
161 | * family exists. | |
162 | * | |
163 | * Returns 0 on success or a negative error code. | |
164 | */ | |
165 | int __rtnl_register(int protocol, int msgtype, | |
c7ac8679 | 166 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
b97bac64 | 167 | unsigned int flags) |
e2849863 TG |
168 | { |
169 | struct rtnl_link *tab; | |
170 | int msgindex; | |
171 | ||
25239cee | 172 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
e2849863 TG |
173 | msgindex = rtm_msgindex(msgtype); |
174 | ||
6853dd48 | 175 | tab = rcu_dereference(rtnl_msg_handlers[protocol]); |
e2849863 TG |
176 | if (tab == NULL) { |
177 | tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); | |
178 | if (tab == NULL) | |
179 | return -ENOBUFS; | |
180 | ||
6853dd48 | 181 | rcu_assign_pointer(rtnl_msg_handlers[protocol], tab); |
e2849863 TG |
182 | } |
183 | ||
184 | if (doit) | |
185 | tab[msgindex].doit = doit; | |
e2849863 TG |
186 | if (dumpit) |
187 | tab[msgindex].dumpit = dumpit; | |
62256f98 | 188 | tab[msgindex].flags |= flags; |
e2849863 TG |
189 | |
190 | return 0; | |
191 | } | |
e2849863 TG |
192 | EXPORT_SYMBOL_GPL(__rtnl_register); |
193 | ||
194 | /** | |
195 | * rtnl_register - Register a rtnetlink message type | |
196 | * | |
197 | * Identical to __rtnl_register() but panics on failure. This is useful | |
198 | * as failure of this function is very unlikely, it can only happen due | |
199 | * to lack of memory when allocating the chain to store all message | |
200 | * handlers for a protocol. Meant for use in init functions where lack | |
25985edc | 201 | * of memory implies no sense in continuing. |
e2849863 TG |
202 | */ |
203 | void rtnl_register(int protocol, int msgtype, | |
c7ac8679 | 204 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
b97bac64 | 205 | unsigned int flags) |
e2849863 | 206 | { |
b97bac64 | 207 | if (__rtnl_register(protocol, msgtype, doit, dumpit, flags) < 0) |
e2849863 TG |
208 | panic("Unable to register rtnetlink message handler, " |
209 | "protocol = %d, message type = %d\n", | |
210 | protocol, msgtype); | |
211 | } | |
e2849863 TG |
212 | EXPORT_SYMBOL_GPL(rtnl_register); |
213 | ||
214 | /** | |
215 | * rtnl_unregister - Unregister a rtnetlink message type | |
216 | * @protocol: Protocol family or PF_UNSPEC | |
217 | * @msgtype: rtnetlink message type | |
218 | * | |
219 | * Returns 0 on success or a negative error code. | |
220 | */ | |
221 | int rtnl_unregister(int protocol, int msgtype) | |
222 | { | |
6853dd48 | 223 | struct rtnl_link *handlers; |
e2849863 TG |
224 | int msgindex; |
225 | ||
25239cee | 226 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
e2849863 TG |
227 | msgindex = rtm_msgindex(msgtype); |
228 | ||
6853dd48 FW |
229 | rtnl_lock(); |
230 | handlers = rtnl_dereference(rtnl_msg_handlers[protocol]); | |
231 | if (!handlers) { | |
232 | rtnl_unlock(); | |
e2849863 | 233 | return -ENOENT; |
6853dd48 | 234 | } |
e2849863 | 235 | |
6853dd48 FW |
236 | handlers[msgindex].doit = NULL; |
237 | handlers[msgindex].dumpit = NULL; | |
62256f98 | 238 | handlers[msgindex].flags = 0; |
6853dd48 | 239 | rtnl_unlock(); |
e2849863 TG |
240 | |
241 | return 0; | |
242 | } | |
e2849863 TG |
243 | EXPORT_SYMBOL_GPL(rtnl_unregister); |
244 | ||
245 | /** | |
246 | * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol | |
247 | * @protocol : Protocol family or PF_UNSPEC | |
248 | * | |
249 | * Identical to calling rtnl_unregster() for all registered message types | |
250 | * of a certain protocol family. | |
251 | */ | |
252 | void rtnl_unregister_all(int protocol) | |
253 | { | |
019a3169 FW |
254 | struct rtnl_link *handlers; |
255 | ||
25239cee | 256 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
e2849863 | 257 | |
019a3169 | 258 | rtnl_lock(); |
6853dd48 FW |
259 | handlers = rtnl_dereference(rtnl_msg_handlers[protocol]); |
260 | RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL); | |
019a3169 FW |
261 | rtnl_unlock(); |
262 | ||
6853dd48 FW |
263 | synchronize_net(); |
264 | ||
019a3169 FW |
265 | while (refcount_read(&rtnl_msg_handlers_ref[protocol]) > 0) |
266 | schedule(); | |
267 | kfree(handlers); | |
e2849863 | 268 | } |
e2849863 | 269 | EXPORT_SYMBOL_GPL(rtnl_unregister_all); |
1da177e4 | 270 | |
38f7b870 PM |
271 | static LIST_HEAD(link_ops); |
272 | ||
c63044f0 ED |
273 | static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) |
274 | { | |
275 | const struct rtnl_link_ops *ops; | |
276 | ||
277 | list_for_each_entry(ops, &link_ops, list) { | |
278 | if (!strcmp(ops->kind, kind)) | |
279 | return ops; | |
280 | } | |
281 | return NULL; | |
282 | } | |
283 | ||
38f7b870 PM |
284 | /** |
285 | * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. | |
286 | * @ops: struct rtnl_link_ops * to register | |
287 | * | |
288 | * The caller must hold the rtnl_mutex. This function should be used | |
289 | * by drivers that create devices during module initialization. It | |
290 | * must be called before registering the devices. | |
291 | * | |
292 | * Returns 0 on success or a negative error code. | |
293 | */ | |
294 | int __rtnl_link_register(struct rtnl_link_ops *ops) | |
295 | { | |
c63044f0 ED |
296 | if (rtnl_link_ops_get(ops->kind)) |
297 | return -EEXIST; | |
298 | ||
b0ab2fab JP |
299 | /* The check for setup is here because if ops |
300 | * does not have that filled up, it is not possible | |
301 | * to use the ops for creating device. So do not | |
302 | * fill up dellink as well. That disables rtnl_dellink. | |
303 | */ | |
304 | if (ops->setup && !ops->dellink) | |
23289a37 | 305 | ops->dellink = unregister_netdevice_queue; |
2d85cba2 | 306 | |
38f7b870 PM |
307 | list_add_tail(&ops->list, &link_ops); |
308 | return 0; | |
309 | } | |
38f7b870 PM |
310 | EXPORT_SYMBOL_GPL(__rtnl_link_register); |
311 | ||
312 | /** | |
313 | * rtnl_link_register - Register rtnl_link_ops with rtnetlink. | |
314 | * @ops: struct rtnl_link_ops * to register | |
315 | * | |
316 | * Returns 0 on success or a negative error code. | |
317 | */ | |
318 | int rtnl_link_register(struct rtnl_link_ops *ops) | |
319 | { | |
320 | int err; | |
321 | ||
322 | rtnl_lock(); | |
323 | err = __rtnl_link_register(ops); | |
324 | rtnl_unlock(); | |
325 | return err; | |
326 | } | |
38f7b870 PM |
327 | EXPORT_SYMBOL_GPL(rtnl_link_register); |
328 | ||
669f87ba PE |
329 | static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) |
330 | { | |
331 | struct net_device *dev; | |
23289a37 ED |
332 | LIST_HEAD(list_kill); |
333 | ||
669f87ba | 334 | for_each_netdev(net, dev) { |
23289a37 ED |
335 | if (dev->rtnl_link_ops == ops) |
336 | ops->dellink(dev, &list_kill); | |
669f87ba | 337 | } |
23289a37 | 338 | unregister_netdevice_many(&list_kill); |
669f87ba PE |
339 | } |
340 | ||
38f7b870 PM |
341 | /** |
342 | * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. | |
343 | * @ops: struct rtnl_link_ops * to unregister | |
344 | * | |
2d85cba2 | 345 | * The caller must hold the rtnl_mutex. |
38f7b870 PM |
346 | */ |
347 | void __rtnl_link_unregister(struct rtnl_link_ops *ops) | |
348 | { | |
881d966b | 349 | struct net *net; |
2d85cba2 | 350 | |
881d966b | 351 | for_each_net(net) { |
669f87ba | 352 | __rtnl_kill_links(net, ops); |
2d85cba2 | 353 | } |
38f7b870 PM |
354 | list_del(&ops->list); |
355 | } | |
38f7b870 PM |
356 | EXPORT_SYMBOL_GPL(__rtnl_link_unregister); |
357 | ||
200b916f CW |
358 | /* Return with the rtnl_lock held when there are no network |
359 | * devices unregistering in any network namespace. | |
360 | */ | |
361 | static void rtnl_lock_unregistering_all(void) | |
362 | { | |
363 | struct net *net; | |
364 | bool unregistering; | |
ff960a73 | 365 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
200b916f | 366 | |
ff960a73 | 367 | add_wait_queue(&netdev_unregistering_wq, &wait); |
200b916f | 368 | for (;;) { |
200b916f CW |
369 | unregistering = false; |
370 | rtnl_lock(); | |
371 | for_each_net(net) { | |
372 | if (net->dev_unreg_count > 0) { | |
373 | unregistering = true; | |
374 | break; | |
375 | } | |
376 | } | |
377 | if (!unregistering) | |
378 | break; | |
379 | __rtnl_unlock(); | |
ff960a73 PZ |
380 | |
381 | wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); | |
200b916f | 382 | } |
ff960a73 | 383 | remove_wait_queue(&netdev_unregistering_wq, &wait); |
200b916f CW |
384 | } |
385 | ||
38f7b870 PM |
386 | /** |
387 | * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. | |
388 | * @ops: struct rtnl_link_ops * to unregister | |
389 | */ | |
390 | void rtnl_link_unregister(struct rtnl_link_ops *ops) | |
391 | { | |
200b916f CW |
392 | /* Close the race with cleanup_net() */ |
393 | mutex_lock(&net_mutex); | |
394 | rtnl_lock_unregistering_all(); | |
38f7b870 PM |
395 | __rtnl_link_unregister(ops); |
396 | rtnl_unlock(); | |
200b916f | 397 | mutex_unlock(&net_mutex); |
38f7b870 | 398 | } |
38f7b870 PM |
399 | EXPORT_SYMBOL_GPL(rtnl_link_unregister); |
400 | ||
ba7d49b1 JP |
401 | static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) |
402 | { | |
403 | struct net_device *master_dev; | |
404 | const struct rtnl_link_ops *ops; | |
405 | ||
406 | master_dev = netdev_master_upper_dev_get((struct net_device *) dev); | |
407 | if (!master_dev) | |
408 | return 0; | |
409 | ops = master_dev->rtnl_link_ops; | |
6049f253 | 410 | if (!ops || !ops->get_slave_size) |
ba7d49b1 JP |
411 | return 0; |
412 | /* IFLA_INFO_SLAVE_DATA + nested data */ | |
413 | return nla_total_size(sizeof(struct nlattr)) + | |
414 | ops->get_slave_size(master_dev, dev); | |
415 | } | |
416 | ||
38f7b870 PM |
417 | static size_t rtnl_link_get_size(const struct net_device *dev) |
418 | { | |
419 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; | |
420 | size_t size; | |
421 | ||
422 | if (!ops) | |
423 | return 0; | |
424 | ||
369cf77a TG |
425 | size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ |
426 | nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ | |
38f7b870 PM |
427 | |
428 | if (ops->get_size) | |
429 | /* IFLA_INFO_DATA + nested data */ | |
369cf77a | 430 | size += nla_total_size(sizeof(struct nlattr)) + |
38f7b870 PM |
431 | ops->get_size(dev); |
432 | ||
433 | if (ops->get_xstats_size) | |
369cf77a TG |
434 | /* IFLA_INFO_XSTATS */ |
435 | size += nla_total_size(ops->get_xstats_size(dev)); | |
38f7b870 | 436 | |
ba7d49b1 JP |
437 | size += rtnl_link_get_slave_info_data_size(dev); |
438 | ||
38f7b870 PM |
439 | return size; |
440 | } | |
441 | ||
f8ff182c TG |
442 | static LIST_HEAD(rtnl_af_ops); |
443 | ||
444 | static const struct rtnl_af_ops *rtnl_af_lookup(const int family) | |
445 | { | |
446 | const struct rtnl_af_ops *ops; | |
447 | ||
448 | list_for_each_entry(ops, &rtnl_af_ops, list) { | |
449 | if (ops->family == family) | |
450 | return ops; | |
451 | } | |
452 | ||
453 | return NULL; | |
454 | } | |
455 | ||
f8ff182c TG |
456 | /** |
457 | * rtnl_af_register - Register rtnl_af_ops with rtnetlink. | |
458 | * @ops: struct rtnl_af_ops * to register | |
459 | * | |
460 | * Returns 0 on success or a negative error code. | |
461 | */ | |
3678a9d8 | 462 | void rtnl_af_register(struct rtnl_af_ops *ops) |
f8ff182c | 463 | { |
f8ff182c | 464 | rtnl_lock(); |
3678a9d8 | 465 | list_add_tail(&ops->list, &rtnl_af_ops); |
f8ff182c | 466 | rtnl_unlock(); |
f8ff182c TG |
467 | } |
468 | EXPORT_SYMBOL_GPL(rtnl_af_register); | |
469 | ||
470 | /** | |
471 | * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. | |
472 | * @ops: struct rtnl_af_ops * to unregister | |
473 | * | |
474 | * The caller must hold the rtnl_mutex. | |
475 | */ | |
476 | void __rtnl_af_unregister(struct rtnl_af_ops *ops) | |
477 | { | |
478 | list_del(&ops->list); | |
479 | } | |
480 | EXPORT_SYMBOL_GPL(__rtnl_af_unregister); | |
481 | ||
482 | /** | |
483 | * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. | |
484 | * @ops: struct rtnl_af_ops * to unregister | |
485 | */ | |
486 | void rtnl_af_unregister(struct rtnl_af_ops *ops) | |
487 | { | |
488 | rtnl_lock(); | |
489 | __rtnl_af_unregister(ops); | |
490 | rtnl_unlock(); | |
491 | } | |
492 | EXPORT_SYMBOL_GPL(rtnl_af_unregister); | |
493 | ||
b1974ed0 AR |
494 | static size_t rtnl_link_get_af_size(const struct net_device *dev, |
495 | u32 ext_filter_mask) | |
f8ff182c TG |
496 | { |
497 | struct rtnl_af_ops *af_ops; | |
498 | size_t size; | |
499 | ||
500 | /* IFLA_AF_SPEC */ | |
501 | size = nla_total_size(sizeof(struct nlattr)); | |
502 | ||
503 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { | |
504 | if (af_ops->get_link_af_size) { | |
505 | /* AF_* + nested data */ | |
506 | size += nla_total_size(sizeof(struct nlattr)) + | |
b1974ed0 | 507 | af_ops->get_link_af_size(dev, ext_filter_mask); |
f8ff182c TG |
508 | } |
509 | } | |
510 | ||
511 | return size; | |
512 | } | |
513 | ||
ba7d49b1 | 514 | static bool rtnl_have_link_slave_info(const struct net_device *dev) |
38f7b870 | 515 | { |
ba7d49b1 | 516 | struct net_device *master_dev; |
38f7b870 | 517 | |
ba7d49b1 | 518 | master_dev = netdev_master_upper_dev_get((struct net_device *) dev); |
813f020c | 519 | if (master_dev && master_dev->rtnl_link_ops) |
ba7d49b1 JP |
520 | return true; |
521 | return false; | |
522 | } | |
523 | ||
524 | static int rtnl_link_slave_info_fill(struct sk_buff *skb, | |
525 | const struct net_device *dev) | |
526 | { | |
527 | struct net_device *master_dev; | |
528 | const struct rtnl_link_ops *ops; | |
529 | struct nlattr *slave_data; | |
530 | int err; | |
38f7b870 | 531 | |
ba7d49b1 JP |
532 | master_dev = netdev_master_upper_dev_get((struct net_device *) dev); |
533 | if (!master_dev) | |
534 | return 0; | |
535 | ops = master_dev->rtnl_link_ops; | |
536 | if (!ops) | |
537 | return 0; | |
538 | if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) | |
539 | return -EMSGSIZE; | |
540 | if (ops->fill_slave_info) { | |
541 | slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA); | |
542 | if (!slave_data) | |
543 | return -EMSGSIZE; | |
544 | err = ops->fill_slave_info(skb, master_dev, dev); | |
545 | if (err < 0) | |
546 | goto err_cancel_slave_data; | |
547 | nla_nest_end(skb, slave_data); | |
548 | } | |
549 | return 0; | |
550 | ||
551 | err_cancel_slave_data: | |
552 | nla_nest_cancel(skb, slave_data); | |
553 | return err; | |
554 | } | |
555 | ||
556 | static int rtnl_link_info_fill(struct sk_buff *skb, | |
557 | const struct net_device *dev) | |
558 | { | |
559 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; | |
560 | struct nlattr *data; | |
561 | int err; | |
562 | ||
563 | if (!ops) | |
564 | return 0; | |
38f7b870 | 565 | if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) |
ba7d49b1 | 566 | return -EMSGSIZE; |
38f7b870 PM |
567 | if (ops->fill_xstats) { |
568 | err = ops->fill_xstats(skb, dev); | |
569 | if (err < 0) | |
ba7d49b1 | 570 | return err; |
38f7b870 PM |
571 | } |
572 | if (ops->fill_info) { | |
573 | data = nla_nest_start(skb, IFLA_INFO_DATA); | |
ba7d49b1 JP |
574 | if (data == NULL) |
575 | return -EMSGSIZE; | |
38f7b870 PM |
576 | err = ops->fill_info(skb, dev); |
577 | if (err < 0) | |
578 | goto err_cancel_data; | |
579 | nla_nest_end(skb, data); | |
580 | } | |
38f7b870 PM |
581 | return 0; |
582 | ||
583 | err_cancel_data: | |
584 | nla_nest_cancel(skb, data); | |
ba7d49b1 JP |
585 | return err; |
586 | } | |
587 | ||
588 | static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) | |
589 | { | |
590 | struct nlattr *linkinfo; | |
591 | int err = -EMSGSIZE; | |
592 | ||
593 | linkinfo = nla_nest_start(skb, IFLA_LINKINFO); | |
594 | if (linkinfo == NULL) | |
595 | goto out; | |
596 | ||
597 | err = rtnl_link_info_fill(skb, dev); | |
598 | if (err < 0) | |
599 | goto err_cancel_link; | |
600 | ||
601 | err = rtnl_link_slave_info_fill(skb, dev); | |
602 | if (err < 0) | |
603 | goto err_cancel_link; | |
604 | ||
605 | nla_nest_end(skb, linkinfo); | |
606 | return 0; | |
607 | ||
38f7b870 PM |
608 | err_cancel_link: |
609 | nla_nest_cancel(skb, linkinfo); | |
610 | out: | |
611 | return err; | |
612 | } | |
613 | ||
95c96174 | 614 | int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) |
1da177e4 | 615 | { |
97c53cac | 616 | struct sock *rtnl = net->rtnl; |
1da177e4 LT |
617 | int err = 0; |
618 | ||
ac6d439d | 619 | NETLINK_CB(skb).dst_group = group; |
1da177e4 | 620 | if (echo) |
63354797 | 621 | refcount_inc(&skb->users); |
1da177e4 LT |
622 | netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); |
623 | if (echo) | |
624 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); | |
625 | return err; | |
626 | } | |
627 | ||
97c53cac | 628 | int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) |
2942e900 | 629 | { |
97c53cac DL |
630 | struct sock *rtnl = net->rtnl; |
631 | ||
2942e900 TG |
632 | return nlmsg_unicast(rtnl, skb, pid); |
633 | } | |
e0d087af | 634 | EXPORT_SYMBOL(rtnl_unicast); |
2942e900 | 635 | |
1ce85fe4 PNA |
636 | void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, |
637 | struct nlmsghdr *nlh, gfp_t flags) | |
97676b6b | 638 | { |
97c53cac | 639 | struct sock *rtnl = net->rtnl; |
97676b6b TG |
640 | int report = 0; |
641 | ||
642 | if (nlh) | |
643 | report = nlmsg_report(nlh); | |
644 | ||
1ce85fe4 | 645 | nlmsg_notify(rtnl, skb, pid, group, report, flags); |
97676b6b | 646 | } |
e0d087af | 647 | EXPORT_SYMBOL(rtnl_notify); |
97676b6b | 648 | |
97c53cac | 649 | void rtnl_set_sk_err(struct net *net, u32 group, int error) |
97676b6b | 650 | { |
97c53cac DL |
651 | struct sock *rtnl = net->rtnl; |
652 | ||
97676b6b TG |
653 | netlink_set_err(rtnl, 0, group, error); |
654 | } | |
e0d087af | 655 | EXPORT_SYMBOL(rtnl_set_sk_err); |
97676b6b | 656 | |
1da177e4 LT |
657 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) |
658 | { | |
2d7202bf TG |
659 | struct nlattr *mx; |
660 | int i, valid = 0; | |
661 | ||
662 | mx = nla_nest_start(skb, RTA_METRICS); | |
663 | if (mx == NULL) | |
664 | return -ENOBUFS; | |
665 | ||
666 | for (i = 0; i < RTAX_MAX; i++) { | |
667 | if (metrics[i]) { | |
ea697639 DB |
668 | if (i == RTAX_CC_ALGO - 1) { |
669 | char tmp[TCP_CA_NAME_MAX], *name; | |
670 | ||
671 | name = tcp_ca_get_name_by_key(metrics[i], tmp); | |
672 | if (!name) | |
673 | continue; | |
674 | if (nla_put_string(skb, i + 1, name)) | |
675 | goto nla_put_failure; | |
c3a8d947 DB |
676 | } else if (i == RTAX_FEATURES - 1) { |
677 | u32 user_features = metrics[i] & RTAX_FEATURE_MASK; | |
678 | ||
f8edcd12 PS |
679 | if (!user_features) |
680 | continue; | |
c3a8d947 DB |
681 | BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); |
682 | if (nla_put_u32(skb, i + 1, user_features)) | |
683 | goto nla_put_failure; | |
ea697639 DB |
684 | } else { |
685 | if (nla_put_u32(skb, i + 1, metrics[i])) | |
686 | goto nla_put_failure; | |
687 | } | |
2d7202bf | 688 | valid++; |
2d7202bf | 689 | } |
1da177e4 | 690 | } |
1da177e4 | 691 | |
a57d27fc DM |
692 | if (!valid) { |
693 | nla_nest_cancel(skb, mx); | |
694 | return 0; | |
695 | } | |
2d7202bf TG |
696 | |
697 | return nla_nest_end(skb, mx); | |
698 | ||
699 | nla_put_failure: | |
bc3ed28c TG |
700 | nla_nest_cancel(skb, mx); |
701 | return -EMSGSIZE; | |
1da177e4 | 702 | } |
e0d087af | 703 | EXPORT_SYMBOL(rtnetlink_put_metrics); |
1da177e4 | 704 | |
e3703b3d | 705 | int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, |
87a50699 | 706 | long expires, u32 error) |
e3703b3d TG |
707 | { |
708 | struct rta_cacheinfo ci = { | |
a399a805 | 709 | .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse), |
e3703b3d TG |
710 | .rta_used = dst->__use, |
711 | .rta_clntref = atomic_read(&(dst->__refcnt)), | |
712 | .rta_error = error, | |
713 | .rta_id = id, | |
e3703b3d TG |
714 | }; |
715 | ||
8253947e LW |
716 | if (expires) { |
717 | unsigned long clock; | |
e3703b3d | 718 | |
8253947e LW |
719 | clock = jiffies_to_clock_t(abs(expires)); |
720 | clock = min_t(unsigned long, clock, INT_MAX); | |
721 | ci.rta_expires = (expires > 0) ? clock : -clock; | |
722 | } | |
e3703b3d TG |
723 | return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); |
724 | } | |
e3703b3d | 725 | EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); |
1da177e4 | 726 | |
93b2d4a2 | 727 | static void set_operstate(struct net_device *dev, unsigned char transition) |
b00055aa SR |
728 | { |
729 | unsigned char operstate = dev->operstate; | |
730 | ||
e0d087af | 731 | switch (transition) { |
b00055aa SR |
732 | case IF_OPER_UP: |
733 | if ((operstate == IF_OPER_DORMANT || | |
734 | operstate == IF_OPER_UNKNOWN) && | |
735 | !netif_dormant(dev)) | |
736 | operstate = IF_OPER_UP; | |
737 | break; | |
738 | ||
739 | case IF_OPER_DORMANT: | |
740 | if (operstate == IF_OPER_UP || | |
741 | operstate == IF_OPER_UNKNOWN) | |
742 | operstate = IF_OPER_DORMANT; | |
743 | break; | |
3ff50b79 | 744 | } |
b00055aa SR |
745 | |
746 | if (dev->operstate != operstate) { | |
747 | write_lock_bh(&dev_base_lock); | |
748 | dev->operstate = operstate; | |
749 | write_unlock_bh(&dev_base_lock); | |
93b2d4a2 DM |
750 | netdev_state_change(dev); |
751 | } | |
b00055aa SR |
752 | } |
753 | ||
b1beb681 JB |
754 | static unsigned int rtnl_dev_get_flags(const struct net_device *dev) |
755 | { | |
756 | return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | | |
757 | (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); | |
758 | } | |
759 | ||
3729d502 PM |
760 | static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, |
761 | const struct ifinfomsg *ifm) | |
762 | { | |
763 | unsigned int flags = ifm->ifi_flags; | |
764 | ||
765 | /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ | |
766 | if (ifm->ifi_change) | |
767 | flags = (flags & ifm->ifi_change) | | |
b1beb681 | 768 | (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); |
3729d502 PM |
769 | |
770 | return flags; | |
771 | } | |
772 | ||
b60c5115 | 773 | static void copy_rtnl_link_stats(struct rtnl_link_stats *a, |
be1f3c2c | 774 | const struct rtnl_link_stats64 *b) |
1da177e4 | 775 | { |
b60c5115 TG |
776 | a->rx_packets = b->rx_packets; |
777 | a->tx_packets = b->tx_packets; | |
778 | a->rx_bytes = b->rx_bytes; | |
779 | a->tx_bytes = b->tx_bytes; | |
780 | a->rx_errors = b->rx_errors; | |
781 | a->tx_errors = b->tx_errors; | |
782 | a->rx_dropped = b->rx_dropped; | |
783 | a->tx_dropped = b->tx_dropped; | |
784 | ||
785 | a->multicast = b->multicast; | |
786 | a->collisions = b->collisions; | |
787 | ||
788 | a->rx_length_errors = b->rx_length_errors; | |
789 | a->rx_over_errors = b->rx_over_errors; | |
790 | a->rx_crc_errors = b->rx_crc_errors; | |
791 | a->rx_frame_errors = b->rx_frame_errors; | |
792 | a->rx_fifo_errors = b->rx_fifo_errors; | |
793 | a->rx_missed_errors = b->rx_missed_errors; | |
794 | ||
795 | a->tx_aborted_errors = b->tx_aborted_errors; | |
796 | a->tx_carrier_errors = b->tx_carrier_errors; | |
797 | a->tx_fifo_errors = b->tx_fifo_errors; | |
798 | a->tx_heartbeat_errors = b->tx_heartbeat_errors; | |
799 | a->tx_window_errors = b->tx_window_errors; | |
800 | ||
801 | a->rx_compressed = b->rx_compressed; | |
802 | a->tx_compressed = b->tx_compressed; | |
6e7333d3 JW |
803 | |
804 | a->rx_nohandler = b->rx_nohandler; | |
10708f37 JE |
805 | } |
806 | ||
c02db8c6 | 807 | /* All VF info */ |
115c9b81 GR |
808 | static inline int rtnl_vfinfo_size(const struct net_device *dev, |
809 | u32 ext_filter_mask) | |
ebc08a6f | 810 | { |
9af15c38 | 811 | if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { |
c02db8c6 | 812 | int num_vfs = dev_num_vf(dev->dev.parent); |
7e75f74a | 813 | size_t size = nla_total_size(0); |
045de01a | 814 | size += num_vfs * |
7e75f74a SD |
815 | (nla_total_size(0) + |
816 | nla_total_size(sizeof(struct ifla_vf_mac)) + | |
817 | nla_total_size(sizeof(struct ifla_vf_vlan)) + | |
818 | nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ | |
79aab093 MS |
819 | nla_total_size(MAX_VLAN_LIST_LEN * |
820 | sizeof(struct ifla_vf_vlan_info)) + | |
ed616689 | 821 | nla_total_size(sizeof(struct ifla_vf_spoofchk)) + |
7e75f74a | 822 | nla_total_size(sizeof(struct ifla_vf_tx_rate)) + |
945a3676 | 823 | nla_total_size(sizeof(struct ifla_vf_rate)) + |
01a3d796 | 824 | nla_total_size(sizeof(struct ifla_vf_link_state)) + |
3b766cd8 | 825 | nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + |
7e75f74a | 826 | nla_total_size(0) + /* nest IFLA_VF_STATS */ |
3b766cd8 | 827 | /* IFLA_VF_STATS_RX_PACKETS */ |
343a6d8e | 828 | nla_total_size_64bit(sizeof(__u64)) + |
3b766cd8 | 829 | /* IFLA_VF_STATS_TX_PACKETS */ |
343a6d8e | 830 | nla_total_size_64bit(sizeof(__u64)) + |
3b766cd8 | 831 | /* IFLA_VF_STATS_RX_BYTES */ |
343a6d8e | 832 | nla_total_size_64bit(sizeof(__u64)) + |
3b766cd8 | 833 | /* IFLA_VF_STATS_TX_BYTES */ |
343a6d8e | 834 | nla_total_size_64bit(sizeof(__u64)) + |
3b766cd8 | 835 | /* IFLA_VF_STATS_BROADCAST */ |
343a6d8e | 836 | nla_total_size_64bit(sizeof(__u64)) + |
3b766cd8 | 837 | /* IFLA_VF_STATS_MULTICAST */ |
343a6d8e | 838 | nla_total_size_64bit(sizeof(__u64)) + |
dd461d6a | 839 | nla_total_size(sizeof(struct ifla_vf_trust))); |
c02db8c6 CW |
840 | return size; |
841 | } else | |
ebc08a6f WM |
842 | return 0; |
843 | } | |
844 | ||
c53864fd DG |
845 | static size_t rtnl_port_size(const struct net_device *dev, |
846 | u32 ext_filter_mask) | |
57b61080 SF |
847 | { |
848 | size_t port_size = nla_total_size(4) /* PORT_VF */ | |
849 | + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ | |
57b61080 SF |
850 | + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ |
851 | + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ | |
852 | + nla_total_size(1) /* PROT_VDP_REQUEST */ | |
853 | + nla_total_size(2); /* PORT_VDP_RESPONSE */ | |
854 | size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); | |
855 | size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) | |
856 | + port_size; | |
857 | size_t port_self_size = nla_total_size(sizeof(struct nlattr)) | |
858 | + port_size; | |
859 | ||
c53864fd DG |
860 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
861 | !(ext_filter_mask & RTEXT_FILTER_VF)) | |
57b61080 SF |
862 | return 0; |
863 | if (dev_num_vf(dev->dev.parent)) | |
864 | return port_self_size + vf_ports_size + | |
865 | vf_port_size * dev_num_vf(dev->dev.parent); | |
866 | else | |
867 | return port_self_size; | |
868 | } | |
869 | ||
b5cdae32 | 870 | static size_t rtnl_xdp_size(void) |
d1fdd913 | 871 | { |
b3cfaa31 | 872 | size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ |
58038695 MKL |
873 | nla_total_size(1) + /* XDP_ATTACHED */ |
874 | nla_total_size(4); /* XDP_PROG_ID */ | |
d1fdd913 | 875 | |
b5cdae32 | 876 | return xdp_size; |
d1fdd913 BB |
877 | } |
878 | ||
115c9b81 GR |
879 | static noinline size_t if_nlmsg_size(const struct net_device *dev, |
880 | u32 ext_filter_mask) | |
339bf98f TG |
881 | { |
882 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
883 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ | |
0b815a1a | 884 | + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ |
339bf98f | 885 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ |
270cb4d0 | 886 | + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) |
339bf98f | 887 | + nla_total_size(sizeof(struct rtnl_link_stats)) |
35c58459 | 888 | + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) |
339bf98f TG |
889 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
890 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ | |
891 | + nla_total_size(4) /* IFLA_TXQLEN */ | |
892 | + nla_total_size(4) /* IFLA_WEIGHT */ | |
893 | + nla_total_size(4) /* IFLA_MTU */ | |
894 | + nla_total_size(4) /* IFLA_LINK */ | |
895 | + nla_total_size(4) /* IFLA_MASTER */ | |
9a57247f | 896 | + nla_total_size(1) /* IFLA_CARRIER */ |
edbc0bb3 | 897 | + nla_total_size(4) /* IFLA_PROMISCUITY */ |
76ff5cc9 JP |
898 | + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ |
899 | + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ | |
6919756c TK |
900 | + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ |
901 | + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ | |
339bf98f | 902 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
38f7b870 | 903 | + nla_total_size(1) /* IFLA_LINKMODE */ |
2d3b479d | 904 | + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ |
d37512a2 | 905 | + nla_total_size(4) /* IFLA_LINK_NETNSID */ |
db833d40 | 906 | + nla_total_size(4) /* IFLA_GROUP */ |
115c9b81 GR |
907 | + nla_total_size(ext_filter_mask |
908 | & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ | |
909 | + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ | |
c53864fd | 910 | + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ |
f8ff182c | 911 | + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ |
b1974ed0 | 912 | + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ |
82f28412 | 913 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ |
88d6378b | 914 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ |
c57c7a95 | 915 | + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ |
b5cdae32 | 916 | + rtnl_xdp_size() /* IFLA_XDP */ |
3d3ea5af | 917 | + nla_total_size(4) /* IFLA_EVENT */ |
88d6378b AK |
918 | + nla_total_size(1); /* IFLA_PROTO_DOWN */ |
919 | ||
339bf98f TG |
920 | } |
921 | ||
57b61080 SF |
922 | static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) |
923 | { | |
924 | struct nlattr *vf_ports; | |
925 | struct nlattr *vf_port; | |
926 | int vf; | |
927 | int err; | |
928 | ||
929 | vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); | |
930 | if (!vf_ports) | |
931 | return -EMSGSIZE; | |
932 | ||
933 | for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { | |
934 | vf_port = nla_nest_start(skb, IFLA_VF_PORT); | |
8ca94183 SF |
935 | if (!vf_port) |
936 | goto nla_put_failure; | |
a6574349 DM |
937 | if (nla_put_u32(skb, IFLA_PORT_VF, vf)) |
938 | goto nla_put_failure; | |
57b61080 | 939 | err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); |
8ca94183 SF |
940 | if (err == -EMSGSIZE) |
941 | goto nla_put_failure; | |
57b61080 | 942 | if (err) { |
57b61080 SF |
943 | nla_nest_cancel(skb, vf_port); |
944 | continue; | |
945 | } | |
946 | nla_nest_end(skb, vf_port); | |
947 | } | |
948 | ||
949 | nla_nest_end(skb, vf_ports); | |
950 | ||
951 | return 0; | |
8ca94183 SF |
952 | |
953 | nla_put_failure: | |
954 | nla_nest_cancel(skb, vf_ports); | |
955 | return -EMSGSIZE; | |
57b61080 SF |
956 | } |
957 | ||
958 | static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) | |
959 | { | |
960 | struct nlattr *port_self; | |
961 | int err; | |
962 | ||
963 | port_self = nla_nest_start(skb, IFLA_PORT_SELF); | |
964 | if (!port_self) | |
965 | return -EMSGSIZE; | |
966 | ||
967 | err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); | |
968 | if (err) { | |
969 | nla_nest_cancel(skb, port_self); | |
8ca94183 | 970 | return (err == -EMSGSIZE) ? err : 0; |
57b61080 SF |
971 | } |
972 | ||
973 | nla_nest_end(skb, port_self); | |
974 | ||
975 | return 0; | |
976 | } | |
977 | ||
c53864fd DG |
978 | static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, |
979 | u32 ext_filter_mask) | |
57b61080 SF |
980 | { |
981 | int err; | |
982 | ||
c53864fd DG |
983 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
984 | !(ext_filter_mask & RTEXT_FILTER_VF)) | |
57b61080 SF |
985 | return 0; |
986 | ||
987 | err = rtnl_port_self_fill(skb, dev); | |
988 | if (err) | |
989 | return err; | |
990 | ||
991 | if (dev_num_vf(dev->dev.parent)) { | |
992 | err = rtnl_vf_ports_fill(skb, dev); | |
993 | if (err) | |
994 | return err; | |
995 | } | |
996 | ||
997 | return 0; | |
998 | } | |
999 | ||
66cae9ed JP |
1000 | static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) |
1001 | { | |
1002 | int err; | |
02637fce | 1003 | struct netdev_phys_item_id ppid; |
66cae9ed JP |
1004 | |
1005 | err = dev_get_phys_port_id(dev, &ppid); | |
1006 | if (err) { | |
1007 | if (err == -EOPNOTSUPP) | |
1008 | return 0; | |
1009 | return err; | |
1010 | } | |
1011 | ||
1012 | if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) | |
1013 | return -EMSGSIZE; | |
1014 | ||
1015 | return 0; | |
1016 | } | |
1017 | ||
db24a904 DA |
1018 | static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) |
1019 | { | |
1020 | char name[IFNAMSIZ]; | |
1021 | int err; | |
1022 | ||
1023 | err = dev_get_phys_port_name(dev, name, sizeof(name)); | |
1024 | if (err) { | |
1025 | if (err == -EOPNOTSUPP) | |
1026 | return 0; | |
1027 | return err; | |
1028 | } | |
1029 | ||
77ef033b | 1030 | if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) |
db24a904 DA |
1031 | return -EMSGSIZE; |
1032 | ||
1033 | return 0; | |
1034 | } | |
1035 | ||
82f28412 JP |
1036 | static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) |
1037 | { | |
1038 | int err; | |
f8e20a9f | 1039 | struct switchdev_attr attr = { |
6ff64f6f | 1040 | .orig_dev = dev, |
1f868398 | 1041 | .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, |
f8e20a9f SF |
1042 | .flags = SWITCHDEV_F_NO_RECURSE, |
1043 | }; | |
82f28412 | 1044 | |
f8e20a9f | 1045 | err = switchdev_port_attr_get(dev, &attr); |
82f28412 JP |
1046 | if (err) { |
1047 | if (err == -EOPNOTSUPP) | |
1048 | return 0; | |
1049 | return err; | |
1050 | } | |
1051 | ||
42275bd8 SF |
1052 | if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, |
1053 | attr.u.ppid.id)) | |
82f28412 JP |
1054 | return -EMSGSIZE; |
1055 | ||
1056 | return 0; | |
1057 | } | |
1058 | ||
b22b941b HFS |
1059 | static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, |
1060 | struct net_device *dev) | |
1061 | { | |
550bce59 | 1062 | struct rtnl_link_stats64 *sp; |
b22b941b | 1063 | struct nlattr *attr; |
18402843 | 1064 | |
58414d32 ND |
1065 | attr = nla_reserve_64bit(skb, IFLA_STATS64, |
1066 | sizeof(struct rtnl_link_stats64), IFLA_PAD); | |
b22b941b HFS |
1067 | if (!attr) |
1068 | return -EMSGSIZE; | |
1069 | ||
550bce59 RP |
1070 | sp = nla_data(attr); |
1071 | dev_get_stats(dev, sp); | |
b22b941b | 1072 | |
550bce59 RP |
1073 | attr = nla_reserve(skb, IFLA_STATS, |
1074 | sizeof(struct rtnl_link_stats)); | |
b22b941b HFS |
1075 | if (!attr) |
1076 | return -EMSGSIZE; | |
1077 | ||
550bce59 | 1078 | copy_rtnl_link_stats(nla_data(attr), sp); |
b22b941b HFS |
1079 | |
1080 | return 0; | |
1081 | } | |
1082 | ||
1083 | static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, | |
1084 | struct net_device *dev, | |
1085 | int vfs_num, | |
1086 | struct nlattr *vfinfo) | |
1087 | { | |
1088 | struct ifla_vf_rss_query_en vf_rss_query_en; | |
79aab093 | 1089 | struct nlattr *vf, *vfstats, *vfvlanlist; |
b22b941b | 1090 | struct ifla_vf_link_state vf_linkstate; |
79aab093 | 1091 | struct ifla_vf_vlan_info vf_vlan_info; |
b22b941b HFS |
1092 | struct ifla_vf_spoofchk vf_spoofchk; |
1093 | struct ifla_vf_tx_rate vf_tx_rate; | |
1094 | struct ifla_vf_stats vf_stats; | |
1095 | struct ifla_vf_trust vf_trust; | |
1096 | struct ifla_vf_vlan vf_vlan; | |
1097 | struct ifla_vf_rate vf_rate; | |
b22b941b HFS |
1098 | struct ifla_vf_mac vf_mac; |
1099 | struct ifla_vf_info ivi; | |
1100 | ||
0eed9cf5 MY |
1101 | memset(&ivi, 0, sizeof(ivi)); |
1102 | ||
b22b941b HFS |
1103 | /* Not all SR-IOV capable drivers support the |
1104 | * spoofcheck and "RSS query enable" query. Preset to | |
1105 | * -1 so the user space tool can detect that the driver | |
1106 | * didn't report anything. | |
1107 | */ | |
1108 | ivi.spoofchk = -1; | |
1109 | ivi.rss_query_en = -1; | |
1110 | ivi.trusted = -1; | |
b22b941b HFS |
1111 | /* The default value for VF link state is "auto" |
1112 | * IFLA_VF_LINK_STATE_AUTO which equals zero | |
1113 | */ | |
1114 | ivi.linkstate = 0; | |
79aab093 MS |
1115 | /* VLAN Protocol by default is 802.1Q */ |
1116 | ivi.vlan_proto = htons(ETH_P_8021Q); | |
b22b941b HFS |
1117 | if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) |
1118 | return 0; | |
1119 | ||
775f4f05 DC |
1120 | memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); |
1121 | ||
b22b941b HFS |
1122 | vf_mac.vf = |
1123 | vf_vlan.vf = | |
79aab093 | 1124 | vf_vlan_info.vf = |
b22b941b HFS |
1125 | vf_rate.vf = |
1126 | vf_tx_rate.vf = | |
1127 | vf_spoofchk.vf = | |
1128 | vf_linkstate.vf = | |
1129 | vf_rss_query_en.vf = | |
1130 | vf_trust.vf = ivi.vf; | |
1131 | ||
1132 | memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); | |
1133 | vf_vlan.vlan = ivi.vlan; | |
1134 | vf_vlan.qos = ivi.qos; | |
79aab093 MS |
1135 | vf_vlan_info.vlan = ivi.vlan; |
1136 | vf_vlan_info.qos = ivi.qos; | |
1137 | vf_vlan_info.vlan_proto = ivi.vlan_proto; | |
b22b941b HFS |
1138 | vf_tx_rate.rate = ivi.max_tx_rate; |
1139 | vf_rate.min_tx_rate = ivi.min_tx_rate; | |
1140 | vf_rate.max_tx_rate = ivi.max_tx_rate; | |
1141 | vf_spoofchk.setting = ivi.spoofchk; | |
1142 | vf_linkstate.link_state = ivi.linkstate; | |
1143 | vf_rss_query_en.setting = ivi.rss_query_en; | |
1144 | vf_trust.setting = ivi.trusted; | |
1145 | vf = nla_nest_start(skb, IFLA_VF_INFO); | |
79aab093 MS |
1146 | if (!vf) |
1147 | goto nla_put_vfinfo_failure; | |
b22b941b HFS |
1148 | if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || |
1149 | nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || | |
1150 | nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), | |
1151 | &vf_rate) || | |
1152 | nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), | |
1153 | &vf_tx_rate) || | |
1154 | nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), | |
1155 | &vf_spoofchk) || | |
1156 | nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), | |
1157 | &vf_linkstate) || | |
1158 | nla_put(skb, IFLA_VF_RSS_QUERY_EN, | |
1159 | sizeof(vf_rss_query_en), | |
1160 | &vf_rss_query_en) || | |
1161 | nla_put(skb, IFLA_VF_TRUST, | |
1162 | sizeof(vf_trust), &vf_trust)) | |
79aab093 MS |
1163 | goto nla_put_vf_failure; |
1164 | vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST); | |
1165 | if (!vfvlanlist) | |
1166 | goto nla_put_vf_failure; | |
1167 | if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), | |
1168 | &vf_vlan_info)) { | |
1169 | nla_nest_cancel(skb, vfvlanlist); | |
1170 | goto nla_put_vf_failure; | |
1171 | } | |
1172 | nla_nest_end(skb, vfvlanlist); | |
b22b941b HFS |
1173 | memset(&vf_stats, 0, sizeof(vf_stats)); |
1174 | if (dev->netdev_ops->ndo_get_vf_stats) | |
1175 | dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, | |
1176 | &vf_stats); | |
1177 | vfstats = nla_nest_start(skb, IFLA_VF_STATS); | |
79aab093 MS |
1178 | if (!vfstats) |
1179 | goto nla_put_vf_failure; | |
343a6d8e ND |
1180 | if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, |
1181 | vf_stats.rx_packets, IFLA_VF_STATS_PAD) || | |
1182 | nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, | |
1183 | vf_stats.tx_packets, IFLA_VF_STATS_PAD) || | |
1184 | nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, | |
1185 | vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || | |
1186 | nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, | |
1187 | vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || | |
1188 | nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, | |
1189 | vf_stats.broadcast, IFLA_VF_STATS_PAD) || | |
1190 | nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, | |
79aab093 MS |
1191 | vf_stats.multicast, IFLA_VF_STATS_PAD)) { |
1192 | nla_nest_cancel(skb, vfstats); | |
1193 | goto nla_put_vf_failure; | |
1194 | } | |
b22b941b HFS |
1195 | nla_nest_end(skb, vfstats); |
1196 | nla_nest_end(skb, vf); | |
1197 | return 0; | |
79aab093 MS |
1198 | |
1199 | nla_put_vf_failure: | |
1200 | nla_nest_cancel(skb, vf); | |
1201 | nla_put_vfinfo_failure: | |
1202 | nla_nest_cancel(skb, vfinfo); | |
1203 | return -EMSGSIZE; | |
b22b941b HFS |
1204 | } |
1205 | ||
1206 | static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) | |
1207 | { | |
5f8e4474 KL |
1208 | struct rtnl_link_ifmap map; |
1209 | ||
1210 | memset(&map, 0, sizeof(map)); | |
1211 | map.mem_start = dev->mem_start; | |
1212 | map.mem_end = dev->mem_end; | |
1213 | map.base_addr = dev->base_addr; | |
1214 | map.irq = dev->irq; | |
1215 | map.dma = dev->dma; | |
1216 | map.port = dev->if_port; | |
1217 | ||
270cb4d0 | 1218 | if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) |
b22b941b HFS |
1219 | return -EMSGSIZE; |
1220 | ||
1221 | return 0; | |
1222 | } | |
1223 | ||
58038695 | 1224 | static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id) |
d67b9cd2 DB |
1225 | { |
1226 | const struct net_device_ops *ops = dev->netdev_ops; | |
58038695 | 1227 | const struct bpf_prog *generic_xdp_prog; |
d67b9cd2 DB |
1228 | |
1229 | ASSERT_RTNL(); | |
1230 | ||
58038695 MKL |
1231 | *prog_id = 0; |
1232 | generic_xdp_prog = rtnl_dereference(dev->xdp_prog); | |
1233 | if (generic_xdp_prog) { | |
1234 | *prog_id = generic_xdp_prog->aux->id; | |
d67b9cd2 | 1235 | return XDP_ATTACHED_SKB; |
58038695 | 1236 | } |
ce158e58 JK |
1237 | if (!ops->ndo_xdp) |
1238 | return XDP_ATTACHED_NONE; | |
d67b9cd2 | 1239 | |
ce158e58 | 1240 | return __dev_xdp_attached(dev, ops->ndo_xdp, prog_id); |
d67b9cd2 DB |
1241 | } |
1242 | ||
d1fdd913 BB |
1243 | static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) |
1244 | { | |
d1fdd913 | 1245 | struct nlattr *xdp; |
58038695 | 1246 | u32 prog_id; |
d1fdd913 BB |
1247 | int err; |
1248 | ||
d1fdd913 BB |
1249 | xdp = nla_nest_start(skb, IFLA_XDP); |
1250 | if (!xdp) | |
1251 | return -EMSGSIZE; | |
d67b9cd2 DB |
1252 | |
1253 | err = nla_put_u8(skb, IFLA_XDP_ATTACHED, | |
58038695 | 1254 | rtnl_xdp_attached_mode(dev, &prog_id)); |
d1fdd913 BB |
1255 | if (err) |
1256 | goto err_cancel; | |
1257 | ||
58038695 MKL |
1258 | if (prog_id) { |
1259 | err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); | |
1260 | if (err) | |
1261 | goto err_cancel; | |
1262 | } | |
1263 | ||
d1fdd913 BB |
1264 | nla_nest_end(skb, xdp); |
1265 | return 0; | |
1266 | ||
1267 | err_cancel: | |
1268 | nla_nest_cancel(skb, xdp); | |
1269 | return err; | |
1270 | } | |
1271 | ||
3d3ea5af VY |
1272 | static u32 rtnl_get_event(unsigned long event) |
1273 | { | |
1274 | u32 rtnl_event_type = IFLA_EVENT_NONE; | |
1275 | ||
1276 | switch (event) { | |
1277 | case NETDEV_REBOOT: | |
1278 | rtnl_event_type = IFLA_EVENT_REBOOT; | |
1279 | break; | |
1280 | case NETDEV_FEAT_CHANGE: | |
1281 | rtnl_event_type = IFLA_EVENT_FEATURES; | |
1282 | break; | |
1283 | case NETDEV_BONDING_FAILOVER: | |
1284 | rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; | |
1285 | break; | |
1286 | case NETDEV_NOTIFY_PEERS: | |
1287 | rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; | |
1288 | break; | |
1289 | case NETDEV_RESEND_IGMP: | |
1290 | rtnl_event_type = IFLA_EVENT_IGMP_RESEND; | |
1291 | break; | |
1292 | case NETDEV_CHANGEINFODATA: | |
1293 | rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; | |
1294 | break; | |
1295 | default: | |
1296 | break; | |
1297 | } | |
1298 | ||
1299 | return rtnl_event_type; | |
1300 | } | |
1301 | ||
b60c5115 | 1302 | static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, |
575c3e2a | 1303 | int type, u32 pid, u32 seq, u32 change, |
3d3ea5af VY |
1304 | unsigned int flags, u32 ext_filter_mask, |
1305 | u32 event) | |
b60c5115 TG |
1306 | { |
1307 | struct ifinfomsg *ifm; | |
1308 | struct nlmsghdr *nlh; | |
b22b941b | 1309 | struct nlattr *af_spec; |
f8ff182c | 1310 | struct rtnl_af_ops *af_ops; |
898e5061 | 1311 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
1da177e4 | 1312 | |
2907c35f | 1313 | ASSERT_RTNL(); |
b60c5115 TG |
1314 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
1315 | if (nlh == NULL) | |
26932566 | 1316 | return -EMSGSIZE; |
1da177e4 | 1317 | |
b60c5115 TG |
1318 | ifm = nlmsg_data(nlh); |
1319 | ifm->ifi_family = AF_UNSPEC; | |
1320 | ifm->__ifi_pad = 0; | |
1321 | ifm->ifi_type = dev->type; | |
1322 | ifm->ifi_index = dev->ifindex; | |
1323 | ifm->ifi_flags = dev_get_flags(dev); | |
1324 | ifm->ifi_change = change; | |
1325 | ||
a6574349 DM |
1326 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
1327 | nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || | |
1328 | nla_put_u8(skb, IFLA_OPERSTATE, | |
1329 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || | |
1330 | nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || | |
1331 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || | |
1332 | nla_put_u32(skb, IFLA_GROUP, dev->group) || | |
edbc0bb3 | 1333 | nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || |
76ff5cc9 | 1334 | nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || |
c70ce028 ED |
1335 | nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || |
1336 | nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || | |
1d69c2b3 | 1337 | #ifdef CONFIG_RPS |
76ff5cc9 | 1338 | nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || |
1d69c2b3 | 1339 | #endif |
a54acb3a ND |
1340 | (dev->ifindex != dev_get_iflink(dev) && |
1341 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || | |
898e5061 JP |
1342 | (upper_dev && |
1343 | nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) || | |
9a57247f | 1344 | nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || |
a6574349 DM |
1345 | (dev->qdisc && |
1346 | nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || | |
1347 | (dev->ifalias && | |
2d3b479d | 1348 | nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) || |
1349 | nla_put_u32(skb, IFLA_CARRIER_CHANGES, | |
88d6378b AK |
1350 | atomic_read(&dev->carrier_changes)) || |
1351 | nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) | |
a6574349 | 1352 | goto nla_put_failure; |
0b815a1a | 1353 | |
3d3ea5af VY |
1354 | if (event != IFLA_EVENT_NONE) { |
1355 | if (nla_put_u32(skb, IFLA_EVENT, event)) | |
1356 | goto nla_put_failure; | |
1357 | } | |
1358 | ||
b22b941b HFS |
1359 | if (rtnl_fill_link_ifmap(skb, dev)) |
1360 | goto nla_put_failure; | |
1da177e4 LT |
1361 | |
1362 | if (dev->addr_len) { | |
a6574349 DM |
1363 | if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || |
1364 | nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) | |
1365 | goto nla_put_failure; | |
1da177e4 LT |
1366 | } |
1367 | ||
66cae9ed JP |
1368 | if (rtnl_phys_port_id_fill(skb, dev)) |
1369 | goto nla_put_failure; | |
1370 | ||
db24a904 DA |
1371 | if (rtnl_phys_port_name_fill(skb, dev)) |
1372 | goto nla_put_failure; | |
1373 | ||
82f28412 JP |
1374 | if (rtnl_phys_switch_id_fill(skb, dev)) |
1375 | goto nla_put_failure; | |
1376 | ||
b22b941b | 1377 | if (rtnl_fill_stats(skb, dev)) |
10708f37 | 1378 | goto nla_put_failure; |
10708f37 | 1379 | |
a6574349 DM |
1380 | if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) && |
1381 | nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent))) | |
1382 | goto nla_put_failure; | |
57b61080 | 1383 | |
b22b941b HFS |
1384 | if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent && |
1385 | ext_filter_mask & RTEXT_FILTER_VF) { | |
ebc08a6f | 1386 | int i; |
b22b941b | 1387 | struct nlattr *vfinfo; |
c02db8c6 CW |
1388 | int num_vfs = dev_num_vf(dev->dev.parent); |
1389 | ||
c02db8c6 CW |
1390 | vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); |
1391 | if (!vfinfo) | |
1392 | goto nla_put_failure; | |
1393 | for (i = 0; i < num_vfs; i++) { | |
b22b941b | 1394 | if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) |
3b766cd8 | 1395 | goto nla_put_failure; |
ebc08a6f | 1396 | } |
b22b941b | 1397 | |
c02db8c6 | 1398 | nla_nest_end(skb, vfinfo); |
ebc08a6f | 1399 | } |
57b61080 | 1400 | |
c53864fd | 1401 | if (rtnl_port_fill(skb, dev, ext_filter_mask)) |
57b61080 SF |
1402 | goto nla_put_failure; |
1403 | ||
d1fdd913 BB |
1404 | if (rtnl_xdp_fill(skb, dev)) |
1405 | goto nla_put_failure; | |
1406 | ||
ba7d49b1 | 1407 | if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { |
38f7b870 PM |
1408 | if (rtnl_link_fill(skb, dev) < 0) |
1409 | goto nla_put_failure; | |
1410 | } | |
1411 | ||
d37512a2 ND |
1412 | if (dev->rtnl_link_ops && |
1413 | dev->rtnl_link_ops->get_link_net) { | |
1414 | struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); | |
1415 | ||
1416 | if (!net_eq(dev_net(dev), link_net)) { | |
7a0877d4 | 1417 | int id = peernet2id_alloc(dev_net(dev), link_net); |
d37512a2 ND |
1418 | |
1419 | if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) | |
1420 | goto nla_put_failure; | |
1421 | } | |
1422 | } | |
1423 | ||
f8ff182c TG |
1424 | if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) |
1425 | goto nla_put_failure; | |
1426 | ||
1427 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { | |
1428 | if (af_ops->fill_link_af) { | |
1429 | struct nlattr *af; | |
1430 | int err; | |
1431 | ||
1432 | if (!(af = nla_nest_start(skb, af_ops->family))) | |
1433 | goto nla_put_failure; | |
1434 | ||
d5566fd7 | 1435 | err = af_ops->fill_link_af(skb, dev, ext_filter_mask); |
f8ff182c TG |
1436 | |
1437 | /* | |
1438 | * Caller may return ENODATA to indicate that there | |
1439 | * was no data to be dumped. This is not an error, it | |
1440 | * means we should trim the attribute header and | |
1441 | * continue. | |
1442 | */ | |
1443 | if (err == -ENODATA) | |
1444 | nla_nest_cancel(skb, af); | |
1445 | else if (err < 0) | |
1446 | goto nla_put_failure; | |
1447 | ||
1448 | nla_nest_end(skb, af); | |
1449 | } | |
1450 | } | |
1451 | ||
1452 | nla_nest_end(skb, af_spec); | |
1453 | ||
053c095a JB |
1454 | nlmsg_end(skb, nlh); |
1455 | return 0; | |
b60c5115 TG |
1456 | |
1457 | nla_put_failure: | |
26932566 PM |
1458 | nlmsg_cancel(skb, nlh); |
1459 | return -EMSGSIZE; | |
1da177e4 LT |
1460 | } |
1461 | ||
f7b12606 | 1462 | static const struct nla_policy ifla_policy[IFLA_MAX+1] = { |
5176f91e | 1463 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
38f7b870 PM |
1464 | [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
1465 | [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, | |
5176f91e | 1466 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
da5e0494 | 1467 | [IFLA_MTU] = { .type = NLA_U32 }, |
76e87306 | 1468 | [IFLA_LINK] = { .type = NLA_U32 }, |
fbaec0ea | 1469 | [IFLA_MASTER] = { .type = NLA_U32 }, |
9a57247f | 1470 | [IFLA_CARRIER] = { .type = NLA_U8 }, |
da5e0494 TG |
1471 | [IFLA_TXQLEN] = { .type = NLA_U32 }, |
1472 | [IFLA_WEIGHT] = { .type = NLA_U32 }, | |
1473 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, | |
1474 | [IFLA_LINKMODE] = { .type = NLA_U8 }, | |
76e87306 | 1475 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, |
d8a5ec67 | 1476 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
f0630529 | 1477 | [IFLA_NET_NS_FD] = { .type = NLA_U32 }, |
0b815a1a | 1478 | [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, |
c02db8c6 | 1479 | [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, |
57b61080 SF |
1480 | [IFLA_VF_PORTS] = { .type = NLA_NESTED }, |
1481 | [IFLA_PORT_SELF] = { .type = NLA_NESTED }, | |
f8ff182c | 1482 | [IFLA_AF_SPEC] = { .type = NLA_NESTED }, |
115c9b81 | 1483 | [IFLA_EXT_MASK] = { .type = NLA_U32 }, |
edbc0bb3 | 1484 | [IFLA_PROMISCUITY] = { .type = NLA_U32 }, |
76ff5cc9 JP |
1485 | [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, |
1486 | [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, | |
02637fce | 1487 | [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, |
2d3b479d | 1488 | [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ |
82f28412 | 1489 | [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, |
317f4810 | 1490 | [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, |
88d6378b | 1491 | [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, |
d1fdd913 | 1492 | [IFLA_XDP] = { .type = NLA_NESTED }, |
3d3ea5af | 1493 | [IFLA_EVENT] = { .type = NLA_U32 }, |
db833d40 | 1494 | [IFLA_GROUP] = { .type = NLA_U32 }, |
da5e0494 TG |
1495 | }; |
1496 | ||
38f7b870 PM |
1497 | static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { |
1498 | [IFLA_INFO_KIND] = { .type = NLA_STRING }, | |
1499 | [IFLA_INFO_DATA] = { .type = NLA_NESTED }, | |
ba7d49b1 JP |
1500 | [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, |
1501 | [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, | |
38f7b870 PM |
1502 | }; |
1503 | ||
c02db8c6 | 1504 | static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { |
364d5716 DB |
1505 | [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, |
1506 | [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, | |
79aab093 | 1507 | [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, |
364d5716 DB |
1508 | [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, |
1509 | [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, | |
1510 | [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, | |
1511 | [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, | |
01a3d796 | 1512 | [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, |
3b766cd8 | 1513 | [IFLA_VF_STATS] = { .type = NLA_NESTED }, |
dd461d6a | 1514 | [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, |
cc8e27cc EC |
1515 | [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, |
1516 | [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, | |
3b766cd8 EBE |
1517 | }; |
1518 | ||
57b61080 SF |
1519 | static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { |
1520 | [IFLA_PORT_VF] = { .type = NLA_U32 }, | |
1521 | [IFLA_PORT_PROFILE] = { .type = NLA_STRING, | |
1522 | .len = PORT_PROFILE_MAX }, | |
57b61080 SF |
1523 | [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, |
1524 | .len = PORT_UUID_MAX }, | |
1525 | [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, | |
1526 | .len = PORT_UUID_MAX }, | |
1527 | [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, | |
1528 | [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, | |
025331df DB |
1529 | |
1530 | /* Unused, but we need to keep it here since user space could | |
1531 | * fill it. It's also broken with regard to NLA_BINARY use in | |
1532 | * combination with structs. | |
1533 | */ | |
1534 | [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, | |
1535 | .len = sizeof(struct ifla_port_vsi) }, | |
57b61080 SF |
1536 | }; |
1537 | ||
d1fdd913 BB |
1538 | static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { |
1539 | [IFLA_XDP_FD] = { .type = NLA_S32 }, | |
1540 | [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, | |
85de8576 | 1541 | [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, |
58038695 | 1542 | [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, |
d1fdd913 BB |
1543 | }; |
1544 | ||
dc599f76 DA |
1545 | static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) |
1546 | { | |
1547 | const struct rtnl_link_ops *ops = NULL; | |
1548 | struct nlattr *linfo[IFLA_INFO_MAX + 1]; | |
1549 | ||
fceb6435 JB |
1550 | if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, |
1551 | ifla_info_policy, NULL) < 0) | |
dc599f76 DA |
1552 | return NULL; |
1553 | ||
1554 | if (linfo[IFLA_INFO_KIND]) { | |
1555 | char kind[MODULE_NAME_LEN]; | |
1556 | ||
1557 | nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); | |
1558 | ops = rtnl_link_ops_get(kind); | |
1559 | } | |
1560 | ||
1561 | return ops; | |
1562 | } | |
1563 | ||
1564 | static bool link_master_filtered(struct net_device *dev, int master_idx) | |
1565 | { | |
1566 | struct net_device *master; | |
1567 | ||
1568 | if (!master_idx) | |
1569 | return false; | |
1570 | ||
1571 | master = netdev_master_upper_dev_get(dev); | |
1572 | if (!master || master->ifindex != master_idx) | |
1573 | return true; | |
1574 | ||
1575 | return false; | |
1576 | } | |
1577 | ||
1578 | static bool link_kind_filtered(const struct net_device *dev, | |
1579 | const struct rtnl_link_ops *kind_ops) | |
1580 | { | |
1581 | if (kind_ops && dev->rtnl_link_ops != kind_ops) | |
1582 | return true; | |
1583 | ||
1584 | return false; | |
1585 | } | |
1586 | ||
1587 | static bool link_dump_filtered(struct net_device *dev, | |
1588 | int master_idx, | |
1589 | const struct rtnl_link_ops *kind_ops) | |
1590 | { | |
1591 | if (link_master_filtered(dev, master_idx) || | |
1592 | link_kind_filtered(dev, kind_ops)) | |
1593 | return true; | |
1594 | ||
1595 | return false; | |
1596 | } | |
1597 | ||
f7b12606 JP |
1598 | static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) |
1599 | { | |
1600 | struct net *net = sock_net(skb->sk); | |
1601 | int h, s_h; | |
1602 | int idx = 0, s_idx; | |
1603 | struct net_device *dev; | |
1604 | struct hlist_head *head; | |
1605 | struct nlattr *tb[IFLA_MAX+1]; | |
1606 | u32 ext_filter_mask = 0; | |
dc599f76 DA |
1607 | const struct rtnl_link_ops *kind_ops = NULL; |
1608 | unsigned int flags = NLM_F_MULTI; | |
1609 | int master_idx = 0; | |
973462bb | 1610 | int err; |
e5eca6d4 | 1611 | int hdrlen; |
f7b12606 JP |
1612 | |
1613 | s_h = cb->args[0]; | |
1614 | s_idx = cb->args[1]; | |
1615 | ||
f7b12606 JP |
1616 | cb->seq = net->dev_base_seq; |
1617 | ||
e5eca6d4 MS |
1618 | /* A hack to preserve kernel<->userspace interface. |
1619 | * The correct header is ifinfomsg. It is consistent with rtnl_getlink. | |
1620 | * However, before Linux v3.9 the code here assumed rtgenmsg and that's | |
1621 | * what iproute2 < v3.9.0 used. | |
1622 | * We can detect the old iproute2. Even including the IFLA_EXT_MASK | |
1623 | * attribute, its netlink message is shorter than struct ifinfomsg. | |
1624 | */ | |
1625 | hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? | |
1626 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); | |
1627 | ||
fceb6435 JB |
1628 | if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, |
1629 | ifla_policy, NULL) >= 0) { | |
f7b12606 JP |
1630 | if (tb[IFLA_EXT_MASK]) |
1631 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); | |
dc599f76 DA |
1632 | |
1633 | if (tb[IFLA_MASTER]) | |
1634 | master_idx = nla_get_u32(tb[IFLA_MASTER]); | |
1635 | ||
1636 | if (tb[IFLA_LINKINFO]) | |
1637 | kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]); | |
1638 | ||
1639 | if (master_idx || kind_ops) | |
1640 | flags |= NLM_F_DUMP_FILTERED; | |
f7b12606 JP |
1641 | } |
1642 | ||
1643 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { | |
1644 | idx = 0; | |
1645 | head = &net->dev_index_head[h]; | |
cac5e65e | 1646 | hlist_for_each_entry(dev, head, index_hlist) { |
dc599f76 | 1647 | if (link_dump_filtered(dev, master_idx, kind_ops)) |
3f0ae05d | 1648 | goto cont; |
f7b12606 JP |
1649 | if (idx < s_idx) |
1650 | goto cont; | |
973462bb DG |
1651 | err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, |
1652 | NETLINK_CB(cb->skb).portid, | |
1653 | cb->nlh->nlmsg_seq, 0, | |
dc599f76 | 1654 | flags, |
3d3ea5af | 1655 | ext_filter_mask, 0); |
973462bb | 1656 | |
f6c5775f DA |
1657 | if (err < 0) { |
1658 | if (likely(skb->len)) | |
1659 | goto out; | |
1660 | ||
1661 | goto out_err; | |
1662 | } | |
f7b12606 JP |
1663 | |
1664 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
1665 | cont: | |
1666 | idx++; | |
1667 | } | |
1668 | } | |
1669 | out: | |
f6c5775f DA |
1670 | err = skb->len; |
1671 | out_err: | |
f7b12606 JP |
1672 | cb->args[1] = idx; |
1673 | cb->args[0] = h; | |
1674 | ||
f6c5775f | 1675 | return err; |
f7b12606 JP |
1676 | } |
1677 | ||
fceb6435 JB |
1678 | int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, |
1679 | struct netlink_ext_ack *exterr) | |
f7b12606 | 1680 | { |
fceb6435 | 1681 | return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr); |
f7b12606 JP |
1682 | } |
1683 | EXPORT_SYMBOL(rtnl_nla_parse_ifla); | |
1684 | ||
81adee47 EB |
1685 | struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) |
1686 | { | |
1687 | struct net *net; | |
1688 | /* Examine the link attributes and figure out which | |
1689 | * network namespace we are talking about. | |
1690 | */ | |
1691 | if (tb[IFLA_NET_NS_PID]) | |
1692 | net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); | |
f0630529 EB |
1693 | else if (tb[IFLA_NET_NS_FD]) |
1694 | net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); | |
81adee47 EB |
1695 | else |
1696 | net = get_net(src_net); | |
1697 | return net; | |
1698 | } | |
1699 | EXPORT_SYMBOL(rtnl_link_get_net); | |
1700 | ||
1840bb13 TG |
1701 | static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) |
1702 | { | |
1703 | if (dev) { | |
1704 | if (tb[IFLA_ADDRESS] && | |
1705 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) | |
1706 | return -EINVAL; | |
1707 | ||
1708 | if (tb[IFLA_BROADCAST] && | |
1709 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) | |
1710 | return -EINVAL; | |
1711 | } | |
1712 | ||
cf7afbfe TG |
1713 | if (tb[IFLA_AF_SPEC]) { |
1714 | struct nlattr *af; | |
1715 | int rem, err; | |
1716 | ||
1717 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { | |
1718 | const struct rtnl_af_ops *af_ops; | |
1719 | ||
1720 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) | |
1721 | return -EAFNOSUPPORT; | |
1722 | ||
1723 | if (!af_ops->set_link_af) | |
1724 | return -EOPNOTSUPP; | |
1725 | ||
1726 | if (af_ops->validate_link_af) { | |
6d3a9a68 | 1727 | err = af_ops->validate_link_af(dev, af); |
cf7afbfe TG |
1728 | if (err < 0) |
1729 | return err; | |
1730 | } | |
1731 | } | |
1732 | } | |
1733 | ||
1840bb13 TG |
1734 | return 0; |
1735 | } | |
1736 | ||
cc8e27cc EC |
1737 | static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, |
1738 | int guid_type) | |
1739 | { | |
1740 | const struct net_device_ops *ops = dev->netdev_ops; | |
1741 | ||
1742 | return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); | |
1743 | } | |
1744 | ||
1745 | static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) | |
1746 | { | |
1747 | if (dev->type != ARPHRD_INFINIBAND) | |
1748 | return -EOPNOTSUPP; | |
1749 | ||
1750 | return handle_infiniband_guid(dev, ivt, guid_type); | |
1751 | } | |
1752 | ||
4f7d2cdf | 1753 | static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) |
c02db8c6 | 1754 | { |
c02db8c6 | 1755 | const struct net_device_ops *ops = dev->netdev_ops; |
4f7d2cdf | 1756 | int err = -EINVAL; |
c02db8c6 | 1757 | |
4f7d2cdf DB |
1758 | if (tb[IFLA_VF_MAC]) { |
1759 | struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); | |
01a3d796 | 1760 | |
4f7d2cdf DB |
1761 | err = -EOPNOTSUPP; |
1762 | if (ops->ndo_set_vf_mac) | |
1763 | err = ops->ndo_set_vf_mac(dev, ivm->vf, | |
1764 | ivm->mac); | |
1765 | if (err < 0) | |
1766 | return err; | |
1767 | } | |
1768 | ||
1769 | if (tb[IFLA_VF_VLAN]) { | |
1770 | struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); | |
1771 | ||
1772 | err = -EOPNOTSUPP; | |
1773 | if (ops->ndo_set_vf_vlan) | |
1774 | err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, | |
79aab093 MS |
1775 | ivv->qos, |
1776 | htons(ETH_P_8021Q)); | |
1777 | if (err < 0) | |
1778 | return err; | |
1779 | } | |
1780 | ||
1781 | if (tb[IFLA_VF_VLAN_LIST]) { | |
1782 | struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; | |
1783 | struct nlattr *attr; | |
1784 | int rem, len = 0; | |
1785 | ||
1786 | err = -EOPNOTSUPP; | |
1787 | if (!ops->ndo_set_vf_vlan) | |
1788 | return err; | |
1789 | ||
1790 | nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { | |
1791 | if (nla_type(attr) != IFLA_VF_VLAN_INFO || | |
1792 | nla_len(attr) < NLA_HDRLEN) { | |
1793 | return -EINVAL; | |
1794 | } | |
1795 | if (len >= MAX_VLAN_LIST_LEN) | |
1796 | return -EOPNOTSUPP; | |
1797 | ivvl[len] = nla_data(attr); | |
1798 | ||
1799 | len++; | |
1800 | } | |
fa34cd94 AB |
1801 | if (len == 0) |
1802 | return -EINVAL; | |
1803 | ||
79aab093 MS |
1804 | err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, |
1805 | ivvl[0]->qos, ivvl[0]->vlan_proto); | |
4f7d2cdf DB |
1806 | if (err < 0) |
1807 | return err; | |
c02db8c6 | 1808 | } |
4f7d2cdf DB |
1809 | |
1810 | if (tb[IFLA_VF_TX_RATE]) { | |
1811 | struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); | |
1812 | struct ifla_vf_info ivf; | |
1813 | ||
1814 | err = -EOPNOTSUPP; | |
1815 | if (ops->ndo_get_vf_config) | |
1816 | err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); | |
1817 | if (err < 0) | |
1818 | return err; | |
1819 | ||
1820 | err = -EOPNOTSUPP; | |
1821 | if (ops->ndo_set_vf_rate) | |
1822 | err = ops->ndo_set_vf_rate(dev, ivt->vf, | |
1823 | ivf.min_tx_rate, | |
1824 | ivt->rate); | |
1825 | if (err < 0) | |
1826 | return err; | |
1827 | } | |
1828 | ||
1829 | if (tb[IFLA_VF_RATE]) { | |
1830 | struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); | |
1831 | ||
1832 | err = -EOPNOTSUPP; | |
1833 | if (ops->ndo_set_vf_rate) | |
1834 | err = ops->ndo_set_vf_rate(dev, ivt->vf, | |
1835 | ivt->min_tx_rate, | |
1836 | ivt->max_tx_rate); | |
1837 | if (err < 0) | |
1838 | return err; | |
1839 | } | |
1840 | ||
1841 | if (tb[IFLA_VF_SPOOFCHK]) { | |
1842 | struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); | |
1843 | ||
1844 | err = -EOPNOTSUPP; | |
1845 | if (ops->ndo_set_vf_spoofchk) | |
1846 | err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, | |
1847 | ivs->setting); | |
1848 | if (err < 0) | |
1849 | return err; | |
1850 | } | |
1851 | ||
1852 | if (tb[IFLA_VF_LINK_STATE]) { | |
1853 | struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); | |
1854 | ||
1855 | err = -EOPNOTSUPP; | |
1856 | if (ops->ndo_set_vf_link_state) | |
1857 | err = ops->ndo_set_vf_link_state(dev, ivl->vf, | |
1858 | ivl->link_state); | |
1859 | if (err < 0) | |
1860 | return err; | |
1861 | } | |
1862 | ||
1863 | if (tb[IFLA_VF_RSS_QUERY_EN]) { | |
1864 | struct ifla_vf_rss_query_en *ivrssq_en; | |
1865 | ||
1866 | err = -EOPNOTSUPP; | |
1867 | ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); | |
1868 | if (ops->ndo_set_vf_rss_query_en) | |
1869 | err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, | |
1870 | ivrssq_en->setting); | |
1871 | if (err < 0) | |
1872 | return err; | |
1873 | } | |
1874 | ||
dd461d6a HS |
1875 | if (tb[IFLA_VF_TRUST]) { |
1876 | struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); | |
1877 | ||
1878 | err = -EOPNOTSUPP; | |
1879 | if (ops->ndo_set_vf_trust) | |
1880 | err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); | |
1881 | if (err < 0) | |
1882 | return err; | |
1883 | } | |
1884 | ||
cc8e27cc EC |
1885 | if (tb[IFLA_VF_IB_NODE_GUID]) { |
1886 | struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); | |
1887 | ||
1888 | if (!ops->ndo_set_vf_guid) | |
1889 | return -EOPNOTSUPP; | |
1890 | ||
1891 | return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); | |
1892 | } | |
1893 | ||
1894 | if (tb[IFLA_VF_IB_PORT_GUID]) { | |
1895 | struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); | |
1896 | ||
1897 | if (!ops->ndo_set_vf_guid) | |
1898 | return -EOPNOTSUPP; | |
1899 | ||
1900 | return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); | |
1901 | } | |
1902 | ||
c02db8c6 CW |
1903 | return err; |
1904 | } | |
1905 | ||
fbaec0ea JP |
1906 | static int do_set_master(struct net_device *dev, int ifindex) |
1907 | { | |
898e5061 | 1908 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
fbaec0ea JP |
1909 | const struct net_device_ops *ops; |
1910 | int err; | |
1911 | ||
898e5061 JP |
1912 | if (upper_dev) { |
1913 | if (upper_dev->ifindex == ifindex) | |
fbaec0ea | 1914 | return 0; |
898e5061 | 1915 | ops = upper_dev->netdev_ops; |
fbaec0ea | 1916 | if (ops->ndo_del_slave) { |
898e5061 | 1917 | err = ops->ndo_del_slave(upper_dev, dev); |
fbaec0ea JP |
1918 | if (err) |
1919 | return err; | |
1920 | } else { | |
1921 | return -EOPNOTSUPP; | |
1922 | } | |
1923 | } | |
1924 | ||
1925 | if (ifindex) { | |
898e5061 JP |
1926 | upper_dev = __dev_get_by_index(dev_net(dev), ifindex); |
1927 | if (!upper_dev) | |
fbaec0ea | 1928 | return -EINVAL; |
898e5061 | 1929 | ops = upper_dev->netdev_ops; |
fbaec0ea | 1930 | if (ops->ndo_add_slave) { |
898e5061 | 1931 | err = ops->ndo_add_slave(upper_dev, dev); |
fbaec0ea JP |
1932 | if (err) |
1933 | return err; | |
1934 | } else { | |
1935 | return -EOPNOTSUPP; | |
1936 | } | |
1937 | } | |
1938 | return 0; | |
1939 | } | |
1940 | ||
90c325e3 | 1941 | #define DO_SETLINK_MODIFIED 0x01 |
ba998906 ND |
1942 | /* notify flag means notify + modified. */ |
1943 | #define DO_SETLINK_NOTIFY 0x03 | |
90f62cf3 EB |
1944 | static int do_setlink(const struct sk_buff *skb, |
1945 | struct net_device *dev, struct ifinfomsg *ifm, | |
ddf9f970 | 1946 | struct netlink_ext_ack *extack, |
90c325e3 | 1947 | struct nlattr **tb, char *ifname, int status) |
1da177e4 | 1948 | { |
d314774c | 1949 | const struct net_device_ops *ops = dev->netdev_ops; |
0157f60c | 1950 | int err; |
1da177e4 | 1951 | |
f0630529 | 1952 | if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { |
81adee47 | 1953 | struct net *net = rtnl_link_get_net(dev_net(dev), tb); |
d8a5ec67 EB |
1954 | if (IS_ERR(net)) { |
1955 | err = PTR_ERR(net); | |
1956 | goto errout; | |
1957 | } | |
90f62cf3 | 1958 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { |
e0ebde0e | 1959 | put_net(net); |
b51642f6 EB |
1960 | err = -EPERM; |
1961 | goto errout; | |
1962 | } | |
d8a5ec67 EB |
1963 | err = dev_change_net_namespace(dev, net, ifname); |
1964 | put_net(net); | |
1965 | if (err) | |
1966 | goto errout; | |
90c325e3 | 1967 | status |= DO_SETLINK_MODIFIED; |
d8a5ec67 EB |
1968 | } |
1969 | ||
da5e0494 | 1970 | if (tb[IFLA_MAP]) { |
1da177e4 LT |
1971 | struct rtnl_link_ifmap *u_map; |
1972 | struct ifmap k_map; | |
1973 | ||
d314774c | 1974 | if (!ops->ndo_set_config) { |
1da177e4 | 1975 | err = -EOPNOTSUPP; |
0157f60c | 1976 | goto errout; |
1da177e4 LT |
1977 | } |
1978 | ||
1979 | if (!netif_device_present(dev)) { | |
1980 | err = -ENODEV; | |
0157f60c | 1981 | goto errout; |
1da177e4 | 1982 | } |
1da177e4 | 1983 | |
da5e0494 | 1984 | u_map = nla_data(tb[IFLA_MAP]); |
1da177e4 LT |
1985 | k_map.mem_start = (unsigned long) u_map->mem_start; |
1986 | k_map.mem_end = (unsigned long) u_map->mem_end; | |
1987 | k_map.base_addr = (unsigned short) u_map->base_addr; | |
1988 | k_map.irq = (unsigned char) u_map->irq; | |
1989 | k_map.dma = (unsigned char) u_map->dma; | |
1990 | k_map.port = (unsigned char) u_map->port; | |
1991 | ||
d314774c | 1992 | err = ops->ndo_set_config(dev, &k_map); |
da5e0494 | 1993 | if (err < 0) |
0157f60c | 1994 | goto errout; |
1da177e4 | 1995 | |
ba998906 | 1996 | status |= DO_SETLINK_NOTIFY; |
1da177e4 LT |
1997 | } |
1998 | ||
da5e0494 | 1999 | if (tb[IFLA_ADDRESS]) { |
70f8e78e DM |
2000 | struct sockaddr *sa; |
2001 | int len; | |
2002 | ||
153711f9 WC |
2003 | len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, |
2004 | sizeof(*sa)); | |
70f8e78e DM |
2005 | sa = kmalloc(len, GFP_KERNEL); |
2006 | if (!sa) { | |
2007 | err = -ENOMEM; | |
0157f60c | 2008 | goto errout; |
70f8e78e DM |
2009 | } |
2010 | sa->sa_family = dev->type; | |
da5e0494 | 2011 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
70f8e78e | 2012 | dev->addr_len); |
e7c3273e | 2013 | err = dev_set_mac_address(dev, sa); |
70f8e78e | 2014 | kfree(sa); |
1da177e4 | 2015 | if (err) |
0157f60c | 2016 | goto errout; |
90c325e3 | 2017 | status |= DO_SETLINK_MODIFIED; |
1da177e4 LT |
2018 | } |
2019 | ||
da5e0494 TG |
2020 | if (tb[IFLA_MTU]) { |
2021 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); | |
2022 | if (err < 0) | |
0157f60c | 2023 | goto errout; |
90c325e3 | 2024 | status |= DO_SETLINK_MODIFIED; |
1da177e4 LT |
2025 | } |
2026 | ||
cbda10fa VD |
2027 | if (tb[IFLA_GROUP]) { |
2028 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); | |
ba998906 | 2029 | status |= DO_SETLINK_NOTIFY; |
cbda10fa VD |
2030 | } |
2031 | ||
da5e0494 TG |
2032 | /* |
2033 | * Interface selected by interface index but interface | |
2034 | * name provided implies that a name change has been | |
2035 | * requested. | |
2036 | */ | |
51055be8 | 2037 | if (ifm->ifi_index > 0 && ifname[0]) { |
da5e0494 TG |
2038 | err = dev_change_name(dev, ifname); |
2039 | if (err < 0) | |
0157f60c | 2040 | goto errout; |
90c325e3 | 2041 | status |= DO_SETLINK_MODIFIED; |
1da177e4 LT |
2042 | } |
2043 | ||
0b815a1a SH |
2044 | if (tb[IFLA_IFALIAS]) { |
2045 | err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), | |
2046 | nla_len(tb[IFLA_IFALIAS])); | |
2047 | if (err < 0) | |
2048 | goto errout; | |
ba998906 | 2049 | status |= DO_SETLINK_NOTIFY; |
0b815a1a SH |
2050 | } |
2051 | ||
da5e0494 TG |
2052 | if (tb[IFLA_BROADCAST]) { |
2053 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); | |
e7c3273e | 2054 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
1da177e4 LT |
2055 | } |
2056 | ||
83b496e9 | 2057 | if (ifm->ifi_flags || ifm->ifi_change) { |
3729d502 | 2058 | err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
5f9021cf JB |
2059 | if (err < 0) |
2060 | goto errout; | |
83b496e9 | 2061 | } |
1da177e4 | 2062 | |
fbaec0ea JP |
2063 | if (tb[IFLA_MASTER]) { |
2064 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); | |
2065 | if (err) | |
2066 | goto errout; | |
90c325e3 | 2067 | status |= DO_SETLINK_MODIFIED; |
fbaec0ea JP |
2068 | } |
2069 | ||
9a57247f JP |
2070 | if (tb[IFLA_CARRIER]) { |
2071 | err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); | |
2072 | if (err) | |
2073 | goto errout; | |
90c325e3 | 2074 | status |= DO_SETLINK_MODIFIED; |
9a57247f JP |
2075 | } |
2076 | ||
5d1180fc | 2077 | if (tb[IFLA_TXQLEN]) { |
0cd29503 AD |
2078 | unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); |
2079 | unsigned int orig_len = dev->tx_queue_len; | |
08294a26 JW |
2080 | |
2081 | if (dev->tx_queue_len ^ value) { | |
2082 | dev->tx_queue_len = value; | |
2083 | err = call_netdevice_notifiers( | |
2084 | NETDEV_CHANGE_TX_QUEUE_LEN, dev); | |
2085 | err = notifier_to_errno(err); | |
2086 | if (err) { | |
2087 | dev->tx_queue_len = orig_len; | |
2088 | goto errout; | |
2089 | } | |
ba998906 | 2090 | status |= DO_SETLINK_NOTIFY; |
08294a26 | 2091 | } |
5d1180fc | 2092 | } |
b00055aa | 2093 | |
da5e0494 | 2094 | if (tb[IFLA_OPERSTATE]) |
93b2d4a2 | 2095 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
b00055aa | 2096 | |
da5e0494 | 2097 | if (tb[IFLA_LINKMODE]) { |
1889b0e7 ND |
2098 | unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); |
2099 | ||
93b2d4a2 | 2100 | write_lock_bh(&dev_base_lock); |
1889b0e7 | 2101 | if (dev->link_mode ^ value) |
ba998906 | 2102 | status |= DO_SETLINK_NOTIFY; |
1889b0e7 | 2103 | dev->link_mode = value; |
93b2d4a2 | 2104 | write_unlock_bh(&dev_base_lock); |
b00055aa SR |
2105 | } |
2106 | ||
c02db8c6 | 2107 | if (tb[IFLA_VFINFO_LIST]) { |
4f7d2cdf | 2108 | struct nlattr *vfinfo[IFLA_VF_MAX + 1]; |
c02db8c6 CW |
2109 | struct nlattr *attr; |
2110 | int rem; | |
4f7d2cdf | 2111 | |
c02db8c6 | 2112 | nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { |
4f7d2cdf DB |
2113 | if (nla_type(attr) != IFLA_VF_INFO || |
2114 | nla_len(attr) < NLA_HDRLEN) { | |
253683bb | 2115 | err = -EINVAL; |
c02db8c6 | 2116 | goto errout; |
253683bb | 2117 | } |
4f7d2cdf | 2118 | err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, |
fceb6435 | 2119 | ifla_vf_policy, NULL); |
4f7d2cdf DB |
2120 | if (err < 0) |
2121 | goto errout; | |
2122 | err = do_setvfinfo(dev, vfinfo); | |
c02db8c6 CW |
2123 | if (err < 0) |
2124 | goto errout; | |
ba998906 | 2125 | status |= DO_SETLINK_NOTIFY; |
c02db8c6 | 2126 | } |
ebc08a6f | 2127 | } |
1da177e4 LT |
2128 | err = 0; |
2129 | ||
57b61080 SF |
2130 | if (tb[IFLA_VF_PORTS]) { |
2131 | struct nlattr *port[IFLA_PORT_MAX+1]; | |
2132 | struct nlattr *attr; | |
2133 | int vf; | |
2134 | int rem; | |
2135 | ||
2136 | err = -EOPNOTSUPP; | |
2137 | if (!ops->ndo_set_vf_port) | |
2138 | goto errout; | |
2139 | ||
2140 | nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { | |
035d210f DB |
2141 | if (nla_type(attr) != IFLA_VF_PORT || |
2142 | nla_len(attr) < NLA_HDRLEN) { | |
2143 | err = -EINVAL; | |
2144 | goto errout; | |
2145 | } | |
2146 | err = nla_parse_nested(port, IFLA_PORT_MAX, attr, | |
fceb6435 | 2147 | ifla_port_policy, NULL); |
57b61080 SF |
2148 | if (err < 0) |
2149 | goto errout; | |
2150 | if (!port[IFLA_PORT_VF]) { | |
2151 | err = -EOPNOTSUPP; | |
2152 | goto errout; | |
2153 | } | |
2154 | vf = nla_get_u32(port[IFLA_PORT_VF]); | |
2155 | err = ops->ndo_set_vf_port(dev, vf, port); | |
2156 | if (err < 0) | |
2157 | goto errout; | |
ba998906 | 2158 | status |= DO_SETLINK_NOTIFY; |
57b61080 SF |
2159 | } |
2160 | } | |
2161 | err = 0; | |
2162 | ||
2163 | if (tb[IFLA_PORT_SELF]) { | |
2164 | struct nlattr *port[IFLA_PORT_MAX+1]; | |
2165 | ||
2166 | err = nla_parse_nested(port, IFLA_PORT_MAX, | |
fceb6435 JB |
2167 | tb[IFLA_PORT_SELF], ifla_port_policy, |
2168 | NULL); | |
57b61080 SF |
2169 | if (err < 0) |
2170 | goto errout; | |
2171 | ||
2172 | err = -EOPNOTSUPP; | |
2173 | if (ops->ndo_set_vf_port) | |
2174 | err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); | |
2175 | if (err < 0) | |
2176 | goto errout; | |
ba998906 | 2177 | status |= DO_SETLINK_NOTIFY; |
57b61080 | 2178 | } |
f8ff182c TG |
2179 | |
2180 | if (tb[IFLA_AF_SPEC]) { | |
2181 | struct nlattr *af; | |
2182 | int rem; | |
2183 | ||
2184 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { | |
2185 | const struct rtnl_af_ops *af_ops; | |
2186 | ||
2187 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) | |
cf7afbfe | 2188 | BUG(); |
f8ff182c | 2189 | |
cf7afbfe | 2190 | err = af_ops->set_link_af(dev, af); |
f8ff182c TG |
2191 | if (err < 0) |
2192 | goto errout; | |
2193 | ||
ba998906 | 2194 | status |= DO_SETLINK_NOTIFY; |
f8ff182c TG |
2195 | } |
2196 | } | |
57b61080 SF |
2197 | err = 0; |
2198 | ||
88d6378b AK |
2199 | if (tb[IFLA_PROTO_DOWN]) { |
2200 | err = dev_change_proto_down(dev, | |
2201 | nla_get_u8(tb[IFLA_PROTO_DOWN])); | |
2202 | if (err) | |
2203 | goto errout; | |
2204 | status |= DO_SETLINK_NOTIFY; | |
2205 | } | |
2206 | ||
d1fdd913 BB |
2207 | if (tb[IFLA_XDP]) { |
2208 | struct nlattr *xdp[IFLA_XDP_MAX + 1]; | |
85de8576 | 2209 | u32 xdp_flags = 0; |
d1fdd913 BB |
2210 | |
2211 | err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP], | |
fceb6435 | 2212 | ifla_xdp_policy, NULL); |
d1fdd913 BB |
2213 | if (err < 0) |
2214 | goto errout; | |
2215 | ||
58038695 | 2216 | if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { |
262d8625 BB |
2217 | err = -EINVAL; |
2218 | goto errout; | |
2219 | } | |
85de8576 DB |
2220 | |
2221 | if (xdp[IFLA_XDP_FLAGS]) { | |
2222 | xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); | |
2223 | if (xdp_flags & ~XDP_FLAGS_MASK) { | |
2224 | err = -EINVAL; | |
2225 | goto errout; | |
2226 | } | |
ee5d032f | 2227 | if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { |
0489df9a DB |
2228 | err = -EINVAL; |
2229 | goto errout; | |
2230 | } | |
85de8576 DB |
2231 | } |
2232 | ||
d1fdd913 | 2233 | if (xdp[IFLA_XDP_FD]) { |
ddf9f970 | 2234 | err = dev_change_xdp_fd(dev, extack, |
85de8576 DB |
2235 | nla_get_s32(xdp[IFLA_XDP_FD]), |
2236 | xdp_flags); | |
d1fdd913 BB |
2237 | if (err) |
2238 | goto errout; | |
2239 | status |= DO_SETLINK_NOTIFY; | |
2240 | } | |
2241 | } | |
2242 | ||
0157f60c | 2243 | errout: |
ba998906 ND |
2244 | if (status & DO_SETLINK_MODIFIED) { |
2245 | if (status & DO_SETLINK_NOTIFY) | |
2246 | netdev_state_change(dev); | |
2247 | ||
2248 | if (err < 0) | |
2249 | net_warn_ratelimited("A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n", | |
2250 | dev->name); | |
2251 | } | |
da5e0494 | 2252 | |
0157f60c PM |
2253 | return err; |
2254 | } | |
1da177e4 | 2255 | |
c21ef3e3 DA |
2256 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
2257 | struct netlink_ext_ack *extack) | |
0157f60c | 2258 | { |
3b1e0a65 | 2259 | struct net *net = sock_net(skb->sk); |
0157f60c PM |
2260 | struct ifinfomsg *ifm; |
2261 | struct net_device *dev; | |
2262 | int err; | |
2263 | struct nlattr *tb[IFLA_MAX+1]; | |
2264 | char ifname[IFNAMSIZ]; | |
2265 | ||
c21ef3e3 DA |
2266 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, |
2267 | extack); | |
0157f60c PM |
2268 | if (err < 0) |
2269 | goto errout; | |
2270 | ||
2271 | if (tb[IFLA_IFNAME]) | |
2272 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
2273 | else | |
2274 | ifname[0] = '\0'; | |
2275 | ||
2276 | err = -EINVAL; | |
2277 | ifm = nlmsg_data(nlh); | |
2278 | if (ifm->ifi_index > 0) | |
a3d12891 | 2279 | dev = __dev_get_by_index(net, ifm->ifi_index); |
0157f60c | 2280 | else if (tb[IFLA_IFNAME]) |
a3d12891 | 2281 | dev = __dev_get_by_name(net, ifname); |
0157f60c PM |
2282 | else |
2283 | goto errout; | |
2284 | ||
2285 | if (dev == NULL) { | |
2286 | err = -ENODEV; | |
2287 | goto errout; | |
2288 | } | |
2289 | ||
e0d087af ED |
2290 | err = validate_linkmsg(dev, tb); |
2291 | if (err < 0) | |
a3d12891 | 2292 | goto errout; |
0157f60c | 2293 | |
ddf9f970 | 2294 | err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); |
da5e0494 | 2295 | errout: |
1da177e4 LT |
2296 | return err; |
2297 | } | |
2298 | ||
66400d54 WC |
2299 | static int rtnl_group_dellink(const struct net *net, int group) |
2300 | { | |
2301 | struct net_device *dev, *aux; | |
2302 | LIST_HEAD(list_kill); | |
2303 | bool found = false; | |
2304 | ||
2305 | if (!group) | |
2306 | return -EPERM; | |
2307 | ||
2308 | for_each_netdev(net, dev) { | |
2309 | if (dev->group == group) { | |
2310 | const struct rtnl_link_ops *ops; | |
2311 | ||
2312 | found = true; | |
2313 | ops = dev->rtnl_link_ops; | |
2314 | if (!ops || !ops->dellink) | |
2315 | return -EOPNOTSUPP; | |
2316 | } | |
2317 | } | |
2318 | ||
2319 | if (!found) | |
2320 | return -ENODEV; | |
2321 | ||
2322 | for_each_netdev_safe(net, dev, aux) { | |
2323 | if (dev->group == group) { | |
2324 | const struct rtnl_link_ops *ops; | |
2325 | ||
2326 | ops = dev->rtnl_link_ops; | |
2327 | ops->dellink(dev, &list_kill); | |
2328 | } | |
2329 | } | |
2330 | unregister_netdevice_many(&list_kill); | |
2331 | ||
2332 | return 0; | |
2333 | } | |
2334 | ||
614732ea TG |
2335 | int rtnl_delete_link(struct net_device *dev) |
2336 | { | |
2337 | const struct rtnl_link_ops *ops; | |
2338 | LIST_HEAD(list_kill); | |
2339 | ||
2340 | ops = dev->rtnl_link_ops; | |
2341 | if (!ops || !ops->dellink) | |
2342 | return -EOPNOTSUPP; | |
2343 | ||
2344 | ops->dellink(dev, &list_kill); | |
2345 | unregister_netdevice_many(&list_kill); | |
2346 | ||
2347 | return 0; | |
2348 | } | |
2349 | EXPORT_SYMBOL_GPL(rtnl_delete_link); | |
2350 | ||
c21ef3e3 DA |
2351 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, |
2352 | struct netlink_ext_ack *extack) | |
38f7b870 | 2353 | { |
3b1e0a65 | 2354 | struct net *net = sock_net(skb->sk); |
38f7b870 PM |
2355 | struct net_device *dev; |
2356 | struct ifinfomsg *ifm; | |
2357 | char ifname[IFNAMSIZ]; | |
2358 | struct nlattr *tb[IFLA_MAX+1]; | |
2359 | int err; | |
2360 | ||
c21ef3e3 | 2361 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); |
38f7b870 PM |
2362 | if (err < 0) |
2363 | return err; | |
2364 | ||
2365 | if (tb[IFLA_IFNAME]) | |
2366 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
2367 | ||
2368 | ifm = nlmsg_data(nlh); | |
2369 | if (ifm->ifi_index > 0) | |
881d966b | 2370 | dev = __dev_get_by_index(net, ifm->ifi_index); |
38f7b870 | 2371 | else if (tb[IFLA_IFNAME]) |
881d966b | 2372 | dev = __dev_get_by_name(net, ifname); |
66400d54 WC |
2373 | else if (tb[IFLA_GROUP]) |
2374 | return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP])); | |
38f7b870 PM |
2375 | else |
2376 | return -EINVAL; | |
2377 | ||
2378 | if (!dev) | |
2379 | return -ENODEV; | |
2380 | ||
614732ea | 2381 | return rtnl_delete_link(dev); |
38f7b870 PM |
2382 | } |
2383 | ||
3729d502 PM |
2384 | int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) |
2385 | { | |
2386 | unsigned int old_flags; | |
2387 | int err; | |
2388 | ||
2389 | old_flags = dev->flags; | |
2390 | if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { | |
2391 | err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); | |
2392 | if (err < 0) | |
2393 | return err; | |
2394 | } | |
2395 | ||
2396 | dev->rtnl_link_state = RTNL_LINK_INITIALIZED; | |
3729d502 | 2397 | |
a528c219 | 2398 | __dev_notify_flags(dev, old_flags, ~0U); |
3729d502 PM |
2399 | return 0; |
2400 | } | |
2401 | EXPORT_SYMBOL(rtnl_configure_link); | |
2402 | ||
c0713563 | 2403 | struct net_device *rtnl_create_link(struct net *net, |
78ebb0d0 | 2404 | const char *ifname, unsigned char name_assign_type, |
5517750f | 2405 | const struct rtnl_link_ops *ops, struct nlattr *tb[]) |
e7199288 | 2406 | { |
e7199288 | 2407 | struct net_device *dev; |
d40156aa JP |
2408 | unsigned int num_tx_queues = 1; |
2409 | unsigned int num_rx_queues = 1; | |
e7199288 | 2410 | |
76ff5cc9 JP |
2411 | if (tb[IFLA_NUM_TX_QUEUES]) |
2412 | num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); | |
2413 | else if (ops->get_num_tx_queues) | |
d40156aa | 2414 | num_tx_queues = ops->get_num_tx_queues(); |
76ff5cc9 JP |
2415 | |
2416 | if (tb[IFLA_NUM_RX_QUEUES]) | |
2417 | num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); | |
2418 | else if (ops->get_num_rx_queues) | |
d40156aa | 2419 | num_rx_queues = ops->get_num_rx_queues(); |
efacb309 | 2420 | |
5517750f | 2421 | dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, |
c835a677 | 2422 | ops->setup, num_tx_queues, num_rx_queues); |
e7199288 | 2423 | if (!dev) |
d1892e4e | 2424 | return ERR_PTR(-ENOMEM); |
e7199288 | 2425 | |
81adee47 EB |
2426 | dev_net_set(dev, net); |
2427 | dev->rtnl_link_ops = ops; | |
3729d502 | 2428 | dev->rtnl_link_state = RTNL_LINK_INITIALIZING; |
81adee47 | 2429 | |
e7199288 PE |
2430 | if (tb[IFLA_MTU]) |
2431 | dev->mtu = nla_get_u32(tb[IFLA_MTU]); | |
2afb9b53 | 2432 | if (tb[IFLA_ADDRESS]) { |
e7199288 PE |
2433 | memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), |
2434 | nla_len(tb[IFLA_ADDRESS])); | |
2afb9b53 JP |
2435 | dev->addr_assign_type = NET_ADDR_SET; |
2436 | } | |
e7199288 PE |
2437 | if (tb[IFLA_BROADCAST]) |
2438 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), | |
2439 | nla_len(tb[IFLA_BROADCAST])); | |
2440 | if (tb[IFLA_TXQLEN]) | |
2441 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); | |
2442 | if (tb[IFLA_OPERSTATE]) | |
93b2d4a2 | 2443 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
e7199288 PE |
2444 | if (tb[IFLA_LINKMODE]) |
2445 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); | |
ffa934f1 PM |
2446 | if (tb[IFLA_GROUP]) |
2447 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); | |
e7199288 PE |
2448 | |
2449 | return dev; | |
e7199288 | 2450 | } |
e0d087af | 2451 | EXPORT_SYMBOL(rtnl_create_link); |
e7199288 | 2452 | |
90f62cf3 EB |
2453 | static int rtnl_group_changelink(const struct sk_buff *skb, |
2454 | struct net *net, int group, | |
e7ed828f | 2455 | struct ifinfomsg *ifm, |
ddf9f970 | 2456 | struct netlink_ext_ack *extack, |
e7ed828f VD |
2457 | struct nlattr **tb) |
2458 | { | |
d079535d | 2459 | struct net_device *dev, *aux; |
e7ed828f VD |
2460 | int err; |
2461 | ||
d079535d | 2462 | for_each_netdev_safe(net, dev, aux) { |
e7ed828f | 2463 | if (dev->group == group) { |
ddf9f970 | 2464 | err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); |
e7ed828f VD |
2465 | if (err < 0) |
2466 | return err; | |
2467 | } | |
2468 | } | |
2469 | ||
2470 | return 0; | |
2471 | } | |
2472 | ||
c21ef3e3 DA |
2473 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
2474 | struct netlink_ext_ack *extack) | |
38f7b870 | 2475 | { |
3b1e0a65 | 2476 | struct net *net = sock_net(skb->sk); |
38f7b870 | 2477 | const struct rtnl_link_ops *ops; |
ba7d49b1 | 2478 | const struct rtnl_link_ops *m_ops = NULL; |
38f7b870 | 2479 | struct net_device *dev; |
ba7d49b1 | 2480 | struct net_device *master_dev = NULL; |
38f7b870 PM |
2481 | struct ifinfomsg *ifm; |
2482 | char kind[MODULE_NAME_LEN]; | |
2483 | char ifname[IFNAMSIZ]; | |
2484 | struct nlattr *tb[IFLA_MAX+1]; | |
2485 | struct nlattr *linkinfo[IFLA_INFO_MAX+1]; | |
5517750f | 2486 | unsigned char name_assign_type = NET_NAME_USER; |
38f7b870 PM |
2487 | int err; |
2488 | ||
95a5afca | 2489 | #ifdef CONFIG_MODULES |
38f7b870 | 2490 | replay: |
8072f085 | 2491 | #endif |
c21ef3e3 | 2492 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); |
38f7b870 PM |
2493 | if (err < 0) |
2494 | return err; | |
2495 | ||
2496 | if (tb[IFLA_IFNAME]) | |
2497 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
2498 | else | |
2499 | ifname[0] = '\0'; | |
2500 | ||
2501 | ifm = nlmsg_data(nlh); | |
2502 | if (ifm->ifi_index > 0) | |
881d966b | 2503 | dev = __dev_get_by_index(net, ifm->ifi_index); |
e7ed828f VD |
2504 | else { |
2505 | if (ifname[0]) | |
2506 | dev = __dev_get_by_name(net, ifname); | |
e7ed828f VD |
2507 | else |
2508 | dev = NULL; | |
2509 | } | |
38f7b870 | 2510 | |
ba7d49b1 JP |
2511 | if (dev) { |
2512 | master_dev = netdev_master_upper_dev_get(dev); | |
2513 | if (master_dev) | |
2514 | m_ops = master_dev->rtnl_link_ops; | |
2515 | } | |
2516 | ||
e0d087af ED |
2517 | err = validate_linkmsg(dev, tb); |
2518 | if (err < 0) | |
1840bb13 TG |
2519 | return err; |
2520 | ||
38f7b870 PM |
2521 | if (tb[IFLA_LINKINFO]) { |
2522 | err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, | |
fceb6435 JB |
2523 | tb[IFLA_LINKINFO], ifla_info_policy, |
2524 | NULL); | |
38f7b870 PM |
2525 | if (err < 0) |
2526 | return err; | |
2527 | } else | |
2528 | memset(linkinfo, 0, sizeof(linkinfo)); | |
2529 | ||
2530 | if (linkinfo[IFLA_INFO_KIND]) { | |
2531 | nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); | |
2532 | ops = rtnl_link_ops_get(kind); | |
2533 | } else { | |
2534 | kind[0] = '\0'; | |
2535 | ops = NULL; | |
2536 | } | |
2537 | ||
2538 | if (1) { | |
4e10fd5b SL |
2539 | struct nlattr *attr[ops ? ops->maxtype + 1 : 1]; |
2540 | struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1]; | |
ba7d49b1 JP |
2541 | struct nlattr **data = NULL; |
2542 | struct nlattr **slave_data = NULL; | |
317f4810 | 2543 | struct net *dest_net, *link_net = NULL; |
38f7b870 PM |
2544 | |
2545 | if (ops) { | |
2546 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { | |
2547 | err = nla_parse_nested(attr, ops->maxtype, | |
2548 | linkinfo[IFLA_INFO_DATA], | |
fceb6435 | 2549 | ops->policy, NULL); |
38f7b870 PM |
2550 | if (err < 0) |
2551 | return err; | |
2552 | data = attr; | |
2553 | } | |
2554 | if (ops->validate) { | |
a8b8a889 | 2555 | err = ops->validate(tb, data, extack); |
38f7b870 PM |
2556 | if (err < 0) |
2557 | return err; | |
2558 | } | |
2559 | } | |
2560 | ||
ba7d49b1 JP |
2561 | if (m_ops) { |
2562 | if (m_ops->slave_maxtype && | |
2563 | linkinfo[IFLA_INFO_SLAVE_DATA]) { | |
2564 | err = nla_parse_nested(slave_attr, | |
2565 | m_ops->slave_maxtype, | |
2566 | linkinfo[IFLA_INFO_SLAVE_DATA], | |
fceb6435 JB |
2567 | m_ops->slave_policy, |
2568 | NULL); | |
ba7d49b1 JP |
2569 | if (err < 0) |
2570 | return err; | |
2571 | slave_data = slave_attr; | |
2572 | } | |
2573 | if (m_ops->slave_validate) { | |
d116ffc7 MS |
2574 | err = m_ops->slave_validate(tb, slave_data, |
2575 | extack); | |
ba7d49b1 JP |
2576 | if (err < 0) |
2577 | return err; | |
2578 | } | |
2579 | } | |
2580 | ||
38f7b870 | 2581 | if (dev) { |
90c325e3 | 2582 | int status = 0; |
38f7b870 PM |
2583 | |
2584 | if (nlh->nlmsg_flags & NLM_F_EXCL) | |
2585 | return -EEXIST; | |
2586 | if (nlh->nlmsg_flags & NLM_F_REPLACE) | |
2587 | return -EOPNOTSUPP; | |
2588 | ||
2589 | if (linkinfo[IFLA_INFO_DATA]) { | |
2590 | if (!ops || ops != dev->rtnl_link_ops || | |
2591 | !ops->changelink) | |
2592 | return -EOPNOTSUPP; | |
2593 | ||
ad744b22 | 2594 | err = ops->changelink(dev, tb, data, extack); |
38f7b870 PM |
2595 | if (err < 0) |
2596 | return err; | |
ba998906 | 2597 | status |= DO_SETLINK_NOTIFY; |
38f7b870 PM |
2598 | } |
2599 | ||
ba7d49b1 JP |
2600 | if (linkinfo[IFLA_INFO_SLAVE_DATA]) { |
2601 | if (!m_ops || !m_ops->slave_changelink) | |
2602 | return -EOPNOTSUPP; | |
2603 | ||
2604 | err = m_ops->slave_changelink(master_dev, dev, | |
17dd0ec4 MS |
2605 | tb, slave_data, |
2606 | extack); | |
ba7d49b1 JP |
2607 | if (err < 0) |
2608 | return err; | |
ba998906 | 2609 | status |= DO_SETLINK_NOTIFY; |
ba7d49b1 JP |
2610 | } |
2611 | ||
ddf9f970 JK |
2612 | return do_setlink(skb, dev, ifm, extack, tb, ifname, |
2613 | status); | |
38f7b870 PM |
2614 | } |
2615 | ||
ffa934f1 PM |
2616 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { |
2617 | if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) | |
90f62cf3 | 2618 | return rtnl_group_changelink(skb, net, |
ffa934f1 | 2619 | nla_get_u32(tb[IFLA_GROUP]), |
ddf9f970 | 2620 | ifm, extack, tb); |
38f7b870 | 2621 | return -ENODEV; |
ffa934f1 | 2622 | } |
38f7b870 | 2623 | |
160ca014 | 2624 | if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) |
38f7b870 PM |
2625 | return -EOPNOTSUPP; |
2626 | ||
2627 | if (!ops) { | |
95a5afca | 2628 | #ifdef CONFIG_MODULES |
38f7b870 PM |
2629 | if (kind[0]) { |
2630 | __rtnl_unlock(); | |
2631 | request_module("rtnl-link-%s", kind); | |
2632 | rtnl_lock(); | |
2633 | ops = rtnl_link_ops_get(kind); | |
2634 | if (ops) | |
2635 | goto replay; | |
2636 | } | |
2637 | #endif | |
2638 | return -EOPNOTSUPP; | |
2639 | } | |
2640 | ||
b0ab2fab JP |
2641 | if (!ops->setup) |
2642 | return -EOPNOTSUPP; | |
2643 | ||
5517750f | 2644 | if (!ifname[0]) { |
38f7b870 | 2645 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); |
5517750f TG |
2646 | name_assign_type = NET_NAME_ENUM; |
2647 | } | |
e7199288 | 2648 | |
81adee47 | 2649 | dest_net = rtnl_link_get_net(net, tb); |
13ad1774 EB |
2650 | if (IS_ERR(dest_net)) |
2651 | return PTR_ERR(dest_net); | |
2652 | ||
505ce415 EB |
2653 | err = -EPERM; |
2654 | if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN)) | |
2655 | goto out; | |
2656 | ||
317f4810 ND |
2657 | if (tb[IFLA_LINK_NETNSID]) { |
2658 | int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); | |
2659 | ||
2660 | link_net = get_net_ns_by_id(dest_net, id); | |
2661 | if (!link_net) { | |
2662 | err = -EINVAL; | |
2663 | goto out; | |
2664 | } | |
06615bed EB |
2665 | err = -EPERM; |
2666 | if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) | |
2667 | goto out; | |
317f4810 ND |
2668 | } |
2669 | ||
2670 | dev = rtnl_create_link(link_net ? : dest_net, ifname, | |
2671 | name_assign_type, ops, tb); | |
9c7dafbf | 2672 | if (IS_ERR(dev)) { |
e7199288 | 2673 | err = PTR_ERR(dev); |
9c7dafbf PE |
2674 | goto out; |
2675 | } | |
2676 | ||
2677 | dev->ifindex = ifm->ifi_index; | |
2678 | ||
0e0eee24 | 2679 | if (ops->newlink) { |
7a3f4a18 MS |
2680 | err = ops->newlink(link_net ? : net, dev, tb, data, |
2681 | extack); | |
0e0eee24 | 2682 | /* Drivers should call free_netdev() in ->destructor |
e51fb152 CW |
2683 | * and unregister it on failure after registration |
2684 | * so that device could be finally freed in rtnl_unlock. | |
0e0eee24 | 2685 | */ |
e51fb152 CW |
2686 | if (err < 0) { |
2687 | /* If device is not registered at all, free it now */ | |
2688 | if (dev->reg_state == NETREG_UNINITIALIZED) | |
2689 | free_netdev(dev); | |
0e0eee24 | 2690 | goto out; |
e51fb152 | 2691 | } |
0e0eee24 | 2692 | } else { |
2d85cba2 | 2693 | err = register_netdevice(dev); |
0e0eee24 CW |
2694 | if (err < 0) { |
2695 | free_netdev(dev); | |
2696 | goto out; | |
2697 | } | |
fce9b9be | 2698 | } |
3729d502 | 2699 | err = rtnl_configure_link(dev, ifm); |
43638900 DM |
2700 | if (err < 0) |
2701 | goto out_unregister; | |
bdef279b | 2702 | if (link_net) { |
317f4810 | 2703 | err = dev_change_net_namespace(dev, dest_net, ifname); |
bdef279b | 2704 | if (err < 0) |
43638900 | 2705 | goto out_unregister; |
bdef279b | 2706 | } |
160ca014 TV |
2707 | if (tb[IFLA_MASTER]) { |
2708 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); | |
2709 | if (err) | |
2710 | goto out_unregister; | |
2711 | } | |
3729d502 | 2712 | out: |
317f4810 ND |
2713 | if (link_net) |
2714 | put_net(link_net); | |
81adee47 | 2715 | put_net(dest_net); |
38f7b870 | 2716 | return err; |
43638900 DM |
2717 | out_unregister: |
2718 | if (ops->newlink) { | |
2719 | LIST_HEAD(list_kill); | |
2720 | ||
2721 | ops->dellink(dev, &list_kill); | |
2722 | unregister_netdevice_many(&list_kill); | |
2723 | } else { | |
2724 | unregister_netdevice(dev); | |
2725 | } | |
2726 | goto out; | |
38f7b870 PM |
2727 | } |
2728 | } | |
2729 | ||
c21ef3e3 DA |
2730 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
2731 | struct netlink_ext_ack *extack) | |
711e2c33 | 2732 | { |
3b1e0a65 | 2733 | struct net *net = sock_net(skb->sk); |
b60c5115 | 2734 | struct ifinfomsg *ifm; |
a3d12891 | 2735 | char ifname[IFNAMSIZ]; |
b60c5115 TG |
2736 | struct nlattr *tb[IFLA_MAX+1]; |
2737 | struct net_device *dev = NULL; | |
2738 | struct sk_buff *nskb; | |
339bf98f | 2739 | int err; |
115c9b81 | 2740 | u32 ext_filter_mask = 0; |
711e2c33 | 2741 | |
c21ef3e3 | 2742 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); |
b60c5115 | 2743 | if (err < 0) |
9918f230 | 2744 | return err; |
b60c5115 | 2745 | |
a3d12891 ED |
2746 | if (tb[IFLA_IFNAME]) |
2747 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
2748 | ||
115c9b81 GR |
2749 | if (tb[IFLA_EXT_MASK]) |
2750 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); | |
2751 | ||
b60c5115 | 2752 | ifm = nlmsg_data(nlh); |
a3d12891 ED |
2753 | if (ifm->ifi_index > 0) |
2754 | dev = __dev_get_by_index(net, ifm->ifi_index); | |
2755 | else if (tb[IFLA_IFNAME]) | |
2756 | dev = __dev_get_by_name(net, ifname); | |
2757 | else | |
711e2c33 | 2758 | return -EINVAL; |
711e2c33 | 2759 | |
a3d12891 ED |
2760 | if (dev == NULL) |
2761 | return -ENODEV; | |
2762 | ||
115c9b81 | 2763 | nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); |
a3d12891 ED |
2764 | if (nskb == NULL) |
2765 | return -ENOBUFS; | |
b60c5115 | 2766 | |
15e47304 | 2767 | err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid, |
3d3ea5af | 2768 | nlh->nlmsg_seq, 0, 0, ext_filter_mask, 0); |
26932566 PM |
2769 | if (err < 0) { |
2770 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ | |
2771 | WARN_ON(err == -EMSGSIZE); | |
2772 | kfree_skb(nskb); | |
a3d12891 | 2773 | } else |
15e47304 | 2774 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); |
711e2c33 | 2775 | |
b60c5115 | 2776 | return err; |
711e2c33 | 2777 | } |
711e2c33 | 2778 | |
115c9b81 | 2779 | static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) |
c7ac8679 | 2780 | { |
115c9b81 GR |
2781 | struct net *net = sock_net(skb->sk); |
2782 | struct net_device *dev; | |
2783 | struct nlattr *tb[IFLA_MAX+1]; | |
2784 | u32 ext_filter_mask = 0; | |
2785 | u16 min_ifinfo_dump_size = 0; | |
e5eca6d4 MS |
2786 | int hdrlen; |
2787 | ||
2788 | /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ | |
2789 | hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? | |
2790 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); | |
115c9b81 | 2791 | |
fceb6435 | 2792 | if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { |
a4b64fbe ED |
2793 | if (tb[IFLA_EXT_MASK]) |
2794 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); | |
2795 | } | |
115c9b81 GR |
2796 | |
2797 | if (!ext_filter_mask) | |
2798 | return NLMSG_GOODSIZE; | |
2799 | /* | |
2800 | * traverse the list of net devices and compute the minimum | |
2801 | * buffer size based upon the filter mask. | |
2802 | */ | |
6853dd48 FW |
2803 | rcu_read_lock(); |
2804 | for_each_netdev_rcu(net, dev) { | |
115c9b81 GR |
2805 | min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, |
2806 | if_nlmsg_size(dev, | |
2807 | ext_filter_mask)); | |
2808 | } | |
6853dd48 | 2809 | rcu_read_unlock(); |
115c9b81 | 2810 | |
93af2056 | 2811 | return nlmsg_total_size(min_ifinfo_dump_size); |
c7ac8679 GR |
2812 | } |
2813 | ||
42bad1da | 2814 | static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) |
1da177e4 LT |
2815 | { |
2816 | int idx; | |
2817 | int s_idx = cb->family; | |
2818 | ||
2819 | if (s_idx == 0) | |
2820 | s_idx = 1; | |
6853dd48 | 2821 | |
25239cee | 2822 | for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { |
1da177e4 | 2823 | int type = cb->nlh->nlmsg_type-RTM_BASE; |
6853dd48 FW |
2824 | struct rtnl_link *handlers; |
2825 | rtnl_dumpit_func dumpit; | |
2826 | ||
1da177e4 LT |
2827 | if (idx < s_idx || idx == PF_PACKET) |
2828 | continue; | |
6853dd48 FW |
2829 | |
2830 | handlers = rtnl_dereference(rtnl_msg_handlers[idx]); | |
2831 | if (!handlers) | |
1da177e4 | 2832 | continue; |
6853dd48 FW |
2833 | |
2834 | dumpit = READ_ONCE(handlers[type].dumpit); | |
2835 | if (!dumpit) | |
2836 | continue; | |
2837 | ||
0465277f | 2838 | if (idx > s_idx) { |
1da177e4 | 2839 | memset(&cb->args[0], 0, sizeof(cb->args)); |
0465277f ND |
2840 | cb->prev_seq = 0; |
2841 | cb->seq = 0; | |
2842 | } | |
6853dd48 | 2843 | if (dumpit(skb, cb)) |
1da177e4 LT |
2844 | break; |
2845 | } | |
2846 | cb->family = idx; | |
2847 | ||
2848 | return skb->len; | |
2849 | } | |
2850 | ||
395eea6c | 2851 | struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, |
3d3ea5af VY |
2852 | unsigned int change, |
2853 | u32 event, gfp_t flags) | |
1da177e4 | 2854 | { |
c346dca1 | 2855 | struct net *net = dev_net(dev); |
1da177e4 | 2856 | struct sk_buff *skb; |
0ec6d3f4 | 2857 | int err = -ENOBUFS; |
c7ac8679 | 2858 | size_t if_info_size; |
1da177e4 | 2859 | |
7f294054 | 2860 | skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); |
0ec6d3f4 TG |
2861 | if (skb == NULL) |
2862 | goto errout; | |
1da177e4 | 2863 | |
3d3ea5af | 2864 | err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0, event); |
26932566 PM |
2865 | if (err < 0) { |
2866 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ | |
2867 | WARN_ON(err == -EMSGSIZE); | |
2868 | kfree_skb(skb); | |
2869 | goto errout; | |
2870 | } | |
395eea6c | 2871 | return skb; |
0ec6d3f4 TG |
2872 | errout: |
2873 | if (err < 0) | |
4b3da706 | 2874 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
395eea6c MB |
2875 | return NULL; |
2876 | } | |
2877 | ||
2878 | void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) | |
2879 | { | |
2880 | struct net *net = dev_net(dev); | |
2881 | ||
2882 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); | |
2883 | } | |
2884 | ||
3d3ea5af VY |
2885 | static void rtmsg_ifinfo_event(int type, struct net_device *dev, |
2886 | unsigned int change, u32 event, | |
2887 | gfp_t flags) | |
395eea6c MB |
2888 | { |
2889 | struct sk_buff *skb; | |
2890 | ||
ed2a80ab ND |
2891 | if (dev->reg_state != NETREG_REGISTERED) |
2892 | return; | |
2893 | ||
3d3ea5af | 2894 | skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags); |
395eea6c MB |
2895 | if (skb) |
2896 | rtmsg_ifinfo_send(skb, dev, flags); | |
1da177e4 | 2897 | } |
3d3ea5af VY |
2898 | |
2899 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, | |
2900 | gfp_t flags) | |
2901 | { | |
8c6c918d | 2902 | rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags); |
3d3ea5af | 2903 | } |
471cb5a3 | 2904 | EXPORT_SYMBOL(rtmsg_ifinfo); |
1da177e4 | 2905 | |
d83b0603 JF |
2906 | static int nlmsg_populate_fdb_fill(struct sk_buff *skb, |
2907 | struct net_device *dev, | |
1e53d5bb | 2908 | u8 *addr, u16 vid, u32 pid, u32 seq, |
1c104a6b | 2909 | int type, unsigned int flags, |
b3379041 | 2910 | int nlflags, u16 ndm_state) |
d83b0603 JF |
2911 | { |
2912 | struct nlmsghdr *nlh; | |
2913 | struct ndmsg *ndm; | |
2914 | ||
1c104a6b | 2915 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); |
d83b0603 JF |
2916 | if (!nlh) |
2917 | return -EMSGSIZE; | |
2918 | ||
2919 | ndm = nlmsg_data(nlh); | |
2920 | ndm->ndm_family = AF_BRIDGE; | |
2921 | ndm->ndm_pad1 = 0; | |
2922 | ndm->ndm_pad2 = 0; | |
2923 | ndm->ndm_flags = flags; | |
2924 | ndm->ndm_type = 0; | |
2925 | ndm->ndm_ifindex = dev->ifindex; | |
b3379041 | 2926 | ndm->ndm_state = ndm_state; |
d83b0603 JF |
2927 | |
2928 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) | |
2929 | goto nla_put_failure; | |
1e53d5bb HS |
2930 | if (vid) |
2931 | if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) | |
2932 | goto nla_put_failure; | |
d83b0603 | 2933 | |
053c095a JB |
2934 | nlmsg_end(skb, nlh); |
2935 | return 0; | |
d83b0603 JF |
2936 | |
2937 | nla_put_failure: | |
2938 | nlmsg_cancel(skb, nlh); | |
2939 | return -EMSGSIZE; | |
2940 | } | |
2941 | ||
3ff661c3 JF |
2942 | static inline size_t rtnl_fdb_nlmsg_size(void) |
2943 | { | |
f82ef3e1 SD |
2944 | return NLMSG_ALIGN(sizeof(struct ndmsg)) + |
2945 | nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ | |
2946 | nla_total_size(sizeof(u16)) + /* NDA_VLAN */ | |
2947 | 0; | |
3ff661c3 JF |
2948 | } |
2949 | ||
b3379041 HS |
2950 | static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, |
2951 | u16 ndm_state) | |
3ff661c3 JF |
2952 | { |
2953 | struct net *net = dev_net(dev); | |
2954 | struct sk_buff *skb; | |
2955 | int err = -ENOBUFS; | |
2956 | ||
2957 | skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); | |
2958 | if (!skb) | |
2959 | goto errout; | |
2960 | ||
1e53d5bb | 2961 | err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, |
b3379041 | 2962 | 0, 0, type, NTF_SELF, 0, ndm_state); |
3ff661c3 JF |
2963 | if (err < 0) { |
2964 | kfree_skb(skb); | |
2965 | goto errout; | |
2966 | } | |
2967 | ||
2968 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); | |
2969 | return; | |
2970 | errout: | |
2971 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); | |
2972 | } | |
2973 | ||
090096bf VY |
2974 | /** |
2975 | * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry | |
2976 | */ | |
2977 | int ndo_dflt_fdb_add(struct ndmsg *ndm, | |
2978 | struct nlattr *tb[], | |
2979 | struct net_device *dev, | |
f6f6424b | 2980 | const unsigned char *addr, u16 vid, |
090096bf VY |
2981 | u16 flags) |
2982 | { | |
2983 | int err = -EINVAL; | |
2984 | ||
2985 | /* If aging addresses are supported device will need to | |
2986 | * implement its own handler for this. | |
2987 | */ | |
2988 | if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { | |
2989 | pr_info("%s: FDB only supports static addresses\n", dev->name); | |
2990 | return err; | |
2991 | } | |
2992 | ||
65891fea OG |
2993 | if (vid) { |
2994 | pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); | |
2995 | return err; | |
2996 | } | |
2997 | ||
090096bf VY |
2998 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
2999 | err = dev_uc_add_excl(dev, addr); | |
3000 | else if (is_multicast_ether_addr(addr)) | |
3001 | err = dev_mc_add_excl(dev, addr); | |
3002 | ||
3003 | /* Only return duplicate errors if NLM_F_EXCL is set */ | |
3004 | if (err == -EEXIST && !(flags & NLM_F_EXCL)) | |
3005 | err = 0; | |
3006 | ||
3007 | return err; | |
3008 | } | |
3009 | EXPORT_SYMBOL(ndo_dflt_fdb_add); | |
3010 | ||
f6f6424b JP |
3011 | static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid) |
3012 | { | |
3013 | u16 vid = 0; | |
3014 | ||
3015 | if (vlan_attr) { | |
3016 | if (nla_len(vlan_attr) != sizeof(u16)) { | |
3017 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n"); | |
3018 | return -EINVAL; | |
3019 | } | |
3020 | ||
3021 | vid = nla_get_u16(vlan_attr); | |
3022 | ||
3023 | if (!vid || vid >= VLAN_VID_MASK) { | |
3024 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n", | |
3025 | vid); | |
3026 | return -EINVAL; | |
3027 | } | |
3028 | } | |
3029 | *p_vid = vid; | |
3030 | return 0; | |
3031 | } | |
3032 | ||
c21ef3e3 DA |
3033 | static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, |
3034 | struct netlink_ext_ack *extack) | |
77162022 JF |
3035 | { |
3036 | struct net *net = sock_net(skb->sk); | |
77162022 JF |
3037 | struct ndmsg *ndm; |
3038 | struct nlattr *tb[NDA_MAX+1]; | |
3039 | struct net_device *dev; | |
3040 | u8 *addr; | |
f6f6424b | 3041 | u16 vid; |
77162022 JF |
3042 | int err; |
3043 | ||
c21ef3e3 | 3044 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); |
77162022 JF |
3045 | if (err < 0) |
3046 | return err; | |
3047 | ||
3048 | ndm = nlmsg_data(nlh); | |
3049 | if (ndm->ndm_ifindex == 0) { | |
3050 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n"); | |
3051 | return -EINVAL; | |
3052 | } | |
3053 | ||
3054 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); | |
3055 | if (dev == NULL) { | |
3056 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n"); | |
3057 | return -ENODEV; | |
3058 | } | |
3059 | ||
3060 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { | |
3061 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n"); | |
3062 | return -EINVAL; | |
3063 | } | |
3064 | ||
3065 | addr = nla_data(tb[NDA_LLADDR]); | |
77162022 | 3066 | |
f6f6424b JP |
3067 | err = fdb_vid_parse(tb[NDA_VLAN], &vid); |
3068 | if (err) | |
3069 | return err; | |
3070 | ||
77162022 JF |
3071 | err = -EOPNOTSUPP; |
3072 | ||
3073 | /* Support fdb on master device the net/bridge default case */ | |
3074 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && | |
3075 | (dev->priv_flags & IFF_BRIDGE_PORT)) { | |
898e5061 JP |
3076 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
3077 | const struct net_device_ops *ops = br_dev->netdev_ops; | |
3078 | ||
f6f6424b JP |
3079 | err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, |
3080 | nlh->nlmsg_flags); | |
77162022 JF |
3081 | if (err) |
3082 | goto out; | |
3083 | else | |
3084 | ndm->ndm_flags &= ~NTF_MASTER; | |
3085 | } | |
3086 | ||
3087 | /* Embedded bridge, macvlan, and any other device support */ | |
090096bf VY |
3088 | if ((ndm->ndm_flags & NTF_SELF)) { |
3089 | if (dev->netdev_ops->ndo_fdb_add) | |
3090 | err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, | |
f6f6424b | 3091 | vid, |
090096bf VY |
3092 | nlh->nlmsg_flags); |
3093 | else | |
f6f6424b | 3094 | err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, |
090096bf | 3095 | nlh->nlmsg_flags); |
77162022 | 3096 | |
3ff661c3 | 3097 | if (!err) { |
b3379041 HS |
3098 | rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, |
3099 | ndm->ndm_state); | |
77162022 | 3100 | ndm->ndm_flags &= ~NTF_SELF; |
3ff661c3 | 3101 | } |
77162022 JF |
3102 | } |
3103 | out: | |
3104 | return err; | |
3105 | } | |
3106 | ||
090096bf VY |
3107 | /** |
3108 | * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry | |
3109 | */ | |
3110 | int ndo_dflt_fdb_del(struct ndmsg *ndm, | |
3111 | struct nlattr *tb[], | |
3112 | struct net_device *dev, | |
f6f6424b | 3113 | const unsigned char *addr, u16 vid) |
090096bf | 3114 | { |
c8a89c4a | 3115 | int err = -EINVAL; |
090096bf VY |
3116 | |
3117 | /* If aging addresses are supported device will need to | |
3118 | * implement its own handler for this. | |
3119 | */ | |
64535993 | 3120 | if (!(ndm->ndm_state & NUD_PERMANENT)) { |
090096bf | 3121 | pr_info("%s: FDB only supports static addresses\n", dev->name); |
c8a89c4a | 3122 | return err; |
090096bf VY |
3123 | } |
3124 | ||
3125 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) | |
3126 | err = dev_uc_del(dev, addr); | |
3127 | else if (is_multicast_ether_addr(addr)) | |
3128 | err = dev_mc_del(dev, addr); | |
090096bf VY |
3129 | |
3130 | return err; | |
3131 | } | |
3132 | EXPORT_SYMBOL(ndo_dflt_fdb_del); | |
3133 | ||
c21ef3e3 DA |
3134 | static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, |
3135 | struct netlink_ext_ack *extack) | |
77162022 JF |
3136 | { |
3137 | struct net *net = sock_net(skb->sk); | |
3138 | struct ndmsg *ndm; | |
1690be63 | 3139 | struct nlattr *tb[NDA_MAX+1]; |
77162022 JF |
3140 | struct net_device *dev; |
3141 | int err = -EINVAL; | |
3142 | __u8 *addr; | |
f6f6424b | 3143 | u16 vid; |
77162022 | 3144 | |
90f62cf3 | 3145 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
1690be63 VY |
3146 | return -EPERM; |
3147 | ||
c21ef3e3 | 3148 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); |
1690be63 VY |
3149 | if (err < 0) |
3150 | return err; | |
77162022 JF |
3151 | |
3152 | ndm = nlmsg_data(nlh); | |
3153 | if (ndm->ndm_ifindex == 0) { | |
3154 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n"); | |
3155 | return -EINVAL; | |
3156 | } | |
3157 | ||
3158 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); | |
3159 | if (dev == NULL) { | |
3160 | pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n"); | |
3161 | return -ENODEV; | |
3162 | } | |
3163 | ||
1690be63 VY |
3164 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
3165 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n"); | |
3166 | return -EINVAL; | |
3167 | } | |
3168 | ||
3169 | addr = nla_data(tb[NDA_LLADDR]); | |
77162022 | 3170 | |
f6f6424b JP |
3171 | err = fdb_vid_parse(tb[NDA_VLAN], &vid); |
3172 | if (err) | |
3173 | return err; | |
3174 | ||
77162022 JF |
3175 | err = -EOPNOTSUPP; |
3176 | ||
3177 | /* Support fdb on master device the net/bridge default case */ | |
3178 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && | |
3179 | (dev->priv_flags & IFF_BRIDGE_PORT)) { | |
898e5061 JP |
3180 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
3181 | const struct net_device_ops *ops = br_dev->netdev_ops; | |
77162022 | 3182 | |
898e5061 | 3183 | if (ops->ndo_fdb_del) |
f6f6424b | 3184 | err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); |
77162022 JF |
3185 | |
3186 | if (err) | |
3187 | goto out; | |
3188 | else | |
3189 | ndm->ndm_flags &= ~NTF_MASTER; | |
3190 | } | |
3191 | ||
3192 | /* Embedded bridge, macvlan, and any other device support */ | |
090096bf VY |
3193 | if (ndm->ndm_flags & NTF_SELF) { |
3194 | if (dev->netdev_ops->ndo_fdb_del) | |
f6f6424b JP |
3195 | err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, |
3196 | vid); | |
090096bf | 3197 | else |
f6f6424b | 3198 | err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); |
77162022 | 3199 | |
3ff661c3 | 3200 | if (!err) { |
b3379041 HS |
3201 | rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, |
3202 | ndm->ndm_state); | |
77162022 | 3203 | ndm->ndm_flags &= ~NTF_SELF; |
3ff661c3 | 3204 | } |
77162022 JF |
3205 | } |
3206 | out: | |
3207 | return err; | |
3208 | } | |
3209 | ||
d83b0603 JF |
3210 | static int nlmsg_populate_fdb(struct sk_buff *skb, |
3211 | struct netlink_callback *cb, | |
3212 | struct net_device *dev, | |
3213 | int *idx, | |
3214 | struct netdev_hw_addr_list *list) | |
3215 | { | |
3216 | struct netdev_hw_addr *ha; | |
3217 | int err; | |
15e47304 | 3218 | u32 portid, seq; |
d83b0603 | 3219 | |
15e47304 | 3220 | portid = NETLINK_CB(cb->skb).portid; |
d83b0603 JF |
3221 | seq = cb->nlh->nlmsg_seq; |
3222 | ||
3223 | list_for_each_entry(ha, &list->list, list) { | |
d297653d | 3224 | if (*idx < cb->args[2]) |
d83b0603 JF |
3225 | goto skip; |
3226 | ||
1e53d5bb | 3227 | err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, |
a7a558fe | 3228 | portid, seq, |
1c104a6b | 3229 | RTM_NEWNEIGH, NTF_SELF, |
b3379041 | 3230 | NLM_F_MULTI, NUD_PERMANENT); |
d83b0603 JF |
3231 | if (err < 0) |
3232 | return err; | |
3233 | skip: | |
3234 | *idx += 1; | |
3235 | } | |
3236 | return 0; | |
3237 | } | |
3238 | ||
3239 | /** | |
2c53040f | 3240 | * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. |
d83b0603 JF |
3241 | * @nlh: netlink message header |
3242 | * @dev: netdevice | |
3243 | * | |
3244 | * Default netdevice operation to dump the existing unicast address list. | |
91f3e7b1 | 3245 | * Returns number of addresses from list put in skb. |
d83b0603 JF |
3246 | */ |
3247 | int ndo_dflt_fdb_dump(struct sk_buff *skb, | |
3248 | struct netlink_callback *cb, | |
3249 | struct net_device *dev, | |
5d5eacb3 | 3250 | struct net_device *filter_dev, |
d297653d | 3251 | int *idx) |
d83b0603 JF |
3252 | { |
3253 | int err; | |
3254 | ||
3255 | netif_addr_lock_bh(dev); | |
d297653d | 3256 | err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); |
d83b0603 JF |
3257 | if (err) |
3258 | goto out; | |
2934c9db | 3259 | err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); |
d83b0603 JF |
3260 | out: |
3261 | netif_addr_unlock_bh(dev); | |
d297653d | 3262 | return err; |
d83b0603 JF |
3263 | } |
3264 | EXPORT_SYMBOL(ndo_dflt_fdb_dump); | |
3265 | ||
77162022 JF |
3266 | static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) |
3267 | { | |
77162022 | 3268 | struct net_device *dev; |
5e6d2435 | 3269 | struct nlattr *tb[IFLA_MAX+1]; |
5e6d2435 JHS |
3270 | struct net_device *br_dev = NULL; |
3271 | const struct net_device_ops *ops = NULL; | |
3272 | const struct net_device_ops *cops = NULL; | |
3273 | struct ifinfomsg *ifm = nlmsg_data(cb->nlh); | |
3274 | struct net *net = sock_net(skb->sk); | |
d297653d | 3275 | struct hlist_head *head; |
5e6d2435 JHS |
3276 | int brport_idx = 0; |
3277 | int br_idx = 0; | |
d297653d RP |
3278 | int h, s_h; |
3279 | int idx = 0, s_idx; | |
3280 | int err = 0; | |
3281 | int fidx = 0; | |
5e6d2435 | 3282 | |
0ff50e83 AP |
3283 | err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, |
3284 | IFLA_MAX, ifla_policy, NULL); | |
3285 | if (err < 0) { | |
3286 | return -EINVAL; | |
3287 | } else if (err == 0) { | |
5e6d2435 JHS |
3288 | if (tb[IFLA_MASTER]) |
3289 | br_idx = nla_get_u32(tb[IFLA_MASTER]); | |
3290 | } | |
3291 | ||
3292 | brport_idx = ifm->ifi_index; | |
3293 | ||
3294 | if (br_idx) { | |
3295 | br_dev = __dev_get_by_index(net, br_idx); | |
3296 | if (!br_dev) | |
3297 | return -ENODEV; | |
3298 | ||
3299 | ops = br_dev->netdev_ops; | |
5e6d2435 JHS |
3300 | } |
3301 | ||
d297653d RP |
3302 | s_h = cb->args[0]; |
3303 | s_idx = cb->args[1]; | |
5e6d2435 | 3304 | |
d297653d RP |
3305 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
3306 | idx = 0; | |
3307 | head = &net->dev_index_head[h]; | |
3308 | hlist_for_each_entry(dev, head, index_hlist) { | |
5e6d2435 | 3309 | |
d297653d | 3310 | if (brport_idx && (dev->ifindex != brport_idx)) |
5e6d2435 JHS |
3311 | continue; |
3312 | ||
d297653d RP |
3313 | if (!br_idx) { /* user did not specify a specific bridge */ |
3314 | if (dev->priv_flags & IFF_BRIDGE_PORT) { | |
3315 | br_dev = netdev_master_upper_dev_get(dev); | |
3316 | cops = br_dev->netdev_ops; | |
3317 | } | |
3318 | } else { | |
3319 | if (dev != br_dev && | |
3320 | !(dev->priv_flags & IFF_BRIDGE_PORT)) | |
3321 | continue; | |
5e6d2435 | 3322 | |
d297653d RP |
3323 | if (br_dev != netdev_master_upper_dev_get(dev) && |
3324 | !(dev->priv_flags & IFF_EBRIDGE)) | |
3325 | continue; | |
3326 | cops = ops; | |
3327 | } | |
77162022 | 3328 | |
d297653d RP |
3329 | if (idx < s_idx) |
3330 | goto cont; | |
77162022 | 3331 | |
d297653d RP |
3332 | if (dev->priv_flags & IFF_BRIDGE_PORT) { |
3333 | if (cops && cops->ndo_fdb_dump) { | |
3334 | err = cops->ndo_fdb_dump(skb, cb, | |
3335 | br_dev, dev, | |
3336 | &fidx); | |
3337 | if (err == -EMSGSIZE) | |
3338 | goto out; | |
3339 | } | |
3340 | } | |
5e6d2435 | 3341 | |
d297653d RP |
3342 | if (dev->netdev_ops->ndo_fdb_dump) |
3343 | err = dev->netdev_ops->ndo_fdb_dump(skb, cb, | |
3344 | dev, NULL, | |
3345 | &fidx); | |
3346 | else | |
3347 | err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, | |
3348 | &fidx); | |
3349 | if (err == -EMSGSIZE) | |
3350 | goto out; | |
3351 | ||
3352 | cops = NULL; | |
3353 | ||
3354 | /* reset fdb offset to 0 for rest of the interfaces */ | |
3355 | cb->args[2] = 0; | |
3356 | fidx = 0; | |
3357 | cont: | |
3358 | idx++; | |
3359 | } | |
77162022 | 3360 | } |
77162022 | 3361 | |
d297653d RP |
3362 | out: |
3363 | cb->args[0] = h; | |
3364 | cb->args[1] = idx; | |
3365 | cb->args[2] = fidx; | |
3366 | ||
77162022 JF |
3367 | return skb->len; |
3368 | } | |
3369 | ||
2c3c031c SF |
3370 | static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, |
3371 | unsigned int attrnum, unsigned int flag) | |
3372 | { | |
3373 | if (mask & flag) | |
3374 | return nla_put_u8(skb, attrnum, !!(flags & flag)); | |
3375 | return 0; | |
3376 | } | |
3377 | ||
815cccbf | 3378 | int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
2c3c031c | 3379 | struct net_device *dev, u16 mode, |
7d4f8d87 SF |
3380 | u32 flags, u32 mask, int nlflags, |
3381 | u32 filter_mask, | |
3382 | int (*vlan_fill)(struct sk_buff *skb, | |
3383 | struct net_device *dev, | |
3384 | u32 filter_mask)) | |
815cccbf JF |
3385 | { |
3386 | struct nlmsghdr *nlh; | |
3387 | struct ifinfomsg *ifm; | |
3388 | struct nlattr *br_afspec; | |
2c3c031c | 3389 | struct nlattr *protinfo; |
815cccbf | 3390 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; |
898e5061 | 3391 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
7d4f8d87 | 3392 | int err = 0; |
815cccbf | 3393 | |
46c264da | 3394 | nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); |
815cccbf JF |
3395 | if (nlh == NULL) |
3396 | return -EMSGSIZE; | |
3397 | ||
3398 | ifm = nlmsg_data(nlh); | |
3399 | ifm->ifi_family = AF_BRIDGE; | |
3400 | ifm->__ifi_pad = 0; | |
3401 | ifm->ifi_type = dev->type; | |
3402 | ifm->ifi_index = dev->ifindex; | |
3403 | ifm->ifi_flags = dev_get_flags(dev); | |
3404 | ifm->ifi_change = 0; | |
3405 | ||
3406 | ||
3407 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || | |
3408 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || | |
3409 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || | |
898e5061 JP |
3410 | (br_dev && |
3411 | nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || | |
815cccbf JF |
3412 | (dev->addr_len && |
3413 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || | |
a54acb3a ND |
3414 | (dev->ifindex != dev_get_iflink(dev) && |
3415 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) | |
815cccbf JF |
3416 | goto nla_put_failure; |
3417 | ||
3418 | br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); | |
3419 | if (!br_afspec) | |
3420 | goto nla_put_failure; | |
3421 | ||
1d460b98 | 3422 | if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { |
815cccbf JF |
3423 | nla_nest_cancel(skb, br_afspec); |
3424 | goto nla_put_failure; | |
3425 | } | |
1d460b98 RP |
3426 | |
3427 | if (mode != BRIDGE_MODE_UNDEF) { | |
3428 | if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { | |
3429 | nla_nest_cancel(skb, br_afspec); | |
3430 | goto nla_put_failure; | |
3431 | } | |
3432 | } | |
7d4f8d87 SF |
3433 | if (vlan_fill) { |
3434 | err = vlan_fill(skb, dev, filter_mask); | |
3435 | if (err) { | |
3436 | nla_nest_cancel(skb, br_afspec); | |
3437 | goto nla_put_failure; | |
3438 | } | |
3439 | } | |
815cccbf JF |
3440 | nla_nest_end(skb, br_afspec); |
3441 | ||
2c3c031c SF |
3442 | protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); |
3443 | if (!protinfo) | |
3444 | goto nla_put_failure; | |
3445 | ||
3446 | if (brport_nla_put_flag(skb, flags, mask, | |
3447 | IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || | |
3448 | brport_nla_put_flag(skb, flags, mask, | |
3449 | IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || | |
3450 | brport_nla_put_flag(skb, flags, mask, | |
3451 | IFLA_BRPORT_FAST_LEAVE, | |
3452 | BR_MULTICAST_FAST_LEAVE) || | |
3453 | brport_nla_put_flag(skb, flags, mask, | |
3454 | IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || | |
3455 | brport_nla_put_flag(skb, flags, mask, | |
3456 | IFLA_BRPORT_LEARNING, BR_LEARNING) || | |
3457 | brport_nla_put_flag(skb, flags, mask, | |
3458 | IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || | |
3459 | brport_nla_put_flag(skb, flags, mask, | |
3460 | IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || | |
3461 | brport_nla_put_flag(skb, flags, mask, | |
3462 | IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { | |
3463 | nla_nest_cancel(skb, protinfo); | |
3464 | goto nla_put_failure; | |
3465 | } | |
3466 | ||
3467 | nla_nest_end(skb, protinfo); | |
3468 | ||
053c095a JB |
3469 | nlmsg_end(skb, nlh); |
3470 | return 0; | |
815cccbf JF |
3471 | nla_put_failure: |
3472 | nlmsg_cancel(skb, nlh); | |
7d4f8d87 | 3473 | return err ? err : -EMSGSIZE; |
815cccbf | 3474 | } |
7d4f8d87 | 3475 | EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); |
815cccbf | 3476 | |
e5a55a89 JF |
3477 | static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) |
3478 | { | |
3479 | struct net *net = sock_net(skb->sk); | |
3480 | struct net_device *dev; | |
3481 | int idx = 0; | |
3482 | u32 portid = NETLINK_CB(cb->skb).portid; | |
3483 | u32 seq = cb->nlh->nlmsg_seq; | |
6cbdceeb | 3484 | u32 filter_mask = 0; |
d64f69b0 | 3485 | int err; |
6cbdceeb | 3486 | |
aa68c20f TG |
3487 | if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { |
3488 | struct nlattr *extfilt; | |
3489 | ||
3490 | extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), | |
3491 | IFLA_EXT_MASK); | |
3492 | if (extfilt) { | |
3493 | if (nla_len(extfilt) < sizeof(filter_mask)) | |
3494 | return -EINVAL; | |
3495 | ||
3496 | filter_mask = nla_get_u32(extfilt); | |
3497 | } | |
3498 | } | |
e5a55a89 JF |
3499 | |
3500 | rcu_read_lock(); | |
3501 | for_each_netdev_rcu(net, dev) { | |
3502 | const struct net_device_ops *ops = dev->netdev_ops; | |
898e5061 | 3503 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
e5a55a89 | 3504 | |
898e5061 | 3505 | if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { |
d64f69b0 RP |
3506 | if (idx >= cb->args[0]) { |
3507 | err = br_dev->netdev_ops->ndo_bridge_getlink( | |
3508 | skb, portid, seq, dev, | |
3509 | filter_mask, NLM_F_MULTI); | |
f6c5775f DA |
3510 | if (err < 0 && err != -EOPNOTSUPP) { |
3511 | if (likely(skb->len)) | |
3512 | break; | |
3513 | ||
3514 | goto out_err; | |
3515 | } | |
d64f69b0 | 3516 | } |
25b1e679 | 3517 | idx++; |
e5a55a89 JF |
3518 | } |
3519 | ||
3520 | if (ops->ndo_bridge_getlink) { | |
d64f69b0 RP |
3521 | if (idx >= cb->args[0]) { |
3522 | err = ops->ndo_bridge_getlink(skb, portid, | |
3523 | seq, dev, | |
3524 | filter_mask, | |
3525 | NLM_F_MULTI); | |
f6c5775f DA |
3526 | if (err < 0 && err != -EOPNOTSUPP) { |
3527 | if (likely(skb->len)) | |
3528 | break; | |
3529 | ||
3530 | goto out_err; | |
3531 | } | |
d64f69b0 | 3532 | } |
25b1e679 | 3533 | idx++; |
e5a55a89 JF |
3534 | } |
3535 | } | |
f6c5775f DA |
3536 | err = skb->len; |
3537 | out_err: | |
e5a55a89 JF |
3538 | rcu_read_unlock(); |
3539 | cb->args[0] = idx; | |
3540 | ||
f6c5775f | 3541 | return err; |
e5a55a89 JF |
3542 | } |
3543 | ||
2469ffd7 JF |
3544 | static inline size_t bridge_nlmsg_size(void) |
3545 | { | |
3546 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
3547 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ | |
3548 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ | |
3549 | + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ | |
3550 | + nla_total_size(sizeof(u32)) /* IFLA_MTU */ | |
3551 | + nla_total_size(sizeof(u32)) /* IFLA_LINK */ | |
3552 | + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ | |
3553 | + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ | |
3554 | + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ | |
3555 | + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ | |
3556 | + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ | |
3557 | } | |
3558 | ||
02dba438 | 3559 | static int rtnl_bridge_notify(struct net_device *dev) |
2469ffd7 JF |
3560 | { |
3561 | struct net *net = dev_net(dev); | |
2469ffd7 JF |
3562 | struct sk_buff *skb; |
3563 | int err = -EOPNOTSUPP; | |
3564 | ||
02dba438 RP |
3565 | if (!dev->netdev_ops->ndo_bridge_getlink) |
3566 | return 0; | |
3567 | ||
2469ffd7 JF |
3568 | skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); |
3569 | if (!skb) { | |
3570 | err = -ENOMEM; | |
3571 | goto errout; | |
3572 | } | |
3573 | ||
46c264da | 3574 | err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); |
02dba438 RP |
3575 | if (err < 0) |
3576 | goto errout; | |
2469ffd7 | 3577 | |
59ccaaaa RP |
3578 | if (!skb->len) |
3579 | goto errout; | |
3580 | ||
2469ffd7 JF |
3581 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
3582 | return 0; | |
3583 | errout: | |
3584 | WARN_ON(err == -EMSGSIZE); | |
3585 | kfree_skb(skb); | |
59ccaaaa RP |
3586 | if (err) |
3587 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); | |
2469ffd7 JF |
3588 | return err; |
3589 | } | |
3590 | ||
c21ef3e3 DA |
3591 | static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
3592 | struct netlink_ext_ack *extack) | |
e5a55a89 JF |
3593 | { |
3594 | struct net *net = sock_net(skb->sk); | |
3595 | struct ifinfomsg *ifm; | |
3596 | struct net_device *dev; | |
2469ffd7 JF |
3597 | struct nlattr *br_spec, *attr = NULL; |
3598 | int rem, err = -EOPNOTSUPP; | |
4de8b413 | 3599 | u16 flags = 0; |
c38e01b8 | 3600 | bool have_flags = false; |
e5a55a89 JF |
3601 | |
3602 | if (nlmsg_len(nlh) < sizeof(*ifm)) | |
3603 | return -EINVAL; | |
3604 | ||
3605 | ifm = nlmsg_data(nlh); | |
3606 | if (ifm->ifi_family != AF_BRIDGE) | |
3607 | return -EPFNOSUPPORT; | |
3608 | ||
3609 | dev = __dev_get_by_index(net, ifm->ifi_index); | |
3610 | if (!dev) { | |
3611 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); | |
3612 | return -ENODEV; | |
3613 | } | |
3614 | ||
2469ffd7 JF |
3615 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
3616 | if (br_spec) { | |
3617 | nla_for_each_nested(attr, br_spec, rem) { | |
3618 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { | |
6e8d1c55 TG |
3619 | if (nla_len(attr) < sizeof(flags)) |
3620 | return -EINVAL; | |
3621 | ||
c38e01b8 | 3622 | have_flags = true; |
2469ffd7 JF |
3623 | flags = nla_get_u16(attr); |
3624 | break; | |
3625 | } | |
3626 | } | |
3627 | } | |
3628 | ||
3629 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { | |
898e5061 JP |
3630 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
3631 | ||
3632 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { | |
2469ffd7 JF |
3633 | err = -EOPNOTSUPP; |
3634 | goto out; | |
3635 | } | |
3636 | ||
add511b3 | 3637 | err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); |
e5a55a89 JF |
3638 | if (err) |
3639 | goto out; | |
2469ffd7 JF |
3640 | |
3641 | flags &= ~BRIDGE_FLAGS_MASTER; | |
e5a55a89 JF |
3642 | } |
3643 | ||
2469ffd7 JF |
3644 | if ((flags & BRIDGE_FLAGS_SELF)) { |
3645 | if (!dev->netdev_ops->ndo_bridge_setlink) | |
3646 | err = -EOPNOTSUPP; | |
3647 | else | |
add511b3 RP |
3648 | err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, |
3649 | flags); | |
02dba438 | 3650 | if (!err) { |
2469ffd7 | 3651 | flags &= ~BRIDGE_FLAGS_SELF; |
02dba438 RP |
3652 | |
3653 | /* Generate event to notify upper layer of bridge | |
3654 | * change | |
3655 | */ | |
3656 | err = rtnl_bridge_notify(dev); | |
3657 | } | |
2469ffd7 | 3658 | } |
e5a55a89 | 3659 | |
c38e01b8 | 3660 | if (have_flags) |
2469ffd7 | 3661 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
e5a55a89 JF |
3662 | out: |
3663 | return err; | |
3664 | } | |
3665 | ||
c21ef3e3 DA |
3666 | static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, |
3667 | struct netlink_ext_ack *extack) | |
407af329 VY |
3668 | { |
3669 | struct net *net = sock_net(skb->sk); | |
3670 | struct ifinfomsg *ifm; | |
3671 | struct net_device *dev; | |
3672 | struct nlattr *br_spec, *attr = NULL; | |
3673 | int rem, err = -EOPNOTSUPP; | |
4de8b413 | 3674 | u16 flags = 0; |
407af329 VY |
3675 | bool have_flags = false; |
3676 | ||
3677 | if (nlmsg_len(nlh) < sizeof(*ifm)) | |
3678 | return -EINVAL; | |
3679 | ||
3680 | ifm = nlmsg_data(nlh); | |
3681 | if (ifm->ifi_family != AF_BRIDGE) | |
3682 | return -EPFNOSUPPORT; | |
3683 | ||
3684 | dev = __dev_get_by_index(net, ifm->ifi_index); | |
3685 | if (!dev) { | |
3686 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); | |
3687 | return -ENODEV; | |
3688 | } | |
3689 | ||
3690 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); | |
3691 | if (br_spec) { | |
3692 | nla_for_each_nested(attr, br_spec, rem) { | |
3693 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { | |
6e8d1c55 TG |
3694 | if (nla_len(attr) < sizeof(flags)) |
3695 | return -EINVAL; | |
3696 | ||
407af329 VY |
3697 | have_flags = true; |
3698 | flags = nla_get_u16(attr); | |
3699 | break; | |
3700 | } | |
3701 | } | |
3702 | } | |
3703 | ||
407af329 VY |
3704 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
3705 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); | |
3706 | ||
3707 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { | |
3708 | err = -EOPNOTSUPP; | |
3709 | goto out; | |
3710 | } | |
3711 | ||
add511b3 | 3712 | err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); |
407af329 VY |
3713 | if (err) |
3714 | goto out; | |
3715 | ||
3716 | flags &= ~BRIDGE_FLAGS_MASTER; | |
3717 | } | |
3718 | ||
3719 | if ((flags & BRIDGE_FLAGS_SELF)) { | |
3720 | if (!dev->netdev_ops->ndo_bridge_dellink) | |
3721 | err = -EOPNOTSUPP; | |
3722 | else | |
add511b3 RP |
3723 | err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, |
3724 | flags); | |
407af329 | 3725 | |
02dba438 | 3726 | if (!err) { |
407af329 | 3727 | flags &= ~BRIDGE_FLAGS_SELF; |
02dba438 RP |
3728 | |
3729 | /* Generate event to notify upper layer of bridge | |
3730 | * change | |
3731 | */ | |
3732 | err = rtnl_bridge_notify(dev); | |
3733 | } | |
407af329 VY |
3734 | } |
3735 | ||
3736 | if (have_flags) | |
3737 | memcpy(nla_data(attr), &flags, sizeof(flags)); | |
407af329 VY |
3738 | out: |
3739 | return err; | |
3740 | } | |
3741 | ||
e8872a25 NA |
3742 | static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) |
3743 | { | |
3744 | return (mask & IFLA_STATS_FILTER_BIT(attrid)) && | |
3745 | (!idxattr || idxattr == attrid); | |
3746 | } | |
3747 | ||
69ae6ad2 NF |
3748 | #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) |
3749 | static int rtnl_get_offload_stats_attr_size(int attr_id) | |
3750 | { | |
3751 | switch (attr_id) { | |
3752 | case IFLA_OFFLOAD_XSTATS_CPU_HIT: | |
3753 | return sizeof(struct rtnl_link_stats64); | |
3754 | } | |
3755 | ||
3756 | return 0; | |
3757 | } | |
3758 | ||
3759 | static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, | |
3760 | int *prividx) | |
3761 | { | |
3762 | struct nlattr *attr = NULL; | |
3763 | int attr_id, size; | |
3764 | void *attr_data; | |
3765 | int err; | |
3766 | ||
3767 | if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && | |
3768 | dev->netdev_ops->ndo_get_offload_stats)) | |
3769 | return -ENODATA; | |
3770 | ||
3771 | for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; | |
3772 | attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { | |
3773 | if (attr_id < *prividx) | |
3774 | continue; | |
3775 | ||
3776 | size = rtnl_get_offload_stats_attr_size(attr_id); | |
3777 | if (!size) | |
3778 | continue; | |
3779 | ||
3df5b3c6 | 3780 | if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) |
69ae6ad2 NF |
3781 | continue; |
3782 | ||
3783 | attr = nla_reserve_64bit(skb, attr_id, size, | |
3784 | IFLA_OFFLOAD_XSTATS_UNSPEC); | |
3785 | if (!attr) | |
3786 | goto nla_put_failure; | |
3787 | ||
3788 | attr_data = nla_data(attr); | |
3789 | memset(attr_data, 0, size); | |
3790 | err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, | |
3791 | attr_data); | |
3792 | if (err) | |
3793 | goto get_offload_stats_failure; | |
3794 | } | |
3795 | ||
3796 | if (!attr) | |
3797 | return -ENODATA; | |
3798 | ||
3799 | *prividx = 0; | |
3800 | return 0; | |
3801 | ||
3802 | nla_put_failure: | |
3803 | err = -EMSGSIZE; | |
3804 | get_offload_stats_failure: | |
3805 | *prividx = attr_id; | |
3806 | return err; | |
3807 | } | |
3808 | ||
3809 | static int rtnl_get_offload_stats_size(const struct net_device *dev) | |
3810 | { | |
3811 | int nla_size = 0; | |
3812 | int attr_id; | |
3813 | int size; | |
3814 | ||
3815 | if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && | |
3816 | dev->netdev_ops->ndo_get_offload_stats)) | |
3817 | return 0; | |
3818 | ||
3819 | for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; | |
3820 | attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { | |
3df5b3c6 | 3821 | if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) |
69ae6ad2 NF |
3822 | continue; |
3823 | size = rtnl_get_offload_stats_attr_size(attr_id); | |
3824 | nla_size += nla_total_size_64bit(size); | |
3825 | } | |
3826 | ||
3827 | if (nla_size != 0) | |
3828 | nla_size += nla_total_size(0); | |
3829 | ||
3830 | return nla_size; | |
3831 | } | |
3832 | ||
10c9ead9 RP |
3833 | static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, |
3834 | int type, u32 pid, u32 seq, u32 change, | |
e8872a25 NA |
3835 | unsigned int flags, unsigned int filter_mask, |
3836 | int *idxattr, int *prividx) | |
10c9ead9 RP |
3837 | { |
3838 | struct if_stats_msg *ifsm; | |
3839 | struct nlmsghdr *nlh; | |
3840 | struct nlattr *attr; | |
e8872a25 | 3841 | int s_prividx = *prividx; |
69ae6ad2 | 3842 | int err; |
10c9ead9 RP |
3843 | |
3844 | ASSERT_RTNL(); | |
3845 | ||
3846 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); | |
3847 | if (!nlh) | |
3848 | return -EMSGSIZE; | |
3849 | ||
3850 | ifsm = nlmsg_data(nlh); | |
3851 | ifsm->ifindex = dev->ifindex; | |
3852 | ifsm->filter_mask = filter_mask; | |
3853 | ||
e8872a25 | 3854 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { |
10c9ead9 | 3855 | struct rtnl_link_stats64 *sp; |
10c9ead9 | 3856 | |
58414d32 ND |
3857 | attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, |
3858 | sizeof(struct rtnl_link_stats64), | |
3859 | IFLA_STATS_UNSPEC); | |
10c9ead9 RP |
3860 | if (!attr) |
3861 | goto nla_put_failure; | |
3862 | ||
3863 | sp = nla_data(attr); | |
3864 | dev_get_stats(dev, sp); | |
3865 | } | |
3866 | ||
97a47fac NA |
3867 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { |
3868 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; | |
3869 | ||
3870 | if (ops && ops->fill_linkxstats) { | |
97a47fac NA |
3871 | *idxattr = IFLA_STATS_LINK_XSTATS; |
3872 | attr = nla_nest_start(skb, | |
3873 | IFLA_STATS_LINK_XSTATS); | |
3874 | if (!attr) | |
3875 | goto nla_put_failure; | |
3876 | ||
80e73cc5 NA |
3877 | err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); |
3878 | nla_nest_end(skb, attr); | |
3879 | if (err) | |
3880 | goto nla_put_failure; | |
3881 | *idxattr = 0; | |
3882 | } | |
3883 | } | |
3884 | ||
3885 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, | |
3886 | *idxattr)) { | |
3887 | const struct rtnl_link_ops *ops = NULL; | |
3888 | const struct net_device *master; | |
3889 | ||
3890 | master = netdev_master_upper_dev_get(dev); | |
3891 | if (master) | |
3892 | ops = master->rtnl_link_ops; | |
3893 | if (ops && ops->fill_linkxstats) { | |
80e73cc5 NA |
3894 | *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; |
3895 | attr = nla_nest_start(skb, | |
3896 | IFLA_STATS_LINK_XSTATS_SLAVE); | |
3897 | if (!attr) | |
3898 | goto nla_put_failure; | |
3899 | ||
3900 | err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); | |
97a47fac NA |
3901 | nla_nest_end(skb, attr); |
3902 | if (err) | |
3903 | goto nla_put_failure; | |
3904 | *idxattr = 0; | |
3905 | } | |
3906 | } | |
3907 | ||
69ae6ad2 NF |
3908 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, |
3909 | *idxattr)) { | |
3910 | *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; | |
3911 | attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS); | |
3912 | if (!attr) | |
3913 | goto nla_put_failure; | |
3914 | ||
3915 | err = rtnl_get_offload_stats(skb, dev, prividx); | |
3916 | if (err == -ENODATA) | |
3917 | nla_nest_cancel(skb, attr); | |
3918 | else | |
3919 | nla_nest_end(skb, attr); | |
3920 | ||
3921 | if (err && err != -ENODATA) | |
3922 | goto nla_put_failure; | |
3923 | *idxattr = 0; | |
3924 | } | |
3925 | ||
aefb4d4a RS |
3926 | if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { |
3927 | struct rtnl_af_ops *af_ops; | |
3928 | ||
3929 | *idxattr = IFLA_STATS_AF_SPEC; | |
3930 | attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC); | |
3931 | if (!attr) | |
3932 | goto nla_put_failure; | |
3933 | ||
3934 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { | |
3935 | if (af_ops->fill_stats_af) { | |
3936 | struct nlattr *af; | |
3937 | int err; | |
3938 | ||
3939 | af = nla_nest_start(skb, af_ops->family); | |
3940 | if (!af) | |
3941 | goto nla_put_failure; | |
3942 | ||
3943 | err = af_ops->fill_stats_af(skb, dev); | |
3944 | ||
3945 | if (err == -ENODATA) | |
3946 | nla_nest_cancel(skb, af); | |
3947 | else if (err < 0) | |
3948 | goto nla_put_failure; | |
3949 | ||
3950 | nla_nest_end(skb, af); | |
3951 | } | |
3952 | } | |
3953 | ||
3954 | nla_nest_end(skb, attr); | |
3955 | ||
3956 | *idxattr = 0; | |
3957 | } | |
3958 | ||
10c9ead9 RP |
3959 | nlmsg_end(skb, nlh); |
3960 | ||
3961 | return 0; | |
3962 | ||
3963 | nla_put_failure: | |
e8872a25 NA |
3964 | /* not a multi message or no progress mean a real error */ |
3965 | if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) | |
3966 | nlmsg_cancel(skb, nlh); | |
3967 | else | |
3968 | nlmsg_end(skb, nlh); | |
10c9ead9 RP |
3969 | |
3970 | return -EMSGSIZE; | |
3971 | } | |
3972 | ||
10c9ead9 RP |
3973 | static size_t if_nlmsg_stats_size(const struct net_device *dev, |
3974 | u32 filter_mask) | |
3975 | { | |
3976 | size_t size = 0; | |
3977 | ||
e8872a25 | 3978 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) |
10c9ead9 RP |
3979 | size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); |
3980 | ||
97a47fac NA |
3981 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { |
3982 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; | |
80e73cc5 | 3983 | int attr = IFLA_STATS_LINK_XSTATS; |
97a47fac NA |
3984 | |
3985 | if (ops && ops->get_linkxstats_size) { | |
80e73cc5 NA |
3986 | size += nla_total_size(ops->get_linkxstats_size(dev, |
3987 | attr)); | |
97a47fac NA |
3988 | /* for IFLA_STATS_LINK_XSTATS */ |
3989 | size += nla_total_size(0); | |
3990 | } | |
3991 | } | |
3992 | ||
80e73cc5 NA |
3993 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { |
3994 | struct net_device *_dev = (struct net_device *)dev; | |
3995 | const struct rtnl_link_ops *ops = NULL; | |
3996 | const struct net_device *master; | |
3997 | ||
3998 | /* netdev_master_upper_dev_get can't take const */ | |
3999 | master = netdev_master_upper_dev_get(_dev); | |
4000 | if (master) | |
4001 | ops = master->rtnl_link_ops; | |
4002 | if (ops && ops->get_linkxstats_size) { | |
4003 | int attr = IFLA_STATS_LINK_XSTATS_SLAVE; | |
4004 | ||
4005 | size += nla_total_size(ops->get_linkxstats_size(dev, | |
4006 | attr)); | |
4007 | /* for IFLA_STATS_LINK_XSTATS_SLAVE */ | |
4008 | size += nla_total_size(0); | |
4009 | } | |
4010 | } | |
4011 | ||
69ae6ad2 NF |
4012 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) |
4013 | size += rtnl_get_offload_stats_size(dev); | |
4014 | ||
aefb4d4a RS |
4015 | if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { |
4016 | struct rtnl_af_ops *af_ops; | |
4017 | ||
4018 | /* for IFLA_STATS_AF_SPEC */ | |
4019 | size += nla_total_size(0); | |
4020 | ||
4021 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { | |
4022 | if (af_ops->get_stats_af_size) { | |
4023 | size += nla_total_size( | |
4024 | af_ops->get_stats_af_size(dev)); | |
4025 | ||
4026 | /* for AF_* */ | |
4027 | size += nla_total_size(0); | |
4028 | } | |
4029 | } | |
4030 | } | |
4031 | ||
10c9ead9 RP |
4032 | return size; |
4033 | } | |
4034 | ||
c21ef3e3 DA |
4035 | static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, |
4036 | struct netlink_ext_ack *extack) | |
10c9ead9 RP |
4037 | { |
4038 | struct net *net = sock_net(skb->sk); | |
10c9ead9 | 4039 | struct net_device *dev = NULL; |
e8872a25 NA |
4040 | int idxattr = 0, prividx = 0; |
4041 | struct if_stats_msg *ifsm; | |
10c9ead9 RP |
4042 | struct sk_buff *nskb; |
4043 | u32 filter_mask; | |
4044 | int err; | |
4045 | ||
4775cc1f MK |
4046 | if (nlmsg_len(nlh) < sizeof(*ifsm)) |
4047 | return -EINVAL; | |
4048 | ||
10c9ead9 RP |
4049 | ifsm = nlmsg_data(nlh); |
4050 | if (ifsm->ifindex > 0) | |
4051 | dev = __dev_get_by_index(net, ifsm->ifindex); | |
4052 | else | |
4053 | return -EINVAL; | |
4054 | ||
4055 | if (!dev) | |
4056 | return -ENODEV; | |
4057 | ||
4058 | filter_mask = ifsm->filter_mask; | |
4059 | if (!filter_mask) | |
4060 | return -EINVAL; | |
4061 | ||
4062 | nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); | |
4063 | if (!nskb) | |
4064 | return -ENOBUFS; | |
4065 | ||
4066 | err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, | |
4067 | NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, | |
e8872a25 | 4068 | 0, filter_mask, &idxattr, &prividx); |
10c9ead9 RP |
4069 | if (err < 0) { |
4070 | /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ | |
4071 | WARN_ON(err == -EMSGSIZE); | |
4072 | kfree_skb(nskb); | |
4073 | } else { | |
4074 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); | |
4075 | } | |
4076 | ||
4077 | return err; | |
4078 | } | |
4079 | ||
4080 | static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
4081 | { | |
e8872a25 | 4082 | int h, s_h, err, s_idx, s_idxattr, s_prividx; |
10c9ead9 | 4083 | struct net *net = sock_net(skb->sk); |
e8872a25 | 4084 | unsigned int flags = NLM_F_MULTI; |
10c9ead9 | 4085 | struct if_stats_msg *ifsm; |
10c9ead9 | 4086 | struct hlist_head *head; |
e8872a25 | 4087 | struct net_device *dev; |
10c9ead9 | 4088 | u32 filter_mask = 0; |
e8872a25 | 4089 | int idx = 0; |
10c9ead9 RP |
4090 | |
4091 | s_h = cb->args[0]; | |
4092 | s_idx = cb->args[1]; | |
e8872a25 NA |
4093 | s_idxattr = cb->args[2]; |
4094 | s_prividx = cb->args[3]; | |
10c9ead9 RP |
4095 | |
4096 | cb->seq = net->dev_base_seq; | |
4097 | ||
4775cc1f MK |
4098 | if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) |
4099 | return -EINVAL; | |
4100 | ||
10c9ead9 RP |
4101 | ifsm = nlmsg_data(cb->nlh); |
4102 | filter_mask = ifsm->filter_mask; | |
4103 | if (!filter_mask) | |
4104 | return -EINVAL; | |
4105 | ||
4106 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { | |
4107 | idx = 0; | |
4108 | head = &net->dev_index_head[h]; | |
4109 | hlist_for_each_entry(dev, head, index_hlist) { | |
4110 | if (idx < s_idx) | |
4111 | goto cont; | |
4112 | err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, | |
4113 | NETLINK_CB(cb->skb).portid, | |
4114 | cb->nlh->nlmsg_seq, 0, | |
e8872a25 NA |
4115 | flags, filter_mask, |
4116 | &s_idxattr, &s_prividx); | |
10c9ead9 RP |
4117 | /* If we ran out of room on the first message, |
4118 | * we're in trouble | |
4119 | */ | |
4120 | WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); | |
4121 | ||
4122 | if (err < 0) | |
4123 | goto out; | |
e8872a25 NA |
4124 | s_prividx = 0; |
4125 | s_idxattr = 0; | |
10c9ead9 RP |
4126 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
4127 | cont: | |
4128 | idx++; | |
4129 | } | |
4130 | } | |
4131 | out: | |
e8872a25 NA |
4132 | cb->args[3] = s_prividx; |
4133 | cb->args[2] = s_idxattr; | |
10c9ead9 RP |
4134 | cb->args[1] = idx; |
4135 | cb->args[0] = h; | |
4136 | ||
4137 | return skb->len; | |
4138 | } | |
4139 | ||
1da177e4 LT |
4140 | /* Process one rtnetlink message. */ |
4141 | ||
2d4bc933 JB |
4142 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, |
4143 | struct netlink_ext_ack *extack) | |
1da177e4 | 4144 | { |
3b1e0a65 | 4145 | struct net *net = sock_net(skb->sk); |
6853dd48 FW |
4146 | struct rtnl_link *handlers; |
4147 | int err = -EOPNOTSUPP; | |
e2849863 | 4148 | rtnl_doit_func doit; |
62256f98 | 4149 | unsigned int flags; |
617cfc75 | 4150 | int kind; |
1da177e4 LT |
4151 | int family; |
4152 | int type; | |
1da177e4 | 4153 | |
1da177e4 | 4154 | type = nlh->nlmsg_type; |
1da177e4 | 4155 | if (type > RTM_MAX) |
038890fe | 4156 | return -EOPNOTSUPP; |
1da177e4 LT |
4157 | |
4158 | type -= RTM_BASE; | |
4159 | ||
4160 | /* All the messages must have at least 1 byte length */ | |
573ce260 | 4161 | if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) |
1da177e4 LT |
4162 | return 0; |
4163 | ||
573ce260 | 4164 | family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; |
1da177e4 LT |
4165 | kind = type&3; |
4166 | ||
90f62cf3 | 4167 | if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) |
1d00a4eb | 4168 | return -EPERM; |
1da177e4 | 4169 | |
6853dd48 FW |
4170 | if (family > ARRAY_SIZE(rtnl_msg_handlers)) |
4171 | family = PF_UNSPEC; | |
4172 | ||
4173 | rcu_read_lock(); | |
4174 | handlers = rcu_dereference(rtnl_msg_handlers[family]); | |
4175 | if (!handlers) { | |
4176 | family = PF_UNSPEC; | |
4177 | handlers = rcu_dereference(rtnl_msg_handlers[family]); | |
4178 | } | |
4179 | ||
b8f3ab42 | 4180 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
97c53cac | 4181 | struct sock *rtnl; |
e2849863 | 4182 | rtnl_dumpit_func dumpit; |
c7ac8679 | 4183 | u16 min_dump_alloc = 0; |
1da177e4 | 4184 | |
6853dd48 FW |
4185 | dumpit = READ_ONCE(handlers[type].dumpit); |
4186 | if (!dumpit) { | |
4187 | family = PF_UNSPEC; | |
4188 | handlers = rcu_dereference(rtnl_msg_handlers[PF_UNSPEC]); | |
4189 | if (!handlers) | |
4190 | goto err_unlock; | |
0cc09020 | 4191 | |
6853dd48 FW |
4192 | dumpit = READ_ONCE(handlers[type].dumpit); |
4193 | if (!dumpit) | |
4194 | goto err_unlock; | |
4195 | } | |
e1fa6d21 | 4196 | |
019a3169 FW |
4197 | refcount_inc(&rtnl_msg_handlers_ref[family]); |
4198 | ||
e1fa6d21 FW |
4199 | if (type == RTM_GETLINK) |
4200 | min_dump_alloc = rtnl_calcit(skb, nlh); | |
9ac4a169 | 4201 | |
6853dd48 FW |
4202 | rcu_read_unlock(); |
4203 | ||
97c53cac | 4204 | rtnl = net->rtnl; |
80d326fa PNA |
4205 | { |
4206 | struct netlink_dump_control c = { | |
4207 | .dump = dumpit, | |
4208 | .min_dump_alloc = min_dump_alloc, | |
4209 | }; | |
4210 | err = netlink_dump_start(rtnl, skb, nlh, &c); | |
4211 | } | |
019a3169 | 4212 | refcount_dec(&rtnl_msg_handlers_ref[family]); |
2907c35f | 4213 | return err; |
1da177e4 LT |
4214 | } |
4215 | ||
62256f98 FW |
4216 | flags = READ_ONCE(handlers[type].flags); |
4217 | if (flags & RTNL_FLAG_DOIT_UNLOCKED) { | |
4218 | refcount_inc(&rtnl_msg_handlers_ref[family]); | |
4219 | doit = READ_ONCE(handlers[type].doit); | |
4220 | rcu_read_unlock(); | |
4221 | if (doit) | |
4222 | err = doit(skb, nlh, extack); | |
4223 | refcount_dec(&rtnl_msg_handlers_ref[family]); | |
4224 | return err; | |
4225 | } | |
4226 | ||
6853dd48 | 4227 | rcu_read_unlock(); |
1da177e4 | 4228 | |
6853dd48 FW |
4229 | rtnl_lock(); |
4230 | handlers = rtnl_dereference(rtnl_msg_handlers[family]); | |
4231 | if (handlers) { | |
4232 | doit = READ_ONCE(handlers[type].doit); | |
4233 | if (doit) | |
4234 | err = doit(skb, nlh, extack); | |
4235 | } | |
0cc09020 | 4236 | rtnl_unlock(); |
0cc09020 FW |
4237 | return err; |
4238 | ||
4239 | err_unlock: | |
6853dd48 | 4240 | rcu_read_unlock(); |
0cc09020 | 4241 | return -EOPNOTSUPP; |
1da177e4 LT |
4242 | } |
4243 | ||
cd40b7d3 | 4244 | static void rtnetlink_rcv(struct sk_buff *skb) |
1da177e4 | 4245 | { |
cd40b7d3 | 4246 | netlink_rcv_skb(skb, &rtnetlink_rcv_msg); |
1da177e4 LT |
4247 | } |
4248 | ||
5f729eaa JG |
4249 | static int rtnetlink_bind(struct net *net, int group) |
4250 | { | |
4251 | switch (group) { | |
4252 | case RTNLGRP_IPV4_MROUTE_R: | |
4253 | case RTNLGRP_IPV6_MROUTE_R: | |
4254 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) | |
4255 | return -EPERM; | |
4256 | break; | |
4257 | } | |
4258 | return 0; | |
4259 | } | |
4260 | ||
1da177e4 LT |
4261 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
4262 | { | |
351638e7 | 4263 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
e9dc8653 | 4264 | |
1da177e4 | 4265 | switch (event) { |
5138e86f | 4266 | case NETDEV_REBOOT: |
3753654e | 4267 | case NETDEV_CHANGEADDR: |
5138e86f VY |
4268 | case NETDEV_CHANGENAME: |
4269 | case NETDEV_FEAT_CHANGE: | |
4270 | case NETDEV_BONDING_FAILOVER: | |
5138e86f | 4271 | case NETDEV_NOTIFY_PEERS: |
5138e86f | 4272 | case NETDEV_RESEND_IGMP: |
5138e86f | 4273 | case NETDEV_CHANGEINFODATA: |
3d3ea5af VY |
4274 | rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), |
4275 | GFP_KERNEL); | |
1da177e4 LT |
4276 | break; |
4277 | default: | |
1da177e4 LT |
4278 | break; |
4279 | } | |
4280 | return NOTIFY_DONE; | |
4281 | } | |
4282 | ||
4283 | static struct notifier_block rtnetlink_dev_notifier = { | |
4284 | .notifier_call = rtnetlink_event, | |
4285 | }; | |
4286 | ||
97c53cac | 4287 | |
2c8c1e72 | 4288 | static int __net_init rtnetlink_net_init(struct net *net) |
97c53cac DL |
4289 | { |
4290 | struct sock *sk; | |
a31f2d17 PNA |
4291 | struct netlink_kernel_cfg cfg = { |
4292 | .groups = RTNLGRP_MAX, | |
4293 | .input = rtnetlink_rcv, | |
4294 | .cb_mutex = &rtnl_mutex, | |
9785e10a | 4295 | .flags = NL_CFG_F_NONROOT_RECV, |
5f729eaa | 4296 | .bind = rtnetlink_bind, |
a31f2d17 PNA |
4297 | }; |
4298 | ||
9f00d977 | 4299 | sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); |
97c53cac DL |
4300 | if (!sk) |
4301 | return -ENOMEM; | |
97c53cac DL |
4302 | net->rtnl = sk; |
4303 | return 0; | |
4304 | } | |
4305 | ||
2c8c1e72 | 4306 | static void __net_exit rtnetlink_net_exit(struct net *net) |
97c53cac | 4307 | { |
775516bf DL |
4308 | netlink_kernel_release(net->rtnl); |
4309 | net->rtnl = NULL; | |
97c53cac DL |
4310 | } |
4311 | ||
4312 | static struct pernet_operations rtnetlink_net_ops = { | |
4313 | .init = rtnetlink_net_init, | |
4314 | .exit = rtnetlink_net_exit, | |
4315 | }; | |
4316 | ||
1da177e4 LT |
4317 | void __init rtnetlink_init(void) |
4318 | { | |
97c53cac | 4319 | if (register_pernet_subsys(&rtnetlink_net_ops)) |
1da177e4 | 4320 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
97c53cac | 4321 | |
1da177e4 | 4322 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
340d17fc | 4323 | |
c7ac8679 | 4324 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, |
b97bac64 FW |
4325 | rtnl_dump_ifinfo, 0); |
4326 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); | |
4327 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); | |
4328 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); | |
687ad8cc | 4329 | |
b97bac64 FW |
4330 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); |
4331 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); | |
4332 | rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); | |
77162022 | 4333 | |
b97bac64 FW |
4334 | rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); |
4335 | rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); | |
4336 | rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0); | |
e5a55a89 | 4337 | |
b97bac64 FW |
4338 | rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); |
4339 | rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); | |
4340 | rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); | |
10c9ead9 RP |
4341 | |
4342 | rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, | |
b97bac64 | 4343 | 0); |
1da177e4 | 4344 | } |