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; | |
10708f37 JE |
807 | } |
808 | ||
be1f3c2c | 809 | static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b) |
10708f37 | 810 | { |
afdcba37 | 811 | memcpy(v, b, sizeof(*b)); |
10708f37 | 812 | } |
1da177e4 | 813 | |
c02db8c6 | 814 | /* All VF info */ |
115c9b81 GR |
815 | static inline int rtnl_vfinfo_size(const struct net_device *dev, |
816 | u32 ext_filter_mask) | |
ebc08a6f | 817 | { |
115c9b81 GR |
818 | if (dev->dev.parent && dev_is_pci(dev->dev.parent) && |
819 | (ext_filter_mask & RTEXT_FILTER_VF)) { | |
c02db8c6 | 820 | int num_vfs = dev_num_vf(dev->dev.parent); |
045de01a SF |
821 | size_t size = nla_total_size(sizeof(struct nlattr)); |
822 | size += nla_total_size(num_vfs * sizeof(struct nlattr)); | |
823 | size += num_vfs * | |
824 | (nla_total_size(sizeof(struct ifla_vf_mac)) + | |
825 | nla_total_size(sizeof(struct ifla_vf_vlan)) + | |
ed616689 | 826 | nla_total_size(sizeof(struct ifla_vf_spoofchk)) + |
945a3676 | 827 | nla_total_size(sizeof(struct ifla_vf_rate)) + |
01a3d796 | 828 | nla_total_size(sizeof(struct ifla_vf_link_state)) + |
3b766cd8 EBE |
829 | nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + |
830 | /* IFLA_VF_STATS_RX_PACKETS */ | |
831 | nla_total_size(sizeof(__u64)) + | |
832 | /* IFLA_VF_STATS_TX_PACKETS */ | |
833 | nla_total_size(sizeof(__u64)) + | |
834 | /* IFLA_VF_STATS_RX_BYTES */ | |
835 | nla_total_size(sizeof(__u64)) + | |
836 | /* IFLA_VF_STATS_TX_BYTES */ | |
837 | nla_total_size(sizeof(__u64)) + | |
838 | /* IFLA_VF_STATS_BROADCAST */ | |
839 | nla_total_size(sizeof(__u64)) + | |
840 | /* IFLA_VF_STATS_MULTICAST */ | |
dd461d6a HS |
841 | nla_total_size(sizeof(__u64)) + |
842 | nla_total_size(sizeof(struct ifla_vf_trust))); | |
c02db8c6 CW |
843 | return size; |
844 | } else | |
ebc08a6f WM |
845 | return 0; |
846 | } | |
847 | ||
c53864fd DG |
848 | static size_t rtnl_port_size(const struct net_device *dev, |
849 | u32 ext_filter_mask) | |
57b61080 SF |
850 | { |
851 | size_t port_size = nla_total_size(4) /* PORT_VF */ | |
852 | + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ | |
853 | + nla_total_size(sizeof(struct ifla_port_vsi)) | |
854 | /* PORT_VSI_TYPE */ | |
855 | + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ | |
856 | + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ | |
857 | + nla_total_size(1) /* PROT_VDP_REQUEST */ | |
858 | + nla_total_size(2); /* PORT_VDP_RESPONSE */ | |
859 | size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); | |
860 | size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) | |
861 | + port_size; | |
862 | size_t port_self_size = nla_total_size(sizeof(struct nlattr)) | |
863 | + port_size; | |
864 | ||
c53864fd DG |
865 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
866 | !(ext_filter_mask & RTEXT_FILTER_VF)) | |
57b61080 SF |
867 | return 0; |
868 | if (dev_num_vf(dev->dev.parent)) | |
869 | return port_self_size + vf_ports_size + | |
870 | vf_port_size * dev_num_vf(dev->dev.parent); | |
871 | else | |
872 | return port_self_size; | |
873 | } | |
874 | ||
115c9b81 GR |
875 | static noinline size_t if_nlmsg_size(const struct net_device *dev, |
876 | u32 ext_filter_mask) | |
339bf98f TG |
877 | { |
878 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
879 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ | |
0b815a1a | 880 | + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ |
339bf98f TG |
881 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ |
882 | + nla_total_size(sizeof(struct rtnl_link_ifmap)) | |
883 | + nla_total_size(sizeof(struct rtnl_link_stats)) | |
adcfe196 | 884 | + nla_total_size(sizeof(struct rtnl_link_stats64)) |
339bf98f TG |
885 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
886 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ | |
887 | + nla_total_size(4) /* IFLA_TXQLEN */ | |
888 | + nla_total_size(4) /* IFLA_WEIGHT */ | |
889 | + nla_total_size(4) /* IFLA_MTU */ | |
890 | + nla_total_size(4) /* IFLA_LINK */ | |
891 | + nla_total_size(4) /* IFLA_MASTER */ | |
9a57247f | 892 | + nla_total_size(1) /* IFLA_CARRIER */ |
edbc0bb3 | 893 | + nla_total_size(4) /* IFLA_PROMISCUITY */ |
76ff5cc9 JP |
894 | + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ |
895 | + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ | |
339bf98f | 896 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
38f7b870 | 897 | + nla_total_size(1) /* IFLA_LINKMODE */ |
2d3b479d | 898 | + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ |
d37512a2 | 899 | + nla_total_size(4) /* IFLA_LINK_NETNSID */ |
115c9b81 GR |
900 | + nla_total_size(ext_filter_mask |
901 | & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ | |
902 | + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ | |
c53864fd | 903 | + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ |
f8ff182c | 904 | + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ |
b1974ed0 | 905 | + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ |
82f28412 | 906 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ |
88d6378b AK |
907 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ |
908 | + nla_total_size(1); /* IFLA_PROTO_DOWN */ | |
909 | ||
339bf98f TG |
910 | } |
911 | ||
57b61080 SF |
912 | static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) |
913 | { | |
914 | struct nlattr *vf_ports; | |
915 | struct nlattr *vf_port; | |
916 | int vf; | |
917 | int err; | |
918 | ||
919 | vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); | |
920 | if (!vf_ports) | |
921 | return -EMSGSIZE; | |
922 | ||
923 | for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { | |
924 | vf_port = nla_nest_start(skb, IFLA_VF_PORT); | |
8ca94183 SF |
925 | if (!vf_port) |
926 | goto nla_put_failure; | |
a6574349 DM |
927 | if (nla_put_u32(skb, IFLA_PORT_VF, vf)) |
928 | goto nla_put_failure; | |
57b61080 | 929 | err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); |
8ca94183 SF |
930 | if (err == -EMSGSIZE) |
931 | goto nla_put_failure; | |
57b61080 | 932 | if (err) { |
57b61080 SF |
933 | nla_nest_cancel(skb, vf_port); |
934 | continue; | |
935 | } | |
936 | nla_nest_end(skb, vf_port); | |
937 | } | |
938 | ||
939 | nla_nest_end(skb, vf_ports); | |
940 | ||
941 | return 0; | |
8ca94183 SF |
942 | |
943 | nla_put_failure: | |
944 | nla_nest_cancel(skb, vf_ports); | |
945 | return -EMSGSIZE; | |
57b61080 SF |
946 | } |
947 | ||
948 | static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) | |
949 | { | |
950 | struct nlattr *port_self; | |
951 | int err; | |
952 | ||
953 | port_self = nla_nest_start(skb, IFLA_PORT_SELF); | |
954 | if (!port_self) | |
955 | return -EMSGSIZE; | |
956 | ||
957 | err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); | |
958 | if (err) { | |
959 | nla_nest_cancel(skb, port_self); | |
8ca94183 | 960 | return (err == -EMSGSIZE) ? err : 0; |
57b61080 SF |
961 | } |
962 | ||
963 | nla_nest_end(skb, port_self); | |
964 | ||
965 | return 0; | |
966 | } | |
967 | ||
c53864fd DG |
968 | static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, |
969 | u32 ext_filter_mask) | |
57b61080 SF |
970 | { |
971 | int err; | |
972 | ||
c53864fd DG |
973 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
974 | !(ext_filter_mask & RTEXT_FILTER_VF)) | |
57b61080 SF |
975 | return 0; |
976 | ||
977 | err = rtnl_port_self_fill(skb, dev); | |
978 | if (err) | |
979 | return err; | |
980 | ||
981 | if (dev_num_vf(dev->dev.parent)) { | |
982 | err = rtnl_vf_ports_fill(skb, dev); | |
983 | if (err) | |
984 | return err; | |
985 | } | |
986 | ||
987 | return 0; | |
988 | } | |
989 | ||
66cae9ed JP |
990 | static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) |
991 | { | |
992 | int err; | |
02637fce | 993 | struct netdev_phys_item_id ppid; |
66cae9ed JP |
994 | |
995 | err = dev_get_phys_port_id(dev, &ppid); | |
996 | if (err) { | |
997 | if (err == -EOPNOTSUPP) | |
998 | return 0; | |
999 | return err; | |
1000 | } | |
1001 | ||
1002 | if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) | |
1003 | return -EMSGSIZE; | |
1004 | ||
1005 | return 0; | |
1006 | } | |
1007 | ||
db24a904 DA |
1008 | static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) |
1009 | { | |
1010 | char name[IFNAMSIZ]; | |
1011 | int err; | |
1012 | ||
1013 | err = dev_get_phys_port_name(dev, name, sizeof(name)); | |
1014 | if (err) { | |
1015 | if (err == -EOPNOTSUPP) | |
1016 | return 0; | |
1017 | return err; | |
1018 | } | |
1019 | ||
1020 | if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name)) | |
1021 | return -EMSGSIZE; | |
1022 | ||
1023 | return 0; | |
1024 | } | |
1025 | ||
82f28412 JP |
1026 | static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) |
1027 | { | |
1028 | int err; | |
f8e20a9f | 1029 | struct switchdev_attr attr = { |
1f868398 | 1030 | .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, |
f8e20a9f SF |
1031 | .flags = SWITCHDEV_F_NO_RECURSE, |
1032 | }; | |
82f28412 | 1033 | |
f8e20a9f | 1034 | err = switchdev_port_attr_get(dev, &attr); |
82f28412 JP |
1035 | if (err) { |
1036 | if (err == -EOPNOTSUPP) | |
1037 | return 0; | |
1038 | return err; | |
1039 | } | |
1040 | ||
42275bd8 SF |
1041 | if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, |
1042 | attr.u.ppid.id)) | |
82f28412 JP |
1043 | return -EMSGSIZE; |
1044 | ||
1045 | return 0; | |
1046 | } | |
1047 | ||
b22b941b HFS |
1048 | static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, |
1049 | struct net_device *dev) | |
1050 | { | |
1051 | const struct rtnl_link_stats64 *stats; | |
1052 | struct rtnl_link_stats64 temp; | |
1053 | struct nlattr *attr; | |
1054 | ||
1055 | stats = dev_get_stats(dev, &temp); | |
1056 | ||
1057 | attr = nla_reserve(skb, IFLA_STATS, | |
1058 | sizeof(struct rtnl_link_stats)); | |
1059 | if (!attr) | |
1060 | return -EMSGSIZE; | |
1061 | ||
1062 | copy_rtnl_link_stats(nla_data(attr), stats); | |
1063 | ||
1064 | attr = nla_reserve(skb, IFLA_STATS64, | |
1065 | sizeof(struct rtnl_link_stats64)); | |
1066 | if (!attr) | |
1067 | return -EMSGSIZE; | |
1068 | ||
1069 | copy_rtnl_link_stats64(nla_data(attr), stats); | |
1070 | ||
1071 | return 0; | |
1072 | } | |
1073 | ||
1074 | static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, | |
1075 | struct net_device *dev, | |
1076 | int vfs_num, | |
1077 | struct nlattr *vfinfo) | |
1078 | { | |
1079 | struct ifla_vf_rss_query_en vf_rss_query_en; | |
1080 | struct ifla_vf_link_state vf_linkstate; | |
1081 | struct ifla_vf_spoofchk vf_spoofchk; | |
1082 | struct ifla_vf_tx_rate vf_tx_rate; | |
1083 | struct ifla_vf_stats vf_stats; | |
1084 | struct ifla_vf_trust vf_trust; | |
1085 | struct ifla_vf_vlan vf_vlan; | |
1086 | struct ifla_vf_rate vf_rate; | |
1087 | struct nlattr *vf, *vfstats; | |
1088 | struct ifla_vf_mac vf_mac; | |
1089 | struct ifla_vf_info ivi; | |
1090 | ||
1091 | /* Not all SR-IOV capable drivers support the | |
1092 | * spoofcheck and "RSS query enable" query. Preset to | |
1093 | * -1 so the user space tool can detect that the driver | |
1094 | * didn't report anything. | |
1095 | */ | |
1096 | ivi.spoofchk = -1; | |
1097 | ivi.rss_query_en = -1; | |
1098 | ivi.trusted = -1; | |
1099 | memset(ivi.mac, 0, sizeof(ivi.mac)); | |
1100 | /* The default value for VF link state is "auto" | |
1101 | * IFLA_VF_LINK_STATE_AUTO which equals zero | |
1102 | */ | |
1103 | ivi.linkstate = 0; | |
1104 | if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) | |
1105 | return 0; | |
1106 | ||
1107 | vf_mac.vf = | |
1108 | vf_vlan.vf = | |
1109 | vf_rate.vf = | |
1110 | vf_tx_rate.vf = | |
1111 | vf_spoofchk.vf = | |
1112 | vf_linkstate.vf = | |
1113 | vf_rss_query_en.vf = | |
1114 | vf_trust.vf = ivi.vf; | |
1115 | ||
1116 | memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); | |
1117 | vf_vlan.vlan = ivi.vlan; | |
1118 | vf_vlan.qos = ivi.qos; | |
1119 | vf_tx_rate.rate = ivi.max_tx_rate; | |
1120 | vf_rate.min_tx_rate = ivi.min_tx_rate; | |
1121 | vf_rate.max_tx_rate = ivi.max_tx_rate; | |
1122 | vf_spoofchk.setting = ivi.spoofchk; | |
1123 | vf_linkstate.link_state = ivi.linkstate; | |
1124 | vf_rss_query_en.setting = ivi.rss_query_en; | |
1125 | vf_trust.setting = ivi.trusted; | |
1126 | vf = nla_nest_start(skb, IFLA_VF_INFO); | |
1127 | if (!vf) { | |
1128 | nla_nest_cancel(skb, vfinfo); | |
1129 | return -EMSGSIZE; | |
1130 | } | |
1131 | if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || | |
1132 | nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || | |
1133 | nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), | |
1134 | &vf_rate) || | |
1135 | nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), | |
1136 | &vf_tx_rate) || | |
1137 | nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), | |
1138 | &vf_spoofchk) || | |
1139 | nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), | |
1140 | &vf_linkstate) || | |
1141 | nla_put(skb, IFLA_VF_RSS_QUERY_EN, | |
1142 | sizeof(vf_rss_query_en), | |
1143 | &vf_rss_query_en) || | |
1144 | nla_put(skb, IFLA_VF_TRUST, | |
1145 | sizeof(vf_trust), &vf_trust)) | |
1146 | return -EMSGSIZE; | |
1147 | memset(&vf_stats, 0, sizeof(vf_stats)); | |
1148 | if (dev->netdev_ops->ndo_get_vf_stats) | |
1149 | dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, | |
1150 | &vf_stats); | |
1151 | vfstats = nla_nest_start(skb, IFLA_VF_STATS); | |
1152 | if (!vfstats) { | |
1153 | nla_nest_cancel(skb, vf); | |
1154 | nla_nest_cancel(skb, vfinfo); | |
1155 | return -EMSGSIZE; | |
1156 | } | |
1157 | if (nla_put_u64(skb, IFLA_VF_STATS_RX_PACKETS, | |
1158 | vf_stats.rx_packets) || | |
1159 | nla_put_u64(skb, IFLA_VF_STATS_TX_PACKETS, | |
1160 | vf_stats.tx_packets) || | |
1161 | nla_put_u64(skb, IFLA_VF_STATS_RX_BYTES, | |
1162 | vf_stats.rx_bytes) || | |
1163 | nla_put_u64(skb, IFLA_VF_STATS_TX_BYTES, | |
1164 | vf_stats.tx_bytes) || | |
1165 | nla_put_u64(skb, IFLA_VF_STATS_BROADCAST, | |
1166 | vf_stats.broadcast) || | |
1167 | nla_put_u64(skb, IFLA_VF_STATS_MULTICAST, | |
1168 | vf_stats.multicast)) | |
1169 | return -EMSGSIZE; | |
1170 | nla_nest_end(skb, vfstats); | |
1171 | nla_nest_end(skb, vf); | |
1172 | return 0; | |
1173 | } | |
1174 | ||
1175 | static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) | |
1176 | { | |
1177 | struct rtnl_link_ifmap map = { | |
1178 | .mem_start = dev->mem_start, | |
1179 | .mem_end = dev->mem_end, | |
1180 | .base_addr = dev->base_addr, | |
1181 | .irq = dev->irq, | |
1182 | .dma = dev->dma, | |
1183 | .port = dev->if_port, | |
1184 | }; | |
1185 | if (nla_put(skb, IFLA_MAP, sizeof(map), &map)) | |
1186 | return -EMSGSIZE; | |
1187 | ||
1188 | return 0; | |
1189 | } | |
1190 | ||
b60c5115 | 1191 | static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, |
575c3e2a | 1192 | int type, u32 pid, u32 seq, u32 change, |
115c9b81 | 1193 | unsigned int flags, u32 ext_filter_mask) |
b60c5115 TG |
1194 | { |
1195 | struct ifinfomsg *ifm; | |
1196 | struct nlmsghdr *nlh; | |
b22b941b | 1197 | struct nlattr *af_spec; |
f8ff182c | 1198 | struct rtnl_af_ops *af_ops; |
898e5061 | 1199 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
1da177e4 | 1200 | |
2907c35f | 1201 | ASSERT_RTNL(); |
b60c5115 TG |
1202 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
1203 | if (nlh == NULL) | |
26932566 | 1204 | return -EMSGSIZE; |
1da177e4 | 1205 | |
b60c5115 TG |
1206 | ifm = nlmsg_data(nlh); |
1207 | ifm->ifi_family = AF_UNSPEC; | |
1208 | ifm->__ifi_pad = 0; | |
1209 | ifm->ifi_type = dev->type; | |
1210 | ifm->ifi_index = dev->ifindex; | |
1211 | ifm->ifi_flags = dev_get_flags(dev); | |
1212 | ifm->ifi_change = change; | |
1213 | ||
a6574349 DM |
1214 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
1215 | nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || | |
1216 | nla_put_u8(skb, IFLA_OPERSTATE, | |
1217 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || | |
1218 | nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || | |
1219 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || | |
1220 | nla_put_u32(skb, IFLA_GROUP, dev->group) || | |
edbc0bb3 | 1221 | nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || |
76ff5cc9 | 1222 | nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || |
1d69c2b3 | 1223 | #ifdef CONFIG_RPS |
76ff5cc9 | 1224 | nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || |
1d69c2b3 | 1225 | #endif |
a54acb3a ND |
1226 | (dev->ifindex != dev_get_iflink(dev) && |
1227 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || | |
898e5061 JP |
1228 | (upper_dev && |
1229 | nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) || | |
9a57247f | 1230 | nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || |
a6574349 DM |
1231 | (dev->qdisc && |
1232 | nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || | |
1233 | (dev->ifalias && | |
2d3b479d | 1234 | nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) || |
1235 | nla_put_u32(skb, IFLA_CARRIER_CHANGES, | |
88d6378b AK |
1236 | atomic_read(&dev->carrier_changes)) || |
1237 | nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) | |
a6574349 | 1238 | goto nla_put_failure; |
0b815a1a | 1239 | |
b22b941b HFS |
1240 | if (rtnl_fill_link_ifmap(skb, dev)) |
1241 | goto nla_put_failure; | |
1da177e4 LT |
1242 | |
1243 | if (dev->addr_len) { | |
a6574349 DM |
1244 | if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || |
1245 | nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) | |
1246 | goto nla_put_failure; | |
1da177e4 LT |
1247 | } |
1248 | ||
66cae9ed JP |
1249 | if (rtnl_phys_port_id_fill(skb, dev)) |
1250 | goto nla_put_failure; | |
1251 | ||
db24a904 DA |
1252 | if (rtnl_phys_port_name_fill(skb, dev)) |
1253 | goto nla_put_failure; | |
1254 | ||
82f28412 JP |
1255 | if (rtnl_phys_switch_id_fill(skb, dev)) |
1256 | goto nla_put_failure; | |
1257 | ||
b22b941b | 1258 | if (rtnl_fill_stats(skb, dev)) |
10708f37 | 1259 | goto nla_put_failure; |
10708f37 | 1260 | |
a6574349 DM |
1261 | if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) && |
1262 | nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent))) | |
1263 | goto nla_put_failure; | |
57b61080 | 1264 | |
b22b941b HFS |
1265 | if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent && |
1266 | ext_filter_mask & RTEXT_FILTER_VF) { | |
ebc08a6f | 1267 | int i; |
b22b941b | 1268 | struct nlattr *vfinfo; |
c02db8c6 CW |
1269 | int num_vfs = dev_num_vf(dev->dev.parent); |
1270 | ||
c02db8c6 CW |
1271 | vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); |
1272 | if (!vfinfo) | |
1273 | goto nla_put_failure; | |
1274 | for (i = 0; i < num_vfs; i++) { | |
b22b941b | 1275 | if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) |
3b766cd8 | 1276 | goto nla_put_failure; |
ebc08a6f | 1277 | } |
b22b941b | 1278 | |
c02db8c6 | 1279 | nla_nest_end(skb, vfinfo); |
ebc08a6f | 1280 | } |
57b61080 | 1281 | |
c53864fd | 1282 | if (rtnl_port_fill(skb, dev, ext_filter_mask)) |
57b61080 SF |
1283 | goto nla_put_failure; |
1284 | ||
ba7d49b1 | 1285 | if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { |
38f7b870 PM |
1286 | if (rtnl_link_fill(skb, dev) < 0) |
1287 | goto nla_put_failure; | |
1288 | } | |
1289 | ||
d37512a2 ND |
1290 | if (dev->rtnl_link_ops && |
1291 | dev->rtnl_link_ops->get_link_net) { | |
1292 | struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); | |
1293 | ||
1294 | if (!net_eq(dev_net(dev), link_net)) { | |
7a0877d4 | 1295 | int id = peernet2id_alloc(dev_net(dev), link_net); |
d37512a2 ND |
1296 | |
1297 | if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) | |
1298 | goto nla_put_failure; | |
1299 | } | |
1300 | } | |
1301 | ||
f8ff182c TG |
1302 | if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) |
1303 | goto nla_put_failure; | |
1304 | ||
1305 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { | |
1306 | if (af_ops->fill_link_af) { | |
1307 | struct nlattr *af; | |
1308 | int err; | |
1309 | ||
1310 | if (!(af = nla_nest_start(skb, af_ops->family))) | |
1311 | goto nla_put_failure; | |
1312 | ||
d5566fd7 | 1313 | err = af_ops->fill_link_af(skb, dev, ext_filter_mask); |
f8ff182c TG |
1314 | |
1315 | /* | |
1316 | * Caller may return ENODATA to indicate that there | |
1317 | * was no data to be dumped. This is not an error, it | |
1318 | * means we should trim the attribute header and | |
1319 | * continue. | |
1320 | */ | |
1321 | if (err == -ENODATA) | |
1322 | nla_nest_cancel(skb, af); | |
1323 | else if (err < 0) | |
1324 | goto nla_put_failure; | |
1325 | ||
1326 | nla_nest_end(skb, af); | |
1327 | } | |
1328 | } | |
1329 | ||
1330 | nla_nest_end(skb, af_spec); | |
1331 | ||
053c095a JB |
1332 | nlmsg_end(skb, nlh); |
1333 | return 0; | |
b60c5115 TG |
1334 | |
1335 | nla_put_failure: | |
26932566 PM |
1336 | nlmsg_cancel(skb, nlh); |
1337 | return -EMSGSIZE; | |
1da177e4 LT |
1338 | } |
1339 | ||
f7b12606 | 1340 | static const struct nla_policy ifla_policy[IFLA_MAX+1] = { |
5176f91e | 1341 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
38f7b870 PM |
1342 | [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
1343 | [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, | |
5176f91e | 1344 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
da5e0494 | 1345 | [IFLA_MTU] = { .type = NLA_U32 }, |
76e87306 | 1346 | [IFLA_LINK] = { .type = NLA_U32 }, |
fbaec0ea | 1347 | [IFLA_MASTER] = { .type = NLA_U32 }, |
9a57247f | 1348 | [IFLA_CARRIER] = { .type = NLA_U8 }, |
da5e0494 TG |
1349 | [IFLA_TXQLEN] = { .type = NLA_U32 }, |
1350 | [IFLA_WEIGHT] = { .type = NLA_U32 }, | |
1351 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, | |
1352 | [IFLA_LINKMODE] = { .type = NLA_U8 }, | |
76e87306 | 1353 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, |
d8a5ec67 | 1354 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
f0630529 | 1355 | [IFLA_NET_NS_FD] = { .type = NLA_U32 }, |
0b815a1a | 1356 | [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, |
c02db8c6 | 1357 | [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, |
57b61080 SF |
1358 | [IFLA_VF_PORTS] = { .type = NLA_NESTED }, |
1359 | [IFLA_PORT_SELF] = { .type = NLA_NESTED }, | |
f8ff182c | 1360 | [IFLA_AF_SPEC] = { .type = NLA_NESTED }, |
115c9b81 | 1361 | [IFLA_EXT_MASK] = { .type = NLA_U32 }, |
edbc0bb3 | 1362 | [IFLA_PROMISCUITY] = { .type = NLA_U32 }, |
76ff5cc9 JP |
1363 | [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, |
1364 | [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, | |
02637fce | 1365 | [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, |
2d3b479d | 1366 | [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ |
82f28412 | 1367 | [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, |
317f4810 | 1368 | [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, |
88d6378b | 1369 | [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, |
da5e0494 TG |
1370 | }; |
1371 | ||
38f7b870 PM |
1372 | static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { |
1373 | [IFLA_INFO_KIND] = { .type = NLA_STRING }, | |
1374 | [IFLA_INFO_DATA] = { .type = NLA_NESTED }, | |
ba7d49b1 JP |
1375 | [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, |
1376 | [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, | |
38f7b870 PM |
1377 | }; |
1378 | ||
c02db8c6 | 1379 | static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { |
364d5716 DB |
1380 | [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, |
1381 | [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, | |
1382 | [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, | |
1383 | [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, | |
1384 | [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, | |
1385 | [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, | |
01a3d796 | 1386 | [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, |
3b766cd8 | 1387 | [IFLA_VF_STATS] = { .type = NLA_NESTED }, |
dd461d6a | 1388 | [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, |
3b766cd8 EBE |
1389 | }; |
1390 | ||
1391 | static const struct nla_policy ifla_vf_stats_policy[IFLA_VF_STATS_MAX + 1] = { | |
1392 | [IFLA_VF_STATS_RX_PACKETS] = { .type = NLA_U64 }, | |
1393 | [IFLA_VF_STATS_TX_PACKETS] = { .type = NLA_U64 }, | |
1394 | [IFLA_VF_STATS_RX_BYTES] = { .type = NLA_U64 }, | |
1395 | [IFLA_VF_STATS_TX_BYTES] = { .type = NLA_U64 }, | |
1396 | [IFLA_VF_STATS_BROADCAST] = { .type = NLA_U64 }, | |
1397 | [IFLA_VF_STATS_MULTICAST] = { .type = NLA_U64 }, | |
c02db8c6 CW |
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 | ||
f7b12606 JP |
1414 | static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) |
1415 | { | |
1416 | struct net *net = sock_net(skb->sk); | |
1417 | int h, s_h; | |
1418 | int idx = 0, s_idx; | |
1419 | struct net_device *dev; | |
1420 | struct hlist_head *head; | |
1421 | struct nlattr *tb[IFLA_MAX+1]; | |
1422 | u32 ext_filter_mask = 0; | |
973462bb | 1423 | int err; |
e5eca6d4 | 1424 | int hdrlen; |
f7b12606 JP |
1425 | |
1426 | s_h = cb->args[0]; | |
1427 | s_idx = cb->args[1]; | |
1428 | ||
f7b12606 JP |
1429 | cb->seq = net->dev_base_seq; |
1430 | ||
e5eca6d4 MS |
1431 | /* A hack to preserve kernel<->userspace interface. |
1432 | * The correct header is ifinfomsg. It is consistent with rtnl_getlink. | |
1433 | * However, before Linux v3.9 the code here assumed rtgenmsg and that's | |
1434 | * what iproute2 < v3.9.0 used. | |
1435 | * We can detect the old iproute2. Even including the IFLA_EXT_MASK | |
1436 | * attribute, its netlink message is shorter than struct ifinfomsg. | |
1437 | */ | |
1438 | hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? | |
1439 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); | |
1440 | ||
1441 | if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { | |
f7b12606 JP |
1442 | |
1443 | if (tb[IFLA_EXT_MASK]) | |
1444 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); | |
1445 | } | |
1446 | ||
1447 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { | |
1448 | idx = 0; | |
1449 | head = &net->dev_index_head[h]; | |
cac5e65e | 1450 | hlist_for_each_entry(dev, head, index_hlist) { |
f7b12606 JP |
1451 | if (idx < s_idx) |
1452 | goto cont; | |
973462bb DG |
1453 | err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, |
1454 | NETLINK_CB(cb->skb).portid, | |
1455 | cb->nlh->nlmsg_seq, 0, | |
1456 | NLM_F_MULTI, | |
1457 | ext_filter_mask); | |
1458 | /* If we ran out of room on the first message, | |
1459 | * we're in trouble | |
1460 | */ | |
1461 | WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); | |
1462 | ||
7b46a644 | 1463 | if (err < 0) |
f7b12606 JP |
1464 | goto out; |
1465 | ||
1466 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
1467 | cont: | |
1468 | idx++; | |
1469 | } | |
1470 | } | |
1471 | out: | |
f7b12606 JP |
1472 | cb->args[1] = idx; |
1473 | cb->args[0] = h; | |
1474 | ||
1475 | return skb->len; | |
1476 | } | |
1477 | ||
1478 | int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len) | |
1479 | { | |
1480 | return nla_parse(tb, IFLA_MAX, head, len, ifla_policy); | |
1481 | } | |
1482 | EXPORT_SYMBOL(rtnl_nla_parse_ifla); | |
1483 | ||
81adee47 EB |
1484 | struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) |
1485 | { | |
1486 | struct net *net; | |
1487 | /* Examine the link attributes and figure out which | |
1488 | * network namespace we are talking about. | |
1489 | */ | |
1490 | if (tb[IFLA_NET_NS_PID]) | |
1491 | net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); | |
f0630529 EB |
1492 | else if (tb[IFLA_NET_NS_FD]) |
1493 | net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); | |
81adee47 EB |
1494 | else |
1495 | net = get_net(src_net); | |
1496 | return net; | |
1497 | } | |
1498 | EXPORT_SYMBOL(rtnl_link_get_net); | |
1499 | ||
1840bb13 TG |
1500 | static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) |
1501 | { | |
1502 | if (dev) { | |
1503 | if (tb[IFLA_ADDRESS] && | |
1504 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) | |
1505 | return -EINVAL; | |
1506 | ||
1507 | if (tb[IFLA_BROADCAST] && | |
1508 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) | |
1509 | return -EINVAL; | |
1510 | } | |
1511 | ||
cf7afbfe TG |
1512 | if (tb[IFLA_AF_SPEC]) { |
1513 | struct nlattr *af; | |
1514 | int rem, err; | |
1515 | ||
1516 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { | |
1517 | const struct rtnl_af_ops *af_ops; | |
1518 | ||
1519 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) | |
1520 | return -EAFNOSUPPORT; | |
1521 | ||
1522 | if (!af_ops->set_link_af) | |
1523 | return -EOPNOTSUPP; | |
1524 | ||
1525 | if (af_ops->validate_link_af) { | |
6d3a9a68 | 1526 | err = af_ops->validate_link_af(dev, af); |
cf7afbfe TG |
1527 | if (err < 0) |
1528 | return err; | |
1529 | } | |
1530 | } | |
1531 | } | |
1532 | ||
1840bb13 TG |
1533 | return 0; |
1534 | } | |
1535 | ||
4f7d2cdf | 1536 | static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) |
c02db8c6 | 1537 | { |
c02db8c6 | 1538 | const struct net_device_ops *ops = dev->netdev_ops; |
4f7d2cdf | 1539 | int err = -EINVAL; |
c02db8c6 | 1540 | |
4f7d2cdf DB |
1541 | if (tb[IFLA_VF_MAC]) { |
1542 | struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); | |
01a3d796 | 1543 | |
4f7d2cdf DB |
1544 | err = -EOPNOTSUPP; |
1545 | if (ops->ndo_set_vf_mac) | |
1546 | err = ops->ndo_set_vf_mac(dev, ivm->vf, | |
1547 | ivm->mac); | |
1548 | if (err < 0) | |
1549 | return err; | |
1550 | } | |
1551 | ||
1552 | if (tb[IFLA_VF_VLAN]) { | |
1553 | struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); | |
1554 | ||
1555 | err = -EOPNOTSUPP; | |
1556 | if (ops->ndo_set_vf_vlan) | |
1557 | err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, | |
1558 | ivv->qos); | |
1559 | if (err < 0) | |
1560 | return err; | |
c02db8c6 | 1561 | } |
4f7d2cdf DB |
1562 | |
1563 | if (tb[IFLA_VF_TX_RATE]) { | |
1564 | struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); | |
1565 | struct ifla_vf_info ivf; | |
1566 | ||
1567 | err = -EOPNOTSUPP; | |
1568 | if (ops->ndo_get_vf_config) | |
1569 | err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); | |
1570 | if (err < 0) | |
1571 | return err; | |
1572 | ||
1573 | err = -EOPNOTSUPP; | |
1574 | if (ops->ndo_set_vf_rate) | |
1575 | err = ops->ndo_set_vf_rate(dev, ivt->vf, | |
1576 | ivf.min_tx_rate, | |
1577 | ivt->rate); | |
1578 | if (err < 0) | |
1579 | return err; | |
1580 | } | |
1581 | ||
1582 | if (tb[IFLA_VF_RATE]) { | |
1583 | struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); | |
1584 | ||
1585 | err = -EOPNOTSUPP; | |
1586 | if (ops->ndo_set_vf_rate) | |
1587 | err = ops->ndo_set_vf_rate(dev, ivt->vf, | |
1588 | ivt->min_tx_rate, | |
1589 | ivt->max_tx_rate); | |
1590 | if (err < 0) | |
1591 | return err; | |
1592 | } | |
1593 | ||
1594 | if (tb[IFLA_VF_SPOOFCHK]) { | |
1595 | struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); | |
1596 | ||
1597 | err = -EOPNOTSUPP; | |
1598 | if (ops->ndo_set_vf_spoofchk) | |
1599 | err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, | |
1600 | ivs->setting); | |
1601 | if (err < 0) | |
1602 | return err; | |
1603 | } | |
1604 | ||
1605 | if (tb[IFLA_VF_LINK_STATE]) { | |
1606 | struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); | |
1607 | ||
1608 | err = -EOPNOTSUPP; | |
1609 | if (ops->ndo_set_vf_link_state) | |
1610 | err = ops->ndo_set_vf_link_state(dev, ivl->vf, | |
1611 | ivl->link_state); | |
1612 | if (err < 0) | |
1613 | return err; | |
1614 | } | |
1615 | ||
1616 | if (tb[IFLA_VF_RSS_QUERY_EN]) { | |
1617 | struct ifla_vf_rss_query_en *ivrssq_en; | |
1618 | ||
1619 | err = -EOPNOTSUPP; | |
1620 | ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); | |
1621 | if (ops->ndo_set_vf_rss_query_en) | |
1622 | err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, | |
1623 | ivrssq_en->setting); | |
1624 | if (err < 0) | |
1625 | return err; | |
1626 | } | |
1627 | ||
dd461d6a HS |
1628 | if (tb[IFLA_VF_TRUST]) { |
1629 | struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); | |
1630 | ||
1631 | err = -EOPNOTSUPP; | |
1632 | if (ops->ndo_set_vf_trust) | |
1633 | err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); | |
1634 | if (err < 0) | |
1635 | return err; | |
1636 | } | |
1637 | ||
c02db8c6 CW |
1638 | return err; |
1639 | } | |
1640 | ||
fbaec0ea JP |
1641 | static int do_set_master(struct net_device *dev, int ifindex) |
1642 | { | |
898e5061 | 1643 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
fbaec0ea JP |
1644 | const struct net_device_ops *ops; |
1645 | int err; | |
1646 | ||
898e5061 JP |
1647 | if (upper_dev) { |
1648 | if (upper_dev->ifindex == ifindex) | |
fbaec0ea | 1649 | return 0; |
898e5061 | 1650 | ops = upper_dev->netdev_ops; |
fbaec0ea | 1651 | if (ops->ndo_del_slave) { |
898e5061 | 1652 | err = ops->ndo_del_slave(upper_dev, dev); |
fbaec0ea JP |
1653 | if (err) |
1654 | return err; | |
1655 | } else { | |
1656 | return -EOPNOTSUPP; | |
1657 | } | |
1658 | } | |
1659 | ||
1660 | if (ifindex) { | |
898e5061 JP |
1661 | upper_dev = __dev_get_by_index(dev_net(dev), ifindex); |
1662 | if (!upper_dev) | |
fbaec0ea | 1663 | return -EINVAL; |
898e5061 | 1664 | ops = upper_dev->netdev_ops; |
fbaec0ea | 1665 | if (ops->ndo_add_slave) { |
898e5061 | 1666 | err = ops->ndo_add_slave(upper_dev, dev); |
fbaec0ea JP |
1667 | if (err) |
1668 | return err; | |
1669 | } else { | |
1670 | return -EOPNOTSUPP; | |
1671 | } | |
1672 | } | |
1673 | return 0; | |
1674 | } | |
1675 | ||
90c325e3 | 1676 | #define DO_SETLINK_MODIFIED 0x01 |
ba998906 ND |
1677 | /* notify flag means notify + modified. */ |
1678 | #define DO_SETLINK_NOTIFY 0x03 | |
90f62cf3 EB |
1679 | static int do_setlink(const struct sk_buff *skb, |
1680 | struct net_device *dev, struct ifinfomsg *ifm, | |
90c325e3 | 1681 | struct nlattr **tb, char *ifname, int status) |
1da177e4 | 1682 | { |
d314774c | 1683 | const struct net_device_ops *ops = dev->netdev_ops; |
0157f60c | 1684 | int err; |
1da177e4 | 1685 | |
f0630529 | 1686 | if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { |
81adee47 | 1687 | struct net *net = rtnl_link_get_net(dev_net(dev), tb); |
d8a5ec67 EB |
1688 | if (IS_ERR(net)) { |
1689 | err = PTR_ERR(net); | |
1690 | goto errout; | |
1691 | } | |
90f62cf3 | 1692 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { |
e0ebde0e | 1693 | put_net(net); |
b51642f6 EB |
1694 | err = -EPERM; |
1695 | goto errout; | |
1696 | } | |
d8a5ec67 EB |
1697 | err = dev_change_net_namespace(dev, net, ifname); |
1698 | put_net(net); | |
1699 | if (err) | |
1700 | goto errout; | |
90c325e3 | 1701 | status |= DO_SETLINK_MODIFIED; |
d8a5ec67 EB |
1702 | } |
1703 | ||
da5e0494 | 1704 | if (tb[IFLA_MAP]) { |
1da177e4 LT |
1705 | struct rtnl_link_ifmap *u_map; |
1706 | struct ifmap k_map; | |
1707 | ||
d314774c | 1708 | if (!ops->ndo_set_config) { |
1da177e4 | 1709 | err = -EOPNOTSUPP; |
0157f60c | 1710 | goto errout; |
1da177e4 LT |
1711 | } |
1712 | ||
1713 | if (!netif_device_present(dev)) { | |
1714 | err = -ENODEV; | |
0157f60c | 1715 | goto errout; |
1da177e4 | 1716 | } |
1da177e4 | 1717 | |
da5e0494 | 1718 | u_map = nla_data(tb[IFLA_MAP]); |
1da177e4 LT |
1719 | k_map.mem_start = (unsigned long) u_map->mem_start; |
1720 | k_map.mem_end = (unsigned long) u_map->mem_end; | |
1721 | k_map.base_addr = (unsigned short) u_map->base_addr; | |
1722 | k_map.irq = (unsigned char) u_map->irq; | |
1723 | k_map.dma = (unsigned char) u_map->dma; | |
1724 | k_map.port = (unsigned char) u_map->port; | |
1725 | ||
d314774c | 1726 | err = ops->ndo_set_config(dev, &k_map); |
da5e0494 | 1727 | if (err < 0) |
0157f60c | 1728 | goto errout; |
1da177e4 | 1729 | |
ba998906 | 1730 | status |= DO_SETLINK_NOTIFY; |
1da177e4 LT |
1731 | } |
1732 | ||
da5e0494 | 1733 | if (tb[IFLA_ADDRESS]) { |
70f8e78e DM |
1734 | struct sockaddr *sa; |
1735 | int len; | |
1736 | ||
70f8e78e DM |
1737 | len = sizeof(sa_family_t) + dev->addr_len; |
1738 | sa = kmalloc(len, GFP_KERNEL); | |
1739 | if (!sa) { | |
1740 | err = -ENOMEM; | |
0157f60c | 1741 | goto errout; |
70f8e78e DM |
1742 | } |
1743 | sa->sa_family = dev->type; | |
da5e0494 | 1744 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
70f8e78e | 1745 | dev->addr_len); |
e7c3273e | 1746 | err = dev_set_mac_address(dev, sa); |
70f8e78e | 1747 | kfree(sa); |
1da177e4 | 1748 | if (err) |
0157f60c | 1749 | goto errout; |
90c325e3 | 1750 | status |= DO_SETLINK_MODIFIED; |
1da177e4 LT |
1751 | } |
1752 | ||
da5e0494 TG |
1753 | if (tb[IFLA_MTU]) { |
1754 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); | |
1755 | if (err < 0) | |
0157f60c | 1756 | goto errout; |
90c325e3 | 1757 | status |= DO_SETLINK_MODIFIED; |
1da177e4 LT |
1758 | } |
1759 | ||
cbda10fa VD |
1760 | if (tb[IFLA_GROUP]) { |
1761 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); | |
ba998906 | 1762 | status |= DO_SETLINK_NOTIFY; |
cbda10fa VD |
1763 | } |
1764 | ||
da5e0494 TG |
1765 | /* |
1766 | * Interface selected by interface index but interface | |
1767 | * name provided implies that a name change has been | |
1768 | * requested. | |
1769 | */ | |
51055be8 | 1770 | if (ifm->ifi_index > 0 && ifname[0]) { |
da5e0494 TG |
1771 | err = dev_change_name(dev, ifname); |
1772 | if (err < 0) | |
0157f60c | 1773 | goto errout; |
90c325e3 | 1774 | status |= DO_SETLINK_MODIFIED; |
1da177e4 LT |
1775 | } |
1776 | ||
0b815a1a SH |
1777 | if (tb[IFLA_IFALIAS]) { |
1778 | err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), | |
1779 | nla_len(tb[IFLA_IFALIAS])); | |
1780 | if (err < 0) | |
1781 | goto errout; | |
ba998906 | 1782 | status |= DO_SETLINK_NOTIFY; |
0b815a1a SH |
1783 | } |
1784 | ||
da5e0494 TG |
1785 | if (tb[IFLA_BROADCAST]) { |
1786 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); | |
e7c3273e | 1787 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
1da177e4 LT |
1788 | } |
1789 | ||
83b496e9 | 1790 | if (ifm->ifi_flags || ifm->ifi_change) { |
3729d502 | 1791 | err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
5f9021cf JB |
1792 | if (err < 0) |
1793 | goto errout; | |
83b496e9 | 1794 | } |
1da177e4 | 1795 | |
fbaec0ea JP |
1796 | if (tb[IFLA_MASTER]) { |
1797 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); | |
1798 | if (err) | |
1799 | goto errout; | |
90c325e3 | 1800 | status |= DO_SETLINK_MODIFIED; |
fbaec0ea JP |
1801 | } |
1802 | ||
9a57247f JP |
1803 | if (tb[IFLA_CARRIER]) { |
1804 | err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); | |
1805 | if (err) | |
1806 | goto errout; | |
90c325e3 | 1807 | status |= DO_SETLINK_MODIFIED; |
9a57247f JP |
1808 | } |
1809 | ||
5d1180fc ND |
1810 | if (tb[IFLA_TXQLEN]) { |
1811 | unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]); | |
1812 | ||
1813 | if (dev->tx_queue_len ^ value) | |
ba998906 | 1814 | status |= DO_SETLINK_NOTIFY; |
5d1180fc ND |
1815 | |
1816 | dev->tx_queue_len = value; | |
1817 | } | |
b00055aa | 1818 | |
da5e0494 | 1819 | if (tb[IFLA_OPERSTATE]) |
93b2d4a2 | 1820 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
b00055aa | 1821 | |
da5e0494 | 1822 | if (tb[IFLA_LINKMODE]) { |
1889b0e7 ND |
1823 | unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); |
1824 | ||
93b2d4a2 | 1825 | write_lock_bh(&dev_base_lock); |
1889b0e7 | 1826 | if (dev->link_mode ^ value) |
ba998906 | 1827 | status |= DO_SETLINK_NOTIFY; |
1889b0e7 | 1828 | dev->link_mode = value; |
93b2d4a2 | 1829 | write_unlock_bh(&dev_base_lock); |
b00055aa SR |
1830 | } |
1831 | ||
c02db8c6 | 1832 | if (tb[IFLA_VFINFO_LIST]) { |
4f7d2cdf | 1833 | struct nlattr *vfinfo[IFLA_VF_MAX + 1]; |
c02db8c6 CW |
1834 | struct nlattr *attr; |
1835 | int rem; | |
4f7d2cdf | 1836 | |
c02db8c6 | 1837 | nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { |
4f7d2cdf DB |
1838 | if (nla_type(attr) != IFLA_VF_INFO || |
1839 | nla_len(attr) < NLA_HDRLEN) { | |
253683bb | 1840 | err = -EINVAL; |
c02db8c6 | 1841 | goto errout; |
253683bb | 1842 | } |
4f7d2cdf DB |
1843 | err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, |
1844 | ifla_vf_policy); | |
1845 | if (err < 0) | |
1846 | goto errout; | |
1847 | err = do_setvfinfo(dev, vfinfo); | |
c02db8c6 CW |
1848 | if (err < 0) |
1849 | goto errout; | |
ba998906 | 1850 | status |= DO_SETLINK_NOTIFY; |
c02db8c6 | 1851 | } |
ebc08a6f | 1852 | } |
1da177e4 LT |
1853 | err = 0; |
1854 | ||
57b61080 SF |
1855 | if (tb[IFLA_VF_PORTS]) { |
1856 | struct nlattr *port[IFLA_PORT_MAX+1]; | |
1857 | struct nlattr *attr; | |
1858 | int vf; | |
1859 | int rem; | |
1860 | ||
1861 | err = -EOPNOTSUPP; | |
1862 | if (!ops->ndo_set_vf_port) | |
1863 | goto errout; | |
1864 | ||
1865 | nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { | |
035d210f DB |
1866 | if (nla_type(attr) != IFLA_VF_PORT || |
1867 | nla_len(attr) < NLA_HDRLEN) { | |
1868 | err = -EINVAL; | |
1869 | goto errout; | |
1870 | } | |
1871 | err = nla_parse_nested(port, IFLA_PORT_MAX, attr, | |
1872 | ifla_port_policy); | |
57b61080 SF |
1873 | if (err < 0) |
1874 | goto errout; | |
1875 | if (!port[IFLA_PORT_VF]) { | |
1876 | err = -EOPNOTSUPP; | |
1877 | goto errout; | |
1878 | } | |
1879 | vf = nla_get_u32(port[IFLA_PORT_VF]); | |
1880 | err = ops->ndo_set_vf_port(dev, vf, port); | |
1881 | if (err < 0) | |
1882 | goto errout; | |
ba998906 | 1883 | status |= DO_SETLINK_NOTIFY; |
57b61080 SF |
1884 | } |
1885 | } | |
1886 | err = 0; | |
1887 | ||
1888 | if (tb[IFLA_PORT_SELF]) { | |
1889 | struct nlattr *port[IFLA_PORT_MAX+1]; | |
1890 | ||
1891 | err = nla_parse_nested(port, IFLA_PORT_MAX, | |
1892 | tb[IFLA_PORT_SELF], ifla_port_policy); | |
1893 | if (err < 0) | |
1894 | goto errout; | |
1895 | ||
1896 | err = -EOPNOTSUPP; | |
1897 | if (ops->ndo_set_vf_port) | |
1898 | err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); | |
1899 | if (err < 0) | |
1900 | goto errout; | |
ba998906 | 1901 | status |= DO_SETLINK_NOTIFY; |
57b61080 | 1902 | } |
f8ff182c TG |
1903 | |
1904 | if (tb[IFLA_AF_SPEC]) { | |
1905 | struct nlattr *af; | |
1906 | int rem; | |
1907 | ||
1908 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { | |
1909 | const struct rtnl_af_ops *af_ops; | |
1910 | ||
1911 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) | |
cf7afbfe | 1912 | BUG(); |
f8ff182c | 1913 | |
cf7afbfe | 1914 | err = af_ops->set_link_af(dev, af); |
f8ff182c TG |
1915 | if (err < 0) |
1916 | goto errout; | |
1917 | ||
ba998906 | 1918 | status |= DO_SETLINK_NOTIFY; |
f8ff182c TG |
1919 | } |
1920 | } | |
57b61080 SF |
1921 | err = 0; |
1922 | ||
88d6378b AK |
1923 | if (tb[IFLA_PROTO_DOWN]) { |
1924 | err = dev_change_proto_down(dev, | |
1925 | nla_get_u8(tb[IFLA_PROTO_DOWN])); | |
1926 | if (err) | |
1927 | goto errout; | |
1928 | status |= DO_SETLINK_NOTIFY; | |
1929 | } | |
1930 | ||
0157f60c | 1931 | errout: |
ba998906 ND |
1932 | if (status & DO_SETLINK_MODIFIED) { |
1933 | if (status & DO_SETLINK_NOTIFY) | |
1934 | netdev_state_change(dev); | |
1935 | ||
1936 | if (err < 0) | |
1937 | 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", | |
1938 | dev->name); | |
1939 | } | |
da5e0494 | 1940 | |
0157f60c PM |
1941 | return err; |
1942 | } | |
1da177e4 | 1943 | |
661d2967 | 1944 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
0157f60c | 1945 | { |
3b1e0a65 | 1946 | struct net *net = sock_net(skb->sk); |
0157f60c PM |
1947 | struct ifinfomsg *ifm; |
1948 | struct net_device *dev; | |
1949 | int err; | |
1950 | struct nlattr *tb[IFLA_MAX+1]; | |
1951 | char ifname[IFNAMSIZ]; | |
1952 | ||
1953 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); | |
1954 | if (err < 0) | |
1955 | goto errout; | |
1956 | ||
1957 | if (tb[IFLA_IFNAME]) | |
1958 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
1959 | else | |
1960 | ifname[0] = '\0'; | |
1961 | ||
1962 | err = -EINVAL; | |
1963 | ifm = nlmsg_data(nlh); | |
1964 | if (ifm->ifi_index > 0) | |
a3d12891 | 1965 | dev = __dev_get_by_index(net, ifm->ifi_index); |
0157f60c | 1966 | else if (tb[IFLA_IFNAME]) |
a3d12891 | 1967 | dev = __dev_get_by_name(net, ifname); |
0157f60c PM |
1968 | else |
1969 | goto errout; | |
1970 | ||
1971 | if (dev == NULL) { | |
1972 | err = -ENODEV; | |
1973 | goto errout; | |
1974 | } | |
1975 | ||
e0d087af ED |
1976 | err = validate_linkmsg(dev, tb); |
1977 | if (err < 0) | |
a3d12891 | 1978 | goto errout; |
0157f60c | 1979 | |
90f62cf3 | 1980 | err = do_setlink(skb, dev, ifm, tb, ifname, 0); |
da5e0494 | 1981 | errout: |
1da177e4 LT |
1982 | return err; |
1983 | } | |
1984 | ||
66400d54 WC |
1985 | static int rtnl_group_dellink(const struct net *net, int group) |
1986 | { | |
1987 | struct net_device *dev, *aux; | |
1988 | LIST_HEAD(list_kill); | |
1989 | bool found = false; | |
1990 | ||
1991 | if (!group) | |
1992 | return -EPERM; | |
1993 | ||
1994 | for_each_netdev(net, dev) { | |
1995 | if (dev->group == group) { | |
1996 | const struct rtnl_link_ops *ops; | |
1997 | ||
1998 | found = true; | |
1999 | ops = dev->rtnl_link_ops; | |
2000 | if (!ops || !ops->dellink) | |
2001 | return -EOPNOTSUPP; | |
2002 | } | |
2003 | } | |
2004 | ||
2005 | if (!found) | |
2006 | return -ENODEV; | |
2007 | ||
2008 | for_each_netdev_safe(net, dev, aux) { | |
2009 | if (dev->group == group) { | |
2010 | const struct rtnl_link_ops *ops; | |
2011 | ||
2012 | ops = dev->rtnl_link_ops; | |
2013 | ops->dellink(dev, &list_kill); | |
2014 | } | |
2015 | } | |
2016 | unregister_netdevice_many(&list_kill); | |
2017 | ||
2018 | return 0; | |
2019 | } | |
2020 | ||
614732ea TG |
2021 | int rtnl_delete_link(struct net_device *dev) |
2022 | { | |
2023 | const struct rtnl_link_ops *ops; | |
2024 | LIST_HEAD(list_kill); | |
2025 | ||
2026 | ops = dev->rtnl_link_ops; | |
2027 | if (!ops || !ops->dellink) | |
2028 | return -EOPNOTSUPP; | |
2029 | ||
2030 | ops->dellink(dev, &list_kill); | |
2031 | unregister_netdevice_many(&list_kill); | |
2032 | ||
2033 | return 0; | |
2034 | } | |
2035 | EXPORT_SYMBOL_GPL(rtnl_delete_link); | |
2036 | ||
661d2967 | 2037 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) |
38f7b870 | 2038 | { |
3b1e0a65 | 2039 | struct net *net = sock_net(skb->sk); |
38f7b870 PM |
2040 | struct net_device *dev; |
2041 | struct ifinfomsg *ifm; | |
2042 | char ifname[IFNAMSIZ]; | |
2043 | struct nlattr *tb[IFLA_MAX+1]; | |
2044 | int err; | |
2045 | ||
2046 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); | |
2047 | if (err < 0) | |
2048 | return err; | |
2049 | ||
2050 | if (tb[IFLA_IFNAME]) | |
2051 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
2052 | ||
2053 | ifm = nlmsg_data(nlh); | |
2054 | if (ifm->ifi_index > 0) | |
881d966b | 2055 | dev = __dev_get_by_index(net, ifm->ifi_index); |
38f7b870 | 2056 | else if (tb[IFLA_IFNAME]) |
881d966b | 2057 | dev = __dev_get_by_name(net, ifname); |
66400d54 WC |
2058 | else if (tb[IFLA_GROUP]) |
2059 | return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP])); | |
38f7b870 PM |
2060 | else |
2061 | return -EINVAL; | |
2062 | ||
2063 | if (!dev) | |
2064 | return -ENODEV; | |
2065 | ||
614732ea | 2066 | return rtnl_delete_link(dev); |
38f7b870 PM |
2067 | } |
2068 | ||
3729d502 PM |
2069 | int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) |
2070 | { | |
2071 | unsigned int old_flags; | |
2072 | int err; | |
2073 | ||
2074 | old_flags = dev->flags; | |
2075 | if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { | |
2076 | err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); | |
2077 | if (err < 0) | |
2078 | return err; | |
2079 | } | |
2080 | ||
2081 | dev->rtnl_link_state = RTNL_LINK_INITIALIZED; | |
3729d502 | 2082 | |
a528c219 | 2083 | __dev_notify_flags(dev, old_flags, ~0U); |
3729d502 PM |
2084 | return 0; |
2085 | } | |
2086 | EXPORT_SYMBOL(rtnl_configure_link); | |
2087 | ||
c0713563 | 2088 | struct net_device *rtnl_create_link(struct net *net, |
78ebb0d0 | 2089 | const char *ifname, unsigned char name_assign_type, |
5517750f | 2090 | const struct rtnl_link_ops *ops, struct nlattr *tb[]) |
e7199288 PE |
2091 | { |
2092 | int err; | |
2093 | struct net_device *dev; | |
d40156aa JP |
2094 | unsigned int num_tx_queues = 1; |
2095 | unsigned int num_rx_queues = 1; | |
e7199288 | 2096 | |
76ff5cc9 JP |
2097 | if (tb[IFLA_NUM_TX_QUEUES]) |
2098 | num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); | |
2099 | else if (ops->get_num_tx_queues) | |
d40156aa | 2100 | num_tx_queues = ops->get_num_tx_queues(); |
76ff5cc9 JP |
2101 | |
2102 | if (tb[IFLA_NUM_RX_QUEUES]) | |
2103 | num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); | |
2104 | else if (ops->get_num_rx_queues) | |
d40156aa | 2105 | num_rx_queues = ops->get_num_rx_queues(); |
efacb309 | 2106 | |
e7199288 | 2107 | err = -ENOMEM; |
5517750f | 2108 | dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, |
c835a677 | 2109 | ops->setup, num_tx_queues, num_rx_queues); |
e7199288 PE |
2110 | if (!dev) |
2111 | goto err; | |
2112 | ||
81adee47 EB |
2113 | dev_net_set(dev, net); |
2114 | dev->rtnl_link_ops = ops; | |
3729d502 | 2115 | dev->rtnl_link_state = RTNL_LINK_INITIALIZING; |
81adee47 | 2116 | |
e7199288 PE |
2117 | if (tb[IFLA_MTU]) |
2118 | dev->mtu = nla_get_u32(tb[IFLA_MTU]); | |
2afb9b53 | 2119 | if (tb[IFLA_ADDRESS]) { |
e7199288 PE |
2120 | memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), |
2121 | nla_len(tb[IFLA_ADDRESS])); | |
2afb9b53 JP |
2122 | dev->addr_assign_type = NET_ADDR_SET; |
2123 | } | |
e7199288 PE |
2124 | if (tb[IFLA_BROADCAST]) |
2125 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), | |
2126 | nla_len(tb[IFLA_BROADCAST])); | |
2127 | if (tb[IFLA_TXQLEN]) | |
2128 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); | |
2129 | if (tb[IFLA_OPERSTATE]) | |
93b2d4a2 | 2130 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
e7199288 PE |
2131 | if (tb[IFLA_LINKMODE]) |
2132 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); | |
ffa934f1 PM |
2133 | if (tb[IFLA_GROUP]) |
2134 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); | |
e7199288 PE |
2135 | |
2136 | return dev; | |
2137 | ||
e7199288 PE |
2138 | err: |
2139 | return ERR_PTR(err); | |
2140 | } | |
e0d087af | 2141 | EXPORT_SYMBOL(rtnl_create_link); |
e7199288 | 2142 | |
90f62cf3 EB |
2143 | static int rtnl_group_changelink(const struct sk_buff *skb, |
2144 | struct net *net, int group, | |
e7ed828f VD |
2145 | struct ifinfomsg *ifm, |
2146 | struct nlattr **tb) | |
2147 | { | |
d079535d | 2148 | struct net_device *dev, *aux; |
e7ed828f VD |
2149 | int err; |
2150 | ||
d079535d | 2151 | for_each_netdev_safe(net, dev, aux) { |
e7ed828f | 2152 | if (dev->group == group) { |
90f62cf3 | 2153 | err = do_setlink(skb, dev, ifm, tb, NULL, 0); |
e7ed828f VD |
2154 | if (err < 0) |
2155 | return err; | |
2156 | } | |
2157 | } | |
2158 | ||
2159 | return 0; | |
2160 | } | |
2161 | ||
661d2967 | 2162 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
38f7b870 | 2163 | { |
3b1e0a65 | 2164 | struct net *net = sock_net(skb->sk); |
38f7b870 | 2165 | const struct rtnl_link_ops *ops; |
ba7d49b1 | 2166 | const struct rtnl_link_ops *m_ops = NULL; |
38f7b870 | 2167 | struct net_device *dev; |
ba7d49b1 | 2168 | struct net_device *master_dev = NULL; |
38f7b870 PM |
2169 | struct ifinfomsg *ifm; |
2170 | char kind[MODULE_NAME_LEN]; | |
2171 | char ifname[IFNAMSIZ]; | |
2172 | struct nlattr *tb[IFLA_MAX+1]; | |
2173 | struct nlattr *linkinfo[IFLA_INFO_MAX+1]; | |
5517750f | 2174 | unsigned char name_assign_type = NET_NAME_USER; |
38f7b870 PM |
2175 | int err; |
2176 | ||
95a5afca | 2177 | #ifdef CONFIG_MODULES |
38f7b870 | 2178 | replay: |
8072f085 | 2179 | #endif |
38f7b870 PM |
2180 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
2181 | if (err < 0) | |
2182 | return err; | |
2183 | ||
2184 | if (tb[IFLA_IFNAME]) | |
2185 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
2186 | else | |
2187 | ifname[0] = '\0'; | |
2188 | ||
2189 | ifm = nlmsg_data(nlh); | |
2190 | if (ifm->ifi_index > 0) | |
881d966b | 2191 | dev = __dev_get_by_index(net, ifm->ifi_index); |
e7ed828f VD |
2192 | else { |
2193 | if (ifname[0]) | |
2194 | dev = __dev_get_by_name(net, ifname); | |
e7ed828f VD |
2195 | else |
2196 | dev = NULL; | |
2197 | } | |
38f7b870 | 2198 | |
ba7d49b1 JP |
2199 | if (dev) { |
2200 | master_dev = netdev_master_upper_dev_get(dev); | |
2201 | if (master_dev) | |
2202 | m_ops = master_dev->rtnl_link_ops; | |
2203 | } | |
2204 | ||
e0d087af ED |
2205 | err = validate_linkmsg(dev, tb); |
2206 | if (err < 0) | |
1840bb13 TG |
2207 | return err; |
2208 | ||
38f7b870 PM |
2209 | if (tb[IFLA_LINKINFO]) { |
2210 | err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, | |
2211 | tb[IFLA_LINKINFO], ifla_info_policy); | |
2212 | if (err < 0) | |
2213 | return err; | |
2214 | } else | |
2215 | memset(linkinfo, 0, sizeof(linkinfo)); | |
2216 | ||
2217 | if (linkinfo[IFLA_INFO_KIND]) { | |
2218 | nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); | |
2219 | ops = rtnl_link_ops_get(kind); | |
2220 | } else { | |
2221 | kind[0] = '\0'; | |
2222 | ops = NULL; | |
2223 | } | |
2224 | ||
2225 | if (1) { | |
4e10fd5b SL |
2226 | struct nlattr *attr[ops ? ops->maxtype + 1 : 1]; |
2227 | struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1]; | |
ba7d49b1 JP |
2228 | struct nlattr **data = NULL; |
2229 | struct nlattr **slave_data = NULL; | |
317f4810 | 2230 | struct net *dest_net, *link_net = NULL; |
38f7b870 PM |
2231 | |
2232 | if (ops) { | |
2233 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { | |
2234 | err = nla_parse_nested(attr, ops->maxtype, | |
2235 | linkinfo[IFLA_INFO_DATA], | |
2236 | ops->policy); | |
2237 | if (err < 0) | |
2238 | return err; | |
2239 | data = attr; | |
2240 | } | |
2241 | if (ops->validate) { | |
2242 | err = ops->validate(tb, data); | |
2243 | if (err < 0) | |
2244 | return err; | |
2245 | } | |
2246 | } | |
2247 | ||
ba7d49b1 JP |
2248 | if (m_ops) { |
2249 | if (m_ops->slave_maxtype && | |
2250 | linkinfo[IFLA_INFO_SLAVE_DATA]) { | |
2251 | err = nla_parse_nested(slave_attr, | |
2252 | m_ops->slave_maxtype, | |
2253 | linkinfo[IFLA_INFO_SLAVE_DATA], | |
2254 | m_ops->slave_policy); | |
2255 | if (err < 0) | |
2256 | return err; | |
2257 | slave_data = slave_attr; | |
2258 | } | |
2259 | if (m_ops->slave_validate) { | |
2260 | err = m_ops->slave_validate(tb, slave_data); | |
2261 | if (err < 0) | |
2262 | return err; | |
2263 | } | |
2264 | } | |
2265 | ||
38f7b870 | 2266 | if (dev) { |
90c325e3 | 2267 | int status = 0; |
38f7b870 PM |
2268 | |
2269 | if (nlh->nlmsg_flags & NLM_F_EXCL) | |
2270 | return -EEXIST; | |
2271 | if (nlh->nlmsg_flags & NLM_F_REPLACE) | |
2272 | return -EOPNOTSUPP; | |
2273 | ||
2274 | if (linkinfo[IFLA_INFO_DATA]) { | |
2275 | if (!ops || ops != dev->rtnl_link_ops || | |
2276 | !ops->changelink) | |
2277 | return -EOPNOTSUPP; | |
2278 | ||
2279 | err = ops->changelink(dev, tb, data); | |
2280 | if (err < 0) | |
2281 | return err; | |
ba998906 | 2282 | status |= DO_SETLINK_NOTIFY; |
38f7b870 PM |
2283 | } |
2284 | ||
ba7d49b1 JP |
2285 | if (linkinfo[IFLA_INFO_SLAVE_DATA]) { |
2286 | if (!m_ops || !m_ops->slave_changelink) | |
2287 | return -EOPNOTSUPP; | |
2288 | ||
2289 | err = m_ops->slave_changelink(master_dev, dev, | |
2290 | tb, slave_data); | |
2291 | if (err < 0) | |
2292 | return err; | |
ba998906 | 2293 | status |= DO_SETLINK_NOTIFY; |
ba7d49b1 JP |
2294 | } |
2295 | ||
90c325e3 | 2296 | return do_setlink(skb, dev, ifm, tb, ifname, status); |
38f7b870 PM |
2297 | } |
2298 | ||
ffa934f1 PM |
2299 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { |
2300 | if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) | |
90f62cf3 | 2301 | return rtnl_group_changelink(skb, net, |
ffa934f1 PM |
2302 | nla_get_u32(tb[IFLA_GROUP]), |
2303 | ifm, tb); | |
38f7b870 | 2304 | return -ENODEV; |
ffa934f1 | 2305 | } |
38f7b870 | 2306 | |
0e06877c | 2307 | if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) |
38f7b870 PM |
2308 | return -EOPNOTSUPP; |
2309 | ||
2310 | if (!ops) { | |
95a5afca | 2311 | #ifdef CONFIG_MODULES |
38f7b870 PM |
2312 | if (kind[0]) { |
2313 | __rtnl_unlock(); | |
2314 | request_module("rtnl-link-%s", kind); | |
2315 | rtnl_lock(); | |
2316 | ops = rtnl_link_ops_get(kind); | |
2317 | if (ops) | |
2318 | goto replay; | |
2319 | } | |
2320 | #endif | |
2321 | return -EOPNOTSUPP; | |
2322 | } | |
2323 | ||
b0ab2fab JP |
2324 | if (!ops->setup) |
2325 | return -EOPNOTSUPP; | |
2326 | ||
5517750f | 2327 | if (!ifname[0]) { |
38f7b870 | 2328 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); |
5517750f TG |
2329 | name_assign_type = NET_NAME_ENUM; |
2330 | } | |
e7199288 | 2331 | |
81adee47 | 2332 | dest_net = rtnl_link_get_net(net, tb); |
13ad1774 EB |
2333 | if (IS_ERR(dest_net)) |
2334 | return PTR_ERR(dest_net); | |
2335 | ||
505ce415 EB |
2336 | err = -EPERM; |
2337 | if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN)) | |
2338 | goto out; | |
2339 | ||
317f4810 ND |
2340 | if (tb[IFLA_LINK_NETNSID]) { |
2341 | int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); | |
2342 | ||
2343 | link_net = get_net_ns_by_id(dest_net, id); | |
2344 | if (!link_net) { | |
2345 | err = -EINVAL; | |
2346 | goto out; | |
2347 | } | |
06615bed EB |
2348 | err = -EPERM; |
2349 | if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) | |
2350 | goto out; | |
317f4810 ND |
2351 | } |
2352 | ||
2353 | dev = rtnl_create_link(link_net ? : dest_net, ifname, | |
2354 | name_assign_type, ops, tb); | |
9c7dafbf | 2355 | if (IS_ERR(dev)) { |
e7199288 | 2356 | err = PTR_ERR(dev); |
9c7dafbf PE |
2357 | goto out; |
2358 | } | |
2359 | ||
2360 | dev->ifindex = ifm->ifi_index; | |
2361 | ||
0e0eee24 | 2362 | if (ops->newlink) { |
7b4ce694 | 2363 | err = ops->newlink(link_net ? : net, dev, tb, data); |
0e0eee24 | 2364 | /* Drivers should call free_netdev() in ->destructor |
e51fb152 CW |
2365 | * and unregister it on failure after registration |
2366 | * so that device could be finally freed in rtnl_unlock. | |
0e0eee24 | 2367 | */ |
e51fb152 CW |
2368 | if (err < 0) { |
2369 | /* If device is not registered at all, free it now */ | |
2370 | if (dev->reg_state == NETREG_UNINITIALIZED) | |
2371 | free_netdev(dev); | |
0e0eee24 | 2372 | goto out; |
e51fb152 | 2373 | } |
0e0eee24 | 2374 | } else { |
2d85cba2 | 2375 | err = register_netdevice(dev); |
0e0eee24 CW |
2376 | if (err < 0) { |
2377 | free_netdev(dev); | |
2378 | goto out; | |
2379 | } | |
fce9b9be | 2380 | } |
3729d502 | 2381 | err = rtnl_configure_link(dev, ifm); |
43638900 DM |
2382 | if (err < 0) |
2383 | goto out_unregister; | |
bdef279b | 2384 | if (link_net) { |
317f4810 | 2385 | err = dev_change_net_namespace(dev, dest_net, ifname); |
bdef279b | 2386 | if (err < 0) |
43638900 | 2387 | goto out_unregister; |
bdef279b | 2388 | } |
3729d502 | 2389 | out: |
317f4810 ND |
2390 | if (link_net) |
2391 | put_net(link_net); | |
81adee47 | 2392 | put_net(dest_net); |
38f7b870 | 2393 | return err; |
43638900 DM |
2394 | out_unregister: |
2395 | if (ops->newlink) { | |
2396 | LIST_HEAD(list_kill); | |
2397 | ||
2398 | ops->dellink(dev, &list_kill); | |
2399 | unregister_netdevice_many(&list_kill); | |
2400 | } else { | |
2401 | unregister_netdevice(dev); | |
2402 | } | |
2403 | goto out; | |
38f7b870 PM |
2404 | } |
2405 | } | |
2406 | ||
661d2967 | 2407 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh) |
711e2c33 | 2408 | { |
3b1e0a65 | 2409 | struct net *net = sock_net(skb->sk); |
b60c5115 | 2410 | struct ifinfomsg *ifm; |
a3d12891 | 2411 | char ifname[IFNAMSIZ]; |
b60c5115 TG |
2412 | struct nlattr *tb[IFLA_MAX+1]; |
2413 | struct net_device *dev = NULL; | |
2414 | struct sk_buff *nskb; | |
339bf98f | 2415 | int err; |
115c9b81 | 2416 | u32 ext_filter_mask = 0; |
711e2c33 | 2417 | |
b60c5115 TG |
2418 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
2419 | if (err < 0) | |
9918f230 | 2420 | return err; |
b60c5115 | 2421 | |
a3d12891 ED |
2422 | if (tb[IFLA_IFNAME]) |
2423 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
2424 | ||
115c9b81 GR |
2425 | if (tb[IFLA_EXT_MASK]) |
2426 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); | |
2427 | ||
b60c5115 | 2428 | ifm = nlmsg_data(nlh); |
a3d12891 ED |
2429 | if (ifm->ifi_index > 0) |
2430 | dev = __dev_get_by_index(net, ifm->ifi_index); | |
2431 | else if (tb[IFLA_IFNAME]) | |
2432 | dev = __dev_get_by_name(net, ifname); | |
2433 | else | |
711e2c33 | 2434 | return -EINVAL; |
711e2c33 | 2435 | |
a3d12891 ED |
2436 | if (dev == NULL) |
2437 | return -ENODEV; | |
2438 | ||
115c9b81 | 2439 | nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); |
a3d12891 ED |
2440 | if (nskb == NULL) |
2441 | return -ENOBUFS; | |
b60c5115 | 2442 | |
15e47304 | 2443 | err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid, |
115c9b81 | 2444 | nlh->nlmsg_seq, 0, 0, ext_filter_mask); |
26932566 PM |
2445 | if (err < 0) { |
2446 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ | |
2447 | WARN_ON(err == -EMSGSIZE); | |
2448 | kfree_skb(nskb); | |
a3d12891 | 2449 | } else |
15e47304 | 2450 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); |
711e2c33 | 2451 | |
b60c5115 | 2452 | return err; |
711e2c33 | 2453 | } |
711e2c33 | 2454 | |
115c9b81 | 2455 | static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) |
c7ac8679 | 2456 | { |
115c9b81 GR |
2457 | struct net *net = sock_net(skb->sk); |
2458 | struct net_device *dev; | |
2459 | struct nlattr *tb[IFLA_MAX+1]; | |
2460 | u32 ext_filter_mask = 0; | |
2461 | u16 min_ifinfo_dump_size = 0; | |
e5eca6d4 MS |
2462 | int hdrlen; |
2463 | ||
2464 | /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ | |
2465 | hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? | |
2466 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); | |
115c9b81 | 2467 | |
e5eca6d4 | 2468 | if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { |
a4b64fbe ED |
2469 | if (tb[IFLA_EXT_MASK]) |
2470 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); | |
2471 | } | |
115c9b81 GR |
2472 | |
2473 | if (!ext_filter_mask) | |
2474 | return NLMSG_GOODSIZE; | |
2475 | /* | |
2476 | * traverse the list of net devices and compute the minimum | |
2477 | * buffer size based upon the filter mask. | |
2478 | */ | |
2479 | list_for_each_entry(dev, &net->dev_base_head, dev_list) { | |
2480 | min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, | |
2481 | if_nlmsg_size(dev, | |
2482 | ext_filter_mask)); | |
2483 | } | |
2484 | ||
c7ac8679 GR |
2485 | return min_ifinfo_dump_size; |
2486 | } | |
2487 | ||
42bad1da | 2488 | static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) |
1da177e4 LT |
2489 | { |
2490 | int idx; | |
2491 | int s_idx = cb->family; | |
2492 | ||
2493 | if (s_idx == 0) | |
2494 | s_idx = 1; | |
25239cee | 2495 | for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { |
1da177e4 LT |
2496 | int type = cb->nlh->nlmsg_type-RTM_BASE; |
2497 | if (idx < s_idx || idx == PF_PACKET) | |
2498 | continue; | |
e2849863 TG |
2499 | if (rtnl_msg_handlers[idx] == NULL || |
2500 | rtnl_msg_handlers[idx][type].dumpit == NULL) | |
1da177e4 | 2501 | continue; |
0465277f | 2502 | if (idx > s_idx) { |
1da177e4 | 2503 | memset(&cb->args[0], 0, sizeof(cb->args)); |
0465277f ND |
2504 | cb->prev_seq = 0; |
2505 | cb->seq = 0; | |
2506 | } | |
e2849863 | 2507 | if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) |
1da177e4 LT |
2508 | break; |
2509 | } | |
2510 | cb->family = idx; | |
2511 | ||
2512 | return skb->len; | |
2513 | } | |
2514 | ||
395eea6c MB |
2515 | struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, |
2516 | unsigned int change, gfp_t flags) | |
1da177e4 | 2517 | { |
c346dca1 | 2518 | struct net *net = dev_net(dev); |
1da177e4 | 2519 | struct sk_buff *skb; |
0ec6d3f4 | 2520 | int err = -ENOBUFS; |
c7ac8679 | 2521 | size_t if_info_size; |
1da177e4 | 2522 | |
7f294054 | 2523 | skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); |
0ec6d3f4 TG |
2524 | if (skb == NULL) |
2525 | goto errout; | |
1da177e4 | 2526 | |
115c9b81 | 2527 | err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0); |
26932566 PM |
2528 | if (err < 0) { |
2529 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ | |
2530 | WARN_ON(err == -EMSGSIZE); | |
2531 | kfree_skb(skb); | |
2532 | goto errout; | |
2533 | } | |
395eea6c | 2534 | return skb; |
0ec6d3f4 TG |
2535 | errout: |
2536 | if (err < 0) | |
4b3da706 | 2537 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
395eea6c MB |
2538 | return NULL; |
2539 | } | |
2540 | ||
2541 | void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) | |
2542 | { | |
2543 | struct net *net = dev_net(dev); | |
2544 | ||
2545 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); | |
2546 | } | |
2547 | ||
2548 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, | |
2549 | gfp_t flags) | |
2550 | { | |
2551 | struct sk_buff *skb; | |
2552 | ||
ed2a80ab ND |
2553 | if (dev->reg_state != NETREG_REGISTERED) |
2554 | return; | |
2555 | ||
395eea6c MB |
2556 | skb = rtmsg_ifinfo_build_skb(type, dev, change, flags); |
2557 | if (skb) | |
2558 | rtmsg_ifinfo_send(skb, dev, flags); | |
1da177e4 | 2559 | } |
471cb5a3 | 2560 | EXPORT_SYMBOL(rtmsg_ifinfo); |
1da177e4 | 2561 | |
d83b0603 JF |
2562 | static int nlmsg_populate_fdb_fill(struct sk_buff *skb, |
2563 | struct net_device *dev, | |
1e53d5bb | 2564 | u8 *addr, u16 vid, u32 pid, u32 seq, |
1c104a6b ND |
2565 | int type, unsigned int flags, |
2566 | int nlflags) | |
d83b0603 JF |
2567 | { |
2568 | struct nlmsghdr *nlh; | |
2569 | struct ndmsg *ndm; | |
2570 | ||
1c104a6b | 2571 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); |
d83b0603 JF |
2572 | if (!nlh) |
2573 | return -EMSGSIZE; | |
2574 | ||
2575 | ndm = nlmsg_data(nlh); | |
2576 | ndm->ndm_family = AF_BRIDGE; | |
2577 | ndm->ndm_pad1 = 0; | |
2578 | ndm->ndm_pad2 = 0; | |
2579 | ndm->ndm_flags = flags; | |
2580 | ndm->ndm_type = 0; | |
2581 | ndm->ndm_ifindex = dev->ifindex; | |
2582 | ndm->ndm_state = NUD_PERMANENT; | |
2583 | ||
2584 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) | |
2585 | goto nla_put_failure; | |
1e53d5bb HS |
2586 | if (vid) |
2587 | if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) | |
2588 | goto nla_put_failure; | |
d83b0603 | 2589 | |
053c095a JB |
2590 | nlmsg_end(skb, nlh); |
2591 | return 0; | |
d83b0603 JF |
2592 | |
2593 | nla_put_failure: | |
2594 | nlmsg_cancel(skb, nlh); | |
2595 | return -EMSGSIZE; | |
2596 | } | |
2597 | ||
3ff661c3 JF |
2598 | static inline size_t rtnl_fdb_nlmsg_size(void) |
2599 | { | |
2600 | return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN); | |
2601 | } | |
2602 | ||
1e53d5bb | 2603 | static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type) |
3ff661c3 JF |
2604 | { |
2605 | struct net *net = dev_net(dev); | |
2606 | struct sk_buff *skb; | |
2607 | int err = -ENOBUFS; | |
2608 | ||
2609 | skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); | |
2610 | if (!skb) | |
2611 | goto errout; | |
2612 | ||
1e53d5bb HS |
2613 | err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, |
2614 | 0, 0, type, NTF_SELF, 0); | |
3ff661c3 JF |
2615 | if (err < 0) { |
2616 | kfree_skb(skb); | |
2617 | goto errout; | |
2618 | } | |
2619 | ||
2620 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); | |
2621 | return; | |
2622 | errout: | |
2623 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); | |
2624 | } | |
2625 | ||
090096bf VY |
2626 | /** |
2627 | * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry | |
2628 | */ | |
2629 | int ndo_dflt_fdb_add(struct ndmsg *ndm, | |
2630 | struct nlattr *tb[], | |
2631 | struct net_device *dev, | |
f6f6424b | 2632 | const unsigned char *addr, u16 vid, |
090096bf VY |
2633 | u16 flags) |
2634 | { | |
2635 | int err = -EINVAL; | |
2636 | ||
2637 | /* If aging addresses are supported device will need to | |
2638 | * implement its own handler for this. | |
2639 | */ | |
2640 | if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { | |
2641 | pr_info("%s: FDB only supports static addresses\n", dev->name); | |
2642 | return err; | |
2643 | } | |
2644 | ||
65891fea OG |
2645 | if (vid) { |
2646 | pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); | |
2647 | return err; | |
2648 | } | |
2649 | ||
090096bf VY |
2650 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
2651 | err = dev_uc_add_excl(dev, addr); | |
2652 | else if (is_multicast_ether_addr(addr)) | |
2653 | err = dev_mc_add_excl(dev, addr); | |
2654 | ||
2655 | /* Only return duplicate errors if NLM_F_EXCL is set */ | |
2656 | if (err == -EEXIST && !(flags & NLM_F_EXCL)) | |
2657 | err = 0; | |
2658 | ||
2659 | return err; | |
2660 | } | |
2661 | EXPORT_SYMBOL(ndo_dflt_fdb_add); | |
2662 | ||
f6f6424b JP |
2663 | static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid) |
2664 | { | |
2665 | u16 vid = 0; | |
2666 | ||
2667 | if (vlan_attr) { | |
2668 | if (nla_len(vlan_attr) != sizeof(u16)) { | |
2669 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n"); | |
2670 | return -EINVAL; | |
2671 | } | |
2672 | ||
2673 | vid = nla_get_u16(vlan_attr); | |
2674 | ||
2675 | if (!vid || vid >= VLAN_VID_MASK) { | |
2676 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n", | |
2677 | vid); | |
2678 | return -EINVAL; | |
2679 | } | |
2680 | } | |
2681 | *p_vid = vid; | |
2682 | return 0; | |
2683 | } | |
2684 | ||
661d2967 | 2685 | static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh) |
77162022 JF |
2686 | { |
2687 | struct net *net = sock_net(skb->sk); | |
77162022 JF |
2688 | struct ndmsg *ndm; |
2689 | struct nlattr *tb[NDA_MAX+1]; | |
2690 | struct net_device *dev; | |
2691 | u8 *addr; | |
f6f6424b | 2692 | u16 vid; |
77162022 JF |
2693 | int err; |
2694 | ||
2695 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); | |
2696 | if (err < 0) | |
2697 | return err; | |
2698 | ||
2699 | ndm = nlmsg_data(nlh); | |
2700 | if (ndm->ndm_ifindex == 0) { | |
2701 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n"); | |
2702 | return -EINVAL; | |
2703 | } | |
2704 | ||
2705 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); | |
2706 | if (dev == NULL) { | |
2707 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n"); | |
2708 | return -ENODEV; | |
2709 | } | |
2710 | ||
2711 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { | |
2712 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n"); | |
2713 | return -EINVAL; | |
2714 | } | |
2715 | ||
2716 | addr = nla_data(tb[NDA_LLADDR]); | |
77162022 | 2717 | |
f6f6424b JP |
2718 | err = fdb_vid_parse(tb[NDA_VLAN], &vid); |
2719 | if (err) | |
2720 | return err; | |
2721 | ||
77162022 JF |
2722 | err = -EOPNOTSUPP; |
2723 | ||
2724 | /* Support fdb on master device the net/bridge default case */ | |
2725 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && | |
2726 | (dev->priv_flags & IFF_BRIDGE_PORT)) { | |
898e5061 JP |
2727 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
2728 | const struct net_device_ops *ops = br_dev->netdev_ops; | |
2729 | ||
f6f6424b JP |
2730 | err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, |
2731 | nlh->nlmsg_flags); | |
77162022 JF |
2732 | if (err) |
2733 | goto out; | |
2734 | else | |
2735 | ndm->ndm_flags &= ~NTF_MASTER; | |
2736 | } | |
2737 | ||
2738 | /* Embedded bridge, macvlan, and any other device support */ | |
090096bf VY |
2739 | if ((ndm->ndm_flags & NTF_SELF)) { |
2740 | if (dev->netdev_ops->ndo_fdb_add) | |
2741 | err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, | |
f6f6424b | 2742 | vid, |
090096bf VY |
2743 | nlh->nlmsg_flags); |
2744 | else | |
f6f6424b | 2745 | err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, |
090096bf | 2746 | nlh->nlmsg_flags); |
77162022 | 2747 | |
3ff661c3 | 2748 | if (!err) { |
1e53d5bb | 2749 | rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH); |
77162022 | 2750 | ndm->ndm_flags &= ~NTF_SELF; |
3ff661c3 | 2751 | } |
77162022 JF |
2752 | } |
2753 | out: | |
2754 | return err; | |
2755 | } | |
2756 | ||
090096bf VY |
2757 | /** |
2758 | * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry | |
2759 | */ | |
2760 | int ndo_dflt_fdb_del(struct ndmsg *ndm, | |
2761 | struct nlattr *tb[], | |
2762 | struct net_device *dev, | |
f6f6424b | 2763 | const unsigned char *addr, u16 vid) |
090096bf | 2764 | { |
c8a89c4a | 2765 | int err = -EINVAL; |
090096bf VY |
2766 | |
2767 | /* If aging addresses are supported device will need to | |
2768 | * implement its own handler for this. | |
2769 | */ | |
64535993 | 2770 | if (!(ndm->ndm_state & NUD_PERMANENT)) { |
090096bf | 2771 | pr_info("%s: FDB only supports static addresses\n", dev->name); |
c8a89c4a | 2772 | return err; |
090096bf VY |
2773 | } |
2774 | ||
2775 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) | |
2776 | err = dev_uc_del(dev, addr); | |
2777 | else if (is_multicast_ether_addr(addr)) | |
2778 | err = dev_mc_del(dev, addr); | |
090096bf VY |
2779 | |
2780 | return err; | |
2781 | } | |
2782 | EXPORT_SYMBOL(ndo_dflt_fdb_del); | |
2783 | ||
661d2967 | 2784 | static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh) |
77162022 JF |
2785 | { |
2786 | struct net *net = sock_net(skb->sk); | |
2787 | struct ndmsg *ndm; | |
1690be63 | 2788 | struct nlattr *tb[NDA_MAX+1]; |
77162022 JF |
2789 | struct net_device *dev; |
2790 | int err = -EINVAL; | |
2791 | __u8 *addr; | |
f6f6424b | 2792 | u16 vid; |
77162022 | 2793 | |
90f62cf3 | 2794 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
1690be63 VY |
2795 | return -EPERM; |
2796 | ||
2797 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); | |
2798 | if (err < 0) | |
2799 | return err; | |
77162022 JF |
2800 | |
2801 | ndm = nlmsg_data(nlh); | |
2802 | if (ndm->ndm_ifindex == 0) { | |
2803 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n"); | |
2804 | return -EINVAL; | |
2805 | } | |
2806 | ||
2807 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); | |
2808 | if (dev == NULL) { | |
2809 | pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n"); | |
2810 | return -ENODEV; | |
2811 | } | |
2812 | ||
1690be63 VY |
2813 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
2814 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n"); | |
2815 | return -EINVAL; | |
2816 | } | |
2817 | ||
2818 | addr = nla_data(tb[NDA_LLADDR]); | |
77162022 | 2819 | |
f6f6424b JP |
2820 | err = fdb_vid_parse(tb[NDA_VLAN], &vid); |
2821 | if (err) | |
2822 | return err; | |
2823 | ||
77162022 JF |
2824 | err = -EOPNOTSUPP; |
2825 | ||
2826 | /* Support fdb on master device the net/bridge default case */ | |
2827 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && | |
2828 | (dev->priv_flags & IFF_BRIDGE_PORT)) { | |
898e5061 JP |
2829 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
2830 | const struct net_device_ops *ops = br_dev->netdev_ops; | |
77162022 | 2831 | |
898e5061 | 2832 | if (ops->ndo_fdb_del) |
f6f6424b | 2833 | err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); |
77162022 JF |
2834 | |
2835 | if (err) | |
2836 | goto out; | |
2837 | else | |
2838 | ndm->ndm_flags &= ~NTF_MASTER; | |
2839 | } | |
2840 | ||
2841 | /* Embedded bridge, macvlan, and any other device support */ | |
090096bf VY |
2842 | if (ndm->ndm_flags & NTF_SELF) { |
2843 | if (dev->netdev_ops->ndo_fdb_del) | |
f6f6424b JP |
2844 | err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, |
2845 | vid); | |
090096bf | 2846 | else |
f6f6424b | 2847 | err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); |
77162022 | 2848 | |
3ff661c3 | 2849 | if (!err) { |
1e53d5bb | 2850 | rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH); |
77162022 | 2851 | ndm->ndm_flags &= ~NTF_SELF; |
3ff661c3 | 2852 | } |
77162022 JF |
2853 | } |
2854 | out: | |
2855 | return err; | |
2856 | } | |
2857 | ||
d83b0603 JF |
2858 | static int nlmsg_populate_fdb(struct sk_buff *skb, |
2859 | struct netlink_callback *cb, | |
2860 | struct net_device *dev, | |
2861 | int *idx, | |
2862 | struct netdev_hw_addr_list *list) | |
2863 | { | |
2864 | struct netdev_hw_addr *ha; | |
2865 | int err; | |
15e47304 | 2866 | u32 portid, seq; |
d83b0603 | 2867 | |
15e47304 | 2868 | portid = NETLINK_CB(cb->skb).portid; |
d83b0603 JF |
2869 | seq = cb->nlh->nlmsg_seq; |
2870 | ||
2871 | list_for_each_entry(ha, &list->list, list) { | |
2872 | if (*idx < cb->args[0]) | |
2873 | goto skip; | |
2874 | ||
1e53d5bb | 2875 | err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, |
a7a558fe | 2876 | portid, seq, |
1c104a6b ND |
2877 | RTM_NEWNEIGH, NTF_SELF, |
2878 | NLM_F_MULTI); | |
d83b0603 JF |
2879 | if (err < 0) |
2880 | return err; | |
2881 | skip: | |
2882 | *idx += 1; | |
2883 | } | |
2884 | return 0; | |
2885 | } | |
2886 | ||
2887 | /** | |
2c53040f | 2888 | * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. |
d83b0603 JF |
2889 | * @nlh: netlink message header |
2890 | * @dev: netdevice | |
2891 | * | |
2892 | * Default netdevice operation to dump the existing unicast address list. | |
91f3e7b1 | 2893 | * Returns number of addresses from list put in skb. |
d83b0603 JF |
2894 | */ |
2895 | int ndo_dflt_fdb_dump(struct sk_buff *skb, | |
2896 | struct netlink_callback *cb, | |
2897 | struct net_device *dev, | |
5d5eacb3 | 2898 | struct net_device *filter_dev, |
d83b0603 JF |
2899 | int idx) |
2900 | { | |
2901 | int err; | |
2902 | ||
2903 | netif_addr_lock_bh(dev); | |
2904 | err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc); | |
2905 | if (err) | |
2906 | goto out; | |
2907 | nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc); | |
2908 | out: | |
2909 | netif_addr_unlock_bh(dev); | |
2910 | return idx; | |
2911 | } | |
2912 | EXPORT_SYMBOL(ndo_dflt_fdb_dump); | |
2913 | ||
77162022 JF |
2914 | static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) |
2915 | { | |
77162022 | 2916 | struct net_device *dev; |
5e6d2435 | 2917 | struct nlattr *tb[IFLA_MAX+1]; |
5e6d2435 JHS |
2918 | struct net_device *br_dev = NULL; |
2919 | const struct net_device_ops *ops = NULL; | |
2920 | const struct net_device_ops *cops = NULL; | |
2921 | struct ifinfomsg *ifm = nlmsg_data(cb->nlh); | |
2922 | struct net *net = sock_net(skb->sk); | |
2923 | int brport_idx = 0; | |
2924 | int br_idx = 0; | |
2925 | int idx = 0; | |
2926 | ||
2927 | if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX, | |
2928 | ifla_policy) == 0) { | |
2929 | if (tb[IFLA_MASTER]) | |
2930 | br_idx = nla_get_u32(tb[IFLA_MASTER]); | |
2931 | } | |
2932 | ||
2933 | brport_idx = ifm->ifi_index; | |
2934 | ||
2935 | if (br_idx) { | |
2936 | br_dev = __dev_get_by_index(net, br_idx); | |
2937 | if (!br_dev) | |
2938 | return -ENODEV; | |
2939 | ||
2940 | ops = br_dev->netdev_ops; | |
5e6d2435 JHS |
2941 | } |
2942 | ||
2943 | for_each_netdev(net, dev) { | |
2944 | if (brport_idx && (dev->ifindex != brport_idx)) | |
2945 | continue; | |
2946 | ||
2947 | if (!br_idx) { /* user did not specify a specific bridge */ | |
2948 | if (dev->priv_flags & IFF_BRIDGE_PORT) { | |
2949 | br_dev = netdev_master_upper_dev_get(dev); | |
2950 | cops = br_dev->netdev_ops; | |
2951 | } | |
2952 | ||
5e6d2435 JHS |
2953 | } else { |
2954 | if (dev != br_dev && | |
2955 | !(dev->priv_flags & IFF_BRIDGE_PORT)) | |
2956 | continue; | |
2957 | ||
2958 | if (br_dev != netdev_master_upper_dev_get(dev) && | |
2959 | !(dev->priv_flags & IFF_EBRIDGE)) | |
2960 | continue; | |
2961 | ||
5e6d2435 JHS |
2962 | cops = ops; |
2963 | } | |
77162022 | 2964 | |
77162022 | 2965 | if (dev->priv_flags & IFF_BRIDGE_PORT) { |
5e6d2435 JHS |
2966 | if (cops && cops->ndo_fdb_dump) |
2967 | idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev, | |
2968 | idx); | |
77162022 JF |
2969 | } |
2970 | ||
2971 | if (dev->netdev_ops->ndo_fdb_dump) | |
6cb69742 | 2972 | idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL, |
5d5eacb3 | 2973 | idx); |
6cb69742 HS |
2974 | else |
2975 | idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx); | |
5e6d2435 JHS |
2976 | |
2977 | cops = NULL; | |
77162022 | 2978 | } |
77162022 JF |
2979 | |
2980 | cb->args[0] = idx; | |
2981 | return skb->len; | |
2982 | } | |
2983 | ||
2c3c031c SF |
2984 | static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, |
2985 | unsigned int attrnum, unsigned int flag) | |
2986 | { | |
2987 | if (mask & flag) | |
2988 | return nla_put_u8(skb, attrnum, !!(flags & flag)); | |
2989 | return 0; | |
2990 | } | |
2991 | ||
815cccbf | 2992 | int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
2c3c031c | 2993 | struct net_device *dev, u16 mode, |
7d4f8d87 SF |
2994 | u32 flags, u32 mask, int nlflags, |
2995 | u32 filter_mask, | |
2996 | int (*vlan_fill)(struct sk_buff *skb, | |
2997 | struct net_device *dev, | |
2998 | u32 filter_mask)) | |
815cccbf JF |
2999 | { |
3000 | struct nlmsghdr *nlh; | |
3001 | struct ifinfomsg *ifm; | |
3002 | struct nlattr *br_afspec; | |
2c3c031c | 3003 | struct nlattr *protinfo; |
815cccbf | 3004 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; |
898e5061 | 3005 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
7d4f8d87 | 3006 | int err = 0; |
815cccbf | 3007 | |
46c264da | 3008 | nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); |
815cccbf JF |
3009 | if (nlh == NULL) |
3010 | return -EMSGSIZE; | |
3011 | ||
3012 | ifm = nlmsg_data(nlh); | |
3013 | ifm->ifi_family = AF_BRIDGE; | |
3014 | ifm->__ifi_pad = 0; | |
3015 | ifm->ifi_type = dev->type; | |
3016 | ifm->ifi_index = dev->ifindex; | |
3017 | ifm->ifi_flags = dev_get_flags(dev); | |
3018 | ifm->ifi_change = 0; | |
3019 | ||
3020 | ||
3021 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || | |
3022 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || | |
3023 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || | |
898e5061 JP |
3024 | (br_dev && |
3025 | nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || | |
815cccbf JF |
3026 | (dev->addr_len && |
3027 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || | |
a54acb3a ND |
3028 | (dev->ifindex != dev_get_iflink(dev) && |
3029 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) | |
815cccbf JF |
3030 | goto nla_put_failure; |
3031 | ||
3032 | br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); | |
3033 | if (!br_afspec) | |
3034 | goto nla_put_failure; | |
3035 | ||
1d460b98 | 3036 | if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { |
815cccbf JF |
3037 | nla_nest_cancel(skb, br_afspec); |
3038 | goto nla_put_failure; | |
3039 | } | |
1d460b98 RP |
3040 | |
3041 | if (mode != BRIDGE_MODE_UNDEF) { | |
3042 | if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { | |
3043 | nla_nest_cancel(skb, br_afspec); | |
3044 | goto nla_put_failure; | |
3045 | } | |
3046 | } | |
7d4f8d87 SF |
3047 | if (vlan_fill) { |
3048 | err = vlan_fill(skb, dev, filter_mask); | |
3049 | if (err) { | |
3050 | nla_nest_cancel(skb, br_afspec); | |
3051 | goto nla_put_failure; | |
3052 | } | |
3053 | } | |
815cccbf JF |
3054 | nla_nest_end(skb, br_afspec); |
3055 | ||
2c3c031c SF |
3056 | protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); |
3057 | if (!protinfo) | |
3058 | goto nla_put_failure; | |
3059 | ||
3060 | if (brport_nla_put_flag(skb, flags, mask, | |
3061 | IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || | |
3062 | brport_nla_put_flag(skb, flags, mask, | |
3063 | IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || | |
3064 | brport_nla_put_flag(skb, flags, mask, | |
3065 | IFLA_BRPORT_FAST_LEAVE, | |
3066 | BR_MULTICAST_FAST_LEAVE) || | |
3067 | brport_nla_put_flag(skb, flags, mask, | |
3068 | IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || | |
3069 | brport_nla_put_flag(skb, flags, mask, | |
3070 | IFLA_BRPORT_LEARNING, BR_LEARNING) || | |
3071 | brport_nla_put_flag(skb, flags, mask, | |
3072 | IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || | |
3073 | brport_nla_put_flag(skb, flags, mask, | |
3074 | IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || | |
3075 | brport_nla_put_flag(skb, flags, mask, | |
3076 | IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { | |
3077 | nla_nest_cancel(skb, protinfo); | |
3078 | goto nla_put_failure; | |
3079 | } | |
3080 | ||
3081 | nla_nest_end(skb, protinfo); | |
3082 | ||
053c095a JB |
3083 | nlmsg_end(skb, nlh); |
3084 | return 0; | |
815cccbf JF |
3085 | nla_put_failure: |
3086 | nlmsg_cancel(skb, nlh); | |
7d4f8d87 | 3087 | return err ? err : -EMSGSIZE; |
815cccbf | 3088 | } |
7d4f8d87 | 3089 | EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); |
815cccbf | 3090 | |
e5a55a89 JF |
3091 | static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) |
3092 | { | |
3093 | struct net *net = sock_net(skb->sk); | |
3094 | struct net_device *dev; | |
3095 | int idx = 0; | |
3096 | u32 portid = NETLINK_CB(cb->skb).portid; | |
3097 | u32 seq = cb->nlh->nlmsg_seq; | |
6cbdceeb | 3098 | u32 filter_mask = 0; |
d64f69b0 | 3099 | int err; |
6cbdceeb | 3100 | |
aa68c20f TG |
3101 | if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { |
3102 | struct nlattr *extfilt; | |
3103 | ||
3104 | extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), | |
3105 | IFLA_EXT_MASK); | |
3106 | if (extfilt) { | |
3107 | if (nla_len(extfilt) < sizeof(filter_mask)) | |
3108 | return -EINVAL; | |
3109 | ||
3110 | filter_mask = nla_get_u32(extfilt); | |
3111 | } | |
3112 | } | |
e5a55a89 JF |
3113 | |
3114 | rcu_read_lock(); | |
3115 | for_each_netdev_rcu(net, dev) { | |
3116 | const struct net_device_ops *ops = dev->netdev_ops; | |
898e5061 | 3117 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
e5a55a89 | 3118 | |
898e5061 | 3119 | if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { |
d64f69b0 RP |
3120 | if (idx >= cb->args[0]) { |
3121 | err = br_dev->netdev_ops->ndo_bridge_getlink( | |
3122 | skb, portid, seq, dev, | |
3123 | filter_mask, NLM_F_MULTI); | |
3124 | if (err < 0 && err != -EOPNOTSUPP) | |
3125 | break; | |
3126 | } | |
25b1e679 | 3127 | idx++; |
e5a55a89 JF |
3128 | } |
3129 | ||
3130 | if (ops->ndo_bridge_getlink) { | |
d64f69b0 RP |
3131 | if (idx >= cb->args[0]) { |
3132 | err = ops->ndo_bridge_getlink(skb, portid, | |
3133 | seq, dev, | |
3134 | filter_mask, | |
3135 | NLM_F_MULTI); | |
3136 | if (err < 0 && err != -EOPNOTSUPP) | |
3137 | break; | |
3138 | } | |
25b1e679 | 3139 | idx++; |
e5a55a89 JF |
3140 | } |
3141 | } | |
3142 | rcu_read_unlock(); | |
3143 | cb->args[0] = idx; | |
3144 | ||
3145 | return skb->len; | |
3146 | } | |
3147 | ||
2469ffd7 JF |
3148 | static inline size_t bridge_nlmsg_size(void) |
3149 | { | |
3150 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
3151 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ | |
3152 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ | |
3153 | + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ | |
3154 | + nla_total_size(sizeof(u32)) /* IFLA_MTU */ | |
3155 | + nla_total_size(sizeof(u32)) /* IFLA_LINK */ | |
3156 | + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ | |
3157 | + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ | |
3158 | + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ | |
3159 | + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ | |
3160 | + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ | |
3161 | } | |
3162 | ||
02dba438 | 3163 | static int rtnl_bridge_notify(struct net_device *dev) |
2469ffd7 JF |
3164 | { |
3165 | struct net *net = dev_net(dev); | |
2469ffd7 JF |
3166 | struct sk_buff *skb; |
3167 | int err = -EOPNOTSUPP; | |
3168 | ||
02dba438 RP |
3169 | if (!dev->netdev_ops->ndo_bridge_getlink) |
3170 | return 0; | |
3171 | ||
2469ffd7 JF |
3172 | skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); |
3173 | if (!skb) { | |
3174 | err = -ENOMEM; | |
3175 | goto errout; | |
3176 | } | |
3177 | ||
46c264da | 3178 | err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); |
02dba438 RP |
3179 | if (err < 0) |
3180 | goto errout; | |
2469ffd7 | 3181 | |
59ccaaaa RP |
3182 | if (!skb->len) |
3183 | goto errout; | |
3184 | ||
2469ffd7 JF |
3185 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
3186 | return 0; | |
3187 | errout: | |
3188 | WARN_ON(err == -EMSGSIZE); | |
3189 | kfree_skb(skb); | |
59ccaaaa RP |
3190 | if (err) |
3191 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); | |
2469ffd7 JF |
3192 | return err; |
3193 | } | |
3194 | ||
661d2967 | 3195 | static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
e5a55a89 JF |
3196 | { |
3197 | struct net *net = sock_net(skb->sk); | |
3198 | struct ifinfomsg *ifm; | |
3199 | struct net_device *dev; | |
2469ffd7 JF |
3200 | struct nlattr *br_spec, *attr = NULL; |
3201 | int rem, err = -EOPNOTSUPP; | |
4de8b413 | 3202 | u16 flags = 0; |
c38e01b8 | 3203 | bool have_flags = false; |
e5a55a89 JF |
3204 | |
3205 | if (nlmsg_len(nlh) < sizeof(*ifm)) | |
3206 | return -EINVAL; | |
3207 | ||
3208 | ifm = nlmsg_data(nlh); | |
3209 | if (ifm->ifi_family != AF_BRIDGE) | |
3210 | return -EPFNOSUPPORT; | |
3211 | ||
3212 | dev = __dev_get_by_index(net, ifm->ifi_index); | |
3213 | if (!dev) { | |
3214 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); | |
3215 | return -ENODEV; | |
3216 | } | |
3217 | ||
2469ffd7 JF |
3218 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
3219 | if (br_spec) { | |
3220 | nla_for_each_nested(attr, br_spec, rem) { | |
3221 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { | |
6e8d1c55 TG |
3222 | if (nla_len(attr) < sizeof(flags)) |
3223 | return -EINVAL; | |
3224 | ||
c38e01b8 | 3225 | have_flags = true; |
2469ffd7 JF |
3226 | flags = nla_get_u16(attr); |
3227 | break; | |
3228 | } | |
3229 | } | |
3230 | } | |
3231 | ||
3232 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { | |
898e5061 JP |
3233 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
3234 | ||
3235 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { | |
2469ffd7 JF |
3236 | err = -EOPNOTSUPP; |
3237 | goto out; | |
3238 | } | |
3239 | ||
add511b3 | 3240 | err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); |
e5a55a89 JF |
3241 | if (err) |
3242 | goto out; | |
2469ffd7 JF |
3243 | |
3244 | flags &= ~BRIDGE_FLAGS_MASTER; | |
e5a55a89 JF |
3245 | } |
3246 | ||
2469ffd7 JF |
3247 | if ((flags & BRIDGE_FLAGS_SELF)) { |
3248 | if (!dev->netdev_ops->ndo_bridge_setlink) | |
3249 | err = -EOPNOTSUPP; | |
3250 | else | |
add511b3 RP |
3251 | err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, |
3252 | flags); | |
02dba438 | 3253 | if (!err) { |
2469ffd7 | 3254 | flags &= ~BRIDGE_FLAGS_SELF; |
02dba438 RP |
3255 | |
3256 | /* Generate event to notify upper layer of bridge | |
3257 | * change | |
3258 | */ | |
3259 | err = rtnl_bridge_notify(dev); | |
3260 | } | |
2469ffd7 | 3261 | } |
e5a55a89 | 3262 | |
c38e01b8 | 3263 | if (have_flags) |
2469ffd7 | 3264 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
e5a55a89 JF |
3265 | out: |
3266 | return err; | |
3267 | } | |
3268 | ||
661d2967 | 3269 | static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) |
407af329 VY |
3270 | { |
3271 | struct net *net = sock_net(skb->sk); | |
3272 | struct ifinfomsg *ifm; | |
3273 | struct net_device *dev; | |
3274 | struct nlattr *br_spec, *attr = NULL; | |
3275 | int rem, err = -EOPNOTSUPP; | |
4de8b413 | 3276 | u16 flags = 0; |
407af329 VY |
3277 | bool have_flags = false; |
3278 | ||
3279 | if (nlmsg_len(nlh) < sizeof(*ifm)) | |
3280 | return -EINVAL; | |
3281 | ||
3282 | ifm = nlmsg_data(nlh); | |
3283 | if (ifm->ifi_family != AF_BRIDGE) | |
3284 | return -EPFNOSUPPORT; | |
3285 | ||
3286 | dev = __dev_get_by_index(net, ifm->ifi_index); | |
3287 | if (!dev) { | |
3288 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); | |
3289 | return -ENODEV; | |
3290 | } | |
3291 | ||
3292 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); | |
3293 | if (br_spec) { | |
3294 | nla_for_each_nested(attr, br_spec, rem) { | |
3295 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { | |
6e8d1c55 TG |
3296 | if (nla_len(attr) < sizeof(flags)) |
3297 | return -EINVAL; | |
3298 | ||
407af329 VY |
3299 | have_flags = true; |
3300 | flags = nla_get_u16(attr); | |
3301 | break; | |
3302 | } | |
3303 | } | |
3304 | } | |
3305 | ||
407af329 VY |
3306 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
3307 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); | |
3308 | ||
3309 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { | |
3310 | err = -EOPNOTSUPP; | |
3311 | goto out; | |
3312 | } | |
3313 | ||
add511b3 | 3314 | err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); |
407af329 VY |
3315 | if (err) |
3316 | goto out; | |
3317 | ||
3318 | flags &= ~BRIDGE_FLAGS_MASTER; | |
3319 | } | |
3320 | ||
3321 | if ((flags & BRIDGE_FLAGS_SELF)) { | |
3322 | if (!dev->netdev_ops->ndo_bridge_dellink) | |
3323 | err = -EOPNOTSUPP; | |
3324 | else | |
add511b3 RP |
3325 | err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, |
3326 | flags); | |
407af329 | 3327 | |
02dba438 | 3328 | if (!err) { |
407af329 | 3329 | flags &= ~BRIDGE_FLAGS_SELF; |
02dba438 RP |
3330 | |
3331 | /* Generate event to notify upper layer of bridge | |
3332 | * change | |
3333 | */ | |
3334 | err = rtnl_bridge_notify(dev); | |
3335 | } | |
407af329 VY |
3336 | } |
3337 | ||
3338 | if (have_flags) | |
3339 | memcpy(nla_data(attr), &flags, sizeof(flags)); | |
407af329 VY |
3340 | out: |
3341 | return err; | |
3342 | } | |
3343 | ||
1da177e4 LT |
3344 | /* Process one rtnetlink message. */ |
3345 | ||
1d00a4eb | 3346 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
1da177e4 | 3347 | { |
3b1e0a65 | 3348 | struct net *net = sock_net(skb->sk); |
e2849863 | 3349 | rtnl_doit_func doit; |
1da177e4 | 3350 | int sz_idx, kind; |
1da177e4 LT |
3351 | int family; |
3352 | int type; | |
2907c35f | 3353 | int err; |
1da177e4 | 3354 | |
1da177e4 | 3355 | type = nlh->nlmsg_type; |
1da177e4 | 3356 | if (type > RTM_MAX) |
038890fe | 3357 | return -EOPNOTSUPP; |
1da177e4 LT |
3358 | |
3359 | type -= RTM_BASE; | |
3360 | ||
3361 | /* All the messages must have at least 1 byte length */ | |
573ce260 | 3362 | if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) |
1da177e4 LT |
3363 | return 0; |
3364 | ||
573ce260 | 3365 | family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; |
1da177e4 LT |
3366 | sz_idx = type>>2; |
3367 | kind = type&3; | |
3368 | ||
90f62cf3 | 3369 | if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) |
1d00a4eb | 3370 | return -EPERM; |
1da177e4 | 3371 | |
b8f3ab42 | 3372 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
97c53cac | 3373 | struct sock *rtnl; |
e2849863 | 3374 | rtnl_dumpit_func dumpit; |
c7ac8679 GR |
3375 | rtnl_calcit_func calcit; |
3376 | u16 min_dump_alloc = 0; | |
1da177e4 | 3377 | |
e2849863 TG |
3378 | dumpit = rtnl_get_dumpit(family, type); |
3379 | if (dumpit == NULL) | |
038890fe | 3380 | return -EOPNOTSUPP; |
c7ac8679 GR |
3381 | calcit = rtnl_get_calcit(family, type); |
3382 | if (calcit) | |
115c9b81 | 3383 | min_dump_alloc = calcit(skb, nlh); |
9ac4a169 | 3384 | |
2907c35f | 3385 | __rtnl_unlock(); |
97c53cac | 3386 | rtnl = net->rtnl; |
80d326fa PNA |
3387 | { |
3388 | struct netlink_dump_control c = { | |
3389 | .dump = dumpit, | |
3390 | .min_dump_alloc = min_dump_alloc, | |
3391 | }; | |
3392 | err = netlink_dump_start(rtnl, skb, nlh, &c); | |
3393 | } | |
2907c35f ED |
3394 | rtnl_lock(); |
3395 | return err; | |
1da177e4 LT |
3396 | } |
3397 | ||
e2849863 TG |
3398 | doit = rtnl_get_doit(family, type); |
3399 | if (doit == NULL) | |
038890fe | 3400 | return -EOPNOTSUPP; |
1da177e4 | 3401 | |
661d2967 | 3402 | return doit(skb, nlh); |
1da177e4 LT |
3403 | } |
3404 | ||
cd40b7d3 | 3405 | static void rtnetlink_rcv(struct sk_buff *skb) |
1da177e4 | 3406 | { |
cd40b7d3 DL |
3407 | rtnl_lock(); |
3408 | netlink_rcv_skb(skb, &rtnetlink_rcv_msg); | |
3409 | rtnl_unlock(); | |
1da177e4 LT |
3410 | } |
3411 | ||
1da177e4 LT |
3412 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
3413 | { | |
351638e7 | 3414 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
e9dc8653 | 3415 | |
1da177e4 | 3416 | switch (event) { |
1da177e4 LT |
3417 | case NETDEV_UP: |
3418 | case NETDEV_DOWN: | |
10de05af | 3419 | case NETDEV_PRE_UP: |
d90a909e EB |
3420 | case NETDEV_POST_INIT: |
3421 | case NETDEV_REGISTER: | |
1da177e4 | 3422 | case NETDEV_CHANGE: |
755d0e77 | 3423 | case NETDEV_PRE_TYPE_CHANGE: |
1da177e4 | 3424 | case NETDEV_GOING_DOWN: |
a2835763 | 3425 | case NETDEV_UNREGISTER: |
0115e8e3 | 3426 | case NETDEV_UNREGISTER_FINAL: |
ac3d3f81 AW |
3427 | case NETDEV_RELEASE: |
3428 | case NETDEV_JOIN: | |
61bd3857 | 3429 | case NETDEV_BONDING_INFO: |
1da177e4 LT |
3430 | break; |
3431 | default: | |
7f294054 | 3432 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); |
1da177e4 LT |
3433 | break; |
3434 | } | |
3435 | return NOTIFY_DONE; | |
3436 | } | |
3437 | ||
3438 | static struct notifier_block rtnetlink_dev_notifier = { | |
3439 | .notifier_call = rtnetlink_event, | |
3440 | }; | |
3441 | ||
97c53cac | 3442 | |
2c8c1e72 | 3443 | static int __net_init rtnetlink_net_init(struct net *net) |
97c53cac DL |
3444 | { |
3445 | struct sock *sk; | |
a31f2d17 PNA |
3446 | struct netlink_kernel_cfg cfg = { |
3447 | .groups = RTNLGRP_MAX, | |
3448 | .input = rtnetlink_rcv, | |
3449 | .cb_mutex = &rtnl_mutex, | |
9785e10a | 3450 | .flags = NL_CFG_F_NONROOT_RECV, |
a31f2d17 PNA |
3451 | }; |
3452 | ||
9f00d977 | 3453 | sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); |
97c53cac DL |
3454 | if (!sk) |
3455 | return -ENOMEM; | |
97c53cac DL |
3456 | net->rtnl = sk; |
3457 | return 0; | |
3458 | } | |
3459 | ||
2c8c1e72 | 3460 | static void __net_exit rtnetlink_net_exit(struct net *net) |
97c53cac | 3461 | { |
775516bf DL |
3462 | netlink_kernel_release(net->rtnl); |
3463 | net->rtnl = NULL; | |
97c53cac DL |
3464 | } |
3465 | ||
3466 | static struct pernet_operations rtnetlink_net_ops = { | |
3467 | .init = rtnetlink_net_init, | |
3468 | .exit = rtnetlink_net_exit, | |
3469 | }; | |
3470 | ||
1da177e4 LT |
3471 | void __init rtnetlink_init(void) |
3472 | { | |
97c53cac | 3473 | if (register_pernet_subsys(&rtnetlink_net_ops)) |
1da177e4 | 3474 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
97c53cac | 3475 | |
1da177e4 | 3476 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
340d17fc | 3477 | |
c7ac8679 GR |
3478 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, |
3479 | rtnl_dump_ifinfo, rtnl_calcit); | |
3480 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL); | |
3481 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL); | |
3482 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL); | |
687ad8cc | 3483 | |
c7ac8679 GR |
3484 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL); |
3485 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL); | |
77162022 JF |
3486 | |
3487 | rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL); | |
3488 | rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL); | |
3489 | rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL); | |
e5a55a89 JF |
3490 | |
3491 | rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL); | |
407af329 | 3492 | rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL); |
e5a55a89 | 3493 | rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL); |
1da177e4 | 3494 | } |