]>
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> |
ebc08a6f | 38 | #include <linux/pci.h> |
1da177e4 LT |
39 | |
40 | #include <asm/uaccess.h> | |
41 | #include <asm/system.h> | |
1da177e4 LT |
42 | |
43 | #include <linux/inet.h> | |
44 | #include <linux/netdevice.h> | |
45 | #include <net/ip.h> | |
46 | #include <net/protocol.h> | |
47 | #include <net/arp.h> | |
48 | #include <net/route.h> | |
49 | #include <net/udp.h> | |
50 | #include <net/sock.h> | |
51 | #include <net/pkt_sched.h> | |
14c0b97d | 52 | #include <net/fib_rules.h> |
e2849863 | 53 | #include <net/rtnetlink.h> |
30ffee84 | 54 | #include <net/net_namespace.h> |
1da177e4 | 55 | |
e0d087af | 56 | struct rtnl_link { |
e2849863 TG |
57 | rtnl_doit_func doit; |
58 | rtnl_dumpit_func dumpit; | |
59 | }; | |
60 | ||
6756ae4b | 61 | static DEFINE_MUTEX(rtnl_mutex); |
1da177e4 LT |
62 | |
63 | void rtnl_lock(void) | |
64 | { | |
6756ae4b | 65 | mutex_lock(&rtnl_mutex); |
1da177e4 | 66 | } |
e0d087af | 67 | EXPORT_SYMBOL(rtnl_lock); |
1da177e4 | 68 | |
6756ae4b | 69 | void __rtnl_unlock(void) |
1da177e4 | 70 | { |
6756ae4b | 71 | mutex_unlock(&rtnl_mutex); |
1da177e4 | 72 | } |
6756ae4b | 73 | |
1da177e4 LT |
74 | void rtnl_unlock(void) |
75 | { | |
58ec3b4d | 76 | /* This fellow will unlock it for us. */ |
1da177e4 LT |
77 | netdev_run_todo(); |
78 | } | |
e0d087af | 79 | EXPORT_SYMBOL(rtnl_unlock); |
1da177e4 | 80 | |
6756ae4b SH |
81 | int rtnl_trylock(void) |
82 | { | |
83 | return mutex_trylock(&rtnl_mutex); | |
84 | } | |
e0d087af | 85 | EXPORT_SYMBOL(rtnl_trylock); |
6756ae4b | 86 | |
c9c1014b PM |
87 | int rtnl_is_locked(void) |
88 | { | |
89 | return mutex_is_locked(&rtnl_mutex); | |
90 | } | |
e0d087af | 91 | EXPORT_SYMBOL(rtnl_is_locked); |
c9c1014b | 92 | |
a898def2 PM |
93 | #ifdef CONFIG_PROVE_LOCKING |
94 | int lockdep_rtnl_is_held(void) | |
95 | { | |
96 | return lockdep_is_held(&rtnl_mutex); | |
97 | } | |
98 | EXPORT_SYMBOL(lockdep_rtnl_is_held); | |
99 | #endif /* #ifdef CONFIG_PROVE_LOCKING */ | |
100 | ||
42bad1da | 101 | static struct rtnl_link *rtnl_msg_handlers[NPROTO]; |
e2849863 TG |
102 | |
103 | static inline int rtm_msgindex(int msgtype) | |
104 | { | |
105 | int msgindex = msgtype - RTM_BASE; | |
106 | ||
107 | /* | |
108 | * msgindex < 0 implies someone tried to register a netlink | |
109 | * control code. msgindex >= RTM_NR_MSGTYPES may indicate that | |
110 | * the message type has not been added to linux/rtnetlink.h | |
111 | */ | |
112 | BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); | |
113 | ||
114 | return msgindex; | |
115 | } | |
116 | ||
117 | static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) | |
118 | { | |
119 | struct rtnl_link *tab; | |
120 | ||
121 | tab = rtnl_msg_handlers[protocol]; | |
51057f2f | 122 | if (tab == NULL || tab[msgindex].doit == NULL) |
e2849863 TG |
123 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
124 | ||
51057f2f | 125 | return tab ? tab[msgindex].doit : NULL; |
e2849863 TG |
126 | } |
127 | ||
128 | static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) | |
129 | { | |
130 | struct rtnl_link *tab; | |
131 | ||
132 | tab = rtnl_msg_handlers[protocol]; | |
51057f2f | 133 | if (tab == NULL || tab[msgindex].dumpit == NULL) |
e2849863 TG |
134 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
135 | ||
51057f2f | 136 | return tab ? tab[msgindex].dumpit : NULL; |
e2849863 TG |
137 | } |
138 | ||
139 | /** | |
140 | * __rtnl_register - Register a rtnetlink message type | |
141 | * @protocol: Protocol family or PF_UNSPEC | |
142 | * @msgtype: rtnetlink message type | |
143 | * @doit: Function pointer called for each request message | |
144 | * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message | |
145 | * | |
146 | * Registers the specified function pointers (at least one of them has | |
147 | * to be non-NULL) to be called whenever a request message for the | |
148 | * specified protocol family and message type is received. | |
149 | * | |
150 | * The special protocol family PF_UNSPEC may be used to define fallback | |
151 | * function pointers for the case when no entry for the specific protocol | |
152 | * family exists. | |
153 | * | |
154 | * Returns 0 on success or a negative error code. | |
155 | */ | |
156 | int __rtnl_register(int protocol, int msgtype, | |
157 | rtnl_doit_func doit, rtnl_dumpit_func dumpit) | |
158 | { | |
159 | struct rtnl_link *tab; | |
160 | int msgindex; | |
161 | ||
162 | BUG_ON(protocol < 0 || protocol >= NPROTO); | |
163 | msgindex = rtm_msgindex(msgtype); | |
164 | ||
165 | tab = rtnl_msg_handlers[protocol]; | |
166 | if (tab == NULL) { | |
167 | tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); | |
168 | if (tab == NULL) | |
169 | return -ENOBUFS; | |
170 | ||
171 | rtnl_msg_handlers[protocol] = tab; | |
172 | } | |
173 | ||
174 | if (doit) | |
175 | tab[msgindex].doit = doit; | |
176 | ||
177 | if (dumpit) | |
178 | tab[msgindex].dumpit = dumpit; | |
179 | ||
180 | return 0; | |
181 | } | |
e2849863 TG |
182 | EXPORT_SYMBOL_GPL(__rtnl_register); |
183 | ||
184 | /** | |
185 | * rtnl_register - Register a rtnetlink message type | |
186 | * | |
187 | * Identical to __rtnl_register() but panics on failure. This is useful | |
188 | * as failure of this function is very unlikely, it can only happen due | |
189 | * to lack of memory when allocating the chain to store all message | |
190 | * handlers for a protocol. Meant for use in init functions where lack | |
191 | * of memory implies no sense in continueing. | |
192 | */ | |
193 | void rtnl_register(int protocol, int msgtype, | |
194 | rtnl_doit_func doit, rtnl_dumpit_func dumpit) | |
195 | { | |
196 | if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0) | |
197 | panic("Unable to register rtnetlink message handler, " | |
198 | "protocol = %d, message type = %d\n", | |
199 | protocol, msgtype); | |
200 | } | |
e2849863 TG |
201 | EXPORT_SYMBOL_GPL(rtnl_register); |
202 | ||
203 | /** | |
204 | * rtnl_unregister - Unregister a rtnetlink message type | |
205 | * @protocol: Protocol family or PF_UNSPEC | |
206 | * @msgtype: rtnetlink message type | |
207 | * | |
208 | * Returns 0 on success or a negative error code. | |
209 | */ | |
210 | int rtnl_unregister(int protocol, int msgtype) | |
211 | { | |
212 | int msgindex; | |
213 | ||
214 | BUG_ON(protocol < 0 || protocol >= NPROTO); | |
215 | msgindex = rtm_msgindex(msgtype); | |
216 | ||
217 | if (rtnl_msg_handlers[protocol] == NULL) | |
218 | return -ENOENT; | |
219 | ||
220 | rtnl_msg_handlers[protocol][msgindex].doit = NULL; | |
221 | rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; | |
222 | ||
223 | return 0; | |
224 | } | |
e2849863 TG |
225 | EXPORT_SYMBOL_GPL(rtnl_unregister); |
226 | ||
227 | /** | |
228 | * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol | |
229 | * @protocol : Protocol family or PF_UNSPEC | |
230 | * | |
231 | * Identical to calling rtnl_unregster() for all registered message types | |
232 | * of a certain protocol family. | |
233 | */ | |
234 | void rtnl_unregister_all(int protocol) | |
235 | { | |
236 | BUG_ON(protocol < 0 || protocol >= NPROTO); | |
237 | ||
238 | kfree(rtnl_msg_handlers[protocol]); | |
239 | rtnl_msg_handlers[protocol] = NULL; | |
240 | } | |
e2849863 | 241 | EXPORT_SYMBOL_GPL(rtnl_unregister_all); |
1da177e4 | 242 | |
38f7b870 PM |
243 | static LIST_HEAD(link_ops); |
244 | ||
245 | /** | |
246 | * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. | |
247 | * @ops: struct rtnl_link_ops * to register | |
248 | * | |
249 | * The caller must hold the rtnl_mutex. This function should be used | |
250 | * by drivers that create devices during module initialization. It | |
251 | * must be called before registering the devices. | |
252 | * | |
253 | * Returns 0 on success or a negative error code. | |
254 | */ | |
255 | int __rtnl_link_register(struct rtnl_link_ops *ops) | |
256 | { | |
2d85cba2 | 257 | if (!ops->dellink) |
23289a37 | 258 | ops->dellink = unregister_netdevice_queue; |
2d85cba2 | 259 | |
38f7b870 PM |
260 | list_add_tail(&ops->list, &link_ops); |
261 | return 0; | |
262 | } | |
38f7b870 PM |
263 | EXPORT_SYMBOL_GPL(__rtnl_link_register); |
264 | ||
265 | /** | |
266 | * rtnl_link_register - Register rtnl_link_ops with rtnetlink. | |
267 | * @ops: struct rtnl_link_ops * to register | |
268 | * | |
269 | * Returns 0 on success or a negative error code. | |
270 | */ | |
271 | int rtnl_link_register(struct rtnl_link_ops *ops) | |
272 | { | |
273 | int err; | |
274 | ||
275 | rtnl_lock(); | |
276 | err = __rtnl_link_register(ops); | |
277 | rtnl_unlock(); | |
278 | return err; | |
279 | } | |
38f7b870 PM |
280 | EXPORT_SYMBOL_GPL(rtnl_link_register); |
281 | ||
669f87ba PE |
282 | static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) |
283 | { | |
284 | struct net_device *dev; | |
23289a37 ED |
285 | LIST_HEAD(list_kill); |
286 | ||
669f87ba | 287 | for_each_netdev(net, dev) { |
23289a37 ED |
288 | if (dev->rtnl_link_ops == ops) |
289 | ops->dellink(dev, &list_kill); | |
669f87ba | 290 | } |
23289a37 | 291 | unregister_netdevice_many(&list_kill); |
669f87ba PE |
292 | } |
293 | ||
294 | void rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) | |
295 | { | |
296 | rtnl_lock(); | |
297 | __rtnl_kill_links(net, ops); | |
298 | rtnl_unlock(); | |
299 | } | |
300 | EXPORT_SYMBOL_GPL(rtnl_kill_links); | |
301 | ||
38f7b870 PM |
302 | /** |
303 | * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. | |
304 | * @ops: struct rtnl_link_ops * to unregister | |
305 | * | |
2d85cba2 | 306 | * The caller must hold the rtnl_mutex. |
38f7b870 PM |
307 | */ |
308 | void __rtnl_link_unregister(struct rtnl_link_ops *ops) | |
309 | { | |
881d966b | 310 | struct net *net; |
2d85cba2 | 311 | |
881d966b | 312 | for_each_net(net) { |
669f87ba | 313 | __rtnl_kill_links(net, ops); |
2d85cba2 | 314 | } |
38f7b870 PM |
315 | list_del(&ops->list); |
316 | } | |
38f7b870 PM |
317 | EXPORT_SYMBOL_GPL(__rtnl_link_unregister); |
318 | ||
319 | /** | |
320 | * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. | |
321 | * @ops: struct rtnl_link_ops * to unregister | |
322 | */ | |
323 | void rtnl_link_unregister(struct rtnl_link_ops *ops) | |
324 | { | |
325 | rtnl_lock(); | |
326 | __rtnl_link_unregister(ops); | |
327 | rtnl_unlock(); | |
328 | } | |
38f7b870 PM |
329 | EXPORT_SYMBOL_GPL(rtnl_link_unregister); |
330 | ||
331 | static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) | |
332 | { | |
333 | const struct rtnl_link_ops *ops; | |
334 | ||
335 | list_for_each_entry(ops, &link_ops, list) { | |
336 | if (!strcmp(ops->kind, kind)) | |
337 | return ops; | |
338 | } | |
339 | return NULL; | |
340 | } | |
341 | ||
342 | static size_t rtnl_link_get_size(const struct net_device *dev) | |
343 | { | |
344 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; | |
345 | size_t size; | |
346 | ||
347 | if (!ops) | |
348 | return 0; | |
349 | ||
350 | size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ | |
351 | nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ | |
352 | ||
353 | if (ops->get_size) | |
354 | /* IFLA_INFO_DATA + nested data */ | |
355 | size += nlmsg_total_size(sizeof(struct nlattr)) + | |
356 | ops->get_size(dev); | |
357 | ||
358 | if (ops->get_xstats_size) | |
359 | size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */ | |
360 | ||
361 | return size; | |
362 | } | |
363 | ||
364 | static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) | |
365 | { | |
366 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; | |
367 | struct nlattr *linkinfo, *data; | |
368 | int err = -EMSGSIZE; | |
369 | ||
370 | linkinfo = nla_nest_start(skb, IFLA_LINKINFO); | |
371 | if (linkinfo == NULL) | |
372 | goto out; | |
373 | ||
374 | if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) | |
375 | goto err_cancel_link; | |
376 | if (ops->fill_xstats) { | |
377 | err = ops->fill_xstats(skb, dev); | |
378 | if (err < 0) | |
379 | goto err_cancel_link; | |
380 | } | |
381 | if (ops->fill_info) { | |
382 | data = nla_nest_start(skb, IFLA_INFO_DATA); | |
383 | if (data == NULL) | |
384 | goto err_cancel_link; | |
385 | err = ops->fill_info(skb, dev); | |
386 | if (err < 0) | |
387 | goto err_cancel_data; | |
388 | nla_nest_end(skb, data); | |
389 | } | |
390 | ||
391 | nla_nest_end(skb, linkinfo); | |
392 | return 0; | |
393 | ||
394 | err_cancel_data: | |
395 | nla_nest_cancel(skb, data); | |
396 | err_cancel_link: | |
397 | nla_nest_cancel(skb, linkinfo); | |
398 | out: | |
399 | return err; | |
400 | } | |
401 | ||
db46edc6 | 402 | static const int rtm_min[RTM_NR_FAMILIES] = |
1da177e4 | 403 | { |
f90a0a74 TG |
404 | [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), |
405 | [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), | |
406 | [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), | |
14c0b97d | 407 | [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), |
f90a0a74 TG |
408 | [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
409 | [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), | |
410 | [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), | |
411 | [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), | |
f90a0a74 TG |
412 | [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
413 | [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), | |
1da177e4 LT |
414 | }; |
415 | ||
db46edc6 | 416 | static const int rta_max[RTM_NR_FAMILIES] = |
1da177e4 | 417 | { |
f90a0a74 TG |
418 | [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, |
419 | [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, | |
420 | [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, | |
14c0b97d | 421 | [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, |
f90a0a74 TG |
422 | [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, |
423 | [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, | |
424 | [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, | |
425 | [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, | |
1da177e4 LT |
426 | }; |
427 | ||
428 | void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) | |
429 | { | |
430 | struct rtattr *rta; | |
431 | int size = RTA_LENGTH(attrlen); | |
432 | ||
e0d087af | 433 | rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size)); |
1da177e4 LT |
434 | rta->rta_type = attrtype; |
435 | rta->rta_len = size; | |
436 | memcpy(RTA_DATA(rta), data, attrlen); | |
b3563c4f | 437 | memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); |
1da177e4 | 438 | } |
e0d087af | 439 | EXPORT_SYMBOL(__rta_fill); |
1da177e4 | 440 | |
97c53cac | 441 | int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo) |
1da177e4 | 442 | { |
97c53cac | 443 | struct sock *rtnl = net->rtnl; |
1da177e4 LT |
444 | int err = 0; |
445 | ||
ac6d439d | 446 | NETLINK_CB(skb).dst_group = group; |
1da177e4 LT |
447 | if (echo) |
448 | atomic_inc(&skb->users); | |
449 | netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); | |
450 | if (echo) | |
451 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); | |
452 | return err; | |
453 | } | |
454 | ||
97c53cac | 455 | int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) |
2942e900 | 456 | { |
97c53cac DL |
457 | struct sock *rtnl = net->rtnl; |
458 | ||
2942e900 TG |
459 | return nlmsg_unicast(rtnl, skb, pid); |
460 | } | |
e0d087af | 461 | EXPORT_SYMBOL(rtnl_unicast); |
2942e900 | 462 | |
1ce85fe4 PNA |
463 | void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, |
464 | struct nlmsghdr *nlh, gfp_t flags) | |
97676b6b | 465 | { |
97c53cac | 466 | struct sock *rtnl = net->rtnl; |
97676b6b TG |
467 | int report = 0; |
468 | ||
469 | if (nlh) | |
470 | report = nlmsg_report(nlh); | |
471 | ||
1ce85fe4 | 472 | nlmsg_notify(rtnl, skb, pid, group, report, flags); |
97676b6b | 473 | } |
e0d087af | 474 | EXPORT_SYMBOL(rtnl_notify); |
97676b6b | 475 | |
97c53cac | 476 | void rtnl_set_sk_err(struct net *net, u32 group, int error) |
97676b6b | 477 | { |
97c53cac DL |
478 | struct sock *rtnl = net->rtnl; |
479 | ||
97676b6b TG |
480 | netlink_set_err(rtnl, 0, group, error); |
481 | } | |
e0d087af | 482 | EXPORT_SYMBOL(rtnl_set_sk_err); |
97676b6b | 483 | |
1da177e4 LT |
484 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) |
485 | { | |
2d7202bf TG |
486 | struct nlattr *mx; |
487 | int i, valid = 0; | |
488 | ||
489 | mx = nla_nest_start(skb, RTA_METRICS); | |
490 | if (mx == NULL) | |
491 | return -ENOBUFS; | |
492 | ||
493 | for (i = 0; i < RTAX_MAX; i++) { | |
494 | if (metrics[i]) { | |
495 | valid++; | |
496 | NLA_PUT_U32(skb, i+1, metrics[i]); | |
497 | } | |
1da177e4 | 498 | } |
1da177e4 | 499 | |
a57d27fc DM |
500 | if (!valid) { |
501 | nla_nest_cancel(skb, mx); | |
502 | return 0; | |
503 | } | |
2d7202bf TG |
504 | |
505 | return nla_nest_end(skb, mx); | |
506 | ||
507 | nla_put_failure: | |
bc3ed28c TG |
508 | nla_nest_cancel(skb, mx); |
509 | return -EMSGSIZE; | |
1da177e4 | 510 | } |
e0d087af | 511 | EXPORT_SYMBOL(rtnetlink_put_metrics); |
1da177e4 | 512 | |
e3703b3d TG |
513 | int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, |
514 | u32 ts, u32 tsage, long expires, u32 error) | |
515 | { | |
516 | struct rta_cacheinfo ci = { | |
517 | .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), | |
518 | .rta_used = dst->__use, | |
519 | .rta_clntref = atomic_read(&(dst->__refcnt)), | |
520 | .rta_error = error, | |
521 | .rta_id = id, | |
522 | .rta_ts = ts, | |
523 | .rta_tsage = tsage, | |
524 | }; | |
525 | ||
526 | if (expires) | |
527 | ci.rta_expires = jiffies_to_clock_t(expires); | |
528 | ||
529 | return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); | |
530 | } | |
e3703b3d | 531 | EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); |
1da177e4 | 532 | |
93b2d4a2 | 533 | static void set_operstate(struct net_device *dev, unsigned char transition) |
b00055aa SR |
534 | { |
535 | unsigned char operstate = dev->operstate; | |
536 | ||
e0d087af | 537 | switch (transition) { |
b00055aa SR |
538 | case IF_OPER_UP: |
539 | if ((operstate == IF_OPER_DORMANT || | |
540 | operstate == IF_OPER_UNKNOWN) && | |
541 | !netif_dormant(dev)) | |
542 | operstate = IF_OPER_UP; | |
543 | break; | |
544 | ||
545 | case IF_OPER_DORMANT: | |
546 | if (operstate == IF_OPER_UP || | |
547 | operstate == IF_OPER_UNKNOWN) | |
548 | operstate = IF_OPER_DORMANT; | |
549 | break; | |
3ff50b79 | 550 | } |
b00055aa SR |
551 | |
552 | if (dev->operstate != operstate) { | |
553 | write_lock_bh(&dev_base_lock); | |
554 | dev->operstate = operstate; | |
555 | write_unlock_bh(&dev_base_lock); | |
93b2d4a2 DM |
556 | netdev_state_change(dev); |
557 | } | |
b00055aa SR |
558 | } |
559 | ||
3729d502 PM |
560 | static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, |
561 | const struct ifinfomsg *ifm) | |
562 | { | |
563 | unsigned int flags = ifm->ifi_flags; | |
564 | ||
565 | /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ | |
566 | if (ifm->ifi_change) | |
567 | flags = (flags & ifm->ifi_change) | | |
568 | (dev->flags & ~ifm->ifi_change); | |
569 | ||
570 | return flags; | |
571 | } | |
572 | ||
b60c5115 | 573 | static void copy_rtnl_link_stats(struct rtnl_link_stats *a, |
eeda3fd6 | 574 | const struct net_device_stats *b) |
1da177e4 | 575 | { |
b60c5115 TG |
576 | a->rx_packets = b->rx_packets; |
577 | a->tx_packets = b->tx_packets; | |
578 | a->rx_bytes = b->rx_bytes; | |
579 | a->tx_bytes = b->tx_bytes; | |
580 | a->rx_errors = b->rx_errors; | |
581 | a->tx_errors = b->tx_errors; | |
582 | a->rx_dropped = b->rx_dropped; | |
583 | a->tx_dropped = b->tx_dropped; | |
584 | ||
585 | a->multicast = b->multicast; | |
586 | a->collisions = b->collisions; | |
587 | ||
588 | a->rx_length_errors = b->rx_length_errors; | |
589 | a->rx_over_errors = b->rx_over_errors; | |
590 | a->rx_crc_errors = b->rx_crc_errors; | |
591 | a->rx_frame_errors = b->rx_frame_errors; | |
592 | a->rx_fifo_errors = b->rx_fifo_errors; | |
593 | a->rx_missed_errors = b->rx_missed_errors; | |
594 | ||
595 | a->tx_aborted_errors = b->tx_aborted_errors; | |
596 | a->tx_carrier_errors = b->tx_carrier_errors; | |
597 | a->tx_fifo_errors = b->tx_fifo_errors; | |
598 | a->tx_heartbeat_errors = b->tx_heartbeat_errors; | |
599 | a->tx_window_errors = b->tx_window_errors; | |
600 | ||
601 | a->rx_compressed = b->rx_compressed; | |
602 | a->tx_compressed = b->tx_compressed; | |
10708f37 JE |
603 | } |
604 | ||
14a4b42b | 605 | static void copy_rtnl_link_stats64(void *v, const struct net_device_stats *b) |
10708f37 | 606 | { |
14a4b42b | 607 | struct rtnl_link_stats64 a; |
10708f37 | 608 | |
14a4b42b JE |
609 | a.rx_packets = b->rx_packets; |
610 | a.tx_packets = b->tx_packets; | |
611 | a.rx_bytes = b->rx_bytes; | |
612 | a.tx_bytes = b->tx_bytes; | |
613 | a.rx_errors = b->rx_errors; | |
614 | a.tx_errors = b->tx_errors; | |
615 | a.rx_dropped = b->rx_dropped; | |
616 | a.tx_dropped = b->tx_dropped; | |
10708f37 | 617 | |
14a4b42b JE |
618 | a.multicast = b->multicast; |
619 | a.collisions = b->collisions; | |
10708f37 | 620 | |
14a4b42b JE |
621 | a.rx_length_errors = b->rx_length_errors; |
622 | a.rx_over_errors = b->rx_over_errors; | |
623 | a.rx_crc_errors = b->rx_crc_errors; | |
624 | a.rx_frame_errors = b->rx_frame_errors; | |
625 | a.rx_fifo_errors = b->rx_fifo_errors; | |
626 | a.rx_missed_errors = b->rx_missed_errors; | |
10708f37 | 627 | |
14a4b42b JE |
628 | a.tx_aborted_errors = b->tx_aborted_errors; |
629 | a.tx_carrier_errors = b->tx_carrier_errors; | |
630 | a.tx_fifo_errors = b->tx_fifo_errors; | |
631 | a.tx_heartbeat_errors = b->tx_heartbeat_errors; | |
632 | a.tx_window_errors = b->tx_window_errors; | |
633 | ||
634 | a.rx_compressed = b->rx_compressed; | |
635 | a.tx_compressed = b->tx_compressed; | |
636 | memcpy(v, &a, sizeof(a)); | |
10708f37 | 637 | } |
1da177e4 | 638 | |
ebc08a6f WM |
639 | static inline int rtnl_vfinfo_size(const struct net_device *dev) |
640 | { | |
641 | if (dev->dev.parent && dev_is_pci(dev->dev.parent)) | |
642 | return dev_num_vf(dev->dev.parent) * | |
643 | sizeof(struct ifla_vf_info); | |
644 | else | |
645 | return 0; | |
646 | } | |
647 | ||
38f7b870 | 648 | static inline size_t if_nlmsg_size(const struct net_device *dev) |
339bf98f TG |
649 | { |
650 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
651 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ | |
0b815a1a | 652 | + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ |
339bf98f TG |
653 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ |
654 | + nla_total_size(sizeof(struct rtnl_link_ifmap)) | |
655 | + nla_total_size(sizeof(struct rtnl_link_stats)) | |
656 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ | |
657 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ | |
658 | + nla_total_size(4) /* IFLA_TXQLEN */ | |
659 | + nla_total_size(4) /* IFLA_WEIGHT */ | |
660 | + nla_total_size(4) /* IFLA_MTU */ | |
661 | + nla_total_size(4) /* IFLA_LINK */ | |
662 | + nla_total_size(4) /* IFLA_MASTER */ | |
663 | + nla_total_size(1) /* IFLA_OPERSTATE */ | |
38f7b870 | 664 | + nla_total_size(1) /* IFLA_LINKMODE */ |
ebc08a6f WM |
665 | + nla_total_size(4) /* IFLA_NUM_VF */ |
666 | + nla_total_size(rtnl_vfinfo_size(dev)) /* IFLA_VFINFO */ | |
38f7b870 | 667 | + rtnl_link_get_size(dev); /* IFLA_LINKINFO */ |
339bf98f TG |
668 | } |
669 | ||
b60c5115 | 670 | static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, |
575c3e2a PM |
671 | int type, u32 pid, u32 seq, u32 change, |
672 | unsigned int flags) | |
b60c5115 TG |
673 | { |
674 | struct ifinfomsg *ifm; | |
675 | struct nlmsghdr *nlh; | |
eeda3fd6 | 676 | const struct net_device_stats *stats; |
96e74088 | 677 | struct nlattr *attr; |
1da177e4 | 678 | |
b60c5115 TG |
679 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
680 | if (nlh == NULL) | |
26932566 | 681 | return -EMSGSIZE; |
1da177e4 | 682 | |
b60c5115 TG |
683 | ifm = nlmsg_data(nlh); |
684 | ifm->ifi_family = AF_UNSPEC; | |
685 | ifm->__ifi_pad = 0; | |
686 | ifm->ifi_type = dev->type; | |
687 | ifm->ifi_index = dev->ifindex; | |
688 | ifm->ifi_flags = dev_get_flags(dev); | |
689 | ifm->ifi_change = change; | |
690 | ||
691 | NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); | |
692 | NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); | |
b60c5115 TG |
693 | NLA_PUT_U8(skb, IFLA_OPERSTATE, |
694 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN); | |
695 | NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); | |
696 | NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); | |
697 | ||
698 | if (dev->ifindex != dev->iflink) | |
699 | NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); | |
700 | ||
701 | if (dev->master) | |
702 | NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); | |
1da177e4 | 703 | |
af356afa PM |
704 | if (dev->qdisc) |
705 | NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id); | |
b00055aa | 706 | |
0b815a1a SH |
707 | if (dev->ifalias) |
708 | NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias); | |
709 | ||
1da177e4 LT |
710 | if (1) { |
711 | struct rtnl_link_ifmap map = { | |
712 | .mem_start = dev->mem_start, | |
713 | .mem_end = dev->mem_end, | |
714 | .base_addr = dev->base_addr, | |
715 | .irq = dev->irq, | |
716 | .dma = dev->dma, | |
717 | .port = dev->if_port, | |
718 | }; | |
b60c5115 | 719 | NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); |
1da177e4 LT |
720 | } |
721 | ||
722 | if (dev->addr_len) { | |
b60c5115 TG |
723 | NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); |
724 | NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); | |
1da177e4 LT |
725 | } |
726 | ||
96e74088 PE |
727 | attr = nla_reserve(skb, IFLA_STATS, |
728 | sizeof(struct rtnl_link_stats)); | |
729 | if (attr == NULL) | |
730 | goto nla_put_failure; | |
b60c5115 | 731 | |
eeda3fd6 | 732 | stats = dev_get_stats(dev); |
96e74088 | 733 | copy_rtnl_link_stats(nla_data(attr), stats); |
1da177e4 | 734 | |
10708f37 JE |
735 | attr = nla_reserve(skb, IFLA_STATS64, |
736 | sizeof(struct rtnl_link_stats64)); | |
737 | if (attr == NULL) | |
738 | goto nla_put_failure; | |
10708f37 JE |
739 | copy_rtnl_link_stats64(nla_data(attr), stats); |
740 | ||
ebc08a6f WM |
741 | if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent) { |
742 | int i; | |
743 | struct ifla_vf_info ivi; | |
744 | ||
745 | NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)); | |
746 | for (i = 0; i < dev_num_vf(dev->dev.parent); i++) { | |
747 | if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi)) | |
748 | break; | |
749 | NLA_PUT(skb, IFLA_VFINFO, sizeof(ivi), &ivi); | |
750 | } | |
751 | } | |
38f7b870 PM |
752 | if (dev->rtnl_link_ops) { |
753 | if (rtnl_link_fill(skb, dev) < 0) | |
754 | goto nla_put_failure; | |
755 | } | |
756 | ||
b60c5115 TG |
757 | return nlmsg_end(skb, nlh); |
758 | ||
759 | nla_put_failure: | |
26932566 PM |
760 | nlmsg_cancel(skb, nlh); |
761 | return -EMSGSIZE; | |
1da177e4 LT |
762 | } |
763 | ||
b60c5115 | 764 | static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) |
1da177e4 | 765 | { |
3b1e0a65 | 766 | struct net *net = sock_net(skb->sk); |
7c28bd0b ED |
767 | int h, s_h; |
768 | int idx = 0, s_idx; | |
1da177e4 | 769 | struct net_device *dev; |
7c28bd0b ED |
770 | struct hlist_head *head; |
771 | struct hlist_node *node; | |
772 | ||
773 | s_h = cb->args[0]; | |
774 | s_idx = cb->args[1]; | |
775 | ||
776 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { | |
777 | idx = 0; | |
778 | head = &net->dev_index_head[h]; | |
779 | hlist_for_each_entry(dev, node, head, index_hlist) { | |
780 | if (idx < s_idx) | |
781 | goto cont; | |
782 | if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, | |
783 | NETLINK_CB(cb->skb).pid, | |
784 | cb->nlh->nlmsg_seq, 0, | |
785 | NLM_F_MULTI) <= 0) | |
786 | goto out; | |
7562f876 | 787 | cont: |
7c28bd0b ED |
788 | idx++; |
789 | } | |
1da177e4 | 790 | } |
7c28bd0b ED |
791 | out: |
792 | cb->args[1] = idx; | |
793 | cb->args[0] = h; | |
1da177e4 LT |
794 | |
795 | return skb->len; | |
796 | } | |
797 | ||
e7199288 | 798 | const struct nla_policy ifla_policy[IFLA_MAX+1] = { |
5176f91e | 799 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
38f7b870 PM |
800 | [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
801 | [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, | |
5176f91e | 802 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
da5e0494 | 803 | [IFLA_MTU] = { .type = NLA_U32 }, |
76e87306 | 804 | [IFLA_LINK] = { .type = NLA_U32 }, |
da5e0494 TG |
805 | [IFLA_TXQLEN] = { .type = NLA_U32 }, |
806 | [IFLA_WEIGHT] = { .type = NLA_U32 }, | |
807 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, | |
808 | [IFLA_LINKMODE] = { .type = NLA_U8 }, | |
76e87306 | 809 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, |
d8a5ec67 | 810 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
0b815a1a | 811 | [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, |
ebc08a6f WM |
812 | [IFLA_VF_MAC] = { .type = NLA_BINARY, |
813 | .len = sizeof(struct ifla_vf_mac) }, | |
814 | [IFLA_VF_VLAN] = { .type = NLA_BINARY, | |
815 | .len = sizeof(struct ifla_vf_vlan) }, | |
816 | [IFLA_VF_TX_RATE] = { .type = NLA_BINARY, | |
817 | .len = sizeof(struct ifla_vf_tx_rate) }, | |
da5e0494 | 818 | }; |
e0d087af | 819 | EXPORT_SYMBOL(ifla_policy); |
da5e0494 | 820 | |
38f7b870 PM |
821 | static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { |
822 | [IFLA_INFO_KIND] = { .type = NLA_STRING }, | |
823 | [IFLA_INFO_DATA] = { .type = NLA_NESTED }, | |
824 | }; | |
825 | ||
81adee47 EB |
826 | struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) |
827 | { | |
828 | struct net *net; | |
829 | /* Examine the link attributes and figure out which | |
830 | * network namespace we are talking about. | |
831 | */ | |
832 | if (tb[IFLA_NET_NS_PID]) | |
833 | net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); | |
834 | else | |
835 | net = get_net(src_net); | |
836 | return net; | |
837 | } | |
838 | EXPORT_SYMBOL(rtnl_link_get_net); | |
839 | ||
1840bb13 TG |
840 | static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) |
841 | { | |
842 | if (dev) { | |
843 | if (tb[IFLA_ADDRESS] && | |
844 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) | |
845 | return -EINVAL; | |
846 | ||
847 | if (tb[IFLA_BROADCAST] && | |
848 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) | |
849 | return -EINVAL; | |
850 | } | |
851 | ||
852 | return 0; | |
853 | } | |
854 | ||
0157f60c | 855 | static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, |
38f7b870 | 856 | struct nlattr **tb, char *ifname, int modified) |
1da177e4 | 857 | { |
d314774c | 858 | const struct net_device_ops *ops = dev->netdev_ops; |
38f7b870 | 859 | int send_addr_notify = 0; |
0157f60c | 860 | int err; |
1da177e4 | 861 | |
d8a5ec67 | 862 | if (tb[IFLA_NET_NS_PID]) { |
81adee47 | 863 | struct net *net = rtnl_link_get_net(dev_net(dev), tb); |
d8a5ec67 EB |
864 | if (IS_ERR(net)) { |
865 | err = PTR_ERR(net); | |
866 | goto errout; | |
867 | } | |
868 | err = dev_change_net_namespace(dev, net, ifname); | |
869 | put_net(net); | |
870 | if (err) | |
871 | goto errout; | |
872 | modified = 1; | |
873 | } | |
874 | ||
da5e0494 | 875 | if (tb[IFLA_MAP]) { |
1da177e4 LT |
876 | struct rtnl_link_ifmap *u_map; |
877 | struct ifmap k_map; | |
878 | ||
d314774c | 879 | if (!ops->ndo_set_config) { |
1da177e4 | 880 | err = -EOPNOTSUPP; |
0157f60c | 881 | goto errout; |
1da177e4 LT |
882 | } |
883 | ||
884 | if (!netif_device_present(dev)) { | |
885 | err = -ENODEV; | |
0157f60c | 886 | goto errout; |
1da177e4 | 887 | } |
1da177e4 | 888 | |
da5e0494 | 889 | u_map = nla_data(tb[IFLA_MAP]); |
1da177e4 LT |
890 | k_map.mem_start = (unsigned long) u_map->mem_start; |
891 | k_map.mem_end = (unsigned long) u_map->mem_end; | |
892 | k_map.base_addr = (unsigned short) u_map->base_addr; | |
893 | k_map.irq = (unsigned char) u_map->irq; | |
894 | k_map.dma = (unsigned char) u_map->dma; | |
895 | k_map.port = (unsigned char) u_map->port; | |
896 | ||
d314774c | 897 | err = ops->ndo_set_config(dev, &k_map); |
da5e0494 | 898 | if (err < 0) |
0157f60c | 899 | goto errout; |
1da177e4 | 900 | |
da5e0494 | 901 | modified = 1; |
1da177e4 LT |
902 | } |
903 | ||
da5e0494 | 904 | if (tb[IFLA_ADDRESS]) { |
70f8e78e DM |
905 | struct sockaddr *sa; |
906 | int len; | |
907 | ||
d314774c | 908 | if (!ops->ndo_set_mac_address) { |
1da177e4 | 909 | err = -EOPNOTSUPP; |
0157f60c | 910 | goto errout; |
1da177e4 | 911 | } |
da5e0494 | 912 | |
1da177e4 LT |
913 | if (!netif_device_present(dev)) { |
914 | err = -ENODEV; | |
0157f60c | 915 | goto errout; |
1da177e4 | 916 | } |
1da177e4 | 917 | |
70f8e78e DM |
918 | len = sizeof(sa_family_t) + dev->addr_len; |
919 | sa = kmalloc(len, GFP_KERNEL); | |
920 | if (!sa) { | |
921 | err = -ENOMEM; | |
0157f60c | 922 | goto errout; |
70f8e78e DM |
923 | } |
924 | sa->sa_family = dev->type; | |
da5e0494 | 925 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
70f8e78e | 926 | dev->addr_len); |
d314774c | 927 | err = ops->ndo_set_mac_address(dev, sa); |
70f8e78e | 928 | kfree(sa); |
1da177e4 | 929 | if (err) |
0157f60c | 930 | goto errout; |
1da177e4 | 931 | send_addr_notify = 1; |
da5e0494 | 932 | modified = 1; |
1da177e4 LT |
933 | } |
934 | ||
da5e0494 TG |
935 | if (tb[IFLA_MTU]) { |
936 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); | |
937 | if (err < 0) | |
0157f60c | 938 | goto errout; |
da5e0494 | 939 | modified = 1; |
1da177e4 LT |
940 | } |
941 | ||
da5e0494 TG |
942 | /* |
943 | * Interface selected by interface index but interface | |
944 | * name provided implies that a name change has been | |
945 | * requested. | |
946 | */ | |
51055be8 | 947 | if (ifm->ifi_index > 0 && ifname[0]) { |
da5e0494 TG |
948 | err = dev_change_name(dev, ifname); |
949 | if (err < 0) | |
0157f60c | 950 | goto errout; |
da5e0494 | 951 | modified = 1; |
1da177e4 LT |
952 | } |
953 | ||
0b815a1a SH |
954 | if (tb[IFLA_IFALIAS]) { |
955 | err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), | |
956 | nla_len(tb[IFLA_IFALIAS])); | |
957 | if (err < 0) | |
958 | goto errout; | |
959 | modified = 1; | |
960 | } | |
961 | ||
da5e0494 TG |
962 | if (tb[IFLA_BROADCAST]) { |
963 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); | |
964 | send_addr_notify = 1; | |
1da177e4 LT |
965 | } |
966 | ||
83b496e9 | 967 | if (ifm->ifi_flags || ifm->ifi_change) { |
3729d502 | 968 | err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
5f9021cf JB |
969 | if (err < 0) |
970 | goto errout; | |
83b496e9 | 971 | } |
1da177e4 | 972 | |
93b2d4a2 DM |
973 | if (tb[IFLA_TXQLEN]) |
974 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); | |
b00055aa | 975 | |
da5e0494 | 976 | if (tb[IFLA_OPERSTATE]) |
93b2d4a2 | 977 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
b00055aa | 978 | |
da5e0494 | 979 | if (tb[IFLA_LINKMODE]) { |
93b2d4a2 DM |
980 | write_lock_bh(&dev_base_lock); |
981 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); | |
982 | write_unlock_bh(&dev_base_lock); | |
b00055aa SR |
983 | } |
984 | ||
ebc08a6f WM |
985 | if (tb[IFLA_VF_MAC]) { |
986 | struct ifla_vf_mac *ivm; | |
987 | ivm = nla_data(tb[IFLA_VF_MAC]); | |
4edb2466 | 988 | err = -EOPNOTSUPP; |
ebc08a6f WM |
989 | if (ops->ndo_set_vf_mac) |
990 | err = ops->ndo_set_vf_mac(dev, ivm->vf, ivm->mac); | |
ebc08a6f WM |
991 | if (err < 0) |
992 | goto errout; | |
993 | modified = 1; | |
994 | } | |
995 | ||
996 | if (tb[IFLA_VF_VLAN]) { | |
997 | struct ifla_vf_vlan *ivv; | |
998 | ivv = nla_data(tb[IFLA_VF_VLAN]); | |
4edb2466 | 999 | err = -EOPNOTSUPP; |
ebc08a6f WM |
1000 | if (ops->ndo_set_vf_vlan) |
1001 | err = ops->ndo_set_vf_vlan(dev, ivv->vf, | |
4edb2466 WM |
1002 | ivv->vlan, |
1003 | ivv->qos); | |
ebc08a6f WM |
1004 | if (err < 0) |
1005 | goto errout; | |
1006 | modified = 1; | |
1007 | } | |
1008 | err = 0; | |
1009 | ||
1010 | if (tb[IFLA_VF_TX_RATE]) { | |
1011 | struct ifla_vf_tx_rate *ivt; | |
1012 | ivt = nla_data(tb[IFLA_VF_TX_RATE]); | |
4edb2466 | 1013 | err = -EOPNOTSUPP; |
ebc08a6f WM |
1014 | if (ops->ndo_set_vf_tx_rate) |
1015 | err = ops->ndo_set_vf_tx_rate(dev, ivt->vf, ivt->rate); | |
ebc08a6f WM |
1016 | if (err < 0) |
1017 | goto errout; | |
1018 | modified = 1; | |
1019 | } | |
1da177e4 LT |
1020 | err = 0; |
1021 | ||
0157f60c | 1022 | errout: |
da5e0494 TG |
1023 | if (err < 0 && modified && net_ratelimit()) |
1024 | printk(KERN_WARNING "A link change request failed with " | |
1025 | "some changes comitted already. Interface %s may " | |
1026 | "have been left with an inconsistent configuration, " | |
1027 | "please check.\n", dev->name); | |
1028 | ||
1da177e4 LT |
1029 | if (send_addr_notify) |
1030 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); | |
0157f60c PM |
1031 | return err; |
1032 | } | |
1da177e4 | 1033 | |
0157f60c PM |
1034 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
1035 | { | |
3b1e0a65 | 1036 | struct net *net = sock_net(skb->sk); |
0157f60c PM |
1037 | struct ifinfomsg *ifm; |
1038 | struct net_device *dev; | |
1039 | int err; | |
1040 | struct nlattr *tb[IFLA_MAX+1]; | |
1041 | char ifname[IFNAMSIZ]; | |
1042 | ||
1043 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); | |
1044 | if (err < 0) | |
1045 | goto errout; | |
1046 | ||
1047 | if (tb[IFLA_IFNAME]) | |
1048 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
1049 | else | |
1050 | ifname[0] = '\0'; | |
1051 | ||
1052 | err = -EINVAL; | |
1053 | ifm = nlmsg_data(nlh); | |
1054 | if (ifm->ifi_index > 0) | |
a3d12891 | 1055 | dev = __dev_get_by_index(net, ifm->ifi_index); |
0157f60c | 1056 | else if (tb[IFLA_IFNAME]) |
a3d12891 | 1057 | dev = __dev_get_by_name(net, ifname); |
0157f60c PM |
1058 | else |
1059 | goto errout; | |
1060 | ||
1061 | if (dev == NULL) { | |
1062 | err = -ENODEV; | |
1063 | goto errout; | |
1064 | } | |
1065 | ||
e0d087af ED |
1066 | err = validate_linkmsg(dev, tb); |
1067 | if (err < 0) | |
a3d12891 | 1068 | goto errout; |
0157f60c | 1069 | |
38f7b870 | 1070 | err = do_setlink(dev, ifm, tb, ifname, 0); |
da5e0494 | 1071 | errout: |
1da177e4 LT |
1072 | return err; |
1073 | } | |
1074 | ||
38f7b870 PM |
1075 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
1076 | { | |
3b1e0a65 | 1077 | struct net *net = sock_net(skb->sk); |
38f7b870 PM |
1078 | const struct rtnl_link_ops *ops; |
1079 | struct net_device *dev; | |
1080 | struct ifinfomsg *ifm; | |
1081 | char ifname[IFNAMSIZ]; | |
1082 | struct nlattr *tb[IFLA_MAX+1]; | |
1083 | int err; | |
1084 | ||
1085 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); | |
1086 | if (err < 0) | |
1087 | return err; | |
1088 | ||
1089 | if (tb[IFLA_IFNAME]) | |
1090 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
1091 | ||
1092 | ifm = nlmsg_data(nlh); | |
1093 | if (ifm->ifi_index > 0) | |
881d966b | 1094 | dev = __dev_get_by_index(net, ifm->ifi_index); |
38f7b870 | 1095 | else if (tb[IFLA_IFNAME]) |
881d966b | 1096 | dev = __dev_get_by_name(net, ifname); |
38f7b870 PM |
1097 | else |
1098 | return -EINVAL; | |
1099 | ||
1100 | if (!dev) | |
1101 | return -ENODEV; | |
1102 | ||
1103 | ops = dev->rtnl_link_ops; | |
1104 | if (!ops) | |
1105 | return -EOPNOTSUPP; | |
1106 | ||
23289a37 | 1107 | ops->dellink(dev, NULL); |
38f7b870 PM |
1108 | return 0; |
1109 | } | |
1110 | ||
3729d502 PM |
1111 | int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) |
1112 | { | |
1113 | unsigned int old_flags; | |
1114 | int err; | |
1115 | ||
1116 | old_flags = dev->flags; | |
1117 | if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { | |
1118 | err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); | |
1119 | if (err < 0) | |
1120 | return err; | |
1121 | } | |
1122 | ||
1123 | dev->rtnl_link_state = RTNL_LINK_INITIALIZED; | |
1124 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); | |
1125 | ||
1126 | __dev_notify_flags(dev, old_flags); | |
1127 | return 0; | |
1128 | } | |
1129 | EXPORT_SYMBOL(rtnl_configure_link); | |
1130 | ||
81adee47 EB |
1131 | struct net_device *rtnl_create_link(struct net *src_net, struct net *net, |
1132 | char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[]) | |
e7199288 PE |
1133 | { |
1134 | int err; | |
1135 | struct net_device *dev; | |
2e59af3d ED |
1136 | unsigned int num_queues = 1; |
1137 | unsigned int real_num_queues = 1; | |
e7199288 | 1138 | |
2e59af3d | 1139 | if (ops->get_tx_queues) { |
81adee47 | 1140 | err = ops->get_tx_queues(src_net, tb, &num_queues, |
e0d087af | 1141 | &real_num_queues); |
2e59af3d ED |
1142 | if (err) |
1143 | goto err; | |
1144 | } | |
e7199288 | 1145 | err = -ENOMEM; |
2e59af3d | 1146 | dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues); |
e7199288 PE |
1147 | if (!dev) |
1148 | goto err; | |
1149 | ||
81adee47 EB |
1150 | dev_net_set(dev, net); |
1151 | dev->rtnl_link_ops = ops; | |
3729d502 | 1152 | dev->rtnl_link_state = RTNL_LINK_INITIALIZING; |
2e59af3d | 1153 | dev->real_num_tx_queues = real_num_queues; |
81adee47 | 1154 | |
e7199288 PE |
1155 | if (strchr(dev->name, '%')) { |
1156 | err = dev_alloc_name(dev, dev->name); | |
1157 | if (err < 0) | |
1158 | goto err_free; | |
1159 | } | |
1160 | ||
e7199288 PE |
1161 | if (tb[IFLA_MTU]) |
1162 | dev->mtu = nla_get_u32(tb[IFLA_MTU]); | |
1163 | if (tb[IFLA_ADDRESS]) | |
1164 | memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), | |
1165 | nla_len(tb[IFLA_ADDRESS])); | |
1166 | if (tb[IFLA_BROADCAST]) | |
1167 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), | |
1168 | nla_len(tb[IFLA_BROADCAST])); | |
1169 | if (tb[IFLA_TXQLEN]) | |
1170 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); | |
1171 | if (tb[IFLA_OPERSTATE]) | |
93b2d4a2 | 1172 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
e7199288 PE |
1173 | if (tb[IFLA_LINKMODE]) |
1174 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); | |
1175 | ||
1176 | return dev; | |
1177 | ||
1178 | err_free: | |
1179 | free_netdev(dev); | |
1180 | err: | |
1181 | return ERR_PTR(err); | |
1182 | } | |
e0d087af | 1183 | EXPORT_SYMBOL(rtnl_create_link); |
e7199288 | 1184 | |
38f7b870 PM |
1185 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
1186 | { | |
3b1e0a65 | 1187 | struct net *net = sock_net(skb->sk); |
38f7b870 PM |
1188 | const struct rtnl_link_ops *ops; |
1189 | struct net_device *dev; | |
1190 | struct ifinfomsg *ifm; | |
1191 | char kind[MODULE_NAME_LEN]; | |
1192 | char ifname[IFNAMSIZ]; | |
1193 | struct nlattr *tb[IFLA_MAX+1]; | |
1194 | struct nlattr *linkinfo[IFLA_INFO_MAX+1]; | |
1195 | int err; | |
1196 | ||
95a5afca | 1197 | #ifdef CONFIG_MODULES |
38f7b870 | 1198 | replay: |
8072f085 | 1199 | #endif |
38f7b870 PM |
1200 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
1201 | if (err < 0) | |
1202 | return err; | |
1203 | ||
1204 | if (tb[IFLA_IFNAME]) | |
1205 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
1206 | else | |
1207 | ifname[0] = '\0'; | |
1208 | ||
1209 | ifm = nlmsg_data(nlh); | |
1210 | if (ifm->ifi_index > 0) | |
881d966b | 1211 | dev = __dev_get_by_index(net, ifm->ifi_index); |
38f7b870 | 1212 | else if (ifname[0]) |
881d966b | 1213 | dev = __dev_get_by_name(net, ifname); |
38f7b870 PM |
1214 | else |
1215 | dev = NULL; | |
1216 | ||
e0d087af ED |
1217 | err = validate_linkmsg(dev, tb); |
1218 | if (err < 0) | |
1840bb13 TG |
1219 | return err; |
1220 | ||
38f7b870 PM |
1221 | if (tb[IFLA_LINKINFO]) { |
1222 | err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, | |
1223 | tb[IFLA_LINKINFO], ifla_info_policy); | |
1224 | if (err < 0) | |
1225 | return err; | |
1226 | } else | |
1227 | memset(linkinfo, 0, sizeof(linkinfo)); | |
1228 | ||
1229 | if (linkinfo[IFLA_INFO_KIND]) { | |
1230 | nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); | |
1231 | ops = rtnl_link_ops_get(kind); | |
1232 | } else { | |
1233 | kind[0] = '\0'; | |
1234 | ops = NULL; | |
1235 | } | |
1236 | ||
1237 | if (1) { | |
1238 | struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; | |
81adee47 | 1239 | struct net *dest_net; |
38f7b870 PM |
1240 | |
1241 | if (ops) { | |
1242 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { | |
1243 | err = nla_parse_nested(attr, ops->maxtype, | |
1244 | linkinfo[IFLA_INFO_DATA], | |
1245 | ops->policy); | |
1246 | if (err < 0) | |
1247 | return err; | |
1248 | data = attr; | |
1249 | } | |
1250 | if (ops->validate) { | |
1251 | err = ops->validate(tb, data); | |
1252 | if (err < 0) | |
1253 | return err; | |
1254 | } | |
1255 | } | |
1256 | ||
1257 | if (dev) { | |
1258 | int modified = 0; | |
1259 | ||
1260 | if (nlh->nlmsg_flags & NLM_F_EXCL) | |
1261 | return -EEXIST; | |
1262 | if (nlh->nlmsg_flags & NLM_F_REPLACE) | |
1263 | return -EOPNOTSUPP; | |
1264 | ||
1265 | if (linkinfo[IFLA_INFO_DATA]) { | |
1266 | if (!ops || ops != dev->rtnl_link_ops || | |
1267 | !ops->changelink) | |
1268 | return -EOPNOTSUPP; | |
1269 | ||
1270 | err = ops->changelink(dev, tb, data); | |
1271 | if (err < 0) | |
1272 | return err; | |
1273 | modified = 1; | |
1274 | } | |
1275 | ||
1276 | return do_setlink(dev, ifm, tb, ifname, modified); | |
1277 | } | |
1278 | ||
1279 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) | |
1280 | return -ENODEV; | |
1281 | ||
3729d502 | 1282 | if (ifm->ifi_index) |
38f7b870 | 1283 | return -EOPNOTSUPP; |
0e06877c | 1284 | if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) |
38f7b870 PM |
1285 | return -EOPNOTSUPP; |
1286 | ||
1287 | if (!ops) { | |
95a5afca | 1288 | #ifdef CONFIG_MODULES |
38f7b870 PM |
1289 | if (kind[0]) { |
1290 | __rtnl_unlock(); | |
1291 | request_module("rtnl-link-%s", kind); | |
1292 | rtnl_lock(); | |
1293 | ops = rtnl_link_ops_get(kind); | |
1294 | if (ops) | |
1295 | goto replay; | |
1296 | } | |
1297 | #endif | |
1298 | return -EOPNOTSUPP; | |
1299 | } | |
1300 | ||
1301 | if (!ifname[0]) | |
1302 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); | |
e7199288 | 1303 | |
81adee47 EB |
1304 | dest_net = rtnl_link_get_net(net, tb); |
1305 | dev = rtnl_create_link(net, dest_net, ifname, ops, tb); | |
e7199288 PE |
1306 | |
1307 | if (IS_ERR(dev)) | |
1308 | err = PTR_ERR(dev); | |
1309 | else if (ops->newlink) | |
81adee47 | 1310 | err = ops->newlink(net, dev, tb, data); |
2d85cba2 PM |
1311 | else |
1312 | err = register_netdevice(dev); | |
3729d502 | 1313 | if (err < 0 && !IS_ERR(dev)) { |
38f7b870 | 1314 | free_netdev(dev); |
3729d502 PM |
1315 | goto out; |
1316 | } | |
81adee47 | 1317 | |
3729d502 PM |
1318 | err = rtnl_configure_link(dev, ifm); |
1319 | if (err < 0) | |
1320 | unregister_netdevice(dev); | |
1321 | out: | |
81adee47 | 1322 | put_net(dest_net); |
38f7b870 PM |
1323 | return err; |
1324 | } | |
1325 | } | |
1326 | ||
b60c5115 | 1327 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) |
711e2c33 | 1328 | { |
3b1e0a65 | 1329 | struct net *net = sock_net(skb->sk); |
b60c5115 | 1330 | struct ifinfomsg *ifm; |
a3d12891 | 1331 | char ifname[IFNAMSIZ]; |
b60c5115 TG |
1332 | struct nlattr *tb[IFLA_MAX+1]; |
1333 | struct net_device *dev = NULL; | |
1334 | struct sk_buff *nskb; | |
339bf98f | 1335 | int err; |
711e2c33 | 1336 | |
b60c5115 TG |
1337 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
1338 | if (err < 0) | |
9918f230 | 1339 | return err; |
b60c5115 | 1340 | |
a3d12891 ED |
1341 | if (tb[IFLA_IFNAME]) |
1342 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
1343 | ||
b60c5115 | 1344 | ifm = nlmsg_data(nlh); |
a3d12891 ED |
1345 | if (ifm->ifi_index > 0) |
1346 | dev = __dev_get_by_index(net, ifm->ifi_index); | |
1347 | else if (tb[IFLA_IFNAME]) | |
1348 | dev = __dev_get_by_name(net, ifname); | |
1349 | else | |
711e2c33 | 1350 | return -EINVAL; |
711e2c33 | 1351 | |
a3d12891 ED |
1352 | if (dev == NULL) |
1353 | return -ENODEV; | |
1354 | ||
38f7b870 | 1355 | nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); |
a3d12891 ED |
1356 | if (nskb == NULL) |
1357 | return -ENOBUFS; | |
b60c5115 | 1358 | |
575c3e2a PM |
1359 | err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid, |
1360 | nlh->nlmsg_seq, 0, 0); | |
26932566 PM |
1361 | if (err < 0) { |
1362 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ | |
1363 | WARN_ON(err == -EMSGSIZE); | |
1364 | kfree_skb(nskb); | |
a3d12891 ED |
1365 | } else |
1366 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid); | |
711e2c33 | 1367 | |
b60c5115 | 1368 | return err; |
711e2c33 | 1369 | } |
711e2c33 | 1370 | |
42bad1da | 1371 | static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) |
1da177e4 LT |
1372 | { |
1373 | int idx; | |
1374 | int s_idx = cb->family; | |
1375 | ||
1376 | if (s_idx == 0) | |
1377 | s_idx = 1; | |
e0d087af | 1378 | for (idx = 1; idx < NPROTO; idx++) { |
1da177e4 LT |
1379 | int type = cb->nlh->nlmsg_type-RTM_BASE; |
1380 | if (idx < s_idx || idx == PF_PACKET) | |
1381 | continue; | |
e2849863 TG |
1382 | if (rtnl_msg_handlers[idx] == NULL || |
1383 | rtnl_msg_handlers[idx][type].dumpit == NULL) | |
1da177e4 LT |
1384 | continue; |
1385 | if (idx > s_idx) | |
1386 | memset(&cb->args[0], 0, sizeof(cb->args)); | |
e2849863 | 1387 | if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) |
1da177e4 LT |
1388 | break; |
1389 | } | |
1390 | cb->family = idx; | |
1391 | ||
1392 | return skb->len; | |
1393 | } | |
1394 | ||
1395 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) | |
1396 | { | |
c346dca1 | 1397 | struct net *net = dev_net(dev); |
1da177e4 | 1398 | struct sk_buff *skb; |
0ec6d3f4 | 1399 | int err = -ENOBUFS; |
1da177e4 | 1400 | |
38f7b870 | 1401 | skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); |
0ec6d3f4 TG |
1402 | if (skb == NULL) |
1403 | goto errout; | |
1da177e4 | 1404 | |
575c3e2a | 1405 | err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0); |
26932566 PM |
1406 | if (err < 0) { |
1407 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ | |
1408 | WARN_ON(err == -EMSGSIZE); | |
1409 | kfree_skb(skb); | |
1410 | goto errout; | |
1411 | } | |
1ce85fe4 PNA |
1412 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); |
1413 | return; | |
0ec6d3f4 TG |
1414 | errout: |
1415 | if (err < 0) | |
4b3da706 | 1416 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
1da177e4 LT |
1417 | } |
1418 | ||
1da177e4 LT |
1419 | /* Protected by RTNL sempahore. */ |
1420 | static struct rtattr **rta_buf; | |
1421 | static int rtattr_max; | |
1422 | ||
1423 | /* Process one rtnetlink message. */ | |
1424 | ||
1d00a4eb | 1425 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
1da177e4 | 1426 | { |
3b1e0a65 | 1427 | struct net *net = sock_net(skb->sk); |
e2849863 | 1428 | rtnl_doit_func doit; |
1da177e4 LT |
1429 | int sz_idx, kind; |
1430 | int min_len; | |
1431 | int family; | |
1432 | int type; | |
1c2d670f | 1433 | int err; |
1da177e4 | 1434 | |
1da177e4 | 1435 | type = nlh->nlmsg_type; |
1da177e4 | 1436 | if (type > RTM_MAX) |
038890fe | 1437 | return -EOPNOTSUPP; |
1da177e4 LT |
1438 | |
1439 | type -= RTM_BASE; | |
1440 | ||
1441 | /* All the messages must have at least 1 byte length */ | |
1442 | if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) | |
1443 | return 0; | |
1444 | ||
e0d087af | 1445 | family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family; |
1d00a4eb TG |
1446 | if (family >= NPROTO) |
1447 | return -EAFNOSUPPORT; | |
1da177e4 | 1448 | |
1da177e4 LT |
1449 | sz_idx = type>>2; |
1450 | kind = type&3; | |
1451 | ||
1d00a4eb TG |
1452 | if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) |
1453 | return -EPERM; | |
1da177e4 LT |
1454 | |
1455 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { | |
97c53cac | 1456 | struct sock *rtnl; |
e2849863 | 1457 | rtnl_dumpit_func dumpit; |
1da177e4 | 1458 | |
e2849863 TG |
1459 | dumpit = rtnl_get_dumpit(family, type); |
1460 | if (dumpit == NULL) | |
038890fe | 1461 | return -EOPNOTSUPP; |
9ac4a169 | 1462 | |
1c2d670f | 1463 | __rtnl_unlock(); |
97c53cac | 1464 | rtnl = net->rtnl; |
1c2d670f PM |
1465 | err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL); |
1466 | rtnl_lock(); | |
1467 | return err; | |
1da177e4 LT |
1468 | } |
1469 | ||
1470 | memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); | |
1471 | ||
1472 | min_len = rtm_min[sz_idx]; | |
1473 | if (nlh->nlmsg_len < min_len) | |
1d00a4eb | 1474 | return -EINVAL; |
1da177e4 LT |
1475 | |
1476 | if (nlh->nlmsg_len > min_len) { | |
1477 | int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); | |
e0d087af | 1478 | struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len); |
1da177e4 LT |
1479 | |
1480 | while (RTA_OK(attr, attrlen)) { | |
1481 | unsigned flavor = attr->rta_type; | |
1482 | if (flavor) { | |
1483 | if (flavor > rta_max[sz_idx]) | |
1d00a4eb | 1484 | return -EINVAL; |
1da177e4 LT |
1485 | rta_buf[flavor-1] = attr; |
1486 | } | |
1487 | attr = RTA_NEXT(attr, attrlen); | |
1488 | } | |
1489 | } | |
1490 | ||
e2849863 TG |
1491 | doit = rtnl_get_doit(family, type); |
1492 | if (doit == NULL) | |
038890fe | 1493 | return -EOPNOTSUPP; |
1da177e4 | 1494 | |
1d00a4eb | 1495 | return doit(skb, nlh, (void *)&rta_buf[0]); |
1da177e4 LT |
1496 | } |
1497 | ||
cd40b7d3 | 1498 | static void rtnetlink_rcv(struct sk_buff *skb) |
1da177e4 | 1499 | { |
cd40b7d3 DL |
1500 | rtnl_lock(); |
1501 | netlink_rcv_skb(skb, &rtnetlink_rcv_msg); | |
1502 | rtnl_unlock(); | |
1da177e4 LT |
1503 | } |
1504 | ||
1da177e4 LT |
1505 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
1506 | { | |
1507 | struct net_device *dev = ptr; | |
e9dc8653 | 1508 | |
1da177e4 | 1509 | switch (event) { |
1da177e4 LT |
1510 | case NETDEV_UP: |
1511 | case NETDEV_DOWN: | |
10de05af | 1512 | case NETDEV_PRE_UP: |
d90a909e EB |
1513 | case NETDEV_POST_INIT: |
1514 | case NETDEV_REGISTER: | |
1da177e4 | 1515 | case NETDEV_CHANGE: |
755d0e77 | 1516 | case NETDEV_PRE_TYPE_CHANGE: |
1da177e4 | 1517 | case NETDEV_GOING_DOWN: |
a2835763 | 1518 | case NETDEV_UNREGISTER: |
d90a909e | 1519 | case NETDEV_UNREGISTER_BATCH: |
1da177e4 LT |
1520 | break; |
1521 | default: | |
1522 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0); | |
1523 | break; | |
1524 | } | |
1525 | return NOTIFY_DONE; | |
1526 | } | |
1527 | ||
1528 | static struct notifier_block rtnetlink_dev_notifier = { | |
1529 | .notifier_call = rtnetlink_event, | |
1530 | }; | |
1531 | ||
97c53cac | 1532 | |
2c8c1e72 | 1533 | static int __net_init rtnetlink_net_init(struct net *net) |
97c53cac DL |
1534 | { |
1535 | struct sock *sk; | |
1536 | sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX, | |
1537 | rtnetlink_rcv, &rtnl_mutex, THIS_MODULE); | |
1538 | if (!sk) | |
1539 | return -ENOMEM; | |
97c53cac DL |
1540 | net->rtnl = sk; |
1541 | return 0; | |
1542 | } | |
1543 | ||
2c8c1e72 | 1544 | static void __net_exit rtnetlink_net_exit(struct net *net) |
97c53cac | 1545 | { |
775516bf DL |
1546 | netlink_kernel_release(net->rtnl); |
1547 | net->rtnl = NULL; | |
97c53cac DL |
1548 | } |
1549 | ||
1550 | static struct pernet_operations rtnetlink_net_ops = { | |
1551 | .init = rtnetlink_net_init, | |
1552 | .exit = rtnetlink_net_exit, | |
1553 | }; | |
1554 | ||
1da177e4 LT |
1555 | void __init rtnetlink_init(void) |
1556 | { | |
1557 | int i; | |
1558 | ||
1559 | rtattr_max = 0; | |
1560 | for (i = 0; i < ARRAY_SIZE(rta_max); i++) | |
1561 | if (rta_max[i] > rtattr_max) | |
1562 | rtattr_max = rta_max[i]; | |
1563 | rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); | |
1564 | if (!rta_buf) | |
1565 | panic("rtnetlink_init: cannot allocate rta_buf\n"); | |
1566 | ||
97c53cac | 1567 | if (register_pernet_subsys(&rtnetlink_net_ops)) |
1da177e4 | 1568 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
97c53cac | 1569 | |
1da177e4 LT |
1570 | netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); |
1571 | register_netdevice_notifier(&rtnetlink_dev_notifier); | |
340d17fc TG |
1572 | |
1573 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo); | |
1574 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL); | |
38f7b870 PM |
1575 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL); |
1576 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL); | |
687ad8cc TG |
1577 | |
1578 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all); | |
1579 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all); | |
1da177e4 LT |
1580 | } |
1581 |