]>
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> |
1da177e4 LT |
38 | |
39 | #include <asm/uaccess.h> | |
40 | #include <asm/system.h> | |
41 | #include <asm/string.h> | |
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> |
1da177e4 | 54 | |
e2849863 TG |
55 | struct rtnl_link |
56 | { | |
57 | rtnl_doit_func doit; | |
58 | rtnl_dumpit_func dumpit; | |
59 | }; | |
60 | ||
6756ae4b | 61 | static DEFINE_MUTEX(rtnl_mutex); |
56fc85ac | 62 | static struct sock *rtnl; |
1da177e4 LT |
63 | |
64 | void rtnl_lock(void) | |
65 | { | |
6756ae4b | 66 | mutex_lock(&rtnl_mutex); |
1da177e4 LT |
67 | } |
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 | { | |
6756ae4b SH |
76 | mutex_unlock(&rtnl_mutex); |
77 | if (rtnl && rtnl->sk_receive_queue.qlen) | |
78 | rtnl->sk_data_ready(rtnl, 0); | |
1da177e4 LT |
79 | netdev_run_todo(); |
80 | } | |
81 | ||
6756ae4b SH |
82 | int rtnl_trylock(void) |
83 | { | |
84 | return mutex_trylock(&rtnl_mutex); | |
85 | } | |
86 | ||
1da177e4 LT |
87 | int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len) |
88 | { | |
89 | memset(tb, 0, sizeof(struct rtattr*)*maxattr); | |
90 | ||
91 | while (RTA_OK(rta, len)) { | |
92 | unsigned flavor = rta->rta_type; | |
93 | if (flavor && flavor <= maxattr) | |
94 | tb[flavor-1] = rta; | |
95 | rta = RTA_NEXT(rta, len); | |
96 | } | |
97 | return 0; | |
98 | } | |
99 | ||
42bad1da | 100 | static struct rtnl_link *rtnl_msg_handlers[NPROTO]; |
e2849863 TG |
101 | |
102 | static inline int rtm_msgindex(int msgtype) | |
103 | { | |
104 | int msgindex = msgtype - RTM_BASE; | |
105 | ||
106 | /* | |
107 | * msgindex < 0 implies someone tried to register a netlink | |
108 | * control code. msgindex >= RTM_NR_MSGTYPES may indicate that | |
109 | * the message type has not been added to linux/rtnetlink.h | |
110 | */ | |
111 | BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); | |
112 | ||
113 | return msgindex; | |
114 | } | |
115 | ||
116 | static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) | |
117 | { | |
118 | struct rtnl_link *tab; | |
119 | ||
120 | tab = rtnl_msg_handlers[protocol]; | |
51057f2f | 121 | if (tab == NULL || tab[msgindex].doit == NULL) |
e2849863 TG |
122 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
123 | ||
51057f2f | 124 | return tab ? tab[msgindex].doit : NULL; |
e2849863 TG |
125 | } |
126 | ||
127 | static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) | |
128 | { | |
129 | struct rtnl_link *tab; | |
130 | ||
131 | tab = rtnl_msg_handlers[protocol]; | |
51057f2f | 132 | if (tab == NULL || tab[msgindex].dumpit == NULL) |
e2849863 TG |
133 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
134 | ||
51057f2f | 135 | return tab ? tab[msgindex].dumpit : NULL; |
e2849863 TG |
136 | } |
137 | ||
138 | /** | |
139 | * __rtnl_register - Register a rtnetlink message type | |
140 | * @protocol: Protocol family or PF_UNSPEC | |
141 | * @msgtype: rtnetlink message type | |
142 | * @doit: Function pointer called for each request message | |
143 | * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message | |
144 | * | |
145 | * Registers the specified function pointers (at least one of them has | |
146 | * to be non-NULL) to be called whenever a request message for the | |
147 | * specified protocol family and message type is received. | |
148 | * | |
149 | * The special protocol family PF_UNSPEC may be used to define fallback | |
150 | * function pointers for the case when no entry for the specific protocol | |
151 | * family exists. | |
152 | * | |
153 | * Returns 0 on success or a negative error code. | |
154 | */ | |
155 | int __rtnl_register(int protocol, int msgtype, | |
156 | rtnl_doit_func doit, rtnl_dumpit_func dumpit) | |
157 | { | |
158 | struct rtnl_link *tab; | |
159 | int msgindex; | |
160 | ||
161 | BUG_ON(protocol < 0 || protocol >= NPROTO); | |
162 | msgindex = rtm_msgindex(msgtype); | |
163 | ||
164 | tab = rtnl_msg_handlers[protocol]; | |
165 | if (tab == NULL) { | |
166 | tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); | |
167 | if (tab == NULL) | |
168 | return -ENOBUFS; | |
169 | ||
170 | rtnl_msg_handlers[protocol] = tab; | |
171 | } | |
172 | ||
173 | if (doit) | |
174 | tab[msgindex].doit = doit; | |
175 | ||
176 | if (dumpit) | |
177 | tab[msgindex].dumpit = dumpit; | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
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 | } | |
201 | ||
202 | EXPORT_SYMBOL_GPL(rtnl_register); | |
203 | ||
204 | /** | |
205 | * rtnl_unregister - Unregister a rtnetlink message type | |
206 | * @protocol: Protocol family or PF_UNSPEC | |
207 | * @msgtype: rtnetlink message type | |
208 | * | |
209 | * Returns 0 on success or a negative error code. | |
210 | */ | |
211 | int rtnl_unregister(int protocol, int msgtype) | |
212 | { | |
213 | int msgindex; | |
214 | ||
215 | BUG_ON(protocol < 0 || protocol >= NPROTO); | |
216 | msgindex = rtm_msgindex(msgtype); | |
217 | ||
218 | if (rtnl_msg_handlers[protocol] == NULL) | |
219 | return -ENOENT; | |
220 | ||
221 | rtnl_msg_handlers[protocol][msgindex].doit = NULL; | |
222 | rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; | |
223 | ||
224 | return 0; | |
225 | } | |
226 | ||
227 | EXPORT_SYMBOL_GPL(rtnl_unregister); | |
228 | ||
229 | /** | |
230 | * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol | |
231 | * @protocol : Protocol family or PF_UNSPEC | |
232 | * | |
233 | * Identical to calling rtnl_unregster() for all registered message types | |
234 | * of a certain protocol family. | |
235 | */ | |
236 | void rtnl_unregister_all(int protocol) | |
237 | { | |
238 | BUG_ON(protocol < 0 || protocol >= NPROTO); | |
239 | ||
240 | kfree(rtnl_msg_handlers[protocol]); | |
241 | rtnl_msg_handlers[protocol] = NULL; | |
242 | } | |
243 | ||
244 | EXPORT_SYMBOL_GPL(rtnl_unregister_all); | |
1da177e4 | 245 | |
db46edc6 | 246 | static const int rtm_min[RTM_NR_FAMILIES] = |
1da177e4 | 247 | { |
f90a0a74 TG |
248 | [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), |
249 | [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), | |
250 | [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), | |
14c0b97d | 251 | [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), |
f90a0a74 TG |
252 | [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
253 | [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), | |
254 | [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), | |
255 | [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), | |
f90a0a74 TG |
256 | [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
257 | [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), | |
1da177e4 LT |
258 | }; |
259 | ||
db46edc6 | 260 | static const int rta_max[RTM_NR_FAMILIES] = |
1da177e4 | 261 | { |
f90a0a74 TG |
262 | [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, |
263 | [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, | |
264 | [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, | |
14c0b97d | 265 | [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, |
f90a0a74 TG |
266 | [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, |
267 | [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, | |
268 | [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, | |
269 | [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, | |
1da177e4 LT |
270 | }; |
271 | ||
272 | void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) | |
273 | { | |
274 | struct rtattr *rta; | |
275 | int size = RTA_LENGTH(attrlen); | |
276 | ||
277 | rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size)); | |
278 | rta->rta_type = attrtype; | |
279 | rta->rta_len = size; | |
280 | memcpy(RTA_DATA(rta), data, attrlen); | |
b3563c4f | 281 | memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); |
1da177e4 LT |
282 | } |
283 | ||
284 | size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size) | |
285 | { | |
286 | size_t ret = RTA_PAYLOAD(rta); | |
287 | char *src = RTA_DATA(rta); | |
288 | ||
289 | if (ret > 0 && src[ret - 1] == '\0') | |
290 | ret--; | |
291 | if (size > 0) { | |
292 | size_t len = (ret >= size) ? size - 1 : ret; | |
293 | memset(dest, 0, size); | |
294 | memcpy(dest, src, len); | |
295 | } | |
296 | return ret; | |
297 | } | |
298 | ||
299 | int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo) | |
300 | { | |
301 | int err = 0; | |
302 | ||
ac6d439d | 303 | NETLINK_CB(skb).dst_group = group; |
1da177e4 LT |
304 | if (echo) |
305 | atomic_inc(&skb->users); | |
306 | netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); | |
307 | if (echo) | |
308 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); | |
309 | return err; | |
310 | } | |
311 | ||
2942e900 TG |
312 | int rtnl_unicast(struct sk_buff *skb, u32 pid) |
313 | { | |
314 | return nlmsg_unicast(rtnl, skb, pid); | |
315 | } | |
316 | ||
97676b6b TG |
317 | int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group, |
318 | struct nlmsghdr *nlh, gfp_t flags) | |
319 | { | |
320 | int report = 0; | |
321 | ||
322 | if (nlh) | |
323 | report = nlmsg_report(nlh); | |
324 | ||
325 | return nlmsg_notify(rtnl, skb, pid, group, report, flags); | |
326 | } | |
327 | ||
328 | void rtnl_set_sk_err(u32 group, int error) | |
329 | { | |
330 | netlink_set_err(rtnl, 0, group, error); | |
331 | } | |
332 | ||
1da177e4 LT |
333 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) |
334 | { | |
2d7202bf TG |
335 | struct nlattr *mx; |
336 | int i, valid = 0; | |
337 | ||
338 | mx = nla_nest_start(skb, RTA_METRICS); | |
339 | if (mx == NULL) | |
340 | return -ENOBUFS; | |
341 | ||
342 | for (i = 0; i < RTAX_MAX; i++) { | |
343 | if (metrics[i]) { | |
344 | valid++; | |
345 | NLA_PUT_U32(skb, i+1, metrics[i]); | |
346 | } | |
1da177e4 | 347 | } |
1da177e4 | 348 | |
a57d27fc DM |
349 | if (!valid) { |
350 | nla_nest_cancel(skb, mx); | |
351 | return 0; | |
352 | } | |
2d7202bf TG |
353 | |
354 | return nla_nest_end(skb, mx); | |
355 | ||
356 | nla_put_failure: | |
357 | return nla_nest_cancel(skb, mx); | |
1da177e4 LT |
358 | } |
359 | ||
e3703b3d TG |
360 | int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, |
361 | u32 ts, u32 tsage, long expires, u32 error) | |
362 | { | |
363 | struct rta_cacheinfo ci = { | |
364 | .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), | |
365 | .rta_used = dst->__use, | |
366 | .rta_clntref = atomic_read(&(dst->__refcnt)), | |
367 | .rta_error = error, | |
368 | .rta_id = id, | |
369 | .rta_ts = ts, | |
370 | .rta_tsage = tsage, | |
371 | }; | |
372 | ||
373 | if (expires) | |
374 | ci.rta_expires = jiffies_to_clock_t(expires); | |
375 | ||
376 | return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); | |
377 | } | |
378 | ||
379 | EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); | |
1da177e4 | 380 | |
b00055aa SR |
381 | static void set_operstate(struct net_device *dev, unsigned char transition) |
382 | { | |
383 | unsigned char operstate = dev->operstate; | |
384 | ||
385 | switch(transition) { | |
386 | case IF_OPER_UP: | |
387 | if ((operstate == IF_OPER_DORMANT || | |
388 | operstate == IF_OPER_UNKNOWN) && | |
389 | !netif_dormant(dev)) | |
390 | operstate = IF_OPER_UP; | |
391 | break; | |
392 | ||
393 | case IF_OPER_DORMANT: | |
394 | if (operstate == IF_OPER_UP || | |
395 | operstate == IF_OPER_UNKNOWN) | |
396 | operstate = IF_OPER_DORMANT; | |
397 | break; | |
3ff50b79 | 398 | } |
b00055aa SR |
399 | |
400 | if (dev->operstate != operstate) { | |
401 | write_lock_bh(&dev_base_lock); | |
402 | dev->operstate = operstate; | |
403 | write_unlock_bh(&dev_base_lock); | |
404 | netdev_state_change(dev); | |
405 | } | |
406 | } | |
407 | ||
b60c5115 TG |
408 | static void copy_rtnl_link_stats(struct rtnl_link_stats *a, |
409 | struct net_device_stats *b) | |
1da177e4 | 410 | { |
b60c5115 TG |
411 | a->rx_packets = b->rx_packets; |
412 | a->tx_packets = b->tx_packets; | |
413 | a->rx_bytes = b->rx_bytes; | |
414 | a->tx_bytes = b->tx_bytes; | |
415 | a->rx_errors = b->rx_errors; | |
416 | a->tx_errors = b->tx_errors; | |
417 | a->rx_dropped = b->rx_dropped; | |
418 | a->tx_dropped = b->tx_dropped; | |
419 | ||
420 | a->multicast = b->multicast; | |
421 | a->collisions = b->collisions; | |
422 | ||
423 | a->rx_length_errors = b->rx_length_errors; | |
424 | a->rx_over_errors = b->rx_over_errors; | |
425 | a->rx_crc_errors = b->rx_crc_errors; | |
426 | a->rx_frame_errors = b->rx_frame_errors; | |
427 | a->rx_fifo_errors = b->rx_fifo_errors; | |
428 | a->rx_missed_errors = b->rx_missed_errors; | |
429 | ||
430 | a->tx_aborted_errors = b->tx_aborted_errors; | |
431 | a->tx_carrier_errors = b->tx_carrier_errors; | |
432 | a->tx_fifo_errors = b->tx_fifo_errors; | |
433 | a->tx_heartbeat_errors = b->tx_heartbeat_errors; | |
434 | a->tx_window_errors = b->tx_window_errors; | |
435 | ||
436 | a->rx_compressed = b->rx_compressed; | |
437 | a->tx_compressed = b->tx_compressed; | |
438 | }; | |
1da177e4 | 439 | |
339bf98f TG |
440 | static inline size_t if_nlmsg_size(int iwbuflen) |
441 | { | |
442 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
443 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ | |
444 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ | |
445 | + nla_total_size(sizeof(struct rtnl_link_ifmap)) | |
446 | + nla_total_size(sizeof(struct rtnl_link_stats)) | |
447 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ | |
448 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ | |
449 | + nla_total_size(4) /* IFLA_TXQLEN */ | |
450 | + nla_total_size(4) /* IFLA_WEIGHT */ | |
451 | + nla_total_size(4) /* IFLA_MTU */ | |
452 | + nla_total_size(4) /* IFLA_LINK */ | |
453 | + nla_total_size(4) /* IFLA_MASTER */ | |
454 | + nla_total_size(1) /* IFLA_OPERSTATE */ | |
455 | + nla_total_size(1) /* IFLA_LINKMODE */ | |
456 | + nla_total_size(iwbuflen); | |
457 | } | |
458 | ||
b60c5115 TG |
459 | static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, |
460 | void *iwbuf, int iwbuflen, int type, u32 pid, | |
461 | u32 seq, u32 change, unsigned int flags) | |
462 | { | |
463 | struct ifinfomsg *ifm; | |
464 | struct nlmsghdr *nlh; | |
1da177e4 | 465 | |
b60c5115 TG |
466 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
467 | if (nlh == NULL) | |
26932566 | 468 | return -EMSGSIZE; |
1da177e4 | 469 | |
b60c5115 TG |
470 | ifm = nlmsg_data(nlh); |
471 | ifm->ifi_family = AF_UNSPEC; | |
472 | ifm->__ifi_pad = 0; | |
473 | ifm->ifi_type = dev->type; | |
474 | ifm->ifi_index = dev->ifindex; | |
475 | ifm->ifi_flags = dev_get_flags(dev); | |
476 | ifm->ifi_change = change; | |
477 | ||
478 | NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); | |
479 | NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); | |
480 | NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight); | |
481 | NLA_PUT_U8(skb, IFLA_OPERSTATE, | |
482 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN); | |
483 | NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); | |
484 | NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); | |
485 | ||
486 | if (dev->ifindex != dev->iflink) | |
487 | NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); | |
488 | ||
489 | if (dev->master) | |
490 | NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); | |
1da177e4 | 491 | |
b60c5115 TG |
492 | if (dev->qdisc_sleeping) |
493 | NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id); | |
b00055aa | 494 | |
1da177e4 LT |
495 | if (1) { |
496 | struct rtnl_link_ifmap map = { | |
497 | .mem_start = dev->mem_start, | |
498 | .mem_end = dev->mem_end, | |
499 | .base_addr = dev->base_addr, | |
500 | .irq = dev->irq, | |
501 | .dma = dev->dma, | |
502 | .port = dev->if_port, | |
503 | }; | |
b60c5115 | 504 | NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); |
1da177e4 LT |
505 | } |
506 | ||
507 | if (dev->addr_len) { | |
b60c5115 TG |
508 | NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); |
509 | NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); | |
1da177e4 LT |
510 | } |
511 | ||
512 | if (dev->get_stats) { | |
b60c5115 | 513 | struct net_device_stats *stats = dev->get_stats(dev); |
1da177e4 | 514 | if (stats) { |
b60c5115 TG |
515 | struct nlattr *attr; |
516 | ||
517 | attr = nla_reserve(skb, IFLA_STATS, | |
518 | sizeof(struct rtnl_link_stats)); | |
519 | if (attr == NULL) | |
520 | goto nla_put_failure; | |
521 | ||
522 | copy_rtnl_link_stats(nla_data(attr), stats); | |
1da177e4 LT |
523 | } |
524 | } | |
1da177e4 | 525 | |
b60c5115 TG |
526 | if (iwbuf) |
527 | NLA_PUT(skb, IFLA_WIRELESS, iwbuflen, iwbuf); | |
528 | ||
529 | return nlmsg_end(skb, nlh); | |
530 | ||
531 | nla_put_failure: | |
26932566 PM |
532 | nlmsg_cancel(skb, nlh); |
533 | return -EMSGSIZE; | |
1da177e4 LT |
534 | } |
535 | ||
b60c5115 | 536 | static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) |
1da177e4 LT |
537 | { |
538 | int idx; | |
539 | int s_idx = cb->args[0]; | |
540 | struct net_device *dev; | |
541 | ||
1da177e4 LT |
542 | for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) { |
543 | if (idx < s_idx) | |
544 | continue; | |
b60c5115 TG |
545 | if (rtnl_fill_ifinfo(skb, dev, NULL, 0, RTM_NEWLINK, |
546 | NETLINK_CB(cb->skb).pid, | |
547 | cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0) | |
1da177e4 LT |
548 | break; |
549 | } | |
1da177e4 LT |
550 | cb->args[0] = idx; |
551 | ||
552 | return skb->len; | |
553 | } | |
554 | ||
da5e0494 | 555 | static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = { |
5176f91e TG |
556 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
557 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, | |
da5e0494 TG |
558 | [IFLA_MTU] = { .type = NLA_U32 }, |
559 | [IFLA_TXQLEN] = { .type = NLA_U32 }, | |
560 | [IFLA_WEIGHT] = { .type = NLA_U32 }, | |
561 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, | |
562 | [IFLA_LINKMODE] = { .type = NLA_U8 }, | |
563 | }; | |
564 | ||
565 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) | |
1da177e4 | 566 | { |
da5e0494 | 567 | struct ifinfomsg *ifm; |
1da177e4 | 568 | struct net_device *dev; |
da5e0494 TG |
569 | int err, send_addr_notify = 0, modified = 0; |
570 | struct nlattr *tb[IFLA_MAX+1]; | |
571 | char ifname[IFNAMSIZ]; | |
1da177e4 | 572 | |
da5e0494 TG |
573 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
574 | if (err < 0) | |
575 | goto errout; | |
576 | ||
5176f91e TG |
577 | if (tb[IFLA_IFNAME]) |
578 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
78e5b891 PM |
579 | else |
580 | ifname[0] = '\0'; | |
da5e0494 TG |
581 | |
582 | err = -EINVAL; | |
583 | ifm = nlmsg_data(nlh); | |
1da177e4 LT |
584 | if (ifm->ifi_index >= 0) |
585 | dev = dev_get_by_index(ifm->ifi_index); | |
da5e0494 | 586 | else if (tb[IFLA_IFNAME]) |
1da177e4 | 587 | dev = dev_get_by_name(ifname); |
da5e0494 TG |
588 | else |
589 | goto errout; | |
1da177e4 | 590 | |
da5e0494 TG |
591 | if (dev == NULL) { |
592 | err = -ENODEV; | |
593 | goto errout; | |
594 | } | |
1da177e4 | 595 | |
da5e0494 TG |
596 | if (tb[IFLA_ADDRESS] && |
597 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) | |
598 | goto errout_dev; | |
1da177e4 | 599 | |
da5e0494 TG |
600 | if (tb[IFLA_BROADCAST] && |
601 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) | |
602 | goto errout_dev; | |
1da177e4 | 603 | |
da5e0494 | 604 | if (tb[IFLA_MAP]) { |
1da177e4 LT |
605 | struct rtnl_link_ifmap *u_map; |
606 | struct ifmap k_map; | |
607 | ||
608 | if (!dev->set_config) { | |
609 | err = -EOPNOTSUPP; | |
da5e0494 | 610 | goto errout_dev; |
1da177e4 LT |
611 | } |
612 | ||
613 | if (!netif_device_present(dev)) { | |
614 | err = -ENODEV; | |
da5e0494 | 615 | goto errout_dev; |
1da177e4 | 616 | } |
1da177e4 | 617 | |
da5e0494 | 618 | u_map = nla_data(tb[IFLA_MAP]); |
1da177e4 LT |
619 | k_map.mem_start = (unsigned long) u_map->mem_start; |
620 | k_map.mem_end = (unsigned long) u_map->mem_end; | |
621 | k_map.base_addr = (unsigned short) u_map->base_addr; | |
622 | k_map.irq = (unsigned char) u_map->irq; | |
623 | k_map.dma = (unsigned char) u_map->dma; | |
624 | k_map.port = (unsigned char) u_map->port; | |
625 | ||
626 | err = dev->set_config(dev, &k_map); | |
da5e0494 TG |
627 | if (err < 0) |
628 | goto errout_dev; | |
1da177e4 | 629 | |
da5e0494 | 630 | modified = 1; |
1da177e4 LT |
631 | } |
632 | ||
da5e0494 | 633 | if (tb[IFLA_ADDRESS]) { |
70f8e78e DM |
634 | struct sockaddr *sa; |
635 | int len; | |
636 | ||
1da177e4 LT |
637 | if (!dev->set_mac_address) { |
638 | err = -EOPNOTSUPP; | |
da5e0494 | 639 | goto errout_dev; |
1da177e4 | 640 | } |
da5e0494 | 641 | |
1da177e4 LT |
642 | if (!netif_device_present(dev)) { |
643 | err = -ENODEV; | |
da5e0494 | 644 | goto errout_dev; |
1da177e4 | 645 | } |
1da177e4 | 646 | |
70f8e78e DM |
647 | len = sizeof(sa_family_t) + dev->addr_len; |
648 | sa = kmalloc(len, GFP_KERNEL); | |
649 | if (!sa) { | |
650 | err = -ENOMEM; | |
da5e0494 | 651 | goto errout_dev; |
70f8e78e DM |
652 | } |
653 | sa->sa_family = dev->type; | |
da5e0494 | 654 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
70f8e78e DM |
655 | dev->addr_len); |
656 | err = dev->set_mac_address(dev, sa); | |
657 | kfree(sa); | |
1da177e4 | 658 | if (err) |
da5e0494 | 659 | goto errout_dev; |
1da177e4 | 660 | send_addr_notify = 1; |
da5e0494 | 661 | modified = 1; |
1da177e4 LT |
662 | } |
663 | ||
da5e0494 TG |
664 | if (tb[IFLA_MTU]) { |
665 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); | |
666 | if (err < 0) | |
667 | goto errout_dev; | |
668 | modified = 1; | |
1da177e4 LT |
669 | } |
670 | ||
da5e0494 TG |
671 | /* |
672 | * Interface selected by interface index but interface | |
673 | * name provided implies that a name change has been | |
674 | * requested. | |
675 | */ | |
676 | if (ifm->ifi_index >= 0 && ifname[0]) { | |
677 | err = dev_change_name(dev, ifname); | |
678 | if (err < 0) | |
679 | goto errout_dev; | |
680 | modified = 1; | |
1da177e4 LT |
681 | } |
682 | ||
da5e0494 TG |
683 | if (tb[IFLA_BROADCAST]) { |
684 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); | |
685 | send_addr_notify = 1; | |
1da177e4 LT |
686 | } |
687 | ||
1da177e4 | 688 | |
da5e0494 TG |
689 | if (ifm->ifi_flags) |
690 | dev_change_flags(dev, ifm->ifi_flags); | |
1da177e4 | 691 | |
da5e0494 TG |
692 | if (tb[IFLA_TXQLEN]) |
693 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); | |
b00055aa | 694 | |
da5e0494 TG |
695 | if (tb[IFLA_WEIGHT]) |
696 | dev->weight = nla_get_u32(tb[IFLA_WEIGHT]); | |
b00055aa | 697 | |
da5e0494 TG |
698 | if (tb[IFLA_OPERSTATE]) |
699 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); | |
b00055aa | 700 | |
da5e0494 | 701 | if (tb[IFLA_LINKMODE]) { |
b00055aa | 702 | write_lock_bh(&dev_base_lock); |
da5e0494 | 703 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
b00055aa SR |
704 | write_unlock_bh(&dev_base_lock); |
705 | } | |
706 | ||
1da177e4 LT |
707 | err = 0; |
708 | ||
da5e0494 TG |
709 | errout_dev: |
710 | if (err < 0 && modified && net_ratelimit()) | |
711 | printk(KERN_WARNING "A link change request failed with " | |
712 | "some changes comitted already. Interface %s may " | |
713 | "have been left with an inconsistent configuration, " | |
714 | "please check.\n", dev->name); | |
715 | ||
1da177e4 LT |
716 | if (send_addr_notify) |
717 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); | |
718 | ||
719 | dev_put(dev); | |
da5e0494 | 720 | errout: |
1da177e4 LT |
721 | return err; |
722 | } | |
723 | ||
b60c5115 | 724 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) |
711e2c33 | 725 | { |
b60c5115 TG |
726 | struct ifinfomsg *ifm; |
727 | struct nlattr *tb[IFLA_MAX+1]; | |
728 | struct net_device *dev = NULL; | |
729 | struct sk_buff *nskb; | |
730 | char *iw_buf = NULL, *iw = NULL; | |
711e2c33 | 731 | int iw_buf_len = 0; |
339bf98f | 732 | int err; |
711e2c33 | 733 | |
b60c5115 TG |
734 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
735 | if (err < 0) | |
9918f230 | 736 | return err; |
b60c5115 TG |
737 | |
738 | ifm = nlmsg_data(nlh); | |
739 | if (ifm->ifi_index >= 0) { | |
711e2c33 | 740 | dev = dev_get_by_index(ifm->ifi_index); |
b60c5115 TG |
741 | if (dev == NULL) |
742 | return -ENODEV; | |
743 | } else | |
711e2c33 | 744 | return -EINVAL; |
711e2c33 | 745 | |
339bf98f | 746 | nskb = nlmsg_new(if_nlmsg_size(iw_buf_len), GFP_KERNEL); |
b60c5115 TG |
747 | if (nskb == NULL) { |
748 | err = -ENOBUFS; | |
749 | goto errout; | |
750 | } | |
751 | ||
752 | err = rtnl_fill_ifinfo(nskb, dev, iw, iw_buf_len, RTM_NEWLINK, | |
753 | NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0, 0); | |
26932566 PM |
754 | if (err < 0) { |
755 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ | |
756 | WARN_ON(err == -EMSGSIZE); | |
757 | kfree_skb(nskb); | |
758 | goto errout; | |
759 | } | |
b974179a | 760 | err = rtnl_unicast(nskb, NETLINK_CB(skb).pid); |
b60c5115 TG |
761 | errout: |
762 | kfree(iw_buf); | |
711e2c33 | 763 | dev_put(dev); |
711e2c33 | 764 | |
b60c5115 | 765 | return err; |
711e2c33 | 766 | } |
711e2c33 | 767 | |
42bad1da | 768 | static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) |
1da177e4 LT |
769 | { |
770 | int idx; | |
771 | int s_idx = cb->family; | |
772 | ||
773 | if (s_idx == 0) | |
774 | s_idx = 1; | |
775 | for (idx=1; idx<NPROTO; idx++) { | |
776 | int type = cb->nlh->nlmsg_type-RTM_BASE; | |
777 | if (idx < s_idx || idx == PF_PACKET) | |
778 | continue; | |
e2849863 TG |
779 | if (rtnl_msg_handlers[idx] == NULL || |
780 | rtnl_msg_handlers[idx][type].dumpit == NULL) | |
1da177e4 LT |
781 | continue; |
782 | if (idx > s_idx) | |
783 | memset(&cb->args[0], 0, sizeof(cb->args)); | |
e2849863 | 784 | if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) |
1da177e4 LT |
785 | break; |
786 | } | |
787 | cb->family = idx; | |
788 | ||
789 | return skb->len; | |
790 | } | |
791 | ||
792 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) | |
793 | { | |
794 | struct sk_buff *skb; | |
0ec6d3f4 | 795 | int err = -ENOBUFS; |
1da177e4 | 796 | |
339bf98f | 797 | skb = nlmsg_new(if_nlmsg_size(0), GFP_KERNEL); |
0ec6d3f4 TG |
798 | if (skb == NULL) |
799 | goto errout; | |
1da177e4 | 800 | |
0ec6d3f4 | 801 | err = rtnl_fill_ifinfo(skb, dev, NULL, 0, type, 0, 0, change, 0); |
26932566 PM |
802 | if (err < 0) { |
803 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ | |
804 | WARN_ON(err == -EMSGSIZE); | |
805 | kfree_skb(skb); | |
806 | goto errout; | |
807 | } | |
0ec6d3f4 TG |
808 | err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); |
809 | errout: | |
810 | if (err < 0) | |
811 | rtnl_set_sk_err(RTNLGRP_LINK, err); | |
1da177e4 LT |
812 | } |
813 | ||
1da177e4 LT |
814 | /* Protected by RTNL sempahore. */ |
815 | static struct rtattr **rta_buf; | |
816 | static int rtattr_max; | |
817 | ||
818 | /* Process one rtnetlink message. */ | |
819 | ||
1d00a4eb | 820 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
1da177e4 | 821 | { |
e2849863 | 822 | rtnl_doit_func doit; |
1da177e4 LT |
823 | int sz_idx, kind; |
824 | int min_len; | |
825 | int family; | |
826 | int type; | |
1c2d670f | 827 | int err; |
1da177e4 | 828 | |
1da177e4 | 829 | type = nlh->nlmsg_type; |
1da177e4 | 830 | if (type > RTM_MAX) |
038890fe | 831 | return -EOPNOTSUPP; |
1da177e4 LT |
832 | |
833 | type -= RTM_BASE; | |
834 | ||
835 | /* All the messages must have at least 1 byte length */ | |
836 | if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) | |
837 | return 0; | |
838 | ||
839 | family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family; | |
1d00a4eb TG |
840 | if (family >= NPROTO) |
841 | return -EAFNOSUPPORT; | |
1da177e4 | 842 | |
1da177e4 LT |
843 | sz_idx = type>>2; |
844 | kind = type&3; | |
845 | ||
1d00a4eb TG |
846 | if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) |
847 | return -EPERM; | |
1da177e4 LT |
848 | |
849 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { | |
e2849863 | 850 | rtnl_dumpit_func dumpit; |
1da177e4 | 851 | |
e2849863 TG |
852 | dumpit = rtnl_get_dumpit(family, type); |
853 | if (dumpit == NULL) | |
038890fe | 854 | return -EOPNOTSUPP; |
9ac4a169 | 855 | |
1c2d670f PM |
856 | __rtnl_unlock(); |
857 | err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL); | |
858 | rtnl_lock(); | |
859 | return err; | |
1da177e4 LT |
860 | } |
861 | ||
862 | memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); | |
863 | ||
864 | min_len = rtm_min[sz_idx]; | |
865 | if (nlh->nlmsg_len < min_len) | |
1d00a4eb | 866 | return -EINVAL; |
1da177e4 LT |
867 | |
868 | if (nlh->nlmsg_len > min_len) { | |
869 | int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); | |
870 | struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len); | |
871 | ||
872 | while (RTA_OK(attr, attrlen)) { | |
873 | unsigned flavor = attr->rta_type; | |
874 | if (flavor) { | |
875 | if (flavor > rta_max[sz_idx]) | |
1d00a4eb | 876 | return -EINVAL; |
1da177e4 LT |
877 | rta_buf[flavor-1] = attr; |
878 | } | |
879 | attr = RTA_NEXT(attr, attrlen); | |
880 | } | |
881 | } | |
882 | ||
e2849863 TG |
883 | doit = rtnl_get_doit(family, type); |
884 | if (doit == NULL) | |
038890fe | 885 | return -EOPNOTSUPP; |
1da177e4 | 886 | |
1d00a4eb | 887 | return doit(skb, nlh, (void *)&rta_buf[0]); |
1da177e4 LT |
888 | } |
889 | ||
1da177e4 LT |
890 | static void rtnetlink_rcv(struct sock *sk, int len) |
891 | { | |
9ac4a169 | 892 | unsigned int qlen = 0; |
2a0a6ebe | 893 | |
1da177e4 | 894 | do { |
6756ae4b | 895 | mutex_lock(&rtnl_mutex); |
9ac4a169 | 896 | netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg); |
6756ae4b | 897 | mutex_unlock(&rtnl_mutex); |
1da177e4 LT |
898 | |
899 | netdev_run_todo(); | |
2a0a6ebe | 900 | } while (qlen); |
1da177e4 LT |
901 | } |
902 | ||
1da177e4 LT |
903 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
904 | { | |
905 | struct net_device *dev = ptr; | |
906 | switch (event) { | |
907 | case NETDEV_UNREGISTER: | |
908 | rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); | |
909 | break; | |
910 | case NETDEV_REGISTER: | |
911 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); | |
912 | break; | |
913 | case NETDEV_UP: | |
914 | case NETDEV_DOWN: | |
915 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); | |
916 | break; | |
917 | case NETDEV_CHANGE: | |
918 | case NETDEV_GOING_DOWN: | |
919 | break; | |
920 | default: | |
921 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0); | |
922 | break; | |
923 | } | |
924 | return NOTIFY_DONE; | |
925 | } | |
926 | ||
927 | static struct notifier_block rtnetlink_dev_notifier = { | |
928 | .notifier_call = rtnetlink_event, | |
929 | }; | |
930 | ||
931 | void __init rtnetlink_init(void) | |
932 | { | |
933 | int i; | |
934 | ||
935 | rtattr_max = 0; | |
936 | for (i = 0; i < ARRAY_SIZE(rta_max); i++) | |
937 | if (rta_max[i] > rtattr_max) | |
938 | rtattr_max = rta_max[i]; | |
939 | rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); | |
940 | if (!rta_buf) | |
941 | panic("rtnetlink_init: cannot allocate rta_buf\n"); | |
942 | ||
06628607 | 943 | rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv, |
1c2d670f | 944 | &rtnl_mutex, THIS_MODULE); |
1da177e4 LT |
945 | if (rtnl == NULL) |
946 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); | |
947 | netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); | |
948 | register_netdevice_notifier(&rtnetlink_dev_notifier); | |
340d17fc TG |
949 | |
950 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo); | |
951 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL); | |
687ad8cc TG |
952 | |
953 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all); | |
954 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all); | |
1da177e4 LT |
955 | } |
956 | ||
957 | EXPORT_SYMBOL(__rta_fill); | |
958 | EXPORT_SYMBOL(rtattr_strlcpy); | |
959 | EXPORT_SYMBOL(rtattr_parse); | |
1da177e4 | 960 | EXPORT_SYMBOL(rtnetlink_put_metrics); |
1da177e4 | 961 | EXPORT_SYMBOL(rtnl_lock); |
6756ae4b | 962 | EXPORT_SYMBOL(rtnl_trylock); |
1da177e4 | 963 | EXPORT_SYMBOL(rtnl_unlock); |
2942e900 | 964 | EXPORT_SYMBOL(rtnl_unicast); |
97676b6b TG |
965 | EXPORT_SYMBOL(rtnl_notify); |
966 | EXPORT_SYMBOL(rtnl_set_sk_err); |