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