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