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> |
ebc08a6f | 39 | #include <linux/pci.h> |
77162022 | 40 | #include <linux/etherdevice.h> |
1da177e4 LT |
41 | |
42 | #include <asm/uaccess.h> | |
1da177e4 LT |
43 | |
44 | #include <linux/inet.h> | |
45 | #include <linux/netdevice.h> | |
46 | #include <net/ip.h> | |
47 | #include <net/protocol.h> | |
48 | #include <net/arp.h> | |
49 | #include <net/route.h> | |
50 | #include <net/udp.h> | |
51 | #include <net/sock.h> | |
52 | #include <net/pkt_sched.h> | |
14c0b97d | 53 | #include <net/fib_rules.h> |
e2849863 | 54 | #include <net/rtnetlink.h> |
30ffee84 | 55 | #include <net/net_namespace.h> |
1da177e4 | 56 | |
6fa3eb70 S |
57 | #include <linux/xlog.h> |
58 | ||
e0d087af | 59 | struct rtnl_link { |
e2849863 TG |
60 | rtnl_doit_func doit; |
61 | rtnl_dumpit_func dumpit; | |
c7ac8679 | 62 | rtnl_calcit_func calcit; |
e2849863 TG |
63 | }; |
64 | ||
6756ae4b | 65 | static DEFINE_MUTEX(rtnl_mutex); |
1da177e4 LT |
66 | |
67 | void rtnl_lock(void) | |
68 | { | |
6fa3eb70 S |
69 | #ifdef CONFIG_MTK_NET_LOGGING |
70 | printk(KERN_DEBUG "[mtk_net][rtnl_lock]rtnl_lock++\n"); | |
71 | #endif | |
6756ae4b | 72 | mutex_lock(&rtnl_mutex); |
6fa3eb70 S |
73 | #ifdef CONFIG_MTK_NET_LOGGING |
74 | printk(KERN_DEBUG "[mtk_net][rtnl_lock]rtnl_lock--\n"); | |
75 | #endif | |
1da177e4 | 76 | } |
e0d087af | 77 | EXPORT_SYMBOL(rtnl_lock); |
1da177e4 | 78 | |
6756ae4b | 79 | void __rtnl_unlock(void) |
1da177e4 | 80 | { |
6756ae4b | 81 | mutex_unlock(&rtnl_mutex); |
6fa3eb70 S |
82 | #ifdef CONFIG_MTK_NET_LOGGING |
83 | printk(KERN_DEBUG "[mtk_net][rtnl_lock]rtnl_unlock done\n"); | |
84 | #endif | |
1da177e4 | 85 | } |
6756ae4b | 86 | |
1da177e4 LT |
87 | void rtnl_unlock(void) |
88 | { | |
58ec3b4d | 89 | /* This fellow will unlock it for us. */ |
1da177e4 LT |
90 | netdev_run_todo(); |
91 | } | |
e0d087af | 92 | EXPORT_SYMBOL(rtnl_unlock); |
1da177e4 | 93 | |
6756ae4b SH |
94 | int rtnl_trylock(void) |
95 | { | |
96 | return mutex_trylock(&rtnl_mutex); | |
97 | } | |
e0d087af | 98 | EXPORT_SYMBOL(rtnl_trylock); |
6756ae4b | 99 | |
c9c1014b PM |
100 | int rtnl_is_locked(void) |
101 | { | |
102 | return mutex_is_locked(&rtnl_mutex); | |
103 | } | |
e0d087af | 104 | EXPORT_SYMBOL(rtnl_is_locked); |
c9c1014b | 105 | |
a898def2 PM |
106 | #ifdef CONFIG_PROVE_LOCKING |
107 | int lockdep_rtnl_is_held(void) | |
108 | { | |
109 | return lockdep_is_held(&rtnl_mutex); | |
110 | } | |
111 | EXPORT_SYMBOL(lockdep_rtnl_is_held); | |
112 | #endif /* #ifdef CONFIG_PROVE_LOCKING */ | |
113 | ||
25239cee | 114 | static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; |
e2849863 TG |
115 | |
116 | static inline int rtm_msgindex(int msgtype) | |
117 | { | |
118 | int msgindex = msgtype - RTM_BASE; | |
119 | ||
120 | /* | |
121 | * msgindex < 0 implies someone tried to register a netlink | |
122 | * control code. msgindex >= RTM_NR_MSGTYPES may indicate that | |
123 | * the message type has not been added to linux/rtnetlink.h | |
124 | */ | |
125 | BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); | |
126 | ||
127 | return msgindex; | |
128 | } | |
129 | ||
130 | static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) | |
131 | { | |
132 | struct rtnl_link *tab; | |
133 | ||
25239cee | 134 | if (protocol <= RTNL_FAMILY_MAX) |
0f87b1dd PM |
135 | tab = rtnl_msg_handlers[protocol]; |
136 | else | |
137 | tab = NULL; | |
138 | ||
51057f2f | 139 | if (tab == NULL || tab[msgindex].doit == NULL) |
e2849863 TG |
140 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
141 | ||
c80bbeae | 142 | return tab[msgindex].doit; |
e2849863 TG |
143 | } |
144 | ||
145 | static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) | |
146 | { | |
147 | struct rtnl_link *tab; | |
148 | ||
25239cee | 149 | if (protocol <= RTNL_FAMILY_MAX) |
0f87b1dd PM |
150 | tab = rtnl_msg_handlers[protocol]; |
151 | else | |
152 | tab = NULL; | |
153 | ||
51057f2f | 154 | if (tab == NULL || tab[msgindex].dumpit == NULL) |
e2849863 TG |
155 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
156 | ||
c80bbeae | 157 | return tab[msgindex].dumpit; |
e2849863 TG |
158 | } |
159 | ||
c7ac8679 GR |
160 | static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex) |
161 | { | |
162 | struct rtnl_link *tab; | |
163 | ||
164 | if (protocol <= RTNL_FAMILY_MAX) | |
165 | tab = rtnl_msg_handlers[protocol]; | |
166 | else | |
167 | tab = NULL; | |
168 | ||
169 | if (tab == NULL || tab[msgindex].calcit == NULL) | |
170 | tab = rtnl_msg_handlers[PF_UNSPEC]; | |
171 | ||
c80bbeae | 172 | return tab[msgindex].calcit; |
c7ac8679 GR |
173 | } |
174 | ||
e2849863 TG |
175 | /** |
176 | * __rtnl_register - Register a rtnetlink message type | |
177 | * @protocol: Protocol family or PF_UNSPEC | |
178 | * @msgtype: rtnetlink message type | |
179 | * @doit: Function pointer called for each request message | |
180 | * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message | |
c7ac8679 | 181 | * @calcit: Function pointer to calc size of dump message |
e2849863 TG |
182 | * |
183 | * Registers the specified function pointers (at least one of them has | |
184 | * to be non-NULL) to be called whenever a request message for the | |
185 | * specified protocol family and message type is received. | |
186 | * | |
187 | * The special protocol family PF_UNSPEC may be used to define fallback | |
188 | * function pointers for the case when no entry for the specific protocol | |
189 | * family exists. | |
190 | * | |
191 | * Returns 0 on success or a negative error code. | |
192 | */ | |
193 | int __rtnl_register(int protocol, int msgtype, | |
c7ac8679 GR |
194 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
195 | rtnl_calcit_func calcit) | |
e2849863 TG |
196 | { |
197 | struct rtnl_link *tab; | |
198 | int msgindex; | |
199 | ||
25239cee | 200 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
e2849863 TG |
201 | msgindex = rtm_msgindex(msgtype); |
202 | ||
203 | tab = rtnl_msg_handlers[protocol]; | |
204 | if (tab == NULL) { | |
205 | tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); | |
206 | if (tab == NULL) | |
207 | return -ENOBUFS; | |
208 | ||
209 | rtnl_msg_handlers[protocol] = tab; | |
210 | } | |
211 | ||
212 | if (doit) | |
213 | tab[msgindex].doit = doit; | |
214 | ||
215 | if (dumpit) | |
216 | tab[msgindex].dumpit = dumpit; | |
217 | ||
c7ac8679 GR |
218 | if (calcit) |
219 | tab[msgindex].calcit = calcit; | |
220 | ||
e2849863 TG |
221 | return 0; |
222 | } | |
e2849863 TG |
223 | EXPORT_SYMBOL_GPL(__rtnl_register); |
224 | ||
225 | /** | |
226 | * rtnl_register - Register a rtnetlink message type | |
227 | * | |
228 | * Identical to __rtnl_register() but panics on failure. This is useful | |
229 | * as failure of this function is very unlikely, it can only happen due | |
230 | * to lack of memory when allocating the chain to store all message | |
231 | * handlers for a protocol. Meant for use in init functions where lack | |
25985edc | 232 | * of memory implies no sense in continuing. |
e2849863 TG |
233 | */ |
234 | void rtnl_register(int protocol, int msgtype, | |
c7ac8679 GR |
235 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
236 | rtnl_calcit_func calcit) | |
e2849863 | 237 | { |
c7ac8679 | 238 | if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0) |
e2849863 TG |
239 | panic("Unable to register rtnetlink message handler, " |
240 | "protocol = %d, message type = %d\n", | |
241 | protocol, msgtype); | |
242 | } | |
e2849863 TG |
243 | EXPORT_SYMBOL_GPL(rtnl_register); |
244 | ||
245 | /** | |
246 | * rtnl_unregister - Unregister a rtnetlink message type | |
247 | * @protocol: Protocol family or PF_UNSPEC | |
248 | * @msgtype: rtnetlink message type | |
249 | * | |
250 | * Returns 0 on success or a negative error code. | |
251 | */ | |
252 | int rtnl_unregister(int protocol, int msgtype) | |
253 | { | |
254 | int msgindex; | |
255 | ||
25239cee | 256 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
e2849863 TG |
257 | msgindex = rtm_msgindex(msgtype); |
258 | ||
259 | if (rtnl_msg_handlers[protocol] == NULL) | |
260 | return -ENOENT; | |
261 | ||
262 | rtnl_msg_handlers[protocol][msgindex].doit = NULL; | |
263 | rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; | |
264 | ||
265 | return 0; | |
266 | } | |
e2849863 TG |
267 | EXPORT_SYMBOL_GPL(rtnl_unregister); |
268 | ||
269 | /** | |
270 | * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol | |
271 | * @protocol : Protocol family or PF_UNSPEC | |
272 | * | |
273 | * Identical to calling rtnl_unregster() for all registered message types | |
274 | * of a certain protocol family. | |
275 | */ | |
276 | void rtnl_unregister_all(int protocol) | |
277 | { | |
25239cee | 278 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
e2849863 TG |
279 | |
280 | kfree(rtnl_msg_handlers[protocol]); | |
281 | rtnl_msg_handlers[protocol] = NULL; | |
282 | } | |
e2849863 | 283 | EXPORT_SYMBOL_GPL(rtnl_unregister_all); |
1da177e4 | 284 | |
38f7b870 PM |
285 | static LIST_HEAD(link_ops); |
286 | ||
c63044f0 ED |
287 | static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) |
288 | { | |
289 | const struct rtnl_link_ops *ops; | |
290 | ||
291 | list_for_each_entry(ops, &link_ops, list) { | |
292 | if (!strcmp(ops->kind, kind)) | |
293 | return ops; | |
294 | } | |
295 | return NULL; | |
296 | } | |
297 | ||
38f7b870 PM |
298 | /** |
299 | * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. | |
300 | * @ops: struct rtnl_link_ops * to register | |
301 | * | |
302 | * The caller must hold the rtnl_mutex. This function should be used | |
303 | * by drivers that create devices during module initialization. It | |
304 | * must be called before registering the devices. | |
305 | * | |
306 | * Returns 0 on success or a negative error code. | |
307 | */ | |
308 | int __rtnl_link_register(struct rtnl_link_ops *ops) | |
309 | { | |
c63044f0 ED |
310 | if (rtnl_link_ops_get(ops->kind)) |
311 | return -EEXIST; | |
312 | ||
2d85cba2 | 313 | if (!ops->dellink) |
23289a37 | 314 | ops->dellink = unregister_netdevice_queue; |
2d85cba2 | 315 | |
38f7b870 PM |
316 | list_add_tail(&ops->list, &link_ops); |
317 | return 0; | |
318 | } | |
38f7b870 PM |
319 | EXPORT_SYMBOL_GPL(__rtnl_link_register); |
320 | ||
321 | /** | |
322 | * rtnl_link_register - Register rtnl_link_ops with rtnetlink. | |
323 | * @ops: struct rtnl_link_ops * to register | |
324 | * | |
325 | * Returns 0 on success or a negative error code. | |
326 | */ | |
327 | int rtnl_link_register(struct rtnl_link_ops *ops) | |
328 | { | |
329 | int err; | |
330 | ||
331 | rtnl_lock(); | |
332 | err = __rtnl_link_register(ops); | |
333 | rtnl_unlock(); | |
334 | return err; | |
335 | } | |
38f7b870 PM |
336 | EXPORT_SYMBOL_GPL(rtnl_link_register); |
337 | ||
669f87ba PE |
338 | static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) |
339 | { | |
340 | struct net_device *dev; | |
23289a37 ED |
341 | LIST_HEAD(list_kill); |
342 | ||
669f87ba | 343 | for_each_netdev(net, dev) { |
23289a37 ED |
344 | if (dev->rtnl_link_ops == ops) |
345 | ops->dellink(dev, &list_kill); | |
669f87ba | 346 | } |
23289a37 | 347 | unregister_netdevice_many(&list_kill); |
669f87ba PE |
348 | } |
349 | ||
38f7b870 PM |
350 | /** |
351 | * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. | |
352 | * @ops: struct rtnl_link_ops * to unregister | |
353 | * | |
2d85cba2 | 354 | * The caller must hold the rtnl_mutex. |
38f7b870 PM |
355 | */ |
356 | void __rtnl_link_unregister(struct rtnl_link_ops *ops) | |
357 | { | |
881d966b | 358 | struct net *net; |
2d85cba2 | 359 | |
881d966b | 360 | for_each_net(net) { |
669f87ba | 361 | __rtnl_kill_links(net, ops); |
2d85cba2 | 362 | } |
38f7b870 PM |
363 | list_del(&ops->list); |
364 | } | |
38f7b870 PM |
365 | EXPORT_SYMBOL_GPL(__rtnl_link_unregister); |
366 | ||
367 | /** | |
368 | * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. | |
369 | * @ops: struct rtnl_link_ops * to unregister | |
370 | */ | |
371 | void rtnl_link_unregister(struct rtnl_link_ops *ops) | |
372 | { | |
373 | rtnl_lock(); | |
374 | __rtnl_link_unregister(ops); | |
375 | rtnl_unlock(); | |
376 | } | |
38f7b870 PM |
377 | EXPORT_SYMBOL_GPL(rtnl_link_unregister); |
378 | ||
38f7b870 PM |
379 | static size_t rtnl_link_get_size(const struct net_device *dev) |
380 | { | |
381 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; | |
382 | size_t size; | |
383 | ||
384 | if (!ops) | |
385 | return 0; | |
386 | ||
369cf77a TG |
387 | size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ |
388 | nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ | |
38f7b870 PM |
389 | |
390 | if (ops->get_size) | |
391 | /* IFLA_INFO_DATA + nested data */ | |
369cf77a | 392 | size += nla_total_size(sizeof(struct nlattr)) + |
38f7b870 PM |
393 | ops->get_size(dev); |
394 | ||
395 | if (ops->get_xstats_size) | |
369cf77a TG |
396 | /* IFLA_INFO_XSTATS */ |
397 | size += nla_total_size(ops->get_xstats_size(dev)); | |
38f7b870 PM |
398 | |
399 | return size; | |
400 | } | |
401 | ||
f8ff182c TG |
402 | static LIST_HEAD(rtnl_af_ops); |
403 | ||
404 | static const struct rtnl_af_ops *rtnl_af_lookup(const int family) | |
405 | { | |
406 | const struct rtnl_af_ops *ops; | |
407 | ||
408 | list_for_each_entry(ops, &rtnl_af_ops, list) { | |
409 | if (ops->family == family) | |
410 | return ops; | |
411 | } | |
412 | ||
413 | return NULL; | |
414 | } | |
415 | ||
416 | /** | |
417 | * __rtnl_af_register - Register rtnl_af_ops with rtnetlink. | |
418 | * @ops: struct rtnl_af_ops * to register | |
419 | * | |
420 | * The caller must hold the rtnl_mutex. | |
421 | * | |
422 | * Returns 0 on success or a negative error code. | |
423 | */ | |
424 | int __rtnl_af_register(struct rtnl_af_ops *ops) | |
425 | { | |
426 | list_add_tail(&ops->list, &rtnl_af_ops); | |
427 | return 0; | |
428 | } | |
429 | EXPORT_SYMBOL_GPL(__rtnl_af_register); | |
430 | ||
431 | /** | |
432 | * rtnl_af_register - Register rtnl_af_ops with rtnetlink. | |
433 | * @ops: struct rtnl_af_ops * to register | |
434 | * | |
435 | * Returns 0 on success or a negative error code. | |
436 | */ | |
437 | int rtnl_af_register(struct rtnl_af_ops *ops) | |
438 | { | |
439 | int err; | |
440 | ||
441 | rtnl_lock(); | |
442 | err = __rtnl_af_register(ops); | |
443 | rtnl_unlock(); | |
444 | return err; | |
445 | } | |
446 | EXPORT_SYMBOL_GPL(rtnl_af_register); | |
447 | ||
448 | /** | |
449 | * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. | |
450 | * @ops: struct rtnl_af_ops * to unregister | |
451 | * | |
452 | * The caller must hold the rtnl_mutex. | |
453 | */ | |
454 | void __rtnl_af_unregister(struct rtnl_af_ops *ops) | |
455 | { | |
456 | list_del(&ops->list); | |
457 | } | |
458 | EXPORT_SYMBOL_GPL(__rtnl_af_unregister); | |
459 | ||
460 | /** | |
461 | * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. | |
462 | * @ops: struct rtnl_af_ops * to unregister | |
463 | */ | |
464 | void rtnl_af_unregister(struct rtnl_af_ops *ops) | |
465 | { | |
466 | rtnl_lock(); | |
467 | __rtnl_af_unregister(ops); | |
468 | rtnl_unlock(); | |
469 | } | |
470 | EXPORT_SYMBOL_GPL(rtnl_af_unregister); | |
471 | ||
472 | static size_t rtnl_link_get_af_size(const struct net_device *dev) | |
473 | { | |
474 | struct rtnl_af_ops *af_ops; | |
475 | size_t size; | |
476 | ||
477 | /* IFLA_AF_SPEC */ | |
478 | size = nla_total_size(sizeof(struct nlattr)); | |
479 | ||
480 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { | |
481 | if (af_ops->get_link_af_size) { | |
482 | /* AF_* + nested data */ | |
483 | size += nla_total_size(sizeof(struct nlattr)) + | |
484 | af_ops->get_link_af_size(dev); | |
485 | } | |
486 | } | |
487 | ||
488 | return size; | |
489 | } | |
490 | ||
38f7b870 PM |
491 | static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) |
492 | { | |
493 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; | |
494 | struct nlattr *linkinfo, *data; | |
495 | int err = -EMSGSIZE; | |
496 | ||
497 | linkinfo = nla_nest_start(skb, IFLA_LINKINFO); | |
498 | if (linkinfo == NULL) | |
499 | goto out; | |
500 | ||
501 | if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) | |
502 | goto err_cancel_link; | |
503 | if (ops->fill_xstats) { | |
504 | err = ops->fill_xstats(skb, dev); | |
505 | if (err < 0) | |
506 | goto err_cancel_link; | |
507 | } | |
508 | if (ops->fill_info) { | |
509 | data = nla_nest_start(skb, IFLA_INFO_DATA); | |
fcca143d WY |
510 | if (data == NULL) { |
511 | err = -EMSGSIZE; | |
38f7b870 | 512 | goto err_cancel_link; |
fcca143d | 513 | } |
38f7b870 PM |
514 | err = ops->fill_info(skb, dev); |
515 | if (err < 0) | |
516 | goto err_cancel_data; | |
517 | nla_nest_end(skb, data); | |
518 | } | |
519 | ||
520 | nla_nest_end(skb, linkinfo); | |
521 | return 0; | |
522 | ||
523 | err_cancel_data: | |
524 | nla_nest_cancel(skb, data); | |
525 | err_cancel_link: | |
526 | nla_nest_cancel(skb, linkinfo); | |
527 | out: | |
528 | return err; | |
529 | } | |
530 | ||
95c96174 | 531 | int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) |
1da177e4 | 532 | { |
97c53cac | 533 | struct sock *rtnl = net->rtnl; |
1da177e4 LT |
534 | int err = 0; |
535 | ||
ac6d439d | 536 | NETLINK_CB(skb).dst_group = group; |
1da177e4 LT |
537 | if (echo) |
538 | atomic_inc(&skb->users); | |
539 | netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); | |
540 | if (echo) | |
541 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); | |
542 | return err; | |
543 | } | |
544 | ||
97c53cac | 545 | int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) |
2942e900 | 546 | { |
97c53cac DL |
547 | struct sock *rtnl = net->rtnl; |
548 | ||
2942e900 TG |
549 | return nlmsg_unicast(rtnl, skb, pid); |
550 | } | |
e0d087af | 551 | EXPORT_SYMBOL(rtnl_unicast); |
2942e900 | 552 | |
1ce85fe4 PNA |
553 | void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, |
554 | struct nlmsghdr *nlh, gfp_t flags) | |
97676b6b | 555 | { |
97c53cac | 556 | struct sock *rtnl = net->rtnl; |
97676b6b TG |
557 | int report = 0; |
558 | ||
559 | if (nlh) | |
560 | report = nlmsg_report(nlh); | |
561 | ||
1ce85fe4 | 562 | nlmsg_notify(rtnl, skb, pid, group, report, flags); |
97676b6b | 563 | } |
e0d087af | 564 | EXPORT_SYMBOL(rtnl_notify); |
97676b6b | 565 | |
97c53cac | 566 | void rtnl_set_sk_err(struct net *net, u32 group, int error) |
97676b6b | 567 | { |
97c53cac DL |
568 | struct sock *rtnl = net->rtnl; |
569 | ||
97676b6b TG |
570 | netlink_set_err(rtnl, 0, group, error); |
571 | } | |
e0d087af | 572 | EXPORT_SYMBOL(rtnl_set_sk_err); |
97676b6b | 573 | |
1da177e4 LT |
574 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) |
575 | { | |
2d7202bf TG |
576 | struct nlattr *mx; |
577 | int i, valid = 0; | |
578 | ||
579 | mx = nla_nest_start(skb, RTA_METRICS); | |
580 | if (mx == NULL) | |
581 | return -ENOBUFS; | |
582 | ||
583 | for (i = 0; i < RTAX_MAX; i++) { | |
584 | if (metrics[i]) { | |
585 | valid++; | |
a6574349 DM |
586 | if (nla_put_u32(skb, i+1, metrics[i])) |
587 | goto nla_put_failure; | |
2d7202bf | 588 | } |
1da177e4 | 589 | } |
1da177e4 | 590 | |
a57d27fc DM |
591 | if (!valid) { |
592 | nla_nest_cancel(skb, mx); | |
593 | return 0; | |
594 | } | |
2d7202bf TG |
595 | |
596 | return nla_nest_end(skb, mx); | |
597 | ||
598 | nla_put_failure: | |
bc3ed28c TG |
599 | nla_nest_cancel(skb, mx); |
600 | return -EMSGSIZE; | |
1da177e4 | 601 | } |
e0d087af | 602 | EXPORT_SYMBOL(rtnetlink_put_metrics); |
1da177e4 | 603 | |
e3703b3d | 604 | int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, |
87a50699 | 605 | long expires, u32 error) |
e3703b3d TG |
606 | { |
607 | struct rta_cacheinfo ci = { | |
a399a805 | 608 | .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse), |
e3703b3d TG |
609 | .rta_used = dst->__use, |
610 | .rta_clntref = atomic_read(&(dst->__refcnt)), | |
611 | .rta_error = error, | |
612 | .rta_id = id, | |
e3703b3d TG |
613 | }; |
614 | ||
8253947e LW |
615 | if (expires) { |
616 | unsigned long clock; | |
e3703b3d | 617 | |
8253947e LW |
618 | clock = jiffies_to_clock_t(abs(expires)); |
619 | clock = min_t(unsigned long, clock, INT_MAX); | |
620 | ci.rta_expires = (expires > 0) ? clock : -clock; | |
621 | } | |
e3703b3d TG |
622 | return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); |
623 | } | |
e3703b3d | 624 | EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); |
1da177e4 | 625 | |
93b2d4a2 | 626 | static void set_operstate(struct net_device *dev, unsigned char transition) |
b00055aa SR |
627 | { |
628 | unsigned char operstate = dev->operstate; | |
629 | ||
e0d087af | 630 | switch (transition) { |
b00055aa SR |
631 | case IF_OPER_UP: |
632 | if ((operstate == IF_OPER_DORMANT || | |
633 | operstate == IF_OPER_UNKNOWN) && | |
634 | !netif_dormant(dev)) | |
635 | operstate = IF_OPER_UP; | |
636 | break; | |
637 | ||
638 | case IF_OPER_DORMANT: | |
639 | if (operstate == IF_OPER_UP || | |
640 | operstate == IF_OPER_UNKNOWN) | |
641 | operstate = IF_OPER_DORMANT; | |
642 | break; | |
3ff50b79 | 643 | } |
b00055aa SR |
644 | |
645 | if (dev->operstate != operstate) { | |
646 | write_lock_bh(&dev_base_lock); | |
647 | dev->operstate = operstate; | |
648 | write_unlock_bh(&dev_base_lock); | |
93b2d4a2 DM |
649 | netdev_state_change(dev); |
650 | } | |
b00055aa SR |
651 | } |
652 | ||
b1beb681 JB |
653 | static unsigned int rtnl_dev_get_flags(const struct net_device *dev) |
654 | { | |
655 | return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | | |
656 | (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); | |
657 | } | |
658 | ||
3729d502 PM |
659 | static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, |
660 | const struct ifinfomsg *ifm) | |
661 | { | |
662 | unsigned int flags = ifm->ifi_flags; | |
663 | ||
664 | /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ | |
665 | if (ifm->ifi_change) | |
666 | flags = (flags & ifm->ifi_change) | | |
b1beb681 | 667 | (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); |
3729d502 PM |
668 | |
669 | return flags; | |
670 | } | |
671 | ||
b60c5115 | 672 | static void copy_rtnl_link_stats(struct rtnl_link_stats *a, |
be1f3c2c | 673 | const struct rtnl_link_stats64 *b) |
1da177e4 | 674 | { |
b60c5115 TG |
675 | a->rx_packets = b->rx_packets; |
676 | a->tx_packets = b->tx_packets; | |
677 | a->rx_bytes = b->rx_bytes; | |
678 | a->tx_bytes = b->tx_bytes; | |
679 | a->rx_errors = b->rx_errors; | |
680 | a->tx_errors = b->tx_errors; | |
681 | a->rx_dropped = b->rx_dropped; | |
682 | a->tx_dropped = b->tx_dropped; | |
683 | ||
684 | a->multicast = b->multicast; | |
685 | a->collisions = b->collisions; | |
686 | ||
687 | a->rx_length_errors = b->rx_length_errors; | |
688 | a->rx_over_errors = b->rx_over_errors; | |
689 | a->rx_crc_errors = b->rx_crc_errors; | |
690 | a->rx_frame_errors = b->rx_frame_errors; | |
691 | a->rx_fifo_errors = b->rx_fifo_errors; | |
692 | a->rx_missed_errors = b->rx_missed_errors; | |
693 | ||
694 | a->tx_aborted_errors = b->tx_aborted_errors; | |
695 | a->tx_carrier_errors = b->tx_carrier_errors; | |
696 | a->tx_fifo_errors = b->tx_fifo_errors; | |
697 | a->tx_heartbeat_errors = b->tx_heartbeat_errors; | |
698 | a->tx_window_errors = b->tx_window_errors; | |
699 | ||
700 | a->rx_compressed = b->rx_compressed; | |
701 | a->tx_compressed = b->tx_compressed; | |
10708f37 JE |
702 | } |
703 | ||
be1f3c2c | 704 | static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b) |
10708f37 | 705 | { |
afdcba37 | 706 | memcpy(v, b, sizeof(*b)); |
10708f37 | 707 | } |
1da177e4 | 708 | |
c02db8c6 | 709 | /* All VF info */ |
115c9b81 GR |
710 | static inline int rtnl_vfinfo_size(const struct net_device *dev, |
711 | u32 ext_filter_mask) | |
ebc08a6f | 712 | { |
115c9b81 GR |
713 | if (dev->dev.parent && dev_is_pci(dev->dev.parent) && |
714 | (ext_filter_mask & RTEXT_FILTER_VF)) { | |
c02db8c6 | 715 | int num_vfs = dev_num_vf(dev->dev.parent); |
045de01a SF |
716 | size_t size = nla_total_size(sizeof(struct nlattr)); |
717 | size += nla_total_size(num_vfs * sizeof(struct nlattr)); | |
718 | size += num_vfs * | |
719 | (nla_total_size(sizeof(struct ifla_vf_mac)) + | |
720 | nla_total_size(sizeof(struct ifla_vf_vlan)) + | |
5f8444a3 GR |
721 | nla_total_size(sizeof(struct ifla_vf_tx_rate)) + |
722 | nla_total_size(sizeof(struct ifla_vf_spoofchk))); | |
c02db8c6 CW |
723 | return size; |
724 | } else | |
ebc08a6f WM |
725 | return 0; |
726 | } | |
727 | ||
fd9fcd8a DG |
728 | static size_t rtnl_port_size(const struct net_device *dev, |
729 | u32 ext_filter_mask) | |
57b61080 SF |
730 | { |
731 | size_t port_size = nla_total_size(4) /* PORT_VF */ | |
732 | + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ | |
733 | + nla_total_size(sizeof(struct ifla_port_vsi)) | |
734 | /* PORT_VSI_TYPE */ | |
735 | + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ | |
736 | + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ | |
737 | + nla_total_size(1) /* PROT_VDP_REQUEST */ | |
738 | + nla_total_size(2); /* PORT_VDP_RESPONSE */ | |
739 | size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); | |
740 | size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) | |
741 | + port_size; | |
742 | size_t port_self_size = nla_total_size(sizeof(struct nlattr)) | |
743 | + port_size; | |
744 | ||
fd9fcd8a DG |
745 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
746 | !(ext_filter_mask & RTEXT_FILTER_VF)) | |
57b61080 SF |
747 | return 0; |
748 | if (dev_num_vf(dev->dev.parent)) | |
749 | return port_self_size + vf_ports_size + | |
750 | vf_port_size * dev_num_vf(dev->dev.parent); | |
751 | else | |
752 | return port_self_size; | |
753 | } | |
754 | ||
115c9b81 GR |
755 | static noinline size_t if_nlmsg_size(const struct net_device *dev, |
756 | u32 ext_filter_mask) | |
339bf98f TG |
757 | { |
758 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
759 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ | |
0b815a1a | 760 | + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ |
339bf98f TG |
761 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ |
762 | + nla_total_size(sizeof(struct rtnl_link_ifmap)) | |
763 | + nla_total_size(sizeof(struct rtnl_link_stats)) | |
adcfe196 | 764 | + nla_total_size(sizeof(struct rtnl_link_stats64)) |
339bf98f TG |
765 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
766 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ | |
767 | + nla_total_size(4) /* IFLA_TXQLEN */ | |
768 | + nla_total_size(4) /* IFLA_WEIGHT */ | |
769 | + nla_total_size(4) /* IFLA_MTU */ | |
770 | + nla_total_size(4) /* IFLA_LINK */ | |
771 | + nla_total_size(4) /* IFLA_MASTER */ | |
9a57247f | 772 | + nla_total_size(1) /* IFLA_CARRIER */ |
edbc0bb3 | 773 | + nla_total_size(4) /* IFLA_PROMISCUITY */ |
76ff5cc9 JP |
774 | + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ |
775 | + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ | |
339bf98f | 776 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
38f7b870 | 777 | + nla_total_size(1) /* IFLA_LINKMODE */ |
115c9b81 GR |
778 | + nla_total_size(ext_filter_mask |
779 | & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ | |
780 | + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ | |
fd9fcd8a | 781 | + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ |
f8ff182c TG |
782 | + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ |
783 | + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */ | |
339bf98f TG |
784 | } |
785 | ||
57b61080 SF |
786 | static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) |
787 | { | |
788 | struct nlattr *vf_ports; | |
789 | struct nlattr *vf_port; | |
790 | int vf; | |
791 | int err; | |
792 | ||
793 | vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); | |
794 | if (!vf_ports) | |
795 | return -EMSGSIZE; | |
796 | ||
797 | for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { | |
798 | vf_port = nla_nest_start(skb, IFLA_VF_PORT); | |
8ca94183 SF |
799 | if (!vf_port) |
800 | goto nla_put_failure; | |
a6574349 DM |
801 | if (nla_put_u32(skb, IFLA_PORT_VF, vf)) |
802 | goto nla_put_failure; | |
57b61080 | 803 | err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); |
8ca94183 SF |
804 | if (err == -EMSGSIZE) |
805 | goto nla_put_failure; | |
57b61080 | 806 | if (err) { |
57b61080 SF |
807 | nla_nest_cancel(skb, vf_port); |
808 | continue; | |
809 | } | |
810 | nla_nest_end(skb, vf_port); | |
811 | } | |
812 | ||
813 | nla_nest_end(skb, vf_ports); | |
814 | ||
815 | return 0; | |
8ca94183 SF |
816 | |
817 | nla_put_failure: | |
818 | nla_nest_cancel(skb, vf_ports); | |
819 | return -EMSGSIZE; | |
57b61080 SF |
820 | } |
821 | ||
822 | static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) | |
823 | { | |
824 | struct nlattr *port_self; | |
825 | int err; | |
826 | ||
827 | port_self = nla_nest_start(skb, IFLA_PORT_SELF); | |
828 | if (!port_self) | |
829 | return -EMSGSIZE; | |
830 | ||
831 | err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); | |
832 | if (err) { | |
833 | nla_nest_cancel(skb, port_self); | |
8ca94183 | 834 | return (err == -EMSGSIZE) ? err : 0; |
57b61080 SF |
835 | } |
836 | ||
837 | nla_nest_end(skb, port_self); | |
838 | ||
839 | return 0; | |
840 | } | |
841 | ||
fd9fcd8a DG |
842 | static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, |
843 | u32 ext_filter_mask) | |
57b61080 SF |
844 | { |
845 | int err; | |
846 | ||
fd9fcd8a DG |
847 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
848 | !(ext_filter_mask & RTEXT_FILTER_VF)) | |
57b61080 SF |
849 | return 0; |
850 | ||
851 | err = rtnl_port_self_fill(skb, dev); | |
852 | if (err) | |
853 | return err; | |
854 | ||
855 | if (dev_num_vf(dev->dev.parent)) { | |
856 | err = rtnl_vf_ports_fill(skb, dev); | |
857 | if (err) | |
858 | return err; | |
859 | } | |
860 | ||
861 | return 0; | |
862 | } | |
863 | ||
b60c5115 | 864 | static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, |
575c3e2a | 865 | int type, u32 pid, u32 seq, u32 change, |
115c9b81 | 866 | unsigned int flags, u32 ext_filter_mask) |
b60c5115 TG |
867 | { |
868 | struct ifinfomsg *ifm; | |
869 | struct nlmsghdr *nlh; | |
28172739 | 870 | struct rtnl_link_stats64 temp; |
be1f3c2c | 871 | const struct rtnl_link_stats64 *stats; |
f8ff182c TG |
872 | struct nlattr *attr, *af_spec; |
873 | struct rtnl_af_ops *af_ops; | |
898e5061 | 874 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
1da177e4 | 875 | |
2907c35f | 876 | ASSERT_RTNL(); |
b60c5115 TG |
877 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
878 | if (nlh == NULL) | |
26932566 | 879 | return -EMSGSIZE; |
1da177e4 | 880 | |
b60c5115 TG |
881 | ifm = nlmsg_data(nlh); |
882 | ifm->ifi_family = AF_UNSPEC; | |
883 | ifm->__ifi_pad = 0; | |
884 | ifm->ifi_type = dev->type; | |
885 | ifm->ifi_index = dev->ifindex; | |
886 | ifm->ifi_flags = dev_get_flags(dev); | |
887 | ifm->ifi_change = change; | |
888 | ||
a6574349 DM |
889 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
890 | nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || | |
891 | nla_put_u8(skb, IFLA_OPERSTATE, | |
892 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || | |
893 | nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || | |
894 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || | |
895 | nla_put_u32(skb, IFLA_GROUP, dev->group) || | |
edbc0bb3 | 896 | nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || |
76ff5cc9 | 897 | nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || |
1d69c2b3 | 898 | #ifdef CONFIG_RPS |
76ff5cc9 | 899 | nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || |
1d69c2b3 | 900 | #endif |
a6574349 DM |
901 | (dev->ifindex != dev->iflink && |
902 | nla_put_u32(skb, IFLA_LINK, dev->iflink)) || | |
898e5061 JP |
903 | (upper_dev && |
904 | nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) || | |
9a57247f | 905 | nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || |
a6574349 DM |
906 | (dev->qdisc && |
907 | nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || | |
908 | (dev->ifalias && | |
909 | nla_put_string(skb, IFLA_IFALIAS, dev->ifalias))) | |
910 | goto nla_put_failure; | |
0b815a1a | 911 | |
1da177e4 | 912 | if (1) { |
f9d69142 KL |
913 | struct rtnl_link_ifmap map; |
914 | ||
915 | memset(&map, 0, sizeof(map)); | |
916 | map.mem_start = dev->mem_start; | |
917 | map.mem_end = dev->mem_end; | |
918 | map.base_addr = dev->base_addr; | |
919 | map.irq = dev->irq; | |
920 | map.dma = dev->dma; | |
921 | map.port = dev->if_port; | |
922 | ||
a6574349 DM |
923 | if (nla_put(skb, IFLA_MAP, sizeof(map), &map)) |
924 | goto nla_put_failure; | |
1da177e4 LT |
925 | } |
926 | ||
927 | if (dev->addr_len) { | |
a6574349 DM |
928 | if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || |
929 | nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) | |
930 | goto nla_put_failure; | |
1da177e4 LT |
931 | } |
932 | ||
96e74088 PE |
933 | attr = nla_reserve(skb, IFLA_STATS, |
934 | sizeof(struct rtnl_link_stats)); | |
935 | if (attr == NULL) | |
936 | goto nla_put_failure; | |
b60c5115 | 937 | |
28172739 | 938 | stats = dev_get_stats(dev, &temp); |
96e74088 | 939 | copy_rtnl_link_stats(nla_data(attr), stats); |
1da177e4 | 940 | |
10708f37 JE |
941 | attr = nla_reserve(skb, IFLA_STATS64, |
942 | sizeof(struct rtnl_link_stats64)); | |
943 | if (attr == NULL) | |
944 | goto nla_put_failure; | |
10708f37 JE |
945 | copy_rtnl_link_stats64(nla_data(attr), stats); |
946 | ||
a6574349 DM |
947 | if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) && |
948 | nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent))) | |
949 | goto nla_put_failure; | |
57b61080 | 950 | |
115c9b81 GR |
951 | if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent |
952 | && (ext_filter_mask & RTEXT_FILTER_VF)) { | |
ebc08a6f | 953 | int i; |
ebc08a6f | 954 | |
c02db8c6 CW |
955 | struct nlattr *vfinfo, *vf; |
956 | int num_vfs = dev_num_vf(dev->dev.parent); | |
957 | ||
c02db8c6 CW |
958 | vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); |
959 | if (!vfinfo) | |
960 | goto nla_put_failure; | |
961 | for (i = 0; i < num_vfs; i++) { | |
962 | struct ifla_vf_info ivi; | |
963 | struct ifla_vf_mac vf_mac; | |
964 | struct ifla_vf_vlan vf_vlan; | |
965 | struct ifla_vf_tx_rate vf_tx_rate; | |
5f8444a3 GR |
966 | struct ifla_vf_spoofchk vf_spoofchk; |
967 | ||
968 | /* | |
969 | * Not all SR-IOV capable drivers support the | |
970 | * spoofcheck query. Preset to -1 so the user | |
971 | * space tool can detect that the driver didn't | |
972 | * report anything. | |
973 | */ | |
974 | ivi.spoofchk = -1; | |
84d73cd3 | 975 | memset(ivi.mac, 0, sizeof(ivi.mac)); |
ebc08a6f WM |
976 | if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi)) |
977 | break; | |
5f8444a3 GR |
978 | vf_mac.vf = |
979 | vf_vlan.vf = | |
980 | vf_tx_rate.vf = | |
981 | vf_spoofchk.vf = ivi.vf; | |
982 | ||
c02db8c6 CW |
983 | memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); |
984 | vf_vlan.vlan = ivi.vlan; | |
985 | vf_vlan.qos = ivi.qos; | |
986 | vf_tx_rate.rate = ivi.tx_rate; | |
5f8444a3 | 987 | vf_spoofchk.setting = ivi.spoofchk; |
c02db8c6 CW |
988 | vf = nla_nest_start(skb, IFLA_VF_INFO); |
989 | if (!vf) { | |
990 | nla_nest_cancel(skb, vfinfo); | |
991 | goto nla_put_failure; | |
992 | } | |
a6574349 DM |
993 | if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || |
994 | nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || | |
995 | nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), | |
996 | &vf_tx_rate) || | |
997 | nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), | |
998 | &vf_spoofchk)) | |
999 | goto nla_put_failure; | |
c02db8c6 | 1000 | nla_nest_end(skb, vf); |
ebc08a6f | 1001 | } |
c02db8c6 | 1002 | nla_nest_end(skb, vfinfo); |
ebc08a6f | 1003 | } |
57b61080 | 1004 | |
fd9fcd8a | 1005 | if (rtnl_port_fill(skb, dev, ext_filter_mask)) |
57b61080 SF |
1006 | goto nla_put_failure; |
1007 | ||
38f7b870 PM |
1008 | if (dev->rtnl_link_ops) { |
1009 | if (rtnl_link_fill(skb, dev) < 0) | |
1010 | goto nla_put_failure; | |
1011 | } | |
1012 | ||
f8ff182c TG |
1013 | if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) |
1014 | goto nla_put_failure; | |
1015 | ||
1016 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { | |
1017 | if (af_ops->fill_link_af) { | |
1018 | struct nlattr *af; | |
1019 | int err; | |
1020 | ||
1021 | if (!(af = nla_nest_start(skb, af_ops->family))) | |
1022 | goto nla_put_failure; | |
1023 | ||
1024 | err = af_ops->fill_link_af(skb, dev); | |
1025 | ||
1026 | /* | |
1027 | * Caller may return ENODATA to indicate that there | |
1028 | * was no data to be dumped. This is not an error, it | |
1029 | * means we should trim the attribute header and | |
1030 | * continue. | |
1031 | */ | |
1032 | if (err == -ENODATA) | |
1033 | nla_nest_cancel(skb, af); | |
1034 | else if (err < 0) | |
1035 | goto nla_put_failure; | |
1036 | ||
1037 | nla_nest_end(skb, af); | |
1038 | } | |
1039 | } | |
1040 | ||
1041 | nla_nest_end(skb, af_spec); | |
1042 | ||
b60c5115 TG |
1043 | return nlmsg_end(skb, nlh); |
1044 | ||
1045 | nla_put_failure: | |
26932566 PM |
1046 | nlmsg_cancel(skb, nlh); |
1047 | return -EMSGSIZE; | |
1da177e4 LT |
1048 | } |
1049 | ||
b60c5115 | 1050 | static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) |
1da177e4 | 1051 | { |
3b1e0a65 | 1052 | struct net *net = sock_net(skb->sk); |
7c28bd0b ED |
1053 | int h, s_h; |
1054 | int idx = 0, s_idx; | |
1da177e4 | 1055 | struct net_device *dev; |
7c28bd0b | 1056 | struct hlist_head *head; |
115c9b81 GR |
1057 | struct nlattr *tb[IFLA_MAX+1]; |
1058 | u32 ext_filter_mask = 0; | |
1ab27dd7 | 1059 | int err; |
fb0d10bf | 1060 | int hdrlen; |
7c28bd0b ED |
1061 | |
1062 | s_h = cb->args[0]; | |
1063 | s_idx = cb->args[1]; | |
1064 | ||
e67f88dd | 1065 | rcu_read_lock(); |
4e985ada TG |
1066 | cb->seq = net->dev_base_seq; |
1067 | ||
fb0d10bf MS |
1068 | /* A hack to preserve kernel<->userspace interface. |
1069 | * The correct header is ifinfomsg. It is consistent with rtnl_getlink. | |
1070 | * However, before Linux v3.9 the code here assumed rtgenmsg and that's | |
1071 | * what iproute2 < v3.9.0 used. | |
1072 | * We can detect the old iproute2. Even including the IFLA_EXT_MASK | |
1073 | * attribute, its netlink message is shorter than struct ifinfomsg. | |
1074 | */ | |
1075 | hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? | |
1076 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); | |
1077 | ||
1078 | if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { | |
115c9b81 | 1079 | |
a4b64fbe ED |
1080 | if (tb[IFLA_EXT_MASK]) |
1081 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); | |
1082 | } | |
115c9b81 | 1083 | |
7c28bd0b ED |
1084 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
1085 | idx = 0; | |
1086 | head = &net->dev_index_head[h]; | |
b67bfe0d | 1087 | hlist_for_each_entry_rcu(dev, head, index_hlist) { |
7c28bd0b ED |
1088 | if (idx < s_idx) |
1089 | goto cont; | |
1ab27dd7 DG |
1090 | err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, |
1091 | NETLINK_CB(cb->skb).portid, | |
1092 | cb->nlh->nlmsg_seq, 0, | |
1093 | NLM_F_MULTI, | |
1094 | ext_filter_mask); | |
1095 | /* If we ran out of room on the first message, | |
1096 | * we're in trouble | |
1097 | */ | |
1098 | WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); | |
1099 | ||
1100 | if (err <= 0) | |
7c28bd0b | 1101 | goto out; |
4e985ada TG |
1102 | |
1103 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
7562f876 | 1104 | cont: |
7c28bd0b ED |
1105 | idx++; |
1106 | } | |
1da177e4 | 1107 | } |
7c28bd0b | 1108 | out: |
e67f88dd | 1109 | rcu_read_unlock(); |
7c28bd0b ED |
1110 | cb->args[1] = idx; |
1111 | cb->args[0] = h; | |
1da177e4 LT |
1112 | |
1113 | return skb->len; | |
1114 | } | |
1115 | ||
e7199288 | 1116 | const struct nla_policy ifla_policy[IFLA_MAX+1] = { |
5176f91e | 1117 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
38f7b870 PM |
1118 | [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
1119 | [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, | |
5176f91e | 1120 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
da5e0494 | 1121 | [IFLA_MTU] = { .type = NLA_U32 }, |
76e87306 | 1122 | [IFLA_LINK] = { .type = NLA_U32 }, |
fbaec0ea | 1123 | [IFLA_MASTER] = { .type = NLA_U32 }, |
9a57247f | 1124 | [IFLA_CARRIER] = { .type = NLA_U8 }, |
da5e0494 TG |
1125 | [IFLA_TXQLEN] = { .type = NLA_U32 }, |
1126 | [IFLA_WEIGHT] = { .type = NLA_U32 }, | |
1127 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, | |
1128 | [IFLA_LINKMODE] = { .type = NLA_U8 }, | |
76e87306 | 1129 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, |
d8a5ec67 | 1130 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
f0630529 | 1131 | [IFLA_NET_NS_FD] = { .type = NLA_U32 }, |
0b815a1a | 1132 | [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, |
c02db8c6 | 1133 | [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, |
57b61080 SF |
1134 | [IFLA_VF_PORTS] = { .type = NLA_NESTED }, |
1135 | [IFLA_PORT_SELF] = { .type = NLA_NESTED }, | |
f8ff182c | 1136 | [IFLA_AF_SPEC] = { .type = NLA_NESTED }, |
115c9b81 | 1137 | [IFLA_EXT_MASK] = { .type = NLA_U32 }, |
edbc0bb3 | 1138 | [IFLA_PROMISCUITY] = { .type = NLA_U32 }, |
76ff5cc9 JP |
1139 | [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, |
1140 | [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, | |
da5e0494 | 1141 | }; |
e0d087af | 1142 | EXPORT_SYMBOL(ifla_policy); |
da5e0494 | 1143 | |
38f7b870 PM |
1144 | static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { |
1145 | [IFLA_INFO_KIND] = { .type = NLA_STRING }, | |
1146 | [IFLA_INFO_DATA] = { .type = NLA_NESTED }, | |
1147 | }; | |
1148 | ||
c02db8c6 CW |
1149 | static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = { |
1150 | [IFLA_VF_INFO] = { .type = NLA_NESTED }, | |
1151 | }; | |
1152 | ||
1153 | static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { | |
1671763b DB |
1154 | [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, |
1155 | [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, | |
1156 | [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, | |
1157 | [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, | |
c02db8c6 CW |
1158 | }; |
1159 | ||
57b61080 SF |
1160 | static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { |
1161 | [IFLA_PORT_VF] = { .type = NLA_U32 }, | |
1162 | [IFLA_PORT_PROFILE] = { .type = NLA_STRING, | |
1163 | .len = PORT_PROFILE_MAX }, | |
1164 | [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, | |
1165 | .len = sizeof(struct ifla_port_vsi)}, | |
1166 | [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, | |
1167 | .len = PORT_UUID_MAX }, | |
1168 | [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, | |
1169 | .len = PORT_UUID_MAX }, | |
1170 | [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, | |
1171 | [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, | |
1172 | }; | |
1173 | ||
81adee47 EB |
1174 | struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) |
1175 | { | |
1176 | struct net *net; | |
1177 | /* Examine the link attributes and figure out which | |
1178 | * network namespace we are talking about. | |
1179 | */ | |
1180 | if (tb[IFLA_NET_NS_PID]) | |
1181 | net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); | |
f0630529 EB |
1182 | else if (tb[IFLA_NET_NS_FD]) |
1183 | net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); | |
81adee47 EB |
1184 | else |
1185 | net = get_net(src_net); | |
1186 | return net; | |
1187 | } | |
1188 | EXPORT_SYMBOL(rtnl_link_get_net); | |
1189 | ||
1840bb13 TG |
1190 | static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) |
1191 | { | |
1192 | if (dev) { | |
1193 | if (tb[IFLA_ADDRESS] && | |
1194 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) | |
1195 | return -EINVAL; | |
1196 | ||
1197 | if (tb[IFLA_BROADCAST] && | |
1198 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) | |
1199 | return -EINVAL; | |
1200 | } | |
1201 | ||
cf7afbfe TG |
1202 | if (tb[IFLA_AF_SPEC]) { |
1203 | struct nlattr *af; | |
1204 | int rem, err; | |
1205 | ||
1206 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { | |
1207 | const struct rtnl_af_ops *af_ops; | |
1208 | ||
1209 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) | |
1210 | return -EAFNOSUPPORT; | |
1211 | ||
1212 | if (!af_ops->set_link_af) | |
1213 | return -EOPNOTSUPP; | |
1214 | ||
1215 | if (af_ops->validate_link_af) { | |
6d3a9a68 | 1216 | err = af_ops->validate_link_af(dev, af); |
cf7afbfe TG |
1217 | if (err < 0) |
1218 | return err; | |
1219 | } | |
1220 | } | |
1221 | } | |
1222 | ||
1840bb13 TG |
1223 | return 0; |
1224 | } | |
1225 | ||
c02db8c6 CW |
1226 | static int do_setvfinfo(struct net_device *dev, struct nlattr *attr) |
1227 | { | |
1228 | int rem, err = -EINVAL; | |
1229 | struct nlattr *vf; | |
1230 | const struct net_device_ops *ops = dev->netdev_ops; | |
1231 | ||
1232 | nla_for_each_nested(vf, attr, rem) { | |
1233 | switch (nla_type(vf)) { | |
1234 | case IFLA_VF_MAC: { | |
1235 | struct ifla_vf_mac *ivm; | |
1236 | ivm = nla_data(vf); | |
1237 | err = -EOPNOTSUPP; | |
1238 | if (ops->ndo_set_vf_mac) | |
1239 | err = ops->ndo_set_vf_mac(dev, ivm->vf, | |
1240 | ivm->mac); | |
1241 | break; | |
1242 | } | |
1243 | case IFLA_VF_VLAN: { | |
1244 | struct ifla_vf_vlan *ivv; | |
1245 | ivv = nla_data(vf); | |
1246 | err = -EOPNOTSUPP; | |
1247 | if (ops->ndo_set_vf_vlan) | |
1248 | err = ops->ndo_set_vf_vlan(dev, ivv->vf, | |
1249 | ivv->vlan, | |
1250 | ivv->qos); | |
1251 | break; | |
1252 | } | |
1253 | case IFLA_VF_TX_RATE: { | |
1254 | struct ifla_vf_tx_rate *ivt; | |
1255 | ivt = nla_data(vf); | |
1256 | err = -EOPNOTSUPP; | |
1257 | if (ops->ndo_set_vf_tx_rate) | |
1258 | err = ops->ndo_set_vf_tx_rate(dev, ivt->vf, | |
1259 | ivt->rate); | |
1260 | break; | |
1261 | } | |
5f8444a3 GR |
1262 | case IFLA_VF_SPOOFCHK: { |
1263 | struct ifla_vf_spoofchk *ivs; | |
1264 | ivs = nla_data(vf); | |
1265 | err = -EOPNOTSUPP; | |
1266 | if (ops->ndo_set_vf_spoofchk) | |
1267 | err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, | |
1268 | ivs->setting); | |
1269 | break; | |
1270 | } | |
c02db8c6 CW |
1271 | default: |
1272 | err = -EINVAL; | |
1273 | break; | |
1274 | } | |
1275 | if (err) | |
1276 | break; | |
1277 | } | |
1278 | return err; | |
1279 | } | |
1280 | ||
fbaec0ea JP |
1281 | static int do_set_master(struct net_device *dev, int ifindex) |
1282 | { | |
898e5061 | 1283 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
fbaec0ea JP |
1284 | const struct net_device_ops *ops; |
1285 | int err; | |
1286 | ||
898e5061 JP |
1287 | if (upper_dev) { |
1288 | if (upper_dev->ifindex == ifindex) | |
fbaec0ea | 1289 | return 0; |
898e5061 | 1290 | ops = upper_dev->netdev_ops; |
fbaec0ea | 1291 | if (ops->ndo_del_slave) { |
898e5061 | 1292 | err = ops->ndo_del_slave(upper_dev, dev); |
fbaec0ea JP |
1293 | if (err) |
1294 | return err; | |
1295 | } else { | |
1296 | return -EOPNOTSUPP; | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | if (ifindex) { | |
898e5061 JP |
1301 | upper_dev = __dev_get_by_index(dev_net(dev), ifindex); |
1302 | if (!upper_dev) | |
fbaec0ea | 1303 | return -EINVAL; |
898e5061 | 1304 | ops = upper_dev->netdev_ops; |
fbaec0ea | 1305 | if (ops->ndo_add_slave) { |
898e5061 | 1306 | err = ops->ndo_add_slave(upper_dev, dev); |
fbaec0ea JP |
1307 | if (err) |
1308 | return err; | |
1309 | } else { | |
1310 | return -EOPNOTSUPP; | |
1311 | } | |
1312 | } | |
1313 | return 0; | |
1314 | } | |
1315 | ||
1141a455 EB |
1316 | static int do_setlink(const struct sk_buff *skb, |
1317 | struct net_device *dev, struct ifinfomsg *ifm, | |
38f7b870 | 1318 | struct nlattr **tb, char *ifname, int modified) |
1da177e4 | 1319 | { |
d314774c | 1320 | const struct net_device_ops *ops = dev->netdev_ops; |
0157f60c | 1321 | int err; |
1da177e4 | 1322 | |
f0630529 | 1323 | if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { |
81adee47 | 1324 | struct net *net = rtnl_link_get_net(dev_net(dev), tb); |
d8a5ec67 EB |
1325 | if (IS_ERR(net)) { |
1326 | err = PTR_ERR(net); | |
1327 | goto errout; | |
1328 | } | |
1141a455 | 1329 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { |
1973ac37 | 1330 | put_net(net); |
b51642f6 EB |
1331 | err = -EPERM; |
1332 | goto errout; | |
1333 | } | |
d8a5ec67 EB |
1334 | err = dev_change_net_namespace(dev, net, ifname); |
1335 | put_net(net); | |
1336 | if (err) | |
1337 | goto errout; | |
1338 | modified = 1; | |
1339 | } | |
1340 | ||
da5e0494 | 1341 | if (tb[IFLA_MAP]) { |
1da177e4 LT |
1342 | struct rtnl_link_ifmap *u_map; |
1343 | struct ifmap k_map; | |
1344 | ||
d314774c | 1345 | if (!ops->ndo_set_config) { |
1da177e4 | 1346 | err = -EOPNOTSUPP; |
0157f60c | 1347 | goto errout; |
1da177e4 LT |
1348 | } |
1349 | ||
1350 | if (!netif_device_present(dev)) { | |
1351 | err = -ENODEV; | |
0157f60c | 1352 | goto errout; |
1da177e4 | 1353 | } |
1da177e4 | 1354 | |
da5e0494 | 1355 | u_map = nla_data(tb[IFLA_MAP]); |
1da177e4 LT |
1356 | k_map.mem_start = (unsigned long) u_map->mem_start; |
1357 | k_map.mem_end = (unsigned long) u_map->mem_end; | |
1358 | k_map.base_addr = (unsigned short) u_map->base_addr; | |
1359 | k_map.irq = (unsigned char) u_map->irq; | |
1360 | k_map.dma = (unsigned char) u_map->dma; | |
1361 | k_map.port = (unsigned char) u_map->port; | |
1362 | ||
d314774c | 1363 | err = ops->ndo_set_config(dev, &k_map); |
da5e0494 | 1364 | if (err < 0) |
0157f60c | 1365 | goto errout; |
1da177e4 | 1366 | |
da5e0494 | 1367 | modified = 1; |
1da177e4 LT |
1368 | } |
1369 | ||
da5e0494 | 1370 | if (tb[IFLA_ADDRESS]) { |
70f8e78e DM |
1371 | struct sockaddr *sa; |
1372 | int len; | |
1373 | ||
70f8e78e DM |
1374 | len = sizeof(sa_family_t) + dev->addr_len; |
1375 | sa = kmalloc(len, GFP_KERNEL); | |
1376 | if (!sa) { | |
1377 | err = -ENOMEM; | |
0157f60c | 1378 | goto errout; |
70f8e78e DM |
1379 | } |
1380 | sa->sa_family = dev->type; | |
da5e0494 | 1381 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
70f8e78e | 1382 | dev->addr_len); |
e7c3273e | 1383 | err = dev_set_mac_address(dev, sa); |
70f8e78e | 1384 | kfree(sa); |
1da177e4 | 1385 | if (err) |
0157f60c | 1386 | goto errout; |
da5e0494 | 1387 | modified = 1; |
1da177e4 LT |
1388 | } |
1389 | ||
da5e0494 TG |
1390 | if (tb[IFLA_MTU]) { |
1391 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); | |
1392 | if (err < 0) | |
0157f60c | 1393 | goto errout; |
da5e0494 | 1394 | modified = 1; |
1da177e4 LT |
1395 | } |
1396 | ||
cbda10fa VD |
1397 | if (tb[IFLA_GROUP]) { |
1398 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); | |
1399 | modified = 1; | |
1400 | } | |
1401 | ||
da5e0494 TG |
1402 | /* |
1403 | * Interface selected by interface index but interface | |
1404 | * name provided implies that a name change has been | |
1405 | * requested. | |
1406 | */ | |
51055be8 | 1407 | if (ifm->ifi_index > 0 && ifname[0]) { |
da5e0494 TG |
1408 | err = dev_change_name(dev, ifname); |
1409 | if (err < 0) | |
0157f60c | 1410 | goto errout; |
da5e0494 | 1411 | modified = 1; |
1da177e4 LT |
1412 | } |
1413 | ||
0b815a1a SH |
1414 | if (tb[IFLA_IFALIAS]) { |
1415 | err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), | |
1416 | nla_len(tb[IFLA_IFALIAS])); | |
1417 | if (err < 0) | |
1418 | goto errout; | |
1419 | modified = 1; | |
1420 | } | |
1421 | ||
da5e0494 TG |
1422 | if (tb[IFLA_BROADCAST]) { |
1423 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); | |
e7c3273e | 1424 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
1da177e4 LT |
1425 | } |
1426 | ||
83b496e9 | 1427 | if (ifm->ifi_flags || ifm->ifi_change) { |
3729d502 | 1428 | err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
5f9021cf JB |
1429 | if (err < 0) |
1430 | goto errout; | |
83b496e9 | 1431 | } |
1da177e4 | 1432 | |
fbaec0ea JP |
1433 | if (tb[IFLA_MASTER]) { |
1434 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); | |
1435 | if (err) | |
1436 | goto errout; | |
1437 | modified = 1; | |
1438 | } | |
1439 | ||
9a57247f JP |
1440 | if (tb[IFLA_CARRIER]) { |
1441 | err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); | |
1442 | if (err) | |
1443 | goto errout; | |
1444 | modified = 1; | |
1445 | } | |
1446 | ||
93b2d4a2 DM |
1447 | if (tb[IFLA_TXQLEN]) |
1448 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); | |
b00055aa | 1449 | |
da5e0494 | 1450 | if (tb[IFLA_OPERSTATE]) |
93b2d4a2 | 1451 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
b00055aa | 1452 | |
da5e0494 | 1453 | if (tb[IFLA_LINKMODE]) { |
93b2d4a2 DM |
1454 | write_lock_bh(&dev_base_lock); |
1455 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); | |
1456 | write_unlock_bh(&dev_base_lock); | |
b00055aa SR |
1457 | } |
1458 | ||
c02db8c6 CW |
1459 | if (tb[IFLA_VFINFO_LIST]) { |
1460 | struct nlattr *attr; | |
1461 | int rem; | |
1462 | nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { | |
253683bb DH |
1463 | if (nla_type(attr) != IFLA_VF_INFO) { |
1464 | err = -EINVAL; | |
c02db8c6 | 1465 | goto errout; |
253683bb | 1466 | } |
c02db8c6 CW |
1467 | err = do_setvfinfo(dev, attr); |
1468 | if (err < 0) | |
1469 | goto errout; | |
1470 | modified = 1; | |
1471 | } | |
ebc08a6f | 1472 | } |
1da177e4 LT |
1473 | err = 0; |
1474 | ||
57b61080 SF |
1475 | if (tb[IFLA_VF_PORTS]) { |
1476 | struct nlattr *port[IFLA_PORT_MAX+1]; | |
1477 | struct nlattr *attr; | |
1478 | int vf; | |
1479 | int rem; | |
1480 | ||
1481 | err = -EOPNOTSUPP; | |
1482 | if (!ops->ndo_set_vf_port) | |
1483 | goto errout; | |
1484 | ||
1485 | nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { | |
1486 | if (nla_type(attr) != IFLA_VF_PORT) | |
1487 | continue; | |
1488 | err = nla_parse_nested(port, IFLA_PORT_MAX, | |
1489 | attr, ifla_port_policy); | |
1490 | if (err < 0) | |
1491 | goto errout; | |
1492 | if (!port[IFLA_PORT_VF]) { | |
1493 | err = -EOPNOTSUPP; | |
1494 | goto errout; | |
1495 | } | |
1496 | vf = nla_get_u32(port[IFLA_PORT_VF]); | |
1497 | err = ops->ndo_set_vf_port(dev, vf, port); | |
1498 | if (err < 0) | |
1499 | goto errout; | |
1500 | modified = 1; | |
1501 | } | |
1502 | } | |
1503 | err = 0; | |
1504 | ||
1505 | if (tb[IFLA_PORT_SELF]) { | |
1506 | struct nlattr *port[IFLA_PORT_MAX+1]; | |
1507 | ||
1508 | err = nla_parse_nested(port, IFLA_PORT_MAX, | |
1509 | tb[IFLA_PORT_SELF], ifla_port_policy); | |
1510 | if (err < 0) | |
1511 | goto errout; | |
1512 | ||
1513 | err = -EOPNOTSUPP; | |
1514 | if (ops->ndo_set_vf_port) | |
1515 | err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); | |
1516 | if (err < 0) | |
1517 | goto errout; | |
1518 | modified = 1; | |
1519 | } | |
f8ff182c TG |
1520 | |
1521 | if (tb[IFLA_AF_SPEC]) { | |
1522 | struct nlattr *af; | |
1523 | int rem; | |
1524 | ||
1525 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { | |
1526 | const struct rtnl_af_ops *af_ops; | |
1527 | ||
1528 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) | |
cf7afbfe | 1529 | BUG(); |
f8ff182c | 1530 | |
cf7afbfe | 1531 | err = af_ops->set_link_af(dev, af); |
f8ff182c TG |
1532 | if (err < 0) |
1533 | goto errout; | |
1534 | ||
1535 | modified = 1; | |
1536 | } | |
1537 | } | |
57b61080 SF |
1538 | err = 0; |
1539 | ||
0157f60c | 1540 | errout: |
e87cc472 JP |
1541 | if (err < 0 && modified) |
1542 | 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", | |
1543 | dev->name); | |
da5e0494 | 1544 | |
0157f60c PM |
1545 | return err; |
1546 | } | |
1da177e4 | 1547 | |
661d2967 | 1548 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
0157f60c | 1549 | { |
3b1e0a65 | 1550 | struct net *net = sock_net(skb->sk); |
0157f60c PM |
1551 | struct ifinfomsg *ifm; |
1552 | struct net_device *dev; | |
1553 | int err; | |
1554 | struct nlattr *tb[IFLA_MAX+1]; | |
1555 | char ifname[IFNAMSIZ]; | |
1556 | ||
1557 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); | |
1558 | if (err < 0) | |
1559 | goto errout; | |
1560 | ||
1561 | if (tb[IFLA_IFNAME]) | |
1562 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
1563 | else | |
1564 | ifname[0] = '\0'; | |
1565 | ||
1566 | err = -EINVAL; | |
1567 | ifm = nlmsg_data(nlh); | |
1568 | if (ifm->ifi_index > 0) | |
a3d12891 | 1569 | dev = __dev_get_by_index(net, ifm->ifi_index); |
0157f60c | 1570 | else if (tb[IFLA_IFNAME]) |
a3d12891 | 1571 | dev = __dev_get_by_name(net, ifname); |
0157f60c PM |
1572 | else |
1573 | goto errout; | |
1574 | ||
1575 | if (dev == NULL) { | |
1576 | err = -ENODEV; | |
1577 | goto errout; | |
1578 | } | |
1579 | ||
e0d087af ED |
1580 | err = validate_linkmsg(dev, tb); |
1581 | if (err < 0) | |
a3d12891 | 1582 | goto errout; |
0157f60c | 1583 | |
1141a455 | 1584 | err = do_setlink(skb, dev, ifm, tb, ifname, 0); |
da5e0494 | 1585 | errout: |
1da177e4 LT |
1586 | return err; |
1587 | } | |
1588 | ||
661d2967 | 1589 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) |
38f7b870 | 1590 | { |
3b1e0a65 | 1591 | struct net *net = sock_net(skb->sk); |
38f7b870 PM |
1592 | const struct rtnl_link_ops *ops; |
1593 | struct net_device *dev; | |
1594 | struct ifinfomsg *ifm; | |
1595 | char ifname[IFNAMSIZ]; | |
1596 | struct nlattr *tb[IFLA_MAX+1]; | |
1597 | int err; | |
226bd341 | 1598 | LIST_HEAD(list_kill); |
38f7b870 PM |
1599 | |
1600 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); | |
1601 | if (err < 0) | |
1602 | return err; | |
1603 | ||
1604 | if (tb[IFLA_IFNAME]) | |
1605 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
1606 | ||
1607 | ifm = nlmsg_data(nlh); | |
1608 | if (ifm->ifi_index > 0) | |
881d966b | 1609 | dev = __dev_get_by_index(net, ifm->ifi_index); |
38f7b870 | 1610 | else if (tb[IFLA_IFNAME]) |
881d966b | 1611 | dev = __dev_get_by_name(net, ifname); |
38f7b870 PM |
1612 | else |
1613 | return -EINVAL; | |
1614 | ||
1615 | if (!dev) | |
1616 | return -ENODEV; | |
1617 | ||
1618 | ops = dev->rtnl_link_ops; | |
1619 | if (!ops) | |
1620 | return -EOPNOTSUPP; | |
1621 | ||
226bd341 ED |
1622 | ops->dellink(dev, &list_kill); |
1623 | unregister_netdevice_many(&list_kill); | |
38f7b870 PM |
1624 | return 0; |
1625 | } | |
1626 | ||
3729d502 PM |
1627 | int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) |
1628 | { | |
1629 | unsigned int old_flags; | |
1630 | int err; | |
1631 | ||
1632 | old_flags = dev->flags; | |
1633 | if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { | |
1634 | err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); | |
1635 | if (err < 0) | |
1636 | return err; | |
1637 | } | |
1638 | ||
1639 | dev->rtnl_link_state = RTNL_LINK_INITIALIZED; | |
1640 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); | |
1641 | ||
1642 | __dev_notify_flags(dev, old_flags); | |
1643 | return 0; | |
1644 | } | |
1645 | EXPORT_SYMBOL(rtnl_configure_link); | |
1646 | ||
c0713563 | 1647 | struct net_device *rtnl_create_link(struct net *net, |
81adee47 | 1648 | char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[]) |
e7199288 PE |
1649 | { |
1650 | int err; | |
1651 | struct net_device *dev; | |
d40156aa JP |
1652 | unsigned int num_tx_queues = 1; |
1653 | unsigned int num_rx_queues = 1; | |
e7199288 | 1654 | |
76ff5cc9 JP |
1655 | if (tb[IFLA_NUM_TX_QUEUES]) |
1656 | num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); | |
1657 | else if (ops->get_num_tx_queues) | |
d40156aa | 1658 | num_tx_queues = ops->get_num_tx_queues(); |
76ff5cc9 JP |
1659 | |
1660 | if (tb[IFLA_NUM_RX_QUEUES]) | |
1661 | num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); | |
1662 | else if (ops->get_num_rx_queues) | |
d40156aa | 1663 | num_rx_queues = ops->get_num_rx_queues(); |
efacb309 | 1664 | |
e7199288 | 1665 | err = -ENOMEM; |
d40156aa JP |
1666 | dev = alloc_netdev_mqs(ops->priv_size, ifname, ops->setup, |
1667 | num_tx_queues, num_rx_queues); | |
e7199288 PE |
1668 | if (!dev) |
1669 | goto err; | |
1670 | ||
81adee47 EB |
1671 | dev_net_set(dev, net); |
1672 | dev->rtnl_link_ops = ops; | |
3729d502 | 1673 | dev->rtnl_link_state = RTNL_LINK_INITIALIZING; |
81adee47 | 1674 | |
e7199288 PE |
1675 | if (tb[IFLA_MTU]) |
1676 | dev->mtu = nla_get_u32(tb[IFLA_MTU]); | |
2afb9b53 | 1677 | if (tb[IFLA_ADDRESS]) { |
e7199288 PE |
1678 | memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), |
1679 | nla_len(tb[IFLA_ADDRESS])); | |
2afb9b53 JP |
1680 | dev->addr_assign_type = NET_ADDR_SET; |
1681 | } | |
e7199288 PE |
1682 | if (tb[IFLA_BROADCAST]) |
1683 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), | |
1684 | nla_len(tb[IFLA_BROADCAST])); | |
1685 | if (tb[IFLA_TXQLEN]) | |
1686 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); | |
1687 | if (tb[IFLA_OPERSTATE]) | |
93b2d4a2 | 1688 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
e7199288 PE |
1689 | if (tb[IFLA_LINKMODE]) |
1690 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); | |
ffa934f1 PM |
1691 | if (tb[IFLA_GROUP]) |
1692 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); | |
e7199288 PE |
1693 | |
1694 | return dev; | |
1695 | ||
e7199288 PE |
1696 | err: |
1697 | return ERR_PTR(err); | |
1698 | } | |
e0d087af | 1699 | EXPORT_SYMBOL(rtnl_create_link); |
e7199288 | 1700 | |
1141a455 EB |
1701 | static int rtnl_group_changelink(const struct sk_buff *skb, |
1702 | struct net *net, int group, | |
e7ed828f VD |
1703 | struct ifinfomsg *ifm, |
1704 | struct nlattr **tb) | |
1705 | { | |
1706 | struct net_device *dev; | |
1707 | int err; | |
1708 | ||
1709 | for_each_netdev(net, dev) { | |
1710 | if (dev->group == group) { | |
1141a455 | 1711 | err = do_setlink(skb, dev, ifm, tb, NULL, 0); |
e7ed828f VD |
1712 | if (err < 0) |
1713 | return err; | |
1714 | } | |
1715 | } | |
1716 | ||
1717 | return 0; | |
1718 | } | |
1719 | ||
661d2967 | 1720 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
38f7b870 | 1721 | { |
3b1e0a65 | 1722 | struct net *net = sock_net(skb->sk); |
38f7b870 PM |
1723 | const struct rtnl_link_ops *ops; |
1724 | struct net_device *dev; | |
1725 | struct ifinfomsg *ifm; | |
1726 | char kind[MODULE_NAME_LEN]; | |
1727 | char ifname[IFNAMSIZ]; | |
1728 | struct nlattr *tb[IFLA_MAX+1]; | |
1729 | struct nlattr *linkinfo[IFLA_INFO_MAX+1]; | |
1730 | int err; | |
1731 | ||
95a5afca | 1732 | #ifdef CONFIG_MODULES |
38f7b870 | 1733 | replay: |
8072f085 | 1734 | #endif |
38f7b870 PM |
1735 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
1736 | if (err < 0) | |
1737 | return err; | |
1738 | ||
1739 | if (tb[IFLA_IFNAME]) | |
1740 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
1741 | else | |
1742 | ifname[0] = '\0'; | |
1743 | ||
1744 | ifm = nlmsg_data(nlh); | |
1745 | if (ifm->ifi_index > 0) | |
881d966b | 1746 | dev = __dev_get_by_index(net, ifm->ifi_index); |
e7ed828f VD |
1747 | else { |
1748 | if (ifname[0]) | |
1749 | dev = __dev_get_by_name(net, ifname); | |
e7ed828f VD |
1750 | else |
1751 | dev = NULL; | |
1752 | } | |
38f7b870 | 1753 | |
e0d087af ED |
1754 | err = validate_linkmsg(dev, tb); |
1755 | if (err < 0) | |
1840bb13 TG |
1756 | return err; |
1757 | ||
38f7b870 PM |
1758 | if (tb[IFLA_LINKINFO]) { |
1759 | err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, | |
1760 | tb[IFLA_LINKINFO], ifla_info_policy); | |
1761 | if (err < 0) | |
1762 | return err; | |
1763 | } else | |
1764 | memset(linkinfo, 0, sizeof(linkinfo)); | |
1765 | ||
1766 | if (linkinfo[IFLA_INFO_KIND]) { | |
1767 | nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); | |
1768 | ops = rtnl_link_ops_get(kind); | |
1769 | } else { | |
1770 | kind[0] = '\0'; | |
1771 | ops = NULL; | |
1772 | } | |
1773 | ||
1774 | if (1) { | |
1775 | struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; | |
81adee47 | 1776 | struct net *dest_net; |
38f7b870 PM |
1777 | |
1778 | if (ops) { | |
1779 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { | |
1780 | err = nla_parse_nested(attr, ops->maxtype, | |
1781 | linkinfo[IFLA_INFO_DATA], | |
1782 | ops->policy); | |
1783 | if (err < 0) | |
1784 | return err; | |
1785 | data = attr; | |
1786 | } | |
1787 | if (ops->validate) { | |
1788 | err = ops->validate(tb, data); | |
1789 | if (err < 0) | |
1790 | return err; | |
1791 | } | |
1792 | } | |
1793 | ||
1794 | if (dev) { | |
1795 | int modified = 0; | |
1796 | ||
1797 | if (nlh->nlmsg_flags & NLM_F_EXCL) | |
1798 | return -EEXIST; | |
1799 | if (nlh->nlmsg_flags & NLM_F_REPLACE) | |
1800 | return -EOPNOTSUPP; | |
1801 | ||
1802 | if (linkinfo[IFLA_INFO_DATA]) { | |
1803 | if (!ops || ops != dev->rtnl_link_ops || | |
1804 | !ops->changelink) | |
1805 | return -EOPNOTSUPP; | |
1806 | ||
1807 | err = ops->changelink(dev, tb, data); | |
1808 | if (err < 0) | |
1809 | return err; | |
1810 | modified = 1; | |
1811 | } | |
1812 | ||
1141a455 | 1813 | return do_setlink(skb, dev, ifm, tb, ifname, modified); |
38f7b870 PM |
1814 | } |
1815 | ||
ffa934f1 PM |
1816 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { |
1817 | if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) | |
1141a455 | 1818 | return rtnl_group_changelink(skb, net, |
ffa934f1 PM |
1819 | nla_get_u32(tb[IFLA_GROUP]), |
1820 | ifm, tb); | |
38f7b870 | 1821 | return -ENODEV; |
ffa934f1 | 1822 | } |
38f7b870 | 1823 | |
0e06877c | 1824 | if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) |
38f7b870 PM |
1825 | return -EOPNOTSUPP; |
1826 | ||
1827 | if (!ops) { | |
95a5afca | 1828 | #ifdef CONFIG_MODULES |
38f7b870 PM |
1829 | if (kind[0]) { |
1830 | __rtnl_unlock(); | |
1831 | request_module("rtnl-link-%s", kind); | |
1832 | rtnl_lock(); | |
1833 | ops = rtnl_link_ops_get(kind); | |
1834 | if (ops) | |
1835 | goto replay; | |
1836 | } | |
1837 | #endif | |
1838 | return -EOPNOTSUPP; | |
1839 | } | |
1840 | ||
1841 | if (!ifname[0]) | |
1842 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); | |
e7199288 | 1843 | |
81adee47 | 1844 | dest_net = rtnl_link_get_net(net, tb); |
13ad1774 EB |
1845 | if (IS_ERR(dest_net)) |
1846 | return PTR_ERR(dest_net); | |
1847 | ||
c0713563 | 1848 | dev = rtnl_create_link(dest_net, ifname, ops, tb); |
9c7dafbf | 1849 | if (IS_ERR(dev)) { |
e7199288 | 1850 | err = PTR_ERR(dev); |
9c7dafbf PE |
1851 | goto out; |
1852 | } | |
1853 | ||
1854 | dev->ifindex = ifm->ifi_index; | |
1855 | ||
1856 | if (ops->newlink) | |
81adee47 | 1857 | err = ops->newlink(net, dev, tb, data); |
2d85cba2 PM |
1858 | else |
1859 | err = register_netdevice(dev); | |
80032cff DC |
1860 | |
1861 | if (err < 0 && !IS_ERR(dev)) | |
38f7b870 | 1862 | free_netdev(dev); |
80032cff | 1863 | if (err < 0) |
3729d502 | 1864 | goto out; |
81adee47 | 1865 | |
3729d502 | 1866 | err = rtnl_configure_link(dev, ifm); |
140b057c WC |
1867 | if (err < 0) { |
1868 | if (ops->newlink) { | |
1869 | LIST_HEAD(list_kill); | |
1870 | ||
1871 | ops->dellink(dev, &list_kill); | |
1872 | unregister_netdevice_many(&list_kill); | |
1873 | } else { | |
1874 | unregister_netdevice(dev); | |
1875 | } | |
1876 | } | |
3729d502 | 1877 | out: |
81adee47 | 1878 | put_net(dest_net); |
38f7b870 PM |
1879 | return err; |
1880 | } | |
1881 | } | |
1882 | ||
661d2967 | 1883 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh) |
711e2c33 | 1884 | { |
3b1e0a65 | 1885 | struct net *net = sock_net(skb->sk); |
b60c5115 | 1886 | struct ifinfomsg *ifm; |
a3d12891 | 1887 | char ifname[IFNAMSIZ]; |
b60c5115 TG |
1888 | struct nlattr *tb[IFLA_MAX+1]; |
1889 | struct net_device *dev = NULL; | |
1890 | struct sk_buff *nskb; | |
339bf98f | 1891 | int err; |
115c9b81 | 1892 | u32 ext_filter_mask = 0; |
711e2c33 | 1893 | |
b60c5115 TG |
1894 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
1895 | if (err < 0) | |
9918f230 | 1896 | return err; |
b60c5115 | 1897 | |
a3d12891 ED |
1898 | if (tb[IFLA_IFNAME]) |
1899 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
1900 | ||
115c9b81 GR |
1901 | if (tb[IFLA_EXT_MASK]) |
1902 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); | |
1903 | ||
b60c5115 | 1904 | ifm = nlmsg_data(nlh); |
a3d12891 ED |
1905 | if (ifm->ifi_index > 0) |
1906 | dev = __dev_get_by_index(net, ifm->ifi_index); | |
1907 | else if (tb[IFLA_IFNAME]) | |
1908 | dev = __dev_get_by_name(net, ifname); | |
1909 | else | |
711e2c33 | 1910 | return -EINVAL; |
711e2c33 | 1911 | |
a3d12891 ED |
1912 | if (dev == NULL) |
1913 | return -ENODEV; | |
1914 | ||
115c9b81 | 1915 | nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); |
a3d12891 ED |
1916 | if (nskb == NULL) |
1917 | return -ENOBUFS; | |
b60c5115 | 1918 | |
15e47304 | 1919 | err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid, |
115c9b81 | 1920 | nlh->nlmsg_seq, 0, 0, ext_filter_mask); |
26932566 PM |
1921 | if (err < 0) { |
1922 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ | |
1923 | WARN_ON(err == -EMSGSIZE); | |
1924 | kfree_skb(nskb); | |
a3d12891 | 1925 | } else |
15e47304 | 1926 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); |
711e2c33 | 1927 | |
b60c5115 | 1928 | return err; |
711e2c33 | 1929 | } |
711e2c33 | 1930 | |
115c9b81 | 1931 | static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) |
c7ac8679 | 1932 | { |
115c9b81 GR |
1933 | struct net *net = sock_net(skb->sk); |
1934 | struct net_device *dev; | |
1935 | struct nlattr *tb[IFLA_MAX+1]; | |
1936 | u32 ext_filter_mask = 0; | |
1937 | u16 min_ifinfo_dump_size = 0; | |
fb0d10bf MS |
1938 | int hdrlen; |
1939 | ||
1940 | /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ | |
1941 | hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? | |
1942 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); | |
115c9b81 | 1943 | |
fb0d10bf | 1944 | if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { |
a4b64fbe ED |
1945 | if (tb[IFLA_EXT_MASK]) |
1946 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); | |
1947 | } | |
115c9b81 GR |
1948 | |
1949 | if (!ext_filter_mask) | |
1950 | return NLMSG_GOODSIZE; | |
1951 | /* | |
1952 | * traverse the list of net devices and compute the minimum | |
1953 | * buffer size based upon the filter mask. | |
1954 | */ | |
1955 | list_for_each_entry(dev, &net->dev_base_head, dev_list) { | |
1956 | min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, | |
1957 | if_nlmsg_size(dev, | |
1958 | ext_filter_mask)); | |
1959 | } | |
1960 | ||
c7ac8679 GR |
1961 | return min_ifinfo_dump_size; |
1962 | } | |
1963 | ||
42bad1da | 1964 | static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) |
1da177e4 LT |
1965 | { |
1966 | int idx; | |
1967 | int s_idx = cb->family; | |
1968 | ||
1969 | if (s_idx == 0) | |
1970 | s_idx = 1; | |
25239cee | 1971 | for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { |
1da177e4 LT |
1972 | int type = cb->nlh->nlmsg_type-RTM_BASE; |
1973 | if (idx < s_idx || idx == PF_PACKET) | |
1974 | continue; | |
e2849863 TG |
1975 | if (rtnl_msg_handlers[idx] == NULL || |
1976 | rtnl_msg_handlers[idx][type].dumpit == NULL) | |
1da177e4 | 1977 | continue; |
0465277f | 1978 | if (idx > s_idx) { |
1da177e4 | 1979 | memset(&cb->args[0], 0, sizeof(cb->args)); |
0465277f ND |
1980 | cb->prev_seq = 0; |
1981 | cb->seq = 0; | |
1982 | } | |
e2849863 | 1983 | if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) |
1da177e4 LT |
1984 | break; |
1985 | } | |
1986 | cb->family = idx; | |
1987 | ||
1988 | return skb->len; | |
1989 | } | |
1990 | ||
95c96174 | 1991 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change) |
1da177e4 | 1992 | { |
c346dca1 | 1993 | struct net *net = dev_net(dev); |
1da177e4 | 1994 | struct sk_buff *skb; |
0ec6d3f4 | 1995 | int err = -ENOBUFS; |
c7ac8679 | 1996 | size_t if_info_size; |
6fa3eb70 S |
1997 | #ifdef CONFIG_MTK_NET_LOGGING |
1998 | printk(KERN_INFO "[mtk_net][rtnetlink]rtmsg_ifinfo type:%d, dev:%s, change:%u, pid = %d", | |
1999 | type, dev->name, change, current->pid); | |
2000 | #endif | |
115c9b81 | 2001 | skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL); |
0ec6d3f4 TG |
2002 | if (skb == NULL) |
2003 | goto errout; | |
1da177e4 | 2004 | |
115c9b81 | 2005 | err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0); |
26932566 PM |
2006 | if (err < 0) { |
2007 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ | |
2008 | WARN_ON(err == -EMSGSIZE); | |
2009 | kfree_skb(skb); | |
2010 | goto errout; | |
2011 | } | |
1ce85fe4 PNA |
2012 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); |
2013 | return; | |
0ec6d3f4 TG |
2014 | errout: |
2015 | if (err < 0) | |
4b3da706 | 2016 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
1da177e4 | 2017 | } |
471cb5a3 | 2018 | EXPORT_SYMBOL(rtmsg_ifinfo); |
1da177e4 | 2019 | |
d83b0603 JF |
2020 | static int nlmsg_populate_fdb_fill(struct sk_buff *skb, |
2021 | struct net_device *dev, | |
2022 | u8 *addr, u32 pid, u32 seq, | |
c0669e49 ND |
2023 | int type, unsigned int flags, |
2024 | int nlflags) | |
d83b0603 JF |
2025 | { |
2026 | struct nlmsghdr *nlh; | |
2027 | struct ndmsg *ndm; | |
2028 | ||
c0669e49 | 2029 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); |
d83b0603 JF |
2030 | if (!nlh) |
2031 | return -EMSGSIZE; | |
2032 | ||
2033 | ndm = nlmsg_data(nlh); | |
2034 | ndm->ndm_family = AF_BRIDGE; | |
2035 | ndm->ndm_pad1 = 0; | |
2036 | ndm->ndm_pad2 = 0; | |
2037 | ndm->ndm_flags = flags; | |
2038 | ndm->ndm_type = 0; | |
2039 | ndm->ndm_ifindex = dev->ifindex; | |
2040 | ndm->ndm_state = NUD_PERMANENT; | |
2041 | ||
2042 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) | |
2043 | goto nla_put_failure; | |
2044 | ||
2045 | return nlmsg_end(skb, nlh); | |
2046 | ||
2047 | nla_put_failure: | |
2048 | nlmsg_cancel(skb, nlh); | |
2049 | return -EMSGSIZE; | |
2050 | } | |
2051 | ||
3ff661c3 JF |
2052 | static inline size_t rtnl_fdb_nlmsg_size(void) |
2053 | { | |
2054 | return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN); | |
2055 | } | |
2056 | ||
2057 | static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type) | |
2058 | { | |
2059 | struct net *net = dev_net(dev); | |
2060 | struct sk_buff *skb; | |
2061 | int err = -ENOBUFS; | |
2062 | ||
2063 | skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); | |
2064 | if (!skb) | |
2065 | goto errout; | |
2066 | ||
c0669e49 | 2067 | err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0); |
3ff661c3 JF |
2068 | if (err < 0) { |
2069 | kfree_skb(skb); | |
2070 | goto errout; | |
2071 | } | |
2072 | ||
2073 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); | |
2074 | return; | |
2075 | errout: | |
2076 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); | |
2077 | } | |
2078 | ||
090096bf VY |
2079 | /** |
2080 | * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry | |
2081 | */ | |
2082 | int ndo_dflt_fdb_add(struct ndmsg *ndm, | |
2083 | struct nlattr *tb[], | |
2084 | struct net_device *dev, | |
2085 | const unsigned char *addr, | |
2086 | u16 flags) | |
2087 | { | |
2088 | int err = -EINVAL; | |
2089 | ||
2090 | /* If aging addresses are supported device will need to | |
2091 | * implement its own handler for this. | |
2092 | */ | |
2093 | if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { | |
2094 | pr_info("%s: FDB only supports static addresses\n", dev->name); | |
2095 | return err; | |
2096 | } | |
2097 | ||
2098 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) | |
2099 | err = dev_uc_add_excl(dev, addr); | |
2100 | else if (is_multicast_ether_addr(addr)) | |
2101 | err = dev_mc_add_excl(dev, addr); | |
2102 | ||
2103 | /* Only return duplicate errors if NLM_F_EXCL is set */ | |
2104 | if (err == -EEXIST && !(flags & NLM_F_EXCL)) | |
2105 | err = 0; | |
2106 | ||
2107 | return err; | |
2108 | } | |
2109 | EXPORT_SYMBOL(ndo_dflt_fdb_add); | |
2110 | ||
661d2967 | 2111 | static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh) |
77162022 JF |
2112 | { |
2113 | struct net *net = sock_net(skb->sk); | |
77162022 JF |
2114 | struct ndmsg *ndm; |
2115 | struct nlattr *tb[NDA_MAX+1]; | |
2116 | struct net_device *dev; | |
2117 | u8 *addr; | |
2118 | int err; | |
2119 | ||
2120 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); | |
2121 | if (err < 0) | |
2122 | return err; | |
2123 | ||
2124 | ndm = nlmsg_data(nlh); | |
2125 | if (ndm->ndm_ifindex == 0) { | |
2126 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n"); | |
2127 | return -EINVAL; | |
2128 | } | |
2129 | ||
2130 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); | |
2131 | if (dev == NULL) { | |
2132 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n"); | |
2133 | return -ENODEV; | |
2134 | } | |
2135 | ||
2136 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { | |
2137 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n"); | |
2138 | return -EINVAL; | |
2139 | } | |
2140 | ||
2141 | addr = nla_data(tb[NDA_LLADDR]); | |
6681712d | 2142 | if (is_zero_ether_addr(addr)) { |
77162022 JF |
2143 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ether address\n"); |
2144 | return -EINVAL; | |
2145 | } | |
2146 | ||
2147 | err = -EOPNOTSUPP; | |
2148 | ||
2149 | /* Support fdb on master device the net/bridge default case */ | |
2150 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && | |
2151 | (dev->priv_flags & IFF_BRIDGE_PORT)) { | |
898e5061 JP |
2152 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
2153 | const struct net_device_ops *ops = br_dev->netdev_ops; | |
2154 | ||
2155 | err = ops->ndo_fdb_add(ndm, tb, dev, addr, nlh->nlmsg_flags); | |
77162022 JF |
2156 | if (err) |
2157 | goto out; | |
2158 | else | |
2159 | ndm->ndm_flags &= ~NTF_MASTER; | |
2160 | } | |
2161 | ||
2162 | /* Embedded bridge, macvlan, and any other device support */ | |
090096bf VY |
2163 | if ((ndm->ndm_flags & NTF_SELF)) { |
2164 | if (dev->netdev_ops->ndo_fdb_add) | |
2165 | err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, | |
2166 | nlh->nlmsg_flags); | |
2167 | else | |
2168 | err = ndo_dflt_fdb_add(ndm, tb, dev, addr, | |
2169 | nlh->nlmsg_flags); | |
77162022 | 2170 | |
3ff661c3 JF |
2171 | if (!err) { |
2172 | rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH); | |
77162022 | 2173 | ndm->ndm_flags &= ~NTF_SELF; |
3ff661c3 | 2174 | } |
77162022 JF |
2175 | } |
2176 | out: | |
2177 | return err; | |
2178 | } | |
2179 | ||
090096bf VY |
2180 | /** |
2181 | * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry | |
2182 | */ | |
2183 | int ndo_dflt_fdb_del(struct ndmsg *ndm, | |
2184 | struct nlattr *tb[], | |
2185 | struct net_device *dev, | |
2186 | const unsigned char *addr) | |
2187 | { | |
2188 | int err = -EOPNOTSUPP; | |
2189 | ||
2190 | /* If aging addresses are supported device will need to | |
2191 | * implement its own handler for this. | |
2192 | */ | |
21db4be1 | 2193 | if (!(ndm->ndm_state & NUD_PERMANENT)) { |
090096bf VY |
2194 | pr_info("%s: FDB only supports static addresses\n", dev->name); |
2195 | return -EINVAL; | |
2196 | } | |
2197 | ||
2198 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) | |
2199 | err = dev_uc_del(dev, addr); | |
2200 | else if (is_multicast_ether_addr(addr)) | |
2201 | err = dev_mc_del(dev, addr); | |
2202 | else | |
2203 | err = -EINVAL; | |
2204 | ||
2205 | return err; | |
2206 | } | |
2207 | EXPORT_SYMBOL(ndo_dflt_fdb_del); | |
2208 | ||
661d2967 | 2209 | static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh) |
77162022 JF |
2210 | { |
2211 | struct net *net = sock_net(skb->sk); | |
2212 | struct ndmsg *ndm; | |
1690be63 | 2213 | struct nlattr *tb[NDA_MAX+1]; |
77162022 JF |
2214 | struct net_device *dev; |
2215 | int err = -EINVAL; | |
2216 | __u8 *addr; | |
2217 | ||
1141a455 | 2218 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
1690be63 VY |
2219 | return -EPERM; |
2220 | ||
2221 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); | |
2222 | if (err < 0) | |
2223 | return err; | |
77162022 JF |
2224 | |
2225 | ndm = nlmsg_data(nlh); | |
2226 | if (ndm->ndm_ifindex == 0) { | |
2227 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n"); | |
2228 | return -EINVAL; | |
2229 | } | |
2230 | ||
2231 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); | |
2232 | if (dev == NULL) { | |
2233 | pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n"); | |
2234 | return -ENODEV; | |
2235 | } | |
2236 | ||
1690be63 VY |
2237 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
2238 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n"); | |
2239 | return -EINVAL; | |
2240 | } | |
2241 | ||
2242 | addr = nla_data(tb[NDA_LLADDR]); | |
37fe0660 | 2243 | if (is_zero_ether_addr(addr)) { |
1690be63 | 2244 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ether address\n"); |
77162022 JF |
2245 | return -EINVAL; |
2246 | } | |
2247 | ||
77162022 JF |
2248 | err = -EOPNOTSUPP; |
2249 | ||
2250 | /* Support fdb on master device the net/bridge default case */ | |
2251 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && | |
2252 | (dev->priv_flags & IFF_BRIDGE_PORT)) { | |
898e5061 JP |
2253 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
2254 | const struct net_device_ops *ops = br_dev->netdev_ops; | |
77162022 | 2255 | |
898e5061 | 2256 | if (ops->ndo_fdb_del) |
1690be63 | 2257 | err = ops->ndo_fdb_del(ndm, tb, dev, addr); |
77162022 JF |
2258 | |
2259 | if (err) | |
2260 | goto out; | |
2261 | else | |
2262 | ndm->ndm_flags &= ~NTF_MASTER; | |
2263 | } | |
2264 | ||
2265 | /* Embedded bridge, macvlan, and any other device support */ | |
090096bf VY |
2266 | if (ndm->ndm_flags & NTF_SELF) { |
2267 | if (dev->netdev_ops->ndo_fdb_del) | |
2268 | err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr); | |
2269 | else | |
2270 | err = ndo_dflt_fdb_del(ndm, tb, dev, addr); | |
77162022 | 2271 | |
3ff661c3 JF |
2272 | if (!err) { |
2273 | rtnl_fdb_notify(dev, addr, RTM_DELNEIGH); | |
77162022 | 2274 | ndm->ndm_flags &= ~NTF_SELF; |
3ff661c3 | 2275 | } |
77162022 JF |
2276 | } |
2277 | out: | |
2278 | return err; | |
2279 | } | |
2280 | ||
d83b0603 JF |
2281 | static int nlmsg_populate_fdb(struct sk_buff *skb, |
2282 | struct netlink_callback *cb, | |
2283 | struct net_device *dev, | |
2284 | int *idx, | |
2285 | struct netdev_hw_addr_list *list) | |
2286 | { | |
2287 | struct netdev_hw_addr *ha; | |
2288 | int err; | |
15e47304 | 2289 | u32 portid, seq; |
d83b0603 | 2290 | |
15e47304 | 2291 | portid = NETLINK_CB(cb->skb).portid; |
d83b0603 JF |
2292 | seq = cb->nlh->nlmsg_seq; |
2293 | ||
2294 | list_for_each_entry(ha, &list->list, list) { | |
2295 | if (*idx < cb->args[0]) | |
2296 | goto skip; | |
2297 | ||
2298 | err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, | |
a7a558fe | 2299 | portid, seq, |
c0669e49 ND |
2300 | RTM_NEWNEIGH, NTF_SELF, |
2301 | NLM_F_MULTI); | |
d83b0603 JF |
2302 | if (err < 0) |
2303 | return err; | |
2304 | skip: | |
2305 | *idx += 1; | |
2306 | } | |
2307 | return 0; | |
2308 | } | |
2309 | ||
2310 | /** | |
2c53040f | 2311 | * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. |
d83b0603 JF |
2312 | * @nlh: netlink message header |
2313 | * @dev: netdevice | |
2314 | * | |
2315 | * Default netdevice operation to dump the existing unicast address list. | |
91f3e7b1 | 2316 | * Returns number of addresses from list put in skb. |
d83b0603 JF |
2317 | */ |
2318 | int ndo_dflt_fdb_dump(struct sk_buff *skb, | |
2319 | struct netlink_callback *cb, | |
2320 | struct net_device *dev, | |
2321 | int idx) | |
2322 | { | |
2323 | int err; | |
2324 | ||
2325 | netif_addr_lock_bh(dev); | |
2326 | err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc); | |
2327 | if (err) | |
2328 | goto out; | |
2329 | nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc); | |
2330 | out: | |
2331 | netif_addr_unlock_bh(dev); | |
2332 | return idx; | |
2333 | } | |
2334 | EXPORT_SYMBOL(ndo_dflt_fdb_dump); | |
2335 | ||
77162022 JF |
2336 | static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) |
2337 | { | |
2338 | int idx = 0; | |
2339 | struct net *net = sock_net(skb->sk); | |
2340 | struct net_device *dev; | |
2341 | ||
2342 | rcu_read_lock(); | |
2343 | for_each_netdev_rcu(net, dev) { | |
2344 | if (dev->priv_flags & IFF_BRIDGE_PORT) { | |
898e5061 JP |
2345 | struct net_device *br_dev; |
2346 | const struct net_device_ops *ops; | |
77162022 | 2347 | |
898e5061 JP |
2348 | br_dev = netdev_master_upper_dev_get(dev); |
2349 | ops = br_dev->netdev_ops; | |
77162022 JF |
2350 | if (ops->ndo_fdb_dump) |
2351 | idx = ops->ndo_fdb_dump(skb, cb, dev, idx); | |
2352 | } | |
2353 | ||
2354 | if (dev->netdev_ops->ndo_fdb_dump) | |
2355 | idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, idx); | |
090096bf | 2356 | else |
91f3e7b1 | 2357 | idx = ndo_dflt_fdb_dump(skb, cb, dev, idx); |
77162022 JF |
2358 | } |
2359 | rcu_read_unlock(); | |
2360 | ||
2361 | cb->args[0] = idx; | |
2362 | return skb->len; | |
2363 | } | |
2364 | ||
815cccbf JF |
2365 | int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
2366 | struct net_device *dev, u16 mode) | |
2367 | { | |
2368 | struct nlmsghdr *nlh; | |
2369 | struct ifinfomsg *ifm; | |
2370 | struct nlattr *br_afspec; | |
2371 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; | |
898e5061 | 2372 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
815cccbf JF |
2373 | |
2374 | nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI); | |
2375 | if (nlh == NULL) | |
2376 | return -EMSGSIZE; | |
2377 | ||
2378 | ifm = nlmsg_data(nlh); | |
2379 | ifm->ifi_family = AF_BRIDGE; | |
2380 | ifm->__ifi_pad = 0; | |
2381 | ifm->ifi_type = dev->type; | |
2382 | ifm->ifi_index = dev->ifindex; | |
2383 | ifm->ifi_flags = dev_get_flags(dev); | |
2384 | ifm->ifi_change = 0; | |
2385 | ||
2386 | ||
2387 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || | |
2388 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || | |
2389 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || | |
898e5061 JP |
2390 | (br_dev && |
2391 | nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || | |
815cccbf JF |
2392 | (dev->addr_len && |
2393 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || | |
2394 | (dev->ifindex != dev->iflink && | |
2395 | nla_put_u32(skb, IFLA_LINK, dev->iflink))) | |
2396 | goto nla_put_failure; | |
2397 | ||
2398 | br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); | |
2399 | if (!br_afspec) | |
2400 | goto nla_put_failure; | |
2401 | ||
2402 | if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) || | |
2403 | nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { | |
2404 | nla_nest_cancel(skb, br_afspec); | |
2405 | goto nla_put_failure; | |
2406 | } | |
2407 | nla_nest_end(skb, br_afspec); | |
2408 | ||
2409 | return nlmsg_end(skb, nlh); | |
2410 | nla_put_failure: | |
2411 | nlmsg_cancel(skb, nlh); | |
2412 | return -EMSGSIZE; | |
2413 | } | |
2414 | EXPORT_SYMBOL(ndo_dflt_bridge_getlink); | |
2415 | ||
e5a55a89 JF |
2416 | static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) |
2417 | { | |
2418 | struct net *net = sock_net(skb->sk); | |
2419 | struct net_device *dev; | |
2420 | int idx = 0; | |
2421 | u32 portid = NETLINK_CB(cb->skb).portid; | |
2422 | u32 seq = cb->nlh->nlmsg_seq; | |
6cbdceeb VY |
2423 | struct nlattr *extfilt; |
2424 | u32 filter_mask = 0; | |
2425 | ||
f784dbb9 | 2426 | extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), |
6cbdceeb VY |
2427 | IFLA_EXT_MASK); |
2428 | if (extfilt) | |
2429 | filter_mask = nla_get_u32(extfilt); | |
e5a55a89 JF |
2430 | |
2431 | rcu_read_lock(); | |
2432 | for_each_netdev_rcu(net, dev) { | |
2433 | const struct net_device_ops *ops = dev->netdev_ops; | |
898e5061 | 2434 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
e5a55a89 | 2435 | |
898e5061 | 2436 | if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { |
25b1e679 | 2437 | if (idx >= cb->args[0] && |
898e5061 | 2438 | br_dev->netdev_ops->ndo_bridge_getlink( |
6cbdceeb | 2439 | skb, portid, seq, dev, filter_mask) < 0) |
e5a55a89 | 2440 | break; |
25b1e679 | 2441 | idx++; |
e5a55a89 JF |
2442 | } |
2443 | ||
2444 | if (ops->ndo_bridge_getlink) { | |
25b1e679 | 2445 | if (idx >= cb->args[0] && |
6cbdceeb VY |
2446 | ops->ndo_bridge_getlink(skb, portid, seq, dev, |
2447 | filter_mask) < 0) | |
e5a55a89 | 2448 | break; |
25b1e679 | 2449 | idx++; |
e5a55a89 JF |
2450 | } |
2451 | } | |
2452 | rcu_read_unlock(); | |
2453 | cb->args[0] = idx; | |
2454 | ||
2455 | return skb->len; | |
2456 | } | |
2457 | ||
2469ffd7 JF |
2458 | static inline size_t bridge_nlmsg_size(void) |
2459 | { | |
2460 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
2461 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ | |
2462 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ | |
2463 | + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ | |
2464 | + nla_total_size(sizeof(u32)) /* IFLA_MTU */ | |
2465 | + nla_total_size(sizeof(u32)) /* IFLA_LINK */ | |
2466 | + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ | |
2467 | + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ | |
2468 | + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ | |
2469 | + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ | |
2470 | + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ | |
2471 | } | |
2472 | ||
2473 | static int rtnl_bridge_notify(struct net_device *dev, u16 flags) | |
2474 | { | |
2475 | struct net *net = dev_net(dev); | |
898e5061 | 2476 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
2469ffd7 JF |
2477 | struct sk_buff *skb; |
2478 | int err = -EOPNOTSUPP; | |
2479 | ||
2480 | skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); | |
2481 | if (!skb) { | |
2482 | err = -ENOMEM; | |
2483 | goto errout; | |
2484 | } | |
2485 | ||
c38e01b8 | 2486 | if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) && |
898e5061 | 2487 | br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { |
6cbdceeb | 2488 | err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0); |
c38e01b8 JF |
2489 | if (err < 0) |
2490 | goto errout; | |
2491 | } | |
2469ffd7 | 2492 | |
c38e01b8 JF |
2493 | if ((flags & BRIDGE_FLAGS_SELF) && |
2494 | dev->netdev_ops->ndo_bridge_getlink) { | |
6cbdceeb | 2495 | err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0); |
c38e01b8 JF |
2496 | if (err < 0) |
2497 | goto errout; | |
2498 | } | |
2469ffd7 | 2499 | |
b4faf21b RP |
2500 | if (!skb->len) |
2501 | goto errout; | |
2502 | ||
2469ffd7 JF |
2503 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
2504 | return 0; | |
2505 | errout: | |
2506 | WARN_ON(err == -EMSGSIZE); | |
2507 | kfree_skb(skb); | |
b4faf21b RP |
2508 | if (err) |
2509 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); | |
2469ffd7 JF |
2510 | return err; |
2511 | } | |
2512 | ||
661d2967 | 2513 | static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
e5a55a89 JF |
2514 | { |
2515 | struct net *net = sock_net(skb->sk); | |
2516 | struct ifinfomsg *ifm; | |
2517 | struct net_device *dev; | |
2469ffd7 JF |
2518 | struct nlattr *br_spec, *attr = NULL; |
2519 | int rem, err = -EOPNOTSUPP; | |
c38e01b8 JF |
2520 | u16 oflags, flags = 0; |
2521 | bool have_flags = false; | |
e5a55a89 JF |
2522 | |
2523 | if (nlmsg_len(nlh) < sizeof(*ifm)) | |
2524 | return -EINVAL; | |
2525 | ||
2526 | ifm = nlmsg_data(nlh); | |
2527 | if (ifm->ifi_family != AF_BRIDGE) | |
2528 | return -EPFNOSUPPORT; | |
2529 | ||
2530 | dev = __dev_get_by_index(net, ifm->ifi_index); | |
2531 | if (!dev) { | |
2532 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); | |
2533 | return -ENODEV; | |
2534 | } | |
2535 | ||
2469ffd7 JF |
2536 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
2537 | if (br_spec) { | |
2538 | nla_for_each_nested(attr, br_spec, rem) { | |
2539 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { | |
c38e01b8 | 2540 | have_flags = true; |
2469ffd7 JF |
2541 | flags = nla_get_u16(attr); |
2542 | break; | |
2543 | } | |
2544 | } | |
2545 | } | |
2546 | ||
c38e01b8 JF |
2547 | oflags = flags; |
2548 | ||
2469ffd7 | 2549 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
898e5061 JP |
2550 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
2551 | ||
2552 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { | |
2469ffd7 JF |
2553 | err = -EOPNOTSUPP; |
2554 | goto out; | |
2555 | } | |
2556 | ||
898e5061 | 2557 | err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh); |
e5a55a89 JF |
2558 | if (err) |
2559 | goto out; | |
2469ffd7 JF |
2560 | |
2561 | flags &= ~BRIDGE_FLAGS_MASTER; | |
e5a55a89 JF |
2562 | } |
2563 | ||
2469ffd7 JF |
2564 | if ((flags & BRIDGE_FLAGS_SELF)) { |
2565 | if (!dev->netdev_ops->ndo_bridge_setlink) | |
2566 | err = -EOPNOTSUPP; | |
2567 | else | |
2568 | err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh); | |
2569 | ||
2570 | if (!err) | |
2571 | flags &= ~BRIDGE_FLAGS_SELF; | |
2572 | } | |
e5a55a89 | 2573 | |
c38e01b8 | 2574 | if (have_flags) |
2469ffd7 JF |
2575 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
2576 | /* Generate event to notify upper layer of bridge change */ | |
2577 | if (!err) | |
c38e01b8 | 2578 | err = rtnl_bridge_notify(dev, oflags); |
e5a55a89 JF |
2579 | out: |
2580 | return err; | |
2581 | } | |
2582 | ||
661d2967 | 2583 | static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) |
407af329 VY |
2584 | { |
2585 | struct net *net = sock_net(skb->sk); | |
2586 | struct ifinfomsg *ifm; | |
2587 | struct net_device *dev; | |
2588 | struct nlattr *br_spec, *attr = NULL; | |
2589 | int rem, err = -EOPNOTSUPP; | |
2590 | u16 oflags, flags = 0; | |
2591 | bool have_flags = false; | |
2592 | ||
2593 | if (nlmsg_len(nlh) < sizeof(*ifm)) | |
2594 | return -EINVAL; | |
2595 | ||
2596 | ifm = nlmsg_data(nlh); | |
2597 | if (ifm->ifi_family != AF_BRIDGE) | |
2598 | return -EPFNOSUPPORT; | |
2599 | ||
2600 | dev = __dev_get_by_index(net, ifm->ifi_index); | |
2601 | if (!dev) { | |
2602 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); | |
2603 | return -ENODEV; | |
2604 | } | |
2605 | ||
2606 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); | |
2607 | if (br_spec) { | |
2608 | nla_for_each_nested(attr, br_spec, rem) { | |
2609 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { | |
2610 | have_flags = true; | |
2611 | flags = nla_get_u16(attr); | |
2612 | break; | |
2613 | } | |
2614 | } | |
2615 | } | |
2616 | ||
2617 | oflags = flags; | |
2618 | ||
2619 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { | |
2620 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); | |
2621 | ||
2622 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { | |
2623 | err = -EOPNOTSUPP; | |
2624 | goto out; | |
2625 | } | |
2626 | ||
2627 | err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh); | |
2628 | if (err) | |
2629 | goto out; | |
2630 | ||
2631 | flags &= ~BRIDGE_FLAGS_MASTER; | |
2632 | } | |
2633 | ||
2634 | if ((flags & BRIDGE_FLAGS_SELF)) { | |
2635 | if (!dev->netdev_ops->ndo_bridge_dellink) | |
2636 | err = -EOPNOTSUPP; | |
2637 | else | |
2638 | err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh); | |
2639 | ||
2640 | if (!err) | |
2641 | flags &= ~BRIDGE_FLAGS_SELF; | |
2642 | } | |
2643 | ||
2644 | if (have_flags) | |
2645 | memcpy(nla_data(attr), &flags, sizeof(flags)); | |
2646 | /* Generate event to notify upper layer of bridge change */ | |
2647 | if (!err) | |
2648 | err = rtnl_bridge_notify(dev, oflags); | |
2649 | out: | |
2650 | return err; | |
2651 | } | |
2652 | ||
1da177e4 LT |
2653 | /* Process one rtnetlink message. */ |
2654 | ||
1d00a4eb | 2655 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
1da177e4 | 2656 | { |
3b1e0a65 | 2657 | struct net *net = sock_net(skb->sk); |
e2849863 | 2658 | rtnl_doit_func doit; |
1da177e4 | 2659 | int sz_idx, kind; |
1da177e4 LT |
2660 | int family; |
2661 | int type; | |
2907c35f | 2662 | int err; |
1da177e4 | 2663 | |
1da177e4 | 2664 | type = nlh->nlmsg_type; |
1da177e4 | 2665 | if (type > RTM_MAX) |
038890fe | 2666 | return -EOPNOTSUPP; |
1da177e4 LT |
2667 | |
2668 | type -= RTM_BASE; | |
2669 | ||
2670 | /* All the messages must have at least 1 byte length */ | |
573ce260 | 2671 | if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) |
1da177e4 LT |
2672 | return 0; |
2673 | ||
573ce260 | 2674 | family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; |
1da177e4 LT |
2675 | sz_idx = type>>2; |
2676 | kind = type&3; | |
2677 | ||
1141a455 | 2678 | if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) |
1d00a4eb | 2679 | return -EPERM; |
1da177e4 | 2680 | |
b8f3ab42 | 2681 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
97c53cac | 2682 | struct sock *rtnl; |
e2849863 | 2683 | rtnl_dumpit_func dumpit; |
c7ac8679 GR |
2684 | rtnl_calcit_func calcit; |
2685 | u16 min_dump_alloc = 0; | |
1da177e4 | 2686 | |
e2849863 TG |
2687 | dumpit = rtnl_get_dumpit(family, type); |
2688 | if (dumpit == NULL) | |
038890fe | 2689 | return -EOPNOTSUPP; |
c7ac8679 GR |
2690 | calcit = rtnl_get_calcit(family, type); |
2691 | if (calcit) | |
115c9b81 | 2692 | min_dump_alloc = calcit(skb, nlh); |
9ac4a169 | 2693 | |
2907c35f | 2694 | __rtnl_unlock(); |
97c53cac | 2695 | rtnl = net->rtnl; |
80d326fa PNA |
2696 | { |
2697 | struct netlink_dump_control c = { | |
2698 | .dump = dumpit, | |
2699 | .min_dump_alloc = min_dump_alloc, | |
2700 | }; | |
2701 | err = netlink_dump_start(rtnl, skb, nlh, &c); | |
2702 | } | |
2907c35f ED |
2703 | rtnl_lock(); |
2704 | return err; | |
1da177e4 LT |
2705 | } |
2706 | ||
e2849863 TG |
2707 | doit = rtnl_get_doit(family, type); |
2708 | if (doit == NULL) | |
038890fe | 2709 | return -EOPNOTSUPP; |
1da177e4 | 2710 | |
661d2967 | 2711 | return doit(skb, nlh); |
1da177e4 LT |
2712 | } |
2713 | ||
cd40b7d3 | 2714 | static void rtnetlink_rcv(struct sk_buff *skb) |
1da177e4 | 2715 | { |
cd40b7d3 DL |
2716 | rtnl_lock(); |
2717 | netlink_rcv_skb(skb, &rtnetlink_rcv_msg); | |
2718 | rtnl_unlock(); | |
1da177e4 LT |
2719 | } |
2720 | ||
1da177e4 LT |
2721 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
2722 | { | |
2723 | struct net_device *dev = ptr; | |
e9dc8653 | 2724 | |
1da177e4 | 2725 | switch (event) { |
1da177e4 LT |
2726 | case NETDEV_UP: |
2727 | case NETDEV_DOWN: | |
10de05af | 2728 | case NETDEV_PRE_UP: |
d90a909e EB |
2729 | case NETDEV_POST_INIT: |
2730 | case NETDEV_REGISTER: | |
1da177e4 | 2731 | case NETDEV_CHANGE: |
755d0e77 | 2732 | case NETDEV_PRE_TYPE_CHANGE: |
1da177e4 | 2733 | case NETDEV_GOING_DOWN: |
a2835763 | 2734 | case NETDEV_UNREGISTER: |
0115e8e3 | 2735 | case NETDEV_UNREGISTER_FINAL: |
ac3d3f81 AW |
2736 | case NETDEV_RELEASE: |
2737 | case NETDEV_JOIN: | |
1da177e4 LT |
2738 | break; |
2739 | default: | |
2740 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0); | |
2741 | break; | |
2742 | } | |
2743 | return NOTIFY_DONE; | |
2744 | } | |
2745 | ||
2746 | static struct notifier_block rtnetlink_dev_notifier = { | |
2747 | .notifier_call = rtnetlink_event, | |
2748 | }; | |
2749 | ||
97c53cac | 2750 | |
2c8c1e72 | 2751 | static int __net_init rtnetlink_net_init(struct net *net) |
97c53cac DL |
2752 | { |
2753 | struct sock *sk; | |
a31f2d17 PNA |
2754 | struct netlink_kernel_cfg cfg = { |
2755 | .groups = RTNLGRP_MAX, | |
2756 | .input = rtnetlink_rcv, | |
2757 | .cb_mutex = &rtnl_mutex, | |
9785e10a | 2758 | .flags = NL_CFG_F_NONROOT_RECV, |
a31f2d17 PNA |
2759 | }; |
2760 | ||
9f00d977 | 2761 | sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); |
97c53cac DL |
2762 | if (!sk) |
2763 | return -ENOMEM; | |
97c53cac DL |
2764 | net->rtnl = sk; |
2765 | return 0; | |
2766 | } | |
2767 | ||
2c8c1e72 | 2768 | static void __net_exit rtnetlink_net_exit(struct net *net) |
97c53cac | 2769 | { |
775516bf DL |
2770 | netlink_kernel_release(net->rtnl); |
2771 | net->rtnl = NULL; | |
97c53cac DL |
2772 | } |
2773 | ||
2774 | static struct pernet_operations rtnetlink_net_ops = { | |
2775 | .init = rtnetlink_net_init, | |
2776 | .exit = rtnetlink_net_exit, | |
2777 | }; | |
2778 | ||
1da177e4 LT |
2779 | void __init rtnetlink_init(void) |
2780 | { | |
97c53cac | 2781 | if (register_pernet_subsys(&rtnetlink_net_ops)) |
1da177e4 | 2782 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
97c53cac | 2783 | |
1da177e4 | 2784 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
340d17fc | 2785 | |
c7ac8679 GR |
2786 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, |
2787 | rtnl_dump_ifinfo, rtnl_calcit); | |
2788 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL); | |
2789 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL); | |
2790 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL); | |
687ad8cc | 2791 | |
c7ac8679 GR |
2792 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL); |
2793 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL); | |
77162022 JF |
2794 | |
2795 | rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL); | |
2796 | rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL); | |
2797 | rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL); | |
e5a55a89 JF |
2798 | |
2799 | rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL); | |
407af329 | 2800 | rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL); |
e5a55a89 | 2801 | rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL); |
1da177e4 LT |
2802 | } |
2803 |