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