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